blob: 3549945a4c6ce36e143fb06962c16d67596ef60b [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070016
Ian Rogers776ac1f2012-04-13 23:36:36 -070017#include "method_verifier.h"
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070018
Elliott Hughes1f359b02011-07-17 14:27:17 -070019#include <iostream>
20
Elliott Hughes07ed66b2012-12-12 18:34:25 -080021#include "base/logging.h"
Ian Rogers637c65b2013-05-31 11:46:00 -070022#include "base/mutex-inl.h"
Elliott Hughese222ee02012-12-13 14:41:43 -080023#include "base/stringpiece.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070024#include "class_linker.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070025#include "dex_file-inl.h"
Ian Rogersd0583802013-06-01 10:51:46 -070026#include "dex_instruction-inl.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070027#include "dex_instruction_visitor.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070028#include "gc/accounting/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:
Brian Carlstrom02c8cc62013-07-18 15:54:44 -070061 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 Rogers637c65b2013-05-31 11:46:00 -0700270 mirror_method_(method),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700271 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),
Ian Rogers637c65b2013-05-31 11:46:00 -0700277 declaring_class_(NULL),
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700278 interesting_dex_pc_(-1),
279 monitor_enter_dex_pcs_(NULL),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700280 have_pending_hard_failure_(false),
jeffhaofaf459e2012-08-31 15:32:47 -0700281 have_pending_runtime_throw_failure_(false),
jeffhaof56197c2012-03-05 18:01:54 -0800282 new_instance_count_(0),
Elliott Hughes80537bb2013-01-04 16:37:26 -0800283 monitor_enter_count_(0),
Jeff Haoee988952013-04-16 14:23:47 -0700284 can_load_classes_(can_load_classes),
Sebastien Hertz4d4adb12013-07-24 16:14:19 +0200285 allow_soft_failures_(allow_soft_failures),
286 has_check_casts_(false),
287 has_virtual_or_interface_invokes_(false) {
jeffhaof56197c2012-03-05 18:01:54 -0800288}
289
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800290void MethodVerifier::FindLocksAtDexPc(mirror::AbstractMethod* m, uint32_t dex_pc,
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800291 std::vector<uint32_t>& monitor_enter_dex_pcs) {
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700292 MethodHelper mh(m);
293 MethodVerifier verifier(&mh.GetDexFile(), mh.GetDexCache(), mh.GetClassLoader(),
294 mh.GetClassDefIndex(), mh.GetCodeItem(), m->GetDexMethodIndex(),
Jeff Haoee988952013-04-16 14:23:47 -0700295 m, m->GetAccessFlags(), false, true);
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700296 verifier.interesting_dex_pc_ = dex_pc;
297 verifier.monitor_enter_dex_pcs_ = &monitor_enter_dex_pcs;
298 verifier.FindLocksAtDexPc();
299}
300
301void MethodVerifier::FindLocksAtDexPc() {
302 CHECK(monitor_enter_dex_pcs_ != NULL);
303 CHECK(code_item_ != NULL); // This only makes sense for methods with code.
304
305 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
306 // verification. In practice, the phase we want relies on data structures set up by all the
307 // earlier passes, so we just run the full method verification and bail out early when we've
308 // got what we wanted.
309 Verify();
310}
311
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200312mirror::Field* MethodVerifier::FindAccessedFieldAtDexPc(mirror::AbstractMethod* m,
313 uint32_t dex_pc) {
314 MethodHelper mh(m);
315 MethodVerifier verifier(&mh.GetDexFile(), mh.GetDexCache(), mh.GetClassLoader(),
316 mh.GetClassDefIndex(), mh.GetCodeItem(), m->GetDexMethodIndex(),
317 m, m->GetAccessFlags(), false, true);
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200318 return verifier.FindAccessedFieldAtDexPc(dex_pc);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200319}
320
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200321mirror::Field* MethodVerifier::FindAccessedFieldAtDexPc(uint32_t dex_pc) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200322 CHECK(code_item_ != NULL); // This only makes sense for methods with code.
323
324 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
325 // verification. In practice, the phase we want relies on data structures set up by all the
326 // earlier passes, so we just run the full method verification and bail out early when we've
327 // got what we wanted.
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200328 bool success = Verify();
329 if (!success) {
330 return NULL;
331 }
332 RegisterLine* register_line = reg_table_.GetLine(dex_pc);
333 if (register_line == NULL) {
334 return NULL;
335 }
336 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
337 return GetQuickFieldAccess(inst, register_line);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200338}
339
340mirror::AbstractMethod* MethodVerifier::FindInvokedMethodAtDexPc(mirror::AbstractMethod* m,
341 uint32_t dex_pc) {
342 MethodHelper mh(m);
343 MethodVerifier verifier(&mh.GetDexFile(), mh.GetDexCache(), mh.GetClassLoader(),
344 mh.GetClassDefIndex(), mh.GetCodeItem(), m->GetDexMethodIndex(),
345 m, m->GetAccessFlags(), false, true);
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200346 return verifier.FindInvokedMethodAtDexPc(dex_pc);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200347}
348
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200349mirror::AbstractMethod* MethodVerifier::FindInvokedMethodAtDexPc(uint32_t dex_pc) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200350 CHECK(code_item_ != NULL); // This only makes sense for methods with code.
351
352 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
353 // verification. In practice, the phase we want relies on data structures set up by all the
354 // earlier passes, so we just run the full method verification and bail out early when we've
355 // got what we wanted.
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200356 bool success = Verify();
357 if (!success) {
358 return NULL;
359 }
360 RegisterLine* register_line = reg_table_.GetLine(dex_pc);
361 if (register_line == NULL) {
362 return NULL;
363 }
364 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
365 const bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
366 return GetQuickInvokedMethod(inst, register_line, is_range);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200367}
368
Ian Rogersad0b3a32012-04-16 14:50:24 -0700369bool MethodVerifier::Verify() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700370 // If there aren't any instructions, make sure that's expected, then exit successfully.
371 if (code_item_ == NULL) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700372 if ((method_access_flags_ & (kAccNative | kAccAbstract)) == 0) {
jeffhaod5347e02012-03-22 17:25:05 -0700373 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "zero-length code in concrete non-native method";
jeffhaobdb76512011-09-07 11:43:16 -0700374 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -0700375 } else {
376 return true;
jeffhaobdb76512011-09-07 11:43:16 -0700377 }
jeffhaobdb76512011-09-07 11:43:16 -0700378 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700379 // Sanity-check the register counts. ins + locals = registers, so make sure that ins <= registers.
380 if (code_item_->ins_size_ > code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700381 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad register counts (ins=" << code_item_->ins_size_
382 << " regs=" << code_item_->registers_size_;
Ian Rogersd81871c2011-10-03 13:57:23 -0700383 return false;
jeffhaobdb76512011-09-07 11:43:16 -0700384 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700385 // Allocate and initialize an array to hold instruction data.
Ian Rogers7b3ddd22013-02-21 15:19:52 -0800386 insn_flags_.reset(new InstructionFlags[code_item_->insns_size_in_code_units_]());
Ian Rogersd81871c2011-10-03 13:57:23 -0700387 // Run through the instructions and see if the width checks out.
388 bool result = ComputeWidthsAndCountOps();
389 // Flag instructions guarded by a "try" block and check exception handlers.
390 result = result && ScanTryCatchBlocks();
391 // Perform static instruction verification.
392 result = result && VerifyInstructions();
Ian Rogersad0b3a32012-04-16 14:50:24 -0700393 // Perform code-flow analysis and return.
394 return result && VerifyCodeFlow();
jeffhaoba5ebb92011-08-25 17:24:37 -0700395}
396
Ian Rogers776ac1f2012-04-13 23:36:36 -0700397std::ostream& MethodVerifier::Fail(VerifyError error) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700398 switch (error) {
399 case VERIFY_ERROR_NO_CLASS:
400 case VERIFY_ERROR_NO_FIELD:
401 case VERIFY_ERROR_NO_METHOD:
402 case VERIFY_ERROR_ACCESS_CLASS:
403 case VERIFY_ERROR_ACCESS_FIELD:
404 case VERIFY_ERROR_ACCESS_METHOD:
Ian Rogers08f753d2012-08-24 14:35:25 -0700405 case VERIFY_ERROR_INSTANTIATION:
406 case VERIFY_ERROR_CLASS_CHANGE:
Elliott Hughes4993bbc2013-01-10 15:41:25 -0800407 if (Runtime::Current()->IsCompiler() || !can_load_classes_) {
jeffhaofaf459e2012-08-31 15:32:47 -0700408 // If we're optimistically running verification at compile time, turn NO_xxx, ACCESS_xxx,
409 // class change and instantiation errors into soft verification errors so that we re-verify
410 // at runtime. We may fail to find or to agree on access because of not yet available class
411 // loaders, or class loaders that will differ at runtime. In these cases, we don't want to
412 // affect the soundness of the code being compiled. Instead, the generated code runs "slow
413 // paths" that dynamically perform the verification and cause the behavior to be that akin
414 // to an interpreter.
415 error = VERIFY_ERROR_BAD_CLASS_SOFT;
416 } else {
417 have_pending_runtime_throw_failure_ = true;
418 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700419 break;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700420 // Indication that verification should be retried at runtime.
421 case VERIFY_ERROR_BAD_CLASS_SOFT:
Jeff Haoee988952013-04-16 14:23:47 -0700422 if (!allow_soft_failures_) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700423 have_pending_hard_failure_ = true;
424 }
425 break;
jeffhaod5347e02012-03-22 17:25:05 -0700426 // Hard verification failures at compile time will still fail at runtime, so the class is
427 // marked as rejected to prevent it from being compiled.
Ian Rogersad0b3a32012-04-16 14:50:24 -0700428 case VERIFY_ERROR_BAD_CLASS_HARD: {
429 if (Runtime::Current()->IsCompiler()) {
Brian Carlstrom51c24672013-07-11 16:00:56 -0700430 ClassReference ref(dex_file_, class_def_idx_);
jeffhaod1224c72012-02-29 13:43:08 -0800431 AddRejectedClass(ref);
jeffhaod1224c72012-02-29 13:43:08 -0800432 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700433 have_pending_hard_failure_ = true;
434 break;
Ian Rogers47a05882012-02-03 12:23:33 -0800435 }
436 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700437 failures_.push_back(error);
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800438 std::string location(StringPrintf("%s: [0x%X]", PrettyMethod(dex_method_idx_, *dex_file_).c_str(),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700439 work_insn_idx_));
440 std::ostringstream* failure_message = new std::ostringstream(location);
441 failure_messages_.push_back(failure_message);
442 return *failure_message;
443}
444
445void MethodVerifier::PrependToLastFailMessage(std::string prepend) {
446 size_t failure_num = failure_messages_.size();
447 DCHECK_NE(failure_num, 0U);
448 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
449 prepend += last_fail_message->str();
450 failure_messages_[failure_num - 1] = new std::ostringstream(prepend);
451 delete last_fail_message;
452}
453
454void MethodVerifier::AppendToLastFailMessage(std::string append) {
455 size_t failure_num = failure_messages_.size();
456 DCHECK_NE(failure_num, 0U);
457 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
458 (*last_fail_message) << append;
Ian Rogers47a05882012-02-03 12:23:33 -0800459}
460
Ian Rogers776ac1f2012-04-13 23:36:36 -0700461bool MethodVerifier::ComputeWidthsAndCountOps() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700462 const uint16_t* insns = code_item_->insns_;
463 size_t insns_size = code_item_->insns_size_in_code_units_;
464 const Instruction* inst = Instruction::At(insns);
jeffhaobdb76512011-09-07 11:43:16 -0700465 size_t new_instance_count = 0;
466 size_t monitor_enter_count = 0;
Ian Rogersd81871c2011-10-03 13:57:23 -0700467 size_t dex_pc = 0;
jeffhaobdb76512011-09-07 11:43:16 -0700468
Ian Rogersd81871c2011-10-03 13:57:23 -0700469 while (dex_pc < insns_size) {
jeffhaobdb76512011-09-07 11:43:16 -0700470 Instruction::Code opcode = inst->Opcode();
471 if (opcode == Instruction::NEW_INSTANCE) {
472 new_instance_count++;
473 } else if (opcode == Instruction::MONITOR_ENTER) {
474 monitor_enter_count++;
Sebastien Hertz4d4adb12013-07-24 16:14:19 +0200475 } else if (opcode == Instruction::CHECK_CAST) {
476 has_check_casts_ = true;
477 } else if ((inst->Opcode() == Instruction::INVOKE_VIRTUAL) ||
478 (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE) ||
479 (inst->Opcode() == Instruction::INVOKE_INTERFACE) ||
480 (inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE)) {
481 has_virtual_or_interface_invokes_ = true;
jeffhaobdb76512011-09-07 11:43:16 -0700482 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700483 size_t inst_size = inst->SizeInCodeUnits();
484 insn_flags_[dex_pc].SetLengthInCodeUnits(inst_size);
485 dex_pc += inst_size;
jeffhaobdb76512011-09-07 11:43:16 -0700486 inst = inst->Next();
487 }
488
Ian Rogersd81871c2011-10-03 13:57:23 -0700489 if (dex_pc != insns_size) {
jeffhaod5347e02012-03-22 17:25:05 -0700490 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "code did not end where expected ("
491 << dex_pc << " vs. " << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700492 return false;
493 }
494
Ian Rogersd81871c2011-10-03 13:57:23 -0700495 new_instance_count_ = new_instance_count;
496 monitor_enter_count_ = monitor_enter_count;
jeffhaobdb76512011-09-07 11:43:16 -0700497 return true;
498}
499
Ian Rogers776ac1f2012-04-13 23:36:36 -0700500bool MethodVerifier::ScanTryCatchBlocks() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700501 uint32_t tries_size = code_item_->tries_size_;
jeffhaobdb76512011-09-07 11:43:16 -0700502 if (tries_size == 0) {
503 return true;
504 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700505 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Ian Rogers0571d352011-11-03 19:51:38 -0700506 const DexFile::TryItem* tries = DexFile::GetTryItems(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -0700507
508 for (uint32_t idx = 0; idx < tries_size; idx++) {
509 const DexFile::TryItem* try_item = &tries[idx];
510 uint32_t start = try_item->start_addr_;
511 uint32_t end = start + try_item->insn_count_;
jeffhaobdb76512011-09-07 11:43:16 -0700512 if ((start >= end) || (start >= insns_size) || (end > insns_size)) {
jeffhaod5347e02012-03-22 17:25:05 -0700513 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad exception entry: startAddr=" << start
514 << " endAddr=" << end << " (size=" << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700515 return false;
516 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700517 if (!insn_flags_[start].IsOpcode()) {
jeffhaod5347e02012-03-22 17:25:05 -0700518 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'try' block starts inside an instruction (" << start << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700519 return false;
520 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700521 for (uint32_t dex_pc = start; dex_pc < end;
522 dex_pc += insn_flags_[dex_pc].GetLengthInCodeUnits()) {
523 insn_flags_[dex_pc].SetInTry();
jeffhaobdb76512011-09-07 11:43:16 -0700524 }
525 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800526 // Iterate over each of the handlers to verify target addresses.
Ian Rogers0571d352011-11-03 19:51:38 -0700527 const byte* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -0700528 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700529 ClassLinker* linker = Runtime::Current()->GetClassLinker();
jeffhaobdb76512011-09-07 11:43:16 -0700530 for (uint32_t idx = 0; idx < handlers_size; idx++) {
Ian Rogers0571d352011-11-03 19:51:38 -0700531 CatchHandlerIterator iterator(handlers_ptr);
532 for (; iterator.HasNext(); iterator.Next()) {
533 uint32_t dex_pc= iterator.GetHandlerAddress();
Ian Rogersd81871c2011-10-03 13:57:23 -0700534 if (!insn_flags_[dex_pc].IsOpcode()) {
jeffhaod5347e02012-03-22 17:25:05 -0700535 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "exception handler starts at bad address (" << dex_pc << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700536 return false;
537 }
jeffhao60f83e32012-02-13 17:16:30 -0800538 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
539 if (inst->Opcode() != Instruction::MOVE_EXCEPTION) {
jeffhaod5347e02012-03-22 17:25:05 -0700540 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "exception handler doesn't start with move-exception ("
Ian Rogersad0b3a32012-04-16 14:50:24 -0700541 << dex_pc << ")";
jeffhao60f83e32012-02-13 17:16:30 -0800542 return false;
543 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700544 insn_flags_[dex_pc].SetBranchTarget();
Ian Rogers28ad40d2011-10-27 15:19:26 -0700545 // Ensure exception types are resolved so that they don't need resolution to be delivered,
546 // unresolved exception types will be ignored by exception delivery
Ian Rogers0571d352011-11-03 19:51:38 -0700547 if (iterator.GetHandlerTypeIndex() != DexFile::kDexNoIndex16) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800548 mirror::Class* exception_type = linker->ResolveType(*dex_file_,
549 iterator.GetHandlerTypeIndex(),
550 dex_cache_, class_loader_);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700551 if (exception_type == NULL) {
552 DCHECK(Thread::Current()->IsExceptionPending());
553 Thread::Current()->ClearException();
554 }
555 }
jeffhaobdb76512011-09-07 11:43:16 -0700556 }
Ian Rogers0571d352011-11-03 19:51:38 -0700557 handlers_ptr = iterator.EndDataPointer();
jeffhaobdb76512011-09-07 11:43:16 -0700558 }
jeffhaobdb76512011-09-07 11:43:16 -0700559 return true;
560}
561
Ian Rogers776ac1f2012-04-13 23:36:36 -0700562bool MethodVerifier::VerifyInstructions() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700563 const Instruction* inst = Instruction::At(code_item_->insns_);
jeffhaoba5ebb92011-08-25 17:24:37 -0700564
Ian Rogers0c7abda2012-09-19 13:33:42 -0700565 /* 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 -0700566 insn_flags_[0].SetBranchTarget();
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700567 insn_flags_[0].SetCompileTimeInfoPoint();
Ian Rogersd81871c2011-10-03 13:57:23 -0700568
569 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700570 for (uint32_t dex_pc = 0; dex_pc < insns_size;) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700571 if (!VerifyInstruction(inst, dex_pc)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700572 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -0700573 return false;
574 }
575 /* Flag instructions that are garbage collection points */
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700576 // All invoke points are marked as "Throw" points already.
577 // We are relying on this to also count all the invokes as interesting.
Ian Rogersb8c78592013-07-25 23:52:52 +0000578 if (inst->IsBranch() || inst->IsSwitch() || inst->IsThrow()) {
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700579 insn_flags_[dex_pc].SetCompileTimeInfoPoint();
Ian Rogersb8c78592013-07-25 23:52:52 +0000580 } else if (inst->IsReturn()) {
581 insn_flags_[dex_pc].SetCompileTimeInfoPointAndReturn();
Ian Rogersd81871c2011-10-03 13:57:23 -0700582 }
583 dex_pc += inst->SizeInCodeUnits();
584 inst = inst->Next();
585 }
586 return true;
587}
588
Ian Rogers776ac1f2012-04-13 23:36:36 -0700589bool MethodVerifier::VerifyInstruction(const Instruction* inst, uint32_t code_offset) {
Elliott Hughesadb8c672012-03-06 16:49:32 -0800590 DecodedInstruction dec_insn(inst);
Ian Rogersd81871c2011-10-03 13:57:23 -0700591 bool result = true;
592 switch (inst->GetVerifyTypeArgumentA()) {
593 case Instruction::kVerifyRegA:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800594 result = result && CheckRegisterIndex(dec_insn.vA);
Ian Rogersd81871c2011-10-03 13:57:23 -0700595 break;
596 case Instruction::kVerifyRegAWide:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800597 result = result && CheckWideRegisterIndex(dec_insn.vA);
Ian Rogersd81871c2011-10-03 13:57:23 -0700598 break;
599 }
600 switch (inst->GetVerifyTypeArgumentB()) {
601 case Instruction::kVerifyRegB:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800602 result = result && CheckRegisterIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700603 break;
604 case Instruction::kVerifyRegBField:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800605 result = result && CheckFieldIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700606 break;
607 case Instruction::kVerifyRegBMethod:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800608 result = result && CheckMethodIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700609 break;
610 case Instruction::kVerifyRegBNewInstance:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800611 result = result && CheckNewInstance(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700612 break;
613 case Instruction::kVerifyRegBString:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800614 result = result && CheckStringIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700615 break;
616 case Instruction::kVerifyRegBType:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800617 result = result && CheckTypeIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700618 break;
619 case Instruction::kVerifyRegBWide:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800620 result = result && CheckWideRegisterIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700621 break;
622 }
623 switch (inst->GetVerifyTypeArgumentC()) {
624 case Instruction::kVerifyRegC:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800625 result = result && CheckRegisterIndex(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700626 break;
627 case Instruction::kVerifyRegCField:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800628 result = result && CheckFieldIndex(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700629 break;
630 case Instruction::kVerifyRegCNewArray:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800631 result = result && CheckNewArray(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700632 break;
633 case Instruction::kVerifyRegCType:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800634 result = result && CheckTypeIndex(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700635 break;
636 case Instruction::kVerifyRegCWide:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800637 result = result && CheckWideRegisterIndex(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700638 break;
639 }
640 switch (inst->GetVerifyExtraFlags()) {
641 case Instruction::kVerifyArrayData:
642 result = result && CheckArrayData(code_offset);
643 break;
644 case Instruction::kVerifyBranchTarget:
645 result = result && CheckBranchTarget(code_offset);
646 break;
647 case Instruction::kVerifySwitchTargets:
648 result = result && CheckSwitchTargets(code_offset);
649 break;
650 case Instruction::kVerifyVarArg:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800651 result = result && CheckVarArgRegs(dec_insn.vA, dec_insn.arg);
Ian Rogersd81871c2011-10-03 13:57:23 -0700652 break;
653 case Instruction::kVerifyVarArgRange:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800654 result = result && CheckVarArgRangeRegs(dec_insn.vA, dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700655 break;
656 case Instruction::kVerifyError:
jeffhaod5347e02012-03-22 17:25:05 -0700657 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected opcode " << inst->Name();
Ian Rogersd81871c2011-10-03 13:57:23 -0700658 result = false;
659 break;
660 }
661 return result;
662}
663
Ian Rogers776ac1f2012-04-13 23:36:36 -0700664bool MethodVerifier::CheckRegisterIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700665 if (idx >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700666 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register index out of range (" << idx << " >= "
667 << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700668 return false;
669 }
670 return true;
671}
672
Ian Rogers776ac1f2012-04-13 23:36:36 -0700673bool MethodVerifier::CheckWideRegisterIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700674 if (idx + 1 >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700675 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "wide register index out of range (" << idx
676 << "+1 >= " << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700677 return false;
678 }
679 return true;
680}
681
Ian Rogers776ac1f2012-04-13 23:36:36 -0700682bool MethodVerifier::CheckFieldIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700683 if (idx >= dex_file_->GetHeader().field_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700684 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad field index " << idx << " (max "
685 << dex_file_->GetHeader().field_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700686 return false;
687 }
688 return true;
689}
690
Ian Rogers776ac1f2012-04-13 23:36:36 -0700691bool MethodVerifier::CheckMethodIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700692 if (idx >= dex_file_->GetHeader().method_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700693 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad method index " << idx << " (max "
694 << dex_file_->GetHeader().method_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700695 return false;
696 }
697 return true;
698}
699
Ian Rogers776ac1f2012-04-13 23:36:36 -0700700bool MethodVerifier::CheckNewInstance(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700701 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700702 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
703 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700704 return false;
705 }
706 // We don't need the actual class, just a pointer to the class name.
Ian Rogers0571d352011-11-03 19:51:38 -0700707 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -0700708 if (descriptor[0] != 'L') {
jeffhaod5347e02012-03-22 17:25:05 -0700709 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "can't call new-instance on type '" << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -0700710 return false;
711 }
712 return true;
713}
714
Ian Rogers776ac1f2012-04-13 23:36:36 -0700715bool MethodVerifier::CheckStringIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700716 if (idx >= dex_file_->GetHeader().string_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700717 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad string index " << idx << " (max "
718 << dex_file_->GetHeader().string_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700719 return false;
720 }
721 return true;
722}
723
Ian Rogers776ac1f2012-04-13 23:36:36 -0700724bool MethodVerifier::CheckTypeIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700725 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700726 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
727 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700728 return false;
729 }
730 return true;
731}
732
Ian Rogers776ac1f2012-04-13 23:36:36 -0700733bool MethodVerifier::CheckNewArray(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700734 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700735 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
736 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700737 return false;
738 }
739 int bracket_count = 0;
Ian Rogers0571d352011-11-03 19:51:38 -0700740 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -0700741 const char* cp = descriptor;
742 while (*cp++ == '[') {
743 bracket_count++;
744 }
745 if (bracket_count == 0) {
746 /* The given class must be an array type. */
jeffhaod5347e02012-03-22 17:25:05 -0700747 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "can't new-array class '" << descriptor << "' (not an array)";
Ian Rogersd81871c2011-10-03 13:57:23 -0700748 return false;
749 } else if (bracket_count > 255) {
750 /* It is illegal to create an array of more than 255 dimensions. */
jeffhaod5347e02012-03-22 17:25:05 -0700751 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "can't new-array class '" << descriptor << "' (exceeds limit)";
Ian Rogersd81871c2011-10-03 13:57:23 -0700752 return false;
753 }
754 return true;
755}
756
Ian Rogers776ac1f2012-04-13 23:36:36 -0700757bool MethodVerifier::CheckArrayData(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700758 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
759 const uint16_t* insns = code_item_->insns_ + cur_offset;
760 const uint16_t* array_data;
761 int32_t array_data_offset;
762
763 DCHECK_LT(cur_offset, insn_count);
764 /* make sure the start of the array data table is in range */
765 array_data_offset = insns[1] | (((int32_t) insns[2]) << 16);
766 if ((int32_t) cur_offset + array_data_offset < 0 ||
767 cur_offset + array_data_offset + 2 >= insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -0700768 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data start: at " << cur_offset
769 << ", data offset " << array_data_offset << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -0700770 return false;
771 }
772 /* offset to array data table is a relative branch-style offset */
773 array_data = insns + array_data_offset;
774 /* make sure the table is 32-bit aligned */
775 if ((((uint32_t) array_data) & 0x03) != 0) {
jeffhaod5347e02012-03-22 17:25:05 -0700776 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned array data table: at " << cur_offset
777 << ", data offset " << array_data_offset;
Ian Rogersd81871c2011-10-03 13:57:23 -0700778 return false;
779 }
780 uint32_t value_width = array_data[1];
Elliott Hughes398f64b2012-03-26 18:05:48 -0700781 uint32_t value_count = *reinterpret_cast<const uint32_t*>(&array_data[2]);
Ian Rogersd81871c2011-10-03 13:57:23 -0700782 uint32_t table_size = 4 + (value_width * value_count + 1) / 2;
783 /* make sure the end of the switch is in range */
784 if (cur_offset + array_data_offset + table_size > insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -0700785 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data end: at " << cur_offset
786 << ", data offset " << array_data_offset << ", end "
787 << cur_offset + array_data_offset + table_size
788 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -0700789 return false;
790 }
791 return true;
792}
793
Ian Rogers776ac1f2012-04-13 23:36:36 -0700794bool MethodVerifier::CheckBranchTarget(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700795 int32_t offset;
796 bool isConditional, selfOkay;
797 if (!GetBranchOffset(cur_offset, &offset, &isConditional, &selfOkay)) {
798 return false;
799 }
800 if (!selfOkay && offset == 0) {
Elliott Hughes398f64b2012-03-26 18:05:48 -0700801 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 -0700802 return false;
803 }
Elliott Hughes81ff3182012-03-23 20:35:56 -0700804 // Check for 32-bit overflow. This isn't strictly necessary if we can depend on the runtime
805 // to have identical "wrap-around" behavior, but it's unwise to depend on that.
Ian Rogersd81871c2011-10-03 13:57:23 -0700806 if (((int64_t) cur_offset + (int64_t) offset) != (int64_t) (cur_offset + offset)) {
Elliott Hughes398f64b2012-03-26 18:05:48 -0700807 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch target overflow " << reinterpret_cast<void*>(cur_offset) << " +" << offset;
Ian Rogersd81871c2011-10-03 13:57:23 -0700808 return false;
809 }
810 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
811 int32_t abs_offset = cur_offset + offset;
812 if (abs_offset < 0 || (uint32_t) abs_offset >= insn_count || !insn_flags_[abs_offset].IsOpcode()) {
jeffhaod5347e02012-03-22 17:25:05 -0700813 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid branch target " << offset << " (-> "
Elliott Hughes398f64b2012-03-26 18:05:48 -0700814 << reinterpret_cast<void*>(abs_offset) << ") at "
815 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -0700816 return false;
817 }
818 insn_flags_[abs_offset].SetBranchTarget();
819 return true;
820}
821
Ian Rogers776ac1f2012-04-13 23:36:36 -0700822bool MethodVerifier::GetBranchOffset(uint32_t cur_offset, int32_t* pOffset, bool* pConditional,
Ian Rogersd81871c2011-10-03 13:57:23 -0700823 bool* selfOkay) {
824 const uint16_t* insns = code_item_->insns_ + cur_offset;
825 *pConditional = false;
826 *selfOkay = false;
jeffhaoba5ebb92011-08-25 17:24:37 -0700827 switch (*insns & 0xff) {
828 case Instruction::GOTO:
829 *pOffset = ((int16_t) *insns) >> 8;
jeffhaoba5ebb92011-08-25 17:24:37 -0700830 break;
831 case Instruction::GOTO_32:
832 *pOffset = insns[1] | (((uint32_t) insns[2]) << 16);
jeffhaoba5ebb92011-08-25 17:24:37 -0700833 *selfOkay = true;
834 break;
835 case Instruction::GOTO_16:
836 *pOffset = (int16_t) insns[1];
jeffhaoba5ebb92011-08-25 17:24:37 -0700837 break;
838 case Instruction::IF_EQ:
839 case Instruction::IF_NE:
840 case Instruction::IF_LT:
841 case Instruction::IF_GE:
842 case Instruction::IF_GT:
843 case Instruction::IF_LE:
844 case Instruction::IF_EQZ:
845 case Instruction::IF_NEZ:
846 case Instruction::IF_LTZ:
847 case Instruction::IF_GEZ:
848 case Instruction::IF_GTZ:
849 case Instruction::IF_LEZ:
850 *pOffset = (int16_t) insns[1];
851 *pConditional = true;
jeffhaoba5ebb92011-08-25 17:24:37 -0700852 break;
853 default:
854 return false;
855 break;
856 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700857 return true;
858}
859
Ian Rogers776ac1f2012-04-13 23:36:36 -0700860bool MethodVerifier::CheckSwitchTargets(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700861 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700862 DCHECK_LT(cur_offset, insn_count);
Ian Rogersd81871c2011-10-03 13:57:23 -0700863 const uint16_t* insns = code_item_->insns_ + cur_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -0700864 /* make sure the start of the switch is in range */
Ian Rogersd81871c2011-10-03 13:57:23 -0700865 int32_t switch_offset = insns[1] | ((int32_t) insns[2]) << 16;
866 if ((int32_t) cur_offset + switch_offset < 0 || cur_offset + switch_offset + 2 >= insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -0700867 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch start: at " << cur_offset
868 << ", switch offset " << switch_offset << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -0700869 return false;
870 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700871 /* offset to switch table is a relative branch-style offset */
Ian Rogersd81871c2011-10-03 13:57:23 -0700872 const uint16_t* switch_insns = insns + switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -0700873 /* make sure the table is 32-bit aligned */
874 if ((((uint32_t) switch_insns) & 0x03) != 0) {
jeffhaod5347e02012-03-22 17:25:05 -0700875 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned switch table: at " << cur_offset
876 << ", switch offset " << switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -0700877 return false;
878 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700879 uint32_t switch_count = switch_insns[1];
880 int32_t keys_offset, targets_offset;
881 uint16_t expected_signature;
jeffhaoba5ebb92011-08-25 17:24:37 -0700882 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
883 /* 0=sig, 1=count, 2/3=firstKey */
884 targets_offset = 4;
885 keys_offset = -1;
886 expected_signature = Instruction::kPackedSwitchSignature;
887 } else {
888 /* 0=sig, 1=count, 2..count*2 = keys */
889 keys_offset = 2;
890 targets_offset = 2 + 2 * switch_count;
891 expected_signature = Instruction::kSparseSwitchSignature;
892 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700893 uint32_t table_size = targets_offset + switch_count * 2;
jeffhaoba5ebb92011-08-25 17:24:37 -0700894 if (switch_insns[0] != expected_signature) {
jeffhaod5347e02012-03-22 17:25:05 -0700895 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << StringPrintf("wrong signature for switch table (%x, wanted %x)",
896 switch_insns[0], expected_signature);
jeffhaoba5ebb92011-08-25 17:24:37 -0700897 return false;
898 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700899 /* make sure the end of the switch is in range */
900 if (cur_offset + switch_offset + table_size > (uint32_t) insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -0700901 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch end: at " << cur_offset << ", switch offset "
902 << switch_offset << ", end "
903 << (cur_offset + switch_offset + table_size)
904 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -0700905 return false;
906 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700907 /* for a sparse switch, verify the keys are in ascending order */
908 if (keys_offset > 0 && switch_count > 1) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700909 int32_t last_key = switch_insns[keys_offset] | (switch_insns[keys_offset + 1] << 16);
910 for (uint32_t targ = 1; targ < switch_count; targ++) {
jeffhaoba5ebb92011-08-25 17:24:37 -0700911 int32_t key = (int32_t) switch_insns[keys_offset + targ * 2] |
912 (int32_t) (switch_insns[keys_offset + targ * 2 + 1] << 16);
913 if (key <= last_key) {
jeffhaod5347e02012-03-22 17:25:05 -0700914 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid packed switch: last key=" << last_key
915 << ", this=" << key;
jeffhaoba5ebb92011-08-25 17:24:37 -0700916 return false;
917 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700918 last_key = key;
919 }
920 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700921 /* verify each switch target */
Ian Rogersd81871c2011-10-03 13:57:23 -0700922 for (uint32_t targ = 0; targ < switch_count; targ++) {
923 int32_t offset = (int32_t) switch_insns[targets_offset + targ * 2] |
924 (int32_t) (switch_insns[targets_offset + targ * 2 + 1] << 16);
925 int32_t abs_offset = cur_offset + offset;
926 if (abs_offset < 0 || abs_offset >= (int32_t) insn_count || !insn_flags_[abs_offset].IsOpcode()) {
jeffhaod5347e02012-03-22 17:25:05 -0700927 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch target " << offset << " (-> "
Elliott Hughes398f64b2012-03-26 18:05:48 -0700928 << reinterpret_cast<void*>(abs_offset) << ") at "
929 << reinterpret_cast<void*>(cur_offset) << "[" << targ << "]";
jeffhaoba5ebb92011-08-25 17:24:37 -0700930 return false;
931 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700932 insn_flags_[abs_offset].SetBranchTarget();
933 }
934 return true;
935}
936
Ian Rogers776ac1f2012-04-13 23:36:36 -0700937bool MethodVerifier::CheckVarArgRegs(uint32_t vA, uint32_t arg[]) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700938 if (vA > 5) {
jeffhaod5347e02012-03-22 17:25:05 -0700939 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << vA << ") in non-range invoke)";
Ian Rogersd81871c2011-10-03 13:57:23 -0700940 return false;
941 }
942 uint16_t registers_size = code_item_->registers_size_;
943 for (uint32_t idx = 0; idx < vA; idx++) {
jeffhao457cc512012-02-02 16:55:13 -0800944 if (arg[idx] >= registers_size) {
jeffhaod5347e02012-03-22 17:25:05 -0700945 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index (" << arg[idx]
946 << ") in non-range invoke (>= " << registers_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700947 return false;
948 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700949 }
950
951 return true;
952}
953
Ian Rogers776ac1f2012-04-13 23:36:36 -0700954bool MethodVerifier::CheckVarArgRangeRegs(uint32_t vA, uint32_t vC) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700955 uint16_t registers_size = code_item_->registers_size_;
956 // vA/vC are unsigned 8-bit/16-bit quantities for /range instructions, so there's no risk of
957 // integer overflow when adding them here.
958 if (vA + vC > registers_size) {
jeffhaod5347e02012-03-22 17:25:05 -0700959 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index " << vA << "+" << vC << " in range invoke (> "
960 << registers_size << ")";
jeffhaoba5ebb92011-08-25 17:24:37 -0700961 return false;
962 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700963 return true;
964}
965
Ian Rogers0c7abda2012-09-19 13:33:42 -0700966static const std::vector<uint8_t>* CreateLengthPrefixedDexGcMap(const std::vector<uint8_t>& gc_map) {
Brian Carlstrom75412882012-01-18 01:26:54 -0800967 std::vector<uint8_t>* length_prefixed_gc_map = new std::vector<uint8_t>;
Ian Rogers637c65b2013-05-31 11:46:00 -0700968 length_prefixed_gc_map->reserve(gc_map.size() + 4);
Brian Carlstrom75412882012-01-18 01:26:54 -0800969 length_prefixed_gc_map->push_back((gc_map.size() & 0xff000000) >> 24);
970 length_prefixed_gc_map->push_back((gc_map.size() & 0x00ff0000) >> 16);
971 length_prefixed_gc_map->push_back((gc_map.size() & 0x0000ff00) >> 8);
972 length_prefixed_gc_map->push_back((gc_map.size() & 0x000000ff) >> 0);
973 length_prefixed_gc_map->insert(length_prefixed_gc_map->end(),
974 gc_map.begin(),
975 gc_map.end());
976 DCHECK_EQ(gc_map.size() + 4, length_prefixed_gc_map->size());
977 DCHECK_EQ(gc_map.size(),
978 static_cast<size_t>((length_prefixed_gc_map->at(0) << 24) |
979 (length_prefixed_gc_map->at(1) << 16) |
980 (length_prefixed_gc_map->at(2) << 8) |
981 (length_prefixed_gc_map->at(3) << 0)));
982 return length_prefixed_gc_map;
983}
984
Ian Rogers776ac1f2012-04-13 23:36:36 -0700985bool MethodVerifier::VerifyCodeFlow() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700986 uint16_t registers_size = code_item_->registers_size_;
987 uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaobdb76512011-09-07 11:43:16 -0700988
Ian Rogersd81871c2011-10-03 13:57:23 -0700989 if (registers_size * insns_size > 4*1024*1024) {
buzbee4922ef92012-02-24 14:32:20 -0800990 LOG(WARNING) << "warning: method is huge (regs=" << registers_size
991 << " insns_size=" << insns_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700992 }
993 /* Create and initialize table holding register status */
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700994 reg_table_.Init(kTrackCompilerInterestPoints, insn_flags_.get(), insns_size, registers_size, this);
995
jeffhaobdb76512011-09-07 11:43:16 -0700996
Ian Rogersd81871c2011-10-03 13:57:23 -0700997 work_line_.reset(new RegisterLine(registers_size, this));
998 saved_line_.reset(new RegisterLine(registers_size, this));
jeffhaobdb76512011-09-07 11:43:16 -0700999
Ian Rogersd81871c2011-10-03 13:57:23 -07001000 /* Initialize register types of method arguments. */
1001 if (!SetTypesFromSignature()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001002 DCHECK_NE(failures_.size(), 0U);
1003 std::string prepend("Bad signature in ");
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001004 prepend += PrettyMethod(dex_method_idx_, *dex_file_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001005 PrependToLastFailMessage(prepend);
Ian Rogersd81871c2011-10-03 13:57:23 -07001006 return false;
1007 }
1008 /* Perform code flow verification. */
1009 if (!CodeFlowVerifyMethod()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001010 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -07001011 return false;
jeffhaobdb76512011-09-07 11:43:16 -07001012 }
1013
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02001014 // Compute information for compiler.
1015 if (Runtime::Current()->IsCompiler()) {
1016 MethodReference ref(dex_file_, dex_method_idx_);
1017 bool compile = IsCandidateForCompilation(code_item_, method_access_flags_);
1018 if (compile) {
1019 /* Generate a register map and add it to the method. */
1020 UniquePtr<const std::vector<uint8_t> > map(GenerateGcMap());
1021 if (map.get() == NULL) {
1022 DCHECK_NE(failures_.size(), 0U);
1023 return false; // Not a real failure, but a failure to encode
1024 }
1025 if (kIsDebugBuild) {
1026 VerifyGcMap(*map);
1027 }
1028 const std::vector<uint8_t>* dex_gc_map = CreateLengthPrefixedDexGcMap(*(map.get()));
1029 verifier::MethodVerifier::SetDexGcMap(ref, *dex_gc_map);
1030 }
Logan Chiendd361c92012-04-10 23:40:37 +08001031
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02001032 if (has_check_casts_) {
1033 MethodVerifier::MethodSafeCastSet* method_to_safe_casts = GenerateSafeCastSet();
1034 if(method_to_safe_casts != NULL ) {
1035 SetSafeCastMap(ref, method_to_safe_casts);
1036 }
1037 }
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07001038
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02001039 if (has_virtual_or_interface_invokes_) {
1040 MethodVerifier::PcToConcreteMethodMap* pc_to_concrete_method = GenerateDevirtMap();
1041 if(pc_to_concrete_method != NULL ) {
1042 SetDevirtMap(ref, pc_to_concrete_method);
1043 }
1044 }
Sameer Abu Asal02c42232013-04-30 12:09:45 -07001045 }
jeffhaobdb76512011-09-07 11:43:16 -07001046 return true;
1047}
1048
Ian Rogersad0b3a32012-04-16 14:50:24 -07001049std::ostream& MethodVerifier::DumpFailures(std::ostream& os) {
1050 DCHECK_EQ(failures_.size(), failure_messages_.size());
1051 for (size_t i = 0; i < failures_.size(); ++i) {
Elliott Hughesc073b072012-05-24 19:29:17 -07001052 os << failure_messages_[i]->str() << "\n";
Ian Rogersad0b3a32012-04-16 14:50:24 -07001053 }
1054 return os;
1055}
1056
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001057extern "C" void MethodVerifierGdbDump(MethodVerifier* v)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001058 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001059 v->Dump(std::cerr);
1060}
1061
Ian Rogers776ac1f2012-04-13 23:36:36 -07001062void MethodVerifier::Dump(std::ostream& os) {
jeffhaof56197c2012-03-05 18:01:54 -08001063 if (code_item_ == NULL) {
Elliott Hughesc073b072012-05-24 19:29:17 -07001064 os << "Native method\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001065 return;
jeffhaobdb76512011-09-07 11:43:16 -07001066 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001067 {
1068 os << "Register Types:\n";
1069 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
1070 std::ostream indent_os(&indent_filter);
1071 reg_types_.Dump(indent_os);
1072 }
Ian Rogersb4903572012-10-11 11:52:56 -07001073 os << "Dumping instructions and register lines:\n";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001074 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
1075 std::ostream indent_os(&indent_filter);
Ian Rogersd81871c2011-10-03 13:57:23 -07001076 const Instruction* inst = Instruction::At(code_item_->insns_);
1077 for (size_t dex_pc = 0; dex_pc < code_item_->insns_size_in_code_units_;
1078 dex_pc += insn_flags_[dex_pc].GetLengthInCodeUnits()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001079 RegisterLine* reg_line = reg_table_.GetLine(dex_pc);
1080 if (reg_line != NULL) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001081 indent_os << reg_line->Dump() << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07001082 }
Ian Rogers7b3ddd22013-02-21 15:19:52 -08001083 indent_os << StringPrintf("0x%04zx", dex_pc) << ": " << insn_flags_[dex_pc].ToString() << " ";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001084 const bool kDumpHexOfInstruction = false;
1085 if (kDumpHexOfInstruction) {
1086 indent_os << inst->DumpHex(5) << " ";
1087 }
1088 indent_os << inst->DumpString(dex_file_) << "\n";
jeffhaoba5ebb92011-08-25 17:24:37 -07001089 inst = inst->Next();
1090 }
jeffhaobdb76512011-09-07 11:43:16 -07001091}
1092
Ian Rogersd81871c2011-10-03 13:57:23 -07001093static bool IsPrimitiveDescriptor(char descriptor) {
1094 switch (descriptor) {
jeffhaobdb76512011-09-07 11:43:16 -07001095 case 'I':
1096 case 'C':
1097 case 'S':
1098 case 'B':
1099 case 'Z':
jeffhaobdb76512011-09-07 11:43:16 -07001100 case 'F':
1101 case 'D':
1102 case 'J':
Ian Rogersd81871c2011-10-03 13:57:23 -07001103 return true;
jeffhaobdb76512011-09-07 11:43:16 -07001104 default:
1105 return false;
1106 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001107}
1108
Ian Rogers776ac1f2012-04-13 23:36:36 -07001109bool MethodVerifier::SetTypesFromSignature() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001110 RegisterLine* reg_line = reg_table_.GetLine(0);
1111 int arg_start = code_item_->registers_size_ - code_item_->ins_size_;
1112 size_t expected_args = code_item_->ins_size_; /* long/double count as two */
jeffhaobdb76512011-09-07 11:43:16 -07001113
Ian Rogersd81871c2011-10-03 13:57:23 -07001114 DCHECK_GE(arg_start, 0); /* should have been verified earlier */
1115 //Include the "this" pointer.
1116 size_t cur_arg = 0;
Ian Rogersad0b3a32012-04-16 14:50:24 -07001117 if (!IsStatic()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001118 // If this is a constructor for a class other than java.lang.Object, mark the first ("this")
1119 // argument as uninitialized. This restricts field access until the superclass constructor is
1120 // called.
Ian Rogersad0b3a32012-04-16 14:50:24 -07001121 const RegType& declaring_class = GetDeclaringClass();
1122 if (IsConstructor() && !declaring_class.IsJavaLangObject()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001123 reg_line->SetRegisterType(arg_start + cur_arg,
1124 reg_types_.UninitializedThisArgument(declaring_class));
1125 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001126 reg_line->SetRegisterType(arg_start + cur_arg, declaring_class);
jeffhaobdb76512011-09-07 11:43:16 -07001127 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001128 cur_arg++;
jeffhaobdb76512011-09-07 11:43:16 -07001129 }
1130
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001131 const DexFile::ProtoId& proto_id =
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001132 dex_file_->GetMethodPrototype(dex_file_->GetMethodId(dex_method_idx_));
Ian Rogers0571d352011-11-03 19:51:38 -07001133 DexFileParameterIterator iterator(*dex_file_, proto_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07001134
1135 for (; iterator.HasNext(); iterator.Next()) {
1136 const char* descriptor = iterator.GetDescriptor();
1137 if (descriptor == NULL) {
1138 LOG(FATAL) << "Null descriptor";
1139 }
1140 if (cur_arg >= expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07001141 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1142 << " args, found more (" << descriptor << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001143 return false;
1144 }
1145 switch (descriptor[0]) {
1146 case 'L':
1147 case '[':
1148 // We assume that reference arguments are initialized. The only way it could be otherwise
1149 // (assuming the caller was verified) is if the current method is <init>, but in that case
1150 // it's effectively considered initialized the instant we reach here (in the sense that we
1151 // can return without doing anything or call virtual methods).
1152 {
Ian Rogersb4903572012-10-11 11:52:56 -07001153 const RegType& reg_type = reg_types_.FromDescriptor(class_loader_, descriptor, false);
Ian Rogers84fa0742011-10-25 18:13:30 -07001154 reg_line->SetRegisterType(arg_start + cur_arg, reg_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001155 }
1156 break;
1157 case 'Z':
1158 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Boolean());
1159 break;
1160 case 'C':
1161 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Char());
1162 break;
1163 case 'B':
1164 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Byte());
1165 break;
1166 case 'I':
1167 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Integer());
1168 break;
1169 case 'S':
1170 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Short());
1171 break;
1172 case 'F':
1173 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Float());
1174 break;
1175 case 'J':
1176 case 'D': {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001177 const RegType& lo_half = descriptor[0] == 'J' ? reg_types_.LongLo() : reg_types_.DoubleLo();
1178 const RegType& hi_half = descriptor[0] == 'J' ? reg_types_.LongHi() : reg_types_.DoubleHi();
1179 reg_line->SetRegisterTypeWide(arg_start + cur_arg, lo_half, hi_half);
Ian Rogersd81871c2011-10-03 13:57:23 -07001180 cur_arg++;
1181 break;
1182 }
1183 default:
jeffhaod5347e02012-03-22 17:25:05 -07001184 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected signature type char '" << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001185 return false;
1186 }
1187 cur_arg++;
1188 }
1189 if (cur_arg != expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07001190 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args << " arguments, found " << cur_arg;
Ian Rogersd81871c2011-10-03 13:57:23 -07001191 return false;
1192 }
1193 const char* descriptor = dex_file_->GetReturnTypeDescriptor(proto_id);
1194 // Validate return type. We don't do the type lookup; just want to make sure that it has the right
1195 // format. Only major difference from the method argument format is that 'V' is supported.
1196 bool result;
1197 if (IsPrimitiveDescriptor(descriptor[0]) || descriptor[0] == 'V') {
1198 result = descriptor[1] == '\0';
1199 } else if (descriptor[0] == '[') { // single/multi-dimensional array of object/primitive
1200 size_t i = 0;
1201 do {
1202 i++;
1203 } while (descriptor[i] == '['); // process leading [
1204 if (descriptor[i] == 'L') { // object array
1205 do {
1206 i++; // find closing ;
1207 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1208 result = descriptor[i] == ';';
1209 } else { // primitive array
1210 result = IsPrimitiveDescriptor(descriptor[i]) && descriptor[i + 1] == '\0';
1211 }
1212 } else if (descriptor[0] == 'L') {
1213 // could be more thorough here, but shouldn't be required
1214 size_t i = 0;
1215 do {
1216 i++;
1217 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1218 result = descriptor[i] == ';';
1219 } else {
1220 result = false;
1221 }
1222 if (!result) {
jeffhaod5347e02012-03-22 17:25:05 -07001223 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected char in return type descriptor '"
1224 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001225 }
1226 return result;
jeffhaobdb76512011-09-07 11:43:16 -07001227}
1228
Ian Rogers776ac1f2012-04-13 23:36:36 -07001229bool MethodVerifier::CodeFlowVerifyMethod() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001230 const uint16_t* insns = code_item_->insns_;
1231 const uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaoba5ebb92011-08-25 17:24:37 -07001232
jeffhaobdb76512011-09-07 11:43:16 -07001233 /* Begin by marking the first instruction as "changed". */
Ian Rogersd81871c2011-10-03 13:57:23 -07001234 insn_flags_[0].SetChanged();
1235 uint32_t start_guess = 0;
jeffhaoba5ebb92011-08-25 17:24:37 -07001236
jeffhaobdb76512011-09-07 11:43:16 -07001237 /* Continue until no instructions are marked "changed". */
1238 while (true) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001239 // Find the first marked one. Use "start_guess" as a way to find one quickly.
1240 uint32_t insn_idx = start_guess;
1241 for (; insn_idx < insns_size; insn_idx++) {
1242 if (insn_flags_[insn_idx].IsChanged())
jeffhaobdb76512011-09-07 11:43:16 -07001243 break;
1244 }
jeffhaobdb76512011-09-07 11:43:16 -07001245 if (insn_idx == insns_size) {
1246 if (start_guess != 0) {
1247 /* try again, starting from the top */
1248 start_guess = 0;
1249 continue;
1250 } else {
1251 /* all flags are clear */
1252 break;
1253 }
1254 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001255 // We carry the working set of registers from instruction to instruction. If this address can
1256 // be the target of a branch (or throw) instruction, or if we're skipping around chasing
1257 // "changed" flags, we need to load the set of registers from the table.
1258 // Because we always prefer to continue on to the next instruction, we should never have a
1259 // situation where we have a stray "changed" flag set on an instruction that isn't a branch
1260 // target.
1261 work_insn_idx_ = insn_idx;
1262 if (insn_flags_[insn_idx].IsBranchTarget()) {
1263 work_line_->CopyFromLine(reg_table_.GetLine(insn_idx));
jeffhaobdb76512011-09-07 11:43:16 -07001264 } else {
1265#ifndef NDEBUG
1266 /*
1267 * Sanity check: retrieve the stored register line (assuming
1268 * a full table) and make sure it actually matches.
1269 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001270 RegisterLine* register_line = reg_table_.GetLine(insn_idx);
1271 if (register_line != NULL) {
1272 if (work_line_->CompareLine(register_line) != 0) {
1273 Dump(std::cout);
1274 std::cout << info_messages_.str();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001275 LOG(FATAL) << "work_line diverged in " << PrettyMethod(dex_method_idx_, *dex_file_)
Elliott Hughesc073b072012-05-24 19:29:17 -07001276 << "@" << reinterpret_cast<void*>(work_insn_idx_) << "\n"
1277 << " work_line=" << *work_line_ << "\n"
Elliott Hughes398f64b2012-03-26 18:05:48 -07001278 << " expected=" << *register_line;
Ian Rogersd81871c2011-10-03 13:57:23 -07001279 }
jeffhaobdb76512011-09-07 11:43:16 -07001280 }
1281#endif
1282 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001283 if (!CodeFlowVerifyInstruction(&start_guess)) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001284 std::string prepend(PrettyMethod(dex_method_idx_, *dex_file_));
Ian Rogersad0b3a32012-04-16 14:50:24 -07001285 prepend += " failed to verify: ";
1286 PrependToLastFailMessage(prepend);
jeffhaoba5ebb92011-08-25 17:24:37 -07001287 return false;
1288 }
jeffhaobdb76512011-09-07 11:43:16 -07001289 /* Clear "changed" and mark as visited. */
Ian Rogersd81871c2011-10-03 13:57:23 -07001290 insn_flags_[insn_idx].SetVisited();
1291 insn_flags_[insn_idx].ClearChanged();
jeffhaobdb76512011-09-07 11:43:16 -07001292 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001293
Ian Rogers1c849e52012-06-28 14:00:33 -07001294 if (gDebugVerify) {
jeffhaobdb76512011-09-07 11:43:16 -07001295 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001296 * Scan for dead code. There's nothing "evil" about dead code
jeffhaobdb76512011-09-07 11:43:16 -07001297 * (besides the wasted space), but it indicates a flaw somewhere
1298 * down the line, possibly in the verifier.
1299 *
1300 * If we've substituted "always throw" instructions into the stream,
1301 * we are almost certainly going to have some dead code.
1302 */
1303 int dead_start = -1;
Ian Rogersd81871c2011-10-03 13:57:23 -07001304 uint32_t insn_idx = 0;
1305 for (; insn_idx < insns_size; insn_idx += insn_flags_[insn_idx].GetLengthInCodeUnits()) {
jeffhaobdb76512011-09-07 11:43:16 -07001306 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001307 * Switch-statement data doesn't get "visited" by scanner. It
jeffhaobdb76512011-09-07 11:43:16 -07001308 * may or may not be preceded by a padding NOP (for alignment).
1309 */
1310 if (insns[insn_idx] == Instruction::kPackedSwitchSignature ||
1311 insns[insn_idx] == Instruction::kSparseSwitchSignature ||
1312 insns[insn_idx] == Instruction::kArrayDataSignature ||
Elliott Hughes380aaa72012-07-09 14:33:15 -07001313 (insns[insn_idx] == Instruction::NOP && (insn_idx + 1 < insns_size) &&
jeffhaobdb76512011-09-07 11:43:16 -07001314 (insns[insn_idx + 1] == Instruction::kPackedSwitchSignature ||
1315 insns[insn_idx + 1] == Instruction::kSparseSwitchSignature ||
1316 insns[insn_idx + 1] == Instruction::kArrayDataSignature))) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001317 insn_flags_[insn_idx].SetVisited();
jeffhaobdb76512011-09-07 11:43:16 -07001318 }
1319
Ian Rogersd81871c2011-10-03 13:57:23 -07001320 if (!insn_flags_[insn_idx].IsVisited()) {
jeffhaobdb76512011-09-07 11:43:16 -07001321 if (dead_start < 0)
1322 dead_start = insn_idx;
1323 } else if (dead_start >= 0) {
Elliott Hughes398f64b2012-03-26 18:05:48 -07001324 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start) << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaobdb76512011-09-07 11:43:16 -07001325 dead_start = -1;
1326 }
1327 }
1328 if (dead_start >= 0) {
Elliott Hughes398f64b2012-03-26 18:05:48 -07001329 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start) << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaoba5ebb92011-08-25 17:24:37 -07001330 }
Ian Rogersc9e463c2013-06-05 16:52:26 -07001331 // To dump the state of the verify after a method, do something like:
1332 // if (PrettyMethod(dex_method_idx_, *dex_file_) ==
1333 // "boolean java.lang.String.equals(java.lang.Object)") {
1334 // LOG(INFO) << info_messages_.str();
1335 // }
jeffhaoba5ebb92011-08-25 17:24:37 -07001336 }
jeffhaobdb76512011-09-07 11:43:16 -07001337 return true;
1338}
1339
Ian Rogers776ac1f2012-04-13 23:36:36 -07001340bool MethodVerifier::CodeFlowVerifyInstruction(uint32_t* start_guess) {
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001341 // If we're doing FindLocksAtDexPc, check whether we're at the dex pc we care about.
1342 // We want the state _before_ the instruction, for the case where the dex pc we're
1343 // interested in is itself a monitor-enter instruction (which is a likely place
1344 // for a thread to be suspended).
1345 if (monitor_enter_dex_pcs_ != NULL && work_insn_idx_ == interesting_dex_pc_) {
Elliott Hughes4993bbc2013-01-10 15:41:25 -08001346 monitor_enter_dex_pcs_->clear(); // The new work line is more accurate than the previous one.
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001347 for (size_t i = 0; i < work_line_->GetMonitorEnterCount(); ++i) {
1348 monitor_enter_dex_pcs_->push_back(work_line_->GetMonitorEnterDexPc(i));
1349 }
1350 }
1351
jeffhaobdb76512011-09-07 11:43:16 -07001352 /*
1353 * Once we finish decoding the instruction, we need to figure out where
jeffhaod1f0fde2011-09-08 17:25:33 -07001354 * we can go from here. There are three possible ways to transfer
jeffhaobdb76512011-09-07 11:43:16 -07001355 * control to another statement:
1356 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001357 * (1) Continue to the next instruction. Applies to all but
jeffhaobdb76512011-09-07 11:43:16 -07001358 * unconditional branches, method returns, and exception throws.
jeffhaod1f0fde2011-09-08 17:25:33 -07001359 * (2) Branch to one or more possible locations. Applies to branches
jeffhaobdb76512011-09-07 11:43:16 -07001360 * and switch statements.
jeffhaod1f0fde2011-09-08 17:25:33 -07001361 * (3) Exception handlers. Applies to any instruction that can
jeffhaobdb76512011-09-07 11:43:16 -07001362 * throw an exception that is handled by an encompassing "try"
1363 * block.
1364 *
1365 * We can also return, in which case there is no successor instruction
1366 * from this point.
1367 *
Elliott Hughesadb8c672012-03-06 16:49:32 -08001368 * The behavior can be determined from the opcode flags.
jeffhaobdb76512011-09-07 11:43:16 -07001369 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001370 const uint16_t* insns = code_item_->insns_ + work_insn_idx_;
1371 const Instruction* inst = Instruction::At(insns);
Ian Rogersa75a0132012-09-28 11:41:42 -07001372 int opcode_flags = Instruction::FlagsOf(inst->Opcode());
jeffhaobdb76512011-09-07 11:43:16 -07001373
jeffhaobdb76512011-09-07 11:43:16 -07001374 int32_t branch_target = 0;
jeffhaobdb76512011-09-07 11:43:16 -07001375 bool just_set_result = false;
Ian Rogers2c8a8572011-10-24 17:11:36 -07001376 if (gDebugVerify) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001377 // Generate processing back trace to debug verifier
Elliott Hughesc073b072012-05-24 19:29:17 -07001378 LogVerifyInfo() << "Processing " << inst->DumpString(dex_file_) << "\n"
1379 << *work_line_.get() << "\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001380 }
jeffhaobdb76512011-09-07 11:43:16 -07001381
1382 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001383 * Make a copy of the previous register state. If the instruction
jeffhaobdb76512011-09-07 11:43:16 -07001384 * can throw an exception, we will copy/merge this into the "catch"
1385 * address rather than work_line, because we don't want the result
1386 * from the "successful" code path (e.g. a check-cast that "improves"
1387 * a type) to be visible to the exception handler.
1388 */
Ian Rogers776ac1f2012-04-13 23:36:36 -07001389 if ((opcode_flags & Instruction::kThrow) != 0 && CurrentInsnFlags()->IsInTry()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001390 saved_line_->CopyFromLine(work_line_.get());
jeffhaobdb76512011-09-07 11:43:16 -07001391 } else {
1392#ifndef NDEBUG
Ian Rogersd81871c2011-10-03 13:57:23 -07001393 saved_line_->FillWithGarbage();
jeffhaobdb76512011-09-07 11:43:16 -07001394#endif
1395 }
1396
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07001397
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001398 // We need to ensure the work line is consistent while performing validation. When we spot a
1399 // peephole pattern we compute a new line for either the fallthrough instruction or the
1400 // branch target.
1401 UniquePtr<RegisterLine> branch_line;
1402 UniquePtr<RegisterLine> fallthrough_line;
1403
Sebastien Hertz5243e912013-05-21 10:55:07 +02001404 switch (inst->Opcode()) {
jeffhaobdb76512011-09-07 11:43:16 -07001405 case Instruction::NOP:
1406 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001407 * A "pure" NOP has no effect on anything. Data tables start with
jeffhaobdb76512011-09-07 11:43:16 -07001408 * a signature that looks like a NOP; if we see one of these in
1409 * the course of executing code then we have a problem.
1410 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001411 if (inst->VRegA_10x() != 0) {
jeffhaod5347e02012-03-22 17:25:05 -07001412 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "encountered data table in instruction stream";
jeffhaobdb76512011-09-07 11:43:16 -07001413 }
1414 break;
1415
1416 case Instruction::MOVE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001417 work_line_->CopyRegister1(inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategory1nr);
1418 break;
jeffhaobdb76512011-09-07 11:43:16 -07001419 case Instruction::MOVE_FROM16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001420 work_line_->CopyRegister1(inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategory1nr);
1421 break;
jeffhaobdb76512011-09-07 11:43:16 -07001422 case Instruction::MOVE_16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001423 work_line_->CopyRegister1(inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategory1nr);
jeffhaobdb76512011-09-07 11:43:16 -07001424 break;
1425 case Instruction::MOVE_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001426 work_line_->CopyRegister2(inst->VRegA_12x(), inst->VRegB_12x());
1427 break;
jeffhaobdb76512011-09-07 11:43:16 -07001428 case Instruction::MOVE_WIDE_FROM16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001429 work_line_->CopyRegister2(inst->VRegA_22x(), inst->VRegB_22x());
1430 break;
jeffhaobdb76512011-09-07 11:43:16 -07001431 case Instruction::MOVE_WIDE_16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001432 work_line_->CopyRegister2(inst->VRegA_32x(), inst->VRegB_32x());
jeffhaobdb76512011-09-07 11:43:16 -07001433 break;
1434 case Instruction::MOVE_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001435 work_line_->CopyRegister1(inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategoryRef);
1436 break;
jeffhaobdb76512011-09-07 11:43:16 -07001437 case Instruction::MOVE_OBJECT_FROM16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001438 work_line_->CopyRegister1(inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategoryRef);
1439 break;
jeffhaobdb76512011-09-07 11:43:16 -07001440 case Instruction::MOVE_OBJECT_16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001441 work_line_->CopyRegister1(inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategoryRef);
jeffhaobdb76512011-09-07 11:43:16 -07001442 break;
1443
1444 /*
1445 * The move-result instructions copy data out of a "pseudo-register"
jeffhaod1f0fde2011-09-08 17:25:33 -07001446 * with the results from the last method invocation. In practice we
jeffhaobdb76512011-09-07 11:43:16 -07001447 * might want to hold the result in an actual CPU register, so the
1448 * Dalvik spec requires that these only appear immediately after an
1449 * invoke or filled-new-array.
1450 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001451 * These calls invalidate the "result" register. (This is now
jeffhaobdb76512011-09-07 11:43:16 -07001452 * redundant with the reset done below, but it can make the debug info
1453 * easier to read in some cases.)
1454 */
1455 case Instruction::MOVE_RESULT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001456 work_line_->CopyResultRegister1(inst->VRegA_11x(), false);
jeffhaobdb76512011-09-07 11:43:16 -07001457 break;
1458 case Instruction::MOVE_RESULT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001459 work_line_->CopyResultRegister2(inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07001460 break;
1461 case Instruction::MOVE_RESULT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001462 work_line_->CopyResultRegister1(inst->VRegA_11x(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001463 break;
1464
Ian Rogersd81871c2011-10-03 13:57:23 -07001465 case Instruction::MOVE_EXCEPTION: {
jeffhaobdb76512011-09-07 11:43:16 -07001466 /*
jeffhao60f83e32012-02-13 17:16:30 -08001467 * This statement can only appear as the first instruction in an exception handler. We verify
1468 * that as part of extracting the exception type from the catch block list.
jeffhaobdb76512011-09-07 11:43:16 -07001469 */
Ian Rogers28ad40d2011-10-27 15:19:26 -07001470 const RegType& res_type = GetCaughtExceptionType();
Sebastien Hertz5243e912013-05-21 10:55:07 +02001471 work_line_->SetRegisterType(inst->VRegA_11x(), res_type);
jeffhaobdb76512011-09-07 11:43:16 -07001472 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001473 }
jeffhaobdb76512011-09-07 11:43:16 -07001474 case Instruction::RETURN_VOID:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001475 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
1476 if (!GetMethodReturnType().IsConflict()) {
jeffhaod5347e02012-03-22 17:25:05 -07001477 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001478 }
jeffhaobdb76512011-09-07 11:43:16 -07001479 }
1480 break;
1481 case Instruction::RETURN:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001482 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
jeffhaobdb76512011-09-07 11:43:16 -07001483 /* check the method signature */
Ian Rogersd81871c2011-10-03 13:57:23 -07001484 const RegType& return_type = GetMethodReturnType();
1485 if (!return_type.IsCategory1Types()) {
jeffhaod5347e02012-03-22 17:25:05 -07001486 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected non-category 1 return type " << return_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001487 } else {
1488 // Compilers may generate synthetic functions that write byte values into boolean fields.
1489 // Also, it may use integer values for boolean, byte, short, and character return types.
Sebastien Hertz5243e912013-05-21 10:55:07 +02001490 const uint32_t vregA = inst->VRegA_11x();
1491 const RegType& src_type = work_line_->GetRegisterType(vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07001492 bool use_src = ((return_type.IsBoolean() && src_type.IsByte()) ||
1493 ((return_type.IsBoolean() || return_type.IsByte() ||
1494 return_type.IsShort() || return_type.IsChar()) &&
1495 src_type.IsInteger()));
1496 /* check the register contents */
Ian Rogersad0b3a32012-04-16 14:50:24 -07001497 bool success =
Sebastien Hertz5243e912013-05-21 10:55:07 +02001498 work_line_->VerifyRegisterType(vregA, use_src ? src_type : return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001499 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001500 AppendToLastFailMessage(StringPrintf(" return-1nr on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07001501 }
jeffhaobdb76512011-09-07 11:43:16 -07001502 }
1503 }
1504 break;
1505 case Instruction::RETURN_WIDE:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001506 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
jeffhaobdb76512011-09-07 11:43:16 -07001507 /* check the method signature */
Ian Rogersd81871c2011-10-03 13:57:23 -07001508 const RegType& return_type = GetMethodReturnType();
1509 if (!return_type.IsCategory2Types()) {
jeffhaod5347e02012-03-22 17:25:05 -07001510 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-wide not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001511 } else {
1512 /* check the register contents */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001513 const uint32_t vregA = inst->VRegA_11x();
1514 bool success = work_line_->VerifyRegisterType(vregA, return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001515 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001516 AppendToLastFailMessage(StringPrintf(" return-wide on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07001517 }
jeffhaobdb76512011-09-07 11:43:16 -07001518 }
1519 }
1520 break;
1521 case Instruction::RETURN_OBJECT:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001522 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001523 const RegType& return_type = GetMethodReturnType();
1524 if (!return_type.IsReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001525 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-object not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001526 } else {
1527 /* return_type is the *expected* return type, not register value */
1528 DCHECK(!return_type.IsZero());
1529 DCHECK(!return_type.IsUninitializedReference());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001530 const uint32_t vregA = inst->VRegA_11x();
1531 const RegType& reg_type = work_line_->GetRegisterType(vregA);
Ian Rogers9074b992011-10-26 17:41:55 -07001532 // Disallow returning uninitialized values and verify that the reference in vAA is an
1533 // instance of the "return_type"
1534 if (reg_type.IsUninitializedTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001535 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "returning uninitialized object '" << reg_type << "'";
Ian Rogers9074b992011-10-26 17:41:55 -07001536 } else if (!return_type.IsAssignableFrom(reg_type)) {
jeffhao666d9b42012-06-12 11:36:38 -07001537 Fail(reg_type.IsUnresolvedTypes() ? VERIFY_ERROR_BAD_CLASS_SOFT : VERIFY_ERROR_BAD_CLASS_HARD)
1538 << "returning '" << reg_type << "', but expected from declaration '" << return_type << "'";
jeffhaobdb76512011-09-07 11:43:16 -07001539 }
1540 }
1541 }
1542 break;
1543
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001544 /* could be boolean, int, float, or a null reference */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001545 case Instruction::CONST_4: {
1546 int32_t val = static_cast<int32_t>(inst->VRegB_11n() << 28) >> 28;
1547 work_line_->SetRegisterType(inst->VRegA_11n(), reg_types_.FromCat1Const(val, true));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001548 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02001549 }
1550 case Instruction::CONST_16: {
1551 int16_t val = static_cast<int16_t>(inst->VRegB_21s());
1552 work_line_->SetRegisterType(inst->VRegA_21s(), reg_types_.FromCat1Const(val, true));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001553 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02001554 }
jeffhaobdb76512011-09-07 11:43:16 -07001555 case Instruction::CONST:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001556 work_line_->SetRegisterType(inst->VRegA_31i(),
1557 reg_types_.FromCat1Const(inst->VRegB_31i(), true));
jeffhaobdb76512011-09-07 11:43:16 -07001558 break;
1559 case Instruction::CONST_HIGH16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001560 work_line_->SetRegisterType(inst->VRegA_21h(),
1561 reg_types_.FromCat1Const(inst->VRegB_21h() << 16, true));
jeffhaobdb76512011-09-07 11:43:16 -07001562 break;
jeffhaobdb76512011-09-07 11:43:16 -07001563 /* could be long or double; resolved upon use */
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001564 case Instruction::CONST_WIDE_16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001565 int64_t val = static_cast<int16_t>(inst->VRegB_21s());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001566 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1567 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001568 work_line_->SetRegisterTypeWide(inst->VRegA_21s(), lo, hi);
jeffhaobdb76512011-09-07 11:43:16 -07001569 break;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001570 }
1571 case Instruction::CONST_WIDE_32: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001572 int64_t val = static_cast<int32_t>(inst->VRegB_31i());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001573 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1574 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001575 work_line_->SetRegisterTypeWide(inst->VRegA_31i(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001576 break;
1577 }
1578 case Instruction::CONST_WIDE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001579 int64_t val = inst->VRegB_51l();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001580 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1581 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001582 work_line_->SetRegisterTypeWide(inst->VRegA_51l(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001583 break;
1584 }
1585 case Instruction::CONST_WIDE_HIGH16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001586 int64_t val = static_cast<uint64_t>(inst->VRegB_21h()) << 48;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001587 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1588 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001589 work_line_->SetRegisterTypeWide(inst->VRegA_21h(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001590 break;
1591 }
jeffhaobdb76512011-09-07 11:43:16 -07001592 case Instruction::CONST_STRING:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001593 work_line_->SetRegisterType(inst->VRegA_21c(), reg_types_.JavaLangString());
1594 break;
jeffhaobdb76512011-09-07 11:43:16 -07001595 case Instruction::CONST_STRING_JUMBO:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001596 work_line_->SetRegisterType(inst->VRegA_31c(), reg_types_.JavaLangString());
jeffhaobdb76512011-09-07 11:43:16 -07001597 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001598 case Instruction::CONST_CLASS: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001599 // Get type from instruction if unresolved then we need an access check
1600 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Sebastien Hertz5243e912013-05-21 10:55:07 +02001601 const RegType& res_type = ResolveClassAndCheckAccess(inst->VRegB_21c());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001602 // Register holds class, ie its type is class, on error it will hold Conflict.
Sebastien Hertz5243e912013-05-21 10:55:07 +02001603 work_line_->SetRegisterType(inst->VRegA_21c(),
Ian Rogersb4903572012-10-11 11:52:56 -07001604 res_type.IsConflict() ? res_type
1605 : reg_types_.JavaLangClass(true));
jeffhaobdb76512011-09-07 11:43:16 -07001606 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001607 }
jeffhaobdb76512011-09-07 11:43:16 -07001608 case Instruction::MONITOR_ENTER:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001609 work_line_->PushMonitor(inst->VRegA_11x(), work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07001610 break;
1611 case Instruction::MONITOR_EXIT:
1612 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001613 * monitor-exit instructions are odd. They can throw exceptions,
jeffhaobdb76512011-09-07 11:43:16 -07001614 * but when they do they act as if they succeeded and the PC is
jeffhaod1f0fde2011-09-08 17:25:33 -07001615 * pointing to the following instruction. (This behavior goes back
jeffhaobdb76512011-09-07 11:43:16 -07001616 * to the need to handle asynchronous exceptions, a now-deprecated
1617 * feature that Dalvik doesn't support.)
1618 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001619 * In practice we don't need to worry about this. The only
jeffhaobdb76512011-09-07 11:43:16 -07001620 * exceptions that can be thrown from monitor-exit are for a
jeffhaod1f0fde2011-09-08 17:25:33 -07001621 * null reference and -exit without a matching -enter. If the
jeffhaobdb76512011-09-07 11:43:16 -07001622 * structured locking checks are working, the former would have
1623 * failed on the -enter instruction, and the latter is impossible.
1624 *
1625 * This is fortunate, because issue 3221411 prevents us from
1626 * chasing the "can throw" path when monitor verification is
jeffhaod1f0fde2011-09-08 17:25:33 -07001627 * enabled. If we can fully verify the locking we can ignore
jeffhaobdb76512011-09-07 11:43:16 -07001628 * some catch blocks (which will show up as "dead" code when
1629 * we skip them here); if we can't, then the code path could be
1630 * "live" so we still need to check it.
1631 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001632 opcode_flags &= ~Instruction::kThrow;
Sebastien Hertz5243e912013-05-21 10:55:07 +02001633 work_line_->PopMonitor(inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07001634 break;
1635
Ian Rogers28ad40d2011-10-27 15:19:26 -07001636 case Instruction::CHECK_CAST:
Ian Rogersd81871c2011-10-03 13:57:23 -07001637 case Instruction::INSTANCE_OF: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001638 /*
1639 * If this instruction succeeds, we will "downcast" register vA to the type in vB. (This
1640 * could be a "upcast" -- not expected, so we don't try to address it.)
1641 *
1642 * If it fails, an exception is thrown, which we deal with later by ignoring the update to
Elliott Hughesadb8c672012-03-06 16:49:32 -08001643 * dec_insn.vA when branching to a handler.
Ian Rogers28ad40d2011-10-27 15:19:26 -07001644 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001645 const bool is_checkcast = (inst->Opcode() == Instruction::CHECK_CAST);
1646 const uint32_t type_idx = (is_checkcast) ? inst->VRegB_21c() : inst->VRegC_22c();
1647 const RegType& res_type = ResolveClassAndCheckAccess(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001648 if (res_type.IsConflict()) {
1649 DCHECK_NE(failures_.size(), 0U);
1650 if (!is_checkcast) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001651 work_line_->SetRegisterType(inst->VRegA_22c(), reg_types_.Boolean());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001652 }
1653 break; // bad class
Ian Rogers9f1ab122011-12-12 08:52:43 -08001654 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001655 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Sebastien Hertz5243e912013-05-21 10:55:07 +02001656 uint32_t orig_type_reg = (is_checkcast) ? inst->VRegA_21c() : inst->VRegB_22c();
1657 const RegType& orig_type = work_line_->GetRegisterType(orig_type_reg);
Ian Rogers28ad40d2011-10-27 15:19:26 -07001658 if (!res_type.IsNonZeroReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001659 if (is_checkcast) {
1660 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on unexpected class " << res_type;
1661 } else {
1662 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on unexpected class " << res_type;
1663 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001664 } else if (!orig_type.IsReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001665 if (is_checkcast) {
1666 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on non-reference in v" << orig_type_reg;
1667 } else {
1668 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on non-reference in v" << orig_type_reg;
1669 }
jeffhao2a8a90e2011-09-26 14:25:31 -07001670 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001671 if (is_checkcast) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001672 work_line_->SetRegisterType(inst->VRegA_21c(), res_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001673 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001674 work_line_->SetRegisterType(inst->VRegA_22c(), reg_types_.Boolean());
jeffhaobdb76512011-09-07 11:43:16 -07001675 }
jeffhaobdb76512011-09-07 11:43:16 -07001676 }
jeffhao2a8a90e2011-09-26 14:25:31 -07001677 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001678 }
1679 case Instruction::ARRAY_LENGTH: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001680 const RegType& res_type = work_line_->GetRegisterType(inst->VRegB_12x());
Ian Rogers28ad40d2011-10-27 15:19:26 -07001681 if (res_type.IsReferenceTypes()) {
Ian Rogers89310de2012-02-01 13:47:30 -08001682 if (!res_type.IsArrayTypes() && !res_type.IsZero()) { // ie not an array or null
jeffhaod5347e02012-03-22 17:25:05 -07001683 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001684 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001685 work_line_->SetRegisterType(inst->VRegA_12x(), reg_types_.Integer());
Ian Rogersd81871c2011-10-03 13:57:23 -07001686 }
1687 }
1688 break;
1689 }
1690 case Instruction::NEW_INSTANCE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001691 const RegType& res_type = ResolveClassAndCheckAccess(inst->VRegB_21c());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001692 if (res_type.IsConflict()) {
1693 DCHECK_NE(failures_.size(), 0U);
1694 break; // bad class
jeffhao8cd6dda2012-02-22 10:15:34 -08001695 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001696 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
1697 // can't create an instance of an interface or abstract class */
1698 if (!res_type.IsInstantiableTypes()) {
1699 Fail(VERIFY_ERROR_INSTANTIATION)
1700 << "new-instance on primitive, interface or abstract class" << res_type;
Ian Rogers08f753d2012-08-24 14:35:25 -07001701 // Soft failure so carry on to set register type.
Ian Rogersd81871c2011-10-03 13:57:23 -07001702 }
Ian Rogers08f753d2012-08-24 14:35:25 -07001703 const RegType& uninit_type = reg_types_.Uninitialized(res_type, work_insn_idx_);
1704 // Any registers holding previous allocations from this address that have not yet been
1705 // initialized must be marked invalid.
1706 work_line_->MarkUninitRefsAsInvalid(uninit_type);
1707 // add the new uninitialized reference to the register state
Sebastien Hertz5243e912013-05-21 10:55:07 +02001708 work_line_->SetRegisterType(inst->VRegA_21c(), uninit_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001709 break;
1710 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08001711 case Instruction::NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001712 VerifyNewArray(inst, false, false);
jeffhaobdb76512011-09-07 11:43:16 -07001713 break;
1714 case Instruction::FILLED_NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001715 VerifyNewArray(inst, true, false);
Ian Rogers0c4a5062012-02-03 15:18:59 -08001716 just_set_result = true; // Filled new array sets result register
jeffhaobdb76512011-09-07 11:43:16 -07001717 break;
Ian Rogers0c4a5062012-02-03 15:18:59 -08001718 case Instruction::FILLED_NEW_ARRAY_RANGE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001719 VerifyNewArray(inst, true, true);
Ian Rogers0c4a5062012-02-03 15:18:59 -08001720 just_set_result = true; // Filled new array range sets result register
1721 break;
jeffhaobdb76512011-09-07 11:43:16 -07001722 case Instruction::CMPL_FLOAT:
1723 case Instruction::CMPG_FLOAT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001724 if (!work_line_->VerifyRegisterType(inst->VRegB_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08001725 break;
1726 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02001727 if (!work_line_->VerifyRegisterType(inst->VRegC_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08001728 break;
1729 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02001730 work_line_->SetRegisterType(inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001731 break;
1732 case Instruction::CMPL_DOUBLE:
1733 case Instruction::CMPG_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001734 if (!work_line_->VerifyRegisterTypeWide(inst->VRegB_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001735 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001736 break;
1737 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02001738 if (!work_line_->VerifyRegisterTypeWide(inst->VRegC_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001739 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001740 break;
1741 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02001742 work_line_->SetRegisterType(inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001743 break;
1744 case Instruction::CMP_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001745 if (!work_line_->VerifyRegisterTypeWide(inst->VRegB_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001746 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001747 break;
1748 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02001749 if (!work_line_->VerifyRegisterTypeWide(inst->VRegC_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001750 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001751 break;
1752 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02001753 work_line_->SetRegisterType(inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001754 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001755 case Instruction::THROW: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001756 const RegType& res_type = work_line_->GetRegisterType(inst->VRegA_11x());
Ian Rogersb4903572012-10-11 11:52:56 -07001757 if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(res_type)) {
jeffhaod5347e02012-03-22 17:25:05 -07001758 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "thrown class " << res_type << " not instanceof Throwable";
jeffhaobdb76512011-09-07 11:43:16 -07001759 }
1760 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001761 }
jeffhaobdb76512011-09-07 11:43:16 -07001762 case Instruction::GOTO:
1763 case Instruction::GOTO_16:
1764 case Instruction::GOTO_32:
1765 /* no effect on or use of registers */
1766 break;
1767
1768 case Instruction::PACKED_SWITCH:
1769 case Instruction::SPARSE_SWITCH:
1770 /* verify that vAA is an integer, or can be converted to one */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001771 work_line_->VerifyRegisterType(inst->VRegA_31t(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001772 break;
1773
Ian Rogersd81871c2011-10-03 13:57:23 -07001774 case Instruction::FILL_ARRAY_DATA: {
1775 /* Similar to the verification done for APUT */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001776 const RegType& array_type = work_line_->GetRegisterType(inst->VRegA_31t());
Ian Rogers89310de2012-02-01 13:47:30 -08001777 /* array_type can be null if the reg type is Zero */
1778 if (!array_type.IsZero()) {
jeffhao457cc512012-02-02 16:55:13 -08001779 if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001780 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with array type " << array_type;
Ian Rogers89310de2012-02-01 13:47:30 -08001781 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001782 const RegType& component_type = reg_types_.GetComponentType(array_type, class_loader_);
1783 DCHECK(!component_type.IsConflict());
jeffhao457cc512012-02-02 16:55:13 -08001784 if (component_type.IsNonZeroReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001785 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with component type "
1786 << component_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001787 } else {
jeffhao457cc512012-02-02 16:55:13 -08001788 // Now verify if the element width in the table matches the element width declared in
1789 // the array
1790 const uint16_t* array_data = insns + (insns[1] | (((int32_t) insns[2]) << 16));
1791 if (array_data[0] != Instruction::kArrayDataSignature) {
jeffhaod5347e02012-03-22 17:25:05 -07001792 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid magic for array-data";
jeffhao457cc512012-02-02 16:55:13 -08001793 } else {
1794 size_t elem_width = Primitive::ComponentSize(component_type.GetPrimitiveType());
1795 // Since we don't compress the data in Dex, expect to see equal width of data stored
1796 // in the table and expected from the array class.
1797 if (array_data[1] != elem_width) {
jeffhaod5347e02012-03-22 17:25:05 -07001798 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-data size mismatch (" << array_data[1]
1799 << " vs " << elem_width << ")";
jeffhao457cc512012-02-02 16:55:13 -08001800 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001801 }
1802 }
jeffhaobdb76512011-09-07 11:43:16 -07001803 }
1804 }
1805 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001806 }
jeffhaobdb76512011-09-07 11:43:16 -07001807 case Instruction::IF_EQ:
Ian Rogersd81871c2011-10-03 13:57:23 -07001808 case Instruction::IF_NE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001809 const RegType& reg_type1 = work_line_->GetRegisterType(inst->VRegA_22t());
1810 const RegType& reg_type2 = work_line_->GetRegisterType(inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07001811 bool mismatch = false;
1812 if (reg_type1.IsZero()) { // zero then integral or reference expected
1813 mismatch = !reg_type2.IsReferenceTypes() && !reg_type2.IsIntegralTypes();
1814 } else if (reg_type1.IsReferenceTypes()) { // both references?
1815 mismatch = !reg_type2.IsReferenceTypes();
1816 } else { // both integral?
1817 mismatch = !reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes();
1818 }
1819 if (mismatch) {
jeffhaod5347e02012-03-22 17:25:05 -07001820 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to if-eq/if-ne (" << reg_type1 << "," << reg_type2
1821 << ") must both be references or integral";
jeffhaobdb76512011-09-07 11:43:16 -07001822 }
1823 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001824 }
jeffhaobdb76512011-09-07 11:43:16 -07001825 case Instruction::IF_LT:
1826 case Instruction::IF_GE:
1827 case Instruction::IF_GT:
Ian Rogersd81871c2011-10-03 13:57:23 -07001828 case Instruction::IF_LE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001829 const RegType& reg_type1 = work_line_->GetRegisterType(inst->VRegA_22t());
1830 const RegType& reg_type2 = work_line_->GetRegisterType(inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07001831 if (!reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001832 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to 'if' (" << reg_type1 << ","
1833 << reg_type2 << ") must be integral";
jeffhaobdb76512011-09-07 11:43:16 -07001834 }
1835 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001836 }
jeffhaobdb76512011-09-07 11:43:16 -07001837 case Instruction::IF_EQZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07001838 case Instruction::IF_NEZ: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001839 const RegType& reg_type = work_line_->GetRegisterType(inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07001840 if (!reg_type.IsReferenceTypes() && !reg_type.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001841 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type << " unexpected as arg to if-eqz/if-nez";
Ian Rogersd81871c2011-10-03 13:57:23 -07001842 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001843
1844 // Find previous instruction - its existence is a precondition to peephole optimization.
Ian Rogers9b360392013-06-06 14:45:07 -07001845 uint32_t instance_of_idx = 0;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001846 if (0 != work_insn_idx_) {
Ian Rogers9b360392013-06-06 14:45:07 -07001847 instance_of_idx = work_insn_idx_ - 1;
Brian Carlstromdf629502013-07-17 22:39:56 -07001848 while (0 != instance_of_idx && !insn_flags_[instance_of_idx].IsOpcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07001849 instance_of_idx--;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001850 }
Ian Rogers9b360392013-06-06 14:45:07 -07001851 CHECK(insn_flags_[instance_of_idx].IsOpcode());
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001852 } else {
1853 break;
1854 }
1855
Ian Rogers9b360392013-06-06 14:45:07 -07001856 const Instruction* instance_of_inst = Instruction::At(code_item_->insns_ + instance_of_idx);
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001857
1858 /* Check for peep-hole pattern of:
1859 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07001860 * instance-of vX, vY, T;
1861 * ifXXX vX, label ;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001862 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07001863 * label:
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001864 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07001865 * and sharpen the type of vY to be type T.
1866 * Note, this pattern can't be if:
1867 * - if there are other branches to this branch,
1868 * - when vX == vY.
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001869 */
Ian Rogersfae370a2013-06-05 08:33:27 -07001870 if (!CurrentInsnFlags()->IsBranchTarget() &&
Ian Rogers9b360392013-06-06 14:45:07 -07001871 (Instruction::INSTANCE_OF == instance_of_inst->Opcode()) &&
1872 (inst->VRegA_21t() == instance_of_inst->VRegA_22c()) &&
1873 (instance_of_inst->VRegA_22c() != instance_of_inst->VRegB_22c())) {
Ian Rogersfae370a2013-06-05 08:33:27 -07001874 // Check that the we are not attempting conversion to interface types,
1875 // which is not done because of the multiple inheritance implications.
Ian Rogers9b360392013-06-06 14:45:07 -07001876 const RegType& cast_type = ResolveClassAndCheckAccess(instance_of_inst->VRegC_22c());
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001877
Brian Carlstromdf629502013-07-17 22:39:56 -07001878 if (!cast_type.IsUnresolvedTypes() && !cast_type.GetClass()->IsInterface()) {
Ian Rogers9b360392013-06-06 14:45:07 -07001879 RegisterLine* update_line = new RegisterLine(code_item_->registers_size_, this);
Ian Rogersfae370a2013-06-05 08:33:27 -07001880 if (inst->Opcode() == Instruction::IF_EQZ) {
Ian Rogers9b360392013-06-06 14:45:07 -07001881 fallthrough_line.reset(update_line);
Ian Rogersfae370a2013-06-05 08:33:27 -07001882 } else {
Ian Rogers9b360392013-06-06 14:45:07 -07001883 branch_line.reset(update_line);
1884 }
1885 update_line->CopyFromLine(work_line_.get());
1886 update_line->SetRegisterType(instance_of_inst->VRegB_22c(), cast_type);
1887 if (!insn_flags_[instance_of_idx].IsBranchTarget() && 0 != instance_of_idx) {
1888 // See if instance-of was preceded by a move-object operation, common due to the small
1889 // register encoding space of instance-of, and propagate type information to the source
1890 // of the move-object.
1891 uint32_t move_idx = instance_of_idx - 1;
Brian Carlstromdf629502013-07-17 22:39:56 -07001892 while (0 != move_idx && !insn_flags_[move_idx].IsOpcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07001893 move_idx--;
1894 }
1895 CHECK(insn_flags_[move_idx].IsOpcode());
1896 const Instruction* move_inst = Instruction::At(code_item_->insns_ + move_idx);
1897 switch (move_inst->Opcode()) {
1898 case Instruction::MOVE_OBJECT:
1899 if (move_inst->VRegA_12x() == instance_of_inst->VRegB_22c()) {
1900 update_line->SetRegisterType(move_inst->VRegB_12x(), cast_type);
1901 }
1902 break;
1903 case Instruction::MOVE_OBJECT_FROM16:
1904 if (move_inst->VRegA_22x() == instance_of_inst->VRegB_22c()) {
1905 update_line->SetRegisterType(move_inst->VRegB_22x(), cast_type);
1906 }
1907 break;
1908 case Instruction::MOVE_OBJECT_16:
1909 if (move_inst->VRegA_32x() == instance_of_inst->VRegB_22c()) {
1910 update_line->SetRegisterType(move_inst->VRegB_32x(), cast_type);
1911 }
1912 break;
1913 default:
1914 break;
1915 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001916 }
1917 }
1918 }
1919
jeffhaobdb76512011-09-07 11:43:16 -07001920 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001921 }
jeffhaobdb76512011-09-07 11:43:16 -07001922 case Instruction::IF_LTZ:
1923 case Instruction::IF_GEZ:
1924 case Instruction::IF_GTZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07001925 case Instruction::IF_LEZ: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001926 const RegType& reg_type = work_line_->GetRegisterType(inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07001927 if (!reg_type.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001928 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
1929 << " unexpected as arg to if-ltz/if-gez/if-gtz/if-lez";
Ian Rogersd81871c2011-10-03 13:57:23 -07001930 }
jeffhaobdb76512011-09-07 11:43:16 -07001931 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001932 }
jeffhaobdb76512011-09-07 11:43:16 -07001933 case Instruction::AGET_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001934 VerifyAGet(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001935 break;
jeffhaobdb76512011-09-07 11:43:16 -07001936 case Instruction::AGET_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001937 VerifyAGet(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001938 break;
jeffhaobdb76512011-09-07 11:43:16 -07001939 case Instruction::AGET_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001940 VerifyAGet(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001941 break;
jeffhaobdb76512011-09-07 11:43:16 -07001942 case Instruction::AGET_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001943 VerifyAGet(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001944 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001945 case Instruction::AGET:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001946 VerifyAGet(inst, reg_types_.Integer(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001947 break;
jeffhaobdb76512011-09-07 11:43:16 -07001948 case Instruction::AGET_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001949 VerifyAGet(inst, reg_types_.LongLo(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001950 break;
1951 case Instruction::AGET_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001952 VerifyAGet(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07001953 break;
1954
Ian Rogersd81871c2011-10-03 13:57:23 -07001955 case Instruction::APUT_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001956 VerifyAPut(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001957 break;
1958 case Instruction::APUT_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001959 VerifyAPut(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001960 break;
1961 case Instruction::APUT_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001962 VerifyAPut(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001963 break;
1964 case Instruction::APUT_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001965 VerifyAPut(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001966 break;
1967 case Instruction::APUT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001968 VerifyAPut(inst, reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001969 break;
1970 case Instruction::APUT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001971 VerifyAPut(inst, reg_types_.LongLo(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001972 break;
1973 case Instruction::APUT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001974 VerifyAPut(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07001975 break;
1976
jeffhaobdb76512011-09-07 11:43:16 -07001977 case Instruction::IGET_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001978 VerifyISGet(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001979 break;
jeffhaobdb76512011-09-07 11:43:16 -07001980 case Instruction::IGET_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001981 VerifyISGet(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001982 break;
jeffhaobdb76512011-09-07 11:43:16 -07001983 case Instruction::IGET_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001984 VerifyISGet(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001985 break;
jeffhaobdb76512011-09-07 11:43:16 -07001986 case Instruction::IGET_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001987 VerifyISGet(inst, reg_types_.Short(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001988 break;
1989 case Instruction::IGET:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001990 VerifyISGet(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07001991 break;
1992 case Instruction::IGET_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001993 VerifyISGet(inst, reg_types_.LongLo(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07001994 break;
1995 case Instruction::IGET_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001996 VerifyISGet(inst, reg_types_.JavaLangObject(false), false, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001997 break;
jeffhaobdb76512011-09-07 11:43:16 -07001998
Ian Rogersd81871c2011-10-03 13:57:23 -07001999 case Instruction::IPUT_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002000 VerifyISPut(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002001 break;
2002 case Instruction::IPUT_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002003 VerifyISPut(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002004 break;
2005 case Instruction::IPUT_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002006 VerifyISPut(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002007 break;
2008 case Instruction::IPUT_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002009 VerifyISPut(inst, reg_types_.Short(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002010 break;
2011 case Instruction::IPUT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002012 VerifyISPut(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002013 break;
2014 case Instruction::IPUT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002015 VerifyISPut(inst, reg_types_.LongLo(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002016 break;
jeffhaobdb76512011-09-07 11:43:16 -07002017 case Instruction::IPUT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002018 VerifyISPut(inst, reg_types_.JavaLangObject(false), false, false);
jeffhaobdb76512011-09-07 11:43:16 -07002019 break;
2020
jeffhaobdb76512011-09-07 11:43:16 -07002021 case Instruction::SGET_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002022 VerifyISGet(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002023 break;
jeffhaobdb76512011-09-07 11:43:16 -07002024 case Instruction::SGET_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002025 VerifyISGet(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002026 break;
jeffhaobdb76512011-09-07 11:43:16 -07002027 case Instruction::SGET_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002028 VerifyISGet(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002029 break;
jeffhaobdb76512011-09-07 11:43:16 -07002030 case Instruction::SGET_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002031 VerifyISGet(inst, reg_types_.Short(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002032 break;
2033 case Instruction::SGET:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002034 VerifyISGet(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002035 break;
2036 case Instruction::SGET_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002037 VerifyISGet(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002038 break;
2039 case Instruction::SGET_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002040 VerifyISGet(inst, reg_types_.JavaLangObject(false), false, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002041 break;
2042
2043 case Instruction::SPUT_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002044 VerifyISPut(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002045 break;
2046 case Instruction::SPUT_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002047 VerifyISPut(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002048 break;
2049 case Instruction::SPUT_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002050 VerifyISPut(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002051 break;
2052 case Instruction::SPUT_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002053 VerifyISPut(inst, reg_types_.Short(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002054 break;
2055 case Instruction::SPUT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002056 VerifyISPut(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002057 break;
2058 case Instruction::SPUT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002059 VerifyISPut(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002060 break;
2061 case Instruction::SPUT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002062 VerifyISPut(inst, reg_types_.JavaLangObject(false), false, true);
jeffhaobdb76512011-09-07 11:43:16 -07002063 break;
2064
2065 case Instruction::INVOKE_VIRTUAL:
2066 case Instruction::INVOKE_VIRTUAL_RANGE:
2067 case Instruction::INVOKE_SUPER:
Ian Rogersd81871c2011-10-03 13:57:23 -07002068 case Instruction::INVOKE_SUPER_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002069 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE ||
2070 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
2071 bool is_super = (inst->Opcode() == Instruction::INVOKE_SUPER ||
2072 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
2073 mirror::AbstractMethod* called_method = VerifyInvocationArgs(inst, METHOD_VIRTUAL,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002074 is_range, is_super);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002075 const char* descriptor;
2076 if (called_method == NULL) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002077 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002078 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2079 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
2080 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2081 } else {
2082 descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
jeffhaobdb76512011-09-07 11:43:16 -07002083 }
Ian Rogersb4903572012-10-11 11:52:56 -07002084 const RegType& return_type = reg_types_.FromDescriptor(class_loader_, descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002085 if (!return_type.IsLowHalf()) {
2086 work_line_->SetResultRegisterType(return_type);
2087 } else {
2088 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2089 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002090 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002091 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002092 }
jeffhaobdb76512011-09-07 11:43:16 -07002093 case Instruction::INVOKE_DIRECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002094 case Instruction::INVOKE_DIRECT_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002095 bool is_range = (inst->Opcode() == Instruction::INVOKE_DIRECT_RANGE);
2096 mirror::AbstractMethod* called_method = VerifyInvocationArgs(inst, METHOD_DIRECT,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002097 is_range, false);
Ian Rogers46685432012-06-03 22:26:43 -07002098 const char* return_type_descriptor;
2099 bool is_constructor;
2100 if (called_method == NULL) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002101 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogers46685432012-06-03 22:26:43 -07002102 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2103 is_constructor = StringPiece(dex_file_->GetMethodName(method_id)) == "<init>";
2104 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
2105 return_type_descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2106 } else {
2107 is_constructor = called_method->IsConstructor();
2108 return_type_descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
2109 }
2110 if (is_constructor) {
jeffhaobdb76512011-09-07 11:43:16 -07002111 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002112 * Some additional checks when calling a constructor. We know from the invocation arg check
2113 * that the "this" argument is an instance of called_method->klass. Now we further restrict
2114 * that to require that called_method->klass is the same as this->klass or this->super,
2115 * allowing the latter only if the "this" argument is the same as the "this" argument to
2116 * this method (which implies that we're in a constructor ourselves).
jeffhaobdb76512011-09-07 11:43:16 -07002117 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002118 const RegType& this_type = work_line_->GetInvocationThis(inst, is_range);
jeffhaob57e9522012-04-26 18:08:21 -07002119 if (this_type.IsConflict()) // failure.
2120 break;
jeffhaobdb76512011-09-07 11:43:16 -07002121
jeffhaob57e9522012-04-26 18:08:21 -07002122 /* no null refs allowed (?) */
2123 if (this_type.IsZero()) {
2124 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unable to initialize null ref";
2125 break;
jeffhao2a8a90e2011-09-26 14:25:31 -07002126 }
jeffhaob57e9522012-04-26 18:08:21 -07002127
2128 /* must be in same class or in superclass */
Ian Rogers46685432012-06-03 22:26:43 -07002129 // const RegType& this_super_klass = this_type.GetSuperClass(&reg_types_);
2130 // TODO: re-enable constructor type verification
2131 // if (this_super_klass.IsConflict()) {
jeffhaob57e9522012-04-26 18:08:21 -07002132 // Unknown super class, fail so we re-check at runtime.
Ian Rogers46685432012-06-03 22:26:43 -07002133 // Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "super class unknown for '" << this_type << "'";
2134 // break;
2135 // }
jeffhaob57e9522012-04-26 18:08:21 -07002136
2137 /* arg must be an uninitialized reference */
2138 if (!this_type.IsUninitializedTypes()) {
2139 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Expected initialization on uninitialized reference "
2140 << this_type;
2141 break;
2142 }
2143
2144 /*
2145 * Replace the uninitialized reference with an initialized one. We need to do this for all
2146 * registers that have the same object instance in them, not just the "this" register.
2147 */
2148 work_line_->MarkRefsAsInitialized(this_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002149 }
Ian Rogersb4903572012-10-11 11:52:56 -07002150 const RegType& return_type = reg_types_.FromDescriptor(class_loader_, return_type_descriptor,
2151 false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002152 if (!return_type.IsLowHalf()) {
2153 work_line_->SetResultRegisterType(return_type);
2154 } else {
2155 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2156 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002157 just_set_result = true;
2158 break;
2159 }
2160 case Instruction::INVOKE_STATIC:
2161 case Instruction::INVOKE_STATIC_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002162 bool is_range = (inst->Opcode() == Instruction::INVOKE_STATIC_RANGE);
2163 mirror::AbstractMethod* called_method = VerifyInvocationArgs(inst, METHOD_STATIC, is_range, false);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002164 const char* descriptor;
2165 if (called_method == NULL) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002166 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002167 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2168 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogers0571d352011-11-03 19:51:38 -07002169 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002170 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002171 descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002172 }
Ian Rogersb4903572012-10-11 11:52:56 -07002173 const RegType& return_type = reg_types_.FromDescriptor(class_loader_, descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002174 if (!return_type.IsLowHalf()) {
2175 work_line_->SetResultRegisterType(return_type);
2176 } else {
2177 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2178 }
jeffhaobdb76512011-09-07 11:43:16 -07002179 just_set_result = true;
2180 }
2181 break;
jeffhaobdb76512011-09-07 11:43:16 -07002182 case Instruction::INVOKE_INTERFACE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002183 case Instruction::INVOKE_INTERFACE_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002184 bool is_range = (inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE);
2185 mirror::AbstractMethod* abs_method = VerifyInvocationArgs(inst, METHOD_INTERFACE, is_range, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002186 if (abs_method != NULL) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002187 mirror::Class* called_interface = abs_method->GetDeclaringClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002188 if (!called_interface->IsInterface() && !called_interface->IsObjectClass()) {
2189 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected interface class in invoke-interface '"
2190 << PrettyMethod(abs_method) << "'";
2191 break;
Ian Rogers28ad40d2011-10-27 15:19:26 -07002192 }
Ian Rogers0d604842012-04-16 14:50:24 -07002193 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002194 /* Get the type of the "this" arg, which should either be a sub-interface of called
2195 * interface or Object (see comments in RegType::JoinClass).
2196 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002197 const RegType& this_type = work_line_->GetInvocationThis(inst, is_range);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002198 if (this_type.IsZero()) {
2199 /* null pointer always passes (and always fails at runtime) */
2200 } else {
2201 if (this_type.IsUninitializedTypes()) {
2202 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface call on uninitialized object "
2203 << this_type;
2204 break;
2205 }
2206 // In the past we have tried to assert that "called_interface" is assignable
2207 // from "this_type.GetClass()", however, as we do an imprecise Join
2208 // (RegType::JoinClass) we don't have full information on what interfaces are
2209 // implemented by "this_type". For example, two classes may implement the same
2210 // interfaces and have a common parent that doesn't implement the interface. The
2211 // join will set "this_type" to the parent class and a test that this implements
2212 // the interface will incorrectly fail.
2213 }
2214 /*
2215 * We don't have an object instance, so we can't find the concrete method. However, all of
2216 * the type information is in the abstract method, so we're good.
2217 */
2218 const char* descriptor;
2219 if (abs_method == NULL) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002220 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002221 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2222 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
2223 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2224 } else {
2225 descriptor = MethodHelper(abs_method).GetReturnTypeDescriptor();
2226 }
Ian Rogersb4903572012-10-11 11:52:56 -07002227 const RegType& return_type = reg_types_.FromDescriptor(class_loader_, descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002228 if (!return_type.IsLowHalf()) {
2229 work_line_->SetResultRegisterType(return_type);
2230 } else {
2231 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2232 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002233 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002234 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002235 }
jeffhaobdb76512011-09-07 11:43:16 -07002236 case Instruction::NEG_INT:
2237 case Instruction::NOT_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002238 work_line_->CheckUnaryOp(inst, reg_types_.Integer(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002239 break;
2240 case Instruction::NEG_LONG:
2241 case Instruction::NOT_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002242 work_line_->CheckUnaryOpWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002243 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002244 break;
2245 case Instruction::NEG_FLOAT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002246 work_line_->CheckUnaryOp(inst, reg_types_.Float(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002247 break;
2248 case Instruction::NEG_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002249 work_line_->CheckUnaryOpWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002250 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002251 break;
2252 case Instruction::INT_TO_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002253 work_line_->CheckUnaryOpToWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002254 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002255 break;
2256 case Instruction::INT_TO_FLOAT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002257 work_line_->CheckUnaryOp(inst, reg_types_.Float(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002258 break;
2259 case Instruction::INT_TO_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002260 work_line_->CheckUnaryOpToWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002261 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002262 break;
2263 case Instruction::LONG_TO_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002264 work_line_->CheckUnaryOpFromWide(inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002265 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002266 break;
2267 case Instruction::LONG_TO_FLOAT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002268 work_line_->CheckUnaryOpFromWide(inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002269 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002270 break;
2271 case Instruction::LONG_TO_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002272 work_line_->CheckUnaryOpWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002273 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002274 break;
2275 case Instruction::FLOAT_TO_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002276 work_line_->CheckUnaryOp(inst, reg_types_.Integer(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002277 break;
2278 case Instruction::FLOAT_TO_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002279 work_line_->CheckUnaryOpToWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002280 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002281 break;
2282 case Instruction::FLOAT_TO_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002283 work_line_->CheckUnaryOpToWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002284 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002285 break;
2286 case Instruction::DOUBLE_TO_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002287 work_line_->CheckUnaryOpFromWide(inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002288 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002289 break;
2290 case Instruction::DOUBLE_TO_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002291 work_line_->CheckUnaryOpWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002292 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002293 break;
2294 case Instruction::DOUBLE_TO_FLOAT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002295 work_line_->CheckUnaryOpFromWide(inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002296 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002297 break;
2298 case Instruction::INT_TO_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002299 work_line_->CheckUnaryOp(inst, reg_types_.Byte(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002300 break;
2301 case Instruction::INT_TO_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002302 work_line_->CheckUnaryOp(inst, reg_types_.Char(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002303 break;
2304 case Instruction::INT_TO_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002305 work_line_->CheckUnaryOp(inst, reg_types_.Short(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002306 break;
2307
2308 case Instruction::ADD_INT:
2309 case Instruction::SUB_INT:
2310 case Instruction::MUL_INT:
2311 case Instruction::REM_INT:
2312 case Instruction::DIV_INT:
2313 case Instruction::SHL_INT:
2314 case Instruction::SHR_INT:
2315 case Instruction::USHR_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002316 work_line_->CheckBinaryOp(inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002317 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002318 break;
2319 case Instruction::AND_INT:
2320 case Instruction::OR_INT:
2321 case Instruction::XOR_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002322 work_line_->CheckBinaryOp(inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002323 reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002324 break;
2325 case Instruction::ADD_LONG:
2326 case Instruction::SUB_LONG:
2327 case Instruction::MUL_LONG:
2328 case Instruction::DIV_LONG:
2329 case Instruction::REM_LONG:
2330 case Instruction::AND_LONG:
2331 case Instruction::OR_LONG:
2332 case Instruction::XOR_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002333 work_line_->CheckBinaryOpWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002334 reg_types_.LongLo(), reg_types_.LongHi(),
2335 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002336 break;
2337 case Instruction::SHL_LONG:
2338 case Instruction::SHR_LONG:
2339 case Instruction::USHR_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002340 /* shift distance is Int, making these different from other binary operations */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002341 work_line_->CheckBinaryOpWideShift(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002342 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002343 break;
2344 case Instruction::ADD_FLOAT:
2345 case Instruction::SUB_FLOAT:
2346 case Instruction::MUL_FLOAT:
2347 case Instruction::DIV_FLOAT:
2348 case Instruction::REM_FLOAT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002349 work_line_->CheckBinaryOp(inst, reg_types_.Float(), reg_types_.Float(), reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002350 break;
2351 case Instruction::ADD_DOUBLE:
2352 case Instruction::SUB_DOUBLE:
2353 case Instruction::MUL_DOUBLE:
2354 case Instruction::DIV_DOUBLE:
2355 case Instruction::REM_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002356 work_line_->CheckBinaryOpWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002357 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2358 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002359 break;
2360 case Instruction::ADD_INT_2ADDR:
2361 case Instruction::SUB_INT_2ADDR:
2362 case Instruction::MUL_INT_2ADDR:
2363 case Instruction::REM_INT_2ADDR:
2364 case Instruction::SHL_INT_2ADDR:
2365 case Instruction::SHR_INT_2ADDR:
2366 case Instruction::USHR_INT_2ADDR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002367 work_line_->CheckBinaryOp2addr(inst, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002368 break;
2369 case Instruction::AND_INT_2ADDR:
2370 case Instruction::OR_INT_2ADDR:
2371 case Instruction::XOR_INT_2ADDR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002372 work_line_->CheckBinaryOp2addr(inst, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002373 break;
2374 case Instruction::DIV_INT_2ADDR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002375 work_line_->CheckBinaryOp2addr(inst, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002376 break;
2377 case Instruction::ADD_LONG_2ADDR:
2378 case Instruction::SUB_LONG_2ADDR:
2379 case Instruction::MUL_LONG_2ADDR:
2380 case Instruction::DIV_LONG_2ADDR:
2381 case Instruction::REM_LONG_2ADDR:
2382 case Instruction::AND_LONG_2ADDR:
2383 case Instruction::OR_LONG_2ADDR:
2384 case Instruction::XOR_LONG_2ADDR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002385 work_line_->CheckBinaryOp2addrWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002386 reg_types_.LongLo(), reg_types_.LongHi(),
2387 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002388 break;
2389 case Instruction::SHL_LONG_2ADDR:
2390 case Instruction::SHR_LONG_2ADDR:
2391 case Instruction::USHR_LONG_2ADDR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002392 work_line_->CheckBinaryOp2addrWideShift(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002393 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002394 break;
2395 case Instruction::ADD_FLOAT_2ADDR:
2396 case Instruction::SUB_FLOAT_2ADDR:
2397 case Instruction::MUL_FLOAT_2ADDR:
2398 case Instruction::DIV_FLOAT_2ADDR:
2399 case Instruction::REM_FLOAT_2ADDR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002400 work_line_->CheckBinaryOp2addr(inst, reg_types_.Float(), reg_types_.Float(), reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002401 break;
2402 case Instruction::ADD_DOUBLE_2ADDR:
2403 case Instruction::SUB_DOUBLE_2ADDR:
2404 case Instruction::MUL_DOUBLE_2ADDR:
2405 case Instruction::DIV_DOUBLE_2ADDR:
2406 case Instruction::REM_DOUBLE_2ADDR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002407 work_line_->CheckBinaryOp2addrWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002408 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2409 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002410 break;
2411 case Instruction::ADD_INT_LIT16:
2412 case Instruction::RSUB_INT:
2413 case Instruction::MUL_INT_LIT16:
2414 case Instruction::DIV_INT_LIT16:
2415 case Instruction::REM_INT_LIT16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002416 work_line_->CheckLiteralOp(inst, reg_types_.Integer(), reg_types_.Integer(), false, true);
jeffhaobdb76512011-09-07 11:43:16 -07002417 break;
2418 case Instruction::AND_INT_LIT16:
2419 case Instruction::OR_INT_LIT16:
2420 case Instruction::XOR_INT_LIT16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002421 work_line_->CheckLiteralOp(inst, reg_types_.Integer(), reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002422 break;
2423 case Instruction::ADD_INT_LIT8:
2424 case Instruction::RSUB_INT_LIT8:
2425 case Instruction::MUL_INT_LIT8:
2426 case Instruction::DIV_INT_LIT8:
2427 case Instruction::REM_INT_LIT8:
2428 case Instruction::SHL_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002429 case Instruction::SHR_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002430 case Instruction::USHR_INT_LIT8:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002431 work_line_->CheckLiteralOp(inst, reg_types_.Integer(), reg_types_.Integer(), false, false);
jeffhaobdb76512011-09-07 11:43:16 -07002432 break;
2433 case Instruction::AND_INT_LIT8:
2434 case Instruction::OR_INT_LIT8:
2435 case Instruction::XOR_INT_LIT8:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002436 work_line_->CheckLiteralOp(inst, reg_types_.Integer(), reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002437 break;
2438
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002439 // Special instructions.
Sebastien Hertzcc10e0e2013-06-28 14:24:48 +02002440 case Instruction::RETURN_VOID_BARRIER:
2441 DCHECK(Runtime::Current()->IsStarted());
2442 if (!IsConstructor()) {
2443 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void-barrier not expected";
2444 }
2445 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002446 // Note: the following instructions encode offsets derived from class linking.
2447 // As such they use Class*/Field*/AbstractMethod* as these offsets only have
2448 // meaning if the class linking and resolution were successful.
2449 case Instruction::IGET_QUICK:
2450 VerifyIGetQuick(inst, reg_types_.Integer(), true);
2451 break;
2452 case Instruction::IGET_WIDE_QUICK:
2453 VerifyIGetQuick(inst, reg_types_.LongLo(), true);
2454 break;
2455 case Instruction::IGET_OBJECT_QUICK:
2456 VerifyIGetQuick(inst, reg_types_.JavaLangObject(false), false);
2457 break;
2458 case Instruction::IPUT_QUICK:
2459 VerifyIPutQuick(inst, reg_types_.Integer(), true);
2460 break;
2461 case Instruction::IPUT_WIDE_QUICK:
2462 VerifyIPutQuick(inst, reg_types_.LongLo(), true);
2463 break;
2464 case Instruction::IPUT_OBJECT_QUICK:
2465 VerifyIPutQuick(inst, reg_types_.JavaLangObject(false), false);
2466 break;
2467 case Instruction::INVOKE_VIRTUAL_QUICK:
2468 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: {
2469 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
2470 mirror::AbstractMethod* called_method = VerifyInvokeVirtualQuickArgs(inst, is_range);
2471 if (called_method != NULL) {
2472 const char* descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
2473 const RegType& return_type = reg_types_.FromDescriptor(class_loader_, descriptor, false);
2474 if (!return_type.IsLowHalf()) {
2475 work_line_->SetResultRegisterType(return_type);
2476 } else {
2477 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2478 }
2479 just_set_result = true;
2480 }
2481 break;
2482 }
2483
Ian Rogersd81871c2011-10-03 13:57:23 -07002484 /* These should never appear during verification. */
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002485 case Instruction::UNUSED_3E:
2486 case Instruction::UNUSED_3F:
2487 case Instruction::UNUSED_40:
2488 case Instruction::UNUSED_41:
2489 case Instruction::UNUSED_42:
2490 case Instruction::UNUSED_43:
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002491 case Instruction::UNUSED_79:
2492 case Instruction::UNUSED_7A:
2493 case Instruction::UNUSED_EB:
2494 case Instruction::UNUSED_EC:
jeffhao9a4f0032012-08-30 16:17:40 -07002495 case Instruction::UNUSED_ED:
jeffhaobdb76512011-09-07 11:43:16 -07002496 case Instruction::UNUSED_EE:
2497 case Instruction::UNUSED_EF:
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002498 case Instruction::UNUSED_F0:
2499 case Instruction::UNUSED_F1:
jeffhaobdb76512011-09-07 11:43:16 -07002500 case Instruction::UNUSED_F2:
2501 case Instruction::UNUSED_F3:
2502 case Instruction::UNUSED_F4:
2503 case Instruction::UNUSED_F5:
2504 case Instruction::UNUSED_F6:
2505 case Instruction::UNUSED_F7:
2506 case Instruction::UNUSED_F8:
2507 case Instruction::UNUSED_F9:
2508 case Instruction::UNUSED_FA:
2509 case Instruction::UNUSED_FB:
jeffhaobdb76512011-09-07 11:43:16 -07002510 case Instruction::UNUSED_FC:
jeffhaobdb76512011-09-07 11:43:16 -07002511 case Instruction::UNUSED_FD:
jeffhaobdb76512011-09-07 11:43:16 -07002512 case Instruction::UNUSED_FE:
jeffhaobdb76512011-09-07 11:43:16 -07002513 case Instruction::UNUSED_FF:
jeffhaod5347e02012-03-22 17:25:05 -07002514 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Unexpected opcode " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07002515 break;
2516
2517 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002518 * DO NOT add a "default" clause here. Without it the compiler will
jeffhaobdb76512011-09-07 11:43:16 -07002519 * complain if an instruction is missing (which is desirable).
2520 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002521 } // end - switch (dec_insn.opcode)
jeffhaobdb76512011-09-07 11:43:16 -07002522
Ian Rogersad0b3a32012-04-16 14:50:24 -07002523 if (have_pending_hard_failure_) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002524 if (Runtime::Current()->IsCompiler()) {
jeffhaob57e9522012-04-26 18:08:21 -07002525 /* When compiling, check that the last failure is a hard failure */
Ian Rogersad0b3a32012-04-16 14:50:24 -07002526 CHECK_EQ(failures_[failures_.size() - 1], VERIFY_ERROR_BAD_CLASS_HARD);
Ian Rogerse1758fe2012-04-19 11:31:15 -07002527 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002528 /* immediate failure, reject class */
2529 info_messages_ << "Rejecting opcode " << inst->DumpString(dex_file_);
2530 return false;
jeffhaofaf459e2012-08-31 15:32:47 -07002531 } else if (have_pending_runtime_throw_failure_) {
2532 /* slow path will throw, mark following code as unreachable */
2533 opcode_flags = Instruction::kThrow;
jeffhaobdb76512011-09-07 11:43:16 -07002534 }
jeffhaobdb76512011-09-07 11:43:16 -07002535 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002536 * If we didn't just set the result register, clear it out. This ensures that you can only use
2537 * "move-result" immediately after the result is set. (We could check this statically, but it's
2538 * not expensive and it makes our debugging output cleaner.)
jeffhaobdb76512011-09-07 11:43:16 -07002539 */
2540 if (!just_set_result) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002541 work_line_->SetResultTypeToUnknown();
jeffhaobdb76512011-09-07 11:43:16 -07002542 }
2543
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002544
jeffhaobdb76512011-09-07 11:43:16 -07002545
2546 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002547 * Handle "branch". Tag the branch target.
jeffhaobdb76512011-09-07 11:43:16 -07002548 *
2549 * NOTE: instructions like Instruction::EQZ provide information about the
jeffhaod1f0fde2011-09-08 17:25:33 -07002550 * state of the register when the branch is taken or not taken. For example,
jeffhaobdb76512011-09-07 11:43:16 -07002551 * somebody could get a reference field, check it for zero, and if the
2552 * branch is taken immediately store that register in a boolean field
jeffhaod1f0fde2011-09-08 17:25:33 -07002553 * since the value is known to be zero. We do not currently account for
jeffhaobdb76512011-09-07 11:43:16 -07002554 * that, and will reject the code.
2555 *
2556 * TODO: avoid re-fetching the branch target
2557 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002558 if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002559 bool isConditional, selfOkay;
Ian Rogersd81871c2011-10-03 13:57:23 -07002560 if (!GetBranchOffset(work_insn_idx_, &branch_target, &isConditional, &selfOkay)) {
jeffhaobdb76512011-09-07 11:43:16 -07002561 /* should never happen after static verification */
jeffhaod5347e02012-03-22 17:25:05 -07002562 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad branch";
jeffhaobdb76512011-09-07 11:43:16 -07002563 return false;
2564 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08002565 DCHECK_EQ(isConditional, (opcode_flags & Instruction::kContinue) != 0);
jeffhaod5347e02012-03-22 17:25:05 -07002566 if (!CheckNotMoveException(code_item_->insns_, work_insn_idx_ + branch_target)) {
jeffhaobdb76512011-09-07 11:43:16 -07002567 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002568 }
jeffhaobdb76512011-09-07 11:43:16 -07002569 /* update branch target, set "changed" if appropriate */
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002570 if (NULL != branch_line.get()) {
2571 if (!UpdateRegisters(work_insn_idx_ + branch_target, branch_line.get())) {
2572 return false;
2573 }
2574 } else {
2575 if (!UpdateRegisters(work_insn_idx_ + branch_target, work_line_.get())) {
2576 return false;
2577 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002578 }
jeffhaobdb76512011-09-07 11:43:16 -07002579 }
2580
2581 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002582 * Handle "switch". Tag all possible branch targets.
jeffhaobdb76512011-09-07 11:43:16 -07002583 *
2584 * We've already verified that the table is structurally sound, so we
2585 * just need to walk through and tag the targets.
2586 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002587 if ((opcode_flags & Instruction::kSwitch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002588 int offset_to_switch = insns[1] | (((int32_t) insns[2]) << 16);
2589 const uint16_t* switch_insns = insns + offset_to_switch;
2590 int switch_count = switch_insns[1];
2591 int offset_to_targets, targ;
2592
2593 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
2594 /* 0 = sig, 1 = count, 2/3 = first key */
2595 offset_to_targets = 4;
2596 } else {
2597 /* 0 = sig, 1 = count, 2..count * 2 = keys */
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07002598 DCHECK((*insns & 0xff) == Instruction::SPARSE_SWITCH);
jeffhaobdb76512011-09-07 11:43:16 -07002599 offset_to_targets = 2 + 2 * switch_count;
2600 }
2601
2602 /* verify each switch target */
2603 for (targ = 0; targ < switch_count; targ++) {
2604 int offset;
2605 uint32_t abs_offset;
2606
2607 /* offsets are 32-bit, and only partly endian-swapped */
2608 offset = switch_insns[offset_to_targets + targ * 2] |
2609 (((int32_t) switch_insns[offset_to_targets + targ * 2 + 1]) << 16);
Ian Rogersd81871c2011-10-03 13:57:23 -07002610 abs_offset = work_insn_idx_ + offset;
2611 DCHECK_LT(abs_offset, code_item_->insns_size_in_code_units_);
jeffhaod5347e02012-03-22 17:25:05 -07002612 if (!CheckNotMoveException(code_item_->insns_, abs_offset)) {
jeffhaobdb76512011-09-07 11:43:16 -07002613 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002614 }
2615 if (!UpdateRegisters(abs_offset, work_line_.get()))
jeffhaobdb76512011-09-07 11:43:16 -07002616 return false;
2617 }
2618 }
2619
2620 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002621 * Handle instructions that can throw and that are sitting in a "try" block. (If they're not in a
2622 * "try" block when they throw, control transfers out of the method.)
jeffhaobdb76512011-09-07 11:43:16 -07002623 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002624 if ((opcode_flags & Instruction::kThrow) != 0 && insn_flags_[work_insn_idx_].IsInTry()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002625 bool within_catch_all = false;
Ian Rogers0571d352011-11-03 19:51:38 -07002626 CatchHandlerIterator iterator(*code_item_, work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07002627
Ian Rogers0571d352011-11-03 19:51:38 -07002628 for (; iterator.HasNext(); iterator.Next()) {
2629 if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002630 within_catch_all = true;
2631 }
jeffhaobdb76512011-09-07 11:43:16 -07002632 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002633 * Merge registers into the "catch" block. We want to use the "savedRegs" rather than
2634 * "work_regs", because at runtime the exception will be thrown before the instruction
2635 * modifies any registers.
jeffhaobdb76512011-09-07 11:43:16 -07002636 */
Ian Rogers0571d352011-11-03 19:51:38 -07002637 if (!UpdateRegisters(iterator.GetHandlerAddress(), saved_line_.get())) {
jeffhaobdb76512011-09-07 11:43:16 -07002638 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002639 }
jeffhaobdb76512011-09-07 11:43:16 -07002640 }
2641
2642 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002643 * If the monitor stack depth is nonzero, there must be a "catch all" handler for this
2644 * instruction. This does apply to monitor-exit because of async exception handling.
jeffhaobdb76512011-09-07 11:43:16 -07002645 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002646 if (work_line_->MonitorStackDepth() > 0 && !within_catch_all) {
jeffhaobdb76512011-09-07 11:43:16 -07002647 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002648 * The state in work_line reflects the post-execution state. If the current instruction is a
2649 * monitor-enter and the monitor stack was empty, we don't need a catch-all (if it throws,
jeffhaobdb76512011-09-07 11:43:16 -07002650 * it will do so before grabbing the lock).
2651 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002652 if (inst->Opcode() != Instruction::MONITOR_ENTER || work_line_->MonitorStackDepth() != 1) {
jeffhaod5347e02012-03-22 17:25:05 -07002653 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
Ian Rogersd81871c2011-10-03 13:57:23 -07002654 << "expected to be within a catch-all for an instruction where a monitor is held";
jeffhaobdb76512011-09-07 11:43:16 -07002655 return false;
2656 }
2657 }
2658 }
2659
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002660 /* Handle "continue". Tag the next consecutive instruction.
2661 * Note: Keep the code handling "continue" case below the "branch" and "switch" cases,
2662 * because it changes work_line_ when performing peephole optimization
2663 * and this change should not be used in those cases.
2664 */
Ian Rogers6d376ae2013-07-23 15:12:40 -07002665 if ((opcode_flags & Instruction::kContinue) != 0) {
2666 uint32_t next_insn_idx = work_insn_idx_ + CurrentInsnFlags()->GetLengthInCodeUnits();
2667 if (next_insn_idx >= code_item_->insns_size_in_code_units_) {
2668 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Execution can walk off end of code area";
2669 return false;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002670 }
Ian Rogers6d376ae2013-07-23 15:12:40 -07002671 // The only way to get to a move-exception instruction is to get thrown there. Make sure the
2672 // next instruction isn't one.
2673 if (!CheckNotMoveException(code_item_->insns_, next_insn_idx)) {
2674 return false;
2675 }
2676 if (NULL != fallthrough_line.get()) {
2677 // Make workline consistent with fallthrough computed from peephole optimization.
2678 work_line_->CopyFromLine(fallthrough_line.get());
2679 }
Ian Rogersb8c78592013-07-25 23:52:52 +00002680 if (insn_flags_[next_insn_idx].IsReturn()) {
2681 // For returns we only care about the operand to the return, all other registers are dead.
2682 const Instruction* ret_inst = Instruction::At(code_item_->insns_ + next_insn_idx);
2683 Instruction::Code opcode = ret_inst->Opcode();
2684 if ((opcode == Instruction::RETURN_VOID) || (opcode == Instruction::RETURN_VOID_BARRIER)) {
2685 work_line_->MarkAllRegistersAsConflicts();
2686 } else {
2687 if (opcode == Instruction::RETURN_WIDE) {
2688 work_line_->MarkAllRegistersAsConflictsExceptWide(ret_inst->VRegA_11x());
2689 } else {
2690 work_line_->MarkAllRegistersAsConflictsExcept(ret_inst->VRegA_11x());
2691 }
2692 }
2693 }
Ian Rogers6d376ae2013-07-23 15:12:40 -07002694 RegisterLine* next_line = reg_table_.GetLine(next_insn_idx);
2695 if (next_line != NULL) {
2696 // Merge registers into what we have for the next instruction,
2697 // and set the "changed" flag if needed.
2698 if (!UpdateRegisters(next_insn_idx, work_line_.get())) {
2699 return false;
2700 }
2701 } else {
2702 /*
2703 * We're not recording register data for the next instruction, so we don't know what the
2704 * prior state was. We have to assume that something has changed and re-evaluate it.
2705 */
2706 insn_flags_[next_insn_idx].SetChanged();
2707 }
2708 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002709
jeffhaod1f0fde2011-09-08 17:25:33 -07002710 /* If we're returning from the method, make sure monitor stack is empty. */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002711 if ((opcode_flags & Instruction::kReturn) != 0) {
Elliott Hughesb25c3f62012-03-26 16:35:06 -07002712 if (!work_line_->VerifyMonitorStackEmpty()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002713 return false;
2714 }
jeffhaobdb76512011-09-07 11:43:16 -07002715 }
2716
2717 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002718 * Update start_guess. Advance to the next instruction of that's
2719 * possible, otherwise use the branch target if one was found. If
jeffhaobdb76512011-09-07 11:43:16 -07002720 * neither of those exists we're in a return or throw; leave start_guess
2721 * alone and let the caller sort it out.
2722 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002723 if ((opcode_flags & Instruction::kContinue) != 0) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002724 *start_guess = work_insn_idx_ + insn_flags_[work_insn_idx_].GetLengthInCodeUnits();
Elliott Hughesadb8c672012-03-06 16:49:32 -08002725 } else if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002726 /* we're still okay if branch_target is zero */
Ian Rogersd81871c2011-10-03 13:57:23 -07002727 *start_guess = work_insn_idx_ + branch_target;
jeffhaobdb76512011-09-07 11:43:16 -07002728 }
2729
Ian Rogersd81871c2011-10-03 13:57:23 -07002730 DCHECK_LT(*start_guess, code_item_->insns_size_in_code_units_);
2731 DCHECK(insn_flags_[*start_guess].IsOpcode());
jeffhaobdb76512011-09-07 11:43:16 -07002732
2733 return true;
Brian Carlstrom1895ea32013-07-18 13:28:37 -07002734} // NOLINT(readability/fn_size)
jeffhaobdb76512011-09-07 11:43:16 -07002735
Ian Rogers776ac1f2012-04-13 23:36:36 -07002736const RegType& MethodVerifier::ResolveClassAndCheckAccess(uint32_t class_idx) {
Ian Rogers0571d352011-11-03 19:51:38 -07002737 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002738 const RegType& referrer = GetDeclaringClass();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002739 mirror::Class* klass = dex_cache_->GetResolvedType(class_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002740 const RegType& result =
Ian Rogers04f94f42013-06-10 15:09:26 -07002741 klass != NULL ? reg_types_.FromClass(descriptor, klass,
2742 klass->CannotBeAssignedFromOtherTypes())
Ian Rogersb4903572012-10-11 11:52:56 -07002743 : reg_types_.FromDescriptor(class_loader_, descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002744 if (result.IsConflict()) {
2745 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "accessing broken descriptor '" << descriptor
2746 << "' in " << referrer;
2747 return result;
2748 }
Ian Rogerse1758fe2012-04-19 11:31:15 -07002749 if (klass == NULL && !result.IsUnresolvedTypes()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002750 dex_cache_->SetResolvedType(class_idx, result.GetClass());
Ian Rogerse1758fe2012-04-19 11:31:15 -07002751 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002752 // Check if access is allowed. Unresolved types use xxxWithAccessCheck to
Ian Rogers28ad40d2011-10-27 15:19:26 -07002753 // check at runtime if access is allowed and so pass here.
Ian Rogersad0b3a32012-04-16 14:50:24 -07002754 if (!result.IsUnresolvedTypes() && !referrer.IsUnresolvedTypes() && !referrer.CanAccess(result)) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002755 Fail(VERIFY_ERROR_ACCESS_CLASS) << "illegal class access: '"
Ian Rogersad0b3a32012-04-16 14:50:24 -07002756 << referrer << "' -> '" << result << "'";
Ian Rogers28ad40d2011-10-27 15:19:26 -07002757 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002758 return result;
Ian Rogersd81871c2011-10-03 13:57:23 -07002759}
2760
Ian Rogers776ac1f2012-04-13 23:36:36 -07002761const RegType& MethodVerifier::GetCaughtExceptionType() {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002762 const RegType* common_super = NULL;
Ian Rogersd81871c2011-10-03 13:57:23 -07002763 if (code_item_->tries_size_ != 0) {
Ian Rogers0571d352011-11-03 19:51:38 -07002764 const byte* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
Ian Rogersd81871c2011-10-03 13:57:23 -07002765 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
2766 for (uint32_t i = 0; i < handlers_size; i++) {
Ian Rogers0571d352011-11-03 19:51:38 -07002767 CatchHandlerIterator iterator(handlers_ptr);
2768 for (; iterator.HasNext(); iterator.Next()) {
2769 if (iterator.GetHandlerAddress() == (uint32_t) work_insn_idx_) {
2770 if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) {
Ian Rogersb4903572012-10-11 11:52:56 -07002771 common_super = &reg_types_.JavaLangThrowable(false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002772 } else {
Ian Rogers0571d352011-11-03 19:51:38 -07002773 const RegType& exception = ResolveClassAndCheckAccess(iterator.GetHandlerTypeIndex());
Ian Rogersc4762272012-02-01 15:55:55 -08002774 if (common_super == NULL) {
2775 // Unconditionally assign for the first handler. We don't assert this is a Throwable
2776 // as that is caught at runtime
2777 common_super = &exception;
Ian Rogersb4903572012-10-11 11:52:56 -07002778 } else if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(exception)) {
Ian Rogersc4762272012-02-01 15:55:55 -08002779 // We don't know enough about the type and the common path merge will result in
2780 // Conflict. Fail here knowing the correct thing can be done at runtime.
jeffhaod5347e02012-03-22 17:25:05 -07002781 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unexpected non-exception class " << exception;
Ian Rogersad0b3a32012-04-16 14:50:24 -07002782 return reg_types_.Conflict();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002783 } else if (common_super->Equals(exception)) {
Ian Rogersc4762272012-02-01 15:55:55 -08002784 // odd case, but nothing to do
Ian Rogersd81871c2011-10-03 13:57:23 -07002785 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002786 common_super = &common_super->Merge(exception, &reg_types_);
Ian Rogersb4903572012-10-11 11:52:56 -07002787 CHECK(reg_types_.JavaLangThrowable(false).IsAssignableFrom(*common_super));
Ian Rogersd81871c2011-10-03 13:57:23 -07002788 }
2789 }
2790 }
2791 }
Ian Rogers0571d352011-11-03 19:51:38 -07002792 handlers_ptr = iterator.EndDataPointer();
Ian Rogersd81871c2011-10-03 13:57:23 -07002793 }
2794 }
2795 if (common_super == NULL) {
2796 /* no catch blocks, or no catches with classes we can find */
jeffhaod5347e02012-03-22 17:25:05 -07002797 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unable to find exception handler";
Ian Rogersad0b3a32012-04-16 14:50:24 -07002798 return reg_types_.Conflict();
Ian Rogersd81871c2011-10-03 13:57:23 -07002799 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002800 return *common_super;
Ian Rogersd81871c2011-10-03 13:57:23 -07002801}
2802
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002803mirror::AbstractMethod* MethodVerifier::ResolveMethodAndCheckAccess(uint32_t dex_method_idx,
2804 MethodType method_type) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002805 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx);
Ian Rogers90040192011-12-16 08:54:29 -08002806 const RegType& klass_type = ResolveClassAndCheckAccess(method_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002807 if (klass_type.IsConflict()) {
2808 std::string append(" in attempt to access method ");
2809 append += dex_file_->GetMethodName(method_id);
2810 AppendToLastFailMessage(append);
Ian Rogers90040192011-12-16 08:54:29 -08002811 return NULL;
2812 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002813 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers90040192011-12-16 08:54:29 -08002814 return NULL; // Can't resolve Class so no more to do here
2815 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002816 mirror::Class* klass = klass_type.GetClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002817 const RegType& referrer = GetDeclaringClass();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002818 mirror::AbstractMethod* res_method = dex_cache_->GetResolvedMethod(dex_method_idx);
Ian Rogersd81871c2011-10-03 13:57:23 -07002819 if (res_method == NULL) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07002820 const char* name = dex_file_->GetMethodName(method_id);
Ian Rogers0571d352011-11-03 19:51:38 -07002821 std::string signature(dex_file_->CreateMethodSignature(method_id.proto_idx_, NULL));
jeffhao8cd6dda2012-02-22 10:15:34 -08002822
2823 if (method_type == METHOD_DIRECT || method_type == METHOD_STATIC) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002824 res_method = klass->FindDirectMethod(name, signature);
jeffhao8cd6dda2012-02-22 10:15:34 -08002825 } else if (method_type == METHOD_INTERFACE) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002826 res_method = klass->FindInterfaceMethod(name, signature);
2827 } else {
2828 res_method = klass->FindVirtualMethod(name, signature);
2829 }
2830 if (res_method != NULL) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002831 dex_cache_->SetResolvedMethod(dex_method_idx, res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07002832 } else {
jeffhao8cd6dda2012-02-22 10:15:34 -08002833 // If a virtual or interface method wasn't found with the expected type, look in
2834 // the direct methods. This can happen when the wrong invoke type is used or when
2835 // a class has changed, and will be flagged as an error in later checks.
2836 if (method_type == METHOD_INTERFACE || method_type == METHOD_VIRTUAL) {
2837 res_method = klass->FindDirectMethod(name, signature);
2838 }
2839 if (res_method == NULL) {
2840 Fail(VERIFY_ERROR_NO_METHOD) << "couldn't find method "
2841 << PrettyDescriptor(klass) << "." << name
2842 << " " << signature;
2843 return NULL;
2844 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002845 }
2846 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002847 // Make sure calls to constructors are "direct". There are additional restrictions but we don't
2848 // enforce them here.
2849 if (res_method->IsConstructor() && method_type != METHOD_DIRECT) {
jeffhaod5347e02012-03-22 17:25:05 -07002850 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting non-direct call to constructor "
2851 << PrettyMethod(res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07002852 return NULL;
2853 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002854 // Disallow any calls to class initializers.
2855 if (MethodHelper(res_method).IsClassInitializer()) {
jeffhaod5347e02012-03-22 17:25:05 -07002856 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting call to class initializer "
2857 << PrettyMethod(res_method);
jeffhao8cd6dda2012-02-22 10:15:34 -08002858 return NULL;
2859 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002860 // Check if access is allowed.
Ian Rogersad0b3a32012-04-16 14:50:24 -07002861 if (!referrer.CanAccessMember(res_method->GetDeclaringClass(), res_method->GetAccessFlags())) {
jeffhao8cd6dda2012-02-22 10:15:34 -08002862 Fail(VERIFY_ERROR_ACCESS_METHOD) << "illegal method access (call " << PrettyMethod(res_method)
Ian Rogersad0b3a32012-04-16 14:50:24 -07002863 << " from " << referrer << ")";
jeffhaob57e9522012-04-26 18:08:21 -07002864 return res_method;
jeffhao8cd6dda2012-02-22 10:15:34 -08002865 }
jeffhaode0d9c92012-02-27 13:58:13 -08002866 // Check that invoke-virtual and invoke-super are not used on private methods of the same class.
2867 if (res_method->IsPrivate() && method_type == METHOD_VIRTUAL) {
jeffhaod5347e02012-03-22 17:25:05 -07002868 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invoke-super/virtual can't be used on private method "
2869 << PrettyMethod(res_method);
jeffhaode0d9c92012-02-27 13:58:13 -08002870 return NULL;
2871 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002872 // Check that interface methods match interface classes.
2873 if (klass->IsInterface() && method_type != METHOD_INTERFACE) {
2874 Fail(VERIFY_ERROR_CLASS_CHANGE) << "non-interface method " << PrettyMethod(res_method)
2875 << " is in an interface class " << PrettyClass(klass);
2876 return NULL;
2877 } else if (!klass->IsInterface() && method_type == METHOD_INTERFACE) {
2878 Fail(VERIFY_ERROR_CLASS_CHANGE) << "interface method " << PrettyMethod(res_method)
2879 << " is in a non-interface class " << PrettyClass(klass);
2880 return NULL;
2881 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002882 // See if the method type implied by the invoke instruction matches the access flags for the
2883 // target method.
2884 if ((method_type == METHOD_DIRECT && !res_method->IsDirect()) ||
2885 (method_type == METHOD_STATIC && !res_method->IsStatic()) ||
2886 ((method_type == METHOD_VIRTUAL || method_type == METHOD_INTERFACE) && res_method->IsDirect())
2887 ) {
Ian Rogers2fc14272012-08-30 10:56:57 -07002888 Fail(VERIFY_ERROR_CLASS_CHANGE) << "invoke type (" << method_type << ") does not match method "
2889 " type of " << PrettyMethod(res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07002890 return NULL;
2891 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002892 return res_method;
2893}
2894
Sebastien Hertz5243e912013-05-21 10:55:07 +02002895mirror::AbstractMethod* MethodVerifier::VerifyInvocationArgs(const Instruction* inst,
2896 MethodType method_type,
2897 bool is_range,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002898 bool is_super) {
jeffhao8cd6dda2012-02-22 10:15:34 -08002899 // Resolve the method. This could be an abstract or concrete method depending on what sort of call
2900 // we're making.
Sebastien Hertz5243e912013-05-21 10:55:07 +02002901 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
2902 mirror::AbstractMethod* res_method = ResolveMethodAndCheckAccess(method_idx, method_type);
jeffhao8cd6dda2012-02-22 10:15:34 -08002903 if (res_method == NULL) { // error or class is unresolved
2904 return NULL;
2905 }
2906
Ian Rogersd81871c2011-10-03 13:57:23 -07002907 // If we're using invoke-super(method), make sure that the executing method's class' superclass
2908 // has a vtable entry for the target method.
2909 if (is_super) {
2910 DCHECK(method_type == METHOD_VIRTUAL);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002911 const RegType& super = GetDeclaringClass().GetSuperClass(&reg_types_);
Ian Rogers529781d2012-07-23 17:24:29 -07002912 if (super.IsUnresolvedTypes()) {
jeffhao4d8df822012-04-24 17:09:36 -07002913 Fail(VERIFY_ERROR_NO_METHOD) << "unknown super class in invoke-super from "
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002914 << PrettyMethod(dex_method_idx_, *dex_file_)
jeffhao4d8df822012-04-24 17:09:36 -07002915 << " to super " << PrettyMethod(res_method);
2916 return NULL;
2917 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002918 mirror::Class* super_klass = super.GetClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002919 if (res_method->GetMethodIndex() >= super_klass->GetVTable()->GetLength()) {
jeffhao4d8df822012-04-24 17:09:36 -07002920 MethodHelper mh(res_method);
2921 Fail(VERIFY_ERROR_NO_METHOD) << "invalid invoke-super from "
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002922 << PrettyMethod(dex_method_idx_, *dex_file_)
jeffhao4d8df822012-04-24 17:09:36 -07002923 << " to super " << super
2924 << "." << mh.GetName()
2925 << mh.GetSignature();
Ian Rogersd81871c2011-10-03 13:57:23 -07002926 return NULL;
2927 }
2928 }
2929 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002930 // match the call to the signature. Also, we might be calling through an abstract method
Ian Rogersd81871c2011-10-03 13:57:23 -07002931 // definition (which doesn't have register count values).
Sebastien Hertz5243e912013-05-21 10:55:07 +02002932 const size_t expected_args = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
Ian Rogersd81871c2011-10-03 13:57:23 -07002933 /* caught by static verifier */
2934 DCHECK(is_range || expected_args <= 5);
2935 if (expected_args > code_item_->outs_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07002936 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
Ian Rogersd81871c2011-10-03 13:57:23 -07002937 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
2938 return NULL;
2939 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002940
jeffhaobdb76512011-09-07 11:43:16 -07002941 /*
Ian Rogersad0b3a32012-04-16 14:50:24 -07002942 * Check the "this" argument, which must be an instance of the class that declared the method.
2943 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
2944 * rigorous check here (which is okay since we have to do it at runtime).
jeffhaobdb76512011-09-07 11:43:16 -07002945 */
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002946 size_t actual_args = 0;
Ian Rogersd81871c2011-10-03 13:57:23 -07002947 if (!res_method->IsStatic()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002948 const RegType& actual_arg_type = work_line_->GetInvocationThis(inst, is_range);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002949 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
Ian Rogersd81871c2011-10-03 13:57:23 -07002950 return NULL;
2951 }
2952 if (actual_arg_type.IsUninitializedReference() && !res_method->IsConstructor()) {
jeffhaod5347e02012-03-22 17:25:05 -07002953 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
Ian Rogersd81871c2011-10-03 13:57:23 -07002954 return NULL;
2955 }
2956 if (method_type != METHOD_INTERFACE && !actual_arg_type.IsZero()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002957 mirror::Class* klass = res_method->GetDeclaringClass();
Ian Rogers04f94f42013-06-10 15:09:26 -07002958 const RegType& res_method_class =
2959 reg_types_.FromClass(ClassHelper(klass).GetDescriptor(), klass,
2960 klass->CannotBeAssignedFromOtherTypes());
Ian Rogers9074b992011-10-26 17:41:55 -07002961 if (!res_method_class.IsAssignableFrom(actual_arg_type)) {
jeffhaod5347e02012-03-22 17:25:05 -07002962 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002963 << "' not instance of '" << res_method_class << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07002964 return NULL;
2965 }
2966 }
2967 actual_args++;
2968 }
2969 /*
2970 * Process the target method's signature. This signature may or may not
2971 * have been verified, so we can't assume it's properly formed.
2972 */
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002973 MethodHelper mh(res_method);
2974 const DexFile::TypeList* params = mh.GetParameterTypeList();
2975 size_t params_size = params == NULL ? 0 : params->Size();
Sebastien Hertz5243e912013-05-21 10:55:07 +02002976 uint32_t arg[5];
2977 if (!is_range) {
2978 inst->GetArgs(arg);
2979 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002980 for (size_t param_index = 0; param_index < params_size; param_index++) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002981 if (actual_args >= expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07002982 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invalid call to '" << PrettyMethod(res_method)
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002983 << "'. Expected " << expected_args << " arguments, processing argument " << actual_args
2984 << " (where longs/doubles count twice).";
Ian Rogersd81871c2011-10-03 13:57:23 -07002985 return NULL;
2986 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002987 const char* descriptor =
2988 mh.GetTypeDescriptorFromTypeIdx(params->GetTypeItem(param_index).type_idx_);
2989 if (descriptor == NULL) {
jeffhaod5347e02012-03-22 17:25:05 -07002990 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002991 << " missing signature component";
2992 return NULL;
Ian Rogersd81871c2011-10-03 13:57:23 -07002993 }
Ian Rogersb4903572012-10-11 11:52:56 -07002994 const RegType& reg_type = reg_types_.FromDescriptor(class_loader_, descriptor, false);
Sebastien Hertz5243e912013-05-21 10:55:07 +02002995 uint32_t get_reg = is_range ? inst->VRegC_3rc() + actual_args : arg[actual_args];
Ian Rogers84fa0742011-10-25 18:13:30 -07002996 if (!work_line_->VerifyRegisterType(get_reg, reg_type)) {
jeffhaob57e9522012-04-26 18:08:21 -07002997 return res_method;
Ian Rogersd81871c2011-10-03 13:57:23 -07002998 }
2999 actual_args = reg_type.IsLongOrDoubleTypes() ? actual_args + 2 : actual_args + 1;
3000 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003001 if (actual_args != expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07003002 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003003 << " expected " << expected_args << " arguments, found " << actual_args;
Ian Rogersd81871c2011-10-03 13:57:23 -07003004 return NULL;
3005 } else {
3006 return res_method;
3007 }
3008}
3009
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003010mirror::AbstractMethod* MethodVerifier::GetQuickInvokedMethod(const Instruction* inst,
3011 RegisterLine* reg_line,
3012 bool is_range) {
3013 DCHECK(inst->Opcode() == Instruction::INVOKE_VIRTUAL_QUICK ||
3014 inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
3015 const RegType& actual_arg_type = reg_line->GetInvocationThis(inst, is_range);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003016 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
3017 return NULL;
3018 }
3019 mirror::Class* this_class = NULL;
3020 if (!actual_arg_type.IsUnresolvedTypes()) {
3021 this_class = actual_arg_type.GetClass();
3022 } else {
3023 const std::string& descriptor(actual_arg_type.GetDescriptor());
3024 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
3025 this_class = class_linker->FindClass(descriptor.c_str(), class_loader_);
3026 if (this_class == NULL) {
3027 Thread::Current()->ClearException();
3028 // Look for a system class
3029 this_class = class_linker->FindClass(descriptor.c_str(), NULL);
3030 }
3031 }
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003032 if (this_class == NULL) {
3033 return NULL;
3034 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003035 mirror::ObjectArray<mirror::AbstractMethod>* vtable = this_class->GetVTable();
3036 CHECK(vtable != NULL);
3037 uint16_t vtable_index = is_range ? inst->VRegB_3rc() : inst->VRegB_35c();
3038 CHECK(vtable_index < vtable->GetLength());
3039 mirror::AbstractMethod* res_method = vtable->Get(vtable_index);
3040 CHECK(!Thread::Current()->IsExceptionPending());
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003041 return res_method;
3042}
3043
3044mirror::AbstractMethod* MethodVerifier::VerifyInvokeVirtualQuickArgs(const Instruction* inst,
3045 bool is_range) {
3046 DCHECK(Runtime::Current()->IsStarted());
3047 mirror::AbstractMethod* res_method = GetQuickInvokedMethod(inst, work_line_.get(),
3048 is_range);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003049 if (res_method == NULL) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003050 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer method from " << inst->Name();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003051 return NULL;
3052 }
3053 CHECK(!res_method->IsDirect() && !res_method->IsStatic());
3054
3055 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
3056 // match the call to the signature. Also, we might be calling through an abstract method
3057 // definition (which doesn't have register count values).
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003058 const RegType& actual_arg_type = work_line_->GetInvocationThis(inst, is_range);
3059 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
3060 return NULL;
3061 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003062 const size_t expected_args = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
3063 /* caught by static verifier */
3064 DCHECK(is_range || expected_args <= 5);
3065 if (expected_args > code_item_->outs_size_) {
3066 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
3067 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
3068 return NULL;
3069 }
3070
3071 /*
3072 * Check the "this" argument, which must be an instance of the class that declared the method.
3073 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
3074 * rigorous check here (which is okay since we have to do it at runtime).
3075 */
3076 if (actual_arg_type.IsUninitializedReference() && !res_method->IsConstructor()) {
3077 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
3078 return NULL;
3079 }
3080 if (!actual_arg_type.IsZero()) {
3081 mirror::Class* klass = res_method->GetDeclaringClass();
3082 const RegType& res_method_class =
3083 reg_types_.FromClass(ClassHelper(klass).GetDescriptor(), klass,
3084 klass->CannotBeAssignedFromOtherTypes());
3085 if (!res_method_class.IsAssignableFrom(actual_arg_type)) {
3086 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type
3087 << "' not instance of '" << res_method_class << "'";
3088 return NULL;
3089 }
3090 }
3091 /*
3092 * Process the target method's signature. This signature may or may not
3093 * have been verified, so we can't assume it's properly formed.
3094 */
3095 MethodHelper mh(res_method);
3096 const DexFile::TypeList* params = mh.GetParameterTypeList();
3097 size_t params_size = params == NULL ? 0 : params->Size();
3098 uint32_t arg[5];
3099 if (!is_range) {
3100 inst->GetArgs(arg);
3101 }
3102 size_t actual_args = 1;
3103 for (size_t param_index = 0; param_index < params_size; param_index++) {
3104 if (actual_args >= expected_args) {
3105 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invalid call to '" << PrettyMethod(res_method)
3106 << "'. Expected " << expected_args << " arguments, processing argument " << actual_args
3107 << " (where longs/doubles count twice).";
3108 return NULL;
3109 }
3110 const char* descriptor =
3111 mh.GetTypeDescriptorFromTypeIdx(params->GetTypeItem(param_index).type_idx_);
3112 if (descriptor == NULL) {
3113 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003114 << " missing signature component";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003115 return NULL;
3116 }
3117 const RegType& reg_type = reg_types_.FromDescriptor(class_loader_, descriptor, false);
3118 uint32_t get_reg = is_range ? inst->VRegC_3rc() + actual_args : arg[actual_args];
3119 if (!work_line_->VerifyRegisterType(get_reg, reg_type)) {
3120 return res_method;
3121 }
3122 actual_args = reg_type.IsLongOrDoubleTypes() ? actual_args + 2 : actual_args + 1;
3123 }
3124 if (actual_args != expected_args) {
3125 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
3126 << " expected " << expected_args << " arguments, found " << actual_args;
3127 return NULL;
3128 } else {
3129 return res_method;
3130 }
3131}
3132
Ian Rogers62342ec2013-06-11 10:26:37 -07003133void MethodVerifier::VerifyNewArray(const Instruction* inst, bool is_filled, bool is_range) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003134 uint32_t type_idx;
3135 if (!is_filled) {
3136 DCHECK_EQ(inst->Opcode(), Instruction::NEW_ARRAY);
3137 type_idx = inst->VRegC_22c();
3138 } else if (!is_range) {
3139 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY);
3140 type_idx = inst->VRegB_35c();
3141 } else {
3142 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY_RANGE);
3143 type_idx = inst->VRegB_3rc();
3144 }
3145 const RegType& res_type = ResolveClassAndCheckAccess(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003146 if (res_type.IsConflict()) { // bad class
3147 DCHECK_NE(failures_.size(), 0U);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003148 } else {
3149 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
3150 if (!res_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003151 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "new-array on non-array class " << res_type;
Ian Rogers0c4a5062012-02-03 15:18:59 -08003152 } else if (!is_filled) {
3153 /* make sure "size" register is valid type */
Sebastien Hertz5243e912013-05-21 10:55:07 +02003154 work_line_->VerifyRegisterType(inst->VRegB_22c(), reg_types_.Integer());
Ian Rogers0c4a5062012-02-03 15:18:59 -08003155 /* set register type to array class */
Ian Rogers62342ec2013-06-11 10:26:37 -07003156 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
3157 work_line_->SetRegisterType(inst->VRegA_22c(), precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003158 } else {
3159 // Verify each register. If "arg_count" is bad, VerifyRegisterType() will run off the end of
3160 // the list and fail. It's legal, if silly, for arg_count to be zero.
Ian Rogersad0b3a32012-04-16 14:50:24 -07003161 const RegType& expected_type = reg_types_.GetComponentType(res_type, class_loader_);
Sebastien Hertz5243e912013-05-21 10:55:07 +02003162 uint32_t arg_count = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
3163 uint32_t arg[5];
3164 if (!is_range) {
3165 inst->GetArgs(arg);
3166 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08003167 for (size_t ui = 0; ui < arg_count; ui++) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003168 uint32_t get_reg = is_range ? inst->VRegC_3rc() + ui : arg[ui];
Ian Rogers0c4a5062012-02-03 15:18:59 -08003169 if (!work_line_->VerifyRegisterType(get_reg, expected_type)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003170 work_line_->SetResultRegisterType(reg_types_.Conflict());
Ian Rogers0c4a5062012-02-03 15:18:59 -08003171 return;
3172 }
3173 }
3174 // filled-array result goes into "result" register
Ian Rogers62342ec2013-06-11 10:26:37 -07003175 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
3176 work_line_->SetResultRegisterType(precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003177 }
3178 }
3179}
3180
Sebastien Hertz5243e912013-05-21 10:55:07 +02003181void MethodVerifier::VerifyAGet(const Instruction* inst,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003182 const RegType& insn_type, bool is_primitive) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003183 const RegType& index_type = work_line_->GetRegisterType(inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07003184 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003185 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07003186 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003187 const RegType& array_type = work_line_->GetRegisterType(inst->VRegB_23x());
Ian Rogers89310de2012-02-01 13:47:30 -08003188 if (array_type.IsZero()) {
3189 // Null array class; this code path will fail at runtime. Infer a merge-able type from the
3190 // instruction type. TODO: have a proper notion of bottom here.
3191 if (!is_primitive || insn_type.IsCategory1Types()) {
3192 // Reference or category 1
Sebastien Hertz5243e912013-05-21 10:55:07 +02003193 work_line_->SetRegisterType(inst->VRegA_23x(), reg_types_.Zero());
Ian Rogersd81871c2011-10-03 13:57:23 -07003194 } else {
Ian Rogers89310de2012-02-01 13:47:30 -08003195 // Category 2
Sebastien Hertz5243e912013-05-21 10:55:07 +02003196 work_line_->SetRegisterTypeWide(inst->VRegA_23x(), reg_types_.FromCat2ConstLo(0, false),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003197 reg_types_.FromCat2ConstHi(0, false));
Ian Rogers89310de2012-02-01 13:47:30 -08003198 }
jeffhaofc3144e2012-02-01 17:21:15 -08003199 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003200 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aget";
Ian Rogers89310de2012-02-01 13:47:30 -08003201 } else {
3202 /* verify the class */
Ian Rogersad0b3a32012-04-16 14:50:24 -07003203 const RegType& component_type = reg_types_.GetComponentType(array_type, class_loader_);
jeffhaofc3144e2012-02-01 17:21:15 -08003204 if (!component_type.IsReferenceTypes() && !is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07003205 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08003206 << " source for aget-object";
3207 } else if (component_type.IsNonZeroReferenceTypes() && is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07003208 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08003209 << " source for category 1 aget";
3210 } else if (is_primitive && !insn_type.Equals(component_type) &&
3211 !((insn_type.IsInteger() && component_type.IsFloat()) ||
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003212 (insn_type.IsLong() && component_type.IsDouble()))) {
3213 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array type " << array_type
3214 << " incompatible with aget of type " << insn_type;
Ian Rogers89310de2012-02-01 13:47:30 -08003215 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003216 // Use knowledge of the field type which is stronger than the type inferred from the
3217 // instruction, which can't differentiate object types and ints from floats, longs from
3218 // doubles.
3219 if (!component_type.IsLowHalf()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003220 work_line_->SetRegisterType(inst->VRegA_23x(), component_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003221 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003222 work_line_->SetRegisterTypeWide(inst->VRegA_23x(), component_type,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003223 component_type.HighHalf(&reg_types_));
3224 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003225 }
3226 }
3227 }
3228}
3229
Sebastien Hertz5243e912013-05-21 10:55:07 +02003230void MethodVerifier::VerifyAPut(const Instruction* inst,
Ian Rogersd81871c2011-10-03 13:57:23 -07003231 const RegType& insn_type, bool is_primitive) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003232 const RegType& index_type = work_line_->GetRegisterType(inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07003233 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003234 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07003235 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003236 const RegType& array_type = work_line_->GetRegisterType(inst->VRegB_23x());
Ian Rogers89310de2012-02-01 13:47:30 -08003237 if (array_type.IsZero()) {
3238 // Null array type; this code path will fail at runtime. Infer a merge-able type from the
3239 // instruction type.
jeffhaofc3144e2012-02-01 17:21:15 -08003240 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003241 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aput";
Ian Rogers89310de2012-02-01 13:47:30 -08003242 } else {
3243 /* verify the class */
Ian Rogersad0b3a32012-04-16 14:50:24 -07003244 const RegType& component_type = reg_types_.GetComponentType(array_type, class_loader_);
jeffhaofc3144e2012-02-01 17:21:15 -08003245 if (!component_type.IsReferenceTypes() && !is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07003246 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08003247 << " source for aput-object";
3248 } else if (component_type.IsNonZeroReferenceTypes() && is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07003249 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08003250 << " source for category 1 aput";
3251 } else if (is_primitive && !insn_type.Equals(component_type) &&
3252 !((insn_type.IsInteger() && component_type.IsFloat()) ||
3253 (insn_type.IsLong() && component_type.IsDouble()))) {
jeffhaod5347e02012-03-22 17:25:05 -07003254 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08003255 << " incompatible with aput of type " << insn_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07003256 } else {
Ian Rogers89310de2012-02-01 13:47:30 -08003257 // The instruction agrees with the type of array, confirm the value to be stored does too
3258 // Note: we use the instruction type (rather than the component type) for aput-object as
3259 // incompatible classes will be caught at runtime as an array store exception
Sebastien Hertz5243e912013-05-21 10:55:07 +02003260 work_line_->VerifyRegisterType(inst->VRegA_23x(), is_primitive ? component_type : insn_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07003261 }
3262 }
3263 }
3264}
3265
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003266mirror::Field* MethodVerifier::GetStaticField(int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08003267 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3268 // Check access to class
3269 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003270 if (klass_type.IsConflict()) { // bad class
3271 AppendToLastFailMessage(StringPrintf(" in attempt to access static field %d (%s) in %s",
3272 field_idx, dex_file_->GetFieldName(field_id),
3273 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers90040192011-12-16 08:54:29 -08003274 return NULL;
3275 }
Elliott Hughesb25c3f62012-03-26 16:35:06 -07003276 if (klass_type.IsUnresolvedTypes()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003277 return NULL; // Can't resolve Class so no more to do here, will do checking at runtime.
Ian Rogers90040192011-12-16 08:54:29 -08003278 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003279 mirror::Field* field = Runtime::Current()->GetClassLinker()->ResolveFieldJLS(*dex_file_, field_idx,
Ian Rogersad0b3a32012-04-16 14:50:24 -07003280 dex_cache_, class_loader_);
Ian Rogersd81871c2011-10-03 13:57:23 -07003281 if (field == NULL) {
Ian Rogers637c65b2013-05-31 11:46:00 -07003282 LOG(INFO) << "Unable to resolve static field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07003283 << dex_file_->GetFieldName(field_id) << ") in "
3284 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07003285 DCHECK(Thread::Current()->IsExceptionPending());
3286 Thread::Current()->ClearException();
3287 return NULL;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003288 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
3289 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003290 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access static field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07003291 << " from " << GetDeclaringClass();
Ian Rogersd81871c2011-10-03 13:57:23 -07003292 return NULL;
3293 } else if (!field->IsStatic()) {
3294 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field) << " to be static";
3295 return NULL;
3296 } else {
3297 return field;
3298 }
3299}
3300
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003301mirror::Field* MethodVerifier::GetInstanceField(const RegType& obj_type, int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08003302 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3303 // Check access to class
3304 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003305 if (klass_type.IsConflict()) {
3306 AppendToLastFailMessage(StringPrintf(" in attempt to access instance field %d (%s) in %s",
3307 field_idx, dex_file_->GetFieldName(field_id),
3308 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers90040192011-12-16 08:54:29 -08003309 return NULL;
3310 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003311 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers90040192011-12-16 08:54:29 -08003312 return NULL; // Can't resolve Class so no more to do here
3313 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003314 mirror::Field* field = Runtime::Current()->GetClassLinker()->ResolveFieldJLS(*dex_file_, field_idx,
Ian Rogersad0b3a32012-04-16 14:50:24 -07003315 dex_cache_, class_loader_);
Ian Rogersd81871c2011-10-03 13:57:23 -07003316 if (field == NULL) {
Ian Rogers637c65b2013-05-31 11:46:00 -07003317 LOG(INFO) << "Unable to resolve instance field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07003318 << dex_file_->GetFieldName(field_id) << ") in "
3319 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07003320 DCHECK(Thread::Current()->IsExceptionPending());
3321 Thread::Current()->ClearException();
3322 return NULL;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003323 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
3324 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003325 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access instance field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07003326 << " from " << GetDeclaringClass();
Ian Rogersd81871c2011-10-03 13:57:23 -07003327 return NULL;
3328 } else if (field->IsStatic()) {
3329 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field)
3330 << " to not be static";
3331 return NULL;
3332 } else if (obj_type.IsZero()) {
3333 // Cannot infer and check type, however, access will cause null pointer exception
3334 return field;
Ian Rogerse1758fe2012-04-19 11:31:15 -07003335 } else {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003336 mirror::Class* klass = field->GetDeclaringClass();
Ian Rogers637c65b2013-05-31 11:46:00 -07003337 const RegType& field_klass =
3338 reg_types_.FromClass(dex_file_->GetFieldDeclaringClassDescriptor(field_id),
Ian Rogers04f94f42013-06-10 15:09:26 -07003339 klass, klass->CannotBeAssignedFromOtherTypes());
Ian Rogersad0b3a32012-04-16 14:50:24 -07003340 if (obj_type.IsUninitializedTypes() &&
3341 (!IsConstructor() || GetDeclaringClass().Equals(obj_type) ||
3342 !field_klass.Equals(GetDeclaringClass()))) {
3343 // Field accesses through uninitialized references are only allowable for constructors where
3344 // the field is declared in this class
3345 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "cannot access instance field " << PrettyField(field)
3346 << " of a not fully initialized object within the context of "
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003347 << PrettyMethod(dex_method_idx_, *dex_file_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003348 return NULL;
3349 } else if (!field_klass.IsAssignableFrom(obj_type)) {
3350 // Trying to access C1.field1 using reference of type C2, which is neither C1 or a sub-class
3351 // of C1. For resolution to occur the declared class of the field must be compatible with
3352 // obj_type, we've discovered this wasn't so, so report the field didn't exist.
3353 Fail(VERIFY_ERROR_NO_FIELD) << "cannot access instance field " << PrettyField(field)
3354 << " from object of type " << obj_type;
3355 return NULL;
3356 } else {
3357 return field;
3358 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003359 }
3360}
3361
Sebastien Hertz5243e912013-05-21 10:55:07 +02003362void MethodVerifier::VerifyISGet(const Instruction* inst, const RegType& insn_type,
3363 bool is_primitive, bool is_static) {
3364 uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003365 mirror::Field* field;
Ian Rogersb94a27b2011-10-26 00:33:41 -07003366 if (is_static) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07003367 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003368 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003369 const RegType& object_type = work_line_->GetRegisterType(inst->VRegB_22c());
Ian Rogersf4028cc2011-11-02 14:56:39 -07003370 field = GetInstanceField(object_type, field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003371 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003372 const char* descriptor;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003373 mirror::ClassLoader* loader;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003374 if (field != NULL) {
3375 descriptor = FieldHelper(field).GetTypeDescriptor();
3376 loader = field->GetDeclaringClass()->GetClassLoader();
Ian Rogersf4028cc2011-11-02 14:56:39 -07003377 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003378 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3379 descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
3380 loader = class_loader_;
Ian Rogers0d604842012-04-16 14:50:24 -07003381 }
Ian Rogersb4903572012-10-11 11:52:56 -07003382 const RegType& field_type = reg_types_.FromDescriptor(loader, descriptor, false);
Sebastien Hertz5243e912013-05-21 10:55:07 +02003383 const uint32_t vregA = (is_static) ? inst->VRegA_21c() : inst->VRegA_22c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07003384 if (is_primitive) {
3385 if (field_type.Equals(insn_type) ||
3386 (field_type.IsFloat() && insn_type.IsIntegralTypes()) ||
3387 (field_type.IsDouble() && insn_type.IsLongTypes())) {
3388 // expected that read is of the correct primitive type or that int reads are reading
3389 // floats or long reads are reading doubles
3390 } else {
3391 // This is a global failure rather than a class change failure as the instructions and
3392 // the descriptors for the type should have been consistent within the same file at
3393 // compile time
3394 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
3395 << " to be of type '" << insn_type
3396 << "' but found type '" << field_type << "' in get";
Ian Rogersad0b3a32012-04-16 14:50:24 -07003397 return;
3398 }
3399 } else {
3400 if (!insn_type.IsAssignableFrom(field_type)) {
3401 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
3402 << " to be compatible with type '" << insn_type
3403 << "' but found type '" << field_type
3404 << "' in get-object";
Sebastien Hertz5243e912013-05-21 10:55:07 +02003405 work_line_->SetRegisterType(vregA, reg_types_.Conflict());
Ian Rogersad0b3a32012-04-16 14:50:24 -07003406 return;
3407 }
3408 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003409 if (!field_type.IsLowHalf()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003410 work_line_->SetRegisterType(vregA, field_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003411 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003412 work_line_->SetRegisterTypeWide(vregA, field_type, field_type.HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003413 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003414}
3415
Sebastien Hertz5243e912013-05-21 10:55:07 +02003416void MethodVerifier::VerifyISPut(const Instruction* inst, const RegType& insn_type,
3417 bool is_primitive, bool is_static) {
3418 uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003419 mirror::Field* field;
Ian Rogersb94a27b2011-10-26 00:33:41 -07003420 if (is_static) {
Ian Rogers55d249f2011-11-02 16:48:09 -07003421 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003422 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003423 const RegType& object_type = work_line_->GetRegisterType(inst->VRegB_22c());
Ian Rogers55d249f2011-11-02 16:48:09 -07003424 field = GetInstanceField(object_type, field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003425 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003426 const char* descriptor;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003427 mirror::ClassLoader* loader;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003428 if (field != NULL) {
3429 descriptor = FieldHelper(field).GetTypeDescriptor();
3430 loader = field->GetDeclaringClass()->GetClassLoader();
Ian Rogers55d249f2011-11-02 16:48:09 -07003431 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003432 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3433 descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
3434 loader = class_loader_;
3435 }
Ian Rogersb4903572012-10-11 11:52:56 -07003436 const RegType& field_type = reg_types_.FromDescriptor(loader, descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003437 if (field != NULL) {
3438 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
3439 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
3440 << " from other class " << GetDeclaringClass();
3441 return;
3442 }
3443 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02003444 const uint32_t vregA = (is_static) ? inst->VRegA_21c() : inst->VRegA_22c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07003445 if (is_primitive) {
3446 // Primitive field assignability rules are weaker than regular assignability rules
3447 bool instruction_compatible;
3448 bool value_compatible;
Sebastien Hertz5243e912013-05-21 10:55:07 +02003449 const RegType& value_type = work_line_->GetRegisterType(vregA);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003450 if (field_type.IsIntegralTypes()) {
3451 instruction_compatible = insn_type.IsIntegralTypes();
3452 value_compatible = value_type.IsIntegralTypes();
3453 } else if (field_type.IsFloat()) {
3454 instruction_compatible = insn_type.IsInteger(); // no [is]put-float, so expect [is]put-int
3455 value_compatible = value_type.IsFloatTypes();
3456 } else if (field_type.IsLong()) {
3457 instruction_compatible = insn_type.IsLong();
3458 value_compatible = value_type.IsLongTypes();
3459 } else if (field_type.IsDouble()) {
3460 instruction_compatible = insn_type.IsLong(); // no [is]put-double, so expect [is]put-long
3461 value_compatible = value_type.IsDoubleTypes();
Ian Rogers55d249f2011-11-02 16:48:09 -07003462 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003463 instruction_compatible = false; // reference field with primitive store
3464 value_compatible = false; // unused
Ian Rogersd81871c2011-10-03 13:57:23 -07003465 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003466 if (!instruction_compatible) {
3467 // This is a global failure rather than a class change failure as the instructions and
3468 // the descriptors for the type should have been consistent within the same file at
3469 // compile time
3470 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
3471 << " to be of type '" << insn_type
3472 << "' but found type '" << field_type
3473 << "' in put";
3474 return;
Ian Rogers55d249f2011-11-02 16:48:09 -07003475 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003476 if (!value_compatible) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003477 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
Ian Rogersad0b3a32012-04-16 14:50:24 -07003478 << " of type " << value_type
3479 << " but expected " << field_type
3480 << " for store to " << PrettyField(field) << " in put";
3481 return;
Ian Rogersd81871c2011-10-03 13:57:23 -07003482 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003483 } else {
3484 if (!insn_type.IsAssignableFrom(field_type)) {
3485 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
3486 << " to be compatible with type '" << insn_type
3487 << "' but found type '" << field_type
3488 << "' in put-object";
3489 return;
3490 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02003491 work_line_->VerifyRegisterType(vregA, field_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07003492 }
3493}
3494
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003495// Look for an instance field with this offset.
3496// TODO: we may speed up the search if offsets are sorted by doing a quick search.
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003497static mirror::Field* FindInstanceFieldWithOffset(const mirror::Class* klass,
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003498 uint32_t field_offset)
3499 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003500 const mirror::ObjectArray<mirror::Field>* instance_fields = klass->GetIFields();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003501 if (instance_fields != NULL) {
3502 for (int32_t i = 0, e = instance_fields->GetLength(); i < e; ++i) {
3503 mirror::Field* field = instance_fields->Get(i);
3504 if (field->GetOffset().Uint32Value() == field_offset) {
3505 return field;
3506 }
3507 }
3508 }
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003509 // We did not find field in class: look into superclass.
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003510 if (klass->GetSuperClass() != NULL) {
3511 return FindInstanceFieldWithOffset(klass->GetSuperClass(), field_offset);
3512 } else {
3513 return NULL;
3514 }
3515}
3516
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003517// Returns the access field of a quick field access (iget/iput-quick) or NULL
3518// if it cannot be found.
3519mirror::Field* MethodVerifier::GetQuickFieldAccess(const Instruction* inst,
3520 RegisterLine* reg_line) {
3521 DCHECK(inst->Opcode() == Instruction::IGET_QUICK ||
3522 inst->Opcode() == Instruction::IGET_WIDE_QUICK ||
3523 inst->Opcode() == Instruction::IGET_OBJECT_QUICK ||
3524 inst->Opcode() == Instruction::IPUT_QUICK ||
3525 inst->Opcode() == Instruction::IPUT_WIDE_QUICK ||
3526 inst->Opcode() == Instruction::IPUT_OBJECT_QUICK);
3527 const RegType& object_type = reg_line->GetRegisterType(inst->VRegB_22c());
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003528 mirror::Class* object_class = NULL;
3529 if (!object_type.IsUnresolvedTypes()) {
3530 object_class = object_type.GetClass();
3531 } else {
3532 // We need to resolve the class from its descriptor.
3533 const std::string& descriptor(object_type.GetDescriptor());
3534 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
3535 object_class = class_linker->FindClass(descriptor.c_str(), class_loader_);
3536 if (object_class == NULL) {
3537 Thread::Current()->ClearException();
3538 // Look for a system class
3539 object_class = class_linker->FindClass(descriptor.c_str(), NULL);
3540 }
3541 }
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003542 if (object_class == NULL) {
3543 // Failed to get the Class* from reg type.
3544 LOG(WARNING) << "Failed to get Class* from " << object_type;
3545 return NULL;
3546 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003547 uint32_t field_offset = static_cast<uint32_t>(inst->VRegC_22c());
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003548 return FindInstanceFieldWithOffset(object_class, field_offset);
3549}
3550
3551void MethodVerifier::VerifyIGetQuick(const Instruction* inst, const RegType& insn_type,
3552 bool is_primitive) {
3553 DCHECK(Runtime::Current()->IsStarted());
3554 mirror::Field* field = GetQuickFieldAccess(inst, work_line_.get());
3555 if (field == NULL) {
3556 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field from " << inst->Name();
3557 return;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003558 }
3559 const char* descriptor = FieldHelper(field).GetTypeDescriptor();
3560 mirror::ClassLoader* loader = field->GetDeclaringClass()->GetClassLoader();
3561 const RegType& field_type = reg_types_.FromDescriptor(loader, descriptor, false);
3562 const uint32_t vregA = inst->VRegA_22c();
3563 if (is_primitive) {
3564 if (field_type.Equals(insn_type) ||
3565 (field_type.IsFloat() && insn_type.IsIntegralTypes()) ||
3566 (field_type.IsDouble() && insn_type.IsLongTypes())) {
3567 // expected that read is of the correct primitive type or that int reads are reading
3568 // floats or long reads are reading doubles
3569 } else {
3570 // This is a global failure rather than a class change failure as the instructions and
3571 // the descriptors for the type should have been consistent within the same file at
3572 // compile time
3573 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003574 << " to be of type '" << insn_type
3575 << "' but found type '" << field_type << "' in get";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003576 return;
3577 }
3578 } else {
3579 if (!insn_type.IsAssignableFrom(field_type)) {
3580 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003581 << " to be compatible with type '" << insn_type
3582 << "' but found type '" << field_type
3583 << "' in get-object";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003584 work_line_->SetRegisterType(vregA, reg_types_.Conflict());
3585 return;
3586 }
3587 }
3588 if (!field_type.IsLowHalf()) {
3589 work_line_->SetRegisterType(vregA, field_type);
3590 } else {
3591 work_line_->SetRegisterTypeWide(vregA, field_type, field_type.HighHalf(&reg_types_));
3592 }
3593}
3594
3595void MethodVerifier::VerifyIPutQuick(const Instruction* inst, const RegType& insn_type,
3596 bool is_primitive) {
3597 DCHECK(Runtime::Current()->IsStarted());
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003598 mirror::Field* field = GetQuickFieldAccess(inst, work_line_.get());
3599 if (field == NULL) {
3600 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field from " << inst->Name();
3601 return;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003602 }
3603 const char* descriptor = FieldHelper(field).GetTypeDescriptor();
3604 mirror::ClassLoader* loader = field->GetDeclaringClass()->GetClassLoader();
3605 const RegType& field_type = reg_types_.FromDescriptor(loader, descriptor, false);
3606 if (field != NULL) {
3607 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
3608 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003609 << " from other class " << GetDeclaringClass();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003610 return;
3611 }
3612 }
3613 const uint32_t vregA = inst->VRegA_22c();
3614 if (is_primitive) {
3615 // Primitive field assignability rules are weaker than regular assignability rules
3616 bool instruction_compatible;
3617 bool value_compatible;
3618 const RegType& value_type = work_line_->GetRegisterType(vregA);
3619 if (field_type.IsIntegralTypes()) {
3620 instruction_compatible = insn_type.IsIntegralTypes();
3621 value_compatible = value_type.IsIntegralTypes();
3622 } else if (field_type.IsFloat()) {
3623 instruction_compatible = insn_type.IsInteger(); // no [is]put-float, so expect [is]put-int
3624 value_compatible = value_type.IsFloatTypes();
3625 } else if (field_type.IsLong()) {
3626 instruction_compatible = insn_type.IsLong();
3627 value_compatible = value_type.IsLongTypes();
3628 } else if (field_type.IsDouble()) {
3629 instruction_compatible = insn_type.IsLong(); // no [is]put-double, so expect [is]put-long
3630 value_compatible = value_type.IsDoubleTypes();
3631 } else {
3632 instruction_compatible = false; // reference field with primitive store
3633 value_compatible = false; // unused
3634 }
3635 if (!instruction_compatible) {
3636 // This is a global failure rather than a class change failure as the instructions and
3637 // the descriptors for the type should have been consistent within the same file at
3638 // compile time
3639 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003640 << " to be of type '" << insn_type
3641 << "' but found type '" << field_type
3642 << "' in put";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003643 return;
3644 }
3645 if (!value_compatible) {
3646 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
3647 << " of type " << value_type
3648 << " but expected " << field_type
3649 << " for store to " << PrettyField(field) << " in put";
3650 return;
3651 }
3652 } else {
3653 if (!insn_type.IsAssignableFrom(field_type)) {
3654 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003655 << " to be compatible with type '" << insn_type
3656 << "' but found type '" << field_type
3657 << "' in put-object";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003658 return;
3659 }
3660 work_line_->VerifyRegisterType(vregA, field_type);
3661 }
3662}
3663
Ian Rogers776ac1f2012-04-13 23:36:36 -07003664bool MethodVerifier::CheckNotMoveException(const uint16_t* insns, int insn_idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003665 if ((insns[insn_idx] & 0xff) == Instruction::MOVE_EXCEPTION) {
jeffhaod5347e02012-03-22 17:25:05 -07003666 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-exception";
Ian Rogersd81871c2011-10-03 13:57:23 -07003667 return false;
3668 }
3669 return true;
3670}
3671
Ian Rogers776ac1f2012-04-13 23:36:36 -07003672bool MethodVerifier::UpdateRegisters(uint32_t next_insn, const RegisterLine* merge_line) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003673 bool changed = true;
3674 RegisterLine* target_line = reg_table_.GetLine(next_insn);
3675 if (!insn_flags_[next_insn].IsVisitedOrChanged()) {
jeffhaobdb76512011-09-07 11:43:16 -07003676 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003677 * We haven't processed this instruction before, and we haven't touched the registers here, so
3678 * there's nothing to "merge". Copy the registers over and mark it as changed. (This is the
3679 * only way a register can transition out of "unknown", so this is not just an optimization.)
jeffhaobdb76512011-09-07 11:43:16 -07003680 */
Ian Rogersb8c78592013-07-25 23:52:52 +00003681 if (!insn_flags_[next_insn].IsReturn()) {
3682 target_line->CopyFromLine(merge_line);
3683 } else {
3684 // For returns we only care about the operand to the return, all other registers are dead.
3685 // Initialize them as conflicts so they don't add to GC and deoptimization information.
3686 const Instruction* ret_inst = Instruction::At(code_item_->insns_ + next_insn);
3687 Instruction::Code opcode = ret_inst->Opcode();
3688 if ((opcode == Instruction::RETURN_VOID) || (opcode == Instruction::RETURN_VOID_BARRIER)) {
3689 target_line->MarkAllRegistersAsConflicts();
3690 } else {
3691 target_line->CopyFromLine(merge_line);
3692 if (opcode == Instruction::RETURN_WIDE) {
3693 target_line->MarkAllRegistersAsConflictsExceptWide(ret_inst->VRegA_11x());
3694 } else {
3695 target_line->MarkAllRegistersAsConflictsExcept(ret_inst->VRegA_11x());
3696 }
3697 }
3698 }
jeffhaobdb76512011-09-07 11:43:16 -07003699 } else {
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003700 UniquePtr<RegisterLine> copy(gDebugVerify ? new RegisterLine(target_line->NumRegs(), this) : NULL);
3701 if (gDebugVerify) {
3702 copy->CopyFromLine(target_line);
3703 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003704 changed = target_line->MergeRegisters(merge_line);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003705 if (have_pending_hard_failure_) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003706 return false;
jeffhaobdb76512011-09-07 11:43:16 -07003707 }
Ian Rogers2c8a8572011-10-24 17:11:36 -07003708 if (gDebugVerify && changed) {
Elliott Hughes398f64b2012-03-26 18:05:48 -07003709 LogVerifyInfo() << "Merging at [" << reinterpret_cast<void*>(work_insn_idx_) << "]"
Elliott Hughesc073b072012-05-24 19:29:17 -07003710 << " to [" << reinterpret_cast<void*>(next_insn) << "]: " << "\n"
3711 << *copy.get() << " MERGE\n"
3712 << *merge_line << " ==\n"
3713 << *target_line << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07003714 }
3715 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003716 if (changed) {
3717 insn_flags_[next_insn].SetChanged();
jeffhaobdb76512011-09-07 11:43:16 -07003718 }
3719 return true;
3720}
3721
Ian Rogers7b3ddd22013-02-21 15:19:52 -08003722InstructionFlags* MethodVerifier::CurrentInsnFlags() {
Ian Rogers776ac1f2012-04-13 23:36:36 -07003723 return &insn_flags_[work_insn_idx_];
3724}
3725
Ian Rogersad0b3a32012-04-16 14:50:24 -07003726const RegType& MethodVerifier::GetMethodReturnType() {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003727 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003728 const DexFile::ProtoId& proto_id = dex_file_->GetMethodPrototype(method_id);
3729 uint16_t return_type_idx = proto_id.return_type_idx_;
3730 const char* descriptor = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(return_type_idx));
Ian Rogersb4903572012-10-11 11:52:56 -07003731 return reg_types_.FromDescriptor(class_loader_, descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003732}
3733
3734const RegType& MethodVerifier::GetDeclaringClass() {
Ian Rogers637c65b2013-05-31 11:46:00 -07003735 if (declaring_class_ == NULL) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003736 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003737 const char* descriptor = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(method_id.class_idx_));
Ian Rogers637c65b2013-05-31 11:46:00 -07003738 if (mirror_method_ != NULL) {
3739 mirror::Class* klass = mirror_method_->GetDeclaringClass();
Ian Rogers04f94f42013-06-10 15:09:26 -07003740 declaring_class_ = &reg_types_.FromClass(descriptor, klass,
3741 klass->CannotBeAssignedFromOtherTypes());
Ian Rogers637c65b2013-05-31 11:46:00 -07003742 } else {
3743 declaring_class_ = &reg_types_.FromDescriptor(class_loader_, descriptor, false);
3744 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003745 }
Ian Rogers637c65b2013-05-31 11:46:00 -07003746 return *declaring_class_;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003747}
3748
Ian Rogers776ac1f2012-04-13 23:36:36 -07003749void MethodVerifier::ComputeGcMapSizes(size_t* gc_points, size_t* ref_bitmap_bits,
Ian Rogers6d376ae2013-07-23 15:12:40 -07003750 size_t* log2_max_gc_pc) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003751 size_t local_gc_points = 0;
3752 size_t max_insn = 0;
3753 size_t max_ref_reg = -1;
3754 for (size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003755 if (insn_flags_[i].IsCompileTimeInfoPoint()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003756 local_gc_points++;
3757 max_insn = i;
3758 RegisterLine* line = reg_table_.GetLine(i);
Ian Rogers84fa0742011-10-25 18:13:30 -07003759 max_ref_reg = line->GetMaxNonZeroReferenceReg(max_ref_reg);
jeffhaobdb76512011-09-07 11:43:16 -07003760 }
3761 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003762 *gc_points = local_gc_points;
3763 *ref_bitmap_bits = max_ref_reg + 1; // if max register is 0 we need 1 bit to encode (ie +1)
3764 size_t i = 0;
Ian Rogers6b0870d2011-12-15 19:38:12 -08003765 while ((1U << i) <= max_insn) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003766 i++;
3767 }
3768 *log2_max_gc_pc = i;
jeffhaobdb76512011-09-07 11:43:16 -07003769}
3770
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07003771MethodVerifier::MethodSafeCastSet* MethodVerifier::GenerateSafeCastSet() {
3772 /*
3773 * Walks over the method code and adds any cast instructions in which
3774 * the type cast is implicit to a set, which is used in the code generation
3775 * to elide these casts.
3776 */
3777 if (!failure_messages_.empty()) {
3778 return NULL;
3779 }
3780 UniquePtr<MethodSafeCastSet> mscs;
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07003781 const Instruction* inst = Instruction::At(code_item_->insns_);
3782 const Instruction* end = Instruction::At(code_item_->insns_ +
Ian Rogersfae370a2013-06-05 08:33:27 -07003783 code_item_->insns_size_in_code_units_);
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07003784
3785 for (; inst < end; inst = inst->Next()) {
Ian Rogersfae370a2013-06-05 08:33:27 -07003786 if (Instruction::CHECK_CAST != inst->Opcode()) {
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07003787 continue;
Ian Rogersfae370a2013-06-05 08:33:27 -07003788 }
3789 uint32_t dex_pc = inst->GetDexPc(code_item_->insns_);
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07003790 RegisterLine* line = reg_table_.GetLine(dex_pc);
Ian Rogersfae370a2013-06-05 08:33:27 -07003791 const RegType& reg_type(line->GetRegisterType(inst->VRegA_21c()));
3792 const RegType& cast_type = ResolveClassAndCheckAccess(inst->VRegB_21c());
Ian Rogers16e3d2c2013-06-07 12:57:00 -07003793 if (cast_type.IsStrictlyAssignableFrom(reg_type)) {
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07003794 if (mscs.get() == NULL) {
3795 mscs.reset(new MethodSafeCastSet());
3796 }
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07003797 mscs->insert(dex_pc);
3798 }
3799 }
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07003800 return mscs.release();
3801}
3802
Ian Rogersd0583802013-06-01 10:51:46 -07003803MethodVerifier::PcToConcreteMethodMap* MethodVerifier::GenerateDevirtMap() {
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003804 // It is risky to rely on reg_types for sharpening in cases of soft
3805 // verification, we might end up sharpening to a wrong implementation. Just abort.
3806 if (!failure_messages_.empty()) {
3807 return NULL;
3808 }
3809
Ian Rogersd0583802013-06-01 10:51:46 -07003810 UniquePtr<PcToConcreteMethodMap> pc_to_concrete_method_map;
Brian Carlstrom02c8cc62013-07-18 15:54:44 -07003811 const uint16_t* insns = code_item_->insns_;
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003812 const Instruction* inst = Instruction::At(insns);
Ian Rogersd0583802013-06-01 10:51:46 -07003813 const Instruction* end = Instruction::At(insns + code_item_->insns_size_in_code_units_);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003814
Ian Rogersd0583802013-06-01 10:51:46 -07003815 for (; inst < end; inst = inst->Next()) {
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003816 bool is_virtual = (inst->Opcode() == Instruction::INVOKE_VIRTUAL) ||
3817 (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE);
3818 bool is_interface = (inst->Opcode() == Instruction::INVOKE_INTERFACE) ||
3819 (inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE);
3820
Brian Carlstromdf629502013-07-17 22:39:56 -07003821 if (!is_interface && !is_virtual) {
Dragos Sbirlea29e2e7e2013-05-22 14:52:11 -07003822 continue;
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003823 }
Ian Rogersd0583802013-06-01 10:51:46 -07003824 // Get reg type for register holding the reference to the object that will be dispatched upon.
3825 uint32_t dex_pc = inst->GetDexPc(insns);
3826 RegisterLine* line = reg_table_.GetLine(dex_pc);
3827 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE) ||
3828 (inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE);
3829 const RegType&
3830 reg_type(line->GetRegisterType(is_range ? inst->VRegC_3rc() : inst->VRegC_35c()));
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003831
Ian Rogersd0583802013-06-01 10:51:46 -07003832 if (!reg_type.HasClass()) {
3833 // We will compute devirtualization information only when we know the Class of the reg type.
3834 continue;
3835 }
3836 mirror::Class* reg_class = reg_type.GetClass();
3837 if (reg_class->IsInterface()) {
3838 // We can't devirtualize when the known type of the register is an interface.
3839 continue;
3840 }
3841 if (reg_class->IsAbstract() && !reg_class->IsArrayClass()) {
3842 // We can't devirtualize abstract classes except on arrays of abstract classes.
3843 continue;
3844 }
3845 mirror::AbstractMethod* abstract_method =
3846 dex_cache_->GetResolvedMethod(is_range ? inst->VRegB_3rc() : inst->VRegB_35c());
Brian Carlstromdf629502013-07-17 22:39:56 -07003847 if (abstract_method == NULL) {
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003848 // If the method is not found in the cache this means that it was never found
3849 // by ResolveMethodAndCheckAccess() called when verifying invoke_*.
3850 continue;
3851 }
3852 // Find the concrete method.
3853 mirror::AbstractMethod* concrete_method = NULL;
3854 if (is_interface) {
3855 concrete_method = reg_type.GetClass()->FindVirtualMethodForInterface(abstract_method);
3856 }
3857 if (is_virtual) {
3858 concrete_method = reg_type.GetClass()->FindVirtualMethodForVirtual(abstract_method);
3859 }
Ian Rogersd0583802013-06-01 10:51:46 -07003860 if (concrete_method == NULL || concrete_method->IsAbstract()) {
3861 // In cases where concrete_method is not found, or is abstract, continue to the next invoke.
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003862 continue;
3863 }
Ian Rogersd0583802013-06-01 10:51:46 -07003864 if (reg_type.IsPreciseReference() || concrete_method->IsFinal() ||
3865 concrete_method->GetDeclaringClass()->IsFinal()) {
3866 // If we knew exactly the class being dispatched upon, or if the target method cannot be
3867 // overridden record the target to be used in the compiler driver.
3868 if (pc_to_concrete_method_map.get() == NULL) {
3869 pc_to_concrete_method_map.reset(new PcToConcreteMethodMap());
3870 }
Brian Carlstrom51c24672013-07-11 16:00:56 -07003871 MethodReference concrete_ref(
Ian Rogersd0583802013-06-01 10:51:46 -07003872 concrete_method->GetDeclaringClass()->GetDexCache()->GetDexFile(),
3873 concrete_method->GetDexMethodIndex());
3874 pc_to_concrete_method_map->Put(dex_pc, concrete_ref);
3875 }
Dragos Sbirlea29e2e7e2013-05-22 14:52:11 -07003876 }
Ian Rogersd0583802013-06-01 10:51:46 -07003877 return pc_to_concrete_method_map.release();
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003878}
3879
Ian Rogers776ac1f2012-04-13 23:36:36 -07003880const std::vector<uint8_t>* MethodVerifier::GenerateGcMap() {
Ian Rogersd81871c2011-10-03 13:57:23 -07003881 size_t num_entries, ref_bitmap_bits, pc_bits;
3882 ComputeGcMapSizes(&num_entries, &ref_bitmap_bits, &pc_bits);
3883 // There's a single byte to encode the size of each bitmap
jeffhao60f83e32012-02-13 17:16:30 -08003884 if (ref_bitmap_bits >= (8 /* bits per byte */ * 8192 /* 13-bit size */ )) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003885 // TODO: either a better GC map format or per method failures
jeffhaod5347e02012-03-22 17:25:05 -07003886 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot encode GC map for method with "
Ian Rogersd81871c2011-10-03 13:57:23 -07003887 << ref_bitmap_bits << " registers";
jeffhaobdb76512011-09-07 11:43:16 -07003888 return NULL;
3889 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003890 size_t ref_bitmap_bytes = (ref_bitmap_bits + 7) / 8;
3891 // There are 2 bytes to encode the number of entries
3892 if (num_entries >= 65536) {
3893 // TODO: either a better GC map format or per method failures
jeffhaod5347e02012-03-22 17:25:05 -07003894 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot encode GC map for method with "
Ian Rogersd81871c2011-10-03 13:57:23 -07003895 << num_entries << " entries";
jeffhaobdb76512011-09-07 11:43:16 -07003896 return NULL;
3897 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003898 size_t pc_bytes;
jeffhaod1f0fde2011-09-08 17:25:33 -07003899 RegisterMapFormat format;
Ian Rogers6b0870d2011-12-15 19:38:12 -08003900 if (pc_bits <= 8) {
jeffhaod1f0fde2011-09-08 17:25:33 -07003901 format = kRegMapFormatCompact8;
Ian Rogersd81871c2011-10-03 13:57:23 -07003902 pc_bytes = 1;
Ian Rogers6b0870d2011-12-15 19:38:12 -08003903 } else if (pc_bits <= 16) {
jeffhaod1f0fde2011-09-08 17:25:33 -07003904 format = kRegMapFormatCompact16;
Ian Rogersd81871c2011-10-03 13:57:23 -07003905 pc_bytes = 2;
jeffhaoa0a764a2011-09-16 10:43:38 -07003906 } else {
Ian Rogersd81871c2011-10-03 13:57:23 -07003907 // TODO: either a better GC map format or per method failures
jeffhaod5347e02012-03-22 17:25:05 -07003908 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot encode GC map for method with "
Ian Rogersd81871c2011-10-03 13:57:23 -07003909 << (1 << pc_bits) << " instructions (number is rounded up to nearest power of 2)";
3910 return NULL;
3911 }
Elliott Hughesb25c3f62012-03-26 16:35:06 -07003912 size_t table_size = ((pc_bytes + ref_bitmap_bytes) * num_entries) + 4;
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003913 std::vector<uint8_t>* table = new std::vector<uint8_t>;
Ian Rogersd81871c2011-10-03 13:57:23 -07003914 if (table == NULL) {
jeffhaod5347e02012-03-22 17:25:05 -07003915 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Failed to encode GC map (size=" << table_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07003916 return NULL;
3917 }
Ian Rogers39ebcb82013-05-30 16:57:23 -07003918 table->reserve(table_size);
Ian Rogersd81871c2011-10-03 13:57:23 -07003919 // Write table header
Ian Rogers46c6bb22012-09-18 13:47:36 -07003920 table->push_back(format | ((ref_bitmap_bytes >> DexPcToReferenceMap::kRegMapFormatShift) &
3921 ~DexPcToReferenceMap::kRegMapFormatMask));
jeffhao60f83e32012-02-13 17:16:30 -08003922 table->push_back(ref_bitmap_bytes & 0xFF);
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003923 table->push_back(num_entries & 0xFF);
3924 table->push_back((num_entries >> 8) & 0xFF);
Ian Rogersd81871c2011-10-03 13:57:23 -07003925 // Write table data
Ian Rogersd81871c2011-10-03 13:57:23 -07003926 for (size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003927 if (insn_flags_[i].IsCompileTimeInfoPoint()) {
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003928 table->push_back(i & 0xFF);
Ian Rogersd81871c2011-10-03 13:57:23 -07003929 if (pc_bytes == 2) {
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003930 table->push_back((i >> 8) & 0xFF);
Ian Rogersd81871c2011-10-03 13:57:23 -07003931 }
3932 RegisterLine* line = reg_table_.GetLine(i);
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003933 line->WriteReferenceBitMap(*table, ref_bitmap_bytes);
Ian Rogersd81871c2011-10-03 13:57:23 -07003934 }
3935 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003936 DCHECK_EQ(table->size(), table_size);
Ian Rogersd81871c2011-10-03 13:57:23 -07003937 return table;
3938}
jeffhaoa0a764a2011-09-16 10:43:38 -07003939
Ian Rogers776ac1f2012-04-13 23:36:36 -07003940void MethodVerifier::VerifyGcMap(const std::vector<uint8_t>& data) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003941 // Check that for every GC point there is a map entry, there aren't entries for non-GC points,
3942 // that the table data is well formed and all references are marked (or not) in the bitmap
Ian Rogers46c6bb22012-09-18 13:47:36 -07003943 DexPcToReferenceMap map(&data[0], data.size());
Ian Rogersd81871c2011-10-03 13:57:23 -07003944 size_t map_index = 0;
Elliott Hughesb25c3f62012-03-26 16:35:06 -07003945 for (size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003946 const uint8_t* reg_bitmap = map.FindBitMap(i, false);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003947 if (insn_flags_[i].IsCompileTimeInfoPoint()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003948 CHECK_LT(map_index, map.NumEntries());
Ian Rogers46c6bb22012-09-18 13:47:36 -07003949 CHECK_EQ(map.GetDexPc(map_index), i);
Ian Rogersd81871c2011-10-03 13:57:23 -07003950 CHECK_EQ(map.GetBitMap(map_index), reg_bitmap);
3951 map_index++;
3952 RegisterLine* line = reg_table_.GetLine(i);
Elliott Hughesb25c3f62012-03-26 16:35:06 -07003953 for (size_t j = 0; j < code_item_->registers_size_; j++) {
Ian Rogers84fa0742011-10-25 18:13:30 -07003954 if (line->GetRegisterType(j).IsNonZeroReferenceTypes()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003955 CHECK_LT(j / 8, map.RegWidth());
3956 CHECK_EQ((reg_bitmap[j / 8] >> (j % 8)) & 1, 1);
3957 } else if ((j / 8) < map.RegWidth()) {
3958 CHECK_EQ((reg_bitmap[j / 8] >> (j % 8)) & 1, 0);
3959 } else {
3960 // If a register doesn't contain a reference then the bitmap may be shorter than the line
3961 }
3962 }
3963 } else {
3964 CHECK(reg_bitmap == NULL);
3965 }
3966 }
3967}
jeffhaoa0a764a2011-09-16 10:43:38 -07003968
Brian Carlstrom51c24672013-07-11 16:00:56 -07003969void MethodVerifier::SetDexGcMap(MethodReference ref, const std::vector<uint8_t>& gc_map) {
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02003970 DCHECK(Runtime::Current()->IsCompiler());
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003971 {
Ian Rogers637c65b2013-05-31 11:46:00 -07003972 WriterMutexLock mu(Thread::Current(), *dex_gc_maps_lock_);
Ian Rogers0c7abda2012-09-19 13:33:42 -07003973 DexGcMapTable::iterator it = dex_gc_maps_->find(ref);
3974 if (it != dex_gc_maps_->end()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003975 delete it->second;
Ian Rogers0c7abda2012-09-19 13:33:42 -07003976 dex_gc_maps_->erase(it);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003977 }
Ian Rogers0c7abda2012-09-19 13:33:42 -07003978 dex_gc_maps_->Put(ref, &gc_map);
Brian Carlstrom73a15f42012-01-17 18:14:39 -08003979 }
Ian Rogers39ebcb82013-05-30 16:57:23 -07003980 DCHECK(GetDexGcMap(ref) != NULL);
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003981}
3982
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07003983
Brian Carlstrom51c24672013-07-11 16:00:56 -07003984void MethodVerifier::SetSafeCastMap(MethodReference ref, const MethodSafeCastSet* cast_set) {
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02003985 DCHECK(Runtime::Current()->IsCompiler());
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07003986 MutexLock mu(Thread::Current(), *safecast_map_lock_);
3987 SafeCastMap::iterator it = safecast_map_->find(ref);
3988 if (it != safecast_map_->end()) {
3989 delete it->second;
3990 safecast_map_->erase(it);
3991 }
3992
3993 safecast_map_->Put(ref, cast_set);
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02003994 DCHECK(safecast_map_->find(ref) != safecast_map_->end());
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07003995}
3996
Brian Carlstrom51c24672013-07-11 16:00:56 -07003997bool MethodVerifier::IsSafeCast(MethodReference ref, uint32_t pc) {
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02003998 DCHECK(Runtime::Current()->IsCompiler());
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07003999 MutexLock mu(Thread::Current(), *safecast_map_lock_);
4000 SafeCastMap::const_iterator it = safecast_map_->find(ref);
4001 if (it == safecast_map_->end()) {
4002 return false;
4003 }
4004
4005 // Look up the cast address in the set of safe casts
4006 MethodVerifier::MethodSafeCastSet::const_iterator cast_it = it->second->find(pc);
4007 return cast_it != it->second->end();
4008}
4009
Brian Carlstrom51c24672013-07-11 16:00:56 -07004010const std::vector<uint8_t>* MethodVerifier::GetDexGcMap(MethodReference ref) {
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004011 DCHECK(Runtime::Current()->IsCompiler());
Ian Rogers637c65b2013-05-31 11:46:00 -07004012 ReaderMutexLock mu(Thread::Current(), *dex_gc_maps_lock_);
4013 DexGcMapTable::const_iterator it = dex_gc_maps_->find(ref);
4014 if (it == dex_gc_maps_->end()) {
4015 LOG(WARNING) << "Didn't find GC map for: " << PrettyMethod(ref.dex_method_index, *ref.dex_file);
4016 return NULL;
4017 }
4018 CHECK(it->second != NULL);
4019 return it->second;
4020}
Sameer Abu Asal02c42232013-04-30 12:09:45 -07004021
Brian Carlstrom51c24672013-07-11 16:00:56 -07004022void MethodVerifier::SetDevirtMap(MethodReference ref,
Ian Rogersd0583802013-06-01 10:51:46 -07004023 const PcToConcreteMethodMap* devirt_map) {
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004024 DCHECK(Runtime::Current()->IsCompiler());
Ian Rogers637c65b2013-05-31 11:46:00 -07004025 WriterMutexLock mu(Thread::Current(), *devirt_maps_lock_);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07004026 DevirtualizationMapTable::iterator it = devirt_maps_->find(ref);
4027 if (it != devirt_maps_->end()) {
4028 delete it->second;
4029 devirt_maps_->erase(it);
4030 }
4031
4032 devirt_maps_->Put(ref, devirt_map);
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004033 DCHECK(devirt_maps_->find(ref) != devirt_maps_->end());
Sameer Abu Asal02c42232013-04-30 12:09:45 -07004034}
4035
Brian Carlstrom51c24672013-07-11 16:00:56 -07004036const MethodReference* MethodVerifier::GetDevirtMap(const MethodReference& ref,
Ian Rogerse3cd2f02013-05-24 15:32:56 -07004037 uint32_t dex_pc) {
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004038 DCHECK(Runtime::Current()->IsCompiler());
Ian Rogers637c65b2013-05-31 11:46:00 -07004039 ReaderMutexLock mu(Thread::Current(), *devirt_maps_lock_);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07004040 DevirtualizationMapTable::const_iterator it = devirt_maps_->find(ref);
4041 if (it == devirt_maps_->end()) {
4042 return NULL;
4043 }
4044
4045 // Look up the PC in the map, get the concrete method to execute and return its reference.
Ian Rogersd0583802013-06-01 10:51:46 -07004046 MethodVerifier::PcToConcreteMethodMap::const_iterator pc_to_concrete_method = it->second->find(dex_pc);
Brian Carlstromdf629502013-07-17 22:39:56 -07004047 if (pc_to_concrete_method != it->second->end()) {
Sameer Abu Asal02c42232013-04-30 12:09:45 -07004048 return &(pc_to_concrete_method->second);
4049 } else {
4050 return NULL;
4051 }
4052}
4053
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004054std::vector<int32_t> MethodVerifier::DescribeVRegs(uint32_t dex_pc) {
4055 RegisterLine* line = reg_table_.GetLine(dex_pc);
4056 std::vector<int32_t> result;
4057 for (size_t i = 0; i < line->NumRegs(); ++i) {
4058 const RegType& type = line->GetRegisterType(i);
4059 if (type.IsConstant()) {
4060 result.push_back(type.IsPreciseConstant() ? kConstant : kImpreciseConstant);
4061 result.push_back(type.ConstantValue());
4062 } else if (type.IsConstantLo()) {
4063 result.push_back(type.IsPreciseConstantLo() ? kConstant : kImpreciseConstant);
4064 result.push_back(type.ConstantValueLo());
4065 } else if (type.IsConstantHi()) {
4066 result.push_back(type.IsPreciseConstantHi() ? kConstant : kImpreciseConstant);
4067 result.push_back(type.ConstantValueHi());
4068 } else if (type.IsIntegralTypes()) {
4069 result.push_back(kIntVReg);
4070 result.push_back(0);
4071 } else if (type.IsFloat()) {
4072 result.push_back(kFloatVReg);
4073 result.push_back(0);
4074 } else if (type.IsLong()) {
4075 result.push_back(kLongLoVReg);
4076 result.push_back(0);
4077 result.push_back(kLongHiVReg);
4078 result.push_back(0);
4079 ++i;
4080 } else if (type.IsDouble()) {
4081 result.push_back(kDoubleLoVReg);
4082 result.push_back(0);
4083 result.push_back(kDoubleHiVReg);
4084 result.push_back(0);
4085 ++i;
4086 } else if (type.IsUndefined() || type.IsConflict() || type.IsHighHalf()) {
4087 result.push_back(kUndefined);
4088 result.push_back(0);
4089 } else {
Ian Rogers7b3ddd22013-02-21 15:19:52 -08004090 CHECK(type.IsNonZeroReferenceTypes());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004091 result.push_back(kReferenceVReg);
4092 result.push_back(0);
4093 }
4094 }
4095 return result;
4096}
4097
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004098bool MethodVerifier::IsCandidateForCompilation(const DexFile::CodeItem* code_item,
4099 const uint32_t access_flags) {
4100 // Don't compile class initializers, ever.
4101 if (((access_flags & kAccConstructor) != 0) && ((access_flags & kAccStatic) != 0)) {
4102 return false;
4103 }
4104
4105 const Runtime* runtime = Runtime::Current();
4106 if (runtime->IsSmallMode() && runtime->UseCompileTimeClassPath()) {
4107 // In Small mode, we only compile small methods.
4108 const uint32_t code_size = code_item->insns_size_in_code_units_;
4109 return (code_size < runtime->GetSmallModeMethodDexSizeLimit());
4110 } else {
4111 // In normal mode, we compile everything.
4112 return true;
4113 }
4114}
4115
Ian Rogers637c65b2013-05-31 11:46:00 -07004116ReaderWriterMutex* MethodVerifier::dex_gc_maps_lock_ = NULL;
Ian Rogers0c7abda2012-09-19 13:33:42 -07004117MethodVerifier::DexGcMapTable* MethodVerifier::dex_gc_maps_ = NULL;
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004118
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07004119Mutex* MethodVerifier::safecast_map_lock_ = NULL;
4120MethodVerifier::SafeCastMap* MethodVerifier::safecast_map_ = NULL;
4121
Ian Rogers637c65b2013-05-31 11:46:00 -07004122ReaderWriterMutex* MethodVerifier::devirt_maps_lock_ = NULL;
Sameer Abu Asal02c42232013-04-30 12:09:45 -07004123MethodVerifier::DevirtualizationMapTable* MethodVerifier::devirt_maps_ = NULL;
4124
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004125Mutex* MethodVerifier::rejected_classes_lock_ = NULL;
4126MethodVerifier::RejectedClassesTable* MethodVerifier::rejected_classes_ = NULL;
4127
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004128void MethodVerifier::Init() {
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004129 if (Runtime::Current()->IsCompiler()) {
4130 dex_gc_maps_lock_ = new ReaderWriterMutex("verifier GC maps lock");
4131 Thread* self = Thread::Current();
4132 {
4133 WriterMutexLock mu(self, *dex_gc_maps_lock_);
4134 dex_gc_maps_ = new MethodVerifier::DexGcMapTable;
4135 }
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004136
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004137 safecast_map_lock_ = new Mutex("verifier Cast Elision lock");
4138 {
4139 MutexLock mu(self, *safecast_map_lock_);
4140 safecast_map_ = new MethodVerifier::SafeCastMap();
4141 }
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07004142
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004143 devirt_maps_lock_ = new ReaderWriterMutex("verifier Devirtualization lock");
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07004144
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004145 {
4146 WriterMutexLock mu(self, *devirt_maps_lock_);
4147 devirt_maps_ = new MethodVerifier::DevirtualizationMapTable();
4148 }
Sameer Abu Asal02c42232013-04-30 12:09:45 -07004149
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004150 rejected_classes_lock_ = new Mutex("verifier rejected classes lock");
4151 {
4152 MutexLock mu(self, *rejected_classes_lock_);
4153 rejected_classes_ = new MethodVerifier::RejectedClassesTable;
4154 }
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004155 }
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08004156 art::verifier::RegTypeCache::Init();
Brian Carlstrome7d856b2012-01-11 18:10:55 -08004157}
4158
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004159void MethodVerifier::Shutdown() {
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004160 if (Runtime::Current()->IsCompiler()) {
4161 Thread* self = Thread::Current();
4162 {
4163 WriterMutexLock mu(self, *dex_gc_maps_lock_);
4164 STLDeleteValues(dex_gc_maps_);
4165 delete dex_gc_maps_;
4166 dex_gc_maps_ = NULL;
4167 }
4168 delete dex_gc_maps_lock_;
4169 dex_gc_maps_lock_ = NULL;
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004170
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004171 {
4172 MutexLock mu(self, *safecast_map_lock_);
4173 STLDeleteValues(safecast_map_);
4174 delete safecast_map_;
4175 safecast_map_ = NULL;
4176 }
4177 delete safecast_map_lock_;
4178 safecast_map_lock_ = NULL;
Sameer Abu Asal02c42232013-04-30 12:09:45 -07004179
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004180 {
4181 WriterMutexLock mu(self, *devirt_maps_lock_);
4182 STLDeleteValues(devirt_maps_);
4183 delete devirt_maps_;
4184 devirt_maps_ = NULL;
4185 }
4186 delete devirt_maps_lock_;
4187 devirt_maps_lock_ = NULL;
4188
4189 {
4190 MutexLock mu(self, *rejected_classes_lock_);
4191 delete rejected_classes_;
4192 rejected_classes_ = NULL;
4193 }
4194 delete rejected_classes_lock_;
4195 rejected_classes_lock_ = NULL;
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004196 }
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08004197 verifier::RegTypeCache::ShutDown();
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08004198}
jeffhaod1224c72012-02-29 13:43:08 -08004199
Brian Carlstrom51c24672013-07-11 16:00:56 -07004200void MethodVerifier::AddRejectedClass(ClassReference ref) {
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004201 DCHECK(Runtime::Current()->IsCompiler());
Ian Rogers00f7d0e2012-07-19 15:28:27 -07004202 {
Ian Rogers50b35e22012-10-04 10:09:15 -07004203 MutexLock mu(Thread::Current(), *rejected_classes_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07004204 rejected_classes_->insert(ref);
4205 }
jeffhaod1224c72012-02-29 13:43:08 -08004206 CHECK(IsClassRejected(ref));
4207}
4208
Brian Carlstrom51c24672013-07-11 16:00:56 -07004209bool MethodVerifier::IsClassRejected(ClassReference ref) {
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004210 DCHECK(Runtime::Current()->IsCompiler());
Ian Rogers50b35e22012-10-04 10:09:15 -07004211 MutexLock mu(Thread::Current(), *rejected_classes_lock_);
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004212 return (rejected_classes_->find(ref) != rejected_classes_->end());
jeffhaod1224c72012-02-29 13:43:08 -08004213}
4214
Ian Rogersd81871c2011-10-03 13:57:23 -07004215} // namespace verifier
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07004216} // namespace art