blob: 91170f0e835379cebe76788d7d27ab429c45163c [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
Mathieu Chartierc645f1d2014-03-06 18:11:53 -080017#include "method_verifier-inl.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"
Brian Carlstrom1f870082011-08-23 16:02:11 -070023#include "class_linker.h"
Vladimir Marko2b5eaa22013-12-13 13:59:30 +000024#include "compiler_callbacks.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070025#include "dex_file-inl.h"
Ian Rogersd0583802013-06-01 10:51:46 -070026#include "dex_instruction-inl.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070027#include "dex_instruction_visitor.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070028#include "gc/accounting/card_table-inl.h"
Ian Rogers2bcb4a42012-11-08 10:39:18 -080029#include "indenter.h"
Ian Rogers84fa0742011-10-25 18:13:30 -070030#include "intern_table.h"
Ian Rogers0571d352011-11-03 19:51:38 -070031#include "leb128.h"
Brian Carlstromea46f952013-07-30 01:26:50 -070032#include "mirror/art_field-inl.h"
33#include "mirror/art_method-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080034#include "mirror/class.h"
35#include "mirror/class-inl.h"
Ian Rogers39ebcb82013-05-30 16:57:23 -070036#include "mirror/dex_cache-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080037#include "mirror/object-inl.h"
38#include "mirror/object_array-inl.h"
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080039#include "object_utils.h"
Ian Rogers39ebcb82013-05-30 16:57:23 -070040#include "register_line-inl.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070041#include "runtime.h"
Mathieu Chartier590fee92013-09-13 13:46:47 -070042#include "scoped_thread_state_change.h"
Mathieu Chartierc645f1d2014-03-06 18:11:53 -080043#include "sirt_ref-inl.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 Rogersef7d42f2014-01-06 12:55:46 -080088MethodVerifier::FailureKind MethodVerifier::VerifyClass(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(),
Ian Rogers9bc54402014-04-17 16:40:01 -0700364 mh.GetCodeItem(), m->GetDexMethodIndex(), m, m->GetAccessFlags(), true,
Mathieu Chartier590fee92013-09-13 13:46:47 -0700365 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) {
Ian Rogers9bc54402014-04-17 16:40:01 -0700378 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200379 }
380 RegisterLine* register_line = reg_table_.GetLine(dex_pc);
381 if (register_line == NULL) {
Ian Rogers9bc54402014-04-17 16:40:01 -0700382 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200383 }
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(),
Andreas Gampe63981562014-04-17 12:28:43 -0700395 mh.GetCodeItem(), m->GetDexMethodIndex(), m, m->GetAccessFlags(), true,
Mathieu Chartier590fee92013-09-13 13:46:47 -0700396 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 */
Ian Rogersef7d42f2014-01-06 12:55:46 -0800840 if ((reinterpret_cast<uintptr_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 */
Ian Rogersef7d42f2014-01-06 12:55:46 -0800944 if ((reinterpret_cast<uintptr_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 {
Sebastien Hertz2ed76f92014-04-22 17:11:08 +02001179 const RegType& reg_type = ResolveClassAndCheckAccess(iterator.GetTypeIdx());
1180 if (!reg_type.IsNonZeroReferenceTypes()) {
1181 DCHECK(HasFailures());
1182 return false;
1183 }
Ian Rogers84fa0742011-10-25 18:13:30 -07001184 reg_line->SetRegisterType(arg_start + cur_arg, reg_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001185 }
1186 break;
1187 case 'Z':
1188 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Boolean());
1189 break;
1190 case 'C':
1191 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Char());
1192 break;
1193 case 'B':
1194 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Byte());
1195 break;
1196 case 'I':
1197 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Integer());
1198 break;
1199 case 'S':
1200 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Short());
1201 break;
1202 case 'F':
1203 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Float());
1204 break;
1205 case 'J':
1206 case 'D': {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001207 const RegType& lo_half = descriptor[0] == 'J' ? reg_types_.LongLo() : reg_types_.DoubleLo();
1208 const RegType& hi_half = descriptor[0] == 'J' ? reg_types_.LongHi() : reg_types_.DoubleHi();
1209 reg_line->SetRegisterTypeWide(arg_start + cur_arg, lo_half, hi_half);
Ian Rogersd81871c2011-10-03 13:57:23 -07001210 cur_arg++;
1211 break;
1212 }
1213 default:
Brian Carlstrom93c33962013-07-26 10:37:43 -07001214 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected signature type char '"
1215 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001216 return false;
1217 }
1218 cur_arg++;
1219 }
1220 if (cur_arg != expected_args) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001221 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1222 << " arguments, found " << cur_arg;
Ian Rogersd81871c2011-10-03 13:57:23 -07001223 return false;
1224 }
1225 const char* descriptor = dex_file_->GetReturnTypeDescriptor(proto_id);
1226 // Validate return type. We don't do the type lookup; just want to make sure that it has the right
1227 // format. Only major difference from the method argument format is that 'V' is supported.
1228 bool result;
1229 if (IsPrimitiveDescriptor(descriptor[0]) || descriptor[0] == 'V') {
1230 result = descriptor[1] == '\0';
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001231 } else if (descriptor[0] == '[') { // single/multi-dimensional array of object/primitive
Ian Rogersd81871c2011-10-03 13:57:23 -07001232 size_t i = 0;
1233 do {
1234 i++;
1235 } while (descriptor[i] == '['); // process leading [
1236 if (descriptor[i] == 'L') { // object array
1237 do {
1238 i++; // find closing ;
1239 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1240 result = descriptor[i] == ';';
1241 } else { // primitive array
1242 result = IsPrimitiveDescriptor(descriptor[i]) && descriptor[i + 1] == '\0';
1243 }
1244 } else if (descriptor[0] == 'L') {
1245 // could be more thorough here, but shouldn't be required
1246 size_t i = 0;
1247 do {
1248 i++;
1249 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1250 result = descriptor[i] == ';';
1251 } else {
1252 result = false;
1253 }
1254 if (!result) {
jeffhaod5347e02012-03-22 17:25:05 -07001255 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected char in return type descriptor '"
1256 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001257 }
1258 return result;
jeffhaobdb76512011-09-07 11:43:16 -07001259}
1260
Ian Rogers776ac1f2012-04-13 23:36:36 -07001261bool MethodVerifier::CodeFlowVerifyMethod() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001262 const uint16_t* insns = code_item_->insns_;
1263 const uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaoba5ebb92011-08-25 17:24:37 -07001264
jeffhaobdb76512011-09-07 11:43:16 -07001265 /* Begin by marking the first instruction as "changed". */
Ian Rogersd81871c2011-10-03 13:57:23 -07001266 insn_flags_[0].SetChanged();
1267 uint32_t start_guess = 0;
jeffhaoba5ebb92011-08-25 17:24:37 -07001268
jeffhaobdb76512011-09-07 11:43:16 -07001269 /* Continue until no instructions are marked "changed". */
1270 while (true) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001271 // Find the first marked one. Use "start_guess" as a way to find one quickly.
1272 uint32_t insn_idx = start_guess;
1273 for (; insn_idx < insns_size; insn_idx++) {
1274 if (insn_flags_[insn_idx].IsChanged())
jeffhaobdb76512011-09-07 11:43:16 -07001275 break;
1276 }
jeffhaobdb76512011-09-07 11:43:16 -07001277 if (insn_idx == insns_size) {
1278 if (start_guess != 0) {
1279 /* try again, starting from the top */
1280 start_guess = 0;
1281 continue;
1282 } else {
1283 /* all flags are clear */
1284 break;
1285 }
1286 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001287 // We carry the working set of registers from instruction to instruction. If this address can
1288 // be the target of a branch (or throw) instruction, or if we're skipping around chasing
1289 // "changed" flags, we need to load the set of registers from the table.
1290 // Because we always prefer to continue on to the next instruction, we should never have a
1291 // situation where we have a stray "changed" flag set on an instruction that isn't a branch
1292 // target.
1293 work_insn_idx_ = insn_idx;
1294 if (insn_flags_[insn_idx].IsBranchTarget()) {
1295 work_line_->CopyFromLine(reg_table_.GetLine(insn_idx));
jeffhaobdb76512011-09-07 11:43:16 -07001296 } else {
1297#ifndef NDEBUG
1298 /*
1299 * Sanity check: retrieve the stored register line (assuming
1300 * a full table) and make sure it actually matches.
1301 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001302 RegisterLine* register_line = reg_table_.GetLine(insn_idx);
1303 if (register_line != NULL) {
1304 if (work_line_->CompareLine(register_line) != 0) {
1305 Dump(std::cout);
1306 std::cout << info_messages_.str();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001307 LOG(FATAL) << "work_line diverged in " << PrettyMethod(dex_method_idx_, *dex_file_)
Elliott Hughesc073b072012-05-24 19:29:17 -07001308 << "@" << reinterpret_cast<void*>(work_insn_idx_) << "\n"
1309 << " work_line=" << *work_line_ << "\n"
Elliott Hughes398f64b2012-03-26 18:05:48 -07001310 << " expected=" << *register_line;
Ian Rogersd81871c2011-10-03 13:57:23 -07001311 }
jeffhaobdb76512011-09-07 11:43:16 -07001312 }
1313#endif
1314 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001315 if (!CodeFlowVerifyInstruction(&start_guess)) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001316 std::string prepend(PrettyMethod(dex_method_idx_, *dex_file_));
Ian Rogersad0b3a32012-04-16 14:50:24 -07001317 prepend += " failed to verify: ";
1318 PrependToLastFailMessage(prepend);
jeffhaoba5ebb92011-08-25 17:24:37 -07001319 return false;
1320 }
jeffhaobdb76512011-09-07 11:43:16 -07001321 /* Clear "changed" and mark as visited. */
Ian Rogersd81871c2011-10-03 13:57:23 -07001322 insn_flags_[insn_idx].SetVisited();
1323 insn_flags_[insn_idx].ClearChanged();
jeffhaobdb76512011-09-07 11:43:16 -07001324 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001325
Ian Rogers1c849e52012-06-28 14:00:33 -07001326 if (gDebugVerify) {
jeffhaobdb76512011-09-07 11:43:16 -07001327 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001328 * Scan for dead code. There's nothing "evil" about dead code
jeffhaobdb76512011-09-07 11:43:16 -07001329 * (besides the wasted space), but it indicates a flaw somewhere
1330 * down the line, possibly in the verifier.
1331 *
1332 * If we've substituted "always throw" instructions into the stream,
1333 * we are almost certainly going to have some dead code.
1334 */
1335 int dead_start = -1;
Ian Rogersd81871c2011-10-03 13:57:23 -07001336 uint32_t insn_idx = 0;
1337 for (; insn_idx < insns_size; insn_idx += insn_flags_[insn_idx].GetLengthInCodeUnits()) {
jeffhaobdb76512011-09-07 11:43:16 -07001338 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001339 * Switch-statement data doesn't get "visited" by scanner. It
jeffhaobdb76512011-09-07 11:43:16 -07001340 * may or may not be preceded by a padding NOP (for alignment).
1341 */
1342 if (insns[insn_idx] == Instruction::kPackedSwitchSignature ||
1343 insns[insn_idx] == Instruction::kSparseSwitchSignature ||
1344 insns[insn_idx] == Instruction::kArrayDataSignature ||
Elliott Hughes380aaa72012-07-09 14:33:15 -07001345 (insns[insn_idx] == Instruction::NOP && (insn_idx + 1 < insns_size) &&
jeffhaobdb76512011-09-07 11:43:16 -07001346 (insns[insn_idx + 1] == Instruction::kPackedSwitchSignature ||
1347 insns[insn_idx + 1] == Instruction::kSparseSwitchSignature ||
1348 insns[insn_idx + 1] == Instruction::kArrayDataSignature))) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001349 insn_flags_[insn_idx].SetVisited();
jeffhaobdb76512011-09-07 11:43:16 -07001350 }
1351
Ian Rogersd81871c2011-10-03 13:57:23 -07001352 if (!insn_flags_[insn_idx].IsVisited()) {
jeffhaobdb76512011-09-07 11:43:16 -07001353 if (dead_start < 0)
1354 dead_start = insn_idx;
1355 } else if (dead_start >= 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001356 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start)
1357 << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaobdb76512011-09-07 11:43:16 -07001358 dead_start = -1;
1359 }
1360 }
1361 if (dead_start >= 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001362 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start)
1363 << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaoba5ebb92011-08-25 17:24:37 -07001364 }
Ian Rogersc9e463c2013-06-05 16:52:26 -07001365 // To dump the state of the verify after a method, do something like:
1366 // if (PrettyMethod(dex_method_idx_, *dex_file_) ==
1367 // "boolean java.lang.String.equals(java.lang.Object)") {
1368 // LOG(INFO) << info_messages_.str();
1369 // }
jeffhaoba5ebb92011-08-25 17:24:37 -07001370 }
jeffhaobdb76512011-09-07 11:43:16 -07001371 return true;
1372}
1373
Ian Rogers776ac1f2012-04-13 23:36:36 -07001374bool MethodVerifier::CodeFlowVerifyInstruction(uint32_t* start_guess) {
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001375 // If we're doing FindLocksAtDexPc, check whether we're at the dex pc we care about.
1376 // We want the state _before_ the instruction, for the case where the dex pc we're
1377 // interested in is itself a monitor-enter instruction (which is a likely place
1378 // for a thread to be suspended).
1379 if (monitor_enter_dex_pcs_ != NULL && work_insn_idx_ == interesting_dex_pc_) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001380 monitor_enter_dex_pcs_->clear(); // The new work line is more accurate than the previous one.
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001381 for (size_t i = 0; i < work_line_->GetMonitorEnterCount(); ++i) {
1382 monitor_enter_dex_pcs_->push_back(work_line_->GetMonitorEnterDexPc(i));
1383 }
1384 }
1385
jeffhaobdb76512011-09-07 11:43:16 -07001386 /*
1387 * Once we finish decoding the instruction, we need to figure out where
jeffhaod1f0fde2011-09-08 17:25:33 -07001388 * we can go from here. There are three possible ways to transfer
jeffhaobdb76512011-09-07 11:43:16 -07001389 * control to another statement:
1390 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001391 * (1) Continue to the next instruction. Applies to all but
jeffhaobdb76512011-09-07 11:43:16 -07001392 * unconditional branches, method returns, and exception throws.
jeffhaod1f0fde2011-09-08 17:25:33 -07001393 * (2) Branch to one or more possible locations. Applies to branches
jeffhaobdb76512011-09-07 11:43:16 -07001394 * and switch statements.
jeffhaod1f0fde2011-09-08 17:25:33 -07001395 * (3) Exception handlers. Applies to any instruction that can
jeffhaobdb76512011-09-07 11:43:16 -07001396 * throw an exception that is handled by an encompassing "try"
1397 * block.
1398 *
1399 * We can also return, in which case there is no successor instruction
1400 * from this point.
1401 *
Elliott Hughesadb8c672012-03-06 16:49:32 -08001402 * The behavior can be determined from the opcode flags.
jeffhaobdb76512011-09-07 11:43:16 -07001403 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001404 const uint16_t* insns = code_item_->insns_ + work_insn_idx_;
1405 const Instruction* inst = Instruction::At(insns);
Ian Rogersa75a0132012-09-28 11:41:42 -07001406 int opcode_flags = Instruction::FlagsOf(inst->Opcode());
jeffhaobdb76512011-09-07 11:43:16 -07001407
jeffhaobdb76512011-09-07 11:43:16 -07001408 int32_t branch_target = 0;
jeffhaobdb76512011-09-07 11:43:16 -07001409 bool just_set_result = false;
Ian Rogers2c8a8572011-10-24 17:11:36 -07001410 if (gDebugVerify) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001411 // Generate processing back trace to debug verifier
Elliott Hughesc073b072012-05-24 19:29:17 -07001412 LogVerifyInfo() << "Processing " << inst->DumpString(dex_file_) << "\n"
1413 << *work_line_.get() << "\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001414 }
jeffhaobdb76512011-09-07 11:43:16 -07001415
1416 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001417 * Make a copy of the previous register state. If the instruction
jeffhaobdb76512011-09-07 11:43:16 -07001418 * can throw an exception, we will copy/merge this into the "catch"
1419 * address rather than work_line, because we don't want the result
1420 * from the "successful" code path (e.g. a check-cast that "improves"
1421 * a type) to be visible to the exception handler.
1422 */
Ian Rogers776ac1f2012-04-13 23:36:36 -07001423 if ((opcode_flags & Instruction::kThrow) != 0 && CurrentInsnFlags()->IsInTry()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001424 saved_line_->CopyFromLine(work_line_.get());
jeffhaobdb76512011-09-07 11:43:16 -07001425 } else {
1426#ifndef NDEBUG
Ian Rogersd81871c2011-10-03 13:57:23 -07001427 saved_line_->FillWithGarbage();
jeffhaobdb76512011-09-07 11:43:16 -07001428#endif
1429 }
1430
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07001431
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001432 // We need to ensure the work line is consistent while performing validation. When we spot a
1433 // peephole pattern we compute a new line for either the fallthrough instruction or the
1434 // branch target.
1435 UniquePtr<RegisterLine> branch_line;
1436 UniquePtr<RegisterLine> fallthrough_line;
1437
Sebastien Hertz849600b2013-12-20 10:28:08 +01001438 // We need precise constant types only for deoptimization which happens at runtime.
1439 const bool need_precise_constant = !Runtime::Current()->IsCompiler();
1440
Sebastien Hertz5243e912013-05-21 10:55:07 +02001441 switch (inst->Opcode()) {
jeffhaobdb76512011-09-07 11:43:16 -07001442 case Instruction::NOP:
1443 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001444 * A "pure" NOP has no effect on anything. Data tables start with
jeffhaobdb76512011-09-07 11:43:16 -07001445 * a signature that looks like a NOP; if we see one of these in
1446 * the course of executing code then we have a problem.
1447 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001448 if (inst->VRegA_10x() != 0) {
jeffhaod5347e02012-03-22 17:25:05 -07001449 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "encountered data table in instruction stream";
jeffhaobdb76512011-09-07 11:43:16 -07001450 }
1451 break;
1452
1453 case Instruction::MOVE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001454 work_line_->CopyRegister1(inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategory1nr);
1455 break;
jeffhaobdb76512011-09-07 11:43:16 -07001456 case Instruction::MOVE_FROM16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001457 work_line_->CopyRegister1(inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategory1nr);
1458 break;
jeffhaobdb76512011-09-07 11:43:16 -07001459 case Instruction::MOVE_16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001460 work_line_->CopyRegister1(inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategory1nr);
jeffhaobdb76512011-09-07 11:43:16 -07001461 break;
1462 case Instruction::MOVE_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001463 work_line_->CopyRegister2(inst->VRegA_12x(), inst->VRegB_12x());
1464 break;
jeffhaobdb76512011-09-07 11:43:16 -07001465 case Instruction::MOVE_WIDE_FROM16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001466 work_line_->CopyRegister2(inst->VRegA_22x(), inst->VRegB_22x());
1467 break;
jeffhaobdb76512011-09-07 11:43:16 -07001468 case Instruction::MOVE_WIDE_16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001469 work_line_->CopyRegister2(inst->VRegA_32x(), inst->VRegB_32x());
jeffhaobdb76512011-09-07 11:43:16 -07001470 break;
1471 case Instruction::MOVE_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001472 work_line_->CopyRegister1(inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategoryRef);
1473 break;
jeffhaobdb76512011-09-07 11:43:16 -07001474 case Instruction::MOVE_OBJECT_FROM16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001475 work_line_->CopyRegister1(inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategoryRef);
1476 break;
jeffhaobdb76512011-09-07 11:43:16 -07001477 case Instruction::MOVE_OBJECT_16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001478 work_line_->CopyRegister1(inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategoryRef);
jeffhaobdb76512011-09-07 11:43:16 -07001479 break;
1480
1481 /*
1482 * The move-result instructions copy data out of a "pseudo-register"
jeffhaod1f0fde2011-09-08 17:25:33 -07001483 * with the results from the last method invocation. In practice we
jeffhaobdb76512011-09-07 11:43:16 -07001484 * might want to hold the result in an actual CPU register, so the
1485 * Dalvik spec requires that these only appear immediately after an
1486 * invoke or filled-new-array.
1487 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001488 * These calls invalidate the "result" register. (This is now
jeffhaobdb76512011-09-07 11:43:16 -07001489 * redundant with the reset done below, but it can make the debug info
1490 * easier to read in some cases.)
1491 */
1492 case Instruction::MOVE_RESULT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001493 work_line_->CopyResultRegister1(inst->VRegA_11x(), false);
jeffhaobdb76512011-09-07 11:43:16 -07001494 break;
1495 case Instruction::MOVE_RESULT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001496 work_line_->CopyResultRegister2(inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07001497 break;
1498 case Instruction::MOVE_RESULT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001499 work_line_->CopyResultRegister1(inst->VRegA_11x(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001500 break;
1501
Ian Rogersd81871c2011-10-03 13:57:23 -07001502 case Instruction::MOVE_EXCEPTION: {
jeffhaobdb76512011-09-07 11:43:16 -07001503 /*
jeffhao60f83e32012-02-13 17:16:30 -08001504 * This statement can only appear as the first instruction in an exception handler. We verify
1505 * that as part of extracting the exception type from the catch block list.
jeffhaobdb76512011-09-07 11:43:16 -07001506 */
Ian Rogers28ad40d2011-10-27 15:19:26 -07001507 const RegType& res_type = GetCaughtExceptionType();
Sebastien Hertz5243e912013-05-21 10:55:07 +02001508 work_line_->SetRegisterType(inst->VRegA_11x(), res_type);
jeffhaobdb76512011-09-07 11:43:16 -07001509 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001510 }
jeffhaobdb76512011-09-07 11:43:16 -07001511 case Instruction::RETURN_VOID:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001512 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
1513 if (!GetMethodReturnType().IsConflict()) {
jeffhaod5347e02012-03-22 17:25:05 -07001514 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001515 }
jeffhaobdb76512011-09-07 11:43:16 -07001516 }
1517 break;
1518 case Instruction::RETURN:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001519 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
jeffhaobdb76512011-09-07 11:43:16 -07001520 /* check the method signature */
Ian Rogersd81871c2011-10-03 13:57:23 -07001521 const RegType& return_type = GetMethodReturnType();
1522 if (!return_type.IsCategory1Types()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001523 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected non-category 1 return type "
1524 << return_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001525 } else {
1526 // Compilers may generate synthetic functions that write byte values into boolean fields.
1527 // Also, it may use integer values for boolean, byte, short, and character return types.
Sebastien Hertz5243e912013-05-21 10:55:07 +02001528 const uint32_t vregA = inst->VRegA_11x();
1529 const RegType& src_type = work_line_->GetRegisterType(vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07001530 bool use_src = ((return_type.IsBoolean() && src_type.IsByte()) ||
1531 ((return_type.IsBoolean() || return_type.IsByte() ||
1532 return_type.IsShort() || return_type.IsChar()) &&
1533 src_type.IsInteger()));
1534 /* check the register contents */
Ian Rogersad0b3a32012-04-16 14:50:24 -07001535 bool success =
Sebastien Hertz5243e912013-05-21 10:55:07 +02001536 work_line_->VerifyRegisterType(vregA, use_src ? src_type : return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001537 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001538 AppendToLastFailMessage(StringPrintf(" return-1nr on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07001539 }
jeffhaobdb76512011-09-07 11:43:16 -07001540 }
1541 }
1542 break;
1543 case Instruction::RETURN_WIDE:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001544 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
jeffhaobdb76512011-09-07 11:43:16 -07001545 /* check the method signature */
Ian Rogersd81871c2011-10-03 13:57:23 -07001546 const RegType& return_type = GetMethodReturnType();
1547 if (!return_type.IsCategory2Types()) {
jeffhaod5347e02012-03-22 17:25:05 -07001548 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-wide not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001549 } else {
1550 /* check the register contents */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001551 const uint32_t vregA = inst->VRegA_11x();
1552 bool success = work_line_->VerifyRegisterType(vregA, return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001553 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001554 AppendToLastFailMessage(StringPrintf(" return-wide on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07001555 }
jeffhaobdb76512011-09-07 11:43:16 -07001556 }
1557 }
1558 break;
1559 case Instruction::RETURN_OBJECT:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001560 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001561 const RegType& return_type = GetMethodReturnType();
1562 if (!return_type.IsReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001563 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-object not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001564 } else {
1565 /* return_type is the *expected* return type, not register value */
1566 DCHECK(!return_type.IsZero());
1567 DCHECK(!return_type.IsUninitializedReference());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001568 const uint32_t vregA = inst->VRegA_11x();
1569 const RegType& reg_type = work_line_->GetRegisterType(vregA);
Ian Rogers9074b992011-10-26 17:41:55 -07001570 // Disallow returning uninitialized values and verify that the reference in vAA is an
1571 // instance of the "return_type"
1572 if (reg_type.IsUninitializedTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001573 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "returning uninitialized object '"
1574 << reg_type << "'";
Ian Rogers9074b992011-10-26 17:41:55 -07001575 } else if (!return_type.IsAssignableFrom(reg_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07001576 if (reg_type.IsUnresolvedTypes() || return_type.IsUnresolvedTypes()) {
1577 Fail(VERIFY_ERROR_NO_CLASS) << " can't resolve returned type '" << return_type
1578 << "' or '" << reg_type << "'";
1579 } else {
1580 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning '" << reg_type
1581 << "', but expected from declaration '" << return_type << "'";
1582 }
jeffhaobdb76512011-09-07 11:43:16 -07001583 }
1584 }
1585 }
1586 break;
1587
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001588 /* could be boolean, int, float, or a null reference */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001589 case Instruction::CONST_4: {
1590 int32_t val = static_cast<int32_t>(inst->VRegB_11n() << 28) >> 28;
Sebastien Hertz849600b2013-12-20 10:28:08 +01001591 work_line_->SetRegisterType(inst->VRegA_11n(),
1592 DetermineCat1Constant(val, need_precise_constant));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001593 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02001594 }
1595 case Instruction::CONST_16: {
1596 int16_t val = static_cast<int16_t>(inst->VRegB_21s());
Sebastien Hertz849600b2013-12-20 10:28:08 +01001597 work_line_->SetRegisterType(inst->VRegA_21s(),
1598 DetermineCat1Constant(val, need_precise_constant));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001599 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02001600 }
Sebastien Hertz849600b2013-12-20 10:28:08 +01001601 case Instruction::CONST: {
1602 int32_t val = inst->VRegB_31i();
Sebastien Hertz5243e912013-05-21 10:55:07 +02001603 work_line_->SetRegisterType(inst->VRegA_31i(),
Sebastien Hertz849600b2013-12-20 10:28:08 +01001604 DetermineCat1Constant(val, need_precise_constant));
jeffhaobdb76512011-09-07 11:43:16 -07001605 break;
Sebastien Hertz849600b2013-12-20 10:28:08 +01001606 }
1607 case Instruction::CONST_HIGH16: {
1608 int32_t val = static_cast<int32_t>(inst->VRegB_21h() << 16);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001609 work_line_->SetRegisterType(inst->VRegA_21h(),
Sebastien Hertz849600b2013-12-20 10:28:08 +01001610 DetermineCat1Constant(val, need_precise_constant));
jeffhaobdb76512011-09-07 11:43:16 -07001611 break;
Sebastien Hertz849600b2013-12-20 10:28:08 +01001612 }
jeffhaobdb76512011-09-07 11:43:16 -07001613 /* could be long or double; resolved upon use */
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001614 case Instruction::CONST_WIDE_16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001615 int64_t val = static_cast<int16_t>(inst->VRegB_21s());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001616 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1617 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001618 work_line_->SetRegisterTypeWide(inst->VRegA_21s(), lo, hi);
jeffhaobdb76512011-09-07 11:43:16 -07001619 break;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001620 }
1621 case Instruction::CONST_WIDE_32: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001622 int64_t val = static_cast<int32_t>(inst->VRegB_31i());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001623 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1624 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001625 work_line_->SetRegisterTypeWide(inst->VRegA_31i(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001626 break;
1627 }
1628 case Instruction::CONST_WIDE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001629 int64_t val = inst->VRegB_51l();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001630 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1631 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001632 work_line_->SetRegisterTypeWide(inst->VRegA_51l(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001633 break;
1634 }
1635 case Instruction::CONST_WIDE_HIGH16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001636 int64_t val = static_cast<uint64_t>(inst->VRegB_21h()) << 48;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001637 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1638 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001639 work_line_->SetRegisterTypeWide(inst->VRegA_21h(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001640 break;
1641 }
jeffhaobdb76512011-09-07 11:43:16 -07001642 case Instruction::CONST_STRING:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001643 work_line_->SetRegisterType(inst->VRegA_21c(), reg_types_.JavaLangString());
1644 break;
jeffhaobdb76512011-09-07 11:43:16 -07001645 case Instruction::CONST_STRING_JUMBO:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001646 work_line_->SetRegisterType(inst->VRegA_31c(), reg_types_.JavaLangString());
jeffhaobdb76512011-09-07 11:43:16 -07001647 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001648 case Instruction::CONST_CLASS: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001649 // Get type from instruction if unresolved then we need an access check
1650 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Sebastien Hertz5243e912013-05-21 10:55:07 +02001651 const RegType& res_type = ResolveClassAndCheckAccess(inst->VRegB_21c());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001652 // Register holds class, ie its type is class, on error it will hold Conflict.
Sebastien Hertz5243e912013-05-21 10:55:07 +02001653 work_line_->SetRegisterType(inst->VRegA_21c(),
Ian Rogersb4903572012-10-11 11:52:56 -07001654 res_type.IsConflict() ? res_type
1655 : reg_types_.JavaLangClass(true));
jeffhaobdb76512011-09-07 11:43:16 -07001656 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001657 }
jeffhaobdb76512011-09-07 11:43:16 -07001658 case Instruction::MONITOR_ENTER:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001659 work_line_->PushMonitor(inst->VRegA_11x(), work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07001660 break;
1661 case Instruction::MONITOR_EXIT:
1662 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001663 * monitor-exit instructions are odd. They can throw exceptions,
jeffhaobdb76512011-09-07 11:43:16 -07001664 * but when they do they act as if they succeeded and the PC is
jeffhaod1f0fde2011-09-08 17:25:33 -07001665 * pointing to the following instruction. (This behavior goes back
jeffhaobdb76512011-09-07 11:43:16 -07001666 * to the need to handle asynchronous exceptions, a now-deprecated
1667 * feature that Dalvik doesn't support.)
1668 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001669 * In practice we don't need to worry about this. The only
jeffhaobdb76512011-09-07 11:43:16 -07001670 * exceptions that can be thrown from monitor-exit are for a
jeffhaod1f0fde2011-09-08 17:25:33 -07001671 * null reference and -exit without a matching -enter. If the
jeffhaobdb76512011-09-07 11:43:16 -07001672 * structured locking checks are working, the former would have
1673 * failed on the -enter instruction, and the latter is impossible.
1674 *
1675 * This is fortunate, because issue 3221411 prevents us from
1676 * chasing the "can throw" path when monitor verification is
jeffhaod1f0fde2011-09-08 17:25:33 -07001677 * enabled. If we can fully verify the locking we can ignore
jeffhaobdb76512011-09-07 11:43:16 -07001678 * some catch blocks (which will show up as "dead" code when
1679 * we skip them here); if we can't, then the code path could be
1680 * "live" so we still need to check it.
1681 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001682 opcode_flags &= ~Instruction::kThrow;
Sebastien Hertz5243e912013-05-21 10:55:07 +02001683 work_line_->PopMonitor(inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07001684 break;
1685
Ian Rogers28ad40d2011-10-27 15:19:26 -07001686 case Instruction::CHECK_CAST:
Ian Rogersd81871c2011-10-03 13:57:23 -07001687 case Instruction::INSTANCE_OF: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001688 /*
1689 * If this instruction succeeds, we will "downcast" register vA to the type in vB. (This
1690 * could be a "upcast" -- not expected, so we don't try to address it.)
1691 *
1692 * If it fails, an exception is thrown, which we deal with later by ignoring the update to
Elliott Hughesadb8c672012-03-06 16:49:32 -08001693 * dec_insn.vA when branching to a handler.
Ian Rogers28ad40d2011-10-27 15:19:26 -07001694 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001695 const bool is_checkcast = (inst->Opcode() == Instruction::CHECK_CAST);
1696 const uint32_t type_idx = (is_checkcast) ? inst->VRegB_21c() : inst->VRegC_22c();
1697 const RegType& res_type = ResolveClassAndCheckAccess(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001698 if (res_type.IsConflict()) {
1699 DCHECK_NE(failures_.size(), 0U);
1700 if (!is_checkcast) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001701 work_line_->SetRegisterType(inst->VRegA_22c(), reg_types_.Boolean());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001702 }
1703 break; // bad class
Ian Rogers9f1ab122011-12-12 08:52:43 -08001704 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001705 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Sebastien Hertz5243e912013-05-21 10:55:07 +02001706 uint32_t orig_type_reg = (is_checkcast) ? inst->VRegA_21c() : inst->VRegB_22c();
1707 const RegType& orig_type = work_line_->GetRegisterType(orig_type_reg);
Ian Rogers28ad40d2011-10-27 15:19:26 -07001708 if (!res_type.IsNonZeroReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001709 if (is_checkcast) {
1710 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on unexpected class " << res_type;
1711 } else {
1712 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on unexpected class " << res_type;
1713 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001714 } else if (!orig_type.IsReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001715 if (is_checkcast) {
1716 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on non-reference in v" << orig_type_reg;
1717 } else {
1718 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on non-reference in v" << orig_type_reg;
1719 }
jeffhao2a8a90e2011-09-26 14:25:31 -07001720 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001721 if (is_checkcast) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001722 work_line_->SetRegisterType(inst->VRegA_21c(), res_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001723 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001724 work_line_->SetRegisterType(inst->VRegA_22c(), reg_types_.Boolean());
jeffhaobdb76512011-09-07 11:43:16 -07001725 }
jeffhaobdb76512011-09-07 11:43:16 -07001726 }
jeffhao2a8a90e2011-09-26 14:25:31 -07001727 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001728 }
1729 case Instruction::ARRAY_LENGTH: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001730 const RegType& res_type = work_line_->GetRegisterType(inst->VRegB_12x());
Ian Rogers28ad40d2011-10-27 15:19:26 -07001731 if (res_type.IsReferenceTypes()) {
Ian Rogers89310de2012-02-01 13:47:30 -08001732 if (!res_type.IsArrayTypes() && !res_type.IsZero()) { // ie not an array or null
jeffhaod5347e02012-03-22 17:25:05 -07001733 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001734 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001735 work_line_->SetRegisterType(inst->VRegA_12x(), reg_types_.Integer());
Ian Rogersd81871c2011-10-03 13:57:23 -07001736 }
1737 }
1738 break;
1739 }
1740 case Instruction::NEW_INSTANCE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001741 const RegType& res_type = ResolveClassAndCheckAccess(inst->VRegB_21c());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001742 if (res_type.IsConflict()) {
1743 DCHECK_NE(failures_.size(), 0U);
1744 break; // bad class
jeffhao8cd6dda2012-02-22 10:15:34 -08001745 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001746 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
1747 // can't create an instance of an interface or abstract class */
1748 if (!res_type.IsInstantiableTypes()) {
1749 Fail(VERIFY_ERROR_INSTANTIATION)
1750 << "new-instance on primitive, interface or abstract class" << res_type;
Ian Rogers08f753d2012-08-24 14:35:25 -07001751 // Soft failure so carry on to set register type.
Ian Rogersd81871c2011-10-03 13:57:23 -07001752 }
Ian Rogers08f753d2012-08-24 14:35:25 -07001753 const RegType& uninit_type = reg_types_.Uninitialized(res_type, work_insn_idx_);
1754 // Any registers holding previous allocations from this address that have not yet been
1755 // initialized must be marked invalid.
1756 work_line_->MarkUninitRefsAsInvalid(uninit_type);
1757 // add the new uninitialized reference to the register state
Sebastien Hertz5243e912013-05-21 10:55:07 +02001758 work_line_->SetRegisterType(inst->VRegA_21c(), uninit_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001759 break;
1760 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08001761 case Instruction::NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001762 VerifyNewArray(inst, false, false);
jeffhaobdb76512011-09-07 11:43:16 -07001763 break;
1764 case Instruction::FILLED_NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001765 VerifyNewArray(inst, true, false);
Ian Rogers0c4a5062012-02-03 15:18:59 -08001766 just_set_result = true; // Filled new array sets result register
jeffhaobdb76512011-09-07 11:43:16 -07001767 break;
Ian Rogers0c4a5062012-02-03 15:18:59 -08001768 case Instruction::FILLED_NEW_ARRAY_RANGE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001769 VerifyNewArray(inst, true, true);
Ian Rogers0c4a5062012-02-03 15:18:59 -08001770 just_set_result = true; // Filled new array range sets result register
1771 break;
jeffhaobdb76512011-09-07 11:43:16 -07001772 case Instruction::CMPL_FLOAT:
1773 case Instruction::CMPG_FLOAT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001774 if (!work_line_->VerifyRegisterType(inst->VRegB_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08001775 break;
1776 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02001777 if (!work_line_->VerifyRegisterType(inst->VRegC_23x(), reg_types_.Float())) {
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::CMPL_DOUBLE:
1783 case Instruction::CMPG_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001784 if (!work_line_->VerifyRegisterTypeWide(inst->VRegB_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001785 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001786 break;
1787 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02001788 if (!work_line_->VerifyRegisterTypeWide(inst->VRegC_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001789 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001790 break;
1791 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02001792 work_line_->SetRegisterType(inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001793 break;
1794 case Instruction::CMP_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001795 if (!work_line_->VerifyRegisterTypeWide(inst->VRegB_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001796 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001797 break;
1798 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02001799 if (!work_line_->VerifyRegisterTypeWide(inst->VRegC_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001800 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001801 break;
1802 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02001803 work_line_->SetRegisterType(inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001804 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001805 case Instruction::THROW: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001806 const RegType& res_type = work_line_->GetRegisterType(inst->VRegA_11x());
Ian Rogersb4903572012-10-11 11:52:56 -07001807 if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(res_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07001808 Fail(res_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS : VERIFY_ERROR_BAD_CLASS_SOFT)
1809 << "thrown class " << res_type << " not instanceof Throwable";
jeffhaobdb76512011-09-07 11:43:16 -07001810 }
1811 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001812 }
jeffhaobdb76512011-09-07 11:43:16 -07001813 case Instruction::GOTO:
1814 case Instruction::GOTO_16:
1815 case Instruction::GOTO_32:
1816 /* no effect on or use of registers */
1817 break;
1818
1819 case Instruction::PACKED_SWITCH:
1820 case Instruction::SPARSE_SWITCH:
1821 /* verify that vAA is an integer, or can be converted to one */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001822 work_line_->VerifyRegisterType(inst->VRegA_31t(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001823 break;
1824
Ian Rogersd81871c2011-10-03 13:57:23 -07001825 case Instruction::FILL_ARRAY_DATA: {
1826 /* Similar to the verification done for APUT */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001827 const RegType& array_type = work_line_->GetRegisterType(inst->VRegA_31t());
Ian Rogers89310de2012-02-01 13:47:30 -08001828 /* array_type can be null if the reg type is Zero */
1829 if (!array_type.IsZero()) {
jeffhao457cc512012-02-02 16:55:13 -08001830 if (!array_type.IsArrayTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001831 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with array type "
1832 << array_type;
Ian Rogers89310de2012-02-01 13:47:30 -08001833 } else {
Mathieu Chartier590fee92013-09-13 13:46:47 -07001834 const RegType& component_type = reg_types_.GetComponentType(array_type,
1835 class_loader_->get());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001836 DCHECK(!component_type.IsConflict());
jeffhao457cc512012-02-02 16:55:13 -08001837 if (component_type.IsNonZeroReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001838 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with component type "
1839 << component_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001840 } else {
jeffhao457cc512012-02-02 16:55:13 -08001841 // Now verify if the element width in the table matches the element width declared in
1842 // the array
1843 const uint16_t* array_data = insns + (insns[1] | (((int32_t) insns[2]) << 16));
1844 if (array_data[0] != Instruction::kArrayDataSignature) {
jeffhaod5347e02012-03-22 17:25:05 -07001845 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid magic for array-data";
jeffhao457cc512012-02-02 16:55:13 -08001846 } else {
1847 size_t elem_width = Primitive::ComponentSize(component_type.GetPrimitiveType());
1848 // Since we don't compress the data in Dex, expect to see equal width of data stored
1849 // in the table and expected from the array class.
1850 if (array_data[1] != elem_width) {
jeffhaod5347e02012-03-22 17:25:05 -07001851 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-data size mismatch (" << array_data[1]
1852 << " vs " << elem_width << ")";
jeffhao457cc512012-02-02 16:55:13 -08001853 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001854 }
1855 }
jeffhaobdb76512011-09-07 11:43:16 -07001856 }
1857 }
1858 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001859 }
jeffhaobdb76512011-09-07 11:43:16 -07001860 case Instruction::IF_EQ:
Ian Rogersd81871c2011-10-03 13:57:23 -07001861 case Instruction::IF_NE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001862 const RegType& reg_type1 = work_line_->GetRegisterType(inst->VRegA_22t());
1863 const RegType& reg_type2 = work_line_->GetRegisterType(inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07001864 bool mismatch = false;
1865 if (reg_type1.IsZero()) { // zero then integral or reference expected
1866 mismatch = !reg_type2.IsReferenceTypes() && !reg_type2.IsIntegralTypes();
1867 } else if (reg_type1.IsReferenceTypes()) { // both references?
1868 mismatch = !reg_type2.IsReferenceTypes();
1869 } else { // both integral?
1870 mismatch = !reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes();
1871 }
1872 if (mismatch) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001873 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to if-eq/if-ne (" << reg_type1 << ","
1874 << reg_type2 << ") must both be references or 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_LT:
1879 case Instruction::IF_GE:
1880 case Instruction::IF_GT:
Ian Rogersd81871c2011-10-03 13:57:23 -07001881 case Instruction::IF_LE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001882 const RegType& reg_type1 = work_line_->GetRegisterType(inst->VRegA_22t());
1883 const RegType& reg_type2 = work_line_->GetRegisterType(inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07001884 if (!reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001885 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to 'if' (" << reg_type1 << ","
1886 << reg_type2 << ") must be integral";
jeffhaobdb76512011-09-07 11:43:16 -07001887 }
1888 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001889 }
jeffhaobdb76512011-09-07 11:43:16 -07001890 case Instruction::IF_EQZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07001891 case Instruction::IF_NEZ: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001892 const RegType& reg_type = work_line_->GetRegisterType(inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07001893 if (!reg_type.IsReferenceTypes() && !reg_type.IsIntegralTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001894 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
1895 << " unexpected as arg to if-eqz/if-nez";
Ian Rogersd81871c2011-10-03 13:57:23 -07001896 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001897
1898 // Find previous instruction - its existence is a precondition to peephole optimization.
Ian Rogers9b360392013-06-06 14:45:07 -07001899 uint32_t instance_of_idx = 0;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001900 if (0 != work_insn_idx_) {
Ian Rogers9b360392013-06-06 14:45:07 -07001901 instance_of_idx = work_insn_idx_ - 1;
Brian Carlstromdf629502013-07-17 22:39:56 -07001902 while (0 != instance_of_idx && !insn_flags_[instance_of_idx].IsOpcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07001903 instance_of_idx--;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001904 }
Ian Rogers9b360392013-06-06 14:45:07 -07001905 CHECK(insn_flags_[instance_of_idx].IsOpcode());
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001906 } else {
1907 break;
1908 }
1909
Ian Rogers9b360392013-06-06 14:45:07 -07001910 const Instruction* instance_of_inst = Instruction::At(code_item_->insns_ + instance_of_idx);
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001911
1912 /* Check for peep-hole pattern of:
1913 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07001914 * instance-of vX, vY, T;
1915 * ifXXX vX, label ;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001916 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07001917 * label:
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001918 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07001919 * and sharpen the type of vY to be type T.
1920 * Note, this pattern can't be if:
1921 * - if there are other branches to this branch,
1922 * - when vX == vY.
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001923 */
Ian Rogersfae370a2013-06-05 08:33:27 -07001924 if (!CurrentInsnFlags()->IsBranchTarget() &&
Ian Rogers9b360392013-06-06 14:45:07 -07001925 (Instruction::INSTANCE_OF == instance_of_inst->Opcode()) &&
1926 (inst->VRegA_21t() == instance_of_inst->VRegA_22c()) &&
1927 (instance_of_inst->VRegA_22c() != instance_of_inst->VRegB_22c())) {
Ian Rogersfae370a2013-06-05 08:33:27 -07001928 // Check that the we are not attempting conversion to interface types,
1929 // which is not done because of the multiple inheritance implications.
Jeff Haoc642ec82013-09-04 16:11:55 -07001930 // Also don't change the type if it would result in an upcast.
1931 const RegType& orig_type = work_line_->GetRegisterType(instance_of_inst->VRegB_22c());
Ian Rogers9b360392013-06-06 14:45:07 -07001932 const RegType& cast_type = ResolveClassAndCheckAccess(instance_of_inst->VRegC_22c());
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001933
Jeff Haoa3faaf42013-09-03 19:07:00 -07001934 if (!cast_type.IsUnresolvedTypes() && !orig_type.IsUnresolvedTypes() &&
1935 !cast_type.GetClass()->IsInterface() && !cast_type.IsAssignableFrom(orig_type)) {
Ian Rogersd0fbd852013-09-24 18:17:04 -07001936 RegisterLine* update_line = RegisterLine::Create(code_item_->registers_size_, this);
Ian Rogersfae370a2013-06-05 08:33:27 -07001937 if (inst->Opcode() == Instruction::IF_EQZ) {
Ian Rogers9b360392013-06-06 14:45:07 -07001938 fallthrough_line.reset(update_line);
Ian Rogersfae370a2013-06-05 08:33:27 -07001939 } else {
Ian Rogers9b360392013-06-06 14:45:07 -07001940 branch_line.reset(update_line);
1941 }
1942 update_line->CopyFromLine(work_line_.get());
1943 update_line->SetRegisterType(instance_of_inst->VRegB_22c(), cast_type);
1944 if (!insn_flags_[instance_of_idx].IsBranchTarget() && 0 != instance_of_idx) {
1945 // See if instance-of was preceded by a move-object operation, common due to the small
1946 // register encoding space of instance-of, and propagate type information to the source
1947 // of the move-object.
1948 uint32_t move_idx = instance_of_idx - 1;
Brian Carlstromdf629502013-07-17 22:39:56 -07001949 while (0 != move_idx && !insn_flags_[move_idx].IsOpcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07001950 move_idx--;
1951 }
1952 CHECK(insn_flags_[move_idx].IsOpcode());
1953 const Instruction* move_inst = Instruction::At(code_item_->insns_ + move_idx);
1954 switch (move_inst->Opcode()) {
1955 case Instruction::MOVE_OBJECT:
1956 if (move_inst->VRegA_12x() == instance_of_inst->VRegB_22c()) {
1957 update_line->SetRegisterType(move_inst->VRegB_12x(), cast_type);
1958 }
1959 break;
1960 case Instruction::MOVE_OBJECT_FROM16:
1961 if (move_inst->VRegA_22x() == instance_of_inst->VRegB_22c()) {
1962 update_line->SetRegisterType(move_inst->VRegB_22x(), cast_type);
1963 }
1964 break;
1965 case Instruction::MOVE_OBJECT_16:
1966 if (move_inst->VRegA_32x() == instance_of_inst->VRegB_22c()) {
1967 update_line->SetRegisterType(move_inst->VRegB_32x(), cast_type);
1968 }
1969 break;
1970 default:
1971 break;
1972 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001973 }
1974 }
1975 }
1976
jeffhaobdb76512011-09-07 11:43:16 -07001977 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001978 }
jeffhaobdb76512011-09-07 11:43:16 -07001979 case Instruction::IF_LTZ:
1980 case Instruction::IF_GEZ:
1981 case Instruction::IF_GTZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07001982 case Instruction::IF_LEZ: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001983 const RegType& reg_type = work_line_->GetRegisterType(inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07001984 if (!reg_type.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001985 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
1986 << " unexpected as arg to if-ltz/if-gez/if-gtz/if-lez";
Ian Rogersd81871c2011-10-03 13:57:23 -07001987 }
jeffhaobdb76512011-09-07 11:43:16 -07001988 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001989 }
jeffhaobdb76512011-09-07 11:43:16 -07001990 case Instruction::AGET_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001991 VerifyAGet(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001992 break;
jeffhaobdb76512011-09-07 11:43:16 -07001993 case Instruction::AGET_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001994 VerifyAGet(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001995 break;
jeffhaobdb76512011-09-07 11:43:16 -07001996 case Instruction::AGET_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001997 VerifyAGet(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001998 break;
jeffhaobdb76512011-09-07 11:43:16 -07001999 case Instruction::AGET_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002000 VerifyAGet(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002001 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002002 case Instruction::AGET:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002003 VerifyAGet(inst, reg_types_.Integer(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002004 break;
jeffhaobdb76512011-09-07 11:43:16 -07002005 case Instruction::AGET_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002006 VerifyAGet(inst, reg_types_.LongLo(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002007 break;
2008 case Instruction::AGET_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002009 VerifyAGet(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07002010 break;
2011
Ian Rogersd81871c2011-10-03 13:57:23 -07002012 case Instruction::APUT_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002013 VerifyAPut(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002014 break;
2015 case Instruction::APUT_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002016 VerifyAPut(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002017 break;
2018 case Instruction::APUT_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002019 VerifyAPut(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002020 break;
2021 case Instruction::APUT_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002022 VerifyAPut(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002023 break;
2024 case Instruction::APUT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002025 VerifyAPut(inst, reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002026 break;
2027 case Instruction::APUT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002028 VerifyAPut(inst, reg_types_.LongLo(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002029 break;
2030 case Instruction::APUT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002031 VerifyAPut(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07002032 break;
2033
jeffhaobdb76512011-09-07 11:43:16 -07002034 case Instruction::IGET_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002035 VerifyISGet(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002036 break;
jeffhaobdb76512011-09-07 11:43:16 -07002037 case Instruction::IGET_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002038 VerifyISGet(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002039 break;
jeffhaobdb76512011-09-07 11:43:16 -07002040 case Instruction::IGET_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002041 VerifyISGet(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002042 break;
jeffhaobdb76512011-09-07 11:43:16 -07002043 case Instruction::IGET_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002044 VerifyISGet(inst, reg_types_.Short(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002045 break;
2046 case Instruction::IGET:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002047 VerifyISGet(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002048 break;
2049 case Instruction::IGET_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002050 VerifyISGet(inst, reg_types_.LongLo(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002051 break;
2052 case Instruction::IGET_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002053 VerifyISGet(inst, reg_types_.JavaLangObject(false), false, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002054 break;
jeffhaobdb76512011-09-07 11:43:16 -07002055
Ian Rogersd81871c2011-10-03 13:57:23 -07002056 case Instruction::IPUT_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002057 VerifyISPut(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002058 break;
2059 case Instruction::IPUT_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002060 VerifyISPut(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002061 break;
2062 case Instruction::IPUT_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002063 VerifyISPut(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002064 break;
2065 case Instruction::IPUT_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002066 VerifyISPut(inst, reg_types_.Short(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002067 break;
2068 case Instruction::IPUT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002069 VerifyISPut(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002070 break;
2071 case Instruction::IPUT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002072 VerifyISPut(inst, reg_types_.LongLo(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002073 break;
jeffhaobdb76512011-09-07 11:43:16 -07002074 case Instruction::IPUT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002075 VerifyISPut(inst, reg_types_.JavaLangObject(false), false, false);
jeffhaobdb76512011-09-07 11:43:16 -07002076 break;
2077
jeffhaobdb76512011-09-07 11:43:16 -07002078 case Instruction::SGET_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002079 VerifyISGet(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002080 break;
jeffhaobdb76512011-09-07 11:43:16 -07002081 case Instruction::SGET_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002082 VerifyISGet(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002083 break;
jeffhaobdb76512011-09-07 11:43:16 -07002084 case Instruction::SGET_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002085 VerifyISGet(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002086 break;
jeffhaobdb76512011-09-07 11:43:16 -07002087 case Instruction::SGET_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002088 VerifyISGet(inst, reg_types_.Short(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002089 break;
2090 case Instruction::SGET:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002091 VerifyISGet(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002092 break;
2093 case Instruction::SGET_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002094 VerifyISGet(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002095 break;
2096 case Instruction::SGET_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002097 VerifyISGet(inst, reg_types_.JavaLangObject(false), false, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002098 break;
2099
2100 case Instruction::SPUT_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002101 VerifyISPut(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002102 break;
2103 case Instruction::SPUT_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002104 VerifyISPut(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002105 break;
2106 case Instruction::SPUT_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002107 VerifyISPut(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002108 break;
2109 case Instruction::SPUT_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002110 VerifyISPut(inst, reg_types_.Short(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002111 break;
2112 case Instruction::SPUT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002113 VerifyISPut(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002114 break;
2115 case Instruction::SPUT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002116 VerifyISPut(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002117 break;
2118 case Instruction::SPUT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002119 VerifyISPut(inst, reg_types_.JavaLangObject(false), false, true);
jeffhaobdb76512011-09-07 11:43:16 -07002120 break;
2121
2122 case Instruction::INVOKE_VIRTUAL:
2123 case Instruction::INVOKE_VIRTUAL_RANGE:
2124 case Instruction::INVOKE_SUPER:
Ian Rogersd81871c2011-10-03 13:57:23 -07002125 case Instruction::INVOKE_SUPER_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002126 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE ||
2127 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
2128 bool is_super = (inst->Opcode() == Instruction::INVOKE_SUPER ||
2129 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
Brian Carlstromea46f952013-07-30 01:26:50 -07002130 mirror::ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_VIRTUAL,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002131 is_range, is_super);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002132 const RegType* return_type = nullptr;
2133 if (called_method != nullptr) {
2134 MethodHelper mh(called_method);
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08002135 mirror::Class* return_type_class = mh.GetReturnType(can_load_classes_);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002136 if (return_type_class != nullptr) {
2137 return_type = &reg_types_.FromClass(mh.GetReturnTypeDescriptor(), return_type_class,
2138 return_type_class->CannotBeAssignedFromOtherTypes());
2139 } else {
2140 Thread* self = Thread::Current();
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08002141 DCHECK(!can_load_classes_ || self->IsExceptionPending());
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002142 self->ClearException();
2143 }
2144 }
2145 if (return_type == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002146 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002147 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2148 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002149 const char* descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002150 return_type = &reg_types_.FromDescriptor(class_loader_->get(), descriptor, false);
jeffhaobdb76512011-09-07 11:43:16 -07002151 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002152 if (!return_type->IsLowHalf()) {
2153 work_line_->SetResultRegisterType(*return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002154 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002155 work_line_->SetResultRegisterTypeWide(*return_type, return_type->HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002156 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002157 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002158 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002159 }
jeffhaobdb76512011-09-07 11:43:16 -07002160 case Instruction::INVOKE_DIRECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002161 case Instruction::INVOKE_DIRECT_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002162 bool is_range = (inst->Opcode() == Instruction::INVOKE_DIRECT_RANGE);
Brian Carlstromea46f952013-07-30 01:26:50 -07002163 mirror::ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_DIRECT,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002164 is_range, false);
Ian Rogers46685432012-06-03 22:26:43 -07002165 const char* return_type_descriptor;
2166 bool is_constructor;
2167 if (called_method == NULL) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002168 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogers46685432012-06-03 22:26:43 -07002169 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
Ian Rogersdfb325e2013-10-30 01:00:44 -07002170 is_constructor = strcmp("<init>", dex_file_->StringDataByIdx(method_id.name_idx_)) == 0;
Ian Rogers46685432012-06-03 22:26:43 -07002171 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
2172 return_type_descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2173 } else {
2174 is_constructor = called_method->IsConstructor();
2175 return_type_descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
2176 }
2177 if (is_constructor) {
jeffhaobdb76512011-09-07 11:43:16 -07002178 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002179 * Some additional checks when calling a constructor. We know from the invocation arg check
2180 * that the "this" argument is an instance of called_method->klass. Now we further restrict
2181 * that to require that called_method->klass is the same as this->klass or this->super,
2182 * allowing the latter only if the "this" argument is the same as the "this" argument to
2183 * this method (which implies that we're in a constructor ourselves).
jeffhaobdb76512011-09-07 11:43:16 -07002184 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002185 const RegType& this_type = work_line_->GetInvocationThis(inst, is_range);
jeffhaob57e9522012-04-26 18:08:21 -07002186 if (this_type.IsConflict()) // failure.
2187 break;
jeffhaobdb76512011-09-07 11:43:16 -07002188
jeffhaob57e9522012-04-26 18:08:21 -07002189 /* no null refs allowed (?) */
2190 if (this_type.IsZero()) {
2191 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unable to initialize null ref";
2192 break;
jeffhao2a8a90e2011-09-26 14:25:31 -07002193 }
jeffhaob57e9522012-04-26 18:08:21 -07002194
2195 /* must be in same class or in superclass */
Ian Rogers46685432012-06-03 22:26:43 -07002196 // const RegType& this_super_klass = this_type.GetSuperClass(&reg_types_);
2197 // TODO: re-enable constructor type verification
2198 // if (this_super_klass.IsConflict()) {
jeffhaob57e9522012-04-26 18:08:21 -07002199 // Unknown super class, fail so we re-check at runtime.
Ian Rogers46685432012-06-03 22:26:43 -07002200 // Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "super class unknown for '" << this_type << "'";
2201 // break;
2202 // }
jeffhaob57e9522012-04-26 18:08:21 -07002203
2204 /* arg must be an uninitialized reference */
2205 if (!this_type.IsUninitializedTypes()) {
2206 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Expected initialization on uninitialized reference "
2207 << this_type;
2208 break;
2209 }
2210
2211 /*
2212 * Replace the uninitialized reference with an initialized one. We need to do this for all
2213 * registers that have the same object instance in them, not just the "this" register.
2214 */
2215 work_line_->MarkRefsAsInitialized(this_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002216 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07002217 const RegType& return_type = reg_types_.FromDescriptor(class_loader_->get(),
2218 return_type_descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002219 if (!return_type.IsLowHalf()) {
2220 work_line_->SetResultRegisterType(return_type);
2221 } else {
2222 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2223 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002224 just_set_result = true;
2225 break;
2226 }
2227 case Instruction::INVOKE_STATIC:
2228 case Instruction::INVOKE_STATIC_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002229 bool is_range = (inst->Opcode() == Instruction::INVOKE_STATIC_RANGE);
Brian Carlstromea46f952013-07-30 01:26:50 -07002230 mirror::ArtMethod* called_method = VerifyInvocationArgs(inst,
Brian Carlstrom93c33962013-07-26 10:37:43 -07002231 METHOD_STATIC,
2232 is_range,
2233 false);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002234 const char* descriptor;
2235 if (called_method == NULL) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002236 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002237 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2238 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Mathieu Chartier590fee92013-09-13 13:46:47 -07002239 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002240 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002241 descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002242 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07002243 const RegType& return_type = reg_types_.FromDescriptor(class_loader_->get(), descriptor,
2244 false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002245 if (!return_type.IsLowHalf()) {
2246 work_line_->SetResultRegisterType(return_type);
2247 } else {
2248 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2249 }
jeffhaobdb76512011-09-07 11:43:16 -07002250 just_set_result = true;
2251 }
2252 break;
jeffhaobdb76512011-09-07 11:43:16 -07002253 case Instruction::INVOKE_INTERFACE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002254 case Instruction::INVOKE_INTERFACE_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002255 bool is_range = (inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE);
Brian Carlstromea46f952013-07-30 01:26:50 -07002256 mirror::ArtMethod* abs_method = VerifyInvocationArgs(inst,
Brian Carlstrom93c33962013-07-26 10:37:43 -07002257 METHOD_INTERFACE,
2258 is_range,
2259 false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002260 if (abs_method != NULL) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002261 mirror::Class* called_interface = abs_method->GetDeclaringClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002262 if (!called_interface->IsInterface() && !called_interface->IsObjectClass()) {
2263 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected interface class in invoke-interface '"
2264 << PrettyMethod(abs_method) << "'";
2265 break;
Ian Rogers28ad40d2011-10-27 15:19:26 -07002266 }
Ian Rogers0d604842012-04-16 14:50:24 -07002267 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002268 /* Get the type of the "this" arg, which should either be a sub-interface of called
2269 * interface or Object (see comments in RegType::JoinClass).
2270 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002271 const RegType& this_type = work_line_->GetInvocationThis(inst, is_range);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002272 if (this_type.IsZero()) {
2273 /* null pointer always passes (and always fails at runtime) */
2274 } else {
2275 if (this_type.IsUninitializedTypes()) {
2276 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface call on uninitialized object "
2277 << this_type;
2278 break;
2279 }
2280 // In the past we have tried to assert that "called_interface" is assignable
2281 // from "this_type.GetClass()", however, as we do an imprecise Join
2282 // (RegType::JoinClass) we don't have full information on what interfaces are
2283 // implemented by "this_type". For example, two classes may implement the same
2284 // interfaces and have a common parent that doesn't implement the interface. The
2285 // join will set "this_type" to the parent class and a test that this implements
2286 // the interface will incorrectly fail.
2287 }
2288 /*
2289 * We don't have an object instance, so we can't find the concrete method. However, all of
2290 * the type information is in the abstract method, so we're good.
2291 */
2292 const char* descriptor;
2293 if (abs_method == NULL) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002294 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002295 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2296 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
2297 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2298 } else {
2299 descriptor = MethodHelper(abs_method).GetReturnTypeDescriptor();
2300 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07002301 const RegType& return_type = reg_types_.FromDescriptor(class_loader_->get(), descriptor,
2302 false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002303 if (!return_type.IsLowHalf()) {
2304 work_line_->SetResultRegisterType(return_type);
2305 } else {
2306 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2307 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002308 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002309 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002310 }
jeffhaobdb76512011-09-07 11:43:16 -07002311 case Instruction::NEG_INT:
2312 case Instruction::NOT_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002313 work_line_->CheckUnaryOp(inst, reg_types_.Integer(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002314 break;
2315 case Instruction::NEG_LONG:
2316 case Instruction::NOT_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002317 work_line_->CheckUnaryOpWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002318 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002319 break;
2320 case Instruction::NEG_FLOAT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002321 work_line_->CheckUnaryOp(inst, reg_types_.Float(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002322 break;
2323 case Instruction::NEG_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002324 work_line_->CheckUnaryOpWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002325 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002326 break;
2327 case Instruction::INT_TO_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002328 work_line_->CheckUnaryOpToWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002329 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002330 break;
2331 case Instruction::INT_TO_FLOAT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002332 work_line_->CheckUnaryOp(inst, reg_types_.Float(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002333 break;
2334 case Instruction::INT_TO_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002335 work_line_->CheckUnaryOpToWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002336 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002337 break;
2338 case Instruction::LONG_TO_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002339 work_line_->CheckUnaryOpFromWide(inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002340 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002341 break;
2342 case Instruction::LONG_TO_FLOAT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002343 work_line_->CheckUnaryOpFromWide(inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002344 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002345 break;
2346 case Instruction::LONG_TO_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002347 work_line_->CheckUnaryOpWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002348 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002349 break;
2350 case Instruction::FLOAT_TO_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002351 work_line_->CheckUnaryOp(inst, reg_types_.Integer(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002352 break;
2353 case Instruction::FLOAT_TO_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002354 work_line_->CheckUnaryOpToWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002355 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002356 break;
2357 case Instruction::FLOAT_TO_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002358 work_line_->CheckUnaryOpToWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002359 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002360 break;
2361 case Instruction::DOUBLE_TO_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002362 work_line_->CheckUnaryOpFromWide(inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002363 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002364 break;
2365 case Instruction::DOUBLE_TO_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002366 work_line_->CheckUnaryOpWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002367 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002368 break;
2369 case Instruction::DOUBLE_TO_FLOAT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002370 work_line_->CheckUnaryOpFromWide(inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002371 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002372 break;
2373 case Instruction::INT_TO_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002374 work_line_->CheckUnaryOp(inst, reg_types_.Byte(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002375 break;
2376 case Instruction::INT_TO_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002377 work_line_->CheckUnaryOp(inst, reg_types_.Char(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002378 break;
2379 case Instruction::INT_TO_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002380 work_line_->CheckUnaryOp(inst, reg_types_.Short(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002381 break;
2382
2383 case Instruction::ADD_INT:
2384 case Instruction::SUB_INT:
2385 case Instruction::MUL_INT:
2386 case Instruction::REM_INT:
2387 case Instruction::DIV_INT:
2388 case Instruction::SHL_INT:
2389 case Instruction::SHR_INT:
2390 case Instruction::USHR_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002391 work_line_->CheckBinaryOp(inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002392 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002393 break;
2394 case Instruction::AND_INT:
2395 case Instruction::OR_INT:
2396 case Instruction::XOR_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002397 work_line_->CheckBinaryOp(inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002398 reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002399 break;
2400 case Instruction::ADD_LONG:
2401 case Instruction::SUB_LONG:
2402 case Instruction::MUL_LONG:
2403 case Instruction::DIV_LONG:
2404 case Instruction::REM_LONG:
2405 case Instruction::AND_LONG:
2406 case Instruction::OR_LONG:
2407 case Instruction::XOR_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002408 work_line_->CheckBinaryOpWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002409 reg_types_.LongLo(), reg_types_.LongHi(),
2410 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002411 break;
2412 case Instruction::SHL_LONG:
2413 case Instruction::SHR_LONG:
2414 case Instruction::USHR_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002415 /* shift distance is Int, making these different from other binary operations */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002416 work_line_->CheckBinaryOpWideShift(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002417 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002418 break;
2419 case Instruction::ADD_FLOAT:
2420 case Instruction::SUB_FLOAT:
2421 case Instruction::MUL_FLOAT:
2422 case Instruction::DIV_FLOAT:
2423 case Instruction::REM_FLOAT:
Brian Carlstrom93c33962013-07-26 10:37:43 -07002424 work_line_->CheckBinaryOp(inst,
2425 reg_types_.Float(),
2426 reg_types_.Float(),
2427 reg_types_.Float(),
2428 false);
jeffhaobdb76512011-09-07 11:43:16 -07002429 break;
2430 case Instruction::ADD_DOUBLE:
2431 case Instruction::SUB_DOUBLE:
2432 case Instruction::MUL_DOUBLE:
2433 case Instruction::DIV_DOUBLE:
2434 case Instruction::REM_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002435 work_line_->CheckBinaryOpWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002436 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2437 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002438 break;
2439 case Instruction::ADD_INT_2ADDR:
2440 case Instruction::SUB_INT_2ADDR:
2441 case Instruction::MUL_INT_2ADDR:
2442 case Instruction::REM_INT_2ADDR:
2443 case Instruction::SHL_INT_2ADDR:
2444 case Instruction::SHR_INT_2ADDR:
2445 case Instruction::USHR_INT_2ADDR:
Brian Carlstrom93c33962013-07-26 10:37:43 -07002446 work_line_->CheckBinaryOp2addr(inst,
2447 reg_types_.Integer(),
2448 reg_types_.Integer(),
2449 reg_types_.Integer(),
2450 false);
jeffhaobdb76512011-09-07 11:43:16 -07002451 break;
2452 case Instruction::AND_INT_2ADDR:
2453 case Instruction::OR_INT_2ADDR:
2454 case Instruction::XOR_INT_2ADDR:
Brian Carlstrom93c33962013-07-26 10:37:43 -07002455 work_line_->CheckBinaryOp2addr(inst,
2456 reg_types_.Integer(),
2457 reg_types_.Integer(),
2458 reg_types_.Integer(),
2459 true);
jeffhaobdb76512011-09-07 11:43:16 -07002460 break;
2461 case Instruction::DIV_INT_2ADDR:
Brian Carlstrom93c33962013-07-26 10:37:43 -07002462 work_line_->CheckBinaryOp2addr(inst,
2463 reg_types_.Integer(),
2464 reg_types_.Integer(),
2465 reg_types_.Integer(),
2466 false);
jeffhaobdb76512011-09-07 11:43:16 -07002467 break;
2468 case Instruction::ADD_LONG_2ADDR:
2469 case Instruction::SUB_LONG_2ADDR:
2470 case Instruction::MUL_LONG_2ADDR:
2471 case Instruction::DIV_LONG_2ADDR:
2472 case Instruction::REM_LONG_2ADDR:
2473 case Instruction::AND_LONG_2ADDR:
2474 case Instruction::OR_LONG_2ADDR:
2475 case Instruction::XOR_LONG_2ADDR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002476 work_line_->CheckBinaryOp2addrWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002477 reg_types_.LongLo(), reg_types_.LongHi(),
2478 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002479 break;
2480 case Instruction::SHL_LONG_2ADDR:
2481 case Instruction::SHR_LONG_2ADDR:
2482 case Instruction::USHR_LONG_2ADDR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002483 work_line_->CheckBinaryOp2addrWideShift(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002484 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002485 break;
2486 case Instruction::ADD_FLOAT_2ADDR:
2487 case Instruction::SUB_FLOAT_2ADDR:
2488 case Instruction::MUL_FLOAT_2ADDR:
2489 case Instruction::DIV_FLOAT_2ADDR:
2490 case Instruction::REM_FLOAT_2ADDR:
Brian Carlstrom93c33962013-07-26 10:37:43 -07002491 work_line_->CheckBinaryOp2addr(inst,
2492 reg_types_.Float(),
2493 reg_types_.Float(),
2494 reg_types_.Float(),
2495 false);
jeffhaobdb76512011-09-07 11:43:16 -07002496 break;
2497 case Instruction::ADD_DOUBLE_2ADDR:
2498 case Instruction::SUB_DOUBLE_2ADDR:
2499 case Instruction::MUL_DOUBLE_2ADDR:
2500 case Instruction::DIV_DOUBLE_2ADDR:
2501 case Instruction::REM_DOUBLE_2ADDR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002502 work_line_->CheckBinaryOp2addrWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002503 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2504 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002505 break;
2506 case Instruction::ADD_INT_LIT16:
2507 case Instruction::RSUB_INT:
2508 case Instruction::MUL_INT_LIT16:
2509 case Instruction::DIV_INT_LIT16:
2510 case Instruction::REM_INT_LIT16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002511 work_line_->CheckLiteralOp(inst, reg_types_.Integer(), reg_types_.Integer(), false, true);
jeffhaobdb76512011-09-07 11:43:16 -07002512 break;
2513 case Instruction::AND_INT_LIT16:
2514 case Instruction::OR_INT_LIT16:
2515 case Instruction::XOR_INT_LIT16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002516 work_line_->CheckLiteralOp(inst, reg_types_.Integer(), reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002517 break;
2518 case Instruction::ADD_INT_LIT8:
2519 case Instruction::RSUB_INT_LIT8:
2520 case Instruction::MUL_INT_LIT8:
2521 case Instruction::DIV_INT_LIT8:
2522 case Instruction::REM_INT_LIT8:
2523 case Instruction::SHL_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002524 case Instruction::SHR_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002525 case Instruction::USHR_INT_LIT8:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002526 work_line_->CheckLiteralOp(inst, reg_types_.Integer(), reg_types_.Integer(), false, false);
jeffhaobdb76512011-09-07 11:43:16 -07002527 break;
2528 case Instruction::AND_INT_LIT8:
2529 case Instruction::OR_INT_LIT8:
2530 case Instruction::XOR_INT_LIT8:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002531 work_line_->CheckLiteralOp(inst, reg_types_.Integer(), reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002532 break;
2533
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002534 // Special instructions.
Sebastien Hertzcc10e0e2013-06-28 14:24:48 +02002535 case Instruction::RETURN_VOID_BARRIER:
Ian Rogers9fc16eb2013-07-31 14:49:16 -07002536 if (!IsConstructor() || IsStatic()) {
Sebastien Hertzcc10e0e2013-06-28 14:24:48 +02002537 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void-barrier not expected";
2538 }
2539 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002540 // Note: the following instructions encode offsets derived from class linking.
2541 // As such they use Class*/Field*/AbstractMethod* as these offsets only have
2542 // meaning if the class linking and resolution were successful.
2543 case Instruction::IGET_QUICK:
2544 VerifyIGetQuick(inst, reg_types_.Integer(), true);
2545 break;
2546 case Instruction::IGET_WIDE_QUICK:
2547 VerifyIGetQuick(inst, reg_types_.LongLo(), true);
2548 break;
2549 case Instruction::IGET_OBJECT_QUICK:
2550 VerifyIGetQuick(inst, reg_types_.JavaLangObject(false), false);
2551 break;
2552 case Instruction::IPUT_QUICK:
2553 VerifyIPutQuick(inst, reg_types_.Integer(), true);
2554 break;
2555 case Instruction::IPUT_WIDE_QUICK:
2556 VerifyIPutQuick(inst, reg_types_.LongLo(), true);
2557 break;
2558 case Instruction::IPUT_OBJECT_QUICK:
2559 VerifyIPutQuick(inst, reg_types_.JavaLangObject(false), false);
2560 break;
2561 case Instruction::INVOKE_VIRTUAL_QUICK:
2562 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: {
2563 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
Brian Carlstromea46f952013-07-30 01:26:50 -07002564 mirror::ArtMethod* called_method = VerifyInvokeVirtualQuickArgs(inst, is_range);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002565 if (called_method != NULL) {
2566 const char* descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
Mathieu Chartier590fee92013-09-13 13:46:47 -07002567 const RegType& return_type = reg_types_.FromDescriptor(class_loader_->get(), descriptor,
2568 false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002569 if (!return_type.IsLowHalf()) {
2570 work_line_->SetResultRegisterType(return_type);
2571 } else {
2572 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2573 }
2574 just_set_result = true;
2575 }
2576 break;
2577 }
2578
Ian Rogersd81871c2011-10-03 13:57:23 -07002579 /* These should never appear during verification. */
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002580 case Instruction::UNUSED_3E:
2581 case Instruction::UNUSED_3F:
2582 case Instruction::UNUSED_40:
2583 case Instruction::UNUSED_41:
2584 case Instruction::UNUSED_42:
2585 case Instruction::UNUSED_43:
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002586 case Instruction::UNUSED_79:
2587 case Instruction::UNUSED_7A:
2588 case Instruction::UNUSED_EB:
2589 case Instruction::UNUSED_EC:
jeffhao9a4f0032012-08-30 16:17:40 -07002590 case Instruction::UNUSED_ED:
jeffhaobdb76512011-09-07 11:43:16 -07002591 case Instruction::UNUSED_EE:
2592 case Instruction::UNUSED_EF:
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002593 case Instruction::UNUSED_F0:
2594 case Instruction::UNUSED_F1:
jeffhaobdb76512011-09-07 11:43:16 -07002595 case Instruction::UNUSED_F2:
2596 case Instruction::UNUSED_F3:
2597 case Instruction::UNUSED_F4:
2598 case Instruction::UNUSED_F5:
2599 case Instruction::UNUSED_F6:
2600 case Instruction::UNUSED_F7:
2601 case Instruction::UNUSED_F8:
2602 case Instruction::UNUSED_F9:
2603 case Instruction::UNUSED_FA:
2604 case Instruction::UNUSED_FB:
jeffhaobdb76512011-09-07 11:43:16 -07002605 case Instruction::UNUSED_FC:
jeffhaobdb76512011-09-07 11:43:16 -07002606 case Instruction::UNUSED_FD:
jeffhaobdb76512011-09-07 11:43:16 -07002607 case Instruction::UNUSED_FE:
jeffhaobdb76512011-09-07 11:43:16 -07002608 case Instruction::UNUSED_FF:
jeffhaod5347e02012-03-22 17:25:05 -07002609 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Unexpected opcode " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07002610 break;
2611
2612 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002613 * DO NOT add a "default" clause here. Without it the compiler will
jeffhaobdb76512011-09-07 11:43:16 -07002614 * complain if an instruction is missing (which is desirable).
2615 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002616 } // end - switch (dec_insn.opcode)
jeffhaobdb76512011-09-07 11:43:16 -07002617
Ian Rogersad0b3a32012-04-16 14:50:24 -07002618 if (have_pending_hard_failure_) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002619 if (Runtime::Current()->IsCompiler()) {
jeffhaob57e9522012-04-26 18:08:21 -07002620 /* When compiling, check that the last failure is a hard failure */
Ian Rogersad0b3a32012-04-16 14:50:24 -07002621 CHECK_EQ(failures_[failures_.size() - 1], VERIFY_ERROR_BAD_CLASS_HARD);
Ian Rogerse1758fe2012-04-19 11:31:15 -07002622 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002623 /* immediate failure, reject class */
2624 info_messages_ << "Rejecting opcode " << inst->DumpString(dex_file_);
2625 return false;
jeffhaofaf459e2012-08-31 15:32:47 -07002626 } else if (have_pending_runtime_throw_failure_) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07002627 /* checking interpreter will throw, mark following code as unreachable */
jeffhaofaf459e2012-08-31 15:32:47 -07002628 opcode_flags = Instruction::kThrow;
jeffhaobdb76512011-09-07 11:43:16 -07002629 }
jeffhaobdb76512011-09-07 11:43:16 -07002630 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002631 * If we didn't just set the result register, clear it out. This ensures that you can only use
2632 * "move-result" immediately after the result is set. (We could check this statically, but it's
2633 * not expensive and it makes our debugging output cleaner.)
jeffhaobdb76512011-09-07 11:43:16 -07002634 */
2635 if (!just_set_result) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002636 work_line_->SetResultTypeToUnknown();
jeffhaobdb76512011-09-07 11:43:16 -07002637 }
2638
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002639
jeffhaobdb76512011-09-07 11:43:16 -07002640
2641 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002642 * Handle "branch". Tag the branch target.
jeffhaobdb76512011-09-07 11:43:16 -07002643 *
2644 * NOTE: instructions like Instruction::EQZ provide information about the
jeffhaod1f0fde2011-09-08 17:25:33 -07002645 * state of the register when the branch is taken or not taken. For example,
jeffhaobdb76512011-09-07 11:43:16 -07002646 * somebody could get a reference field, check it for zero, and if the
2647 * branch is taken immediately store that register in a boolean field
jeffhaod1f0fde2011-09-08 17:25:33 -07002648 * since the value is known to be zero. We do not currently account for
jeffhaobdb76512011-09-07 11:43:16 -07002649 * that, and will reject the code.
2650 *
2651 * TODO: avoid re-fetching the branch target
2652 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002653 if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002654 bool isConditional, selfOkay;
Ian Rogersd81871c2011-10-03 13:57:23 -07002655 if (!GetBranchOffset(work_insn_idx_, &branch_target, &isConditional, &selfOkay)) {
jeffhaobdb76512011-09-07 11:43:16 -07002656 /* should never happen after static verification */
jeffhaod5347e02012-03-22 17:25:05 -07002657 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad branch";
jeffhaobdb76512011-09-07 11:43:16 -07002658 return false;
2659 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08002660 DCHECK_EQ(isConditional, (opcode_flags & Instruction::kContinue) != 0);
jeffhaod5347e02012-03-22 17:25:05 -07002661 if (!CheckNotMoveException(code_item_->insns_, work_insn_idx_ + branch_target)) {
jeffhaobdb76512011-09-07 11:43:16 -07002662 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002663 }
jeffhaobdb76512011-09-07 11:43:16 -07002664 /* update branch target, set "changed" if appropriate */
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002665 if (NULL != branch_line.get()) {
2666 if (!UpdateRegisters(work_insn_idx_ + branch_target, branch_line.get())) {
2667 return false;
2668 }
2669 } else {
2670 if (!UpdateRegisters(work_insn_idx_ + branch_target, work_line_.get())) {
2671 return false;
2672 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002673 }
jeffhaobdb76512011-09-07 11:43:16 -07002674 }
2675
2676 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002677 * Handle "switch". Tag all possible branch targets.
jeffhaobdb76512011-09-07 11:43:16 -07002678 *
2679 * We've already verified that the table is structurally sound, so we
2680 * just need to walk through and tag the targets.
2681 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002682 if ((opcode_flags & Instruction::kSwitch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002683 int offset_to_switch = insns[1] | (((int32_t) insns[2]) << 16);
2684 const uint16_t* switch_insns = insns + offset_to_switch;
2685 int switch_count = switch_insns[1];
2686 int offset_to_targets, targ;
2687
2688 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
2689 /* 0 = sig, 1 = count, 2/3 = first key */
2690 offset_to_targets = 4;
2691 } else {
2692 /* 0 = sig, 1 = count, 2..count * 2 = keys */
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07002693 DCHECK((*insns & 0xff) == Instruction::SPARSE_SWITCH);
jeffhaobdb76512011-09-07 11:43:16 -07002694 offset_to_targets = 2 + 2 * switch_count;
2695 }
2696
2697 /* verify each switch target */
2698 for (targ = 0; targ < switch_count; targ++) {
2699 int offset;
2700 uint32_t abs_offset;
2701
2702 /* offsets are 32-bit, and only partly endian-swapped */
2703 offset = switch_insns[offset_to_targets + targ * 2] |
2704 (((int32_t) switch_insns[offset_to_targets + targ * 2 + 1]) << 16);
Ian Rogersd81871c2011-10-03 13:57:23 -07002705 abs_offset = work_insn_idx_ + offset;
2706 DCHECK_LT(abs_offset, code_item_->insns_size_in_code_units_);
jeffhaod5347e02012-03-22 17:25:05 -07002707 if (!CheckNotMoveException(code_item_->insns_, abs_offset)) {
jeffhaobdb76512011-09-07 11:43:16 -07002708 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002709 }
2710 if (!UpdateRegisters(abs_offset, work_line_.get()))
jeffhaobdb76512011-09-07 11:43:16 -07002711 return false;
2712 }
2713 }
2714
2715 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002716 * Handle instructions that can throw and that are sitting in a "try" block. (If they're not in a
2717 * "try" block when they throw, control transfers out of the method.)
jeffhaobdb76512011-09-07 11:43:16 -07002718 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002719 if ((opcode_flags & Instruction::kThrow) != 0 && insn_flags_[work_insn_idx_].IsInTry()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002720 bool within_catch_all = false;
Ian Rogers0571d352011-11-03 19:51:38 -07002721 CatchHandlerIterator iterator(*code_item_, work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07002722
Ian Rogers0571d352011-11-03 19:51:38 -07002723 for (; iterator.HasNext(); iterator.Next()) {
2724 if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002725 within_catch_all = true;
2726 }
jeffhaobdb76512011-09-07 11:43:16 -07002727 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002728 * Merge registers into the "catch" block. We want to use the "savedRegs" rather than
2729 * "work_regs", because at runtime the exception will be thrown before the instruction
2730 * modifies any registers.
jeffhaobdb76512011-09-07 11:43:16 -07002731 */
Ian Rogers0571d352011-11-03 19:51:38 -07002732 if (!UpdateRegisters(iterator.GetHandlerAddress(), saved_line_.get())) {
jeffhaobdb76512011-09-07 11:43:16 -07002733 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002734 }
jeffhaobdb76512011-09-07 11:43:16 -07002735 }
2736
2737 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002738 * If the monitor stack depth is nonzero, there must be a "catch all" handler for this
2739 * instruction. This does apply to monitor-exit because of async exception handling.
jeffhaobdb76512011-09-07 11:43:16 -07002740 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002741 if (work_line_->MonitorStackDepth() > 0 && !within_catch_all) {
jeffhaobdb76512011-09-07 11:43:16 -07002742 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002743 * The state in work_line reflects the post-execution state. If the current instruction is a
2744 * monitor-enter and the monitor stack was empty, we don't need a catch-all (if it throws,
jeffhaobdb76512011-09-07 11:43:16 -07002745 * it will do so before grabbing the lock).
2746 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002747 if (inst->Opcode() != Instruction::MONITOR_ENTER || work_line_->MonitorStackDepth() != 1) {
jeffhaod5347e02012-03-22 17:25:05 -07002748 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
Ian Rogersd81871c2011-10-03 13:57:23 -07002749 << "expected to be within a catch-all for an instruction where a monitor is held";
jeffhaobdb76512011-09-07 11:43:16 -07002750 return false;
2751 }
2752 }
2753 }
2754
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002755 /* Handle "continue". Tag the next consecutive instruction.
2756 * Note: Keep the code handling "continue" case below the "branch" and "switch" cases,
2757 * because it changes work_line_ when performing peephole optimization
2758 * and this change should not be used in those cases.
2759 */
Ian Rogers6d376ae2013-07-23 15:12:40 -07002760 if ((opcode_flags & Instruction::kContinue) != 0) {
2761 uint32_t next_insn_idx = work_insn_idx_ + CurrentInsnFlags()->GetLengthInCodeUnits();
2762 if (next_insn_idx >= code_item_->insns_size_in_code_units_) {
2763 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Execution can walk off end of code area";
2764 return false;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002765 }
Ian Rogers6d376ae2013-07-23 15:12:40 -07002766 // The only way to get to a move-exception instruction is to get thrown there. Make sure the
2767 // next instruction isn't one.
2768 if (!CheckNotMoveException(code_item_->insns_, next_insn_idx)) {
2769 return false;
2770 }
2771 if (NULL != fallthrough_line.get()) {
2772 // Make workline consistent with fallthrough computed from peephole optimization.
2773 work_line_->CopyFromLine(fallthrough_line.get());
2774 }
Ian Rogersb8c78592013-07-25 23:52:52 +00002775 if (insn_flags_[next_insn_idx].IsReturn()) {
2776 // For returns we only care about the operand to the return, all other registers are dead.
2777 const Instruction* ret_inst = Instruction::At(code_item_->insns_ + next_insn_idx);
2778 Instruction::Code opcode = ret_inst->Opcode();
2779 if ((opcode == Instruction::RETURN_VOID) || (opcode == Instruction::RETURN_VOID_BARRIER)) {
2780 work_line_->MarkAllRegistersAsConflicts();
2781 } else {
2782 if (opcode == Instruction::RETURN_WIDE) {
2783 work_line_->MarkAllRegistersAsConflictsExceptWide(ret_inst->VRegA_11x());
2784 } else {
2785 work_line_->MarkAllRegistersAsConflictsExcept(ret_inst->VRegA_11x());
2786 }
2787 }
2788 }
Ian Rogers6d376ae2013-07-23 15:12:40 -07002789 RegisterLine* next_line = reg_table_.GetLine(next_insn_idx);
2790 if (next_line != NULL) {
2791 // Merge registers into what we have for the next instruction,
2792 // and set the "changed" flag if needed.
2793 if (!UpdateRegisters(next_insn_idx, work_line_.get())) {
2794 return false;
2795 }
2796 } else {
2797 /*
2798 * We're not recording register data for the next instruction, so we don't know what the
2799 * prior state was. We have to assume that something has changed and re-evaluate it.
2800 */
2801 insn_flags_[next_insn_idx].SetChanged();
2802 }
2803 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002804
jeffhaod1f0fde2011-09-08 17:25:33 -07002805 /* If we're returning from the method, make sure monitor stack is empty. */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002806 if ((opcode_flags & Instruction::kReturn) != 0) {
Elliott Hughesb25c3f62012-03-26 16:35:06 -07002807 if (!work_line_->VerifyMonitorStackEmpty()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002808 return false;
2809 }
jeffhaobdb76512011-09-07 11:43:16 -07002810 }
2811
2812 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002813 * Update start_guess. Advance to the next instruction of that's
2814 * possible, otherwise use the branch target if one was found. If
jeffhaobdb76512011-09-07 11:43:16 -07002815 * neither of those exists we're in a return or throw; leave start_guess
2816 * alone and let the caller sort it out.
2817 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002818 if ((opcode_flags & Instruction::kContinue) != 0) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002819 *start_guess = work_insn_idx_ + insn_flags_[work_insn_idx_].GetLengthInCodeUnits();
Elliott Hughesadb8c672012-03-06 16:49:32 -08002820 } else if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002821 /* we're still okay if branch_target is zero */
Ian Rogersd81871c2011-10-03 13:57:23 -07002822 *start_guess = work_insn_idx_ + branch_target;
jeffhaobdb76512011-09-07 11:43:16 -07002823 }
2824
Ian Rogersd81871c2011-10-03 13:57:23 -07002825 DCHECK_LT(*start_guess, code_item_->insns_size_in_code_units_);
2826 DCHECK(insn_flags_[*start_guess].IsOpcode());
jeffhaobdb76512011-09-07 11:43:16 -07002827
2828 return true;
Brian Carlstrom7934ac22013-07-26 10:54:15 -07002829} // NOLINT(readability/fn_size)
jeffhaobdb76512011-09-07 11:43:16 -07002830
Ian Rogers776ac1f2012-04-13 23:36:36 -07002831const RegType& MethodVerifier::ResolveClassAndCheckAccess(uint32_t class_idx) {
Ian Rogers0571d352011-11-03 19:51:38 -07002832 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002833 const RegType& referrer = GetDeclaringClass();
Mathieu Chartier590fee92013-09-13 13:46:47 -07002834 mirror::Class* klass = (*dex_cache_)->GetResolvedType(class_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002835 const RegType& result =
Ian Rogers04f94f42013-06-10 15:09:26 -07002836 klass != NULL ? reg_types_.FromClass(descriptor, klass,
2837 klass->CannotBeAssignedFromOtherTypes())
Mathieu Chartier590fee92013-09-13 13:46:47 -07002838 : reg_types_.FromDescriptor(class_loader_->get(), descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002839 if (result.IsConflict()) {
2840 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "accessing broken descriptor '" << descriptor
2841 << "' in " << referrer;
2842 return result;
2843 }
Ian Rogerse1758fe2012-04-19 11:31:15 -07002844 if (klass == NULL && !result.IsUnresolvedTypes()) {
Mathieu Chartier590fee92013-09-13 13:46:47 -07002845 (*dex_cache_)->SetResolvedType(class_idx, result.GetClass());
Ian Rogerse1758fe2012-04-19 11:31:15 -07002846 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002847 // Check if access is allowed. Unresolved types use xxxWithAccessCheck to
Jeff Haob24b4a72013-07-31 13:47:31 -07002848 // check at runtime if access is allowed and so pass here. If result is
2849 // primitive, skip the access check.
2850 if (result.IsNonZeroReferenceTypes() && !result.IsUnresolvedTypes() &&
2851 !referrer.IsUnresolvedTypes() && !referrer.CanAccess(result)) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002852 Fail(VERIFY_ERROR_ACCESS_CLASS) << "illegal class access: '"
Ian Rogersad0b3a32012-04-16 14:50:24 -07002853 << referrer << "' -> '" << result << "'";
Ian Rogers28ad40d2011-10-27 15:19:26 -07002854 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002855 return result;
Ian Rogersd81871c2011-10-03 13:57:23 -07002856}
2857
Ian Rogers776ac1f2012-04-13 23:36:36 -07002858const RegType& MethodVerifier::GetCaughtExceptionType() {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002859 const RegType* common_super = NULL;
Ian Rogersd81871c2011-10-03 13:57:23 -07002860 if (code_item_->tries_size_ != 0) {
Ian Rogers0571d352011-11-03 19:51:38 -07002861 const byte* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
Ian Rogersd81871c2011-10-03 13:57:23 -07002862 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
2863 for (uint32_t i = 0; i < handlers_size; i++) {
Ian Rogers0571d352011-11-03 19:51:38 -07002864 CatchHandlerIterator iterator(handlers_ptr);
2865 for (; iterator.HasNext(); iterator.Next()) {
2866 if (iterator.GetHandlerAddress() == (uint32_t) work_insn_idx_) {
2867 if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) {
Ian Rogersb4903572012-10-11 11:52:56 -07002868 common_super = &reg_types_.JavaLangThrowable(false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002869 } else {
Ian Rogers0571d352011-11-03 19:51:38 -07002870 const RegType& exception = ResolveClassAndCheckAccess(iterator.GetHandlerTypeIndex());
Jeff Haob878f212014-04-24 16:25:36 -07002871 if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(exception)) {
Jeff Haoc26a56c2013-11-04 12:00:47 -08002872 if (exception.IsUnresolvedTypes()) {
2873 // We don't know enough about the type. Fail here and let runtime handle it.
2874 Fail(VERIFY_ERROR_NO_CLASS) << "unresolved exception class " << exception;
2875 return exception;
2876 } else {
2877 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unexpected non-exception class " << exception;
2878 return reg_types_.Conflict();
2879 }
Jeff Haob878f212014-04-24 16:25:36 -07002880 } else if (common_super == nullptr) {
2881 common_super = &exception;
Ian Rogers28ad40d2011-10-27 15:19:26 -07002882 } else if (common_super->Equals(exception)) {
Ian Rogersc4762272012-02-01 15:55:55 -08002883 // odd case, but nothing to do
Ian Rogersd81871c2011-10-03 13:57:23 -07002884 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002885 common_super = &common_super->Merge(exception, &reg_types_);
Ian Rogersb4903572012-10-11 11:52:56 -07002886 CHECK(reg_types_.JavaLangThrowable(false).IsAssignableFrom(*common_super));
Ian Rogersd81871c2011-10-03 13:57:23 -07002887 }
2888 }
2889 }
2890 }
Ian Rogers0571d352011-11-03 19:51:38 -07002891 handlers_ptr = iterator.EndDataPointer();
Ian Rogersd81871c2011-10-03 13:57:23 -07002892 }
2893 }
2894 if (common_super == NULL) {
2895 /* no catch blocks, or no catches with classes we can find */
jeffhaod5347e02012-03-22 17:25:05 -07002896 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unable to find exception handler";
Ian Rogersad0b3a32012-04-16 14:50:24 -07002897 return reg_types_.Conflict();
Ian Rogersd81871c2011-10-03 13:57:23 -07002898 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002899 return *common_super;
Ian Rogersd81871c2011-10-03 13:57:23 -07002900}
2901
Brian Carlstromea46f952013-07-30 01:26:50 -07002902mirror::ArtMethod* MethodVerifier::ResolveMethodAndCheckAccess(uint32_t dex_method_idx,
Ian Rogersd91d6d62013-09-25 20:26:14 -07002903 MethodType method_type) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002904 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx);
Ian Rogers90040192011-12-16 08:54:29 -08002905 const RegType& klass_type = ResolveClassAndCheckAccess(method_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002906 if (klass_type.IsConflict()) {
2907 std::string append(" in attempt to access method ");
2908 append += dex_file_->GetMethodName(method_id);
2909 AppendToLastFailMessage(append);
Ian Rogers90040192011-12-16 08:54:29 -08002910 return NULL;
2911 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002912 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers90040192011-12-16 08:54:29 -08002913 return NULL; // Can't resolve Class so no more to do here
2914 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002915 mirror::Class* klass = klass_type.GetClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002916 const RegType& referrer = GetDeclaringClass();
Mathieu Chartier590fee92013-09-13 13:46:47 -07002917 mirror::ArtMethod* res_method = (*dex_cache_)->GetResolvedMethod(dex_method_idx);
Ian Rogersd81871c2011-10-03 13:57:23 -07002918 if (res_method == NULL) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07002919 const char* name = dex_file_->GetMethodName(method_id);
Ian Rogersd91d6d62013-09-25 20:26:14 -07002920 const Signature signature = dex_file_->GetMethodSignature(method_id);
jeffhao8cd6dda2012-02-22 10:15:34 -08002921
2922 if (method_type == METHOD_DIRECT || method_type == METHOD_STATIC) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002923 res_method = klass->FindDirectMethod(name, signature);
jeffhao8cd6dda2012-02-22 10:15:34 -08002924 } else if (method_type == METHOD_INTERFACE) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002925 res_method = klass->FindInterfaceMethod(name, signature);
2926 } else {
2927 res_method = klass->FindVirtualMethod(name, signature);
2928 }
2929 if (res_method != NULL) {
Mathieu Chartier590fee92013-09-13 13:46:47 -07002930 (*dex_cache_)->SetResolvedMethod(dex_method_idx, res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07002931 } else {
jeffhao8cd6dda2012-02-22 10:15:34 -08002932 // If a virtual or interface method wasn't found with the expected type, look in
2933 // the direct methods. This can happen when the wrong invoke type is used or when
2934 // a class has changed, and will be flagged as an error in later checks.
2935 if (method_type == METHOD_INTERFACE || method_type == METHOD_VIRTUAL) {
2936 res_method = klass->FindDirectMethod(name, signature);
2937 }
2938 if (res_method == NULL) {
2939 Fail(VERIFY_ERROR_NO_METHOD) << "couldn't find method "
2940 << PrettyDescriptor(klass) << "." << name
2941 << " " << signature;
2942 return NULL;
2943 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002944 }
2945 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002946 // Make sure calls to constructors are "direct". There are additional restrictions but we don't
2947 // enforce them here.
2948 if (res_method->IsConstructor() && method_type != METHOD_DIRECT) {
jeffhaod5347e02012-03-22 17:25:05 -07002949 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting non-direct call to constructor "
2950 << PrettyMethod(res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07002951 return NULL;
2952 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002953 // Disallow any calls to class initializers.
2954 if (MethodHelper(res_method).IsClassInitializer()) {
jeffhaod5347e02012-03-22 17:25:05 -07002955 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting call to class initializer "
2956 << PrettyMethod(res_method);
jeffhao8cd6dda2012-02-22 10:15:34 -08002957 return NULL;
2958 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002959 // Check if access is allowed.
Ian Rogersad0b3a32012-04-16 14:50:24 -07002960 if (!referrer.CanAccessMember(res_method->GetDeclaringClass(), res_method->GetAccessFlags())) {
jeffhao8cd6dda2012-02-22 10:15:34 -08002961 Fail(VERIFY_ERROR_ACCESS_METHOD) << "illegal method access (call " << PrettyMethod(res_method)
Ian Rogersad0b3a32012-04-16 14:50:24 -07002962 << " from " << referrer << ")";
jeffhaob57e9522012-04-26 18:08:21 -07002963 return res_method;
jeffhao8cd6dda2012-02-22 10:15:34 -08002964 }
jeffhaode0d9c92012-02-27 13:58:13 -08002965 // Check that invoke-virtual and invoke-super are not used on private methods of the same class.
2966 if (res_method->IsPrivate() && method_type == METHOD_VIRTUAL) {
jeffhaod5347e02012-03-22 17:25:05 -07002967 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invoke-super/virtual can't be used on private method "
2968 << PrettyMethod(res_method);
jeffhaode0d9c92012-02-27 13:58:13 -08002969 return NULL;
2970 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002971 // Check that interface methods match interface classes.
2972 if (klass->IsInterface() && method_type != METHOD_INTERFACE) {
2973 Fail(VERIFY_ERROR_CLASS_CHANGE) << "non-interface method " << PrettyMethod(res_method)
2974 << " is in an interface class " << PrettyClass(klass);
2975 return NULL;
2976 } else if (!klass->IsInterface() && method_type == METHOD_INTERFACE) {
2977 Fail(VERIFY_ERROR_CLASS_CHANGE) << "interface method " << PrettyMethod(res_method)
2978 << " is in a non-interface class " << PrettyClass(klass);
2979 return NULL;
2980 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002981 // See if the method type implied by the invoke instruction matches the access flags for the
2982 // target method.
2983 if ((method_type == METHOD_DIRECT && !res_method->IsDirect()) ||
2984 (method_type == METHOD_STATIC && !res_method->IsStatic()) ||
2985 ((method_type == METHOD_VIRTUAL || method_type == METHOD_INTERFACE) && res_method->IsDirect())
2986 ) {
Ian Rogers2fc14272012-08-30 10:56:57 -07002987 Fail(VERIFY_ERROR_CLASS_CHANGE) << "invoke type (" << method_type << ") does not match method "
2988 " type of " << PrettyMethod(res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07002989 return NULL;
2990 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002991 return res_method;
2992}
2993
Brian Carlstromea46f952013-07-30 01:26:50 -07002994mirror::ArtMethod* MethodVerifier::VerifyInvocationArgs(const Instruction* inst,
Sebastien Hertz5243e912013-05-21 10:55:07 +02002995 MethodType method_type,
2996 bool is_range,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002997 bool is_super) {
jeffhao8cd6dda2012-02-22 10:15:34 -08002998 // Resolve the method. This could be an abstract or concrete method depending on what sort of call
2999 // we're making.
Sebastien Hertz5243e912013-05-21 10:55:07 +02003000 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Brian Carlstromea46f952013-07-30 01:26:50 -07003001 mirror::ArtMethod* res_method = ResolveMethodAndCheckAccess(method_idx, method_type);
jeffhao8cd6dda2012-02-22 10:15:34 -08003002 if (res_method == NULL) { // error or class is unresolved
3003 return NULL;
3004 }
3005
Ian Rogersd81871c2011-10-03 13:57:23 -07003006 // If we're using invoke-super(method), make sure that the executing method's class' superclass
3007 // has a vtable entry for the target method.
3008 if (is_super) {
3009 DCHECK(method_type == METHOD_VIRTUAL);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003010 const RegType& super = GetDeclaringClass().GetSuperClass(&reg_types_);
Ian Rogers529781d2012-07-23 17:24:29 -07003011 if (super.IsUnresolvedTypes()) {
jeffhao4d8df822012-04-24 17:09:36 -07003012 Fail(VERIFY_ERROR_NO_METHOD) << "unknown super class in invoke-super from "
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003013 << PrettyMethod(dex_method_idx_, *dex_file_)
jeffhao4d8df822012-04-24 17:09:36 -07003014 << " to super " << PrettyMethod(res_method);
3015 return NULL;
3016 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003017 mirror::Class* super_klass = super.GetClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07003018 if (res_method->GetMethodIndex() >= super_klass->GetVTable()->GetLength()) {
jeffhao4d8df822012-04-24 17:09:36 -07003019 MethodHelper mh(res_method);
3020 Fail(VERIFY_ERROR_NO_METHOD) << "invalid invoke-super from "
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003021 << PrettyMethod(dex_method_idx_, *dex_file_)
jeffhao4d8df822012-04-24 17:09:36 -07003022 << " to super " << super
3023 << "." << mh.GetName()
3024 << mh.GetSignature();
Ian Rogersd81871c2011-10-03 13:57:23 -07003025 return NULL;
3026 }
3027 }
3028 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003029 // match the call to the signature. Also, we might be calling through an abstract method
Ian Rogersd81871c2011-10-03 13:57:23 -07003030 // definition (which doesn't have register count values).
Sebastien Hertz5243e912013-05-21 10:55:07 +02003031 const size_t expected_args = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
Ian Rogersd81871c2011-10-03 13:57:23 -07003032 /* caught by static verifier */
3033 DCHECK(is_range || expected_args <= 5);
3034 if (expected_args > code_item_->outs_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07003035 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
Ian Rogersd81871c2011-10-03 13:57:23 -07003036 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
3037 return NULL;
3038 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003039
jeffhaobdb76512011-09-07 11:43:16 -07003040 /*
Ian Rogersad0b3a32012-04-16 14:50:24 -07003041 * Check the "this" argument, which must be an instance of the class that declared the method.
3042 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
3043 * rigorous check here (which is okay since we have to do it at runtime).
jeffhaobdb76512011-09-07 11:43:16 -07003044 */
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003045 size_t actual_args = 0;
Ian Rogersd81871c2011-10-03 13:57:23 -07003046 if (!res_method->IsStatic()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003047 const RegType& actual_arg_type = work_line_->GetInvocationThis(inst, is_range);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003048 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
Ian Rogersd81871c2011-10-03 13:57:23 -07003049 return NULL;
3050 }
3051 if (actual_arg_type.IsUninitializedReference() && !res_method->IsConstructor()) {
jeffhaod5347e02012-03-22 17:25:05 -07003052 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
Ian Rogersd81871c2011-10-03 13:57:23 -07003053 return NULL;
3054 }
3055 if (method_type != METHOD_INTERFACE && !actual_arg_type.IsZero()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003056 mirror::Class* klass = res_method->GetDeclaringClass();
Ian Rogers04f94f42013-06-10 15:09:26 -07003057 const RegType& res_method_class =
3058 reg_types_.FromClass(ClassHelper(klass).GetDescriptor(), klass,
3059 klass->CannotBeAssignedFromOtherTypes());
Ian Rogers9074b992011-10-26 17:41:55 -07003060 if (!res_method_class.IsAssignableFrom(actual_arg_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07003061 Fail(actual_arg_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS:
3062 VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003063 << "' not instance of '" << res_method_class << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07003064 return NULL;
3065 }
3066 }
3067 actual_args++;
3068 }
3069 /*
3070 * Process the target method's signature. This signature may or may not
3071 * have been verified, so we can't assume it's properly formed.
3072 */
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003073 MethodHelper mh(res_method);
3074 const DexFile::TypeList* params = mh.GetParameterTypeList();
3075 size_t params_size = params == NULL ? 0 : params->Size();
Sebastien Hertz5243e912013-05-21 10:55:07 +02003076 uint32_t arg[5];
3077 if (!is_range) {
3078 inst->GetArgs(arg);
3079 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003080 for (size_t param_index = 0; param_index < params_size; param_index++) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003081 if (actual_args >= expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07003082 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invalid call to '" << PrettyMethod(res_method)
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003083 << "'. Expected " << expected_args << " arguments, processing argument " << actual_args
3084 << " (where longs/doubles count twice).";
Ian Rogersd81871c2011-10-03 13:57:23 -07003085 return NULL;
3086 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003087 const char* descriptor =
3088 mh.GetTypeDescriptorFromTypeIdx(params->GetTypeItem(param_index).type_idx_);
3089 if (descriptor == NULL) {
jeffhaod5347e02012-03-22 17:25:05 -07003090 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003091 << " missing signature component";
3092 return NULL;
Ian Rogersd81871c2011-10-03 13:57:23 -07003093 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07003094 const RegType& reg_type = reg_types_.FromDescriptor(class_loader_->get(), descriptor, false);
Sebastien Hertz5243e912013-05-21 10:55:07 +02003095 uint32_t get_reg = is_range ? inst->VRegC_3rc() + actual_args : arg[actual_args];
Jeff Haoa6b22c52013-10-04 14:33:22 -07003096 if (reg_type.IsIntegralTypes()) {
3097 const RegType& src_type = work_line_->GetRegisterType(get_reg);
3098 if (!src_type.IsIntegralTypes()) {
3099 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register v" << get_reg << " has type " << src_type
3100 << " but expected " << reg_type;
3101 return res_method;
3102 }
3103 } else if (!work_line_->VerifyRegisterType(get_reg, reg_type)) {
jeffhaob57e9522012-04-26 18:08:21 -07003104 return res_method;
Ian Rogersd81871c2011-10-03 13:57:23 -07003105 }
3106 actual_args = reg_type.IsLongOrDoubleTypes() ? actual_args + 2 : actual_args + 1;
3107 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003108 if (actual_args != expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07003109 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003110 << " expected " << expected_args << " arguments, found " << actual_args;
Ian Rogersd81871c2011-10-03 13:57:23 -07003111 return NULL;
3112 } else {
3113 return res_method;
3114 }
3115}
3116
Brian Carlstromea46f952013-07-30 01:26:50 -07003117mirror::ArtMethod* MethodVerifier::GetQuickInvokedMethod(const Instruction* inst,
Mathieu Chartier590fee92013-09-13 13:46:47 -07003118 RegisterLine* reg_line, bool is_range) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003119 DCHECK(inst->Opcode() == Instruction::INVOKE_VIRTUAL_QUICK ||
3120 inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
3121 const RegType& actual_arg_type = reg_line->GetInvocationThis(inst, is_range);
Ian Rogers9bc54402014-04-17 16:40:01 -07003122 if (!actual_arg_type.HasClass()) {
3123 VLOG(verifier) << "Failed to get mirror::Class* from '" << actual_arg_type << "'";
Andreas Gampe63981562014-04-17 12:28:43 -07003124 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003125 }
Ian Rogers9bc54402014-04-17 16:40:01 -07003126 mirror::ObjectArray<mirror::ArtMethod>* vtable = actual_arg_type.GetClass()->GetVTable();
3127 CHECK(vtable != nullptr);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003128 uint16_t vtable_index = is_range ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogers9bc54402014-04-17 16:40:01 -07003129 CHECK_LT(static_cast<int32_t>(vtable_index), vtable->GetLength());
Brian Carlstromea46f952013-07-30 01:26:50 -07003130 mirror::ArtMethod* res_method = vtable->Get(vtable_index);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003131 CHECK(!Thread::Current()->IsExceptionPending());
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003132 return res_method;
3133}
3134
Brian Carlstromea46f952013-07-30 01:26:50 -07003135mirror::ArtMethod* MethodVerifier::VerifyInvokeVirtualQuickArgs(const Instruction* inst,
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003136 bool is_range) {
3137 DCHECK(Runtime::Current()->IsStarted());
Brian Carlstromea46f952013-07-30 01:26:50 -07003138 mirror::ArtMethod* res_method = GetQuickInvokedMethod(inst, work_line_.get(),
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003139 is_range);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003140 if (res_method == NULL) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003141 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer method from " << inst->Name();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003142 return NULL;
3143 }
3144 CHECK(!res_method->IsDirect() && !res_method->IsStatic());
3145
3146 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
3147 // match the call to the signature. Also, we might be calling through an abstract method
3148 // definition (which doesn't have register count values).
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003149 const RegType& actual_arg_type = work_line_->GetInvocationThis(inst, is_range);
3150 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
3151 return NULL;
3152 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003153 const size_t expected_args = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
3154 /* caught by static verifier */
3155 DCHECK(is_range || expected_args <= 5);
3156 if (expected_args > code_item_->outs_size_) {
3157 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
3158 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
3159 return NULL;
3160 }
3161
3162 /*
3163 * Check the "this" argument, which must be an instance of the class that declared the method.
3164 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
3165 * rigorous check here (which is okay since we have to do it at runtime).
3166 */
3167 if (actual_arg_type.IsUninitializedReference() && !res_method->IsConstructor()) {
3168 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
3169 return NULL;
3170 }
3171 if (!actual_arg_type.IsZero()) {
3172 mirror::Class* klass = res_method->GetDeclaringClass();
3173 const RegType& res_method_class =
3174 reg_types_.FromClass(ClassHelper(klass).GetDescriptor(), klass,
3175 klass->CannotBeAssignedFromOtherTypes());
3176 if (!res_method_class.IsAssignableFrom(actual_arg_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07003177 Fail(actual_arg_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS :
3178 VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003179 << "' not instance of '" << res_method_class << "'";
3180 return NULL;
3181 }
3182 }
3183 /*
3184 * Process the target method's signature. This signature may or may not
3185 * have been verified, so we can't assume it's properly formed.
3186 */
3187 MethodHelper mh(res_method);
3188 const DexFile::TypeList* params = mh.GetParameterTypeList();
3189 size_t params_size = params == NULL ? 0 : params->Size();
3190 uint32_t arg[5];
3191 if (!is_range) {
3192 inst->GetArgs(arg);
3193 }
3194 size_t actual_args = 1;
3195 for (size_t param_index = 0; param_index < params_size; param_index++) {
3196 if (actual_args >= expected_args) {
3197 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invalid call to '" << PrettyMethod(res_method)
Brian Carlstrom93c33962013-07-26 10:37:43 -07003198 << "'. Expected " << expected_args
3199 << " arguments, processing argument " << actual_args
3200 << " (where longs/doubles count twice).";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003201 return NULL;
3202 }
3203 const char* descriptor =
3204 mh.GetTypeDescriptorFromTypeIdx(params->GetTypeItem(param_index).type_idx_);
3205 if (descriptor == NULL) {
3206 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003207 << " missing signature component";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003208 return NULL;
3209 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07003210 const RegType& reg_type = reg_types_.FromDescriptor(class_loader_->get(), descriptor, false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003211 uint32_t get_reg = is_range ? inst->VRegC_3rc() + actual_args : arg[actual_args];
3212 if (!work_line_->VerifyRegisterType(get_reg, reg_type)) {
3213 return res_method;
3214 }
3215 actual_args = reg_type.IsLongOrDoubleTypes() ? actual_args + 2 : actual_args + 1;
3216 }
3217 if (actual_args != expected_args) {
3218 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
3219 << " expected " << expected_args << " arguments, found " << actual_args;
3220 return NULL;
3221 } else {
3222 return res_method;
3223 }
3224}
3225
Ian Rogers62342ec2013-06-11 10:26:37 -07003226void MethodVerifier::VerifyNewArray(const Instruction* inst, bool is_filled, bool is_range) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003227 uint32_t type_idx;
3228 if (!is_filled) {
3229 DCHECK_EQ(inst->Opcode(), Instruction::NEW_ARRAY);
3230 type_idx = inst->VRegC_22c();
3231 } else if (!is_range) {
3232 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY);
3233 type_idx = inst->VRegB_35c();
3234 } else {
3235 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY_RANGE);
3236 type_idx = inst->VRegB_3rc();
3237 }
3238 const RegType& res_type = ResolveClassAndCheckAccess(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003239 if (res_type.IsConflict()) { // bad class
3240 DCHECK_NE(failures_.size(), 0U);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003241 } else {
3242 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
3243 if (!res_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003244 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "new-array on non-array class " << res_type;
Ian Rogers0c4a5062012-02-03 15:18:59 -08003245 } else if (!is_filled) {
3246 /* make sure "size" register is valid type */
Sebastien Hertz5243e912013-05-21 10:55:07 +02003247 work_line_->VerifyRegisterType(inst->VRegB_22c(), reg_types_.Integer());
Ian Rogers0c4a5062012-02-03 15:18:59 -08003248 /* set register type to array class */
Ian Rogers62342ec2013-06-11 10:26:37 -07003249 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
3250 work_line_->SetRegisterType(inst->VRegA_22c(), precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003251 } else {
3252 // Verify each register. If "arg_count" is bad, VerifyRegisterType() will run off the end of
3253 // the list and fail. It's legal, if silly, for arg_count to be zero.
Mathieu Chartier590fee92013-09-13 13:46:47 -07003254 const RegType& expected_type = reg_types_.GetComponentType(res_type, class_loader_->get());
Sebastien Hertz5243e912013-05-21 10:55:07 +02003255 uint32_t arg_count = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
3256 uint32_t arg[5];
3257 if (!is_range) {
3258 inst->GetArgs(arg);
3259 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08003260 for (size_t ui = 0; ui < arg_count; ui++) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003261 uint32_t get_reg = is_range ? inst->VRegC_3rc() + ui : arg[ui];
Ian Rogers0c4a5062012-02-03 15:18:59 -08003262 if (!work_line_->VerifyRegisterType(get_reg, expected_type)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003263 work_line_->SetResultRegisterType(reg_types_.Conflict());
Ian Rogers0c4a5062012-02-03 15:18:59 -08003264 return;
3265 }
3266 }
3267 // filled-array result goes into "result" register
Ian Rogers62342ec2013-06-11 10:26:37 -07003268 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
3269 work_line_->SetResultRegisterType(precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003270 }
3271 }
3272}
3273
Sebastien Hertz5243e912013-05-21 10:55:07 +02003274void MethodVerifier::VerifyAGet(const Instruction* inst,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003275 const RegType& insn_type, bool is_primitive) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003276 const RegType& index_type = work_line_->GetRegisterType(inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07003277 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003278 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07003279 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003280 const RegType& array_type = work_line_->GetRegisterType(inst->VRegB_23x());
Ian Rogers89310de2012-02-01 13:47:30 -08003281 if (array_type.IsZero()) {
3282 // Null array class; this code path will fail at runtime. Infer a merge-able type from the
3283 // instruction type. TODO: have a proper notion of bottom here.
3284 if (!is_primitive || insn_type.IsCategory1Types()) {
3285 // Reference or category 1
Sebastien Hertz5243e912013-05-21 10:55:07 +02003286 work_line_->SetRegisterType(inst->VRegA_23x(), reg_types_.Zero());
Ian Rogersd81871c2011-10-03 13:57:23 -07003287 } else {
Ian Rogers89310de2012-02-01 13:47:30 -08003288 // Category 2
Sebastien Hertz5243e912013-05-21 10:55:07 +02003289 work_line_->SetRegisterTypeWide(inst->VRegA_23x(), reg_types_.FromCat2ConstLo(0, false),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003290 reg_types_.FromCat2ConstHi(0, false));
Ian Rogers89310de2012-02-01 13:47:30 -08003291 }
jeffhaofc3144e2012-02-01 17:21:15 -08003292 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003293 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aget";
Ian Rogers89310de2012-02-01 13:47:30 -08003294 } else {
3295 /* verify the class */
Mathieu Chartier590fee92013-09-13 13:46:47 -07003296 const RegType& component_type = reg_types_.GetComponentType(array_type, class_loader_->get());
jeffhaofc3144e2012-02-01 17:21:15 -08003297 if (!component_type.IsReferenceTypes() && !is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07003298 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08003299 << " source for aget-object";
3300 } else if (component_type.IsNonZeroReferenceTypes() && is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07003301 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08003302 << " source for category 1 aget";
3303 } else if (is_primitive && !insn_type.Equals(component_type) &&
3304 !((insn_type.IsInteger() && component_type.IsFloat()) ||
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003305 (insn_type.IsLong() && component_type.IsDouble()))) {
3306 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array type " << array_type
3307 << " incompatible with aget of type " << insn_type;
Ian Rogers89310de2012-02-01 13:47:30 -08003308 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003309 // Use knowledge of the field type which is stronger than the type inferred from the
3310 // instruction, which can't differentiate object types and ints from floats, longs from
3311 // doubles.
3312 if (!component_type.IsLowHalf()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003313 work_line_->SetRegisterType(inst->VRegA_23x(), component_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003314 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003315 work_line_->SetRegisterTypeWide(inst->VRegA_23x(), component_type,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003316 component_type.HighHalf(&reg_types_));
3317 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003318 }
3319 }
3320 }
3321}
3322
Jeff Haofe1f7c82013-08-01 14:50:24 -07003323void MethodVerifier::VerifyPrimitivePut(const RegType& target_type, const RegType& insn_type,
3324 const uint32_t vregA) {
3325 // Primitive assignability rules are weaker than regular assignability rules.
3326 bool instruction_compatible;
3327 bool value_compatible;
3328 const RegType& value_type = work_line_->GetRegisterType(vregA);
3329 if (target_type.IsIntegralTypes()) {
Jeff Haoa4647482013-08-06 15:35:47 -07003330 instruction_compatible = target_type.Equals(insn_type);
Jeff Haofe1f7c82013-08-01 14:50:24 -07003331 value_compatible = value_type.IsIntegralTypes();
3332 } else if (target_type.IsFloat()) {
3333 instruction_compatible = insn_type.IsInteger(); // no put-float, so expect put-int
3334 value_compatible = value_type.IsFloatTypes();
3335 } else if (target_type.IsLong()) {
3336 instruction_compatible = insn_type.IsLong();
3337 value_compatible = value_type.IsLongTypes();
3338 } else if (target_type.IsDouble()) {
3339 instruction_compatible = insn_type.IsLong(); // no put-double, so expect put-long
3340 value_compatible = value_type.IsDoubleTypes();
3341 } else {
3342 instruction_compatible = false; // reference with primitive store
3343 value_compatible = false; // unused
3344 }
3345 if (!instruction_compatible) {
3346 // This is a global failure rather than a class change failure as the instructions and
3347 // the descriptors for the type should have been consistent within the same file at
3348 // compile time.
3349 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "put insn has type '" << insn_type
3350 << "' but expected type '" << target_type << "'";
3351 return;
3352 }
3353 if (!value_compatible) {
3354 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
3355 << " of type " << value_type << " but expected " << target_type << " for put";
3356 return;
3357 }
3358}
3359
Sebastien Hertz5243e912013-05-21 10:55:07 +02003360void MethodVerifier::VerifyAPut(const Instruction* inst,
Sebastien Hertz757b3042014-03-28 14:34:28 +01003361 const RegType& insn_type, bool is_primitive) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003362 const RegType& index_type = work_line_->GetRegisterType(inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07003363 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003364 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07003365 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003366 const RegType& array_type = work_line_->GetRegisterType(inst->VRegB_23x());
Ian Rogers89310de2012-02-01 13:47:30 -08003367 if (array_type.IsZero()) {
3368 // Null array type; this code path will fail at runtime. Infer a merge-able type from the
3369 // instruction type.
jeffhaofc3144e2012-02-01 17:21:15 -08003370 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003371 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aput";
Ian Rogers89310de2012-02-01 13:47:30 -08003372 } else {
Mathieu Chartier590fee92013-09-13 13:46:47 -07003373 const RegType& component_type = reg_types_.GetComponentType(array_type, class_loader_->get());
Jeff Haofe1f7c82013-08-01 14:50:24 -07003374 const uint32_t vregA = inst->VRegA_23x();
Jeff Haob24b4a72013-07-31 13:47:31 -07003375 if (is_primitive) {
Jeff Haofe1f7c82013-08-01 14:50:24 -07003376 VerifyPrimitivePut(component_type, insn_type, vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07003377 } else {
Jeff Haob24b4a72013-07-31 13:47:31 -07003378 if (!component_type.IsReferenceTypes()) {
3379 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
3380 << " source for aput-object";
3381 } else {
3382 // The instruction agrees with the type of array, confirm the value to be stored does too
3383 // Note: we use the instruction type (rather than the component type) for aput-object as
3384 // incompatible classes will be caught at runtime as an array store exception
Jeff Haofe1f7c82013-08-01 14:50:24 -07003385 work_line_->VerifyRegisterType(vregA, insn_type);
Jeff Haob24b4a72013-07-31 13:47:31 -07003386 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003387 }
3388 }
3389 }
3390}
3391
Brian Carlstromea46f952013-07-30 01:26:50 -07003392mirror::ArtField* MethodVerifier::GetStaticField(int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08003393 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3394 // Check access to class
3395 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Brian Carlstrom7934ac22013-07-26 10:54:15 -07003396 if (klass_type.IsConflict()) { // bad class
Ian Rogersad0b3a32012-04-16 14:50:24 -07003397 AppendToLastFailMessage(StringPrintf(" in attempt to access static field %d (%s) in %s",
3398 field_idx, dex_file_->GetFieldName(field_id),
3399 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers90040192011-12-16 08:54:29 -08003400 return NULL;
3401 }
Elliott Hughesb25c3f62012-03-26 16:35:06 -07003402 if (klass_type.IsUnresolvedTypes()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003403 return NULL; // Can't resolve Class so no more to do here, will do checking at runtime.
Ian Rogers90040192011-12-16 08:54:29 -08003404 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07003405 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
3406 mirror::ArtField* field = class_linker->ResolveFieldJLS(*dex_file_, field_idx, *dex_cache_,
3407 *class_loader_);
Ian Rogersd81871c2011-10-03 13:57:23 -07003408 if (field == NULL) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07003409 VLOG(verifier) << "Unable to resolve static field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07003410 << dex_file_->GetFieldName(field_id) << ") in "
3411 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07003412 DCHECK(Thread::Current()->IsExceptionPending());
3413 Thread::Current()->ClearException();
3414 return NULL;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003415 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
3416 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003417 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access static field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07003418 << " from " << GetDeclaringClass();
Ian Rogersd81871c2011-10-03 13:57:23 -07003419 return NULL;
3420 } else if (!field->IsStatic()) {
3421 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field) << " to be static";
3422 return NULL;
Ian Rogersd81871c2011-10-03 13:57:23 -07003423 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07003424 return field;
Ian Rogersd81871c2011-10-03 13:57:23 -07003425}
3426
Brian Carlstromea46f952013-07-30 01:26:50 -07003427mirror::ArtField* MethodVerifier::GetInstanceField(const RegType& obj_type, int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08003428 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3429 // Check access to class
3430 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003431 if (klass_type.IsConflict()) {
3432 AppendToLastFailMessage(StringPrintf(" in attempt to access instance field %d (%s) in %s",
3433 field_idx, dex_file_->GetFieldName(field_id),
3434 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers90040192011-12-16 08:54:29 -08003435 return NULL;
3436 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003437 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers90040192011-12-16 08:54:29 -08003438 return NULL; // Can't resolve Class so no more to do here
3439 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07003440 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
3441 mirror::ArtField* field = class_linker->ResolveFieldJLS(*dex_file_, field_idx, *dex_cache_,
3442 *class_loader_);
Ian Rogersd81871c2011-10-03 13:57:23 -07003443 if (field == NULL) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07003444 VLOG(verifier) << "Unable to resolve instance field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07003445 << dex_file_->GetFieldName(field_id) << ") in "
3446 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07003447 DCHECK(Thread::Current()->IsExceptionPending());
3448 Thread::Current()->ClearException();
3449 return NULL;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003450 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
3451 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003452 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access instance field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07003453 << " from " << GetDeclaringClass();
Ian Rogersd81871c2011-10-03 13:57:23 -07003454 return NULL;
3455 } else if (field->IsStatic()) {
3456 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field)
3457 << " to not be static";
3458 return NULL;
3459 } else if (obj_type.IsZero()) {
3460 // Cannot infer and check type, however, access will cause null pointer exception
3461 return field;
Ian Rogerse1758fe2012-04-19 11:31:15 -07003462 } else {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003463 mirror::Class* klass = field->GetDeclaringClass();
Ian Rogers637c65b2013-05-31 11:46:00 -07003464 const RegType& field_klass =
3465 reg_types_.FromClass(dex_file_->GetFieldDeclaringClassDescriptor(field_id),
Ian Rogers04f94f42013-06-10 15:09:26 -07003466 klass, klass->CannotBeAssignedFromOtherTypes());
Ian Rogersad0b3a32012-04-16 14:50:24 -07003467 if (obj_type.IsUninitializedTypes() &&
3468 (!IsConstructor() || GetDeclaringClass().Equals(obj_type) ||
3469 !field_klass.Equals(GetDeclaringClass()))) {
3470 // Field accesses through uninitialized references are only allowable for constructors where
3471 // the field is declared in this class
3472 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "cannot access instance field " << PrettyField(field)
Brian Carlstrom93c33962013-07-26 10:37:43 -07003473 << " of a not fully initialized object within the context"
3474 << " of " << PrettyMethod(dex_method_idx_, *dex_file_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003475 return NULL;
3476 } else if (!field_klass.IsAssignableFrom(obj_type)) {
3477 // Trying to access C1.field1 using reference of type C2, which is neither C1 or a sub-class
3478 // of C1. For resolution to occur the declared class of the field must be compatible with
3479 // obj_type, we've discovered this wasn't so, so report the field didn't exist.
3480 Fail(VERIFY_ERROR_NO_FIELD) << "cannot access instance field " << PrettyField(field)
3481 << " from object of type " << obj_type;
3482 return NULL;
3483 } else {
3484 return field;
3485 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003486 }
3487}
3488
Sebastien Hertz5243e912013-05-21 10:55:07 +02003489void MethodVerifier::VerifyISGet(const Instruction* inst, const RegType& insn_type,
3490 bool is_primitive, bool is_static) {
3491 uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
Brian Carlstromea46f952013-07-30 01:26:50 -07003492 mirror::ArtField* field;
Ian Rogersb94a27b2011-10-26 00:33:41 -07003493 if (is_static) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07003494 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003495 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003496 const RegType& object_type = work_line_->GetRegisterType(inst->VRegB_22c());
Ian Rogersf4028cc2011-11-02 14:56:39 -07003497 field = GetInstanceField(object_type, field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003498 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003499 const RegType* field_type = nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003500 if (field != NULL) {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003501 FieldHelper fh(field);
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08003502 mirror::Class* field_type_class = fh.GetType(can_load_classes_);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003503 if (field_type_class != nullptr) {
3504 field_type = &reg_types_.FromClass(fh.GetTypeDescriptor(), field_type_class,
3505 field_type_class->CannotBeAssignedFromOtherTypes());
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08003506 } else {
3507 Thread* self = Thread::Current();
3508 DCHECK(!can_load_classes_ || self->IsExceptionPending());
3509 self->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003510 }
Ian Rogers0d604842012-04-16 14:50:24 -07003511 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003512 if (field_type == nullptr) {
3513 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3514 const char* descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
Mathieu Chartier590fee92013-09-13 13:46:47 -07003515 field_type = &reg_types_.FromDescriptor(class_loader_->get(), descriptor, false);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003516 }
Sebastien Hertz757b3042014-03-28 14:34:28 +01003517 DCHECK(field_type != nullptr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02003518 const uint32_t vregA = (is_static) ? inst->VRegA_21c() : inst->VRegA_22c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07003519 if (is_primitive) {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003520 if (field_type->Equals(insn_type) ||
3521 (field_type->IsFloat() && insn_type.IsInteger()) ||
3522 (field_type->IsDouble() && insn_type.IsLong())) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003523 // expected that read is of the correct primitive type or that int reads are reading
3524 // floats or long reads are reading doubles
3525 } else {
3526 // This is a global failure rather than a class change failure as the instructions and
3527 // the descriptors for the type should have been consistent within the same file at
3528 // compile time
3529 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
3530 << " to be of type '" << insn_type
Sebastien Hertz757b3042014-03-28 14:34:28 +01003531 << "' but found type '" << *field_type << "' in get";
Ian Rogersad0b3a32012-04-16 14:50:24 -07003532 return;
3533 }
3534 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003535 if (!insn_type.IsAssignableFrom(*field_type)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003536 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
3537 << " to be compatible with type '" << insn_type
Sebastien Hertz757b3042014-03-28 14:34:28 +01003538 << "' but found type '" << *field_type
Ian Rogersad0b3a32012-04-16 14:50:24 -07003539 << "' in get-object";
Sebastien Hertz5243e912013-05-21 10:55:07 +02003540 work_line_->SetRegisterType(vregA, reg_types_.Conflict());
Ian Rogersad0b3a32012-04-16 14:50:24 -07003541 return;
3542 }
3543 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003544 if (!field_type->IsLowHalf()) {
3545 work_line_->SetRegisterType(vregA, *field_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003546 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003547 work_line_->SetRegisterTypeWide(vregA, *field_type, field_type->HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003548 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003549}
3550
Sebastien Hertz5243e912013-05-21 10:55:07 +02003551void MethodVerifier::VerifyISPut(const Instruction* inst, const RegType& insn_type,
3552 bool is_primitive, bool is_static) {
3553 uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
Brian Carlstromea46f952013-07-30 01:26:50 -07003554 mirror::ArtField* field;
Ian Rogersb94a27b2011-10-26 00:33:41 -07003555 if (is_static) {
Ian Rogers55d249f2011-11-02 16:48:09 -07003556 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003557 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003558 const RegType& object_type = work_line_->GetRegisterType(inst->VRegB_22c());
Ian Rogers55d249f2011-11-02 16:48:09 -07003559 field = GetInstanceField(object_type, field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003560 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003561 const RegType* field_type = nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003562 if (field != NULL) {
3563 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
3564 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
3565 << " from other class " << GetDeclaringClass();
3566 return;
3567 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003568 FieldHelper fh(field);
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08003569 mirror::Class* field_type_class = fh.GetType(can_load_classes_);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003570 if (field_type_class != nullptr) {
3571 field_type = &reg_types_.FromClass(fh.GetTypeDescriptor(), field_type_class,
3572 field_type_class->CannotBeAssignedFromOtherTypes());
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08003573 } else {
3574 Thread* self = Thread::Current();
3575 DCHECK(!can_load_classes_ || self->IsExceptionPending());
3576 self->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003577 }
3578 }
3579 if (field_type == nullptr) {
3580 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3581 const char* descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
Mathieu Chartier590fee92013-09-13 13:46:47 -07003582 field_type = &reg_types_.FromDescriptor(class_loader_->get(), descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003583 }
Sebastien Hertz757b3042014-03-28 14:34:28 +01003584 DCHECK(field_type != nullptr);
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
Sebastien Hertz757b3042014-03-28 14:34:28 +01003592 << "' but found type '" << *field_type
Ian Rogersad0b3a32012-04-16 14:50:24 -07003593 << "' 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
Brian Carlstromea46f952013-07-30 01:26:50 -07003600mirror::ArtField* MethodVerifier::GetQuickFieldAccess(const Instruction* inst,
Ian Rogers98379392014-02-24 16:53:16 -08003601 RegisterLine* reg_line) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003602 DCHECK(inst->Opcode() == Instruction::IGET_QUICK ||
3603 inst->Opcode() == Instruction::IGET_WIDE_QUICK ||
3604 inst->Opcode() == Instruction::IGET_OBJECT_QUICK ||
3605 inst->Opcode() == Instruction::IPUT_QUICK ||
3606 inst->Opcode() == Instruction::IPUT_WIDE_QUICK ||
3607 inst->Opcode() == Instruction::IPUT_OBJECT_QUICK);
3608 const RegType& object_type = reg_line->GetRegisterType(inst->VRegB_22c());
Ian Rogers9bc54402014-04-17 16:40:01 -07003609 if (!object_type.HasClass()) {
3610 VLOG(verifier) << "Failed to get mirror::Class* from '" << object_type << "'";
3611 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003612 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003613 uint32_t field_offset = static_cast<uint32_t>(inst->VRegC_22c());
Sebastien Hertz479fc1e2014-04-04 17:51:34 +02003614 mirror::ArtField* f = mirror::ArtField::FindInstanceFieldWithOffset(object_type.GetClass(),
3615 field_offset);
3616 if (f == nullptr) {
3617 VLOG(verifier) << "Failed to find instance field at offset '" << field_offset
3618 << "' from '" << PrettyDescriptor(object_type.GetClass()) << "'";
3619 }
3620 return f;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003621}
3622
3623void MethodVerifier::VerifyIGetQuick(const Instruction* inst, const RegType& insn_type,
3624 bool is_primitive) {
3625 DCHECK(Runtime::Current()->IsStarted());
Brian Carlstromea46f952013-07-30 01:26:50 -07003626 mirror::ArtField* field = GetQuickFieldAccess(inst, work_line_.get());
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003627 if (field == NULL) {
3628 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field from " << inst->Name();
3629 return;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003630 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003631 FieldHelper fh(field);
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08003632 mirror::Class* field_type_class = fh.GetType(can_load_classes_);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003633 const RegType* field_type;
3634 if (field_type_class != nullptr) {
3635 field_type = &reg_types_.FromClass(fh.GetTypeDescriptor(), field_type_class,
3636 field_type_class->CannotBeAssignedFromOtherTypes());
3637 } else {
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08003638 Thread* self = Thread::Current();
3639 DCHECK(!can_load_classes_ || self->IsExceptionPending());
3640 self->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003641 field_type = &reg_types_.FromDescriptor(field->GetDeclaringClass()->GetClassLoader(),
3642 fh.GetTypeDescriptor(), false);
3643 }
Sebastien Hertz757b3042014-03-28 14:34:28 +01003644 DCHECK(field_type != nullptr);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003645 const uint32_t vregA = inst->VRegA_22c();
3646 if (is_primitive) {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003647 if (field_type->Equals(insn_type) ||
3648 (field_type->IsFloat() && insn_type.IsIntegralTypes()) ||
3649 (field_type->IsDouble() && insn_type.IsLongTypes())) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003650 // expected that read is of the correct primitive type or that int reads are reading
3651 // floats or long reads are reading doubles
3652 } else {
3653 // This is a global failure rather than a class change failure as the instructions and
3654 // the descriptors for the type should have been consistent within the same file at
3655 // compile time
3656 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003657 << " to be of type '" << insn_type
Sebastien Hertz757b3042014-03-28 14:34:28 +01003658 << "' but found type '" << *field_type << "' in get";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003659 return;
3660 }
3661 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003662 if (!insn_type.IsAssignableFrom(*field_type)) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003663 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003664 << " to be compatible with type '" << insn_type
Sebastien Hertz757b3042014-03-28 14:34:28 +01003665 << "' but found type '" << *field_type
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003666 << "' in get-object";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003667 work_line_->SetRegisterType(vregA, reg_types_.Conflict());
3668 return;
3669 }
3670 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003671 if (!field_type->IsLowHalf()) {
3672 work_line_->SetRegisterType(vregA, *field_type);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003673 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003674 work_line_->SetRegisterTypeWide(vregA, *field_type, field_type->HighHalf(&reg_types_));
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003675 }
3676}
3677
3678void MethodVerifier::VerifyIPutQuick(const Instruction* inst, const RegType& insn_type,
3679 bool is_primitive) {
3680 DCHECK(Runtime::Current()->IsStarted());
Brian Carlstromea46f952013-07-30 01:26:50 -07003681 mirror::ArtField* field = GetQuickFieldAccess(inst, work_line_.get());
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003682 if (field == NULL) {
3683 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field from " << inst->Name();
3684 return;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003685 }
3686 const char* descriptor = FieldHelper(field).GetTypeDescriptor();
3687 mirror::ClassLoader* loader = field->GetDeclaringClass()->GetClassLoader();
3688 const RegType& field_type = reg_types_.FromDescriptor(loader, descriptor, false);
3689 if (field != NULL) {
3690 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
3691 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003692 << " from other class " << GetDeclaringClass();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003693 return;
3694 }
3695 }
3696 const uint32_t vregA = inst->VRegA_22c();
3697 if (is_primitive) {
3698 // Primitive field assignability rules are weaker than regular assignability rules
3699 bool instruction_compatible;
3700 bool value_compatible;
3701 const RegType& value_type = work_line_->GetRegisterType(vregA);
3702 if (field_type.IsIntegralTypes()) {
3703 instruction_compatible = insn_type.IsIntegralTypes();
3704 value_compatible = value_type.IsIntegralTypes();
3705 } else if (field_type.IsFloat()) {
3706 instruction_compatible = insn_type.IsInteger(); // no [is]put-float, so expect [is]put-int
3707 value_compatible = value_type.IsFloatTypes();
3708 } else if (field_type.IsLong()) {
3709 instruction_compatible = insn_type.IsLong();
3710 value_compatible = value_type.IsLongTypes();
3711 } else if (field_type.IsDouble()) {
3712 instruction_compatible = insn_type.IsLong(); // no [is]put-double, so expect [is]put-long
3713 value_compatible = value_type.IsDoubleTypes();
3714 } else {
3715 instruction_compatible = false; // reference field with primitive store
3716 value_compatible = false; // unused
3717 }
3718 if (!instruction_compatible) {
3719 // This is a global failure rather than a class change failure as the instructions and
3720 // the descriptors for the type should have been consistent within the same file at
3721 // compile time
3722 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003723 << " to be of type '" << insn_type
3724 << "' but found type '" << field_type
3725 << "' in put";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003726 return;
3727 }
3728 if (!value_compatible) {
3729 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
3730 << " of type " << value_type
3731 << " but expected " << field_type
3732 << " for store to " << PrettyField(field) << " in put";
3733 return;
3734 }
3735 } else {
3736 if (!insn_type.IsAssignableFrom(field_type)) {
3737 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003738 << " to be compatible with type '" << insn_type
3739 << "' but found type '" << field_type
3740 << "' in put-object";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003741 return;
3742 }
3743 work_line_->VerifyRegisterType(vregA, field_type);
3744 }
3745}
3746
Ian Rogers776ac1f2012-04-13 23:36:36 -07003747bool MethodVerifier::CheckNotMoveException(const uint16_t* insns, int insn_idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003748 if ((insns[insn_idx] & 0xff) == Instruction::MOVE_EXCEPTION) {
jeffhaod5347e02012-03-22 17:25:05 -07003749 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-exception";
Ian Rogersd81871c2011-10-03 13:57:23 -07003750 return false;
3751 }
3752 return true;
3753}
3754
Ian Rogers776ac1f2012-04-13 23:36:36 -07003755bool MethodVerifier::UpdateRegisters(uint32_t next_insn, const RegisterLine* merge_line) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003756 bool changed = true;
3757 RegisterLine* target_line = reg_table_.GetLine(next_insn);
3758 if (!insn_flags_[next_insn].IsVisitedOrChanged()) {
jeffhaobdb76512011-09-07 11:43:16 -07003759 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003760 * We haven't processed this instruction before, and we haven't touched the registers here, so
3761 * there's nothing to "merge". Copy the registers over and mark it as changed. (This is the
3762 * only way a register can transition out of "unknown", so this is not just an optimization.)
jeffhaobdb76512011-09-07 11:43:16 -07003763 */
Ian Rogersb8c78592013-07-25 23:52:52 +00003764 if (!insn_flags_[next_insn].IsReturn()) {
3765 target_line->CopyFromLine(merge_line);
3766 } else {
Jeff Haob24b4a72013-07-31 13:47:31 -07003767 // Verify that the monitor stack is empty on return.
3768 if (!merge_line->VerifyMonitorStackEmpty()) {
3769 return false;
3770 }
Ian Rogersb8c78592013-07-25 23:52:52 +00003771 // For returns we only care about the operand to the return, all other registers are dead.
3772 // Initialize them as conflicts so they don't add to GC and deoptimization information.
3773 const Instruction* ret_inst = Instruction::At(code_item_->insns_ + next_insn);
3774 Instruction::Code opcode = ret_inst->Opcode();
3775 if ((opcode == Instruction::RETURN_VOID) || (opcode == Instruction::RETURN_VOID_BARRIER)) {
3776 target_line->MarkAllRegistersAsConflicts();
3777 } else {
3778 target_line->CopyFromLine(merge_line);
3779 if (opcode == Instruction::RETURN_WIDE) {
3780 target_line->MarkAllRegistersAsConflictsExceptWide(ret_inst->VRegA_11x());
3781 } else {
3782 target_line->MarkAllRegistersAsConflictsExcept(ret_inst->VRegA_11x());
3783 }
3784 }
3785 }
jeffhaobdb76512011-09-07 11:43:16 -07003786 } else {
Brian Carlstrom93c33962013-07-26 10:37:43 -07003787 UniquePtr<RegisterLine> copy(gDebugVerify ?
Ian Rogersd0fbd852013-09-24 18:17:04 -07003788 RegisterLine::Create(target_line->NumRegs(), this) :
Brian Carlstrom93c33962013-07-26 10:37:43 -07003789 NULL);
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003790 if (gDebugVerify) {
3791 copy->CopyFromLine(target_line);
3792 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003793 changed = target_line->MergeRegisters(merge_line);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003794 if (have_pending_hard_failure_) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003795 return false;
jeffhaobdb76512011-09-07 11:43:16 -07003796 }
Ian Rogers2c8a8572011-10-24 17:11:36 -07003797 if (gDebugVerify && changed) {
Elliott Hughes398f64b2012-03-26 18:05:48 -07003798 LogVerifyInfo() << "Merging at [" << reinterpret_cast<void*>(work_insn_idx_) << "]"
Elliott Hughesc073b072012-05-24 19:29:17 -07003799 << " to [" << reinterpret_cast<void*>(next_insn) << "]: " << "\n"
3800 << *copy.get() << " MERGE\n"
3801 << *merge_line << " ==\n"
3802 << *target_line << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07003803 }
3804 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003805 if (changed) {
3806 insn_flags_[next_insn].SetChanged();
jeffhaobdb76512011-09-07 11:43:16 -07003807 }
3808 return true;
3809}
3810
Ian Rogers7b3ddd22013-02-21 15:19:52 -08003811InstructionFlags* MethodVerifier::CurrentInsnFlags() {
Ian Rogers776ac1f2012-04-13 23:36:36 -07003812 return &insn_flags_[work_insn_idx_];
3813}
3814
Ian Rogersad0b3a32012-04-16 14:50:24 -07003815const RegType& MethodVerifier::GetMethodReturnType() {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003816 if (return_type_ == nullptr) {
3817 if (mirror_method_ != NULL) {
3818 MethodHelper mh(mirror_method_);
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08003819 mirror::Class* return_type_class = mh.GetReturnType(can_load_classes_);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003820 if (return_type_class != nullptr) {
Mathieu Chartier590fee92013-09-13 13:46:47 -07003821 return_type_ = &reg_types_.FromClass(mh.GetReturnTypeDescriptor(), return_type_class,
3822 return_type_class->CannotBeAssignedFromOtherTypes());
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003823 } else {
3824 Thread* self = Thread::Current();
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08003825 DCHECK(!can_load_classes_ || self->IsExceptionPending());
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003826 self->ClearException();
3827 }
3828 }
3829 if (return_type_ == nullptr) {
3830 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
3831 const DexFile::ProtoId& proto_id = dex_file_->GetMethodPrototype(method_id);
3832 uint16_t return_type_idx = proto_id.return_type_idx_;
3833 const char* descriptor = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(return_type_idx));
Mathieu Chartier590fee92013-09-13 13:46:47 -07003834 return_type_ = &reg_types_.FromDescriptor(class_loader_->get(), descriptor, false);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003835 }
3836 }
3837 return *return_type_;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003838}
3839
3840const RegType& MethodVerifier::GetDeclaringClass() {
Ian Rogers637c65b2013-05-31 11:46:00 -07003841 if (declaring_class_ == NULL) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003842 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
Brian Carlstrom93c33962013-07-26 10:37:43 -07003843 const char* descriptor
3844 = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(method_id.class_idx_));
Ian Rogers637c65b2013-05-31 11:46:00 -07003845 if (mirror_method_ != NULL) {
3846 mirror::Class* klass = mirror_method_->GetDeclaringClass();
Ian Rogers04f94f42013-06-10 15:09:26 -07003847 declaring_class_ = &reg_types_.FromClass(descriptor, klass,
3848 klass->CannotBeAssignedFromOtherTypes());
Ian Rogers637c65b2013-05-31 11:46:00 -07003849 } else {
Mathieu Chartier590fee92013-09-13 13:46:47 -07003850 declaring_class_ = &reg_types_.FromDescriptor(class_loader_->get(), descriptor, false);
Ian Rogers637c65b2013-05-31 11:46:00 -07003851 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003852 }
Ian Rogers637c65b2013-05-31 11:46:00 -07003853 return *declaring_class_;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003854}
3855
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003856std::vector<int32_t> MethodVerifier::DescribeVRegs(uint32_t dex_pc) {
3857 RegisterLine* line = reg_table_.GetLine(dex_pc);
Sebastien Hertzaa0c00c2014-03-14 17:58:54 +01003858 DCHECK(line != nullptr) << "No register line at DEX pc " << StringPrintf("0x%x", dex_pc);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003859 std::vector<int32_t> result;
3860 for (size_t i = 0; i < line->NumRegs(); ++i) {
3861 const RegType& type = line->GetRegisterType(i);
3862 if (type.IsConstant()) {
3863 result.push_back(type.IsPreciseConstant() ? kConstant : kImpreciseConstant);
3864 result.push_back(type.ConstantValue());
3865 } else if (type.IsConstantLo()) {
3866 result.push_back(type.IsPreciseConstantLo() ? kConstant : kImpreciseConstant);
3867 result.push_back(type.ConstantValueLo());
3868 } else if (type.IsConstantHi()) {
3869 result.push_back(type.IsPreciseConstantHi() ? kConstant : kImpreciseConstant);
3870 result.push_back(type.ConstantValueHi());
3871 } else if (type.IsIntegralTypes()) {
3872 result.push_back(kIntVReg);
3873 result.push_back(0);
3874 } else if (type.IsFloat()) {
3875 result.push_back(kFloatVReg);
3876 result.push_back(0);
3877 } else if (type.IsLong()) {
3878 result.push_back(kLongLoVReg);
3879 result.push_back(0);
3880 result.push_back(kLongHiVReg);
3881 result.push_back(0);
3882 ++i;
3883 } else if (type.IsDouble()) {
3884 result.push_back(kDoubleLoVReg);
3885 result.push_back(0);
3886 result.push_back(kDoubleHiVReg);
3887 result.push_back(0);
3888 ++i;
3889 } else if (type.IsUndefined() || type.IsConflict() || type.IsHighHalf()) {
3890 result.push_back(kUndefined);
3891 result.push_back(0);
3892 } else {
Ian Rogers7b3ddd22013-02-21 15:19:52 -08003893 CHECK(type.IsNonZeroReferenceTypes());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003894 result.push_back(kReferenceVReg);
3895 result.push_back(0);
3896 }
3897 }
3898 return result;
3899}
3900
Sebastien Hertz849600b2013-12-20 10:28:08 +01003901const RegType& MethodVerifier::DetermineCat1Constant(int32_t value, bool precise) {
3902 if (precise) {
3903 // Precise constant type.
3904 return reg_types_.FromCat1Const(value, true);
3905 } else {
3906 // Imprecise constant type.
3907 if (value < -32768) {
3908 return reg_types_.IntConstant();
3909 } else if (value < -128) {
3910 return reg_types_.ShortConstant();
3911 } else if (value < 0) {
3912 return reg_types_.ByteConstant();
3913 } else if (value == 0) {
3914 return reg_types_.Zero();
3915 } else if (value == 1) {
3916 return reg_types_.One();
3917 } else if (value < 128) {
3918 return reg_types_.PosByteConstant();
3919 } else if (value < 32768) {
3920 return reg_types_.PosShortConstant();
3921 } else if (value < 65536) {
3922 return reg_types_.CharConstant();
3923 } else {
3924 return reg_types_.IntConstant();
3925 }
3926 }
3927}
3928
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003929void MethodVerifier::Init() {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08003930 art::verifier::RegTypeCache::Init();
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003931}
3932
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003933void MethodVerifier::Shutdown() {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08003934 verifier::RegTypeCache::ShutDown();
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08003935}
jeffhaod1224c72012-02-29 13:43:08 -08003936
Mathieu Chartier83c8ee02014-01-28 14:50:23 -08003937void MethodVerifier::VisitRoots(RootCallback* callback, void* arg) {
3938 reg_types_.VisitRoots(callback, arg);
Mathieu Chartierc528dba2013-11-26 12:00:11 -08003939}
3940
Ian Rogersd81871c2011-10-03 13:57:23 -07003941} // namespace verifier
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07003942} // namespace art