blob: 9dd366dcc2d9a116cf962f851e501242201500be [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070016
Mathieu Chartierc645f1d2014-03-06 18:11:53 -080017#include "method_verifier-inl.h"
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070018
Elliott Hughes1f359b02011-07-17 14:27:17 -070019#include <iostream>
20
Elliott Hughes07ed66b2012-12-12 18:34:25 -080021#include "base/logging.h"
Ian Rogers637c65b2013-05-31 11:46:00 -070022#include "base/mutex-inl.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070023#include "class_linker.h"
Vladimir Marko2b5eaa22013-12-13 13:59:30 +000024#include "compiler_callbacks.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070025#include "dex_file-inl.h"
Ian Rogersd0583802013-06-01 10:51:46 -070026#include "dex_instruction-inl.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070027#include "dex_instruction_visitor.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070028#include "gc/accounting/card_table-inl.h"
Ian Rogers2bcb4a42012-11-08 10:39:18 -080029#include "indenter.h"
Ian Rogers84fa0742011-10-25 18:13:30 -070030#include "intern_table.h"
Ian Rogers0571d352011-11-03 19:51:38 -070031#include "leb128.h"
Brian Carlstromea46f952013-07-30 01:26:50 -070032#include "mirror/art_field-inl.h"
33#include "mirror/art_method-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080034#include "mirror/class.h"
35#include "mirror/class-inl.h"
Ian Rogers39ebcb82013-05-30 16:57:23 -070036#include "mirror/dex_cache-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080037#include "mirror/object-inl.h"
38#include "mirror/object_array-inl.h"
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080039#include "object_utils.h"
Ian Rogers39ebcb82013-05-30 16:57:23 -070040#include "register_line-inl.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070041#include "runtime.h"
Mathieu Chartier590fee92013-09-13 13:46:47 -070042#include "scoped_thread_state_change.h"
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070043#include "handle_scope-inl.h"
Elliott Hughese222ee02012-12-13 14:41:43 -080044#include "verifier/dex_gc_map.h"
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070045
46namespace art {
Ian Rogersd81871c2011-10-03 13:57:23 -070047namespace verifier {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070048
Ian Rogers2c8a8572011-10-24 17:11:36 -070049static const bool gDebugVerify = false;
Anwar Ghuloum75a43f12013-08-13 17:22:14 -070050// TODO: Add a constant to method_verifier to turn on verbose logging?
Ian Rogers2c8a8572011-10-24 17:11:36 -070051
Ian Rogers7b3ddd22013-02-21 15:19:52 -080052void PcToRegisterLineTable::Init(RegisterTrackingMode mode, InstructionFlags* flags,
Ian Rogersd81871c2011-10-03 13:57:23 -070053 uint32_t insns_size, uint16_t registers_size,
Ian Rogers776ac1f2012-04-13 23:36:36 -070054 MethodVerifier* verifier) {
Ian Rogersd81871c2011-10-03 13:57:23 -070055 DCHECK_GT(insns_size, 0U);
Ian Rogersd0fbd852013-09-24 18:17:04 -070056 register_lines_.reset(new RegisterLine*[insns_size]());
57 size_ = insns_size;
Ian Rogersd81871c2011-10-03 13:57:23 -070058 for (uint32_t i = 0; i < insns_size; i++) {
59 bool interesting = false;
60 switch (mode) {
61 case kTrackRegsAll:
62 interesting = flags[i].IsOpcode();
63 break;
Sameer Abu Asal02c42232013-04-30 12:09:45 -070064 case kTrackCompilerInterestPoints:
Brian Carlstrom02c8cc62013-07-18 15:54:44 -070065 interesting = flags[i].IsCompileTimeInfoPoint() || flags[i].IsBranchTarget();
Ian Rogersd81871c2011-10-03 13:57:23 -070066 break;
67 case kTrackRegsBranches:
68 interesting = flags[i].IsBranchTarget();
69 break;
70 default:
71 break;
72 }
73 if (interesting) {
Ian Rogersd0fbd852013-09-24 18:17:04 -070074 register_lines_[i] = RegisterLine::Create(registers_size, verifier);
75 }
76 }
77}
78
79PcToRegisterLineTable::~PcToRegisterLineTable() {
80 for (size_t i = 0; i < size_; i++) {
81 delete register_lines_[i];
82 if (kIsDebugBuild) {
83 register_lines_[i] = nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -070084 }
85 }
86}
87
Ian Rogersef7d42f2014-01-06 12:55:46 -080088MethodVerifier::FailureKind MethodVerifier::VerifyClass(mirror::Class* klass,
Ian Rogers8b2c0b92013-09-19 02:56:49 -070089 bool allow_soft_failures,
90 std::string* error) {
jeffhaobdb76512011-09-07 11:43:16 -070091 if (klass->IsVerified()) {
jeffhaof1e6b7c2012-06-05 18:33:30 -070092 return kNoFailure;
jeffhaobdb76512011-09-07 11:43:16 -070093 }
Jeff Hao2d7e5aa2013-12-13 17:39:59 -080094 bool early_failure = false;
95 std::string failure_message;
Ian Rogersad0b3a32012-04-16 14:50:24 -070096 ClassHelper kh(klass);
97 const DexFile& dex_file = kh.GetDexFile();
Ian Rogers8b2c0b92013-09-19 02:56:49 -070098 const DexFile::ClassDef* class_def = kh.GetClassDef();
Jeff Hao2d7e5aa2013-12-13 17:39:59 -080099 mirror::Class* super = klass->GetSuperClass();
100 if (super == NULL && strcmp("Ljava/lang/Object;", kh.GetDescriptor()) != 0) {
101 early_failure = true;
102 failure_message = " that has no super class";
103 } else if (super != NULL && super->IsFinal()) {
104 early_failure = true;
105 failure_message = " that attempts to sub-class final class " + PrettyDescriptor(super);
106 } else if (class_def == NULL) {
107 early_failure = true;
108 failure_message = " that isn't present in dex file " + dex_file.GetLocation();
109 }
110 if (early_failure) {
111 *error = "Verifier rejected class " + PrettyDescriptor(klass) + failure_message;
112 if (Runtime::Current()->IsCompiler()) {
113 ClassReference ref(&dex_file, klass->GetDexClassDefIndex());
Vladimir Marko2b5eaa22013-12-13 13:59:30 +0000114 Runtime::Current()->GetCompilerCallbacks()->ClassRejected(ref);
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800115 }
jeffhaof1e6b7c2012-06-05 18:33:30 -0700116 return kHardFailure;
jeffhaobdb76512011-09-07 11:43:16 -0700117 }
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700118 StackHandleScope<2> hs(Thread::Current());
119 Handle<mirror::DexCache> dex_cache(hs.NewHandle(kh.GetDexCache()));
120 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(klass->GetClassLoader()));
Mathieu Chartier590fee92013-09-13 13:46:47 -0700121 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 Chartiereb8167a2014-05-07 15:43:14 -0700125 Handle<mirror::DexCache>& dex_cache,
126 Handle<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 Chartiereb8167a2014-05-07 15:43:14 -0700236 Handle<mirror::DexCache>& dex_cache,
237 Handle<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,
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700281 Handle<mirror::DexCache>& dex_cache,
282 Handle<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 Chartiereb8167a2014-05-07 15:43:14 -0700295MethodVerifier::MethodVerifier(const DexFile* dex_file, Handle<mirror::DexCache>* dex_cache,
296 Handle<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 Chartiereb8167a2014-05-07 15:43:14 -0700335 StackHandleScope<2> hs(Thread::Current());
336 Handle<mirror::DexCache> dex_cache(hs.NewHandle(mh.GetDexCache()));
337 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(mh.GetClassLoader()));
Mathieu Chartier590fee92013-09-13 13:46:47 -0700338 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 Chartiereb8167a2014-05-07 15:43:14 -0700360 StackHandleScope<2> hs(Thread::Current());
361 Handle<mirror::DexCache> dex_cache(hs.NewHandle(mh.GetDexCache()));
362 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(mh.GetClassLoader()));
Mathieu Chartier590fee92013-09-13 13:46:47 -0700363 MethodVerifier verifier(&mh.GetDexFile(), &dex_cache, &class_loader, &mh.GetClassDef(),
Ian Rogers9bc54402014-04-17 16:40:01 -0700364 mh.GetCodeItem(), m->GetDexMethodIndex(), m, m->GetAccessFlags(), true,
Mathieu Chartier590fee92013-09-13 13:46:47 -0700365 true);
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200366 return verifier.FindAccessedFieldAtDexPc(dex_pc);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200367}
368
Brian Carlstromea46f952013-07-30 01:26:50 -0700369mirror::ArtField* MethodVerifier::FindAccessedFieldAtDexPc(uint32_t dex_pc) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700370 CHECK(code_item_ != NULL); // This only makes sense for methods with code.
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200371
372 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
373 // verification. In practice, the phase we want relies on data structures set up by all the
374 // earlier passes, so we just run the full method verification and bail out early when we've
375 // got what we wanted.
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200376 bool success = Verify();
377 if (!success) {
Ian Rogers9bc54402014-04-17 16:40:01 -0700378 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200379 }
380 RegisterLine* register_line = reg_table_.GetLine(dex_pc);
381 if (register_line == NULL) {
Ian Rogers9bc54402014-04-17 16:40:01 -0700382 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200383 }
384 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
385 return GetQuickFieldAccess(inst, register_line);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200386}
387
Brian Carlstromea46f952013-07-30 01:26:50 -0700388mirror::ArtMethod* MethodVerifier::FindInvokedMethodAtDexPc(mirror::ArtMethod* m,
Mathieu Chartier590fee92013-09-13 13:46:47 -0700389 uint32_t dex_pc) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200390 MethodHelper mh(m);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700391 StackHandleScope<2> hs(Thread::Current());
392 Handle<mirror::DexCache> dex_cache(hs.NewHandle(mh.GetDexCache()));
393 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(mh.GetClassLoader()));
Mathieu Chartier590fee92013-09-13 13:46:47 -0700394 MethodVerifier verifier(&mh.GetDexFile(), &dex_cache, &class_loader, &mh.GetClassDef(),
Andreas Gampe63981562014-04-17 12:28:43 -0700395 mh.GetCodeItem(), m->GetDexMethodIndex(), m, m->GetAccessFlags(), true,
Mathieu Chartier590fee92013-09-13 13:46:47 -0700396 true);
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200397 return verifier.FindInvokedMethodAtDexPc(dex_pc);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200398}
399
Brian Carlstromea46f952013-07-30 01:26:50 -0700400mirror::ArtMethod* MethodVerifier::FindInvokedMethodAtDexPc(uint32_t dex_pc) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700401 CHECK(code_item_ != NULL); // This only makes sense for methods with code.
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200402
403 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
404 // verification. In practice, the phase we want relies on data structures set up by all the
405 // earlier passes, so we just run the full method verification and bail out early when we've
406 // got what we wanted.
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200407 bool success = Verify();
408 if (!success) {
409 return NULL;
410 }
411 RegisterLine* register_line = reg_table_.GetLine(dex_pc);
412 if (register_line == NULL) {
413 return NULL;
414 }
415 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
416 const bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
417 return GetQuickInvokedMethod(inst, register_line, is_range);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200418}
419
Ian Rogersad0b3a32012-04-16 14:50:24 -0700420bool MethodVerifier::Verify() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700421 // If there aren't any instructions, make sure that's expected, then exit successfully.
422 if (code_item_ == NULL) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700423 if ((method_access_flags_ & (kAccNative | kAccAbstract)) == 0) {
jeffhaod5347e02012-03-22 17:25:05 -0700424 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "zero-length code in concrete non-native method";
jeffhaobdb76512011-09-07 11:43:16 -0700425 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -0700426 } else {
427 return true;
jeffhaobdb76512011-09-07 11:43:16 -0700428 }
jeffhaobdb76512011-09-07 11:43:16 -0700429 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700430 // Sanity-check the register counts. ins + locals = registers, so make sure that ins <= registers.
431 if (code_item_->ins_size_ > code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700432 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad register counts (ins=" << code_item_->ins_size_
433 << " regs=" << code_item_->registers_size_;
Ian Rogersd81871c2011-10-03 13:57:23 -0700434 return false;
jeffhaobdb76512011-09-07 11:43:16 -0700435 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700436 // Allocate and initialize an array to hold instruction data.
Ian Rogers7b3ddd22013-02-21 15:19:52 -0800437 insn_flags_.reset(new InstructionFlags[code_item_->insns_size_in_code_units_]());
Ian Rogersd81871c2011-10-03 13:57:23 -0700438 // Run through the instructions and see if the width checks out.
439 bool result = ComputeWidthsAndCountOps();
440 // Flag instructions guarded by a "try" block and check exception handlers.
441 result = result && ScanTryCatchBlocks();
442 // Perform static instruction verification.
443 result = result && VerifyInstructions();
Ian Rogersad0b3a32012-04-16 14:50:24 -0700444 // Perform code-flow analysis and return.
Vladimir Marko2b5eaa22013-12-13 13:59:30 +0000445 result = result && VerifyCodeFlow();
446 // Compute information for compiler.
447 if (result && Runtime::Current()->IsCompiler()) {
448 result = Runtime::Current()->GetCompilerCallbacks()->MethodVerified(this);
449 }
450 return result;
jeffhaoba5ebb92011-08-25 17:24:37 -0700451}
452
Ian Rogers776ac1f2012-04-13 23:36:36 -0700453std::ostream& MethodVerifier::Fail(VerifyError error) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700454 switch (error) {
455 case VERIFY_ERROR_NO_CLASS:
456 case VERIFY_ERROR_NO_FIELD:
457 case VERIFY_ERROR_NO_METHOD:
458 case VERIFY_ERROR_ACCESS_CLASS:
459 case VERIFY_ERROR_ACCESS_FIELD:
460 case VERIFY_ERROR_ACCESS_METHOD:
Ian Rogers08f753d2012-08-24 14:35:25 -0700461 case VERIFY_ERROR_INSTANTIATION:
462 case VERIFY_ERROR_CLASS_CHANGE:
Elliott Hughes4993bbc2013-01-10 15:41:25 -0800463 if (Runtime::Current()->IsCompiler() || !can_load_classes_) {
jeffhaofaf459e2012-08-31 15:32:47 -0700464 // If we're optimistically running verification at compile time, turn NO_xxx, ACCESS_xxx,
465 // class change and instantiation errors into soft verification errors so that we re-verify
466 // at runtime. We may fail to find or to agree on access because of not yet available class
467 // loaders, or class loaders that will differ at runtime. In these cases, we don't want to
468 // affect the soundness of the code being compiled. Instead, the generated code runs "slow
469 // paths" that dynamically perform the verification and cause the behavior to be that akin
470 // to an interpreter.
471 error = VERIFY_ERROR_BAD_CLASS_SOFT;
472 } else {
Jeff Haoa3faaf42013-09-03 19:07:00 -0700473 // If we fail again at runtime, mark that this instruction would throw and force this
474 // method to be executed using the interpreter with checks.
jeffhaofaf459e2012-08-31 15:32:47 -0700475 have_pending_runtime_throw_failure_ = true;
476 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700477 break;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700478 // Indication that verification should be retried at runtime.
479 case VERIFY_ERROR_BAD_CLASS_SOFT:
Jeff Haoee988952013-04-16 14:23:47 -0700480 if (!allow_soft_failures_) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700481 have_pending_hard_failure_ = true;
482 }
483 break;
jeffhaod5347e02012-03-22 17:25:05 -0700484 // Hard verification failures at compile time will still fail at runtime, so the class is
485 // marked as rejected to prevent it from being compiled.
Ian Rogersad0b3a32012-04-16 14:50:24 -0700486 case VERIFY_ERROR_BAD_CLASS_HARD: {
487 if (Runtime::Current()->IsCompiler()) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700488 ClassReference ref(dex_file_, dex_file_->GetIndexForClassDef(*class_def_));
Vladimir Marko2b5eaa22013-12-13 13:59:30 +0000489 Runtime::Current()->GetCompilerCallbacks()->ClassRejected(ref);
jeffhaod1224c72012-02-29 13:43:08 -0800490 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700491 have_pending_hard_failure_ = true;
492 break;
Ian Rogers47a05882012-02-03 12:23:33 -0800493 }
494 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700495 failures_.push_back(error);
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800496 std::string location(StringPrintf("%s: [0x%X]", PrettyMethod(dex_method_idx_, *dex_file_).c_str(),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700497 work_insn_idx_));
498 std::ostringstream* failure_message = new std::ostringstream(location);
499 failure_messages_.push_back(failure_message);
500 return *failure_message;
501}
502
503void MethodVerifier::PrependToLastFailMessage(std::string prepend) {
504 size_t failure_num = failure_messages_.size();
505 DCHECK_NE(failure_num, 0U);
506 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
507 prepend += last_fail_message->str();
508 failure_messages_[failure_num - 1] = new std::ostringstream(prepend);
509 delete last_fail_message;
510}
511
512void MethodVerifier::AppendToLastFailMessage(std::string append) {
513 size_t failure_num = failure_messages_.size();
514 DCHECK_NE(failure_num, 0U);
515 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
516 (*last_fail_message) << append;
Ian Rogers47a05882012-02-03 12:23:33 -0800517}
518
Ian Rogers776ac1f2012-04-13 23:36:36 -0700519bool MethodVerifier::ComputeWidthsAndCountOps() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700520 const uint16_t* insns = code_item_->insns_;
521 size_t insns_size = code_item_->insns_size_in_code_units_;
522 const Instruction* inst = Instruction::At(insns);
jeffhaobdb76512011-09-07 11:43:16 -0700523 size_t new_instance_count = 0;
524 size_t monitor_enter_count = 0;
Ian Rogersd81871c2011-10-03 13:57:23 -0700525 size_t dex_pc = 0;
jeffhaobdb76512011-09-07 11:43:16 -0700526
Ian Rogersd81871c2011-10-03 13:57:23 -0700527 while (dex_pc < insns_size) {
jeffhaobdb76512011-09-07 11:43:16 -0700528 Instruction::Code opcode = inst->Opcode();
Ian Rogersa9a82542013-10-04 11:17:26 -0700529 switch (opcode) {
530 case Instruction::APUT_OBJECT:
531 case Instruction::CHECK_CAST:
532 has_check_casts_ = true;
533 break;
534 case Instruction::INVOKE_VIRTUAL:
535 case Instruction::INVOKE_VIRTUAL_RANGE:
536 case Instruction::INVOKE_INTERFACE:
537 case Instruction::INVOKE_INTERFACE_RANGE:
538 has_virtual_or_interface_invokes_ = true;
539 break;
540 case Instruction::MONITOR_ENTER:
541 monitor_enter_count++;
542 break;
543 case Instruction::NEW_INSTANCE:
544 new_instance_count++;
545 break;
546 default:
547 break;
jeffhaobdb76512011-09-07 11:43:16 -0700548 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700549 size_t inst_size = inst->SizeInCodeUnits();
550 insn_flags_[dex_pc].SetLengthInCodeUnits(inst_size);
551 dex_pc += inst_size;
jeffhaobdb76512011-09-07 11:43:16 -0700552 inst = inst->Next();
553 }
554
Ian Rogersd81871c2011-10-03 13:57:23 -0700555 if (dex_pc != insns_size) {
jeffhaod5347e02012-03-22 17:25:05 -0700556 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "code did not end where expected ("
557 << dex_pc << " vs. " << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700558 return false;
559 }
560
Ian Rogersd81871c2011-10-03 13:57:23 -0700561 new_instance_count_ = new_instance_count;
562 monitor_enter_count_ = monitor_enter_count;
jeffhaobdb76512011-09-07 11:43:16 -0700563 return true;
564}
565
Ian Rogers776ac1f2012-04-13 23:36:36 -0700566bool MethodVerifier::ScanTryCatchBlocks() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700567 uint32_t tries_size = code_item_->tries_size_;
jeffhaobdb76512011-09-07 11:43:16 -0700568 if (tries_size == 0) {
569 return true;
570 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700571 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Ian Rogers0571d352011-11-03 19:51:38 -0700572 const DexFile::TryItem* tries = DexFile::GetTryItems(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -0700573
574 for (uint32_t idx = 0; idx < tries_size; idx++) {
575 const DexFile::TryItem* try_item = &tries[idx];
576 uint32_t start = try_item->start_addr_;
577 uint32_t end = start + try_item->insn_count_;
jeffhaobdb76512011-09-07 11:43:16 -0700578 if ((start >= end) || (start >= insns_size) || (end > insns_size)) {
jeffhaod5347e02012-03-22 17:25:05 -0700579 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad exception entry: startAddr=" << start
580 << " endAddr=" << end << " (size=" << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700581 return false;
582 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700583 if (!insn_flags_[start].IsOpcode()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700584 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
585 << "'try' block starts inside an instruction (" << start << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700586 return false;
587 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700588 for (uint32_t dex_pc = start; dex_pc < end;
589 dex_pc += insn_flags_[dex_pc].GetLengthInCodeUnits()) {
590 insn_flags_[dex_pc].SetInTry();
jeffhaobdb76512011-09-07 11:43:16 -0700591 }
592 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800593 // Iterate over each of the handlers to verify target addresses.
Ian Rogers0571d352011-11-03 19:51:38 -0700594 const byte* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -0700595 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700596 ClassLinker* linker = Runtime::Current()->GetClassLinker();
jeffhaobdb76512011-09-07 11:43:16 -0700597 for (uint32_t idx = 0; idx < handlers_size; idx++) {
Ian Rogers0571d352011-11-03 19:51:38 -0700598 CatchHandlerIterator iterator(handlers_ptr);
599 for (; iterator.HasNext(); iterator.Next()) {
600 uint32_t dex_pc= iterator.GetHandlerAddress();
Ian Rogersd81871c2011-10-03 13:57:23 -0700601 if (!insn_flags_[dex_pc].IsOpcode()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700602 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
603 << "exception handler starts at bad address (" << dex_pc << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700604 return false;
605 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700606 insn_flags_[dex_pc].SetBranchTarget();
Ian Rogers28ad40d2011-10-27 15:19:26 -0700607 // Ensure exception types are resolved so that they don't need resolution to be delivered,
608 // unresolved exception types will be ignored by exception delivery
Ian Rogers0571d352011-11-03 19:51:38 -0700609 if (iterator.GetHandlerTypeIndex() != DexFile::kDexNoIndex16) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800610 mirror::Class* exception_type = linker->ResolveType(*dex_file_,
611 iterator.GetHandlerTypeIndex(),
Mathieu Chartier590fee92013-09-13 13:46:47 -0700612 *dex_cache_, *class_loader_);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700613 if (exception_type == NULL) {
614 DCHECK(Thread::Current()->IsExceptionPending());
615 Thread::Current()->ClearException();
616 }
617 }
jeffhaobdb76512011-09-07 11:43:16 -0700618 }
Ian Rogers0571d352011-11-03 19:51:38 -0700619 handlers_ptr = iterator.EndDataPointer();
jeffhaobdb76512011-09-07 11:43:16 -0700620 }
jeffhaobdb76512011-09-07 11:43:16 -0700621 return true;
622}
623
Ian Rogers776ac1f2012-04-13 23:36:36 -0700624bool MethodVerifier::VerifyInstructions() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700625 const Instruction* inst = Instruction::At(code_item_->insns_);
jeffhaoba5ebb92011-08-25 17:24:37 -0700626
Ian Rogers0c7abda2012-09-19 13:33:42 -0700627 /* Flag the start of the method as a branch target, and a GC point due to stack overflow errors */
Ian Rogersd81871c2011-10-03 13:57:23 -0700628 insn_flags_[0].SetBranchTarget();
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700629 insn_flags_[0].SetCompileTimeInfoPoint();
Ian Rogersd81871c2011-10-03 13:57:23 -0700630
631 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700632 for (uint32_t dex_pc = 0; dex_pc < insns_size;) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700633 if (!VerifyInstruction(inst, dex_pc)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700634 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -0700635 return false;
636 }
637 /* Flag instructions that are garbage collection points */
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700638 // All invoke points are marked as "Throw" points already.
639 // We are relying on this to also count all the invokes as interesting.
Ian Rogersb8c78592013-07-25 23:52:52 +0000640 if (inst->IsBranch() || inst->IsSwitch() || inst->IsThrow()) {
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700641 insn_flags_[dex_pc].SetCompileTimeInfoPoint();
Ian Rogersb8c78592013-07-25 23:52:52 +0000642 } else if (inst->IsReturn()) {
643 insn_flags_[dex_pc].SetCompileTimeInfoPointAndReturn();
Ian Rogersd81871c2011-10-03 13:57:23 -0700644 }
645 dex_pc += inst->SizeInCodeUnits();
646 inst = inst->Next();
647 }
648 return true;
649}
650
Ian Rogers776ac1f2012-04-13 23:36:36 -0700651bool MethodVerifier::VerifyInstruction(const Instruction* inst, uint32_t code_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700652 bool result = true;
653 switch (inst->GetVerifyTypeArgumentA()) {
654 case Instruction::kVerifyRegA:
Ian Rogers29a26482014-05-02 15:27:29 -0700655 result = result && CheckRegisterIndex(inst->VRegA());
Ian Rogersd81871c2011-10-03 13:57:23 -0700656 break;
657 case Instruction::kVerifyRegAWide:
Ian Rogers29a26482014-05-02 15:27:29 -0700658 result = result && CheckWideRegisterIndex(inst->VRegA());
Ian Rogersd81871c2011-10-03 13:57:23 -0700659 break;
660 }
661 switch (inst->GetVerifyTypeArgumentB()) {
662 case Instruction::kVerifyRegB:
Ian Rogers29a26482014-05-02 15:27:29 -0700663 result = result && CheckRegisterIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700664 break;
665 case Instruction::kVerifyRegBField:
Ian Rogers29a26482014-05-02 15:27:29 -0700666 result = result && CheckFieldIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700667 break;
668 case Instruction::kVerifyRegBMethod:
Ian Rogers29a26482014-05-02 15:27:29 -0700669 result = result && CheckMethodIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700670 break;
671 case Instruction::kVerifyRegBNewInstance:
Ian Rogers29a26482014-05-02 15:27:29 -0700672 result = result && CheckNewInstance(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700673 break;
674 case Instruction::kVerifyRegBString:
Ian Rogers29a26482014-05-02 15:27:29 -0700675 result = result && CheckStringIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700676 break;
677 case Instruction::kVerifyRegBType:
Ian Rogers29a26482014-05-02 15:27:29 -0700678 result = result && CheckTypeIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700679 break;
680 case Instruction::kVerifyRegBWide:
Ian Rogers29a26482014-05-02 15:27:29 -0700681 result = result && CheckWideRegisterIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700682 break;
683 }
684 switch (inst->GetVerifyTypeArgumentC()) {
685 case Instruction::kVerifyRegC:
Ian Rogers29a26482014-05-02 15:27:29 -0700686 result = result && CheckRegisterIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700687 break;
688 case Instruction::kVerifyRegCField:
Ian Rogers29a26482014-05-02 15:27:29 -0700689 result = result && CheckFieldIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700690 break;
691 case Instruction::kVerifyRegCNewArray:
Ian Rogers29a26482014-05-02 15:27:29 -0700692 result = result && CheckNewArray(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700693 break;
694 case Instruction::kVerifyRegCType:
Ian Rogers29a26482014-05-02 15:27:29 -0700695 result = result && CheckTypeIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700696 break;
697 case Instruction::kVerifyRegCWide:
Ian Rogers29a26482014-05-02 15:27:29 -0700698 result = result && CheckWideRegisterIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700699 break;
700 }
701 switch (inst->GetVerifyExtraFlags()) {
702 case Instruction::kVerifyArrayData:
703 result = result && CheckArrayData(code_offset);
704 break;
705 case Instruction::kVerifyBranchTarget:
706 result = result && CheckBranchTarget(code_offset);
707 break;
708 case Instruction::kVerifySwitchTargets:
709 result = result && CheckSwitchTargets(code_offset);
710 break;
Ian Rogers29a26482014-05-02 15:27:29 -0700711 case Instruction::kVerifyVarArg: {
712 uint32_t args[Instruction::kMaxVarArgRegs];
713 inst->GetVarArgs(args);
714 result = result && CheckVarArgRegs(inst->VRegA(), args);
Ian Rogersd81871c2011-10-03 13:57:23 -0700715 break;
Ian Rogers29a26482014-05-02 15:27:29 -0700716 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700717 case Instruction::kVerifyVarArgRange:
Ian Rogers29a26482014-05-02 15:27:29 -0700718 result = result && CheckVarArgRangeRegs(inst->VRegA(), inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700719 break;
720 case Instruction::kVerifyError:
jeffhaod5347e02012-03-22 17:25:05 -0700721 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected opcode " << inst->Name();
Ian Rogersd81871c2011-10-03 13:57:23 -0700722 result = false;
723 break;
724 }
725 return result;
726}
727
Ian Rogers776ac1f2012-04-13 23:36:36 -0700728bool MethodVerifier::CheckRegisterIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700729 if (idx >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700730 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register index out of range (" << idx << " >= "
731 << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700732 return false;
733 }
734 return true;
735}
736
Ian Rogers776ac1f2012-04-13 23:36:36 -0700737bool MethodVerifier::CheckWideRegisterIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700738 if (idx + 1 >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700739 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "wide register index out of range (" << idx
740 << "+1 >= " << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700741 return false;
742 }
743 return true;
744}
745
Ian Rogers776ac1f2012-04-13 23:36:36 -0700746bool MethodVerifier::CheckFieldIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700747 if (idx >= dex_file_->GetHeader().field_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700748 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad field index " << idx << " (max "
749 << dex_file_->GetHeader().field_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700750 return false;
751 }
752 return true;
753}
754
Ian Rogers776ac1f2012-04-13 23:36:36 -0700755bool MethodVerifier::CheckMethodIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700756 if (idx >= dex_file_->GetHeader().method_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700757 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad method index " << idx << " (max "
758 << dex_file_->GetHeader().method_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700759 return false;
760 }
761 return true;
762}
763
Ian Rogers776ac1f2012-04-13 23:36:36 -0700764bool MethodVerifier::CheckNewInstance(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700765 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700766 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
767 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700768 return false;
769 }
770 // We don't need the actual class, just a pointer to the class name.
Ian Rogers0571d352011-11-03 19:51:38 -0700771 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -0700772 if (descriptor[0] != 'L') {
jeffhaod5347e02012-03-22 17:25:05 -0700773 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "can't call new-instance on type '" << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -0700774 return false;
775 }
776 return true;
777}
778
Ian Rogers776ac1f2012-04-13 23:36:36 -0700779bool MethodVerifier::CheckStringIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700780 if (idx >= dex_file_->GetHeader().string_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700781 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad string index " << idx << " (max "
782 << dex_file_->GetHeader().string_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700783 return false;
784 }
785 return true;
786}
787
Ian Rogers776ac1f2012-04-13 23:36:36 -0700788bool MethodVerifier::CheckTypeIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700789 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700790 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
791 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700792 return false;
793 }
794 return true;
795}
796
Ian Rogers776ac1f2012-04-13 23:36:36 -0700797bool MethodVerifier::CheckNewArray(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700798 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700799 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
800 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700801 return false;
802 }
803 int bracket_count = 0;
Ian Rogers0571d352011-11-03 19:51:38 -0700804 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -0700805 const char* cp = descriptor;
806 while (*cp++ == '[') {
807 bracket_count++;
808 }
809 if (bracket_count == 0) {
810 /* The given class must be an array type. */
Brian Carlstrom93c33962013-07-26 10:37:43 -0700811 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
812 << "can't new-array class '" << descriptor << "' (not an array)";
Ian Rogersd81871c2011-10-03 13:57:23 -0700813 return false;
814 } else if (bracket_count > 255) {
815 /* It is illegal to create an array of more than 255 dimensions. */
Brian Carlstrom93c33962013-07-26 10:37:43 -0700816 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
817 << "can't new-array class '" << descriptor << "' (exceeds limit)";
Ian Rogersd81871c2011-10-03 13:57:23 -0700818 return false;
819 }
820 return true;
821}
822
Ian Rogers776ac1f2012-04-13 23:36:36 -0700823bool MethodVerifier::CheckArrayData(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700824 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
825 const uint16_t* insns = code_item_->insns_ + cur_offset;
826 const uint16_t* array_data;
827 int32_t array_data_offset;
828
829 DCHECK_LT(cur_offset, insn_count);
830 /* make sure the start of the array data table is in range */
831 array_data_offset = insns[1] | (((int32_t) insns[2]) << 16);
832 if ((int32_t) cur_offset + array_data_offset < 0 ||
833 cur_offset + array_data_offset + 2 >= insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -0700834 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data start: at " << cur_offset
Brian Carlstrom93c33962013-07-26 10:37:43 -0700835 << ", data offset " << array_data_offset
836 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -0700837 return false;
838 }
839 /* offset to array data table is a relative branch-style offset */
840 array_data = insns + array_data_offset;
841 /* make sure the table is 32-bit aligned */
Ian Rogersef7d42f2014-01-06 12:55:46 -0800842 if ((reinterpret_cast<uintptr_t>(array_data) & 0x03) != 0) {
jeffhaod5347e02012-03-22 17:25:05 -0700843 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned array data table: at " << cur_offset
844 << ", data offset " << array_data_offset;
Ian Rogersd81871c2011-10-03 13:57:23 -0700845 return false;
846 }
847 uint32_t value_width = array_data[1];
Elliott Hughes398f64b2012-03-26 18:05:48 -0700848 uint32_t value_count = *reinterpret_cast<const uint32_t*>(&array_data[2]);
Ian Rogersd81871c2011-10-03 13:57:23 -0700849 uint32_t table_size = 4 + (value_width * value_count + 1) / 2;
850 /* make sure the end of the switch is in range */
851 if (cur_offset + array_data_offset + table_size > insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -0700852 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data end: at " << cur_offset
853 << ", data offset " << array_data_offset << ", end "
854 << cur_offset + array_data_offset + table_size
855 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -0700856 return false;
857 }
858 return true;
859}
860
Ian Rogers776ac1f2012-04-13 23:36:36 -0700861bool MethodVerifier::CheckBranchTarget(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700862 int32_t offset;
863 bool isConditional, selfOkay;
864 if (!GetBranchOffset(cur_offset, &offset, &isConditional, &selfOkay)) {
865 return false;
866 }
867 if (!selfOkay && offset == 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700868 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch offset of zero not allowed at"
869 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -0700870 return false;
871 }
Elliott Hughes81ff3182012-03-23 20:35:56 -0700872 // Check for 32-bit overflow. This isn't strictly necessary if we can depend on the runtime
873 // to have identical "wrap-around" behavior, but it's unwise to depend on that.
Ian Rogersd81871c2011-10-03 13:57:23 -0700874 if (((int64_t) cur_offset + (int64_t) offset) != (int64_t) (cur_offset + offset)) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700875 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch target overflow "
876 << reinterpret_cast<void*>(cur_offset) << " +" << offset;
Ian Rogersd81871c2011-10-03 13:57:23 -0700877 return false;
878 }
879 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
880 int32_t abs_offset = cur_offset + offset;
Brian Carlstrom93c33962013-07-26 10:37:43 -0700881 if (abs_offset < 0 ||
882 (uint32_t) abs_offset >= insn_count ||
883 !insn_flags_[abs_offset].IsOpcode()) {
jeffhaod5347e02012-03-22 17:25:05 -0700884 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid branch target " << offset << " (-> "
Elliott Hughes398f64b2012-03-26 18:05:48 -0700885 << reinterpret_cast<void*>(abs_offset) << ") at "
886 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -0700887 return false;
888 }
889 insn_flags_[abs_offset].SetBranchTarget();
890 return true;
891}
892
Ian Rogers776ac1f2012-04-13 23:36:36 -0700893bool MethodVerifier::GetBranchOffset(uint32_t cur_offset, int32_t* pOffset, bool* pConditional,
Ian Rogersd81871c2011-10-03 13:57:23 -0700894 bool* selfOkay) {
895 const uint16_t* insns = code_item_->insns_ + cur_offset;
896 *pConditional = false;
897 *selfOkay = false;
jeffhaoba5ebb92011-08-25 17:24:37 -0700898 switch (*insns & 0xff) {
899 case Instruction::GOTO:
900 *pOffset = ((int16_t) *insns) >> 8;
jeffhaoba5ebb92011-08-25 17:24:37 -0700901 break;
902 case Instruction::GOTO_32:
903 *pOffset = insns[1] | (((uint32_t) insns[2]) << 16);
jeffhaoba5ebb92011-08-25 17:24:37 -0700904 *selfOkay = true;
905 break;
906 case Instruction::GOTO_16:
907 *pOffset = (int16_t) insns[1];
jeffhaoba5ebb92011-08-25 17:24:37 -0700908 break;
909 case Instruction::IF_EQ:
910 case Instruction::IF_NE:
911 case Instruction::IF_LT:
912 case Instruction::IF_GE:
913 case Instruction::IF_GT:
914 case Instruction::IF_LE:
915 case Instruction::IF_EQZ:
916 case Instruction::IF_NEZ:
917 case Instruction::IF_LTZ:
918 case Instruction::IF_GEZ:
919 case Instruction::IF_GTZ:
920 case Instruction::IF_LEZ:
921 *pOffset = (int16_t) insns[1];
922 *pConditional = true;
jeffhaoba5ebb92011-08-25 17:24:37 -0700923 break;
924 default:
925 return false;
926 break;
927 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700928 return true;
929}
930
Ian Rogers776ac1f2012-04-13 23:36:36 -0700931bool MethodVerifier::CheckSwitchTargets(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700932 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700933 DCHECK_LT(cur_offset, insn_count);
Ian Rogersd81871c2011-10-03 13:57:23 -0700934 const uint16_t* insns = code_item_->insns_ + cur_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -0700935 /* make sure the start of the switch is in range */
Ian Rogersd81871c2011-10-03 13:57:23 -0700936 int32_t switch_offset = insns[1] | ((int32_t) insns[2]) << 16;
937 if ((int32_t) cur_offset + switch_offset < 0 || cur_offset + switch_offset + 2 >= insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -0700938 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch start: at " << cur_offset
Brian Carlstrom93c33962013-07-26 10:37:43 -0700939 << ", switch offset " << switch_offset
940 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -0700941 return false;
942 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700943 /* offset to switch table is a relative branch-style offset */
Ian Rogersd81871c2011-10-03 13:57:23 -0700944 const uint16_t* switch_insns = insns + switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -0700945 /* make sure the table is 32-bit aligned */
Ian Rogersef7d42f2014-01-06 12:55:46 -0800946 if ((reinterpret_cast<uintptr_t>(switch_insns) & 0x03) != 0) {
jeffhaod5347e02012-03-22 17:25:05 -0700947 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned switch table: at " << cur_offset
948 << ", switch offset " << switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -0700949 return false;
950 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700951 uint32_t switch_count = switch_insns[1];
952 int32_t keys_offset, targets_offset;
953 uint16_t expected_signature;
jeffhaoba5ebb92011-08-25 17:24:37 -0700954 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
955 /* 0=sig, 1=count, 2/3=firstKey */
956 targets_offset = 4;
957 keys_offset = -1;
958 expected_signature = Instruction::kPackedSwitchSignature;
959 } else {
960 /* 0=sig, 1=count, 2..count*2 = keys */
961 keys_offset = 2;
962 targets_offset = 2 + 2 * switch_count;
963 expected_signature = Instruction::kSparseSwitchSignature;
964 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700965 uint32_t table_size = targets_offset + switch_count * 2;
jeffhaoba5ebb92011-08-25 17:24:37 -0700966 if (switch_insns[0] != expected_signature) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700967 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
968 << StringPrintf("wrong signature for switch table (%x, wanted %x)",
969 switch_insns[0], expected_signature);
jeffhaoba5ebb92011-08-25 17:24:37 -0700970 return false;
971 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700972 /* make sure the end of the switch is in range */
973 if (cur_offset + switch_offset + table_size > (uint32_t) insn_count) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700974 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch end: at " << cur_offset
975 << ", switch offset " << switch_offset
976 << ", end " << (cur_offset + switch_offset + table_size)
jeffhaod5347e02012-03-22 17:25:05 -0700977 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -0700978 return false;
979 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700980 /* for a sparse switch, verify the keys are in ascending order */
981 if (keys_offset > 0 && switch_count > 1) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700982 int32_t last_key = switch_insns[keys_offset] | (switch_insns[keys_offset + 1] << 16);
983 for (uint32_t targ = 1; targ < switch_count; targ++) {
jeffhaoba5ebb92011-08-25 17:24:37 -0700984 int32_t key = (int32_t) switch_insns[keys_offset + targ * 2] |
985 (int32_t) (switch_insns[keys_offset + targ * 2 + 1] << 16);
986 if (key <= last_key) {
jeffhaod5347e02012-03-22 17:25:05 -0700987 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid packed switch: last key=" << last_key
988 << ", this=" << key;
jeffhaoba5ebb92011-08-25 17:24:37 -0700989 return false;
990 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700991 last_key = key;
992 }
993 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700994 /* verify each switch target */
Ian Rogersd81871c2011-10-03 13:57:23 -0700995 for (uint32_t targ = 0; targ < switch_count; targ++) {
996 int32_t offset = (int32_t) switch_insns[targets_offset + targ * 2] |
997 (int32_t) (switch_insns[targets_offset + targ * 2 + 1] << 16);
998 int32_t abs_offset = cur_offset + offset;
Brian Carlstrom93c33962013-07-26 10:37:43 -0700999 if (abs_offset < 0 ||
1000 abs_offset >= (int32_t) insn_count ||
1001 !insn_flags_[abs_offset].IsOpcode()) {
1002 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch target " << offset
1003 << " (-> " << reinterpret_cast<void*>(abs_offset) << ") at "
1004 << reinterpret_cast<void*>(cur_offset)
1005 << "[" << targ << "]";
jeffhaoba5ebb92011-08-25 17:24:37 -07001006 return false;
1007 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001008 insn_flags_[abs_offset].SetBranchTarget();
1009 }
1010 return true;
1011}
1012
Ian Rogers776ac1f2012-04-13 23:36:36 -07001013bool MethodVerifier::CheckVarArgRegs(uint32_t vA, uint32_t arg[]) {
Ian Rogers29a26482014-05-02 15:27:29 -07001014 if (vA > Instruction::kMaxVarArgRegs) {
jeffhaod5347e02012-03-22 17:25:05 -07001015 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << vA << ") in non-range invoke)";
Ian Rogersd81871c2011-10-03 13:57:23 -07001016 return false;
1017 }
1018 uint16_t registers_size = code_item_->registers_size_;
1019 for (uint32_t idx = 0; idx < vA; idx++) {
jeffhao457cc512012-02-02 16:55:13 -08001020 if (arg[idx] >= registers_size) {
jeffhaod5347e02012-03-22 17:25:05 -07001021 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index (" << arg[idx]
1022 << ") in non-range invoke (>= " << registers_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001023 return false;
1024 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001025 }
1026
1027 return true;
1028}
1029
Ian Rogers776ac1f2012-04-13 23:36:36 -07001030bool MethodVerifier::CheckVarArgRangeRegs(uint32_t vA, uint32_t vC) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001031 uint16_t registers_size = code_item_->registers_size_;
1032 // vA/vC are unsigned 8-bit/16-bit quantities for /range instructions, so there's no risk of
1033 // integer overflow when adding them here.
1034 if (vA + vC > registers_size) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001035 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index " << vA << "+" << vC
1036 << " in range invoke (> " << registers_size << ")";
jeffhaoba5ebb92011-08-25 17:24:37 -07001037 return false;
1038 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001039 return true;
1040}
1041
Ian Rogers776ac1f2012-04-13 23:36:36 -07001042bool MethodVerifier::VerifyCodeFlow() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001043 uint16_t registers_size = code_item_->registers_size_;
1044 uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaobdb76512011-09-07 11:43:16 -07001045
Ian Rogersd81871c2011-10-03 13:57:23 -07001046 if (registers_size * insns_size > 4*1024*1024) {
buzbee4922ef92012-02-24 14:32:20 -08001047 LOG(WARNING) << "warning: method is huge (regs=" << registers_size
1048 << " insns_size=" << insns_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001049 }
1050 /* Create and initialize table holding register status */
Brian Carlstrom93c33962013-07-26 10:37:43 -07001051 reg_table_.Init(kTrackCompilerInterestPoints,
1052 insn_flags_.get(),
1053 insns_size,
1054 registers_size,
1055 this);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07001056
jeffhaobdb76512011-09-07 11:43:16 -07001057
Ian Rogersd0fbd852013-09-24 18:17:04 -07001058 work_line_.reset(RegisterLine::Create(registers_size, this));
1059 saved_line_.reset(RegisterLine::Create(registers_size, this));
jeffhaobdb76512011-09-07 11:43:16 -07001060
Ian Rogersd81871c2011-10-03 13:57:23 -07001061 /* Initialize register types of method arguments. */
1062 if (!SetTypesFromSignature()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001063 DCHECK_NE(failures_.size(), 0U);
1064 std::string prepend("Bad signature in ");
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001065 prepend += PrettyMethod(dex_method_idx_, *dex_file_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001066 PrependToLastFailMessage(prepend);
Ian Rogersd81871c2011-10-03 13:57:23 -07001067 return false;
1068 }
1069 /* Perform code flow verification. */
1070 if (!CodeFlowVerifyMethod()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001071 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -07001072 return false;
jeffhaobdb76512011-09-07 11:43:16 -07001073 }
jeffhaobdb76512011-09-07 11:43:16 -07001074 return true;
1075}
1076
Ian Rogersad0b3a32012-04-16 14:50:24 -07001077std::ostream& MethodVerifier::DumpFailures(std::ostream& os) {
1078 DCHECK_EQ(failures_.size(), failure_messages_.size());
Jeff Hao4137f482013-11-22 11:44:57 -08001079 for (size_t i = 0; i < failures_.size(); ++i) {
1080 os << failure_messages_[i]->str() << "\n";
Ian Rogersad0b3a32012-04-16 14:50:24 -07001081 }
1082 return os;
1083}
1084
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001085extern "C" void MethodVerifierGdbDump(MethodVerifier* v)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001086 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001087 v->Dump(std::cerr);
1088}
1089
Ian Rogers776ac1f2012-04-13 23:36:36 -07001090void MethodVerifier::Dump(std::ostream& os) {
jeffhaof56197c2012-03-05 18:01:54 -08001091 if (code_item_ == NULL) {
Elliott Hughesc073b072012-05-24 19:29:17 -07001092 os << "Native method\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001093 return;
jeffhaobdb76512011-09-07 11:43:16 -07001094 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001095 {
1096 os << "Register Types:\n";
1097 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
1098 std::ostream indent_os(&indent_filter);
1099 reg_types_.Dump(indent_os);
1100 }
Ian Rogersb4903572012-10-11 11:52:56 -07001101 os << "Dumping instructions and register lines:\n";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001102 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
1103 std::ostream indent_os(&indent_filter);
Ian Rogersd81871c2011-10-03 13:57:23 -07001104 const Instruction* inst = Instruction::At(code_item_->insns_);
1105 for (size_t dex_pc = 0; dex_pc < code_item_->insns_size_in_code_units_;
1106 dex_pc += insn_flags_[dex_pc].GetLengthInCodeUnits()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001107 RegisterLine* reg_line = reg_table_.GetLine(dex_pc);
1108 if (reg_line != NULL) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001109 indent_os << reg_line->Dump() << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07001110 }
Ian Rogers7b3ddd22013-02-21 15:19:52 -08001111 indent_os << StringPrintf("0x%04zx", dex_pc) << ": " << insn_flags_[dex_pc].ToString() << " ";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001112 const bool kDumpHexOfInstruction = false;
1113 if (kDumpHexOfInstruction) {
1114 indent_os << inst->DumpHex(5) << " ";
1115 }
1116 indent_os << inst->DumpString(dex_file_) << "\n";
jeffhaoba5ebb92011-08-25 17:24:37 -07001117 inst = inst->Next();
1118 }
jeffhaobdb76512011-09-07 11:43:16 -07001119}
1120
Ian Rogersd81871c2011-10-03 13:57:23 -07001121static bool IsPrimitiveDescriptor(char descriptor) {
1122 switch (descriptor) {
jeffhaobdb76512011-09-07 11:43:16 -07001123 case 'I':
1124 case 'C':
1125 case 'S':
1126 case 'B':
1127 case 'Z':
jeffhaobdb76512011-09-07 11:43:16 -07001128 case 'F':
1129 case 'D':
1130 case 'J':
Ian Rogersd81871c2011-10-03 13:57:23 -07001131 return true;
jeffhaobdb76512011-09-07 11:43:16 -07001132 default:
1133 return false;
1134 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001135}
1136
Ian Rogers776ac1f2012-04-13 23:36:36 -07001137bool MethodVerifier::SetTypesFromSignature() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001138 RegisterLine* reg_line = reg_table_.GetLine(0);
1139 int arg_start = code_item_->registers_size_ - code_item_->ins_size_;
1140 size_t expected_args = code_item_->ins_size_; /* long/double count as two */
jeffhaobdb76512011-09-07 11:43:16 -07001141
Ian Rogersd81871c2011-10-03 13:57:23 -07001142 DCHECK_GE(arg_start, 0); /* should have been verified earlier */
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001143 // Include the "this" pointer.
Ian Rogersd81871c2011-10-03 13:57:23 -07001144 size_t cur_arg = 0;
Ian Rogersad0b3a32012-04-16 14:50:24 -07001145 if (!IsStatic()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001146 // If this is a constructor for a class other than java.lang.Object, mark the first ("this")
1147 // argument as uninitialized. This restricts field access until the superclass constructor is
1148 // called.
Ian Rogersad0b3a32012-04-16 14:50:24 -07001149 const RegType& declaring_class = GetDeclaringClass();
1150 if (IsConstructor() && !declaring_class.IsJavaLangObject()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001151 reg_line->SetRegisterType(arg_start + cur_arg,
1152 reg_types_.UninitializedThisArgument(declaring_class));
1153 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001154 reg_line->SetRegisterType(arg_start + cur_arg, declaring_class);
jeffhaobdb76512011-09-07 11:43:16 -07001155 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001156 cur_arg++;
jeffhaobdb76512011-09-07 11:43:16 -07001157 }
1158
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001159 const DexFile::ProtoId& proto_id =
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001160 dex_file_->GetMethodPrototype(dex_file_->GetMethodId(dex_method_idx_));
Ian Rogers0571d352011-11-03 19:51:38 -07001161 DexFileParameterIterator iterator(*dex_file_, proto_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07001162
1163 for (; iterator.HasNext(); iterator.Next()) {
1164 const char* descriptor = iterator.GetDescriptor();
1165 if (descriptor == NULL) {
1166 LOG(FATAL) << "Null descriptor";
1167 }
1168 if (cur_arg >= expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07001169 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1170 << " args, found more (" << descriptor << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001171 return false;
1172 }
1173 switch (descriptor[0]) {
1174 case 'L':
1175 case '[':
1176 // We assume that reference arguments are initialized. The only way it could be otherwise
1177 // (assuming the caller was verified) is if the current method is <init>, but in that case
1178 // it's effectively considered initialized the instant we reach here (in the sense that we
1179 // can return without doing anything or call virtual methods).
1180 {
Sebastien Hertz2ed76f92014-04-22 17:11:08 +02001181 const RegType& reg_type = ResolveClassAndCheckAccess(iterator.GetTypeIdx());
1182 if (!reg_type.IsNonZeroReferenceTypes()) {
1183 DCHECK(HasFailures());
1184 return false;
1185 }
Ian Rogers84fa0742011-10-25 18:13:30 -07001186 reg_line->SetRegisterType(arg_start + cur_arg, reg_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001187 }
1188 break;
1189 case 'Z':
1190 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Boolean());
1191 break;
1192 case 'C':
1193 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Char());
1194 break;
1195 case 'B':
1196 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Byte());
1197 break;
1198 case 'I':
1199 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Integer());
1200 break;
1201 case 'S':
1202 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Short());
1203 break;
1204 case 'F':
1205 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Float());
1206 break;
1207 case 'J':
1208 case 'D': {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001209 const RegType& lo_half = descriptor[0] == 'J' ? reg_types_.LongLo() : reg_types_.DoubleLo();
1210 const RegType& hi_half = descriptor[0] == 'J' ? reg_types_.LongHi() : reg_types_.DoubleHi();
1211 reg_line->SetRegisterTypeWide(arg_start + cur_arg, lo_half, hi_half);
Ian Rogersd81871c2011-10-03 13:57:23 -07001212 cur_arg++;
1213 break;
1214 }
1215 default:
Brian Carlstrom93c33962013-07-26 10:37:43 -07001216 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected signature type char '"
1217 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001218 return false;
1219 }
1220 cur_arg++;
1221 }
1222 if (cur_arg != expected_args) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001223 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1224 << " arguments, found " << cur_arg;
Ian Rogersd81871c2011-10-03 13:57:23 -07001225 return false;
1226 }
1227 const char* descriptor = dex_file_->GetReturnTypeDescriptor(proto_id);
1228 // Validate return type. We don't do the type lookup; just want to make sure that it has the right
1229 // format. Only major difference from the method argument format is that 'V' is supported.
1230 bool result;
1231 if (IsPrimitiveDescriptor(descriptor[0]) || descriptor[0] == 'V') {
1232 result = descriptor[1] == '\0';
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001233 } else if (descriptor[0] == '[') { // single/multi-dimensional array of object/primitive
Ian Rogersd81871c2011-10-03 13:57:23 -07001234 size_t i = 0;
1235 do {
1236 i++;
1237 } while (descriptor[i] == '['); // process leading [
1238 if (descriptor[i] == 'L') { // object array
1239 do {
1240 i++; // find closing ;
1241 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1242 result = descriptor[i] == ';';
1243 } else { // primitive array
1244 result = IsPrimitiveDescriptor(descriptor[i]) && descriptor[i + 1] == '\0';
1245 }
1246 } else if (descriptor[0] == 'L') {
1247 // could be more thorough here, but shouldn't be required
1248 size_t i = 0;
1249 do {
1250 i++;
1251 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1252 result = descriptor[i] == ';';
1253 } else {
1254 result = false;
1255 }
1256 if (!result) {
jeffhaod5347e02012-03-22 17:25:05 -07001257 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected char in return type descriptor '"
1258 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001259 }
1260 return result;
jeffhaobdb76512011-09-07 11:43:16 -07001261}
1262
Ian Rogers776ac1f2012-04-13 23:36:36 -07001263bool MethodVerifier::CodeFlowVerifyMethod() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001264 const uint16_t* insns = code_item_->insns_;
1265 const uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaoba5ebb92011-08-25 17:24:37 -07001266
jeffhaobdb76512011-09-07 11:43:16 -07001267 /* Begin by marking the first instruction as "changed". */
Ian Rogersd81871c2011-10-03 13:57:23 -07001268 insn_flags_[0].SetChanged();
1269 uint32_t start_guess = 0;
jeffhaoba5ebb92011-08-25 17:24:37 -07001270
jeffhaobdb76512011-09-07 11:43:16 -07001271 /* Continue until no instructions are marked "changed". */
1272 while (true) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001273 // Find the first marked one. Use "start_guess" as a way to find one quickly.
1274 uint32_t insn_idx = start_guess;
1275 for (; insn_idx < insns_size; insn_idx++) {
1276 if (insn_flags_[insn_idx].IsChanged())
jeffhaobdb76512011-09-07 11:43:16 -07001277 break;
1278 }
jeffhaobdb76512011-09-07 11:43:16 -07001279 if (insn_idx == insns_size) {
1280 if (start_guess != 0) {
1281 /* try again, starting from the top */
1282 start_guess = 0;
1283 continue;
1284 } else {
1285 /* all flags are clear */
1286 break;
1287 }
1288 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001289 // We carry the working set of registers from instruction to instruction. If this address can
1290 // be the target of a branch (or throw) instruction, or if we're skipping around chasing
1291 // "changed" flags, we need to load the set of registers from the table.
1292 // Because we always prefer to continue on to the next instruction, we should never have a
1293 // situation where we have a stray "changed" flag set on an instruction that isn't a branch
1294 // target.
1295 work_insn_idx_ = insn_idx;
1296 if (insn_flags_[insn_idx].IsBranchTarget()) {
1297 work_line_->CopyFromLine(reg_table_.GetLine(insn_idx));
jeffhaobdb76512011-09-07 11:43:16 -07001298 } else {
1299#ifndef NDEBUG
1300 /*
1301 * Sanity check: retrieve the stored register line (assuming
1302 * a full table) and make sure it actually matches.
1303 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001304 RegisterLine* register_line = reg_table_.GetLine(insn_idx);
1305 if (register_line != NULL) {
1306 if (work_line_->CompareLine(register_line) != 0) {
1307 Dump(std::cout);
1308 std::cout << info_messages_.str();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001309 LOG(FATAL) << "work_line diverged in " << PrettyMethod(dex_method_idx_, *dex_file_)
Elliott Hughesc073b072012-05-24 19:29:17 -07001310 << "@" << reinterpret_cast<void*>(work_insn_idx_) << "\n"
1311 << " work_line=" << *work_line_ << "\n"
Elliott Hughes398f64b2012-03-26 18:05:48 -07001312 << " expected=" << *register_line;
Ian Rogersd81871c2011-10-03 13:57:23 -07001313 }
jeffhaobdb76512011-09-07 11:43:16 -07001314 }
1315#endif
1316 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001317 if (!CodeFlowVerifyInstruction(&start_guess)) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001318 std::string prepend(PrettyMethod(dex_method_idx_, *dex_file_));
Ian Rogersad0b3a32012-04-16 14:50:24 -07001319 prepend += " failed to verify: ";
1320 PrependToLastFailMessage(prepend);
jeffhaoba5ebb92011-08-25 17:24:37 -07001321 return false;
1322 }
jeffhaobdb76512011-09-07 11:43:16 -07001323 /* Clear "changed" and mark as visited. */
Ian Rogersd81871c2011-10-03 13:57:23 -07001324 insn_flags_[insn_idx].SetVisited();
1325 insn_flags_[insn_idx].ClearChanged();
jeffhaobdb76512011-09-07 11:43:16 -07001326 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001327
Ian Rogers1c849e52012-06-28 14:00:33 -07001328 if (gDebugVerify) {
jeffhaobdb76512011-09-07 11:43:16 -07001329 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001330 * Scan for dead code. There's nothing "evil" about dead code
jeffhaobdb76512011-09-07 11:43:16 -07001331 * (besides the wasted space), but it indicates a flaw somewhere
1332 * down the line, possibly in the verifier.
1333 *
1334 * If we've substituted "always throw" instructions into the stream,
1335 * we are almost certainly going to have some dead code.
1336 */
1337 int dead_start = -1;
Ian Rogersd81871c2011-10-03 13:57:23 -07001338 uint32_t insn_idx = 0;
1339 for (; insn_idx < insns_size; insn_idx += insn_flags_[insn_idx].GetLengthInCodeUnits()) {
jeffhaobdb76512011-09-07 11:43:16 -07001340 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001341 * Switch-statement data doesn't get "visited" by scanner. It
jeffhaobdb76512011-09-07 11:43:16 -07001342 * may or may not be preceded by a padding NOP (for alignment).
1343 */
1344 if (insns[insn_idx] == Instruction::kPackedSwitchSignature ||
1345 insns[insn_idx] == Instruction::kSparseSwitchSignature ||
1346 insns[insn_idx] == Instruction::kArrayDataSignature ||
Elliott Hughes380aaa72012-07-09 14:33:15 -07001347 (insns[insn_idx] == Instruction::NOP && (insn_idx + 1 < insns_size) &&
jeffhaobdb76512011-09-07 11:43:16 -07001348 (insns[insn_idx + 1] == Instruction::kPackedSwitchSignature ||
1349 insns[insn_idx + 1] == Instruction::kSparseSwitchSignature ||
1350 insns[insn_idx + 1] == Instruction::kArrayDataSignature))) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001351 insn_flags_[insn_idx].SetVisited();
jeffhaobdb76512011-09-07 11:43:16 -07001352 }
1353
Ian Rogersd81871c2011-10-03 13:57:23 -07001354 if (!insn_flags_[insn_idx].IsVisited()) {
jeffhaobdb76512011-09-07 11:43:16 -07001355 if (dead_start < 0)
1356 dead_start = insn_idx;
1357 } else if (dead_start >= 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001358 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start)
1359 << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaobdb76512011-09-07 11:43:16 -07001360 dead_start = -1;
1361 }
1362 }
1363 if (dead_start >= 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001364 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start)
1365 << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaoba5ebb92011-08-25 17:24:37 -07001366 }
Ian Rogersc9e463c2013-06-05 16:52:26 -07001367 // To dump the state of the verify after a method, do something like:
1368 // if (PrettyMethod(dex_method_idx_, *dex_file_) ==
1369 // "boolean java.lang.String.equals(java.lang.Object)") {
1370 // LOG(INFO) << info_messages_.str();
1371 // }
jeffhaoba5ebb92011-08-25 17:24:37 -07001372 }
jeffhaobdb76512011-09-07 11:43:16 -07001373 return true;
1374}
1375
Ian Rogers776ac1f2012-04-13 23:36:36 -07001376bool MethodVerifier::CodeFlowVerifyInstruction(uint32_t* start_guess) {
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001377 // If we're doing FindLocksAtDexPc, check whether we're at the dex pc we care about.
1378 // We want the state _before_ the instruction, for the case where the dex pc we're
1379 // interested in is itself a monitor-enter instruction (which is a likely place
1380 // for a thread to be suspended).
1381 if (monitor_enter_dex_pcs_ != NULL && work_insn_idx_ == interesting_dex_pc_) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001382 monitor_enter_dex_pcs_->clear(); // The new work line is more accurate than the previous one.
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001383 for (size_t i = 0; i < work_line_->GetMonitorEnterCount(); ++i) {
1384 monitor_enter_dex_pcs_->push_back(work_line_->GetMonitorEnterDexPc(i));
1385 }
1386 }
1387
jeffhaobdb76512011-09-07 11:43:16 -07001388 /*
1389 * Once we finish decoding the instruction, we need to figure out where
jeffhaod1f0fde2011-09-08 17:25:33 -07001390 * we can go from here. There are three possible ways to transfer
jeffhaobdb76512011-09-07 11:43:16 -07001391 * control to another statement:
1392 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001393 * (1) Continue to the next instruction. Applies to all but
jeffhaobdb76512011-09-07 11:43:16 -07001394 * unconditional branches, method returns, and exception throws.
jeffhaod1f0fde2011-09-08 17:25:33 -07001395 * (2) Branch to one or more possible locations. Applies to branches
jeffhaobdb76512011-09-07 11:43:16 -07001396 * and switch statements.
jeffhaod1f0fde2011-09-08 17:25:33 -07001397 * (3) Exception handlers. Applies to any instruction that can
jeffhaobdb76512011-09-07 11:43:16 -07001398 * throw an exception that is handled by an encompassing "try"
1399 * block.
1400 *
1401 * We can also return, in which case there is no successor instruction
1402 * from this point.
1403 *
Elliott Hughesadb8c672012-03-06 16:49:32 -08001404 * The behavior can be determined from the opcode flags.
jeffhaobdb76512011-09-07 11:43:16 -07001405 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001406 const uint16_t* insns = code_item_->insns_ + work_insn_idx_;
1407 const Instruction* inst = Instruction::At(insns);
Ian Rogersa75a0132012-09-28 11:41:42 -07001408 int opcode_flags = Instruction::FlagsOf(inst->Opcode());
jeffhaobdb76512011-09-07 11:43:16 -07001409
jeffhaobdb76512011-09-07 11:43:16 -07001410 int32_t branch_target = 0;
jeffhaobdb76512011-09-07 11:43:16 -07001411 bool just_set_result = false;
Ian Rogers2c8a8572011-10-24 17:11:36 -07001412 if (gDebugVerify) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001413 // Generate processing back trace to debug verifier
Elliott Hughesc073b072012-05-24 19:29:17 -07001414 LogVerifyInfo() << "Processing " << inst->DumpString(dex_file_) << "\n"
1415 << *work_line_.get() << "\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001416 }
jeffhaobdb76512011-09-07 11:43:16 -07001417
1418 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001419 * Make a copy of the previous register state. If the instruction
jeffhaobdb76512011-09-07 11:43:16 -07001420 * can throw an exception, we will copy/merge this into the "catch"
1421 * address rather than work_line, because we don't want the result
1422 * from the "successful" code path (e.g. a check-cast that "improves"
1423 * a type) to be visible to the exception handler.
1424 */
Ian Rogers776ac1f2012-04-13 23:36:36 -07001425 if ((opcode_flags & Instruction::kThrow) != 0 && CurrentInsnFlags()->IsInTry()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001426 saved_line_->CopyFromLine(work_line_.get());
jeffhaobdb76512011-09-07 11:43:16 -07001427 } else {
1428#ifndef NDEBUG
Ian Rogersd81871c2011-10-03 13:57:23 -07001429 saved_line_->FillWithGarbage();
jeffhaobdb76512011-09-07 11:43:16 -07001430#endif
1431 }
1432
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07001433
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001434 // We need to ensure the work line is consistent while performing validation. When we spot a
1435 // peephole pattern we compute a new line for either the fallthrough instruction or the
1436 // branch target.
1437 UniquePtr<RegisterLine> branch_line;
1438 UniquePtr<RegisterLine> fallthrough_line;
1439
Sebastien Hertz849600b2013-12-20 10:28:08 +01001440 // We need precise constant types only for deoptimization which happens at runtime.
1441 const bool need_precise_constant = !Runtime::Current()->IsCompiler();
1442
Sebastien Hertz5243e912013-05-21 10:55:07 +02001443 switch (inst->Opcode()) {
jeffhaobdb76512011-09-07 11:43:16 -07001444 case Instruction::NOP:
1445 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001446 * A "pure" NOP has no effect on anything. Data tables start with
jeffhaobdb76512011-09-07 11:43:16 -07001447 * a signature that looks like a NOP; if we see one of these in
1448 * the course of executing code then we have a problem.
1449 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001450 if (inst->VRegA_10x() != 0) {
jeffhaod5347e02012-03-22 17:25:05 -07001451 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "encountered data table in instruction stream";
jeffhaobdb76512011-09-07 11:43:16 -07001452 }
1453 break;
1454
1455 case Instruction::MOVE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001456 work_line_->CopyRegister1(inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategory1nr);
1457 break;
jeffhaobdb76512011-09-07 11:43:16 -07001458 case Instruction::MOVE_FROM16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001459 work_line_->CopyRegister1(inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategory1nr);
1460 break;
jeffhaobdb76512011-09-07 11:43:16 -07001461 case Instruction::MOVE_16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001462 work_line_->CopyRegister1(inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategory1nr);
jeffhaobdb76512011-09-07 11:43:16 -07001463 break;
1464 case Instruction::MOVE_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001465 work_line_->CopyRegister2(inst->VRegA_12x(), inst->VRegB_12x());
1466 break;
jeffhaobdb76512011-09-07 11:43:16 -07001467 case Instruction::MOVE_WIDE_FROM16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001468 work_line_->CopyRegister2(inst->VRegA_22x(), inst->VRegB_22x());
1469 break;
jeffhaobdb76512011-09-07 11:43:16 -07001470 case Instruction::MOVE_WIDE_16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001471 work_line_->CopyRegister2(inst->VRegA_32x(), inst->VRegB_32x());
jeffhaobdb76512011-09-07 11:43:16 -07001472 break;
1473 case Instruction::MOVE_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001474 work_line_->CopyRegister1(inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategoryRef);
1475 break;
jeffhaobdb76512011-09-07 11:43:16 -07001476 case Instruction::MOVE_OBJECT_FROM16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001477 work_line_->CopyRegister1(inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategoryRef);
1478 break;
jeffhaobdb76512011-09-07 11:43:16 -07001479 case Instruction::MOVE_OBJECT_16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001480 work_line_->CopyRegister1(inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategoryRef);
jeffhaobdb76512011-09-07 11:43:16 -07001481 break;
1482
1483 /*
1484 * The move-result instructions copy data out of a "pseudo-register"
jeffhaod1f0fde2011-09-08 17:25:33 -07001485 * with the results from the last method invocation. In practice we
jeffhaobdb76512011-09-07 11:43:16 -07001486 * might want to hold the result in an actual CPU register, so the
1487 * Dalvik spec requires that these only appear immediately after an
1488 * invoke or filled-new-array.
1489 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001490 * These calls invalidate the "result" register. (This is now
jeffhaobdb76512011-09-07 11:43:16 -07001491 * redundant with the reset done below, but it can make the debug info
1492 * easier to read in some cases.)
1493 */
1494 case Instruction::MOVE_RESULT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001495 work_line_->CopyResultRegister1(inst->VRegA_11x(), false);
jeffhaobdb76512011-09-07 11:43:16 -07001496 break;
1497 case Instruction::MOVE_RESULT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001498 work_line_->CopyResultRegister2(inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07001499 break;
1500 case Instruction::MOVE_RESULT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001501 work_line_->CopyResultRegister1(inst->VRegA_11x(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001502 break;
1503
Ian Rogersd81871c2011-10-03 13:57:23 -07001504 case Instruction::MOVE_EXCEPTION: {
jeffhaobdb76512011-09-07 11:43:16 -07001505 /*
jeffhao60f83e32012-02-13 17:16:30 -08001506 * This statement can only appear as the first instruction in an exception handler. We verify
1507 * that as part of extracting the exception type from the catch block list.
jeffhaobdb76512011-09-07 11:43:16 -07001508 */
Ian Rogers28ad40d2011-10-27 15:19:26 -07001509 const RegType& res_type = GetCaughtExceptionType();
Sebastien Hertz5243e912013-05-21 10:55:07 +02001510 work_line_->SetRegisterType(inst->VRegA_11x(), res_type);
jeffhaobdb76512011-09-07 11:43:16 -07001511 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001512 }
jeffhaobdb76512011-09-07 11:43:16 -07001513 case Instruction::RETURN_VOID:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001514 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
1515 if (!GetMethodReturnType().IsConflict()) {
jeffhaod5347e02012-03-22 17:25:05 -07001516 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001517 }
jeffhaobdb76512011-09-07 11:43:16 -07001518 }
1519 break;
1520 case Instruction::RETURN:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001521 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
jeffhaobdb76512011-09-07 11:43:16 -07001522 /* check the method signature */
Ian Rogersd81871c2011-10-03 13:57:23 -07001523 const RegType& return_type = GetMethodReturnType();
1524 if (!return_type.IsCategory1Types()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001525 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected non-category 1 return type "
1526 << return_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001527 } else {
1528 // Compilers may generate synthetic functions that write byte values into boolean fields.
1529 // Also, it may use integer values for boolean, byte, short, and character return types.
Sebastien Hertz5243e912013-05-21 10:55:07 +02001530 const uint32_t vregA = inst->VRegA_11x();
1531 const RegType& src_type = work_line_->GetRegisterType(vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07001532 bool use_src = ((return_type.IsBoolean() && src_type.IsByte()) ||
1533 ((return_type.IsBoolean() || return_type.IsByte() ||
1534 return_type.IsShort() || return_type.IsChar()) &&
1535 src_type.IsInteger()));
1536 /* check the register contents */
Ian Rogersad0b3a32012-04-16 14:50:24 -07001537 bool success =
Sebastien Hertz5243e912013-05-21 10:55:07 +02001538 work_line_->VerifyRegisterType(vregA, use_src ? src_type : return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001539 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001540 AppendToLastFailMessage(StringPrintf(" return-1nr on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07001541 }
jeffhaobdb76512011-09-07 11:43:16 -07001542 }
1543 }
1544 break;
1545 case Instruction::RETURN_WIDE:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001546 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
jeffhaobdb76512011-09-07 11:43:16 -07001547 /* check the method signature */
Ian Rogersd81871c2011-10-03 13:57:23 -07001548 const RegType& return_type = GetMethodReturnType();
1549 if (!return_type.IsCategory2Types()) {
jeffhaod5347e02012-03-22 17:25:05 -07001550 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-wide not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001551 } else {
1552 /* check the register contents */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001553 const uint32_t vregA = inst->VRegA_11x();
1554 bool success = work_line_->VerifyRegisterType(vregA, return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001555 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001556 AppendToLastFailMessage(StringPrintf(" return-wide on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07001557 }
jeffhaobdb76512011-09-07 11:43:16 -07001558 }
1559 }
1560 break;
1561 case Instruction::RETURN_OBJECT:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001562 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001563 const RegType& return_type = GetMethodReturnType();
1564 if (!return_type.IsReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001565 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-object not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001566 } else {
1567 /* return_type is the *expected* return type, not register value */
1568 DCHECK(!return_type.IsZero());
1569 DCHECK(!return_type.IsUninitializedReference());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001570 const uint32_t vregA = inst->VRegA_11x();
1571 const RegType& reg_type = work_line_->GetRegisterType(vregA);
Ian Rogers9074b992011-10-26 17:41:55 -07001572 // Disallow returning uninitialized values and verify that the reference in vAA is an
1573 // instance of the "return_type"
1574 if (reg_type.IsUninitializedTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001575 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "returning uninitialized object '"
1576 << reg_type << "'";
Ian Rogers9074b992011-10-26 17:41:55 -07001577 } else if (!return_type.IsAssignableFrom(reg_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07001578 if (reg_type.IsUnresolvedTypes() || return_type.IsUnresolvedTypes()) {
1579 Fail(VERIFY_ERROR_NO_CLASS) << " can't resolve returned type '" << return_type
1580 << "' or '" << reg_type << "'";
1581 } else {
1582 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning '" << reg_type
1583 << "', but expected from declaration '" << return_type << "'";
1584 }
jeffhaobdb76512011-09-07 11:43:16 -07001585 }
1586 }
1587 }
1588 break;
1589
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001590 /* could be boolean, int, float, or a null reference */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001591 case Instruction::CONST_4: {
1592 int32_t val = static_cast<int32_t>(inst->VRegB_11n() << 28) >> 28;
Sebastien Hertz849600b2013-12-20 10:28:08 +01001593 work_line_->SetRegisterType(inst->VRegA_11n(),
1594 DetermineCat1Constant(val, need_precise_constant));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001595 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02001596 }
1597 case Instruction::CONST_16: {
1598 int16_t val = static_cast<int16_t>(inst->VRegB_21s());
Sebastien Hertz849600b2013-12-20 10:28:08 +01001599 work_line_->SetRegisterType(inst->VRegA_21s(),
1600 DetermineCat1Constant(val, need_precise_constant));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001601 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02001602 }
Sebastien Hertz849600b2013-12-20 10:28:08 +01001603 case Instruction::CONST: {
1604 int32_t val = inst->VRegB_31i();
Sebastien Hertz5243e912013-05-21 10:55:07 +02001605 work_line_->SetRegisterType(inst->VRegA_31i(),
Sebastien Hertz849600b2013-12-20 10:28:08 +01001606 DetermineCat1Constant(val, need_precise_constant));
jeffhaobdb76512011-09-07 11:43:16 -07001607 break;
Sebastien Hertz849600b2013-12-20 10:28:08 +01001608 }
1609 case Instruction::CONST_HIGH16: {
1610 int32_t val = static_cast<int32_t>(inst->VRegB_21h() << 16);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001611 work_line_->SetRegisterType(inst->VRegA_21h(),
Sebastien Hertz849600b2013-12-20 10:28:08 +01001612 DetermineCat1Constant(val, need_precise_constant));
jeffhaobdb76512011-09-07 11:43:16 -07001613 break;
Sebastien Hertz849600b2013-12-20 10:28:08 +01001614 }
jeffhaobdb76512011-09-07 11:43:16 -07001615 /* could be long or double; resolved upon use */
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001616 case Instruction::CONST_WIDE_16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001617 int64_t val = static_cast<int16_t>(inst->VRegB_21s());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001618 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1619 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001620 work_line_->SetRegisterTypeWide(inst->VRegA_21s(), lo, hi);
jeffhaobdb76512011-09-07 11:43:16 -07001621 break;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001622 }
1623 case Instruction::CONST_WIDE_32: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001624 int64_t val = static_cast<int32_t>(inst->VRegB_31i());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001625 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1626 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001627 work_line_->SetRegisterTypeWide(inst->VRegA_31i(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001628 break;
1629 }
1630 case Instruction::CONST_WIDE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001631 int64_t val = inst->VRegB_51l();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001632 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1633 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001634 work_line_->SetRegisterTypeWide(inst->VRegA_51l(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001635 break;
1636 }
1637 case Instruction::CONST_WIDE_HIGH16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001638 int64_t val = static_cast<uint64_t>(inst->VRegB_21h()) << 48;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001639 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1640 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001641 work_line_->SetRegisterTypeWide(inst->VRegA_21h(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001642 break;
1643 }
jeffhaobdb76512011-09-07 11:43:16 -07001644 case Instruction::CONST_STRING:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001645 work_line_->SetRegisterType(inst->VRegA_21c(), reg_types_.JavaLangString());
1646 break;
jeffhaobdb76512011-09-07 11:43:16 -07001647 case Instruction::CONST_STRING_JUMBO:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001648 work_line_->SetRegisterType(inst->VRegA_31c(), reg_types_.JavaLangString());
jeffhaobdb76512011-09-07 11:43:16 -07001649 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001650 case Instruction::CONST_CLASS: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001651 // Get type from instruction if unresolved then we need an access check
1652 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Sebastien Hertz5243e912013-05-21 10:55:07 +02001653 const RegType& res_type = ResolveClassAndCheckAccess(inst->VRegB_21c());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001654 // Register holds class, ie its type is class, on error it will hold Conflict.
Sebastien Hertz5243e912013-05-21 10:55:07 +02001655 work_line_->SetRegisterType(inst->VRegA_21c(),
Ian Rogersb4903572012-10-11 11:52:56 -07001656 res_type.IsConflict() ? res_type
1657 : reg_types_.JavaLangClass(true));
jeffhaobdb76512011-09-07 11:43:16 -07001658 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001659 }
jeffhaobdb76512011-09-07 11:43:16 -07001660 case Instruction::MONITOR_ENTER:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001661 work_line_->PushMonitor(inst->VRegA_11x(), work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07001662 break;
1663 case Instruction::MONITOR_EXIT:
1664 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001665 * monitor-exit instructions are odd. They can throw exceptions,
jeffhaobdb76512011-09-07 11:43:16 -07001666 * but when they do they act as if they succeeded and the PC is
jeffhaod1f0fde2011-09-08 17:25:33 -07001667 * pointing to the following instruction. (This behavior goes back
jeffhaobdb76512011-09-07 11:43:16 -07001668 * to the need to handle asynchronous exceptions, a now-deprecated
1669 * feature that Dalvik doesn't support.)
1670 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001671 * In practice we don't need to worry about this. The only
jeffhaobdb76512011-09-07 11:43:16 -07001672 * exceptions that can be thrown from monitor-exit are for a
jeffhaod1f0fde2011-09-08 17:25:33 -07001673 * null reference and -exit without a matching -enter. If the
jeffhaobdb76512011-09-07 11:43:16 -07001674 * structured locking checks are working, the former would have
1675 * failed on the -enter instruction, and the latter is impossible.
1676 *
1677 * This is fortunate, because issue 3221411 prevents us from
1678 * chasing the "can throw" path when monitor verification is
jeffhaod1f0fde2011-09-08 17:25:33 -07001679 * enabled. If we can fully verify the locking we can ignore
jeffhaobdb76512011-09-07 11:43:16 -07001680 * some catch blocks (which will show up as "dead" code when
1681 * we skip them here); if we can't, then the code path could be
1682 * "live" so we still need to check it.
1683 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001684 opcode_flags &= ~Instruction::kThrow;
Sebastien Hertz5243e912013-05-21 10:55:07 +02001685 work_line_->PopMonitor(inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07001686 break;
1687
Ian Rogers28ad40d2011-10-27 15:19:26 -07001688 case Instruction::CHECK_CAST:
Ian Rogersd81871c2011-10-03 13:57:23 -07001689 case Instruction::INSTANCE_OF: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001690 /*
1691 * If this instruction succeeds, we will "downcast" register vA to the type in vB. (This
1692 * could be a "upcast" -- not expected, so we don't try to address it.)
1693 *
1694 * If it fails, an exception is thrown, which we deal with later by ignoring the update to
Elliott Hughesadb8c672012-03-06 16:49:32 -08001695 * dec_insn.vA when branching to a handler.
Ian Rogers28ad40d2011-10-27 15:19:26 -07001696 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001697 const bool is_checkcast = (inst->Opcode() == Instruction::CHECK_CAST);
1698 const uint32_t type_idx = (is_checkcast) ? inst->VRegB_21c() : inst->VRegC_22c();
1699 const RegType& res_type = ResolveClassAndCheckAccess(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001700 if (res_type.IsConflict()) {
1701 DCHECK_NE(failures_.size(), 0U);
1702 if (!is_checkcast) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001703 work_line_->SetRegisterType(inst->VRegA_22c(), reg_types_.Boolean());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001704 }
1705 break; // bad class
Ian Rogers9f1ab122011-12-12 08:52:43 -08001706 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001707 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Sebastien Hertz5243e912013-05-21 10:55:07 +02001708 uint32_t orig_type_reg = (is_checkcast) ? inst->VRegA_21c() : inst->VRegB_22c();
1709 const RegType& orig_type = work_line_->GetRegisterType(orig_type_reg);
Ian Rogers28ad40d2011-10-27 15:19:26 -07001710 if (!res_type.IsNonZeroReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001711 if (is_checkcast) {
1712 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on unexpected class " << res_type;
1713 } else {
1714 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on unexpected class " << res_type;
1715 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001716 } else if (!orig_type.IsReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001717 if (is_checkcast) {
1718 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on non-reference in v" << orig_type_reg;
1719 } else {
1720 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on non-reference in v" << orig_type_reg;
1721 }
jeffhao2a8a90e2011-09-26 14:25:31 -07001722 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001723 if (is_checkcast) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001724 work_line_->SetRegisterType(inst->VRegA_21c(), res_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001725 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001726 work_line_->SetRegisterType(inst->VRegA_22c(), reg_types_.Boolean());
jeffhaobdb76512011-09-07 11:43:16 -07001727 }
jeffhaobdb76512011-09-07 11:43:16 -07001728 }
jeffhao2a8a90e2011-09-26 14:25:31 -07001729 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001730 }
1731 case Instruction::ARRAY_LENGTH: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001732 const RegType& res_type = work_line_->GetRegisterType(inst->VRegB_12x());
Ian Rogers28ad40d2011-10-27 15:19:26 -07001733 if (res_type.IsReferenceTypes()) {
Ian Rogers89310de2012-02-01 13:47:30 -08001734 if (!res_type.IsArrayTypes() && !res_type.IsZero()) { // ie not an array or null
jeffhaod5347e02012-03-22 17:25:05 -07001735 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001736 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001737 work_line_->SetRegisterType(inst->VRegA_12x(), reg_types_.Integer());
Ian Rogersd81871c2011-10-03 13:57:23 -07001738 }
1739 }
1740 break;
1741 }
1742 case Instruction::NEW_INSTANCE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001743 const RegType& res_type = ResolveClassAndCheckAccess(inst->VRegB_21c());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001744 if (res_type.IsConflict()) {
1745 DCHECK_NE(failures_.size(), 0U);
1746 break; // bad class
jeffhao8cd6dda2012-02-22 10:15:34 -08001747 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001748 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
1749 // can't create an instance of an interface or abstract class */
1750 if (!res_type.IsInstantiableTypes()) {
1751 Fail(VERIFY_ERROR_INSTANTIATION)
1752 << "new-instance on primitive, interface or abstract class" << res_type;
Ian Rogers08f753d2012-08-24 14:35:25 -07001753 // Soft failure so carry on to set register type.
Ian Rogersd81871c2011-10-03 13:57:23 -07001754 }
Ian Rogers08f753d2012-08-24 14:35:25 -07001755 const RegType& uninit_type = reg_types_.Uninitialized(res_type, work_insn_idx_);
1756 // Any registers holding previous allocations from this address that have not yet been
1757 // initialized must be marked invalid.
1758 work_line_->MarkUninitRefsAsInvalid(uninit_type);
1759 // add the new uninitialized reference to the register state
Sebastien Hertz5243e912013-05-21 10:55:07 +02001760 work_line_->SetRegisterType(inst->VRegA_21c(), uninit_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001761 break;
1762 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08001763 case Instruction::NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001764 VerifyNewArray(inst, false, false);
jeffhaobdb76512011-09-07 11:43:16 -07001765 break;
1766 case Instruction::FILLED_NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001767 VerifyNewArray(inst, true, false);
Ian Rogers0c4a5062012-02-03 15:18:59 -08001768 just_set_result = true; // Filled new array sets result register
jeffhaobdb76512011-09-07 11:43:16 -07001769 break;
Ian Rogers0c4a5062012-02-03 15:18:59 -08001770 case Instruction::FILLED_NEW_ARRAY_RANGE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001771 VerifyNewArray(inst, true, true);
Ian Rogers0c4a5062012-02-03 15:18:59 -08001772 just_set_result = true; // Filled new array range sets result register
1773 break;
jeffhaobdb76512011-09-07 11:43:16 -07001774 case Instruction::CMPL_FLOAT:
1775 case Instruction::CMPG_FLOAT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001776 if (!work_line_->VerifyRegisterType(inst->VRegB_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08001777 break;
1778 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02001779 if (!work_line_->VerifyRegisterType(inst->VRegC_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08001780 break;
1781 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02001782 work_line_->SetRegisterType(inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001783 break;
1784 case Instruction::CMPL_DOUBLE:
1785 case Instruction::CMPG_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001786 if (!work_line_->VerifyRegisterTypeWide(inst->VRegB_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001787 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001788 break;
1789 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02001790 if (!work_line_->VerifyRegisterTypeWide(inst->VRegC_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001791 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001792 break;
1793 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02001794 work_line_->SetRegisterType(inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001795 break;
1796 case Instruction::CMP_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001797 if (!work_line_->VerifyRegisterTypeWide(inst->VRegB_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001798 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001799 break;
1800 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02001801 if (!work_line_->VerifyRegisterTypeWide(inst->VRegC_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001802 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001803 break;
1804 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02001805 work_line_->SetRegisterType(inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001806 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001807 case Instruction::THROW: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001808 const RegType& res_type = work_line_->GetRegisterType(inst->VRegA_11x());
Ian Rogersb4903572012-10-11 11:52:56 -07001809 if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(res_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07001810 Fail(res_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS : VERIFY_ERROR_BAD_CLASS_SOFT)
1811 << "thrown class " << res_type << " not instanceof Throwable";
jeffhaobdb76512011-09-07 11:43:16 -07001812 }
1813 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001814 }
jeffhaobdb76512011-09-07 11:43:16 -07001815 case Instruction::GOTO:
1816 case Instruction::GOTO_16:
1817 case Instruction::GOTO_32:
1818 /* no effect on or use of registers */
1819 break;
1820
1821 case Instruction::PACKED_SWITCH:
1822 case Instruction::SPARSE_SWITCH:
1823 /* verify that vAA is an integer, or can be converted to one */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001824 work_line_->VerifyRegisterType(inst->VRegA_31t(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001825 break;
1826
Ian Rogersd81871c2011-10-03 13:57:23 -07001827 case Instruction::FILL_ARRAY_DATA: {
1828 /* Similar to the verification done for APUT */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001829 const RegType& array_type = work_line_->GetRegisterType(inst->VRegA_31t());
Ian Rogers89310de2012-02-01 13:47:30 -08001830 /* array_type can be null if the reg type is Zero */
1831 if (!array_type.IsZero()) {
jeffhao457cc512012-02-02 16:55:13 -08001832 if (!array_type.IsArrayTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001833 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with array type "
1834 << array_type;
Ian Rogers89310de2012-02-01 13:47:30 -08001835 } else {
Mathieu Chartier590fee92013-09-13 13:46:47 -07001836 const RegType& component_type = reg_types_.GetComponentType(array_type,
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001837 class_loader_->Get());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001838 DCHECK(!component_type.IsConflict());
jeffhao457cc512012-02-02 16:55:13 -08001839 if (component_type.IsNonZeroReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001840 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with component type "
1841 << component_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001842 } else {
jeffhao457cc512012-02-02 16:55:13 -08001843 // Now verify if the element width in the table matches the element width declared in
1844 // the array
1845 const uint16_t* array_data = insns + (insns[1] | (((int32_t) insns[2]) << 16));
1846 if (array_data[0] != Instruction::kArrayDataSignature) {
jeffhaod5347e02012-03-22 17:25:05 -07001847 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid magic for array-data";
jeffhao457cc512012-02-02 16:55:13 -08001848 } else {
1849 size_t elem_width = Primitive::ComponentSize(component_type.GetPrimitiveType());
1850 // Since we don't compress the data in Dex, expect to see equal width of data stored
1851 // in the table and expected from the array class.
1852 if (array_data[1] != elem_width) {
jeffhaod5347e02012-03-22 17:25:05 -07001853 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-data size mismatch (" << array_data[1]
1854 << " vs " << elem_width << ")";
jeffhao457cc512012-02-02 16:55:13 -08001855 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001856 }
1857 }
jeffhaobdb76512011-09-07 11:43:16 -07001858 }
1859 }
1860 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001861 }
jeffhaobdb76512011-09-07 11:43:16 -07001862 case Instruction::IF_EQ:
Ian Rogersd81871c2011-10-03 13:57:23 -07001863 case Instruction::IF_NE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001864 const RegType& reg_type1 = work_line_->GetRegisterType(inst->VRegA_22t());
1865 const RegType& reg_type2 = work_line_->GetRegisterType(inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07001866 bool mismatch = false;
1867 if (reg_type1.IsZero()) { // zero then integral or reference expected
1868 mismatch = !reg_type2.IsReferenceTypes() && !reg_type2.IsIntegralTypes();
1869 } else if (reg_type1.IsReferenceTypes()) { // both references?
1870 mismatch = !reg_type2.IsReferenceTypes();
1871 } else { // both integral?
1872 mismatch = !reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes();
1873 }
1874 if (mismatch) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001875 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to if-eq/if-ne (" << reg_type1 << ","
1876 << reg_type2 << ") must both be references or integral";
jeffhaobdb76512011-09-07 11:43:16 -07001877 }
1878 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001879 }
jeffhaobdb76512011-09-07 11:43:16 -07001880 case Instruction::IF_LT:
1881 case Instruction::IF_GE:
1882 case Instruction::IF_GT:
Ian Rogersd81871c2011-10-03 13:57:23 -07001883 case Instruction::IF_LE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001884 const RegType& reg_type1 = work_line_->GetRegisterType(inst->VRegA_22t());
1885 const RegType& reg_type2 = work_line_->GetRegisterType(inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07001886 if (!reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001887 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to 'if' (" << reg_type1 << ","
1888 << reg_type2 << ") must be integral";
jeffhaobdb76512011-09-07 11:43:16 -07001889 }
1890 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001891 }
jeffhaobdb76512011-09-07 11:43:16 -07001892 case Instruction::IF_EQZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07001893 case Instruction::IF_NEZ: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001894 const RegType& reg_type = work_line_->GetRegisterType(inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07001895 if (!reg_type.IsReferenceTypes() && !reg_type.IsIntegralTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001896 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
1897 << " unexpected as arg to if-eqz/if-nez";
Ian Rogersd81871c2011-10-03 13:57:23 -07001898 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001899
1900 // Find previous instruction - its existence is a precondition to peephole optimization.
Ian Rogers9b360392013-06-06 14:45:07 -07001901 uint32_t instance_of_idx = 0;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001902 if (0 != work_insn_idx_) {
Ian Rogers9b360392013-06-06 14:45:07 -07001903 instance_of_idx = work_insn_idx_ - 1;
Brian Carlstromdf629502013-07-17 22:39:56 -07001904 while (0 != instance_of_idx && !insn_flags_[instance_of_idx].IsOpcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07001905 instance_of_idx--;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001906 }
Ian Rogers9b360392013-06-06 14:45:07 -07001907 CHECK(insn_flags_[instance_of_idx].IsOpcode());
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001908 } else {
1909 break;
1910 }
1911
Ian Rogers9b360392013-06-06 14:45:07 -07001912 const Instruction* instance_of_inst = Instruction::At(code_item_->insns_ + instance_of_idx);
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001913
1914 /* Check for peep-hole pattern of:
1915 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07001916 * instance-of vX, vY, T;
1917 * ifXXX vX, label ;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001918 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07001919 * label:
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001920 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07001921 * and sharpen the type of vY to be type T.
1922 * Note, this pattern can't be if:
1923 * - if there are other branches to this branch,
1924 * - when vX == vY.
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001925 */
Ian Rogersfae370a2013-06-05 08:33:27 -07001926 if (!CurrentInsnFlags()->IsBranchTarget() &&
Ian Rogers9b360392013-06-06 14:45:07 -07001927 (Instruction::INSTANCE_OF == instance_of_inst->Opcode()) &&
1928 (inst->VRegA_21t() == instance_of_inst->VRegA_22c()) &&
1929 (instance_of_inst->VRegA_22c() != instance_of_inst->VRegB_22c())) {
Ian Rogersfae370a2013-06-05 08:33:27 -07001930 // Check that the we are not attempting conversion to interface types,
1931 // which is not done because of the multiple inheritance implications.
Jeff Haoc642ec82013-09-04 16:11:55 -07001932 // Also don't change the type if it would result in an upcast.
1933 const RegType& orig_type = work_line_->GetRegisterType(instance_of_inst->VRegB_22c());
Ian Rogers9b360392013-06-06 14:45:07 -07001934 const RegType& cast_type = ResolveClassAndCheckAccess(instance_of_inst->VRegC_22c());
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001935
Jeff Haoa3faaf42013-09-03 19:07:00 -07001936 if (!cast_type.IsUnresolvedTypes() && !orig_type.IsUnresolvedTypes() &&
1937 !cast_type.GetClass()->IsInterface() && !cast_type.IsAssignableFrom(orig_type)) {
Ian Rogersd0fbd852013-09-24 18:17:04 -07001938 RegisterLine* update_line = RegisterLine::Create(code_item_->registers_size_, this);
Ian Rogersfae370a2013-06-05 08:33:27 -07001939 if (inst->Opcode() == Instruction::IF_EQZ) {
Ian Rogers9b360392013-06-06 14:45:07 -07001940 fallthrough_line.reset(update_line);
Ian Rogersfae370a2013-06-05 08:33:27 -07001941 } else {
Ian Rogers9b360392013-06-06 14:45:07 -07001942 branch_line.reset(update_line);
1943 }
1944 update_line->CopyFromLine(work_line_.get());
1945 update_line->SetRegisterType(instance_of_inst->VRegB_22c(), cast_type);
1946 if (!insn_flags_[instance_of_idx].IsBranchTarget() && 0 != instance_of_idx) {
1947 // See if instance-of was preceded by a move-object operation, common due to the small
1948 // register encoding space of instance-of, and propagate type information to the source
1949 // of the move-object.
1950 uint32_t move_idx = instance_of_idx - 1;
Brian Carlstromdf629502013-07-17 22:39:56 -07001951 while (0 != move_idx && !insn_flags_[move_idx].IsOpcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07001952 move_idx--;
1953 }
1954 CHECK(insn_flags_[move_idx].IsOpcode());
1955 const Instruction* move_inst = Instruction::At(code_item_->insns_ + move_idx);
1956 switch (move_inst->Opcode()) {
1957 case Instruction::MOVE_OBJECT:
1958 if (move_inst->VRegA_12x() == instance_of_inst->VRegB_22c()) {
1959 update_line->SetRegisterType(move_inst->VRegB_12x(), cast_type);
1960 }
1961 break;
1962 case Instruction::MOVE_OBJECT_FROM16:
1963 if (move_inst->VRegA_22x() == instance_of_inst->VRegB_22c()) {
1964 update_line->SetRegisterType(move_inst->VRegB_22x(), cast_type);
1965 }
1966 break;
1967 case Instruction::MOVE_OBJECT_16:
1968 if (move_inst->VRegA_32x() == instance_of_inst->VRegB_22c()) {
1969 update_line->SetRegisterType(move_inst->VRegB_32x(), cast_type);
1970 }
1971 break;
1972 default:
1973 break;
1974 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001975 }
1976 }
1977 }
1978
jeffhaobdb76512011-09-07 11:43:16 -07001979 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001980 }
jeffhaobdb76512011-09-07 11:43:16 -07001981 case Instruction::IF_LTZ:
1982 case Instruction::IF_GEZ:
1983 case Instruction::IF_GTZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07001984 case Instruction::IF_LEZ: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001985 const RegType& reg_type = work_line_->GetRegisterType(inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07001986 if (!reg_type.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001987 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
1988 << " unexpected as arg to if-ltz/if-gez/if-gtz/if-lez";
Ian Rogersd81871c2011-10-03 13:57:23 -07001989 }
jeffhaobdb76512011-09-07 11:43:16 -07001990 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001991 }
jeffhaobdb76512011-09-07 11:43:16 -07001992 case Instruction::AGET_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001993 VerifyAGet(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001994 break;
jeffhaobdb76512011-09-07 11:43:16 -07001995 case Instruction::AGET_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001996 VerifyAGet(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001997 break;
jeffhaobdb76512011-09-07 11:43:16 -07001998 case Instruction::AGET_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001999 VerifyAGet(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002000 break;
jeffhaobdb76512011-09-07 11:43:16 -07002001 case Instruction::AGET_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002002 VerifyAGet(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002003 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002004 case Instruction::AGET:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002005 VerifyAGet(inst, reg_types_.Integer(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002006 break;
jeffhaobdb76512011-09-07 11:43:16 -07002007 case Instruction::AGET_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002008 VerifyAGet(inst, reg_types_.LongLo(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002009 break;
2010 case Instruction::AGET_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002011 VerifyAGet(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07002012 break;
2013
Ian Rogersd81871c2011-10-03 13:57:23 -07002014 case Instruction::APUT_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002015 VerifyAPut(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002016 break;
2017 case Instruction::APUT_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002018 VerifyAPut(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002019 break;
2020 case Instruction::APUT_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002021 VerifyAPut(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002022 break;
2023 case Instruction::APUT_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002024 VerifyAPut(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002025 break;
2026 case Instruction::APUT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002027 VerifyAPut(inst, reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002028 break;
2029 case Instruction::APUT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002030 VerifyAPut(inst, reg_types_.LongLo(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002031 break;
2032 case Instruction::APUT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002033 VerifyAPut(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07002034 break;
2035
jeffhaobdb76512011-09-07 11:43:16 -07002036 case Instruction::IGET_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002037 VerifyISGet(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002038 break;
jeffhaobdb76512011-09-07 11:43:16 -07002039 case Instruction::IGET_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002040 VerifyISGet(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002041 break;
jeffhaobdb76512011-09-07 11:43:16 -07002042 case Instruction::IGET_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002043 VerifyISGet(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002044 break;
jeffhaobdb76512011-09-07 11:43:16 -07002045 case Instruction::IGET_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002046 VerifyISGet(inst, reg_types_.Short(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002047 break;
2048 case Instruction::IGET:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002049 VerifyISGet(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002050 break;
2051 case Instruction::IGET_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002052 VerifyISGet(inst, reg_types_.LongLo(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002053 break;
2054 case Instruction::IGET_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002055 VerifyISGet(inst, reg_types_.JavaLangObject(false), false, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002056 break;
jeffhaobdb76512011-09-07 11:43:16 -07002057
Ian Rogersd81871c2011-10-03 13:57:23 -07002058 case Instruction::IPUT_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002059 VerifyISPut(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002060 break;
2061 case Instruction::IPUT_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002062 VerifyISPut(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002063 break;
2064 case Instruction::IPUT_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002065 VerifyISPut(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002066 break;
2067 case Instruction::IPUT_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002068 VerifyISPut(inst, reg_types_.Short(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002069 break;
2070 case Instruction::IPUT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002071 VerifyISPut(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002072 break;
2073 case Instruction::IPUT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002074 VerifyISPut(inst, reg_types_.LongLo(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002075 break;
jeffhaobdb76512011-09-07 11:43:16 -07002076 case Instruction::IPUT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002077 VerifyISPut(inst, reg_types_.JavaLangObject(false), false, false);
jeffhaobdb76512011-09-07 11:43:16 -07002078 break;
2079
jeffhaobdb76512011-09-07 11:43:16 -07002080 case Instruction::SGET_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002081 VerifyISGet(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002082 break;
jeffhaobdb76512011-09-07 11:43:16 -07002083 case Instruction::SGET_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002084 VerifyISGet(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002085 break;
jeffhaobdb76512011-09-07 11:43:16 -07002086 case Instruction::SGET_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002087 VerifyISGet(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002088 break;
jeffhaobdb76512011-09-07 11:43:16 -07002089 case Instruction::SGET_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002090 VerifyISGet(inst, reg_types_.Short(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002091 break;
2092 case Instruction::SGET:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002093 VerifyISGet(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002094 break;
2095 case Instruction::SGET_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002096 VerifyISGet(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002097 break;
2098 case Instruction::SGET_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002099 VerifyISGet(inst, reg_types_.JavaLangObject(false), false, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002100 break;
2101
2102 case Instruction::SPUT_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002103 VerifyISPut(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002104 break;
2105 case Instruction::SPUT_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002106 VerifyISPut(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002107 break;
2108 case Instruction::SPUT_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002109 VerifyISPut(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002110 break;
2111 case Instruction::SPUT_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002112 VerifyISPut(inst, reg_types_.Short(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002113 break;
2114 case Instruction::SPUT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002115 VerifyISPut(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002116 break;
2117 case Instruction::SPUT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002118 VerifyISPut(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002119 break;
2120 case Instruction::SPUT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002121 VerifyISPut(inst, reg_types_.JavaLangObject(false), false, true);
jeffhaobdb76512011-09-07 11:43:16 -07002122 break;
2123
2124 case Instruction::INVOKE_VIRTUAL:
2125 case Instruction::INVOKE_VIRTUAL_RANGE:
2126 case Instruction::INVOKE_SUPER:
Ian Rogersd81871c2011-10-03 13:57:23 -07002127 case Instruction::INVOKE_SUPER_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002128 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE ||
2129 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
2130 bool is_super = (inst->Opcode() == Instruction::INVOKE_SUPER ||
2131 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
Brian Carlstromea46f952013-07-30 01:26:50 -07002132 mirror::ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_VIRTUAL,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002133 is_range, is_super);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002134 const RegType* return_type = nullptr;
2135 if (called_method != nullptr) {
2136 MethodHelper mh(called_method);
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08002137 mirror::Class* return_type_class = mh.GetReturnType(can_load_classes_);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002138 if (return_type_class != nullptr) {
2139 return_type = &reg_types_.FromClass(mh.GetReturnTypeDescriptor(), return_type_class,
2140 return_type_class->CannotBeAssignedFromOtherTypes());
2141 } else {
2142 Thread* self = Thread::Current();
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08002143 DCHECK(!can_load_classes_ || self->IsExceptionPending());
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002144 self->ClearException();
2145 }
2146 }
2147 if (return_type == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002148 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002149 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2150 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002151 const char* descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07002152 return_type = &reg_types_.FromDescriptor(class_loader_->Get(), descriptor, false);
jeffhaobdb76512011-09-07 11:43:16 -07002153 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002154 if (!return_type->IsLowHalf()) {
2155 work_line_->SetResultRegisterType(*return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002156 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002157 work_line_->SetResultRegisterTypeWide(*return_type, return_type->HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002158 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002159 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002160 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002161 }
jeffhaobdb76512011-09-07 11:43:16 -07002162 case Instruction::INVOKE_DIRECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002163 case Instruction::INVOKE_DIRECT_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002164 bool is_range = (inst->Opcode() == Instruction::INVOKE_DIRECT_RANGE);
Brian Carlstromea46f952013-07-30 01:26:50 -07002165 mirror::ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_DIRECT,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002166 is_range, false);
Ian Rogers46685432012-06-03 22:26:43 -07002167 const char* return_type_descriptor;
2168 bool is_constructor;
2169 if (called_method == NULL) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002170 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogers46685432012-06-03 22:26:43 -07002171 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
Ian Rogersdfb325e2013-10-30 01:00:44 -07002172 is_constructor = strcmp("<init>", dex_file_->StringDataByIdx(method_id.name_idx_)) == 0;
Ian Rogers46685432012-06-03 22:26:43 -07002173 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
2174 return_type_descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2175 } else {
2176 is_constructor = called_method->IsConstructor();
2177 return_type_descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
2178 }
2179 if (is_constructor) {
jeffhaobdb76512011-09-07 11:43:16 -07002180 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002181 * Some additional checks when calling a constructor. We know from the invocation arg check
2182 * that the "this" argument is an instance of called_method->klass. Now we further restrict
2183 * that to require that called_method->klass is the same as this->klass or this->super,
2184 * allowing the latter only if the "this" argument is the same as the "this" argument to
2185 * this method (which implies that we're in a constructor ourselves).
jeffhaobdb76512011-09-07 11:43:16 -07002186 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002187 const RegType& this_type = work_line_->GetInvocationThis(inst, is_range);
jeffhaob57e9522012-04-26 18:08:21 -07002188 if (this_type.IsConflict()) // failure.
2189 break;
jeffhaobdb76512011-09-07 11:43:16 -07002190
jeffhaob57e9522012-04-26 18:08:21 -07002191 /* no null refs allowed (?) */
2192 if (this_type.IsZero()) {
2193 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unable to initialize null ref";
2194 break;
jeffhao2a8a90e2011-09-26 14:25:31 -07002195 }
jeffhaob57e9522012-04-26 18:08:21 -07002196
2197 /* must be in same class or in superclass */
Ian Rogers46685432012-06-03 22:26:43 -07002198 // const RegType& this_super_klass = this_type.GetSuperClass(&reg_types_);
2199 // TODO: re-enable constructor type verification
2200 // if (this_super_klass.IsConflict()) {
jeffhaob57e9522012-04-26 18:08:21 -07002201 // Unknown super class, fail so we re-check at runtime.
Ian Rogers46685432012-06-03 22:26:43 -07002202 // Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "super class unknown for '" << this_type << "'";
2203 // break;
2204 // }
jeffhaob57e9522012-04-26 18:08:21 -07002205
2206 /* arg must be an uninitialized reference */
2207 if (!this_type.IsUninitializedTypes()) {
2208 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Expected initialization on uninitialized reference "
2209 << this_type;
2210 break;
2211 }
2212
2213 /*
2214 * Replace the uninitialized reference with an initialized one. We need to do this for all
2215 * registers that have the same object instance in them, not just the "this" register.
2216 */
2217 work_line_->MarkRefsAsInitialized(this_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002218 }
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07002219 const RegType& return_type = reg_types_.FromDescriptor(class_loader_->Get(),
Mathieu Chartier590fee92013-09-13 13:46:47 -07002220 return_type_descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002221 if (!return_type.IsLowHalf()) {
2222 work_line_->SetResultRegisterType(return_type);
2223 } else {
2224 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2225 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002226 just_set_result = true;
2227 break;
2228 }
2229 case Instruction::INVOKE_STATIC:
2230 case Instruction::INVOKE_STATIC_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002231 bool is_range = (inst->Opcode() == Instruction::INVOKE_STATIC_RANGE);
Brian Carlstromea46f952013-07-30 01:26:50 -07002232 mirror::ArtMethod* called_method = VerifyInvocationArgs(inst,
Brian Carlstrom93c33962013-07-26 10:37:43 -07002233 METHOD_STATIC,
2234 is_range,
2235 false);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002236 const char* descriptor;
2237 if (called_method == NULL) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002238 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002239 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2240 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Mathieu Chartier590fee92013-09-13 13:46:47 -07002241 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002242 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002243 descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002244 }
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07002245 const RegType& return_type = reg_types_.FromDescriptor(class_loader_->Get(), descriptor,
Mathieu Chartier590fee92013-09-13 13:46:47 -07002246 false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002247 if (!return_type.IsLowHalf()) {
2248 work_line_->SetResultRegisterType(return_type);
2249 } else {
2250 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2251 }
jeffhaobdb76512011-09-07 11:43:16 -07002252 just_set_result = true;
2253 }
2254 break;
jeffhaobdb76512011-09-07 11:43:16 -07002255 case Instruction::INVOKE_INTERFACE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002256 case Instruction::INVOKE_INTERFACE_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002257 bool is_range = (inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE);
Brian Carlstromea46f952013-07-30 01:26:50 -07002258 mirror::ArtMethod* abs_method = VerifyInvocationArgs(inst,
Brian Carlstrom93c33962013-07-26 10:37:43 -07002259 METHOD_INTERFACE,
2260 is_range,
2261 false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002262 if (abs_method != NULL) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002263 mirror::Class* called_interface = abs_method->GetDeclaringClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002264 if (!called_interface->IsInterface() && !called_interface->IsObjectClass()) {
2265 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected interface class in invoke-interface '"
2266 << PrettyMethod(abs_method) << "'";
2267 break;
Ian Rogers28ad40d2011-10-27 15:19:26 -07002268 }
Ian Rogers0d604842012-04-16 14:50:24 -07002269 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002270 /* Get the type of the "this" arg, which should either be a sub-interface of called
2271 * interface or Object (see comments in RegType::JoinClass).
2272 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002273 const RegType& this_type = work_line_->GetInvocationThis(inst, is_range);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002274 if (this_type.IsZero()) {
2275 /* null pointer always passes (and always fails at runtime) */
2276 } else {
2277 if (this_type.IsUninitializedTypes()) {
2278 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface call on uninitialized object "
2279 << this_type;
2280 break;
2281 }
2282 // In the past we have tried to assert that "called_interface" is assignable
2283 // from "this_type.GetClass()", however, as we do an imprecise Join
2284 // (RegType::JoinClass) we don't have full information on what interfaces are
2285 // implemented by "this_type". For example, two classes may implement the same
2286 // interfaces and have a common parent that doesn't implement the interface. The
2287 // join will set "this_type" to the parent class and a test that this implements
2288 // the interface will incorrectly fail.
2289 }
2290 /*
2291 * We don't have an object instance, so we can't find the concrete method. However, all of
2292 * the type information is in the abstract method, so we're good.
2293 */
2294 const char* descriptor;
2295 if (abs_method == NULL) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002296 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002297 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2298 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
2299 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2300 } else {
2301 descriptor = MethodHelper(abs_method).GetReturnTypeDescriptor();
2302 }
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07002303 const RegType& return_type = reg_types_.FromDescriptor(class_loader_->Get(), descriptor,
Mathieu Chartier590fee92013-09-13 13:46:47 -07002304 false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002305 if (!return_type.IsLowHalf()) {
2306 work_line_->SetResultRegisterType(return_type);
2307 } else {
2308 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2309 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002310 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002311 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002312 }
jeffhaobdb76512011-09-07 11:43:16 -07002313 case Instruction::NEG_INT:
2314 case Instruction::NOT_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002315 work_line_->CheckUnaryOp(inst, reg_types_.Integer(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002316 break;
2317 case Instruction::NEG_LONG:
2318 case Instruction::NOT_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002319 work_line_->CheckUnaryOpWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002320 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002321 break;
2322 case Instruction::NEG_FLOAT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002323 work_line_->CheckUnaryOp(inst, reg_types_.Float(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002324 break;
2325 case Instruction::NEG_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002326 work_line_->CheckUnaryOpWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002327 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002328 break;
2329 case Instruction::INT_TO_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002330 work_line_->CheckUnaryOpToWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002331 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002332 break;
2333 case Instruction::INT_TO_FLOAT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002334 work_line_->CheckUnaryOp(inst, reg_types_.Float(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002335 break;
2336 case Instruction::INT_TO_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002337 work_line_->CheckUnaryOpToWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002338 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002339 break;
2340 case Instruction::LONG_TO_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002341 work_line_->CheckUnaryOpFromWide(inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002342 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002343 break;
2344 case Instruction::LONG_TO_FLOAT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002345 work_line_->CheckUnaryOpFromWide(inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002346 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002347 break;
2348 case Instruction::LONG_TO_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002349 work_line_->CheckUnaryOpWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002350 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002351 break;
2352 case Instruction::FLOAT_TO_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002353 work_line_->CheckUnaryOp(inst, reg_types_.Integer(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002354 break;
2355 case Instruction::FLOAT_TO_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002356 work_line_->CheckUnaryOpToWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002357 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002358 break;
2359 case Instruction::FLOAT_TO_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002360 work_line_->CheckUnaryOpToWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002361 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002362 break;
2363 case Instruction::DOUBLE_TO_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002364 work_line_->CheckUnaryOpFromWide(inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002365 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002366 break;
2367 case Instruction::DOUBLE_TO_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002368 work_line_->CheckUnaryOpWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002369 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002370 break;
2371 case Instruction::DOUBLE_TO_FLOAT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002372 work_line_->CheckUnaryOpFromWide(inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002373 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002374 break;
2375 case Instruction::INT_TO_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002376 work_line_->CheckUnaryOp(inst, reg_types_.Byte(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002377 break;
2378 case Instruction::INT_TO_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002379 work_line_->CheckUnaryOp(inst, reg_types_.Char(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002380 break;
2381 case Instruction::INT_TO_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002382 work_line_->CheckUnaryOp(inst, reg_types_.Short(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002383 break;
2384
2385 case Instruction::ADD_INT:
2386 case Instruction::SUB_INT:
2387 case Instruction::MUL_INT:
2388 case Instruction::REM_INT:
2389 case Instruction::DIV_INT:
2390 case Instruction::SHL_INT:
2391 case Instruction::SHR_INT:
2392 case Instruction::USHR_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002393 work_line_->CheckBinaryOp(inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002394 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002395 break;
2396 case Instruction::AND_INT:
2397 case Instruction::OR_INT:
2398 case Instruction::XOR_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002399 work_line_->CheckBinaryOp(inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002400 reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002401 break;
2402 case Instruction::ADD_LONG:
2403 case Instruction::SUB_LONG:
2404 case Instruction::MUL_LONG:
2405 case Instruction::DIV_LONG:
2406 case Instruction::REM_LONG:
2407 case Instruction::AND_LONG:
2408 case Instruction::OR_LONG:
2409 case Instruction::XOR_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002410 work_line_->CheckBinaryOpWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002411 reg_types_.LongLo(), reg_types_.LongHi(),
2412 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002413 break;
2414 case Instruction::SHL_LONG:
2415 case Instruction::SHR_LONG:
2416 case Instruction::USHR_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002417 /* shift distance is Int, making these different from other binary operations */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002418 work_line_->CheckBinaryOpWideShift(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002419 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002420 break;
2421 case Instruction::ADD_FLOAT:
2422 case Instruction::SUB_FLOAT:
2423 case Instruction::MUL_FLOAT:
2424 case Instruction::DIV_FLOAT:
2425 case Instruction::REM_FLOAT:
Brian Carlstrom93c33962013-07-26 10:37:43 -07002426 work_line_->CheckBinaryOp(inst,
2427 reg_types_.Float(),
2428 reg_types_.Float(),
2429 reg_types_.Float(),
2430 false);
jeffhaobdb76512011-09-07 11:43:16 -07002431 break;
2432 case Instruction::ADD_DOUBLE:
2433 case Instruction::SUB_DOUBLE:
2434 case Instruction::MUL_DOUBLE:
2435 case Instruction::DIV_DOUBLE:
2436 case Instruction::REM_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002437 work_line_->CheckBinaryOpWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002438 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2439 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002440 break;
2441 case Instruction::ADD_INT_2ADDR:
2442 case Instruction::SUB_INT_2ADDR:
2443 case Instruction::MUL_INT_2ADDR:
2444 case Instruction::REM_INT_2ADDR:
2445 case Instruction::SHL_INT_2ADDR:
2446 case Instruction::SHR_INT_2ADDR:
2447 case Instruction::USHR_INT_2ADDR:
Brian Carlstrom93c33962013-07-26 10:37:43 -07002448 work_line_->CheckBinaryOp2addr(inst,
2449 reg_types_.Integer(),
2450 reg_types_.Integer(),
2451 reg_types_.Integer(),
2452 false);
jeffhaobdb76512011-09-07 11:43:16 -07002453 break;
2454 case Instruction::AND_INT_2ADDR:
2455 case Instruction::OR_INT_2ADDR:
2456 case Instruction::XOR_INT_2ADDR:
Brian Carlstrom93c33962013-07-26 10:37:43 -07002457 work_line_->CheckBinaryOp2addr(inst,
2458 reg_types_.Integer(),
2459 reg_types_.Integer(),
2460 reg_types_.Integer(),
2461 true);
jeffhaobdb76512011-09-07 11:43:16 -07002462 break;
2463 case Instruction::DIV_INT_2ADDR:
Brian Carlstrom93c33962013-07-26 10:37:43 -07002464 work_line_->CheckBinaryOp2addr(inst,
2465 reg_types_.Integer(),
2466 reg_types_.Integer(),
2467 reg_types_.Integer(),
2468 false);
jeffhaobdb76512011-09-07 11:43:16 -07002469 break;
2470 case Instruction::ADD_LONG_2ADDR:
2471 case Instruction::SUB_LONG_2ADDR:
2472 case Instruction::MUL_LONG_2ADDR:
2473 case Instruction::DIV_LONG_2ADDR:
2474 case Instruction::REM_LONG_2ADDR:
2475 case Instruction::AND_LONG_2ADDR:
2476 case Instruction::OR_LONG_2ADDR:
2477 case Instruction::XOR_LONG_2ADDR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002478 work_line_->CheckBinaryOp2addrWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002479 reg_types_.LongLo(), reg_types_.LongHi(),
2480 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002481 break;
2482 case Instruction::SHL_LONG_2ADDR:
2483 case Instruction::SHR_LONG_2ADDR:
2484 case Instruction::USHR_LONG_2ADDR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002485 work_line_->CheckBinaryOp2addrWideShift(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002486 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002487 break;
2488 case Instruction::ADD_FLOAT_2ADDR:
2489 case Instruction::SUB_FLOAT_2ADDR:
2490 case Instruction::MUL_FLOAT_2ADDR:
2491 case Instruction::DIV_FLOAT_2ADDR:
2492 case Instruction::REM_FLOAT_2ADDR:
Brian Carlstrom93c33962013-07-26 10:37:43 -07002493 work_line_->CheckBinaryOp2addr(inst,
2494 reg_types_.Float(),
2495 reg_types_.Float(),
2496 reg_types_.Float(),
2497 false);
jeffhaobdb76512011-09-07 11:43:16 -07002498 break;
2499 case Instruction::ADD_DOUBLE_2ADDR:
2500 case Instruction::SUB_DOUBLE_2ADDR:
2501 case Instruction::MUL_DOUBLE_2ADDR:
2502 case Instruction::DIV_DOUBLE_2ADDR:
2503 case Instruction::REM_DOUBLE_2ADDR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002504 work_line_->CheckBinaryOp2addrWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002505 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2506 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002507 break;
2508 case Instruction::ADD_INT_LIT16:
2509 case Instruction::RSUB_INT:
2510 case Instruction::MUL_INT_LIT16:
2511 case Instruction::DIV_INT_LIT16:
2512 case Instruction::REM_INT_LIT16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002513 work_line_->CheckLiteralOp(inst, reg_types_.Integer(), reg_types_.Integer(), false, true);
jeffhaobdb76512011-09-07 11:43:16 -07002514 break;
2515 case Instruction::AND_INT_LIT16:
2516 case Instruction::OR_INT_LIT16:
2517 case Instruction::XOR_INT_LIT16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002518 work_line_->CheckLiteralOp(inst, reg_types_.Integer(), reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002519 break;
2520 case Instruction::ADD_INT_LIT8:
2521 case Instruction::RSUB_INT_LIT8:
2522 case Instruction::MUL_INT_LIT8:
2523 case Instruction::DIV_INT_LIT8:
2524 case Instruction::REM_INT_LIT8:
2525 case Instruction::SHL_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002526 case Instruction::SHR_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002527 case Instruction::USHR_INT_LIT8:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002528 work_line_->CheckLiteralOp(inst, reg_types_.Integer(), reg_types_.Integer(), false, false);
jeffhaobdb76512011-09-07 11:43:16 -07002529 break;
2530 case Instruction::AND_INT_LIT8:
2531 case Instruction::OR_INT_LIT8:
2532 case Instruction::XOR_INT_LIT8:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002533 work_line_->CheckLiteralOp(inst, reg_types_.Integer(), reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002534 break;
2535
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002536 // Special instructions.
Sebastien Hertzcc10e0e2013-06-28 14:24:48 +02002537 case Instruction::RETURN_VOID_BARRIER:
Ian Rogers9fc16eb2013-07-31 14:49:16 -07002538 if (!IsConstructor() || IsStatic()) {
Sebastien Hertzcc10e0e2013-06-28 14:24:48 +02002539 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void-barrier not expected";
2540 }
2541 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002542 // Note: the following instructions encode offsets derived from class linking.
2543 // As such they use Class*/Field*/AbstractMethod* as these offsets only have
2544 // meaning if the class linking and resolution were successful.
2545 case Instruction::IGET_QUICK:
2546 VerifyIGetQuick(inst, reg_types_.Integer(), true);
2547 break;
2548 case Instruction::IGET_WIDE_QUICK:
2549 VerifyIGetQuick(inst, reg_types_.LongLo(), true);
2550 break;
2551 case Instruction::IGET_OBJECT_QUICK:
2552 VerifyIGetQuick(inst, reg_types_.JavaLangObject(false), false);
2553 break;
2554 case Instruction::IPUT_QUICK:
2555 VerifyIPutQuick(inst, reg_types_.Integer(), true);
2556 break;
2557 case Instruction::IPUT_WIDE_QUICK:
2558 VerifyIPutQuick(inst, reg_types_.LongLo(), true);
2559 break;
2560 case Instruction::IPUT_OBJECT_QUICK:
2561 VerifyIPutQuick(inst, reg_types_.JavaLangObject(false), false);
2562 break;
2563 case Instruction::INVOKE_VIRTUAL_QUICK:
2564 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: {
2565 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
Brian Carlstromea46f952013-07-30 01:26:50 -07002566 mirror::ArtMethod* called_method = VerifyInvokeVirtualQuickArgs(inst, is_range);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002567 if (called_method != NULL) {
2568 const char* descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07002569 const RegType& return_type = reg_types_.FromDescriptor(class_loader_->Get(), descriptor,
Mathieu Chartier590fee92013-09-13 13:46:47 -07002570 false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002571 if (!return_type.IsLowHalf()) {
2572 work_line_->SetResultRegisterType(return_type);
2573 } else {
2574 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2575 }
2576 just_set_result = true;
2577 }
2578 break;
2579 }
2580
Ian Rogersd81871c2011-10-03 13:57:23 -07002581 /* These should never appear during verification. */
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002582 case Instruction::UNUSED_3E:
2583 case Instruction::UNUSED_3F:
2584 case Instruction::UNUSED_40:
2585 case Instruction::UNUSED_41:
2586 case Instruction::UNUSED_42:
2587 case Instruction::UNUSED_43:
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002588 case Instruction::UNUSED_79:
2589 case Instruction::UNUSED_7A:
2590 case Instruction::UNUSED_EB:
2591 case Instruction::UNUSED_EC:
jeffhao9a4f0032012-08-30 16:17:40 -07002592 case Instruction::UNUSED_ED:
jeffhaobdb76512011-09-07 11:43:16 -07002593 case Instruction::UNUSED_EE:
2594 case Instruction::UNUSED_EF:
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002595 case Instruction::UNUSED_F0:
2596 case Instruction::UNUSED_F1:
jeffhaobdb76512011-09-07 11:43:16 -07002597 case Instruction::UNUSED_F2:
2598 case Instruction::UNUSED_F3:
2599 case Instruction::UNUSED_F4:
2600 case Instruction::UNUSED_F5:
2601 case Instruction::UNUSED_F6:
2602 case Instruction::UNUSED_F7:
2603 case Instruction::UNUSED_F8:
2604 case Instruction::UNUSED_F9:
2605 case Instruction::UNUSED_FA:
2606 case Instruction::UNUSED_FB:
jeffhaobdb76512011-09-07 11:43:16 -07002607 case Instruction::UNUSED_FC:
jeffhaobdb76512011-09-07 11:43:16 -07002608 case Instruction::UNUSED_FD:
jeffhaobdb76512011-09-07 11:43:16 -07002609 case Instruction::UNUSED_FE:
jeffhaobdb76512011-09-07 11:43:16 -07002610 case Instruction::UNUSED_FF:
jeffhaod5347e02012-03-22 17:25:05 -07002611 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Unexpected opcode " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07002612 break;
2613
2614 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002615 * DO NOT add a "default" clause here. Without it the compiler will
jeffhaobdb76512011-09-07 11:43:16 -07002616 * complain if an instruction is missing (which is desirable).
2617 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002618 } // end - switch (dec_insn.opcode)
jeffhaobdb76512011-09-07 11:43:16 -07002619
Ian Rogersad0b3a32012-04-16 14:50:24 -07002620 if (have_pending_hard_failure_) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002621 if (Runtime::Current()->IsCompiler()) {
jeffhaob57e9522012-04-26 18:08:21 -07002622 /* When compiling, check that the last failure is a hard failure */
Ian Rogersad0b3a32012-04-16 14:50:24 -07002623 CHECK_EQ(failures_[failures_.size() - 1], VERIFY_ERROR_BAD_CLASS_HARD);
Ian Rogerse1758fe2012-04-19 11:31:15 -07002624 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002625 /* immediate failure, reject class */
2626 info_messages_ << "Rejecting opcode " << inst->DumpString(dex_file_);
2627 return false;
jeffhaofaf459e2012-08-31 15:32:47 -07002628 } else if (have_pending_runtime_throw_failure_) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07002629 /* checking interpreter will throw, mark following code as unreachable */
jeffhaofaf459e2012-08-31 15:32:47 -07002630 opcode_flags = Instruction::kThrow;
jeffhaobdb76512011-09-07 11:43:16 -07002631 }
jeffhaobdb76512011-09-07 11:43:16 -07002632 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002633 * If we didn't just set the result register, clear it out. This ensures that you can only use
2634 * "move-result" immediately after the result is set. (We could check this statically, but it's
2635 * not expensive and it makes our debugging output cleaner.)
jeffhaobdb76512011-09-07 11:43:16 -07002636 */
2637 if (!just_set_result) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002638 work_line_->SetResultTypeToUnknown();
jeffhaobdb76512011-09-07 11:43:16 -07002639 }
2640
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002641
jeffhaobdb76512011-09-07 11:43:16 -07002642
2643 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002644 * Handle "branch". Tag the branch target.
jeffhaobdb76512011-09-07 11:43:16 -07002645 *
2646 * NOTE: instructions like Instruction::EQZ provide information about the
jeffhaod1f0fde2011-09-08 17:25:33 -07002647 * state of the register when the branch is taken or not taken. For example,
jeffhaobdb76512011-09-07 11:43:16 -07002648 * somebody could get a reference field, check it for zero, and if the
2649 * branch is taken immediately store that register in a boolean field
jeffhaod1f0fde2011-09-08 17:25:33 -07002650 * since the value is known to be zero. We do not currently account for
jeffhaobdb76512011-09-07 11:43:16 -07002651 * that, and will reject the code.
2652 *
2653 * TODO: avoid re-fetching the branch target
2654 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002655 if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002656 bool isConditional, selfOkay;
Ian Rogersd81871c2011-10-03 13:57:23 -07002657 if (!GetBranchOffset(work_insn_idx_, &branch_target, &isConditional, &selfOkay)) {
jeffhaobdb76512011-09-07 11:43:16 -07002658 /* should never happen after static verification */
jeffhaod5347e02012-03-22 17:25:05 -07002659 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad branch";
jeffhaobdb76512011-09-07 11:43:16 -07002660 return false;
2661 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08002662 DCHECK_EQ(isConditional, (opcode_flags & Instruction::kContinue) != 0);
jeffhaod5347e02012-03-22 17:25:05 -07002663 if (!CheckNotMoveException(code_item_->insns_, work_insn_idx_ + branch_target)) {
jeffhaobdb76512011-09-07 11:43:16 -07002664 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002665 }
jeffhaobdb76512011-09-07 11:43:16 -07002666 /* update branch target, set "changed" if appropriate */
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002667 if (NULL != branch_line.get()) {
2668 if (!UpdateRegisters(work_insn_idx_ + branch_target, branch_line.get())) {
2669 return false;
2670 }
2671 } else {
2672 if (!UpdateRegisters(work_insn_idx_ + branch_target, work_line_.get())) {
2673 return false;
2674 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002675 }
jeffhaobdb76512011-09-07 11:43:16 -07002676 }
2677
2678 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002679 * Handle "switch". Tag all possible branch targets.
jeffhaobdb76512011-09-07 11:43:16 -07002680 *
2681 * We've already verified that the table is structurally sound, so we
2682 * just need to walk through and tag the targets.
2683 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002684 if ((opcode_flags & Instruction::kSwitch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002685 int offset_to_switch = insns[1] | (((int32_t) insns[2]) << 16);
2686 const uint16_t* switch_insns = insns + offset_to_switch;
2687 int switch_count = switch_insns[1];
2688 int offset_to_targets, targ;
2689
2690 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
2691 /* 0 = sig, 1 = count, 2/3 = first key */
2692 offset_to_targets = 4;
2693 } else {
2694 /* 0 = sig, 1 = count, 2..count * 2 = keys */
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07002695 DCHECK((*insns & 0xff) == Instruction::SPARSE_SWITCH);
jeffhaobdb76512011-09-07 11:43:16 -07002696 offset_to_targets = 2 + 2 * switch_count;
2697 }
2698
2699 /* verify each switch target */
2700 for (targ = 0; targ < switch_count; targ++) {
2701 int offset;
2702 uint32_t abs_offset;
2703
2704 /* offsets are 32-bit, and only partly endian-swapped */
2705 offset = switch_insns[offset_to_targets + targ * 2] |
2706 (((int32_t) switch_insns[offset_to_targets + targ * 2 + 1]) << 16);
Ian Rogersd81871c2011-10-03 13:57:23 -07002707 abs_offset = work_insn_idx_ + offset;
2708 DCHECK_LT(abs_offset, code_item_->insns_size_in_code_units_);
jeffhaod5347e02012-03-22 17:25:05 -07002709 if (!CheckNotMoveException(code_item_->insns_, abs_offset)) {
jeffhaobdb76512011-09-07 11:43:16 -07002710 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002711 }
2712 if (!UpdateRegisters(abs_offset, work_line_.get()))
jeffhaobdb76512011-09-07 11:43:16 -07002713 return false;
2714 }
2715 }
2716
2717 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002718 * Handle instructions that can throw and that are sitting in a "try" block. (If they're not in a
2719 * "try" block when they throw, control transfers out of the method.)
jeffhaobdb76512011-09-07 11:43:16 -07002720 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002721 if ((opcode_flags & Instruction::kThrow) != 0 && insn_flags_[work_insn_idx_].IsInTry()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002722 bool within_catch_all = false;
Ian Rogers0571d352011-11-03 19:51:38 -07002723 CatchHandlerIterator iterator(*code_item_, work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07002724
Ian Rogers0571d352011-11-03 19:51:38 -07002725 for (; iterator.HasNext(); iterator.Next()) {
2726 if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002727 within_catch_all = true;
2728 }
jeffhaobdb76512011-09-07 11:43:16 -07002729 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002730 * Merge registers into the "catch" block. We want to use the "savedRegs" rather than
2731 * "work_regs", because at runtime the exception will be thrown before the instruction
2732 * modifies any registers.
jeffhaobdb76512011-09-07 11:43:16 -07002733 */
Ian Rogers0571d352011-11-03 19:51:38 -07002734 if (!UpdateRegisters(iterator.GetHandlerAddress(), saved_line_.get())) {
jeffhaobdb76512011-09-07 11:43:16 -07002735 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002736 }
jeffhaobdb76512011-09-07 11:43:16 -07002737 }
2738
2739 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002740 * If the monitor stack depth is nonzero, there must be a "catch all" handler for this
2741 * instruction. This does apply to monitor-exit because of async exception handling.
jeffhaobdb76512011-09-07 11:43:16 -07002742 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002743 if (work_line_->MonitorStackDepth() > 0 && !within_catch_all) {
jeffhaobdb76512011-09-07 11:43:16 -07002744 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002745 * The state in work_line reflects the post-execution state. If the current instruction is a
2746 * monitor-enter and the monitor stack was empty, we don't need a catch-all (if it throws,
jeffhaobdb76512011-09-07 11:43:16 -07002747 * it will do so before grabbing the lock).
2748 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002749 if (inst->Opcode() != Instruction::MONITOR_ENTER || work_line_->MonitorStackDepth() != 1) {
jeffhaod5347e02012-03-22 17:25:05 -07002750 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
Ian Rogersd81871c2011-10-03 13:57:23 -07002751 << "expected to be within a catch-all for an instruction where a monitor is held";
jeffhaobdb76512011-09-07 11:43:16 -07002752 return false;
2753 }
2754 }
2755 }
2756
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002757 /* Handle "continue". Tag the next consecutive instruction.
2758 * Note: Keep the code handling "continue" case below the "branch" and "switch" cases,
2759 * because it changes work_line_ when performing peephole optimization
2760 * and this change should not be used in those cases.
2761 */
Ian Rogers6d376ae2013-07-23 15:12:40 -07002762 if ((opcode_flags & Instruction::kContinue) != 0) {
2763 uint32_t next_insn_idx = work_insn_idx_ + CurrentInsnFlags()->GetLengthInCodeUnits();
2764 if (next_insn_idx >= code_item_->insns_size_in_code_units_) {
2765 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Execution can walk off end of code area";
2766 return false;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002767 }
Ian Rogers6d376ae2013-07-23 15:12:40 -07002768 // The only way to get to a move-exception instruction is to get thrown there. Make sure the
2769 // next instruction isn't one.
2770 if (!CheckNotMoveException(code_item_->insns_, next_insn_idx)) {
2771 return false;
2772 }
2773 if (NULL != fallthrough_line.get()) {
2774 // Make workline consistent with fallthrough computed from peephole optimization.
2775 work_line_->CopyFromLine(fallthrough_line.get());
2776 }
Ian Rogersb8c78592013-07-25 23:52:52 +00002777 if (insn_flags_[next_insn_idx].IsReturn()) {
2778 // For returns we only care about the operand to the return, all other registers are dead.
2779 const Instruction* ret_inst = Instruction::At(code_item_->insns_ + next_insn_idx);
2780 Instruction::Code opcode = ret_inst->Opcode();
2781 if ((opcode == Instruction::RETURN_VOID) || (opcode == Instruction::RETURN_VOID_BARRIER)) {
2782 work_line_->MarkAllRegistersAsConflicts();
2783 } else {
2784 if (opcode == Instruction::RETURN_WIDE) {
2785 work_line_->MarkAllRegistersAsConflictsExceptWide(ret_inst->VRegA_11x());
2786 } else {
2787 work_line_->MarkAllRegistersAsConflictsExcept(ret_inst->VRegA_11x());
2788 }
2789 }
2790 }
Ian Rogers6d376ae2013-07-23 15:12:40 -07002791 RegisterLine* next_line = reg_table_.GetLine(next_insn_idx);
2792 if (next_line != NULL) {
2793 // Merge registers into what we have for the next instruction,
2794 // and set the "changed" flag if needed.
2795 if (!UpdateRegisters(next_insn_idx, work_line_.get())) {
2796 return false;
2797 }
2798 } else {
2799 /*
2800 * We're not recording register data for the next instruction, so we don't know what the
2801 * prior state was. We have to assume that something has changed and re-evaluate it.
2802 */
2803 insn_flags_[next_insn_idx].SetChanged();
2804 }
2805 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002806
jeffhaod1f0fde2011-09-08 17:25:33 -07002807 /* If we're returning from the method, make sure monitor stack is empty. */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002808 if ((opcode_flags & Instruction::kReturn) != 0) {
Elliott Hughesb25c3f62012-03-26 16:35:06 -07002809 if (!work_line_->VerifyMonitorStackEmpty()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002810 return false;
2811 }
jeffhaobdb76512011-09-07 11:43:16 -07002812 }
2813
2814 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002815 * Update start_guess. Advance to the next instruction of that's
2816 * possible, otherwise use the branch target if one was found. If
jeffhaobdb76512011-09-07 11:43:16 -07002817 * neither of those exists we're in a return or throw; leave start_guess
2818 * alone and let the caller sort it out.
2819 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002820 if ((opcode_flags & Instruction::kContinue) != 0) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002821 *start_guess = work_insn_idx_ + insn_flags_[work_insn_idx_].GetLengthInCodeUnits();
Elliott Hughesadb8c672012-03-06 16:49:32 -08002822 } else if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002823 /* we're still okay if branch_target is zero */
Ian Rogersd81871c2011-10-03 13:57:23 -07002824 *start_guess = work_insn_idx_ + branch_target;
jeffhaobdb76512011-09-07 11:43:16 -07002825 }
2826
Ian Rogersd81871c2011-10-03 13:57:23 -07002827 DCHECK_LT(*start_guess, code_item_->insns_size_in_code_units_);
2828 DCHECK(insn_flags_[*start_guess].IsOpcode());
jeffhaobdb76512011-09-07 11:43:16 -07002829
2830 return true;
Brian Carlstrom7934ac22013-07-26 10:54:15 -07002831} // NOLINT(readability/fn_size)
jeffhaobdb76512011-09-07 11:43:16 -07002832
Ian Rogers776ac1f2012-04-13 23:36:36 -07002833const RegType& MethodVerifier::ResolveClassAndCheckAccess(uint32_t class_idx) {
Ian Rogers0571d352011-11-03 19:51:38 -07002834 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002835 const RegType& referrer = GetDeclaringClass();
Mathieu Chartier590fee92013-09-13 13:46:47 -07002836 mirror::Class* klass = (*dex_cache_)->GetResolvedType(class_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002837 const RegType& result =
Ian Rogers04f94f42013-06-10 15:09:26 -07002838 klass != NULL ? reg_types_.FromClass(descriptor, klass,
2839 klass->CannotBeAssignedFromOtherTypes())
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07002840 : reg_types_.FromDescriptor(class_loader_->Get(), descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002841 if (result.IsConflict()) {
2842 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "accessing broken descriptor '" << descriptor
2843 << "' in " << referrer;
2844 return result;
2845 }
Ian Rogerse1758fe2012-04-19 11:31:15 -07002846 if (klass == NULL && !result.IsUnresolvedTypes()) {
Mathieu Chartier590fee92013-09-13 13:46:47 -07002847 (*dex_cache_)->SetResolvedType(class_idx, result.GetClass());
Ian Rogerse1758fe2012-04-19 11:31:15 -07002848 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002849 // Check if access is allowed. Unresolved types use xxxWithAccessCheck to
Jeff Haob24b4a72013-07-31 13:47:31 -07002850 // check at runtime if access is allowed and so pass here. If result is
2851 // primitive, skip the access check.
2852 if (result.IsNonZeroReferenceTypes() && !result.IsUnresolvedTypes() &&
2853 !referrer.IsUnresolvedTypes() && !referrer.CanAccess(result)) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002854 Fail(VERIFY_ERROR_ACCESS_CLASS) << "illegal class access: '"
Ian Rogersad0b3a32012-04-16 14:50:24 -07002855 << referrer << "' -> '" << result << "'";
Ian Rogers28ad40d2011-10-27 15:19:26 -07002856 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002857 return result;
Ian Rogersd81871c2011-10-03 13:57:23 -07002858}
2859
Ian Rogers776ac1f2012-04-13 23:36:36 -07002860const RegType& MethodVerifier::GetCaughtExceptionType() {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002861 const RegType* common_super = NULL;
Ian Rogersd81871c2011-10-03 13:57:23 -07002862 if (code_item_->tries_size_ != 0) {
Ian Rogers0571d352011-11-03 19:51:38 -07002863 const byte* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
Ian Rogersd81871c2011-10-03 13:57:23 -07002864 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
2865 for (uint32_t i = 0; i < handlers_size; i++) {
Ian Rogers0571d352011-11-03 19:51:38 -07002866 CatchHandlerIterator iterator(handlers_ptr);
2867 for (; iterator.HasNext(); iterator.Next()) {
2868 if (iterator.GetHandlerAddress() == (uint32_t) work_insn_idx_) {
2869 if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) {
Ian Rogersb4903572012-10-11 11:52:56 -07002870 common_super = &reg_types_.JavaLangThrowable(false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002871 } else {
Ian Rogers0571d352011-11-03 19:51:38 -07002872 const RegType& exception = ResolveClassAndCheckAccess(iterator.GetHandlerTypeIndex());
Jeff Haob878f212014-04-24 16:25:36 -07002873 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 }
Jeff Haob878f212014-04-24 16:25:36 -07002882 } else if (common_super == nullptr) {
2883 common_super = &exception;
Ian Rogers28ad40d2011-10-27 15:19:26 -07002884 } else if (common_super->Equals(exception)) {
Ian Rogersc4762272012-02-01 15:55:55 -08002885 // odd case, but nothing to do
Ian Rogersd81871c2011-10-03 13:57:23 -07002886 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002887 common_super = &common_super->Merge(exception, &reg_types_);
Ian Rogersb4903572012-10-11 11:52:56 -07002888 CHECK(reg_types_.JavaLangThrowable(false).IsAssignableFrom(*common_super));
Ian Rogersd81871c2011-10-03 13:57:23 -07002889 }
2890 }
2891 }
2892 }
Ian Rogers0571d352011-11-03 19:51:38 -07002893 handlers_ptr = iterator.EndDataPointer();
Ian Rogersd81871c2011-10-03 13:57:23 -07002894 }
2895 }
2896 if (common_super == NULL) {
2897 /* no catch blocks, or no catches with classes we can find */
jeffhaod5347e02012-03-22 17:25:05 -07002898 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unable to find exception handler";
Ian Rogersad0b3a32012-04-16 14:50:24 -07002899 return reg_types_.Conflict();
Ian Rogersd81871c2011-10-03 13:57:23 -07002900 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002901 return *common_super;
Ian Rogersd81871c2011-10-03 13:57:23 -07002902}
2903
Brian Carlstromea46f952013-07-30 01:26:50 -07002904mirror::ArtMethod* MethodVerifier::ResolveMethodAndCheckAccess(uint32_t dex_method_idx,
Ian Rogersd91d6d62013-09-25 20:26:14 -07002905 MethodType method_type) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002906 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx);
Ian Rogers90040192011-12-16 08:54:29 -08002907 const RegType& klass_type = ResolveClassAndCheckAccess(method_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002908 if (klass_type.IsConflict()) {
2909 std::string append(" in attempt to access method ");
2910 append += dex_file_->GetMethodName(method_id);
2911 AppendToLastFailMessage(append);
Ian Rogers90040192011-12-16 08:54:29 -08002912 return NULL;
2913 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002914 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers90040192011-12-16 08:54:29 -08002915 return NULL; // Can't resolve Class so no more to do here
2916 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002917 mirror::Class* klass = klass_type.GetClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002918 const RegType& referrer = GetDeclaringClass();
Mathieu Chartier590fee92013-09-13 13:46:47 -07002919 mirror::ArtMethod* res_method = (*dex_cache_)->GetResolvedMethod(dex_method_idx);
Ian Rogersd81871c2011-10-03 13:57:23 -07002920 if (res_method == NULL) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07002921 const char* name = dex_file_->GetMethodName(method_id);
Ian Rogersd91d6d62013-09-25 20:26:14 -07002922 const Signature signature = dex_file_->GetMethodSignature(method_id);
jeffhao8cd6dda2012-02-22 10:15:34 -08002923
2924 if (method_type == METHOD_DIRECT || method_type == METHOD_STATIC) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002925 res_method = klass->FindDirectMethod(name, signature);
jeffhao8cd6dda2012-02-22 10:15:34 -08002926 } else if (method_type == METHOD_INTERFACE) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002927 res_method = klass->FindInterfaceMethod(name, signature);
2928 } else {
2929 res_method = klass->FindVirtualMethod(name, signature);
2930 }
2931 if (res_method != NULL) {
Mathieu Chartier590fee92013-09-13 13:46:47 -07002932 (*dex_cache_)->SetResolvedMethod(dex_method_idx, res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07002933 } else {
jeffhao8cd6dda2012-02-22 10:15:34 -08002934 // If a virtual or interface method wasn't found with the expected type, look in
2935 // the direct methods. This can happen when the wrong invoke type is used or when
2936 // a class has changed, and will be flagged as an error in later checks.
2937 if (method_type == METHOD_INTERFACE || method_type == METHOD_VIRTUAL) {
2938 res_method = klass->FindDirectMethod(name, signature);
2939 }
2940 if (res_method == NULL) {
2941 Fail(VERIFY_ERROR_NO_METHOD) << "couldn't find method "
2942 << PrettyDescriptor(klass) << "." << name
2943 << " " << signature;
2944 return NULL;
2945 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002946 }
2947 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002948 // Make sure calls to constructors are "direct". There are additional restrictions but we don't
2949 // enforce them here.
2950 if (res_method->IsConstructor() && method_type != METHOD_DIRECT) {
jeffhaod5347e02012-03-22 17:25:05 -07002951 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting non-direct call to constructor "
2952 << PrettyMethod(res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07002953 return NULL;
2954 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002955 // Disallow any calls to class initializers.
2956 if (MethodHelper(res_method).IsClassInitializer()) {
jeffhaod5347e02012-03-22 17:25:05 -07002957 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting call to class initializer "
2958 << PrettyMethod(res_method);
jeffhao8cd6dda2012-02-22 10:15:34 -08002959 return NULL;
2960 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002961 // Check if access is allowed.
Ian Rogersad0b3a32012-04-16 14:50:24 -07002962 if (!referrer.CanAccessMember(res_method->GetDeclaringClass(), res_method->GetAccessFlags())) {
jeffhao8cd6dda2012-02-22 10:15:34 -08002963 Fail(VERIFY_ERROR_ACCESS_METHOD) << "illegal method access (call " << PrettyMethod(res_method)
Ian Rogersad0b3a32012-04-16 14:50:24 -07002964 << " from " << referrer << ")";
jeffhaob57e9522012-04-26 18:08:21 -07002965 return res_method;
jeffhao8cd6dda2012-02-22 10:15:34 -08002966 }
jeffhaode0d9c92012-02-27 13:58:13 -08002967 // Check that invoke-virtual and invoke-super are not used on private methods of the same class.
2968 if (res_method->IsPrivate() && method_type == METHOD_VIRTUAL) {
jeffhaod5347e02012-03-22 17:25:05 -07002969 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invoke-super/virtual can't be used on private method "
2970 << PrettyMethod(res_method);
jeffhaode0d9c92012-02-27 13:58:13 -08002971 return NULL;
2972 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002973 // Check that interface methods match interface classes.
2974 if (klass->IsInterface() && method_type != METHOD_INTERFACE) {
2975 Fail(VERIFY_ERROR_CLASS_CHANGE) << "non-interface method " << PrettyMethod(res_method)
2976 << " is in an interface class " << PrettyClass(klass);
2977 return NULL;
2978 } else if (!klass->IsInterface() && method_type == METHOD_INTERFACE) {
2979 Fail(VERIFY_ERROR_CLASS_CHANGE) << "interface method " << PrettyMethod(res_method)
2980 << " is in a non-interface class " << PrettyClass(klass);
2981 return NULL;
2982 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002983 // See if the method type implied by the invoke instruction matches the access flags for the
2984 // target method.
2985 if ((method_type == METHOD_DIRECT && !res_method->IsDirect()) ||
2986 (method_type == METHOD_STATIC && !res_method->IsStatic()) ||
2987 ((method_type == METHOD_VIRTUAL || method_type == METHOD_INTERFACE) && res_method->IsDirect())
2988 ) {
Ian Rogers2fc14272012-08-30 10:56:57 -07002989 Fail(VERIFY_ERROR_CLASS_CHANGE) << "invoke type (" << method_type << ") does not match method "
2990 " type of " << PrettyMethod(res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07002991 return NULL;
2992 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002993 return res_method;
2994}
2995
Brian Carlstromea46f952013-07-30 01:26:50 -07002996mirror::ArtMethod* MethodVerifier::VerifyInvocationArgs(const Instruction* inst,
Sebastien Hertz5243e912013-05-21 10:55:07 +02002997 MethodType method_type,
2998 bool is_range,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002999 bool is_super) {
jeffhao8cd6dda2012-02-22 10:15:34 -08003000 // Resolve the method. This could be an abstract or concrete method depending on what sort of call
3001 // we're making.
Sebastien Hertz5243e912013-05-21 10:55:07 +02003002 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Brian Carlstromea46f952013-07-30 01:26:50 -07003003 mirror::ArtMethod* res_method = ResolveMethodAndCheckAccess(method_idx, method_type);
jeffhao8cd6dda2012-02-22 10:15:34 -08003004 if (res_method == NULL) { // error or class is unresolved
3005 return NULL;
3006 }
3007
Ian Rogersd81871c2011-10-03 13:57:23 -07003008 // If we're using invoke-super(method), make sure that the executing method's class' superclass
3009 // has a vtable entry for the target method.
3010 if (is_super) {
3011 DCHECK(method_type == METHOD_VIRTUAL);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003012 const RegType& super = GetDeclaringClass().GetSuperClass(&reg_types_);
Ian Rogers529781d2012-07-23 17:24:29 -07003013 if (super.IsUnresolvedTypes()) {
jeffhao4d8df822012-04-24 17:09:36 -07003014 Fail(VERIFY_ERROR_NO_METHOD) << "unknown super class in invoke-super from "
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003015 << PrettyMethod(dex_method_idx_, *dex_file_)
jeffhao4d8df822012-04-24 17:09:36 -07003016 << " to super " << PrettyMethod(res_method);
3017 return NULL;
3018 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003019 mirror::Class* super_klass = super.GetClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07003020 if (res_method->GetMethodIndex() >= super_klass->GetVTable()->GetLength()) {
jeffhao4d8df822012-04-24 17:09:36 -07003021 MethodHelper mh(res_method);
3022 Fail(VERIFY_ERROR_NO_METHOD) << "invalid invoke-super from "
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003023 << PrettyMethod(dex_method_idx_, *dex_file_)
jeffhao4d8df822012-04-24 17:09:36 -07003024 << " to super " << super
3025 << "." << mh.GetName()
3026 << mh.GetSignature();
Ian Rogersd81871c2011-10-03 13:57:23 -07003027 return NULL;
3028 }
3029 }
3030 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003031 // match the call to the signature. Also, we might be calling through an abstract method
Ian Rogersd81871c2011-10-03 13:57:23 -07003032 // definition (which doesn't have register count values).
Sebastien Hertz5243e912013-05-21 10:55:07 +02003033 const size_t expected_args = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
Ian Rogersd81871c2011-10-03 13:57:23 -07003034 /* caught by static verifier */
3035 DCHECK(is_range || expected_args <= 5);
3036 if (expected_args > code_item_->outs_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07003037 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
Ian Rogersd81871c2011-10-03 13:57:23 -07003038 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
3039 return NULL;
3040 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003041
jeffhaobdb76512011-09-07 11:43:16 -07003042 /*
Ian Rogersad0b3a32012-04-16 14:50:24 -07003043 * Check the "this" argument, which must be an instance of the class that declared the method.
3044 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
3045 * rigorous check here (which is okay since we have to do it at runtime).
jeffhaobdb76512011-09-07 11:43:16 -07003046 */
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003047 size_t actual_args = 0;
Ian Rogersd81871c2011-10-03 13:57:23 -07003048 if (!res_method->IsStatic()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003049 const RegType& actual_arg_type = work_line_->GetInvocationThis(inst, is_range);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003050 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
Ian Rogersd81871c2011-10-03 13:57:23 -07003051 return NULL;
3052 }
3053 if (actual_arg_type.IsUninitializedReference() && !res_method->IsConstructor()) {
jeffhaod5347e02012-03-22 17:25:05 -07003054 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
Ian Rogersd81871c2011-10-03 13:57:23 -07003055 return NULL;
3056 }
3057 if (method_type != METHOD_INTERFACE && !actual_arg_type.IsZero()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003058 mirror::Class* klass = res_method->GetDeclaringClass();
Ian Rogers04f94f42013-06-10 15:09:26 -07003059 const RegType& res_method_class =
3060 reg_types_.FromClass(ClassHelper(klass).GetDescriptor(), klass,
3061 klass->CannotBeAssignedFromOtherTypes());
Ian Rogers9074b992011-10-26 17:41:55 -07003062 if (!res_method_class.IsAssignableFrom(actual_arg_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07003063 Fail(actual_arg_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS:
3064 VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003065 << "' not instance of '" << res_method_class << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07003066 return NULL;
3067 }
3068 }
3069 actual_args++;
3070 }
3071 /*
3072 * Process the target method's signature. This signature may or may not
3073 * have been verified, so we can't assume it's properly formed.
3074 */
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003075 MethodHelper mh(res_method);
3076 const DexFile::TypeList* params = mh.GetParameterTypeList();
3077 size_t params_size = params == NULL ? 0 : params->Size();
Sebastien Hertz5243e912013-05-21 10:55:07 +02003078 uint32_t arg[5];
3079 if (!is_range) {
Ian Rogers29a26482014-05-02 15:27:29 -07003080 inst->GetVarArgs(arg);
Sebastien Hertz5243e912013-05-21 10:55:07 +02003081 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003082 for (size_t param_index = 0; param_index < params_size; param_index++) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003083 if (actual_args >= expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07003084 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invalid call to '" << PrettyMethod(res_method)
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003085 << "'. Expected " << expected_args << " arguments, processing argument " << actual_args
3086 << " (where longs/doubles count twice).";
Ian Rogersd81871c2011-10-03 13:57:23 -07003087 return NULL;
3088 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003089 const char* descriptor =
3090 mh.GetTypeDescriptorFromTypeIdx(params->GetTypeItem(param_index).type_idx_);
3091 if (descriptor == NULL) {
jeffhaod5347e02012-03-22 17:25:05 -07003092 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003093 << " missing signature component";
3094 return NULL;
Ian Rogersd81871c2011-10-03 13:57:23 -07003095 }
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003096 const RegType& reg_type = reg_types_.FromDescriptor(class_loader_->Get(), descriptor, false);
Sebastien Hertz5243e912013-05-21 10:55:07 +02003097 uint32_t get_reg = is_range ? inst->VRegC_3rc() + actual_args : arg[actual_args];
Jeff Haoa6b22c52013-10-04 14:33:22 -07003098 if (reg_type.IsIntegralTypes()) {
3099 const RegType& src_type = work_line_->GetRegisterType(get_reg);
3100 if (!src_type.IsIntegralTypes()) {
3101 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register v" << get_reg << " has type " << src_type
3102 << " but expected " << reg_type;
3103 return res_method;
3104 }
3105 } else if (!work_line_->VerifyRegisterType(get_reg, reg_type)) {
jeffhaob57e9522012-04-26 18:08:21 -07003106 return res_method;
Ian Rogersd81871c2011-10-03 13:57:23 -07003107 }
3108 actual_args = reg_type.IsLongOrDoubleTypes() ? actual_args + 2 : actual_args + 1;
3109 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003110 if (actual_args != expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07003111 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003112 << " expected " << expected_args << " arguments, found " << actual_args;
Ian Rogersd81871c2011-10-03 13:57:23 -07003113 return NULL;
3114 } else {
3115 return res_method;
3116 }
3117}
3118
Brian Carlstromea46f952013-07-30 01:26:50 -07003119mirror::ArtMethod* MethodVerifier::GetQuickInvokedMethod(const Instruction* inst,
Mathieu Chartier590fee92013-09-13 13:46:47 -07003120 RegisterLine* reg_line, bool is_range) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003121 DCHECK(inst->Opcode() == Instruction::INVOKE_VIRTUAL_QUICK ||
3122 inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
3123 const RegType& actual_arg_type = reg_line->GetInvocationThis(inst, is_range);
Ian Rogers9bc54402014-04-17 16:40:01 -07003124 if (!actual_arg_type.HasClass()) {
3125 VLOG(verifier) << "Failed to get mirror::Class* from '" << actual_arg_type << "'";
Andreas Gampe63981562014-04-17 12:28:43 -07003126 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003127 }
Ian Rogersa4cf1df2014-05-07 19:47:17 -07003128 mirror::ObjectArray<mirror::ArtMethod>* vtable = nullptr;
3129 mirror::Class* klass = actual_arg_type.GetClass();
3130 if (klass->IsInterface()) {
3131 // Derive Object.class from Class.class.getSuperclass().
3132 mirror::Class* object_klass = klass->GetClass()->GetSuperClass();
3133 CHECK(object_klass->IsObjectClass());
3134 vtable = object_klass->GetVTable();
3135 } else {
3136 vtable = klass->GetVTable();
3137 }
3138 CHECK(vtable != nullptr) << PrettyDescriptor(klass);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003139 uint16_t vtable_index = is_range ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersa4cf1df2014-05-07 19:47:17 -07003140 CHECK_LT(static_cast<int32_t>(vtable_index), vtable->GetLength()) << PrettyDescriptor(klass);
Brian Carlstromea46f952013-07-30 01:26:50 -07003141 mirror::ArtMethod* res_method = vtable->Get(vtable_index);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003142 CHECK(!Thread::Current()->IsExceptionPending());
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003143 return res_method;
3144}
3145
Brian Carlstromea46f952013-07-30 01:26:50 -07003146mirror::ArtMethod* MethodVerifier::VerifyInvokeVirtualQuickArgs(const Instruction* inst,
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003147 bool is_range) {
3148 DCHECK(Runtime::Current()->IsStarted());
Brian Carlstromea46f952013-07-30 01:26:50 -07003149 mirror::ArtMethod* res_method = GetQuickInvokedMethod(inst, work_line_.get(),
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003150 is_range);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003151 if (res_method == NULL) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003152 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer method from " << inst->Name();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003153 return NULL;
3154 }
3155 CHECK(!res_method->IsDirect() && !res_method->IsStatic());
3156
3157 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
3158 // match the call to the signature. Also, we might be calling through an abstract method
3159 // definition (which doesn't have register count values).
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003160 const RegType& actual_arg_type = work_line_->GetInvocationThis(inst, is_range);
3161 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
3162 return NULL;
3163 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003164 const size_t expected_args = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
3165 /* caught by static verifier */
3166 DCHECK(is_range || expected_args <= 5);
3167 if (expected_args > code_item_->outs_size_) {
3168 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
3169 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
3170 return NULL;
3171 }
3172
3173 /*
3174 * Check the "this" argument, which must be an instance of the class that declared the method.
3175 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
3176 * rigorous check here (which is okay since we have to do it at runtime).
3177 */
3178 if (actual_arg_type.IsUninitializedReference() && !res_method->IsConstructor()) {
3179 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
3180 return NULL;
3181 }
3182 if (!actual_arg_type.IsZero()) {
3183 mirror::Class* klass = res_method->GetDeclaringClass();
3184 const RegType& res_method_class =
3185 reg_types_.FromClass(ClassHelper(klass).GetDescriptor(), klass,
3186 klass->CannotBeAssignedFromOtherTypes());
3187 if (!res_method_class.IsAssignableFrom(actual_arg_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07003188 Fail(actual_arg_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS :
3189 VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003190 << "' not instance of '" << res_method_class << "'";
3191 return NULL;
3192 }
3193 }
3194 /*
3195 * Process the target method's signature. This signature may or may not
3196 * have been verified, so we can't assume it's properly formed.
3197 */
3198 MethodHelper mh(res_method);
3199 const DexFile::TypeList* params = mh.GetParameterTypeList();
3200 size_t params_size = params == NULL ? 0 : params->Size();
3201 uint32_t arg[5];
3202 if (!is_range) {
Ian Rogers29a26482014-05-02 15:27:29 -07003203 inst->GetVarArgs(arg);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003204 }
3205 size_t actual_args = 1;
3206 for (size_t param_index = 0; param_index < params_size; param_index++) {
3207 if (actual_args >= expected_args) {
3208 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invalid call to '" << PrettyMethod(res_method)
Brian Carlstrom93c33962013-07-26 10:37:43 -07003209 << "'. Expected " << expected_args
3210 << " arguments, processing argument " << actual_args
3211 << " (where longs/doubles count twice).";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003212 return NULL;
3213 }
3214 const char* descriptor =
3215 mh.GetTypeDescriptorFromTypeIdx(params->GetTypeItem(param_index).type_idx_);
3216 if (descriptor == NULL) {
3217 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003218 << " missing signature component";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003219 return NULL;
3220 }
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003221 const RegType& reg_type = reg_types_.FromDescriptor(class_loader_->Get(), descriptor, false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003222 uint32_t get_reg = is_range ? inst->VRegC_3rc() + actual_args : arg[actual_args];
3223 if (!work_line_->VerifyRegisterType(get_reg, reg_type)) {
3224 return res_method;
3225 }
3226 actual_args = reg_type.IsLongOrDoubleTypes() ? actual_args + 2 : actual_args + 1;
3227 }
3228 if (actual_args != expected_args) {
3229 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
3230 << " expected " << expected_args << " arguments, found " << actual_args;
3231 return NULL;
3232 } else {
3233 return res_method;
3234 }
3235}
3236
Ian Rogers62342ec2013-06-11 10:26:37 -07003237void MethodVerifier::VerifyNewArray(const Instruction* inst, bool is_filled, bool is_range) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003238 uint32_t type_idx;
3239 if (!is_filled) {
3240 DCHECK_EQ(inst->Opcode(), Instruction::NEW_ARRAY);
3241 type_idx = inst->VRegC_22c();
3242 } else if (!is_range) {
3243 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY);
3244 type_idx = inst->VRegB_35c();
3245 } else {
3246 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY_RANGE);
3247 type_idx = inst->VRegB_3rc();
3248 }
3249 const RegType& res_type = ResolveClassAndCheckAccess(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003250 if (res_type.IsConflict()) { // bad class
3251 DCHECK_NE(failures_.size(), 0U);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003252 } else {
3253 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
3254 if (!res_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003255 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "new-array on non-array class " << res_type;
Ian Rogers0c4a5062012-02-03 15:18:59 -08003256 } else if (!is_filled) {
3257 /* make sure "size" register is valid type */
Sebastien Hertz5243e912013-05-21 10:55:07 +02003258 work_line_->VerifyRegisterType(inst->VRegB_22c(), reg_types_.Integer());
Ian Rogers0c4a5062012-02-03 15:18:59 -08003259 /* set register type to array class */
Ian Rogers62342ec2013-06-11 10:26:37 -07003260 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
3261 work_line_->SetRegisterType(inst->VRegA_22c(), precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003262 } else {
3263 // Verify each register. If "arg_count" is bad, VerifyRegisterType() will run off the end of
3264 // the list and fail. It's legal, if silly, for arg_count to be zero.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003265 const RegType& expected_type = reg_types_.GetComponentType(res_type, class_loader_->Get());
Sebastien Hertz5243e912013-05-21 10:55:07 +02003266 uint32_t arg_count = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
3267 uint32_t arg[5];
3268 if (!is_range) {
Ian Rogers29a26482014-05-02 15:27:29 -07003269 inst->GetVarArgs(arg);
Sebastien Hertz5243e912013-05-21 10:55:07 +02003270 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08003271 for (size_t ui = 0; ui < arg_count; ui++) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003272 uint32_t get_reg = is_range ? inst->VRegC_3rc() + ui : arg[ui];
Ian Rogers0c4a5062012-02-03 15:18:59 -08003273 if (!work_line_->VerifyRegisterType(get_reg, expected_type)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003274 work_line_->SetResultRegisterType(reg_types_.Conflict());
Ian Rogers0c4a5062012-02-03 15:18:59 -08003275 return;
3276 }
3277 }
3278 // filled-array result goes into "result" register
Ian Rogers62342ec2013-06-11 10:26:37 -07003279 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
3280 work_line_->SetResultRegisterType(precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003281 }
3282 }
3283}
3284
Sebastien Hertz5243e912013-05-21 10:55:07 +02003285void MethodVerifier::VerifyAGet(const Instruction* inst,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003286 const RegType& insn_type, bool is_primitive) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003287 const RegType& index_type = work_line_->GetRegisterType(inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07003288 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003289 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07003290 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003291 const RegType& array_type = work_line_->GetRegisterType(inst->VRegB_23x());
Ian Rogers89310de2012-02-01 13:47:30 -08003292 if (array_type.IsZero()) {
3293 // Null array class; this code path will fail at runtime. Infer a merge-able type from the
3294 // instruction type. TODO: have a proper notion of bottom here.
3295 if (!is_primitive || insn_type.IsCategory1Types()) {
3296 // Reference or category 1
Sebastien Hertz5243e912013-05-21 10:55:07 +02003297 work_line_->SetRegisterType(inst->VRegA_23x(), reg_types_.Zero());
Ian Rogersd81871c2011-10-03 13:57:23 -07003298 } else {
Ian Rogers89310de2012-02-01 13:47:30 -08003299 // Category 2
Sebastien Hertz5243e912013-05-21 10:55:07 +02003300 work_line_->SetRegisterTypeWide(inst->VRegA_23x(), reg_types_.FromCat2ConstLo(0, false),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003301 reg_types_.FromCat2ConstHi(0, false));
Ian Rogers89310de2012-02-01 13:47:30 -08003302 }
jeffhaofc3144e2012-02-01 17:21:15 -08003303 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003304 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aget";
Ian Rogers89310de2012-02-01 13:47:30 -08003305 } else {
3306 /* verify the class */
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003307 const RegType& component_type = reg_types_.GetComponentType(array_type, class_loader_->Get());
jeffhaofc3144e2012-02-01 17:21:15 -08003308 if (!component_type.IsReferenceTypes() && !is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07003309 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08003310 << " source for aget-object";
3311 } else if (component_type.IsNonZeroReferenceTypes() && is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07003312 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08003313 << " source for category 1 aget";
3314 } else if (is_primitive && !insn_type.Equals(component_type) &&
3315 !((insn_type.IsInteger() && component_type.IsFloat()) ||
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003316 (insn_type.IsLong() && component_type.IsDouble()))) {
3317 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array type " << array_type
3318 << " incompatible with aget of type " << insn_type;
Ian Rogers89310de2012-02-01 13:47:30 -08003319 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003320 // Use knowledge of the field type which is stronger than the type inferred from the
3321 // instruction, which can't differentiate object types and ints from floats, longs from
3322 // doubles.
3323 if (!component_type.IsLowHalf()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003324 work_line_->SetRegisterType(inst->VRegA_23x(), component_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003325 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003326 work_line_->SetRegisterTypeWide(inst->VRegA_23x(), component_type,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003327 component_type.HighHalf(&reg_types_));
3328 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003329 }
3330 }
3331 }
3332}
3333
Jeff Haofe1f7c82013-08-01 14:50:24 -07003334void MethodVerifier::VerifyPrimitivePut(const RegType& target_type, const RegType& insn_type,
3335 const uint32_t vregA) {
3336 // Primitive assignability rules are weaker than regular assignability rules.
3337 bool instruction_compatible;
3338 bool value_compatible;
3339 const RegType& value_type = work_line_->GetRegisterType(vregA);
3340 if (target_type.IsIntegralTypes()) {
Jeff Haoa4647482013-08-06 15:35:47 -07003341 instruction_compatible = target_type.Equals(insn_type);
Jeff Haofe1f7c82013-08-01 14:50:24 -07003342 value_compatible = value_type.IsIntegralTypes();
3343 } else if (target_type.IsFloat()) {
3344 instruction_compatible = insn_type.IsInteger(); // no put-float, so expect put-int
3345 value_compatible = value_type.IsFloatTypes();
3346 } else if (target_type.IsLong()) {
3347 instruction_compatible = insn_type.IsLong();
3348 value_compatible = value_type.IsLongTypes();
3349 } else if (target_type.IsDouble()) {
3350 instruction_compatible = insn_type.IsLong(); // no put-double, so expect put-long
3351 value_compatible = value_type.IsDoubleTypes();
3352 } else {
3353 instruction_compatible = false; // reference with primitive store
3354 value_compatible = false; // unused
3355 }
3356 if (!instruction_compatible) {
3357 // This is a global failure rather than a class change failure as the instructions and
3358 // the descriptors for the type should have been consistent within the same file at
3359 // compile time.
3360 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "put insn has type '" << insn_type
3361 << "' but expected type '" << target_type << "'";
3362 return;
3363 }
3364 if (!value_compatible) {
3365 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
3366 << " of type " << value_type << " but expected " << target_type << " for put";
3367 return;
3368 }
3369}
3370
Sebastien Hertz5243e912013-05-21 10:55:07 +02003371void MethodVerifier::VerifyAPut(const Instruction* inst,
Sebastien Hertz757b3042014-03-28 14:34:28 +01003372 const RegType& insn_type, bool is_primitive) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003373 const RegType& index_type = work_line_->GetRegisterType(inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07003374 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003375 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07003376 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003377 const RegType& array_type = work_line_->GetRegisterType(inst->VRegB_23x());
Ian Rogers89310de2012-02-01 13:47:30 -08003378 if (array_type.IsZero()) {
3379 // Null array type; this code path will fail at runtime. Infer a merge-able type from the
3380 // instruction type.
jeffhaofc3144e2012-02-01 17:21:15 -08003381 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003382 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aput";
Ian Rogers89310de2012-02-01 13:47:30 -08003383 } else {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003384 const RegType& component_type = reg_types_.GetComponentType(array_type, class_loader_->Get());
Jeff Haofe1f7c82013-08-01 14:50:24 -07003385 const uint32_t vregA = inst->VRegA_23x();
Jeff Haob24b4a72013-07-31 13:47:31 -07003386 if (is_primitive) {
Jeff Haofe1f7c82013-08-01 14:50:24 -07003387 VerifyPrimitivePut(component_type, insn_type, vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07003388 } else {
Jeff Haob24b4a72013-07-31 13:47:31 -07003389 if (!component_type.IsReferenceTypes()) {
3390 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
3391 << " source for aput-object";
3392 } else {
3393 // The instruction agrees with the type of array, confirm the value to be stored does too
3394 // Note: we use the instruction type (rather than the component type) for aput-object as
3395 // incompatible classes will be caught at runtime as an array store exception
Jeff Haofe1f7c82013-08-01 14:50:24 -07003396 work_line_->VerifyRegisterType(vregA, insn_type);
Jeff Haob24b4a72013-07-31 13:47:31 -07003397 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003398 }
3399 }
3400 }
3401}
3402
Brian Carlstromea46f952013-07-30 01:26:50 -07003403mirror::ArtField* MethodVerifier::GetStaticField(int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08003404 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3405 // Check access to class
3406 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Brian Carlstrom7934ac22013-07-26 10:54:15 -07003407 if (klass_type.IsConflict()) { // bad class
Ian Rogersad0b3a32012-04-16 14:50:24 -07003408 AppendToLastFailMessage(StringPrintf(" in attempt to access static field %d (%s) in %s",
3409 field_idx, dex_file_->GetFieldName(field_id),
3410 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers90040192011-12-16 08:54:29 -08003411 return NULL;
3412 }
Elliott Hughesb25c3f62012-03-26 16:35:06 -07003413 if (klass_type.IsUnresolvedTypes()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003414 return NULL; // Can't resolve Class so no more to do here, will do checking at runtime.
Ian Rogers90040192011-12-16 08:54:29 -08003415 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07003416 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
3417 mirror::ArtField* field = class_linker->ResolveFieldJLS(*dex_file_, field_idx, *dex_cache_,
3418 *class_loader_);
Ian Rogersd81871c2011-10-03 13:57:23 -07003419 if (field == NULL) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07003420 VLOG(verifier) << "Unable to resolve static field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07003421 << dex_file_->GetFieldName(field_id) << ") in "
3422 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07003423 DCHECK(Thread::Current()->IsExceptionPending());
3424 Thread::Current()->ClearException();
3425 return NULL;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003426 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
3427 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003428 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access static field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07003429 << " from " << GetDeclaringClass();
Ian Rogersd81871c2011-10-03 13:57:23 -07003430 return NULL;
3431 } else if (!field->IsStatic()) {
3432 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field) << " to be static";
3433 return NULL;
Ian Rogersd81871c2011-10-03 13:57:23 -07003434 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07003435 return field;
Ian Rogersd81871c2011-10-03 13:57:23 -07003436}
3437
Brian Carlstromea46f952013-07-30 01:26:50 -07003438mirror::ArtField* MethodVerifier::GetInstanceField(const RegType& obj_type, int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08003439 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3440 // Check access to class
3441 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003442 if (klass_type.IsConflict()) {
3443 AppendToLastFailMessage(StringPrintf(" in attempt to access instance field %d (%s) in %s",
3444 field_idx, dex_file_->GetFieldName(field_id),
3445 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers90040192011-12-16 08:54:29 -08003446 return NULL;
3447 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003448 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers90040192011-12-16 08:54:29 -08003449 return NULL; // Can't resolve Class so no more to do here
3450 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07003451 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
3452 mirror::ArtField* field = class_linker->ResolveFieldJLS(*dex_file_, field_idx, *dex_cache_,
3453 *class_loader_);
Ian Rogersd81871c2011-10-03 13:57:23 -07003454 if (field == NULL) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07003455 VLOG(verifier) << "Unable to resolve instance field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07003456 << dex_file_->GetFieldName(field_id) << ") in "
3457 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07003458 DCHECK(Thread::Current()->IsExceptionPending());
3459 Thread::Current()->ClearException();
3460 return NULL;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003461 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
3462 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003463 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access instance field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07003464 << " from " << GetDeclaringClass();
Ian Rogersd81871c2011-10-03 13:57:23 -07003465 return NULL;
3466 } else if (field->IsStatic()) {
3467 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field)
3468 << " to not be static";
3469 return NULL;
3470 } else if (obj_type.IsZero()) {
3471 // Cannot infer and check type, however, access will cause null pointer exception
3472 return field;
Ian Rogerse1758fe2012-04-19 11:31:15 -07003473 } else {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003474 mirror::Class* klass = field->GetDeclaringClass();
Ian Rogers637c65b2013-05-31 11:46:00 -07003475 const RegType& field_klass =
3476 reg_types_.FromClass(dex_file_->GetFieldDeclaringClassDescriptor(field_id),
Ian Rogers04f94f42013-06-10 15:09:26 -07003477 klass, klass->CannotBeAssignedFromOtherTypes());
Ian Rogersad0b3a32012-04-16 14:50:24 -07003478 if (obj_type.IsUninitializedTypes() &&
3479 (!IsConstructor() || GetDeclaringClass().Equals(obj_type) ||
3480 !field_klass.Equals(GetDeclaringClass()))) {
3481 // Field accesses through uninitialized references are only allowable for constructors where
3482 // the field is declared in this class
3483 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "cannot access instance field " << PrettyField(field)
Brian Carlstrom93c33962013-07-26 10:37:43 -07003484 << " of a not fully initialized object within the context"
3485 << " of " << PrettyMethod(dex_method_idx_, *dex_file_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003486 return NULL;
3487 } else if (!field_klass.IsAssignableFrom(obj_type)) {
3488 // Trying to access C1.field1 using reference of type C2, which is neither C1 or a sub-class
3489 // of C1. For resolution to occur the declared class of the field must be compatible with
3490 // obj_type, we've discovered this wasn't so, so report the field didn't exist.
3491 Fail(VERIFY_ERROR_NO_FIELD) << "cannot access instance field " << PrettyField(field)
3492 << " from object of type " << obj_type;
3493 return NULL;
3494 } else {
3495 return field;
3496 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003497 }
3498}
3499
Sebastien Hertz5243e912013-05-21 10:55:07 +02003500void MethodVerifier::VerifyISGet(const Instruction* inst, const RegType& insn_type,
3501 bool is_primitive, bool is_static) {
3502 uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
Brian Carlstromea46f952013-07-30 01:26:50 -07003503 mirror::ArtField* field;
Ian Rogersb94a27b2011-10-26 00:33:41 -07003504 if (is_static) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07003505 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003506 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003507 const RegType& object_type = work_line_->GetRegisterType(inst->VRegB_22c());
Ian Rogersf4028cc2011-11-02 14:56:39 -07003508 field = GetInstanceField(object_type, field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003509 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003510 const RegType* field_type = nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003511 if (field != NULL) {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003512 FieldHelper fh(field);
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08003513 mirror::Class* field_type_class = fh.GetType(can_load_classes_);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003514 if (field_type_class != nullptr) {
3515 field_type = &reg_types_.FromClass(fh.GetTypeDescriptor(), field_type_class,
3516 field_type_class->CannotBeAssignedFromOtherTypes());
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08003517 } else {
3518 Thread* self = Thread::Current();
3519 DCHECK(!can_load_classes_ || self->IsExceptionPending());
3520 self->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003521 }
Ian Rogers0d604842012-04-16 14:50:24 -07003522 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003523 if (field_type == nullptr) {
3524 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3525 const char* descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003526 field_type = &reg_types_.FromDescriptor(class_loader_->Get(), descriptor, false);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003527 }
Sebastien Hertz757b3042014-03-28 14:34:28 +01003528 DCHECK(field_type != nullptr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02003529 const uint32_t vregA = (is_static) ? inst->VRegA_21c() : inst->VRegA_22c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07003530 if (is_primitive) {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003531 if (field_type->Equals(insn_type) ||
3532 (field_type->IsFloat() && insn_type.IsInteger()) ||
3533 (field_type->IsDouble() && insn_type.IsLong())) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003534 // expected that read is of the correct primitive type or that int reads are reading
3535 // floats or long reads are reading doubles
3536 } else {
3537 // This is a global failure rather than a class change failure as the instructions and
3538 // the descriptors for the type should have been consistent within the same file at
3539 // compile time
3540 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
3541 << " to be of type '" << insn_type
Sebastien Hertz757b3042014-03-28 14:34:28 +01003542 << "' but found type '" << *field_type << "' in get";
Ian Rogersad0b3a32012-04-16 14:50:24 -07003543 return;
3544 }
3545 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003546 if (!insn_type.IsAssignableFrom(*field_type)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003547 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
3548 << " to be compatible with type '" << insn_type
Sebastien Hertz757b3042014-03-28 14:34:28 +01003549 << "' but found type '" << *field_type
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003550 << "' in Get-object";
Sebastien Hertz5243e912013-05-21 10:55:07 +02003551 work_line_->SetRegisterType(vregA, reg_types_.Conflict());
Ian Rogersad0b3a32012-04-16 14:50:24 -07003552 return;
3553 }
3554 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003555 if (!field_type->IsLowHalf()) {
3556 work_line_->SetRegisterType(vregA, *field_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003557 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003558 work_line_->SetRegisterTypeWide(vregA, *field_type, field_type->HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003559 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003560}
3561
Sebastien Hertz5243e912013-05-21 10:55:07 +02003562void MethodVerifier::VerifyISPut(const Instruction* inst, const RegType& insn_type,
3563 bool is_primitive, bool is_static) {
3564 uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
Brian Carlstromea46f952013-07-30 01:26:50 -07003565 mirror::ArtField* field;
Ian Rogersb94a27b2011-10-26 00:33:41 -07003566 if (is_static) {
Ian Rogers55d249f2011-11-02 16:48:09 -07003567 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003568 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003569 const RegType& object_type = work_line_->GetRegisterType(inst->VRegB_22c());
Ian Rogers55d249f2011-11-02 16:48:09 -07003570 field = GetInstanceField(object_type, field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003571 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003572 const RegType* field_type = nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003573 if (field != NULL) {
3574 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
3575 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
3576 << " from other class " << GetDeclaringClass();
3577 return;
3578 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003579 FieldHelper fh(field);
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08003580 mirror::Class* field_type_class = fh.GetType(can_load_classes_);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003581 if (field_type_class != nullptr) {
3582 field_type = &reg_types_.FromClass(fh.GetTypeDescriptor(), field_type_class,
3583 field_type_class->CannotBeAssignedFromOtherTypes());
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08003584 } else {
3585 Thread* self = Thread::Current();
3586 DCHECK(!can_load_classes_ || self->IsExceptionPending());
3587 self->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003588 }
3589 }
3590 if (field_type == nullptr) {
3591 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3592 const char* descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003593 field_type = &reg_types_.FromDescriptor(class_loader_->Get(), descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003594 }
Sebastien Hertz757b3042014-03-28 14:34:28 +01003595 DCHECK(field_type != nullptr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02003596 const uint32_t vregA = (is_static) ? inst->VRegA_21c() : inst->VRegA_22c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07003597 if (is_primitive) {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003598 VerifyPrimitivePut(*field_type, insn_type, vregA);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003599 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003600 if (!insn_type.IsAssignableFrom(*field_type)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003601 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
3602 << " to be compatible with type '" << insn_type
Sebastien Hertz757b3042014-03-28 14:34:28 +01003603 << "' but found type '" << *field_type
Ian Rogersad0b3a32012-04-16 14:50:24 -07003604 << "' in put-object";
3605 return;
3606 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003607 work_line_->VerifyRegisterType(vregA, *field_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07003608 }
3609}
3610
Brian Carlstromea46f952013-07-30 01:26:50 -07003611mirror::ArtField* MethodVerifier::GetQuickFieldAccess(const Instruction* inst,
Ian Rogers98379392014-02-24 16:53:16 -08003612 RegisterLine* reg_line) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003613 DCHECK(inst->Opcode() == Instruction::IGET_QUICK ||
3614 inst->Opcode() == Instruction::IGET_WIDE_QUICK ||
3615 inst->Opcode() == Instruction::IGET_OBJECT_QUICK ||
3616 inst->Opcode() == Instruction::IPUT_QUICK ||
3617 inst->Opcode() == Instruction::IPUT_WIDE_QUICK ||
3618 inst->Opcode() == Instruction::IPUT_OBJECT_QUICK);
3619 const RegType& object_type = reg_line->GetRegisterType(inst->VRegB_22c());
Ian Rogers9bc54402014-04-17 16:40:01 -07003620 if (!object_type.HasClass()) {
3621 VLOG(verifier) << "Failed to get mirror::Class* from '" << object_type << "'";
3622 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003623 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003624 uint32_t field_offset = static_cast<uint32_t>(inst->VRegC_22c());
Sebastien Hertz479fc1e2014-04-04 17:51:34 +02003625 mirror::ArtField* f = mirror::ArtField::FindInstanceFieldWithOffset(object_type.GetClass(),
3626 field_offset);
3627 if (f == nullptr) {
3628 VLOG(verifier) << "Failed to find instance field at offset '" << field_offset
3629 << "' from '" << PrettyDescriptor(object_type.GetClass()) << "'";
3630 }
3631 return f;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003632}
3633
3634void MethodVerifier::VerifyIGetQuick(const Instruction* inst, const RegType& insn_type,
3635 bool is_primitive) {
3636 DCHECK(Runtime::Current()->IsStarted());
Brian Carlstromea46f952013-07-30 01:26:50 -07003637 mirror::ArtField* field = GetQuickFieldAccess(inst, work_line_.get());
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003638 if (field == NULL) {
3639 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field from " << inst->Name();
3640 return;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003641 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003642 FieldHelper fh(field);
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08003643 mirror::Class* field_type_class = fh.GetType(can_load_classes_);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003644 const RegType* field_type;
3645 if (field_type_class != nullptr) {
3646 field_type = &reg_types_.FromClass(fh.GetTypeDescriptor(), field_type_class,
3647 field_type_class->CannotBeAssignedFromOtherTypes());
3648 } else {
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08003649 Thread* self = Thread::Current();
3650 DCHECK(!can_load_classes_ || self->IsExceptionPending());
3651 self->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003652 field_type = &reg_types_.FromDescriptor(field->GetDeclaringClass()->GetClassLoader(),
3653 fh.GetTypeDescriptor(), false);
3654 }
Sebastien Hertz757b3042014-03-28 14:34:28 +01003655 DCHECK(field_type != nullptr);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003656 const uint32_t vregA = inst->VRegA_22c();
3657 if (is_primitive) {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003658 if (field_type->Equals(insn_type) ||
3659 (field_type->IsFloat() && insn_type.IsIntegralTypes()) ||
3660 (field_type->IsDouble() && insn_type.IsLongTypes())) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003661 // expected that read is of the correct primitive type or that int reads are reading
3662 // floats or long reads are reading doubles
3663 } else {
3664 // This is a global failure rather than a class change failure as the instructions and
3665 // the descriptors for the type should have been consistent within the same file at
3666 // compile time
3667 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003668 << " to be of type '" << insn_type
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003669 << "' but found type '" << *field_type << "' in Get";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003670 return;
3671 }
3672 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003673 if (!insn_type.IsAssignableFrom(*field_type)) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003674 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003675 << " to be compatible with type '" << insn_type
Sebastien Hertz757b3042014-03-28 14:34:28 +01003676 << "' but found type '" << *field_type
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003677 << "' in get-object";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003678 work_line_->SetRegisterType(vregA, reg_types_.Conflict());
3679 return;
3680 }
3681 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003682 if (!field_type->IsLowHalf()) {
3683 work_line_->SetRegisterType(vregA, *field_type);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003684 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003685 work_line_->SetRegisterTypeWide(vregA, *field_type, field_type->HighHalf(&reg_types_));
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003686 }
3687}
3688
3689void MethodVerifier::VerifyIPutQuick(const Instruction* inst, const RegType& insn_type,
3690 bool is_primitive) {
3691 DCHECK(Runtime::Current()->IsStarted());
Brian Carlstromea46f952013-07-30 01:26:50 -07003692 mirror::ArtField* field = GetQuickFieldAccess(inst, work_line_.get());
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003693 if (field == NULL) {
3694 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field from " << inst->Name();
3695 return;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003696 }
3697 const char* descriptor = FieldHelper(field).GetTypeDescriptor();
3698 mirror::ClassLoader* loader = field->GetDeclaringClass()->GetClassLoader();
3699 const RegType& field_type = reg_types_.FromDescriptor(loader, descriptor, false);
3700 if (field != NULL) {
3701 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
3702 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003703 << " from other class " << GetDeclaringClass();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003704 return;
3705 }
3706 }
3707 const uint32_t vregA = inst->VRegA_22c();
3708 if (is_primitive) {
3709 // Primitive field assignability rules are weaker than regular assignability rules
3710 bool instruction_compatible;
3711 bool value_compatible;
3712 const RegType& value_type = work_line_->GetRegisterType(vregA);
3713 if (field_type.IsIntegralTypes()) {
3714 instruction_compatible = insn_type.IsIntegralTypes();
3715 value_compatible = value_type.IsIntegralTypes();
3716 } else if (field_type.IsFloat()) {
3717 instruction_compatible = insn_type.IsInteger(); // no [is]put-float, so expect [is]put-int
3718 value_compatible = value_type.IsFloatTypes();
3719 } else if (field_type.IsLong()) {
3720 instruction_compatible = insn_type.IsLong();
3721 value_compatible = value_type.IsLongTypes();
3722 } else if (field_type.IsDouble()) {
3723 instruction_compatible = insn_type.IsLong(); // no [is]put-double, so expect [is]put-long
3724 value_compatible = value_type.IsDoubleTypes();
3725 } else {
3726 instruction_compatible = false; // reference field with primitive store
3727 value_compatible = false; // unused
3728 }
3729 if (!instruction_compatible) {
3730 // This is a global failure rather than a class change failure as the instructions and
3731 // the descriptors for the type should have been consistent within the same file at
3732 // compile time
3733 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003734 << " to be of type '" << insn_type
3735 << "' but found type '" << field_type
3736 << "' in put";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003737 return;
3738 }
3739 if (!value_compatible) {
3740 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
3741 << " of type " << value_type
3742 << " but expected " << field_type
3743 << " for store to " << PrettyField(field) << " in put";
3744 return;
3745 }
3746 } else {
3747 if (!insn_type.IsAssignableFrom(field_type)) {
3748 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003749 << " to be compatible with type '" << insn_type
3750 << "' but found type '" << field_type
3751 << "' in put-object";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003752 return;
3753 }
3754 work_line_->VerifyRegisterType(vregA, field_type);
3755 }
3756}
3757
Ian Rogers776ac1f2012-04-13 23:36:36 -07003758bool MethodVerifier::CheckNotMoveException(const uint16_t* insns, int insn_idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003759 if ((insns[insn_idx] & 0xff) == Instruction::MOVE_EXCEPTION) {
jeffhaod5347e02012-03-22 17:25:05 -07003760 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-exception";
Ian Rogersd81871c2011-10-03 13:57:23 -07003761 return false;
3762 }
3763 return true;
3764}
3765
Ian Rogers776ac1f2012-04-13 23:36:36 -07003766bool MethodVerifier::UpdateRegisters(uint32_t next_insn, const RegisterLine* merge_line) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003767 bool changed = true;
3768 RegisterLine* target_line = reg_table_.GetLine(next_insn);
3769 if (!insn_flags_[next_insn].IsVisitedOrChanged()) {
jeffhaobdb76512011-09-07 11:43:16 -07003770 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003771 * We haven't processed this instruction before, and we haven't touched the registers here, so
3772 * there's nothing to "merge". Copy the registers over and mark it as changed. (This is the
3773 * only way a register can transition out of "unknown", so this is not just an optimization.)
jeffhaobdb76512011-09-07 11:43:16 -07003774 */
Ian Rogersb8c78592013-07-25 23:52:52 +00003775 if (!insn_flags_[next_insn].IsReturn()) {
3776 target_line->CopyFromLine(merge_line);
3777 } else {
Jeff Haob24b4a72013-07-31 13:47:31 -07003778 // Verify that the monitor stack is empty on return.
3779 if (!merge_line->VerifyMonitorStackEmpty()) {
3780 return false;
3781 }
Ian Rogersb8c78592013-07-25 23:52:52 +00003782 // For returns we only care about the operand to the return, all other registers are dead.
3783 // Initialize them as conflicts so they don't add to GC and deoptimization information.
3784 const Instruction* ret_inst = Instruction::At(code_item_->insns_ + next_insn);
3785 Instruction::Code opcode = ret_inst->Opcode();
3786 if ((opcode == Instruction::RETURN_VOID) || (opcode == Instruction::RETURN_VOID_BARRIER)) {
3787 target_line->MarkAllRegistersAsConflicts();
3788 } else {
3789 target_line->CopyFromLine(merge_line);
3790 if (opcode == Instruction::RETURN_WIDE) {
3791 target_line->MarkAllRegistersAsConflictsExceptWide(ret_inst->VRegA_11x());
3792 } else {
3793 target_line->MarkAllRegistersAsConflictsExcept(ret_inst->VRegA_11x());
3794 }
3795 }
3796 }
jeffhaobdb76512011-09-07 11:43:16 -07003797 } else {
Brian Carlstrom93c33962013-07-26 10:37:43 -07003798 UniquePtr<RegisterLine> copy(gDebugVerify ?
Ian Rogersd0fbd852013-09-24 18:17:04 -07003799 RegisterLine::Create(target_line->NumRegs(), this) :
Brian Carlstrom93c33962013-07-26 10:37:43 -07003800 NULL);
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003801 if (gDebugVerify) {
3802 copy->CopyFromLine(target_line);
3803 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003804 changed = target_line->MergeRegisters(merge_line);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003805 if (have_pending_hard_failure_) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003806 return false;
jeffhaobdb76512011-09-07 11:43:16 -07003807 }
Ian Rogers2c8a8572011-10-24 17:11:36 -07003808 if (gDebugVerify && changed) {
Elliott Hughes398f64b2012-03-26 18:05:48 -07003809 LogVerifyInfo() << "Merging at [" << reinterpret_cast<void*>(work_insn_idx_) << "]"
Elliott Hughesc073b072012-05-24 19:29:17 -07003810 << " to [" << reinterpret_cast<void*>(next_insn) << "]: " << "\n"
3811 << *copy.get() << " MERGE\n"
3812 << *merge_line << " ==\n"
3813 << *target_line << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07003814 }
3815 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003816 if (changed) {
3817 insn_flags_[next_insn].SetChanged();
jeffhaobdb76512011-09-07 11:43:16 -07003818 }
3819 return true;
3820}
3821
Ian Rogers7b3ddd22013-02-21 15:19:52 -08003822InstructionFlags* MethodVerifier::CurrentInsnFlags() {
Ian Rogers776ac1f2012-04-13 23:36:36 -07003823 return &insn_flags_[work_insn_idx_];
3824}
3825
Ian Rogersad0b3a32012-04-16 14:50:24 -07003826const RegType& MethodVerifier::GetMethodReturnType() {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003827 if (return_type_ == nullptr) {
3828 if (mirror_method_ != NULL) {
3829 MethodHelper mh(mirror_method_);
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08003830 mirror::Class* return_type_class = mh.GetReturnType(can_load_classes_);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003831 if (return_type_class != nullptr) {
Mathieu Chartier590fee92013-09-13 13:46:47 -07003832 return_type_ = &reg_types_.FromClass(mh.GetReturnTypeDescriptor(), return_type_class,
3833 return_type_class->CannotBeAssignedFromOtherTypes());
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003834 } else {
3835 Thread* self = Thread::Current();
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08003836 DCHECK(!can_load_classes_ || self->IsExceptionPending());
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003837 self->ClearException();
3838 }
3839 }
3840 if (return_type_ == nullptr) {
3841 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
3842 const DexFile::ProtoId& proto_id = dex_file_->GetMethodPrototype(method_id);
3843 uint16_t return_type_idx = proto_id.return_type_idx_;
3844 const char* descriptor = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(return_type_idx));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003845 return_type_ = &reg_types_.FromDescriptor(class_loader_->Get(), descriptor, false);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003846 }
3847 }
3848 return *return_type_;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003849}
3850
3851const RegType& MethodVerifier::GetDeclaringClass() {
Ian Rogers637c65b2013-05-31 11:46:00 -07003852 if (declaring_class_ == NULL) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003853 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
Brian Carlstrom93c33962013-07-26 10:37:43 -07003854 const char* descriptor
3855 = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(method_id.class_idx_));
Ian Rogers637c65b2013-05-31 11:46:00 -07003856 if (mirror_method_ != NULL) {
3857 mirror::Class* klass = mirror_method_->GetDeclaringClass();
Ian Rogers04f94f42013-06-10 15:09:26 -07003858 declaring_class_ = &reg_types_.FromClass(descriptor, klass,
3859 klass->CannotBeAssignedFromOtherTypes());
Ian Rogers637c65b2013-05-31 11:46:00 -07003860 } else {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003861 declaring_class_ = &reg_types_.FromDescriptor(class_loader_->Get(), descriptor, false);
Ian Rogers637c65b2013-05-31 11:46:00 -07003862 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003863 }
Ian Rogers637c65b2013-05-31 11:46:00 -07003864 return *declaring_class_;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003865}
3866
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003867std::vector<int32_t> MethodVerifier::DescribeVRegs(uint32_t dex_pc) {
3868 RegisterLine* line = reg_table_.GetLine(dex_pc);
Sebastien Hertzaa0c00c2014-03-14 17:58:54 +01003869 DCHECK(line != nullptr) << "No register line at DEX pc " << StringPrintf("0x%x", dex_pc);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003870 std::vector<int32_t> result;
3871 for (size_t i = 0; i < line->NumRegs(); ++i) {
3872 const RegType& type = line->GetRegisterType(i);
3873 if (type.IsConstant()) {
3874 result.push_back(type.IsPreciseConstant() ? kConstant : kImpreciseConstant);
3875 result.push_back(type.ConstantValue());
3876 } else if (type.IsConstantLo()) {
3877 result.push_back(type.IsPreciseConstantLo() ? kConstant : kImpreciseConstant);
3878 result.push_back(type.ConstantValueLo());
3879 } else if (type.IsConstantHi()) {
3880 result.push_back(type.IsPreciseConstantHi() ? kConstant : kImpreciseConstant);
3881 result.push_back(type.ConstantValueHi());
3882 } else if (type.IsIntegralTypes()) {
3883 result.push_back(kIntVReg);
3884 result.push_back(0);
3885 } else if (type.IsFloat()) {
3886 result.push_back(kFloatVReg);
3887 result.push_back(0);
3888 } else if (type.IsLong()) {
3889 result.push_back(kLongLoVReg);
3890 result.push_back(0);
3891 result.push_back(kLongHiVReg);
3892 result.push_back(0);
3893 ++i;
3894 } else if (type.IsDouble()) {
3895 result.push_back(kDoubleLoVReg);
3896 result.push_back(0);
3897 result.push_back(kDoubleHiVReg);
3898 result.push_back(0);
3899 ++i;
3900 } else if (type.IsUndefined() || type.IsConflict() || type.IsHighHalf()) {
3901 result.push_back(kUndefined);
3902 result.push_back(0);
3903 } else {
Ian Rogers7b3ddd22013-02-21 15:19:52 -08003904 CHECK(type.IsNonZeroReferenceTypes());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003905 result.push_back(kReferenceVReg);
3906 result.push_back(0);
3907 }
3908 }
3909 return result;
3910}
3911
Sebastien Hertz849600b2013-12-20 10:28:08 +01003912const RegType& MethodVerifier::DetermineCat1Constant(int32_t value, bool precise) {
3913 if (precise) {
3914 // Precise constant type.
3915 return reg_types_.FromCat1Const(value, true);
3916 } else {
3917 // Imprecise constant type.
3918 if (value < -32768) {
3919 return reg_types_.IntConstant();
3920 } else if (value < -128) {
3921 return reg_types_.ShortConstant();
3922 } else if (value < 0) {
3923 return reg_types_.ByteConstant();
3924 } else if (value == 0) {
3925 return reg_types_.Zero();
3926 } else if (value == 1) {
3927 return reg_types_.One();
3928 } else if (value < 128) {
3929 return reg_types_.PosByteConstant();
3930 } else if (value < 32768) {
3931 return reg_types_.PosShortConstant();
3932 } else if (value < 65536) {
3933 return reg_types_.CharConstant();
3934 } else {
3935 return reg_types_.IntConstant();
3936 }
3937 }
3938}
3939
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003940void MethodVerifier::Init() {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08003941 art::verifier::RegTypeCache::Init();
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003942}
3943
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003944void MethodVerifier::Shutdown() {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08003945 verifier::RegTypeCache::ShutDown();
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08003946}
jeffhaod1224c72012-02-29 13:43:08 -08003947
Mathieu Chartier83c8ee02014-01-28 14:50:23 -08003948void MethodVerifier::VisitRoots(RootCallback* callback, void* arg) {
3949 reg_types_.VisitRoots(callback, arg);
Mathieu Chartierc528dba2013-11-26 12:00:11 -08003950}
3951
Ian Rogersd81871c2011-10-03 13:57:23 -07003952} // namespace verifier
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07003953} // namespace art