blob: de611e1ca670ec55983bf2dc795fc54fba523ec2 [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) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700341 StackHandleScope<2> hs(Thread::Current());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700342 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache()));
343 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader()));
344 MethodVerifier verifier(m->GetDexFile(), &dex_cache, &class_loader, &m->GetClassDef(),
345 m->GetCodeItem(), m->GetDexMethodIndex(), m, m->GetAccessFlags(), false,
Ian Rogers46960fe2014-05-23 10:43:43 -0700346 true, false);
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700347 verifier.interesting_dex_pc_ = dex_pc;
Ian Rogers46960fe2014-05-23 10:43:43 -0700348 verifier.monitor_enter_dex_pcs_ = monitor_enter_dex_pcs;
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700349 verifier.FindLocksAtDexPc();
350}
351
352void MethodVerifier::FindLocksAtDexPc() {
353 CHECK(monitor_enter_dex_pcs_ != NULL);
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700354 CHECK(code_item_ != NULL); // This only makes sense for methods with code.
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700355
356 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
357 // verification. In practice, the phase we want relies on data structures set up by all the
358 // earlier passes, so we just run the full method verification and bail out early when we've
359 // got what we wanted.
360 Verify();
361}
362
Brian Carlstromea46f952013-07-30 01:26:50 -0700363mirror::ArtField* MethodVerifier::FindAccessedFieldAtDexPc(mirror::ArtMethod* m,
Ian Rogers46960fe2014-05-23 10:43:43 -0700364 uint32_t dex_pc) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700365 StackHandleScope<2> hs(Thread::Current());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700366 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache()));
367 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader()));
368 MethodVerifier verifier(m->GetDexFile(), &dex_cache, &class_loader, &m->GetClassDef(),
369 m->GetCodeItem(), m->GetDexMethodIndex(), m, m->GetAccessFlags(), true,
Ian Rogers46960fe2014-05-23 10:43:43 -0700370 true, false);
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200371 return verifier.FindAccessedFieldAtDexPc(dex_pc);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200372}
373
Brian Carlstromea46f952013-07-30 01:26:50 -0700374mirror::ArtField* MethodVerifier::FindAccessedFieldAtDexPc(uint32_t dex_pc) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700375 CHECK(code_item_ != NULL); // This only makes sense for methods with code.
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200376
377 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
378 // verification. In practice, the phase we want relies on data structures set up by all the
379 // earlier passes, so we just run the full method verification and bail out early when we've
380 // got what we wanted.
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200381 bool success = Verify();
382 if (!success) {
Ian Rogers9bc54402014-04-17 16:40:01 -0700383 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200384 }
385 RegisterLine* register_line = reg_table_.GetLine(dex_pc);
386 if (register_line == NULL) {
Ian Rogers9bc54402014-04-17 16:40:01 -0700387 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200388 }
389 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
390 return GetQuickFieldAccess(inst, register_line);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200391}
392
Brian Carlstromea46f952013-07-30 01:26:50 -0700393mirror::ArtMethod* MethodVerifier::FindInvokedMethodAtDexPc(mirror::ArtMethod* m,
Mathieu Chartier590fee92013-09-13 13:46:47 -0700394 uint32_t dex_pc) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700395 StackHandleScope<2> hs(Thread::Current());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700396 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache()));
397 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader()));
398 MethodVerifier verifier(m->GetDexFile(), &dex_cache, &class_loader, &m->GetClassDef(),
399 m->GetCodeItem(), m->GetDexMethodIndex(), m, m->GetAccessFlags(), true,
Ian Rogers46960fe2014-05-23 10:43:43 -0700400 true, false);
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200401 return verifier.FindInvokedMethodAtDexPc(dex_pc);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200402}
403
Brian Carlstromea46f952013-07-30 01:26:50 -0700404mirror::ArtMethod* MethodVerifier::FindInvokedMethodAtDexPc(uint32_t dex_pc) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700405 CHECK(code_item_ != NULL); // This only makes sense for methods with code.
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200406
407 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
408 // verification. In practice, the phase we want relies on data structures set up by all the
409 // earlier passes, so we just run the full method verification and bail out early when we've
410 // got what we wanted.
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200411 bool success = Verify();
412 if (!success) {
413 return NULL;
414 }
415 RegisterLine* register_line = reg_table_.GetLine(dex_pc);
416 if (register_line == NULL) {
417 return NULL;
418 }
419 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
420 const bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
421 return GetQuickInvokedMethod(inst, register_line, is_range);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200422}
423
Ian Rogersad0b3a32012-04-16 14:50:24 -0700424bool MethodVerifier::Verify() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700425 // If there aren't any instructions, make sure that's expected, then exit successfully.
426 if (code_item_ == NULL) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700427 if ((method_access_flags_ & (kAccNative | kAccAbstract)) == 0) {
jeffhaod5347e02012-03-22 17:25:05 -0700428 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "zero-length code in concrete non-native method";
jeffhaobdb76512011-09-07 11:43:16 -0700429 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -0700430 } else {
431 return true;
jeffhaobdb76512011-09-07 11:43:16 -0700432 }
jeffhaobdb76512011-09-07 11:43:16 -0700433 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700434 // Sanity-check the register counts. ins + locals = registers, so make sure that ins <= registers.
435 if (code_item_->ins_size_ > code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700436 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad register counts (ins=" << code_item_->ins_size_
437 << " regs=" << code_item_->registers_size_;
Ian Rogersd81871c2011-10-03 13:57:23 -0700438 return false;
jeffhaobdb76512011-09-07 11:43:16 -0700439 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700440 // Allocate and initialize an array to hold instruction data.
Ian Rogers7b3ddd22013-02-21 15:19:52 -0800441 insn_flags_.reset(new InstructionFlags[code_item_->insns_size_in_code_units_]());
Ian Rogersd81871c2011-10-03 13:57:23 -0700442 // Run through the instructions and see if the width checks out.
443 bool result = ComputeWidthsAndCountOps();
444 // Flag instructions guarded by a "try" block and check exception handlers.
445 result = result && ScanTryCatchBlocks();
446 // Perform static instruction verification.
447 result = result && VerifyInstructions();
Ian Rogersad0b3a32012-04-16 14:50:24 -0700448 // Perform code-flow analysis and return.
Vladimir Marko2b5eaa22013-12-13 13:59:30 +0000449 result = result && VerifyCodeFlow();
450 // Compute information for compiler.
451 if (result && Runtime::Current()->IsCompiler()) {
452 result = Runtime::Current()->GetCompilerCallbacks()->MethodVerified(this);
453 }
454 return result;
jeffhaoba5ebb92011-08-25 17:24:37 -0700455}
456
Ian Rogers776ac1f2012-04-13 23:36:36 -0700457std::ostream& MethodVerifier::Fail(VerifyError error) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700458 switch (error) {
459 case VERIFY_ERROR_NO_CLASS:
460 case VERIFY_ERROR_NO_FIELD:
461 case VERIFY_ERROR_NO_METHOD:
462 case VERIFY_ERROR_ACCESS_CLASS:
463 case VERIFY_ERROR_ACCESS_FIELD:
464 case VERIFY_ERROR_ACCESS_METHOD:
Ian Rogers08f753d2012-08-24 14:35:25 -0700465 case VERIFY_ERROR_INSTANTIATION:
466 case VERIFY_ERROR_CLASS_CHANGE:
Elliott Hughes4993bbc2013-01-10 15:41:25 -0800467 if (Runtime::Current()->IsCompiler() || !can_load_classes_) {
jeffhaofaf459e2012-08-31 15:32:47 -0700468 // If we're optimistically running verification at compile time, turn NO_xxx, ACCESS_xxx,
469 // class change and instantiation errors into soft verification errors so that we re-verify
470 // at runtime. We may fail to find or to agree on access because of not yet available class
471 // loaders, or class loaders that will differ at runtime. In these cases, we don't want to
472 // affect the soundness of the code being compiled. Instead, the generated code runs "slow
473 // paths" that dynamically perform the verification and cause the behavior to be that akin
474 // to an interpreter.
475 error = VERIFY_ERROR_BAD_CLASS_SOFT;
476 } else {
Jeff Haoa3faaf42013-09-03 19:07:00 -0700477 // If we fail again at runtime, mark that this instruction would throw and force this
478 // method to be executed using the interpreter with checks.
jeffhaofaf459e2012-08-31 15:32:47 -0700479 have_pending_runtime_throw_failure_ = true;
480 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700481 break;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700482 // Indication that verification should be retried at runtime.
483 case VERIFY_ERROR_BAD_CLASS_SOFT:
Jeff Haoee988952013-04-16 14:23:47 -0700484 if (!allow_soft_failures_) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700485 have_pending_hard_failure_ = true;
486 }
487 break;
jeffhaod5347e02012-03-22 17:25:05 -0700488 // Hard verification failures at compile time will still fail at runtime, so the class is
489 // marked as rejected to prevent it from being compiled.
Ian Rogersad0b3a32012-04-16 14:50:24 -0700490 case VERIFY_ERROR_BAD_CLASS_HARD: {
491 if (Runtime::Current()->IsCompiler()) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700492 ClassReference ref(dex_file_, dex_file_->GetIndexForClassDef(*class_def_));
Vladimir Marko2b5eaa22013-12-13 13:59:30 +0000493 Runtime::Current()->GetCompilerCallbacks()->ClassRejected(ref);
jeffhaod1224c72012-02-29 13:43:08 -0800494 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700495 have_pending_hard_failure_ = true;
496 break;
Ian Rogers47a05882012-02-03 12:23:33 -0800497 }
498 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700499 failures_.push_back(error);
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800500 std::string location(StringPrintf("%s: [0x%X]", PrettyMethod(dex_method_idx_, *dex_file_).c_str(),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700501 work_insn_idx_));
502 std::ostringstream* failure_message = new std::ostringstream(location);
503 failure_messages_.push_back(failure_message);
504 return *failure_message;
505}
506
Ian Rogers576ca0c2014-06-06 15:58:22 -0700507std::ostream& MethodVerifier::LogVerifyInfo() {
508 return info_messages_ << "VFY: " << PrettyMethod(dex_method_idx_, *dex_file_)
509 << '[' << reinterpret_cast<void*>(work_insn_idx_) << "] : ";
510}
511
Ian Rogersad0b3a32012-04-16 14:50:24 -0700512void MethodVerifier::PrependToLastFailMessage(std::string prepend) {
513 size_t failure_num = failure_messages_.size();
514 DCHECK_NE(failure_num, 0U);
515 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
516 prepend += last_fail_message->str();
517 failure_messages_[failure_num - 1] = new std::ostringstream(prepend);
518 delete last_fail_message;
519}
520
521void MethodVerifier::AppendToLastFailMessage(std::string append) {
522 size_t failure_num = failure_messages_.size();
523 DCHECK_NE(failure_num, 0U);
524 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
525 (*last_fail_message) << append;
Ian Rogers47a05882012-02-03 12:23:33 -0800526}
527
Ian Rogers776ac1f2012-04-13 23:36:36 -0700528bool MethodVerifier::ComputeWidthsAndCountOps() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700529 const uint16_t* insns = code_item_->insns_;
530 size_t insns_size = code_item_->insns_size_in_code_units_;
531 const Instruction* inst = Instruction::At(insns);
jeffhaobdb76512011-09-07 11:43:16 -0700532 size_t new_instance_count = 0;
533 size_t monitor_enter_count = 0;
Ian Rogersd81871c2011-10-03 13:57:23 -0700534 size_t dex_pc = 0;
jeffhaobdb76512011-09-07 11:43:16 -0700535
Ian Rogersd81871c2011-10-03 13:57:23 -0700536 while (dex_pc < insns_size) {
jeffhaobdb76512011-09-07 11:43:16 -0700537 Instruction::Code opcode = inst->Opcode();
Ian Rogersa9a82542013-10-04 11:17:26 -0700538 switch (opcode) {
539 case Instruction::APUT_OBJECT:
540 case Instruction::CHECK_CAST:
541 has_check_casts_ = true;
542 break;
543 case Instruction::INVOKE_VIRTUAL:
544 case Instruction::INVOKE_VIRTUAL_RANGE:
545 case Instruction::INVOKE_INTERFACE:
546 case Instruction::INVOKE_INTERFACE_RANGE:
547 has_virtual_or_interface_invokes_ = true;
548 break;
549 case Instruction::MONITOR_ENTER:
550 monitor_enter_count++;
551 break;
552 case Instruction::NEW_INSTANCE:
553 new_instance_count++;
554 break;
555 default:
556 break;
jeffhaobdb76512011-09-07 11:43:16 -0700557 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700558 size_t inst_size = inst->SizeInCodeUnits();
559 insn_flags_[dex_pc].SetLengthInCodeUnits(inst_size);
560 dex_pc += inst_size;
jeffhaobdb76512011-09-07 11:43:16 -0700561 inst = inst->Next();
562 }
563
Ian Rogersd81871c2011-10-03 13:57:23 -0700564 if (dex_pc != insns_size) {
jeffhaod5347e02012-03-22 17:25:05 -0700565 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "code did not end where expected ("
566 << dex_pc << " vs. " << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700567 return false;
568 }
569
Ian Rogersd81871c2011-10-03 13:57:23 -0700570 new_instance_count_ = new_instance_count;
571 monitor_enter_count_ = monitor_enter_count;
jeffhaobdb76512011-09-07 11:43:16 -0700572 return true;
573}
574
Ian Rogers776ac1f2012-04-13 23:36:36 -0700575bool MethodVerifier::ScanTryCatchBlocks() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700576 uint32_t tries_size = code_item_->tries_size_;
jeffhaobdb76512011-09-07 11:43:16 -0700577 if (tries_size == 0) {
578 return true;
579 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700580 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Ian Rogers0571d352011-11-03 19:51:38 -0700581 const DexFile::TryItem* tries = DexFile::GetTryItems(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -0700582
583 for (uint32_t idx = 0; idx < tries_size; idx++) {
584 const DexFile::TryItem* try_item = &tries[idx];
585 uint32_t start = try_item->start_addr_;
586 uint32_t end = start + try_item->insn_count_;
jeffhaobdb76512011-09-07 11:43:16 -0700587 if ((start >= end) || (start >= insns_size) || (end > insns_size)) {
jeffhaod5347e02012-03-22 17:25:05 -0700588 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad exception entry: startAddr=" << start
589 << " endAddr=" << end << " (size=" << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700590 return false;
591 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700592 if (!insn_flags_[start].IsOpcode()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700593 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
594 << "'try' block starts inside an instruction (" << start << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700595 return false;
596 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700597 for (uint32_t dex_pc = start; dex_pc < end;
598 dex_pc += insn_flags_[dex_pc].GetLengthInCodeUnits()) {
599 insn_flags_[dex_pc].SetInTry();
jeffhaobdb76512011-09-07 11:43:16 -0700600 }
601 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800602 // Iterate over each of the handlers to verify target addresses.
Ian Rogers0571d352011-11-03 19:51:38 -0700603 const byte* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -0700604 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700605 ClassLinker* linker = Runtime::Current()->GetClassLinker();
jeffhaobdb76512011-09-07 11:43:16 -0700606 for (uint32_t idx = 0; idx < handlers_size; idx++) {
Ian Rogers0571d352011-11-03 19:51:38 -0700607 CatchHandlerIterator iterator(handlers_ptr);
608 for (; iterator.HasNext(); iterator.Next()) {
609 uint32_t dex_pc= iterator.GetHandlerAddress();
Ian Rogersd81871c2011-10-03 13:57:23 -0700610 if (!insn_flags_[dex_pc].IsOpcode()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700611 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
612 << "exception handler starts at bad address (" << dex_pc << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700613 return false;
614 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700615 insn_flags_[dex_pc].SetBranchTarget();
Ian Rogers28ad40d2011-10-27 15:19:26 -0700616 // Ensure exception types are resolved so that they don't need resolution to be delivered,
617 // unresolved exception types will be ignored by exception delivery
Ian Rogers0571d352011-11-03 19:51:38 -0700618 if (iterator.GetHandlerTypeIndex() != DexFile::kDexNoIndex16) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800619 mirror::Class* exception_type = linker->ResolveType(*dex_file_,
620 iterator.GetHandlerTypeIndex(),
Mathieu Chartier590fee92013-09-13 13:46:47 -0700621 *dex_cache_, *class_loader_);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700622 if (exception_type == NULL) {
623 DCHECK(Thread::Current()->IsExceptionPending());
624 Thread::Current()->ClearException();
625 }
626 }
jeffhaobdb76512011-09-07 11:43:16 -0700627 }
Ian Rogers0571d352011-11-03 19:51:38 -0700628 handlers_ptr = iterator.EndDataPointer();
jeffhaobdb76512011-09-07 11:43:16 -0700629 }
jeffhaobdb76512011-09-07 11:43:16 -0700630 return true;
631}
632
Ian Rogers776ac1f2012-04-13 23:36:36 -0700633bool MethodVerifier::VerifyInstructions() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700634 const Instruction* inst = Instruction::At(code_item_->insns_);
jeffhaoba5ebb92011-08-25 17:24:37 -0700635
Ian Rogers0c7abda2012-09-19 13:33:42 -0700636 /* 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 -0700637 insn_flags_[0].SetBranchTarget();
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700638 insn_flags_[0].SetCompileTimeInfoPoint();
Ian Rogersd81871c2011-10-03 13:57:23 -0700639
640 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700641 for (uint32_t dex_pc = 0; dex_pc < insns_size;) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700642 if (!VerifyInstruction(inst, dex_pc)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700643 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -0700644 return false;
645 }
646 /* Flag instructions that are garbage collection points */
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700647 // All invoke points are marked as "Throw" points already.
648 // We are relying on this to also count all the invokes as interesting.
Ian Rogersb8c78592013-07-25 23:52:52 +0000649 if (inst->IsBranch() || inst->IsSwitch() || inst->IsThrow()) {
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700650 insn_flags_[dex_pc].SetCompileTimeInfoPoint();
Ian Rogersb8c78592013-07-25 23:52:52 +0000651 } else if (inst->IsReturn()) {
652 insn_flags_[dex_pc].SetCompileTimeInfoPointAndReturn();
Ian Rogersd81871c2011-10-03 13:57:23 -0700653 }
654 dex_pc += inst->SizeInCodeUnits();
655 inst = inst->Next();
656 }
657 return true;
658}
659
Ian Rogers776ac1f2012-04-13 23:36:36 -0700660bool MethodVerifier::VerifyInstruction(const Instruction* inst, uint32_t code_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700661 bool result = true;
662 switch (inst->GetVerifyTypeArgumentA()) {
663 case Instruction::kVerifyRegA:
Ian Rogers29a26482014-05-02 15:27:29 -0700664 result = result && CheckRegisterIndex(inst->VRegA());
Ian Rogersd81871c2011-10-03 13:57:23 -0700665 break;
666 case Instruction::kVerifyRegAWide:
Ian Rogers29a26482014-05-02 15:27:29 -0700667 result = result && CheckWideRegisterIndex(inst->VRegA());
Ian Rogersd81871c2011-10-03 13:57:23 -0700668 break;
669 }
670 switch (inst->GetVerifyTypeArgumentB()) {
671 case Instruction::kVerifyRegB:
Ian Rogers29a26482014-05-02 15:27:29 -0700672 result = result && CheckRegisterIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700673 break;
674 case Instruction::kVerifyRegBField:
Ian Rogers29a26482014-05-02 15:27:29 -0700675 result = result && CheckFieldIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700676 break;
677 case Instruction::kVerifyRegBMethod:
Ian Rogers29a26482014-05-02 15:27:29 -0700678 result = result && CheckMethodIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700679 break;
680 case Instruction::kVerifyRegBNewInstance:
Ian Rogers29a26482014-05-02 15:27:29 -0700681 result = result && CheckNewInstance(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700682 break;
683 case Instruction::kVerifyRegBString:
Ian Rogers29a26482014-05-02 15:27:29 -0700684 result = result && CheckStringIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700685 break;
686 case Instruction::kVerifyRegBType:
Ian Rogers29a26482014-05-02 15:27:29 -0700687 result = result && CheckTypeIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700688 break;
689 case Instruction::kVerifyRegBWide:
Ian Rogers29a26482014-05-02 15:27:29 -0700690 result = result && CheckWideRegisterIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700691 break;
692 }
693 switch (inst->GetVerifyTypeArgumentC()) {
694 case Instruction::kVerifyRegC:
Ian Rogers29a26482014-05-02 15:27:29 -0700695 result = result && CheckRegisterIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700696 break;
697 case Instruction::kVerifyRegCField:
Ian Rogers29a26482014-05-02 15:27:29 -0700698 result = result && CheckFieldIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700699 break;
700 case Instruction::kVerifyRegCNewArray:
Ian Rogers29a26482014-05-02 15:27:29 -0700701 result = result && CheckNewArray(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700702 break;
703 case Instruction::kVerifyRegCType:
Ian Rogers29a26482014-05-02 15:27:29 -0700704 result = result && CheckTypeIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700705 break;
706 case Instruction::kVerifyRegCWide:
Ian Rogers29a26482014-05-02 15:27:29 -0700707 result = result && CheckWideRegisterIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700708 break;
709 }
710 switch (inst->GetVerifyExtraFlags()) {
711 case Instruction::kVerifyArrayData:
712 result = result && CheckArrayData(code_offset);
713 break;
714 case Instruction::kVerifyBranchTarget:
715 result = result && CheckBranchTarget(code_offset);
716 break;
717 case Instruction::kVerifySwitchTargets:
718 result = result && CheckSwitchTargets(code_offset);
719 break;
Ian Rogers29a26482014-05-02 15:27:29 -0700720 case Instruction::kVerifyVarArg: {
721 uint32_t args[Instruction::kMaxVarArgRegs];
722 inst->GetVarArgs(args);
723 result = result && CheckVarArgRegs(inst->VRegA(), args);
Ian Rogersd81871c2011-10-03 13:57:23 -0700724 break;
Ian Rogers29a26482014-05-02 15:27:29 -0700725 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700726 case Instruction::kVerifyVarArgRange:
Ian Rogers29a26482014-05-02 15:27:29 -0700727 result = result && CheckVarArgRangeRegs(inst->VRegA(), inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700728 break;
729 case Instruction::kVerifyError:
jeffhaod5347e02012-03-22 17:25:05 -0700730 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected opcode " << inst->Name();
Ian Rogersd81871c2011-10-03 13:57:23 -0700731 result = false;
732 break;
733 }
734 return result;
735}
736
Ian Rogers776ac1f2012-04-13 23:36:36 -0700737bool MethodVerifier::CheckRegisterIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700738 if (idx >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700739 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register index out of range (" << idx << " >= "
740 << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700741 return false;
742 }
743 return true;
744}
745
Ian Rogers776ac1f2012-04-13 23:36:36 -0700746bool MethodVerifier::CheckWideRegisterIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700747 if (idx + 1 >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700748 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "wide register index out of range (" << idx
749 << "+1 >= " << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700750 return false;
751 }
752 return true;
753}
754
Ian Rogers776ac1f2012-04-13 23:36:36 -0700755bool MethodVerifier::CheckFieldIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700756 if (idx >= dex_file_->GetHeader().field_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700757 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad field index " << idx << " (max "
758 << dex_file_->GetHeader().field_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700759 return false;
760 }
761 return true;
762}
763
Ian Rogers776ac1f2012-04-13 23:36:36 -0700764bool MethodVerifier::CheckMethodIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700765 if (idx >= dex_file_->GetHeader().method_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700766 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad method index " << idx << " (max "
767 << dex_file_->GetHeader().method_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700768 return false;
769 }
770 return true;
771}
772
Ian Rogers776ac1f2012-04-13 23:36:36 -0700773bool MethodVerifier::CheckNewInstance(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700774 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700775 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
776 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700777 return false;
778 }
779 // We don't need the actual class, just a pointer to the class name.
Ian Rogers0571d352011-11-03 19:51:38 -0700780 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -0700781 if (descriptor[0] != 'L') {
jeffhaod5347e02012-03-22 17:25:05 -0700782 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "can't call new-instance on type '" << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -0700783 return false;
784 }
785 return true;
786}
787
Ian Rogers776ac1f2012-04-13 23:36:36 -0700788bool MethodVerifier::CheckStringIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700789 if (idx >= dex_file_->GetHeader().string_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700790 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad string index " << idx << " (max "
791 << dex_file_->GetHeader().string_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700792 return false;
793 }
794 return true;
795}
796
Ian Rogers776ac1f2012-04-13 23:36:36 -0700797bool MethodVerifier::CheckTypeIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700798 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700799 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
800 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700801 return false;
802 }
803 return true;
804}
805
Ian Rogers776ac1f2012-04-13 23:36:36 -0700806bool MethodVerifier::CheckNewArray(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700807 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700808 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
809 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700810 return false;
811 }
812 int bracket_count = 0;
Ian Rogers0571d352011-11-03 19:51:38 -0700813 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -0700814 const char* cp = descriptor;
815 while (*cp++ == '[') {
816 bracket_count++;
817 }
818 if (bracket_count == 0) {
819 /* The given class must be an array type. */
Brian Carlstrom93c33962013-07-26 10:37:43 -0700820 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
821 << "can't new-array class '" << descriptor << "' (not an array)";
Ian Rogersd81871c2011-10-03 13:57:23 -0700822 return false;
823 } else if (bracket_count > 255) {
824 /* It is illegal to create an array of more than 255 dimensions. */
Brian Carlstrom93c33962013-07-26 10:37:43 -0700825 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
826 << "can't new-array class '" << descriptor << "' (exceeds limit)";
Ian Rogersd81871c2011-10-03 13:57:23 -0700827 return false;
828 }
829 return true;
830}
831
Ian Rogers776ac1f2012-04-13 23:36:36 -0700832bool MethodVerifier::CheckArrayData(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700833 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
834 const uint16_t* insns = code_item_->insns_ + cur_offset;
835 const uint16_t* array_data;
836 int32_t array_data_offset;
837
838 DCHECK_LT(cur_offset, insn_count);
839 /* make sure the start of the array data table is in range */
840 array_data_offset = insns[1] | (((int32_t) insns[2]) << 16);
841 if ((int32_t) cur_offset + array_data_offset < 0 ||
842 cur_offset + array_data_offset + 2 >= insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -0700843 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data start: at " << cur_offset
Brian Carlstrom93c33962013-07-26 10:37:43 -0700844 << ", data offset " << array_data_offset
845 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -0700846 return false;
847 }
848 /* offset to array data table is a relative branch-style offset */
849 array_data = insns + array_data_offset;
850 /* make sure the table is 32-bit aligned */
Ian Rogersef7d42f2014-01-06 12:55:46 -0800851 if ((reinterpret_cast<uintptr_t>(array_data) & 0x03) != 0) {
jeffhaod5347e02012-03-22 17:25:05 -0700852 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned array data table: at " << cur_offset
853 << ", data offset " << array_data_offset;
Ian Rogersd81871c2011-10-03 13:57:23 -0700854 return false;
855 }
856 uint32_t value_width = array_data[1];
Elliott Hughes398f64b2012-03-26 18:05:48 -0700857 uint32_t value_count = *reinterpret_cast<const uint32_t*>(&array_data[2]);
Ian Rogersd81871c2011-10-03 13:57:23 -0700858 uint32_t table_size = 4 + (value_width * value_count + 1) / 2;
859 /* make sure the end of the switch is in range */
860 if (cur_offset + array_data_offset + table_size > insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -0700861 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data end: at " << cur_offset
862 << ", data offset " << array_data_offset << ", end "
863 << cur_offset + array_data_offset + table_size
864 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -0700865 return false;
866 }
867 return true;
868}
869
Ian Rogers776ac1f2012-04-13 23:36:36 -0700870bool MethodVerifier::CheckBranchTarget(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700871 int32_t offset;
872 bool isConditional, selfOkay;
873 if (!GetBranchOffset(cur_offset, &offset, &isConditional, &selfOkay)) {
874 return false;
875 }
876 if (!selfOkay && offset == 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700877 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch offset of zero not allowed at"
878 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -0700879 return false;
880 }
Elliott Hughes81ff3182012-03-23 20:35:56 -0700881 // Check for 32-bit overflow. This isn't strictly necessary if we can depend on the runtime
882 // to have identical "wrap-around" behavior, but it's unwise to depend on that.
Ian Rogersd81871c2011-10-03 13:57:23 -0700883 if (((int64_t) cur_offset + (int64_t) offset) != (int64_t) (cur_offset + offset)) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700884 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch target overflow "
885 << reinterpret_cast<void*>(cur_offset) << " +" << offset;
Ian Rogersd81871c2011-10-03 13:57:23 -0700886 return false;
887 }
888 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
889 int32_t abs_offset = cur_offset + offset;
Brian Carlstrom93c33962013-07-26 10:37:43 -0700890 if (abs_offset < 0 ||
891 (uint32_t) abs_offset >= insn_count ||
892 !insn_flags_[abs_offset].IsOpcode()) {
jeffhaod5347e02012-03-22 17:25:05 -0700893 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid branch target " << offset << " (-> "
Elliott Hughes398f64b2012-03-26 18:05:48 -0700894 << reinterpret_cast<void*>(abs_offset) << ") at "
895 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -0700896 return false;
897 }
898 insn_flags_[abs_offset].SetBranchTarget();
899 return true;
900}
901
Ian Rogers776ac1f2012-04-13 23:36:36 -0700902bool MethodVerifier::GetBranchOffset(uint32_t cur_offset, int32_t* pOffset, bool* pConditional,
Ian Rogersd81871c2011-10-03 13:57:23 -0700903 bool* selfOkay) {
904 const uint16_t* insns = code_item_->insns_ + cur_offset;
905 *pConditional = false;
906 *selfOkay = false;
jeffhaoba5ebb92011-08-25 17:24:37 -0700907 switch (*insns & 0xff) {
908 case Instruction::GOTO:
909 *pOffset = ((int16_t) *insns) >> 8;
jeffhaoba5ebb92011-08-25 17:24:37 -0700910 break;
911 case Instruction::GOTO_32:
912 *pOffset = insns[1] | (((uint32_t) insns[2]) << 16);
jeffhaoba5ebb92011-08-25 17:24:37 -0700913 *selfOkay = true;
914 break;
915 case Instruction::GOTO_16:
916 *pOffset = (int16_t) insns[1];
jeffhaoba5ebb92011-08-25 17:24:37 -0700917 break;
918 case Instruction::IF_EQ:
919 case Instruction::IF_NE:
920 case Instruction::IF_LT:
921 case Instruction::IF_GE:
922 case Instruction::IF_GT:
923 case Instruction::IF_LE:
924 case Instruction::IF_EQZ:
925 case Instruction::IF_NEZ:
926 case Instruction::IF_LTZ:
927 case Instruction::IF_GEZ:
928 case Instruction::IF_GTZ:
929 case Instruction::IF_LEZ:
930 *pOffset = (int16_t) insns[1];
931 *pConditional = true;
jeffhaoba5ebb92011-08-25 17:24:37 -0700932 break;
933 default:
934 return false;
935 break;
936 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700937 return true;
938}
939
Ian Rogers776ac1f2012-04-13 23:36:36 -0700940bool MethodVerifier::CheckSwitchTargets(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700941 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700942 DCHECK_LT(cur_offset, insn_count);
Ian Rogersd81871c2011-10-03 13:57:23 -0700943 const uint16_t* insns = code_item_->insns_ + cur_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -0700944 /* make sure the start of the switch is in range */
Ian Rogersd81871c2011-10-03 13:57:23 -0700945 int32_t switch_offset = insns[1] | ((int32_t) insns[2]) << 16;
946 if ((int32_t) cur_offset + switch_offset < 0 || cur_offset + switch_offset + 2 >= insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -0700947 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch start: at " << cur_offset
Brian Carlstrom93c33962013-07-26 10:37:43 -0700948 << ", switch offset " << switch_offset
949 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -0700950 return false;
951 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700952 /* offset to switch table is a relative branch-style offset */
Ian Rogersd81871c2011-10-03 13:57:23 -0700953 const uint16_t* switch_insns = insns + switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -0700954 /* make sure the table is 32-bit aligned */
Ian Rogersef7d42f2014-01-06 12:55:46 -0800955 if ((reinterpret_cast<uintptr_t>(switch_insns) & 0x03) != 0) {
jeffhaod5347e02012-03-22 17:25:05 -0700956 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned switch table: at " << cur_offset
957 << ", switch offset " << switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -0700958 return false;
959 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700960 uint32_t switch_count = switch_insns[1];
961 int32_t keys_offset, targets_offset;
962 uint16_t expected_signature;
jeffhaoba5ebb92011-08-25 17:24:37 -0700963 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
964 /* 0=sig, 1=count, 2/3=firstKey */
965 targets_offset = 4;
966 keys_offset = -1;
967 expected_signature = Instruction::kPackedSwitchSignature;
968 } else {
969 /* 0=sig, 1=count, 2..count*2 = keys */
970 keys_offset = 2;
971 targets_offset = 2 + 2 * switch_count;
972 expected_signature = Instruction::kSparseSwitchSignature;
973 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700974 uint32_t table_size = targets_offset + switch_count * 2;
jeffhaoba5ebb92011-08-25 17:24:37 -0700975 if (switch_insns[0] != expected_signature) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700976 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
977 << StringPrintf("wrong signature for switch table (%x, wanted %x)",
978 switch_insns[0], expected_signature);
jeffhaoba5ebb92011-08-25 17:24:37 -0700979 return false;
980 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700981 /* make sure the end of the switch is in range */
982 if (cur_offset + switch_offset + table_size > (uint32_t) insn_count) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700983 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch end: at " << cur_offset
984 << ", switch offset " << switch_offset
985 << ", end " << (cur_offset + switch_offset + table_size)
jeffhaod5347e02012-03-22 17:25:05 -0700986 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -0700987 return false;
988 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700989 /* for a sparse switch, verify the keys are in ascending order */
990 if (keys_offset > 0 && switch_count > 1) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700991 int32_t last_key = switch_insns[keys_offset] | (switch_insns[keys_offset + 1] << 16);
992 for (uint32_t targ = 1; targ < switch_count; targ++) {
jeffhaoba5ebb92011-08-25 17:24:37 -0700993 int32_t key = (int32_t) switch_insns[keys_offset + targ * 2] |
994 (int32_t) (switch_insns[keys_offset + targ * 2 + 1] << 16);
995 if (key <= last_key) {
jeffhaod5347e02012-03-22 17:25:05 -0700996 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid packed switch: last key=" << last_key
997 << ", this=" << key;
jeffhaoba5ebb92011-08-25 17:24:37 -0700998 return false;
999 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001000 last_key = key;
1001 }
1002 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001003 /* verify each switch target */
Ian Rogersd81871c2011-10-03 13:57:23 -07001004 for (uint32_t targ = 0; targ < switch_count; targ++) {
1005 int32_t offset = (int32_t) switch_insns[targets_offset + targ * 2] |
1006 (int32_t) (switch_insns[targets_offset + targ * 2 + 1] << 16);
1007 int32_t abs_offset = cur_offset + offset;
Brian Carlstrom93c33962013-07-26 10:37:43 -07001008 if (abs_offset < 0 ||
1009 abs_offset >= (int32_t) insn_count ||
1010 !insn_flags_[abs_offset].IsOpcode()) {
1011 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch target " << offset
1012 << " (-> " << reinterpret_cast<void*>(abs_offset) << ") at "
1013 << reinterpret_cast<void*>(cur_offset)
1014 << "[" << targ << "]";
jeffhaoba5ebb92011-08-25 17:24:37 -07001015 return false;
1016 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001017 insn_flags_[abs_offset].SetBranchTarget();
1018 }
1019 return true;
1020}
1021
Ian Rogers776ac1f2012-04-13 23:36:36 -07001022bool MethodVerifier::CheckVarArgRegs(uint32_t vA, uint32_t arg[]) {
Ian Rogers29a26482014-05-02 15:27:29 -07001023 if (vA > Instruction::kMaxVarArgRegs) {
jeffhaod5347e02012-03-22 17:25:05 -07001024 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << vA << ") in non-range invoke)";
Ian Rogersd81871c2011-10-03 13:57:23 -07001025 return false;
1026 }
1027 uint16_t registers_size = code_item_->registers_size_;
1028 for (uint32_t idx = 0; idx < vA; idx++) {
jeffhao457cc512012-02-02 16:55:13 -08001029 if (arg[idx] >= registers_size) {
jeffhaod5347e02012-03-22 17:25:05 -07001030 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index (" << arg[idx]
1031 << ") in non-range invoke (>= " << registers_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001032 return false;
1033 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001034 }
1035
1036 return true;
1037}
1038
Ian Rogers776ac1f2012-04-13 23:36:36 -07001039bool MethodVerifier::CheckVarArgRangeRegs(uint32_t vA, uint32_t vC) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001040 uint16_t registers_size = code_item_->registers_size_;
1041 // vA/vC are unsigned 8-bit/16-bit quantities for /range instructions, so there's no risk of
1042 // integer overflow when adding them here.
1043 if (vA + vC > registers_size) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001044 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index " << vA << "+" << vC
1045 << " in range invoke (> " << registers_size << ")";
jeffhaoba5ebb92011-08-25 17:24:37 -07001046 return false;
1047 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001048 return true;
1049}
1050
Ian Rogers776ac1f2012-04-13 23:36:36 -07001051bool MethodVerifier::VerifyCodeFlow() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001052 uint16_t registers_size = code_item_->registers_size_;
1053 uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaobdb76512011-09-07 11:43:16 -07001054
Ian Rogersd81871c2011-10-03 13:57:23 -07001055 if (registers_size * insns_size > 4*1024*1024) {
buzbee4922ef92012-02-24 14:32:20 -08001056 LOG(WARNING) << "warning: method is huge (regs=" << registers_size
1057 << " insns_size=" << insns_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001058 }
1059 /* Create and initialize table holding register status */
Brian Carlstrom93c33962013-07-26 10:37:43 -07001060 reg_table_.Init(kTrackCompilerInterestPoints,
1061 insn_flags_.get(),
1062 insns_size,
1063 registers_size,
1064 this);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07001065
jeffhaobdb76512011-09-07 11:43:16 -07001066
Ian Rogersd0fbd852013-09-24 18:17:04 -07001067 work_line_.reset(RegisterLine::Create(registers_size, this));
1068 saved_line_.reset(RegisterLine::Create(registers_size, this));
jeffhaobdb76512011-09-07 11:43:16 -07001069
Ian Rogersd81871c2011-10-03 13:57:23 -07001070 /* Initialize register types of method arguments. */
1071 if (!SetTypesFromSignature()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001072 DCHECK_NE(failures_.size(), 0U);
1073 std::string prepend("Bad signature in ");
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001074 prepend += PrettyMethod(dex_method_idx_, *dex_file_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001075 PrependToLastFailMessage(prepend);
Ian Rogersd81871c2011-10-03 13:57:23 -07001076 return false;
1077 }
1078 /* Perform code flow verification. */
1079 if (!CodeFlowVerifyMethod()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001080 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -07001081 return false;
jeffhaobdb76512011-09-07 11:43:16 -07001082 }
jeffhaobdb76512011-09-07 11:43:16 -07001083 return true;
1084}
1085
Ian Rogersad0b3a32012-04-16 14:50:24 -07001086std::ostream& MethodVerifier::DumpFailures(std::ostream& os) {
1087 DCHECK_EQ(failures_.size(), failure_messages_.size());
Jeff Hao4137f482013-11-22 11:44:57 -08001088 for (size_t i = 0; i < failures_.size(); ++i) {
1089 os << failure_messages_[i]->str() << "\n";
Ian Rogersad0b3a32012-04-16 14:50:24 -07001090 }
1091 return os;
1092}
1093
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001094extern "C" void MethodVerifierGdbDump(MethodVerifier* v)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001095 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001096 v->Dump(std::cerr);
1097}
1098
Ian Rogers776ac1f2012-04-13 23:36:36 -07001099void MethodVerifier::Dump(std::ostream& os) {
jeffhaof56197c2012-03-05 18:01:54 -08001100 if (code_item_ == NULL) {
Elliott Hughesc073b072012-05-24 19:29:17 -07001101 os << "Native method\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001102 return;
jeffhaobdb76512011-09-07 11:43:16 -07001103 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001104 {
1105 os << "Register Types:\n";
1106 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
1107 std::ostream indent_os(&indent_filter);
1108 reg_types_.Dump(indent_os);
1109 }
Ian Rogersb4903572012-10-11 11:52:56 -07001110 os << "Dumping instructions and register lines:\n";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001111 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
1112 std::ostream indent_os(&indent_filter);
Ian Rogersd81871c2011-10-03 13:57:23 -07001113 const Instruction* inst = Instruction::At(code_item_->insns_);
1114 for (size_t dex_pc = 0; dex_pc < code_item_->insns_size_in_code_units_;
1115 dex_pc += insn_flags_[dex_pc].GetLengthInCodeUnits()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001116 RegisterLine* reg_line = reg_table_.GetLine(dex_pc);
1117 if (reg_line != NULL) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001118 indent_os << reg_line->Dump() << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07001119 }
Ian Rogers7b3ddd22013-02-21 15:19:52 -08001120 indent_os << StringPrintf("0x%04zx", dex_pc) << ": " << insn_flags_[dex_pc].ToString() << " ";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001121 const bool kDumpHexOfInstruction = false;
1122 if (kDumpHexOfInstruction) {
1123 indent_os << inst->DumpHex(5) << " ";
1124 }
1125 indent_os << inst->DumpString(dex_file_) << "\n";
jeffhaoba5ebb92011-08-25 17:24:37 -07001126 inst = inst->Next();
1127 }
jeffhaobdb76512011-09-07 11:43:16 -07001128}
1129
Ian Rogersd81871c2011-10-03 13:57:23 -07001130static bool IsPrimitiveDescriptor(char descriptor) {
1131 switch (descriptor) {
jeffhaobdb76512011-09-07 11:43:16 -07001132 case 'I':
1133 case 'C':
1134 case 'S':
1135 case 'B':
1136 case 'Z':
jeffhaobdb76512011-09-07 11:43:16 -07001137 case 'F':
1138 case 'D':
1139 case 'J':
Ian Rogersd81871c2011-10-03 13:57:23 -07001140 return true;
jeffhaobdb76512011-09-07 11:43:16 -07001141 default:
1142 return false;
1143 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001144}
1145
Ian Rogers776ac1f2012-04-13 23:36:36 -07001146bool MethodVerifier::SetTypesFromSignature() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001147 RegisterLine* reg_line = reg_table_.GetLine(0);
1148 int arg_start = code_item_->registers_size_ - code_item_->ins_size_;
1149 size_t expected_args = code_item_->ins_size_; /* long/double count as two */
jeffhaobdb76512011-09-07 11:43:16 -07001150
Ian Rogersd81871c2011-10-03 13:57:23 -07001151 DCHECK_GE(arg_start, 0); /* should have been verified earlier */
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001152 // Include the "this" pointer.
Ian Rogersd81871c2011-10-03 13:57:23 -07001153 size_t cur_arg = 0;
Ian Rogersad0b3a32012-04-16 14:50:24 -07001154 if (!IsStatic()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001155 // If this is a constructor for a class other than java.lang.Object, mark the first ("this")
1156 // argument as uninitialized. This restricts field access until the superclass constructor is
1157 // called.
Ian Rogersad0b3a32012-04-16 14:50:24 -07001158 const RegType& declaring_class = GetDeclaringClass();
1159 if (IsConstructor() && !declaring_class.IsJavaLangObject()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001160 reg_line->SetRegisterType(arg_start + cur_arg,
1161 reg_types_.UninitializedThisArgument(declaring_class));
1162 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001163 reg_line->SetRegisterType(arg_start + cur_arg, declaring_class);
jeffhaobdb76512011-09-07 11:43:16 -07001164 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001165 cur_arg++;
jeffhaobdb76512011-09-07 11:43:16 -07001166 }
1167
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001168 const DexFile::ProtoId& proto_id =
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001169 dex_file_->GetMethodPrototype(dex_file_->GetMethodId(dex_method_idx_));
Ian Rogers0571d352011-11-03 19:51:38 -07001170 DexFileParameterIterator iterator(*dex_file_, proto_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07001171
1172 for (; iterator.HasNext(); iterator.Next()) {
1173 const char* descriptor = iterator.GetDescriptor();
1174 if (descriptor == NULL) {
1175 LOG(FATAL) << "Null descriptor";
1176 }
1177 if (cur_arg >= expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07001178 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1179 << " args, found more (" << descriptor << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001180 return false;
1181 }
1182 switch (descriptor[0]) {
1183 case 'L':
1184 case '[':
1185 // We assume that reference arguments are initialized. The only way it could be otherwise
1186 // (assuming the caller was verified) is if the current method is <init>, but in that case
1187 // it's effectively considered initialized the instant we reach here (in the sense that we
1188 // can return without doing anything or call virtual methods).
1189 {
Sebastien Hertz2ed76f92014-04-22 17:11:08 +02001190 const RegType& reg_type = ResolveClassAndCheckAccess(iterator.GetTypeIdx());
1191 if (!reg_type.IsNonZeroReferenceTypes()) {
1192 DCHECK(HasFailures());
1193 return false;
1194 }
Ian Rogers84fa0742011-10-25 18:13:30 -07001195 reg_line->SetRegisterType(arg_start + cur_arg, reg_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001196 }
1197 break;
1198 case 'Z':
1199 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Boolean());
1200 break;
1201 case 'C':
1202 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Char());
1203 break;
1204 case 'B':
1205 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Byte());
1206 break;
1207 case 'I':
1208 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Integer());
1209 break;
1210 case 'S':
1211 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Short());
1212 break;
1213 case 'F':
1214 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Float());
1215 break;
1216 case 'J':
1217 case 'D': {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001218 const RegType& lo_half = descriptor[0] == 'J' ? reg_types_.LongLo() : reg_types_.DoubleLo();
1219 const RegType& hi_half = descriptor[0] == 'J' ? reg_types_.LongHi() : reg_types_.DoubleHi();
1220 reg_line->SetRegisterTypeWide(arg_start + cur_arg, lo_half, hi_half);
Ian Rogersd81871c2011-10-03 13:57:23 -07001221 cur_arg++;
1222 break;
1223 }
1224 default:
Brian Carlstrom93c33962013-07-26 10:37:43 -07001225 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected signature type char '"
1226 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001227 return false;
1228 }
1229 cur_arg++;
1230 }
1231 if (cur_arg != expected_args) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001232 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1233 << " arguments, found " << cur_arg;
Ian Rogersd81871c2011-10-03 13:57:23 -07001234 return false;
1235 }
1236 const char* descriptor = dex_file_->GetReturnTypeDescriptor(proto_id);
1237 // Validate return type. We don't do the type lookup; just want to make sure that it has the right
1238 // format. Only major difference from the method argument format is that 'V' is supported.
1239 bool result;
1240 if (IsPrimitiveDescriptor(descriptor[0]) || descriptor[0] == 'V') {
1241 result = descriptor[1] == '\0';
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001242 } else if (descriptor[0] == '[') { // single/multi-dimensional array of object/primitive
Ian Rogersd81871c2011-10-03 13:57:23 -07001243 size_t i = 0;
1244 do {
1245 i++;
1246 } while (descriptor[i] == '['); // process leading [
1247 if (descriptor[i] == 'L') { // object array
1248 do {
1249 i++; // find closing ;
1250 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1251 result = descriptor[i] == ';';
1252 } else { // primitive array
1253 result = IsPrimitiveDescriptor(descriptor[i]) && descriptor[i + 1] == '\0';
1254 }
1255 } else if (descriptor[0] == 'L') {
1256 // could be more thorough here, but shouldn't be required
1257 size_t i = 0;
1258 do {
1259 i++;
1260 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1261 result = descriptor[i] == ';';
1262 } else {
1263 result = false;
1264 }
1265 if (!result) {
jeffhaod5347e02012-03-22 17:25:05 -07001266 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected char in return type descriptor '"
1267 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001268 }
1269 return result;
jeffhaobdb76512011-09-07 11:43:16 -07001270}
1271
Ian Rogers776ac1f2012-04-13 23:36:36 -07001272bool MethodVerifier::CodeFlowVerifyMethod() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001273 const uint16_t* insns = code_item_->insns_;
1274 const uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaoba5ebb92011-08-25 17:24:37 -07001275
jeffhaobdb76512011-09-07 11:43:16 -07001276 /* Begin by marking the first instruction as "changed". */
Ian Rogersd81871c2011-10-03 13:57:23 -07001277 insn_flags_[0].SetChanged();
1278 uint32_t start_guess = 0;
jeffhaoba5ebb92011-08-25 17:24:37 -07001279
jeffhaobdb76512011-09-07 11:43:16 -07001280 /* Continue until no instructions are marked "changed". */
1281 while (true) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001282 // Find the first marked one. Use "start_guess" as a way to find one quickly.
1283 uint32_t insn_idx = start_guess;
1284 for (; insn_idx < insns_size; insn_idx++) {
1285 if (insn_flags_[insn_idx].IsChanged())
jeffhaobdb76512011-09-07 11:43:16 -07001286 break;
1287 }
jeffhaobdb76512011-09-07 11:43:16 -07001288 if (insn_idx == insns_size) {
1289 if (start_guess != 0) {
1290 /* try again, starting from the top */
1291 start_guess = 0;
1292 continue;
1293 } else {
1294 /* all flags are clear */
1295 break;
1296 }
1297 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001298 // We carry the working set of registers from instruction to instruction. If this address can
1299 // be the target of a branch (or throw) instruction, or if we're skipping around chasing
1300 // "changed" flags, we need to load the set of registers from the table.
1301 // Because we always prefer to continue on to the next instruction, we should never have a
1302 // situation where we have a stray "changed" flag set on an instruction that isn't a branch
1303 // target.
1304 work_insn_idx_ = insn_idx;
1305 if (insn_flags_[insn_idx].IsBranchTarget()) {
1306 work_line_->CopyFromLine(reg_table_.GetLine(insn_idx));
jeffhaobdb76512011-09-07 11:43:16 -07001307 } else {
1308#ifndef NDEBUG
1309 /*
1310 * Sanity check: retrieve the stored register line (assuming
1311 * a full table) and make sure it actually matches.
1312 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001313 RegisterLine* register_line = reg_table_.GetLine(insn_idx);
1314 if (register_line != NULL) {
1315 if (work_line_->CompareLine(register_line) != 0) {
1316 Dump(std::cout);
1317 std::cout << info_messages_.str();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001318 LOG(FATAL) << "work_line diverged in " << PrettyMethod(dex_method_idx_, *dex_file_)
Elliott Hughesc073b072012-05-24 19:29:17 -07001319 << "@" << reinterpret_cast<void*>(work_insn_idx_) << "\n"
1320 << " work_line=" << *work_line_ << "\n"
Elliott Hughes398f64b2012-03-26 18:05:48 -07001321 << " expected=" << *register_line;
Ian Rogersd81871c2011-10-03 13:57:23 -07001322 }
jeffhaobdb76512011-09-07 11:43:16 -07001323 }
1324#endif
1325 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001326 if (!CodeFlowVerifyInstruction(&start_guess)) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001327 std::string prepend(PrettyMethod(dex_method_idx_, *dex_file_));
Ian Rogersad0b3a32012-04-16 14:50:24 -07001328 prepend += " failed to verify: ";
1329 PrependToLastFailMessage(prepend);
jeffhaoba5ebb92011-08-25 17:24:37 -07001330 return false;
1331 }
jeffhaobdb76512011-09-07 11:43:16 -07001332 /* Clear "changed" and mark as visited. */
Ian Rogersd81871c2011-10-03 13:57:23 -07001333 insn_flags_[insn_idx].SetVisited();
1334 insn_flags_[insn_idx].ClearChanged();
jeffhaobdb76512011-09-07 11:43:16 -07001335 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001336
Andreas Gampee2256622014-06-12 14:35:51 -07001337 // When we're in compiler mode, do not accept quickened instructions.
1338 // We explicitly iterate over *all* instructions to check code that may be unreachable and
1339 // missed by the loop above.
1340 if (Runtime::Current() != nullptr && Runtime::Current()->IsCompiler()) {
1341 uint32_t insn_idx = 0;
1342 for (; insn_idx < insns_size; insn_idx += insn_flags_[insn_idx].GetLengthInCodeUnits()) {
1343 const Instruction* inst = Instruction::At(insns + insn_idx);
1344 switch (inst->Opcode()) {
1345 case Instruction::IGET_QUICK:
1346 case Instruction::IGET_WIDE_QUICK:
1347 case Instruction::IGET_OBJECT_QUICK:
1348 case Instruction::IPUT_QUICK:
1349 case Instruction::IPUT_WIDE_QUICK:
1350 case Instruction::IPUT_OBJECT_QUICK:
1351 case Instruction::INVOKE_VIRTUAL_QUICK:
1352 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK:
1353 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Quickened instructions not allowed. ";
1354 return false;
1355
1356 default:
1357 break;
1358 }
1359 }
1360 }
1361
Ian Rogers1c849e52012-06-28 14:00:33 -07001362 if (gDebugVerify) {
jeffhaobdb76512011-09-07 11:43:16 -07001363 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001364 * Scan for dead code. There's nothing "evil" about dead code
jeffhaobdb76512011-09-07 11:43:16 -07001365 * (besides the wasted space), but it indicates a flaw somewhere
1366 * down the line, possibly in the verifier.
1367 *
1368 * If we've substituted "always throw" instructions into the stream,
1369 * we are almost certainly going to have some dead code.
1370 */
1371 int dead_start = -1;
Ian Rogersd81871c2011-10-03 13:57:23 -07001372 uint32_t insn_idx = 0;
1373 for (; insn_idx < insns_size; insn_idx += insn_flags_[insn_idx].GetLengthInCodeUnits()) {
jeffhaobdb76512011-09-07 11:43:16 -07001374 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001375 * Switch-statement data doesn't get "visited" by scanner. It
jeffhaobdb76512011-09-07 11:43:16 -07001376 * may or may not be preceded by a padding NOP (for alignment).
1377 */
1378 if (insns[insn_idx] == Instruction::kPackedSwitchSignature ||
1379 insns[insn_idx] == Instruction::kSparseSwitchSignature ||
1380 insns[insn_idx] == Instruction::kArrayDataSignature ||
Elliott Hughes380aaa72012-07-09 14:33:15 -07001381 (insns[insn_idx] == Instruction::NOP && (insn_idx + 1 < insns_size) &&
jeffhaobdb76512011-09-07 11:43:16 -07001382 (insns[insn_idx + 1] == Instruction::kPackedSwitchSignature ||
1383 insns[insn_idx + 1] == Instruction::kSparseSwitchSignature ||
1384 insns[insn_idx + 1] == Instruction::kArrayDataSignature))) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001385 insn_flags_[insn_idx].SetVisited();
jeffhaobdb76512011-09-07 11:43:16 -07001386 }
1387
Ian Rogersd81871c2011-10-03 13:57:23 -07001388 if (!insn_flags_[insn_idx].IsVisited()) {
jeffhaobdb76512011-09-07 11:43:16 -07001389 if (dead_start < 0)
1390 dead_start = insn_idx;
1391 } else if (dead_start >= 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001392 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start)
1393 << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaobdb76512011-09-07 11:43:16 -07001394 dead_start = -1;
1395 }
1396 }
1397 if (dead_start >= 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001398 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start)
1399 << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaoba5ebb92011-08-25 17:24:37 -07001400 }
Ian Rogersc9e463c2013-06-05 16:52:26 -07001401 // To dump the state of the verify after a method, do something like:
1402 // if (PrettyMethod(dex_method_idx_, *dex_file_) ==
1403 // "boolean java.lang.String.equals(java.lang.Object)") {
1404 // LOG(INFO) << info_messages_.str();
1405 // }
jeffhaoba5ebb92011-08-25 17:24:37 -07001406 }
jeffhaobdb76512011-09-07 11:43:16 -07001407 return true;
1408}
1409
Ian Rogers776ac1f2012-04-13 23:36:36 -07001410bool MethodVerifier::CodeFlowVerifyInstruction(uint32_t* start_guess) {
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001411 // If we're doing FindLocksAtDexPc, check whether we're at the dex pc we care about.
1412 // We want the state _before_ the instruction, for the case where the dex pc we're
1413 // interested in is itself a monitor-enter instruction (which is a likely place
1414 // for a thread to be suspended).
1415 if (monitor_enter_dex_pcs_ != NULL && work_insn_idx_ == interesting_dex_pc_) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001416 monitor_enter_dex_pcs_->clear(); // The new work line is more accurate than the previous one.
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001417 for (size_t i = 0; i < work_line_->GetMonitorEnterCount(); ++i) {
1418 monitor_enter_dex_pcs_->push_back(work_line_->GetMonitorEnterDexPc(i));
1419 }
1420 }
1421
jeffhaobdb76512011-09-07 11:43:16 -07001422 /*
1423 * Once we finish decoding the instruction, we need to figure out where
jeffhaod1f0fde2011-09-08 17:25:33 -07001424 * we can go from here. There are three possible ways to transfer
jeffhaobdb76512011-09-07 11:43:16 -07001425 * control to another statement:
1426 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001427 * (1) Continue to the next instruction. Applies to all but
jeffhaobdb76512011-09-07 11:43:16 -07001428 * unconditional branches, method returns, and exception throws.
jeffhaod1f0fde2011-09-08 17:25:33 -07001429 * (2) Branch to one or more possible locations. Applies to branches
jeffhaobdb76512011-09-07 11:43:16 -07001430 * and switch statements.
jeffhaod1f0fde2011-09-08 17:25:33 -07001431 * (3) Exception handlers. Applies to any instruction that can
jeffhaobdb76512011-09-07 11:43:16 -07001432 * throw an exception that is handled by an encompassing "try"
1433 * block.
1434 *
1435 * We can also return, in which case there is no successor instruction
1436 * from this point.
1437 *
Elliott Hughesadb8c672012-03-06 16:49:32 -08001438 * The behavior can be determined from the opcode flags.
jeffhaobdb76512011-09-07 11:43:16 -07001439 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001440 const uint16_t* insns = code_item_->insns_ + work_insn_idx_;
1441 const Instruction* inst = Instruction::At(insns);
Ian Rogersa75a0132012-09-28 11:41:42 -07001442 int opcode_flags = Instruction::FlagsOf(inst->Opcode());
jeffhaobdb76512011-09-07 11:43:16 -07001443
jeffhaobdb76512011-09-07 11:43:16 -07001444 int32_t branch_target = 0;
jeffhaobdb76512011-09-07 11:43:16 -07001445 bool just_set_result = false;
Ian Rogers2c8a8572011-10-24 17:11:36 -07001446 if (gDebugVerify) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001447 // Generate processing back trace to debug verifier
Elliott Hughesc073b072012-05-24 19:29:17 -07001448 LogVerifyInfo() << "Processing " << inst->DumpString(dex_file_) << "\n"
1449 << *work_line_.get() << "\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001450 }
jeffhaobdb76512011-09-07 11:43:16 -07001451
1452 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001453 * Make a copy of the previous register state. If the instruction
jeffhaobdb76512011-09-07 11:43:16 -07001454 * can throw an exception, we will copy/merge this into the "catch"
1455 * address rather than work_line, because we don't want the result
1456 * from the "successful" code path (e.g. a check-cast that "improves"
1457 * a type) to be visible to the exception handler.
1458 */
Ian Rogers776ac1f2012-04-13 23:36:36 -07001459 if ((opcode_flags & Instruction::kThrow) != 0 && CurrentInsnFlags()->IsInTry()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001460 saved_line_->CopyFromLine(work_line_.get());
jeffhaobdb76512011-09-07 11:43:16 -07001461 } else {
1462#ifndef NDEBUG
Ian Rogersd81871c2011-10-03 13:57:23 -07001463 saved_line_->FillWithGarbage();
jeffhaobdb76512011-09-07 11:43:16 -07001464#endif
1465 }
1466
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07001467
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001468 // We need to ensure the work line is consistent while performing validation. When we spot a
1469 // peephole pattern we compute a new line for either the fallthrough instruction or the
1470 // branch target.
Ian Rogers700a4022014-05-19 16:49:03 -07001471 std::unique_ptr<RegisterLine> branch_line;
1472 std::unique_ptr<RegisterLine> fallthrough_line;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001473
Sebastien Hertz5243e912013-05-21 10:55:07 +02001474 switch (inst->Opcode()) {
jeffhaobdb76512011-09-07 11:43:16 -07001475 case Instruction::NOP:
1476 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001477 * A "pure" NOP has no effect on anything. Data tables start with
jeffhaobdb76512011-09-07 11:43:16 -07001478 * a signature that looks like a NOP; if we see one of these in
1479 * the course of executing code then we have a problem.
1480 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001481 if (inst->VRegA_10x() != 0) {
jeffhaod5347e02012-03-22 17:25:05 -07001482 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "encountered data table in instruction stream";
jeffhaobdb76512011-09-07 11:43:16 -07001483 }
1484 break;
1485
1486 case Instruction::MOVE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001487 work_line_->CopyRegister1(inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategory1nr);
1488 break;
jeffhaobdb76512011-09-07 11:43:16 -07001489 case Instruction::MOVE_FROM16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001490 work_line_->CopyRegister1(inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategory1nr);
1491 break;
jeffhaobdb76512011-09-07 11:43:16 -07001492 case Instruction::MOVE_16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001493 work_line_->CopyRegister1(inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategory1nr);
jeffhaobdb76512011-09-07 11:43:16 -07001494 break;
1495 case Instruction::MOVE_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001496 work_line_->CopyRegister2(inst->VRegA_12x(), inst->VRegB_12x());
1497 break;
jeffhaobdb76512011-09-07 11:43:16 -07001498 case Instruction::MOVE_WIDE_FROM16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001499 work_line_->CopyRegister2(inst->VRegA_22x(), inst->VRegB_22x());
1500 break;
jeffhaobdb76512011-09-07 11:43:16 -07001501 case Instruction::MOVE_WIDE_16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001502 work_line_->CopyRegister2(inst->VRegA_32x(), inst->VRegB_32x());
jeffhaobdb76512011-09-07 11:43:16 -07001503 break;
1504 case Instruction::MOVE_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001505 work_line_->CopyRegister1(inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategoryRef);
1506 break;
jeffhaobdb76512011-09-07 11:43:16 -07001507 case Instruction::MOVE_OBJECT_FROM16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001508 work_line_->CopyRegister1(inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategoryRef);
1509 break;
jeffhaobdb76512011-09-07 11:43:16 -07001510 case Instruction::MOVE_OBJECT_16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001511 work_line_->CopyRegister1(inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategoryRef);
jeffhaobdb76512011-09-07 11:43:16 -07001512 break;
1513
1514 /*
1515 * The move-result instructions copy data out of a "pseudo-register"
jeffhaod1f0fde2011-09-08 17:25:33 -07001516 * with the results from the last method invocation. In practice we
jeffhaobdb76512011-09-07 11:43:16 -07001517 * might want to hold the result in an actual CPU register, so the
1518 * Dalvik spec requires that these only appear immediately after an
1519 * invoke or filled-new-array.
1520 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001521 * These calls invalidate the "result" register. (This is now
jeffhaobdb76512011-09-07 11:43:16 -07001522 * redundant with the reset done below, but it can make the debug info
1523 * easier to read in some cases.)
1524 */
1525 case Instruction::MOVE_RESULT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001526 work_line_->CopyResultRegister1(inst->VRegA_11x(), false);
jeffhaobdb76512011-09-07 11:43:16 -07001527 break;
1528 case Instruction::MOVE_RESULT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001529 work_line_->CopyResultRegister2(inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07001530 break;
1531 case Instruction::MOVE_RESULT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001532 work_line_->CopyResultRegister1(inst->VRegA_11x(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001533 break;
1534
Ian Rogersd81871c2011-10-03 13:57:23 -07001535 case Instruction::MOVE_EXCEPTION: {
jeffhaobdb76512011-09-07 11:43:16 -07001536 /*
jeffhao60f83e32012-02-13 17:16:30 -08001537 * This statement can only appear as the first instruction in an exception handler. We verify
1538 * that as part of extracting the exception type from the catch block list.
jeffhaobdb76512011-09-07 11:43:16 -07001539 */
Ian Rogers28ad40d2011-10-27 15:19:26 -07001540 const RegType& res_type = GetCaughtExceptionType();
Sebastien Hertz5243e912013-05-21 10:55:07 +02001541 work_line_->SetRegisterType(inst->VRegA_11x(), res_type);
jeffhaobdb76512011-09-07 11:43:16 -07001542 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001543 }
jeffhaobdb76512011-09-07 11:43:16 -07001544 case Instruction::RETURN_VOID:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001545 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
1546 if (!GetMethodReturnType().IsConflict()) {
jeffhaod5347e02012-03-22 17:25:05 -07001547 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001548 }
jeffhaobdb76512011-09-07 11:43:16 -07001549 }
1550 break;
1551 case Instruction::RETURN:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001552 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
jeffhaobdb76512011-09-07 11:43:16 -07001553 /* check the method signature */
Ian Rogersd81871c2011-10-03 13:57:23 -07001554 const RegType& return_type = GetMethodReturnType();
1555 if (!return_type.IsCategory1Types()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001556 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected non-category 1 return type "
1557 << return_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001558 } else {
1559 // Compilers may generate synthetic functions that write byte values into boolean fields.
1560 // Also, it may use integer values for boolean, byte, short, and character return types.
Sebastien Hertz5243e912013-05-21 10:55:07 +02001561 const uint32_t vregA = inst->VRegA_11x();
1562 const RegType& src_type = work_line_->GetRegisterType(vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07001563 bool use_src = ((return_type.IsBoolean() && src_type.IsByte()) ||
1564 ((return_type.IsBoolean() || return_type.IsByte() ||
1565 return_type.IsShort() || return_type.IsChar()) &&
1566 src_type.IsInteger()));
1567 /* check the register contents */
Ian Rogersad0b3a32012-04-16 14:50:24 -07001568 bool success =
Sebastien Hertz5243e912013-05-21 10:55:07 +02001569 work_line_->VerifyRegisterType(vregA, use_src ? src_type : return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001570 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001571 AppendToLastFailMessage(StringPrintf(" return-1nr on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07001572 }
jeffhaobdb76512011-09-07 11:43:16 -07001573 }
1574 }
1575 break;
1576 case Instruction::RETURN_WIDE:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001577 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
jeffhaobdb76512011-09-07 11:43:16 -07001578 /* check the method signature */
Ian Rogersd81871c2011-10-03 13:57:23 -07001579 const RegType& return_type = GetMethodReturnType();
1580 if (!return_type.IsCategory2Types()) {
jeffhaod5347e02012-03-22 17:25:05 -07001581 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-wide not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001582 } else {
1583 /* check the register contents */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001584 const uint32_t vregA = inst->VRegA_11x();
1585 bool success = work_line_->VerifyRegisterType(vregA, return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001586 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001587 AppendToLastFailMessage(StringPrintf(" return-wide on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07001588 }
jeffhaobdb76512011-09-07 11:43:16 -07001589 }
1590 }
1591 break;
1592 case Instruction::RETURN_OBJECT:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001593 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001594 const RegType& return_type = GetMethodReturnType();
1595 if (!return_type.IsReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001596 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-object not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001597 } else {
1598 /* return_type is the *expected* return type, not register value */
1599 DCHECK(!return_type.IsZero());
1600 DCHECK(!return_type.IsUninitializedReference());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001601 const uint32_t vregA = inst->VRegA_11x();
1602 const RegType& reg_type = work_line_->GetRegisterType(vregA);
Ian Rogers9074b992011-10-26 17:41:55 -07001603 // Disallow returning uninitialized values and verify that the reference in vAA is an
1604 // instance of the "return_type"
1605 if (reg_type.IsUninitializedTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001606 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "returning uninitialized object '"
1607 << reg_type << "'";
Ian Rogers9074b992011-10-26 17:41:55 -07001608 } else if (!return_type.IsAssignableFrom(reg_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07001609 if (reg_type.IsUnresolvedTypes() || return_type.IsUnresolvedTypes()) {
1610 Fail(VERIFY_ERROR_NO_CLASS) << " can't resolve returned type '" << return_type
1611 << "' or '" << reg_type << "'";
1612 } else {
1613 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning '" << reg_type
1614 << "', but expected from declaration '" << return_type << "'";
1615 }
jeffhaobdb76512011-09-07 11:43:16 -07001616 }
1617 }
1618 }
1619 break;
1620
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001621 /* could be boolean, int, float, or a null reference */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001622 case Instruction::CONST_4: {
1623 int32_t val = static_cast<int32_t>(inst->VRegB_11n() << 28) >> 28;
Sebastien Hertz849600b2013-12-20 10:28:08 +01001624 work_line_->SetRegisterType(inst->VRegA_11n(),
Ian Rogers46960fe2014-05-23 10:43:43 -07001625 DetermineCat1Constant(val, need_precise_constants_));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001626 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02001627 }
1628 case Instruction::CONST_16: {
1629 int16_t val = static_cast<int16_t>(inst->VRegB_21s());
Sebastien Hertz849600b2013-12-20 10:28:08 +01001630 work_line_->SetRegisterType(inst->VRegA_21s(),
Ian Rogers46960fe2014-05-23 10:43:43 -07001631 DetermineCat1Constant(val, need_precise_constants_));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001632 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02001633 }
Sebastien Hertz849600b2013-12-20 10:28:08 +01001634 case Instruction::CONST: {
1635 int32_t val = inst->VRegB_31i();
Sebastien Hertz5243e912013-05-21 10:55:07 +02001636 work_line_->SetRegisterType(inst->VRegA_31i(),
Ian Rogers46960fe2014-05-23 10:43:43 -07001637 DetermineCat1Constant(val, need_precise_constants_));
jeffhaobdb76512011-09-07 11:43:16 -07001638 break;
Sebastien Hertz849600b2013-12-20 10:28:08 +01001639 }
1640 case Instruction::CONST_HIGH16: {
1641 int32_t val = static_cast<int32_t>(inst->VRegB_21h() << 16);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001642 work_line_->SetRegisterType(inst->VRegA_21h(),
Ian Rogers46960fe2014-05-23 10:43:43 -07001643 DetermineCat1Constant(val, need_precise_constants_));
jeffhaobdb76512011-09-07 11:43:16 -07001644 break;
Sebastien Hertz849600b2013-12-20 10:28:08 +01001645 }
jeffhaobdb76512011-09-07 11:43:16 -07001646 /* could be long or double; resolved upon use */
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001647 case Instruction::CONST_WIDE_16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001648 int64_t val = static_cast<int16_t>(inst->VRegB_21s());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001649 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1650 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001651 work_line_->SetRegisterTypeWide(inst->VRegA_21s(), lo, hi);
jeffhaobdb76512011-09-07 11:43:16 -07001652 break;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001653 }
1654 case Instruction::CONST_WIDE_32: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001655 int64_t val = static_cast<int32_t>(inst->VRegB_31i());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001656 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1657 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001658 work_line_->SetRegisterTypeWide(inst->VRegA_31i(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001659 break;
1660 }
1661 case Instruction::CONST_WIDE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001662 int64_t val = inst->VRegB_51l();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001663 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1664 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001665 work_line_->SetRegisterTypeWide(inst->VRegA_51l(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001666 break;
1667 }
1668 case Instruction::CONST_WIDE_HIGH16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001669 int64_t val = static_cast<uint64_t>(inst->VRegB_21h()) << 48;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001670 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1671 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001672 work_line_->SetRegisterTypeWide(inst->VRegA_21h(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001673 break;
1674 }
jeffhaobdb76512011-09-07 11:43:16 -07001675 case Instruction::CONST_STRING:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001676 work_line_->SetRegisterType(inst->VRegA_21c(), reg_types_.JavaLangString());
1677 break;
jeffhaobdb76512011-09-07 11:43:16 -07001678 case Instruction::CONST_STRING_JUMBO:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001679 work_line_->SetRegisterType(inst->VRegA_31c(), reg_types_.JavaLangString());
jeffhaobdb76512011-09-07 11:43:16 -07001680 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001681 case Instruction::CONST_CLASS: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001682 // Get type from instruction if unresolved then we need an access check
1683 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Sebastien Hertz5243e912013-05-21 10:55:07 +02001684 const RegType& res_type = ResolveClassAndCheckAccess(inst->VRegB_21c());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001685 // Register holds class, ie its type is class, on error it will hold Conflict.
Sebastien Hertz5243e912013-05-21 10:55:07 +02001686 work_line_->SetRegisterType(inst->VRegA_21c(),
Ian Rogersb4903572012-10-11 11:52:56 -07001687 res_type.IsConflict() ? res_type
1688 : reg_types_.JavaLangClass(true));
jeffhaobdb76512011-09-07 11:43:16 -07001689 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001690 }
jeffhaobdb76512011-09-07 11:43:16 -07001691 case Instruction::MONITOR_ENTER:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001692 work_line_->PushMonitor(inst->VRegA_11x(), work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07001693 break;
1694 case Instruction::MONITOR_EXIT:
1695 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001696 * monitor-exit instructions are odd. They can throw exceptions,
jeffhaobdb76512011-09-07 11:43:16 -07001697 * but when they do they act as if they succeeded and the PC is
jeffhaod1f0fde2011-09-08 17:25:33 -07001698 * pointing to the following instruction. (This behavior goes back
jeffhaobdb76512011-09-07 11:43:16 -07001699 * to the need to handle asynchronous exceptions, a now-deprecated
1700 * feature that Dalvik doesn't support.)
1701 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001702 * In practice we don't need to worry about this. The only
jeffhaobdb76512011-09-07 11:43:16 -07001703 * exceptions that can be thrown from monitor-exit are for a
jeffhaod1f0fde2011-09-08 17:25:33 -07001704 * null reference and -exit without a matching -enter. If the
jeffhaobdb76512011-09-07 11:43:16 -07001705 * structured locking checks are working, the former would have
1706 * failed on the -enter instruction, and the latter is impossible.
1707 *
1708 * This is fortunate, because issue 3221411 prevents us from
1709 * chasing the "can throw" path when monitor verification is
jeffhaod1f0fde2011-09-08 17:25:33 -07001710 * enabled. If we can fully verify the locking we can ignore
jeffhaobdb76512011-09-07 11:43:16 -07001711 * some catch blocks (which will show up as "dead" code when
1712 * we skip them here); if we can't, then the code path could be
1713 * "live" so we still need to check it.
1714 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001715 opcode_flags &= ~Instruction::kThrow;
Sebastien Hertz5243e912013-05-21 10:55:07 +02001716 work_line_->PopMonitor(inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07001717 break;
1718
Ian Rogers28ad40d2011-10-27 15:19:26 -07001719 case Instruction::CHECK_CAST:
Ian Rogersd81871c2011-10-03 13:57:23 -07001720 case Instruction::INSTANCE_OF: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001721 /*
1722 * If this instruction succeeds, we will "downcast" register vA to the type in vB. (This
1723 * could be a "upcast" -- not expected, so we don't try to address it.)
1724 *
1725 * If it fails, an exception is thrown, which we deal with later by ignoring the update to
Elliott Hughesadb8c672012-03-06 16:49:32 -08001726 * dec_insn.vA when branching to a handler.
Ian Rogers28ad40d2011-10-27 15:19:26 -07001727 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001728 const bool is_checkcast = (inst->Opcode() == Instruction::CHECK_CAST);
1729 const uint32_t type_idx = (is_checkcast) ? inst->VRegB_21c() : inst->VRegC_22c();
1730 const RegType& res_type = ResolveClassAndCheckAccess(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001731 if (res_type.IsConflict()) {
1732 DCHECK_NE(failures_.size(), 0U);
1733 if (!is_checkcast) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001734 work_line_->SetRegisterType(inst->VRegA_22c(), reg_types_.Boolean());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001735 }
1736 break; // bad class
Ian Rogers9f1ab122011-12-12 08:52:43 -08001737 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001738 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Sebastien Hertz5243e912013-05-21 10:55:07 +02001739 uint32_t orig_type_reg = (is_checkcast) ? inst->VRegA_21c() : inst->VRegB_22c();
1740 const RegType& orig_type = work_line_->GetRegisterType(orig_type_reg);
Ian Rogers28ad40d2011-10-27 15:19:26 -07001741 if (!res_type.IsNonZeroReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001742 if (is_checkcast) {
1743 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on unexpected class " << res_type;
1744 } else {
1745 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on unexpected class " << res_type;
1746 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001747 } else if (!orig_type.IsReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001748 if (is_checkcast) {
1749 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on non-reference in v" << orig_type_reg;
1750 } else {
1751 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on non-reference in v" << orig_type_reg;
1752 }
jeffhao2a8a90e2011-09-26 14:25:31 -07001753 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001754 if (is_checkcast) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001755 work_line_->SetRegisterType(inst->VRegA_21c(), res_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001756 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001757 work_line_->SetRegisterType(inst->VRegA_22c(), reg_types_.Boolean());
jeffhaobdb76512011-09-07 11:43:16 -07001758 }
jeffhaobdb76512011-09-07 11:43:16 -07001759 }
jeffhao2a8a90e2011-09-26 14:25:31 -07001760 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001761 }
1762 case Instruction::ARRAY_LENGTH: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001763 const RegType& res_type = work_line_->GetRegisterType(inst->VRegB_12x());
Ian Rogers28ad40d2011-10-27 15:19:26 -07001764 if (res_type.IsReferenceTypes()) {
Ian Rogers89310de2012-02-01 13:47:30 -08001765 if (!res_type.IsArrayTypes() && !res_type.IsZero()) { // ie not an array or null
jeffhaod5347e02012-03-22 17:25:05 -07001766 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001767 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001768 work_line_->SetRegisterType(inst->VRegA_12x(), reg_types_.Integer());
Ian Rogersd81871c2011-10-03 13:57:23 -07001769 }
1770 }
1771 break;
1772 }
1773 case Instruction::NEW_INSTANCE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001774 const RegType& res_type = ResolveClassAndCheckAccess(inst->VRegB_21c());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001775 if (res_type.IsConflict()) {
1776 DCHECK_NE(failures_.size(), 0U);
1777 break; // bad class
jeffhao8cd6dda2012-02-22 10:15:34 -08001778 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001779 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
1780 // can't create an instance of an interface or abstract class */
1781 if (!res_type.IsInstantiableTypes()) {
1782 Fail(VERIFY_ERROR_INSTANTIATION)
1783 << "new-instance on primitive, interface or abstract class" << res_type;
Ian Rogers08f753d2012-08-24 14:35:25 -07001784 // Soft failure so carry on to set register type.
Ian Rogersd81871c2011-10-03 13:57:23 -07001785 }
Ian Rogers08f753d2012-08-24 14:35:25 -07001786 const RegType& uninit_type = reg_types_.Uninitialized(res_type, work_insn_idx_);
1787 // Any registers holding previous allocations from this address that have not yet been
1788 // initialized must be marked invalid.
1789 work_line_->MarkUninitRefsAsInvalid(uninit_type);
1790 // add the new uninitialized reference to the register state
Sebastien Hertz5243e912013-05-21 10:55:07 +02001791 work_line_->SetRegisterType(inst->VRegA_21c(), uninit_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001792 break;
1793 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08001794 case Instruction::NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001795 VerifyNewArray(inst, false, false);
jeffhaobdb76512011-09-07 11:43:16 -07001796 break;
1797 case Instruction::FILLED_NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001798 VerifyNewArray(inst, true, false);
Ian Rogers0c4a5062012-02-03 15:18:59 -08001799 just_set_result = true; // Filled new array sets result register
jeffhaobdb76512011-09-07 11:43:16 -07001800 break;
Ian Rogers0c4a5062012-02-03 15:18:59 -08001801 case Instruction::FILLED_NEW_ARRAY_RANGE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001802 VerifyNewArray(inst, true, true);
Ian Rogers0c4a5062012-02-03 15:18:59 -08001803 just_set_result = true; // Filled new array range sets result register
1804 break;
jeffhaobdb76512011-09-07 11:43:16 -07001805 case Instruction::CMPL_FLOAT:
1806 case Instruction::CMPG_FLOAT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001807 if (!work_line_->VerifyRegisterType(inst->VRegB_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08001808 break;
1809 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02001810 if (!work_line_->VerifyRegisterType(inst->VRegC_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08001811 break;
1812 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02001813 work_line_->SetRegisterType(inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001814 break;
1815 case Instruction::CMPL_DOUBLE:
1816 case Instruction::CMPG_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001817 if (!work_line_->VerifyRegisterTypeWide(inst->VRegB_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001818 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001819 break;
1820 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02001821 if (!work_line_->VerifyRegisterTypeWide(inst->VRegC_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001822 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001823 break;
1824 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02001825 work_line_->SetRegisterType(inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001826 break;
1827 case Instruction::CMP_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001828 if (!work_line_->VerifyRegisterTypeWide(inst->VRegB_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001829 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001830 break;
1831 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02001832 if (!work_line_->VerifyRegisterTypeWide(inst->VRegC_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001833 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001834 break;
1835 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02001836 work_line_->SetRegisterType(inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001837 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001838 case Instruction::THROW: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001839 const RegType& res_type = work_line_->GetRegisterType(inst->VRegA_11x());
Ian Rogersb4903572012-10-11 11:52:56 -07001840 if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(res_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07001841 Fail(res_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS : VERIFY_ERROR_BAD_CLASS_SOFT)
1842 << "thrown class " << res_type << " not instanceof Throwable";
jeffhaobdb76512011-09-07 11:43:16 -07001843 }
1844 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001845 }
jeffhaobdb76512011-09-07 11:43:16 -07001846 case Instruction::GOTO:
1847 case Instruction::GOTO_16:
1848 case Instruction::GOTO_32:
1849 /* no effect on or use of registers */
1850 break;
1851
1852 case Instruction::PACKED_SWITCH:
1853 case Instruction::SPARSE_SWITCH:
1854 /* verify that vAA is an integer, or can be converted to one */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001855 work_line_->VerifyRegisterType(inst->VRegA_31t(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001856 break;
1857
Ian Rogersd81871c2011-10-03 13:57:23 -07001858 case Instruction::FILL_ARRAY_DATA: {
1859 /* Similar to the verification done for APUT */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001860 const RegType& array_type = work_line_->GetRegisterType(inst->VRegA_31t());
Ian Rogers89310de2012-02-01 13:47:30 -08001861 /* array_type can be null if the reg type is Zero */
1862 if (!array_type.IsZero()) {
jeffhao457cc512012-02-02 16:55:13 -08001863 if (!array_type.IsArrayTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001864 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with array type "
1865 << array_type;
Ian Rogers89310de2012-02-01 13:47:30 -08001866 } else {
Mathieu Chartier590fee92013-09-13 13:46:47 -07001867 const RegType& component_type = reg_types_.GetComponentType(array_type,
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001868 class_loader_->Get());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001869 DCHECK(!component_type.IsConflict());
jeffhao457cc512012-02-02 16:55:13 -08001870 if (component_type.IsNonZeroReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001871 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with component type "
1872 << component_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001873 } else {
jeffhao457cc512012-02-02 16:55:13 -08001874 // Now verify if the element width in the table matches the element width declared in
1875 // the array
1876 const uint16_t* array_data = insns + (insns[1] | (((int32_t) insns[2]) << 16));
1877 if (array_data[0] != Instruction::kArrayDataSignature) {
jeffhaod5347e02012-03-22 17:25:05 -07001878 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid magic for array-data";
jeffhao457cc512012-02-02 16:55:13 -08001879 } else {
1880 size_t elem_width = Primitive::ComponentSize(component_type.GetPrimitiveType());
1881 // Since we don't compress the data in Dex, expect to see equal width of data stored
1882 // in the table and expected from the array class.
1883 if (array_data[1] != elem_width) {
jeffhaod5347e02012-03-22 17:25:05 -07001884 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-data size mismatch (" << array_data[1]
1885 << " vs " << elem_width << ")";
jeffhao457cc512012-02-02 16:55:13 -08001886 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001887 }
1888 }
jeffhaobdb76512011-09-07 11:43:16 -07001889 }
1890 }
1891 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001892 }
jeffhaobdb76512011-09-07 11:43:16 -07001893 case Instruction::IF_EQ:
Ian Rogersd81871c2011-10-03 13:57:23 -07001894 case Instruction::IF_NE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001895 const RegType& reg_type1 = work_line_->GetRegisterType(inst->VRegA_22t());
1896 const RegType& reg_type2 = work_line_->GetRegisterType(inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07001897 bool mismatch = false;
1898 if (reg_type1.IsZero()) { // zero then integral or reference expected
1899 mismatch = !reg_type2.IsReferenceTypes() && !reg_type2.IsIntegralTypes();
1900 } else if (reg_type1.IsReferenceTypes()) { // both references?
1901 mismatch = !reg_type2.IsReferenceTypes();
1902 } else { // both integral?
1903 mismatch = !reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes();
1904 }
1905 if (mismatch) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001906 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to if-eq/if-ne (" << reg_type1 << ","
1907 << reg_type2 << ") must both be references or integral";
jeffhaobdb76512011-09-07 11:43:16 -07001908 }
1909 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001910 }
jeffhaobdb76512011-09-07 11:43:16 -07001911 case Instruction::IF_LT:
1912 case Instruction::IF_GE:
1913 case Instruction::IF_GT:
Ian Rogersd81871c2011-10-03 13:57:23 -07001914 case Instruction::IF_LE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001915 const RegType& reg_type1 = work_line_->GetRegisterType(inst->VRegA_22t());
1916 const RegType& reg_type2 = work_line_->GetRegisterType(inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07001917 if (!reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001918 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to 'if' (" << reg_type1 << ","
1919 << reg_type2 << ") must be integral";
jeffhaobdb76512011-09-07 11:43:16 -07001920 }
1921 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001922 }
jeffhaobdb76512011-09-07 11:43:16 -07001923 case Instruction::IF_EQZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07001924 case Instruction::IF_NEZ: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001925 const RegType& reg_type = work_line_->GetRegisterType(inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07001926 if (!reg_type.IsReferenceTypes() && !reg_type.IsIntegralTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001927 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
1928 << " unexpected as arg to if-eqz/if-nez";
Ian Rogersd81871c2011-10-03 13:57:23 -07001929 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001930
1931 // Find previous instruction - its existence is a precondition to peephole optimization.
Ian Rogers9b360392013-06-06 14:45:07 -07001932 uint32_t instance_of_idx = 0;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001933 if (0 != work_insn_idx_) {
Ian Rogers9b360392013-06-06 14:45:07 -07001934 instance_of_idx = work_insn_idx_ - 1;
Brian Carlstromdf629502013-07-17 22:39:56 -07001935 while (0 != instance_of_idx && !insn_flags_[instance_of_idx].IsOpcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07001936 instance_of_idx--;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001937 }
Ian Rogers9b360392013-06-06 14:45:07 -07001938 CHECK(insn_flags_[instance_of_idx].IsOpcode());
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001939 } else {
1940 break;
1941 }
1942
Ian Rogers9b360392013-06-06 14:45:07 -07001943 const Instruction* instance_of_inst = Instruction::At(code_item_->insns_ + instance_of_idx);
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001944
1945 /* Check for peep-hole pattern of:
1946 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07001947 * instance-of vX, vY, T;
1948 * ifXXX vX, label ;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001949 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07001950 * label:
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001951 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07001952 * and sharpen the type of vY to be type T.
1953 * Note, this pattern can't be if:
1954 * - if there are other branches to this branch,
1955 * - when vX == vY.
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001956 */
Ian Rogersfae370a2013-06-05 08:33:27 -07001957 if (!CurrentInsnFlags()->IsBranchTarget() &&
Ian Rogers9b360392013-06-06 14:45:07 -07001958 (Instruction::INSTANCE_OF == instance_of_inst->Opcode()) &&
1959 (inst->VRegA_21t() == instance_of_inst->VRegA_22c()) &&
1960 (instance_of_inst->VRegA_22c() != instance_of_inst->VRegB_22c())) {
Ian Rogersfae370a2013-06-05 08:33:27 -07001961 // Check that the we are not attempting conversion to interface types,
1962 // which is not done because of the multiple inheritance implications.
Jeff Haoc642ec82013-09-04 16:11:55 -07001963 // Also don't change the type if it would result in an upcast.
1964 const RegType& orig_type = work_line_->GetRegisterType(instance_of_inst->VRegB_22c());
Ian Rogers9b360392013-06-06 14:45:07 -07001965 const RegType& cast_type = ResolveClassAndCheckAccess(instance_of_inst->VRegC_22c());
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001966
Jeff Haoa3faaf42013-09-03 19:07:00 -07001967 if (!cast_type.IsUnresolvedTypes() && !orig_type.IsUnresolvedTypes() &&
1968 !cast_type.GetClass()->IsInterface() && !cast_type.IsAssignableFrom(orig_type)) {
Ian Rogersd0fbd852013-09-24 18:17:04 -07001969 RegisterLine* update_line = RegisterLine::Create(code_item_->registers_size_, this);
Ian Rogersfae370a2013-06-05 08:33:27 -07001970 if (inst->Opcode() == Instruction::IF_EQZ) {
Ian Rogers9b360392013-06-06 14:45:07 -07001971 fallthrough_line.reset(update_line);
Ian Rogersfae370a2013-06-05 08:33:27 -07001972 } else {
Ian Rogers9b360392013-06-06 14:45:07 -07001973 branch_line.reset(update_line);
1974 }
1975 update_line->CopyFromLine(work_line_.get());
1976 update_line->SetRegisterType(instance_of_inst->VRegB_22c(), cast_type);
1977 if (!insn_flags_[instance_of_idx].IsBranchTarget() && 0 != instance_of_idx) {
1978 // See if instance-of was preceded by a move-object operation, common due to the small
1979 // register encoding space of instance-of, and propagate type information to the source
1980 // of the move-object.
1981 uint32_t move_idx = instance_of_idx - 1;
Brian Carlstromdf629502013-07-17 22:39:56 -07001982 while (0 != move_idx && !insn_flags_[move_idx].IsOpcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07001983 move_idx--;
1984 }
1985 CHECK(insn_flags_[move_idx].IsOpcode());
1986 const Instruction* move_inst = Instruction::At(code_item_->insns_ + move_idx);
1987 switch (move_inst->Opcode()) {
1988 case Instruction::MOVE_OBJECT:
1989 if (move_inst->VRegA_12x() == instance_of_inst->VRegB_22c()) {
1990 update_line->SetRegisterType(move_inst->VRegB_12x(), cast_type);
1991 }
1992 break;
1993 case Instruction::MOVE_OBJECT_FROM16:
1994 if (move_inst->VRegA_22x() == instance_of_inst->VRegB_22c()) {
1995 update_line->SetRegisterType(move_inst->VRegB_22x(), cast_type);
1996 }
1997 break;
1998 case Instruction::MOVE_OBJECT_16:
1999 if (move_inst->VRegA_32x() == instance_of_inst->VRegB_22c()) {
2000 update_line->SetRegisterType(move_inst->VRegB_32x(), cast_type);
2001 }
2002 break;
2003 default:
2004 break;
2005 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002006 }
2007 }
2008 }
2009
jeffhaobdb76512011-09-07 11:43:16 -07002010 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002011 }
jeffhaobdb76512011-09-07 11:43:16 -07002012 case Instruction::IF_LTZ:
2013 case Instruction::IF_GEZ:
2014 case Instruction::IF_GTZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002015 case Instruction::IF_LEZ: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002016 const RegType& reg_type = work_line_->GetRegisterType(inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002017 if (!reg_type.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002018 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
2019 << " unexpected as arg to if-ltz/if-gez/if-gtz/if-lez";
Ian Rogersd81871c2011-10-03 13:57:23 -07002020 }
jeffhaobdb76512011-09-07 11:43:16 -07002021 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002022 }
jeffhaobdb76512011-09-07 11:43:16 -07002023 case Instruction::AGET_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002024 VerifyAGet(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002025 break;
jeffhaobdb76512011-09-07 11:43:16 -07002026 case Instruction::AGET_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002027 VerifyAGet(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002028 break;
jeffhaobdb76512011-09-07 11:43:16 -07002029 case Instruction::AGET_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002030 VerifyAGet(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002031 break;
jeffhaobdb76512011-09-07 11:43:16 -07002032 case Instruction::AGET_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002033 VerifyAGet(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002034 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002035 case Instruction::AGET:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002036 VerifyAGet(inst, reg_types_.Integer(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002037 break;
jeffhaobdb76512011-09-07 11:43:16 -07002038 case Instruction::AGET_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002039 VerifyAGet(inst, reg_types_.LongLo(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002040 break;
2041 case Instruction::AGET_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002042 VerifyAGet(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07002043 break;
2044
Ian Rogersd81871c2011-10-03 13:57:23 -07002045 case Instruction::APUT_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002046 VerifyAPut(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002047 break;
2048 case Instruction::APUT_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002049 VerifyAPut(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002050 break;
2051 case Instruction::APUT_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002052 VerifyAPut(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002053 break;
2054 case Instruction::APUT_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002055 VerifyAPut(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002056 break;
2057 case Instruction::APUT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002058 VerifyAPut(inst, reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002059 break;
2060 case Instruction::APUT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002061 VerifyAPut(inst, reg_types_.LongLo(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002062 break;
2063 case Instruction::APUT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002064 VerifyAPut(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07002065 break;
2066
jeffhaobdb76512011-09-07 11:43:16 -07002067 case Instruction::IGET_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002068 VerifyISGet(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002069 break;
jeffhaobdb76512011-09-07 11:43:16 -07002070 case Instruction::IGET_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002071 VerifyISGet(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002072 break;
jeffhaobdb76512011-09-07 11:43:16 -07002073 case Instruction::IGET_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002074 VerifyISGet(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002075 break;
jeffhaobdb76512011-09-07 11:43:16 -07002076 case Instruction::IGET_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002077 VerifyISGet(inst, reg_types_.Short(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002078 break;
2079 case Instruction::IGET:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002080 VerifyISGet(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002081 break;
2082 case Instruction::IGET_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002083 VerifyISGet(inst, reg_types_.LongLo(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002084 break;
2085 case Instruction::IGET_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002086 VerifyISGet(inst, reg_types_.JavaLangObject(false), false, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002087 break;
jeffhaobdb76512011-09-07 11:43:16 -07002088
Ian Rogersd81871c2011-10-03 13:57:23 -07002089 case Instruction::IPUT_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002090 VerifyISPut(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002091 break;
2092 case Instruction::IPUT_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002093 VerifyISPut(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002094 break;
2095 case Instruction::IPUT_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002096 VerifyISPut(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002097 break;
2098 case Instruction::IPUT_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002099 VerifyISPut(inst, reg_types_.Short(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002100 break;
2101 case Instruction::IPUT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002102 VerifyISPut(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002103 break;
2104 case Instruction::IPUT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002105 VerifyISPut(inst, reg_types_.LongLo(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002106 break;
jeffhaobdb76512011-09-07 11:43:16 -07002107 case Instruction::IPUT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002108 VerifyISPut(inst, reg_types_.JavaLangObject(false), false, false);
jeffhaobdb76512011-09-07 11:43:16 -07002109 break;
2110
jeffhaobdb76512011-09-07 11:43:16 -07002111 case Instruction::SGET_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002112 VerifyISGet(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002113 break;
jeffhaobdb76512011-09-07 11:43:16 -07002114 case Instruction::SGET_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002115 VerifyISGet(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002116 break;
jeffhaobdb76512011-09-07 11:43:16 -07002117 case Instruction::SGET_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002118 VerifyISGet(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002119 break;
jeffhaobdb76512011-09-07 11:43:16 -07002120 case Instruction::SGET_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002121 VerifyISGet(inst, reg_types_.Short(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002122 break;
2123 case Instruction::SGET:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002124 VerifyISGet(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002125 break;
2126 case Instruction::SGET_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002127 VerifyISGet(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002128 break;
2129 case Instruction::SGET_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002130 VerifyISGet(inst, reg_types_.JavaLangObject(false), false, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002131 break;
2132
2133 case Instruction::SPUT_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002134 VerifyISPut(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002135 break;
2136 case Instruction::SPUT_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002137 VerifyISPut(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002138 break;
2139 case Instruction::SPUT_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002140 VerifyISPut(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002141 break;
2142 case Instruction::SPUT_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002143 VerifyISPut(inst, reg_types_.Short(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002144 break;
2145 case Instruction::SPUT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002146 VerifyISPut(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002147 break;
2148 case Instruction::SPUT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002149 VerifyISPut(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002150 break;
2151 case Instruction::SPUT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002152 VerifyISPut(inst, reg_types_.JavaLangObject(false), false, true);
jeffhaobdb76512011-09-07 11:43:16 -07002153 break;
2154
2155 case Instruction::INVOKE_VIRTUAL:
2156 case Instruction::INVOKE_VIRTUAL_RANGE:
2157 case Instruction::INVOKE_SUPER:
Ian Rogersd81871c2011-10-03 13:57:23 -07002158 case Instruction::INVOKE_SUPER_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002159 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE ||
2160 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002161 bool is_super = (inst->Opcode() == Instruction::INVOKE_SUPER ||
2162 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
Andreas Gampeacc4d2f2014-06-12 19:35:05 -07002163 mirror::ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_VIRTUAL, is_range,
2164 is_super);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002165 const RegType* return_type = nullptr;
2166 if (called_method != nullptr) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002167 Thread* self = Thread::Current();
2168 StackHandleScope<1> hs(self);
2169 Handle<mirror::ArtMethod> h_called_method(hs.NewHandle(called_method));
2170 MethodHelper mh(h_called_method);
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08002171 mirror::Class* return_type_class = mh.GetReturnType(can_load_classes_);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002172 if (return_type_class != nullptr) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002173 return_type = &reg_types_.FromClass(h_called_method->GetReturnTypeDescriptor(),
2174 return_type_class,
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002175 return_type_class->CannotBeAssignedFromOtherTypes());
2176 } else {
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08002177 DCHECK(!can_load_classes_ || self->IsExceptionPending());
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002178 self->ClearException();
2179 }
2180 }
2181 if (return_type == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002182 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002183 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2184 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002185 const char* descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07002186 return_type = &reg_types_.FromDescriptor(class_loader_->Get(), descriptor, false);
jeffhaobdb76512011-09-07 11:43:16 -07002187 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002188 if (!return_type->IsLowHalf()) {
2189 work_line_->SetResultRegisterType(*return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002190 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002191 work_line_->SetResultRegisterTypeWide(*return_type, return_type->HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002192 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002193 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002194 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002195 }
jeffhaobdb76512011-09-07 11:43:16 -07002196 case Instruction::INVOKE_DIRECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002197 case Instruction::INVOKE_DIRECT_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002198 bool is_range = (inst->Opcode() == Instruction::INVOKE_DIRECT_RANGE);
Brian Carlstromea46f952013-07-30 01:26:50 -07002199 mirror::ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_DIRECT,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002200 is_range, false);
Ian Rogers46685432012-06-03 22:26:43 -07002201 const char* return_type_descriptor;
2202 bool is_constructor;
2203 if (called_method == NULL) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002204 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogers46685432012-06-03 22:26:43 -07002205 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
Ian Rogersdfb325e2013-10-30 01:00:44 -07002206 is_constructor = strcmp("<init>", dex_file_->StringDataByIdx(method_id.name_idx_)) == 0;
Ian Rogers46685432012-06-03 22:26:43 -07002207 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
2208 return_type_descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2209 } else {
2210 is_constructor = called_method->IsConstructor();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002211 return_type_descriptor = called_method->GetReturnTypeDescriptor();
Ian Rogers46685432012-06-03 22:26:43 -07002212 }
2213 if (is_constructor) {
jeffhaobdb76512011-09-07 11:43:16 -07002214 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002215 * Some additional checks when calling a constructor. We know from the invocation arg check
2216 * that the "this" argument is an instance of called_method->klass. Now we further restrict
2217 * that to require that called_method->klass is the same as this->klass or this->super,
2218 * allowing the latter only if the "this" argument is the same as the "this" argument to
2219 * this method (which implies that we're in a constructor ourselves).
jeffhaobdb76512011-09-07 11:43:16 -07002220 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002221 const RegType& this_type = work_line_->GetInvocationThis(inst, is_range);
jeffhaob57e9522012-04-26 18:08:21 -07002222 if (this_type.IsConflict()) // failure.
2223 break;
jeffhaobdb76512011-09-07 11:43:16 -07002224
jeffhaob57e9522012-04-26 18:08:21 -07002225 /* no null refs allowed (?) */
2226 if (this_type.IsZero()) {
2227 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unable to initialize null ref";
2228 break;
jeffhao2a8a90e2011-09-26 14:25:31 -07002229 }
jeffhaob57e9522012-04-26 18:08:21 -07002230
2231 /* must be in same class or in superclass */
Ian Rogers46685432012-06-03 22:26:43 -07002232 // const RegType& this_super_klass = this_type.GetSuperClass(&reg_types_);
2233 // TODO: re-enable constructor type verification
2234 // if (this_super_klass.IsConflict()) {
jeffhaob57e9522012-04-26 18:08:21 -07002235 // Unknown super class, fail so we re-check at runtime.
Ian Rogers46685432012-06-03 22:26:43 -07002236 // Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "super class unknown for '" << this_type << "'";
2237 // break;
2238 // }
jeffhaob57e9522012-04-26 18:08:21 -07002239
2240 /* arg must be an uninitialized reference */
2241 if (!this_type.IsUninitializedTypes()) {
2242 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Expected initialization on uninitialized reference "
2243 << this_type;
2244 break;
2245 }
2246
2247 /*
2248 * Replace the uninitialized reference with an initialized one. We need to do this for all
2249 * registers that have the same object instance in them, not just the "this" register.
2250 */
2251 work_line_->MarkRefsAsInitialized(this_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002252 }
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07002253 const RegType& return_type = reg_types_.FromDescriptor(class_loader_->Get(),
Mathieu Chartier590fee92013-09-13 13:46:47 -07002254 return_type_descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002255 if (!return_type.IsLowHalf()) {
2256 work_line_->SetResultRegisterType(return_type);
2257 } else {
2258 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2259 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002260 just_set_result = true;
2261 break;
2262 }
2263 case Instruction::INVOKE_STATIC:
2264 case Instruction::INVOKE_STATIC_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002265 bool is_range = (inst->Opcode() == Instruction::INVOKE_STATIC_RANGE);
Brian Carlstromea46f952013-07-30 01:26:50 -07002266 mirror::ArtMethod* called_method = VerifyInvocationArgs(inst,
Brian Carlstrom93c33962013-07-26 10:37:43 -07002267 METHOD_STATIC,
2268 is_range,
2269 false);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002270 const char* descriptor;
2271 if (called_method == NULL) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002272 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002273 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2274 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Mathieu Chartier590fee92013-09-13 13:46:47 -07002275 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002276 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002277 descriptor = called_method->GetReturnTypeDescriptor();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002278 }
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002279 const RegType& return_type = reg_types_.FromDescriptor(class_loader_->Get(), descriptor,
2280 false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002281 if (!return_type.IsLowHalf()) {
2282 work_line_->SetResultRegisterType(return_type);
2283 } else {
2284 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2285 }
jeffhaobdb76512011-09-07 11:43:16 -07002286 just_set_result = true;
2287 }
2288 break;
jeffhaobdb76512011-09-07 11:43:16 -07002289 case Instruction::INVOKE_INTERFACE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002290 case Instruction::INVOKE_INTERFACE_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002291 bool is_range = (inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE);
Brian Carlstromea46f952013-07-30 01:26:50 -07002292 mirror::ArtMethod* abs_method = VerifyInvocationArgs(inst,
Brian Carlstrom93c33962013-07-26 10:37:43 -07002293 METHOD_INTERFACE,
2294 is_range,
2295 false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002296 if (abs_method != NULL) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002297 mirror::Class* called_interface = abs_method->GetDeclaringClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002298 if (!called_interface->IsInterface() && !called_interface->IsObjectClass()) {
2299 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected interface class in invoke-interface '"
2300 << PrettyMethod(abs_method) << "'";
2301 break;
Ian Rogers28ad40d2011-10-27 15:19:26 -07002302 }
Ian Rogers0d604842012-04-16 14:50:24 -07002303 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002304 /* Get the type of the "this" arg, which should either be a sub-interface of called
2305 * interface or Object (see comments in RegType::JoinClass).
2306 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002307 const RegType& this_type = work_line_->GetInvocationThis(inst, is_range);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002308 if (this_type.IsZero()) {
2309 /* null pointer always passes (and always fails at runtime) */
2310 } else {
2311 if (this_type.IsUninitializedTypes()) {
2312 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface call on uninitialized object "
2313 << this_type;
2314 break;
2315 }
2316 // In the past we have tried to assert that "called_interface" is assignable
2317 // from "this_type.GetClass()", however, as we do an imprecise Join
2318 // (RegType::JoinClass) we don't have full information on what interfaces are
2319 // implemented by "this_type". For example, two classes may implement the same
2320 // interfaces and have a common parent that doesn't implement the interface. The
2321 // join will set "this_type" to the parent class and a test that this implements
2322 // the interface will incorrectly fail.
2323 }
2324 /*
2325 * We don't have an object instance, so we can't find the concrete method. However, all of
2326 * the type information is in the abstract method, so we're good.
2327 */
2328 const char* descriptor;
2329 if (abs_method == NULL) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002330 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002331 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2332 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
2333 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2334 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002335 descriptor = abs_method->GetReturnTypeDescriptor();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002336 }
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07002337 const RegType& return_type = reg_types_.FromDescriptor(class_loader_->Get(), descriptor,
Mathieu Chartier590fee92013-09-13 13:46:47 -07002338 false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002339 if (!return_type.IsLowHalf()) {
2340 work_line_->SetResultRegisterType(return_type);
2341 } else {
2342 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2343 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002344 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002345 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002346 }
jeffhaobdb76512011-09-07 11:43:16 -07002347 case Instruction::NEG_INT:
2348 case Instruction::NOT_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002349 work_line_->CheckUnaryOp(inst, reg_types_.Integer(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002350 break;
2351 case Instruction::NEG_LONG:
2352 case Instruction::NOT_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002353 work_line_->CheckUnaryOpWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
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::NEG_FLOAT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002357 work_line_->CheckUnaryOp(inst, reg_types_.Float(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002358 break;
2359 case Instruction::NEG_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002360 work_line_->CheckUnaryOpWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002361 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002362 break;
2363 case Instruction::INT_TO_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002364 work_line_->CheckUnaryOpToWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002365 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002366 break;
2367 case Instruction::INT_TO_FLOAT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002368 work_line_->CheckUnaryOp(inst, reg_types_.Float(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002369 break;
2370 case Instruction::INT_TO_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002371 work_line_->CheckUnaryOpToWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002372 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002373 break;
2374 case Instruction::LONG_TO_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002375 work_line_->CheckUnaryOpFromWide(inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002376 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002377 break;
2378 case Instruction::LONG_TO_FLOAT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002379 work_line_->CheckUnaryOpFromWide(inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002380 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002381 break;
2382 case Instruction::LONG_TO_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002383 work_line_->CheckUnaryOpWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002384 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002385 break;
2386 case Instruction::FLOAT_TO_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002387 work_line_->CheckUnaryOp(inst, reg_types_.Integer(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002388 break;
2389 case Instruction::FLOAT_TO_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002390 work_line_->CheckUnaryOpToWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002391 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002392 break;
2393 case Instruction::FLOAT_TO_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002394 work_line_->CheckUnaryOpToWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002395 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002396 break;
2397 case Instruction::DOUBLE_TO_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002398 work_line_->CheckUnaryOpFromWide(inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002399 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002400 break;
2401 case Instruction::DOUBLE_TO_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002402 work_line_->CheckUnaryOpWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002403 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002404 break;
2405 case Instruction::DOUBLE_TO_FLOAT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002406 work_line_->CheckUnaryOpFromWide(inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002407 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002408 break;
2409 case Instruction::INT_TO_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002410 work_line_->CheckUnaryOp(inst, reg_types_.Byte(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002411 break;
2412 case Instruction::INT_TO_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002413 work_line_->CheckUnaryOp(inst, reg_types_.Char(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002414 break;
2415 case Instruction::INT_TO_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002416 work_line_->CheckUnaryOp(inst, reg_types_.Short(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002417 break;
2418
2419 case Instruction::ADD_INT:
2420 case Instruction::SUB_INT:
2421 case Instruction::MUL_INT:
2422 case Instruction::REM_INT:
2423 case Instruction::DIV_INT:
2424 case Instruction::SHL_INT:
2425 case Instruction::SHR_INT:
2426 case Instruction::USHR_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002427 work_line_->CheckBinaryOp(inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002428 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002429 break;
2430 case Instruction::AND_INT:
2431 case Instruction::OR_INT:
2432 case Instruction::XOR_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002433 work_line_->CheckBinaryOp(inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002434 reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002435 break;
2436 case Instruction::ADD_LONG:
2437 case Instruction::SUB_LONG:
2438 case Instruction::MUL_LONG:
2439 case Instruction::DIV_LONG:
2440 case Instruction::REM_LONG:
2441 case Instruction::AND_LONG:
2442 case Instruction::OR_LONG:
2443 case Instruction::XOR_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002444 work_line_->CheckBinaryOpWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002445 reg_types_.LongLo(), reg_types_.LongHi(),
2446 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002447 break;
2448 case Instruction::SHL_LONG:
2449 case Instruction::SHR_LONG:
2450 case Instruction::USHR_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002451 /* shift distance is Int, making these different from other binary operations */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002452 work_line_->CheckBinaryOpWideShift(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002453 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002454 break;
2455 case Instruction::ADD_FLOAT:
2456 case Instruction::SUB_FLOAT:
2457 case Instruction::MUL_FLOAT:
2458 case Instruction::DIV_FLOAT:
2459 case Instruction::REM_FLOAT:
Brian Carlstrom93c33962013-07-26 10:37:43 -07002460 work_line_->CheckBinaryOp(inst,
2461 reg_types_.Float(),
2462 reg_types_.Float(),
2463 reg_types_.Float(),
2464 false);
jeffhaobdb76512011-09-07 11:43:16 -07002465 break;
2466 case Instruction::ADD_DOUBLE:
2467 case Instruction::SUB_DOUBLE:
2468 case Instruction::MUL_DOUBLE:
2469 case Instruction::DIV_DOUBLE:
2470 case Instruction::REM_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002471 work_line_->CheckBinaryOpWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002472 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2473 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002474 break;
2475 case Instruction::ADD_INT_2ADDR:
2476 case Instruction::SUB_INT_2ADDR:
2477 case Instruction::MUL_INT_2ADDR:
2478 case Instruction::REM_INT_2ADDR:
2479 case Instruction::SHL_INT_2ADDR:
2480 case Instruction::SHR_INT_2ADDR:
2481 case Instruction::USHR_INT_2ADDR:
Brian Carlstrom93c33962013-07-26 10:37:43 -07002482 work_line_->CheckBinaryOp2addr(inst,
2483 reg_types_.Integer(),
2484 reg_types_.Integer(),
2485 reg_types_.Integer(),
2486 false);
jeffhaobdb76512011-09-07 11:43:16 -07002487 break;
2488 case Instruction::AND_INT_2ADDR:
2489 case Instruction::OR_INT_2ADDR:
2490 case Instruction::XOR_INT_2ADDR:
Brian Carlstrom93c33962013-07-26 10:37:43 -07002491 work_line_->CheckBinaryOp2addr(inst,
2492 reg_types_.Integer(),
2493 reg_types_.Integer(),
2494 reg_types_.Integer(),
2495 true);
jeffhaobdb76512011-09-07 11:43:16 -07002496 break;
2497 case Instruction::DIV_INT_2ADDR:
Brian Carlstrom93c33962013-07-26 10:37:43 -07002498 work_line_->CheckBinaryOp2addr(inst,
2499 reg_types_.Integer(),
2500 reg_types_.Integer(),
2501 reg_types_.Integer(),
2502 false);
jeffhaobdb76512011-09-07 11:43:16 -07002503 break;
2504 case Instruction::ADD_LONG_2ADDR:
2505 case Instruction::SUB_LONG_2ADDR:
2506 case Instruction::MUL_LONG_2ADDR:
2507 case Instruction::DIV_LONG_2ADDR:
2508 case Instruction::REM_LONG_2ADDR:
2509 case Instruction::AND_LONG_2ADDR:
2510 case Instruction::OR_LONG_2ADDR:
2511 case Instruction::XOR_LONG_2ADDR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002512 work_line_->CheckBinaryOp2addrWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002513 reg_types_.LongLo(), reg_types_.LongHi(),
2514 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002515 break;
2516 case Instruction::SHL_LONG_2ADDR:
2517 case Instruction::SHR_LONG_2ADDR:
2518 case Instruction::USHR_LONG_2ADDR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002519 work_line_->CheckBinaryOp2addrWideShift(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002520 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002521 break;
2522 case Instruction::ADD_FLOAT_2ADDR:
2523 case Instruction::SUB_FLOAT_2ADDR:
2524 case Instruction::MUL_FLOAT_2ADDR:
2525 case Instruction::DIV_FLOAT_2ADDR:
2526 case Instruction::REM_FLOAT_2ADDR:
Brian Carlstrom93c33962013-07-26 10:37:43 -07002527 work_line_->CheckBinaryOp2addr(inst,
2528 reg_types_.Float(),
2529 reg_types_.Float(),
2530 reg_types_.Float(),
2531 false);
jeffhaobdb76512011-09-07 11:43:16 -07002532 break;
2533 case Instruction::ADD_DOUBLE_2ADDR:
2534 case Instruction::SUB_DOUBLE_2ADDR:
2535 case Instruction::MUL_DOUBLE_2ADDR:
2536 case Instruction::DIV_DOUBLE_2ADDR:
2537 case Instruction::REM_DOUBLE_2ADDR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002538 work_line_->CheckBinaryOp2addrWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002539 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2540 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002541 break;
2542 case Instruction::ADD_INT_LIT16:
2543 case Instruction::RSUB_INT:
2544 case Instruction::MUL_INT_LIT16:
2545 case Instruction::DIV_INT_LIT16:
2546 case Instruction::REM_INT_LIT16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002547 work_line_->CheckLiteralOp(inst, reg_types_.Integer(), reg_types_.Integer(), false, true);
jeffhaobdb76512011-09-07 11:43:16 -07002548 break;
2549 case Instruction::AND_INT_LIT16:
2550 case Instruction::OR_INT_LIT16:
2551 case Instruction::XOR_INT_LIT16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002552 work_line_->CheckLiteralOp(inst, reg_types_.Integer(), reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002553 break;
2554 case Instruction::ADD_INT_LIT8:
2555 case Instruction::RSUB_INT_LIT8:
2556 case Instruction::MUL_INT_LIT8:
2557 case Instruction::DIV_INT_LIT8:
2558 case Instruction::REM_INT_LIT8:
2559 case Instruction::SHL_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002560 case Instruction::SHR_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002561 case Instruction::USHR_INT_LIT8:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002562 work_line_->CheckLiteralOp(inst, reg_types_.Integer(), reg_types_.Integer(), false, false);
jeffhaobdb76512011-09-07 11:43:16 -07002563 break;
2564 case Instruction::AND_INT_LIT8:
2565 case Instruction::OR_INT_LIT8:
2566 case Instruction::XOR_INT_LIT8:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002567 work_line_->CheckLiteralOp(inst, reg_types_.Integer(), reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002568 break;
2569
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002570 // Special instructions.
Sebastien Hertzcc10e0e2013-06-28 14:24:48 +02002571 case Instruction::RETURN_VOID_BARRIER:
Ian Rogers9fc16eb2013-07-31 14:49:16 -07002572 if (!IsConstructor() || IsStatic()) {
Sebastien Hertzcc10e0e2013-06-28 14:24:48 +02002573 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void-barrier not expected";
2574 }
2575 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002576 // Note: the following instructions encode offsets derived from class linking.
2577 // As such they use Class*/Field*/AbstractMethod* as these offsets only have
2578 // meaning if the class linking and resolution were successful.
2579 case Instruction::IGET_QUICK:
2580 VerifyIGetQuick(inst, reg_types_.Integer(), true);
2581 break;
2582 case Instruction::IGET_WIDE_QUICK:
2583 VerifyIGetQuick(inst, reg_types_.LongLo(), true);
2584 break;
2585 case Instruction::IGET_OBJECT_QUICK:
2586 VerifyIGetQuick(inst, reg_types_.JavaLangObject(false), false);
2587 break;
2588 case Instruction::IPUT_QUICK:
2589 VerifyIPutQuick(inst, reg_types_.Integer(), true);
2590 break;
2591 case Instruction::IPUT_WIDE_QUICK:
2592 VerifyIPutQuick(inst, reg_types_.LongLo(), true);
2593 break;
2594 case Instruction::IPUT_OBJECT_QUICK:
2595 VerifyIPutQuick(inst, reg_types_.JavaLangObject(false), false);
2596 break;
2597 case Instruction::INVOKE_VIRTUAL_QUICK:
2598 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: {
2599 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
Brian Carlstromea46f952013-07-30 01:26:50 -07002600 mirror::ArtMethod* called_method = VerifyInvokeVirtualQuickArgs(inst, is_range);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002601 if (called_method != NULL) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002602 const char* descriptor = called_method->GetReturnTypeDescriptor();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07002603 const RegType& return_type = reg_types_.FromDescriptor(class_loader_->Get(), descriptor,
Mathieu Chartier590fee92013-09-13 13:46:47 -07002604 false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002605 if (!return_type.IsLowHalf()) {
2606 work_line_->SetResultRegisterType(return_type);
2607 } else {
2608 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2609 }
2610 just_set_result = true;
2611 }
2612 break;
2613 }
2614
Ian Rogersd81871c2011-10-03 13:57:23 -07002615 /* These should never appear during verification. */
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002616 case Instruction::UNUSED_3E:
2617 case Instruction::UNUSED_3F:
2618 case Instruction::UNUSED_40:
2619 case Instruction::UNUSED_41:
2620 case Instruction::UNUSED_42:
2621 case Instruction::UNUSED_43:
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002622 case Instruction::UNUSED_79:
2623 case Instruction::UNUSED_7A:
2624 case Instruction::UNUSED_EB:
2625 case Instruction::UNUSED_EC:
jeffhao9a4f0032012-08-30 16:17:40 -07002626 case Instruction::UNUSED_ED:
jeffhaobdb76512011-09-07 11:43:16 -07002627 case Instruction::UNUSED_EE:
2628 case Instruction::UNUSED_EF:
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002629 case Instruction::UNUSED_F0:
2630 case Instruction::UNUSED_F1:
jeffhaobdb76512011-09-07 11:43:16 -07002631 case Instruction::UNUSED_F2:
2632 case Instruction::UNUSED_F3:
2633 case Instruction::UNUSED_F4:
2634 case Instruction::UNUSED_F5:
2635 case Instruction::UNUSED_F6:
2636 case Instruction::UNUSED_F7:
2637 case Instruction::UNUSED_F8:
2638 case Instruction::UNUSED_F9:
2639 case Instruction::UNUSED_FA:
2640 case Instruction::UNUSED_FB:
jeffhaobdb76512011-09-07 11:43:16 -07002641 case Instruction::UNUSED_FC:
jeffhaobdb76512011-09-07 11:43:16 -07002642 case Instruction::UNUSED_FD:
jeffhaobdb76512011-09-07 11:43:16 -07002643 case Instruction::UNUSED_FE:
jeffhaobdb76512011-09-07 11:43:16 -07002644 case Instruction::UNUSED_FF:
jeffhaod5347e02012-03-22 17:25:05 -07002645 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Unexpected opcode " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07002646 break;
2647
2648 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002649 * DO NOT add a "default" clause here. Without it the compiler will
jeffhaobdb76512011-09-07 11:43:16 -07002650 * complain if an instruction is missing (which is desirable).
2651 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002652 } // end - switch (dec_insn.opcode)
jeffhaobdb76512011-09-07 11:43:16 -07002653
Ian Rogersad0b3a32012-04-16 14:50:24 -07002654 if (have_pending_hard_failure_) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002655 if (Runtime::Current()->IsCompiler()) {
jeffhaob57e9522012-04-26 18:08:21 -07002656 /* When compiling, check that the last failure is a hard failure */
Ian Rogersad0b3a32012-04-16 14:50:24 -07002657 CHECK_EQ(failures_[failures_.size() - 1], VERIFY_ERROR_BAD_CLASS_HARD);
Ian Rogerse1758fe2012-04-19 11:31:15 -07002658 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002659 /* immediate failure, reject class */
2660 info_messages_ << "Rejecting opcode " << inst->DumpString(dex_file_);
2661 return false;
jeffhaofaf459e2012-08-31 15:32:47 -07002662 } else if (have_pending_runtime_throw_failure_) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07002663 /* checking interpreter will throw, mark following code as unreachable */
jeffhaofaf459e2012-08-31 15:32:47 -07002664 opcode_flags = Instruction::kThrow;
jeffhaobdb76512011-09-07 11:43:16 -07002665 }
jeffhaobdb76512011-09-07 11:43:16 -07002666 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002667 * If we didn't just set the result register, clear it out. This ensures that you can only use
2668 * "move-result" immediately after the result is set. (We could check this statically, but it's
2669 * not expensive and it makes our debugging output cleaner.)
jeffhaobdb76512011-09-07 11:43:16 -07002670 */
2671 if (!just_set_result) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002672 work_line_->SetResultTypeToUnknown();
jeffhaobdb76512011-09-07 11:43:16 -07002673 }
2674
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002675
jeffhaobdb76512011-09-07 11:43:16 -07002676
2677 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002678 * Handle "branch". Tag the branch target.
jeffhaobdb76512011-09-07 11:43:16 -07002679 *
2680 * NOTE: instructions like Instruction::EQZ provide information about the
jeffhaod1f0fde2011-09-08 17:25:33 -07002681 * state of the register when the branch is taken or not taken. For example,
jeffhaobdb76512011-09-07 11:43:16 -07002682 * somebody could get a reference field, check it for zero, and if the
2683 * branch is taken immediately store that register in a boolean field
jeffhaod1f0fde2011-09-08 17:25:33 -07002684 * since the value is known to be zero. We do not currently account for
jeffhaobdb76512011-09-07 11:43:16 -07002685 * that, and will reject the code.
2686 *
2687 * TODO: avoid re-fetching the branch target
2688 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002689 if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002690 bool isConditional, selfOkay;
Ian Rogersd81871c2011-10-03 13:57:23 -07002691 if (!GetBranchOffset(work_insn_idx_, &branch_target, &isConditional, &selfOkay)) {
jeffhaobdb76512011-09-07 11:43:16 -07002692 /* should never happen after static verification */
jeffhaod5347e02012-03-22 17:25:05 -07002693 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad branch";
jeffhaobdb76512011-09-07 11:43:16 -07002694 return false;
2695 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08002696 DCHECK_EQ(isConditional, (opcode_flags & Instruction::kContinue) != 0);
jeffhaod5347e02012-03-22 17:25:05 -07002697 if (!CheckNotMoveException(code_item_->insns_, work_insn_idx_ + branch_target)) {
jeffhaobdb76512011-09-07 11:43:16 -07002698 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002699 }
jeffhaobdb76512011-09-07 11:43:16 -07002700 /* update branch target, set "changed" if appropriate */
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002701 if (NULL != branch_line.get()) {
2702 if (!UpdateRegisters(work_insn_idx_ + branch_target, branch_line.get())) {
2703 return false;
2704 }
2705 } else {
2706 if (!UpdateRegisters(work_insn_idx_ + branch_target, work_line_.get())) {
2707 return false;
2708 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002709 }
jeffhaobdb76512011-09-07 11:43:16 -07002710 }
2711
2712 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002713 * Handle "switch". Tag all possible branch targets.
jeffhaobdb76512011-09-07 11:43:16 -07002714 *
2715 * We've already verified that the table is structurally sound, so we
2716 * just need to walk through and tag the targets.
2717 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002718 if ((opcode_flags & Instruction::kSwitch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002719 int offset_to_switch = insns[1] | (((int32_t) insns[2]) << 16);
2720 const uint16_t* switch_insns = insns + offset_to_switch;
2721 int switch_count = switch_insns[1];
2722 int offset_to_targets, targ;
2723
2724 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
2725 /* 0 = sig, 1 = count, 2/3 = first key */
2726 offset_to_targets = 4;
2727 } else {
2728 /* 0 = sig, 1 = count, 2..count * 2 = keys */
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07002729 DCHECK((*insns & 0xff) == Instruction::SPARSE_SWITCH);
jeffhaobdb76512011-09-07 11:43:16 -07002730 offset_to_targets = 2 + 2 * switch_count;
2731 }
2732
2733 /* verify each switch target */
2734 for (targ = 0; targ < switch_count; targ++) {
2735 int offset;
2736 uint32_t abs_offset;
2737
2738 /* offsets are 32-bit, and only partly endian-swapped */
2739 offset = switch_insns[offset_to_targets + targ * 2] |
2740 (((int32_t) switch_insns[offset_to_targets + targ * 2 + 1]) << 16);
Ian Rogersd81871c2011-10-03 13:57:23 -07002741 abs_offset = work_insn_idx_ + offset;
2742 DCHECK_LT(abs_offset, code_item_->insns_size_in_code_units_);
jeffhaod5347e02012-03-22 17:25:05 -07002743 if (!CheckNotMoveException(code_item_->insns_, abs_offset)) {
jeffhaobdb76512011-09-07 11:43:16 -07002744 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002745 }
2746 if (!UpdateRegisters(abs_offset, work_line_.get()))
jeffhaobdb76512011-09-07 11:43:16 -07002747 return false;
2748 }
2749 }
2750
2751 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002752 * Handle instructions that can throw and that are sitting in a "try" block. (If they're not in a
2753 * "try" block when they throw, control transfers out of the method.)
jeffhaobdb76512011-09-07 11:43:16 -07002754 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002755 if ((opcode_flags & Instruction::kThrow) != 0 && insn_flags_[work_insn_idx_].IsInTry()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002756 bool within_catch_all = false;
Ian Rogers0571d352011-11-03 19:51:38 -07002757 CatchHandlerIterator iterator(*code_item_, work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07002758
Ian Rogers0571d352011-11-03 19:51:38 -07002759 for (; iterator.HasNext(); iterator.Next()) {
2760 if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002761 within_catch_all = true;
2762 }
jeffhaobdb76512011-09-07 11:43:16 -07002763 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002764 * Merge registers into the "catch" block. We want to use the "savedRegs" rather than
2765 * "work_regs", because at runtime the exception will be thrown before the instruction
2766 * modifies any registers.
jeffhaobdb76512011-09-07 11:43:16 -07002767 */
Ian Rogers0571d352011-11-03 19:51:38 -07002768 if (!UpdateRegisters(iterator.GetHandlerAddress(), saved_line_.get())) {
jeffhaobdb76512011-09-07 11:43:16 -07002769 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002770 }
jeffhaobdb76512011-09-07 11:43:16 -07002771 }
2772
2773 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002774 * If the monitor stack depth is nonzero, there must be a "catch all" handler for this
2775 * instruction. This does apply to monitor-exit because of async exception handling.
jeffhaobdb76512011-09-07 11:43:16 -07002776 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002777 if (work_line_->MonitorStackDepth() > 0 && !within_catch_all) {
jeffhaobdb76512011-09-07 11:43:16 -07002778 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002779 * The state in work_line reflects the post-execution state. If the current instruction is a
2780 * monitor-enter and the monitor stack was empty, we don't need a catch-all (if it throws,
jeffhaobdb76512011-09-07 11:43:16 -07002781 * it will do so before grabbing the lock).
2782 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002783 if (inst->Opcode() != Instruction::MONITOR_ENTER || work_line_->MonitorStackDepth() != 1) {
jeffhaod5347e02012-03-22 17:25:05 -07002784 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
Ian Rogersd81871c2011-10-03 13:57:23 -07002785 << "expected to be within a catch-all for an instruction where a monitor is held";
jeffhaobdb76512011-09-07 11:43:16 -07002786 return false;
2787 }
2788 }
2789 }
2790
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002791 /* Handle "continue". Tag the next consecutive instruction.
2792 * Note: Keep the code handling "continue" case below the "branch" and "switch" cases,
2793 * because it changes work_line_ when performing peephole optimization
2794 * and this change should not be used in those cases.
2795 */
Ian Rogers6d376ae2013-07-23 15:12:40 -07002796 if ((opcode_flags & Instruction::kContinue) != 0) {
2797 uint32_t next_insn_idx = work_insn_idx_ + CurrentInsnFlags()->GetLengthInCodeUnits();
2798 if (next_insn_idx >= code_item_->insns_size_in_code_units_) {
2799 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Execution can walk off end of code area";
2800 return false;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002801 }
Ian Rogers6d376ae2013-07-23 15:12:40 -07002802 // The only way to get to a move-exception instruction is to get thrown there. Make sure the
2803 // next instruction isn't one.
2804 if (!CheckNotMoveException(code_item_->insns_, next_insn_idx)) {
2805 return false;
2806 }
2807 if (NULL != fallthrough_line.get()) {
2808 // Make workline consistent with fallthrough computed from peephole optimization.
2809 work_line_->CopyFromLine(fallthrough_line.get());
2810 }
Ian Rogersb8c78592013-07-25 23:52:52 +00002811 if (insn_flags_[next_insn_idx].IsReturn()) {
2812 // For returns we only care about the operand to the return, all other registers are dead.
2813 const Instruction* ret_inst = Instruction::At(code_item_->insns_ + next_insn_idx);
2814 Instruction::Code opcode = ret_inst->Opcode();
2815 if ((opcode == Instruction::RETURN_VOID) || (opcode == Instruction::RETURN_VOID_BARRIER)) {
2816 work_line_->MarkAllRegistersAsConflicts();
2817 } else {
2818 if (opcode == Instruction::RETURN_WIDE) {
2819 work_line_->MarkAllRegistersAsConflictsExceptWide(ret_inst->VRegA_11x());
2820 } else {
2821 work_line_->MarkAllRegistersAsConflictsExcept(ret_inst->VRegA_11x());
2822 }
2823 }
2824 }
Ian Rogers6d376ae2013-07-23 15:12:40 -07002825 RegisterLine* next_line = reg_table_.GetLine(next_insn_idx);
2826 if (next_line != NULL) {
2827 // Merge registers into what we have for the next instruction,
2828 // and set the "changed" flag if needed.
2829 if (!UpdateRegisters(next_insn_idx, work_line_.get())) {
2830 return false;
2831 }
2832 } else {
2833 /*
2834 * We're not recording register data for the next instruction, so we don't know what the
2835 * prior state was. We have to assume that something has changed and re-evaluate it.
2836 */
2837 insn_flags_[next_insn_idx].SetChanged();
2838 }
2839 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002840
jeffhaod1f0fde2011-09-08 17:25:33 -07002841 /* If we're returning from the method, make sure monitor stack is empty. */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002842 if ((opcode_flags & Instruction::kReturn) != 0) {
Elliott Hughesb25c3f62012-03-26 16:35:06 -07002843 if (!work_line_->VerifyMonitorStackEmpty()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002844 return false;
2845 }
jeffhaobdb76512011-09-07 11:43:16 -07002846 }
2847
2848 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002849 * Update start_guess. Advance to the next instruction of that's
2850 * possible, otherwise use the branch target if one was found. If
jeffhaobdb76512011-09-07 11:43:16 -07002851 * neither of those exists we're in a return or throw; leave start_guess
2852 * alone and let the caller sort it out.
2853 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002854 if ((opcode_flags & Instruction::kContinue) != 0) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002855 *start_guess = work_insn_idx_ + insn_flags_[work_insn_idx_].GetLengthInCodeUnits();
Elliott Hughesadb8c672012-03-06 16:49:32 -08002856 } else if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002857 /* we're still okay if branch_target is zero */
Ian Rogersd81871c2011-10-03 13:57:23 -07002858 *start_guess = work_insn_idx_ + branch_target;
jeffhaobdb76512011-09-07 11:43:16 -07002859 }
2860
Ian Rogersd81871c2011-10-03 13:57:23 -07002861 DCHECK_LT(*start_guess, code_item_->insns_size_in_code_units_);
2862 DCHECK(insn_flags_[*start_guess].IsOpcode());
jeffhaobdb76512011-09-07 11:43:16 -07002863
2864 return true;
Brian Carlstrom7934ac22013-07-26 10:54:15 -07002865} // NOLINT(readability/fn_size)
jeffhaobdb76512011-09-07 11:43:16 -07002866
Ian Rogers776ac1f2012-04-13 23:36:36 -07002867const RegType& MethodVerifier::ResolveClassAndCheckAccess(uint32_t class_idx) {
Ian Rogers0571d352011-11-03 19:51:38 -07002868 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002869 const RegType& referrer = GetDeclaringClass();
Mathieu Chartier590fee92013-09-13 13:46:47 -07002870 mirror::Class* klass = (*dex_cache_)->GetResolvedType(class_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002871 const RegType& result =
Ian Rogers04f94f42013-06-10 15:09:26 -07002872 klass != NULL ? reg_types_.FromClass(descriptor, klass,
2873 klass->CannotBeAssignedFromOtherTypes())
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07002874 : reg_types_.FromDescriptor(class_loader_->Get(), descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002875 if (result.IsConflict()) {
2876 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "accessing broken descriptor '" << descriptor
2877 << "' in " << referrer;
2878 return result;
2879 }
Ian Rogerse1758fe2012-04-19 11:31:15 -07002880 if (klass == NULL && !result.IsUnresolvedTypes()) {
Mathieu Chartier590fee92013-09-13 13:46:47 -07002881 (*dex_cache_)->SetResolvedType(class_idx, result.GetClass());
Ian Rogerse1758fe2012-04-19 11:31:15 -07002882 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002883 // Check if access is allowed. Unresolved types use xxxWithAccessCheck to
Jeff Haob24b4a72013-07-31 13:47:31 -07002884 // check at runtime if access is allowed and so pass here. If result is
2885 // primitive, skip the access check.
2886 if (result.IsNonZeroReferenceTypes() && !result.IsUnresolvedTypes() &&
2887 !referrer.IsUnresolvedTypes() && !referrer.CanAccess(result)) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002888 Fail(VERIFY_ERROR_ACCESS_CLASS) << "illegal class access: '"
Ian Rogersad0b3a32012-04-16 14:50:24 -07002889 << referrer << "' -> '" << result << "'";
Ian Rogers28ad40d2011-10-27 15:19:26 -07002890 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002891 return result;
Ian Rogersd81871c2011-10-03 13:57:23 -07002892}
2893
Ian Rogers776ac1f2012-04-13 23:36:36 -07002894const RegType& MethodVerifier::GetCaughtExceptionType() {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002895 const RegType* common_super = NULL;
Ian Rogersd81871c2011-10-03 13:57:23 -07002896 if (code_item_->tries_size_ != 0) {
Ian Rogers0571d352011-11-03 19:51:38 -07002897 const byte* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
Ian Rogersd81871c2011-10-03 13:57:23 -07002898 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
2899 for (uint32_t i = 0; i < handlers_size; i++) {
Ian Rogers0571d352011-11-03 19:51:38 -07002900 CatchHandlerIterator iterator(handlers_ptr);
2901 for (; iterator.HasNext(); iterator.Next()) {
2902 if (iterator.GetHandlerAddress() == (uint32_t) work_insn_idx_) {
2903 if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) {
Ian Rogersb4903572012-10-11 11:52:56 -07002904 common_super = &reg_types_.JavaLangThrowable(false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002905 } else {
Ian Rogers0571d352011-11-03 19:51:38 -07002906 const RegType& exception = ResolveClassAndCheckAccess(iterator.GetHandlerTypeIndex());
Jeff Haob878f212014-04-24 16:25:36 -07002907 if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(exception)) {
Jeff Haoc26a56c2013-11-04 12:00:47 -08002908 if (exception.IsUnresolvedTypes()) {
2909 // We don't know enough about the type. Fail here and let runtime handle it.
2910 Fail(VERIFY_ERROR_NO_CLASS) << "unresolved exception class " << exception;
2911 return exception;
2912 } else {
2913 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unexpected non-exception class " << exception;
2914 return reg_types_.Conflict();
2915 }
Jeff Haob878f212014-04-24 16:25:36 -07002916 } else if (common_super == nullptr) {
2917 common_super = &exception;
Ian Rogers28ad40d2011-10-27 15:19:26 -07002918 } else if (common_super->Equals(exception)) {
Ian Rogersc4762272012-02-01 15:55:55 -08002919 // odd case, but nothing to do
Ian Rogersd81871c2011-10-03 13:57:23 -07002920 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002921 common_super = &common_super->Merge(exception, &reg_types_);
Ian Rogersb4903572012-10-11 11:52:56 -07002922 CHECK(reg_types_.JavaLangThrowable(false).IsAssignableFrom(*common_super));
Ian Rogersd81871c2011-10-03 13:57:23 -07002923 }
2924 }
2925 }
2926 }
Ian Rogers0571d352011-11-03 19:51:38 -07002927 handlers_ptr = iterator.EndDataPointer();
Ian Rogersd81871c2011-10-03 13:57:23 -07002928 }
2929 }
2930 if (common_super == NULL) {
2931 /* no catch blocks, or no catches with classes we can find */
jeffhaod5347e02012-03-22 17:25:05 -07002932 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unable to find exception handler";
Ian Rogersad0b3a32012-04-16 14:50:24 -07002933 return reg_types_.Conflict();
Ian Rogersd81871c2011-10-03 13:57:23 -07002934 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002935 return *common_super;
Ian Rogersd81871c2011-10-03 13:57:23 -07002936}
2937
Brian Carlstromea46f952013-07-30 01:26:50 -07002938mirror::ArtMethod* MethodVerifier::ResolveMethodAndCheckAccess(uint32_t dex_method_idx,
Ian Rogersd91d6d62013-09-25 20:26:14 -07002939 MethodType method_type) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002940 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx);
Ian Rogers90040192011-12-16 08:54:29 -08002941 const RegType& klass_type = ResolveClassAndCheckAccess(method_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002942 if (klass_type.IsConflict()) {
2943 std::string append(" in attempt to access method ");
2944 append += dex_file_->GetMethodName(method_id);
2945 AppendToLastFailMessage(append);
Ian Rogers90040192011-12-16 08:54:29 -08002946 return NULL;
2947 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002948 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers90040192011-12-16 08:54:29 -08002949 return NULL; // Can't resolve Class so no more to do here
2950 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002951 mirror::Class* klass = klass_type.GetClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002952 const RegType& referrer = GetDeclaringClass();
Mathieu Chartier590fee92013-09-13 13:46:47 -07002953 mirror::ArtMethod* res_method = (*dex_cache_)->GetResolvedMethod(dex_method_idx);
Ian Rogersd81871c2011-10-03 13:57:23 -07002954 if (res_method == NULL) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07002955 const char* name = dex_file_->GetMethodName(method_id);
Ian Rogersd91d6d62013-09-25 20:26:14 -07002956 const Signature signature = dex_file_->GetMethodSignature(method_id);
jeffhao8cd6dda2012-02-22 10:15:34 -08002957
2958 if (method_type == METHOD_DIRECT || method_type == METHOD_STATIC) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002959 res_method = klass->FindDirectMethod(name, signature);
jeffhao8cd6dda2012-02-22 10:15:34 -08002960 } else if (method_type == METHOD_INTERFACE) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002961 res_method = klass->FindInterfaceMethod(name, signature);
2962 } else {
2963 res_method = klass->FindVirtualMethod(name, signature);
2964 }
2965 if (res_method != NULL) {
Mathieu Chartier590fee92013-09-13 13:46:47 -07002966 (*dex_cache_)->SetResolvedMethod(dex_method_idx, res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07002967 } else {
jeffhao8cd6dda2012-02-22 10:15:34 -08002968 // If a virtual or interface method wasn't found with the expected type, look in
2969 // the direct methods. This can happen when the wrong invoke type is used or when
2970 // a class has changed, and will be flagged as an error in later checks.
2971 if (method_type == METHOD_INTERFACE || method_type == METHOD_VIRTUAL) {
2972 res_method = klass->FindDirectMethod(name, signature);
2973 }
2974 if (res_method == NULL) {
2975 Fail(VERIFY_ERROR_NO_METHOD) << "couldn't find method "
2976 << PrettyDescriptor(klass) << "." << name
2977 << " " << signature;
2978 return NULL;
2979 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002980 }
2981 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002982 // Make sure calls to constructors are "direct". There are additional restrictions but we don't
2983 // enforce them here.
2984 if (res_method->IsConstructor() && method_type != METHOD_DIRECT) {
jeffhaod5347e02012-03-22 17:25:05 -07002985 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting non-direct call to constructor "
2986 << PrettyMethod(res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07002987 return NULL;
2988 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002989 // Disallow any calls to class initializers.
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002990 if (res_method->IsClassInitializer()) {
jeffhaod5347e02012-03-22 17:25:05 -07002991 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting call to class initializer "
2992 << PrettyMethod(res_method);
jeffhao8cd6dda2012-02-22 10:15:34 -08002993 return NULL;
2994 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002995 // Check if access is allowed.
Ian Rogersad0b3a32012-04-16 14:50:24 -07002996 if (!referrer.CanAccessMember(res_method->GetDeclaringClass(), res_method->GetAccessFlags())) {
jeffhao8cd6dda2012-02-22 10:15:34 -08002997 Fail(VERIFY_ERROR_ACCESS_METHOD) << "illegal method access (call " << PrettyMethod(res_method)
Ian Rogersad0b3a32012-04-16 14:50:24 -07002998 << " from " << referrer << ")";
jeffhaob57e9522012-04-26 18:08:21 -07002999 return res_method;
jeffhao8cd6dda2012-02-22 10:15:34 -08003000 }
jeffhaode0d9c92012-02-27 13:58:13 -08003001 // Check that invoke-virtual and invoke-super are not used on private methods of the same class.
3002 if (res_method->IsPrivate() && method_type == METHOD_VIRTUAL) {
jeffhaod5347e02012-03-22 17:25:05 -07003003 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invoke-super/virtual can't be used on private method "
3004 << PrettyMethod(res_method);
jeffhaode0d9c92012-02-27 13:58:13 -08003005 return NULL;
3006 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003007 // Check that interface methods match interface classes.
3008 if (klass->IsInterface() && method_type != METHOD_INTERFACE) {
3009 Fail(VERIFY_ERROR_CLASS_CHANGE) << "non-interface method " << PrettyMethod(res_method)
3010 << " is in an interface class " << PrettyClass(klass);
3011 return NULL;
3012 } else if (!klass->IsInterface() && method_type == METHOD_INTERFACE) {
3013 Fail(VERIFY_ERROR_CLASS_CHANGE) << "interface method " << PrettyMethod(res_method)
3014 << " is in a non-interface class " << PrettyClass(klass);
3015 return NULL;
3016 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003017 // See if the method type implied by the invoke instruction matches the access flags for the
3018 // target method.
3019 if ((method_type == METHOD_DIRECT && !res_method->IsDirect()) ||
3020 (method_type == METHOD_STATIC && !res_method->IsStatic()) ||
3021 ((method_type == METHOD_VIRTUAL || method_type == METHOD_INTERFACE) && res_method->IsDirect())
3022 ) {
Ian Rogers2fc14272012-08-30 10:56:57 -07003023 Fail(VERIFY_ERROR_CLASS_CHANGE) << "invoke type (" << method_type << ") does not match method "
3024 " type of " << PrettyMethod(res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07003025 return NULL;
3026 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003027 return res_method;
3028}
3029
Brian Carlstromea46f952013-07-30 01:26:50 -07003030mirror::ArtMethod* MethodVerifier::VerifyInvocationArgs(const Instruction* inst,
Sebastien Hertz5243e912013-05-21 10:55:07 +02003031 MethodType method_type,
3032 bool is_range,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003033 bool is_super) {
jeffhao8cd6dda2012-02-22 10:15:34 -08003034 // Resolve the method. This could be an abstract or concrete method depending on what sort of call
3035 // we're making.
Sebastien Hertz5243e912013-05-21 10:55:07 +02003036 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Andreas Gampeacc4d2f2014-06-12 19:35:05 -07003037
3038 // As the method may not have been resolved, make this static check against what we expect.
3039 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
3040 uint32_t shorty_idx = dex_file_->GetProtoId(method_id.proto_idx_).shorty_idx_;
3041 uint32_t shorty_len;
3042 const char* descriptor = dex_file_->StringDataAndUtf16LengthByIdx(shorty_idx, &shorty_len);
3043 int32_t sig_registers = method_type == METHOD_STATIC ? 0 : 1;
3044 for (size_t i = 1; i < shorty_len; i++) {
3045 if (descriptor[i] == 'J' || descriptor[i] == 'D') {
3046 sig_registers += 2;
3047 } else {
3048 sig_registers++;
3049 }
3050 }
3051 if (inst->VRegA() != sig_registers) {
3052 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, expected " << inst->VRegA() <<
3053 " arguments, found " << sig_registers;
3054 return nullptr;
3055 }
3056
Brian Carlstromea46f952013-07-30 01:26:50 -07003057 mirror::ArtMethod* res_method = ResolveMethodAndCheckAccess(method_idx, method_type);
jeffhao8cd6dda2012-02-22 10:15:34 -08003058 if (res_method == NULL) { // error or class is unresolved
3059 return NULL;
3060 }
3061
Ian Rogersd81871c2011-10-03 13:57:23 -07003062 // If we're using invoke-super(method), make sure that the executing method's class' superclass
3063 // has a vtable entry for the target method.
3064 if (is_super) {
3065 DCHECK(method_type == METHOD_VIRTUAL);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003066 const RegType& super = GetDeclaringClass().GetSuperClass(&reg_types_);
Ian Rogers529781d2012-07-23 17:24:29 -07003067 if (super.IsUnresolvedTypes()) {
jeffhao4d8df822012-04-24 17:09:36 -07003068 Fail(VERIFY_ERROR_NO_METHOD) << "unknown super class in invoke-super from "
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003069 << PrettyMethod(dex_method_idx_, *dex_file_)
jeffhao4d8df822012-04-24 17:09:36 -07003070 << " to super " << PrettyMethod(res_method);
3071 return NULL;
3072 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003073 mirror::Class* super_klass = super.GetClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07003074 if (res_method->GetMethodIndex() >= super_klass->GetVTable()->GetLength()) {
jeffhao4d8df822012-04-24 17:09:36 -07003075 Fail(VERIFY_ERROR_NO_METHOD) << "invalid invoke-super from "
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003076 << PrettyMethod(dex_method_idx_, *dex_file_)
jeffhao4d8df822012-04-24 17:09:36 -07003077 << " to super " << super
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003078 << "." << res_method->GetName()
3079 << res_method->GetSignature();
Ian Rogersd81871c2011-10-03 13:57:23 -07003080 return NULL;
3081 }
3082 }
3083 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003084 // match the call to the signature. Also, we might be calling through an abstract method
Ian Rogersd81871c2011-10-03 13:57:23 -07003085 // definition (which doesn't have register count values).
Sebastien Hertz5243e912013-05-21 10:55:07 +02003086 const size_t expected_args = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
Ian Rogersd81871c2011-10-03 13:57:23 -07003087 /* caught by static verifier */
3088 DCHECK(is_range || expected_args <= 5);
3089 if (expected_args > code_item_->outs_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07003090 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
Ian Rogersd81871c2011-10-03 13:57:23 -07003091 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
3092 return NULL;
3093 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003094
jeffhaobdb76512011-09-07 11:43:16 -07003095 /*
Ian Rogersad0b3a32012-04-16 14:50:24 -07003096 * Check the "this" argument, which must be an instance of the class that declared the method.
3097 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
3098 * rigorous check here (which is okay since we have to do it at runtime).
jeffhaobdb76512011-09-07 11:43:16 -07003099 */
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003100 size_t actual_args = 0;
Ian Rogersd81871c2011-10-03 13:57:23 -07003101 if (!res_method->IsStatic()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003102 const RegType& actual_arg_type = work_line_->GetInvocationThis(inst, is_range);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003103 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
Ian Rogersd81871c2011-10-03 13:57:23 -07003104 return NULL;
3105 }
3106 if (actual_arg_type.IsUninitializedReference() && !res_method->IsConstructor()) {
jeffhaod5347e02012-03-22 17:25:05 -07003107 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
Ian Rogersd81871c2011-10-03 13:57:23 -07003108 return NULL;
3109 }
3110 if (method_type != METHOD_INTERFACE && !actual_arg_type.IsZero()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003111 mirror::Class* klass = res_method->GetDeclaringClass();
Ian Rogers04f94f42013-06-10 15:09:26 -07003112 const RegType& res_method_class =
Mathieu Chartierf8322842014-05-16 10:59:25 -07003113 reg_types_.FromClass(klass->GetDescriptor().c_str(), klass,
Ian Rogers04f94f42013-06-10 15:09:26 -07003114 klass->CannotBeAssignedFromOtherTypes());
Ian Rogers9074b992011-10-26 17:41:55 -07003115 if (!res_method_class.IsAssignableFrom(actual_arg_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07003116 Fail(actual_arg_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS:
3117 VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003118 << "' not instance of '" << res_method_class << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07003119 return NULL;
3120 }
3121 }
3122 actual_args++;
3123 }
3124 /*
3125 * Process the target method's signature. This signature may or may not
3126 * have been verified, so we can't assume it's properly formed.
3127 */
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003128 const DexFile::TypeList* params = res_method->GetParameterTypeList();
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003129 size_t params_size = params == NULL ? 0 : params->Size();
Sebastien Hertz5243e912013-05-21 10:55:07 +02003130 uint32_t arg[5];
3131 if (!is_range) {
Ian Rogers29a26482014-05-02 15:27:29 -07003132 inst->GetVarArgs(arg);
Sebastien Hertz5243e912013-05-21 10:55:07 +02003133 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003134 for (size_t param_index = 0; param_index < params_size; param_index++) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003135 if (actual_args >= expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07003136 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invalid call to '" << PrettyMethod(res_method)
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003137 << "'. Expected " << expected_args << " arguments, processing argument " << actual_args
3138 << " (where longs/doubles count twice).";
Ian Rogersd81871c2011-10-03 13:57:23 -07003139 return NULL;
3140 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003141 const char* descriptor =
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003142 res_method->GetTypeDescriptorFromTypeIdx(params->GetTypeItem(param_index).type_idx_);
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003143 if (descriptor == NULL) {
jeffhaod5347e02012-03-22 17:25:05 -07003144 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003145 << " missing signature component";
3146 return NULL;
Ian Rogersd81871c2011-10-03 13:57:23 -07003147 }
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003148 const RegType& reg_type = reg_types_.FromDescriptor(class_loader_->Get(), descriptor, false);
Sebastien Hertz5243e912013-05-21 10:55:07 +02003149 uint32_t get_reg = is_range ? inst->VRegC_3rc() + actual_args : arg[actual_args];
Jeff Haoa6b22c52013-10-04 14:33:22 -07003150 if (reg_type.IsIntegralTypes()) {
3151 const RegType& src_type = work_line_->GetRegisterType(get_reg);
3152 if (!src_type.IsIntegralTypes()) {
3153 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register v" << get_reg << " has type " << src_type
3154 << " but expected " << reg_type;
3155 return res_method;
3156 }
3157 } else if (!work_line_->VerifyRegisterType(get_reg, reg_type)) {
jeffhaob57e9522012-04-26 18:08:21 -07003158 return res_method;
Ian Rogersd81871c2011-10-03 13:57:23 -07003159 }
3160 actual_args = reg_type.IsLongOrDoubleTypes() ? actual_args + 2 : actual_args + 1;
3161 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003162 if (actual_args != expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07003163 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003164 << " expected " << expected_args << " arguments, found " << actual_args;
Ian Rogersd81871c2011-10-03 13:57:23 -07003165 return NULL;
3166 } else {
3167 return res_method;
3168 }
3169}
3170
Brian Carlstromea46f952013-07-30 01:26:50 -07003171mirror::ArtMethod* MethodVerifier::GetQuickInvokedMethod(const Instruction* inst,
Mathieu Chartier590fee92013-09-13 13:46:47 -07003172 RegisterLine* reg_line, bool is_range) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003173 DCHECK(inst->Opcode() == Instruction::INVOKE_VIRTUAL_QUICK ||
3174 inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
3175 const RegType& actual_arg_type = reg_line->GetInvocationThis(inst, is_range);
Ian Rogers9bc54402014-04-17 16:40:01 -07003176 if (!actual_arg_type.HasClass()) {
3177 VLOG(verifier) << "Failed to get mirror::Class* from '" << actual_arg_type << "'";
Andreas Gampe63981562014-04-17 12:28:43 -07003178 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003179 }
Ian Rogersa4cf1df2014-05-07 19:47:17 -07003180 mirror::ObjectArray<mirror::ArtMethod>* vtable = nullptr;
3181 mirror::Class* klass = actual_arg_type.GetClass();
3182 if (klass->IsInterface()) {
3183 // Derive Object.class from Class.class.getSuperclass().
3184 mirror::Class* object_klass = klass->GetClass()->GetSuperClass();
3185 CHECK(object_klass->IsObjectClass());
3186 vtable = object_klass->GetVTable();
3187 } else {
3188 vtable = klass->GetVTable();
3189 }
3190 CHECK(vtable != nullptr) << PrettyDescriptor(klass);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003191 uint16_t vtable_index = is_range ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersa4cf1df2014-05-07 19:47:17 -07003192 CHECK_LT(static_cast<int32_t>(vtable_index), vtable->GetLength()) << PrettyDescriptor(klass);
Brian Carlstromea46f952013-07-30 01:26:50 -07003193 mirror::ArtMethod* res_method = vtable->Get(vtable_index);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003194 CHECK(!Thread::Current()->IsExceptionPending());
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003195 return res_method;
3196}
3197
Brian Carlstromea46f952013-07-30 01:26:50 -07003198mirror::ArtMethod* MethodVerifier::VerifyInvokeVirtualQuickArgs(const Instruction* inst,
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003199 bool is_range) {
3200 DCHECK(Runtime::Current()->IsStarted());
Brian Carlstromea46f952013-07-30 01:26:50 -07003201 mirror::ArtMethod* res_method = GetQuickInvokedMethod(inst, work_line_.get(),
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003202 is_range);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003203 if (res_method == NULL) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003204 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer method from " << inst->Name();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003205 return NULL;
3206 }
3207 CHECK(!res_method->IsDirect() && !res_method->IsStatic());
3208
3209 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
3210 // match the call to the signature. Also, we might be calling through an abstract method
3211 // definition (which doesn't have register count values).
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003212 const RegType& actual_arg_type = work_line_->GetInvocationThis(inst, is_range);
3213 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
3214 return NULL;
3215 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003216 const size_t expected_args = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
3217 /* caught by static verifier */
3218 DCHECK(is_range || expected_args <= 5);
3219 if (expected_args > code_item_->outs_size_) {
3220 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
3221 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
3222 return NULL;
3223 }
3224
3225 /*
3226 * Check the "this" argument, which must be an instance of the class that declared the method.
3227 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
3228 * rigorous check here (which is okay since we have to do it at runtime).
3229 */
3230 if (actual_arg_type.IsUninitializedReference() && !res_method->IsConstructor()) {
3231 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
3232 return NULL;
3233 }
3234 if (!actual_arg_type.IsZero()) {
3235 mirror::Class* klass = res_method->GetDeclaringClass();
3236 const RegType& res_method_class =
Mathieu Chartierf8322842014-05-16 10:59:25 -07003237 reg_types_.FromClass(klass->GetDescriptor().c_str(), klass,
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003238 klass->CannotBeAssignedFromOtherTypes());
3239 if (!res_method_class.IsAssignableFrom(actual_arg_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07003240 Fail(actual_arg_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS :
3241 VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003242 << "' not instance of '" << res_method_class << "'";
3243 return NULL;
3244 }
3245 }
3246 /*
3247 * Process the target method's signature. This signature may or may not
3248 * have been verified, so we can't assume it's properly formed.
3249 */
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003250 const DexFile::TypeList* params = res_method->GetParameterTypeList();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003251 size_t params_size = params == NULL ? 0 : params->Size();
3252 uint32_t arg[5];
3253 if (!is_range) {
Ian Rogers29a26482014-05-02 15:27:29 -07003254 inst->GetVarArgs(arg);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003255 }
3256 size_t actual_args = 1;
3257 for (size_t param_index = 0; param_index < params_size; param_index++) {
3258 if (actual_args >= expected_args) {
3259 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invalid call to '" << PrettyMethod(res_method)
Brian Carlstrom93c33962013-07-26 10:37:43 -07003260 << "'. Expected " << expected_args
3261 << " arguments, processing argument " << actual_args
3262 << " (where longs/doubles count twice).";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003263 return NULL;
3264 }
3265 const char* descriptor =
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003266 res_method->GetTypeDescriptorFromTypeIdx(params->GetTypeItem(param_index).type_idx_);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003267 if (descriptor == NULL) {
3268 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003269 << " missing signature component";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003270 return NULL;
3271 }
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003272 const RegType& reg_type = reg_types_.FromDescriptor(class_loader_->Get(), descriptor, false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003273 uint32_t get_reg = is_range ? inst->VRegC_3rc() + actual_args : arg[actual_args];
3274 if (!work_line_->VerifyRegisterType(get_reg, reg_type)) {
3275 return res_method;
3276 }
3277 actual_args = reg_type.IsLongOrDoubleTypes() ? actual_args + 2 : actual_args + 1;
3278 }
3279 if (actual_args != expected_args) {
3280 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
3281 << " expected " << expected_args << " arguments, found " << actual_args;
3282 return NULL;
3283 } else {
3284 return res_method;
3285 }
3286}
3287
Ian Rogers62342ec2013-06-11 10:26:37 -07003288void MethodVerifier::VerifyNewArray(const Instruction* inst, bool is_filled, bool is_range) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003289 uint32_t type_idx;
3290 if (!is_filled) {
3291 DCHECK_EQ(inst->Opcode(), Instruction::NEW_ARRAY);
3292 type_idx = inst->VRegC_22c();
3293 } else if (!is_range) {
3294 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY);
3295 type_idx = inst->VRegB_35c();
3296 } else {
3297 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY_RANGE);
3298 type_idx = inst->VRegB_3rc();
3299 }
3300 const RegType& res_type = ResolveClassAndCheckAccess(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003301 if (res_type.IsConflict()) { // bad class
3302 DCHECK_NE(failures_.size(), 0U);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003303 } else {
3304 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
3305 if (!res_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003306 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "new-array on non-array class " << res_type;
Ian Rogers0c4a5062012-02-03 15:18:59 -08003307 } else if (!is_filled) {
3308 /* make sure "size" register is valid type */
Sebastien Hertz5243e912013-05-21 10:55:07 +02003309 work_line_->VerifyRegisterType(inst->VRegB_22c(), reg_types_.Integer());
Ian Rogers0c4a5062012-02-03 15:18:59 -08003310 /* set register type to array class */
Ian Rogers62342ec2013-06-11 10:26:37 -07003311 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
3312 work_line_->SetRegisterType(inst->VRegA_22c(), precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003313 } else {
3314 // Verify each register. If "arg_count" is bad, VerifyRegisterType() will run off the end of
3315 // the list and fail. It's legal, if silly, for arg_count to be zero.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003316 const RegType& expected_type = reg_types_.GetComponentType(res_type, class_loader_->Get());
Sebastien Hertz5243e912013-05-21 10:55:07 +02003317 uint32_t arg_count = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
3318 uint32_t arg[5];
3319 if (!is_range) {
Ian Rogers29a26482014-05-02 15:27:29 -07003320 inst->GetVarArgs(arg);
Sebastien Hertz5243e912013-05-21 10:55:07 +02003321 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08003322 for (size_t ui = 0; ui < arg_count; ui++) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003323 uint32_t get_reg = is_range ? inst->VRegC_3rc() + ui : arg[ui];
Ian Rogers0c4a5062012-02-03 15:18:59 -08003324 if (!work_line_->VerifyRegisterType(get_reg, expected_type)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003325 work_line_->SetResultRegisterType(reg_types_.Conflict());
Ian Rogers0c4a5062012-02-03 15:18:59 -08003326 return;
3327 }
3328 }
3329 // filled-array result goes into "result" register
Ian Rogers62342ec2013-06-11 10:26:37 -07003330 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
3331 work_line_->SetResultRegisterType(precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003332 }
3333 }
3334}
3335
Sebastien Hertz5243e912013-05-21 10:55:07 +02003336void MethodVerifier::VerifyAGet(const Instruction* inst,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003337 const RegType& insn_type, bool is_primitive) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003338 const RegType& index_type = work_line_->GetRegisterType(inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07003339 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003340 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07003341 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003342 const RegType& array_type = work_line_->GetRegisterType(inst->VRegB_23x());
Ian Rogers89310de2012-02-01 13:47:30 -08003343 if (array_type.IsZero()) {
3344 // Null array class; this code path will fail at runtime. Infer a merge-able type from the
3345 // instruction type. TODO: have a proper notion of bottom here.
3346 if (!is_primitive || insn_type.IsCategory1Types()) {
3347 // Reference or category 1
Sebastien Hertz5243e912013-05-21 10:55:07 +02003348 work_line_->SetRegisterType(inst->VRegA_23x(), reg_types_.Zero());
Ian Rogersd81871c2011-10-03 13:57:23 -07003349 } else {
Ian Rogers89310de2012-02-01 13:47:30 -08003350 // Category 2
Sebastien Hertz5243e912013-05-21 10:55:07 +02003351 work_line_->SetRegisterTypeWide(inst->VRegA_23x(), reg_types_.FromCat2ConstLo(0, false),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003352 reg_types_.FromCat2ConstHi(0, false));
Ian Rogers89310de2012-02-01 13:47:30 -08003353 }
jeffhaofc3144e2012-02-01 17:21:15 -08003354 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003355 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aget";
Ian Rogers89310de2012-02-01 13:47:30 -08003356 } else {
3357 /* verify the class */
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003358 const RegType& component_type = reg_types_.GetComponentType(array_type, class_loader_->Get());
jeffhaofc3144e2012-02-01 17:21:15 -08003359 if (!component_type.IsReferenceTypes() && !is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07003360 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08003361 << " source for aget-object";
3362 } else if (component_type.IsNonZeroReferenceTypes() && is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07003363 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08003364 << " source for category 1 aget";
3365 } else if (is_primitive && !insn_type.Equals(component_type) &&
3366 !((insn_type.IsInteger() && component_type.IsFloat()) ||
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003367 (insn_type.IsLong() && component_type.IsDouble()))) {
3368 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array type " << array_type
3369 << " incompatible with aget of type " << insn_type;
Ian Rogers89310de2012-02-01 13:47:30 -08003370 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003371 // Use knowledge of the field type which is stronger than the type inferred from the
3372 // instruction, which can't differentiate object types and ints from floats, longs from
3373 // doubles.
3374 if (!component_type.IsLowHalf()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003375 work_line_->SetRegisterType(inst->VRegA_23x(), component_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003376 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003377 work_line_->SetRegisterTypeWide(inst->VRegA_23x(), component_type,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003378 component_type.HighHalf(&reg_types_));
3379 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003380 }
3381 }
3382 }
3383}
3384
Jeff Haofe1f7c82013-08-01 14:50:24 -07003385void MethodVerifier::VerifyPrimitivePut(const RegType& target_type, const RegType& insn_type,
3386 const uint32_t vregA) {
3387 // Primitive assignability rules are weaker than regular assignability rules.
3388 bool instruction_compatible;
3389 bool value_compatible;
3390 const RegType& value_type = work_line_->GetRegisterType(vregA);
3391 if (target_type.IsIntegralTypes()) {
Jeff Haoa4647482013-08-06 15:35:47 -07003392 instruction_compatible = target_type.Equals(insn_type);
Jeff Haofe1f7c82013-08-01 14:50:24 -07003393 value_compatible = value_type.IsIntegralTypes();
3394 } else if (target_type.IsFloat()) {
3395 instruction_compatible = insn_type.IsInteger(); // no put-float, so expect put-int
3396 value_compatible = value_type.IsFloatTypes();
3397 } else if (target_type.IsLong()) {
3398 instruction_compatible = insn_type.IsLong();
3399 value_compatible = value_type.IsLongTypes();
3400 } else if (target_type.IsDouble()) {
3401 instruction_compatible = insn_type.IsLong(); // no put-double, so expect put-long
3402 value_compatible = value_type.IsDoubleTypes();
3403 } else {
3404 instruction_compatible = false; // reference with primitive store
3405 value_compatible = false; // unused
3406 }
3407 if (!instruction_compatible) {
3408 // This is a global failure rather than a class change failure as the instructions and
3409 // the descriptors for the type should have been consistent within the same file at
3410 // compile time.
3411 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "put insn has type '" << insn_type
3412 << "' but expected type '" << target_type << "'";
3413 return;
3414 }
3415 if (!value_compatible) {
3416 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
3417 << " of type " << value_type << " but expected " << target_type << " for put";
3418 return;
3419 }
3420}
3421
Sebastien Hertz5243e912013-05-21 10:55:07 +02003422void MethodVerifier::VerifyAPut(const Instruction* inst,
Sebastien Hertz757b3042014-03-28 14:34:28 +01003423 const RegType& insn_type, bool is_primitive) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003424 const RegType& index_type = work_line_->GetRegisterType(inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07003425 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003426 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07003427 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003428 const RegType& array_type = work_line_->GetRegisterType(inst->VRegB_23x());
Ian Rogers89310de2012-02-01 13:47:30 -08003429 if (array_type.IsZero()) {
3430 // Null array type; this code path will fail at runtime. Infer a merge-able type from the
3431 // instruction type.
jeffhaofc3144e2012-02-01 17:21:15 -08003432 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003433 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aput";
Ian Rogers89310de2012-02-01 13:47:30 -08003434 } else {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003435 const RegType& component_type = reg_types_.GetComponentType(array_type, class_loader_->Get());
Jeff Haofe1f7c82013-08-01 14:50:24 -07003436 const uint32_t vregA = inst->VRegA_23x();
Jeff Haob24b4a72013-07-31 13:47:31 -07003437 if (is_primitive) {
Jeff Haofe1f7c82013-08-01 14:50:24 -07003438 VerifyPrimitivePut(component_type, insn_type, vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07003439 } else {
Jeff Haob24b4a72013-07-31 13:47:31 -07003440 if (!component_type.IsReferenceTypes()) {
3441 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
3442 << " source for aput-object";
3443 } else {
3444 // The instruction agrees with the type of array, confirm the value to be stored does too
3445 // Note: we use the instruction type (rather than the component type) for aput-object as
3446 // incompatible classes will be caught at runtime as an array store exception
Jeff Haofe1f7c82013-08-01 14:50:24 -07003447 work_line_->VerifyRegisterType(vregA, insn_type);
Jeff Haob24b4a72013-07-31 13:47:31 -07003448 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003449 }
3450 }
3451 }
3452}
3453
Brian Carlstromea46f952013-07-30 01:26:50 -07003454mirror::ArtField* MethodVerifier::GetStaticField(int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08003455 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3456 // Check access to class
3457 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Brian Carlstrom7934ac22013-07-26 10:54:15 -07003458 if (klass_type.IsConflict()) { // bad class
Ian Rogersad0b3a32012-04-16 14:50:24 -07003459 AppendToLastFailMessage(StringPrintf(" in attempt to access static field %d (%s) in %s",
3460 field_idx, dex_file_->GetFieldName(field_id),
3461 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers90040192011-12-16 08:54:29 -08003462 return NULL;
3463 }
Elliott Hughesb25c3f62012-03-26 16:35:06 -07003464 if (klass_type.IsUnresolvedTypes()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003465 return NULL; // Can't resolve Class so no more to do here, will do checking at runtime.
Ian Rogers90040192011-12-16 08:54:29 -08003466 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07003467 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
3468 mirror::ArtField* field = class_linker->ResolveFieldJLS(*dex_file_, field_idx, *dex_cache_,
3469 *class_loader_);
Ian Rogersd81871c2011-10-03 13:57:23 -07003470 if (field == NULL) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07003471 VLOG(verifier) << "Unable to resolve static field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07003472 << dex_file_->GetFieldName(field_id) << ") in "
3473 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07003474 DCHECK(Thread::Current()->IsExceptionPending());
3475 Thread::Current()->ClearException();
3476 return NULL;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003477 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
3478 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003479 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access static field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07003480 << " from " << GetDeclaringClass();
Ian Rogersd81871c2011-10-03 13:57:23 -07003481 return NULL;
3482 } else if (!field->IsStatic()) {
3483 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field) << " to be static";
3484 return NULL;
Ian Rogersd81871c2011-10-03 13:57:23 -07003485 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07003486 return field;
Ian Rogersd81871c2011-10-03 13:57:23 -07003487}
3488
Brian Carlstromea46f952013-07-30 01:26:50 -07003489mirror::ArtField* MethodVerifier::GetInstanceField(const RegType& obj_type, int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08003490 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3491 // Check access to class
3492 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003493 if (klass_type.IsConflict()) {
3494 AppendToLastFailMessage(StringPrintf(" in attempt to access instance field %d (%s) in %s",
3495 field_idx, dex_file_->GetFieldName(field_id),
3496 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers90040192011-12-16 08:54:29 -08003497 return NULL;
3498 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003499 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers90040192011-12-16 08:54:29 -08003500 return NULL; // Can't resolve Class so no more to do here
3501 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07003502 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
3503 mirror::ArtField* field = class_linker->ResolveFieldJLS(*dex_file_, field_idx, *dex_cache_,
3504 *class_loader_);
Ian Rogersd81871c2011-10-03 13:57:23 -07003505 if (field == NULL) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07003506 VLOG(verifier) << "Unable to resolve instance field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07003507 << dex_file_->GetFieldName(field_id) << ") in "
3508 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07003509 DCHECK(Thread::Current()->IsExceptionPending());
3510 Thread::Current()->ClearException();
3511 return NULL;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003512 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
3513 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003514 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access instance field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07003515 << " from " << GetDeclaringClass();
Ian Rogersd81871c2011-10-03 13:57:23 -07003516 return NULL;
3517 } else if (field->IsStatic()) {
3518 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field)
3519 << " to not be static";
3520 return NULL;
3521 } else if (obj_type.IsZero()) {
3522 // Cannot infer and check type, however, access will cause null pointer exception
3523 return field;
Ian Rogerse1758fe2012-04-19 11:31:15 -07003524 } else {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003525 mirror::Class* klass = field->GetDeclaringClass();
Ian Rogers637c65b2013-05-31 11:46:00 -07003526 const RegType& field_klass =
3527 reg_types_.FromClass(dex_file_->GetFieldDeclaringClassDescriptor(field_id),
Ian Rogers04f94f42013-06-10 15:09:26 -07003528 klass, klass->CannotBeAssignedFromOtherTypes());
Ian Rogersad0b3a32012-04-16 14:50:24 -07003529 if (obj_type.IsUninitializedTypes() &&
3530 (!IsConstructor() || GetDeclaringClass().Equals(obj_type) ||
3531 !field_klass.Equals(GetDeclaringClass()))) {
3532 // Field accesses through uninitialized references are only allowable for constructors where
3533 // the field is declared in this class
3534 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "cannot access instance field " << PrettyField(field)
Brian Carlstrom93c33962013-07-26 10:37:43 -07003535 << " of a not fully initialized object within the context"
3536 << " of " << PrettyMethod(dex_method_idx_, *dex_file_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003537 return NULL;
3538 } else if (!field_klass.IsAssignableFrom(obj_type)) {
3539 // Trying to access C1.field1 using reference of type C2, which is neither C1 or a sub-class
3540 // of C1. For resolution to occur the declared class of the field must be compatible with
3541 // obj_type, we've discovered this wasn't so, so report the field didn't exist.
3542 Fail(VERIFY_ERROR_NO_FIELD) << "cannot access instance field " << PrettyField(field)
3543 << " from object of type " << obj_type;
3544 return NULL;
3545 } else {
3546 return field;
3547 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003548 }
3549}
3550
Sebastien Hertz5243e912013-05-21 10:55:07 +02003551void MethodVerifier::VerifyISGet(const Instruction* inst, const RegType& insn_type,
3552 bool is_primitive, bool is_static) {
3553 uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
Brian Carlstromea46f952013-07-30 01:26:50 -07003554 mirror::ArtField* field;
Ian Rogersb94a27b2011-10-26 00:33:41 -07003555 if (is_static) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07003556 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003557 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003558 const RegType& object_type = work_line_->GetRegisterType(inst->VRegB_22c());
Ian Rogersf4028cc2011-11-02 14:56:39 -07003559 field = GetInstanceField(object_type, field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003560 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003561 const RegType* field_type = nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003562 if (field != NULL) {
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07003563 Thread* self = Thread::Current();
3564 mirror::Class* field_type_class;
3565 {
3566 StackHandleScope<1> hs(self);
3567 HandleWrapper<mirror::ArtField> h_field(hs.NewHandleWrapper(&field));
3568 field_type_class = FieldHelper(h_field).GetType(can_load_classes_);
3569 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003570 if (field_type_class != nullptr) {
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07003571 field_type = &reg_types_.FromClass(field->GetTypeDescriptor(), field_type_class,
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003572 field_type_class->CannotBeAssignedFromOtherTypes());
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08003573 } else {
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08003574 DCHECK(!can_load_classes_ || self->IsExceptionPending());
3575 self->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003576 }
Ian Rogers0d604842012-04-16 14:50:24 -07003577 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003578 if (field_type == nullptr) {
3579 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3580 const char* descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003581 field_type = &reg_types_.FromDescriptor(class_loader_->Get(), descriptor, false);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003582 }
Sebastien Hertz757b3042014-03-28 14:34:28 +01003583 DCHECK(field_type != nullptr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02003584 const uint32_t vregA = (is_static) ? inst->VRegA_21c() : inst->VRegA_22c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07003585 if (is_primitive) {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003586 if (field_type->Equals(insn_type) ||
3587 (field_type->IsFloat() && insn_type.IsInteger()) ||
3588 (field_type->IsDouble() && insn_type.IsLong())) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003589 // expected that read is of the correct primitive type or that int reads are reading
3590 // floats or long reads are reading doubles
3591 } else {
3592 // This is a global failure rather than a class change failure as the instructions and
3593 // the descriptors for the type should have been consistent within the same file at
3594 // compile time
3595 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
3596 << " to be of type '" << insn_type
Sebastien Hertz757b3042014-03-28 14:34:28 +01003597 << "' but found type '" << *field_type << "' in get";
Ian Rogersad0b3a32012-04-16 14:50:24 -07003598 return;
3599 }
3600 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003601 if (!insn_type.IsAssignableFrom(*field_type)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003602 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
3603 << " to be compatible with type '" << insn_type
Sebastien Hertz757b3042014-03-28 14:34:28 +01003604 << "' but found type '" << *field_type
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003605 << "' in Get-object";
Sebastien Hertz5243e912013-05-21 10:55:07 +02003606 work_line_->SetRegisterType(vregA, reg_types_.Conflict());
Ian Rogersad0b3a32012-04-16 14:50:24 -07003607 return;
3608 }
3609 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003610 if (!field_type->IsLowHalf()) {
3611 work_line_->SetRegisterType(vregA, *field_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003612 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003613 work_line_->SetRegisterTypeWide(vregA, *field_type, field_type->HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003614 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003615}
3616
Sebastien Hertz5243e912013-05-21 10:55:07 +02003617void MethodVerifier::VerifyISPut(const Instruction* inst, const RegType& insn_type,
3618 bool is_primitive, bool is_static) {
3619 uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
Brian Carlstromea46f952013-07-30 01:26:50 -07003620 mirror::ArtField* field;
Ian Rogersb94a27b2011-10-26 00:33:41 -07003621 if (is_static) {
Ian Rogers55d249f2011-11-02 16:48:09 -07003622 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003623 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003624 const RegType& object_type = work_line_->GetRegisterType(inst->VRegB_22c());
Ian Rogers55d249f2011-11-02 16:48:09 -07003625 field = GetInstanceField(object_type, field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003626 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003627 const RegType* field_type = nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003628 if (field != NULL) {
3629 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
3630 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
3631 << " from other class " << GetDeclaringClass();
3632 return;
3633 }
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07003634 mirror::Class* field_type_class;
3635 {
3636 StackHandleScope<1> hs(Thread::Current());
3637 HandleWrapper<mirror::ArtField> h_field(hs.NewHandleWrapper(&field));
3638 FieldHelper fh(h_field);
3639 field_type_class = fh.GetType(can_load_classes_);
3640 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003641 if (field_type_class != nullptr) {
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07003642 field_type = &reg_types_.FromClass(field->GetTypeDescriptor(), field_type_class,
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003643 field_type_class->CannotBeAssignedFromOtherTypes());
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08003644 } else {
3645 Thread* self = Thread::Current();
3646 DCHECK(!can_load_classes_ || self->IsExceptionPending());
3647 self->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003648 }
3649 }
3650 if (field_type == nullptr) {
3651 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3652 const char* descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003653 field_type = &reg_types_.FromDescriptor(class_loader_->Get(), descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003654 }
Sebastien Hertz757b3042014-03-28 14:34:28 +01003655 DCHECK(field_type != nullptr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02003656 const uint32_t vregA = (is_static) ? inst->VRegA_21c() : inst->VRegA_22c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07003657 if (is_primitive) {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003658 VerifyPrimitivePut(*field_type, insn_type, vregA);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003659 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003660 if (!insn_type.IsAssignableFrom(*field_type)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003661 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
3662 << " to be compatible with type '" << insn_type
Sebastien Hertz757b3042014-03-28 14:34:28 +01003663 << "' but found type '" << *field_type
Ian Rogersad0b3a32012-04-16 14:50:24 -07003664 << "' in put-object";
3665 return;
3666 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003667 work_line_->VerifyRegisterType(vregA, *field_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07003668 }
3669}
3670
Brian Carlstromea46f952013-07-30 01:26:50 -07003671mirror::ArtField* MethodVerifier::GetQuickFieldAccess(const Instruction* inst,
Ian Rogers98379392014-02-24 16:53:16 -08003672 RegisterLine* reg_line) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003673 DCHECK(inst->Opcode() == Instruction::IGET_QUICK ||
3674 inst->Opcode() == Instruction::IGET_WIDE_QUICK ||
3675 inst->Opcode() == Instruction::IGET_OBJECT_QUICK ||
3676 inst->Opcode() == Instruction::IPUT_QUICK ||
3677 inst->Opcode() == Instruction::IPUT_WIDE_QUICK ||
3678 inst->Opcode() == Instruction::IPUT_OBJECT_QUICK);
3679 const RegType& object_type = reg_line->GetRegisterType(inst->VRegB_22c());
Ian Rogers9bc54402014-04-17 16:40:01 -07003680 if (!object_type.HasClass()) {
3681 VLOG(verifier) << "Failed to get mirror::Class* from '" << object_type << "'";
3682 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003683 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003684 uint32_t field_offset = static_cast<uint32_t>(inst->VRegC_22c());
Sebastien Hertz479fc1e2014-04-04 17:51:34 +02003685 mirror::ArtField* f = mirror::ArtField::FindInstanceFieldWithOffset(object_type.GetClass(),
3686 field_offset);
3687 if (f == nullptr) {
3688 VLOG(verifier) << "Failed to find instance field at offset '" << field_offset
3689 << "' from '" << PrettyDescriptor(object_type.GetClass()) << "'";
3690 }
3691 return f;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003692}
3693
3694void MethodVerifier::VerifyIGetQuick(const Instruction* inst, const RegType& insn_type,
3695 bool is_primitive) {
3696 DCHECK(Runtime::Current()->IsStarted());
Brian Carlstromea46f952013-07-30 01:26:50 -07003697 mirror::ArtField* field = GetQuickFieldAccess(inst, work_line_.get());
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003698 if (field == NULL) {
3699 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field from " << inst->Name();
3700 return;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003701 }
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07003702 mirror::Class* field_type_class;
3703 {
3704 StackHandleScope<1> hs(Thread::Current());
3705 HandleWrapper<mirror::ArtField> h_field(hs.NewHandleWrapper(&field));
3706 FieldHelper fh(h_field);
3707 field_type_class = fh.GetType(can_load_classes_);
3708 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003709 const RegType* field_type;
3710 if (field_type_class != nullptr) {
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07003711 field_type = &reg_types_.FromClass(field->GetTypeDescriptor(), field_type_class,
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003712 field_type_class->CannotBeAssignedFromOtherTypes());
3713 } else {
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08003714 Thread* self = Thread::Current();
3715 DCHECK(!can_load_classes_ || self->IsExceptionPending());
3716 self->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003717 field_type = &reg_types_.FromDescriptor(field->GetDeclaringClass()->GetClassLoader(),
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07003718 field->GetTypeDescriptor(), false);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003719 }
Sebastien Hertz757b3042014-03-28 14:34:28 +01003720 DCHECK(field_type != nullptr);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003721 const uint32_t vregA = inst->VRegA_22c();
3722 if (is_primitive) {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003723 if (field_type->Equals(insn_type) ||
3724 (field_type->IsFloat() && insn_type.IsIntegralTypes()) ||
3725 (field_type->IsDouble() && insn_type.IsLongTypes())) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003726 // expected that read is of the correct primitive type or that int reads are reading
3727 // floats or long reads are reading doubles
3728 } else {
3729 // This is a global failure rather than a class change failure as the instructions and
3730 // the descriptors for the type should have been consistent within the same file at
3731 // compile time
3732 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003733 << " to be of type '" << insn_type
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003734 << "' but found type '" << *field_type << "' in Get";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003735 return;
3736 }
3737 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003738 if (!insn_type.IsAssignableFrom(*field_type)) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003739 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003740 << " to be compatible with type '" << insn_type
Sebastien Hertz757b3042014-03-28 14:34:28 +01003741 << "' but found type '" << *field_type
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003742 << "' in get-object";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003743 work_line_->SetRegisterType(vregA, reg_types_.Conflict());
3744 return;
3745 }
3746 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003747 if (!field_type->IsLowHalf()) {
3748 work_line_->SetRegisterType(vregA, *field_type);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003749 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003750 work_line_->SetRegisterTypeWide(vregA, *field_type, field_type->HighHalf(&reg_types_));
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003751 }
3752}
3753
3754void MethodVerifier::VerifyIPutQuick(const Instruction* inst, const RegType& insn_type,
3755 bool is_primitive) {
3756 DCHECK(Runtime::Current()->IsStarted());
Brian Carlstromea46f952013-07-30 01:26:50 -07003757 mirror::ArtField* field = GetQuickFieldAccess(inst, work_line_.get());
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003758 if (field == NULL) {
3759 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field from " << inst->Name();
3760 return;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003761 }
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07003762 const char* descriptor = field->GetTypeDescriptor();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003763 mirror::ClassLoader* loader = field->GetDeclaringClass()->GetClassLoader();
3764 const RegType& field_type = reg_types_.FromDescriptor(loader, descriptor, false);
3765 if (field != NULL) {
3766 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
3767 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003768 << " from other class " << GetDeclaringClass();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003769 return;
3770 }
3771 }
3772 const uint32_t vregA = inst->VRegA_22c();
3773 if (is_primitive) {
3774 // Primitive field assignability rules are weaker than regular assignability rules
3775 bool instruction_compatible;
3776 bool value_compatible;
3777 const RegType& value_type = work_line_->GetRegisterType(vregA);
3778 if (field_type.IsIntegralTypes()) {
3779 instruction_compatible = insn_type.IsIntegralTypes();
3780 value_compatible = value_type.IsIntegralTypes();
3781 } else if (field_type.IsFloat()) {
3782 instruction_compatible = insn_type.IsInteger(); // no [is]put-float, so expect [is]put-int
3783 value_compatible = value_type.IsFloatTypes();
3784 } else if (field_type.IsLong()) {
3785 instruction_compatible = insn_type.IsLong();
3786 value_compatible = value_type.IsLongTypes();
3787 } else if (field_type.IsDouble()) {
3788 instruction_compatible = insn_type.IsLong(); // no [is]put-double, so expect [is]put-long
3789 value_compatible = value_type.IsDoubleTypes();
3790 } else {
3791 instruction_compatible = false; // reference field with primitive store
3792 value_compatible = false; // unused
3793 }
3794 if (!instruction_compatible) {
3795 // This is a global failure rather than a class change failure as the instructions and
3796 // the descriptors for the type should have been consistent within the same file at
3797 // compile time
3798 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003799 << " to be of type '" << insn_type
3800 << "' but found type '" << field_type
3801 << "' in put";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003802 return;
3803 }
3804 if (!value_compatible) {
3805 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
3806 << " of type " << value_type
3807 << " but expected " << field_type
3808 << " for store to " << PrettyField(field) << " in put";
3809 return;
3810 }
3811 } else {
3812 if (!insn_type.IsAssignableFrom(field_type)) {
3813 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003814 << " to be compatible with type '" << insn_type
3815 << "' but found type '" << field_type
3816 << "' in put-object";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003817 return;
3818 }
3819 work_line_->VerifyRegisterType(vregA, field_type);
3820 }
3821}
3822
Ian Rogers776ac1f2012-04-13 23:36:36 -07003823bool MethodVerifier::CheckNotMoveException(const uint16_t* insns, int insn_idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003824 if ((insns[insn_idx] & 0xff) == Instruction::MOVE_EXCEPTION) {
jeffhaod5347e02012-03-22 17:25:05 -07003825 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-exception";
Ian Rogersd81871c2011-10-03 13:57:23 -07003826 return false;
3827 }
3828 return true;
3829}
3830
Ian Rogers776ac1f2012-04-13 23:36:36 -07003831bool MethodVerifier::UpdateRegisters(uint32_t next_insn, const RegisterLine* merge_line) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003832 bool changed = true;
3833 RegisterLine* target_line = reg_table_.GetLine(next_insn);
3834 if (!insn_flags_[next_insn].IsVisitedOrChanged()) {
jeffhaobdb76512011-09-07 11:43:16 -07003835 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003836 * We haven't processed this instruction before, and we haven't touched the registers here, so
3837 * there's nothing to "merge". Copy the registers over and mark it as changed. (This is the
3838 * only way a register can transition out of "unknown", so this is not just an optimization.)
jeffhaobdb76512011-09-07 11:43:16 -07003839 */
Ian Rogersb8c78592013-07-25 23:52:52 +00003840 if (!insn_flags_[next_insn].IsReturn()) {
3841 target_line->CopyFromLine(merge_line);
3842 } else {
Jeff Haob24b4a72013-07-31 13:47:31 -07003843 // Verify that the monitor stack is empty on return.
3844 if (!merge_line->VerifyMonitorStackEmpty()) {
3845 return false;
3846 }
Ian Rogersb8c78592013-07-25 23:52:52 +00003847 // For returns we only care about the operand to the return, all other registers are dead.
3848 // Initialize them as conflicts so they don't add to GC and deoptimization information.
3849 const Instruction* ret_inst = Instruction::At(code_item_->insns_ + next_insn);
3850 Instruction::Code opcode = ret_inst->Opcode();
3851 if ((opcode == Instruction::RETURN_VOID) || (opcode == Instruction::RETURN_VOID_BARRIER)) {
3852 target_line->MarkAllRegistersAsConflicts();
3853 } else {
3854 target_line->CopyFromLine(merge_line);
3855 if (opcode == Instruction::RETURN_WIDE) {
3856 target_line->MarkAllRegistersAsConflictsExceptWide(ret_inst->VRegA_11x());
3857 } else {
3858 target_line->MarkAllRegistersAsConflictsExcept(ret_inst->VRegA_11x());
3859 }
3860 }
3861 }
jeffhaobdb76512011-09-07 11:43:16 -07003862 } else {
Ian Rogers700a4022014-05-19 16:49:03 -07003863 std::unique_ptr<RegisterLine> copy(gDebugVerify ?
Ian Rogersd0fbd852013-09-24 18:17:04 -07003864 RegisterLine::Create(target_line->NumRegs(), this) :
Brian Carlstrom93c33962013-07-26 10:37:43 -07003865 NULL);
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003866 if (gDebugVerify) {
3867 copy->CopyFromLine(target_line);
3868 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003869 changed = target_line->MergeRegisters(merge_line);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003870 if (have_pending_hard_failure_) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003871 return false;
jeffhaobdb76512011-09-07 11:43:16 -07003872 }
Ian Rogers2c8a8572011-10-24 17:11:36 -07003873 if (gDebugVerify && changed) {
Elliott Hughes398f64b2012-03-26 18:05:48 -07003874 LogVerifyInfo() << "Merging at [" << reinterpret_cast<void*>(work_insn_idx_) << "]"
Elliott Hughesc073b072012-05-24 19:29:17 -07003875 << " to [" << reinterpret_cast<void*>(next_insn) << "]: " << "\n"
3876 << *copy.get() << " MERGE\n"
3877 << *merge_line << " ==\n"
3878 << *target_line << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07003879 }
3880 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003881 if (changed) {
3882 insn_flags_[next_insn].SetChanged();
jeffhaobdb76512011-09-07 11:43:16 -07003883 }
3884 return true;
3885}
3886
Ian Rogers7b3ddd22013-02-21 15:19:52 -08003887InstructionFlags* MethodVerifier::CurrentInsnFlags() {
Ian Rogers776ac1f2012-04-13 23:36:36 -07003888 return &insn_flags_[work_insn_idx_];
3889}
3890
Ian Rogersad0b3a32012-04-16 14:50:24 -07003891const RegType& MethodVerifier::GetMethodReturnType() {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003892 if (return_type_ == nullptr) {
3893 if (mirror_method_ != NULL) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003894 Thread* self = Thread::Current();
3895 StackHandleScope<1> hs(self);
3896 mirror::Class* return_type_class;
3897 {
3898 HandleWrapper<mirror::ArtMethod> h_mirror_method(hs.NewHandleWrapper(&mirror_method_));
3899 return_type_class = MethodHelper(h_mirror_method).GetReturnType(can_load_classes_);
3900 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003901 if (return_type_class != nullptr) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003902 return_type_ = &reg_types_.FromClass(mirror_method_->GetReturnTypeDescriptor(),
3903 return_type_class,
Mathieu Chartier590fee92013-09-13 13:46:47 -07003904 return_type_class->CannotBeAssignedFromOtherTypes());
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003905 } else {
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08003906 DCHECK(!can_load_classes_ || self->IsExceptionPending());
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003907 self->ClearException();
3908 }
3909 }
3910 if (return_type_ == nullptr) {
3911 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
3912 const DexFile::ProtoId& proto_id = dex_file_->GetMethodPrototype(method_id);
3913 uint16_t return_type_idx = proto_id.return_type_idx_;
3914 const char* descriptor = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(return_type_idx));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003915 return_type_ = &reg_types_.FromDescriptor(class_loader_->Get(), descriptor, false);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003916 }
3917 }
3918 return *return_type_;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003919}
3920
3921const RegType& MethodVerifier::GetDeclaringClass() {
Ian Rogers637c65b2013-05-31 11:46:00 -07003922 if (declaring_class_ == NULL) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003923 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
Brian Carlstrom93c33962013-07-26 10:37:43 -07003924 const char* descriptor
3925 = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(method_id.class_idx_));
Ian Rogers637c65b2013-05-31 11:46:00 -07003926 if (mirror_method_ != NULL) {
3927 mirror::Class* klass = mirror_method_->GetDeclaringClass();
Ian Rogers04f94f42013-06-10 15:09:26 -07003928 declaring_class_ = &reg_types_.FromClass(descriptor, klass,
3929 klass->CannotBeAssignedFromOtherTypes());
Ian Rogers637c65b2013-05-31 11:46:00 -07003930 } else {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003931 declaring_class_ = &reg_types_.FromDescriptor(class_loader_->Get(), descriptor, false);
Ian Rogers637c65b2013-05-31 11:46:00 -07003932 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003933 }
Ian Rogers637c65b2013-05-31 11:46:00 -07003934 return *declaring_class_;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003935}
3936
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003937std::vector<int32_t> MethodVerifier::DescribeVRegs(uint32_t dex_pc) {
3938 RegisterLine* line = reg_table_.GetLine(dex_pc);
Sebastien Hertzaa0c00c2014-03-14 17:58:54 +01003939 DCHECK(line != nullptr) << "No register line at DEX pc " << StringPrintf("0x%x", dex_pc);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003940 std::vector<int32_t> result;
3941 for (size_t i = 0; i < line->NumRegs(); ++i) {
3942 const RegType& type = line->GetRegisterType(i);
3943 if (type.IsConstant()) {
3944 result.push_back(type.IsPreciseConstant() ? kConstant : kImpreciseConstant);
3945 result.push_back(type.ConstantValue());
3946 } else if (type.IsConstantLo()) {
3947 result.push_back(type.IsPreciseConstantLo() ? kConstant : kImpreciseConstant);
3948 result.push_back(type.ConstantValueLo());
3949 } else if (type.IsConstantHi()) {
3950 result.push_back(type.IsPreciseConstantHi() ? kConstant : kImpreciseConstant);
3951 result.push_back(type.ConstantValueHi());
3952 } else if (type.IsIntegralTypes()) {
3953 result.push_back(kIntVReg);
3954 result.push_back(0);
3955 } else if (type.IsFloat()) {
3956 result.push_back(kFloatVReg);
3957 result.push_back(0);
3958 } else if (type.IsLong()) {
3959 result.push_back(kLongLoVReg);
3960 result.push_back(0);
3961 result.push_back(kLongHiVReg);
3962 result.push_back(0);
3963 ++i;
3964 } else if (type.IsDouble()) {
3965 result.push_back(kDoubleLoVReg);
3966 result.push_back(0);
3967 result.push_back(kDoubleHiVReg);
3968 result.push_back(0);
3969 ++i;
3970 } else if (type.IsUndefined() || type.IsConflict() || type.IsHighHalf()) {
3971 result.push_back(kUndefined);
3972 result.push_back(0);
3973 } else {
Ian Rogers7b3ddd22013-02-21 15:19:52 -08003974 CHECK(type.IsNonZeroReferenceTypes());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003975 result.push_back(kReferenceVReg);
3976 result.push_back(0);
3977 }
3978 }
3979 return result;
3980}
3981
Sebastien Hertz849600b2013-12-20 10:28:08 +01003982const RegType& MethodVerifier::DetermineCat1Constant(int32_t value, bool precise) {
3983 if (precise) {
3984 // Precise constant type.
3985 return reg_types_.FromCat1Const(value, true);
3986 } else {
3987 // Imprecise constant type.
3988 if (value < -32768) {
3989 return reg_types_.IntConstant();
3990 } else if (value < -128) {
3991 return reg_types_.ShortConstant();
3992 } else if (value < 0) {
3993 return reg_types_.ByteConstant();
3994 } else if (value == 0) {
3995 return reg_types_.Zero();
3996 } else if (value == 1) {
3997 return reg_types_.One();
3998 } else if (value < 128) {
3999 return reg_types_.PosByteConstant();
4000 } else if (value < 32768) {
4001 return reg_types_.PosShortConstant();
4002 } else if (value < 65536) {
4003 return reg_types_.CharConstant();
4004 } else {
4005 return reg_types_.IntConstant();
4006 }
4007 }
4008}
4009
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004010void MethodVerifier::Init() {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08004011 art::verifier::RegTypeCache::Init();
Brian Carlstrome7d856b2012-01-11 18:10:55 -08004012}
4013
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004014void MethodVerifier::Shutdown() {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08004015 verifier::RegTypeCache::ShutDown();
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08004016}
jeffhaod1224c72012-02-29 13:43:08 -08004017
Mathieu Chartier83c8ee02014-01-28 14:50:23 -08004018void MethodVerifier::VisitRoots(RootCallback* callback, void* arg) {
4019 reg_types_.VisitRoots(callback, arg);
Mathieu Chartierc528dba2013-11-26 12:00:11 -08004020}
4021
Ian Rogersd81871c2011-10-03 13:57:23 -07004022} // namespace verifier
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07004023} // namespace art