blob: 2293ad7c087b0826cb993e8b5d416c92a8771142 [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;
Mathieu Chartierf8322842014-05-16 10:59:25 -070096 const DexFile& dex_file = klass->GetDexFile();
97 const DexFile::ClassDef* class_def = klass->GetClassDef();
Jeff Hao2d7e5aa2013-12-13 17:39:59 -080098 mirror::Class* super = klass->GetSuperClass();
Mathieu Chartierf8322842014-05-16 10:59:25 -070099 if (super == NULL && "Ljava/lang/Object;" != klass->GetDescriptor()) {
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800100 early_failure = true;
101 failure_message = " that has no super class";
102 } else if (super != NULL && super->IsFinal()) {
103 early_failure = true;
104 failure_message = " that attempts to sub-class final class " + PrettyDescriptor(super);
105 } else if (class_def == NULL) {
106 early_failure = true;
107 failure_message = " that isn't present in dex file " + dex_file.GetLocation();
108 }
109 if (early_failure) {
110 *error = "Verifier rejected class " + PrettyDescriptor(klass) + failure_message;
111 if (Runtime::Current()->IsCompiler()) {
112 ClassReference ref(&dex_file, klass->GetDexClassDefIndex());
Vladimir Marko2b5eaa22013-12-13 13:59:30 +0000113 Runtime::Current()->GetCompilerCallbacks()->ClassRejected(ref);
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800114 }
jeffhaof1e6b7c2012-06-05 18:33:30 -0700115 return kHardFailure;
jeffhaobdb76512011-09-07 11:43:16 -0700116 }
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700117 StackHandleScope<2> hs(Thread::Current());
Mathieu Chartierf8322842014-05-16 10:59:25 -0700118 Handle<mirror::DexCache> dex_cache(hs.NewHandle(klass->GetDexCache()));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700119 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(klass->GetClassLoader()));
Mathieu Chartier590fee92013-09-13 13:46:47 -0700120 return VerifyClass(&dex_file, dex_cache, class_loader, class_def, allow_soft_failures, error);
Shih-wei Liao371814f2011-10-27 16:52:10 -0700121}
122
Ian Rogers365c1022012-06-22 15:05:28 -0700123MethodVerifier::FailureKind MethodVerifier::VerifyClass(const DexFile* dex_file,
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700124 Handle<mirror::DexCache> dex_cache,
125 Handle<mirror::ClassLoader> class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700126 const DexFile::ClassDef* class_def,
127 bool allow_soft_failures,
128 std::string* error) {
129 DCHECK(class_def != nullptr);
130 const byte* class_data = dex_file->GetClassData(*class_def);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700131 if (class_data == NULL) {
132 // empty class, probably a marker interface
jeffhaof1e6b7c2012-06-05 18:33:30 -0700133 return kNoFailure;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700134 }
jeffhaof56197c2012-03-05 18:01:54 -0800135 ClassDataItemIterator it(*dex_file, class_data);
136 while (it.HasNextStaticField() || it.HasNextInstanceField()) {
137 it.Next();
138 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700139 size_t error_count = 0;
jeffhaof1e6b7c2012-06-05 18:33:30 -0700140 bool hard_fail = false;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700141 ClassLinker* linker = Runtime::Current()->GetClassLinker();
jeffhao9b0b1882012-10-01 16:51:22 -0700142 int64_t previous_direct_method_idx = -1;
jeffhaof56197c2012-03-05 18:01:54 -0800143 while (it.HasNextDirectMethod()) {
144 uint32_t method_idx = it.GetMemberIndex();
jeffhao9b0b1882012-10-01 16:51:22 -0700145 if (method_idx == previous_direct_method_idx) {
146 // smali can create dex files with two encoded_methods sharing the same method_idx
147 // http://code.google.com/p/smali/issues/detail?id=119
148 it.Next();
149 continue;
150 }
151 previous_direct_method_idx = method_idx;
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700152 InvokeType type = it.GetMethodInvokeType(*class_def);
Brian Carlstromea46f952013-07-30 01:26:50 -0700153 mirror::ArtMethod* method =
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700154 linker->ResolveMethod(*dex_file, method_idx, dex_cache, class_loader,
155 NullHandle<mirror::ArtMethod>(), 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 =
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700197 linker->ResolveMethod(*dex_file, method_idx, dex_cache, class_loader,
198 NullHandle<mirror::ArtMethod>(), type);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700199 if (method == NULL) {
200 DCHECK(Thread::Current()->IsExceptionPending());
201 // We couldn't resolve the method, but continue regardless.
202 Thread::Current()->ClearException();
203 }
Brian Carlstrom93c33962013-07-26 10:37:43 -0700204 MethodVerifier::FailureKind result = VerifyMethod(method_idx,
205 dex_file,
206 dex_cache,
207 class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700208 class_def,
Brian Carlstrom93c33962013-07-26 10:37:43 -0700209 it.GetMethodCodeItem(),
210 method,
211 it.GetMemberAccessFlags(),
212 allow_soft_failures);
jeffhaof1e6b7c2012-06-05 18:33:30 -0700213 if (result != kNoFailure) {
214 if (result == kHardFailure) {
215 hard_fail = true;
216 if (error_count > 0) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700217 *error += "\n";
jeffhaof1e6b7c2012-06-05 18:33:30 -0700218 }
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700219 *error = "Verifier rejected class ";
220 *error += PrettyDescriptor(dex_file->GetClassDescriptor(*class_def));
221 *error += " due to bad method ";
222 *error += PrettyMethod(method_idx, *dex_file);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700223 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700224 ++error_count;
jeffhaof56197c2012-03-05 18:01:54 -0800225 }
226 it.Next();
227 }
jeffhaof1e6b7c2012-06-05 18:33:30 -0700228 if (error_count == 0) {
229 return kNoFailure;
230 } else {
231 return hard_fail ? kHardFailure : kSoftFailure;
232 }
jeffhaof56197c2012-03-05 18:01:54 -0800233}
234
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800235MethodVerifier::FailureKind MethodVerifier::VerifyMethod(uint32_t method_idx,
236 const DexFile* dex_file,
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700237 Handle<mirror::DexCache> dex_cache,
238 Handle<mirror::ClassLoader> class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700239 const DexFile::ClassDef* class_def,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800240 const DexFile::CodeItem* code_item,
Brian Carlstromea46f952013-07-30 01:26:50 -0700241 mirror::ArtMethod* method,
Jeff Haoee988952013-04-16 14:23:47 -0700242 uint32_t method_access_flags,
243 bool allow_soft_failures) {
Ian Rogersc8982582012-09-07 16:53:25 -0700244 MethodVerifier::FailureKind result = kNoFailure;
245 uint64_t start_ns = NanoTime();
246
Mathieu Chartier590fee92013-09-13 13:46:47 -0700247 MethodVerifier verifier_(dex_file, &dex_cache, &class_loader, class_def, code_item,
248 method_idx, method, method_access_flags, true, allow_soft_failures);
Anwar Ghuloum75a43f12013-08-13 17:22:14 -0700249 if (verifier_.Verify()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700250 // Verification completed, however failures may be pending that didn't cause the verification
251 // to hard fail.
Anwar Ghuloum75a43f12013-08-13 17:22:14 -0700252 CHECK(!verifier_.have_pending_hard_failure_);
253 if (verifier_.failures_.size() != 0) {
254 if (VLOG_IS_ON(verifier)) {
255 verifier_.DumpFailures(VLOG_STREAM(verifier) << "Soft verification failures in "
256 << PrettyMethod(method_idx, *dex_file) << "\n");
257 }
Ian Rogersc8982582012-09-07 16:53:25 -0700258 result = kSoftFailure;
jeffhaof56197c2012-03-05 18:01:54 -0800259 }
260 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700261 // Bad method data.
Anwar Ghuloum75a43f12013-08-13 17:22:14 -0700262 CHECK_NE(verifier_.failures_.size(), 0U);
263 CHECK(verifier_.have_pending_hard_failure_);
264 verifier_.DumpFailures(LOG(INFO) << "Verification error in "
Elliott Hughesc073b072012-05-24 19:29:17 -0700265 << PrettyMethod(method_idx, *dex_file) << "\n");
jeffhaof56197c2012-03-05 18:01:54 -0800266 if (gDebugVerify) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -0700267 std::cout << "\n" << verifier_.info_messages_.str();
268 verifier_.Dump(std::cout);
jeffhaof56197c2012-03-05 18:01:54 -0800269 }
Ian Rogersc8982582012-09-07 16:53:25 -0700270 result = kHardFailure;
jeffhaof56197c2012-03-05 18:01:54 -0800271 }
Ian Rogersc8982582012-09-07 16:53:25 -0700272 uint64_t duration_ns = NanoTime() - start_ns;
273 if (duration_ns > MsToNs(100)) {
274 LOG(WARNING) << "Verification of " << PrettyMethod(method_idx, *dex_file)
275 << " took " << PrettyDuration(duration_ns);
276 }
277 return result;
jeffhaof56197c2012-03-05 18:01:54 -0800278}
279
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800280void MethodVerifier::VerifyMethodAndDump(std::ostream& os, uint32_t dex_method_idx,
Mathieu Chartier590fee92013-09-13 13:46:47 -0700281 const DexFile* dex_file,
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700282 Handle<mirror::DexCache> dex_cache,
283 Handle<mirror::ClassLoader> class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700284 const DexFile::ClassDef* class_def,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800285 const DexFile::CodeItem* code_item,
Brian Carlstromea46f952013-07-30 01:26:50 -0700286 mirror::ArtMethod* method,
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800287 uint32_t method_access_flags) {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700288 MethodVerifier verifier(dex_file, &dex_cache, &class_loader, class_def, code_item,
Jeff Haoee988952013-04-16 14:23:47 -0700289 dex_method_idx, method, method_access_flags, true, true);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700290 verifier.Verify();
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800291 verifier.DumpFailures(os);
292 os << verifier.info_messages_.str();
293 verifier.Dump(os);
294}
295
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700296MethodVerifier::MethodVerifier(const DexFile* dex_file, Handle<mirror::DexCache>* dex_cache,
297 Handle<mirror::ClassLoader>* class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700298 const DexFile::ClassDef* class_def,
Mathieu Chartier590fee92013-09-13 13:46:47 -0700299 const DexFile::CodeItem* code_item, uint32_t dex_method_idx,
300 mirror::ArtMethod* method, uint32_t method_access_flags,
301 bool can_load_classes, bool allow_soft_failures)
Elliott Hughes80537bb2013-01-04 16:37:26 -0800302 : reg_types_(can_load_classes),
303 work_insn_idx_(-1),
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800304 dex_method_idx_(dex_method_idx),
Ian Rogers637c65b2013-05-31 11:46:00 -0700305 mirror_method_(method),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700306 method_access_flags_(method_access_flags),
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700307 return_type_(nullptr),
jeffhaof56197c2012-03-05 18:01:54 -0800308 dex_file_(dex_file),
309 dex_cache_(dex_cache),
310 class_loader_(class_loader),
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700311 class_def_(class_def),
jeffhaof56197c2012-03-05 18:01:54 -0800312 code_item_(code_item),
Ian Rogers637c65b2013-05-31 11:46:00 -0700313 declaring_class_(NULL),
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700314 interesting_dex_pc_(-1),
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700315 monitor_enter_dex_pcs_(nullptr),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700316 have_pending_hard_failure_(false),
jeffhaofaf459e2012-08-31 15:32:47 -0700317 have_pending_runtime_throw_failure_(false),
jeffhaof56197c2012-03-05 18:01:54 -0800318 new_instance_count_(0),
Elliott Hughes80537bb2013-01-04 16:37:26 -0800319 monitor_enter_count_(0),
Jeff Haoee988952013-04-16 14:23:47 -0700320 can_load_classes_(can_load_classes),
Sebastien Hertz4d4adb12013-07-24 16:14:19 +0200321 allow_soft_failures_(allow_soft_failures),
322 has_check_casts_(false),
323 has_virtual_or_interface_invokes_(false) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800324 Runtime::Current()->AddMethodVerifier(this);
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700325 DCHECK(class_def != nullptr);
jeffhaof56197c2012-03-05 18:01:54 -0800326}
327
Mathieu Chartier590fee92013-09-13 13:46:47 -0700328MethodVerifier::~MethodVerifier() {
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800329 Runtime::Current()->RemoveMethodVerifier(this);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700330 STLDeleteElements(&failure_messages_);
331}
332
Brian Carlstromea46f952013-07-30 01:26:50 -0700333void MethodVerifier::FindLocksAtDexPc(mirror::ArtMethod* m, uint32_t dex_pc,
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800334 std::vector<uint32_t>& monitor_enter_dex_pcs) {
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700335 MethodHelper mh(m);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700336 StackHandleScope<2> hs(Thread::Current());
337 Handle<mirror::DexCache> dex_cache(hs.NewHandle(mh.GetDexCache()));
338 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(mh.GetClassLoader()));
Mathieu Chartier590fee92013-09-13 13:46:47 -0700339 MethodVerifier verifier(&mh.GetDexFile(), &dex_cache, &class_loader, &mh.GetClassDef(),
340 mh.GetCodeItem(), m->GetDexMethodIndex(), m, m->GetAccessFlags(), false,
341 true);
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700342 verifier.interesting_dex_pc_ = dex_pc;
343 verifier.monitor_enter_dex_pcs_ = &monitor_enter_dex_pcs;
344 verifier.FindLocksAtDexPc();
345}
346
347void MethodVerifier::FindLocksAtDexPc() {
348 CHECK(monitor_enter_dex_pcs_ != NULL);
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700349 CHECK(code_item_ != NULL); // This only makes sense for methods with code.
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700350
351 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
352 // verification. In practice, the phase we want relies on data structures set up by all the
353 // earlier passes, so we just run the full method verification and bail out early when we've
354 // got what we wanted.
355 Verify();
356}
357
Brian Carlstromea46f952013-07-30 01:26:50 -0700358mirror::ArtField* MethodVerifier::FindAccessedFieldAtDexPc(mirror::ArtMethod* m,
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200359 uint32_t dex_pc) {
360 MethodHelper mh(m);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700361 StackHandleScope<2> hs(Thread::Current());
362 Handle<mirror::DexCache> dex_cache(hs.NewHandle(mh.GetDexCache()));
363 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(mh.GetClassLoader()));
Mathieu Chartier590fee92013-09-13 13:46:47 -0700364 MethodVerifier verifier(&mh.GetDexFile(), &dex_cache, &class_loader, &mh.GetClassDef(),
Ian Rogers9bc54402014-04-17 16:40:01 -0700365 mh.GetCodeItem(), m->GetDexMethodIndex(), m, m->GetAccessFlags(), true,
Mathieu Chartier590fee92013-09-13 13:46:47 -0700366 true);
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200367 return verifier.FindAccessedFieldAtDexPc(dex_pc);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200368}
369
Brian Carlstromea46f952013-07-30 01:26:50 -0700370mirror::ArtField* MethodVerifier::FindAccessedFieldAtDexPc(uint32_t dex_pc) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700371 CHECK(code_item_ != NULL); // This only makes sense for methods with code.
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200372
373 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
374 // verification. In practice, the phase we want relies on data structures set up by all the
375 // earlier passes, so we just run the full method verification and bail out early when we've
376 // got what we wanted.
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200377 bool success = Verify();
378 if (!success) {
Ian Rogers9bc54402014-04-17 16:40:01 -0700379 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200380 }
381 RegisterLine* register_line = reg_table_.GetLine(dex_pc);
382 if (register_line == NULL) {
Ian Rogers9bc54402014-04-17 16:40:01 -0700383 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200384 }
385 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
386 return GetQuickFieldAccess(inst, register_line);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200387}
388
Brian Carlstromea46f952013-07-30 01:26:50 -0700389mirror::ArtMethod* MethodVerifier::FindInvokedMethodAtDexPc(mirror::ArtMethod* m,
Mathieu Chartier590fee92013-09-13 13:46:47 -0700390 uint32_t dex_pc) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200391 MethodHelper mh(m);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700392 StackHandleScope<2> hs(Thread::Current());
393 Handle<mirror::DexCache> dex_cache(hs.NewHandle(mh.GetDexCache()));
394 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(mh.GetClassLoader()));
Mathieu Chartier590fee92013-09-13 13:46:47 -0700395 MethodVerifier verifier(&mh.GetDexFile(), &dex_cache, &class_loader, &mh.GetClassDef(),
Andreas Gampe63981562014-04-17 12:28:43 -0700396 mh.GetCodeItem(), m->GetDexMethodIndex(), m, m->GetAccessFlags(), true,
Mathieu Chartier590fee92013-09-13 13:46:47 -0700397 true);
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200398 return verifier.FindInvokedMethodAtDexPc(dex_pc);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200399}
400
Brian Carlstromea46f952013-07-30 01:26:50 -0700401mirror::ArtMethod* MethodVerifier::FindInvokedMethodAtDexPc(uint32_t dex_pc) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700402 CHECK(code_item_ != NULL); // This only makes sense for methods with code.
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200403
404 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
405 // verification. In practice, the phase we want relies on data structures set up by all the
406 // earlier passes, so we just run the full method verification and bail out early when we've
407 // got what we wanted.
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200408 bool success = Verify();
409 if (!success) {
410 return NULL;
411 }
412 RegisterLine* register_line = reg_table_.GetLine(dex_pc);
413 if (register_line == NULL) {
414 return NULL;
415 }
416 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
417 const bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
418 return GetQuickInvokedMethod(inst, register_line, is_range);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200419}
420
Ian Rogersad0b3a32012-04-16 14:50:24 -0700421bool MethodVerifier::Verify() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700422 // If there aren't any instructions, make sure that's expected, then exit successfully.
423 if (code_item_ == NULL) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700424 if ((method_access_flags_ & (kAccNative | kAccAbstract)) == 0) {
jeffhaod5347e02012-03-22 17:25:05 -0700425 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "zero-length code in concrete non-native method";
jeffhaobdb76512011-09-07 11:43:16 -0700426 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -0700427 } else {
428 return true;
jeffhaobdb76512011-09-07 11:43:16 -0700429 }
jeffhaobdb76512011-09-07 11:43:16 -0700430 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700431 // Sanity-check the register counts. ins + locals = registers, so make sure that ins <= registers.
432 if (code_item_->ins_size_ > code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700433 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad register counts (ins=" << code_item_->ins_size_
434 << " regs=" << code_item_->registers_size_;
Ian Rogersd81871c2011-10-03 13:57:23 -0700435 return false;
jeffhaobdb76512011-09-07 11:43:16 -0700436 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700437 // Allocate and initialize an array to hold instruction data.
Ian Rogers7b3ddd22013-02-21 15:19:52 -0800438 insn_flags_.reset(new InstructionFlags[code_item_->insns_size_in_code_units_]());
Ian Rogersd81871c2011-10-03 13:57:23 -0700439 // Run through the instructions and see if the width checks out.
440 bool result = ComputeWidthsAndCountOps();
441 // Flag instructions guarded by a "try" block and check exception handlers.
442 result = result && ScanTryCatchBlocks();
443 // Perform static instruction verification.
444 result = result && VerifyInstructions();
Ian Rogersad0b3a32012-04-16 14:50:24 -0700445 // Perform code-flow analysis and return.
Vladimir Marko2b5eaa22013-12-13 13:59:30 +0000446 result = result && VerifyCodeFlow();
447 // Compute information for compiler.
448 if (result && Runtime::Current()->IsCompiler()) {
449 result = Runtime::Current()->GetCompilerCallbacks()->MethodVerified(this);
450 }
451 return result;
jeffhaoba5ebb92011-08-25 17:24:37 -0700452}
453
Ian Rogers776ac1f2012-04-13 23:36:36 -0700454std::ostream& MethodVerifier::Fail(VerifyError error) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700455 switch (error) {
456 case VERIFY_ERROR_NO_CLASS:
457 case VERIFY_ERROR_NO_FIELD:
458 case VERIFY_ERROR_NO_METHOD:
459 case VERIFY_ERROR_ACCESS_CLASS:
460 case VERIFY_ERROR_ACCESS_FIELD:
461 case VERIFY_ERROR_ACCESS_METHOD:
Ian Rogers08f753d2012-08-24 14:35:25 -0700462 case VERIFY_ERROR_INSTANTIATION:
463 case VERIFY_ERROR_CLASS_CHANGE:
Elliott Hughes4993bbc2013-01-10 15:41:25 -0800464 if (Runtime::Current()->IsCompiler() || !can_load_classes_) {
jeffhaofaf459e2012-08-31 15:32:47 -0700465 // If we're optimistically running verification at compile time, turn NO_xxx, ACCESS_xxx,
466 // class change and instantiation errors into soft verification errors so that we re-verify
467 // at runtime. We may fail to find or to agree on access because of not yet available class
468 // loaders, or class loaders that will differ at runtime. In these cases, we don't want to
469 // affect the soundness of the code being compiled. Instead, the generated code runs "slow
470 // paths" that dynamically perform the verification and cause the behavior to be that akin
471 // to an interpreter.
472 error = VERIFY_ERROR_BAD_CLASS_SOFT;
473 } else {
Jeff Haoa3faaf42013-09-03 19:07:00 -0700474 // If we fail again at runtime, mark that this instruction would throw and force this
475 // method to be executed using the interpreter with checks.
jeffhaofaf459e2012-08-31 15:32:47 -0700476 have_pending_runtime_throw_failure_ = true;
477 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700478 break;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700479 // Indication that verification should be retried at runtime.
480 case VERIFY_ERROR_BAD_CLASS_SOFT:
Jeff Haoee988952013-04-16 14:23:47 -0700481 if (!allow_soft_failures_) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700482 have_pending_hard_failure_ = true;
483 }
484 break;
jeffhaod5347e02012-03-22 17:25:05 -0700485 // Hard verification failures at compile time will still fail at runtime, so the class is
486 // marked as rejected to prevent it from being compiled.
Ian Rogersad0b3a32012-04-16 14:50:24 -0700487 case VERIFY_ERROR_BAD_CLASS_HARD: {
488 if (Runtime::Current()->IsCompiler()) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700489 ClassReference ref(dex_file_, dex_file_->GetIndexForClassDef(*class_def_));
Vladimir Marko2b5eaa22013-12-13 13:59:30 +0000490 Runtime::Current()->GetCompilerCallbacks()->ClassRejected(ref);
jeffhaod1224c72012-02-29 13:43:08 -0800491 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700492 have_pending_hard_failure_ = true;
493 break;
Ian Rogers47a05882012-02-03 12:23:33 -0800494 }
495 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700496 failures_.push_back(error);
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800497 std::string location(StringPrintf("%s: [0x%X]", PrettyMethod(dex_method_idx_, *dex_file_).c_str(),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700498 work_insn_idx_));
499 std::ostringstream* failure_message = new std::ostringstream(location);
500 failure_messages_.push_back(failure_message);
501 return *failure_message;
502}
503
504void MethodVerifier::PrependToLastFailMessage(std::string prepend) {
505 size_t failure_num = failure_messages_.size();
506 DCHECK_NE(failure_num, 0U);
507 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
508 prepend += last_fail_message->str();
509 failure_messages_[failure_num - 1] = new std::ostringstream(prepend);
510 delete last_fail_message;
511}
512
513void MethodVerifier::AppendToLastFailMessage(std::string append) {
514 size_t failure_num = failure_messages_.size();
515 DCHECK_NE(failure_num, 0U);
516 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
517 (*last_fail_message) << append;
Ian Rogers47a05882012-02-03 12:23:33 -0800518}
519
Ian Rogers776ac1f2012-04-13 23:36:36 -0700520bool MethodVerifier::ComputeWidthsAndCountOps() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700521 const uint16_t* insns = code_item_->insns_;
522 size_t insns_size = code_item_->insns_size_in_code_units_;
523 const Instruction* inst = Instruction::At(insns);
jeffhaobdb76512011-09-07 11:43:16 -0700524 size_t new_instance_count = 0;
525 size_t monitor_enter_count = 0;
Ian Rogersd81871c2011-10-03 13:57:23 -0700526 size_t dex_pc = 0;
jeffhaobdb76512011-09-07 11:43:16 -0700527
Ian Rogersd81871c2011-10-03 13:57:23 -0700528 while (dex_pc < insns_size) {
jeffhaobdb76512011-09-07 11:43:16 -0700529 Instruction::Code opcode = inst->Opcode();
Ian Rogersa9a82542013-10-04 11:17:26 -0700530 switch (opcode) {
531 case Instruction::APUT_OBJECT:
532 case Instruction::CHECK_CAST:
533 has_check_casts_ = true;
534 break;
535 case Instruction::INVOKE_VIRTUAL:
536 case Instruction::INVOKE_VIRTUAL_RANGE:
537 case Instruction::INVOKE_INTERFACE:
538 case Instruction::INVOKE_INTERFACE_RANGE:
539 has_virtual_or_interface_invokes_ = true;
540 break;
541 case Instruction::MONITOR_ENTER:
542 monitor_enter_count++;
543 break;
544 case Instruction::NEW_INSTANCE:
545 new_instance_count++;
546 break;
547 default:
548 break;
jeffhaobdb76512011-09-07 11:43:16 -0700549 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700550 size_t inst_size = inst->SizeInCodeUnits();
551 insn_flags_[dex_pc].SetLengthInCodeUnits(inst_size);
552 dex_pc += inst_size;
jeffhaobdb76512011-09-07 11:43:16 -0700553 inst = inst->Next();
554 }
555
Ian Rogersd81871c2011-10-03 13:57:23 -0700556 if (dex_pc != insns_size) {
jeffhaod5347e02012-03-22 17:25:05 -0700557 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "code did not end where expected ("
558 << dex_pc << " vs. " << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700559 return false;
560 }
561
Ian Rogersd81871c2011-10-03 13:57:23 -0700562 new_instance_count_ = new_instance_count;
563 monitor_enter_count_ = monitor_enter_count;
jeffhaobdb76512011-09-07 11:43:16 -0700564 return true;
565}
566
Ian Rogers776ac1f2012-04-13 23:36:36 -0700567bool MethodVerifier::ScanTryCatchBlocks() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700568 uint32_t tries_size = code_item_->tries_size_;
jeffhaobdb76512011-09-07 11:43:16 -0700569 if (tries_size == 0) {
570 return true;
571 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700572 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Ian Rogers0571d352011-11-03 19:51:38 -0700573 const DexFile::TryItem* tries = DexFile::GetTryItems(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -0700574
575 for (uint32_t idx = 0; idx < tries_size; idx++) {
576 const DexFile::TryItem* try_item = &tries[idx];
577 uint32_t start = try_item->start_addr_;
578 uint32_t end = start + try_item->insn_count_;
jeffhaobdb76512011-09-07 11:43:16 -0700579 if ((start >= end) || (start >= insns_size) || (end > insns_size)) {
jeffhaod5347e02012-03-22 17:25:05 -0700580 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad exception entry: startAddr=" << start
581 << " endAddr=" << end << " (size=" << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700582 return false;
583 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700584 if (!insn_flags_[start].IsOpcode()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700585 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
586 << "'try' block starts inside an instruction (" << start << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700587 return false;
588 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700589 for (uint32_t dex_pc = start; dex_pc < end;
590 dex_pc += insn_flags_[dex_pc].GetLengthInCodeUnits()) {
591 insn_flags_[dex_pc].SetInTry();
jeffhaobdb76512011-09-07 11:43:16 -0700592 }
593 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800594 // Iterate over each of the handlers to verify target addresses.
Ian Rogers0571d352011-11-03 19:51:38 -0700595 const byte* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -0700596 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700597 ClassLinker* linker = Runtime::Current()->GetClassLinker();
jeffhaobdb76512011-09-07 11:43:16 -0700598 for (uint32_t idx = 0; idx < handlers_size; idx++) {
Ian Rogers0571d352011-11-03 19:51:38 -0700599 CatchHandlerIterator iterator(handlers_ptr);
600 for (; iterator.HasNext(); iterator.Next()) {
601 uint32_t dex_pc= iterator.GetHandlerAddress();
Ian Rogersd81871c2011-10-03 13:57:23 -0700602 if (!insn_flags_[dex_pc].IsOpcode()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700603 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
604 << "exception handler starts at bad address (" << dex_pc << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700605 return false;
606 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700607 insn_flags_[dex_pc].SetBranchTarget();
Ian Rogers28ad40d2011-10-27 15:19:26 -0700608 // Ensure exception types are resolved so that they don't need resolution to be delivered,
609 // unresolved exception types will be ignored by exception delivery
Ian Rogers0571d352011-11-03 19:51:38 -0700610 if (iterator.GetHandlerTypeIndex() != DexFile::kDexNoIndex16) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800611 mirror::Class* exception_type = linker->ResolveType(*dex_file_,
612 iterator.GetHandlerTypeIndex(),
Mathieu Chartier590fee92013-09-13 13:46:47 -0700613 *dex_cache_, *class_loader_);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700614 if (exception_type == NULL) {
615 DCHECK(Thread::Current()->IsExceptionPending());
616 Thread::Current()->ClearException();
617 }
618 }
jeffhaobdb76512011-09-07 11:43:16 -0700619 }
Ian Rogers0571d352011-11-03 19:51:38 -0700620 handlers_ptr = iterator.EndDataPointer();
jeffhaobdb76512011-09-07 11:43:16 -0700621 }
jeffhaobdb76512011-09-07 11:43:16 -0700622 return true;
623}
624
Ian Rogers776ac1f2012-04-13 23:36:36 -0700625bool MethodVerifier::VerifyInstructions() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700626 const Instruction* inst = Instruction::At(code_item_->insns_);
jeffhaoba5ebb92011-08-25 17:24:37 -0700627
Ian Rogers0c7abda2012-09-19 13:33:42 -0700628 /* 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 -0700629 insn_flags_[0].SetBranchTarget();
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700630 insn_flags_[0].SetCompileTimeInfoPoint();
Ian Rogersd81871c2011-10-03 13:57:23 -0700631
632 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700633 for (uint32_t dex_pc = 0; dex_pc < insns_size;) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700634 if (!VerifyInstruction(inst, dex_pc)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700635 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -0700636 return false;
637 }
638 /* Flag instructions that are garbage collection points */
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700639 // All invoke points are marked as "Throw" points already.
640 // We are relying on this to also count all the invokes as interesting.
Ian Rogersb8c78592013-07-25 23:52:52 +0000641 if (inst->IsBranch() || inst->IsSwitch() || inst->IsThrow()) {
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700642 insn_flags_[dex_pc].SetCompileTimeInfoPoint();
Ian Rogersb8c78592013-07-25 23:52:52 +0000643 } else if (inst->IsReturn()) {
644 insn_flags_[dex_pc].SetCompileTimeInfoPointAndReturn();
Ian Rogersd81871c2011-10-03 13:57:23 -0700645 }
646 dex_pc += inst->SizeInCodeUnits();
647 inst = inst->Next();
648 }
649 return true;
650}
651
Ian Rogers776ac1f2012-04-13 23:36:36 -0700652bool MethodVerifier::VerifyInstruction(const Instruction* inst, uint32_t code_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700653 bool result = true;
654 switch (inst->GetVerifyTypeArgumentA()) {
655 case Instruction::kVerifyRegA:
Ian Rogers29a26482014-05-02 15:27:29 -0700656 result = result && CheckRegisterIndex(inst->VRegA());
Ian Rogersd81871c2011-10-03 13:57:23 -0700657 break;
658 case Instruction::kVerifyRegAWide:
Ian Rogers29a26482014-05-02 15:27:29 -0700659 result = result && CheckWideRegisterIndex(inst->VRegA());
Ian Rogersd81871c2011-10-03 13:57:23 -0700660 break;
661 }
662 switch (inst->GetVerifyTypeArgumentB()) {
663 case Instruction::kVerifyRegB:
Ian Rogers29a26482014-05-02 15:27:29 -0700664 result = result && CheckRegisterIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700665 break;
666 case Instruction::kVerifyRegBField:
Ian Rogers29a26482014-05-02 15:27:29 -0700667 result = result && CheckFieldIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700668 break;
669 case Instruction::kVerifyRegBMethod:
Ian Rogers29a26482014-05-02 15:27:29 -0700670 result = result && CheckMethodIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700671 break;
672 case Instruction::kVerifyRegBNewInstance:
Ian Rogers29a26482014-05-02 15:27:29 -0700673 result = result && CheckNewInstance(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700674 break;
675 case Instruction::kVerifyRegBString:
Ian Rogers29a26482014-05-02 15:27:29 -0700676 result = result && CheckStringIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700677 break;
678 case Instruction::kVerifyRegBType:
Ian Rogers29a26482014-05-02 15:27:29 -0700679 result = result && CheckTypeIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700680 break;
681 case Instruction::kVerifyRegBWide:
Ian Rogers29a26482014-05-02 15:27:29 -0700682 result = result && CheckWideRegisterIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700683 break;
684 }
685 switch (inst->GetVerifyTypeArgumentC()) {
686 case Instruction::kVerifyRegC:
Ian Rogers29a26482014-05-02 15:27:29 -0700687 result = result && CheckRegisterIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700688 break;
689 case Instruction::kVerifyRegCField:
Ian Rogers29a26482014-05-02 15:27:29 -0700690 result = result && CheckFieldIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700691 break;
692 case Instruction::kVerifyRegCNewArray:
Ian Rogers29a26482014-05-02 15:27:29 -0700693 result = result && CheckNewArray(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700694 break;
695 case Instruction::kVerifyRegCType:
Ian Rogers29a26482014-05-02 15:27:29 -0700696 result = result && CheckTypeIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700697 break;
698 case Instruction::kVerifyRegCWide:
Ian Rogers29a26482014-05-02 15:27:29 -0700699 result = result && CheckWideRegisterIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700700 break;
701 }
702 switch (inst->GetVerifyExtraFlags()) {
703 case Instruction::kVerifyArrayData:
704 result = result && CheckArrayData(code_offset);
705 break;
706 case Instruction::kVerifyBranchTarget:
707 result = result && CheckBranchTarget(code_offset);
708 break;
709 case Instruction::kVerifySwitchTargets:
710 result = result && CheckSwitchTargets(code_offset);
711 break;
Ian Rogers29a26482014-05-02 15:27:29 -0700712 case Instruction::kVerifyVarArg: {
713 uint32_t args[Instruction::kMaxVarArgRegs];
714 inst->GetVarArgs(args);
715 result = result && CheckVarArgRegs(inst->VRegA(), args);
Ian Rogersd81871c2011-10-03 13:57:23 -0700716 break;
Ian Rogers29a26482014-05-02 15:27:29 -0700717 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700718 case Instruction::kVerifyVarArgRange:
Ian Rogers29a26482014-05-02 15:27:29 -0700719 result = result && CheckVarArgRangeRegs(inst->VRegA(), inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700720 break;
721 case Instruction::kVerifyError:
jeffhaod5347e02012-03-22 17:25:05 -0700722 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected opcode " << inst->Name();
Ian Rogersd81871c2011-10-03 13:57:23 -0700723 result = false;
724 break;
725 }
726 return result;
727}
728
Ian Rogers776ac1f2012-04-13 23:36:36 -0700729bool MethodVerifier::CheckRegisterIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700730 if (idx >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700731 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register index out of range (" << idx << " >= "
732 << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700733 return false;
734 }
735 return true;
736}
737
Ian Rogers776ac1f2012-04-13 23:36:36 -0700738bool MethodVerifier::CheckWideRegisterIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700739 if (idx + 1 >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700740 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "wide register index out of range (" << idx
741 << "+1 >= " << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700742 return false;
743 }
744 return true;
745}
746
Ian Rogers776ac1f2012-04-13 23:36:36 -0700747bool MethodVerifier::CheckFieldIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700748 if (idx >= dex_file_->GetHeader().field_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700749 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad field index " << idx << " (max "
750 << dex_file_->GetHeader().field_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700751 return false;
752 }
753 return true;
754}
755
Ian Rogers776ac1f2012-04-13 23:36:36 -0700756bool MethodVerifier::CheckMethodIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700757 if (idx >= dex_file_->GetHeader().method_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700758 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad method index " << idx << " (max "
759 << dex_file_->GetHeader().method_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700760 return false;
761 }
762 return true;
763}
764
Ian Rogers776ac1f2012-04-13 23:36:36 -0700765bool MethodVerifier::CheckNewInstance(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700766 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700767 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
768 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700769 return false;
770 }
771 // We don't need the actual class, just a pointer to the class name.
Ian Rogers0571d352011-11-03 19:51:38 -0700772 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -0700773 if (descriptor[0] != 'L') {
jeffhaod5347e02012-03-22 17:25:05 -0700774 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "can't call new-instance on type '" << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -0700775 return false;
776 }
777 return true;
778}
779
Ian Rogers776ac1f2012-04-13 23:36:36 -0700780bool MethodVerifier::CheckStringIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700781 if (idx >= dex_file_->GetHeader().string_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700782 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad string index " << idx << " (max "
783 << dex_file_->GetHeader().string_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700784 return false;
785 }
786 return true;
787}
788
Ian Rogers776ac1f2012-04-13 23:36:36 -0700789bool MethodVerifier::CheckTypeIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700790 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700791 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
792 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700793 return false;
794 }
795 return true;
796}
797
Ian Rogers776ac1f2012-04-13 23:36:36 -0700798bool MethodVerifier::CheckNewArray(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700799 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700800 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
801 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700802 return false;
803 }
804 int bracket_count = 0;
Ian Rogers0571d352011-11-03 19:51:38 -0700805 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -0700806 const char* cp = descriptor;
807 while (*cp++ == '[') {
808 bracket_count++;
809 }
810 if (bracket_count == 0) {
811 /* The given class must be an array type. */
Brian Carlstrom93c33962013-07-26 10:37:43 -0700812 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
813 << "can't new-array class '" << descriptor << "' (not an array)";
Ian Rogersd81871c2011-10-03 13:57:23 -0700814 return false;
815 } else if (bracket_count > 255) {
816 /* It is illegal to create an array of more than 255 dimensions. */
Brian Carlstrom93c33962013-07-26 10:37:43 -0700817 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
818 << "can't new-array class '" << descriptor << "' (exceeds limit)";
Ian Rogersd81871c2011-10-03 13:57:23 -0700819 return false;
820 }
821 return true;
822}
823
Ian Rogers776ac1f2012-04-13 23:36:36 -0700824bool MethodVerifier::CheckArrayData(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700825 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
826 const uint16_t* insns = code_item_->insns_ + cur_offset;
827 const uint16_t* array_data;
828 int32_t array_data_offset;
829
830 DCHECK_LT(cur_offset, insn_count);
831 /* make sure the start of the array data table is in range */
832 array_data_offset = insns[1] | (((int32_t) insns[2]) << 16);
833 if ((int32_t) cur_offset + array_data_offset < 0 ||
834 cur_offset + array_data_offset + 2 >= insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -0700835 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data start: at " << cur_offset
Brian Carlstrom93c33962013-07-26 10:37:43 -0700836 << ", data offset " << array_data_offset
837 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -0700838 return false;
839 }
840 /* offset to array data table is a relative branch-style offset */
841 array_data = insns + array_data_offset;
842 /* make sure the table is 32-bit aligned */
Ian Rogersef7d42f2014-01-06 12:55:46 -0800843 if ((reinterpret_cast<uintptr_t>(array_data) & 0x03) != 0) {
jeffhaod5347e02012-03-22 17:25:05 -0700844 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned array data table: at " << cur_offset
845 << ", data offset " << array_data_offset;
Ian Rogersd81871c2011-10-03 13:57:23 -0700846 return false;
847 }
848 uint32_t value_width = array_data[1];
Elliott Hughes398f64b2012-03-26 18:05:48 -0700849 uint32_t value_count = *reinterpret_cast<const uint32_t*>(&array_data[2]);
Ian Rogersd81871c2011-10-03 13:57:23 -0700850 uint32_t table_size = 4 + (value_width * value_count + 1) / 2;
851 /* make sure the end of the switch is in range */
852 if (cur_offset + array_data_offset + table_size > insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -0700853 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data end: at " << cur_offset
854 << ", data offset " << array_data_offset << ", end "
855 << cur_offset + array_data_offset + table_size
856 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -0700857 return false;
858 }
859 return true;
860}
861
Ian Rogers776ac1f2012-04-13 23:36:36 -0700862bool MethodVerifier::CheckBranchTarget(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700863 int32_t offset;
864 bool isConditional, selfOkay;
865 if (!GetBranchOffset(cur_offset, &offset, &isConditional, &selfOkay)) {
866 return false;
867 }
868 if (!selfOkay && offset == 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700869 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch offset of zero not allowed at"
870 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -0700871 return false;
872 }
Elliott Hughes81ff3182012-03-23 20:35:56 -0700873 // Check for 32-bit overflow. This isn't strictly necessary if we can depend on the runtime
874 // to have identical "wrap-around" behavior, but it's unwise to depend on that.
Ian Rogersd81871c2011-10-03 13:57:23 -0700875 if (((int64_t) cur_offset + (int64_t) offset) != (int64_t) (cur_offset + offset)) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700876 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch target overflow "
877 << reinterpret_cast<void*>(cur_offset) << " +" << offset;
Ian Rogersd81871c2011-10-03 13:57:23 -0700878 return false;
879 }
880 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
881 int32_t abs_offset = cur_offset + offset;
Brian Carlstrom93c33962013-07-26 10:37:43 -0700882 if (abs_offset < 0 ||
883 (uint32_t) abs_offset >= insn_count ||
884 !insn_flags_[abs_offset].IsOpcode()) {
jeffhaod5347e02012-03-22 17:25:05 -0700885 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid branch target " << offset << " (-> "
Elliott Hughes398f64b2012-03-26 18:05:48 -0700886 << reinterpret_cast<void*>(abs_offset) << ") at "
887 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -0700888 return false;
889 }
890 insn_flags_[abs_offset].SetBranchTarget();
891 return true;
892}
893
Ian Rogers776ac1f2012-04-13 23:36:36 -0700894bool MethodVerifier::GetBranchOffset(uint32_t cur_offset, int32_t* pOffset, bool* pConditional,
Ian Rogersd81871c2011-10-03 13:57:23 -0700895 bool* selfOkay) {
896 const uint16_t* insns = code_item_->insns_ + cur_offset;
897 *pConditional = false;
898 *selfOkay = false;
jeffhaoba5ebb92011-08-25 17:24:37 -0700899 switch (*insns & 0xff) {
900 case Instruction::GOTO:
901 *pOffset = ((int16_t) *insns) >> 8;
jeffhaoba5ebb92011-08-25 17:24:37 -0700902 break;
903 case Instruction::GOTO_32:
904 *pOffset = insns[1] | (((uint32_t) insns[2]) << 16);
jeffhaoba5ebb92011-08-25 17:24:37 -0700905 *selfOkay = true;
906 break;
907 case Instruction::GOTO_16:
908 *pOffset = (int16_t) insns[1];
jeffhaoba5ebb92011-08-25 17:24:37 -0700909 break;
910 case Instruction::IF_EQ:
911 case Instruction::IF_NE:
912 case Instruction::IF_LT:
913 case Instruction::IF_GE:
914 case Instruction::IF_GT:
915 case Instruction::IF_LE:
916 case Instruction::IF_EQZ:
917 case Instruction::IF_NEZ:
918 case Instruction::IF_LTZ:
919 case Instruction::IF_GEZ:
920 case Instruction::IF_GTZ:
921 case Instruction::IF_LEZ:
922 *pOffset = (int16_t) insns[1];
923 *pConditional = true;
jeffhaoba5ebb92011-08-25 17:24:37 -0700924 break;
925 default:
926 return false;
927 break;
928 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700929 return true;
930}
931
Ian Rogers776ac1f2012-04-13 23:36:36 -0700932bool MethodVerifier::CheckSwitchTargets(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700933 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700934 DCHECK_LT(cur_offset, insn_count);
Ian Rogersd81871c2011-10-03 13:57:23 -0700935 const uint16_t* insns = code_item_->insns_ + cur_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -0700936 /* make sure the start of the switch is in range */
Ian Rogersd81871c2011-10-03 13:57:23 -0700937 int32_t switch_offset = insns[1] | ((int32_t) insns[2]) << 16;
938 if ((int32_t) cur_offset + switch_offset < 0 || cur_offset + switch_offset + 2 >= insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -0700939 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch start: at " << cur_offset
Brian Carlstrom93c33962013-07-26 10:37:43 -0700940 << ", switch offset " << switch_offset
941 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -0700942 return false;
943 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700944 /* offset to switch table is a relative branch-style offset */
Ian Rogersd81871c2011-10-03 13:57:23 -0700945 const uint16_t* switch_insns = insns + switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -0700946 /* make sure the table is 32-bit aligned */
Ian Rogersef7d42f2014-01-06 12:55:46 -0800947 if ((reinterpret_cast<uintptr_t>(switch_insns) & 0x03) != 0) {
jeffhaod5347e02012-03-22 17:25:05 -0700948 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned switch table: at " << cur_offset
949 << ", switch offset " << switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -0700950 return false;
951 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700952 uint32_t switch_count = switch_insns[1];
953 int32_t keys_offset, targets_offset;
954 uint16_t expected_signature;
jeffhaoba5ebb92011-08-25 17:24:37 -0700955 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
956 /* 0=sig, 1=count, 2/3=firstKey */
957 targets_offset = 4;
958 keys_offset = -1;
959 expected_signature = Instruction::kPackedSwitchSignature;
960 } else {
961 /* 0=sig, 1=count, 2..count*2 = keys */
962 keys_offset = 2;
963 targets_offset = 2 + 2 * switch_count;
964 expected_signature = Instruction::kSparseSwitchSignature;
965 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700966 uint32_t table_size = targets_offset + switch_count * 2;
jeffhaoba5ebb92011-08-25 17:24:37 -0700967 if (switch_insns[0] != expected_signature) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700968 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
969 << StringPrintf("wrong signature for switch table (%x, wanted %x)",
970 switch_insns[0], expected_signature);
jeffhaoba5ebb92011-08-25 17:24:37 -0700971 return false;
972 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700973 /* make sure the end of the switch is in range */
974 if (cur_offset + switch_offset + table_size > (uint32_t) insn_count) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700975 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch end: at " << cur_offset
976 << ", switch offset " << switch_offset
977 << ", end " << (cur_offset + switch_offset + table_size)
jeffhaod5347e02012-03-22 17:25:05 -0700978 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -0700979 return false;
980 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700981 /* for a sparse switch, verify the keys are in ascending order */
982 if (keys_offset > 0 && switch_count > 1) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700983 int32_t last_key = switch_insns[keys_offset] | (switch_insns[keys_offset + 1] << 16);
984 for (uint32_t targ = 1; targ < switch_count; targ++) {
jeffhaoba5ebb92011-08-25 17:24:37 -0700985 int32_t key = (int32_t) switch_insns[keys_offset + targ * 2] |
986 (int32_t) (switch_insns[keys_offset + targ * 2 + 1] << 16);
987 if (key <= last_key) {
jeffhaod5347e02012-03-22 17:25:05 -0700988 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid packed switch: last key=" << last_key
989 << ", this=" << key;
jeffhaoba5ebb92011-08-25 17:24:37 -0700990 return false;
991 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700992 last_key = key;
993 }
994 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700995 /* verify each switch target */
Ian Rogersd81871c2011-10-03 13:57:23 -0700996 for (uint32_t targ = 0; targ < switch_count; targ++) {
997 int32_t offset = (int32_t) switch_insns[targets_offset + targ * 2] |
998 (int32_t) (switch_insns[targets_offset + targ * 2 + 1] << 16);
999 int32_t abs_offset = cur_offset + offset;
Brian Carlstrom93c33962013-07-26 10:37:43 -07001000 if (abs_offset < 0 ||
1001 abs_offset >= (int32_t) insn_count ||
1002 !insn_flags_[abs_offset].IsOpcode()) {
1003 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch target " << offset
1004 << " (-> " << reinterpret_cast<void*>(abs_offset) << ") at "
1005 << reinterpret_cast<void*>(cur_offset)
1006 << "[" << targ << "]";
jeffhaoba5ebb92011-08-25 17:24:37 -07001007 return false;
1008 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001009 insn_flags_[abs_offset].SetBranchTarget();
1010 }
1011 return true;
1012}
1013
Ian Rogers776ac1f2012-04-13 23:36:36 -07001014bool MethodVerifier::CheckVarArgRegs(uint32_t vA, uint32_t arg[]) {
Ian Rogers29a26482014-05-02 15:27:29 -07001015 if (vA > Instruction::kMaxVarArgRegs) {
jeffhaod5347e02012-03-22 17:25:05 -07001016 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << vA << ") in non-range invoke)";
Ian Rogersd81871c2011-10-03 13:57:23 -07001017 return false;
1018 }
1019 uint16_t registers_size = code_item_->registers_size_;
1020 for (uint32_t idx = 0; idx < vA; idx++) {
jeffhao457cc512012-02-02 16:55:13 -08001021 if (arg[idx] >= registers_size) {
jeffhaod5347e02012-03-22 17:25:05 -07001022 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index (" << arg[idx]
1023 << ") in non-range invoke (>= " << registers_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001024 return false;
1025 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001026 }
1027
1028 return true;
1029}
1030
Ian Rogers776ac1f2012-04-13 23:36:36 -07001031bool MethodVerifier::CheckVarArgRangeRegs(uint32_t vA, uint32_t vC) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001032 uint16_t registers_size = code_item_->registers_size_;
1033 // vA/vC are unsigned 8-bit/16-bit quantities for /range instructions, so there's no risk of
1034 // integer overflow when adding them here.
1035 if (vA + vC > registers_size) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001036 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index " << vA << "+" << vC
1037 << " in range invoke (> " << registers_size << ")";
jeffhaoba5ebb92011-08-25 17:24:37 -07001038 return false;
1039 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001040 return true;
1041}
1042
Ian Rogers776ac1f2012-04-13 23:36:36 -07001043bool MethodVerifier::VerifyCodeFlow() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001044 uint16_t registers_size = code_item_->registers_size_;
1045 uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaobdb76512011-09-07 11:43:16 -07001046
Ian Rogersd81871c2011-10-03 13:57:23 -07001047 if (registers_size * insns_size > 4*1024*1024) {
buzbee4922ef92012-02-24 14:32:20 -08001048 LOG(WARNING) << "warning: method is huge (regs=" << registers_size
1049 << " insns_size=" << insns_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001050 }
1051 /* Create and initialize table holding register status */
Brian Carlstrom93c33962013-07-26 10:37:43 -07001052 reg_table_.Init(kTrackCompilerInterestPoints,
1053 insn_flags_.get(),
1054 insns_size,
1055 registers_size,
1056 this);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07001057
jeffhaobdb76512011-09-07 11:43:16 -07001058
Ian Rogersd0fbd852013-09-24 18:17:04 -07001059 work_line_.reset(RegisterLine::Create(registers_size, this));
1060 saved_line_.reset(RegisterLine::Create(registers_size, this));
jeffhaobdb76512011-09-07 11:43:16 -07001061
Ian Rogersd81871c2011-10-03 13:57:23 -07001062 /* Initialize register types of method arguments. */
1063 if (!SetTypesFromSignature()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001064 DCHECK_NE(failures_.size(), 0U);
1065 std::string prepend("Bad signature in ");
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001066 prepend += PrettyMethod(dex_method_idx_, *dex_file_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001067 PrependToLastFailMessage(prepend);
Ian Rogersd81871c2011-10-03 13:57:23 -07001068 return false;
1069 }
1070 /* Perform code flow verification. */
1071 if (!CodeFlowVerifyMethod()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001072 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -07001073 return false;
jeffhaobdb76512011-09-07 11:43:16 -07001074 }
jeffhaobdb76512011-09-07 11:43:16 -07001075 return true;
1076}
1077
Ian Rogersad0b3a32012-04-16 14:50:24 -07001078std::ostream& MethodVerifier::DumpFailures(std::ostream& os) {
1079 DCHECK_EQ(failures_.size(), failure_messages_.size());
Jeff Hao4137f482013-11-22 11:44:57 -08001080 for (size_t i = 0; i < failures_.size(); ++i) {
1081 os << failure_messages_[i]->str() << "\n";
Ian Rogersad0b3a32012-04-16 14:50:24 -07001082 }
1083 return os;
1084}
1085
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001086extern "C" void MethodVerifierGdbDump(MethodVerifier* v)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001087 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001088 v->Dump(std::cerr);
1089}
1090
Ian Rogers776ac1f2012-04-13 23:36:36 -07001091void MethodVerifier::Dump(std::ostream& os) {
jeffhaof56197c2012-03-05 18:01:54 -08001092 if (code_item_ == NULL) {
Elliott Hughesc073b072012-05-24 19:29:17 -07001093 os << "Native method\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001094 return;
jeffhaobdb76512011-09-07 11:43:16 -07001095 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001096 {
1097 os << "Register Types:\n";
1098 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
1099 std::ostream indent_os(&indent_filter);
1100 reg_types_.Dump(indent_os);
1101 }
Ian Rogersb4903572012-10-11 11:52:56 -07001102 os << "Dumping instructions and register lines:\n";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001103 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
1104 std::ostream indent_os(&indent_filter);
Ian Rogersd81871c2011-10-03 13:57:23 -07001105 const Instruction* inst = Instruction::At(code_item_->insns_);
1106 for (size_t dex_pc = 0; dex_pc < code_item_->insns_size_in_code_units_;
1107 dex_pc += insn_flags_[dex_pc].GetLengthInCodeUnits()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001108 RegisterLine* reg_line = reg_table_.GetLine(dex_pc);
1109 if (reg_line != NULL) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001110 indent_os << reg_line->Dump() << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07001111 }
Ian Rogers7b3ddd22013-02-21 15:19:52 -08001112 indent_os << StringPrintf("0x%04zx", dex_pc) << ": " << insn_flags_[dex_pc].ToString() << " ";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001113 const bool kDumpHexOfInstruction = false;
1114 if (kDumpHexOfInstruction) {
1115 indent_os << inst->DumpHex(5) << " ";
1116 }
1117 indent_os << inst->DumpString(dex_file_) << "\n";
jeffhaoba5ebb92011-08-25 17:24:37 -07001118 inst = inst->Next();
1119 }
jeffhaobdb76512011-09-07 11:43:16 -07001120}
1121
Ian Rogersd81871c2011-10-03 13:57:23 -07001122static bool IsPrimitiveDescriptor(char descriptor) {
1123 switch (descriptor) {
jeffhaobdb76512011-09-07 11:43:16 -07001124 case 'I':
1125 case 'C':
1126 case 'S':
1127 case 'B':
1128 case 'Z':
jeffhaobdb76512011-09-07 11:43:16 -07001129 case 'F':
1130 case 'D':
1131 case 'J':
Ian Rogersd81871c2011-10-03 13:57:23 -07001132 return true;
jeffhaobdb76512011-09-07 11:43:16 -07001133 default:
1134 return false;
1135 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001136}
1137
Ian Rogers776ac1f2012-04-13 23:36:36 -07001138bool MethodVerifier::SetTypesFromSignature() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001139 RegisterLine* reg_line = reg_table_.GetLine(0);
1140 int arg_start = code_item_->registers_size_ - code_item_->ins_size_;
1141 size_t expected_args = code_item_->ins_size_; /* long/double count as two */
jeffhaobdb76512011-09-07 11:43:16 -07001142
Ian Rogersd81871c2011-10-03 13:57:23 -07001143 DCHECK_GE(arg_start, 0); /* should have been verified earlier */
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001144 // Include the "this" pointer.
Ian Rogersd81871c2011-10-03 13:57:23 -07001145 size_t cur_arg = 0;
Ian Rogersad0b3a32012-04-16 14:50:24 -07001146 if (!IsStatic()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001147 // If this is a constructor for a class other than java.lang.Object, mark the first ("this")
1148 // argument as uninitialized. This restricts field access until the superclass constructor is
1149 // called.
Ian Rogersad0b3a32012-04-16 14:50:24 -07001150 const RegType& declaring_class = GetDeclaringClass();
1151 if (IsConstructor() && !declaring_class.IsJavaLangObject()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001152 reg_line->SetRegisterType(arg_start + cur_arg,
1153 reg_types_.UninitializedThisArgument(declaring_class));
1154 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001155 reg_line->SetRegisterType(arg_start + cur_arg, declaring_class);
jeffhaobdb76512011-09-07 11:43:16 -07001156 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001157 cur_arg++;
jeffhaobdb76512011-09-07 11:43:16 -07001158 }
1159
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001160 const DexFile::ProtoId& proto_id =
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001161 dex_file_->GetMethodPrototype(dex_file_->GetMethodId(dex_method_idx_));
Ian Rogers0571d352011-11-03 19:51:38 -07001162 DexFileParameterIterator iterator(*dex_file_, proto_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07001163
1164 for (; iterator.HasNext(); iterator.Next()) {
1165 const char* descriptor = iterator.GetDescriptor();
1166 if (descriptor == NULL) {
1167 LOG(FATAL) << "Null descriptor";
1168 }
1169 if (cur_arg >= expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07001170 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1171 << " args, found more (" << descriptor << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001172 return false;
1173 }
1174 switch (descriptor[0]) {
1175 case 'L':
1176 case '[':
1177 // We assume that reference arguments are initialized. The only way it could be otherwise
1178 // (assuming the caller was verified) is if the current method is <init>, but in that case
1179 // it's effectively considered initialized the instant we reach here (in the sense that we
1180 // can return without doing anything or call virtual methods).
1181 {
Sebastien Hertz2ed76f92014-04-22 17:11:08 +02001182 const RegType& reg_type = ResolveClassAndCheckAccess(iterator.GetTypeIdx());
1183 if (!reg_type.IsNonZeroReferenceTypes()) {
1184 DCHECK(HasFailures());
1185 return false;
1186 }
Ian Rogers84fa0742011-10-25 18:13:30 -07001187 reg_line->SetRegisterType(arg_start + cur_arg, reg_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001188 }
1189 break;
1190 case 'Z':
1191 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Boolean());
1192 break;
1193 case 'C':
1194 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Char());
1195 break;
1196 case 'B':
1197 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Byte());
1198 break;
1199 case 'I':
1200 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Integer());
1201 break;
1202 case 'S':
1203 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Short());
1204 break;
1205 case 'F':
1206 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Float());
1207 break;
1208 case 'J':
1209 case 'D': {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001210 const RegType& lo_half = descriptor[0] == 'J' ? reg_types_.LongLo() : reg_types_.DoubleLo();
1211 const RegType& hi_half = descriptor[0] == 'J' ? reg_types_.LongHi() : reg_types_.DoubleHi();
1212 reg_line->SetRegisterTypeWide(arg_start + cur_arg, lo_half, hi_half);
Ian Rogersd81871c2011-10-03 13:57:23 -07001213 cur_arg++;
1214 break;
1215 }
1216 default:
Brian Carlstrom93c33962013-07-26 10:37:43 -07001217 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected signature type char '"
1218 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001219 return false;
1220 }
1221 cur_arg++;
1222 }
1223 if (cur_arg != expected_args) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001224 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1225 << " arguments, found " << cur_arg;
Ian Rogersd81871c2011-10-03 13:57:23 -07001226 return false;
1227 }
1228 const char* descriptor = dex_file_->GetReturnTypeDescriptor(proto_id);
1229 // Validate return type. We don't do the type lookup; just want to make sure that it has the right
1230 // format. Only major difference from the method argument format is that 'V' is supported.
1231 bool result;
1232 if (IsPrimitiveDescriptor(descriptor[0]) || descriptor[0] == 'V') {
1233 result = descriptor[1] == '\0';
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001234 } else if (descriptor[0] == '[') { // single/multi-dimensional array of object/primitive
Ian Rogersd81871c2011-10-03 13:57:23 -07001235 size_t i = 0;
1236 do {
1237 i++;
1238 } while (descriptor[i] == '['); // process leading [
1239 if (descriptor[i] == 'L') { // object array
1240 do {
1241 i++; // find closing ;
1242 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1243 result = descriptor[i] == ';';
1244 } else { // primitive array
1245 result = IsPrimitiveDescriptor(descriptor[i]) && descriptor[i + 1] == '\0';
1246 }
1247 } else if (descriptor[0] == 'L') {
1248 // could be more thorough here, but shouldn't be required
1249 size_t i = 0;
1250 do {
1251 i++;
1252 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1253 result = descriptor[i] == ';';
1254 } else {
1255 result = false;
1256 }
1257 if (!result) {
jeffhaod5347e02012-03-22 17:25:05 -07001258 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected char in return type descriptor '"
1259 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001260 }
1261 return result;
jeffhaobdb76512011-09-07 11:43:16 -07001262}
1263
Ian Rogers776ac1f2012-04-13 23:36:36 -07001264bool MethodVerifier::CodeFlowVerifyMethod() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001265 const uint16_t* insns = code_item_->insns_;
1266 const uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaoba5ebb92011-08-25 17:24:37 -07001267
jeffhaobdb76512011-09-07 11:43:16 -07001268 /* Begin by marking the first instruction as "changed". */
Ian Rogersd81871c2011-10-03 13:57:23 -07001269 insn_flags_[0].SetChanged();
1270 uint32_t start_guess = 0;
jeffhaoba5ebb92011-08-25 17:24:37 -07001271
jeffhaobdb76512011-09-07 11:43:16 -07001272 /* Continue until no instructions are marked "changed". */
1273 while (true) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001274 // Find the first marked one. Use "start_guess" as a way to find one quickly.
1275 uint32_t insn_idx = start_guess;
1276 for (; insn_idx < insns_size; insn_idx++) {
1277 if (insn_flags_[insn_idx].IsChanged())
jeffhaobdb76512011-09-07 11:43:16 -07001278 break;
1279 }
jeffhaobdb76512011-09-07 11:43:16 -07001280 if (insn_idx == insns_size) {
1281 if (start_guess != 0) {
1282 /* try again, starting from the top */
1283 start_guess = 0;
1284 continue;
1285 } else {
1286 /* all flags are clear */
1287 break;
1288 }
1289 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001290 // We carry the working set of registers from instruction to instruction. If this address can
1291 // be the target of a branch (or throw) instruction, or if we're skipping around chasing
1292 // "changed" flags, we need to load the set of registers from the table.
1293 // Because we always prefer to continue on to the next instruction, we should never have a
1294 // situation where we have a stray "changed" flag set on an instruction that isn't a branch
1295 // target.
1296 work_insn_idx_ = insn_idx;
1297 if (insn_flags_[insn_idx].IsBranchTarget()) {
1298 work_line_->CopyFromLine(reg_table_.GetLine(insn_idx));
jeffhaobdb76512011-09-07 11:43:16 -07001299 } else {
1300#ifndef NDEBUG
1301 /*
1302 * Sanity check: retrieve the stored register line (assuming
1303 * a full table) and make sure it actually matches.
1304 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001305 RegisterLine* register_line = reg_table_.GetLine(insn_idx);
1306 if (register_line != NULL) {
1307 if (work_line_->CompareLine(register_line) != 0) {
1308 Dump(std::cout);
1309 std::cout << info_messages_.str();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001310 LOG(FATAL) << "work_line diverged in " << PrettyMethod(dex_method_idx_, *dex_file_)
Elliott Hughesc073b072012-05-24 19:29:17 -07001311 << "@" << reinterpret_cast<void*>(work_insn_idx_) << "\n"
1312 << " work_line=" << *work_line_ << "\n"
Elliott Hughes398f64b2012-03-26 18:05:48 -07001313 << " expected=" << *register_line;
Ian Rogersd81871c2011-10-03 13:57:23 -07001314 }
jeffhaobdb76512011-09-07 11:43:16 -07001315 }
1316#endif
1317 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001318 if (!CodeFlowVerifyInstruction(&start_guess)) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001319 std::string prepend(PrettyMethod(dex_method_idx_, *dex_file_));
Ian Rogersad0b3a32012-04-16 14:50:24 -07001320 prepend += " failed to verify: ";
1321 PrependToLastFailMessage(prepend);
jeffhaoba5ebb92011-08-25 17:24:37 -07001322 return false;
1323 }
jeffhaobdb76512011-09-07 11:43:16 -07001324 /* Clear "changed" and mark as visited. */
Ian Rogersd81871c2011-10-03 13:57:23 -07001325 insn_flags_[insn_idx].SetVisited();
1326 insn_flags_[insn_idx].ClearChanged();
jeffhaobdb76512011-09-07 11:43:16 -07001327 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001328
Ian Rogers1c849e52012-06-28 14:00:33 -07001329 if (gDebugVerify) {
jeffhaobdb76512011-09-07 11:43:16 -07001330 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001331 * Scan for dead code. There's nothing "evil" about dead code
jeffhaobdb76512011-09-07 11:43:16 -07001332 * (besides the wasted space), but it indicates a flaw somewhere
1333 * down the line, possibly in the verifier.
1334 *
1335 * If we've substituted "always throw" instructions into the stream,
1336 * we are almost certainly going to have some dead code.
1337 */
1338 int dead_start = -1;
Ian Rogersd81871c2011-10-03 13:57:23 -07001339 uint32_t insn_idx = 0;
1340 for (; insn_idx < insns_size; insn_idx += insn_flags_[insn_idx].GetLengthInCodeUnits()) {
jeffhaobdb76512011-09-07 11:43:16 -07001341 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001342 * Switch-statement data doesn't get "visited" by scanner. It
jeffhaobdb76512011-09-07 11:43:16 -07001343 * may or may not be preceded by a padding NOP (for alignment).
1344 */
1345 if (insns[insn_idx] == Instruction::kPackedSwitchSignature ||
1346 insns[insn_idx] == Instruction::kSparseSwitchSignature ||
1347 insns[insn_idx] == Instruction::kArrayDataSignature ||
Elliott Hughes380aaa72012-07-09 14:33:15 -07001348 (insns[insn_idx] == Instruction::NOP && (insn_idx + 1 < insns_size) &&
jeffhaobdb76512011-09-07 11:43:16 -07001349 (insns[insn_idx + 1] == Instruction::kPackedSwitchSignature ||
1350 insns[insn_idx + 1] == Instruction::kSparseSwitchSignature ||
1351 insns[insn_idx + 1] == Instruction::kArrayDataSignature))) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001352 insn_flags_[insn_idx].SetVisited();
jeffhaobdb76512011-09-07 11:43:16 -07001353 }
1354
Ian Rogersd81871c2011-10-03 13:57:23 -07001355 if (!insn_flags_[insn_idx].IsVisited()) {
jeffhaobdb76512011-09-07 11:43:16 -07001356 if (dead_start < 0)
1357 dead_start = insn_idx;
1358 } else if (dead_start >= 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001359 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start)
1360 << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaobdb76512011-09-07 11:43:16 -07001361 dead_start = -1;
1362 }
1363 }
1364 if (dead_start >= 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001365 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start)
1366 << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaoba5ebb92011-08-25 17:24:37 -07001367 }
Ian Rogersc9e463c2013-06-05 16:52:26 -07001368 // To dump the state of the verify after a method, do something like:
1369 // if (PrettyMethod(dex_method_idx_, *dex_file_) ==
1370 // "boolean java.lang.String.equals(java.lang.Object)") {
1371 // LOG(INFO) << info_messages_.str();
1372 // }
jeffhaoba5ebb92011-08-25 17:24:37 -07001373 }
jeffhaobdb76512011-09-07 11:43:16 -07001374 return true;
1375}
1376
Ian Rogers776ac1f2012-04-13 23:36:36 -07001377bool MethodVerifier::CodeFlowVerifyInstruction(uint32_t* start_guess) {
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001378 // If we're doing FindLocksAtDexPc, check whether we're at the dex pc we care about.
1379 // We want the state _before_ the instruction, for the case where the dex pc we're
1380 // interested in is itself a monitor-enter instruction (which is a likely place
1381 // for a thread to be suspended).
1382 if (monitor_enter_dex_pcs_ != NULL && work_insn_idx_ == interesting_dex_pc_) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001383 monitor_enter_dex_pcs_->clear(); // The new work line is more accurate than the previous one.
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001384 for (size_t i = 0; i < work_line_->GetMonitorEnterCount(); ++i) {
1385 monitor_enter_dex_pcs_->push_back(work_line_->GetMonitorEnterDexPc(i));
1386 }
1387 }
1388
jeffhaobdb76512011-09-07 11:43:16 -07001389 /*
1390 * Once we finish decoding the instruction, we need to figure out where
jeffhaod1f0fde2011-09-08 17:25:33 -07001391 * we can go from here. There are three possible ways to transfer
jeffhaobdb76512011-09-07 11:43:16 -07001392 * control to another statement:
1393 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001394 * (1) Continue to the next instruction. Applies to all but
jeffhaobdb76512011-09-07 11:43:16 -07001395 * unconditional branches, method returns, and exception throws.
jeffhaod1f0fde2011-09-08 17:25:33 -07001396 * (2) Branch to one or more possible locations. Applies to branches
jeffhaobdb76512011-09-07 11:43:16 -07001397 * and switch statements.
jeffhaod1f0fde2011-09-08 17:25:33 -07001398 * (3) Exception handlers. Applies to any instruction that can
jeffhaobdb76512011-09-07 11:43:16 -07001399 * throw an exception that is handled by an encompassing "try"
1400 * block.
1401 *
1402 * We can also return, in which case there is no successor instruction
1403 * from this point.
1404 *
Elliott Hughesadb8c672012-03-06 16:49:32 -08001405 * The behavior can be determined from the opcode flags.
jeffhaobdb76512011-09-07 11:43:16 -07001406 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001407 const uint16_t* insns = code_item_->insns_ + work_insn_idx_;
1408 const Instruction* inst = Instruction::At(insns);
Ian Rogersa75a0132012-09-28 11:41:42 -07001409 int opcode_flags = Instruction::FlagsOf(inst->Opcode());
jeffhaobdb76512011-09-07 11:43:16 -07001410
jeffhaobdb76512011-09-07 11:43:16 -07001411 int32_t branch_target = 0;
jeffhaobdb76512011-09-07 11:43:16 -07001412 bool just_set_result = false;
Ian Rogers2c8a8572011-10-24 17:11:36 -07001413 if (gDebugVerify) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001414 // Generate processing back trace to debug verifier
Elliott Hughesc073b072012-05-24 19:29:17 -07001415 LogVerifyInfo() << "Processing " << inst->DumpString(dex_file_) << "\n"
1416 << *work_line_.get() << "\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001417 }
jeffhaobdb76512011-09-07 11:43:16 -07001418
1419 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001420 * Make a copy of the previous register state. If the instruction
jeffhaobdb76512011-09-07 11:43:16 -07001421 * can throw an exception, we will copy/merge this into the "catch"
1422 * address rather than work_line, because we don't want the result
1423 * from the "successful" code path (e.g. a check-cast that "improves"
1424 * a type) to be visible to the exception handler.
1425 */
Ian Rogers776ac1f2012-04-13 23:36:36 -07001426 if ((opcode_flags & Instruction::kThrow) != 0 && CurrentInsnFlags()->IsInTry()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001427 saved_line_->CopyFromLine(work_line_.get());
jeffhaobdb76512011-09-07 11:43:16 -07001428 } else {
1429#ifndef NDEBUG
Ian Rogersd81871c2011-10-03 13:57:23 -07001430 saved_line_->FillWithGarbage();
jeffhaobdb76512011-09-07 11:43:16 -07001431#endif
1432 }
1433
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07001434
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001435 // We need to ensure the work line is consistent while performing validation. When we spot a
1436 // peephole pattern we compute a new line for either the fallthrough instruction or the
1437 // branch target.
Ian Rogers700a4022014-05-19 16:49:03 -07001438 std::unique_ptr<RegisterLine> branch_line;
1439 std::unique_ptr<RegisterLine> fallthrough_line;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001440
Sebastien Hertz849600b2013-12-20 10:28:08 +01001441 // We need precise constant types only for deoptimization which happens at runtime.
1442 const bool need_precise_constant = !Runtime::Current()->IsCompiler();
1443
Sebastien Hertz5243e912013-05-21 10:55:07 +02001444 switch (inst->Opcode()) {
jeffhaobdb76512011-09-07 11:43:16 -07001445 case Instruction::NOP:
1446 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001447 * A "pure" NOP has no effect on anything. Data tables start with
jeffhaobdb76512011-09-07 11:43:16 -07001448 * a signature that looks like a NOP; if we see one of these in
1449 * the course of executing code then we have a problem.
1450 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001451 if (inst->VRegA_10x() != 0) {
jeffhaod5347e02012-03-22 17:25:05 -07001452 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "encountered data table in instruction stream";
jeffhaobdb76512011-09-07 11:43:16 -07001453 }
1454 break;
1455
1456 case Instruction::MOVE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001457 work_line_->CopyRegister1(inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategory1nr);
1458 break;
jeffhaobdb76512011-09-07 11:43:16 -07001459 case Instruction::MOVE_FROM16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001460 work_line_->CopyRegister1(inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategory1nr);
1461 break;
jeffhaobdb76512011-09-07 11:43:16 -07001462 case Instruction::MOVE_16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001463 work_line_->CopyRegister1(inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategory1nr);
jeffhaobdb76512011-09-07 11:43:16 -07001464 break;
1465 case Instruction::MOVE_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001466 work_line_->CopyRegister2(inst->VRegA_12x(), inst->VRegB_12x());
1467 break;
jeffhaobdb76512011-09-07 11:43:16 -07001468 case Instruction::MOVE_WIDE_FROM16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001469 work_line_->CopyRegister2(inst->VRegA_22x(), inst->VRegB_22x());
1470 break;
jeffhaobdb76512011-09-07 11:43:16 -07001471 case Instruction::MOVE_WIDE_16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001472 work_line_->CopyRegister2(inst->VRegA_32x(), inst->VRegB_32x());
jeffhaobdb76512011-09-07 11:43:16 -07001473 break;
1474 case Instruction::MOVE_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001475 work_line_->CopyRegister1(inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategoryRef);
1476 break;
jeffhaobdb76512011-09-07 11:43:16 -07001477 case Instruction::MOVE_OBJECT_FROM16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001478 work_line_->CopyRegister1(inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategoryRef);
1479 break;
jeffhaobdb76512011-09-07 11:43:16 -07001480 case Instruction::MOVE_OBJECT_16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001481 work_line_->CopyRegister1(inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategoryRef);
jeffhaobdb76512011-09-07 11:43:16 -07001482 break;
1483
1484 /*
1485 * The move-result instructions copy data out of a "pseudo-register"
jeffhaod1f0fde2011-09-08 17:25:33 -07001486 * with the results from the last method invocation. In practice we
jeffhaobdb76512011-09-07 11:43:16 -07001487 * might want to hold the result in an actual CPU register, so the
1488 * Dalvik spec requires that these only appear immediately after an
1489 * invoke or filled-new-array.
1490 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001491 * These calls invalidate the "result" register. (This is now
jeffhaobdb76512011-09-07 11:43:16 -07001492 * redundant with the reset done below, but it can make the debug info
1493 * easier to read in some cases.)
1494 */
1495 case Instruction::MOVE_RESULT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001496 work_line_->CopyResultRegister1(inst->VRegA_11x(), false);
jeffhaobdb76512011-09-07 11:43:16 -07001497 break;
1498 case Instruction::MOVE_RESULT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001499 work_line_->CopyResultRegister2(inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07001500 break;
1501 case Instruction::MOVE_RESULT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001502 work_line_->CopyResultRegister1(inst->VRegA_11x(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001503 break;
1504
Ian Rogersd81871c2011-10-03 13:57:23 -07001505 case Instruction::MOVE_EXCEPTION: {
jeffhaobdb76512011-09-07 11:43:16 -07001506 /*
jeffhao60f83e32012-02-13 17:16:30 -08001507 * This statement can only appear as the first instruction in an exception handler. We verify
1508 * that as part of extracting the exception type from the catch block list.
jeffhaobdb76512011-09-07 11:43:16 -07001509 */
Ian Rogers28ad40d2011-10-27 15:19:26 -07001510 const RegType& res_type = GetCaughtExceptionType();
Sebastien Hertz5243e912013-05-21 10:55:07 +02001511 work_line_->SetRegisterType(inst->VRegA_11x(), res_type);
jeffhaobdb76512011-09-07 11:43:16 -07001512 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001513 }
jeffhaobdb76512011-09-07 11:43:16 -07001514 case Instruction::RETURN_VOID:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001515 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
1516 if (!GetMethodReturnType().IsConflict()) {
jeffhaod5347e02012-03-22 17:25:05 -07001517 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001518 }
jeffhaobdb76512011-09-07 11:43:16 -07001519 }
1520 break;
1521 case Instruction::RETURN:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001522 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
jeffhaobdb76512011-09-07 11:43:16 -07001523 /* check the method signature */
Ian Rogersd81871c2011-10-03 13:57:23 -07001524 const RegType& return_type = GetMethodReturnType();
1525 if (!return_type.IsCategory1Types()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001526 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected non-category 1 return type "
1527 << return_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001528 } else {
1529 // Compilers may generate synthetic functions that write byte values into boolean fields.
1530 // Also, it may use integer values for boolean, byte, short, and character return types.
Sebastien Hertz5243e912013-05-21 10:55:07 +02001531 const uint32_t vregA = inst->VRegA_11x();
1532 const RegType& src_type = work_line_->GetRegisterType(vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07001533 bool use_src = ((return_type.IsBoolean() && src_type.IsByte()) ||
1534 ((return_type.IsBoolean() || return_type.IsByte() ||
1535 return_type.IsShort() || return_type.IsChar()) &&
1536 src_type.IsInteger()));
1537 /* check the register contents */
Ian Rogersad0b3a32012-04-16 14:50:24 -07001538 bool success =
Sebastien Hertz5243e912013-05-21 10:55:07 +02001539 work_line_->VerifyRegisterType(vregA, use_src ? src_type : return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001540 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001541 AppendToLastFailMessage(StringPrintf(" return-1nr on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07001542 }
jeffhaobdb76512011-09-07 11:43:16 -07001543 }
1544 }
1545 break;
1546 case Instruction::RETURN_WIDE:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001547 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
jeffhaobdb76512011-09-07 11:43:16 -07001548 /* check the method signature */
Ian Rogersd81871c2011-10-03 13:57:23 -07001549 const RegType& return_type = GetMethodReturnType();
1550 if (!return_type.IsCategory2Types()) {
jeffhaod5347e02012-03-22 17:25:05 -07001551 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-wide not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001552 } else {
1553 /* check the register contents */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001554 const uint32_t vregA = inst->VRegA_11x();
1555 bool success = work_line_->VerifyRegisterType(vregA, return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001556 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001557 AppendToLastFailMessage(StringPrintf(" return-wide on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07001558 }
jeffhaobdb76512011-09-07 11:43:16 -07001559 }
1560 }
1561 break;
1562 case Instruction::RETURN_OBJECT:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001563 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001564 const RegType& return_type = GetMethodReturnType();
1565 if (!return_type.IsReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001566 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-object not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001567 } else {
1568 /* return_type is the *expected* return type, not register value */
1569 DCHECK(!return_type.IsZero());
1570 DCHECK(!return_type.IsUninitializedReference());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001571 const uint32_t vregA = inst->VRegA_11x();
1572 const RegType& reg_type = work_line_->GetRegisterType(vregA);
Ian Rogers9074b992011-10-26 17:41:55 -07001573 // Disallow returning uninitialized values and verify that the reference in vAA is an
1574 // instance of the "return_type"
1575 if (reg_type.IsUninitializedTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001576 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "returning uninitialized object '"
1577 << reg_type << "'";
Ian Rogers9074b992011-10-26 17:41:55 -07001578 } else if (!return_type.IsAssignableFrom(reg_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07001579 if (reg_type.IsUnresolvedTypes() || return_type.IsUnresolvedTypes()) {
1580 Fail(VERIFY_ERROR_NO_CLASS) << " can't resolve returned type '" << return_type
1581 << "' or '" << reg_type << "'";
1582 } else {
1583 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning '" << reg_type
1584 << "', but expected from declaration '" << return_type << "'";
1585 }
jeffhaobdb76512011-09-07 11:43:16 -07001586 }
1587 }
1588 }
1589 break;
1590
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001591 /* could be boolean, int, float, or a null reference */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001592 case Instruction::CONST_4: {
1593 int32_t val = static_cast<int32_t>(inst->VRegB_11n() << 28) >> 28;
Sebastien Hertz849600b2013-12-20 10:28:08 +01001594 work_line_->SetRegisterType(inst->VRegA_11n(),
1595 DetermineCat1Constant(val, need_precise_constant));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001596 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02001597 }
1598 case Instruction::CONST_16: {
1599 int16_t val = static_cast<int16_t>(inst->VRegB_21s());
Sebastien Hertz849600b2013-12-20 10:28:08 +01001600 work_line_->SetRegisterType(inst->VRegA_21s(),
1601 DetermineCat1Constant(val, need_precise_constant));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001602 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02001603 }
Sebastien Hertz849600b2013-12-20 10:28:08 +01001604 case Instruction::CONST: {
1605 int32_t val = inst->VRegB_31i();
Sebastien Hertz5243e912013-05-21 10:55:07 +02001606 work_line_->SetRegisterType(inst->VRegA_31i(),
Sebastien Hertz849600b2013-12-20 10:28:08 +01001607 DetermineCat1Constant(val, need_precise_constant));
jeffhaobdb76512011-09-07 11:43:16 -07001608 break;
Sebastien Hertz849600b2013-12-20 10:28:08 +01001609 }
1610 case Instruction::CONST_HIGH16: {
1611 int32_t val = static_cast<int32_t>(inst->VRegB_21h() << 16);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001612 work_line_->SetRegisterType(inst->VRegA_21h(),
Sebastien Hertz849600b2013-12-20 10:28:08 +01001613 DetermineCat1Constant(val, need_precise_constant));
jeffhaobdb76512011-09-07 11:43:16 -07001614 break;
Sebastien Hertz849600b2013-12-20 10:28:08 +01001615 }
jeffhaobdb76512011-09-07 11:43:16 -07001616 /* could be long or double; resolved upon use */
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001617 case Instruction::CONST_WIDE_16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001618 int64_t val = static_cast<int16_t>(inst->VRegB_21s());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001619 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1620 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001621 work_line_->SetRegisterTypeWide(inst->VRegA_21s(), lo, hi);
jeffhaobdb76512011-09-07 11:43:16 -07001622 break;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001623 }
1624 case Instruction::CONST_WIDE_32: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001625 int64_t val = static_cast<int32_t>(inst->VRegB_31i());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001626 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1627 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001628 work_line_->SetRegisterTypeWide(inst->VRegA_31i(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001629 break;
1630 }
1631 case Instruction::CONST_WIDE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001632 int64_t val = inst->VRegB_51l();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001633 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1634 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001635 work_line_->SetRegisterTypeWide(inst->VRegA_51l(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001636 break;
1637 }
1638 case Instruction::CONST_WIDE_HIGH16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001639 int64_t val = static_cast<uint64_t>(inst->VRegB_21h()) << 48;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001640 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1641 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001642 work_line_->SetRegisterTypeWide(inst->VRegA_21h(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001643 break;
1644 }
jeffhaobdb76512011-09-07 11:43:16 -07001645 case Instruction::CONST_STRING:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001646 work_line_->SetRegisterType(inst->VRegA_21c(), reg_types_.JavaLangString());
1647 break;
jeffhaobdb76512011-09-07 11:43:16 -07001648 case Instruction::CONST_STRING_JUMBO:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001649 work_line_->SetRegisterType(inst->VRegA_31c(), reg_types_.JavaLangString());
jeffhaobdb76512011-09-07 11:43:16 -07001650 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001651 case Instruction::CONST_CLASS: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001652 // Get type from instruction if unresolved then we need an access check
1653 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Sebastien Hertz5243e912013-05-21 10:55:07 +02001654 const RegType& res_type = ResolveClassAndCheckAccess(inst->VRegB_21c());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001655 // Register holds class, ie its type is class, on error it will hold Conflict.
Sebastien Hertz5243e912013-05-21 10:55:07 +02001656 work_line_->SetRegisterType(inst->VRegA_21c(),
Ian Rogersb4903572012-10-11 11:52:56 -07001657 res_type.IsConflict() ? res_type
1658 : reg_types_.JavaLangClass(true));
jeffhaobdb76512011-09-07 11:43:16 -07001659 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001660 }
jeffhaobdb76512011-09-07 11:43:16 -07001661 case Instruction::MONITOR_ENTER:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001662 work_line_->PushMonitor(inst->VRegA_11x(), work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07001663 break;
1664 case Instruction::MONITOR_EXIT:
1665 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001666 * monitor-exit instructions are odd. They can throw exceptions,
jeffhaobdb76512011-09-07 11:43:16 -07001667 * but when they do they act as if they succeeded and the PC is
jeffhaod1f0fde2011-09-08 17:25:33 -07001668 * pointing to the following instruction. (This behavior goes back
jeffhaobdb76512011-09-07 11:43:16 -07001669 * to the need to handle asynchronous exceptions, a now-deprecated
1670 * feature that Dalvik doesn't support.)
1671 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001672 * In practice we don't need to worry about this. The only
jeffhaobdb76512011-09-07 11:43:16 -07001673 * exceptions that can be thrown from monitor-exit are for a
jeffhaod1f0fde2011-09-08 17:25:33 -07001674 * null reference and -exit without a matching -enter. If the
jeffhaobdb76512011-09-07 11:43:16 -07001675 * structured locking checks are working, the former would have
1676 * failed on the -enter instruction, and the latter is impossible.
1677 *
1678 * This is fortunate, because issue 3221411 prevents us from
1679 * chasing the "can throw" path when monitor verification is
jeffhaod1f0fde2011-09-08 17:25:33 -07001680 * enabled. If we can fully verify the locking we can ignore
jeffhaobdb76512011-09-07 11:43:16 -07001681 * some catch blocks (which will show up as "dead" code when
1682 * we skip them here); if we can't, then the code path could be
1683 * "live" so we still need to check it.
1684 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001685 opcode_flags &= ~Instruction::kThrow;
Sebastien Hertz5243e912013-05-21 10:55:07 +02001686 work_line_->PopMonitor(inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07001687 break;
1688
Ian Rogers28ad40d2011-10-27 15:19:26 -07001689 case Instruction::CHECK_CAST:
Ian Rogersd81871c2011-10-03 13:57:23 -07001690 case Instruction::INSTANCE_OF: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001691 /*
1692 * If this instruction succeeds, we will "downcast" register vA to the type in vB. (This
1693 * could be a "upcast" -- not expected, so we don't try to address it.)
1694 *
1695 * If it fails, an exception is thrown, which we deal with later by ignoring the update to
Elliott Hughesadb8c672012-03-06 16:49:32 -08001696 * dec_insn.vA when branching to a handler.
Ian Rogers28ad40d2011-10-27 15:19:26 -07001697 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001698 const bool is_checkcast = (inst->Opcode() == Instruction::CHECK_CAST);
1699 const uint32_t type_idx = (is_checkcast) ? inst->VRegB_21c() : inst->VRegC_22c();
1700 const RegType& res_type = ResolveClassAndCheckAccess(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001701 if (res_type.IsConflict()) {
1702 DCHECK_NE(failures_.size(), 0U);
1703 if (!is_checkcast) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001704 work_line_->SetRegisterType(inst->VRegA_22c(), reg_types_.Boolean());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001705 }
1706 break; // bad class
Ian Rogers9f1ab122011-12-12 08:52:43 -08001707 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001708 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Sebastien Hertz5243e912013-05-21 10:55:07 +02001709 uint32_t orig_type_reg = (is_checkcast) ? inst->VRegA_21c() : inst->VRegB_22c();
1710 const RegType& orig_type = work_line_->GetRegisterType(orig_type_reg);
Ian Rogers28ad40d2011-10-27 15:19:26 -07001711 if (!res_type.IsNonZeroReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001712 if (is_checkcast) {
1713 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on unexpected class " << res_type;
1714 } else {
1715 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on unexpected class " << res_type;
1716 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001717 } else if (!orig_type.IsReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001718 if (is_checkcast) {
1719 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on non-reference in v" << orig_type_reg;
1720 } else {
1721 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on non-reference in v" << orig_type_reg;
1722 }
jeffhao2a8a90e2011-09-26 14:25:31 -07001723 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001724 if (is_checkcast) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001725 work_line_->SetRegisterType(inst->VRegA_21c(), res_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001726 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001727 work_line_->SetRegisterType(inst->VRegA_22c(), reg_types_.Boolean());
jeffhaobdb76512011-09-07 11:43:16 -07001728 }
jeffhaobdb76512011-09-07 11:43:16 -07001729 }
jeffhao2a8a90e2011-09-26 14:25:31 -07001730 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001731 }
1732 case Instruction::ARRAY_LENGTH: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001733 const RegType& res_type = work_line_->GetRegisterType(inst->VRegB_12x());
Ian Rogers28ad40d2011-10-27 15:19:26 -07001734 if (res_type.IsReferenceTypes()) {
Ian Rogers89310de2012-02-01 13:47:30 -08001735 if (!res_type.IsArrayTypes() && !res_type.IsZero()) { // ie not an array or null
jeffhaod5347e02012-03-22 17:25:05 -07001736 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001737 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001738 work_line_->SetRegisterType(inst->VRegA_12x(), reg_types_.Integer());
Ian Rogersd81871c2011-10-03 13:57:23 -07001739 }
1740 }
1741 break;
1742 }
1743 case Instruction::NEW_INSTANCE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001744 const RegType& res_type = ResolveClassAndCheckAccess(inst->VRegB_21c());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001745 if (res_type.IsConflict()) {
1746 DCHECK_NE(failures_.size(), 0U);
1747 break; // bad class
jeffhao8cd6dda2012-02-22 10:15:34 -08001748 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001749 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
1750 // can't create an instance of an interface or abstract class */
1751 if (!res_type.IsInstantiableTypes()) {
1752 Fail(VERIFY_ERROR_INSTANTIATION)
1753 << "new-instance on primitive, interface or abstract class" << res_type;
Ian Rogers08f753d2012-08-24 14:35:25 -07001754 // Soft failure so carry on to set register type.
Ian Rogersd81871c2011-10-03 13:57:23 -07001755 }
Ian Rogers08f753d2012-08-24 14:35:25 -07001756 const RegType& uninit_type = reg_types_.Uninitialized(res_type, work_insn_idx_);
1757 // Any registers holding previous allocations from this address that have not yet been
1758 // initialized must be marked invalid.
1759 work_line_->MarkUninitRefsAsInvalid(uninit_type);
1760 // add the new uninitialized reference to the register state
Sebastien Hertz5243e912013-05-21 10:55:07 +02001761 work_line_->SetRegisterType(inst->VRegA_21c(), uninit_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001762 break;
1763 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08001764 case Instruction::NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001765 VerifyNewArray(inst, false, false);
jeffhaobdb76512011-09-07 11:43:16 -07001766 break;
1767 case Instruction::FILLED_NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001768 VerifyNewArray(inst, true, false);
Ian Rogers0c4a5062012-02-03 15:18:59 -08001769 just_set_result = true; // Filled new array sets result register
jeffhaobdb76512011-09-07 11:43:16 -07001770 break;
Ian Rogers0c4a5062012-02-03 15:18:59 -08001771 case Instruction::FILLED_NEW_ARRAY_RANGE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001772 VerifyNewArray(inst, true, true);
Ian Rogers0c4a5062012-02-03 15:18:59 -08001773 just_set_result = true; // Filled new array range sets result register
1774 break;
jeffhaobdb76512011-09-07 11:43:16 -07001775 case Instruction::CMPL_FLOAT:
1776 case Instruction::CMPG_FLOAT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001777 if (!work_line_->VerifyRegisterType(inst->VRegB_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08001778 break;
1779 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02001780 if (!work_line_->VerifyRegisterType(inst->VRegC_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08001781 break;
1782 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02001783 work_line_->SetRegisterType(inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001784 break;
1785 case Instruction::CMPL_DOUBLE:
1786 case Instruction::CMPG_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001787 if (!work_line_->VerifyRegisterTypeWide(inst->VRegB_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001788 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001789 break;
1790 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02001791 if (!work_line_->VerifyRegisterTypeWide(inst->VRegC_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001792 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001793 break;
1794 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02001795 work_line_->SetRegisterType(inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001796 break;
1797 case Instruction::CMP_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001798 if (!work_line_->VerifyRegisterTypeWide(inst->VRegB_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001799 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001800 break;
1801 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02001802 if (!work_line_->VerifyRegisterTypeWide(inst->VRegC_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001803 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001804 break;
1805 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02001806 work_line_->SetRegisterType(inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001807 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001808 case Instruction::THROW: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001809 const RegType& res_type = work_line_->GetRegisterType(inst->VRegA_11x());
Ian Rogersb4903572012-10-11 11:52:56 -07001810 if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(res_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07001811 Fail(res_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS : VERIFY_ERROR_BAD_CLASS_SOFT)
1812 << "thrown class " << res_type << " not instanceof Throwable";
jeffhaobdb76512011-09-07 11:43:16 -07001813 }
1814 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001815 }
jeffhaobdb76512011-09-07 11:43:16 -07001816 case Instruction::GOTO:
1817 case Instruction::GOTO_16:
1818 case Instruction::GOTO_32:
1819 /* no effect on or use of registers */
1820 break;
1821
1822 case Instruction::PACKED_SWITCH:
1823 case Instruction::SPARSE_SWITCH:
1824 /* verify that vAA is an integer, or can be converted to one */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001825 work_line_->VerifyRegisterType(inst->VRegA_31t(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001826 break;
1827
Ian Rogersd81871c2011-10-03 13:57:23 -07001828 case Instruction::FILL_ARRAY_DATA: {
1829 /* Similar to the verification done for APUT */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001830 const RegType& array_type = work_line_->GetRegisterType(inst->VRegA_31t());
Ian Rogers89310de2012-02-01 13:47:30 -08001831 /* array_type can be null if the reg type is Zero */
1832 if (!array_type.IsZero()) {
jeffhao457cc512012-02-02 16:55:13 -08001833 if (!array_type.IsArrayTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001834 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with array type "
1835 << array_type;
Ian Rogers89310de2012-02-01 13:47:30 -08001836 } else {
Mathieu Chartier590fee92013-09-13 13:46:47 -07001837 const RegType& component_type = reg_types_.GetComponentType(array_type,
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001838 class_loader_->Get());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001839 DCHECK(!component_type.IsConflict());
jeffhao457cc512012-02-02 16:55:13 -08001840 if (component_type.IsNonZeroReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001841 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with component type "
1842 << component_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001843 } else {
jeffhao457cc512012-02-02 16:55:13 -08001844 // Now verify if the element width in the table matches the element width declared in
1845 // the array
1846 const uint16_t* array_data = insns + (insns[1] | (((int32_t) insns[2]) << 16));
1847 if (array_data[0] != Instruction::kArrayDataSignature) {
jeffhaod5347e02012-03-22 17:25:05 -07001848 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid magic for array-data";
jeffhao457cc512012-02-02 16:55:13 -08001849 } else {
1850 size_t elem_width = Primitive::ComponentSize(component_type.GetPrimitiveType());
1851 // Since we don't compress the data in Dex, expect to see equal width of data stored
1852 // in the table and expected from the array class.
1853 if (array_data[1] != elem_width) {
jeffhaod5347e02012-03-22 17:25:05 -07001854 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-data size mismatch (" << array_data[1]
1855 << " vs " << elem_width << ")";
jeffhao457cc512012-02-02 16:55:13 -08001856 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001857 }
1858 }
jeffhaobdb76512011-09-07 11:43:16 -07001859 }
1860 }
1861 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001862 }
jeffhaobdb76512011-09-07 11:43:16 -07001863 case Instruction::IF_EQ:
Ian Rogersd81871c2011-10-03 13:57:23 -07001864 case Instruction::IF_NE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001865 const RegType& reg_type1 = work_line_->GetRegisterType(inst->VRegA_22t());
1866 const RegType& reg_type2 = work_line_->GetRegisterType(inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07001867 bool mismatch = false;
1868 if (reg_type1.IsZero()) { // zero then integral or reference expected
1869 mismatch = !reg_type2.IsReferenceTypes() && !reg_type2.IsIntegralTypes();
1870 } else if (reg_type1.IsReferenceTypes()) { // both references?
1871 mismatch = !reg_type2.IsReferenceTypes();
1872 } else { // both integral?
1873 mismatch = !reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes();
1874 }
1875 if (mismatch) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001876 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to if-eq/if-ne (" << reg_type1 << ","
1877 << reg_type2 << ") must both be references or integral";
jeffhaobdb76512011-09-07 11:43:16 -07001878 }
1879 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001880 }
jeffhaobdb76512011-09-07 11:43:16 -07001881 case Instruction::IF_LT:
1882 case Instruction::IF_GE:
1883 case Instruction::IF_GT:
Ian Rogersd81871c2011-10-03 13:57:23 -07001884 case Instruction::IF_LE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001885 const RegType& reg_type1 = work_line_->GetRegisterType(inst->VRegA_22t());
1886 const RegType& reg_type2 = work_line_->GetRegisterType(inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07001887 if (!reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001888 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to 'if' (" << reg_type1 << ","
1889 << reg_type2 << ") must be integral";
jeffhaobdb76512011-09-07 11:43:16 -07001890 }
1891 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001892 }
jeffhaobdb76512011-09-07 11:43:16 -07001893 case Instruction::IF_EQZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07001894 case Instruction::IF_NEZ: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001895 const RegType& reg_type = work_line_->GetRegisterType(inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07001896 if (!reg_type.IsReferenceTypes() && !reg_type.IsIntegralTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001897 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
1898 << " unexpected as arg to if-eqz/if-nez";
Ian Rogersd81871c2011-10-03 13:57:23 -07001899 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001900
1901 // Find previous instruction - its existence is a precondition to peephole optimization.
Ian Rogers9b360392013-06-06 14:45:07 -07001902 uint32_t instance_of_idx = 0;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001903 if (0 != work_insn_idx_) {
Ian Rogers9b360392013-06-06 14:45:07 -07001904 instance_of_idx = work_insn_idx_ - 1;
Brian Carlstromdf629502013-07-17 22:39:56 -07001905 while (0 != instance_of_idx && !insn_flags_[instance_of_idx].IsOpcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07001906 instance_of_idx--;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001907 }
Ian Rogers9b360392013-06-06 14:45:07 -07001908 CHECK(insn_flags_[instance_of_idx].IsOpcode());
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001909 } else {
1910 break;
1911 }
1912
Ian Rogers9b360392013-06-06 14:45:07 -07001913 const Instruction* instance_of_inst = Instruction::At(code_item_->insns_ + instance_of_idx);
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001914
1915 /* Check for peep-hole pattern of:
1916 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07001917 * instance-of vX, vY, T;
1918 * ifXXX vX, label ;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001919 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07001920 * label:
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001921 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07001922 * and sharpen the type of vY to be type T.
1923 * Note, this pattern can't be if:
1924 * - if there are other branches to this branch,
1925 * - when vX == vY.
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001926 */
Ian Rogersfae370a2013-06-05 08:33:27 -07001927 if (!CurrentInsnFlags()->IsBranchTarget() &&
Ian Rogers9b360392013-06-06 14:45:07 -07001928 (Instruction::INSTANCE_OF == instance_of_inst->Opcode()) &&
1929 (inst->VRegA_21t() == instance_of_inst->VRegA_22c()) &&
1930 (instance_of_inst->VRegA_22c() != instance_of_inst->VRegB_22c())) {
Ian Rogersfae370a2013-06-05 08:33:27 -07001931 // Check that the we are not attempting conversion to interface types,
1932 // which is not done because of the multiple inheritance implications.
Jeff Haoc642ec82013-09-04 16:11:55 -07001933 // Also don't change the type if it would result in an upcast.
1934 const RegType& orig_type = work_line_->GetRegisterType(instance_of_inst->VRegB_22c());
Ian Rogers9b360392013-06-06 14:45:07 -07001935 const RegType& cast_type = ResolveClassAndCheckAccess(instance_of_inst->VRegC_22c());
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001936
Jeff Haoa3faaf42013-09-03 19:07:00 -07001937 if (!cast_type.IsUnresolvedTypes() && !orig_type.IsUnresolvedTypes() &&
1938 !cast_type.GetClass()->IsInterface() && !cast_type.IsAssignableFrom(orig_type)) {
Ian Rogersd0fbd852013-09-24 18:17:04 -07001939 RegisterLine* update_line = RegisterLine::Create(code_item_->registers_size_, this);
Ian Rogersfae370a2013-06-05 08:33:27 -07001940 if (inst->Opcode() == Instruction::IF_EQZ) {
Ian Rogers9b360392013-06-06 14:45:07 -07001941 fallthrough_line.reset(update_line);
Ian Rogersfae370a2013-06-05 08:33:27 -07001942 } else {
Ian Rogers9b360392013-06-06 14:45:07 -07001943 branch_line.reset(update_line);
1944 }
1945 update_line->CopyFromLine(work_line_.get());
1946 update_line->SetRegisterType(instance_of_inst->VRegB_22c(), cast_type);
1947 if (!insn_flags_[instance_of_idx].IsBranchTarget() && 0 != instance_of_idx) {
1948 // See if instance-of was preceded by a move-object operation, common due to the small
1949 // register encoding space of instance-of, and propagate type information to the source
1950 // of the move-object.
1951 uint32_t move_idx = instance_of_idx - 1;
Brian Carlstromdf629502013-07-17 22:39:56 -07001952 while (0 != move_idx && !insn_flags_[move_idx].IsOpcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07001953 move_idx--;
1954 }
1955 CHECK(insn_flags_[move_idx].IsOpcode());
1956 const Instruction* move_inst = Instruction::At(code_item_->insns_ + move_idx);
1957 switch (move_inst->Opcode()) {
1958 case Instruction::MOVE_OBJECT:
1959 if (move_inst->VRegA_12x() == instance_of_inst->VRegB_22c()) {
1960 update_line->SetRegisterType(move_inst->VRegB_12x(), cast_type);
1961 }
1962 break;
1963 case Instruction::MOVE_OBJECT_FROM16:
1964 if (move_inst->VRegA_22x() == instance_of_inst->VRegB_22c()) {
1965 update_line->SetRegisterType(move_inst->VRegB_22x(), cast_type);
1966 }
1967 break;
1968 case Instruction::MOVE_OBJECT_16:
1969 if (move_inst->VRegA_32x() == instance_of_inst->VRegB_22c()) {
1970 update_line->SetRegisterType(move_inst->VRegB_32x(), cast_type);
1971 }
1972 break;
1973 default:
1974 break;
1975 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001976 }
1977 }
1978 }
1979
jeffhaobdb76512011-09-07 11:43:16 -07001980 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001981 }
jeffhaobdb76512011-09-07 11:43:16 -07001982 case Instruction::IF_LTZ:
1983 case Instruction::IF_GEZ:
1984 case Instruction::IF_GTZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07001985 case Instruction::IF_LEZ: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001986 const RegType& reg_type = work_line_->GetRegisterType(inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07001987 if (!reg_type.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001988 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
1989 << " unexpected as arg to if-ltz/if-gez/if-gtz/if-lez";
Ian Rogersd81871c2011-10-03 13:57:23 -07001990 }
jeffhaobdb76512011-09-07 11:43:16 -07001991 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001992 }
jeffhaobdb76512011-09-07 11:43:16 -07001993 case Instruction::AGET_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001994 VerifyAGet(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001995 break;
jeffhaobdb76512011-09-07 11:43:16 -07001996 case Instruction::AGET_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001997 VerifyAGet(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001998 break;
jeffhaobdb76512011-09-07 11:43:16 -07001999 case Instruction::AGET_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002000 VerifyAGet(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002001 break;
jeffhaobdb76512011-09-07 11:43:16 -07002002 case Instruction::AGET_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002003 VerifyAGet(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002004 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002005 case Instruction::AGET:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002006 VerifyAGet(inst, reg_types_.Integer(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002007 break;
jeffhaobdb76512011-09-07 11:43:16 -07002008 case Instruction::AGET_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002009 VerifyAGet(inst, reg_types_.LongLo(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002010 break;
2011 case Instruction::AGET_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002012 VerifyAGet(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07002013 break;
2014
Ian Rogersd81871c2011-10-03 13:57:23 -07002015 case Instruction::APUT_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002016 VerifyAPut(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002017 break;
2018 case Instruction::APUT_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002019 VerifyAPut(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002020 break;
2021 case Instruction::APUT_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002022 VerifyAPut(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002023 break;
2024 case Instruction::APUT_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002025 VerifyAPut(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002026 break;
2027 case Instruction::APUT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002028 VerifyAPut(inst, reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002029 break;
2030 case Instruction::APUT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002031 VerifyAPut(inst, reg_types_.LongLo(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002032 break;
2033 case Instruction::APUT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002034 VerifyAPut(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07002035 break;
2036
jeffhaobdb76512011-09-07 11:43:16 -07002037 case Instruction::IGET_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002038 VerifyISGet(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002039 break;
jeffhaobdb76512011-09-07 11:43:16 -07002040 case Instruction::IGET_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002041 VerifyISGet(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002042 break;
jeffhaobdb76512011-09-07 11:43:16 -07002043 case Instruction::IGET_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002044 VerifyISGet(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002045 break;
jeffhaobdb76512011-09-07 11:43:16 -07002046 case Instruction::IGET_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002047 VerifyISGet(inst, reg_types_.Short(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002048 break;
2049 case Instruction::IGET:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002050 VerifyISGet(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002051 break;
2052 case Instruction::IGET_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002053 VerifyISGet(inst, reg_types_.LongLo(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002054 break;
2055 case Instruction::IGET_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002056 VerifyISGet(inst, reg_types_.JavaLangObject(false), false, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002057 break;
jeffhaobdb76512011-09-07 11:43:16 -07002058
Ian Rogersd81871c2011-10-03 13:57:23 -07002059 case Instruction::IPUT_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002060 VerifyISPut(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002061 break;
2062 case Instruction::IPUT_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002063 VerifyISPut(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002064 break;
2065 case Instruction::IPUT_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002066 VerifyISPut(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002067 break;
2068 case Instruction::IPUT_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002069 VerifyISPut(inst, reg_types_.Short(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002070 break;
2071 case Instruction::IPUT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002072 VerifyISPut(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002073 break;
2074 case Instruction::IPUT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002075 VerifyISPut(inst, reg_types_.LongLo(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002076 break;
jeffhaobdb76512011-09-07 11:43:16 -07002077 case Instruction::IPUT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002078 VerifyISPut(inst, reg_types_.JavaLangObject(false), false, false);
jeffhaobdb76512011-09-07 11:43:16 -07002079 break;
2080
jeffhaobdb76512011-09-07 11:43:16 -07002081 case Instruction::SGET_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002082 VerifyISGet(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002083 break;
jeffhaobdb76512011-09-07 11:43:16 -07002084 case Instruction::SGET_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002085 VerifyISGet(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002086 break;
jeffhaobdb76512011-09-07 11:43:16 -07002087 case Instruction::SGET_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002088 VerifyISGet(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002089 break;
jeffhaobdb76512011-09-07 11:43:16 -07002090 case Instruction::SGET_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002091 VerifyISGet(inst, reg_types_.Short(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002092 break;
2093 case Instruction::SGET:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002094 VerifyISGet(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002095 break;
2096 case Instruction::SGET_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002097 VerifyISGet(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002098 break;
2099 case Instruction::SGET_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002100 VerifyISGet(inst, reg_types_.JavaLangObject(false), false, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002101 break;
2102
2103 case Instruction::SPUT_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002104 VerifyISPut(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002105 break;
2106 case Instruction::SPUT_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002107 VerifyISPut(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002108 break;
2109 case Instruction::SPUT_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002110 VerifyISPut(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002111 break;
2112 case Instruction::SPUT_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002113 VerifyISPut(inst, reg_types_.Short(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002114 break;
2115 case Instruction::SPUT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002116 VerifyISPut(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002117 break;
2118 case Instruction::SPUT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002119 VerifyISPut(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002120 break;
2121 case Instruction::SPUT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002122 VerifyISPut(inst, reg_types_.JavaLangObject(false), false, true);
jeffhaobdb76512011-09-07 11:43:16 -07002123 break;
2124
2125 case Instruction::INVOKE_VIRTUAL:
2126 case Instruction::INVOKE_VIRTUAL_RANGE:
2127 case Instruction::INVOKE_SUPER:
Ian Rogersd81871c2011-10-03 13:57:23 -07002128 case Instruction::INVOKE_SUPER_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002129 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE ||
2130 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
2131 bool is_super = (inst->Opcode() == Instruction::INVOKE_SUPER ||
2132 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
Brian Carlstromea46f952013-07-30 01:26:50 -07002133 mirror::ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_VIRTUAL,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002134 is_range, is_super);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002135 const RegType* return_type = nullptr;
2136 if (called_method != nullptr) {
2137 MethodHelper mh(called_method);
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08002138 mirror::Class* return_type_class = mh.GetReturnType(can_load_classes_);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002139 if (return_type_class != nullptr) {
2140 return_type = &reg_types_.FromClass(mh.GetReturnTypeDescriptor(), return_type_class,
2141 return_type_class->CannotBeAssignedFromOtherTypes());
2142 } else {
2143 Thread* self = Thread::Current();
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08002144 DCHECK(!can_load_classes_ || self->IsExceptionPending());
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002145 self->ClearException();
2146 }
2147 }
2148 if (return_type == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002149 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002150 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2151 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002152 const char* descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07002153 return_type = &reg_types_.FromDescriptor(class_loader_->Get(), descriptor, false);
jeffhaobdb76512011-09-07 11:43:16 -07002154 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002155 if (!return_type->IsLowHalf()) {
2156 work_line_->SetResultRegisterType(*return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002157 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002158 work_line_->SetResultRegisterTypeWide(*return_type, return_type->HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002159 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002160 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002161 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002162 }
jeffhaobdb76512011-09-07 11:43:16 -07002163 case Instruction::INVOKE_DIRECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002164 case Instruction::INVOKE_DIRECT_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002165 bool is_range = (inst->Opcode() == Instruction::INVOKE_DIRECT_RANGE);
Brian Carlstromea46f952013-07-30 01:26:50 -07002166 mirror::ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_DIRECT,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002167 is_range, false);
Ian Rogers46685432012-06-03 22:26:43 -07002168 const char* return_type_descriptor;
2169 bool is_constructor;
2170 if (called_method == NULL) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002171 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogers46685432012-06-03 22:26:43 -07002172 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
Ian Rogersdfb325e2013-10-30 01:00:44 -07002173 is_constructor = strcmp("<init>", dex_file_->StringDataByIdx(method_id.name_idx_)) == 0;
Ian Rogers46685432012-06-03 22:26:43 -07002174 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
2175 return_type_descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2176 } else {
2177 is_constructor = called_method->IsConstructor();
2178 return_type_descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
2179 }
2180 if (is_constructor) {
jeffhaobdb76512011-09-07 11:43:16 -07002181 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002182 * Some additional checks when calling a constructor. We know from the invocation arg check
2183 * that the "this" argument is an instance of called_method->klass. Now we further restrict
2184 * that to require that called_method->klass is the same as this->klass or this->super,
2185 * allowing the latter only if the "this" argument is the same as the "this" argument to
2186 * this method (which implies that we're in a constructor ourselves).
jeffhaobdb76512011-09-07 11:43:16 -07002187 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002188 const RegType& this_type = work_line_->GetInvocationThis(inst, is_range);
jeffhaob57e9522012-04-26 18:08:21 -07002189 if (this_type.IsConflict()) // failure.
2190 break;
jeffhaobdb76512011-09-07 11:43:16 -07002191
jeffhaob57e9522012-04-26 18:08:21 -07002192 /* no null refs allowed (?) */
2193 if (this_type.IsZero()) {
2194 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unable to initialize null ref";
2195 break;
jeffhao2a8a90e2011-09-26 14:25:31 -07002196 }
jeffhaob57e9522012-04-26 18:08:21 -07002197
2198 /* must be in same class or in superclass */
Ian Rogers46685432012-06-03 22:26:43 -07002199 // const RegType& this_super_klass = this_type.GetSuperClass(&reg_types_);
2200 // TODO: re-enable constructor type verification
2201 // if (this_super_klass.IsConflict()) {
jeffhaob57e9522012-04-26 18:08:21 -07002202 // Unknown super class, fail so we re-check at runtime.
Ian Rogers46685432012-06-03 22:26:43 -07002203 // Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "super class unknown for '" << this_type << "'";
2204 // break;
2205 // }
jeffhaob57e9522012-04-26 18:08:21 -07002206
2207 /* arg must be an uninitialized reference */
2208 if (!this_type.IsUninitializedTypes()) {
2209 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Expected initialization on uninitialized reference "
2210 << this_type;
2211 break;
2212 }
2213
2214 /*
2215 * Replace the uninitialized reference with an initialized one. We need to do this for all
2216 * registers that have the same object instance in them, not just the "this" register.
2217 */
2218 work_line_->MarkRefsAsInitialized(this_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002219 }
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07002220 const RegType& return_type = reg_types_.FromDescriptor(class_loader_->Get(),
Mathieu Chartier590fee92013-09-13 13:46:47 -07002221 return_type_descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002222 if (!return_type.IsLowHalf()) {
2223 work_line_->SetResultRegisterType(return_type);
2224 } else {
2225 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2226 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002227 just_set_result = true;
2228 break;
2229 }
2230 case Instruction::INVOKE_STATIC:
2231 case Instruction::INVOKE_STATIC_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002232 bool is_range = (inst->Opcode() == Instruction::INVOKE_STATIC_RANGE);
Brian Carlstromea46f952013-07-30 01:26:50 -07002233 mirror::ArtMethod* called_method = VerifyInvocationArgs(inst,
Brian Carlstrom93c33962013-07-26 10:37:43 -07002234 METHOD_STATIC,
2235 is_range,
2236 false);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002237 const char* descriptor;
2238 if (called_method == NULL) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002239 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002240 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2241 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Mathieu Chartier590fee92013-09-13 13:46:47 -07002242 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002243 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002244 descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002245 }
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07002246 const RegType& return_type = reg_types_.FromDescriptor(class_loader_->Get(), descriptor,
Mathieu Chartier590fee92013-09-13 13:46:47 -07002247 false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002248 if (!return_type.IsLowHalf()) {
2249 work_line_->SetResultRegisterType(return_type);
2250 } else {
2251 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2252 }
jeffhaobdb76512011-09-07 11:43:16 -07002253 just_set_result = true;
2254 }
2255 break;
jeffhaobdb76512011-09-07 11:43:16 -07002256 case Instruction::INVOKE_INTERFACE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002257 case Instruction::INVOKE_INTERFACE_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002258 bool is_range = (inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE);
Brian Carlstromea46f952013-07-30 01:26:50 -07002259 mirror::ArtMethod* abs_method = VerifyInvocationArgs(inst,
Brian Carlstrom93c33962013-07-26 10:37:43 -07002260 METHOD_INTERFACE,
2261 is_range,
2262 false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002263 if (abs_method != NULL) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002264 mirror::Class* called_interface = abs_method->GetDeclaringClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002265 if (!called_interface->IsInterface() && !called_interface->IsObjectClass()) {
2266 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected interface class in invoke-interface '"
2267 << PrettyMethod(abs_method) << "'";
2268 break;
Ian Rogers28ad40d2011-10-27 15:19:26 -07002269 }
Ian Rogers0d604842012-04-16 14:50:24 -07002270 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002271 /* Get the type of the "this" arg, which should either be a sub-interface of called
2272 * interface or Object (see comments in RegType::JoinClass).
2273 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002274 const RegType& this_type = work_line_->GetInvocationThis(inst, is_range);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002275 if (this_type.IsZero()) {
2276 /* null pointer always passes (and always fails at runtime) */
2277 } else {
2278 if (this_type.IsUninitializedTypes()) {
2279 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface call on uninitialized object "
2280 << this_type;
2281 break;
2282 }
2283 // In the past we have tried to assert that "called_interface" is assignable
2284 // from "this_type.GetClass()", however, as we do an imprecise Join
2285 // (RegType::JoinClass) we don't have full information on what interfaces are
2286 // implemented by "this_type". For example, two classes may implement the same
2287 // interfaces and have a common parent that doesn't implement the interface. The
2288 // join will set "this_type" to the parent class and a test that this implements
2289 // the interface will incorrectly fail.
2290 }
2291 /*
2292 * We don't have an object instance, so we can't find the concrete method. However, all of
2293 * the type information is in the abstract method, so we're good.
2294 */
2295 const char* descriptor;
2296 if (abs_method == NULL) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002297 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002298 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2299 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
2300 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2301 } else {
2302 descriptor = MethodHelper(abs_method).GetReturnTypeDescriptor();
2303 }
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07002304 const RegType& return_type = reg_types_.FromDescriptor(class_loader_->Get(), descriptor,
Mathieu Chartier590fee92013-09-13 13:46:47 -07002305 false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002306 if (!return_type.IsLowHalf()) {
2307 work_line_->SetResultRegisterType(return_type);
2308 } else {
2309 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2310 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002311 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002312 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002313 }
jeffhaobdb76512011-09-07 11:43:16 -07002314 case Instruction::NEG_INT:
2315 case Instruction::NOT_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002316 work_line_->CheckUnaryOp(inst, reg_types_.Integer(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002317 break;
2318 case Instruction::NEG_LONG:
2319 case Instruction::NOT_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002320 work_line_->CheckUnaryOpWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002321 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002322 break;
2323 case Instruction::NEG_FLOAT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002324 work_line_->CheckUnaryOp(inst, reg_types_.Float(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002325 break;
2326 case Instruction::NEG_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002327 work_line_->CheckUnaryOpWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002328 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002329 break;
2330 case Instruction::INT_TO_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002331 work_line_->CheckUnaryOpToWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002332 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002333 break;
2334 case Instruction::INT_TO_FLOAT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002335 work_line_->CheckUnaryOp(inst, reg_types_.Float(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002336 break;
2337 case Instruction::INT_TO_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002338 work_line_->CheckUnaryOpToWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002339 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002340 break;
2341 case Instruction::LONG_TO_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002342 work_line_->CheckUnaryOpFromWide(inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002343 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002344 break;
2345 case Instruction::LONG_TO_FLOAT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002346 work_line_->CheckUnaryOpFromWide(inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002347 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002348 break;
2349 case Instruction::LONG_TO_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002350 work_line_->CheckUnaryOpWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002351 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002352 break;
2353 case Instruction::FLOAT_TO_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002354 work_line_->CheckUnaryOp(inst, reg_types_.Integer(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002355 break;
2356 case Instruction::FLOAT_TO_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002357 work_line_->CheckUnaryOpToWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002358 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002359 break;
2360 case Instruction::FLOAT_TO_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002361 work_line_->CheckUnaryOpToWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002362 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002363 break;
2364 case Instruction::DOUBLE_TO_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002365 work_line_->CheckUnaryOpFromWide(inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002366 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002367 break;
2368 case Instruction::DOUBLE_TO_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002369 work_line_->CheckUnaryOpWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002370 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002371 break;
2372 case Instruction::DOUBLE_TO_FLOAT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002373 work_line_->CheckUnaryOpFromWide(inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002374 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002375 break;
2376 case Instruction::INT_TO_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002377 work_line_->CheckUnaryOp(inst, reg_types_.Byte(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002378 break;
2379 case Instruction::INT_TO_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002380 work_line_->CheckUnaryOp(inst, reg_types_.Char(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002381 break;
2382 case Instruction::INT_TO_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002383 work_line_->CheckUnaryOp(inst, reg_types_.Short(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002384 break;
2385
2386 case Instruction::ADD_INT:
2387 case Instruction::SUB_INT:
2388 case Instruction::MUL_INT:
2389 case Instruction::REM_INT:
2390 case Instruction::DIV_INT:
2391 case Instruction::SHL_INT:
2392 case Instruction::SHR_INT:
2393 case Instruction::USHR_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002394 work_line_->CheckBinaryOp(inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002395 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002396 break;
2397 case Instruction::AND_INT:
2398 case Instruction::OR_INT:
2399 case Instruction::XOR_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002400 work_line_->CheckBinaryOp(inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002401 reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002402 break;
2403 case Instruction::ADD_LONG:
2404 case Instruction::SUB_LONG:
2405 case Instruction::MUL_LONG:
2406 case Instruction::DIV_LONG:
2407 case Instruction::REM_LONG:
2408 case Instruction::AND_LONG:
2409 case Instruction::OR_LONG:
2410 case Instruction::XOR_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002411 work_line_->CheckBinaryOpWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002412 reg_types_.LongLo(), reg_types_.LongHi(),
2413 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002414 break;
2415 case Instruction::SHL_LONG:
2416 case Instruction::SHR_LONG:
2417 case Instruction::USHR_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002418 /* shift distance is Int, making these different from other binary operations */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002419 work_line_->CheckBinaryOpWideShift(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002420 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002421 break;
2422 case Instruction::ADD_FLOAT:
2423 case Instruction::SUB_FLOAT:
2424 case Instruction::MUL_FLOAT:
2425 case Instruction::DIV_FLOAT:
2426 case Instruction::REM_FLOAT:
Brian Carlstrom93c33962013-07-26 10:37:43 -07002427 work_line_->CheckBinaryOp(inst,
2428 reg_types_.Float(),
2429 reg_types_.Float(),
2430 reg_types_.Float(),
2431 false);
jeffhaobdb76512011-09-07 11:43:16 -07002432 break;
2433 case Instruction::ADD_DOUBLE:
2434 case Instruction::SUB_DOUBLE:
2435 case Instruction::MUL_DOUBLE:
2436 case Instruction::DIV_DOUBLE:
2437 case Instruction::REM_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002438 work_line_->CheckBinaryOpWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002439 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2440 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002441 break;
2442 case Instruction::ADD_INT_2ADDR:
2443 case Instruction::SUB_INT_2ADDR:
2444 case Instruction::MUL_INT_2ADDR:
2445 case Instruction::REM_INT_2ADDR:
2446 case Instruction::SHL_INT_2ADDR:
2447 case Instruction::SHR_INT_2ADDR:
2448 case Instruction::USHR_INT_2ADDR:
Brian Carlstrom93c33962013-07-26 10:37:43 -07002449 work_line_->CheckBinaryOp2addr(inst,
2450 reg_types_.Integer(),
2451 reg_types_.Integer(),
2452 reg_types_.Integer(),
2453 false);
jeffhaobdb76512011-09-07 11:43:16 -07002454 break;
2455 case Instruction::AND_INT_2ADDR:
2456 case Instruction::OR_INT_2ADDR:
2457 case Instruction::XOR_INT_2ADDR:
Brian Carlstrom93c33962013-07-26 10:37:43 -07002458 work_line_->CheckBinaryOp2addr(inst,
2459 reg_types_.Integer(),
2460 reg_types_.Integer(),
2461 reg_types_.Integer(),
2462 true);
jeffhaobdb76512011-09-07 11:43:16 -07002463 break;
2464 case Instruction::DIV_INT_2ADDR:
Brian Carlstrom93c33962013-07-26 10:37:43 -07002465 work_line_->CheckBinaryOp2addr(inst,
2466 reg_types_.Integer(),
2467 reg_types_.Integer(),
2468 reg_types_.Integer(),
2469 false);
jeffhaobdb76512011-09-07 11:43:16 -07002470 break;
2471 case Instruction::ADD_LONG_2ADDR:
2472 case Instruction::SUB_LONG_2ADDR:
2473 case Instruction::MUL_LONG_2ADDR:
2474 case Instruction::DIV_LONG_2ADDR:
2475 case Instruction::REM_LONG_2ADDR:
2476 case Instruction::AND_LONG_2ADDR:
2477 case Instruction::OR_LONG_2ADDR:
2478 case Instruction::XOR_LONG_2ADDR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002479 work_line_->CheckBinaryOp2addrWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002480 reg_types_.LongLo(), reg_types_.LongHi(),
2481 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002482 break;
2483 case Instruction::SHL_LONG_2ADDR:
2484 case Instruction::SHR_LONG_2ADDR:
2485 case Instruction::USHR_LONG_2ADDR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002486 work_line_->CheckBinaryOp2addrWideShift(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002487 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002488 break;
2489 case Instruction::ADD_FLOAT_2ADDR:
2490 case Instruction::SUB_FLOAT_2ADDR:
2491 case Instruction::MUL_FLOAT_2ADDR:
2492 case Instruction::DIV_FLOAT_2ADDR:
2493 case Instruction::REM_FLOAT_2ADDR:
Brian Carlstrom93c33962013-07-26 10:37:43 -07002494 work_line_->CheckBinaryOp2addr(inst,
2495 reg_types_.Float(),
2496 reg_types_.Float(),
2497 reg_types_.Float(),
2498 false);
jeffhaobdb76512011-09-07 11:43:16 -07002499 break;
2500 case Instruction::ADD_DOUBLE_2ADDR:
2501 case Instruction::SUB_DOUBLE_2ADDR:
2502 case Instruction::MUL_DOUBLE_2ADDR:
2503 case Instruction::DIV_DOUBLE_2ADDR:
2504 case Instruction::REM_DOUBLE_2ADDR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002505 work_line_->CheckBinaryOp2addrWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002506 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2507 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002508 break;
2509 case Instruction::ADD_INT_LIT16:
2510 case Instruction::RSUB_INT:
2511 case Instruction::MUL_INT_LIT16:
2512 case Instruction::DIV_INT_LIT16:
2513 case Instruction::REM_INT_LIT16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002514 work_line_->CheckLiteralOp(inst, reg_types_.Integer(), reg_types_.Integer(), false, true);
jeffhaobdb76512011-09-07 11:43:16 -07002515 break;
2516 case Instruction::AND_INT_LIT16:
2517 case Instruction::OR_INT_LIT16:
2518 case Instruction::XOR_INT_LIT16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002519 work_line_->CheckLiteralOp(inst, reg_types_.Integer(), reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002520 break;
2521 case Instruction::ADD_INT_LIT8:
2522 case Instruction::RSUB_INT_LIT8:
2523 case Instruction::MUL_INT_LIT8:
2524 case Instruction::DIV_INT_LIT8:
2525 case Instruction::REM_INT_LIT8:
2526 case Instruction::SHL_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002527 case Instruction::SHR_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002528 case Instruction::USHR_INT_LIT8:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002529 work_line_->CheckLiteralOp(inst, reg_types_.Integer(), reg_types_.Integer(), false, false);
jeffhaobdb76512011-09-07 11:43:16 -07002530 break;
2531 case Instruction::AND_INT_LIT8:
2532 case Instruction::OR_INT_LIT8:
2533 case Instruction::XOR_INT_LIT8:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002534 work_line_->CheckLiteralOp(inst, reg_types_.Integer(), reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002535 break;
2536
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002537 // Special instructions.
Sebastien Hertzcc10e0e2013-06-28 14:24:48 +02002538 case Instruction::RETURN_VOID_BARRIER:
Ian Rogers9fc16eb2013-07-31 14:49:16 -07002539 if (!IsConstructor() || IsStatic()) {
Sebastien Hertzcc10e0e2013-06-28 14:24:48 +02002540 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void-barrier not expected";
2541 }
2542 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002543 // Note: the following instructions encode offsets derived from class linking.
2544 // As such they use Class*/Field*/AbstractMethod* as these offsets only have
2545 // meaning if the class linking and resolution were successful.
2546 case Instruction::IGET_QUICK:
2547 VerifyIGetQuick(inst, reg_types_.Integer(), true);
2548 break;
2549 case Instruction::IGET_WIDE_QUICK:
2550 VerifyIGetQuick(inst, reg_types_.LongLo(), true);
2551 break;
2552 case Instruction::IGET_OBJECT_QUICK:
2553 VerifyIGetQuick(inst, reg_types_.JavaLangObject(false), false);
2554 break;
2555 case Instruction::IPUT_QUICK:
2556 VerifyIPutQuick(inst, reg_types_.Integer(), true);
2557 break;
2558 case Instruction::IPUT_WIDE_QUICK:
2559 VerifyIPutQuick(inst, reg_types_.LongLo(), true);
2560 break;
2561 case Instruction::IPUT_OBJECT_QUICK:
2562 VerifyIPutQuick(inst, reg_types_.JavaLangObject(false), false);
2563 break;
2564 case Instruction::INVOKE_VIRTUAL_QUICK:
2565 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: {
2566 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
Brian Carlstromea46f952013-07-30 01:26:50 -07002567 mirror::ArtMethod* called_method = VerifyInvokeVirtualQuickArgs(inst, is_range);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002568 if (called_method != NULL) {
2569 const char* descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07002570 const RegType& return_type = reg_types_.FromDescriptor(class_loader_->Get(), descriptor,
Mathieu Chartier590fee92013-09-13 13:46:47 -07002571 false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002572 if (!return_type.IsLowHalf()) {
2573 work_line_->SetResultRegisterType(return_type);
2574 } else {
2575 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2576 }
2577 just_set_result = true;
2578 }
2579 break;
2580 }
2581
Ian Rogersd81871c2011-10-03 13:57:23 -07002582 /* These should never appear during verification. */
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002583 case Instruction::UNUSED_3E:
2584 case Instruction::UNUSED_3F:
2585 case Instruction::UNUSED_40:
2586 case Instruction::UNUSED_41:
2587 case Instruction::UNUSED_42:
2588 case Instruction::UNUSED_43:
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002589 case Instruction::UNUSED_79:
2590 case Instruction::UNUSED_7A:
2591 case Instruction::UNUSED_EB:
2592 case Instruction::UNUSED_EC:
jeffhao9a4f0032012-08-30 16:17:40 -07002593 case Instruction::UNUSED_ED:
jeffhaobdb76512011-09-07 11:43:16 -07002594 case Instruction::UNUSED_EE:
2595 case Instruction::UNUSED_EF:
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002596 case Instruction::UNUSED_F0:
2597 case Instruction::UNUSED_F1:
jeffhaobdb76512011-09-07 11:43:16 -07002598 case Instruction::UNUSED_F2:
2599 case Instruction::UNUSED_F3:
2600 case Instruction::UNUSED_F4:
2601 case Instruction::UNUSED_F5:
2602 case Instruction::UNUSED_F6:
2603 case Instruction::UNUSED_F7:
2604 case Instruction::UNUSED_F8:
2605 case Instruction::UNUSED_F9:
2606 case Instruction::UNUSED_FA:
2607 case Instruction::UNUSED_FB:
jeffhaobdb76512011-09-07 11:43:16 -07002608 case Instruction::UNUSED_FC:
jeffhaobdb76512011-09-07 11:43:16 -07002609 case Instruction::UNUSED_FD:
jeffhaobdb76512011-09-07 11:43:16 -07002610 case Instruction::UNUSED_FE:
jeffhaobdb76512011-09-07 11:43:16 -07002611 case Instruction::UNUSED_FF:
jeffhaod5347e02012-03-22 17:25:05 -07002612 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Unexpected opcode " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07002613 break;
2614
2615 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002616 * DO NOT add a "default" clause here. Without it the compiler will
jeffhaobdb76512011-09-07 11:43:16 -07002617 * complain if an instruction is missing (which is desirable).
2618 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002619 } // end - switch (dec_insn.opcode)
jeffhaobdb76512011-09-07 11:43:16 -07002620
Ian Rogersad0b3a32012-04-16 14:50:24 -07002621 if (have_pending_hard_failure_) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002622 if (Runtime::Current()->IsCompiler()) {
jeffhaob57e9522012-04-26 18:08:21 -07002623 /* When compiling, check that the last failure is a hard failure */
Ian Rogersad0b3a32012-04-16 14:50:24 -07002624 CHECK_EQ(failures_[failures_.size() - 1], VERIFY_ERROR_BAD_CLASS_HARD);
Ian Rogerse1758fe2012-04-19 11:31:15 -07002625 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002626 /* immediate failure, reject class */
2627 info_messages_ << "Rejecting opcode " << inst->DumpString(dex_file_);
2628 return false;
jeffhaofaf459e2012-08-31 15:32:47 -07002629 } else if (have_pending_runtime_throw_failure_) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07002630 /* checking interpreter will throw, mark following code as unreachable */
jeffhaofaf459e2012-08-31 15:32:47 -07002631 opcode_flags = Instruction::kThrow;
jeffhaobdb76512011-09-07 11:43:16 -07002632 }
jeffhaobdb76512011-09-07 11:43:16 -07002633 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002634 * If we didn't just set the result register, clear it out. This ensures that you can only use
2635 * "move-result" immediately after the result is set. (We could check this statically, but it's
2636 * not expensive and it makes our debugging output cleaner.)
jeffhaobdb76512011-09-07 11:43:16 -07002637 */
2638 if (!just_set_result) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002639 work_line_->SetResultTypeToUnknown();
jeffhaobdb76512011-09-07 11:43:16 -07002640 }
2641
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002642
jeffhaobdb76512011-09-07 11:43:16 -07002643
2644 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002645 * Handle "branch". Tag the branch target.
jeffhaobdb76512011-09-07 11:43:16 -07002646 *
2647 * NOTE: instructions like Instruction::EQZ provide information about the
jeffhaod1f0fde2011-09-08 17:25:33 -07002648 * state of the register when the branch is taken or not taken. For example,
jeffhaobdb76512011-09-07 11:43:16 -07002649 * somebody could get a reference field, check it for zero, and if the
2650 * branch is taken immediately store that register in a boolean field
jeffhaod1f0fde2011-09-08 17:25:33 -07002651 * since the value is known to be zero. We do not currently account for
jeffhaobdb76512011-09-07 11:43:16 -07002652 * that, and will reject the code.
2653 *
2654 * TODO: avoid re-fetching the branch target
2655 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002656 if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002657 bool isConditional, selfOkay;
Ian Rogersd81871c2011-10-03 13:57:23 -07002658 if (!GetBranchOffset(work_insn_idx_, &branch_target, &isConditional, &selfOkay)) {
jeffhaobdb76512011-09-07 11:43:16 -07002659 /* should never happen after static verification */
jeffhaod5347e02012-03-22 17:25:05 -07002660 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad branch";
jeffhaobdb76512011-09-07 11:43:16 -07002661 return false;
2662 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08002663 DCHECK_EQ(isConditional, (opcode_flags & Instruction::kContinue) != 0);
jeffhaod5347e02012-03-22 17:25:05 -07002664 if (!CheckNotMoveException(code_item_->insns_, work_insn_idx_ + branch_target)) {
jeffhaobdb76512011-09-07 11:43:16 -07002665 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002666 }
jeffhaobdb76512011-09-07 11:43:16 -07002667 /* update branch target, set "changed" if appropriate */
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002668 if (NULL != branch_line.get()) {
2669 if (!UpdateRegisters(work_insn_idx_ + branch_target, branch_line.get())) {
2670 return false;
2671 }
2672 } else {
2673 if (!UpdateRegisters(work_insn_idx_ + branch_target, work_line_.get())) {
2674 return false;
2675 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002676 }
jeffhaobdb76512011-09-07 11:43:16 -07002677 }
2678
2679 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002680 * Handle "switch". Tag all possible branch targets.
jeffhaobdb76512011-09-07 11:43:16 -07002681 *
2682 * We've already verified that the table is structurally sound, so we
2683 * just need to walk through and tag the targets.
2684 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002685 if ((opcode_flags & Instruction::kSwitch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002686 int offset_to_switch = insns[1] | (((int32_t) insns[2]) << 16);
2687 const uint16_t* switch_insns = insns + offset_to_switch;
2688 int switch_count = switch_insns[1];
2689 int offset_to_targets, targ;
2690
2691 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
2692 /* 0 = sig, 1 = count, 2/3 = first key */
2693 offset_to_targets = 4;
2694 } else {
2695 /* 0 = sig, 1 = count, 2..count * 2 = keys */
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07002696 DCHECK((*insns & 0xff) == Instruction::SPARSE_SWITCH);
jeffhaobdb76512011-09-07 11:43:16 -07002697 offset_to_targets = 2 + 2 * switch_count;
2698 }
2699
2700 /* verify each switch target */
2701 for (targ = 0; targ < switch_count; targ++) {
2702 int offset;
2703 uint32_t abs_offset;
2704
2705 /* offsets are 32-bit, and only partly endian-swapped */
2706 offset = switch_insns[offset_to_targets + targ * 2] |
2707 (((int32_t) switch_insns[offset_to_targets + targ * 2 + 1]) << 16);
Ian Rogersd81871c2011-10-03 13:57:23 -07002708 abs_offset = work_insn_idx_ + offset;
2709 DCHECK_LT(abs_offset, code_item_->insns_size_in_code_units_);
jeffhaod5347e02012-03-22 17:25:05 -07002710 if (!CheckNotMoveException(code_item_->insns_, abs_offset)) {
jeffhaobdb76512011-09-07 11:43:16 -07002711 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002712 }
2713 if (!UpdateRegisters(abs_offset, work_line_.get()))
jeffhaobdb76512011-09-07 11:43:16 -07002714 return false;
2715 }
2716 }
2717
2718 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002719 * Handle instructions that can throw and that are sitting in a "try" block. (If they're not in a
2720 * "try" block when they throw, control transfers out of the method.)
jeffhaobdb76512011-09-07 11:43:16 -07002721 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002722 if ((opcode_flags & Instruction::kThrow) != 0 && insn_flags_[work_insn_idx_].IsInTry()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002723 bool within_catch_all = false;
Ian Rogers0571d352011-11-03 19:51:38 -07002724 CatchHandlerIterator iterator(*code_item_, work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07002725
Ian Rogers0571d352011-11-03 19:51:38 -07002726 for (; iterator.HasNext(); iterator.Next()) {
2727 if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002728 within_catch_all = true;
2729 }
jeffhaobdb76512011-09-07 11:43:16 -07002730 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002731 * Merge registers into the "catch" block. We want to use the "savedRegs" rather than
2732 * "work_regs", because at runtime the exception will be thrown before the instruction
2733 * modifies any registers.
jeffhaobdb76512011-09-07 11:43:16 -07002734 */
Ian Rogers0571d352011-11-03 19:51:38 -07002735 if (!UpdateRegisters(iterator.GetHandlerAddress(), saved_line_.get())) {
jeffhaobdb76512011-09-07 11:43:16 -07002736 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002737 }
jeffhaobdb76512011-09-07 11:43:16 -07002738 }
2739
2740 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002741 * If the monitor stack depth is nonzero, there must be a "catch all" handler for this
2742 * instruction. This does apply to monitor-exit because of async exception handling.
jeffhaobdb76512011-09-07 11:43:16 -07002743 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002744 if (work_line_->MonitorStackDepth() > 0 && !within_catch_all) {
jeffhaobdb76512011-09-07 11:43:16 -07002745 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002746 * The state in work_line reflects the post-execution state. If the current instruction is a
2747 * monitor-enter and the monitor stack was empty, we don't need a catch-all (if it throws,
jeffhaobdb76512011-09-07 11:43:16 -07002748 * it will do so before grabbing the lock).
2749 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002750 if (inst->Opcode() != Instruction::MONITOR_ENTER || work_line_->MonitorStackDepth() != 1) {
jeffhaod5347e02012-03-22 17:25:05 -07002751 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
Ian Rogersd81871c2011-10-03 13:57:23 -07002752 << "expected to be within a catch-all for an instruction where a monitor is held";
jeffhaobdb76512011-09-07 11:43:16 -07002753 return false;
2754 }
2755 }
2756 }
2757
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002758 /* Handle "continue". Tag the next consecutive instruction.
2759 * Note: Keep the code handling "continue" case below the "branch" and "switch" cases,
2760 * because it changes work_line_ when performing peephole optimization
2761 * and this change should not be used in those cases.
2762 */
Ian Rogers6d376ae2013-07-23 15:12:40 -07002763 if ((opcode_flags & Instruction::kContinue) != 0) {
2764 uint32_t next_insn_idx = work_insn_idx_ + CurrentInsnFlags()->GetLengthInCodeUnits();
2765 if (next_insn_idx >= code_item_->insns_size_in_code_units_) {
2766 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Execution can walk off end of code area";
2767 return false;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002768 }
Ian Rogers6d376ae2013-07-23 15:12:40 -07002769 // The only way to get to a move-exception instruction is to get thrown there. Make sure the
2770 // next instruction isn't one.
2771 if (!CheckNotMoveException(code_item_->insns_, next_insn_idx)) {
2772 return false;
2773 }
2774 if (NULL != fallthrough_line.get()) {
2775 // Make workline consistent with fallthrough computed from peephole optimization.
2776 work_line_->CopyFromLine(fallthrough_line.get());
2777 }
Ian Rogersb8c78592013-07-25 23:52:52 +00002778 if (insn_flags_[next_insn_idx].IsReturn()) {
2779 // For returns we only care about the operand to the return, all other registers are dead.
2780 const Instruction* ret_inst = Instruction::At(code_item_->insns_ + next_insn_idx);
2781 Instruction::Code opcode = ret_inst->Opcode();
2782 if ((opcode == Instruction::RETURN_VOID) || (opcode == Instruction::RETURN_VOID_BARRIER)) {
2783 work_line_->MarkAllRegistersAsConflicts();
2784 } else {
2785 if (opcode == Instruction::RETURN_WIDE) {
2786 work_line_->MarkAllRegistersAsConflictsExceptWide(ret_inst->VRegA_11x());
2787 } else {
2788 work_line_->MarkAllRegistersAsConflictsExcept(ret_inst->VRegA_11x());
2789 }
2790 }
2791 }
Ian Rogers6d376ae2013-07-23 15:12:40 -07002792 RegisterLine* next_line = reg_table_.GetLine(next_insn_idx);
2793 if (next_line != NULL) {
2794 // Merge registers into what we have for the next instruction,
2795 // and set the "changed" flag if needed.
2796 if (!UpdateRegisters(next_insn_idx, work_line_.get())) {
2797 return false;
2798 }
2799 } else {
2800 /*
2801 * We're not recording register data for the next instruction, so we don't know what the
2802 * prior state was. We have to assume that something has changed and re-evaluate it.
2803 */
2804 insn_flags_[next_insn_idx].SetChanged();
2805 }
2806 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002807
jeffhaod1f0fde2011-09-08 17:25:33 -07002808 /* If we're returning from the method, make sure monitor stack is empty. */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002809 if ((opcode_flags & Instruction::kReturn) != 0) {
Elliott Hughesb25c3f62012-03-26 16:35:06 -07002810 if (!work_line_->VerifyMonitorStackEmpty()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002811 return false;
2812 }
jeffhaobdb76512011-09-07 11:43:16 -07002813 }
2814
2815 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002816 * Update start_guess. Advance to the next instruction of that's
2817 * possible, otherwise use the branch target if one was found. If
jeffhaobdb76512011-09-07 11:43:16 -07002818 * neither of those exists we're in a return or throw; leave start_guess
2819 * alone and let the caller sort it out.
2820 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002821 if ((opcode_flags & Instruction::kContinue) != 0) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002822 *start_guess = work_insn_idx_ + insn_flags_[work_insn_idx_].GetLengthInCodeUnits();
Elliott Hughesadb8c672012-03-06 16:49:32 -08002823 } else if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002824 /* we're still okay if branch_target is zero */
Ian Rogersd81871c2011-10-03 13:57:23 -07002825 *start_guess = work_insn_idx_ + branch_target;
jeffhaobdb76512011-09-07 11:43:16 -07002826 }
2827
Ian Rogersd81871c2011-10-03 13:57:23 -07002828 DCHECK_LT(*start_guess, code_item_->insns_size_in_code_units_);
2829 DCHECK(insn_flags_[*start_guess].IsOpcode());
jeffhaobdb76512011-09-07 11:43:16 -07002830
2831 return true;
Brian Carlstrom7934ac22013-07-26 10:54:15 -07002832} // NOLINT(readability/fn_size)
jeffhaobdb76512011-09-07 11:43:16 -07002833
Ian Rogers776ac1f2012-04-13 23:36:36 -07002834const RegType& MethodVerifier::ResolveClassAndCheckAccess(uint32_t class_idx) {
Ian Rogers0571d352011-11-03 19:51:38 -07002835 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002836 const RegType& referrer = GetDeclaringClass();
Mathieu Chartier590fee92013-09-13 13:46:47 -07002837 mirror::Class* klass = (*dex_cache_)->GetResolvedType(class_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002838 const RegType& result =
Ian Rogers04f94f42013-06-10 15:09:26 -07002839 klass != NULL ? reg_types_.FromClass(descriptor, klass,
2840 klass->CannotBeAssignedFromOtherTypes())
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07002841 : reg_types_.FromDescriptor(class_loader_->Get(), descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002842 if (result.IsConflict()) {
2843 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "accessing broken descriptor '" << descriptor
2844 << "' in " << referrer;
2845 return result;
2846 }
Ian Rogerse1758fe2012-04-19 11:31:15 -07002847 if (klass == NULL && !result.IsUnresolvedTypes()) {
Mathieu Chartier590fee92013-09-13 13:46:47 -07002848 (*dex_cache_)->SetResolvedType(class_idx, result.GetClass());
Ian Rogerse1758fe2012-04-19 11:31:15 -07002849 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002850 // Check if access is allowed. Unresolved types use xxxWithAccessCheck to
Jeff Haob24b4a72013-07-31 13:47:31 -07002851 // check at runtime if access is allowed and so pass here. If result is
2852 // primitive, skip the access check.
2853 if (result.IsNonZeroReferenceTypes() && !result.IsUnresolvedTypes() &&
2854 !referrer.IsUnresolvedTypes() && !referrer.CanAccess(result)) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002855 Fail(VERIFY_ERROR_ACCESS_CLASS) << "illegal class access: '"
Ian Rogersad0b3a32012-04-16 14:50:24 -07002856 << referrer << "' -> '" << result << "'";
Ian Rogers28ad40d2011-10-27 15:19:26 -07002857 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002858 return result;
Ian Rogersd81871c2011-10-03 13:57:23 -07002859}
2860
Ian Rogers776ac1f2012-04-13 23:36:36 -07002861const RegType& MethodVerifier::GetCaughtExceptionType() {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002862 const RegType* common_super = NULL;
Ian Rogersd81871c2011-10-03 13:57:23 -07002863 if (code_item_->tries_size_ != 0) {
Ian Rogers0571d352011-11-03 19:51:38 -07002864 const byte* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
Ian Rogersd81871c2011-10-03 13:57:23 -07002865 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
2866 for (uint32_t i = 0; i < handlers_size; i++) {
Ian Rogers0571d352011-11-03 19:51:38 -07002867 CatchHandlerIterator iterator(handlers_ptr);
2868 for (; iterator.HasNext(); iterator.Next()) {
2869 if (iterator.GetHandlerAddress() == (uint32_t) work_insn_idx_) {
2870 if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) {
Ian Rogersb4903572012-10-11 11:52:56 -07002871 common_super = &reg_types_.JavaLangThrowable(false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002872 } else {
Ian Rogers0571d352011-11-03 19:51:38 -07002873 const RegType& exception = ResolveClassAndCheckAccess(iterator.GetHandlerTypeIndex());
Jeff Haob878f212014-04-24 16:25:36 -07002874 if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(exception)) {
Jeff Haoc26a56c2013-11-04 12:00:47 -08002875 if (exception.IsUnresolvedTypes()) {
2876 // We don't know enough about the type. Fail here and let runtime handle it.
2877 Fail(VERIFY_ERROR_NO_CLASS) << "unresolved exception class " << exception;
2878 return exception;
2879 } else {
2880 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unexpected non-exception class " << exception;
2881 return reg_types_.Conflict();
2882 }
Jeff Haob878f212014-04-24 16:25:36 -07002883 } else if (common_super == nullptr) {
2884 common_super = &exception;
Ian Rogers28ad40d2011-10-27 15:19:26 -07002885 } else if (common_super->Equals(exception)) {
Ian Rogersc4762272012-02-01 15:55:55 -08002886 // odd case, but nothing to do
Ian Rogersd81871c2011-10-03 13:57:23 -07002887 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002888 common_super = &common_super->Merge(exception, &reg_types_);
Ian Rogersb4903572012-10-11 11:52:56 -07002889 CHECK(reg_types_.JavaLangThrowable(false).IsAssignableFrom(*common_super));
Ian Rogersd81871c2011-10-03 13:57:23 -07002890 }
2891 }
2892 }
2893 }
Ian Rogers0571d352011-11-03 19:51:38 -07002894 handlers_ptr = iterator.EndDataPointer();
Ian Rogersd81871c2011-10-03 13:57:23 -07002895 }
2896 }
2897 if (common_super == NULL) {
2898 /* no catch blocks, or no catches with classes we can find */
jeffhaod5347e02012-03-22 17:25:05 -07002899 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unable to find exception handler";
Ian Rogersad0b3a32012-04-16 14:50:24 -07002900 return reg_types_.Conflict();
Ian Rogersd81871c2011-10-03 13:57:23 -07002901 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002902 return *common_super;
Ian Rogersd81871c2011-10-03 13:57:23 -07002903}
2904
Brian Carlstromea46f952013-07-30 01:26:50 -07002905mirror::ArtMethod* MethodVerifier::ResolveMethodAndCheckAccess(uint32_t dex_method_idx,
Ian Rogersd91d6d62013-09-25 20:26:14 -07002906 MethodType method_type) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002907 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx);
Ian Rogers90040192011-12-16 08:54:29 -08002908 const RegType& klass_type = ResolveClassAndCheckAccess(method_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002909 if (klass_type.IsConflict()) {
2910 std::string append(" in attempt to access method ");
2911 append += dex_file_->GetMethodName(method_id);
2912 AppendToLastFailMessage(append);
Ian Rogers90040192011-12-16 08:54:29 -08002913 return NULL;
2914 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002915 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers90040192011-12-16 08:54:29 -08002916 return NULL; // Can't resolve Class so no more to do here
2917 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002918 mirror::Class* klass = klass_type.GetClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002919 const RegType& referrer = GetDeclaringClass();
Mathieu Chartier590fee92013-09-13 13:46:47 -07002920 mirror::ArtMethod* res_method = (*dex_cache_)->GetResolvedMethod(dex_method_idx);
Ian Rogersd81871c2011-10-03 13:57:23 -07002921 if (res_method == NULL) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07002922 const char* name = dex_file_->GetMethodName(method_id);
Ian Rogersd91d6d62013-09-25 20:26:14 -07002923 const Signature signature = dex_file_->GetMethodSignature(method_id);
jeffhao8cd6dda2012-02-22 10:15:34 -08002924
2925 if (method_type == METHOD_DIRECT || method_type == METHOD_STATIC) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002926 res_method = klass->FindDirectMethod(name, signature);
jeffhao8cd6dda2012-02-22 10:15:34 -08002927 } else if (method_type == METHOD_INTERFACE) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002928 res_method = klass->FindInterfaceMethod(name, signature);
2929 } else {
2930 res_method = klass->FindVirtualMethod(name, signature);
2931 }
2932 if (res_method != NULL) {
Mathieu Chartier590fee92013-09-13 13:46:47 -07002933 (*dex_cache_)->SetResolvedMethod(dex_method_idx, res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07002934 } else {
jeffhao8cd6dda2012-02-22 10:15:34 -08002935 // If a virtual or interface method wasn't found with the expected type, look in
2936 // the direct methods. This can happen when the wrong invoke type is used or when
2937 // a class has changed, and will be flagged as an error in later checks.
2938 if (method_type == METHOD_INTERFACE || method_type == METHOD_VIRTUAL) {
2939 res_method = klass->FindDirectMethod(name, signature);
2940 }
2941 if (res_method == NULL) {
2942 Fail(VERIFY_ERROR_NO_METHOD) << "couldn't find method "
2943 << PrettyDescriptor(klass) << "." << name
2944 << " " << signature;
2945 return NULL;
2946 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002947 }
2948 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002949 // Make sure calls to constructors are "direct". There are additional restrictions but we don't
2950 // enforce them here.
2951 if (res_method->IsConstructor() && method_type != METHOD_DIRECT) {
jeffhaod5347e02012-03-22 17:25:05 -07002952 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting non-direct call to constructor "
2953 << PrettyMethod(res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07002954 return NULL;
2955 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002956 // Disallow any calls to class initializers.
2957 if (MethodHelper(res_method).IsClassInitializer()) {
jeffhaod5347e02012-03-22 17:25:05 -07002958 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting call to class initializer "
2959 << PrettyMethod(res_method);
jeffhao8cd6dda2012-02-22 10:15:34 -08002960 return NULL;
2961 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002962 // Check if access is allowed.
Ian Rogersad0b3a32012-04-16 14:50:24 -07002963 if (!referrer.CanAccessMember(res_method->GetDeclaringClass(), res_method->GetAccessFlags())) {
jeffhao8cd6dda2012-02-22 10:15:34 -08002964 Fail(VERIFY_ERROR_ACCESS_METHOD) << "illegal method access (call " << PrettyMethod(res_method)
Ian Rogersad0b3a32012-04-16 14:50:24 -07002965 << " from " << referrer << ")";
jeffhaob57e9522012-04-26 18:08:21 -07002966 return res_method;
jeffhao8cd6dda2012-02-22 10:15:34 -08002967 }
jeffhaode0d9c92012-02-27 13:58:13 -08002968 // Check that invoke-virtual and invoke-super are not used on private methods of the same class.
2969 if (res_method->IsPrivate() && method_type == METHOD_VIRTUAL) {
jeffhaod5347e02012-03-22 17:25:05 -07002970 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invoke-super/virtual can't be used on private method "
2971 << PrettyMethod(res_method);
jeffhaode0d9c92012-02-27 13:58:13 -08002972 return NULL;
2973 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002974 // Check that interface methods match interface classes.
2975 if (klass->IsInterface() && method_type != METHOD_INTERFACE) {
2976 Fail(VERIFY_ERROR_CLASS_CHANGE) << "non-interface method " << PrettyMethod(res_method)
2977 << " is in an interface class " << PrettyClass(klass);
2978 return NULL;
2979 } else if (!klass->IsInterface() && method_type == METHOD_INTERFACE) {
2980 Fail(VERIFY_ERROR_CLASS_CHANGE) << "interface method " << PrettyMethod(res_method)
2981 << " is in a non-interface class " << PrettyClass(klass);
2982 return NULL;
2983 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002984 // See if the method type implied by the invoke instruction matches the access flags for the
2985 // target method.
2986 if ((method_type == METHOD_DIRECT && !res_method->IsDirect()) ||
2987 (method_type == METHOD_STATIC && !res_method->IsStatic()) ||
2988 ((method_type == METHOD_VIRTUAL || method_type == METHOD_INTERFACE) && res_method->IsDirect())
2989 ) {
Ian Rogers2fc14272012-08-30 10:56:57 -07002990 Fail(VERIFY_ERROR_CLASS_CHANGE) << "invoke type (" << method_type << ") does not match method "
2991 " type of " << PrettyMethod(res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07002992 return NULL;
2993 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002994 return res_method;
2995}
2996
Brian Carlstromea46f952013-07-30 01:26:50 -07002997mirror::ArtMethod* MethodVerifier::VerifyInvocationArgs(const Instruction* inst,
Sebastien Hertz5243e912013-05-21 10:55:07 +02002998 MethodType method_type,
2999 bool is_range,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003000 bool is_super) {
jeffhao8cd6dda2012-02-22 10:15:34 -08003001 // Resolve the method. This could be an abstract or concrete method depending on what sort of call
3002 // we're making.
Sebastien Hertz5243e912013-05-21 10:55:07 +02003003 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Brian Carlstromea46f952013-07-30 01:26:50 -07003004 mirror::ArtMethod* res_method = ResolveMethodAndCheckAccess(method_idx, method_type);
jeffhao8cd6dda2012-02-22 10:15:34 -08003005 if (res_method == NULL) { // error or class is unresolved
3006 return NULL;
3007 }
3008
Ian Rogersd81871c2011-10-03 13:57:23 -07003009 // If we're using invoke-super(method), make sure that the executing method's class' superclass
3010 // has a vtable entry for the target method.
3011 if (is_super) {
3012 DCHECK(method_type == METHOD_VIRTUAL);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003013 const RegType& super = GetDeclaringClass().GetSuperClass(&reg_types_);
Ian Rogers529781d2012-07-23 17:24:29 -07003014 if (super.IsUnresolvedTypes()) {
jeffhao4d8df822012-04-24 17:09:36 -07003015 Fail(VERIFY_ERROR_NO_METHOD) << "unknown super class in invoke-super from "
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003016 << PrettyMethod(dex_method_idx_, *dex_file_)
jeffhao4d8df822012-04-24 17:09:36 -07003017 << " to super " << PrettyMethod(res_method);
3018 return NULL;
3019 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003020 mirror::Class* super_klass = super.GetClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07003021 if (res_method->GetMethodIndex() >= super_klass->GetVTable()->GetLength()) {
jeffhao4d8df822012-04-24 17:09:36 -07003022 MethodHelper mh(res_method);
3023 Fail(VERIFY_ERROR_NO_METHOD) << "invalid invoke-super from "
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003024 << PrettyMethod(dex_method_idx_, *dex_file_)
jeffhao4d8df822012-04-24 17:09:36 -07003025 << " to super " << super
3026 << "." << mh.GetName()
3027 << mh.GetSignature();
Ian Rogersd81871c2011-10-03 13:57:23 -07003028 return NULL;
3029 }
3030 }
3031 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003032 // match the call to the signature. Also, we might be calling through an abstract method
Ian Rogersd81871c2011-10-03 13:57:23 -07003033 // definition (which doesn't have register count values).
Sebastien Hertz5243e912013-05-21 10:55:07 +02003034 const size_t expected_args = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
Ian Rogersd81871c2011-10-03 13:57:23 -07003035 /* caught by static verifier */
3036 DCHECK(is_range || expected_args <= 5);
3037 if (expected_args > code_item_->outs_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07003038 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
Ian Rogersd81871c2011-10-03 13:57:23 -07003039 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
3040 return NULL;
3041 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003042
jeffhaobdb76512011-09-07 11:43:16 -07003043 /*
Ian Rogersad0b3a32012-04-16 14:50:24 -07003044 * Check the "this" argument, which must be an instance of the class that declared the method.
3045 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
3046 * rigorous check here (which is okay since we have to do it at runtime).
jeffhaobdb76512011-09-07 11:43:16 -07003047 */
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003048 size_t actual_args = 0;
Ian Rogersd81871c2011-10-03 13:57:23 -07003049 if (!res_method->IsStatic()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003050 const RegType& actual_arg_type = work_line_->GetInvocationThis(inst, is_range);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003051 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
Ian Rogersd81871c2011-10-03 13:57:23 -07003052 return NULL;
3053 }
3054 if (actual_arg_type.IsUninitializedReference() && !res_method->IsConstructor()) {
jeffhaod5347e02012-03-22 17:25:05 -07003055 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
Ian Rogersd81871c2011-10-03 13:57:23 -07003056 return NULL;
3057 }
3058 if (method_type != METHOD_INTERFACE && !actual_arg_type.IsZero()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003059 mirror::Class* klass = res_method->GetDeclaringClass();
Ian Rogers04f94f42013-06-10 15:09:26 -07003060 const RegType& res_method_class =
Mathieu Chartierf8322842014-05-16 10:59:25 -07003061 reg_types_.FromClass(klass->GetDescriptor().c_str(), klass,
Ian Rogers04f94f42013-06-10 15:09:26 -07003062 klass->CannotBeAssignedFromOtherTypes());
Ian Rogers9074b992011-10-26 17:41:55 -07003063 if (!res_method_class.IsAssignableFrom(actual_arg_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07003064 Fail(actual_arg_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS:
3065 VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003066 << "' not instance of '" << res_method_class << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07003067 return NULL;
3068 }
3069 }
3070 actual_args++;
3071 }
3072 /*
3073 * Process the target method's signature. This signature may or may not
3074 * have been verified, so we can't assume it's properly formed.
3075 */
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003076 MethodHelper mh(res_method);
3077 const DexFile::TypeList* params = mh.GetParameterTypeList();
3078 size_t params_size = params == NULL ? 0 : params->Size();
Sebastien Hertz5243e912013-05-21 10:55:07 +02003079 uint32_t arg[5];
3080 if (!is_range) {
Ian Rogers29a26482014-05-02 15:27:29 -07003081 inst->GetVarArgs(arg);
Sebastien Hertz5243e912013-05-21 10:55:07 +02003082 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003083 for (size_t param_index = 0; param_index < params_size; param_index++) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003084 if (actual_args >= expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07003085 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invalid call to '" << PrettyMethod(res_method)
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003086 << "'. Expected " << expected_args << " arguments, processing argument " << actual_args
3087 << " (where longs/doubles count twice).";
Ian Rogersd81871c2011-10-03 13:57:23 -07003088 return NULL;
3089 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003090 const char* descriptor =
3091 mh.GetTypeDescriptorFromTypeIdx(params->GetTypeItem(param_index).type_idx_);
3092 if (descriptor == NULL) {
jeffhaod5347e02012-03-22 17:25:05 -07003093 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003094 << " missing signature component";
3095 return NULL;
Ian Rogersd81871c2011-10-03 13:57:23 -07003096 }
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003097 const RegType& reg_type = reg_types_.FromDescriptor(class_loader_->Get(), descriptor, false);
Sebastien Hertz5243e912013-05-21 10:55:07 +02003098 uint32_t get_reg = is_range ? inst->VRegC_3rc() + actual_args : arg[actual_args];
Jeff Haoa6b22c52013-10-04 14:33:22 -07003099 if (reg_type.IsIntegralTypes()) {
3100 const RegType& src_type = work_line_->GetRegisterType(get_reg);
3101 if (!src_type.IsIntegralTypes()) {
3102 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register v" << get_reg << " has type " << src_type
3103 << " but expected " << reg_type;
3104 return res_method;
3105 }
3106 } else if (!work_line_->VerifyRegisterType(get_reg, reg_type)) {
jeffhaob57e9522012-04-26 18:08:21 -07003107 return res_method;
Ian Rogersd81871c2011-10-03 13:57:23 -07003108 }
3109 actual_args = reg_type.IsLongOrDoubleTypes() ? actual_args + 2 : actual_args + 1;
3110 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003111 if (actual_args != expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07003112 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003113 << " expected " << expected_args << " arguments, found " << actual_args;
Ian Rogersd81871c2011-10-03 13:57:23 -07003114 return NULL;
3115 } else {
3116 return res_method;
3117 }
3118}
3119
Brian Carlstromea46f952013-07-30 01:26:50 -07003120mirror::ArtMethod* MethodVerifier::GetQuickInvokedMethod(const Instruction* inst,
Mathieu Chartier590fee92013-09-13 13:46:47 -07003121 RegisterLine* reg_line, bool is_range) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003122 DCHECK(inst->Opcode() == Instruction::INVOKE_VIRTUAL_QUICK ||
3123 inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
3124 const RegType& actual_arg_type = reg_line->GetInvocationThis(inst, is_range);
Ian Rogers9bc54402014-04-17 16:40:01 -07003125 if (!actual_arg_type.HasClass()) {
3126 VLOG(verifier) << "Failed to get mirror::Class* from '" << actual_arg_type << "'";
Andreas Gampe63981562014-04-17 12:28:43 -07003127 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003128 }
Ian Rogersa4cf1df2014-05-07 19:47:17 -07003129 mirror::ObjectArray<mirror::ArtMethod>* vtable = nullptr;
3130 mirror::Class* klass = actual_arg_type.GetClass();
3131 if (klass->IsInterface()) {
3132 // Derive Object.class from Class.class.getSuperclass().
3133 mirror::Class* object_klass = klass->GetClass()->GetSuperClass();
3134 CHECK(object_klass->IsObjectClass());
3135 vtable = object_klass->GetVTable();
3136 } else {
3137 vtable = klass->GetVTable();
3138 }
3139 CHECK(vtable != nullptr) << PrettyDescriptor(klass);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003140 uint16_t vtable_index = is_range ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersa4cf1df2014-05-07 19:47:17 -07003141 CHECK_LT(static_cast<int32_t>(vtable_index), vtable->GetLength()) << PrettyDescriptor(klass);
Brian Carlstromea46f952013-07-30 01:26:50 -07003142 mirror::ArtMethod* res_method = vtable->Get(vtable_index);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003143 CHECK(!Thread::Current()->IsExceptionPending());
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003144 return res_method;
3145}
3146
Brian Carlstromea46f952013-07-30 01:26:50 -07003147mirror::ArtMethod* MethodVerifier::VerifyInvokeVirtualQuickArgs(const Instruction* inst,
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003148 bool is_range) {
3149 DCHECK(Runtime::Current()->IsStarted());
Brian Carlstromea46f952013-07-30 01:26:50 -07003150 mirror::ArtMethod* res_method = GetQuickInvokedMethod(inst, work_line_.get(),
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003151 is_range);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003152 if (res_method == NULL) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003153 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer method from " << inst->Name();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003154 return NULL;
3155 }
3156 CHECK(!res_method->IsDirect() && !res_method->IsStatic());
3157
3158 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
3159 // match the call to the signature. Also, we might be calling through an abstract method
3160 // definition (which doesn't have register count values).
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003161 const RegType& actual_arg_type = work_line_->GetInvocationThis(inst, is_range);
3162 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
3163 return NULL;
3164 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003165 const size_t expected_args = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
3166 /* caught by static verifier */
3167 DCHECK(is_range || expected_args <= 5);
3168 if (expected_args > code_item_->outs_size_) {
3169 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
3170 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
3171 return NULL;
3172 }
3173
3174 /*
3175 * Check the "this" argument, which must be an instance of the class that declared the method.
3176 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
3177 * rigorous check here (which is okay since we have to do it at runtime).
3178 */
3179 if (actual_arg_type.IsUninitializedReference() && !res_method->IsConstructor()) {
3180 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
3181 return NULL;
3182 }
3183 if (!actual_arg_type.IsZero()) {
3184 mirror::Class* klass = res_method->GetDeclaringClass();
3185 const RegType& res_method_class =
Mathieu Chartierf8322842014-05-16 10:59:25 -07003186 reg_types_.FromClass(klass->GetDescriptor().c_str(), klass,
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003187 klass->CannotBeAssignedFromOtherTypes());
3188 if (!res_method_class.IsAssignableFrom(actual_arg_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07003189 Fail(actual_arg_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS :
3190 VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003191 << "' not instance of '" << res_method_class << "'";
3192 return NULL;
3193 }
3194 }
3195 /*
3196 * Process the target method's signature. This signature may or may not
3197 * have been verified, so we can't assume it's properly formed.
3198 */
3199 MethodHelper mh(res_method);
3200 const DexFile::TypeList* params = mh.GetParameterTypeList();
3201 size_t params_size = params == NULL ? 0 : params->Size();
3202 uint32_t arg[5];
3203 if (!is_range) {
Ian Rogers29a26482014-05-02 15:27:29 -07003204 inst->GetVarArgs(arg);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003205 }
3206 size_t actual_args = 1;
3207 for (size_t param_index = 0; param_index < params_size; param_index++) {
3208 if (actual_args >= expected_args) {
3209 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invalid call to '" << PrettyMethod(res_method)
Brian Carlstrom93c33962013-07-26 10:37:43 -07003210 << "'. Expected " << expected_args
3211 << " arguments, processing argument " << actual_args
3212 << " (where longs/doubles count twice).";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003213 return NULL;
3214 }
3215 const char* descriptor =
3216 mh.GetTypeDescriptorFromTypeIdx(params->GetTypeItem(param_index).type_idx_);
3217 if (descriptor == NULL) {
3218 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003219 << " missing signature component";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003220 return NULL;
3221 }
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003222 const RegType& reg_type = reg_types_.FromDescriptor(class_loader_->Get(), descriptor, false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003223 uint32_t get_reg = is_range ? inst->VRegC_3rc() + actual_args : arg[actual_args];
3224 if (!work_line_->VerifyRegisterType(get_reg, reg_type)) {
3225 return res_method;
3226 }
3227 actual_args = reg_type.IsLongOrDoubleTypes() ? actual_args + 2 : actual_args + 1;
3228 }
3229 if (actual_args != expected_args) {
3230 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
3231 << " expected " << expected_args << " arguments, found " << actual_args;
3232 return NULL;
3233 } else {
3234 return res_method;
3235 }
3236}
3237
Ian Rogers62342ec2013-06-11 10:26:37 -07003238void MethodVerifier::VerifyNewArray(const Instruction* inst, bool is_filled, bool is_range) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003239 uint32_t type_idx;
3240 if (!is_filled) {
3241 DCHECK_EQ(inst->Opcode(), Instruction::NEW_ARRAY);
3242 type_idx = inst->VRegC_22c();
3243 } else if (!is_range) {
3244 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY);
3245 type_idx = inst->VRegB_35c();
3246 } else {
3247 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY_RANGE);
3248 type_idx = inst->VRegB_3rc();
3249 }
3250 const RegType& res_type = ResolveClassAndCheckAccess(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003251 if (res_type.IsConflict()) { // bad class
3252 DCHECK_NE(failures_.size(), 0U);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003253 } else {
3254 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
3255 if (!res_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003256 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "new-array on non-array class " << res_type;
Ian Rogers0c4a5062012-02-03 15:18:59 -08003257 } else if (!is_filled) {
3258 /* make sure "size" register is valid type */
Sebastien Hertz5243e912013-05-21 10:55:07 +02003259 work_line_->VerifyRegisterType(inst->VRegB_22c(), reg_types_.Integer());
Ian Rogers0c4a5062012-02-03 15:18:59 -08003260 /* set register type to array class */
Ian Rogers62342ec2013-06-11 10:26:37 -07003261 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
3262 work_line_->SetRegisterType(inst->VRegA_22c(), precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003263 } else {
3264 // Verify each register. If "arg_count" is bad, VerifyRegisterType() will run off the end of
3265 // the list and fail. It's legal, if silly, for arg_count to be zero.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003266 const RegType& expected_type = reg_types_.GetComponentType(res_type, class_loader_->Get());
Sebastien Hertz5243e912013-05-21 10:55:07 +02003267 uint32_t arg_count = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
3268 uint32_t arg[5];
3269 if (!is_range) {
Ian Rogers29a26482014-05-02 15:27:29 -07003270 inst->GetVarArgs(arg);
Sebastien Hertz5243e912013-05-21 10:55:07 +02003271 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08003272 for (size_t ui = 0; ui < arg_count; ui++) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003273 uint32_t get_reg = is_range ? inst->VRegC_3rc() + ui : arg[ui];
Ian Rogers0c4a5062012-02-03 15:18:59 -08003274 if (!work_line_->VerifyRegisterType(get_reg, expected_type)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003275 work_line_->SetResultRegisterType(reg_types_.Conflict());
Ian Rogers0c4a5062012-02-03 15:18:59 -08003276 return;
3277 }
3278 }
3279 // filled-array result goes into "result" register
Ian Rogers62342ec2013-06-11 10:26:37 -07003280 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
3281 work_line_->SetResultRegisterType(precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003282 }
3283 }
3284}
3285
Sebastien Hertz5243e912013-05-21 10:55:07 +02003286void MethodVerifier::VerifyAGet(const Instruction* inst,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003287 const RegType& insn_type, bool is_primitive) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003288 const RegType& index_type = work_line_->GetRegisterType(inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07003289 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003290 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07003291 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003292 const RegType& array_type = work_line_->GetRegisterType(inst->VRegB_23x());
Ian Rogers89310de2012-02-01 13:47:30 -08003293 if (array_type.IsZero()) {
3294 // Null array class; this code path will fail at runtime. Infer a merge-able type from the
3295 // instruction type. TODO: have a proper notion of bottom here.
3296 if (!is_primitive || insn_type.IsCategory1Types()) {
3297 // Reference or category 1
Sebastien Hertz5243e912013-05-21 10:55:07 +02003298 work_line_->SetRegisterType(inst->VRegA_23x(), reg_types_.Zero());
Ian Rogersd81871c2011-10-03 13:57:23 -07003299 } else {
Ian Rogers89310de2012-02-01 13:47:30 -08003300 // Category 2
Sebastien Hertz5243e912013-05-21 10:55:07 +02003301 work_line_->SetRegisterTypeWide(inst->VRegA_23x(), reg_types_.FromCat2ConstLo(0, false),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003302 reg_types_.FromCat2ConstHi(0, false));
Ian Rogers89310de2012-02-01 13:47:30 -08003303 }
jeffhaofc3144e2012-02-01 17:21:15 -08003304 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003305 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aget";
Ian Rogers89310de2012-02-01 13:47:30 -08003306 } else {
3307 /* verify the class */
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003308 const RegType& component_type = reg_types_.GetComponentType(array_type, class_loader_->Get());
jeffhaofc3144e2012-02-01 17:21:15 -08003309 if (!component_type.IsReferenceTypes() && !is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07003310 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08003311 << " source for aget-object";
3312 } else if (component_type.IsNonZeroReferenceTypes() && is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07003313 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08003314 << " source for category 1 aget";
3315 } else if (is_primitive && !insn_type.Equals(component_type) &&
3316 !((insn_type.IsInteger() && component_type.IsFloat()) ||
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003317 (insn_type.IsLong() && component_type.IsDouble()))) {
3318 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array type " << array_type
3319 << " incompatible with aget of type " << insn_type;
Ian Rogers89310de2012-02-01 13:47:30 -08003320 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003321 // Use knowledge of the field type which is stronger than the type inferred from the
3322 // instruction, which can't differentiate object types and ints from floats, longs from
3323 // doubles.
3324 if (!component_type.IsLowHalf()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003325 work_line_->SetRegisterType(inst->VRegA_23x(), component_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003326 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003327 work_line_->SetRegisterTypeWide(inst->VRegA_23x(), component_type,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003328 component_type.HighHalf(&reg_types_));
3329 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003330 }
3331 }
3332 }
3333}
3334
Jeff Haofe1f7c82013-08-01 14:50:24 -07003335void MethodVerifier::VerifyPrimitivePut(const RegType& target_type, const RegType& insn_type,
3336 const uint32_t vregA) {
3337 // Primitive assignability rules are weaker than regular assignability rules.
3338 bool instruction_compatible;
3339 bool value_compatible;
3340 const RegType& value_type = work_line_->GetRegisterType(vregA);
3341 if (target_type.IsIntegralTypes()) {
Jeff Haoa4647482013-08-06 15:35:47 -07003342 instruction_compatible = target_type.Equals(insn_type);
Jeff Haofe1f7c82013-08-01 14:50:24 -07003343 value_compatible = value_type.IsIntegralTypes();
3344 } else if (target_type.IsFloat()) {
3345 instruction_compatible = insn_type.IsInteger(); // no put-float, so expect put-int
3346 value_compatible = value_type.IsFloatTypes();
3347 } else if (target_type.IsLong()) {
3348 instruction_compatible = insn_type.IsLong();
3349 value_compatible = value_type.IsLongTypes();
3350 } else if (target_type.IsDouble()) {
3351 instruction_compatible = insn_type.IsLong(); // no put-double, so expect put-long
3352 value_compatible = value_type.IsDoubleTypes();
3353 } else {
3354 instruction_compatible = false; // reference with primitive store
3355 value_compatible = false; // unused
3356 }
3357 if (!instruction_compatible) {
3358 // This is a global failure rather than a class change failure as the instructions and
3359 // the descriptors for the type should have been consistent within the same file at
3360 // compile time.
3361 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "put insn has type '" << insn_type
3362 << "' but expected type '" << target_type << "'";
3363 return;
3364 }
3365 if (!value_compatible) {
3366 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
3367 << " of type " << value_type << " but expected " << target_type << " for put";
3368 return;
3369 }
3370}
3371
Sebastien Hertz5243e912013-05-21 10:55:07 +02003372void MethodVerifier::VerifyAPut(const Instruction* inst,
Sebastien Hertz757b3042014-03-28 14:34:28 +01003373 const RegType& insn_type, bool is_primitive) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003374 const RegType& index_type = work_line_->GetRegisterType(inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07003375 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003376 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07003377 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003378 const RegType& array_type = work_line_->GetRegisterType(inst->VRegB_23x());
Ian Rogers89310de2012-02-01 13:47:30 -08003379 if (array_type.IsZero()) {
3380 // Null array type; this code path will fail at runtime. Infer a merge-able type from the
3381 // instruction type.
jeffhaofc3144e2012-02-01 17:21:15 -08003382 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003383 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aput";
Ian Rogers89310de2012-02-01 13:47:30 -08003384 } else {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003385 const RegType& component_type = reg_types_.GetComponentType(array_type, class_loader_->Get());
Jeff Haofe1f7c82013-08-01 14:50:24 -07003386 const uint32_t vregA = inst->VRegA_23x();
Jeff Haob24b4a72013-07-31 13:47:31 -07003387 if (is_primitive) {
Jeff Haofe1f7c82013-08-01 14:50:24 -07003388 VerifyPrimitivePut(component_type, insn_type, vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07003389 } else {
Jeff Haob24b4a72013-07-31 13:47:31 -07003390 if (!component_type.IsReferenceTypes()) {
3391 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
3392 << " source for aput-object";
3393 } else {
3394 // The instruction agrees with the type of array, confirm the value to be stored does too
3395 // Note: we use the instruction type (rather than the component type) for aput-object as
3396 // incompatible classes will be caught at runtime as an array store exception
Jeff Haofe1f7c82013-08-01 14:50:24 -07003397 work_line_->VerifyRegisterType(vregA, insn_type);
Jeff Haob24b4a72013-07-31 13:47:31 -07003398 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003399 }
3400 }
3401 }
3402}
3403
Brian Carlstromea46f952013-07-30 01:26:50 -07003404mirror::ArtField* MethodVerifier::GetStaticField(int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08003405 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3406 // Check access to class
3407 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Brian Carlstrom7934ac22013-07-26 10:54:15 -07003408 if (klass_type.IsConflict()) { // bad class
Ian Rogersad0b3a32012-04-16 14:50:24 -07003409 AppendToLastFailMessage(StringPrintf(" in attempt to access static field %d (%s) in %s",
3410 field_idx, dex_file_->GetFieldName(field_id),
3411 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers90040192011-12-16 08:54:29 -08003412 return NULL;
3413 }
Elliott Hughesb25c3f62012-03-26 16:35:06 -07003414 if (klass_type.IsUnresolvedTypes()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003415 return NULL; // Can't resolve Class so no more to do here, will do checking at runtime.
Ian Rogers90040192011-12-16 08:54:29 -08003416 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07003417 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
3418 mirror::ArtField* field = class_linker->ResolveFieldJLS(*dex_file_, field_idx, *dex_cache_,
3419 *class_loader_);
Ian Rogersd81871c2011-10-03 13:57:23 -07003420 if (field == NULL) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07003421 VLOG(verifier) << "Unable to resolve static field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07003422 << dex_file_->GetFieldName(field_id) << ") in "
3423 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07003424 DCHECK(Thread::Current()->IsExceptionPending());
3425 Thread::Current()->ClearException();
3426 return NULL;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003427 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
3428 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003429 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access static field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07003430 << " from " << GetDeclaringClass();
Ian Rogersd81871c2011-10-03 13:57:23 -07003431 return NULL;
3432 } else if (!field->IsStatic()) {
3433 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field) << " to be static";
3434 return NULL;
Ian Rogersd81871c2011-10-03 13:57:23 -07003435 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07003436 return field;
Ian Rogersd81871c2011-10-03 13:57:23 -07003437}
3438
Brian Carlstromea46f952013-07-30 01:26:50 -07003439mirror::ArtField* MethodVerifier::GetInstanceField(const RegType& obj_type, int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08003440 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3441 // Check access to class
3442 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003443 if (klass_type.IsConflict()) {
3444 AppendToLastFailMessage(StringPrintf(" in attempt to access instance field %d (%s) in %s",
3445 field_idx, dex_file_->GetFieldName(field_id),
3446 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers90040192011-12-16 08:54:29 -08003447 return NULL;
3448 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003449 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers90040192011-12-16 08:54:29 -08003450 return NULL; // Can't resolve Class so no more to do here
3451 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07003452 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
3453 mirror::ArtField* field = class_linker->ResolveFieldJLS(*dex_file_, field_idx, *dex_cache_,
3454 *class_loader_);
Ian Rogersd81871c2011-10-03 13:57:23 -07003455 if (field == NULL) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07003456 VLOG(verifier) << "Unable to resolve instance field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07003457 << dex_file_->GetFieldName(field_id) << ") in "
3458 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07003459 DCHECK(Thread::Current()->IsExceptionPending());
3460 Thread::Current()->ClearException();
3461 return NULL;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003462 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
3463 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003464 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access instance field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07003465 << " from " << GetDeclaringClass();
Ian Rogersd81871c2011-10-03 13:57:23 -07003466 return NULL;
3467 } else if (field->IsStatic()) {
3468 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field)
3469 << " to not be static";
3470 return NULL;
3471 } else if (obj_type.IsZero()) {
3472 // Cannot infer and check type, however, access will cause null pointer exception
3473 return field;
Ian Rogerse1758fe2012-04-19 11:31:15 -07003474 } else {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003475 mirror::Class* klass = field->GetDeclaringClass();
Ian Rogers637c65b2013-05-31 11:46:00 -07003476 const RegType& field_klass =
3477 reg_types_.FromClass(dex_file_->GetFieldDeclaringClassDescriptor(field_id),
Ian Rogers04f94f42013-06-10 15:09:26 -07003478 klass, klass->CannotBeAssignedFromOtherTypes());
Ian Rogersad0b3a32012-04-16 14:50:24 -07003479 if (obj_type.IsUninitializedTypes() &&
3480 (!IsConstructor() || GetDeclaringClass().Equals(obj_type) ||
3481 !field_klass.Equals(GetDeclaringClass()))) {
3482 // Field accesses through uninitialized references are only allowable for constructors where
3483 // the field is declared in this class
3484 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "cannot access instance field " << PrettyField(field)
Brian Carlstrom93c33962013-07-26 10:37:43 -07003485 << " of a not fully initialized object within the context"
3486 << " of " << PrettyMethod(dex_method_idx_, *dex_file_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003487 return NULL;
3488 } else if (!field_klass.IsAssignableFrom(obj_type)) {
3489 // Trying to access C1.field1 using reference of type C2, which is neither C1 or a sub-class
3490 // of C1. For resolution to occur the declared class of the field must be compatible with
3491 // obj_type, we've discovered this wasn't so, so report the field didn't exist.
3492 Fail(VERIFY_ERROR_NO_FIELD) << "cannot access instance field " << PrettyField(field)
3493 << " from object of type " << obj_type;
3494 return NULL;
3495 } else {
3496 return field;
3497 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003498 }
3499}
3500
Sebastien Hertz5243e912013-05-21 10:55:07 +02003501void MethodVerifier::VerifyISGet(const Instruction* inst, const RegType& insn_type,
3502 bool is_primitive, bool is_static) {
3503 uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
Brian Carlstromea46f952013-07-30 01:26:50 -07003504 mirror::ArtField* field;
Ian Rogersb94a27b2011-10-26 00:33:41 -07003505 if (is_static) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07003506 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003507 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003508 const RegType& object_type = work_line_->GetRegisterType(inst->VRegB_22c());
Ian Rogersf4028cc2011-11-02 14:56:39 -07003509 field = GetInstanceField(object_type, field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003510 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003511 const RegType* field_type = nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003512 if (field != NULL) {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003513 FieldHelper fh(field);
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08003514 mirror::Class* field_type_class = fh.GetType(can_load_classes_);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003515 if (field_type_class != nullptr) {
3516 field_type = &reg_types_.FromClass(fh.GetTypeDescriptor(), field_type_class,
3517 field_type_class->CannotBeAssignedFromOtherTypes());
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08003518 } else {
3519 Thread* self = Thread::Current();
3520 DCHECK(!can_load_classes_ || self->IsExceptionPending());
3521 self->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003522 }
Ian Rogers0d604842012-04-16 14:50:24 -07003523 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003524 if (field_type == nullptr) {
3525 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3526 const char* descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003527 field_type = &reg_types_.FromDescriptor(class_loader_->Get(), descriptor, false);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003528 }
Sebastien Hertz757b3042014-03-28 14:34:28 +01003529 DCHECK(field_type != nullptr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02003530 const uint32_t vregA = (is_static) ? inst->VRegA_21c() : inst->VRegA_22c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07003531 if (is_primitive) {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003532 if (field_type->Equals(insn_type) ||
3533 (field_type->IsFloat() && insn_type.IsInteger()) ||
3534 (field_type->IsDouble() && insn_type.IsLong())) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003535 // expected that read is of the correct primitive type or that int reads are reading
3536 // floats or long reads are reading doubles
3537 } else {
3538 // This is a global failure rather than a class change failure as the instructions and
3539 // the descriptors for the type should have been consistent within the same file at
3540 // compile time
3541 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
3542 << " to be of type '" << insn_type
Sebastien Hertz757b3042014-03-28 14:34:28 +01003543 << "' but found type '" << *field_type << "' in get";
Ian Rogersad0b3a32012-04-16 14:50:24 -07003544 return;
3545 }
3546 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003547 if (!insn_type.IsAssignableFrom(*field_type)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003548 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
3549 << " to be compatible with type '" << insn_type
Sebastien Hertz757b3042014-03-28 14:34:28 +01003550 << "' but found type '" << *field_type
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003551 << "' in Get-object";
Sebastien Hertz5243e912013-05-21 10:55:07 +02003552 work_line_->SetRegisterType(vregA, reg_types_.Conflict());
Ian Rogersad0b3a32012-04-16 14:50:24 -07003553 return;
3554 }
3555 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003556 if (!field_type->IsLowHalf()) {
3557 work_line_->SetRegisterType(vregA, *field_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003558 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003559 work_line_->SetRegisterTypeWide(vregA, *field_type, field_type->HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003560 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003561}
3562
Sebastien Hertz5243e912013-05-21 10:55:07 +02003563void MethodVerifier::VerifyISPut(const Instruction* inst, const RegType& insn_type,
3564 bool is_primitive, bool is_static) {
3565 uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
Brian Carlstromea46f952013-07-30 01:26:50 -07003566 mirror::ArtField* field;
Ian Rogersb94a27b2011-10-26 00:33:41 -07003567 if (is_static) {
Ian Rogers55d249f2011-11-02 16:48:09 -07003568 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003569 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003570 const RegType& object_type = work_line_->GetRegisterType(inst->VRegB_22c());
Ian Rogers55d249f2011-11-02 16:48:09 -07003571 field = GetInstanceField(object_type, field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003572 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003573 const RegType* field_type = nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003574 if (field != NULL) {
3575 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
3576 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
3577 << " from other class " << GetDeclaringClass();
3578 return;
3579 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003580 FieldHelper fh(field);
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08003581 mirror::Class* field_type_class = fh.GetType(can_load_classes_);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003582 if (field_type_class != nullptr) {
3583 field_type = &reg_types_.FromClass(fh.GetTypeDescriptor(), field_type_class,
3584 field_type_class->CannotBeAssignedFromOtherTypes());
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08003585 } else {
3586 Thread* self = Thread::Current();
3587 DCHECK(!can_load_classes_ || self->IsExceptionPending());
3588 self->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003589 }
3590 }
3591 if (field_type == nullptr) {
3592 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3593 const char* descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003594 field_type = &reg_types_.FromDescriptor(class_loader_->Get(), descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003595 }
Sebastien Hertz757b3042014-03-28 14:34:28 +01003596 DCHECK(field_type != nullptr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02003597 const uint32_t vregA = (is_static) ? inst->VRegA_21c() : inst->VRegA_22c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07003598 if (is_primitive) {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003599 VerifyPrimitivePut(*field_type, insn_type, vregA);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003600 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003601 if (!insn_type.IsAssignableFrom(*field_type)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003602 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
3603 << " to be compatible with type '" << insn_type
Sebastien Hertz757b3042014-03-28 14:34:28 +01003604 << "' but found type '" << *field_type
Ian Rogersad0b3a32012-04-16 14:50:24 -07003605 << "' in put-object";
3606 return;
3607 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003608 work_line_->VerifyRegisterType(vregA, *field_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07003609 }
3610}
3611
Brian Carlstromea46f952013-07-30 01:26:50 -07003612mirror::ArtField* MethodVerifier::GetQuickFieldAccess(const Instruction* inst,
Ian Rogers98379392014-02-24 16:53:16 -08003613 RegisterLine* reg_line) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003614 DCHECK(inst->Opcode() == Instruction::IGET_QUICK ||
3615 inst->Opcode() == Instruction::IGET_WIDE_QUICK ||
3616 inst->Opcode() == Instruction::IGET_OBJECT_QUICK ||
3617 inst->Opcode() == Instruction::IPUT_QUICK ||
3618 inst->Opcode() == Instruction::IPUT_WIDE_QUICK ||
3619 inst->Opcode() == Instruction::IPUT_OBJECT_QUICK);
3620 const RegType& object_type = reg_line->GetRegisterType(inst->VRegB_22c());
Ian Rogers9bc54402014-04-17 16:40:01 -07003621 if (!object_type.HasClass()) {
3622 VLOG(verifier) << "Failed to get mirror::Class* from '" << object_type << "'";
3623 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003624 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003625 uint32_t field_offset = static_cast<uint32_t>(inst->VRegC_22c());
Sebastien Hertz479fc1e2014-04-04 17:51:34 +02003626 mirror::ArtField* f = mirror::ArtField::FindInstanceFieldWithOffset(object_type.GetClass(),
3627 field_offset);
3628 if (f == nullptr) {
3629 VLOG(verifier) << "Failed to find instance field at offset '" << field_offset
3630 << "' from '" << PrettyDescriptor(object_type.GetClass()) << "'";
3631 }
3632 return f;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003633}
3634
3635void MethodVerifier::VerifyIGetQuick(const Instruction* inst, const RegType& insn_type,
3636 bool is_primitive) {
3637 DCHECK(Runtime::Current()->IsStarted());
Brian Carlstromea46f952013-07-30 01:26:50 -07003638 mirror::ArtField* field = GetQuickFieldAccess(inst, work_line_.get());
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003639 if (field == NULL) {
3640 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field from " << inst->Name();
3641 return;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003642 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003643 FieldHelper fh(field);
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08003644 mirror::Class* field_type_class = fh.GetType(can_load_classes_);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003645 const RegType* field_type;
3646 if (field_type_class != nullptr) {
3647 field_type = &reg_types_.FromClass(fh.GetTypeDescriptor(), field_type_class,
3648 field_type_class->CannotBeAssignedFromOtherTypes());
3649 } else {
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08003650 Thread* self = Thread::Current();
3651 DCHECK(!can_load_classes_ || self->IsExceptionPending());
3652 self->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003653 field_type = &reg_types_.FromDescriptor(field->GetDeclaringClass()->GetClassLoader(),
3654 fh.GetTypeDescriptor(), false);
3655 }
Sebastien Hertz757b3042014-03-28 14:34:28 +01003656 DCHECK(field_type != nullptr);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003657 const uint32_t vregA = inst->VRegA_22c();
3658 if (is_primitive) {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003659 if (field_type->Equals(insn_type) ||
3660 (field_type->IsFloat() && insn_type.IsIntegralTypes()) ||
3661 (field_type->IsDouble() && insn_type.IsLongTypes())) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003662 // expected that read is of the correct primitive type or that int reads are reading
3663 // floats or long reads are reading doubles
3664 } else {
3665 // This is a global failure rather than a class change failure as the instructions and
3666 // the descriptors for the type should have been consistent within the same file at
3667 // compile time
3668 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003669 << " to be of type '" << insn_type
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003670 << "' but found type '" << *field_type << "' in Get";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003671 return;
3672 }
3673 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003674 if (!insn_type.IsAssignableFrom(*field_type)) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003675 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003676 << " to be compatible with type '" << insn_type
Sebastien Hertz757b3042014-03-28 14:34:28 +01003677 << "' but found type '" << *field_type
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003678 << "' in get-object";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003679 work_line_->SetRegisterType(vregA, reg_types_.Conflict());
3680 return;
3681 }
3682 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003683 if (!field_type->IsLowHalf()) {
3684 work_line_->SetRegisterType(vregA, *field_type);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003685 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003686 work_line_->SetRegisterTypeWide(vregA, *field_type, field_type->HighHalf(&reg_types_));
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003687 }
3688}
3689
3690void MethodVerifier::VerifyIPutQuick(const Instruction* inst, const RegType& insn_type,
3691 bool is_primitive) {
3692 DCHECK(Runtime::Current()->IsStarted());
Brian Carlstromea46f952013-07-30 01:26:50 -07003693 mirror::ArtField* field = GetQuickFieldAccess(inst, work_line_.get());
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003694 if (field == NULL) {
3695 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field from " << inst->Name();
3696 return;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003697 }
3698 const char* descriptor = FieldHelper(field).GetTypeDescriptor();
3699 mirror::ClassLoader* loader = field->GetDeclaringClass()->GetClassLoader();
3700 const RegType& field_type = reg_types_.FromDescriptor(loader, descriptor, false);
3701 if (field != NULL) {
3702 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
3703 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003704 << " from other class " << GetDeclaringClass();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003705 return;
3706 }
3707 }
3708 const uint32_t vregA = inst->VRegA_22c();
3709 if (is_primitive) {
3710 // Primitive field assignability rules are weaker than regular assignability rules
3711 bool instruction_compatible;
3712 bool value_compatible;
3713 const RegType& value_type = work_line_->GetRegisterType(vregA);
3714 if (field_type.IsIntegralTypes()) {
3715 instruction_compatible = insn_type.IsIntegralTypes();
3716 value_compatible = value_type.IsIntegralTypes();
3717 } else if (field_type.IsFloat()) {
3718 instruction_compatible = insn_type.IsInteger(); // no [is]put-float, so expect [is]put-int
3719 value_compatible = value_type.IsFloatTypes();
3720 } else if (field_type.IsLong()) {
3721 instruction_compatible = insn_type.IsLong();
3722 value_compatible = value_type.IsLongTypes();
3723 } else if (field_type.IsDouble()) {
3724 instruction_compatible = insn_type.IsLong(); // no [is]put-double, so expect [is]put-long
3725 value_compatible = value_type.IsDoubleTypes();
3726 } else {
3727 instruction_compatible = false; // reference field with primitive store
3728 value_compatible = false; // unused
3729 }
3730 if (!instruction_compatible) {
3731 // This is a global failure rather than a class change failure as the instructions and
3732 // the descriptors for the type should have been consistent within the same file at
3733 // compile time
3734 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003735 << " to be of type '" << insn_type
3736 << "' but found type '" << field_type
3737 << "' in put";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003738 return;
3739 }
3740 if (!value_compatible) {
3741 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
3742 << " of type " << value_type
3743 << " but expected " << field_type
3744 << " for store to " << PrettyField(field) << " in put";
3745 return;
3746 }
3747 } else {
3748 if (!insn_type.IsAssignableFrom(field_type)) {
3749 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003750 << " to be compatible with type '" << insn_type
3751 << "' but found type '" << field_type
3752 << "' in put-object";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003753 return;
3754 }
3755 work_line_->VerifyRegisterType(vregA, field_type);
3756 }
3757}
3758
Ian Rogers776ac1f2012-04-13 23:36:36 -07003759bool MethodVerifier::CheckNotMoveException(const uint16_t* insns, int insn_idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003760 if ((insns[insn_idx] & 0xff) == Instruction::MOVE_EXCEPTION) {
jeffhaod5347e02012-03-22 17:25:05 -07003761 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-exception";
Ian Rogersd81871c2011-10-03 13:57:23 -07003762 return false;
3763 }
3764 return true;
3765}
3766
Ian Rogers776ac1f2012-04-13 23:36:36 -07003767bool MethodVerifier::UpdateRegisters(uint32_t next_insn, const RegisterLine* merge_line) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003768 bool changed = true;
3769 RegisterLine* target_line = reg_table_.GetLine(next_insn);
3770 if (!insn_flags_[next_insn].IsVisitedOrChanged()) {
jeffhaobdb76512011-09-07 11:43:16 -07003771 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003772 * We haven't processed this instruction before, and we haven't touched the registers here, so
3773 * there's nothing to "merge". Copy the registers over and mark it as changed. (This is the
3774 * only way a register can transition out of "unknown", so this is not just an optimization.)
jeffhaobdb76512011-09-07 11:43:16 -07003775 */
Ian Rogersb8c78592013-07-25 23:52:52 +00003776 if (!insn_flags_[next_insn].IsReturn()) {
3777 target_line->CopyFromLine(merge_line);
3778 } else {
Jeff Haob24b4a72013-07-31 13:47:31 -07003779 // Verify that the monitor stack is empty on return.
3780 if (!merge_line->VerifyMonitorStackEmpty()) {
3781 return false;
3782 }
Ian Rogersb8c78592013-07-25 23:52:52 +00003783 // For returns we only care about the operand to the return, all other registers are dead.
3784 // Initialize them as conflicts so they don't add to GC and deoptimization information.
3785 const Instruction* ret_inst = Instruction::At(code_item_->insns_ + next_insn);
3786 Instruction::Code opcode = ret_inst->Opcode();
3787 if ((opcode == Instruction::RETURN_VOID) || (opcode == Instruction::RETURN_VOID_BARRIER)) {
3788 target_line->MarkAllRegistersAsConflicts();
3789 } else {
3790 target_line->CopyFromLine(merge_line);
3791 if (opcode == Instruction::RETURN_WIDE) {
3792 target_line->MarkAllRegistersAsConflictsExceptWide(ret_inst->VRegA_11x());
3793 } else {
3794 target_line->MarkAllRegistersAsConflictsExcept(ret_inst->VRegA_11x());
3795 }
3796 }
3797 }
jeffhaobdb76512011-09-07 11:43:16 -07003798 } else {
Ian Rogers700a4022014-05-19 16:49:03 -07003799 std::unique_ptr<RegisterLine> copy(gDebugVerify ?
Ian Rogersd0fbd852013-09-24 18:17:04 -07003800 RegisterLine::Create(target_line->NumRegs(), this) :
Brian Carlstrom93c33962013-07-26 10:37:43 -07003801 NULL);
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003802 if (gDebugVerify) {
3803 copy->CopyFromLine(target_line);
3804 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003805 changed = target_line->MergeRegisters(merge_line);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003806 if (have_pending_hard_failure_) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003807 return false;
jeffhaobdb76512011-09-07 11:43:16 -07003808 }
Ian Rogers2c8a8572011-10-24 17:11:36 -07003809 if (gDebugVerify && changed) {
Elliott Hughes398f64b2012-03-26 18:05:48 -07003810 LogVerifyInfo() << "Merging at [" << reinterpret_cast<void*>(work_insn_idx_) << "]"
Elliott Hughesc073b072012-05-24 19:29:17 -07003811 << " to [" << reinterpret_cast<void*>(next_insn) << "]: " << "\n"
3812 << *copy.get() << " MERGE\n"
3813 << *merge_line << " ==\n"
3814 << *target_line << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07003815 }
3816 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003817 if (changed) {
3818 insn_flags_[next_insn].SetChanged();
jeffhaobdb76512011-09-07 11:43:16 -07003819 }
3820 return true;
3821}
3822
Ian Rogers7b3ddd22013-02-21 15:19:52 -08003823InstructionFlags* MethodVerifier::CurrentInsnFlags() {
Ian Rogers776ac1f2012-04-13 23:36:36 -07003824 return &insn_flags_[work_insn_idx_];
3825}
3826
Ian Rogersad0b3a32012-04-16 14:50:24 -07003827const RegType& MethodVerifier::GetMethodReturnType() {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003828 if (return_type_ == nullptr) {
3829 if (mirror_method_ != NULL) {
3830 MethodHelper mh(mirror_method_);
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08003831 mirror::Class* return_type_class = mh.GetReturnType(can_load_classes_);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003832 if (return_type_class != nullptr) {
Mathieu Chartier590fee92013-09-13 13:46:47 -07003833 return_type_ = &reg_types_.FromClass(mh.GetReturnTypeDescriptor(), return_type_class,
3834 return_type_class->CannotBeAssignedFromOtherTypes());
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003835 } else {
3836 Thread* self = Thread::Current();
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08003837 DCHECK(!can_load_classes_ || self->IsExceptionPending());
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003838 self->ClearException();
3839 }
3840 }
3841 if (return_type_ == nullptr) {
3842 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
3843 const DexFile::ProtoId& proto_id = dex_file_->GetMethodPrototype(method_id);
3844 uint16_t return_type_idx = proto_id.return_type_idx_;
3845 const char* descriptor = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(return_type_idx));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003846 return_type_ = &reg_types_.FromDescriptor(class_loader_->Get(), descriptor, false);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003847 }
3848 }
3849 return *return_type_;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003850}
3851
3852const RegType& MethodVerifier::GetDeclaringClass() {
Ian Rogers637c65b2013-05-31 11:46:00 -07003853 if (declaring_class_ == NULL) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003854 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
Brian Carlstrom93c33962013-07-26 10:37:43 -07003855 const char* descriptor
3856 = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(method_id.class_idx_));
Ian Rogers637c65b2013-05-31 11:46:00 -07003857 if (mirror_method_ != NULL) {
3858 mirror::Class* klass = mirror_method_->GetDeclaringClass();
Ian Rogers04f94f42013-06-10 15:09:26 -07003859 declaring_class_ = &reg_types_.FromClass(descriptor, klass,
3860 klass->CannotBeAssignedFromOtherTypes());
Ian Rogers637c65b2013-05-31 11:46:00 -07003861 } else {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003862 declaring_class_ = &reg_types_.FromDescriptor(class_loader_->Get(), descriptor, false);
Ian Rogers637c65b2013-05-31 11:46:00 -07003863 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003864 }
Ian Rogers637c65b2013-05-31 11:46:00 -07003865 return *declaring_class_;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003866}
3867
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003868std::vector<int32_t> MethodVerifier::DescribeVRegs(uint32_t dex_pc) {
3869 RegisterLine* line = reg_table_.GetLine(dex_pc);
Sebastien Hertzaa0c00c2014-03-14 17:58:54 +01003870 DCHECK(line != nullptr) << "No register line at DEX pc " << StringPrintf("0x%x", dex_pc);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003871 std::vector<int32_t> result;
3872 for (size_t i = 0; i < line->NumRegs(); ++i) {
3873 const RegType& type = line->GetRegisterType(i);
3874 if (type.IsConstant()) {
3875 result.push_back(type.IsPreciseConstant() ? kConstant : kImpreciseConstant);
3876 result.push_back(type.ConstantValue());
3877 } else if (type.IsConstantLo()) {
3878 result.push_back(type.IsPreciseConstantLo() ? kConstant : kImpreciseConstant);
3879 result.push_back(type.ConstantValueLo());
3880 } else if (type.IsConstantHi()) {
3881 result.push_back(type.IsPreciseConstantHi() ? kConstant : kImpreciseConstant);
3882 result.push_back(type.ConstantValueHi());
3883 } else if (type.IsIntegralTypes()) {
3884 result.push_back(kIntVReg);
3885 result.push_back(0);
3886 } else if (type.IsFloat()) {
3887 result.push_back(kFloatVReg);
3888 result.push_back(0);
3889 } else if (type.IsLong()) {
3890 result.push_back(kLongLoVReg);
3891 result.push_back(0);
3892 result.push_back(kLongHiVReg);
3893 result.push_back(0);
3894 ++i;
3895 } else if (type.IsDouble()) {
3896 result.push_back(kDoubleLoVReg);
3897 result.push_back(0);
3898 result.push_back(kDoubleHiVReg);
3899 result.push_back(0);
3900 ++i;
3901 } else if (type.IsUndefined() || type.IsConflict() || type.IsHighHalf()) {
3902 result.push_back(kUndefined);
3903 result.push_back(0);
3904 } else {
Ian Rogers7b3ddd22013-02-21 15:19:52 -08003905 CHECK(type.IsNonZeroReferenceTypes());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003906 result.push_back(kReferenceVReg);
3907 result.push_back(0);
3908 }
3909 }
3910 return result;
3911}
3912
Sebastien Hertz849600b2013-12-20 10:28:08 +01003913const RegType& MethodVerifier::DetermineCat1Constant(int32_t value, bool precise) {
3914 if (precise) {
3915 // Precise constant type.
3916 return reg_types_.FromCat1Const(value, true);
3917 } else {
3918 // Imprecise constant type.
3919 if (value < -32768) {
3920 return reg_types_.IntConstant();
3921 } else if (value < -128) {
3922 return reg_types_.ShortConstant();
3923 } else if (value < 0) {
3924 return reg_types_.ByteConstant();
3925 } else if (value == 0) {
3926 return reg_types_.Zero();
3927 } else if (value == 1) {
3928 return reg_types_.One();
3929 } else if (value < 128) {
3930 return reg_types_.PosByteConstant();
3931 } else if (value < 32768) {
3932 return reg_types_.PosShortConstant();
3933 } else if (value < 65536) {
3934 return reg_types_.CharConstant();
3935 } else {
3936 return reg_types_.IntConstant();
3937 }
3938 }
3939}
3940
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003941void MethodVerifier::Init() {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08003942 art::verifier::RegTypeCache::Init();
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003943}
3944
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003945void MethodVerifier::Shutdown() {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08003946 verifier::RegTypeCache::ShutDown();
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08003947}
jeffhaod1224c72012-02-29 13:43:08 -08003948
Mathieu Chartier83c8ee02014-01-28 14:50:23 -08003949void MethodVerifier::VisitRoots(RootCallback* callback, void* arg) {
3950 reg_types_.VisitRoots(callback, arg);
Mathieu Chartierc528dba2013-11-26 12:00:11 -08003951}
3952
Ian Rogersd81871c2011-10-03 13:57:23 -07003953} // namespace verifier
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07003954} // namespace art