blob: 9948e00acc9e0eca2c497bde0281a0562fe055a0 [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);
321 CHECK(code_item_ != NULL); // This only makes sense for methods with code.
322
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) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200340 CHECK(code_item_ != NULL); // This only makes sense for methods with code.
341
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) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200368 CHECK(code_item_ != NULL); // This only makes sense for methods with code.
369
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 }
jeffhao60f83e32012-02-13 17:16:30 -0800558 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
559 if (inst->Opcode() != Instruction::MOVE_EXCEPTION) {
jeffhaod5347e02012-03-22 17:25:05 -0700560 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "exception handler doesn't start with move-exception ("
Ian Rogersad0b3a32012-04-16 14:50:24 -0700561 << dex_pc << ")";
jeffhao60f83e32012-02-13 17:16:30 -0800562 return false;
563 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700564 insn_flags_[dex_pc].SetBranchTarget();
Ian Rogers28ad40d2011-10-27 15:19:26 -0700565 // Ensure exception types are resolved so that they don't need resolution to be delivered,
566 // unresolved exception types will be ignored by exception delivery
Ian Rogers0571d352011-11-03 19:51:38 -0700567 if (iterator.GetHandlerTypeIndex() != DexFile::kDexNoIndex16) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800568 mirror::Class* exception_type = linker->ResolveType(*dex_file_,
569 iterator.GetHandlerTypeIndex(),
570 dex_cache_, class_loader_);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700571 if (exception_type == NULL) {
572 DCHECK(Thread::Current()->IsExceptionPending());
573 Thread::Current()->ClearException();
574 }
575 }
jeffhaobdb76512011-09-07 11:43:16 -0700576 }
Ian Rogers0571d352011-11-03 19:51:38 -0700577 handlers_ptr = iterator.EndDataPointer();
jeffhaobdb76512011-09-07 11:43:16 -0700578 }
jeffhaobdb76512011-09-07 11:43:16 -0700579 return true;
580}
581
Ian Rogers776ac1f2012-04-13 23:36:36 -0700582bool MethodVerifier::VerifyInstructions() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700583 const Instruction* inst = Instruction::At(code_item_->insns_);
jeffhaoba5ebb92011-08-25 17:24:37 -0700584
Ian Rogers0c7abda2012-09-19 13:33:42 -0700585 /* 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 -0700586 insn_flags_[0].SetBranchTarget();
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700587 insn_flags_[0].SetCompileTimeInfoPoint();
Ian Rogersd81871c2011-10-03 13:57:23 -0700588
589 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700590 for (uint32_t dex_pc = 0; dex_pc < insns_size;) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700591 if (!VerifyInstruction(inst, dex_pc)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700592 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -0700593 return false;
594 }
595 /* Flag instructions that are garbage collection points */
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700596 // All invoke points are marked as "Throw" points already.
597 // We are relying on this to also count all the invokes as interesting.
Ian Rogersb8c78592013-07-25 23:52:52 +0000598 if (inst->IsBranch() || inst->IsSwitch() || inst->IsThrow()) {
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700599 insn_flags_[dex_pc].SetCompileTimeInfoPoint();
Ian Rogersb8c78592013-07-25 23:52:52 +0000600 } else if (inst->IsReturn()) {
601 insn_flags_[dex_pc].SetCompileTimeInfoPointAndReturn();
Ian Rogersd81871c2011-10-03 13:57:23 -0700602 }
603 dex_pc += inst->SizeInCodeUnits();
604 inst = inst->Next();
605 }
606 return true;
607}
608
Ian Rogers776ac1f2012-04-13 23:36:36 -0700609bool MethodVerifier::VerifyInstruction(const Instruction* inst, uint32_t code_offset) {
Elliott Hughesadb8c672012-03-06 16:49:32 -0800610 DecodedInstruction dec_insn(inst);
Ian Rogersd81871c2011-10-03 13:57:23 -0700611 bool result = true;
612 switch (inst->GetVerifyTypeArgumentA()) {
613 case Instruction::kVerifyRegA:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800614 result = result && CheckRegisterIndex(dec_insn.vA);
Ian Rogersd81871c2011-10-03 13:57:23 -0700615 break;
616 case Instruction::kVerifyRegAWide:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800617 result = result && CheckWideRegisterIndex(dec_insn.vA);
Ian Rogersd81871c2011-10-03 13:57:23 -0700618 break;
619 }
620 switch (inst->GetVerifyTypeArgumentB()) {
621 case Instruction::kVerifyRegB:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800622 result = result && CheckRegisterIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700623 break;
624 case Instruction::kVerifyRegBField:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800625 result = result && CheckFieldIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700626 break;
627 case Instruction::kVerifyRegBMethod:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800628 result = result && CheckMethodIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700629 break;
630 case Instruction::kVerifyRegBNewInstance:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800631 result = result && CheckNewInstance(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700632 break;
633 case Instruction::kVerifyRegBString:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800634 result = result && CheckStringIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700635 break;
636 case Instruction::kVerifyRegBType:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800637 result = result && CheckTypeIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700638 break;
639 case Instruction::kVerifyRegBWide:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800640 result = result && CheckWideRegisterIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700641 break;
642 }
643 switch (inst->GetVerifyTypeArgumentC()) {
644 case Instruction::kVerifyRegC:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800645 result = result && CheckRegisterIndex(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700646 break;
647 case Instruction::kVerifyRegCField:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800648 result = result && CheckFieldIndex(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700649 break;
650 case Instruction::kVerifyRegCNewArray:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800651 result = result && CheckNewArray(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700652 break;
653 case Instruction::kVerifyRegCType:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800654 result = result && CheckTypeIndex(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700655 break;
656 case Instruction::kVerifyRegCWide:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800657 result = result && CheckWideRegisterIndex(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700658 break;
659 }
660 switch (inst->GetVerifyExtraFlags()) {
661 case Instruction::kVerifyArrayData:
662 result = result && CheckArrayData(code_offset);
663 break;
664 case Instruction::kVerifyBranchTarget:
665 result = result && CheckBranchTarget(code_offset);
666 break;
667 case Instruction::kVerifySwitchTargets:
668 result = result && CheckSwitchTargets(code_offset);
669 break;
670 case Instruction::kVerifyVarArg:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800671 result = result && CheckVarArgRegs(dec_insn.vA, dec_insn.arg);
Ian Rogersd81871c2011-10-03 13:57:23 -0700672 break;
673 case Instruction::kVerifyVarArgRange:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800674 result = result && CheckVarArgRangeRegs(dec_insn.vA, dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700675 break;
676 case Instruction::kVerifyError:
jeffhaod5347e02012-03-22 17:25:05 -0700677 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected opcode " << inst->Name();
Ian Rogersd81871c2011-10-03 13:57:23 -0700678 result = false;
679 break;
680 }
681 return result;
682}
683
Ian Rogers776ac1f2012-04-13 23:36:36 -0700684bool MethodVerifier::CheckRegisterIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700685 if (idx >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700686 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register index out of range (" << idx << " >= "
687 << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700688 return false;
689 }
690 return true;
691}
692
Ian Rogers776ac1f2012-04-13 23:36:36 -0700693bool MethodVerifier::CheckWideRegisterIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700694 if (idx + 1 >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700695 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "wide register index out of range (" << idx
696 << "+1 >= " << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700697 return false;
698 }
699 return true;
700}
701
Ian Rogers776ac1f2012-04-13 23:36:36 -0700702bool MethodVerifier::CheckFieldIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700703 if (idx >= dex_file_->GetHeader().field_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700704 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad field index " << idx << " (max "
705 << dex_file_->GetHeader().field_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700706 return false;
707 }
708 return true;
709}
710
Ian Rogers776ac1f2012-04-13 23:36:36 -0700711bool MethodVerifier::CheckMethodIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700712 if (idx >= dex_file_->GetHeader().method_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700713 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad method index " << idx << " (max "
714 << dex_file_->GetHeader().method_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700715 return false;
716 }
717 return true;
718}
719
Ian Rogers776ac1f2012-04-13 23:36:36 -0700720bool MethodVerifier::CheckNewInstance(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700721 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700722 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
723 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700724 return false;
725 }
726 // We don't need the actual class, just a pointer to the class name.
Ian Rogers0571d352011-11-03 19:51:38 -0700727 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -0700728 if (descriptor[0] != 'L') {
jeffhaod5347e02012-03-22 17:25:05 -0700729 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "can't call new-instance on type '" << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -0700730 return false;
731 }
732 return true;
733}
734
Ian Rogers776ac1f2012-04-13 23:36:36 -0700735bool MethodVerifier::CheckStringIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700736 if (idx >= dex_file_->GetHeader().string_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700737 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad string index " << idx << " (max "
738 << dex_file_->GetHeader().string_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700739 return false;
740 }
741 return true;
742}
743
Ian Rogers776ac1f2012-04-13 23:36:36 -0700744bool MethodVerifier::CheckTypeIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700745 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700746 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
747 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700748 return false;
749 }
750 return true;
751}
752
Ian Rogers776ac1f2012-04-13 23:36:36 -0700753bool MethodVerifier::CheckNewArray(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700754 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700755 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
756 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700757 return false;
758 }
759 int bracket_count = 0;
Ian Rogers0571d352011-11-03 19:51:38 -0700760 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -0700761 const char* cp = descriptor;
762 while (*cp++ == '[') {
763 bracket_count++;
764 }
765 if (bracket_count == 0) {
766 /* The given class must be an array type. */
Brian Carlstrom93c33962013-07-26 10:37:43 -0700767 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
768 << "can't new-array class '" << descriptor << "' (not an array)";
Ian Rogersd81871c2011-10-03 13:57:23 -0700769 return false;
770 } else if (bracket_count > 255) {
771 /* It is illegal to create an array of more than 255 dimensions. */
Brian Carlstrom93c33962013-07-26 10:37:43 -0700772 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
773 << "can't new-array class '" << descriptor << "' (exceeds limit)";
Ian Rogersd81871c2011-10-03 13:57:23 -0700774 return false;
775 }
776 return true;
777}
778
Ian Rogers776ac1f2012-04-13 23:36:36 -0700779bool MethodVerifier::CheckArrayData(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700780 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
781 const uint16_t* insns = code_item_->insns_ + cur_offset;
782 const uint16_t* array_data;
783 int32_t array_data_offset;
784
785 DCHECK_LT(cur_offset, insn_count);
786 /* make sure the start of the array data table is in range */
787 array_data_offset = insns[1] | (((int32_t) insns[2]) << 16);
788 if ((int32_t) cur_offset + array_data_offset < 0 ||
789 cur_offset + array_data_offset + 2 >= insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -0700790 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data start: at " << cur_offset
Brian Carlstrom93c33962013-07-26 10:37:43 -0700791 << ", data offset " << array_data_offset
792 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -0700793 return false;
794 }
795 /* offset to array data table is a relative branch-style offset */
796 array_data = insns + array_data_offset;
797 /* make sure the table is 32-bit aligned */
798 if ((((uint32_t) array_data) & 0x03) != 0) {
jeffhaod5347e02012-03-22 17:25:05 -0700799 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned array data table: at " << cur_offset
800 << ", data offset " << array_data_offset;
Ian Rogersd81871c2011-10-03 13:57:23 -0700801 return false;
802 }
803 uint32_t value_width = array_data[1];
Elliott Hughes398f64b2012-03-26 18:05:48 -0700804 uint32_t value_count = *reinterpret_cast<const uint32_t*>(&array_data[2]);
Ian Rogersd81871c2011-10-03 13:57:23 -0700805 uint32_t table_size = 4 + (value_width * value_count + 1) / 2;
806 /* make sure the end of the switch is in range */
807 if (cur_offset + array_data_offset + table_size > insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -0700808 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data end: at " << cur_offset
809 << ", data offset " << array_data_offset << ", end "
810 << cur_offset + array_data_offset + table_size
811 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -0700812 return false;
813 }
814 return true;
815}
816
Ian Rogers776ac1f2012-04-13 23:36:36 -0700817bool MethodVerifier::CheckBranchTarget(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700818 int32_t offset;
819 bool isConditional, selfOkay;
820 if (!GetBranchOffset(cur_offset, &offset, &isConditional, &selfOkay)) {
821 return false;
822 }
823 if (!selfOkay && offset == 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700824 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch offset of zero not allowed at"
825 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -0700826 return false;
827 }
Elliott Hughes81ff3182012-03-23 20:35:56 -0700828 // Check for 32-bit overflow. This isn't strictly necessary if we can depend on the runtime
829 // to have identical "wrap-around" behavior, but it's unwise to depend on that.
Ian Rogersd81871c2011-10-03 13:57:23 -0700830 if (((int64_t) cur_offset + (int64_t) offset) != (int64_t) (cur_offset + offset)) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700831 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch target overflow "
832 << reinterpret_cast<void*>(cur_offset) << " +" << offset;
Ian Rogersd81871c2011-10-03 13:57:23 -0700833 return false;
834 }
835 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
836 int32_t abs_offset = cur_offset + offset;
Brian Carlstrom93c33962013-07-26 10:37:43 -0700837 if (abs_offset < 0 ||
838 (uint32_t) abs_offset >= insn_count ||
839 !insn_flags_[abs_offset].IsOpcode()) {
jeffhaod5347e02012-03-22 17:25:05 -0700840 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid branch target " << offset << " (-> "
Elliott Hughes398f64b2012-03-26 18:05:48 -0700841 << reinterpret_cast<void*>(abs_offset) << ") at "
842 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -0700843 return false;
844 }
845 insn_flags_[abs_offset].SetBranchTarget();
846 return true;
847}
848
Ian Rogers776ac1f2012-04-13 23:36:36 -0700849bool MethodVerifier::GetBranchOffset(uint32_t cur_offset, int32_t* pOffset, bool* pConditional,
Ian Rogersd81871c2011-10-03 13:57:23 -0700850 bool* selfOkay) {
851 const uint16_t* insns = code_item_->insns_ + cur_offset;
852 *pConditional = false;
853 *selfOkay = false;
jeffhaoba5ebb92011-08-25 17:24:37 -0700854 switch (*insns & 0xff) {
855 case Instruction::GOTO:
856 *pOffset = ((int16_t) *insns) >> 8;
jeffhaoba5ebb92011-08-25 17:24:37 -0700857 break;
858 case Instruction::GOTO_32:
859 *pOffset = insns[1] | (((uint32_t) insns[2]) << 16);
jeffhaoba5ebb92011-08-25 17:24:37 -0700860 *selfOkay = true;
861 break;
862 case Instruction::GOTO_16:
863 *pOffset = (int16_t) insns[1];
jeffhaoba5ebb92011-08-25 17:24:37 -0700864 break;
865 case Instruction::IF_EQ:
866 case Instruction::IF_NE:
867 case Instruction::IF_LT:
868 case Instruction::IF_GE:
869 case Instruction::IF_GT:
870 case Instruction::IF_LE:
871 case Instruction::IF_EQZ:
872 case Instruction::IF_NEZ:
873 case Instruction::IF_LTZ:
874 case Instruction::IF_GEZ:
875 case Instruction::IF_GTZ:
876 case Instruction::IF_LEZ:
877 *pOffset = (int16_t) insns[1];
878 *pConditional = true;
jeffhaoba5ebb92011-08-25 17:24:37 -0700879 break;
880 default:
881 return false;
882 break;
883 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700884 return true;
885}
886
Ian Rogers776ac1f2012-04-13 23:36:36 -0700887bool MethodVerifier::CheckSwitchTargets(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700888 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700889 DCHECK_LT(cur_offset, insn_count);
Ian Rogersd81871c2011-10-03 13:57:23 -0700890 const uint16_t* insns = code_item_->insns_ + cur_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -0700891 /* make sure the start of the switch is in range */
Ian Rogersd81871c2011-10-03 13:57:23 -0700892 int32_t switch_offset = insns[1] | ((int32_t) insns[2]) << 16;
893 if ((int32_t) cur_offset + switch_offset < 0 || cur_offset + switch_offset + 2 >= insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -0700894 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch start: at " << cur_offset
Brian Carlstrom93c33962013-07-26 10:37:43 -0700895 << ", switch offset " << switch_offset
896 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -0700897 return false;
898 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700899 /* offset to switch table is a relative branch-style offset */
Ian Rogersd81871c2011-10-03 13:57:23 -0700900 const uint16_t* switch_insns = insns + switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -0700901 /* make sure the table is 32-bit aligned */
902 if ((((uint32_t) switch_insns) & 0x03) != 0) {
jeffhaod5347e02012-03-22 17:25:05 -0700903 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned switch table: at " << cur_offset
904 << ", switch offset " << switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -0700905 return false;
906 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700907 uint32_t switch_count = switch_insns[1];
908 int32_t keys_offset, targets_offset;
909 uint16_t expected_signature;
jeffhaoba5ebb92011-08-25 17:24:37 -0700910 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
911 /* 0=sig, 1=count, 2/3=firstKey */
912 targets_offset = 4;
913 keys_offset = -1;
914 expected_signature = Instruction::kPackedSwitchSignature;
915 } else {
916 /* 0=sig, 1=count, 2..count*2 = keys */
917 keys_offset = 2;
918 targets_offset = 2 + 2 * switch_count;
919 expected_signature = Instruction::kSparseSwitchSignature;
920 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700921 uint32_t table_size = targets_offset + switch_count * 2;
jeffhaoba5ebb92011-08-25 17:24:37 -0700922 if (switch_insns[0] != expected_signature) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700923 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
924 << StringPrintf("wrong signature for switch table (%x, wanted %x)",
925 switch_insns[0], expected_signature);
jeffhaoba5ebb92011-08-25 17:24:37 -0700926 return false;
927 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700928 /* make sure the end of the switch is in range */
929 if (cur_offset + switch_offset + table_size > (uint32_t) insn_count) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700930 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch end: at " << cur_offset
931 << ", switch offset " << switch_offset
932 << ", end " << (cur_offset + switch_offset + table_size)
jeffhaod5347e02012-03-22 17:25:05 -0700933 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -0700934 return false;
935 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700936 /* for a sparse switch, verify the keys are in ascending order */
937 if (keys_offset > 0 && switch_count > 1) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700938 int32_t last_key = switch_insns[keys_offset] | (switch_insns[keys_offset + 1] << 16);
939 for (uint32_t targ = 1; targ < switch_count; targ++) {
jeffhaoba5ebb92011-08-25 17:24:37 -0700940 int32_t key = (int32_t) switch_insns[keys_offset + targ * 2] |
941 (int32_t) (switch_insns[keys_offset + targ * 2 + 1] << 16);
942 if (key <= last_key) {
jeffhaod5347e02012-03-22 17:25:05 -0700943 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid packed switch: last key=" << last_key
944 << ", this=" << key;
jeffhaoba5ebb92011-08-25 17:24:37 -0700945 return false;
946 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700947 last_key = key;
948 }
949 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700950 /* verify each switch target */
Ian Rogersd81871c2011-10-03 13:57:23 -0700951 for (uint32_t targ = 0; targ < switch_count; targ++) {
952 int32_t offset = (int32_t) switch_insns[targets_offset + targ * 2] |
953 (int32_t) (switch_insns[targets_offset + targ * 2 + 1] << 16);
954 int32_t abs_offset = cur_offset + offset;
Brian Carlstrom93c33962013-07-26 10:37:43 -0700955 if (abs_offset < 0 ||
956 abs_offset >= (int32_t) insn_count ||
957 !insn_flags_[abs_offset].IsOpcode()) {
958 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch target " << offset
959 << " (-> " << reinterpret_cast<void*>(abs_offset) << ") at "
960 << reinterpret_cast<void*>(cur_offset)
961 << "[" << targ << "]";
jeffhaoba5ebb92011-08-25 17:24:37 -0700962 return false;
963 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700964 insn_flags_[abs_offset].SetBranchTarget();
965 }
966 return true;
967}
968
Ian Rogers776ac1f2012-04-13 23:36:36 -0700969bool MethodVerifier::CheckVarArgRegs(uint32_t vA, uint32_t arg[]) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700970 if (vA > 5) {
jeffhaod5347e02012-03-22 17:25:05 -0700971 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << vA << ") in non-range invoke)";
Ian Rogersd81871c2011-10-03 13:57:23 -0700972 return false;
973 }
974 uint16_t registers_size = code_item_->registers_size_;
975 for (uint32_t idx = 0; idx < vA; idx++) {
jeffhao457cc512012-02-02 16:55:13 -0800976 if (arg[idx] >= registers_size) {
jeffhaod5347e02012-03-22 17:25:05 -0700977 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index (" << arg[idx]
978 << ") in non-range invoke (>= " << registers_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700979 return false;
980 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700981 }
982
983 return true;
984}
985
Ian Rogers776ac1f2012-04-13 23:36:36 -0700986bool MethodVerifier::CheckVarArgRangeRegs(uint32_t vA, uint32_t vC) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700987 uint16_t registers_size = code_item_->registers_size_;
988 // vA/vC are unsigned 8-bit/16-bit quantities for /range instructions, so there's no risk of
989 // integer overflow when adding them here.
990 if (vA + vC > registers_size) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700991 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index " << vA << "+" << vC
992 << " in range invoke (> " << registers_size << ")";
jeffhaoba5ebb92011-08-25 17:24:37 -0700993 return false;
994 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700995 return true;
996}
997
Brian Carlstrom93c33962013-07-26 10:37:43 -0700998static const std::vector<uint8_t>* CreateLengthPrefixedDexGcMap(
999 const std::vector<uint8_t>& gc_map) {
Brian Carlstrom75412882012-01-18 01:26:54 -08001000 std::vector<uint8_t>* length_prefixed_gc_map = new std::vector<uint8_t>;
Ian Rogers637c65b2013-05-31 11:46:00 -07001001 length_prefixed_gc_map->reserve(gc_map.size() + 4);
Brian Carlstrom75412882012-01-18 01:26:54 -08001002 length_prefixed_gc_map->push_back((gc_map.size() & 0xff000000) >> 24);
1003 length_prefixed_gc_map->push_back((gc_map.size() & 0x00ff0000) >> 16);
1004 length_prefixed_gc_map->push_back((gc_map.size() & 0x0000ff00) >> 8);
1005 length_prefixed_gc_map->push_back((gc_map.size() & 0x000000ff) >> 0);
1006 length_prefixed_gc_map->insert(length_prefixed_gc_map->end(),
1007 gc_map.begin(),
1008 gc_map.end());
1009 DCHECK_EQ(gc_map.size() + 4, length_prefixed_gc_map->size());
1010 DCHECK_EQ(gc_map.size(),
1011 static_cast<size_t>((length_prefixed_gc_map->at(0) << 24) |
1012 (length_prefixed_gc_map->at(1) << 16) |
1013 (length_prefixed_gc_map->at(2) << 8) |
1014 (length_prefixed_gc_map->at(3) << 0)));
1015 return length_prefixed_gc_map;
1016}
1017
Ian Rogers776ac1f2012-04-13 23:36:36 -07001018bool MethodVerifier::VerifyCodeFlow() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001019 uint16_t registers_size = code_item_->registers_size_;
1020 uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaobdb76512011-09-07 11:43:16 -07001021
Ian Rogersd81871c2011-10-03 13:57:23 -07001022 if (registers_size * insns_size > 4*1024*1024) {
buzbee4922ef92012-02-24 14:32:20 -08001023 LOG(WARNING) << "warning: method is huge (regs=" << registers_size
1024 << " insns_size=" << insns_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001025 }
1026 /* Create and initialize table holding register status */
Brian Carlstrom93c33962013-07-26 10:37:43 -07001027 reg_table_.Init(kTrackCompilerInterestPoints,
1028 insn_flags_.get(),
1029 insns_size,
1030 registers_size,
1031 this);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07001032
jeffhaobdb76512011-09-07 11:43:16 -07001033
Ian Rogersd81871c2011-10-03 13:57:23 -07001034 work_line_.reset(new RegisterLine(registers_size, this));
1035 saved_line_.reset(new RegisterLine(registers_size, this));
jeffhaobdb76512011-09-07 11:43:16 -07001036
Ian Rogersd81871c2011-10-03 13:57:23 -07001037 /* Initialize register types of method arguments. */
1038 if (!SetTypesFromSignature()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001039 DCHECK_NE(failures_.size(), 0U);
1040 std::string prepend("Bad signature in ");
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001041 prepend += PrettyMethod(dex_method_idx_, *dex_file_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001042 PrependToLastFailMessage(prepend);
Ian Rogersd81871c2011-10-03 13:57:23 -07001043 return false;
1044 }
1045 /* Perform code flow verification. */
1046 if (!CodeFlowVerifyMethod()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001047 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -07001048 return false;
jeffhaobdb76512011-09-07 11:43:16 -07001049 }
1050
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02001051 // Compute information for compiler.
1052 if (Runtime::Current()->IsCompiler()) {
1053 MethodReference ref(dex_file_, dex_method_idx_);
1054 bool compile = IsCandidateForCompilation(code_item_, method_access_flags_);
1055 if (compile) {
1056 /* Generate a register map and add it to the method. */
1057 UniquePtr<const std::vector<uint8_t> > map(GenerateGcMap());
1058 if (map.get() == NULL) {
1059 DCHECK_NE(failures_.size(), 0U);
1060 return false; // Not a real failure, but a failure to encode
1061 }
1062 if (kIsDebugBuild) {
1063 VerifyGcMap(*map);
1064 }
1065 const std::vector<uint8_t>* dex_gc_map = CreateLengthPrefixedDexGcMap(*(map.get()));
1066 verifier::MethodVerifier::SetDexGcMap(ref, *dex_gc_map);
1067 }
Logan Chiendd361c92012-04-10 23:40:37 +08001068
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02001069 if (has_check_casts_) {
1070 MethodVerifier::MethodSafeCastSet* method_to_safe_casts = GenerateSafeCastSet();
Brian Carlstrom93c33962013-07-26 10:37:43 -07001071 if (method_to_safe_casts != NULL) {
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02001072 SetSafeCastMap(ref, method_to_safe_casts);
1073 }
1074 }
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07001075
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02001076 if (has_virtual_or_interface_invokes_) {
1077 MethodVerifier::PcToConcreteMethodMap* pc_to_concrete_method = GenerateDevirtMap();
Brian Carlstrom93c33962013-07-26 10:37:43 -07001078 if (pc_to_concrete_method != NULL) {
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02001079 SetDevirtMap(ref, pc_to_concrete_method);
1080 }
1081 }
Sameer Abu Asal02c42232013-04-30 12:09:45 -07001082 }
jeffhaobdb76512011-09-07 11:43:16 -07001083 return true;
1084}
1085
Ian Rogersad0b3a32012-04-16 14:50:24 -07001086std::ostream& MethodVerifier::DumpFailures(std::ostream& os) {
1087 DCHECK_EQ(failures_.size(), failure_messages_.size());
1088 for (size_t i = 0; i < failures_.size(); ++i) {
Elliott Hughesc073b072012-05-24 19:29:17 -07001089 os << failure_messages_[i]->str() << "\n";
Ian Rogersad0b3a32012-04-16 14:50:24 -07001090 }
1091 return os;
1092}
1093
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001094extern "C" void MethodVerifierGdbDump(MethodVerifier* v)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001095 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001096 v->Dump(std::cerr);
1097}
1098
Ian Rogers776ac1f2012-04-13 23:36:36 -07001099void MethodVerifier::Dump(std::ostream& os) {
jeffhaof56197c2012-03-05 18:01:54 -08001100 if (code_item_ == NULL) {
Elliott Hughesc073b072012-05-24 19:29:17 -07001101 os << "Native method\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001102 return;
jeffhaobdb76512011-09-07 11:43:16 -07001103 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001104 {
1105 os << "Register Types:\n";
1106 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
1107 std::ostream indent_os(&indent_filter);
1108 reg_types_.Dump(indent_os);
1109 }
Ian Rogersb4903572012-10-11 11:52:56 -07001110 os << "Dumping instructions and register lines:\n";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001111 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
1112 std::ostream indent_os(&indent_filter);
Ian Rogersd81871c2011-10-03 13:57:23 -07001113 const Instruction* inst = Instruction::At(code_item_->insns_);
1114 for (size_t dex_pc = 0; dex_pc < code_item_->insns_size_in_code_units_;
1115 dex_pc += insn_flags_[dex_pc].GetLengthInCodeUnits()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001116 RegisterLine* reg_line = reg_table_.GetLine(dex_pc);
1117 if (reg_line != NULL) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001118 indent_os << reg_line->Dump() << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07001119 }
Ian Rogers7b3ddd22013-02-21 15:19:52 -08001120 indent_os << StringPrintf("0x%04zx", dex_pc) << ": " << insn_flags_[dex_pc].ToString() << " ";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001121 const bool kDumpHexOfInstruction = false;
1122 if (kDumpHexOfInstruction) {
1123 indent_os << inst->DumpHex(5) << " ";
1124 }
1125 indent_os << inst->DumpString(dex_file_) << "\n";
jeffhaoba5ebb92011-08-25 17:24:37 -07001126 inst = inst->Next();
1127 }
jeffhaobdb76512011-09-07 11:43:16 -07001128}
1129
Ian Rogersd81871c2011-10-03 13:57:23 -07001130static bool IsPrimitiveDescriptor(char descriptor) {
1131 switch (descriptor) {
jeffhaobdb76512011-09-07 11:43:16 -07001132 case 'I':
1133 case 'C':
1134 case 'S':
1135 case 'B':
1136 case 'Z':
jeffhaobdb76512011-09-07 11:43:16 -07001137 case 'F':
1138 case 'D':
1139 case 'J':
Ian Rogersd81871c2011-10-03 13:57:23 -07001140 return true;
jeffhaobdb76512011-09-07 11:43:16 -07001141 default:
1142 return false;
1143 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001144}
1145
Ian Rogers776ac1f2012-04-13 23:36:36 -07001146bool MethodVerifier::SetTypesFromSignature() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001147 RegisterLine* reg_line = reg_table_.GetLine(0);
1148 int arg_start = code_item_->registers_size_ - code_item_->ins_size_;
1149 size_t expected_args = code_item_->ins_size_; /* long/double count as two */
jeffhaobdb76512011-09-07 11:43:16 -07001150
Ian Rogersd81871c2011-10-03 13:57:23 -07001151 DCHECK_GE(arg_start, 0); /* should have been verified earlier */
1152 //Include the "this" pointer.
1153 size_t cur_arg = 0;
Ian Rogersad0b3a32012-04-16 14:50:24 -07001154 if (!IsStatic()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001155 // If this is a constructor for a class other than java.lang.Object, mark the first ("this")
1156 // argument as uninitialized. This restricts field access until the superclass constructor is
1157 // called.
Ian Rogersad0b3a32012-04-16 14:50:24 -07001158 const RegType& declaring_class = GetDeclaringClass();
1159 if (IsConstructor() && !declaring_class.IsJavaLangObject()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001160 reg_line->SetRegisterType(arg_start + cur_arg,
1161 reg_types_.UninitializedThisArgument(declaring_class));
1162 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001163 reg_line->SetRegisterType(arg_start + cur_arg, declaring_class);
jeffhaobdb76512011-09-07 11:43:16 -07001164 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001165 cur_arg++;
jeffhaobdb76512011-09-07 11:43:16 -07001166 }
1167
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001168 const DexFile::ProtoId& proto_id =
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001169 dex_file_->GetMethodPrototype(dex_file_->GetMethodId(dex_method_idx_));
Ian Rogers0571d352011-11-03 19:51:38 -07001170 DexFileParameterIterator iterator(*dex_file_, proto_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07001171
1172 for (; iterator.HasNext(); iterator.Next()) {
1173 const char* descriptor = iterator.GetDescriptor();
1174 if (descriptor == NULL) {
1175 LOG(FATAL) << "Null descriptor";
1176 }
1177 if (cur_arg >= expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07001178 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1179 << " args, found more (" << descriptor << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001180 return false;
1181 }
1182 switch (descriptor[0]) {
1183 case 'L':
1184 case '[':
1185 // We assume that reference arguments are initialized. The only way it could be otherwise
1186 // (assuming the caller was verified) is if the current method is <init>, but in that case
1187 // it's effectively considered initialized the instant we reach here (in the sense that we
1188 // can return without doing anything or call virtual methods).
1189 {
Ian Rogersb4903572012-10-11 11:52:56 -07001190 const RegType& reg_type = reg_types_.FromDescriptor(class_loader_, descriptor, false);
Ian Rogers84fa0742011-10-25 18:13:30 -07001191 reg_line->SetRegisterType(arg_start + cur_arg, reg_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001192 }
1193 break;
1194 case 'Z':
1195 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Boolean());
1196 break;
1197 case 'C':
1198 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Char());
1199 break;
1200 case 'B':
1201 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Byte());
1202 break;
1203 case 'I':
1204 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Integer());
1205 break;
1206 case 'S':
1207 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Short());
1208 break;
1209 case 'F':
1210 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Float());
1211 break;
1212 case 'J':
1213 case 'D': {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001214 const RegType& lo_half = descriptor[0] == 'J' ? reg_types_.LongLo() : reg_types_.DoubleLo();
1215 const RegType& hi_half = descriptor[0] == 'J' ? reg_types_.LongHi() : reg_types_.DoubleHi();
1216 reg_line->SetRegisterTypeWide(arg_start + cur_arg, lo_half, hi_half);
Ian Rogersd81871c2011-10-03 13:57:23 -07001217 cur_arg++;
1218 break;
1219 }
1220 default:
Brian Carlstrom93c33962013-07-26 10:37:43 -07001221 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected signature type char '"
1222 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001223 return false;
1224 }
1225 cur_arg++;
1226 }
1227 if (cur_arg != expected_args) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001228 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1229 << " arguments, found " << cur_arg;
Ian Rogersd81871c2011-10-03 13:57:23 -07001230 return false;
1231 }
1232 const char* descriptor = dex_file_->GetReturnTypeDescriptor(proto_id);
1233 // Validate return type. We don't do the type lookup; just want to make sure that it has the right
1234 // format. Only major difference from the method argument format is that 'V' is supported.
1235 bool result;
1236 if (IsPrimitiveDescriptor(descriptor[0]) || descriptor[0] == 'V') {
1237 result = descriptor[1] == '\0';
1238 } else if (descriptor[0] == '[') { // single/multi-dimensional array of object/primitive
1239 size_t i = 0;
1240 do {
1241 i++;
1242 } while (descriptor[i] == '['); // process leading [
1243 if (descriptor[i] == 'L') { // object array
1244 do {
1245 i++; // find closing ;
1246 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1247 result = descriptor[i] == ';';
1248 } else { // primitive array
1249 result = IsPrimitiveDescriptor(descriptor[i]) && descriptor[i + 1] == '\0';
1250 }
1251 } else if (descriptor[0] == 'L') {
1252 // could be more thorough here, but shouldn't be required
1253 size_t i = 0;
1254 do {
1255 i++;
1256 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1257 result = descriptor[i] == ';';
1258 } else {
1259 result = false;
1260 }
1261 if (!result) {
jeffhaod5347e02012-03-22 17:25:05 -07001262 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected char in return type descriptor '"
1263 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001264 }
1265 return result;
jeffhaobdb76512011-09-07 11:43:16 -07001266}
1267
Ian Rogers776ac1f2012-04-13 23:36:36 -07001268bool MethodVerifier::CodeFlowVerifyMethod() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001269 const uint16_t* insns = code_item_->insns_;
1270 const uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaoba5ebb92011-08-25 17:24:37 -07001271
jeffhaobdb76512011-09-07 11:43:16 -07001272 /* Begin by marking the first instruction as "changed". */
Ian Rogersd81871c2011-10-03 13:57:23 -07001273 insn_flags_[0].SetChanged();
1274 uint32_t start_guess = 0;
jeffhaoba5ebb92011-08-25 17:24:37 -07001275
jeffhaobdb76512011-09-07 11:43:16 -07001276 /* Continue until no instructions are marked "changed". */
1277 while (true) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001278 // Find the first marked one. Use "start_guess" as a way to find one quickly.
1279 uint32_t insn_idx = start_guess;
1280 for (; insn_idx < insns_size; insn_idx++) {
1281 if (insn_flags_[insn_idx].IsChanged())
jeffhaobdb76512011-09-07 11:43:16 -07001282 break;
1283 }
jeffhaobdb76512011-09-07 11:43:16 -07001284 if (insn_idx == insns_size) {
1285 if (start_guess != 0) {
1286 /* try again, starting from the top */
1287 start_guess = 0;
1288 continue;
1289 } else {
1290 /* all flags are clear */
1291 break;
1292 }
1293 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001294 // We carry the working set of registers from instruction to instruction. If this address can
1295 // be the target of a branch (or throw) instruction, or if we're skipping around chasing
1296 // "changed" flags, we need to load the set of registers from the table.
1297 // Because we always prefer to continue on to the next instruction, we should never have a
1298 // situation where we have a stray "changed" flag set on an instruction that isn't a branch
1299 // target.
1300 work_insn_idx_ = insn_idx;
1301 if (insn_flags_[insn_idx].IsBranchTarget()) {
1302 work_line_->CopyFromLine(reg_table_.GetLine(insn_idx));
jeffhaobdb76512011-09-07 11:43:16 -07001303 } else {
1304#ifndef NDEBUG
1305 /*
1306 * Sanity check: retrieve the stored register line (assuming
1307 * a full table) and make sure it actually matches.
1308 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001309 RegisterLine* register_line = reg_table_.GetLine(insn_idx);
1310 if (register_line != NULL) {
1311 if (work_line_->CompareLine(register_line) != 0) {
1312 Dump(std::cout);
1313 std::cout << info_messages_.str();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001314 LOG(FATAL) << "work_line diverged in " << PrettyMethod(dex_method_idx_, *dex_file_)
Elliott Hughesc073b072012-05-24 19:29:17 -07001315 << "@" << reinterpret_cast<void*>(work_insn_idx_) << "\n"
1316 << " work_line=" << *work_line_ << "\n"
Elliott Hughes398f64b2012-03-26 18:05:48 -07001317 << " expected=" << *register_line;
Ian Rogersd81871c2011-10-03 13:57:23 -07001318 }
jeffhaobdb76512011-09-07 11:43:16 -07001319 }
1320#endif
1321 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001322 if (!CodeFlowVerifyInstruction(&start_guess)) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001323 std::string prepend(PrettyMethod(dex_method_idx_, *dex_file_));
Ian Rogersad0b3a32012-04-16 14:50:24 -07001324 prepend += " failed to verify: ";
1325 PrependToLastFailMessage(prepend);
jeffhaoba5ebb92011-08-25 17:24:37 -07001326 return false;
1327 }
jeffhaobdb76512011-09-07 11:43:16 -07001328 /* Clear "changed" and mark as visited. */
Ian Rogersd81871c2011-10-03 13:57:23 -07001329 insn_flags_[insn_idx].SetVisited();
1330 insn_flags_[insn_idx].ClearChanged();
jeffhaobdb76512011-09-07 11:43:16 -07001331 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001332
Ian Rogers1c849e52012-06-28 14:00:33 -07001333 if (gDebugVerify) {
jeffhaobdb76512011-09-07 11:43:16 -07001334 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001335 * Scan for dead code. There's nothing "evil" about dead code
jeffhaobdb76512011-09-07 11:43:16 -07001336 * (besides the wasted space), but it indicates a flaw somewhere
1337 * down the line, possibly in the verifier.
1338 *
1339 * If we've substituted "always throw" instructions into the stream,
1340 * we are almost certainly going to have some dead code.
1341 */
1342 int dead_start = -1;
Ian Rogersd81871c2011-10-03 13:57:23 -07001343 uint32_t insn_idx = 0;
1344 for (; insn_idx < insns_size; insn_idx += insn_flags_[insn_idx].GetLengthInCodeUnits()) {
jeffhaobdb76512011-09-07 11:43:16 -07001345 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001346 * Switch-statement data doesn't get "visited" by scanner. It
jeffhaobdb76512011-09-07 11:43:16 -07001347 * may or may not be preceded by a padding NOP (for alignment).
1348 */
1349 if (insns[insn_idx] == Instruction::kPackedSwitchSignature ||
1350 insns[insn_idx] == Instruction::kSparseSwitchSignature ||
1351 insns[insn_idx] == Instruction::kArrayDataSignature ||
Elliott Hughes380aaa72012-07-09 14:33:15 -07001352 (insns[insn_idx] == Instruction::NOP && (insn_idx + 1 < insns_size) &&
jeffhaobdb76512011-09-07 11:43:16 -07001353 (insns[insn_idx + 1] == Instruction::kPackedSwitchSignature ||
1354 insns[insn_idx + 1] == Instruction::kSparseSwitchSignature ||
1355 insns[insn_idx + 1] == Instruction::kArrayDataSignature))) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001356 insn_flags_[insn_idx].SetVisited();
jeffhaobdb76512011-09-07 11:43:16 -07001357 }
1358
Ian Rogersd81871c2011-10-03 13:57:23 -07001359 if (!insn_flags_[insn_idx].IsVisited()) {
jeffhaobdb76512011-09-07 11:43:16 -07001360 if (dead_start < 0)
1361 dead_start = insn_idx;
1362 } else 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);
jeffhaobdb76512011-09-07 11:43:16 -07001365 dead_start = -1;
1366 }
1367 }
1368 if (dead_start >= 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001369 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start)
1370 << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaoba5ebb92011-08-25 17:24:37 -07001371 }
Ian Rogersc9e463c2013-06-05 16:52:26 -07001372 // To dump the state of the verify after a method, do something like:
1373 // if (PrettyMethod(dex_method_idx_, *dex_file_) ==
1374 // "boolean java.lang.String.equals(java.lang.Object)") {
1375 // LOG(INFO) << info_messages_.str();
1376 // }
jeffhaoba5ebb92011-08-25 17:24:37 -07001377 }
jeffhaobdb76512011-09-07 11:43:16 -07001378 return true;
1379}
1380
Ian Rogers776ac1f2012-04-13 23:36:36 -07001381bool MethodVerifier::CodeFlowVerifyInstruction(uint32_t* start_guess) {
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001382 // If we're doing FindLocksAtDexPc, check whether we're at the dex pc we care about.
1383 // We want the state _before_ the instruction, for the case where the dex pc we're
1384 // interested in is itself a monitor-enter instruction (which is a likely place
1385 // for a thread to be suspended).
1386 if (monitor_enter_dex_pcs_ != NULL && work_insn_idx_ == interesting_dex_pc_) {
Elliott Hughes4993bbc2013-01-10 15:41:25 -08001387 monitor_enter_dex_pcs_->clear(); // The new work line is more accurate than the previous one.
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001388 for (size_t i = 0; i < work_line_->GetMonitorEnterCount(); ++i) {
1389 monitor_enter_dex_pcs_->push_back(work_line_->GetMonitorEnterDexPc(i));
1390 }
1391 }
1392
jeffhaobdb76512011-09-07 11:43:16 -07001393 /*
1394 * Once we finish decoding the instruction, we need to figure out where
jeffhaod1f0fde2011-09-08 17:25:33 -07001395 * we can go from here. There are three possible ways to transfer
jeffhaobdb76512011-09-07 11:43:16 -07001396 * control to another statement:
1397 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001398 * (1) Continue to the next instruction. Applies to all but
jeffhaobdb76512011-09-07 11:43:16 -07001399 * unconditional branches, method returns, and exception throws.
jeffhaod1f0fde2011-09-08 17:25:33 -07001400 * (2) Branch to one or more possible locations. Applies to branches
jeffhaobdb76512011-09-07 11:43:16 -07001401 * and switch statements.
jeffhaod1f0fde2011-09-08 17:25:33 -07001402 * (3) Exception handlers. Applies to any instruction that can
jeffhaobdb76512011-09-07 11:43:16 -07001403 * throw an exception that is handled by an encompassing "try"
1404 * block.
1405 *
1406 * We can also return, in which case there is no successor instruction
1407 * from this point.
1408 *
Elliott Hughesadb8c672012-03-06 16:49:32 -08001409 * The behavior can be determined from the opcode flags.
jeffhaobdb76512011-09-07 11:43:16 -07001410 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001411 const uint16_t* insns = code_item_->insns_ + work_insn_idx_;
1412 const Instruction* inst = Instruction::At(insns);
Ian Rogersa75a0132012-09-28 11:41:42 -07001413 int opcode_flags = Instruction::FlagsOf(inst->Opcode());
jeffhaobdb76512011-09-07 11:43:16 -07001414
jeffhaobdb76512011-09-07 11:43:16 -07001415 int32_t branch_target = 0;
jeffhaobdb76512011-09-07 11:43:16 -07001416 bool just_set_result = false;
Ian Rogers2c8a8572011-10-24 17:11:36 -07001417 if (gDebugVerify) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001418 // Generate processing back trace to debug verifier
Elliott Hughesc073b072012-05-24 19:29:17 -07001419 LogVerifyInfo() << "Processing " << inst->DumpString(dex_file_) << "\n"
1420 << *work_line_.get() << "\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001421 }
jeffhaobdb76512011-09-07 11:43:16 -07001422
1423 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001424 * Make a copy of the previous register state. If the instruction
jeffhaobdb76512011-09-07 11:43:16 -07001425 * can throw an exception, we will copy/merge this into the "catch"
1426 * address rather than work_line, because we don't want the result
1427 * from the "successful" code path (e.g. a check-cast that "improves"
1428 * a type) to be visible to the exception handler.
1429 */
Ian Rogers776ac1f2012-04-13 23:36:36 -07001430 if ((opcode_flags & Instruction::kThrow) != 0 && CurrentInsnFlags()->IsInTry()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001431 saved_line_->CopyFromLine(work_line_.get());
jeffhaobdb76512011-09-07 11:43:16 -07001432 } else {
1433#ifndef NDEBUG
Ian Rogersd81871c2011-10-03 13:57:23 -07001434 saved_line_->FillWithGarbage();
jeffhaobdb76512011-09-07 11:43:16 -07001435#endif
1436 }
1437
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07001438
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001439 // We need to ensure the work line is consistent while performing validation. When we spot a
1440 // peephole pattern we compute a new line for either the fallthrough instruction or the
1441 // branch target.
1442 UniquePtr<RegisterLine> branch_line;
1443 UniquePtr<RegisterLine> fallthrough_line;
1444
Sebastien Hertz5243e912013-05-21 10:55:07 +02001445 switch (inst->Opcode()) {
jeffhaobdb76512011-09-07 11:43:16 -07001446 case Instruction::NOP:
1447 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001448 * A "pure" NOP has no effect on anything. Data tables start with
jeffhaobdb76512011-09-07 11:43:16 -07001449 * a signature that looks like a NOP; if we see one of these in
1450 * the course of executing code then we have a problem.
1451 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001452 if (inst->VRegA_10x() != 0) {
jeffhaod5347e02012-03-22 17:25:05 -07001453 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "encountered data table in instruction stream";
jeffhaobdb76512011-09-07 11:43:16 -07001454 }
1455 break;
1456
1457 case Instruction::MOVE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001458 work_line_->CopyRegister1(inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategory1nr);
1459 break;
jeffhaobdb76512011-09-07 11:43:16 -07001460 case Instruction::MOVE_FROM16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001461 work_line_->CopyRegister1(inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategory1nr);
1462 break;
jeffhaobdb76512011-09-07 11:43:16 -07001463 case Instruction::MOVE_16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001464 work_line_->CopyRegister1(inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategory1nr);
jeffhaobdb76512011-09-07 11:43:16 -07001465 break;
1466 case Instruction::MOVE_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001467 work_line_->CopyRegister2(inst->VRegA_12x(), inst->VRegB_12x());
1468 break;
jeffhaobdb76512011-09-07 11:43:16 -07001469 case Instruction::MOVE_WIDE_FROM16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001470 work_line_->CopyRegister2(inst->VRegA_22x(), inst->VRegB_22x());
1471 break;
jeffhaobdb76512011-09-07 11:43:16 -07001472 case Instruction::MOVE_WIDE_16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001473 work_line_->CopyRegister2(inst->VRegA_32x(), inst->VRegB_32x());
jeffhaobdb76512011-09-07 11:43:16 -07001474 break;
1475 case Instruction::MOVE_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001476 work_line_->CopyRegister1(inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategoryRef);
1477 break;
jeffhaobdb76512011-09-07 11:43:16 -07001478 case Instruction::MOVE_OBJECT_FROM16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001479 work_line_->CopyRegister1(inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategoryRef);
1480 break;
jeffhaobdb76512011-09-07 11:43:16 -07001481 case Instruction::MOVE_OBJECT_16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001482 work_line_->CopyRegister1(inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategoryRef);
jeffhaobdb76512011-09-07 11:43:16 -07001483 break;
1484
1485 /*
1486 * The move-result instructions copy data out of a "pseudo-register"
jeffhaod1f0fde2011-09-08 17:25:33 -07001487 * with the results from the last method invocation. In practice we
jeffhaobdb76512011-09-07 11:43:16 -07001488 * might want to hold the result in an actual CPU register, so the
1489 * Dalvik spec requires that these only appear immediately after an
1490 * invoke or filled-new-array.
1491 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001492 * These calls invalidate the "result" register. (This is now
jeffhaobdb76512011-09-07 11:43:16 -07001493 * redundant with the reset done below, but it can make the debug info
1494 * easier to read in some cases.)
1495 */
1496 case Instruction::MOVE_RESULT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001497 work_line_->CopyResultRegister1(inst->VRegA_11x(), false);
jeffhaobdb76512011-09-07 11:43:16 -07001498 break;
1499 case Instruction::MOVE_RESULT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001500 work_line_->CopyResultRegister2(inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07001501 break;
1502 case Instruction::MOVE_RESULT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001503 work_line_->CopyResultRegister1(inst->VRegA_11x(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001504 break;
1505
Ian Rogersd81871c2011-10-03 13:57:23 -07001506 case Instruction::MOVE_EXCEPTION: {
jeffhaobdb76512011-09-07 11:43:16 -07001507 /*
jeffhao60f83e32012-02-13 17:16:30 -08001508 * This statement can only appear as the first instruction in an exception handler. We verify
1509 * that as part of extracting the exception type from the catch block list.
jeffhaobdb76512011-09-07 11:43:16 -07001510 */
Ian Rogers28ad40d2011-10-27 15:19:26 -07001511 const RegType& res_type = GetCaughtExceptionType();
Sebastien Hertz5243e912013-05-21 10:55:07 +02001512 work_line_->SetRegisterType(inst->VRegA_11x(), res_type);
jeffhaobdb76512011-09-07 11:43:16 -07001513 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001514 }
jeffhaobdb76512011-09-07 11:43:16 -07001515 case Instruction::RETURN_VOID:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001516 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
1517 if (!GetMethodReturnType().IsConflict()) {
jeffhaod5347e02012-03-22 17:25:05 -07001518 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001519 }
jeffhaobdb76512011-09-07 11:43:16 -07001520 }
1521 break;
1522 case Instruction::RETURN:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001523 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
jeffhaobdb76512011-09-07 11:43:16 -07001524 /* check the method signature */
Ian Rogersd81871c2011-10-03 13:57:23 -07001525 const RegType& return_type = GetMethodReturnType();
1526 if (!return_type.IsCategory1Types()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001527 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected non-category 1 return type "
1528 << return_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001529 } else {
1530 // Compilers may generate synthetic functions that write byte values into boolean fields.
1531 // Also, it may use integer values for boolean, byte, short, and character return types.
Sebastien Hertz5243e912013-05-21 10:55:07 +02001532 const uint32_t vregA = inst->VRegA_11x();
1533 const RegType& src_type = work_line_->GetRegisterType(vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07001534 bool use_src = ((return_type.IsBoolean() && src_type.IsByte()) ||
1535 ((return_type.IsBoolean() || return_type.IsByte() ||
1536 return_type.IsShort() || return_type.IsChar()) &&
1537 src_type.IsInteger()));
1538 /* check the register contents */
Ian Rogersad0b3a32012-04-16 14:50:24 -07001539 bool success =
Sebastien Hertz5243e912013-05-21 10:55:07 +02001540 work_line_->VerifyRegisterType(vregA, use_src ? src_type : return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001541 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001542 AppendToLastFailMessage(StringPrintf(" return-1nr on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07001543 }
jeffhaobdb76512011-09-07 11:43:16 -07001544 }
1545 }
1546 break;
1547 case Instruction::RETURN_WIDE:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001548 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
jeffhaobdb76512011-09-07 11:43:16 -07001549 /* check the method signature */
Ian Rogersd81871c2011-10-03 13:57:23 -07001550 const RegType& return_type = GetMethodReturnType();
1551 if (!return_type.IsCategory2Types()) {
jeffhaod5347e02012-03-22 17:25:05 -07001552 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-wide not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001553 } else {
1554 /* check the register contents */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001555 const uint32_t vregA = inst->VRegA_11x();
1556 bool success = work_line_->VerifyRegisterType(vregA, return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001557 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001558 AppendToLastFailMessage(StringPrintf(" return-wide on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07001559 }
jeffhaobdb76512011-09-07 11:43:16 -07001560 }
1561 }
1562 break;
1563 case Instruction::RETURN_OBJECT:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001564 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001565 const RegType& return_type = GetMethodReturnType();
1566 if (!return_type.IsReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001567 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-object not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001568 } else {
1569 /* return_type is the *expected* return type, not register value */
1570 DCHECK(!return_type.IsZero());
1571 DCHECK(!return_type.IsUninitializedReference());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001572 const uint32_t vregA = inst->VRegA_11x();
1573 const RegType& reg_type = work_line_->GetRegisterType(vregA);
Ian Rogers9074b992011-10-26 17:41:55 -07001574 // Disallow returning uninitialized values and verify that the reference in vAA is an
1575 // instance of the "return_type"
1576 if (reg_type.IsUninitializedTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001577 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "returning uninitialized object '"
1578 << reg_type << "'";
Ian Rogers9074b992011-10-26 17:41:55 -07001579 } else if (!return_type.IsAssignableFrom(reg_type)) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001580 Fail(reg_type.IsUnresolvedTypes() ?
1581 VERIFY_ERROR_BAD_CLASS_SOFT :
1582 VERIFY_ERROR_BAD_CLASS_HARD)
1583 << "returning '" << reg_type << "', but expected from declaration '"
1584 << return_type << "'";
jeffhaobdb76512011-09-07 11:43:16 -07001585 }
1586 }
1587 }
1588 break;
1589
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001590 /* could be boolean, int, float, or a null reference */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001591 case Instruction::CONST_4: {
1592 int32_t val = static_cast<int32_t>(inst->VRegB_11n() << 28) >> 28;
1593 work_line_->SetRegisterType(inst->VRegA_11n(), reg_types_.FromCat1Const(val, true));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001594 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02001595 }
1596 case Instruction::CONST_16: {
1597 int16_t val = static_cast<int16_t>(inst->VRegB_21s());
1598 work_line_->SetRegisterType(inst->VRegA_21s(), reg_types_.FromCat1Const(val, true));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001599 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02001600 }
jeffhaobdb76512011-09-07 11:43:16 -07001601 case Instruction::CONST:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001602 work_line_->SetRegisterType(inst->VRegA_31i(),
1603 reg_types_.FromCat1Const(inst->VRegB_31i(), true));
jeffhaobdb76512011-09-07 11:43:16 -07001604 break;
1605 case Instruction::CONST_HIGH16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001606 work_line_->SetRegisterType(inst->VRegA_21h(),
1607 reg_types_.FromCat1Const(inst->VRegB_21h() << 16, true));
jeffhaobdb76512011-09-07 11:43:16 -07001608 break;
jeffhaobdb76512011-09-07 11:43:16 -07001609 /* could be long or double; resolved upon use */
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001610 case Instruction::CONST_WIDE_16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001611 int64_t val = static_cast<int16_t>(inst->VRegB_21s());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001612 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1613 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001614 work_line_->SetRegisterTypeWide(inst->VRegA_21s(), lo, hi);
jeffhaobdb76512011-09-07 11:43:16 -07001615 break;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001616 }
1617 case Instruction::CONST_WIDE_32: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001618 int64_t val = static_cast<int32_t>(inst->VRegB_31i());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001619 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1620 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001621 work_line_->SetRegisterTypeWide(inst->VRegA_31i(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001622 break;
1623 }
1624 case Instruction::CONST_WIDE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001625 int64_t val = inst->VRegB_51l();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001626 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1627 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001628 work_line_->SetRegisterTypeWide(inst->VRegA_51l(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001629 break;
1630 }
1631 case Instruction::CONST_WIDE_HIGH16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001632 int64_t val = static_cast<uint64_t>(inst->VRegB_21h()) << 48;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001633 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1634 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001635 work_line_->SetRegisterTypeWide(inst->VRegA_21h(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001636 break;
1637 }
jeffhaobdb76512011-09-07 11:43:16 -07001638 case Instruction::CONST_STRING:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001639 work_line_->SetRegisterType(inst->VRegA_21c(), reg_types_.JavaLangString());
1640 break;
jeffhaobdb76512011-09-07 11:43:16 -07001641 case Instruction::CONST_STRING_JUMBO:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001642 work_line_->SetRegisterType(inst->VRegA_31c(), reg_types_.JavaLangString());
jeffhaobdb76512011-09-07 11:43:16 -07001643 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001644 case Instruction::CONST_CLASS: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001645 // Get type from instruction if unresolved then we need an access check
1646 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Sebastien Hertz5243e912013-05-21 10:55:07 +02001647 const RegType& res_type = ResolveClassAndCheckAccess(inst->VRegB_21c());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001648 // Register holds class, ie its type is class, on error it will hold Conflict.
Sebastien Hertz5243e912013-05-21 10:55:07 +02001649 work_line_->SetRegisterType(inst->VRegA_21c(),
Ian Rogersb4903572012-10-11 11:52:56 -07001650 res_type.IsConflict() ? res_type
1651 : reg_types_.JavaLangClass(true));
jeffhaobdb76512011-09-07 11:43:16 -07001652 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001653 }
jeffhaobdb76512011-09-07 11:43:16 -07001654 case Instruction::MONITOR_ENTER:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001655 work_line_->PushMonitor(inst->VRegA_11x(), work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07001656 break;
1657 case Instruction::MONITOR_EXIT:
1658 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001659 * monitor-exit instructions are odd. They can throw exceptions,
jeffhaobdb76512011-09-07 11:43:16 -07001660 * but when they do they act as if they succeeded and the PC is
jeffhaod1f0fde2011-09-08 17:25:33 -07001661 * pointing to the following instruction. (This behavior goes back
jeffhaobdb76512011-09-07 11:43:16 -07001662 * to the need to handle asynchronous exceptions, a now-deprecated
1663 * feature that Dalvik doesn't support.)
1664 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001665 * In practice we don't need to worry about this. The only
jeffhaobdb76512011-09-07 11:43:16 -07001666 * exceptions that can be thrown from monitor-exit are for a
jeffhaod1f0fde2011-09-08 17:25:33 -07001667 * null reference and -exit without a matching -enter. If the
jeffhaobdb76512011-09-07 11:43:16 -07001668 * structured locking checks are working, the former would have
1669 * failed on the -enter instruction, and the latter is impossible.
1670 *
1671 * This is fortunate, because issue 3221411 prevents us from
1672 * chasing the "can throw" path when monitor verification is
jeffhaod1f0fde2011-09-08 17:25:33 -07001673 * enabled. If we can fully verify the locking we can ignore
jeffhaobdb76512011-09-07 11:43:16 -07001674 * some catch blocks (which will show up as "dead" code when
1675 * we skip them here); if we can't, then the code path could be
1676 * "live" so we still need to check it.
1677 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001678 opcode_flags &= ~Instruction::kThrow;
Sebastien Hertz5243e912013-05-21 10:55:07 +02001679 work_line_->PopMonitor(inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07001680 break;
1681
Ian Rogers28ad40d2011-10-27 15:19:26 -07001682 case Instruction::CHECK_CAST:
Ian Rogersd81871c2011-10-03 13:57:23 -07001683 case Instruction::INSTANCE_OF: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001684 /*
1685 * If this instruction succeeds, we will "downcast" register vA to the type in vB. (This
1686 * could be a "upcast" -- not expected, so we don't try to address it.)
1687 *
1688 * If it fails, an exception is thrown, which we deal with later by ignoring the update to
Elliott Hughesadb8c672012-03-06 16:49:32 -08001689 * dec_insn.vA when branching to a handler.
Ian Rogers28ad40d2011-10-27 15:19:26 -07001690 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001691 const bool is_checkcast = (inst->Opcode() == Instruction::CHECK_CAST);
1692 const uint32_t type_idx = (is_checkcast) ? inst->VRegB_21c() : inst->VRegC_22c();
1693 const RegType& res_type = ResolveClassAndCheckAccess(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001694 if (res_type.IsConflict()) {
1695 DCHECK_NE(failures_.size(), 0U);
1696 if (!is_checkcast) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001697 work_line_->SetRegisterType(inst->VRegA_22c(), reg_types_.Boolean());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001698 }
1699 break; // bad class
Ian Rogers9f1ab122011-12-12 08:52:43 -08001700 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001701 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Sebastien Hertz5243e912013-05-21 10:55:07 +02001702 uint32_t orig_type_reg = (is_checkcast) ? inst->VRegA_21c() : inst->VRegB_22c();
1703 const RegType& orig_type = work_line_->GetRegisterType(orig_type_reg);
Ian Rogers28ad40d2011-10-27 15:19:26 -07001704 if (!res_type.IsNonZeroReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001705 if (is_checkcast) {
1706 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on unexpected class " << res_type;
1707 } else {
1708 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on unexpected class " << res_type;
1709 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001710 } else if (!orig_type.IsReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001711 if (is_checkcast) {
1712 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on non-reference in v" << orig_type_reg;
1713 } else {
1714 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on non-reference in v" << orig_type_reg;
1715 }
jeffhao2a8a90e2011-09-26 14:25:31 -07001716 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001717 if (is_checkcast) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001718 work_line_->SetRegisterType(inst->VRegA_21c(), res_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001719 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001720 work_line_->SetRegisterType(inst->VRegA_22c(), reg_types_.Boolean());
jeffhaobdb76512011-09-07 11:43:16 -07001721 }
jeffhaobdb76512011-09-07 11:43:16 -07001722 }
jeffhao2a8a90e2011-09-26 14:25:31 -07001723 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001724 }
1725 case Instruction::ARRAY_LENGTH: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001726 const RegType& res_type = work_line_->GetRegisterType(inst->VRegB_12x());
Ian Rogers28ad40d2011-10-27 15:19:26 -07001727 if (res_type.IsReferenceTypes()) {
Ian Rogers89310de2012-02-01 13:47:30 -08001728 if (!res_type.IsArrayTypes() && !res_type.IsZero()) { // ie not an array or null
jeffhaod5347e02012-03-22 17:25:05 -07001729 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001730 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001731 work_line_->SetRegisterType(inst->VRegA_12x(), reg_types_.Integer());
Ian Rogersd81871c2011-10-03 13:57:23 -07001732 }
1733 }
1734 break;
1735 }
1736 case Instruction::NEW_INSTANCE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001737 const RegType& res_type = ResolveClassAndCheckAccess(inst->VRegB_21c());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001738 if (res_type.IsConflict()) {
1739 DCHECK_NE(failures_.size(), 0U);
1740 break; // bad class
jeffhao8cd6dda2012-02-22 10:15:34 -08001741 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001742 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
1743 // can't create an instance of an interface or abstract class */
1744 if (!res_type.IsInstantiableTypes()) {
1745 Fail(VERIFY_ERROR_INSTANTIATION)
1746 << "new-instance on primitive, interface or abstract class" << res_type;
Ian Rogers08f753d2012-08-24 14:35:25 -07001747 // Soft failure so carry on to set register type.
Ian Rogersd81871c2011-10-03 13:57:23 -07001748 }
Ian Rogers08f753d2012-08-24 14:35:25 -07001749 const RegType& uninit_type = reg_types_.Uninitialized(res_type, work_insn_idx_);
1750 // Any registers holding previous allocations from this address that have not yet been
1751 // initialized must be marked invalid.
1752 work_line_->MarkUninitRefsAsInvalid(uninit_type);
1753 // add the new uninitialized reference to the register state
Sebastien Hertz5243e912013-05-21 10:55:07 +02001754 work_line_->SetRegisterType(inst->VRegA_21c(), uninit_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001755 break;
1756 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08001757 case Instruction::NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001758 VerifyNewArray(inst, false, false);
jeffhaobdb76512011-09-07 11:43:16 -07001759 break;
1760 case Instruction::FILLED_NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001761 VerifyNewArray(inst, true, false);
Ian Rogers0c4a5062012-02-03 15:18:59 -08001762 just_set_result = true; // Filled new array sets result register
jeffhaobdb76512011-09-07 11:43:16 -07001763 break;
Ian Rogers0c4a5062012-02-03 15:18:59 -08001764 case Instruction::FILLED_NEW_ARRAY_RANGE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001765 VerifyNewArray(inst, true, true);
Ian Rogers0c4a5062012-02-03 15:18:59 -08001766 just_set_result = true; // Filled new array range sets result register
1767 break;
jeffhaobdb76512011-09-07 11:43:16 -07001768 case Instruction::CMPL_FLOAT:
1769 case Instruction::CMPG_FLOAT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001770 if (!work_line_->VerifyRegisterType(inst->VRegB_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08001771 break;
1772 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02001773 if (!work_line_->VerifyRegisterType(inst->VRegC_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08001774 break;
1775 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02001776 work_line_->SetRegisterType(inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001777 break;
1778 case Instruction::CMPL_DOUBLE:
1779 case Instruction::CMPG_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001780 if (!work_line_->VerifyRegisterTypeWide(inst->VRegB_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001781 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001782 break;
1783 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02001784 if (!work_line_->VerifyRegisterTypeWide(inst->VRegC_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001785 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001786 break;
1787 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02001788 work_line_->SetRegisterType(inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001789 break;
1790 case Instruction::CMP_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001791 if (!work_line_->VerifyRegisterTypeWide(inst->VRegB_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001792 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001793 break;
1794 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02001795 if (!work_line_->VerifyRegisterTypeWide(inst->VRegC_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001796 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001797 break;
1798 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02001799 work_line_->SetRegisterType(inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001800 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001801 case Instruction::THROW: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001802 const RegType& res_type = work_line_->GetRegisterType(inst->VRegA_11x());
Ian Rogersb4903572012-10-11 11:52:56 -07001803 if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(res_type)) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001804 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "thrown class " << res_type
1805 << " not instanceof Throwable";
jeffhaobdb76512011-09-07 11:43:16 -07001806 }
1807 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001808 }
jeffhaobdb76512011-09-07 11:43:16 -07001809 case Instruction::GOTO:
1810 case Instruction::GOTO_16:
1811 case Instruction::GOTO_32:
1812 /* no effect on or use of registers */
1813 break;
1814
1815 case Instruction::PACKED_SWITCH:
1816 case Instruction::SPARSE_SWITCH:
1817 /* verify that vAA is an integer, or can be converted to one */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001818 work_line_->VerifyRegisterType(inst->VRegA_31t(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001819 break;
1820
Ian Rogersd81871c2011-10-03 13:57:23 -07001821 case Instruction::FILL_ARRAY_DATA: {
1822 /* Similar to the verification done for APUT */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001823 const RegType& array_type = work_line_->GetRegisterType(inst->VRegA_31t());
Ian Rogers89310de2012-02-01 13:47:30 -08001824 /* array_type can be null if the reg type is Zero */
1825 if (!array_type.IsZero()) {
jeffhao457cc512012-02-02 16:55:13 -08001826 if (!array_type.IsArrayTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001827 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with array type "
1828 << array_type;
Ian Rogers89310de2012-02-01 13:47:30 -08001829 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001830 const RegType& component_type = reg_types_.GetComponentType(array_type, class_loader_);
1831 DCHECK(!component_type.IsConflict());
jeffhao457cc512012-02-02 16:55:13 -08001832 if (component_type.IsNonZeroReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001833 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with component type "
1834 << component_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001835 } else {
jeffhao457cc512012-02-02 16:55:13 -08001836 // Now verify if the element width in the table matches the element width declared in
1837 // the array
1838 const uint16_t* array_data = insns + (insns[1] | (((int32_t) insns[2]) << 16));
1839 if (array_data[0] != Instruction::kArrayDataSignature) {
jeffhaod5347e02012-03-22 17:25:05 -07001840 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid magic for array-data";
jeffhao457cc512012-02-02 16:55:13 -08001841 } else {
1842 size_t elem_width = Primitive::ComponentSize(component_type.GetPrimitiveType());
1843 // Since we don't compress the data in Dex, expect to see equal width of data stored
1844 // in the table and expected from the array class.
1845 if (array_data[1] != elem_width) {
jeffhaod5347e02012-03-22 17:25:05 -07001846 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-data size mismatch (" << array_data[1]
1847 << " vs " << elem_width << ")";
jeffhao457cc512012-02-02 16:55:13 -08001848 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001849 }
1850 }
jeffhaobdb76512011-09-07 11:43:16 -07001851 }
1852 }
1853 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001854 }
jeffhaobdb76512011-09-07 11:43:16 -07001855 case Instruction::IF_EQ:
Ian Rogersd81871c2011-10-03 13:57:23 -07001856 case Instruction::IF_NE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001857 const RegType& reg_type1 = work_line_->GetRegisterType(inst->VRegA_22t());
1858 const RegType& reg_type2 = work_line_->GetRegisterType(inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07001859 bool mismatch = false;
1860 if (reg_type1.IsZero()) { // zero then integral or reference expected
1861 mismatch = !reg_type2.IsReferenceTypes() && !reg_type2.IsIntegralTypes();
1862 } else if (reg_type1.IsReferenceTypes()) { // both references?
1863 mismatch = !reg_type2.IsReferenceTypes();
1864 } else { // both integral?
1865 mismatch = !reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes();
1866 }
1867 if (mismatch) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001868 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to if-eq/if-ne (" << reg_type1 << ","
1869 << reg_type2 << ") must both be references or integral";
jeffhaobdb76512011-09-07 11:43:16 -07001870 }
1871 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001872 }
jeffhaobdb76512011-09-07 11:43:16 -07001873 case Instruction::IF_LT:
1874 case Instruction::IF_GE:
1875 case Instruction::IF_GT:
Ian Rogersd81871c2011-10-03 13:57:23 -07001876 case Instruction::IF_LE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001877 const RegType& reg_type1 = work_line_->GetRegisterType(inst->VRegA_22t());
1878 const RegType& reg_type2 = work_line_->GetRegisterType(inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07001879 if (!reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001880 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to 'if' (" << reg_type1 << ","
1881 << reg_type2 << ") must be integral";
jeffhaobdb76512011-09-07 11:43:16 -07001882 }
1883 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001884 }
jeffhaobdb76512011-09-07 11:43:16 -07001885 case Instruction::IF_EQZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07001886 case Instruction::IF_NEZ: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001887 const RegType& reg_type = work_line_->GetRegisterType(inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07001888 if (!reg_type.IsReferenceTypes() && !reg_type.IsIntegralTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001889 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
1890 << " unexpected as arg to if-eqz/if-nez";
Ian Rogersd81871c2011-10-03 13:57:23 -07001891 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001892
1893 // Find previous instruction - its existence is a precondition to peephole optimization.
Ian Rogers9b360392013-06-06 14:45:07 -07001894 uint32_t instance_of_idx = 0;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001895 if (0 != work_insn_idx_) {
Ian Rogers9b360392013-06-06 14:45:07 -07001896 instance_of_idx = work_insn_idx_ - 1;
Brian Carlstromdf629502013-07-17 22:39:56 -07001897 while (0 != instance_of_idx && !insn_flags_[instance_of_idx].IsOpcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07001898 instance_of_idx--;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001899 }
Ian Rogers9b360392013-06-06 14:45:07 -07001900 CHECK(insn_flags_[instance_of_idx].IsOpcode());
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001901 } else {
1902 break;
1903 }
1904
Ian Rogers9b360392013-06-06 14:45:07 -07001905 const Instruction* instance_of_inst = Instruction::At(code_item_->insns_ + instance_of_idx);
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001906
1907 /* Check for peep-hole pattern of:
1908 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07001909 * instance-of vX, vY, T;
1910 * ifXXX vX, label ;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001911 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07001912 * label:
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001913 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07001914 * and sharpen the type of vY to be type T.
1915 * Note, this pattern can't be if:
1916 * - if there are other branches to this branch,
1917 * - when vX == vY.
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001918 */
Ian Rogersfae370a2013-06-05 08:33:27 -07001919 if (!CurrentInsnFlags()->IsBranchTarget() &&
Ian Rogers9b360392013-06-06 14:45:07 -07001920 (Instruction::INSTANCE_OF == instance_of_inst->Opcode()) &&
1921 (inst->VRegA_21t() == instance_of_inst->VRegA_22c()) &&
1922 (instance_of_inst->VRegA_22c() != instance_of_inst->VRegB_22c())) {
Ian Rogersfae370a2013-06-05 08:33:27 -07001923 // Check that the we are not attempting conversion to interface types,
1924 // which is not done because of the multiple inheritance implications.
Ian Rogers9b360392013-06-06 14:45:07 -07001925 const RegType& cast_type = ResolveClassAndCheckAccess(instance_of_inst->VRegC_22c());
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001926
Brian Carlstromdf629502013-07-17 22:39:56 -07001927 if (!cast_type.IsUnresolvedTypes() && !cast_type.GetClass()->IsInterface()) {
Ian Rogers9b360392013-06-06 14:45:07 -07001928 RegisterLine* update_line = new RegisterLine(code_item_->registers_size_, this);
Ian Rogersfae370a2013-06-05 08:33:27 -07001929 if (inst->Opcode() == Instruction::IF_EQZ) {
Ian Rogers9b360392013-06-06 14:45:07 -07001930 fallthrough_line.reset(update_line);
Ian Rogersfae370a2013-06-05 08:33:27 -07001931 } else {
Ian Rogers9b360392013-06-06 14:45:07 -07001932 branch_line.reset(update_line);
1933 }
1934 update_line->CopyFromLine(work_line_.get());
1935 update_line->SetRegisterType(instance_of_inst->VRegB_22c(), cast_type);
1936 if (!insn_flags_[instance_of_idx].IsBranchTarget() && 0 != instance_of_idx) {
1937 // See if instance-of was preceded by a move-object operation, common due to the small
1938 // register encoding space of instance-of, and propagate type information to the source
1939 // of the move-object.
1940 uint32_t move_idx = instance_of_idx - 1;
Brian Carlstromdf629502013-07-17 22:39:56 -07001941 while (0 != move_idx && !insn_flags_[move_idx].IsOpcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07001942 move_idx--;
1943 }
1944 CHECK(insn_flags_[move_idx].IsOpcode());
1945 const Instruction* move_inst = Instruction::At(code_item_->insns_ + move_idx);
1946 switch (move_inst->Opcode()) {
1947 case Instruction::MOVE_OBJECT:
1948 if (move_inst->VRegA_12x() == instance_of_inst->VRegB_22c()) {
1949 update_line->SetRegisterType(move_inst->VRegB_12x(), cast_type);
1950 }
1951 break;
1952 case Instruction::MOVE_OBJECT_FROM16:
1953 if (move_inst->VRegA_22x() == instance_of_inst->VRegB_22c()) {
1954 update_line->SetRegisterType(move_inst->VRegB_22x(), cast_type);
1955 }
1956 break;
1957 case Instruction::MOVE_OBJECT_16:
1958 if (move_inst->VRegA_32x() == instance_of_inst->VRegB_22c()) {
1959 update_line->SetRegisterType(move_inst->VRegB_32x(), cast_type);
1960 }
1961 break;
1962 default:
1963 break;
1964 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001965 }
1966 }
1967 }
1968
jeffhaobdb76512011-09-07 11:43:16 -07001969 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001970 }
jeffhaobdb76512011-09-07 11:43:16 -07001971 case Instruction::IF_LTZ:
1972 case Instruction::IF_GEZ:
1973 case Instruction::IF_GTZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07001974 case Instruction::IF_LEZ: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001975 const RegType& reg_type = work_line_->GetRegisterType(inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07001976 if (!reg_type.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001977 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
1978 << " unexpected as arg to if-ltz/if-gez/if-gtz/if-lez";
Ian Rogersd81871c2011-10-03 13:57:23 -07001979 }
jeffhaobdb76512011-09-07 11:43:16 -07001980 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001981 }
jeffhaobdb76512011-09-07 11:43:16 -07001982 case Instruction::AGET_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001983 VerifyAGet(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001984 break;
jeffhaobdb76512011-09-07 11:43:16 -07001985 case Instruction::AGET_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001986 VerifyAGet(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001987 break;
jeffhaobdb76512011-09-07 11:43:16 -07001988 case Instruction::AGET_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001989 VerifyAGet(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001990 break;
jeffhaobdb76512011-09-07 11:43:16 -07001991 case Instruction::AGET_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001992 VerifyAGet(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001993 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001994 case Instruction::AGET:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001995 VerifyAGet(inst, reg_types_.Integer(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001996 break;
jeffhaobdb76512011-09-07 11:43:16 -07001997 case Instruction::AGET_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001998 VerifyAGet(inst, reg_types_.LongLo(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001999 break;
2000 case Instruction::AGET_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002001 VerifyAGet(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07002002 break;
2003
Ian Rogersd81871c2011-10-03 13:57:23 -07002004 case Instruction::APUT_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002005 VerifyAPut(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002006 break;
2007 case Instruction::APUT_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002008 VerifyAPut(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002009 break;
2010 case Instruction::APUT_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002011 VerifyAPut(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002012 break;
2013 case Instruction::APUT_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002014 VerifyAPut(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002015 break;
2016 case Instruction::APUT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002017 VerifyAPut(inst, reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002018 break;
2019 case Instruction::APUT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002020 VerifyAPut(inst, reg_types_.LongLo(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002021 break;
2022 case Instruction::APUT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002023 VerifyAPut(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07002024 break;
2025
jeffhaobdb76512011-09-07 11:43:16 -07002026 case Instruction::IGET_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002027 VerifyISGet(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002028 break;
jeffhaobdb76512011-09-07 11:43:16 -07002029 case Instruction::IGET_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002030 VerifyISGet(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002031 break;
jeffhaobdb76512011-09-07 11:43:16 -07002032 case Instruction::IGET_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002033 VerifyISGet(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002034 break;
jeffhaobdb76512011-09-07 11:43:16 -07002035 case Instruction::IGET_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002036 VerifyISGet(inst, reg_types_.Short(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002037 break;
2038 case Instruction::IGET:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002039 VerifyISGet(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002040 break;
2041 case Instruction::IGET_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002042 VerifyISGet(inst, reg_types_.LongLo(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002043 break;
2044 case Instruction::IGET_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002045 VerifyISGet(inst, reg_types_.JavaLangObject(false), false, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002046 break;
jeffhaobdb76512011-09-07 11:43:16 -07002047
Ian Rogersd81871c2011-10-03 13:57:23 -07002048 case Instruction::IPUT_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002049 VerifyISPut(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002050 break;
2051 case Instruction::IPUT_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002052 VerifyISPut(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002053 break;
2054 case Instruction::IPUT_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002055 VerifyISPut(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002056 break;
2057 case Instruction::IPUT_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002058 VerifyISPut(inst, reg_types_.Short(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002059 break;
2060 case Instruction::IPUT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002061 VerifyISPut(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002062 break;
2063 case Instruction::IPUT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002064 VerifyISPut(inst, reg_types_.LongLo(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002065 break;
jeffhaobdb76512011-09-07 11:43:16 -07002066 case Instruction::IPUT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002067 VerifyISPut(inst, reg_types_.JavaLangObject(false), false, false);
jeffhaobdb76512011-09-07 11:43:16 -07002068 break;
2069
jeffhaobdb76512011-09-07 11:43:16 -07002070 case Instruction::SGET_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002071 VerifyISGet(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002072 break;
jeffhaobdb76512011-09-07 11:43:16 -07002073 case Instruction::SGET_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002074 VerifyISGet(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002075 break;
jeffhaobdb76512011-09-07 11:43:16 -07002076 case Instruction::SGET_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002077 VerifyISGet(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002078 break;
jeffhaobdb76512011-09-07 11:43:16 -07002079 case Instruction::SGET_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002080 VerifyISGet(inst, reg_types_.Short(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002081 break;
2082 case Instruction::SGET:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002083 VerifyISGet(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002084 break;
2085 case Instruction::SGET_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002086 VerifyISGet(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002087 break;
2088 case Instruction::SGET_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002089 VerifyISGet(inst, reg_types_.JavaLangObject(false), false, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002090 break;
2091
2092 case Instruction::SPUT_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002093 VerifyISPut(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002094 break;
2095 case Instruction::SPUT_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002096 VerifyISPut(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002097 break;
2098 case Instruction::SPUT_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002099 VerifyISPut(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002100 break;
2101 case Instruction::SPUT_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002102 VerifyISPut(inst, reg_types_.Short(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002103 break;
2104 case Instruction::SPUT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002105 VerifyISPut(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002106 break;
2107 case Instruction::SPUT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002108 VerifyISPut(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002109 break;
2110 case Instruction::SPUT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002111 VerifyISPut(inst, reg_types_.JavaLangObject(false), false, true);
jeffhaobdb76512011-09-07 11:43:16 -07002112 break;
2113
2114 case Instruction::INVOKE_VIRTUAL:
2115 case Instruction::INVOKE_VIRTUAL_RANGE:
2116 case Instruction::INVOKE_SUPER:
Ian Rogersd81871c2011-10-03 13:57:23 -07002117 case Instruction::INVOKE_SUPER_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002118 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE ||
2119 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
2120 bool is_super = (inst->Opcode() == Instruction::INVOKE_SUPER ||
2121 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
2122 mirror::AbstractMethod* called_method = VerifyInvocationArgs(inst, METHOD_VIRTUAL,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002123 is_range, is_super);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002124 const char* descriptor;
2125 if (called_method == NULL) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002126 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002127 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2128 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
2129 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2130 } else {
2131 descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
jeffhaobdb76512011-09-07 11:43:16 -07002132 }
Ian Rogersb4903572012-10-11 11:52:56 -07002133 const RegType& return_type = reg_types_.FromDescriptor(class_loader_, descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002134 if (!return_type.IsLowHalf()) {
2135 work_line_->SetResultRegisterType(return_type);
2136 } else {
2137 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2138 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002139 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002140 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002141 }
jeffhaobdb76512011-09-07 11:43:16 -07002142 case Instruction::INVOKE_DIRECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002143 case Instruction::INVOKE_DIRECT_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002144 bool is_range = (inst->Opcode() == Instruction::INVOKE_DIRECT_RANGE);
2145 mirror::AbstractMethod* called_method = VerifyInvocationArgs(inst, METHOD_DIRECT,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002146 is_range, false);
Ian Rogers46685432012-06-03 22:26:43 -07002147 const char* return_type_descriptor;
2148 bool is_constructor;
2149 if (called_method == NULL) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002150 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogers46685432012-06-03 22:26:43 -07002151 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2152 is_constructor = StringPiece(dex_file_->GetMethodName(method_id)) == "<init>";
2153 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
2154 return_type_descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2155 } else {
2156 is_constructor = called_method->IsConstructor();
2157 return_type_descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
2158 }
2159 if (is_constructor) {
jeffhaobdb76512011-09-07 11:43:16 -07002160 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002161 * Some additional checks when calling a constructor. We know from the invocation arg check
2162 * that the "this" argument is an instance of called_method->klass. Now we further restrict
2163 * that to require that called_method->klass is the same as this->klass or this->super,
2164 * allowing the latter only if the "this" argument is the same as the "this" argument to
2165 * this method (which implies that we're in a constructor ourselves).
jeffhaobdb76512011-09-07 11:43:16 -07002166 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002167 const RegType& this_type = work_line_->GetInvocationThis(inst, is_range);
jeffhaob57e9522012-04-26 18:08:21 -07002168 if (this_type.IsConflict()) // failure.
2169 break;
jeffhaobdb76512011-09-07 11:43:16 -07002170
jeffhaob57e9522012-04-26 18:08:21 -07002171 /* no null refs allowed (?) */
2172 if (this_type.IsZero()) {
2173 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unable to initialize null ref";
2174 break;
jeffhao2a8a90e2011-09-26 14:25:31 -07002175 }
jeffhaob57e9522012-04-26 18:08:21 -07002176
2177 /* must be in same class or in superclass */
Ian Rogers46685432012-06-03 22:26:43 -07002178 // const RegType& this_super_klass = this_type.GetSuperClass(&reg_types_);
2179 // TODO: re-enable constructor type verification
2180 // if (this_super_klass.IsConflict()) {
jeffhaob57e9522012-04-26 18:08:21 -07002181 // Unknown super class, fail so we re-check at runtime.
Ian Rogers46685432012-06-03 22:26:43 -07002182 // Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "super class unknown for '" << this_type << "'";
2183 // break;
2184 // }
jeffhaob57e9522012-04-26 18:08:21 -07002185
2186 /* arg must be an uninitialized reference */
2187 if (!this_type.IsUninitializedTypes()) {
2188 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Expected initialization on uninitialized reference "
2189 << this_type;
2190 break;
2191 }
2192
2193 /*
2194 * Replace the uninitialized reference with an initialized one. We need to do this for all
2195 * registers that have the same object instance in them, not just the "this" register.
2196 */
2197 work_line_->MarkRefsAsInitialized(this_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002198 }
Ian Rogersb4903572012-10-11 11:52:56 -07002199 const RegType& return_type = reg_types_.FromDescriptor(class_loader_, return_type_descriptor,
2200 false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002201 if (!return_type.IsLowHalf()) {
2202 work_line_->SetResultRegisterType(return_type);
2203 } else {
2204 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2205 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002206 just_set_result = true;
2207 break;
2208 }
2209 case Instruction::INVOKE_STATIC:
2210 case Instruction::INVOKE_STATIC_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002211 bool is_range = (inst->Opcode() == Instruction::INVOKE_STATIC_RANGE);
Brian Carlstrom93c33962013-07-26 10:37:43 -07002212 mirror::AbstractMethod* called_method = VerifyInvocationArgs(inst,
2213 METHOD_STATIC,
2214 is_range,
2215 false);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002216 const char* descriptor;
2217 if (called_method == NULL) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002218 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002219 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2220 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogers0571d352011-11-03 19:51:38 -07002221 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002222 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002223 descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002224 }
Ian Rogersb4903572012-10-11 11:52:56 -07002225 const RegType& return_type = reg_types_.FromDescriptor(class_loader_, descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002226 if (!return_type.IsLowHalf()) {
2227 work_line_->SetResultRegisterType(return_type);
2228 } else {
2229 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2230 }
jeffhaobdb76512011-09-07 11:43:16 -07002231 just_set_result = true;
2232 }
2233 break;
jeffhaobdb76512011-09-07 11:43:16 -07002234 case Instruction::INVOKE_INTERFACE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002235 case Instruction::INVOKE_INTERFACE_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002236 bool is_range = (inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE);
Brian Carlstrom93c33962013-07-26 10:37:43 -07002237 mirror::AbstractMethod* abs_method = VerifyInvocationArgs(inst,
2238 METHOD_INTERFACE,
2239 is_range,
2240 false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002241 if (abs_method != NULL) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002242 mirror::Class* called_interface = abs_method->GetDeclaringClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002243 if (!called_interface->IsInterface() && !called_interface->IsObjectClass()) {
2244 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected interface class in invoke-interface '"
2245 << PrettyMethod(abs_method) << "'";
2246 break;
Ian Rogers28ad40d2011-10-27 15:19:26 -07002247 }
Ian Rogers0d604842012-04-16 14:50:24 -07002248 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002249 /* Get the type of the "this" arg, which should either be a sub-interface of called
2250 * interface or Object (see comments in RegType::JoinClass).
2251 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002252 const RegType& this_type = work_line_->GetInvocationThis(inst, is_range);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002253 if (this_type.IsZero()) {
2254 /* null pointer always passes (and always fails at runtime) */
2255 } else {
2256 if (this_type.IsUninitializedTypes()) {
2257 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface call on uninitialized object "
2258 << this_type;
2259 break;
2260 }
2261 // In the past we have tried to assert that "called_interface" is assignable
2262 // from "this_type.GetClass()", however, as we do an imprecise Join
2263 // (RegType::JoinClass) we don't have full information on what interfaces are
2264 // implemented by "this_type". For example, two classes may implement the same
2265 // interfaces and have a common parent that doesn't implement the interface. The
2266 // join will set "this_type" to the parent class and a test that this implements
2267 // the interface will incorrectly fail.
2268 }
2269 /*
2270 * We don't have an object instance, so we can't find the concrete method. However, all of
2271 * the type information is in the abstract method, so we're good.
2272 */
2273 const char* descriptor;
2274 if (abs_method == NULL) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002275 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002276 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2277 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
2278 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2279 } else {
2280 descriptor = MethodHelper(abs_method).GetReturnTypeDescriptor();
2281 }
Ian Rogersb4903572012-10-11 11:52:56 -07002282 const RegType& return_type = reg_types_.FromDescriptor(class_loader_, descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002283 if (!return_type.IsLowHalf()) {
2284 work_line_->SetResultRegisterType(return_type);
2285 } else {
2286 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2287 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002288 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002289 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002290 }
jeffhaobdb76512011-09-07 11:43:16 -07002291 case Instruction::NEG_INT:
2292 case Instruction::NOT_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002293 work_line_->CheckUnaryOp(inst, reg_types_.Integer(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002294 break;
2295 case Instruction::NEG_LONG:
2296 case Instruction::NOT_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002297 work_line_->CheckUnaryOpWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002298 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002299 break;
2300 case Instruction::NEG_FLOAT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002301 work_line_->CheckUnaryOp(inst, reg_types_.Float(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002302 break;
2303 case Instruction::NEG_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002304 work_line_->CheckUnaryOpWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002305 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002306 break;
2307 case Instruction::INT_TO_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002308 work_line_->CheckUnaryOpToWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002309 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002310 break;
2311 case Instruction::INT_TO_FLOAT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002312 work_line_->CheckUnaryOp(inst, reg_types_.Float(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002313 break;
2314 case Instruction::INT_TO_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002315 work_line_->CheckUnaryOpToWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002316 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002317 break;
2318 case Instruction::LONG_TO_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002319 work_line_->CheckUnaryOpFromWide(inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002320 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002321 break;
2322 case Instruction::LONG_TO_FLOAT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002323 work_line_->CheckUnaryOpFromWide(inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002324 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002325 break;
2326 case Instruction::LONG_TO_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002327 work_line_->CheckUnaryOpWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002328 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002329 break;
2330 case Instruction::FLOAT_TO_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002331 work_line_->CheckUnaryOp(inst, reg_types_.Integer(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002332 break;
2333 case Instruction::FLOAT_TO_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002334 work_line_->CheckUnaryOpToWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002335 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002336 break;
2337 case Instruction::FLOAT_TO_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002338 work_line_->CheckUnaryOpToWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002339 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002340 break;
2341 case Instruction::DOUBLE_TO_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002342 work_line_->CheckUnaryOpFromWide(inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002343 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002344 break;
2345 case Instruction::DOUBLE_TO_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002346 work_line_->CheckUnaryOpWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002347 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002348 break;
2349 case Instruction::DOUBLE_TO_FLOAT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002350 work_line_->CheckUnaryOpFromWide(inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002351 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002352 break;
2353 case Instruction::INT_TO_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002354 work_line_->CheckUnaryOp(inst, reg_types_.Byte(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002355 break;
2356 case Instruction::INT_TO_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002357 work_line_->CheckUnaryOp(inst, reg_types_.Char(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002358 break;
2359 case Instruction::INT_TO_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002360 work_line_->CheckUnaryOp(inst, reg_types_.Short(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002361 break;
2362
2363 case Instruction::ADD_INT:
2364 case Instruction::SUB_INT:
2365 case Instruction::MUL_INT:
2366 case Instruction::REM_INT:
2367 case Instruction::DIV_INT:
2368 case Instruction::SHL_INT:
2369 case Instruction::SHR_INT:
2370 case Instruction::USHR_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(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002373 break;
2374 case Instruction::AND_INT:
2375 case Instruction::OR_INT:
2376 case Instruction::XOR_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002377 work_line_->CheckBinaryOp(inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002378 reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002379 break;
2380 case Instruction::ADD_LONG:
2381 case Instruction::SUB_LONG:
2382 case Instruction::MUL_LONG:
2383 case Instruction::DIV_LONG:
2384 case Instruction::REM_LONG:
2385 case Instruction::AND_LONG:
2386 case Instruction::OR_LONG:
2387 case Instruction::XOR_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002388 work_line_->CheckBinaryOpWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002389 reg_types_.LongLo(), reg_types_.LongHi(),
2390 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002391 break;
2392 case Instruction::SHL_LONG:
2393 case Instruction::SHR_LONG:
2394 case Instruction::USHR_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002395 /* shift distance is Int, making these different from other binary operations */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002396 work_line_->CheckBinaryOpWideShift(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002397 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002398 break;
2399 case Instruction::ADD_FLOAT:
2400 case Instruction::SUB_FLOAT:
2401 case Instruction::MUL_FLOAT:
2402 case Instruction::DIV_FLOAT:
2403 case Instruction::REM_FLOAT:
Brian Carlstrom93c33962013-07-26 10:37:43 -07002404 work_line_->CheckBinaryOp(inst,
2405 reg_types_.Float(),
2406 reg_types_.Float(),
2407 reg_types_.Float(),
2408 false);
jeffhaobdb76512011-09-07 11:43:16 -07002409 break;
2410 case Instruction::ADD_DOUBLE:
2411 case Instruction::SUB_DOUBLE:
2412 case Instruction::MUL_DOUBLE:
2413 case Instruction::DIV_DOUBLE:
2414 case Instruction::REM_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002415 work_line_->CheckBinaryOpWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002416 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2417 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002418 break;
2419 case Instruction::ADD_INT_2ADDR:
2420 case Instruction::SUB_INT_2ADDR:
2421 case Instruction::MUL_INT_2ADDR:
2422 case Instruction::REM_INT_2ADDR:
2423 case Instruction::SHL_INT_2ADDR:
2424 case Instruction::SHR_INT_2ADDR:
2425 case Instruction::USHR_INT_2ADDR:
Brian Carlstrom93c33962013-07-26 10:37:43 -07002426 work_line_->CheckBinaryOp2addr(inst,
2427 reg_types_.Integer(),
2428 reg_types_.Integer(),
2429 reg_types_.Integer(),
2430 false);
jeffhaobdb76512011-09-07 11:43:16 -07002431 break;
2432 case Instruction::AND_INT_2ADDR:
2433 case Instruction::OR_INT_2ADDR:
2434 case Instruction::XOR_INT_2ADDR:
Brian Carlstrom93c33962013-07-26 10:37:43 -07002435 work_line_->CheckBinaryOp2addr(inst,
2436 reg_types_.Integer(),
2437 reg_types_.Integer(),
2438 reg_types_.Integer(),
2439 true);
jeffhaobdb76512011-09-07 11:43:16 -07002440 break;
2441 case Instruction::DIV_INT_2ADDR:
Brian Carlstrom93c33962013-07-26 10:37:43 -07002442 work_line_->CheckBinaryOp2addr(inst,
2443 reg_types_.Integer(),
2444 reg_types_.Integer(),
2445 reg_types_.Integer(),
2446 false);
jeffhaobdb76512011-09-07 11:43:16 -07002447 break;
2448 case Instruction::ADD_LONG_2ADDR:
2449 case Instruction::SUB_LONG_2ADDR:
2450 case Instruction::MUL_LONG_2ADDR:
2451 case Instruction::DIV_LONG_2ADDR:
2452 case Instruction::REM_LONG_2ADDR:
2453 case Instruction::AND_LONG_2ADDR:
2454 case Instruction::OR_LONG_2ADDR:
2455 case Instruction::XOR_LONG_2ADDR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002456 work_line_->CheckBinaryOp2addrWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002457 reg_types_.LongLo(), reg_types_.LongHi(),
2458 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002459 break;
2460 case Instruction::SHL_LONG_2ADDR:
2461 case Instruction::SHR_LONG_2ADDR:
2462 case Instruction::USHR_LONG_2ADDR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002463 work_line_->CheckBinaryOp2addrWideShift(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002464 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002465 break;
2466 case Instruction::ADD_FLOAT_2ADDR:
2467 case Instruction::SUB_FLOAT_2ADDR:
2468 case Instruction::MUL_FLOAT_2ADDR:
2469 case Instruction::DIV_FLOAT_2ADDR:
2470 case Instruction::REM_FLOAT_2ADDR:
Brian Carlstrom93c33962013-07-26 10:37:43 -07002471 work_line_->CheckBinaryOp2addr(inst,
2472 reg_types_.Float(),
2473 reg_types_.Float(),
2474 reg_types_.Float(),
2475 false);
jeffhaobdb76512011-09-07 11:43:16 -07002476 break;
2477 case Instruction::ADD_DOUBLE_2ADDR:
2478 case Instruction::SUB_DOUBLE_2ADDR:
2479 case Instruction::MUL_DOUBLE_2ADDR:
2480 case Instruction::DIV_DOUBLE_2ADDR:
2481 case Instruction::REM_DOUBLE_2ADDR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002482 work_line_->CheckBinaryOp2addrWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002483 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2484 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002485 break;
2486 case Instruction::ADD_INT_LIT16:
2487 case Instruction::RSUB_INT:
2488 case Instruction::MUL_INT_LIT16:
2489 case Instruction::DIV_INT_LIT16:
2490 case Instruction::REM_INT_LIT16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002491 work_line_->CheckLiteralOp(inst, reg_types_.Integer(), reg_types_.Integer(), false, true);
jeffhaobdb76512011-09-07 11:43:16 -07002492 break;
2493 case Instruction::AND_INT_LIT16:
2494 case Instruction::OR_INT_LIT16:
2495 case Instruction::XOR_INT_LIT16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002496 work_line_->CheckLiteralOp(inst, reg_types_.Integer(), reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002497 break;
2498 case Instruction::ADD_INT_LIT8:
2499 case Instruction::RSUB_INT_LIT8:
2500 case Instruction::MUL_INT_LIT8:
2501 case Instruction::DIV_INT_LIT8:
2502 case Instruction::REM_INT_LIT8:
2503 case Instruction::SHL_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002504 case Instruction::SHR_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002505 case Instruction::USHR_INT_LIT8:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002506 work_line_->CheckLiteralOp(inst, reg_types_.Integer(), reg_types_.Integer(), false, false);
jeffhaobdb76512011-09-07 11:43:16 -07002507 break;
2508 case Instruction::AND_INT_LIT8:
2509 case Instruction::OR_INT_LIT8:
2510 case Instruction::XOR_INT_LIT8:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002511 work_line_->CheckLiteralOp(inst, reg_types_.Integer(), reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002512 break;
2513
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002514 // Special instructions.
Sebastien Hertzcc10e0e2013-06-28 14:24:48 +02002515 case Instruction::RETURN_VOID_BARRIER:
2516 DCHECK(Runtime::Current()->IsStarted());
2517 if (!IsConstructor()) {
2518 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void-barrier not expected";
2519 }
2520 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002521 // Note: the following instructions encode offsets derived from class linking.
2522 // As such they use Class*/Field*/AbstractMethod* as these offsets only have
2523 // meaning if the class linking and resolution were successful.
2524 case Instruction::IGET_QUICK:
2525 VerifyIGetQuick(inst, reg_types_.Integer(), true);
2526 break;
2527 case Instruction::IGET_WIDE_QUICK:
2528 VerifyIGetQuick(inst, reg_types_.LongLo(), true);
2529 break;
2530 case Instruction::IGET_OBJECT_QUICK:
2531 VerifyIGetQuick(inst, reg_types_.JavaLangObject(false), false);
2532 break;
2533 case Instruction::IPUT_QUICK:
2534 VerifyIPutQuick(inst, reg_types_.Integer(), true);
2535 break;
2536 case Instruction::IPUT_WIDE_QUICK:
2537 VerifyIPutQuick(inst, reg_types_.LongLo(), true);
2538 break;
2539 case Instruction::IPUT_OBJECT_QUICK:
2540 VerifyIPutQuick(inst, reg_types_.JavaLangObject(false), false);
2541 break;
2542 case Instruction::INVOKE_VIRTUAL_QUICK:
2543 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: {
2544 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
2545 mirror::AbstractMethod* called_method = VerifyInvokeVirtualQuickArgs(inst, is_range);
2546 if (called_method != NULL) {
2547 const char* descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
2548 const RegType& return_type = reg_types_.FromDescriptor(class_loader_, descriptor, false);
2549 if (!return_type.IsLowHalf()) {
2550 work_line_->SetResultRegisterType(return_type);
2551 } else {
2552 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2553 }
2554 just_set_result = true;
2555 }
2556 break;
2557 }
2558
Ian Rogersd81871c2011-10-03 13:57:23 -07002559 /* These should never appear during verification. */
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002560 case Instruction::UNUSED_3E:
2561 case Instruction::UNUSED_3F:
2562 case Instruction::UNUSED_40:
2563 case Instruction::UNUSED_41:
2564 case Instruction::UNUSED_42:
2565 case Instruction::UNUSED_43:
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002566 case Instruction::UNUSED_79:
2567 case Instruction::UNUSED_7A:
2568 case Instruction::UNUSED_EB:
2569 case Instruction::UNUSED_EC:
jeffhao9a4f0032012-08-30 16:17:40 -07002570 case Instruction::UNUSED_ED:
jeffhaobdb76512011-09-07 11:43:16 -07002571 case Instruction::UNUSED_EE:
2572 case Instruction::UNUSED_EF:
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002573 case Instruction::UNUSED_F0:
2574 case Instruction::UNUSED_F1:
jeffhaobdb76512011-09-07 11:43:16 -07002575 case Instruction::UNUSED_F2:
2576 case Instruction::UNUSED_F3:
2577 case Instruction::UNUSED_F4:
2578 case Instruction::UNUSED_F5:
2579 case Instruction::UNUSED_F6:
2580 case Instruction::UNUSED_F7:
2581 case Instruction::UNUSED_F8:
2582 case Instruction::UNUSED_F9:
2583 case Instruction::UNUSED_FA:
2584 case Instruction::UNUSED_FB:
jeffhaobdb76512011-09-07 11:43:16 -07002585 case Instruction::UNUSED_FC:
jeffhaobdb76512011-09-07 11:43:16 -07002586 case Instruction::UNUSED_FD:
jeffhaobdb76512011-09-07 11:43:16 -07002587 case Instruction::UNUSED_FE:
jeffhaobdb76512011-09-07 11:43:16 -07002588 case Instruction::UNUSED_FF:
jeffhaod5347e02012-03-22 17:25:05 -07002589 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Unexpected opcode " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07002590 break;
2591
2592 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002593 * DO NOT add a "default" clause here. Without it the compiler will
jeffhaobdb76512011-09-07 11:43:16 -07002594 * complain if an instruction is missing (which is desirable).
2595 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002596 } // end - switch (dec_insn.opcode)
jeffhaobdb76512011-09-07 11:43:16 -07002597
Ian Rogersad0b3a32012-04-16 14:50:24 -07002598 if (have_pending_hard_failure_) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002599 if (Runtime::Current()->IsCompiler()) {
jeffhaob57e9522012-04-26 18:08:21 -07002600 /* When compiling, check that the last failure is a hard failure */
Ian Rogersad0b3a32012-04-16 14:50:24 -07002601 CHECK_EQ(failures_[failures_.size() - 1], VERIFY_ERROR_BAD_CLASS_HARD);
Ian Rogerse1758fe2012-04-19 11:31:15 -07002602 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002603 /* immediate failure, reject class */
2604 info_messages_ << "Rejecting opcode " << inst->DumpString(dex_file_);
2605 return false;
jeffhaofaf459e2012-08-31 15:32:47 -07002606 } else if (have_pending_runtime_throw_failure_) {
2607 /* slow path will throw, mark following code as unreachable */
2608 opcode_flags = Instruction::kThrow;
jeffhaobdb76512011-09-07 11:43:16 -07002609 }
jeffhaobdb76512011-09-07 11:43:16 -07002610 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002611 * If we didn't just set the result register, clear it out. This ensures that you can only use
2612 * "move-result" immediately after the result is set. (We could check this statically, but it's
2613 * not expensive and it makes our debugging output cleaner.)
jeffhaobdb76512011-09-07 11:43:16 -07002614 */
2615 if (!just_set_result) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002616 work_line_->SetResultTypeToUnknown();
jeffhaobdb76512011-09-07 11:43:16 -07002617 }
2618
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002619
jeffhaobdb76512011-09-07 11:43:16 -07002620
2621 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002622 * Handle "branch". Tag the branch target.
jeffhaobdb76512011-09-07 11:43:16 -07002623 *
2624 * NOTE: instructions like Instruction::EQZ provide information about the
jeffhaod1f0fde2011-09-08 17:25:33 -07002625 * state of the register when the branch is taken or not taken. For example,
jeffhaobdb76512011-09-07 11:43:16 -07002626 * somebody could get a reference field, check it for zero, and if the
2627 * branch is taken immediately store that register in a boolean field
jeffhaod1f0fde2011-09-08 17:25:33 -07002628 * since the value is known to be zero. We do not currently account for
jeffhaobdb76512011-09-07 11:43:16 -07002629 * that, and will reject the code.
2630 *
2631 * TODO: avoid re-fetching the branch target
2632 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002633 if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002634 bool isConditional, selfOkay;
Ian Rogersd81871c2011-10-03 13:57:23 -07002635 if (!GetBranchOffset(work_insn_idx_, &branch_target, &isConditional, &selfOkay)) {
jeffhaobdb76512011-09-07 11:43:16 -07002636 /* should never happen after static verification */
jeffhaod5347e02012-03-22 17:25:05 -07002637 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad branch";
jeffhaobdb76512011-09-07 11:43:16 -07002638 return false;
2639 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08002640 DCHECK_EQ(isConditional, (opcode_flags & Instruction::kContinue) != 0);
jeffhaod5347e02012-03-22 17:25:05 -07002641 if (!CheckNotMoveException(code_item_->insns_, work_insn_idx_ + branch_target)) {
jeffhaobdb76512011-09-07 11:43:16 -07002642 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002643 }
jeffhaobdb76512011-09-07 11:43:16 -07002644 /* update branch target, set "changed" if appropriate */
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002645 if (NULL != branch_line.get()) {
2646 if (!UpdateRegisters(work_insn_idx_ + branch_target, branch_line.get())) {
2647 return false;
2648 }
2649 } else {
2650 if (!UpdateRegisters(work_insn_idx_ + branch_target, work_line_.get())) {
2651 return false;
2652 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002653 }
jeffhaobdb76512011-09-07 11:43:16 -07002654 }
2655
2656 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002657 * Handle "switch". Tag all possible branch targets.
jeffhaobdb76512011-09-07 11:43:16 -07002658 *
2659 * We've already verified that the table is structurally sound, so we
2660 * just need to walk through and tag the targets.
2661 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002662 if ((opcode_flags & Instruction::kSwitch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002663 int offset_to_switch = insns[1] | (((int32_t) insns[2]) << 16);
2664 const uint16_t* switch_insns = insns + offset_to_switch;
2665 int switch_count = switch_insns[1];
2666 int offset_to_targets, targ;
2667
2668 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
2669 /* 0 = sig, 1 = count, 2/3 = first key */
2670 offset_to_targets = 4;
2671 } else {
2672 /* 0 = sig, 1 = count, 2..count * 2 = keys */
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07002673 DCHECK((*insns & 0xff) == Instruction::SPARSE_SWITCH);
jeffhaobdb76512011-09-07 11:43:16 -07002674 offset_to_targets = 2 + 2 * switch_count;
2675 }
2676
2677 /* verify each switch target */
2678 for (targ = 0; targ < switch_count; targ++) {
2679 int offset;
2680 uint32_t abs_offset;
2681
2682 /* offsets are 32-bit, and only partly endian-swapped */
2683 offset = switch_insns[offset_to_targets + targ * 2] |
2684 (((int32_t) switch_insns[offset_to_targets + targ * 2 + 1]) << 16);
Ian Rogersd81871c2011-10-03 13:57:23 -07002685 abs_offset = work_insn_idx_ + offset;
2686 DCHECK_LT(abs_offset, code_item_->insns_size_in_code_units_);
jeffhaod5347e02012-03-22 17:25:05 -07002687 if (!CheckNotMoveException(code_item_->insns_, abs_offset)) {
jeffhaobdb76512011-09-07 11:43:16 -07002688 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002689 }
2690 if (!UpdateRegisters(abs_offset, work_line_.get()))
jeffhaobdb76512011-09-07 11:43:16 -07002691 return false;
2692 }
2693 }
2694
2695 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002696 * Handle instructions that can throw and that are sitting in a "try" block. (If they're not in a
2697 * "try" block when they throw, control transfers out of the method.)
jeffhaobdb76512011-09-07 11:43:16 -07002698 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002699 if ((opcode_flags & Instruction::kThrow) != 0 && insn_flags_[work_insn_idx_].IsInTry()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002700 bool within_catch_all = false;
Ian Rogers0571d352011-11-03 19:51:38 -07002701 CatchHandlerIterator iterator(*code_item_, work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07002702
Ian Rogers0571d352011-11-03 19:51:38 -07002703 for (; iterator.HasNext(); iterator.Next()) {
2704 if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002705 within_catch_all = true;
2706 }
jeffhaobdb76512011-09-07 11:43:16 -07002707 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002708 * Merge registers into the "catch" block. We want to use the "savedRegs" rather than
2709 * "work_regs", because at runtime the exception will be thrown before the instruction
2710 * modifies any registers.
jeffhaobdb76512011-09-07 11:43:16 -07002711 */
Ian Rogers0571d352011-11-03 19:51:38 -07002712 if (!UpdateRegisters(iterator.GetHandlerAddress(), saved_line_.get())) {
jeffhaobdb76512011-09-07 11:43:16 -07002713 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002714 }
jeffhaobdb76512011-09-07 11:43:16 -07002715 }
2716
2717 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002718 * If the monitor stack depth is nonzero, there must be a "catch all" handler for this
2719 * instruction. This does apply to monitor-exit because of async exception handling.
jeffhaobdb76512011-09-07 11:43:16 -07002720 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002721 if (work_line_->MonitorStackDepth() > 0 && !within_catch_all) {
jeffhaobdb76512011-09-07 11:43:16 -07002722 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002723 * The state in work_line reflects the post-execution state. If the current instruction is a
2724 * monitor-enter and the monitor stack was empty, we don't need a catch-all (if it throws,
jeffhaobdb76512011-09-07 11:43:16 -07002725 * it will do so before grabbing the lock).
2726 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002727 if (inst->Opcode() != Instruction::MONITOR_ENTER || work_line_->MonitorStackDepth() != 1) {
jeffhaod5347e02012-03-22 17:25:05 -07002728 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
Ian Rogersd81871c2011-10-03 13:57:23 -07002729 << "expected to be within a catch-all for an instruction where a monitor is held";
jeffhaobdb76512011-09-07 11:43:16 -07002730 return false;
2731 }
2732 }
2733 }
2734
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002735 /* Handle "continue". Tag the next consecutive instruction.
2736 * Note: Keep the code handling "continue" case below the "branch" and "switch" cases,
2737 * because it changes work_line_ when performing peephole optimization
2738 * and this change should not be used in those cases.
2739 */
Ian Rogers6d376ae2013-07-23 15:12:40 -07002740 if ((opcode_flags & Instruction::kContinue) != 0) {
2741 uint32_t next_insn_idx = work_insn_idx_ + CurrentInsnFlags()->GetLengthInCodeUnits();
2742 if (next_insn_idx >= code_item_->insns_size_in_code_units_) {
2743 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Execution can walk off end of code area";
2744 return false;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002745 }
Ian Rogers6d376ae2013-07-23 15:12:40 -07002746 // The only way to get to a move-exception instruction is to get thrown there. Make sure the
2747 // next instruction isn't one.
2748 if (!CheckNotMoveException(code_item_->insns_, next_insn_idx)) {
2749 return false;
2750 }
2751 if (NULL != fallthrough_line.get()) {
2752 // Make workline consistent with fallthrough computed from peephole optimization.
2753 work_line_->CopyFromLine(fallthrough_line.get());
2754 }
Ian Rogersb8c78592013-07-25 23:52:52 +00002755 if (insn_flags_[next_insn_idx].IsReturn()) {
2756 // For returns we only care about the operand to the return, all other registers are dead.
2757 const Instruction* ret_inst = Instruction::At(code_item_->insns_ + next_insn_idx);
2758 Instruction::Code opcode = ret_inst->Opcode();
2759 if ((opcode == Instruction::RETURN_VOID) || (opcode == Instruction::RETURN_VOID_BARRIER)) {
2760 work_line_->MarkAllRegistersAsConflicts();
2761 } else {
2762 if (opcode == Instruction::RETURN_WIDE) {
2763 work_line_->MarkAllRegistersAsConflictsExceptWide(ret_inst->VRegA_11x());
2764 } else {
2765 work_line_->MarkAllRegistersAsConflictsExcept(ret_inst->VRegA_11x());
2766 }
2767 }
2768 }
Ian Rogers6d376ae2013-07-23 15:12:40 -07002769 RegisterLine* next_line = reg_table_.GetLine(next_insn_idx);
2770 if (next_line != NULL) {
2771 // Merge registers into what we have for the next instruction,
2772 // and set the "changed" flag if needed.
2773 if (!UpdateRegisters(next_insn_idx, work_line_.get())) {
2774 return false;
2775 }
2776 } else {
2777 /*
2778 * We're not recording register data for the next instruction, so we don't know what the
2779 * prior state was. We have to assume that something has changed and re-evaluate it.
2780 */
2781 insn_flags_[next_insn_idx].SetChanged();
2782 }
2783 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002784
jeffhaod1f0fde2011-09-08 17:25:33 -07002785 /* If we're returning from the method, make sure monitor stack is empty. */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002786 if ((opcode_flags & Instruction::kReturn) != 0) {
Elliott Hughesb25c3f62012-03-26 16:35:06 -07002787 if (!work_line_->VerifyMonitorStackEmpty()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002788 return false;
2789 }
jeffhaobdb76512011-09-07 11:43:16 -07002790 }
2791
2792 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002793 * Update start_guess. Advance to the next instruction of that's
2794 * possible, otherwise use the branch target if one was found. If
jeffhaobdb76512011-09-07 11:43:16 -07002795 * neither of those exists we're in a return or throw; leave start_guess
2796 * alone and let the caller sort it out.
2797 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002798 if ((opcode_flags & Instruction::kContinue) != 0) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002799 *start_guess = work_insn_idx_ + insn_flags_[work_insn_idx_].GetLengthInCodeUnits();
Elliott Hughesadb8c672012-03-06 16:49:32 -08002800 } else if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002801 /* we're still okay if branch_target is zero */
Ian Rogersd81871c2011-10-03 13:57:23 -07002802 *start_guess = work_insn_idx_ + branch_target;
jeffhaobdb76512011-09-07 11:43:16 -07002803 }
2804
Ian Rogersd81871c2011-10-03 13:57:23 -07002805 DCHECK_LT(*start_guess, code_item_->insns_size_in_code_units_);
2806 DCHECK(insn_flags_[*start_guess].IsOpcode());
jeffhaobdb76512011-09-07 11:43:16 -07002807
2808 return true;
Brian Carlstrom1895ea32013-07-18 13:28:37 -07002809} // NOLINT(readability/fn_size)
jeffhaobdb76512011-09-07 11:43:16 -07002810
Ian Rogers776ac1f2012-04-13 23:36:36 -07002811const RegType& MethodVerifier::ResolveClassAndCheckAccess(uint32_t class_idx) {
Ian Rogers0571d352011-11-03 19:51:38 -07002812 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002813 const RegType& referrer = GetDeclaringClass();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002814 mirror::Class* klass = dex_cache_->GetResolvedType(class_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002815 const RegType& result =
Ian Rogers04f94f42013-06-10 15:09:26 -07002816 klass != NULL ? reg_types_.FromClass(descriptor, klass,
2817 klass->CannotBeAssignedFromOtherTypes())
Ian Rogersb4903572012-10-11 11:52:56 -07002818 : reg_types_.FromDescriptor(class_loader_, descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002819 if (result.IsConflict()) {
2820 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "accessing broken descriptor '" << descriptor
2821 << "' in " << referrer;
2822 return result;
2823 }
Ian Rogerse1758fe2012-04-19 11:31:15 -07002824 if (klass == NULL && !result.IsUnresolvedTypes()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002825 dex_cache_->SetResolvedType(class_idx, result.GetClass());
Ian Rogerse1758fe2012-04-19 11:31:15 -07002826 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002827 // Check if access is allowed. Unresolved types use xxxWithAccessCheck to
Ian Rogers28ad40d2011-10-27 15:19:26 -07002828 // check at runtime if access is allowed and so pass here.
Ian Rogersad0b3a32012-04-16 14:50:24 -07002829 if (!result.IsUnresolvedTypes() && !referrer.IsUnresolvedTypes() && !referrer.CanAccess(result)) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002830 Fail(VERIFY_ERROR_ACCESS_CLASS) << "illegal class access: '"
Ian Rogersad0b3a32012-04-16 14:50:24 -07002831 << referrer << "' -> '" << result << "'";
Ian Rogers28ad40d2011-10-27 15:19:26 -07002832 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002833 return result;
Ian Rogersd81871c2011-10-03 13:57:23 -07002834}
2835
Ian Rogers776ac1f2012-04-13 23:36:36 -07002836const RegType& MethodVerifier::GetCaughtExceptionType() {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002837 const RegType* common_super = NULL;
Ian Rogersd81871c2011-10-03 13:57:23 -07002838 if (code_item_->tries_size_ != 0) {
Ian Rogers0571d352011-11-03 19:51:38 -07002839 const byte* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
Ian Rogersd81871c2011-10-03 13:57:23 -07002840 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
2841 for (uint32_t i = 0; i < handlers_size; i++) {
Ian Rogers0571d352011-11-03 19:51:38 -07002842 CatchHandlerIterator iterator(handlers_ptr);
2843 for (; iterator.HasNext(); iterator.Next()) {
2844 if (iterator.GetHandlerAddress() == (uint32_t) work_insn_idx_) {
2845 if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) {
Ian Rogersb4903572012-10-11 11:52:56 -07002846 common_super = &reg_types_.JavaLangThrowable(false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002847 } else {
Ian Rogers0571d352011-11-03 19:51:38 -07002848 const RegType& exception = ResolveClassAndCheckAccess(iterator.GetHandlerTypeIndex());
Ian Rogersc4762272012-02-01 15:55:55 -08002849 if (common_super == NULL) {
2850 // Unconditionally assign for the first handler. We don't assert this is a Throwable
2851 // as that is caught at runtime
2852 common_super = &exception;
Ian Rogersb4903572012-10-11 11:52:56 -07002853 } else if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(exception)) {
Ian Rogersc4762272012-02-01 15:55:55 -08002854 // We don't know enough about the type and the common path merge will result in
2855 // Conflict. Fail here knowing the correct thing can be done at runtime.
jeffhaod5347e02012-03-22 17:25:05 -07002856 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unexpected non-exception class " << exception;
Ian Rogersad0b3a32012-04-16 14:50:24 -07002857 return reg_types_.Conflict();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002858 } else if (common_super->Equals(exception)) {
Ian Rogersc4762272012-02-01 15:55:55 -08002859 // odd case, but nothing to do
Ian Rogersd81871c2011-10-03 13:57:23 -07002860 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002861 common_super = &common_super->Merge(exception, &reg_types_);
Ian Rogersb4903572012-10-11 11:52:56 -07002862 CHECK(reg_types_.JavaLangThrowable(false).IsAssignableFrom(*common_super));
Ian Rogersd81871c2011-10-03 13:57:23 -07002863 }
2864 }
2865 }
2866 }
Ian Rogers0571d352011-11-03 19:51:38 -07002867 handlers_ptr = iterator.EndDataPointer();
Ian Rogersd81871c2011-10-03 13:57:23 -07002868 }
2869 }
2870 if (common_super == NULL) {
2871 /* no catch blocks, or no catches with classes we can find */
jeffhaod5347e02012-03-22 17:25:05 -07002872 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unable to find exception handler";
Ian Rogersad0b3a32012-04-16 14:50:24 -07002873 return reg_types_.Conflict();
Ian Rogersd81871c2011-10-03 13:57:23 -07002874 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002875 return *common_super;
Ian Rogersd81871c2011-10-03 13:57:23 -07002876}
2877
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002878mirror::AbstractMethod* MethodVerifier::ResolveMethodAndCheckAccess(uint32_t dex_method_idx,
2879 MethodType method_type) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002880 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx);
Ian Rogers90040192011-12-16 08:54:29 -08002881 const RegType& klass_type = ResolveClassAndCheckAccess(method_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002882 if (klass_type.IsConflict()) {
2883 std::string append(" in attempt to access method ");
2884 append += dex_file_->GetMethodName(method_id);
2885 AppendToLastFailMessage(append);
Ian Rogers90040192011-12-16 08:54:29 -08002886 return NULL;
2887 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002888 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers90040192011-12-16 08:54:29 -08002889 return NULL; // Can't resolve Class so no more to do here
2890 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002891 mirror::Class* klass = klass_type.GetClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002892 const RegType& referrer = GetDeclaringClass();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002893 mirror::AbstractMethod* res_method = dex_cache_->GetResolvedMethod(dex_method_idx);
Ian Rogersd81871c2011-10-03 13:57:23 -07002894 if (res_method == NULL) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07002895 const char* name = dex_file_->GetMethodName(method_id);
Ian Rogers0571d352011-11-03 19:51:38 -07002896 std::string signature(dex_file_->CreateMethodSignature(method_id.proto_idx_, NULL));
jeffhao8cd6dda2012-02-22 10:15:34 -08002897
2898 if (method_type == METHOD_DIRECT || method_type == METHOD_STATIC) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002899 res_method = klass->FindDirectMethod(name, signature);
jeffhao8cd6dda2012-02-22 10:15:34 -08002900 } else if (method_type == METHOD_INTERFACE) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002901 res_method = klass->FindInterfaceMethod(name, signature);
2902 } else {
2903 res_method = klass->FindVirtualMethod(name, signature);
2904 }
2905 if (res_method != NULL) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002906 dex_cache_->SetResolvedMethod(dex_method_idx, res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07002907 } else {
jeffhao8cd6dda2012-02-22 10:15:34 -08002908 // If a virtual or interface method wasn't found with the expected type, look in
2909 // the direct methods. This can happen when the wrong invoke type is used or when
2910 // a class has changed, and will be flagged as an error in later checks.
2911 if (method_type == METHOD_INTERFACE || method_type == METHOD_VIRTUAL) {
2912 res_method = klass->FindDirectMethod(name, signature);
2913 }
2914 if (res_method == NULL) {
2915 Fail(VERIFY_ERROR_NO_METHOD) << "couldn't find method "
2916 << PrettyDescriptor(klass) << "." << name
2917 << " " << signature;
2918 return NULL;
2919 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002920 }
2921 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002922 // Make sure calls to constructors are "direct". There are additional restrictions but we don't
2923 // enforce them here.
2924 if (res_method->IsConstructor() && method_type != METHOD_DIRECT) {
jeffhaod5347e02012-03-22 17:25:05 -07002925 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting non-direct call to constructor "
2926 << PrettyMethod(res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07002927 return NULL;
2928 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002929 // Disallow any calls to class initializers.
2930 if (MethodHelper(res_method).IsClassInitializer()) {
jeffhaod5347e02012-03-22 17:25:05 -07002931 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting call to class initializer "
2932 << PrettyMethod(res_method);
jeffhao8cd6dda2012-02-22 10:15:34 -08002933 return NULL;
2934 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002935 // Check if access is allowed.
Ian Rogersad0b3a32012-04-16 14:50:24 -07002936 if (!referrer.CanAccessMember(res_method->GetDeclaringClass(), res_method->GetAccessFlags())) {
jeffhao8cd6dda2012-02-22 10:15:34 -08002937 Fail(VERIFY_ERROR_ACCESS_METHOD) << "illegal method access (call " << PrettyMethod(res_method)
Ian Rogersad0b3a32012-04-16 14:50:24 -07002938 << " from " << referrer << ")";
jeffhaob57e9522012-04-26 18:08:21 -07002939 return res_method;
jeffhao8cd6dda2012-02-22 10:15:34 -08002940 }
jeffhaode0d9c92012-02-27 13:58:13 -08002941 // Check that invoke-virtual and invoke-super are not used on private methods of the same class.
2942 if (res_method->IsPrivate() && method_type == METHOD_VIRTUAL) {
jeffhaod5347e02012-03-22 17:25:05 -07002943 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invoke-super/virtual can't be used on private method "
2944 << PrettyMethod(res_method);
jeffhaode0d9c92012-02-27 13:58:13 -08002945 return NULL;
2946 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002947 // Check that interface methods match interface classes.
2948 if (klass->IsInterface() && method_type != METHOD_INTERFACE) {
2949 Fail(VERIFY_ERROR_CLASS_CHANGE) << "non-interface method " << PrettyMethod(res_method)
2950 << " is in an interface class " << PrettyClass(klass);
2951 return NULL;
2952 } else if (!klass->IsInterface() && method_type == METHOD_INTERFACE) {
2953 Fail(VERIFY_ERROR_CLASS_CHANGE) << "interface method " << PrettyMethod(res_method)
2954 << " is in a non-interface class " << PrettyClass(klass);
2955 return NULL;
2956 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002957 // See if the method type implied by the invoke instruction matches the access flags for the
2958 // target method.
2959 if ((method_type == METHOD_DIRECT && !res_method->IsDirect()) ||
2960 (method_type == METHOD_STATIC && !res_method->IsStatic()) ||
2961 ((method_type == METHOD_VIRTUAL || method_type == METHOD_INTERFACE) && res_method->IsDirect())
2962 ) {
Ian Rogers2fc14272012-08-30 10:56:57 -07002963 Fail(VERIFY_ERROR_CLASS_CHANGE) << "invoke type (" << method_type << ") does not match method "
2964 " type of " << PrettyMethod(res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07002965 return NULL;
2966 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002967 return res_method;
2968}
2969
Sebastien Hertz5243e912013-05-21 10:55:07 +02002970mirror::AbstractMethod* MethodVerifier::VerifyInvocationArgs(const Instruction* inst,
2971 MethodType method_type,
2972 bool is_range,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002973 bool is_super) {
jeffhao8cd6dda2012-02-22 10:15:34 -08002974 // Resolve the method. This could be an abstract or concrete method depending on what sort of call
2975 // we're making.
Sebastien Hertz5243e912013-05-21 10:55:07 +02002976 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
2977 mirror::AbstractMethod* res_method = ResolveMethodAndCheckAccess(method_idx, method_type);
jeffhao8cd6dda2012-02-22 10:15:34 -08002978 if (res_method == NULL) { // error or class is unresolved
2979 return NULL;
2980 }
2981
Ian Rogersd81871c2011-10-03 13:57:23 -07002982 // If we're using invoke-super(method), make sure that the executing method's class' superclass
2983 // has a vtable entry for the target method.
2984 if (is_super) {
2985 DCHECK(method_type == METHOD_VIRTUAL);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002986 const RegType& super = GetDeclaringClass().GetSuperClass(&reg_types_);
Ian Rogers529781d2012-07-23 17:24:29 -07002987 if (super.IsUnresolvedTypes()) {
jeffhao4d8df822012-04-24 17:09:36 -07002988 Fail(VERIFY_ERROR_NO_METHOD) << "unknown super class in invoke-super from "
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002989 << PrettyMethod(dex_method_idx_, *dex_file_)
jeffhao4d8df822012-04-24 17:09:36 -07002990 << " to super " << PrettyMethod(res_method);
2991 return NULL;
2992 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002993 mirror::Class* super_klass = super.GetClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002994 if (res_method->GetMethodIndex() >= super_klass->GetVTable()->GetLength()) {
jeffhao4d8df822012-04-24 17:09:36 -07002995 MethodHelper mh(res_method);
2996 Fail(VERIFY_ERROR_NO_METHOD) << "invalid invoke-super from "
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002997 << PrettyMethod(dex_method_idx_, *dex_file_)
jeffhao4d8df822012-04-24 17:09:36 -07002998 << " to super " << super
2999 << "." << mh.GetName()
3000 << mh.GetSignature();
Ian Rogersd81871c2011-10-03 13:57:23 -07003001 return NULL;
3002 }
3003 }
3004 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003005 // match the call to the signature. Also, we might be calling through an abstract method
Ian Rogersd81871c2011-10-03 13:57:23 -07003006 // definition (which doesn't have register count values).
Sebastien Hertz5243e912013-05-21 10:55:07 +02003007 const size_t expected_args = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
Ian Rogersd81871c2011-10-03 13:57:23 -07003008 /* caught by static verifier */
3009 DCHECK(is_range || expected_args <= 5);
3010 if (expected_args > code_item_->outs_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07003011 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
Ian Rogersd81871c2011-10-03 13:57:23 -07003012 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
3013 return NULL;
3014 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003015
jeffhaobdb76512011-09-07 11:43:16 -07003016 /*
Ian Rogersad0b3a32012-04-16 14:50:24 -07003017 * Check the "this" argument, which must be an instance of the class that declared the method.
3018 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
3019 * rigorous check here (which is okay since we have to do it at runtime).
jeffhaobdb76512011-09-07 11:43:16 -07003020 */
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003021 size_t actual_args = 0;
Ian Rogersd81871c2011-10-03 13:57:23 -07003022 if (!res_method->IsStatic()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003023 const RegType& actual_arg_type = work_line_->GetInvocationThis(inst, is_range);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003024 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
Ian Rogersd81871c2011-10-03 13:57:23 -07003025 return NULL;
3026 }
3027 if (actual_arg_type.IsUninitializedReference() && !res_method->IsConstructor()) {
jeffhaod5347e02012-03-22 17:25:05 -07003028 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
Ian Rogersd81871c2011-10-03 13:57:23 -07003029 return NULL;
3030 }
3031 if (method_type != METHOD_INTERFACE && !actual_arg_type.IsZero()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003032 mirror::Class* klass = res_method->GetDeclaringClass();
Ian Rogers04f94f42013-06-10 15:09:26 -07003033 const RegType& res_method_class =
3034 reg_types_.FromClass(ClassHelper(klass).GetDescriptor(), klass,
3035 klass->CannotBeAssignedFromOtherTypes());
Ian Rogers9074b992011-10-26 17:41:55 -07003036 if (!res_method_class.IsAssignableFrom(actual_arg_type)) {
jeffhaod5347e02012-03-22 17:25:05 -07003037 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003038 << "' not instance of '" << res_method_class << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07003039 return NULL;
3040 }
3041 }
3042 actual_args++;
3043 }
3044 /*
3045 * Process the target method's signature. This signature may or may not
3046 * have been verified, so we can't assume it's properly formed.
3047 */
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003048 MethodHelper mh(res_method);
3049 const DexFile::TypeList* params = mh.GetParameterTypeList();
3050 size_t params_size = params == NULL ? 0 : params->Size();
Sebastien Hertz5243e912013-05-21 10:55:07 +02003051 uint32_t arg[5];
3052 if (!is_range) {
3053 inst->GetArgs(arg);
3054 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003055 for (size_t param_index = 0; param_index < params_size; param_index++) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003056 if (actual_args >= expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07003057 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invalid call to '" << PrettyMethod(res_method)
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003058 << "'. Expected " << expected_args << " arguments, processing argument " << actual_args
3059 << " (where longs/doubles count twice).";
Ian Rogersd81871c2011-10-03 13:57:23 -07003060 return NULL;
3061 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003062 const char* descriptor =
3063 mh.GetTypeDescriptorFromTypeIdx(params->GetTypeItem(param_index).type_idx_);
3064 if (descriptor == NULL) {
jeffhaod5347e02012-03-22 17:25:05 -07003065 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003066 << " missing signature component";
3067 return NULL;
Ian Rogersd81871c2011-10-03 13:57:23 -07003068 }
Ian Rogersb4903572012-10-11 11:52:56 -07003069 const RegType& reg_type = reg_types_.FromDescriptor(class_loader_, descriptor, false);
Sebastien Hertz5243e912013-05-21 10:55:07 +02003070 uint32_t get_reg = is_range ? inst->VRegC_3rc() + actual_args : arg[actual_args];
Ian Rogers84fa0742011-10-25 18:13:30 -07003071 if (!work_line_->VerifyRegisterType(get_reg, reg_type)) {
jeffhaob57e9522012-04-26 18:08:21 -07003072 return res_method;
Ian Rogersd81871c2011-10-03 13:57:23 -07003073 }
3074 actual_args = reg_type.IsLongOrDoubleTypes() ? actual_args + 2 : actual_args + 1;
3075 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003076 if (actual_args != expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07003077 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003078 << " expected " << expected_args << " arguments, found " << actual_args;
Ian Rogersd81871c2011-10-03 13:57:23 -07003079 return NULL;
3080 } else {
3081 return res_method;
3082 }
3083}
3084
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003085mirror::AbstractMethod* MethodVerifier::GetQuickInvokedMethod(const Instruction* inst,
3086 RegisterLine* reg_line,
3087 bool is_range) {
3088 DCHECK(inst->Opcode() == Instruction::INVOKE_VIRTUAL_QUICK ||
3089 inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
3090 const RegType& actual_arg_type = reg_line->GetInvocationThis(inst, is_range);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003091 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
3092 return NULL;
3093 }
3094 mirror::Class* this_class = NULL;
3095 if (!actual_arg_type.IsUnresolvedTypes()) {
3096 this_class = actual_arg_type.GetClass();
3097 } else {
3098 const std::string& descriptor(actual_arg_type.GetDescriptor());
3099 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
3100 this_class = class_linker->FindClass(descriptor.c_str(), class_loader_);
3101 if (this_class == NULL) {
3102 Thread::Current()->ClearException();
3103 // Look for a system class
3104 this_class = class_linker->FindClass(descriptor.c_str(), NULL);
3105 }
3106 }
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003107 if (this_class == NULL) {
3108 return NULL;
3109 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003110 mirror::ObjectArray<mirror::AbstractMethod>* vtable = this_class->GetVTable();
3111 CHECK(vtable != NULL);
3112 uint16_t vtable_index = is_range ? inst->VRegB_3rc() : inst->VRegB_35c();
3113 CHECK(vtable_index < vtable->GetLength());
3114 mirror::AbstractMethod* res_method = vtable->Get(vtable_index);
3115 CHECK(!Thread::Current()->IsExceptionPending());
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003116 return res_method;
3117}
3118
3119mirror::AbstractMethod* MethodVerifier::VerifyInvokeVirtualQuickArgs(const Instruction* inst,
3120 bool is_range) {
3121 DCHECK(Runtime::Current()->IsStarted());
3122 mirror::AbstractMethod* res_method = GetQuickInvokedMethod(inst, work_line_.get(),
3123 is_range);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003124 if (res_method == NULL) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003125 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer method from " << inst->Name();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003126 return NULL;
3127 }
3128 CHECK(!res_method->IsDirect() && !res_method->IsStatic());
3129
3130 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
3131 // match the call to the signature. Also, we might be calling through an abstract method
3132 // definition (which doesn't have register count values).
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003133 const RegType& actual_arg_type = work_line_->GetInvocationThis(inst, is_range);
3134 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
3135 return NULL;
3136 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003137 const size_t expected_args = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
3138 /* caught by static verifier */
3139 DCHECK(is_range || expected_args <= 5);
3140 if (expected_args > code_item_->outs_size_) {
3141 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
3142 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
3143 return NULL;
3144 }
3145
3146 /*
3147 * Check the "this" argument, which must be an instance of the class that declared the method.
3148 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
3149 * rigorous check here (which is okay since we have to do it at runtime).
3150 */
3151 if (actual_arg_type.IsUninitializedReference() && !res_method->IsConstructor()) {
3152 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
3153 return NULL;
3154 }
3155 if (!actual_arg_type.IsZero()) {
3156 mirror::Class* klass = res_method->GetDeclaringClass();
3157 const RegType& res_method_class =
3158 reg_types_.FromClass(ClassHelper(klass).GetDescriptor(), klass,
3159 klass->CannotBeAssignedFromOtherTypes());
3160 if (!res_method_class.IsAssignableFrom(actual_arg_type)) {
3161 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type
3162 << "' not instance of '" << res_method_class << "'";
3163 return NULL;
3164 }
3165 }
3166 /*
3167 * Process the target method's signature. This signature may or may not
3168 * have been verified, so we can't assume it's properly formed.
3169 */
3170 MethodHelper mh(res_method);
3171 const DexFile::TypeList* params = mh.GetParameterTypeList();
3172 size_t params_size = params == NULL ? 0 : params->Size();
3173 uint32_t arg[5];
3174 if (!is_range) {
3175 inst->GetArgs(arg);
3176 }
3177 size_t actual_args = 1;
3178 for (size_t param_index = 0; param_index < params_size; param_index++) {
3179 if (actual_args >= expected_args) {
3180 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invalid call to '" << PrettyMethod(res_method)
Brian Carlstrom93c33962013-07-26 10:37:43 -07003181 << "'. Expected " << expected_args
3182 << " arguments, processing argument " << actual_args
3183 << " (where longs/doubles count twice).";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003184 return NULL;
3185 }
3186 const char* descriptor =
3187 mh.GetTypeDescriptorFromTypeIdx(params->GetTypeItem(param_index).type_idx_);
3188 if (descriptor == NULL) {
3189 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003190 << " missing signature component";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003191 return NULL;
3192 }
3193 const RegType& reg_type = reg_types_.FromDescriptor(class_loader_, descriptor, false);
3194 uint32_t get_reg = is_range ? inst->VRegC_3rc() + actual_args : arg[actual_args];
3195 if (!work_line_->VerifyRegisterType(get_reg, reg_type)) {
3196 return res_method;
3197 }
3198 actual_args = reg_type.IsLongOrDoubleTypes() ? actual_args + 2 : actual_args + 1;
3199 }
3200 if (actual_args != expected_args) {
3201 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
3202 << " expected " << expected_args << " arguments, found " << actual_args;
3203 return NULL;
3204 } else {
3205 return res_method;
3206 }
3207}
3208
Ian Rogers62342ec2013-06-11 10:26:37 -07003209void MethodVerifier::VerifyNewArray(const Instruction* inst, bool is_filled, bool is_range) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003210 uint32_t type_idx;
3211 if (!is_filled) {
3212 DCHECK_EQ(inst->Opcode(), Instruction::NEW_ARRAY);
3213 type_idx = inst->VRegC_22c();
3214 } else if (!is_range) {
3215 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY);
3216 type_idx = inst->VRegB_35c();
3217 } else {
3218 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY_RANGE);
3219 type_idx = inst->VRegB_3rc();
3220 }
3221 const RegType& res_type = ResolveClassAndCheckAccess(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003222 if (res_type.IsConflict()) { // bad class
3223 DCHECK_NE(failures_.size(), 0U);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003224 } else {
3225 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
3226 if (!res_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003227 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "new-array on non-array class " << res_type;
Ian Rogers0c4a5062012-02-03 15:18:59 -08003228 } else if (!is_filled) {
3229 /* make sure "size" register is valid type */
Sebastien Hertz5243e912013-05-21 10:55:07 +02003230 work_line_->VerifyRegisterType(inst->VRegB_22c(), reg_types_.Integer());
Ian Rogers0c4a5062012-02-03 15:18:59 -08003231 /* set register type to array class */
Ian Rogers62342ec2013-06-11 10:26:37 -07003232 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
3233 work_line_->SetRegisterType(inst->VRegA_22c(), precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003234 } else {
3235 // Verify each register. If "arg_count" is bad, VerifyRegisterType() will run off the end of
3236 // the list and fail. It's legal, if silly, for arg_count to be zero.
Ian Rogersad0b3a32012-04-16 14:50:24 -07003237 const RegType& expected_type = reg_types_.GetComponentType(res_type, class_loader_);
Sebastien Hertz5243e912013-05-21 10:55:07 +02003238 uint32_t arg_count = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
3239 uint32_t arg[5];
3240 if (!is_range) {
3241 inst->GetArgs(arg);
3242 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08003243 for (size_t ui = 0; ui < arg_count; ui++) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003244 uint32_t get_reg = is_range ? inst->VRegC_3rc() + ui : arg[ui];
Ian Rogers0c4a5062012-02-03 15:18:59 -08003245 if (!work_line_->VerifyRegisterType(get_reg, expected_type)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003246 work_line_->SetResultRegisterType(reg_types_.Conflict());
Ian Rogers0c4a5062012-02-03 15:18:59 -08003247 return;
3248 }
3249 }
3250 // filled-array result goes into "result" register
Ian Rogers62342ec2013-06-11 10:26:37 -07003251 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
3252 work_line_->SetResultRegisterType(precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003253 }
3254 }
3255}
3256
Sebastien Hertz5243e912013-05-21 10:55:07 +02003257void MethodVerifier::VerifyAGet(const Instruction* inst,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003258 const RegType& insn_type, bool is_primitive) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003259 const RegType& index_type = work_line_->GetRegisterType(inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07003260 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003261 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07003262 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003263 const RegType& array_type = work_line_->GetRegisterType(inst->VRegB_23x());
Ian Rogers89310de2012-02-01 13:47:30 -08003264 if (array_type.IsZero()) {
3265 // Null array class; this code path will fail at runtime. Infer a merge-able type from the
3266 // instruction type. TODO: have a proper notion of bottom here.
3267 if (!is_primitive || insn_type.IsCategory1Types()) {
3268 // Reference or category 1
Sebastien Hertz5243e912013-05-21 10:55:07 +02003269 work_line_->SetRegisterType(inst->VRegA_23x(), reg_types_.Zero());
Ian Rogersd81871c2011-10-03 13:57:23 -07003270 } else {
Ian Rogers89310de2012-02-01 13:47:30 -08003271 // Category 2
Sebastien Hertz5243e912013-05-21 10:55:07 +02003272 work_line_->SetRegisterTypeWide(inst->VRegA_23x(), reg_types_.FromCat2ConstLo(0, false),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003273 reg_types_.FromCat2ConstHi(0, false));
Ian Rogers89310de2012-02-01 13:47:30 -08003274 }
jeffhaofc3144e2012-02-01 17:21:15 -08003275 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003276 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aget";
Ian Rogers89310de2012-02-01 13:47:30 -08003277 } else {
3278 /* verify the class */
Ian Rogersad0b3a32012-04-16 14:50:24 -07003279 const RegType& component_type = reg_types_.GetComponentType(array_type, class_loader_);
jeffhaofc3144e2012-02-01 17:21:15 -08003280 if (!component_type.IsReferenceTypes() && !is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07003281 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08003282 << " source for aget-object";
3283 } else if (component_type.IsNonZeroReferenceTypes() && is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07003284 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08003285 << " source for category 1 aget";
3286 } else if (is_primitive && !insn_type.Equals(component_type) &&
3287 !((insn_type.IsInteger() && component_type.IsFloat()) ||
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003288 (insn_type.IsLong() && component_type.IsDouble()))) {
3289 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array type " << array_type
3290 << " incompatible with aget of type " << insn_type;
Ian Rogers89310de2012-02-01 13:47:30 -08003291 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003292 // Use knowledge of the field type which is stronger than the type inferred from the
3293 // instruction, which can't differentiate object types and ints from floats, longs from
3294 // doubles.
3295 if (!component_type.IsLowHalf()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003296 work_line_->SetRegisterType(inst->VRegA_23x(), component_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003297 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003298 work_line_->SetRegisterTypeWide(inst->VRegA_23x(), component_type,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003299 component_type.HighHalf(&reg_types_));
3300 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003301 }
3302 }
3303 }
3304}
3305
Sebastien Hertz5243e912013-05-21 10:55:07 +02003306void MethodVerifier::VerifyAPut(const Instruction* inst,
Ian Rogersd81871c2011-10-03 13:57:23 -07003307 const RegType& insn_type, bool is_primitive) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003308 const RegType& index_type = work_line_->GetRegisterType(inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07003309 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003310 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07003311 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003312 const RegType& array_type = work_line_->GetRegisterType(inst->VRegB_23x());
Ian Rogers89310de2012-02-01 13:47:30 -08003313 if (array_type.IsZero()) {
3314 // Null array type; this code path will fail at runtime. Infer a merge-able type from the
3315 // instruction type.
jeffhaofc3144e2012-02-01 17:21:15 -08003316 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003317 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aput";
Ian Rogers89310de2012-02-01 13:47:30 -08003318 } else {
3319 /* verify the class */
Ian Rogersad0b3a32012-04-16 14:50:24 -07003320 const RegType& component_type = reg_types_.GetComponentType(array_type, class_loader_);
jeffhaofc3144e2012-02-01 17:21:15 -08003321 if (!component_type.IsReferenceTypes() && !is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07003322 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08003323 << " source for aput-object";
3324 } else if (component_type.IsNonZeroReferenceTypes() && is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07003325 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08003326 << " source for category 1 aput";
3327 } else if (is_primitive && !insn_type.Equals(component_type) &&
3328 !((insn_type.IsInteger() && component_type.IsFloat()) ||
3329 (insn_type.IsLong() && component_type.IsDouble()))) {
jeffhaod5347e02012-03-22 17:25:05 -07003330 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08003331 << " incompatible with aput of type " << insn_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07003332 } else {
Ian Rogers89310de2012-02-01 13:47:30 -08003333 // The instruction agrees with the type of array, confirm the value to be stored does too
3334 // Note: we use the instruction type (rather than the component type) for aput-object as
3335 // incompatible classes will be caught at runtime as an array store exception
Brian Carlstrom93c33962013-07-26 10:37:43 -07003336 work_line_->VerifyRegisterType(inst->VRegA_23x(),
3337 is_primitive ? component_type : insn_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07003338 }
3339 }
3340 }
3341}
3342
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003343mirror::Field* MethodVerifier::GetStaticField(int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08003344 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3345 // Check access to class
3346 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003347 if (klass_type.IsConflict()) { // bad class
3348 AppendToLastFailMessage(StringPrintf(" in attempt to access static field %d (%s) in %s",
3349 field_idx, dex_file_->GetFieldName(field_id),
3350 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers90040192011-12-16 08:54:29 -08003351 return NULL;
3352 }
Elliott Hughesb25c3f62012-03-26 16:35:06 -07003353 if (klass_type.IsUnresolvedTypes()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003354 return NULL; // Can't resolve Class so no more to do here, will do checking at runtime.
Ian Rogers90040192011-12-16 08:54:29 -08003355 }
Brian Carlstrom93c33962013-07-26 10:37:43 -07003356 mirror::Field* field = Runtime::Current()->GetClassLinker()->ResolveFieldJLS(*dex_file_,
3357 field_idx,
3358 dex_cache_,
3359 class_loader_);
Ian Rogersd81871c2011-10-03 13:57:23 -07003360 if (field == NULL) {
Ian Rogers637c65b2013-05-31 11:46:00 -07003361 LOG(INFO) << "Unable to resolve static field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07003362 << dex_file_->GetFieldName(field_id) << ") in "
3363 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07003364 DCHECK(Thread::Current()->IsExceptionPending());
3365 Thread::Current()->ClearException();
3366 return NULL;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003367 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
3368 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003369 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access static field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07003370 << " from " << GetDeclaringClass();
Ian Rogersd81871c2011-10-03 13:57:23 -07003371 return NULL;
3372 } else if (!field->IsStatic()) {
3373 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field) << " to be static";
3374 return NULL;
3375 } else {
3376 return field;
3377 }
3378}
3379
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003380mirror::Field* MethodVerifier::GetInstanceField(const RegType& obj_type, int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08003381 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3382 // Check access to class
3383 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003384 if (klass_type.IsConflict()) {
3385 AppendToLastFailMessage(StringPrintf(" in attempt to access instance field %d (%s) in %s",
3386 field_idx, dex_file_->GetFieldName(field_id),
3387 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers90040192011-12-16 08:54:29 -08003388 return NULL;
3389 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003390 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers90040192011-12-16 08:54:29 -08003391 return NULL; // Can't resolve Class so no more to do here
3392 }
Brian Carlstrom93c33962013-07-26 10:37:43 -07003393 mirror::Field* field = Runtime::Current()->GetClassLinker()->ResolveFieldJLS(*dex_file_,
3394 field_idx,
3395 dex_cache_,
3396 class_loader_);
Ian Rogersd81871c2011-10-03 13:57:23 -07003397 if (field == NULL) {
Ian Rogers637c65b2013-05-31 11:46:00 -07003398 LOG(INFO) << "Unable to resolve instance field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07003399 << dex_file_->GetFieldName(field_id) << ") in "
3400 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07003401 DCHECK(Thread::Current()->IsExceptionPending());
3402 Thread::Current()->ClearException();
3403 return NULL;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003404 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
3405 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003406 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access instance field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07003407 << " from " << GetDeclaringClass();
Ian Rogersd81871c2011-10-03 13:57:23 -07003408 return NULL;
3409 } else if (field->IsStatic()) {
3410 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field)
3411 << " to not be static";
3412 return NULL;
3413 } else if (obj_type.IsZero()) {
3414 // Cannot infer and check type, however, access will cause null pointer exception
3415 return field;
Ian Rogerse1758fe2012-04-19 11:31:15 -07003416 } else {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003417 mirror::Class* klass = field->GetDeclaringClass();
Ian Rogers637c65b2013-05-31 11:46:00 -07003418 const RegType& field_klass =
3419 reg_types_.FromClass(dex_file_->GetFieldDeclaringClassDescriptor(field_id),
Ian Rogers04f94f42013-06-10 15:09:26 -07003420 klass, klass->CannotBeAssignedFromOtherTypes());
Ian Rogersad0b3a32012-04-16 14:50:24 -07003421 if (obj_type.IsUninitializedTypes() &&
3422 (!IsConstructor() || GetDeclaringClass().Equals(obj_type) ||
3423 !field_klass.Equals(GetDeclaringClass()))) {
3424 // Field accesses through uninitialized references are only allowable for constructors where
3425 // the field is declared in this class
3426 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "cannot access instance field " << PrettyField(field)
Brian Carlstrom93c33962013-07-26 10:37:43 -07003427 << " of a not fully initialized object within the context"
3428 << " of " << PrettyMethod(dex_method_idx_, *dex_file_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003429 return NULL;
3430 } else if (!field_klass.IsAssignableFrom(obj_type)) {
3431 // Trying to access C1.field1 using reference of type C2, which is neither C1 or a sub-class
3432 // of C1. For resolution to occur the declared class of the field must be compatible with
3433 // obj_type, we've discovered this wasn't so, so report the field didn't exist.
3434 Fail(VERIFY_ERROR_NO_FIELD) << "cannot access instance field " << PrettyField(field)
3435 << " from object of type " << obj_type;
3436 return NULL;
3437 } else {
3438 return field;
3439 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003440 }
3441}
3442
Sebastien Hertz5243e912013-05-21 10:55:07 +02003443void MethodVerifier::VerifyISGet(const Instruction* inst, const RegType& insn_type,
3444 bool is_primitive, bool is_static) {
3445 uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003446 mirror::Field* field;
Ian Rogersb94a27b2011-10-26 00:33:41 -07003447 if (is_static) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07003448 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003449 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003450 const RegType& object_type = work_line_->GetRegisterType(inst->VRegB_22c());
Ian Rogersf4028cc2011-11-02 14:56:39 -07003451 field = GetInstanceField(object_type, field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003452 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003453 const char* descriptor;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003454 mirror::ClassLoader* loader;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003455 if (field != NULL) {
3456 descriptor = FieldHelper(field).GetTypeDescriptor();
3457 loader = field->GetDeclaringClass()->GetClassLoader();
Ian Rogersf4028cc2011-11-02 14:56:39 -07003458 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003459 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3460 descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
3461 loader = class_loader_;
Ian Rogers0d604842012-04-16 14:50:24 -07003462 }
Ian Rogersb4903572012-10-11 11:52:56 -07003463 const RegType& field_type = reg_types_.FromDescriptor(loader, descriptor, false);
Sebastien Hertz5243e912013-05-21 10:55:07 +02003464 const uint32_t vregA = (is_static) ? inst->VRegA_21c() : inst->VRegA_22c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07003465 if (is_primitive) {
3466 if (field_type.Equals(insn_type) ||
3467 (field_type.IsFloat() && insn_type.IsIntegralTypes()) ||
3468 (field_type.IsDouble() && insn_type.IsLongTypes())) {
3469 // expected that read is of the correct primitive type or that int reads are reading
3470 // floats or long reads are reading doubles
3471 } else {
3472 // This is a global failure rather than a class change failure as the instructions and
3473 // the descriptors for the type should have been consistent within the same file at
3474 // compile time
3475 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
3476 << " to be of type '" << insn_type
3477 << "' but found type '" << field_type << "' in get";
Ian Rogersad0b3a32012-04-16 14:50:24 -07003478 return;
3479 }
3480 } else {
3481 if (!insn_type.IsAssignableFrom(field_type)) {
3482 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
3483 << " to be compatible with type '" << insn_type
3484 << "' but found type '" << field_type
3485 << "' in get-object";
Sebastien Hertz5243e912013-05-21 10:55:07 +02003486 work_line_->SetRegisterType(vregA, reg_types_.Conflict());
Ian Rogersad0b3a32012-04-16 14:50:24 -07003487 return;
3488 }
3489 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003490 if (!field_type.IsLowHalf()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003491 work_line_->SetRegisterType(vregA, field_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003492 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003493 work_line_->SetRegisterTypeWide(vregA, field_type, field_type.HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003494 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003495}
3496
Sebastien Hertz5243e912013-05-21 10:55:07 +02003497void MethodVerifier::VerifyISPut(const Instruction* inst, const RegType& insn_type,
3498 bool is_primitive, bool is_static) {
3499 uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003500 mirror::Field* field;
Ian Rogersb94a27b2011-10-26 00:33:41 -07003501 if (is_static) {
Ian Rogers55d249f2011-11-02 16:48:09 -07003502 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003503 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003504 const RegType& object_type = work_line_->GetRegisterType(inst->VRegB_22c());
Ian Rogers55d249f2011-11-02 16:48:09 -07003505 field = GetInstanceField(object_type, field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003506 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003507 const char* descriptor;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003508 mirror::ClassLoader* loader;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003509 if (field != NULL) {
3510 descriptor = FieldHelper(field).GetTypeDescriptor();
3511 loader = field->GetDeclaringClass()->GetClassLoader();
Ian Rogers55d249f2011-11-02 16:48:09 -07003512 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003513 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3514 descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
3515 loader = class_loader_;
3516 }
Ian Rogersb4903572012-10-11 11:52:56 -07003517 const RegType& field_type = reg_types_.FromDescriptor(loader, descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003518 if (field != NULL) {
3519 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
3520 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
3521 << " from other class " << GetDeclaringClass();
3522 return;
3523 }
3524 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02003525 const uint32_t vregA = (is_static) ? inst->VRegA_21c() : inst->VRegA_22c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07003526 if (is_primitive) {
3527 // Primitive field assignability rules are weaker than regular assignability rules
3528 bool instruction_compatible;
3529 bool value_compatible;
Sebastien Hertz5243e912013-05-21 10:55:07 +02003530 const RegType& value_type = work_line_->GetRegisterType(vregA);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003531 if (field_type.IsIntegralTypes()) {
3532 instruction_compatible = insn_type.IsIntegralTypes();
3533 value_compatible = value_type.IsIntegralTypes();
3534 } else if (field_type.IsFloat()) {
3535 instruction_compatible = insn_type.IsInteger(); // no [is]put-float, so expect [is]put-int
3536 value_compatible = value_type.IsFloatTypes();
3537 } else if (field_type.IsLong()) {
3538 instruction_compatible = insn_type.IsLong();
3539 value_compatible = value_type.IsLongTypes();
3540 } else if (field_type.IsDouble()) {
3541 instruction_compatible = insn_type.IsLong(); // no [is]put-double, so expect [is]put-long
3542 value_compatible = value_type.IsDoubleTypes();
Ian Rogers55d249f2011-11-02 16:48:09 -07003543 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003544 instruction_compatible = false; // reference field with primitive store
3545 value_compatible = false; // unused
Ian Rogersd81871c2011-10-03 13:57:23 -07003546 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003547 if (!instruction_compatible) {
3548 // This is a global failure rather than a class change failure as the instructions and
3549 // the descriptors for the type should have been consistent within the same file at
3550 // compile time
3551 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
3552 << " to be of type '" << insn_type
3553 << "' but found type '" << field_type
3554 << "' in put";
3555 return;
Ian Rogers55d249f2011-11-02 16:48:09 -07003556 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003557 if (!value_compatible) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003558 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
Ian Rogersad0b3a32012-04-16 14:50:24 -07003559 << " of type " << value_type
3560 << " but expected " << field_type
3561 << " for store to " << PrettyField(field) << " in put";
3562 return;
Ian Rogersd81871c2011-10-03 13:57:23 -07003563 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003564 } else {
3565 if (!insn_type.IsAssignableFrom(field_type)) {
3566 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
3567 << " to be compatible with type '" << insn_type
3568 << "' but found type '" << field_type
3569 << "' in put-object";
3570 return;
3571 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02003572 work_line_->VerifyRegisterType(vregA, field_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07003573 }
3574}
3575
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003576// Look for an instance field with this offset.
3577// TODO: we may speed up the search if offsets are sorted by doing a quick search.
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003578static mirror::Field* FindInstanceFieldWithOffset(const mirror::Class* klass,
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003579 uint32_t field_offset)
3580 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003581 const mirror::ObjectArray<mirror::Field>* instance_fields = klass->GetIFields();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003582 if (instance_fields != NULL) {
3583 for (int32_t i = 0, e = instance_fields->GetLength(); i < e; ++i) {
3584 mirror::Field* field = instance_fields->Get(i);
3585 if (field->GetOffset().Uint32Value() == field_offset) {
3586 return field;
3587 }
3588 }
3589 }
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003590 // We did not find field in class: look into superclass.
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003591 if (klass->GetSuperClass() != NULL) {
3592 return FindInstanceFieldWithOffset(klass->GetSuperClass(), field_offset);
3593 } else {
3594 return NULL;
3595 }
3596}
3597
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003598// Returns the access field of a quick field access (iget/iput-quick) or NULL
3599// if it cannot be found.
3600mirror::Field* MethodVerifier::GetQuickFieldAccess(const Instruction* inst,
3601 RegisterLine* reg_line) {
3602 DCHECK(inst->Opcode() == Instruction::IGET_QUICK ||
3603 inst->Opcode() == Instruction::IGET_WIDE_QUICK ||
3604 inst->Opcode() == Instruction::IGET_OBJECT_QUICK ||
3605 inst->Opcode() == Instruction::IPUT_QUICK ||
3606 inst->Opcode() == Instruction::IPUT_WIDE_QUICK ||
3607 inst->Opcode() == Instruction::IPUT_OBJECT_QUICK);
3608 const RegType& object_type = reg_line->GetRegisterType(inst->VRegB_22c());
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003609 mirror::Class* object_class = NULL;
3610 if (!object_type.IsUnresolvedTypes()) {
3611 object_class = object_type.GetClass();
3612 } else {
3613 // We need to resolve the class from its descriptor.
3614 const std::string& descriptor(object_type.GetDescriptor());
3615 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
3616 object_class = class_linker->FindClass(descriptor.c_str(), class_loader_);
3617 if (object_class == NULL) {
3618 Thread::Current()->ClearException();
3619 // Look for a system class
3620 object_class = class_linker->FindClass(descriptor.c_str(), NULL);
3621 }
3622 }
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003623 if (object_class == NULL) {
3624 // Failed to get the Class* from reg type.
3625 LOG(WARNING) << "Failed to get Class* from " << object_type;
3626 return NULL;
3627 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003628 uint32_t field_offset = static_cast<uint32_t>(inst->VRegC_22c());
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003629 return FindInstanceFieldWithOffset(object_class, field_offset);
3630}
3631
3632void MethodVerifier::VerifyIGetQuick(const Instruction* inst, const RegType& insn_type,
3633 bool is_primitive) {
3634 DCHECK(Runtime::Current()->IsStarted());
3635 mirror::Field* field = GetQuickFieldAccess(inst, work_line_.get());
3636 if (field == NULL) {
3637 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field from " << inst->Name();
3638 return;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003639 }
3640 const char* descriptor = FieldHelper(field).GetTypeDescriptor();
3641 mirror::ClassLoader* loader = field->GetDeclaringClass()->GetClassLoader();
3642 const RegType& field_type = reg_types_.FromDescriptor(loader, descriptor, false);
3643 const uint32_t vregA = inst->VRegA_22c();
3644 if (is_primitive) {
3645 if (field_type.Equals(insn_type) ||
3646 (field_type.IsFloat() && insn_type.IsIntegralTypes()) ||
3647 (field_type.IsDouble() && insn_type.IsLongTypes())) {
3648 // expected that read is of the correct primitive type or that int reads are reading
3649 // floats or long reads are reading doubles
3650 } else {
3651 // This is a global failure rather than a class change failure as the instructions and
3652 // the descriptors for the type should have been consistent within the same file at
3653 // compile time
3654 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003655 << " to be of type '" << insn_type
3656 << "' but found type '" << field_type << "' in get";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003657 return;
3658 }
3659 } else {
3660 if (!insn_type.IsAssignableFrom(field_type)) {
3661 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003662 << " to be compatible with type '" << insn_type
3663 << "' but found type '" << field_type
3664 << "' in get-object";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003665 work_line_->SetRegisterType(vregA, reg_types_.Conflict());
3666 return;
3667 }
3668 }
3669 if (!field_type.IsLowHalf()) {
3670 work_line_->SetRegisterType(vregA, field_type);
3671 } else {
3672 work_line_->SetRegisterTypeWide(vregA, field_type, field_type.HighHalf(&reg_types_));
3673 }
3674}
3675
3676void MethodVerifier::VerifyIPutQuick(const Instruction* inst, const RegType& insn_type,
3677 bool is_primitive) {
3678 DCHECK(Runtime::Current()->IsStarted());
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003679 mirror::Field* field = GetQuickFieldAccess(inst, work_line_.get());
3680 if (field == NULL) {
3681 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field from " << inst->Name();
3682 return;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003683 }
3684 const char* descriptor = FieldHelper(field).GetTypeDescriptor();
3685 mirror::ClassLoader* loader = field->GetDeclaringClass()->GetClassLoader();
3686 const RegType& field_type = reg_types_.FromDescriptor(loader, descriptor, false);
3687 if (field != NULL) {
3688 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
3689 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003690 << " from other class " << GetDeclaringClass();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003691 return;
3692 }
3693 }
3694 const uint32_t vregA = inst->VRegA_22c();
3695 if (is_primitive) {
3696 // Primitive field assignability rules are weaker than regular assignability rules
3697 bool instruction_compatible;
3698 bool value_compatible;
3699 const RegType& value_type = work_line_->GetRegisterType(vregA);
3700 if (field_type.IsIntegralTypes()) {
3701 instruction_compatible = insn_type.IsIntegralTypes();
3702 value_compatible = value_type.IsIntegralTypes();
3703 } else if (field_type.IsFloat()) {
3704 instruction_compatible = insn_type.IsInteger(); // no [is]put-float, so expect [is]put-int
3705 value_compatible = value_type.IsFloatTypes();
3706 } else if (field_type.IsLong()) {
3707 instruction_compatible = insn_type.IsLong();
3708 value_compatible = value_type.IsLongTypes();
3709 } else if (field_type.IsDouble()) {
3710 instruction_compatible = insn_type.IsLong(); // no [is]put-double, so expect [is]put-long
3711 value_compatible = value_type.IsDoubleTypes();
3712 } else {
3713 instruction_compatible = false; // reference field with primitive store
3714 value_compatible = false; // unused
3715 }
3716 if (!instruction_compatible) {
3717 // This is a global failure rather than a class change failure as the instructions and
3718 // the descriptors for the type should have been consistent within the same file at
3719 // compile time
3720 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003721 << " to be of type '" << insn_type
3722 << "' but found type '" << field_type
3723 << "' in put";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003724 return;
3725 }
3726 if (!value_compatible) {
3727 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
3728 << " of type " << value_type
3729 << " but expected " << field_type
3730 << " for store to " << PrettyField(field) << " in put";
3731 return;
3732 }
3733 } else {
3734 if (!insn_type.IsAssignableFrom(field_type)) {
3735 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003736 << " to be compatible with type '" << insn_type
3737 << "' but found type '" << field_type
3738 << "' in put-object";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003739 return;
3740 }
3741 work_line_->VerifyRegisterType(vregA, field_type);
3742 }
3743}
3744
Ian Rogers776ac1f2012-04-13 23:36:36 -07003745bool MethodVerifier::CheckNotMoveException(const uint16_t* insns, int insn_idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003746 if ((insns[insn_idx] & 0xff) == Instruction::MOVE_EXCEPTION) {
jeffhaod5347e02012-03-22 17:25:05 -07003747 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-exception";
Ian Rogersd81871c2011-10-03 13:57:23 -07003748 return false;
3749 }
3750 return true;
3751}
3752
Ian Rogers776ac1f2012-04-13 23:36:36 -07003753bool MethodVerifier::UpdateRegisters(uint32_t next_insn, const RegisterLine* merge_line) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003754 bool changed = true;
3755 RegisterLine* target_line = reg_table_.GetLine(next_insn);
3756 if (!insn_flags_[next_insn].IsVisitedOrChanged()) {
jeffhaobdb76512011-09-07 11:43:16 -07003757 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003758 * We haven't processed this instruction before, and we haven't touched the registers here, so
3759 * there's nothing to "merge". Copy the registers over and mark it as changed. (This is the
3760 * only way a register can transition out of "unknown", so this is not just an optimization.)
jeffhaobdb76512011-09-07 11:43:16 -07003761 */
Ian Rogersb8c78592013-07-25 23:52:52 +00003762 if (!insn_flags_[next_insn].IsReturn()) {
3763 target_line->CopyFromLine(merge_line);
3764 } else {
3765 // For returns we only care about the operand to the return, all other registers are dead.
3766 // Initialize them as conflicts so they don't add to GC and deoptimization information.
3767 const Instruction* ret_inst = Instruction::At(code_item_->insns_ + next_insn);
3768 Instruction::Code opcode = ret_inst->Opcode();
3769 if ((opcode == Instruction::RETURN_VOID) || (opcode == Instruction::RETURN_VOID_BARRIER)) {
3770 target_line->MarkAllRegistersAsConflicts();
3771 } else {
3772 target_line->CopyFromLine(merge_line);
3773 if (opcode == Instruction::RETURN_WIDE) {
3774 target_line->MarkAllRegistersAsConflictsExceptWide(ret_inst->VRegA_11x());
3775 } else {
3776 target_line->MarkAllRegistersAsConflictsExcept(ret_inst->VRegA_11x());
3777 }
3778 }
3779 }
jeffhaobdb76512011-09-07 11:43:16 -07003780 } else {
Brian Carlstrom93c33962013-07-26 10:37:43 -07003781 UniquePtr<RegisterLine> copy(gDebugVerify ?
3782 new RegisterLine(target_line->NumRegs(), this) :
3783 NULL);
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003784 if (gDebugVerify) {
3785 copy->CopyFromLine(target_line);
3786 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003787 changed = target_line->MergeRegisters(merge_line);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003788 if (have_pending_hard_failure_) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003789 return false;
jeffhaobdb76512011-09-07 11:43:16 -07003790 }
Ian Rogers2c8a8572011-10-24 17:11:36 -07003791 if (gDebugVerify && changed) {
Elliott Hughes398f64b2012-03-26 18:05:48 -07003792 LogVerifyInfo() << "Merging at [" << reinterpret_cast<void*>(work_insn_idx_) << "]"
Elliott Hughesc073b072012-05-24 19:29:17 -07003793 << " to [" << reinterpret_cast<void*>(next_insn) << "]: " << "\n"
3794 << *copy.get() << " MERGE\n"
3795 << *merge_line << " ==\n"
3796 << *target_line << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07003797 }
3798 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003799 if (changed) {
3800 insn_flags_[next_insn].SetChanged();
jeffhaobdb76512011-09-07 11:43:16 -07003801 }
3802 return true;
3803}
3804
Ian Rogers7b3ddd22013-02-21 15:19:52 -08003805InstructionFlags* MethodVerifier::CurrentInsnFlags() {
Ian Rogers776ac1f2012-04-13 23:36:36 -07003806 return &insn_flags_[work_insn_idx_];
3807}
3808
Ian Rogersad0b3a32012-04-16 14:50:24 -07003809const RegType& MethodVerifier::GetMethodReturnType() {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003810 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003811 const DexFile::ProtoId& proto_id = dex_file_->GetMethodPrototype(method_id);
3812 uint16_t return_type_idx = proto_id.return_type_idx_;
3813 const char* descriptor = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(return_type_idx));
Ian Rogersb4903572012-10-11 11:52:56 -07003814 return reg_types_.FromDescriptor(class_loader_, descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003815}
3816
3817const RegType& MethodVerifier::GetDeclaringClass() {
Ian Rogers637c65b2013-05-31 11:46:00 -07003818 if (declaring_class_ == NULL) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003819 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
Brian Carlstrom93c33962013-07-26 10:37:43 -07003820 const char* descriptor
3821 = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(method_id.class_idx_));
Ian Rogers637c65b2013-05-31 11:46:00 -07003822 if (mirror_method_ != NULL) {
3823 mirror::Class* klass = mirror_method_->GetDeclaringClass();
Ian Rogers04f94f42013-06-10 15:09:26 -07003824 declaring_class_ = &reg_types_.FromClass(descriptor, klass,
3825 klass->CannotBeAssignedFromOtherTypes());
Ian Rogers637c65b2013-05-31 11:46:00 -07003826 } else {
3827 declaring_class_ = &reg_types_.FromDescriptor(class_loader_, descriptor, false);
3828 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003829 }
Ian Rogers637c65b2013-05-31 11:46:00 -07003830 return *declaring_class_;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003831}
3832
Ian Rogers776ac1f2012-04-13 23:36:36 -07003833void MethodVerifier::ComputeGcMapSizes(size_t* gc_points, size_t* ref_bitmap_bits,
Ian Rogers6d376ae2013-07-23 15:12:40 -07003834 size_t* log2_max_gc_pc) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003835 size_t local_gc_points = 0;
3836 size_t max_insn = 0;
3837 size_t max_ref_reg = -1;
3838 for (size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003839 if (insn_flags_[i].IsCompileTimeInfoPoint()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003840 local_gc_points++;
3841 max_insn = i;
3842 RegisterLine* line = reg_table_.GetLine(i);
Ian Rogers84fa0742011-10-25 18:13:30 -07003843 max_ref_reg = line->GetMaxNonZeroReferenceReg(max_ref_reg);
jeffhaobdb76512011-09-07 11:43:16 -07003844 }
3845 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003846 *gc_points = local_gc_points;
3847 *ref_bitmap_bits = max_ref_reg + 1; // if max register is 0 we need 1 bit to encode (ie +1)
3848 size_t i = 0;
Ian Rogers6b0870d2011-12-15 19:38:12 -08003849 while ((1U << i) <= max_insn) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003850 i++;
3851 }
3852 *log2_max_gc_pc = i;
jeffhaobdb76512011-09-07 11:43:16 -07003853}
3854
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07003855MethodVerifier::MethodSafeCastSet* MethodVerifier::GenerateSafeCastSet() {
3856 /*
3857 * Walks over the method code and adds any cast instructions in which
3858 * the type cast is implicit to a set, which is used in the code generation
3859 * to elide these casts.
3860 */
3861 if (!failure_messages_.empty()) {
3862 return NULL;
3863 }
3864 UniquePtr<MethodSafeCastSet> mscs;
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07003865 const Instruction* inst = Instruction::At(code_item_->insns_);
3866 const Instruction* end = Instruction::At(code_item_->insns_ +
Ian Rogersfae370a2013-06-05 08:33:27 -07003867 code_item_->insns_size_in_code_units_);
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07003868
3869 for (; inst < end; inst = inst->Next()) {
Ian Rogersfae370a2013-06-05 08:33:27 -07003870 if (Instruction::CHECK_CAST != inst->Opcode()) {
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07003871 continue;
Ian Rogersfae370a2013-06-05 08:33:27 -07003872 }
3873 uint32_t dex_pc = inst->GetDexPc(code_item_->insns_);
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07003874 RegisterLine* line = reg_table_.GetLine(dex_pc);
Ian Rogersfae370a2013-06-05 08:33:27 -07003875 const RegType& reg_type(line->GetRegisterType(inst->VRegA_21c()));
3876 const RegType& cast_type = ResolveClassAndCheckAccess(inst->VRegB_21c());
Ian Rogers16e3d2c2013-06-07 12:57:00 -07003877 if (cast_type.IsStrictlyAssignableFrom(reg_type)) {
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07003878 if (mscs.get() == NULL) {
3879 mscs.reset(new MethodSafeCastSet());
3880 }
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07003881 mscs->insert(dex_pc);
3882 }
3883 }
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07003884 return mscs.release();
3885}
3886
Ian Rogersd0583802013-06-01 10:51:46 -07003887MethodVerifier::PcToConcreteMethodMap* MethodVerifier::GenerateDevirtMap() {
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003888 // It is risky to rely on reg_types for sharpening in cases of soft
3889 // verification, we might end up sharpening to a wrong implementation. Just abort.
3890 if (!failure_messages_.empty()) {
3891 return NULL;
3892 }
3893
Ian Rogersd0583802013-06-01 10:51:46 -07003894 UniquePtr<PcToConcreteMethodMap> pc_to_concrete_method_map;
Brian Carlstrom02c8cc62013-07-18 15:54:44 -07003895 const uint16_t* insns = code_item_->insns_;
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003896 const Instruction* inst = Instruction::At(insns);
Ian Rogersd0583802013-06-01 10:51:46 -07003897 const Instruction* end = Instruction::At(insns + code_item_->insns_size_in_code_units_);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003898
Ian Rogersd0583802013-06-01 10:51:46 -07003899 for (; inst < end; inst = inst->Next()) {
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003900 bool is_virtual = (inst->Opcode() == Instruction::INVOKE_VIRTUAL) ||
3901 (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE);
3902 bool is_interface = (inst->Opcode() == Instruction::INVOKE_INTERFACE) ||
3903 (inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE);
3904
Brian Carlstromdf629502013-07-17 22:39:56 -07003905 if (!is_interface && !is_virtual) {
Dragos Sbirlea29e2e7e2013-05-22 14:52:11 -07003906 continue;
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003907 }
Ian Rogersd0583802013-06-01 10:51:46 -07003908 // Get reg type for register holding the reference to the object that will be dispatched upon.
3909 uint32_t dex_pc = inst->GetDexPc(insns);
3910 RegisterLine* line = reg_table_.GetLine(dex_pc);
3911 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE) ||
3912 (inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE);
3913 const RegType&
3914 reg_type(line->GetRegisterType(is_range ? inst->VRegC_3rc() : inst->VRegC_35c()));
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003915
Ian Rogersd0583802013-06-01 10:51:46 -07003916 if (!reg_type.HasClass()) {
3917 // We will compute devirtualization information only when we know the Class of the reg type.
3918 continue;
3919 }
3920 mirror::Class* reg_class = reg_type.GetClass();
3921 if (reg_class->IsInterface()) {
3922 // We can't devirtualize when the known type of the register is an interface.
3923 continue;
3924 }
3925 if (reg_class->IsAbstract() && !reg_class->IsArrayClass()) {
3926 // We can't devirtualize abstract classes except on arrays of abstract classes.
3927 continue;
3928 }
3929 mirror::AbstractMethod* abstract_method =
3930 dex_cache_->GetResolvedMethod(is_range ? inst->VRegB_3rc() : inst->VRegB_35c());
Brian Carlstromdf629502013-07-17 22:39:56 -07003931 if (abstract_method == NULL) {
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003932 // If the method is not found in the cache this means that it was never found
3933 // by ResolveMethodAndCheckAccess() called when verifying invoke_*.
3934 continue;
3935 }
3936 // Find the concrete method.
3937 mirror::AbstractMethod* concrete_method = NULL;
3938 if (is_interface) {
3939 concrete_method = reg_type.GetClass()->FindVirtualMethodForInterface(abstract_method);
3940 }
3941 if (is_virtual) {
3942 concrete_method = reg_type.GetClass()->FindVirtualMethodForVirtual(abstract_method);
3943 }
Ian Rogersd0583802013-06-01 10:51:46 -07003944 if (concrete_method == NULL || concrete_method->IsAbstract()) {
3945 // In cases where concrete_method is not found, or is abstract, continue to the next invoke.
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003946 continue;
3947 }
Ian Rogersd0583802013-06-01 10:51:46 -07003948 if (reg_type.IsPreciseReference() || concrete_method->IsFinal() ||
3949 concrete_method->GetDeclaringClass()->IsFinal()) {
3950 // If we knew exactly the class being dispatched upon, or if the target method cannot be
3951 // overridden record the target to be used in the compiler driver.
3952 if (pc_to_concrete_method_map.get() == NULL) {
3953 pc_to_concrete_method_map.reset(new PcToConcreteMethodMap());
3954 }
Brian Carlstrom51c24672013-07-11 16:00:56 -07003955 MethodReference concrete_ref(
Ian Rogersd0583802013-06-01 10:51:46 -07003956 concrete_method->GetDeclaringClass()->GetDexCache()->GetDexFile(),
3957 concrete_method->GetDexMethodIndex());
3958 pc_to_concrete_method_map->Put(dex_pc, concrete_ref);
3959 }
Dragos Sbirlea29e2e7e2013-05-22 14:52:11 -07003960 }
Ian Rogersd0583802013-06-01 10:51:46 -07003961 return pc_to_concrete_method_map.release();
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003962}
3963
Ian Rogers776ac1f2012-04-13 23:36:36 -07003964const std::vector<uint8_t>* MethodVerifier::GenerateGcMap() {
Ian Rogersd81871c2011-10-03 13:57:23 -07003965 size_t num_entries, ref_bitmap_bits, pc_bits;
3966 ComputeGcMapSizes(&num_entries, &ref_bitmap_bits, &pc_bits);
3967 // There's a single byte to encode the size of each bitmap
jeffhao60f83e32012-02-13 17:16:30 -08003968 if (ref_bitmap_bits >= (8 /* bits per byte */ * 8192 /* 13-bit size */ )) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003969 // TODO: either a better GC map format or per method failures
jeffhaod5347e02012-03-22 17:25:05 -07003970 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot encode GC map for method with "
Ian Rogersd81871c2011-10-03 13:57:23 -07003971 << ref_bitmap_bits << " registers";
jeffhaobdb76512011-09-07 11:43:16 -07003972 return NULL;
3973 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003974 size_t ref_bitmap_bytes = (ref_bitmap_bits + 7) / 8;
3975 // There are 2 bytes to encode the number of entries
3976 if (num_entries >= 65536) {
3977 // TODO: either a better GC map format or per method failures
jeffhaod5347e02012-03-22 17:25:05 -07003978 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot encode GC map for method with "
Ian Rogersd81871c2011-10-03 13:57:23 -07003979 << num_entries << " entries";
jeffhaobdb76512011-09-07 11:43:16 -07003980 return NULL;
3981 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003982 size_t pc_bytes;
jeffhaod1f0fde2011-09-08 17:25:33 -07003983 RegisterMapFormat format;
Ian Rogers6b0870d2011-12-15 19:38:12 -08003984 if (pc_bits <= 8) {
jeffhaod1f0fde2011-09-08 17:25:33 -07003985 format = kRegMapFormatCompact8;
Ian Rogersd81871c2011-10-03 13:57:23 -07003986 pc_bytes = 1;
Ian Rogers6b0870d2011-12-15 19:38:12 -08003987 } else if (pc_bits <= 16) {
jeffhaod1f0fde2011-09-08 17:25:33 -07003988 format = kRegMapFormatCompact16;
Ian Rogersd81871c2011-10-03 13:57:23 -07003989 pc_bytes = 2;
jeffhaoa0a764a2011-09-16 10:43:38 -07003990 } else {
Ian Rogersd81871c2011-10-03 13:57:23 -07003991 // TODO: either a better GC map format or per method failures
jeffhaod5347e02012-03-22 17:25:05 -07003992 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot encode GC map for method with "
Ian Rogersd81871c2011-10-03 13:57:23 -07003993 << (1 << pc_bits) << " instructions (number is rounded up to nearest power of 2)";
3994 return NULL;
3995 }
Elliott Hughesb25c3f62012-03-26 16:35:06 -07003996 size_t table_size = ((pc_bytes + ref_bitmap_bytes) * num_entries) + 4;
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003997 std::vector<uint8_t>* table = new std::vector<uint8_t>;
Ian Rogersd81871c2011-10-03 13:57:23 -07003998 if (table == NULL) {
jeffhaod5347e02012-03-22 17:25:05 -07003999 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Failed to encode GC map (size=" << table_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07004000 return NULL;
4001 }
Ian Rogers39ebcb82013-05-30 16:57:23 -07004002 table->reserve(table_size);
Ian Rogersd81871c2011-10-03 13:57:23 -07004003 // Write table header
Ian Rogers46c6bb22012-09-18 13:47:36 -07004004 table->push_back(format | ((ref_bitmap_bytes >> DexPcToReferenceMap::kRegMapFormatShift) &
4005 ~DexPcToReferenceMap::kRegMapFormatMask));
jeffhao60f83e32012-02-13 17:16:30 -08004006 table->push_back(ref_bitmap_bytes & 0xFF);
Brian Carlstrome7d856b2012-01-11 18:10:55 -08004007 table->push_back(num_entries & 0xFF);
4008 table->push_back((num_entries >> 8) & 0xFF);
Ian Rogersd81871c2011-10-03 13:57:23 -07004009 // Write table data
Ian Rogersd81871c2011-10-03 13:57:23 -07004010 for (size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
Sameer Abu Asal02c42232013-04-30 12:09:45 -07004011 if (insn_flags_[i].IsCompileTimeInfoPoint()) {
Brian Carlstrome7d856b2012-01-11 18:10:55 -08004012 table->push_back(i & 0xFF);
Ian Rogersd81871c2011-10-03 13:57:23 -07004013 if (pc_bytes == 2) {
Brian Carlstrome7d856b2012-01-11 18:10:55 -08004014 table->push_back((i >> 8) & 0xFF);
Ian Rogersd81871c2011-10-03 13:57:23 -07004015 }
4016 RegisterLine* line = reg_table_.GetLine(i);
Brian Carlstrome7d856b2012-01-11 18:10:55 -08004017 line->WriteReferenceBitMap(*table, ref_bitmap_bytes);
Ian Rogersd81871c2011-10-03 13:57:23 -07004018 }
4019 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -08004020 DCHECK_EQ(table->size(), table_size);
Ian Rogersd81871c2011-10-03 13:57:23 -07004021 return table;
4022}
jeffhaoa0a764a2011-09-16 10:43:38 -07004023
Ian Rogers776ac1f2012-04-13 23:36:36 -07004024void MethodVerifier::VerifyGcMap(const std::vector<uint8_t>& data) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004025 // Check that for every GC point there is a map entry, there aren't entries for non-GC points,
4026 // that the table data is well formed and all references are marked (or not) in the bitmap
Ian Rogers46c6bb22012-09-18 13:47:36 -07004027 DexPcToReferenceMap map(&data[0], data.size());
Ian Rogersd81871c2011-10-03 13:57:23 -07004028 size_t map_index = 0;
Elliott Hughesb25c3f62012-03-26 16:35:06 -07004029 for (size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004030 const uint8_t* reg_bitmap = map.FindBitMap(i, false);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07004031 if (insn_flags_[i].IsCompileTimeInfoPoint()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004032 CHECK_LT(map_index, map.NumEntries());
Ian Rogers46c6bb22012-09-18 13:47:36 -07004033 CHECK_EQ(map.GetDexPc(map_index), i);
Ian Rogersd81871c2011-10-03 13:57:23 -07004034 CHECK_EQ(map.GetBitMap(map_index), reg_bitmap);
4035 map_index++;
4036 RegisterLine* line = reg_table_.GetLine(i);
Elliott Hughesb25c3f62012-03-26 16:35:06 -07004037 for (size_t j = 0; j < code_item_->registers_size_; j++) {
Ian Rogers84fa0742011-10-25 18:13:30 -07004038 if (line->GetRegisterType(j).IsNonZeroReferenceTypes()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004039 CHECK_LT(j / 8, map.RegWidth());
4040 CHECK_EQ((reg_bitmap[j / 8] >> (j % 8)) & 1, 1);
4041 } else if ((j / 8) < map.RegWidth()) {
4042 CHECK_EQ((reg_bitmap[j / 8] >> (j % 8)) & 1, 0);
4043 } else {
4044 // If a register doesn't contain a reference then the bitmap may be shorter than the line
4045 }
4046 }
4047 } else {
4048 CHECK(reg_bitmap == NULL);
4049 }
4050 }
4051}
jeffhaoa0a764a2011-09-16 10:43:38 -07004052
Brian Carlstrom51c24672013-07-11 16:00:56 -07004053void MethodVerifier::SetDexGcMap(MethodReference ref, const std::vector<uint8_t>& gc_map) {
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004054 DCHECK(Runtime::Current()->IsCompiler());
Ian Rogers00f7d0e2012-07-19 15:28:27 -07004055 {
Ian Rogers637c65b2013-05-31 11:46:00 -07004056 WriterMutexLock mu(Thread::Current(), *dex_gc_maps_lock_);
Ian Rogers0c7abda2012-09-19 13:33:42 -07004057 DexGcMapTable::iterator it = dex_gc_maps_->find(ref);
4058 if (it != dex_gc_maps_->end()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07004059 delete it->second;
Ian Rogers0c7abda2012-09-19 13:33:42 -07004060 dex_gc_maps_->erase(it);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07004061 }
Ian Rogers0c7abda2012-09-19 13:33:42 -07004062 dex_gc_maps_->Put(ref, &gc_map);
Brian Carlstrom73a15f42012-01-17 18:14:39 -08004063 }
Ian Rogers39ebcb82013-05-30 16:57:23 -07004064 DCHECK(GetDexGcMap(ref) != NULL);
Brian Carlstrome7d856b2012-01-11 18:10:55 -08004065}
4066
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07004067
Brian Carlstrom51c24672013-07-11 16:00:56 -07004068void MethodVerifier::SetSafeCastMap(MethodReference ref, const MethodSafeCastSet* cast_set) {
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004069 DCHECK(Runtime::Current()->IsCompiler());
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07004070 MutexLock mu(Thread::Current(), *safecast_map_lock_);
4071 SafeCastMap::iterator it = safecast_map_->find(ref);
4072 if (it != safecast_map_->end()) {
4073 delete it->second;
4074 safecast_map_->erase(it);
4075 }
4076
4077 safecast_map_->Put(ref, cast_set);
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004078 DCHECK(safecast_map_->find(ref) != safecast_map_->end());
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07004079}
4080
Brian Carlstrom51c24672013-07-11 16:00:56 -07004081bool MethodVerifier::IsSafeCast(MethodReference ref, uint32_t pc) {
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004082 DCHECK(Runtime::Current()->IsCompiler());
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07004083 MutexLock mu(Thread::Current(), *safecast_map_lock_);
4084 SafeCastMap::const_iterator it = safecast_map_->find(ref);
4085 if (it == safecast_map_->end()) {
4086 return false;
4087 }
4088
4089 // Look up the cast address in the set of safe casts
4090 MethodVerifier::MethodSafeCastSet::const_iterator cast_it = it->second->find(pc);
4091 return cast_it != it->second->end();
4092}
4093
Brian Carlstrom51c24672013-07-11 16:00:56 -07004094const std::vector<uint8_t>* MethodVerifier::GetDexGcMap(MethodReference ref) {
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004095 DCHECK(Runtime::Current()->IsCompiler());
Ian Rogers637c65b2013-05-31 11:46:00 -07004096 ReaderMutexLock mu(Thread::Current(), *dex_gc_maps_lock_);
4097 DexGcMapTable::const_iterator it = dex_gc_maps_->find(ref);
4098 if (it == dex_gc_maps_->end()) {
4099 LOG(WARNING) << "Didn't find GC map for: " << PrettyMethod(ref.dex_method_index, *ref.dex_file);
4100 return NULL;
4101 }
4102 CHECK(it->second != NULL);
4103 return it->second;
4104}
Sameer Abu Asal02c42232013-04-30 12:09:45 -07004105
Brian Carlstrom51c24672013-07-11 16:00:56 -07004106void MethodVerifier::SetDevirtMap(MethodReference ref,
Ian Rogersd0583802013-06-01 10:51:46 -07004107 const PcToConcreteMethodMap* devirt_map) {
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004108 DCHECK(Runtime::Current()->IsCompiler());
Ian Rogers637c65b2013-05-31 11:46:00 -07004109 WriterMutexLock mu(Thread::Current(), *devirt_maps_lock_);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07004110 DevirtualizationMapTable::iterator it = devirt_maps_->find(ref);
4111 if (it != devirt_maps_->end()) {
4112 delete it->second;
4113 devirt_maps_->erase(it);
4114 }
4115
4116 devirt_maps_->Put(ref, devirt_map);
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004117 DCHECK(devirt_maps_->find(ref) != devirt_maps_->end());
Sameer Abu Asal02c42232013-04-30 12:09:45 -07004118}
4119
Brian Carlstrom51c24672013-07-11 16:00:56 -07004120const MethodReference* MethodVerifier::GetDevirtMap(const MethodReference& ref,
Ian Rogerse3cd2f02013-05-24 15:32:56 -07004121 uint32_t dex_pc) {
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004122 DCHECK(Runtime::Current()->IsCompiler());
Ian Rogers637c65b2013-05-31 11:46:00 -07004123 ReaderMutexLock mu(Thread::Current(), *devirt_maps_lock_);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07004124 DevirtualizationMapTable::const_iterator it = devirt_maps_->find(ref);
4125 if (it == devirt_maps_->end()) {
4126 return NULL;
4127 }
4128
4129 // Look up the PC in the map, get the concrete method to execute and return its reference.
Brian Carlstrom93c33962013-07-26 10:37:43 -07004130 MethodVerifier::PcToConcreteMethodMap::const_iterator pc_to_concrete_method
4131 = it->second->find(dex_pc);
Brian Carlstromdf629502013-07-17 22:39:56 -07004132 if (pc_to_concrete_method != it->second->end()) {
Sameer Abu Asal02c42232013-04-30 12:09:45 -07004133 return &(pc_to_concrete_method->second);
4134 } else {
4135 return NULL;
4136 }
4137}
4138
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004139std::vector<int32_t> MethodVerifier::DescribeVRegs(uint32_t dex_pc) {
4140 RegisterLine* line = reg_table_.GetLine(dex_pc);
4141 std::vector<int32_t> result;
4142 for (size_t i = 0; i < line->NumRegs(); ++i) {
4143 const RegType& type = line->GetRegisterType(i);
4144 if (type.IsConstant()) {
4145 result.push_back(type.IsPreciseConstant() ? kConstant : kImpreciseConstant);
4146 result.push_back(type.ConstantValue());
4147 } else if (type.IsConstantLo()) {
4148 result.push_back(type.IsPreciseConstantLo() ? kConstant : kImpreciseConstant);
4149 result.push_back(type.ConstantValueLo());
4150 } else if (type.IsConstantHi()) {
4151 result.push_back(type.IsPreciseConstantHi() ? kConstant : kImpreciseConstant);
4152 result.push_back(type.ConstantValueHi());
4153 } else if (type.IsIntegralTypes()) {
4154 result.push_back(kIntVReg);
4155 result.push_back(0);
4156 } else if (type.IsFloat()) {
4157 result.push_back(kFloatVReg);
4158 result.push_back(0);
4159 } else if (type.IsLong()) {
4160 result.push_back(kLongLoVReg);
4161 result.push_back(0);
4162 result.push_back(kLongHiVReg);
4163 result.push_back(0);
4164 ++i;
4165 } else if (type.IsDouble()) {
4166 result.push_back(kDoubleLoVReg);
4167 result.push_back(0);
4168 result.push_back(kDoubleHiVReg);
4169 result.push_back(0);
4170 ++i;
4171 } else if (type.IsUndefined() || type.IsConflict() || type.IsHighHalf()) {
4172 result.push_back(kUndefined);
4173 result.push_back(0);
4174 } else {
Ian Rogers7b3ddd22013-02-21 15:19:52 -08004175 CHECK(type.IsNonZeroReferenceTypes());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004176 result.push_back(kReferenceVReg);
4177 result.push_back(0);
4178 }
4179 }
4180 return result;
4181}
4182
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004183bool MethodVerifier::IsCandidateForCompilation(const DexFile::CodeItem* code_item,
4184 const uint32_t access_flags) {
4185 // Don't compile class initializers, ever.
4186 if (((access_flags & kAccConstructor) != 0) && ((access_flags & kAccStatic) != 0)) {
4187 return false;
4188 }
4189
4190 const Runtime* runtime = Runtime::Current();
4191 if (runtime->IsSmallMode() && runtime->UseCompileTimeClassPath()) {
4192 // In Small mode, we only compile small methods.
4193 const uint32_t code_size = code_item->insns_size_in_code_units_;
4194 return (code_size < runtime->GetSmallModeMethodDexSizeLimit());
4195 } else {
4196 // In normal mode, we compile everything.
4197 return true;
4198 }
4199}
4200
Ian Rogers637c65b2013-05-31 11:46:00 -07004201ReaderWriterMutex* MethodVerifier::dex_gc_maps_lock_ = NULL;
Ian Rogers0c7abda2012-09-19 13:33:42 -07004202MethodVerifier::DexGcMapTable* MethodVerifier::dex_gc_maps_ = NULL;
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004203
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07004204Mutex* MethodVerifier::safecast_map_lock_ = NULL;
4205MethodVerifier::SafeCastMap* MethodVerifier::safecast_map_ = NULL;
4206
Ian Rogers637c65b2013-05-31 11:46:00 -07004207ReaderWriterMutex* MethodVerifier::devirt_maps_lock_ = NULL;
Sameer Abu Asal02c42232013-04-30 12:09:45 -07004208MethodVerifier::DevirtualizationMapTable* MethodVerifier::devirt_maps_ = NULL;
4209
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004210Mutex* MethodVerifier::rejected_classes_lock_ = NULL;
4211MethodVerifier::RejectedClassesTable* MethodVerifier::rejected_classes_ = NULL;
4212
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004213void MethodVerifier::Init() {
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004214 if (Runtime::Current()->IsCompiler()) {
4215 dex_gc_maps_lock_ = new ReaderWriterMutex("verifier GC maps lock");
4216 Thread* self = Thread::Current();
4217 {
4218 WriterMutexLock mu(self, *dex_gc_maps_lock_);
4219 dex_gc_maps_ = new MethodVerifier::DexGcMapTable;
4220 }
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004221
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004222 safecast_map_lock_ = new Mutex("verifier Cast Elision lock");
4223 {
4224 MutexLock mu(self, *safecast_map_lock_);
4225 safecast_map_ = new MethodVerifier::SafeCastMap();
4226 }
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07004227
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004228 devirt_maps_lock_ = new ReaderWriterMutex("verifier Devirtualization lock");
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07004229
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004230 {
4231 WriterMutexLock mu(self, *devirt_maps_lock_);
4232 devirt_maps_ = new MethodVerifier::DevirtualizationMapTable();
4233 }
Sameer Abu Asal02c42232013-04-30 12:09:45 -07004234
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004235 rejected_classes_lock_ = new Mutex("verifier rejected classes lock");
4236 {
4237 MutexLock mu(self, *rejected_classes_lock_);
4238 rejected_classes_ = new MethodVerifier::RejectedClassesTable;
4239 }
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004240 }
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08004241 art::verifier::RegTypeCache::Init();
Brian Carlstrome7d856b2012-01-11 18:10:55 -08004242}
4243
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004244void MethodVerifier::Shutdown() {
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004245 if (Runtime::Current()->IsCompiler()) {
4246 Thread* self = Thread::Current();
4247 {
4248 WriterMutexLock mu(self, *dex_gc_maps_lock_);
4249 STLDeleteValues(dex_gc_maps_);
4250 delete dex_gc_maps_;
4251 dex_gc_maps_ = NULL;
4252 }
4253 delete dex_gc_maps_lock_;
4254 dex_gc_maps_lock_ = NULL;
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004255
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004256 {
4257 MutexLock mu(self, *safecast_map_lock_);
4258 STLDeleteValues(safecast_map_);
4259 delete safecast_map_;
4260 safecast_map_ = NULL;
4261 }
4262 delete safecast_map_lock_;
4263 safecast_map_lock_ = NULL;
Sameer Abu Asal02c42232013-04-30 12:09:45 -07004264
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004265 {
4266 WriterMutexLock mu(self, *devirt_maps_lock_);
4267 STLDeleteValues(devirt_maps_);
4268 delete devirt_maps_;
4269 devirt_maps_ = NULL;
4270 }
4271 delete devirt_maps_lock_;
4272 devirt_maps_lock_ = NULL;
4273
4274 {
4275 MutexLock mu(self, *rejected_classes_lock_);
4276 delete rejected_classes_;
4277 rejected_classes_ = NULL;
4278 }
4279 delete rejected_classes_lock_;
4280 rejected_classes_lock_ = NULL;
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004281 }
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08004282 verifier::RegTypeCache::ShutDown();
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08004283}
jeffhaod1224c72012-02-29 13:43:08 -08004284
Brian Carlstrom51c24672013-07-11 16:00:56 -07004285void MethodVerifier::AddRejectedClass(ClassReference ref) {
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004286 DCHECK(Runtime::Current()->IsCompiler());
Ian Rogers00f7d0e2012-07-19 15:28:27 -07004287 {
Ian Rogers50b35e22012-10-04 10:09:15 -07004288 MutexLock mu(Thread::Current(), *rejected_classes_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07004289 rejected_classes_->insert(ref);
4290 }
jeffhaod1224c72012-02-29 13:43:08 -08004291 CHECK(IsClassRejected(ref));
4292}
4293
Brian Carlstrom51c24672013-07-11 16:00:56 -07004294bool MethodVerifier::IsClassRejected(ClassReference ref) {
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004295 DCHECK(Runtime::Current()->IsCompiler());
Ian Rogers50b35e22012-10-04 10:09:15 -07004296 MutexLock mu(Thread::Current(), *rejected_classes_lock_);
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004297 return (rejected_classes_->find(ref) != rejected_classes_->end());
jeffhaod1224c72012-02-29 13:43:08 -08004298}
4299
Ian Rogersd81871c2011-10-03 13:57:23 -07004300} // namespace verifier
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07004301} // namespace art