blob: ab943a629e5e0930fb34e895d1c64c3195f8c6ee [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070016
Ian Rogers776ac1f2012-04-13 23:36:36 -070017#include "method_verifier.h"
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070018
Elliott Hughes1f359b02011-07-17 14:27:17 -070019#include <iostream>
20
Elliott Hughes07ed66b2012-12-12 18:34:25 -080021#include "base/logging.h"
Ian Rogers637c65b2013-05-31 11:46:00 -070022#include "base/mutex-inl.h"
Elliott Hughese222ee02012-12-13 14:41:43 -080023#include "base/stringpiece.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070024#include "class_linker.h"
Vladimir Marko2b5eaa22013-12-13 13:59:30 +000025#include "compiler_callbacks.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070026#include "dex_file-inl.h"
Ian Rogersd0583802013-06-01 10:51:46 -070027#include "dex_instruction-inl.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070028#include "dex_instruction_visitor.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070029#include "gc/accounting/card_table-inl.h"
Ian Rogers2bcb4a42012-11-08 10:39:18 -080030#include "indenter.h"
Ian Rogers84fa0742011-10-25 18:13:30 -070031#include "intern_table.h"
Ian Rogers0571d352011-11-03 19:51:38 -070032#include "leb128.h"
Brian Carlstromea46f952013-07-30 01:26:50 -070033#include "mirror/art_field-inl.h"
34#include "mirror/art_method-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080035#include "mirror/class.h"
36#include "mirror/class-inl.h"
Ian Rogers39ebcb82013-05-30 16:57:23 -070037#include "mirror/dex_cache-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080038#include "mirror/object-inl.h"
39#include "mirror/object_array-inl.h"
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080040#include "object_utils.h"
Ian Rogers39ebcb82013-05-30 16:57:23 -070041#include "register_line-inl.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070042#include "runtime.h"
Mathieu Chartier590fee92013-09-13 13:46:47 -070043#include "scoped_thread_state_change.h"
Elliott Hughese222ee02012-12-13 14:41:43 -080044#include "verifier/dex_gc_map.h"
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070045
46namespace art {
Ian Rogersd81871c2011-10-03 13:57:23 -070047namespace verifier {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070048
Ian Rogers2c8a8572011-10-24 17:11:36 -070049static const bool gDebugVerify = false;
Anwar Ghuloum75a43f12013-08-13 17:22:14 -070050// TODO: Add a constant to method_verifier to turn on verbose logging?
Ian Rogers2c8a8572011-10-24 17:11:36 -070051
Ian Rogers7b3ddd22013-02-21 15:19:52 -080052void PcToRegisterLineTable::Init(RegisterTrackingMode mode, InstructionFlags* flags,
Ian Rogersd81871c2011-10-03 13:57:23 -070053 uint32_t insns_size, uint16_t registers_size,
Ian Rogers776ac1f2012-04-13 23:36:36 -070054 MethodVerifier* verifier) {
Ian Rogersd81871c2011-10-03 13:57:23 -070055 DCHECK_GT(insns_size, 0U);
Ian Rogersd0fbd852013-09-24 18:17:04 -070056 register_lines_.reset(new RegisterLine*[insns_size]());
57 size_ = insns_size;
Ian Rogersd81871c2011-10-03 13:57:23 -070058 for (uint32_t i = 0; i < insns_size; i++) {
59 bool interesting = false;
60 switch (mode) {
61 case kTrackRegsAll:
62 interesting = flags[i].IsOpcode();
63 break;
Sameer Abu Asal02c42232013-04-30 12:09:45 -070064 case kTrackCompilerInterestPoints:
Brian Carlstrom02c8cc62013-07-18 15:54:44 -070065 interesting = flags[i].IsCompileTimeInfoPoint() || flags[i].IsBranchTarget();
Ian Rogersd81871c2011-10-03 13:57:23 -070066 break;
67 case kTrackRegsBranches:
68 interesting = flags[i].IsBranchTarget();
69 break;
70 default:
71 break;
72 }
73 if (interesting) {
Ian Rogersd0fbd852013-09-24 18:17:04 -070074 register_lines_[i] = RegisterLine::Create(registers_size, verifier);
75 }
76 }
77}
78
79PcToRegisterLineTable::~PcToRegisterLineTable() {
80 for (size_t i = 0; i < size_; i++) {
81 delete register_lines_[i];
82 if (kIsDebugBuild) {
83 register_lines_[i] = nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -070084 }
85 }
86}
87
Ian 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(),
364 mh.GetCodeItem(), m->GetDexMethodIndex(), m, m->GetAccessFlags(), false,
365 true);
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200366 return verifier.FindAccessedFieldAtDexPc(dex_pc);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200367}
368
Brian Carlstromea46f952013-07-30 01:26:50 -0700369mirror::ArtField* MethodVerifier::FindAccessedFieldAtDexPc(uint32_t dex_pc) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700370 CHECK(code_item_ != NULL); // This only makes sense for methods with code.
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200371
372 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
373 // verification. In practice, the phase we want relies on data structures set up by all the
374 // earlier passes, so we just run the full method verification and bail out early when we've
375 // got what we wanted.
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200376 bool success = Verify();
377 if (!success) {
378 return NULL;
379 }
380 RegisterLine* register_line = reg_table_.GetLine(dex_pc);
381 if (register_line == NULL) {
382 return NULL;
383 }
384 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
385 return GetQuickFieldAccess(inst, register_line);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200386}
387
Brian Carlstromea46f952013-07-30 01:26:50 -0700388mirror::ArtMethod* MethodVerifier::FindInvokedMethodAtDexPc(mirror::ArtMethod* m,
Mathieu Chartier590fee92013-09-13 13:46:47 -0700389 uint32_t dex_pc) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200390 MethodHelper mh(m);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700391 Thread* self = Thread::Current();
392 SirtRef<mirror::DexCache> dex_cache(self, mh.GetDexCache());
393 SirtRef<mirror::ClassLoader> class_loader(self, mh.GetClassLoader());
394 MethodVerifier verifier(&mh.GetDexFile(), &dex_cache, &class_loader, &mh.GetClassDef(),
395 mh.GetCodeItem(), m->GetDexMethodIndex(), m, m->GetAccessFlags(), false,
396 true);
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200397 return verifier.FindInvokedMethodAtDexPc(dex_pc);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200398}
399
Brian Carlstromea46f952013-07-30 01:26:50 -0700400mirror::ArtMethod* MethodVerifier::FindInvokedMethodAtDexPc(uint32_t dex_pc) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700401 CHECK(code_item_ != NULL); // This only makes sense for methods with code.
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200402
403 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
404 // verification. In practice, the phase we want relies on data structures set up by all the
405 // earlier passes, so we just run the full method verification and bail out early when we've
406 // got what we wanted.
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200407 bool success = Verify();
408 if (!success) {
409 return NULL;
410 }
411 RegisterLine* register_line = reg_table_.GetLine(dex_pc);
412 if (register_line == NULL) {
413 return NULL;
414 }
415 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
416 const bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
417 return GetQuickInvokedMethod(inst, register_line, is_range);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200418}
419
Ian Rogersad0b3a32012-04-16 14:50:24 -0700420bool MethodVerifier::Verify() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700421 // If there aren't any instructions, make sure that's expected, then exit successfully.
422 if (code_item_ == NULL) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700423 if ((method_access_flags_ & (kAccNative | kAccAbstract)) == 0) {
jeffhaod5347e02012-03-22 17:25:05 -0700424 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "zero-length code in concrete non-native method";
jeffhaobdb76512011-09-07 11:43:16 -0700425 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -0700426 } else {
427 return true;
jeffhaobdb76512011-09-07 11:43:16 -0700428 }
jeffhaobdb76512011-09-07 11:43:16 -0700429 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700430 // Sanity-check the register counts. ins + locals = registers, so make sure that ins <= registers.
431 if (code_item_->ins_size_ > code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700432 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad register counts (ins=" << code_item_->ins_size_
433 << " regs=" << code_item_->registers_size_;
Ian Rogersd81871c2011-10-03 13:57:23 -0700434 return false;
jeffhaobdb76512011-09-07 11:43:16 -0700435 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700436 // Allocate and initialize an array to hold instruction data.
Ian Rogers7b3ddd22013-02-21 15:19:52 -0800437 insn_flags_.reset(new InstructionFlags[code_item_->insns_size_in_code_units_]());
Ian Rogersd81871c2011-10-03 13:57:23 -0700438 // Run through the instructions and see if the width checks out.
439 bool result = ComputeWidthsAndCountOps();
440 // Flag instructions guarded by a "try" block and check exception handlers.
441 result = result && ScanTryCatchBlocks();
442 // Perform static instruction verification.
443 result = result && VerifyInstructions();
Ian Rogersad0b3a32012-04-16 14:50:24 -0700444 // Perform code-flow analysis and return.
Vladimir Marko2b5eaa22013-12-13 13:59:30 +0000445 result = result && VerifyCodeFlow();
446 // Compute information for compiler.
447 if (result && Runtime::Current()->IsCompiler()) {
448 result = Runtime::Current()->GetCompilerCallbacks()->MethodVerified(this);
449 }
450 return result;
jeffhaoba5ebb92011-08-25 17:24:37 -0700451}
452
Ian Rogers776ac1f2012-04-13 23:36:36 -0700453std::ostream& MethodVerifier::Fail(VerifyError error) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700454 switch (error) {
455 case VERIFY_ERROR_NO_CLASS:
456 case VERIFY_ERROR_NO_FIELD:
457 case VERIFY_ERROR_NO_METHOD:
458 case VERIFY_ERROR_ACCESS_CLASS:
459 case VERIFY_ERROR_ACCESS_FIELD:
460 case VERIFY_ERROR_ACCESS_METHOD:
Ian Rogers08f753d2012-08-24 14:35:25 -0700461 case VERIFY_ERROR_INSTANTIATION:
462 case VERIFY_ERROR_CLASS_CHANGE:
Elliott Hughes4993bbc2013-01-10 15:41:25 -0800463 if (Runtime::Current()->IsCompiler() || !can_load_classes_) {
jeffhaofaf459e2012-08-31 15:32:47 -0700464 // If we're optimistically running verification at compile time, turn NO_xxx, ACCESS_xxx,
465 // class change and instantiation errors into soft verification errors so that we re-verify
466 // at runtime. We may fail to find or to agree on access because of not yet available class
467 // loaders, or class loaders that will differ at runtime. In these cases, we don't want to
468 // affect the soundness of the code being compiled. Instead, the generated code runs "slow
469 // paths" that dynamically perform the verification and cause the behavior to be that akin
470 // to an interpreter.
471 error = VERIFY_ERROR_BAD_CLASS_SOFT;
472 } else {
Jeff Haoa3faaf42013-09-03 19:07:00 -0700473 // If we fail again at runtime, mark that this instruction would throw and force this
474 // method to be executed using the interpreter with checks.
jeffhaofaf459e2012-08-31 15:32:47 -0700475 have_pending_runtime_throw_failure_ = true;
476 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700477 break;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700478 // Indication that verification should be retried at runtime.
479 case VERIFY_ERROR_BAD_CLASS_SOFT:
Jeff Haoee988952013-04-16 14:23:47 -0700480 if (!allow_soft_failures_) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700481 have_pending_hard_failure_ = true;
482 }
483 break;
jeffhaod5347e02012-03-22 17:25:05 -0700484 // Hard verification failures at compile time will still fail at runtime, so the class is
485 // marked as rejected to prevent it from being compiled.
Ian Rogersad0b3a32012-04-16 14:50:24 -0700486 case VERIFY_ERROR_BAD_CLASS_HARD: {
487 if (Runtime::Current()->IsCompiler()) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700488 ClassReference ref(dex_file_, dex_file_->GetIndexForClassDef(*class_def_));
Vladimir Marko2b5eaa22013-12-13 13:59:30 +0000489 Runtime::Current()->GetCompilerCallbacks()->ClassRejected(ref);
jeffhaod1224c72012-02-29 13:43:08 -0800490 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700491 have_pending_hard_failure_ = true;
492 break;
Ian Rogers47a05882012-02-03 12:23:33 -0800493 }
494 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700495 failures_.push_back(error);
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800496 std::string location(StringPrintf("%s: [0x%X]", PrettyMethod(dex_method_idx_, *dex_file_).c_str(),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700497 work_insn_idx_));
498 std::ostringstream* failure_message = new std::ostringstream(location);
499 failure_messages_.push_back(failure_message);
500 return *failure_message;
501}
502
503void MethodVerifier::PrependToLastFailMessage(std::string prepend) {
504 size_t failure_num = failure_messages_.size();
505 DCHECK_NE(failure_num, 0U);
506 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
507 prepend += last_fail_message->str();
508 failure_messages_[failure_num - 1] = new std::ostringstream(prepend);
509 delete last_fail_message;
510}
511
512void MethodVerifier::AppendToLastFailMessage(std::string append) {
513 size_t failure_num = failure_messages_.size();
514 DCHECK_NE(failure_num, 0U);
515 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
516 (*last_fail_message) << append;
Ian Rogers47a05882012-02-03 12:23:33 -0800517}
518
Ian Rogers776ac1f2012-04-13 23:36:36 -0700519bool MethodVerifier::ComputeWidthsAndCountOps() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700520 const uint16_t* insns = code_item_->insns_;
521 size_t insns_size = code_item_->insns_size_in_code_units_;
522 const Instruction* inst = Instruction::At(insns);
jeffhaobdb76512011-09-07 11:43:16 -0700523 size_t new_instance_count = 0;
524 size_t monitor_enter_count = 0;
Ian Rogersd81871c2011-10-03 13:57:23 -0700525 size_t dex_pc = 0;
jeffhaobdb76512011-09-07 11:43:16 -0700526
Ian Rogersd81871c2011-10-03 13:57:23 -0700527 while (dex_pc < insns_size) {
jeffhaobdb76512011-09-07 11:43:16 -0700528 Instruction::Code opcode = inst->Opcode();
Ian Rogersa9a82542013-10-04 11:17:26 -0700529 switch (opcode) {
530 case Instruction::APUT_OBJECT:
531 case Instruction::CHECK_CAST:
532 has_check_casts_ = true;
533 break;
534 case Instruction::INVOKE_VIRTUAL:
535 case Instruction::INVOKE_VIRTUAL_RANGE:
536 case Instruction::INVOKE_INTERFACE:
537 case Instruction::INVOKE_INTERFACE_RANGE:
538 has_virtual_or_interface_invokes_ = true;
539 break;
540 case Instruction::MONITOR_ENTER:
541 monitor_enter_count++;
542 break;
543 case Instruction::NEW_INSTANCE:
544 new_instance_count++;
545 break;
546 default:
547 break;
jeffhaobdb76512011-09-07 11:43:16 -0700548 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700549 size_t inst_size = inst->SizeInCodeUnits();
550 insn_flags_[dex_pc].SetLengthInCodeUnits(inst_size);
551 dex_pc += inst_size;
jeffhaobdb76512011-09-07 11:43:16 -0700552 inst = inst->Next();
553 }
554
Ian Rogersd81871c2011-10-03 13:57:23 -0700555 if (dex_pc != insns_size) {
jeffhaod5347e02012-03-22 17:25:05 -0700556 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "code did not end where expected ("
557 << dex_pc << " vs. " << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700558 return false;
559 }
560
Ian Rogersd81871c2011-10-03 13:57:23 -0700561 new_instance_count_ = new_instance_count;
562 monitor_enter_count_ = monitor_enter_count;
jeffhaobdb76512011-09-07 11:43:16 -0700563 return true;
564}
565
Ian Rogers776ac1f2012-04-13 23:36:36 -0700566bool MethodVerifier::ScanTryCatchBlocks() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700567 uint32_t tries_size = code_item_->tries_size_;
jeffhaobdb76512011-09-07 11:43:16 -0700568 if (tries_size == 0) {
569 return true;
570 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700571 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Ian Rogers0571d352011-11-03 19:51:38 -0700572 const DexFile::TryItem* tries = DexFile::GetTryItems(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -0700573
574 for (uint32_t idx = 0; idx < tries_size; idx++) {
575 const DexFile::TryItem* try_item = &tries[idx];
576 uint32_t start = try_item->start_addr_;
577 uint32_t end = start + try_item->insn_count_;
jeffhaobdb76512011-09-07 11:43:16 -0700578 if ((start >= end) || (start >= insns_size) || (end > insns_size)) {
jeffhaod5347e02012-03-22 17:25:05 -0700579 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad exception entry: startAddr=" << start
580 << " endAddr=" << end << " (size=" << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700581 return false;
582 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700583 if (!insn_flags_[start].IsOpcode()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700584 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
585 << "'try' block starts inside an instruction (" << start << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700586 return false;
587 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700588 for (uint32_t dex_pc = start; dex_pc < end;
589 dex_pc += insn_flags_[dex_pc].GetLengthInCodeUnits()) {
590 insn_flags_[dex_pc].SetInTry();
jeffhaobdb76512011-09-07 11:43:16 -0700591 }
592 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800593 // Iterate over each of the handlers to verify target addresses.
Ian Rogers0571d352011-11-03 19:51:38 -0700594 const byte* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -0700595 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700596 ClassLinker* linker = Runtime::Current()->GetClassLinker();
jeffhaobdb76512011-09-07 11:43:16 -0700597 for (uint32_t idx = 0; idx < handlers_size; idx++) {
Ian Rogers0571d352011-11-03 19:51:38 -0700598 CatchHandlerIterator iterator(handlers_ptr);
599 for (; iterator.HasNext(); iterator.Next()) {
600 uint32_t dex_pc= iterator.GetHandlerAddress();
Ian Rogersd81871c2011-10-03 13:57:23 -0700601 if (!insn_flags_[dex_pc].IsOpcode()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700602 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
603 << "exception handler starts at bad address (" << dex_pc << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700604 return false;
605 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700606 insn_flags_[dex_pc].SetBranchTarget();
Ian Rogers28ad40d2011-10-27 15:19:26 -0700607 // Ensure exception types are resolved so that they don't need resolution to be delivered,
608 // unresolved exception types will be ignored by exception delivery
Ian Rogers0571d352011-11-03 19:51:38 -0700609 if (iterator.GetHandlerTypeIndex() != DexFile::kDexNoIndex16) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800610 mirror::Class* exception_type = linker->ResolveType(*dex_file_,
611 iterator.GetHandlerTypeIndex(),
Mathieu Chartier590fee92013-09-13 13:46:47 -0700612 *dex_cache_, *class_loader_);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700613 if (exception_type == NULL) {
614 DCHECK(Thread::Current()->IsExceptionPending());
615 Thread::Current()->ClearException();
616 }
617 }
jeffhaobdb76512011-09-07 11:43:16 -0700618 }
Ian Rogers0571d352011-11-03 19:51:38 -0700619 handlers_ptr = iterator.EndDataPointer();
jeffhaobdb76512011-09-07 11:43:16 -0700620 }
jeffhaobdb76512011-09-07 11:43:16 -0700621 return true;
622}
623
Ian Rogers776ac1f2012-04-13 23:36:36 -0700624bool MethodVerifier::VerifyInstructions() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700625 const Instruction* inst = Instruction::At(code_item_->insns_);
jeffhaoba5ebb92011-08-25 17:24:37 -0700626
Ian Rogers0c7abda2012-09-19 13:33:42 -0700627 /* Flag the start of the method as a branch target, and a GC point due to stack overflow errors */
Ian Rogersd81871c2011-10-03 13:57:23 -0700628 insn_flags_[0].SetBranchTarget();
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700629 insn_flags_[0].SetCompileTimeInfoPoint();
Ian Rogersd81871c2011-10-03 13:57:23 -0700630
631 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700632 for (uint32_t dex_pc = 0; dex_pc < insns_size;) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700633 if (!VerifyInstruction(inst, dex_pc)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700634 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -0700635 return false;
636 }
637 /* Flag instructions that are garbage collection points */
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700638 // All invoke points are marked as "Throw" points already.
639 // We are relying on this to also count all the invokes as interesting.
Ian Rogersb8c78592013-07-25 23:52:52 +0000640 if (inst->IsBranch() || inst->IsSwitch() || inst->IsThrow()) {
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700641 insn_flags_[dex_pc].SetCompileTimeInfoPoint();
Ian Rogersb8c78592013-07-25 23:52:52 +0000642 } else if (inst->IsReturn()) {
643 insn_flags_[dex_pc].SetCompileTimeInfoPointAndReturn();
Ian Rogersd81871c2011-10-03 13:57:23 -0700644 }
645 dex_pc += inst->SizeInCodeUnits();
646 inst = inst->Next();
647 }
648 return true;
649}
650
Ian Rogers776ac1f2012-04-13 23:36:36 -0700651bool MethodVerifier::VerifyInstruction(const Instruction* inst, uint32_t code_offset) {
Elliott Hughesadb8c672012-03-06 16:49:32 -0800652 DecodedInstruction dec_insn(inst);
Ian Rogersd81871c2011-10-03 13:57:23 -0700653 bool result = true;
654 switch (inst->GetVerifyTypeArgumentA()) {
655 case Instruction::kVerifyRegA:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800656 result = result && CheckRegisterIndex(dec_insn.vA);
Ian Rogersd81871c2011-10-03 13:57:23 -0700657 break;
658 case Instruction::kVerifyRegAWide:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800659 result = result && CheckWideRegisterIndex(dec_insn.vA);
Ian Rogersd81871c2011-10-03 13:57:23 -0700660 break;
661 }
662 switch (inst->GetVerifyTypeArgumentB()) {
663 case Instruction::kVerifyRegB:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800664 result = result && CheckRegisterIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700665 break;
666 case Instruction::kVerifyRegBField:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800667 result = result && CheckFieldIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700668 break;
669 case Instruction::kVerifyRegBMethod:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800670 result = result && CheckMethodIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700671 break;
672 case Instruction::kVerifyRegBNewInstance:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800673 result = result && CheckNewInstance(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700674 break;
675 case Instruction::kVerifyRegBString:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800676 result = result && CheckStringIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700677 break;
678 case Instruction::kVerifyRegBType:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800679 result = result && CheckTypeIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700680 break;
681 case Instruction::kVerifyRegBWide:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800682 result = result && CheckWideRegisterIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700683 break;
684 }
685 switch (inst->GetVerifyTypeArgumentC()) {
686 case Instruction::kVerifyRegC:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800687 result = result && CheckRegisterIndex(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700688 break;
689 case Instruction::kVerifyRegCField:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800690 result = result && CheckFieldIndex(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700691 break;
692 case Instruction::kVerifyRegCNewArray:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800693 result = result && CheckNewArray(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700694 break;
695 case Instruction::kVerifyRegCType:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800696 result = result && CheckTypeIndex(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700697 break;
698 case Instruction::kVerifyRegCWide:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800699 result = result && CheckWideRegisterIndex(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700700 break;
701 }
702 switch (inst->GetVerifyExtraFlags()) {
703 case Instruction::kVerifyArrayData:
704 result = result && CheckArrayData(code_offset);
705 break;
706 case Instruction::kVerifyBranchTarget:
707 result = result && CheckBranchTarget(code_offset);
708 break;
709 case Instruction::kVerifySwitchTargets:
710 result = result && CheckSwitchTargets(code_offset);
711 break;
712 case Instruction::kVerifyVarArg:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800713 result = result && CheckVarArgRegs(dec_insn.vA, dec_insn.arg);
Ian Rogersd81871c2011-10-03 13:57:23 -0700714 break;
715 case Instruction::kVerifyVarArgRange:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800716 result = result && CheckVarArgRangeRegs(dec_insn.vA, dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700717 break;
718 case Instruction::kVerifyError:
jeffhaod5347e02012-03-22 17:25:05 -0700719 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected opcode " << inst->Name();
Ian Rogersd81871c2011-10-03 13:57:23 -0700720 result = false;
721 break;
722 }
723 return result;
724}
725
Ian Rogers776ac1f2012-04-13 23:36:36 -0700726bool MethodVerifier::CheckRegisterIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700727 if (idx >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700728 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register index out of range (" << idx << " >= "
729 << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700730 return false;
731 }
732 return true;
733}
734
Ian Rogers776ac1f2012-04-13 23:36:36 -0700735bool MethodVerifier::CheckWideRegisterIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700736 if (idx + 1 >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700737 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "wide register index out of range (" << idx
738 << "+1 >= " << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700739 return false;
740 }
741 return true;
742}
743
Ian Rogers776ac1f2012-04-13 23:36:36 -0700744bool MethodVerifier::CheckFieldIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700745 if (idx >= dex_file_->GetHeader().field_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700746 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad field index " << idx << " (max "
747 << dex_file_->GetHeader().field_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700748 return false;
749 }
750 return true;
751}
752
Ian Rogers776ac1f2012-04-13 23:36:36 -0700753bool MethodVerifier::CheckMethodIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700754 if (idx >= dex_file_->GetHeader().method_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700755 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad method index " << idx << " (max "
756 << dex_file_->GetHeader().method_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700757 return false;
758 }
759 return true;
760}
761
Ian Rogers776ac1f2012-04-13 23:36:36 -0700762bool MethodVerifier::CheckNewInstance(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700763 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700764 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
765 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700766 return false;
767 }
768 // We don't need the actual class, just a pointer to the class name.
Ian Rogers0571d352011-11-03 19:51:38 -0700769 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -0700770 if (descriptor[0] != 'L') {
jeffhaod5347e02012-03-22 17:25:05 -0700771 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "can't call new-instance on type '" << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -0700772 return false;
773 }
774 return true;
775}
776
Ian Rogers776ac1f2012-04-13 23:36:36 -0700777bool MethodVerifier::CheckStringIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700778 if (idx >= dex_file_->GetHeader().string_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700779 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad string index " << idx << " (max "
780 << dex_file_->GetHeader().string_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700781 return false;
782 }
783 return true;
784}
785
Ian Rogers776ac1f2012-04-13 23:36:36 -0700786bool MethodVerifier::CheckTypeIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700787 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700788 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
789 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700790 return false;
791 }
792 return true;
793}
794
Ian Rogers776ac1f2012-04-13 23:36:36 -0700795bool MethodVerifier::CheckNewArray(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700796 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700797 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
798 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700799 return false;
800 }
801 int bracket_count = 0;
Ian Rogers0571d352011-11-03 19:51:38 -0700802 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -0700803 const char* cp = descriptor;
804 while (*cp++ == '[') {
805 bracket_count++;
806 }
807 if (bracket_count == 0) {
808 /* The given class must be an array type. */
Brian Carlstrom93c33962013-07-26 10:37:43 -0700809 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
810 << "can't new-array class '" << descriptor << "' (not an array)";
Ian Rogersd81871c2011-10-03 13:57:23 -0700811 return false;
812 } else if (bracket_count > 255) {
813 /* It is illegal to create an array of more than 255 dimensions. */
Brian Carlstrom93c33962013-07-26 10:37:43 -0700814 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
815 << "can't new-array class '" << descriptor << "' (exceeds limit)";
Ian Rogersd81871c2011-10-03 13:57:23 -0700816 return false;
817 }
818 return true;
819}
820
Ian Rogers776ac1f2012-04-13 23:36:36 -0700821bool MethodVerifier::CheckArrayData(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700822 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
823 const uint16_t* insns = code_item_->insns_ + cur_offset;
824 const uint16_t* array_data;
825 int32_t array_data_offset;
826
827 DCHECK_LT(cur_offset, insn_count);
828 /* make sure the start of the array data table is in range */
829 array_data_offset = insns[1] | (((int32_t) insns[2]) << 16);
830 if ((int32_t) cur_offset + array_data_offset < 0 ||
831 cur_offset + array_data_offset + 2 >= insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -0700832 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data start: at " << cur_offset
Brian Carlstrom93c33962013-07-26 10:37:43 -0700833 << ", data offset " << array_data_offset
834 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -0700835 return false;
836 }
837 /* offset to array data table is a relative branch-style offset */
838 array_data = insns + array_data_offset;
839 /* make sure the table is 32-bit aligned */
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 {
Mathieu Chartier590fee92013-09-13 13:46:47 -07001179 const RegType& reg_type = reg_types_.FromDescriptor(class_loader_->get(), descriptor,
1180 false);
Ian Rogers84fa0742011-10-25 18:13:30 -07001181 reg_line->SetRegisterType(arg_start + cur_arg, reg_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001182 }
1183 break;
1184 case 'Z':
1185 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Boolean());
1186 break;
1187 case 'C':
1188 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Char());
1189 break;
1190 case 'B':
1191 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Byte());
1192 break;
1193 case 'I':
1194 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Integer());
1195 break;
1196 case 'S':
1197 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Short());
1198 break;
1199 case 'F':
1200 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Float());
1201 break;
1202 case 'J':
1203 case 'D': {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001204 const RegType& lo_half = descriptor[0] == 'J' ? reg_types_.LongLo() : reg_types_.DoubleLo();
1205 const RegType& hi_half = descriptor[0] == 'J' ? reg_types_.LongHi() : reg_types_.DoubleHi();
1206 reg_line->SetRegisterTypeWide(arg_start + cur_arg, lo_half, hi_half);
Ian Rogersd81871c2011-10-03 13:57:23 -07001207 cur_arg++;
1208 break;
1209 }
1210 default:
Brian Carlstrom93c33962013-07-26 10:37:43 -07001211 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected signature type char '"
1212 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001213 return false;
1214 }
1215 cur_arg++;
1216 }
1217 if (cur_arg != expected_args) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001218 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1219 << " arguments, found " << cur_arg;
Ian Rogersd81871c2011-10-03 13:57:23 -07001220 return false;
1221 }
1222 const char* descriptor = dex_file_->GetReturnTypeDescriptor(proto_id);
1223 // Validate return type. We don't do the type lookup; just want to make sure that it has the right
1224 // format. Only major difference from the method argument format is that 'V' is supported.
1225 bool result;
1226 if (IsPrimitiveDescriptor(descriptor[0]) || descriptor[0] == 'V') {
1227 result = descriptor[1] == '\0';
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001228 } else if (descriptor[0] == '[') { // single/multi-dimensional array of object/primitive
Ian Rogersd81871c2011-10-03 13:57:23 -07001229 size_t i = 0;
1230 do {
1231 i++;
1232 } while (descriptor[i] == '['); // process leading [
1233 if (descriptor[i] == 'L') { // object array
1234 do {
1235 i++; // find closing ;
1236 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1237 result = descriptor[i] == ';';
1238 } else { // primitive array
1239 result = IsPrimitiveDescriptor(descriptor[i]) && descriptor[i + 1] == '\0';
1240 }
1241 } else if (descriptor[0] == 'L') {
1242 // could be more thorough here, but shouldn't be required
1243 size_t i = 0;
1244 do {
1245 i++;
1246 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1247 result = descriptor[i] == ';';
1248 } else {
1249 result = false;
1250 }
1251 if (!result) {
jeffhaod5347e02012-03-22 17:25:05 -07001252 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected char in return type descriptor '"
1253 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001254 }
1255 return result;
jeffhaobdb76512011-09-07 11:43:16 -07001256}
1257
Ian Rogers776ac1f2012-04-13 23:36:36 -07001258bool MethodVerifier::CodeFlowVerifyMethod() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001259 const uint16_t* insns = code_item_->insns_;
1260 const uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaoba5ebb92011-08-25 17:24:37 -07001261
jeffhaobdb76512011-09-07 11:43:16 -07001262 /* Begin by marking the first instruction as "changed". */
Ian Rogersd81871c2011-10-03 13:57:23 -07001263 insn_flags_[0].SetChanged();
1264 uint32_t start_guess = 0;
jeffhaoba5ebb92011-08-25 17:24:37 -07001265
jeffhaobdb76512011-09-07 11:43:16 -07001266 /* Continue until no instructions are marked "changed". */
1267 while (true) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001268 // Find the first marked one. Use "start_guess" as a way to find one quickly.
1269 uint32_t insn_idx = start_guess;
1270 for (; insn_idx < insns_size; insn_idx++) {
1271 if (insn_flags_[insn_idx].IsChanged())
jeffhaobdb76512011-09-07 11:43:16 -07001272 break;
1273 }
jeffhaobdb76512011-09-07 11:43:16 -07001274 if (insn_idx == insns_size) {
1275 if (start_guess != 0) {
1276 /* try again, starting from the top */
1277 start_guess = 0;
1278 continue;
1279 } else {
1280 /* all flags are clear */
1281 break;
1282 }
1283 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001284 // We carry the working set of registers from instruction to instruction. If this address can
1285 // be the target of a branch (or throw) instruction, or if we're skipping around chasing
1286 // "changed" flags, we need to load the set of registers from the table.
1287 // Because we always prefer to continue on to the next instruction, we should never have a
1288 // situation where we have a stray "changed" flag set on an instruction that isn't a branch
1289 // target.
1290 work_insn_idx_ = insn_idx;
1291 if (insn_flags_[insn_idx].IsBranchTarget()) {
1292 work_line_->CopyFromLine(reg_table_.GetLine(insn_idx));
jeffhaobdb76512011-09-07 11:43:16 -07001293 } else {
1294#ifndef NDEBUG
1295 /*
1296 * Sanity check: retrieve the stored register line (assuming
1297 * a full table) and make sure it actually matches.
1298 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001299 RegisterLine* register_line = reg_table_.GetLine(insn_idx);
1300 if (register_line != NULL) {
1301 if (work_line_->CompareLine(register_line) != 0) {
1302 Dump(std::cout);
1303 std::cout << info_messages_.str();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001304 LOG(FATAL) << "work_line diverged in " << PrettyMethod(dex_method_idx_, *dex_file_)
Elliott Hughesc073b072012-05-24 19:29:17 -07001305 << "@" << reinterpret_cast<void*>(work_insn_idx_) << "\n"
1306 << " work_line=" << *work_line_ << "\n"
Elliott Hughes398f64b2012-03-26 18:05:48 -07001307 << " expected=" << *register_line;
Ian Rogersd81871c2011-10-03 13:57:23 -07001308 }
jeffhaobdb76512011-09-07 11:43:16 -07001309 }
1310#endif
1311 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001312 if (!CodeFlowVerifyInstruction(&start_guess)) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001313 std::string prepend(PrettyMethod(dex_method_idx_, *dex_file_));
Ian Rogersad0b3a32012-04-16 14:50:24 -07001314 prepend += " failed to verify: ";
1315 PrependToLastFailMessage(prepend);
jeffhaoba5ebb92011-08-25 17:24:37 -07001316 return false;
1317 }
jeffhaobdb76512011-09-07 11:43:16 -07001318 /* Clear "changed" and mark as visited. */
Ian Rogersd81871c2011-10-03 13:57:23 -07001319 insn_flags_[insn_idx].SetVisited();
1320 insn_flags_[insn_idx].ClearChanged();
jeffhaobdb76512011-09-07 11:43:16 -07001321 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001322
Ian Rogers1c849e52012-06-28 14:00:33 -07001323 if (gDebugVerify) {
jeffhaobdb76512011-09-07 11:43:16 -07001324 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001325 * Scan for dead code. There's nothing "evil" about dead code
jeffhaobdb76512011-09-07 11:43:16 -07001326 * (besides the wasted space), but it indicates a flaw somewhere
1327 * down the line, possibly in the verifier.
1328 *
1329 * If we've substituted "always throw" instructions into the stream,
1330 * we are almost certainly going to have some dead code.
1331 */
1332 int dead_start = -1;
Ian Rogersd81871c2011-10-03 13:57:23 -07001333 uint32_t insn_idx = 0;
1334 for (; insn_idx < insns_size; insn_idx += insn_flags_[insn_idx].GetLengthInCodeUnits()) {
jeffhaobdb76512011-09-07 11:43:16 -07001335 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001336 * Switch-statement data doesn't get "visited" by scanner. It
jeffhaobdb76512011-09-07 11:43:16 -07001337 * may or may not be preceded by a padding NOP (for alignment).
1338 */
1339 if (insns[insn_idx] == Instruction::kPackedSwitchSignature ||
1340 insns[insn_idx] == Instruction::kSparseSwitchSignature ||
1341 insns[insn_idx] == Instruction::kArrayDataSignature ||
Elliott Hughes380aaa72012-07-09 14:33:15 -07001342 (insns[insn_idx] == Instruction::NOP && (insn_idx + 1 < insns_size) &&
jeffhaobdb76512011-09-07 11:43:16 -07001343 (insns[insn_idx + 1] == Instruction::kPackedSwitchSignature ||
1344 insns[insn_idx + 1] == Instruction::kSparseSwitchSignature ||
1345 insns[insn_idx + 1] == Instruction::kArrayDataSignature))) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001346 insn_flags_[insn_idx].SetVisited();
jeffhaobdb76512011-09-07 11:43:16 -07001347 }
1348
Ian Rogersd81871c2011-10-03 13:57:23 -07001349 if (!insn_flags_[insn_idx].IsVisited()) {
jeffhaobdb76512011-09-07 11:43:16 -07001350 if (dead_start < 0)
1351 dead_start = insn_idx;
1352 } else if (dead_start >= 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001353 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start)
1354 << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaobdb76512011-09-07 11:43:16 -07001355 dead_start = -1;
1356 }
1357 }
1358 if (dead_start >= 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001359 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start)
1360 << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaoba5ebb92011-08-25 17:24:37 -07001361 }
Ian Rogersc9e463c2013-06-05 16:52:26 -07001362 // To dump the state of the verify after a method, do something like:
1363 // if (PrettyMethod(dex_method_idx_, *dex_file_) ==
1364 // "boolean java.lang.String.equals(java.lang.Object)") {
1365 // LOG(INFO) << info_messages_.str();
1366 // }
jeffhaoba5ebb92011-08-25 17:24:37 -07001367 }
jeffhaobdb76512011-09-07 11:43:16 -07001368 return true;
1369}
1370
Ian Rogers776ac1f2012-04-13 23:36:36 -07001371bool MethodVerifier::CodeFlowVerifyInstruction(uint32_t* start_guess) {
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001372 // If we're doing FindLocksAtDexPc, check whether we're at the dex pc we care about.
1373 // We want the state _before_ the instruction, for the case where the dex pc we're
1374 // interested in is itself a monitor-enter instruction (which is a likely place
1375 // for a thread to be suspended).
1376 if (monitor_enter_dex_pcs_ != NULL && work_insn_idx_ == interesting_dex_pc_) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001377 monitor_enter_dex_pcs_->clear(); // The new work line is more accurate than the previous one.
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001378 for (size_t i = 0; i < work_line_->GetMonitorEnterCount(); ++i) {
1379 monitor_enter_dex_pcs_->push_back(work_line_->GetMonitorEnterDexPc(i));
1380 }
1381 }
1382
jeffhaobdb76512011-09-07 11:43:16 -07001383 /*
1384 * Once we finish decoding the instruction, we need to figure out where
jeffhaod1f0fde2011-09-08 17:25:33 -07001385 * we can go from here. There are three possible ways to transfer
jeffhaobdb76512011-09-07 11:43:16 -07001386 * control to another statement:
1387 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001388 * (1) Continue to the next instruction. Applies to all but
jeffhaobdb76512011-09-07 11:43:16 -07001389 * unconditional branches, method returns, and exception throws.
jeffhaod1f0fde2011-09-08 17:25:33 -07001390 * (2) Branch to one or more possible locations. Applies to branches
jeffhaobdb76512011-09-07 11:43:16 -07001391 * and switch statements.
jeffhaod1f0fde2011-09-08 17:25:33 -07001392 * (3) Exception handlers. Applies to any instruction that can
jeffhaobdb76512011-09-07 11:43:16 -07001393 * throw an exception that is handled by an encompassing "try"
1394 * block.
1395 *
1396 * We can also return, in which case there is no successor instruction
1397 * from this point.
1398 *
Elliott Hughesadb8c672012-03-06 16:49:32 -08001399 * The behavior can be determined from the opcode flags.
jeffhaobdb76512011-09-07 11:43:16 -07001400 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001401 const uint16_t* insns = code_item_->insns_ + work_insn_idx_;
1402 const Instruction* inst = Instruction::At(insns);
Ian Rogersa75a0132012-09-28 11:41:42 -07001403 int opcode_flags = Instruction::FlagsOf(inst->Opcode());
jeffhaobdb76512011-09-07 11:43:16 -07001404
jeffhaobdb76512011-09-07 11:43:16 -07001405 int32_t branch_target = 0;
jeffhaobdb76512011-09-07 11:43:16 -07001406 bool just_set_result = false;
Ian Rogers2c8a8572011-10-24 17:11:36 -07001407 if (gDebugVerify) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001408 // Generate processing back trace to debug verifier
Elliott Hughesc073b072012-05-24 19:29:17 -07001409 LogVerifyInfo() << "Processing " << inst->DumpString(dex_file_) << "\n"
1410 << *work_line_.get() << "\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001411 }
jeffhaobdb76512011-09-07 11:43:16 -07001412
1413 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001414 * Make a copy of the previous register state. If the instruction
jeffhaobdb76512011-09-07 11:43:16 -07001415 * can throw an exception, we will copy/merge this into the "catch"
1416 * address rather than work_line, because we don't want the result
1417 * from the "successful" code path (e.g. a check-cast that "improves"
1418 * a type) to be visible to the exception handler.
1419 */
Ian Rogers776ac1f2012-04-13 23:36:36 -07001420 if ((opcode_flags & Instruction::kThrow) != 0 && CurrentInsnFlags()->IsInTry()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001421 saved_line_->CopyFromLine(work_line_.get());
jeffhaobdb76512011-09-07 11:43:16 -07001422 } else {
1423#ifndef NDEBUG
Ian Rogersd81871c2011-10-03 13:57:23 -07001424 saved_line_->FillWithGarbage();
jeffhaobdb76512011-09-07 11:43:16 -07001425#endif
1426 }
1427
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07001428
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001429 // We need to ensure the work line is consistent while performing validation. When we spot a
1430 // peephole pattern we compute a new line for either the fallthrough instruction or the
1431 // branch target.
1432 UniquePtr<RegisterLine> branch_line;
1433 UniquePtr<RegisterLine> fallthrough_line;
1434
Sebastien Hertz849600b2013-12-20 10:28:08 +01001435 // We need precise constant types only for deoptimization which happens at runtime.
1436 const bool need_precise_constant = !Runtime::Current()->IsCompiler();
1437
Sebastien Hertz5243e912013-05-21 10:55:07 +02001438 switch (inst->Opcode()) {
jeffhaobdb76512011-09-07 11:43:16 -07001439 case Instruction::NOP:
1440 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001441 * A "pure" NOP has no effect on anything. Data tables start with
jeffhaobdb76512011-09-07 11:43:16 -07001442 * a signature that looks like a NOP; if we see one of these in
1443 * the course of executing code then we have a problem.
1444 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001445 if (inst->VRegA_10x() != 0) {
jeffhaod5347e02012-03-22 17:25:05 -07001446 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "encountered data table in instruction stream";
jeffhaobdb76512011-09-07 11:43:16 -07001447 }
1448 break;
1449
1450 case Instruction::MOVE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001451 work_line_->CopyRegister1(inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategory1nr);
1452 break;
jeffhaobdb76512011-09-07 11:43:16 -07001453 case Instruction::MOVE_FROM16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001454 work_line_->CopyRegister1(inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategory1nr);
1455 break;
jeffhaobdb76512011-09-07 11:43:16 -07001456 case Instruction::MOVE_16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001457 work_line_->CopyRegister1(inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategory1nr);
jeffhaobdb76512011-09-07 11:43:16 -07001458 break;
1459 case Instruction::MOVE_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001460 work_line_->CopyRegister2(inst->VRegA_12x(), inst->VRegB_12x());
1461 break;
jeffhaobdb76512011-09-07 11:43:16 -07001462 case Instruction::MOVE_WIDE_FROM16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001463 work_line_->CopyRegister2(inst->VRegA_22x(), inst->VRegB_22x());
1464 break;
jeffhaobdb76512011-09-07 11:43:16 -07001465 case Instruction::MOVE_WIDE_16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001466 work_line_->CopyRegister2(inst->VRegA_32x(), inst->VRegB_32x());
jeffhaobdb76512011-09-07 11:43:16 -07001467 break;
1468 case Instruction::MOVE_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001469 work_line_->CopyRegister1(inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategoryRef);
1470 break;
jeffhaobdb76512011-09-07 11:43:16 -07001471 case Instruction::MOVE_OBJECT_FROM16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001472 work_line_->CopyRegister1(inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategoryRef);
1473 break;
jeffhaobdb76512011-09-07 11:43:16 -07001474 case Instruction::MOVE_OBJECT_16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001475 work_line_->CopyRegister1(inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategoryRef);
jeffhaobdb76512011-09-07 11:43:16 -07001476 break;
1477
1478 /*
1479 * The move-result instructions copy data out of a "pseudo-register"
jeffhaod1f0fde2011-09-08 17:25:33 -07001480 * with the results from the last method invocation. In practice we
jeffhaobdb76512011-09-07 11:43:16 -07001481 * might want to hold the result in an actual CPU register, so the
1482 * Dalvik spec requires that these only appear immediately after an
1483 * invoke or filled-new-array.
1484 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001485 * These calls invalidate the "result" register. (This is now
jeffhaobdb76512011-09-07 11:43:16 -07001486 * redundant with the reset done below, but it can make the debug info
1487 * easier to read in some cases.)
1488 */
1489 case Instruction::MOVE_RESULT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001490 work_line_->CopyResultRegister1(inst->VRegA_11x(), false);
jeffhaobdb76512011-09-07 11:43:16 -07001491 break;
1492 case Instruction::MOVE_RESULT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001493 work_line_->CopyResultRegister2(inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07001494 break;
1495 case Instruction::MOVE_RESULT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001496 work_line_->CopyResultRegister1(inst->VRegA_11x(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001497 break;
1498
Ian Rogersd81871c2011-10-03 13:57:23 -07001499 case Instruction::MOVE_EXCEPTION: {
jeffhaobdb76512011-09-07 11:43:16 -07001500 /*
jeffhao60f83e32012-02-13 17:16:30 -08001501 * This statement can only appear as the first instruction in an exception handler. We verify
1502 * that as part of extracting the exception type from the catch block list.
jeffhaobdb76512011-09-07 11:43:16 -07001503 */
Ian Rogers28ad40d2011-10-27 15:19:26 -07001504 const RegType& res_type = GetCaughtExceptionType();
Sebastien Hertz5243e912013-05-21 10:55:07 +02001505 work_line_->SetRegisterType(inst->VRegA_11x(), res_type);
jeffhaobdb76512011-09-07 11:43:16 -07001506 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001507 }
jeffhaobdb76512011-09-07 11:43:16 -07001508 case Instruction::RETURN_VOID:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001509 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
1510 if (!GetMethodReturnType().IsConflict()) {
jeffhaod5347e02012-03-22 17:25:05 -07001511 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001512 }
jeffhaobdb76512011-09-07 11:43:16 -07001513 }
1514 break;
1515 case Instruction::RETURN:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001516 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
jeffhaobdb76512011-09-07 11:43:16 -07001517 /* check the method signature */
Ian Rogersd81871c2011-10-03 13:57:23 -07001518 const RegType& return_type = GetMethodReturnType();
1519 if (!return_type.IsCategory1Types()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001520 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected non-category 1 return type "
1521 << return_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001522 } else {
1523 // Compilers may generate synthetic functions that write byte values into boolean fields.
1524 // Also, it may use integer values for boolean, byte, short, and character return types.
Sebastien Hertz5243e912013-05-21 10:55:07 +02001525 const uint32_t vregA = inst->VRegA_11x();
1526 const RegType& src_type = work_line_->GetRegisterType(vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07001527 bool use_src = ((return_type.IsBoolean() && src_type.IsByte()) ||
1528 ((return_type.IsBoolean() || return_type.IsByte() ||
1529 return_type.IsShort() || return_type.IsChar()) &&
1530 src_type.IsInteger()));
1531 /* check the register contents */
Ian Rogersad0b3a32012-04-16 14:50:24 -07001532 bool success =
Sebastien Hertz5243e912013-05-21 10:55:07 +02001533 work_line_->VerifyRegisterType(vregA, use_src ? src_type : return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001534 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001535 AppendToLastFailMessage(StringPrintf(" return-1nr on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07001536 }
jeffhaobdb76512011-09-07 11:43:16 -07001537 }
1538 }
1539 break;
1540 case Instruction::RETURN_WIDE:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001541 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
jeffhaobdb76512011-09-07 11:43:16 -07001542 /* check the method signature */
Ian Rogersd81871c2011-10-03 13:57:23 -07001543 const RegType& return_type = GetMethodReturnType();
1544 if (!return_type.IsCategory2Types()) {
jeffhaod5347e02012-03-22 17:25:05 -07001545 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-wide not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001546 } else {
1547 /* check the register contents */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001548 const uint32_t vregA = inst->VRegA_11x();
1549 bool success = work_line_->VerifyRegisterType(vregA, return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001550 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001551 AppendToLastFailMessage(StringPrintf(" return-wide on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07001552 }
jeffhaobdb76512011-09-07 11:43:16 -07001553 }
1554 }
1555 break;
1556 case Instruction::RETURN_OBJECT:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001557 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001558 const RegType& return_type = GetMethodReturnType();
1559 if (!return_type.IsReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001560 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-object not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001561 } else {
1562 /* return_type is the *expected* return type, not register value */
1563 DCHECK(!return_type.IsZero());
1564 DCHECK(!return_type.IsUninitializedReference());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001565 const uint32_t vregA = inst->VRegA_11x();
1566 const RegType& reg_type = work_line_->GetRegisterType(vregA);
Ian Rogers9074b992011-10-26 17:41:55 -07001567 // Disallow returning uninitialized values and verify that the reference in vAA is an
1568 // instance of the "return_type"
1569 if (reg_type.IsUninitializedTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001570 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "returning uninitialized object '"
1571 << reg_type << "'";
Ian Rogers9074b992011-10-26 17:41:55 -07001572 } else if (!return_type.IsAssignableFrom(reg_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07001573 if (reg_type.IsUnresolvedTypes() || return_type.IsUnresolvedTypes()) {
1574 Fail(VERIFY_ERROR_NO_CLASS) << " can't resolve returned type '" << return_type
1575 << "' or '" << reg_type << "'";
1576 } else {
1577 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning '" << reg_type
1578 << "', but expected from declaration '" << return_type << "'";
1579 }
jeffhaobdb76512011-09-07 11:43:16 -07001580 }
1581 }
1582 }
1583 break;
1584
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001585 /* could be boolean, int, float, or a null reference */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001586 case Instruction::CONST_4: {
1587 int32_t val = static_cast<int32_t>(inst->VRegB_11n() << 28) >> 28;
Sebastien Hertz849600b2013-12-20 10:28:08 +01001588 work_line_->SetRegisterType(inst->VRegA_11n(),
1589 DetermineCat1Constant(val, need_precise_constant));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001590 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02001591 }
1592 case Instruction::CONST_16: {
1593 int16_t val = static_cast<int16_t>(inst->VRegB_21s());
Sebastien Hertz849600b2013-12-20 10:28:08 +01001594 work_line_->SetRegisterType(inst->VRegA_21s(),
1595 DetermineCat1Constant(val, need_precise_constant));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001596 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02001597 }
Sebastien Hertz849600b2013-12-20 10:28:08 +01001598 case Instruction::CONST: {
1599 int32_t val = inst->VRegB_31i();
Sebastien Hertz5243e912013-05-21 10:55:07 +02001600 work_line_->SetRegisterType(inst->VRegA_31i(),
Sebastien Hertz849600b2013-12-20 10:28:08 +01001601 DetermineCat1Constant(val, need_precise_constant));
jeffhaobdb76512011-09-07 11:43:16 -07001602 break;
Sebastien Hertz849600b2013-12-20 10:28:08 +01001603 }
1604 case Instruction::CONST_HIGH16: {
1605 int32_t val = static_cast<int32_t>(inst->VRegB_21h() << 16);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001606 work_line_->SetRegisterType(inst->VRegA_21h(),
Sebastien Hertz849600b2013-12-20 10:28:08 +01001607 DetermineCat1Constant(val, need_precise_constant));
jeffhaobdb76512011-09-07 11:43:16 -07001608 break;
Sebastien Hertz849600b2013-12-20 10:28:08 +01001609 }
jeffhaobdb76512011-09-07 11:43:16 -07001610 /* could be long or double; resolved upon use */
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001611 case Instruction::CONST_WIDE_16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001612 int64_t val = static_cast<int16_t>(inst->VRegB_21s());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001613 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1614 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001615 work_line_->SetRegisterTypeWide(inst->VRegA_21s(), lo, hi);
jeffhaobdb76512011-09-07 11:43:16 -07001616 break;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001617 }
1618 case Instruction::CONST_WIDE_32: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001619 int64_t val = static_cast<int32_t>(inst->VRegB_31i());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001620 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1621 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001622 work_line_->SetRegisterTypeWide(inst->VRegA_31i(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001623 break;
1624 }
1625 case Instruction::CONST_WIDE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001626 int64_t val = inst->VRegB_51l();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001627 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1628 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001629 work_line_->SetRegisterTypeWide(inst->VRegA_51l(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001630 break;
1631 }
1632 case Instruction::CONST_WIDE_HIGH16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001633 int64_t val = static_cast<uint64_t>(inst->VRegB_21h()) << 48;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001634 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1635 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001636 work_line_->SetRegisterTypeWide(inst->VRegA_21h(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001637 break;
1638 }
jeffhaobdb76512011-09-07 11:43:16 -07001639 case Instruction::CONST_STRING:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001640 work_line_->SetRegisterType(inst->VRegA_21c(), reg_types_.JavaLangString());
1641 break;
jeffhaobdb76512011-09-07 11:43:16 -07001642 case Instruction::CONST_STRING_JUMBO:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001643 work_line_->SetRegisterType(inst->VRegA_31c(), reg_types_.JavaLangString());
jeffhaobdb76512011-09-07 11:43:16 -07001644 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001645 case Instruction::CONST_CLASS: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001646 // Get type from instruction if unresolved then we need an access check
1647 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Sebastien Hertz5243e912013-05-21 10:55:07 +02001648 const RegType& res_type = ResolveClassAndCheckAccess(inst->VRegB_21c());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001649 // Register holds class, ie its type is class, on error it will hold Conflict.
Sebastien Hertz5243e912013-05-21 10:55:07 +02001650 work_line_->SetRegisterType(inst->VRegA_21c(),
Ian Rogersb4903572012-10-11 11:52:56 -07001651 res_type.IsConflict() ? res_type
1652 : reg_types_.JavaLangClass(true));
jeffhaobdb76512011-09-07 11:43:16 -07001653 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001654 }
jeffhaobdb76512011-09-07 11:43:16 -07001655 case Instruction::MONITOR_ENTER:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001656 work_line_->PushMonitor(inst->VRegA_11x(), work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07001657 break;
1658 case Instruction::MONITOR_EXIT:
1659 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001660 * monitor-exit instructions are odd. They can throw exceptions,
jeffhaobdb76512011-09-07 11:43:16 -07001661 * but when they do they act as if they succeeded and the PC is
jeffhaod1f0fde2011-09-08 17:25:33 -07001662 * pointing to the following instruction. (This behavior goes back
jeffhaobdb76512011-09-07 11:43:16 -07001663 * to the need to handle asynchronous exceptions, a now-deprecated
1664 * feature that Dalvik doesn't support.)
1665 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001666 * In practice we don't need to worry about this. The only
jeffhaobdb76512011-09-07 11:43:16 -07001667 * exceptions that can be thrown from monitor-exit are for a
jeffhaod1f0fde2011-09-08 17:25:33 -07001668 * null reference and -exit without a matching -enter. If the
jeffhaobdb76512011-09-07 11:43:16 -07001669 * structured locking checks are working, the former would have
1670 * failed on the -enter instruction, and the latter is impossible.
1671 *
1672 * This is fortunate, because issue 3221411 prevents us from
1673 * chasing the "can throw" path when monitor verification is
jeffhaod1f0fde2011-09-08 17:25:33 -07001674 * enabled. If we can fully verify the locking we can ignore
jeffhaobdb76512011-09-07 11:43:16 -07001675 * some catch blocks (which will show up as "dead" code when
1676 * we skip them here); if we can't, then the code path could be
1677 * "live" so we still need to check it.
1678 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001679 opcode_flags &= ~Instruction::kThrow;
Sebastien Hertz5243e912013-05-21 10:55:07 +02001680 work_line_->PopMonitor(inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07001681 break;
1682
Ian Rogers28ad40d2011-10-27 15:19:26 -07001683 case Instruction::CHECK_CAST:
Ian Rogersd81871c2011-10-03 13:57:23 -07001684 case Instruction::INSTANCE_OF: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001685 /*
1686 * If this instruction succeeds, we will "downcast" register vA to the type in vB. (This
1687 * could be a "upcast" -- not expected, so we don't try to address it.)
1688 *
1689 * If it fails, an exception is thrown, which we deal with later by ignoring the update to
Elliott Hughesadb8c672012-03-06 16:49:32 -08001690 * dec_insn.vA when branching to a handler.
Ian Rogers28ad40d2011-10-27 15:19:26 -07001691 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001692 const bool is_checkcast = (inst->Opcode() == Instruction::CHECK_CAST);
1693 const uint32_t type_idx = (is_checkcast) ? inst->VRegB_21c() : inst->VRegC_22c();
1694 const RegType& res_type = ResolveClassAndCheckAccess(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001695 if (res_type.IsConflict()) {
1696 DCHECK_NE(failures_.size(), 0U);
1697 if (!is_checkcast) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001698 work_line_->SetRegisterType(inst->VRegA_22c(), reg_types_.Boolean());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001699 }
1700 break; // bad class
Ian Rogers9f1ab122011-12-12 08:52:43 -08001701 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001702 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Sebastien Hertz5243e912013-05-21 10:55:07 +02001703 uint32_t orig_type_reg = (is_checkcast) ? inst->VRegA_21c() : inst->VRegB_22c();
1704 const RegType& orig_type = work_line_->GetRegisterType(orig_type_reg);
Ian Rogers28ad40d2011-10-27 15:19:26 -07001705 if (!res_type.IsNonZeroReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001706 if (is_checkcast) {
1707 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on unexpected class " << res_type;
1708 } else {
1709 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on unexpected class " << res_type;
1710 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001711 } else if (!orig_type.IsReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001712 if (is_checkcast) {
1713 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on non-reference in v" << orig_type_reg;
1714 } else {
1715 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on non-reference in v" << orig_type_reg;
1716 }
jeffhao2a8a90e2011-09-26 14:25:31 -07001717 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001718 if (is_checkcast) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001719 work_line_->SetRegisterType(inst->VRegA_21c(), res_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001720 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001721 work_line_->SetRegisterType(inst->VRegA_22c(), reg_types_.Boolean());
jeffhaobdb76512011-09-07 11:43:16 -07001722 }
jeffhaobdb76512011-09-07 11:43:16 -07001723 }
jeffhao2a8a90e2011-09-26 14:25:31 -07001724 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001725 }
1726 case Instruction::ARRAY_LENGTH: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001727 const RegType& res_type = work_line_->GetRegisterType(inst->VRegB_12x());
Ian Rogers28ad40d2011-10-27 15:19:26 -07001728 if (res_type.IsReferenceTypes()) {
Ian Rogers89310de2012-02-01 13:47:30 -08001729 if (!res_type.IsArrayTypes() && !res_type.IsZero()) { // ie not an array or null
jeffhaod5347e02012-03-22 17:25:05 -07001730 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001731 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001732 work_line_->SetRegisterType(inst->VRegA_12x(), reg_types_.Integer());
Ian Rogersd81871c2011-10-03 13:57:23 -07001733 }
1734 }
1735 break;
1736 }
1737 case Instruction::NEW_INSTANCE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001738 const RegType& res_type = ResolveClassAndCheckAccess(inst->VRegB_21c());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001739 if (res_type.IsConflict()) {
1740 DCHECK_NE(failures_.size(), 0U);
1741 break; // bad class
jeffhao8cd6dda2012-02-22 10:15:34 -08001742 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001743 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
1744 // can't create an instance of an interface or abstract class */
1745 if (!res_type.IsInstantiableTypes()) {
1746 Fail(VERIFY_ERROR_INSTANTIATION)
1747 << "new-instance on primitive, interface or abstract class" << res_type;
Ian Rogers08f753d2012-08-24 14:35:25 -07001748 // Soft failure so carry on to set register type.
Ian Rogersd81871c2011-10-03 13:57:23 -07001749 }
Ian Rogers08f753d2012-08-24 14:35:25 -07001750 const RegType& uninit_type = reg_types_.Uninitialized(res_type, work_insn_idx_);
1751 // Any registers holding previous allocations from this address that have not yet been
1752 // initialized must be marked invalid.
1753 work_line_->MarkUninitRefsAsInvalid(uninit_type);
1754 // add the new uninitialized reference to the register state
Sebastien Hertz5243e912013-05-21 10:55:07 +02001755 work_line_->SetRegisterType(inst->VRegA_21c(), uninit_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001756 break;
1757 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08001758 case Instruction::NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001759 VerifyNewArray(inst, false, false);
jeffhaobdb76512011-09-07 11:43:16 -07001760 break;
1761 case Instruction::FILLED_NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001762 VerifyNewArray(inst, true, false);
Ian Rogers0c4a5062012-02-03 15:18:59 -08001763 just_set_result = true; // Filled new array sets result register
jeffhaobdb76512011-09-07 11:43:16 -07001764 break;
Ian Rogers0c4a5062012-02-03 15:18:59 -08001765 case Instruction::FILLED_NEW_ARRAY_RANGE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001766 VerifyNewArray(inst, true, true);
Ian Rogers0c4a5062012-02-03 15:18:59 -08001767 just_set_result = true; // Filled new array range sets result register
1768 break;
jeffhaobdb76512011-09-07 11:43:16 -07001769 case Instruction::CMPL_FLOAT:
1770 case Instruction::CMPG_FLOAT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001771 if (!work_line_->VerifyRegisterType(inst->VRegB_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08001772 break;
1773 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02001774 if (!work_line_->VerifyRegisterType(inst->VRegC_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08001775 break;
1776 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02001777 work_line_->SetRegisterType(inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001778 break;
1779 case Instruction::CMPL_DOUBLE:
1780 case Instruction::CMPG_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001781 if (!work_line_->VerifyRegisterTypeWide(inst->VRegB_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001782 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001783 break;
1784 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02001785 if (!work_line_->VerifyRegisterTypeWide(inst->VRegC_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001786 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001787 break;
1788 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02001789 work_line_->SetRegisterType(inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001790 break;
1791 case Instruction::CMP_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001792 if (!work_line_->VerifyRegisterTypeWide(inst->VRegB_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001793 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001794 break;
1795 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02001796 if (!work_line_->VerifyRegisterTypeWide(inst->VRegC_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001797 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001798 break;
1799 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02001800 work_line_->SetRegisterType(inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001801 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001802 case Instruction::THROW: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001803 const RegType& res_type = work_line_->GetRegisterType(inst->VRegA_11x());
Ian Rogersb4903572012-10-11 11:52:56 -07001804 if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(res_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07001805 Fail(res_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS : VERIFY_ERROR_BAD_CLASS_SOFT)
1806 << "thrown class " << res_type << " not instanceof Throwable";
jeffhaobdb76512011-09-07 11:43:16 -07001807 }
1808 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001809 }
jeffhaobdb76512011-09-07 11:43:16 -07001810 case Instruction::GOTO:
1811 case Instruction::GOTO_16:
1812 case Instruction::GOTO_32:
1813 /* no effect on or use of registers */
1814 break;
1815
1816 case Instruction::PACKED_SWITCH:
1817 case Instruction::SPARSE_SWITCH:
1818 /* verify that vAA is an integer, or can be converted to one */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001819 work_line_->VerifyRegisterType(inst->VRegA_31t(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001820 break;
1821
Ian Rogersd81871c2011-10-03 13:57:23 -07001822 case Instruction::FILL_ARRAY_DATA: {
1823 /* Similar to the verification done for APUT */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001824 const RegType& array_type = work_line_->GetRegisterType(inst->VRegA_31t());
Ian Rogers89310de2012-02-01 13:47:30 -08001825 /* array_type can be null if the reg type is Zero */
1826 if (!array_type.IsZero()) {
jeffhao457cc512012-02-02 16:55:13 -08001827 if (!array_type.IsArrayTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001828 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with array type "
1829 << array_type;
Ian Rogers89310de2012-02-01 13:47:30 -08001830 } else {
Mathieu Chartier590fee92013-09-13 13:46:47 -07001831 const RegType& component_type = reg_types_.GetComponentType(array_type,
1832 class_loader_->get());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001833 DCHECK(!component_type.IsConflict());
jeffhao457cc512012-02-02 16:55:13 -08001834 if (component_type.IsNonZeroReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001835 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with component type "
1836 << component_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001837 } else {
jeffhao457cc512012-02-02 16:55:13 -08001838 // Now verify if the element width in the table matches the element width declared in
1839 // the array
1840 const uint16_t* array_data = insns + (insns[1] | (((int32_t) insns[2]) << 16));
1841 if (array_data[0] != Instruction::kArrayDataSignature) {
jeffhaod5347e02012-03-22 17:25:05 -07001842 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid magic for array-data";
jeffhao457cc512012-02-02 16:55:13 -08001843 } else {
1844 size_t elem_width = Primitive::ComponentSize(component_type.GetPrimitiveType());
1845 // Since we don't compress the data in Dex, expect to see equal width of data stored
1846 // in the table and expected from the array class.
1847 if (array_data[1] != elem_width) {
jeffhaod5347e02012-03-22 17:25:05 -07001848 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-data size mismatch (" << array_data[1]
1849 << " vs " << elem_width << ")";
jeffhao457cc512012-02-02 16:55:13 -08001850 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001851 }
1852 }
jeffhaobdb76512011-09-07 11:43:16 -07001853 }
1854 }
1855 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001856 }
jeffhaobdb76512011-09-07 11:43:16 -07001857 case Instruction::IF_EQ:
Ian Rogersd81871c2011-10-03 13:57:23 -07001858 case Instruction::IF_NE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001859 const RegType& reg_type1 = work_line_->GetRegisterType(inst->VRegA_22t());
1860 const RegType& reg_type2 = work_line_->GetRegisterType(inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07001861 bool mismatch = false;
1862 if (reg_type1.IsZero()) { // zero then integral or reference expected
1863 mismatch = !reg_type2.IsReferenceTypes() && !reg_type2.IsIntegralTypes();
1864 } else if (reg_type1.IsReferenceTypes()) { // both references?
1865 mismatch = !reg_type2.IsReferenceTypes();
1866 } else { // both integral?
1867 mismatch = !reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes();
1868 }
1869 if (mismatch) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001870 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to if-eq/if-ne (" << reg_type1 << ","
1871 << reg_type2 << ") must both be references or integral";
jeffhaobdb76512011-09-07 11:43:16 -07001872 }
1873 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001874 }
jeffhaobdb76512011-09-07 11:43:16 -07001875 case Instruction::IF_LT:
1876 case Instruction::IF_GE:
1877 case Instruction::IF_GT:
Ian Rogersd81871c2011-10-03 13:57:23 -07001878 case Instruction::IF_LE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001879 const RegType& reg_type1 = work_line_->GetRegisterType(inst->VRegA_22t());
1880 const RegType& reg_type2 = work_line_->GetRegisterType(inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07001881 if (!reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001882 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to 'if' (" << reg_type1 << ","
1883 << reg_type2 << ") must be integral";
jeffhaobdb76512011-09-07 11:43:16 -07001884 }
1885 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001886 }
jeffhaobdb76512011-09-07 11:43:16 -07001887 case Instruction::IF_EQZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07001888 case Instruction::IF_NEZ: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001889 const RegType& reg_type = work_line_->GetRegisterType(inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07001890 if (!reg_type.IsReferenceTypes() && !reg_type.IsIntegralTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001891 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
1892 << " unexpected as arg to if-eqz/if-nez";
Ian Rogersd81871c2011-10-03 13:57:23 -07001893 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001894
1895 // Find previous instruction - its existence is a precondition to peephole optimization.
Ian Rogers9b360392013-06-06 14:45:07 -07001896 uint32_t instance_of_idx = 0;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001897 if (0 != work_insn_idx_) {
Ian Rogers9b360392013-06-06 14:45:07 -07001898 instance_of_idx = work_insn_idx_ - 1;
Brian Carlstromdf629502013-07-17 22:39:56 -07001899 while (0 != instance_of_idx && !insn_flags_[instance_of_idx].IsOpcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07001900 instance_of_idx--;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001901 }
Ian Rogers9b360392013-06-06 14:45:07 -07001902 CHECK(insn_flags_[instance_of_idx].IsOpcode());
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001903 } else {
1904 break;
1905 }
1906
Ian Rogers9b360392013-06-06 14:45:07 -07001907 const Instruction* instance_of_inst = Instruction::At(code_item_->insns_ + instance_of_idx);
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001908
1909 /* Check for peep-hole pattern of:
1910 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07001911 * instance-of vX, vY, T;
1912 * ifXXX vX, label ;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001913 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07001914 * label:
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001915 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07001916 * and sharpen the type of vY to be type T.
1917 * Note, this pattern can't be if:
1918 * - if there are other branches to this branch,
1919 * - when vX == vY.
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001920 */
Ian Rogersfae370a2013-06-05 08:33:27 -07001921 if (!CurrentInsnFlags()->IsBranchTarget() &&
Ian Rogers9b360392013-06-06 14:45:07 -07001922 (Instruction::INSTANCE_OF == instance_of_inst->Opcode()) &&
1923 (inst->VRegA_21t() == instance_of_inst->VRegA_22c()) &&
1924 (instance_of_inst->VRegA_22c() != instance_of_inst->VRegB_22c())) {
Ian Rogersfae370a2013-06-05 08:33:27 -07001925 // Check that the we are not attempting conversion to interface types,
1926 // which is not done because of the multiple inheritance implications.
Jeff Haoc642ec82013-09-04 16:11:55 -07001927 // Also don't change the type if it would result in an upcast.
1928 const RegType& orig_type = work_line_->GetRegisterType(instance_of_inst->VRegB_22c());
Ian Rogers9b360392013-06-06 14:45:07 -07001929 const RegType& cast_type = ResolveClassAndCheckAccess(instance_of_inst->VRegC_22c());
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001930
Jeff Haoa3faaf42013-09-03 19:07:00 -07001931 if (!cast_type.IsUnresolvedTypes() && !orig_type.IsUnresolvedTypes() &&
1932 !cast_type.GetClass()->IsInterface() && !cast_type.IsAssignableFrom(orig_type)) {
Ian Rogersd0fbd852013-09-24 18:17:04 -07001933 RegisterLine* update_line = RegisterLine::Create(code_item_->registers_size_, this);
Ian Rogersfae370a2013-06-05 08:33:27 -07001934 if (inst->Opcode() == Instruction::IF_EQZ) {
Ian Rogers9b360392013-06-06 14:45:07 -07001935 fallthrough_line.reset(update_line);
Ian Rogersfae370a2013-06-05 08:33:27 -07001936 } else {
Ian Rogers9b360392013-06-06 14:45:07 -07001937 branch_line.reset(update_line);
1938 }
1939 update_line->CopyFromLine(work_line_.get());
1940 update_line->SetRegisterType(instance_of_inst->VRegB_22c(), cast_type);
1941 if (!insn_flags_[instance_of_idx].IsBranchTarget() && 0 != instance_of_idx) {
1942 // See if instance-of was preceded by a move-object operation, common due to the small
1943 // register encoding space of instance-of, and propagate type information to the source
1944 // of the move-object.
1945 uint32_t move_idx = instance_of_idx - 1;
Brian Carlstromdf629502013-07-17 22:39:56 -07001946 while (0 != move_idx && !insn_flags_[move_idx].IsOpcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07001947 move_idx--;
1948 }
1949 CHECK(insn_flags_[move_idx].IsOpcode());
1950 const Instruction* move_inst = Instruction::At(code_item_->insns_ + move_idx);
1951 switch (move_inst->Opcode()) {
1952 case Instruction::MOVE_OBJECT:
1953 if (move_inst->VRegA_12x() == instance_of_inst->VRegB_22c()) {
1954 update_line->SetRegisterType(move_inst->VRegB_12x(), cast_type);
1955 }
1956 break;
1957 case Instruction::MOVE_OBJECT_FROM16:
1958 if (move_inst->VRegA_22x() == instance_of_inst->VRegB_22c()) {
1959 update_line->SetRegisterType(move_inst->VRegB_22x(), cast_type);
1960 }
1961 break;
1962 case Instruction::MOVE_OBJECT_16:
1963 if (move_inst->VRegA_32x() == instance_of_inst->VRegB_22c()) {
1964 update_line->SetRegisterType(move_inst->VRegB_32x(), cast_type);
1965 }
1966 break;
1967 default:
1968 break;
1969 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001970 }
1971 }
1972 }
1973
jeffhaobdb76512011-09-07 11:43:16 -07001974 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001975 }
jeffhaobdb76512011-09-07 11:43:16 -07001976 case Instruction::IF_LTZ:
1977 case Instruction::IF_GEZ:
1978 case Instruction::IF_GTZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07001979 case Instruction::IF_LEZ: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001980 const RegType& reg_type = work_line_->GetRegisterType(inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07001981 if (!reg_type.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001982 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
1983 << " unexpected as arg to if-ltz/if-gez/if-gtz/if-lez";
Ian Rogersd81871c2011-10-03 13:57:23 -07001984 }
jeffhaobdb76512011-09-07 11:43:16 -07001985 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001986 }
jeffhaobdb76512011-09-07 11:43:16 -07001987 case Instruction::AGET_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001988 VerifyAGet(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001989 break;
jeffhaobdb76512011-09-07 11:43:16 -07001990 case Instruction::AGET_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001991 VerifyAGet(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001992 break;
jeffhaobdb76512011-09-07 11:43:16 -07001993 case Instruction::AGET_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001994 VerifyAGet(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001995 break;
jeffhaobdb76512011-09-07 11:43:16 -07001996 case Instruction::AGET_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001997 VerifyAGet(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001998 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001999 case Instruction::AGET:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002000 VerifyAGet(inst, reg_types_.Integer(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002001 break;
jeffhaobdb76512011-09-07 11:43:16 -07002002 case Instruction::AGET_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002003 VerifyAGet(inst, reg_types_.LongLo(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002004 break;
2005 case Instruction::AGET_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002006 VerifyAGet(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07002007 break;
2008
Ian Rogersd81871c2011-10-03 13:57:23 -07002009 case Instruction::APUT_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002010 VerifyAPut(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002011 break;
2012 case Instruction::APUT_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002013 VerifyAPut(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002014 break;
2015 case Instruction::APUT_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002016 VerifyAPut(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002017 break;
2018 case Instruction::APUT_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002019 VerifyAPut(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002020 break;
2021 case Instruction::APUT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002022 VerifyAPut(inst, reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002023 break;
2024 case Instruction::APUT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002025 VerifyAPut(inst, reg_types_.LongLo(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002026 break;
2027 case Instruction::APUT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002028 VerifyAPut(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07002029 break;
2030
jeffhaobdb76512011-09-07 11:43:16 -07002031 case Instruction::IGET_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002032 VerifyISGet(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002033 break;
jeffhaobdb76512011-09-07 11:43:16 -07002034 case Instruction::IGET_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002035 VerifyISGet(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002036 break;
jeffhaobdb76512011-09-07 11:43:16 -07002037 case Instruction::IGET_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002038 VerifyISGet(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002039 break;
jeffhaobdb76512011-09-07 11:43:16 -07002040 case Instruction::IGET_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002041 VerifyISGet(inst, reg_types_.Short(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002042 break;
2043 case Instruction::IGET:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002044 VerifyISGet(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002045 break;
2046 case Instruction::IGET_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002047 VerifyISGet(inst, reg_types_.LongLo(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002048 break;
2049 case Instruction::IGET_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002050 VerifyISGet(inst, reg_types_.JavaLangObject(false), false, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002051 break;
jeffhaobdb76512011-09-07 11:43:16 -07002052
Ian Rogersd81871c2011-10-03 13:57:23 -07002053 case Instruction::IPUT_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002054 VerifyISPut(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002055 break;
2056 case Instruction::IPUT_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002057 VerifyISPut(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002058 break;
2059 case Instruction::IPUT_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002060 VerifyISPut(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002061 break;
2062 case Instruction::IPUT_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002063 VerifyISPut(inst, reg_types_.Short(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002064 break;
2065 case Instruction::IPUT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002066 VerifyISPut(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002067 break;
2068 case Instruction::IPUT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002069 VerifyISPut(inst, reg_types_.LongLo(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002070 break;
jeffhaobdb76512011-09-07 11:43:16 -07002071 case Instruction::IPUT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002072 VerifyISPut(inst, reg_types_.JavaLangObject(false), false, false);
jeffhaobdb76512011-09-07 11:43:16 -07002073 break;
2074
jeffhaobdb76512011-09-07 11:43:16 -07002075 case Instruction::SGET_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002076 VerifyISGet(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002077 break;
jeffhaobdb76512011-09-07 11:43:16 -07002078 case Instruction::SGET_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002079 VerifyISGet(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002080 break;
jeffhaobdb76512011-09-07 11:43:16 -07002081 case Instruction::SGET_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002082 VerifyISGet(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002083 break;
jeffhaobdb76512011-09-07 11:43:16 -07002084 case Instruction::SGET_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002085 VerifyISGet(inst, reg_types_.Short(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002086 break;
2087 case Instruction::SGET:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002088 VerifyISGet(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002089 break;
2090 case Instruction::SGET_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002091 VerifyISGet(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002092 break;
2093 case Instruction::SGET_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002094 VerifyISGet(inst, reg_types_.JavaLangObject(false), false, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002095 break;
2096
2097 case Instruction::SPUT_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002098 VerifyISPut(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002099 break;
2100 case Instruction::SPUT_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002101 VerifyISPut(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002102 break;
2103 case Instruction::SPUT_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002104 VerifyISPut(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002105 break;
2106 case Instruction::SPUT_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002107 VerifyISPut(inst, reg_types_.Short(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002108 break;
2109 case Instruction::SPUT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002110 VerifyISPut(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002111 break;
2112 case Instruction::SPUT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002113 VerifyISPut(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002114 break;
2115 case Instruction::SPUT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002116 VerifyISPut(inst, reg_types_.JavaLangObject(false), false, true);
jeffhaobdb76512011-09-07 11:43:16 -07002117 break;
2118
2119 case Instruction::INVOKE_VIRTUAL:
2120 case Instruction::INVOKE_VIRTUAL_RANGE:
2121 case Instruction::INVOKE_SUPER:
Ian Rogersd81871c2011-10-03 13:57:23 -07002122 case Instruction::INVOKE_SUPER_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002123 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE ||
2124 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
2125 bool is_super = (inst->Opcode() == Instruction::INVOKE_SUPER ||
2126 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
Brian Carlstromea46f952013-07-30 01:26:50 -07002127 mirror::ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_VIRTUAL,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002128 is_range, is_super);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002129 const RegType* return_type = nullptr;
2130 if (called_method != nullptr) {
2131 MethodHelper mh(called_method);
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08002132 mirror::Class* return_type_class = mh.GetReturnType(can_load_classes_);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002133 if (return_type_class != nullptr) {
2134 return_type = &reg_types_.FromClass(mh.GetReturnTypeDescriptor(), return_type_class,
2135 return_type_class->CannotBeAssignedFromOtherTypes());
2136 } else {
2137 Thread* self = Thread::Current();
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08002138 DCHECK(!can_load_classes_ || self->IsExceptionPending());
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002139 self->ClearException();
2140 }
2141 }
2142 if (return_type == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002143 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002144 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2145 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002146 const char* descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002147 return_type = &reg_types_.FromDescriptor(class_loader_->get(), descriptor, false);
jeffhaobdb76512011-09-07 11:43:16 -07002148 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002149 if (!return_type->IsLowHalf()) {
2150 work_line_->SetResultRegisterType(*return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002151 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002152 work_line_->SetResultRegisterTypeWide(*return_type, return_type->HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002153 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002154 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002155 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002156 }
jeffhaobdb76512011-09-07 11:43:16 -07002157 case Instruction::INVOKE_DIRECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002158 case Instruction::INVOKE_DIRECT_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002159 bool is_range = (inst->Opcode() == Instruction::INVOKE_DIRECT_RANGE);
Brian Carlstromea46f952013-07-30 01:26:50 -07002160 mirror::ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_DIRECT,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002161 is_range, false);
Ian Rogers46685432012-06-03 22:26:43 -07002162 const char* return_type_descriptor;
2163 bool is_constructor;
2164 if (called_method == NULL) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002165 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogers46685432012-06-03 22:26:43 -07002166 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
Ian Rogersdfb325e2013-10-30 01:00:44 -07002167 is_constructor = strcmp("<init>", dex_file_->StringDataByIdx(method_id.name_idx_)) == 0;
Ian Rogers46685432012-06-03 22:26:43 -07002168 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
2169 return_type_descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2170 } else {
2171 is_constructor = called_method->IsConstructor();
2172 return_type_descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
2173 }
2174 if (is_constructor) {
jeffhaobdb76512011-09-07 11:43:16 -07002175 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002176 * Some additional checks when calling a constructor. We know from the invocation arg check
2177 * that the "this" argument is an instance of called_method->klass. Now we further restrict
2178 * that to require that called_method->klass is the same as this->klass or this->super,
2179 * allowing the latter only if the "this" argument is the same as the "this" argument to
2180 * this method (which implies that we're in a constructor ourselves).
jeffhaobdb76512011-09-07 11:43:16 -07002181 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002182 const RegType& this_type = work_line_->GetInvocationThis(inst, is_range);
jeffhaob57e9522012-04-26 18:08:21 -07002183 if (this_type.IsConflict()) // failure.
2184 break;
jeffhaobdb76512011-09-07 11:43:16 -07002185
jeffhaob57e9522012-04-26 18:08:21 -07002186 /* no null refs allowed (?) */
2187 if (this_type.IsZero()) {
2188 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unable to initialize null ref";
2189 break;
jeffhao2a8a90e2011-09-26 14:25:31 -07002190 }
jeffhaob57e9522012-04-26 18:08:21 -07002191
2192 /* must be in same class or in superclass */
Ian Rogers46685432012-06-03 22:26:43 -07002193 // const RegType& this_super_klass = this_type.GetSuperClass(&reg_types_);
2194 // TODO: re-enable constructor type verification
2195 // if (this_super_klass.IsConflict()) {
jeffhaob57e9522012-04-26 18:08:21 -07002196 // Unknown super class, fail so we re-check at runtime.
Ian Rogers46685432012-06-03 22:26:43 -07002197 // Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "super class unknown for '" << this_type << "'";
2198 // break;
2199 // }
jeffhaob57e9522012-04-26 18:08:21 -07002200
2201 /* arg must be an uninitialized reference */
2202 if (!this_type.IsUninitializedTypes()) {
2203 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Expected initialization on uninitialized reference "
2204 << this_type;
2205 break;
2206 }
2207
2208 /*
2209 * Replace the uninitialized reference with an initialized one. We need to do this for all
2210 * registers that have the same object instance in them, not just the "this" register.
2211 */
2212 work_line_->MarkRefsAsInitialized(this_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002213 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07002214 const RegType& return_type = reg_types_.FromDescriptor(class_loader_->get(),
2215 return_type_descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002216 if (!return_type.IsLowHalf()) {
2217 work_line_->SetResultRegisterType(return_type);
2218 } else {
2219 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2220 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002221 just_set_result = true;
2222 break;
2223 }
2224 case Instruction::INVOKE_STATIC:
2225 case Instruction::INVOKE_STATIC_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002226 bool is_range = (inst->Opcode() == Instruction::INVOKE_STATIC_RANGE);
Brian Carlstromea46f952013-07-30 01:26:50 -07002227 mirror::ArtMethod* called_method = VerifyInvocationArgs(inst,
Brian Carlstrom93c33962013-07-26 10:37:43 -07002228 METHOD_STATIC,
2229 is_range,
2230 false);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002231 const char* descriptor;
2232 if (called_method == NULL) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002233 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002234 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2235 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Mathieu Chartier590fee92013-09-13 13:46:47 -07002236 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002237 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002238 descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002239 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07002240 const RegType& return_type = reg_types_.FromDescriptor(class_loader_->get(), descriptor,
2241 false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002242 if (!return_type.IsLowHalf()) {
2243 work_line_->SetResultRegisterType(return_type);
2244 } else {
2245 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2246 }
jeffhaobdb76512011-09-07 11:43:16 -07002247 just_set_result = true;
2248 }
2249 break;
jeffhaobdb76512011-09-07 11:43:16 -07002250 case Instruction::INVOKE_INTERFACE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002251 case Instruction::INVOKE_INTERFACE_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002252 bool is_range = (inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE);
Brian Carlstromea46f952013-07-30 01:26:50 -07002253 mirror::ArtMethod* abs_method = VerifyInvocationArgs(inst,
Brian Carlstrom93c33962013-07-26 10:37:43 -07002254 METHOD_INTERFACE,
2255 is_range,
2256 false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002257 if (abs_method != NULL) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002258 mirror::Class* called_interface = abs_method->GetDeclaringClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002259 if (!called_interface->IsInterface() && !called_interface->IsObjectClass()) {
2260 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected interface class in invoke-interface '"
2261 << PrettyMethod(abs_method) << "'";
2262 break;
Ian Rogers28ad40d2011-10-27 15:19:26 -07002263 }
Ian Rogers0d604842012-04-16 14:50:24 -07002264 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002265 /* Get the type of the "this" arg, which should either be a sub-interface of called
2266 * interface or Object (see comments in RegType::JoinClass).
2267 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002268 const RegType& this_type = work_line_->GetInvocationThis(inst, is_range);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002269 if (this_type.IsZero()) {
2270 /* null pointer always passes (and always fails at runtime) */
2271 } else {
2272 if (this_type.IsUninitializedTypes()) {
2273 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface call on uninitialized object "
2274 << this_type;
2275 break;
2276 }
2277 // In the past we have tried to assert that "called_interface" is assignable
2278 // from "this_type.GetClass()", however, as we do an imprecise Join
2279 // (RegType::JoinClass) we don't have full information on what interfaces are
2280 // implemented by "this_type". For example, two classes may implement the same
2281 // interfaces and have a common parent that doesn't implement the interface. The
2282 // join will set "this_type" to the parent class and a test that this implements
2283 // the interface will incorrectly fail.
2284 }
2285 /*
2286 * We don't have an object instance, so we can't find the concrete method. However, all of
2287 * the type information is in the abstract method, so we're good.
2288 */
2289 const char* descriptor;
2290 if (abs_method == NULL) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002291 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002292 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2293 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
2294 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2295 } else {
2296 descriptor = MethodHelper(abs_method).GetReturnTypeDescriptor();
2297 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07002298 const RegType& return_type = reg_types_.FromDescriptor(class_loader_->get(), descriptor,
2299 false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002300 if (!return_type.IsLowHalf()) {
2301 work_line_->SetResultRegisterType(return_type);
2302 } else {
2303 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2304 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002305 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002306 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002307 }
jeffhaobdb76512011-09-07 11:43:16 -07002308 case Instruction::NEG_INT:
2309 case Instruction::NOT_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002310 work_line_->CheckUnaryOp(inst, reg_types_.Integer(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002311 break;
2312 case Instruction::NEG_LONG:
2313 case Instruction::NOT_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002314 work_line_->CheckUnaryOpWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002315 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002316 break;
2317 case Instruction::NEG_FLOAT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002318 work_line_->CheckUnaryOp(inst, reg_types_.Float(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002319 break;
2320 case Instruction::NEG_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002321 work_line_->CheckUnaryOpWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002322 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002323 break;
2324 case Instruction::INT_TO_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002325 work_line_->CheckUnaryOpToWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002326 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002327 break;
2328 case Instruction::INT_TO_FLOAT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002329 work_line_->CheckUnaryOp(inst, reg_types_.Float(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002330 break;
2331 case Instruction::INT_TO_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002332 work_line_->CheckUnaryOpToWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002333 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002334 break;
2335 case Instruction::LONG_TO_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002336 work_line_->CheckUnaryOpFromWide(inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002337 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002338 break;
2339 case Instruction::LONG_TO_FLOAT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002340 work_line_->CheckUnaryOpFromWide(inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002341 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002342 break;
2343 case Instruction::LONG_TO_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002344 work_line_->CheckUnaryOpWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002345 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002346 break;
2347 case Instruction::FLOAT_TO_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002348 work_line_->CheckUnaryOp(inst, reg_types_.Integer(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002349 break;
2350 case Instruction::FLOAT_TO_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002351 work_line_->CheckUnaryOpToWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002352 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002353 break;
2354 case Instruction::FLOAT_TO_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002355 work_line_->CheckUnaryOpToWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002356 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002357 break;
2358 case Instruction::DOUBLE_TO_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002359 work_line_->CheckUnaryOpFromWide(inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002360 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002361 break;
2362 case Instruction::DOUBLE_TO_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002363 work_line_->CheckUnaryOpWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002364 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002365 break;
2366 case Instruction::DOUBLE_TO_FLOAT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002367 work_line_->CheckUnaryOpFromWide(inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002368 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002369 break;
2370 case Instruction::INT_TO_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002371 work_line_->CheckUnaryOp(inst, reg_types_.Byte(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002372 break;
2373 case Instruction::INT_TO_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002374 work_line_->CheckUnaryOp(inst, reg_types_.Char(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002375 break;
2376 case Instruction::INT_TO_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002377 work_line_->CheckUnaryOp(inst, reg_types_.Short(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002378 break;
2379
2380 case Instruction::ADD_INT:
2381 case Instruction::SUB_INT:
2382 case Instruction::MUL_INT:
2383 case Instruction::REM_INT:
2384 case Instruction::DIV_INT:
2385 case Instruction::SHL_INT:
2386 case Instruction::SHR_INT:
2387 case Instruction::USHR_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002388 work_line_->CheckBinaryOp(inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002389 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002390 break;
2391 case Instruction::AND_INT:
2392 case Instruction::OR_INT:
2393 case Instruction::XOR_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002394 work_line_->CheckBinaryOp(inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002395 reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002396 break;
2397 case Instruction::ADD_LONG:
2398 case Instruction::SUB_LONG:
2399 case Instruction::MUL_LONG:
2400 case Instruction::DIV_LONG:
2401 case Instruction::REM_LONG:
2402 case Instruction::AND_LONG:
2403 case Instruction::OR_LONG:
2404 case Instruction::XOR_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002405 work_line_->CheckBinaryOpWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002406 reg_types_.LongLo(), reg_types_.LongHi(),
2407 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002408 break;
2409 case Instruction::SHL_LONG:
2410 case Instruction::SHR_LONG:
2411 case Instruction::USHR_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002412 /* shift distance is Int, making these different from other binary operations */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002413 work_line_->CheckBinaryOpWideShift(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002414 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002415 break;
2416 case Instruction::ADD_FLOAT:
2417 case Instruction::SUB_FLOAT:
2418 case Instruction::MUL_FLOAT:
2419 case Instruction::DIV_FLOAT:
2420 case Instruction::REM_FLOAT:
Brian Carlstrom93c33962013-07-26 10:37:43 -07002421 work_line_->CheckBinaryOp(inst,
2422 reg_types_.Float(),
2423 reg_types_.Float(),
2424 reg_types_.Float(),
2425 false);
jeffhaobdb76512011-09-07 11:43:16 -07002426 break;
2427 case Instruction::ADD_DOUBLE:
2428 case Instruction::SUB_DOUBLE:
2429 case Instruction::MUL_DOUBLE:
2430 case Instruction::DIV_DOUBLE:
2431 case Instruction::REM_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002432 work_line_->CheckBinaryOpWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002433 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2434 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002435 break;
2436 case Instruction::ADD_INT_2ADDR:
2437 case Instruction::SUB_INT_2ADDR:
2438 case Instruction::MUL_INT_2ADDR:
2439 case Instruction::REM_INT_2ADDR:
2440 case Instruction::SHL_INT_2ADDR:
2441 case Instruction::SHR_INT_2ADDR:
2442 case Instruction::USHR_INT_2ADDR:
Brian Carlstrom93c33962013-07-26 10:37:43 -07002443 work_line_->CheckBinaryOp2addr(inst,
2444 reg_types_.Integer(),
2445 reg_types_.Integer(),
2446 reg_types_.Integer(),
2447 false);
jeffhaobdb76512011-09-07 11:43:16 -07002448 break;
2449 case Instruction::AND_INT_2ADDR:
2450 case Instruction::OR_INT_2ADDR:
2451 case Instruction::XOR_INT_2ADDR:
Brian Carlstrom93c33962013-07-26 10:37:43 -07002452 work_line_->CheckBinaryOp2addr(inst,
2453 reg_types_.Integer(),
2454 reg_types_.Integer(),
2455 reg_types_.Integer(),
2456 true);
jeffhaobdb76512011-09-07 11:43:16 -07002457 break;
2458 case Instruction::DIV_INT_2ADDR:
Brian Carlstrom93c33962013-07-26 10:37:43 -07002459 work_line_->CheckBinaryOp2addr(inst,
2460 reg_types_.Integer(),
2461 reg_types_.Integer(),
2462 reg_types_.Integer(),
2463 false);
jeffhaobdb76512011-09-07 11:43:16 -07002464 break;
2465 case Instruction::ADD_LONG_2ADDR:
2466 case Instruction::SUB_LONG_2ADDR:
2467 case Instruction::MUL_LONG_2ADDR:
2468 case Instruction::DIV_LONG_2ADDR:
2469 case Instruction::REM_LONG_2ADDR:
2470 case Instruction::AND_LONG_2ADDR:
2471 case Instruction::OR_LONG_2ADDR:
2472 case Instruction::XOR_LONG_2ADDR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002473 work_line_->CheckBinaryOp2addrWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002474 reg_types_.LongLo(), reg_types_.LongHi(),
2475 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002476 break;
2477 case Instruction::SHL_LONG_2ADDR:
2478 case Instruction::SHR_LONG_2ADDR:
2479 case Instruction::USHR_LONG_2ADDR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002480 work_line_->CheckBinaryOp2addrWideShift(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002481 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002482 break;
2483 case Instruction::ADD_FLOAT_2ADDR:
2484 case Instruction::SUB_FLOAT_2ADDR:
2485 case Instruction::MUL_FLOAT_2ADDR:
2486 case Instruction::DIV_FLOAT_2ADDR:
2487 case Instruction::REM_FLOAT_2ADDR:
Brian Carlstrom93c33962013-07-26 10:37:43 -07002488 work_line_->CheckBinaryOp2addr(inst,
2489 reg_types_.Float(),
2490 reg_types_.Float(),
2491 reg_types_.Float(),
2492 false);
jeffhaobdb76512011-09-07 11:43:16 -07002493 break;
2494 case Instruction::ADD_DOUBLE_2ADDR:
2495 case Instruction::SUB_DOUBLE_2ADDR:
2496 case Instruction::MUL_DOUBLE_2ADDR:
2497 case Instruction::DIV_DOUBLE_2ADDR:
2498 case Instruction::REM_DOUBLE_2ADDR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002499 work_line_->CheckBinaryOp2addrWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002500 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2501 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002502 break;
2503 case Instruction::ADD_INT_LIT16:
2504 case Instruction::RSUB_INT:
2505 case Instruction::MUL_INT_LIT16:
2506 case Instruction::DIV_INT_LIT16:
2507 case Instruction::REM_INT_LIT16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002508 work_line_->CheckLiteralOp(inst, reg_types_.Integer(), reg_types_.Integer(), false, true);
jeffhaobdb76512011-09-07 11:43:16 -07002509 break;
2510 case Instruction::AND_INT_LIT16:
2511 case Instruction::OR_INT_LIT16:
2512 case Instruction::XOR_INT_LIT16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002513 work_line_->CheckLiteralOp(inst, reg_types_.Integer(), reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002514 break;
2515 case Instruction::ADD_INT_LIT8:
2516 case Instruction::RSUB_INT_LIT8:
2517 case Instruction::MUL_INT_LIT8:
2518 case Instruction::DIV_INT_LIT8:
2519 case Instruction::REM_INT_LIT8:
2520 case Instruction::SHL_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002521 case Instruction::SHR_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002522 case Instruction::USHR_INT_LIT8:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002523 work_line_->CheckLiteralOp(inst, reg_types_.Integer(), reg_types_.Integer(), false, false);
jeffhaobdb76512011-09-07 11:43:16 -07002524 break;
2525 case Instruction::AND_INT_LIT8:
2526 case Instruction::OR_INT_LIT8:
2527 case Instruction::XOR_INT_LIT8:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002528 work_line_->CheckLiteralOp(inst, reg_types_.Integer(), reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002529 break;
2530
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002531 // Special instructions.
Sebastien Hertzcc10e0e2013-06-28 14:24:48 +02002532 case Instruction::RETURN_VOID_BARRIER:
Ian Rogers9fc16eb2013-07-31 14:49:16 -07002533 DCHECK(Runtime::Current()->IsStarted()) << PrettyMethod(dex_method_idx_, *dex_file_);
2534 if (!IsConstructor() || IsStatic()) {
Sebastien Hertzcc10e0e2013-06-28 14:24:48 +02002535 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void-barrier not expected";
2536 }
2537 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002538 // Note: the following instructions encode offsets derived from class linking.
2539 // As such they use Class*/Field*/AbstractMethod* as these offsets only have
2540 // meaning if the class linking and resolution were successful.
2541 case Instruction::IGET_QUICK:
2542 VerifyIGetQuick(inst, reg_types_.Integer(), true);
2543 break;
2544 case Instruction::IGET_WIDE_QUICK:
2545 VerifyIGetQuick(inst, reg_types_.LongLo(), true);
2546 break;
2547 case Instruction::IGET_OBJECT_QUICK:
2548 VerifyIGetQuick(inst, reg_types_.JavaLangObject(false), false);
2549 break;
2550 case Instruction::IPUT_QUICK:
2551 VerifyIPutQuick(inst, reg_types_.Integer(), true);
2552 break;
2553 case Instruction::IPUT_WIDE_QUICK:
2554 VerifyIPutQuick(inst, reg_types_.LongLo(), true);
2555 break;
2556 case Instruction::IPUT_OBJECT_QUICK:
2557 VerifyIPutQuick(inst, reg_types_.JavaLangObject(false), false);
2558 break;
2559 case Instruction::INVOKE_VIRTUAL_QUICK:
2560 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: {
2561 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
Brian Carlstromea46f952013-07-30 01:26:50 -07002562 mirror::ArtMethod* called_method = VerifyInvokeVirtualQuickArgs(inst, is_range);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002563 if (called_method != NULL) {
2564 const char* descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
Mathieu Chartier590fee92013-09-13 13:46:47 -07002565 const RegType& return_type = reg_types_.FromDescriptor(class_loader_->get(), descriptor,
2566 false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002567 if (!return_type.IsLowHalf()) {
2568 work_line_->SetResultRegisterType(return_type);
2569 } else {
2570 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2571 }
2572 just_set_result = true;
2573 }
2574 break;
2575 }
2576
Ian Rogersd81871c2011-10-03 13:57:23 -07002577 /* These should never appear during verification. */
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002578 case Instruction::UNUSED_3E:
2579 case Instruction::UNUSED_3F:
2580 case Instruction::UNUSED_40:
2581 case Instruction::UNUSED_41:
2582 case Instruction::UNUSED_42:
2583 case Instruction::UNUSED_43:
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002584 case Instruction::UNUSED_79:
2585 case Instruction::UNUSED_7A:
2586 case Instruction::UNUSED_EB:
2587 case Instruction::UNUSED_EC:
jeffhao9a4f0032012-08-30 16:17:40 -07002588 case Instruction::UNUSED_ED:
jeffhaobdb76512011-09-07 11:43:16 -07002589 case Instruction::UNUSED_EE:
2590 case Instruction::UNUSED_EF:
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002591 case Instruction::UNUSED_F0:
2592 case Instruction::UNUSED_F1:
jeffhaobdb76512011-09-07 11:43:16 -07002593 case Instruction::UNUSED_F2:
2594 case Instruction::UNUSED_F3:
2595 case Instruction::UNUSED_F4:
2596 case Instruction::UNUSED_F5:
2597 case Instruction::UNUSED_F6:
2598 case Instruction::UNUSED_F7:
2599 case Instruction::UNUSED_F8:
2600 case Instruction::UNUSED_F9:
2601 case Instruction::UNUSED_FA:
2602 case Instruction::UNUSED_FB:
jeffhaobdb76512011-09-07 11:43:16 -07002603 case Instruction::UNUSED_FC:
jeffhaobdb76512011-09-07 11:43:16 -07002604 case Instruction::UNUSED_FD:
jeffhaobdb76512011-09-07 11:43:16 -07002605 case Instruction::UNUSED_FE:
jeffhaobdb76512011-09-07 11:43:16 -07002606 case Instruction::UNUSED_FF:
jeffhaod5347e02012-03-22 17:25:05 -07002607 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Unexpected opcode " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07002608 break;
2609
2610 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002611 * DO NOT add a "default" clause here. Without it the compiler will
jeffhaobdb76512011-09-07 11:43:16 -07002612 * complain if an instruction is missing (which is desirable).
2613 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002614 } // end - switch (dec_insn.opcode)
jeffhaobdb76512011-09-07 11:43:16 -07002615
Ian Rogersad0b3a32012-04-16 14:50:24 -07002616 if (have_pending_hard_failure_) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002617 if (Runtime::Current()->IsCompiler()) {
jeffhaob57e9522012-04-26 18:08:21 -07002618 /* When compiling, check that the last failure is a hard failure */
Ian Rogersad0b3a32012-04-16 14:50:24 -07002619 CHECK_EQ(failures_[failures_.size() - 1], VERIFY_ERROR_BAD_CLASS_HARD);
Ian Rogerse1758fe2012-04-19 11:31:15 -07002620 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002621 /* immediate failure, reject class */
2622 info_messages_ << "Rejecting opcode " << inst->DumpString(dex_file_);
2623 return false;
jeffhaofaf459e2012-08-31 15:32:47 -07002624 } else if (have_pending_runtime_throw_failure_) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07002625 /* checking interpreter will throw, mark following code as unreachable */
jeffhaofaf459e2012-08-31 15:32:47 -07002626 opcode_flags = Instruction::kThrow;
jeffhaobdb76512011-09-07 11:43:16 -07002627 }
jeffhaobdb76512011-09-07 11:43:16 -07002628 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002629 * If we didn't just set the result register, clear it out. This ensures that you can only use
2630 * "move-result" immediately after the result is set. (We could check this statically, but it's
2631 * not expensive and it makes our debugging output cleaner.)
jeffhaobdb76512011-09-07 11:43:16 -07002632 */
2633 if (!just_set_result) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002634 work_line_->SetResultTypeToUnknown();
jeffhaobdb76512011-09-07 11:43:16 -07002635 }
2636
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002637
jeffhaobdb76512011-09-07 11:43:16 -07002638
2639 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002640 * Handle "branch". Tag the branch target.
jeffhaobdb76512011-09-07 11:43:16 -07002641 *
2642 * NOTE: instructions like Instruction::EQZ provide information about the
jeffhaod1f0fde2011-09-08 17:25:33 -07002643 * state of the register when the branch is taken or not taken. For example,
jeffhaobdb76512011-09-07 11:43:16 -07002644 * somebody could get a reference field, check it for zero, and if the
2645 * branch is taken immediately store that register in a boolean field
jeffhaod1f0fde2011-09-08 17:25:33 -07002646 * since the value is known to be zero. We do not currently account for
jeffhaobdb76512011-09-07 11:43:16 -07002647 * that, and will reject the code.
2648 *
2649 * TODO: avoid re-fetching the branch target
2650 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002651 if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002652 bool isConditional, selfOkay;
Ian Rogersd81871c2011-10-03 13:57:23 -07002653 if (!GetBranchOffset(work_insn_idx_, &branch_target, &isConditional, &selfOkay)) {
jeffhaobdb76512011-09-07 11:43:16 -07002654 /* should never happen after static verification */
jeffhaod5347e02012-03-22 17:25:05 -07002655 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad branch";
jeffhaobdb76512011-09-07 11:43:16 -07002656 return false;
2657 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08002658 DCHECK_EQ(isConditional, (opcode_flags & Instruction::kContinue) != 0);
jeffhaod5347e02012-03-22 17:25:05 -07002659 if (!CheckNotMoveException(code_item_->insns_, work_insn_idx_ + branch_target)) {
jeffhaobdb76512011-09-07 11:43:16 -07002660 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002661 }
jeffhaobdb76512011-09-07 11:43:16 -07002662 /* update branch target, set "changed" if appropriate */
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002663 if (NULL != branch_line.get()) {
2664 if (!UpdateRegisters(work_insn_idx_ + branch_target, branch_line.get())) {
2665 return false;
2666 }
2667 } else {
2668 if (!UpdateRegisters(work_insn_idx_ + branch_target, work_line_.get())) {
2669 return false;
2670 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002671 }
jeffhaobdb76512011-09-07 11:43:16 -07002672 }
2673
2674 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002675 * Handle "switch". Tag all possible branch targets.
jeffhaobdb76512011-09-07 11:43:16 -07002676 *
2677 * We've already verified that the table is structurally sound, so we
2678 * just need to walk through and tag the targets.
2679 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002680 if ((opcode_flags & Instruction::kSwitch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002681 int offset_to_switch = insns[1] | (((int32_t) insns[2]) << 16);
2682 const uint16_t* switch_insns = insns + offset_to_switch;
2683 int switch_count = switch_insns[1];
2684 int offset_to_targets, targ;
2685
2686 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
2687 /* 0 = sig, 1 = count, 2/3 = first key */
2688 offset_to_targets = 4;
2689 } else {
2690 /* 0 = sig, 1 = count, 2..count * 2 = keys */
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07002691 DCHECK((*insns & 0xff) == Instruction::SPARSE_SWITCH);
jeffhaobdb76512011-09-07 11:43:16 -07002692 offset_to_targets = 2 + 2 * switch_count;
2693 }
2694
2695 /* verify each switch target */
2696 for (targ = 0; targ < switch_count; targ++) {
2697 int offset;
2698 uint32_t abs_offset;
2699
2700 /* offsets are 32-bit, and only partly endian-swapped */
2701 offset = switch_insns[offset_to_targets + targ * 2] |
2702 (((int32_t) switch_insns[offset_to_targets + targ * 2 + 1]) << 16);
Ian Rogersd81871c2011-10-03 13:57:23 -07002703 abs_offset = work_insn_idx_ + offset;
2704 DCHECK_LT(abs_offset, code_item_->insns_size_in_code_units_);
jeffhaod5347e02012-03-22 17:25:05 -07002705 if (!CheckNotMoveException(code_item_->insns_, abs_offset)) {
jeffhaobdb76512011-09-07 11:43:16 -07002706 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002707 }
2708 if (!UpdateRegisters(abs_offset, work_line_.get()))
jeffhaobdb76512011-09-07 11:43:16 -07002709 return false;
2710 }
2711 }
2712
2713 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002714 * Handle instructions that can throw and that are sitting in a "try" block. (If they're not in a
2715 * "try" block when they throw, control transfers out of the method.)
jeffhaobdb76512011-09-07 11:43:16 -07002716 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002717 if ((opcode_flags & Instruction::kThrow) != 0 && insn_flags_[work_insn_idx_].IsInTry()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002718 bool within_catch_all = false;
Ian Rogers0571d352011-11-03 19:51:38 -07002719 CatchHandlerIterator iterator(*code_item_, work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07002720
Ian Rogers0571d352011-11-03 19:51:38 -07002721 for (; iterator.HasNext(); iterator.Next()) {
2722 if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002723 within_catch_all = true;
2724 }
jeffhaobdb76512011-09-07 11:43:16 -07002725 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002726 * Merge registers into the "catch" block. We want to use the "savedRegs" rather than
2727 * "work_regs", because at runtime the exception will be thrown before the instruction
2728 * modifies any registers.
jeffhaobdb76512011-09-07 11:43:16 -07002729 */
Ian Rogers0571d352011-11-03 19:51:38 -07002730 if (!UpdateRegisters(iterator.GetHandlerAddress(), saved_line_.get())) {
jeffhaobdb76512011-09-07 11:43:16 -07002731 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002732 }
jeffhaobdb76512011-09-07 11:43:16 -07002733 }
2734
2735 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002736 * If the monitor stack depth is nonzero, there must be a "catch all" handler for this
2737 * instruction. This does apply to monitor-exit because of async exception handling.
jeffhaobdb76512011-09-07 11:43:16 -07002738 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002739 if (work_line_->MonitorStackDepth() > 0 && !within_catch_all) {
jeffhaobdb76512011-09-07 11:43:16 -07002740 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002741 * The state in work_line reflects the post-execution state. If the current instruction is a
2742 * monitor-enter and the monitor stack was empty, we don't need a catch-all (if it throws,
jeffhaobdb76512011-09-07 11:43:16 -07002743 * it will do so before grabbing the lock).
2744 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002745 if (inst->Opcode() != Instruction::MONITOR_ENTER || work_line_->MonitorStackDepth() != 1) {
jeffhaod5347e02012-03-22 17:25:05 -07002746 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
Ian Rogersd81871c2011-10-03 13:57:23 -07002747 << "expected to be within a catch-all for an instruction where a monitor is held";
jeffhaobdb76512011-09-07 11:43:16 -07002748 return false;
2749 }
2750 }
2751 }
2752
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002753 /* Handle "continue". Tag the next consecutive instruction.
2754 * Note: Keep the code handling "continue" case below the "branch" and "switch" cases,
2755 * because it changes work_line_ when performing peephole optimization
2756 * and this change should not be used in those cases.
2757 */
Ian Rogers6d376ae2013-07-23 15:12:40 -07002758 if ((opcode_flags & Instruction::kContinue) != 0) {
2759 uint32_t next_insn_idx = work_insn_idx_ + CurrentInsnFlags()->GetLengthInCodeUnits();
2760 if (next_insn_idx >= code_item_->insns_size_in_code_units_) {
2761 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Execution can walk off end of code area";
2762 return false;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002763 }
Ian Rogers6d376ae2013-07-23 15:12:40 -07002764 // The only way to get to a move-exception instruction is to get thrown there. Make sure the
2765 // next instruction isn't one.
2766 if (!CheckNotMoveException(code_item_->insns_, next_insn_idx)) {
2767 return false;
2768 }
2769 if (NULL != fallthrough_line.get()) {
2770 // Make workline consistent with fallthrough computed from peephole optimization.
2771 work_line_->CopyFromLine(fallthrough_line.get());
2772 }
Ian Rogersb8c78592013-07-25 23:52:52 +00002773 if (insn_flags_[next_insn_idx].IsReturn()) {
2774 // For returns we only care about the operand to the return, all other registers are dead.
2775 const Instruction* ret_inst = Instruction::At(code_item_->insns_ + next_insn_idx);
2776 Instruction::Code opcode = ret_inst->Opcode();
2777 if ((opcode == Instruction::RETURN_VOID) || (opcode == Instruction::RETURN_VOID_BARRIER)) {
2778 work_line_->MarkAllRegistersAsConflicts();
2779 } else {
2780 if (opcode == Instruction::RETURN_WIDE) {
2781 work_line_->MarkAllRegistersAsConflictsExceptWide(ret_inst->VRegA_11x());
2782 } else {
2783 work_line_->MarkAllRegistersAsConflictsExcept(ret_inst->VRegA_11x());
2784 }
2785 }
2786 }
Ian Rogers6d376ae2013-07-23 15:12:40 -07002787 RegisterLine* next_line = reg_table_.GetLine(next_insn_idx);
2788 if (next_line != NULL) {
2789 // Merge registers into what we have for the next instruction,
2790 // and set the "changed" flag if needed.
2791 if (!UpdateRegisters(next_insn_idx, work_line_.get())) {
2792 return false;
2793 }
2794 } else {
2795 /*
2796 * We're not recording register data for the next instruction, so we don't know what the
2797 * prior state was. We have to assume that something has changed and re-evaluate it.
2798 */
2799 insn_flags_[next_insn_idx].SetChanged();
2800 }
2801 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002802
jeffhaod1f0fde2011-09-08 17:25:33 -07002803 /* If we're returning from the method, make sure monitor stack is empty. */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002804 if ((opcode_flags & Instruction::kReturn) != 0) {
Elliott Hughesb25c3f62012-03-26 16:35:06 -07002805 if (!work_line_->VerifyMonitorStackEmpty()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002806 return false;
2807 }
jeffhaobdb76512011-09-07 11:43:16 -07002808 }
2809
2810 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002811 * Update start_guess. Advance to the next instruction of that's
2812 * possible, otherwise use the branch target if one was found. If
jeffhaobdb76512011-09-07 11:43:16 -07002813 * neither of those exists we're in a return or throw; leave start_guess
2814 * alone and let the caller sort it out.
2815 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002816 if ((opcode_flags & Instruction::kContinue) != 0) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002817 *start_guess = work_insn_idx_ + insn_flags_[work_insn_idx_].GetLengthInCodeUnits();
Elliott Hughesadb8c672012-03-06 16:49:32 -08002818 } else if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002819 /* we're still okay if branch_target is zero */
Ian Rogersd81871c2011-10-03 13:57:23 -07002820 *start_guess = work_insn_idx_ + branch_target;
jeffhaobdb76512011-09-07 11:43:16 -07002821 }
2822
Ian Rogersd81871c2011-10-03 13:57:23 -07002823 DCHECK_LT(*start_guess, code_item_->insns_size_in_code_units_);
2824 DCHECK(insn_flags_[*start_guess].IsOpcode());
jeffhaobdb76512011-09-07 11:43:16 -07002825
2826 return true;
Brian Carlstrom7934ac22013-07-26 10:54:15 -07002827} // NOLINT(readability/fn_size)
jeffhaobdb76512011-09-07 11:43:16 -07002828
Ian Rogers776ac1f2012-04-13 23:36:36 -07002829const RegType& MethodVerifier::ResolveClassAndCheckAccess(uint32_t class_idx) {
Ian Rogers0571d352011-11-03 19:51:38 -07002830 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002831 const RegType& referrer = GetDeclaringClass();
Mathieu Chartier590fee92013-09-13 13:46:47 -07002832 mirror::Class* klass = (*dex_cache_)->GetResolvedType(class_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002833 const RegType& result =
Ian Rogers04f94f42013-06-10 15:09:26 -07002834 klass != NULL ? reg_types_.FromClass(descriptor, klass,
2835 klass->CannotBeAssignedFromOtherTypes())
Mathieu Chartier590fee92013-09-13 13:46:47 -07002836 : reg_types_.FromDescriptor(class_loader_->get(), descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002837 if (result.IsConflict()) {
2838 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "accessing broken descriptor '" << descriptor
2839 << "' in " << referrer;
2840 return result;
2841 }
Ian Rogerse1758fe2012-04-19 11:31:15 -07002842 if (klass == NULL && !result.IsUnresolvedTypes()) {
Mathieu Chartier590fee92013-09-13 13:46:47 -07002843 (*dex_cache_)->SetResolvedType(class_idx, result.GetClass());
Ian Rogerse1758fe2012-04-19 11:31:15 -07002844 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002845 // Check if access is allowed. Unresolved types use xxxWithAccessCheck to
Jeff Haob24b4a72013-07-31 13:47:31 -07002846 // check at runtime if access is allowed and so pass here. If result is
2847 // primitive, skip the access check.
2848 if (result.IsNonZeroReferenceTypes() && !result.IsUnresolvedTypes() &&
2849 !referrer.IsUnresolvedTypes() && !referrer.CanAccess(result)) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002850 Fail(VERIFY_ERROR_ACCESS_CLASS) << "illegal class access: '"
Ian Rogersad0b3a32012-04-16 14:50:24 -07002851 << referrer << "' -> '" << result << "'";
Ian Rogers28ad40d2011-10-27 15:19:26 -07002852 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002853 return result;
Ian Rogersd81871c2011-10-03 13:57:23 -07002854}
2855
Ian Rogers776ac1f2012-04-13 23:36:36 -07002856const RegType& MethodVerifier::GetCaughtExceptionType() {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002857 const RegType* common_super = NULL;
Ian Rogersd81871c2011-10-03 13:57:23 -07002858 if (code_item_->tries_size_ != 0) {
Ian Rogers0571d352011-11-03 19:51:38 -07002859 const byte* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
Ian Rogersd81871c2011-10-03 13:57:23 -07002860 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
2861 for (uint32_t i = 0; i < handlers_size; i++) {
Ian Rogers0571d352011-11-03 19:51:38 -07002862 CatchHandlerIterator iterator(handlers_ptr);
2863 for (; iterator.HasNext(); iterator.Next()) {
2864 if (iterator.GetHandlerAddress() == (uint32_t) work_insn_idx_) {
2865 if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) {
Ian Rogersb4903572012-10-11 11:52:56 -07002866 common_super = &reg_types_.JavaLangThrowable(false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002867 } else {
Ian Rogers0571d352011-11-03 19:51:38 -07002868 const RegType& exception = ResolveClassAndCheckAccess(iterator.GetHandlerTypeIndex());
Ian Rogersc4762272012-02-01 15:55:55 -08002869 if (common_super == NULL) {
2870 // Unconditionally assign for the first handler. We don't assert this is a Throwable
2871 // as that is caught at runtime
2872 common_super = &exception;
Ian Rogersb4903572012-10-11 11:52:56 -07002873 } else if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(exception)) {
Jeff Haoc26a56c2013-11-04 12:00:47 -08002874 if (exception.IsUnresolvedTypes()) {
2875 // We don't know enough about the type. Fail here and let runtime handle it.
2876 Fail(VERIFY_ERROR_NO_CLASS) << "unresolved exception class " << exception;
2877 return exception;
2878 } else {
2879 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unexpected non-exception class " << exception;
2880 return reg_types_.Conflict();
2881 }
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);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003122 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
3123 return NULL;
Sebastien Hertz8249b422013-10-29 17:50:55 +01003124 } else if (actual_arg_type.IsZero()) { // Invoke on "null" instance: we can't go further.
3125 return NULL;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003126 }
3127 mirror::Class* this_class = NULL;
3128 if (!actual_arg_type.IsUnresolvedTypes()) {
3129 this_class = actual_arg_type.GetClass();
3130 } else {
3131 const std::string& descriptor(actual_arg_type.GetDescriptor());
3132 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartier590fee92013-09-13 13:46:47 -07003133 this_class = class_linker->FindClass(descriptor.c_str(), *class_loader_);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003134 if (this_class == NULL) {
Mathieu Chartier590fee92013-09-13 13:46:47 -07003135 Thread* self = Thread::Current();
3136 self->ClearException();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003137 // Look for a system class
Mathieu Chartier590fee92013-09-13 13:46:47 -07003138 SirtRef<mirror::ClassLoader> null_class_loader(self, nullptr);
3139 this_class = class_linker->FindClass(descriptor.c_str(), null_class_loader);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003140 }
3141 }
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003142 if (this_class == NULL) {
3143 return NULL;
3144 }
Brian Carlstromea46f952013-07-30 01:26:50 -07003145 mirror::ObjectArray<mirror::ArtMethod>* vtable = this_class->GetVTable();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003146 CHECK(vtable != NULL);
3147 uint16_t vtable_index = is_range ? inst->VRegB_3rc() : inst->VRegB_35c();
3148 CHECK(vtable_index < vtable->GetLength());
Brian Carlstromea46f952013-07-30 01:26:50 -07003149 mirror::ArtMethod* res_method = vtable->Get(vtable_index);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003150 CHECK(!Thread::Current()->IsExceptionPending());
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003151 return res_method;
3152}
3153
Brian Carlstromea46f952013-07-30 01:26:50 -07003154mirror::ArtMethod* MethodVerifier::VerifyInvokeVirtualQuickArgs(const Instruction* inst,
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003155 bool is_range) {
3156 DCHECK(Runtime::Current()->IsStarted());
Brian Carlstromea46f952013-07-30 01:26:50 -07003157 mirror::ArtMethod* res_method = GetQuickInvokedMethod(inst, work_line_.get(),
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003158 is_range);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003159 if (res_method == NULL) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003160 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer method from " << inst->Name();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003161 return NULL;
3162 }
3163 CHECK(!res_method->IsDirect() && !res_method->IsStatic());
3164
3165 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
3166 // match the call to the signature. Also, we might be calling through an abstract method
3167 // definition (which doesn't have register count values).
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003168 const RegType& actual_arg_type = work_line_->GetInvocationThis(inst, is_range);
3169 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
3170 return NULL;
3171 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003172 const size_t expected_args = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
3173 /* caught by static verifier */
3174 DCHECK(is_range || expected_args <= 5);
3175 if (expected_args > code_item_->outs_size_) {
3176 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
3177 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
3178 return NULL;
3179 }
3180
3181 /*
3182 * Check the "this" argument, which must be an instance of the class that declared the method.
3183 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
3184 * rigorous check here (which is okay since we have to do it at runtime).
3185 */
3186 if (actual_arg_type.IsUninitializedReference() && !res_method->IsConstructor()) {
3187 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
3188 return NULL;
3189 }
3190 if (!actual_arg_type.IsZero()) {
3191 mirror::Class* klass = res_method->GetDeclaringClass();
3192 const RegType& res_method_class =
3193 reg_types_.FromClass(ClassHelper(klass).GetDescriptor(), klass,
3194 klass->CannotBeAssignedFromOtherTypes());
3195 if (!res_method_class.IsAssignableFrom(actual_arg_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07003196 Fail(actual_arg_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS :
3197 VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003198 << "' not instance of '" << res_method_class << "'";
3199 return NULL;
3200 }
3201 }
3202 /*
3203 * Process the target method's signature. This signature may or may not
3204 * have been verified, so we can't assume it's properly formed.
3205 */
3206 MethodHelper mh(res_method);
3207 const DexFile::TypeList* params = mh.GetParameterTypeList();
3208 size_t params_size = params == NULL ? 0 : params->Size();
3209 uint32_t arg[5];
3210 if (!is_range) {
3211 inst->GetArgs(arg);
3212 }
3213 size_t actual_args = 1;
3214 for (size_t param_index = 0; param_index < params_size; param_index++) {
3215 if (actual_args >= expected_args) {
3216 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invalid call to '" << PrettyMethod(res_method)
Brian Carlstrom93c33962013-07-26 10:37:43 -07003217 << "'. Expected " << expected_args
3218 << " arguments, processing argument " << actual_args
3219 << " (where longs/doubles count twice).";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003220 return NULL;
3221 }
3222 const char* descriptor =
3223 mh.GetTypeDescriptorFromTypeIdx(params->GetTypeItem(param_index).type_idx_);
3224 if (descriptor == NULL) {
3225 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003226 << " missing signature component";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003227 return NULL;
3228 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07003229 const RegType& reg_type = reg_types_.FromDescriptor(class_loader_->get(), descriptor, false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003230 uint32_t get_reg = is_range ? inst->VRegC_3rc() + actual_args : arg[actual_args];
3231 if (!work_line_->VerifyRegisterType(get_reg, reg_type)) {
3232 return res_method;
3233 }
3234 actual_args = reg_type.IsLongOrDoubleTypes() ? actual_args + 2 : actual_args + 1;
3235 }
3236 if (actual_args != expected_args) {
3237 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
3238 << " expected " << expected_args << " arguments, found " << actual_args;
3239 return NULL;
3240 } else {
3241 return res_method;
3242 }
3243}
3244
Ian Rogers62342ec2013-06-11 10:26:37 -07003245void MethodVerifier::VerifyNewArray(const Instruction* inst, bool is_filled, bool is_range) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003246 uint32_t type_idx;
3247 if (!is_filled) {
3248 DCHECK_EQ(inst->Opcode(), Instruction::NEW_ARRAY);
3249 type_idx = inst->VRegC_22c();
3250 } else if (!is_range) {
3251 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY);
3252 type_idx = inst->VRegB_35c();
3253 } else {
3254 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY_RANGE);
3255 type_idx = inst->VRegB_3rc();
3256 }
3257 const RegType& res_type = ResolveClassAndCheckAccess(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003258 if (res_type.IsConflict()) { // bad class
3259 DCHECK_NE(failures_.size(), 0U);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003260 } else {
3261 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
3262 if (!res_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003263 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "new-array on non-array class " << res_type;
Ian Rogers0c4a5062012-02-03 15:18:59 -08003264 } else if (!is_filled) {
3265 /* make sure "size" register is valid type */
Sebastien Hertz5243e912013-05-21 10:55:07 +02003266 work_line_->VerifyRegisterType(inst->VRegB_22c(), reg_types_.Integer());
Ian Rogers0c4a5062012-02-03 15:18:59 -08003267 /* set register type to array class */
Ian Rogers62342ec2013-06-11 10:26:37 -07003268 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
3269 work_line_->SetRegisterType(inst->VRegA_22c(), precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003270 } else {
3271 // Verify each register. If "arg_count" is bad, VerifyRegisterType() will run off the end of
3272 // the list and fail. It's legal, if silly, for arg_count to be zero.
Mathieu Chartier590fee92013-09-13 13:46:47 -07003273 const RegType& expected_type = reg_types_.GetComponentType(res_type, class_loader_->get());
Sebastien Hertz5243e912013-05-21 10:55:07 +02003274 uint32_t arg_count = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
3275 uint32_t arg[5];
3276 if (!is_range) {
3277 inst->GetArgs(arg);
3278 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08003279 for (size_t ui = 0; ui < arg_count; ui++) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003280 uint32_t get_reg = is_range ? inst->VRegC_3rc() + ui : arg[ui];
Ian Rogers0c4a5062012-02-03 15:18:59 -08003281 if (!work_line_->VerifyRegisterType(get_reg, expected_type)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003282 work_line_->SetResultRegisterType(reg_types_.Conflict());
Ian Rogers0c4a5062012-02-03 15:18:59 -08003283 return;
3284 }
3285 }
3286 // filled-array result goes into "result" register
Ian Rogers62342ec2013-06-11 10:26:37 -07003287 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
3288 work_line_->SetResultRegisterType(precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003289 }
3290 }
3291}
3292
Sebastien Hertz5243e912013-05-21 10:55:07 +02003293void MethodVerifier::VerifyAGet(const Instruction* inst,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003294 const RegType& insn_type, bool is_primitive) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003295 const RegType& index_type = work_line_->GetRegisterType(inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07003296 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003297 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07003298 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003299 const RegType& array_type = work_line_->GetRegisterType(inst->VRegB_23x());
Ian Rogers89310de2012-02-01 13:47:30 -08003300 if (array_type.IsZero()) {
3301 // Null array class; this code path will fail at runtime. Infer a merge-able type from the
3302 // instruction type. TODO: have a proper notion of bottom here.
3303 if (!is_primitive || insn_type.IsCategory1Types()) {
3304 // Reference or category 1
Sebastien Hertz5243e912013-05-21 10:55:07 +02003305 work_line_->SetRegisterType(inst->VRegA_23x(), reg_types_.Zero());
Ian Rogersd81871c2011-10-03 13:57:23 -07003306 } else {
Ian Rogers89310de2012-02-01 13:47:30 -08003307 // Category 2
Sebastien Hertz5243e912013-05-21 10:55:07 +02003308 work_line_->SetRegisterTypeWide(inst->VRegA_23x(), reg_types_.FromCat2ConstLo(0, false),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003309 reg_types_.FromCat2ConstHi(0, false));
Ian Rogers89310de2012-02-01 13:47:30 -08003310 }
jeffhaofc3144e2012-02-01 17:21:15 -08003311 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003312 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aget";
Ian Rogers89310de2012-02-01 13:47:30 -08003313 } else {
3314 /* verify the class */
Mathieu Chartier590fee92013-09-13 13:46:47 -07003315 const RegType& component_type = reg_types_.GetComponentType(array_type, class_loader_->get());
jeffhaofc3144e2012-02-01 17:21:15 -08003316 if (!component_type.IsReferenceTypes() && !is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07003317 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08003318 << " source for aget-object";
3319 } else if (component_type.IsNonZeroReferenceTypes() && is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07003320 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08003321 << " source for category 1 aget";
3322 } else if (is_primitive && !insn_type.Equals(component_type) &&
3323 !((insn_type.IsInteger() && component_type.IsFloat()) ||
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003324 (insn_type.IsLong() && component_type.IsDouble()))) {
3325 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array type " << array_type
3326 << " incompatible with aget of type " << insn_type;
Ian Rogers89310de2012-02-01 13:47:30 -08003327 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003328 // Use knowledge of the field type which is stronger than the type inferred from the
3329 // instruction, which can't differentiate object types and ints from floats, longs from
3330 // doubles.
3331 if (!component_type.IsLowHalf()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003332 work_line_->SetRegisterType(inst->VRegA_23x(), component_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003333 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003334 work_line_->SetRegisterTypeWide(inst->VRegA_23x(), component_type,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003335 component_type.HighHalf(&reg_types_));
3336 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003337 }
3338 }
3339 }
3340}
3341
Jeff Haofe1f7c82013-08-01 14:50:24 -07003342void MethodVerifier::VerifyPrimitivePut(const RegType& target_type, const RegType& insn_type,
3343 const uint32_t vregA) {
3344 // Primitive assignability rules are weaker than regular assignability rules.
3345 bool instruction_compatible;
3346 bool value_compatible;
3347 const RegType& value_type = work_line_->GetRegisterType(vregA);
3348 if (target_type.IsIntegralTypes()) {
Jeff Haoa4647482013-08-06 15:35:47 -07003349 instruction_compatible = target_type.Equals(insn_type);
Jeff Haofe1f7c82013-08-01 14:50:24 -07003350 value_compatible = value_type.IsIntegralTypes();
3351 } else if (target_type.IsFloat()) {
3352 instruction_compatible = insn_type.IsInteger(); // no put-float, so expect put-int
3353 value_compatible = value_type.IsFloatTypes();
3354 } else if (target_type.IsLong()) {
3355 instruction_compatible = insn_type.IsLong();
3356 value_compatible = value_type.IsLongTypes();
3357 } else if (target_type.IsDouble()) {
3358 instruction_compatible = insn_type.IsLong(); // no put-double, so expect put-long
3359 value_compatible = value_type.IsDoubleTypes();
3360 } else {
3361 instruction_compatible = false; // reference with primitive store
3362 value_compatible = false; // unused
3363 }
3364 if (!instruction_compatible) {
3365 // This is a global failure rather than a class change failure as the instructions and
3366 // the descriptors for the type should have been consistent within the same file at
3367 // compile time.
3368 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "put insn has type '" << insn_type
3369 << "' but expected type '" << target_type << "'";
3370 return;
3371 }
3372 if (!value_compatible) {
3373 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
3374 << " of type " << value_type << " but expected " << target_type << " for put";
3375 return;
3376 }
3377}
3378
Sebastien Hertz5243e912013-05-21 10:55:07 +02003379void MethodVerifier::VerifyAPut(const Instruction* inst,
Ian Rogersd81871c2011-10-03 13:57:23 -07003380 const RegType& insn_type, bool is_primitive) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003381 const RegType& index_type = work_line_->GetRegisterType(inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07003382 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003383 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07003384 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003385 const RegType& array_type = work_line_->GetRegisterType(inst->VRegB_23x());
Ian Rogers89310de2012-02-01 13:47:30 -08003386 if (array_type.IsZero()) {
3387 // Null array type; this code path will fail at runtime. Infer a merge-able type from the
3388 // instruction type.
jeffhaofc3144e2012-02-01 17:21:15 -08003389 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003390 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aput";
Ian Rogers89310de2012-02-01 13:47:30 -08003391 } else {
Mathieu Chartier590fee92013-09-13 13:46:47 -07003392 const RegType& component_type = reg_types_.GetComponentType(array_type, class_loader_->get());
Jeff Haofe1f7c82013-08-01 14:50:24 -07003393 const uint32_t vregA = inst->VRegA_23x();
Jeff Haob24b4a72013-07-31 13:47:31 -07003394 if (is_primitive) {
Jeff Haofe1f7c82013-08-01 14:50:24 -07003395 VerifyPrimitivePut(component_type, insn_type, vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07003396 } else {
Jeff Haob24b4a72013-07-31 13:47:31 -07003397 if (!component_type.IsReferenceTypes()) {
3398 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
3399 << " source for aput-object";
3400 } else {
3401 // The instruction agrees with the type of array, confirm the value to be stored does too
3402 // Note: we use the instruction type (rather than the component type) for aput-object as
3403 // incompatible classes will be caught at runtime as an array store exception
Jeff Haofe1f7c82013-08-01 14:50:24 -07003404 work_line_->VerifyRegisterType(vregA, insn_type);
Jeff Haob24b4a72013-07-31 13:47:31 -07003405 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003406 }
3407 }
3408 }
3409}
3410
Brian Carlstromea46f952013-07-30 01:26:50 -07003411mirror::ArtField* MethodVerifier::GetStaticField(int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08003412 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3413 // Check access to class
3414 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Brian Carlstrom7934ac22013-07-26 10:54:15 -07003415 if (klass_type.IsConflict()) { // bad class
Ian Rogersad0b3a32012-04-16 14:50:24 -07003416 AppendToLastFailMessage(StringPrintf(" in attempt to access static field %d (%s) in %s",
3417 field_idx, dex_file_->GetFieldName(field_id),
3418 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers90040192011-12-16 08:54:29 -08003419 return NULL;
3420 }
Elliott Hughesb25c3f62012-03-26 16:35:06 -07003421 if (klass_type.IsUnresolvedTypes()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003422 return NULL; // Can't resolve Class so no more to do here, will do checking at runtime.
Ian Rogers90040192011-12-16 08:54:29 -08003423 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07003424 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
3425 mirror::ArtField* field = class_linker->ResolveFieldJLS(*dex_file_, field_idx, *dex_cache_,
3426 *class_loader_);
Ian Rogersd81871c2011-10-03 13:57:23 -07003427 if (field == NULL) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07003428 VLOG(verifier) << "Unable to resolve static field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07003429 << dex_file_->GetFieldName(field_id) << ") in "
3430 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07003431 DCHECK(Thread::Current()->IsExceptionPending());
3432 Thread::Current()->ClearException();
3433 return NULL;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003434 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
3435 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003436 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access static field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07003437 << " from " << GetDeclaringClass();
Ian Rogersd81871c2011-10-03 13:57:23 -07003438 return NULL;
3439 } else if (!field->IsStatic()) {
3440 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field) << " to be static";
3441 return NULL;
Ian Rogersd81871c2011-10-03 13:57:23 -07003442 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07003443 return field;
Ian Rogersd81871c2011-10-03 13:57:23 -07003444}
3445
Brian Carlstromea46f952013-07-30 01:26:50 -07003446mirror::ArtField* MethodVerifier::GetInstanceField(const RegType& obj_type, int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08003447 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3448 // Check access to class
3449 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003450 if (klass_type.IsConflict()) {
3451 AppendToLastFailMessage(StringPrintf(" in attempt to access instance field %d (%s) in %s",
3452 field_idx, dex_file_->GetFieldName(field_id),
3453 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers90040192011-12-16 08:54:29 -08003454 return NULL;
3455 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003456 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers90040192011-12-16 08:54:29 -08003457 return NULL; // Can't resolve Class so no more to do here
3458 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07003459 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
3460 mirror::ArtField* field = class_linker->ResolveFieldJLS(*dex_file_, field_idx, *dex_cache_,
3461 *class_loader_);
Ian Rogersd81871c2011-10-03 13:57:23 -07003462 if (field == NULL) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07003463 VLOG(verifier) << "Unable to resolve instance field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07003464 << dex_file_->GetFieldName(field_id) << ") in "
3465 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07003466 DCHECK(Thread::Current()->IsExceptionPending());
3467 Thread::Current()->ClearException();
3468 return NULL;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003469 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
3470 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003471 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access instance field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07003472 << " from " << GetDeclaringClass();
Ian Rogersd81871c2011-10-03 13:57:23 -07003473 return NULL;
3474 } else if (field->IsStatic()) {
3475 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field)
3476 << " to not be static";
3477 return NULL;
3478 } else if (obj_type.IsZero()) {
3479 // Cannot infer and check type, however, access will cause null pointer exception
3480 return field;
Ian Rogerse1758fe2012-04-19 11:31:15 -07003481 } else {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003482 mirror::Class* klass = field->GetDeclaringClass();
Ian Rogers637c65b2013-05-31 11:46:00 -07003483 const RegType& field_klass =
3484 reg_types_.FromClass(dex_file_->GetFieldDeclaringClassDescriptor(field_id),
Ian Rogers04f94f42013-06-10 15:09:26 -07003485 klass, klass->CannotBeAssignedFromOtherTypes());
Ian Rogersad0b3a32012-04-16 14:50:24 -07003486 if (obj_type.IsUninitializedTypes() &&
3487 (!IsConstructor() || GetDeclaringClass().Equals(obj_type) ||
3488 !field_klass.Equals(GetDeclaringClass()))) {
3489 // Field accesses through uninitialized references are only allowable for constructors where
3490 // the field is declared in this class
3491 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "cannot access instance field " << PrettyField(field)
Brian Carlstrom93c33962013-07-26 10:37:43 -07003492 << " of a not fully initialized object within the context"
3493 << " of " << PrettyMethod(dex_method_idx_, *dex_file_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003494 return NULL;
3495 } else if (!field_klass.IsAssignableFrom(obj_type)) {
3496 // Trying to access C1.field1 using reference of type C2, which is neither C1 or a sub-class
3497 // of C1. For resolution to occur the declared class of the field must be compatible with
3498 // obj_type, we've discovered this wasn't so, so report the field didn't exist.
3499 Fail(VERIFY_ERROR_NO_FIELD) << "cannot access instance field " << PrettyField(field)
3500 << " from object of type " << obj_type;
3501 return NULL;
3502 } else {
3503 return field;
3504 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003505 }
3506}
3507
Sebastien Hertz5243e912013-05-21 10:55:07 +02003508void MethodVerifier::VerifyISGet(const Instruction* inst, const RegType& insn_type,
3509 bool is_primitive, bool is_static) {
3510 uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
Brian Carlstromea46f952013-07-30 01:26:50 -07003511 mirror::ArtField* field;
Ian Rogersb94a27b2011-10-26 00:33:41 -07003512 if (is_static) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07003513 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003514 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003515 const RegType& object_type = work_line_->GetRegisterType(inst->VRegB_22c());
Ian Rogersf4028cc2011-11-02 14:56:39 -07003516 field = GetInstanceField(object_type, field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003517 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003518 const RegType* field_type = nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003519 if (field != NULL) {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003520 FieldHelper fh(field);
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08003521 mirror::Class* field_type_class = fh.GetType(can_load_classes_);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003522 if (field_type_class != nullptr) {
3523 field_type = &reg_types_.FromClass(fh.GetTypeDescriptor(), field_type_class,
3524 field_type_class->CannotBeAssignedFromOtherTypes());
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08003525 } else {
3526 Thread* self = Thread::Current();
3527 DCHECK(!can_load_classes_ || self->IsExceptionPending());
3528 self->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003529 }
Ian Rogers0d604842012-04-16 14:50:24 -07003530 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003531 if (field_type == nullptr) {
3532 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3533 const char* descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
Mathieu Chartier590fee92013-09-13 13:46:47 -07003534 field_type = &reg_types_.FromDescriptor(class_loader_->get(), descriptor, false);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003535 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02003536 const uint32_t vregA = (is_static) ? inst->VRegA_21c() : inst->VRegA_22c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07003537 if (is_primitive) {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003538 if (field_type->Equals(insn_type) ||
3539 (field_type->IsFloat() && insn_type.IsInteger()) ||
3540 (field_type->IsDouble() && insn_type.IsLong())) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003541 // expected that read is of the correct primitive type or that int reads are reading
3542 // floats or long reads are reading doubles
3543 } else {
3544 // This is a global failure rather than a class change failure as the instructions and
3545 // the descriptors for the type should have been consistent within the same file at
3546 // compile time
3547 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
3548 << " to be of type '" << insn_type
3549 << "' but found type '" << field_type << "' in get";
Ian Rogersad0b3a32012-04-16 14:50:24 -07003550 return;
3551 }
3552 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003553 if (!insn_type.IsAssignableFrom(*field_type)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003554 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
3555 << " to be compatible with type '" << insn_type
3556 << "' but found type '" << field_type
3557 << "' in get-object";
Sebastien Hertz5243e912013-05-21 10:55:07 +02003558 work_line_->SetRegisterType(vregA, reg_types_.Conflict());
Ian Rogersad0b3a32012-04-16 14:50:24 -07003559 return;
3560 }
3561 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003562 if (!field_type->IsLowHalf()) {
3563 work_line_->SetRegisterType(vregA, *field_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003564 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003565 work_line_->SetRegisterTypeWide(vregA, *field_type, field_type->HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003566 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003567}
3568
Sebastien Hertz5243e912013-05-21 10:55:07 +02003569void MethodVerifier::VerifyISPut(const Instruction* inst, const RegType& insn_type,
3570 bool is_primitive, bool is_static) {
3571 uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
Brian Carlstromea46f952013-07-30 01:26:50 -07003572 mirror::ArtField* field;
Ian Rogersb94a27b2011-10-26 00:33:41 -07003573 if (is_static) {
Ian Rogers55d249f2011-11-02 16:48:09 -07003574 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003575 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003576 const RegType& object_type = work_line_->GetRegisterType(inst->VRegB_22c());
Ian Rogers55d249f2011-11-02 16:48:09 -07003577 field = GetInstanceField(object_type, field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003578 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003579 const RegType* field_type = nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003580 if (field != NULL) {
3581 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
3582 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
3583 << " from other class " << GetDeclaringClass();
3584 return;
3585 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003586 FieldHelper fh(field);
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08003587 mirror::Class* field_type_class = fh.GetType(can_load_classes_);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003588 if (field_type_class != nullptr) {
3589 field_type = &reg_types_.FromClass(fh.GetTypeDescriptor(), field_type_class,
3590 field_type_class->CannotBeAssignedFromOtherTypes());
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08003591 } else {
3592 Thread* self = Thread::Current();
3593 DCHECK(!can_load_classes_ || self->IsExceptionPending());
3594 self->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003595 }
3596 }
3597 if (field_type == nullptr) {
3598 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3599 const char* descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
Mathieu Chartier590fee92013-09-13 13:46:47 -07003600 field_type = &reg_types_.FromDescriptor(class_loader_->get(), descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003601 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02003602 const uint32_t vregA = (is_static) ? inst->VRegA_21c() : inst->VRegA_22c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07003603 if (is_primitive) {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003604 VerifyPrimitivePut(*field_type, insn_type, vregA);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003605 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003606 if (!insn_type.IsAssignableFrom(*field_type)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003607 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
3608 << " to be compatible with type '" << insn_type
3609 << "' but found type '" << field_type
3610 << "' in put-object";
3611 return;
3612 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003613 work_line_->VerifyRegisterType(vregA, *field_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07003614 }
3615}
3616
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003617// Look for an instance field with this offset.
3618// TODO: we may speed up the search if offsets are sorted by doing a quick search.
Ian Rogersef7d42f2014-01-06 12:55:46 -08003619static mirror::ArtField* FindInstanceFieldWithOffset(mirror::Class* klass, uint32_t field_offset)
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003620 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08003621 mirror::ObjectArray<mirror::ArtField>* instance_fields = klass->GetIFields();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003622 if (instance_fields != NULL) {
3623 for (int32_t i = 0, e = instance_fields->GetLength(); i < e; ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -07003624 mirror::ArtField* field = instance_fields->Get(i);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003625 if (field->GetOffset().Uint32Value() == field_offset) {
3626 return field;
3627 }
3628 }
3629 }
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003630 // We did not find field in class: look into superclass.
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003631 if (klass->GetSuperClass() != NULL) {
3632 return FindInstanceFieldWithOffset(klass->GetSuperClass(), field_offset);
3633 } else {
3634 return NULL;
3635 }
3636}
3637
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003638// Returns the access field of a quick field access (iget/iput-quick) or NULL
3639// if it cannot be found.
Brian Carlstromea46f952013-07-30 01:26:50 -07003640mirror::ArtField* MethodVerifier::GetQuickFieldAccess(const Instruction* inst,
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003641 RegisterLine* reg_line) {
3642 DCHECK(inst->Opcode() == Instruction::IGET_QUICK ||
3643 inst->Opcode() == Instruction::IGET_WIDE_QUICK ||
3644 inst->Opcode() == Instruction::IGET_OBJECT_QUICK ||
3645 inst->Opcode() == Instruction::IPUT_QUICK ||
3646 inst->Opcode() == Instruction::IPUT_WIDE_QUICK ||
3647 inst->Opcode() == Instruction::IPUT_OBJECT_QUICK);
3648 const RegType& object_type = reg_line->GetRegisterType(inst->VRegB_22c());
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003649 mirror::Class* object_class = NULL;
3650 if (!object_type.IsUnresolvedTypes()) {
3651 object_class = object_type.GetClass();
3652 } else {
3653 // We need to resolve the class from its descriptor.
3654 const std::string& descriptor(object_type.GetDescriptor());
3655 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartier590fee92013-09-13 13:46:47 -07003656 Thread* self = Thread::Current();
3657 object_class = class_linker->FindClass(descriptor.c_str(), *class_loader_);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003658 if (object_class == NULL) {
Mathieu Chartier590fee92013-09-13 13:46:47 -07003659 self->ClearException();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003660 // Look for a system class
Mathieu Chartier590fee92013-09-13 13:46:47 -07003661 SirtRef<mirror::ClassLoader> null_class_loader(self, nullptr);
3662 object_class = class_linker->FindClass(descriptor.c_str(), null_class_loader);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003663 }
3664 }
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003665 if (object_class == NULL) {
3666 // Failed to get the Class* from reg type.
3667 LOG(WARNING) << "Failed to get Class* from " << object_type;
3668 return NULL;
3669 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003670 uint32_t field_offset = static_cast<uint32_t>(inst->VRegC_22c());
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003671 return FindInstanceFieldWithOffset(object_class, field_offset);
3672}
3673
3674void MethodVerifier::VerifyIGetQuick(const Instruction* inst, const RegType& insn_type,
3675 bool is_primitive) {
3676 DCHECK(Runtime::Current()->IsStarted());
Brian Carlstromea46f952013-07-30 01:26:50 -07003677 mirror::ArtField* field = GetQuickFieldAccess(inst, work_line_.get());
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003678 if (field == NULL) {
3679 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field from " << inst->Name();
3680 return;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003681 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003682 FieldHelper fh(field);
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08003683 mirror::Class* field_type_class = fh.GetType(can_load_classes_);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003684 const RegType* field_type;
3685 if (field_type_class != nullptr) {
3686 field_type = &reg_types_.FromClass(fh.GetTypeDescriptor(), field_type_class,
3687 field_type_class->CannotBeAssignedFromOtherTypes());
3688 } else {
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08003689 Thread* self = Thread::Current();
3690 DCHECK(!can_load_classes_ || self->IsExceptionPending());
3691 self->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003692 field_type = &reg_types_.FromDescriptor(field->GetDeclaringClass()->GetClassLoader(),
3693 fh.GetTypeDescriptor(), false);
3694 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003695 const uint32_t vregA = inst->VRegA_22c();
3696 if (is_primitive) {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003697 if (field_type->Equals(insn_type) ||
3698 (field_type->IsFloat() && insn_type.IsIntegralTypes()) ||
3699 (field_type->IsDouble() && insn_type.IsLongTypes())) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003700 // expected that read is of the correct primitive type or that int reads are reading
3701 // floats or long reads are reading doubles
3702 } else {
3703 // This is a global failure rather than a class change failure as the instructions and
3704 // the descriptors for the type should have been consistent within the same file at
3705 // compile time
3706 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003707 << " to be of type '" << insn_type
3708 << "' but found type '" << field_type << "' in get";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003709 return;
3710 }
3711 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003712 if (!insn_type.IsAssignableFrom(*field_type)) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003713 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003714 << " to be compatible with type '" << insn_type
3715 << "' but found type '" << field_type
3716 << "' in get-object";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003717 work_line_->SetRegisterType(vregA, reg_types_.Conflict());
3718 return;
3719 }
3720 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003721 if (!field_type->IsLowHalf()) {
3722 work_line_->SetRegisterType(vregA, *field_type);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003723 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003724 work_line_->SetRegisterTypeWide(vregA, *field_type, field_type->HighHalf(&reg_types_));
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003725 }
3726}
3727
3728void MethodVerifier::VerifyIPutQuick(const Instruction* inst, const RegType& insn_type,
3729 bool is_primitive) {
3730 DCHECK(Runtime::Current()->IsStarted());
Brian Carlstromea46f952013-07-30 01:26:50 -07003731 mirror::ArtField* field = GetQuickFieldAccess(inst, work_line_.get());
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003732 if (field == NULL) {
3733 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field from " << inst->Name();
3734 return;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003735 }
3736 const char* descriptor = FieldHelper(field).GetTypeDescriptor();
3737 mirror::ClassLoader* loader = field->GetDeclaringClass()->GetClassLoader();
3738 const RegType& field_type = reg_types_.FromDescriptor(loader, descriptor, false);
3739 if (field != NULL) {
3740 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
3741 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003742 << " from other class " << GetDeclaringClass();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003743 return;
3744 }
3745 }
3746 const uint32_t vregA = inst->VRegA_22c();
3747 if (is_primitive) {
3748 // Primitive field assignability rules are weaker than regular assignability rules
3749 bool instruction_compatible;
3750 bool value_compatible;
3751 const RegType& value_type = work_line_->GetRegisterType(vregA);
3752 if (field_type.IsIntegralTypes()) {
3753 instruction_compatible = insn_type.IsIntegralTypes();
3754 value_compatible = value_type.IsIntegralTypes();
3755 } else if (field_type.IsFloat()) {
3756 instruction_compatible = insn_type.IsInteger(); // no [is]put-float, so expect [is]put-int
3757 value_compatible = value_type.IsFloatTypes();
3758 } else if (field_type.IsLong()) {
3759 instruction_compatible = insn_type.IsLong();
3760 value_compatible = value_type.IsLongTypes();
3761 } else if (field_type.IsDouble()) {
3762 instruction_compatible = insn_type.IsLong(); // no [is]put-double, so expect [is]put-long
3763 value_compatible = value_type.IsDoubleTypes();
3764 } else {
3765 instruction_compatible = false; // reference field with primitive store
3766 value_compatible = false; // unused
3767 }
3768 if (!instruction_compatible) {
3769 // This is a global failure rather than a class change failure as the instructions and
3770 // the descriptors for the type should have been consistent within the same file at
3771 // compile time
3772 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003773 << " to be of type '" << insn_type
3774 << "' but found type '" << field_type
3775 << "' in put";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003776 return;
3777 }
3778 if (!value_compatible) {
3779 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
3780 << " of type " << value_type
3781 << " but expected " << field_type
3782 << " for store to " << PrettyField(field) << " in put";
3783 return;
3784 }
3785 } else {
3786 if (!insn_type.IsAssignableFrom(field_type)) {
3787 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003788 << " to be compatible with type '" << insn_type
3789 << "' but found type '" << field_type
3790 << "' in put-object";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003791 return;
3792 }
3793 work_line_->VerifyRegisterType(vregA, field_type);
3794 }
3795}
3796
Ian Rogers776ac1f2012-04-13 23:36:36 -07003797bool MethodVerifier::CheckNotMoveException(const uint16_t* insns, int insn_idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003798 if ((insns[insn_idx] & 0xff) == Instruction::MOVE_EXCEPTION) {
jeffhaod5347e02012-03-22 17:25:05 -07003799 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-exception";
Ian Rogersd81871c2011-10-03 13:57:23 -07003800 return false;
3801 }
3802 return true;
3803}
3804
Ian Rogers776ac1f2012-04-13 23:36:36 -07003805bool MethodVerifier::UpdateRegisters(uint32_t next_insn, const RegisterLine* merge_line) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003806 bool changed = true;
3807 RegisterLine* target_line = reg_table_.GetLine(next_insn);
3808 if (!insn_flags_[next_insn].IsVisitedOrChanged()) {
jeffhaobdb76512011-09-07 11:43:16 -07003809 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003810 * We haven't processed this instruction before, and we haven't touched the registers here, so
3811 * there's nothing to "merge". Copy the registers over and mark it as changed. (This is the
3812 * only way a register can transition out of "unknown", so this is not just an optimization.)
jeffhaobdb76512011-09-07 11:43:16 -07003813 */
Ian Rogersb8c78592013-07-25 23:52:52 +00003814 if (!insn_flags_[next_insn].IsReturn()) {
3815 target_line->CopyFromLine(merge_line);
3816 } else {
Jeff Haob24b4a72013-07-31 13:47:31 -07003817 // Verify that the monitor stack is empty on return.
3818 if (!merge_line->VerifyMonitorStackEmpty()) {
3819 return false;
3820 }
Ian Rogersb8c78592013-07-25 23:52:52 +00003821 // For returns we only care about the operand to the return, all other registers are dead.
3822 // Initialize them as conflicts so they don't add to GC and deoptimization information.
3823 const Instruction* ret_inst = Instruction::At(code_item_->insns_ + next_insn);
3824 Instruction::Code opcode = ret_inst->Opcode();
3825 if ((opcode == Instruction::RETURN_VOID) || (opcode == Instruction::RETURN_VOID_BARRIER)) {
3826 target_line->MarkAllRegistersAsConflicts();
3827 } else {
3828 target_line->CopyFromLine(merge_line);
3829 if (opcode == Instruction::RETURN_WIDE) {
3830 target_line->MarkAllRegistersAsConflictsExceptWide(ret_inst->VRegA_11x());
3831 } else {
3832 target_line->MarkAllRegistersAsConflictsExcept(ret_inst->VRegA_11x());
3833 }
3834 }
3835 }
jeffhaobdb76512011-09-07 11:43:16 -07003836 } else {
Brian Carlstrom93c33962013-07-26 10:37:43 -07003837 UniquePtr<RegisterLine> copy(gDebugVerify ?
Ian Rogersd0fbd852013-09-24 18:17:04 -07003838 RegisterLine::Create(target_line->NumRegs(), this) :
Brian Carlstrom93c33962013-07-26 10:37:43 -07003839 NULL);
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003840 if (gDebugVerify) {
3841 copy->CopyFromLine(target_line);
3842 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003843 changed = target_line->MergeRegisters(merge_line);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003844 if (have_pending_hard_failure_) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003845 return false;
jeffhaobdb76512011-09-07 11:43:16 -07003846 }
Ian Rogers2c8a8572011-10-24 17:11:36 -07003847 if (gDebugVerify && changed) {
Elliott Hughes398f64b2012-03-26 18:05:48 -07003848 LogVerifyInfo() << "Merging at [" << reinterpret_cast<void*>(work_insn_idx_) << "]"
Elliott Hughesc073b072012-05-24 19:29:17 -07003849 << " to [" << reinterpret_cast<void*>(next_insn) << "]: " << "\n"
3850 << *copy.get() << " MERGE\n"
3851 << *merge_line << " ==\n"
3852 << *target_line << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07003853 }
3854 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003855 if (changed) {
3856 insn_flags_[next_insn].SetChanged();
jeffhaobdb76512011-09-07 11:43:16 -07003857 }
3858 return true;
3859}
3860
Ian Rogers7b3ddd22013-02-21 15:19:52 -08003861InstructionFlags* MethodVerifier::CurrentInsnFlags() {
Ian Rogers776ac1f2012-04-13 23:36:36 -07003862 return &insn_flags_[work_insn_idx_];
3863}
3864
Ian Rogersad0b3a32012-04-16 14:50:24 -07003865const RegType& MethodVerifier::GetMethodReturnType() {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003866 if (return_type_ == nullptr) {
3867 if (mirror_method_ != NULL) {
3868 MethodHelper mh(mirror_method_);
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08003869 mirror::Class* return_type_class = mh.GetReturnType(can_load_classes_);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003870 if (return_type_class != nullptr) {
Mathieu Chartier590fee92013-09-13 13:46:47 -07003871 return_type_ = &reg_types_.FromClass(mh.GetReturnTypeDescriptor(), return_type_class,
3872 return_type_class->CannotBeAssignedFromOtherTypes());
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003873 } else {
3874 Thread* self = Thread::Current();
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08003875 DCHECK(!can_load_classes_ || self->IsExceptionPending());
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003876 self->ClearException();
3877 }
3878 }
3879 if (return_type_ == nullptr) {
3880 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
3881 const DexFile::ProtoId& proto_id = dex_file_->GetMethodPrototype(method_id);
3882 uint16_t return_type_idx = proto_id.return_type_idx_;
3883 const char* descriptor = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(return_type_idx));
Mathieu Chartier590fee92013-09-13 13:46:47 -07003884 return_type_ = &reg_types_.FromDescriptor(class_loader_->get(), descriptor, false);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003885 }
3886 }
3887 return *return_type_;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003888}
3889
3890const RegType& MethodVerifier::GetDeclaringClass() {
Ian Rogers637c65b2013-05-31 11:46:00 -07003891 if (declaring_class_ == NULL) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003892 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
Brian Carlstrom93c33962013-07-26 10:37:43 -07003893 const char* descriptor
3894 = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(method_id.class_idx_));
Ian Rogers637c65b2013-05-31 11:46:00 -07003895 if (mirror_method_ != NULL) {
3896 mirror::Class* klass = mirror_method_->GetDeclaringClass();
Ian Rogers04f94f42013-06-10 15:09:26 -07003897 declaring_class_ = &reg_types_.FromClass(descriptor, klass,
3898 klass->CannotBeAssignedFromOtherTypes());
Ian Rogers637c65b2013-05-31 11:46:00 -07003899 } else {
Mathieu Chartier590fee92013-09-13 13:46:47 -07003900 declaring_class_ = &reg_types_.FromDescriptor(class_loader_->get(), descriptor, false);
Ian Rogers637c65b2013-05-31 11:46:00 -07003901 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003902 }
Ian Rogers637c65b2013-05-31 11:46:00 -07003903 return *declaring_class_;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003904}
3905
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003906std::vector<int32_t> MethodVerifier::DescribeVRegs(uint32_t dex_pc) {
3907 RegisterLine* line = reg_table_.GetLine(dex_pc);
3908 std::vector<int32_t> result;
3909 for (size_t i = 0; i < line->NumRegs(); ++i) {
3910 const RegType& type = line->GetRegisterType(i);
3911 if (type.IsConstant()) {
3912 result.push_back(type.IsPreciseConstant() ? kConstant : kImpreciseConstant);
3913 result.push_back(type.ConstantValue());
3914 } else if (type.IsConstantLo()) {
3915 result.push_back(type.IsPreciseConstantLo() ? kConstant : kImpreciseConstant);
3916 result.push_back(type.ConstantValueLo());
3917 } else if (type.IsConstantHi()) {
3918 result.push_back(type.IsPreciseConstantHi() ? kConstant : kImpreciseConstant);
3919 result.push_back(type.ConstantValueHi());
3920 } else if (type.IsIntegralTypes()) {
3921 result.push_back(kIntVReg);
3922 result.push_back(0);
3923 } else if (type.IsFloat()) {
3924 result.push_back(kFloatVReg);
3925 result.push_back(0);
3926 } else if (type.IsLong()) {
3927 result.push_back(kLongLoVReg);
3928 result.push_back(0);
3929 result.push_back(kLongHiVReg);
3930 result.push_back(0);
3931 ++i;
3932 } else if (type.IsDouble()) {
3933 result.push_back(kDoubleLoVReg);
3934 result.push_back(0);
3935 result.push_back(kDoubleHiVReg);
3936 result.push_back(0);
3937 ++i;
3938 } else if (type.IsUndefined() || type.IsConflict() || type.IsHighHalf()) {
3939 result.push_back(kUndefined);
3940 result.push_back(0);
3941 } else {
Ian Rogers7b3ddd22013-02-21 15:19:52 -08003942 CHECK(type.IsNonZeroReferenceTypes());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003943 result.push_back(kReferenceVReg);
3944 result.push_back(0);
3945 }
3946 }
3947 return result;
3948}
3949
Sebastien Hertz849600b2013-12-20 10:28:08 +01003950const RegType& MethodVerifier::DetermineCat1Constant(int32_t value, bool precise) {
3951 if (precise) {
3952 // Precise constant type.
3953 return reg_types_.FromCat1Const(value, true);
3954 } else {
3955 // Imprecise constant type.
3956 if (value < -32768) {
3957 return reg_types_.IntConstant();
3958 } else if (value < -128) {
3959 return reg_types_.ShortConstant();
3960 } else if (value < 0) {
3961 return reg_types_.ByteConstant();
3962 } else if (value == 0) {
3963 return reg_types_.Zero();
3964 } else if (value == 1) {
3965 return reg_types_.One();
3966 } else if (value < 128) {
3967 return reg_types_.PosByteConstant();
3968 } else if (value < 32768) {
3969 return reg_types_.PosShortConstant();
3970 } else if (value < 65536) {
3971 return reg_types_.CharConstant();
3972 } else {
3973 return reg_types_.IntConstant();
3974 }
3975 }
3976}
3977
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003978void MethodVerifier::Init() {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08003979 art::verifier::RegTypeCache::Init();
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003980}
3981
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003982void MethodVerifier::Shutdown() {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08003983 verifier::RegTypeCache::ShutDown();
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08003984}
jeffhaod1224c72012-02-29 13:43:08 -08003985
Mathieu Chartier83c8ee02014-01-28 14:50:23 -08003986void MethodVerifier::VisitRoots(RootCallback* callback, void* arg) {
3987 reg_types_.VisitRoots(callback, arg);
Mathieu Chartierc528dba2013-11-26 12:00:11 -08003988}
3989
Ian Rogersd81871c2011-10-03 13:57:23 -07003990} // namespace verifier
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07003991} // namespace art