blob: c9c3bbabdfb9f66ac28c963239c77103ffd2a9be [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;
Andreas Gampe073ed9b2014-06-13 15:46:46 -0700277 if (duration_ns > MsToNs(100) && !kIsDebugBuild) {
Ian Rogersc8982582012-09-07 16:53:25 -0700278 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 }
Ian Rogers5fb22a92014-06-13 10:31:28 -0700734 if (inst->GetVerifyIsRuntimeOnly() && Runtime::Current()->IsCompiler()) {
735 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "opcode only expected at runtime " << inst->Name();
736 result = false;
737 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700738 return result;
739}
740
Ian Rogers776ac1f2012-04-13 23:36:36 -0700741bool MethodVerifier::CheckRegisterIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700742 if (idx >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700743 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register index out of range (" << idx << " >= "
744 << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700745 return false;
746 }
747 return true;
748}
749
Ian Rogers776ac1f2012-04-13 23:36:36 -0700750bool MethodVerifier::CheckWideRegisterIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700751 if (idx + 1 >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700752 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "wide register index out of range (" << idx
753 << "+1 >= " << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700754 return false;
755 }
756 return true;
757}
758
Ian Rogers776ac1f2012-04-13 23:36:36 -0700759bool MethodVerifier::CheckFieldIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700760 if (idx >= dex_file_->GetHeader().field_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700761 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad field index " << idx << " (max "
762 << dex_file_->GetHeader().field_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700763 return false;
764 }
765 return true;
766}
767
Ian Rogers776ac1f2012-04-13 23:36:36 -0700768bool MethodVerifier::CheckMethodIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700769 if (idx >= dex_file_->GetHeader().method_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700770 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad method index " << idx << " (max "
771 << dex_file_->GetHeader().method_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700772 return false;
773 }
774 return true;
775}
776
Ian Rogers776ac1f2012-04-13 23:36:36 -0700777bool MethodVerifier::CheckNewInstance(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700778 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700779 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
780 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700781 return false;
782 }
783 // We don't need the actual class, just a pointer to the class name.
Ian Rogers0571d352011-11-03 19:51:38 -0700784 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -0700785 if (descriptor[0] != 'L') {
jeffhaod5347e02012-03-22 17:25:05 -0700786 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "can't call new-instance on type '" << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -0700787 return false;
788 }
789 return true;
790}
791
Ian Rogers776ac1f2012-04-13 23:36:36 -0700792bool MethodVerifier::CheckStringIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700793 if (idx >= dex_file_->GetHeader().string_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700794 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad string index " << idx << " (max "
795 << dex_file_->GetHeader().string_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700796 return false;
797 }
798 return true;
799}
800
Ian Rogers776ac1f2012-04-13 23:36:36 -0700801bool MethodVerifier::CheckTypeIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700802 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700803 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
804 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700805 return false;
806 }
807 return true;
808}
809
Ian Rogers776ac1f2012-04-13 23:36:36 -0700810bool MethodVerifier::CheckNewArray(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700811 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700812 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
813 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700814 return false;
815 }
816 int bracket_count = 0;
Ian Rogers0571d352011-11-03 19:51:38 -0700817 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -0700818 const char* cp = descriptor;
819 while (*cp++ == '[') {
820 bracket_count++;
821 }
822 if (bracket_count == 0) {
823 /* The given class must be an array type. */
Brian Carlstrom93c33962013-07-26 10:37:43 -0700824 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
825 << "can't new-array class '" << descriptor << "' (not an array)";
Ian Rogersd81871c2011-10-03 13:57:23 -0700826 return false;
827 } else if (bracket_count > 255) {
828 /* It is illegal to create an array of more than 255 dimensions. */
Brian Carlstrom93c33962013-07-26 10:37:43 -0700829 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
830 << "can't new-array class '" << descriptor << "' (exceeds limit)";
Ian Rogersd81871c2011-10-03 13:57:23 -0700831 return false;
832 }
833 return true;
834}
835
Ian Rogers776ac1f2012-04-13 23:36:36 -0700836bool MethodVerifier::CheckArrayData(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700837 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
838 const uint16_t* insns = code_item_->insns_ + cur_offset;
839 const uint16_t* array_data;
840 int32_t array_data_offset;
841
842 DCHECK_LT(cur_offset, insn_count);
843 /* make sure the start of the array data table is in range */
844 array_data_offset = insns[1] | (((int32_t) insns[2]) << 16);
845 if ((int32_t) cur_offset + array_data_offset < 0 ||
846 cur_offset + array_data_offset + 2 >= insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -0700847 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data start: at " << cur_offset
Brian Carlstrom93c33962013-07-26 10:37:43 -0700848 << ", data offset " << array_data_offset
849 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -0700850 return false;
851 }
852 /* offset to array data table is a relative branch-style offset */
853 array_data = insns + array_data_offset;
854 /* make sure the table is 32-bit aligned */
Ian Rogersef7d42f2014-01-06 12:55:46 -0800855 if ((reinterpret_cast<uintptr_t>(array_data) & 0x03) != 0) {
jeffhaod5347e02012-03-22 17:25:05 -0700856 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned array data table: at " << cur_offset
857 << ", data offset " << array_data_offset;
Ian Rogersd81871c2011-10-03 13:57:23 -0700858 return false;
859 }
860 uint32_t value_width = array_data[1];
Elliott Hughes398f64b2012-03-26 18:05:48 -0700861 uint32_t value_count = *reinterpret_cast<const uint32_t*>(&array_data[2]);
Ian Rogersd81871c2011-10-03 13:57:23 -0700862 uint32_t table_size = 4 + (value_width * value_count + 1) / 2;
863 /* make sure the end of the switch is in range */
864 if (cur_offset + array_data_offset + table_size > insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -0700865 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data end: at " << cur_offset
866 << ", data offset " << array_data_offset << ", end "
867 << cur_offset + array_data_offset + table_size
868 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -0700869 return false;
870 }
871 return true;
872}
873
Ian Rogers776ac1f2012-04-13 23:36:36 -0700874bool MethodVerifier::CheckBranchTarget(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700875 int32_t offset;
876 bool isConditional, selfOkay;
877 if (!GetBranchOffset(cur_offset, &offset, &isConditional, &selfOkay)) {
878 return false;
879 }
880 if (!selfOkay && offset == 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700881 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch offset of zero not allowed at"
882 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -0700883 return false;
884 }
Elliott Hughes81ff3182012-03-23 20:35:56 -0700885 // Check for 32-bit overflow. This isn't strictly necessary if we can depend on the runtime
886 // to have identical "wrap-around" behavior, but it's unwise to depend on that.
Ian Rogersd81871c2011-10-03 13:57:23 -0700887 if (((int64_t) cur_offset + (int64_t) offset) != (int64_t) (cur_offset + offset)) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700888 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch target overflow "
889 << reinterpret_cast<void*>(cur_offset) << " +" << offset;
Ian Rogersd81871c2011-10-03 13:57:23 -0700890 return false;
891 }
892 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
893 int32_t abs_offset = cur_offset + offset;
Brian Carlstrom93c33962013-07-26 10:37:43 -0700894 if (abs_offset < 0 ||
895 (uint32_t) abs_offset >= insn_count ||
896 !insn_flags_[abs_offset].IsOpcode()) {
jeffhaod5347e02012-03-22 17:25:05 -0700897 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid branch target " << offset << " (-> "
Elliott Hughes398f64b2012-03-26 18:05:48 -0700898 << reinterpret_cast<void*>(abs_offset) << ") at "
899 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -0700900 return false;
901 }
902 insn_flags_[abs_offset].SetBranchTarget();
903 return true;
904}
905
Ian Rogers776ac1f2012-04-13 23:36:36 -0700906bool MethodVerifier::GetBranchOffset(uint32_t cur_offset, int32_t* pOffset, bool* pConditional,
Ian Rogersd81871c2011-10-03 13:57:23 -0700907 bool* selfOkay) {
908 const uint16_t* insns = code_item_->insns_ + cur_offset;
909 *pConditional = false;
910 *selfOkay = false;
jeffhaoba5ebb92011-08-25 17:24:37 -0700911 switch (*insns & 0xff) {
912 case Instruction::GOTO:
913 *pOffset = ((int16_t) *insns) >> 8;
jeffhaoba5ebb92011-08-25 17:24:37 -0700914 break;
915 case Instruction::GOTO_32:
916 *pOffset = insns[1] | (((uint32_t) insns[2]) << 16);
jeffhaoba5ebb92011-08-25 17:24:37 -0700917 *selfOkay = true;
918 break;
919 case Instruction::GOTO_16:
920 *pOffset = (int16_t) insns[1];
jeffhaoba5ebb92011-08-25 17:24:37 -0700921 break;
922 case Instruction::IF_EQ:
923 case Instruction::IF_NE:
924 case Instruction::IF_LT:
925 case Instruction::IF_GE:
926 case Instruction::IF_GT:
927 case Instruction::IF_LE:
928 case Instruction::IF_EQZ:
929 case Instruction::IF_NEZ:
930 case Instruction::IF_LTZ:
931 case Instruction::IF_GEZ:
932 case Instruction::IF_GTZ:
933 case Instruction::IF_LEZ:
934 *pOffset = (int16_t) insns[1];
935 *pConditional = true;
jeffhaoba5ebb92011-08-25 17:24:37 -0700936 break;
937 default:
938 return false;
939 break;
940 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700941 return true;
942}
943
Ian Rogers776ac1f2012-04-13 23:36:36 -0700944bool MethodVerifier::CheckSwitchTargets(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700945 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700946 DCHECK_LT(cur_offset, insn_count);
Ian Rogersd81871c2011-10-03 13:57:23 -0700947 const uint16_t* insns = code_item_->insns_ + cur_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -0700948 /* make sure the start of the switch is in range */
Ian Rogersd81871c2011-10-03 13:57:23 -0700949 int32_t switch_offset = insns[1] | ((int32_t) insns[2]) << 16;
950 if ((int32_t) cur_offset + switch_offset < 0 || cur_offset + switch_offset + 2 >= insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -0700951 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch start: at " << cur_offset
Brian Carlstrom93c33962013-07-26 10:37:43 -0700952 << ", switch offset " << switch_offset
953 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -0700954 return false;
955 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700956 /* offset to switch table is a relative branch-style offset */
Ian Rogersd81871c2011-10-03 13:57:23 -0700957 const uint16_t* switch_insns = insns + switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -0700958 /* make sure the table is 32-bit aligned */
Ian Rogersef7d42f2014-01-06 12:55:46 -0800959 if ((reinterpret_cast<uintptr_t>(switch_insns) & 0x03) != 0) {
jeffhaod5347e02012-03-22 17:25:05 -0700960 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned switch table: at " << cur_offset
961 << ", switch offset " << switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -0700962 return false;
963 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700964 uint32_t switch_count = switch_insns[1];
965 int32_t keys_offset, targets_offset;
966 uint16_t expected_signature;
jeffhaoba5ebb92011-08-25 17:24:37 -0700967 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
968 /* 0=sig, 1=count, 2/3=firstKey */
969 targets_offset = 4;
970 keys_offset = -1;
971 expected_signature = Instruction::kPackedSwitchSignature;
972 } else {
973 /* 0=sig, 1=count, 2..count*2 = keys */
974 keys_offset = 2;
975 targets_offset = 2 + 2 * switch_count;
976 expected_signature = Instruction::kSparseSwitchSignature;
977 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700978 uint32_t table_size = targets_offset + switch_count * 2;
jeffhaoba5ebb92011-08-25 17:24:37 -0700979 if (switch_insns[0] != expected_signature) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700980 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
981 << StringPrintf("wrong signature for switch table (%x, wanted %x)",
982 switch_insns[0], expected_signature);
jeffhaoba5ebb92011-08-25 17:24:37 -0700983 return false;
984 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700985 /* make sure the end of the switch is in range */
986 if (cur_offset + switch_offset + table_size > (uint32_t) insn_count) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700987 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch end: at " << cur_offset
988 << ", switch offset " << switch_offset
989 << ", end " << (cur_offset + switch_offset + table_size)
jeffhaod5347e02012-03-22 17:25:05 -0700990 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -0700991 return false;
992 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700993 /* for a sparse switch, verify the keys are in ascending order */
994 if (keys_offset > 0 && switch_count > 1) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700995 int32_t last_key = switch_insns[keys_offset] | (switch_insns[keys_offset + 1] << 16);
996 for (uint32_t targ = 1; targ < switch_count; targ++) {
jeffhaoba5ebb92011-08-25 17:24:37 -0700997 int32_t key = (int32_t) switch_insns[keys_offset + targ * 2] |
998 (int32_t) (switch_insns[keys_offset + targ * 2 + 1] << 16);
999 if (key <= last_key) {
jeffhaod5347e02012-03-22 17:25:05 -07001000 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid packed switch: last key=" << last_key
1001 << ", this=" << key;
jeffhaoba5ebb92011-08-25 17:24:37 -07001002 return false;
1003 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001004 last_key = key;
1005 }
1006 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001007 /* verify each switch target */
Ian Rogersd81871c2011-10-03 13:57:23 -07001008 for (uint32_t targ = 0; targ < switch_count; targ++) {
1009 int32_t offset = (int32_t) switch_insns[targets_offset + targ * 2] |
1010 (int32_t) (switch_insns[targets_offset + targ * 2 + 1] << 16);
1011 int32_t abs_offset = cur_offset + offset;
Brian Carlstrom93c33962013-07-26 10:37:43 -07001012 if (abs_offset < 0 ||
1013 abs_offset >= (int32_t) insn_count ||
1014 !insn_flags_[abs_offset].IsOpcode()) {
1015 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch target " << offset
1016 << " (-> " << reinterpret_cast<void*>(abs_offset) << ") at "
1017 << reinterpret_cast<void*>(cur_offset)
1018 << "[" << targ << "]";
jeffhaoba5ebb92011-08-25 17:24:37 -07001019 return false;
1020 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001021 insn_flags_[abs_offset].SetBranchTarget();
1022 }
1023 return true;
1024}
1025
Ian Rogers776ac1f2012-04-13 23:36:36 -07001026bool MethodVerifier::CheckVarArgRegs(uint32_t vA, uint32_t arg[]) {
Ian Rogers29a26482014-05-02 15:27:29 -07001027 if (vA > Instruction::kMaxVarArgRegs) {
jeffhaod5347e02012-03-22 17:25:05 -07001028 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << vA << ") in non-range invoke)";
Ian Rogersd81871c2011-10-03 13:57:23 -07001029 return false;
1030 }
1031 uint16_t registers_size = code_item_->registers_size_;
1032 for (uint32_t idx = 0; idx < vA; idx++) {
jeffhao457cc512012-02-02 16:55:13 -08001033 if (arg[idx] >= registers_size) {
jeffhaod5347e02012-03-22 17:25:05 -07001034 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index (" << arg[idx]
1035 << ") in non-range invoke (>= " << registers_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001036 return false;
1037 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001038 }
1039
1040 return true;
1041}
1042
Ian Rogers776ac1f2012-04-13 23:36:36 -07001043bool MethodVerifier::CheckVarArgRangeRegs(uint32_t vA, uint32_t vC) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001044 uint16_t registers_size = code_item_->registers_size_;
1045 // vA/vC are unsigned 8-bit/16-bit quantities for /range instructions, so there's no risk of
1046 // integer overflow when adding them here.
1047 if (vA + vC > registers_size) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001048 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index " << vA << "+" << vC
1049 << " in range invoke (> " << registers_size << ")";
jeffhaoba5ebb92011-08-25 17:24:37 -07001050 return false;
1051 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001052 return true;
1053}
1054
Ian Rogers776ac1f2012-04-13 23:36:36 -07001055bool MethodVerifier::VerifyCodeFlow() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001056 uint16_t registers_size = code_item_->registers_size_;
1057 uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaobdb76512011-09-07 11:43:16 -07001058
Ian Rogersd81871c2011-10-03 13:57:23 -07001059 if (registers_size * insns_size > 4*1024*1024) {
buzbee4922ef92012-02-24 14:32:20 -08001060 LOG(WARNING) << "warning: method is huge (regs=" << registers_size
1061 << " insns_size=" << insns_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001062 }
1063 /* Create and initialize table holding register status */
Brian Carlstrom93c33962013-07-26 10:37:43 -07001064 reg_table_.Init(kTrackCompilerInterestPoints,
1065 insn_flags_.get(),
1066 insns_size,
1067 registers_size,
1068 this);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07001069
jeffhaobdb76512011-09-07 11:43:16 -07001070
Ian Rogersd0fbd852013-09-24 18:17:04 -07001071 work_line_.reset(RegisterLine::Create(registers_size, this));
1072 saved_line_.reset(RegisterLine::Create(registers_size, this));
jeffhaobdb76512011-09-07 11:43:16 -07001073
Ian Rogersd81871c2011-10-03 13:57:23 -07001074 /* Initialize register types of method arguments. */
1075 if (!SetTypesFromSignature()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001076 DCHECK_NE(failures_.size(), 0U);
1077 std::string prepend("Bad signature in ");
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001078 prepend += PrettyMethod(dex_method_idx_, *dex_file_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001079 PrependToLastFailMessage(prepend);
Ian Rogersd81871c2011-10-03 13:57:23 -07001080 return false;
1081 }
1082 /* Perform code flow verification. */
1083 if (!CodeFlowVerifyMethod()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001084 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -07001085 return false;
jeffhaobdb76512011-09-07 11:43:16 -07001086 }
jeffhaobdb76512011-09-07 11:43:16 -07001087 return true;
1088}
1089
Ian Rogersad0b3a32012-04-16 14:50:24 -07001090std::ostream& MethodVerifier::DumpFailures(std::ostream& os) {
1091 DCHECK_EQ(failures_.size(), failure_messages_.size());
Jeff Hao4137f482013-11-22 11:44:57 -08001092 for (size_t i = 0; i < failures_.size(); ++i) {
1093 os << failure_messages_[i]->str() << "\n";
Ian Rogersad0b3a32012-04-16 14:50:24 -07001094 }
1095 return os;
1096}
1097
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001098extern "C" void MethodVerifierGdbDump(MethodVerifier* v)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001099 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001100 v->Dump(std::cerr);
1101}
1102
Ian Rogers776ac1f2012-04-13 23:36:36 -07001103void MethodVerifier::Dump(std::ostream& os) {
jeffhaof56197c2012-03-05 18:01:54 -08001104 if (code_item_ == NULL) {
Elliott Hughesc073b072012-05-24 19:29:17 -07001105 os << "Native method\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001106 return;
jeffhaobdb76512011-09-07 11:43:16 -07001107 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001108 {
1109 os << "Register Types:\n";
1110 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
1111 std::ostream indent_os(&indent_filter);
1112 reg_types_.Dump(indent_os);
1113 }
Ian Rogersb4903572012-10-11 11:52:56 -07001114 os << "Dumping instructions and register lines:\n";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001115 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
1116 std::ostream indent_os(&indent_filter);
Ian Rogersd81871c2011-10-03 13:57:23 -07001117 const Instruction* inst = Instruction::At(code_item_->insns_);
1118 for (size_t dex_pc = 0; dex_pc < code_item_->insns_size_in_code_units_;
1119 dex_pc += insn_flags_[dex_pc].GetLengthInCodeUnits()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001120 RegisterLine* reg_line = reg_table_.GetLine(dex_pc);
1121 if (reg_line != NULL) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001122 indent_os << reg_line->Dump() << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07001123 }
Ian Rogers7b3ddd22013-02-21 15:19:52 -08001124 indent_os << StringPrintf("0x%04zx", dex_pc) << ": " << insn_flags_[dex_pc].ToString() << " ";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001125 const bool kDumpHexOfInstruction = false;
1126 if (kDumpHexOfInstruction) {
1127 indent_os << inst->DumpHex(5) << " ";
1128 }
1129 indent_os << inst->DumpString(dex_file_) << "\n";
jeffhaoba5ebb92011-08-25 17:24:37 -07001130 inst = inst->Next();
1131 }
jeffhaobdb76512011-09-07 11:43:16 -07001132}
1133
Ian Rogersd81871c2011-10-03 13:57:23 -07001134static bool IsPrimitiveDescriptor(char descriptor) {
1135 switch (descriptor) {
jeffhaobdb76512011-09-07 11:43:16 -07001136 case 'I':
1137 case 'C':
1138 case 'S':
1139 case 'B':
1140 case 'Z':
jeffhaobdb76512011-09-07 11:43:16 -07001141 case 'F':
1142 case 'D':
1143 case 'J':
Ian Rogersd81871c2011-10-03 13:57:23 -07001144 return true;
jeffhaobdb76512011-09-07 11:43:16 -07001145 default:
1146 return false;
1147 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001148}
1149
Ian Rogers776ac1f2012-04-13 23:36:36 -07001150bool MethodVerifier::SetTypesFromSignature() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001151 RegisterLine* reg_line = reg_table_.GetLine(0);
1152 int arg_start = code_item_->registers_size_ - code_item_->ins_size_;
1153 size_t expected_args = code_item_->ins_size_; /* long/double count as two */
jeffhaobdb76512011-09-07 11:43:16 -07001154
Ian Rogersd81871c2011-10-03 13:57:23 -07001155 DCHECK_GE(arg_start, 0); /* should have been verified earlier */
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001156 // Include the "this" pointer.
Ian Rogersd81871c2011-10-03 13:57:23 -07001157 size_t cur_arg = 0;
Ian Rogersad0b3a32012-04-16 14:50:24 -07001158 if (!IsStatic()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001159 // If this is a constructor for a class other than java.lang.Object, mark the first ("this")
1160 // argument as uninitialized. This restricts field access until the superclass constructor is
1161 // called.
Ian Rogersad0b3a32012-04-16 14:50:24 -07001162 const RegType& declaring_class = GetDeclaringClass();
1163 if (IsConstructor() && !declaring_class.IsJavaLangObject()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001164 reg_line->SetRegisterType(arg_start + cur_arg,
1165 reg_types_.UninitializedThisArgument(declaring_class));
1166 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001167 reg_line->SetRegisterType(arg_start + cur_arg, declaring_class);
jeffhaobdb76512011-09-07 11:43:16 -07001168 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001169 cur_arg++;
jeffhaobdb76512011-09-07 11:43:16 -07001170 }
1171
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001172 const DexFile::ProtoId& proto_id =
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001173 dex_file_->GetMethodPrototype(dex_file_->GetMethodId(dex_method_idx_));
Ian Rogers0571d352011-11-03 19:51:38 -07001174 DexFileParameterIterator iterator(*dex_file_, proto_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07001175
1176 for (; iterator.HasNext(); iterator.Next()) {
1177 const char* descriptor = iterator.GetDescriptor();
1178 if (descriptor == NULL) {
1179 LOG(FATAL) << "Null descriptor";
1180 }
1181 if (cur_arg >= expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07001182 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1183 << " args, found more (" << descriptor << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001184 return false;
1185 }
1186 switch (descriptor[0]) {
1187 case 'L':
1188 case '[':
1189 // We assume that reference arguments are initialized. The only way it could be otherwise
1190 // (assuming the caller was verified) is if the current method is <init>, but in that case
1191 // it's effectively considered initialized the instant we reach here (in the sense that we
1192 // can return without doing anything or call virtual methods).
1193 {
Sebastien Hertz2ed76f92014-04-22 17:11:08 +02001194 const RegType& reg_type = ResolveClassAndCheckAccess(iterator.GetTypeIdx());
1195 if (!reg_type.IsNonZeroReferenceTypes()) {
1196 DCHECK(HasFailures());
1197 return false;
1198 }
Ian Rogers84fa0742011-10-25 18:13:30 -07001199 reg_line->SetRegisterType(arg_start + cur_arg, reg_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001200 }
1201 break;
1202 case 'Z':
1203 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Boolean());
1204 break;
1205 case 'C':
1206 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Char());
1207 break;
1208 case 'B':
1209 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Byte());
1210 break;
1211 case 'I':
1212 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Integer());
1213 break;
1214 case 'S':
1215 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Short());
1216 break;
1217 case 'F':
1218 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Float());
1219 break;
1220 case 'J':
1221 case 'D': {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001222 const RegType& lo_half = descriptor[0] == 'J' ? reg_types_.LongLo() : reg_types_.DoubleLo();
1223 const RegType& hi_half = descriptor[0] == 'J' ? reg_types_.LongHi() : reg_types_.DoubleHi();
1224 reg_line->SetRegisterTypeWide(arg_start + cur_arg, lo_half, hi_half);
Ian Rogersd81871c2011-10-03 13:57:23 -07001225 cur_arg++;
1226 break;
1227 }
1228 default:
Brian Carlstrom93c33962013-07-26 10:37:43 -07001229 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected signature type char '"
1230 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001231 return false;
1232 }
1233 cur_arg++;
1234 }
1235 if (cur_arg != expected_args) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001236 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1237 << " arguments, found " << cur_arg;
Ian Rogersd81871c2011-10-03 13:57:23 -07001238 return false;
1239 }
1240 const char* descriptor = dex_file_->GetReturnTypeDescriptor(proto_id);
1241 // Validate return type. We don't do the type lookup; just want to make sure that it has the right
1242 // format. Only major difference from the method argument format is that 'V' is supported.
1243 bool result;
1244 if (IsPrimitiveDescriptor(descriptor[0]) || descriptor[0] == 'V') {
1245 result = descriptor[1] == '\0';
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001246 } else if (descriptor[0] == '[') { // single/multi-dimensional array of object/primitive
Ian Rogersd81871c2011-10-03 13:57:23 -07001247 size_t i = 0;
1248 do {
1249 i++;
1250 } while (descriptor[i] == '['); // process leading [
1251 if (descriptor[i] == 'L') { // object array
1252 do {
1253 i++; // find closing ;
1254 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1255 result = descriptor[i] == ';';
1256 } else { // primitive array
1257 result = IsPrimitiveDescriptor(descriptor[i]) && descriptor[i + 1] == '\0';
1258 }
1259 } else if (descriptor[0] == 'L') {
1260 // could be more thorough here, but shouldn't be required
1261 size_t i = 0;
1262 do {
1263 i++;
1264 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1265 result = descriptor[i] == ';';
1266 } else {
1267 result = false;
1268 }
1269 if (!result) {
jeffhaod5347e02012-03-22 17:25:05 -07001270 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected char in return type descriptor '"
1271 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001272 }
1273 return result;
jeffhaobdb76512011-09-07 11:43:16 -07001274}
1275
Ian Rogers776ac1f2012-04-13 23:36:36 -07001276bool MethodVerifier::CodeFlowVerifyMethod() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001277 const uint16_t* insns = code_item_->insns_;
1278 const uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaoba5ebb92011-08-25 17:24:37 -07001279
jeffhaobdb76512011-09-07 11:43:16 -07001280 /* Begin by marking the first instruction as "changed". */
Ian Rogersd81871c2011-10-03 13:57:23 -07001281 insn_flags_[0].SetChanged();
1282 uint32_t start_guess = 0;
jeffhaoba5ebb92011-08-25 17:24:37 -07001283
jeffhaobdb76512011-09-07 11:43:16 -07001284 /* Continue until no instructions are marked "changed". */
1285 while (true) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001286 // Find the first marked one. Use "start_guess" as a way to find one quickly.
1287 uint32_t insn_idx = start_guess;
1288 for (; insn_idx < insns_size; insn_idx++) {
1289 if (insn_flags_[insn_idx].IsChanged())
jeffhaobdb76512011-09-07 11:43:16 -07001290 break;
1291 }
jeffhaobdb76512011-09-07 11:43:16 -07001292 if (insn_idx == insns_size) {
1293 if (start_guess != 0) {
1294 /* try again, starting from the top */
1295 start_guess = 0;
1296 continue;
1297 } else {
1298 /* all flags are clear */
1299 break;
1300 }
1301 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001302 // We carry the working set of registers from instruction to instruction. If this address can
1303 // be the target of a branch (or throw) instruction, or if we're skipping around chasing
1304 // "changed" flags, we need to load the set of registers from the table.
1305 // Because we always prefer to continue on to the next instruction, we should never have a
1306 // situation where we have a stray "changed" flag set on an instruction that isn't a branch
1307 // target.
1308 work_insn_idx_ = insn_idx;
1309 if (insn_flags_[insn_idx].IsBranchTarget()) {
1310 work_line_->CopyFromLine(reg_table_.GetLine(insn_idx));
jeffhaobdb76512011-09-07 11:43:16 -07001311 } else {
1312#ifndef NDEBUG
1313 /*
1314 * Sanity check: retrieve the stored register line (assuming
1315 * a full table) and make sure it actually matches.
1316 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001317 RegisterLine* register_line = reg_table_.GetLine(insn_idx);
1318 if (register_line != NULL) {
1319 if (work_line_->CompareLine(register_line) != 0) {
1320 Dump(std::cout);
1321 std::cout << info_messages_.str();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001322 LOG(FATAL) << "work_line diverged in " << PrettyMethod(dex_method_idx_, *dex_file_)
Elliott Hughesc073b072012-05-24 19:29:17 -07001323 << "@" << reinterpret_cast<void*>(work_insn_idx_) << "\n"
1324 << " work_line=" << *work_line_ << "\n"
Elliott Hughes398f64b2012-03-26 18:05:48 -07001325 << " expected=" << *register_line;
Ian Rogersd81871c2011-10-03 13:57:23 -07001326 }
jeffhaobdb76512011-09-07 11:43:16 -07001327 }
1328#endif
1329 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001330 if (!CodeFlowVerifyInstruction(&start_guess)) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001331 std::string prepend(PrettyMethod(dex_method_idx_, *dex_file_));
Ian Rogersad0b3a32012-04-16 14:50:24 -07001332 prepend += " failed to verify: ";
1333 PrependToLastFailMessage(prepend);
jeffhaoba5ebb92011-08-25 17:24:37 -07001334 return false;
1335 }
jeffhaobdb76512011-09-07 11:43:16 -07001336 /* Clear "changed" and mark as visited. */
Ian Rogersd81871c2011-10-03 13:57:23 -07001337 insn_flags_[insn_idx].SetVisited();
1338 insn_flags_[insn_idx].ClearChanged();
jeffhaobdb76512011-09-07 11:43:16 -07001339 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001340
Ian Rogers1c849e52012-06-28 14:00:33 -07001341 if (gDebugVerify) {
jeffhaobdb76512011-09-07 11:43:16 -07001342 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001343 * Scan for dead code. There's nothing "evil" about dead code
jeffhaobdb76512011-09-07 11:43:16 -07001344 * (besides the wasted space), but it indicates a flaw somewhere
1345 * down the line, possibly in the verifier.
1346 *
1347 * If we've substituted "always throw" instructions into the stream,
1348 * we are almost certainly going to have some dead code.
1349 */
1350 int dead_start = -1;
Ian Rogersd81871c2011-10-03 13:57:23 -07001351 uint32_t insn_idx = 0;
1352 for (; insn_idx < insns_size; insn_idx += insn_flags_[insn_idx].GetLengthInCodeUnits()) {
jeffhaobdb76512011-09-07 11:43:16 -07001353 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001354 * Switch-statement data doesn't get "visited" by scanner. It
jeffhaobdb76512011-09-07 11:43:16 -07001355 * may or may not be preceded by a padding NOP (for alignment).
1356 */
1357 if (insns[insn_idx] == Instruction::kPackedSwitchSignature ||
1358 insns[insn_idx] == Instruction::kSparseSwitchSignature ||
1359 insns[insn_idx] == Instruction::kArrayDataSignature ||
Elliott Hughes380aaa72012-07-09 14:33:15 -07001360 (insns[insn_idx] == Instruction::NOP && (insn_idx + 1 < insns_size) &&
jeffhaobdb76512011-09-07 11:43:16 -07001361 (insns[insn_idx + 1] == Instruction::kPackedSwitchSignature ||
1362 insns[insn_idx + 1] == Instruction::kSparseSwitchSignature ||
1363 insns[insn_idx + 1] == Instruction::kArrayDataSignature))) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001364 insn_flags_[insn_idx].SetVisited();
jeffhaobdb76512011-09-07 11:43:16 -07001365 }
1366
Ian Rogersd81871c2011-10-03 13:57:23 -07001367 if (!insn_flags_[insn_idx].IsVisited()) {
jeffhaobdb76512011-09-07 11:43:16 -07001368 if (dead_start < 0)
1369 dead_start = insn_idx;
1370 } else if (dead_start >= 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001371 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start)
1372 << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaobdb76512011-09-07 11:43:16 -07001373 dead_start = -1;
1374 }
1375 }
1376 if (dead_start >= 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001377 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start)
1378 << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaoba5ebb92011-08-25 17:24:37 -07001379 }
Ian Rogersc9e463c2013-06-05 16:52:26 -07001380 // To dump the state of the verify after a method, do something like:
1381 // if (PrettyMethod(dex_method_idx_, *dex_file_) ==
1382 // "boolean java.lang.String.equals(java.lang.Object)") {
1383 // LOG(INFO) << info_messages_.str();
1384 // }
jeffhaoba5ebb92011-08-25 17:24:37 -07001385 }
jeffhaobdb76512011-09-07 11:43:16 -07001386 return true;
1387}
1388
Ian Rogers776ac1f2012-04-13 23:36:36 -07001389bool MethodVerifier::CodeFlowVerifyInstruction(uint32_t* start_guess) {
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001390 // If we're doing FindLocksAtDexPc, check whether we're at the dex pc we care about.
1391 // We want the state _before_ the instruction, for the case where the dex pc we're
1392 // interested in is itself a monitor-enter instruction (which is a likely place
1393 // for a thread to be suspended).
1394 if (monitor_enter_dex_pcs_ != NULL && work_insn_idx_ == interesting_dex_pc_) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001395 monitor_enter_dex_pcs_->clear(); // The new work line is more accurate than the previous one.
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001396 for (size_t i = 0; i < work_line_->GetMonitorEnterCount(); ++i) {
1397 monitor_enter_dex_pcs_->push_back(work_line_->GetMonitorEnterDexPc(i));
1398 }
1399 }
1400
jeffhaobdb76512011-09-07 11:43:16 -07001401 /*
1402 * Once we finish decoding the instruction, we need to figure out where
jeffhaod1f0fde2011-09-08 17:25:33 -07001403 * we can go from here. There are three possible ways to transfer
jeffhaobdb76512011-09-07 11:43:16 -07001404 * control to another statement:
1405 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001406 * (1) Continue to the next instruction. Applies to all but
jeffhaobdb76512011-09-07 11:43:16 -07001407 * unconditional branches, method returns, and exception throws.
jeffhaod1f0fde2011-09-08 17:25:33 -07001408 * (2) Branch to one or more possible locations. Applies to branches
jeffhaobdb76512011-09-07 11:43:16 -07001409 * and switch statements.
jeffhaod1f0fde2011-09-08 17:25:33 -07001410 * (3) Exception handlers. Applies to any instruction that can
jeffhaobdb76512011-09-07 11:43:16 -07001411 * throw an exception that is handled by an encompassing "try"
1412 * block.
1413 *
1414 * We can also return, in which case there is no successor instruction
1415 * from this point.
1416 *
Elliott Hughesadb8c672012-03-06 16:49:32 -08001417 * The behavior can be determined from the opcode flags.
jeffhaobdb76512011-09-07 11:43:16 -07001418 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001419 const uint16_t* insns = code_item_->insns_ + work_insn_idx_;
1420 const Instruction* inst = Instruction::At(insns);
Ian Rogersa75a0132012-09-28 11:41:42 -07001421 int opcode_flags = Instruction::FlagsOf(inst->Opcode());
jeffhaobdb76512011-09-07 11:43:16 -07001422
jeffhaobdb76512011-09-07 11:43:16 -07001423 int32_t branch_target = 0;
jeffhaobdb76512011-09-07 11:43:16 -07001424 bool just_set_result = false;
Ian Rogers2c8a8572011-10-24 17:11:36 -07001425 if (gDebugVerify) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001426 // Generate processing back trace to debug verifier
Elliott Hughesc073b072012-05-24 19:29:17 -07001427 LogVerifyInfo() << "Processing " << inst->DumpString(dex_file_) << "\n"
1428 << *work_line_.get() << "\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001429 }
jeffhaobdb76512011-09-07 11:43:16 -07001430
1431 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001432 * Make a copy of the previous register state. If the instruction
jeffhaobdb76512011-09-07 11:43:16 -07001433 * can throw an exception, we will copy/merge this into the "catch"
1434 * address rather than work_line, because we don't want the result
1435 * from the "successful" code path (e.g. a check-cast that "improves"
1436 * a type) to be visible to the exception handler.
1437 */
Ian Rogers776ac1f2012-04-13 23:36:36 -07001438 if ((opcode_flags & Instruction::kThrow) != 0 && CurrentInsnFlags()->IsInTry()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001439 saved_line_->CopyFromLine(work_line_.get());
jeffhaobdb76512011-09-07 11:43:16 -07001440 } else {
1441#ifndef NDEBUG
Ian Rogersd81871c2011-10-03 13:57:23 -07001442 saved_line_->FillWithGarbage();
jeffhaobdb76512011-09-07 11:43:16 -07001443#endif
1444 }
1445
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07001446
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001447 // We need to ensure the work line is consistent while performing validation. When we spot a
1448 // peephole pattern we compute a new line for either the fallthrough instruction or the
1449 // branch target.
Ian Rogers700a4022014-05-19 16:49:03 -07001450 std::unique_ptr<RegisterLine> branch_line;
1451 std::unique_ptr<RegisterLine> fallthrough_line;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001452
Sebastien Hertz5243e912013-05-21 10:55:07 +02001453 switch (inst->Opcode()) {
jeffhaobdb76512011-09-07 11:43:16 -07001454 case Instruction::NOP:
1455 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001456 * A "pure" NOP has no effect on anything. Data tables start with
jeffhaobdb76512011-09-07 11:43:16 -07001457 * a signature that looks like a NOP; if we see one of these in
1458 * the course of executing code then we have a problem.
1459 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001460 if (inst->VRegA_10x() != 0) {
jeffhaod5347e02012-03-22 17:25:05 -07001461 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "encountered data table in instruction stream";
jeffhaobdb76512011-09-07 11:43:16 -07001462 }
1463 break;
1464
1465 case Instruction::MOVE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001466 work_line_->CopyRegister1(inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategory1nr);
1467 break;
jeffhaobdb76512011-09-07 11:43:16 -07001468 case Instruction::MOVE_FROM16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001469 work_line_->CopyRegister1(inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategory1nr);
1470 break;
jeffhaobdb76512011-09-07 11:43:16 -07001471 case Instruction::MOVE_16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001472 work_line_->CopyRegister1(inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategory1nr);
jeffhaobdb76512011-09-07 11:43:16 -07001473 break;
1474 case Instruction::MOVE_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001475 work_line_->CopyRegister2(inst->VRegA_12x(), inst->VRegB_12x());
1476 break;
jeffhaobdb76512011-09-07 11:43:16 -07001477 case Instruction::MOVE_WIDE_FROM16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001478 work_line_->CopyRegister2(inst->VRegA_22x(), inst->VRegB_22x());
1479 break;
jeffhaobdb76512011-09-07 11:43:16 -07001480 case Instruction::MOVE_WIDE_16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001481 work_line_->CopyRegister2(inst->VRegA_32x(), inst->VRegB_32x());
jeffhaobdb76512011-09-07 11:43:16 -07001482 break;
1483 case Instruction::MOVE_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001484 work_line_->CopyRegister1(inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategoryRef);
1485 break;
jeffhaobdb76512011-09-07 11:43:16 -07001486 case Instruction::MOVE_OBJECT_FROM16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001487 work_line_->CopyRegister1(inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategoryRef);
1488 break;
jeffhaobdb76512011-09-07 11:43:16 -07001489 case Instruction::MOVE_OBJECT_16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001490 work_line_->CopyRegister1(inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategoryRef);
jeffhaobdb76512011-09-07 11:43:16 -07001491 break;
1492
1493 /*
1494 * The move-result instructions copy data out of a "pseudo-register"
jeffhaod1f0fde2011-09-08 17:25:33 -07001495 * with the results from the last method invocation. In practice we
jeffhaobdb76512011-09-07 11:43:16 -07001496 * might want to hold the result in an actual CPU register, so the
1497 * Dalvik spec requires that these only appear immediately after an
1498 * invoke or filled-new-array.
1499 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001500 * These calls invalidate the "result" register. (This is now
jeffhaobdb76512011-09-07 11:43:16 -07001501 * redundant with the reset done below, but it can make the debug info
1502 * easier to read in some cases.)
1503 */
1504 case Instruction::MOVE_RESULT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001505 work_line_->CopyResultRegister1(inst->VRegA_11x(), false);
jeffhaobdb76512011-09-07 11:43:16 -07001506 break;
1507 case Instruction::MOVE_RESULT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001508 work_line_->CopyResultRegister2(inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07001509 break;
1510 case Instruction::MOVE_RESULT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001511 work_line_->CopyResultRegister1(inst->VRegA_11x(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001512 break;
1513
Ian Rogersd81871c2011-10-03 13:57:23 -07001514 case Instruction::MOVE_EXCEPTION: {
jeffhaobdb76512011-09-07 11:43:16 -07001515 /*
jeffhao60f83e32012-02-13 17:16:30 -08001516 * This statement can only appear as the first instruction in an exception handler. We verify
1517 * that as part of extracting the exception type from the catch block list.
jeffhaobdb76512011-09-07 11:43:16 -07001518 */
Ian Rogers28ad40d2011-10-27 15:19:26 -07001519 const RegType& res_type = GetCaughtExceptionType();
Sebastien Hertz5243e912013-05-21 10:55:07 +02001520 work_line_->SetRegisterType(inst->VRegA_11x(), res_type);
jeffhaobdb76512011-09-07 11:43:16 -07001521 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001522 }
jeffhaobdb76512011-09-07 11:43:16 -07001523 case Instruction::RETURN_VOID:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001524 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
1525 if (!GetMethodReturnType().IsConflict()) {
jeffhaod5347e02012-03-22 17:25:05 -07001526 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001527 }
jeffhaobdb76512011-09-07 11:43:16 -07001528 }
1529 break;
1530 case Instruction::RETURN:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001531 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
jeffhaobdb76512011-09-07 11:43:16 -07001532 /* check the method signature */
Ian Rogersd81871c2011-10-03 13:57:23 -07001533 const RegType& return_type = GetMethodReturnType();
1534 if (!return_type.IsCategory1Types()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001535 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected non-category 1 return type "
1536 << return_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001537 } else {
1538 // Compilers may generate synthetic functions that write byte values into boolean fields.
1539 // Also, it may use integer values for boolean, byte, short, and character return types.
Sebastien Hertz5243e912013-05-21 10:55:07 +02001540 const uint32_t vregA = inst->VRegA_11x();
1541 const RegType& src_type = work_line_->GetRegisterType(vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07001542 bool use_src = ((return_type.IsBoolean() && src_type.IsByte()) ||
1543 ((return_type.IsBoolean() || return_type.IsByte() ||
1544 return_type.IsShort() || return_type.IsChar()) &&
1545 src_type.IsInteger()));
1546 /* check the register contents */
Ian Rogersad0b3a32012-04-16 14:50:24 -07001547 bool success =
Sebastien Hertz5243e912013-05-21 10:55:07 +02001548 work_line_->VerifyRegisterType(vregA, use_src ? src_type : return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001549 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001550 AppendToLastFailMessage(StringPrintf(" return-1nr on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07001551 }
jeffhaobdb76512011-09-07 11:43:16 -07001552 }
1553 }
1554 break;
1555 case Instruction::RETURN_WIDE:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001556 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
jeffhaobdb76512011-09-07 11:43:16 -07001557 /* check the method signature */
Ian Rogersd81871c2011-10-03 13:57:23 -07001558 const RegType& return_type = GetMethodReturnType();
1559 if (!return_type.IsCategory2Types()) {
jeffhaod5347e02012-03-22 17:25:05 -07001560 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-wide not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001561 } else {
1562 /* check the register contents */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001563 const uint32_t vregA = inst->VRegA_11x();
1564 bool success = work_line_->VerifyRegisterType(vregA, return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001565 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001566 AppendToLastFailMessage(StringPrintf(" return-wide on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07001567 }
jeffhaobdb76512011-09-07 11:43:16 -07001568 }
1569 }
1570 break;
1571 case Instruction::RETURN_OBJECT:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001572 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001573 const RegType& return_type = GetMethodReturnType();
1574 if (!return_type.IsReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001575 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-object not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001576 } else {
1577 /* return_type is the *expected* return type, not register value */
1578 DCHECK(!return_type.IsZero());
1579 DCHECK(!return_type.IsUninitializedReference());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001580 const uint32_t vregA = inst->VRegA_11x();
1581 const RegType& reg_type = work_line_->GetRegisterType(vregA);
Ian Rogers9074b992011-10-26 17:41:55 -07001582 // Disallow returning uninitialized values and verify that the reference in vAA is an
1583 // instance of the "return_type"
1584 if (reg_type.IsUninitializedTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001585 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "returning uninitialized object '"
1586 << reg_type << "'";
Ian Rogers9074b992011-10-26 17:41:55 -07001587 } else if (!return_type.IsAssignableFrom(reg_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07001588 if (reg_type.IsUnresolvedTypes() || return_type.IsUnresolvedTypes()) {
1589 Fail(VERIFY_ERROR_NO_CLASS) << " can't resolve returned type '" << return_type
1590 << "' or '" << reg_type << "'";
1591 } else {
1592 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning '" << reg_type
1593 << "', but expected from declaration '" << return_type << "'";
1594 }
jeffhaobdb76512011-09-07 11:43:16 -07001595 }
1596 }
1597 }
1598 break;
1599
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001600 /* could be boolean, int, float, or a null reference */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001601 case Instruction::CONST_4: {
1602 int32_t val = static_cast<int32_t>(inst->VRegB_11n() << 28) >> 28;
Sebastien Hertz849600b2013-12-20 10:28:08 +01001603 work_line_->SetRegisterType(inst->VRegA_11n(),
Ian Rogers46960fe2014-05-23 10:43:43 -07001604 DetermineCat1Constant(val, need_precise_constants_));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001605 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02001606 }
1607 case Instruction::CONST_16: {
1608 int16_t val = static_cast<int16_t>(inst->VRegB_21s());
Sebastien Hertz849600b2013-12-20 10:28:08 +01001609 work_line_->SetRegisterType(inst->VRegA_21s(),
Ian Rogers46960fe2014-05-23 10:43:43 -07001610 DetermineCat1Constant(val, need_precise_constants_));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001611 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02001612 }
Sebastien Hertz849600b2013-12-20 10:28:08 +01001613 case Instruction::CONST: {
1614 int32_t val = inst->VRegB_31i();
Sebastien Hertz5243e912013-05-21 10:55:07 +02001615 work_line_->SetRegisterType(inst->VRegA_31i(),
Ian Rogers46960fe2014-05-23 10:43:43 -07001616 DetermineCat1Constant(val, need_precise_constants_));
jeffhaobdb76512011-09-07 11:43:16 -07001617 break;
Sebastien Hertz849600b2013-12-20 10:28:08 +01001618 }
1619 case Instruction::CONST_HIGH16: {
1620 int32_t val = static_cast<int32_t>(inst->VRegB_21h() << 16);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001621 work_line_->SetRegisterType(inst->VRegA_21h(),
Ian Rogers46960fe2014-05-23 10:43:43 -07001622 DetermineCat1Constant(val, need_precise_constants_));
jeffhaobdb76512011-09-07 11:43:16 -07001623 break;
Sebastien Hertz849600b2013-12-20 10:28:08 +01001624 }
jeffhaobdb76512011-09-07 11:43:16 -07001625 /* could be long or double; resolved upon use */
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001626 case Instruction::CONST_WIDE_16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001627 int64_t val = static_cast<int16_t>(inst->VRegB_21s());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001628 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1629 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001630 work_line_->SetRegisterTypeWide(inst->VRegA_21s(), lo, hi);
jeffhaobdb76512011-09-07 11:43:16 -07001631 break;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001632 }
1633 case Instruction::CONST_WIDE_32: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001634 int64_t val = static_cast<int32_t>(inst->VRegB_31i());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001635 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1636 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001637 work_line_->SetRegisterTypeWide(inst->VRegA_31i(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001638 break;
1639 }
1640 case Instruction::CONST_WIDE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001641 int64_t val = inst->VRegB_51l();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001642 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1643 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001644 work_line_->SetRegisterTypeWide(inst->VRegA_51l(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001645 break;
1646 }
1647 case Instruction::CONST_WIDE_HIGH16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001648 int64_t val = static_cast<uint64_t>(inst->VRegB_21h()) << 48;
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_21h(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001652 break;
1653 }
jeffhaobdb76512011-09-07 11:43:16 -07001654 case Instruction::CONST_STRING:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001655 work_line_->SetRegisterType(inst->VRegA_21c(), reg_types_.JavaLangString());
1656 break;
jeffhaobdb76512011-09-07 11:43:16 -07001657 case Instruction::CONST_STRING_JUMBO:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001658 work_line_->SetRegisterType(inst->VRegA_31c(), reg_types_.JavaLangString());
jeffhaobdb76512011-09-07 11:43:16 -07001659 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001660 case Instruction::CONST_CLASS: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001661 // Get type from instruction if unresolved then we need an access check
1662 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Sebastien Hertz5243e912013-05-21 10:55:07 +02001663 const RegType& res_type = ResolveClassAndCheckAccess(inst->VRegB_21c());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001664 // Register holds class, ie its type is class, on error it will hold Conflict.
Sebastien Hertz5243e912013-05-21 10:55:07 +02001665 work_line_->SetRegisterType(inst->VRegA_21c(),
Ian Rogersb4903572012-10-11 11:52:56 -07001666 res_type.IsConflict() ? res_type
1667 : reg_types_.JavaLangClass(true));
jeffhaobdb76512011-09-07 11:43:16 -07001668 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001669 }
jeffhaobdb76512011-09-07 11:43:16 -07001670 case Instruction::MONITOR_ENTER:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001671 work_line_->PushMonitor(inst->VRegA_11x(), work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07001672 break;
1673 case Instruction::MONITOR_EXIT:
1674 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001675 * monitor-exit instructions are odd. They can throw exceptions,
jeffhaobdb76512011-09-07 11:43:16 -07001676 * but when they do they act as if they succeeded and the PC is
jeffhaod1f0fde2011-09-08 17:25:33 -07001677 * pointing to the following instruction. (This behavior goes back
jeffhaobdb76512011-09-07 11:43:16 -07001678 * to the need to handle asynchronous exceptions, a now-deprecated
1679 * feature that Dalvik doesn't support.)
1680 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001681 * In practice we don't need to worry about this. The only
jeffhaobdb76512011-09-07 11:43:16 -07001682 * exceptions that can be thrown from monitor-exit are for a
jeffhaod1f0fde2011-09-08 17:25:33 -07001683 * null reference and -exit without a matching -enter. If the
jeffhaobdb76512011-09-07 11:43:16 -07001684 * structured locking checks are working, the former would have
1685 * failed on the -enter instruction, and the latter is impossible.
1686 *
1687 * This is fortunate, because issue 3221411 prevents us from
1688 * chasing the "can throw" path when monitor verification is
jeffhaod1f0fde2011-09-08 17:25:33 -07001689 * enabled. If we can fully verify the locking we can ignore
jeffhaobdb76512011-09-07 11:43:16 -07001690 * some catch blocks (which will show up as "dead" code when
1691 * we skip them here); if we can't, then the code path could be
1692 * "live" so we still need to check it.
1693 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001694 opcode_flags &= ~Instruction::kThrow;
Sebastien Hertz5243e912013-05-21 10:55:07 +02001695 work_line_->PopMonitor(inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07001696 break;
1697
Ian Rogers28ad40d2011-10-27 15:19:26 -07001698 case Instruction::CHECK_CAST:
Ian Rogersd81871c2011-10-03 13:57:23 -07001699 case Instruction::INSTANCE_OF: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001700 /*
1701 * If this instruction succeeds, we will "downcast" register vA to the type in vB. (This
1702 * could be a "upcast" -- not expected, so we don't try to address it.)
1703 *
1704 * If it fails, an exception is thrown, which we deal with later by ignoring the update to
Elliott Hughesadb8c672012-03-06 16:49:32 -08001705 * dec_insn.vA when branching to a handler.
Ian Rogers28ad40d2011-10-27 15:19:26 -07001706 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001707 const bool is_checkcast = (inst->Opcode() == Instruction::CHECK_CAST);
1708 const uint32_t type_idx = (is_checkcast) ? inst->VRegB_21c() : inst->VRegC_22c();
1709 const RegType& res_type = ResolveClassAndCheckAccess(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001710 if (res_type.IsConflict()) {
1711 DCHECK_NE(failures_.size(), 0U);
1712 if (!is_checkcast) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001713 work_line_->SetRegisterType(inst->VRegA_22c(), reg_types_.Boolean());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001714 }
1715 break; // bad class
Ian Rogers9f1ab122011-12-12 08:52:43 -08001716 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001717 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Sebastien Hertz5243e912013-05-21 10:55:07 +02001718 uint32_t orig_type_reg = (is_checkcast) ? inst->VRegA_21c() : inst->VRegB_22c();
1719 const RegType& orig_type = work_line_->GetRegisterType(orig_type_reg);
Ian Rogers28ad40d2011-10-27 15:19:26 -07001720 if (!res_type.IsNonZeroReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001721 if (is_checkcast) {
1722 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on unexpected class " << res_type;
1723 } else {
1724 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on unexpected class " << res_type;
1725 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001726 } else if (!orig_type.IsReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001727 if (is_checkcast) {
1728 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on non-reference in v" << orig_type_reg;
1729 } else {
1730 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on non-reference in v" << orig_type_reg;
1731 }
jeffhao2a8a90e2011-09-26 14:25:31 -07001732 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001733 if (is_checkcast) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001734 work_line_->SetRegisterType(inst->VRegA_21c(), res_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001735 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001736 work_line_->SetRegisterType(inst->VRegA_22c(), reg_types_.Boolean());
jeffhaobdb76512011-09-07 11:43:16 -07001737 }
jeffhaobdb76512011-09-07 11:43:16 -07001738 }
jeffhao2a8a90e2011-09-26 14:25:31 -07001739 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001740 }
1741 case Instruction::ARRAY_LENGTH: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001742 const RegType& res_type = work_line_->GetRegisterType(inst->VRegB_12x());
Ian Rogers28ad40d2011-10-27 15:19:26 -07001743 if (res_type.IsReferenceTypes()) {
Ian Rogers89310de2012-02-01 13:47:30 -08001744 if (!res_type.IsArrayTypes() && !res_type.IsZero()) { // ie not an array or null
jeffhaod5347e02012-03-22 17:25:05 -07001745 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001746 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001747 work_line_->SetRegisterType(inst->VRegA_12x(), reg_types_.Integer());
Ian Rogersd81871c2011-10-03 13:57:23 -07001748 }
1749 }
1750 break;
1751 }
1752 case Instruction::NEW_INSTANCE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001753 const RegType& res_type = ResolveClassAndCheckAccess(inst->VRegB_21c());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001754 if (res_type.IsConflict()) {
1755 DCHECK_NE(failures_.size(), 0U);
1756 break; // bad class
jeffhao8cd6dda2012-02-22 10:15:34 -08001757 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001758 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
1759 // can't create an instance of an interface or abstract class */
1760 if (!res_type.IsInstantiableTypes()) {
1761 Fail(VERIFY_ERROR_INSTANTIATION)
1762 << "new-instance on primitive, interface or abstract class" << res_type;
Ian Rogers08f753d2012-08-24 14:35:25 -07001763 // Soft failure so carry on to set register type.
Ian Rogersd81871c2011-10-03 13:57:23 -07001764 }
Ian Rogers08f753d2012-08-24 14:35:25 -07001765 const RegType& uninit_type = reg_types_.Uninitialized(res_type, work_insn_idx_);
1766 // Any registers holding previous allocations from this address that have not yet been
1767 // initialized must be marked invalid.
1768 work_line_->MarkUninitRefsAsInvalid(uninit_type);
1769 // add the new uninitialized reference to the register state
Sebastien Hertz5243e912013-05-21 10:55:07 +02001770 work_line_->SetRegisterType(inst->VRegA_21c(), uninit_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001771 break;
1772 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08001773 case Instruction::NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001774 VerifyNewArray(inst, false, false);
jeffhaobdb76512011-09-07 11:43:16 -07001775 break;
1776 case Instruction::FILLED_NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001777 VerifyNewArray(inst, true, false);
Ian Rogers0c4a5062012-02-03 15:18:59 -08001778 just_set_result = true; // Filled new array sets result register
jeffhaobdb76512011-09-07 11:43:16 -07001779 break;
Ian Rogers0c4a5062012-02-03 15:18:59 -08001780 case Instruction::FILLED_NEW_ARRAY_RANGE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001781 VerifyNewArray(inst, true, true);
Ian Rogers0c4a5062012-02-03 15:18:59 -08001782 just_set_result = true; // Filled new array range sets result register
1783 break;
jeffhaobdb76512011-09-07 11:43:16 -07001784 case Instruction::CMPL_FLOAT:
1785 case Instruction::CMPG_FLOAT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001786 if (!work_line_->VerifyRegisterType(inst->VRegB_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08001787 break;
1788 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02001789 if (!work_line_->VerifyRegisterType(inst->VRegC_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08001790 break;
1791 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02001792 work_line_->SetRegisterType(inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001793 break;
1794 case Instruction::CMPL_DOUBLE:
1795 case Instruction::CMPG_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001796 if (!work_line_->VerifyRegisterTypeWide(inst->VRegB_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001797 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001798 break;
1799 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02001800 if (!work_line_->VerifyRegisterTypeWide(inst->VRegC_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001801 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001802 break;
1803 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02001804 work_line_->SetRegisterType(inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001805 break;
1806 case Instruction::CMP_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001807 if (!work_line_->VerifyRegisterTypeWide(inst->VRegB_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001808 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001809 break;
1810 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02001811 if (!work_line_->VerifyRegisterTypeWide(inst->VRegC_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001812 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001813 break;
1814 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02001815 work_line_->SetRegisterType(inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001816 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001817 case Instruction::THROW: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001818 const RegType& res_type = work_line_->GetRegisterType(inst->VRegA_11x());
Ian Rogersb4903572012-10-11 11:52:56 -07001819 if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(res_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07001820 Fail(res_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS : VERIFY_ERROR_BAD_CLASS_SOFT)
1821 << "thrown class " << res_type << " not instanceof Throwable";
jeffhaobdb76512011-09-07 11:43:16 -07001822 }
1823 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001824 }
jeffhaobdb76512011-09-07 11:43:16 -07001825 case Instruction::GOTO:
1826 case Instruction::GOTO_16:
1827 case Instruction::GOTO_32:
1828 /* no effect on or use of registers */
1829 break;
1830
1831 case Instruction::PACKED_SWITCH:
1832 case Instruction::SPARSE_SWITCH:
1833 /* verify that vAA is an integer, or can be converted to one */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001834 work_line_->VerifyRegisterType(inst->VRegA_31t(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001835 break;
1836
Ian Rogersd81871c2011-10-03 13:57:23 -07001837 case Instruction::FILL_ARRAY_DATA: {
1838 /* Similar to the verification done for APUT */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001839 const RegType& array_type = work_line_->GetRegisterType(inst->VRegA_31t());
Ian Rogers89310de2012-02-01 13:47:30 -08001840 /* array_type can be null if the reg type is Zero */
1841 if (!array_type.IsZero()) {
jeffhao457cc512012-02-02 16:55:13 -08001842 if (!array_type.IsArrayTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001843 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with array type "
1844 << array_type;
Ian Rogers89310de2012-02-01 13:47:30 -08001845 } else {
Mathieu Chartier590fee92013-09-13 13:46:47 -07001846 const RegType& component_type = reg_types_.GetComponentType(array_type,
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001847 class_loader_->Get());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001848 DCHECK(!component_type.IsConflict());
jeffhao457cc512012-02-02 16:55:13 -08001849 if (component_type.IsNonZeroReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001850 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with component type "
1851 << component_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001852 } else {
jeffhao457cc512012-02-02 16:55:13 -08001853 // Now verify if the element width in the table matches the element width declared in
1854 // the array
1855 const uint16_t* array_data = insns + (insns[1] | (((int32_t) insns[2]) << 16));
1856 if (array_data[0] != Instruction::kArrayDataSignature) {
jeffhaod5347e02012-03-22 17:25:05 -07001857 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid magic for array-data";
jeffhao457cc512012-02-02 16:55:13 -08001858 } else {
1859 size_t elem_width = Primitive::ComponentSize(component_type.GetPrimitiveType());
1860 // Since we don't compress the data in Dex, expect to see equal width of data stored
1861 // in the table and expected from the array class.
1862 if (array_data[1] != elem_width) {
jeffhaod5347e02012-03-22 17:25:05 -07001863 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-data size mismatch (" << array_data[1]
1864 << " vs " << elem_width << ")";
jeffhao457cc512012-02-02 16:55:13 -08001865 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001866 }
1867 }
jeffhaobdb76512011-09-07 11:43:16 -07001868 }
1869 }
1870 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001871 }
jeffhaobdb76512011-09-07 11:43:16 -07001872 case Instruction::IF_EQ:
Ian Rogersd81871c2011-10-03 13:57:23 -07001873 case Instruction::IF_NE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001874 const RegType& reg_type1 = work_line_->GetRegisterType(inst->VRegA_22t());
1875 const RegType& reg_type2 = work_line_->GetRegisterType(inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07001876 bool mismatch = false;
1877 if (reg_type1.IsZero()) { // zero then integral or reference expected
1878 mismatch = !reg_type2.IsReferenceTypes() && !reg_type2.IsIntegralTypes();
1879 } else if (reg_type1.IsReferenceTypes()) { // both references?
1880 mismatch = !reg_type2.IsReferenceTypes();
1881 } else { // both integral?
1882 mismatch = !reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes();
1883 }
1884 if (mismatch) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001885 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to if-eq/if-ne (" << reg_type1 << ","
1886 << reg_type2 << ") must both be references or integral";
jeffhaobdb76512011-09-07 11:43:16 -07001887 }
1888 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001889 }
jeffhaobdb76512011-09-07 11:43:16 -07001890 case Instruction::IF_LT:
1891 case Instruction::IF_GE:
1892 case Instruction::IF_GT:
Ian Rogersd81871c2011-10-03 13:57:23 -07001893 case Instruction::IF_LE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001894 const RegType& reg_type1 = work_line_->GetRegisterType(inst->VRegA_22t());
1895 const RegType& reg_type2 = work_line_->GetRegisterType(inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07001896 if (!reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001897 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to 'if' (" << reg_type1 << ","
1898 << reg_type2 << ") must be integral";
jeffhaobdb76512011-09-07 11:43:16 -07001899 }
1900 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001901 }
jeffhaobdb76512011-09-07 11:43:16 -07001902 case Instruction::IF_EQZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07001903 case Instruction::IF_NEZ: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001904 const RegType& reg_type = work_line_->GetRegisterType(inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07001905 if (!reg_type.IsReferenceTypes() && !reg_type.IsIntegralTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001906 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
1907 << " unexpected as arg to if-eqz/if-nez";
Ian Rogersd81871c2011-10-03 13:57:23 -07001908 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001909
1910 // Find previous instruction - its existence is a precondition to peephole optimization.
Ian Rogers9b360392013-06-06 14:45:07 -07001911 uint32_t instance_of_idx = 0;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001912 if (0 != work_insn_idx_) {
Ian Rogers9b360392013-06-06 14:45:07 -07001913 instance_of_idx = work_insn_idx_ - 1;
Brian Carlstromdf629502013-07-17 22:39:56 -07001914 while (0 != instance_of_idx && !insn_flags_[instance_of_idx].IsOpcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07001915 instance_of_idx--;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001916 }
Ian Rogers9b360392013-06-06 14:45:07 -07001917 CHECK(insn_flags_[instance_of_idx].IsOpcode());
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001918 } else {
1919 break;
1920 }
1921
Ian Rogers9b360392013-06-06 14:45:07 -07001922 const Instruction* instance_of_inst = Instruction::At(code_item_->insns_ + instance_of_idx);
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001923
1924 /* Check for peep-hole pattern of:
1925 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07001926 * instance-of vX, vY, T;
1927 * ifXXX vX, label ;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001928 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07001929 * label:
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001930 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07001931 * and sharpen the type of vY to be type T.
1932 * Note, this pattern can't be if:
1933 * - if there are other branches to this branch,
1934 * - when vX == vY.
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001935 */
Ian Rogersfae370a2013-06-05 08:33:27 -07001936 if (!CurrentInsnFlags()->IsBranchTarget() &&
Ian Rogers9b360392013-06-06 14:45:07 -07001937 (Instruction::INSTANCE_OF == instance_of_inst->Opcode()) &&
1938 (inst->VRegA_21t() == instance_of_inst->VRegA_22c()) &&
1939 (instance_of_inst->VRegA_22c() != instance_of_inst->VRegB_22c())) {
Ian Rogersfae370a2013-06-05 08:33:27 -07001940 // Check that the we are not attempting conversion to interface types,
1941 // which is not done because of the multiple inheritance implications.
Jeff Haoc642ec82013-09-04 16:11:55 -07001942 // Also don't change the type if it would result in an upcast.
1943 const RegType& orig_type = work_line_->GetRegisterType(instance_of_inst->VRegB_22c());
Ian Rogers9b360392013-06-06 14:45:07 -07001944 const RegType& cast_type = ResolveClassAndCheckAccess(instance_of_inst->VRegC_22c());
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001945
Jeff Haoa3faaf42013-09-03 19:07:00 -07001946 if (!cast_type.IsUnresolvedTypes() && !orig_type.IsUnresolvedTypes() &&
1947 !cast_type.GetClass()->IsInterface() && !cast_type.IsAssignableFrom(orig_type)) {
Ian Rogersd0fbd852013-09-24 18:17:04 -07001948 RegisterLine* update_line = RegisterLine::Create(code_item_->registers_size_, this);
Ian Rogersfae370a2013-06-05 08:33:27 -07001949 if (inst->Opcode() == Instruction::IF_EQZ) {
Ian Rogers9b360392013-06-06 14:45:07 -07001950 fallthrough_line.reset(update_line);
Ian Rogersfae370a2013-06-05 08:33:27 -07001951 } else {
Ian Rogers9b360392013-06-06 14:45:07 -07001952 branch_line.reset(update_line);
1953 }
1954 update_line->CopyFromLine(work_line_.get());
1955 update_line->SetRegisterType(instance_of_inst->VRegB_22c(), cast_type);
1956 if (!insn_flags_[instance_of_idx].IsBranchTarget() && 0 != instance_of_idx) {
1957 // See if instance-of was preceded by a move-object operation, common due to the small
1958 // register encoding space of instance-of, and propagate type information to the source
1959 // of the move-object.
1960 uint32_t move_idx = instance_of_idx - 1;
Brian Carlstromdf629502013-07-17 22:39:56 -07001961 while (0 != move_idx && !insn_flags_[move_idx].IsOpcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07001962 move_idx--;
1963 }
1964 CHECK(insn_flags_[move_idx].IsOpcode());
1965 const Instruction* move_inst = Instruction::At(code_item_->insns_ + move_idx);
1966 switch (move_inst->Opcode()) {
1967 case Instruction::MOVE_OBJECT:
1968 if (move_inst->VRegA_12x() == instance_of_inst->VRegB_22c()) {
1969 update_line->SetRegisterType(move_inst->VRegB_12x(), cast_type);
1970 }
1971 break;
1972 case Instruction::MOVE_OBJECT_FROM16:
1973 if (move_inst->VRegA_22x() == instance_of_inst->VRegB_22c()) {
1974 update_line->SetRegisterType(move_inst->VRegB_22x(), cast_type);
1975 }
1976 break;
1977 case Instruction::MOVE_OBJECT_16:
1978 if (move_inst->VRegA_32x() == instance_of_inst->VRegB_22c()) {
1979 update_line->SetRegisterType(move_inst->VRegB_32x(), cast_type);
1980 }
1981 break;
1982 default:
1983 break;
1984 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001985 }
1986 }
1987 }
1988
jeffhaobdb76512011-09-07 11:43:16 -07001989 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001990 }
jeffhaobdb76512011-09-07 11:43:16 -07001991 case Instruction::IF_LTZ:
1992 case Instruction::IF_GEZ:
1993 case Instruction::IF_GTZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07001994 case Instruction::IF_LEZ: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001995 const RegType& reg_type = work_line_->GetRegisterType(inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07001996 if (!reg_type.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001997 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
1998 << " unexpected as arg to if-ltz/if-gez/if-gtz/if-lez";
Ian Rogersd81871c2011-10-03 13:57:23 -07001999 }
jeffhaobdb76512011-09-07 11:43:16 -07002000 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002001 }
jeffhaobdb76512011-09-07 11:43:16 -07002002 case Instruction::AGET_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002003 VerifyAGet(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002004 break;
jeffhaobdb76512011-09-07 11:43:16 -07002005 case Instruction::AGET_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002006 VerifyAGet(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002007 break;
jeffhaobdb76512011-09-07 11:43:16 -07002008 case Instruction::AGET_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002009 VerifyAGet(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002010 break;
jeffhaobdb76512011-09-07 11:43:16 -07002011 case Instruction::AGET_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002012 VerifyAGet(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002013 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002014 case Instruction::AGET:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002015 VerifyAGet(inst, reg_types_.Integer(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002016 break;
jeffhaobdb76512011-09-07 11:43:16 -07002017 case Instruction::AGET_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002018 VerifyAGet(inst, reg_types_.LongLo(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002019 break;
2020 case Instruction::AGET_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002021 VerifyAGet(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07002022 break;
2023
Ian Rogersd81871c2011-10-03 13:57:23 -07002024 case Instruction::APUT_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002025 VerifyAPut(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002026 break;
2027 case Instruction::APUT_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002028 VerifyAPut(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002029 break;
2030 case Instruction::APUT_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002031 VerifyAPut(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002032 break;
2033 case Instruction::APUT_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002034 VerifyAPut(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002035 break;
2036 case Instruction::APUT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002037 VerifyAPut(inst, reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002038 break;
2039 case Instruction::APUT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002040 VerifyAPut(inst, reg_types_.LongLo(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002041 break;
2042 case Instruction::APUT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002043 VerifyAPut(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07002044 break;
2045
jeffhaobdb76512011-09-07 11:43:16 -07002046 case Instruction::IGET_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002047 VerifyISGet(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002048 break;
jeffhaobdb76512011-09-07 11:43:16 -07002049 case Instruction::IGET_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002050 VerifyISGet(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002051 break;
jeffhaobdb76512011-09-07 11:43:16 -07002052 case Instruction::IGET_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002053 VerifyISGet(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002054 break;
jeffhaobdb76512011-09-07 11:43:16 -07002055 case Instruction::IGET_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002056 VerifyISGet(inst, reg_types_.Short(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002057 break;
2058 case Instruction::IGET:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002059 VerifyISGet(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002060 break;
2061 case Instruction::IGET_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002062 VerifyISGet(inst, reg_types_.LongLo(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002063 break;
2064 case Instruction::IGET_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002065 VerifyISGet(inst, reg_types_.JavaLangObject(false), false, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002066 break;
jeffhaobdb76512011-09-07 11:43:16 -07002067
Ian Rogersd81871c2011-10-03 13:57:23 -07002068 case Instruction::IPUT_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002069 VerifyISPut(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002070 break;
2071 case Instruction::IPUT_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002072 VerifyISPut(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002073 break;
2074 case Instruction::IPUT_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002075 VerifyISPut(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002076 break;
2077 case Instruction::IPUT_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002078 VerifyISPut(inst, reg_types_.Short(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002079 break;
2080 case Instruction::IPUT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002081 VerifyISPut(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002082 break;
2083 case Instruction::IPUT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002084 VerifyISPut(inst, reg_types_.LongLo(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002085 break;
jeffhaobdb76512011-09-07 11:43:16 -07002086 case Instruction::IPUT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002087 VerifyISPut(inst, reg_types_.JavaLangObject(false), false, false);
jeffhaobdb76512011-09-07 11:43:16 -07002088 break;
2089
jeffhaobdb76512011-09-07 11:43:16 -07002090 case Instruction::SGET_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002091 VerifyISGet(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002092 break;
jeffhaobdb76512011-09-07 11:43:16 -07002093 case Instruction::SGET_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002094 VerifyISGet(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002095 break;
jeffhaobdb76512011-09-07 11:43:16 -07002096 case Instruction::SGET_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002097 VerifyISGet(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002098 break;
jeffhaobdb76512011-09-07 11:43:16 -07002099 case Instruction::SGET_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002100 VerifyISGet(inst, reg_types_.Short(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002101 break;
2102 case Instruction::SGET:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002103 VerifyISGet(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002104 break;
2105 case Instruction::SGET_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002106 VerifyISGet(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002107 break;
2108 case Instruction::SGET_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002109 VerifyISGet(inst, reg_types_.JavaLangObject(false), false, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002110 break;
2111
2112 case Instruction::SPUT_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002113 VerifyISPut(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002114 break;
2115 case Instruction::SPUT_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002116 VerifyISPut(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002117 break;
2118 case Instruction::SPUT_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002119 VerifyISPut(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002120 break;
2121 case Instruction::SPUT_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002122 VerifyISPut(inst, reg_types_.Short(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002123 break;
2124 case Instruction::SPUT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002125 VerifyISPut(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002126 break;
2127 case Instruction::SPUT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002128 VerifyISPut(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002129 break;
2130 case Instruction::SPUT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002131 VerifyISPut(inst, reg_types_.JavaLangObject(false), false, true);
jeffhaobdb76512011-09-07 11:43:16 -07002132 break;
2133
2134 case Instruction::INVOKE_VIRTUAL:
2135 case Instruction::INVOKE_VIRTUAL_RANGE:
2136 case Instruction::INVOKE_SUPER:
Ian Rogersd81871c2011-10-03 13:57:23 -07002137 case Instruction::INVOKE_SUPER_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002138 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE ||
2139 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002140 bool is_super = (inst->Opcode() == Instruction::INVOKE_SUPER ||
2141 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
Andreas Gampeacc4d2f2014-06-12 19:35:05 -07002142 mirror::ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_VIRTUAL, is_range,
2143 is_super);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002144 const RegType* return_type = nullptr;
2145 if (called_method != nullptr) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002146 Thread* self = Thread::Current();
2147 StackHandleScope<1> hs(self);
2148 Handle<mirror::ArtMethod> h_called_method(hs.NewHandle(called_method));
2149 MethodHelper mh(h_called_method);
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08002150 mirror::Class* return_type_class = mh.GetReturnType(can_load_classes_);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002151 if (return_type_class != nullptr) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002152 return_type = &reg_types_.FromClass(h_called_method->GetReturnTypeDescriptor(),
2153 return_type_class,
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002154 return_type_class->CannotBeAssignedFromOtherTypes());
2155 } else {
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08002156 DCHECK(!can_load_classes_ || self->IsExceptionPending());
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002157 self->ClearException();
2158 }
2159 }
2160 if (return_type == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002161 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002162 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2163 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002164 const char* descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07002165 return_type = &reg_types_.FromDescriptor(class_loader_->Get(), descriptor, false);
jeffhaobdb76512011-09-07 11:43:16 -07002166 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002167 if (!return_type->IsLowHalf()) {
2168 work_line_->SetResultRegisterType(*return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002169 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002170 work_line_->SetResultRegisterTypeWide(*return_type, return_type->HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002171 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002172 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002173 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002174 }
jeffhaobdb76512011-09-07 11:43:16 -07002175 case Instruction::INVOKE_DIRECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002176 case Instruction::INVOKE_DIRECT_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002177 bool is_range = (inst->Opcode() == Instruction::INVOKE_DIRECT_RANGE);
Brian Carlstromea46f952013-07-30 01:26:50 -07002178 mirror::ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_DIRECT,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002179 is_range, false);
Ian Rogers46685432012-06-03 22:26:43 -07002180 const char* return_type_descriptor;
2181 bool is_constructor;
2182 if (called_method == NULL) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002183 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogers46685432012-06-03 22:26:43 -07002184 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
Ian Rogersdfb325e2013-10-30 01:00:44 -07002185 is_constructor = strcmp("<init>", dex_file_->StringDataByIdx(method_id.name_idx_)) == 0;
Ian Rogers46685432012-06-03 22:26:43 -07002186 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
2187 return_type_descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2188 } else {
2189 is_constructor = called_method->IsConstructor();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002190 return_type_descriptor = called_method->GetReturnTypeDescriptor();
Ian Rogers46685432012-06-03 22:26:43 -07002191 }
2192 if (is_constructor) {
jeffhaobdb76512011-09-07 11:43:16 -07002193 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002194 * Some additional checks when calling a constructor. We know from the invocation arg check
2195 * that the "this" argument is an instance of called_method->klass. Now we further restrict
2196 * that to require that called_method->klass is the same as this->klass or this->super,
2197 * allowing the latter only if the "this" argument is the same as the "this" argument to
2198 * this method (which implies that we're in a constructor ourselves).
jeffhaobdb76512011-09-07 11:43:16 -07002199 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002200 const RegType& this_type = work_line_->GetInvocationThis(inst, is_range);
jeffhaob57e9522012-04-26 18:08:21 -07002201 if (this_type.IsConflict()) // failure.
2202 break;
jeffhaobdb76512011-09-07 11:43:16 -07002203
jeffhaob57e9522012-04-26 18:08:21 -07002204 /* no null refs allowed (?) */
2205 if (this_type.IsZero()) {
2206 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unable to initialize null ref";
2207 break;
jeffhao2a8a90e2011-09-26 14:25:31 -07002208 }
jeffhaob57e9522012-04-26 18:08:21 -07002209
2210 /* must be in same class or in superclass */
Ian Rogers46685432012-06-03 22:26:43 -07002211 // const RegType& this_super_klass = this_type.GetSuperClass(&reg_types_);
2212 // TODO: re-enable constructor type verification
2213 // if (this_super_klass.IsConflict()) {
jeffhaob57e9522012-04-26 18:08:21 -07002214 // Unknown super class, fail so we re-check at runtime.
Ian Rogers46685432012-06-03 22:26:43 -07002215 // Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "super class unknown for '" << this_type << "'";
2216 // break;
2217 // }
jeffhaob57e9522012-04-26 18:08:21 -07002218
2219 /* arg must be an uninitialized reference */
2220 if (!this_type.IsUninitializedTypes()) {
2221 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Expected initialization on uninitialized reference "
2222 << this_type;
2223 break;
2224 }
2225
2226 /*
2227 * Replace the uninitialized reference with an initialized one. We need to do this for all
2228 * registers that have the same object instance in them, not just the "this" register.
2229 */
2230 work_line_->MarkRefsAsInitialized(this_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002231 }
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07002232 const RegType& return_type = reg_types_.FromDescriptor(class_loader_->Get(),
Mathieu Chartier590fee92013-09-13 13:46:47 -07002233 return_type_descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002234 if (!return_type.IsLowHalf()) {
2235 work_line_->SetResultRegisterType(return_type);
2236 } else {
2237 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2238 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002239 just_set_result = true;
2240 break;
2241 }
2242 case Instruction::INVOKE_STATIC:
2243 case Instruction::INVOKE_STATIC_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002244 bool is_range = (inst->Opcode() == Instruction::INVOKE_STATIC_RANGE);
Brian Carlstromea46f952013-07-30 01:26:50 -07002245 mirror::ArtMethod* called_method = VerifyInvocationArgs(inst,
Brian Carlstrom93c33962013-07-26 10:37:43 -07002246 METHOD_STATIC,
2247 is_range,
2248 false);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002249 const char* descriptor;
2250 if (called_method == NULL) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002251 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002252 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2253 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Mathieu Chartier590fee92013-09-13 13:46:47 -07002254 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002255 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002256 descriptor = called_method->GetReturnTypeDescriptor();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002257 }
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002258 const RegType& return_type = reg_types_.FromDescriptor(class_loader_->Get(), descriptor,
2259 false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002260 if (!return_type.IsLowHalf()) {
2261 work_line_->SetResultRegisterType(return_type);
2262 } else {
2263 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2264 }
jeffhaobdb76512011-09-07 11:43:16 -07002265 just_set_result = true;
2266 }
2267 break;
jeffhaobdb76512011-09-07 11:43:16 -07002268 case Instruction::INVOKE_INTERFACE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002269 case Instruction::INVOKE_INTERFACE_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002270 bool is_range = (inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE);
Brian Carlstromea46f952013-07-30 01:26:50 -07002271 mirror::ArtMethod* abs_method = VerifyInvocationArgs(inst,
Brian Carlstrom93c33962013-07-26 10:37:43 -07002272 METHOD_INTERFACE,
2273 is_range,
2274 false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002275 if (abs_method != NULL) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002276 mirror::Class* called_interface = abs_method->GetDeclaringClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002277 if (!called_interface->IsInterface() && !called_interface->IsObjectClass()) {
2278 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected interface class in invoke-interface '"
2279 << PrettyMethod(abs_method) << "'";
2280 break;
Ian Rogers28ad40d2011-10-27 15:19:26 -07002281 }
Ian Rogers0d604842012-04-16 14:50:24 -07002282 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002283 /* Get the type of the "this" arg, which should either be a sub-interface of called
2284 * interface or Object (see comments in RegType::JoinClass).
2285 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002286 const RegType& this_type = work_line_->GetInvocationThis(inst, is_range);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002287 if (this_type.IsZero()) {
2288 /* null pointer always passes (and always fails at runtime) */
2289 } else {
2290 if (this_type.IsUninitializedTypes()) {
2291 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface call on uninitialized object "
2292 << this_type;
2293 break;
2294 }
2295 // In the past we have tried to assert that "called_interface" is assignable
2296 // from "this_type.GetClass()", however, as we do an imprecise Join
2297 // (RegType::JoinClass) we don't have full information on what interfaces are
2298 // implemented by "this_type". For example, two classes may implement the same
2299 // interfaces and have a common parent that doesn't implement the interface. The
2300 // join will set "this_type" to the parent class and a test that this implements
2301 // the interface will incorrectly fail.
2302 }
2303 /*
2304 * We don't have an object instance, so we can't find the concrete method. However, all of
2305 * the type information is in the abstract method, so we're good.
2306 */
2307 const char* descriptor;
2308 if (abs_method == NULL) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002309 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002310 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2311 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
2312 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2313 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002314 descriptor = abs_method->GetReturnTypeDescriptor();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002315 }
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07002316 const RegType& return_type = reg_types_.FromDescriptor(class_loader_->Get(), descriptor,
Mathieu Chartier590fee92013-09-13 13:46:47 -07002317 false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002318 if (!return_type.IsLowHalf()) {
2319 work_line_->SetResultRegisterType(return_type);
2320 } else {
2321 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2322 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002323 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002324 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002325 }
jeffhaobdb76512011-09-07 11:43:16 -07002326 case Instruction::NEG_INT:
2327 case Instruction::NOT_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002328 work_line_->CheckUnaryOp(inst, reg_types_.Integer(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002329 break;
2330 case Instruction::NEG_LONG:
2331 case Instruction::NOT_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002332 work_line_->CheckUnaryOpWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002333 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002334 break;
2335 case Instruction::NEG_FLOAT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002336 work_line_->CheckUnaryOp(inst, reg_types_.Float(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002337 break;
2338 case Instruction::NEG_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002339 work_line_->CheckUnaryOpWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002340 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002341 break;
2342 case Instruction::INT_TO_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002343 work_line_->CheckUnaryOpToWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002344 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002345 break;
2346 case Instruction::INT_TO_FLOAT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002347 work_line_->CheckUnaryOp(inst, reg_types_.Float(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002348 break;
2349 case Instruction::INT_TO_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002350 work_line_->CheckUnaryOpToWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002351 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002352 break;
2353 case Instruction::LONG_TO_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002354 work_line_->CheckUnaryOpFromWide(inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002355 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002356 break;
2357 case Instruction::LONG_TO_FLOAT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002358 work_line_->CheckUnaryOpFromWide(inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002359 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002360 break;
2361 case Instruction::LONG_TO_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002362 work_line_->CheckUnaryOpWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002363 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002364 break;
2365 case Instruction::FLOAT_TO_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002366 work_line_->CheckUnaryOp(inst, reg_types_.Integer(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002367 break;
2368 case Instruction::FLOAT_TO_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002369 work_line_->CheckUnaryOpToWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002370 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002371 break;
2372 case Instruction::FLOAT_TO_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002373 work_line_->CheckUnaryOpToWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002374 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002375 break;
2376 case Instruction::DOUBLE_TO_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002377 work_line_->CheckUnaryOpFromWide(inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002378 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002379 break;
2380 case Instruction::DOUBLE_TO_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002381 work_line_->CheckUnaryOpWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002382 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002383 break;
2384 case Instruction::DOUBLE_TO_FLOAT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002385 work_line_->CheckUnaryOpFromWide(inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002386 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002387 break;
2388 case Instruction::INT_TO_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002389 work_line_->CheckUnaryOp(inst, reg_types_.Byte(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002390 break;
2391 case Instruction::INT_TO_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002392 work_line_->CheckUnaryOp(inst, reg_types_.Char(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002393 break;
2394 case Instruction::INT_TO_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002395 work_line_->CheckUnaryOp(inst, reg_types_.Short(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002396 break;
2397
2398 case Instruction::ADD_INT:
2399 case Instruction::SUB_INT:
2400 case Instruction::MUL_INT:
2401 case Instruction::REM_INT:
2402 case Instruction::DIV_INT:
2403 case Instruction::SHL_INT:
2404 case Instruction::SHR_INT:
2405 case Instruction::USHR_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002406 work_line_->CheckBinaryOp(inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002407 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002408 break;
2409 case Instruction::AND_INT:
2410 case Instruction::OR_INT:
2411 case Instruction::XOR_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002412 work_line_->CheckBinaryOp(inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002413 reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002414 break;
2415 case Instruction::ADD_LONG:
2416 case Instruction::SUB_LONG:
2417 case Instruction::MUL_LONG:
2418 case Instruction::DIV_LONG:
2419 case Instruction::REM_LONG:
2420 case Instruction::AND_LONG:
2421 case Instruction::OR_LONG:
2422 case Instruction::XOR_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002423 work_line_->CheckBinaryOpWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002424 reg_types_.LongLo(), reg_types_.LongHi(),
2425 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002426 break;
2427 case Instruction::SHL_LONG:
2428 case Instruction::SHR_LONG:
2429 case Instruction::USHR_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002430 /* shift distance is Int, making these different from other binary operations */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002431 work_line_->CheckBinaryOpWideShift(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002432 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002433 break;
2434 case Instruction::ADD_FLOAT:
2435 case Instruction::SUB_FLOAT:
2436 case Instruction::MUL_FLOAT:
2437 case Instruction::DIV_FLOAT:
2438 case Instruction::REM_FLOAT:
Brian Carlstrom93c33962013-07-26 10:37:43 -07002439 work_line_->CheckBinaryOp(inst,
2440 reg_types_.Float(),
2441 reg_types_.Float(),
2442 reg_types_.Float(),
2443 false);
jeffhaobdb76512011-09-07 11:43:16 -07002444 break;
2445 case Instruction::ADD_DOUBLE:
2446 case Instruction::SUB_DOUBLE:
2447 case Instruction::MUL_DOUBLE:
2448 case Instruction::DIV_DOUBLE:
2449 case Instruction::REM_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002450 work_line_->CheckBinaryOpWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002451 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2452 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002453 break;
2454 case Instruction::ADD_INT_2ADDR:
2455 case Instruction::SUB_INT_2ADDR:
2456 case Instruction::MUL_INT_2ADDR:
2457 case Instruction::REM_INT_2ADDR:
2458 case Instruction::SHL_INT_2ADDR:
2459 case Instruction::SHR_INT_2ADDR:
2460 case Instruction::USHR_INT_2ADDR:
Brian Carlstrom93c33962013-07-26 10:37:43 -07002461 work_line_->CheckBinaryOp2addr(inst,
2462 reg_types_.Integer(),
2463 reg_types_.Integer(),
2464 reg_types_.Integer(),
2465 false);
jeffhaobdb76512011-09-07 11:43:16 -07002466 break;
2467 case Instruction::AND_INT_2ADDR:
2468 case Instruction::OR_INT_2ADDR:
2469 case Instruction::XOR_INT_2ADDR:
Brian Carlstrom93c33962013-07-26 10:37:43 -07002470 work_line_->CheckBinaryOp2addr(inst,
2471 reg_types_.Integer(),
2472 reg_types_.Integer(),
2473 reg_types_.Integer(),
2474 true);
jeffhaobdb76512011-09-07 11:43:16 -07002475 break;
2476 case Instruction::DIV_INT_2ADDR:
Brian Carlstrom93c33962013-07-26 10:37:43 -07002477 work_line_->CheckBinaryOp2addr(inst,
2478 reg_types_.Integer(),
2479 reg_types_.Integer(),
2480 reg_types_.Integer(),
2481 false);
jeffhaobdb76512011-09-07 11:43:16 -07002482 break;
2483 case Instruction::ADD_LONG_2ADDR:
2484 case Instruction::SUB_LONG_2ADDR:
2485 case Instruction::MUL_LONG_2ADDR:
2486 case Instruction::DIV_LONG_2ADDR:
2487 case Instruction::REM_LONG_2ADDR:
2488 case Instruction::AND_LONG_2ADDR:
2489 case Instruction::OR_LONG_2ADDR:
2490 case Instruction::XOR_LONG_2ADDR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002491 work_line_->CheckBinaryOp2addrWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002492 reg_types_.LongLo(), reg_types_.LongHi(),
2493 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002494 break;
2495 case Instruction::SHL_LONG_2ADDR:
2496 case Instruction::SHR_LONG_2ADDR:
2497 case Instruction::USHR_LONG_2ADDR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002498 work_line_->CheckBinaryOp2addrWideShift(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002499 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002500 break;
2501 case Instruction::ADD_FLOAT_2ADDR:
2502 case Instruction::SUB_FLOAT_2ADDR:
2503 case Instruction::MUL_FLOAT_2ADDR:
2504 case Instruction::DIV_FLOAT_2ADDR:
2505 case Instruction::REM_FLOAT_2ADDR:
Brian Carlstrom93c33962013-07-26 10:37:43 -07002506 work_line_->CheckBinaryOp2addr(inst,
2507 reg_types_.Float(),
2508 reg_types_.Float(),
2509 reg_types_.Float(),
2510 false);
jeffhaobdb76512011-09-07 11:43:16 -07002511 break;
2512 case Instruction::ADD_DOUBLE_2ADDR:
2513 case Instruction::SUB_DOUBLE_2ADDR:
2514 case Instruction::MUL_DOUBLE_2ADDR:
2515 case Instruction::DIV_DOUBLE_2ADDR:
2516 case Instruction::REM_DOUBLE_2ADDR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002517 work_line_->CheckBinaryOp2addrWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002518 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2519 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002520 break;
2521 case Instruction::ADD_INT_LIT16:
2522 case Instruction::RSUB_INT:
2523 case Instruction::MUL_INT_LIT16:
2524 case Instruction::DIV_INT_LIT16:
2525 case Instruction::REM_INT_LIT16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002526 work_line_->CheckLiteralOp(inst, reg_types_.Integer(), reg_types_.Integer(), false, true);
jeffhaobdb76512011-09-07 11:43:16 -07002527 break;
2528 case Instruction::AND_INT_LIT16:
2529 case Instruction::OR_INT_LIT16:
2530 case Instruction::XOR_INT_LIT16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002531 work_line_->CheckLiteralOp(inst, reg_types_.Integer(), reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002532 break;
2533 case Instruction::ADD_INT_LIT8:
2534 case Instruction::RSUB_INT_LIT8:
2535 case Instruction::MUL_INT_LIT8:
2536 case Instruction::DIV_INT_LIT8:
2537 case Instruction::REM_INT_LIT8:
2538 case Instruction::SHL_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002539 case Instruction::SHR_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002540 case Instruction::USHR_INT_LIT8:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002541 work_line_->CheckLiteralOp(inst, reg_types_.Integer(), reg_types_.Integer(), false, false);
jeffhaobdb76512011-09-07 11:43:16 -07002542 break;
2543 case Instruction::AND_INT_LIT8:
2544 case Instruction::OR_INT_LIT8:
2545 case Instruction::XOR_INT_LIT8:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002546 work_line_->CheckLiteralOp(inst, reg_types_.Integer(), reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002547 break;
2548
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002549 // Special instructions.
Sebastien Hertzcc10e0e2013-06-28 14:24:48 +02002550 case Instruction::RETURN_VOID_BARRIER:
Ian Rogers9fc16eb2013-07-31 14:49:16 -07002551 if (!IsConstructor() || IsStatic()) {
Sebastien Hertzcc10e0e2013-06-28 14:24:48 +02002552 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void-barrier not expected";
2553 }
2554 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002555 // Note: the following instructions encode offsets derived from class linking.
2556 // As such they use Class*/Field*/AbstractMethod* as these offsets only have
2557 // meaning if the class linking and resolution were successful.
2558 case Instruction::IGET_QUICK:
2559 VerifyIGetQuick(inst, reg_types_.Integer(), true);
2560 break;
2561 case Instruction::IGET_WIDE_QUICK:
2562 VerifyIGetQuick(inst, reg_types_.LongLo(), true);
2563 break;
2564 case Instruction::IGET_OBJECT_QUICK:
2565 VerifyIGetQuick(inst, reg_types_.JavaLangObject(false), false);
2566 break;
2567 case Instruction::IPUT_QUICK:
2568 VerifyIPutQuick(inst, reg_types_.Integer(), true);
2569 break;
2570 case Instruction::IPUT_WIDE_QUICK:
2571 VerifyIPutQuick(inst, reg_types_.LongLo(), true);
2572 break;
2573 case Instruction::IPUT_OBJECT_QUICK:
2574 VerifyIPutQuick(inst, reg_types_.JavaLangObject(false), false);
2575 break;
2576 case Instruction::INVOKE_VIRTUAL_QUICK:
2577 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: {
2578 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
Brian Carlstromea46f952013-07-30 01:26:50 -07002579 mirror::ArtMethod* called_method = VerifyInvokeVirtualQuickArgs(inst, is_range);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002580 if (called_method != NULL) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002581 const char* descriptor = called_method->GetReturnTypeDescriptor();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07002582 const RegType& return_type = reg_types_.FromDescriptor(class_loader_->Get(), descriptor,
Mathieu Chartier590fee92013-09-13 13:46:47 -07002583 false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002584 if (!return_type.IsLowHalf()) {
2585 work_line_->SetResultRegisterType(return_type);
2586 } else {
2587 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2588 }
2589 just_set_result = true;
2590 }
2591 break;
2592 }
2593
Ian Rogersd81871c2011-10-03 13:57:23 -07002594 /* These should never appear during verification. */
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002595 case Instruction::UNUSED_3E:
2596 case Instruction::UNUSED_3F:
2597 case Instruction::UNUSED_40:
2598 case Instruction::UNUSED_41:
2599 case Instruction::UNUSED_42:
2600 case Instruction::UNUSED_43:
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002601 case Instruction::UNUSED_79:
2602 case Instruction::UNUSED_7A:
2603 case Instruction::UNUSED_EB:
2604 case Instruction::UNUSED_EC:
jeffhao9a4f0032012-08-30 16:17:40 -07002605 case Instruction::UNUSED_ED:
jeffhaobdb76512011-09-07 11:43:16 -07002606 case Instruction::UNUSED_EE:
2607 case Instruction::UNUSED_EF:
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002608 case Instruction::UNUSED_F0:
2609 case Instruction::UNUSED_F1:
jeffhaobdb76512011-09-07 11:43:16 -07002610 case Instruction::UNUSED_F2:
2611 case Instruction::UNUSED_F3:
2612 case Instruction::UNUSED_F4:
2613 case Instruction::UNUSED_F5:
2614 case Instruction::UNUSED_F6:
2615 case Instruction::UNUSED_F7:
2616 case Instruction::UNUSED_F8:
2617 case Instruction::UNUSED_F9:
2618 case Instruction::UNUSED_FA:
2619 case Instruction::UNUSED_FB:
jeffhaobdb76512011-09-07 11:43:16 -07002620 case Instruction::UNUSED_FC:
jeffhaobdb76512011-09-07 11:43:16 -07002621 case Instruction::UNUSED_FD:
jeffhaobdb76512011-09-07 11:43:16 -07002622 case Instruction::UNUSED_FE:
jeffhaobdb76512011-09-07 11:43:16 -07002623 case Instruction::UNUSED_FF:
jeffhaod5347e02012-03-22 17:25:05 -07002624 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Unexpected opcode " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07002625 break;
2626
2627 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002628 * DO NOT add a "default" clause here. Without it the compiler will
jeffhaobdb76512011-09-07 11:43:16 -07002629 * complain if an instruction is missing (which is desirable).
2630 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002631 } // end - switch (dec_insn.opcode)
jeffhaobdb76512011-09-07 11:43:16 -07002632
Ian Rogersad0b3a32012-04-16 14:50:24 -07002633 if (have_pending_hard_failure_) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002634 if (Runtime::Current()->IsCompiler()) {
jeffhaob57e9522012-04-26 18:08:21 -07002635 /* When compiling, check that the last failure is a hard failure */
Ian Rogersad0b3a32012-04-16 14:50:24 -07002636 CHECK_EQ(failures_[failures_.size() - 1], VERIFY_ERROR_BAD_CLASS_HARD);
Ian Rogerse1758fe2012-04-19 11:31:15 -07002637 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002638 /* immediate failure, reject class */
2639 info_messages_ << "Rejecting opcode " << inst->DumpString(dex_file_);
2640 return false;
jeffhaofaf459e2012-08-31 15:32:47 -07002641 } else if (have_pending_runtime_throw_failure_) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07002642 /* checking interpreter will throw, mark following code as unreachable */
jeffhaofaf459e2012-08-31 15:32:47 -07002643 opcode_flags = Instruction::kThrow;
jeffhaobdb76512011-09-07 11:43:16 -07002644 }
jeffhaobdb76512011-09-07 11:43:16 -07002645 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002646 * If we didn't just set the result register, clear it out. This ensures that you can only use
2647 * "move-result" immediately after the result is set. (We could check this statically, but it's
2648 * not expensive and it makes our debugging output cleaner.)
jeffhaobdb76512011-09-07 11:43:16 -07002649 */
2650 if (!just_set_result) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002651 work_line_->SetResultTypeToUnknown();
jeffhaobdb76512011-09-07 11:43:16 -07002652 }
2653
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002654
jeffhaobdb76512011-09-07 11:43:16 -07002655
2656 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002657 * Handle "branch". Tag the branch target.
jeffhaobdb76512011-09-07 11:43:16 -07002658 *
2659 * NOTE: instructions like Instruction::EQZ provide information about the
jeffhaod1f0fde2011-09-08 17:25:33 -07002660 * state of the register when the branch is taken or not taken. For example,
jeffhaobdb76512011-09-07 11:43:16 -07002661 * somebody could get a reference field, check it for zero, and if the
2662 * branch is taken immediately store that register in a boolean field
jeffhaod1f0fde2011-09-08 17:25:33 -07002663 * since the value is known to be zero. We do not currently account for
jeffhaobdb76512011-09-07 11:43:16 -07002664 * that, and will reject the code.
2665 *
2666 * TODO: avoid re-fetching the branch target
2667 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002668 if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002669 bool isConditional, selfOkay;
Ian Rogersd81871c2011-10-03 13:57:23 -07002670 if (!GetBranchOffset(work_insn_idx_, &branch_target, &isConditional, &selfOkay)) {
jeffhaobdb76512011-09-07 11:43:16 -07002671 /* should never happen after static verification */
jeffhaod5347e02012-03-22 17:25:05 -07002672 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad branch";
jeffhaobdb76512011-09-07 11:43:16 -07002673 return false;
2674 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08002675 DCHECK_EQ(isConditional, (opcode_flags & Instruction::kContinue) != 0);
jeffhaod5347e02012-03-22 17:25:05 -07002676 if (!CheckNotMoveException(code_item_->insns_, work_insn_idx_ + branch_target)) {
jeffhaobdb76512011-09-07 11:43:16 -07002677 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002678 }
jeffhaobdb76512011-09-07 11:43:16 -07002679 /* update branch target, set "changed" if appropriate */
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002680 if (NULL != branch_line.get()) {
2681 if (!UpdateRegisters(work_insn_idx_ + branch_target, branch_line.get())) {
2682 return false;
2683 }
2684 } else {
2685 if (!UpdateRegisters(work_insn_idx_ + branch_target, work_line_.get())) {
2686 return false;
2687 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002688 }
jeffhaobdb76512011-09-07 11:43:16 -07002689 }
2690
2691 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002692 * Handle "switch". Tag all possible branch targets.
jeffhaobdb76512011-09-07 11:43:16 -07002693 *
2694 * We've already verified that the table is structurally sound, so we
2695 * just need to walk through and tag the targets.
2696 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002697 if ((opcode_flags & Instruction::kSwitch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002698 int offset_to_switch = insns[1] | (((int32_t) insns[2]) << 16);
2699 const uint16_t* switch_insns = insns + offset_to_switch;
2700 int switch_count = switch_insns[1];
2701 int offset_to_targets, targ;
2702
2703 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
2704 /* 0 = sig, 1 = count, 2/3 = first key */
2705 offset_to_targets = 4;
2706 } else {
2707 /* 0 = sig, 1 = count, 2..count * 2 = keys */
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07002708 DCHECK((*insns & 0xff) == Instruction::SPARSE_SWITCH);
jeffhaobdb76512011-09-07 11:43:16 -07002709 offset_to_targets = 2 + 2 * switch_count;
2710 }
2711
2712 /* verify each switch target */
2713 for (targ = 0; targ < switch_count; targ++) {
2714 int offset;
2715 uint32_t abs_offset;
2716
2717 /* offsets are 32-bit, and only partly endian-swapped */
2718 offset = switch_insns[offset_to_targets + targ * 2] |
2719 (((int32_t) switch_insns[offset_to_targets + targ * 2 + 1]) << 16);
Ian Rogersd81871c2011-10-03 13:57:23 -07002720 abs_offset = work_insn_idx_ + offset;
2721 DCHECK_LT(abs_offset, code_item_->insns_size_in_code_units_);
jeffhaod5347e02012-03-22 17:25:05 -07002722 if (!CheckNotMoveException(code_item_->insns_, abs_offset)) {
jeffhaobdb76512011-09-07 11:43:16 -07002723 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002724 }
2725 if (!UpdateRegisters(abs_offset, work_line_.get()))
jeffhaobdb76512011-09-07 11:43:16 -07002726 return false;
2727 }
2728 }
2729
2730 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002731 * Handle instructions that can throw and that are sitting in a "try" block. (If they're not in a
2732 * "try" block when they throw, control transfers out of the method.)
jeffhaobdb76512011-09-07 11:43:16 -07002733 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002734 if ((opcode_flags & Instruction::kThrow) != 0 && insn_flags_[work_insn_idx_].IsInTry()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002735 bool within_catch_all = false;
Ian Rogers0571d352011-11-03 19:51:38 -07002736 CatchHandlerIterator iterator(*code_item_, work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07002737
Ian Rogers0571d352011-11-03 19:51:38 -07002738 for (; iterator.HasNext(); iterator.Next()) {
2739 if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002740 within_catch_all = true;
2741 }
jeffhaobdb76512011-09-07 11:43:16 -07002742 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002743 * Merge registers into the "catch" block. We want to use the "savedRegs" rather than
2744 * "work_regs", because at runtime the exception will be thrown before the instruction
2745 * modifies any registers.
jeffhaobdb76512011-09-07 11:43:16 -07002746 */
Ian Rogers0571d352011-11-03 19:51:38 -07002747 if (!UpdateRegisters(iterator.GetHandlerAddress(), saved_line_.get())) {
jeffhaobdb76512011-09-07 11:43:16 -07002748 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002749 }
jeffhaobdb76512011-09-07 11:43:16 -07002750 }
2751
2752 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002753 * If the monitor stack depth is nonzero, there must be a "catch all" handler for this
2754 * instruction. This does apply to monitor-exit because of async exception handling.
jeffhaobdb76512011-09-07 11:43:16 -07002755 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002756 if (work_line_->MonitorStackDepth() > 0 && !within_catch_all) {
jeffhaobdb76512011-09-07 11:43:16 -07002757 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002758 * The state in work_line reflects the post-execution state. If the current instruction is a
2759 * monitor-enter and the monitor stack was empty, we don't need a catch-all (if it throws,
jeffhaobdb76512011-09-07 11:43:16 -07002760 * it will do so before grabbing the lock).
2761 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002762 if (inst->Opcode() != Instruction::MONITOR_ENTER || work_line_->MonitorStackDepth() != 1) {
jeffhaod5347e02012-03-22 17:25:05 -07002763 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
Ian Rogersd81871c2011-10-03 13:57:23 -07002764 << "expected to be within a catch-all for an instruction where a monitor is held";
jeffhaobdb76512011-09-07 11:43:16 -07002765 return false;
2766 }
2767 }
2768 }
2769
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002770 /* Handle "continue". Tag the next consecutive instruction.
2771 * Note: Keep the code handling "continue" case below the "branch" and "switch" cases,
2772 * because it changes work_line_ when performing peephole optimization
2773 * and this change should not be used in those cases.
2774 */
Ian Rogers6d376ae2013-07-23 15:12:40 -07002775 if ((opcode_flags & Instruction::kContinue) != 0) {
2776 uint32_t next_insn_idx = work_insn_idx_ + CurrentInsnFlags()->GetLengthInCodeUnits();
2777 if (next_insn_idx >= code_item_->insns_size_in_code_units_) {
2778 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Execution can walk off end of code area";
2779 return false;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002780 }
Ian Rogers6d376ae2013-07-23 15:12:40 -07002781 // The only way to get to a move-exception instruction is to get thrown there. Make sure the
2782 // next instruction isn't one.
2783 if (!CheckNotMoveException(code_item_->insns_, next_insn_idx)) {
2784 return false;
2785 }
2786 if (NULL != fallthrough_line.get()) {
2787 // Make workline consistent with fallthrough computed from peephole optimization.
2788 work_line_->CopyFromLine(fallthrough_line.get());
2789 }
Ian Rogersb8c78592013-07-25 23:52:52 +00002790 if (insn_flags_[next_insn_idx].IsReturn()) {
2791 // For returns we only care about the operand to the return, all other registers are dead.
2792 const Instruction* ret_inst = Instruction::At(code_item_->insns_ + next_insn_idx);
2793 Instruction::Code opcode = ret_inst->Opcode();
2794 if ((opcode == Instruction::RETURN_VOID) || (opcode == Instruction::RETURN_VOID_BARRIER)) {
2795 work_line_->MarkAllRegistersAsConflicts();
2796 } else {
2797 if (opcode == Instruction::RETURN_WIDE) {
2798 work_line_->MarkAllRegistersAsConflictsExceptWide(ret_inst->VRegA_11x());
2799 } else {
2800 work_line_->MarkAllRegistersAsConflictsExcept(ret_inst->VRegA_11x());
2801 }
2802 }
2803 }
Ian Rogers6d376ae2013-07-23 15:12:40 -07002804 RegisterLine* next_line = reg_table_.GetLine(next_insn_idx);
2805 if (next_line != NULL) {
2806 // Merge registers into what we have for the next instruction,
2807 // and set the "changed" flag if needed.
2808 if (!UpdateRegisters(next_insn_idx, work_line_.get())) {
2809 return false;
2810 }
2811 } else {
2812 /*
2813 * We're not recording register data for the next instruction, so we don't know what the
2814 * prior state was. We have to assume that something has changed and re-evaluate it.
2815 */
2816 insn_flags_[next_insn_idx].SetChanged();
2817 }
2818 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002819
jeffhaod1f0fde2011-09-08 17:25:33 -07002820 /* If we're returning from the method, make sure monitor stack is empty. */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002821 if ((opcode_flags & Instruction::kReturn) != 0) {
Elliott Hughesb25c3f62012-03-26 16:35:06 -07002822 if (!work_line_->VerifyMonitorStackEmpty()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002823 return false;
2824 }
jeffhaobdb76512011-09-07 11:43:16 -07002825 }
2826
2827 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002828 * Update start_guess. Advance to the next instruction of that's
2829 * possible, otherwise use the branch target if one was found. If
jeffhaobdb76512011-09-07 11:43:16 -07002830 * neither of those exists we're in a return or throw; leave start_guess
2831 * alone and let the caller sort it out.
2832 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002833 if ((opcode_flags & Instruction::kContinue) != 0) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002834 *start_guess = work_insn_idx_ + insn_flags_[work_insn_idx_].GetLengthInCodeUnits();
Elliott Hughesadb8c672012-03-06 16:49:32 -08002835 } else if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002836 /* we're still okay if branch_target is zero */
Ian Rogersd81871c2011-10-03 13:57:23 -07002837 *start_guess = work_insn_idx_ + branch_target;
jeffhaobdb76512011-09-07 11:43:16 -07002838 }
2839
Ian Rogersd81871c2011-10-03 13:57:23 -07002840 DCHECK_LT(*start_guess, code_item_->insns_size_in_code_units_);
2841 DCHECK(insn_flags_[*start_guess].IsOpcode());
jeffhaobdb76512011-09-07 11:43:16 -07002842
2843 return true;
Brian Carlstrom7934ac22013-07-26 10:54:15 -07002844} // NOLINT(readability/fn_size)
jeffhaobdb76512011-09-07 11:43:16 -07002845
Ian Rogers776ac1f2012-04-13 23:36:36 -07002846const RegType& MethodVerifier::ResolveClassAndCheckAccess(uint32_t class_idx) {
Ian Rogers0571d352011-11-03 19:51:38 -07002847 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002848 const RegType& referrer = GetDeclaringClass();
Mathieu Chartier590fee92013-09-13 13:46:47 -07002849 mirror::Class* klass = (*dex_cache_)->GetResolvedType(class_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002850 const RegType& result =
Ian Rogers04f94f42013-06-10 15:09:26 -07002851 klass != NULL ? reg_types_.FromClass(descriptor, klass,
2852 klass->CannotBeAssignedFromOtherTypes())
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07002853 : reg_types_.FromDescriptor(class_loader_->Get(), descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002854 if (result.IsConflict()) {
2855 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "accessing broken descriptor '" << descriptor
2856 << "' in " << referrer;
2857 return result;
2858 }
Ian Rogerse1758fe2012-04-19 11:31:15 -07002859 if (klass == NULL && !result.IsUnresolvedTypes()) {
Mathieu Chartier590fee92013-09-13 13:46:47 -07002860 (*dex_cache_)->SetResolvedType(class_idx, result.GetClass());
Ian Rogerse1758fe2012-04-19 11:31:15 -07002861 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002862 // Check if access is allowed. Unresolved types use xxxWithAccessCheck to
Jeff Haob24b4a72013-07-31 13:47:31 -07002863 // check at runtime if access is allowed and so pass here. If result is
2864 // primitive, skip the access check.
2865 if (result.IsNonZeroReferenceTypes() && !result.IsUnresolvedTypes() &&
2866 !referrer.IsUnresolvedTypes() && !referrer.CanAccess(result)) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002867 Fail(VERIFY_ERROR_ACCESS_CLASS) << "illegal class access: '"
Ian Rogersad0b3a32012-04-16 14:50:24 -07002868 << referrer << "' -> '" << result << "'";
Ian Rogers28ad40d2011-10-27 15:19:26 -07002869 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002870 return result;
Ian Rogersd81871c2011-10-03 13:57:23 -07002871}
2872
Ian Rogers776ac1f2012-04-13 23:36:36 -07002873const RegType& MethodVerifier::GetCaughtExceptionType() {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002874 const RegType* common_super = NULL;
Ian Rogersd81871c2011-10-03 13:57:23 -07002875 if (code_item_->tries_size_ != 0) {
Ian Rogers0571d352011-11-03 19:51:38 -07002876 const byte* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
Ian Rogersd81871c2011-10-03 13:57:23 -07002877 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
2878 for (uint32_t i = 0; i < handlers_size; i++) {
Ian Rogers0571d352011-11-03 19:51:38 -07002879 CatchHandlerIterator iterator(handlers_ptr);
2880 for (; iterator.HasNext(); iterator.Next()) {
2881 if (iterator.GetHandlerAddress() == (uint32_t) work_insn_idx_) {
2882 if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) {
Ian Rogersb4903572012-10-11 11:52:56 -07002883 common_super = &reg_types_.JavaLangThrowable(false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002884 } else {
Ian Rogers0571d352011-11-03 19:51:38 -07002885 const RegType& exception = ResolveClassAndCheckAccess(iterator.GetHandlerTypeIndex());
Jeff Haob878f212014-04-24 16:25:36 -07002886 if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(exception)) {
Jeff Haoc26a56c2013-11-04 12:00:47 -08002887 if (exception.IsUnresolvedTypes()) {
2888 // We don't know enough about the type. Fail here and let runtime handle it.
2889 Fail(VERIFY_ERROR_NO_CLASS) << "unresolved exception class " << exception;
2890 return exception;
2891 } else {
2892 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unexpected non-exception class " << exception;
2893 return reg_types_.Conflict();
2894 }
Jeff Haob878f212014-04-24 16:25:36 -07002895 } else if (common_super == nullptr) {
2896 common_super = &exception;
Ian Rogers28ad40d2011-10-27 15:19:26 -07002897 } else if (common_super->Equals(exception)) {
Ian Rogersc4762272012-02-01 15:55:55 -08002898 // odd case, but nothing to do
Ian Rogersd81871c2011-10-03 13:57:23 -07002899 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002900 common_super = &common_super->Merge(exception, &reg_types_);
Ian Rogersb4903572012-10-11 11:52:56 -07002901 CHECK(reg_types_.JavaLangThrowable(false).IsAssignableFrom(*common_super));
Ian Rogersd81871c2011-10-03 13:57:23 -07002902 }
2903 }
2904 }
2905 }
Ian Rogers0571d352011-11-03 19:51:38 -07002906 handlers_ptr = iterator.EndDataPointer();
Ian Rogersd81871c2011-10-03 13:57:23 -07002907 }
2908 }
2909 if (common_super == NULL) {
2910 /* no catch blocks, or no catches with classes we can find */
jeffhaod5347e02012-03-22 17:25:05 -07002911 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unable to find exception handler";
Ian Rogersad0b3a32012-04-16 14:50:24 -07002912 return reg_types_.Conflict();
Ian Rogersd81871c2011-10-03 13:57:23 -07002913 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002914 return *common_super;
Ian Rogersd81871c2011-10-03 13:57:23 -07002915}
2916
Brian Carlstromea46f952013-07-30 01:26:50 -07002917mirror::ArtMethod* MethodVerifier::ResolveMethodAndCheckAccess(uint32_t dex_method_idx,
Ian Rogersd91d6d62013-09-25 20:26:14 -07002918 MethodType method_type) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002919 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx);
Ian Rogers90040192011-12-16 08:54:29 -08002920 const RegType& klass_type = ResolveClassAndCheckAccess(method_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002921 if (klass_type.IsConflict()) {
2922 std::string append(" in attempt to access method ");
2923 append += dex_file_->GetMethodName(method_id);
2924 AppendToLastFailMessage(append);
Ian Rogers90040192011-12-16 08:54:29 -08002925 return NULL;
2926 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002927 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers90040192011-12-16 08:54:29 -08002928 return NULL; // Can't resolve Class so no more to do here
2929 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002930 mirror::Class* klass = klass_type.GetClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002931 const RegType& referrer = GetDeclaringClass();
Mathieu Chartier590fee92013-09-13 13:46:47 -07002932 mirror::ArtMethod* res_method = (*dex_cache_)->GetResolvedMethod(dex_method_idx);
Ian Rogersd81871c2011-10-03 13:57:23 -07002933 if (res_method == NULL) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07002934 const char* name = dex_file_->GetMethodName(method_id);
Ian Rogersd91d6d62013-09-25 20:26:14 -07002935 const Signature signature = dex_file_->GetMethodSignature(method_id);
jeffhao8cd6dda2012-02-22 10:15:34 -08002936
2937 if (method_type == METHOD_DIRECT || method_type == METHOD_STATIC) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002938 res_method = klass->FindDirectMethod(name, signature);
jeffhao8cd6dda2012-02-22 10:15:34 -08002939 } else if (method_type == METHOD_INTERFACE) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002940 res_method = klass->FindInterfaceMethod(name, signature);
2941 } else {
2942 res_method = klass->FindVirtualMethod(name, signature);
2943 }
2944 if (res_method != NULL) {
Mathieu Chartier590fee92013-09-13 13:46:47 -07002945 (*dex_cache_)->SetResolvedMethod(dex_method_idx, res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07002946 } else {
jeffhao8cd6dda2012-02-22 10:15:34 -08002947 // If a virtual or interface method wasn't found with the expected type, look in
2948 // the direct methods. This can happen when the wrong invoke type is used or when
2949 // a class has changed, and will be flagged as an error in later checks.
2950 if (method_type == METHOD_INTERFACE || method_type == METHOD_VIRTUAL) {
2951 res_method = klass->FindDirectMethod(name, signature);
2952 }
2953 if (res_method == NULL) {
2954 Fail(VERIFY_ERROR_NO_METHOD) << "couldn't find method "
2955 << PrettyDescriptor(klass) << "." << name
2956 << " " << signature;
2957 return NULL;
2958 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002959 }
2960 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002961 // Make sure calls to constructors are "direct". There are additional restrictions but we don't
2962 // enforce them here.
2963 if (res_method->IsConstructor() && method_type != METHOD_DIRECT) {
jeffhaod5347e02012-03-22 17:25:05 -07002964 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting non-direct call to constructor "
2965 << PrettyMethod(res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07002966 return NULL;
2967 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002968 // Disallow any calls to class initializers.
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002969 if (res_method->IsClassInitializer()) {
jeffhaod5347e02012-03-22 17:25:05 -07002970 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting call to class initializer "
2971 << PrettyMethod(res_method);
jeffhao8cd6dda2012-02-22 10:15:34 -08002972 return NULL;
2973 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002974 // Check if access is allowed.
Ian Rogersad0b3a32012-04-16 14:50:24 -07002975 if (!referrer.CanAccessMember(res_method->GetDeclaringClass(), res_method->GetAccessFlags())) {
jeffhao8cd6dda2012-02-22 10:15:34 -08002976 Fail(VERIFY_ERROR_ACCESS_METHOD) << "illegal method access (call " << PrettyMethod(res_method)
Ian Rogersad0b3a32012-04-16 14:50:24 -07002977 << " from " << referrer << ")";
jeffhaob57e9522012-04-26 18:08:21 -07002978 return res_method;
jeffhao8cd6dda2012-02-22 10:15:34 -08002979 }
jeffhaode0d9c92012-02-27 13:58:13 -08002980 // Check that invoke-virtual and invoke-super are not used on private methods of the same class.
2981 if (res_method->IsPrivate() && method_type == METHOD_VIRTUAL) {
jeffhaod5347e02012-03-22 17:25:05 -07002982 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invoke-super/virtual can't be used on private method "
2983 << PrettyMethod(res_method);
jeffhaode0d9c92012-02-27 13:58:13 -08002984 return NULL;
2985 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002986 // Check that interface methods match interface classes.
2987 if (klass->IsInterface() && method_type != METHOD_INTERFACE) {
2988 Fail(VERIFY_ERROR_CLASS_CHANGE) << "non-interface method " << PrettyMethod(res_method)
2989 << " is in an interface class " << PrettyClass(klass);
2990 return NULL;
2991 } else if (!klass->IsInterface() && method_type == METHOD_INTERFACE) {
2992 Fail(VERIFY_ERROR_CLASS_CHANGE) << "interface method " << PrettyMethod(res_method)
2993 << " is in a non-interface class " << PrettyClass(klass);
2994 return NULL;
2995 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002996 // See if the method type implied by the invoke instruction matches the access flags for the
2997 // target method.
2998 if ((method_type == METHOD_DIRECT && !res_method->IsDirect()) ||
2999 (method_type == METHOD_STATIC && !res_method->IsStatic()) ||
3000 ((method_type == METHOD_VIRTUAL || method_type == METHOD_INTERFACE) && res_method->IsDirect())
3001 ) {
Ian Rogers2fc14272012-08-30 10:56:57 -07003002 Fail(VERIFY_ERROR_CLASS_CHANGE) << "invoke type (" << method_type << ") does not match method "
3003 " type of " << PrettyMethod(res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07003004 return NULL;
3005 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003006 return res_method;
3007}
3008
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003009template <class T>
3010mirror::ArtMethod* MethodVerifier::VerifyInvocationArgsFromIterator(T* it, const Instruction* inst,
3011 MethodType method_type,
3012 bool is_range,
3013 mirror::ArtMethod* res_method) {
3014 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
3015 // match the call to the signature. Also, we might be calling through an abstract method
3016 // definition (which doesn't have register count values).
3017 const size_t expected_args = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
3018 /* caught by static verifier */
3019 DCHECK(is_range || expected_args <= 5);
3020 if (expected_args > code_item_->outs_size_) {
3021 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
3022 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
3023 return nullptr;
3024 }
3025
3026 uint32_t arg[5];
3027 if (!is_range) {
3028 inst->GetVarArgs(arg);
3029 }
3030 uint32_t sig_registers = 0;
3031
3032 /*
3033 * Check the "this" argument, which must be an instance of the class that declared the method.
3034 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
3035 * rigorous check here (which is okay since we have to do it at runtime).
3036 */
3037 if (method_type != METHOD_STATIC) {
3038 const RegType& actual_arg_type = work_line_->GetInvocationThis(inst, is_range);
3039 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
3040 CHECK(have_pending_hard_failure_);
3041 return nullptr;
3042 }
3043 if (actual_arg_type.IsUninitializedReference()) {
3044 if (res_method) {
3045 if (!res_method->IsConstructor()) {
3046 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
3047 return nullptr;
3048 }
3049 } else {
3050 // Check whether the name of the called method is "<init>"
3051 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
3052 if (strcmp(dex_file_->GetMethodName(dex_file_->GetMethodId(method_idx)), "init") != 0) {
3053 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
3054 return nullptr;
3055 }
3056 }
3057 }
3058 if (method_type != METHOD_INTERFACE && !actual_arg_type.IsZero()) {
3059 const RegType* res_method_class;
3060 if (res_method != nullptr) {
3061 mirror::Class* klass = res_method->GetDeclaringClass();
3062 res_method_class = &reg_types_.FromClass(klass->GetDescriptor().c_str(), klass,
3063 klass->CannotBeAssignedFromOtherTypes());
3064 } else {
3065 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
3066 const uint16_t class_idx = dex_file_->GetMethodId(method_idx).class_idx_;
3067 res_method_class = &reg_types_.FromDescriptor(class_loader_->Get(),
3068 dex_file_->StringByTypeIdx(class_idx),
3069 false);
3070 }
3071 if (!res_method_class->IsAssignableFrom(actual_arg_type)) {
3072 Fail(actual_arg_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS:
3073 VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type
3074 << "' not instance of '" << *res_method_class << "'";
3075 // Continue on soft failures. We need to find possible hard failures to avoid problems in
3076 // the compiler.
3077 if (have_pending_hard_failure_) {
3078 return nullptr;
3079 }
3080 }
3081 }
3082 sig_registers = 1;
3083 }
3084
3085 for ( ; it->HasNext(); it->Next()) {
3086 if (sig_registers >= expected_args) {
3087 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, expected " << inst->VRegA() <<
3088 " arguments, found " << sig_registers << " or more.";
3089 return nullptr;
3090 }
3091
3092 const char* param_descriptor = it->GetDescriptor();
3093
3094 if (param_descriptor == nullptr) {
3095 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation because of missing signature "
3096 "component";
3097 return nullptr;
3098 }
3099
3100 const RegType& reg_type = reg_types_.FromDescriptor(class_loader_->Get(), param_descriptor,
3101 false);
3102 uint32_t get_reg = is_range ? inst->VRegC_3rc() + static_cast<uint32_t>(sig_registers) :
3103 arg[sig_registers];
3104 if (reg_type.IsIntegralTypes()) {
3105 const RegType& src_type = work_line_->GetRegisterType(get_reg);
3106 if (!src_type.IsIntegralTypes()) {
3107 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register v" << get_reg << " has type " << src_type
3108 << " but expected " << reg_type;
3109 return res_method;
3110 }
3111 } else if (!work_line_->VerifyRegisterType(get_reg, reg_type)) {
3112 // Continue on soft failures. We need to find possible hard failures to avoid problems in the
3113 // compiler.
3114 if (have_pending_hard_failure_) {
3115 return res_method;
3116 }
3117 }
3118 sig_registers += reg_type.IsLongOrDoubleTypes() ? 2 : 1;
3119 }
3120 if (expected_args != sig_registers) {
3121 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, expected " << expected_args <<
3122 " arguments, found " << sig_registers;
3123 return nullptr;
3124 }
3125 return res_method;
3126}
3127
3128void MethodVerifier::VerifyInvocationArgsUnresolvedMethod(const Instruction* inst,
3129 MethodType method_type,
3130 bool is_range) {
3131 // As the method may not have been resolved, make this static check against what we expect.
3132 // The main reason for this code block is to fail hard when we find an illegal use, e.g.,
3133 // wrong number of arguments or wrong primitive types, even if the method could not be resolved.
3134 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
3135 DexFileParameterIterator it(*dex_file_,
3136 dex_file_->GetProtoId(dex_file_->GetMethodId(method_idx).proto_idx_));
3137 VerifyInvocationArgsFromIterator<DexFileParameterIterator>(&it, inst, method_type, is_range,
3138 nullptr);
3139}
3140
3141class MethodParamListDescriptorIterator {
3142 public:
3143 explicit MethodParamListDescriptorIterator(mirror::ArtMethod* res_method) :
3144 res_method_(res_method), pos_(0), params_(res_method->GetParameterTypeList()),
3145 params_size_(params_ == nullptr ? 0 : params_->Size()) {
3146 }
3147
3148 bool HasNext() {
3149 return pos_ < params_size_;
3150 }
3151
3152 void Next() {
3153 ++pos_;
3154 }
3155
3156 const char* GetDescriptor() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
3157 return res_method_->GetTypeDescriptorFromTypeIdx(params_->GetTypeItem(pos_).type_idx_);
3158 }
3159
3160 private:
3161 mirror::ArtMethod* res_method_;
3162 size_t pos_;
3163 const DexFile::TypeList* params_;
3164 const size_t params_size_;
3165};
3166
Brian Carlstromea46f952013-07-30 01:26:50 -07003167mirror::ArtMethod* MethodVerifier::VerifyInvocationArgs(const Instruction* inst,
Sebastien Hertz5243e912013-05-21 10:55:07 +02003168 MethodType method_type,
3169 bool is_range,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003170 bool is_super) {
jeffhao8cd6dda2012-02-22 10:15:34 -08003171 // Resolve the method. This could be an abstract or concrete method depending on what sort of call
3172 // we're making.
Sebastien Hertz5243e912013-05-21 10:55:07 +02003173 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Andreas Gampeacc4d2f2014-06-12 19:35:05 -07003174
Brian Carlstromea46f952013-07-30 01:26:50 -07003175 mirror::ArtMethod* res_method = ResolveMethodAndCheckAccess(method_idx, method_type);
jeffhao8cd6dda2012-02-22 10:15:34 -08003176 if (res_method == NULL) { // error or class is unresolved
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003177 // Check what we can statically.
3178 if (!have_pending_hard_failure_) {
3179 VerifyInvocationArgsUnresolvedMethod(inst, method_type, is_range);
3180 }
3181 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003182 }
3183
Ian Rogersd81871c2011-10-03 13:57:23 -07003184 // If we're using invoke-super(method), make sure that the executing method's class' superclass
3185 // has a vtable entry for the target method.
3186 if (is_super) {
3187 DCHECK(method_type == METHOD_VIRTUAL);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003188 const RegType& super = GetDeclaringClass().GetSuperClass(&reg_types_);
Ian Rogers529781d2012-07-23 17:24:29 -07003189 if (super.IsUnresolvedTypes()) {
jeffhao4d8df822012-04-24 17:09:36 -07003190 Fail(VERIFY_ERROR_NO_METHOD) << "unknown super class in invoke-super from "
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003191 << PrettyMethod(dex_method_idx_, *dex_file_)
jeffhao4d8df822012-04-24 17:09:36 -07003192 << " to super " << PrettyMethod(res_method);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003193 return nullptr;
jeffhao4d8df822012-04-24 17:09:36 -07003194 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003195 mirror::Class* super_klass = super.GetClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07003196 if (res_method->GetMethodIndex() >= super_klass->GetVTable()->GetLength()) {
jeffhao4d8df822012-04-24 17:09:36 -07003197 Fail(VERIFY_ERROR_NO_METHOD) << "invalid invoke-super from "
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003198 << PrettyMethod(dex_method_idx_, *dex_file_)
jeffhao4d8df822012-04-24 17:09:36 -07003199 << " to super " << super
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003200 << "." << res_method->GetName()
3201 << res_method->GetSignature();
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003202 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003203 }
3204 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003205
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003206 // Process the target method's signature. This signature may or may not
3207 MethodParamListDescriptorIterator it(res_method);
3208 return VerifyInvocationArgsFromIterator<MethodParamListDescriptorIterator>(&it, inst, method_type,
3209 is_range, res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07003210}
3211
Brian Carlstromea46f952013-07-30 01:26:50 -07003212mirror::ArtMethod* MethodVerifier::GetQuickInvokedMethod(const Instruction* inst,
Mathieu Chartier590fee92013-09-13 13:46:47 -07003213 RegisterLine* reg_line, bool is_range) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003214 DCHECK(inst->Opcode() == Instruction::INVOKE_VIRTUAL_QUICK ||
3215 inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
3216 const RegType& actual_arg_type = reg_line->GetInvocationThis(inst, is_range);
Ian Rogers9bc54402014-04-17 16:40:01 -07003217 if (!actual_arg_type.HasClass()) {
3218 VLOG(verifier) << "Failed to get mirror::Class* from '" << actual_arg_type << "'";
Andreas Gampe63981562014-04-17 12:28:43 -07003219 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003220 }
Ian Rogersa4cf1df2014-05-07 19:47:17 -07003221 mirror::ObjectArray<mirror::ArtMethod>* vtable = nullptr;
3222 mirror::Class* klass = actual_arg_type.GetClass();
3223 if (klass->IsInterface()) {
3224 // Derive Object.class from Class.class.getSuperclass().
3225 mirror::Class* object_klass = klass->GetClass()->GetSuperClass();
3226 CHECK(object_klass->IsObjectClass());
3227 vtable = object_klass->GetVTable();
3228 } else {
3229 vtable = klass->GetVTable();
3230 }
3231 CHECK(vtable != nullptr) << PrettyDescriptor(klass);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003232 uint16_t vtable_index = is_range ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersa4cf1df2014-05-07 19:47:17 -07003233 CHECK_LT(static_cast<int32_t>(vtable_index), vtable->GetLength()) << PrettyDescriptor(klass);
Brian Carlstromea46f952013-07-30 01:26:50 -07003234 mirror::ArtMethod* res_method = vtable->Get(vtable_index);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003235 CHECK(!Thread::Current()->IsExceptionPending());
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003236 return res_method;
3237}
3238
Brian Carlstromea46f952013-07-30 01:26:50 -07003239mirror::ArtMethod* MethodVerifier::VerifyInvokeVirtualQuickArgs(const Instruction* inst,
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003240 bool is_range) {
3241 DCHECK(Runtime::Current()->IsStarted());
Brian Carlstromea46f952013-07-30 01:26:50 -07003242 mirror::ArtMethod* res_method = GetQuickInvokedMethod(inst, work_line_.get(),
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003243 is_range);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003244 if (res_method == NULL) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003245 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer method from " << inst->Name();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003246 return NULL;
3247 }
3248 CHECK(!res_method->IsDirect() && !res_method->IsStatic());
3249
3250 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
3251 // match the call to the signature. Also, we might be calling through an abstract method
3252 // definition (which doesn't have register count values).
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003253 const RegType& actual_arg_type = work_line_->GetInvocationThis(inst, is_range);
3254 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
3255 return NULL;
3256 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003257 const size_t expected_args = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
3258 /* caught by static verifier */
3259 DCHECK(is_range || expected_args <= 5);
3260 if (expected_args > code_item_->outs_size_) {
3261 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
3262 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
3263 return NULL;
3264 }
3265
3266 /*
3267 * Check the "this" argument, which must be an instance of the class that declared the method.
3268 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
3269 * rigorous check here (which is okay since we have to do it at runtime).
3270 */
3271 if (actual_arg_type.IsUninitializedReference() && !res_method->IsConstructor()) {
3272 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
3273 return NULL;
3274 }
3275 if (!actual_arg_type.IsZero()) {
3276 mirror::Class* klass = res_method->GetDeclaringClass();
3277 const RegType& res_method_class =
Mathieu Chartierf8322842014-05-16 10:59:25 -07003278 reg_types_.FromClass(klass->GetDescriptor().c_str(), klass,
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003279 klass->CannotBeAssignedFromOtherTypes());
3280 if (!res_method_class.IsAssignableFrom(actual_arg_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07003281 Fail(actual_arg_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS :
3282 VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003283 << "' not instance of '" << res_method_class << "'";
3284 return NULL;
3285 }
3286 }
3287 /*
3288 * Process the target method's signature. This signature may or may not
3289 * have been verified, so we can't assume it's properly formed.
3290 */
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003291 const DexFile::TypeList* params = res_method->GetParameterTypeList();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003292 size_t params_size = params == NULL ? 0 : params->Size();
3293 uint32_t arg[5];
3294 if (!is_range) {
Ian Rogers29a26482014-05-02 15:27:29 -07003295 inst->GetVarArgs(arg);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003296 }
3297 size_t actual_args = 1;
3298 for (size_t param_index = 0; param_index < params_size; param_index++) {
3299 if (actual_args >= expected_args) {
3300 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invalid call to '" << PrettyMethod(res_method)
Brian Carlstrom93c33962013-07-26 10:37:43 -07003301 << "'. Expected " << expected_args
3302 << " arguments, processing argument " << actual_args
3303 << " (where longs/doubles count twice).";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003304 return NULL;
3305 }
3306 const char* descriptor =
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003307 res_method->GetTypeDescriptorFromTypeIdx(params->GetTypeItem(param_index).type_idx_);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003308 if (descriptor == NULL) {
3309 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003310 << " missing signature component";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003311 return NULL;
3312 }
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003313 const RegType& reg_type = reg_types_.FromDescriptor(class_loader_->Get(), descriptor, false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003314 uint32_t get_reg = is_range ? inst->VRegC_3rc() + actual_args : arg[actual_args];
3315 if (!work_line_->VerifyRegisterType(get_reg, reg_type)) {
3316 return res_method;
3317 }
3318 actual_args = reg_type.IsLongOrDoubleTypes() ? actual_args + 2 : actual_args + 1;
3319 }
3320 if (actual_args != expected_args) {
3321 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
3322 << " expected " << expected_args << " arguments, found " << actual_args;
3323 return NULL;
3324 } else {
3325 return res_method;
3326 }
3327}
3328
Ian Rogers62342ec2013-06-11 10:26:37 -07003329void MethodVerifier::VerifyNewArray(const Instruction* inst, bool is_filled, bool is_range) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003330 uint32_t type_idx;
3331 if (!is_filled) {
3332 DCHECK_EQ(inst->Opcode(), Instruction::NEW_ARRAY);
3333 type_idx = inst->VRegC_22c();
3334 } else if (!is_range) {
3335 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY);
3336 type_idx = inst->VRegB_35c();
3337 } else {
3338 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY_RANGE);
3339 type_idx = inst->VRegB_3rc();
3340 }
3341 const RegType& res_type = ResolveClassAndCheckAccess(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003342 if (res_type.IsConflict()) { // bad class
3343 DCHECK_NE(failures_.size(), 0U);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003344 } else {
3345 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
3346 if (!res_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003347 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "new-array on non-array class " << res_type;
Ian Rogers0c4a5062012-02-03 15:18:59 -08003348 } else if (!is_filled) {
3349 /* make sure "size" register is valid type */
Sebastien Hertz5243e912013-05-21 10:55:07 +02003350 work_line_->VerifyRegisterType(inst->VRegB_22c(), reg_types_.Integer());
Ian Rogers0c4a5062012-02-03 15:18:59 -08003351 /* set register type to array class */
Ian Rogers62342ec2013-06-11 10:26:37 -07003352 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
3353 work_line_->SetRegisterType(inst->VRegA_22c(), precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003354 } else {
3355 // Verify each register. If "arg_count" is bad, VerifyRegisterType() will run off the end of
3356 // the list and fail. It's legal, if silly, for arg_count to be zero.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003357 const RegType& expected_type = reg_types_.GetComponentType(res_type, class_loader_->Get());
Sebastien Hertz5243e912013-05-21 10:55:07 +02003358 uint32_t arg_count = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
3359 uint32_t arg[5];
3360 if (!is_range) {
Ian Rogers29a26482014-05-02 15:27:29 -07003361 inst->GetVarArgs(arg);
Sebastien Hertz5243e912013-05-21 10:55:07 +02003362 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08003363 for (size_t ui = 0; ui < arg_count; ui++) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003364 uint32_t get_reg = is_range ? inst->VRegC_3rc() + ui : arg[ui];
Ian Rogers0c4a5062012-02-03 15:18:59 -08003365 if (!work_line_->VerifyRegisterType(get_reg, expected_type)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003366 work_line_->SetResultRegisterType(reg_types_.Conflict());
Ian Rogers0c4a5062012-02-03 15:18:59 -08003367 return;
3368 }
3369 }
3370 // filled-array result goes into "result" register
Ian Rogers62342ec2013-06-11 10:26:37 -07003371 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
3372 work_line_->SetResultRegisterType(precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003373 }
3374 }
3375}
3376
Sebastien Hertz5243e912013-05-21 10:55:07 +02003377void MethodVerifier::VerifyAGet(const Instruction* inst,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003378 const RegType& insn_type, bool is_primitive) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003379 const RegType& index_type = work_line_->GetRegisterType(inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07003380 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003381 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07003382 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003383 const RegType& array_type = work_line_->GetRegisterType(inst->VRegB_23x());
Ian Rogers89310de2012-02-01 13:47:30 -08003384 if (array_type.IsZero()) {
3385 // Null array class; this code path will fail at runtime. Infer a merge-able type from the
3386 // instruction type. TODO: have a proper notion of bottom here.
3387 if (!is_primitive || insn_type.IsCategory1Types()) {
3388 // Reference or category 1
Sebastien Hertz5243e912013-05-21 10:55:07 +02003389 work_line_->SetRegisterType(inst->VRegA_23x(), reg_types_.Zero());
Ian Rogersd81871c2011-10-03 13:57:23 -07003390 } else {
Ian Rogers89310de2012-02-01 13:47:30 -08003391 // Category 2
Sebastien Hertz5243e912013-05-21 10:55:07 +02003392 work_line_->SetRegisterTypeWide(inst->VRegA_23x(), reg_types_.FromCat2ConstLo(0, false),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003393 reg_types_.FromCat2ConstHi(0, false));
Ian Rogers89310de2012-02-01 13:47:30 -08003394 }
jeffhaofc3144e2012-02-01 17:21:15 -08003395 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003396 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aget";
Ian Rogers89310de2012-02-01 13:47:30 -08003397 } else {
3398 /* verify the class */
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003399 const RegType& component_type = reg_types_.GetComponentType(array_type, class_loader_->Get());
jeffhaofc3144e2012-02-01 17:21:15 -08003400 if (!component_type.IsReferenceTypes() && !is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07003401 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08003402 << " source for aget-object";
3403 } else if (component_type.IsNonZeroReferenceTypes() && is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07003404 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08003405 << " source for category 1 aget";
3406 } else if (is_primitive && !insn_type.Equals(component_type) &&
3407 !((insn_type.IsInteger() && component_type.IsFloat()) ||
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003408 (insn_type.IsLong() && component_type.IsDouble()))) {
3409 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array type " << array_type
3410 << " incompatible with aget of type " << insn_type;
Ian Rogers89310de2012-02-01 13:47:30 -08003411 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003412 // Use knowledge of the field type which is stronger than the type inferred from the
3413 // instruction, which can't differentiate object types and ints from floats, longs from
3414 // doubles.
3415 if (!component_type.IsLowHalf()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003416 work_line_->SetRegisterType(inst->VRegA_23x(), component_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003417 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003418 work_line_->SetRegisterTypeWide(inst->VRegA_23x(), component_type,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003419 component_type.HighHalf(&reg_types_));
3420 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003421 }
3422 }
3423 }
3424}
3425
Jeff Haofe1f7c82013-08-01 14:50:24 -07003426void MethodVerifier::VerifyPrimitivePut(const RegType& target_type, const RegType& insn_type,
3427 const uint32_t vregA) {
3428 // Primitive assignability rules are weaker than regular assignability rules.
3429 bool instruction_compatible;
3430 bool value_compatible;
3431 const RegType& value_type = work_line_->GetRegisterType(vregA);
3432 if (target_type.IsIntegralTypes()) {
Jeff Haoa4647482013-08-06 15:35:47 -07003433 instruction_compatible = target_type.Equals(insn_type);
Jeff Haofe1f7c82013-08-01 14:50:24 -07003434 value_compatible = value_type.IsIntegralTypes();
3435 } else if (target_type.IsFloat()) {
3436 instruction_compatible = insn_type.IsInteger(); // no put-float, so expect put-int
3437 value_compatible = value_type.IsFloatTypes();
3438 } else if (target_type.IsLong()) {
3439 instruction_compatible = insn_type.IsLong();
3440 value_compatible = value_type.IsLongTypes();
3441 } else if (target_type.IsDouble()) {
3442 instruction_compatible = insn_type.IsLong(); // no put-double, so expect put-long
3443 value_compatible = value_type.IsDoubleTypes();
3444 } else {
3445 instruction_compatible = false; // reference with primitive store
3446 value_compatible = false; // unused
3447 }
3448 if (!instruction_compatible) {
3449 // This is a global failure rather than a class change failure as the instructions and
3450 // the descriptors for the type should have been consistent within the same file at
3451 // compile time.
3452 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "put insn has type '" << insn_type
3453 << "' but expected type '" << target_type << "'";
3454 return;
3455 }
3456 if (!value_compatible) {
3457 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
3458 << " of type " << value_type << " but expected " << target_type << " for put";
3459 return;
3460 }
3461}
3462
Sebastien Hertz5243e912013-05-21 10:55:07 +02003463void MethodVerifier::VerifyAPut(const Instruction* inst,
Sebastien Hertz757b3042014-03-28 14:34:28 +01003464 const RegType& insn_type, bool is_primitive) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003465 const RegType& index_type = work_line_->GetRegisterType(inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07003466 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003467 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07003468 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003469 const RegType& array_type = work_line_->GetRegisterType(inst->VRegB_23x());
Ian Rogers89310de2012-02-01 13:47:30 -08003470 if (array_type.IsZero()) {
3471 // Null array type; this code path will fail at runtime. Infer a merge-able type from the
3472 // instruction type.
jeffhaofc3144e2012-02-01 17:21:15 -08003473 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003474 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aput";
Ian Rogers89310de2012-02-01 13:47:30 -08003475 } else {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003476 const RegType& component_type = reg_types_.GetComponentType(array_type, class_loader_->Get());
Jeff Haofe1f7c82013-08-01 14:50:24 -07003477 const uint32_t vregA = inst->VRegA_23x();
Jeff Haob24b4a72013-07-31 13:47:31 -07003478 if (is_primitive) {
Jeff Haofe1f7c82013-08-01 14:50:24 -07003479 VerifyPrimitivePut(component_type, insn_type, vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07003480 } else {
Jeff Haob24b4a72013-07-31 13:47:31 -07003481 if (!component_type.IsReferenceTypes()) {
3482 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
3483 << " source for aput-object";
3484 } else {
3485 // The instruction agrees with the type of array, confirm the value to be stored does too
3486 // Note: we use the instruction type (rather than the component type) for aput-object as
3487 // incompatible classes will be caught at runtime as an array store exception
Jeff Haofe1f7c82013-08-01 14:50:24 -07003488 work_line_->VerifyRegisterType(vregA, insn_type);
Jeff Haob24b4a72013-07-31 13:47:31 -07003489 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003490 }
3491 }
3492 }
3493}
3494
Brian Carlstromea46f952013-07-30 01:26:50 -07003495mirror::ArtField* MethodVerifier::GetStaticField(int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08003496 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3497 // Check access to class
3498 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Brian Carlstrom7934ac22013-07-26 10:54:15 -07003499 if (klass_type.IsConflict()) { // bad class
Ian Rogersad0b3a32012-04-16 14:50:24 -07003500 AppendToLastFailMessage(StringPrintf(" in attempt to access static field %d (%s) in %s",
3501 field_idx, dex_file_->GetFieldName(field_id),
3502 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers90040192011-12-16 08:54:29 -08003503 return NULL;
3504 }
Elliott Hughesb25c3f62012-03-26 16:35:06 -07003505 if (klass_type.IsUnresolvedTypes()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003506 return NULL; // Can't resolve Class so no more to do here, will do checking at runtime.
Ian Rogers90040192011-12-16 08:54:29 -08003507 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07003508 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
3509 mirror::ArtField* field = class_linker->ResolveFieldJLS(*dex_file_, field_idx, *dex_cache_,
3510 *class_loader_);
Ian Rogersd81871c2011-10-03 13:57:23 -07003511 if (field == NULL) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07003512 VLOG(verifier) << "Unable to resolve static field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07003513 << dex_file_->GetFieldName(field_id) << ") in "
3514 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07003515 DCHECK(Thread::Current()->IsExceptionPending());
3516 Thread::Current()->ClearException();
3517 return NULL;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003518 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
3519 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003520 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access static field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07003521 << " from " << GetDeclaringClass();
Ian Rogersd81871c2011-10-03 13:57:23 -07003522 return NULL;
3523 } else if (!field->IsStatic()) {
3524 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field) << " to be static";
3525 return NULL;
Ian Rogersd81871c2011-10-03 13:57:23 -07003526 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07003527 return field;
Ian Rogersd81871c2011-10-03 13:57:23 -07003528}
3529
Brian Carlstromea46f952013-07-30 01:26:50 -07003530mirror::ArtField* MethodVerifier::GetInstanceField(const RegType& obj_type, int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08003531 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3532 // Check access to class
3533 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003534 if (klass_type.IsConflict()) {
3535 AppendToLastFailMessage(StringPrintf(" in attempt to access instance field %d (%s) in %s",
3536 field_idx, dex_file_->GetFieldName(field_id),
3537 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers90040192011-12-16 08:54:29 -08003538 return NULL;
3539 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003540 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers90040192011-12-16 08:54:29 -08003541 return NULL; // Can't resolve Class so no more to do here
3542 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07003543 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
3544 mirror::ArtField* field = class_linker->ResolveFieldJLS(*dex_file_, field_idx, *dex_cache_,
3545 *class_loader_);
Ian Rogersd81871c2011-10-03 13:57:23 -07003546 if (field == NULL) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07003547 VLOG(verifier) << "Unable to resolve instance field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07003548 << dex_file_->GetFieldName(field_id) << ") in "
3549 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07003550 DCHECK(Thread::Current()->IsExceptionPending());
3551 Thread::Current()->ClearException();
3552 return NULL;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003553 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
3554 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003555 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access instance field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07003556 << " from " << GetDeclaringClass();
Ian Rogersd81871c2011-10-03 13:57:23 -07003557 return NULL;
3558 } else if (field->IsStatic()) {
3559 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field)
3560 << " to not be static";
3561 return NULL;
3562 } else if (obj_type.IsZero()) {
3563 // Cannot infer and check type, however, access will cause null pointer exception
3564 return field;
Ian Rogerse1758fe2012-04-19 11:31:15 -07003565 } else {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003566 mirror::Class* klass = field->GetDeclaringClass();
Ian Rogers637c65b2013-05-31 11:46:00 -07003567 const RegType& field_klass =
3568 reg_types_.FromClass(dex_file_->GetFieldDeclaringClassDescriptor(field_id),
Ian Rogers04f94f42013-06-10 15:09:26 -07003569 klass, klass->CannotBeAssignedFromOtherTypes());
Ian Rogersad0b3a32012-04-16 14:50:24 -07003570 if (obj_type.IsUninitializedTypes() &&
3571 (!IsConstructor() || GetDeclaringClass().Equals(obj_type) ||
3572 !field_klass.Equals(GetDeclaringClass()))) {
3573 // Field accesses through uninitialized references are only allowable for constructors where
3574 // the field is declared in this class
3575 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "cannot access instance field " << PrettyField(field)
Brian Carlstrom93c33962013-07-26 10:37:43 -07003576 << " of a not fully initialized object within the context"
3577 << " of " << PrettyMethod(dex_method_idx_, *dex_file_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003578 return NULL;
3579 } else if (!field_klass.IsAssignableFrom(obj_type)) {
3580 // Trying to access C1.field1 using reference of type C2, which is neither C1 or a sub-class
3581 // of C1. For resolution to occur the declared class of the field must be compatible with
3582 // obj_type, we've discovered this wasn't so, so report the field didn't exist.
3583 Fail(VERIFY_ERROR_NO_FIELD) << "cannot access instance field " << PrettyField(field)
3584 << " from object of type " << obj_type;
3585 return NULL;
3586 } else {
3587 return field;
3588 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003589 }
3590}
3591
Sebastien Hertz5243e912013-05-21 10:55:07 +02003592void MethodVerifier::VerifyISGet(const Instruction* inst, const RegType& insn_type,
3593 bool is_primitive, bool is_static) {
3594 uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
Brian Carlstromea46f952013-07-30 01:26:50 -07003595 mirror::ArtField* field;
Ian Rogersb94a27b2011-10-26 00:33:41 -07003596 if (is_static) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07003597 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003598 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003599 const RegType& object_type = work_line_->GetRegisterType(inst->VRegB_22c());
Ian Rogersf4028cc2011-11-02 14:56:39 -07003600 field = GetInstanceField(object_type, field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003601 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003602 const RegType* field_type = nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003603 if (field != NULL) {
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07003604 Thread* self = Thread::Current();
3605 mirror::Class* field_type_class;
3606 {
3607 StackHandleScope<1> hs(self);
3608 HandleWrapper<mirror::ArtField> h_field(hs.NewHandleWrapper(&field));
3609 field_type_class = FieldHelper(h_field).GetType(can_load_classes_);
3610 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003611 if (field_type_class != nullptr) {
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07003612 field_type = &reg_types_.FromClass(field->GetTypeDescriptor(), field_type_class,
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003613 field_type_class->CannotBeAssignedFromOtherTypes());
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08003614 } else {
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08003615 DCHECK(!can_load_classes_ || self->IsExceptionPending());
3616 self->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003617 }
Ian Rogers0d604842012-04-16 14:50:24 -07003618 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003619 if (field_type == nullptr) {
3620 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3621 const char* descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003622 field_type = &reg_types_.FromDescriptor(class_loader_->Get(), descriptor, false);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003623 }
Sebastien Hertz757b3042014-03-28 14:34:28 +01003624 DCHECK(field_type != nullptr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02003625 const uint32_t vregA = (is_static) ? inst->VRegA_21c() : inst->VRegA_22c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07003626 if (is_primitive) {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003627 if (field_type->Equals(insn_type) ||
3628 (field_type->IsFloat() && insn_type.IsInteger()) ||
3629 (field_type->IsDouble() && insn_type.IsLong())) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003630 // expected that read is of the correct primitive type or that int reads are reading
3631 // floats or long reads are reading doubles
3632 } else {
3633 // This is a global failure rather than a class change failure as the instructions and
3634 // the descriptors for the type should have been consistent within the same file at
3635 // compile time
3636 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
3637 << " to be of type '" << insn_type
Sebastien Hertz757b3042014-03-28 14:34:28 +01003638 << "' but found type '" << *field_type << "' in get";
Ian Rogersad0b3a32012-04-16 14:50:24 -07003639 return;
3640 }
3641 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003642 if (!insn_type.IsAssignableFrom(*field_type)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003643 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
3644 << " to be compatible with type '" << insn_type
Sebastien Hertz757b3042014-03-28 14:34:28 +01003645 << "' but found type '" << *field_type
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003646 << "' in Get-object";
Sebastien Hertz5243e912013-05-21 10:55:07 +02003647 work_line_->SetRegisterType(vregA, reg_types_.Conflict());
Ian Rogersad0b3a32012-04-16 14:50:24 -07003648 return;
3649 }
3650 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003651 if (!field_type->IsLowHalf()) {
3652 work_line_->SetRegisterType(vregA, *field_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003653 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003654 work_line_->SetRegisterTypeWide(vregA, *field_type, field_type->HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003655 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003656}
3657
Sebastien Hertz5243e912013-05-21 10:55:07 +02003658void MethodVerifier::VerifyISPut(const Instruction* inst, const RegType& insn_type,
3659 bool is_primitive, bool is_static) {
3660 uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
Brian Carlstromea46f952013-07-30 01:26:50 -07003661 mirror::ArtField* field;
Ian Rogersb94a27b2011-10-26 00:33:41 -07003662 if (is_static) {
Ian Rogers55d249f2011-11-02 16:48:09 -07003663 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003664 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003665 const RegType& object_type = work_line_->GetRegisterType(inst->VRegB_22c());
Ian Rogers55d249f2011-11-02 16:48:09 -07003666 field = GetInstanceField(object_type, field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003667 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003668 const RegType* field_type = nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003669 if (field != NULL) {
3670 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
3671 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
3672 << " from other class " << GetDeclaringClass();
3673 return;
3674 }
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07003675 mirror::Class* field_type_class;
3676 {
3677 StackHandleScope<1> hs(Thread::Current());
3678 HandleWrapper<mirror::ArtField> h_field(hs.NewHandleWrapper(&field));
3679 FieldHelper fh(h_field);
3680 field_type_class = fh.GetType(can_load_classes_);
3681 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003682 if (field_type_class != nullptr) {
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07003683 field_type = &reg_types_.FromClass(field->GetTypeDescriptor(), field_type_class,
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003684 field_type_class->CannotBeAssignedFromOtherTypes());
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08003685 } else {
3686 Thread* self = Thread::Current();
3687 DCHECK(!can_load_classes_ || self->IsExceptionPending());
3688 self->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003689 }
3690 }
3691 if (field_type == nullptr) {
3692 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3693 const char* descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003694 field_type = &reg_types_.FromDescriptor(class_loader_->Get(), descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003695 }
Sebastien Hertz757b3042014-03-28 14:34:28 +01003696 DCHECK(field_type != nullptr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02003697 const uint32_t vregA = (is_static) ? inst->VRegA_21c() : inst->VRegA_22c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07003698 if (is_primitive) {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003699 VerifyPrimitivePut(*field_type, insn_type, vregA);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003700 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003701 if (!insn_type.IsAssignableFrom(*field_type)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003702 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
3703 << " to be compatible with type '" << insn_type
Sebastien Hertz757b3042014-03-28 14:34:28 +01003704 << "' but found type '" << *field_type
Ian Rogersad0b3a32012-04-16 14:50:24 -07003705 << "' in put-object";
3706 return;
3707 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003708 work_line_->VerifyRegisterType(vregA, *field_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07003709 }
3710}
3711
Brian Carlstromea46f952013-07-30 01:26:50 -07003712mirror::ArtField* MethodVerifier::GetQuickFieldAccess(const Instruction* inst,
Ian Rogers98379392014-02-24 16:53:16 -08003713 RegisterLine* reg_line) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003714 DCHECK(inst->Opcode() == Instruction::IGET_QUICK ||
3715 inst->Opcode() == Instruction::IGET_WIDE_QUICK ||
3716 inst->Opcode() == Instruction::IGET_OBJECT_QUICK ||
3717 inst->Opcode() == Instruction::IPUT_QUICK ||
3718 inst->Opcode() == Instruction::IPUT_WIDE_QUICK ||
3719 inst->Opcode() == Instruction::IPUT_OBJECT_QUICK);
3720 const RegType& object_type = reg_line->GetRegisterType(inst->VRegB_22c());
Ian Rogers9bc54402014-04-17 16:40:01 -07003721 if (!object_type.HasClass()) {
3722 VLOG(verifier) << "Failed to get mirror::Class* from '" << object_type << "'";
3723 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003724 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003725 uint32_t field_offset = static_cast<uint32_t>(inst->VRegC_22c());
Sebastien Hertz479fc1e2014-04-04 17:51:34 +02003726 mirror::ArtField* f = mirror::ArtField::FindInstanceFieldWithOffset(object_type.GetClass(),
3727 field_offset);
3728 if (f == nullptr) {
3729 VLOG(verifier) << "Failed to find instance field at offset '" << field_offset
3730 << "' from '" << PrettyDescriptor(object_type.GetClass()) << "'";
3731 }
3732 return f;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003733}
3734
3735void MethodVerifier::VerifyIGetQuick(const Instruction* inst, const RegType& insn_type,
3736 bool is_primitive) {
3737 DCHECK(Runtime::Current()->IsStarted());
Brian Carlstromea46f952013-07-30 01:26:50 -07003738 mirror::ArtField* field = GetQuickFieldAccess(inst, work_line_.get());
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003739 if (field == NULL) {
3740 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field from " << inst->Name();
3741 return;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003742 }
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07003743 mirror::Class* field_type_class;
3744 {
3745 StackHandleScope<1> hs(Thread::Current());
3746 HandleWrapper<mirror::ArtField> h_field(hs.NewHandleWrapper(&field));
3747 FieldHelper fh(h_field);
3748 field_type_class = fh.GetType(can_load_classes_);
3749 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003750 const RegType* field_type;
3751 if (field_type_class != nullptr) {
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07003752 field_type = &reg_types_.FromClass(field->GetTypeDescriptor(), field_type_class,
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003753 field_type_class->CannotBeAssignedFromOtherTypes());
3754 } else {
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08003755 Thread* self = Thread::Current();
3756 DCHECK(!can_load_classes_ || self->IsExceptionPending());
3757 self->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003758 field_type = &reg_types_.FromDescriptor(field->GetDeclaringClass()->GetClassLoader(),
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07003759 field->GetTypeDescriptor(), false);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003760 }
Sebastien Hertz757b3042014-03-28 14:34:28 +01003761 DCHECK(field_type != nullptr);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003762 const uint32_t vregA = inst->VRegA_22c();
3763 if (is_primitive) {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003764 if (field_type->Equals(insn_type) ||
3765 (field_type->IsFloat() && insn_type.IsIntegralTypes()) ||
3766 (field_type->IsDouble() && insn_type.IsLongTypes())) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003767 // expected that read is of the correct primitive type or that int reads are reading
3768 // floats or long reads are reading doubles
3769 } else {
3770 // This is a global failure rather than a class change failure as the instructions and
3771 // the descriptors for the type should have been consistent within the same file at
3772 // compile time
3773 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003774 << " to be of type '" << insn_type
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003775 << "' but found type '" << *field_type << "' in Get";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003776 return;
3777 }
3778 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003779 if (!insn_type.IsAssignableFrom(*field_type)) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003780 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003781 << " to be compatible with type '" << insn_type
Sebastien Hertz757b3042014-03-28 14:34:28 +01003782 << "' but found type '" << *field_type
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003783 << "' in get-object";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003784 work_line_->SetRegisterType(vregA, reg_types_.Conflict());
3785 return;
3786 }
3787 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003788 if (!field_type->IsLowHalf()) {
3789 work_line_->SetRegisterType(vregA, *field_type);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003790 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003791 work_line_->SetRegisterTypeWide(vregA, *field_type, field_type->HighHalf(&reg_types_));
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003792 }
3793}
3794
3795void MethodVerifier::VerifyIPutQuick(const Instruction* inst, const RegType& insn_type,
3796 bool is_primitive) {
3797 DCHECK(Runtime::Current()->IsStarted());
Brian Carlstromea46f952013-07-30 01:26:50 -07003798 mirror::ArtField* field = GetQuickFieldAccess(inst, work_line_.get());
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003799 if (field == NULL) {
3800 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field from " << inst->Name();
3801 return;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003802 }
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07003803 const char* descriptor = field->GetTypeDescriptor();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003804 mirror::ClassLoader* loader = field->GetDeclaringClass()->GetClassLoader();
3805 const RegType& field_type = reg_types_.FromDescriptor(loader, descriptor, false);
3806 if (field != NULL) {
3807 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
3808 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003809 << " from other class " << GetDeclaringClass();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003810 return;
3811 }
3812 }
3813 const uint32_t vregA = inst->VRegA_22c();
3814 if (is_primitive) {
3815 // Primitive field assignability rules are weaker than regular assignability rules
3816 bool instruction_compatible;
3817 bool value_compatible;
3818 const RegType& value_type = work_line_->GetRegisterType(vregA);
3819 if (field_type.IsIntegralTypes()) {
3820 instruction_compatible = insn_type.IsIntegralTypes();
3821 value_compatible = value_type.IsIntegralTypes();
3822 } else if (field_type.IsFloat()) {
3823 instruction_compatible = insn_type.IsInteger(); // no [is]put-float, so expect [is]put-int
3824 value_compatible = value_type.IsFloatTypes();
3825 } else if (field_type.IsLong()) {
3826 instruction_compatible = insn_type.IsLong();
3827 value_compatible = value_type.IsLongTypes();
3828 } else if (field_type.IsDouble()) {
3829 instruction_compatible = insn_type.IsLong(); // no [is]put-double, so expect [is]put-long
3830 value_compatible = value_type.IsDoubleTypes();
3831 } else {
3832 instruction_compatible = false; // reference field with primitive store
3833 value_compatible = false; // unused
3834 }
3835 if (!instruction_compatible) {
3836 // This is a global failure rather than a class change failure as the instructions and
3837 // the descriptors for the type should have been consistent within the same file at
3838 // compile time
3839 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003840 << " to be of type '" << insn_type
3841 << "' but found type '" << field_type
3842 << "' in put";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003843 return;
3844 }
3845 if (!value_compatible) {
3846 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
3847 << " of type " << value_type
3848 << " but expected " << field_type
3849 << " for store to " << PrettyField(field) << " in put";
3850 return;
3851 }
3852 } else {
3853 if (!insn_type.IsAssignableFrom(field_type)) {
3854 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003855 << " to be compatible with type '" << insn_type
3856 << "' but found type '" << field_type
3857 << "' in put-object";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003858 return;
3859 }
3860 work_line_->VerifyRegisterType(vregA, field_type);
3861 }
3862}
3863
Ian Rogers776ac1f2012-04-13 23:36:36 -07003864bool MethodVerifier::CheckNotMoveException(const uint16_t* insns, int insn_idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003865 if ((insns[insn_idx] & 0xff) == Instruction::MOVE_EXCEPTION) {
jeffhaod5347e02012-03-22 17:25:05 -07003866 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-exception";
Ian Rogersd81871c2011-10-03 13:57:23 -07003867 return false;
3868 }
3869 return true;
3870}
3871
Ian Rogers776ac1f2012-04-13 23:36:36 -07003872bool MethodVerifier::UpdateRegisters(uint32_t next_insn, const RegisterLine* merge_line) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003873 bool changed = true;
3874 RegisterLine* target_line = reg_table_.GetLine(next_insn);
3875 if (!insn_flags_[next_insn].IsVisitedOrChanged()) {
jeffhaobdb76512011-09-07 11:43:16 -07003876 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003877 * We haven't processed this instruction before, and we haven't touched the registers here, so
3878 * there's nothing to "merge". Copy the registers over and mark it as changed. (This is the
3879 * only way a register can transition out of "unknown", so this is not just an optimization.)
jeffhaobdb76512011-09-07 11:43:16 -07003880 */
Ian Rogersb8c78592013-07-25 23:52:52 +00003881 if (!insn_flags_[next_insn].IsReturn()) {
3882 target_line->CopyFromLine(merge_line);
3883 } else {
Jeff Haob24b4a72013-07-31 13:47:31 -07003884 // Verify that the monitor stack is empty on return.
3885 if (!merge_line->VerifyMonitorStackEmpty()) {
3886 return false;
3887 }
Ian Rogersb8c78592013-07-25 23:52:52 +00003888 // For returns we only care about the operand to the return, all other registers are dead.
3889 // Initialize them as conflicts so they don't add to GC and deoptimization information.
3890 const Instruction* ret_inst = Instruction::At(code_item_->insns_ + next_insn);
3891 Instruction::Code opcode = ret_inst->Opcode();
3892 if ((opcode == Instruction::RETURN_VOID) || (opcode == Instruction::RETURN_VOID_BARRIER)) {
3893 target_line->MarkAllRegistersAsConflicts();
3894 } else {
3895 target_line->CopyFromLine(merge_line);
3896 if (opcode == Instruction::RETURN_WIDE) {
3897 target_line->MarkAllRegistersAsConflictsExceptWide(ret_inst->VRegA_11x());
3898 } else {
3899 target_line->MarkAllRegistersAsConflictsExcept(ret_inst->VRegA_11x());
3900 }
3901 }
3902 }
jeffhaobdb76512011-09-07 11:43:16 -07003903 } else {
Ian Rogers700a4022014-05-19 16:49:03 -07003904 std::unique_ptr<RegisterLine> copy(gDebugVerify ?
Ian Rogersd0fbd852013-09-24 18:17:04 -07003905 RegisterLine::Create(target_line->NumRegs(), this) :
Brian Carlstrom93c33962013-07-26 10:37:43 -07003906 NULL);
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003907 if (gDebugVerify) {
3908 copy->CopyFromLine(target_line);
3909 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003910 changed = target_line->MergeRegisters(merge_line);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003911 if (have_pending_hard_failure_) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003912 return false;
jeffhaobdb76512011-09-07 11:43:16 -07003913 }
Ian Rogers2c8a8572011-10-24 17:11:36 -07003914 if (gDebugVerify && changed) {
Elliott Hughes398f64b2012-03-26 18:05:48 -07003915 LogVerifyInfo() << "Merging at [" << reinterpret_cast<void*>(work_insn_idx_) << "]"
Elliott Hughesc073b072012-05-24 19:29:17 -07003916 << " to [" << reinterpret_cast<void*>(next_insn) << "]: " << "\n"
3917 << *copy.get() << " MERGE\n"
3918 << *merge_line << " ==\n"
3919 << *target_line << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07003920 }
3921 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003922 if (changed) {
3923 insn_flags_[next_insn].SetChanged();
jeffhaobdb76512011-09-07 11:43:16 -07003924 }
3925 return true;
3926}
3927
Ian Rogers7b3ddd22013-02-21 15:19:52 -08003928InstructionFlags* MethodVerifier::CurrentInsnFlags() {
Ian Rogers776ac1f2012-04-13 23:36:36 -07003929 return &insn_flags_[work_insn_idx_];
3930}
3931
Ian Rogersad0b3a32012-04-16 14:50:24 -07003932const RegType& MethodVerifier::GetMethodReturnType() {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003933 if (return_type_ == nullptr) {
3934 if (mirror_method_ != NULL) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003935 Thread* self = Thread::Current();
3936 StackHandleScope<1> hs(self);
3937 mirror::Class* return_type_class;
3938 {
3939 HandleWrapper<mirror::ArtMethod> h_mirror_method(hs.NewHandleWrapper(&mirror_method_));
3940 return_type_class = MethodHelper(h_mirror_method).GetReturnType(can_load_classes_);
3941 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003942 if (return_type_class != nullptr) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003943 return_type_ = &reg_types_.FromClass(mirror_method_->GetReturnTypeDescriptor(),
3944 return_type_class,
Mathieu Chartier590fee92013-09-13 13:46:47 -07003945 return_type_class->CannotBeAssignedFromOtherTypes());
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003946 } else {
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08003947 DCHECK(!can_load_classes_ || self->IsExceptionPending());
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003948 self->ClearException();
3949 }
3950 }
3951 if (return_type_ == nullptr) {
3952 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
3953 const DexFile::ProtoId& proto_id = dex_file_->GetMethodPrototype(method_id);
3954 uint16_t return_type_idx = proto_id.return_type_idx_;
3955 const char* descriptor = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(return_type_idx));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003956 return_type_ = &reg_types_.FromDescriptor(class_loader_->Get(), descriptor, false);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003957 }
3958 }
3959 return *return_type_;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003960}
3961
3962const RegType& MethodVerifier::GetDeclaringClass() {
Ian Rogers637c65b2013-05-31 11:46:00 -07003963 if (declaring_class_ == NULL) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003964 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
Brian Carlstrom93c33962013-07-26 10:37:43 -07003965 const char* descriptor
3966 = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(method_id.class_idx_));
Ian Rogers637c65b2013-05-31 11:46:00 -07003967 if (mirror_method_ != NULL) {
3968 mirror::Class* klass = mirror_method_->GetDeclaringClass();
Ian Rogers04f94f42013-06-10 15:09:26 -07003969 declaring_class_ = &reg_types_.FromClass(descriptor, klass,
3970 klass->CannotBeAssignedFromOtherTypes());
Ian Rogers637c65b2013-05-31 11:46:00 -07003971 } else {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003972 declaring_class_ = &reg_types_.FromDescriptor(class_loader_->Get(), descriptor, false);
Ian Rogers637c65b2013-05-31 11:46:00 -07003973 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003974 }
Ian Rogers637c65b2013-05-31 11:46:00 -07003975 return *declaring_class_;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003976}
3977
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003978std::vector<int32_t> MethodVerifier::DescribeVRegs(uint32_t dex_pc) {
3979 RegisterLine* line = reg_table_.GetLine(dex_pc);
Sebastien Hertzaa0c00c2014-03-14 17:58:54 +01003980 DCHECK(line != nullptr) << "No register line at DEX pc " << StringPrintf("0x%x", dex_pc);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003981 std::vector<int32_t> result;
3982 for (size_t i = 0; i < line->NumRegs(); ++i) {
3983 const RegType& type = line->GetRegisterType(i);
3984 if (type.IsConstant()) {
3985 result.push_back(type.IsPreciseConstant() ? kConstant : kImpreciseConstant);
3986 result.push_back(type.ConstantValue());
3987 } else if (type.IsConstantLo()) {
3988 result.push_back(type.IsPreciseConstantLo() ? kConstant : kImpreciseConstant);
3989 result.push_back(type.ConstantValueLo());
3990 } else if (type.IsConstantHi()) {
3991 result.push_back(type.IsPreciseConstantHi() ? kConstant : kImpreciseConstant);
3992 result.push_back(type.ConstantValueHi());
3993 } else if (type.IsIntegralTypes()) {
3994 result.push_back(kIntVReg);
3995 result.push_back(0);
3996 } else if (type.IsFloat()) {
3997 result.push_back(kFloatVReg);
3998 result.push_back(0);
3999 } else if (type.IsLong()) {
4000 result.push_back(kLongLoVReg);
4001 result.push_back(0);
4002 result.push_back(kLongHiVReg);
4003 result.push_back(0);
4004 ++i;
4005 } else if (type.IsDouble()) {
4006 result.push_back(kDoubleLoVReg);
4007 result.push_back(0);
4008 result.push_back(kDoubleHiVReg);
4009 result.push_back(0);
4010 ++i;
4011 } else if (type.IsUndefined() || type.IsConflict() || type.IsHighHalf()) {
4012 result.push_back(kUndefined);
4013 result.push_back(0);
4014 } else {
Ian Rogers7b3ddd22013-02-21 15:19:52 -08004015 CHECK(type.IsNonZeroReferenceTypes());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004016 result.push_back(kReferenceVReg);
4017 result.push_back(0);
4018 }
4019 }
4020 return result;
4021}
4022
Sebastien Hertz849600b2013-12-20 10:28:08 +01004023const RegType& MethodVerifier::DetermineCat1Constant(int32_t value, bool precise) {
4024 if (precise) {
4025 // Precise constant type.
4026 return reg_types_.FromCat1Const(value, true);
4027 } else {
4028 // Imprecise constant type.
4029 if (value < -32768) {
4030 return reg_types_.IntConstant();
4031 } else if (value < -128) {
4032 return reg_types_.ShortConstant();
4033 } else if (value < 0) {
4034 return reg_types_.ByteConstant();
4035 } else if (value == 0) {
4036 return reg_types_.Zero();
4037 } else if (value == 1) {
4038 return reg_types_.One();
4039 } else if (value < 128) {
4040 return reg_types_.PosByteConstant();
4041 } else if (value < 32768) {
4042 return reg_types_.PosShortConstant();
4043 } else if (value < 65536) {
4044 return reg_types_.CharConstant();
4045 } else {
4046 return reg_types_.IntConstant();
4047 }
4048 }
4049}
4050
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004051void MethodVerifier::Init() {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08004052 art::verifier::RegTypeCache::Init();
Brian Carlstrome7d856b2012-01-11 18:10:55 -08004053}
4054
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004055void MethodVerifier::Shutdown() {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08004056 verifier::RegTypeCache::ShutDown();
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08004057}
jeffhaod1224c72012-02-29 13:43:08 -08004058
Mathieu Chartier83c8ee02014-01-28 14:50:23 -08004059void MethodVerifier::VisitRoots(RootCallback* callback, void* arg) {
4060 reg_types_.VisitRoots(callback, arg);
Mathieu Chartierc528dba2013-11-26 12:00:11 -08004061}
4062
Ian Rogersd81871c2011-10-03 13:57:23 -07004063} // namespace verifier
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07004064} // namespace art