blob: f03cdcdcd40483f207d0dabaeeeac4b70e97d1fe [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"
Vladimir Marko2b5eaa22013-12-13 13:59:30 +000025#include "compiler_callbacks.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"
Brian Carlstromea46f952013-07-30 01:26:50 -070033#include "mirror/art_field-inl.h"
34#include "mirror/art_method-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080035#include "mirror/class.h"
36#include "mirror/class-inl.h"
Ian Rogers39ebcb82013-05-30 16:57:23 -070037#include "mirror/dex_cache-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080038#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"
Mathieu Chartier590fee92013-09-13 13:46:47 -070043#include "scoped_thread_state_change.h"
Elliott Hughese222ee02012-12-13 14:41:43 -080044#include "verifier/dex_gc_map.h"
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070045
46namespace art {
Ian Rogersd81871c2011-10-03 13:57:23 -070047namespace verifier {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070048
Ian Rogers2c8a8572011-10-24 17:11:36 -070049static const bool gDebugVerify = false;
Anwar Ghuloum75a43f12013-08-13 17:22:14 -070050// TODO: Add a constant to method_verifier to turn on verbose logging?
Ian Rogers2c8a8572011-10-24 17:11:36 -070051
Ian Rogers7b3ddd22013-02-21 15:19:52 -080052void PcToRegisterLineTable::Init(RegisterTrackingMode mode, InstructionFlags* flags,
Ian Rogersd81871c2011-10-03 13:57:23 -070053 uint32_t insns_size, uint16_t registers_size,
Ian Rogers776ac1f2012-04-13 23:36:36 -070054 MethodVerifier* verifier) {
Ian Rogersd81871c2011-10-03 13:57:23 -070055 DCHECK_GT(insns_size, 0U);
Ian Rogersd0fbd852013-09-24 18:17:04 -070056 register_lines_.reset(new RegisterLine*[insns_size]());
57 size_ = insns_size;
Ian Rogersd81871c2011-10-03 13:57:23 -070058 for (uint32_t i = 0; i < insns_size; i++) {
59 bool interesting = false;
60 switch (mode) {
61 case kTrackRegsAll:
62 interesting = flags[i].IsOpcode();
63 break;
Sameer Abu Asal02c42232013-04-30 12:09:45 -070064 case kTrackCompilerInterestPoints:
Brian Carlstrom02c8cc62013-07-18 15:54:44 -070065 interesting = flags[i].IsCompileTimeInfoPoint() || flags[i].IsBranchTarget();
Ian Rogersd81871c2011-10-03 13:57:23 -070066 break;
67 case kTrackRegsBranches:
68 interesting = flags[i].IsBranchTarget();
69 break;
70 default:
71 break;
72 }
73 if (interesting) {
Ian Rogersd0fbd852013-09-24 18:17:04 -070074 register_lines_[i] = RegisterLine::Create(registers_size, verifier);
75 }
76 }
77}
78
79PcToRegisterLineTable::~PcToRegisterLineTable() {
80 for (size_t i = 0; i < size_; i++) {
81 delete register_lines_[i];
82 if (kIsDebugBuild) {
83 register_lines_[i] = nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -070084 }
85 }
86}
87
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080088MethodVerifier::FailureKind MethodVerifier::VerifyClass(const mirror::Class* klass,
Ian Rogers8b2c0b92013-09-19 02:56:49 -070089 bool allow_soft_failures,
90 std::string* error) {
jeffhaobdb76512011-09-07 11:43:16 -070091 if (klass->IsVerified()) {
jeffhaof1e6b7c2012-06-05 18:33:30 -070092 return kNoFailure;
jeffhaobdb76512011-09-07 11:43:16 -070093 }
Jeff Hao2d7e5aa2013-12-13 17:39:59 -080094 bool early_failure = false;
95 std::string failure_message;
Ian Rogersad0b3a32012-04-16 14:50:24 -070096 ClassHelper kh(klass);
97 const DexFile& dex_file = kh.GetDexFile();
Ian Rogers8b2c0b92013-09-19 02:56:49 -070098 const DexFile::ClassDef* class_def = kh.GetClassDef();
Jeff Hao2d7e5aa2013-12-13 17:39:59 -080099 mirror::Class* super = klass->GetSuperClass();
100 if (super == NULL && strcmp("Ljava/lang/Object;", kh.GetDescriptor()) != 0) {
101 early_failure = true;
102 failure_message = " that has no super class";
103 } else if (super != NULL && super->IsFinal()) {
104 early_failure = true;
105 failure_message = " that attempts to sub-class final class " + PrettyDescriptor(super);
106 } else if (class_def == NULL) {
107 early_failure = true;
108 failure_message = " that isn't present in dex file " + dex_file.GetLocation();
109 }
110 if (early_failure) {
111 *error = "Verifier rejected class " + PrettyDescriptor(klass) + failure_message;
112 if (Runtime::Current()->IsCompiler()) {
113 ClassReference ref(&dex_file, klass->GetDexClassDefIndex());
Vladimir Marko2b5eaa22013-12-13 13:59:30 +0000114 Runtime::Current()->GetCompilerCallbacks()->ClassRejected(ref);
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800115 }
jeffhaof1e6b7c2012-06-05 18:33:30 -0700116 return kHardFailure;
jeffhaobdb76512011-09-07 11:43:16 -0700117 }
Mathieu Chartier590fee92013-09-13 13:46:47 -0700118 Thread* self = Thread::Current();
119 SirtRef<mirror::DexCache> dex_cache(self, kh.GetDexCache());
120 SirtRef<mirror::ClassLoader> class_loader(self, klass->GetClassLoader());
121 return VerifyClass(&dex_file, dex_cache, class_loader, class_def, allow_soft_failures, error);
Shih-wei Liao371814f2011-10-27 16:52:10 -0700122}
123
Ian Rogers365c1022012-06-22 15:05:28 -0700124MethodVerifier::FailureKind MethodVerifier::VerifyClass(const DexFile* dex_file,
Mathieu Chartier590fee92013-09-13 13:46:47 -0700125 SirtRef<mirror::DexCache>& dex_cache,
126 SirtRef<mirror::ClassLoader>& class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700127 const DexFile::ClassDef* class_def,
128 bool allow_soft_failures,
129 std::string* error) {
130 DCHECK(class_def != nullptr);
131 const byte* class_data = dex_file->GetClassData(*class_def);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700132 if (class_data == NULL) {
133 // empty class, probably a marker interface
jeffhaof1e6b7c2012-06-05 18:33:30 -0700134 return kNoFailure;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700135 }
jeffhaof56197c2012-03-05 18:01:54 -0800136 ClassDataItemIterator it(*dex_file, class_data);
137 while (it.HasNextStaticField() || it.HasNextInstanceField()) {
138 it.Next();
139 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700140 size_t error_count = 0;
jeffhaof1e6b7c2012-06-05 18:33:30 -0700141 bool hard_fail = false;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700142 ClassLinker* linker = Runtime::Current()->GetClassLinker();
jeffhao9b0b1882012-10-01 16:51:22 -0700143 int64_t previous_direct_method_idx = -1;
jeffhaof56197c2012-03-05 18:01:54 -0800144 while (it.HasNextDirectMethod()) {
145 uint32_t method_idx = it.GetMemberIndex();
jeffhao9b0b1882012-10-01 16:51:22 -0700146 if (method_idx == previous_direct_method_idx) {
147 // smali can create dex files with two encoded_methods sharing the same method_idx
148 // http://code.google.com/p/smali/issues/detail?id=119
149 it.Next();
150 continue;
151 }
152 previous_direct_method_idx = method_idx;
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700153 InvokeType type = it.GetMethodInvokeType(*class_def);
Brian Carlstromea46f952013-07-30 01:26:50 -0700154 mirror::ArtMethod* method =
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800155 linker->ResolveMethod(*dex_file, method_idx, dex_cache, class_loader, NULL, type);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700156 if (method == NULL) {
157 DCHECK(Thread::Current()->IsExceptionPending());
158 // We couldn't resolve the method, but continue regardless.
159 Thread::Current()->ClearException();
160 }
Brian Carlstrom93c33962013-07-26 10:37:43 -0700161 MethodVerifier::FailureKind result = VerifyMethod(method_idx,
162 dex_file,
163 dex_cache,
164 class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700165 class_def,
Brian Carlstrom93c33962013-07-26 10:37:43 -0700166 it.GetMethodCodeItem(),
167 method,
168 it.GetMemberAccessFlags(),
169 allow_soft_failures);
jeffhaof1e6b7c2012-06-05 18:33:30 -0700170 if (result != kNoFailure) {
171 if (result == kHardFailure) {
172 hard_fail = true;
173 if (error_count > 0) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700174 *error += "\n";
jeffhaof1e6b7c2012-06-05 18:33:30 -0700175 }
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700176 *error = "Verifier rejected class ";
177 *error += PrettyDescriptor(dex_file->GetClassDescriptor(*class_def));
178 *error += " due to bad method ";
179 *error += PrettyMethod(method_idx, *dex_file);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700180 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700181 ++error_count;
jeffhaof56197c2012-03-05 18:01:54 -0800182 }
183 it.Next();
184 }
jeffhao9b0b1882012-10-01 16:51:22 -0700185 int64_t previous_virtual_method_idx = -1;
jeffhaof56197c2012-03-05 18:01:54 -0800186 while (it.HasNextVirtualMethod()) {
187 uint32_t method_idx = it.GetMemberIndex();
jeffhao9b0b1882012-10-01 16:51:22 -0700188 if (method_idx == previous_virtual_method_idx) {
189 // smali can create dex files with two encoded_methods sharing the same method_idx
190 // http://code.google.com/p/smali/issues/detail?id=119
191 it.Next();
192 continue;
193 }
194 previous_virtual_method_idx = method_idx;
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700195 InvokeType type = it.GetMethodInvokeType(*class_def);
Brian Carlstromea46f952013-07-30 01:26:50 -0700196 mirror::ArtMethod* method =
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800197 linker->ResolveMethod(*dex_file, method_idx, dex_cache, class_loader, NULL, type);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700198 if (method == NULL) {
199 DCHECK(Thread::Current()->IsExceptionPending());
200 // We couldn't resolve the method, but continue regardless.
201 Thread::Current()->ClearException();
202 }
Brian Carlstrom93c33962013-07-26 10:37:43 -0700203 MethodVerifier::FailureKind result = VerifyMethod(method_idx,
204 dex_file,
205 dex_cache,
206 class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700207 class_def,
Brian Carlstrom93c33962013-07-26 10:37:43 -0700208 it.GetMethodCodeItem(),
209 method,
210 it.GetMemberAccessFlags(),
211 allow_soft_failures);
jeffhaof1e6b7c2012-06-05 18:33:30 -0700212 if (result != kNoFailure) {
213 if (result == kHardFailure) {
214 hard_fail = true;
215 if (error_count > 0) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700216 *error += "\n";
jeffhaof1e6b7c2012-06-05 18:33:30 -0700217 }
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700218 *error = "Verifier rejected class ";
219 *error += PrettyDescriptor(dex_file->GetClassDescriptor(*class_def));
220 *error += " due to bad method ";
221 *error += PrettyMethod(method_idx, *dex_file);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700222 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700223 ++error_count;
jeffhaof56197c2012-03-05 18:01:54 -0800224 }
225 it.Next();
226 }
jeffhaof1e6b7c2012-06-05 18:33:30 -0700227 if (error_count == 0) {
228 return kNoFailure;
229 } else {
230 return hard_fail ? kHardFailure : kSoftFailure;
231 }
jeffhaof56197c2012-03-05 18:01:54 -0800232}
233
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800234MethodVerifier::FailureKind MethodVerifier::VerifyMethod(uint32_t method_idx,
235 const DexFile* dex_file,
Mathieu Chartier590fee92013-09-13 13:46:47 -0700236 SirtRef<mirror::DexCache>& dex_cache,
237 SirtRef<mirror::ClassLoader>& class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700238 const DexFile::ClassDef* class_def,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800239 const DexFile::CodeItem* code_item,
Brian Carlstromea46f952013-07-30 01:26:50 -0700240 mirror::ArtMethod* method,
Jeff Haoee988952013-04-16 14:23:47 -0700241 uint32_t method_access_flags,
242 bool allow_soft_failures) {
Ian Rogersc8982582012-09-07 16:53:25 -0700243 MethodVerifier::FailureKind result = kNoFailure;
244 uint64_t start_ns = NanoTime();
245
Mathieu Chartier590fee92013-09-13 13:46:47 -0700246 MethodVerifier verifier_(dex_file, &dex_cache, &class_loader, class_def, code_item,
247 method_idx, method, method_access_flags, true, allow_soft_failures);
Anwar Ghuloum75a43f12013-08-13 17:22:14 -0700248 if (verifier_.Verify()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700249 // Verification completed, however failures may be pending that didn't cause the verification
250 // to hard fail.
Anwar Ghuloum75a43f12013-08-13 17:22:14 -0700251 CHECK(!verifier_.have_pending_hard_failure_);
252 if (verifier_.failures_.size() != 0) {
253 if (VLOG_IS_ON(verifier)) {
254 verifier_.DumpFailures(VLOG_STREAM(verifier) << "Soft verification failures in "
255 << PrettyMethod(method_idx, *dex_file) << "\n");
256 }
Ian Rogersc8982582012-09-07 16:53:25 -0700257 result = kSoftFailure;
jeffhaof56197c2012-03-05 18:01:54 -0800258 }
259 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700260 // Bad method data.
Anwar Ghuloum75a43f12013-08-13 17:22:14 -0700261 CHECK_NE(verifier_.failures_.size(), 0U);
262 CHECK(verifier_.have_pending_hard_failure_);
263 verifier_.DumpFailures(LOG(INFO) << "Verification error in "
Elliott Hughesc073b072012-05-24 19:29:17 -0700264 << PrettyMethod(method_idx, *dex_file) << "\n");
jeffhaof56197c2012-03-05 18:01:54 -0800265 if (gDebugVerify) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -0700266 std::cout << "\n" << verifier_.info_messages_.str();
267 verifier_.Dump(std::cout);
jeffhaof56197c2012-03-05 18:01:54 -0800268 }
Ian Rogersc8982582012-09-07 16:53:25 -0700269 result = kHardFailure;
jeffhaof56197c2012-03-05 18:01:54 -0800270 }
Ian Rogersc8982582012-09-07 16:53:25 -0700271 uint64_t duration_ns = NanoTime() - start_ns;
272 if (duration_ns > MsToNs(100)) {
273 LOG(WARNING) << "Verification of " << PrettyMethod(method_idx, *dex_file)
274 << " took " << PrettyDuration(duration_ns);
275 }
276 return result;
jeffhaof56197c2012-03-05 18:01:54 -0800277}
278
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800279void MethodVerifier::VerifyMethodAndDump(std::ostream& os, uint32_t dex_method_idx,
Mathieu Chartier590fee92013-09-13 13:46:47 -0700280 const DexFile* dex_file,
281 SirtRef<mirror::DexCache>& dex_cache,
282 SirtRef<mirror::ClassLoader>& class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700283 const DexFile::ClassDef* class_def,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800284 const DexFile::CodeItem* code_item,
Brian Carlstromea46f952013-07-30 01:26:50 -0700285 mirror::ArtMethod* method,
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800286 uint32_t method_access_flags) {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700287 MethodVerifier verifier(dex_file, &dex_cache, &class_loader, class_def, code_item,
Jeff Haoee988952013-04-16 14:23:47 -0700288 dex_method_idx, method, method_access_flags, true, true);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700289 verifier.Verify();
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800290 verifier.DumpFailures(os);
291 os << verifier.info_messages_.str();
292 verifier.Dump(os);
293}
294
Mathieu Chartier590fee92013-09-13 13:46:47 -0700295MethodVerifier::MethodVerifier(const DexFile* dex_file, SirtRef<mirror::DexCache>* dex_cache,
296 SirtRef<mirror::ClassLoader>* class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700297 const DexFile::ClassDef* class_def,
Mathieu Chartier590fee92013-09-13 13:46:47 -0700298 const DexFile::CodeItem* code_item, uint32_t dex_method_idx,
299 mirror::ArtMethod* method, uint32_t method_access_flags,
300 bool can_load_classes, bool allow_soft_failures)
Elliott Hughes80537bb2013-01-04 16:37:26 -0800301 : reg_types_(can_load_classes),
302 work_insn_idx_(-1),
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800303 dex_method_idx_(dex_method_idx),
Ian Rogers637c65b2013-05-31 11:46:00 -0700304 mirror_method_(method),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700305 method_access_flags_(method_access_flags),
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700306 return_type_(nullptr),
jeffhaof56197c2012-03-05 18:01:54 -0800307 dex_file_(dex_file),
308 dex_cache_(dex_cache),
309 class_loader_(class_loader),
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700310 class_def_(class_def),
jeffhaof56197c2012-03-05 18:01:54 -0800311 code_item_(code_item),
Ian Rogers637c65b2013-05-31 11:46:00 -0700312 declaring_class_(NULL),
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700313 interesting_dex_pc_(-1),
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700314 monitor_enter_dex_pcs_(nullptr),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700315 have_pending_hard_failure_(false),
jeffhaofaf459e2012-08-31 15:32:47 -0700316 have_pending_runtime_throw_failure_(false),
jeffhaof56197c2012-03-05 18:01:54 -0800317 new_instance_count_(0),
Elliott Hughes80537bb2013-01-04 16:37:26 -0800318 monitor_enter_count_(0),
Jeff Haoee988952013-04-16 14:23:47 -0700319 can_load_classes_(can_load_classes),
Sebastien Hertz4d4adb12013-07-24 16:14:19 +0200320 allow_soft_failures_(allow_soft_failures),
321 has_check_casts_(false),
322 has_virtual_or_interface_invokes_(false) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800323 Runtime::Current()->AddMethodVerifier(this);
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700324 DCHECK(class_def != nullptr);
jeffhaof56197c2012-03-05 18:01:54 -0800325}
326
Mathieu Chartier590fee92013-09-13 13:46:47 -0700327MethodVerifier::~MethodVerifier() {
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800328 Runtime::Current()->RemoveMethodVerifier(this);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700329 STLDeleteElements(&failure_messages_);
330}
331
Brian Carlstromea46f952013-07-30 01:26:50 -0700332void MethodVerifier::FindLocksAtDexPc(mirror::ArtMethod* m, uint32_t dex_pc,
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800333 std::vector<uint32_t>& monitor_enter_dex_pcs) {
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700334 MethodHelper mh(m);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700335 Thread* self = Thread::Current();
336 SirtRef<mirror::DexCache> dex_cache(self, mh.GetDexCache());
337 SirtRef<mirror::ClassLoader> class_loader(self, mh.GetClassLoader());
338 MethodVerifier verifier(&mh.GetDexFile(), &dex_cache, &class_loader, &mh.GetClassDef(),
339 mh.GetCodeItem(), m->GetDexMethodIndex(), m, m->GetAccessFlags(), false,
340 true);
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700341 verifier.interesting_dex_pc_ = dex_pc;
342 verifier.monitor_enter_dex_pcs_ = &monitor_enter_dex_pcs;
343 verifier.FindLocksAtDexPc();
344}
345
346void MethodVerifier::FindLocksAtDexPc() {
347 CHECK(monitor_enter_dex_pcs_ != NULL);
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700348 CHECK(code_item_ != NULL); // This only makes sense for methods with code.
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700349
350 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
351 // verification. In practice, the phase we want relies on data structures set up by all the
352 // earlier passes, so we just run the full method verification and bail out early when we've
353 // got what we wanted.
354 Verify();
355}
356
Brian Carlstromea46f952013-07-30 01:26:50 -0700357mirror::ArtField* MethodVerifier::FindAccessedFieldAtDexPc(mirror::ArtMethod* m,
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200358 uint32_t dex_pc) {
359 MethodHelper mh(m);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700360 Thread* self = Thread::Current();
361 SirtRef<mirror::DexCache> dex_cache(self, mh.GetDexCache());
362 SirtRef<mirror::ClassLoader> class_loader(self, mh.GetClassLoader());
363 MethodVerifier verifier(&mh.GetDexFile(), &dex_cache, &class_loader, &mh.GetClassDef(),
364 mh.GetCodeItem(), m->GetDexMethodIndex(), m, m->GetAccessFlags(), false,
365 true);
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200366 return verifier.FindAccessedFieldAtDexPc(dex_pc);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200367}
368
Brian Carlstromea46f952013-07-30 01:26:50 -0700369mirror::ArtField* MethodVerifier::FindAccessedFieldAtDexPc(uint32_t dex_pc) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700370 CHECK(code_item_ != NULL); // This only makes sense for methods with code.
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200371
372 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
373 // verification. In practice, the phase we want relies on data structures set up by all the
374 // earlier passes, so we just run the full method verification and bail out early when we've
375 // got what we wanted.
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200376 bool success = Verify();
377 if (!success) {
378 return NULL;
379 }
380 RegisterLine* register_line = reg_table_.GetLine(dex_pc);
381 if (register_line == NULL) {
382 return NULL;
383 }
384 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
385 return GetQuickFieldAccess(inst, register_line);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200386}
387
Brian Carlstromea46f952013-07-30 01:26:50 -0700388mirror::ArtMethod* MethodVerifier::FindInvokedMethodAtDexPc(mirror::ArtMethod* m,
Mathieu Chartier590fee92013-09-13 13:46:47 -0700389 uint32_t dex_pc) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200390 MethodHelper mh(m);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700391 Thread* self = Thread::Current();
392 SirtRef<mirror::DexCache> dex_cache(self, mh.GetDexCache());
393 SirtRef<mirror::ClassLoader> class_loader(self, mh.GetClassLoader());
394 MethodVerifier verifier(&mh.GetDexFile(), &dex_cache, &class_loader, &mh.GetClassDef(),
395 mh.GetCodeItem(), m->GetDexMethodIndex(), m, m->GetAccessFlags(), false,
396 true);
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200397 return verifier.FindInvokedMethodAtDexPc(dex_pc);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200398}
399
Brian Carlstromea46f952013-07-30 01:26:50 -0700400mirror::ArtMethod* MethodVerifier::FindInvokedMethodAtDexPc(uint32_t dex_pc) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700401 CHECK(code_item_ != NULL); // This only makes sense for methods with code.
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200402
403 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
404 // verification. In practice, the phase we want relies on data structures set up by all the
405 // earlier passes, so we just run the full method verification and bail out early when we've
406 // got what we wanted.
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200407 bool success = Verify();
408 if (!success) {
409 return NULL;
410 }
411 RegisterLine* register_line = reg_table_.GetLine(dex_pc);
412 if (register_line == NULL) {
413 return NULL;
414 }
415 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
416 const bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
417 return GetQuickInvokedMethod(inst, register_line, is_range);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200418}
419
Ian Rogersad0b3a32012-04-16 14:50:24 -0700420bool MethodVerifier::Verify() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700421 // If there aren't any instructions, make sure that's expected, then exit successfully.
422 if (code_item_ == NULL) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700423 if ((method_access_flags_ & (kAccNative | kAccAbstract)) == 0) {
jeffhaod5347e02012-03-22 17:25:05 -0700424 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "zero-length code in concrete non-native method";
jeffhaobdb76512011-09-07 11:43:16 -0700425 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -0700426 } else {
427 return true;
jeffhaobdb76512011-09-07 11:43:16 -0700428 }
jeffhaobdb76512011-09-07 11:43:16 -0700429 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700430 // Sanity-check the register counts. ins + locals = registers, so make sure that ins <= registers.
431 if (code_item_->ins_size_ > code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700432 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad register counts (ins=" << code_item_->ins_size_
433 << " regs=" << code_item_->registers_size_;
Ian Rogersd81871c2011-10-03 13:57:23 -0700434 return false;
jeffhaobdb76512011-09-07 11:43:16 -0700435 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700436 // Allocate and initialize an array to hold instruction data.
Ian Rogers7b3ddd22013-02-21 15:19:52 -0800437 insn_flags_.reset(new InstructionFlags[code_item_->insns_size_in_code_units_]());
Ian Rogersd81871c2011-10-03 13:57:23 -0700438 // Run through the instructions and see if the width checks out.
439 bool result = ComputeWidthsAndCountOps();
440 // Flag instructions guarded by a "try" block and check exception handlers.
441 result = result && ScanTryCatchBlocks();
442 // Perform static instruction verification.
443 result = result && VerifyInstructions();
Ian Rogersad0b3a32012-04-16 14:50:24 -0700444 // Perform code-flow analysis and return.
Vladimir Marko2b5eaa22013-12-13 13:59:30 +0000445 result = result && VerifyCodeFlow();
446 // Compute information for compiler.
447 if (result && Runtime::Current()->IsCompiler()) {
448 result = Runtime::Current()->GetCompilerCallbacks()->MethodVerified(this);
449 }
450 return result;
jeffhaoba5ebb92011-08-25 17:24:37 -0700451}
452
Ian Rogers776ac1f2012-04-13 23:36:36 -0700453std::ostream& MethodVerifier::Fail(VerifyError error) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700454 switch (error) {
455 case VERIFY_ERROR_NO_CLASS:
456 case VERIFY_ERROR_NO_FIELD:
457 case VERIFY_ERROR_NO_METHOD:
458 case VERIFY_ERROR_ACCESS_CLASS:
459 case VERIFY_ERROR_ACCESS_FIELD:
460 case VERIFY_ERROR_ACCESS_METHOD:
Ian Rogers08f753d2012-08-24 14:35:25 -0700461 case VERIFY_ERROR_INSTANTIATION:
462 case VERIFY_ERROR_CLASS_CHANGE:
Elliott Hughes4993bbc2013-01-10 15:41:25 -0800463 if (Runtime::Current()->IsCompiler() || !can_load_classes_) {
jeffhaofaf459e2012-08-31 15:32:47 -0700464 // If we're optimistically running verification at compile time, turn NO_xxx, ACCESS_xxx,
465 // class change and instantiation errors into soft verification errors so that we re-verify
466 // at runtime. We may fail to find or to agree on access because of not yet available class
467 // loaders, or class loaders that will differ at runtime. In these cases, we don't want to
468 // affect the soundness of the code being compiled. Instead, the generated code runs "slow
469 // paths" that dynamically perform the verification and cause the behavior to be that akin
470 // to an interpreter.
471 error = VERIFY_ERROR_BAD_CLASS_SOFT;
472 } else {
Jeff Haoa3faaf42013-09-03 19:07:00 -0700473 // If we fail again at runtime, mark that this instruction would throw and force this
474 // method to be executed using the interpreter with checks.
jeffhaofaf459e2012-08-31 15:32:47 -0700475 have_pending_runtime_throw_failure_ = true;
476 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700477 break;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700478 // Indication that verification should be retried at runtime.
479 case VERIFY_ERROR_BAD_CLASS_SOFT:
Jeff Haoee988952013-04-16 14:23:47 -0700480 if (!allow_soft_failures_) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700481 have_pending_hard_failure_ = true;
482 }
483 break;
jeffhaod5347e02012-03-22 17:25:05 -0700484 // Hard verification failures at compile time will still fail at runtime, so the class is
485 // marked as rejected to prevent it from being compiled.
Ian Rogersad0b3a32012-04-16 14:50:24 -0700486 case VERIFY_ERROR_BAD_CLASS_HARD: {
487 if (Runtime::Current()->IsCompiler()) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700488 ClassReference ref(dex_file_, dex_file_->GetIndexForClassDef(*class_def_));
Vladimir Marko2b5eaa22013-12-13 13:59:30 +0000489 Runtime::Current()->GetCompilerCallbacks()->ClassRejected(ref);
jeffhaod1224c72012-02-29 13:43:08 -0800490 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700491 have_pending_hard_failure_ = true;
492 break;
Ian Rogers47a05882012-02-03 12:23:33 -0800493 }
494 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700495 failures_.push_back(error);
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800496 std::string location(StringPrintf("%s: [0x%X]", PrettyMethod(dex_method_idx_, *dex_file_).c_str(),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700497 work_insn_idx_));
498 std::ostringstream* failure_message = new std::ostringstream(location);
499 failure_messages_.push_back(failure_message);
500 return *failure_message;
501}
502
503void MethodVerifier::PrependToLastFailMessage(std::string prepend) {
504 size_t failure_num = failure_messages_.size();
505 DCHECK_NE(failure_num, 0U);
506 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
507 prepend += last_fail_message->str();
508 failure_messages_[failure_num - 1] = new std::ostringstream(prepend);
509 delete last_fail_message;
510}
511
512void MethodVerifier::AppendToLastFailMessage(std::string append) {
513 size_t failure_num = failure_messages_.size();
514 DCHECK_NE(failure_num, 0U);
515 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
516 (*last_fail_message) << append;
Ian Rogers47a05882012-02-03 12:23:33 -0800517}
518
Ian Rogers776ac1f2012-04-13 23:36:36 -0700519bool MethodVerifier::ComputeWidthsAndCountOps() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700520 const uint16_t* insns = code_item_->insns_;
521 size_t insns_size = code_item_->insns_size_in_code_units_;
522 const Instruction* inst = Instruction::At(insns);
jeffhaobdb76512011-09-07 11:43:16 -0700523 size_t new_instance_count = 0;
524 size_t monitor_enter_count = 0;
Ian Rogersd81871c2011-10-03 13:57:23 -0700525 size_t dex_pc = 0;
jeffhaobdb76512011-09-07 11:43:16 -0700526
Ian Rogersd81871c2011-10-03 13:57:23 -0700527 while (dex_pc < insns_size) {
jeffhaobdb76512011-09-07 11:43:16 -0700528 Instruction::Code opcode = inst->Opcode();
Ian Rogersa9a82542013-10-04 11:17:26 -0700529 switch (opcode) {
530 case Instruction::APUT_OBJECT:
531 case Instruction::CHECK_CAST:
532 has_check_casts_ = true;
533 break;
534 case Instruction::INVOKE_VIRTUAL:
535 case Instruction::INVOKE_VIRTUAL_RANGE:
536 case Instruction::INVOKE_INTERFACE:
537 case Instruction::INVOKE_INTERFACE_RANGE:
538 has_virtual_or_interface_invokes_ = true;
539 break;
540 case Instruction::MONITOR_ENTER:
541 monitor_enter_count++;
542 break;
543 case Instruction::NEW_INSTANCE:
544 new_instance_count++;
545 break;
546 default:
547 break;
jeffhaobdb76512011-09-07 11:43:16 -0700548 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700549 size_t inst_size = inst->SizeInCodeUnits();
550 insn_flags_[dex_pc].SetLengthInCodeUnits(inst_size);
551 dex_pc += inst_size;
jeffhaobdb76512011-09-07 11:43:16 -0700552 inst = inst->Next();
553 }
554
Ian Rogersd81871c2011-10-03 13:57:23 -0700555 if (dex_pc != insns_size) {
jeffhaod5347e02012-03-22 17:25:05 -0700556 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "code did not end where expected ("
557 << dex_pc << " vs. " << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700558 return false;
559 }
560
Ian Rogersd81871c2011-10-03 13:57:23 -0700561 new_instance_count_ = new_instance_count;
562 monitor_enter_count_ = monitor_enter_count;
jeffhaobdb76512011-09-07 11:43:16 -0700563 return true;
564}
565
Ian Rogers776ac1f2012-04-13 23:36:36 -0700566bool MethodVerifier::ScanTryCatchBlocks() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700567 uint32_t tries_size = code_item_->tries_size_;
jeffhaobdb76512011-09-07 11:43:16 -0700568 if (tries_size == 0) {
569 return true;
570 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700571 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Ian Rogers0571d352011-11-03 19:51:38 -0700572 const DexFile::TryItem* tries = DexFile::GetTryItems(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -0700573
574 for (uint32_t idx = 0; idx < tries_size; idx++) {
575 const DexFile::TryItem* try_item = &tries[idx];
576 uint32_t start = try_item->start_addr_;
577 uint32_t end = start + try_item->insn_count_;
jeffhaobdb76512011-09-07 11:43:16 -0700578 if ((start >= end) || (start >= insns_size) || (end > insns_size)) {
jeffhaod5347e02012-03-22 17:25:05 -0700579 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad exception entry: startAddr=" << start
580 << " endAddr=" << end << " (size=" << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700581 return false;
582 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700583 if (!insn_flags_[start].IsOpcode()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700584 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
585 << "'try' block starts inside an instruction (" << start << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700586 return false;
587 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700588 for (uint32_t dex_pc = start; dex_pc < end;
589 dex_pc += insn_flags_[dex_pc].GetLengthInCodeUnits()) {
590 insn_flags_[dex_pc].SetInTry();
jeffhaobdb76512011-09-07 11:43:16 -0700591 }
592 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800593 // Iterate over each of the handlers to verify target addresses.
Ian Rogers0571d352011-11-03 19:51:38 -0700594 const byte* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -0700595 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700596 ClassLinker* linker = Runtime::Current()->GetClassLinker();
jeffhaobdb76512011-09-07 11:43:16 -0700597 for (uint32_t idx = 0; idx < handlers_size; idx++) {
Ian Rogers0571d352011-11-03 19:51:38 -0700598 CatchHandlerIterator iterator(handlers_ptr);
599 for (; iterator.HasNext(); iterator.Next()) {
600 uint32_t dex_pc= iterator.GetHandlerAddress();
Ian Rogersd81871c2011-10-03 13:57:23 -0700601 if (!insn_flags_[dex_pc].IsOpcode()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700602 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
603 << "exception handler starts at bad address (" << dex_pc << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700604 return false;
605 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700606 insn_flags_[dex_pc].SetBranchTarget();
Ian Rogers28ad40d2011-10-27 15:19:26 -0700607 // Ensure exception types are resolved so that they don't need resolution to be delivered,
608 // unresolved exception types will be ignored by exception delivery
Ian Rogers0571d352011-11-03 19:51:38 -0700609 if (iterator.GetHandlerTypeIndex() != DexFile::kDexNoIndex16) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800610 mirror::Class* exception_type = linker->ResolveType(*dex_file_,
611 iterator.GetHandlerTypeIndex(),
Mathieu Chartier590fee92013-09-13 13:46:47 -0700612 *dex_cache_, *class_loader_);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700613 if (exception_type == NULL) {
614 DCHECK(Thread::Current()->IsExceptionPending());
615 Thread::Current()->ClearException();
616 }
617 }
jeffhaobdb76512011-09-07 11:43:16 -0700618 }
Ian Rogers0571d352011-11-03 19:51:38 -0700619 handlers_ptr = iterator.EndDataPointer();
jeffhaobdb76512011-09-07 11:43:16 -0700620 }
jeffhaobdb76512011-09-07 11:43:16 -0700621 return true;
622}
623
Ian Rogers776ac1f2012-04-13 23:36:36 -0700624bool MethodVerifier::VerifyInstructions() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700625 const Instruction* inst = Instruction::At(code_item_->insns_);
jeffhaoba5ebb92011-08-25 17:24:37 -0700626
Ian Rogers0c7abda2012-09-19 13:33:42 -0700627 /* 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 -0700628 insn_flags_[0].SetBranchTarget();
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700629 insn_flags_[0].SetCompileTimeInfoPoint();
Ian Rogersd81871c2011-10-03 13:57:23 -0700630
631 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700632 for (uint32_t dex_pc = 0; dex_pc < insns_size;) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700633 if (!VerifyInstruction(inst, dex_pc)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700634 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -0700635 return false;
636 }
637 /* Flag instructions that are garbage collection points */
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700638 // All invoke points are marked as "Throw" points already.
639 // We are relying on this to also count all the invokes as interesting.
Ian Rogersb8c78592013-07-25 23:52:52 +0000640 if (inst->IsBranch() || inst->IsSwitch() || inst->IsThrow()) {
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700641 insn_flags_[dex_pc].SetCompileTimeInfoPoint();
Ian Rogersb8c78592013-07-25 23:52:52 +0000642 } else if (inst->IsReturn()) {
643 insn_flags_[dex_pc].SetCompileTimeInfoPointAndReturn();
Ian Rogersd81871c2011-10-03 13:57:23 -0700644 }
645 dex_pc += inst->SizeInCodeUnits();
646 inst = inst->Next();
647 }
648 return true;
649}
650
Ian Rogers776ac1f2012-04-13 23:36:36 -0700651bool MethodVerifier::VerifyInstruction(const Instruction* inst, uint32_t code_offset) {
Elliott Hughesadb8c672012-03-06 16:49:32 -0800652 DecodedInstruction dec_insn(inst);
Ian Rogersd81871c2011-10-03 13:57:23 -0700653 bool result = true;
654 switch (inst->GetVerifyTypeArgumentA()) {
655 case Instruction::kVerifyRegA:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800656 result = result && CheckRegisterIndex(dec_insn.vA);
Ian Rogersd81871c2011-10-03 13:57:23 -0700657 break;
658 case Instruction::kVerifyRegAWide:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800659 result = result && CheckWideRegisterIndex(dec_insn.vA);
Ian Rogersd81871c2011-10-03 13:57:23 -0700660 break;
661 }
662 switch (inst->GetVerifyTypeArgumentB()) {
663 case Instruction::kVerifyRegB:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800664 result = result && CheckRegisterIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700665 break;
666 case Instruction::kVerifyRegBField:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800667 result = result && CheckFieldIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700668 break;
669 case Instruction::kVerifyRegBMethod:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800670 result = result && CheckMethodIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700671 break;
672 case Instruction::kVerifyRegBNewInstance:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800673 result = result && CheckNewInstance(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700674 break;
675 case Instruction::kVerifyRegBString:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800676 result = result && CheckStringIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700677 break;
678 case Instruction::kVerifyRegBType:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800679 result = result && CheckTypeIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700680 break;
681 case Instruction::kVerifyRegBWide:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800682 result = result && CheckWideRegisterIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700683 break;
684 }
685 switch (inst->GetVerifyTypeArgumentC()) {
686 case Instruction::kVerifyRegC:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800687 result = result && CheckRegisterIndex(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700688 break;
689 case Instruction::kVerifyRegCField:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800690 result = result && CheckFieldIndex(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700691 break;
692 case Instruction::kVerifyRegCNewArray:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800693 result = result && CheckNewArray(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700694 break;
695 case Instruction::kVerifyRegCType:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800696 result = result && CheckTypeIndex(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700697 break;
698 case Instruction::kVerifyRegCWide:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800699 result = result && CheckWideRegisterIndex(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700700 break;
701 }
702 switch (inst->GetVerifyExtraFlags()) {
703 case Instruction::kVerifyArrayData:
704 result = result && CheckArrayData(code_offset);
705 break;
706 case Instruction::kVerifyBranchTarget:
707 result = result && CheckBranchTarget(code_offset);
708 break;
709 case Instruction::kVerifySwitchTargets:
710 result = result && CheckSwitchTargets(code_offset);
711 break;
712 case Instruction::kVerifyVarArg:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800713 result = result && CheckVarArgRegs(dec_insn.vA, dec_insn.arg);
Ian Rogersd81871c2011-10-03 13:57:23 -0700714 break;
715 case Instruction::kVerifyVarArgRange:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800716 result = result && CheckVarArgRangeRegs(dec_insn.vA, dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700717 break;
718 case Instruction::kVerifyError:
jeffhaod5347e02012-03-22 17:25:05 -0700719 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected opcode " << inst->Name();
Ian Rogersd81871c2011-10-03 13:57:23 -0700720 result = false;
721 break;
722 }
723 return result;
724}
725
Ian Rogers776ac1f2012-04-13 23:36:36 -0700726bool MethodVerifier::CheckRegisterIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700727 if (idx >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700728 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register index out of range (" << idx << " >= "
729 << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700730 return false;
731 }
732 return true;
733}
734
Ian Rogers776ac1f2012-04-13 23:36:36 -0700735bool MethodVerifier::CheckWideRegisterIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700736 if (idx + 1 >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700737 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "wide register index out of range (" << idx
738 << "+1 >= " << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700739 return false;
740 }
741 return true;
742}
743
Ian Rogers776ac1f2012-04-13 23:36:36 -0700744bool MethodVerifier::CheckFieldIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700745 if (idx >= dex_file_->GetHeader().field_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700746 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad field index " << idx << " (max "
747 << dex_file_->GetHeader().field_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700748 return false;
749 }
750 return true;
751}
752
Ian Rogers776ac1f2012-04-13 23:36:36 -0700753bool MethodVerifier::CheckMethodIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700754 if (idx >= dex_file_->GetHeader().method_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700755 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad method index " << idx << " (max "
756 << dex_file_->GetHeader().method_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700757 return false;
758 }
759 return true;
760}
761
Ian Rogers776ac1f2012-04-13 23:36:36 -0700762bool MethodVerifier::CheckNewInstance(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700763 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700764 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
765 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700766 return false;
767 }
768 // We don't need the actual class, just a pointer to the class name.
Ian Rogers0571d352011-11-03 19:51:38 -0700769 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -0700770 if (descriptor[0] != 'L') {
jeffhaod5347e02012-03-22 17:25:05 -0700771 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "can't call new-instance on type '" << descriptor << "'";
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::CheckStringIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700778 if (idx >= dex_file_->GetHeader().string_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700779 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad string index " << idx << " (max "
780 << dex_file_->GetHeader().string_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700781 return false;
782 }
783 return true;
784}
785
Ian Rogers776ac1f2012-04-13 23:36:36 -0700786bool MethodVerifier::CheckTypeIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700787 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700788 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
789 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700790 return false;
791 }
792 return true;
793}
794
Ian Rogers776ac1f2012-04-13 23:36:36 -0700795bool MethodVerifier::CheckNewArray(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700796 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700797 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
798 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700799 return false;
800 }
801 int bracket_count = 0;
Ian Rogers0571d352011-11-03 19:51:38 -0700802 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -0700803 const char* cp = descriptor;
804 while (*cp++ == '[') {
805 bracket_count++;
806 }
807 if (bracket_count == 0) {
808 /* The given class must be an array type. */
Brian Carlstrom93c33962013-07-26 10:37:43 -0700809 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
810 << "can't new-array class '" << descriptor << "' (not an array)";
Ian Rogersd81871c2011-10-03 13:57:23 -0700811 return false;
812 } else if (bracket_count > 255) {
813 /* It is illegal to create an array of more than 255 dimensions. */
Brian Carlstrom93c33962013-07-26 10:37:43 -0700814 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
815 << "can't new-array class '" << descriptor << "' (exceeds limit)";
Ian Rogersd81871c2011-10-03 13:57:23 -0700816 return false;
817 }
818 return true;
819}
820
Ian Rogers776ac1f2012-04-13 23:36:36 -0700821bool MethodVerifier::CheckArrayData(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700822 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
823 const uint16_t* insns = code_item_->insns_ + cur_offset;
824 const uint16_t* array_data;
825 int32_t array_data_offset;
826
827 DCHECK_LT(cur_offset, insn_count);
828 /* make sure the start of the array data table is in range */
829 array_data_offset = insns[1] | (((int32_t) insns[2]) << 16);
830 if ((int32_t) cur_offset + array_data_offset < 0 ||
831 cur_offset + array_data_offset + 2 >= insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -0700832 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data start: at " << cur_offset
Brian Carlstrom93c33962013-07-26 10:37:43 -0700833 << ", data offset " << array_data_offset
834 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -0700835 return false;
836 }
837 /* offset to array data table is a relative branch-style offset */
838 array_data = insns + array_data_offset;
839 /* make sure the table is 32-bit aligned */
840 if ((((uint32_t) array_data) & 0x03) != 0) {
jeffhaod5347e02012-03-22 17:25:05 -0700841 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned array data table: at " << cur_offset
842 << ", data offset " << array_data_offset;
Ian Rogersd81871c2011-10-03 13:57:23 -0700843 return false;
844 }
845 uint32_t value_width = array_data[1];
Elliott Hughes398f64b2012-03-26 18:05:48 -0700846 uint32_t value_count = *reinterpret_cast<const uint32_t*>(&array_data[2]);
Ian Rogersd81871c2011-10-03 13:57:23 -0700847 uint32_t table_size = 4 + (value_width * value_count + 1) / 2;
848 /* make sure the end of the switch is in range */
849 if (cur_offset + array_data_offset + table_size > insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -0700850 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data end: at " << cur_offset
851 << ", data offset " << array_data_offset << ", end "
852 << cur_offset + array_data_offset + table_size
853 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -0700854 return false;
855 }
856 return true;
857}
858
Ian Rogers776ac1f2012-04-13 23:36:36 -0700859bool MethodVerifier::CheckBranchTarget(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700860 int32_t offset;
861 bool isConditional, selfOkay;
862 if (!GetBranchOffset(cur_offset, &offset, &isConditional, &selfOkay)) {
863 return false;
864 }
865 if (!selfOkay && offset == 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700866 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch offset of zero not allowed at"
867 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -0700868 return false;
869 }
Elliott Hughes81ff3182012-03-23 20:35:56 -0700870 // Check for 32-bit overflow. This isn't strictly necessary if we can depend on the runtime
871 // to have identical "wrap-around" behavior, but it's unwise to depend on that.
Ian Rogersd81871c2011-10-03 13:57:23 -0700872 if (((int64_t) cur_offset + (int64_t) offset) != (int64_t) (cur_offset + offset)) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700873 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch target overflow "
874 << reinterpret_cast<void*>(cur_offset) << " +" << offset;
Ian Rogersd81871c2011-10-03 13:57:23 -0700875 return false;
876 }
877 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
878 int32_t abs_offset = cur_offset + offset;
Brian Carlstrom93c33962013-07-26 10:37:43 -0700879 if (abs_offset < 0 ||
880 (uint32_t) abs_offset >= insn_count ||
881 !insn_flags_[abs_offset].IsOpcode()) {
jeffhaod5347e02012-03-22 17:25:05 -0700882 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid branch target " << offset << " (-> "
Elliott Hughes398f64b2012-03-26 18:05:48 -0700883 << reinterpret_cast<void*>(abs_offset) << ") at "
884 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -0700885 return false;
886 }
887 insn_flags_[abs_offset].SetBranchTarget();
888 return true;
889}
890
Ian Rogers776ac1f2012-04-13 23:36:36 -0700891bool MethodVerifier::GetBranchOffset(uint32_t cur_offset, int32_t* pOffset, bool* pConditional,
Ian Rogersd81871c2011-10-03 13:57:23 -0700892 bool* selfOkay) {
893 const uint16_t* insns = code_item_->insns_ + cur_offset;
894 *pConditional = false;
895 *selfOkay = false;
jeffhaoba5ebb92011-08-25 17:24:37 -0700896 switch (*insns & 0xff) {
897 case Instruction::GOTO:
898 *pOffset = ((int16_t) *insns) >> 8;
jeffhaoba5ebb92011-08-25 17:24:37 -0700899 break;
900 case Instruction::GOTO_32:
901 *pOffset = insns[1] | (((uint32_t) insns[2]) << 16);
jeffhaoba5ebb92011-08-25 17:24:37 -0700902 *selfOkay = true;
903 break;
904 case Instruction::GOTO_16:
905 *pOffset = (int16_t) insns[1];
jeffhaoba5ebb92011-08-25 17:24:37 -0700906 break;
907 case Instruction::IF_EQ:
908 case Instruction::IF_NE:
909 case Instruction::IF_LT:
910 case Instruction::IF_GE:
911 case Instruction::IF_GT:
912 case Instruction::IF_LE:
913 case Instruction::IF_EQZ:
914 case Instruction::IF_NEZ:
915 case Instruction::IF_LTZ:
916 case Instruction::IF_GEZ:
917 case Instruction::IF_GTZ:
918 case Instruction::IF_LEZ:
919 *pOffset = (int16_t) insns[1];
920 *pConditional = true;
jeffhaoba5ebb92011-08-25 17:24:37 -0700921 break;
922 default:
923 return false;
924 break;
925 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700926 return true;
927}
928
Ian Rogers776ac1f2012-04-13 23:36:36 -0700929bool MethodVerifier::CheckSwitchTargets(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700930 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700931 DCHECK_LT(cur_offset, insn_count);
Ian Rogersd81871c2011-10-03 13:57:23 -0700932 const uint16_t* insns = code_item_->insns_ + cur_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -0700933 /* make sure the start of the switch is in range */
Ian Rogersd81871c2011-10-03 13:57:23 -0700934 int32_t switch_offset = insns[1] | ((int32_t) insns[2]) << 16;
935 if ((int32_t) cur_offset + switch_offset < 0 || cur_offset + switch_offset + 2 >= insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -0700936 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch start: at " << cur_offset
Brian Carlstrom93c33962013-07-26 10:37:43 -0700937 << ", switch offset " << switch_offset
938 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -0700939 return false;
940 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700941 /* offset to switch table is a relative branch-style offset */
Ian Rogersd81871c2011-10-03 13:57:23 -0700942 const uint16_t* switch_insns = insns + switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -0700943 /* make sure the table is 32-bit aligned */
944 if ((((uint32_t) switch_insns) & 0x03) != 0) {
jeffhaod5347e02012-03-22 17:25:05 -0700945 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned switch table: at " << cur_offset
946 << ", switch offset " << switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -0700947 return false;
948 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700949 uint32_t switch_count = switch_insns[1];
950 int32_t keys_offset, targets_offset;
951 uint16_t expected_signature;
jeffhaoba5ebb92011-08-25 17:24:37 -0700952 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
953 /* 0=sig, 1=count, 2/3=firstKey */
954 targets_offset = 4;
955 keys_offset = -1;
956 expected_signature = Instruction::kPackedSwitchSignature;
957 } else {
958 /* 0=sig, 1=count, 2..count*2 = keys */
959 keys_offset = 2;
960 targets_offset = 2 + 2 * switch_count;
961 expected_signature = Instruction::kSparseSwitchSignature;
962 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700963 uint32_t table_size = targets_offset + switch_count * 2;
jeffhaoba5ebb92011-08-25 17:24:37 -0700964 if (switch_insns[0] != expected_signature) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700965 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
966 << StringPrintf("wrong signature for switch table (%x, wanted %x)",
967 switch_insns[0], expected_signature);
jeffhaoba5ebb92011-08-25 17:24:37 -0700968 return false;
969 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700970 /* make sure the end of the switch is in range */
971 if (cur_offset + switch_offset + table_size > (uint32_t) insn_count) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700972 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch end: at " << cur_offset
973 << ", switch offset " << switch_offset
974 << ", end " << (cur_offset + switch_offset + table_size)
jeffhaod5347e02012-03-22 17:25:05 -0700975 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -0700976 return false;
977 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700978 /* for a sparse switch, verify the keys are in ascending order */
979 if (keys_offset > 0 && switch_count > 1) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700980 int32_t last_key = switch_insns[keys_offset] | (switch_insns[keys_offset + 1] << 16);
981 for (uint32_t targ = 1; targ < switch_count; targ++) {
jeffhaoba5ebb92011-08-25 17:24:37 -0700982 int32_t key = (int32_t) switch_insns[keys_offset + targ * 2] |
983 (int32_t) (switch_insns[keys_offset + targ * 2 + 1] << 16);
984 if (key <= last_key) {
jeffhaod5347e02012-03-22 17:25:05 -0700985 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid packed switch: last key=" << last_key
986 << ", this=" << key;
jeffhaoba5ebb92011-08-25 17:24:37 -0700987 return false;
988 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700989 last_key = key;
990 }
991 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700992 /* verify each switch target */
Ian Rogersd81871c2011-10-03 13:57:23 -0700993 for (uint32_t targ = 0; targ < switch_count; targ++) {
994 int32_t offset = (int32_t) switch_insns[targets_offset + targ * 2] |
995 (int32_t) (switch_insns[targets_offset + targ * 2 + 1] << 16);
996 int32_t abs_offset = cur_offset + offset;
Brian Carlstrom93c33962013-07-26 10:37:43 -0700997 if (abs_offset < 0 ||
998 abs_offset >= (int32_t) insn_count ||
999 !insn_flags_[abs_offset].IsOpcode()) {
1000 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch target " << offset
1001 << " (-> " << reinterpret_cast<void*>(abs_offset) << ") at "
1002 << reinterpret_cast<void*>(cur_offset)
1003 << "[" << targ << "]";
jeffhaoba5ebb92011-08-25 17:24:37 -07001004 return false;
1005 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001006 insn_flags_[abs_offset].SetBranchTarget();
1007 }
1008 return true;
1009}
1010
Ian Rogers776ac1f2012-04-13 23:36:36 -07001011bool MethodVerifier::CheckVarArgRegs(uint32_t vA, uint32_t arg[]) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001012 if (vA > 5) {
jeffhaod5347e02012-03-22 17:25:05 -07001013 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << vA << ") in non-range invoke)";
Ian Rogersd81871c2011-10-03 13:57:23 -07001014 return false;
1015 }
1016 uint16_t registers_size = code_item_->registers_size_;
1017 for (uint32_t idx = 0; idx < vA; idx++) {
jeffhao457cc512012-02-02 16:55:13 -08001018 if (arg[idx] >= registers_size) {
jeffhaod5347e02012-03-22 17:25:05 -07001019 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index (" << arg[idx]
1020 << ") in non-range invoke (>= " << registers_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001021 return false;
1022 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001023 }
1024
1025 return true;
1026}
1027
Ian Rogers776ac1f2012-04-13 23:36:36 -07001028bool MethodVerifier::CheckVarArgRangeRegs(uint32_t vA, uint32_t vC) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001029 uint16_t registers_size = code_item_->registers_size_;
1030 // vA/vC are unsigned 8-bit/16-bit quantities for /range instructions, so there's no risk of
1031 // integer overflow when adding them here.
1032 if (vA + vC > registers_size) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001033 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index " << vA << "+" << vC
1034 << " in range invoke (> " << registers_size << ")";
jeffhaoba5ebb92011-08-25 17:24:37 -07001035 return false;
1036 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001037 return true;
1038}
1039
Ian Rogers776ac1f2012-04-13 23:36:36 -07001040bool MethodVerifier::VerifyCodeFlow() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001041 uint16_t registers_size = code_item_->registers_size_;
1042 uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaobdb76512011-09-07 11:43:16 -07001043
Ian Rogersd81871c2011-10-03 13:57:23 -07001044 if (registers_size * insns_size > 4*1024*1024) {
buzbee4922ef92012-02-24 14:32:20 -08001045 LOG(WARNING) << "warning: method is huge (regs=" << registers_size
1046 << " insns_size=" << insns_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001047 }
1048 /* Create and initialize table holding register status */
Brian Carlstrom93c33962013-07-26 10:37:43 -07001049 reg_table_.Init(kTrackCompilerInterestPoints,
1050 insn_flags_.get(),
1051 insns_size,
1052 registers_size,
1053 this);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07001054
jeffhaobdb76512011-09-07 11:43:16 -07001055
Ian Rogersd0fbd852013-09-24 18:17:04 -07001056 work_line_.reset(RegisterLine::Create(registers_size, this));
1057 saved_line_.reset(RegisterLine::Create(registers_size, this));
jeffhaobdb76512011-09-07 11:43:16 -07001058
Ian Rogersd81871c2011-10-03 13:57:23 -07001059 /* Initialize register types of method arguments. */
1060 if (!SetTypesFromSignature()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001061 DCHECK_NE(failures_.size(), 0U);
1062 std::string prepend("Bad signature in ");
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001063 prepend += PrettyMethod(dex_method_idx_, *dex_file_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001064 PrependToLastFailMessage(prepend);
Ian Rogersd81871c2011-10-03 13:57:23 -07001065 return false;
1066 }
1067 /* Perform code flow verification. */
1068 if (!CodeFlowVerifyMethod()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001069 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -07001070 return false;
jeffhaobdb76512011-09-07 11:43:16 -07001071 }
jeffhaobdb76512011-09-07 11:43:16 -07001072 return true;
1073}
1074
Ian Rogersad0b3a32012-04-16 14:50:24 -07001075std::ostream& MethodVerifier::DumpFailures(std::ostream& os) {
1076 DCHECK_EQ(failures_.size(), failure_messages_.size());
Jeff Hao4137f482013-11-22 11:44:57 -08001077 for (size_t i = 0; i < failures_.size(); ++i) {
1078 os << failure_messages_[i]->str() << "\n";
Ian Rogersad0b3a32012-04-16 14:50:24 -07001079 }
1080 return os;
1081}
1082
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001083extern "C" void MethodVerifierGdbDump(MethodVerifier* v)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001084 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001085 v->Dump(std::cerr);
1086}
1087
Ian Rogers776ac1f2012-04-13 23:36:36 -07001088void MethodVerifier::Dump(std::ostream& os) {
jeffhaof56197c2012-03-05 18:01:54 -08001089 if (code_item_ == NULL) {
Elliott Hughesc073b072012-05-24 19:29:17 -07001090 os << "Native method\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001091 return;
jeffhaobdb76512011-09-07 11:43:16 -07001092 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001093 {
1094 os << "Register Types:\n";
1095 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
1096 std::ostream indent_os(&indent_filter);
1097 reg_types_.Dump(indent_os);
1098 }
Ian Rogersb4903572012-10-11 11:52:56 -07001099 os << "Dumping instructions and register lines:\n";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001100 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
1101 std::ostream indent_os(&indent_filter);
Ian Rogersd81871c2011-10-03 13:57:23 -07001102 const Instruction* inst = Instruction::At(code_item_->insns_);
1103 for (size_t dex_pc = 0; dex_pc < code_item_->insns_size_in_code_units_;
1104 dex_pc += insn_flags_[dex_pc].GetLengthInCodeUnits()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001105 RegisterLine* reg_line = reg_table_.GetLine(dex_pc);
1106 if (reg_line != NULL) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001107 indent_os << reg_line->Dump() << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07001108 }
Ian Rogers7b3ddd22013-02-21 15:19:52 -08001109 indent_os << StringPrintf("0x%04zx", dex_pc) << ": " << insn_flags_[dex_pc].ToString() << " ";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001110 const bool kDumpHexOfInstruction = false;
1111 if (kDumpHexOfInstruction) {
1112 indent_os << inst->DumpHex(5) << " ";
1113 }
1114 indent_os << inst->DumpString(dex_file_) << "\n";
jeffhaoba5ebb92011-08-25 17:24:37 -07001115 inst = inst->Next();
1116 }
jeffhaobdb76512011-09-07 11:43:16 -07001117}
1118
Ian Rogersd81871c2011-10-03 13:57:23 -07001119static bool IsPrimitiveDescriptor(char descriptor) {
1120 switch (descriptor) {
jeffhaobdb76512011-09-07 11:43:16 -07001121 case 'I':
1122 case 'C':
1123 case 'S':
1124 case 'B':
1125 case 'Z':
jeffhaobdb76512011-09-07 11:43:16 -07001126 case 'F':
1127 case 'D':
1128 case 'J':
Ian Rogersd81871c2011-10-03 13:57:23 -07001129 return true;
jeffhaobdb76512011-09-07 11:43:16 -07001130 default:
1131 return false;
1132 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001133}
1134
Ian Rogers776ac1f2012-04-13 23:36:36 -07001135bool MethodVerifier::SetTypesFromSignature() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001136 RegisterLine* reg_line = reg_table_.GetLine(0);
1137 int arg_start = code_item_->registers_size_ - code_item_->ins_size_;
1138 size_t expected_args = code_item_->ins_size_; /* long/double count as two */
jeffhaobdb76512011-09-07 11:43:16 -07001139
Ian Rogersd81871c2011-10-03 13:57:23 -07001140 DCHECK_GE(arg_start, 0); /* should have been verified earlier */
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001141 // Include the "this" pointer.
Ian Rogersd81871c2011-10-03 13:57:23 -07001142 size_t cur_arg = 0;
Ian Rogersad0b3a32012-04-16 14:50:24 -07001143 if (!IsStatic()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001144 // If this is a constructor for a class other than java.lang.Object, mark the first ("this")
1145 // argument as uninitialized. This restricts field access until the superclass constructor is
1146 // called.
Ian Rogersad0b3a32012-04-16 14:50:24 -07001147 const RegType& declaring_class = GetDeclaringClass();
1148 if (IsConstructor() && !declaring_class.IsJavaLangObject()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001149 reg_line->SetRegisterType(arg_start + cur_arg,
1150 reg_types_.UninitializedThisArgument(declaring_class));
1151 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001152 reg_line->SetRegisterType(arg_start + cur_arg, declaring_class);
jeffhaobdb76512011-09-07 11:43:16 -07001153 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001154 cur_arg++;
jeffhaobdb76512011-09-07 11:43:16 -07001155 }
1156
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001157 const DexFile::ProtoId& proto_id =
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001158 dex_file_->GetMethodPrototype(dex_file_->GetMethodId(dex_method_idx_));
Ian Rogers0571d352011-11-03 19:51:38 -07001159 DexFileParameterIterator iterator(*dex_file_, proto_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07001160
1161 for (; iterator.HasNext(); iterator.Next()) {
1162 const char* descriptor = iterator.GetDescriptor();
1163 if (descriptor == NULL) {
1164 LOG(FATAL) << "Null descriptor";
1165 }
1166 if (cur_arg >= expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07001167 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1168 << " args, found more (" << descriptor << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001169 return false;
1170 }
1171 switch (descriptor[0]) {
1172 case 'L':
1173 case '[':
1174 // We assume that reference arguments are initialized. The only way it could be otherwise
1175 // (assuming the caller was verified) is if the current method is <init>, but in that case
1176 // it's effectively considered initialized the instant we reach here (in the sense that we
1177 // can return without doing anything or call virtual methods).
1178 {
Mathieu Chartier590fee92013-09-13 13:46:47 -07001179 const RegType& reg_type = reg_types_.FromDescriptor(class_loader_->get(), descriptor,
1180 false);
Ian Rogers84fa0742011-10-25 18:13:30 -07001181 reg_line->SetRegisterType(arg_start + cur_arg, reg_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001182 }
1183 break;
1184 case 'Z':
1185 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Boolean());
1186 break;
1187 case 'C':
1188 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Char());
1189 break;
1190 case 'B':
1191 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Byte());
1192 break;
1193 case 'I':
1194 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Integer());
1195 break;
1196 case 'S':
1197 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Short());
1198 break;
1199 case 'F':
1200 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Float());
1201 break;
1202 case 'J':
1203 case 'D': {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001204 const RegType& lo_half = descriptor[0] == 'J' ? reg_types_.LongLo() : reg_types_.DoubleLo();
1205 const RegType& hi_half = descriptor[0] == 'J' ? reg_types_.LongHi() : reg_types_.DoubleHi();
1206 reg_line->SetRegisterTypeWide(arg_start + cur_arg, lo_half, hi_half);
Ian Rogersd81871c2011-10-03 13:57:23 -07001207 cur_arg++;
1208 break;
1209 }
1210 default:
Brian Carlstrom93c33962013-07-26 10:37:43 -07001211 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected signature type char '"
1212 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001213 return false;
1214 }
1215 cur_arg++;
1216 }
1217 if (cur_arg != expected_args) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001218 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1219 << " arguments, found " << cur_arg;
Ian Rogersd81871c2011-10-03 13:57:23 -07001220 return false;
1221 }
1222 const char* descriptor = dex_file_->GetReturnTypeDescriptor(proto_id);
1223 // Validate return type. We don't do the type lookup; just want to make sure that it has the right
1224 // format. Only major difference from the method argument format is that 'V' is supported.
1225 bool result;
1226 if (IsPrimitiveDescriptor(descriptor[0]) || descriptor[0] == 'V') {
1227 result = descriptor[1] == '\0';
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001228 } else if (descriptor[0] == '[') { // single/multi-dimensional array of object/primitive
Ian Rogersd81871c2011-10-03 13:57:23 -07001229 size_t i = 0;
1230 do {
1231 i++;
1232 } while (descriptor[i] == '['); // process leading [
1233 if (descriptor[i] == 'L') { // object array
1234 do {
1235 i++; // find closing ;
1236 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1237 result = descriptor[i] == ';';
1238 } else { // primitive array
1239 result = IsPrimitiveDescriptor(descriptor[i]) && descriptor[i + 1] == '\0';
1240 }
1241 } else if (descriptor[0] == 'L') {
1242 // could be more thorough here, but shouldn't be required
1243 size_t i = 0;
1244 do {
1245 i++;
1246 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1247 result = descriptor[i] == ';';
1248 } else {
1249 result = false;
1250 }
1251 if (!result) {
jeffhaod5347e02012-03-22 17:25:05 -07001252 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected char in return type descriptor '"
1253 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001254 }
1255 return result;
jeffhaobdb76512011-09-07 11:43:16 -07001256}
1257
Ian Rogers776ac1f2012-04-13 23:36:36 -07001258bool MethodVerifier::CodeFlowVerifyMethod() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001259 const uint16_t* insns = code_item_->insns_;
1260 const uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaoba5ebb92011-08-25 17:24:37 -07001261
jeffhaobdb76512011-09-07 11:43:16 -07001262 /* Begin by marking the first instruction as "changed". */
Ian Rogersd81871c2011-10-03 13:57:23 -07001263 insn_flags_[0].SetChanged();
1264 uint32_t start_guess = 0;
jeffhaoba5ebb92011-08-25 17:24:37 -07001265
jeffhaobdb76512011-09-07 11:43:16 -07001266 /* Continue until no instructions are marked "changed". */
1267 while (true) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001268 // Find the first marked one. Use "start_guess" as a way to find one quickly.
1269 uint32_t insn_idx = start_guess;
1270 for (; insn_idx < insns_size; insn_idx++) {
1271 if (insn_flags_[insn_idx].IsChanged())
jeffhaobdb76512011-09-07 11:43:16 -07001272 break;
1273 }
jeffhaobdb76512011-09-07 11:43:16 -07001274 if (insn_idx == insns_size) {
1275 if (start_guess != 0) {
1276 /* try again, starting from the top */
1277 start_guess = 0;
1278 continue;
1279 } else {
1280 /* all flags are clear */
1281 break;
1282 }
1283 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001284 // We carry the working set of registers from instruction to instruction. If this address can
1285 // be the target of a branch (or throw) instruction, or if we're skipping around chasing
1286 // "changed" flags, we need to load the set of registers from the table.
1287 // Because we always prefer to continue on to the next instruction, we should never have a
1288 // situation where we have a stray "changed" flag set on an instruction that isn't a branch
1289 // target.
1290 work_insn_idx_ = insn_idx;
1291 if (insn_flags_[insn_idx].IsBranchTarget()) {
1292 work_line_->CopyFromLine(reg_table_.GetLine(insn_idx));
jeffhaobdb76512011-09-07 11:43:16 -07001293 } else {
1294#ifndef NDEBUG
1295 /*
1296 * Sanity check: retrieve the stored register line (assuming
1297 * a full table) and make sure it actually matches.
1298 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001299 RegisterLine* register_line = reg_table_.GetLine(insn_idx);
1300 if (register_line != NULL) {
1301 if (work_line_->CompareLine(register_line) != 0) {
1302 Dump(std::cout);
1303 std::cout << info_messages_.str();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001304 LOG(FATAL) << "work_line diverged in " << PrettyMethod(dex_method_idx_, *dex_file_)
Elliott Hughesc073b072012-05-24 19:29:17 -07001305 << "@" << reinterpret_cast<void*>(work_insn_idx_) << "\n"
1306 << " work_line=" << *work_line_ << "\n"
Elliott Hughes398f64b2012-03-26 18:05:48 -07001307 << " expected=" << *register_line;
Ian Rogersd81871c2011-10-03 13:57:23 -07001308 }
jeffhaobdb76512011-09-07 11:43:16 -07001309 }
1310#endif
1311 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001312 if (!CodeFlowVerifyInstruction(&start_guess)) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001313 std::string prepend(PrettyMethod(dex_method_idx_, *dex_file_));
Ian Rogersad0b3a32012-04-16 14:50:24 -07001314 prepend += " failed to verify: ";
1315 PrependToLastFailMessage(prepend);
jeffhaoba5ebb92011-08-25 17:24:37 -07001316 return false;
1317 }
jeffhaobdb76512011-09-07 11:43:16 -07001318 /* Clear "changed" and mark as visited. */
Ian Rogersd81871c2011-10-03 13:57:23 -07001319 insn_flags_[insn_idx].SetVisited();
1320 insn_flags_[insn_idx].ClearChanged();
jeffhaobdb76512011-09-07 11:43:16 -07001321 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001322
Ian Rogers1c849e52012-06-28 14:00:33 -07001323 if (gDebugVerify) {
jeffhaobdb76512011-09-07 11:43:16 -07001324 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001325 * Scan for dead code. There's nothing "evil" about dead code
jeffhaobdb76512011-09-07 11:43:16 -07001326 * (besides the wasted space), but it indicates a flaw somewhere
1327 * down the line, possibly in the verifier.
1328 *
1329 * If we've substituted "always throw" instructions into the stream,
1330 * we are almost certainly going to have some dead code.
1331 */
1332 int dead_start = -1;
Ian Rogersd81871c2011-10-03 13:57:23 -07001333 uint32_t insn_idx = 0;
1334 for (; insn_idx < insns_size; insn_idx += insn_flags_[insn_idx].GetLengthInCodeUnits()) {
jeffhaobdb76512011-09-07 11:43:16 -07001335 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001336 * Switch-statement data doesn't get "visited" by scanner. It
jeffhaobdb76512011-09-07 11:43:16 -07001337 * may or may not be preceded by a padding NOP (for alignment).
1338 */
1339 if (insns[insn_idx] == Instruction::kPackedSwitchSignature ||
1340 insns[insn_idx] == Instruction::kSparseSwitchSignature ||
1341 insns[insn_idx] == Instruction::kArrayDataSignature ||
Elliott Hughes380aaa72012-07-09 14:33:15 -07001342 (insns[insn_idx] == Instruction::NOP && (insn_idx + 1 < insns_size) &&
jeffhaobdb76512011-09-07 11:43:16 -07001343 (insns[insn_idx + 1] == Instruction::kPackedSwitchSignature ||
1344 insns[insn_idx + 1] == Instruction::kSparseSwitchSignature ||
1345 insns[insn_idx + 1] == Instruction::kArrayDataSignature))) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001346 insn_flags_[insn_idx].SetVisited();
jeffhaobdb76512011-09-07 11:43:16 -07001347 }
1348
Ian Rogersd81871c2011-10-03 13:57:23 -07001349 if (!insn_flags_[insn_idx].IsVisited()) {
jeffhaobdb76512011-09-07 11:43:16 -07001350 if (dead_start < 0)
1351 dead_start = insn_idx;
1352 } else if (dead_start >= 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001353 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start)
1354 << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaobdb76512011-09-07 11:43:16 -07001355 dead_start = -1;
1356 }
1357 }
1358 if (dead_start >= 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001359 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start)
1360 << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaoba5ebb92011-08-25 17:24:37 -07001361 }
Ian Rogersc9e463c2013-06-05 16:52:26 -07001362 // To dump the state of the verify after a method, do something like:
1363 // if (PrettyMethod(dex_method_idx_, *dex_file_) ==
1364 // "boolean java.lang.String.equals(java.lang.Object)") {
1365 // LOG(INFO) << info_messages_.str();
1366 // }
jeffhaoba5ebb92011-08-25 17:24:37 -07001367 }
jeffhaobdb76512011-09-07 11:43:16 -07001368 return true;
1369}
1370
Ian Rogers776ac1f2012-04-13 23:36:36 -07001371bool MethodVerifier::CodeFlowVerifyInstruction(uint32_t* start_guess) {
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001372 // If we're doing FindLocksAtDexPc, check whether we're at the dex pc we care about.
1373 // We want the state _before_ the instruction, for the case where the dex pc we're
1374 // interested in is itself a monitor-enter instruction (which is a likely place
1375 // for a thread to be suspended).
1376 if (monitor_enter_dex_pcs_ != NULL && work_insn_idx_ == interesting_dex_pc_) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001377 monitor_enter_dex_pcs_->clear(); // The new work line is more accurate than the previous one.
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001378 for (size_t i = 0; i < work_line_->GetMonitorEnterCount(); ++i) {
1379 monitor_enter_dex_pcs_->push_back(work_line_->GetMonitorEnterDexPc(i));
1380 }
1381 }
1382
jeffhaobdb76512011-09-07 11:43:16 -07001383 /*
1384 * Once we finish decoding the instruction, we need to figure out where
jeffhaod1f0fde2011-09-08 17:25:33 -07001385 * we can go from here. There are three possible ways to transfer
jeffhaobdb76512011-09-07 11:43:16 -07001386 * control to another statement:
1387 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001388 * (1) Continue to the next instruction. Applies to all but
jeffhaobdb76512011-09-07 11:43:16 -07001389 * unconditional branches, method returns, and exception throws.
jeffhaod1f0fde2011-09-08 17:25:33 -07001390 * (2) Branch to one or more possible locations. Applies to branches
jeffhaobdb76512011-09-07 11:43:16 -07001391 * and switch statements.
jeffhaod1f0fde2011-09-08 17:25:33 -07001392 * (3) Exception handlers. Applies to any instruction that can
jeffhaobdb76512011-09-07 11:43:16 -07001393 * throw an exception that is handled by an encompassing "try"
1394 * block.
1395 *
1396 * We can also return, in which case there is no successor instruction
1397 * from this point.
1398 *
Elliott Hughesadb8c672012-03-06 16:49:32 -08001399 * The behavior can be determined from the opcode flags.
jeffhaobdb76512011-09-07 11:43:16 -07001400 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001401 const uint16_t* insns = code_item_->insns_ + work_insn_idx_;
1402 const Instruction* inst = Instruction::At(insns);
Ian Rogersa75a0132012-09-28 11:41:42 -07001403 int opcode_flags = Instruction::FlagsOf(inst->Opcode());
jeffhaobdb76512011-09-07 11:43:16 -07001404
jeffhaobdb76512011-09-07 11:43:16 -07001405 int32_t branch_target = 0;
jeffhaobdb76512011-09-07 11:43:16 -07001406 bool just_set_result = false;
Ian Rogers2c8a8572011-10-24 17:11:36 -07001407 if (gDebugVerify) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001408 // Generate processing back trace to debug verifier
Elliott Hughesc073b072012-05-24 19:29:17 -07001409 LogVerifyInfo() << "Processing " << inst->DumpString(dex_file_) << "\n"
1410 << *work_line_.get() << "\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001411 }
jeffhaobdb76512011-09-07 11:43:16 -07001412
1413 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001414 * Make a copy of the previous register state. If the instruction
jeffhaobdb76512011-09-07 11:43:16 -07001415 * can throw an exception, we will copy/merge this into the "catch"
1416 * address rather than work_line, because we don't want the result
1417 * from the "successful" code path (e.g. a check-cast that "improves"
1418 * a type) to be visible to the exception handler.
1419 */
Ian Rogers776ac1f2012-04-13 23:36:36 -07001420 if ((opcode_flags & Instruction::kThrow) != 0 && CurrentInsnFlags()->IsInTry()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001421 saved_line_->CopyFromLine(work_line_.get());
jeffhaobdb76512011-09-07 11:43:16 -07001422 } else {
1423#ifndef NDEBUG
Ian Rogersd81871c2011-10-03 13:57:23 -07001424 saved_line_->FillWithGarbage();
jeffhaobdb76512011-09-07 11:43:16 -07001425#endif
1426 }
1427
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07001428
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001429 // We need to ensure the work line is consistent while performing validation. When we spot a
1430 // peephole pattern we compute a new line for either the fallthrough instruction or the
1431 // branch target.
1432 UniquePtr<RegisterLine> branch_line;
1433 UniquePtr<RegisterLine> fallthrough_line;
1434
Sebastien Hertz5243e912013-05-21 10:55:07 +02001435 switch (inst->Opcode()) {
jeffhaobdb76512011-09-07 11:43:16 -07001436 case Instruction::NOP:
1437 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001438 * A "pure" NOP has no effect on anything. Data tables start with
jeffhaobdb76512011-09-07 11:43:16 -07001439 * a signature that looks like a NOP; if we see one of these in
1440 * the course of executing code then we have a problem.
1441 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001442 if (inst->VRegA_10x() != 0) {
jeffhaod5347e02012-03-22 17:25:05 -07001443 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "encountered data table in instruction stream";
jeffhaobdb76512011-09-07 11:43:16 -07001444 }
1445 break;
1446
1447 case Instruction::MOVE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001448 work_line_->CopyRegister1(inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategory1nr);
1449 break;
jeffhaobdb76512011-09-07 11:43:16 -07001450 case Instruction::MOVE_FROM16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001451 work_line_->CopyRegister1(inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategory1nr);
1452 break;
jeffhaobdb76512011-09-07 11:43:16 -07001453 case Instruction::MOVE_16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001454 work_line_->CopyRegister1(inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategory1nr);
jeffhaobdb76512011-09-07 11:43:16 -07001455 break;
1456 case Instruction::MOVE_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001457 work_line_->CopyRegister2(inst->VRegA_12x(), inst->VRegB_12x());
1458 break;
jeffhaobdb76512011-09-07 11:43:16 -07001459 case Instruction::MOVE_WIDE_FROM16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001460 work_line_->CopyRegister2(inst->VRegA_22x(), inst->VRegB_22x());
1461 break;
jeffhaobdb76512011-09-07 11:43:16 -07001462 case Instruction::MOVE_WIDE_16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001463 work_line_->CopyRegister2(inst->VRegA_32x(), inst->VRegB_32x());
jeffhaobdb76512011-09-07 11:43:16 -07001464 break;
1465 case Instruction::MOVE_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001466 work_line_->CopyRegister1(inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategoryRef);
1467 break;
jeffhaobdb76512011-09-07 11:43:16 -07001468 case Instruction::MOVE_OBJECT_FROM16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001469 work_line_->CopyRegister1(inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategoryRef);
1470 break;
jeffhaobdb76512011-09-07 11:43:16 -07001471 case Instruction::MOVE_OBJECT_16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001472 work_line_->CopyRegister1(inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategoryRef);
jeffhaobdb76512011-09-07 11:43:16 -07001473 break;
1474
1475 /*
1476 * The move-result instructions copy data out of a "pseudo-register"
jeffhaod1f0fde2011-09-08 17:25:33 -07001477 * with the results from the last method invocation. In practice we
jeffhaobdb76512011-09-07 11:43:16 -07001478 * might want to hold the result in an actual CPU register, so the
1479 * Dalvik spec requires that these only appear immediately after an
1480 * invoke or filled-new-array.
1481 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001482 * These calls invalidate the "result" register. (This is now
jeffhaobdb76512011-09-07 11:43:16 -07001483 * redundant with the reset done below, but it can make the debug info
1484 * easier to read in some cases.)
1485 */
1486 case Instruction::MOVE_RESULT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001487 work_line_->CopyResultRegister1(inst->VRegA_11x(), false);
jeffhaobdb76512011-09-07 11:43:16 -07001488 break;
1489 case Instruction::MOVE_RESULT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001490 work_line_->CopyResultRegister2(inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07001491 break;
1492 case Instruction::MOVE_RESULT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001493 work_line_->CopyResultRegister1(inst->VRegA_11x(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001494 break;
1495
Ian Rogersd81871c2011-10-03 13:57:23 -07001496 case Instruction::MOVE_EXCEPTION: {
jeffhaobdb76512011-09-07 11:43:16 -07001497 /*
jeffhao60f83e32012-02-13 17:16:30 -08001498 * This statement can only appear as the first instruction in an exception handler. We verify
1499 * that as part of extracting the exception type from the catch block list.
jeffhaobdb76512011-09-07 11:43:16 -07001500 */
Ian Rogers28ad40d2011-10-27 15:19:26 -07001501 const RegType& res_type = GetCaughtExceptionType();
Sebastien Hertz5243e912013-05-21 10:55:07 +02001502 work_line_->SetRegisterType(inst->VRegA_11x(), res_type);
jeffhaobdb76512011-09-07 11:43:16 -07001503 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001504 }
jeffhaobdb76512011-09-07 11:43:16 -07001505 case Instruction::RETURN_VOID:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001506 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
1507 if (!GetMethodReturnType().IsConflict()) {
jeffhaod5347e02012-03-22 17:25:05 -07001508 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001509 }
jeffhaobdb76512011-09-07 11:43:16 -07001510 }
1511 break;
1512 case Instruction::RETURN:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001513 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
jeffhaobdb76512011-09-07 11:43:16 -07001514 /* check the method signature */
Ian Rogersd81871c2011-10-03 13:57:23 -07001515 const RegType& return_type = GetMethodReturnType();
1516 if (!return_type.IsCategory1Types()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001517 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected non-category 1 return type "
1518 << return_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001519 } else {
1520 // Compilers may generate synthetic functions that write byte values into boolean fields.
1521 // Also, it may use integer values for boolean, byte, short, and character return types.
Sebastien Hertz5243e912013-05-21 10:55:07 +02001522 const uint32_t vregA = inst->VRegA_11x();
1523 const RegType& src_type = work_line_->GetRegisterType(vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07001524 bool use_src = ((return_type.IsBoolean() && src_type.IsByte()) ||
1525 ((return_type.IsBoolean() || return_type.IsByte() ||
1526 return_type.IsShort() || return_type.IsChar()) &&
1527 src_type.IsInteger()));
1528 /* check the register contents */
Ian Rogersad0b3a32012-04-16 14:50:24 -07001529 bool success =
Sebastien Hertz5243e912013-05-21 10:55:07 +02001530 work_line_->VerifyRegisterType(vregA, use_src ? src_type : return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001531 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001532 AppendToLastFailMessage(StringPrintf(" return-1nr on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07001533 }
jeffhaobdb76512011-09-07 11:43:16 -07001534 }
1535 }
1536 break;
1537 case Instruction::RETURN_WIDE:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001538 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
jeffhaobdb76512011-09-07 11:43:16 -07001539 /* check the method signature */
Ian Rogersd81871c2011-10-03 13:57:23 -07001540 const RegType& return_type = GetMethodReturnType();
1541 if (!return_type.IsCategory2Types()) {
jeffhaod5347e02012-03-22 17:25:05 -07001542 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-wide not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001543 } else {
1544 /* check the register contents */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001545 const uint32_t vregA = inst->VRegA_11x();
1546 bool success = work_line_->VerifyRegisterType(vregA, return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001547 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001548 AppendToLastFailMessage(StringPrintf(" return-wide on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07001549 }
jeffhaobdb76512011-09-07 11:43:16 -07001550 }
1551 }
1552 break;
1553 case Instruction::RETURN_OBJECT:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001554 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001555 const RegType& return_type = GetMethodReturnType();
1556 if (!return_type.IsReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001557 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-object not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001558 } else {
1559 /* return_type is the *expected* return type, not register value */
1560 DCHECK(!return_type.IsZero());
1561 DCHECK(!return_type.IsUninitializedReference());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001562 const uint32_t vregA = inst->VRegA_11x();
1563 const RegType& reg_type = work_line_->GetRegisterType(vregA);
Ian Rogers9074b992011-10-26 17:41:55 -07001564 // Disallow returning uninitialized values and verify that the reference in vAA is an
1565 // instance of the "return_type"
1566 if (reg_type.IsUninitializedTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001567 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "returning uninitialized object '"
1568 << reg_type << "'";
Ian Rogers9074b992011-10-26 17:41:55 -07001569 } else if (!return_type.IsAssignableFrom(reg_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07001570 if (reg_type.IsUnresolvedTypes() || return_type.IsUnresolvedTypes()) {
1571 Fail(VERIFY_ERROR_NO_CLASS) << " can't resolve returned type '" << return_type
1572 << "' or '" << reg_type << "'";
1573 } else {
1574 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning '" << reg_type
1575 << "', but expected from declaration '" << return_type << "'";
1576 }
jeffhaobdb76512011-09-07 11:43:16 -07001577 }
1578 }
1579 }
1580 break;
1581
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001582 /* could be boolean, int, float, or a null reference */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001583 case Instruction::CONST_4: {
1584 int32_t val = static_cast<int32_t>(inst->VRegB_11n() << 28) >> 28;
1585 work_line_->SetRegisterType(inst->VRegA_11n(), reg_types_.FromCat1Const(val, true));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001586 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02001587 }
1588 case Instruction::CONST_16: {
1589 int16_t val = static_cast<int16_t>(inst->VRegB_21s());
1590 work_line_->SetRegisterType(inst->VRegA_21s(), reg_types_.FromCat1Const(val, true));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001591 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02001592 }
jeffhaobdb76512011-09-07 11:43:16 -07001593 case Instruction::CONST:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001594 work_line_->SetRegisterType(inst->VRegA_31i(),
1595 reg_types_.FromCat1Const(inst->VRegB_31i(), true));
jeffhaobdb76512011-09-07 11:43:16 -07001596 break;
1597 case Instruction::CONST_HIGH16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001598 work_line_->SetRegisterType(inst->VRegA_21h(),
1599 reg_types_.FromCat1Const(inst->VRegB_21h() << 16, true));
jeffhaobdb76512011-09-07 11:43:16 -07001600 break;
jeffhaobdb76512011-09-07 11:43:16 -07001601 /* could be long or double; resolved upon use */
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001602 case Instruction::CONST_WIDE_16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001603 int64_t val = static_cast<int16_t>(inst->VRegB_21s());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001604 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1605 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001606 work_line_->SetRegisterTypeWide(inst->VRegA_21s(), lo, hi);
jeffhaobdb76512011-09-07 11:43:16 -07001607 break;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001608 }
1609 case Instruction::CONST_WIDE_32: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001610 int64_t val = static_cast<int32_t>(inst->VRegB_31i());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001611 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1612 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001613 work_line_->SetRegisterTypeWide(inst->VRegA_31i(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001614 break;
1615 }
1616 case Instruction::CONST_WIDE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001617 int64_t val = inst->VRegB_51l();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001618 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1619 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001620 work_line_->SetRegisterTypeWide(inst->VRegA_51l(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001621 break;
1622 }
1623 case Instruction::CONST_WIDE_HIGH16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001624 int64_t val = static_cast<uint64_t>(inst->VRegB_21h()) << 48;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001625 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1626 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001627 work_line_->SetRegisterTypeWide(inst->VRegA_21h(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001628 break;
1629 }
jeffhaobdb76512011-09-07 11:43:16 -07001630 case Instruction::CONST_STRING:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001631 work_line_->SetRegisterType(inst->VRegA_21c(), reg_types_.JavaLangString());
1632 break;
jeffhaobdb76512011-09-07 11:43:16 -07001633 case Instruction::CONST_STRING_JUMBO:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001634 work_line_->SetRegisterType(inst->VRegA_31c(), reg_types_.JavaLangString());
jeffhaobdb76512011-09-07 11:43:16 -07001635 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001636 case Instruction::CONST_CLASS: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001637 // Get type from instruction if unresolved then we need an access check
1638 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Sebastien Hertz5243e912013-05-21 10:55:07 +02001639 const RegType& res_type = ResolveClassAndCheckAccess(inst->VRegB_21c());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001640 // Register holds class, ie its type is class, on error it will hold Conflict.
Sebastien Hertz5243e912013-05-21 10:55:07 +02001641 work_line_->SetRegisterType(inst->VRegA_21c(),
Ian Rogersb4903572012-10-11 11:52:56 -07001642 res_type.IsConflict() ? res_type
1643 : reg_types_.JavaLangClass(true));
jeffhaobdb76512011-09-07 11:43:16 -07001644 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001645 }
jeffhaobdb76512011-09-07 11:43:16 -07001646 case Instruction::MONITOR_ENTER:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001647 work_line_->PushMonitor(inst->VRegA_11x(), work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07001648 break;
1649 case Instruction::MONITOR_EXIT:
1650 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001651 * monitor-exit instructions are odd. They can throw exceptions,
jeffhaobdb76512011-09-07 11:43:16 -07001652 * but when they do they act as if they succeeded and the PC is
jeffhaod1f0fde2011-09-08 17:25:33 -07001653 * pointing to the following instruction. (This behavior goes back
jeffhaobdb76512011-09-07 11:43:16 -07001654 * to the need to handle asynchronous exceptions, a now-deprecated
1655 * feature that Dalvik doesn't support.)
1656 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001657 * In practice we don't need to worry about this. The only
jeffhaobdb76512011-09-07 11:43:16 -07001658 * exceptions that can be thrown from monitor-exit are for a
jeffhaod1f0fde2011-09-08 17:25:33 -07001659 * null reference and -exit without a matching -enter. If the
jeffhaobdb76512011-09-07 11:43:16 -07001660 * structured locking checks are working, the former would have
1661 * failed on the -enter instruction, and the latter is impossible.
1662 *
1663 * This is fortunate, because issue 3221411 prevents us from
1664 * chasing the "can throw" path when monitor verification is
jeffhaod1f0fde2011-09-08 17:25:33 -07001665 * enabled. If we can fully verify the locking we can ignore
jeffhaobdb76512011-09-07 11:43:16 -07001666 * some catch blocks (which will show up as "dead" code when
1667 * we skip them here); if we can't, then the code path could be
1668 * "live" so we still need to check it.
1669 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001670 opcode_flags &= ~Instruction::kThrow;
Sebastien Hertz5243e912013-05-21 10:55:07 +02001671 work_line_->PopMonitor(inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07001672 break;
1673
Ian Rogers28ad40d2011-10-27 15:19:26 -07001674 case Instruction::CHECK_CAST:
Ian Rogersd81871c2011-10-03 13:57:23 -07001675 case Instruction::INSTANCE_OF: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001676 /*
1677 * If this instruction succeeds, we will "downcast" register vA to the type in vB. (This
1678 * could be a "upcast" -- not expected, so we don't try to address it.)
1679 *
1680 * If it fails, an exception is thrown, which we deal with later by ignoring the update to
Elliott Hughesadb8c672012-03-06 16:49:32 -08001681 * dec_insn.vA when branching to a handler.
Ian Rogers28ad40d2011-10-27 15:19:26 -07001682 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001683 const bool is_checkcast = (inst->Opcode() == Instruction::CHECK_CAST);
1684 const uint32_t type_idx = (is_checkcast) ? inst->VRegB_21c() : inst->VRegC_22c();
1685 const RegType& res_type = ResolveClassAndCheckAccess(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001686 if (res_type.IsConflict()) {
1687 DCHECK_NE(failures_.size(), 0U);
1688 if (!is_checkcast) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001689 work_line_->SetRegisterType(inst->VRegA_22c(), reg_types_.Boolean());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001690 }
1691 break; // bad class
Ian Rogers9f1ab122011-12-12 08:52:43 -08001692 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001693 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Sebastien Hertz5243e912013-05-21 10:55:07 +02001694 uint32_t orig_type_reg = (is_checkcast) ? inst->VRegA_21c() : inst->VRegB_22c();
1695 const RegType& orig_type = work_line_->GetRegisterType(orig_type_reg);
Ian Rogers28ad40d2011-10-27 15:19:26 -07001696 if (!res_type.IsNonZeroReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001697 if (is_checkcast) {
1698 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on unexpected class " << res_type;
1699 } else {
1700 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on unexpected class " << res_type;
1701 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001702 } else if (!orig_type.IsReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001703 if (is_checkcast) {
1704 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on non-reference in v" << orig_type_reg;
1705 } else {
1706 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on non-reference in v" << orig_type_reg;
1707 }
jeffhao2a8a90e2011-09-26 14:25:31 -07001708 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001709 if (is_checkcast) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001710 work_line_->SetRegisterType(inst->VRegA_21c(), res_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001711 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001712 work_line_->SetRegisterType(inst->VRegA_22c(), reg_types_.Boolean());
jeffhaobdb76512011-09-07 11:43:16 -07001713 }
jeffhaobdb76512011-09-07 11:43:16 -07001714 }
jeffhao2a8a90e2011-09-26 14:25:31 -07001715 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001716 }
1717 case Instruction::ARRAY_LENGTH: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001718 const RegType& res_type = work_line_->GetRegisterType(inst->VRegB_12x());
Ian Rogers28ad40d2011-10-27 15:19:26 -07001719 if (res_type.IsReferenceTypes()) {
Ian Rogers89310de2012-02-01 13:47:30 -08001720 if (!res_type.IsArrayTypes() && !res_type.IsZero()) { // ie not an array or null
jeffhaod5347e02012-03-22 17:25:05 -07001721 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001722 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001723 work_line_->SetRegisterType(inst->VRegA_12x(), reg_types_.Integer());
Ian Rogersd81871c2011-10-03 13:57:23 -07001724 }
1725 }
1726 break;
1727 }
1728 case Instruction::NEW_INSTANCE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001729 const RegType& res_type = ResolveClassAndCheckAccess(inst->VRegB_21c());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001730 if (res_type.IsConflict()) {
1731 DCHECK_NE(failures_.size(), 0U);
1732 break; // bad class
jeffhao8cd6dda2012-02-22 10:15:34 -08001733 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001734 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
1735 // can't create an instance of an interface or abstract class */
1736 if (!res_type.IsInstantiableTypes()) {
1737 Fail(VERIFY_ERROR_INSTANTIATION)
1738 << "new-instance on primitive, interface or abstract class" << res_type;
Ian Rogers08f753d2012-08-24 14:35:25 -07001739 // Soft failure so carry on to set register type.
Ian Rogersd81871c2011-10-03 13:57:23 -07001740 }
Ian Rogers08f753d2012-08-24 14:35:25 -07001741 const RegType& uninit_type = reg_types_.Uninitialized(res_type, work_insn_idx_);
1742 // Any registers holding previous allocations from this address that have not yet been
1743 // initialized must be marked invalid.
1744 work_line_->MarkUninitRefsAsInvalid(uninit_type);
1745 // add the new uninitialized reference to the register state
Sebastien Hertz5243e912013-05-21 10:55:07 +02001746 work_line_->SetRegisterType(inst->VRegA_21c(), uninit_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001747 break;
1748 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08001749 case Instruction::NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001750 VerifyNewArray(inst, false, false);
jeffhaobdb76512011-09-07 11:43:16 -07001751 break;
1752 case Instruction::FILLED_NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001753 VerifyNewArray(inst, true, false);
Ian Rogers0c4a5062012-02-03 15:18:59 -08001754 just_set_result = true; // Filled new array sets result register
jeffhaobdb76512011-09-07 11:43:16 -07001755 break;
Ian Rogers0c4a5062012-02-03 15:18:59 -08001756 case Instruction::FILLED_NEW_ARRAY_RANGE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001757 VerifyNewArray(inst, true, true);
Ian Rogers0c4a5062012-02-03 15:18:59 -08001758 just_set_result = true; // Filled new array range sets result register
1759 break;
jeffhaobdb76512011-09-07 11:43:16 -07001760 case Instruction::CMPL_FLOAT:
1761 case Instruction::CMPG_FLOAT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001762 if (!work_line_->VerifyRegisterType(inst->VRegB_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08001763 break;
1764 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02001765 if (!work_line_->VerifyRegisterType(inst->VRegC_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08001766 break;
1767 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02001768 work_line_->SetRegisterType(inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001769 break;
1770 case Instruction::CMPL_DOUBLE:
1771 case Instruction::CMPG_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001772 if (!work_line_->VerifyRegisterTypeWide(inst->VRegB_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001773 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001774 break;
1775 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02001776 if (!work_line_->VerifyRegisterTypeWide(inst->VRegC_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001777 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001778 break;
1779 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02001780 work_line_->SetRegisterType(inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001781 break;
1782 case Instruction::CMP_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001783 if (!work_line_->VerifyRegisterTypeWide(inst->VRegB_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001784 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001785 break;
1786 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02001787 if (!work_line_->VerifyRegisterTypeWide(inst->VRegC_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001788 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001789 break;
1790 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02001791 work_line_->SetRegisterType(inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001792 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001793 case Instruction::THROW: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001794 const RegType& res_type = work_line_->GetRegisterType(inst->VRegA_11x());
Ian Rogersb4903572012-10-11 11:52:56 -07001795 if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(res_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07001796 Fail(res_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS : VERIFY_ERROR_BAD_CLASS_SOFT)
1797 << "thrown class " << res_type << " not instanceof Throwable";
jeffhaobdb76512011-09-07 11:43:16 -07001798 }
1799 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001800 }
jeffhaobdb76512011-09-07 11:43:16 -07001801 case Instruction::GOTO:
1802 case Instruction::GOTO_16:
1803 case Instruction::GOTO_32:
1804 /* no effect on or use of registers */
1805 break;
1806
1807 case Instruction::PACKED_SWITCH:
1808 case Instruction::SPARSE_SWITCH:
1809 /* verify that vAA is an integer, or can be converted to one */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001810 work_line_->VerifyRegisterType(inst->VRegA_31t(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001811 break;
1812
Ian Rogersd81871c2011-10-03 13:57:23 -07001813 case Instruction::FILL_ARRAY_DATA: {
1814 /* Similar to the verification done for APUT */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001815 const RegType& array_type = work_line_->GetRegisterType(inst->VRegA_31t());
Ian Rogers89310de2012-02-01 13:47:30 -08001816 /* array_type can be null if the reg type is Zero */
1817 if (!array_type.IsZero()) {
jeffhao457cc512012-02-02 16:55:13 -08001818 if (!array_type.IsArrayTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001819 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with array type "
1820 << array_type;
Ian Rogers89310de2012-02-01 13:47:30 -08001821 } else {
Mathieu Chartier590fee92013-09-13 13:46:47 -07001822 const RegType& component_type = reg_types_.GetComponentType(array_type,
1823 class_loader_->get());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001824 DCHECK(!component_type.IsConflict());
jeffhao457cc512012-02-02 16:55:13 -08001825 if (component_type.IsNonZeroReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001826 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with component type "
1827 << component_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001828 } else {
jeffhao457cc512012-02-02 16:55:13 -08001829 // Now verify if the element width in the table matches the element width declared in
1830 // the array
1831 const uint16_t* array_data = insns + (insns[1] | (((int32_t) insns[2]) << 16));
1832 if (array_data[0] != Instruction::kArrayDataSignature) {
jeffhaod5347e02012-03-22 17:25:05 -07001833 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid magic for array-data";
jeffhao457cc512012-02-02 16:55:13 -08001834 } else {
1835 size_t elem_width = Primitive::ComponentSize(component_type.GetPrimitiveType());
1836 // Since we don't compress the data in Dex, expect to see equal width of data stored
1837 // in the table and expected from the array class.
1838 if (array_data[1] != elem_width) {
jeffhaod5347e02012-03-22 17:25:05 -07001839 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-data size mismatch (" << array_data[1]
1840 << " vs " << elem_width << ")";
jeffhao457cc512012-02-02 16:55:13 -08001841 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001842 }
1843 }
jeffhaobdb76512011-09-07 11:43:16 -07001844 }
1845 }
1846 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001847 }
jeffhaobdb76512011-09-07 11:43:16 -07001848 case Instruction::IF_EQ:
Ian Rogersd81871c2011-10-03 13:57:23 -07001849 case Instruction::IF_NE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001850 const RegType& reg_type1 = work_line_->GetRegisterType(inst->VRegA_22t());
1851 const RegType& reg_type2 = work_line_->GetRegisterType(inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07001852 bool mismatch = false;
1853 if (reg_type1.IsZero()) { // zero then integral or reference expected
1854 mismatch = !reg_type2.IsReferenceTypes() && !reg_type2.IsIntegralTypes();
1855 } else if (reg_type1.IsReferenceTypes()) { // both references?
1856 mismatch = !reg_type2.IsReferenceTypes();
1857 } else { // both integral?
1858 mismatch = !reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes();
1859 }
1860 if (mismatch) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001861 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to if-eq/if-ne (" << reg_type1 << ","
1862 << reg_type2 << ") must both be references or integral";
jeffhaobdb76512011-09-07 11:43:16 -07001863 }
1864 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001865 }
jeffhaobdb76512011-09-07 11:43:16 -07001866 case Instruction::IF_LT:
1867 case Instruction::IF_GE:
1868 case Instruction::IF_GT:
Ian Rogersd81871c2011-10-03 13:57:23 -07001869 case Instruction::IF_LE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001870 const RegType& reg_type1 = work_line_->GetRegisterType(inst->VRegA_22t());
1871 const RegType& reg_type2 = work_line_->GetRegisterType(inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07001872 if (!reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001873 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to 'if' (" << reg_type1 << ","
1874 << reg_type2 << ") must be integral";
jeffhaobdb76512011-09-07 11:43:16 -07001875 }
1876 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001877 }
jeffhaobdb76512011-09-07 11:43:16 -07001878 case Instruction::IF_EQZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07001879 case Instruction::IF_NEZ: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001880 const RegType& reg_type = work_line_->GetRegisterType(inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07001881 if (!reg_type.IsReferenceTypes() && !reg_type.IsIntegralTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001882 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
1883 << " unexpected as arg to if-eqz/if-nez";
Ian Rogersd81871c2011-10-03 13:57:23 -07001884 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001885
1886 // Find previous instruction - its existence is a precondition to peephole optimization.
Ian Rogers9b360392013-06-06 14:45:07 -07001887 uint32_t instance_of_idx = 0;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001888 if (0 != work_insn_idx_) {
Ian Rogers9b360392013-06-06 14:45:07 -07001889 instance_of_idx = work_insn_idx_ - 1;
Brian Carlstromdf629502013-07-17 22:39:56 -07001890 while (0 != instance_of_idx && !insn_flags_[instance_of_idx].IsOpcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07001891 instance_of_idx--;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001892 }
Ian Rogers9b360392013-06-06 14:45:07 -07001893 CHECK(insn_flags_[instance_of_idx].IsOpcode());
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001894 } else {
1895 break;
1896 }
1897
Ian Rogers9b360392013-06-06 14:45:07 -07001898 const Instruction* instance_of_inst = Instruction::At(code_item_->insns_ + instance_of_idx);
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001899
1900 /* Check for peep-hole pattern of:
1901 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07001902 * instance-of vX, vY, T;
1903 * ifXXX vX, label ;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001904 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07001905 * label:
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001906 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07001907 * and sharpen the type of vY to be type T.
1908 * Note, this pattern can't be if:
1909 * - if there are other branches to this branch,
1910 * - when vX == vY.
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001911 */
Ian Rogersfae370a2013-06-05 08:33:27 -07001912 if (!CurrentInsnFlags()->IsBranchTarget() &&
Ian Rogers9b360392013-06-06 14:45:07 -07001913 (Instruction::INSTANCE_OF == instance_of_inst->Opcode()) &&
1914 (inst->VRegA_21t() == instance_of_inst->VRegA_22c()) &&
1915 (instance_of_inst->VRegA_22c() != instance_of_inst->VRegB_22c())) {
Ian Rogersfae370a2013-06-05 08:33:27 -07001916 // Check that the we are not attempting conversion to interface types,
1917 // which is not done because of the multiple inheritance implications.
Jeff Haoc642ec82013-09-04 16:11:55 -07001918 // Also don't change the type if it would result in an upcast.
1919 const RegType& orig_type = work_line_->GetRegisterType(instance_of_inst->VRegB_22c());
Ian Rogers9b360392013-06-06 14:45:07 -07001920 const RegType& cast_type = ResolveClassAndCheckAccess(instance_of_inst->VRegC_22c());
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001921
Jeff Haoa3faaf42013-09-03 19:07:00 -07001922 if (!cast_type.IsUnresolvedTypes() && !orig_type.IsUnresolvedTypes() &&
1923 !cast_type.GetClass()->IsInterface() && !cast_type.IsAssignableFrom(orig_type)) {
Ian Rogersd0fbd852013-09-24 18:17:04 -07001924 RegisterLine* update_line = RegisterLine::Create(code_item_->registers_size_, this);
Ian Rogersfae370a2013-06-05 08:33:27 -07001925 if (inst->Opcode() == Instruction::IF_EQZ) {
Ian Rogers9b360392013-06-06 14:45:07 -07001926 fallthrough_line.reset(update_line);
Ian Rogersfae370a2013-06-05 08:33:27 -07001927 } else {
Ian Rogers9b360392013-06-06 14:45:07 -07001928 branch_line.reset(update_line);
1929 }
1930 update_line->CopyFromLine(work_line_.get());
1931 update_line->SetRegisterType(instance_of_inst->VRegB_22c(), cast_type);
1932 if (!insn_flags_[instance_of_idx].IsBranchTarget() && 0 != instance_of_idx) {
1933 // See if instance-of was preceded by a move-object operation, common due to the small
1934 // register encoding space of instance-of, and propagate type information to the source
1935 // of the move-object.
1936 uint32_t move_idx = instance_of_idx - 1;
Brian Carlstromdf629502013-07-17 22:39:56 -07001937 while (0 != move_idx && !insn_flags_[move_idx].IsOpcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07001938 move_idx--;
1939 }
1940 CHECK(insn_flags_[move_idx].IsOpcode());
1941 const Instruction* move_inst = Instruction::At(code_item_->insns_ + move_idx);
1942 switch (move_inst->Opcode()) {
1943 case Instruction::MOVE_OBJECT:
1944 if (move_inst->VRegA_12x() == instance_of_inst->VRegB_22c()) {
1945 update_line->SetRegisterType(move_inst->VRegB_12x(), cast_type);
1946 }
1947 break;
1948 case Instruction::MOVE_OBJECT_FROM16:
1949 if (move_inst->VRegA_22x() == instance_of_inst->VRegB_22c()) {
1950 update_line->SetRegisterType(move_inst->VRegB_22x(), cast_type);
1951 }
1952 break;
1953 case Instruction::MOVE_OBJECT_16:
1954 if (move_inst->VRegA_32x() == instance_of_inst->VRegB_22c()) {
1955 update_line->SetRegisterType(move_inst->VRegB_32x(), cast_type);
1956 }
1957 break;
1958 default:
1959 break;
1960 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001961 }
1962 }
1963 }
1964
jeffhaobdb76512011-09-07 11:43:16 -07001965 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001966 }
jeffhaobdb76512011-09-07 11:43:16 -07001967 case Instruction::IF_LTZ:
1968 case Instruction::IF_GEZ:
1969 case Instruction::IF_GTZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07001970 case Instruction::IF_LEZ: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001971 const RegType& reg_type = work_line_->GetRegisterType(inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07001972 if (!reg_type.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001973 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
1974 << " unexpected as arg to if-ltz/if-gez/if-gtz/if-lez";
Ian Rogersd81871c2011-10-03 13:57:23 -07001975 }
jeffhaobdb76512011-09-07 11:43:16 -07001976 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001977 }
jeffhaobdb76512011-09-07 11:43:16 -07001978 case Instruction::AGET_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001979 VerifyAGet(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001980 break;
jeffhaobdb76512011-09-07 11:43:16 -07001981 case Instruction::AGET_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001982 VerifyAGet(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001983 break;
jeffhaobdb76512011-09-07 11:43:16 -07001984 case Instruction::AGET_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001985 VerifyAGet(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001986 break;
jeffhaobdb76512011-09-07 11:43:16 -07001987 case Instruction::AGET_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001988 VerifyAGet(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001989 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001990 case Instruction::AGET:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001991 VerifyAGet(inst, reg_types_.Integer(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001992 break;
jeffhaobdb76512011-09-07 11:43:16 -07001993 case Instruction::AGET_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001994 VerifyAGet(inst, reg_types_.LongLo(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001995 break;
1996 case Instruction::AGET_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001997 VerifyAGet(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07001998 break;
1999
Ian Rogersd81871c2011-10-03 13:57:23 -07002000 case Instruction::APUT_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002001 VerifyAPut(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002002 break;
2003 case Instruction::APUT_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002004 VerifyAPut(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002005 break;
2006 case Instruction::APUT_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002007 VerifyAPut(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002008 break;
2009 case Instruction::APUT_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002010 VerifyAPut(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002011 break;
2012 case Instruction::APUT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002013 VerifyAPut(inst, reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002014 break;
2015 case Instruction::APUT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002016 VerifyAPut(inst, reg_types_.LongLo(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002017 break;
2018 case Instruction::APUT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002019 VerifyAPut(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07002020 break;
2021
jeffhaobdb76512011-09-07 11:43:16 -07002022 case Instruction::IGET_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002023 VerifyISGet(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002024 break;
jeffhaobdb76512011-09-07 11:43:16 -07002025 case Instruction::IGET_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002026 VerifyISGet(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002027 break;
jeffhaobdb76512011-09-07 11:43:16 -07002028 case Instruction::IGET_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002029 VerifyISGet(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002030 break;
jeffhaobdb76512011-09-07 11:43:16 -07002031 case Instruction::IGET_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002032 VerifyISGet(inst, reg_types_.Short(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002033 break;
2034 case Instruction::IGET:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002035 VerifyISGet(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002036 break;
2037 case Instruction::IGET_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002038 VerifyISGet(inst, reg_types_.LongLo(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002039 break;
2040 case Instruction::IGET_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002041 VerifyISGet(inst, reg_types_.JavaLangObject(false), false, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002042 break;
jeffhaobdb76512011-09-07 11:43:16 -07002043
Ian Rogersd81871c2011-10-03 13:57:23 -07002044 case Instruction::IPUT_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002045 VerifyISPut(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002046 break;
2047 case Instruction::IPUT_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002048 VerifyISPut(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002049 break;
2050 case Instruction::IPUT_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002051 VerifyISPut(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002052 break;
2053 case Instruction::IPUT_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002054 VerifyISPut(inst, reg_types_.Short(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002055 break;
2056 case Instruction::IPUT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002057 VerifyISPut(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002058 break;
2059 case Instruction::IPUT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002060 VerifyISPut(inst, reg_types_.LongLo(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002061 break;
jeffhaobdb76512011-09-07 11:43:16 -07002062 case Instruction::IPUT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002063 VerifyISPut(inst, reg_types_.JavaLangObject(false), false, false);
jeffhaobdb76512011-09-07 11:43:16 -07002064 break;
2065
jeffhaobdb76512011-09-07 11:43:16 -07002066 case Instruction::SGET_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002067 VerifyISGet(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002068 break;
jeffhaobdb76512011-09-07 11:43:16 -07002069 case Instruction::SGET_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002070 VerifyISGet(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002071 break;
jeffhaobdb76512011-09-07 11:43:16 -07002072 case Instruction::SGET_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002073 VerifyISGet(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002074 break;
jeffhaobdb76512011-09-07 11:43:16 -07002075 case Instruction::SGET_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002076 VerifyISGet(inst, reg_types_.Short(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002077 break;
2078 case Instruction::SGET:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002079 VerifyISGet(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002080 break;
2081 case Instruction::SGET_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002082 VerifyISGet(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002083 break;
2084 case Instruction::SGET_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002085 VerifyISGet(inst, reg_types_.JavaLangObject(false), false, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002086 break;
2087
2088 case Instruction::SPUT_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002089 VerifyISPut(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002090 break;
2091 case Instruction::SPUT_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002092 VerifyISPut(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002093 break;
2094 case Instruction::SPUT_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002095 VerifyISPut(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002096 break;
2097 case Instruction::SPUT_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002098 VerifyISPut(inst, reg_types_.Short(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002099 break;
2100 case Instruction::SPUT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002101 VerifyISPut(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002102 break;
2103 case Instruction::SPUT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002104 VerifyISPut(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002105 break;
2106 case Instruction::SPUT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002107 VerifyISPut(inst, reg_types_.JavaLangObject(false), false, true);
jeffhaobdb76512011-09-07 11:43:16 -07002108 break;
2109
2110 case Instruction::INVOKE_VIRTUAL:
2111 case Instruction::INVOKE_VIRTUAL_RANGE:
2112 case Instruction::INVOKE_SUPER:
Ian Rogersd81871c2011-10-03 13:57:23 -07002113 case Instruction::INVOKE_SUPER_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002114 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE ||
2115 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
2116 bool is_super = (inst->Opcode() == Instruction::INVOKE_SUPER ||
2117 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
Brian Carlstromea46f952013-07-30 01:26:50 -07002118 mirror::ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_VIRTUAL,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002119 is_range, is_super);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002120 const RegType* return_type = nullptr;
2121 if (called_method != nullptr) {
2122 MethodHelper mh(called_method);
2123 mirror::Class* return_type_class = mh.GetReturnType();
2124 if (return_type_class != nullptr) {
2125 return_type = &reg_types_.FromClass(mh.GetReturnTypeDescriptor(), return_type_class,
2126 return_type_class->CannotBeAssignedFromOtherTypes());
2127 } else {
2128 Thread* self = Thread::Current();
2129 DCHECK(self->IsExceptionPending());
2130 self->ClearException();
2131 }
2132 }
2133 if (return_type == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002134 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002135 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2136 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002137 const char* descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002138 return_type = &reg_types_.FromDescriptor(class_loader_->get(), descriptor, false);
jeffhaobdb76512011-09-07 11:43:16 -07002139 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002140 if (!return_type->IsLowHalf()) {
2141 work_line_->SetResultRegisterType(*return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002142 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002143 work_line_->SetResultRegisterTypeWide(*return_type, return_type->HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002144 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002145 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002146 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002147 }
jeffhaobdb76512011-09-07 11:43:16 -07002148 case Instruction::INVOKE_DIRECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002149 case Instruction::INVOKE_DIRECT_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002150 bool is_range = (inst->Opcode() == Instruction::INVOKE_DIRECT_RANGE);
Brian Carlstromea46f952013-07-30 01:26:50 -07002151 mirror::ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_DIRECT,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002152 is_range, false);
Ian Rogers46685432012-06-03 22:26:43 -07002153 const char* return_type_descriptor;
2154 bool is_constructor;
2155 if (called_method == NULL) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002156 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogers46685432012-06-03 22:26:43 -07002157 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
Ian Rogersdfb325e2013-10-30 01:00:44 -07002158 is_constructor = strcmp("<init>", dex_file_->StringDataByIdx(method_id.name_idx_)) == 0;
Ian Rogers46685432012-06-03 22:26:43 -07002159 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
2160 return_type_descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2161 } else {
2162 is_constructor = called_method->IsConstructor();
2163 return_type_descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
2164 }
2165 if (is_constructor) {
jeffhaobdb76512011-09-07 11:43:16 -07002166 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002167 * Some additional checks when calling a constructor. We know from the invocation arg check
2168 * that the "this" argument is an instance of called_method->klass. Now we further restrict
2169 * that to require that called_method->klass is the same as this->klass or this->super,
2170 * allowing the latter only if the "this" argument is the same as the "this" argument to
2171 * this method (which implies that we're in a constructor ourselves).
jeffhaobdb76512011-09-07 11:43:16 -07002172 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002173 const RegType& this_type = work_line_->GetInvocationThis(inst, is_range);
jeffhaob57e9522012-04-26 18:08:21 -07002174 if (this_type.IsConflict()) // failure.
2175 break;
jeffhaobdb76512011-09-07 11:43:16 -07002176
jeffhaob57e9522012-04-26 18:08:21 -07002177 /* no null refs allowed (?) */
2178 if (this_type.IsZero()) {
2179 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unable to initialize null ref";
2180 break;
jeffhao2a8a90e2011-09-26 14:25:31 -07002181 }
jeffhaob57e9522012-04-26 18:08:21 -07002182
2183 /* must be in same class or in superclass */
Ian Rogers46685432012-06-03 22:26:43 -07002184 // const RegType& this_super_klass = this_type.GetSuperClass(&reg_types_);
2185 // TODO: re-enable constructor type verification
2186 // if (this_super_klass.IsConflict()) {
jeffhaob57e9522012-04-26 18:08:21 -07002187 // Unknown super class, fail so we re-check at runtime.
Ian Rogers46685432012-06-03 22:26:43 -07002188 // Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "super class unknown for '" << this_type << "'";
2189 // break;
2190 // }
jeffhaob57e9522012-04-26 18:08:21 -07002191
2192 /* arg must be an uninitialized reference */
2193 if (!this_type.IsUninitializedTypes()) {
2194 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Expected initialization on uninitialized reference "
2195 << this_type;
2196 break;
2197 }
2198
2199 /*
2200 * Replace the uninitialized reference with an initialized one. We need to do this for all
2201 * registers that have the same object instance in them, not just the "this" register.
2202 */
2203 work_line_->MarkRefsAsInitialized(this_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002204 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07002205 const RegType& return_type = reg_types_.FromDescriptor(class_loader_->get(),
2206 return_type_descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002207 if (!return_type.IsLowHalf()) {
2208 work_line_->SetResultRegisterType(return_type);
2209 } else {
2210 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2211 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002212 just_set_result = true;
2213 break;
2214 }
2215 case Instruction::INVOKE_STATIC:
2216 case Instruction::INVOKE_STATIC_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002217 bool is_range = (inst->Opcode() == Instruction::INVOKE_STATIC_RANGE);
Brian Carlstromea46f952013-07-30 01:26:50 -07002218 mirror::ArtMethod* called_method = VerifyInvocationArgs(inst,
Brian Carlstrom93c33962013-07-26 10:37:43 -07002219 METHOD_STATIC,
2220 is_range,
2221 false);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002222 const char* descriptor;
2223 if (called_method == NULL) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002224 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002225 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2226 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Mathieu Chartier590fee92013-09-13 13:46:47 -07002227 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002228 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002229 descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002230 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07002231 const RegType& return_type = reg_types_.FromDescriptor(class_loader_->get(), descriptor,
2232 false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002233 if (!return_type.IsLowHalf()) {
2234 work_line_->SetResultRegisterType(return_type);
2235 } else {
2236 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2237 }
jeffhaobdb76512011-09-07 11:43:16 -07002238 just_set_result = true;
2239 }
2240 break;
jeffhaobdb76512011-09-07 11:43:16 -07002241 case Instruction::INVOKE_INTERFACE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002242 case Instruction::INVOKE_INTERFACE_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002243 bool is_range = (inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE);
Brian Carlstromea46f952013-07-30 01:26:50 -07002244 mirror::ArtMethod* abs_method = VerifyInvocationArgs(inst,
Brian Carlstrom93c33962013-07-26 10:37:43 -07002245 METHOD_INTERFACE,
2246 is_range,
2247 false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002248 if (abs_method != NULL) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002249 mirror::Class* called_interface = abs_method->GetDeclaringClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002250 if (!called_interface->IsInterface() && !called_interface->IsObjectClass()) {
2251 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected interface class in invoke-interface '"
2252 << PrettyMethod(abs_method) << "'";
2253 break;
Ian Rogers28ad40d2011-10-27 15:19:26 -07002254 }
Ian Rogers0d604842012-04-16 14:50:24 -07002255 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002256 /* Get the type of the "this" arg, which should either be a sub-interface of called
2257 * interface or Object (see comments in RegType::JoinClass).
2258 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002259 const RegType& this_type = work_line_->GetInvocationThis(inst, is_range);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002260 if (this_type.IsZero()) {
2261 /* null pointer always passes (and always fails at runtime) */
2262 } else {
2263 if (this_type.IsUninitializedTypes()) {
2264 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface call on uninitialized object "
2265 << this_type;
2266 break;
2267 }
2268 // In the past we have tried to assert that "called_interface" is assignable
2269 // from "this_type.GetClass()", however, as we do an imprecise Join
2270 // (RegType::JoinClass) we don't have full information on what interfaces are
2271 // implemented by "this_type". For example, two classes may implement the same
2272 // interfaces and have a common parent that doesn't implement the interface. The
2273 // join will set "this_type" to the parent class and a test that this implements
2274 // the interface will incorrectly fail.
2275 }
2276 /*
2277 * We don't have an object instance, so we can't find the concrete method. However, all of
2278 * the type information is in the abstract method, so we're good.
2279 */
2280 const char* descriptor;
2281 if (abs_method == NULL) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002282 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002283 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2284 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
2285 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2286 } else {
2287 descriptor = MethodHelper(abs_method).GetReturnTypeDescriptor();
2288 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07002289 const RegType& return_type = reg_types_.FromDescriptor(class_loader_->get(), descriptor,
2290 false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002291 if (!return_type.IsLowHalf()) {
2292 work_line_->SetResultRegisterType(return_type);
2293 } else {
2294 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2295 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002296 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002297 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002298 }
jeffhaobdb76512011-09-07 11:43:16 -07002299 case Instruction::NEG_INT:
2300 case Instruction::NOT_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002301 work_line_->CheckUnaryOp(inst, reg_types_.Integer(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002302 break;
2303 case Instruction::NEG_LONG:
2304 case Instruction::NOT_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002305 work_line_->CheckUnaryOpWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002306 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002307 break;
2308 case Instruction::NEG_FLOAT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002309 work_line_->CheckUnaryOp(inst, reg_types_.Float(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002310 break;
2311 case Instruction::NEG_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002312 work_line_->CheckUnaryOpWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002313 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002314 break;
2315 case Instruction::INT_TO_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002316 work_line_->CheckUnaryOpToWide(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::INT_TO_FLOAT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002320 work_line_->CheckUnaryOp(inst, reg_types_.Float(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002321 break;
2322 case Instruction::INT_TO_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002323 work_line_->CheckUnaryOpToWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002324 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002325 break;
2326 case Instruction::LONG_TO_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002327 work_line_->CheckUnaryOpFromWide(inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002328 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002329 break;
2330 case Instruction::LONG_TO_FLOAT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002331 work_line_->CheckUnaryOpFromWide(inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002332 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002333 break;
2334 case Instruction::LONG_TO_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002335 work_line_->CheckUnaryOpWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002336 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002337 break;
2338 case Instruction::FLOAT_TO_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002339 work_line_->CheckUnaryOp(inst, reg_types_.Integer(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002340 break;
2341 case Instruction::FLOAT_TO_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002342 work_line_->CheckUnaryOpToWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002343 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002344 break;
2345 case Instruction::FLOAT_TO_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002346 work_line_->CheckUnaryOpToWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002347 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002348 break;
2349 case Instruction::DOUBLE_TO_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002350 work_line_->CheckUnaryOpFromWide(inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002351 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002352 break;
2353 case Instruction::DOUBLE_TO_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002354 work_line_->CheckUnaryOpWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002355 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002356 break;
2357 case Instruction::DOUBLE_TO_FLOAT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002358 work_line_->CheckUnaryOpFromWide(inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002359 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002360 break;
2361 case Instruction::INT_TO_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002362 work_line_->CheckUnaryOp(inst, reg_types_.Byte(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002363 break;
2364 case Instruction::INT_TO_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002365 work_line_->CheckUnaryOp(inst, reg_types_.Char(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002366 break;
2367 case Instruction::INT_TO_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002368 work_line_->CheckUnaryOp(inst, reg_types_.Short(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002369 break;
2370
2371 case Instruction::ADD_INT:
2372 case Instruction::SUB_INT:
2373 case Instruction::MUL_INT:
2374 case Instruction::REM_INT:
2375 case Instruction::DIV_INT:
2376 case Instruction::SHL_INT:
2377 case Instruction::SHR_INT:
2378 case Instruction::USHR_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002379 work_line_->CheckBinaryOp(inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002380 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002381 break;
2382 case Instruction::AND_INT:
2383 case Instruction::OR_INT:
2384 case Instruction::XOR_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002385 work_line_->CheckBinaryOp(inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002386 reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002387 break;
2388 case Instruction::ADD_LONG:
2389 case Instruction::SUB_LONG:
2390 case Instruction::MUL_LONG:
2391 case Instruction::DIV_LONG:
2392 case Instruction::REM_LONG:
2393 case Instruction::AND_LONG:
2394 case Instruction::OR_LONG:
2395 case Instruction::XOR_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002396 work_line_->CheckBinaryOpWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002397 reg_types_.LongLo(), reg_types_.LongHi(),
2398 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002399 break;
2400 case Instruction::SHL_LONG:
2401 case Instruction::SHR_LONG:
2402 case Instruction::USHR_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002403 /* shift distance is Int, making these different from other binary operations */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002404 work_line_->CheckBinaryOpWideShift(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002405 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002406 break;
2407 case Instruction::ADD_FLOAT:
2408 case Instruction::SUB_FLOAT:
2409 case Instruction::MUL_FLOAT:
2410 case Instruction::DIV_FLOAT:
2411 case Instruction::REM_FLOAT:
Brian Carlstrom93c33962013-07-26 10:37:43 -07002412 work_line_->CheckBinaryOp(inst,
2413 reg_types_.Float(),
2414 reg_types_.Float(),
2415 reg_types_.Float(),
2416 false);
jeffhaobdb76512011-09-07 11:43:16 -07002417 break;
2418 case Instruction::ADD_DOUBLE:
2419 case Instruction::SUB_DOUBLE:
2420 case Instruction::MUL_DOUBLE:
2421 case Instruction::DIV_DOUBLE:
2422 case Instruction::REM_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002423 work_line_->CheckBinaryOpWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002424 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2425 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002426 break;
2427 case Instruction::ADD_INT_2ADDR:
2428 case Instruction::SUB_INT_2ADDR:
2429 case Instruction::MUL_INT_2ADDR:
2430 case Instruction::REM_INT_2ADDR:
2431 case Instruction::SHL_INT_2ADDR:
2432 case Instruction::SHR_INT_2ADDR:
2433 case Instruction::USHR_INT_2ADDR:
Brian Carlstrom93c33962013-07-26 10:37:43 -07002434 work_line_->CheckBinaryOp2addr(inst,
2435 reg_types_.Integer(),
2436 reg_types_.Integer(),
2437 reg_types_.Integer(),
2438 false);
jeffhaobdb76512011-09-07 11:43:16 -07002439 break;
2440 case Instruction::AND_INT_2ADDR:
2441 case Instruction::OR_INT_2ADDR:
2442 case Instruction::XOR_INT_2ADDR:
Brian Carlstrom93c33962013-07-26 10:37:43 -07002443 work_line_->CheckBinaryOp2addr(inst,
2444 reg_types_.Integer(),
2445 reg_types_.Integer(),
2446 reg_types_.Integer(),
2447 true);
jeffhaobdb76512011-09-07 11:43:16 -07002448 break;
2449 case Instruction::DIV_INT_2ADDR:
Brian Carlstrom93c33962013-07-26 10:37:43 -07002450 work_line_->CheckBinaryOp2addr(inst,
2451 reg_types_.Integer(),
2452 reg_types_.Integer(),
2453 reg_types_.Integer(),
2454 false);
jeffhaobdb76512011-09-07 11:43:16 -07002455 break;
2456 case Instruction::ADD_LONG_2ADDR:
2457 case Instruction::SUB_LONG_2ADDR:
2458 case Instruction::MUL_LONG_2ADDR:
2459 case Instruction::DIV_LONG_2ADDR:
2460 case Instruction::REM_LONG_2ADDR:
2461 case Instruction::AND_LONG_2ADDR:
2462 case Instruction::OR_LONG_2ADDR:
2463 case Instruction::XOR_LONG_2ADDR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002464 work_line_->CheckBinaryOp2addrWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002465 reg_types_.LongLo(), reg_types_.LongHi(),
2466 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002467 break;
2468 case Instruction::SHL_LONG_2ADDR:
2469 case Instruction::SHR_LONG_2ADDR:
2470 case Instruction::USHR_LONG_2ADDR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002471 work_line_->CheckBinaryOp2addrWideShift(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002472 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002473 break;
2474 case Instruction::ADD_FLOAT_2ADDR:
2475 case Instruction::SUB_FLOAT_2ADDR:
2476 case Instruction::MUL_FLOAT_2ADDR:
2477 case Instruction::DIV_FLOAT_2ADDR:
2478 case Instruction::REM_FLOAT_2ADDR:
Brian Carlstrom93c33962013-07-26 10:37:43 -07002479 work_line_->CheckBinaryOp2addr(inst,
2480 reg_types_.Float(),
2481 reg_types_.Float(),
2482 reg_types_.Float(),
2483 false);
jeffhaobdb76512011-09-07 11:43:16 -07002484 break;
2485 case Instruction::ADD_DOUBLE_2ADDR:
2486 case Instruction::SUB_DOUBLE_2ADDR:
2487 case Instruction::MUL_DOUBLE_2ADDR:
2488 case Instruction::DIV_DOUBLE_2ADDR:
2489 case Instruction::REM_DOUBLE_2ADDR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002490 work_line_->CheckBinaryOp2addrWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002491 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2492 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002493 break;
2494 case Instruction::ADD_INT_LIT16:
2495 case Instruction::RSUB_INT:
2496 case Instruction::MUL_INT_LIT16:
2497 case Instruction::DIV_INT_LIT16:
2498 case Instruction::REM_INT_LIT16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002499 work_line_->CheckLiteralOp(inst, reg_types_.Integer(), reg_types_.Integer(), false, true);
jeffhaobdb76512011-09-07 11:43:16 -07002500 break;
2501 case Instruction::AND_INT_LIT16:
2502 case Instruction::OR_INT_LIT16:
2503 case Instruction::XOR_INT_LIT16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002504 work_line_->CheckLiteralOp(inst, reg_types_.Integer(), reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002505 break;
2506 case Instruction::ADD_INT_LIT8:
2507 case Instruction::RSUB_INT_LIT8:
2508 case Instruction::MUL_INT_LIT8:
2509 case Instruction::DIV_INT_LIT8:
2510 case Instruction::REM_INT_LIT8:
2511 case Instruction::SHL_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002512 case Instruction::SHR_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002513 case Instruction::USHR_INT_LIT8:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002514 work_line_->CheckLiteralOp(inst, reg_types_.Integer(), reg_types_.Integer(), false, false);
jeffhaobdb76512011-09-07 11:43:16 -07002515 break;
2516 case Instruction::AND_INT_LIT8:
2517 case Instruction::OR_INT_LIT8:
2518 case Instruction::XOR_INT_LIT8:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002519 work_line_->CheckLiteralOp(inst, reg_types_.Integer(), reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002520 break;
2521
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002522 // Special instructions.
Sebastien Hertzcc10e0e2013-06-28 14:24:48 +02002523 case Instruction::RETURN_VOID_BARRIER:
Ian Rogers9fc16eb2013-07-31 14:49:16 -07002524 DCHECK(Runtime::Current()->IsStarted()) << PrettyMethod(dex_method_idx_, *dex_file_);
2525 if (!IsConstructor() || IsStatic()) {
Sebastien Hertzcc10e0e2013-06-28 14:24:48 +02002526 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void-barrier not expected";
2527 }
2528 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002529 // Note: the following instructions encode offsets derived from class linking.
2530 // As such they use Class*/Field*/AbstractMethod* as these offsets only have
2531 // meaning if the class linking and resolution were successful.
2532 case Instruction::IGET_QUICK:
2533 VerifyIGetQuick(inst, reg_types_.Integer(), true);
2534 break;
2535 case Instruction::IGET_WIDE_QUICK:
2536 VerifyIGetQuick(inst, reg_types_.LongLo(), true);
2537 break;
2538 case Instruction::IGET_OBJECT_QUICK:
2539 VerifyIGetQuick(inst, reg_types_.JavaLangObject(false), false);
2540 break;
2541 case Instruction::IPUT_QUICK:
2542 VerifyIPutQuick(inst, reg_types_.Integer(), true);
2543 break;
2544 case Instruction::IPUT_WIDE_QUICK:
2545 VerifyIPutQuick(inst, reg_types_.LongLo(), true);
2546 break;
2547 case Instruction::IPUT_OBJECT_QUICK:
2548 VerifyIPutQuick(inst, reg_types_.JavaLangObject(false), false);
2549 break;
2550 case Instruction::INVOKE_VIRTUAL_QUICK:
2551 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: {
2552 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
Brian Carlstromea46f952013-07-30 01:26:50 -07002553 mirror::ArtMethod* called_method = VerifyInvokeVirtualQuickArgs(inst, is_range);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002554 if (called_method != NULL) {
2555 const char* descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
Mathieu Chartier590fee92013-09-13 13:46:47 -07002556 const RegType& return_type = reg_types_.FromDescriptor(class_loader_->get(), descriptor,
2557 false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002558 if (!return_type.IsLowHalf()) {
2559 work_line_->SetResultRegisterType(return_type);
2560 } else {
2561 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2562 }
2563 just_set_result = true;
2564 }
2565 break;
2566 }
2567
Ian Rogersd81871c2011-10-03 13:57:23 -07002568 /* These should never appear during verification. */
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002569 case Instruction::UNUSED_3E:
2570 case Instruction::UNUSED_3F:
2571 case Instruction::UNUSED_40:
2572 case Instruction::UNUSED_41:
2573 case Instruction::UNUSED_42:
2574 case Instruction::UNUSED_43:
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002575 case Instruction::UNUSED_79:
2576 case Instruction::UNUSED_7A:
2577 case Instruction::UNUSED_EB:
2578 case Instruction::UNUSED_EC:
jeffhao9a4f0032012-08-30 16:17:40 -07002579 case Instruction::UNUSED_ED:
jeffhaobdb76512011-09-07 11:43:16 -07002580 case Instruction::UNUSED_EE:
2581 case Instruction::UNUSED_EF:
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002582 case Instruction::UNUSED_F0:
2583 case Instruction::UNUSED_F1:
jeffhaobdb76512011-09-07 11:43:16 -07002584 case Instruction::UNUSED_F2:
2585 case Instruction::UNUSED_F3:
2586 case Instruction::UNUSED_F4:
2587 case Instruction::UNUSED_F5:
2588 case Instruction::UNUSED_F6:
2589 case Instruction::UNUSED_F7:
2590 case Instruction::UNUSED_F8:
2591 case Instruction::UNUSED_F9:
2592 case Instruction::UNUSED_FA:
2593 case Instruction::UNUSED_FB:
jeffhaobdb76512011-09-07 11:43:16 -07002594 case Instruction::UNUSED_FC:
jeffhaobdb76512011-09-07 11:43:16 -07002595 case Instruction::UNUSED_FD:
jeffhaobdb76512011-09-07 11:43:16 -07002596 case Instruction::UNUSED_FE:
jeffhaobdb76512011-09-07 11:43:16 -07002597 case Instruction::UNUSED_FF:
jeffhaod5347e02012-03-22 17:25:05 -07002598 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Unexpected opcode " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07002599 break;
2600
2601 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002602 * DO NOT add a "default" clause here. Without it the compiler will
jeffhaobdb76512011-09-07 11:43:16 -07002603 * complain if an instruction is missing (which is desirable).
2604 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002605 } // end - switch (dec_insn.opcode)
jeffhaobdb76512011-09-07 11:43:16 -07002606
Ian Rogersad0b3a32012-04-16 14:50:24 -07002607 if (have_pending_hard_failure_) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002608 if (Runtime::Current()->IsCompiler()) {
jeffhaob57e9522012-04-26 18:08:21 -07002609 /* When compiling, check that the last failure is a hard failure */
Ian Rogersad0b3a32012-04-16 14:50:24 -07002610 CHECK_EQ(failures_[failures_.size() - 1], VERIFY_ERROR_BAD_CLASS_HARD);
Ian Rogerse1758fe2012-04-19 11:31:15 -07002611 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002612 /* immediate failure, reject class */
2613 info_messages_ << "Rejecting opcode " << inst->DumpString(dex_file_);
2614 return false;
jeffhaofaf459e2012-08-31 15:32:47 -07002615 } else if (have_pending_runtime_throw_failure_) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07002616 /* checking interpreter will throw, mark following code as unreachable */
jeffhaofaf459e2012-08-31 15:32:47 -07002617 opcode_flags = Instruction::kThrow;
jeffhaobdb76512011-09-07 11:43:16 -07002618 }
jeffhaobdb76512011-09-07 11:43:16 -07002619 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002620 * If we didn't just set the result register, clear it out. This ensures that you can only use
2621 * "move-result" immediately after the result is set. (We could check this statically, but it's
2622 * not expensive and it makes our debugging output cleaner.)
jeffhaobdb76512011-09-07 11:43:16 -07002623 */
2624 if (!just_set_result) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002625 work_line_->SetResultTypeToUnknown();
jeffhaobdb76512011-09-07 11:43:16 -07002626 }
2627
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002628
jeffhaobdb76512011-09-07 11:43:16 -07002629
2630 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002631 * Handle "branch". Tag the branch target.
jeffhaobdb76512011-09-07 11:43:16 -07002632 *
2633 * NOTE: instructions like Instruction::EQZ provide information about the
jeffhaod1f0fde2011-09-08 17:25:33 -07002634 * state of the register when the branch is taken or not taken. For example,
jeffhaobdb76512011-09-07 11:43:16 -07002635 * somebody could get a reference field, check it for zero, and if the
2636 * branch is taken immediately store that register in a boolean field
jeffhaod1f0fde2011-09-08 17:25:33 -07002637 * since the value is known to be zero. We do not currently account for
jeffhaobdb76512011-09-07 11:43:16 -07002638 * that, and will reject the code.
2639 *
2640 * TODO: avoid re-fetching the branch target
2641 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002642 if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002643 bool isConditional, selfOkay;
Ian Rogersd81871c2011-10-03 13:57:23 -07002644 if (!GetBranchOffset(work_insn_idx_, &branch_target, &isConditional, &selfOkay)) {
jeffhaobdb76512011-09-07 11:43:16 -07002645 /* should never happen after static verification */
jeffhaod5347e02012-03-22 17:25:05 -07002646 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad branch";
jeffhaobdb76512011-09-07 11:43:16 -07002647 return false;
2648 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08002649 DCHECK_EQ(isConditional, (opcode_flags & Instruction::kContinue) != 0);
jeffhaod5347e02012-03-22 17:25:05 -07002650 if (!CheckNotMoveException(code_item_->insns_, work_insn_idx_ + branch_target)) {
jeffhaobdb76512011-09-07 11:43:16 -07002651 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002652 }
jeffhaobdb76512011-09-07 11:43:16 -07002653 /* update branch target, set "changed" if appropriate */
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002654 if (NULL != branch_line.get()) {
2655 if (!UpdateRegisters(work_insn_idx_ + branch_target, branch_line.get())) {
2656 return false;
2657 }
2658 } else {
2659 if (!UpdateRegisters(work_insn_idx_ + branch_target, work_line_.get())) {
2660 return false;
2661 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002662 }
jeffhaobdb76512011-09-07 11:43:16 -07002663 }
2664
2665 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002666 * Handle "switch". Tag all possible branch targets.
jeffhaobdb76512011-09-07 11:43:16 -07002667 *
2668 * We've already verified that the table is structurally sound, so we
2669 * just need to walk through and tag the targets.
2670 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002671 if ((opcode_flags & Instruction::kSwitch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002672 int offset_to_switch = insns[1] | (((int32_t) insns[2]) << 16);
2673 const uint16_t* switch_insns = insns + offset_to_switch;
2674 int switch_count = switch_insns[1];
2675 int offset_to_targets, targ;
2676
2677 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
2678 /* 0 = sig, 1 = count, 2/3 = first key */
2679 offset_to_targets = 4;
2680 } else {
2681 /* 0 = sig, 1 = count, 2..count * 2 = keys */
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07002682 DCHECK((*insns & 0xff) == Instruction::SPARSE_SWITCH);
jeffhaobdb76512011-09-07 11:43:16 -07002683 offset_to_targets = 2 + 2 * switch_count;
2684 }
2685
2686 /* verify each switch target */
2687 for (targ = 0; targ < switch_count; targ++) {
2688 int offset;
2689 uint32_t abs_offset;
2690
2691 /* offsets are 32-bit, and only partly endian-swapped */
2692 offset = switch_insns[offset_to_targets + targ * 2] |
2693 (((int32_t) switch_insns[offset_to_targets + targ * 2 + 1]) << 16);
Ian Rogersd81871c2011-10-03 13:57:23 -07002694 abs_offset = work_insn_idx_ + offset;
2695 DCHECK_LT(abs_offset, code_item_->insns_size_in_code_units_);
jeffhaod5347e02012-03-22 17:25:05 -07002696 if (!CheckNotMoveException(code_item_->insns_, abs_offset)) {
jeffhaobdb76512011-09-07 11:43:16 -07002697 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002698 }
2699 if (!UpdateRegisters(abs_offset, work_line_.get()))
jeffhaobdb76512011-09-07 11:43:16 -07002700 return false;
2701 }
2702 }
2703
2704 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002705 * Handle instructions that can throw and that are sitting in a "try" block. (If they're not in a
2706 * "try" block when they throw, control transfers out of the method.)
jeffhaobdb76512011-09-07 11:43:16 -07002707 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002708 if ((opcode_flags & Instruction::kThrow) != 0 && insn_flags_[work_insn_idx_].IsInTry()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002709 bool within_catch_all = false;
Ian Rogers0571d352011-11-03 19:51:38 -07002710 CatchHandlerIterator iterator(*code_item_, work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07002711
Ian Rogers0571d352011-11-03 19:51:38 -07002712 for (; iterator.HasNext(); iterator.Next()) {
2713 if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002714 within_catch_all = true;
2715 }
jeffhaobdb76512011-09-07 11:43:16 -07002716 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002717 * Merge registers into the "catch" block. We want to use the "savedRegs" rather than
2718 * "work_regs", because at runtime the exception will be thrown before the instruction
2719 * modifies any registers.
jeffhaobdb76512011-09-07 11:43:16 -07002720 */
Ian Rogers0571d352011-11-03 19:51:38 -07002721 if (!UpdateRegisters(iterator.GetHandlerAddress(), saved_line_.get())) {
jeffhaobdb76512011-09-07 11:43:16 -07002722 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002723 }
jeffhaobdb76512011-09-07 11:43:16 -07002724 }
2725
2726 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002727 * If the monitor stack depth is nonzero, there must be a "catch all" handler for this
2728 * instruction. This does apply to monitor-exit because of async exception handling.
jeffhaobdb76512011-09-07 11:43:16 -07002729 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002730 if (work_line_->MonitorStackDepth() > 0 && !within_catch_all) {
jeffhaobdb76512011-09-07 11:43:16 -07002731 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002732 * The state in work_line reflects the post-execution state. If the current instruction is a
2733 * monitor-enter and the monitor stack was empty, we don't need a catch-all (if it throws,
jeffhaobdb76512011-09-07 11:43:16 -07002734 * it will do so before grabbing the lock).
2735 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002736 if (inst->Opcode() != Instruction::MONITOR_ENTER || work_line_->MonitorStackDepth() != 1) {
jeffhaod5347e02012-03-22 17:25:05 -07002737 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
Ian Rogersd81871c2011-10-03 13:57:23 -07002738 << "expected to be within a catch-all for an instruction where a monitor is held";
jeffhaobdb76512011-09-07 11:43:16 -07002739 return false;
2740 }
2741 }
2742 }
2743
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002744 /* Handle "continue". Tag the next consecutive instruction.
2745 * Note: Keep the code handling "continue" case below the "branch" and "switch" cases,
2746 * because it changes work_line_ when performing peephole optimization
2747 * and this change should not be used in those cases.
2748 */
Ian Rogers6d376ae2013-07-23 15:12:40 -07002749 if ((opcode_flags & Instruction::kContinue) != 0) {
2750 uint32_t next_insn_idx = work_insn_idx_ + CurrentInsnFlags()->GetLengthInCodeUnits();
2751 if (next_insn_idx >= code_item_->insns_size_in_code_units_) {
2752 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Execution can walk off end of code area";
2753 return false;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002754 }
Ian Rogers6d376ae2013-07-23 15:12:40 -07002755 // The only way to get to a move-exception instruction is to get thrown there. Make sure the
2756 // next instruction isn't one.
2757 if (!CheckNotMoveException(code_item_->insns_, next_insn_idx)) {
2758 return false;
2759 }
2760 if (NULL != fallthrough_line.get()) {
2761 // Make workline consistent with fallthrough computed from peephole optimization.
2762 work_line_->CopyFromLine(fallthrough_line.get());
2763 }
Ian Rogersb8c78592013-07-25 23:52:52 +00002764 if (insn_flags_[next_insn_idx].IsReturn()) {
2765 // For returns we only care about the operand to the return, all other registers are dead.
2766 const Instruction* ret_inst = Instruction::At(code_item_->insns_ + next_insn_idx);
2767 Instruction::Code opcode = ret_inst->Opcode();
2768 if ((opcode == Instruction::RETURN_VOID) || (opcode == Instruction::RETURN_VOID_BARRIER)) {
2769 work_line_->MarkAllRegistersAsConflicts();
2770 } else {
2771 if (opcode == Instruction::RETURN_WIDE) {
2772 work_line_->MarkAllRegistersAsConflictsExceptWide(ret_inst->VRegA_11x());
2773 } else {
2774 work_line_->MarkAllRegistersAsConflictsExcept(ret_inst->VRegA_11x());
2775 }
2776 }
2777 }
Ian Rogers6d376ae2013-07-23 15:12:40 -07002778 RegisterLine* next_line = reg_table_.GetLine(next_insn_idx);
2779 if (next_line != NULL) {
2780 // Merge registers into what we have for the next instruction,
2781 // and set the "changed" flag if needed.
2782 if (!UpdateRegisters(next_insn_idx, work_line_.get())) {
2783 return false;
2784 }
2785 } else {
2786 /*
2787 * We're not recording register data for the next instruction, so we don't know what the
2788 * prior state was. We have to assume that something has changed and re-evaluate it.
2789 */
2790 insn_flags_[next_insn_idx].SetChanged();
2791 }
2792 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002793
jeffhaod1f0fde2011-09-08 17:25:33 -07002794 /* If we're returning from the method, make sure monitor stack is empty. */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002795 if ((opcode_flags & Instruction::kReturn) != 0) {
Elliott Hughesb25c3f62012-03-26 16:35:06 -07002796 if (!work_line_->VerifyMonitorStackEmpty()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002797 return false;
2798 }
jeffhaobdb76512011-09-07 11:43:16 -07002799 }
2800
2801 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002802 * Update start_guess. Advance to the next instruction of that's
2803 * possible, otherwise use the branch target if one was found. If
jeffhaobdb76512011-09-07 11:43:16 -07002804 * neither of those exists we're in a return or throw; leave start_guess
2805 * alone and let the caller sort it out.
2806 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002807 if ((opcode_flags & Instruction::kContinue) != 0) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002808 *start_guess = work_insn_idx_ + insn_flags_[work_insn_idx_].GetLengthInCodeUnits();
Elliott Hughesadb8c672012-03-06 16:49:32 -08002809 } else if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002810 /* we're still okay if branch_target is zero */
Ian Rogersd81871c2011-10-03 13:57:23 -07002811 *start_guess = work_insn_idx_ + branch_target;
jeffhaobdb76512011-09-07 11:43:16 -07002812 }
2813
Ian Rogersd81871c2011-10-03 13:57:23 -07002814 DCHECK_LT(*start_guess, code_item_->insns_size_in_code_units_);
2815 DCHECK(insn_flags_[*start_guess].IsOpcode());
jeffhaobdb76512011-09-07 11:43:16 -07002816
2817 return true;
Brian Carlstrom7934ac22013-07-26 10:54:15 -07002818} // NOLINT(readability/fn_size)
jeffhaobdb76512011-09-07 11:43:16 -07002819
Ian Rogers776ac1f2012-04-13 23:36:36 -07002820const RegType& MethodVerifier::ResolveClassAndCheckAccess(uint32_t class_idx) {
Ian Rogers0571d352011-11-03 19:51:38 -07002821 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002822 const RegType& referrer = GetDeclaringClass();
Mathieu Chartier590fee92013-09-13 13:46:47 -07002823 mirror::Class* klass = (*dex_cache_)->GetResolvedType(class_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002824 const RegType& result =
Ian Rogers04f94f42013-06-10 15:09:26 -07002825 klass != NULL ? reg_types_.FromClass(descriptor, klass,
2826 klass->CannotBeAssignedFromOtherTypes())
Mathieu Chartier590fee92013-09-13 13:46:47 -07002827 : reg_types_.FromDescriptor(class_loader_->get(), descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002828 if (result.IsConflict()) {
2829 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "accessing broken descriptor '" << descriptor
2830 << "' in " << referrer;
2831 return result;
2832 }
Ian Rogerse1758fe2012-04-19 11:31:15 -07002833 if (klass == NULL && !result.IsUnresolvedTypes()) {
Mathieu Chartier590fee92013-09-13 13:46:47 -07002834 (*dex_cache_)->SetResolvedType(class_idx, result.GetClass());
Ian Rogerse1758fe2012-04-19 11:31:15 -07002835 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002836 // Check if access is allowed. Unresolved types use xxxWithAccessCheck to
Jeff Haob24b4a72013-07-31 13:47:31 -07002837 // check at runtime if access is allowed and so pass here. If result is
2838 // primitive, skip the access check.
2839 if (result.IsNonZeroReferenceTypes() && !result.IsUnresolvedTypes() &&
2840 !referrer.IsUnresolvedTypes() && !referrer.CanAccess(result)) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002841 Fail(VERIFY_ERROR_ACCESS_CLASS) << "illegal class access: '"
Ian Rogersad0b3a32012-04-16 14:50:24 -07002842 << referrer << "' -> '" << result << "'";
Ian Rogers28ad40d2011-10-27 15:19:26 -07002843 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002844 return result;
Ian Rogersd81871c2011-10-03 13:57:23 -07002845}
2846
Ian Rogers776ac1f2012-04-13 23:36:36 -07002847const RegType& MethodVerifier::GetCaughtExceptionType() {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002848 const RegType* common_super = NULL;
Ian Rogersd81871c2011-10-03 13:57:23 -07002849 if (code_item_->tries_size_ != 0) {
Ian Rogers0571d352011-11-03 19:51:38 -07002850 const byte* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
Ian Rogersd81871c2011-10-03 13:57:23 -07002851 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
2852 for (uint32_t i = 0; i < handlers_size; i++) {
Ian Rogers0571d352011-11-03 19:51:38 -07002853 CatchHandlerIterator iterator(handlers_ptr);
2854 for (; iterator.HasNext(); iterator.Next()) {
2855 if (iterator.GetHandlerAddress() == (uint32_t) work_insn_idx_) {
2856 if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) {
Ian Rogersb4903572012-10-11 11:52:56 -07002857 common_super = &reg_types_.JavaLangThrowable(false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002858 } else {
Ian Rogers0571d352011-11-03 19:51:38 -07002859 const RegType& exception = ResolveClassAndCheckAccess(iterator.GetHandlerTypeIndex());
Ian Rogersc4762272012-02-01 15:55:55 -08002860 if (common_super == NULL) {
2861 // Unconditionally assign for the first handler. We don't assert this is a Throwable
2862 // as that is caught at runtime
2863 common_super = &exception;
Ian Rogersb4903572012-10-11 11:52:56 -07002864 } else if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(exception)) {
Jeff Haoc26a56c2013-11-04 12:00:47 -08002865 if (exception.IsUnresolvedTypes()) {
2866 // We don't know enough about the type. Fail here and let runtime handle it.
2867 Fail(VERIFY_ERROR_NO_CLASS) << "unresolved exception class " << exception;
2868 return exception;
2869 } else {
2870 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unexpected non-exception class " << exception;
2871 return reg_types_.Conflict();
2872 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002873 } else if (common_super->Equals(exception)) {
Ian Rogersc4762272012-02-01 15:55:55 -08002874 // odd case, but nothing to do
Ian Rogersd81871c2011-10-03 13:57:23 -07002875 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002876 common_super = &common_super->Merge(exception, &reg_types_);
Ian Rogersb4903572012-10-11 11:52:56 -07002877 CHECK(reg_types_.JavaLangThrowable(false).IsAssignableFrom(*common_super));
Ian Rogersd81871c2011-10-03 13:57:23 -07002878 }
2879 }
2880 }
2881 }
Ian Rogers0571d352011-11-03 19:51:38 -07002882 handlers_ptr = iterator.EndDataPointer();
Ian Rogersd81871c2011-10-03 13:57:23 -07002883 }
2884 }
2885 if (common_super == NULL) {
2886 /* no catch blocks, or no catches with classes we can find */
jeffhaod5347e02012-03-22 17:25:05 -07002887 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unable to find exception handler";
Ian Rogersad0b3a32012-04-16 14:50:24 -07002888 return reg_types_.Conflict();
Ian Rogersd81871c2011-10-03 13:57:23 -07002889 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002890 return *common_super;
Ian Rogersd81871c2011-10-03 13:57:23 -07002891}
2892
Brian Carlstromea46f952013-07-30 01:26:50 -07002893mirror::ArtMethod* MethodVerifier::ResolveMethodAndCheckAccess(uint32_t dex_method_idx,
Ian Rogersd91d6d62013-09-25 20:26:14 -07002894 MethodType method_type) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002895 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx);
Ian Rogers90040192011-12-16 08:54:29 -08002896 const RegType& klass_type = ResolveClassAndCheckAccess(method_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002897 if (klass_type.IsConflict()) {
2898 std::string append(" in attempt to access method ");
2899 append += dex_file_->GetMethodName(method_id);
2900 AppendToLastFailMessage(append);
Ian Rogers90040192011-12-16 08:54:29 -08002901 return NULL;
2902 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002903 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers90040192011-12-16 08:54:29 -08002904 return NULL; // Can't resolve Class so no more to do here
2905 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002906 mirror::Class* klass = klass_type.GetClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002907 const RegType& referrer = GetDeclaringClass();
Mathieu Chartier590fee92013-09-13 13:46:47 -07002908 mirror::ArtMethod* res_method = (*dex_cache_)->GetResolvedMethod(dex_method_idx);
Ian Rogersd81871c2011-10-03 13:57:23 -07002909 if (res_method == NULL) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07002910 const char* name = dex_file_->GetMethodName(method_id);
Ian Rogersd91d6d62013-09-25 20:26:14 -07002911 const Signature signature = dex_file_->GetMethodSignature(method_id);
jeffhao8cd6dda2012-02-22 10:15:34 -08002912
2913 if (method_type == METHOD_DIRECT || method_type == METHOD_STATIC) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002914 res_method = klass->FindDirectMethod(name, signature);
jeffhao8cd6dda2012-02-22 10:15:34 -08002915 } else if (method_type == METHOD_INTERFACE) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002916 res_method = klass->FindInterfaceMethod(name, signature);
2917 } else {
2918 res_method = klass->FindVirtualMethod(name, signature);
2919 }
2920 if (res_method != NULL) {
Mathieu Chartier590fee92013-09-13 13:46:47 -07002921 (*dex_cache_)->SetResolvedMethod(dex_method_idx, res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07002922 } else {
jeffhao8cd6dda2012-02-22 10:15:34 -08002923 // If a virtual or interface method wasn't found with the expected type, look in
2924 // the direct methods. This can happen when the wrong invoke type is used or when
2925 // a class has changed, and will be flagged as an error in later checks.
2926 if (method_type == METHOD_INTERFACE || method_type == METHOD_VIRTUAL) {
2927 res_method = klass->FindDirectMethod(name, signature);
2928 }
2929 if (res_method == NULL) {
2930 Fail(VERIFY_ERROR_NO_METHOD) << "couldn't find method "
2931 << PrettyDescriptor(klass) << "." << name
2932 << " " << signature;
2933 return NULL;
2934 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002935 }
2936 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002937 // Make sure calls to constructors are "direct". There are additional restrictions but we don't
2938 // enforce them here.
2939 if (res_method->IsConstructor() && method_type != METHOD_DIRECT) {
jeffhaod5347e02012-03-22 17:25:05 -07002940 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting non-direct call to constructor "
2941 << PrettyMethod(res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07002942 return NULL;
2943 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002944 // Disallow any calls to class initializers.
2945 if (MethodHelper(res_method).IsClassInitializer()) {
jeffhaod5347e02012-03-22 17:25:05 -07002946 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting call to class initializer "
2947 << PrettyMethod(res_method);
jeffhao8cd6dda2012-02-22 10:15:34 -08002948 return NULL;
2949 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002950 // Check if access is allowed.
Ian Rogersad0b3a32012-04-16 14:50:24 -07002951 if (!referrer.CanAccessMember(res_method->GetDeclaringClass(), res_method->GetAccessFlags())) {
jeffhao8cd6dda2012-02-22 10:15:34 -08002952 Fail(VERIFY_ERROR_ACCESS_METHOD) << "illegal method access (call " << PrettyMethod(res_method)
Ian Rogersad0b3a32012-04-16 14:50:24 -07002953 << " from " << referrer << ")";
jeffhaob57e9522012-04-26 18:08:21 -07002954 return res_method;
jeffhao8cd6dda2012-02-22 10:15:34 -08002955 }
jeffhaode0d9c92012-02-27 13:58:13 -08002956 // Check that invoke-virtual and invoke-super are not used on private methods of the same class.
2957 if (res_method->IsPrivate() && method_type == METHOD_VIRTUAL) {
jeffhaod5347e02012-03-22 17:25:05 -07002958 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invoke-super/virtual can't be used on private method "
2959 << PrettyMethod(res_method);
jeffhaode0d9c92012-02-27 13:58:13 -08002960 return NULL;
2961 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002962 // Check that interface methods match interface classes.
2963 if (klass->IsInterface() && method_type != METHOD_INTERFACE) {
2964 Fail(VERIFY_ERROR_CLASS_CHANGE) << "non-interface method " << PrettyMethod(res_method)
2965 << " is in an interface class " << PrettyClass(klass);
2966 return NULL;
2967 } else if (!klass->IsInterface() && method_type == METHOD_INTERFACE) {
2968 Fail(VERIFY_ERROR_CLASS_CHANGE) << "interface method " << PrettyMethod(res_method)
2969 << " is in a non-interface class " << PrettyClass(klass);
2970 return NULL;
2971 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002972 // See if the method type implied by the invoke instruction matches the access flags for the
2973 // target method.
2974 if ((method_type == METHOD_DIRECT && !res_method->IsDirect()) ||
2975 (method_type == METHOD_STATIC && !res_method->IsStatic()) ||
2976 ((method_type == METHOD_VIRTUAL || method_type == METHOD_INTERFACE) && res_method->IsDirect())
2977 ) {
Ian Rogers2fc14272012-08-30 10:56:57 -07002978 Fail(VERIFY_ERROR_CLASS_CHANGE) << "invoke type (" << method_type << ") does not match method "
2979 " type of " << PrettyMethod(res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07002980 return NULL;
2981 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002982 return res_method;
2983}
2984
Brian Carlstromea46f952013-07-30 01:26:50 -07002985mirror::ArtMethod* MethodVerifier::VerifyInvocationArgs(const Instruction* inst,
Sebastien Hertz5243e912013-05-21 10:55:07 +02002986 MethodType method_type,
2987 bool is_range,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002988 bool is_super) {
jeffhao8cd6dda2012-02-22 10:15:34 -08002989 // Resolve the method. This could be an abstract or concrete method depending on what sort of call
2990 // we're making.
Sebastien Hertz5243e912013-05-21 10:55:07 +02002991 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Brian Carlstromea46f952013-07-30 01:26:50 -07002992 mirror::ArtMethod* res_method = ResolveMethodAndCheckAccess(method_idx, method_type);
jeffhao8cd6dda2012-02-22 10:15:34 -08002993 if (res_method == NULL) { // error or class is unresolved
2994 return NULL;
2995 }
2996
Ian Rogersd81871c2011-10-03 13:57:23 -07002997 // If we're using invoke-super(method), make sure that the executing method's class' superclass
2998 // has a vtable entry for the target method.
2999 if (is_super) {
3000 DCHECK(method_type == METHOD_VIRTUAL);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003001 const RegType& super = GetDeclaringClass().GetSuperClass(&reg_types_);
Ian Rogers529781d2012-07-23 17:24:29 -07003002 if (super.IsUnresolvedTypes()) {
jeffhao4d8df822012-04-24 17:09:36 -07003003 Fail(VERIFY_ERROR_NO_METHOD) << "unknown super class in invoke-super from "
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003004 << PrettyMethod(dex_method_idx_, *dex_file_)
jeffhao4d8df822012-04-24 17:09:36 -07003005 << " to super " << PrettyMethod(res_method);
3006 return NULL;
3007 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003008 mirror::Class* super_klass = super.GetClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07003009 if (res_method->GetMethodIndex() >= super_klass->GetVTable()->GetLength()) {
jeffhao4d8df822012-04-24 17:09:36 -07003010 MethodHelper mh(res_method);
3011 Fail(VERIFY_ERROR_NO_METHOD) << "invalid invoke-super from "
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003012 << PrettyMethod(dex_method_idx_, *dex_file_)
jeffhao4d8df822012-04-24 17:09:36 -07003013 << " to super " << super
3014 << "." << mh.GetName()
3015 << mh.GetSignature();
Ian Rogersd81871c2011-10-03 13:57:23 -07003016 return NULL;
3017 }
3018 }
3019 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003020 // match the call to the signature. Also, we might be calling through an abstract method
Ian Rogersd81871c2011-10-03 13:57:23 -07003021 // definition (which doesn't have register count values).
Sebastien Hertz5243e912013-05-21 10:55:07 +02003022 const size_t expected_args = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
Ian Rogersd81871c2011-10-03 13:57:23 -07003023 /* caught by static verifier */
3024 DCHECK(is_range || expected_args <= 5);
3025 if (expected_args > code_item_->outs_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07003026 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
Ian Rogersd81871c2011-10-03 13:57:23 -07003027 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
3028 return NULL;
3029 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003030
jeffhaobdb76512011-09-07 11:43:16 -07003031 /*
Ian Rogersad0b3a32012-04-16 14:50:24 -07003032 * Check the "this" argument, which must be an instance of the class that declared the method.
3033 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
3034 * rigorous check here (which is okay since we have to do it at runtime).
jeffhaobdb76512011-09-07 11:43:16 -07003035 */
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003036 size_t actual_args = 0;
Ian Rogersd81871c2011-10-03 13:57:23 -07003037 if (!res_method->IsStatic()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003038 const RegType& actual_arg_type = work_line_->GetInvocationThis(inst, is_range);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003039 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
Ian Rogersd81871c2011-10-03 13:57:23 -07003040 return NULL;
3041 }
3042 if (actual_arg_type.IsUninitializedReference() && !res_method->IsConstructor()) {
jeffhaod5347e02012-03-22 17:25:05 -07003043 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
Ian Rogersd81871c2011-10-03 13:57:23 -07003044 return NULL;
3045 }
3046 if (method_type != METHOD_INTERFACE && !actual_arg_type.IsZero()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003047 mirror::Class* klass = res_method->GetDeclaringClass();
Ian Rogers04f94f42013-06-10 15:09:26 -07003048 const RegType& res_method_class =
3049 reg_types_.FromClass(ClassHelper(klass).GetDescriptor(), klass,
3050 klass->CannotBeAssignedFromOtherTypes());
Ian Rogers9074b992011-10-26 17:41:55 -07003051 if (!res_method_class.IsAssignableFrom(actual_arg_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07003052 Fail(actual_arg_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS:
3053 VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003054 << "' not instance of '" << res_method_class << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07003055 return NULL;
3056 }
3057 }
3058 actual_args++;
3059 }
3060 /*
3061 * Process the target method's signature. This signature may or may not
3062 * have been verified, so we can't assume it's properly formed.
3063 */
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003064 MethodHelper mh(res_method);
3065 const DexFile::TypeList* params = mh.GetParameterTypeList();
3066 size_t params_size = params == NULL ? 0 : params->Size();
Sebastien Hertz5243e912013-05-21 10:55:07 +02003067 uint32_t arg[5];
3068 if (!is_range) {
3069 inst->GetArgs(arg);
3070 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003071 for (size_t param_index = 0; param_index < params_size; param_index++) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003072 if (actual_args >= expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07003073 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invalid call to '" << PrettyMethod(res_method)
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003074 << "'. Expected " << expected_args << " arguments, processing argument " << actual_args
3075 << " (where longs/doubles count twice).";
Ian Rogersd81871c2011-10-03 13:57:23 -07003076 return NULL;
3077 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003078 const char* descriptor =
3079 mh.GetTypeDescriptorFromTypeIdx(params->GetTypeItem(param_index).type_idx_);
3080 if (descriptor == NULL) {
jeffhaod5347e02012-03-22 17:25:05 -07003081 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003082 << " missing signature component";
3083 return NULL;
Ian Rogersd81871c2011-10-03 13:57:23 -07003084 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07003085 const RegType& reg_type = reg_types_.FromDescriptor(class_loader_->get(), descriptor, false);
Sebastien Hertz5243e912013-05-21 10:55:07 +02003086 uint32_t get_reg = is_range ? inst->VRegC_3rc() + actual_args : arg[actual_args];
Jeff Haoa6b22c52013-10-04 14:33:22 -07003087 if (reg_type.IsIntegralTypes()) {
3088 const RegType& src_type = work_line_->GetRegisterType(get_reg);
3089 if (!src_type.IsIntegralTypes()) {
3090 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register v" << get_reg << " has type " << src_type
3091 << " but expected " << reg_type;
3092 return res_method;
3093 }
3094 } else if (!work_line_->VerifyRegisterType(get_reg, reg_type)) {
jeffhaob57e9522012-04-26 18:08:21 -07003095 return res_method;
Ian Rogersd81871c2011-10-03 13:57:23 -07003096 }
3097 actual_args = reg_type.IsLongOrDoubleTypes() ? actual_args + 2 : actual_args + 1;
3098 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003099 if (actual_args != expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07003100 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003101 << " expected " << expected_args << " arguments, found " << actual_args;
Ian Rogersd81871c2011-10-03 13:57:23 -07003102 return NULL;
3103 } else {
3104 return res_method;
3105 }
3106}
3107
Brian Carlstromea46f952013-07-30 01:26:50 -07003108mirror::ArtMethod* MethodVerifier::GetQuickInvokedMethod(const Instruction* inst,
Mathieu Chartier590fee92013-09-13 13:46:47 -07003109 RegisterLine* reg_line, bool is_range) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003110 DCHECK(inst->Opcode() == Instruction::INVOKE_VIRTUAL_QUICK ||
3111 inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
3112 const RegType& actual_arg_type = reg_line->GetInvocationThis(inst, is_range);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003113 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
3114 return NULL;
Sebastien Hertz8249b422013-10-29 17:50:55 +01003115 } else if (actual_arg_type.IsZero()) { // Invoke on "null" instance: we can't go further.
3116 return NULL;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003117 }
3118 mirror::Class* this_class = NULL;
3119 if (!actual_arg_type.IsUnresolvedTypes()) {
3120 this_class = actual_arg_type.GetClass();
3121 } else {
3122 const std::string& descriptor(actual_arg_type.GetDescriptor());
3123 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartier590fee92013-09-13 13:46:47 -07003124 this_class = class_linker->FindClass(descriptor.c_str(), *class_loader_);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003125 if (this_class == NULL) {
Mathieu Chartier590fee92013-09-13 13:46:47 -07003126 Thread* self = Thread::Current();
3127 self->ClearException();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003128 // Look for a system class
Mathieu Chartier590fee92013-09-13 13:46:47 -07003129 SirtRef<mirror::ClassLoader> null_class_loader(self, nullptr);
3130 this_class = class_linker->FindClass(descriptor.c_str(), null_class_loader);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003131 }
3132 }
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003133 if (this_class == NULL) {
3134 return NULL;
3135 }
Brian Carlstromea46f952013-07-30 01:26:50 -07003136 mirror::ObjectArray<mirror::ArtMethod>* vtable = this_class->GetVTable();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003137 CHECK(vtable != NULL);
3138 uint16_t vtable_index = is_range ? inst->VRegB_3rc() : inst->VRegB_35c();
3139 CHECK(vtable_index < vtable->GetLength());
Brian Carlstromea46f952013-07-30 01:26:50 -07003140 mirror::ArtMethod* res_method = vtable->Get(vtable_index);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003141 CHECK(!Thread::Current()->IsExceptionPending());
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003142 return res_method;
3143}
3144
Brian Carlstromea46f952013-07-30 01:26:50 -07003145mirror::ArtMethod* MethodVerifier::VerifyInvokeVirtualQuickArgs(const Instruction* inst,
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003146 bool is_range) {
3147 DCHECK(Runtime::Current()->IsStarted());
Brian Carlstromea46f952013-07-30 01:26:50 -07003148 mirror::ArtMethod* res_method = GetQuickInvokedMethod(inst, work_line_.get(),
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003149 is_range);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003150 if (res_method == NULL) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003151 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer method from " << inst->Name();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003152 return NULL;
3153 }
3154 CHECK(!res_method->IsDirect() && !res_method->IsStatic());
3155
3156 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
3157 // match the call to the signature. Also, we might be calling through an abstract method
3158 // definition (which doesn't have register count values).
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003159 const RegType& actual_arg_type = work_line_->GetInvocationThis(inst, is_range);
3160 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
3161 return NULL;
3162 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003163 const size_t expected_args = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
3164 /* caught by static verifier */
3165 DCHECK(is_range || expected_args <= 5);
3166 if (expected_args > code_item_->outs_size_) {
3167 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
3168 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
3169 return NULL;
3170 }
3171
3172 /*
3173 * Check the "this" argument, which must be an instance of the class that declared the method.
3174 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
3175 * rigorous check here (which is okay since we have to do it at runtime).
3176 */
3177 if (actual_arg_type.IsUninitializedReference() && !res_method->IsConstructor()) {
3178 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
3179 return NULL;
3180 }
3181 if (!actual_arg_type.IsZero()) {
3182 mirror::Class* klass = res_method->GetDeclaringClass();
3183 const RegType& res_method_class =
3184 reg_types_.FromClass(ClassHelper(klass).GetDescriptor(), klass,
3185 klass->CannotBeAssignedFromOtherTypes());
3186 if (!res_method_class.IsAssignableFrom(actual_arg_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07003187 Fail(actual_arg_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS :
3188 VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003189 << "' not instance of '" << res_method_class << "'";
3190 return NULL;
3191 }
3192 }
3193 /*
3194 * Process the target method's signature. This signature may or may not
3195 * have been verified, so we can't assume it's properly formed.
3196 */
3197 MethodHelper mh(res_method);
3198 const DexFile::TypeList* params = mh.GetParameterTypeList();
3199 size_t params_size = params == NULL ? 0 : params->Size();
3200 uint32_t arg[5];
3201 if (!is_range) {
3202 inst->GetArgs(arg);
3203 }
3204 size_t actual_args = 1;
3205 for (size_t param_index = 0; param_index < params_size; param_index++) {
3206 if (actual_args >= expected_args) {
3207 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invalid call to '" << PrettyMethod(res_method)
Brian Carlstrom93c33962013-07-26 10:37:43 -07003208 << "'. Expected " << expected_args
3209 << " arguments, processing argument " << actual_args
3210 << " (where longs/doubles count twice).";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003211 return NULL;
3212 }
3213 const char* descriptor =
3214 mh.GetTypeDescriptorFromTypeIdx(params->GetTypeItem(param_index).type_idx_);
3215 if (descriptor == NULL) {
3216 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003217 << " missing signature component";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003218 return NULL;
3219 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07003220 const RegType& reg_type = reg_types_.FromDescriptor(class_loader_->get(), descriptor, false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003221 uint32_t get_reg = is_range ? inst->VRegC_3rc() + actual_args : arg[actual_args];
3222 if (!work_line_->VerifyRegisterType(get_reg, reg_type)) {
3223 return res_method;
3224 }
3225 actual_args = reg_type.IsLongOrDoubleTypes() ? actual_args + 2 : actual_args + 1;
3226 }
3227 if (actual_args != expected_args) {
3228 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
3229 << " expected " << expected_args << " arguments, found " << actual_args;
3230 return NULL;
3231 } else {
3232 return res_method;
3233 }
3234}
3235
Ian Rogers62342ec2013-06-11 10:26:37 -07003236void MethodVerifier::VerifyNewArray(const Instruction* inst, bool is_filled, bool is_range) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003237 uint32_t type_idx;
3238 if (!is_filled) {
3239 DCHECK_EQ(inst->Opcode(), Instruction::NEW_ARRAY);
3240 type_idx = inst->VRegC_22c();
3241 } else if (!is_range) {
3242 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY);
3243 type_idx = inst->VRegB_35c();
3244 } else {
3245 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY_RANGE);
3246 type_idx = inst->VRegB_3rc();
3247 }
3248 const RegType& res_type = ResolveClassAndCheckAccess(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003249 if (res_type.IsConflict()) { // bad class
3250 DCHECK_NE(failures_.size(), 0U);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003251 } else {
3252 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
3253 if (!res_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003254 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "new-array on non-array class " << res_type;
Ian Rogers0c4a5062012-02-03 15:18:59 -08003255 } else if (!is_filled) {
3256 /* make sure "size" register is valid type */
Sebastien Hertz5243e912013-05-21 10:55:07 +02003257 work_line_->VerifyRegisterType(inst->VRegB_22c(), reg_types_.Integer());
Ian Rogers0c4a5062012-02-03 15:18:59 -08003258 /* set register type to array class */
Ian Rogers62342ec2013-06-11 10:26:37 -07003259 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
3260 work_line_->SetRegisterType(inst->VRegA_22c(), precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003261 } else {
3262 // Verify each register. If "arg_count" is bad, VerifyRegisterType() will run off the end of
3263 // the list and fail. It's legal, if silly, for arg_count to be zero.
Mathieu Chartier590fee92013-09-13 13:46:47 -07003264 const RegType& expected_type = reg_types_.GetComponentType(res_type, class_loader_->get());
Sebastien Hertz5243e912013-05-21 10:55:07 +02003265 uint32_t arg_count = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
3266 uint32_t arg[5];
3267 if (!is_range) {
3268 inst->GetArgs(arg);
3269 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08003270 for (size_t ui = 0; ui < arg_count; ui++) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003271 uint32_t get_reg = is_range ? inst->VRegC_3rc() + ui : arg[ui];
Ian Rogers0c4a5062012-02-03 15:18:59 -08003272 if (!work_line_->VerifyRegisterType(get_reg, expected_type)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003273 work_line_->SetResultRegisterType(reg_types_.Conflict());
Ian Rogers0c4a5062012-02-03 15:18:59 -08003274 return;
3275 }
3276 }
3277 // filled-array result goes into "result" register
Ian Rogers62342ec2013-06-11 10:26:37 -07003278 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
3279 work_line_->SetResultRegisterType(precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003280 }
3281 }
3282}
3283
Sebastien Hertz5243e912013-05-21 10:55:07 +02003284void MethodVerifier::VerifyAGet(const Instruction* inst,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003285 const RegType& insn_type, bool is_primitive) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003286 const RegType& index_type = work_line_->GetRegisterType(inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07003287 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003288 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07003289 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003290 const RegType& array_type = work_line_->GetRegisterType(inst->VRegB_23x());
Ian Rogers89310de2012-02-01 13:47:30 -08003291 if (array_type.IsZero()) {
3292 // Null array class; this code path will fail at runtime. Infer a merge-able type from the
3293 // instruction type. TODO: have a proper notion of bottom here.
3294 if (!is_primitive || insn_type.IsCategory1Types()) {
3295 // Reference or category 1
Sebastien Hertz5243e912013-05-21 10:55:07 +02003296 work_line_->SetRegisterType(inst->VRegA_23x(), reg_types_.Zero());
Ian Rogersd81871c2011-10-03 13:57:23 -07003297 } else {
Ian Rogers89310de2012-02-01 13:47:30 -08003298 // Category 2
Sebastien Hertz5243e912013-05-21 10:55:07 +02003299 work_line_->SetRegisterTypeWide(inst->VRegA_23x(), reg_types_.FromCat2ConstLo(0, false),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003300 reg_types_.FromCat2ConstHi(0, false));
Ian Rogers89310de2012-02-01 13:47:30 -08003301 }
jeffhaofc3144e2012-02-01 17:21:15 -08003302 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003303 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aget";
Ian Rogers89310de2012-02-01 13:47:30 -08003304 } else {
3305 /* verify the class */
Mathieu Chartier590fee92013-09-13 13:46:47 -07003306 const RegType& component_type = reg_types_.GetComponentType(array_type, class_loader_->get());
jeffhaofc3144e2012-02-01 17:21:15 -08003307 if (!component_type.IsReferenceTypes() && !is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07003308 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08003309 << " source for aget-object";
3310 } else if (component_type.IsNonZeroReferenceTypes() && is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07003311 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08003312 << " source for category 1 aget";
3313 } else if (is_primitive && !insn_type.Equals(component_type) &&
3314 !((insn_type.IsInteger() && component_type.IsFloat()) ||
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003315 (insn_type.IsLong() && component_type.IsDouble()))) {
3316 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array type " << array_type
3317 << " incompatible with aget of type " << insn_type;
Ian Rogers89310de2012-02-01 13:47:30 -08003318 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003319 // Use knowledge of the field type which is stronger than the type inferred from the
3320 // instruction, which can't differentiate object types and ints from floats, longs from
3321 // doubles.
3322 if (!component_type.IsLowHalf()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003323 work_line_->SetRegisterType(inst->VRegA_23x(), component_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003324 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003325 work_line_->SetRegisterTypeWide(inst->VRegA_23x(), component_type,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003326 component_type.HighHalf(&reg_types_));
3327 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003328 }
3329 }
3330 }
3331}
3332
Jeff Haofe1f7c82013-08-01 14:50:24 -07003333void MethodVerifier::VerifyPrimitivePut(const RegType& target_type, const RegType& insn_type,
3334 const uint32_t vregA) {
3335 // Primitive assignability rules are weaker than regular assignability rules.
3336 bool instruction_compatible;
3337 bool value_compatible;
3338 const RegType& value_type = work_line_->GetRegisterType(vregA);
3339 if (target_type.IsIntegralTypes()) {
Jeff Haoa4647482013-08-06 15:35:47 -07003340 instruction_compatible = target_type.Equals(insn_type);
Jeff Haofe1f7c82013-08-01 14:50:24 -07003341 value_compatible = value_type.IsIntegralTypes();
3342 } else if (target_type.IsFloat()) {
3343 instruction_compatible = insn_type.IsInteger(); // no put-float, so expect put-int
3344 value_compatible = value_type.IsFloatTypes();
3345 } else if (target_type.IsLong()) {
3346 instruction_compatible = insn_type.IsLong();
3347 value_compatible = value_type.IsLongTypes();
3348 } else if (target_type.IsDouble()) {
3349 instruction_compatible = insn_type.IsLong(); // no put-double, so expect put-long
3350 value_compatible = value_type.IsDoubleTypes();
3351 } else {
3352 instruction_compatible = false; // reference with primitive store
3353 value_compatible = false; // unused
3354 }
3355 if (!instruction_compatible) {
3356 // This is a global failure rather than a class change failure as the instructions and
3357 // the descriptors for the type should have been consistent within the same file at
3358 // compile time.
3359 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "put insn has type '" << insn_type
3360 << "' but expected type '" << target_type << "'";
3361 return;
3362 }
3363 if (!value_compatible) {
3364 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
3365 << " of type " << value_type << " but expected " << target_type << " for put";
3366 return;
3367 }
3368}
3369
Sebastien Hertz5243e912013-05-21 10:55:07 +02003370void MethodVerifier::VerifyAPut(const Instruction* inst,
Ian Rogersd81871c2011-10-03 13:57:23 -07003371 const RegType& insn_type, bool is_primitive) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003372 const RegType& index_type = work_line_->GetRegisterType(inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07003373 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003374 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07003375 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003376 const RegType& array_type = work_line_->GetRegisterType(inst->VRegB_23x());
Ian Rogers89310de2012-02-01 13:47:30 -08003377 if (array_type.IsZero()) {
3378 // Null array type; this code path will fail at runtime. Infer a merge-able type from the
3379 // instruction type.
jeffhaofc3144e2012-02-01 17:21:15 -08003380 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003381 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aput";
Ian Rogers89310de2012-02-01 13:47:30 -08003382 } else {
Mathieu Chartier590fee92013-09-13 13:46:47 -07003383 const RegType& component_type = reg_types_.GetComponentType(array_type, class_loader_->get());
Jeff Haofe1f7c82013-08-01 14:50:24 -07003384 const uint32_t vregA = inst->VRegA_23x();
Jeff Haob24b4a72013-07-31 13:47:31 -07003385 if (is_primitive) {
Jeff Haofe1f7c82013-08-01 14:50:24 -07003386 VerifyPrimitivePut(component_type, insn_type, vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07003387 } else {
Jeff Haob24b4a72013-07-31 13:47:31 -07003388 if (!component_type.IsReferenceTypes()) {
3389 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
3390 << " source for aput-object";
3391 } else {
3392 // The instruction agrees with the type of array, confirm the value to be stored does too
3393 // Note: we use the instruction type (rather than the component type) for aput-object as
3394 // incompatible classes will be caught at runtime as an array store exception
Jeff Haofe1f7c82013-08-01 14:50:24 -07003395 work_line_->VerifyRegisterType(vregA, insn_type);
Jeff Haob24b4a72013-07-31 13:47:31 -07003396 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003397 }
3398 }
3399 }
3400}
3401
Brian Carlstromea46f952013-07-30 01:26:50 -07003402mirror::ArtField* MethodVerifier::GetStaticField(int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08003403 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3404 // Check access to class
3405 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Brian Carlstrom7934ac22013-07-26 10:54:15 -07003406 if (klass_type.IsConflict()) { // bad class
Ian Rogersad0b3a32012-04-16 14:50:24 -07003407 AppendToLastFailMessage(StringPrintf(" in attempt to access static field %d (%s) in %s",
3408 field_idx, dex_file_->GetFieldName(field_id),
3409 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers90040192011-12-16 08:54:29 -08003410 return NULL;
3411 }
Elliott Hughesb25c3f62012-03-26 16:35:06 -07003412 if (klass_type.IsUnresolvedTypes()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003413 return NULL; // Can't resolve Class so no more to do here, will do checking at runtime.
Ian Rogers90040192011-12-16 08:54:29 -08003414 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07003415 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
3416 mirror::ArtField* field = class_linker->ResolveFieldJLS(*dex_file_, field_idx, *dex_cache_,
3417 *class_loader_);
Ian Rogersd81871c2011-10-03 13:57:23 -07003418 if (field == NULL) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07003419 VLOG(verifier) << "Unable to resolve static field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07003420 << dex_file_->GetFieldName(field_id) << ") in "
3421 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07003422 DCHECK(Thread::Current()->IsExceptionPending());
3423 Thread::Current()->ClearException();
3424 return NULL;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003425 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
3426 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003427 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access static field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07003428 << " from " << GetDeclaringClass();
Ian Rogersd81871c2011-10-03 13:57:23 -07003429 return NULL;
3430 } else if (!field->IsStatic()) {
3431 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field) << " to be static";
3432 return NULL;
Ian Rogersd81871c2011-10-03 13:57:23 -07003433 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07003434 return field;
Ian Rogersd81871c2011-10-03 13:57:23 -07003435}
3436
Brian Carlstromea46f952013-07-30 01:26:50 -07003437mirror::ArtField* MethodVerifier::GetInstanceField(const RegType& obj_type, int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08003438 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3439 // Check access to class
3440 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003441 if (klass_type.IsConflict()) {
3442 AppendToLastFailMessage(StringPrintf(" in attempt to access instance field %d (%s) in %s",
3443 field_idx, dex_file_->GetFieldName(field_id),
3444 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers90040192011-12-16 08:54:29 -08003445 return NULL;
3446 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003447 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers90040192011-12-16 08:54:29 -08003448 return NULL; // Can't resolve Class so no more to do here
3449 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07003450 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
3451 mirror::ArtField* field = class_linker->ResolveFieldJLS(*dex_file_, field_idx, *dex_cache_,
3452 *class_loader_);
Ian Rogersd81871c2011-10-03 13:57:23 -07003453 if (field == NULL) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07003454 VLOG(verifier) << "Unable to resolve instance field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07003455 << dex_file_->GetFieldName(field_id) << ") in "
3456 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07003457 DCHECK(Thread::Current()->IsExceptionPending());
3458 Thread::Current()->ClearException();
3459 return NULL;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003460 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
3461 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003462 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access instance field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07003463 << " from " << GetDeclaringClass();
Ian Rogersd81871c2011-10-03 13:57:23 -07003464 return NULL;
3465 } else if (field->IsStatic()) {
3466 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field)
3467 << " to not be static";
3468 return NULL;
3469 } else if (obj_type.IsZero()) {
3470 // Cannot infer and check type, however, access will cause null pointer exception
3471 return field;
Ian Rogerse1758fe2012-04-19 11:31:15 -07003472 } else {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003473 mirror::Class* klass = field->GetDeclaringClass();
Ian Rogers637c65b2013-05-31 11:46:00 -07003474 const RegType& field_klass =
3475 reg_types_.FromClass(dex_file_->GetFieldDeclaringClassDescriptor(field_id),
Ian Rogers04f94f42013-06-10 15:09:26 -07003476 klass, klass->CannotBeAssignedFromOtherTypes());
Ian Rogersad0b3a32012-04-16 14:50:24 -07003477 if (obj_type.IsUninitializedTypes() &&
3478 (!IsConstructor() || GetDeclaringClass().Equals(obj_type) ||
3479 !field_klass.Equals(GetDeclaringClass()))) {
3480 // Field accesses through uninitialized references are only allowable for constructors where
3481 // the field is declared in this class
3482 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "cannot access instance field " << PrettyField(field)
Brian Carlstrom93c33962013-07-26 10:37:43 -07003483 << " of a not fully initialized object within the context"
3484 << " of " << PrettyMethod(dex_method_idx_, *dex_file_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003485 return NULL;
3486 } else if (!field_klass.IsAssignableFrom(obj_type)) {
3487 // Trying to access C1.field1 using reference of type C2, which is neither C1 or a sub-class
3488 // of C1. For resolution to occur the declared class of the field must be compatible with
3489 // obj_type, we've discovered this wasn't so, so report the field didn't exist.
3490 Fail(VERIFY_ERROR_NO_FIELD) << "cannot access instance field " << PrettyField(field)
3491 << " from object of type " << obj_type;
3492 return NULL;
3493 } else {
3494 return field;
3495 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003496 }
3497}
3498
Sebastien Hertz5243e912013-05-21 10:55:07 +02003499void MethodVerifier::VerifyISGet(const Instruction* inst, const RegType& insn_type,
3500 bool is_primitive, bool is_static) {
3501 uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
Brian Carlstromea46f952013-07-30 01:26:50 -07003502 mirror::ArtField* field;
Ian Rogersb94a27b2011-10-26 00:33:41 -07003503 if (is_static) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07003504 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003505 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003506 const RegType& object_type = work_line_->GetRegisterType(inst->VRegB_22c());
Ian Rogersf4028cc2011-11-02 14:56:39 -07003507 field = GetInstanceField(object_type, field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003508 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003509 const RegType* field_type = nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003510 if (field != NULL) {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003511 FieldHelper fh(field);
3512 mirror::Class* field_type_class = fh.GetType(false);
3513 if (field_type_class != nullptr) {
3514 field_type = &reg_types_.FromClass(fh.GetTypeDescriptor(), field_type_class,
3515 field_type_class->CannotBeAssignedFromOtherTypes());
3516 }
Ian Rogers0d604842012-04-16 14:50:24 -07003517 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003518 if (field_type == nullptr) {
3519 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3520 const char* descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
Mathieu Chartier590fee92013-09-13 13:46:47 -07003521 field_type = &reg_types_.FromDescriptor(class_loader_->get(), descriptor, false);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003522 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02003523 const uint32_t vregA = (is_static) ? inst->VRegA_21c() : inst->VRegA_22c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07003524 if (is_primitive) {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003525 if (field_type->Equals(insn_type) ||
3526 (field_type->IsFloat() && insn_type.IsInteger()) ||
3527 (field_type->IsDouble() && insn_type.IsLong())) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003528 // expected that read is of the correct primitive type or that int reads are reading
3529 // floats or long reads are reading doubles
3530 } else {
3531 // This is a global failure rather than a class change failure as the instructions and
3532 // the descriptors for the type should have been consistent within the same file at
3533 // compile time
3534 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
3535 << " to be of type '" << insn_type
3536 << "' but found type '" << field_type << "' in get";
Ian Rogersad0b3a32012-04-16 14:50:24 -07003537 return;
3538 }
3539 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003540 if (!insn_type.IsAssignableFrom(*field_type)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003541 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
3542 << " to be compatible with type '" << insn_type
3543 << "' but found type '" << field_type
3544 << "' in get-object";
Sebastien Hertz5243e912013-05-21 10:55:07 +02003545 work_line_->SetRegisterType(vregA, reg_types_.Conflict());
Ian Rogersad0b3a32012-04-16 14:50:24 -07003546 return;
3547 }
3548 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003549 if (!field_type->IsLowHalf()) {
3550 work_line_->SetRegisterType(vregA, *field_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003551 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003552 work_line_->SetRegisterTypeWide(vregA, *field_type, field_type->HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003553 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003554}
3555
Sebastien Hertz5243e912013-05-21 10:55:07 +02003556void MethodVerifier::VerifyISPut(const Instruction* inst, const RegType& insn_type,
3557 bool is_primitive, bool is_static) {
3558 uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
Brian Carlstromea46f952013-07-30 01:26:50 -07003559 mirror::ArtField* field;
Ian Rogersb94a27b2011-10-26 00:33:41 -07003560 if (is_static) {
Ian Rogers55d249f2011-11-02 16:48:09 -07003561 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003562 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003563 const RegType& object_type = work_line_->GetRegisterType(inst->VRegB_22c());
Ian Rogers55d249f2011-11-02 16:48:09 -07003564 field = GetInstanceField(object_type, field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003565 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003566 const RegType* field_type = nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003567 if (field != NULL) {
3568 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
3569 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
3570 << " from other class " << GetDeclaringClass();
3571 return;
3572 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003573 FieldHelper fh(field);
3574 mirror::Class* field_type_class = fh.GetType(false);
3575 if (field_type_class != nullptr) {
3576 field_type = &reg_types_.FromClass(fh.GetTypeDescriptor(), field_type_class,
3577 field_type_class->CannotBeAssignedFromOtherTypes());
3578 }
3579 }
3580 if (field_type == nullptr) {
3581 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3582 const char* descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
Mathieu Chartier590fee92013-09-13 13:46:47 -07003583 field_type = &reg_types_.FromDescriptor(class_loader_->get(), descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003584 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02003585 const uint32_t vregA = (is_static) ? inst->VRegA_21c() : inst->VRegA_22c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07003586 if (is_primitive) {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003587 VerifyPrimitivePut(*field_type, insn_type, vregA);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003588 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003589 if (!insn_type.IsAssignableFrom(*field_type)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003590 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
3591 << " to be compatible with type '" << insn_type
3592 << "' but found type '" << field_type
3593 << "' in put-object";
3594 return;
3595 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003596 work_line_->VerifyRegisterType(vregA, *field_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07003597 }
3598}
3599
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003600// Look for an instance field with this offset.
3601// TODO: we may speed up the search if offsets are sorted by doing a quick search.
Brian Carlstromea46f952013-07-30 01:26:50 -07003602static mirror::ArtField* FindInstanceFieldWithOffset(const mirror::Class* klass,
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003603 uint32_t field_offset)
3604 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstromea46f952013-07-30 01:26:50 -07003605 const mirror::ObjectArray<mirror::ArtField>* instance_fields = klass->GetIFields();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003606 if (instance_fields != NULL) {
3607 for (int32_t i = 0, e = instance_fields->GetLength(); i < e; ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -07003608 mirror::ArtField* field = instance_fields->Get(i);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003609 if (field->GetOffset().Uint32Value() == field_offset) {
3610 return field;
3611 }
3612 }
3613 }
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003614 // We did not find field in class: look into superclass.
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003615 if (klass->GetSuperClass() != NULL) {
3616 return FindInstanceFieldWithOffset(klass->GetSuperClass(), field_offset);
3617 } else {
3618 return NULL;
3619 }
3620}
3621
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003622// Returns the access field of a quick field access (iget/iput-quick) or NULL
3623// if it cannot be found.
Brian Carlstromea46f952013-07-30 01:26:50 -07003624mirror::ArtField* MethodVerifier::GetQuickFieldAccess(const Instruction* inst,
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003625 RegisterLine* reg_line) {
3626 DCHECK(inst->Opcode() == Instruction::IGET_QUICK ||
3627 inst->Opcode() == Instruction::IGET_WIDE_QUICK ||
3628 inst->Opcode() == Instruction::IGET_OBJECT_QUICK ||
3629 inst->Opcode() == Instruction::IPUT_QUICK ||
3630 inst->Opcode() == Instruction::IPUT_WIDE_QUICK ||
3631 inst->Opcode() == Instruction::IPUT_OBJECT_QUICK);
3632 const RegType& object_type = reg_line->GetRegisterType(inst->VRegB_22c());
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003633 mirror::Class* object_class = NULL;
3634 if (!object_type.IsUnresolvedTypes()) {
3635 object_class = object_type.GetClass();
3636 } else {
3637 // We need to resolve the class from its descriptor.
3638 const std::string& descriptor(object_type.GetDescriptor());
3639 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartier590fee92013-09-13 13:46:47 -07003640 Thread* self = Thread::Current();
3641 object_class = class_linker->FindClass(descriptor.c_str(), *class_loader_);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003642 if (object_class == NULL) {
Mathieu Chartier590fee92013-09-13 13:46:47 -07003643 self->ClearException();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003644 // Look for a system class
Mathieu Chartier590fee92013-09-13 13:46:47 -07003645 SirtRef<mirror::ClassLoader> null_class_loader(self, nullptr);
3646 object_class = class_linker->FindClass(descriptor.c_str(), null_class_loader);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003647 }
3648 }
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003649 if (object_class == NULL) {
3650 // Failed to get the Class* from reg type.
3651 LOG(WARNING) << "Failed to get Class* from " << object_type;
3652 return NULL;
3653 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003654 uint32_t field_offset = static_cast<uint32_t>(inst->VRegC_22c());
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003655 return FindInstanceFieldWithOffset(object_class, field_offset);
3656}
3657
3658void MethodVerifier::VerifyIGetQuick(const Instruction* inst, const RegType& insn_type,
3659 bool is_primitive) {
3660 DCHECK(Runtime::Current()->IsStarted());
Brian Carlstromea46f952013-07-30 01:26:50 -07003661 mirror::ArtField* field = GetQuickFieldAccess(inst, work_line_.get());
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003662 if (field == NULL) {
3663 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field from " << inst->Name();
3664 return;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003665 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003666 FieldHelper fh(field);
3667 mirror::Class* field_type_class = fh.GetType(false);
3668 const RegType* field_type;
3669 if (field_type_class != nullptr) {
3670 field_type = &reg_types_.FromClass(fh.GetTypeDescriptor(), field_type_class,
3671 field_type_class->CannotBeAssignedFromOtherTypes());
3672 } else {
3673 field_type = &reg_types_.FromDescriptor(field->GetDeclaringClass()->GetClassLoader(),
3674 fh.GetTypeDescriptor(), false);
3675 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003676 const uint32_t vregA = inst->VRegA_22c();
3677 if (is_primitive) {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003678 if (field_type->Equals(insn_type) ||
3679 (field_type->IsFloat() && insn_type.IsIntegralTypes()) ||
3680 (field_type->IsDouble() && insn_type.IsLongTypes())) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003681 // expected that read is of the correct primitive type or that int reads are reading
3682 // floats or long reads are reading doubles
3683 } else {
3684 // This is a global failure rather than a class change failure as the instructions and
3685 // the descriptors for the type should have been consistent within the same file at
3686 // compile time
3687 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003688 << " to be of type '" << insn_type
3689 << "' but found type '" << field_type << "' in get";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003690 return;
3691 }
3692 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003693 if (!insn_type.IsAssignableFrom(*field_type)) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003694 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003695 << " to be compatible with type '" << insn_type
3696 << "' but found type '" << field_type
3697 << "' in get-object";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003698 work_line_->SetRegisterType(vregA, reg_types_.Conflict());
3699 return;
3700 }
3701 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003702 if (!field_type->IsLowHalf()) {
3703 work_line_->SetRegisterType(vregA, *field_type);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003704 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003705 work_line_->SetRegisterTypeWide(vregA, *field_type, field_type->HighHalf(&reg_types_));
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003706 }
3707}
3708
3709void MethodVerifier::VerifyIPutQuick(const Instruction* inst, const RegType& insn_type,
3710 bool is_primitive) {
3711 DCHECK(Runtime::Current()->IsStarted());
Brian Carlstromea46f952013-07-30 01:26:50 -07003712 mirror::ArtField* field = GetQuickFieldAccess(inst, work_line_.get());
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003713 if (field == NULL) {
3714 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field from " << inst->Name();
3715 return;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003716 }
3717 const char* descriptor = FieldHelper(field).GetTypeDescriptor();
3718 mirror::ClassLoader* loader = field->GetDeclaringClass()->GetClassLoader();
3719 const RegType& field_type = reg_types_.FromDescriptor(loader, descriptor, false);
3720 if (field != NULL) {
3721 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
3722 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003723 << " from other class " << GetDeclaringClass();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003724 return;
3725 }
3726 }
3727 const uint32_t vregA = inst->VRegA_22c();
3728 if (is_primitive) {
3729 // Primitive field assignability rules are weaker than regular assignability rules
3730 bool instruction_compatible;
3731 bool value_compatible;
3732 const RegType& value_type = work_line_->GetRegisterType(vregA);
3733 if (field_type.IsIntegralTypes()) {
3734 instruction_compatible = insn_type.IsIntegralTypes();
3735 value_compatible = value_type.IsIntegralTypes();
3736 } else if (field_type.IsFloat()) {
3737 instruction_compatible = insn_type.IsInteger(); // no [is]put-float, so expect [is]put-int
3738 value_compatible = value_type.IsFloatTypes();
3739 } else if (field_type.IsLong()) {
3740 instruction_compatible = insn_type.IsLong();
3741 value_compatible = value_type.IsLongTypes();
3742 } else if (field_type.IsDouble()) {
3743 instruction_compatible = insn_type.IsLong(); // no [is]put-double, so expect [is]put-long
3744 value_compatible = value_type.IsDoubleTypes();
3745 } else {
3746 instruction_compatible = false; // reference field with primitive store
3747 value_compatible = false; // unused
3748 }
3749 if (!instruction_compatible) {
3750 // This is a global failure rather than a class change failure as the instructions and
3751 // the descriptors for the type should have been consistent within the same file at
3752 // compile time
3753 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003754 << " to be of type '" << insn_type
3755 << "' but found type '" << field_type
3756 << "' in put";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003757 return;
3758 }
3759 if (!value_compatible) {
3760 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
3761 << " of type " << value_type
3762 << " but expected " << field_type
3763 << " for store to " << PrettyField(field) << " in put";
3764 return;
3765 }
3766 } else {
3767 if (!insn_type.IsAssignableFrom(field_type)) {
3768 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003769 << " to be compatible with type '" << insn_type
3770 << "' but found type '" << field_type
3771 << "' in put-object";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003772 return;
3773 }
3774 work_line_->VerifyRegisterType(vregA, field_type);
3775 }
3776}
3777
Ian Rogers776ac1f2012-04-13 23:36:36 -07003778bool MethodVerifier::CheckNotMoveException(const uint16_t* insns, int insn_idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003779 if ((insns[insn_idx] & 0xff) == Instruction::MOVE_EXCEPTION) {
jeffhaod5347e02012-03-22 17:25:05 -07003780 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-exception";
Ian Rogersd81871c2011-10-03 13:57:23 -07003781 return false;
3782 }
3783 return true;
3784}
3785
Ian Rogers776ac1f2012-04-13 23:36:36 -07003786bool MethodVerifier::UpdateRegisters(uint32_t next_insn, const RegisterLine* merge_line) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003787 bool changed = true;
3788 RegisterLine* target_line = reg_table_.GetLine(next_insn);
3789 if (!insn_flags_[next_insn].IsVisitedOrChanged()) {
jeffhaobdb76512011-09-07 11:43:16 -07003790 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003791 * We haven't processed this instruction before, and we haven't touched the registers here, so
3792 * there's nothing to "merge". Copy the registers over and mark it as changed. (This is the
3793 * only way a register can transition out of "unknown", so this is not just an optimization.)
jeffhaobdb76512011-09-07 11:43:16 -07003794 */
Ian Rogersb8c78592013-07-25 23:52:52 +00003795 if (!insn_flags_[next_insn].IsReturn()) {
3796 target_line->CopyFromLine(merge_line);
3797 } else {
Jeff Haob24b4a72013-07-31 13:47:31 -07003798 // Verify that the monitor stack is empty on return.
3799 if (!merge_line->VerifyMonitorStackEmpty()) {
3800 return false;
3801 }
Ian Rogersb8c78592013-07-25 23:52:52 +00003802 // For returns we only care about the operand to the return, all other registers are dead.
3803 // Initialize them as conflicts so they don't add to GC and deoptimization information.
3804 const Instruction* ret_inst = Instruction::At(code_item_->insns_ + next_insn);
3805 Instruction::Code opcode = ret_inst->Opcode();
3806 if ((opcode == Instruction::RETURN_VOID) || (opcode == Instruction::RETURN_VOID_BARRIER)) {
3807 target_line->MarkAllRegistersAsConflicts();
3808 } else {
3809 target_line->CopyFromLine(merge_line);
3810 if (opcode == Instruction::RETURN_WIDE) {
3811 target_line->MarkAllRegistersAsConflictsExceptWide(ret_inst->VRegA_11x());
3812 } else {
3813 target_line->MarkAllRegistersAsConflictsExcept(ret_inst->VRegA_11x());
3814 }
3815 }
3816 }
jeffhaobdb76512011-09-07 11:43:16 -07003817 } else {
Brian Carlstrom93c33962013-07-26 10:37:43 -07003818 UniquePtr<RegisterLine> copy(gDebugVerify ?
Ian Rogersd0fbd852013-09-24 18:17:04 -07003819 RegisterLine::Create(target_line->NumRegs(), this) :
Brian Carlstrom93c33962013-07-26 10:37:43 -07003820 NULL);
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003821 if (gDebugVerify) {
3822 copy->CopyFromLine(target_line);
3823 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003824 changed = target_line->MergeRegisters(merge_line);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003825 if (have_pending_hard_failure_) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003826 return false;
jeffhaobdb76512011-09-07 11:43:16 -07003827 }
Ian Rogers2c8a8572011-10-24 17:11:36 -07003828 if (gDebugVerify && changed) {
Elliott Hughes398f64b2012-03-26 18:05:48 -07003829 LogVerifyInfo() << "Merging at [" << reinterpret_cast<void*>(work_insn_idx_) << "]"
Elliott Hughesc073b072012-05-24 19:29:17 -07003830 << " to [" << reinterpret_cast<void*>(next_insn) << "]: " << "\n"
3831 << *copy.get() << " MERGE\n"
3832 << *merge_line << " ==\n"
3833 << *target_line << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07003834 }
3835 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003836 if (changed) {
3837 insn_flags_[next_insn].SetChanged();
jeffhaobdb76512011-09-07 11:43:16 -07003838 }
3839 return true;
3840}
3841
Ian Rogers7b3ddd22013-02-21 15:19:52 -08003842InstructionFlags* MethodVerifier::CurrentInsnFlags() {
Ian Rogers776ac1f2012-04-13 23:36:36 -07003843 return &insn_flags_[work_insn_idx_];
3844}
3845
Ian Rogersad0b3a32012-04-16 14:50:24 -07003846const RegType& MethodVerifier::GetMethodReturnType() {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003847 if (return_type_ == nullptr) {
3848 if (mirror_method_ != NULL) {
3849 MethodHelper mh(mirror_method_);
3850 mirror::Class* return_type_class = mh.GetReturnType();
3851 if (return_type_class != nullptr) {
Mathieu Chartier590fee92013-09-13 13:46:47 -07003852 return_type_ = &reg_types_.FromClass(mh.GetReturnTypeDescriptor(), return_type_class,
3853 return_type_class->CannotBeAssignedFromOtherTypes());
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003854 } else {
3855 Thread* self = Thread::Current();
3856 DCHECK(self->IsExceptionPending());
3857 self->ClearException();
3858 }
3859 }
3860 if (return_type_ == nullptr) {
3861 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
3862 const DexFile::ProtoId& proto_id = dex_file_->GetMethodPrototype(method_id);
3863 uint16_t return_type_idx = proto_id.return_type_idx_;
3864 const char* descriptor = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(return_type_idx));
Mathieu Chartier590fee92013-09-13 13:46:47 -07003865 return_type_ = &reg_types_.FromDescriptor(class_loader_->get(), descriptor, false);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003866 }
3867 }
3868 return *return_type_;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003869}
3870
3871const RegType& MethodVerifier::GetDeclaringClass() {
Ian Rogers637c65b2013-05-31 11:46:00 -07003872 if (declaring_class_ == NULL) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003873 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
Brian Carlstrom93c33962013-07-26 10:37:43 -07003874 const char* descriptor
3875 = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(method_id.class_idx_));
Ian Rogers637c65b2013-05-31 11:46:00 -07003876 if (mirror_method_ != NULL) {
3877 mirror::Class* klass = mirror_method_->GetDeclaringClass();
Ian Rogers04f94f42013-06-10 15:09:26 -07003878 declaring_class_ = &reg_types_.FromClass(descriptor, klass,
3879 klass->CannotBeAssignedFromOtherTypes());
Ian Rogers637c65b2013-05-31 11:46:00 -07003880 } else {
Mathieu Chartier590fee92013-09-13 13:46:47 -07003881 declaring_class_ = &reg_types_.FromDescriptor(class_loader_->get(), descriptor, false);
Ian Rogers637c65b2013-05-31 11:46:00 -07003882 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003883 }
Ian Rogers637c65b2013-05-31 11:46:00 -07003884 return *declaring_class_;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003885}
3886
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003887std::vector<int32_t> MethodVerifier::DescribeVRegs(uint32_t dex_pc) {
3888 RegisterLine* line = reg_table_.GetLine(dex_pc);
3889 std::vector<int32_t> result;
3890 for (size_t i = 0; i < line->NumRegs(); ++i) {
3891 const RegType& type = line->GetRegisterType(i);
3892 if (type.IsConstant()) {
3893 result.push_back(type.IsPreciseConstant() ? kConstant : kImpreciseConstant);
3894 result.push_back(type.ConstantValue());
3895 } else if (type.IsConstantLo()) {
3896 result.push_back(type.IsPreciseConstantLo() ? kConstant : kImpreciseConstant);
3897 result.push_back(type.ConstantValueLo());
3898 } else if (type.IsConstantHi()) {
3899 result.push_back(type.IsPreciseConstantHi() ? kConstant : kImpreciseConstant);
3900 result.push_back(type.ConstantValueHi());
3901 } else if (type.IsIntegralTypes()) {
3902 result.push_back(kIntVReg);
3903 result.push_back(0);
3904 } else if (type.IsFloat()) {
3905 result.push_back(kFloatVReg);
3906 result.push_back(0);
3907 } else if (type.IsLong()) {
3908 result.push_back(kLongLoVReg);
3909 result.push_back(0);
3910 result.push_back(kLongHiVReg);
3911 result.push_back(0);
3912 ++i;
3913 } else if (type.IsDouble()) {
3914 result.push_back(kDoubleLoVReg);
3915 result.push_back(0);
3916 result.push_back(kDoubleHiVReg);
3917 result.push_back(0);
3918 ++i;
3919 } else if (type.IsUndefined() || type.IsConflict() || type.IsHighHalf()) {
3920 result.push_back(kUndefined);
3921 result.push_back(0);
3922 } else {
Ian Rogers7b3ddd22013-02-21 15:19:52 -08003923 CHECK(type.IsNonZeroReferenceTypes());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003924 result.push_back(kReferenceVReg);
3925 result.push_back(0);
3926 }
3927 }
3928 return result;
3929}
3930
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003931void MethodVerifier::Init() {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08003932 art::verifier::RegTypeCache::Init();
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003933}
3934
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003935void MethodVerifier::Shutdown() {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08003936 verifier::RegTypeCache::ShutDown();
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08003937}
jeffhaod1224c72012-02-29 13:43:08 -08003938
Mathieu Chartierc528dba2013-11-26 12:00:11 -08003939void MethodVerifier::VisitRoots(RootVisitor* visitor, void* arg) {
3940 reg_types_.VisitRoots(visitor, arg);
3941}
3942
Ian Rogersd81871c2011-10-03 13:57:23 -07003943} // namespace verifier
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07003944} // namespace art