blob: f1de565046fbfd34dcd364c0f4ef395254af5731 [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 }
Brian Carlstrom93c33962013-07-26 10:37:43 -0700105 return VerifyClass(&dex_file,
106 kh.GetDexCache(),
107 klass->GetClassLoader(),
108 class_def_idx, error,
109 allow_soft_failures);
Shih-wei Liao371814f2011-10-27 16:52:10 -0700110}
111
Ian Rogers365c1022012-06-22 15:05:28 -0700112MethodVerifier::FailureKind MethodVerifier::VerifyClass(const DexFile* dex_file,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800113 mirror::DexCache* dex_cache,
114 mirror::ClassLoader* class_loader,
115 uint32_t class_def_idx,
Jeff Haoee988952013-04-16 14:23:47 -0700116 std::string& error,
117 bool allow_soft_failures) {
jeffhaof56197c2012-03-05 18:01:54 -0800118 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_idx);
119 const byte* class_data = dex_file->GetClassData(class_def);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700120 if (class_data == NULL) {
121 // empty class, probably a marker interface
jeffhaof1e6b7c2012-06-05 18:33:30 -0700122 return kNoFailure;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700123 }
jeffhaof56197c2012-03-05 18:01:54 -0800124 ClassDataItemIterator it(*dex_file, class_data);
125 while (it.HasNextStaticField() || it.HasNextInstanceField()) {
126 it.Next();
127 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700128 size_t error_count = 0;
jeffhaof1e6b7c2012-06-05 18:33:30 -0700129 bool hard_fail = false;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700130 ClassLinker* linker = Runtime::Current()->GetClassLinker();
jeffhao9b0b1882012-10-01 16:51:22 -0700131 int64_t previous_direct_method_idx = -1;
jeffhaof56197c2012-03-05 18:01:54 -0800132 while (it.HasNextDirectMethod()) {
133 uint32_t method_idx = it.GetMemberIndex();
jeffhao9b0b1882012-10-01 16:51:22 -0700134 if (method_idx == previous_direct_method_idx) {
135 // smali can create dex files with two encoded_methods sharing the same method_idx
136 // http://code.google.com/p/smali/issues/detail?id=119
137 it.Next();
138 continue;
139 }
140 previous_direct_method_idx = method_idx;
Ian Rogers08f753d2012-08-24 14:35:25 -0700141 InvokeType type = it.GetMethodInvokeType(class_def);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800142 mirror::AbstractMethod* method =
143 linker->ResolveMethod(*dex_file, method_idx, dex_cache, class_loader, NULL, type);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700144 if (method == NULL) {
145 DCHECK(Thread::Current()->IsExceptionPending());
146 // We couldn't resolve the method, but continue regardless.
147 Thread::Current()->ClearException();
148 }
Brian Carlstrom93c33962013-07-26 10:37:43 -0700149 MethodVerifier::FailureKind result = VerifyMethod(method_idx,
150 dex_file,
151 dex_cache,
152 class_loader,
153 class_def_idx,
154 it.GetMethodCodeItem(),
155 method,
156 it.GetMemberAccessFlags(),
157 allow_soft_failures);
jeffhaof1e6b7c2012-06-05 18:33:30 -0700158 if (result != kNoFailure) {
159 if (result == kHardFailure) {
160 hard_fail = true;
161 if (error_count > 0) {
162 error += "\n";
163 }
164 error = "Verifier rejected class ";
165 error += PrettyDescriptor(dex_file->GetClassDescriptor(class_def));
166 error += " due to bad method ";
167 error += PrettyMethod(method_idx, *dex_file);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700168 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700169 ++error_count;
jeffhaof56197c2012-03-05 18:01:54 -0800170 }
171 it.Next();
172 }
jeffhao9b0b1882012-10-01 16:51:22 -0700173 int64_t previous_virtual_method_idx = -1;
jeffhaof56197c2012-03-05 18:01:54 -0800174 while (it.HasNextVirtualMethod()) {
175 uint32_t method_idx = it.GetMemberIndex();
jeffhao9b0b1882012-10-01 16:51:22 -0700176 if (method_idx == previous_virtual_method_idx) {
177 // smali can create dex files with two encoded_methods sharing the same method_idx
178 // http://code.google.com/p/smali/issues/detail?id=119
179 it.Next();
180 continue;
181 }
182 previous_virtual_method_idx = method_idx;
Ian Rogers08f753d2012-08-24 14:35:25 -0700183 InvokeType type = it.GetMethodInvokeType(class_def);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800184 mirror::AbstractMethod* method =
185 linker->ResolveMethod(*dex_file, method_idx, dex_cache, class_loader, NULL, type);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700186 if (method == NULL) {
187 DCHECK(Thread::Current()->IsExceptionPending());
188 // We couldn't resolve the method, but continue regardless.
189 Thread::Current()->ClearException();
190 }
Brian Carlstrom93c33962013-07-26 10:37:43 -0700191 MethodVerifier::FailureKind result = VerifyMethod(method_idx,
192 dex_file,
193 dex_cache,
194 class_loader,
195 class_def_idx,
196 it.GetMethodCodeItem(),
197 method,
198 it.GetMemberAccessFlags(),
199 allow_soft_failures);
jeffhaof1e6b7c2012-06-05 18:33:30 -0700200 if (result != kNoFailure) {
201 if (result == kHardFailure) {
202 hard_fail = true;
203 if (error_count > 0) {
204 error += "\n";
205 }
206 error = "Verifier rejected class ";
207 error += PrettyDescriptor(dex_file->GetClassDescriptor(class_def));
208 error += " due to bad method ";
209 error += PrettyMethod(method_idx, *dex_file);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700210 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700211 ++error_count;
jeffhaof56197c2012-03-05 18:01:54 -0800212 }
213 it.Next();
214 }
jeffhaof1e6b7c2012-06-05 18:33:30 -0700215 if (error_count == 0) {
216 return kNoFailure;
217 } else {
218 return hard_fail ? kHardFailure : kSoftFailure;
219 }
jeffhaof56197c2012-03-05 18:01:54 -0800220}
221
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800222MethodVerifier::FailureKind MethodVerifier::VerifyMethod(uint32_t method_idx,
223 const DexFile* dex_file,
224 mirror::DexCache* dex_cache,
225 mirror::ClassLoader* class_loader,
226 uint32_t class_def_idx,
227 const DexFile::CodeItem* code_item,
228 mirror::AbstractMethod* method,
Jeff Haoee988952013-04-16 14:23:47 -0700229 uint32_t method_access_flags,
230 bool allow_soft_failures) {
Ian Rogersc8982582012-09-07 16:53:25 -0700231 MethodVerifier::FailureKind result = kNoFailure;
232 uint64_t start_ns = NanoTime();
233
Ian Rogersad0b3a32012-04-16 14:50:24 -0700234 MethodVerifier verifier(dex_file, dex_cache, class_loader, class_def_idx, code_item, method_idx,
Jeff Haoee988952013-04-16 14:23:47 -0700235 method, method_access_flags, true, allow_soft_failures);
jeffhaof1e6b7c2012-06-05 18:33:30 -0700236 if (verifier.Verify()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700237 // Verification completed, however failures may be pending that didn't cause the verification
238 // to hard fail.
Ian Rogerse551e952012-06-03 22:59:14 -0700239 CHECK(!verifier.have_pending_hard_failure_);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700240 if (verifier.failures_.size() != 0) {
241 verifier.DumpFailures(LOG(INFO) << "Soft verification failures in "
Elliott Hughesc073b072012-05-24 19:29:17 -0700242 << PrettyMethod(method_idx, *dex_file) << "\n");
Ian Rogersc8982582012-09-07 16:53:25 -0700243 result = kSoftFailure;
jeffhaof56197c2012-03-05 18:01:54 -0800244 }
245 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700246 // Bad method data.
247 CHECK_NE(verifier.failures_.size(), 0U);
248 CHECK(verifier.have_pending_hard_failure_);
249 verifier.DumpFailures(LOG(INFO) << "Verification error in "
Elliott Hughesc073b072012-05-24 19:29:17 -0700250 << PrettyMethod(method_idx, *dex_file) << "\n");
jeffhaof56197c2012-03-05 18:01:54 -0800251 if (gDebugVerify) {
Elliott Hughesc073b072012-05-24 19:29:17 -0700252 std::cout << "\n" << verifier.info_messages_.str();
jeffhaof56197c2012-03-05 18:01:54 -0800253 verifier.Dump(std::cout);
254 }
Ian Rogersc8982582012-09-07 16:53:25 -0700255 result = kHardFailure;
jeffhaof56197c2012-03-05 18:01:54 -0800256 }
Ian Rogersc8982582012-09-07 16:53:25 -0700257 uint64_t duration_ns = NanoTime() - start_ns;
258 if (duration_ns > MsToNs(100)) {
259 LOG(WARNING) << "Verification of " << PrettyMethod(method_idx, *dex_file)
260 << " took " << PrettyDuration(duration_ns);
261 }
262 return result;
jeffhaof56197c2012-03-05 18:01:54 -0800263}
264
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800265void MethodVerifier::VerifyMethodAndDump(std::ostream& os, uint32_t dex_method_idx,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800266 const DexFile* dex_file, mirror::DexCache* dex_cache,
267 mirror::ClassLoader* class_loader, uint32_t class_def_idx,
268 const DexFile::CodeItem* code_item,
269 mirror::AbstractMethod* method,
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800270 uint32_t method_access_flags) {
271 MethodVerifier verifier(dex_file, dex_cache, class_loader, class_def_idx, code_item,
Jeff Haoee988952013-04-16 14:23:47 -0700272 dex_method_idx, method, method_access_flags, true, true);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700273 verifier.Verify();
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800274 verifier.DumpFailures(os);
275 os << verifier.info_messages_.str();
276 verifier.Dump(os);
277}
278
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800279MethodVerifier::MethodVerifier(const DexFile* dex_file, mirror::DexCache* dex_cache,
280 mirror::ClassLoader* class_loader, uint32_t class_def_idx,
281 const DexFile::CodeItem* code_item,
282 uint32_t dex_method_idx, mirror::AbstractMethod* method,
Jeff Haoee988952013-04-16 14:23:47 -0700283 uint32_t method_access_flags, bool can_load_classes,
284 bool allow_soft_failures)
Elliott Hughes80537bb2013-01-04 16:37:26 -0800285 : reg_types_(can_load_classes),
286 work_insn_idx_(-1),
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800287 dex_method_idx_(dex_method_idx),
Ian Rogers637c65b2013-05-31 11:46:00 -0700288 mirror_method_(method),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700289 method_access_flags_(method_access_flags),
jeffhaof56197c2012-03-05 18:01:54 -0800290 dex_file_(dex_file),
291 dex_cache_(dex_cache),
292 class_loader_(class_loader),
293 class_def_idx_(class_def_idx),
294 code_item_(code_item),
Ian Rogers637c65b2013-05-31 11:46:00 -0700295 declaring_class_(NULL),
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700296 interesting_dex_pc_(-1),
297 monitor_enter_dex_pcs_(NULL),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700298 have_pending_hard_failure_(false),
jeffhaofaf459e2012-08-31 15:32:47 -0700299 have_pending_runtime_throw_failure_(false),
jeffhaof56197c2012-03-05 18:01:54 -0800300 new_instance_count_(0),
Elliott Hughes80537bb2013-01-04 16:37:26 -0800301 monitor_enter_count_(0),
Jeff Haoee988952013-04-16 14:23:47 -0700302 can_load_classes_(can_load_classes),
Sebastien Hertz4d4adb12013-07-24 16:14:19 +0200303 allow_soft_failures_(allow_soft_failures),
304 has_check_casts_(false),
305 has_virtual_or_interface_invokes_(false) {
jeffhaof56197c2012-03-05 18:01:54 -0800306}
307
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800308void MethodVerifier::FindLocksAtDexPc(mirror::AbstractMethod* m, uint32_t dex_pc,
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800309 std::vector<uint32_t>& monitor_enter_dex_pcs) {
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700310 MethodHelper mh(m);
311 MethodVerifier verifier(&mh.GetDexFile(), mh.GetDexCache(), mh.GetClassLoader(),
312 mh.GetClassDefIndex(), mh.GetCodeItem(), m->GetDexMethodIndex(),
Jeff Haoee988952013-04-16 14:23:47 -0700313 m, m->GetAccessFlags(), false, true);
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700314 verifier.interesting_dex_pc_ = dex_pc;
315 verifier.monitor_enter_dex_pcs_ = &monitor_enter_dex_pcs;
316 verifier.FindLocksAtDexPc();
317}
318
319void MethodVerifier::FindLocksAtDexPc() {
320 CHECK(monitor_enter_dex_pcs_ != NULL);
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700321 CHECK(code_item_ != NULL); // This only makes sense for methods with code.
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700322
323 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
324 // verification. In practice, the phase we want relies on data structures set up by all the
325 // earlier passes, so we just run the full method verification and bail out early when we've
326 // got what we wanted.
327 Verify();
328}
329
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200330mirror::Field* MethodVerifier::FindAccessedFieldAtDexPc(mirror::AbstractMethod* m,
331 uint32_t dex_pc) {
332 MethodHelper mh(m);
333 MethodVerifier verifier(&mh.GetDexFile(), mh.GetDexCache(), mh.GetClassLoader(),
334 mh.GetClassDefIndex(), mh.GetCodeItem(), m->GetDexMethodIndex(),
335 m, m->GetAccessFlags(), false, true);
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200336 return verifier.FindAccessedFieldAtDexPc(dex_pc);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200337}
338
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200339mirror::Field* MethodVerifier::FindAccessedFieldAtDexPc(uint32_t dex_pc) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700340 CHECK(code_item_ != NULL); // This only makes sense for methods with code.
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200341
342 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
343 // verification. In practice, the phase we want relies on data structures set up by all the
344 // earlier passes, so we just run the full method verification and bail out early when we've
345 // got what we wanted.
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200346 bool success = Verify();
347 if (!success) {
348 return NULL;
349 }
350 RegisterLine* register_line = reg_table_.GetLine(dex_pc);
351 if (register_line == NULL) {
352 return NULL;
353 }
354 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
355 return GetQuickFieldAccess(inst, register_line);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200356}
357
358mirror::AbstractMethod* MethodVerifier::FindInvokedMethodAtDexPc(mirror::AbstractMethod* m,
359 uint32_t dex_pc) {
360 MethodHelper mh(m);
361 MethodVerifier verifier(&mh.GetDexFile(), mh.GetDexCache(), mh.GetClassLoader(),
362 mh.GetClassDefIndex(), mh.GetCodeItem(), m->GetDexMethodIndex(),
363 m, m->GetAccessFlags(), false, true);
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200364 return verifier.FindInvokedMethodAtDexPc(dex_pc);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200365}
366
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200367mirror::AbstractMethod* MethodVerifier::FindInvokedMethodAtDexPc(uint32_t dex_pc) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700368 CHECK(code_item_ != NULL); // This only makes sense for methods with code.
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200369
370 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
371 // verification. In practice, the phase we want relies on data structures set up by all the
372 // earlier passes, so we just run the full method verification and bail out early when we've
373 // got what we wanted.
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200374 bool success = Verify();
375 if (!success) {
376 return NULL;
377 }
378 RegisterLine* register_line = reg_table_.GetLine(dex_pc);
379 if (register_line == NULL) {
380 return NULL;
381 }
382 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
383 const bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
384 return GetQuickInvokedMethod(inst, register_line, is_range);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200385}
386
Ian Rogersad0b3a32012-04-16 14:50:24 -0700387bool MethodVerifier::Verify() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700388 // If there aren't any instructions, make sure that's expected, then exit successfully.
389 if (code_item_ == NULL) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700390 if ((method_access_flags_ & (kAccNative | kAccAbstract)) == 0) {
jeffhaod5347e02012-03-22 17:25:05 -0700391 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "zero-length code in concrete non-native method";
jeffhaobdb76512011-09-07 11:43:16 -0700392 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -0700393 } else {
394 return true;
jeffhaobdb76512011-09-07 11:43:16 -0700395 }
jeffhaobdb76512011-09-07 11:43:16 -0700396 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700397 // Sanity-check the register counts. ins + locals = registers, so make sure that ins <= registers.
398 if (code_item_->ins_size_ > code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700399 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad register counts (ins=" << code_item_->ins_size_
400 << " regs=" << code_item_->registers_size_;
Ian Rogersd81871c2011-10-03 13:57:23 -0700401 return false;
jeffhaobdb76512011-09-07 11:43:16 -0700402 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700403 // Allocate and initialize an array to hold instruction data.
Ian Rogers7b3ddd22013-02-21 15:19:52 -0800404 insn_flags_.reset(new InstructionFlags[code_item_->insns_size_in_code_units_]());
Ian Rogersd81871c2011-10-03 13:57:23 -0700405 // Run through the instructions and see if the width checks out.
406 bool result = ComputeWidthsAndCountOps();
407 // Flag instructions guarded by a "try" block and check exception handlers.
408 result = result && ScanTryCatchBlocks();
409 // Perform static instruction verification.
410 result = result && VerifyInstructions();
Ian Rogersad0b3a32012-04-16 14:50:24 -0700411 // Perform code-flow analysis and return.
412 return result && VerifyCodeFlow();
jeffhaoba5ebb92011-08-25 17:24:37 -0700413}
414
Ian Rogers776ac1f2012-04-13 23:36:36 -0700415std::ostream& MethodVerifier::Fail(VerifyError error) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700416 switch (error) {
417 case VERIFY_ERROR_NO_CLASS:
418 case VERIFY_ERROR_NO_FIELD:
419 case VERIFY_ERROR_NO_METHOD:
420 case VERIFY_ERROR_ACCESS_CLASS:
421 case VERIFY_ERROR_ACCESS_FIELD:
422 case VERIFY_ERROR_ACCESS_METHOD:
Ian Rogers08f753d2012-08-24 14:35:25 -0700423 case VERIFY_ERROR_INSTANTIATION:
424 case VERIFY_ERROR_CLASS_CHANGE:
Elliott Hughes4993bbc2013-01-10 15:41:25 -0800425 if (Runtime::Current()->IsCompiler() || !can_load_classes_) {
jeffhaofaf459e2012-08-31 15:32:47 -0700426 // If we're optimistically running verification at compile time, turn NO_xxx, ACCESS_xxx,
427 // class change and instantiation errors into soft verification errors so that we re-verify
428 // at runtime. We may fail to find or to agree on access because of not yet available class
429 // loaders, or class loaders that will differ at runtime. In these cases, we don't want to
430 // affect the soundness of the code being compiled. Instead, the generated code runs "slow
431 // paths" that dynamically perform the verification and cause the behavior to be that akin
432 // to an interpreter.
433 error = VERIFY_ERROR_BAD_CLASS_SOFT;
434 } else {
435 have_pending_runtime_throw_failure_ = true;
436 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700437 break;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700438 // Indication that verification should be retried at runtime.
439 case VERIFY_ERROR_BAD_CLASS_SOFT:
Jeff Haoee988952013-04-16 14:23:47 -0700440 if (!allow_soft_failures_) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700441 have_pending_hard_failure_ = true;
442 }
443 break;
jeffhaod5347e02012-03-22 17:25:05 -0700444 // Hard verification failures at compile time will still fail at runtime, so the class is
445 // marked as rejected to prevent it from being compiled.
Ian Rogersad0b3a32012-04-16 14:50:24 -0700446 case VERIFY_ERROR_BAD_CLASS_HARD: {
447 if (Runtime::Current()->IsCompiler()) {
Brian Carlstrom51c24672013-07-11 16:00:56 -0700448 ClassReference ref(dex_file_, class_def_idx_);
jeffhaod1224c72012-02-29 13:43:08 -0800449 AddRejectedClass(ref);
jeffhaod1224c72012-02-29 13:43:08 -0800450 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700451 have_pending_hard_failure_ = true;
452 break;
Ian Rogers47a05882012-02-03 12:23:33 -0800453 }
454 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700455 failures_.push_back(error);
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800456 std::string location(StringPrintf("%s: [0x%X]", PrettyMethod(dex_method_idx_, *dex_file_).c_str(),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700457 work_insn_idx_));
458 std::ostringstream* failure_message = new std::ostringstream(location);
459 failure_messages_.push_back(failure_message);
460 return *failure_message;
461}
462
463void MethodVerifier::PrependToLastFailMessage(std::string prepend) {
464 size_t failure_num = failure_messages_.size();
465 DCHECK_NE(failure_num, 0U);
466 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
467 prepend += last_fail_message->str();
468 failure_messages_[failure_num - 1] = new std::ostringstream(prepend);
469 delete last_fail_message;
470}
471
472void MethodVerifier::AppendToLastFailMessage(std::string append) {
473 size_t failure_num = failure_messages_.size();
474 DCHECK_NE(failure_num, 0U);
475 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
476 (*last_fail_message) << append;
Ian Rogers47a05882012-02-03 12:23:33 -0800477}
478
Ian Rogers776ac1f2012-04-13 23:36:36 -0700479bool MethodVerifier::ComputeWidthsAndCountOps() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700480 const uint16_t* insns = code_item_->insns_;
481 size_t insns_size = code_item_->insns_size_in_code_units_;
482 const Instruction* inst = Instruction::At(insns);
jeffhaobdb76512011-09-07 11:43:16 -0700483 size_t new_instance_count = 0;
484 size_t monitor_enter_count = 0;
Ian Rogersd81871c2011-10-03 13:57:23 -0700485 size_t dex_pc = 0;
jeffhaobdb76512011-09-07 11:43:16 -0700486
Ian Rogersd81871c2011-10-03 13:57:23 -0700487 while (dex_pc < insns_size) {
jeffhaobdb76512011-09-07 11:43:16 -0700488 Instruction::Code opcode = inst->Opcode();
489 if (opcode == Instruction::NEW_INSTANCE) {
490 new_instance_count++;
491 } else if (opcode == Instruction::MONITOR_ENTER) {
492 monitor_enter_count++;
Sebastien Hertz4d4adb12013-07-24 16:14:19 +0200493 } else if (opcode == Instruction::CHECK_CAST) {
494 has_check_casts_ = true;
495 } else if ((inst->Opcode() == Instruction::INVOKE_VIRTUAL) ||
496 (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE) ||
497 (inst->Opcode() == Instruction::INVOKE_INTERFACE) ||
498 (inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE)) {
499 has_virtual_or_interface_invokes_ = true;
jeffhaobdb76512011-09-07 11:43:16 -0700500 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700501 size_t inst_size = inst->SizeInCodeUnits();
502 insn_flags_[dex_pc].SetLengthInCodeUnits(inst_size);
503 dex_pc += inst_size;
jeffhaobdb76512011-09-07 11:43:16 -0700504 inst = inst->Next();
505 }
506
Ian Rogersd81871c2011-10-03 13:57:23 -0700507 if (dex_pc != insns_size) {
jeffhaod5347e02012-03-22 17:25:05 -0700508 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "code did not end where expected ("
509 << dex_pc << " vs. " << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700510 return false;
511 }
512
Ian Rogersd81871c2011-10-03 13:57:23 -0700513 new_instance_count_ = new_instance_count;
514 monitor_enter_count_ = monitor_enter_count;
jeffhaobdb76512011-09-07 11:43:16 -0700515 return true;
516}
517
Ian Rogers776ac1f2012-04-13 23:36:36 -0700518bool MethodVerifier::ScanTryCatchBlocks() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700519 uint32_t tries_size = code_item_->tries_size_;
jeffhaobdb76512011-09-07 11:43:16 -0700520 if (tries_size == 0) {
521 return true;
522 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700523 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Ian Rogers0571d352011-11-03 19:51:38 -0700524 const DexFile::TryItem* tries = DexFile::GetTryItems(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -0700525
526 for (uint32_t idx = 0; idx < tries_size; idx++) {
527 const DexFile::TryItem* try_item = &tries[idx];
528 uint32_t start = try_item->start_addr_;
529 uint32_t end = start + try_item->insn_count_;
jeffhaobdb76512011-09-07 11:43:16 -0700530 if ((start >= end) || (start >= insns_size) || (end > insns_size)) {
jeffhaod5347e02012-03-22 17:25:05 -0700531 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad exception entry: startAddr=" << start
532 << " endAddr=" << end << " (size=" << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700533 return false;
534 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700535 if (!insn_flags_[start].IsOpcode()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700536 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
537 << "'try' block starts inside an instruction (" << start << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700538 return false;
539 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700540 for (uint32_t dex_pc = start; dex_pc < end;
541 dex_pc += insn_flags_[dex_pc].GetLengthInCodeUnits()) {
542 insn_flags_[dex_pc].SetInTry();
jeffhaobdb76512011-09-07 11:43:16 -0700543 }
544 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800545 // Iterate over each of the handlers to verify target addresses.
Ian Rogers0571d352011-11-03 19:51:38 -0700546 const byte* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -0700547 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700548 ClassLinker* linker = Runtime::Current()->GetClassLinker();
jeffhaobdb76512011-09-07 11:43:16 -0700549 for (uint32_t idx = 0; idx < handlers_size; idx++) {
Ian Rogers0571d352011-11-03 19:51:38 -0700550 CatchHandlerIterator iterator(handlers_ptr);
551 for (; iterator.HasNext(); iterator.Next()) {
552 uint32_t dex_pc= iterator.GetHandlerAddress();
Ian Rogersd81871c2011-10-03 13:57:23 -0700553 if (!insn_flags_[dex_pc].IsOpcode()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700554 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
555 << "exception handler starts at bad address (" << dex_pc << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700556 return false;
557 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700558 insn_flags_[dex_pc].SetBranchTarget();
Ian Rogers28ad40d2011-10-27 15:19:26 -0700559 // Ensure exception types are resolved so that they don't need resolution to be delivered,
560 // unresolved exception types will be ignored by exception delivery
Ian Rogers0571d352011-11-03 19:51:38 -0700561 if (iterator.GetHandlerTypeIndex() != DexFile::kDexNoIndex16) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800562 mirror::Class* exception_type = linker->ResolveType(*dex_file_,
563 iterator.GetHandlerTypeIndex(),
564 dex_cache_, class_loader_);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700565 if (exception_type == NULL) {
566 DCHECK(Thread::Current()->IsExceptionPending());
567 Thread::Current()->ClearException();
568 }
569 }
jeffhaobdb76512011-09-07 11:43:16 -0700570 }
Ian Rogers0571d352011-11-03 19:51:38 -0700571 handlers_ptr = iterator.EndDataPointer();
jeffhaobdb76512011-09-07 11:43:16 -0700572 }
jeffhaobdb76512011-09-07 11:43:16 -0700573 return true;
574}
575
Ian Rogers776ac1f2012-04-13 23:36:36 -0700576bool MethodVerifier::VerifyInstructions() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700577 const Instruction* inst = Instruction::At(code_item_->insns_);
jeffhaoba5ebb92011-08-25 17:24:37 -0700578
Ian Rogers0c7abda2012-09-19 13:33:42 -0700579 /* 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 -0700580 insn_flags_[0].SetBranchTarget();
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700581 insn_flags_[0].SetCompileTimeInfoPoint();
Ian Rogersd81871c2011-10-03 13:57:23 -0700582
583 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700584 for (uint32_t dex_pc = 0; dex_pc < insns_size;) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700585 if (!VerifyInstruction(inst, dex_pc)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700586 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -0700587 return false;
588 }
589 /* Flag instructions that are garbage collection points */
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700590 // All invoke points are marked as "Throw" points already.
591 // We are relying on this to also count all the invokes as interesting.
Ian Rogersb8c78592013-07-25 23:52:52 +0000592 if (inst->IsBranch() || inst->IsSwitch() || inst->IsThrow()) {
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700593 insn_flags_[dex_pc].SetCompileTimeInfoPoint();
Ian Rogersb8c78592013-07-25 23:52:52 +0000594 } else if (inst->IsReturn()) {
595 insn_flags_[dex_pc].SetCompileTimeInfoPointAndReturn();
Ian Rogersd81871c2011-10-03 13:57:23 -0700596 }
597 dex_pc += inst->SizeInCodeUnits();
598 inst = inst->Next();
599 }
600 return true;
601}
602
Ian Rogers776ac1f2012-04-13 23:36:36 -0700603bool MethodVerifier::VerifyInstruction(const Instruction* inst, uint32_t code_offset) {
Elliott Hughesadb8c672012-03-06 16:49:32 -0800604 DecodedInstruction dec_insn(inst);
Ian Rogersd81871c2011-10-03 13:57:23 -0700605 bool result = true;
606 switch (inst->GetVerifyTypeArgumentA()) {
607 case Instruction::kVerifyRegA:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800608 result = result && CheckRegisterIndex(dec_insn.vA);
Ian Rogersd81871c2011-10-03 13:57:23 -0700609 break;
610 case Instruction::kVerifyRegAWide:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800611 result = result && CheckWideRegisterIndex(dec_insn.vA);
Ian Rogersd81871c2011-10-03 13:57:23 -0700612 break;
613 }
614 switch (inst->GetVerifyTypeArgumentB()) {
615 case Instruction::kVerifyRegB:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800616 result = result && CheckRegisterIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700617 break;
618 case Instruction::kVerifyRegBField:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800619 result = result && CheckFieldIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700620 break;
621 case Instruction::kVerifyRegBMethod:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800622 result = result && CheckMethodIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700623 break;
624 case Instruction::kVerifyRegBNewInstance:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800625 result = result && CheckNewInstance(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700626 break;
627 case Instruction::kVerifyRegBString:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800628 result = result && CheckStringIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700629 break;
630 case Instruction::kVerifyRegBType:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800631 result = result && CheckTypeIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700632 break;
633 case Instruction::kVerifyRegBWide:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800634 result = result && CheckWideRegisterIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700635 break;
636 }
637 switch (inst->GetVerifyTypeArgumentC()) {
638 case Instruction::kVerifyRegC:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800639 result = result && CheckRegisterIndex(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700640 break;
641 case Instruction::kVerifyRegCField:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800642 result = result && CheckFieldIndex(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700643 break;
644 case Instruction::kVerifyRegCNewArray:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800645 result = result && CheckNewArray(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700646 break;
647 case Instruction::kVerifyRegCType:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800648 result = result && CheckTypeIndex(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700649 break;
650 case Instruction::kVerifyRegCWide:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800651 result = result && CheckWideRegisterIndex(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700652 break;
653 }
654 switch (inst->GetVerifyExtraFlags()) {
655 case Instruction::kVerifyArrayData:
656 result = result && CheckArrayData(code_offset);
657 break;
658 case Instruction::kVerifyBranchTarget:
659 result = result && CheckBranchTarget(code_offset);
660 break;
661 case Instruction::kVerifySwitchTargets:
662 result = result && CheckSwitchTargets(code_offset);
663 break;
664 case Instruction::kVerifyVarArg:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800665 result = result && CheckVarArgRegs(dec_insn.vA, dec_insn.arg);
Ian Rogersd81871c2011-10-03 13:57:23 -0700666 break;
667 case Instruction::kVerifyVarArgRange:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800668 result = result && CheckVarArgRangeRegs(dec_insn.vA, dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700669 break;
670 case Instruction::kVerifyError:
jeffhaod5347e02012-03-22 17:25:05 -0700671 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected opcode " << inst->Name();
Ian Rogersd81871c2011-10-03 13:57:23 -0700672 result = false;
673 break;
674 }
675 return result;
676}
677
Ian Rogers776ac1f2012-04-13 23:36:36 -0700678bool MethodVerifier::CheckRegisterIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700679 if (idx >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700680 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register index out of range (" << idx << " >= "
681 << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700682 return false;
683 }
684 return true;
685}
686
Ian Rogers776ac1f2012-04-13 23:36:36 -0700687bool MethodVerifier::CheckWideRegisterIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700688 if (idx + 1 >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700689 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "wide register index out of range (" << idx
690 << "+1 >= " << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700691 return false;
692 }
693 return true;
694}
695
Ian Rogers776ac1f2012-04-13 23:36:36 -0700696bool MethodVerifier::CheckFieldIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700697 if (idx >= dex_file_->GetHeader().field_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700698 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad field index " << idx << " (max "
699 << dex_file_->GetHeader().field_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700700 return false;
701 }
702 return true;
703}
704
Ian Rogers776ac1f2012-04-13 23:36:36 -0700705bool MethodVerifier::CheckMethodIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700706 if (idx >= dex_file_->GetHeader().method_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700707 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad method index " << idx << " (max "
708 << dex_file_->GetHeader().method_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700709 return false;
710 }
711 return true;
712}
713
Ian Rogers776ac1f2012-04-13 23:36:36 -0700714bool MethodVerifier::CheckNewInstance(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700715 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700716 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
717 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700718 return false;
719 }
720 // We don't need the actual class, just a pointer to the class name.
Ian Rogers0571d352011-11-03 19:51:38 -0700721 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -0700722 if (descriptor[0] != 'L') {
jeffhaod5347e02012-03-22 17:25:05 -0700723 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "can't call new-instance on type '" << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -0700724 return false;
725 }
726 return true;
727}
728
Ian Rogers776ac1f2012-04-13 23:36:36 -0700729bool MethodVerifier::CheckStringIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700730 if (idx >= dex_file_->GetHeader().string_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700731 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad string index " << idx << " (max "
732 << dex_file_->GetHeader().string_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700733 return false;
734 }
735 return true;
736}
737
Ian Rogers776ac1f2012-04-13 23:36:36 -0700738bool MethodVerifier::CheckTypeIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700739 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700740 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
741 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700742 return false;
743 }
744 return true;
745}
746
Ian Rogers776ac1f2012-04-13 23:36:36 -0700747bool MethodVerifier::CheckNewArray(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700748 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700749 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
750 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700751 return false;
752 }
753 int bracket_count = 0;
Ian Rogers0571d352011-11-03 19:51:38 -0700754 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -0700755 const char* cp = descriptor;
756 while (*cp++ == '[') {
757 bracket_count++;
758 }
759 if (bracket_count == 0) {
760 /* The given class must be an array type. */
Brian Carlstrom93c33962013-07-26 10:37:43 -0700761 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
762 << "can't new-array class '" << descriptor << "' (not an array)";
Ian Rogersd81871c2011-10-03 13:57:23 -0700763 return false;
764 } else if (bracket_count > 255) {
765 /* It is illegal to create an array of more than 255 dimensions. */
Brian Carlstrom93c33962013-07-26 10:37:43 -0700766 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
767 << "can't new-array class '" << descriptor << "' (exceeds limit)";
Ian Rogersd81871c2011-10-03 13:57:23 -0700768 return false;
769 }
770 return true;
771}
772
Ian Rogers776ac1f2012-04-13 23:36:36 -0700773bool MethodVerifier::CheckArrayData(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700774 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
775 const uint16_t* insns = code_item_->insns_ + cur_offset;
776 const uint16_t* array_data;
777 int32_t array_data_offset;
778
779 DCHECK_LT(cur_offset, insn_count);
780 /* make sure the start of the array data table is in range */
781 array_data_offset = insns[1] | (((int32_t) insns[2]) << 16);
782 if ((int32_t) cur_offset + array_data_offset < 0 ||
783 cur_offset + array_data_offset + 2 >= insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -0700784 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data start: at " << cur_offset
Brian Carlstrom93c33962013-07-26 10:37:43 -0700785 << ", data offset " << array_data_offset
786 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -0700787 return false;
788 }
789 /* offset to array data table is a relative branch-style offset */
790 array_data = insns + array_data_offset;
791 /* make sure the table is 32-bit aligned */
792 if ((((uint32_t) array_data) & 0x03) != 0) {
jeffhaod5347e02012-03-22 17:25:05 -0700793 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned array data table: at " << cur_offset
794 << ", data offset " << array_data_offset;
Ian Rogersd81871c2011-10-03 13:57:23 -0700795 return false;
796 }
797 uint32_t value_width = array_data[1];
Elliott Hughes398f64b2012-03-26 18:05:48 -0700798 uint32_t value_count = *reinterpret_cast<const uint32_t*>(&array_data[2]);
Ian Rogersd81871c2011-10-03 13:57:23 -0700799 uint32_t table_size = 4 + (value_width * value_count + 1) / 2;
800 /* make sure the end of the switch is in range */
801 if (cur_offset + array_data_offset + table_size > insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -0700802 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data end: at " << cur_offset
803 << ", data offset " << array_data_offset << ", end "
804 << cur_offset + array_data_offset + table_size
805 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -0700806 return false;
807 }
808 return true;
809}
810
Ian Rogers776ac1f2012-04-13 23:36:36 -0700811bool MethodVerifier::CheckBranchTarget(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700812 int32_t offset;
813 bool isConditional, selfOkay;
814 if (!GetBranchOffset(cur_offset, &offset, &isConditional, &selfOkay)) {
815 return false;
816 }
817 if (!selfOkay && offset == 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700818 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch offset of zero not allowed at"
819 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -0700820 return false;
821 }
Elliott Hughes81ff3182012-03-23 20:35:56 -0700822 // Check for 32-bit overflow. This isn't strictly necessary if we can depend on the runtime
823 // to have identical "wrap-around" behavior, but it's unwise to depend on that.
Ian Rogersd81871c2011-10-03 13:57:23 -0700824 if (((int64_t) cur_offset + (int64_t) offset) != (int64_t) (cur_offset + offset)) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700825 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch target overflow "
826 << reinterpret_cast<void*>(cur_offset) << " +" << offset;
Ian Rogersd81871c2011-10-03 13:57:23 -0700827 return false;
828 }
829 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
830 int32_t abs_offset = cur_offset + offset;
Brian Carlstrom93c33962013-07-26 10:37:43 -0700831 if (abs_offset < 0 ||
832 (uint32_t) abs_offset >= insn_count ||
833 !insn_flags_[abs_offset].IsOpcode()) {
jeffhaod5347e02012-03-22 17:25:05 -0700834 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid branch target " << offset << " (-> "
Elliott Hughes398f64b2012-03-26 18:05:48 -0700835 << reinterpret_cast<void*>(abs_offset) << ") at "
836 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -0700837 return false;
838 }
839 insn_flags_[abs_offset].SetBranchTarget();
840 return true;
841}
842
Ian Rogers776ac1f2012-04-13 23:36:36 -0700843bool MethodVerifier::GetBranchOffset(uint32_t cur_offset, int32_t* pOffset, bool* pConditional,
Ian Rogersd81871c2011-10-03 13:57:23 -0700844 bool* selfOkay) {
845 const uint16_t* insns = code_item_->insns_ + cur_offset;
846 *pConditional = false;
847 *selfOkay = false;
jeffhaoba5ebb92011-08-25 17:24:37 -0700848 switch (*insns & 0xff) {
849 case Instruction::GOTO:
850 *pOffset = ((int16_t) *insns) >> 8;
jeffhaoba5ebb92011-08-25 17:24:37 -0700851 break;
852 case Instruction::GOTO_32:
853 *pOffset = insns[1] | (((uint32_t) insns[2]) << 16);
jeffhaoba5ebb92011-08-25 17:24:37 -0700854 *selfOkay = true;
855 break;
856 case Instruction::GOTO_16:
857 *pOffset = (int16_t) insns[1];
jeffhaoba5ebb92011-08-25 17:24:37 -0700858 break;
859 case Instruction::IF_EQ:
860 case Instruction::IF_NE:
861 case Instruction::IF_LT:
862 case Instruction::IF_GE:
863 case Instruction::IF_GT:
864 case Instruction::IF_LE:
865 case Instruction::IF_EQZ:
866 case Instruction::IF_NEZ:
867 case Instruction::IF_LTZ:
868 case Instruction::IF_GEZ:
869 case Instruction::IF_GTZ:
870 case Instruction::IF_LEZ:
871 *pOffset = (int16_t) insns[1];
872 *pConditional = true;
jeffhaoba5ebb92011-08-25 17:24:37 -0700873 break;
874 default:
875 return false;
876 break;
877 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700878 return true;
879}
880
Ian Rogers776ac1f2012-04-13 23:36:36 -0700881bool MethodVerifier::CheckSwitchTargets(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700882 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700883 DCHECK_LT(cur_offset, insn_count);
Ian Rogersd81871c2011-10-03 13:57:23 -0700884 const uint16_t* insns = code_item_->insns_ + cur_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -0700885 /* make sure the start of the switch is in range */
Ian Rogersd81871c2011-10-03 13:57:23 -0700886 int32_t switch_offset = insns[1] | ((int32_t) insns[2]) << 16;
887 if ((int32_t) cur_offset + switch_offset < 0 || cur_offset + switch_offset + 2 >= insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -0700888 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch start: at " << cur_offset
Brian Carlstrom93c33962013-07-26 10:37:43 -0700889 << ", switch offset " << switch_offset
890 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -0700891 return false;
892 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700893 /* offset to switch table is a relative branch-style offset */
Ian Rogersd81871c2011-10-03 13:57:23 -0700894 const uint16_t* switch_insns = insns + switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -0700895 /* make sure the table is 32-bit aligned */
896 if ((((uint32_t) switch_insns) & 0x03) != 0) {
jeffhaod5347e02012-03-22 17:25:05 -0700897 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned switch table: at " << cur_offset
898 << ", switch offset " << switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -0700899 return false;
900 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700901 uint32_t switch_count = switch_insns[1];
902 int32_t keys_offset, targets_offset;
903 uint16_t expected_signature;
jeffhaoba5ebb92011-08-25 17:24:37 -0700904 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
905 /* 0=sig, 1=count, 2/3=firstKey */
906 targets_offset = 4;
907 keys_offset = -1;
908 expected_signature = Instruction::kPackedSwitchSignature;
909 } else {
910 /* 0=sig, 1=count, 2..count*2 = keys */
911 keys_offset = 2;
912 targets_offset = 2 + 2 * switch_count;
913 expected_signature = Instruction::kSparseSwitchSignature;
914 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700915 uint32_t table_size = targets_offset + switch_count * 2;
jeffhaoba5ebb92011-08-25 17:24:37 -0700916 if (switch_insns[0] != expected_signature) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700917 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
918 << StringPrintf("wrong signature for switch table (%x, wanted %x)",
919 switch_insns[0], expected_signature);
jeffhaoba5ebb92011-08-25 17:24:37 -0700920 return false;
921 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700922 /* make sure the end of the switch is in range */
923 if (cur_offset + switch_offset + table_size > (uint32_t) insn_count) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700924 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch end: at " << cur_offset
925 << ", switch offset " << switch_offset
926 << ", end " << (cur_offset + switch_offset + table_size)
jeffhaod5347e02012-03-22 17:25:05 -0700927 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -0700928 return false;
929 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700930 /* for a sparse switch, verify the keys are in ascending order */
931 if (keys_offset > 0 && switch_count > 1) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700932 int32_t last_key = switch_insns[keys_offset] | (switch_insns[keys_offset + 1] << 16);
933 for (uint32_t targ = 1; targ < switch_count; targ++) {
jeffhaoba5ebb92011-08-25 17:24:37 -0700934 int32_t key = (int32_t) switch_insns[keys_offset + targ * 2] |
935 (int32_t) (switch_insns[keys_offset + targ * 2 + 1] << 16);
936 if (key <= last_key) {
jeffhaod5347e02012-03-22 17:25:05 -0700937 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid packed switch: last key=" << last_key
938 << ", this=" << key;
jeffhaoba5ebb92011-08-25 17:24:37 -0700939 return false;
940 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700941 last_key = key;
942 }
943 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700944 /* verify each switch target */
Ian Rogersd81871c2011-10-03 13:57:23 -0700945 for (uint32_t targ = 0; targ < switch_count; targ++) {
946 int32_t offset = (int32_t) switch_insns[targets_offset + targ * 2] |
947 (int32_t) (switch_insns[targets_offset + targ * 2 + 1] << 16);
948 int32_t abs_offset = cur_offset + offset;
Brian Carlstrom93c33962013-07-26 10:37:43 -0700949 if (abs_offset < 0 ||
950 abs_offset >= (int32_t) insn_count ||
951 !insn_flags_[abs_offset].IsOpcode()) {
952 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch target " << offset
953 << " (-> " << reinterpret_cast<void*>(abs_offset) << ") at "
954 << reinterpret_cast<void*>(cur_offset)
955 << "[" << targ << "]";
jeffhaoba5ebb92011-08-25 17:24:37 -0700956 return false;
957 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700958 insn_flags_[abs_offset].SetBranchTarget();
959 }
960 return true;
961}
962
Ian Rogers776ac1f2012-04-13 23:36:36 -0700963bool MethodVerifier::CheckVarArgRegs(uint32_t vA, uint32_t arg[]) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700964 if (vA > 5) {
jeffhaod5347e02012-03-22 17:25:05 -0700965 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << vA << ") in non-range invoke)";
Ian Rogersd81871c2011-10-03 13:57:23 -0700966 return false;
967 }
968 uint16_t registers_size = code_item_->registers_size_;
969 for (uint32_t idx = 0; idx < vA; idx++) {
jeffhao457cc512012-02-02 16:55:13 -0800970 if (arg[idx] >= registers_size) {
jeffhaod5347e02012-03-22 17:25:05 -0700971 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index (" << arg[idx]
972 << ") in non-range invoke (>= " << registers_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700973 return false;
974 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700975 }
976
977 return true;
978}
979
Ian Rogers776ac1f2012-04-13 23:36:36 -0700980bool MethodVerifier::CheckVarArgRangeRegs(uint32_t vA, uint32_t vC) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700981 uint16_t registers_size = code_item_->registers_size_;
982 // vA/vC are unsigned 8-bit/16-bit quantities for /range instructions, so there's no risk of
983 // integer overflow when adding them here.
984 if (vA + vC > registers_size) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700985 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index " << vA << "+" << vC
986 << " in range invoke (> " << registers_size << ")";
jeffhaoba5ebb92011-08-25 17:24:37 -0700987 return false;
988 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700989 return true;
990}
991
Brian Carlstrom93c33962013-07-26 10:37:43 -0700992static const std::vector<uint8_t>* CreateLengthPrefixedDexGcMap(
993 const std::vector<uint8_t>& gc_map) {
Brian Carlstrom75412882012-01-18 01:26:54 -0800994 std::vector<uint8_t>* length_prefixed_gc_map = new std::vector<uint8_t>;
Ian Rogers637c65b2013-05-31 11:46:00 -0700995 length_prefixed_gc_map->reserve(gc_map.size() + 4);
Brian Carlstrom75412882012-01-18 01:26:54 -0800996 length_prefixed_gc_map->push_back((gc_map.size() & 0xff000000) >> 24);
997 length_prefixed_gc_map->push_back((gc_map.size() & 0x00ff0000) >> 16);
998 length_prefixed_gc_map->push_back((gc_map.size() & 0x0000ff00) >> 8);
999 length_prefixed_gc_map->push_back((gc_map.size() & 0x000000ff) >> 0);
1000 length_prefixed_gc_map->insert(length_prefixed_gc_map->end(),
1001 gc_map.begin(),
1002 gc_map.end());
1003 DCHECK_EQ(gc_map.size() + 4, length_prefixed_gc_map->size());
1004 DCHECK_EQ(gc_map.size(),
1005 static_cast<size_t>((length_prefixed_gc_map->at(0) << 24) |
1006 (length_prefixed_gc_map->at(1) << 16) |
1007 (length_prefixed_gc_map->at(2) << 8) |
1008 (length_prefixed_gc_map->at(3) << 0)));
1009 return length_prefixed_gc_map;
1010}
1011
Ian Rogers776ac1f2012-04-13 23:36:36 -07001012bool MethodVerifier::VerifyCodeFlow() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001013 uint16_t registers_size = code_item_->registers_size_;
1014 uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaobdb76512011-09-07 11:43:16 -07001015
Ian Rogersd81871c2011-10-03 13:57:23 -07001016 if (registers_size * insns_size > 4*1024*1024) {
buzbee4922ef92012-02-24 14:32:20 -08001017 LOG(WARNING) << "warning: method is huge (regs=" << registers_size
1018 << " insns_size=" << insns_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001019 }
1020 /* Create and initialize table holding register status */
Brian Carlstrom93c33962013-07-26 10:37:43 -07001021 reg_table_.Init(kTrackCompilerInterestPoints,
1022 insn_flags_.get(),
1023 insns_size,
1024 registers_size,
1025 this);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07001026
jeffhaobdb76512011-09-07 11:43:16 -07001027
Ian Rogersd81871c2011-10-03 13:57:23 -07001028 work_line_.reset(new RegisterLine(registers_size, this));
1029 saved_line_.reset(new RegisterLine(registers_size, this));
jeffhaobdb76512011-09-07 11:43:16 -07001030
Ian Rogersd81871c2011-10-03 13:57:23 -07001031 /* Initialize register types of method arguments. */
1032 if (!SetTypesFromSignature()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001033 DCHECK_NE(failures_.size(), 0U);
1034 std::string prepend("Bad signature in ");
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001035 prepend += PrettyMethod(dex_method_idx_, *dex_file_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001036 PrependToLastFailMessage(prepend);
Ian Rogersd81871c2011-10-03 13:57:23 -07001037 return false;
1038 }
1039 /* Perform code flow verification. */
1040 if (!CodeFlowVerifyMethod()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001041 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -07001042 return false;
jeffhaobdb76512011-09-07 11:43:16 -07001043 }
1044
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02001045 // Compute information for compiler.
1046 if (Runtime::Current()->IsCompiler()) {
1047 MethodReference ref(dex_file_, dex_method_idx_);
1048 bool compile = IsCandidateForCompilation(code_item_, method_access_flags_);
1049 if (compile) {
1050 /* Generate a register map and add it to the method. */
1051 UniquePtr<const std::vector<uint8_t> > map(GenerateGcMap());
1052 if (map.get() == NULL) {
1053 DCHECK_NE(failures_.size(), 0U);
1054 return false; // Not a real failure, but a failure to encode
1055 }
1056 if (kIsDebugBuild) {
1057 VerifyGcMap(*map);
1058 }
1059 const std::vector<uint8_t>* dex_gc_map = CreateLengthPrefixedDexGcMap(*(map.get()));
1060 verifier::MethodVerifier::SetDexGcMap(ref, *dex_gc_map);
1061 }
Logan Chiendd361c92012-04-10 23:40:37 +08001062
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02001063 if (has_check_casts_) {
1064 MethodVerifier::MethodSafeCastSet* method_to_safe_casts = GenerateSafeCastSet();
Brian Carlstrom93c33962013-07-26 10:37:43 -07001065 if (method_to_safe_casts != NULL) {
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02001066 SetSafeCastMap(ref, method_to_safe_casts);
1067 }
1068 }
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07001069
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02001070 if (has_virtual_or_interface_invokes_) {
1071 MethodVerifier::PcToConcreteMethodMap* pc_to_concrete_method = GenerateDevirtMap();
Brian Carlstrom93c33962013-07-26 10:37:43 -07001072 if (pc_to_concrete_method != NULL) {
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02001073 SetDevirtMap(ref, pc_to_concrete_method);
1074 }
1075 }
Sameer Abu Asal02c42232013-04-30 12:09:45 -07001076 }
jeffhaobdb76512011-09-07 11:43:16 -07001077 return true;
1078}
1079
Ian Rogersad0b3a32012-04-16 14:50:24 -07001080std::ostream& MethodVerifier::DumpFailures(std::ostream& os) {
1081 DCHECK_EQ(failures_.size(), failure_messages_.size());
1082 for (size_t i = 0; i < failures_.size(); ++i) {
Elliott Hughesc073b072012-05-24 19:29:17 -07001083 os << failure_messages_[i]->str() << "\n";
Ian Rogersad0b3a32012-04-16 14:50:24 -07001084 }
1085 return os;
1086}
1087
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001088extern "C" void MethodVerifierGdbDump(MethodVerifier* v)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001089 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001090 v->Dump(std::cerr);
1091}
1092
Ian Rogers776ac1f2012-04-13 23:36:36 -07001093void MethodVerifier::Dump(std::ostream& os) {
jeffhaof56197c2012-03-05 18:01:54 -08001094 if (code_item_ == NULL) {
Elliott Hughesc073b072012-05-24 19:29:17 -07001095 os << "Native method\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001096 return;
jeffhaobdb76512011-09-07 11:43:16 -07001097 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001098 {
1099 os << "Register Types:\n";
1100 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
1101 std::ostream indent_os(&indent_filter);
1102 reg_types_.Dump(indent_os);
1103 }
Ian Rogersb4903572012-10-11 11:52:56 -07001104 os << "Dumping instructions and register lines:\n";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001105 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
1106 std::ostream indent_os(&indent_filter);
Ian Rogersd81871c2011-10-03 13:57:23 -07001107 const Instruction* inst = Instruction::At(code_item_->insns_);
1108 for (size_t dex_pc = 0; dex_pc < code_item_->insns_size_in_code_units_;
1109 dex_pc += insn_flags_[dex_pc].GetLengthInCodeUnits()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001110 RegisterLine* reg_line = reg_table_.GetLine(dex_pc);
1111 if (reg_line != NULL) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001112 indent_os << reg_line->Dump() << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07001113 }
Ian Rogers7b3ddd22013-02-21 15:19:52 -08001114 indent_os << StringPrintf("0x%04zx", dex_pc) << ": " << insn_flags_[dex_pc].ToString() << " ";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001115 const bool kDumpHexOfInstruction = false;
1116 if (kDumpHexOfInstruction) {
1117 indent_os << inst->DumpHex(5) << " ";
1118 }
1119 indent_os << inst->DumpString(dex_file_) << "\n";
jeffhaoba5ebb92011-08-25 17:24:37 -07001120 inst = inst->Next();
1121 }
jeffhaobdb76512011-09-07 11:43:16 -07001122}
1123
Ian Rogersd81871c2011-10-03 13:57:23 -07001124static bool IsPrimitiveDescriptor(char descriptor) {
1125 switch (descriptor) {
jeffhaobdb76512011-09-07 11:43:16 -07001126 case 'I':
1127 case 'C':
1128 case 'S':
1129 case 'B':
1130 case 'Z':
jeffhaobdb76512011-09-07 11:43:16 -07001131 case 'F':
1132 case 'D':
1133 case 'J':
Ian Rogersd81871c2011-10-03 13:57:23 -07001134 return true;
jeffhaobdb76512011-09-07 11:43:16 -07001135 default:
1136 return false;
1137 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001138}
1139
Ian Rogers776ac1f2012-04-13 23:36:36 -07001140bool MethodVerifier::SetTypesFromSignature() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001141 RegisterLine* reg_line = reg_table_.GetLine(0);
1142 int arg_start = code_item_->registers_size_ - code_item_->ins_size_;
1143 size_t expected_args = code_item_->ins_size_; /* long/double count as two */
jeffhaobdb76512011-09-07 11:43:16 -07001144
Ian Rogersd81871c2011-10-03 13:57:23 -07001145 DCHECK_GE(arg_start, 0); /* should have been verified earlier */
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001146 // Include the "this" pointer.
Ian Rogersd81871c2011-10-03 13:57:23 -07001147 size_t cur_arg = 0;
Ian Rogersad0b3a32012-04-16 14:50:24 -07001148 if (!IsStatic()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001149 // If this is a constructor for a class other than java.lang.Object, mark the first ("this")
1150 // argument as uninitialized. This restricts field access until the superclass constructor is
1151 // called.
Ian Rogersad0b3a32012-04-16 14:50:24 -07001152 const RegType& declaring_class = GetDeclaringClass();
1153 if (IsConstructor() && !declaring_class.IsJavaLangObject()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001154 reg_line->SetRegisterType(arg_start + cur_arg,
1155 reg_types_.UninitializedThisArgument(declaring_class));
1156 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001157 reg_line->SetRegisterType(arg_start + cur_arg, declaring_class);
jeffhaobdb76512011-09-07 11:43:16 -07001158 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001159 cur_arg++;
jeffhaobdb76512011-09-07 11:43:16 -07001160 }
1161
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001162 const DexFile::ProtoId& proto_id =
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001163 dex_file_->GetMethodPrototype(dex_file_->GetMethodId(dex_method_idx_));
Ian Rogers0571d352011-11-03 19:51:38 -07001164 DexFileParameterIterator iterator(*dex_file_, proto_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07001165
1166 for (; iterator.HasNext(); iterator.Next()) {
1167 const char* descriptor = iterator.GetDescriptor();
1168 if (descriptor == NULL) {
1169 LOG(FATAL) << "Null descriptor";
1170 }
1171 if (cur_arg >= expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07001172 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1173 << " args, found more (" << descriptor << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001174 return false;
1175 }
1176 switch (descriptor[0]) {
1177 case 'L':
1178 case '[':
1179 // We assume that reference arguments are initialized. The only way it could be otherwise
1180 // (assuming the caller was verified) is if the current method is <init>, but in that case
1181 // it's effectively considered initialized the instant we reach here (in the sense that we
1182 // can return without doing anything or call virtual methods).
1183 {
Ian Rogersb4903572012-10-11 11:52:56 -07001184 const RegType& reg_type = reg_types_.FromDescriptor(class_loader_, descriptor, false);
Ian Rogers84fa0742011-10-25 18:13:30 -07001185 reg_line->SetRegisterType(arg_start + cur_arg, reg_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001186 }
1187 break;
1188 case 'Z':
1189 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Boolean());
1190 break;
1191 case 'C':
1192 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Char());
1193 break;
1194 case 'B':
1195 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Byte());
1196 break;
1197 case 'I':
1198 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Integer());
1199 break;
1200 case 'S':
1201 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Short());
1202 break;
1203 case 'F':
1204 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Float());
1205 break;
1206 case 'J':
1207 case 'D': {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001208 const RegType& lo_half = descriptor[0] == 'J' ? reg_types_.LongLo() : reg_types_.DoubleLo();
1209 const RegType& hi_half = descriptor[0] == 'J' ? reg_types_.LongHi() : reg_types_.DoubleHi();
1210 reg_line->SetRegisterTypeWide(arg_start + cur_arg, lo_half, hi_half);
Ian Rogersd81871c2011-10-03 13:57:23 -07001211 cur_arg++;
1212 break;
1213 }
1214 default:
Brian Carlstrom93c33962013-07-26 10:37:43 -07001215 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected signature type char '"
1216 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001217 return false;
1218 }
1219 cur_arg++;
1220 }
1221 if (cur_arg != expected_args) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001222 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1223 << " arguments, found " << cur_arg;
Ian Rogersd81871c2011-10-03 13:57:23 -07001224 return false;
1225 }
1226 const char* descriptor = dex_file_->GetReturnTypeDescriptor(proto_id);
1227 // Validate return type. We don't do the type lookup; just want to make sure that it has the right
1228 // format. Only major difference from the method argument format is that 'V' is supported.
1229 bool result;
1230 if (IsPrimitiveDescriptor(descriptor[0]) || descriptor[0] == 'V') {
1231 result = descriptor[1] == '\0';
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001232 } else if (descriptor[0] == '[') { // single/multi-dimensional array of object/primitive
Ian Rogersd81871c2011-10-03 13:57:23 -07001233 size_t i = 0;
1234 do {
1235 i++;
1236 } while (descriptor[i] == '['); // process leading [
1237 if (descriptor[i] == 'L') { // object array
1238 do {
1239 i++; // find closing ;
1240 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1241 result = descriptor[i] == ';';
1242 } else { // primitive array
1243 result = IsPrimitiveDescriptor(descriptor[i]) && descriptor[i + 1] == '\0';
1244 }
1245 } else if (descriptor[0] == 'L') {
1246 // could be more thorough here, but shouldn't be required
1247 size_t i = 0;
1248 do {
1249 i++;
1250 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1251 result = descriptor[i] == ';';
1252 } else {
1253 result = false;
1254 }
1255 if (!result) {
jeffhaod5347e02012-03-22 17:25:05 -07001256 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected char in return type descriptor '"
1257 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001258 }
1259 return result;
jeffhaobdb76512011-09-07 11:43:16 -07001260}
1261
Ian Rogers776ac1f2012-04-13 23:36:36 -07001262bool MethodVerifier::CodeFlowVerifyMethod() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001263 const uint16_t* insns = code_item_->insns_;
1264 const uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaoba5ebb92011-08-25 17:24:37 -07001265
jeffhaobdb76512011-09-07 11:43:16 -07001266 /* Begin by marking the first instruction as "changed". */
Ian Rogersd81871c2011-10-03 13:57:23 -07001267 insn_flags_[0].SetChanged();
1268 uint32_t start_guess = 0;
jeffhaoba5ebb92011-08-25 17:24:37 -07001269
jeffhaobdb76512011-09-07 11:43:16 -07001270 /* Continue until no instructions are marked "changed". */
1271 while (true) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001272 // Find the first marked one. Use "start_guess" as a way to find one quickly.
1273 uint32_t insn_idx = start_guess;
1274 for (; insn_idx < insns_size; insn_idx++) {
1275 if (insn_flags_[insn_idx].IsChanged())
jeffhaobdb76512011-09-07 11:43:16 -07001276 break;
1277 }
jeffhaobdb76512011-09-07 11:43:16 -07001278 if (insn_idx == insns_size) {
1279 if (start_guess != 0) {
1280 /* try again, starting from the top */
1281 start_guess = 0;
1282 continue;
1283 } else {
1284 /* all flags are clear */
1285 break;
1286 }
1287 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001288 // We carry the working set of registers from instruction to instruction. If this address can
1289 // be the target of a branch (or throw) instruction, or if we're skipping around chasing
1290 // "changed" flags, we need to load the set of registers from the table.
1291 // Because we always prefer to continue on to the next instruction, we should never have a
1292 // situation where we have a stray "changed" flag set on an instruction that isn't a branch
1293 // target.
1294 work_insn_idx_ = insn_idx;
1295 if (insn_flags_[insn_idx].IsBranchTarget()) {
1296 work_line_->CopyFromLine(reg_table_.GetLine(insn_idx));
jeffhaobdb76512011-09-07 11:43:16 -07001297 } else {
1298#ifndef NDEBUG
1299 /*
1300 * Sanity check: retrieve the stored register line (assuming
1301 * a full table) and make sure it actually matches.
1302 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001303 RegisterLine* register_line = reg_table_.GetLine(insn_idx);
1304 if (register_line != NULL) {
1305 if (work_line_->CompareLine(register_line) != 0) {
1306 Dump(std::cout);
1307 std::cout << info_messages_.str();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001308 LOG(FATAL) << "work_line diverged in " << PrettyMethod(dex_method_idx_, *dex_file_)
Elliott Hughesc073b072012-05-24 19:29:17 -07001309 << "@" << reinterpret_cast<void*>(work_insn_idx_) << "\n"
1310 << " work_line=" << *work_line_ << "\n"
Elliott Hughes398f64b2012-03-26 18:05:48 -07001311 << " expected=" << *register_line;
Ian Rogersd81871c2011-10-03 13:57:23 -07001312 }
jeffhaobdb76512011-09-07 11:43:16 -07001313 }
1314#endif
1315 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001316 if (!CodeFlowVerifyInstruction(&start_guess)) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001317 std::string prepend(PrettyMethod(dex_method_idx_, *dex_file_));
Ian Rogersad0b3a32012-04-16 14:50:24 -07001318 prepend += " failed to verify: ";
1319 PrependToLastFailMessage(prepend);
jeffhaoba5ebb92011-08-25 17:24:37 -07001320 return false;
1321 }
jeffhaobdb76512011-09-07 11:43:16 -07001322 /* Clear "changed" and mark as visited. */
Ian Rogersd81871c2011-10-03 13:57:23 -07001323 insn_flags_[insn_idx].SetVisited();
1324 insn_flags_[insn_idx].ClearChanged();
jeffhaobdb76512011-09-07 11:43:16 -07001325 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001326
Ian Rogers1c849e52012-06-28 14:00:33 -07001327 if (gDebugVerify) {
jeffhaobdb76512011-09-07 11:43:16 -07001328 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001329 * Scan for dead code. There's nothing "evil" about dead code
jeffhaobdb76512011-09-07 11:43:16 -07001330 * (besides the wasted space), but it indicates a flaw somewhere
1331 * down the line, possibly in the verifier.
1332 *
1333 * If we've substituted "always throw" instructions into the stream,
1334 * we are almost certainly going to have some dead code.
1335 */
1336 int dead_start = -1;
Ian Rogersd81871c2011-10-03 13:57:23 -07001337 uint32_t insn_idx = 0;
1338 for (; insn_idx < insns_size; insn_idx += insn_flags_[insn_idx].GetLengthInCodeUnits()) {
jeffhaobdb76512011-09-07 11:43:16 -07001339 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001340 * Switch-statement data doesn't get "visited" by scanner. It
jeffhaobdb76512011-09-07 11:43:16 -07001341 * may or may not be preceded by a padding NOP (for alignment).
1342 */
1343 if (insns[insn_idx] == Instruction::kPackedSwitchSignature ||
1344 insns[insn_idx] == Instruction::kSparseSwitchSignature ||
1345 insns[insn_idx] == Instruction::kArrayDataSignature ||
Elliott Hughes380aaa72012-07-09 14:33:15 -07001346 (insns[insn_idx] == Instruction::NOP && (insn_idx + 1 < insns_size) &&
jeffhaobdb76512011-09-07 11:43:16 -07001347 (insns[insn_idx + 1] == Instruction::kPackedSwitchSignature ||
1348 insns[insn_idx + 1] == Instruction::kSparseSwitchSignature ||
1349 insns[insn_idx + 1] == Instruction::kArrayDataSignature))) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001350 insn_flags_[insn_idx].SetVisited();
jeffhaobdb76512011-09-07 11:43:16 -07001351 }
1352
Ian Rogersd81871c2011-10-03 13:57:23 -07001353 if (!insn_flags_[insn_idx].IsVisited()) {
jeffhaobdb76512011-09-07 11:43:16 -07001354 if (dead_start < 0)
1355 dead_start = insn_idx;
1356 } else if (dead_start >= 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001357 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start)
1358 << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaobdb76512011-09-07 11:43:16 -07001359 dead_start = -1;
1360 }
1361 }
1362 if (dead_start >= 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001363 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start)
1364 << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaoba5ebb92011-08-25 17:24:37 -07001365 }
Ian Rogersc9e463c2013-06-05 16:52:26 -07001366 // To dump the state of the verify after a method, do something like:
1367 // if (PrettyMethod(dex_method_idx_, *dex_file_) ==
1368 // "boolean java.lang.String.equals(java.lang.Object)") {
1369 // LOG(INFO) << info_messages_.str();
1370 // }
jeffhaoba5ebb92011-08-25 17:24:37 -07001371 }
jeffhaobdb76512011-09-07 11:43:16 -07001372 return true;
1373}
1374
Ian Rogers776ac1f2012-04-13 23:36:36 -07001375bool MethodVerifier::CodeFlowVerifyInstruction(uint32_t* start_guess) {
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001376 // If we're doing FindLocksAtDexPc, check whether we're at the dex pc we care about.
1377 // We want the state _before_ the instruction, for the case where the dex pc we're
1378 // interested in is itself a monitor-enter instruction (which is a likely place
1379 // for a thread to be suspended).
1380 if (monitor_enter_dex_pcs_ != NULL && work_insn_idx_ == interesting_dex_pc_) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001381 monitor_enter_dex_pcs_->clear(); // The new work line is more accurate than the previous one.
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001382 for (size_t i = 0; i < work_line_->GetMonitorEnterCount(); ++i) {
1383 monitor_enter_dex_pcs_->push_back(work_line_->GetMonitorEnterDexPc(i));
1384 }
1385 }
1386
jeffhaobdb76512011-09-07 11:43:16 -07001387 /*
1388 * Once we finish decoding the instruction, we need to figure out where
jeffhaod1f0fde2011-09-08 17:25:33 -07001389 * we can go from here. There are three possible ways to transfer
jeffhaobdb76512011-09-07 11:43:16 -07001390 * control to another statement:
1391 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001392 * (1) Continue to the next instruction. Applies to all but
jeffhaobdb76512011-09-07 11:43:16 -07001393 * unconditional branches, method returns, and exception throws.
jeffhaod1f0fde2011-09-08 17:25:33 -07001394 * (2) Branch to one or more possible locations. Applies to branches
jeffhaobdb76512011-09-07 11:43:16 -07001395 * and switch statements.
jeffhaod1f0fde2011-09-08 17:25:33 -07001396 * (3) Exception handlers. Applies to any instruction that can
jeffhaobdb76512011-09-07 11:43:16 -07001397 * throw an exception that is handled by an encompassing "try"
1398 * block.
1399 *
1400 * We can also return, in which case there is no successor instruction
1401 * from this point.
1402 *
Elliott Hughesadb8c672012-03-06 16:49:32 -08001403 * The behavior can be determined from the opcode flags.
jeffhaobdb76512011-09-07 11:43:16 -07001404 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001405 const uint16_t* insns = code_item_->insns_ + work_insn_idx_;
1406 const Instruction* inst = Instruction::At(insns);
Ian Rogersa75a0132012-09-28 11:41:42 -07001407 int opcode_flags = Instruction::FlagsOf(inst->Opcode());
jeffhaobdb76512011-09-07 11:43:16 -07001408
jeffhaobdb76512011-09-07 11:43:16 -07001409 int32_t branch_target = 0;
jeffhaobdb76512011-09-07 11:43:16 -07001410 bool just_set_result = false;
Ian Rogers2c8a8572011-10-24 17:11:36 -07001411 if (gDebugVerify) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001412 // Generate processing back trace to debug verifier
Elliott Hughesc073b072012-05-24 19:29:17 -07001413 LogVerifyInfo() << "Processing " << inst->DumpString(dex_file_) << "\n"
1414 << *work_line_.get() << "\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001415 }
jeffhaobdb76512011-09-07 11:43:16 -07001416
1417 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001418 * Make a copy of the previous register state. If the instruction
jeffhaobdb76512011-09-07 11:43:16 -07001419 * can throw an exception, we will copy/merge this into the "catch"
1420 * address rather than work_line, because we don't want the result
1421 * from the "successful" code path (e.g. a check-cast that "improves"
1422 * a type) to be visible to the exception handler.
1423 */
Ian Rogers776ac1f2012-04-13 23:36:36 -07001424 if ((opcode_flags & Instruction::kThrow) != 0 && CurrentInsnFlags()->IsInTry()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001425 saved_line_->CopyFromLine(work_line_.get());
jeffhaobdb76512011-09-07 11:43:16 -07001426 } else {
1427#ifndef NDEBUG
Ian Rogersd81871c2011-10-03 13:57:23 -07001428 saved_line_->FillWithGarbage();
jeffhaobdb76512011-09-07 11:43:16 -07001429#endif
1430 }
1431
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07001432
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001433 // We need to ensure the work line is consistent while performing validation. When we spot a
1434 // peephole pattern we compute a new line for either the fallthrough instruction or the
1435 // branch target.
1436 UniquePtr<RegisterLine> branch_line;
1437 UniquePtr<RegisterLine> fallthrough_line;
1438
Sebastien Hertz5243e912013-05-21 10:55:07 +02001439 switch (inst->Opcode()) {
jeffhaobdb76512011-09-07 11:43:16 -07001440 case Instruction::NOP:
1441 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001442 * A "pure" NOP has no effect on anything. Data tables start with
jeffhaobdb76512011-09-07 11:43:16 -07001443 * a signature that looks like a NOP; if we see one of these in
1444 * the course of executing code then we have a problem.
1445 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001446 if (inst->VRegA_10x() != 0) {
jeffhaod5347e02012-03-22 17:25:05 -07001447 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "encountered data table in instruction stream";
jeffhaobdb76512011-09-07 11:43:16 -07001448 }
1449 break;
1450
1451 case Instruction::MOVE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001452 work_line_->CopyRegister1(inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategory1nr);
1453 break;
jeffhaobdb76512011-09-07 11:43:16 -07001454 case Instruction::MOVE_FROM16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001455 work_line_->CopyRegister1(inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategory1nr);
1456 break;
jeffhaobdb76512011-09-07 11:43:16 -07001457 case Instruction::MOVE_16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001458 work_line_->CopyRegister1(inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategory1nr);
jeffhaobdb76512011-09-07 11:43:16 -07001459 break;
1460 case Instruction::MOVE_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001461 work_line_->CopyRegister2(inst->VRegA_12x(), inst->VRegB_12x());
1462 break;
jeffhaobdb76512011-09-07 11:43:16 -07001463 case Instruction::MOVE_WIDE_FROM16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001464 work_line_->CopyRegister2(inst->VRegA_22x(), inst->VRegB_22x());
1465 break;
jeffhaobdb76512011-09-07 11:43:16 -07001466 case Instruction::MOVE_WIDE_16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001467 work_line_->CopyRegister2(inst->VRegA_32x(), inst->VRegB_32x());
jeffhaobdb76512011-09-07 11:43:16 -07001468 break;
1469 case Instruction::MOVE_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001470 work_line_->CopyRegister1(inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategoryRef);
1471 break;
jeffhaobdb76512011-09-07 11:43:16 -07001472 case Instruction::MOVE_OBJECT_FROM16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001473 work_line_->CopyRegister1(inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategoryRef);
1474 break;
jeffhaobdb76512011-09-07 11:43:16 -07001475 case Instruction::MOVE_OBJECT_16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001476 work_line_->CopyRegister1(inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategoryRef);
jeffhaobdb76512011-09-07 11:43:16 -07001477 break;
1478
1479 /*
1480 * The move-result instructions copy data out of a "pseudo-register"
jeffhaod1f0fde2011-09-08 17:25:33 -07001481 * with the results from the last method invocation. In practice we
jeffhaobdb76512011-09-07 11:43:16 -07001482 * might want to hold the result in an actual CPU register, so the
1483 * Dalvik spec requires that these only appear immediately after an
1484 * invoke or filled-new-array.
1485 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001486 * These calls invalidate the "result" register. (This is now
jeffhaobdb76512011-09-07 11:43:16 -07001487 * redundant with the reset done below, but it can make the debug info
1488 * easier to read in some cases.)
1489 */
1490 case Instruction::MOVE_RESULT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001491 work_line_->CopyResultRegister1(inst->VRegA_11x(), false);
jeffhaobdb76512011-09-07 11:43:16 -07001492 break;
1493 case Instruction::MOVE_RESULT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001494 work_line_->CopyResultRegister2(inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07001495 break;
1496 case Instruction::MOVE_RESULT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001497 work_line_->CopyResultRegister1(inst->VRegA_11x(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001498 break;
1499
Ian Rogersd81871c2011-10-03 13:57:23 -07001500 case Instruction::MOVE_EXCEPTION: {
jeffhaobdb76512011-09-07 11:43:16 -07001501 /*
jeffhao60f83e32012-02-13 17:16:30 -08001502 * This statement can only appear as the first instruction in an exception handler. We verify
1503 * that as part of extracting the exception type from the catch block list.
jeffhaobdb76512011-09-07 11:43:16 -07001504 */
Ian Rogers28ad40d2011-10-27 15:19:26 -07001505 const RegType& res_type = GetCaughtExceptionType();
Sebastien Hertz5243e912013-05-21 10:55:07 +02001506 work_line_->SetRegisterType(inst->VRegA_11x(), res_type);
jeffhaobdb76512011-09-07 11:43:16 -07001507 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001508 }
jeffhaobdb76512011-09-07 11:43:16 -07001509 case Instruction::RETURN_VOID:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001510 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
1511 if (!GetMethodReturnType().IsConflict()) {
jeffhaod5347e02012-03-22 17:25:05 -07001512 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001513 }
jeffhaobdb76512011-09-07 11:43:16 -07001514 }
1515 break;
1516 case Instruction::RETURN:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001517 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
jeffhaobdb76512011-09-07 11:43:16 -07001518 /* check the method signature */
Ian Rogersd81871c2011-10-03 13:57:23 -07001519 const RegType& return_type = GetMethodReturnType();
1520 if (!return_type.IsCategory1Types()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001521 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected non-category 1 return type "
1522 << return_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001523 } else {
1524 // Compilers may generate synthetic functions that write byte values into boolean fields.
1525 // Also, it may use integer values for boolean, byte, short, and character return types.
Sebastien Hertz5243e912013-05-21 10:55:07 +02001526 const uint32_t vregA = inst->VRegA_11x();
1527 const RegType& src_type = work_line_->GetRegisterType(vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07001528 bool use_src = ((return_type.IsBoolean() && src_type.IsByte()) ||
1529 ((return_type.IsBoolean() || return_type.IsByte() ||
1530 return_type.IsShort() || return_type.IsChar()) &&
1531 src_type.IsInteger()));
1532 /* check the register contents */
Ian Rogersad0b3a32012-04-16 14:50:24 -07001533 bool success =
Sebastien Hertz5243e912013-05-21 10:55:07 +02001534 work_line_->VerifyRegisterType(vregA, use_src ? src_type : return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001535 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001536 AppendToLastFailMessage(StringPrintf(" return-1nr on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07001537 }
jeffhaobdb76512011-09-07 11:43:16 -07001538 }
1539 }
1540 break;
1541 case Instruction::RETURN_WIDE:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001542 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
jeffhaobdb76512011-09-07 11:43:16 -07001543 /* check the method signature */
Ian Rogersd81871c2011-10-03 13:57:23 -07001544 const RegType& return_type = GetMethodReturnType();
1545 if (!return_type.IsCategory2Types()) {
jeffhaod5347e02012-03-22 17:25:05 -07001546 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-wide not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001547 } else {
1548 /* check the register contents */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001549 const uint32_t vregA = inst->VRegA_11x();
1550 bool success = work_line_->VerifyRegisterType(vregA, return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001551 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001552 AppendToLastFailMessage(StringPrintf(" return-wide on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07001553 }
jeffhaobdb76512011-09-07 11:43:16 -07001554 }
1555 }
1556 break;
1557 case Instruction::RETURN_OBJECT:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001558 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001559 const RegType& return_type = GetMethodReturnType();
1560 if (!return_type.IsReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001561 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-object not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001562 } else {
1563 /* return_type is the *expected* return type, not register value */
1564 DCHECK(!return_type.IsZero());
1565 DCHECK(!return_type.IsUninitializedReference());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001566 const uint32_t vregA = inst->VRegA_11x();
1567 const RegType& reg_type = work_line_->GetRegisterType(vregA);
Ian Rogers9074b992011-10-26 17:41:55 -07001568 // Disallow returning uninitialized values and verify that the reference in vAA is an
1569 // instance of the "return_type"
1570 if (reg_type.IsUninitializedTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001571 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "returning uninitialized object '"
1572 << reg_type << "'";
Ian Rogers9074b992011-10-26 17:41:55 -07001573 } else if (!return_type.IsAssignableFrom(reg_type)) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001574 Fail(reg_type.IsUnresolvedTypes() ?
1575 VERIFY_ERROR_BAD_CLASS_SOFT :
1576 VERIFY_ERROR_BAD_CLASS_HARD)
1577 << "returning '" << reg_type << "', but expected from declaration '"
1578 << return_type << "'";
jeffhaobdb76512011-09-07 11:43:16 -07001579 }
1580 }
1581 }
1582 break;
1583
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001584 /* could be boolean, int, float, or a null reference */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001585 case Instruction::CONST_4: {
1586 int32_t val = static_cast<int32_t>(inst->VRegB_11n() << 28) >> 28;
1587 work_line_->SetRegisterType(inst->VRegA_11n(), reg_types_.FromCat1Const(val, true));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001588 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02001589 }
1590 case Instruction::CONST_16: {
1591 int16_t val = static_cast<int16_t>(inst->VRegB_21s());
1592 work_line_->SetRegisterType(inst->VRegA_21s(), reg_types_.FromCat1Const(val, true));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001593 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02001594 }
jeffhaobdb76512011-09-07 11:43:16 -07001595 case Instruction::CONST:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001596 work_line_->SetRegisterType(inst->VRegA_31i(),
1597 reg_types_.FromCat1Const(inst->VRegB_31i(), true));
jeffhaobdb76512011-09-07 11:43:16 -07001598 break;
1599 case Instruction::CONST_HIGH16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001600 work_line_->SetRegisterType(inst->VRegA_21h(),
1601 reg_types_.FromCat1Const(inst->VRegB_21h() << 16, true));
jeffhaobdb76512011-09-07 11:43:16 -07001602 break;
jeffhaobdb76512011-09-07 11:43:16 -07001603 /* could be long or double; resolved upon use */
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001604 case Instruction::CONST_WIDE_16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001605 int64_t val = static_cast<int16_t>(inst->VRegB_21s());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001606 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1607 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001608 work_line_->SetRegisterTypeWide(inst->VRegA_21s(), lo, hi);
jeffhaobdb76512011-09-07 11:43:16 -07001609 break;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001610 }
1611 case Instruction::CONST_WIDE_32: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001612 int64_t val = static_cast<int32_t>(inst->VRegB_31i());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001613 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1614 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001615 work_line_->SetRegisterTypeWide(inst->VRegA_31i(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001616 break;
1617 }
1618 case Instruction::CONST_WIDE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001619 int64_t val = inst->VRegB_51l();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001620 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1621 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001622 work_line_->SetRegisterTypeWide(inst->VRegA_51l(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001623 break;
1624 }
1625 case Instruction::CONST_WIDE_HIGH16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001626 int64_t val = static_cast<uint64_t>(inst->VRegB_21h()) << 48;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001627 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1628 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001629 work_line_->SetRegisterTypeWide(inst->VRegA_21h(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001630 break;
1631 }
jeffhaobdb76512011-09-07 11:43:16 -07001632 case Instruction::CONST_STRING:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001633 work_line_->SetRegisterType(inst->VRegA_21c(), reg_types_.JavaLangString());
1634 break;
jeffhaobdb76512011-09-07 11:43:16 -07001635 case Instruction::CONST_STRING_JUMBO:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001636 work_line_->SetRegisterType(inst->VRegA_31c(), reg_types_.JavaLangString());
jeffhaobdb76512011-09-07 11:43:16 -07001637 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001638 case Instruction::CONST_CLASS: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001639 // Get type from instruction if unresolved then we need an access check
1640 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Sebastien Hertz5243e912013-05-21 10:55:07 +02001641 const RegType& res_type = ResolveClassAndCheckAccess(inst->VRegB_21c());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001642 // Register holds class, ie its type is class, on error it will hold Conflict.
Sebastien Hertz5243e912013-05-21 10:55:07 +02001643 work_line_->SetRegisterType(inst->VRegA_21c(),
Ian Rogersb4903572012-10-11 11:52:56 -07001644 res_type.IsConflict() ? res_type
1645 : reg_types_.JavaLangClass(true));
jeffhaobdb76512011-09-07 11:43:16 -07001646 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001647 }
jeffhaobdb76512011-09-07 11:43:16 -07001648 case Instruction::MONITOR_ENTER:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001649 work_line_->PushMonitor(inst->VRegA_11x(), work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07001650 break;
1651 case Instruction::MONITOR_EXIT:
1652 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001653 * monitor-exit instructions are odd. They can throw exceptions,
jeffhaobdb76512011-09-07 11:43:16 -07001654 * but when they do they act as if they succeeded and the PC is
jeffhaod1f0fde2011-09-08 17:25:33 -07001655 * pointing to the following instruction. (This behavior goes back
jeffhaobdb76512011-09-07 11:43:16 -07001656 * to the need to handle asynchronous exceptions, a now-deprecated
1657 * feature that Dalvik doesn't support.)
1658 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001659 * In practice we don't need to worry about this. The only
jeffhaobdb76512011-09-07 11:43:16 -07001660 * exceptions that can be thrown from monitor-exit are for a
jeffhaod1f0fde2011-09-08 17:25:33 -07001661 * null reference and -exit without a matching -enter. If the
jeffhaobdb76512011-09-07 11:43:16 -07001662 * structured locking checks are working, the former would have
1663 * failed on the -enter instruction, and the latter is impossible.
1664 *
1665 * This is fortunate, because issue 3221411 prevents us from
1666 * chasing the "can throw" path when monitor verification is
jeffhaod1f0fde2011-09-08 17:25:33 -07001667 * enabled. If we can fully verify the locking we can ignore
jeffhaobdb76512011-09-07 11:43:16 -07001668 * some catch blocks (which will show up as "dead" code when
1669 * we skip them here); if we can't, then the code path could be
1670 * "live" so we still need to check it.
1671 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001672 opcode_flags &= ~Instruction::kThrow;
Sebastien Hertz5243e912013-05-21 10:55:07 +02001673 work_line_->PopMonitor(inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07001674 break;
1675
Ian Rogers28ad40d2011-10-27 15:19:26 -07001676 case Instruction::CHECK_CAST:
Ian Rogersd81871c2011-10-03 13:57:23 -07001677 case Instruction::INSTANCE_OF: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001678 /*
1679 * If this instruction succeeds, we will "downcast" register vA to the type in vB. (This
1680 * could be a "upcast" -- not expected, so we don't try to address it.)
1681 *
1682 * If it fails, an exception is thrown, which we deal with later by ignoring the update to
Elliott Hughesadb8c672012-03-06 16:49:32 -08001683 * dec_insn.vA when branching to a handler.
Ian Rogers28ad40d2011-10-27 15:19:26 -07001684 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001685 const bool is_checkcast = (inst->Opcode() == Instruction::CHECK_CAST);
1686 const uint32_t type_idx = (is_checkcast) ? inst->VRegB_21c() : inst->VRegC_22c();
1687 const RegType& res_type = ResolveClassAndCheckAccess(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001688 if (res_type.IsConflict()) {
1689 DCHECK_NE(failures_.size(), 0U);
1690 if (!is_checkcast) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001691 work_line_->SetRegisterType(inst->VRegA_22c(), reg_types_.Boolean());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001692 }
1693 break; // bad class
Ian Rogers9f1ab122011-12-12 08:52:43 -08001694 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001695 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Sebastien Hertz5243e912013-05-21 10:55:07 +02001696 uint32_t orig_type_reg = (is_checkcast) ? inst->VRegA_21c() : inst->VRegB_22c();
1697 const RegType& orig_type = work_line_->GetRegisterType(orig_type_reg);
Ian Rogers28ad40d2011-10-27 15:19:26 -07001698 if (!res_type.IsNonZeroReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001699 if (is_checkcast) {
1700 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on unexpected class " << res_type;
1701 } else {
1702 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on unexpected class " << res_type;
1703 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001704 } else if (!orig_type.IsReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001705 if (is_checkcast) {
1706 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on non-reference in v" << orig_type_reg;
1707 } else {
1708 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on non-reference in v" << orig_type_reg;
1709 }
jeffhao2a8a90e2011-09-26 14:25:31 -07001710 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001711 if (is_checkcast) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001712 work_line_->SetRegisterType(inst->VRegA_21c(), res_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001713 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001714 work_line_->SetRegisterType(inst->VRegA_22c(), reg_types_.Boolean());
jeffhaobdb76512011-09-07 11:43:16 -07001715 }
jeffhaobdb76512011-09-07 11:43:16 -07001716 }
jeffhao2a8a90e2011-09-26 14:25:31 -07001717 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001718 }
1719 case Instruction::ARRAY_LENGTH: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001720 const RegType& res_type = work_line_->GetRegisterType(inst->VRegB_12x());
Ian Rogers28ad40d2011-10-27 15:19:26 -07001721 if (res_type.IsReferenceTypes()) {
Ian Rogers89310de2012-02-01 13:47:30 -08001722 if (!res_type.IsArrayTypes() && !res_type.IsZero()) { // ie not an array or null
jeffhaod5347e02012-03-22 17:25:05 -07001723 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001724 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001725 work_line_->SetRegisterType(inst->VRegA_12x(), reg_types_.Integer());
Ian Rogersd81871c2011-10-03 13:57:23 -07001726 }
1727 }
1728 break;
1729 }
1730 case Instruction::NEW_INSTANCE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001731 const RegType& res_type = ResolveClassAndCheckAccess(inst->VRegB_21c());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001732 if (res_type.IsConflict()) {
1733 DCHECK_NE(failures_.size(), 0U);
1734 break; // bad class
jeffhao8cd6dda2012-02-22 10:15:34 -08001735 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001736 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
1737 // can't create an instance of an interface or abstract class */
1738 if (!res_type.IsInstantiableTypes()) {
1739 Fail(VERIFY_ERROR_INSTANTIATION)
1740 << "new-instance on primitive, interface or abstract class" << res_type;
Ian Rogers08f753d2012-08-24 14:35:25 -07001741 // Soft failure so carry on to set register type.
Ian Rogersd81871c2011-10-03 13:57:23 -07001742 }
Ian Rogers08f753d2012-08-24 14:35:25 -07001743 const RegType& uninit_type = reg_types_.Uninitialized(res_type, work_insn_idx_);
1744 // Any registers holding previous allocations from this address that have not yet been
1745 // initialized must be marked invalid.
1746 work_line_->MarkUninitRefsAsInvalid(uninit_type);
1747 // add the new uninitialized reference to the register state
Sebastien Hertz5243e912013-05-21 10:55:07 +02001748 work_line_->SetRegisterType(inst->VRegA_21c(), uninit_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001749 break;
1750 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08001751 case Instruction::NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001752 VerifyNewArray(inst, false, false);
jeffhaobdb76512011-09-07 11:43:16 -07001753 break;
1754 case Instruction::FILLED_NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001755 VerifyNewArray(inst, true, false);
Ian Rogers0c4a5062012-02-03 15:18:59 -08001756 just_set_result = true; // Filled new array sets result register
jeffhaobdb76512011-09-07 11:43:16 -07001757 break;
Ian Rogers0c4a5062012-02-03 15:18:59 -08001758 case Instruction::FILLED_NEW_ARRAY_RANGE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001759 VerifyNewArray(inst, true, true);
Ian Rogers0c4a5062012-02-03 15:18:59 -08001760 just_set_result = true; // Filled new array range sets result register
1761 break;
jeffhaobdb76512011-09-07 11:43:16 -07001762 case Instruction::CMPL_FLOAT:
1763 case Instruction::CMPG_FLOAT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001764 if (!work_line_->VerifyRegisterType(inst->VRegB_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08001765 break;
1766 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02001767 if (!work_line_->VerifyRegisterType(inst->VRegC_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08001768 break;
1769 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02001770 work_line_->SetRegisterType(inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001771 break;
1772 case Instruction::CMPL_DOUBLE:
1773 case Instruction::CMPG_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001774 if (!work_line_->VerifyRegisterTypeWide(inst->VRegB_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001775 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001776 break;
1777 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02001778 if (!work_line_->VerifyRegisterTypeWide(inst->VRegC_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001779 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001780 break;
1781 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02001782 work_line_->SetRegisterType(inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001783 break;
1784 case Instruction::CMP_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001785 if (!work_line_->VerifyRegisterTypeWide(inst->VRegB_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001786 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001787 break;
1788 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02001789 if (!work_line_->VerifyRegisterTypeWide(inst->VRegC_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001790 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001791 break;
1792 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02001793 work_line_->SetRegisterType(inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001794 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001795 case Instruction::THROW: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001796 const RegType& res_type = work_line_->GetRegisterType(inst->VRegA_11x());
Ian Rogersb4903572012-10-11 11:52:56 -07001797 if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(res_type)) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001798 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "thrown class " << res_type
1799 << " not instanceof Throwable";
jeffhaobdb76512011-09-07 11:43:16 -07001800 }
1801 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001802 }
jeffhaobdb76512011-09-07 11:43:16 -07001803 case Instruction::GOTO:
1804 case Instruction::GOTO_16:
1805 case Instruction::GOTO_32:
1806 /* no effect on or use of registers */
1807 break;
1808
1809 case Instruction::PACKED_SWITCH:
1810 case Instruction::SPARSE_SWITCH:
1811 /* verify that vAA is an integer, or can be converted to one */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001812 work_line_->VerifyRegisterType(inst->VRegA_31t(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001813 break;
1814
Ian Rogersd81871c2011-10-03 13:57:23 -07001815 case Instruction::FILL_ARRAY_DATA: {
1816 /* Similar to the verification done for APUT */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001817 const RegType& array_type = work_line_->GetRegisterType(inst->VRegA_31t());
Ian Rogers89310de2012-02-01 13:47:30 -08001818 /* array_type can be null if the reg type is Zero */
1819 if (!array_type.IsZero()) {
jeffhao457cc512012-02-02 16:55:13 -08001820 if (!array_type.IsArrayTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001821 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with array type "
1822 << array_type;
Ian Rogers89310de2012-02-01 13:47:30 -08001823 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001824 const RegType& component_type = reg_types_.GetComponentType(array_type, class_loader_);
1825 DCHECK(!component_type.IsConflict());
jeffhao457cc512012-02-02 16:55:13 -08001826 if (component_type.IsNonZeroReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001827 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with component type "
1828 << component_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001829 } else {
jeffhao457cc512012-02-02 16:55:13 -08001830 // Now verify if the element width in the table matches the element width declared in
1831 // the array
1832 const uint16_t* array_data = insns + (insns[1] | (((int32_t) insns[2]) << 16));
1833 if (array_data[0] != Instruction::kArrayDataSignature) {
jeffhaod5347e02012-03-22 17:25:05 -07001834 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid magic for array-data";
jeffhao457cc512012-02-02 16:55:13 -08001835 } else {
1836 size_t elem_width = Primitive::ComponentSize(component_type.GetPrimitiveType());
1837 // Since we don't compress the data in Dex, expect to see equal width of data stored
1838 // in the table and expected from the array class.
1839 if (array_data[1] != elem_width) {
jeffhaod5347e02012-03-22 17:25:05 -07001840 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-data size mismatch (" << array_data[1]
1841 << " vs " << elem_width << ")";
jeffhao457cc512012-02-02 16:55:13 -08001842 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001843 }
1844 }
jeffhaobdb76512011-09-07 11:43:16 -07001845 }
1846 }
1847 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001848 }
jeffhaobdb76512011-09-07 11:43:16 -07001849 case Instruction::IF_EQ:
Ian Rogersd81871c2011-10-03 13:57:23 -07001850 case Instruction::IF_NE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001851 const RegType& reg_type1 = work_line_->GetRegisterType(inst->VRegA_22t());
1852 const RegType& reg_type2 = work_line_->GetRegisterType(inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07001853 bool mismatch = false;
1854 if (reg_type1.IsZero()) { // zero then integral or reference expected
1855 mismatch = !reg_type2.IsReferenceTypes() && !reg_type2.IsIntegralTypes();
1856 } else if (reg_type1.IsReferenceTypes()) { // both references?
1857 mismatch = !reg_type2.IsReferenceTypes();
1858 } else { // both integral?
1859 mismatch = !reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes();
1860 }
1861 if (mismatch) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001862 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to if-eq/if-ne (" << reg_type1 << ","
1863 << reg_type2 << ") must both be references or integral";
jeffhaobdb76512011-09-07 11:43:16 -07001864 }
1865 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001866 }
jeffhaobdb76512011-09-07 11:43:16 -07001867 case Instruction::IF_LT:
1868 case Instruction::IF_GE:
1869 case Instruction::IF_GT:
Ian Rogersd81871c2011-10-03 13:57:23 -07001870 case Instruction::IF_LE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001871 const RegType& reg_type1 = work_line_->GetRegisterType(inst->VRegA_22t());
1872 const RegType& reg_type2 = work_line_->GetRegisterType(inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07001873 if (!reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001874 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to 'if' (" << reg_type1 << ","
1875 << reg_type2 << ") must be integral";
jeffhaobdb76512011-09-07 11:43:16 -07001876 }
1877 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001878 }
jeffhaobdb76512011-09-07 11:43:16 -07001879 case Instruction::IF_EQZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07001880 case Instruction::IF_NEZ: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001881 const RegType& reg_type = work_line_->GetRegisterType(inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07001882 if (!reg_type.IsReferenceTypes() && !reg_type.IsIntegralTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001883 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
1884 << " unexpected as arg to if-eqz/if-nez";
Ian Rogersd81871c2011-10-03 13:57:23 -07001885 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001886
1887 // Find previous instruction - its existence is a precondition to peephole optimization.
Ian Rogers9b360392013-06-06 14:45:07 -07001888 uint32_t instance_of_idx = 0;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001889 if (0 != work_insn_idx_) {
Ian Rogers9b360392013-06-06 14:45:07 -07001890 instance_of_idx = work_insn_idx_ - 1;
Brian Carlstromdf629502013-07-17 22:39:56 -07001891 while (0 != instance_of_idx && !insn_flags_[instance_of_idx].IsOpcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07001892 instance_of_idx--;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001893 }
Ian Rogers9b360392013-06-06 14:45:07 -07001894 CHECK(insn_flags_[instance_of_idx].IsOpcode());
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001895 } else {
1896 break;
1897 }
1898
Ian Rogers9b360392013-06-06 14:45:07 -07001899 const Instruction* instance_of_inst = Instruction::At(code_item_->insns_ + instance_of_idx);
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001900
1901 /* Check for peep-hole pattern of:
1902 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07001903 * instance-of vX, vY, T;
1904 * ifXXX vX, label ;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001905 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07001906 * label:
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001907 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07001908 * and sharpen the type of vY to be type T.
1909 * Note, this pattern can't be if:
1910 * - if there are other branches to this branch,
1911 * - when vX == vY.
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001912 */
Ian Rogersfae370a2013-06-05 08:33:27 -07001913 if (!CurrentInsnFlags()->IsBranchTarget() &&
Ian Rogers9b360392013-06-06 14:45:07 -07001914 (Instruction::INSTANCE_OF == instance_of_inst->Opcode()) &&
1915 (inst->VRegA_21t() == instance_of_inst->VRegA_22c()) &&
1916 (instance_of_inst->VRegA_22c() != instance_of_inst->VRegB_22c())) {
Ian Rogersfae370a2013-06-05 08:33:27 -07001917 // Check that the we are not attempting conversion to interface types,
1918 // which is not done because of the multiple inheritance implications.
Ian Rogers9b360392013-06-06 14:45:07 -07001919 const RegType& cast_type = ResolveClassAndCheckAccess(instance_of_inst->VRegC_22c());
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001920
Brian Carlstromdf629502013-07-17 22:39:56 -07001921 if (!cast_type.IsUnresolvedTypes() && !cast_type.GetClass()->IsInterface()) {
Ian Rogers9b360392013-06-06 14:45:07 -07001922 RegisterLine* update_line = new RegisterLine(code_item_->registers_size_, this);
Ian Rogersfae370a2013-06-05 08:33:27 -07001923 if (inst->Opcode() == Instruction::IF_EQZ) {
Ian Rogers9b360392013-06-06 14:45:07 -07001924 fallthrough_line.reset(update_line);
Ian Rogersfae370a2013-06-05 08:33:27 -07001925 } else {
Ian Rogers9b360392013-06-06 14:45:07 -07001926 branch_line.reset(update_line);
1927 }
1928 update_line->CopyFromLine(work_line_.get());
1929 update_line->SetRegisterType(instance_of_inst->VRegB_22c(), cast_type);
1930 if (!insn_flags_[instance_of_idx].IsBranchTarget() && 0 != instance_of_idx) {
1931 // See if instance-of was preceded by a move-object operation, common due to the small
1932 // register encoding space of instance-of, and propagate type information to the source
1933 // of the move-object.
1934 uint32_t move_idx = instance_of_idx - 1;
Brian Carlstromdf629502013-07-17 22:39:56 -07001935 while (0 != move_idx && !insn_flags_[move_idx].IsOpcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07001936 move_idx--;
1937 }
1938 CHECK(insn_flags_[move_idx].IsOpcode());
1939 const Instruction* move_inst = Instruction::At(code_item_->insns_ + move_idx);
1940 switch (move_inst->Opcode()) {
1941 case Instruction::MOVE_OBJECT:
1942 if (move_inst->VRegA_12x() == instance_of_inst->VRegB_22c()) {
1943 update_line->SetRegisterType(move_inst->VRegB_12x(), cast_type);
1944 }
1945 break;
1946 case Instruction::MOVE_OBJECT_FROM16:
1947 if (move_inst->VRegA_22x() == instance_of_inst->VRegB_22c()) {
1948 update_line->SetRegisterType(move_inst->VRegB_22x(), cast_type);
1949 }
1950 break;
1951 case Instruction::MOVE_OBJECT_16:
1952 if (move_inst->VRegA_32x() == instance_of_inst->VRegB_22c()) {
1953 update_line->SetRegisterType(move_inst->VRegB_32x(), cast_type);
1954 }
1955 break;
1956 default:
1957 break;
1958 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001959 }
1960 }
1961 }
1962
jeffhaobdb76512011-09-07 11:43:16 -07001963 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001964 }
jeffhaobdb76512011-09-07 11:43:16 -07001965 case Instruction::IF_LTZ:
1966 case Instruction::IF_GEZ:
1967 case Instruction::IF_GTZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07001968 case Instruction::IF_LEZ: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001969 const RegType& reg_type = work_line_->GetRegisterType(inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07001970 if (!reg_type.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001971 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
1972 << " unexpected as arg to if-ltz/if-gez/if-gtz/if-lez";
Ian Rogersd81871c2011-10-03 13:57:23 -07001973 }
jeffhaobdb76512011-09-07 11:43:16 -07001974 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001975 }
jeffhaobdb76512011-09-07 11:43:16 -07001976 case Instruction::AGET_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001977 VerifyAGet(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001978 break;
jeffhaobdb76512011-09-07 11:43:16 -07001979 case Instruction::AGET_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001980 VerifyAGet(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001981 break;
jeffhaobdb76512011-09-07 11:43:16 -07001982 case Instruction::AGET_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001983 VerifyAGet(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001984 break;
jeffhaobdb76512011-09-07 11:43:16 -07001985 case Instruction::AGET_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001986 VerifyAGet(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001987 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001988 case Instruction::AGET:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001989 VerifyAGet(inst, reg_types_.Integer(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001990 break;
jeffhaobdb76512011-09-07 11:43:16 -07001991 case Instruction::AGET_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001992 VerifyAGet(inst, reg_types_.LongLo(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001993 break;
1994 case Instruction::AGET_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001995 VerifyAGet(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07001996 break;
1997
Ian Rogersd81871c2011-10-03 13:57:23 -07001998 case Instruction::APUT_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001999 VerifyAPut(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002000 break;
2001 case Instruction::APUT_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002002 VerifyAPut(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002003 break;
2004 case Instruction::APUT_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002005 VerifyAPut(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002006 break;
2007 case Instruction::APUT_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002008 VerifyAPut(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002009 break;
2010 case Instruction::APUT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002011 VerifyAPut(inst, reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002012 break;
2013 case Instruction::APUT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002014 VerifyAPut(inst, reg_types_.LongLo(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002015 break;
2016 case Instruction::APUT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002017 VerifyAPut(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07002018 break;
2019
jeffhaobdb76512011-09-07 11:43:16 -07002020 case Instruction::IGET_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002021 VerifyISGet(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002022 break;
jeffhaobdb76512011-09-07 11:43:16 -07002023 case Instruction::IGET_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002024 VerifyISGet(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002025 break;
jeffhaobdb76512011-09-07 11:43:16 -07002026 case Instruction::IGET_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002027 VerifyISGet(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002028 break;
jeffhaobdb76512011-09-07 11:43:16 -07002029 case Instruction::IGET_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002030 VerifyISGet(inst, reg_types_.Short(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002031 break;
2032 case Instruction::IGET:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002033 VerifyISGet(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002034 break;
2035 case Instruction::IGET_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002036 VerifyISGet(inst, reg_types_.LongLo(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002037 break;
2038 case Instruction::IGET_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002039 VerifyISGet(inst, reg_types_.JavaLangObject(false), false, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002040 break;
jeffhaobdb76512011-09-07 11:43:16 -07002041
Ian Rogersd81871c2011-10-03 13:57:23 -07002042 case Instruction::IPUT_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002043 VerifyISPut(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002044 break;
2045 case Instruction::IPUT_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002046 VerifyISPut(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002047 break;
2048 case Instruction::IPUT_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002049 VerifyISPut(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002050 break;
2051 case Instruction::IPUT_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002052 VerifyISPut(inst, reg_types_.Short(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002053 break;
2054 case Instruction::IPUT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002055 VerifyISPut(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002056 break;
2057 case Instruction::IPUT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002058 VerifyISPut(inst, reg_types_.LongLo(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002059 break;
jeffhaobdb76512011-09-07 11:43:16 -07002060 case Instruction::IPUT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002061 VerifyISPut(inst, reg_types_.JavaLangObject(false), false, false);
jeffhaobdb76512011-09-07 11:43:16 -07002062 break;
2063
jeffhaobdb76512011-09-07 11:43:16 -07002064 case Instruction::SGET_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002065 VerifyISGet(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002066 break;
jeffhaobdb76512011-09-07 11:43:16 -07002067 case Instruction::SGET_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002068 VerifyISGet(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002069 break;
jeffhaobdb76512011-09-07 11:43:16 -07002070 case Instruction::SGET_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002071 VerifyISGet(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002072 break;
jeffhaobdb76512011-09-07 11:43:16 -07002073 case Instruction::SGET_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002074 VerifyISGet(inst, reg_types_.Short(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002075 break;
2076 case Instruction::SGET:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002077 VerifyISGet(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002078 break;
2079 case Instruction::SGET_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002080 VerifyISGet(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002081 break;
2082 case Instruction::SGET_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002083 VerifyISGet(inst, reg_types_.JavaLangObject(false), false, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002084 break;
2085
2086 case Instruction::SPUT_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002087 VerifyISPut(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002088 break;
2089 case Instruction::SPUT_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002090 VerifyISPut(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002091 break;
2092 case Instruction::SPUT_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002093 VerifyISPut(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002094 break;
2095 case Instruction::SPUT_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002096 VerifyISPut(inst, reg_types_.Short(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002097 break;
2098 case Instruction::SPUT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002099 VerifyISPut(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002100 break;
2101 case Instruction::SPUT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002102 VerifyISPut(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002103 break;
2104 case Instruction::SPUT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002105 VerifyISPut(inst, reg_types_.JavaLangObject(false), false, true);
jeffhaobdb76512011-09-07 11:43:16 -07002106 break;
2107
2108 case Instruction::INVOKE_VIRTUAL:
2109 case Instruction::INVOKE_VIRTUAL_RANGE:
2110 case Instruction::INVOKE_SUPER:
Ian Rogersd81871c2011-10-03 13:57:23 -07002111 case Instruction::INVOKE_SUPER_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002112 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE ||
2113 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
2114 bool is_super = (inst->Opcode() == Instruction::INVOKE_SUPER ||
2115 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
2116 mirror::AbstractMethod* called_method = VerifyInvocationArgs(inst, METHOD_VIRTUAL,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002117 is_range, is_super);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002118 const char* descriptor;
2119 if (called_method == NULL) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002120 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002121 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2122 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
2123 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2124 } else {
2125 descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
jeffhaobdb76512011-09-07 11:43:16 -07002126 }
Ian Rogersb4903572012-10-11 11:52:56 -07002127 const RegType& return_type = reg_types_.FromDescriptor(class_loader_, descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002128 if (!return_type.IsLowHalf()) {
2129 work_line_->SetResultRegisterType(return_type);
2130 } else {
2131 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2132 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002133 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002134 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002135 }
jeffhaobdb76512011-09-07 11:43:16 -07002136 case Instruction::INVOKE_DIRECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002137 case Instruction::INVOKE_DIRECT_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002138 bool is_range = (inst->Opcode() == Instruction::INVOKE_DIRECT_RANGE);
2139 mirror::AbstractMethod* called_method = VerifyInvocationArgs(inst, METHOD_DIRECT,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002140 is_range, false);
Ian Rogers46685432012-06-03 22:26:43 -07002141 const char* return_type_descriptor;
2142 bool is_constructor;
2143 if (called_method == NULL) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002144 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogers46685432012-06-03 22:26:43 -07002145 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2146 is_constructor = StringPiece(dex_file_->GetMethodName(method_id)) == "<init>";
2147 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
2148 return_type_descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2149 } else {
2150 is_constructor = called_method->IsConstructor();
2151 return_type_descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
2152 }
2153 if (is_constructor) {
jeffhaobdb76512011-09-07 11:43:16 -07002154 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002155 * Some additional checks when calling a constructor. We know from the invocation arg check
2156 * that the "this" argument is an instance of called_method->klass. Now we further restrict
2157 * that to require that called_method->klass is the same as this->klass or this->super,
2158 * allowing the latter only if the "this" argument is the same as the "this" argument to
2159 * this method (which implies that we're in a constructor ourselves).
jeffhaobdb76512011-09-07 11:43:16 -07002160 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002161 const RegType& this_type = work_line_->GetInvocationThis(inst, is_range);
jeffhaob57e9522012-04-26 18:08:21 -07002162 if (this_type.IsConflict()) // failure.
2163 break;
jeffhaobdb76512011-09-07 11:43:16 -07002164
jeffhaob57e9522012-04-26 18:08:21 -07002165 /* no null refs allowed (?) */
2166 if (this_type.IsZero()) {
2167 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unable to initialize null ref";
2168 break;
jeffhao2a8a90e2011-09-26 14:25:31 -07002169 }
jeffhaob57e9522012-04-26 18:08:21 -07002170
2171 /* must be in same class or in superclass */
Ian Rogers46685432012-06-03 22:26:43 -07002172 // const RegType& this_super_klass = this_type.GetSuperClass(&reg_types_);
2173 // TODO: re-enable constructor type verification
2174 // if (this_super_klass.IsConflict()) {
jeffhaob57e9522012-04-26 18:08:21 -07002175 // Unknown super class, fail so we re-check at runtime.
Ian Rogers46685432012-06-03 22:26:43 -07002176 // Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "super class unknown for '" << this_type << "'";
2177 // break;
2178 // }
jeffhaob57e9522012-04-26 18:08:21 -07002179
2180 /* arg must be an uninitialized reference */
2181 if (!this_type.IsUninitializedTypes()) {
2182 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Expected initialization on uninitialized reference "
2183 << this_type;
2184 break;
2185 }
2186
2187 /*
2188 * Replace the uninitialized reference with an initialized one. We need to do this for all
2189 * registers that have the same object instance in them, not just the "this" register.
2190 */
2191 work_line_->MarkRefsAsInitialized(this_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002192 }
Ian Rogersb4903572012-10-11 11:52:56 -07002193 const RegType& return_type = reg_types_.FromDescriptor(class_loader_, return_type_descriptor,
2194 false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002195 if (!return_type.IsLowHalf()) {
2196 work_line_->SetResultRegisterType(return_type);
2197 } else {
2198 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2199 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002200 just_set_result = true;
2201 break;
2202 }
2203 case Instruction::INVOKE_STATIC:
2204 case Instruction::INVOKE_STATIC_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002205 bool is_range = (inst->Opcode() == Instruction::INVOKE_STATIC_RANGE);
Brian Carlstrom93c33962013-07-26 10:37:43 -07002206 mirror::AbstractMethod* called_method = VerifyInvocationArgs(inst,
2207 METHOD_STATIC,
2208 is_range,
2209 false);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002210 const char* descriptor;
2211 if (called_method == NULL) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002212 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002213 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2214 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogers0571d352011-11-03 19:51:38 -07002215 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002216 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002217 descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002218 }
Ian Rogersb4903572012-10-11 11:52:56 -07002219 const RegType& return_type = reg_types_.FromDescriptor(class_loader_, descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002220 if (!return_type.IsLowHalf()) {
2221 work_line_->SetResultRegisterType(return_type);
2222 } else {
2223 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2224 }
jeffhaobdb76512011-09-07 11:43:16 -07002225 just_set_result = true;
2226 }
2227 break;
jeffhaobdb76512011-09-07 11:43:16 -07002228 case Instruction::INVOKE_INTERFACE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002229 case Instruction::INVOKE_INTERFACE_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002230 bool is_range = (inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE);
Brian Carlstrom93c33962013-07-26 10:37:43 -07002231 mirror::AbstractMethod* abs_method = VerifyInvocationArgs(inst,
2232 METHOD_INTERFACE,
2233 is_range,
2234 false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002235 if (abs_method != NULL) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002236 mirror::Class* called_interface = abs_method->GetDeclaringClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002237 if (!called_interface->IsInterface() && !called_interface->IsObjectClass()) {
2238 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected interface class in invoke-interface '"
2239 << PrettyMethod(abs_method) << "'";
2240 break;
Ian Rogers28ad40d2011-10-27 15:19:26 -07002241 }
Ian Rogers0d604842012-04-16 14:50:24 -07002242 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002243 /* Get the type of the "this" arg, which should either be a sub-interface of called
2244 * interface or Object (see comments in RegType::JoinClass).
2245 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002246 const RegType& this_type = work_line_->GetInvocationThis(inst, is_range);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002247 if (this_type.IsZero()) {
2248 /* null pointer always passes (and always fails at runtime) */
2249 } else {
2250 if (this_type.IsUninitializedTypes()) {
2251 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface call on uninitialized object "
2252 << this_type;
2253 break;
2254 }
2255 // In the past we have tried to assert that "called_interface" is assignable
2256 // from "this_type.GetClass()", however, as we do an imprecise Join
2257 // (RegType::JoinClass) we don't have full information on what interfaces are
2258 // implemented by "this_type". For example, two classes may implement the same
2259 // interfaces and have a common parent that doesn't implement the interface. The
2260 // join will set "this_type" to the parent class and a test that this implements
2261 // the interface will incorrectly fail.
2262 }
2263 /*
2264 * We don't have an object instance, so we can't find the concrete method. However, all of
2265 * the type information is in the abstract method, so we're good.
2266 */
2267 const char* descriptor;
2268 if (abs_method == NULL) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002269 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002270 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2271 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
2272 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2273 } else {
2274 descriptor = MethodHelper(abs_method).GetReturnTypeDescriptor();
2275 }
Ian Rogersb4903572012-10-11 11:52:56 -07002276 const RegType& return_type = reg_types_.FromDescriptor(class_loader_, descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002277 if (!return_type.IsLowHalf()) {
2278 work_line_->SetResultRegisterType(return_type);
2279 } else {
2280 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2281 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002282 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002283 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002284 }
jeffhaobdb76512011-09-07 11:43:16 -07002285 case Instruction::NEG_INT:
2286 case Instruction::NOT_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002287 work_line_->CheckUnaryOp(inst, reg_types_.Integer(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002288 break;
2289 case Instruction::NEG_LONG:
2290 case Instruction::NOT_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_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002293 break;
2294 case Instruction::NEG_FLOAT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002295 work_line_->CheckUnaryOp(inst, reg_types_.Float(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002296 break;
2297 case Instruction::NEG_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002298 work_line_->CheckUnaryOpWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002299 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002300 break;
2301 case Instruction::INT_TO_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002302 work_line_->CheckUnaryOpToWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002303 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002304 break;
2305 case Instruction::INT_TO_FLOAT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002306 work_line_->CheckUnaryOp(inst, reg_types_.Float(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002307 break;
2308 case Instruction::INT_TO_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002309 work_line_->CheckUnaryOpToWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002310 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002311 break;
2312 case Instruction::LONG_TO_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002313 work_line_->CheckUnaryOpFromWide(inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002314 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002315 break;
2316 case Instruction::LONG_TO_FLOAT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002317 work_line_->CheckUnaryOpFromWide(inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002318 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002319 break;
2320 case Instruction::LONG_TO_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002321 work_line_->CheckUnaryOpWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002322 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002323 break;
2324 case Instruction::FLOAT_TO_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002325 work_line_->CheckUnaryOp(inst, reg_types_.Integer(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002326 break;
2327 case Instruction::FLOAT_TO_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002328 work_line_->CheckUnaryOpToWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002329 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002330 break;
2331 case Instruction::FLOAT_TO_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002332 work_line_->CheckUnaryOpToWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002333 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002334 break;
2335 case Instruction::DOUBLE_TO_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002336 work_line_->CheckUnaryOpFromWide(inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002337 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002338 break;
2339 case Instruction::DOUBLE_TO_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002340 work_line_->CheckUnaryOpWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002341 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002342 break;
2343 case Instruction::DOUBLE_TO_FLOAT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002344 work_line_->CheckUnaryOpFromWide(inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002345 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002346 break;
2347 case Instruction::INT_TO_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002348 work_line_->CheckUnaryOp(inst, reg_types_.Byte(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002349 break;
2350 case Instruction::INT_TO_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002351 work_line_->CheckUnaryOp(inst, reg_types_.Char(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002352 break;
2353 case Instruction::INT_TO_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002354 work_line_->CheckUnaryOp(inst, reg_types_.Short(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002355 break;
2356
2357 case Instruction::ADD_INT:
2358 case Instruction::SUB_INT:
2359 case Instruction::MUL_INT:
2360 case Instruction::REM_INT:
2361 case Instruction::DIV_INT:
2362 case Instruction::SHL_INT:
2363 case Instruction::SHR_INT:
2364 case Instruction::USHR_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002365 work_line_->CheckBinaryOp(inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002366 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002367 break;
2368 case Instruction::AND_INT:
2369 case Instruction::OR_INT:
2370 case Instruction::XOR_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002371 work_line_->CheckBinaryOp(inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002372 reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002373 break;
2374 case Instruction::ADD_LONG:
2375 case Instruction::SUB_LONG:
2376 case Instruction::MUL_LONG:
2377 case Instruction::DIV_LONG:
2378 case Instruction::REM_LONG:
2379 case Instruction::AND_LONG:
2380 case Instruction::OR_LONG:
2381 case Instruction::XOR_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002382 work_line_->CheckBinaryOpWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002383 reg_types_.LongLo(), reg_types_.LongHi(),
2384 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002385 break;
2386 case Instruction::SHL_LONG:
2387 case Instruction::SHR_LONG:
2388 case Instruction::USHR_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002389 /* shift distance is Int, making these different from other binary operations */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002390 work_line_->CheckBinaryOpWideShift(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002391 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002392 break;
2393 case Instruction::ADD_FLOAT:
2394 case Instruction::SUB_FLOAT:
2395 case Instruction::MUL_FLOAT:
2396 case Instruction::DIV_FLOAT:
2397 case Instruction::REM_FLOAT:
Brian Carlstrom93c33962013-07-26 10:37:43 -07002398 work_line_->CheckBinaryOp(inst,
2399 reg_types_.Float(),
2400 reg_types_.Float(),
2401 reg_types_.Float(),
2402 false);
jeffhaobdb76512011-09-07 11:43:16 -07002403 break;
2404 case Instruction::ADD_DOUBLE:
2405 case Instruction::SUB_DOUBLE:
2406 case Instruction::MUL_DOUBLE:
2407 case Instruction::DIV_DOUBLE:
2408 case Instruction::REM_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002409 work_line_->CheckBinaryOpWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002410 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2411 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002412 break;
2413 case Instruction::ADD_INT_2ADDR:
2414 case Instruction::SUB_INT_2ADDR:
2415 case Instruction::MUL_INT_2ADDR:
2416 case Instruction::REM_INT_2ADDR:
2417 case Instruction::SHL_INT_2ADDR:
2418 case Instruction::SHR_INT_2ADDR:
2419 case Instruction::USHR_INT_2ADDR:
Brian Carlstrom93c33962013-07-26 10:37:43 -07002420 work_line_->CheckBinaryOp2addr(inst,
2421 reg_types_.Integer(),
2422 reg_types_.Integer(),
2423 reg_types_.Integer(),
2424 false);
jeffhaobdb76512011-09-07 11:43:16 -07002425 break;
2426 case Instruction::AND_INT_2ADDR:
2427 case Instruction::OR_INT_2ADDR:
2428 case Instruction::XOR_INT_2ADDR:
Brian Carlstrom93c33962013-07-26 10:37:43 -07002429 work_line_->CheckBinaryOp2addr(inst,
2430 reg_types_.Integer(),
2431 reg_types_.Integer(),
2432 reg_types_.Integer(),
2433 true);
jeffhaobdb76512011-09-07 11:43:16 -07002434 break;
2435 case Instruction::DIV_INT_2ADDR:
Brian Carlstrom93c33962013-07-26 10:37:43 -07002436 work_line_->CheckBinaryOp2addr(inst,
2437 reg_types_.Integer(),
2438 reg_types_.Integer(),
2439 reg_types_.Integer(),
2440 false);
jeffhaobdb76512011-09-07 11:43:16 -07002441 break;
2442 case Instruction::ADD_LONG_2ADDR:
2443 case Instruction::SUB_LONG_2ADDR:
2444 case Instruction::MUL_LONG_2ADDR:
2445 case Instruction::DIV_LONG_2ADDR:
2446 case Instruction::REM_LONG_2ADDR:
2447 case Instruction::AND_LONG_2ADDR:
2448 case Instruction::OR_LONG_2ADDR:
2449 case Instruction::XOR_LONG_2ADDR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002450 work_line_->CheckBinaryOp2addrWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002451 reg_types_.LongLo(), reg_types_.LongHi(),
2452 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002453 break;
2454 case Instruction::SHL_LONG_2ADDR:
2455 case Instruction::SHR_LONG_2ADDR:
2456 case Instruction::USHR_LONG_2ADDR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002457 work_line_->CheckBinaryOp2addrWideShift(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002458 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002459 break;
2460 case Instruction::ADD_FLOAT_2ADDR:
2461 case Instruction::SUB_FLOAT_2ADDR:
2462 case Instruction::MUL_FLOAT_2ADDR:
2463 case Instruction::DIV_FLOAT_2ADDR:
2464 case Instruction::REM_FLOAT_2ADDR:
Brian Carlstrom93c33962013-07-26 10:37:43 -07002465 work_line_->CheckBinaryOp2addr(inst,
2466 reg_types_.Float(),
2467 reg_types_.Float(),
2468 reg_types_.Float(),
2469 false);
jeffhaobdb76512011-09-07 11:43:16 -07002470 break;
2471 case Instruction::ADD_DOUBLE_2ADDR:
2472 case Instruction::SUB_DOUBLE_2ADDR:
2473 case Instruction::MUL_DOUBLE_2ADDR:
2474 case Instruction::DIV_DOUBLE_2ADDR:
2475 case Instruction::REM_DOUBLE_2ADDR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002476 work_line_->CheckBinaryOp2addrWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002477 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2478 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002479 break;
2480 case Instruction::ADD_INT_LIT16:
2481 case Instruction::RSUB_INT:
2482 case Instruction::MUL_INT_LIT16:
2483 case Instruction::DIV_INT_LIT16:
2484 case Instruction::REM_INT_LIT16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002485 work_line_->CheckLiteralOp(inst, reg_types_.Integer(), reg_types_.Integer(), false, true);
jeffhaobdb76512011-09-07 11:43:16 -07002486 break;
2487 case Instruction::AND_INT_LIT16:
2488 case Instruction::OR_INT_LIT16:
2489 case Instruction::XOR_INT_LIT16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002490 work_line_->CheckLiteralOp(inst, reg_types_.Integer(), reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002491 break;
2492 case Instruction::ADD_INT_LIT8:
2493 case Instruction::RSUB_INT_LIT8:
2494 case Instruction::MUL_INT_LIT8:
2495 case Instruction::DIV_INT_LIT8:
2496 case Instruction::REM_INT_LIT8:
2497 case Instruction::SHL_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002498 case Instruction::SHR_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002499 case Instruction::USHR_INT_LIT8:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002500 work_line_->CheckLiteralOp(inst, reg_types_.Integer(), reg_types_.Integer(), false, false);
jeffhaobdb76512011-09-07 11:43:16 -07002501 break;
2502 case Instruction::AND_INT_LIT8:
2503 case Instruction::OR_INT_LIT8:
2504 case Instruction::XOR_INT_LIT8:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002505 work_line_->CheckLiteralOp(inst, reg_types_.Integer(), reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002506 break;
2507
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002508 // Special instructions.
Sebastien Hertzcc10e0e2013-06-28 14:24:48 +02002509 case Instruction::RETURN_VOID_BARRIER:
2510 DCHECK(Runtime::Current()->IsStarted());
2511 if (!IsConstructor()) {
2512 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void-barrier not expected";
2513 }
2514 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002515 // Note: the following instructions encode offsets derived from class linking.
2516 // As such they use Class*/Field*/AbstractMethod* as these offsets only have
2517 // meaning if the class linking and resolution were successful.
2518 case Instruction::IGET_QUICK:
2519 VerifyIGetQuick(inst, reg_types_.Integer(), true);
2520 break;
2521 case Instruction::IGET_WIDE_QUICK:
2522 VerifyIGetQuick(inst, reg_types_.LongLo(), true);
2523 break;
2524 case Instruction::IGET_OBJECT_QUICK:
2525 VerifyIGetQuick(inst, reg_types_.JavaLangObject(false), false);
2526 break;
2527 case Instruction::IPUT_QUICK:
2528 VerifyIPutQuick(inst, reg_types_.Integer(), true);
2529 break;
2530 case Instruction::IPUT_WIDE_QUICK:
2531 VerifyIPutQuick(inst, reg_types_.LongLo(), true);
2532 break;
2533 case Instruction::IPUT_OBJECT_QUICK:
2534 VerifyIPutQuick(inst, reg_types_.JavaLangObject(false), false);
2535 break;
2536 case Instruction::INVOKE_VIRTUAL_QUICK:
2537 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: {
2538 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
2539 mirror::AbstractMethod* called_method = VerifyInvokeVirtualQuickArgs(inst, is_range);
2540 if (called_method != NULL) {
2541 const char* descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
2542 const RegType& return_type = reg_types_.FromDescriptor(class_loader_, descriptor, false);
2543 if (!return_type.IsLowHalf()) {
2544 work_line_->SetResultRegisterType(return_type);
2545 } else {
2546 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2547 }
2548 just_set_result = true;
2549 }
2550 break;
2551 }
2552
Ian Rogersd81871c2011-10-03 13:57:23 -07002553 /* These should never appear during verification. */
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002554 case Instruction::UNUSED_3E:
2555 case Instruction::UNUSED_3F:
2556 case Instruction::UNUSED_40:
2557 case Instruction::UNUSED_41:
2558 case Instruction::UNUSED_42:
2559 case Instruction::UNUSED_43:
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002560 case Instruction::UNUSED_79:
2561 case Instruction::UNUSED_7A:
2562 case Instruction::UNUSED_EB:
2563 case Instruction::UNUSED_EC:
jeffhao9a4f0032012-08-30 16:17:40 -07002564 case Instruction::UNUSED_ED:
jeffhaobdb76512011-09-07 11:43:16 -07002565 case Instruction::UNUSED_EE:
2566 case Instruction::UNUSED_EF:
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002567 case Instruction::UNUSED_F0:
2568 case Instruction::UNUSED_F1:
jeffhaobdb76512011-09-07 11:43:16 -07002569 case Instruction::UNUSED_F2:
2570 case Instruction::UNUSED_F3:
2571 case Instruction::UNUSED_F4:
2572 case Instruction::UNUSED_F5:
2573 case Instruction::UNUSED_F6:
2574 case Instruction::UNUSED_F7:
2575 case Instruction::UNUSED_F8:
2576 case Instruction::UNUSED_F9:
2577 case Instruction::UNUSED_FA:
2578 case Instruction::UNUSED_FB:
jeffhaobdb76512011-09-07 11:43:16 -07002579 case Instruction::UNUSED_FC:
jeffhaobdb76512011-09-07 11:43:16 -07002580 case Instruction::UNUSED_FD:
jeffhaobdb76512011-09-07 11:43:16 -07002581 case Instruction::UNUSED_FE:
jeffhaobdb76512011-09-07 11:43:16 -07002582 case Instruction::UNUSED_FF:
jeffhaod5347e02012-03-22 17:25:05 -07002583 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Unexpected opcode " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07002584 break;
2585
2586 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002587 * DO NOT add a "default" clause here. Without it the compiler will
jeffhaobdb76512011-09-07 11:43:16 -07002588 * complain if an instruction is missing (which is desirable).
2589 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002590 } // end - switch (dec_insn.opcode)
jeffhaobdb76512011-09-07 11:43:16 -07002591
Ian Rogersad0b3a32012-04-16 14:50:24 -07002592 if (have_pending_hard_failure_) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002593 if (Runtime::Current()->IsCompiler()) {
jeffhaob57e9522012-04-26 18:08:21 -07002594 /* When compiling, check that the last failure is a hard failure */
Ian Rogersad0b3a32012-04-16 14:50:24 -07002595 CHECK_EQ(failures_[failures_.size() - 1], VERIFY_ERROR_BAD_CLASS_HARD);
Ian Rogerse1758fe2012-04-19 11:31:15 -07002596 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002597 /* immediate failure, reject class */
2598 info_messages_ << "Rejecting opcode " << inst->DumpString(dex_file_);
2599 return false;
jeffhaofaf459e2012-08-31 15:32:47 -07002600 } else if (have_pending_runtime_throw_failure_) {
2601 /* slow path will throw, mark following code as unreachable */
2602 opcode_flags = Instruction::kThrow;
jeffhaobdb76512011-09-07 11:43:16 -07002603 }
jeffhaobdb76512011-09-07 11:43:16 -07002604 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002605 * If we didn't just set the result register, clear it out. This ensures that you can only use
2606 * "move-result" immediately after the result is set. (We could check this statically, but it's
2607 * not expensive and it makes our debugging output cleaner.)
jeffhaobdb76512011-09-07 11:43:16 -07002608 */
2609 if (!just_set_result) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002610 work_line_->SetResultTypeToUnknown();
jeffhaobdb76512011-09-07 11:43:16 -07002611 }
2612
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002613
jeffhaobdb76512011-09-07 11:43:16 -07002614
2615 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002616 * Handle "branch". Tag the branch target.
jeffhaobdb76512011-09-07 11:43:16 -07002617 *
2618 * NOTE: instructions like Instruction::EQZ provide information about the
jeffhaod1f0fde2011-09-08 17:25:33 -07002619 * state of the register when the branch is taken or not taken. For example,
jeffhaobdb76512011-09-07 11:43:16 -07002620 * somebody could get a reference field, check it for zero, and if the
2621 * branch is taken immediately store that register in a boolean field
jeffhaod1f0fde2011-09-08 17:25:33 -07002622 * since the value is known to be zero. We do not currently account for
jeffhaobdb76512011-09-07 11:43:16 -07002623 * that, and will reject the code.
2624 *
2625 * TODO: avoid re-fetching the branch target
2626 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002627 if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002628 bool isConditional, selfOkay;
Ian Rogersd81871c2011-10-03 13:57:23 -07002629 if (!GetBranchOffset(work_insn_idx_, &branch_target, &isConditional, &selfOkay)) {
jeffhaobdb76512011-09-07 11:43:16 -07002630 /* should never happen after static verification */
jeffhaod5347e02012-03-22 17:25:05 -07002631 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad branch";
jeffhaobdb76512011-09-07 11:43:16 -07002632 return false;
2633 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08002634 DCHECK_EQ(isConditional, (opcode_flags & Instruction::kContinue) != 0);
jeffhaod5347e02012-03-22 17:25:05 -07002635 if (!CheckNotMoveException(code_item_->insns_, work_insn_idx_ + branch_target)) {
jeffhaobdb76512011-09-07 11:43:16 -07002636 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002637 }
jeffhaobdb76512011-09-07 11:43:16 -07002638 /* update branch target, set "changed" if appropriate */
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002639 if (NULL != branch_line.get()) {
2640 if (!UpdateRegisters(work_insn_idx_ + branch_target, branch_line.get())) {
2641 return false;
2642 }
2643 } else {
2644 if (!UpdateRegisters(work_insn_idx_ + branch_target, work_line_.get())) {
2645 return false;
2646 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002647 }
jeffhaobdb76512011-09-07 11:43:16 -07002648 }
2649
2650 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002651 * Handle "switch". Tag all possible branch targets.
jeffhaobdb76512011-09-07 11:43:16 -07002652 *
2653 * We've already verified that the table is structurally sound, so we
2654 * just need to walk through and tag the targets.
2655 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002656 if ((opcode_flags & Instruction::kSwitch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002657 int offset_to_switch = insns[1] | (((int32_t) insns[2]) << 16);
2658 const uint16_t* switch_insns = insns + offset_to_switch;
2659 int switch_count = switch_insns[1];
2660 int offset_to_targets, targ;
2661
2662 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
2663 /* 0 = sig, 1 = count, 2/3 = first key */
2664 offset_to_targets = 4;
2665 } else {
2666 /* 0 = sig, 1 = count, 2..count * 2 = keys */
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07002667 DCHECK((*insns & 0xff) == Instruction::SPARSE_SWITCH);
jeffhaobdb76512011-09-07 11:43:16 -07002668 offset_to_targets = 2 + 2 * switch_count;
2669 }
2670
2671 /* verify each switch target */
2672 for (targ = 0; targ < switch_count; targ++) {
2673 int offset;
2674 uint32_t abs_offset;
2675
2676 /* offsets are 32-bit, and only partly endian-swapped */
2677 offset = switch_insns[offset_to_targets + targ * 2] |
2678 (((int32_t) switch_insns[offset_to_targets + targ * 2 + 1]) << 16);
Ian Rogersd81871c2011-10-03 13:57:23 -07002679 abs_offset = work_insn_idx_ + offset;
2680 DCHECK_LT(abs_offset, code_item_->insns_size_in_code_units_);
jeffhaod5347e02012-03-22 17:25:05 -07002681 if (!CheckNotMoveException(code_item_->insns_, abs_offset)) {
jeffhaobdb76512011-09-07 11:43:16 -07002682 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002683 }
2684 if (!UpdateRegisters(abs_offset, work_line_.get()))
jeffhaobdb76512011-09-07 11:43:16 -07002685 return false;
2686 }
2687 }
2688
2689 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002690 * Handle instructions that can throw and that are sitting in a "try" block. (If they're not in a
2691 * "try" block when they throw, control transfers out of the method.)
jeffhaobdb76512011-09-07 11:43:16 -07002692 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002693 if ((opcode_flags & Instruction::kThrow) != 0 && insn_flags_[work_insn_idx_].IsInTry()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002694 bool within_catch_all = false;
Ian Rogers0571d352011-11-03 19:51:38 -07002695 CatchHandlerIterator iterator(*code_item_, work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07002696
Ian Rogers0571d352011-11-03 19:51:38 -07002697 for (; iterator.HasNext(); iterator.Next()) {
2698 if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002699 within_catch_all = true;
2700 }
jeffhaobdb76512011-09-07 11:43:16 -07002701 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002702 * Merge registers into the "catch" block. We want to use the "savedRegs" rather than
2703 * "work_regs", because at runtime the exception will be thrown before the instruction
2704 * modifies any registers.
jeffhaobdb76512011-09-07 11:43:16 -07002705 */
Ian Rogers0571d352011-11-03 19:51:38 -07002706 if (!UpdateRegisters(iterator.GetHandlerAddress(), saved_line_.get())) {
jeffhaobdb76512011-09-07 11:43:16 -07002707 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002708 }
jeffhaobdb76512011-09-07 11:43:16 -07002709 }
2710
2711 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002712 * If the monitor stack depth is nonzero, there must be a "catch all" handler for this
2713 * instruction. This does apply to monitor-exit because of async exception handling.
jeffhaobdb76512011-09-07 11:43:16 -07002714 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002715 if (work_line_->MonitorStackDepth() > 0 && !within_catch_all) {
jeffhaobdb76512011-09-07 11:43:16 -07002716 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002717 * The state in work_line reflects the post-execution state. If the current instruction is a
2718 * monitor-enter and the monitor stack was empty, we don't need a catch-all (if it throws,
jeffhaobdb76512011-09-07 11:43:16 -07002719 * it will do so before grabbing the lock).
2720 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002721 if (inst->Opcode() != Instruction::MONITOR_ENTER || work_line_->MonitorStackDepth() != 1) {
jeffhaod5347e02012-03-22 17:25:05 -07002722 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
Ian Rogersd81871c2011-10-03 13:57:23 -07002723 << "expected to be within a catch-all for an instruction where a monitor is held";
jeffhaobdb76512011-09-07 11:43:16 -07002724 return false;
2725 }
2726 }
2727 }
2728
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002729 /* Handle "continue". Tag the next consecutive instruction.
2730 * Note: Keep the code handling "continue" case below the "branch" and "switch" cases,
2731 * because it changes work_line_ when performing peephole optimization
2732 * and this change should not be used in those cases.
2733 */
Ian Rogers6d376ae2013-07-23 15:12:40 -07002734 if ((opcode_flags & Instruction::kContinue) != 0) {
2735 uint32_t next_insn_idx = work_insn_idx_ + CurrentInsnFlags()->GetLengthInCodeUnits();
2736 if (next_insn_idx >= code_item_->insns_size_in_code_units_) {
2737 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Execution can walk off end of code area";
2738 return false;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002739 }
Ian Rogers6d376ae2013-07-23 15:12:40 -07002740 // The only way to get to a move-exception instruction is to get thrown there. Make sure the
2741 // next instruction isn't one.
2742 if (!CheckNotMoveException(code_item_->insns_, next_insn_idx)) {
2743 return false;
2744 }
2745 if (NULL != fallthrough_line.get()) {
2746 // Make workline consistent with fallthrough computed from peephole optimization.
2747 work_line_->CopyFromLine(fallthrough_line.get());
2748 }
Ian Rogersb8c78592013-07-25 23:52:52 +00002749 if (insn_flags_[next_insn_idx].IsReturn()) {
2750 // For returns we only care about the operand to the return, all other registers are dead.
2751 const Instruction* ret_inst = Instruction::At(code_item_->insns_ + next_insn_idx);
2752 Instruction::Code opcode = ret_inst->Opcode();
2753 if ((opcode == Instruction::RETURN_VOID) || (opcode == Instruction::RETURN_VOID_BARRIER)) {
2754 work_line_->MarkAllRegistersAsConflicts();
2755 } else {
2756 if (opcode == Instruction::RETURN_WIDE) {
2757 work_line_->MarkAllRegistersAsConflictsExceptWide(ret_inst->VRegA_11x());
2758 } else {
2759 work_line_->MarkAllRegistersAsConflictsExcept(ret_inst->VRegA_11x());
2760 }
2761 }
2762 }
Ian Rogers6d376ae2013-07-23 15:12:40 -07002763 RegisterLine* next_line = reg_table_.GetLine(next_insn_idx);
2764 if (next_line != NULL) {
2765 // Merge registers into what we have for the next instruction,
2766 // and set the "changed" flag if needed.
2767 if (!UpdateRegisters(next_insn_idx, work_line_.get())) {
2768 return false;
2769 }
2770 } else {
2771 /*
2772 * We're not recording register data for the next instruction, so we don't know what the
2773 * prior state was. We have to assume that something has changed and re-evaluate it.
2774 */
2775 insn_flags_[next_insn_idx].SetChanged();
2776 }
2777 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002778
jeffhaod1f0fde2011-09-08 17:25:33 -07002779 /* If we're returning from the method, make sure monitor stack is empty. */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002780 if ((opcode_flags & Instruction::kReturn) != 0) {
Elliott Hughesb25c3f62012-03-26 16:35:06 -07002781 if (!work_line_->VerifyMonitorStackEmpty()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002782 return false;
2783 }
jeffhaobdb76512011-09-07 11:43:16 -07002784 }
2785
2786 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002787 * Update start_guess. Advance to the next instruction of that's
2788 * possible, otherwise use the branch target if one was found. If
jeffhaobdb76512011-09-07 11:43:16 -07002789 * neither of those exists we're in a return or throw; leave start_guess
2790 * alone and let the caller sort it out.
2791 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002792 if ((opcode_flags & Instruction::kContinue) != 0) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002793 *start_guess = work_insn_idx_ + insn_flags_[work_insn_idx_].GetLengthInCodeUnits();
Elliott Hughesadb8c672012-03-06 16:49:32 -08002794 } else if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002795 /* we're still okay if branch_target is zero */
Ian Rogersd81871c2011-10-03 13:57:23 -07002796 *start_guess = work_insn_idx_ + branch_target;
jeffhaobdb76512011-09-07 11:43:16 -07002797 }
2798
Ian Rogersd81871c2011-10-03 13:57:23 -07002799 DCHECK_LT(*start_guess, code_item_->insns_size_in_code_units_);
2800 DCHECK(insn_flags_[*start_guess].IsOpcode());
jeffhaobdb76512011-09-07 11:43:16 -07002801
2802 return true;
Brian Carlstrom7934ac22013-07-26 10:54:15 -07002803} // NOLINT(readability/fn_size)
jeffhaobdb76512011-09-07 11:43:16 -07002804
Ian Rogers776ac1f2012-04-13 23:36:36 -07002805const RegType& MethodVerifier::ResolveClassAndCheckAccess(uint32_t class_idx) {
Ian Rogers0571d352011-11-03 19:51:38 -07002806 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002807 const RegType& referrer = GetDeclaringClass();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002808 mirror::Class* klass = dex_cache_->GetResolvedType(class_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002809 const RegType& result =
Ian Rogers04f94f42013-06-10 15:09:26 -07002810 klass != NULL ? reg_types_.FromClass(descriptor, klass,
2811 klass->CannotBeAssignedFromOtherTypes())
Ian Rogersb4903572012-10-11 11:52:56 -07002812 : reg_types_.FromDescriptor(class_loader_, descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002813 if (result.IsConflict()) {
2814 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "accessing broken descriptor '" << descriptor
2815 << "' in " << referrer;
2816 return result;
2817 }
Ian Rogerse1758fe2012-04-19 11:31:15 -07002818 if (klass == NULL && !result.IsUnresolvedTypes()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002819 dex_cache_->SetResolvedType(class_idx, result.GetClass());
Ian Rogerse1758fe2012-04-19 11:31:15 -07002820 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002821 // Check if access is allowed. Unresolved types use xxxWithAccessCheck to
Ian Rogers28ad40d2011-10-27 15:19:26 -07002822 // check at runtime if access is allowed and so pass here.
Ian Rogersad0b3a32012-04-16 14:50:24 -07002823 if (!result.IsUnresolvedTypes() && !referrer.IsUnresolvedTypes() && !referrer.CanAccess(result)) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002824 Fail(VERIFY_ERROR_ACCESS_CLASS) << "illegal class access: '"
Ian Rogersad0b3a32012-04-16 14:50:24 -07002825 << referrer << "' -> '" << result << "'";
Ian Rogers28ad40d2011-10-27 15:19:26 -07002826 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002827 return result;
Ian Rogersd81871c2011-10-03 13:57:23 -07002828}
2829
Ian Rogers776ac1f2012-04-13 23:36:36 -07002830const RegType& MethodVerifier::GetCaughtExceptionType() {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002831 const RegType* common_super = NULL;
Ian Rogersd81871c2011-10-03 13:57:23 -07002832 if (code_item_->tries_size_ != 0) {
Ian Rogers0571d352011-11-03 19:51:38 -07002833 const byte* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
Ian Rogersd81871c2011-10-03 13:57:23 -07002834 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
2835 for (uint32_t i = 0; i < handlers_size; i++) {
Ian Rogers0571d352011-11-03 19:51:38 -07002836 CatchHandlerIterator iterator(handlers_ptr);
2837 for (; iterator.HasNext(); iterator.Next()) {
2838 if (iterator.GetHandlerAddress() == (uint32_t) work_insn_idx_) {
2839 if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) {
Ian Rogersb4903572012-10-11 11:52:56 -07002840 common_super = &reg_types_.JavaLangThrowable(false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002841 } else {
Ian Rogers0571d352011-11-03 19:51:38 -07002842 const RegType& exception = ResolveClassAndCheckAccess(iterator.GetHandlerTypeIndex());
Ian Rogersc4762272012-02-01 15:55:55 -08002843 if (common_super == NULL) {
2844 // Unconditionally assign for the first handler. We don't assert this is a Throwable
2845 // as that is caught at runtime
2846 common_super = &exception;
Ian Rogersb4903572012-10-11 11:52:56 -07002847 } else if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(exception)) {
Ian Rogersc4762272012-02-01 15:55:55 -08002848 // We don't know enough about the type and the common path merge will result in
2849 // Conflict. Fail here knowing the correct thing can be done at runtime.
jeffhaod5347e02012-03-22 17:25:05 -07002850 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unexpected non-exception class " << exception;
Ian Rogersad0b3a32012-04-16 14:50:24 -07002851 return reg_types_.Conflict();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002852 } else if (common_super->Equals(exception)) {
Ian Rogersc4762272012-02-01 15:55:55 -08002853 // odd case, but nothing to do
Ian Rogersd81871c2011-10-03 13:57:23 -07002854 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002855 common_super = &common_super->Merge(exception, &reg_types_);
Ian Rogersb4903572012-10-11 11:52:56 -07002856 CHECK(reg_types_.JavaLangThrowable(false).IsAssignableFrom(*common_super));
Ian Rogersd81871c2011-10-03 13:57:23 -07002857 }
2858 }
2859 }
2860 }
Ian Rogers0571d352011-11-03 19:51:38 -07002861 handlers_ptr = iterator.EndDataPointer();
Ian Rogersd81871c2011-10-03 13:57:23 -07002862 }
2863 }
2864 if (common_super == NULL) {
2865 /* no catch blocks, or no catches with classes we can find */
jeffhaod5347e02012-03-22 17:25:05 -07002866 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unable to find exception handler";
Ian Rogersad0b3a32012-04-16 14:50:24 -07002867 return reg_types_.Conflict();
Ian Rogersd81871c2011-10-03 13:57:23 -07002868 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002869 return *common_super;
Ian Rogersd81871c2011-10-03 13:57:23 -07002870}
2871
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002872mirror::AbstractMethod* MethodVerifier::ResolveMethodAndCheckAccess(uint32_t dex_method_idx,
2873 MethodType method_type) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002874 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx);
Ian Rogers90040192011-12-16 08:54:29 -08002875 const RegType& klass_type = ResolveClassAndCheckAccess(method_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002876 if (klass_type.IsConflict()) {
2877 std::string append(" in attempt to access method ");
2878 append += dex_file_->GetMethodName(method_id);
2879 AppendToLastFailMessage(append);
Ian Rogers90040192011-12-16 08:54:29 -08002880 return NULL;
2881 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002882 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers90040192011-12-16 08:54:29 -08002883 return NULL; // Can't resolve Class so no more to do here
2884 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002885 mirror::Class* klass = klass_type.GetClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002886 const RegType& referrer = GetDeclaringClass();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002887 mirror::AbstractMethod* res_method = dex_cache_->GetResolvedMethod(dex_method_idx);
Ian Rogersd81871c2011-10-03 13:57:23 -07002888 if (res_method == NULL) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07002889 const char* name = dex_file_->GetMethodName(method_id);
Ian Rogers0571d352011-11-03 19:51:38 -07002890 std::string signature(dex_file_->CreateMethodSignature(method_id.proto_idx_, NULL));
jeffhao8cd6dda2012-02-22 10:15:34 -08002891
2892 if (method_type == METHOD_DIRECT || method_type == METHOD_STATIC) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002893 res_method = klass->FindDirectMethod(name, signature);
jeffhao8cd6dda2012-02-22 10:15:34 -08002894 } else if (method_type == METHOD_INTERFACE) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002895 res_method = klass->FindInterfaceMethod(name, signature);
2896 } else {
2897 res_method = klass->FindVirtualMethod(name, signature);
2898 }
2899 if (res_method != NULL) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002900 dex_cache_->SetResolvedMethod(dex_method_idx, res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07002901 } else {
jeffhao8cd6dda2012-02-22 10:15:34 -08002902 // If a virtual or interface method wasn't found with the expected type, look in
2903 // the direct methods. This can happen when the wrong invoke type is used or when
2904 // a class has changed, and will be flagged as an error in later checks.
2905 if (method_type == METHOD_INTERFACE || method_type == METHOD_VIRTUAL) {
2906 res_method = klass->FindDirectMethod(name, signature);
2907 }
2908 if (res_method == NULL) {
2909 Fail(VERIFY_ERROR_NO_METHOD) << "couldn't find method "
2910 << PrettyDescriptor(klass) << "." << name
2911 << " " << signature;
2912 return NULL;
2913 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002914 }
2915 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002916 // Make sure calls to constructors are "direct". There are additional restrictions but we don't
2917 // enforce them here.
2918 if (res_method->IsConstructor() && method_type != METHOD_DIRECT) {
jeffhaod5347e02012-03-22 17:25:05 -07002919 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting non-direct call to constructor "
2920 << PrettyMethod(res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07002921 return NULL;
2922 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002923 // Disallow any calls to class initializers.
2924 if (MethodHelper(res_method).IsClassInitializer()) {
jeffhaod5347e02012-03-22 17:25:05 -07002925 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting call to class initializer "
2926 << PrettyMethod(res_method);
jeffhao8cd6dda2012-02-22 10:15:34 -08002927 return NULL;
2928 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002929 // Check if access is allowed.
Ian Rogersad0b3a32012-04-16 14:50:24 -07002930 if (!referrer.CanAccessMember(res_method->GetDeclaringClass(), res_method->GetAccessFlags())) {
jeffhao8cd6dda2012-02-22 10:15:34 -08002931 Fail(VERIFY_ERROR_ACCESS_METHOD) << "illegal method access (call " << PrettyMethod(res_method)
Ian Rogersad0b3a32012-04-16 14:50:24 -07002932 << " from " << referrer << ")";
jeffhaob57e9522012-04-26 18:08:21 -07002933 return res_method;
jeffhao8cd6dda2012-02-22 10:15:34 -08002934 }
jeffhaode0d9c92012-02-27 13:58:13 -08002935 // Check that invoke-virtual and invoke-super are not used on private methods of the same class.
2936 if (res_method->IsPrivate() && method_type == METHOD_VIRTUAL) {
jeffhaod5347e02012-03-22 17:25:05 -07002937 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invoke-super/virtual can't be used on private method "
2938 << PrettyMethod(res_method);
jeffhaode0d9c92012-02-27 13:58:13 -08002939 return NULL;
2940 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002941 // Check that interface methods match interface classes.
2942 if (klass->IsInterface() && method_type != METHOD_INTERFACE) {
2943 Fail(VERIFY_ERROR_CLASS_CHANGE) << "non-interface method " << PrettyMethod(res_method)
2944 << " is in an interface class " << PrettyClass(klass);
2945 return NULL;
2946 } else if (!klass->IsInterface() && method_type == METHOD_INTERFACE) {
2947 Fail(VERIFY_ERROR_CLASS_CHANGE) << "interface method " << PrettyMethod(res_method)
2948 << " is in a non-interface class " << PrettyClass(klass);
2949 return NULL;
2950 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002951 // See if the method type implied by the invoke instruction matches the access flags for the
2952 // target method.
2953 if ((method_type == METHOD_DIRECT && !res_method->IsDirect()) ||
2954 (method_type == METHOD_STATIC && !res_method->IsStatic()) ||
2955 ((method_type == METHOD_VIRTUAL || method_type == METHOD_INTERFACE) && res_method->IsDirect())
2956 ) {
Ian Rogers2fc14272012-08-30 10:56:57 -07002957 Fail(VERIFY_ERROR_CLASS_CHANGE) << "invoke type (" << method_type << ") does not match method "
2958 " type of " << PrettyMethod(res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07002959 return NULL;
2960 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002961 return res_method;
2962}
2963
Sebastien Hertz5243e912013-05-21 10:55:07 +02002964mirror::AbstractMethod* MethodVerifier::VerifyInvocationArgs(const Instruction* inst,
2965 MethodType method_type,
2966 bool is_range,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002967 bool is_super) {
jeffhao8cd6dda2012-02-22 10:15:34 -08002968 // Resolve the method. This could be an abstract or concrete method depending on what sort of call
2969 // we're making.
Sebastien Hertz5243e912013-05-21 10:55:07 +02002970 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
2971 mirror::AbstractMethod* res_method = ResolveMethodAndCheckAccess(method_idx, method_type);
jeffhao8cd6dda2012-02-22 10:15:34 -08002972 if (res_method == NULL) { // error or class is unresolved
2973 return NULL;
2974 }
2975
Ian Rogersd81871c2011-10-03 13:57:23 -07002976 // If we're using invoke-super(method), make sure that the executing method's class' superclass
2977 // has a vtable entry for the target method.
2978 if (is_super) {
2979 DCHECK(method_type == METHOD_VIRTUAL);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002980 const RegType& super = GetDeclaringClass().GetSuperClass(&reg_types_);
Ian Rogers529781d2012-07-23 17:24:29 -07002981 if (super.IsUnresolvedTypes()) {
jeffhao4d8df822012-04-24 17:09:36 -07002982 Fail(VERIFY_ERROR_NO_METHOD) << "unknown super class in invoke-super from "
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002983 << PrettyMethod(dex_method_idx_, *dex_file_)
jeffhao4d8df822012-04-24 17:09:36 -07002984 << " to super " << PrettyMethod(res_method);
2985 return NULL;
2986 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002987 mirror::Class* super_klass = super.GetClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002988 if (res_method->GetMethodIndex() >= super_klass->GetVTable()->GetLength()) {
jeffhao4d8df822012-04-24 17:09:36 -07002989 MethodHelper mh(res_method);
2990 Fail(VERIFY_ERROR_NO_METHOD) << "invalid invoke-super from "
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002991 << PrettyMethod(dex_method_idx_, *dex_file_)
jeffhao4d8df822012-04-24 17:09:36 -07002992 << " to super " << super
2993 << "." << mh.GetName()
2994 << mh.GetSignature();
Ian Rogersd81871c2011-10-03 13:57:23 -07002995 return NULL;
2996 }
2997 }
2998 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002999 // match the call to the signature. Also, we might be calling through an abstract method
Ian Rogersd81871c2011-10-03 13:57:23 -07003000 // definition (which doesn't have register count values).
Sebastien Hertz5243e912013-05-21 10:55:07 +02003001 const size_t expected_args = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
Ian Rogersd81871c2011-10-03 13:57:23 -07003002 /* caught by static verifier */
3003 DCHECK(is_range || expected_args <= 5);
3004 if (expected_args > code_item_->outs_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07003005 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
Ian Rogersd81871c2011-10-03 13:57:23 -07003006 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
3007 return NULL;
3008 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003009
jeffhaobdb76512011-09-07 11:43:16 -07003010 /*
Ian Rogersad0b3a32012-04-16 14:50:24 -07003011 * Check the "this" argument, which must be an instance of the class that declared the method.
3012 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
3013 * rigorous check here (which is okay since we have to do it at runtime).
jeffhaobdb76512011-09-07 11:43:16 -07003014 */
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003015 size_t actual_args = 0;
Ian Rogersd81871c2011-10-03 13:57:23 -07003016 if (!res_method->IsStatic()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003017 const RegType& actual_arg_type = work_line_->GetInvocationThis(inst, is_range);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003018 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
Ian Rogersd81871c2011-10-03 13:57:23 -07003019 return NULL;
3020 }
3021 if (actual_arg_type.IsUninitializedReference() && !res_method->IsConstructor()) {
jeffhaod5347e02012-03-22 17:25:05 -07003022 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
Ian Rogersd81871c2011-10-03 13:57:23 -07003023 return NULL;
3024 }
3025 if (method_type != METHOD_INTERFACE && !actual_arg_type.IsZero()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003026 mirror::Class* klass = res_method->GetDeclaringClass();
Ian Rogers04f94f42013-06-10 15:09:26 -07003027 const RegType& res_method_class =
3028 reg_types_.FromClass(ClassHelper(klass).GetDescriptor(), klass,
3029 klass->CannotBeAssignedFromOtherTypes());
Ian Rogers9074b992011-10-26 17:41:55 -07003030 if (!res_method_class.IsAssignableFrom(actual_arg_type)) {
jeffhaod5347e02012-03-22 17:25:05 -07003031 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003032 << "' not instance of '" << res_method_class << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07003033 return NULL;
3034 }
3035 }
3036 actual_args++;
3037 }
3038 /*
3039 * Process the target method's signature. This signature may or may not
3040 * have been verified, so we can't assume it's properly formed.
3041 */
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003042 MethodHelper mh(res_method);
3043 const DexFile::TypeList* params = mh.GetParameterTypeList();
3044 size_t params_size = params == NULL ? 0 : params->Size();
Sebastien Hertz5243e912013-05-21 10:55:07 +02003045 uint32_t arg[5];
3046 if (!is_range) {
3047 inst->GetArgs(arg);
3048 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003049 for (size_t param_index = 0; param_index < params_size; param_index++) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003050 if (actual_args >= expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07003051 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invalid call to '" << PrettyMethod(res_method)
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003052 << "'. Expected " << expected_args << " arguments, processing argument " << actual_args
3053 << " (where longs/doubles count twice).";
Ian Rogersd81871c2011-10-03 13:57:23 -07003054 return NULL;
3055 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003056 const char* descriptor =
3057 mh.GetTypeDescriptorFromTypeIdx(params->GetTypeItem(param_index).type_idx_);
3058 if (descriptor == NULL) {
jeffhaod5347e02012-03-22 17:25:05 -07003059 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003060 << " missing signature component";
3061 return NULL;
Ian Rogersd81871c2011-10-03 13:57:23 -07003062 }
Ian Rogersb4903572012-10-11 11:52:56 -07003063 const RegType& reg_type = reg_types_.FromDescriptor(class_loader_, descriptor, false);
Sebastien Hertz5243e912013-05-21 10:55:07 +02003064 uint32_t get_reg = is_range ? inst->VRegC_3rc() + actual_args : arg[actual_args];
Ian Rogers84fa0742011-10-25 18:13:30 -07003065 if (!work_line_->VerifyRegisterType(get_reg, reg_type)) {
jeffhaob57e9522012-04-26 18:08:21 -07003066 return res_method;
Ian Rogersd81871c2011-10-03 13:57:23 -07003067 }
3068 actual_args = reg_type.IsLongOrDoubleTypes() ? actual_args + 2 : actual_args + 1;
3069 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003070 if (actual_args != expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07003071 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003072 << " expected " << expected_args << " arguments, found " << actual_args;
Ian Rogersd81871c2011-10-03 13:57:23 -07003073 return NULL;
3074 } else {
3075 return res_method;
3076 }
3077}
3078
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003079mirror::AbstractMethod* MethodVerifier::GetQuickInvokedMethod(const Instruction* inst,
3080 RegisterLine* reg_line,
3081 bool is_range) {
3082 DCHECK(inst->Opcode() == Instruction::INVOKE_VIRTUAL_QUICK ||
3083 inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
3084 const RegType& actual_arg_type = reg_line->GetInvocationThis(inst, is_range);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003085 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
3086 return NULL;
3087 }
3088 mirror::Class* this_class = NULL;
3089 if (!actual_arg_type.IsUnresolvedTypes()) {
3090 this_class = actual_arg_type.GetClass();
3091 } else {
3092 const std::string& descriptor(actual_arg_type.GetDescriptor());
3093 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
3094 this_class = class_linker->FindClass(descriptor.c_str(), class_loader_);
3095 if (this_class == NULL) {
3096 Thread::Current()->ClearException();
3097 // Look for a system class
3098 this_class = class_linker->FindClass(descriptor.c_str(), NULL);
3099 }
3100 }
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003101 if (this_class == NULL) {
3102 return NULL;
3103 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003104 mirror::ObjectArray<mirror::AbstractMethod>* vtable = this_class->GetVTable();
3105 CHECK(vtable != NULL);
3106 uint16_t vtable_index = is_range ? inst->VRegB_3rc() : inst->VRegB_35c();
3107 CHECK(vtable_index < vtable->GetLength());
3108 mirror::AbstractMethod* res_method = vtable->Get(vtable_index);
3109 CHECK(!Thread::Current()->IsExceptionPending());
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003110 return res_method;
3111}
3112
3113mirror::AbstractMethod* MethodVerifier::VerifyInvokeVirtualQuickArgs(const Instruction* inst,
3114 bool is_range) {
3115 DCHECK(Runtime::Current()->IsStarted());
3116 mirror::AbstractMethod* res_method = GetQuickInvokedMethod(inst, work_line_.get(),
3117 is_range);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003118 if (res_method == NULL) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003119 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer method from " << inst->Name();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003120 return NULL;
3121 }
3122 CHECK(!res_method->IsDirect() && !res_method->IsStatic());
3123
3124 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
3125 // match the call to the signature. Also, we might be calling through an abstract method
3126 // definition (which doesn't have register count values).
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003127 const RegType& actual_arg_type = work_line_->GetInvocationThis(inst, is_range);
3128 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
3129 return NULL;
3130 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003131 const size_t expected_args = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
3132 /* caught by static verifier */
3133 DCHECK(is_range || expected_args <= 5);
3134 if (expected_args > code_item_->outs_size_) {
3135 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
3136 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
3137 return NULL;
3138 }
3139
3140 /*
3141 * Check the "this" argument, which must be an instance of the class that declared the method.
3142 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
3143 * rigorous check here (which is okay since we have to do it at runtime).
3144 */
3145 if (actual_arg_type.IsUninitializedReference() && !res_method->IsConstructor()) {
3146 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
3147 return NULL;
3148 }
3149 if (!actual_arg_type.IsZero()) {
3150 mirror::Class* klass = res_method->GetDeclaringClass();
3151 const RegType& res_method_class =
3152 reg_types_.FromClass(ClassHelper(klass).GetDescriptor(), klass,
3153 klass->CannotBeAssignedFromOtherTypes());
3154 if (!res_method_class.IsAssignableFrom(actual_arg_type)) {
3155 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type
3156 << "' not instance of '" << res_method_class << "'";
3157 return NULL;
3158 }
3159 }
3160 /*
3161 * Process the target method's signature. This signature may or may not
3162 * have been verified, so we can't assume it's properly formed.
3163 */
3164 MethodHelper mh(res_method);
3165 const DexFile::TypeList* params = mh.GetParameterTypeList();
3166 size_t params_size = params == NULL ? 0 : params->Size();
3167 uint32_t arg[5];
3168 if (!is_range) {
3169 inst->GetArgs(arg);
3170 }
3171 size_t actual_args = 1;
3172 for (size_t param_index = 0; param_index < params_size; param_index++) {
3173 if (actual_args >= expected_args) {
3174 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invalid call to '" << PrettyMethod(res_method)
Brian Carlstrom93c33962013-07-26 10:37:43 -07003175 << "'. Expected " << expected_args
3176 << " arguments, processing argument " << actual_args
3177 << " (where longs/doubles count twice).";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003178 return NULL;
3179 }
3180 const char* descriptor =
3181 mh.GetTypeDescriptorFromTypeIdx(params->GetTypeItem(param_index).type_idx_);
3182 if (descriptor == NULL) {
3183 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003184 << " missing signature component";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003185 return NULL;
3186 }
3187 const RegType& reg_type = reg_types_.FromDescriptor(class_loader_, descriptor, false);
3188 uint32_t get_reg = is_range ? inst->VRegC_3rc() + actual_args : arg[actual_args];
3189 if (!work_line_->VerifyRegisterType(get_reg, reg_type)) {
3190 return res_method;
3191 }
3192 actual_args = reg_type.IsLongOrDoubleTypes() ? actual_args + 2 : actual_args + 1;
3193 }
3194 if (actual_args != expected_args) {
3195 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
3196 << " expected " << expected_args << " arguments, found " << actual_args;
3197 return NULL;
3198 } else {
3199 return res_method;
3200 }
3201}
3202
Ian Rogers62342ec2013-06-11 10:26:37 -07003203void MethodVerifier::VerifyNewArray(const Instruction* inst, bool is_filled, bool is_range) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003204 uint32_t type_idx;
3205 if (!is_filled) {
3206 DCHECK_EQ(inst->Opcode(), Instruction::NEW_ARRAY);
3207 type_idx = inst->VRegC_22c();
3208 } else if (!is_range) {
3209 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY);
3210 type_idx = inst->VRegB_35c();
3211 } else {
3212 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY_RANGE);
3213 type_idx = inst->VRegB_3rc();
3214 }
3215 const RegType& res_type = ResolveClassAndCheckAccess(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003216 if (res_type.IsConflict()) { // bad class
3217 DCHECK_NE(failures_.size(), 0U);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003218 } else {
3219 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
3220 if (!res_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003221 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "new-array on non-array class " << res_type;
Ian Rogers0c4a5062012-02-03 15:18:59 -08003222 } else if (!is_filled) {
3223 /* make sure "size" register is valid type */
Sebastien Hertz5243e912013-05-21 10:55:07 +02003224 work_line_->VerifyRegisterType(inst->VRegB_22c(), reg_types_.Integer());
Ian Rogers0c4a5062012-02-03 15:18:59 -08003225 /* set register type to array class */
Ian Rogers62342ec2013-06-11 10:26:37 -07003226 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
3227 work_line_->SetRegisterType(inst->VRegA_22c(), precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003228 } else {
3229 // Verify each register. If "arg_count" is bad, VerifyRegisterType() will run off the end of
3230 // the list and fail. It's legal, if silly, for arg_count to be zero.
Ian Rogersad0b3a32012-04-16 14:50:24 -07003231 const RegType& expected_type = reg_types_.GetComponentType(res_type, class_loader_);
Sebastien Hertz5243e912013-05-21 10:55:07 +02003232 uint32_t arg_count = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
3233 uint32_t arg[5];
3234 if (!is_range) {
3235 inst->GetArgs(arg);
3236 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08003237 for (size_t ui = 0; ui < arg_count; ui++) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003238 uint32_t get_reg = is_range ? inst->VRegC_3rc() + ui : arg[ui];
Ian Rogers0c4a5062012-02-03 15:18:59 -08003239 if (!work_line_->VerifyRegisterType(get_reg, expected_type)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003240 work_line_->SetResultRegisterType(reg_types_.Conflict());
Ian Rogers0c4a5062012-02-03 15:18:59 -08003241 return;
3242 }
3243 }
3244 // filled-array result goes into "result" register
Ian Rogers62342ec2013-06-11 10:26:37 -07003245 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
3246 work_line_->SetResultRegisterType(precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003247 }
3248 }
3249}
3250
Sebastien Hertz5243e912013-05-21 10:55:07 +02003251void MethodVerifier::VerifyAGet(const Instruction* inst,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003252 const RegType& insn_type, bool is_primitive) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003253 const RegType& index_type = work_line_->GetRegisterType(inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07003254 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003255 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07003256 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003257 const RegType& array_type = work_line_->GetRegisterType(inst->VRegB_23x());
Ian Rogers89310de2012-02-01 13:47:30 -08003258 if (array_type.IsZero()) {
3259 // Null array class; this code path will fail at runtime. Infer a merge-able type from the
3260 // instruction type. TODO: have a proper notion of bottom here.
3261 if (!is_primitive || insn_type.IsCategory1Types()) {
3262 // Reference or category 1
Sebastien Hertz5243e912013-05-21 10:55:07 +02003263 work_line_->SetRegisterType(inst->VRegA_23x(), reg_types_.Zero());
Ian Rogersd81871c2011-10-03 13:57:23 -07003264 } else {
Ian Rogers89310de2012-02-01 13:47:30 -08003265 // Category 2
Sebastien Hertz5243e912013-05-21 10:55:07 +02003266 work_line_->SetRegisterTypeWide(inst->VRegA_23x(), reg_types_.FromCat2ConstLo(0, false),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003267 reg_types_.FromCat2ConstHi(0, false));
Ian Rogers89310de2012-02-01 13:47:30 -08003268 }
jeffhaofc3144e2012-02-01 17:21:15 -08003269 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003270 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aget";
Ian Rogers89310de2012-02-01 13:47:30 -08003271 } else {
3272 /* verify the class */
Ian Rogersad0b3a32012-04-16 14:50:24 -07003273 const RegType& component_type = reg_types_.GetComponentType(array_type, class_loader_);
jeffhaofc3144e2012-02-01 17:21:15 -08003274 if (!component_type.IsReferenceTypes() && !is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07003275 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08003276 << " source for aget-object";
3277 } else if (component_type.IsNonZeroReferenceTypes() && is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07003278 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08003279 << " source for category 1 aget";
3280 } else if (is_primitive && !insn_type.Equals(component_type) &&
3281 !((insn_type.IsInteger() && component_type.IsFloat()) ||
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003282 (insn_type.IsLong() && component_type.IsDouble()))) {
3283 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array type " << array_type
3284 << " incompatible with aget of type " << insn_type;
Ian Rogers89310de2012-02-01 13:47:30 -08003285 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003286 // Use knowledge of the field type which is stronger than the type inferred from the
3287 // instruction, which can't differentiate object types and ints from floats, longs from
3288 // doubles.
3289 if (!component_type.IsLowHalf()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003290 work_line_->SetRegisterType(inst->VRegA_23x(), component_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003291 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003292 work_line_->SetRegisterTypeWide(inst->VRegA_23x(), component_type,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003293 component_type.HighHalf(&reg_types_));
3294 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003295 }
3296 }
3297 }
3298}
3299
Sebastien Hertz5243e912013-05-21 10:55:07 +02003300void MethodVerifier::VerifyAPut(const Instruction* inst,
Ian Rogersd81871c2011-10-03 13:57:23 -07003301 const RegType& insn_type, bool is_primitive) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003302 const RegType& index_type = work_line_->GetRegisterType(inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07003303 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003304 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07003305 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003306 const RegType& array_type = work_line_->GetRegisterType(inst->VRegB_23x());
Ian Rogers89310de2012-02-01 13:47:30 -08003307 if (array_type.IsZero()) {
3308 // Null array type; this code path will fail at runtime. Infer a merge-able type from the
3309 // instruction type.
jeffhaofc3144e2012-02-01 17:21:15 -08003310 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003311 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aput";
Ian Rogers89310de2012-02-01 13:47:30 -08003312 } else {
3313 /* verify the class */
Ian Rogersad0b3a32012-04-16 14:50:24 -07003314 const RegType& component_type = reg_types_.GetComponentType(array_type, class_loader_);
jeffhaofc3144e2012-02-01 17:21:15 -08003315 if (!component_type.IsReferenceTypes() && !is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07003316 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08003317 << " source for aput-object";
3318 } else if (component_type.IsNonZeroReferenceTypes() && is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07003319 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08003320 << " source for category 1 aput";
3321 } else if (is_primitive && !insn_type.Equals(component_type) &&
3322 !((insn_type.IsInteger() && component_type.IsFloat()) ||
3323 (insn_type.IsLong() && component_type.IsDouble()))) {
jeffhaod5347e02012-03-22 17:25:05 -07003324 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08003325 << " incompatible with aput of type " << insn_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07003326 } else {
Ian Rogers89310de2012-02-01 13:47:30 -08003327 // The instruction agrees with the type of array, confirm the value to be stored does too
3328 // Note: we use the instruction type (rather than the component type) for aput-object as
3329 // incompatible classes will be caught at runtime as an array store exception
Brian Carlstrom93c33962013-07-26 10:37:43 -07003330 work_line_->VerifyRegisterType(inst->VRegA_23x(),
3331 is_primitive ? component_type : insn_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07003332 }
3333 }
3334 }
3335}
3336
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003337mirror::Field* MethodVerifier::GetStaticField(int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08003338 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3339 // Check access to class
3340 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Brian Carlstrom7934ac22013-07-26 10:54:15 -07003341 if (klass_type.IsConflict()) { // bad class
Ian Rogersad0b3a32012-04-16 14:50:24 -07003342 AppendToLastFailMessage(StringPrintf(" in attempt to access static field %d (%s) in %s",
3343 field_idx, dex_file_->GetFieldName(field_id),
3344 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers90040192011-12-16 08:54:29 -08003345 return NULL;
3346 }
Elliott Hughesb25c3f62012-03-26 16:35:06 -07003347 if (klass_type.IsUnresolvedTypes()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003348 return NULL; // Can't resolve Class so no more to do here, will do checking at runtime.
Ian Rogers90040192011-12-16 08:54:29 -08003349 }
Brian Carlstrom93c33962013-07-26 10:37:43 -07003350 mirror::Field* field = Runtime::Current()->GetClassLinker()->ResolveFieldJLS(*dex_file_,
3351 field_idx,
3352 dex_cache_,
3353 class_loader_);
Ian Rogersd81871c2011-10-03 13:57:23 -07003354 if (field == NULL) {
Ian Rogers637c65b2013-05-31 11:46:00 -07003355 LOG(INFO) << "Unable to resolve static field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07003356 << dex_file_->GetFieldName(field_id) << ") in "
3357 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07003358 DCHECK(Thread::Current()->IsExceptionPending());
3359 Thread::Current()->ClearException();
3360 return NULL;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003361 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
3362 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003363 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access static field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07003364 << " from " << GetDeclaringClass();
Ian Rogersd81871c2011-10-03 13:57:23 -07003365 return NULL;
3366 } else if (!field->IsStatic()) {
3367 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field) << " to be static";
3368 return NULL;
3369 } else {
3370 return field;
3371 }
3372}
3373
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003374mirror::Field* MethodVerifier::GetInstanceField(const RegType& obj_type, int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08003375 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3376 // Check access to class
3377 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003378 if (klass_type.IsConflict()) {
3379 AppendToLastFailMessage(StringPrintf(" in attempt to access instance field %d (%s) in %s",
3380 field_idx, dex_file_->GetFieldName(field_id),
3381 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers90040192011-12-16 08:54:29 -08003382 return NULL;
3383 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003384 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers90040192011-12-16 08:54:29 -08003385 return NULL; // Can't resolve Class so no more to do here
3386 }
Brian Carlstrom93c33962013-07-26 10:37:43 -07003387 mirror::Field* field = Runtime::Current()->GetClassLinker()->ResolveFieldJLS(*dex_file_,
3388 field_idx,
3389 dex_cache_,
3390 class_loader_);
Ian Rogersd81871c2011-10-03 13:57:23 -07003391 if (field == NULL) {
Ian Rogers637c65b2013-05-31 11:46:00 -07003392 LOG(INFO) << "Unable to resolve instance field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07003393 << dex_file_->GetFieldName(field_id) << ") in "
3394 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07003395 DCHECK(Thread::Current()->IsExceptionPending());
3396 Thread::Current()->ClearException();
3397 return NULL;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003398 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
3399 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003400 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access instance field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07003401 << " from " << GetDeclaringClass();
Ian Rogersd81871c2011-10-03 13:57:23 -07003402 return NULL;
3403 } else if (field->IsStatic()) {
3404 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field)
3405 << " to not be static";
3406 return NULL;
3407 } else if (obj_type.IsZero()) {
3408 // Cannot infer and check type, however, access will cause null pointer exception
3409 return field;
Ian Rogerse1758fe2012-04-19 11:31:15 -07003410 } else {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003411 mirror::Class* klass = field->GetDeclaringClass();
Ian Rogers637c65b2013-05-31 11:46:00 -07003412 const RegType& field_klass =
3413 reg_types_.FromClass(dex_file_->GetFieldDeclaringClassDescriptor(field_id),
Ian Rogers04f94f42013-06-10 15:09:26 -07003414 klass, klass->CannotBeAssignedFromOtherTypes());
Ian Rogersad0b3a32012-04-16 14:50:24 -07003415 if (obj_type.IsUninitializedTypes() &&
3416 (!IsConstructor() || GetDeclaringClass().Equals(obj_type) ||
3417 !field_klass.Equals(GetDeclaringClass()))) {
3418 // Field accesses through uninitialized references are only allowable for constructors where
3419 // the field is declared in this class
3420 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "cannot access instance field " << PrettyField(field)
Brian Carlstrom93c33962013-07-26 10:37:43 -07003421 << " of a not fully initialized object within the context"
3422 << " of " << PrettyMethod(dex_method_idx_, *dex_file_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003423 return NULL;
3424 } else if (!field_klass.IsAssignableFrom(obj_type)) {
3425 // Trying to access C1.field1 using reference of type C2, which is neither C1 or a sub-class
3426 // of C1. For resolution to occur the declared class of the field must be compatible with
3427 // obj_type, we've discovered this wasn't so, so report the field didn't exist.
3428 Fail(VERIFY_ERROR_NO_FIELD) << "cannot access instance field " << PrettyField(field)
3429 << " from object of type " << obj_type;
3430 return NULL;
3431 } else {
3432 return field;
3433 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003434 }
3435}
3436
Sebastien Hertz5243e912013-05-21 10:55:07 +02003437void MethodVerifier::VerifyISGet(const Instruction* inst, const RegType& insn_type,
3438 bool is_primitive, bool is_static) {
3439 uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003440 mirror::Field* field;
Ian Rogersb94a27b2011-10-26 00:33:41 -07003441 if (is_static) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07003442 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003443 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003444 const RegType& object_type = work_line_->GetRegisterType(inst->VRegB_22c());
Ian Rogersf4028cc2011-11-02 14:56:39 -07003445 field = GetInstanceField(object_type, field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003446 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003447 const char* descriptor;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003448 mirror::ClassLoader* loader;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003449 if (field != NULL) {
3450 descriptor = FieldHelper(field).GetTypeDescriptor();
3451 loader = field->GetDeclaringClass()->GetClassLoader();
Ian Rogersf4028cc2011-11-02 14:56:39 -07003452 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003453 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3454 descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
3455 loader = class_loader_;
Ian Rogers0d604842012-04-16 14:50:24 -07003456 }
Ian Rogersb4903572012-10-11 11:52:56 -07003457 const RegType& field_type = reg_types_.FromDescriptor(loader, descriptor, false);
Sebastien Hertz5243e912013-05-21 10:55:07 +02003458 const uint32_t vregA = (is_static) ? inst->VRegA_21c() : inst->VRegA_22c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07003459 if (is_primitive) {
3460 if (field_type.Equals(insn_type) ||
3461 (field_type.IsFloat() && insn_type.IsIntegralTypes()) ||
3462 (field_type.IsDouble() && insn_type.IsLongTypes())) {
3463 // expected that read is of the correct primitive type or that int reads are reading
3464 // floats or long reads are reading doubles
3465 } else {
3466 // This is a global failure rather than a class change failure as the instructions and
3467 // the descriptors for the type should have been consistent within the same file at
3468 // compile time
3469 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
3470 << " to be of type '" << insn_type
3471 << "' but found type '" << field_type << "' in get";
Ian Rogersad0b3a32012-04-16 14:50:24 -07003472 return;
3473 }
3474 } else {
3475 if (!insn_type.IsAssignableFrom(field_type)) {
3476 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
3477 << " to be compatible with type '" << insn_type
3478 << "' but found type '" << field_type
3479 << "' in get-object";
Sebastien Hertz5243e912013-05-21 10:55:07 +02003480 work_line_->SetRegisterType(vregA, reg_types_.Conflict());
Ian Rogersad0b3a32012-04-16 14:50:24 -07003481 return;
3482 }
3483 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003484 if (!field_type.IsLowHalf()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003485 work_line_->SetRegisterType(vregA, field_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003486 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003487 work_line_->SetRegisterTypeWide(vregA, field_type, field_type.HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003488 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003489}
3490
Sebastien Hertz5243e912013-05-21 10:55:07 +02003491void MethodVerifier::VerifyISPut(const Instruction* inst, const RegType& insn_type,
3492 bool is_primitive, bool is_static) {
3493 uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003494 mirror::Field* field;
Ian Rogersb94a27b2011-10-26 00:33:41 -07003495 if (is_static) {
Ian Rogers55d249f2011-11-02 16:48:09 -07003496 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003497 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003498 const RegType& object_type = work_line_->GetRegisterType(inst->VRegB_22c());
Ian Rogers55d249f2011-11-02 16:48:09 -07003499 field = GetInstanceField(object_type, field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003500 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003501 const char* descriptor;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003502 mirror::ClassLoader* loader;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003503 if (field != NULL) {
3504 descriptor = FieldHelper(field).GetTypeDescriptor();
3505 loader = field->GetDeclaringClass()->GetClassLoader();
Ian Rogers55d249f2011-11-02 16:48:09 -07003506 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003507 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3508 descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
3509 loader = class_loader_;
3510 }
Ian Rogersb4903572012-10-11 11:52:56 -07003511 const RegType& field_type = reg_types_.FromDescriptor(loader, descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003512 if (field != NULL) {
3513 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
3514 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
3515 << " from other class " << GetDeclaringClass();
3516 return;
3517 }
3518 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02003519 const uint32_t vregA = (is_static) ? inst->VRegA_21c() : inst->VRegA_22c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07003520 if (is_primitive) {
3521 // Primitive field assignability rules are weaker than regular assignability rules
3522 bool instruction_compatible;
3523 bool value_compatible;
Sebastien Hertz5243e912013-05-21 10:55:07 +02003524 const RegType& value_type = work_line_->GetRegisterType(vregA);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003525 if (field_type.IsIntegralTypes()) {
3526 instruction_compatible = insn_type.IsIntegralTypes();
3527 value_compatible = value_type.IsIntegralTypes();
3528 } else if (field_type.IsFloat()) {
3529 instruction_compatible = insn_type.IsInteger(); // no [is]put-float, so expect [is]put-int
3530 value_compatible = value_type.IsFloatTypes();
3531 } else if (field_type.IsLong()) {
3532 instruction_compatible = insn_type.IsLong();
3533 value_compatible = value_type.IsLongTypes();
3534 } else if (field_type.IsDouble()) {
3535 instruction_compatible = insn_type.IsLong(); // no [is]put-double, so expect [is]put-long
3536 value_compatible = value_type.IsDoubleTypes();
Ian Rogers55d249f2011-11-02 16:48:09 -07003537 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003538 instruction_compatible = false; // reference field with primitive store
3539 value_compatible = false; // unused
Ian Rogersd81871c2011-10-03 13:57:23 -07003540 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003541 if (!instruction_compatible) {
3542 // This is a global failure rather than a class change failure as the instructions and
3543 // the descriptors for the type should have been consistent within the same file at
3544 // compile time
3545 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
3546 << " to be of type '" << insn_type
3547 << "' but found type '" << field_type
3548 << "' in put";
3549 return;
Ian Rogers55d249f2011-11-02 16:48:09 -07003550 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003551 if (!value_compatible) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003552 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
Ian Rogersad0b3a32012-04-16 14:50:24 -07003553 << " of type " << value_type
3554 << " but expected " << field_type
3555 << " for store to " << PrettyField(field) << " in put";
3556 return;
Ian Rogersd81871c2011-10-03 13:57:23 -07003557 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003558 } else {
3559 if (!insn_type.IsAssignableFrom(field_type)) {
3560 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
3561 << " to be compatible with type '" << insn_type
3562 << "' but found type '" << field_type
3563 << "' in put-object";
3564 return;
3565 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02003566 work_line_->VerifyRegisterType(vregA, field_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07003567 }
3568}
3569
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003570// Look for an instance field with this offset.
3571// TODO: we may speed up the search if offsets are sorted by doing a quick search.
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003572static mirror::Field* FindInstanceFieldWithOffset(const mirror::Class* klass,
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003573 uint32_t field_offset)
3574 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003575 const mirror::ObjectArray<mirror::Field>* instance_fields = klass->GetIFields();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003576 if (instance_fields != NULL) {
3577 for (int32_t i = 0, e = instance_fields->GetLength(); i < e; ++i) {
3578 mirror::Field* field = instance_fields->Get(i);
3579 if (field->GetOffset().Uint32Value() == field_offset) {
3580 return field;
3581 }
3582 }
3583 }
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003584 // We did not find field in class: look into superclass.
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003585 if (klass->GetSuperClass() != NULL) {
3586 return FindInstanceFieldWithOffset(klass->GetSuperClass(), field_offset);
3587 } else {
3588 return NULL;
3589 }
3590}
3591
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003592// Returns the access field of a quick field access (iget/iput-quick) or NULL
3593// if it cannot be found.
3594mirror::Field* MethodVerifier::GetQuickFieldAccess(const Instruction* inst,
3595 RegisterLine* reg_line) {
3596 DCHECK(inst->Opcode() == Instruction::IGET_QUICK ||
3597 inst->Opcode() == Instruction::IGET_WIDE_QUICK ||
3598 inst->Opcode() == Instruction::IGET_OBJECT_QUICK ||
3599 inst->Opcode() == Instruction::IPUT_QUICK ||
3600 inst->Opcode() == Instruction::IPUT_WIDE_QUICK ||
3601 inst->Opcode() == Instruction::IPUT_OBJECT_QUICK);
3602 const RegType& object_type = reg_line->GetRegisterType(inst->VRegB_22c());
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003603 mirror::Class* object_class = NULL;
3604 if (!object_type.IsUnresolvedTypes()) {
3605 object_class = object_type.GetClass();
3606 } else {
3607 // We need to resolve the class from its descriptor.
3608 const std::string& descriptor(object_type.GetDescriptor());
3609 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
3610 object_class = class_linker->FindClass(descriptor.c_str(), class_loader_);
3611 if (object_class == NULL) {
3612 Thread::Current()->ClearException();
3613 // Look for a system class
3614 object_class = class_linker->FindClass(descriptor.c_str(), NULL);
3615 }
3616 }
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003617 if (object_class == NULL) {
3618 // Failed to get the Class* from reg type.
3619 LOG(WARNING) << "Failed to get Class* from " << object_type;
3620 return NULL;
3621 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003622 uint32_t field_offset = static_cast<uint32_t>(inst->VRegC_22c());
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003623 return FindInstanceFieldWithOffset(object_class, field_offset);
3624}
3625
3626void MethodVerifier::VerifyIGetQuick(const Instruction* inst, const RegType& insn_type,
3627 bool is_primitive) {
3628 DCHECK(Runtime::Current()->IsStarted());
3629 mirror::Field* field = GetQuickFieldAccess(inst, work_line_.get());
3630 if (field == NULL) {
3631 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field from " << inst->Name();
3632 return;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003633 }
3634 const char* descriptor = FieldHelper(field).GetTypeDescriptor();
3635 mirror::ClassLoader* loader = field->GetDeclaringClass()->GetClassLoader();
3636 const RegType& field_type = reg_types_.FromDescriptor(loader, descriptor, false);
3637 const uint32_t vregA = inst->VRegA_22c();
3638 if (is_primitive) {
3639 if (field_type.Equals(insn_type) ||
3640 (field_type.IsFloat() && insn_type.IsIntegralTypes()) ||
3641 (field_type.IsDouble() && insn_type.IsLongTypes())) {
3642 // expected that read is of the correct primitive type or that int reads are reading
3643 // floats or long reads are reading doubles
3644 } else {
3645 // This is a global failure rather than a class change failure as the instructions and
3646 // the descriptors for the type should have been consistent within the same file at
3647 // compile time
3648 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003649 << " to be of type '" << insn_type
3650 << "' but found type '" << field_type << "' in get";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003651 return;
3652 }
3653 } else {
3654 if (!insn_type.IsAssignableFrom(field_type)) {
3655 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003656 << " to be compatible with type '" << insn_type
3657 << "' but found type '" << field_type
3658 << "' in get-object";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003659 work_line_->SetRegisterType(vregA, reg_types_.Conflict());
3660 return;
3661 }
3662 }
3663 if (!field_type.IsLowHalf()) {
3664 work_line_->SetRegisterType(vregA, field_type);
3665 } else {
3666 work_line_->SetRegisterTypeWide(vregA, field_type, field_type.HighHalf(&reg_types_));
3667 }
3668}
3669
3670void MethodVerifier::VerifyIPutQuick(const Instruction* inst, const RegType& insn_type,
3671 bool is_primitive) {
3672 DCHECK(Runtime::Current()->IsStarted());
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003673 mirror::Field* field = GetQuickFieldAccess(inst, work_line_.get());
3674 if (field == NULL) {
3675 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field from " << inst->Name();
3676 return;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003677 }
3678 const char* descriptor = FieldHelper(field).GetTypeDescriptor();
3679 mirror::ClassLoader* loader = field->GetDeclaringClass()->GetClassLoader();
3680 const RegType& field_type = reg_types_.FromDescriptor(loader, descriptor, false);
3681 if (field != NULL) {
3682 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
3683 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003684 << " from other class " << GetDeclaringClass();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003685 return;
3686 }
3687 }
3688 const uint32_t vregA = inst->VRegA_22c();
3689 if (is_primitive) {
3690 // Primitive field assignability rules are weaker than regular assignability rules
3691 bool instruction_compatible;
3692 bool value_compatible;
3693 const RegType& value_type = work_line_->GetRegisterType(vregA);
3694 if (field_type.IsIntegralTypes()) {
3695 instruction_compatible = insn_type.IsIntegralTypes();
3696 value_compatible = value_type.IsIntegralTypes();
3697 } else if (field_type.IsFloat()) {
3698 instruction_compatible = insn_type.IsInteger(); // no [is]put-float, so expect [is]put-int
3699 value_compatible = value_type.IsFloatTypes();
3700 } else if (field_type.IsLong()) {
3701 instruction_compatible = insn_type.IsLong();
3702 value_compatible = value_type.IsLongTypes();
3703 } else if (field_type.IsDouble()) {
3704 instruction_compatible = insn_type.IsLong(); // no [is]put-double, so expect [is]put-long
3705 value_compatible = value_type.IsDoubleTypes();
3706 } else {
3707 instruction_compatible = false; // reference field with primitive store
3708 value_compatible = false; // unused
3709 }
3710 if (!instruction_compatible) {
3711 // This is a global failure rather than a class change failure as the instructions and
3712 // the descriptors for the type should have been consistent within the same file at
3713 // compile time
3714 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003715 << " to be of type '" << insn_type
3716 << "' but found type '" << field_type
3717 << "' in put";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003718 return;
3719 }
3720 if (!value_compatible) {
3721 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
3722 << " of type " << value_type
3723 << " but expected " << field_type
3724 << " for store to " << PrettyField(field) << " in put";
3725 return;
3726 }
3727 } else {
3728 if (!insn_type.IsAssignableFrom(field_type)) {
3729 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003730 << " to be compatible with type '" << insn_type
3731 << "' but found type '" << field_type
3732 << "' in put-object";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003733 return;
3734 }
3735 work_line_->VerifyRegisterType(vregA, field_type);
3736 }
3737}
3738
Ian Rogers776ac1f2012-04-13 23:36:36 -07003739bool MethodVerifier::CheckNotMoveException(const uint16_t* insns, int insn_idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003740 if ((insns[insn_idx] & 0xff) == Instruction::MOVE_EXCEPTION) {
jeffhaod5347e02012-03-22 17:25:05 -07003741 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-exception";
Ian Rogersd81871c2011-10-03 13:57:23 -07003742 return false;
3743 }
3744 return true;
3745}
3746
Ian Rogers776ac1f2012-04-13 23:36:36 -07003747bool MethodVerifier::UpdateRegisters(uint32_t next_insn, const RegisterLine* merge_line) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003748 bool changed = true;
3749 RegisterLine* target_line = reg_table_.GetLine(next_insn);
3750 if (!insn_flags_[next_insn].IsVisitedOrChanged()) {
jeffhaobdb76512011-09-07 11:43:16 -07003751 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003752 * We haven't processed this instruction before, and we haven't touched the registers here, so
3753 * there's nothing to "merge". Copy the registers over and mark it as changed. (This is the
3754 * only way a register can transition out of "unknown", so this is not just an optimization.)
jeffhaobdb76512011-09-07 11:43:16 -07003755 */
Ian Rogersb8c78592013-07-25 23:52:52 +00003756 if (!insn_flags_[next_insn].IsReturn()) {
3757 target_line->CopyFromLine(merge_line);
3758 } else {
3759 // For returns we only care about the operand to the return, all other registers are dead.
3760 // Initialize them as conflicts so they don't add to GC and deoptimization information.
3761 const Instruction* ret_inst = Instruction::At(code_item_->insns_ + next_insn);
3762 Instruction::Code opcode = ret_inst->Opcode();
3763 if ((opcode == Instruction::RETURN_VOID) || (opcode == Instruction::RETURN_VOID_BARRIER)) {
3764 target_line->MarkAllRegistersAsConflicts();
3765 } else {
3766 target_line->CopyFromLine(merge_line);
3767 if (opcode == Instruction::RETURN_WIDE) {
3768 target_line->MarkAllRegistersAsConflictsExceptWide(ret_inst->VRegA_11x());
3769 } else {
3770 target_line->MarkAllRegistersAsConflictsExcept(ret_inst->VRegA_11x());
3771 }
3772 }
3773 }
jeffhaobdb76512011-09-07 11:43:16 -07003774 } else {
Brian Carlstrom93c33962013-07-26 10:37:43 -07003775 UniquePtr<RegisterLine> copy(gDebugVerify ?
3776 new RegisterLine(target_line->NumRegs(), this) :
3777 NULL);
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003778 if (gDebugVerify) {
3779 copy->CopyFromLine(target_line);
3780 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003781 changed = target_line->MergeRegisters(merge_line);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003782 if (have_pending_hard_failure_) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003783 return false;
jeffhaobdb76512011-09-07 11:43:16 -07003784 }
Ian Rogers2c8a8572011-10-24 17:11:36 -07003785 if (gDebugVerify && changed) {
Elliott Hughes398f64b2012-03-26 18:05:48 -07003786 LogVerifyInfo() << "Merging at [" << reinterpret_cast<void*>(work_insn_idx_) << "]"
Elliott Hughesc073b072012-05-24 19:29:17 -07003787 << " to [" << reinterpret_cast<void*>(next_insn) << "]: " << "\n"
3788 << *copy.get() << " MERGE\n"
3789 << *merge_line << " ==\n"
3790 << *target_line << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07003791 }
3792 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003793 if (changed) {
3794 insn_flags_[next_insn].SetChanged();
jeffhaobdb76512011-09-07 11:43:16 -07003795 }
3796 return true;
3797}
3798
Ian Rogers7b3ddd22013-02-21 15:19:52 -08003799InstructionFlags* MethodVerifier::CurrentInsnFlags() {
Ian Rogers776ac1f2012-04-13 23:36:36 -07003800 return &insn_flags_[work_insn_idx_];
3801}
3802
Ian Rogersad0b3a32012-04-16 14:50:24 -07003803const RegType& MethodVerifier::GetMethodReturnType() {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003804 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003805 const DexFile::ProtoId& proto_id = dex_file_->GetMethodPrototype(method_id);
3806 uint16_t return_type_idx = proto_id.return_type_idx_;
3807 const char* descriptor = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(return_type_idx));
Ian Rogersb4903572012-10-11 11:52:56 -07003808 return reg_types_.FromDescriptor(class_loader_, descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003809}
3810
3811const RegType& MethodVerifier::GetDeclaringClass() {
Ian Rogers637c65b2013-05-31 11:46:00 -07003812 if (declaring_class_ == NULL) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003813 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
Brian Carlstrom93c33962013-07-26 10:37:43 -07003814 const char* descriptor
3815 = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(method_id.class_idx_));
Ian Rogers637c65b2013-05-31 11:46:00 -07003816 if (mirror_method_ != NULL) {
3817 mirror::Class* klass = mirror_method_->GetDeclaringClass();
Ian Rogers04f94f42013-06-10 15:09:26 -07003818 declaring_class_ = &reg_types_.FromClass(descriptor, klass,
3819 klass->CannotBeAssignedFromOtherTypes());
Ian Rogers637c65b2013-05-31 11:46:00 -07003820 } else {
3821 declaring_class_ = &reg_types_.FromDescriptor(class_loader_, descriptor, false);
3822 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003823 }
Ian Rogers637c65b2013-05-31 11:46:00 -07003824 return *declaring_class_;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003825}
3826
Ian Rogers776ac1f2012-04-13 23:36:36 -07003827void MethodVerifier::ComputeGcMapSizes(size_t* gc_points, size_t* ref_bitmap_bits,
Ian Rogers6d376ae2013-07-23 15:12:40 -07003828 size_t* log2_max_gc_pc) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003829 size_t local_gc_points = 0;
3830 size_t max_insn = 0;
3831 size_t max_ref_reg = -1;
3832 for (size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003833 if (insn_flags_[i].IsCompileTimeInfoPoint()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003834 local_gc_points++;
3835 max_insn = i;
3836 RegisterLine* line = reg_table_.GetLine(i);
Ian Rogers84fa0742011-10-25 18:13:30 -07003837 max_ref_reg = line->GetMaxNonZeroReferenceReg(max_ref_reg);
jeffhaobdb76512011-09-07 11:43:16 -07003838 }
3839 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003840 *gc_points = local_gc_points;
3841 *ref_bitmap_bits = max_ref_reg + 1; // if max register is 0 we need 1 bit to encode (ie +1)
3842 size_t i = 0;
Ian Rogers6b0870d2011-12-15 19:38:12 -08003843 while ((1U << i) <= max_insn) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003844 i++;
3845 }
3846 *log2_max_gc_pc = i;
jeffhaobdb76512011-09-07 11:43:16 -07003847}
3848
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07003849MethodVerifier::MethodSafeCastSet* MethodVerifier::GenerateSafeCastSet() {
3850 /*
3851 * Walks over the method code and adds any cast instructions in which
3852 * the type cast is implicit to a set, which is used in the code generation
3853 * to elide these casts.
3854 */
3855 if (!failure_messages_.empty()) {
3856 return NULL;
3857 }
3858 UniquePtr<MethodSafeCastSet> mscs;
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07003859 const Instruction* inst = Instruction::At(code_item_->insns_);
3860 const Instruction* end = Instruction::At(code_item_->insns_ +
Ian Rogersfae370a2013-06-05 08:33:27 -07003861 code_item_->insns_size_in_code_units_);
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07003862
3863 for (; inst < end; inst = inst->Next()) {
Ian Rogersfae370a2013-06-05 08:33:27 -07003864 if (Instruction::CHECK_CAST != inst->Opcode()) {
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07003865 continue;
Ian Rogersfae370a2013-06-05 08:33:27 -07003866 }
3867 uint32_t dex_pc = inst->GetDexPc(code_item_->insns_);
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07003868 RegisterLine* line = reg_table_.GetLine(dex_pc);
Ian Rogersfae370a2013-06-05 08:33:27 -07003869 const RegType& reg_type(line->GetRegisterType(inst->VRegA_21c()));
3870 const RegType& cast_type = ResolveClassAndCheckAccess(inst->VRegB_21c());
Ian Rogers16e3d2c2013-06-07 12:57:00 -07003871 if (cast_type.IsStrictlyAssignableFrom(reg_type)) {
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07003872 if (mscs.get() == NULL) {
3873 mscs.reset(new MethodSafeCastSet());
3874 }
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07003875 mscs->insert(dex_pc);
3876 }
3877 }
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07003878 return mscs.release();
3879}
3880
Ian Rogersd0583802013-06-01 10:51:46 -07003881MethodVerifier::PcToConcreteMethodMap* MethodVerifier::GenerateDevirtMap() {
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003882 // It is risky to rely on reg_types for sharpening in cases of soft
3883 // verification, we might end up sharpening to a wrong implementation. Just abort.
3884 if (!failure_messages_.empty()) {
3885 return NULL;
3886 }
3887
Ian Rogersd0583802013-06-01 10:51:46 -07003888 UniquePtr<PcToConcreteMethodMap> pc_to_concrete_method_map;
Brian Carlstrom02c8cc62013-07-18 15:54:44 -07003889 const uint16_t* insns = code_item_->insns_;
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003890 const Instruction* inst = Instruction::At(insns);
Ian Rogersd0583802013-06-01 10:51:46 -07003891 const Instruction* end = Instruction::At(insns + code_item_->insns_size_in_code_units_);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003892
Ian Rogersd0583802013-06-01 10:51:46 -07003893 for (; inst < end; inst = inst->Next()) {
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003894 bool is_virtual = (inst->Opcode() == Instruction::INVOKE_VIRTUAL) ||
3895 (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE);
3896 bool is_interface = (inst->Opcode() == Instruction::INVOKE_INTERFACE) ||
3897 (inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE);
3898
Brian Carlstromdf629502013-07-17 22:39:56 -07003899 if (!is_interface && !is_virtual) {
Dragos Sbirlea29e2e7e2013-05-22 14:52:11 -07003900 continue;
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003901 }
Ian Rogersd0583802013-06-01 10:51:46 -07003902 // Get reg type for register holding the reference to the object that will be dispatched upon.
3903 uint32_t dex_pc = inst->GetDexPc(insns);
3904 RegisterLine* line = reg_table_.GetLine(dex_pc);
3905 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE) ||
3906 (inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE);
3907 const RegType&
3908 reg_type(line->GetRegisterType(is_range ? inst->VRegC_3rc() : inst->VRegC_35c()));
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003909
Ian Rogersd0583802013-06-01 10:51:46 -07003910 if (!reg_type.HasClass()) {
3911 // We will compute devirtualization information only when we know the Class of the reg type.
3912 continue;
3913 }
3914 mirror::Class* reg_class = reg_type.GetClass();
3915 if (reg_class->IsInterface()) {
3916 // We can't devirtualize when the known type of the register is an interface.
3917 continue;
3918 }
3919 if (reg_class->IsAbstract() && !reg_class->IsArrayClass()) {
3920 // We can't devirtualize abstract classes except on arrays of abstract classes.
3921 continue;
3922 }
3923 mirror::AbstractMethod* abstract_method =
3924 dex_cache_->GetResolvedMethod(is_range ? inst->VRegB_3rc() : inst->VRegB_35c());
Brian Carlstromdf629502013-07-17 22:39:56 -07003925 if (abstract_method == NULL) {
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003926 // If the method is not found in the cache this means that it was never found
3927 // by ResolveMethodAndCheckAccess() called when verifying invoke_*.
3928 continue;
3929 }
3930 // Find the concrete method.
3931 mirror::AbstractMethod* concrete_method = NULL;
3932 if (is_interface) {
3933 concrete_method = reg_type.GetClass()->FindVirtualMethodForInterface(abstract_method);
3934 }
3935 if (is_virtual) {
3936 concrete_method = reg_type.GetClass()->FindVirtualMethodForVirtual(abstract_method);
3937 }
Ian Rogersd0583802013-06-01 10:51:46 -07003938 if (concrete_method == NULL || concrete_method->IsAbstract()) {
3939 // In cases where concrete_method is not found, or is abstract, continue to the next invoke.
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003940 continue;
3941 }
Ian Rogersd0583802013-06-01 10:51:46 -07003942 if (reg_type.IsPreciseReference() || concrete_method->IsFinal() ||
3943 concrete_method->GetDeclaringClass()->IsFinal()) {
3944 // If we knew exactly the class being dispatched upon, or if the target method cannot be
3945 // overridden record the target to be used in the compiler driver.
3946 if (pc_to_concrete_method_map.get() == NULL) {
3947 pc_to_concrete_method_map.reset(new PcToConcreteMethodMap());
3948 }
Brian Carlstrom51c24672013-07-11 16:00:56 -07003949 MethodReference concrete_ref(
Ian Rogersd0583802013-06-01 10:51:46 -07003950 concrete_method->GetDeclaringClass()->GetDexCache()->GetDexFile(),
3951 concrete_method->GetDexMethodIndex());
3952 pc_to_concrete_method_map->Put(dex_pc, concrete_ref);
3953 }
Dragos Sbirlea29e2e7e2013-05-22 14:52:11 -07003954 }
Ian Rogersd0583802013-06-01 10:51:46 -07003955 return pc_to_concrete_method_map.release();
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003956}
3957
Ian Rogers776ac1f2012-04-13 23:36:36 -07003958const std::vector<uint8_t>* MethodVerifier::GenerateGcMap() {
Ian Rogersd81871c2011-10-03 13:57:23 -07003959 size_t num_entries, ref_bitmap_bits, pc_bits;
3960 ComputeGcMapSizes(&num_entries, &ref_bitmap_bits, &pc_bits);
3961 // There's a single byte to encode the size of each bitmap
jeffhao60f83e32012-02-13 17:16:30 -08003962 if (ref_bitmap_bits >= (8 /* bits per byte */ * 8192 /* 13-bit size */ )) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003963 // TODO: either a better GC map format or per method failures
jeffhaod5347e02012-03-22 17:25:05 -07003964 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot encode GC map for method with "
Ian Rogersd81871c2011-10-03 13:57:23 -07003965 << ref_bitmap_bits << " registers";
jeffhaobdb76512011-09-07 11:43:16 -07003966 return NULL;
3967 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003968 size_t ref_bitmap_bytes = (ref_bitmap_bits + 7) / 8;
3969 // There are 2 bytes to encode the number of entries
3970 if (num_entries >= 65536) {
3971 // TODO: either a better GC map format or per method failures
jeffhaod5347e02012-03-22 17:25:05 -07003972 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot encode GC map for method with "
Ian Rogersd81871c2011-10-03 13:57:23 -07003973 << num_entries << " entries";
jeffhaobdb76512011-09-07 11:43:16 -07003974 return NULL;
3975 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003976 size_t pc_bytes;
jeffhaod1f0fde2011-09-08 17:25:33 -07003977 RegisterMapFormat format;
Ian Rogers6b0870d2011-12-15 19:38:12 -08003978 if (pc_bits <= 8) {
jeffhaod1f0fde2011-09-08 17:25:33 -07003979 format = kRegMapFormatCompact8;
Ian Rogersd81871c2011-10-03 13:57:23 -07003980 pc_bytes = 1;
Ian Rogers6b0870d2011-12-15 19:38:12 -08003981 } else if (pc_bits <= 16) {
jeffhaod1f0fde2011-09-08 17:25:33 -07003982 format = kRegMapFormatCompact16;
Ian Rogersd81871c2011-10-03 13:57:23 -07003983 pc_bytes = 2;
jeffhaoa0a764a2011-09-16 10:43:38 -07003984 } else {
Ian Rogersd81871c2011-10-03 13:57:23 -07003985 // TODO: either a better GC map format or per method failures
jeffhaod5347e02012-03-22 17:25:05 -07003986 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot encode GC map for method with "
Ian Rogersd81871c2011-10-03 13:57:23 -07003987 << (1 << pc_bits) << " instructions (number is rounded up to nearest power of 2)";
3988 return NULL;
3989 }
Elliott Hughesb25c3f62012-03-26 16:35:06 -07003990 size_t table_size = ((pc_bytes + ref_bitmap_bytes) * num_entries) + 4;
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003991 std::vector<uint8_t>* table = new std::vector<uint8_t>;
Ian Rogersd81871c2011-10-03 13:57:23 -07003992 if (table == NULL) {
jeffhaod5347e02012-03-22 17:25:05 -07003993 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Failed to encode GC map (size=" << table_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07003994 return NULL;
3995 }
Ian Rogers39ebcb82013-05-30 16:57:23 -07003996 table->reserve(table_size);
Ian Rogersd81871c2011-10-03 13:57:23 -07003997 // Write table header
Ian Rogers46c6bb22012-09-18 13:47:36 -07003998 table->push_back(format | ((ref_bitmap_bytes >> DexPcToReferenceMap::kRegMapFormatShift) &
3999 ~DexPcToReferenceMap::kRegMapFormatMask));
jeffhao60f83e32012-02-13 17:16:30 -08004000 table->push_back(ref_bitmap_bytes & 0xFF);
Brian Carlstrome7d856b2012-01-11 18:10:55 -08004001 table->push_back(num_entries & 0xFF);
4002 table->push_back((num_entries >> 8) & 0xFF);
Ian Rogersd81871c2011-10-03 13:57:23 -07004003 // Write table data
Ian Rogersd81871c2011-10-03 13:57:23 -07004004 for (size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
Sameer Abu Asal02c42232013-04-30 12:09:45 -07004005 if (insn_flags_[i].IsCompileTimeInfoPoint()) {
Brian Carlstrome7d856b2012-01-11 18:10:55 -08004006 table->push_back(i & 0xFF);
Ian Rogersd81871c2011-10-03 13:57:23 -07004007 if (pc_bytes == 2) {
Brian Carlstrome7d856b2012-01-11 18:10:55 -08004008 table->push_back((i >> 8) & 0xFF);
Ian Rogersd81871c2011-10-03 13:57:23 -07004009 }
4010 RegisterLine* line = reg_table_.GetLine(i);
Brian Carlstrome7d856b2012-01-11 18:10:55 -08004011 line->WriteReferenceBitMap(*table, ref_bitmap_bytes);
Ian Rogersd81871c2011-10-03 13:57:23 -07004012 }
4013 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -08004014 DCHECK_EQ(table->size(), table_size);
Ian Rogersd81871c2011-10-03 13:57:23 -07004015 return table;
4016}
jeffhaoa0a764a2011-09-16 10:43:38 -07004017
Ian Rogers776ac1f2012-04-13 23:36:36 -07004018void MethodVerifier::VerifyGcMap(const std::vector<uint8_t>& data) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004019 // Check that for every GC point there is a map entry, there aren't entries for non-GC points,
4020 // that the table data is well formed and all references are marked (or not) in the bitmap
Ian Rogers46c6bb22012-09-18 13:47:36 -07004021 DexPcToReferenceMap map(&data[0], data.size());
Ian Rogersd81871c2011-10-03 13:57:23 -07004022 size_t map_index = 0;
Elliott Hughesb25c3f62012-03-26 16:35:06 -07004023 for (size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004024 const uint8_t* reg_bitmap = map.FindBitMap(i, false);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07004025 if (insn_flags_[i].IsCompileTimeInfoPoint()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004026 CHECK_LT(map_index, map.NumEntries());
Ian Rogers46c6bb22012-09-18 13:47:36 -07004027 CHECK_EQ(map.GetDexPc(map_index), i);
Ian Rogersd81871c2011-10-03 13:57:23 -07004028 CHECK_EQ(map.GetBitMap(map_index), reg_bitmap);
4029 map_index++;
4030 RegisterLine* line = reg_table_.GetLine(i);
Elliott Hughesb25c3f62012-03-26 16:35:06 -07004031 for (size_t j = 0; j < code_item_->registers_size_; j++) {
Ian Rogers84fa0742011-10-25 18:13:30 -07004032 if (line->GetRegisterType(j).IsNonZeroReferenceTypes()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004033 CHECK_LT(j / 8, map.RegWidth());
4034 CHECK_EQ((reg_bitmap[j / 8] >> (j % 8)) & 1, 1);
4035 } else if ((j / 8) < map.RegWidth()) {
4036 CHECK_EQ((reg_bitmap[j / 8] >> (j % 8)) & 1, 0);
4037 } else {
4038 // If a register doesn't contain a reference then the bitmap may be shorter than the line
4039 }
4040 }
4041 } else {
4042 CHECK(reg_bitmap == NULL);
4043 }
4044 }
4045}
jeffhaoa0a764a2011-09-16 10:43:38 -07004046
Brian Carlstrom51c24672013-07-11 16:00:56 -07004047void MethodVerifier::SetDexGcMap(MethodReference ref, const std::vector<uint8_t>& gc_map) {
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004048 DCHECK(Runtime::Current()->IsCompiler());
Ian Rogers00f7d0e2012-07-19 15:28:27 -07004049 {
Ian Rogers637c65b2013-05-31 11:46:00 -07004050 WriterMutexLock mu(Thread::Current(), *dex_gc_maps_lock_);
Ian Rogers0c7abda2012-09-19 13:33:42 -07004051 DexGcMapTable::iterator it = dex_gc_maps_->find(ref);
4052 if (it != dex_gc_maps_->end()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07004053 delete it->second;
Ian Rogers0c7abda2012-09-19 13:33:42 -07004054 dex_gc_maps_->erase(it);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07004055 }
Ian Rogers0c7abda2012-09-19 13:33:42 -07004056 dex_gc_maps_->Put(ref, &gc_map);
Brian Carlstrom73a15f42012-01-17 18:14:39 -08004057 }
Ian Rogers39ebcb82013-05-30 16:57:23 -07004058 DCHECK(GetDexGcMap(ref) != NULL);
Brian Carlstrome7d856b2012-01-11 18:10:55 -08004059}
4060
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07004061
Brian Carlstrom51c24672013-07-11 16:00:56 -07004062void MethodVerifier::SetSafeCastMap(MethodReference ref, const MethodSafeCastSet* cast_set) {
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004063 DCHECK(Runtime::Current()->IsCompiler());
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07004064 MutexLock mu(Thread::Current(), *safecast_map_lock_);
4065 SafeCastMap::iterator it = safecast_map_->find(ref);
4066 if (it != safecast_map_->end()) {
4067 delete it->second;
4068 safecast_map_->erase(it);
4069 }
4070
4071 safecast_map_->Put(ref, cast_set);
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004072 DCHECK(safecast_map_->find(ref) != safecast_map_->end());
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07004073}
4074
Brian Carlstrom51c24672013-07-11 16:00:56 -07004075bool MethodVerifier::IsSafeCast(MethodReference ref, uint32_t pc) {
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004076 DCHECK(Runtime::Current()->IsCompiler());
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07004077 MutexLock mu(Thread::Current(), *safecast_map_lock_);
4078 SafeCastMap::const_iterator it = safecast_map_->find(ref);
4079 if (it == safecast_map_->end()) {
4080 return false;
4081 }
4082
4083 // Look up the cast address in the set of safe casts
4084 MethodVerifier::MethodSafeCastSet::const_iterator cast_it = it->second->find(pc);
4085 return cast_it != it->second->end();
4086}
4087
Brian Carlstrom51c24672013-07-11 16:00:56 -07004088const std::vector<uint8_t>* MethodVerifier::GetDexGcMap(MethodReference ref) {
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004089 DCHECK(Runtime::Current()->IsCompiler());
Ian Rogers637c65b2013-05-31 11:46:00 -07004090 ReaderMutexLock mu(Thread::Current(), *dex_gc_maps_lock_);
4091 DexGcMapTable::const_iterator it = dex_gc_maps_->find(ref);
4092 if (it == dex_gc_maps_->end()) {
4093 LOG(WARNING) << "Didn't find GC map for: " << PrettyMethod(ref.dex_method_index, *ref.dex_file);
4094 return NULL;
4095 }
4096 CHECK(it->second != NULL);
4097 return it->second;
4098}
Sameer Abu Asal02c42232013-04-30 12:09:45 -07004099
Brian Carlstrom51c24672013-07-11 16:00:56 -07004100void MethodVerifier::SetDevirtMap(MethodReference ref,
Ian Rogersd0583802013-06-01 10:51:46 -07004101 const PcToConcreteMethodMap* devirt_map) {
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004102 DCHECK(Runtime::Current()->IsCompiler());
Ian Rogers637c65b2013-05-31 11:46:00 -07004103 WriterMutexLock mu(Thread::Current(), *devirt_maps_lock_);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07004104 DevirtualizationMapTable::iterator it = devirt_maps_->find(ref);
4105 if (it != devirt_maps_->end()) {
4106 delete it->second;
4107 devirt_maps_->erase(it);
4108 }
4109
4110 devirt_maps_->Put(ref, devirt_map);
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004111 DCHECK(devirt_maps_->find(ref) != devirt_maps_->end());
Sameer Abu Asal02c42232013-04-30 12:09:45 -07004112}
4113
Brian Carlstrom51c24672013-07-11 16:00:56 -07004114const MethodReference* MethodVerifier::GetDevirtMap(const MethodReference& ref,
Ian Rogerse3cd2f02013-05-24 15:32:56 -07004115 uint32_t dex_pc) {
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004116 DCHECK(Runtime::Current()->IsCompiler());
Ian Rogers637c65b2013-05-31 11:46:00 -07004117 ReaderMutexLock mu(Thread::Current(), *devirt_maps_lock_);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07004118 DevirtualizationMapTable::const_iterator it = devirt_maps_->find(ref);
4119 if (it == devirt_maps_->end()) {
4120 return NULL;
4121 }
4122
4123 // Look up the PC in the map, get the concrete method to execute and return its reference.
Brian Carlstrom93c33962013-07-26 10:37:43 -07004124 MethodVerifier::PcToConcreteMethodMap::const_iterator pc_to_concrete_method
4125 = it->second->find(dex_pc);
Brian Carlstromdf629502013-07-17 22:39:56 -07004126 if (pc_to_concrete_method != it->second->end()) {
Sameer Abu Asal02c42232013-04-30 12:09:45 -07004127 return &(pc_to_concrete_method->second);
4128 } else {
4129 return NULL;
4130 }
4131}
4132
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004133std::vector<int32_t> MethodVerifier::DescribeVRegs(uint32_t dex_pc) {
4134 RegisterLine* line = reg_table_.GetLine(dex_pc);
4135 std::vector<int32_t> result;
4136 for (size_t i = 0; i < line->NumRegs(); ++i) {
4137 const RegType& type = line->GetRegisterType(i);
4138 if (type.IsConstant()) {
4139 result.push_back(type.IsPreciseConstant() ? kConstant : kImpreciseConstant);
4140 result.push_back(type.ConstantValue());
4141 } else if (type.IsConstantLo()) {
4142 result.push_back(type.IsPreciseConstantLo() ? kConstant : kImpreciseConstant);
4143 result.push_back(type.ConstantValueLo());
4144 } else if (type.IsConstantHi()) {
4145 result.push_back(type.IsPreciseConstantHi() ? kConstant : kImpreciseConstant);
4146 result.push_back(type.ConstantValueHi());
4147 } else if (type.IsIntegralTypes()) {
4148 result.push_back(kIntVReg);
4149 result.push_back(0);
4150 } else if (type.IsFloat()) {
4151 result.push_back(kFloatVReg);
4152 result.push_back(0);
4153 } else if (type.IsLong()) {
4154 result.push_back(kLongLoVReg);
4155 result.push_back(0);
4156 result.push_back(kLongHiVReg);
4157 result.push_back(0);
4158 ++i;
4159 } else if (type.IsDouble()) {
4160 result.push_back(kDoubleLoVReg);
4161 result.push_back(0);
4162 result.push_back(kDoubleHiVReg);
4163 result.push_back(0);
4164 ++i;
4165 } else if (type.IsUndefined() || type.IsConflict() || type.IsHighHalf()) {
4166 result.push_back(kUndefined);
4167 result.push_back(0);
4168 } else {
Ian Rogers7b3ddd22013-02-21 15:19:52 -08004169 CHECK(type.IsNonZeroReferenceTypes());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004170 result.push_back(kReferenceVReg);
4171 result.push_back(0);
4172 }
4173 }
4174 return result;
4175}
4176
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004177bool MethodVerifier::IsCandidateForCompilation(const DexFile::CodeItem* code_item,
4178 const uint32_t access_flags) {
4179 // Don't compile class initializers, ever.
4180 if (((access_flags & kAccConstructor) != 0) && ((access_flags & kAccStatic) != 0)) {
4181 return false;
4182 }
buzbeea024a062013-07-31 10:47:37 -07004183 return (Runtime::Current()->GetCompilerFilter() != Runtime::kInterpretOnly);
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004184}
4185
Ian Rogers637c65b2013-05-31 11:46:00 -07004186ReaderWriterMutex* MethodVerifier::dex_gc_maps_lock_ = NULL;
Ian Rogers0c7abda2012-09-19 13:33:42 -07004187MethodVerifier::DexGcMapTable* MethodVerifier::dex_gc_maps_ = NULL;
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004188
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07004189Mutex* MethodVerifier::safecast_map_lock_ = NULL;
4190MethodVerifier::SafeCastMap* MethodVerifier::safecast_map_ = NULL;
4191
Ian Rogers637c65b2013-05-31 11:46:00 -07004192ReaderWriterMutex* MethodVerifier::devirt_maps_lock_ = NULL;
Sameer Abu Asal02c42232013-04-30 12:09:45 -07004193MethodVerifier::DevirtualizationMapTable* MethodVerifier::devirt_maps_ = NULL;
4194
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004195Mutex* MethodVerifier::rejected_classes_lock_ = NULL;
4196MethodVerifier::RejectedClassesTable* MethodVerifier::rejected_classes_ = NULL;
4197
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004198void MethodVerifier::Init() {
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004199 if (Runtime::Current()->IsCompiler()) {
4200 dex_gc_maps_lock_ = new ReaderWriterMutex("verifier GC maps lock");
4201 Thread* self = Thread::Current();
4202 {
4203 WriterMutexLock mu(self, *dex_gc_maps_lock_);
4204 dex_gc_maps_ = new MethodVerifier::DexGcMapTable;
4205 }
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004206
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004207 safecast_map_lock_ = new Mutex("verifier Cast Elision lock");
4208 {
4209 MutexLock mu(self, *safecast_map_lock_);
4210 safecast_map_ = new MethodVerifier::SafeCastMap();
4211 }
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07004212
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004213 devirt_maps_lock_ = new ReaderWriterMutex("verifier Devirtualization lock");
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07004214
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004215 {
4216 WriterMutexLock mu(self, *devirt_maps_lock_);
4217 devirt_maps_ = new MethodVerifier::DevirtualizationMapTable();
4218 }
Sameer Abu Asal02c42232013-04-30 12:09:45 -07004219
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004220 rejected_classes_lock_ = new Mutex("verifier rejected classes lock");
4221 {
4222 MutexLock mu(self, *rejected_classes_lock_);
4223 rejected_classes_ = new MethodVerifier::RejectedClassesTable;
4224 }
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004225 }
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08004226 art::verifier::RegTypeCache::Init();
Brian Carlstrome7d856b2012-01-11 18:10:55 -08004227}
4228
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004229void MethodVerifier::Shutdown() {
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004230 if (Runtime::Current()->IsCompiler()) {
4231 Thread* self = Thread::Current();
4232 {
4233 WriterMutexLock mu(self, *dex_gc_maps_lock_);
4234 STLDeleteValues(dex_gc_maps_);
4235 delete dex_gc_maps_;
4236 dex_gc_maps_ = NULL;
4237 }
4238 delete dex_gc_maps_lock_;
4239 dex_gc_maps_lock_ = NULL;
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004240
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004241 {
4242 MutexLock mu(self, *safecast_map_lock_);
4243 STLDeleteValues(safecast_map_);
4244 delete safecast_map_;
4245 safecast_map_ = NULL;
4246 }
4247 delete safecast_map_lock_;
4248 safecast_map_lock_ = NULL;
Sameer Abu Asal02c42232013-04-30 12:09:45 -07004249
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004250 {
4251 WriterMutexLock mu(self, *devirt_maps_lock_);
4252 STLDeleteValues(devirt_maps_);
4253 delete devirt_maps_;
4254 devirt_maps_ = NULL;
4255 }
4256 delete devirt_maps_lock_;
4257 devirt_maps_lock_ = NULL;
4258
4259 {
4260 MutexLock mu(self, *rejected_classes_lock_);
4261 delete rejected_classes_;
4262 rejected_classes_ = NULL;
4263 }
4264 delete rejected_classes_lock_;
4265 rejected_classes_lock_ = NULL;
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004266 }
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08004267 verifier::RegTypeCache::ShutDown();
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08004268}
jeffhaod1224c72012-02-29 13:43:08 -08004269
Brian Carlstrom51c24672013-07-11 16:00:56 -07004270void MethodVerifier::AddRejectedClass(ClassReference ref) {
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004271 DCHECK(Runtime::Current()->IsCompiler());
Ian Rogers00f7d0e2012-07-19 15:28:27 -07004272 {
Ian Rogers50b35e22012-10-04 10:09:15 -07004273 MutexLock mu(Thread::Current(), *rejected_classes_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07004274 rejected_classes_->insert(ref);
4275 }
jeffhaod1224c72012-02-29 13:43:08 -08004276 CHECK(IsClassRejected(ref));
4277}
4278
Brian Carlstrom51c24672013-07-11 16:00:56 -07004279bool MethodVerifier::IsClassRejected(ClassReference ref) {
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004280 DCHECK(Runtime::Current()->IsCompiler());
Ian Rogers50b35e22012-10-04 10:09:15 -07004281 MutexLock mu(Thread::Current(), *rejected_classes_lock_);
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004282 return (rejected_classes_->find(ref) != rejected_classes_->end());
jeffhaod1224c72012-02-29 13:43:08 -08004283}
4284
Ian Rogersd81871c2011-10-03 13:57:23 -07004285} // namespace verifier
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07004286} // namespace art