blob: 32ebcf8e319c8a33ae9afcaf86c0adc30a65cce3 [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070016
Ian Rogers776ac1f2012-04-13 23:36:36 -070017#include "method_verifier.h"
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070018
Elliott Hughes1f359b02011-07-17 14:27:17 -070019#include <iostream>
20
Elliott Hughes07ed66b2012-12-12 18:34:25 -080021#include "base/logging.h"
Ian Rogers637c65b2013-05-31 11:46:00 -070022#include "base/mutex-inl.h"
Elliott Hughese222ee02012-12-13 14:41:43 -080023#include "base/stringpiece.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070024#include "class_linker.h"
Ian Rogers1212a022013-03-04 10:48:41 -080025#include "compiler/driver/compiler_driver.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070026#include "dex_file-inl.h"
Ian Rogersd0583802013-06-01 10:51:46 -070027#include "dex_instruction-inl.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070028#include "dex_instruction_visitor.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070029#include "gc/accounting/card_table-inl.h"
Ian Rogers2bcb4a42012-11-08 10:39:18 -080030#include "indenter.h"
Ian Rogers84fa0742011-10-25 18:13:30 -070031#include "intern_table.h"
Ian Rogers0571d352011-11-03 19:51:38 -070032#include "leb128.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080033#include "mirror/abstract_method-inl.h"
34#include "mirror/class.h"
35#include "mirror/class-inl.h"
Ian Rogers39ebcb82013-05-30 16:57:23 -070036#include "mirror/dex_cache-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080037#include "mirror/field-inl.h"
38#include "mirror/object-inl.h"
39#include "mirror/object_array-inl.h"
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080040#include "object_utils.h"
Ian Rogers39ebcb82013-05-30 16:57:23 -070041#include "register_line-inl.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070042#include "runtime.h"
Elliott Hughese222ee02012-12-13 14:41:43 -080043#include "verifier/dex_gc_map.h"
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070044
45namespace art {
Ian Rogersd81871c2011-10-03 13:57:23 -070046namespace verifier {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070047
Ian Rogers2c8a8572011-10-24 17:11:36 -070048static const bool gDebugVerify = false;
49
Ian Rogers7b3ddd22013-02-21 15:19:52 -080050void PcToRegisterLineTable::Init(RegisterTrackingMode mode, InstructionFlags* flags,
Ian Rogersd81871c2011-10-03 13:57:23 -070051 uint32_t insns_size, uint16_t registers_size,
Ian Rogers776ac1f2012-04-13 23:36:36 -070052 MethodVerifier* verifier) {
Ian Rogersd81871c2011-10-03 13:57:23 -070053 DCHECK_GT(insns_size, 0U);
54
55 for (uint32_t i = 0; i < insns_size; i++) {
56 bool interesting = false;
57 switch (mode) {
58 case kTrackRegsAll:
59 interesting = flags[i].IsOpcode();
60 break;
Sameer Abu Asal02c42232013-04-30 12:09:45 -070061 case kTrackCompilerInterestPoints:
62 interesting = flags[i].IsCompileTimeInfoPoint() || flags[i].IsBranchTarget() ;
Ian Rogersd81871c2011-10-03 13:57:23 -070063 break;
64 case kTrackRegsBranches:
65 interesting = flags[i].IsBranchTarget();
66 break;
67 default:
68 break;
69 }
70 if (interesting) {
Elliott Hughesa0e18062012-04-13 15:59:59 -070071 pc_to_register_line_.Put(i, new RegisterLine(registers_size, verifier));
Ian Rogersd81871c2011-10-03 13:57:23 -070072 }
73 }
74}
75
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080076MethodVerifier::FailureKind MethodVerifier::VerifyClass(const mirror::Class* klass,
Jeff Haoee988952013-04-16 14:23:47 -070077 std::string& error,
78 bool allow_soft_failures) {
jeffhaobdb76512011-09-07 11:43:16 -070079 if (klass->IsVerified()) {
jeffhaof1e6b7c2012-06-05 18:33:30 -070080 return kNoFailure;
jeffhaobdb76512011-09-07 11:43:16 -070081 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080082 mirror::Class* super = klass->GetSuperClass();
Elliott Hughes91250e02011-12-13 22:30:35 -080083 if (super == NULL && StringPiece(ClassHelper(klass).GetDescriptor()) != "Ljava/lang/Object;") {
Ian Rogers1c5eb702012-02-01 09:18:34 -080084 error = "Verifier rejected class ";
85 error += PrettyDescriptor(klass);
86 error += " that has no super class";
jeffhaof1e6b7c2012-06-05 18:33:30 -070087 return kHardFailure;
Ian Rogersd81871c2011-10-03 13:57:23 -070088 }
Ian Rogers1c5eb702012-02-01 09:18:34 -080089 if (super != NULL && super->IsFinal()) {
90 error = "Verifier rejected class ";
91 error += PrettyDescriptor(klass);
92 error += " that attempts to sub-class final class ";
93 error += PrettyDescriptor(super);
jeffhaof1e6b7c2012-06-05 18:33:30 -070094 return kHardFailure;
Ian Rogersd81871c2011-10-03 13:57:23 -070095 }
Ian Rogersad0b3a32012-04-16 14:50:24 -070096 ClassHelper kh(klass);
97 const DexFile& dex_file = kh.GetDexFile();
98 uint32_t class_def_idx;
99 if (!dex_file.FindClassDefIndex(kh.GetDescriptor(), class_def_idx)) {
100 error = "Verifier rejected class ";
101 error += PrettyDescriptor(klass);
102 error += " that isn't present in dex file ";
103 error += dex_file.GetLocation();
jeffhaof1e6b7c2012-06-05 18:33:30 -0700104 return kHardFailure;
jeffhaobdb76512011-09-07 11:43:16 -0700105 }
Jeff Haoee988952013-04-16 14:23:47 -0700106 return VerifyClass(&dex_file, kh.GetDexCache(), klass->GetClassLoader(), class_def_idx, error, allow_soft_failures);
Shih-wei Liao371814f2011-10-27 16:52:10 -0700107}
108
Ian Rogers365c1022012-06-22 15:05:28 -0700109MethodVerifier::FailureKind MethodVerifier::VerifyClass(const DexFile* dex_file,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800110 mirror::DexCache* dex_cache,
111 mirror::ClassLoader* class_loader,
112 uint32_t class_def_idx,
Jeff Haoee988952013-04-16 14:23:47 -0700113 std::string& error,
114 bool allow_soft_failures) {
jeffhaof56197c2012-03-05 18:01:54 -0800115 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_idx);
116 const byte* class_data = dex_file->GetClassData(class_def);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700117 if (class_data == NULL) {
118 // empty class, probably a marker interface
jeffhaof1e6b7c2012-06-05 18:33:30 -0700119 return kNoFailure;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700120 }
jeffhaof56197c2012-03-05 18:01:54 -0800121 ClassDataItemIterator it(*dex_file, class_data);
122 while (it.HasNextStaticField() || it.HasNextInstanceField()) {
123 it.Next();
124 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700125 size_t error_count = 0;
jeffhaof1e6b7c2012-06-05 18:33:30 -0700126 bool hard_fail = false;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700127 ClassLinker* linker = Runtime::Current()->GetClassLinker();
jeffhao9b0b1882012-10-01 16:51:22 -0700128 int64_t previous_direct_method_idx = -1;
jeffhaof56197c2012-03-05 18:01:54 -0800129 while (it.HasNextDirectMethod()) {
130 uint32_t method_idx = it.GetMemberIndex();
jeffhao9b0b1882012-10-01 16:51:22 -0700131 if (method_idx == previous_direct_method_idx) {
132 // smali can create dex files with two encoded_methods sharing the same method_idx
133 // http://code.google.com/p/smali/issues/detail?id=119
134 it.Next();
135 continue;
136 }
137 previous_direct_method_idx = method_idx;
Ian Rogers08f753d2012-08-24 14:35:25 -0700138 InvokeType type = it.GetMethodInvokeType(class_def);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800139 mirror::AbstractMethod* method =
140 linker->ResolveMethod(*dex_file, method_idx, dex_cache, class_loader, NULL, type);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700141 if (method == NULL) {
142 DCHECK(Thread::Current()->IsExceptionPending());
143 // We couldn't resolve the method, but continue regardless.
144 Thread::Current()->ClearException();
145 }
jeffhaof1e6b7c2012-06-05 18:33:30 -0700146 MethodVerifier::FailureKind result = VerifyMethod(method_idx, dex_file, dex_cache, class_loader,
Jeff Haoee988952013-04-16 14:23:47 -0700147 class_def_idx, it.GetMethodCodeItem(), method, it.GetMemberAccessFlags(), allow_soft_failures);
jeffhaof1e6b7c2012-06-05 18:33:30 -0700148 if (result != kNoFailure) {
149 if (result == kHardFailure) {
150 hard_fail = true;
151 if (error_count > 0) {
152 error += "\n";
153 }
154 error = "Verifier rejected class ";
155 error += PrettyDescriptor(dex_file->GetClassDescriptor(class_def));
156 error += " due to bad method ";
157 error += PrettyMethod(method_idx, *dex_file);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700158 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700159 ++error_count;
jeffhaof56197c2012-03-05 18:01:54 -0800160 }
161 it.Next();
162 }
jeffhao9b0b1882012-10-01 16:51:22 -0700163 int64_t previous_virtual_method_idx = -1;
jeffhaof56197c2012-03-05 18:01:54 -0800164 while (it.HasNextVirtualMethod()) {
165 uint32_t method_idx = it.GetMemberIndex();
jeffhao9b0b1882012-10-01 16:51:22 -0700166 if (method_idx == previous_virtual_method_idx) {
167 // smali can create dex files with two encoded_methods sharing the same method_idx
168 // http://code.google.com/p/smali/issues/detail?id=119
169 it.Next();
170 continue;
171 }
172 previous_virtual_method_idx = method_idx;
Ian Rogers08f753d2012-08-24 14:35:25 -0700173 InvokeType type = it.GetMethodInvokeType(class_def);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800174 mirror::AbstractMethod* method =
175 linker->ResolveMethod(*dex_file, method_idx, dex_cache, class_loader, NULL, type);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700176 if (method == NULL) {
177 DCHECK(Thread::Current()->IsExceptionPending());
178 // We couldn't resolve the method, but continue regardless.
179 Thread::Current()->ClearException();
180 }
jeffhaof1e6b7c2012-06-05 18:33:30 -0700181 MethodVerifier::FailureKind result = VerifyMethod(method_idx, dex_file, dex_cache, class_loader,
Jeff Haoee988952013-04-16 14:23:47 -0700182 class_def_idx, it.GetMethodCodeItem(), method, it.GetMemberAccessFlags(), allow_soft_failures);
jeffhaof1e6b7c2012-06-05 18:33:30 -0700183 if (result != kNoFailure) {
184 if (result == kHardFailure) {
185 hard_fail = true;
186 if (error_count > 0) {
187 error += "\n";
188 }
189 error = "Verifier rejected class ";
190 error += PrettyDescriptor(dex_file->GetClassDescriptor(class_def));
191 error += " due to bad method ";
192 error += PrettyMethod(method_idx, *dex_file);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700193 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700194 ++error_count;
jeffhaof56197c2012-03-05 18:01:54 -0800195 }
196 it.Next();
197 }
jeffhaof1e6b7c2012-06-05 18:33:30 -0700198 if (error_count == 0) {
199 return kNoFailure;
200 } else {
201 return hard_fail ? kHardFailure : kSoftFailure;
202 }
jeffhaof56197c2012-03-05 18:01:54 -0800203}
204
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800205MethodVerifier::FailureKind MethodVerifier::VerifyMethod(uint32_t method_idx,
206 const DexFile* dex_file,
207 mirror::DexCache* dex_cache,
208 mirror::ClassLoader* class_loader,
209 uint32_t class_def_idx,
210 const DexFile::CodeItem* code_item,
211 mirror::AbstractMethod* method,
Jeff Haoee988952013-04-16 14:23:47 -0700212 uint32_t method_access_flags,
213 bool allow_soft_failures) {
Ian Rogersc8982582012-09-07 16:53:25 -0700214 MethodVerifier::FailureKind result = kNoFailure;
215 uint64_t start_ns = NanoTime();
216
Ian Rogersad0b3a32012-04-16 14:50:24 -0700217 MethodVerifier verifier(dex_file, dex_cache, class_loader, class_def_idx, code_item, method_idx,
Jeff Haoee988952013-04-16 14:23:47 -0700218 method, method_access_flags, true, allow_soft_failures);
jeffhaof1e6b7c2012-06-05 18:33:30 -0700219 if (verifier.Verify()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700220 // Verification completed, however failures may be pending that didn't cause the verification
221 // to hard fail.
Ian Rogerse551e952012-06-03 22:59:14 -0700222 CHECK(!verifier.have_pending_hard_failure_);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700223 if (verifier.failures_.size() != 0) {
224 verifier.DumpFailures(LOG(INFO) << "Soft verification failures in "
Elliott Hughesc073b072012-05-24 19:29:17 -0700225 << PrettyMethod(method_idx, *dex_file) << "\n");
Ian Rogersc8982582012-09-07 16:53:25 -0700226 result = kSoftFailure;
jeffhaof56197c2012-03-05 18:01:54 -0800227 }
228 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700229 // Bad method data.
230 CHECK_NE(verifier.failures_.size(), 0U);
231 CHECK(verifier.have_pending_hard_failure_);
232 verifier.DumpFailures(LOG(INFO) << "Verification error in "
Elliott Hughesc073b072012-05-24 19:29:17 -0700233 << PrettyMethod(method_idx, *dex_file) << "\n");
jeffhaof56197c2012-03-05 18:01:54 -0800234 if (gDebugVerify) {
Elliott Hughesc073b072012-05-24 19:29:17 -0700235 std::cout << "\n" << verifier.info_messages_.str();
jeffhaof56197c2012-03-05 18:01:54 -0800236 verifier.Dump(std::cout);
237 }
Ian Rogersc8982582012-09-07 16:53:25 -0700238 result = kHardFailure;
jeffhaof56197c2012-03-05 18:01:54 -0800239 }
Ian Rogersc8982582012-09-07 16:53:25 -0700240 uint64_t duration_ns = NanoTime() - start_ns;
241 if (duration_ns > MsToNs(100)) {
242 LOG(WARNING) << "Verification of " << PrettyMethod(method_idx, *dex_file)
243 << " took " << PrettyDuration(duration_ns);
244 }
245 return result;
jeffhaof56197c2012-03-05 18:01:54 -0800246}
247
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800248void MethodVerifier::VerifyMethodAndDump(std::ostream& os, uint32_t dex_method_idx,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800249 const DexFile* dex_file, mirror::DexCache* dex_cache,
250 mirror::ClassLoader* class_loader, uint32_t class_def_idx,
251 const DexFile::CodeItem* code_item,
252 mirror::AbstractMethod* method,
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800253 uint32_t method_access_flags) {
254 MethodVerifier verifier(dex_file, dex_cache, class_loader, class_def_idx, code_item,
Jeff Haoee988952013-04-16 14:23:47 -0700255 dex_method_idx, method, method_access_flags, true, true);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700256 verifier.Verify();
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800257 verifier.DumpFailures(os);
258 os << verifier.info_messages_.str();
259 verifier.Dump(os);
260}
261
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800262MethodVerifier::MethodVerifier(const DexFile* dex_file, mirror::DexCache* dex_cache,
263 mirror::ClassLoader* class_loader, uint32_t class_def_idx,
264 const DexFile::CodeItem* code_item,
265 uint32_t dex_method_idx, mirror::AbstractMethod* method,
Jeff Haoee988952013-04-16 14:23:47 -0700266 uint32_t method_access_flags, bool can_load_classes,
267 bool allow_soft_failures)
Elliott Hughes80537bb2013-01-04 16:37:26 -0800268 : reg_types_(can_load_classes),
269 work_insn_idx_(-1),
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800270 dex_method_idx_(dex_method_idx),
Ian Rogers637c65b2013-05-31 11:46:00 -0700271 mirror_method_(method),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700272 method_access_flags_(method_access_flags),
jeffhaof56197c2012-03-05 18:01:54 -0800273 dex_file_(dex_file),
274 dex_cache_(dex_cache),
275 class_loader_(class_loader),
276 class_def_idx_(class_def_idx),
277 code_item_(code_item),
Ian Rogers637c65b2013-05-31 11:46:00 -0700278 declaring_class_(NULL),
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700279 interesting_dex_pc_(-1),
280 monitor_enter_dex_pcs_(NULL),
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200281 accessed_field(NULL),
282 invoked_method(NULL),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700283 have_pending_hard_failure_(false),
jeffhaofaf459e2012-08-31 15:32:47 -0700284 have_pending_runtime_throw_failure_(false),
jeffhaof56197c2012-03-05 18:01:54 -0800285 new_instance_count_(0),
Elliott Hughes80537bb2013-01-04 16:37:26 -0800286 monitor_enter_count_(0),
Jeff Haoee988952013-04-16 14:23:47 -0700287 can_load_classes_(can_load_classes),
288 allow_soft_failures_(allow_soft_failures) {
jeffhaof56197c2012-03-05 18:01:54 -0800289}
290
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800291void MethodVerifier::FindLocksAtDexPc(mirror::AbstractMethod* m, uint32_t dex_pc,
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800292 std::vector<uint32_t>& monitor_enter_dex_pcs) {
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700293 MethodHelper mh(m);
294 MethodVerifier verifier(&mh.GetDexFile(), mh.GetDexCache(), mh.GetClassLoader(),
295 mh.GetClassDefIndex(), mh.GetCodeItem(), m->GetDexMethodIndex(),
Jeff Haoee988952013-04-16 14:23:47 -0700296 m, m->GetAccessFlags(), false, true);
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700297 verifier.interesting_dex_pc_ = dex_pc;
298 verifier.monitor_enter_dex_pcs_ = &monitor_enter_dex_pcs;
299 verifier.FindLocksAtDexPc();
300}
301
302void MethodVerifier::FindLocksAtDexPc() {
303 CHECK(monitor_enter_dex_pcs_ != NULL);
304 CHECK(code_item_ != NULL); // This only makes sense for methods with code.
305
306 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
307 // verification. In practice, the phase we want relies on data structures set up by all the
308 // earlier passes, so we just run the full method verification and bail out early when we've
309 // got what we wanted.
310 Verify();
311}
312
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200313mirror::Field* MethodVerifier::FindAccessedFieldAtDexPc(mirror::AbstractMethod* m,
314 uint32_t dex_pc) {
315 MethodHelper mh(m);
316 MethodVerifier verifier(&mh.GetDexFile(), mh.GetDexCache(), mh.GetClassLoader(),
317 mh.GetClassDefIndex(), mh.GetCodeItem(), m->GetDexMethodIndex(),
318 m, m->GetAccessFlags(), false, true);
319 mirror::Field* field = NULL;
320 verifier.interesting_dex_pc_ = dex_pc;
321 verifier.accessed_field = &field;
322 verifier.FindAccessedFieldAtDexPc();
323 return field;
324}
325
326void MethodVerifier::FindAccessedFieldAtDexPc() {
327 CHECK(accessed_field != NULL);
328 CHECK(code_item_ != NULL); // This only makes sense for methods with code.
329
330 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
331 // verification. In practice, the phase we want relies on data structures set up by all the
332 // earlier passes, so we just run the full method verification and bail out early when we've
333 // got what we wanted.
334 Verify();
335}
336
337mirror::AbstractMethod* MethodVerifier::FindInvokedMethodAtDexPc(mirror::AbstractMethod* m,
338 uint32_t dex_pc) {
339 MethodHelper mh(m);
340 MethodVerifier verifier(&mh.GetDexFile(), mh.GetDexCache(), mh.GetClassLoader(),
341 mh.GetClassDefIndex(), mh.GetCodeItem(), m->GetDexMethodIndex(),
342 m, m->GetAccessFlags(), false, true);
343 mirror::AbstractMethod* method = NULL;
344 verifier.interesting_dex_pc_ = dex_pc;
345 verifier.invoked_method = &method;
346 verifier.FindInvokedMethodAtDexPc();
347 return method;
348}
349
350void MethodVerifier::FindInvokedMethodAtDexPc() {
351 CHECK(invoked_method != NULL);
352 CHECK(code_item_ != NULL); // This only makes sense for methods with code.
353
354 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
355 // verification. In practice, the phase we want relies on data structures set up by all the
356 // earlier passes, so we just run the full method verification and bail out early when we've
357 // got what we wanted.
358 Verify();
359}
360
Ian Rogersad0b3a32012-04-16 14:50:24 -0700361bool MethodVerifier::Verify() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700362 // If there aren't any instructions, make sure that's expected, then exit successfully.
363 if (code_item_ == NULL) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700364 if ((method_access_flags_ & (kAccNative | kAccAbstract)) == 0) {
jeffhaod5347e02012-03-22 17:25:05 -0700365 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "zero-length code in concrete non-native method";
jeffhaobdb76512011-09-07 11:43:16 -0700366 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -0700367 } else {
368 return true;
jeffhaobdb76512011-09-07 11:43:16 -0700369 }
jeffhaobdb76512011-09-07 11:43:16 -0700370 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700371 // Sanity-check the register counts. ins + locals = registers, so make sure that ins <= registers.
372 if (code_item_->ins_size_ > code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700373 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad register counts (ins=" << code_item_->ins_size_
374 << " regs=" << code_item_->registers_size_;
Ian Rogersd81871c2011-10-03 13:57:23 -0700375 return false;
jeffhaobdb76512011-09-07 11:43:16 -0700376 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700377 // Allocate and initialize an array to hold instruction data.
Ian Rogers7b3ddd22013-02-21 15:19:52 -0800378 insn_flags_.reset(new InstructionFlags[code_item_->insns_size_in_code_units_]());
Ian Rogersd81871c2011-10-03 13:57:23 -0700379 // Run through the instructions and see if the width checks out.
380 bool result = ComputeWidthsAndCountOps();
381 // Flag instructions guarded by a "try" block and check exception handlers.
382 result = result && ScanTryCatchBlocks();
383 // Perform static instruction verification.
384 result = result && VerifyInstructions();
Ian Rogersad0b3a32012-04-16 14:50:24 -0700385 // Perform code-flow analysis and return.
386 return result && VerifyCodeFlow();
jeffhaoba5ebb92011-08-25 17:24:37 -0700387}
388
Ian Rogers776ac1f2012-04-13 23:36:36 -0700389std::ostream& MethodVerifier::Fail(VerifyError error) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700390 switch (error) {
391 case VERIFY_ERROR_NO_CLASS:
392 case VERIFY_ERROR_NO_FIELD:
393 case VERIFY_ERROR_NO_METHOD:
394 case VERIFY_ERROR_ACCESS_CLASS:
395 case VERIFY_ERROR_ACCESS_FIELD:
396 case VERIFY_ERROR_ACCESS_METHOD:
Ian Rogers08f753d2012-08-24 14:35:25 -0700397 case VERIFY_ERROR_INSTANTIATION:
398 case VERIFY_ERROR_CLASS_CHANGE:
Elliott Hughes4993bbc2013-01-10 15:41:25 -0800399 if (Runtime::Current()->IsCompiler() || !can_load_classes_) {
jeffhaofaf459e2012-08-31 15:32:47 -0700400 // If we're optimistically running verification at compile time, turn NO_xxx, ACCESS_xxx,
401 // class change and instantiation errors into soft verification errors so that we re-verify
402 // at runtime. We may fail to find or to agree on access because of not yet available class
403 // loaders, or class loaders that will differ at runtime. In these cases, we don't want to
404 // affect the soundness of the code being compiled. Instead, the generated code runs "slow
405 // paths" that dynamically perform the verification and cause the behavior to be that akin
406 // to an interpreter.
407 error = VERIFY_ERROR_BAD_CLASS_SOFT;
408 } else {
409 have_pending_runtime_throw_failure_ = true;
410 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700411 break;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700412 // Indication that verification should be retried at runtime.
413 case VERIFY_ERROR_BAD_CLASS_SOFT:
Jeff Haoee988952013-04-16 14:23:47 -0700414 if (!allow_soft_failures_) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700415 have_pending_hard_failure_ = true;
416 }
417 break;
jeffhaod5347e02012-03-22 17:25:05 -0700418 // Hard verification failures at compile time will still fail at runtime, so the class is
419 // marked as rejected to prevent it from being compiled.
Ian Rogersad0b3a32012-04-16 14:50:24 -0700420 case VERIFY_ERROR_BAD_CLASS_HARD: {
421 if (Runtime::Current()->IsCompiler()) {
Ian Rogers1212a022013-03-04 10:48:41 -0800422 CompilerDriver::ClassReference ref(dex_file_, class_def_idx_);
jeffhaod1224c72012-02-29 13:43:08 -0800423 AddRejectedClass(ref);
jeffhaod1224c72012-02-29 13:43:08 -0800424 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700425 have_pending_hard_failure_ = true;
426 break;
Ian Rogers47a05882012-02-03 12:23:33 -0800427 }
428 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700429 failures_.push_back(error);
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800430 std::string location(StringPrintf("%s: [0x%X]", PrettyMethod(dex_method_idx_, *dex_file_).c_str(),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700431 work_insn_idx_));
432 std::ostringstream* failure_message = new std::ostringstream(location);
433 failure_messages_.push_back(failure_message);
434 return *failure_message;
435}
436
437void MethodVerifier::PrependToLastFailMessage(std::string prepend) {
438 size_t failure_num = failure_messages_.size();
439 DCHECK_NE(failure_num, 0U);
440 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
441 prepend += last_fail_message->str();
442 failure_messages_[failure_num - 1] = new std::ostringstream(prepend);
443 delete last_fail_message;
444}
445
446void MethodVerifier::AppendToLastFailMessage(std::string append) {
447 size_t failure_num = failure_messages_.size();
448 DCHECK_NE(failure_num, 0U);
449 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
450 (*last_fail_message) << append;
Ian Rogers47a05882012-02-03 12:23:33 -0800451}
452
Ian Rogers776ac1f2012-04-13 23:36:36 -0700453bool MethodVerifier::ComputeWidthsAndCountOps() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700454 const uint16_t* insns = code_item_->insns_;
455 size_t insns_size = code_item_->insns_size_in_code_units_;
456 const Instruction* inst = Instruction::At(insns);
jeffhaobdb76512011-09-07 11:43:16 -0700457 size_t new_instance_count = 0;
458 size_t monitor_enter_count = 0;
Ian Rogersd81871c2011-10-03 13:57:23 -0700459 size_t dex_pc = 0;
jeffhaobdb76512011-09-07 11:43:16 -0700460
Ian Rogersd81871c2011-10-03 13:57:23 -0700461 while (dex_pc < insns_size) {
jeffhaobdb76512011-09-07 11:43:16 -0700462 Instruction::Code opcode = inst->Opcode();
463 if (opcode == Instruction::NEW_INSTANCE) {
464 new_instance_count++;
465 } else if (opcode == Instruction::MONITOR_ENTER) {
466 monitor_enter_count++;
467 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700468 size_t inst_size = inst->SizeInCodeUnits();
469 insn_flags_[dex_pc].SetLengthInCodeUnits(inst_size);
470 dex_pc += inst_size;
jeffhaobdb76512011-09-07 11:43:16 -0700471 inst = inst->Next();
472 }
473
Ian Rogersd81871c2011-10-03 13:57:23 -0700474 if (dex_pc != insns_size) {
jeffhaod5347e02012-03-22 17:25:05 -0700475 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "code did not end where expected ("
476 << dex_pc << " vs. " << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700477 return false;
478 }
479
Ian Rogersd81871c2011-10-03 13:57:23 -0700480 new_instance_count_ = new_instance_count;
481 monitor_enter_count_ = monitor_enter_count;
jeffhaobdb76512011-09-07 11:43:16 -0700482 return true;
483}
484
Ian Rogers776ac1f2012-04-13 23:36:36 -0700485bool MethodVerifier::ScanTryCatchBlocks() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700486 uint32_t tries_size = code_item_->tries_size_;
jeffhaobdb76512011-09-07 11:43:16 -0700487 if (tries_size == 0) {
488 return true;
489 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700490 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Ian Rogers0571d352011-11-03 19:51:38 -0700491 const DexFile::TryItem* tries = DexFile::GetTryItems(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -0700492
493 for (uint32_t idx = 0; idx < tries_size; idx++) {
494 const DexFile::TryItem* try_item = &tries[idx];
495 uint32_t start = try_item->start_addr_;
496 uint32_t end = start + try_item->insn_count_;
jeffhaobdb76512011-09-07 11:43:16 -0700497 if ((start >= end) || (start >= insns_size) || (end > insns_size)) {
jeffhaod5347e02012-03-22 17:25:05 -0700498 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad exception entry: startAddr=" << start
499 << " endAddr=" << end << " (size=" << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700500 return false;
501 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700502 if (!insn_flags_[start].IsOpcode()) {
jeffhaod5347e02012-03-22 17:25:05 -0700503 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'try' block starts inside an instruction (" << start << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700504 return false;
505 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700506 for (uint32_t dex_pc = start; dex_pc < end;
507 dex_pc += insn_flags_[dex_pc].GetLengthInCodeUnits()) {
508 insn_flags_[dex_pc].SetInTry();
jeffhaobdb76512011-09-07 11:43:16 -0700509 }
510 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800511 // Iterate over each of the handlers to verify target addresses.
Ian Rogers0571d352011-11-03 19:51:38 -0700512 const byte* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -0700513 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700514 ClassLinker* linker = Runtime::Current()->GetClassLinker();
jeffhaobdb76512011-09-07 11:43:16 -0700515 for (uint32_t idx = 0; idx < handlers_size; idx++) {
Ian Rogers0571d352011-11-03 19:51:38 -0700516 CatchHandlerIterator iterator(handlers_ptr);
517 for (; iterator.HasNext(); iterator.Next()) {
518 uint32_t dex_pc= iterator.GetHandlerAddress();
Ian Rogersd81871c2011-10-03 13:57:23 -0700519 if (!insn_flags_[dex_pc].IsOpcode()) {
jeffhaod5347e02012-03-22 17:25:05 -0700520 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "exception handler starts at bad address (" << dex_pc << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700521 return false;
522 }
jeffhao60f83e32012-02-13 17:16:30 -0800523 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
524 if (inst->Opcode() != Instruction::MOVE_EXCEPTION) {
jeffhaod5347e02012-03-22 17:25:05 -0700525 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "exception handler doesn't start with move-exception ("
Ian Rogersad0b3a32012-04-16 14:50:24 -0700526 << dex_pc << ")";
jeffhao60f83e32012-02-13 17:16:30 -0800527 return false;
528 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700529 insn_flags_[dex_pc].SetBranchTarget();
Ian Rogers28ad40d2011-10-27 15:19:26 -0700530 // Ensure exception types are resolved so that they don't need resolution to be delivered,
531 // unresolved exception types will be ignored by exception delivery
Ian Rogers0571d352011-11-03 19:51:38 -0700532 if (iterator.GetHandlerTypeIndex() != DexFile::kDexNoIndex16) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800533 mirror::Class* exception_type = linker->ResolveType(*dex_file_,
534 iterator.GetHandlerTypeIndex(),
535 dex_cache_, class_loader_);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700536 if (exception_type == NULL) {
537 DCHECK(Thread::Current()->IsExceptionPending());
538 Thread::Current()->ClearException();
539 }
540 }
jeffhaobdb76512011-09-07 11:43:16 -0700541 }
Ian Rogers0571d352011-11-03 19:51:38 -0700542 handlers_ptr = iterator.EndDataPointer();
jeffhaobdb76512011-09-07 11:43:16 -0700543 }
jeffhaobdb76512011-09-07 11:43:16 -0700544 return true;
545}
546
Ian Rogers776ac1f2012-04-13 23:36:36 -0700547bool MethodVerifier::VerifyInstructions() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700548 const Instruction* inst = Instruction::At(code_item_->insns_);
jeffhaoba5ebb92011-08-25 17:24:37 -0700549
Ian Rogers0c7abda2012-09-19 13:33:42 -0700550 /* 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 -0700551 insn_flags_[0].SetBranchTarget();
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700552 insn_flags_[0].SetCompileTimeInfoPoint();
Ian Rogersd81871c2011-10-03 13:57:23 -0700553
554 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700555 for (uint32_t dex_pc = 0; dex_pc < insns_size;) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700556 if (!VerifyInstruction(inst, dex_pc)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700557 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -0700558 return false;
559 }
560 /* Flag instructions that are garbage collection points */
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700561 // All invoke points are marked as "Throw" points already.
562 // We are relying on this to also count all the invokes as interesting.
Ian Rogersd81871c2011-10-03 13:57:23 -0700563 if (inst->IsBranch() || inst->IsSwitch() || inst->IsThrow() || inst->IsReturn()) {
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700564 insn_flags_[dex_pc].SetCompileTimeInfoPoint();
Ian Rogersd81871c2011-10-03 13:57:23 -0700565 }
566 dex_pc += inst->SizeInCodeUnits();
567 inst = inst->Next();
568 }
569 return true;
570}
571
Ian Rogers776ac1f2012-04-13 23:36:36 -0700572bool MethodVerifier::VerifyInstruction(const Instruction* inst, uint32_t code_offset) {
Elliott Hughesadb8c672012-03-06 16:49:32 -0800573 DecodedInstruction dec_insn(inst);
Ian Rogersd81871c2011-10-03 13:57:23 -0700574 bool result = true;
575 switch (inst->GetVerifyTypeArgumentA()) {
576 case Instruction::kVerifyRegA:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800577 result = result && CheckRegisterIndex(dec_insn.vA);
Ian Rogersd81871c2011-10-03 13:57:23 -0700578 break;
579 case Instruction::kVerifyRegAWide:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800580 result = result && CheckWideRegisterIndex(dec_insn.vA);
Ian Rogersd81871c2011-10-03 13:57:23 -0700581 break;
582 }
583 switch (inst->GetVerifyTypeArgumentB()) {
584 case Instruction::kVerifyRegB:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800585 result = result && CheckRegisterIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700586 break;
587 case Instruction::kVerifyRegBField:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800588 result = result && CheckFieldIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700589 break;
590 case Instruction::kVerifyRegBMethod:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800591 result = result && CheckMethodIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700592 break;
593 case Instruction::kVerifyRegBNewInstance:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800594 result = result && CheckNewInstance(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700595 break;
596 case Instruction::kVerifyRegBString:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800597 result = result && CheckStringIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700598 break;
599 case Instruction::kVerifyRegBType:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800600 result = result && CheckTypeIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700601 break;
602 case Instruction::kVerifyRegBWide:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800603 result = result && CheckWideRegisterIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700604 break;
605 }
606 switch (inst->GetVerifyTypeArgumentC()) {
607 case Instruction::kVerifyRegC:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800608 result = result && CheckRegisterIndex(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700609 break;
610 case Instruction::kVerifyRegCField:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800611 result = result && CheckFieldIndex(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700612 break;
613 case Instruction::kVerifyRegCNewArray:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800614 result = result && CheckNewArray(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700615 break;
616 case Instruction::kVerifyRegCType:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800617 result = result && CheckTypeIndex(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700618 break;
619 case Instruction::kVerifyRegCWide:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800620 result = result && CheckWideRegisterIndex(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700621 break;
622 }
623 switch (inst->GetVerifyExtraFlags()) {
624 case Instruction::kVerifyArrayData:
625 result = result && CheckArrayData(code_offset);
626 break;
627 case Instruction::kVerifyBranchTarget:
628 result = result && CheckBranchTarget(code_offset);
629 break;
630 case Instruction::kVerifySwitchTargets:
631 result = result && CheckSwitchTargets(code_offset);
632 break;
633 case Instruction::kVerifyVarArg:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800634 result = result && CheckVarArgRegs(dec_insn.vA, dec_insn.arg);
Ian Rogersd81871c2011-10-03 13:57:23 -0700635 break;
636 case Instruction::kVerifyVarArgRange:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800637 result = result && CheckVarArgRangeRegs(dec_insn.vA, dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700638 break;
639 case Instruction::kVerifyError:
jeffhaod5347e02012-03-22 17:25:05 -0700640 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected opcode " << inst->Name();
Ian Rogersd81871c2011-10-03 13:57:23 -0700641 result = false;
642 break;
643 }
644 return result;
645}
646
Ian Rogers776ac1f2012-04-13 23:36:36 -0700647bool MethodVerifier::CheckRegisterIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700648 if (idx >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700649 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register index out of range (" << idx << " >= "
650 << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700651 return false;
652 }
653 return true;
654}
655
Ian Rogers776ac1f2012-04-13 23:36:36 -0700656bool MethodVerifier::CheckWideRegisterIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700657 if (idx + 1 >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700658 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "wide register index out of range (" << idx
659 << "+1 >= " << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700660 return false;
661 }
662 return true;
663}
664
Ian Rogers776ac1f2012-04-13 23:36:36 -0700665bool MethodVerifier::CheckFieldIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700666 if (idx >= dex_file_->GetHeader().field_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700667 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad field index " << idx << " (max "
668 << dex_file_->GetHeader().field_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700669 return false;
670 }
671 return true;
672}
673
Ian Rogers776ac1f2012-04-13 23:36:36 -0700674bool MethodVerifier::CheckMethodIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700675 if (idx >= dex_file_->GetHeader().method_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700676 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad method index " << idx << " (max "
677 << dex_file_->GetHeader().method_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700678 return false;
679 }
680 return true;
681}
682
Ian Rogers776ac1f2012-04-13 23:36:36 -0700683bool MethodVerifier::CheckNewInstance(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700684 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700685 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
686 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700687 return false;
688 }
689 // We don't need the actual class, just a pointer to the class name.
Ian Rogers0571d352011-11-03 19:51:38 -0700690 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -0700691 if (descriptor[0] != 'L') {
jeffhaod5347e02012-03-22 17:25:05 -0700692 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "can't call new-instance on type '" << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -0700693 return false;
694 }
695 return true;
696}
697
Ian Rogers776ac1f2012-04-13 23:36:36 -0700698bool MethodVerifier::CheckStringIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700699 if (idx >= dex_file_->GetHeader().string_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700700 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad string index " << idx << " (max "
701 << dex_file_->GetHeader().string_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700702 return false;
703 }
704 return true;
705}
706
Ian Rogers776ac1f2012-04-13 23:36:36 -0700707bool MethodVerifier::CheckTypeIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700708 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700709 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
710 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700711 return false;
712 }
713 return true;
714}
715
Ian Rogers776ac1f2012-04-13 23:36:36 -0700716bool MethodVerifier::CheckNewArray(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700717 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700718 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
719 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700720 return false;
721 }
722 int bracket_count = 0;
Ian Rogers0571d352011-11-03 19:51:38 -0700723 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -0700724 const char* cp = descriptor;
725 while (*cp++ == '[') {
726 bracket_count++;
727 }
728 if (bracket_count == 0) {
729 /* The given class must be an array type. */
jeffhaod5347e02012-03-22 17:25:05 -0700730 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "can't new-array class '" << descriptor << "' (not an array)";
Ian Rogersd81871c2011-10-03 13:57:23 -0700731 return false;
732 } else if (bracket_count > 255) {
733 /* It is illegal to create an array of more than 255 dimensions. */
jeffhaod5347e02012-03-22 17:25:05 -0700734 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "can't new-array class '" << descriptor << "' (exceeds limit)";
Ian Rogersd81871c2011-10-03 13:57:23 -0700735 return false;
736 }
737 return true;
738}
739
Ian Rogers776ac1f2012-04-13 23:36:36 -0700740bool MethodVerifier::CheckArrayData(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700741 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
742 const uint16_t* insns = code_item_->insns_ + cur_offset;
743 const uint16_t* array_data;
744 int32_t array_data_offset;
745
746 DCHECK_LT(cur_offset, insn_count);
747 /* make sure the start of the array data table is in range */
748 array_data_offset = insns[1] | (((int32_t) insns[2]) << 16);
749 if ((int32_t) cur_offset + array_data_offset < 0 ||
750 cur_offset + array_data_offset + 2 >= insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -0700751 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data start: at " << cur_offset
752 << ", data offset " << array_data_offset << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -0700753 return false;
754 }
755 /* offset to array data table is a relative branch-style offset */
756 array_data = insns + array_data_offset;
757 /* make sure the table is 32-bit aligned */
758 if ((((uint32_t) array_data) & 0x03) != 0) {
jeffhaod5347e02012-03-22 17:25:05 -0700759 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned array data table: at " << cur_offset
760 << ", data offset " << array_data_offset;
Ian Rogersd81871c2011-10-03 13:57:23 -0700761 return false;
762 }
763 uint32_t value_width = array_data[1];
Elliott Hughes398f64b2012-03-26 18:05:48 -0700764 uint32_t value_count = *reinterpret_cast<const uint32_t*>(&array_data[2]);
Ian Rogersd81871c2011-10-03 13:57:23 -0700765 uint32_t table_size = 4 + (value_width * value_count + 1) / 2;
766 /* make sure the end of the switch is in range */
767 if (cur_offset + array_data_offset + table_size > insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -0700768 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data end: at " << cur_offset
769 << ", data offset " << array_data_offset << ", end "
770 << cur_offset + array_data_offset + table_size
771 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -0700772 return false;
773 }
774 return true;
775}
776
Ian Rogers776ac1f2012-04-13 23:36:36 -0700777bool MethodVerifier::CheckBranchTarget(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700778 int32_t offset;
779 bool isConditional, selfOkay;
780 if (!GetBranchOffset(cur_offset, &offset, &isConditional, &selfOkay)) {
781 return false;
782 }
783 if (!selfOkay && offset == 0) {
Elliott Hughes398f64b2012-03-26 18:05:48 -0700784 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch offset of zero not allowed at" << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -0700785 return false;
786 }
Elliott Hughes81ff3182012-03-23 20:35:56 -0700787 // Check for 32-bit overflow. This isn't strictly necessary if we can depend on the runtime
788 // to have identical "wrap-around" behavior, but it's unwise to depend on that.
Ian Rogersd81871c2011-10-03 13:57:23 -0700789 if (((int64_t) cur_offset + (int64_t) offset) != (int64_t) (cur_offset + offset)) {
Elliott Hughes398f64b2012-03-26 18:05:48 -0700790 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch target overflow " << reinterpret_cast<void*>(cur_offset) << " +" << offset;
Ian Rogersd81871c2011-10-03 13:57:23 -0700791 return false;
792 }
793 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
794 int32_t abs_offset = cur_offset + offset;
795 if (abs_offset < 0 || (uint32_t) abs_offset >= insn_count || !insn_flags_[abs_offset].IsOpcode()) {
jeffhaod5347e02012-03-22 17:25:05 -0700796 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid branch target " << offset << " (-> "
Elliott Hughes398f64b2012-03-26 18:05:48 -0700797 << reinterpret_cast<void*>(abs_offset) << ") at "
798 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -0700799 return false;
800 }
801 insn_flags_[abs_offset].SetBranchTarget();
802 return true;
803}
804
Ian Rogers776ac1f2012-04-13 23:36:36 -0700805bool MethodVerifier::GetBranchOffset(uint32_t cur_offset, int32_t* pOffset, bool* pConditional,
Ian Rogersd81871c2011-10-03 13:57:23 -0700806 bool* selfOkay) {
807 const uint16_t* insns = code_item_->insns_ + cur_offset;
808 *pConditional = false;
809 *selfOkay = false;
jeffhaoba5ebb92011-08-25 17:24:37 -0700810 switch (*insns & 0xff) {
811 case Instruction::GOTO:
812 *pOffset = ((int16_t) *insns) >> 8;
jeffhaoba5ebb92011-08-25 17:24:37 -0700813 break;
814 case Instruction::GOTO_32:
815 *pOffset = insns[1] | (((uint32_t) insns[2]) << 16);
jeffhaoba5ebb92011-08-25 17:24:37 -0700816 *selfOkay = true;
817 break;
818 case Instruction::GOTO_16:
819 *pOffset = (int16_t) insns[1];
jeffhaoba5ebb92011-08-25 17:24:37 -0700820 break;
821 case Instruction::IF_EQ:
822 case Instruction::IF_NE:
823 case Instruction::IF_LT:
824 case Instruction::IF_GE:
825 case Instruction::IF_GT:
826 case Instruction::IF_LE:
827 case Instruction::IF_EQZ:
828 case Instruction::IF_NEZ:
829 case Instruction::IF_LTZ:
830 case Instruction::IF_GEZ:
831 case Instruction::IF_GTZ:
832 case Instruction::IF_LEZ:
833 *pOffset = (int16_t) insns[1];
834 *pConditional = true;
jeffhaoba5ebb92011-08-25 17:24:37 -0700835 break;
836 default:
837 return false;
838 break;
839 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700840 return true;
841}
842
Ian Rogers776ac1f2012-04-13 23:36:36 -0700843bool MethodVerifier::CheckSwitchTargets(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700844 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700845 DCHECK_LT(cur_offset, insn_count);
Ian Rogersd81871c2011-10-03 13:57:23 -0700846 const uint16_t* insns = code_item_->insns_ + cur_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -0700847 /* make sure the start of the switch is in range */
Ian Rogersd81871c2011-10-03 13:57:23 -0700848 int32_t switch_offset = insns[1] | ((int32_t) insns[2]) << 16;
849 if ((int32_t) cur_offset + switch_offset < 0 || cur_offset + switch_offset + 2 >= insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -0700850 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch start: at " << cur_offset
851 << ", switch offset " << switch_offset << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -0700852 return false;
853 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700854 /* offset to switch table is a relative branch-style offset */
Ian Rogersd81871c2011-10-03 13:57:23 -0700855 const uint16_t* switch_insns = insns + switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -0700856 /* make sure the table is 32-bit aligned */
857 if ((((uint32_t) switch_insns) & 0x03) != 0) {
jeffhaod5347e02012-03-22 17:25:05 -0700858 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned switch table: at " << cur_offset
859 << ", switch offset " << switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -0700860 return false;
861 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700862 uint32_t switch_count = switch_insns[1];
863 int32_t keys_offset, targets_offset;
864 uint16_t expected_signature;
jeffhaoba5ebb92011-08-25 17:24:37 -0700865 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
866 /* 0=sig, 1=count, 2/3=firstKey */
867 targets_offset = 4;
868 keys_offset = -1;
869 expected_signature = Instruction::kPackedSwitchSignature;
870 } else {
871 /* 0=sig, 1=count, 2..count*2 = keys */
872 keys_offset = 2;
873 targets_offset = 2 + 2 * switch_count;
874 expected_signature = Instruction::kSparseSwitchSignature;
875 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700876 uint32_t table_size = targets_offset + switch_count * 2;
jeffhaoba5ebb92011-08-25 17:24:37 -0700877 if (switch_insns[0] != expected_signature) {
jeffhaod5347e02012-03-22 17:25:05 -0700878 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << StringPrintf("wrong signature for switch table (%x, wanted %x)",
879 switch_insns[0], expected_signature);
jeffhaoba5ebb92011-08-25 17:24:37 -0700880 return false;
881 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700882 /* make sure the end of the switch is in range */
883 if (cur_offset + switch_offset + table_size > (uint32_t) insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -0700884 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch end: at " << cur_offset << ", switch offset "
885 << switch_offset << ", end "
886 << (cur_offset + switch_offset + table_size)
887 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -0700888 return false;
889 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700890 /* for a sparse switch, verify the keys are in ascending order */
891 if (keys_offset > 0 && switch_count > 1) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700892 int32_t last_key = switch_insns[keys_offset] | (switch_insns[keys_offset + 1] << 16);
893 for (uint32_t targ = 1; targ < switch_count; targ++) {
jeffhaoba5ebb92011-08-25 17:24:37 -0700894 int32_t key = (int32_t) switch_insns[keys_offset + targ * 2] |
895 (int32_t) (switch_insns[keys_offset + targ * 2 + 1] << 16);
896 if (key <= last_key) {
jeffhaod5347e02012-03-22 17:25:05 -0700897 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid packed switch: last key=" << last_key
898 << ", this=" << key;
jeffhaoba5ebb92011-08-25 17:24:37 -0700899 return false;
900 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700901 last_key = key;
902 }
903 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700904 /* verify each switch target */
Ian Rogersd81871c2011-10-03 13:57:23 -0700905 for (uint32_t targ = 0; targ < switch_count; targ++) {
906 int32_t offset = (int32_t) switch_insns[targets_offset + targ * 2] |
907 (int32_t) (switch_insns[targets_offset + targ * 2 + 1] << 16);
908 int32_t abs_offset = cur_offset + offset;
909 if (abs_offset < 0 || abs_offset >= (int32_t) insn_count || !insn_flags_[abs_offset].IsOpcode()) {
jeffhaod5347e02012-03-22 17:25:05 -0700910 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch target " << offset << " (-> "
Elliott Hughes398f64b2012-03-26 18:05:48 -0700911 << reinterpret_cast<void*>(abs_offset) << ") at "
912 << reinterpret_cast<void*>(cur_offset) << "[" << targ << "]";
jeffhaoba5ebb92011-08-25 17:24:37 -0700913 return false;
914 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700915 insn_flags_[abs_offset].SetBranchTarget();
916 }
917 return true;
918}
919
Ian Rogers776ac1f2012-04-13 23:36:36 -0700920bool MethodVerifier::CheckVarArgRegs(uint32_t vA, uint32_t arg[]) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700921 if (vA > 5) {
jeffhaod5347e02012-03-22 17:25:05 -0700922 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << vA << ") in non-range invoke)";
Ian Rogersd81871c2011-10-03 13:57:23 -0700923 return false;
924 }
925 uint16_t registers_size = code_item_->registers_size_;
926 for (uint32_t idx = 0; idx < vA; idx++) {
jeffhao457cc512012-02-02 16:55:13 -0800927 if (arg[idx] >= registers_size) {
jeffhaod5347e02012-03-22 17:25:05 -0700928 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index (" << arg[idx]
929 << ") in non-range invoke (>= " << registers_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700930 return false;
931 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700932 }
933
934 return true;
935}
936
Ian Rogers776ac1f2012-04-13 23:36:36 -0700937bool MethodVerifier::CheckVarArgRangeRegs(uint32_t vA, uint32_t vC) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700938 uint16_t registers_size = code_item_->registers_size_;
939 // vA/vC are unsigned 8-bit/16-bit quantities for /range instructions, so there's no risk of
940 // integer overflow when adding them here.
941 if (vA + vC > registers_size) {
jeffhaod5347e02012-03-22 17:25:05 -0700942 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index " << vA << "+" << vC << " in range invoke (> "
943 << registers_size << ")";
jeffhaoba5ebb92011-08-25 17:24:37 -0700944 return false;
945 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700946 return true;
947}
948
Ian Rogers0c7abda2012-09-19 13:33:42 -0700949static const std::vector<uint8_t>* CreateLengthPrefixedDexGcMap(const std::vector<uint8_t>& gc_map) {
Brian Carlstrom75412882012-01-18 01:26:54 -0800950 std::vector<uint8_t>* length_prefixed_gc_map = new std::vector<uint8_t>;
Ian Rogers637c65b2013-05-31 11:46:00 -0700951 length_prefixed_gc_map->reserve(gc_map.size() + 4);
Brian Carlstrom75412882012-01-18 01:26:54 -0800952 length_prefixed_gc_map->push_back((gc_map.size() & 0xff000000) >> 24);
953 length_prefixed_gc_map->push_back((gc_map.size() & 0x00ff0000) >> 16);
954 length_prefixed_gc_map->push_back((gc_map.size() & 0x0000ff00) >> 8);
955 length_prefixed_gc_map->push_back((gc_map.size() & 0x000000ff) >> 0);
956 length_prefixed_gc_map->insert(length_prefixed_gc_map->end(),
957 gc_map.begin(),
958 gc_map.end());
959 DCHECK_EQ(gc_map.size() + 4, length_prefixed_gc_map->size());
960 DCHECK_EQ(gc_map.size(),
961 static_cast<size_t>((length_prefixed_gc_map->at(0) << 24) |
962 (length_prefixed_gc_map->at(1) << 16) |
963 (length_prefixed_gc_map->at(2) << 8) |
964 (length_prefixed_gc_map->at(3) << 0)));
965 return length_prefixed_gc_map;
966}
967
Ian Rogers776ac1f2012-04-13 23:36:36 -0700968bool MethodVerifier::VerifyCodeFlow() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700969 uint16_t registers_size = code_item_->registers_size_;
970 uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaobdb76512011-09-07 11:43:16 -0700971
Ian Rogersd81871c2011-10-03 13:57:23 -0700972 if (registers_size * insns_size > 4*1024*1024) {
buzbee4922ef92012-02-24 14:32:20 -0800973 LOG(WARNING) << "warning: method is huge (regs=" << registers_size
974 << " insns_size=" << insns_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700975 }
976 /* Create and initialize table holding register status */
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700977 reg_table_.Init(kTrackCompilerInterestPoints, insn_flags_.get(), insns_size, registers_size, this);
978
jeffhaobdb76512011-09-07 11:43:16 -0700979
Ian Rogersd81871c2011-10-03 13:57:23 -0700980 work_line_.reset(new RegisterLine(registers_size, this));
981 saved_line_.reset(new RegisterLine(registers_size, this));
jeffhaobdb76512011-09-07 11:43:16 -0700982
Ian Rogersd81871c2011-10-03 13:57:23 -0700983 /* Initialize register types of method arguments. */
984 if (!SetTypesFromSignature()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700985 DCHECK_NE(failures_.size(), 0U);
986 std::string prepend("Bad signature in ");
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800987 prepend += PrettyMethod(dex_method_idx_, *dex_file_);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700988 PrependToLastFailMessage(prepend);
Ian Rogersd81871c2011-10-03 13:57:23 -0700989 return false;
990 }
991 /* Perform code flow verification. */
992 if (!CodeFlowVerifyMethod()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700993 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -0700994 return false;
jeffhaobdb76512011-09-07 11:43:16 -0700995 }
996
Ian Rogers1212a022013-03-04 10:48:41 -0800997 CompilerDriver::MethodReference ref(dex_file_, dex_method_idx_);
TDYa127b2eb5c12012-05-24 15:52:10 -0700998
TDYa127b2eb5c12012-05-24 15:52:10 -0700999
Ian Rogersd81871c2011-10-03 13:57:23 -07001000 /* Generate a register map and add it to the method. */
Brian Carlstrom75412882012-01-18 01:26:54 -08001001 UniquePtr<const std::vector<uint8_t> > map(GenerateGcMap());
1002 if (map.get() == NULL) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001003 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -07001004 return false; // Not a real failure, but a failure to encode
1005 }
Ian Rogers39ebcb82013-05-30 16:57:23 -07001006 if (kIsDebugBuild) {
1007 VerifyGcMap(*map);
1008 }
Ian Rogers0c7abda2012-09-19 13:33:42 -07001009 const std::vector<uint8_t>* dex_gc_map = CreateLengthPrefixedDexGcMap(*(map.get()));
1010 verifier::MethodVerifier::SetDexGcMap(ref, *dex_gc_map);
Logan Chiendd361c92012-04-10 23:40:37 +08001011
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07001012 MethodVerifier::MethodSafeCastSet* method_to_safe_casts = GenerateSafeCastSet();
1013 if(method_to_safe_casts != NULL ) {
1014 SetSafeCastMap(ref, method_to_safe_casts);
1015 }
1016
Ian Rogersd0583802013-06-01 10:51:46 -07001017 MethodVerifier::PcToConcreteMethodMap* pc_to_concrete_method = GenerateDevirtMap();
Ian Rogers1bf8d4d2013-05-30 00:18:49 -07001018 if(pc_to_concrete_method != NULL ) {
1019 SetDevirtMap(ref, pc_to_concrete_method);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07001020 }
jeffhaobdb76512011-09-07 11:43:16 -07001021 return true;
1022}
1023
Ian Rogersad0b3a32012-04-16 14:50:24 -07001024std::ostream& MethodVerifier::DumpFailures(std::ostream& os) {
1025 DCHECK_EQ(failures_.size(), failure_messages_.size());
1026 for (size_t i = 0; i < failures_.size(); ++i) {
Elliott Hughesc073b072012-05-24 19:29:17 -07001027 os << failure_messages_[i]->str() << "\n";
Ian Rogersad0b3a32012-04-16 14:50:24 -07001028 }
1029 return os;
1030}
1031
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001032extern "C" void MethodVerifierGdbDump(MethodVerifier* v)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001033 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001034 v->Dump(std::cerr);
1035}
1036
Ian Rogers776ac1f2012-04-13 23:36:36 -07001037void MethodVerifier::Dump(std::ostream& os) {
jeffhaof56197c2012-03-05 18:01:54 -08001038 if (code_item_ == NULL) {
Elliott Hughesc073b072012-05-24 19:29:17 -07001039 os << "Native method\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001040 return;
jeffhaobdb76512011-09-07 11:43:16 -07001041 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001042 {
1043 os << "Register Types:\n";
1044 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
1045 std::ostream indent_os(&indent_filter);
1046 reg_types_.Dump(indent_os);
1047 }
Ian Rogersb4903572012-10-11 11:52:56 -07001048 os << "Dumping instructions and register lines:\n";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001049 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
1050 std::ostream indent_os(&indent_filter);
Ian Rogersd81871c2011-10-03 13:57:23 -07001051 const Instruction* inst = Instruction::At(code_item_->insns_);
1052 for (size_t dex_pc = 0; dex_pc < code_item_->insns_size_in_code_units_;
1053 dex_pc += insn_flags_[dex_pc].GetLengthInCodeUnits()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001054 RegisterLine* reg_line = reg_table_.GetLine(dex_pc);
1055 if (reg_line != NULL) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001056 indent_os << reg_line->Dump() << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07001057 }
Ian Rogers7b3ddd22013-02-21 15:19:52 -08001058 indent_os << StringPrintf("0x%04zx", dex_pc) << ": " << insn_flags_[dex_pc].ToString() << " ";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001059 const bool kDumpHexOfInstruction = false;
1060 if (kDumpHexOfInstruction) {
1061 indent_os << inst->DumpHex(5) << " ";
1062 }
1063 indent_os << inst->DumpString(dex_file_) << "\n";
jeffhaoba5ebb92011-08-25 17:24:37 -07001064 inst = inst->Next();
1065 }
jeffhaobdb76512011-09-07 11:43:16 -07001066}
1067
Ian Rogersd81871c2011-10-03 13:57:23 -07001068static bool IsPrimitiveDescriptor(char descriptor) {
1069 switch (descriptor) {
jeffhaobdb76512011-09-07 11:43:16 -07001070 case 'I':
1071 case 'C':
1072 case 'S':
1073 case 'B':
1074 case 'Z':
jeffhaobdb76512011-09-07 11:43:16 -07001075 case 'F':
1076 case 'D':
1077 case 'J':
Ian Rogersd81871c2011-10-03 13:57:23 -07001078 return true;
jeffhaobdb76512011-09-07 11:43:16 -07001079 default:
1080 return false;
1081 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001082}
1083
Ian Rogers776ac1f2012-04-13 23:36:36 -07001084bool MethodVerifier::SetTypesFromSignature() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001085 RegisterLine* reg_line = reg_table_.GetLine(0);
1086 int arg_start = code_item_->registers_size_ - code_item_->ins_size_;
1087 size_t expected_args = code_item_->ins_size_; /* long/double count as two */
jeffhaobdb76512011-09-07 11:43:16 -07001088
Ian Rogersd81871c2011-10-03 13:57:23 -07001089 DCHECK_GE(arg_start, 0); /* should have been verified earlier */
1090 //Include the "this" pointer.
1091 size_t cur_arg = 0;
Ian Rogersad0b3a32012-04-16 14:50:24 -07001092 if (!IsStatic()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001093 // If this is a constructor for a class other than java.lang.Object, mark the first ("this")
1094 // argument as uninitialized. This restricts field access until the superclass constructor is
1095 // called.
Ian Rogersad0b3a32012-04-16 14:50:24 -07001096 const RegType& declaring_class = GetDeclaringClass();
1097 if (IsConstructor() && !declaring_class.IsJavaLangObject()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001098 reg_line->SetRegisterType(arg_start + cur_arg,
1099 reg_types_.UninitializedThisArgument(declaring_class));
1100 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001101 reg_line->SetRegisterType(arg_start + cur_arg, declaring_class);
jeffhaobdb76512011-09-07 11:43:16 -07001102 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001103 cur_arg++;
jeffhaobdb76512011-09-07 11:43:16 -07001104 }
1105
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001106 const DexFile::ProtoId& proto_id =
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001107 dex_file_->GetMethodPrototype(dex_file_->GetMethodId(dex_method_idx_));
Ian Rogers0571d352011-11-03 19:51:38 -07001108 DexFileParameterIterator iterator(*dex_file_, proto_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07001109
1110 for (; iterator.HasNext(); iterator.Next()) {
1111 const char* descriptor = iterator.GetDescriptor();
1112 if (descriptor == NULL) {
1113 LOG(FATAL) << "Null descriptor";
1114 }
1115 if (cur_arg >= expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07001116 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1117 << " args, found more (" << descriptor << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001118 return false;
1119 }
1120 switch (descriptor[0]) {
1121 case 'L':
1122 case '[':
1123 // We assume that reference arguments are initialized. The only way it could be otherwise
1124 // (assuming the caller was verified) is if the current method is <init>, but in that case
1125 // it's effectively considered initialized the instant we reach here (in the sense that we
1126 // can return without doing anything or call virtual methods).
1127 {
Ian Rogersb4903572012-10-11 11:52:56 -07001128 const RegType& reg_type = reg_types_.FromDescriptor(class_loader_, descriptor, false);
Ian Rogers84fa0742011-10-25 18:13:30 -07001129 reg_line->SetRegisterType(arg_start + cur_arg, reg_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001130 }
1131 break;
1132 case 'Z':
1133 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Boolean());
1134 break;
1135 case 'C':
1136 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Char());
1137 break;
1138 case 'B':
1139 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Byte());
1140 break;
1141 case 'I':
1142 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Integer());
1143 break;
1144 case 'S':
1145 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Short());
1146 break;
1147 case 'F':
1148 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Float());
1149 break;
1150 case 'J':
1151 case 'D': {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001152 const RegType& lo_half = descriptor[0] == 'J' ? reg_types_.LongLo() : reg_types_.DoubleLo();
1153 const RegType& hi_half = descriptor[0] == 'J' ? reg_types_.LongHi() : reg_types_.DoubleHi();
1154 reg_line->SetRegisterTypeWide(arg_start + cur_arg, lo_half, hi_half);
Ian Rogersd81871c2011-10-03 13:57:23 -07001155 cur_arg++;
1156 break;
1157 }
1158 default:
jeffhaod5347e02012-03-22 17:25:05 -07001159 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected signature type char '" << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001160 return false;
1161 }
1162 cur_arg++;
1163 }
1164 if (cur_arg != expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07001165 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args << " arguments, found " << cur_arg;
Ian Rogersd81871c2011-10-03 13:57:23 -07001166 return false;
1167 }
1168 const char* descriptor = dex_file_->GetReturnTypeDescriptor(proto_id);
1169 // Validate return type. We don't do the type lookup; just want to make sure that it has the right
1170 // format. Only major difference from the method argument format is that 'V' is supported.
1171 bool result;
1172 if (IsPrimitiveDescriptor(descriptor[0]) || descriptor[0] == 'V') {
1173 result = descriptor[1] == '\0';
1174 } else if (descriptor[0] == '[') { // single/multi-dimensional array of object/primitive
1175 size_t i = 0;
1176 do {
1177 i++;
1178 } while (descriptor[i] == '['); // process leading [
1179 if (descriptor[i] == 'L') { // object array
1180 do {
1181 i++; // find closing ;
1182 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1183 result = descriptor[i] == ';';
1184 } else { // primitive array
1185 result = IsPrimitiveDescriptor(descriptor[i]) && descriptor[i + 1] == '\0';
1186 }
1187 } else if (descriptor[0] == 'L') {
1188 // could be more thorough here, but shouldn't be required
1189 size_t i = 0;
1190 do {
1191 i++;
1192 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1193 result = descriptor[i] == ';';
1194 } else {
1195 result = false;
1196 }
1197 if (!result) {
jeffhaod5347e02012-03-22 17:25:05 -07001198 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected char in return type descriptor '"
1199 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001200 }
1201 return result;
jeffhaobdb76512011-09-07 11:43:16 -07001202}
1203
Ian Rogers776ac1f2012-04-13 23:36:36 -07001204bool MethodVerifier::CodeFlowVerifyMethod() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001205 const uint16_t* insns = code_item_->insns_;
1206 const uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaoba5ebb92011-08-25 17:24:37 -07001207
jeffhaobdb76512011-09-07 11:43:16 -07001208 /* Begin by marking the first instruction as "changed". */
Ian Rogersd81871c2011-10-03 13:57:23 -07001209 insn_flags_[0].SetChanged();
1210 uint32_t start_guess = 0;
jeffhaoba5ebb92011-08-25 17:24:37 -07001211
jeffhaobdb76512011-09-07 11:43:16 -07001212 /* Continue until no instructions are marked "changed". */
1213 while (true) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001214 // Find the first marked one. Use "start_guess" as a way to find one quickly.
1215 uint32_t insn_idx = start_guess;
1216 for (; insn_idx < insns_size; insn_idx++) {
1217 if (insn_flags_[insn_idx].IsChanged())
jeffhaobdb76512011-09-07 11:43:16 -07001218 break;
1219 }
jeffhaobdb76512011-09-07 11:43:16 -07001220 if (insn_idx == insns_size) {
1221 if (start_guess != 0) {
1222 /* try again, starting from the top */
1223 start_guess = 0;
1224 continue;
1225 } else {
1226 /* all flags are clear */
1227 break;
1228 }
1229 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001230 // We carry the working set of registers from instruction to instruction. If this address can
1231 // be the target of a branch (or throw) instruction, or if we're skipping around chasing
1232 // "changed" flags, we need to load the set of registers from the table.
1233 // Because we always prefer to continue on to the next instruction, we should never have a
1234 // situation where we have a stray "changed" flag set on an instruction that isn't a branch
1235 // target.
1236 work_insn_idx_ = insn_idx;
1237 if (insn_flags_[insn_idx].IsBranchTarget()) {
1238 work_line_->CopyFromLine(reg_table_.GetLine(insn_idx));
jeffhaobdb76512011-09-07 11:43:16 -07001239 } else {
1240#ifndef NDEBUG
1241 /*
1242 * Sanity check: retrieve the stored register line (assuming
1243 * a full table) and make sure it actually matches.
1244 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001245 RegisterLine* register_line = reg_table_.GetLine(insn_idx);
1246 if (register_line != NULL) {
1247 if (work_line_->CompareLine(register_line) != 0) {
1248 Dump(std::cout);
1249 std::cout << info_messages_.str();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001250 LOG(FATAL) << "work_line diverged in " << PrettyMethod(dex_method_idx_, *dex_file_)
Elliott Hughesc073b072012-05-24 19:29:17 -07001251 << "@" << reinterpret_cast<void*>(work_insn_idx_) << "\n"
1252 << " work_line=" << *work_line_ << "\n"
Elliott Hughes398f64b2012-03-26 18:05:48 -07001253 << " expected=" << *register_line;
Ian Rogersd81871c2011-10-03 13:57:23 -07001254 }
jeffhaobdb76512011-09-07 11:43:16 -07001255 }
1256#endif
1257 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001258 if (!CodeFlowVerifyInstruction(&start_guess)) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001259 std::string prepend(PrettyMethod(dex_method_idx_, *dex_file_));
Ian Rogersad0b3a32012-04-16 14:50:24 -07001260 prepend += " failed to verify: ";
1261 PrependToLastFailMessage(prepend);
jeffhaoba5ebb92011-08-25 17:24:37 -07001262 return false;
1263 }
jeffhaobdb76512011-09-07 11:43:16 -07001264 /* Clear "changed" and mark as visited. */
Ian Rogersd81871c2011-10-03 13:57:23 -07001265 insn_flags_[insn_idx].SetVisited();
1266 insn_flags_[insn_idx].ClearChanged();
jeffhaobdb76512011-09-07 11:43:16 -07001267 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001268
Ian Rogers1c849e52012-06-28 14:00:33 -07001269 if (gDebugVerify) {
jeffhaobdb76512011-09-07 11:43:16 -07001270 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001271 * Scan for dead code. There's nothing "evil" about dead code
jeffhaobdb76512011-09-07 11:43:16 -07001272 * (besides the wasted space), but it indicates a flaw somewhere
1273 * down the line, possibly in the verifier.
1274 *
1275 * If we've substituted "always throw" instructions into the stream,
1276 * we are almost certainly going to have some dead code.
1277 */
1278 int dead_start = -1;
Ian Rogersd81871c2011-10-03 13:57:23 -07001279 uint32_t insn_idx = 0;
1280 for (; insn_idx < insns_size; insn_idx += insn_flags_[insn_idx].GetLengthInCodeUnits()) {
jeffhaobdb76512011-09-07 11:43:16 -07001281 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001282 * Switch-statement data doesn't get "visited" by scanner. It
jeffhaobdb76512011-09-07 11:43:16 -07001283 * may or may not be preceded by a padding NOP (for alignment).
1284 */
1285 if (insns[insn_idx] == Instruction::kPackedSwitchSignature ||
1286 insns[insn_idx] == Instruction::kSparseSwitchSignature ||
1287 insns[insn_idx] == Instruction::kArrayDataSignature ||
Elliott Hughes380aaa72012-07-09 14:33:15 -07001288 (insns[insn_idx] == Instruction::NOP && (insn_idx + 1 < insns_size) &&
jeffhaobdb76512011-09-07 11:43:16 -07001289 (insns[insn_idx + 1] == Instruction::kPackedSwitchSignature ||
1290 insns[insn_idx + 1] == Instruction::kSparseSwitchSignature ||
1291 insns[insn_idx + 1] == Instruction::kArrayDataSignature))) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001292 insn_flags_[insn_idx].SetVisited();
jeffhaobdb76512011-09-07 11:43:16 -07001293 }
1294
Ian Rogersd81871c2011-10-03 13:57:23 -07001295 if (!insn_flags_[insn_idx].IsVisited()) {
jeffhaobdb76512011-09-07 11:43:16 -07001296 if (dead_start < 0)
1297 dead_start = insn_idx;
1298 } else if (dead_start >= 0) {
Elliott Hughes398f64b2012-03-26 18:05:48 -07001299 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start) << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaobdb76512011-09-07 11:43:16 -07001300 dead_start = -1;
1301 }
1302 }
1303 if (dead_start >= 0) {
Elliott Hughes398f64b2012-03-26 18:05:48 -07001304 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start) << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaoba5ebb92011-08-25 17:24:37 -07001305 }
Ian Rogersc9e463c2013-06-05 16:52:26 -07001306 // To dump the state of the verify after a method, do something like:
1307 // if (PrettyMethod(dex_method_idx_, *dex_file_) ==
1308 // "boolean java.lang.String.equals(java.lang.Object)") {
1309 // LOG(INFO) << info_messages_.str();
1310 // }
jeffhaoba5ebb92011-08-25 17:24:37 -07001311 }
jeffhaobdb76512011-09-07 11:43:16 -07001312 return true;
1313}
1314
Ian Rogers776ac1f2012-04-13 23:36:36 -07001315bool MethodVerifier::CodeFlowVerifyInstruction(uint32_t* start_guess) {
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001316 // If we're doing FindLocksAtDexPc, check whether we're at the dex pc we care about.
1317 // We want the state _before_ the instruction, for the case where the dex pc we're
1318 // interested in is itself a monitor-enter instruction (which is a likely place
1319 // for a thread to be suspended).
1320 if (monitor_enter_dex_pcs_ != NULL && work_insn_idx_ == interesting_dex_pc_) {
Elliott Hughes4993bbc2013-01-10 15:41:25 -08001321 monitor_enter_dex_pcs_->clear(); // The new work line is more accurate than the previous one.
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001322 for (size_t i = 0; i < work_line_->GetMonitorEnterCount(); ++i) {
1323 monitor_enter_dex_pcs_->push_back(work_line_->GetMonitorEnterDexPc(i));
1324 }
1325 }
1326
jeffhaobdb76512011-09-07 11:43:16 -07001327 /*
1328 * Once we finish decoding the instruction, we need to figure out where
jeffhaod1f0fde2011-09-08 17:25:33 -07001329 * we can go from here. There are three possible ways to transfer
jeffhaobdb76512011-09-07 11:43:16 -07001330 * control to another statement:
1331 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001332 * (1) Continue to the next instruction. Applies to all but
jeffhaobdb76512011-09-07 11:43:16 -07001333 * unconditional branches, method returns, and exception throws.
jeffhaod1f0fde2011-09-08 17:25:33 -07001334 * (2) Branch to one or more possible locations. Applies to branches
jeffhaobdb76512011-09-07 11:43:16 -07001335 * and switch statements.
jeffhaod1f0fde2011-09-08 17:25:33 -07001336 * (3) Exception handlers. Applies to any instruction that can
jeffhaobdb76512011-09-07 11:43:16 -07001337 * throw an exception that is handled by an encompassing "try"
1338 * block.
1339 *
1340 * We can also return, in which case there is no successor instruction
1341 * from this point.
1342 *
Elliott Hughesadb8c672012-03-06 16:49:32 -08001343 * The behavior can be determined from the opcode flags.
jeffhaobdb76512011-09-07 11:43:16 -07001344 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001345 const uint16_t* insns = code_item_->insns_ + work_insn_idx_;
1346 const Instruction* inst = Instruction::At(insns);
Ian Rogersa75a0132012-09-28 11:41:42 -07001347 int opcode_flags = Instruction::FlagsOf(inst->Opcode());
jeffhaobdb76512011-09-07 11:43:16 -07001348
jeffhaobdb76512011-09-07 11:43:16 -07001349 int32_t branch_target = 0;
jeffhaobdb76512011-09-07 11:43:16 -07001350 bool just_set_result = false;
Ian Rogers2c8a8572011-10-24 17:11:36 -07001351 if (gDebugVerify) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001352 // Generate processing back trace to debug verifier
Elliott Hughesc073b072012-05-24 19:29:17 -07001353 LogVerifyInfo() << "Processing " << inst->DumpString(dex_file_) << "\n"
1354 << *work_line_.get() << "\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001355 }
jeffhaobdb76512011-09-07 11:43:16 -07001356
1357 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001358 * Make a copy of the previous register state. If the instruction
jeffhaobdb76512011-09-07 11:43:16 -07001359 * can throw an exception, we will copy/merge this into the "catch"
1360 * address rather than work_line, because we don't want the result
1361 * from the "successful" code path (e.g. a check-cast that "improves"
1362 * a type) to be visible to the exception handler.
1363 */
Ian Rogers776ac1f2012-04-13 23:36:36 -07001364 if ((opcode_flags & Instruction::kThrow) != 0 && CurrentInsnFlags()->IsInTry()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001365 saved_line_->CopyFromLine(work_line_.get());
jeffhaobdb76512011-09-07 11:43:16 -07001366 } else {
1367#ifndef NDEBUG
Ian Rogersd81871c2011-10-03 13:57:23 -07001368 saved_line_->FillWithGarbage();
jeffhaobdb76512011-09-07 11:43:16 -07001369#endif
1370 }
1371
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07001372
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001373 // We need to ensure the work line is consistent while performing validation. When we spot a
1374 // peephole pattern we compute a new line for either the fallthrough instruction or the
1375 // branch target.
1376 UniquePtr<RegisterLine> branch_line;
1377 UniquePtr<RegisterLine> fallthrough_line;
1378
Sebastien Hertz5243e912013-05-21 10:55:07 +02001379 switch (inst->Opcode()) {
jeffhaobdb76512011-09-07 11:43:16 -07001380 case Instruction::NOP:
1381 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001382 * A "pure" NOP has no effect on anything. Data tables start with
jeffhaobdb76512011-09-07 11:43:16 -07001383 * a signature that looks like a NOP; if we see one of these in
1384 * the course of executing code then we have a problem.
1385 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001386 if (inst->VRegA_10x() != 0) {
jeffhaod5347e02012-03-22 17:25:05 -07001387 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "encountered data table in instruction stream";
jeffhaobdb76512011-09-07 11:43:16 -07001388 }
1389 break;
1390
1391 case Instruction::MOVE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001392 work_line_->CopyRegister1(inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategory1nr);
1393 break;
jeffhaobdb76512011-09-07 11:43:16 -07001394 case Instruction::MOVE_FROM16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001395 work_line_->CopyRegister1(inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategory1nr);
1396 break;
jeffhaobdb76512011-09-07 11:43:16 -07001397 case Instruction::MOVE_16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001398 work_line_->CopyRegister1(inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategory1nr);
jeffhaobdb76512011-09-07 11:43:16 -07001399 break;
1400 case Instruction::MOVE_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001401 work_line_->CopyRegister2(inst->VRegA_12x(), inst->VRegB_12x());
1402 break;
jeffhaobdb76512011-09-07 11:43:16 -07001403 case Instruction::MOVE_WIDE_FROM16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001404 work_line_->CopyRegister2(inst->VRegA_22x(), inst->VRegB_22x());
1405 break;
jeffhaobdb76512011-09-07 11:43:16 -07001406 case Instruction::MOVE_WIDE_16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001407 work_line_->CopyRegister2(inst->VRegA_32x(), inst->VRegB_32x());
jeffhaobdb76512011-09-07 11:43:16 -07001408 break;
1409 case Instruction::MOVE_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001410 work_line_->CopyRegister1(inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategoryRef);
1411 break;
jeffhaobdb76512011-09-07 11:43:16 -07001412 case Instruction::MOVE_OBJECT_FROM16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001413 work_line_->CopyRegister1(inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategoryRef);
1414 break;
jeffhaobdb76512011-09-07 11:43:16 -07001415 case Instruction::MOVE_OBJECT_16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001416 work_line_->CopyRegister1(inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategoryRef);
jeffhaobdb76512011-09-07 11:43:16 -07001417 break;
1418
1419 /*
1420 * The move-result instructions copy data out of a "pseudo-register"
jeffhaod1f0fde2011-09-08 17:25:33 -07001421 * with the results from the last method invocation. In practice we
jeffhaobdb76512011-09-07 11:43:16 -07001422 * might want to hold the result in an actual CPU register, so the
1423 * Dalvik spec requires that these only appear immediately after an
1424 * invoke or filled-new-array.
1425 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001426 * These calls invalidate the "result" register. (This is now
jeffhaobdb76512011-09-07 11:43:16 -07001427 * redundant with the reset done below, but it can make the debug info
1428 * easier to read in some cases.)
1429 */
1430 case Instruction::MOVE_RESULT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001431 work_line_->CopyResultRegister1(inst->VRegA_11x(), false);
jeffhaobdb76512011-09-07 11:43:16 -07001432 break;
1433 case Instruction::MOVE_RESULT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001434 work_line_->CopyResultRegister2(inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07001435 break;
1436 case Instruction::MOVE_RESULT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001437 work_line_->CopyResultRegister1(inst->VRegA_11x(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001438 break;
1439
Ian Rogersd81871c2011-10-03 13:57:23 -07001440 case Instruction::MOVE_EXCEPTION: {
jeffhaobdb76512011-09-07 11:43:16 -07001441 /*
jeffhao60f83e32012-02-13 17:16:30 -08001442 * This statement can only appear as the first instruction in an exception handler. We verify
1443 * that as part of extracting the exception type from the catch block list.
jeffhaobdb76512011-09-07 11:43:16 -07001444 */
Ian Rogers28ad40d2011-10-27 15:19:26 -07001445 const RegType& res_type = GetCaughtExceptionType();
Sebastien Hertz5243e912013-05-21 10:55:07 +02001446 work_line_->SetRegisterType(inst->VRegA_11x(), res_type);
jeffhaobdb76512011-09-07 11:43:16 -07001447 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001448 }
jeffhaobdb76512011-09-07 11:43:16 -07001449 case Instruction::RETURN_VOID:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001450 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
1451 if (!GetMethodReturnType().IsConflict()) {
jeffhaod5347e02012-03-22 17:25:05 -07001452 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001453 }
jeffhaobdb76512011-09-07 11:43:16 -07001454 }
1455 break;
1456 case Instruction::RETURN:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001457 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
jeffhaobdb76512011-09-07 11:43:16 -07001458 /* check the method signature */
Ian Rogersd81871c2011-10-03 13:57:23 -07001459 const RegType& return_type = GetMethodReturnType();
1460 if (!return_type.IsCategory1Types()) {
jeffhaod5347e02012-03-22 17:25:05 -07001461 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected non-category 1 return type " << return_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001462 } else {
1463 // Compilers may generate synthetic functions that write byte values into boolean fields.
1464 // Also, it may use integer values for boolean, byte, short, and character return types.
Sebastien Hertz5243e912013-05-21 10:55:07 +02001465 const uint32_t vregA = inst->VRegA_11x();
1466 const RegType& src_type = work_line_->GetRegisterType(vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07001467 bool use_src = ((return_type.IsBoolean() && src_type.IsByte()) ||
1468 ((return_type.IsBoolean() || return_type.IsByte() ||
1469 return_type.IsShort() || return_type.IsChar()) &&
1470 src_type.IsInteger()));
1471 /* check the register contents */
Ian Rogersad0b3a32012-04-16 14:50:24 -07001472 bool success =
Sebastien Hertz5243e912013-05-21 10:55:07 +02001473 work_line_->VerifyRegisterType(vregA, use_src ? src_type : return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001474 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001475 AppendToLastFailMessage(StringPrintf(" return-1nr on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07001476 }
jeffhaobdb76512011-09-07 11:43:16 -07001477 }
1478 }
1479 break;
1480 case Instruction::RETURN_WIDE:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001481 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
jeffhaobdb76512011-09-07 11:43:16 -07001482 /* check the method signature */
Ian Rogersd81871c2011-10-03 13:57:23 -07001483 const RegType& return_type = GetMethodReturnType();
1484 if (!return_type.IsCategory2Types()) {
jeffhaod5347e02012-03-22 17:25:05 -07001485 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-wide not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001486 } else {
1487 /* check the register contents */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001488 const uint32_t vregA = inst->VRegA_11x();
1489 bool success = work_line_->VerifyRegisterType(vregA, return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001490 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001491 AppendToLastFailMessage(StringPrintf(" return-wide on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07001492 }
jeffhaobdb76512011-09-07 11:43:16 -07001493 }
1494 }
1495 break;
1496 case Instruction::RETURN_OBJECT:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001497 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001498 const RegType& return_type = GetMethodReturnType();
1499 if (!return_type.IsReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001500 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-object not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001501 } else {
1502 /* return_type is the *expected* return type, not register value */
1503 DCHECK(!return_type.IsZero());
1504 DCHECK(!return_type.IsUninitializedReference());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001505 const uint32_t vregA = inst->VRegA_11x();
1506 const RegType& reg_type = work_line_->GetRegisterType(vregA);
Ian Rogers9074b992011-10-26 17:41:55 -07001507 // Disallow returning uninitialized values and verify that the reference in vAA is an
1508 // instance of the "return_type"
1509 if (reg_type.IsUninitializedTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001510 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "returning uninitialized object '" << reg_type << "'";
Ian Rogers9074b992011-10-26 17:41:55 -07001511 } else if (!return_type.IsAssignableFrom(reg_type)) {
jeffhao666d9b42012-06-12 11:36:38 -07001512 Fail(reg_type.IsUnresolvedTypes() ? VERIFY_ERROR_BAD_CLASS_SOFT : VERIFY_ERROR_BAD_CLASS_HARD)
1513 << "returning '" << reg_type << "', but expected from declaration '" << return_type << "'";
jeffhaobdb76512011-09-07 11:43:16 -07001514 }
1515 }
1516 }
1517 break;
1518
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001519 /* could be boolean, int, float, or a null reference */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001520 case Instruction::CONST_4: {
1521 int32_t val = static_cast<int32_t>(inst->VRegB_11n() << 28) >> 28;
1522 work_line_->SetRegisterType(inst->VRegA_11n(), reg_types_.FromCat1Const(val, true));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001523 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02001524 }
1525 case Instruction::CONST_16: {
1526 int16_t val = static_cast<int16_t>(inst->VRegB_21s());
1527 work_line_->SetRegisterType(inst->VRegA_21s(), reg_types_.FromCat1Const(val, true));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001528 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02001529 }
jeffhaobdb76512011-09-07 11:43:16 -07001530 case Instruction::CONST:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001531 work_line_->SetRegisterType(inst->VRegA_31i(),
1532 reg_types_.FromCat1Const(inst->VRegB_31i(), true));
jeffhaobdb76512011-09-07 11:43:16 -07001533 break;
1534 case Instruction::CONST_HIGH16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001535 work_line_->SetRegisterType(inst->VRegA_21h(),
1536 reg_types_.FromCat1Const(inst->VRegB_21h() << 16, true));
jeffhaobdb76512011-09-07 11:43:16 -07001537 break;
jeffhaobdb76512011-09-07 11:43:16 -07001538 /* could be long or double; resolved upon use */
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001539 case Instruction::CONST_WIDE_16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001540 int64_t val = static_cast<int16_t>(inst->VRegB_21s());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001541 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1542 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001543 work_line_->SetRegisterTypeWide(inst->VRegA_21s(), lo, hi);
jeffhaobdb76512011-09-07 11:43:16 -07001544 break;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001545 }
1546 case Instruction::CONST_WIDE_32: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001547 int64_t val = static_cast<int32_t>(inst->VRegB_31i());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001548 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1549 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001550 work_line_->SetRegisterTypeWide(inst->VRegA_31i(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001551 break;
1552 }
1553 case Instruction::CONST_WIDE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001554 int64_t val = inst->VRegB_51l();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001555 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1556 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001557 work_line_->SetRegisterTypeWide(inst->VRegA_51l(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001558 break;
1559 }
1560 case Instruction::CONST_WIDE_HIGH16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001561 int64_t val = static_cast<uint64_t>(inst->VRegB_21h()) << 48;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001562 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1563 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001564 work_line_->SetRegisterTypeWide(inst->VRegA_21h(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001565 break;
1566 }
jeffhaobdb76512011-09-07 11:43:16 -07001567 case Instruction::CONST_STRING:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001568 work_line_->SetRegisterType(inst->VRegA_21c(), reg_types_.JavaLangString());
1569 break;
jeffhaobdb76512011-09-07 11:43:16 -07001570 case Instruction::CONST_STRING_JUMBO:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001571 work_line_->SetRegisterType(inst->VRegA_31c(), reg_types_.JavaLangString());
jeffhaobdb76512011-09-07 11:43:16 -07001572 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001573 case Instruction::CONST_CLASS: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001574 // Get type from instruction if unresolved then we need an access check
1575 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Sebastien Hertz5243e912013-05-21 10:55:07 +02001576 const RegType& res_type = ResolveClassAndCheckAccess(inst->VRegB_21c());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001577 // Register holds class, ie its type is class, on error it will hold Conflict.
Sebastien Hertz5243e912013-05-21 10:55:07 +02001578 work_line_->SetRegisterType(inst->VRegA_21c(),
Ian Rogersb4903572012-10-11 11:52:56 -07001579 res_type.IsConflict() ? res_type
1580 : reg_types_.JavaLangClass(true));
jeffhaobdb76512011-09-07 11:43:16 -07001581 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001582 }
jeffhaobdb76512011-09-07 11:43:16 -07001583 case Instruction::MONITOR_ENTER:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001584 work_line_->PushMonitor(inst->VRegA_11x(), work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07001585 break;
1586 case Instruction::MONITOR_EXIT:
1587 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001588 * monitor-exit instructions are odd. They can throw exceptions,
jeffhaobdb76512011-09-07 11:43:16 -07001589 * but when they do they act as if they succeeded and the PC is
jeffhaod1f0fde2011-09-08 17:25:33 -07001590 * pointing to the following instruction. (This behavior goes back
jeffhaobdb76512011-09-07 11:43:16 -07001591 * to the need to handle asynchronous exceptions, a now-deprecated
1592 * feature that Dalvik doesn't support.)
1593 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001594 * In practice we don't need to worry about this. The only
jeffhaobdb76512011-09-07 11:43:16 -07001595 * exceptions that can be thrown from monitor-exit are for a
jeffhaod1f0fde2011-09-08 17:25:33 -07001596 * null reference and -exit without a matching -enter. If the
jeffhaobdb76512011-09-07 11:43:16 -07001597 * structured locking checks are working, the former would have
1598 * failed on the -enter instruction, and the latter is impossible.
1599 *
1600 * This is fortunate, because issue 3221411 prevents us from
1601 * chasing the "can throw" path when monitor verification is
jeffhaod1f0fde2011-09-08 17:25:33 -07001602 * enabled. If we can fully verify the locking we can ignore
jeffhaobdb76512011-09-07 11:43:16 -07001603 * some catch blocks (which will show up as "dead" code when
1604 * we skip them here); if we can't, then the code path could be
1605 * "live" so we still need to check it.
1606 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001607 opcode_flags &= ~Instruction::kThrow;
Sebastien Hertz5243e912013-05-21 10:55:07 +02001608 work_line_->PopMonitor(inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07001609 break;
1610
Ian Rogers28ad40d2011-10-27 15:19:26 -07001611 case Instruction::CHECK_CAST:
Ian Rogersd81871c2011-10-03 13:57:23 -07001612 case Instruction::INSTANCE_OF: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001613 /*
1614 * If this instruction succeeds, we will "downcast" register vA to the type in vB. (This
1615 * could be a "upcast" -- not expected, so we don't try to address it.)
1616 *
1617 * If it fails, an exception is thrown, which we deal with later by ignoring the update to
Elliott Hughesadb8c672012-03-06 16:49:32 -08001618 * dec_insn.vA when branching to a handler.
Ian Rogers28ad40d2011-10-27 15:19:26 -07001619 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001620 const bool is_checkcast = (inst->Opcode() == Instruction::CHECK_CAST);
1621 const uint32_t type_idx = (is_checkcast) ? inst->VRegB_21c() : inst->VRegC_22c();
1622 const RegType& res_type = ResolveClassAndCheckAccess(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001623 if (res_type.IsConflict()) {
1624 DCHECK_NE(failures_.size(), 0U);
1625 if (!is_checkcast) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001626 work_line_->SetRegisterType(inst->VRegA_22c(), reg_types_.Boolean());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001627 }
1628 break; // bad class
Ian Rogers9f1ab122011-12-12 08:52:43 -08001629 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001630 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Sebastien Hertz5243e912013-05-21 10:55:07 +02001631 uint32_t orig_type_reg = (is_checkcast) ? inst->VRegA_21c() : inst->VRegB_22c();
1632 const RegType& orig_type = work_line_->GetRegisterType(orig_type_reg);
Ian Rogers28ad40d2011-10-27 15:19:26 -07001633 if (!res_type.IsNonZeroReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001634 if (is_checkcast) {
1635 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on unexpected class " << res_type;
1636 } else {
1637 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on unexpected class " << res_type;
1638 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001639 } else if (!orig_type.IsReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001640 if (is_checkcast) {
1641 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on non-reference in v" << orig_type_reg;
1642 } else {
1643 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on non-reference in v" << orig_type_reg;
1644 }
jeffhao2a8a90e2011-09-26 14:25:31 -07001645 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001646 if (is_checkcast) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001647 work_line_->SetRegisterType(inst->VRegA_21c(), res_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001648 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001649 work_line_->SetRegisterType(inst->VRegA_22c(), reg_types_.Boolean());
jeffhaobdb76512011-09-07 11:43:16 -07001650 }
jeffhaobdb76512011-09-07 11:43:16 -07001651 }
jeffhao2a8a90e2011-09-26 14:25:31 -07001652 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001653 }
1654 case Instruction::ARRAY_LENGTH: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001655 const RegType& res_type = work_line_->GetRegisterType(inst->VRegB_12x());
Ian Rogers28ad40d2011-10-27 15:19:26 -07001656 if (res_type.IsReferenceTypes()) {
Ian Rogers89310de2012-02-01 13:47:30 -08001657 if (!res_type.IsArrayTypes() && !res_type.IsZero()) { // ie not an array or null
jeffhaod5347e02012-03-22 17:25:05 -07001658 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001659 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001660 work_line_->SetRegisterType(inst->VRegA_12x(), reg_types_.Integer());
Ian Rogersd81871c2011-10-03 13:57:23 -07001661 }
1662 }
1663 break;
1664 }
1665 case Instruction::NEW_INSTANCE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001666 const RegType& res_type = ResolveClassAndCheckAccess(inst->VRegB_21c());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001667 if (res_type.IsConflict()) {
1668 DCHECK_NE(failures_.size(), 0U);
1669 break; // bad class
jeffhao8cd6dda2012-02-22 10:15:34 -08001670 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001671 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
1672 // can't create an instance of an interface or abstract class */
1673 if (!res_type.IsInstantiableTypes()) {
1674 Fail(VERIFY_ERROR_INSTANTIATION)
1675 << "new-instance on primitive, interface or abstract class" << res_type;
Ian Rogers08f753d2012-08-24 14:35:25 -07001676 // Soft failure so carry on to set register type.
Ian Rogersd81871c2011-10-03 13:57:23 -07001677 }
Ian Rogers08f753d2012-08-24 14:35:25 -07001678 const RegType& uninit_type = reg_types_.Uninitialized(res_type, work_insn_idx_);
1679 // Any registers holding previous allocations from this address that have not yet been
1680 // initialized must be marked invalid.
1681 work_line_->MarkUninitRefsAsInvalid(uninit_type);
1682 // add the new uninitialized reference to the register state
Sebastien Hertz5243e912013-05-21 10:55:07 +02001683 work_line_->SetRegisterType(inst->VRegA_21c(), uninit_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001684 break;
1685 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08001686 case Instruction::NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001687 VerifyNewArray(inst, false, false);
jeffhaobdb76512011-09-07 11:43:16 -07001688 break;
1689 case Instruction::FILLED_NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001690 VerifyNewArray(inst, true, false);
Ian Rogers0c4a5062012-02-03 15:18:59 -08001691 just_set_result = true; // Filled new array sets result register
jeffhaobdb76512011-09-07 11:43:16 -07001692 break;
Ian Rogers0c4a5062012-02-03 15:18:59 -08001693 case Instruction::FILLED_NEW_ARRAY_RANGE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001694 VerifyNewArray(inst, true, true);
Ian Rogers0c4a5062012-02-03 15:18:59 -08001695 just_set_result = true; // Filled new array range sets result register
1696 break;
jeffhaobdb76512011-09-07 11:43:16 -07001697 case Instruction::CMPL_FLOAT:
1698 case Instruction::CMPG_FLOAT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001699 if (!work_line_->VerifyRegisterType(inst->VRegB_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08001700 break;
1701 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02001702 if (!work_line_->VerifyRegisterType(inst->VRegC_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08001703 break;
1704 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02001705 work_line_->SetRegisterType(inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001706 break;
1707 case Instruction::CMPL_DOUBLE:
1708 case Instruction::CMPG_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001709 if (!work_line_->VerifyRegisterTypeWide(inst->VRegB_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001710 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001711 break;
1712 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02001713 if (!work_line_->VerifyRegisterTypeWide(inst->VRegC_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001714 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001715 break;
1716 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02001717 work_line_->SetRegisterType(inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001718 break;
1719 case Instruction::CMP_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001720 if (!work_line_->VerifyRegisterTypeWide(inst->VRegB_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001721 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001722 break;
1723 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02001724 if (!work_line_->VerifyRegisterTypeWide(inst->VRegC_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001725 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001726 break;
1727 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02001728 work_line_->SetRegisterType(inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001729 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001730 case Instruction::THROW: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001731 const RegType& res_type = work_line_->GetRegisterType(inst->VRegA_11x());
Ian Rogersb4903572012-10-11 11:52:56 -07001732 if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(res_type)) {
jeffhaod5347e02012-03-22 17:25:05 -07001733 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "thrown class " << res_type << " not instanceof Throwable";
jeffhaobdb76512011-09-07 11:43:16 -07001734 }
1735 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001736 }
jeffhaobdb76512011-09-07 11:43:16 -07001737 case Instruction::GOTO:
1738 case Instruction::GOTO_16:
1739 case Instruction::GOTO_32:
1740 /* no effect on or use of registers */
1741 break;
1742
1743 case Instruction::PACKED_SWITCH:
1744 case Instruction::SPARSE_SWITCH:
1745 /* verify that vAA is an integer, or can be converted to one */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001746 work_line_->VerifyRegisterType(inst->VRegA_31t(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001747 break;
1748
Ian Rogersd81871c2011-10-03 13:57:23 -07001749 case Instruction::FILL_ARRAY_DATA: {
1750 /* Similar to the verification done for APUT */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001751 const RegType& array_type = work_line_->GetRegisterType(inst->VRegA_31t());
Ian Rogers89310de2012-02-01 13:47:30 -08001752 /* array_type can be null if the reg type is Zero */
1753 if (!array_type.IsZero()) {
jeffhao457cc512012-02-02 16:55:13 -08001754 if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001755 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with array type " << array_type;
Ian Rogers89310de2012-02-01 13:47:30 -08001756 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001757 const RegType& component_type = reg_types_.GetComponentType(array_type, class_loader_);
1758 DCHECK(!component_type.IsConflict());
jeffhao457cc512012-02-02 16:55:13 -08001759 if (component_type.IsNonZeroReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001760 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with component type "
1761 << component_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001762 } else {
jeffhao457cc512012-02-02 16:55:13 -08001763 // Now verify if the element width in the table matches the element width declared in
1764 // the array
1765 const uint16_t* array_data = insns + (insns[1] | (((int32_t) insns[2]) << 16));
1766 if (array_data[0] != Instruction::kArrayDataSignature) {
jeffhaod5347e02012-03-22 17:25:05 -07001767 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid magic for array-data";
jeffhao457cc512012-02-02 16:55:13 -08001768 } else {
1769 size_t elem_width = Primitive::ComponentSize(component_type.GetPrimitiveType());
1770 // Since we don't compress the data in Dex, expect to see equal width of data stored
1771 // in the table and expected from the array class.
1772 if (array_data[1] != elem_width) {
jeffhaod5347e02012-03-22 17:25:05 -07001773 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-data size mismatch (" << array_data[1]
1774 << " vs " << elem_width << ")";
jeffhao457cc512012-02-02 16:55:13 -08001775 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001776 }
1777 }
jeffhaobdb76512011-09-07 11:43:16 -07001778 }
1779 }
1780 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001781 }
jeffhaobdb76512011-09-07 11:43:16 -07001782 case Instruction::IF_EQ:
Ian Rogersd81871c2011-10-03 13:57:23 -07001783 case Instruction::IF_NE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001784 const RegType& reg_type1 = work_line_->GetRegisterType(inst->VRegA_22t());
1785 const RegType& reg_type2 = work_line_->GetRegisterType(inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07001786 bool mismatch = false;
1787 if (reg_type1.IsZero()) { // zero then integral or reference expected
1788 mismatch = !reg_type2.IsReferenceTypes() && !reg_type2.IsIntegralTypes();
1789 } else if (reg_type1.IsReferenceTypes()) { // both references?
1790 mismatch = !reg_type2.IsReferenceTypes();
1791 } else { // both integral?
1792 mismatch = !reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes();
1793 }
1794 if (mismatch) {
jeffhaod5347e02012-03-22 17:25:05 -07001795 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to if-eq/if-ne (" << reg_type1 << "," << reg_type2
1796 << ") must both be references or integral";
jeffhaobdb76512011-09-07 11:43:16 -07001797 }
1798 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001799 }
jeffhaobdb76512011-09-07 11:43:16 -07001800 case Instruction::IF_LT:
1801 case Instruction::IF_GE:
1802 case Instruction::IF_GT:
Ian Rogersd81871c2011-10-03 13:57:23 -07001803 case Instruction::IF_LE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001804 const RegType& reg_type1 = work_line_->GetRegisterType(inst->VRegA_22t());
1805 const RegType& reg_type2 = work_line_->GetRegisterType(inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07001806 if (!reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001807 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to 'if' (" << reg_type1 << ","
1808 << reg_type2 << ") must be integral";
jeffhaobdb76512011-09-07 11:43:16 -07001809 }
1810 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001811 }
jeffhaobdb76512011-09-07 11:43:16 -07001812 case Instruction::IF_EQZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07001813 case Instruction::IF_NEZ: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001814 const RegType& reg_type = work_line_->GetRegisterType(inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07001815 if (!reg_type.IsReferenceTypes() && !reg_type.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001816 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type << " unexpected as arg to if-eqz/if-nez";
Ian Rogersd81871c2011-10-03 13:57:23 -07001817 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001818
1819 // Find previous instruction - its existence is a precondition to peephole optimization.
Ian Rogers9b360392013-06-06 14:45:07 -07001820 uint32_t instance_of_idx = 0;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001821 if (0 != work_insn_idx_) {
Ian Rogers9b360392013-06-06 14:45:07 -07001822 instance_of_idx = work_insn_idx_ - 1;
1823 while(0 != instance_of_idx && !insn_flags_[instance_of_idx].IsOpcode()) {
1824 instance_of_idx--;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001825 }
Ian Rogers9b360392013-06-06 14:45:07 -07001826 CHECK(insn_flags_[instance_of_idx].IsOpcode());
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001827 } else {
1828 break;
1829 }
1830
Ian Rogers9b360392013-06-06 14:45:07 -07001831 const Instruction* instance_of_inst = Instruction::At(code_item_->insns_ + instance_of_idx);
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001832
1833 /* Check for peep-hole pattern of:
1834 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07001835 * instance-of vX, vY, T;
1836 * ifXXX vX, label ;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001837 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07001838 * label:
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001839 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07001840 * and sharpen the type of vY to be type T.
1841 * Note, this pattern can't be if:
1842 * - if there are other branches to this branch,
1843 * - when vX == vY.
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001844 */
Ian Rogersfae370a2013-06-05 08:33:27 -07001845 if (!CurrentInsnFlags()->IsBranchTarget() &&
Ian Rogers9b360392013-06-06 14:45:07 -07001846 (Instruction::INSTANCE_OF == instance_of_inst->Opcode()) &&
1847 (inst->VRegA_21t() == instance_of_inst->VRegA_22c()) &&
1848 (instance_of_inst->VRegA_22c() != instance_of_inst->VRegB_22c())) {
Ian Rogersfae370a2013-06-05 08:33:27 -07001849 // Check that the we are not attempting conversion to interface types,
1850 // which is not done because of the multiple inheritance implications.
Ian Rogers9b360392013-06-06 14:45:07 -07001851 const RegType& cast_type = ResolveClassAndCheckAccess(instance_of_inst->VRegC_22c());
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001852
Ian Rogersfae370a2013-06-05 08:33:27 -07001853 if(!cast_type.IsUnresolvedTypes() && !cast_type.GetClass()->IsInterface()) {
Ian Rogers9b360392013-06-06 14:45:07 -07001854 RegisterLine* update_line = new RegisterLine(code_item_->registers_size_, this);
Ian Rogersfae370a2013-06-05 08:33:27 -07001855 if (inst->Opcode() == Instruction::IF_EQZ) {
Ian Rogers9b360392013-06-06 14:45:07 -07001856 fallthrough_line.reset(update_line);
Ian Rogersfae370a2013-06-05 08:33:27 -07001857 } else {
Ian Rogers9b360392013-06-06 14:45:07 -07001858 branch_line.reset(update_line);
1859 }
1860 update_line->CopyFromLine(work_line_.get());
1861 update_line->SetRegisterType(instance_of_inst->VRegB_22c(), cast_type);
1862 if (!insn_flags_[instance_of_idx].IsBranchTarget() && 0 != instance_of_idx) {
1863 // See if instance-of was preceded by a move-object operation, common due to the small
1864 // register encoding space of instance-of, and propagate type information to the source
1865 // of the move-object.
1866 uint32_t move_idx = instance_of_idx - 1;
1867 while(0 != move_idx && !insn_flags_[move_idx].IsOpcode()) {
1868 move_idx--;
1869 }
1870 CHECK(insn_flags_[move_idx].IsOpcode());
1871 const Instruction* move_inst = Instruction::At(code_item_->insns_ + move_idx);
1872 switch (move_inst->Opcode()) {
1873 case Instruction::MOVE_OBJECT:
1874 if (move_inst->VRegA_12x() == instance_of_inst->VRegB_22c()) {
1875 update_line->SetRegisterType(move_inst->VRegB_12x(), cast_type);
1876 }
1877 break;
1878 case Instruction::MOVE_OBJECT_FROM16:
1879 if (move_inst->VRegA_22x() == instance_of_inst->VRegB_22c()) {
1880 update_line->SetRegisterType(move_inst->VRegB_22x(), cast_type);
1881 }
1882 break;
1883 case Instruction::MOVE_OBJECT_16:
1884 if (move_inst->VRegA_32x() == instance_of_inst->VRegB_22c()) {
1885 update_line->SetRegisterType(move_inst->VRegB_32x(), cast_type);
1886 }
1887 break;
1888 default:
1889 break;
1890 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001891 }
1892 }
1893 }
1894
jeffhaobdb76512011-09-07 11:43:16 -07001895 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001896 }
jeffhaobdb76512011-09-07 11:43:16 -07001897 case Instruction::IF_LTZ:
1898 case Instruction::IF_GEZ:
1899 case Instruction::IF_GTZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07001900 case Instruction::IF_LEZ: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001901 const RegType& reg_type = work_line_->GetRegisterType(inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07001902 if (!reg_type.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001903 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
1904 << " unexpected as arg to if-ltz/if-gez/if-gtz/if-lez";
Ian Rogersd81871c2011-10-03 13:57:23 -07001905 }
jeffhaobdb76512011-09-07 11:43:16 -07001906 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001907 }
jeffhaobdb76512011-09-07 11:43:16 -07001908 case Instruction::AGET_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001909 VerifyAGet(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001910 break;
jeffhaobdb76512011-09-07 11:43:16 -07001911 case Instruction::AGET_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001912 VerifyAGet(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001913 break;
jeffhaobdb76512011-09-07 11:43:16 -07001914 case Instruction::AGET_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001915 VerifyAGet(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001916 break;
jeffhaobdb76512011-09-07 11:43:16 -07001917 case Instruction::AGET_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001918 VerifyAGet(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001919 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001920 case Instruction::AGET:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001921 VerifyAGet(inst, reg_types_.Integer(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001922 break;
jeffhaobdb76512011-09-07 11:43:16 -07001923 case Instruction::AGET_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001924 VerifyAGet(inst, reg_types_.LongLo(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001925 break;
1926 case Instruction::AGET_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001927 VerifyAGet(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07001928 break;
1929
Ian Rogersd81871c2011-10-03 13:57:23 -07001930 case Instruction::APUT_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001931 VerifyAPut(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001932 break;
1933 case Instruction::APUT_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001934 VerifyAPut(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001935 break;
1936 case Instruction::APUT_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001937 VerifyAPut(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001938 break;
1939 case Instruction::APUT_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001940 VerifyAPut(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001941 break;
1942 case Instruction::APUT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001943 VerifyAPut(inst, reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001944 break;
1945 case Instruction::APUT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001946 VerifyAPut(inst, reg_types_.LongLo(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001947 break;
1948 case Instruction::APUT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001949 VerifyAPut(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07001950 break;
1951
jeffhaobdb76512011-09-07 11:43:16 -07001952 case Instruction::IGET_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001953 VerifyISGet(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001954 break;
jeffhaobdb76512011-09-07 11:43:16 -07001955 case Instruction::IGET_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001956 VerifyISGet(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001957 break;
jeffhaobdb76512011-09-07 11:43:16 -07001958 case Instruction::IGET_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001959 VerifyISGet(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001960 break;
jeffhaobdb76512011-09-07 11:43:16 -07001961 case Instruction::IGET_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001962 VerifyISGet(inst, reg_types_.Short(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001963 break;
1964 case Instruction::IGET:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001965 VerifyISGet(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07001966 break;
1967 case Instruction::IGET_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001968 VerifyISGet(inst, reg_types_.LongLo(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07001969 break;
1970 case Instruction::IGET_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001971 VerifyISGet(inst, reg_types_.JavaLangObject(false), false, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001972 break;
jeffhaobdb76512011-09-07 11:43:16 -07001973
Ian Rogersd81871c2011-10-03 13:57:23 -07001974 case Instruction::IPUT_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001975 VerifyISPut(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001976 break;
1977 case Instruction::IPUT_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001978 VerifyISPut(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001979 break;
1980 case Instruction::IPUT_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001981 VerifyISPut(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001982 break;
1983 case Instruction::IPUT_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001984 VerifyISPut(inst, reg_types_.Short(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07001985 break;
1986 case Instruction::IPUT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001987 VerifyISPut(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07001988 break;
1989 case Instruction::IPUT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001990 VerifyISPut(inst, reg_types_.LongLo(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001991 break;
jeffhaobdb76512011-09-07 11:43:16 -07001992 case Instruction::IPUT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001993 VerifyISPut(inst, reg_types_.JavaLangObject(false), false, false);
jeffhaobdb76512011-09-07 11:43:16 -07001994 break;
1995
jeffhaobdb76512011-09-07 11:43:16 -07001996 case Instruction::SGET_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001997 VerifyISGet(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001998 break;
jeffhaobdb76512011-09-07 11:43:16 -07001999 case Instruction::SGET_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002000 VerifyISGet(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002001 break;
jeffhaobdb76512011-09-07 11:43:16 -07002002 case Instruction::SGET_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002003 VerifyISGet(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002004 break;
jeffhaobdb76512011-09-07 11:43:16 -07002005 case Instruction::SGET_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002006 VerifyISGet(inst, reg_types_.Short(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002007 break;
2008 case Instruction::SGET:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002009 VerifyISGet(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002010 break;
2011 case Instruction::SGET_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002012 VerifyISGet(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002013 break;
2014 case Instruction::SGET_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002015 VerifyISGet(inst, reg_types_.JavaLangObject(false), false, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002016 break;
2017
2018 case Instruction::SPUT_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002019 VerifyISPut(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002020 break;
2021 case Instruction::SPUT_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002022 VerifyISPut(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002023 break;
2024 case Instruction::SPUT_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002025 VerifyISPut(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002026 break;
2027 case Instruction::SPUT_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002028 VerifyISPut(inst, reg_types_.Short(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002029 break;
2030 case Instruction::SPUT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002031 VerifyISPut(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002032 break;
2033 case Instruction::SPUT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002034 VerifyISPut(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002035 break;
2036 case Instruction::SPUT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002037 VerifyISPut(inst, reg_types_.JavaLangObject(false), false, true);
jeffhaobdb76512011-09-07 11:43:16 -07002038 break;
2039
2040 case Instruction::INVOKE_VIRTUAL:
2041 case Instruction::INVOKE_VIRTUAL_RANGE:
2042 case Instruction::INVOKE_SUPER:
Ian Rogersd81871c2011-10-03 13:57:23 -07002043 case Instruction::INVOKE_SUPER_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002044 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE ||
2045 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
2046 bool is_super = (inst->Opcode() == Instruction::INVOKE_SUPER ||
2047 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
2048 mirror::AbstractMethod* called_method = VerifyInvocationArgs(inst, METHOD_VIRTUAL,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002049 is_range, is_super);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002050 const char* descriptor;
2051 if (called_method == NULL) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002052 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002053 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2054 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
2055 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2056 } else {
2057 descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
jeffhaobdb76512011-09-07 11:43:16 -07002058 }
Ian Rogersb4903572012-10-11 11:52:56 -07002059 const RegType& return_type = reg_types_.FromDescriptor(class_loader_, descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002060 if (!return_type.IsLowHalf()) {
2061 work_line_->SetResultRegisterType(return_type);
2062 } else {
2063 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2064 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002065 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002066 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002067 }
jeffhaobdb76512011-09-07 11:43:16 -07002068 case Instruction::INVOKE_DIRECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002069 case Instruction::INVOKE_DIRECT_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002070 bool is_range = (inst->Opcode() == Instruction::INVOKE_DIRECT_RANGE);
2071 mirror::AbstractMethod* called_method = VerifyInvocationArgs(inst, METHOD_DIRECT,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002072 is_range, false);
Ian Rogers46685432012-06-03 22:26:43 -07002073 const char* return_type_descriptor;
2074 bool is_constructor;
2075 if (called_method == NULL) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002076 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogers46685432012-06-03 22:26:43 -07002077 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2078 is_constructor = StringPiece(dex_file_->GetMethodName(method_id)) == "<init>";
2079 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
2080 return_type_descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2081 } else {
2082 is_constructor = called_method->IsConstructor();
2083 return_type_descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
2084 }
2085 if (is_constructor) {
jeffhaobdb76512011-09-07 11:43:16 -07002086 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002087 * Some additional checks when calling a constructor. We know from the invocation arg check
2088 * that the "this" argument is an instance of called_method->klass. Now we further restrict
2089 * that to require that called_method->klass is the same as this->klass or this->super,
2090 * allowing the latter only if the "this" argument is the same as the "this" argument to
2091 * this method (which implies that we're in a constructor ourselves).
jeffhaobdb76512011-09-07 11:43:16 -07002092 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002093 const RegType& this_type = work_line_->GetInvocationThis(inst, is_range);
jeffhaob57e9522012-04-26 18:08:21 -07002094 if (this_type.IsConflict()) // failure.
2095 break;
jeffhaobdb76512011-09-07 11:43:16 -07002096
jeffhaob57e9522012-04-26 18:08:21 -07002097 /* no null refs allowed (?) */
2098 if (this_type.IsZero()) {
2099 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unable to initialize null ref";
2100 break;
jeffhao2a8a90e2011-09-26 14:25:31 -07002101 }
jeffhaob57e9522012-04-26 18:08:21 -07002102
2103 /* must be in same class or in superclass */
Ian Rogers46685432012-06-03 22:26:43 -07002104 // const RegType& this_super_klass = this_type.GetSuperClass(&reg_types_);
2105 // TODO: re-enable constructor type verification
2106 // if (this_super_klass.IsConflict()) {
jeffhaob57e9522012-04-26 18:08:21 -07002107 // Unknown super class, fail so we re-check at runtime.
Ian Rogers46685432012-06-03 22:26:43 -07002108 // Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "super class unknown for '" << this_type << "'";
2109 // break;
2110 // }
jeffhaob57e9522012-04-26 18:08:21 -07002111
2112 /* arg must be an uninitialized reference */
2113 if (!this_type.IsUninitializedTypes()) {
2114 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Expected initialization on uninitialized reference "
2115 << this_type;
2116 break;
2117 }
2118
2119 /*
2120 * Replace the uninitialized reference with an initialized one. We need to do this for all
2121 * registers that have the same object instance in them, not just the "this" register.
2122 */
2123 work_line_->MarkRefsAsInitialized(this_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002124 }
Ian Rogersb4903572012-10-11 11:52:56 -07002125 const RegType& return_type = reg_types_.FromDescriptor(class_loader_, return_type_descriptor,
2126 false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002127 if (!return_type.IsLowHalf()) {
2128 work_line_->SetResultRegisterType(return_type);
2129 } else {
2130 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2131 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002132 just_set_result = true;
2133 break;
2134 }
2135 case Instruction::INVOKE_STATIC:
2136 case Instruction::INVOKE_STATIC_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002137 bool is_range = (inst->Opcode() == Instruction::INVOKE_STATIC_RANGE);
2138 mirror::AbstractMethod* called_method = VerifyInvocationArgs(inst, METHOD_STATIC, is_range, false);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002139 const char* descriptor;
2140 if (called_method == NULL) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002141 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002142 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2143 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogers0571d352011-11-03 19:51:38 -07002144 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002145 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002146 descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002147 }
Ian Rogersb4903572012-10-11 11:52:56 -07002148 const RegType& return_type = reg_types_.FromDescriptor(class_loader_, descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002149 if (!return_type.IsLowHalf()) {
2150 work_line_->SetResultRegisterType(return_type);
2151 } else {
2152 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2153 }
jeffhaobdb76512011-09-07 11:43:16 -07002154 just_set_result = true;
2155 }
2156 break;
jeffhaobdb76512011-09-07 11:43:16 -07002157 case Instruction::INVOKE_INTERFACE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002158 case Instruction::INVOKE_INTERFACE_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002159 bool is_range = (inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE);
2160 mirror::AbstractMethod* abs_method = VerifyInvocationArgs(inst, METHOD_INTERFACE, is_range, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002161 if (abs_method != NULL) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002162 mirror::Class* called_interface = abs_method->GetDeclaringClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002163 if (!called_interface->IsInterface() && !called_interface->IsObjectClass()) {
2164 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected interface class in invoke-interface '"
2165 << PrettyMethod(abs_method) << "'";
2166 break;
Ian Rogers28ad40d2011-10-27 15:19:26 -07002167 }
Ian Rogers0d604842012-04-16 14:50:24 -07002168 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002169 /* Get the type of the "this" arg, which should either be a sub-interface of called
2170 * interface or Object (see comments in RegType::JoinClass).
2171 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002172 const RegType& this_type = work_line_->GetInvocationThis(inst, is_range);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002173 if (this_type.IsZero()) {
2174 /* null pointer always passes (and always fails at runtime) */
2175 } else {
2176 if (this_type.IsUninitializedTypes()) {
2177 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface call on uninitialized object "
2178 << this_type;
2179 break;
2180 }
2181 // In the past we have tried to assert that "called_interface" is assignable
2182 // from "this_type.GetClass()", however, as we do an imprecise Join
2183 // (RegType::JoinClass) we don't have full information on what interfaces are
2184 // implemented by "this_type". For example, two classes may implement the same
2185 // interfaces and have a common parent that doesn't implement the interface. The
2186 // join will set "this_type" to the parent class and a test that this implements
2187 // the interface will incorrectly fail.
2188 }
2189 /*
2190 * We don't have an object instance, so we can't find the concrete method. However, all of
2191 * the type information is in the abstract method, so we're good.
2192 */
2193 const char* descriptor;
2194 if (abs_method == NULL) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002195 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002196 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2197 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
2198 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2199 } else {
2200 descriptor = MethodHelper(abs_method).GetReturnTypeDescriptor();
2201 }
Ian Rogersb4903572012-10-11 11:52:56 -07002202 const RegType& return_type = reg_types_.FromDescriptor(class_loader_, descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002203 if (!return_type.IsLowHalf()) {
2204 work_line_->SetResultRegisterType(return_type);
2205 } else {
2206 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2207 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002208 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002209 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002210 }
jeffhaobdb76512011-09-07 11:43:16 -07002211 case Instruction::NEG_INT:
2212 case Instruction::NOT_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002213 work_line_->CheckUnaryOp(inst, reg_types_.Integer(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002214 break;
2215 case Instruction::NEG_LONG:
2216 case Instruction::NOT_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002217 work_line_->CheckUnaryOpWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002218 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002219 break;
2220 case Instruction::NEG_FLOAT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002221 work_line_->CheckUnaryOp(inst, reg_types_.Float(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002222 break;
2223 case Instruction::NEG_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002224 work_line_->CheckUnaryOpWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002225 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002226 break;
2227 case Instruction::INT_TO_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002228 work_line_->CheckUnaryOpToWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002229 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002230 break;
2231 case Instruction::INT_TO_FLOAT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002232 work_line_->CheckUnaryOp(inst, reg_types_.Float(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002233 break;
2234 case Instruction::INT_TO_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002235 work_line_->CheckUnaryOpToWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002236 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002237 break;
2238 case Instruction::LONG_TO_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002239 work_line_->CheckUnaryOpFromWide(inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002240 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002241 break;
2242 case Instruction::LONG_TO_FLOAT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002243 work_line_->CheckUnaryOpFromWide(inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002244 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002245 break;
2246 case Instruction::LONG_TO_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002247 work_line_->CheckUnaryOpWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002248 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002249 break;
2250 case Instruction::FLOAT_TO_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002251 work_line_->CheckUnaryOp(inst, reg_types_.Integer(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002252 break;
2253 case Instruction::FLOAT_TO_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002254 work_line_->CheckUnaryOpToWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002255 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002256 break;
2257 case Instruction::FLOAT_TO_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002258 work_line_->CheckUnaryOpToWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002259 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002260 break;
2261 case Instruction::DOUBLE_TO_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002262 work_line_->CheckUnaryOpFromWide(inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002263 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002264 break;
2265 case Instruction::DOUBLE_TO_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002266 work_line_->CheckUnaryOpWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002267 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002268 break;
2269 case Instruction::DOUBLE_TO_FLOAT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002270 work_line_->CheckUnaryOpFromWide(inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002271 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002272 break;
2273 case Instruction::INT_TO_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002274 work_line_->CheckUnaryOp(inst, reg_types_.Byte(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002275 break;
2276 case Instruction::INT_TO_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002277 work_line_->CheckUnaryOp(inst, reg_types_.Char(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002278 break;
2279 case Instruction::INT_TO_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002280 work_line_->CheckUnaryOp(inst, reg_types_.Short(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002281 break;
2282
2283 case Instruction::ADD_INT:
2284 case Instruction::SUB_INT:
2285 case Instruction::MUL_INT:
2286 case Instruction::REM_INT:
2287 case Instruction::DIV_INT:
2288 case Instruction::SHL_INT:
2289 case Instruction::SHR_INT:
2290 case Instruction::USHR_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002291 work_line_->CheckBinaryOp(inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002292 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002293 break;
2294 case Instruction::AND_INT:
2295 case Instruction::OR_INT:
2296 case Instruction::XOR_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002297 work_line_->CheckBinaryOp(inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002298 reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002299 break;
2300 case Instruction::ADD_LONG:
2301 case Instruction::SUB_LONG:
2302 case Instruction::MUL_LONG:
2303 case Instruction::DIV_LONG:
2304 case Instruction::REM_LONG:
2305 case Instruction::AND_LONG:
2306 case Instruction::OR_LONG:
2307 case Instruction::XOR_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002308 work_line_->CheckBinaryOpWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002309 reg_types_.LongLo(), reg_types_.LongHi(),
2310 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002311 break;
2312 case Instruction::SHL_LONG:
2313 case Instruction::SHR_LONG:
2314 case Instruction::USHR_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002315 /* shift distance is Int, making these different from other binary operations */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002316 work_line_->CheckBinaryOpWideShift(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002317 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002318 break;
2319 case Instruction::ADD_FLOAT:
2320 case Instruction::SUB_FLOAT:
2321 case Instruction::MUL_FLOAT:
2322 case Instruction::DIV_FLOAT:
2323 case Instruction::REM_FLOAT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002324 work_line_->CheckBinaryOp(inst, reg_types_.Float(), reg_types_.Float(), reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002325 break;
2326 case Instruction::ADD_DOUBLE:
2327 case Instruction::SUB_DOUBLE:
2328 case Instruction::MUL_DOUBLE:
2329 case Instruction::DIV_DOUBLE:
2330 case Instruction::REM_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002331 work_line_->CheckBinaryOpWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002332 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2333 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002334 break;
2335 case Instruction::ADD_INT_2ADDR:
2336 case Instruction::SUB_INT_2ADDR:
2337 case Instruction::MUL_INT_2ADDR:
2338 case Instruction::REM_INT_2ADDR:
2339 case Instruction::SHL_INT_2ADDR:
2340 case Instruction::SHR_INT_2ADDR:
2341 case Instruction::USHR_INT_2ADDR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002342 work_line_->CheckBinaryOp2addr(inst, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002343 break;
2344 case Instruction::AND_INT_2ADDR:
2345 case Instruction::OR_INT_2ADDR:
2346 case Instruction::XOR_INT_2ADDR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002347 work_line_->CheckBinaryOp2addr(inst, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002348 break;
2349 case Instruction::DIV_INT_2ADDR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002350 work_line_->CheckBinaryOp2addr(inst, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002351 break;
2352 case Instruction::ADD_LONG_2ADDR:
2353 case Instruction::SUB_LONG_2ADDR:
2354 case Instruction::MUL_LONG_2ADDR:
2355 case Instruction::DIV_LONG_2ADDR:
2356 case Instruction::REM_LONG_2ADDR:
2357 case Instruction::AND_LONG_2ADDR:
2358 case Instruction::OR_LONG_2ADDR:
2359 case Instruction::XOR_LONG_2ADDR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002360 work_line_->CheckBinaryOp2addrWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002361 reg_types_.LongLo(), reg_types_.LongHi(),
2362 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002363 break;
2364 case Instruction::SHL_LONG_2ADDR:
2365 case Instruction::SHR_LONG_2ADDR:
2366 case Instruction::USHR_LONG_2ADDR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002367 work_line_->CheckBinaryOp2addrWideShift(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002368 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002369 break;
2370 case Instruction::ADD_FLOAT_2ADDR:
2371 case Instruction::SUB_FLOAT_2ADDR:
2372 case Instruction::MUL_FLOAT_2ADDR:
2373 case Instruction::DIV_FLOAT_2ADDR:
2374 case Instruction::REM_FLOAT_2ADDR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002375 work_line_->CheckBinaryOp2addr(inst, reg_types_.Float(), reg_types_.Float(), reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002376 break;
2377 case Instruction::ADD_DOUBLE_2ADDR:
2378 case Instruction::SUB_DOUBLE_2ADDR:
2379 case Instruction::MUL_DOUBLE_2ADDR:
2380 case Instruction::DIV_DOUBLE_2ADDR:
2381 case Instruction::REM_DOUBLE_2ADDR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002382 work_line_->CheckBinaryOp2addrWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002383 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2384 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002385 break;
2386 case Instruction::ADD_INT_LIT16:
2387 case Instruction::RSUB_INT:
2388 case Instruction::MUL_INT_LIT16:
2389 case Instruction::DIV_INT_LIT16:
2390 case Instruction::REM_INT_LIT16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002391 work_line_->CheckLiteralOp(inst, reg_types_.Integer(), reg_types_.Integer(), false, true);
jeffhaobdb76512011-09-07 11:43:16 -07002392 break;
2393 case Instruction::AND_INT_LIT16:
2394 case Instruction::OR_INT_LIT16:
2395 case Instruction::XOR_INT_LIT16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002396 work_line_->CheckLiteralOp(inst, reg_types_.Integer(), reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002397 break;
2398 case Instruction::ADD_INT_LIT8:
2399 case Instruction::RSUB_INT_LIT8:
2400 case Instruction::MUL_INT_LIT8:
2401 case Instruction::DIV_INT_LIT8:
2402 case Instruction::REM_INT_LIT8:
2403 case Instruction::SHL_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002404 case Instruction::SHR_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002405 case Instruction::USHR_INT_LIT8:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002406 work_line_->CheckLiteralOp(inst, reg_types_.Integer(), reg_types_.Integer(), false, false);
jeffhaobdb76512011-09-07 11:43:16 -07002407 break;
2408 case Instruction::AND_INT_LIT8:
2409 case Instruction::OR_INT_LIT8:
2410 case Instruction::XOR_INT_LIT8:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002411 work_line_->CheckLiteralOp(inst, reg_types_.Integer(), reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002412 break;
2413
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002414 // Special instructions.
Sebastien Hertzcc10e0e2013-06-28 14:24:48 +02002415 case Instruction::RETURN_VOID_BARRIER:
2416 DCHECK(Runtime::Current()->IsStarted());
2417 if (!IsConstructor()) {
2418 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void-barrier not expected";
2419 }
2420 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002421 // Note: the following instructions encode offsets derived from class linking.
2422 // As such they use Class*/Field*/AbstractMethod* as these offsets only have
2423 // meaning if the class linking and resolution were successful.
2424 case Instruction::IGET_QUICK:
2425 VerifyIGetQuick(inst, reg_types_.Integer(), true);
2426 break;
2427 case Instruction::IGET_WIDE_QUICK:
2428 VerifyIGetQuick(inst, reg_types_.LongLo(), true);
2429 break;
2430 case Instruction::IGET_OBJECT_QUICK:
2431 VerifyIGetQuick(inst, reg_types_.JavaLangObject(false), false);
2432 break;
2433 case Instruction::IPUT_QUICK:
2434 VerifyIPutQuick(inst, reg_types_.Integer(), true);
2435 break;
2436 case Instruction::IPUT_WIDE_QUICK:
2437 VerifyIPutQuick(inst, reg_types_.LongLo(), true);
2438 break;
2439 case Instruction::IPUT_OBJECT_QUICK:
2440 VerifyIPutQuick(inst, reg_types_.JavaLangObject(false), false);
2441 break;
2442 case Instruction::INVOKE_VIRTUAL_QUICK:
2443 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: {
2444 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
2445 mirror::AbstractMethod* called_method = VerifyInvokeVirtualQuickArgs(inst, is_range);
2446 if (called_method != NULL) {
2447 const char* descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
2448 const RegType& return_type = reg_types_.FromDescriptor(class_loader_, descriptor, false);
2449 if (!return_type.IsLowHalf()) {
2450 work_line_->SetResultRegisterType(return_type);
2451 } else {
2452 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2453 }
2454 just_set_result = true;
2455 }
2456 break;
2457 }
2458
Ian Rogersd81871c2011-10-03 13:57:23 -07002459 /* These should never appear during verification. */
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002460 case Instruction::UNUSED_3E:
2461 case Instruction::UNUSED_3F:
2462 case Instruction::UNUSED_40:
2463 case Instruction::UNUSED_41:
2464 case Instruction::UNUSED_42:
2465 case Instruction::UNUSED_43:
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002466 case Instruction::UNUSED_79:
2467 case Instruction::UNUSED_7A:
2468 case Instruction::UNUSED_EB:
2469 case Instruction::UNUSED_EC:
jeffhao9a4f0032012-08-30 16:17:40 -07002470 case Instruction::UNUSED_ED:
jeffhaobdb76512011-09-07 11:43:16 -07002471 case Instruction::UNUSED_EE:
2472 case Instruction::UNUSED_EF:
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002473 case Instruction::UNUSED_F0:
2474 case Instruction::UNUSED_F1:
jeffhaobdb76512011-09-07 11:43:16 -07002475 case Instruction::UNUSED_F2:
2476 case Instruction::UNUSED_F3:
2477 case Instruction::UNUSED_F4:
2478 case Instruction::UNUSED_F5:
2479 case Instruction::UNUSED_F6:
2480 case Instruction::UNUSED_F7:
2481 case Instruction::UNUSED_F8:
2482 case Instruction::UNUSED_F9:
2483 case Instruction::UNUSED_FA:
2484 case Instruction::UNUSED_FB:
jeffhaobdb76512011-09-07 11:43:16 -07002485 case Instruction::UNUSED_FC:
jeffhaobdb76512011-09-07 11:43:16 -07002486 case Instruction::UNUSED_FD:
jeffhaobdb76512011-09-07 11:43:16 -07002487 case Instruction::UNUSED_FE:
jeffhaobdb76512011-09-07 11:43:16 -07002488 case Instruction::UNUSED_FF:
jeffhaod5347e02012-03-22 17:25:05 -07002489 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Unexpected opcode " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07002490 break;
2491
2492 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002493 * DO NOT add a "default" clause here. Without it the compiler will
jeffhaobdb76512011-09-07 11:43:16 -07002494 * complain if an instruction is missing (which is desirable).
2495 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002496 } // end - switch (dec_insn.opcode)
jeffhaobdb76512011-09-07 11:43:16 -07002497
Ian Rogersad0b3a32012-04-16 14:50:24 -07002498 if (have_pending_hard_failure_) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002499 if (Runtime::Current()->IsCompiler()) {
jeffhaob57e9522012-04-26 18:08:21 -07002500 /* When compiling, check that the last failure is a hard failure */
Ian Rogersad0b3a32012-04-16 14:50:24 -07002501 CHECK_EQ(failures_[failures_.size() - 1], VERIFY_ERROR_BAD_CLASS_HARD);
Ian Rogerse1758fe2012-04-19 11:31:15 -07002502 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002503 /* immediate failure, reject class */
2504 info_messages_ << "Rejecting opcode " << inst->DumpString(dex_file_);
2505 return false;
jeffhaofaf459e2012-08-31 15:32:47 -07002506 } else if (have_pending_runtime_throw_failure_) {
2507 /* slow path will throw, mark following code as unreachable */
2508 opcode_flags = Instruction::kThrow;
jeffhaobdb76512011-09-07 11:43:16 -07002509 }
jeffhaobdb76512011-09-07 11:43:16 -07002510 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002511 * If we didn't just set the result register, clear it out. This ensures that you can only use
2512 * "move-result" immediately after the result is set. (We could check this statically, but it's
2513 * not expensive and it makes our debugging output cleaner.)
jeffhaobdb76512011-09-07 11:43:16 -07002514 */
2515 if (!just_set_result) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002516 work_line_->SetResultTypeToUnknown();
jeffhaobdb76512011-09-07 11:43:16 -07002517 }
2518
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002519
jeffhaobdb76512011-09-07 11:43:16 -07002520
2521 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002522 * Handle "branch". Tag the branch target.
jeffhaobdb76512011-09-07 11:43:16 -07002523 *
2524 * NOTE: instructions like Instruction::EQZ provide information about the
jeffhaod1f0fde2011-09-08 17:25:33 -07002525 * state of the register when the branch is taken or not taken. For example,
jeffhaobdb76512011-09-07 11:43:16 -07002526 * somebody could get a reference field, check it for zero, and if the
2527 * branch is taken immediately store that register in a boolean field
jeffhaod1f0fde2011-09-08 17:25:33 -07002528 * since the value is known to be zero. We do not currently account for
jeffhaobdb76512011-09-07 11:43:16 -07002529 * that, and will reject the code.
2530 *
2531 * TODO: avoid re-fetching the branch target
2532 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002533 if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002534 bool isConditional, selfOkay;
Ian Rogersd81871c2011-10-03 13:57:23 -07002535 if (!GetBranchOffset(work_insn_idx_, &branch_target, &isConditional, &selfOkay)) {
jeffhaobdb76512011-09-07 11:43:16 -07002536 /* should never happen after static verification */
jeffhaod5347e02012-03-22 17:25:05 -07002537 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad branch";
jeffhaobdb76512011-09-07 11:43:16 -07002538 return false;
2539 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08002540 DCHECK_EQ(isConditional, (opcode_flags & Instruction::kContinue) != 0);
jeffhaod5347e02012-03-22 17:25:05 -07002541 if (!CheckNotMoveException(code_item_->insns_, work_insn_idx_ + branch_target)) {
jeffhaobdb76512011-09-07 11:43:16 -07002542 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002543 }
jeffhaobdb76512011-09-07 11:43:16 -07002544 /* update branch target, set "changed" if appropriate */
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002545 if (NULL != branch_line.get()) {
2546 if (!UpdateRegisters(work_insn_idx_ + branch_target, branch_line.get())) {
2547 return false;
2548 }
2549 } else {
2550 if (!UpdateRegisters(work_insn_idx_ + branch_target, work_line_.get())) {
2551 return false;
2552 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002553 }
jeffhaobdb76512011-09-07 11:43:16 -07002554 }
2555
2556 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002557 * Handle "switch". Tag all possible branch targets.
jeffhaobdb76512011-09-07 11:43:16 -07002558 *
2559 * We've already verified that the table is structurally sound, so we
2560 * just need to walk through and tag the targets.
2561 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002562 if ((opcode_flags & Instruction::kSwitch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002563 int offset_to_switch = insns[1] | (((int32_t) insns[2]) << 16);
2564 const uint16_t* switch_insns = insns + offset_to_switch;
2565 int switch_count = switch_insns[1];
2566 int offset_to_targets, targ;
2567
2568 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
2569 /* 0 = sig, 1 = count, 2/3 = first key */
2570 offset_to_targets = 4;
2571 } else {
2572 /* 0 = sig, 1 = count, 2..count * 2 = keys */
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07002573 DCHECK((*insns & 0xff) == Instruction::SPARSE_SWITCH);
jeffhaobdb76512011-09-07 11:43:16 -07002574 offset_to_targets = 2 + 2 * switch_count;
2575 }
2576
2577 /* verify each switch target */
2578 for (targ = 0; targ < switch_count; targ++) {
2579 int offset;
2580 uint32_t abs_offset;
2581
2582 /* offsets are 32-bit, and only partly endian-swapped */
2583 offset = switch_insns[offset_to_targets + targ * 2] |
2584 (((int32_t) switch_insns[offset_to_targets + targ * 2 + 1]) << 16);
Ian Rogersd81871c2011-10-03 13:57:23 -07002585 abs_offset = work_insn_idx_ + offset;
2586 DCHECK_LT(abs_offset, code_item_->insns_size_in_code_units_);
jeffhaod5347e02012-03-22 17:25:05 -07002587 if (!CheckNotMoveException(code_item_->insns_, abs_offset)) {
jeffhaobdb76512011-09-07 11:43:16 -07002588 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002589 }
2590 if (!UpdateRegisters(abs_offset, work_line_.get()))
jeffhaobdb76512011-09-07 11:43:16 -07002591 return false;
2592 }
2593 }
2594
2595 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002596 * Handle instructions that can throw and that are sitting in a "try" block. (If they're not in a
2597 * "try" block when they throw, control transfers out of the method.)
jeffhaobdb76512011-09-07 11:43:16 -07002598 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002599 if ((opcode_flags & Instruction::kThrow) != 0 && insn_flags_[work_insn_idx_].IsInTry()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002600 bool within_catch_all = false;
Ian Rogers0571d352011-11-03 19:51:38 -07002601 CatchHandlerIterator iterator(*code_item_, work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07002602
Ian Rogers0571d352011-11-03 19:51:38 -07002603 for (; iterator.HasNext(); iterator.Next()) {
2604 if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002605 within_catch_all = true;
2606 }
jeffhaobdb76512011-09-07 11:43:16 -07002607 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002608 * Merge registers into the "catch" block. We want to use the "savedRegs" rather than
2609 * "work_regs", because at runtime the exception will be thrown before the instruction
2610 * modifies any registers.
jeffhaobdb76512011-09-07 11:43:16 -07002611 */
Ian Rogers0571d352011-11-03 19:51:38 -07002612 if (!UpdateRegisters(iterator.GetHandlerAddress(), saved_line_.get())) {
jeffhaobdb76512011-09-07 11:43:16 -07002613 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002614 }
jeffhaobdb76512011-09-07 11:43:16 -07002615 }
2616
2617 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002618 * If the monitor stack depth is nonzero, there must be a "catch all" handler for this
2619 * instruction. This does apply to monitor-exit because of async exception handling.
jeffhaobdb76512011-09-07 11:43:16 -07002620 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002621 if (work_line_->MonitorStackDepth() > 0 && !within_catch_all) {
jeffhaobdb76512011-09-07 11:43:16 -07002622 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002623 * The state in work_line reflects the post-execution state. If the current instruction is a
2624 * monitor-enter and the monitor stack was empty, we don't need a catch-all (if it throws,
jeffhaobdb76512011-09-07 11:43:16 -07002625 * it will do so before grabbing the lock).
2626 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002627 if (inst->Opcode() != Instruction::MONITOR_ENTER || work_line_->MonitorStackDepth() != 1) {
jeffhaod5347e02012-03-22 17:25:05 -07002628 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
Ian Rogersd81871c2011-10-03 13:57:23 -07002629 << "expected to be within a catch-all for an instruction where a monitor is held";
jeffhaobdb76512011-09-07 11:43:16 -07002630 return false;
2631 }
2632 }
2633 }
2634
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002635 /* Handle "continue". Tag the next consecutive instruction.
2636 * Note: Keep the code handling "continue" case below the "branch" and "switch" cases,
2637 * because it changes work_line_ when performing peephole optimization
2638 * and this change should not be used in those cases.
2639 */
2640 if ((opcode_flags & Instruction::kContinue) != 0) {
2641 uint32_t next_insn_idx = work_insn_idx_ + CurrentInsnFlags()->GetLengthInCodeUnits();
2642 if (next_insn_idx >= code_item_->insns_size_in_code_units_) {
2643 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Execution can walk off end of code area";
2644 return false;
2645 }
2646 // The only way to get to a move-exception instruction is to get thrown there. Make sure the
2647 // next instruction isn't one.
2648 if (!CheckNotMoveException(code_item_->insns_, next_insn_idx)) {
2649 return false;
2650 }
Ian Rogersc9e463c2013-06-05 16:52:26 -07002651 if (NULL != fallthrough_line.get()) {
2652 // Make workline consistent with fallthrough computed from peephole optimization.
2653 work_line_->CopyFromLine(fallthrough_line.get());
2654 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002655 RegisterLine* next_line = reg_table_.GetLine(next_insn_idx);
2656 if (next_line != NULL) {
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002657 // Merge registers into what we have for the next instruction,
2658 // and set the "changed" flag if needed.
2659 if (!UpdateRegisters(next_insn_idx, work_line_.get())) {
2660 return false;
2661 }
2662 } else {
2663 /*
2664 * We're not recording register data for the next instruction, so we don't know what the
2665 * prior state was. We have to assume that something has changed and re-evaluate it.
2666 */
2667 insn_flags_[next_insn_idx].SetChanged();
2668 }
2669 }
2670
jeffhaod1f0fde2011-09-08 17:25:33 -07002671 /* If we're returning from the method, make sure monitor stack is empty. */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002672 if ((opcode_flags & Instruction::kReturn) != 0) {
Elliott Hughesb25c3f62012-03-26 16:35:06 -07002673 if (!work_line_->VerifyMonitorStackEmpty()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002674 return false;
2675 }
jeffhaobdb76512011-09-07 11:43:16 -07002676 }
2677
2678 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002679 * Update start_guess. Advance to the next instruction of that's
2680 * possible, otherwise use the branch target if one was found. If
jeffhaobdb76512011-09-07 11:43:16 -07002681 * neither of those exists we're in a return or throw; leave start_guess
2682 * alone and let the caller sort it out.
2683 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002684 if ((opcode_flags & Instruction::kContinue) != 0) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002685 *start_guess = work_insn_idx_ + insn_flags_[work_insn_idx_].GetLengthInCodeUnits();
Elliott Hughesadb8c672012-03-06 16:49:32 -08002686 } else if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002687 /* we're still okay if branch_target is zero */
Ian Rogersd81871c2011-10-03 13:57:23 -07002688 *start_guess = work_insn_idx_ + branch_target;
jeffhaobdb76512011-09-07 11:43:16 -07002689 }
2690
Ian Rogersd81871c2011-10-03 13:57:23 -07002691 DCHECK_LT(*start_guess, code_item_->insns_size_in_code_units_);
2692 DCHECK(insn_flags_[*start_guess].IsOpcode());
jeffhaobdb76512011-09-07 11:43:16 -07002693
2694 return true;
2695}
2696
Ian Rogers776ac1f2012-04-13 23:36:36 -07002697const RegType& MethodVerifier::ResolveClassAndCheckAccess(uint32_t class_idx) {
Ian Rogers0571d352011-11-03 19:51:38 -07002698 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002699 const RegType& referrer = GetDeclaringClass();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002700 mirror::Class* klass = dex_cache_->GetResolvedType(class_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002701 const RegType& result =
Ian Rogers04f94f42013-06-10 15:09:26 -07002702 klass != NULL ? reg_types_.FromClass(descriptor, klass,
2703 klass->CannotBeAssignedFromOtherTypes())
Ian Rogersb4903572012-10-11 11:52:56 -07002704 : reg_types_.FromDescriptor(class_loader_, descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002705 if (result.IsConflict()) {
2706 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "accessing broken descriptor '" << descriptor
2707 << "' in " << referrer;
2708 return result;
2709 }
Ian Rogerse1758fe2012-04-19 11:31:15 -07002710 if (klass == NULL && !result.IsUnresolvedTypes()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002711 dex_cache_->SetResolvedType(class_idx, result.GetClass());
Ian Rogerse1758fe2012-04-19 11:31:15 -07002712 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002713 // Check if access is allowed. Unresolved types use xxxWithAccessCheck to
Ian Rogers28ad40d2011-10-27 15:19:26 -07002714 // check at runtime if access is allowed and so pass here.
Ian Rogersad0b3a32012-04-16 14:50:24 -07002715 if (!result.IsUnresolvedTypes() && !referrer.IsUnresolvedTypes() && !referrer.CanAccess(result)) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002716 Fail(VERIFY_ERROR_ACCESS_CLASS) << "illegal class access: '"
Ian Rogersad0b3a32012-04-16 14:50:24 -07002717 << referrer << "' -> '" << result << "'";
Ian Rogers28ad40d2011-10-27 15:19:26 -07002718 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002719 return result;
Ian Rogersd81871c2011-10-03 13:57:23 -07002720}
2721
Ian Rogers776ac1f2012-04-13 23:36:36 -07002722const RegType& MethodVerifier::GetCaughtExceptionType() {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002723 const RegType* common_super = NULL;
Ian Rogersd81871c2011-10-03 13:57:23 -07002724 if (code_item_->tries_size_ != 0) {
Ian Rogers0571d352011-11-03 19:51:38 -07002725 const byte* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
Ian Rogersd81871c2011-10-03 13:57:23 -07002726 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
2727 for (uint32_t i = 0; i < handlers_size; i++) {
Ian Rogers0571d352011-11-03 19:51:38 -07002728 CatchHandlerIterator iterator(handlers_ptr);
2729 for (; iterator.HasNext(); iterator.Next()) {
2730 if (iterator.GetHandlerAddress() == (uint32_t) work_insn_idx_) {
2731 if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) {
Ian Rogersb4903572012-10-11 11:52:56 -07002732 common_super = &reg_types_.JavaLangThrowable(false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002733 } else {
Ian Rogers0571d352011-11-03 19:51:38 -07002734 const RegType& exception = ResolveClassAndCheckAccess(iterator.GetHandlerTypeIndex());
Ian Rogersc4762272012-02-01 15:55:55 -08002735 if (common_super == NULL) {
2736 // Unconditionally assign for the first handler. We don't assert this is a Throwable
2737 // as that is caught at runtime
2738 common_super = &exception;
Ian Rogersb4903572012-10-11 11:52:56 -07002739 } else if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(exception)) {
Ian Rogersc4762272012-02-01 15:55:55 -08002740 // We don't know enough about the type and the common path merge will result in
2741 // Conflict. Fail here knowing the correct thing can be done at runtime.
jeffhaod5347e02012-03-22 17:25:05 -07002742 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unexpected non-exception class " << exception;
Ian Rogersad0b3a32012-04-16 14:50:24 -07002743 return reg_types_.Conflict();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002744 } else if (common_super->Equals(exception)) {
Ian Rogersc4762272012-02-01 15:55:55 -08002745 // odd case, but nothing to do
Ian Rogersd81871c2011-10-03 13:57:23 -07002746 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002747 common_super = &common_super->Merge(exception, &reg_types_);
Ian Rogersb4903572012-10-11 11:52:56 -07002748 CHECK(reg_types_.JavaLangThrowable(false).IsAssignableFrom(*common_super));
Ian Rogersd81871c2011-10-03 13:57:23 -07002749 }
2750 }
2751 }
2752 }
Ian Rogers0571d352011-11-03 19:51:38 -07002753 handlers_ptr = iterator.EndDataPointer();
Ian Rogersd81871c2011-10-03 13:57:23 -07002754 }
2755 }
2756 if (common_super == NULL) {
2757 /* no catch blocks, or no catches with classes we can find */
jeffhaod5347e02012-03-22 17:25:05 -07002758 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unable to find exception handler";
Ian Rogersad0b3a32012-04-16 14:50:24 -07002759 return reg_types_.Conflict();
Ian Rogersd81871c2011-10-03 13:57:23 -07002760 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002761 return *common_super;
Ian Rogersd81871c2011-10-03 13:57:23 -07002762}
2763
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002764mirror::AbstractMethod* MethodVerifier::ResolveMethodAndCheckAccess(uint32_t dex_method_idx,
2765 MethodType method_type) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002766 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx);
Ian Rogers90040192011-12-16 08:54:29 -08002767 const RegType& klass_type = ResolveClassAndCheckAccess(method_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002768 if (klass_type.IsConflict()) {
2769 std::string append(" in attempt to access method ");
2770 append += dex_file_->GetMethodName(method_id);
2771 AppendToLastFailMessage(append);
Ian Rogers90040192011-12-16 08:54:29 -08002772 return NULL;
2773 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002774 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers90040192011-12-16 08:54:29 -08002775 return NULL; // Can't resolve Class so no more to do here
2776 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002777 mirror::Class* klass = klass_type.GetClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002778 const RegType& referrer = GetDeclaringClass();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002779 mirror::AbstractMethod* res_method = dex_cache_->GetResolvedMethod(dex_method_idx);
Ian Rogersd81871c2011-10-03 13:57:23 -07002780 if (res_method == NULL) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07002781 const char* name = dex_file_->GetMethodName(method_id);
Ian Rogers0571d352011-11-03 19:51:38 -07002782 std::string signature(dex_file_->CreateMethodSignature(method_id.proto_idx_, NULL));
jeffhao8cd6dda2012-02-22 10:15:34 -08002783
2784 if (method_type == METHOD_DIRECT || method_type == METHOD_STATIC) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002785 res_method = klass->FindDirectMethod(name, signature);
jeffhao8cd6dda2012-02-22 10:15:34 -08002786 } else if (method_type == METHOD_INTERFACE) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002787 res_method = klass->FindInterfaceMethod(name, signature);
2788 } else {
2789 res_method = klass->FindVirtualMethod(name, signature);
2790 }
2791 if (res_method != NULL) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002792 dex_cache_->SetResolvedMethod(dex_method_idx, res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07002793 } else {
jeffhao8cd6dda2012-02-22 10:15:34 -08002794 // If a virtual or interface method wasn't found with the expected type, look in
2795 // the direct methods. This can happen when the wrong invoke type is used or when
2796 // a class has changed, and will be flagged as an error in later checks.
2797 if (method_type == METHOD_INTERFACE || method_type == METHOD_VIRTUAL) {
2798 res_method = klass->FindDirectMethod(name, signature);
2799 }
2800 if (res_method == NULL) {
2801 Fail(VERIFY_ERROR_NO_METHOD) << "couldn't find method "
2802 << PrettyDescriptor(klass) << "." << name
2803 << " " << signature;
2804 return NULL;
2805 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002806 }
2807 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002808 // Make sure calls to constructors are "direct". There are additional restrictions but we don't
2809 // enforce them here.
2810 if (res_method->IsConstructor() && method_type != METHOD_DIRECT) {
jeffhaod5347e02012-03-22 17:25:05 -07002811 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting non-direct call to constructor "
2812 << PrettyMethod(res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07002813 return NULL;
2814 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002815 // Disallow any calls to class initializers.
2816 if (MethodHelper(res_method).IsClassInitializer()) {
jeffhaod5347e02012-03-22 17:25:05 -07002817 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting call to class initializer "
2818 << PrettyMethod(res_method);
jeffhao8cd6dda2012-02-22 10:15:34 -08002819 return NULL;
2820 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002821 // Check if access is allowed.
Ian Rogersad0b3a32012-04-16 14:50:24 -07002822 if (!referrer.CanAccessMember(res_method->GetDeclaringClass(), res_method->GetAccessFlags())) {
jeffhao8cd6dda2012-02-22 10:15:34 -08002823 Fail(VERIFY_ERROR_ACCESS_METHOD) << "illegal method access (call " << PrettyMethod(res_method)
Ian Rogersad0b3a32012-04-16 14:50:24 -07002824 << " from " << referrer << ")";
jeffhaob57e9522012-04-26 18:08:21 -07002825 return res_method;
jeffhao8cd6dda2012-02-22 10:15:34 -08002826 }
jeffhaode0d9c92012-02-27 13:58:13 -08002827 // Check that invoke-virtual and invoke-super are not used on private methods of the same class.
2828 if (res_method->IsPrivate() && method_type == METHOD_VIRTUAL) {
jeffhaod5347e02012-03-22 17:25:05 -07002829 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invoke-super/virtual can't be used on private method "
2830 << PrettyMethod(res_method);
jeffhaode0d9c92012-02-27 13:58:13 -08002831 return NULL;
2832 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002833 // Check that interface methods match interface classes.
2834 if (klass->IsInterface() && method_type != METHOD_INTERFACE) {
2835 Fail(VERIFY_ERROR_CLASS_CHANGE) << "non-interface method " << PrettyMethod(res_method)
2836 << " is in an interface class " << PrettyClass(klass);
2837 return NULL;
2838 } else if (!klass->IsInterface() && method_type == METHOD_INTERFACE) {
2839 Fail(VERIFY_ERROR_CLASS_CHANGE) << "interface method " << PrettyMethod(res_method)
2840 << " is in a non-interface class " << PrettyClass(klass);
2841 return NULL;
2842 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002843 // See if the method type implied by the invoke instruction matches the access flags for the
2844 // target method.
2845 if ((method_type == METHOD_DIRECT && !res_method->IsDirect()) ||
2846 (method_type == METHOD_STATIC && !res_method->IsStatic()) ||
2847 ((method_type == METHOD_VIRTUAL || method_type == METHOD_INTERFACE) && res_method->IsDirect())
2848 ) {
Ian Rogers2fc14272012-08-30 10:56:57 -07002849 Fail(VERIFY_ERROR_CLASS_CHANGE) << "invoke type (" << method_type << ") does not match method "
2850 " type of " << PrettyMethod(res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07002851 return NULL;
2852 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002853 return res_method;
2854}
2855
Sebastien Hertz5243e912013-05-21 10:55:07 +02002856mirror::AbstractMethod* MethodVerifier::VerifyInvocationArgs(const Instruction* inst,
2857 MethodType method_type,
2858 bool is_range,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002859 bool is_super) {
jeffhao8cd6dda2012-02-22 10:15:34 -08002860 // Resolve the method. This could be an abstract or concrete method depending on what sort of call
2861 // we're making.
Sebastien Hertz5243e912013-05-21 10:55:07 +02002862 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
2863 mirror::AbstractMethod* res_method = ResolveMethodAndCheckAccess(method_idx, method_type);
jeffhao8cd6dda2012-02-22 10:15:34 -08002864 if (res_method == NULL) { // error or class is unresolved
2865 return NULL;
2866 }
2867
Ian Rogersd81871c2011-10-03 13:57:23 -07002868 // If we're using invoke-super(method), make sure that the executing method's class' superclass
2869 // has a vtable entry for the target method.
2870 if (is_super) {
2871 DCHECK(method_type == METHOD_VIRTUAL);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002872 const RegType& super = GetDeclaringClass().GetSuperClass(&reg_types_);
Ian Rogers529781d2012-07-23 17:24:29 -07002873 if (super.IsUnresolvedTypes()) {
jeffhao4d8df822012-04-24 17:09:36 -07002874 Fail(VERIFY_ERROR_NO_METHOD) << "unknown super class in invoke-super from "
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002875 << PrettyMethod(dex_method_idx_, *dex_file_)
jeffhao4d8df822012-04-24 17:09:36 -07002876 << " to super " << PrettyMethod(res_method);
2877 return NULL;
2878 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002879 mirror::Class* super_klass = super.GetClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002880 if (res_method->GetMethodIndex() >= super_klass->GetVTable()->GetLength()) {
jeffhao4d8df822012-04-24 17:09:36 -07002881 MethodHelper mh(res_method);
2882 Fail(VERIFY_ERROR_NO_METHOD) << "invalid invoke-super from "
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002883 << PrettyMethod(dex_method_idx_, *dex_file_)
jeffhao4d8df822012-04-24 17:09:36 -07002884 << " to super " << super
2885 << "." << mh.GetName()
2886 << mh.GetSignature();
Ian Rogersd81871c2011-10-03 13:57:23 -07002887 return NULL;
2888 }
2889 }
2890 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002891 // match the call to the signature. Also, we might be calling through an abstract method
Ian Rogersd81871c2011-10-03 13:57:23 -07002892 // definition (which doesn't have register count values).
Sebastien Hertz5243e912013-05-21 10:55:07 +02002893 const size_t expected_args = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
Ian Rogersd81871c2011-10-03 13:57:23 -07002894 /* caught by static verifier */
2895 DCHECK(is_range || expected_args <= 5);
2896 if (expected_args > code_item_->outs_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07002897 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
Ian Rogersd81871c2011-10-03 13:57:23 -07002898 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
2899 return NULL;
2900 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002901
jeffhaobdb76512011-09-07 11:43:16 -07002902 /*
Ian Rogersad0b3a32012-04-16 14:50:24 -07002903 * Check the "this" argument, which must be an instance of the class that declared the method.
2904 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
2905 * rigorous check here (which is okay since we have to do it at runtime).
jeffhaobdb76512011-09-07 11:43:16 -07002906 */
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002907 size_t actual_args = 0;
Ian Rogersd81871c2011-10-03 13:57:23 -07002908 if (!res_method->IsStatic()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002909 const RegType& actual_arg_type = work_line_->GetInvocationThis(inst, is_range);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002910 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
Ian Rogersd81871c2011-10-03 13:57:23 -07002911 return NULL;
2912 }
2913 if (actual_arg_type.IsUninitializedReference() && !res_method->IsConstructor()) {
jeffhaod5347e02012-03-22 17:25:05 -07002914 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
Ian Rogersd81871c2011-10-03 13:57:23 -07002915 return NULL;
2916 }
2917 if (method_type != METHOD_INTERFACE && !actual_arg_type.IsZero()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002918 mirror::Class* klass = res_method->GetDeclaringClass();
Ian Rogers04f94f42013-06-10 15:09:26 -07002919 const RegType& res_method_class =
2920 reg_types_.FromClass(ClassHelper(klass).GetDescriptor(), klass,
2921 klass->CannotBeAssignedFromOtherTypes());
Ian Rogers9074b992011-10-26 17:41:55 -07002922 if (!res_method_class.IsAssignableFrom(actual_arg_type)) {
jeffhaod5347e02012-03-22 17:25:05 -07002923 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002924 << "' not instance of '" << res_method_class << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07002925 return NULL;
2926 }
2927 }
2928 actual_args++;
2929 }
2930 /*
2931 * Process the target method's signature. This signature may or may not
2932 * have been verified, so we can't assume it's properly formed.
2933 */
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002934 MethodHelper mh(res_method);
2935 const DexFile::TypeList* params = mh.GetParameterTypeList();
2936 size_t params_size = params == NULL ? 0 : params->Size();
Sebastien Hertz5243e912013-05-21 10:55:07 +02002937 uint32_t arg[5];
2938 if (!is_range) {
2939 inst->GetArgs(arg);
2940 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002941 for (size_t param_index = 0; param_index < params_size; param_index++) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002942 if (actual_args >= expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07002943 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invalid call to '" << PrettyMethod(res_method)
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002944 << "'. Expected " << expected_args << " arguments, processing argument " << actual_args
2945 << " (where longs/doubles count twice).";
Ian Rogersd81871c2011-10-03 13:57:23 -07002946 return NULL;
2947 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002948 const char* descriptor =
2949 mh.GetTypeDescriptorFromTypeIdx(params->GetTypeItem(param_index).type_idx_);
2950 if (descriptor == NULL) {
jeffhaod5347e02012-03-22 17:25:05 -07002951 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002952 << " missing signature component";
2953 return NULL;
Ian Rogersd81871c2011-10-03 13:57:23 -07002954 }
Ian Rogersb4903572012-10-11 11:52:56 -07002955 const RegType& reg_type = reg_types_.FromDescriptor(class_loader_, descriptor, false);
Sebastien Hertz5243e912013-05-21 10:55:07 +02002956 uint32_t get_reg = is_range ? inst->VRegC_3rc() + actual_args : arg[actual_args];
Ian Rogers84fa0742011-10-25 18:13:30 -07002957 if (!work_line_->VerifyRegisterType(get_reg, reg_type)) {
jeffhaob57e9522012-04-26 18:08:21 -07002958 return res_method;
Ian Rogersd81871c2011-10-03 13:57:23 -07002959 }
2960 actual_args = reg_type.IsLongOrDoubleTypes() ? actual_args + 2 : actual_args + 1;
2961 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002962 if (actual_args != expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07002963 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002964 << " expected " << expected_args << " arguments, found " << actual_args;
Ian Rogersd81871c2011-10-03 13:57:23 -07002965 return NULL;
2966 } else {
2967 return res_method;
2968 }
2969}
2970
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002971mirror::AbstractMethod* MethodVerifier::VerifyInvokeVirtualQuickArgs(const Instruction* inst,
2972 bool is_range) {
2973 DCHECK(Runtime::Current()->IsStarted());
2974 CHECK(invoked_method != NULL || accessed_field != NULL)
2975 << "We should not be verifying " << inst->Name();
2976 const RegType& actual_arg_type = work_line_->GetInvocationThis(inst, is_range);
2977 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
2978 return NULL;
2979 }
2980 mirror::Class* this_class = NULL;
2981 if (!actual_arg_type.IsUnresolvedTypes()) {
2982 this_class = actual_arg_type.GetClass();
2983 } else {
2984 const std::string& descriptor(actual_arg_type.GetDescriptor());
2985 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
2986 this_class = class_linker->FindClass(descriptor.c_str(), class_loader_);
2987 if (this_class == NULL) {
2988 Thread::Current()->ClearException();
2989 // Look for a system class
2990 this_class = class_linker->FindClass(descriptor.c_str(), NULL);
2991 }
2992 }
2993 CHECK(this_class != NULL) << "Cannot get Class* for type " << actual_arg_type;
2994 mirror::ObjectArray<mirror::AbstractMethod>* vtable = this_class->GetVTable();
2995 CHECK(vtable != NULL);
2996 uint16_t vtable_index = is_range ? inst->VRegB_3rc() : inst->VRegB_35c();
2997 CHECK(vtable_index < vtable->GetLength());
2998 mirror::AbstractMethod* res_method = vtable->Get(vtable_index);
2999 CHECK(!Thread::Current()->IsExceptionPending());
3000 // TODO: we should move the code below to FindInvokedMethodAtDexPc. Once the
3001 // method is verified, we could access the information we need from register
3002 // lines for the dex pc we are looking for.
3003 if (invoked_method != NULL && work_insn_idx_ == interesting_dex_pc_) {
3004 // We've been requested to tell which method is invoked at this dex pc.
3005 *invoked_method = res_method;
3006 }
3007 if (res_method == NULL) {
3008 return NULL;
3009 }
3010 CHECK(!res_method->IsDirect() && !res_method->IsStatic());
3011
3012 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
3013 // match the call to the signature. Also, we might be calling through an abstract method
3014 // definition (which doesn't have register count values).
3015 const size_t expected_args = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
3016 /* caught by static verifier */
3017 DCHECK(is_range || expected_args <= 5);
3018 if (expected_args > code_item_->outs_size_) {
3019 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
3020 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
3021 return NULL;
3022 }
3023
3024 /*
3025 * Check the "this" argument, which must be an instance of the class that declared the method.
3026 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
3027 * rigorous check here (which is okay since we have to do it at runtime).
3028 */
3029 if (actual_arg_type.IsUninitializedReference() && !res_method->IsConstructor()) {
3030 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
3031 return NULL;
3032 }
3033 if (!actual_arg_type.IsZero()) {
3034 mirror::Class* klass = res_method->GetDeclaringClass();
3035 const RegType& res_method_class =
3036 reg_types_.FromClass(ClassHelper(klass).GetDescriptor(), klass,
3037 klass->CannotBeAssignedFromOtherTypes());
3038 if (!res_method_class.IsAssignableFrom(actual_arg_type)) {
3039 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type
3040 << "' not instance of '" << res_method_class << "'";
3041 return NULL;
3042 }
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 */
3048 MethodHelper mh(res_method);
3049 const DexFile::TypeList* params = mh.GetParameterTypeList();
3050 size_t params_size = params == NULL ? 0 : params->Size();
3051 uint32_t arg[5];
3052 if (!is_range) {
3053 inst->GetArgs(arg);
3054 }
3055 size_t actual_args = 1;
3056 for (size_t param_index = 0; param_index < params_size; param_index++) {
3057 if (actual_args >= expected_args) {
3058 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invalid call to '" << PrettyMethod(res_method)
3059 << "'. Expected " << expected_args << " arguments, processing argument " << actual_args
3060 << " (where longs/doubles count twice).";
3061 return NULL;
3062 }
3063 const char* descriptor =
3064 mh.GetTypeDescriptorFromTypeIdx(params->GetTypeItem(param_index).type_idx_);
3065 if (descriptor == NULL) {
3066 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
3067 << " missing signature component";
3068 return NULL;
3069 }
3070 const RegType& reg_type = reg_types_.FromDescriptor(class_loader_, descriptor, false);
3071 uint32_t get_reg = is_range ? inst->VRegC_3rc() + actual_args : arg[actual_args];
3072 if (!work_line_->VerifyRegisterType(get_reg, reg_type)) {
3073 return res_method;
3074 }
3075 actual_args = reg_type.IsLongOrDoubleTypes() ? actual_args + 2 : actual_args + 1;
3076 }
3077 if (actual_args != expected_args) {
3078 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
3079 << " expected " << expected_args << " arguments, found " << actual_args;
3080 return NULL;
3081 } else {
3082 return res_method;
3083 }
3084}
3085
Ian Rogers62342ec2013-06-11 10:26:37 -07003086void MethodVerifier::VerifyNewArray(const Instruction* inst, bool is_filled, bool is_range) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003087 uint32_t type_idx;
3088 if (!is_filled) {
3089 DCHECK_EQ(inst->Opcode(), Instruction::NEW_ARRAY);
3090 type_idx = inst->VRegC_22c();
3091 } else if (!is_range) {
3092 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY);
3093 type_idx = inst->VRegB_35c();
3094 } else {
3095 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY_RANGE);
3096 type_idx = inst->VRegB_3rc();
3097 }
3098 const RegType& res_type = ResolveClassAndCheckAccess(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003099 if (res_type.IsConflict()) { // bad class
3100 DCHECK_NE(failures_.size(), 0U);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003101 } else {
3102 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
3103 if (!res_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003104 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "new-array on non-array class " << res_type;
Ian Rogers0c4a5062012-02-03 15:18:59 -08003105 } else if (!is_filled) {
3106 /* make sure "size" register is valid type */
Sebastien Hertz5243e912013-05-21 10:55:07 +02003107 work_line_->VerifyRegisterType(inst->VRegB_22c(), reg_types_.Integer());
Ian Rogers0c4a5062012-02-03 15:18:59 -08003108 /* set register type to array class */
Ian Rogers62342ec2013-06-11 10:26:37 -07003109 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
3110 work_line_->SetRegisterType(inst->VRegA_22c(), precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003111 } else {
3112 // Verify each register. If "arg_count" is bad, VerifyRegisterType() will run off the end of
3113 // the list and fail. It's legal, if silly, for arg_count to be zero.
Ian Rogersad0b3a32012-04-16 14:50:24 -07003114 const RegType& expected_type = reg_types_.GetComponentType(res_type, class_loader_);
Sebastien Hertz5243e912013-05-21 10:55:07 +02003115 uint32_t arg_count = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
3116 uint32_t arg[5];
3117 if (!is_range) {
3118 inst->GetArgs(arg);
3119 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08003120 for (size_t ui = 0; ui < arg_count; ui++) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003121 uint32_t get_reg = is_range ? inst->VRegC_3rc() + ui : arg[ui];
Ian Rogers0c4a5062012-02-03 15:18:59 -08003122 if (!work_line_->VerifyRegisterType(get_reg, expected_type)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003123 work_line_->SetResultRegisterType(reg_types_.Conflict());
Ian Rogers0c4a5062012-02-03 15:18:59 -08003124 return;
3125 }
3126 }
3127 // filled-array result goes into "result" register
Ian Rogers62342ec2013-06-11 10:26:37 -07003128 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
3129 work_line_->SetResultRegisterType(precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003130 }
3131 }
3132}
3133
Sebastien Hertz5243e912013-05-21 10:55:07 +02003134void MethodVerifier::VerifyAGet(const Instruction* inst,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003135 const RegType& insn_type, bool is_primitive) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003136 const RegType& index_type = work_line_->GetRegisterType(inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07003137 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003138 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07003139 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003140 const RegType& array_type = work_line_->GetRegisterType(inst->VRegB_23x());
Ian Rogers89310de2012-02-01 13:47:30 -08003141 if (array_type.IsZero()) {
3142 // Null array class; this code path will fail at runtime. Infer a merge-able type from the
3143 // instruction type. TODO: have a proper notion of bottom here.
3144 if (!is_primitive || insn_type.IsCategory1Types()) {
3145 // Reference or category 1
Sebastien Hertz5243e912013-05-21 10:55:07 +02003146 work_line_->SetRegisterType(inst->VRegA_23x(), reg_types_.Zero());
Ian Rogersd81871c2011-10-03 13:57:23 -07003147 } else {
Ian Rogers89310de2012-02-01 13:47:30 -08003148 // Category 2
Sebastien Hertz5243e912013-05-21 10:55:07 +02003149 work_line_->SetRegisterTypeWide(inst->VRegA_23x(), reg_types_.FromCat2ConstLo(0, false),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003150 reg_types_.FromCat2ConstHi(0, false));
Ian Rogers89310de2012-02-01 13:47:30 -08003151 }
jeffhaofc3144e2012-02-01 17:21:15 -08003152 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003153 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aget";
Ian Rogers89310de2012-02-01 13:47:30 -08003154 } else {
3155 /* verify the class */
Ian Rogersad0b3a32012-04-16 14:50:24 -07003156 const RegType& component_type = reg_types_.GetComponentType(array_type, class_loader_);
jeffhaofc3144e2012-02-01 17:21:15 -08003157 if (!component_type.IsReferenceTypes() && !is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07003158 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08003159 << " source for aget-object";
3160 } else if (component_type.IsNonZeroReferenceTypes() && is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07003161 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08003162 << " source for category 1 aget";
3163 } else if (is_primitive && !insn_type.Equals(component_type) &&
3164 !((insn_type.IsInteger() && component_type.IsFloat()) ||
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003165 (insn_type.IsLong() && component_type.IsDouble()))) {
3166 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array type " << array_type
3167 << " incompatible with aget of type " << insn_type;
Ian Rogers89310de2012-02-01 13:47:30 -08003168 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003169 // Use knowledge of the field type which is stronger than the type inferred from the
3170 // instruction, which can't differentiate object types and ints from floats, longs from
3171 // doubles.
3172 if (!component_type.IsLowHalf()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003173 work_line_->SetRegisterType(inst->VRegA_23x(), component_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003174 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003175 work_line_->SetRegisterTypeWide(inst->VRegA_23x(), component_type,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003176 component_type.HighHalf(&reg_types_));
3177 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003178 }
3179 }
3180 }
3181}
3182
Sebastien Hertz5243e912013-05-21 10:55:07 +02003183void MethodVerifier::VerifyAPut(const Instruction* inst,
Ian Rogersd81871c2011-10-03 13:57:23 -07003184 const RegType& insn_type, bool is_primitive) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003185 const RegType& index_type = work_line_->GetRegisterType(inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07003186 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003187 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07003188 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003189 const RegType& array_type = work_line_->GetRegisterType(inst->VRegB_23x());
Ian Rogers89310de2012-02-01 13:47:30 -08003190 if (array_type.IsZero()) {
3191 // Null array type; this code path will fail at runtime. Infer a merge-able type from the
3192 // instruction type.
jeffhaofc3144e2012-02-01 17:21:15 -08003193 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003194 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aput";
Ian Rogers89310de2012-02-01 13:47:30 -08003195 } else {
3196 /* verify the class */
Ian Rogersad0b3a32012-04-16 14:50:24 -07003197 const RegType& component_type = reg_types_.GetComponentType(array_type, class_loader_);
jeffhaofc3144e2012-02-01 17:21:15 -08003198 if (!component_type.IsReferenceTypes() && !is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07003199 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08003200 << " source for aput-object";
3201 } else if (component_type.IsNonZeroReferenceTypes() && is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07003202 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08003203 << " source for category 1 aput";
3204 } else if (is_primitive && !insn_type.Equals(component_type) &&
3205 !((insn_type.IsInteger() && component_type.IsFloat()) ||
3206 (insn_type.IsLong() && component_type.IsDouble()))) {
jeffhaod5347e02012-03-22 17:25:05 -07003207 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08003208 << " incompatible with aput of type " << insn_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07003209 } else {
Ian Rogers89310de2012-02-01 13:47:30 -08003210 // The instruction agrees with the type of array, confirm the value to be stored does too
3211 // Note: we use the instruction type (rather than the component type) for aput-object as
3212 // incompatible classes will be caught at runtime as an array store exception
Sebastien Hertz5243e912013-05-21 10:55:07 +02003213 work_line_->VerifyRegisterType(inst->VRegA_23x(), is_primitive ? component_type : insn_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07003214 }
3215 }
3216 }
3217}
3218
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003219mirror::Field* MethodVerifier::GetStaticField(int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08003220 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3221 // Check access to class
3222 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003223 if (klass_type.IsConflict()) { // bad class
3224 AppendToLastFailMessage(StringPrintf(" in attempt to access static field %d (%s) in %s",
3225 field_idx, dex_file_->GetFieldName(field_id),
3226 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers90040192011-12-16 08:54:29 -08003227 return NULL;
3228 }
Elliott Hughesb25c3f62012-03-26 16:35:06 -07003229 if (klass_type.IsUnresolvedTypes()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003230 return NULL; // Can't resolve Class so no more to do here, will do checking at runtime.
Ian Rogers90040192011-12-16 08:54:29 -08003231 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003232 mirror::Field* field = Runtime::Current()->GetClassLinker()->ResolveFieldJLS(*dex_file_, field_idx,
Ian Rogersad0b3a32012-04-16 14:50:24 -07003233 dex_cache_, class_loader_);
Ian Rogersd81871c2011-10-03 13:57:23 -07003234 if (field == NULL) {
Ian Rogers637c65b2013-05-31 11:46:00 -07003235 LOG(INFO) << "Unable to resolve static field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07003236 << dex_file_->GetFieldName(field_id) << ") in "
3237 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07003238 DCHECK(Thread::Current()->IsExceptionPending());
3239 Thread::Current()->ClearException();
3240 return NULL;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003241 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
3242 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003243 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access static field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07003244 << " from " << GetDeclaringClass();
Ian Rogersd81871c2011-10-03 13:57:23 -07003245 return NULL;
3246 } else if (!field->IsStatic()) {
3247 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field) << " to be static";
3248 return NULL;
3249 } else {
3250 return field;
3251 }
3252}
3253
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003254mirror::Field* MethodVerifier::GetInstanceField(const RegType& obj_type, int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08003255 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3256 // Check access to class
3257 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003258 if (klass_type.IsConflict()) {
3259 AppendToLastFailMessage(StringPrintf(" in attempt to access instance field %d (%s) in %s",
3260 field_idx, dex_file_->GetFieldName(field_id),
3261 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers90040192011-12-16 08:54:29 -08003262 return NULL;
3263 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003264 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers90040192011-12-16 08:54:29 -08003265 return NULL; // Can't resolve Class so no more to do here
3266 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003267 mirror::Field* field = Runtime::Current()->GetClassLinker()->ResolveFieldJLS(*dex_file_, field_idx,
Ian Rogersad0b3a32012-04-16 14:50:24 -07003268 dex_cache_, class_loader_);
Ian Rogersd81871c2011-10-03 13:57:23 -07003269 if (field == NULL) {
Ian Rogers637c65b2013-05-31 11:46:00 -07003270 LOG(INFO) << "Unable to resolve instance field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07003271 << dex_file_->GetFieldName(field_id) << ") in "
3272 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07003273 DCHECK(Thread::Current()->IsExceptionPending());
3274 Thread::Current()->ClearException();
3275 return NULL;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003276 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
3277 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003278 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access instance field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07003279 << " from " << GetDeclaringClass();
Ian Rogersd81871c2011-10-03 13:57:23 -07003280 return NULL;
3281 } else if (field->IsStatic()) {
3282 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field)
3283 << " to not be static";
3284 return NULL;
3285 } else if (obj_type.IsZero()) {
3286 // Cannot infer and check type, however, access will cause null pointer exception
3287 return field;
Ian Rogerse1758fe2012-04-19 11:31:15 -07003288 } else {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003289 mirror::Class* klass = field->GetDeclaringClass();
Ian Rogers637c65b2013-05-31 11:46:00 -07003290 const RegType& field_klass =
3291 reg_types_.FromClass(dex_file_->GetFieldDeclaringClassDescriptor(field_id),
Ian Rogers04f94f42013-06-10 15:09:26 -07003292 klass, klass->CannotBeAssignedFromOtherTypes());
Ian Rogersad0b3a32012-04-16 14:50:24 -07003293 if (obj_type.IsUninitializedTypes() &&
3294 (!IsConstructor() || GetDeclaringClass().Equals(obj_type) ||
3295 !field_klass.Equals(GetDeclaringClass()))) {
3296 // Field accesses through uninitialized references are only allowable for constructors where
3297 // the field is declared in this class
3298 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "cannot access instance field " << PrettyField(field)
3299 << " of a not fully initialized object within the context of "
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003300 << PrettyMethod(dex_method_idx_, *dex_file_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003301 return NULL;
3302 } else if (!field_klass.IsAssignableFrom(obj_type)) {
3303 // Trying to access C1.field1 using reference of type C2, which is neither C1 or a sub-class
3304 // of C1. For resolution to occur the declared class of the field must be compatible with
3305 // obj_type, we've discovered this wasn't so, so report the field didn't exist.
3306 Fail(VERIFY_ERROR_NO_FIELD) << "cannot access instance field " << PrettyField(field)
3307 << " from object of type " << obj_type;
3308 return NULL;
3309 } else {
3310 return field;
3311 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003312 }
3313}
3314
Sebastien Hertz5243e912013-05-21 10:55:07 +02003315void MethodVerifier::VerifyISGet(const Instruction* inst, const RegType& insn_type,
3316 bool is_primitive, bool is_static) {
3317 uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003318 mirror::Field* field;
Ian Rogersb94a27b2011-10-26 00:33:41 -07003319 if (is_static) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07003320 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003321 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003322 const RegType& object_type = work_line_->GetRegisterType(inst->VRegB_22c());
Ian Rogersf4028cc2011-11-02 14:56:39 -07003323 field = GetInstanceField(object_type, field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003324 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003325 const char* descriptor;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003326 mirror::ClassLoader* loader;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003327 if (field != NULL) {
3328 descriptor = FieldHelper(field).GetTypeDescriptor();
3329 loader = field->GetDeclaringClass()->GetClassLoader();
Ian Rogersf4028cc2011-11-02 14:56:39 -07003330 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003331 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3332 descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
3333 loader = class_loader_;
Ian Rogers0d604842012-04-16 14:50:24 -07003334 }
Ian Rogersb4903572012-10-11 11:52:56 -07003335 const RegType& field_type = reg_types_.FromDescriptor(loader, descriptor, false);
Sebastien Hertz5243e912013-05-21 10:55:07 +02003336 const uint32_t vregA = (is_static) ? inst->VRegA_21c() : inst->VRegA_22c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07003337 if (is_primitive) {
3338 if (field_type.Equals(insn_type) ||
3339 (field_type.IsFloat() && insn_type.IsIntegralTypes()) ||
3340 (field_type.IsDouble() && insn_type.IsLongTypes())) {
3341 // expected that read is of the correct primitive type or that int reads are reading
3342 // floats or long reads are reading doubles
3343 } else {
3344 // This is a global failure rather than a class change failure as the instructions and
3345 // the descriptors for the type should have been consistent within the same file at
3346 // compile time
3347 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
3348 << " to be of type '" << insn_type
3349 << "' but found type '" << field_type << "' in get";
Ian Rogersad0b3a32012-04-16 14:50:24 -07003350 return;
3351 }
3352 } else {
3353 if (!insn_type.IsAssignableFrom(field_type)) {
3354 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
3355 << " to be compatible with type '" << insn_type
3356 << "' but found type '" << field_type
3357 << "' in get-object";
Sebastien Hertz5243e912013-05-21 10:55:07 +02003358 work_line_->SetRegisterType(vregA, reg_types_.Conflict());
Ian Rogersad0b3a32012-04-16 14:50:24 -07003359 return;
3360 }
3361 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003362 if (!field_type.IsLowHalf()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003363 work_line_->SetRegisterType(vregA, field_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003364 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003365 work_line_->SetRegisterTypeWide(vregA, field_type, field_type.HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003366 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003367}
3368
Sebastien Hertz5243e912013-05-21 10:55:07 +02003369void MethodVerifier::VerifyISPut(const Instruction* inst, const RegType& insn_type,
3370 bool is_primitive, bool is_static) {
3371 uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003372 mirror::Field* field;
Ian Rogersb94a27b2011-10-26 00:33:41 -07003373 if (is_static) {
Ian Rogers55d249f2011-11-02 16:48:09 -07003374 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003375 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003376 const RegType& object_type = work_line_->GetRegisterType(inst->VRegB_22c());
Ian Rogers55d249f2011-11-02 16:48:09 -07003377 field = GetInstanceField(object_type, field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003378 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003379 const char* descriptor;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003380 mirror::ClassLoader* loader;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003381 if (field != NULL) {
3382 descriptor = FieldHelper(field).GetTypeDescriptor();
3383 loader = field->GetDeclaringClass()->GetClassLoader();
Ian Rogers55d249f2011-11-02 16:48:09 -07003384 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003385 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3386 descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
3387 loader = class_loader_;
3388 }
Ian Rogersb4903572012-10-11 11:52:56 -07003389 const RegType& field_type = reg_types_.FromDescriptor(loader, descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003390 if (field != NULL) {
3391 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
3392 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
3393 << " from other class " << GetDeclaringClass();
3394 return;
3395 }
3396 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02003397 const uint32_t vregA = (is_static) ? inst->VRegA_21c() : inst->VRegA_22c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07003398 if (is_primitive) {
3399 // Primitive field assignability rules are weaker than regular assignability rules
3400 bool instruction_compatible;
3401 bool value_compatible;
Sebastien Hertz5243e912013-05-21 10:55:07 +02003402 const RegType& value_type = work_line_->GetRegisterType(vregA);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003403 if (field_type.IsIntegralTypes()) {
3404 instruction_compatible = insn_type.IsIntegralTypes();
3405 value_compatible = value_type.IsIntegralTypes();
3406 } else if (field_type.IsFloat()) {
3407 instruction_compatible = insn_type.IsInteger(); // no [is]put-float, so expect [is]put-int
3408 value_compatible = value_type.IsFloatTypes();
3409 } else if (field_type.IsLong()) {
3410 instruction_compatible = insn_type.IsLong();
3411 value_compatible = value_type.IsLongTypes();
3412 } else if (field_type.IsDouble()) {
3413 instruction_compatible = insn_type.IsLong(); // no [is]put-double, so expect [is]put-long
3414 value_compatible = value_type.IsDoubleTypes();
Ian Rogers55d249f2011-11-02 16:48:09 -07003415 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003416 instruction_compatible = false; // reference field with primitive store
3417 value_compatible = false; // unused
Ian Rogersd81871c2011-10-03 13:57:23 -07003418 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003419 if (!instruction_compatible) {
3420 // This is a global failure rather than a class change failure as the instructions and
3421 // the descriptors for the type should have been consistent within the same file at
3422 // compile time
3423 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
3424 << " to be of type '" << insn_type
3425 << "' but found type '" << field_type
3426 << "' in put";
3427 return;
Ian Rogers55d249f2011-11-02 16:48:09 -07003428 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003429 if (!value_compatible) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003430 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
Ian Rogersad0b3a32012-04-16 14:50:24 -07003431 << " of type " << value_type
3432 << " but expected " << field_type
3433 << " for store to " << PrettyField(field) << " in put";
3434 return;
Ian Rogersd81871c2011-10-03 13:57:23 -07003435 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003436 } else {
3437 if (!insn_type.IsAssignableFrom(field_type)) {
3438 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
3439 << " to be compatible with type '" << insn_type
3440 << "' but found type '" << field_type
3441 << "' in put-object";
3442 return;
3443 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02003444 work_line_->VerifyRegisterType(vregA, field_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07003445 }
3446}
3447
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003448// Look for an instance field with this offset.
3449// TODO: we may speed up the search if offsets are sorted by doing a quick search.
3450static mirror::Field* FindInstanceFieldWithOffset(mirror::Class* klass,
3451 uint32_t field_offset)
3452 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
3453 mirror::ObjectArray<mirror::Field>* instance_fields = klass->GetIFields();
3454 if (instance_fields != NULL) {
3455 for (int32_t i = 0, e = instance_fields->GetLength(); i < e; ++i) {
3456 mirror::Field* field = instance_fields->Get(i);
3457 if (field->GetOffset().Uint32Value() == field_offset) {
3458 return field;
3459 }
3460 }
3461 }
3462 if (klass->GetSuperClass() != NULL) {
3463 return FindInstanceFieldWithOffset(klass->GetSuperClass(), field_offset);
3464 } else {
3465 return NULL;
3466 }
3467}
3468
3469void MethodVerifier::VerifyIGetQuick(const Instruction* inst, const RegType& insn_type,
3470 bool is_primitive) {
3471 DCHECK(Runtime::Current()->IsStarted());
3472 CHECK(accessed_field != NULL || invoked_method != NULL)
3473 << "We should not be verifying " << inst->Name();
3474 const RegType& object_type = work_line_->GetRegisterType(inst->VRegB_22c());
3475 mirror::Class* object_class = NULL;
3476 if (!object_type.IsUnresolvedTypes()) {
3477 object_class = object_type.GetClass();
3478 } else {
3479 // We need to resolve the class from its descriptor.
3480 const std::string& descriptor(object_type.GetDescriptor());
3481 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
3482 object_class = class_linker->FindClass(descriptor.c_str(), class_loader_);
3483 if (object_class == NULL) {
3484 Thread::Current()->ClearException();
3485 // Look for a system class
3486 object_class = class_linker->FindClass(descriptor.c_str(), NULL);
3487 }
3488 }
3489 CHECK(object_class != NULL) << "Cannot get Class* for type " << object_type;
3490 uint32_t field_offset = static_cast<uint32_t>(inst->VRegC_22c());
3491 mirror::Field* field = FindInstanceFieldWithOffset(object_class, field_offset);
3492 CHECK(field != NULL);
3493 // TODO: we should move the code below to FindAccessedFieldAtDexPc. Once the
3494 // method is verified, we could access the information we need from register
3495 // lines for the dex pc we are looking for.
3496 if (accessed_field != NULL && work_insn_idx_ == interesting_dex_pc_) {
3497 // We've been requested to tell which field is accessed at this dex pc.
3498 *accessed_field = field;
3499 }
3500 const char* descriptor = FieldHelper(field).GetTypeDescriptor();
3501 mirror::ClassLoader* loader = field->GetDeclaringClass()->GetClassLoader();
3502 const RegType& field_type = reg_types_.FromDescriptor(loader, descriptor, false);
3503 const uint32_t vregA = inst->VRegA_22c();
3504 if (is_primitive) {
3505 if (field_type.Equals(insn_type) ||
3506 (field_type.IsFloat() && insn_type.IsIntegralTypes()) ||
3507 (field_type.IsDouble() && insn_type.IsLongTypes())) {
3508 // expected that read is of the correct primitive type or that int reads are reading
3509 // floats or long reads are reading doubles
3510 } else {
3511 // This is a global failure rather than a class change failure as the instructions and
3512 // the descriptors for the type should have been consistent within the same file at
3513 // compile time
3514 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
3515 << " to be of type '" << insn_type
3516 << "' but found type '" << field_type << "' in get";
3517 return;
3518 }
3519 } else {
3520 if (!insn_type.IsAssignableFrom(field_type)) {
3521 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
3522 << " to be compatible with type '" << insn_type
3523 << "' but found type '" << field_type
3524 << "' in get-object";
3525 work_line_->SetRegisterType(vregA, reg_types_.Conflict());
3526 return;
3527 }
3528 }
3529 if (!field_type.IsLowHalf()) {
3530 work_line_->SetRegisterType(vregA, field_type);
3531 } else {
3532 work_line_->SetRegisterTypeWide(vregA, field_type, field_type.HighHalf(&reg_types_));
3533 }
3534}
3535
3536void MethodVerifier::VerifyIPutQuick(const Instruction* inst, const RegType& insn_type,
3537 bool is_primitive) {
3538 DCHECK(Runtime::Current()->IsStarted());
3539 CHECK(accessed_field != NULL || invoked_method != NULL)
3540 << "We should not be verifying " << inst->Name();
3541 const RegType& object_type = work_line_->GetRegisterType(inst->VRegB_22c());
3542 mirror::Class* object_class = NULL;
3543 if (!object_type.IsUnresolvedTypes()) {
3544 object_class = object_type.GetClass();
3545 } else {
3546 // We need to resolve the class from its descriptor.
3547 const std::string& descriptor(object_type.GetDescriptor());
3548 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
3549 object_class = class_linker->FindClass(descriptor.c_str(), class_loader_);
3550 if (object_class == NULL) {
3551 Thread::Current()->ClearException();
3552 // Look for a system class
3553 object_class = class_linker->FindClass(descriptor.c_str(), NULL);
3554 }
3555 }
3556 CHECK(object_class != NULL) << "Cannot get Class* for type " << object_type;
3557 uint32_t field_offset = static_cast<uint32_t>(inst->VRegC_22c());
3558 mirror::Field* field = FindInstanceFieldWithOffset(object_class, field_offset);
3559 CHECK(field != NULL);
3560 // TODO: like VerifyIGetQuick, we should move the code below to
3561 // FindAccessedFieldAtDexPc.
3562 if (accessed_field != NULL && work_insn_idx_ == interesting_dex_pc_) {
3563 // We've been requested to tell which field is accessed at this dex pc.
3564 *accessed_field = field;
3565 }
3566 const char* descriptor = FieldHelper(field).GetTypeDescriptor();
3567 mirror::ClassLoader* loader = field->GetDeclaringClass()->GetClassLoader();
3568 const RegType& field_type = reg_types_.FromDescriptor(loader, descriptor, false);
3569 if (field != NULL) {
3570 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
3571 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
3572 << " from other class " << GetDeclaringClass();
3573 return;
3574 }
3575 }
3576 const uint32_t vregA = inst->VRegA_22c();
3577 if (is_primitive) {
3578 // Primitive field assignability rules are weaker than regular assignability rules
3579 bool instruction_compatible;
3580 bool value_compatible;
3581 const RegType& value_type = work_line_->GetRegisterType(vregA);
3582 if (field_type.IsIntegralTypes()) {
3583 instruction_compatible = insn_type.IsIntegralTypes();
3584 value_compatible = value_type.IsIntegralTypes();
3585 } else if (field_type.IsFloat()) {
3586 instruction_compatible = insn_type.IsInteger(); // no [is]put-float, so expect [is]put-int
3587 value_compatible = value_type.IsFloatTypes();
3588 } else if (field_type.IsLong()) {
3589 instruction_compatible = insn_type.IsLong();
3590 value_compatible = value_type.IsLongTypes();
3591 } else if (field_type.IsDouble()) {
3592 instruction_compatible = insn_type.IsLong(); // no [is]put-double, so expect [is]put-long
3593 value_compatible = value_type.IsDoubleTypes();
3594 } else {
3595 instruction_compatible = false; // reference field with primitive store
3596 value_compatible = false; // unused
3597 }
3598 if (!instruction_compatible) {
3599 // This is a global failure rather than a class change failure as the instructions and
3600 // the descriptors for the type should have been consistent within the same file at
3601 // compile time
3602 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
3603 << " to be of type '" << insn_type
3604 << "' but found type '" << field_type
3605 << "' in put";
3606 return;
3607 }
3608 if (!value_compatible) {
3609 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
3610 << " of type " << value_type
3611 << " but expected " << field_type
3612 << " for store to " << PrettyField(field) << " in put";
3613 return;
3614 }
3615 } else {
3616 if (!insn_type.IsAssignableFrom(field_type)) {
3617 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
3618 << " to be compatible with type '" << insn_type
3619 << "' but found type '" << field_type
3620 << "' in put-object";
3621 return;
3622 }
3623 work_line_->VerifyRegisterType(vregA, field_type);
3624 }
3625}
3626
Ian Rogers776ac1f2012-04-13 23:36:36 -07003627bool MethodVerifier::CheckNotMoveException(const uint16_t* insns, int insn_idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003628 if ((insns[insn_idx] & 0xff) == Instruction::MOVE_EXCEPTION) {
jeffhaod5347e02012-03-22 17:25:05 -07003629 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-exception";
Ian Rogersd81871c2011-10-03 13:57:23 -07003630 return false;
3631 }
3632 return true;
3633}
3634
Ian Rogers776ac1f2012-04-13 23:36:36 -07003635bool MethodVerifier::UpdateRegisters(uint32_t next_insn, const RegisterLine* merge_line) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003636 bool changed = true;
3637 RegisterLine* target_line = reg_table_.GetLine(next_insn);
3638 if (!insn_flags_[next_insn].IsVisitedOrChanged()) {
jeffhaobdb76512011-09-07 11:43:16 -07003639 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003640 * We haven't processed this instruction before, and we haven't touched the registers here, so
3641 * there's nothing to "merge". Copy the registers over and mark it as changed. (This is the
3642 * only way a register can transition out of "unknown", so this is not just an optimization.)
jeffhaobdb76512011-09-07 11:43:16 -07003643 */
Ian Rogersd81871c2011-10-03 13:57:23 -07003644 target_line->CopyFromLine(merge_line);
jeffhaobdb76512011-09-07 11:43:16 -07003645 } else {
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003646 UniquePtr<RegisterLine> copy(gDebugVerify ? new RegisterLine(target_line->NumRegs(), this) : NULL);
3647 if (gDebugVerify) {
3648 copy->CopyFromLine(target_line);
3649 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003650 changed = target_line->MergeRegisters(merge_line);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003651 if (have_pending_hard_failure_) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003652 return false;
jeffhaobdb76512011-09-07 11:43:16 -07003653 }
Ian Rogers2c8a8572011-10-24 17:11:36 -07003654 if (gDebugVerify && changed) {
Elliott Hughes398f64b2012-03-26 18:05:48 -07003655 LogVerifyInfo() << "Merging at [" << reinterpret_cast<void*>(work_insn_idx_) << "]"
Elliott Hughesc073b072012-05-24 19:29:17 -07003656 << " to [" << reinterpret_cast<void*>(next_insn) << "]: " << "\n"
3657 << *copy.get() << " MERGE\n"
3658 << *merge_line << " ==\n"
3659 << *target_line << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07003660 }
3661 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003662 if (changed) {
3663 insn_flags_[next_insn].SetChanged();
jeffhaobdb76512011-09-07 11:43:16 -07003664 }
3665 return true;
3666}
3667
Ian Rogers7b3ddd22013-02-21 15:19:52 -08003668InstructionFlags* MethodVerifier::CurrentInsnFlags() {
Ian Rogers776ac1f2012-04-13 23:36:36 -07003669 return &insn_flags_[work_insn_idx_];
3670}
3671
Ian Rogersad0b3a32012-04-16 14:50:24 -07003672const RegType& MethodVerifier::GetMethodReturnType() {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003673 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003674 const DexFile::ProtoId& proto_id = dex_file_->GetMethodPrototype(method_id);
3675 uint16_t return_type_idx = proto_id.return_type_idx_;
3676 const char* descriptor = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(return_type_idx));
Ian Rogersb4903572012-10-11 11:52:56 -07003677 return reg_types_.FromDescriptor(class_loader_, descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003678}
3679
3680const RegType& MethodVerifier::GetDeclaringClass() {
Ian Rogers637c65b2013-05-31 11:46:00 -07003681 if (declaring_class_ == NULL) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003682 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003683 const char* descriptor = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(method_id.class_idx_));
Ian Rogers637c65b2013-05-31 11:46:00 -07003684 if (mirror_method_ != NULL) {
3685 mirror::Class* klass = mirror_method_->GetDeclaringClass();
Ian Rogers04f94f42013-06-10 15:09:26 -07003686 declaring_class_ = &reg_types_.FromClass(descriptor, klass,
3687 klass->CannotBeAssignedFromOtherTypes());
Ian Rogers637c65b2013-05-31 11:46:00 -07003688 } else {
3689 declaring_class_ = &reg_types_.FromDescriptor(class_loader_, descriptor, false);
3690 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003691 }
Ian Rogers637c65b2013-05-31 11:46:00 -07003692 return *declaring_class_;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003693}
3694
Ian Rogers776ac1f2012-04-13 23:36:36 -07003695void MethodVerifier::ComputeGcMapSizes(size_t* gc_points, size_t* ref_bitmap_bits,
Ian Rogersd81871c2011-10-03 13:57:23 -07003696 size_t* log2_max_gc_pc) {
3697 size_t local_gc_points = 0;
3698 size_t max_insn = 0;
3699 size_t max_ref_reg = -1;
3700 for (size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003701 if (insn_flags_[i].IsCompileTimeInfoPoint()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003702 local_gc_points++;
3703 max_insn = i;
3704 RegisterLine* line = reg_table_.GetLine(i);
Ian Rogers84fa0742011-10-25 18:13:30 -07003705 max_ref_reg = line->GetMaxNonZeroReferenceReg(max_ref_reg);
jeffhaobdb76512011-09-07 11:43:16 -07003706 }
3707 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003708 *gc_points = local_gc_points;
3709 *ref_bitmap_bits = max_ref_reg + 1; // if max register is 0 we need 1 bit to encode (ie +1)
3710 size_t i = 0;
Ian Rogers6b0870d2011-12-15 19:38:12 -08003711 while ((1U << i) <= max_insn) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003712 i++;
3713 }
3714 *log2_max_gc_pc = i;
jeffhaobdb76512011-09-07 11:43:16 -07003715}
3716
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07003717MethodVerifier::MethodSafeCastSet* MethodVerifier::GenerateSafeCastSet() {
3718 /*
3719 * Walks over the method code and adds any cast instructions in which
3720 * the type cast is implicit to a set, which is used in the code generation
3721 * to elide these casts.
3722 */
3723 if (!failure_messages_.empty()) {
3724 return NULL;
3725 }
3726 UniquePtr<MethodSafeCastSet> mscs;
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07003727 const Instruction* inst = Instruction::At(code_item_->insns_);
3728 const Instruction* end = Instruction::At(code_item_->insns_ +
Ian Rogersfae370a2013-06-05 08:33:27 -07003729 code_item_->insns_size_in_code_units_);
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07003730
3731 for (; inst < end; inst = inst->Next()) {
Ian Rogersfae370a2013-06-05 08:33:27 -07003732 if (Instruction::CHECK_CAST != inst->Opcode()) {
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07003733 continue;
Ian Rogersfae370a2013-06-05 08:33:27 -07003734 }
3735 uint32_t dex_pc = inst->GetDexPc(code_item_->insns_);
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07003736 RegisterLine* line = reg_table_.GetLine(dex_pc);
Ian Rogersfae370a2013-06-05 08:33:27 -07003737 const RegType& reg_type(line->GetRegisterType(inst->VRegA_21c()));
3738 const RegType& cast_type = ResolveClassAndCheckAccess(inst->VRegB_21c());
Ian Rogers16e3d2c2013-06-07 12:57:00 -07003739 if (cast_type.IsStrictlyAssignableFrom(reg_type)) {
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07003740 if (mscs.get() == NULL) {
3741 mscs.reset(new MethodSafeCastSet());
3742 }
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07003743 mscs->insert(dex_pc);
3744 }
3745 }
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07003746 return mscs.release();
3747}
3748
Ian Rogersd0583802013-06-01 10:51:46 -07003749MethodVerifier::PcToConcreteMethodMap* MethodVerifier::GenerateDevirtMap() {
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003750
3751 // It is risky to rely on reg_types for sharpening in cases of soft
3752 // verification, we might end up sharpening to a wrong implementation. Just abort.
3753 if (!failure_messages_.empty()) {
3754 return NULL;
3755 }
3756
Ian Rogersd0583802013-06-01 10:51:46 -07003757 UniquePtr<PcToConcreteMethodMap> pc_to_concrete_method_map;
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003758 const uint16_t* insns = code_item_->insns_ ;
3759 const Instruction* inst = Instruction::At(insns);
Ian Rogersd0583802013-06-01 10:51:46 -07003760 const Instruction* end = Instruction::At(insns + code_item_->insns_size_in_code_units_);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003761
Ian Rogersd0583802013-06-01 10:51:46 -07003762 for (; inst < end; inst = inst->Next()) {
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003763 bool is_virtual = (inst->Opcode() == Instruction::INVOKE_VIRTUAL) ||
3764 (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE);
3765 bool is_interface = (inst->Opcode() == Instruction::INVOKE_INTERFACE) ||
3766 (inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE);
3767
Ian Rogersd0583802013-06-01 10:51:46 -07003768 if(!is_interface && !is_virtual) {
Dragos Sbirlea29e2e7e2013-05-22 14:52:11 -07003769 continue;
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003770 }
Ian Rogersd0583802013-06-01 10:51:46 -07003771 // Get reg type for register holding the reference to the object that will be dispatched upon.
3772 uint32_t dex_pc = inst->GetDexPc(insns);
3773 RegisterLine* line = reg_table_.GetLine(dex_pc);
3774 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE) ||
3775 (inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE);
3776 const RegType&
3777 reg_type(line->GetRegisterType(is_range ? inst->VRegC_3rc() : inst->VRegC_35c()));
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003778
Ian Rogersd0583802013-06-01 10:51:46 -07003779 if (!reg_type.HasClass()) {
3780 // We will compute devirtualization information only when we know the Class of the reg type.
3781 continue;
3782 }
3783 mirror::Class* reg_class = reg_type.GetClass();
3784 if (reg_class->IsInterface()) {
3785 // We can't devirtualize when the known type of the register is an interface.
3786 continue;
3787 }
3788 if (reg_class->IsAbstract() && !reg_class->IsArrayClass()) {
3789 // We can't devirtualize abstract classes except on arrays of abstract classes.
3790 continue;
3791 }
3792 mirror::AbstractMethod* abstract_method =
3793 dex_cache_->GetResolvedMethod(is_range ? inst->VRegB_3rc() : inst->VRegB_35c());
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003794 if(abstract_method == NULL) {
3795 // If the method is not found in the cache this means that it was never found
3796 // by ResolveMethodAndCheckAccess() called when verifying invoke_*.
3797 continue;
3798 }
3799 // Find the concrete method.
3800 mirror::AbstractMethod* concrete_method = NULL;
3801 if (is_interface) {
3802 concrete_method = reg_type.GetClass()->FindVirtualMethodForInterface(abstract_method);
3803 }
3804 if (is_virtual) {
3805 concrete_method = reg_type.GetClass()->FindVirtualMethodForVirtual(abstract_method);
3806 }
Ian Rogersd0583802013-06-01 10:51:46 -07003807 if (concrete_method == NULL || concrete_method->IsAbstract()) {
3808 // In cases where concrete_method is not found, or is abstract, continue to the next invoke.
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003809 continue;
3810 }
Ian Rogersd0583802013-06-01 10:51:46 -07003811 if (reg_type.IsPreciseReference() || concrete_method->IsFinal() ||
3812 concrete_method->GetDeclaringClass()->IsFinal()) {
3813 // If we knew exactly the class being dispatched upon, or if the target method cannot be
3814 // overridden record the target to be used in the compiler driver.
3815 if (pc_to_concrete_method_map.get() == NULL) {
3816 pc_to_concrete_method_map.reset(new PcToConcreteMethodMap());
3817 }
3818 CompilerDriver::MethodReference concrete_ref(
3819 concrete_method->GetDeclaringClass()->GetDexCache()->GetDexFile(),
3820 concrete_method->GetDexMethodIndex());
3821 pc_to_concrete_method_map->Put(dex_pc, concrete_ref);
3822 }
Dragos Sbirlea29e2e7e2013-05-22 14:52:11 -07003823 }
Ian Rogersd0583802013-06-01 10:51:46 -07003824 return pc_to_concrete_method_map.release();
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003825}
3826
Ian Rogers776ac1f2012-04-13 23:36:36 -07003827const std::vector<uint8_t>* MethodVerifier::GenerateGcMap() {
Ian Rogersd81871c2011-10-03 13:57:23 -07003828 size_t num_entries, ref_bitmap_bits, pc_bits;
3829 ComputeGcMapSizes(&num_entries, &ref_bitmap_bits, &pc_bits);
3830 // There's a single byte to encode the size of each bitmap
jeffhao60f83e32012-02-13 17:16:30 -08003831 if (ref_bitmap_bits >= (8 /* bits per byte */ * 8192 /* 13-bit size */ )) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003832 // TODO: either a better GC map format or per method failures
jeffhaod5347e02012-03-22 17:25:05 -07003833 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot encode GC map for method with "
Ian Rogersd81871c2011-10-03 13:57:23 -07003834 << ref_bitmap_bits << " registers";
jeffhaobdb76512011-09-07 11:43:16 -07003835 return NULL;
3836 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003837 size_t ref_bitmap_bytes = (ref_bitmap_bits + 7) / 8;
3838 // There are 2 bytes to encode the number of entries
3839 if (num_entries >= 65536) {
3840 // TODO: either a better GC map format or per method failures
jeffhaod5347e02012-03-22 17:25:05 -07003841 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot encode GC map for method with "
Ian Rogersd81871c2011-10-03 13:57:23 -07003842 << num_entries << " entries";
jeffhaobdb76512011-09-07 11:43:16 -07003843 return NULL;
3844 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003845 size_t pc_bytes;
jeffhaod1f0fde2011-09-08 17:25:33 -07003846 RegisterMapFormat format;
Ian Rogers6b0870d2011-12-15 19:38:12 -08003847 if (pc_bits <= 8) {
jeffhaod1f0fde2011-09-08 17:25:33 -07003848 format = kRegMapFormatCompact8;
Ian Rogersd81871c2011-10-03 13:57:23 -07003849 pc_bytes = 1;
Ian Rogers6b0870d2011-12-15 19:38:12 -08003850 } else if (pc_bits <= 16) {
jeffhaod1f0fde2011-09-08 17:25:33 -07003851 format = kRegMapFormatCompact16;
Ian Rogersd81871c2011-10-03 13:57:23 -07003852 pc_bytes = 2;
jeffhaoa0a764a2011-09-16 10:43:38 -07003853 } else {
Ian Rogersd81871c2011-10-03 13:57:23 -07003854 // TODO: either a better GC map format or per method failures
jeffhaod5347e02012-03-22 17:25:05 -07003855 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot encode GC map for method with "
Ian Rogersd81871c2011-10-03 13:57:23 -07003856 << (1 << pc_bits) << " instructions (number is rounded up to nearest power of 2)";
3857 return NULL;
3858 }
Elliott Hughesb25c3f62012-03-26 16:35:06 -07003859 size_t table_size = ((pc_bytes + ref_bitmap_bytes) * num_entries) + 4;
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003860 std::vector<uint8_t>* table = new std::vector<uint8_t>;
Ian Rogersd81871c2011-10-03 13:57:23 -07003861 if (table == NULL) {
jeffhaod5347e02012-03-22 17:25:05 -07003862 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Failed to encode GC map (size=" << table_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07003863 return NULL;
3864 }
Ian Rogers39ebcb82013-05-30 16:57:23 -07003865 table->reserve(table_size);
Ian Rogersd81871c2011-10-03 13:57:23 -07003866 // Write table header
Ian Rogers46c6bb22012-09-18 13:47:36 -07003867 table->push_back(format | ((ref_bitmap_bytes >> DexPcToReferenceMap::kRegMapFormatShift) &
3868 ~DexPcToReferenceMap::kRegMapFormatMask));
jeffhao60f83e32012-02-13 17:16:30 -08003869 table->push_back(ref_bitmap_bytes & 0xFF);
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003870 table->push_back(num_entries & 0xFF);
3871 table->push_back((num_entries >> 8) & 0xFF);
Ian Rogersd81871c2011-10-03 13:57:23 -07003872 // Write table data
Ian Rogersd81871c2011-10-03 13:57:23 -07003873 for (size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003874 if (insn_flags_[i].IsCompileTimeInfoPoint()) {
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003875 table->push_back(i & 0xFF);
Ian Rogersd81871c2011-10-03 13:57:23 -07003876 if (pc_bytes == 2) {
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003877 table->push_back((i >> 8) & 0xFF);
Ian Rogersd81871c2011-10-03 13:57:23 -07003878 }
3879 RegisterLine* line = reg_table_.GetLine(i);
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003880 line->WriteReferenceBitMap(*table, ref_bitmap_bytes);
Ian Rogersd81871c2011-10-03 13:57:23 -07003881 }
3882 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003883 DCHECK_EQ(table->size(), table_size);
Ian Rogersd81871c2011-10-03 13:57:23 -07003884 return table;
3885}
jeffhaoa0a764a2011-09-16 10:43:38 -07003886
Ian Rogers776ac1f2012-04-13 23:36:36 -07003887void MethodVerifier::VerifyGcMap(const std::vector<uint8_t>& data) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003888 // Check that for every GC point there is a map entry, there aren't entries for non-GC points,
3889 // that the table data is well formed and all references are marked (or not) in the bitmap
Ian Rogers46c6bb22012-09-18 13:47:36 -07003890 DexPcToReferenceMap map(&data[0], data.size());
Ian Rogersd81871c2011-10-03 13:57:23 -07003891 size_t map_index = 0;
Elliott Hughesb25c3f62012-03-26 16:35:06 -07003892 for (size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003893 const uint8_t* reg_bitmap = map.FindBitMap(i, false);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003894 if (insn_flags_[i].IsCompileTimeInfoPoint()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003895 CHECK_LT(map_index, map.NumEntries());
Ian Rogers46c6bb22012-09-18 13:47:36 -07003896 CHECK_EQ(map.GetDexPc(map_index), i);
Ian Rogersd81871c2011-10-03 13:57:23 -07003897 CHECK_EQ(map.GetBitMap(map_index), reg_bitmap);
3898 map_index++;
3899 RegisterLine* line = reg_table_.GetLine(i);
Elliott Hughesb25c3f62012-03-26 16:35:06 -07003900 for (size_t j = 0; j < code_item_->registers_size_; j++) {
Ian Rogers84fa0742011-10-25 18:13:30 -07003901 if (line->GetRegisterType(j).IsNonZeroReferenceTypes()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003902 CHECK_LT(j / 8, map.RegWidth());
3903 CHECK_EQ((reg_bitmap[j / 8] >> (j % 8)) & 1, 1);
3904 } else if ((j / 8) < map.RegWidth()) {
3905 CHECK_EQ((reg_bitmap[j / 8] >> (j % 8)) & 1, 0);
3906 } else {
3907 // If a register doesn't contain a reference then the bitmap may be shorter than the line
3908 }
3909 }
3910 } else {
3911 CHECK(reg_bitmap == NULL);
3912 }
3913 }
3914}
jeffhaoa0a764a2011-09-16 10:43:38 -07003915
Ian Rogers637c65b2013-05-31 11:46:00 -07003916void MethodVerifier::SetDexGcMap(CompilerDriver::MethodReference ref,
3917 const std::vector<uint8_t>& gc_map) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003918 {
Ian Rogers637c65b2013-05-31 11:46:00 -07003919 WriterMutexLock mu(Thread::Current(), *dex_gc_maps_lock_);
Ian Rogers0c7abda2012-09-19 13:33:42 -07003920 DexGcMapTable::iterator it = dex_gc_maps_->find(ref);
3921 if (it != dex_gc_maps_->end()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003922 delete it->second;
Ian Rogers0c7abda2012-09-19 13:33:42 -07003923 dex_gc_maps_->erase(it);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003924 }
Ian Rogers0c7abda2012-09-19 13:33:42 -07003925 dex_gc_maps_->Put(ref, &gc_map);
Brian Carlstrom73a15f42012-01-17 18:14:39 -08003926 }
Ian Rogers39ebcb82013-05-30 16:57:23 -07003927 DCHECK(GetDexGcMap(ref) != NULL);
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003928}
3929
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07003930
Ian Rogersfae370a2013-06-05 08:33:27 -07003931void MethodVerifier::SetSafeCastMap(CompilerDriver::MethodReference ref,
3932 const MethodSafeCastSet* cast_set) {
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07003933 MutexLock mu(Thread::Current(), *safecast_map_lock_);
3934 SafeCastMap::iterator it = safecast_map_->find(ref);
3935 if (it != safecast_map_->end()) {
3936 delete it->second;
3937 safecast_map_->erase(it);
3938 }
3939
3940 safecast_map_->Put(ref, cast_set);
3941 CHECK(safecast_map_->find(ref) != safecast_map_->end());
3942}
3943
3944bool MethodVerifier::IsSafeCast(CompilerDriver::MethodReference ref, uint32_t pc) {
3945 MutexLock mu(Thread::Current(), *safecast_map_lock_);
3946 SafeCastMap::const_iterator it = safecast_map_->find(ref);
3947 if (it == safecast_map_->end()) {
3948 return false;
3949 }
3950
3951 // Look up the cast address in the set of safe casts
3952 MethodVerifier::MethodSafeCastSet::const_iterator cast_it = it->second->find(pc);
3953 return cast_it != it->second->end();
3954}
3955
Ian Rogers637c65b2013-05-31 11:46:00 -07003956const std::vector<uint8_t>* MethodVerifier::GetDexGcMap(CompilerDriver::MethodReference ref) {
3957 ReaderMutexLock mu(Thread::Current(), *dex_gc_maps_lock_);
3958 DexGcMapTable::const_iterator it = dex_gc_maps_->find(ref);
3959 if (it == dex_gc_maps_->end()) {
3960 LOG(WARNING) << "Didn't find GC map for: " << PrettyMethod(ref.dex_method_index, *ref.dex_file);
3961 return NULL;
3962 }
3963 CHECK(it->second != NULL);
3964 return it->second;
3965}
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003966
Ian Rogers637c65b2013-05-31 11:46:00 -07003967void MethodVerifier::SetDevirtMap(CompilerDriver::MethodReference ref,
Ian Rogersd0583802013-06-01 10:51:46 -07003968 const PcToConcreteMethodMap* devirt_map) {
Ian Rogers637c65b2013-05-31 11:46:00 -07003969 WriterMutexLock mu(Thread::Current(), *devirt_maps_lock_);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003970 DevirtualizationMapTable::iterator it = devirt_maps_->find(ref);
3971 if (it != devirt_maps_->end()) {
3972 delete it->second;
3973 devirt_maps_->erase(it);
3974 }
3975
3976 devirt_maps_->Put(ref, devirt_map);
3977 CHECK(devirt_maps_->find(ref) != devirt_maps_->end());
3978}
3979
Ian Rogerse3cd2f02013-05-24 15:32:56 -07003980const CompilerDriver::MethodReference* MethodVerifier::GetDevirtMap(const CompilerDriver::MethodReference& ref,
3981 uint32_t dex_pc) {
Ian Rogers637c65b2013-05-31 11:46:00 -07003982 ReaderMutexLock mu(Thread::Current(), *devirt_maps_lock_);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003983 DevirtualizationMapTable::const_iterator it = devirt_maps_->find(ref);
3984 if (it == devirt_maps_->end()) {
3985 return NULL;
3986 }
3987
3988 // Look up the PC in the map, get the concrete method to execute and return its reference.
Ian Rogersd0583802013-06-01 10:51:46 -07003989 MethodVerifier::PcToConcreteMethodMap::const_iterator pc_to_concrete_method = it->second->find(dex_pc);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003990 if(pc_to_concrete_method != it->second->end()) {
3991 return &(pc_to_concrete_method->second);
3992 } else {
3993 return NULL;
3994 }
3995}
3996
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003997std::vector<int32_t> MethodVerifier::DescribeVRegs(uint32_t dex_pc) {
3998 RegisterLine* line = reg_table_.GetLine(dex_pc);
3999 std::vector<int32_t> result;
4000 for (size_t i = 0; i < line->NumRegs(); ++i) {
4001 const RegType& type = line->GetRegisterType(i);
4002 if (type.IsConstant()) {
4003 result.push_back(type.IsPreciseConstant() ? kConstant : kImpreciseConstant);
4004 result.push_back(type.ConstantValue());
4005 } else if (type.IsConstantLo()) {
4006 result.push_back(type.IsPreciseConstantLo() ? kConstant : kImpreciseConstant);
4007 result.push_back(type.ConstantValueLo());
4008 } else if (type.IsConstantHi()) {
4009 result.push_back(type.IsPreciseConstantHi() ? kConstant : kImpreciseConstant);
4010 result.push_back(type.ConstantValueHi());
4011 } else if (type.IsIntegralTypes()) {
4012 result.push_back(kIntVReg);
4013 result.push_back(0);
4014 } else if (type.IsFloat()) {
4015 result.push_back(kFloatVReg);
4016 result.push_back(0);
4017 } else if (type.IsLong()) {
4018 result.push_back(kLongLoVReg);
4019 result.push_back(0);
4020 result.push_back(kLongHiVReg);
4021 result.push_back(0);
4022 ++i;
4023 } else if (type.IsDouble()) {
4024 result.push_back(kDoubleLoVReg);
4025 result.push_back(0);
4026 result.push_back(kDoubleHiVReg);
4027 result.push_back(0);
4028 ++i;
4029 } else if (type.IsUndefined() || type.IsConflict() || type.IsHighHalf()) {
4030 result.push_back(kUndefined);
4031 result.push_back(0);
4032 } else {
Ian Rogers7b3ddd22013-02-21 15:19:52 -08004033 CHECK(type.IsNonZeroReferenceTypes());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004034 result.push_back(kReferenceVReg);
4035 result.push_back(0);
4036 }
4037 }
4038 return result;
4039}
4040
Ian Rogers637c65b2013-05-31 11:46:00 -07004041ReaderWriterMutex* MethodVerifier::dex_gc_maps_lock_ = NULL;
Ian Rogers0c7abda2012-09-19 13:33:42 -07004042MethodVerifier::DexGcMapTable* MethodVerifier::dex_gc_maps_ = NULL;
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004043
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07004044Mutex* MethodVerifier::safecast_map_lock_ = NULL;
4045MethodVerifier::SafeCastMap* MethodVerifier::safecast_map_ = NULL;
4046
Ian Rogers637c65b2013-05-31 11:46:00 -07004047ReaderWriterMutex* MethodVerifier::devirt_maps_lock_ = NULL;
Sameer Abu Asal02c42232013-04-30 12:09:45 -07004048MethodVerifier::DevirtualizationMapTable* MethodVerifier::devirt_maps_ = NULL;
4049
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004050Mutex* MethodVerifier::rejected_classes_lock_ = NULL;
4051MethodVerifier::RejectedClassesTable* MethodVerifier::rejected_classes_ = NULL;
4052
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004053void MethodVerifier::Init() {
Ian Rogers637c65b2013-05-31 11:46:00 -07004054 dex_gc_maps_lock_ = new ReaderWriterMutex("verifier GC maps lock");
Ian Rogers50b35e22012-10-04 10:09:15 -07004055 Thread* self = Thread::Current();
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004056 {
Ian Rogers637c65b2013-05-31 11:46:00 -07004057 WriterMutexLock mu(self, *dex_gc_maps_lock_);
Ian Rogers0c7abda2012-09-19 13:33:42 -07004058 dex_gc_maps_ = new MethodVerifier::DexGcMapTable;
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004059 }
4060
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07004061 safecast_map_lock_ = new Mutex("verifier Cast Elision lock");
4062 {
4063 MutexLock mu(self, *safecast_map_lock_);
4064 safecast_map_ = new MethodVerifier::SafeCastMap();
4065 }
4066
Ian Rogers637c65b2013-05-31 11:46:00 -07004067 devirt_maps_lock_ = new ReaderWriterMutex("verifier Devirtualization lock");
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07004068
Sameer Abu Asal02c42232013-04-30 12:09:45 -07004069 {
Ian Rogers637c65b2013-05-31 11:46:00 -07004070 WriterMutexLock mu(self, *devirt_maps_lock_);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07004071 devirt_maps_ = new MethodVerifier::DevirtualizationMapTable();
4072 }
4073
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004074 rejected_classes_lock_ = new Mutex("verifier rejected classes lock");
4075 {
Ian Rogers50b35e22012-10-04 10:09:15 -07004076 MutexLock mu(self, *rejected_classes_lock_);
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004077 rejected_classes_ = new MethodVerifier::RejectedClassesTable;
4078 }
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08004079 art::verifier::RegTypeCache::Init();
Brian Carlstrome7d856b2012-01-11 18:10:55 -08004080}
4081
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004082void MethodVerifier::Shutdown() {
Ian Rogers50b35e22012-10-04 10:09:15 -07004083 Thread* self = Thread::Current();
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004084 {
Ian Rogers637c65b2013-05-31 11:46:00 -07004085 WriterMutexLock mu(self, *dex_gc_maps_lock_);
Ian Rogers0c7abda2012-09-19 13:33:42 -07004086 STLDeleteValues(dex_gc_maps_);
4087 delete dex_gc_maps_;
4088 dex_gc_maps_ = NULL;
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004089 }
Ian Rogers0c7abda2012-09-19 13:33:42 -07004090 delete dex_gc_maps_lock_;
4091 dex_gc_maps_lock_ = NULL;
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004092
4093 {
Ian Rogers637c65b2013-05-31 11:46:00 -07004094 WriterMutexLock mu(self, *devirt_maps_lock_);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07004095 STLDeleteValues(devirt_maps_);
4096 delete devirt_maps_;
4097 devirt_maps_ = NULL;
4098 }
4099 delete devirt_maps_lock_;
4100 devirt_maps_lock_ = NULL;
4101
4102 {
Ian Rogers50b35e22012-10-04 10:09:15 -07004103 MutexLock mu(self, *rejected_classes_lock_);
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004104 delete rejected_classes_;
4105 rejected_classes_ = NULL;
4106 }
4107 delete rejected_classes_lock_;
4108 rejected_classes_lock_ = NULL;
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08004109 verifier::RegTypeCache::ShutDown();
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08004110}
jeffhaod1224c72012-02-29 13:43:08 -08004111
Ian Rogers1212a022013-03-04 10:48:41 -08004112void MethodVerifier::AddRejectedClass(CompilerDriver::ClassReference ref) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07004113 {
Ian Rogers50b35e22012-10-04 10:09:15 -07004114 MutexLock mu(Thread::Current(), *rejected_classes_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07004115 rejected_classes_->insert(ref);
4116 }
jeffhaod1224c72012-02-29 13:43:08 -08004117 CHECK(IsClassRejected(ref));
4118}
4119
Ian Rogers1212a022013-03-04 10:48:41 -08004120bool MethodVerifier::IsClassRejected(CompilerDriver::ClassReference ref) {
Ian Rogers50b35e22012-10-04 10:09:15 -07004121 MutexLock mu(Thread::Current(), *rejected_classes_lock_);
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004122 return (rejected_classes_->find(ref) != rejected_classes_->end());
jeffhaod1224c72012-02-29 13:43:08 -08004123}
4124
Ian Rogersd81871c2011-10-03 13:57:23 -07004125} // namespace verifier
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07004126} // namespace art