blob: 0a19ce189d3a5c10cfbbe86fc2b93f9ed4810fd4 [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(),
Ian Rogers46960fe2014-05-23 10:43:43 -0700169 allow_soft_failures,
170 false);
jeffhaof1e6b7c2012-06-05 18:33:30 -0700171 if (result != kNoFailure) {
172 if (result == kHardFailure) {
173 hard_fail = true;
174 if (error_count > 0) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700175 *error += "\n";
jeffhaof1e6b7c2012-06-05 18:33:30 -0700176 }
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700177 *error = "Verifier rejected class ";
178 *error += PrettyDescriptor(dex_file->GetClassDescriptor(*class_def));
179 *error += " due to bad method ";
180 *error += PrettyMethod(method_idx, *dex_file);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700181 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700182 ++error_count;
jeffhaof56197c2012-03-05 18:01:54 -0800183 }
184 it.Next();
185 }
jeffhao9b0b1882012-10-01 16:51:22 -0700186 int64_t previous_virtual_method_idx = -1;
jeffhaof56197c2012-03-05 18:01:54 -0800187 while (it.HasNextVirtualMethod()) {
188 uint32_t method_idx = it.GetMemberIndex();
jeffhao9b0b1882012-10-01 16:51:22 -0700189 if (method_idx == previous_virtual_method_idx) {
190 // smali can create dex files with two encoded_methods sharing the same method_idx
191 // http://code.google.com/p/smali/issues/detail?id=119
192 it.Next();
193 continue;
194 }
195 previous_virtual_method_idx = method_idx;
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700196 InvokeType type = it.GetMethodInvokeType(*class_def);
Brian Carlstromea46f952013-07-30 01:26:50 -0700197 mirror::ArtMethod* method =
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700198 linker->ResolveMethod(*dex_file, method_idx, dex_cache, class_loader,
199 NullHandle<mirror::ArtMethod>(), type);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700200 if (method == NULL) {
201 DCHECK(Thread::Current()->IsExceptionPending());
202 // We couldn't resolve the method, but continue regardless.
203 Thread::Current()->ClearException();
204 }
Brian Carlstrom93c33962013-07-26 10:37:43 -0700205 MethodVerifier::FailureKind result = VerifyMethod(method_idx,
206 dex_file,
207 dex_cache,
208 class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700209 class_def,
Brian Carlstrom93c33962013-07-26 10:37:43 -0700210 it.GetMethodCodeItem(),
211 method,
212 it.GetMemberAccessFlags(),
Ian Rogers46960fe2014-05-23 10:43:43 -0700213 allow_soft_failures,
214 false);
jeffhaof1e6b7c2012-06-05 18:33:30 -0700215 if (result != kNoFailure) {
216 if (result == kHardFailure) {
217 hard_fail = true;
218 if (error_count > 0) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700219 *error += "\n";
jeffhaof1e6b7c2012-06-05 18:33:30 -0700220 }
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700221 *error = "Verifier rejected class ";
222 *error += PrettyDescriptor(dex_file->GetClassDescriptor(*class_def));
223 *error += " due to bad method ";
224 *error += PrettyMethod(method_idx, *dex_file);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700225 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700226 ++error_count;
jeffhaof56197c2012-03-05 18:01:54 -0800227 }
228 it.Next();
229 }
jeffhaof1e6b7c2012-06-05 18:33:30 -0700230 if (error_count == 0) {
231 return kNoFailure;
232 } else {
233 return hard_fail ? kHardFailure : kSoftFailure;
234 }
jeffhaof56197c2012-03-05 18:01:54 -0800235}
236
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800237MethodVerifier::FailureKind MethodVerifier::VerifyMethod(uint32_t method_idx,
238 const DexFile* dex_file,
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700239 Handle<mirror::DexCache> dex_cache,
240 Handle<mirror::ClassLoader> class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700241 const DexFile::ClassDef* class_def,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800242 const DexFile::CodeItem* code_item,
Brian Carlstromea46f952013-07-30 01:26:50 -0700243 mirror::ArtMethod* method,
Jeff Haoee988952013-04-16 14:23:47 -0700244 uint32_t method_access_flags,
Ian Rogers46960fe2014-05-23 10:43:43 -0700245 bool allow_soft_failures,
246 bool need_precise_constants) {
Ian Rogersc8982582012-09-07 16:53:25 -0700247 MethodVerifier::FailureKind result = kNoFailure;
248 uint64_t start_ns = NanoTime();
249
Ian Rogers46960fe2014-05-23 10:43:43 -0700250 MethodVerifier verifier(dex_file, &dex_cache, &class_loader, class_def, code_item,
251 method_idx, method, method_access_flags, true, allow_soft_failures,
252 need_precise_constants);
253 if (verifier.Verify()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700254 // Verification completed, however failures may be pending that didn't cause the verification
255 // to hard fail.
Ian Rogers46960fe2014-05-23 10:43:43 -0700256 CHECK(!verifier.have_pending_hard_failure_);
257 if (verifier.failures_.size() != 0) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -0700258 if (VLOG_IS_ON(verifier)) {
Ian Rogers46960fe2014-05-23 10:43:43 -0700259 verifier.DumpFailures(VLOG_STREAM(verifier) << "Soft verification failures in "
Anwar Ghuloum75a43f12013-08-13 17:22:14 -0700260 << PrettyMethod(method_idx, *dex_file) << "\n");
261 }
Ian Rogersc8982582012-09-07 16:53:25 -0700262 result = kSoftFailure;
jeffhaof56197c2012-03-05 18:01:54 -0800263 }
264 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700265 // Bad method data.
Ian Rogers46960fe2014-05-23 10:43:43 -0700266 CHECK_NE(verifier.failures_.size(), 0U);
267 CHECK(verifier.have_pending_hard_failure_);
268 verifier.DumpFailures(LOG(INFO) << "Verification error in "
Elliott Hughesc073b072012-05-24 19:29:17 -0700269 << PrettyMethod(method_idx, *dex_file) << "\n");
jeffhaof56197c2012-03-05 18:01:54 -0800270 if (gDebugVerify) {
Ian Rogers46960fe2014-05-23 10:43:43 -0700271 std::cout << "\n" << verifier.info_messages_.str();
272 verifier.Dump(std::cout);
jeffhaof56197c2012-03-05 18:01:54 -0800273 }
Ian Rogersc8982582012-09-07 16:53:25 -0700274 result = kHardFailure;
jeffhaof56197c2012-03-05 18:01:54 -0800275 }
Ian Rogersc8982582012-09-07 16:53:25 -0700276 uint64_t duration_ns = NanoTime() - start_ns;
277 if (duration_ns > MsToNs(100)) {
278 LOG(WARNING) << "Verification of " << PrettyMethod(method_idx, *dex_file)
279 << " took " << PrettyDuration(duration_ns);
280 }
281 return result;
jeffhaof56197c2012-03-05 18:01:54 -0800282}
283
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800284void MethodVerifier::VerifyMethodAndDump(std::ostream& os, uint32_t dex_method_idx,
Mathieu Chartier590fee92013-09-13 13:46:47 -0700285 const DexFile* dex_file,
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700286 Handle<mirror::DexCache> dex_cache,
287 Handle<mirror::ClassLoader> class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700288 const DexFile::ClassDef* class_def,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800289 const DexFile::CodeItem* code_item,
Brian Carlstromea46f952013-07-30 01:26:50 -0700290 mirror::ArtMethod* method,
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800291 uint32_t method_access_flags) {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700292 MethodVerifier verifier(dex_file, &dex_cache, &class_loader, class_def, code_item,
Ian Rogers46960fe2014-05-23 10:43:43 -0700293 dex_method_idx, method, method_access_flags, true, true, true);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700294 verifier.Verify();
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800295 verifier.DumpFailures(os);
296 os << verifier.info_messages_.str();
297 verifier.Dump(os);
298}
299
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700300MethodVerifier::MethodVerifier(const DexFile* dex_file, Handle<mirror::DexCache>* dex_cache,
301 Handle<mirror::ClassLoader>* class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700302 const DexFile::ClassDef* class_def,
Mathieu Chartier590fee92013-09-13 13:46:47 -0700303 const DexFile::CodeItem* code_item, uint32_t dex_method_idx,
304 mirror::ArtMethod* method, uint32_t method_access_flags,
Ian Rogers46960fe2014-05-23 10:43:43 -0700305 bool can_load_classes, bool allow_soft_failures,
306 bool need_precise_constants)
Elliott Hughes80537bb2013-01-04 16:37:26 -0800307 : reg_types_(can_load_classes),
308 work_insn_idx_(-1),
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800309 dex_method_idx_(dex_method_idx),
Ian Rogers637c65b2013-05-31 11:46:00 -0700310 mirror_method_(method),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700311 method_access_flags_(method_access_flags),
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700312 return_type_(nullptr),
jeffhaof56197c2012-03-05 18:01:54 -0800313 dex_file_(dex_file),
314 dex_cache_(dex_cache),
315 class_loader_(class_loader),
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700316 class_def_(class_def),
jeffhaof56197c2012-03-05 18:01:54 -0800317 code_item_(code_item),
Ian Rogers637c65b2013-05-31 11:46:00 -0700318 declaring_class_(NULL),
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700319 interesting_dex_pc_(-1),
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700320 monitor_enter_dex_pcs_(nullptr),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700321 have_pending_hard_failure_(false),
jeffhaofaf459e2012-08-31 15:32:47 -0700322 have_pending_runtime_throw_failure_(false),
jeffhaof56197c2012-03-05 18:01:54 -0800323 new_instance_count_(0),
Elliott Hughes80537bb2013-01-04 16:37:26 -0800324 monitor_enter_count_(0),
Jeff Haoee988952013-04-16 14:23:47 -0700325 can_load_classes_(can_load_classes),
Sebastien Hertz4d4adb12013-07-24 16:14:19 +0200326 allow_soft_failures_(allow_soft_failures),
Ian Rogers46960fe2014-05-23 10:43:43 -0700327 need_precise_constants_(need_precise_constants),
Sebastien Hertz4d4adb12013-07-24 16:14:19 +0200328 has_check_casts_(false),
329 has_virtual_or_interface_invokes_(false) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800330 Runtime::Current()->AddMethodVerifier(this);
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700331 DCHECK(class_def != nullptr);
jeffhaof56197c2012-03-05 18:01:54 -0800332}
333
Mathieu Chartier590fee92013-09-13 13:46:47 -0700334MethodVerifier::~MethodVerifier() {
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800335 Runtime::Current()->RemoveMethodVerifier(this);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700336 STLDeleteElements(&failure_messages_);
337}
338
Brian Carlstromea46f952013-07-30 01:26:50 -0700339void MethodVerifier::FindLocksAtDexPc(mirror::ArtMethod* m, uint32_t dex_pc,
Ian Rogers46960fe2014-05-23 10:43:43 -0700340 std::vector<uint32_t>* monitor_enter_dex_pcs) {
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700341 MethodHelper mh(m);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700342 StackHandleScope<2> hs(Thread::Current());
343 Handle<mirror::DexCache> dex_cache(hs.NewHandle(mh.GetDexCache()));
344 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(mh.GetClassLoader()));
Mathieu Chartier590fee92013-09-13 13:46:47 -0700345 MethodVerifier verifier(&mh.GetDexFile(), &dex_cache, &class_loader, &mh.GetClassDef(),
346 mh.GetCodeItem(), m->GetDexMethodIndex(), m, m->GetAccessFlags(), false,
Ian Rogers46960fe2014-05-23 10:43:43 -0700347 true, false);
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700348 verifier.interesting_dex_pc_ = dex_pc;
Ian Rogers46960fe2014-05-23 10:43:43 -0700349 verifier.monitor_enter_dex_pcs_ = monitor_enter_dex_pcs;
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700350 verifier.FindLocksAtDexPc();
351}
352
353void MethodVerifier::FindLocksAtDexPc() {
354 CHECK(monitor_enter_dex_pcs_ != NULL);
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700355 CHECK(code_item_ != NULL); // This only makes sense for methods with code.
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700356
357 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
358 // verification. In practice, the phase we want relies on data structures set up by all the
359 // earlier passes, so we just run the full method verification and bail out early when we've
360 // got what we wanted.
361 Verify();
362}
363
Brian Carlstromea46f952013-07-30 01:26:50 -0700364mirror::ArtField* MethodVerifier::FindAccessedFieldAtDexPc(mirror::ArtMethod* m,
Ian Rogers46960fe2014-05-23 10:43:43 -0700365 uint32_t dex_pc) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200366 MethodHelper mh(m);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700367 StackHandleScope<2> hs(Thread::Current());
368 Handle<mirror::DexCache> dex_cache(hs.NewHandle(mh.GetDexCache()));
369 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(mh.GetClassLoader()));
Mathieu Chartier590fee92013-09-13 13:46:47 -0700370 MethodVerifier verifier(&mh.GetDexFile(), &dex_cache, &class_loader, &mh.GetClassDef(),
Ian Rogers9bc54402014-04-17 16:40:01 -0700371 mh.GetCodeItem(), m->GetDexMethodIndex(), m, m->GetAccessFlags(), true,
Ian Rogers46960fe2014-05-23 10:43:43 -0700372 true, false);
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200373 return verifier.FindAccessedFieldAtDexPc(dex_pc);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200374}
375
Brian Carlstromea46f952013-07-30 01:26:50 -0700376mirror::ArtField* MethodVerifier::FindAccessedFieldAtDexPc(uint32_t dex_pc) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700377 CHECK(code_item_ != NULL); // This only makes sense for methods with code.
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200378
379 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
380 // verification. In practice, the phase we want relies on data structures set up by all the
381 // earlier passes, so we just run the full method verification and bail out early when we've
382 // got what we wanted.
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200383 bool success = Verify();
384 if (!success) {
Ian Rogers9bc54402014-04-17 16:40:01 -0700385 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200386 }
387 RegisterLine* register_line = reg_table_.GetLine(dex_pc);
388 if (register_line == NULL) {
Ian Rogers9bc54402014-04-17 16:40:01 -0700389 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200390 }
391 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
392 return GetQuickFieldAccess(inst, register_line);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200393}
394
Brian Carlstromea46f952013-07-30 01:26:50 -0700395mirror::ArtMethod* MethodVerifier::FindInvokedMethodAtDexPc(mirror::ArtMethod* m,
Mathieu Chartier590fee92013-09-13 13:46:47 -0700396 uint32_t dex_pc) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200397 MethodHelper mh(m);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700398 StackHandleScope<2> hs(Thread::Current());
399 Handle<mirror::DexCache> dex_cache(hs.NewHandle(mh.GetDexCache()));
400 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(mh.GetClassLoader()));
Mathieu Chartier590fee92013-09-13 13:46:47 -0700401 MethodVerifier verifier(&mh.GetDexFile(), &dex_cache, &class_loader, &mh.GetClassDef(),
Andreas Gampe63981562014-04-17 12:28:43 -0700402 mh.GetCodeItem(), m->GetDexMethodIndex(), m, m->GetAccessFlags(), true,
Ian Rogers46960fe2014-05-23 10:43:43 -0700403 true, false);
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200404 return verifier.FindInvokedMethodAtDexPc(dex_pc);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200405}
406
Brian Carlstromea46f952013-07-30 01:26:50 -0700407mirror::ArtMethod* MethodVerifier::FindInvokedMethodAtDexPc(uint32_t dex_pc) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700408 CHECK(code_item_ != NULL); // This only makes sense for methods with code.
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200409
410 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
411 // verification. In practice, the phase we want relies on data structures set up by all the
412 // earlier passes, so we just run the full method verification and bail out early when we've
413 // got what we wanted.
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200414 bool success = Verify();
415 if (!success) {
416 return NULL;
417 }
418 RegisterLine* register_line = reg_table_.GetLine(dex_pc);
419 if (register_line == NULL) {
420 return NULL;
421 }
422 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
423 const bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
424 return GetQuickInvokedMethod(inst, register_line, is_range);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200425}
426
Ian Rogersad0b3a32012-04-16 14:50:24 -0700427bool MethodVerifier::Verify() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700428 // If there aren't any instructions, make sure that's expected, then exit successfully.
429 if (code_item_ == NULL) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700430 if ((method_access_flags_ & (kAccNative | kAccAbstract)) == 0) {
jeffhaod5347e02012-03-22 17:25:05 -0700431 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "zero-length code in concrete non-native method";
jeffhaobdb76512011-09-07 11:43:16 -0700432 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -0700433 } else {
434 return true;
jeffhaobdb76512011-09-07 11:43:16 -0700435 }
jeffhaobdb76512011-09-07 11:43:16 -0700436 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700437 // Sanity-check the register counts. ins + locals = registers, so make sure that ins <= registers.
438 if (code_item_->ins_size_ > code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700439 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad register counts (ins=" << code_item_->ins_size_
440 << " regs=" << code_item_->registers_size_;
Ian Rogersd81871c2011-10-03 13:57:23 -0700441 return false;
jeffhaobdb76512011-09-07 11:43:16 -0700442 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700443 // Allocate and initialize an array to hold instruction data.
Ian Rogers7b3ddd22013-02-21 15:19:52 -0800444 insn_flags_.reset(new InstructionFlags[code_item_->insns_size_in_code_units_]());
Ian Rogersd81871c2011-10-03 13:57:23 -0700445 // Run through the instructions and see if the width checks out.
446 bool result = ComputeWidthsAndCountOps();
447 // Flag instructions guarded by a "try" block and check exception handlers.
448 result = result && ScanTryCatchBlocks();
449 // Perform static instruction verification.
450 result = result && VerifyInstructions();
Ian Rogersad0b3a32012-04-16 14:50:24 -0700451 // Perform code-flow analysis and return.
Vladimir Marko2b5eaa22013-12-13 13:59:30 +0000452 result = result && VerifyCodeFlow();
453 // Compute information for compiler.
454 if (result && Runtime::Current()->IsCompiler()) {
455 result = Runtime::Current()->GetCompilerCallbacks()->MethodVerified(this);
456 }
457 return result;
jeffhaoba5ebb92011-08-25 17:24:37 -0700458}
459
Ian Rogers776ac1f2012-04-13 23:36:36 -0700460std::ostream& MethodVerifier::Fail(VerifyError error) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700461 switch (error) {
462 case VERIFY_ERROR_NO_CLASS:
463 case VERIFY_ERROR_NO_FIELD:
464 case VERIFY_ERROR_NO_METHOD:
465 case VERIFY_ERROR_ACCESS_CLASS:
466 case VERIFY_ERROR_ACCESS_FIELD:
467 case VERIFY_ERROR_ACCESS_METHOD:
Ian Rogers08f753d2012-08-24 14:35:25 -0700468 case VERIFY_ERROR_INSTANTIATION:
469 case VERIFY_ERROR_CLASS_CHANGE:
Elliott Hughes4993bbc2013-01-10 15:41:25 -0800470 if (Runtime::Current()->IsCompiler() || !can_load_classes_) {
jeffhaofaf459e2012-08-31 15:32:47 -0700471 // If we're optimistically running verification at compile time, turn NO_xxx, ACCESS_xxx,
472 // class change and instantiation errors into soft verification errors so that we re-verify
473 // at runtime. We may fail to find or to agree on access because of not yet available class
474 // loaders, or class loaders that will differ at runtime. In these cases, we don't want to
475 // affect the soundness of the code being compiled. Instead, the generated code runs "slow
476 // paths" that dynamically perform the verification and cause the behavior to be that akin
477 // to an interpreter.
478 error = VERIFY_ERROR_BAD_CLASS_SOFT;
479 } else {
Jeff Haoa3faaf42013-09-03 19:07:00 -0700480 // If we fail again at runtime, mark that this instruction would throw and force this
481 // method to be executed using the interpreter with checks.
jeffhaofaf459e2012-08-31 15:32:47 -0700482 have_pending_runtime_throw_failure_ = true;
483 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700484 break;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700485 // Indication that verification should be retried at runtime.
486 case VERIFY_ERROR_BAD_CLASS_SOFT:
Jeff Haoee988952013-04-16 14:23:47 -0700487 if (!allow_soft_failures_) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700488 have_pending_hard_failure_ = true;
489 }
490 break;
jeffhaod5347e02012-03-22 17:25:05 -0700491 // Hard verification failures at compile time will still fail at runtime, so the class is
492 // marked as rejected to prevent it from being compiled.
Ian Rogersad0b3a32012-04-16 14:50:24 -0700493 case VERIFY_ERROR_BAD_CLASS_HARD: {
494 if (Runtime::Current()->IsCompiler()) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700495 ClassReference ref(dex_file_, dex_file_->GetIndexForClassDef(*class_def_));
Vladimir Marko2b5eaa22013-12-13 13:59:30 +0000496 Runtime::Current()->GetCompilerCallbacks()->ClassRejected(ref);
jeffhaod1224c72012-02-29 13:43:08 -0800497 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700498 have_pending_hard_failure_ = true;
499 break;
Ian Rogers47a05882012-02-03 12:23:33 -0800500 }
501 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700502 failures_.push_back(error);
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800503 std::string location(StringPrintf("%s: [0x%X]", PrettyMethod(dex_method_idx_, *dex_file_).c_str(),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700504 work_insn_idx_));
505 std::ostringstream* failure_message = new std::ostringstream(location);
506 failure_messages_.push_back(failure_message);
507 return *failure_message;
508}
509
510void MethodVerifier::PrependToLastFailMessage(std::string prepend) {
511 size_t failure_num = failure_messages_.size();
512 DCHECK_NE(failure_num, 0U);
513 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
514 prepend += last_fail_message->str();
515 failure_messages_[failure_num - 1] = new std::ostringstream(prepend);
516 delete last_fail_message;
517}
518
519void MethodVerifier::AppendToLastFailMessage(std::string append) {
520 size_t failure_num = failure_messages_.size();
521 DCHECK_NE(failure_num, 0U);
522 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
523 (*last_fail_message) << append;
Ian Rogers47a05882012-02-03 12:23:33 -0800524}
525
Ian Rogers776ac1f2012-04-13 23:36:36 -0700526bool MethodVerifier::ComputeWidthsAndCountOps() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700527 const uint16_t* insns = code_item_->insns_;
528 size_t insns_size = code_item_->insns_size_in_code_units_;
529 const Instruction* inst = Instruction::At(insns);
jeffhaobdb76512011-09-07 11:43:16 -0700530 size_t new_instance_count = 0;
531 size_t monitor_enter_count = 0;
Ian Rogersd81871c2011-10-03 13:57:23 -0700532 size_t dex_pc = 0;
jeffhaobdb76512011-09-07 11:43:16 -0700533
Ian Rogersd81871c2011-10-03 13:57:23 -0700534 while (dex_pc < insns_size) {
jeffhaobdb76512011-09-07 11:43:16 -0700535 Instruction::Code opcode = inst->Opcode();
Ian Rogersa9a82542013-10-04 11:17:26 -0700536 switch (opcode) {
537 case Instruction::APUT_OBJECT:
538 case Instruction::CHECK_CAST:
539 has_check_casts_ = true;
540 break;
541 case Instruction::INVOKE_VIRTUAL:
542 case Instruction::INVOKE_VIRTUAL_RANGE:
543 case Instruction::INVOKE_INTERFACE:
544 case Instruction::INVOKE_INTERFACE_RANGE:
545 has_virtual_or_interface_invokes_ = true;
546 break;
547 case Instruction::MONITOR_ENTER:
548 monitor_enter_count++;
549 break;
550 case Instruction::NEW_INSTANCE:
551 new_instance_count++;
552 break;
553 default:
554 break;
jeffhaobdb76512011-09-07 11:43:16 -0700555 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700556 size_t inst_size = inst->SizeInCodeUnits();
557 insn_flags_[dex_pc].SetLengthInCodeUnits(inst_size);
558 dex_pc += inst_size;
jeffhaobdb76512011-09-07 11:43:16 -0700559 inst = inst->Next();
560 }
561
Ian Rogersd81871c2011-10-03 13:57:23 -0700562 if (dex_pc != insns_size) {
jeffhaod5347e02012-03-22 17:25:05 -0700563 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "code did not end where expected ("
564 << dex_pc << " vs. " << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700565 return false;
566 }
567
Ian Rogersd81871c2011-10-03 13:57:23 -0700568 new_instance_count_ = new_instance_count;
569 monitor_enter_count_ = monitor_enter_count;
jeffhaobdb76512011-09-07 11:43:16 -0700570 return true;
571}
572
Ian Rogers776ac1f2012-04-13 23:36:36 -0700573bool MethodVerifier::ScanTryCatchBlocks() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700574 uint32_t tries_size = code_item_->tries_size_;
jeffhaobdb76512011-09-07 11:43:16 -0700575 if (tries_size == 0) {
576 return true;
577 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700578 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Ian Rogers0571d352011-11-03 19:51:38 -0700579 const DexFile::TryItem* tries = DexFile::GetTryItems(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -0700580
581 for (uint32_t idx = 0; idx < tries_size; idx++) {
582 const DexFile::TryItem* try_item = &tries[idx];
583 uint32_t start = try_item->start_addr_;
584 uint32_t end = start + try_item->insn_count_;
jeffhaobdb76512011-09-07 11:43:16 -0700585 if ((start >= end) || (start >= insns_size) || (end > insns_size)) {
jeffhaod5347e02012-03-22 17:25:05 -0700586 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad exception entry: startAddr=" << start
587 << " endAddr=" << end << " (size=" << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700588 return false;
589 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700590 if (!insn_flags_[start].IsOpcode()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700591 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
592 << "'try' block starts inside an instruction (" << start << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700593 return false;
594 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700595 for (uint32_t dex_pc = start; dex_pc < end;
596 dex_pc += insn_flags_[dex_pc].GetLengthInCodeUnits()) {
597 insn_flags_[dex_pc].SetInTry();
jeffhaobdb76512011-09-07 11:43:16 -0700598 }
599 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800600 // Iterate over each of the handlers to verify target addresses.
Ian Rogers0571d352011-11-03 19:51:38 -0700601 const byte* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -0700602 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700603 ClassLinker* linker = Runtime::Current()->GetClassLinker();
jeffhaobdb76512011-09-07 11:43:16 -0700604 for (uint32_t idx = 0; idx < handlers_size; idx++) {
Ian Rogers0571d352011-11-03 19:51:38 -0700605 CatchHandlerIterator iterator(handlers_ptr);
606 for (; iterator.HasNext(); iterator.Next()) {
607 uint32_t dex_pc= iterator.GetHandlerAddress();
Ian Rogersd81871c2011-10-03 13:57:23 -0700608 if (!insn_flags_[dex_pc].IsOpcode()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700609 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
610 << "exception handler starts at bad address (" << dex_pc << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700611 return false;
612 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700613 insn_flags_[dex_pc].SetBranchTarget();
Ian Rogers28ad40d2011-10-27 15:19:26 -0700614 // Ensure exception types are resolved so that they don't need resolution to be delivered,
615 // unresolved exception types will be ignored by exception delivery
Ian Rogers0571d352011-11-03 19:51:38 -0700616 if (iterator.GetHandlerTypeIndex() != DexFile::kDexNoIndex16) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800617 mirror::Class* exception_type = linker->ResolveType(*dex_file_,
618 iterator.GetHandlerTypeIndex(),
Mathieu Chartier590fee92013-09-13 13:46:47 -0700619 *dex_cache_, *class_loader_);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700620 if (exception_type == NULL) {
621 DCHECK(Thread::Current()->IsExceptionPending());
622 Thread::Current()->ClearException();
623 }
624 }
jeffhaobdb76512011-09-07 11:43:16 -0700625 }
Ian Rogers0571d352011-11-03 19:51:38 -0700626 handlers_ptr = iterator.EndDataPointer();
jeffhaobdb76512011-09-07 11:43:16 -0700627 }
jeffhaobdb76512011-09-07 11:43:16 -0700628 return true;
629}
630
Ian Rogers776ac1f2012-04-13 23:36:36 -0700631bool MethodVerifier::VerifyInstructions() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700632 const Instruction* inst = Instruction::At(code_item_->insns_);
jeffhaoba5ebb92011-08-25 17:24:37 -0700633
Ian Rogers0c7abda2012-09-19 13:33:42 -0700634 /* 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 -0700635 insn_flags_[0].SetBranchTarget();
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700636 insn_flags_[0].SetCompileTimeInfoPoint();
Ian Rogersd81871c2011-10-03 13:57:23 -0700637
638 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700639 for (uint32_t dex_pc = 0; dex_pc < insns_size;) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700640 if (!VerifyInstruction(inst, dex_pc)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700641 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -0700642 return false;
643 }
644 /* Flag instructions that are garbage collection points */
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700645 // All invoke points are marked as "Throw" points already.
646 // We are relying on this to also count all the invokes as interesting.
Ian Rogersb8c78592013-07-25 23:52:52 +0000647 if (inst->IsBranch() || inst->IsSwitch() || inst->IsThrow()) {
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700648 insn_flags_[dex_pc].SetCompileTimeInfoPoint();
Ian Rogersb8c78592013-07-25 23:52:52 +0000649 } else if (inst->IsReturn()) {
650 insn_flags_[dex_pc].SetCompileTimeInfoPointAndReturn();
Ian Rogersd81871c2011-10-03 13:57:23 -0700651 }
652 dex_pc += inst->SizeInCodeUnits();
653 inst = inst->Next();
654 }
655 return true;
656}
657
Ian Rogers776ac1f2012-04-13 23:36:36 -0700658bool MethodVerifier::VerifyInstruction(const Instruction* inst, uint32_t code_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700659 bool result = true;
660 switch (inst->GetVerifyTypeArgumentA()) {
661 case Instruction::kVerifyRegA:
Ian Rogers29a26482014-05-02 15:27:29 -0700662 result = result && CheckRegisterIndex(inst->VRegA());
Ian Rogersd81871c2011-10-03 13:57:23 -0700663 break;
664 case Instruction::kVerifyRegAWide:
Ian Rogers29a26482014-05-02 15:27:29 -0700665 result = result && CheckWideRegisterIndex(inst->VRegA());
Ian Rogersd81871c2011-10-03 13:57:23 -0700666 break;
667 }
668 switch (inst->GetVerifyTypeArgumentB()) {
669 case Instruction::kVerifyRegB:
Ian Rogers29a26482014-05-02 15:27:29 -0700670 result = result && CheckRegisterIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700671 break;
672 case Instruction::kVerifyRegBField:
Ian Rogers29a26482014-05-02 15:27:29 -0700673 result = result && CheckFieldIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700674 break;
675 case Instruction::kVerifyRegBMethod:
Ian Rogers29a26482014-05-02 15:27:29 -0700676 result = result && CheckMethodIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700677 break;
678 case Instruction::kVerifyRegBNewInstance:
Ian Rogers29a26482014-05-02 15:27:29 -0700679 result = result && CheckNewInstance(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700680 break;
681 case Instruction::kVerifyRegBString:
Ian Rogers29a26482014-05-02 15:27:29 -0700682 result = result && CheckStringIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700683 break;
684 case Instruction::kVerifyRegBType:
Ian Rogers29a26482014-05-02 15:27:29 -0700685 result = result && CheckTypeIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700686 break;
687 case Instruction::kVerifyRegBWide:
Ian Rogers29a26482014-05-02 15:27:29 -0700688 result = result && CheckWideRegisterIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700689 break;
690 }
691 switch (inst->GetVerifyTypeArgumentC()) {
692 case Instruction::kVerifyRegC:
Ian Rogers29a26482014-05-02 15:27:29 -0700693 result = result && CheckRegisterIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700694 break;
695 case Instruction::kVerifyRegCField:
Ian Rogers29a26482014-05-02 15:27:29 -0700696 result = result && CheckFieldIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700697 break;
698 case Instruction::kVerifyRegCNewArray:
Ian Rogers29a26482014-05-02 15:27:29 -0700699 result = result && CheckNewArray(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700700 break;
701 case Instruction::kVerifyRegCType:
Ian Rogers29a26482014-05-02 15:27:29 -0700702 result = result && CheckTypeIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700703 break;
704 case Instruction::kVerifyRegCWide:
Ian Rogers29a26482014-05-02 15:27:29 -0700705 result = result && CheckWideRegisterIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700706 break;
707 }
708 switch (inst->GetVerifyExtraFlags()) {
709 case Instruction::kVerifyArrayData:
710 result = result && CheckArrayData(code_offset);
711 break;
712 case Instruction::kVerifyBranchTarget:
713 result = result && CheckBranchTarget(code_offset);
714 break;
715 case Instruction::kVerifySwitchTargets:
716 result = result && CheckSwitchTargets(code_offset);
717 break;
Ian Rogers29a26482014-05-02 15:27:29 -0700718 case Instruction::kVerifyVarArg: {
719 uint32_t args[Instruction::kMaxVarArgRegs];
720 inst->GetVarArgs(args);
721 result = result && CheckVarArgRegs(inst->VRegA(), args);
Ian Rogersd81871c2011-10-03 13:57:23 -0700722 break;
Ian Rogers29a26482014-05-02 15:27:29 -0700723 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700724 case Instruction::kVerifyVarArgRange:
Ian Rogers29a26482014-05-02 15:27:29 -0700725 result = result && CheckVarArgRangeRegs(inst->VRegA(), inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700726 break;
727 case Instruction::kVerifyError:
jeffhaod5347e02012-03-22 17:25:05 -0700728 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected opcode " << inst->Name();
Ian Rogersd81871c2011-10-03 13:57:23 -0700729 result = false;
730 break;
731 }
732 return result;
733}
734
Ian Rogers776ac1f2012-04-13 23:36:36 -0700735bool MethodVerifier::CheckRegisterIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700736 if (idx >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700737 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register index out of range (" << idx << " >= "
738 << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700739 return false;
740 }
741 return true;
742}
743
Ian Rogers776ac1f2012-04-13 23:36:36 -0700744bool MethodVerifier::CheckWideRegisterIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700745 if (idx + 1 >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700746 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "wide register index out of range (" << idx
747 << "+1 >= " << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700748 return false;
749 }
750 return true;
751}
752
Ian Rogers776ac1f2012-04-13 23:36:36 -0700753bool MethodVerifier::CheckFieldIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700754 if (idx >= dex_file_->GetHeader().field_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700755 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad field index " << idx << " (max "
756 << dex_file_->GetHeader().field_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700757 return false;
758 }
759 return true;
760}
761
Ian Rogers776ac1f2012-04-13 23:36:36 -0700762bool MethodVerifier::CheckMethodIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700763 if (idx >= dex_file_->GetHeader().method_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700764 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad method index " << idx << " (max "
765 << dex_file_->GetHeader().method_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700766 return false;
767 }
768 return true;
769}
770
Ian Rogers776ac1f2012-04-13 23:36:36 -0700771bool MethodVerifier::CheckNewInstance(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700772 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700773 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
774 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700775 return false;
776 }
777 // We don't need the actual class, just a pointer to the class name.
Ian Rogers0571d352011-11-03 19:51:38 -0700778 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -0700779 if (descriptor[0] != 'L') {
jeffhaod5347e02012-03-22 17:25:05 -0700780 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "can't call new-instance on type '" << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -0700781 return false;
782 }
783 return true;
784}
785
Ian Rogers776ac1f2012-04-13 23:36:36 -0700786bool MethodVerifier::CheckStringIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700787 if (idx >= dex_file_->GetHeader().string_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700788 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad string index " << idx << " (max "
789 << dex_file_->GetHeader().string_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700790 return false;
791 }
792 return true;
793}
794
Ian Rogers776ac1f2012-04-13 23:36:36 -0700795bool MethodVerifier::CheckTypeIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700796 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700797 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
798 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700799 return false;
800 }
801 return true;
802}
803
Ian Rogers776ac1f2012-04-13 23:36:36 -0700804bool MethodVerifier::CheckNewArray(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700805 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700806 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
807 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700808 return false;
809 }
810 int bracket_count = 0;
Ian Rogers0571d352011-11-03 19:51:38 -0700811 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -0700812 const char* cp = descriptor;
813 while (*cp++ == '[') {
814 bracket_count++;
815 }
816 if (bracket_count == 0) {
817 /* The given class must be an array type. */
Brian Carlstrom93c33962013-07-26 10:37:43 -0700818 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
819 << "can't new-array class '" << descriptor << "' (not an array)";
Ian Rogersd81871c2011-10-03 13:57:23 -0700820 return false;
821 } else if (bracket_count > 255) {
822 /* It is illegal to create an array of more than 255 dimensions. */
Brian Carlstrom93c33962013-07-26 10:37:43 -0700823 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
824 << "can't new-array class '" << descriptor << "' (exceeds limit)";
Ian Rogersd81871c2011-10-03 13:57:23 -0700825 return false;
826 }
827 return true;
828}
829
Ian Rogers776ac1f2012-04-13 23:36:36 -0700830bool MethodVerifier::CheckArrayData(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700831 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
832 const uint16_t* insns = code_item_->insns_ + cur_offset;
833 const uint16_t* array_data;
834 int32_t array_data_offset;
835
836 DCHECK_LT(cur_offset, insn_count);
837 /* make sure the start of the array data table is in range */
838 array_data_offset = insns[1] | (((int32_t) insns[2]) << 16);
839 if ((int32_t) cur_offset + array_data_offset < 0 ||
840 cur_offset + array_data_offset + 2 >= insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -0700841 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data start: at " << cur_offset
Brian Carlstrom93c33962013-07-26 10:37:43 -0700842 << ", data offset " << array_data_offset
843 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -0700844 return false;
845 }
846 /* offset to array data table is a relative branch-style offset */
847 array_data = insns + array_data_offset;
848 /* make sure the table is 32-bit aligned */
Ian Rogersef7d42f2014-01-06 12:55:46 -0800849 if ((reinterpret_cast<uintptr_t>(array_data) & 0x03) != 0) {
jeffhaod5347e02012-03-22 17:25:05 -0700850 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned array data table: at " << cur_offset
851 << ", data offset " << array_data_offset;
Ian Rogersd81871c2011-10-03 13:57:23 -0700852 return false;
853 }
854 uint32_t value_width = array_data[1];
Elliott Hughes398f64b2012-03-26 18:05:48 -0700855 uint32_t value_count = *reinterpret_cast<const uint32_t*>(&array_data[2]);
Ian Rogersd81871c2011-10-03 13:57:23 -0700856 uint32_t table_size = 4 + (value_width * value_count + 1) / 2;
857 /* make sure the end of the switch is in range */
858 if (cur_offset + array_data_offset + table_size > insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -0700859 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data end: at " << cur_offset
860 << ", data offset " << array_data_offset << ", end "
861 << cur_offset + array_data_offset + table_size
862 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -0700863 return false;
864 }
865 return true;
866}
867
Ian Rogers776ac1f2012-04-13 23:36:36 -0700868bool MethodVerifier::CheckBranchTarget(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700869 int32_t offset;
870 bool isConditional, selfOkay;
871 if (!GetBranchOffset(cur_offset, &offset, &isConditional, &selfOkay)) {
872 return false;
873 }
874 if (!selfOkay && offset == 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700875 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch offset of zero not allowed at"
876 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -0700877 return false;
878 }
Elliott Hughes81ff3182012-03-23 20:35:56 -0700879 // Check for 32-bit overflow. This isn't strictly necessary if we can depend on the runtime
880 // to have identical "wrap-around" behavior, but it's unwise to depend on that.
Ian Rogersd81871c2011-10-03 13:57:23 -0700881 if (((int64_t) cur_offset + (int64_t) offset) != (int64_t) (cur_offset + offset)) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700882 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch target overflow "
883 << reinterpret_cast<void*>(cur_offset) << " +" << offset;
Ian Rogersd81871c2011-10-03 13:57:23 -0700884 return false;
885 }
886 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
887 int32_t abs_offset = cur_offset + offset;
Brian Carlstrom93c33962013-07-26 10:37:43 -0700888 if (abs_offset < 0 ||
889 (uint32_t) abs_offset >= insn_count ||
890 !insn_flags_[abs_offset].IsOpcode()) {
jeffhaod5347e02012-03-22 17:25:05 -0700891 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid branch target " << offset << " (-> "
Elliott Hughes398f64b2012-03-26 18:05:48 -0700892 << reinterpret_cast<void*>(abs_offset) << ") at "
893 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -0700894 return false;
895 }
896 insn_flags_[abs_offset].SetBranchTarget();
897 return true;
898}
899
Ian Rogers776ac1f2012-04-13 23:36:36 -0700900bool MethodVerifier::GetBranchOffset(uint32_t cur_offset, int32_t* pOffset, bool* pConditional,
Ian Rogersd81871c2011-10-03 13:57:23 -0700901 bool* selfOkay) {
902 const uint16_t* insns = code_item_->insns_ + cur_offset;
903 *pConditional = false;
904 *selfOkay = false;
jeffhaoba5ebb92011-08-25 17:24:37 -0700905 switch (*insns & 0xff) {
906 case Instruction::GOTO:
907 *pOffset = ((int16_t) *insns) >> 8;
jeffhaoba5ebb92011-08-25 17:24:37 -0700908 break;
909 case Instruction::GOTO_32:
910 *pOffset = insns[1] | (((uint32_t) insns[2]) << 16);
jeffhaoba5ebb92011-08-25 17:24:37 -0700911 *selfOkay = true;
912 break;
913 case Instruction::GOTO_16:
914 *pOffset = (int16_t) insns[1];
jeffhaoba5ebb92011-08-25 17:24:37 -0700915 break;
916 case Instruction::IF_EQ:
917 case Instruction::IF_NE:
918 case Instruction::IF_LT:
919 case Instruction::IF_GE:
920 case Instruction::IF_GT:
921 case Instruction::IF_LE:
922 case Instruction::IF_EQZ:
923 case Instruction::IF_NEZ:
924 case Instruction::IF_LTZ:
925 case Instruction::IF_GEZ:
926 case Instruction::IF_GTZ:
927 case Instruction::IF_LEZ:
928 *pOffset = (int16_t) insns[1];
929 *pConditional = true;
jeffhaoba5ebb92011-08-25 17:24:37 -0700930 break;
931 default:
932 return false;
933 break;
934 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700935 return true;
936}
937
Ian Rogers776ac1f2012-04-13 23:36:36 -0700938bool MethodVerifier::CheckSwitchTargets(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700939 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700940 DCHECK_LT(cur_offset, insn_count);
Ian Rogersd81871c2011-10-03 13:57:23 -0700941 const uint16_t* insns = code_item_->insns_ + cur_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -0700942 /* make sure the start of the switch is in range */
Ian Rogersd81871c2011-10-03 13:57:23 -0700943 int32_t switch_offset = insns[1] | ((int32_t) insns[2]) << 16;
944 if ((int32_t) cur_offset + switch_offset < 0 || cur_offset + switch_offset + 2 >= insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -0700945 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch start: at " << cur_offset
Brian Carlstrom93c33962013-07-26 10:37:43 -0700946 << ", switch offset " << switch_offset
947 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -0700948 return false;
949 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700950 /* offset to switch table is a relative branch-style offset */
Ian Rogersd81871c2011-10-03 13:57:23 -0700951 const uint16_t* switch_insns = insns + switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -0700952 /* make sure the table is 32-bit aligned */
Ian Rogersef7d42f2014-01-06 12:55:46 -0800953 if ((reinterpret_cast<uintptr_t>(switch_insns) & 0x03) != 0) {
jeffhaod5347e02012-03-22 17:25:05 -0700954 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned switch table: at " << cur_offset
955 << ", switch offset " << switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -0700956 return false;
957 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700958 uint32_t switch_count = switch_insns[1];
959 int32_t keys_offset, targets_offset;
960 uint16_t expected_signature;
jeffhaoba5ebb92011-08-25 17:24:37 -0700961 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
962 /* 0=sig, 1=count, 2/3=firstKey */
963 targets_offset = 4;
964 keys_offset = -1;
965 expected_signature = Instruction::kPackedSwitchSignature;
966 } else {
967 /* 0=sig, 1=count, 2..count*2 = keys */
968 keys_offset = 2;
969 targets_offset = 2 + 2 * switch_count;
970 expected_signature = Instruction::kSparseSwitchSignature;
971 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700972 uint32_t table_size = targets_offset + switch_count * 2;
jeffhaoba5ebb92011-08-25 17:24:37 -0700973 if (switch_insns[0] != expected_signature) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700974 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
975 << StringPrintf("wrong signature for switch table (%x, wanted %x)",
976 switch_insns[0], expected_signature);
jeffhaoba5ebb92011-08-25 17:24:37 -0700977 return false;
978 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700979 /* make sure the end of the switch is in range */
980 if (cur_offset + switch_offset + table_size > (uint32_t) insn_count) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700981 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch end: at " << cur_offset
982 << ", switch offset " << switch_offset
983 << ", end " << (cur_offset + switch_offset + table_size)
jeffhaod5347e02012-03-22 17:25:05 -0700984 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -0700985 return false;
986 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700987 /* for a sparse switch, verify the keys are in ascending order */
988 if (keys_offset > 0 && switch_count > 1) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700989 int32_t last_key = switch_insns[keys_offset] | (switch_insns[keys_offset + 1] << 16);
990 for (uint32_t targ = 1; targ < switch_count; targ++) {
jeffhaoba5ebb92011-08-25 17:24:37 -0700991 int32_t key = (int32_t) switch_insns[keys_offset + targ * 2] |
992 (int32_t) (switch_insns[keys_offset + targ * 2 + 1] << 16);
993 if (key <= last_key) {
jeffhaod5347e02012-03-22 17:25:05 -0700994 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid packed switch: last key=" << last_key
995 << ", this=" << key;
jeffhaoba5ebb92011-08-25 17:24:37 -0700996 return false;
997 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700998 last_key = key;
999 }
1000 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001001 /* verify each switch target */
Ian Rogersd81871c2011-10-03 13:57:23 -07001002 for (uint32_t targ = 0; targ < switch_count; targ++) {
1003 int32_t offset = (int32_t) switch_insns[targets_offset + targ * 2] |
1004 (int32_t) (switch_insns[targets_offset + targ * 2 + 1] << 16);
1005 int32_t abs_offset = cur_offset + offset;
Brian Carlstrom93c33962013-07-26 10:37:43 -07001006 if (abs_offset < 0 ||
1007 abs_offset >= (int32_t) insn_count ||
1008 !insn_flags_[abs_offset].IsOpcode()) {
1009 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch target " << offset
1010 << " (-> " << reinterpret_cast<void*>(abs_offset) << ") at "
1011 << reinterpret_cast<void*>(cur_offset)
1012 << "[" << targ << "]";
jeffhaoba5ebb92011-08-25 17:24:37 -07001013 return false;
1014 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001015 insn_flags_[abs_offset].SetBranchTarget();
1016 }
1017 return true;
1018}
1019
Ian Rogers776ac1f2012-04-13 23:36:36 -07001020bool MethodVerifier::CheckVarArgRegs(uint32_t vA, uint32_t arg[]) {
Ian Rogers29a26482014-05-02 15:27:29 -07001021 if (vA > Instruction::kMaxVarArgRegs) {
jeffhaod5347e02012-03-22 17:25:05 -07001022 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << vA << ") in non-range invoke)";
Ian Rogersd81871c2011-10-03 13:57:23 -07001023 return false;
1024 }
1025 uint16_t registers_size = code_item_->registers_size_;
1026 for (uint32_t idx = 0; idx < vA; idx++) {
jeffhao457cc512012-02-02 16:55:13 -08001027 if (arg[idx] >= registers_size) {
jeffhaod5347e02012-03-22 17:25:05 -07001028 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index (" << arg[idx]
1029 << ") in non-range invoke (>= " << registers_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001030 return false;
1031 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001032 }
1033
1034 return true;
1035}
1036
Ian Rogers776ac1f2012-04-13 23:36:36 -07001037bool MethodVerifier::CheckVarArgRangeRegs(uint32_t vA, uint32_t vC) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001038 uint16_t registers_size = code_item_->registers_size_;
1039 // vA/vC are unsigned 8-bit/16-bit quantities for /range instructions, so there's no risk of
1040 // integer overflow when adding them here.
1041 if (vA + vC > registers_size) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001042 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index " << vA << "+" << vC
1043 << " in range invoke (> " << registers_size << ")";
jeffhaoba5ebb92011-08-25 17:24:37 -07001044 return false;
1045 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001046 return true;
1047}
1048
Ian Rogers776ac1f2012-04-13 23:36:36 -07001049bool MethodVerifier::VerifyCodeFlow() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001050 uint16_t registers_size = code_item_->registers_size_;
1051 uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaobdb76512011-09-07 11:43:16 -07001052
Ian Rogersd81871c2011-10-03 13:57:23 -07001053 if (registers_size * insns_size > 4*1024*1024) {
buzbee4922ef92012-02-24 14:32:20 -08001054 LOG(WARNING) << "warning: method is huge (regs=" << registers_size
1055 << " insns_size=" << insns_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001056 }
1057 /* Create and initialize table holding register status */
Brian Carlstrom93c33962013-07-26 10:37:43 -07001058 reg_table_.Init(kTrackCompilerInterestPoints,
1059 insn_flags_.get(),
1060 insns_size,
1061 registers_size,
1062 this);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07001063
jeffhaobdb76512011-09-07 11:43:16 -07001064
Ian Rogersd0fbd852013-09-24 18:17:04 -07001065 work_line_.reset(RegisterLine::Create(registers_size, this));
1066 saved_line_.reset(RegisterLine::Create(registers_size, this));
jeffhaobdb76512011-09-07 11:43:16 -07001067
Ian Rogersd81871c2011-10-03 13:57:23 -07001068 /* Initialize register types of method arguments. */
1069 if (!SetTypesFromSignature()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001070 DCHECK_NE(failures_.size(), 0U);
1071 std::string prepend("Bad signature in ");
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001072 prepend += PrettyMethod(dex_method_idx_, *dex_file_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001073 PrependToLastFailMessage(prepend);
Ian Rogersd81871c2011-10-03 13:57:23 -07001074 return false;
1075 }
1076 /* Perform code flow verification. */
1077 if (!CodeFlowVerifyMethod()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001078 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -07001079 return false;
jeffhaobdb76512011-09-07 11:43:16 -07001080 }
jeffhaobdb76512011-09-07 11:43:16 -07001081 return true;
1082}
1083
Ian Rogersad0b3a32012-04-16 14:50:24 -07001084std::ostream& MethodVerifier::DumpFailures(std::ostream& os) {
1085 DCHECK_EQ(failures_.size(), failure_messages_.size());
Jeff Hao4137f482013-11-22 11:44:57 -08001086 for (size_t i = 0; i < failures_.size(); ++i) {
1087 os << failure_messages_[i]->str() << "\n";
Ian Rogersad0b3a32012-04-16 14:50:24 -07001088 }
1089 return os;
1090}
1091
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001092extern "C" void MethodVerifierGdbDump(MethodVerifier* v)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001093 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001094 v->Dump(std::cerr);
1095}
1096
Ian Rogers776ac1f2012-04-13 23:36:36 -07001097void MethodVerifier::Dump(std::ostream& os) {
jeffhaof56197c2012-03-05 18:01:54 -08001098 if (code_item_ == NULL) {
Elliott Hughesc073b072012-05-24 19:29:17 -07001099 os << "Native method\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001100 return;
jeffhaobdb76512011-09-07 11:43:16 -07001101 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001102 {
1103 os << "Register Types:\n";
1104 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
1105 std::ostream indent_os(&indent_filter);
1106 reg_types_.Dump(indent_os);
1107 }
Ian Rogersb4903572012-10-11 11:52:56 -07001108 os << "Dumping instructions and register lines:\n";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001109 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
1110 std::ostream indent_os(&indent_filter);
Ian Rogersd81871c2011-10-03 13:57:23 -07001111 const Instruction* inst = Instruction::At(code_item_->insns_);
1112 for (size_t dex_pc = 0; dex_pc < code_item_->insns_size_in_code_units_;
1113 dex_pc += insn_flags_[dex_pc].GetLengthInCodeUnits()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001114 RegisterLine* reg_line = reg_table_.GetLine(dex_pc);
1115 if (reg_line != NULL) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001116 indent_os << reg_line->Dump() << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07001117 }
Ian Rogers7b3ddd22013-02-21 15:19:52 -08001118 indent_os << StringPrintf("0x%04zx", dex_pc) << ": " << insn_flags_[dex_pc].ToString() << " ";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001119 const bool kDumpHexOfInstruction = false;
1120 if (kDumpHexOfInstruction) {
1121 indent_os << inst->DumpHex(5) << " ";
1122 }
1123 indent_os << inst->DumpString(dex_file_) << "\n";
jeffhaoba5ebb92011-08-25 17:24:37 -07001124 inst = inst->Next();
1125 }
jeffhaobdb76512011-09-07 11:43:16 -07001126}
1127
Ian Rogersd81871c2011-10-03 13:57:23 -07001128static bool IsPrimitiveDescriptor(char descriptor) {
1129 switch (descriptor) {
jeffhaobdb76512011-09-07 11:43:16 -07001130 case 'I':
1131 case 'C':
1132 case 'S':
1133 case 'B':
1134 case 'Z':
jeffhaobdb76512011-09-07 11:43:16 -07001135 case 'F':
1136 case 'D':
1137 case 'J':
Ian Rogersd81871c2011-10-03 13:57:23 -07001138 return true;
jeffhaobdb76512011-09-07 11:43:16 -07001139 default:
1140 return false;
1141 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001142}
1143
Ian Rogers776ac1f2012-04-13 23:36:36 -07001144bool MethodVerifier::SetTypesFromSignature() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001145 RegisterLine* reg_line = reg_table_.GetLine(0);
1146 int arg_start = code_item_->registers_size_ - code_item_->ins_size_;
1147 size_t expected_args = code_item_->ins_size_; /* long/double count as two */
jeffhaobdb76512011-09-07 11:43:16 -07001148
Ian Rogersd81871c2011-10-03 13:57:23 -07001149 DCHECK_GE(arg_start, 0); /* should have been verified earlier */
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001150 // Include the "this" pointer.
Ian Rogersd81871c2011-10-03 13:57:23 -07001151 size_t cur_arg = 0;
Ian Rogersad0b3a32012-04-16 14:50:24 -07001152 if (!IsStatic()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001153 // If this is a constructor for a class other than java.lang.Object, mark the first ("this")
1154 // argument as uninitialized. This restricts field access until the superclass constructor is
1155 // called.
Ian Rogersad0b3a32012-04-16 14:50:24 -07001156 const RegType& declaring_class = GetDeclaringClass();
1157 if (IsConstructor() && !declaring_class.IsJavaLangObject()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001158 reg_line->SetRegisterType(arg_start + cur_arg,
1159 reg_types_.UninitializedThisArgument(declaring_class));
1160 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001161 reg_line->SetRegisterType(arg_start + cur_arg, declaring_class);
jeffhaobdb76512011-09-07 11:43:16 -07001162 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001163 cur_arg++;
jeffhaobdb76512011-09-07 11:43:16 -07001164 }
1165
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001166 const DexFile::ProtoId& proto_id =
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001167 dex_file_->GetMethodPrototype(dex_file_->GetMethodId(dex_method_idx_));
Ian Rogers0571d352011-11-03 19:51:38 -07001168 DexFileParameterIterator iterator(*dex_file_, proto_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07001169
1170 for (; iterator.HasNext(); iterator.Next()) {
1171 const char* descriptor = iterator.GetDescriptor();
1172 if (descriptor == NULL) {
1173 LOG(FATAL) << "Null descriptor";
1174 }
1175 if (cur_arg >= expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07001176 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1177 << " args, found more (" << descriptor << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001178 return false;
1179 }
1180 switch (descriptor[0]) {
1181 case 'L':
1182 case '[':
1183 // We assume that reference arguments are initialized. The only way it could be otherwise
1184 // (assuming the caller was verified) is if the current method is <init>, but in that case
1185 // it's effectively considered initialized the instant we reach here (in the sense that we
1186 // can return without doing anything or call virtual methods).
1187 {
Sebastien Hertz2ed76f92014-04-22 17:11:08 +02001188 const RegType& reg_type = ResolveClassAndCheckAccess(iterator.GetTypeIdx());
1189 if (!reg_type.IsNonZeroReferenceTypes()) {
1190 DCHECK(HasFailures());
1191 return false;
1192 }
Ian Rogers84fa0742011-10-25 18:13:30 -07001193 reg_line->SetRegisterType(arg_start + cur_arg, reg_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001194 }
1195 break;
1196 case 'Z':
1197 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Boolean());
1198 break;
1199 case 'C':
1200 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Char());
1201 break;
1202 case 'B':
1203 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Byte());
1204 break;
1205 case 'I':
1206 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Integer());
1207 break;
1208 case 'S':
1209 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Short());
1210 break;
1211 case 'F':
1212 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Float());
1213 break;
1214 case 'J':
1215 case 'D': {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001216 const RegType& lo_half = descriptor[0] == 'J' ? reg_types_.LongLo() : reg_types_.DoubleLo();
1217 const RegType& hi_half = descriptor[0] == 'J' ? reg_types_.LongHi() : reg_types_.DoubleHi();
1218 reg_line->SetRegisterTypeWide(arg_start + cur_arg, lo_half, hi_half);
Ian Rogersd81871c2011-10-03 13:57:23 -07001219 cur_arg++;
1220 break;
1221 }
1222 default:
Brian Carlstrom93c33962013-07-26 10:37:43 -07001223 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected signature type char '"
1224 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001225 return false;
1226 }
1227 cur_arg++;
1228 }
1229 if (cur_arg != expected_args) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001230 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1231 << " arguments, found " << cur_arg;
Ian Rogersd81871c2011-10-03 13:57:23 -07001232 return false;
1233 }
1234 const char* descriptor = dex_file_->GetReturnTypeDescriptor(proto_id);
1235 // Validate return type. We don't do the type lookup; just want to make sure that it has the right
1236 // format. Only major difference from the method argument format is that 'V' is supported.
1237 bool result;
1238 if (IsPrimitiveDescriptor(descriptor[0]) || descriptor[0] == 'V') {
1239 result = descriptor[1] == '\0';
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001240 } else if (descriptor[0] == '[') { // single/multi-dimensional array of object/primitive
Ian Rogersd81871c2011-10-03 13:57:23 -07001241 size_t i = 0;
1242 do {
1243 i++;
1244 } while (descriptor[i] == '['); // process leading [
1245 if (descriptor[i] == 'L') { // object array
1246 do {
1247 i++; // find closing ;
1248 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1249 result = descriptor[i] == ';';
1250 } else { // primitive array
1251 result = IsPrimitiveDescriptor(descriptor[i]) && descriptor[i + 1] == '\0';
1252 }
1253 } else if (descriptor[0] == 'L') {
1254 // could be more thorough here, but shouldn't be required
1255 size_t i = 0;
1256 do {
1257 i++;
1258 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1259 result = descriptor[i] == ';';
1260 } else {
1261 result = false;
1262 }
1263 if (!result) {
jeffhaod5347e02012-03-22 17:25:05 -07001264 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected char in return type descriptor '"
1265 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001266 }
1267 return result;
jeffhaobdb76512011-09-07 11:43:16 -07001268}
1269
Ian Rogers776ac1f2012-04-13 23:36:36 -07001270bool MethodVerifier::CodeFlowVerifyMethod() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001271 const uint16_t* insns = code_item_->insns_;
1272 const uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaoba5ebb92011-08-25 17:24:37 -07001273
jeffhaobdb76512011-09-07 11:43:16 -07001274 /* Begin by marking the first instruction as "changed". */
Ian Rogersd81871c2011-10-03 13:57:23 -07001275 insn_flags_[0].SetChanged();
1276 uint32_t start_guess = 0;
jeffhaoba5ebb92011-08-25 17:24:37 -07001277
jeffhaobdb76512011-09-07 11:43:16 -07001278 /* Continue until no instructions are marked "changed". */
1279 while (true) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001280 // Find the first marked one. Use "start_guess" as a way to find one quickly.
1281 uint32_t insn_idx = start_guess;
1282 for (; insn_idx < insns_size; insn_idx++) {
1283 if (insn_flags_[insn_idx].IsChanged())
jeffhaobdb76512011-09-07 11:43:16 -07001284 break;
1285 }
jeffhaobdb76512011-09-07 11:43:16 -07001286 if (insn_idx == insns_size) {
1287 if (start_guess != 0) {
1288 /* try again, starting from the top */
1289 start_guess = 0;
1290 continue;
1291 } else {
1292 /* all flags are clear */
1293 break;
1294 }
1295 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001296 // We carry the working set of registers from instruction to instruction. If this address can
1297 // be the target of a branch (or throw) instruction, or if we're skipping around chasing
1298 // "changed" flags, we need to load the set of registers from the table.
1299 // Because we always prefer to continue on to the next instruction, we should never have a
1300 // situation where we have a stray "changed" flag set on an instruction that isn't a branch
1301 // target.
1302 work_insn_idx_ = insn_idx;
1303 if (insn_flags_[insn_idx].IsBranchTarget()) {
1304 work_line_->CopyFromLine(reg_table_.GetLine(insn_idx));
jeffhaobdb76512011-09-07 11:43:16 -07001305 } else {
1306#ifndef NDEBUG
1307 /*
1308 * Sanity check: retrieve the stored register line (assuming
1309 * a full table) and make sure it actually matches.
1310 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001311 RegisterLine* register_line = reg_table_.GetLine(insn_idx);
1312 if (register_line != NULL) {
1313 if (work_line_->CompareLine(register_line) != 0) {
1314 Dump(std::cout);
1315 std::cout << info_messages_.str();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001316 LOG(FATAL) << "work_line diverged in " << PrettyMethod(dex_method_idx_, *dex_file_)
Elliott Hughesc073b072012-05-24 19:29:17 -07001317 << "@" << reinterpret_cast<void*>(work_insn_idx_) << "\n"
1318 << " work_line=" << *work_line_ << "\n"
Elliott Hughes398f64b2012-03-26 18:05:48 -07001319 << " expected=" << *register_line;
Ian Rogersd81871c2011-10-03 13:57:23 -07001320 }
jeffhaobdb76512011-09-07 11:43:16 -07001321 }
1322#endif
1323 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001324 if (!CodeFlowVerifyInstruction(&start_guess)) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001325 std::string prepend(PrettyMethod(dex_method_idx_, *dex_file_));
Ian Rogersad0b3a32012-04-16 14:50:24 -07001326 prepend += " failed to verify: ";
1327 PrependToLastFailMessage(prepend);
jeffhaoba5ebb92011-08-25 17:24:37 -07001328 return false;
1329 }
jeffhaobdb76512011-09-07 11:43:16 -07001330 /* Clear "changed" and mark as visited. */
Ian Rogersd81871c2011-10-03 13:57:23 -07001331 insn_flags_[insn_idx].SetVisited();
1332 insn_flags_[insn_idx].ClearChanged();
jeffhaobdb76512011-09-07 11:43:16 -07001333 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001334
Ian Rogers1c849e52012-06-28 14:00:33 -07001335 if (gDebugVerify) {
jeffhaobdb76512011-09-07 11:43:16 -07001336 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001337 * Scan for dead code. There's nothing "evil" about dead code
jeffhaobdb76512011-09-07 11:43:16 -07001338 * (besides the wasted space), but it indicates a flaw somewhere
1339 * down the line, possibly in the verifier.
1340 *
1341 * If we've substituted "always throw" instructions into the stream,
1342 * we are almost certainly going to have some dead code.
1343 */
1344 int dead_start = -1;
Ian Rogersd81871c2011-10-03 13:57:23 -07001345 uint32_t insn_idx = 0;
1346 for (; insn_idx < insns_size; insn_idx += insn_flags_[insn_idx].GetLengthInCodeUnits()) {
jeffhaobdb76512011-09-07 11:43:16 -07001347 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001348 * Switch-statement data doesn't get "visited" by scanner. It
jeffhaobdb76512011-09-07 11:43:16 -07001349 * may or may not be preceded by a padding NOP (for alignment).
1350 */
1351 if (insns[insn_idx] == Instruction::kPackedSwitchSignature ||
1352 insns[insn_idx] == Instruction::kSparseSwitchSignature ||
1353 insns[insn_idx] == Instruction::kArrayDataSignature ||
Elliott Hughes380aaa72012-07-09 14:33:15 -07001354 (insns[insn_idx] == Instruction::NOP && (insn_idx + 1 < insns_size) &&
jeffhaobdb76512011-09-07 11:43:16 -07001355 (insns[insn_idx + 1] == Instruction::kPackedSwitchSignature ||
1356 insns[insn_idx + 1] == Instruction::kSparseSwitchSignature ||
1357 insns[insn_idx + 1] == Instruction::kArrayDataSignature))) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001358 insn_flags_[insn_idx].SetVisited();
jeffhaobdb76512011-09-07 11:43:16 -07001359 }
1360
Ian Rogersd81871c2011-10-03 13:57:23 -07001361 if (!insn_flags_[insn_idx].IsVisited()) {
jeffhaobdb76512011-09-07 11:43:16 -07001362 if (dead_start < 0)
1363 dead_start = insn_idx;
1364 } else 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);
jeffhaobdb76512011-09-07 11:43:16 -07001367 dead_start = -1;
1368 }
1369 }
1370 if (dead_start >= 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001371 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start)
1372 << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaoba5ebb92011-08-25 17:24:37 -07001373 }
Ian Rogersc9e463c2013-06-05 16:52:26 -07001374 // To dump the state of the verify after a method, do something like:
1375 // if (PrettyMethod(dex_method_idx_, *dex_file_) ==
1376 // "boolean java.lang.String.equals(java.lang.Object)") {
1377 // LOG(INFO) << info_messages_.str();
1378 // }
jeffhaoba5ebb92011-08-25 17:24:37 -07001379 }
jeffhaobdb76512011-09-07 11:43:16 -07001380 return true;
1381}
1382
Ian Rogers776ac1f2012-04-13 23:36:36 -07001383bool MethodVerifier::CodeFlowVerifyInstruction(uint32_t* start_guess) {
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001384 // If we're doing FindLocksAtDexPc, check whether we're at the dex pc we care about.
1385 // We want the state _before_ the instruction, for the case where the dex pc we're
1386 // interested in is itself a monitor-enter instruction (which is a likely place
1387 // for a thread to be suspended).
1388 if (monitor_enter_dex_pcs_ != NULL && work_insn_idx_ == interesting_dex_pc_) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001389 monitor_enter_dex_pcs_->clear(); // The new work line is more accurate than the previous one.
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001390 for (size_t i = 0; i < work_line_->GetMonitorEnterCount(); ++i) {
1391 monitor_enter_dex_pcs_->push_back(work_line_->GetMonitorEnterDexPc(i));
1392 }
1393 }
1394
jeffhaobdb76512011-09-07 11:43:16 -07001395 /*
1396 * Once we finish decoding the instruction, we need to figure out where
jeffhaod1f0fde2011-09-08 17:25:33 -07001397 * we can go from here. There are three possible ways to transfer
jeffhaobdb76512011-09-07 11:43:16 -07001398 * control to another statement:
1399 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001400 * (1) Continue to the next instruction. Applies to all but
jeffhaobdb76512011-09-07 11:43:16 -07001401 * unconditional branches, method returns, and exception throws.
jeffhaod1f0fde2011-09-08 17:25:33 -07001402 * (2) Branch to one or more possible locations. Applies to branches
jeffhaobdb76512011-09-07 11:43:16 -07001403 * and switch statements.
jeffhaod1f0fde2011-09-08 17:25:33 -07001404 * (3) Exception handlers. Applies to any instruction that can
jeffhaobdb76512011-09-07 11:43:16 -07001405 * throw an exception that is handled by an encompassing "try"
1406 * block.
1407 *
1408 * We can also return, in which case there is no successor instruction
1409 * from this point.
1410 *
Elliott Hughesadb8c672012-03-06 16:49:32 -08001411 * The behavior can be determined from the opcode flags.
jeffhaobdb76512011-09-07 11:43:16 -07001412 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001413 const uint16_t* insns = code_item_->insns_ + work_insn_idx_;
1414 const Instruction* inst = Instruction::At(insns);
Ian Rogersa75a0132012-09-28 11:41:42 -07001415 int opcode_flags = Instruction::FlagsOf(inst->Opcode());
jeffhaobdb76512011-09-07 11:43:16 -07001416
jeffhaobdb76512011-09-07 11:43:16 -07001417 int32_t branch_target = 0;
jeffhaobdb76512011-09-07 11:43:16 -07001418 bool just_set_result = false;
Ian Rogers2c8a8572011-10-24 17:11:36 -07001419 if (gDebugVerify) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001420 // Generate processing back trace to debug verifier
Elliott Hughesc073b072012-05-24 19:29:17 -07001421 LogVerifyInfo() << "Processing " << inst->DumpString(dex_file_) << "\n"
1422 << *work_line_.get() << "\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001423 }
jeffhaobdb76512011-09-07 11:43:16 -07001424
1425 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001426 * Make a copy of the previous register state. If the instruction
jeffhaobdb76512011-09-07 11:43:16 -07001427 * can throw an exception, we will copy/merge this into the "catch"
1428 * address rather than work_line, because we don't want the result
1429 * from the "successful" code path (e.g. a check-cast that "improves"
1430 * a type) to be visible to the exception handler.
1431 */
Ian Rogers776ac1f2012-04-13 23:36:36 -07001432 if ((opcode_flags & Instruction::kThrow) != 0 && CurrentInsnFlags()->IsInTry()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001433 saved_line_->CopyFromLine(work_line_.get());
jeffhaobdb76512011-09-07 11:43:16 -07001434 } else {
1435#ifndef NDEBUG
Ian Rogersd81871c2011-10-03 13:57:23 -07001436 saved_line_->FillWithGarbage();
jeffhaobdb76512011-09-07 11:43:16 -07001437#endif
1438 }
1439
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07001440
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001441 // We need to ensure the work line is consistent while performing validation. When we spot a
1442 // peephole pattern we compute a new line for either the fallthrough instruction or the
1443 // branch target.
Ian Rogers700a4022014-05-19 16:49:03 -07001444 std::unique_ptr<RegisterLine> branch_line;
1445 std::unique_ptr<RegisterLine> fallthrough_line;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001446
Sebastien Hertz5243e912013-05-21 10:55:07 +02001447 switch (inst->Opcode()) {
jeffhaobdb76512011-09-07 11:43:16 -07001448 case Instruction::NOP:
1449 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001450 * A "pure" NOP has no effect on anything. Data tables start with
jeffhaobdb76512011-09-07 11:43:16 -07001451 * a signature that looks like a NOP; if we see one of these in
1452 * the course of executing code then we have a problem.
1453 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001454 if (inst->VRegA_10x() != 0) {
jeffhaod5347e02012-03-22 17:25:05 -07001455 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "encountered data table in instruction stream";
jeffhaobdb76512011-09-07 11:43:16 -07001456 }
1457 break;
1458
1459 case Instruction::MOVE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001460 work_line_->CopyRegister1(inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategory1nr);
1461 break;
jeffhaobdb76512011-09-07 11:43:16 -07001462 case Instruction::MOVE_FROM16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001463 work_line_->CopyRegister1(inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategory1nr);
1464 break;
jeffhaobdb76512011-09-07 11:43:16 -07001465 case Instruction::MOVE_16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001466 work_line_->CopyRegister1(inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategory1nr);
jeffhaobdb76512011-09-07 11:43:16 -07001467 break;
1468 case Instruction::MOVE_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001469 work_line_->CopyRegister2(inst->VRegA_12x(), inst->VRegB_12x());
1470 break;
jeffhaobdb76512011-09-07 11:43:16 -07001471 case Instruction::MOVE_WIDE_FROM16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001472 work_line_->CopyRegister2(inst->VRegA_22x(), inst->VRegB_22x());
1473 break;
jeffhaobdb76512011-09-07 11:43:16 -07001474 case Instruction::MOVE_WIDE_16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001475 work_line_->CopyRegister2(inst->VRegA_32x(), inst->VRegB_32x());
jeffhaobdb76512011-09-07 11:43:16 -07001476 break;
1477 case Instruction::MOVE_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001478 work_line_->CopyRegister1(inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategoryRef);
1479 break;
jeffhaobdb76512011-09-07 11:43:16 -07001480 case Instruction::MOVE_OBJECT_FROM16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001481 work_line_->CopyRegister1(inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategoryRef);
1482 break;
jeffhaobdb76512011-09-07 11:43:16 -07001483 case Instruction::MOVE_OBJECT_16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001484 work_line_->CopyRegister1(inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategoryRef);
jeffhaobdb76512011-09-07 11:43:16 -07001485 break;
1486
1487 /*
1488 * The move-result instructions copy data out of a "pseudo-register"
jeffhaod1f0fde2011-09-08 17:25:33 -07001489 * with the results from the last method invocation. In practice we
jeffhaobdb76512011-09-07 11:43:16 -07001490 * might want to hold the result in an actual CPU register, so the
1491 * Dalvik spec requires that these only appear immediately after an
1492 * invoke or filled-new-array.
1493 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001494 * These calls invalidate the "result" register. (This is now
jeffhaobdb76512011-09-07 11:43:16 -07001495 * redundant with the reset done below, but it can make the debug info
1496 * easier to read in some cases.)
1497 */
1498 case Instruction::MOVE_RESULT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001499 work_line_->CopyResultRegister1(inst->VRegA_11x(), false);
jeffhaobdb76512011-09-07 11:43:16 -07001500 break;
1501 case Instruction::MOVE_RESULT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001502 work_line_->CopyResultRegister2(inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07001503 break;
1504 case Instruction::MOVE_RESULT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001505 work_line_->CopyResultRegister1(inst->VRegA_11x(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001506 break;
1507
Ian Rogersd81871c2011-10-03 13:57:23 -07001508 case Instruction::MOVE_EXCEPTION: {
jeffhaobdb76512011-09-07 11:43:16 -07001509 /*
jeffhao60f83e32012-02-13 17:16:30 -08001510 * This statement can only appear as the first instruction in an exception handler. We verify
1511 * that as part of extracting the exception type from the catch block list.
jeffhaobdb76512011-09-07 11:43:16 -07001512 */
Ian Rogers28ad40d2011-10-27 15:19:26 -07001513 const RegType& res_type = GetCaughtExceptionType();
Sebastien Hertz5243e912013-05-21 10:55:07 +02001514 work_line_->SetRegisterType(inst->VRegA_11x(), res_type);
jeffhaobdb76512011-09-07 11:43:16 -07001515 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001516 }
jeffhaobdb76512011-09-07 11:43:16 -07001517 case Instruction::RETURN_VOID:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001518 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
1519 if (!GetMethodReturnType().IsConflict()) {
jeffhaod5347e02012-03-22 17:25:05 -07001520 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001521 }
jeffhaobdb76512011-09-07 11:43:16 -07001522 }
1523 break;
1524 case Instruction::RETURN:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001525 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
jeffhaobdb76512011-09-07 11:43:16 -07001526 /* check the method signature */
Ian Rogersd81871c2011-10-03 13:57:23 -07001527 const RegType& return_type = GetMethodReturnType();
1528 if (!return_type.IsCategory1Types()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001529 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected non-category 1 return type "
1530 << return_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001531 } else {
1532 // Compilers may generate synthetic functions that write byte values into boolean fields.
1533 // Also, it may use integer values for boolean, byte, short, and character return types.
Sebastien Hertz5243e912013-05-21 10:55:07 +02001534 const uint32_t vregA = inst->VRegA_11x();
1535 const RegType& src_type = work_line_->GetRegisterType(vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07001536 bool use_src = ((return_type.IsBoolean() && src_type.IsByte()) ||
1537 ((return_type.IsBoolean() || return_type.IsByte() ||
1538 return_type.IsShort() || return_type.IsChar()) &&
1539 src_type.IsInteger()));
1540 /* check the register contents */
Ian Rogersad0b3a32012-04-16 14:50:24 -07001541 bool success =
Sebastien Hertz5243e912013-05-21 10:55:07 +02001542 work_line_->VerifyRegisterType(vregA, use_src ? src_type : return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001543 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001544 AppendToLastFailMessage(StringPrintf(" return-1nr on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07001545 }
jeffhaobdb76512011-09-07 11:43:16 -07001546 }
1547 }
1548 break;
1549 case Instruction::RETURN_WIDE:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001550 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
jeffhaobdb76512011-09-07 11:43:16 -07001551 /* check the method signature */
Ian Rogersd81871c2011-10-03 13:57:23 -07001552 const RegType& return_type = GetMethodReturnType();
1553 if (!return_type.IsCategory2Types()) {
jeffhaod5347e02012-03-22 17:25:05 -07001554 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-wide not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001555 } else {
1556 /* check the register contents */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001557 const uint32_t vregA = inst->VRegA_11x();
1558 bool success = work_line_->VerifyRegisterType(vregA, return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001559 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001560 AppendToLastFailMessage(StringPrintf(" return-wide on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07001561 }
jeffhaobdb76512011-09-07 11:43:16 -07001562 }
1563 }
1564 break;
1565 case Instruction::RETURN_OBJECT:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001566 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001567 const RegType& return_type = GetMethodReturnType();
1568 if (!return_type.IsReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001569 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-object not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001570 } else {
1571 /* return_type is the *expected* return type, not register value */
1572 DCHECK(!return_type.IsZero());
1573 DCHECK(!return_type.IsUninitializedReference());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001574 const uint32_t vregA = inst->VRegA_11x();
1575 const RegType& reg_type = work_line_->GetRegisterType(vregA);
Ian Rogers9074b992011-10-26 17:41:55 -07001576 // Disallow returning uninitialized values and verify that the reference in vAA is an
1577 // instance of the "return_type"
1578 if (reg_type.IsUninitializedTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001579 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "returning uninitialized object '"
1580 << reg_type << "'";
Ian Rogers9074b992011-10-26 17:41:55 -07001581 } else if (!return_type.IsAssignableFrom(reg_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07001582 if (reg_type.IsUnresolvedTypes() || return_type.IsUnresolvedTypes()) {
1583 Fail(VERIFY_ERROR_NO_CLASS) << " can't resolve returned type '" << return_type
1584 << "' or '" << reg_type << "'";
1585 } else {
1586 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning '" << reg_type
1587 << "', but expected from declaration '" << return_type << "'";
1588 }
jeffhaobdb76512011-09-07 11:43:16 -07001589 }
1590 }
1591 }
1592 break;
1593
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001594 /* could be boolean, int, float, or a null reference */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001595 case Instruction::CONST_4: {
1596 int32_t val = static_cast<int32_t>(inst->VRegB_11n() << 28) >> 28;
Sebastien Hertz849600b2013-12-20 10:28:08 +01001597 work_line_->SetRegisterType(inst->VRegA_11n(),
Ian Rogers46960fe2014-05-23 10:43:43 -07001598 DetermineCat1Constant(val, need_precise_constants_));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001599 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02001600 }
1601 case Instruction::CONST_16: {
1602 int16_t val = static_cast<int16_t>(inst->VRegB_21s());
Sebastien Hertz849600b2013-12-20 10:28:08 +01001603 work_line_->SetRegisterType(inst->VRegA_21s(),
Ian Rogers46960fe2014-05-23 10:43:43 -07001604 DetermineCat1Constant(val, need_precise_constants_));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001605 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02001606 }
Sebastien Hertz849600b2013-12-20 10:28:08 +01001607 case Instruction::CONST: {
1608 int32_t val = inst->VRegB_31i();
Sebastien Hertz5243e912013-05-21 10:55:07 +02001609 work_line_->SetRegisterType(inst->VRegA_31i(),
Ian Rogers46960fe2014-05-23 10:43:43 -07001610 DetermineCat1Constant(val, need_precise_constants_));
jeffhaobdb76512011-09-07 11:43:16 -07001611 break;
Sebastien Hertz849600b2013-12-20 10:28:08 +01001612 }
1613 case Instruction::CONST_HIGH16: {
1614 int32_t val = static_cast<int32_t>(inst->VRegB_21h() << 16);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001615 work_line_->SetRegisterType(inst->VRegA_21h(),
Ian Rogers46960fe2014-05-23 10:43:43 -07001616 DetermineCat1Constant(val, need_precise_constants_));
jeffhaobdb76512011-09-07 11:43:16 -07001617 break;
Sebastien Hertz849600b2013-12-20 10:28:08 +01001618 }
jeffhaobdb76512011-09-07 11:43:16 -07001619 /* could be long or double; resolved upon use */
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001620 case Instruction::CONST_WIDE_16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001621 int64_t val = static_cast<int16_t>(inst->VRegB_21s());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001622 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1623 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001624 work_line_->SetRegisterTypeWide(inst->VRegA_21s(), lo, hi);
jeffhaobdb76512011-09-07 11:43:16 -07001625 break;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001626 }
1627 case Instruction::CONST_WIDE_32: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001628 int64_t val = static_cast<int32_t>(inst->VRegB_31i());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001629 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1630 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001631 work_line_->SetRegisterTypeWide(inst->VRegA_31i(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001632 break;
1633 }
1634 case Instruction::CONST_WIDE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001635 int64_t val = inst->VRegB_51l();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001636 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1637 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001638 work_line_->SetRegisterTypeWide(inst->VRegA_51l(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001639 break;
1640 }
1641 case Instruction::CONST_WIDE_HIGH16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001642 int64_t val = static_cast<uint64_t>(inst->VRegB_21h()) << 48;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001643 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1644 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001645 work_line_->SetRegisterTypeWide(inst->VRegA_21h(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001646 break;
1647 }
jeffhaobdb76512011-09-07 11:43:16 -07001648 case Instruction::CONST_STRING:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001649 work_line_->SetRegisterType(inst->VRegA_21c(), reg_types_.JavaLangString());
1650 break;
jeffhaobdb76512011-09-07 11:43:16 -07001651 case Instruction::CONST_STRING_JUMBO:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001652 work_line_->SetRegisterType(inst->VRegA_31c(), reg_types_.JavaLangString());
jeffhaobdb76512011-09-07 11:43:16 -07001653 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001654 case Instruction::CONST_CLASS: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001655 // Get type from instruction if unresolved then we need an access check
1656 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Sebastien Hertz5243e912013-05-21 10:55:07 +02001657 const RegType& res_type = ResolveClassAndCheckAccess(inst->VRegB_21c());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001658 // Register holds class, ie its type is class, on error it will hold Conflict.
Sebastien Hertz5243e912013-05-21 10:55:07 +02001659 work_line_->SetRegisterType(inst->VRegA_21c(),
Ian Rogersb4903572012-10-11 11:52:56 -07001660 res_type.IsConflict() ? res_type
1661 : reg_types_.JavaLangClass(true));
jeffhaobdb76512011-09-07 11:43:16 -07001662 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001663 }
jeffhaobdb76512011-09-07 11:43:16 -07001664 case Instruction::MONITOR_ENTER:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001665 work_line_->PushMonitor(inst->VRegA_11x(), work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07001666 break;
1667 case Instruction::MONITOR_EXIT:
1668 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001669 * monitor-exit instructions are odd. They can throw exceptions,
jeffhaobdb76512011-09-07 11:43:16 -07001670 * but when they do they act as if they succeeded and the PC is
jeffhaod1f0fde2011-09-08 17:25:33 -07001671 * pointing to the following instruction. (This behavior goes back
jeffhaobdb76512011-09-07 11:43:16 -07001672 * to the need to handle asynchronous exceptions, a now-deprecated
1673 * feature that Dalvik doesn't support.)
1674 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001675 * In practice we don't need to worry about this. The only
jeffhaobdb76512011-09-07 11:43:16 -07001676 * exceptions that can be thrown from monitor-exit are for a
jeffhaod1f0fde2011-09-08 17:25:33 -07001677 * null reference and -exit without a matching -enter. If the
jeffhaobdb76512011-09-07 11:43:16 -07001678 * structured locking checks are working, the former would have
1679 * failed on the -enter instruction, and the latter is impossible.
1680 *
1681 * This is fortunate, because issue 3221411 prevents us from
1682 * chasing the "can throw" path when monitor verification is
jeffhaod1f0fde2011-09-08 17:25:33 -07001683 * enabled. If we can fully verify the locking we can ignore
jeffhaobdb76512011-09-07 11:43:16 -07001684 * some catch blocks (which will show up as "dead" code when
1685 * we skip them here); if we can't, then the code path could be
1686 * "live" so we still need to check it.
1687 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001688 opcode_flags &= ~Instruction::kThrow;
Sebastien Hertz5243e912013-05-21 10:55:07 +02001689 work_line_->PopMonitor(inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07001690 break;
1691
Ian Rogers28ad40d2011-10-27 15:19:26 -07001692 case Instruction::CHECK_CAST:
Ian Rogersd81871c2011-10-03 13:57:23 -07001693 case Instruction::INSTANCE_OF: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001694 /*
1695 * If this instruction succeeds, we will "downcast" register vA to the type in vB. (This
1696 * could be a "upcast" -- not expected, so we don't try to address it.)
1697 *
1698 * If it fails, an exception is thrown, which we deal with later by ignoring the update to
Elliott Hughesadb8c672012-03-06 16:49:32 -08001699 * dec_insn.vA when branching to a handler.
Ian Rogers28ad40d2011-10-27 15:19:26 -07001700 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001701 const bool is_checkcast = (inst->Opcode() == Instruction::CHECK_CAST);
1702 const uint32_t type_idx = (is_checkcast) ? inst->VRegB_21c() : inst->VRegC_22c();
1703 const RegType& res_type = ResolveClassAndCheckAccess(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001704 if (res_type.IsConflict()) {
1705 DCHECK_NE(failures_.size(), 0U);
1706 if (!is_checkcast) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001707 work_line_->SetRegisterType(inst->VRegA_22c(), reg_types_.Boolean());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001708 }
1709 break; // bad class
Ian Rogers9f1ab122011-12-12 08:52:43 -08001710 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001711 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Sebastien Hertz5243e912013-05-21 10:55:07 +02001712 uint32_t orig_type_reg = (is_checkcast) ? inst->VRegA_21c() : inst->VRegB_22c();
1713 const RegType& orig_type = work_line_->GetRegisterType(orig_type_reg);
Ian Rogers28ad40d2011-10-27 15:19:26 -07001714 if (!res_type.IsNonZeroReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001715 if (is_checkcast) {
1716 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on unexpected class " << res_type;
1717 } else {
1718 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on unexpected class " << res_type;
1719 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001720 } else if (!orig_type.IsReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001721 if (is_checkcast) {
1722 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on non-reference in v" << orig_type_reg;
1723 } else {
1724 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on non-reference in v" << orig_type_reg;
1725 }
jeffhao2a8a90e2011-09-26 14:25:31 -07001726 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001727 if (is_checkcast) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001728 work_line_->SetRegisterType(inst->VRegA_21c(), res_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001729 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001730 work_line_->SetRegisterType(inst->VRegA_22c(), reg_types_.Boolean());
jeffhaobdb76512011-09-07 11:43:16 -07001731 }
jeffhaobdb76512011-09-07 11:43:16 -07001732 }
jeffhao2a8a90e2011-09-26 14:25:31 -07001733 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001734 }
1735 case Instruction::ARRAY_LENGTH: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001736 const RegType& res_type = work_line_->GetRegisterType(inst->VRegB_12x());
Ian Rogers28ad40d2011-10-27 15:19:26 -07001737 if (res_type.IsReferenceTypes()) {
Ian Rogers89310de2012-02-01 13:47:30 -08001738 if (!res_type.IsArrayTypes() && !res_type.IsZero()) { // ie not an array or null
jeffhaod5347e02012-03-22 17:25:05 -07001739 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001740 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001741 work_line_->SetRegisterType(inst->VRegA_12x(), reg_types_.Integer());
Ian Rogersd81871c2011-10-03 13:57:23 -07001742 }
1743 }
1744 break;
1745 }
1746 case Instruction::NEW_INSTANCE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001747 const RegType& res_type = ResolveClassAndCheckAccess(inst->VRegB_21c());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001748 if (res_type.IsConflict()) {
1749 DCHECK_NE(failures_.size(), 0U);
1750 break; // bad class
jeffhao8cd6dda2012-02-22 10:15:34 -08001751 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001752 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
1753 // can't create an instance of an interface or abstract class */
1754 if (!res_type.IsInstantiableTypes()) {
1755 Fail(VERIFY_ERROR_INSTANTIATION)
1756 << "new-instance on primitive, interface or abstract class" << res_type;
Ian Rogers08f753d2012-08-24 14:35:25 -07001757 // Soft failure so carry on to set register type.
Ian Rogersd81871c2011-10-03 13:57:23 -07001758 }
Ian Rogers08f753d2012-08-24 14:35:25 -07001759 const RegType& uninit_type = reg_types_.Uninitialized(res_type, work_insn_idx_);
1760 // Any registers holding previous allocations from this address that have not yet been
1761 // initialized must be marked invalid.
1762 work_line_->MarkUninitRefsAsInvalid(uninit_type);
1763 // add the new uninitialized reference to the register state
Sebastien Hertz5243e912013-05-21 10:55:07 +02001764 work_line_->SetRegisterType(inst->VRegA_21c(), uninit_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001765 break;
1766 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08001767 case Instruction::NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001768 VerifyNewArray(inst, false, false);
jeffhaobdb76512011-09-07 11:43:16 -07001769 break;
1770 case Instruction::FILLED_NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001771 VerifyNewArray(inst, true, false);
Ian Rogers0c4a5062012-02-03 15:18:59 -08001772 just_set_result = true; // Filled new array sets result register
jeffhaobdb76512011-09-07 11:43:16 -07001773 break;
Ian Rogers0c4a5062012-02-03 15:18:59 -08001774 case Instruction::FILLED_NEW_ARRAY_RANGE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001775 VerifyNewArray(inst, true, true);
Ian Rogers0c4a5062012-02-03 15:18:59 -08001776 just_set_result = true; // Filled new array range sets result register
1777 break;
jeffhaobdb76512011-09-07 11:43:16 -07001778 case Instruction::CMPL_FLOAT:
1779 case Instruction::CMPG_FLOAT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001780 if (!work_line_->VerifyRegisterType(inst->VRegB_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08001781 break;
1782 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02001783 if (!work_line_->VerifyRegisterType(inst->VRegC_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08001784 break;
1785 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02001786 work_line_->SetRegisterType(inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001787 break;
1788 case Instruction::CMPL_DOUBLE:
1789 case Instruction::CMPG_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001790 if (!work_line_->VerifyRegisterTypeWide(inst->VRegB_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001791 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001792 break;
1793 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02001794 if (!work_line_->VerifyRegisterTypeWide(inst->VRegC_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001795 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001796 break;
1797 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02001798 work_line_->SetRegisterType(inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001799 break;
1800 case Instruction::CMP_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001801 if (!work_line_->VerifyRegisterTypeWide(inst->VRegB_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001802 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001803 break;
1804 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02001805 if (!work_line_->VerifyRegisterTypeWide(inst->VRegC_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001806 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001807 break;
1808 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02001809 work_line_->SetRegisterType(inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001810 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001811 case Instruction::THROW: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001812 const RegType& res_type = work_line_->GetRegisterType(inst->VRegA_11x());
Ian Rogersb4903572012-10-11 11:52:56 -07001813 if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(res_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07001814 Fail(res_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS : VERIFY_ERROR_BAD_CLASS_SOFT)
1815 << "thrown class " << res_type << " not instanceof Throwable";
jeffhaobdb76512011-09-07 11:43:16 -07001816 }
1817 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001818 }
jeffhaobdb76512011-09-07 11:43:16 -07001819 case Instruction::GOTO:
1820 case Instruction::GOTO_16:
1821 case Instruction::GOTO_32:
1822 /* no effect on or use of registers */
1823 break;
1824
1825 case Instruction::PACKED_SWITCH:
1826 case Instruction::SPARSE_SWITCH:
1827 /* verify that vAA is an integer, or can be converted to one */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001828 work_line_->VerifyRegisterType(inst->VRegA_31t(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001829 break;
1830
Ian Rogersd81871c2011-10-03 13:57:23 -07001831 case Instruction::FILL_ARRAY_DATA: {
1832 /* Similar to the verification done for APUT */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001833 const RegType& array_type = work_line_->GetRegisterType(inst->VRegA_31t());
Ian Rogers89310de2012-02-01 13:47:30 -08001834 /* array_type can be null if the reg type is Zero */
1835 if (!array_type.IsZero()) {
jeffhao457cc512012-02-02 16:55:13 -08001836 if (!array_type.IsArrayTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001837 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with array type "
1838 << array_type;
Ian Rogers89310de2012-02-01 13:47:30 -08001839 } else {
Mathieu Chartier590fee92013-09-13 13:46:47 -07001840 const RegType& component_type = reg_types_.GetComponentType(array_type,
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001841 class_loader_->Get());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001842 DCHECK(!component_type.IsConflict());
jeffhao457cc512012-02-02 16:55:13 -08001843 if (component_type.IsNonZeroReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001844 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with component type "
1845 << component_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001846 } else {
jeffhao457cc512012-02-02 16:55:13 -08001847 // Now verify if the element width in the table matches the element width declared in
1848 // the array
1849 const uint16_t* array_data = insns + (insns[1] | (((int32_t) insns[2]) << 16));
1850 if (array_data[0] != Instruction::kArrayDataSignature) {
jeffhaod5347e02012-03-22 17:25:05 -07001851 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid magic for array-data";
jeffhao457cc512012-02-02 16:55:13 -08001852 } else {
1853 size_t elem_width = Primitive::ComponentSize(component_type.GetPrimitiveType());
1854 // Since we don't compress the data in Dex, expect to see equal width of data stored
1855 // in the table and expected from the array class.
1856 if (array_data[1] != elem_width) {
jeffhaod5347e02012-03-22 17:25:05 -07001857 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-data size mismatch (" << array_data[1]
1858 << " vs " << elem_width << ")";
jeffhao457cc512012-02-02 16:55:13 -08001859 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001860 }
1861 }
jeffhaobdb76512011-09-07 11:43:16 -07001862 }
1863 }
1864 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001865 }
jeffhaobdb76512011-09-07 11:43:16 -07001866 case Instruction::IF_EQ:
Ian Rogersd81871c2011-10-03 13:57:23 -07001867 case Instruction::IF_NE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001868 const RegType& reg_type1 = work_line_->GetRegisterType(inst->VRegA_22t());
1869 const RegType& reg_type2 = work_line_->GetRegisterType(inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07001870 bool mismatch = false;
1871 if (reg_type1.IsZero()) { // zero then integral or reference expected
1872 mismatch = !reg_type2.IsReferenceTypes() && !reg_type2.IsIntegralTypes();
1873 } else if (reg_type1.IsReferenceTypes()) { // both references?
1874 mismatch = !reg_type2.IsReferenceTypes();
1875 } else { // both integral?
1876 mismatch = !reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes();
1877 }
1878 if (mismatch) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001879 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to if-eq/if-ne (" << reg_type1 << ","
1880 << reg_type2 << ") must both be references or integral";
jeffhaobdb76512011-09-07 11:43:16 -07001881 }
1882 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001883 }
jeffhaobdb76512011-09-07 11:43:16 -07001884 case Instruction::IF_LT:
1885 case Instruction::IF_GE:
1886 case Instruction::IF_GT:
Ian Rogersd81871c2011-10-03 13:57:23 -07001887 case Instruction::IF_LE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001888 const RegType& reg_type1 = work_line_->GetRegisterType(inst->VRegA_22t());
1889 const RegType& reg_type2 = work_line_->GetRegisterType(inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07001890 if (!reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001891 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to 'if' (" << reg_type1 << ","
1892 << reg_type2 << ") must be integral";
jeffhaobdb76512011-09-07 11:43:16 -07001893 }
1894 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001895 }
jeffhaobdb76512011-09-07 11:43:16 -07001896 case Instruction::IF_EQZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07001897 case Instruction::IF_NEZ: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001898 const RegType& reg_type = work_line_->GetRegisterType(inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07001899 if (!reg_type.IsReferenceTypes() && !reg_type.IsIntegralTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001900 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
1901 << " unexpected as arg to if-eqz/if-nez";
Ian Rogersd81871c2011-10-03 13:57:23 -07001902 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001903
1904 // Find previous instruction - its existence is a precondition to peephole optimization.
Ian Rogers9b360392013-06-06 14:45:07 -07001905 uint32_t instance_of_idx = 0;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001906 if (0 != work_insn_idx_) {
Ian Rogers9b360392013-06-06 14:45:07 -07001907 instance_of_idx = work_insn_idx_ - 1;
Brian Carlstromdf629502013-07-17 22:39:56 -07001908 while (0 != instance_of_idx && !insn_flags_[instance_of_idx].IsOpcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07001909 instance_of_idx--;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001910 }
Ian Rogers9b360392013-06-06 14:45:07 -07001911 CHECK(insn_flags_[instance_of_idx].IsOpcode());
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001912 } else {
1913 break;
1914 }
1915
Ian Rogers9b360392013-06-06 14:45:07 -07001916 const Instruction* instance_of_inst = Instruction::At(code_item_->insns_ + instance_of_idx);
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001917
1918 /* Check for peep-hole pattern of:
1919 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07001920 * instance-of vX, vY, T;
1921 * ifXXX vX, label ;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001922 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07001923 * label:
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001924 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07001925 * and sharpen the type of vY to be type T.
1926 * Note, this pattern can't be if:
1927 * - if there are other branches to this branch,
1928 * - when vX == vY.
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001929 */
Ian Rogersfae370a2013-06-05 08:33:27 -07001930 if (!CurrentInsnFlags()->IsBranchTarget() &&
Ian Rogers9b360392013-06-06 14:45:07 -07001931 (Instruction::INSTANCE_OF == instance_of_inst->Opcode()) &&
1932 (inst->VRegA_21t() == instance_of_inst->VRegA_22c()) &&
1933 (instance_of_inst->VRegA_22c() != instance_of_inst->VRegB_22c())) {
Ian Rogersfae370a2013-06-05 08:33:27 -07001934 // Check that the we are not attempting conversion to interface types,
1935 // which is not done because of the multiple inheritance implications.
Jeff Haoc642ec82013-09-04 16:11:55 -07001936 // Also don't change the type if it would result in an upcast.
1937 const RegType& orig_type = work_line_->GetRegisterType(instance_of_inst->VRegB_22c());
Ian Rogers9b360392013-06-06 14:45:07 -07001938 const RegType& cast_type = ResolveClassAndCheckAccess(instance_of_inst->VRegC_22c());
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001939
Jeff Haoa3faaf42013-09-03 19:07:00 -07001940 if (!cast_type.IsUnresolvedTypes() && !orig_type.IsUnresolvedTypes() &&
1941 !cast_type.GetClass()->IsInterface() && !cast_type.IsAssignableFrom(orig_type)) {
Ian Rogersd0fbd852013-09-24 18:17:04 -07001942 RegisterLine* update_line = RegisterLine::Create(code_item_->registers_size_, this);
Ian Rogersfae370a2013-06-05 08:33:27 -07001943 if (inst->Opcode() == Instruction::IF_EQZ) {
Ian Rogers9b360392013-06-06 14:45:07 -07001944 fallthrough_line.reset(update_line);
Ian Rogersfae370a2013-06-05 08:33:27 -07001945 } else {
Ian Rogers9b360392013-06-06 14:45:07 -07001946 branch_line.reset(update_line);
1947 }
1948 update_line->CopyFromLine(work_line_.get());
1949 update_line->SetRegisterType(instance_of_inst->VRegB_22c(), cast_type);
1950 if (!insn_flags_[instance_of_idx].IsBranchTarget() && 0 != instance_of_idx) {
1951 // See if instance-of was preceded by a move-object operation, common due to the small
1952 // register encoding space of instance-of, and propagate type information to the source
1953 // of the move-object.
1954 uint32_t move_idx = instance_of_idx - 1;
Brian Carlstromdf629502013-07-17 22:39:56 -07001955 while (0 != move_idx && !insn_flags_[move_idx].IsOpcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07001956 move_idx--;
1957 }
1958 CHECK(insn_flags_[move_idx].IsOpcode());
1959 const Instruction* move_inst = Instruction::At(code_item_->insns_ + move_idx);
1960 switch (move_inst->Opcode()) {
1961 case Instruction::MOVE_OBJECT:
1962 if (move_inst->VRegA_12x() == instance_of_inst->VRegB_22c()) {
1963 update_line->SetRegisterType(move_inst->VRegB_12x(), cast_type);
1964 }
1965 break;
1966 case Instruction::MOVE_OBJECT_FROM16:
1967 if (move_inst->VRegA_22x() == instance_of_inst->VRegB_22c()) {
1968 update_line->SetRegisterType(move_inst->VRegB_22x(), cast_type);
1969 }
1970 break;
1971 case Instruction::MOVE_OBJECT_16:
1972 if (move_inst->VRegA_32x() == instance_of_inst->VRegB_22c()) {
1973 update_line->SetRegisterType(move_inst->VRegB_32x(), cast_type);
1974 }
1975 break;
1976 default:
1977 break;
1978 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001979 }
1980 }
1981 }
1982
jeffhaobdb76512011-09-07 11:43:16 -07001983 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001984 }
jeffhaobdb76512011-09-07 11:43:16 -07001985 case Instruction::IF_LTZ:
1986 case Instruction::IF_GEZ:
1987 case Instruction::IF_GTZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07001988 case Instruction::IF_LEZ: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001989 const RegType& reg_type = work_line_->GetRegisterType(inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07001990 if (!reg_type.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001991 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
1992 << " unexpected as arg to if-ltz/if-gez/if-gtz/if-lez";
Ian Rogersd81871c2011-10-03 13:57:23 -07001993 }
jeffhaobdb76512011-09-07 11:43:16 -07001994 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001995 }
jeffhaobdb76512011-09-07 11:43:16 -07001996 case Instruction::AGET_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001997 VerifyAGet(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001998 break;
jeffhaobdb76512011-09-07 11:43:16 -07001999 case Instruction::AGET_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002000 VerifyAGet(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002001 break;
jeffhaobdb76512011-09-07 11:43:16 -07002002 case Instruction::AGET_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002003 VerifyAGet(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002004 break;
jeffhaobdb76512011-09-07 11:43:16 -07002005 case Instruction::AGET_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002006 VerifyAGet(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002007 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002008 case Instruction::AGET:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002009 VerifyAGet(inst, reg_types_.Integer(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002010 break;
jeffhaobdb76512011-09-07 11:43:16 -07002011 case Instruction::AGET_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002012 VerifyAGet(inst, reg_types_.LongLo(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002013 break;
2014 case Instruction::AGET_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002015 VerifyAGet(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07002016 break;
2017
Ian Rogersd81871c2011-10-03 13:57:23 -07002018 case Instruction::APUT_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002019 VerifyAPut(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002020 break;
2021 case Instruction::APUT_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002022 VerifyAPut(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002023 break;
2024 case Instruction::APUT_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002025 VerifyAPut(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002026 break;
2027 case Instruction::APUT_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002028 VerifyAPut(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002029 break;
2030 case Instruction::APUT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002031 VerifyAPut(inst, reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002032 break;
2033 case Instruction::APUT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002034 VerifyAPut(inst, reg_types_.LongLo(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002035 break;
2036 case Instruction::APUT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002037 VerifyAPut(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07002038 break;
2039
jeffhaobdb76512011-09-07 11:43:16 -07002040 case Instruction::IGET_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002041 VerifyISGet(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002042 break;
jeffhaobdb76512011-09-07 11:43:16 -07002043 case Instruction::IGET_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002044 VerifyISGet(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002045 break;
jeffhaobdb76512011-09-07 11:43:16 -07002046 case Instruction::IGET_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002047 VerifyISGet(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002048 break;
jeffhaobdb76512011-09-07 11:43:16 -07002049 case Instruction::IGET_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002050 VerifyISGet(inst, reg_types_.Short(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002051 break;
2052 case Instruction::IGET:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002053 VerifyISGet(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002054 break;
2055 case Instruction::IGET_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002056 VerifyISGet(inst, reg_types_.LongLo(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002057 break;
2058 case Instruction::IGET_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002059 VerifyISGet(inst, reg_types_.JavaLangObject(false), false, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002060 break;
jeffhaobdb76512011-09-07 11:43:16 -07002061
Ian Rogersd81871c2011-10-03 13:57:23 -07002062 case Instruction::IPUT_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002063 VerifyISPut(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002064 break;
2065 case Instruction::IPUT_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002066 VerifyISPut(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002067 break;
2068 case Instruction::IPUT_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002069 VerifyISPut(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002070 break;
2071 case Instruction::IPUT_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002072 VerifyISPut(inst, reg_types_.Short(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002073 break;
2074 case Instruction::IPUT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002075 VerifyISPut(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002076 break;
2077 case Instruction::IPUT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002078 VerifyISPut(inst, reg_types_.LongLo(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002079 break;
jeffhaobdb76512011-09-07 11:43:16 -07002080 case Instruction::IPUT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002081 VerifyISPut(inst, reg_types_.JavaLangObject(false), false, false);
jeffhaobdb76512011-09-07 11:43:16 -07002082 break;
2083
jeffhaobdb76512011-09-07 11:43:16 -07002084 case Instruction::SGET_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002085 VerifyISGet(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002086 break;
jeffhaobdb76512011-09-07 11:43:16 -07002087 case Instruction::SGET_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002088 VerifyISGet(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002089 break;
jeffhaobdb76512011-09-07 11:43:16 -07002090 case Instruction::SGET_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002091 VerifyISGet(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002092 break;
jeffhaobdb76512011-09-07 11:43:16 -07002093 case Instruction::SGET_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002094 VerifyISGet(inst, reg_types_.Short(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002095 break;
2096 case Instruction::SGET:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002097 VerifyISGet(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002098 break;
2099 case Instruction::SGET_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002100 VerifyISGet(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002101 break;
2102 case Instruction::SGET_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002103 VerifyISGet(inst, reg_types_.JavaLangObject(false), false, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002104 break;
2105
2106 case Instruction::SPUT_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002107 VerifyISPut(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002108 break;
2109 case Instruction::SPUT_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002110 VerifyISPut(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002111 break;
2112 case Instruction::SPUT_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002113 VerifyISPut(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002114 break;
2115 case Instruction::SPUT_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002116 VerifyISPut(inst, reg_types_.Short(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002117 break;
2118 case Instruction::SPUT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002119 VerifyISPut(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002120 break;
2121 case Instruction::SPUT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002122 VerifyISPut(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002123 break;
2124 case Instruction::SPUT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002125 VerifyISPut(inst, reg_types_.JavaLangObject(false), false, true);
jeffhaobdb76512011-09-07 11:43:16 -07002126 break;
2127
2128 case Instruction::INVOKE_VIRTUAL:
2129 case Instruction::INVOKE_VIRTUAL_RANGE:
2130 case Instruction::INVOKE_SUPER:
Ian Rogersd81871c2011-10-03 13:57:23 -07002131 case Instruction::INVOKE_SUPER_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002132 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE ||
2133 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
2134 bool is_super = (inst->Opcode() == Instruction::INVOKE_SUPER ||
2135 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
Brian Carlstromea46f952013-07-30 01:26:50 -07002136 mirror::ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_VIRTUAL,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002137 is_range, is_super);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002138 const RegType* return_type = nullptr;
2139 if (called_method != nullptr) {
2140 MethodHelper mh(called_method);
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08002141 mirror::Class* return_type_class = mh.GetReturnType(can_load_classes_);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002142 if (return_type_class != nullptr) {
2143 return_type = &reg_types_.FromClass(mh.GetReturnTypeDescriptor(), return_type_class,
2144 return_type_class->CannotBeAssignedFromOtherTypes());
2145 } else {
2146 Thread* self = Thread::Current();
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08002147 DCHECK(!can_load_classes_ || self->IsExceptionPending());
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002148 self->ClearException();
2149 }
2150 }
2151 if (return_type == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002152 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002153 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2154 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002155 const char* descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07002156 return_type = &reg_types_.FromDescriptor(class_loader_->Get(), descriptor, false);
jeffhaobdb76512011-09-07 11:43:16 -07002157 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002158 if (!return_type->IsLowHalf()) {
2159 work_line_->SetResultRegisterType(*return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002160 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002161 work_line_->SetResultRegisterTypeWide(*return_type, return_type->HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002162 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002163 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002164 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002165 }
jeffhaobdb76512011-09-07 11:43:16 -07002166 case Instruction::INVOKE_DIRECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002167 case Instruction::INVOKE_DIRECT_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002168 bool is_range = (inst->Opcode() == Instruction::INVOKE_DIRECT_RANGE);
Brian Carlstromea46f952013-07-30 01:26:50 -07002169 mirror::ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_DIRECT,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002170 is_range, false);
Ian Rogers46685432012-06-03 22:26:43 -07002171 const char* return_type_descriptor;
2172 bool is_constructor;
2173 if (called_method == NULL) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002174 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogers46685432012-06-03 22:26:43 -07002175 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
Ian Rogersdfb325e2013-10-30 01:00:44 -07002176 is_constructor = strcmp("<init>", dex_file_->StringDataByIdx(method_id.name_idx_)) == 0;
Ian Rogers46685432012-06-03 22:26:43 -07002177 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
2178 return_type_descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2179 } else {
2180 is_constructor = called_method->IsConstructor();
2181 return_type_descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
2182 }
2183 if (is_constructor) {
jeffhaobdb76512011-09-07 11:43:16 -07002184 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002185 * Some additional checks when calling a constructor. We know from the invocation arg check
2186 * that the "this" argument is an instance of called_method->klass. Now we further restrict
2187 * that to require that called_method->klass is the same as this->klass or this->super,
2188 * allowing the latter only if the "this" argument is the same as the "this" argument to
2189 * this method (which implies that we're in a constructor ourselves).
jeffhaobdb76512011-09-07 11:43:16 -07002190 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002191 const RegType& this_type = work_line_->GetInvocationThis(inst, is_range);
jeffhaob57e9522012-04-26 18:08:21 -07002192 if (this_type.IsConflict()) // failure.
2193 break;
jeffhaobdb76512011-09-07 11:43:16 -07002194
jeffhaob57e9522012-04-26 18:08:21 -07002195 /* no null refs allowed (?) */
2196 if (this_type.IsZero()) {
2197 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unable to initialize null ref";
2198 break;
jeffhao2a8a90e2011-09-26 14:25:31 -07002199 }
jeffhaob57e9522012-04-26 18:08:21 -07002200
2201 /* must be in same class or in superclass */
Ian Rogers46685432012-06-03 22:26:43 -07002202 // const RegType& this_super_klass = this_type.GetSuperClass(&reg_types_);
2203 // TODO: re-enable constructor type verification
2204 // if (this_super_klass.IsConflict()) {
jeffhaob57e9522012-04-26 18:08:21 -07002205 // Unknown super class, fail so we re-check at runtime.
Ian Rogers46685432012-06-03 22:26:43 -07002206 // Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "super class unknown for '" << this_type << "'";
2207 // break;
2208 // }
jeffhaob57e9522012-04-26 18:08:21 -07002209
2210 /* arg must be an uninitialized reference */
2211 if (!this_type.IsUninitializedTypes()) {
2212 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Expected initialization on uninitialized reference "
2213 << this_type;
2214 break;
2215 }
2216
2217 /*
2218 * Replace the uninitialized reference with an initialized one. We need to do this for all
2219 * registers that have the same object instance in them, not just the "this" register.
2220 */
2221 work_line_->MarkRefsAsInitialized(this_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002222 }
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07002223 const RegType& return_type = reg_types_.FromDescriptor(class_loader_->Get(),
Mathieu Chartier590fee92013-09-13 13:46:47 -07002224 return_type_descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002225 if (!return_type.IsLowHalf()) {
2226 work_line_->SetResultRegisterType(return_type);
2227 } else {
2228 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2229 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002230 just_set_result = true;
2231 break;
2232 }
2233 case Instruction::INVOKE_STATIC:
2234 case Instruction::INVOKE_STATIC_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002235 bool is_range = (inst->Opcode() == Instruction::INVOKE_STATIC_RANGE);
Brian Carlstromea46f952013-07-30 01:26:50 -07002236 mirror::ArtMethod* called_method = VerifyInvocationArgs(inst,
Brian Carlstrom93c33962013-07-26 10:37:43 -07002237 METHOD_STATIC,
2238 is_range,
2239 false);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002240 const char* descriptor;
2241 if (called_method == NULL) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002242 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002243 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2244 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Mathieu Chartier590fee92013-09-13 13:46:47 -07002245 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002246 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002247 descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002248 }
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07002249 const RegType& return_type = reg_types_.FromDescriptor(class_loader_->Get(), descriptor,
Mathieu Chartier590fee92013-09-13 13:46:47 -07002250 false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002251 if (!return_type.IsLowHalf()) {
2252 work_line_->SetResultRegisterType(return_type);
2253 } else {
2254 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2255 }
jeffhaobdb76512011-09-07 11:43:16 -07002256 just_set_result = true;
2257 }
2258 break;
jeffhaobdb76512011-09-07 11:43:16 -07002259 case Instruction::INVOKE_INTERFACE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002260 case Instruction::INVOKE_INTERFACE_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002261 bool is_range = (inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE);
Brian Carlstromea46f952013-07-30 01:26:50 -07002262 mirror::ArtMethod* abs_method = VerifyInvocationArgs(inst,
Brian Carlstrom93c33962013-07-26 10:37:43 -07002263 METHOD_INTERFACE,
2264 is_range,
2265 false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002266 if (abs_method != NULL) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002267 mirror::Class* called_interface = abs_method->GetDeclaringClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002268 if (!called_interface->IsInterface() && !called_interface->IsObjectClass()) {
2269 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected interface class in invoke-interface '"
2270 << PrettyMethod(abs_method) << "'";
2271 break;
Ian Rogers28ad40d2011-10-27 15:19:26 -07002272 }
Ian Rogers0d604842012-04-16 14:50:24 -07002273 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002274 /* Get the type of the "this" arg, which should either be a sub-interface of called
2275 * interface or Object (see comments in RegType::JoinClass).
2276 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002277 const RegType& this_type = work_line_->GetInvocationThis(inst, is_range);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002278 if (this_type.IsZero()) {
2279 /* null pointer always passes (and always fails at runtime) */
2280 } else {
2281 if (this_type.IsUninitializedTypes()) {
2282 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface call on uninitialized object "
2283 << this_type;
2284 break;
2285 }
2286 // In the past we have tried to assert that "called_interface" is assignable
2287 // from "this_type.GetClass()", however, as we do an imprecise Join
2288 // (RegType::JoinClass) we don't have full information on what interfaces are
2289 // implemented by "this_type". For example, two classes may implement the same
2290 // interfaces and have a common parent that doesn't implement the interface. The
2291 // join will set "this_type" to the parent class and a test that this implements
2292 // the interface will incorrectly fail.
2293 }
2294 /*
2295 * We don't have an object instance, so we can't find the concrete method. However, all of
2296 * the type information is in the abstract method, so we're good.
2297 */
2298 const char* descriptor;
2299 if (abs_method == NULL) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002300 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002301 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2302 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
2303 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2304 } else {
2305 descriptor = MethodHelper(abs_method).GetReturnTypeDescriptor();
2306 }
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07002307 const RegType& return_type = reg_types_.FromDescriptor(class_loader_->Get(), descriptor,
Mathieu Chartier590fee92013-09-13 13:46:47 -07002308 false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002309 if (!return_type.IsLowHalf()) {
2310 work_line_->SetResultRegisterType(return_type);
2311 } else {
2312 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2313 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002314 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002315 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002316 }
jeffhaobdb76512011-09-07 11:43:16 -07002317 case Instruction::NEG_INT:
2318 case Instruction::NOT_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002319 work_line_->CheckUnaryOp(inst, reg_types_.Integer(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002320 break;
2321 case Instruction::NEG_LONG:
2322 case Instruction::NOT_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002323 work_line_->CheckUnaryOpWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002324 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002325 break;
2326 case Instruction::NEG_FLOAT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002327 work_line_->CheckUnaryOp(inst, reg_types_.Float(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002328 break;
2329 case Instruction::NEG_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002330 work_line_->CheckUnaryOpWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002331 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002332 break;
2333 case Instruction::INT_TO_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002334 work_line_->CheckUnaryOpToWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002335 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002336 break;
2337 case Instruction::INT_TO_FLOAT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002338 work_line_->CheckUnaryOp(inst, reg_types_.Float(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002339 break;
2340 case Instruction::INT_TO_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002341 work_line_->CheckUnaryOpToWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002342 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002343 break;
2344 case Instruction::LONG_TO_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002345 work_line_->CheckUnaryOpFromWide(inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002346 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002347 break;
2348 case Instruction::LONG_TO_FLOAT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002349 work_line_->CheckUnaryOpFromWide(inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002350 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002351 break;
2352 case Instruction::LONG_TO_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002353 work_line_->CheckUnaryOpWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002354 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002355 break;
2356 case Instruction::FLOAT_TO_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002357 work_line_->CheckUnaryOp(inst, reg_types_.Integer(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002358 break;
2359 case Instruction::FLOAT_TO_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002360 work_line_->CheckUnaryOpToWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002361 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002362 break;
2363 case Instruction::FLOAT_TO_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002364 work_line_->CheckUnaryOpToWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002365 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002366 break;
2367 case Instruction::DOUBLE_TO_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002368 work_line_->CheckUnaryOpFromWide(inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002369 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002370 break;
2371 case Instruction::DOUBLE_TO_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002372 work_line_->CheckUnaryOpWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002373 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002374 break;
2375 case Instruction::DOUBLE_TO_FLOAT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002376 work_line_->CheckUnaryOpFromWide(inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002377 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002378 break;
2379 case Instruction::INT_TO_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002380 work_line_->CheckUnaryOp(inst, reg_types_.Byte(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002381 break;
2382 case Instruction::INT_TO_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002383 work_line_->CheckUnaryOp(inst, reg_types_.Char(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002384 break;
2385 case Instruction::INT_TO_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002386 work_line_->CheckUnaryOp(inst, reg_types_.Short(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002387 break;
2388
2389 case Instruction::ADD_INT:
2390 case Instruction::SUB_INT:
2391 case Instruction::MUL_INT:
2392 case Instruction::REM_INT:
2393 case Instruction::DIV_INT:
2394 case Instruction::SHL_INT:
2395 case Instruction::SHR_INT:
2396 case Instruction::USHR_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002397 work_line_->CheckBinaryOp(inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002398 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002399 break;
2400 case Instruction::AND_INT:
2401 case Instruction::OR_INT:
2402 case Instruction::XOR_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002403 work_line_->CheckBinaryOp(inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002404 reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002405 break;
2406 case Instruction::ADD_LONG:
2407 case Instruction::SUB_LONG:
2408 case Instruction::MUL_LONG:
2409 case Instruction::DIV_LONG:
2410 case Instruction::REM_LONG:
2411 case Instruction::AND_LONG:
2412 case Instruction::OR_LONG:
2413 case Instruction::XOR_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002414 work_line_->CheckBinaryOpWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002415 reg_types_.LongLo(), reg_types_.LongHi(),
2416 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002417 break;
2418 case Instruction::SHL_LONG:
2419 case Instruction::SHR_LONG:
2420 case Instruction::USHR_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002421 /* shift distance is Int, making these different from other binary operations */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002422 work_line_->CheckBinaryOpWideShift(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002423 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002424 break;
2425 case Instruction::ADD_FLOAT:
2426 case Instruction::SUB_FLOAT:
2427 case Instruction::MUL_FLOAT:
2428 case Instruction::DIV_FLOAT:
2429 case Instruction::REM_FLOAT:
Brian Carlstrom93c33962013-07-26 10:37:43 -07002430 work_line_->CheckBinaryOp(inst,
2431 reg_types_.Float(),
2432 reg_types_.Float(),
2433 reg_types_.Float(),
2434 false);
jeffhaobdb76512011-09-07 11:43:16 -07002435 break;
2436 case Instruction::ADD_DOUBLE:
2437 case Instruction::SUB_DOUBLE:
2438 case Instruction::MUL_DOUBLE:
2439 case Instruction::DIV_DOUBLE:
2440 case Instruction::REM_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002441 work_line_->CheckBinaryOpWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002442 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2443 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002444 break;
2445 case Instruction::ADD_INT_2ADDR:
2446 case Instruction::SUB_INT_2ADDR:
2447 case Instruction::MUL_INT_2ADDR:
2448 case Instruction::REM_INT_2ADDR:
2449 case Instruction::SHL_INT_2ADDR:
2450 case Instruction::SHR_INT_2ADDR:
2451 case Instruction::USHR_INT_2ADDR:
Brian Carlstrom93c33962013-07-26 10:37:43 -07002452 work_line_->CheckBinaryOp2addr(inst,
2453 reg_types_.Integer(),
2454 reg_types_.Integer(),
2455 reg_types_.Integer(),
2456 false);
jeffhaobdb76512011-09-07 11:43:16 -07002457 break;
2458 case Instruction::AND_INT_2ADDR:
2459 case Instruction::OR_INT_2ADDR:
2460 case Instruction::XOR_INT_2ADDR:
Brian Carlstrom93c33962013-07-26 10:37:43 -07002461 work_line_->CheckBinaryOp2addr(inst,
2462 reg_types_.Integer(),
2463 reg_types_.Integer(),
2464 reg_types_.Integer(),
2465 true);
jeffhaobdb76512011-09-07 11:43:16 -07002466 break;
2467 case Instruction::DIV_INT_2ADDR:
Brian Carlstrom93c33962013-07-26 10:37:43 -07002468 work_line_->CheckBinaryOp2addr(inst,
2469 reg_types_.Integer(),
2470 reg_types_.Integer(),
2471 reg_types_.Integer(),
2472 false);
jeffhaobdb76512011-09-07 11:43:16 -07002473 break;
2474 case Instruction::ADD_LONG_2ADDR:
2475 case Instruction::SUB_LONG_2ADDR:
2476 case Instruction::MUL_LONG_2ADDR:
2477 case Instruction::DIV_LONG_2ADDR:
2478 case Instruction::REM_LONG_2ADDR:
2479 case Instruction::AND_LONG_2ADDR:
2480 case Instruction::OR_LONG_2ADDR:
2481 case Instruction::XOR_LONG_2ADDR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002482 work_line_->CheckBinaryOp2addrWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002483 reg_types_.LongLo(), reg_types_.LongHi(),
2484 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002485 break;
2486 case Instruction::SHL_LONG_2ADDR:
2487 case Instruction::SHR_LONG_2ADDR:
2488 case Instruction::USHR_LONG_2ADDR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002489 work_line_->CheckBinaryOp2addrWideShift(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002490 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002491 break;
2492 case Instruction::ADD_FLOAT_2ADDR:
2493 case Instruction::SUB_FLOAT_2ADDR:
2494 case Instruction::MUL_FLOAT_2ADDR:
2495 case Instruction::DIV_FLOAT_2ADDR:
2496 case Instruction::REM_FLOAT_2ADDR:
Brian Carlstrom93c33962013-07-26 10:37:43 -07002497 work_line_->CheckBinaryOp2addr(inst,
2498 reg_types_.Float(),
2499 reg_types_.Float(),
2500 reg_types_.Float(),
2501 false);
jeffhaobdb76512011-09-07 11:43:16 -07002502 break;
2503 case Instruction::ADD_DOUBLE_2ADDR:
2504 case Instruction::SUB_DOUBLE_2ADDR:
2505 case Instruction::MUL_DOUBLE_2ADDR:
2506 case Instruction::DIV_DOUBLE_2ADDR:
2507 case Instruction::REM_DOUBLE_2ADDR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002508 work_line_->CheckBinaryOp2addrWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002509 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2510 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002511 break;
2512 case Instruction::ADD_INT_LIT16:
2513 case Instruction::RSUB_INT:
2514 case Instruction::MUL_INT_LIT16:
2515 case Instruction::DIV_INT_LIT16:
2516 case Instruction::REM_INT_LIT16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002517 work_line_->CheckLiteralOp(inst, reg_types_.Integer(), reg_types_.Integer(), false, true);
jeffhaobdb76512011-09-07 11:43:16 -07002518 break;
2519 case Instruction::AND_INT_LIT16:
2520 case Instruction::OR_INT_LIT16:
2521 case Instruction::XOR_INT_LIT16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002522 work_line_->CheckLiteralOp(inst, reg_types_.Integer(), reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002523 break;
2524 case Instruction::ADD_INT_LIT8:
2525 case Instruction::RSUB_INT_LIT8:
2526 case Instruction::MUL_INT_LIT8:
2527 case Instruction::DIV_INT_LIT8:
2528 case Instruction::REM_INT_LIT8:
2529 case Instruction::SHL_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002530 case Instruction::SHR_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002531 case Instruction::USHR_INT_LIT8:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002532 work_line_->CheckLiteralOp(inst, reg_types_.Integer(), reg_types_.Integer(), false, false);
jeffhaobdb76512011-09-07 11:43:16 -07002533 break;
2534 case Instruction::AND_INT_LIT8:
2535 case Instruction::OR_INT_LIT8:
2536 case Instruction::XOR_INT_LIT8:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002537 work_line_->CheckLiteralOp(inst, reg_types_.Integer(), reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002538 break;
2539
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002540 // Special instructions.
Sebastien Hertzcc10e0e2013-06-28 14:24:48 +02002541 case Instruction::RETURN_VOID_BARRIER:
Ian Rogers9fc16eb2013-07-31 14:49:16 -07002542 if (!IsConstructor() || IsStatic()) {
Sebastien Hertzcc10e0e2013-06-28 14:24:48 +02002543 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void-barrier not expected";
2544 }
2545 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002546 // Note: the following instructions encode offsets derived from class linking.
2547 // As such they use Class*/Field*/AbstractMethod* as these offsets only have
2548 // meaning if the class linking and resolution were successful.
2549 case Instruction::IGET_QUICK:
2550 VerifyIGetQuick(inst, reg_types_.Integer(), true);
2551 break;
2552 case Instruction::IGET_WIDE_QUICK:
2553 VerifyIGetQuick(inst, reg_types_.LongLo(), true);
2554 break;
2555 case Instruction::IGET_OBJECT_QUICK:
2556 VerifyIGetQuick(inst, reg_types_.JavaLangObject(false), false);
2557 break;
2558 case Instruction::IPUT_QUICK:
2559 VerifyIPutQuick(inst, reg_types_.Integer(), true);
2560 break;
2561 case Instruction::IPUT_WIDE_QUICK:
2562 VerifyIPutQuick(inst, reg_types_.LongLo(), true);
2563 break;
2564 case Instruction::IPUT_OBJECT_QUICK:
2565 VerifyIPutQuick(inst, reg_types_.JavaLangObject(false), false);
2566 break;
2567 case Instruction::INVOKE_VIRTUAL_QUICK:
2568 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: {
2569 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
Brian Carlstromea46f952013-07-30 01:26:50 -07002570 mirror::ArtMethod* called_method = VerifyInvokeVirtualQuickArgs(inst, is_range);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002571 if (called_method != NULL) {
2572 const char* descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07002573 const RegType& return_type = reg_types_.FromDescriptor(class_loader_->Get(), descriptor,
Mathieu Chartier590fee92013-09-13 13:46:47 -07002574 false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002575 if (!return_type.IsLowHalf()) {
2576 work_line_->SetResultRegisterType(return_type);
2577 } else {
2578 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2579 }
2580 just_set_result = true;
2581 }
2582 break;
2583 }
2584
Ian Rogersd81871c2011-10-03 13:57:23 -07002585 /* These should never appear during verification. */
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002586 case Instruction::UNUSED_3E:
2587 case Instruction::UNUSED_3F:
2588 case Instruction::UNUSED_40:
2589 case Instruction::UNUSED_41:
2590 case Instruction::UNUSED_42:
2591 case Instruction::UNUSED_43:
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002592 case Instruction::UNUSED_79:
2593 case Instruction::UNUSED_7A:
2594 case Instruction::UNUSED_EB:
2595 case Instruction::UNUSED_EC:
jeffhao9a4f0032012-08-30 16:17:40 -07002596 case Instruction::UNUSED_ED:
jeffhaobdb76512011-09-07 11:43:16 -07002597 case Instruction::UNUSED_EE:
2598 case Instruction::UNUSED_EF:
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002599 case Instruction::UNUSED_F0:
2600 case Instruction::UNUSED_F1:
jeffhaobdb76512011-09-07 11:43:16 -07002601 case Instruction::UNUSED_F2:
2602 case Instruction::UNUSED_F3:
2603 case Instruction::UNUSED_F4:
2604 case Instruction::UNUSED_F5:
2605 case Instruction::UNUSED_F6:
2606 case Instruction::UNUSED_F7:
2607 case Instruction::UNUSED_F8:
2608 case Instruction::UNUSED_F9:
2609 case Instruction::UNUSED_FA:
2610 case Instruction::UNUSED_FB:
jeffhaobdb76512011-09-07 11:43:16 -07002611 case Instruction::UNUSED_FC:
jeffhaobdb76512011-09-07 11:43:16 -07002612 case Instruction::UNUSED_FD:
jeffhaobdb76512011-09-07 11:43:16 -07002613 case Instruction::UNUSED_FE:
jeffhaobdb76512011-09-07 11:43:16 -07002614 case Instruction::UNUSED_FF:
jeffhaod5347e02012-03-22 17:25:05 -07002615 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Unexpected opcode " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07002616 break;
2617
2618 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002619 * DO NOT add a "default" clause here. Without it the compiler will
jeffhaobdb76512011-09-07 11:43:16 -07002620 * complain if an instruction is missing (which is desirable).
2621 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002622 } // end - switch (dec_insn.opcode)
jeffhaobdb76512011-09-07 11:43:16 -07002623
Ian Rogersad0b3a32012-04-16 14:50:24 -07002624 if (have_pending_hard_failure_) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002625 if (Runtime::Current()->IsCompiler()) {
jeffhaob57e9522012-04-26 18:08:21 -07002626 /* When compiling, check that the last failure is a hard failure */
Ian Rogersad0b3a32012-04-16 14:50:24 -07002627 CHECK_EQ(failures_[failures_.size() - 1], VERIFY_ERROR_BAD_CLASS_HARD);
Ian Rogerse1758fe2012-04-19 11:31:15 -07002628 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002629 /* immediate failure, reject class */
2630 info_messages_ << "Rejecting opcode " << inst->DumpString(dex_file_);
2631 return false;
jeffhaofaf459e2012-08-31 15:32:47 -07002632 } else if (have_pending_runtime_throw_failure_) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07002633 /* checking interpreter will throw, mark following code as unreachable */
jeffhaofaf459e2012-08-31 15:32:47 -07002634 opcode_flags = Instruction::kThrow;
jeffhaobdb76512011-09-07 11:43:16 -07002635 }
jeffhaobdb76512011-09-07 11:43:16 -07002636 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002637 * If we didn't just set the result register, clear it out. This ensures that you can only use
2638 * "move-result" immediately after the result is set. (We could check this statically, but it's
2639 * not expensive and it makes our debugging output cleaner.)
jeffhaobdb76512011-09-07 11:43:16 -07002640 */
2641 if (!just_set_result) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002642 work_line_->SetResultTypeToUnknown();
jeffhaobdb76512011-09-07 11:43:16 -07002643 }
2644
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002645
jeffhaobdb76512011-09-07 11:43:16 -07002646
2647 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002648 * Handle "branch". Tag the branch target.
jeffhaobdb76512011-09-07 11:43:16 -07002649 *
2650 * NOTE: instructions like Instruction::EQZ provide information about the
jeffhaod1f0fde2011-09-08 17:25:33 -07002651 * state of the register when the branch is taken or not taken. For example,
jeffhaobdb76512011-09-07 11:43:16 -07002652 * somebody could get a reference field, check it for zero, and if the
2653 * branch is taken immediately store that register in a boolean field
jeffhaod1f0fde2011-09-08 17:25:33 -07002654 * since the value is known to be zero. We do not currently account for
jeffhaobdb76512011-09-07 11:43:16 -07002655 * that, and will reject the code.
2656 *
2657 * TODO: avoid re-fetching the branch target
2658 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002659 if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002660 bool isConditional, selfOkay;
Ian Rogersd81871c2011-10-03 13:57:23 -07002661 if (!GetBranchOffset(work_insn_idx_, &branch_target, &isConditional, &selfOkay)) {
jeffhaobdb76512011-09-07 11:43:16 -07002662 /* should never happen after static verification */
jeffhaod5347e02012-03-22 17:25:05 -07002663 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad branch";
jeffhaobdb76512011-09-07 11:43:16 -07002664 return false;
2665 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08002666 DCHECK_EQ(isConditional, (opcode_flags & Instruction::kContinue) != 0);
jeffhaod5347e02012-03-22 17:25:05 -07002667 if (!CheckNotMoveException(code_item_->insns_, work_insn_idx_ + branch_target)) {
jeffhaobdb76512011-09-07 11:43:16 -07002668 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002669 }
jeffhaobdb76512011-09-07 11:43:16 -07002670 /* update branch target, set "changed" if appropriate */
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002671 if (NULL != branch_line.get()) {
2672 if (!UpdateRegisters(work_insn_idx_ + branch_target, branch_line.get())) {
2673 return false;
2674 }
2675 } else {
2676 if (!UpdateRegisters(work_insn_idx_ + branch_target, work_line_.get())) {
2677 return false;
2678 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002679 }
jeffhaobdb76512011-09-07 11:43:16 -07002680 }
2681
2682 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002683 * Handle "switch". Tag all possible branch targets.
jeffhaobdb76512011-09-07 11:43:16 -07002684 *
2685 * We've already verified that the table is structurally sound, so we
2686 * just need to walk through and tag the targets.
2687 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002688 if ((opcode_flags & Instruction::kSwitch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002689 int offset_to_switch = insns[1] | (((int32_t) insns[2]) << 16);
2690 const uint16_t* switch_insns = insns + offset_to_switch;
2691 int switch_count = switch_insns[1];
2692 int offset_to_targets, targ;
2693
2694 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
2695 /* 0 = sig, 1 = count, 2/3 = first key */
2696 offset_to_targets = 4;
2697 } else {
2698 /* 0 = sig, 1 = count, 2..count * 2 = keys */
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07002699 DCHECK((*insns & 0xff) == Instruction::SPARSE_SWITCH);
jeffhaobdb76512011-09-07 11:43:16 -07002700 offset_to_targets = 2 + 2 * switch_count;
2701 }
2702
2703 /* verify each switch target */
2704 for (targ = 0; targ < switch_count; targ++) {
2705 int offset;
2706 uint32_t abs_offset;
2707
2708 /* offsets are 32-bit, and only partly endian-swapped */
2709 offset = switch_insns[offset_to_targets + targ * 2] |
2710 (((int32_t) switch_insns[offset_to_targets + targ * 2 + 1]) << 16);
Ian Rogersd81871c2011-10-03 13:57:23 -07002711 abs_offset = work_insn_idx_ + offset;
2712 DCHECK_LT(abs_offset, code_item_->insns_size_in_code_units_);
jeffhaod5347e02012-03-22 17:25:05 -07002713 if (!CheckNotMoveException(code_item_->insns_, abs_offset)) {
jeffhaobdb76512011-09-07 11:43:16 -07002714 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002715 }
2716 if (!UpdateRegisters(abs_offset, work_line_.get()))
jeffhaobdb76512011-09-07 11:43:16 -07002717 return false;
2718 }
2719 }
2720
2721 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002722 * Handle instructions that can throw and that are sitting in a "try" block. (If they're not in a
2723 * "try" block when they throw, control transfers out of the method.)
jeffhaobdb76512011-09-07 11:43:16 -07002724 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002725 if ((opcode_flags & Instruction::kThrow) != 0 && insn_flags_[work_insn_idx_].IsInTry()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002726 bool within_catch_all = false;
Ian Rogers0571d352011-11-03 19:51:38 -07002727 CatchHandlerIterator iterator(*code_item_, work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07002728
Ian Rogers0571d352011-11-03 19:51:38 -07002729 for (; iterator.HasNext(); iterator.Next()) {
2730 if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002731 within_catch_all = true;
2732 }
jeffhaobdb76512011-09-07 11:43:16 -07002733 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002734 * Merge registers into the "catch" block. We want to use the "savedRegs" rather than
2735 * "work_regs", because at runtime the exception will be thrown before the instruction
2736 * modifies any registers.
jeffhaobdb76512011-09-07 11:43:16 -07002737 */
Ian Rogers0571d352011-11-03 19:51:38 -07002738 if (!UpdateRegisters(iterator.GetHandlerAddress(), saved_line_.get())) {
jeffhaobdb76512011-09-07 11:43:16 -07002739 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002740 }
jeffhaobdb76512011-09-07 11:43:16 -07002741 }
2742
2743 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002744 * If the monitor stack depth is nonzero, there must be a "catch all" handler for this
2745 * instruction. This does apply to monitor-exit because of async exception handling.
jeffhaobdb76512011-09-07 11:43:16 -07002746 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002747 if (work_line_->MonitorStackDepth() > 0 && !within_catch_all) {
jeffhaobdb76512011-09-07 11:43:16 -07002748 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002749 * The state in work_line reflects the post-execution state. If the current instruction is a
2750 * monitor-enter and the monitor stack was empty, we don't need a catch-all (if it throws,
jeffhaobdb76512011-09-07 11:43:16 -07002751 * it will do so before grabbing the lock).
2752 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002753 if (inst->Opcode() != Instruction::MONITOR_ENTER || work_line_->MonitorStackDepth() != 1) {
jeffhaod5347e02012-03-22 17:25:05 -07002754 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
Ian Rogersd81871c2011-10-03 13:57:23 -07002755 << "expected to be within a catch-all for an instruction where a monitor is held";
jeffhaobdb76512011-09-07 11:43:16 -07002756 return false;
2757 }
2758 }
2759 }
2760
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002761 /* Handle "continue". Tag the next consecutive instruction.
2762 * Note: Keep the code handling "continue" case below the "branch" and "switch" cases,
2763 * because it changes work_line_ when performing peephole optimization
2764 * and this change should not be used in those cases.
2765 */
Ian Rogers6d376ae2013-07-23 15:12:40 -07002766 if ((opcode_flags & Instruction::kContinue) != 0) {
2767 uint32_t next_insn_idx = work_insn_idx_ + CurrentInsnFlags()->GetLengthInCodeUnits();
2768 if (next_insn_idx >= code_item_->insns_size_in_code_units_) {
2769 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Execution can walk off end of code area";
2770 return false;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002771 }
Ian Rogers6d376ae2013-07-23 15:12:40 -07002772 // The only way to get to a move-exception instruction is to get thrown there. Make sure the
2773 // next instruction isn't one.
2774 if (!CheckNotMoveException(code_item_->insns_, next_insn_idx)) {
2775 return false;
2776 }
2777 if (NULL != fallthrough_line.get()) {
2778 // Make workline consistent with fallthrough computed from peephole optimization.
2779 work_line_->CopyFromLine(fallthrough_line.get());
2780 }
Ian Rogersb8c78592013-07-25 23:52:52 +00002781 if (insn_flags_[next_insn_idx].IsReturn()) {
2782 // For returns we only care about the operand to the return, all other registers are dead.
2783 const Instruction* ret_inst = Instruction::At(code_item_->insns_ + next_insn_idx);
2784 Instruction::Code opcode = ret_inst->Opcode();
2785 if ((opcode == Instruction::RETURN_VOID) || (opcode == Instruction::RETURN_VOID_BARRIER)) {
2786 work_line_->MarkAllRegistersAsConflicts();
2787 } else {
2788 if (opcode == Instruction::RETURN_WIDE) {
2789 work_line_->MarkAllRegistersAsConflictsExceptWide(ret_inst->VRegA_11x());
2790 } else {
2791 work_line_->MarkAllRegistersAsConflictsExcept(ret_inst->VRegA_11x());
2792 }
2793 }
2794 }
Ian Rogers6d376ae2013-07-23 15:12:40 -07002795 RegisterLine* next_line = reg_table_.GetLine(next_insn_idx);
2796 if (next_line != NULL) {
2797 // Merge registers into what we have for the next instruction,
2798 // and set the "changed" flag if needed.
2799 if (!UpdateRegisters(next_insn_idx, work_line_.get())) {
2800 return false;
2801 }
2802 } else {
2803 /*
2804 * We're not recording register data for the next instruction, so we don't know what the
2805 * prior state was. We have to assume that something has changed and re-evaluate it.
2806 */
2807 insn_flags_[next_insn_idx].SetChanged();
2808 }
2809 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002810
jeffhaod1f0fde2011-09-08 17:25:33 -07002811 /* If we're returning from the method, make sure monitor stack is empty. */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002812 if ((opcode_flags & Instruction::kReturn) != 0) {
Elliott Hughesb25c3f62012-03-26 16:35:06 -07002813 if (!work_line_->VerifyMonitorStackEmpty()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002814 return false;
2815 }
jeffhaobdb76512011-09-07 11:43:16 -07002816 }
2817
2818 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002819 * Update start_guess. Advance to the next instruction of that's
2820 * possible, otherwise use the branch target if one was found. If
jeffhaobdb76512011-09-07 11:43:16 -07002821 * neither of those exists we're in a return or throw; leave start_guess
2822 * alone and let the caller sort it out.
2823 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002824 if ((opcode_flags & Instruction::kContinue) != 0) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002825 *start_guess = work_insn_idx_ + insn_flags_[work_insn_idx_].GetLengthInCodeUnits();
Elliott Hughesadb8c672012-03-06 16:49:32 -08002826 } else if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002827 /* we're still okay if branch_target is zero */
Ian Rogersd81871c2011-10-03 13:57:23 -07002828 *start_guess = work_insn_idx_ + branch_target;
jeffhaobdb76512011-09-07 11:43:16 -07002829 }
2830
Ian Rogersd81871c2011-10-03 13:57:23 -07002831 DCHECK_LT(*start_guess, code_item_->insns_size_in_code_units_);
2832 DCHECK(insn_flags_[*start_guess].IsOpcode());
jeffhaobdb76512011-09-07 11:43:16 -07002833
2834 return true;
Brian Carlstrom7934ac22013-07-26 10:54:15 -07002835} // NOLINT(readability/fn_size)
jeffhaobdb76512011-09-07 11:43:16 -07002836
Ian Rogers776ac1f2012-04-13 23:36:36 -07002837const RegType& MethodVerifier::ResolveClassAndCheckAccess(uint32_t class_idx) {
Ian Rogers0571d352011-11-03 19:51:38 -07002838 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002839 const RegType& referrer = GetDeclaringClass();
Mathieu Chartier590fee92013-09-13 13:46:47 -07002840 mirror::Class* klass = (*dex_cache_)->GetResolvedType(class_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002841 const RegType& result =
Ian Rogers04f94f42013-06-10 15:09:26 -07002842 klass != NULL ? reg_types_.FromClass(descriptor, klass,
2843 klass->CannotBeAssignedFromOtherTypes())
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07002844 : reg_types_.FromDescriptor(class_loader_->Get(), descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002845 if (result.IsConflict()) {
2846 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "accessing broken descriptor '" << descriptor
2847 << "' in " << referrer;
2848 return result;
2849 }
Ian Rogerse1758fe2012-04-19 11:31:15 -07002850 if (klass == NULL && !result.IsUnresolvedTypes()) {
Mathieu Chartier590fee92013-09-13 13:46:47 -07002851 (*dex_cache_)->SetResolvedType(class_idx, result.GetClass());
Ian Rogerse1758fe2012-04-19 11:31:15 -07002852 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002853 // Check if access is allowed. Unresolved types use xxxWithAccessCheck to
Jeff Haob24b4a72013-07-31 13:47:31 -07002854 // check at runtime if access is allowed and so pass here. If result is
2855 // primitive, skip the access check.
2856 if (result.IsNonZeroReferenceTypes() && !result.IsUnresolvedTypes() &&
2857 !referrer.IsUnresolvedTypes() && !referrer.CanAccess(result)) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002858 Fail(VERIFY_ERROR_ACCESS_CLASS) << "illegal class access: '"
Ian Rogersad0b3a32012-04-16 14:50:24 -07002859 << referrer << "' -> '" << result << "'";
Ian Rogers28ad40d2011-10-27 15:19:26 -07002860 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002861 return result;
Ian Rogersd81871c2011-10-03 13:57:23 -07002862}
2863
Ian Rogers776ac1f2012-04-13 23:36:36 -07002864const RegType& MethodVerifier::GetCaughtExceptionType() {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002865 const RegType* common_super = NULL;
Ian Rogersd81871c2011-10-03 13:57:23 -07002866 if (code_item_->tries_size_ != 0) {
Ian Rogers0571d352011-11-03 19:51:38 -07002867 const byte* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
Ian Rogersd81871c2011-10-03 13:57:23 -07002868 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
2869 for (uint32_t i = 0; i < handlers_size; i++) {
Ian Rogers0571d352011-11-03 19:51:38 -07002870 CatchHandlerIterator iterator(handlers_ptr);
2871 for (; iterator.HasNext(); iterator.Next()) {
2872 if (iterator.GetHandlerAddress() == (uint32_t) work_insn_idx_) {
2873 if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) {
Ian Rogersb4903572012-10-11 11:52:56 -07002874 common_super = &reg_types_.JavaLangThrowable(false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002875 } else {
Ian Rogers0571d352011-11-03 19:51:38 -07002876 const RegType& exception = ResolveClassAndCheckAccess(iterator.GetHandlerTypeIndex());
Jeff Haob878f212014-04-24 16:25:36 -07002877 if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(exception)) {
Jeff Haoc26a56c2013-11-04 12:00:47 -08002878 if (exception.IsUnresolvedTypes()) {
2879 // We don't know enough about the type. Fail here and let runtime handle it.
2880 Fail(VERIFY_ERROR_NO_CLASS) << "unresolved exception class " << exception;
2881 return exception;
2882 } else {
2883 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unexpected non-exception class " << exception;
2884 return reg_types_.Conflict();
2885 }
Jeff Haob878f212014-04-24 16:25:36 -07002886 } else if (common_super == nullptr) {
2887 common_super = &exception;
Ian Rogers28ad40d2011-10-27 15:19:26 -07002888 } else if (common_super->Equals(exception)) {
Ian Rogersc4762272012-02-01 15:55:55 -08002889 // odd case, but nothing to do
Ian Rogersd81871c2011-10-03 13:57:23 -07002890 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002891 common_super = &common_super->Merge(exception, &reg_types_);
Ian Rogersb4903572012-10-11 11:52:56 -07002892 CHECK(reg_types_.JavaLangThrowable(false).IsAssignableFrom(*common_super));
Ian Rogersd81871c2011-10-03 13:57:23 -07002893 }
2894 }
2895 }
2896 }
Ian Rogers0571d352011-11-03 19:51:38 -07002897 handlers_ptr = iterator.EndDataPointer();
Ian Rogersd81871c2011-10-03 13:57:23 -07002898 }
2899 }
2900 if (common_super == NULL) {
2901 /* no catch blocks, or no catches with classes we can find */
jeffhaod5347e02012-03-22 17:25:05 -07002902 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unable to find exception handler";
Ian Rogersad0b3a32012-04-16 14:50:24 -07002903 return reg_types_.Conflict();
Ian Rogersd81871c2011-10-03 13:57:23 -07002904 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002905 return *common_super;
Ian Rogersd81871c2011-10-03 13:57:23 -07002906}
2907
Brian Carlstromea46f952013-07-30 01:26:50 -07002908mirror::ArtMethod* MethodVerifier::ResolveMethodAndCheckAccess(uint32_t dex_method_idx,
Ian Rogersd91d6d62013-09-25 20:26:14 -07002909 MethodType method_type) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002910 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx);
Ian Rogers90040192011-12-16 08:54:29 -08002911 const RegType& klass_type = ResolveClassAndCheckAccess(method_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002912 if (klass_type.IsConflict()) {
2913 std::string append(" in attempt to access method ");
2914 append += dex_file_->GetMethodName(method_id);
2915 AppendToLastFailMessage(append);
Ian Rogers90040192011-12-16 08:54:29 -08002916 return NULL;
2917 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002918 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers90040192011-12-16 08:54:29 -08002919 return NULL; // Can't resolve Class so no more to do here
2920 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002921 mirror::Class* klass = klass_type.GetClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002922 const RegType& referrer = GetDeclaringClass();
Mathieu Chartier590fee92013-09-13 13:46:47 -07002923 mirror::ArtMethod* res_method = (*dex_cache_)->GetResolvedMethod(dex_method_idx);
Ian Rogersd81871c2011-10-03 13:57:23 -07002924 if (res_method == NULL) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07002925 const char* name = dex_file_->GetMethodName(method_id);
Ian Rogersd91d6d62013-09-25 20:26:14 -07002926 const Signature signature = dex_file_->GetMethodSignature(method_id);
jeffhao8cd6dda2012-02-22 10:15:34 -08002927
2928 if (method_type == METHOD_DIRECT || method_type == METHOD_STATIC) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002929 res_method = klass->FindDirectMethod(name, signature);
jeffhao8cd6dda2012-02-22 10:15:34 -08002930 } else if (method_type == METHOD_INTERFACE) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002931 res_method = klass->FindInterfaceMethod(name, signature);
2932 } else {
2933 res_method = klass->FindVirtualMethod(name, signature);
2934 }
2935 if (res_method != NULL) {
Mathieu Chartier590fee92013-09-13 13:46:47 -07002936 (*dex_cache_)->SetResolvedMethod(dex_method_idx, res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07002937 } else {
jeffhao8cd6dda2012-02-22 10:15:34 -08002938 // If a virtual or interface method wasn't found with the expected type, look in
2939 // the direct methods. This can happen when the wrong invoke type is used or when
2940 // a class has changed, and will be flagged as an error in later checks.
2941 if (method_type == METHOD_INTERFACE || method_type == METHOD_VIRTUAL) {
2942 res_method = klass->FindDirectMethod(name, signature);
2943 }
2944 if (res_method == NULL) {
2945 Fail(VERIFY_ERROR_NO_METHOD) << "couldn't find method "
2946 << PrettyDescriptor(klass) << "." << name
2947 << " " << signature;
2948 return NULL;
2949 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002950 }
2951 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002952 // Make sure calls to constructors are "direct". There are additional restrictions but we don't
2953 // enforce them here.
2954 if (res_method->IsConstructor() && method_type != METHOD_DIRECT) {
jeffhaod5347e02012-03-22 17:25:05 -07002955 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting non-direct call to constructor "
2956 << PrettyMethod(res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07002957 return NULL;
2958 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002959 // Disallow any calls to class initializers.
2960 if (MethodHelper(res_method).IsClassInitializer()) {
jeffhaod5347e02012-03-22 17:25:05 -07002961 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting call to class initializer "
2962 << PrettyMethod(res_method);
jeffhao8cd6dda2012-02-22 10:15:34 -08002963 return NULL;
2964 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002965 // Check if access is allowed.
Ian Rogersad0b3a32012-04-16 14:50:24 -07002966 if (!referrer.CanAccessMember(res_method->GetDeclaringClass(), res_method->GetAccessFlags())) {
jeffhao8cd6dda2012-02-22 10:15:34 -08002967 Fail(VERIFY_ERROR_ACCESS_METHOD) << "illegal method access (call " << PrettyMethod(res_method)
Ian Rogersad0b3a32012-04-16 14:50:24 -07002968 << " from " << referrer << ")";
jeffhaob57e9522012-04-26 18:08:21 -07002969 return res_method;
jeffhao8cd6dda2012-02-22 10:15:34 -08002970 }
jeffhaode0d9c92012-02-27 13:58:13 -08002971 // Check that invoke-virtual and invoke-super are not used on private methods of the same class.
2972 if (res_method->IsPrivate() && method_type == METHOD_VIRTUAL) {
jeffhaod5347e02012-03-22 17:25:05 -07002973 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invoke-super/virtual can't be used on private method "
2974 << PrettyMethod(res_method);
jeffhaode0d9c92012-02-27 13:58:13 -08002975 return NULL;
2976 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002977 // Check that interface methods match interface classes.
2978 if (klass->IsInterface() && method_type != METHOD_INTERFACE) {
2979 Fail(VERIFY_ERROR_CLASS_CHANGE) << "non-interface method " << PrettyMethod(res_method)
2980 << " is in an interface class " << PrettyClass(klass);
2981 return NULL;
2982 } else if (!klass->IsInterface() && method_type == METHOD_INTERFACE) {
2983 Fail(VERIFY_ERROR_CLASS_CHANGE) << "interface method " << PrettyMethod(res_method)
2984 << " is in a non-interface class " << PrettyClass(klass);
2985 return NULL;
2986 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002987 // See if the method type implied by the invoke instruction matches the access flags for the
2988 // target method.
2989 if ((method_type == METHOD_DIRECT && !res_method->IsDirect()) ||
2990 (method_type == METHOD_STATIC && !res_method->IsStatic()) ||
2991 ((method_type == METHOD_VIRTUAL || method_type == METHOD_INTERFACE) && res_method->IsDirect())
2992 ) {
Ian Rogers2fc14272012-08-30 10:56:57 -07002993 Fail(VERIFY_ERROR_CLASS_CHANGE) << "invoke type (" << method_type << ") does not match method "
2994 " type of " << PrettyMethod(res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07002995 return NULL;
2996 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002997 return res_method;
2998}
2999
Brian Carlstromea46f952013-07-30 01:26:50 -07003000mirror::ArtMethod* MethodVerifier::VerifyInvocationArgs(const Instruction* inst,
Sebastien Hertz5243e912013-05-21 10:55:07 +02003001 MethodType method_type,
3002 bool is_range,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003003 bool is_super) {
jeffhao8cd6dda2012-02-22 10:15:34 -08003004 // Resolve the method. This could be an abstract or concrete method depending on what sort of call
3005 // we're making.
Sebastien Hertz5243e912013-05-21 10:55:07 +02003006 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Brian Carlstromea46f952013-07-30 01:26:50 -07003007 mirror::ArtMethod* res_method = ResolveMethodAndCheckAccess(method_idx, method_type);
jeffhao8cd6dda2012-02-22 10:15:34 -08003008 if (res_method == NULL) { // error or class is unresolved
3009 return NULL;
3010 }
3011
Ian Rogersd81871c2011-10-03 13:57:23 -07003012 // If we're using invoke-super(method), make sure that the executing method's class' superclass
3013 // has a vtable entry for the target method.
3014 if (is_super) {
3015 DCHECK(method_type == METHOD_VIRTUAL);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003016 const RegType& super = GetDeclaringClass().GetSuperClass(&reg_types_);
Ian Rogers529781d2012-07-23 17:24:29 -07003017 if (super.IsUnresolvedTypes()) {
jeffhao4d8df822012-04-24 17:09:36 -07003018 Fail(VERIFY_ERROR_NO_METHOD) << "unknown super class in invoke-super from "
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003019 << PrettyMethod(dex_method_idx_, *dex_file_)
jeffhao4d8df822012-04-24 17:09:36 -07003020 << " to super " << PrettyMethod(res_method);
3021 return NULL;
3022 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003023 mirror::Class* super_klass = super.GetClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07003024 if (res_method->GetMethodIndex() >= super_klass->GetVTable()->GetLength()) {
jeffhao4d8df822012-04-24 17:09:36 -07003025 MethodHelper mh(res_method);
3026 Fail(VERIFY_ERROR_NO_METHOD) << "invalid invoke-super from "
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003027 << PrettyMethod(dex_method_idx_, *dex_file_)
jeffhao4d8df822012-04-24 17:09:36 -07003028 << " to super " << super
3029 << "." << mh.GetName()
3030 << mh.GetSignature();
Ian Rogersd81871c2011-10-03 13:57:23 -07003031 return NULL;
3032 }
3033 }
3034 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003035 // match the call to the signature. Also, we might be calling through an abstract method
Ian Rogersd81871c2011-10-03 13:57:23 -07003036 // definition (which doesn't have register count values).
Sebastien Hertz5243e912013-05-21 10:55:07 +02003037 const size_t expected_args = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
Ian Rogersd81871c2011-10-03 13:57:23 -07003038 /* caught by static verifier */
3039 DCHECK(is_range || expected_args <= 5);
3040 if (expected_args > code_item_->outs_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07003041 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
Ian Rogersd81871c2011-10-03 13:57:23 -07003042 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
3043 return NULL;
3044 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003045
jeffhaobdb76512011-09-07 11:43:16 -07003046 /*
Ian Rogersad0b3a32012-04-16 14:50:24 -07003047 * Check the "this" argument, which must be an instance of the class that declared the method.
3048 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
3049 * rigorous check here (which is okay since we have to do it at runtime).
jeffhaobdb76512011-09-07 11:43:16 -07003050 */
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003051 size_t actual_args = 0;
Ian Rogersd81871c2011-10-03 13:57:23 -07003052 if (!res_method->IsStatic()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003053 const RegType& actual_arg_type = work_line_->GetInvocationThis(inst, is_range);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003054 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
Ian Rogersd81871c2011-10-03 13:57:23 -07003055 return NULL;
3056 }
3057 if (actual_arg_type.IsUninitializedReference() && !res_method->IsConstructor()) {
jeffhaod5347e02012-03-22 17:25:05 -07003058 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
Ian Rogersd81871c2011-10-03 13:57:23 -07003059 return NULL;
3060 }
3061 if (method_type != METHOD_INTERFACE && !actual_arg_type.IsZero()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003062 mirror::Class* klass = res_method->GetDeclaringClass();
Ian Rogers04f94f42013-06-10 15:09:26 -07003063 const RegType& res_method_class =
Mathieu Chartierf8322842014-05-16 10:59:25 -07003064 reg_types_.FromClass(klass->GetDescriptor().c_str(), klass,
Ian Rogers04f94f42013-06-10 15:09:26 -07003065 klass->CannotBeAssignedFromOtherTypes());
Ian Rogers9074b992011-10-26 17:41:55 -07003066 if (!res_method_class.IsAssignableFrom(actual_arg_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07003067 Fail(actual_arg_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS:
3068 VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003069 << "' not instance of '" << res_method_class << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07003070 return NULL;
3071 }
3072 }
3073 actual_args++;
3074 }
3075 /*
3076 * Process the target method's signature. This signature may or may not
3077 * have been verified, so we can't assume it's properly formed.
3078 */
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003079 MethodHelper mh(res_method);
3080 const DexFile::TypeList* params = mh.GetParameterTypeList();
3081 size_t params_size = params == NULL ? 0 : params->Size();
Sebastien Hertz5243e912013-05-21 10:55:07 +02003082 uint32_t arg[5];
3083 if (!is_range) {
Ian Rogers29a26482014-05-02 15:27:29 -07003084 inst->GetVarArgs(arg);
Sebastien Hertz5243e912013-05-21 10:55:07 +02003085 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003086 for (size_t param_index = 0; param_index < params_size; param_index++) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003087 if (actual_args >= expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07003088 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invalid call to '" << PrettyMethod(res_method)
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003089 << "'. Expected " << expected_args << " arguments, processing argument " << actual_args
3090 << " (where longs/doubles count twice).";
Ian Rogersd81871c2011-10-03 13:57:23 -07003091 return NULL;
3092 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003093 const char* descriptor =
3094 mh.GetTypeDescriptorFromTypeIdx(params->GetTypeItem(param_index).type_idx_);
3095 if (descriptor == NULL) {
jeffhaod5347e02012-03-22 17:25:05 -07003096 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003097 << " missing signature component";
3098 return NULL;
Ian Rogersd81871c2011-10-03 13:57:23 -07003099 }
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003100 const RegType& reg_type = reg_types_.FromDescriptor(class_loader_->Get(), descriptor, false);
Sebastien Hertz5243e912013-05-21 10:55:07 +02003101 uint32_t get_reg = is_range ? inst->VRegC_3rc() + actual_args : arg[actual_args];
Jeff Haoa6b22c52013-10-04 14:33:22 -07003102 if (reg_type.IsIntegralTypes()) {
3103 const RegType& src_type = work_line_->GetRegisterType(get_reg);
3104 if (!src_type.IsIntegralTypes()) {
3105 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register v" << get_reg << " has type " << src_type
3106 << " but expected " << reg_type;
3107 return res_method;
3108 }
3109 } else if (!work_line_->VerifyRegisterType(get_reg, reg_type)) {
jeffhaob57e9522012-04-26 18:08:21 -07003110 return res_method;
Ian Rogersd81871c2011-10-03 13:57:23 -07003111 }
3112 actual_args = reg_type.IsLongOrDoubleTypes() ? actual_args + 2 : actual_args + 1;
3113 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003114 if (actual_args != expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07003115 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003116 << " expected " << expected_args << " arguments, found " << actual_args;
Ian Rogersd81871c2011-10-03 13:57:23 -07003117 return NULL;
3118 } else {
3119 return res_method;
3120 }
3121}
3122
Brian Carlstromea46f952013-07-30 01:26:50 -07003123mirror::ArtMethod* MethodVerifier::GetQuickInvokedMethod(const Instruction* inst,
Mathieu Chartier590fee92013-09-13 13:46:47 -07003124 RegisterLine* reg_line, bool is_range) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003125 DCHECK(inst->Opcode() == Instruction::INVOKE_VIRTUAL_QUICK ||
3126 inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
3127 const RegType& actual_arg_type = reg_line->GetInvocationThis(inst, is_range);
Ian Rogers9bc54402014-04-17 16:40:01 -07003128 if (!actual_arg_type.HasClass()) {
3129 VLOG(verifier) << "Failed to get mirror::Class* from '" << actual_arg_type << "'";
Andreas Gampe63981562014-04-17 12:28:43 -07003130 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003131 }
Ian Rogersa4cf1df2014-05-07 19:47:17 -07003132 mirror::ObjectArray<mirror::ArtMethod>* vtable = nullptr;
3133 mirror::Class* klass = actual_arg_type.GetClass();
3134 if (klass->IsInterface()) {
3135 // Derive Object.class from Class.class.getSuperclass().
3136 mirror::Class* object_klass = klass->GetClass()->GetSuperClass();
3137 CHECK(object_klass->IsObjectClass());
3138 vtable = object_klass->GetVTable();
3139 } else {
3140 vtable = klass->GetVTable();
3141 }
3142 CHECK(vtable != nullptr) << PrettyDescriptor(klass);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003143 uint16_t vtable_index = is_range ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersa4cf1df2014-05-07 19:47:17 -07003144 CHECK_LT(static_cast<int32_t>(vtable_index), vtable->GetLength()) << PrettyDescriptor(klass);
Brian Carlstromea46f952013-07-30 01:26:50 -07003145 mirror::ArtMethod* res_method = vtable->Get(vtable_index);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003146 CHECK(!Thread::Current()->IsExceptionPending());
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003147 return res_method;
3148}
3149
Brian Carlstromea46f952013-07-30 01:26:50 -07003150mirror::ArtMethod* MethodVerifier::VerifyInvokeVirtualQuickArgs(const Instruction* inst,
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003151 bool is_range) {
3152 DCHECK(Runtime::Current()->IsStarted());
Brian Carlstromea46f952013-07-30 01:26:50 -07003153 mirror::ArtMethod* res_method = GetQuickInvokedMethod(inst, work_line_.get(),
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003154 is_range);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003155 if (res_method == NULL) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003156 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer method from " << inst->Name();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003157 return NULL;
3158 }
3159 CHECK(!res_method->IsDirect() && !res_method->IsStatic());
3160
3161 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
3162 // match the call to the signature. Also, we might be calling through an abstract method
3163 // definition (which doesn't have register count values).
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003164 const RegType& actual_arg_type = work_line_->GetInvocationThis(inst, is_range);
3165 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
3166 return NULL;
3167 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003168 const size_t expected_args = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
3169 /* caught by static verifier */
3170 DCHECK(is_range || expected_args <= 5);
3171 if (expected_args > code_item_->outs_size_) {
3172 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
3173 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
3174 return NULL;
3175 }
3176
3177 /*
3178 * Check the "this" argument, which must be an instance of the class that declared the method.
3179 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
3180 * rigorous check here (which is okay since we have to do it at runtime).
3181 */
3182 if (actual_arg_type.IsUninitializedReference() && !res_method->IsConstructor()) {
3183 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
3184 return NULL;
3185 }
3186 if (!actual_arg_type.IsZero()) {
3187 mirror::Class* klass = res_method->GetDeclaringClass();
3188 const RegType& res_method_class =
Mathieu Chartierf8322842014-05-16 10:59:25 -07003189 reg_types_.FromClass(klass->GetDescriptor().c_str(), klass,
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003190 klass->CannotBeAssignedFromOtherTypes());
3191 if (!res_method_class.IsAssignableFrom(actual_arg_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07003192 Fail(actual_arg_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS :
3193 VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003194 << "' not instance of '" << res_method_class << "'";
3195 return NULL;
3196 }
3197 }
3198 /*
3199 * Process the target method's signature. This signature may or may not
3200 * have been verified, so we can't assume it's properly formed.
3201 */
3202 MethodHelper mh(res_method);
3203 const DexFile::TypeList* params = mh.GetParameterTypeList();
3204 size_t params_size = params == NULL ? 0 : params->Size();
3205 uint32_t arg[5];
3206 if (!is_range) {
Ian Rogers29a26482014-05-02 15:27:29 -07003207 inst->GetVarArgs(arg);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003208 }
3209 size_t actual_args = 1;
3210 for (size_t param_index = 0; param_index < params_size; param_index++) {
3211 if (actual_args >= expected_args) {
3212 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invalid call to '" << PrettyMethod(res_method)
Brian Carlstrom93c33962013-07-26 10:37:43 -07003213 << "'. Expected " << expected_args
3214 << " arguments, processing argument " << actual_args
3215 << " (where longs/doubles count twice).";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003216 return NULL;
3217 }
3218 const char* descriptor =
3219 mh.GetTypeDescriptorFromTypeIdx(params->GetTypeItem(param_index).type_idx_);
3220 if (descriptor == NULL) {
3221 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003222 << " missing signature component";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003223 return NULL;
3224 }
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003225 const RegType& reg_type = reg_types_.FromDescriptor(class_loader_->Get(), descriptor, false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003226 uint32_t get_reg = is_range ? inst->VRegC_3rc() + actual_args : arg[actual_args];
3227 if (!work_line_->VerifyRegisterType(get_reg, reg_type)) {
3228 return res_method;
3229 }
3230 actual_args = reg_type.IsLongOrDoubleTypes() ? actual_args + 2 : actual_args + 1;
3231 }
3232 if (actual_args != expected_args) {
3233 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
3234 << " expected " << expected_args << " arguments, found " << actual_args;
3235 return NULL;
3236 } else {
3237 return res_method;
3238 }
3239}
3240
Ian Rogers62342ec2013-06-11 10:26:37 -07003241void MethodVerifier::VerifyNewArray(const Instruction* inst, bool is_filled, bool is_range) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003242 uint32_t type_idx;
3243 if (!is_filled) {
3244 DCHECK_EQ(inst->Opcode(), Instruction::NEW_ARRAY);
3245 type_idx = inst->VRegC_22c();
3246 } else if (!is_range) {
3247 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY);
3248 type_idx = inst->VRegB_35c();
3249 } else {
3250 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY_RANGE);
3251 type_idx = inst->VRegB_3rc();
3252 }
3253 const RegType& res_type = ResolveClassAndCheckAccess(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003254 if (res_type.IsConflict()) { // bad class
3255 DCHECK_NE(failures_.size(), 0U);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003256 } else {
3257 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
3258 if (!res_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003259 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "new-array on non-array class " << res_type;
Ian Rogers0c4a5062012-02-03 15:18:59 -08003260 } else if (!is_filled) {
3261 /* make sure "size" register is valid type */
Sebastien Hertz5243e912013-05-21 10:55:07 +02003262 work_line_->VerifyRegisterType(inst->VRegB_22c(), reg_types_.Integer());
Ian Rogers0c4a5062012-02-03 15:18:59 -08003263 /* set register type to array class */
Ian Rogers62342ec2013-06-11 10:26:37 -07003264 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
3265 work_line_->SetRegisterType(inst->VRegA_22c(), precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003266 } else {
3267 // Verify each register. If "arg_count" is bad, VerifyRegisterType() will run off the end of
3268 // the list and fail. It's legal, if silly, for arg_count to be zero.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003269 const RegType& expected_type = reg_types_.GetComponentType(res_type, class_loader_->Get());
Sebastien Hertz5243e912013-05-21 10:55:07 +02003270 uint32_t arg_count = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
3271 uint32_t arg[5];
3272 if (!is_range) {
Ian Rogers29a26482014-05-02 15:27:29 -07003273 inst->GetVarArgs(arg);
Sebastien Hertz5243e912013-05-21 10:55:07 +02003274 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08003275 for (size_t ui = 0; ui < arg_count; ui++) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003276 uint32_t get_reg = is_range ? inst->VRegC_3rc() + ui : arg[ui];
Ian Rogers0c4a5062012-02-03 15:18:59 -08003277 if (!work_line_->VerifyRegisterType(get_reg, expected_type)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003278 work_line_->SetResultRegisterType(reg_types_.Conflict());
Ian Rogers0c4a5062012-02-03 15:18:59 -08003279 return;
3280 }
3281 }
3282 // filled-array result goes into "result" register
Ian Rogers62342ec2013-06-11 10:26:37 -07003283 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
3284 work_line_->SetResultRegisterType(precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003285 }
3286 }
3287}
3288
Sebastien Hertz5243e912013-05-21 10:55:07 +02003289void MethodVerifier::VerifyAGet(const Instruction* inst,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003290 const RegType& insn_type, bool is_primitive) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003291 const RegType& index_type = work_line_->GetRegisterType(inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07003292 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003293 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07003294 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003295 const RegType& array_type = work_line_->GetRegisterType(inst->VRegB_23x());
Ian Rogers89310de2012-02-01 13:47:30 -08003296 if (array_type.IsZero()) {
3297 // Null array class; this code path will fail at runtime. Infer a merge-able type from the
3298 // instruction type. TODO: have a proper notion of bottom here.
3299 if (!is_primitive || insn_type.IsCategory1Types()) {
3300 // Reference or category 1
Sebastien Hertz5243e912013-05-21 10:55:07 +02003301 work_line_->SetRegisterType(inst->VRegA_23x(), reg_types_.Zero());
Ian Rogersd81871c2011-10-03 13:57:23 -07003302 } else {
Ian Rogers89310de2012-02-01 13:47:30 -08003303 // Category 2
Sebastien Hertz5243e912013-05-21 10:55:07 +02003304 work_line_->SetRegisterTypeWide(inst->VRegA_23x(), reg_types_.FromCat2ConstLo(0, false),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003305 reg_types_.FromCat2ConstHi(0, false));
Ian Rogers89310de2012-02-01 13:47:30 -08003306 }
jeffhaofc3144e2012-02-01 17:21:15 -08003307 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003308 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aget";
Ian Rogers89310de2012-02-01 13:47:30 -08003309 } else {
3310 /* verify the class */
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003311 const RegType& component_type = reg_types_.GetComponentType(array_type, class_loader_->Get());
jeffhaofc3144e2012-02-01 17:21:15 -08003312 if (!component_type.IsReferenceTypes() && !is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07003313 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08003314 << " source for aget-object";
3315 } else if (component_type.IsNonZeroReferenceTypes() && is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07003316 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08003317 << " source for category 1 aget";
3318 } else if (is_primitive && !insn_type.Equals(component_type) &&
3319 !((insn_type.IsInteger() && component_type.IsFloat()) ||
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003320 (insn_type.IsLong() && component_type.IsDouble()))) {
3321 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array type " << array_type
3322 << " incompatible with aget of type " << insn_type;
Ian Rogers89310de2012-02-01 13:47:30 -08003323 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003324 // Use knowledge of the field type which is stronger than the type inferred from the
3325 // instruction, which can't differentiate object types and ints from floats, longs from
3326 // doubles.
3327 if (!component_type.IsLowHalf()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003328 work_line_->SetRegisterType(inst->VRegA_23x(), component_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003329 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003330 work_line_->SetRegisterTypeWide(inst->VRegA_23x(), component_type,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003331 component_type.HighHalf(&reg_types_));
3332 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003333 }
3334 }
3335 }
3336}
3337
Jeff Haofe1f7c82013-08-01 14:50:24 -07003338void MethodVerifier::VerifyPrimitivePut(const RegType& target_type, const RegType& insn_type,
3339 const uint32_t vregA) {
3340 // Primitive assignability rules are weaker than regular assignability rules.
3341 bool instruction_compatible;
3342 bool value_compatible;
3343 const RegType& value_type = work_line_->GetRegisterType(vregA);
3344 if (target_type.IsIntegralTypes()) {
Jeff Haoa4647482013-08-06 15:35:47 -07003345 instruction_compatible = target_type.Equals(insn_type);
Jeff Haofe1f7c82013-08-01 14:50:24 -07003346 value_compatible = value_type.IsIntegralTypes();
3347 } else if (target_type.IsFloat()) {
3348 instruction_compatible = insn_type.IsInteger(); // no put-float, so expect put-int
3349 value_compatible = value_type.IsFloatTypes();
3350 } else if (target_type.IsLong()) {
3351 instruction_compatible = insn_type.IsLong();
3352 value_compatible = value_type.IsLongTypes();
3353 } else if (target_type.IsDouble()) {
3354 instruction_compatible = insn_type.IsLong(); // no put-double, so expect put-long
3355 value_compatible = value_type.IsDoubleTypes();
3356 } else {
3357 instruction_compatible = false; // reference with primitive store
3358 value_compatible = false; // unused
3359 }
3360 if (!instruction_compatible) {
3361 // This is a global failure rather than a class change failure as the instructions and
3362 // the descriptors for the type should have been consistent within the same file at
3363 // compile time.
3364 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "put insn has type '" << insn_type
3365 << "' but expected type '" << target_type << "'";
3366 return;
3367 }
3368 if (!value_compatible) {
3369 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
3370 << " of type " << value_type << " but expected " << target_type << " for put";
3371 return;
3372 }
3373}
3374
Sebastien Hertz5243e912013-05-21 10:55:07 +02003375void MethodVerifier::VerifyAPut(const Instruction* inst,
Sebastien Hertz757b3042014-03-28 14:34:28 +01003376 const RegType& insn_type, bool is_primitive) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003377 const RegType& index_type = work_line_->GetRegisterType(inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07003378 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003379 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07003380 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003381 const RegType& array_type = work_line_->GetRegisterType(inst->VRegB_23x());
Ian Rogers89310de2012-02-01 13:47:30 -08003382 if (array_type.IsZero()) {
3383 // Null array type; this code path will fail at runtime. Infer a merge-able type from the
3384 // instruction type.
jeffhaofc3144e2012-02-01 17:21:15 -08003385 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003386 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aput";
Ian Rogers89310de2012-02-01 13:47:30 -08003387 } else {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003388 const RegType& component_type = reg_types_.GetComponentType(array_type, class_loader_->Get());
Jeff Haofe1f7c82013-08-01 14:50:24 -07003389 const uint32_t vregA = inst->VRegA_23x();
Jeff Haob24b4a72013-07-31 13:47:31 -07003390 if (is_primitive) {
Jeff Haofe1f7c82013-08-01 14:50:24 -07003391 VerifyPrimitivePut(component_type, insn_type, vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07003392 } else {
Jeff Haob24b4a72013-07-31 13:47:31 -07003393 if (!component_type.IsReferenceTypes()) {
3394 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
3395 << " source for aput-object";
3396 } else {
3397 // The instruction agrees with the type of array, confirm the value to be stored does too
3398 // Note: we use the instruction type (rather than the component type) for aput-object as
3399 // incompatible classes will be caught at runtime as an array store exception
Jeff Haofe1f7c82013-08-01 14:50:24 -07003400 work_line_->VerifyRegisterType(vregA, insn_type);
Jeff Haob24b4a72013-07-31 13:47:31 -07003401 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003402 }
3403 }
3404 }
3405}
3406
Brian Carlstromea46f952013-07-30 01:26:50 -07003407mirror::ArtField* MethodVerifier::GetStaticField(int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08003408 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3409 // Check access to class
3410 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Brian Carlstrom7934ac22013-07-26 10:54:15 -07003411 if (klass_type.IsConflict()) { // bad class
Ian Rogersad0b3a32012-04-16 14:50:24 -07003412 AppendToLastFailMessage(StringPrintf(" in attempt to access static field %d (%s) in %s",
3413 field_idx, dex_file_->GetFieldName(field_id),
3414 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers90040192011-12-16 08:54:29 -08003415 return NULL;
3416 }
Elliott Hughesb25c3f62012-03-26 16:35:06 -07003417 if (klass_type.IsUnresolvedTypes()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003418 return NULL; // Can't resolve Class so no more to do here, will do checking at runtime.
Ian Rogers90040192011-12-16 08:54:29 -08003419 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07003420 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
3421 mirror::ArtField* field = class_linker->ResolveFieldJLS(*dex_file_, field_idx, *dex_cache_,
3422 *class_loader_);
Ian Rogersd81871c2011-10-03 13:57:23 -07003423 if (field == NULL) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07003424 VLOG(verifier) << "Unable to resolve static field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07003425 << dex_file_->GetFieldName(field_id) << ") in "
3426 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07003427 DCHECK(Thread::Current()->IsExceptionPending());
3428 Thread::Current()->ClearException();
3429 return NULL;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003430 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
3431 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003432 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access static field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07003433 << " from " << GetDeclaringClass();
Ian Rogersd81871c2011-10-03 13:57:23 -07003434 return NULL;
3435 } else if (!field->IsStatic()) {
3436 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field) << " to be static";
3437 return NULL;
Ian Rogersd81871c2011-10-03 13:57:23 -07003438 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07003439 return field;
Ian Rogersd81871c2011-10-03 13:57:23 -07003440}
3441
Brian Carlstromea46f952013-07-30 01:26:50 -07003442mirror::ArtField* MethodVerifier::GetInstanceField(const RegType& obj_type, int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08003443 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3444 // Check access to class
3445 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003446 if (klass_type.IsConflict()) {
3447 AppendToLastFailMessage(StringPrintf(" in attempt to access instance field %d (%s) in %s",
3448 field_idx, dex_file_->GetFieldName(field_id),
3449 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers90040192011-12-16 08:54:29 -08003450 return NULL;
3451 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003452 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers90040192011-12-16 08:54:29 -08003453 return NULL; // Can't resolve Class so no more to do here
3454 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07003455 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
3456 mirror::ArtField* field = class_linker->ResolveFieldJLS(*dex_file_, field_idx, *dex_cache_,
3457 *class_loader_);
Ian Rogersd81871c2011-10-03 13:57:23 -07003458 if (field == NULL) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07003459 VLOG(verifier) << "Unable to resolve instance field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07003460 << dex_file_->GetFieldName(field_id) << ") in "
3461 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07003462 DCHECK(Thread::Current()->IsExceptionPending());
3463 Thread::Current()->ClearException();
3464 return NULL;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003465 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
3466 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003467 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access instance field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07003468 << " from " << GetDeclaringClass();
Ian Rogersd81871c2011-10-03 13:57:23 -07003469 return NULL;
3470 } else if (field->IsStatic()) {
3471 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field)
3472 << " to not be static";
3473 return NULL;
3474 } else if (obj_type.IsZero()) {
3475 // Cannot infer and check type, however, access will cause null pointer exception
3476 return field;
Ian Rogerse1758fe2012-04-19 11:31:15 -07003477 } else {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003478 mirror::Class* klass = field->GetDeclaringClass();
Ian Rogers637c65b2013-05-31 11:46:00 -07003479 const RegType& field_klass =
3480 reg_types_.FromClass(dex_file_->GetFieldDeclaringClassDescriptor(field_id),
Ian Rogers04f94f42013-06-10 15:09:26 -07003481 klass, klass->CannotBeAssignedFromOtherTypes());
Ian Rogersad0b3a32012-04-16 14:50:24 -07003482 if (obj_type.IsUninitializedTypes() &&
3483 (!IsConstructor() || GetDeclaringClass().Equals(obj_type) ||
3484 !field_klass.Equals(GetDeclaringClass()))) {
3485 // Field accesses through uninitialized references are only allowable for constructors where
3486 // the field is declared in this class
3487 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "cannot access instance field " << PrettyField(field)
Brian Carlstrom93c33962013-07-26 10:37:43 -07003488 << " of a not fully initialized object within the context"
3489 << " of " << PrettyMethod(dex_method_idx_, *dex_file_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003490 return NULL;
3491 } else if (!field_klass.IsAssignableFrom(obj_type)) {
3492 // Trying to access C1.field1 using reference of type C2, which is neither C1 or a sub-class
3493 // of C1. For resolution to occur the declared class of the field must be compatible with
3494 // obj_type, we've discovered this wasn't so, so report the field didn't exist.
3495 Fail(VERIFY_ERROR_NO_FIELD) << "cannot access instance field " << PrettyField(field)
3496 << " from object of type " << obj_type;
3497 return NULL;
3498 } else {
3499 return field;
3500 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003501 }
3502}
3503
Sebastien Hertz5243e912013-05-21 10:55:07 +02003504void MethodVerifier::VerifyISGet(const Instruction* inst, const RegType& insn_type,
3505 bool is_primitive, bool is_static) {
3506 uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
Brian Carlstromea46f952013-07-30 01:26:50 -07003507 mirror::ArtField* field;
Ian Rogersb94a27b2011-10-26 00:33:41 -07003508 if (is_static) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07003509 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003510 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003511 const RegType& object_type = work_line_->GetRegisterType(inst->VRegB_22c());
Ian Rogersf4028cc2011-11-02 14:56:39 -07003512 field = GetInstanceField(object_type, field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003513 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003514 const RegType* field_type = nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003515 if (field != NULL) {
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07003516 Thread* self = Thread::Current();
3517 mirror::Class* field_type_class;
3518 {
3519 StackHandleScope<1> hs(self);
3520 HandleWrapper<mirror::ArtField> h_field(hs.NewHandleWrapper(&field));
3521 field_type_class = FieldHelper(h_field).GetType(can_load_classes_);
3522 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003523 if (field_type_class != nullptr) {
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07003524 field_type = &reg_types_.FromClass(field->GetTypeDescriptor(), field_type_class,
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003525 field_type_class->CannotBeAssignedFromOtherTypes());
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08003526 } else {
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08003527 DCHECK(!can_load_classes_ || self->IsExceptionPending());
3528 self->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003529 }
Ian Rogers0d604842012-04-16 14:50:24 -07003530 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003531 if (field_type == nullptr) {
3532 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3533 const char* descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003534 field_type = &reg_types_.FromDescriptor(class_loader_->Get(), descriptor, false);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003535 }
Sebastien Hertz757b3042014-03-28 14:34:28 +01003536 DCHECK(field_type != nullptr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02003537 const uint32_t vregA = (is_static) ? inst->VRegA_21c() : inst->VRegA_22c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07003538 if (is_primitive) {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003539 if (field_type->Equals(insn_type) ||
3540 (field_type->IsFloat() && insn_type.IsInteger()) ||
3541 (field_type->IsDouble() && insn_type.IsLong())) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003542 // expected that read is of the correct primitive type or that int reads are reading
3543 // floats or long reads are reading doubles
3544 } else {
3545 // This is a global failure rather than a class change failure as the instructions and
3546 // the descriptors for the type should have been consistent within the same file at
3547 // compile time
3548 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
3549 << " to be of type '" << insn_type
Sebastien Hertz757b3042014-03-28 14:34:28 +01003550 << "' but found type '" << *field_type << "' in get";
Ian Rogersad0b3a32012-04-16 14:50:24 -07003551 return;
3552 }
3553 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003554 if (!insn_type.IsAssignableFrom(*field_type)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003555 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
3556 << " to be compatible with type '" << insn_type
Sebastien Hertz757b3042014-03-28 14:34:28 +01003557 << "' but found type '" << *field_type
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003558 << "' in Get-object";
Sebastien Hertz5243e912013-05-21 10:55:07 +02003559 work_line_->SetRegisterType(vregA, reg_types_.Conflict());
Ian Rogersad0b3a32012-04-16 14:50:24 -07003560 return;
3561 }
3562 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003563 if (!field_type->IsLowHalf()) {
3564 work_line_->SetRegisterType(vregA, *field_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003565 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003566 work_line_->SetRegisterTypeWide(vregA, *field_type, field_type->HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003567 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003568}
3569
Sebastien Hertz5243e912013-05-21 10:55:07 +02003570void MethodVerifier::VerifyISPut(const Instruction* inst, const RegType& insn_type,
3571 bool is_primitive, bool is_static) {
3572 uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
Brian Carlstromea46f952013-07-30 01:26:50 -07003573 mirror::ArtField* field;
Ian Rogersb94a27b2011-10-26 00:33:41 -07003574 if (is_static) {
Ian Rogers55d249f2011-11-02 16:48:09 -07003575 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003576 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003577 const RegType& object_type = work_line_->GetRegisterType(inst->VRegB_22c());
Ian Rogers55d249f2011-11-02 16:48:09 -07003578 field = GetInstanceField(object_type, field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003579 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003580 const RegType* field_type = nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003581 if (field != NULL) {
3582 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
3583 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
3584 << " from other class " << GetDeclaringClass();
3585 return;
3586 }
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07003587 mirror::Class* field_type_class;
3588 {
3589 StackHandleScope<1> hs(Thread::Current());
3590 HandleWrapper<mirror::ArtField> h_field(hs.NewHandleWrapper(&field));
3591 FieldHelper fh(h_field);
3592 field_type_class = fh.GetType(can_load_classes_);
3593 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003594 if (field_type_class != nullptr) {
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07003595 field_type = &reg_types_.FromClass(field->GetTypeDescriptor(), field_type_class,
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003596 field_type_class->CannotBeAssignedFromOtherTypes());
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08003597 } else {
3598 Thread* self = Thread::Current();
3599 DCHECK(!can_load_classes_ || self->IsExceptionPending());
3600 self->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003601 }
3602 }
3603 if (field_type == nullptr) {
3604 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3605 const char* descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003606 field_type = &reg_types_.FromDescriptor(class_loader_->Get(), descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003607 }
Sebastien Hertz757b3042014-03-28 14:34:28 +01003608 DCHECK(field_type != nullptr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02003609 const uint32_t vregA = (is_static) ? inst->VRegA_21c() : inst->VRegA_22c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07003610 if (is_primitive) {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003611 VerifyPrimitivePut(*field_type, insn_type, vregA);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003612 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003613 if (!insn_type.IsAssignableFrom(*field_type)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003614 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
3615 << " to be compatible with type '" << insn_type
Sebastien Hertz757b3042014-03-28 14:34:28 +01003616 << "' but found type '" << *field_type
Ian Rogersad0b3a32012-04-16 14:50:24 -07003617 << "' in put-object";
3618 return;
3619 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003620 work_line_->VerifyRegisterType(vregA, *field_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07003621 }
3622}
3623
Brian Carlstromea46f952013-07-30 01:26:50 -07003624mirror::ArtField* MethodVerifier::GetQuickFieldAccess(const Instruction* inst,
Ian Rogers98379392014-02-24 16:53:16 -08003625 RegisterLine* reg_line) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003626 DCHECK(inst->Opcode() == Instruction::IGET_QUICK ||
3627 inst->Opcode() == Instruction::IGET_WIDE_QUICK ||
3628 inst->Opcode() == Instruction::IGET_OBJECT_QUICK ||
3629 inst->Opcode() == Instruction::IPUT_QUICK ||
3630 inst->Opcode() == Instruction::IPUT_WIDE_QUICK ||
3631 inst->Opcode() == Instruction::IPUT_OBJECT_QUICK);
3632 const RegType& object_type = reg_line->GetRegisterType(inst->VRegB_22c());
Ian Rogers9bc54402014-04-17 16:40:01 -07003633 if (!object_type.HasClass()) {
3634 VLOG(verifier) << "Failed to get mirror::Class* from '" << object_type << "'";
3635 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003636 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003637 uint32_t field_offset = static_cast<uint32_t>(inst->VRegC_22c());
Sebastien Hertz479fc1e2014-04-04 17:51:34 +02003638 mirror::ArtField* f = mirror::ArtField::FindInstanceFieldWithOffset(object_type.GetClass(),
3639 field_offset);
3640 if (f == nullptr) {
3641 VLOG(verifier) << "Failed to find instance field at offset '" << field_offset
3642 << "' from '" << PrettyDescriptor(object_type.GetClass()) << "'";
3643 }
3644 return f;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003645}
3646
3647void MethodVerifier::VerifyIGetQuick(const Instruction* inst, const RegType& insn_type,
3648 bool is_primitive) {
3649 DCHECK(Runtime::Current()->IsStarted());
Brian Carlstromea46f952013-07-30 01:26:50 -07003650 mirror::ArtField* field = GetQuickFieldAccess(inst, work_line_.get());
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003651 if (field == NULL) {
3652 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field from " << inst->Name();
3653 return;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003654 }
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07003655 mirror::Class* field_type_class;
3656 {
3657 StackHandleScope<1> hs(Thread::Current());
3658 HandleWrapper<mirror::ArtField> h_field(hs.NewHandleWrapper(&field));
3659 FieldHelper fh(h_field);
3660 field_type_class = fh.GetType(can_load_classes_);
3661 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003662 const RegType* field_type;
3663 if (field_type_class != nullptr) {
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07003664 field_type = &reg_types_.FromClass(field->GetTypeDescriptor(), field_type_class,
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003665 field_type_class->CannotBeAssignedFromOtherTypes());
3666 } else {
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08003667 Thread* self = Thread::Current();
3668 DCHECK(!can_load_classes_ || self->IsExceptionPending());
3669 self->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003670 field_type = &reg_types_.FromDescriptor(field->GetDeclaringClass()->GetClassLoader(),
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07003671 field->GetTypeDescriptor(), false);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003672 }
Sebastien Hertz757b3042014-03-28 14:34:28 +01003673 DCHECK(field_type != nullptr);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003674 const uint32_t vregA = inst->VRegA_22c();
3675 if (is_primitive) {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003676 if (field_type->Equals(insn_type) ||
3677 (field_type->IsFloat() && insn_type.IsIntegralTypes()) ||
3678 (field_type->IsDouble() && insn_type.IsLongTypes())) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003679 // expected that read is of the correct primitive type or that int reads are reading
3680 // floats or long reads are reading doubles
3681 } else {
3682 // This is a global failure rather than a class change failure as the instructions and
3683 // the descriptors for the type should have been consistent within the same file at
3684 // compile time
3685 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003686 << " to be of type '" << insn_type
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003687 << "' but found type '" << *field_type << "' in Get";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003688 return;
3689 }
3690 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003691 if (!insn_type.IsAssignableFrom(*field_type)) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003692 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003693 << " to be compatible with type '" << insn_type
Sebastien Hertz757b3042014-03-28 14:34:28 +01003694 << "' but found type '" << *field_type
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003695 << "' in get-object";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003696 work_line_->SetRegisterType(vregA, reg_types_.Conflict());
3697 return;
3698 }
3699 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003700 if (!field_type->IsLowHalf()) {
3701 work_line_->SetRegisterType(vregA, *field_type);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003702 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003703 work_line_->SetRegisterTypeWide(vregA, *field_type, field_type->HighHalf(&reg_types_));
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003704 }
3705}
3706
3707void MethodVerifier::VerifyIPutQuick(const Instruction* inst, const RegType& insn_type,
3708 bool is_primitive) {
3709 DCHECK(Runtime::Current()->IsStarted());
Brian Carlstromea46f952013-07-30 01:26:50 -07003710 mirror::ArtField* field = GetQuickFieldAccess(inst, work_line_.get());
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003711 if (field == NULL) {
3712 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field from " << inst->Name();
3713 return;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003714 }
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07003715 const char* descriptor = field->GetTypeDescriptor();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003716 mirror::ClassLoader* loader = field->GetDeclaringClass()->GetClassLoader();
3717 const RegType& field_type = reg_types_.FromDescriptor(loader, descriptor, false);
3718 if (field != NULL) {
3719 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
3720 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003721 << " from other class " << GetDeclaringClass();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003722 return;
3723 }
3724 }
3725 const uint32_t vregA = inst->VRegA_22c();
3726 if (is_primitive) {
3727 // Primitive field assignability rules are weaker than regular assignability rules
3728 bool instruction_compatible;
3729 bool value_compatible;
3730 const RegType& value_type = work_line_->GetRegisterType(vregA);
3731 if (field_type.IsIntegralTypes()) {
3732 instruction_compatible = insn_type.IsIntegralTypes();
3733 value_compatible = value_type.IsIntegralTypes();
3734 } else if (field_type.IsFloat()) {
3735 instruction_compatible = insn_type.IsInteger(); // no [is]put-float, so expect [is]put-int
3736 value_compatible = value_type.IsFloatTypes();
3737 } else if (field_type.IsLong()) {
3738 instruction_compatible = insn_type.IsLong();
3739 value_compatible = value_type.IsLongTypes();
3740 } else if (field_type.IsDouble()) {
3741 instruction_compatible = insn_type.IsLong(); // no [is]put-double, so expect [is]put-long
3742 value_compatible = value_type.IsDoubleTypes();
3743 } else {
3744 instruction_compatible = false; // reference field with primitive store
3745 value_compatible = false; // unused
3746 }
3747 if (!instruction_compatible) {
3748 // This is a global failure rather than a class change failure as the instructions and
3749 // the descriptors for the type should have been consistent within the same file at
3750 // compile time
3751 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003752 << " to be of type '" << insn_type
3753 << "' but found type '" << field_type
3754 << "' in put";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003755 return;
3756 }
3757 if (!value_compatible) {
3758 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
3759 << " of type " << value_type
3760 << " but expected " << field_type
3761 << " for store to " << PrettyField(field) << " in put";
3762 return;
3763 }
3764 } else {
3765 if (!insn_type.IsAssignableFrom(field_type)) {
3766 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003767 << " to be compatible with type '" << insn_type
3768 << "' but found type '" << field_type
3769 << "' in put-object";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003770 return;
3771 }
3772 work_line_->VerifyRegisterType(vregA, field_type);
3773 }
3774}
3775
Ian Rogers776ac1f2012-04-13 23:36:36 -07003776bool MethodVerifier::CheckNotMoveException(const uint16_t* insns, int insn_idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003777 if ((insns[insn_idx] & 0xff) == Instruction::MOVE_EXCEPTION) {
jeffhaod5347e02012-03-22 17:25:05 -07003778 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-exception";
Ian Rogersd81871c2011-10-03 13:57:23 -07003779 return false;
3780 }
3781 return true;
3782}
3783
Ian Rogers776ac1f2012-04-13 23:36:36 -07003784bool MethodVerifier::UpdateRegisters(uint32_t next_insn, const RegisterLine* merge_line) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003785 bool changed = true;
3786 RegisterLine* target_line = reg_table_.GetLine(next_insn);
3787 if (!insn_flags_[next_insn].IsVisitedOrChanged()) {
jeffhaobdb76512011-09-07 11:43:16 -07003788 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003789 * We haven't processed this instruction before, and we haven't touched the registers here, so
3790 * there's nothing to "merge". Copy the registers over and mark it as changed. (This is the
3791 * only way a register can transition out of "unknown", so this is not just an optimization.)
jeffhaobdb76512011-09-07 11:43:16 -07003792 */
Ian Rogersb8c78592013-07-25 23:52:52 +00003793 if (!insn_flags_[next_insn].IsReturn()) {
3794 target_line->CopyFromLine(merge_line);
3795 } else {
Jeff Haob24b4a72013-07-31 13:47:31 -07003796 // Verify that the monitor stack is empty on return.
3797 if (!merge_line->VerifyMonitorStackEmpty()) {
3798 return false;
3799 }
Ian Rogersb8c78592013-07-25 23:52:52 +00003800 // For returns we only care about the operand to the return, all other registers are dead.
3801 // Initialize them as conflicts so they don't add to GC and deoptimization information.
3802 const Instruction* ret_inst = Instruction::At(code_item_->insns_ + next_insn);
3803 Instruction::Code opcode = ret_inst->Opcode();
3804 if ((opcode == Instruction::RETURN_VOID) || (opcode == Instruction::RETURN_VOID_BARRIER)) {
3805 target_line->MarkAllRegistersAsConflicts();
3806 } else {
3807 target_line->CopyFromLine(merge_line);
3808 if (opcode == Instruction::RETURN_WIDE) {
3809 target_line->MarkAllRegistersAsConflictsExceptWide(ret_inst->VRegA_11x());
3810 } else {
3811 target_line->MarkAllRegistersAsConflictsExcept(ret_inst->VRegA_11x());
3812 }
3813 }
3814 }
jeffhaobdb76512011-09-07 11:43:16 -07003815 } else {
Ian Rogers700a4022014-05-19 16:49:03 -07003816 std::unique_ptr<RegisterLine> copy(gDebugVerify ?
Ian Rogersd0fbd852013-09-24 18:17:04 -07003817 RegisterLine::Create(target_line->NumRegs(), this) :
Brian Carlstrom93c33962013-07-26 10:37:43 -07003818 NULL);
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003819 if (gDebugVerify) {
3820 copy->CopyFromLine(target_line);
3821 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003822 changed = target_line->MergeRegisters(merge_line);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003823 if (have_pending_hard_failure_) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003824 return false;
jeffhaobdb76512011-09-07 11:43:16 -07003825 }
Ian Rogers2c8a8572011-10-24 17:11:36 -07003826 if (gDebugVerify && changed) {
Elliott Hughes398f64b2012-03-26 18:05:48 -07003827 LogVerifyInfo() << "Merging at [" << reinterpret_cast<void*>(work_insn_idx_) << "]"
Elliott Hughesc073b072012-05-24 19:29:17 -07003828 << " to [" << reinterpret_cast<void*>(next_insn) << "]: " << "\n"
3829 << *copy.get() << " MERGE\n"
3830 << *merge_line << " ==\n"
3831 << *target_line << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07003832 }
3833 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003834 if (changed) {
3835 insn_flags_[next_insn].SetChanged();
jeffhaobdb76512011-09-07 11:43:16 -07003836 }
3837 return true;
3838}
3839
Ian Rogers7b3ddd22013-02-21 15:19:52 -08003840InstructionFlags* MethodVerifier::CurrentInsnFlags() {
Ian Rogers776ac1f2012-04-13 23:36:36 -07003841 return &insn_flags_[work_insn_idx_];
3842}
3843
Ian Rogersad0b3a32012-04-16 14:50:24 -07003844const RegType& MethodVerifier::GetMethodReturnType() {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003845 if (return_type_ == nullptr) {
3846 if (mirror_method_ != NULL) {
3847 MethodHelper mh(mirror_method_);
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08003848 mirror::Class* return_type_class = mh.GetReturnType(can_load_classes_);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003849 if (return_type_class != nullptr) {
Mathieu Chartier590fee92013-09-13 13:46:47 -07003850 return_type_ = &reg_types_.FromClass(mh.GetReturnTypeDescriptor(), return_type_class,
3851 return_type_class->CannotBeAssignedFromOtherTypes());
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003852 } else {
3853 Thread* self = Thread::Current();
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08003854 DCHECK(!can_load_classes_ || self->IsExceptionPending());
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003855 self->ClearException();
3856 }
3857 }
3858 if (return_type_ == nullptr) {
3859 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
3860 const DexFile::ProtoId& proto_id = dex_file_->GetMethodPrototype(method_id);
3861 uint16_t return_type_idx = proto_id.return_type_idx_;
3862 const char* descriptor = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(return_type_idx));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003863 return_type_ = &reg_types_.FromDescriptor(class_loader_->Get(), descriptor, false);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003864 }
3865 }
3866 return *return_type_;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003867}
3868
3869const RegType& MethodVerifier::GetDeclaringClass() {
Ian Rogers637c65b2013-05-31 11:46:00 -07003870 if (declaring_class_ == NULL) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003871 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
Brian Carlstrom93c33962013-07-26 10:37:43 -07003872 const char* descriptor
3873 = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(method_id.class_idx_));
Ian Rogers637c65b2013-05-31 11:46:00 -07003874 if (mirror_method_ != NULL) {
3875 mirror::Class* klass = mirror_method_->GetDeclaringClass();
Ian Rogers04f94f42013-06-10 15:09:26 -07003876 declaring_class_ = &reg_types_.FromClass(descriptor, klass,
3877 klass->CannotBeAssignedFromOtherTypes());
Ian Rogers637c65b2013-05-31 11:46:00 -07003878 } else {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003879 declaring_class_ = &reg_types_.FromDescriptor(class_loader_->Get(), descriptor, false);
Ian Rogers637c65b2013-05-31 11:46:00 -07003880 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003881 }
Ian Rogers637c65b2013-05-31 11:46:00 -07003882 return *declaring_class_;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003883}
3884
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003885std::vector<int32_t> MethodVerifier::DescribeVRegs(uint32_t dex_pc) {
3886 RegisterLine* line = reg_table_.GetLine(dex_pc);
Sebastien Hertzaa0c00c2014-03-14 17:58:54 +01003887 DCHECK(line != nullptr) << "No register line at DEX pc " << StringPrintf("0x%x", dex_pc);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003888 std::vector<int32_t> result;
3889 for (size_t i = 0; i < line->NumRegs(); ++i) {
3890 const RegType& type = line->GetRegisterType(i);
3891 if (type.IsConstant()) {
3892 result.push_back(type.IsPreciseConstant() ? kConstant : kImpreciseConstant);
3893 result.push_back(type.ConstantValue());
3894 } else if (type.IsConstantLo()) {
3895 result.push_back(type.IsPreciseConstantLo() ? kConstant : kImpreciseConstant);
3896 result.push_back(type.ConstantValueLo());
3897 } else if (type.IsConstantHi()) {
3898 result.push_back(type.IsPreciseConstantHi() ? kConstant : kImpreciseConstant);
3899 result.push_back(type.ConstantValueHi());
3900 } else if (type.IsIntegralTypes()) {
3901 result.push_back(kIntVReg);
3902 result.push_back(0);
3903 } else if (type.IsFloat()) {
3904 result.push_back(kFloatVReg);
3905 result.push_back(0);
3906 } else if (type.IsLong()) {
3907 result.push_back(kLongLoVReg);
3908 result.push_back(0);
3909 result.push_back(kLongHiVReg);
3910 result.push_back(0);
3911 ++i;
3912 } else if (type.IsDouble()) {
3913 result.push_back(kDoubleLoVReg);
3914 result.push_back(0);
3915 result.push_back(kDoubleHiVReg);
3916 result.push_back(0);
3917 ++i;
3918 } else if (type.IsUndefined() || type.IsConflict() || type.IsHighHalf()) {
3919 result.push_back(kUndefined);
3920 result.push_back(0);
3921 } else {
Ian Rogers7b3ddd22013-02-21 15:19:52 -08003922 CHECK(type.IsNonZeroReferenceTypes());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003923 result.push_back(kReferenceVReg);
3924 result.push_back(0);
3925 }
3926 }
3927 return result;
3928}
3929
Sebastien Hertz849600b2013-12-20 10:28:08 +01003930const RegType& MethodVerifier::DetermineCat1Constant(int32_t value, bool precise) {
3931 if (precise) {
3932 // Precise constant type.
3933 return reg_types_.FromCat1Const(value, true);
3934 } else {
3935 // Imprecise constant type.
3936 if (value < -32768) {
3937 return reg_types_.IntConstant();
3938 } else if (value < -128) {
3939 return reg_types_.ShortConstant();
3940 } else if (value < 0) {
3941 return reg_types_.ByteConstant();
3942 } else if (value == 0) {
3943 return reg_types_.Zero();
3944 } else if (value == 1) {
3945 return reg_types_.One();
3946 } else if (value < 128) {
3947 return reg_types_.PosByteConstant();
3948 } else if (value < 32768) {
3949 return reg_types_.PosShortConstant();
3950 } else if (value < 65536) {
3951 return reg_types_.CharConstant();
3952 } else {
3953 return reg_types_.IntConstant();
3954 }
3955 }
3956}
3957
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003958void MethodVerifier::Init() {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08003959 art::verifier::RegTypeCache::Init();
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003960}
3961
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003962void MethodVerifier::Shutdown() {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08003963 verifier::RegTypeCache::ShutDown();
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08003964}
jeffhaod1224c72012-02-29 13:43:08 -08003965
Mathieu Chartier83c8ee02014-01-28 14:50:23 -08003966void MethodVerifier::VisitRoots(RootCallback* callback, void* arg) {
3967 reg_types_.VisitRoots(callback, arg);
Mathieu Chartierc528dba2013-11-26 12:00:11 -08003968}
3969
Ian Rogersd81871c2011-10-03 13:57:23 -07003970} // namespace verifier
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07003971} // namespace art