blob: 4863b8367ba01c61e365101b283a872bed2b5ae2 [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 Chartiereb8167a2014-05-07 15:43:14 -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 =
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800154 linker->ResolveMethod(*dex_file, method_idx, dex_cache, class_loader, NULL, type);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700155 if (method == NULL) {
156 DCHECK(Thread::Current()->IsExceptionPending());
157 // We couldn't resolve the method, but continue regardless.
158 Thread::Current()->ClearException();
159 }
Brian Carlstrom93c33962013-07-26 10:37:43 -0700160 MethodVerifier::FailureKind result = VerifyMethod(method_idx,
161 dex_file,
162 dex_cache,
163 class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700164 class_def,
Brian Carlstrom93c33962013-07-26 10:37:43 -0700165 it.GetMethodCodeItem(),
166 method,
167 it.GetMemberAccessFlags(),
168 allow_soft_failures);
jeffhaof1e6b7c2012-06-05 18:33:30 -0700169 if (result != kNoFailure) {
170 if (result == kHardFailure) {
171 hard_fail = true;
172 if (error_count > 0) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700173 *error += "\n";
jeffhaof1e6b7c2012-06-05 18:33:30 -0700174 }
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700175 *error = "Verifier rejected class ";
176 *error += PrettyDescriptor(dex_file->GetClassDescriptor(*class_def));
177 *error += " due to bad method ";
178 *error += PrettyMethod(method_idx, *dex_file);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700179 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700180 ++error_count;
jeffhaof56197c2012-03-05 18:01:54 -0800181 }
182 it.Next();
183 }
jeffhao9b0b1882012-10-01 16:51:22 -0700184 int64_t previous_virtual_method_idx = -1;
jeffhaof56197c2012-03-05 18:01:54 -0800185 while (it.HasNextVirtualMethod()) {
186 uint32_t method_idx = it.GetMemberIndex();
jeffhao9b0b1882012-10-01 16:51:22 -0700187 if (method_idx == previous_virtual_method_idx) {
188 // smali can create dex files with two encoded_methods sharing the same method_idx
189 // http://code.google.com/p/smali/issues/detail?id=119
190 it.Next();
191 continue;
192 }
193 previous_virtual_method_idx = method_idx;
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700194 InvokeType type = it.GetMethodInvokeType(*class_def);
Brian Carlstromea46f952013-07-30 01:26:50 -0700195 mirror::ArtMethod* method =
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800196 linker->ResolveMethod(*dex_file, method_idx, dex_cache, class_loader, NULL, type);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700197 if (method == NULL) {
198 DCHECK(Thread::Current()->IsExceptionPending());
199 // We couldn't resolve the method, but continue regardless.
200 Thread::Current()->ClearException();
201 }
Brian Carlstrom93c33962013-07-26 10:37:43 -0700202 MethodVerifier::FailureKind result = VerifyMethod(method_idx,
203 dex_file,
204 dex_cache,
205 class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700206 class_def,
Brian Carlstrom93c33962013-07-26 10:37:43 -0700207 it.GetMethodCodeItem(),
208 method,
209 it.GetMemberAccessFlags(),
210 allow_soft_failures);
jeffhaof1e6b7c2012-06-05 18:33:30 -0700211 if (result != kNoFailure) {
212 if (result == kHardFailure) {
213 hard_fail = true;
214 if (error_count > 0) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700215 *error += "\n";
jeffhaof1e6b7c2012-06-05 18:33:30 -0700216 }
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700217 *error = "Verifier rejected class ";
218 *error += PrettyDescriptor(dex_file->GetClassDescriptor(*class_def));
219 *error += " due to bad method ";
220 *error += PrettyMethod(method_idx, *dex_file);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700221 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700222 ++error_count;
jeffhaof56197c2012-03-05 18:01:54 -0800223 }
224 it.Next();
225 }
jeffhaof1e6b7c2012-06-05 18:33:30 -0700226 if (error_count == 0) {
227 return kNoFailure;
228 } else {
229 return hard_fail ? kHardFailure : kSoftFailure;
230 }
jeffhaof56197c2012-03-05 18:01:54 -0800231}
232
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800233MethodVerifier::FailureKind MethodVerifier::VerifyMethod(uint32_t method_idx,
234 const DexFile* dex_file,
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700235 Handle<mirror::DexCache>& dex_cache,
236 Handle<mirror::ClassLoader>& class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700237 const DexFile::ClassDef* class_def,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800238 const DexFile::CodeItem* code_item,
Brian Carlstromea46f952013-07-30 01:26:50 -0700239 mirror::ArtMethod* method,
Jeff Haoee988952013-04-16 14:23:47 -0700240 uint32_t method_access_flags,
241 bool allow_soft_failures) {
Ian Rogersc8982582012-09-07 16:53:25 -0700242 MethodVerifier::FailureKind result = kNoFailure;
243 uint64_t start_ns = NanoTime();
244
Mathieu Chartier590fee92013-09-13 13:46:47 -0700245 MethodVerifier verifier_(dex_file, &dex_cache, &class_loader, class_def, code_item,
246 method_idx, method, method_access_flags, true, allow_soft_failures);
Anwar Ghuloum75a43f12013-08-13 17:22:14 -0700247 if (verifier_.Verify()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700248 // Verification completed, however failures may be pending that didn't cause the verification
249 // to hard fail.
Anwar Ghuloum75a43f12013-08-13 17:22:14 -0700250 CHECK(!verifier_.have_pending_hard_failure_);
251 if (verifier_.failures_.size() != 0) {
252 if (VLOG_IS_ON(verifier)) {
253 verifier_.DumpFailures(VLOG_STREAM(verifier) << "Soft verification failures in "
254 << PrettyMethod(method_idx, *dex_file) << "\n");
255 }
Ian Rogersc8982582012-09-07 16:53:25 -0700256 result = kSoftFailure;
jeffhaof56197c2012-03-05 18:01:54 -0800257 }
258 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700259 // Bad method data.
Anwar Ghuloum75a43f12013-08-13 17:22:14 -0700260 CHECK_NE(verifier_.failures_.size(), 0U);
261 CHECK(verifier_.have_pending_hard_failure_);
262 verifier_.DumpFailures(LOG(INFO) << "Verification error in "
Elliott Hughesc073b072012-05-24 19:29:17 -0700263 << PrettyMethod(method_idx, *dex_file) << "\n");
jeffhaof56197c2012-03-05 18:01:54 -0800264 if (gDebugVerify) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -0700265 std::cout << "\n" << verifier_.info_messages_.str();
266 verifier_.Dump(std::cout);
jeffhaof56197c2012-03-05 18:01:54 -0800267 }
Ian Rogersc8982582012-09-07 16:53:25 -0700268 result = kHardFailure;
jeffhaof56197c2012-03-05 18:01:54 -0800269 }
Ian Rogersc8982582012-09-07 16:53:25 -0700270 uint64_t duration_ns = NanoTime() - start_ns;
271 if (duration_ns > MsToNs(100)) {
272 LOG(WARNING) << "Verification of " << PrettyMethod(method_idx, *dex_file)
273 << " took " << PrettyDuration(duration_ns);
274 }
275 return result;
jeffhaof56197c2012-03-05 18:01:54 -0800276}
277
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800278void MethodVerifier::VerifyMethodAndDump(std::ostream& os, uint32_t dex_method_idx,
Mathieu Chartier590fee92013-09-13 13:46:47 -0700279 const DexFile* dex_file,
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700280 Handle<mirror::DexCache>& dex_cache,
281 Handle<mirror::ClassLoader>& class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700282 const DexFile::ClassDef* class_def,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800283 const DexFile::CodeItem* code_item,
Brian Carlstromea46f952013-07-30 01:26:50 -0700284 mirror::ArtMethod* method,
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800285 uint32_t method_access_flags) {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700286 MethodVerifier verifier(dex_file, &dex_cache, &class_loader, class_def, code_item,
Jeff Haoee988952013-04-16 14:23:47 -0700287 dex_method_idx, method, method_access_flags, true, true);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700288 verifier.Verify();
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800289 verifier.DumpFailures(os);
290 os << verifier.info_messages_.str();
291 verifier.Dump(os);
292}
293
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700294MethodVerifier::MethodVerifier(const DexFile* dex_file, Handle<mirror::DexCache>* dex_cache,
295 Handle<mirror::ClassLoader>* class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700296 const DexFile::ClassDef* class_def,
Mathieu Chartier590fee92013-09-13 13:46:47 -0700297 const DexFile::CodeItem* code_item, uint32_t dex_method_idx,
298 mirror::ArtMethod* method, uint32_t method_access_flags,
299 bool can_load_classes, bool allow_soft_failures)
Elliott Hughes80537bb2013-01-04 16:37:26 -0800300 : reg_types_(can_load_classes),
301 work_insn_idx_(-1),
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800302 dex_method_idx_(dex_method_idx),
Ian Rogers637c65b2013-05-31 11:46:00 -0700303 mirror_method_(method),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700304 method_access_flags_(method_access_flags),
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700305 return_type_(nullptr),
jeffhaof56197c2012-03-05 18:01:54 -0800306 dex_file_(dex_file),
307 dex_cache_(dex_cache),
308 class_loader_(class_loader),
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700309 class_def_(class_def),
jeffhaof56197c2012-03-05 18:01:54 -0800310 code_item_(code_item),
Ian Rogers637c65b2013-05-31 11:46:00 -0700311 declaring_class_(NULL),
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700312 interesting_dex_pc_(-1),
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700313 monitor_enter_dex_pcs_(nullptr),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700314 have_pending_hard_failure_(false),
jeffhaofaf459e2012-08-31 15:32:47 -0700315 have_pending_runtime_throw_failure_(false),
jeffhaof56197c2012-03-05 18:01:54 -0800316 new_instance_count_(0),
Elliott Hughes80537bb2013-01-04 16:37:26 -0800317 monitor_enter_count_(0),
Jeff Haoee988952013-04-16 14:23:47 -0700318 can_load_classes_(can_load_classes),
Sebastien Hertz4d4adb12013-07-24 16:14:19 +0200319 allow_soft_failures_(allow_soft_failures),
320 has_check_casts_(false),
321 has_virtual_or_interface_invokes_(false) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800322 Runtime::Current()->AddMethodVerifier(this);
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700323 DCHECK(class_def != nullptr);
jeffhaof56197c2012-03-05 18:01:54 -0800324}
325
Mathieu Chartier590fee92013-09-13 13:46:47 -0700326MethodVerifier::~MethodVerifier() {
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800327 Runtime::Current()->RemoveMethodVerifier(this);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700328 STLDeleteElements(&failure_messages_);
329}
330
Brian Carlstromea46f952013-07-30 01:26:50 -0700331void MethodVerifier::FindLocksAtDexPc(mirror::ArtMethod* m, uint32_t dex_pc,
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800332 std::vector<uint32_t>& monitor_enter_dex_pcs) {
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700333 MethodHelper mh(m);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700334 StackHandleScope<2> hs(Thread::Current());
335 Handle<mirror::DexCache> dex_cache(hs.NewHandle(mh.GetDexCache()));
336 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(mh.GetClassLoader()));
Mathieu Chartier590fee92013-09-13 13:46:47 -0700337 MethodVerifier verifier(&mh.GetDexFile(), &dex_cache, &class_loader, &mh.GetClassDef(),
338 mh.GetCodeItem(), m->GetDexMethodIndex(), m, m->GetAccessFlags(), false,
339 true);
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700340 verifier.interesting_dex_pc_ = dex_pc;
341 verifier.monitor_enter_dex_pcs_ = &monitor_enter_dex_pcs;
342 verifier.FindLocksAtDexPc();
343}
344
345void MethodVerifier::FindLocksAtDexPc() {
346 CHECK(monitor_enter_dex_pcs_ != NULL);
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700347 CHECK(code_item_ != NULL); // This only makes sense for methods with code.
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700348
349 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
350 // verification. In practice, the phase we want relies on data structures set up by all the
351 // earlier passes, so we just run the full method verification and bail out early when we've
352 // got what we wanted.
353 Verify();
354}
355
Brian Carlstromea46f952013-07-30 01:26:50 -0700356mirror::ArtField* MethodVerifier::FindAccessedFieldAtDexPc(mirror::ArtMethod* m,
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200357 uint32_t dex_pc) {
358 MethodHelper mh(m);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700359 StackHandleScope<2> hs(Thread::Current());
360 Handle<mirror::DexCache> dex_cache(hs.NewHandle(mh.GetDexCache()));
361 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(mh.GetClassLoader()));
Mathieu Chartier590fee92013-09-13 13:46:47 -0700362 MethodVerifier verifier(&mh.GetDexFile(), &dex_cache, &class_loader, &mh.GetClassDef(),
Ian Rogers9bc54402014-04-17 16:40:01 -0700363 mh.GetCodeItem(), m->GetDexMethodIndex(), m, m->GetAccessFlags(), true,
Mathieu Chartier590fee92013-09-13 13:46:47 -0700364 true);
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200365 return verifier.FindAccessedFieldAtDexPc(dex_pc);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200366}
367
Brian Carlstromea46f952013-07-30 01:26:50 -0700368mirror::ArtField* MethodVerifier::FindAccessedFieldAtDexPc(uint32_t dex_pc) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700369 CHECK(code_item_ != NULL); // This only makes sense for methods with code.
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200370
371 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
372 // verification. In practice, the phase we want relies on data structures set up by all the
373 // earlier passes, so we just run the full method verification and bail out early when we've
374 // got what we wanted.
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200375 bool success = Verify();
376 if (!success) {
Ian Rogers9bc54402014-04-17 16:40:01 -0700377 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200378 }
379 RegisterLine* register_line = reg_table_.GetLine(dex_pc);
380 if (register_line == NULL) {
Ian Rogers9bc54402014-04-17 16:40:01 -0700381 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200382 }
383 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
384 return GetQuickFieldAccess(inst, register_line);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200385}
386
Brian Carlstromea46f952013-07-30 01:26:50 -0700387mirror::ArtMethod* MethodVerifier::FindInvokedMethodAtDexPc(mirror::ArtMethod* m,
Mathieu Chartier590fee92013-09-13 13:46:47 -0700388 uint32_t dex_pc) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200389 MethodHelper mh(m);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700390 StackHandleScope<2> hs(Thread::Current());
391 Handle<mirror::DexCache> dex_cache(hs.NewHandle(mh.GetDexCache()));
392 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(mh.GetClassLoader()));
Mathieu Chartier590fee92013-09-13 13:46:47 -0700393 MethodVerifier verifier(&mh.GetDexFile(), &dex_cache, &class_loader, &mh.GetClassDef(),
Andreas Gampe63981562014-04-17 12:28:43 -0700394 mh.GetCodeItem(), m->GetDexMethodIndex(), m, m->GetAccessFlags(), true,
Mathieu Chartier590fee92013-09-13 13:46:47 -0700395 true);
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200396 return verifier.FindInvokedMethodAtDexPc(dex_pc);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200397}
398
Brian Carlstromea46f952013-07-30 01:26:50 -0700399mirror::ArtMethod* MethodVerifier::FindInvokedMethodAtDexPc(uint32_t dex_pc) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700400 CHECK(code_item_ != NULL); // This only makes sense for methods with code.
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200401
402 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
403 // verification. In practice, the phase we want relies on data structures set up by all the
404 // earlier passes, so we just run the full method verification and bail out early when we've
405 // got what we wanted.
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200406 bool success = Verify();
407 if (!success) {
408 return NULL;
409 }
410 RegisterLine* register_line = reg_table_.GetLine(dex_pc);
411 if (register_line == NULL) {
412 return NULL;
413 }
414 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
415 const bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
416 return GetQuickInvokedMethod(inst, register_line, is_range);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200417}
418
Ian Rogersad0b3a32012-04-16 14:50:24 -0700419bool MethodVerifier::Verify() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700420 // If there aren't any instructions, make sure that's expected, then exit successfully.
421 if (code_item_ == NULL) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700422 if ((method_access_flags_ & (kAccNative | kAccAbstract)) == 0) {
jeffhaod5347e02012-03-22 17:25:05 -0700423 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "zero-length code in concrete non-native method";
jeffhaobdb76512011-09-07 11:43:16 -0700424 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -0700425 } else {
426 return true;
jeffhaobdb76512011-09-07 11:43:16 -0700427 }
jeffhaobdb76512011-09-07 11:43:16 -0700428 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700429 // Sanity-check the register counts. ins + locals = registers, so make sure that ins <= registers.
430 if (code_item_->ins_size_ > code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700431 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad register counts (ins=" << code_item_->ins_size_
432 << " regs=" << code_item_->registers_size_;
Ian Rogersd81871c2011-10-03 13:57:23 -0700433 return false;
jeffhaobdb76512011-09-07 11:43:16 -0700434 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700435 // Allocate and initialize an array to hold instruction data.
Ian Rogers7b3ddd22013-02-21 15:19:52 -0800436 insn_flags_.reset(new InstructionFlags[code_item_->insns_size_in_code_units_]());
Ian Rogersd81871c2011-10-03 13:57:23 -0700437 // Run through the instructions and see if the width checks out.
438 bool result = ComputeWidthsAndCountOps();
439 // Flag instructions guarded by a "try" block and check exception handlers.
440 result = result && ScanTryCatchBlocks();
441 // Perform static instruction verification.
442 result = result && VerifyInstructions();
Ian Rogersad0b3a32012-04-16 14:50:24 -0700443 // Perform code-flow analysis and return.
Vladimir Marko2b5eaa22013-12-13 13:59:30 +0000444 result = result && VerifyCodeFlow();
445 // Compute information for compiler.
446 if (result && Runtime::Current()->IsCompiler()) {
447 result = Runtime::Current()->GetCompilerCallbacks()->MethodVerified(this);
448 }
449 return result;
jeffhaoba5ebb92011-08-25 17:24:37 -0700450}
451
Ian Rogers776ac1f2012-04-13 23:36:36 -0700452std::ostream& MethodVerifier::Fail(VerifyError error) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700453 switch (error) {
454 case VERIFY_ERROR_NO_CLASS:
455 case VERIFY_ERROR_NO_FIELD:
456 case VERIFY_ERROR_NO_METHOD:
457 case VERIFY_ERROR_ACCESS_CLASS:
458 case VERIFY_ERROR_ACCESS_FIELD:
459 case VERIFY_ERROR_ACCESS_METHOD:
Ian Rogers08f753d2012-08-24 14:35:25 -0700460 case VERIFY_ERROR_INSTANTIATION:
461 case VERIFY_ERROR_CLASS_CHANGE:
Elliott Hughes4993bbc2013-01-10 15:41:25 -0800462 if (Runtime::Current()->IsCompiler() || !can_load_classes_) {
jeffhaofaf459e2012-08-31 15:32:47 -0700463 // If we're optimistically running verification at compile time, turn NO_xxx, ACCESS_xxx,
464 // class change and instantiation errors into soft verification errors so that we re-verify
465 // at runtime. We may fail to find or to agree on access because of not yet available class
466 // loaders, or class loaders that will differ at runtime. In these cases, we don't want to
467 // affect the soundness of the code being compiled. Instead, the generated code runs "slow
468 // paths" that dynamically perform the verification and cause the behavior to be that akin
469 // to an interpreter.
470 error = VERIFY_ERROR_BAD_CLASS_SOFT;
471 } else {
Jeff Haoa3faaf42013-09-03 19:07:00 -0700472 // If we fail again at runtime, mark that this instruction would throw and force this
473 // method to be executed using the interpreter with checks.
jeffhaofaf459e2012-08-31 15:32:47 -0700474 have_pending_runtime_throw_failure_ = true;
475 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700476 break;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700477 // Indication that verification should be retried at runtime.
478 case VERIFY_ERROR_BAD_CLASS_SOFT:
Jeff Haoee988952013-04-16 14:23:47 -0700479 if (!allow_soft_failures_) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700480 have_pending_hard_failure_ = true;
481 }
482 break;
jeffhaod5347e02012-03-22 17:25:05 -0700483 // Hard verification failures at compile time will still fail at runtime, so the class is
484 // marked as rejected to prevent it from being compiled.
Ian Rogersad0b3a32012-04-16 14:50:24 -0700485 case VERIFY_ERROR_BAD_CLASS_HARD: {
486 if (Runtime::Current()->IsCompiler()) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700487 ClassReference ref(dex_file_, dex_file_->GetIndexForClassDef(*class_def_));
Vladimir Marko2b5eaa22013-12-13 13:59:30 +0000488 Runtime::Current()->GetCompilerCallbacks()->ClassRejected(ref);
jeffhaod1224c72012-02-29 13:43:08 -0800489 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700490 have_pending_hard_failure_ = true;
491 break;
Ian Rogers47a05882012-02-03 12:23:33 -0800492 }
493 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700494 failures_.push_back(error);
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800495 std::string location(StringPrintf("%s: [0x%X]", PrettyMethod(dex_method_idx_, *dex_file_).c_str(),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700496 work_insn_idx_));
497 std::ostringstream* failure_message = new std::ostringstream(location);
498 failure_messages_.push_back(failure_message);
499 return *failure_message;
500}
501
502void MethodVerifier::PrependToLastFailMessage(std::string prepend) {
503 size_t failure_num = failure_messages_.size();
504 DCHECK_NE(failure_num, 0U);
505 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
506 prepend += last_fail_message->str();
507 failure_messages_[failure_num - 1] = new std::ostringstream(prepend);
508 delete last_fail_message;
509}
510
511void MethodVerifier::AppendToLastFailMessage(std::string append) {
512 size_t failure_num = failure_messages_.size();
513 DCHECK_NE(failure_num, 0U);
514 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
515 (*last_fail_message) << append;
Ian Rogers47a05882012-02-03 12:23:33 -0800516}
517
Ian Rogers776ac1f2012-04-13 23:36:36 -0700518bool MethodVerifier::ComputeWidthsAndCountOps() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700519 const uint16_t* insns = code_item_->insns_;
520 size_t insns_size = code_item_->insns_size_in_code_units_;
521 const Instruction* inst = Instruction::At(insns);
jeffhaobdb76512011-09-07 11:43:16 -0700522 size_t new_instance_count = 0;
523 size_t monitor_enter_count = 0;
Ian Rogersd81871c2011-10-03 13:57:23 -0700524 size_t dex_pc = 0;
jeffhaobdb76512011-09-07 11:43:16 -0700525
Ian Rogersd81871c2011-10-03 13:57:23 -0700526 while (dex_pc < insns_size) {
jeffhaobdb76512011-09-07 11:43:16 -0700527 Instruction::Code opcode = inst->Opcode();
Ian Rogersa9a82542013-10-04 11:17:26 -0700528 switch (opcode) {
529 case Instruction::APUT_OBJECT:
530 case Instruction::CHECK_CAST:
531 has_check_casts_ = true;
532 break;
533 case Instruction::INVOKE_VIRTUAL:
534 case Instruction::INVOKE_VIRTUAL_RANGE:
535 case Instruction::INVOKE_INTERFACE:
536 case Instruction::INVOKE_INTERFACE_RANGE:
537 has_virtual_or_interface_invokes_ = true;
538 break;
539 case Instruction::MONITOR_ENTER:
540 monitor_enter_count++;
541 break;
542 case Instruction::NEW_INSTANCE:
543 new_instance_count++;
544 break;
545 default:
546 break;
jeffhaobdb76512011-09-07 11:43:16 -0700547 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700548 size_t inst_size = inst->SizeInCodeUnits();
549 insn_flags_[dex_pc].SetLengthInCodeUnits(inst_size);
550 dex_pc += inst_size;
jeffhaobdb76512011-09-07 11:43:16 -0700551 inst = inst->Next();
552 }
553
Ian Rogersd81871c2011-10-03 13:57:23 -0700554 if (dex_pc != insns_size) {
jeffhaod5347e02012-03-22 17:25:05 -0700555 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "code did not end where expected ("
556 << dex_pc << " vs. " << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700557 return false;
558 }
559
Ian Rogersd81871c2011-10-03 13:57:23 -0700560 new_instance_count_ = new_instance_count;
561 monitor_enter_count_ = monitor_enter_count;
jeffhaobdb76512011-09-07 11:43:16 -0700562 return true;
563}
564
Ian Rogers776ac1f2012-04-13 23:36:36 -0700565bool MethodVerifier::ScanTryCatchBlocks() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700566 uint32_t tries_size = code_item_->tries_size_;
jeffhaobdb76512011-09-07 11:43:16 -0700567 if (tries_size == 0) {
568 return true;
569 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700570 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Ian Rogers0571d352011-11-03 19:51:38 -0700571 const DexFile::TryItem* tries = DexFile::GetTryItems(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -0700572
573 for (uint32_t idx = 0; idx < tries_size; idx++) {
574 const DexFile::TryItem* try_item = &tries[idx];
575 uint32_t start = try_item->start_addr_;
576 uint32_t end = start + try_item->insn_count_;
jeffhaobdb76512011-09-07 11:43:16 -0700577 if ((start >= end) || (start >= insns_size) || (end > insns_size)) {
jeffhaod5347e02012-03-22 17:25:05 -0700578 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad exception entry: startAddr=" << start
579 << " endAddr=" << end << " (size=" << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700580 return false;
581 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700582 if (!insn_flags_[start].IsOpcode()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700583 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
584 << "'try' block starts inside an instruction (" << start << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700585 return false;
586 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700587 for (uint32_t dex_pc = start; dex_pc < end;
588 dex_pc += insn_flags_[dex_pc].GetLengthInCodeUnits()) {
589 insn_flags_[dex_pc].SetInTry();
jeffhaobdb76512011-09-07 11:43:16 -0700590 }
591 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800592 // Iterate over each of the handlers to verify target addresses.
Ian Rogers0571d352011-11-03 19:51:38 -0700593 const byte* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -0700594 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700595 ClassLinker* linker = Runtime::Current()->GetClassLinker();
jeffhaobdb76512011-09-07 11:43:16 -0700596 for (uint32_t idx = 0; idx < handlers_size; idx++) {
Ian Rogers0571d352011-11-03 19:51:38 -0700597 CatchHandlerIterator iterator(handlers_ptr);
598 for (; iterator.HasNext(); iterator.Next()) {
599 uint32_t dex_pc= iterator.GetHandlerAddress();
Ian Rogersd81871c2011-10-03 13:57:23 -0700600 if (!insn_flags_[dex_pc].IsOpcode()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700601 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
602 << "exception handler starts at bad address (" << dex_pc << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700603 return false;
604 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700605 insn_flags_[dex_pc].SetBranchTarget();
Ian Rogers28ad40d2011-10-27 15:19:26 -0700606 // Ensure exception types are resolved so that they don't need resolution to be delivered,
607 // unresolved exception types will be ignored by exception delivery
Ian Rogers0571d352011-11-03 19:51:38 -0700608 if (iterator.GetHandlerTypeIndex() != DexFile::kDexNoIndex16) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800609 mirror::Class* exception_type = linker->ResolveType(*dex_file_,
610 iterator.GetHandlerTypeIndex(),
Mathieu Chartier590fee92013-09-13 13:46:47 -0700611 *dex_cache_, *class_loader_);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700612 if (exception_type == NULL) {
613 DCHECK(Thread::Current()->IsExceptionPending());
614 Thread::Current()->ClearException();
615 }
616 }
jeffhaobdb76512011-09-07 11:43:16 -0700617 }
Ian Rogers0571d352011-11-03 19:51:38 -0700618 handlers_ptr = iterator.EndDataPointer();
jeffhaobdb76512011-09-07 11:43:16 -0700619 }
jeffhaobdb76512011-09-07 11:43:16 -0700620 return true;
621}
622
Ian Rogers776ac1f2012-04-13 23:36:36 -0700623bool MethodVerifier::VerifyInstructions() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700624 const Instruction* inst = Instruction::At(code_item_->insns_);
jeffhaoba5ebb92011-08-25 17:24:37 -0700625
Ian Rogers0c7abda2012-09-19 13:33:42 -0700626 /* 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 -0700627 insn_flags_[0].SetBranchTarget();
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700628 insn_flags_[0].SetCompileTimeInfoPoint();
Ian Rogersd81871c2011-10-03 13:57:23 -0700629
630 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700631 for (uint32_t dex_pc = 0; dex_pc < insns_size;) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700632 if (!VerifyInstruction(inst, dex_pc)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700633 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -0700634 return false;
635 }
636 /* Flag instructions that are garbage collection points */
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700637 // All invoke points are marked as "Throw" points already.
638 // We are relying on this to also count all the invokes as interesting.
Ian Rogersb8c78592013-07-25 23:52:52 +0000639 if (inst->IsBranch() || inst->IsSwitch() || inst->IsThrow()) {
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700640 insn_flags_[dex_pc].SetCompileTimeInfoPoint();
Ian Rogersb8c78592013-07-25 23:52:52 +0000641 } else if (inst->IsReturn()) {
642 insn_flags_[dex_pc].SetCompileTimeInfoPointAndReturn();
Ian Rogersd81871c2011-10-03 13:57:23 -0700643 }
644 dex_pc += inst->SizeInCodeUnits();
645 inst = inst->Next();
646 }
647 return true;
648}
649
Ian Rogers776ac1f2012-04-13 23:36:36 -0700650bool MethodVerifier::VerifyInstruction(const Instruction* inst, uint32_t code_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700651 bool result = true;
652 switch (inst->GetVerifyTypeArgumentA()) {
653 case Instruction::kVerifyRegA:
Ian Rogers29a26482014-05-02 15:27:29 -0700654 result = result && CheckRegisterIndex(inst->VRegA());
Ian Rogersd81871c2011-10-03 13:57:23 -0700655 break;
656 case Instruction::kVerifyRegAWide:
Ian Rogers29a26482014-05-02 15:27:29 -0700657 result = result && CheckWideRegisterIndex(inst->VRegA());
Ian Rogersd81871c2011-10-03 13:57:23 -0700658 break;
659 }
660 switch (inst->GetVerifyTypeArgumentB()) {
661 case Instruction::kVerifyRegB:
Ian Rogers29a26482014-05-02 15:27:29 -0700662 result = result && CheckRegisterIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700663 break;
664 case Instruction::kVerifyRegBField:
Ian Rogers29a26482014-05-02 15:27:29 -0700665 result = result && CheckFieldIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700666 break;
667 case Instruction::kVerifyRegBMethod:
Ian Rogers29a26482014-05-02 15:27:29 -0700668 result = result && CheckMethodIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700669 break;
670 case Instruction::kVerifyRegBNewInstance:
Ian Rogers29a26482014-05-02 15:27:29 -0700671 result = result && CheckNewInstance(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700672 break;
673 case Instruction::kVerifyRegBString:
Ian Rogers29a26482014-05-02 15:27:29 -0700674 result = result && CheckStringIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700675 break;
676 case Instruction::kVerifyRegBType:
Ian Rogers29a26482014-05-02 15:27:29 -0700677 result = result && CheckTypeIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700678 break;
679 case Instruction::kVerifyRegBWide:
Ian Rogers29a26482014-05-02 15:27:29 -0700680 result = result && CheckWideRegisterIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700681 break;
682 }
683 switch (inst->GetVerifyTypeArgumentC()) {
684 case Instruction::kVerifyRegC:
Ian Rogers29a26482014-05-02 15:27:29 -0700685 result = result && CheckRegisterIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700686 break;
687 case Instruction::kVerifyRegCField:
Ian Rogers29a26482014-05-02 15:27:29 -0700688 result = result && CheckFieldIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700689 break;
690 case Instruction::kVerifyRegCNewArray:
Ian Rogers29a26482014-05-02 15:27:29 -0700691 result = result && CheckNewArray(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700692 break;
693 case Instruction::kVerifyRegCType:
Ian Rogers29a26482014-05-02 15:27:29 -0700694 result = result && CheckTypeIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700695 break;
696 case Instruction::kVerifyRegCWide:
Ian Rogers29a26482014-05-02 15:27:29 -0700697 result = result && CheckWideRegisterIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700698 break;
699 }
700 switch (inst->GetVerifyExtraFlags()) {
701 case Instruction::kVerifyArrayData:
702 result = result && CheckArrayData(code_offset);
703 break;
704 case Instruction::kVerifyBranchTarget:
705 result = result && CheckBranchTarget(code_offset);
706 break;
707 case Instruction::kVerifySwitchTargets:
708 result = result && CheckSwitchTargets(code_offset);
709 break;
Ian Rogers29a26482014-05-02 15:27:29 -0700710 case Instruction::kVerifyVarArg: {
711 uint32_t args[Instruction::kMaxVarArgRegs];
712 inst->GetVarArgs(args);
713 result = result && CheckVarArgRegs(inst->VRegA(), args);
Ian Rogersd81871c2011-10-03 13:57:23 -0700714 break;
Ian Rogers29a26482014-05-02 15:27:29 -0700715 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700716 case Instruction::kVerifyVarArgRange:
Ian Rogers29a26482014-05-02 15:27:29 -0700717 result = result && CheckVarArgRangeRegs(inst->VRegA(), inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700718 break;
719 case Instruction::kVerifyError:
jeffhaod5347e02012-03-22 17:25:05 -0700720 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected opcode " << inst->Name();
Ian Rogersd81871c2011-10-03 13:57:23 -0700721 result = false;
722 break;
723 }
724 return result;
725}
726
Ian Rogers776ac1f2012-04-13 23:36:36 -0700727bool MethodVerifier::CheckRegisterIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700728 if (idx >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700729 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register index out of range (" << idx << " >= "
730 << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700731 return false;
732 }
733 return true;
734}
735
Ian Rogers776ac1f2012-04-13 23:36:36 -0700736bool MethodVerifier::CheckWideRegisterIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700737 if (idx + 1 >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700738 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "wide register index out of range (" << idx
739 << "+1 >= " << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700740 return false;
741 }
742 return true;
743}
744
Ian Rogers776ac1f2012-04-13 23:36:36 -0700745bool MethodVerifier::CheckFieldIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700746 if (idx >= dex_file_->GetHeader().field_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700747 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad field index " << idx << " (max "
748 << dex_file_->GetHeader().field_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700749 return false;
750 }
751 return true;
752}
753
Ian Rogers776ac1f2012-04-13 23:36:36 -0700754bool MethodVerifier::CheckMethodIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700755 if (idx >= dex_file_->GetHeader().method_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700756 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad method index " << idx << " (max "
757 << dex_file_->GetHeader().method_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700758 return false;
759 }
760 return true;
761}
762
Ian Rogers776ac1f2012-04-13 23:36:36 -0700763bool MethodVerifier::CheckNewInstance(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700764 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700765 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
766 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700767 return false;
768 }
769 // We don't need the actual class, just a pointer to the class name.
Ian Rogers0571d352011-11-03 19:51:38 -0700770 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -0700771 if (descriptor[0] != 'L') {
jeffhaod5347e02012-03-22 17:25:05 -0700772 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "can't call new-instance on type '" << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -0700773 return false;
774 }
775 return true;
776}
777
Ian Rogers776ac1f2012-04-13 23:36:36 -0700778bool MethodVerifier::CheckStringIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700779 if (idx >= dex_file_->GetHeader().string_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700780 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad string index " << idx << " (max "
781 << dex_file_->GetHeader().string_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700782 return false;
783 }
784 return true;
785}
786
Ian Rogers776ac1f2012-04-13 23:36:36 -0700787bool MethodVerifier::CheckTypeIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700788 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700789 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
790 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700791 return false;
792 }
793 return true;
794}
795
Ian Rogers776ac1f2012-04-13 23:36:36 -0700796bool MethodVerifier::CheckNewArray(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700797 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700798 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
799 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700800 return false;
801 }
802 int bracket_count = 0;
Ian Rogers0571d352011-11-03 19:51:38 -0700803 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -0700804 const char* cp = descriptor;
805 while (*cp++ == '[') {
806 bracket_count++;
807 }
808 if (bracket_count == 0) {
809 /* The given class must be an array type. */
Brian Carlstrom93c33962013-07-26 10:37:43 -0700810 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
811 << "can't new-array class '" << descriptor << "' (not an array)";
Ian Rogersd81871c2011-10-03 13:57:23 -0700812 return false;
813 } else if (bracket_count > 255) {
814 /* It is illegal to create an array of more than 255 dimensions. */
Brian Carlstrom93c33962013-07-26 10:37:43 -0700815 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
816 << "can't new-array class '" << descriptor << "' (exceeds limit)";
Ian Rogersd81871c2011-10-03 13:57:23 -0700817 return false;
818 }
819 return true;
820}
821
Ian Rogers776ac1f2012-04-13 23:36:36 -0700822bool MethodVerifier::CheckArrayData(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700823 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
824 const uint16_t* insns = code_item_->insns_ + cur_offset;
825 const uint16_t* array_data;
826 int32_t array_data_offset;
827
828 DCHECK_LT(cur_offset, insn_count);
829 /* make sure the start of the array data table is in range */
830 array_data_offset = insns[1] | (((int32_t) insns[2]) << 16);
831 if ((int32_t) cur_offset + array_data_offset < 0 ||
832 cur_offset + array_data_offset + 2 >= insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -0700833 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data start: at " << cur_offset
Brian Carlstrom93c33962013-07-26 10:37:43 -0700834 << ", data offset " << array_data_offset
835 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -0700836 return false;
837 }
838 /* offset to array data table is a relative branch-style offset */
839 array_data = insns + array_data_offset;
840 /* make sure the table is 32-bit aligned */
Ian Rogersef7d42f2014-01-06 12:55:46 -0800841 if ((reinterpret_cast<uintptr_t>(array_data) & 0x03) != 0) {
jeffhaod5347e02012-03-22 17:25:05 -0700842 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned array data table: at " << cur_offset
843 << ", data offset " << array_data_offset;
Ian Rogersd81871c2011-10-03 13:57:23 -0700844 return false;
845 }
846 uint32_t value_width = array_data[1];
Elliott Hughes398f64b2012-03-26 18:05:48 -0700847 uint32_t value_count = *reinterpret_cast<const uint32_t*>(&array_data[2]);
Ian Rogersd81871c2011-10-03 13:57:23 -0700848 uint32_t table_size = 4 + (value_width * value_count + 1) / 2;
849 /* make sure the end of the switch is in range */
850 if (cur_offset + array_data_offset + table_size > insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -0700851 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data end: at " << cur_offset
852 << ", data offset " << array_data_offset << ", end "
853 << cur_offset + array_data_offset + table_size
854 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -0700855 return false;
856 }
857 return true;
858}
859
Ian Rogers776ac1f2012-04-13 23:36:36 -0700860bool MethodVerifier::CheckBranchTarget(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700861 int32_t offset;
862 bool isConditional, selfOkay;
863 if (!GetBranchOffset(cur_offset, &offset, &isConditional, &selfOkay)) {
864 return false;
865 }
866 if (!selfOkay && offset == 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700867 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch offset of zero not allowed at"
868 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -0700869 return false;
870 }
Elliott Hughes81ff3182012-03-23 20:35:56 -0700871 // Check for 32-bit overflow. This isn't strictly necessary if we can depend on the runtime
872 // to have identical "wrap-around" behavior, but it's unwise to depend on that.
Ian Rogersd81871c2011-10-03 13:57:23 -0700873 if (((int64_t) cur_offset + (int64_t) offset) != (int64_t) (cur_offset + offset)) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700874 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch target overflow "
875 << reinterpret_cast<void*>(cur_offset) << " +" << offset;
Ian Rogersd81871c2011-10-03 13:57:23 -0700876 return false;
877 }
878 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
879 int32_t abs_offset = cur_offset + offset;
Brian Carlstrom93c33962013-07-26 10:37:43 -0700880 if (abs_offset < 0 ||
881 (uint32_t) abs_offset >= insn_count ||
882 !insn_flags_[abs_offset].IsOpcode()) {
jeffhaod5347e02012-03-22 17:25:05 -0700883 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid branch target " << offset << " (-> "
Elliott Hughes398f64b2012-03-26 18:05:48 -0700884 << reinterpret_cast<void*>(abs_offset) << ") at "
885 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -0700886 return false;
887 }
888 insn_flags_[abs_offset].SetBranchTarget();
889 return true;
890}
891
Ian Rogers776ac1f2012-04-13 23:36:36 -0700892bool MethodVerifier::GetBranchOffset(uint32_t cur_offset, int32_t* pOffset, bool* pConditional,
Ian Rogersd81871c2011-10-03 13:57:23 -0700893 bool* selfOkay) {
894 const uint16_t* insns = code_item_->insns_ + cur_offset;
895 *pConditional = false;
896 *selfOkay = false;
jeffhaoba5ebb92011-08-25 17:24:37 -0700897 switch (*insns & 0xff) {
898 case Instruction::GOTO:
899 *pOffset = ((int16_t) *insns) >> 8;
jeffhaoba5ebb92011-08-25 17:24:37 -0700900 break;
901 case Instruction::GOTO_32:
902 *pOffset = insns[1] | (((uint32_t) insns[2]) << 16);
jeffhaoba5ebb92011-08-25 17:24:37 -0700903 *selfOkay = true;
904 break;
905 case Instruction::GOTO_16:
906 *pOffset = (int16_t) insns[1];
jeffhaoba5ebb92011-08-25 17:24:37 -0700907 break;
908 case Instruction::IF_EQ:
909 case Instruction::IF_NE:
910 case Instruction::IF_LT:
911 case Instruction::IF_GE:
912 case Instruction::IF_GT:
913 case Instruction::IF_LE:
914 case Instruction::IF_EQZ:
915 case Instruction::IF_NEZ:
916 case Instruction::IF_LTZ:
917 case Instruction::IF_GEZ:
918 case Instruction::IF_GTZ:
919 case Instruction::IF_LEZ:
920 *pOffset = (int16_t) insns[1];
921 *pConditional = true;
jeffhaoba5ebb92011-08-25 17:24:37 -0700922 break;
923 default:
924 return false;
925 break;
926 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700927 return true;
928}
929
Ian Rogers776ac1f2012-04-13 23:36:36 -0700930bool MethodVerifier::CheckSwitchTargets(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700931 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700932 DCHECK_LT(cur_offset, insn_count);
Ian Rogersd81871c2011-10-03 13:57:23 -0700933 const uint16_t* insns = code_item_->insns_ + cur_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -0700934 /* make sure the start of the switch is in range */
Ian Rogersd81871c2011-10-03 13:57:23 -0700935 int32_t switch_offset = insns[1] | ((int32_t) insns[2]) << 16;
936 if ((int32_t) cur_offset + switch_offset < 0 || cur_offset + switch_offset + 2 >= insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -0700937 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch start: at " << cur_offset
Brian Carlstrom93c33962013-07-26 10:37:43 -0700938 << ", switch offset " << switch_offset
939 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -0700940 return false;
941 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700942 /* offset to switch table is a relative branch-style offset */
Ian Rogersd81871c2011-10-03 13:57:23 -0700943 const uint16_t* switch_insns = insns + switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -0700944 /* make sure the table is 32-bit aligned */
Ian Rogersef7d42f2014-01-06 12:55:46 -0800945 if ((reinterpret_cast<uintptr_t>(switch_insns) & 0x03) != 0) {
jeffhaod5347e02012-03-22 17:25:05 -0700946 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned switch table: at " << cur_offset
947 << ", switch offset " << switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -0700948 return false;
949 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700950 uint32_t switch_count = switch_insns[1];
951 int32_t keys_offset, targets_offset;
952 uint16_t expected_signature;
jeffhaoba5ebb92011-08-25 17:24:37 -0700953 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
954 /* 0=sig, 1=count, 2/3=firstKey */
955 targets_offset = 4;
956 keys_offset = -1;
957 expected_signature = Instruction::kPackedSwitchSignature;
958 } else {
959 /* 0=sig, 1=count, 2..count*2 = keys */
960 keys_offset = 2;
961 targets_offset = 2 + 2 * switch_count;
962 expected_signature = Instruction::kSparseSwitchSignature;
963 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700964 uint32_t table_size = targets_offset + switch_count * 2;
jeffhaoba5ebb92011-08-25 17:24:37 -0700965 if (switch_insns[0] != expected_signature) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700966 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
967 << StringPrintf("wrong signature for switch table (%x, wanted %x)",
968 switch_insns[0], expected_signature);
jeffhaoba5ebb92011-08-25 17:24:37 -0700969 return false;
970 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700971 /* make sure the end of the switch is in range */
972 if (cur_offset + switch_offset + table_size > (uint32_t) insn_count) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700973 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch end: at " << cur_offset
974 << ", switch offset " << switch_offset
975 << ", end " << (cur_offset + switch_offset + table_size)
jeffhaod5347e02012-03-22 17:25:05 -0700976 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -0700977 return false;
978 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700979 /* for a sparse switch, verify the keys are in ascending order */
980 if (keys_offset > 0 && switch_count > 1) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700981 int32_t last_key = switch_insns[keys_offset] | (switch_insns[keys_offset + 1] << 16);
982 for (uint32_t targ = 1; targ < switch_count; targ++) {
jeffhaoba5ebb92011-08-25 17:24:37 -0700983 int32_t key = (int32_t) switch_insns[keys_offset + targ * 2] |
984 (int32_t) (switch_insns[keys_offset + targ * 2 + 1] << 16);
985 if (key <= last_key) {
jeffhaod5347e02012-03-22 17:25:05 -0700986 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid packed switch: last key=" << last_key
987 << ", this=" << key;
jeffhaoba5ebb92011-08-25 17:24:37 -0700988 return false;
989 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700990 last_key = key;
991 }
992 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700993 /* verify each switch target */
Ian Rogersd81871c2011-10-03 13:57:23 -0700994 for (uint32_t targ = 0; targ < switch_count; targ++) {
995 int32_t offset = (int32_t) switch_insns[targets_offset + targ * 2] |
996 (int32_t) (switch_insns[targets_offset + targ * 2 + 1] << 16);
997 int32_t abs_offset = cur_offset + offset;
Brian Carlstrom93c33962013-07-26 10:37:43 -0700998 if (abs_offset < 0 ||
999 abs_offset >= (int32_t) insn_count ||
1000 !insn_flags_[abs_offset].IsOpcode()) {
1001 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch target " << offset
1002 << " (-> " << reinterpret_cast<void*>(abs_offset) << ") at "
1003 << reinterpret_cast<void*>(cur_offset)
1004 << "[" << targ << "]";
jeffhaoba5ebb92011-08-25 17:24:37 -07001005 return false;
1006 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001007 insn_flags_[abs_offset].SetBranchTarget();
1008 }
1009 return true;
1010}
1011
Ian Rogers776ac1f2012-04-13 23:36:36 -07001012bool MethodVerifier::CheckVarArgRegs(uint32_t vA, uint32_t arg[]) {
Ian Rogers29a26482014-05-02 15:27:29 -07001013 if (vA > Instruction::kMaxVarArgRegs) {
jeffhaod5347e02012-03-22 17:25:05 -07001014 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << vA << ") in non-range invoke)";
Ian Rogersd81871c2011-10-03 13:57:23 -07001015 return false;
1016 }
1017 uint16_t registers_size = code_item_->registers_size_;
1018 for (uint32_t idx = 0; idx < vA; idx++) {
jeffhao457cc512012-02-02 16:55:13 -08001019 if (arg[idx] >= registers_size) {
jeffhaod5347e02012-03-22 17:25:05 -07001020 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index (" << arg[idx]
1021 << ") in non-range invoke (>= " << registers_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001022 return false;
1023 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001024 }
1025
1026 return true;
1027}
1028
Ian Rogers776ac1f2012-04-13 23:36:36 -07001029bool MethodVerifier::CheckVarArgRangeRegs(uint32_t vA, uint32_t vC) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001030 uint16_t registers_size = code_item_->registers_size_;
1031 // vA/vC are unsigned 8-bit/16-bit quantities for /range instructions, so there's no risk of
1032 // integer overflow when adding them here.
1033 if (vA + vC > registers_size) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001034 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index " << vA << "+" << vC
1035 << " in range invoke (> " << registers_size << ")";
jeffhaoba5ebb92011-08-25 17:24:37 -07001036 return false;
1037 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001038 return true;
1039}
1040
Ian Rogers776ac1f2012-04-13 23:36:36 -07001041bool MethodVerifier::VerifyCodeFlow() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001042 uint16_t registers_size = code_item_->registers_size_;
1043 uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaobdb76512011-09-07 11:43:16 -07001044
Ian Rogersd81871c2011-10-03 13:57:23 -07001045 if (registers_size * insns_size > 4*1024*1024) {
buzbee4922ef92012-02-24 14:32:20 -08001046 LOG(WARNING) << "warning: method is huge (regs=" << registers_size
1047 << " insns_size=" << insns_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001048 }
1049 /* Create and initialize table holding register status */
Brian Carlstrom93c33962013-07-26 10:37:43 -07001050 reg_table_.Init(kTrackCompilerInterestPoints,
1051 insn_flags_.get(),
1052 insns_size,
1053 registers_size,
1054 this);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07001055
jeffhaobdb76512011-09-07 11:43:16 -07001056
Ian Rogersd0fbd852013-09-24 18:17:04 -07001057 work_line_.reset(RegisterLine::Create(registers_size, this));
1058 saved_line_.reset(RegisterLine::Create(registers_size, this));
jeffhaobdb76512011-09-07 11:43:16 -07001059
Ian Rogersd81871c2011-10-03 13:57:23 -07001060 /* Initialize register types of method arguments. */
1061 if (!SetTypesFromSignature()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001062 DCHECK_NE(failures_.size(), 0U);
1063 std::string prepend("Bad signature in ");
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001064 prepend += PrettyMethod(dex_method_idx_, *dex_file_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001065 PrependToLastFailMessage(prepend);
Ian Rogersd81871c2011-10-03 13:57:23 -07001066 return false;
1067 }
1068 /* Perform code flow verification. */
1069 if (!CodeFlowVerifyMethod()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001070 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -07001071 return false;
jeffhaobdb76512011-09-07 11:43:16 -07001072 }
jeffhaobdb76512011-09-07 11:43:16 -07001073 return true;
1074}
1075
Ian Rogersad0b3a32012-04-16 14:50:24 -07001076std::ostream& MethodVerifier::DumpFailures(std::ostream& os) {
1077 DCHECK_EQ(failures_.size(), failure_messages_.size());
Jeff Hao4137f482013-11-22 11:44:57 -08001078 for (size_t i = 0; i < failures_.size(); ++i) {
1079 os << failure_messages_[i]->str() << "\n";
Ian Rogersad0b3a32012-04-16 14:50:24 -07001080 }
1081 return os;
1082}
1083
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001084extern "C" void MethodVerifierGdbDump(MethodVerifier* v)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001085 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001086 v->Dump(std::cerr);
1087}
1088
Ian Rogers776ac1f2012-04-13 23:36:36 -07001089void MethodVerifier::Dump(std::ostream& os) {
jeffhaof56197c2012-03-05 18:01:54 -08001090 if (code_item_ == NULL) {
Elliott Hughesc073b072012-05-24 19:29:17 -07001091 os << "Native method\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001092 return;
jeffhaobdb76512011-09-07 11:43:16 -07001093 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001094 {
1095 os << "Register Types:\n";
1096 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
1097 std::ostream indent_os(&indent_filter);
1098 reg_types_.Dump(indent_os);
1099 }
Ian Rogersb4903572012-10-11 11:52:56 -07001100 os << "Dumping instructions and register lines:\n";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001101 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
1102 std::ostream indent_os(&indent_filter);
Ian Rogersd81871c2011-10-03 13:57:23 -07001103 const Instruction* inst = Instruction::At(code_item_->insns_);
1104 for (size_t dex_pc = 0; dex_pc < code_item_->insns_size_in_code_units_;
1105 dex_pc += insn_flags_[dex_pc].GetLengthInCodeUnits()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001106 RegisterLine* reg_line = reg_table_.GetLine(dex_pc);
1107 if (reg_line != NULL) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001108 indent_os << reg_line->Dump() << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07001109 }
Ian Rogers7b3ddd22013-02-21 15:19:52 -08001110 indent_os << StringPrintf("0x%04zx", dex_pc) << ": " << insn_flags_[dex_pc].ToString() << " ";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001111 const bool kDumpHexOfInstruction = false;
1112 if (kDumpHexOfInstruction) {
1113 indent_os << inst->DumpHex(5) << " ";
1114 }
1115 indent_os << inst->DumpString(dex_file_) << "\n";
jeffhaoba5ebb92011-08-25 17:24:37 -07001116 inst = inst->Next();
1117 }
jeffhaobdb76512011-09-07 11:43:16 -07001118}
1119
Ian Rogersd81871c2011-10-03 13:57:23 -07001120static bool IsPrimitiveDescriptor(char descriptor) {
1121 switch (descriptor) {
jeffhaobdb76512011-09-07 11:43:16 -07001122 case 'I':
1123 case 'C':
1124 case 'S':
1125 case 'B':
1126 case 'Z':
jeffhaobdb76512011-09-07 11:43:16 -07001127 case 'F':
1128 case 'D':
1129 case 'J':
Ian Rogersd81871c2011-10-03 13:57:23 -07001130 return true;
jeffhaobdb76512011-09-07 11:43:16 -07001131 default:
1132 return false;
1133 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001134}
1135
Ian Rogers776ac1f2012-04-13 23:36:36 -07001136bool MethodVerifier::SetTypesFromSignature() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001137 RegisterLine* reg_line = reg_table_.GetLine(0);
1138 int arg_start = code_item_->registers_size_ - code_item_->ins_size_;
1139 size_t expected_args = code_item_->ins_size_; /* long/double count as two */
jeffhaobdb76512011-09-07 11:43:16 -07001140
Ian Rogersd81871c2011-10-03 13:57:23 -07001141 DCHECK_GE(arg_start, 0); /* should have been verified earlier */
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001142 // Include the "this" pointer.
Ian Rogersd81871c2011-10-03 13:57:23 -07001143 size_t cur_arg = 0;
Ian Rogersad0b3a32012-04-16 14:50:24 -07001144 if (!IsStatic()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001145 // If this is a constructor for a class other than java.lang.Object, mark the first ("this")
1146 // argument as uninitialized. This restricts field access until the superclass constructor is
1147 // called.
Ian Rogersad0b3a32012-04-16 14:50:24 -07001148 const RegType& declaring_class = GetDeclaringClass();
1149 if (IsConstructor() && !declaring_class.IsJavaLangObject()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001150 reg_line->SetRegisterType(arg_start + cur_arg,
1151 reg_types_.UninitializedThisArgument(declaring_class));
1152 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001153 reg_line->SetRegisterType(arg_start + cur_arg, declaring_class);
jeffhaobdb76512011-09-07 11:43:16 -07001154 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001155 cur_arg++;
jeffhaobdb76512011-09-07 11:43:16 -07001156 }
1157
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001158 const DexFile::ProtoId& proto_id =
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001159 dex_file_->GetMethodPrototype(dex_file_->GetMethodId(dex_method_idx_));
Ian Rogers0571d352011-11-03 19:51:38 -07001160 DexFileParameterIterator iterator(*dex_file_, proto_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07001161
1162 for (; iterator.HasNext(); iterator.Next()) {
1163 const char* descriptor = iterator.GetDescriptor();
1164 if (descriptor == NULL) {
1165 LOG(FATAL) << "Null descriptor";
1166 }
1167 if (cur_arg >= expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07001168 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1169 << " args, found more (" << descriptor << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001170 return false;
1171 }
1172 switch (descriptor[0]) {
1173 case 'L':
1174 case '[':
1175 // We assume that reference arguments are initialized. The only way it could be otherwise
1176 // (assuming the caller was verified) is if the current method is <init>, but in that case
1177 // it's effectively considered initialized the instant we reach here (in the sense that we
1178 // can return without doing anything or call virtual methods).
1179 {
Sebastien Hertz2ed76f92014-04-22 17:11:08 +02001180 const RegType& reg_type = ResolveClassAndCheckAccess(iterator.GetTypeIdx());
1181 if (!reg_type.IsNonZeroReferenceTypes()) {
1182 DCHECK(HasFailures());
1183 return false;
1184 }
Ian Rogers84fa0742011-10-25 18:13:30 -07001185 reg_line->SetRegisterType(arg_start + cur_arg, reg_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001186 }
1187 break;
1188 case 'Z':
1189 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Boolean());
1190 break;
1191 case 'C':
1192 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Char());
1193 break;
1194 case 'B':
1195 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Byte());
1196 break;
1197 case 'I':
1198 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Integer());
1199 break;
1200 case 'S':
1201 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Short());
1202 break;
1203 case 'F':
1204 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Float());
1205 break;
1206 case 'J':
1207 case 'D': {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001208 const RegType& lo_half = descriptor[0] == 'J' ? reg_types_.LongLo() : reg_types_.DoubleLo();
1209 const RegType& hi_half = descriptor[0] == 'J' ? reg_types_.LongHi() : reg_types_.DoubleHi();
1210 reg_line->SetRegisterTypeWide(arg_start + cur_arg, lo_half, hi_half);
Ian Rogersd81871c2011-10-03 13:57:23 -07001211 cur_arg++;
1212 break;
1213 }
1214 default:
Brian Carlstrom93c33962013-07-26 10:37:43 -07001215 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected signature type char '"
1216 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001217 return false;
1218 }
1219 cur_arg++;
1220 }
1221 if (cur_arg != expected_args) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001222 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1223 << " arguments, found " << cur_arg;
Ian Rogersd81871c2011-10-03 13:57:23 -07001224 return false;
1225 }
1226 const char* descriptor = dex_file_->GetReturnTypeDescriptor(proto_id);
1227 // Validate return type. We don't do the type lookup; just want to make sure that it has the right
1228 // format. Only major difference from the method argument format is that 'V' is supported.
1229 bool result;
1230 if (IsPrimitiveDescriptor(descriptor[0]) || descriptor[0] == 'V') {
1231 result = descriptor[1] == '\0';
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001232 } else if (descriptor[0] == '[') { // single/multi-dimensional array of object/primitive
Ian Rogersd81871c2011-10-03 13:57:23 -07001233 size_t i = 0;
1234 do {
1235 i++;
1236 } while (descriptor[i] == '['); // process leading [
1237 if (descriptor[i] == 'L') { // object array
1238 do {
1239 i++; // find closing ;
1240 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1241 result = descriptor[i] == ';';
1242 } else { // primitive array
1243 result = IsPrimitiveDescriptor(descriptor[i]) && descriptor[i + 1] == '\0';
1244 }
1245 } else if (descriptor[0] == 'L') {
1246 // could be more thorough here, but shouldn't be required
1247 size_t i = 0;
1248 do {
1249 i++;
1250 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1251 result = descriptor[i] == ';';
1252 } else {
1253 result = false;
1254 }
1255 if (!result) {
jeffhaod5347e02012-03-22 17:25:05 -07001256 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected char in return type descriptor '"
1257 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001258 }
1259 return result;
jeffhaobdb76512011-09-07 11:43:16 -07001260}
1261
Ian Rogers776ac1f2012-04-13 23:36:36 -07001262bool MethodVerifier::CodeFlowVerifyMethod() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001263 const uint16_t* insns = code_item_->insns_;
1264 const uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaoba5ebb92011-08-25 17:24:37 -07001265
jeffhaobdb76512011-09-07 11:43:16 -07001266 /* Begin by marking the first instruction as "changed". */
Ian Rogersd81871c2011-10-03 13:57:23 -07001267 insn_flags_[0].SetChanged();
1268 uint32_t start_guess = 0;
jeffhaoba5ebb92011-08-25 17:24:37 -07001269
jeffhaobdb76512011-09-07 11:43:16 -07001270 /* Continue until no instructions are marked "changed". */
1271 while (true) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001272 // Find the first marked one. Use "start_guess" as a way to find one quickly.
1273 uint32_t insn_idx = start_guess;
1274 for (; insn_idx < insns_size; insn_idx++) {
1275 if (insn_flags_[insn_idx].IsChanged())
jeffhaobdb76512011-09-07 11:43:16 -07001276 break;
1277 }
jeffhaobdb76512011-09-07 11:43:16 -07001278 if (insn_idx == insns_size) {
1279 if (start_guess != 0) {
1280 /* try again, starting from the top */
1281 start_guess = 0;
1282 continue;
1283 } else {
1284 /* all flags are clear */
1285 break;
1286 }
1287 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001288 // We carry the working set of registers from instruction to instruction. If this address can
1289 // be the target of a branch (or throw) instruction, or if we're skipping around chasing
1290 // "changed" flags, we need to load the set of registers from the table.
1291 // Because we always prefer to continue on to the next instruction, we should never have a
1292 // situation where we have a stray "changed" flag set on an instruction that isn't a branch
1293 // target.
1294 work_insn_idx_ = insn_idx;
1295 if (insn_flags_[insn_idx].IsBranchTarget()) {
1296 work_line_->CopyFromLine(reg_table_.GetLine(insn_idx));
jeffhaobdb76512011-09-07 11:43:16 -07001297 } else {
1298#ifndef NDEBUG
1299 /*
1300 * Sanity check: retrieve the stored register line (assuming
1301 * a full table) and make sure it actually matches.
1302 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001303 RegisterLine* register_line = reg_table_.GetLine(insn_idx);
1304 if (register_line != NULL) {
1305 if (work_line_->CompareLine(register_line) != 0) {
1306 Dump(std::cout);
1307 std::cout << info_messages_.str();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001308 LOG(FATAL) << "work_line diverged in " << PrettyMethod(dex_method_idx_, *dex_file_)
Elliott Hughesc073b072012-05-24 19:29:17 -07001309 << "@" << reinterpret_cast<void*>(work_insn_idx_) << "\n"
1310 << " work_line=" << *work_line_ << "\n"
Elliott Hughes398f64b2012-03-26 18:05:48 -07001311 << " expected=" << *register_line;
Ian Rogersd81871c2011-10-03 13:57:23 -07001312 }
jeffhaobdb76512011-09-07 11:43:16 -07001313 }
1314#endif
1315 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001316 if (!CodeFlowVerifyInstruction(&start_guess)) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001317 std::string prepend(PrettyMethod(dex_method_idx_, *dex_file_));
Ian Rogersad0b3a32012-04-16 14:50:24 -07001318 prepend += " failed to verify: ";
1319 PrependToLastFailMessage(prepend);
jeffhaoba5ebb92011-08-25 17:24:37 -07001320 return false;
1321 }
jeffhaobdb76512011-09-07 11:43:16 -07001322 /* Clear "changed" and mark as visited. */
Ian Rogersd81871c2011-10-03 13:57:23 -07001323 insn_flags_[insn_idx].SetVisited();
1324 insn_flags_[insn_idx].ClearChanged();
jeffhaobdb76512011-09-07 11:43:16 -07001325 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001326
Ian Rogers1c849e52012-06-28 14:00:33 -07001327 if (gDebugVerify) {
jeffhaobdb76512011-09-07 11:43:16 -07001328 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001329 * Scan for dead code. There's nothing "evil" about dead code
jeffhaobdb76512011-09-07 11:43:16 -07001330 * (besides the wasted space), but it indicates a flaw somewhere
1331 * down the line, possibly in the verifier.
1332 *
1333 * If we've substituted "always throw" instructions into the stream,
1334 * we are almost certainly going to have some dead code.
1335 */
1336 int dead_start = -1;
Ian Rogersd81871c2011-10-03 13:57:23 -07001337 uint32_t insn_idx = 0;
1338 for (; insn_idx < insns_size; insn_idx += insn_flags_[insn_idx].GetLengthInCodeUnits()) {
jeffhaobdb76512011-09-07 11:43:16 -07001339 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001340 * Switch-statement data doesn't get "visited" by scanner. It
jeffhaobdb76512011-09-07 11:43:16 -07001341 * may or may not be preceded by a padding NOP (for alignment).
1342 */
1343 if (insns[insn_idx] == Instruction::kPackedSwitchSignature ||
1344 insns[insn_idx] == Instruction::kSparseSwitchSignature ||
1345 insns[insn_idx] == Instruction::kArrayDataSignature ||
Elliott Hughes380aaa72012-07-09 14:33:15 -07001346 (insns[insn_idx] == Instruction::NOP && (insn_idx + 1 < insns_size) &&
jeffhaobdb76512011-09-07 11:43:16 -07001347 (insns[insn_idx + 1] == Instruction::kPackedSwitchSignature ||
1348 insns[insn_idx + 1] == Instruction::kSparseSwitchSignature ||
1349 insns[insn_idx + 1] == Instruction::kArrayDataSignature))) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001350 insn_flags_[insn_idx].SetVisited();
jeffhaobdb76512011-09-07 11:43:16 -07001351 }
1352
Ian Rogersd81871c2011-10-03 13:57:23 -07001353 if (!insn_flags_[insn_idx].IsVisited()) {
jeffhaobdb76512011-09-07 11:43:16 -07001354 if (dead_start < 0)
1355 dead_start = insn_idx;
1356 } else if (dead_start >= 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001357 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start)
1358 << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaobdb76512011-09-07 11:43:16 -07001359 dead_start = -1;
1360 }
1361 }
1362 if (dead_start >= 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001363 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start)
1364 << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaoba5ebb92011-08-25 17:24:37 -07001365 }
Ian Rogersc9e463c2013-06-05 16:52:26 -07001366 // To dump the state of the verify after a method, do something like:
1367 // if (PrettyMethod(dex_method_idx_, *dex_file_) ==
1368 // "boolean java.lang.String.equals(java.lang.Object)") {
1369 // LOG(INFO) << info_messages_.str();
1370 // }
jeffhaoba5ebb92011-08-25 17:24:37 -07001371 }
jeffhaobdb76512011-09-07 11:43:16 -07001372 return true;
1373}
1374
Ian Rogers776ac1f2012-04-13 23:36:36 -07001375bool MethodVerifier::CodeFlowVerifyInstruction(uint32_t* start_guess) {
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001376 // If we're doing FindLocksAtDexPc, check whether we're at the dex pc we care about.
1377 // We want the state _before_ the instruction, for the case where the dex pc we're
1378 // interested in is itself a monitor-enter instruction (which is a likely place
1379 // for a thread to be suspended).
1380 if (monitor_enter_dex_pcs_ != NULL && work_insn_idx_ == interesting_dex_pc_) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001381 monitor_enter_dex_pcs_->clear(); // The new work line is more accurate than the previous one.
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001382 for (size_t i = 0; i < work_line_->GetMonitorEnterCount(); ++i) {
1383 monitor_enter_dex_pcs_->push_back(work_line_->GetMonitorEnterDexPc(i));
1384 }
1385 }
1386
jeffhaobdb76512011-09-07 11:43:16 -07001387 /*
1388 * Once we finish decoding the instruction, we need to figure out where
jeffhaod1f0fde2011-09-08 17:25:33 -07001389 * we can go from here. There are three possible ways to transfer
jeffhaobdb76512011-09-07 11:43:16 -07001390 * control to another statement:
1391 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001392 * (1) Continue to the next instruction. Applies to all but
jeffhaobdb76512011-09-07 11:43:16 -07001393 * unconditional branches, method returns, and exception throws.
jeffhaod1f0fde2011-09-08 17:25:33 -07001394 * (2) Branch to one or more possible locations. Applies to branches
jeffhaobdb76512011-09-07 11:43:16 -07001395 * and switch statements.
jeffhaod1f0fde2011-09-08 17:25:33 -07001396 * (3) Exception handlers. Applies to any instruction that can
jeffhaobdb76512011-09-07 11:43:16 -07001397 * throw an exception that is handled by an encompassing "try"
1398 * block.
1399 *
1400 * We can also return, in which case there is no successor instruction
1401 * from this point.
1402 *
Elliott Hughesadb8c672012-03-06 16:49:32 -08001403 * The behavior can be determined from the opcode flags.
jeffhaobdb76512011-09-07 11:43:16 -07001404 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001405 const uint16_t* insns = code_item_->insns_ + work_insn_idx_;
1406 const Instruction* inst = Instruction::At(insns);
Ian Rogersa75a0132012-09-28 11:41:42 -07001407 int opcode_flags = Instruction::FlagsOf(inst->Opcode());
jeffhaobdb76512011-09-07 11:43:16 -07001408
jeffhaobdb76512011-09-07 11:43:16 -07001409 int32_t branch_target = 0;
jeffhaobdb76512011-09-07 11:43:16 -07001410 bool just_set_result = false;
Ian Rogers2c8a8572011-10-24 17:11:36 -07001411 if (gDebugVerify) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001412 // Generate processing back trace to debug verifier
Elliott Hughesc073b072012-05-24 19:29:17 -07001413 LogVerifyInfo() << "Processing " << inst->DumpString(dex_file_) << "\n"
1414 << *work_line_.get() << "\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001415 }
jeffhaobdb76512011-09-07 11:43:16 -07001416
1417 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001418 * Make a copy of the previous register state. If the instruction
jeffhaobdb76512011-09-07 11:43:16 -07001419 * can throw an exception, we will copy/merge this into the "catch"
1420 * address rather than work_line, because we don't want the result
1421 * from the "successful" code path (e.g. a check-cast that "improves"
1422 * a type) to be visible to the exception handler.
1423 */
Ian Rogers776ac1f2012-04-13 23:36:36 -07001424 if ((opcode_flags & Instruction::kThrow) != 0 && CurrentInsnFlags()->IsInTry()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001425 saved_line_->CopyFromLine(work_line_.get());
jeffhaobdb76512011-09-07 11:43:16 -07001426 } else {
1427#ifndef NDEBUG
Ian Rogersd81871c2011-10-03 13:57:23 -07001428 saved_line_->FillWithGarbage();
jeffhaobdb76512011-09-07 11:43:16 -07001429#endif
1430 }
1431
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07001432
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001433 // We need to ensure the work line is consistent while performing validation. When we spot a
1434 // peephole pattern we compute a new line for either the fallthrough instruction or the
1435 // branch target.
Ian Rogers700a4022014-05-19 16:49:03 -07001436 std::unique_ptr<RegisterLine> branch_line;
1437 std::unique_ptr<RegisterLine> fallthrough_line;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001438
Sebastien Hertz849600b2013-12-20 10:28:08 +01001439 // We need precise constant types only for deoptimization which happens at runtime.
1440 const bool need_precise_constant = !Runtime::Current()->IsCompiler();
1441
Sebastien Hertz5243e912013-05-21 10:55:07 +02001442 switch (inst->Opcode()) {
jeffhaobdb76512011-09-07 11:43:16 -07001443 case Instruction::NOP:
1444 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001445 * A "pure" NOP has no effect on anything. Data tables start with
jeffhaobdb76512011-09-07 11:43:16 -07001446 * a signature that looks like a NOP; if we see one of these in
1447 * the course of executing code then we have a problem.
1448 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001449 if (inst->VRegA_10x() != 0) {
jeffhaod5347e02012-03-22 17:25:05 -07001450 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "encountered data table in instruction stream";
jeffhaobdb76512011-09-07 11:43:16 -07001451 }
1452 break;
1453
1454 case Instruction::MOVE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001455 work_line_->CopyRegister1(inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategory1nr);
1456 break;
jeffhaobdb76512011-09-07 11:43:16 -07001457 case Instruction::MOVE_FROM16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001458 work_line_->CopyRegister1(inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategory1nr);
1459 break;
jeffhaobdb76512011-09-07 11:43:16 -07001460 case Instruction::MOVE_16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001461 work_line_->CopyRegister1(inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategory1nr);
jeffhaobdb76512011-09-07 11:43:16 -07001462 break;
1463 case Instruction::MOVE_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001464 work_line_->CopyRegister2(inst->VRegA_12x(), inst->VRegB_12x());
1465 break;
jeffhaobdb76512011-09-07 11:43:16 -07001466 case Instruction::MOVE_WIDE_FROM16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001467 work_line_->CopyRegister2(inst->VRegA_22x(), inst->VRegB_22x());
1468 break;
jeffhaobdb76512011-09-07 11:43:16 -07001469 case Instruction::MOVE_WIDE_16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001470 work_line_->CopyRegister2(inst->VRegA_32x(), inst->VRegB_32x());
jeffhaobdb76512011-09-07 11:43:16 -07001471 break;
1472 case Instruction::MOVE_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001473 work_line_->CopyRegister1(inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategoryRef);
1474 break;
jeffhaobdb76512011-09-07 11:43:16 -07001475 case Instruction::MOVE_OBJECT_FROM16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001476 work_line_->CopyRegister1(inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategoryRef);
1477 break;
jeffhaobdb76512011-09-07 11:43:16 -07001478 case Instruction::MOVE_OBJECT_16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001479 work_line_->CopyRegister1(inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategoryRef);
jeffhaobdb76512011-09-07 11:43:16 -07001480 break;
1481
1482 /*
1483 * The move-result instructions copy data out of a "pseudo-register"
jeffhaod1f0fde2011-09-08 17:25:33 -07001484 * with the results from the last method invocation. In practice we
jeffhaobdb76512011-09-07 11:43:16 -07001485 * might want to hold the result in an actual CPU register, so the
1486 * Dalvik spec requires that these only appear immediately after an
1487 * invoke or filled-new-array.
1488 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001489 * These calls invalidate the "result" register. (This is now
jeffhaobdb76512011-09-07 11:43:16 -07001490 * redundant with the reset done below, but it can make the debug info
1491 * easier to read in some cases.)
1492 */
1493 case Instruction::MOVE_RESULT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001494 work_line_->CopyResultRegister1(inst->VRegA_11x(), false);
jeffhaobdb76512011-09-07 11:43:16 -07001495 break;
1496 case Instruction::MOVE_RESULT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001497 work_line_->CopyResultRegister2(inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07001498 break;
1499 case Instruction::MOVE_RESULT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001500 work_line_->CopyResultRegister1(inst->VRegA_11x(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001501 break;
1502
Ian Rogersd81871c2011-10-03 13:57:23 -07001503 case Instruction::MOVE_EXCEPTION: {
jeffhaobdb76512011-09-07 11:43:16 -07001504 /*
jeffhao60f83e32012-02-13 17:16:30 -08001505 * This statement can only appear as the first instruction in an exception handler. We verify
1506 * that as part of extracting the exception type from the catch block list.
jeffhaobdb76512011-09-07 11:43:16 -07001507 */
Ian Rogers28ad40d2011-10-27 15:19:26 -07001508 const RegType& res_type = GetCaughtExceptionType();
Sebastien Hertz5243e912013-05-21 10:55:07 +02001509 work_line_->SetRegisterType(inst->VRegA_11x(), res_type);
jeffhaobdb76512011-09-07 11:43:16 -07001510 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001511 }
jeffhaobdb76512011-09-07 11:43:16 -07001512 case Instruction::RETURN_VOID:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001513 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
1514 if (!GetMethodReturnType().IsConflict()) {
jeffhaod5347e02012-03-22 17:25:05 -07001515 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001516 }
jeffhaobdb76512011-09-07 11:43:16 -07001517 }
1518 break;
1519 case Instruction::RETURN:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001520 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
jeffhaobdb76512011-09-07 11:43:16 -07001521 /* check the method signature */
Ian Rogersd81871c2011-10-03 13:57:23 -07001522 const RegType& return_type = GetMethodReturnType();
1523 if (!return_type.IsCategory1Types()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001524 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected non-category 1 return type "
1525 << return_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001526 } else {
1527 // Compilers may generate synthetic functions that write byte values into boolean fields.
1528 // Also, it may use integer values for boolean, byte, short, and character return types.
Sebastien Hertz5243e912013-05-21 10:55:07 +02001529 const uint32_t vregA = inst->VRegA_11x();
1530 const RegType& src_type = work_line_->GetRegisterType(vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07001531 bool use_src = ((return_type.IsBoolean() && src_type.IsByte()) ||
1532 ((return_type.IsBoolean() || return_type.IsByte() ||
1533 return_type.IsShort() || return_type.IsChar()) &&
1534 src_type.IsInteger()));
1535 /* check the register contents */
Ian Rogersad0b3a32012-04-16 14:50:24 -07001536 bool success =
Sebastien Hertz5243e912013-05-21 10:55:07 +02001537 work_line_->VerifyRegisterType(vregA, use_src ? src_type : return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001538 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001539 AppendToLastFailMessage(StringPrintf(" return-1nr on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07001540 }
jeffhaobdb76512011-09-07 11:43:16 -07001541 }
1542 }
1543 break;
1544 case Instruction::RETURN_WIDE:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001545 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
jeffhaobdb76512011-09-07 11:43:16 -07001546 /* check the method signature */
Ian Rogersd81871c2011-10-03 13:57:23 -07001547 const RegType& return_type = GetMethodReturnType();
1548 if (!return_type.IsCategory2Types()) {
jeffhaod5347e02012-03-22 17:25:05 -07001549 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-wide not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001550 } else {
1551 /* check the register contents */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001552 const uint32_t vregA = inst->VRegA_11x();
1553 bool success = work_line_->VerifyRegisterType(vregA, return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001554 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001555 AppendToLastFailMessage(StringPrintf(" return-wide on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07001556 }
jeffhaobdb76512011-09-07 11:43:16 -07001557 }
1558 }
1559 break;
1560 case Instruction::RETURN_OBJECT:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001561 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001562 const RegType& return_type = GetMethodReturnType();
1563 if (!return_type.IsReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001564 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-object not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001565 } else {
1566 /* return_type is the *expected* return type, not register value */
1567 DCHECK(!return_type.IsZero());
1568 DCHECK(!return_type.IsUninitializedReference());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001569 const uint32_t vregA = inst->VRegA_11x();
1570 const RegType& reg_type = work_line_->GetRegisterType(vregA);
Ian Rogers9074b992011-10-26 17:41:55 -07001571 // Disallow returning uninitialized values and verify that the reference in vAA is an
1572 // instance of the "return_type"
1573 if (reg_type.IsUninitializedTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001574 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "returning uninitialized object '"
1575 << reg_type << "'";
Ian Rogers9074b992011-10-26 17:41:55 -07001576 } else if (!return_type.IsAssignableFrom(reg_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07001577 if (reg_type.IsUnresolvedTypes() || return_type.IsUnresolvedTypes()) {
1578 Fail(VERIFY_ERROR_NO_CLASS) << " can't resolve returned type '" << return_type
1579 << "' or '" << reg_type << "'";
1580 } else {
1581 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning '" << reg_type
1582 << "', but expected from declaration '" << return_type << "'";
1583 }
jeffhaobdb76512011-09-07 11:43:16 -07001584 }
1585 }
1586 }
1587 break;
1588
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001589 /* could be boolean, int, float, or a null reference */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001590 case Instruction::CONST_4: {
1591 int32_t val = static_cast<int32_t>(inst->VRegB_11n() << 28) >> 28;
Sebastien Hertz849600b2013-12-20 10:28:08 +01001592 work_line_->SetRegisterType(inst->VRegA_11n(),
1593 DetermineCat1Constant(val, need_precise_constant));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001594 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02001595 }
1596 case Instruction::CONST_16: {
1597 int16_t val = static_cast<int16_t>(inst->VRegB_21s());
Sebastien Hertz849600b2013-12-20 10:28:08 +01001598 work_line_->SetRegisterType(inst->VRegA_21s(),
1599 DetermineCat1Constant(val, need_precise_constant));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001600 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02001601 }
Sebastien Hertz849600b2013-12-20 10:28:08 +01001602 case Instruction::CONST: {
1603 int32_t val = inst->VRegB_31i();
Sebastien Hertz5243e912013-05-21 10:55:07 +02001604 work_line_->SetRegisterType(inst->VRegA_31i(),
Sebastien Hertz849600b2013-12-20 10:28:08 +01001605 DetermineCat1Constant(val, need_precise_constant));
jeffhaobdb76512011-09-07 11:43:16 -07001606 break;
Sebastien Hertz849600b2013-12-20 10:28:08 +01001607 }
1608 case Instruction::CONST_HIGH16: {
1609 int32_t val = static_cast<int32_t>(inst->VRegB_21h() << 16);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001610 work_line_->SetRegisterType(inst->VRegA_21h(),
Sebastien Hertz849600b2013-12-20 10:28:08 +01001611 DetermineCat1Constant(val, need_precise_constant));
jeffhaobdb76512011-09-07 11:43:16 -07001612 break;
Sebastien Hertz849600b2013-12-20 10:28:08 +01001613 }
jeffhaobdb76512011-09-07 11:43:16 -07001614 /* could be long or double; resolved upon use */
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001615 case Instruction::CONST_WIDE_16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001616 int64_t val = static_cast<int16_t>(inst->VRegB_21s());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001617 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1618 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001619 work_line_->SetRegisterTypeWide(inst->VRegA_21s(), lo, hi);
jeffhaobdb76512011-09-07 11:43:16 -07001620 break;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001621 }
1622 case Instruction::CONST_WIDE_32: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001623 int64_t val = static_cast<int32_t>(inst->VRegB_31i());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001624 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1625 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001626 work_line_->SetRegisterTypeWide(inst->VRegA_31i(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001627 break;
1628 }
1629 case Instruction::CONST_WIDE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001630 int64_t val = inst->VRegB_51l();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001631 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1632 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001633 work_line_->SetRegisterTypeWide(inst->VRegA_51l(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001634 break;
1635 }
1636 case Instruction::CONST_WIDE_HIGH16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001637 int64_t val = static_cast<uint64_t>(inst->VRegB_21h()) << 48;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001638 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1639 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001640 work_line_->SetRegisterTypeWide(inst->VRegA_21h(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001641 break;
1642 }
jeffhaobdb76512011-09-07 11:43:16 -07001643 case Instruction::CONST_STRING:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001644 work_line_->SetRegisterType(inst->VRegA_21c(), reg_types_.JavaLangString());
1645 break;
jeffhaobdb76512011-09-07 11:43:16 -07001646 case Instruction::CONST_STRING_JUMBO:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001647 work_line_->SetRegisterType(inst->VRegA_31c(), reg_types_.JavaLangString());
jeffhaobdb76512011-09-07 11:43:16 -07001648 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001649 case Instruction::CONST_CLASS: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001650 // Get type from instruction if unresolved then we need an access check
1651 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Sebastien Hertz5243e912013-05-21 10:55:07 +02001652 const RegType& res_type = ResolveClassAndCheckAccess(inst->VRegB_21c());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001653 // Register holds class, ie its type is class, on error it will hold Conflict.
Sebastien Hertz5243e912013-05-21 10:55:07 +02001654 work_line_->SetRegisterType(inst->VRegA_21c(),
Ian Rogersb4903572012-10-11 11:52:56 -07001655 res_type.IsConflict() ? res_type
1656 : reg_types_.JavaLangClass(true));
jeffhaobdb76512011-09-07 11:43:16 -07001657 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001658 }
jeffhaobdb76512011-09-07 11:43:16 -07001659 case Instruction::MONITOR_ENTER:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001660 work_line_->PushMonitor(inst->VRegA_11x(), work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07001661 break;
1662 case Instruction::MONITOR_EXIT:
1663 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001664 * monitor-exit instructions are odd. They can throw exceptions,
jeffhaobdb76512011-09-07 11:43:16 -07001665 * but when they do they act as if they succeeded and the PC is
jeffhaod1f0fde2011-09-08 17:25:33 -07001666 * pointing to the following instruction. (This behavior goes back
jeffhaobdb76512011-09-07 11:43:16 -07001667 * to the need to handle asynchronous exceptions, a now-deprecated
1668 * feature that Dalvik doesn't support.)
1669 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001670 * In practice we don't need to worry about this. The only
jeffhaobdb76512011-09-07 11:43:16 -07001671 * exceptions that can be thrown from monitor-exit are for a
jeffhaod1f0fde2011-09-08 17:25:33 -07001672 * null reference and -exit without a matching -enter. If the
jeffhaobdb76512011-09-07 11:43:16 -07001673 * structured locking checks are working, the former would have
1674 * failed on the -enter instruction, and the latter is impossible.
1675 *
1676 * This is fortunate, because issue 3221411 prevents us from
1677 * chasing the "can throw" path when monitor verification is
jeffhaod1f0fde2011-09-08 17:25:33 -07001678 * enabled. If we can fully verify the locking we can ignore
jeffhaobdb76512011-09-07 11:43:16 -07001679 * some catch blocks (which will show up as "dead" code when
1680 * we skip them here); if we can't, then the code path could be
1681 * "live" so we still need to check it.
1682 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001683 opcode_flags &= ~Instruction::kThrow;
Sebastien Hertz5243e912013-05-21 10:55:07 +02001684 work_line_->PopMonitor(inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07001685 break;
1686
Ian Rogers28ad40d2011-10-27 15:19:26 -07001687 case Instruction::CHECK_CAST:
Ian Rogersd81871c2011-10-03 13:57:23 -07001688 case Instruction::INSTANCE_OF: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001689 /*
1690 * If this instruction succeeds, we will "downcast" register vA to the type in vB. (This
1691 * could be a "upcast" -- not expected, so we don't try to address it.)
1692 *
1693 * If it fails, an exception is thrown, which we deal with later by ignoring the update to
Elliott Hughesadb8c672012-03-06 16:49:32 -08001694 * dec_insn.vA when branching to a handler.
Ian Rogers28ad40d2011-10-27 15:19:26 -07001695 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001696 const bool is_checkcast = (inst->Opcode() == Instruction::CHECK_CAST);
1697 const uint32_t type_idx = (is_checkcast) ? inst->VRegB_21c() : inst->VRegC_22c();
1698 const RegType& res_type = ResolveClassAndCheckAccess(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001699 if (res_type.IsConflict()) {
1700 DCHECK_NE(failures_.size(), 0U);
1701 if (!is_checkcast) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001702 work_line_->SetRegisterType(inst->VRegA_22c(), reg_types_.Boolean());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001703 }
1704 break; // bad class
Ian Rogers9f1ab122011-12-12 08:52:43 -08001705 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001706 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Sebastien Hertz5243e912013-05-21 10:55:07 +02001707 uint32_t orig_type_reg = (is_checkcast) ? inst->VRegA_21c() : inst->VRegB_22c();
1708 const RegType& orig_type = work_line_->GetRegisterType(orig_type_reg);
Ian Rogers28ad40d2011-10-27 15:19:26 -07001709 if (!res_type.IsNonZeroReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001710 if (is_checkcast) {
1711 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on unexpected class " << res_type;
1712 } else {
1713 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on unexpected class " << res_type;
1714 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001715 } else if (!orig_type.IsReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001716 if (is_checkcast) {
1717 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on non-reference in v" << orig_type_reg;
1718 } else {
1719 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on non-reference in v" << orig_type_reg;
1720 }
jeffhao2a8a90e2011-09-26 14:25:31 -07001721 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001722 if (is_checkcast) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001723 work_line_->SetRegisterType(inst->VRegA_21c(), res_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001724 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001725 work_line_->SetRegisterType(inst->VRegA_22c(), reg_types_.Boolean());
jeffhaobdb76512011-09-07 11:43:16 -07001726 }
jeffhaobdb76512011-09-07 11:43:16 -07001727 }
jeffhao2a8a90e2011-09-26 14:25:31 -07001728 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001729 }
1730 case Instruction::ARRAY_LENGTH: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001731 const RegType& res_type = work_line_->GetRegisterType(inst->VRegB_12x());
Ian Rogers28ad40d2011-10-27 15:19:26 -07001732 if (res_type.IsReferenceTypes()) {
Ian Rogers89310de2012-02-01 13:47:30 -08001733 if (!res_type.IsArrayTypes() && !res_type.IsZero()) { // ie not an array or null
jeffhaod5347e02012-03-22 17:25:05 -07001734 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001735 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001736 work_line_->SetRegisterType(inst->VRegA_12x(), reg_types_.Integer());
Ian Rogersd81871c2011-10-03 13:57:23 -07001737 }
1738 }
1739 break;
1740 }
1741 case Instruction::NEW_INSTANCE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001742 const RegType& res_type = ResolveClassAndCheckAccess(inst->VRegB_21c());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001743 if (res_type.IsConflict()) {
1744 DCHECK_NE(failures_.size(), 0U);
1745 break; // bad class
jeffhao8cd6dda2012-02-22 10:15:34 -08001746 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001747 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
1748 // can't create an instance of an interface or abstract class */
1749 if (!res_type.IsInstantiableTypes()) {
1750 Fail(VERIFY_ERROR_INSTANTIATION)
1751 << "new-instance on primitive, interface or abstract class" << res_type;
Ian Rogers08f753d2012-08-24 14:35:25 -07001752 // Soft failure so carry on to set register type.
Ian Rogersd81871c2011-10-03 13:57:23 -07001753 }
Ian Rogers08f753d2012-08-24 14:35:25 -07001754 const RegType& uninit_type = reg_types_.Uninitialized(res_type, work_insn_idx_);
1755 // Any registers holding previous allocations from this address that have not yet been
1756 // initialized must be marked invalid.
1757 work_line_->MarkUninitRefsAsInvalid(uninit_type);
1758 // add the new uninitialized reference to the register state
Sebastien Hertz5243e912013-05-21 10:55:07 +02001759 work_line_->SetRegisterType(inst->VRegA_21c(), uninit_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001760 break;
1761 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08001762 case Instruction::NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001763 VerifyNewArray(inst, false, false);
jeffhaobdb76512011-09-07 11:43:16 -07001764 break;
1765 case Instruction::FILLED_NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001766 VerifyNewArray(inst, true, false);
Ian Rogers0c4a5062012-02-03 15:18:59 -08001767 just_set_result = true; // Filled new array sets result register
jeffhaobdb76512011-09-07 11:43:16 -07001768 break;
Ian Rogers0c4a5062012-02-03 15:18:59 -08001769 case Instruction::FILLED_NEW_ARRAY_RANGE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001770 VerifyNewArray(inst, true, true);
Ian Rogers0c4a5062012-02-03 15:18:59 -08001771 just_set_result = true; // Filled new array range sets result register
1772 break;
jeffhaobdb76512011-09-07 11:43:16 -07001773 case Instruction::CMPL_FLOAT:
1774 case Instruction::CMPG_FLOAT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001775 if (!work_line_->VerifyRegisterType(inst->VRegB_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08001776 break;
1777 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02001778 if (!work_line_->VerifyRegisterType(inst->VRegC_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08001779 break;
1780 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02001781 work_line_->SetRegisterType(inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001782 break;
1783 case Instruction::CMPL_DOUBLE:
1784 case Instruction::CMPG_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001785 if (!work_line_->VerifyRegisterTypeWide(inst->VRegB_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001786 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001787 break;
1788 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02001789 if (!work_line_->VerifyRegisterTypeWide(inst->VRegC_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001790 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001791 break;
1792 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02001793 work_line_->SetRegisterType(inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001794 break;
1795 case Instruction::CMP_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001796 if (!work_line_->VerifyRegisterTypeWide(inst->VRegB_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001797 reg_types_.LongHi())) {
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_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001801 reg_types_.LongHi())) {
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;
Ian Rogersd81871c2011-10-03 13:57:23 -07001806 case Instruction::THROW: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001807 const RegType& res_type = work_line_->GetRegisterType(inst->VRegA_11x());
Ian Rogersb4903572012-10-11 11:52:56 -07001808 if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(res_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07001809 Fail(res_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS : VERIFY_ERROR_BAD_CLASS_SOFT)
1810 << "thrown class " << res_type << " not instanceof Throwable";
jeffhaobdb76512011-09-07 11:43:16 -07001811 }
1812 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001813 }
jeffhaobdb76512011-09-07 11:43:16 -07001814 case Instruction::GOTO:
1815 case Instruction::GOTO_16:
1816 case Instruction::GOTO_32:
1817 /* no effect on or use of registers */
1818 break;
1819
1820 case Instruction::PACKED_SWITCH:
1821 case Instruction::SPARSE_SWITCH:
1822 /* verify that vAA is an integer, or can be converted to one */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001823 work_line_->VerifyRegisterType(inst->VRegA_31t(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001824 break;
1825
Ian Rogersd81871c2011-10-03 13:57:23 -07001826 case Instruction::FILL_ARRAY_DATA: {
1827 /* Similar to the verification done for APUT */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001828 const RegType& array_type = work_line_->GetRegisterType(inst->VRegA_31t());
Ian Rogers89310de2012-02-01 13:47:30 -08001829 /* array_type can be null if the reg type is Zero */
1830 if (!array_type.IsZero()) {
jeffhao457cc512012-02-02 16:55:13 -08001831 if (!array_type.IsArrayTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001832 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with array type "
1833 << array_type;
Ian Rogers89310de2012-02-01 13:47:30 -08001834 } else {
Mathieu Chartier590fee92013-09-13 13:46:47 -07001835 const RegType& component_type = reg_types_.GetComponentType(array_type,
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001836 class_loader_->Get());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001837 DCHECK(!component_type.IsConflict());
jeffhao457cc512012-02-02 16:55:13 -08001838 if (component_type.IsNonZeroReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001839 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with component type "
1840 << component_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001841 } else {
jeffhao457cc512012-02-02 16:55:13 -08001842 // Now verify if the element width in the table matches the element width declared in
1843 // the array
1844 const uint16_t* array_data = insns + (insns[1] | (((int32_t) insns[2]) << 16));
1845 if (array_data[0] != Instruction::kArrayDataSignature) {
jeffhaod5347e02012-03-22 17:25:05 -07001846 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid magic for array-data";
jeffhao457cc512012-02-02 16:55:13 -08001847 } else {
1848 size_t elem_width = Primitive::ComponentSize(component_type.GetPrimitiveType());
1849 // Since we don't compress the data in Dex, expect to see equal width of data stored
1850 // in the table and expected from the array class.
1851 if (array_data[1] != elem_width) {
jeffhaod5347e02012-03-22 17:25:05 -07001852 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-data size mismatch (" << array_data[1]
1853 << " vs " << elem_width << ")";
jeffhao457cc512012-02-02 16:55:13 -08001854 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001855 }
1856 }
jeffhaobdb76512011-09-07 11:43:16 -07001857 }
1858 }
1859 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001860 }
jeffhaobdb76512011-09-07 11:43:16 -07001861 case Instruction::IF_EQ:
Ian Rogersd81871c2011-10-03 13:57:23 -07001862 case Instruction::IF_NE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001863 const RegType& reg_type1 = work_line_->GetRegisterType(inst->VRegA_22t());
1864 const RegType& reg_type2 = work_line_->GetRegisterType(inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07001865 bool mismatch = false;
1866 if (reg_type1.IsZero()) { // zero then integral or reference expected
1867 mismatch = !reg_type2.IsReferenceTypes() && !reg_type2.IsIntegralTypes();
1868 } else if (reg_type1.IsReferenceTypes()) { // both references?
1869 mismatch = !reg_type2.IsReferenceTypes();
1870 } else { // both integral?
1871 mismatch = !reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes();
1872 }
1873 if (mismatch) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001874 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to if-eq/if-ne (" << reg_type1 << ","
1875 << reg_type2 << ") must both be references or integral";
jeffhaobdb76512011-09-07 11:43:16 -07001876 }
1877 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001878 }
jeffhaobdb76512011-09-07 11:43:16 -07001879 case Instruction::IF_LT:
1880 case Instruction::IF_GE:
1881 case Instruction::IF_GT:
Ian Rogersd81871c2011-10-03 13:57:23 -07001882 case Instruction::IF_LE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001883 const RegType& reg_type1 = work_line_->GetRegisterType(inst->VRegA_22t());
1884 const RegType& reg_type2 = work_line_->GetRegisterType(inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07001885 if (!reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001886 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to 'if' (" << reg_type1 << ","
1887 << reg_type2 << ") must be integral";
jeffhaobdb76512011-09-07 11:43:16 -07001888 }
1889 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001890 }
jeffhaobdb76512011-09-07 11:43:16 -07001891 case Instruction::IF_EQZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07001892 case Instruction::IF_NEZ: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001893 const RegType& reg_type = work_line_->GetRegisterType(inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07001894 if (!reg_type.IsReferenceTypes() && !reg_type.IsIntegralTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001895 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
1896 << " unexpected as arg to if-eqz/if-nez";
Ian Rogersd81871c2011-10-03 13:57:23 -07001897 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001898
1899 // Find previous instruction - its existence is a precondition to peephole optimization.
Ian Rogers9b360392013-06-06 14:45:07 -07001900 uint32_t instance_of_idx = 0;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001901 if (0 != work_insn_idx_) {
Ian Rogers9b360392013-06-06 14:45:07 -07001902 instance_of_idx = work_insn_idx_ - 1;
Brian Carlstromdf629502013-07-17 22:39:56 -07001903 while (0 != instance_of_idx && !insn_flags_[instance_of_idx].IsOpcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07001904 instance_of_idx--;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001905 }
Ian Rogers9b360392013-06-06 14:45:07 -07001906 CHECK(insn_flags_[instance_of_idx].IsOpcode());
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001907 } else {
1908 break;
1909 }
1910
Ian Rogers9b360392013-06-06 14:45:07 -07001911 const Instruction* instance_of_inst = Instruction::At(code_item_->insns_ + instance_of_idx);
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001912
1913 /* Check for peep-hole pattern of:
1914 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07001915 * instance-of vX, vY, T;
1916 * ifXXX vX, label ;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001917 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07001918 * label:
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001919 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07001920 * and sharpen the type of vY to be type T.
1921 * Note, this pattern can't be if:
1922 * - if there are other branches to this branch,
1923 * - when vX == vY.
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001924 */
Ian Rogersfae370a2013-06-05 08:33:27 -07001925 if (!CurrentInsnFlags()->IsBranchTarget() &&
Ian Rogers9b360392013-06-06 14:45:07 -07001926 (Instruction::INSTANCE_OF == instance_of_inst->Opcode()) &&
1927 (inst->VRegA_21t() == instance_of_inst->VRegA_22c()) &&
1928 (instance_of_inst->VRegA_22c() != instance_of_inst->VRegB_22c())) {
Ian Rogersfae370a2013-06-05 08:33:27 -07001929 // Check that the we are not attempting conversion to interface types,
1930 // which is not done because of the multiple inheritance implications.
Jeff Haoc642ec82013-09-04 16:11:55 -07001931 // Also don't change the type if it would result in an upcast.
1932 const RegType& orig_type = work_line_->GetRegisterType(instance_of_inst->VRegB_22c());
Ian Rogers9b360392013-06-06 14:45:07 -07001933 const RegType& cast_type = ResolveClassAndCheckAccess(instance_of_inst->VRegC_22c());
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001934
Jeff Haoa3faaf42013-09-03 19:07:00 -07001935 if (!cast_type.IsUnresolvedTypes() && !orig_type.IsUnresolvedTypes() &&
1936 !cast_type.GetClass()->IsInterface() && !cast_type.IsAssignableFrom(orig_type)) {
Ian Rogersd0fbd852013-09-24 18:17:04 -07001937 RegisterLine* update_line = RegisterLine::Create(code_item_->registers_size_, this);
Ian Rogersfae370a2013-06-05 08:33:27 -07001938 if (inst->Opcode() == Instruction::IF_EQZ) {
Ian Rogers9b360392013-06-06 14:45:07 -07001939 fallthrough_line.reset(update_line);
Ian Rogersfae370a2013-06-05 08:33:27 -07001940 } else {
Ian Rogers9b360392013-06-06 14:45:07 -07001941 branch_line.reset(update_line);
1942 }
1943 update_line->CopyFromLine(work_line_.get());
1944 update_line->SetRegisterType(instance_of_inst->VRegB_22c(), cast_type);
1945 if (!insn_flags_[instance_of_idx].IsBranchTarget() && 0 != instance_of_idx) {
1946 // See if instance-of was preceded by a move-object operation, common due to the small
1947 // register encoding space of instance-of, and propagate type information to the source
1948 // of the move-object.
1949 uint32_t move_idx = instance_of_idx - 1;
Brian Carlstromdf629502013-07-17 22:39:56 -07001950 while (0 != move_idx && !insn_flags_[move_idx].IsOpcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07001951 move_idx--;
1952 }
1953 CHECK(insn_flags_[move_idx].IsOpcode());
1954 const Instruction* move_inst = Instruction::At(code_item_->insns_ + move_idx);
1955 switch (move_inst->Opcode()) {
1956 case Instruction::MOVE_OBJECT:
1957 if (move_inst->VRegA_12x() == instance_of_inst->VRegB_22c()) {
1958 update_line->SetRegisterType(move_inst->VRegB_12x(), cast_type);
1959 }
1960 break;
1961 case Instruction::MOVE_OBJECT_FROM16:
1962 if (move_inst->VRegA_22x() == instance_of_inst->VRegB_22c()) {
1963 update_line->SetRegisterType(move_inst->VRegB_22x(), cast_type);
1964 }
1965 break;
1966 case Instruction::MOVE_OBJECT_16:
1967 if (move_inst->VRegA_32x() == instance_of_inst->VRegB_22c()) {
1968 update_line->SetRegisterType(move_inst->VRegB_32x(), cast_type);
1969 }
1970 break;
1971 default:
1972 break;
1973 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001974 }
1975 }
1976 }
1977
jeffhaobdb76512011-09-07 11:43:16 -07001978 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001979 }
jeffhaobdb76512011-09-07 11:43:16 -07001980 case Instruction::IF_LTZ:
1981 case Instruction::IF_GEZ:
1982 case Instruction::IF_GTZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07001983 case Instruction::IF_LEZ: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001984 const RegType& reg_type = work_line_->GetRegisterType(inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07001985 if (!reg_type.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001986 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
1987 << " unexpected as arg to if-ltz/if-gez/if-gtz/if-lez";
Ian Rogersd81871c2011-10-03 13:57:23 -07001988 }
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::AGET_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001992 VerifyAGet(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001993 break;
jeffhaobdb76512011-09-07 11:43:16 -07001994 case Instruction::AGET_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001995 VerifyAGet(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001996 break;
jeffhaobdb76512011-09-07 11:43:16 -07001997 case Instruction::AGET_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001998 VerifyAGet(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001999 break;
jeffhaobdb76512011-09-07 11:43:16 -07002000 case Instruction::AGET_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002001 VerifyAGet(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002002 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002003 case Instruction::AGET:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002004 VerifyAGet(inst, reg_types_.Integer(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002005 break;
jeffhaobdb76512011-09-07 11:43:16 -07002006 case Instruction::AGET_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002007 VerifyAGet(inst, reg_types_.LongLo(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002008 break;
2009 case Instruction::AGET_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002010 VerifyAGet(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07002011 break;
2012
Ian Rogersd81871c2011-10-03 13:57:23 -07002013 case Instruction::APUT_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002014 VerifyAPut(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002015 break;
2016 case Instruction::APUT_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002017 VerifyAPut(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002018 break;
2019 case Instruction::APUT_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002020 VerifyAPut(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002021 break;
2022 case Instruction::APUT_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002023 VerifyAPut(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002024 break;
2025 case Instruction::APUT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002026 VerifyAPut(inst, reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002027 break;
2028 case Instruction::APUT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002029 VerifyAPut(inst, reg_types_.LongLo(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002030 break;
2031 case Instruction::APUT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002032 VerifyAPut(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07002033 break;
2034
jeffhaobdb76512011-09-07 11:43:16 -07002035 case Instruction::IGET_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002036 VerifyISGet(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002037 break;
jeffhaobdb76512011-09-07 11:43:16 -07002038 case Instruction::IGET_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002039 VerifyISGet(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002040 break;
jeffhaobdb76512011-09-07 11:43:16 -07002041 case Instruction::IGET_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002042 VerifyISGet(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002043 break;
jeffhaobdb76512011-09-07 11:43:16 -07002044 case Instruction::IGET_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002045 VerifyISGet(inst, reg_types_.Short(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002046 break;
2047 case Instruction::IGET:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002048 VerifyISGet(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002049 break;
2050 case Instruction::IGET_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002051 VerifyISGet(inst, reg_types_.LongLo(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002052 break;
2053 case Instruction::IGET_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002054 VerifyISGet(inst, reg_types_.JavaLangObject(false), false, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002055 break;
jeffhaobdb76512011-09-07 11:43:16 -07002056
Ian Rogersd81871c2011-10-03 13:57:23 -07002057 case Instruction::IPUT_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002058 VerifyISPut(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002059 break;
2060 case Instruction::IPUT_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002061 VerifyISPut(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002062 break;
2063 case Instruction::IPUT_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002064 VerifyISPut(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002065 break;
2066 case Instruction::IPUT_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002067 VerifyISPut(inst, reg_types_.Short(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002068 break;
2069 case Instruction::IPUT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002070 VerifyISPut(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002071 break;
2072 case Instruction::IPUT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002073 VerifyISPut(inst, reg_types_.LongLo(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002074 break;
jeffhaobdb76512011-09-07 11:43:16 -07002075 case Instruction::IPUT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002076 VerifyISPut(inst, reg_types_.JavaLangObject(false), false, false);
jeffhaobdb76512011-09-07 11:43:16 -07002077 break;
2078
jeffhaobdb76512011-09-07 11:43:16 -07002079 case Instruction::SGET_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002080 VerifyISGet(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002081 break;
jeffhaobdb76512011-09-07 11:43:16 -07002082 case Instruction::SGET_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002083 VerifyISGet(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002084 break;
jeffhaobdb76512011-09-07 11:43:16 -07002085 case Instruction::SGET_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002086 VerifyISGet(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002087 break;
jeffhaobdb76512011-09-07 11:43:16 -07002088 case Instruction::SGET_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002089 VerifyISGet(inst, reg_types_.Short(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002090 break;
2091 case Instruction::SGET:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002092 VerifyISGet(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002093 break;
2094 case Instruction::SGET_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002095 VerifyISGet(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002096 break;
2097 case Instruction::SGET_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002098 VerifyISGet(inst, reg_types_.JavaLangObject(false), false, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002099 break;
2100
2101 case Instruction::SPUT_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002102 VerifyISPut(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002103 break;
2104 case Instruction::SPUT_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002105 VerifyISPut(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002106 break;
2107 case Instruction::SPUT_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002108 VerifyISPut(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002109 break;
2110 case Instruction::SPUT_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002111 VerifyISPut(inst, reg_types_.Short(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002112 break;
2113 case Instruction::SPUT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002114 VerifyISPut(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002115 break;
2116 case Instruction::SPUT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002117 VerifyISPut(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002118 break;
2119 case Instruction::SPUT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002120 VerifyISPut(inst, reg_types_.JavaLangObject(false), false, true);
jeffhaobdb76512011-09-07 11:43:16 -07002121 break;
2122
2123 case Instruction::INVOKE_VIRTUAL:
2124 case Instruction::INVOKE_VIRTUAL_RANGE:
2125 case Instruction::INVOKE_SUPER:
Ian Rogersd81871c2011-10-03 13:57:23 -07002126 case Instruction::INVOKE_SUPER_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002127 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE ||
2128 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
2129 bool is_super = (inst->Opcode() == Instruction::INVOKE_SUPER ||
2130 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
Brian Carlstromea46f952013-07-30 01:26:50 -07002131 mirror::ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_VIRTUAL,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002132 is_range, is_super);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002133 const RegType* return_type = nullptr;
2134 if (called_method != nullptr) {
2135 MethodHelper mh(called_method);
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08002136 mirror::Class* return_type_class = mh.GetReturnType(can_load_classes_);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002137 if (return_type_class != nullptr) {
2138 return_type = &reg_types_.FromClass(mh.GetReturnTypeDescriptor(), return_type_class,
2139 return_type_class->CannotBeAssignedFromOtherTypes());
2140 } else {
2141 Thread* self = Thread::Current();
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08002142 DCHECK(!can_load_classes_ || self->IsExceptionPending());
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002143 self->ClearException();
2144 }
2145 }
2146 if (return_type == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002147 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002148 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2149 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002150 const char* descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07002151 return_type = &reg_types_.FromDescriptor(class_loader_->Get(), descriptor, false);
jeffhaobdb76512011-09-07 11:43:16 -07002152 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002153 if (!return_type->IsLowHalf()) {
2154 work_line_->SetResultRegisterType(*return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002155 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002156 work_line_->SetResultRegisterTypeWide(*return_type, return_type->HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002157 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002158 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002159 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002160 }
jeffhaobdb76512011-09-07 11:43:16 -07002161 case Instruction::INVOKE_DIRECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002162 case Instruction::INVOKE_DIRECT_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002163 bool is_range = (inst->Opcode() == Instruction::INVOKE_DIRECT_RANGE);
Brian Carlstromea46f952013-07-30 01:26:50 -07002164 mirror::ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_DIRECT,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002165 is_range, false);
Ian Rogers46685432012-06-03 22:26:43 -07002166 const char* return_type_descriptor;
2167 bool is_constructor;
2168 if (called_method == NULL) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002169 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogers46685432012-06-03 22:26:43 -07002170 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
Ian Rogersdfb325e2013-10-30 01:00:44 -07002171 is_constructor = strcmp("<init>", dex_file_->StringDataByIdx(method_id.name_idx_)) == 0;
Ian Rogers46685432012-06-03 22:26:43 -07002172 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
2173 return_type_descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2174 } else {
2175 is_constructor = called_method->IsConstructor();
2176 return_type_descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
2177 }
2178 if (is_constructor) {
jeffhaobdb76512011-09-07 11:43:16 -07002179 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002180 * Some additional checks when calling a constructor. We know from the invocation arg check
2181 * that the "this" argument is an instance of called_method->klass. Now we further restrict
2182 * that to require that called_method->klass is the same as this->klass or this->super,
2183 * allowing the latter only if the "this" argument is the same as the "this" argument to
2184 * this method (which implies that we're in a constructor ourselves).
jeffhaobdb76512011-09-07 11:43:16 -07002185 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002186 const RegType& this_type = work_line_->GetInvocationThis(inst, is_range);
jeffhaob57e9522012-04-26 18:08:21 -07002187 if (this_type.IsConflict()) // failure.
2188 break;
jeffhaobdb76512011-09-07 11:43:16 -07002189
jeffhaob57e9522012-04-26 18:08:21 -07002190 /* no null refs allowed (?) */
2191 if (this_type.IsZero()) {
2192 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unable to initialize null ref";
2193 break;
jeffhao2a8a90e2011-09-26 14:25:31 -07002194 }
jeffhaob57e9522012-04-26 18:08:21 -07002195
2196 /* must be in same class or in superclass */
Ian Rogers46685432012-06-03 22:26:43 -07002197 // const RegType& this_super_klass = this_type.GetSuperClass(&reg_types_);
2198 // TODO: re-enable constructor type verification
2199 // if (this_super_klass.IsConflict()) {
jeffhaob57e9522012-04-26 18:08:21 -07002200 // Unknown super class, fail so we re-check at runtime.
Ian Rogers46685432012-06-03 22:26:43 -07002201 // Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "super class unknown for '" << this_type << "'";
2202 // break;
2203 // }
jeffhaob57e9522012-04-26 18:08:21 -07002204
2205 /* arg must be an uninitialized reference */
2206 if (!this_type.IsUninitializedTypes()) {
2207 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Expected initialization on uninitialized reference "
2208 << this_type;
2209 break;
2210 }
2211
2212 /*
2213 * Replace the uninitialized reference with an initialized one. We need to do this for all
2214 * registers that have the same object instance in them, not just the "this" register.
2215 */
2216 work_line_->MarkRefsAsInitialized(this_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002217 }
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07002218 const RegType& return_type = reg_types_.FromDescriptor(class_loader_->Get(),
Mathieu Chartier590fee92013-09-13 13:46:47 -07002219 return_type_descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002220 if (!return_type.IsLowHalf()) {
2221 work_line_->SetResultRegisterType(return_type);
2222 } else {
2223 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2224 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002225 just_set_result = true;
2226 break;
2227 }
2228 case Instruction::INVOKE_STATIC:
2229 case Instruction::INVOKE_STATIC_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002230 bool is_range = (inst->Opcode() == Instruction::INVOKE_STATIC_RANGE);
Brian Carlstromea46f952013-07-30 01:26:50 -07002231 mirror::ArtMethod* called_method = VerifyInvocationArgs(inst,
Brian Carlstrom93c33962013-07-26 10:37:43 -07002232 METHOD_STATIC,
2233 is_range,
2234 false);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002235 const char* descriptor;
2236 if (called_method == NULL) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002237 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002238 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2239 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Mathieu Chartier590fee92013-09-13 13:46:47 -07002240 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002241 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002242 descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002243 }
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07002244 const RegType& return_type = reg_types_.FromDescriptor(class_loader_->Get(), descriptor,
Mathieu Chartier590fee92013-09-13 13:46:47 -07002245 false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002246 if (!return_type.IsLowHalf()) {
2247 work_line_->SetResultRegisterType(return_type);
2248 } else {
2249 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2250 }
jeffhaobdb76512011-09-07 11:43:16 -07002251 just_set_result = true;
2252 }
2253 break;
jeffhaobdb76512011-09-07 11:43:16 -07002254 case Instruction::INVOKE_INTERFACE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002255 case Instruction::INVOKE_INTERFACE_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002256 bool is_range = (inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE);
Brian Carlstromea46f952013-07-30 01:26:50 -07002257 mirror::ArtMethod* abs_method = VerifyInvocationArgs(inst,
Brian Carlstrom93c33962013-07-26 10:37:43 -07002258 METHOD_INTERFACE,
2259 is_range,
2260 false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002261 if (abs_method != NULL) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002262 mirror::Class* called_interface = abs_method->GetDeclaringClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002263 if (!called_interface->IsInterface() && !called_interface->IsObjectClass()) {
2264 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected interface class in invoke-interface '"
2265 << PrettyMethod(abs_method) << "'";
2266 break;
Ian Rogers28ad40d2011-10-27 15:19:26 -07002267 }
Ian Rogers0d604842012-04-16 14:50:24 -07002268 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002269 /* Get the type of the "this" arg, which should either be a sub-interface of called
2270 * interface or Object (see comments in RegType::JoinClass).
2271 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002272 const RegType& this_type = work_line_->GetInvocationThis(inst, is_range);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002273 if (this_type.IsZero()) {
2274 /* null pointer always passes (and always fails at runtime) */
2275 } else {
2276 if (this_type.IsUninitializedTypes()) {
2277 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface call on uninitialized object "
2278 << this_type;
2279 break;
2280 }
2281 // In the past we have tried to assert that "called_interface" is assignable
2282 // from "this_type.GetClass()", however, as we do an imprecise Join
2283 // (RegType::JoinClass) we don't have full information on what interfaces are
2284 // implemented by "this_type". For example, two classes may implement the same
2285 // interfaces and have a common parent that doesn't implement the interface. The
2286 // join will set "this_type" to the parent class and a test that this implements
2287 // the interface will incorrectly fail.
2288 }
2289 /*
2290 * We don't have an object instance, so we can't find the concrete method. However, all of
2291 * the type information is in the abstract method, so we're good.
2292 */
2293 const char* descriptor;
2294 if (abs_method == NULL) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002295 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002296 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2297 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
2298 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2299 } else {
2300 descriptor = MethodHelper(abs_method).GetReturnTypeDescriptor();
2301 }
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07002302 const RegType& return_type = reg_types_.FromDescriptor(class_loader_->Get(), descriptor,
Mathieu Chartier590fee92013-09-13 13:46:47 -07002303 false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002304 if (!return_type.IsLowHalf()) {
2305 work_line_->SetResultRegisterType(return_type);
2306 } else {
2307 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2308 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002309 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002310 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002311 }
jeffhaobdb76512011-09-07 11:43:16 -07002312 case Instruction::NEG_INT:
2313 case Instruction::NOT_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002314 work_line_->CheckUnaryOp(inst, reg_types_.Integer(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002315 break;
2316 case Instruction::NEG_LONG:
2317 case Instruction::NOT_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002318 work_line_->CheckUnaryOpWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002319 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002320 break;
2321 case Instruction::NEG_FLOAT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002322 work_line_->CheckUnaryOp(inst, reg_types_.Float(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002323 break;
2324 case Instruction::NEG_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002325 work_line_->CheckUnaryOpWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002326 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002327 break;
2328 case Instruction::INT_TO_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002329 work_line_->CheckUnaryOpToWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002330 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002331 break;
2332 case Instruction::INT_TO_FLOAT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002333 work_line_->CheckUnaryOp(inst, reg_types_.Float(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002334 break;
2335 case Instruction::INT_TO_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002336 work_line_->CheckUnaryOpToWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002337 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002338 break;
2339 case Instruction::LONG_TO_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002340 work_line_->CheckUnaryOpFromWide(inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002341 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002342 break;
2343 case Instruction::LONG_TO_FLOAT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002344 work_line_->CheckUnaryOpFromWide(inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002345 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002346 break;
2347 case Instruction::LONG_TO_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002348 work_line_->CheckUnaryOpWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002349 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002350 break;
2351 case Instruction::FLOAT_TO_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002352 work_line_->CheckUnaryOp(inst, reg_types_.Integer(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002353 break;
2354 case Instruction::FLOAT_TO_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002355 work_line_->CheckUnaryOpToWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002356 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002357 break;
2358 case Instruction::FLOAT_TO_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002359 work_line_->CheckUnaryOpToWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002360 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002361 break;
2362 case Instruction::DOUBLE_TO_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002363 work_line_->CheckUnaryOpFromWide(inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002364 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002365 break;
2366 case Instruction::DOUBLE_TO_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002367 work_line_->CheckUnaryOpWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002368 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002369 break;
2370 case Instruction::DOUBLE_TO_FLOAT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002371 work_line_->CheckUnaryOpFromWide(inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002372 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002373 break;
2374 case Instruction::INT_TO_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002375 work_line_->CheckUnaryOp(inst, reg_types_.Byte(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002376 break;
2377 case Instruction::INT_TO_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002378 work_line_->CheckUnaryOp(inst, reg_types_.Char(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002379 break;
2380 case Instruction::INT_TO_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002381 work_line_->CheckUnaryOp(inst, reg_types_.Short(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002382 break;
2383
2384 case Instruction::ADD_INT:
2385 case Instruction::SUB_INT:
2386 case Instruction::MUL_INT:
2387 case Instruction::REM_INT:
2388 case Instruction::DIV_INT:
2389 case Instruction::SHL_INT:
2390 case Instruction::SHR_INT:
2391 case Instruction::USHR_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002392 work_line_->CheckBinaryOp(inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002393 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002394 break;
2395 case Instruction::AND_INT:
2396 case Instruction::OR_INT:
2397 case Instruction::XOR_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002398 work_line_->CheckBinaryOp(inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002399 reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002400 break;
2401 case Instruction::ADD_LONG:
2402 case Instruction::SUB_LONG:
2403 case Instruction::MUL_LONG:
2404 case Instruction::DIV_LONG:
2405 case Instruction::REM_LONG:
2406 case Instruction::AND_LONG:
2407 case Instruction::OR_LONG:
2408 case Instruction::XOR_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002409 work_line_->CheckBinaryOpWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002410 reg_types_.LongLo(), reg_types_.LongHi(),
2411 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002412 break;
2413 case Instruction::SHL_LONG:
2414 case Instruction::SHR_LONG:
2415 case Instruction::USHR_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002416 /* shift distance is Int, making these different from other binary operations */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002417 work_line_->CheckBinaryOpWideShift(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002418 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002419 break;
2420 case Instruction::ADD_FLOAT:
2421 case Instruction::SUB_FLOAT:
2422 case Instruction::MUL_FLOAT:
2423 case Instruction::DIV_FLOAT:
2424 case Instruction::REM_FLOAT:
Brian Carlstrom93c33962013-07-26 10:37:43 -07002425 work_line_->CheckBinaryOp(inst,
2426 reg_types_.Float(),
2427 reg_types_.Float(),
2428 reg_types_.Float(),
2429 false);
jeffhaobdb76512011-09-07 11:43:16 -07002430 break;
2431 case Instruction::ADD_DOUBLE:
2432 case Instruction::SUB_DOUBLE:
2433 case Instruction::MUL_DOUBLE:
2434 case Instruction::DIV_DOUBLE:
2435 case Instruction::REM_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002436 work_line_->CheckBinaryOpWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002437 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2438 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002439 break;
2440 case Instruction::ADD_INT_2ADDR:
2441 case Instruction::SUB_INT_2ADDR:
2442 case Instruction::MUL_INT_2ADDR:
2443 case Instruction::REM_INT_2ADDR:
2444 case Instruction::SHL_INT_2ADDR:
2445 case Instruction::SHR_INT_2ADDR:
2446 case Instruction::USHR_INT_2ADDR:
Brian Carlstrom93c33962013-07-26 10:37:43 -07002447 work_line_->CheckBinaryOp2addr(inst,
2448 reg_types_.Integer(),
2449 reg_types_.Integer(),
2450 reg_types_.Integer(),
2451 false);
jeffhaobdb76512011-09-07 11:43:16 -07002452 break;
2453 case Instruction::AND_INT_2ADDR:
2454 case Instruction::OR_INT_2ADDR:
2455 case Instruction::XOR_INT_2ADDR:
Brian Carlstrom93c33962013-07-26 10:37:43 -07002456 work_line_->CheckBinaryOp2addr(inst,
2457 reg_types_.Integer(),
2458 reg_types_.Integer(),
2459 reg_types_.Integer(),
2460 true);
jeffhaobdb76512011-09-07 11:43:16 -07002461 break;
2462 case Instruction::DIV_INT_2ADDR:
Brian Carlstrom93c33962013-07-26 10:37:43 -07002463 work_line_->CheckBinaryOp2addr(inst,
2464 reg_types_.Integer(),
2465 reg_types_.Integer(),
2466 reg_types_.Integer(),
2467 false);
jeffhaobdb76512011-09-07 11:43:16 -07002468 break;
2469 case Instruction::ADD_LONG_2ADDR:
2470 case Instruction::SUB_LONG_2ADDR:
2471 case Instruction::MUL_LONG_2ADDR:
2472 case Instruction::DIV_LONG_2ADDR:
2473 case Instruction::REM_LONG_2ADDR:
2474 case Instruction::AND_LONG_2ADDR:
2475 case Instruction::OR_LONG_2ADDR:
2476 case Instruction::XOR_LONG_2ADDR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002477 work_line_->CheckBinaryOp2addrWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002478 reg_types_.LongLo(), reg_types_.LongHi(),
2479 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002480 break;
2481 case Instruction::SHL_LONG_2ADDR:
2482 case Instruction::SHR_LONG_2ADDR:
2483 case Instruction::USHR_LONG_2ADDR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002484 work_line_->CheckBinaryOp2addrWideShift(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002485 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002486 break;
2487 case Instruction::ADD_FLOAT_2ADDR:
2488 case Instruction::SUB_FLOAT_2ADDR:
2489 case Instruction::MUL_FLOAT_2ADDR:
2490 case Instruction::DIV_FLOAT_2ADDR:
2491 case Instruction::REM_FLOAT_2ADDR:
Brian Carlstrom93c33962013-07-26 10:37:43 -07002492 work_line_->CheckBinaryOp2addr(inst,
2493 reg_types_.Float(),
2494 reg_types_.Float(),
2495 reg_types_.Float(),
2496 false);
jeffhaobdb76512011-09-07 11:43:16 -07002497 break;
2498 case Instruction::ADD_DOUBLE_2ADDR:
2499 case Instruction::SUB_DOUBLE_2ADDR:
2500 case Instruction::MUL_DOUBLE_2ADDR:
2501 case Instruction::DIV_DOUBLE_2ADDR:
2502 case Instruction::REM_DOUBLE_2ADDR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002503 work_line_->CheckBinaryOp2addrWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002504 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2505 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002506 break;
2507 case Instruction::ADD_INT_LIT16:
2508 case Instruction::RSUB_INT:
2509 case Instruction::MUL_INT_LIT16:
2510 case Instruction::DIV_INT_LIT16:
2511 case Instruction::REM_INT_LIT16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002512 work_line_->CheckLiteralOp(inst, reg_types_.Integer(), reg_types_.Integer(), false, true);
jeffhaobdb76512011-09-07 11:43:16 -07002513 break;
2514 case Instruction::AND_INT_LIT16:
2515 case Instruction::OR_INT_LIT16:
2516 case Instruction::XOR_INT_LIT16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002517 work_line_->CheckLiteralOp(inst, reg_types_.Integer(), reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002518 break;
2519 case Instruction::ADD_INT_LIT8:
2520 case Instruction::RSUB_INT_LIT8:
2521 case Instruction::MUL_INT_LIT8:
2522 case Instruction::DIV_INT_LIT8:
2523 case Instruction::REM_INT_LIT8:
2524 case Instruction::SHL_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002525 case Instruction::SHR_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002526 case Instruction::USHR_INT_LIT8:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002527 work_line_->CheckLiteralOp(inst, reg_types_.Integer(), reg_types_.Integer(), false, false);
jeffhaobdb76512011-09-07 11:43:16 -07002528 break;
2529 case Instruction::AND_INT_LIT8:
2530 case Instruction::OR_INT_LIT8:
2531 case Instruction::XOR_INT_LIT8:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002532 work_line_->CheckLiteralOp(inst, reg_types_.Integer(), reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002533 break;
2534
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002535 // Special instructions.
Sebastien Hertzcc10e0e2013-06-28 14:24:48 +02002536 case Instruction::RETURN_VOID_BARRIER:
Ian Rogers9fc16eb2013-07-31 14:49:16 -07002537 if (!IsConstructor() || IsStatic()) {
Sebastien Hertzcc10e0e2013-06-28 14:24:48 +02002538 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void-barrier not expected";
2539 }
2540 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002541 // Note: the following instructions encode offsets derived from class linking.
2542 // As such they use Class*/Field*/AbstractMethod* as these offsets only have
2543 // meaning if the class linking and resolution were successful.
2544 case Instruction::IGET_QUICK:
2545 VerifyIGetQuick(inst, reg_types_.Integer(), true);
2546 break;
2547 case Instruction::IGET_WIDE_QUICK:
2548 VerifyIGetQuick(inst, reg_types_.LongLo(), true);
2549 break;
2550 case Instruction::IGET_OBJECT_QUICK:
2551 VerifyIGetQuick(inst, reg_types_.JavaLangObject(false), false);
2552 break;
2553 case Instruction::IPUT_QUICK:
2554 VerifyIPutQuick(inst, reg_types_.Integer(), true);
2555 break;
2556 case Instruction::IPUT_WIDE_QUICK:
2557 VerifyIPutQuick(inst, reg_types_.LongLo(), true);
2558 break;
2559 case Instruction::IPUT_OBJECT_QUICK:
2560 VerifyIPutQuick(inst, reg_types_.JavaLangObject(false), false);
2561 break;
2562 case Instruction::INVOKE_VIRTUAL_QUICK:
2563 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: {
2564 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
Brian Carlstromea46f952013-07-30 01:26:50 -07002565 mirror::ArtMethod* called_method = VerifyInvokeVirtualQuickArgs(inst, is_range);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002566 if (called_method != NULL) {
2567 const char* descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07002568 const RegType& return_type = reg_types_.FromDescriptor(class_loader_->Get(), descriptor,
Mathieu Chartier590fee92013-09-13 13:46:47 -07002569 false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002570 if (!return_type.IsLowHalf()) {
2571 work_line_->SetResultRegisterType(return_type);
2572 } else {
2573 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2574 }
2575 just_set_result = true;
2576 }
2577 break;
2578 }
2579
Ian Rogersd81871c2011-10-03 13:57:23 -07002580 /* These should never appear during verification. */
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002581 case Instruction::UNUSED_3E:
2582 case Instruction::UNUSED_3F:
2583 case Instruction::UNUSED_40:
2584 case Instruction::UNUSED_41:
2585 case Instruction::UNUSED_42:
2586 case Instruction::UNUSED_43:
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002587 case Instruction::UNUSED_79:
2588 case Instruction::UNUSED_7A:
2589 case Instruction::UNUSED_EB:
2590 case Instruction::UNUSED_EC:
jeffhao9a4f0032012-08-30 16:17:40 -07002591 case Instruction::UNUSED_ED:
jeffhaobdb76512011-09-07 11:43:16 -07002592 case Instruction::UNUSED_EE:
2593 case Instruction::UNUSED_EF:
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002594 case Instruction::UNUSED_F0:
2595 case Instruction::UNUSED_F1:
jeffhaobdb76512011-09-07 11:43:16 -07002596 case Instruction::UNUSED_F2:
2597 case Instruction::UNUSED_F3:
2598 case Instruction::UNUSED_F4:
2599 case Instruction::UNUSED_F5:
2600 case Instruction::UNUSED_F6:
2601 case Instruction::UNUSED_F7:
2602 case Instruction::UNUSED_F8:
2603 case Instruction::UNUSED_F9:
2604 case Instruction::UNUSED_FA:
2605 case Instruction::UNUSED_FB:
jeffhaobdb76512011-09-07 11:43:16 -07002606 case Instruction::UNUSED_FC:
jeffhaobdb76512011-09-07 11:43:16 -07002607 case Instruction::UNUSED_FD:
jeffhaobdb76512011-09-07 11:43:16 -07002608 case Instruction::UNUSED_FE:
jeffhaobdb76512011-09-07 11:43:16 -07002609 case Instruction::UNUSED_FF:
jeffhaod5347e02012-03-22 17:25:05 -07002610 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Unexpected opcode " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07002611 break;
2612
2613 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002614 * DO NOT add a "default" clause here. Without it the compiler will
jeffhaobdb76512011-09-07 11:43:16 -07002615 * complain if an instruction is missing (which is desirable).
2616 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002617 } // end - switch (dec_insn.opcode)
jeffhaobdb76512011-09-07 11:43:16 -07002618
Ian Rogersad0b3a32012-04-16 14:50:24 -07002619 if (have_pending_hard_failure_) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002620 if (Runtime::Current()->IsCompiler()) {
jeffhaob57e9522012-04-26 18:08:21 -07002621 /* When compiling, check that the last failure is a hard failure */
Ian Rogersad0b3a32012-04-16 14:50:24 -07002622 CHECK_EQ(failures_[failures_.size() - 1], VERIFY_ERROR_BAD_CLASS_HARD);
Ian Rogerse1758fe2012-04-19 11:31:15 -07002623 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002624 /* immediate failure, reject class */
2625 info_messages_ << "Rejecting opcode " << inst->DumpString(dex_file_);
2626 return false;
jeffhaofaf459e2012-08-31 15:32:47 -07002627 } else if (have_pending_runtime_throw_failure_) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07002628 /* checking interpreter will throw, mark following code as unreachable */
jeffhaofaf459e2012-08-31 15:32:47 -07002629 opcode_flags = Instruction::kThrow;
jeffhaobdb76512011-09-07 11:43:16 -07002630 }
jeffhaobdb76512011-09-07 11:43:16 -07002631 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002632 * If we didn't just set the result register, clear it out. This ensures that you can only use
2633 * "move-result" immediately after the result is set. (We could check this statically, but it's
2634 * not expensive and it makes our debugging output cleaner.)
jeffhaobdb76512011-09-07 11:43:16 -07002635 */
2636 if (!just_set_result) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002637 work_line_->SetResultTypeToUnknown();
jeffhaobdb76512011-09-07 11:43:16 -07002638 }
2639
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002640
jeffhaobdb76512011-09-07 11:43:16 -07002641
2642 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002643 * Handle "branch". Tag the branch target.
jeffhaobdb76512011-09-07 11:43:16 -07002644 *
2645 * NOTE: instructions like Instruction::EQZ provide information about the
jeffhaod1f0fde2011-09-08 17:25:33 -07002646 * state of the register when the branch is taken or not taken. For example,
jeffhaobdb76512011-09-07 11:43:16 -07002647 * somebody could get a reference field, check it for zero, and if the
2648 * branch is taken immediately store that register in a boolean field
jeffhaod1f0fde2011-09-08 17:25:33 -07002649 * since the value is known to be zero. We do not currently account for
jeffhaobdb76512011-09-07 11:43:16 -07002650 * that, and will reject the code.
2651 *
2652 * TODO: avoid re-fetching the branch target
2653 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002654 if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002655 bool isConditional, selfOkay;
Ian Rogersd81871c2011-10-03 13:57:23 -07002656 if (!GetBranchOffset(work_insn_idx_, &branch_target, &isConditional, &selfOkay)) {
jeffhaobdb76512011-09-07 11:43:16 -07002657 /* should never happen after static verification */
jeffhaod5347e02012-03-22 17:25:05 -07002658 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad branch";
jeffhaobdb76512011-09-07 11:43:16 -07002659 return false;
2660 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08002661 DCHECK_EQ(isConditional, (opcode_flags & Instruction::kContinue) != 0);
jeffhaod5347e02012-03-22 17:25:05 -07002662 if (!CheckNotMoveException(code_item_->insns_, work_insn_idx_ + branch_target)) {
jeffhaobdb76512011-09-07 11:43:16 -07002663 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002664 }
jeffhaobdb76512011-09-07 11:43:16 -07002665 /* update branch target, set "changed" if appropriate */
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002666 if (NULL != branch_line.get()) {
2667 if (!UpdateRegisters(work_insn_idx_ + branch_target, branch_line.get())) {
2668 return false;
2669 }
2670 } else {
2671 if (!UpdateRegisters(work_insn_idx_ + branch_target, work_line_.get())) {
2672 return false;
2673 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002674 }
jeffhaobdb76512011-09-07 11:43:16 -07002675 }
2676
2677 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002678 * Handle "switch". Tag all possible branch targets.
jeffhaobdb76512011-09-07 11:43:16 -07002679 *
2680 * We've already verified that the table is structurally sound, so we
2681 * just need to walk through and tag the targets.
2682 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002683 if ((opcode_flags & Instruction::kSwitch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002684 int offset_to_switch = insns[1] | (((int32_t) insns[2]) << 16);
2685 const uint16_t* switch_insns = insns + offset_to_switch;
2686 int switch_count = switch_insns[1];
2687 int offset_to_targets, targ;
2688
2689 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
2690 /* 0 = sig, 1 = count, 2/3 = first key */
2691 offset_to_targets = 4;
2692 } else {
2693 /* 0 = sig, 1 = count, 2..count * 2 = keys */
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07002694 DCHECK((*insns & 0xff) == Instruction::SPARSE_SWITCH);
jeffhaobdb76512011-09-07 11:43:16 -07002695 offset_to_targets = 2 + 2 * switch_count;
2696 }
2697
2698 /* verify each switch target */
2699 for (targ = 0; targ < switch_count; targ++) {
2700 int offset;
2701 uint32_t abs_offset;
2702
2703 /* offsets are 32-bit, and only partly endian-swapped */
2704 offset = switch_insns[offset_to_targets + targ * 2] |
2705 (((int32_t) switch_insns[offset_to_targets + targ * 2 + 1]) << 16);
Ian Rogersd81871c2011-10-03 13:57:23 -07002706 abs_offset = work_insn_idx_ + offset;
2707 DCHECK_LT(abs_offset, code_item_->insns_size_in_code_units_);
jeffhaod5347e02012-03-22 17:25:05 -07002708 if (!CheckNotMoveException(code_item_->insns_, abs_offset)) {
jeffhaobdb76512011-09-07 11:43:16 -07002709 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002710 }
2711 if (!UpdateRegisters(abs_offset, work_line_.get()))
jeffhaobdb76512011-09-07 11:43:16 -07002712 return false;
2713 }
2714 }
2715
2716 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002717 * Handle instructions that can throw and that are sitting in a "try" block. (If they're not in a
2718 * "try" block when they throw, control transfers out of the method.)
jeffhaobdb76512011-09-07 11:43:16 -07002719 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002720 if ((opcode_flags & Instruction::kThrow) != 0 && insn_flags_[work_insn_idx_].IsInTry()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002721 bool within_catch_all = false;
Ian Rogers0571d352011-11-03 19:51:38 -07002722 CatchHandlerIterator iterator(*code_item_, work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07002723
Ian Rogers0571d352011-11-03 19:51:38 -07002724 for (; iterator.HasNext(); iterator.Next()) {
2725 if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002726 within_catch_all = true;
2727 }
jeffhaobdb76512011-09-07 11:43:16 -07002728 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002729 * Merge registers into the "catch" block. We want to use the "savedRegs" rather than
2730 * "work_regs", because at runtime the exception will be thrown before the instruction
2731 * modifies any registers.
jeffhaobdb76512011-09-07 11:43:16 -07002732 */
Ian Rogers0571d352011-11-03 19:51:38 -07002733 if (!UpdateRegisters(iterator.GetHandlerAddress(), saved_line_.get())) {
jeffhaobdb76512011-09-07 11:43:16 -07002734 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002735 }
jeffhaobdb76512011-09-07 11:43:16 -07002736 }
2737
2738 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002739 * If the monitor stack depth is nonzero, there must be a "catch all" handler for this
2740 * instruction. This does apply to monitor-exit because of async exception handling.
jeffhaobdb76512011-09-07 11:43:16 -07002741 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002742 if (work_line_->MonitorStackDepth() > 0 && !within_catch_all) {
jeffhaobdb76512011-09-07 11:43:16 -07002743 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002744 * The state in work_line reflects the post-execution state. If the current instruction is a
2745 * monitor-enter and the monitor stack was empty, we don't need a catch-all (if it throws,
jeffhaobdb76512011-09-07 11:43:16 -07002746 * it will do so before grabbing the lock).
2747 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002748 if (inst->Opcode() != Instruction::MONITOR_ENTER || work_line_->MonitorStackDepth() != 1) {
jeffhaod5347e02012-03-22 17:25:05 -07002749 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
Ian Rogersd81871c2011-10-03 13:57:23 -07002750 << "expected to be within a catch-all for an instruction where a monitor is held";
jeffhaobdb76512011-09-07 11:43:16 -07002751 return false;
2752 }
2753 }
2754 }
2755
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002756 /* Handle "continue". Tag the next consecutive instruction.
2757 * Note: Keep the code handling "continue" case below the "branch" and "switch" cases,
2758 * because it changes work_line_ when performing peephole optimization
2759 * and this change should not be used in those cases.
2760 */
Ian Rogers6d376ae2013-07-23 15:12:40 -07002761 if ((opcode_flags & Instruction::kContinue) != 0) {
2762 uint32_t next_insn_idx = work_insn_idx_ + CurrentInsnFlags()->GetLengthInCodeUnits();
2763 if (next_insn_idx >= code_item_->insns_size_in_code_units_) {
2764 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Execution can walk off end of code area";
2765 return false;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002766 }
Ian Rogers6d376ae2013-07-23 15:12:40 -07002767 // The only way to get to a move-exception instruction is to get thrown there. Make sure the
2768 // next instruction isn't one.
2769 if (!CheckNotMoveException(code_item_->insns_, next_insn_idx)) {
2770 return false;
2771 }
2772 if (NULL != fallthrough_line.get()) {
2773 // Make workline consistent with fallthrough computed from peephole optimization.
2774 work_line_->CopyFromLine(fallthrough_line.get());
2775 }
Ian Rogersb8c78592013-07-25 23:52:52 +00002776 if (insn_flags_[next_insn_idx].IsReturn()) {
2777 // For returns we only care about the operand to the return, all other registers are dead.
2778 const Instruction* ret_inst = Instruction::At(code_item_->insns_ + next_insn_idx);
2779 Instruction::Code opcode = ret_inst->Opcode();
2780 if ((opcode == Instruction::RETURN_VOID) || (opcode == Instruction::RETURN_VOID_BARRIER)) {
2781 work_line_->MarkAllRegistersAsConflicts();
2782 } else {
2783 if (opcode == Instruction::RETURN_WIDE) {
2784 work_line_->MarkAllRegistersAsConflictsExceptWide(ret_inst->VRegA_11x());
2785 } else {
2786 work_line_->MarkAllRegistersAsConflictsExcept(ret_inst->VRegA_11x());
2787 }
2788 }
2789 }
Ian Rogers6d376ae2013-07-23 15:12:40 -07002790 RegisterLine* next_line = reg_table_.GetLine(next_insn_idx);
2791 if (next_line != NULL) {
2792 // Merge registers into what we have for the next instruction,
2793 // and set the "changed" flag if needed.
2794 if (!UpdateRegisters(next_insn_idx, work_line_.get())) {
2795 return false;
2796 }
2797 } else {
2798 /*
2799 * We're not recording register data for the next instruction, so we don't know what the
2800 * prior state was. We have to assume that something has changed and re-evaluate it.
2801 */
2802 insn_flags_[next_insn_idx].SetChanged();
2803 }
2804 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002805
jeffhaod1f0fde2011-09-08 17:25:33 -07002806 /* If we're returning from the method, make sure monitor stack is empty. */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002807 if ((opcode_flags & Instruction::kReturn) != 0) {
Elliott Hughesb25c3f62012-03-26 16:35:06 -07002808 if (!work_line_->VerifyMonitorStackEmpty()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002809 return false;
2810 }
jeffhaobdb76512011-09-07 11:43:16 -07002811 }
2812
2813 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002814 * Update start_guess. Advance to the next instruction of that's
2815 * possible, otherwise use the branch target if one was found. If
jeffhaobdb76512011-09-07 11:43:16 -07002816 * neither of those exists we're in a return or throw; leave start_guess
2817 * alone and let the caller sort it out.
2818 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002819 if ((opcode_flags & Instruction::kContinue) != 0) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002820 *start_guess = work_insn_idx_ + insn_flags_[work_insn_idx_].GetLengthInCodeUnits();
Elliott Hughesadb8c672012-03-06 16:49:32 -08002821 } else if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002822 /* we're still okay if branch_target is zero */
Ian Rogersd81871c2011-10-03 13:57:23 -07002823 *start_guess = work_insn_idx_ + branch_target;
jeffhaobdb76512011-09-07 11:43:16 -07002824 }
2825
Ian Rogersd81871c2011-10-03 13:57:23 -07002826 DCHECK_LT(*start_guess, code_item_->insns_size_in_code_units_);
2827 DCHECK(insn_flags_[*start_guess].IsOpcode());
jeffhaobdb76512011-09-07 11:43:16 -07002828
2829 return true;
Brian Carlstrom7934ac22013-07-26 10:54:15 -07002830} // NOLINT(readability/fn_size)
jeffhaobdb76512011-09-07 11:43:16 -07002831
Ian Rogers776ac1f2012-04-13 23:36:36 -07002832const RegType& MethodVerifier::ResolveClassAndCheckAccess(uint32_t class_idx) {
Ian Rogers0571d352011-11-03 19:51:38 -07002833 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002834 const RegType& referrer = GetDeclaringClass();
Mathieu Chartier590fee92013-09-13 13:46:47 -07002835 mirror::Class* klass = (*dex_cache_)->GetResolvedType(class_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002836 const RegType& result =
Ian Rogers04f94f42013-06-10 15:09:26 -07002837 klass != NULL ? reg_types_.FromClass(descriptor, klass,
2838 klass->CannotBeAssignedFromOtherTypes())
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07002839 : reg_types_.FromDescriptor(class_loader_->Get(), descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002840 if (result.IsConflict()) {
2841 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "accessing broken descriptor '" << descriptor
2842 << "' in " << referrer;
2843 return result;
2844 }
Ian Rogerse1758fe2012-04-19 11:31:15 -07002845 if (klass == NULL && !result.IsUnresolvedTypes()) {
Mathieu Chartier590fee92013-09-13 13:46:47 -07002846 (*dex_cache_)->SetResolvedType(class_idx, result.GetClass());
Ian Rogerse1758fe2012-04-19 11:31:15 -07002847 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002848 // Check if access is allowed. Unresolved types use xxxWithAccessCheck to
Jeff Haob24b4a72013-07-31 13:47:31 -07002849 // check at runtime if access is allowed and so pass here. If result is
2850 // primitive, skip the access check.
2851 if (result.IsNonZeroReferenceTypes() && !result.IsUnresolvedTypes() &&
2852 !referrer.IsUnresolvedTypes() && !referrer.CanAccess(result)) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002853 Fail(VERIFY_ERROR_ACCESS_CLASS) << "illegal class access: '"
Ian Rogersad0b3a32012-04-16 14:50:24 -07002854 << referrer << "' -> '" << result << "'";
Ian Rogers28ad40d2011-10-27 15:19:26 -07002855 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002856 return result;
Ian Rogersd81871c2011-10-03 13:57:23 -07002857}
2858
Ian Rogers776ac1f2012-04-13 23:36:36 -07002859const RegType& MethodVerifier::GetCaughtExceptionType() {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002860 const RegType* common_super = NULL;
Ian Rogersd81871c2011-10-03 13:57:23 -07002861 if (code_item_->tries_size_ != 0) {
Ian Rogers0571d352011-11-03 19:51:38 -07002862 const byte* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
Ian Rogersd81871c2011-10-03 13:57:23 -07002863 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
2864 for (uint32_t i = 0; i < handlers_size; i++) {
Ian Rogers0571d352011-11-03 19:51:38 -07002865 CatchHandlerIterator iterator(handlers_ptr);
2866 for (; iterator.HasNext(); iterator.Next()) {
2867 if (iterator.GetHandlerAddress() == (uint32_t) work_insn_idx_) {
2868 if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) {
Ian Rogersb4903572012-10-11 11:52:56 -07002869 common_super = &reg_types_.JavaLangThrowable(false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002870 } else {
Ian Rogers0571d352011-11-03 19:51:38 -07002871 const RegType& exception = ResolveClassAndCheckAccess(iterator.GetHandlerTypeIndex());
Jeff Haob878f212014-04-24 16:25:36 -07002872 if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(exception)) {
Jeff Haoc26a56c2013-11-04 12:00:47 -08002873 if (exception.IsUnresolvedTypes()) {
2874 // We don't know enough about the type. Fail here and let runtime handle it.
2875 Fail(VERIFY_ERROR_NO_CLASS) << "unresolved exception class " << exception;
2876 return exception;
2877 } else {
2878 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unexpected non-exception class " << exception;
2879 return reg_types_.Conflict();
2880 }
Jeff Haob878f212014-04-24 16:25:36 -07002881 } else if (common_super == nullptr) {
2882 common_super = &exception;
Ian Rogers28ad40d2011-10-27 15:19:26 -07002883 } else if (common_super->Equals(exception)) {
Ian Rogersc4762272012-02-01 15:55:55 -08002884 // odd case, but nothing to do
Ian Rogersd81871c2011-10-03 13:57:23 -07002885 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002886 common_super = &common_super->Merge(exception, &reg_types_);
Ian Rogersb4903572012-10-11 11:52:56 -07002887 CHECK(reg_types_.JavaLangThrowable(false).IsAssignableFrom(*common_super));
Ian Rogersd81871c2011-10-03 13:57:23 -07002888 }
2889 }
2890 }
2891 }
Ian Rogers0571d352011-11-03 19:51:38 -07002892 handlers_ptr = iterator.EndDataPointer();
Ian Rogersd81871c2011-10-03 13:57:23 -07002893 }
2894 }
2895 if (common_super == NULL) {
2896 /* no catch blocks, or no catches with classes we can find */
jeffhaod5347e02012-03-22 17:25:05 -07002897 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unable to find exception handler";
Ian Rogersad0b3a32012-04-16 14:50:24 -07002898 return reg_types_.Conflict();
Ian Rogersd81871c2011-10-03 13:57:23 -07002899 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002900 return *common_super;
Ian Rogersd81871c2011-10-03 13:57:23 -07002901}
2902
Brian Carlstromea46f952013-07-30 01:26:50 -07002903mirror::ArtMethod* MethodVerifier::ResolveMethodAndCheckAccess(uint32_t dex_method_idx,
Ian Rogersd91d6d62013-09-25 20:26:14 -07002904 MethodType method_type) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002905 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx);
Ian Rogers90040192011-12-16 08:54:29 -08002906 const RegType& klass_type = ResolveClassAndCheckAccess(method_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002907 if (klass_type.IsConflict()) {
2908 std::string append(" in attempt to access method ");
2909 append += dex_file_->GetMethodName(method_id);
2910 AppendToLastFailMessage(append);
Ian Rogers90040192011-12-16 08:54:29 -08002911 return NULL;
2912 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002913 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers90040192011-12-16 08:54:29 -08002914 return NULL; // Can't resolve Class so no more to do here
2915 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002916 mirror::Class* klass = klass_type.GetClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002917 const RegType& referrer = GetDeclaringClass();
Mathieu Chartier590fee92013-09-13 13:46:47 -07002918 mirror::ArtMethod* res_method = (*dex_cache_)->GetResolvedMethod(dex_method_idx);
Ian Rogersd81871c2011-10-03 13:57:23 -07002919 if (res_method == NULL) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07002920 const char* name = dex_file_->GetMethodName(method_id);
Ian Rogersd91d6d62013-09-25 20:26:14 -07002921 const Signature signature = dex_file_->GetMethodSignature(method_id);
jeffhao8cd6dda2012-02-22 10:15:34 -08002922
2923 if (method_type == METHOD_DIRECT || method_type == METHOD_STATIC) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002924 res_method = klass->FindDirectMethod(name, signature);
jeffhao8cd6dda2012-02-22 10:15:34 -08002925 } else if (method_type == METHOD_INTERFACE) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002926 res_method = klass->FindInterfaceMethod(name, signature);
2927 } else {
2928 res_method = klass->FindVirtualMethod(name, signature);
2929 }
2930 if (res_method != NULL) {
Mathieu Chartier590fee92013-09-13 13:46:47 -07002931 (*dex_cache_)->SetResolvedMethod(dex_method_idx, res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07002932 } else {
jeffhao8cd6dda2012-02-22 10:15:34 -08002933 // If a virtual or interface method wasn't found with the expected type, look in
2934 // the direct methods. This can happen when the wrong invoke type is used or when
2935 // a class has changed, and will be flagged as an error in later checks.
2936 if (method_type == METHOD_INTERFACE || method_type == METHOD_VIRTUAL) {
2937 res_method = klass->FindDirectMethod(name, signature);
2938 }
2939 if (res_method == NULL) {
2940 Fail(VERIFY_ERROR_NO_METHOD) << "couldn't find method "
2941 << PrettyDescriptor(klass) << "." << name
2942 << " " << signature;
2943 return NULL;
2944 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002945 }
2946 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002947 // Make sure calls to constructors are "direct". There are additional restrictions but we don't
2948 // enforce them here.
2949 if (res_method->IsConstructor() && method_type != METHOD_DIRECT) {
jeffhaod5347e02012-03-22 17:25:05 -07002950 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting non-direct call to constructor "
2951 << PrettyMethod(res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07002952 return NULL;
2953 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002954 // Disallow any calls to class initializers.
2955 if (MethodHelper(res_method).IsClassInitializer()) {
jeffhaod5347e02012-03-22 17:25:05 -07002956 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting call to class initializer "
2957 << PrettyMethod(res_method);
jeffhao8cd6dda2012-02-22 10:15:34 -08002958 return NULL;
2959 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002960 // Check if access is allowed.
Ian Rogersad0b3a32012-04-16 14:50:24 -07002961 if (!referrer.CanAccessMember(res_method->GetDeclaringClass(), res_method->GetAccessFlags())) {
jeffhao8cd6dda2012-02-22 10:15:34 -08002962 Fail(VERIFY_ERROR_ACCESS_METHOD) << "illegal method access (call " << PrettyMethod(res_method)
Ian Rogersad0b3a32012-04-16 14:50:24 -07002963 << " from " << referrer << ")";
jeffhaob57e9522012-04-26 18:08:21 -07002964 return res_method;
jeffhao8cd6dda2012-02-22 10:15:34 -08002965 }
jeffhaode0d9c92012-02-27 13:58:13 -08002966 // Check that invoke-virtual and invoke-super are not used on private methods of the same class.
2967 if (res_method->IsPrivate() && method_type == METHOD_VIRTUAL) {
jeffhaod5347e02012-03-22 17:25:05 -07002968 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invoke-super/virtual can't be used on private method "
2969 << PrettyMethod(res_method);
jeffhaode0d9c92012-02-27 13:58:13 -08002970 return NULL;
2971 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002972 // Check that interface methods match interface classes.
2973 if (klass->IsInterface() && method_type != METHOD_INTERFACE) {
2974 Fail(VERIFY_ERROR_CLASS_CHANGE) << "non-interface method " << PrettyMethod(res_method)
2975 << " is in an interface class " << PrettyClass(klass);
2976 return NULL;
2977 } else if (!klass->IsInterface() && method_type == METHOD_INTERFACE) {
2978 Fail(VERIFY_ERROR_CLASS_CHANGE) << "interface method " << PrettyMethod(res_method)
2979 << " is in a non-interface class " << PrettyClass(klass);
2980 return NULL;
2981 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002982 // See if the method type implied by the invoke instruction matches the access flags for the
2983 // target method.
2984 if ((method_type == METHOD_DIRECT && !res_method->IsDirect()) ||
2985 (method_type == METHOD_STATIC && !res_method->IsStatic()) ||
2986 ((method_type == METHOD_VIRTUAL || method_type == METHOD_INTERFACE) && res_method->IsDirect())
2987 ) {
Ian Rogers2fc14272012-08-30 10:56:57 -07002988 Fail(VERIFY_ERROR_CLASS_CHANGE) << "invoke type (" << method_type << ") does not match method "
2989 " type of " << PrettyMethod(res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07002990 return NULL;
2991 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002992 return res_method;
2993}
2994
Brian Carlstromea46f952013-07-30 01:26:50 -07002995mirror::ArtMethod* MethodVerifier::VerifyInvocationArgs(const Instruction* inst,
Sebastien Hertz5243e912013-05-21 10:55:07 +02002996 MethodType method_type,
2997 bool is_range,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002998 bool is_super) {
jeffhao8cd6dda2012-02-22 10:15:34 -08002999 // Resolve the method. This could be an abstract or concrete method depending on what sort of call
3000 // we're making.
Sebastien Hertz5243e912013-05-21 10:55:07 +02003001 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Brian Carlstromea46f952013-07-30 01:26:50 -07003002 mirror::ArtMethod* res_method = ResolveMethodAndCheckAccess(method_idx, method_type);
jeffhao8cd6dda2012-02-22 10:15:34 -08003003 if (res_method == NULL) { // error or class is unresolved
3004 return NULL;
3005 }
3006
Ian Rogersd81871c2011-10-03 13:57:23 -07003007 // If we're using invoke-super(method), make sure that the executing method's class' superclass
3008 // has a vtable entry for the target method.
3009 if (is_super) {
3010 DCHECK(method_type == METHOD_VIRTUAL);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003011 const RegType& super = GetDeclaringClass().GetSuperClass(&reg_types_);
Ian Rogers529781d2012-07-23 17:24:29 -07003012 if (super.IsUnresolvedTypes()) {
jeffhao4d8df822012-04-24 17:09:36 -07003013 Fail(VERIFY_ERROR_NO_METHOD) << "unknown super class in invoke-super from "
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003014 << PrettyMethod(dex_method_idx_, *dex_file_)
jeffhao4d8df822012-04-24 17:09:36 -07003015 << " to super " << PrettyMethod(res_method);
3016 return NULL;
3017 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003018 mirror::Class* super_klass = super.GetClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07003019 if (res_method->GetMethodIndex() >= super_klass->GetVTable()->GetLength()) {
jeffhao4d8df822012-04-24 17:09:36 -07003020 MethodHelper mh(res_method);
3021 Fail(VERIFY_ERROR_NO_METHOD) << "invalid invoke-super from "
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003022 << PrettyMethod(dex_method_idx_, *dex_file_)
jeffhao4d8df822012-04-24 17:09:36 -07003023 << " to super " << super
3024 << "." << mh.GetName()
3025 << mh.GetSignature();
Ian Rogersd81871c2011-10-03 13:57:23 -07003026 return NULL;
3027 }
3028 }
3029 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003030 // match the call to the signature. Also, we might be calling through an abstract method
Ian Rogersd81871c2011-10-03 13:57:23 -07003031 // definition (which doesn't have register count values).
Sebastien Hertz5243e912013-05-21 10:55:07 +02003032 const size_t expected_args = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
Ian Rogersd81871c2011-10-03 13:57:23 -07003033 /* caught by static verifier */
3034 DCHECK(is_range || expected_args <= 5);
3035 if (expected_args > code_item_->outs_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07003036 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
Ian Rogersd81871c2011-10-03 13:57:23 -07003037 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
3038 return NULL;
3039 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003040
jeffhaobdb76512011-09-07 11:43:16 -07003041 /*
Ian Rogersad0b3a32012-04-16 14:50:24 -07003042 * Check the "this" argument, which must be an instance of the class that declared the method.
3043 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
3044 * rigorous check here (which is okay since we have to do it at runtime).
jeffhaobdb76512011-09-07 11:43:16 -07003045 */
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003046 size_t actual_args = 0;
Ian Rogersd81871c2011-10-03 13:57:23 -07003047 if (!res_method->IsStatic()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003048 const RegType& actual_arg_type = work_line_->GetInvocationThis(inst, is_range);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003049 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
Ian Rogersd81871c2011-10-03 13:57:23 -07003050 return NULL;
3051 }
3052 if (actual_arg_type.IsUninitializedReference() && !res_method->IsConstructor()) {
jeffhaod5347e02012-03-22 17:25:05 -07003053 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
Ian Rogersd81871c2011-10-03 13:57:23 -07003054 return NULL;
3055 }
3056 if (method_type != METHOD_INTERFACE && !actual_arg_type.IsZero()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003057 mirror::Class* klass = res_method->GetDeclaringClass();
Ian Rogers04f94f42013-06-10 15:09:26 -07003058 const RegType& res_method_class =
Mathieu Chartierf8322842014-05-16 10:59:25 -07003059 reg_types_.FromClass(klass->GetDescriptor().c_str(), klass,
Ian Rogers04f94f42013-06-10 15:09:26 -07003060 klass->CannotBeAssignedFromOtherTypes());
Ian Rogers9074b992011-10-26 17:41:55 -07003061 if (!res_method_class.IsAssignableFrom(actual_arg_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07003062 Fail(actual_arg_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS:
3063 VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003064 << "' not instance of '" << res_method_class << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07003065 return NULL;
3066 }
3067 }
3068 actual_args++;
3069 }
3070 /*
3071 * Process the target method's signature. This signature may or may not
3072 * have been verified, so we can't assume it's properly formed.
3073 */
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003074 MethodHelper mh(res_method);
3075 const DexFile::TypeList* params = mh.GetParameterTypeList();
3076 size_t params_size = params == NULL ? 0 : params->Size();
Sebastien Hertz5243e912013-05-21 10:55:07 +02003077 uint32_t arg[5];
3078 if (!is_range) {
Ian Rogers29a26482014-05-02 15:27:29 -07003079 inst->GetVarArgs(arg);
Sebastien Hertz5243e912013-05-21 10:55:07 +02003080 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003081 for (size_t param_index = 0; param_index < params_size; param_index++) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003082 if (actual_args >= expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07003083 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invalid call to '" << PrettyMethod(res_method)
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003084 << "'. Expected " << expected_args << " arguments, processing argument " << actual_args
3085 << " (where longs/doubles count twice).";
Ian Rogersd81871c2011-10-03 13:57:23 -07003086 return NULL;
3087 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003088 const char* descriptor =
3089 mh.GetTypeDescriptorFromTypeIdx(params->GetTypeItem(param_index).type_idx_);
3090 if (descriptor == NULL) {
jeffhaod5347e02012-03-22 17:25:05 -07003091 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003092 << " missing signature component";
3093 return NULL;
Ian Rogersd81871c2011-10-03 13:57:23 -07003094 }
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003095 const RegType& reg_type = reg_types_.FromDescriptor(class_loader_->Get(), descriptor, false);
Sebastien Hertz5243e912013-05-21 10:55:07 +02003096 uint32_t get_reg = is_range ? inst->VRegC_3rc() + actual_args : arg[actual_args];
Jeff Haoa6b22c52013-10-04 14:33:22 -07003097 if (reg_type.IsIntegralTypes()) {
3098 const RegType& src_type = work_line_->GetRegisterType(get_reg);
3099 if (!src_type.IsIntegralTypes()) {
3100 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register v" << get_reg << " has type " << src_type
3101 << " but expected " << reg_type;
3102 return res_method;
3103 }
3104 } else if (!work_line_->VerifyRegisterType(get_reg, reg_type)) {
jeffhaob57e9522012-04-26 18:08:21 -07003105 return res_method;
Ian Rogersd81871c2011-10-03 13:57:23 -07003106 }
3107 actual_args = reg_type.IsLongOrDoubleTypes() ? actual_args + 2 : actual_args + 1;
3108 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003109 if (actual_args != expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07003110 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003111 << " expected " << expected_args << " arguments, found " << actual_args;
Ian Rogersd81871c2011-10-03 13:57:23 -07003112 return NULL;
3113 } else {
3114 return res_method;
3115 }
3116}
3117
Brian Carlstromea46f952013-07-30 01:26:50 -07003118mirror::ArtMethod* MethodVerifier::GetQuickInvokedMethod(const Instruction* inst,
Mathieu Chartier590fee92013-09-13 13:46:47 -07003119 RegisterLine* reg_line, bool is_range) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003120 DCHECK(inst->Opcode() == Instruction::INVOKE_VIRTUAL_QUICK ||
3121 inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
3122 const RegType& actual_arg_type = reg_line->GetInvocationThis(inst, is_range);
Ian Rogers9bc54402014-04-17 16:40:01 -07003123 if (!actual_arg_type.HasClass()) {
3124 VLOG(verifier) << "Failed to get mirror::Class* from '" << actual_arg_type << "'";
Andreas Gampe63981562014-04-17 12:28:43 -07003125 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003126 }
Ian Rogersa4cf1df2014-05-07 19:47:17 -07003127 mirror::ObjectArray<mirror::ArtMethod>* vtable = nullptr;
3128 mirror::Class* klass = actual_arg_type.GetClass();
3129 if (klass->IsInterface()) {
3130 // Derive Object.class from Class.class.getSuperclass().
3131 mirror::Class* object_klass = klass->GetClass()->GetSuperClass();
3132 CHECK(object_klass->IsObjectClass());
3133 vtable = object_klass->GetVTable();
3134 } else {
3135 vtable = klass->GetVTable();
3136 }
3137 CHECK(vtable != nullptr) << PrettyDescriptor(klass);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003138 uint16_t vtable_index = is_range ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersa4cf1df2014-05-07 19:47:17 -07003139 CHECK_LT(static_cast<int32_t>(vtable_index), vtable->GetLength()) << PrettyDescriptor(klass);
Brian Carlstromea46f952013-07-30 01:26:50 -07003140 mirror::ArtMethod* res_method = vtable->Get(vtable_index);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003141 CHECK(!Thread::Current()->IsExceptionPending());
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003142 return res_method;
3143}
3144
Brian Carlstromea46f952013-07-30 01:26:50 -07003145mirror::ArtMethod* MethodVerifier::VerifyInvokeVirtualQuickArgs(const Instruction* inst,
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003146 bool is_range) {
3147 DCHECK(Runtime::Current()->IsStarted());
Brian Carlstromea46f952013-07-30 01:26:50 -07003148 mirror::ArtMethod* res_method = GetQuickInvokedMethod(inst, work_line_.get(),
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003149 is_range);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003150 if (res_method == NULL) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003151 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer method from " << inst->Name();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003152 return NULL;
3153 }
3154 CHECK(!res_method->IsDirect() && !res_method->IsStatic());
3155
3156 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
3157 // match the call to the signature. Also, we might be calling through an abstract method
3158 // definition (which doesn't have register count values).
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003159 const RegType& actual_arg_type = work_line_->GetInvocationThis(inst, is_range);
3160 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
3161 return NULL;
3162 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003163 const size_t expected_args = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
3164 /* caught by static verifier */
3165 DCHECK(is_range || expected_args <= 5);
3166 if (expected_args > code_item_->outs_size_) {
3167 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
3168 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
3169 return NULL;
3170 }
3171
3172 /*
3173 * Check the "this" argument, which must be an instance of the class that declared the method.
3174 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
3175 * rigorous check here (which is okay since we have to do it at runtime).
3176 */
3177 if (actual_arg_type.IsUninitializedReference() && !res_method->IsConstructor()) {
3178 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
3179 return NULL;
3180 }
3181 if (!actual_arg_type.IsZero()) {
3182 mirror::Class* klass = res_method->GetDeclaringClass();
3183 const RegType& res_method_class =
Mathieu Chartierf8322842014-05-16 10:59:25 -07003184 reg_types_.FromClass(klass->GetDescriptor().c_str(), klass,
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003185 klass->CannotBeAssignedFromOtherTypes());
3186 if (!res_method_class.IsAssignableFrom(actual_arg_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07003187 Fail(actual_arg_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS :
3188 VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003189 << "' not instance of '" << res_method_class << "'";
3190 return NULL;
3191 }
3192 }
3193 /*
3194 * Process the target method's signature. This signature may or may not
3195 * have been verified, so we can't assume it's properly formed.
3196 */
3197 MethodHelper mh(res_method);
3198 const DexFile::TypeList* params = mh.GetParameterTypeList();
3199 size_t params_size = params == NULL ? 0 : params->Size();
3200 uint32_t arg[5];
3201 if (!is_range) {
Ian Rogers29a26482014-05-02 15:27:29 -07003202 inst->GetVarArgs(arg);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003203 }
3204 size_t actual_args = 1;
3205 for (size_t param_index = 0; param_index < params_size; param_index++) {
3206 if (actual_args >= expected_args) {
3207 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invalid call to '" << PrettyMethod(res_method)
Brian Carlstrom93c33962013-07-26 10:37:43 -07003208 << "'. Expected " << expected_args
3209 << " arguments, processing argument " << actual_args
3210 << " (where longs/doubles count twice).";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003211 return NULL;
3212 }
3213 const char* descriptor =
3214 mh.GetTypeDescriptorFromTypeIdx(params->GetTypeItem(param_index).type_idx_);
3215 if (descriptor == NULL) {
3216 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003217 << " missing signature component";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003218 return NULL;
3219 }
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003220 const RegType& reg_type = reg_types_.FromDescriptor(class_loader_->Get(), descriptor, false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003221 uint32_t get_reg = is_range ? inst->VRegC_3rc() + actual_args : arg[actual_args];
3222 if (!work_line_->VerifyRegisterType(get_reg, reg_type)) {
3223 return res_method;
3224 }
3225 actual_args = reg_type.IsLongOrDoubleTypes() ? actual_args + 2 : actual_args + 1;
3226 }
3227 if (actual_args != expected_args) {
3228 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
3229 << " expected " << expected_args << " arguments, found " << actual_args;
3230 return NULL;
3231 } else {
3232 return res_method;
3233 }
3234}
3235
Ian Rogers62342ec2013-06-11 10:26:37 -07003236void MethodVerifier::VerifyNewArray(const Instruction* inst, bool is_filled, bool is_range) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003237 uint32_t type_idx;
3238 if (!is_filled) {
3239 DCHECK_EQ(inst->Opcode(), Instruction::NEW_ARRAY);
3240 type_idx = inst->VRegC_22c();
3241 } else if (!is_range) {
3242 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY);
3243 type_idx = inst->VRegB_35c();
3244 } else {
3245 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY_RANGE);
3246 type_idx = inst->VRegB_3rc();
3247 }
3248 const RegType& res_type = ResolveClassAndCheckAccess(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003249 if (res_type.IsConflict()) { // bad class
3250 DCHECK_NE(failures_.size(), 0U);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003251 } else {
3252 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
3253 if (!res_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003254 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "new-array on non-array class " << res_type;
Ian Rogers0c4a5062012-02-03 15:18:59 -08003255 } else if (!is_filled) {
3256 /* make sure "size" register is valid type */
Sebastien Hertz5243e912013-05-21 10:55:07 +02003257 work_line_->VerifyRegisterType(inst->VRegB_22c(), reg_types_.Integer());
Ian Rogers0c4a5062012-02-03 15:18:59 -08003258 /* set register type to array class */
Ian Rogers62342ec2013-06-11 10:26:37 -07003259 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
3260 work_line_->SetRegisterType(inst->VRegA_22c(), precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003261 } else {
3262 // Verify each register. If "arg_count" is bad, VerifyRegisterType() will run off the end of
3263 // the list and fail. It's legal, if silly, for arg_count to be zero.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003264 const RegType& expected_type = reg_types_.GetComponentType(res_type, class_loader_->Get());
Sebastien Hertz5243e912013-05-21 10:55:07 +02003265 uint32_t arg_count = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
3266 uint32_t arg[5];
3267 if (!is_range) {
Ian Rogers29a26482014-05-02 15:27:29 -07003268 inst->GetVarArgs(arg);
Sebastien Hertz5243e912013-05-21 10:55:07 +02003269 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08003270 for (size_t ui = 0; ui < arg_count; ui++) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003271 uint32_t get_reg = is_range ? inst->VRegC_3rc() + ui : arg[ui];
Ian Rogers0c4a5062012-02-03 15:18:59 -08003272 if (!work_line_->VerifyRegisterType(get_reg, expected_type)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003273 work_line_->SetResultRegisterType(reg_types_.Conflict());
Ian Rogers0c4a5062012-02-03 15:18:59 -08003274 return;
3275 }
3276 }
3277 // filled-array result goes into "result" register
Ian Rogers62342ec2013-06-11 10:26:37 -07003278 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
3279 work_line_->SetResultRegisterType(precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003280 }
3281 }
3282}
3283
Sebastien Hertz5243e912013-05-21 10:55:07 +02003284void MethodVerifier::VerifyAGet(const Instruction* inst,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003285 const RegType& insn_type, bool is_primitive) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003286 const RegType& index_type = work_line_->GetRegisterType(inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07003287 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003288 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07003289 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003290 const RegType& array_type = work_line_->GetRegisterType(inst->VRegB_23x());
Ian Rogers89310de2012-02-01 13:47:30 -08003291 if (array_type.IsZero()) {
3292 // Null array class; this code path will fail at runtime. Infer a merge-able type from the
3293 // instruction type. TODO: have a proper notion of bottom here.
3294 if (!is_primitive || insn_type.IsCategory1Types()) {
3295 // Reference or category 1
Sebastien Hertz5243e912013-05-21 10:55:07 +02003296 work_line_->SetRegisterType(inst->VRegA_23x(), reg_types_.Zero());
Ian Rogersd81871c2011-10-03 13:57:23 -07003297 } else {
Ian Rogers89310de2012-02-01 13:47:30 -08003298 // Category 2
Sebastien Hertz5243e912013-05-21 10:55:07 +02003299 work_line_->SetRegisterTypeWide(inst->VRegA_23x(), reg_types_.FromCat2ConstLo(0, false),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003300 reg_types_.FromCat2ConstHi(0, false));
Ian Rogers89310de2012-02-01 13:47:30 -08003301 }
jeffhaofc3144e2012-02-01 17:21:15 -08003302 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003303 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aget";
Ian Rogers89310de2012-02-01 13:47:30 -08003304 } else {
3305 /* verify the class */
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003306 const RegType& component_type = reg_types_.GetComponentType(array_type, class_loader_->Get());
jeffhaofc3144e2012-02-01 17:21:15 -08003307 if (!component_type.IsReferenceTypes() && !is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07003308 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08003309 << " source for aget-object";
3310 } else if (component_type.IsNonZeroReferenceTypes() && is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07003311 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08003312 << " source for category 1 aget";
3313 } else if (is_primitive && !insn_type.Equals(component_type) &&
3314 !((insn_type.IsInteger() && component_type.IsFloat()) ||
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003315 (insn_type.IsLong() && component_type.IsDouble()))) {
3316 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array type " << array_type
3317 << " incompatible with aget of type " << insn_type;
Ian Rogers89310de2012-02-01 13:47:30 -08003318 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003319 // Use knowledge of the field type which is stronger than the type inferred from the
3320 // instruction, which can't differentiate object types and ints from floats, longs from
3321 // doubles.
3322 if (!component_type.IsLowHalf()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003323 work_line_->SetRegisterType(inst->VRegA_23x(), component_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003324 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003325 work_line_->SetRegisterTypeWide(inst->VRegA_23x(), component_type,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003326 component_type.HighHalf(&reg_types_));
3327 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003328 }
3329 }
3330 }
3331}
3332
Jeff Haofe1f7c82013-08-01 14:50:24 -07003333void MethodVerifier::VerifyPrimitivePut(const RegType& target_type, const RegType& insn_type,
3334 const uint32_t vregA) {
3335 // Primitive assignability rules are weaker than regular assignability rules.
3336 bool instruction_compatible;
3337 bool value_compatible;
3338 const RegType& value_type = work_line_->GetRegisterType(vregA);
3339 if (target_type.IsIntegralTypes()) {
Jeff Haoa4647482013-08-06 15:35:47 -07003340 instruction_compatible = target_type.Equals(insn_type);
Jeff Haofe1f7c82013-08-01 14:50:24 -07003341 value_compatible = value_type.IsIntegralTypes();
3342 } else if (target_type.IsFloat()) {
3343 instruction_compatible = insn_type.IsInteger(); // no put-float, so expect put-int
3344 value_compatible = value_type.IsFloatTypes();
3345 } else if (target_type.IsLong()) {
3346 instruction_compatible = insn_type.IsLong();
3347 value_compatible = value_type.IsLongTypes();
3348 } else if (target_type.IsDouble()) {
3349 instruction_compatible = insn_type.IsLong(); // no put-double, so expect put-long
3350 value_compatible = value_type.IsDoubleTypes();
3351 } else {
3352 instruction_compatible = false; // reference with primitive store
3353 value_compatible = false; // unused
3354 }
3355 if (!instruction_compatible) {
3356 // This is a global failure rather than a class change failure as the instructions and
3357 // the descriptors for the type should have been consistent within the same file at
3358 // compile time.
3359 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "put insn has type '" << insn_type
3360 << "' but expected type '" << target_type << "'";
3361 return;
3362 }
3363 if (!value_compatible) {
3364 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
3365 << " of type " << value_type << " but expected " << target_type << " for put";
3366 return;
3367 }
3368}
3369
Sebastien Hertz5243e912013-05-21 10:55:07 +02003370void MethodVerifier::VerifyAPut(const Instruction* inst,
Sebastien Hertz757b3042014-03-28 14:34:28 +01003371 const RegType& insn_type, bool is_primitive) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003372 const RegType& index_type = work_line_->GetRegisterType(inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07003373 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003374 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07003375 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003376 const RegType& array_type = work_line_->GetRegisterType(inst->VRegB_23x());
Ian Rogers89310de2012-02-01 13:47:30 -08003377 if (array_type.IsZero()) {
3378 // Null array type; this code path will fail at runtime. Infer a merge-able type from the
3379 // instruction type.
jeffhaofc3144e2012-02-01 17:21:15 -08003380 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003381 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aput";
Ian Rogers89310de2012-02-01 13:47:30 -08003382 } else {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003383 const RegType& component_type = reg_types_.GetComponentType(array_type, class_loader_->Get());
Jeff Haofe1f7c82013-08-01 14:50:24 -07003384 const uint32_t vregA = inst->VRegA_23x();
Jeff Haob24b4a72013-07-31 13:47:31 -07003385 if (is_primitive) {
Jeff Haofe1f7c82013-08-01 14:50:24 -07003386 VerifyPrimitivePut(component_type, insn_type, vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07003387 } else {
Jeff Haob24b4a72013-07-31 13:47:31 -07003388 if (!component_type.IsReferenceTypes()) {
3389 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
3390 << " source for aput-object";
3391 } else {
3392 // The instruction agrees with the type of array, confirm the value to be stored does too
3393 // Note: we use the instruction type (rather than the component type) for aput-object as
3394 // incompatible classes will be caught at runtime as an array store exception
Jeff Haofe1f7c82013-08-01 14:50:24 -07003395 work_line_->VerifyRegisterType(vregA, insn_type);
Jeff Haob24b4a72013-07-31 13:47:31 -07003396 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003397 }
3398 }
3399 }
3400}
3401
Brian Carlstromea46f952013-07-30 01:26:50 -07003402mirror::ArtField* MethodVerifier::GetStaticField(int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08003403 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3404 // Check access to class
3405 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Brian Carlstrom7934ac22013-07-26 10:54:15 -07003406 if (klass_type.IsConflict()) { // bad class
Ian Rogersad0b3a32012-04-16 14:50:24 -07003407 AppendToLastFailMessage(StringPrintf(" in attempt to access static field %d (%s) in %s",
3408 field_idx, dex_file_->GetFieldName(field_id),
3409 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers90040192011-12-16 08:54:29 -08003410 return NULL;
3411 }
Elliott Hughesb25c3f62012-03-26 16:35:06 -07003412 if (klass_type.IsUnresolvedTypes()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003413 return NULL; // Can't resolve Class so no more to do here, will do checking at runtime.
Ian Rogers90040192011-12-16 08:54:29 -08003414 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07003415 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
3416 mirror::ArtField* field = class_linker->ResolveFieldJLS(*dex_file_, field_idx, *dex_cache_,
3417 *class_loader_);
Ian Rogersd81871c2011-10-03 13:57:23 -07003418 if (field == NULL) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07003419 VLOG(verifier) << "Unable to resolve static field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07003420 << dex_file_->GetFieldName(field_id) << ") in "
3421 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07003422 DCHECK(Thread::Current()->IsExceptionPending());
3423 Thread::Current()->ClearException();
3424 return NULL;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003425 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
3426 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003427 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access static field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07003428 << " from " << GetDeclaringClass();
Ian Rogersd81871c2011-10-03 13:57:23 -07003429 return NULL;
3430 } else if (!field->IsStatic()) {
3431 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field) << " to be static";
3432 return NULL;
Ian Rogersd81871c2011-10-03 13:57:23 -07003433 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07003434 return field;
Ian Rogersd81871c2011-10-03 13:57:23 -07003435}
3436
Brian Carlstromea46f952013-07-30 01:26:50 -07003437mirror::ArtField* MethodVerifier::GetInstanceField(const RegType& obj_type, int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08003438 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3439 // Check access to class
3440 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003441 if (klass_type.IsConflict()) {
3442 AppendToLastFailMessage(StringPrintf(" in attempt to access instance field %d (%s) in %s",
3443 field_idx, dex_file_->GetFieldName(field_id),
3444 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers90040192011-12-16 08:54:29 -08003445 return NULL;
3446 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003447 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers90040192011-12-16 08:54:29 -08003448 return NULL; // Can't resolve Class so no more to do here
3449 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07003450 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
3451 mirror::ArtField* field = class_linker->ResolveFieldJLS(*dex_file_, field_idx, *dex_cache_,
3452 *class_loader_);
Ian Rogersd81871c2011-10-03 13:57:23 -07003453 if (field == NULL) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07003454 VLOG(verifier) << "Unable to resolve instance field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07003455 << dex_file_->GetFieldName(field_id) << ") in "
3456 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07003457 DCHECK(Thread::Current()->IsExceptionPending());
3458 Thread::Current()->ClearException();
3459 return NULL;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003460 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
3461 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003462 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access instance field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07003463 << " from " << GetDeclaringClass();
Ian Rogersd81871c2011-10-03 13:57:23 -07003464 return NULL;
3465 } else if (field->IsStatic()) {
3466 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field)
3467 << " to not be static";
3468 return NULL;
3469 } else if (obj_type.IsZero()) {
3470 // Cannot infer and check type, however, access will cause null pointer exception
3471 return field;
Ian Rogerse1758fe2012-04-19 11:31:15 -07003472 } else {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003473 mirror::Class* klass = field->GetDeclaringClass();
Ian Rogers637c65b2013-05-31 11:46:00 -07003474 const RegType& field_klass =
3475 reg_types_.FromClass(dex_file_->GetFieldDeclaringClassDescriptor(field_id),
Ian Rogers04f94f42013-06-10 15:09:26 -07003476 klass, klass->CannotBeAssignedFromOtherTypes());
Ian Rogersad0b3a32012-04-16 14:50:24 -07003477 if (obj_type.IsUninitializedTypes() &&
3478 (!IsConstructor() || GetDeclaringClass().Equals(obj_type) ||
3479 !field_klass.Equals(GetDeclaringClass()))) {
3480 // Field accesses through uninitialized references are only allowable for constructors where
3481 // the field is declared in this class
3482 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "cannot access instance field " << PrettyField(field)
Brian Carlstrom93c33962013-07-26 10:37:43 -07003483 << " of a not fully initialized object within the context"
3484 << " of " << PrettyMethod(dex_method_idx_, *dex_file_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003485 return NULL;
3486 } else if (!field_klass.IsAssignableFrom(obj_type)) {
3487 // Trying to access C1.field1 using reference of type C2, which is neither C1 or a sub-class
3488 // of C1. For resolution to occur the declared class of the field must be compatible with
3489 // obj_type, we've discovered this wasn't so, so report the field didn't exist.
3490 Fail(VERIFY_ERROR_NO_FIELD) << "cannot access instance field " << PrettyField(field)
3491 << " from object of type " << obj_type;
3492 return NULL;
3493 } else {
3494 return field;
3495 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003496 }
3497}
3498
Sebastien Hertz5243e912013-05-21 10:55:07 +02003499void MethodVerifier::VerifyISGet(const Instruction* inst, const RegType& insn_type,
3500 bool is_primitive, bool is_static) {
3501 uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
Brian Carlstromea46f952013-07-30 01:26:50 -07003502 mirror::ArtField* field;
Ian Rogersb94a27b2011-10-26 00:33:41 -07003503 if (is_static) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07003504 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003505 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003506 const RegType& object_type = work_line_->GetRegisterType(inst->VRegB_22c());
Ian Rogersf4028cc2011-11-02 14:56:39 -07003507 field = GetInstanceField(object_type, field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003508 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003509 const RegType* field_type = nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003510 if (field != NULL) {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003511 FieldHelper fh(field);
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08003512 mirror::Class* field_type_class = fh.GetType(can_load_classes_);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003513 if (field_type_class != nullptr) {
3514 field_type = &reg_types_.FromClass(fh.GetTypeDescriptor(), field_type_class,
3515 field_type_class->CannotBeAssignedFromOtherTypes());
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08003516 } else {
3517 Thread* self = Thread::Current();
3518 DCHECK(!can_load_classes_ || self->IsExceptionPending());
3519 self->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003520 }
Ian Rogers0d604842012-04-16 14:50:24 -07003521 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003522 if (field_type == nullptr) {
3523 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3524 const char* descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003525 field_type = &reg_types_.FromDescriptor(class_loader_->Get(), descriptor, false);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003526 }
Sebastien Hertz757b3042014-03-28 14:34:28 +01003527 DCHECK(field_type != nullptr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02003528 const uint32_t vregA = (is_static) ? inst->VRegA_21c() : inst->VRegA_22c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07003529 if (is_primitive) {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003530 if (field_type->Equals(insn_type) ||
3531 (field_type->IsFloat() && insn_type.IsInteger()) ||
3532 (field_type->IsDouble() && insn_type.IsLong())) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003533 // expected that read is of the correct primitive type or that int reads are reading
3534 // floats or long reads are reading doubles
3535 } else {
3536 // This is a global failure rather than a class change failure as the instructions and
3537 // the descriptors for the type should have been consistent within the same file at
3538 // compile time
3539 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
3540 << " to be of type '" << insn_type
Sebastien Hertz757b3042014-03-28 14:34:28 +01003541 << "' but found type '" << *field_type << "' in get";
Ian Rogersad0b3a32012-04-16 14:50:24 -07003542 return;
3543 }
3544 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003545 if (!insn_type.IsAssignableFrom(*field_type)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003546 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
3547 << " to be compatible with type '" << insn_type
Sebastien Hertz757b3042014-03-28 14:34:28 +01003548 << "' but found type '" << *field_type
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003549 << "' in Get-object";
Sebastien Hertz5243e912013-05-21 10:55:07 +02003550 work_line_->SetRegisterType(vregA, reg_types_.Conflict());
Ian Rogersad0b3a32012-04-16 14:50:24 -07003551 return;
3552 }
3553 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003554 if (!field_type->IsLowHalf()) {
3555 work_line_->SetRegisterType(vregA, *field_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003556 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003557 work_line_->SetRegisterTypeWide(vregA, *field_type, field_type->HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003558 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003559}
3560
Sebastien Hertz5243e912013-05-21 10:55:07 +02003561void MethodVerifier::VerifyISPut(const Instruction* inst, const RegType& insn_type,
3562 bool is_primitive, bool is_static) {
3563 uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
Brian Carlstromea46f952013-07-30 01:26:50 -07003564 mirror::ArtField* field;
Ian Rogersb94a27b2011-10-26 00:33:41 -07003565 if (is_static) {
Ian Rogers55d249f2011-11-02 16:48:09 -07003566 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003567 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003568 const RegType& object_type = work_line_->GetRegisterType(inst->VRegB_22c());
Ian Rogers55d249f2011-11-02 16:48:09 -07003569 field = GetInstanceField(object_type, field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003570 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003571 const RegType* field_type = nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003572 if (field != NULL) {
3573 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
3574 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
3575 << " from other class " << GetDeclaringClass();
3576 return;
3577 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003578 FieldHelper fh(field);
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08003579 mirror::Class* field_type_class = fh.GetType(can_load_classes_);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003580 if (field_type_class != nullptr) {
3581 field_type = &reg_types_.FromClass(fh.GetTypeDescriptor(), field_type_class,
3582 field_type_class->CannotBeAssignedFromOtherTypes());
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08003583 } else {
3584 Thread* self = Thread::Current();
3585 DCHECK(!can_load_classes_ || self->IsExceptionPending());
3586 self->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003587 }
3588 }
3589 if (field_type == nullptr) {
3590 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3591 const char* descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003592 field_type = &reg_types_.FromDescriptor(class_loader_->Get(), descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003593 }
Sebastien Hertz757b3042014-03-28 14:34:28 +01003594 DCHECK(field_type != nullptr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02003595 const uint32_t vregA = (is_static) ? inst->VRegA_21c() : inst->VRegA_22c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07003596 if (is_primitive) {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003597 VerifyPrimitivePut(*field_type, insn_type, vregA);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003598 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003599 if (!insn_type.IsAssignableFrom(*field_type)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003600 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
3601 << " to be compatible with type '" << insn_type
Sebastien Hertz757b3042014-03-28 14:34:28 +01003602 << "' but found type '" << *field_type
Ian Rogersad0b3a32012-04-16 14:50:24 -07003603 << "' in put-object";
3604 return;
3605 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003606 work_line_->VerifyRegisterType(vregA, *field_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07003607 }
3608}
3609
Brian Carlstromea46f952013-07-30 01:26:50 -07003610mirror::ArtField* MethodVerifier::GetQuickFieldAccess(const Instruction* inst,
Ian Rogers98379392014-02-24 16:53:16 -08003611 RegisterLine* reg_line) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003612 DCHECK(inst->Opcode() == Instruction::IGET_QUICK ||
3613 inst->Opcode() == Instruction::IGET_WIDE_QUICK ||
3614 inst->Opcode() == Instruction::IGET_OBJECT_QUICK ||
3615 inst->Opcode() == Instruction::IPUT_QUICK ||
3616 inst->Opcode() == Instruction::IPUT_WIDE_QUICK ||
3617 inst->Opcode() == Instruction::IPUT_OBJECT_QUICK);
3618 const RegType& object_type = reg_line->GetRegisterType(inst->VRegB_22c());
Ian Rogers9bc54402014-04-17 16:40:01 -07003619 if (!object_type.HasClass()) {
3620 VLOG(verifier) << "Failed to get mirror::Class* from '" << object_type << "'";
3621 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003622 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003623 uint32_t field_offset = static_cast<uint32_t>(inst->VRegC_22c());
Sebastien Hertz479fc1e2014-04-04 17:51:34 +02003624 mirror::ArtField* f = mirror::ArtField::FindInstanceFieldWithOffset(object_type.GetClass(),
3625 field_offset);
3626 if (f == nullptr) {
3627 VLOG(verifier) << "Failed to find instance field at offset '" << field_offset
3628 << "' from '" << PrettyDescriptor(object_type.GetClass()) << "'";
3629 }
3630 return f;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003631}
3632
3633void MethodVerifier::VerifyIGetQuick(const Instruction* inst, const RegType& insn_type,
3634 bool is_primitive) {
3635 DCHECK(Runtime::Current()->IsStarted());
Brian Carlstromea46f952013-07-30 01:26:50 -07003636 mirror::ArtField* field = GetQuickFieldAccess(inst, work_line_.get());
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003637 if (field == NULL) {
3638 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field from " << inst->Name();
3639 return;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003640 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003641 FieldHelper fh(field);
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08003642 mirror::Class* field_type_class = fh.GetType(can_load_classes_);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003643 const RegType* field_type;
3644 if (field_type_class != nullptr) {
3645 field_type = &reg_types_.FromClass(fh.GetTypeDescriptor(), field_type_class,
3646 field_type_class->CannotBeAssignedFromOtherTypes());
3647 } else {
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08003648 Thread* self = Thread::Current();
3649 DCHECK(!can_load_classes_ || self->IsExceptionPending());
3650 self->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003651 field_type = &reg_types_.FromDescriptor(field->GetDeclaringClass()->GetClassLoader(),
3652 fh.GetTypeDescriptor(), false);
3653 }
Sebastien Hertz757b3042014-03-28 14:34:28 +01003654 DCHECK(field_type != nullptr);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003655 const uint32_t vregA = inst->VRegA_22c();
3656 if (is_primitive) {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003657 if (field_type->Equals(insn_type) ||
3658 (field_type->IsFloat() && insn_type.IsIntegralTypes()) ||
3659 (field_type->IsDouble() && insn_type.IsLongTypes())) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003660 // expected that read is of the correct primitive type or that int reads are reading
3661 // floats or long reads are reading doubles
3662 } else {
3663 // This is a global failure rather than a class change failure as the instructions and
3664 // the descriptors for the type should have been consistent within the same file at
3665 // compile time
3666 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003667 << " to be of type '" << insn_type
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003668 << "' but found type '" << *field_type << "' in Get";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003669 return;
3670 }
3671 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003672 if (!insn_type.IsAssignableFrom(*field_type)) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003673 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003674 << " to be compatible with type '" << insn_type
Sebastien Hertz757b3042014-03-28 14:34:28 +01003675 << "' but found type '" << *field_type
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003676 << "' in get-object";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003677 work_line_->SetRegisterType(vregA, reg_types_.Conflict());
3678 return;
3679 }
3680 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003681 if (!field_type->IsLowHalf()) {
3682 work_line_->SetRegisterType(vregA, *field_type);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003683 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003684 work_line_->SetRegisterTypeWide(vregA, *field_type, field_type->HighHalf(&reg_types_));
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003685 }
3686}
3687
3688void MethodVerifier::VerifyIPutQuick(const Instruction* inst, const RegType& insn_type,
3689 bool is_primitive) {
3690 DCHECK(Runtime::Current()->IsStarted());
Brian Carlstromea46f952013-07-30 01:26:50 -07003691 mirror::ArtField* field = GetQuickFieldAccess(inst, work_line_.get());
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003692 if (field == NULL) {
3693 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field from " << inst->Name();
3694 return;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003695 }
3696 const char* descriptor = FieldHelper(field).GetTypeDescriptor();
3697 mirror::ClassLoader* loader = field->GetDeclaringClass()->GetClassLoader();
3698 const RegType& field_type = reg_types_.FromDescriptor(loader, descriptor, false);
3699 if (field != NULL) {
3700 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
3701 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003702 << " from other class " << GetDeclaringClass();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003703 return;
3704 }
3705 }
3706 const uint32_t vregA = inst->VRegA_22c();
3707 if (is_primitive) {
3708 // Primitive field assignability rules are weaker than regular assignability rules
3709 bool instruction_compatible;
3710 bool value_compatible;
3711 const RegType& value_type = work_line_->GetRegisterType(vregA);
3712 if (field_type.IsIntegralTypes()) {
3713 instruction_compatible = insn_type.IsIntegralTypes();
3714 value_compatible = value_type.IsIntegralTypes();
3715 } else if (field_type.IsFloat()) {
3716 instruction_compatible = insn_type.IsInteger(); // no [is]put-float, so expect [is]put-int
3717 value_compatible = value_type.IsFloatTypes();
3718 } else if (field_type.IsLong()) {
3719 instruction_compatible = insn_type.IsLong();
3720 value_compatible = value_type.IsLongTypes();
3721 } else if (field_type.IsDouble()) {
3722 instruction_compatible = insn_type.IsLong(); // no [is]put-double, so expect [is]put-long
3723 value_compatible = value_type.IsDoubleTypes();
3724 } else {
3725 instruction_compatible = false; // reference field with primitive store
3726 value_compatible = false; // unused
3727 }
3728 if (!instruction_compatible) {
3729 // This is a global failure rather than a class change failure as the instructions and
3730 // the descriptors for the type should have been consistent within the same file at
3731 // compile time
3732 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003733 << " to be of type '" << insn_type
3734 << "' but found type '" << field_type
3735 << "' in put";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003736 return;
3737 }
3738 if (!value_compatible) {
3739 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
3740 << " of type " << value_type
3741 << " but expected " << field_type
3742 << " for store to " << PrettyField(field) << " in put";
3743 return;
3744 }
3745 } else {
3746 if (!insn_type.IsAssignableFrom(field_type)) {
3747 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003748 << " to be compatible with type '" << insn_type
3749 << "' but found type '" << field_type
3750 << "' in put-object";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003751 return;
3752 }
3753 work_line_->VerifyRegisterType(vregA, field_type);
3754 }
3755}
3756
Ian Rogers776ac1f2012-04-13 23:36:36 -07003757bool MethodVerifier::CheckNotMoveException(const uint16_t* insns, int insn_idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003758 if ((insns[insn_idx] & 0xff) == Instruction::MOVE_EXCEPTION) {
jeffhaod5347e02012-03-22 17:25:05 -07003759 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-exception";
Ian Rogersd81871c2011-10-03 13:57:23 -07003760 return false;
3761 }
3762 return true;
3763}
3764
Ian Rogers776ac1f2012-04-13 23:36:36 -07003765bool MethodVerifier::UpdateRegisters(uint32_t next_insn, const RegisterLine* merge_line) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003766 bool changed = true;
3767 RegisterLine* target_line = reg_table_.GetLine(next_insn);
3768 if (!insn_flags_[next_insn].IsVisitedOrChanged()) {
jeffhaobdb76512011-09-07 11:43:16 -07003769 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003770 * We haven't processed this instruction before, and we haven't touched the registers here, so
3771 * there's nothing to "merge". Copy the registers over and mark it as changed. (This is the
3772 * only way a register can transition out of "unknown", so this is not just an optimization.)
jeffhaobdb76512011-09-07 11:43:16 -07003773 */
Ian Rogersb8c78592013-07-25 23:52:52 +00003774 if (!insn_flags_[next_insn].IsReturn()) {
3775 target_line->CopyFromLine(merge_line);
3776 } else {
Jeff Haob24b4a72013-07-31 13:47:31 -07003777 // Verify that the monitor stack is empty on return.
3778 if (!merge_line->VerifyMonitorStackEmpty()) {
3779 return false;
3780 }
Ian Rogersb8c78592013-07-25 23:52:52 +00003781 // For returns we only care about the operand to the return, all other registers are dead.
3782 // Initialize them as conflicts so they don't add to GC and deoptimization information.
3783 const Instruction* ret_inst = Instruction::At(code_item_->insns_ + next_insn);
3784 Instruction::Code opcode = ret_inst->Opcode();
3785 if ((opcode == Instruction::RETURN_VOID) || (opcode == Instruction::RETURN_VOID_BARRIER)) {
3786 target_line->MarkAllRegistersAsConflicts();
3787 } else {
3788 target_line->CopyFromLine(merge_line);
3789 if (opcode == Instruction::RETURN_WIDE) {
3790 target_line->MarkAllRegistersAsConflictsExceptWide(ret_inst->VRegA_11x());
3791 } else {
3792 target_line->MarkAllRegistersAsConflictsExcept(ret_inst->VRegA_11x());
3793 }
3794 }
3795 }
jeffhaobdb76512011-09-07 11:43:16 -07003796 } else {
Ian Rogers700a4022014-05-19 16:49:03 -07003797 std::unique_ptr<RegisterLine> copy(gDebugVerify ?
Ian Rogersd0fbd852013-09-24 18:17:04 -07003798 RegisterLine::Create(target_line->NumRegs(), this) :
Brian Carlstrom93c33962013-07-26 10:37:43 -07003799 NULL);
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003800 if (gDebugVerify) {
3801 copy->CopyFromLine(target_line);
3802 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003803 changed = target_line->MergeRegisters(merge_line);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003804 if (have_pending_hard_failure_) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003805 return false;
jeffhaobdb76512011-09-07 11:43:16 -07003806 }
Ian Rogers2c8a8572011-10-24 17:11:36 -07003807 if (gDebugVerify && changed) {
Elliott Hughes398f64b2012-03-26 18:05:48 -07003808 LogVerifyInfo() << "Merging at [" << reinterpret_cast<void*>(work_insn_idx_) << "]"
Elliott Hughesc073b072012-05-24 19:29:17 -07003809 << " to [" << reinterpret_cast<void*>(next_insn) << "]: " << "\n"
3810 << *copy.get() << " MERGE\n"
3811 << *merge_line << " ==\n"
3812 << *target_line << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07003813 }
3814 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003815 if (changed) {
3816 insn_flags_[next_insn].SetChanged();
jeffhaobdb76512011-09-07 11:43:16 -07003817 }
3818 return true;
3819}
3820
Ian Rogers7b3ddd22013-02-21 15:19:52 -08003821InstructionFlags* MethodVerifier::CurrentInsnFlags() {
Ian Rogers776ac1f2012-04-13 23:36:36 -07003822 return &insn_flags_[work_insn_idx_];
3823}
3824
Ian Rogersad0b3a32012-04-16 14:50:24 -07003825const RegType& MethodVerifier::GetMethodReturnType() {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003826 if (return_type_ == nullptr) {
3827 if (mirror_method_ != NULL) {
3828 MethodHelper mh(mirror_method_);
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08003829 mirror::Class* return_type_class = mh.GetReturnType(can_load_classes_);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003830 if (return_type_class != nullptr) {
Mathieu Chartier590fee92013-09-13 13:46:47 -07003831 return_type_ = &reg_types_.FromClass(mh.GetReturnTypeDescriptor(), return_type_class,
3832 return_type_class->CannotBeAssignedFromOtherTypes());
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003833 } else {
3834 Thread* self = Thread::Current();
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08003835 DCHECK(!can_load_classes_ || self->IsExceptionPending());
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003836 self->ClearException();
3837 }
3838 }
3839 if (return_type_ == nullptr) {
3840 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
3841 const DexFile::ProtoId& proto_id = dex_file_->GetMethodPrototype(method_id);
3842 uint16_t return_type_idx = proto_id.return_type_idx_;
3843 const char* descriptor = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(return_type_idx));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003844 return_type_ = &reg_types_.FromDescriptor(class_loader_->Get(), descriptor, false);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003845 }
3846 }
3847 return *return_type_;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003848}
3849
3850const RegType& MethodVerifier::GetDeclaringClass() {
Ian Rogers637c65b2013-05-31 11:46:00 -07003851 if (declaring_class_ == NULL) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003852 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
Brian Carlstrom93c33962013-07-26 10:37:43 -07003853 const char* descriptor
3854 = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(method_id.class_idx_));
Ian Rogers637c65b2013-05-31 11:46:00 -07003855 if (mirror_method_ != NULL) {
3856 mirror::Class* klass = mirror_method_->GetDeclaringClass();
Ian Rogers04f94f42013-06-10 15:09:26 -07003857 declaring_class_ = &reg_types_.FromClass(descriptor, klass,
3858 klass->CannotBeAssignedFromOtherTypes());
Ian Rogers637c65b2013-05-31 11:46:00 -07003859 } else {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003860 declaring_class_ = &reg_types_.FromDescriptor(class_loader_->Get(), descriptor, false);
Ian Rogers637c65b2013-05-31 11:46:00 -07003861 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003862 }
Ian Rogers637c65b2013-05-31 11:46:00 -07003863 return *declaring_class_;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003864}
3865
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003866std::vector<int32_t> MethodVerifier::DescribeVRegs(uint32_t dex_pc) {
3867 RegisterLine* line = reg_table_.GetLine(dex_pc);
Sebastien Hertzaa0c00c2014-03-14 17:58:54 +01003868 DCHECK(line != nullptr) << "No register line at DEX pc " << StringPrintf("0x%x", dex_pc);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003869 std::vector<int32_t> result;
3870 for (size_t i = 0; i < line->NumRegs(); ++i) {
3871 const RegType& type = line->GetRegisterType(i);
3872 if (type.IsConstant()) {
3873 result.push_back(type.IsPreciseConstant() ? kConstant : kImpreciseConstant);
3874 result.push_back(type.ConstantValue());
3875 } else if (type.IsConstantLo()) {
3876 result.push_back(type.IsPreciseConstantLo() ? kConstant : kImpreciseConstant);
3877 result.push_back(type.ConstantValueLo());
3878 } else if (type.IsConstantHi()) {
3879 result.push_back(type.IsPreciseConstantHi() ? kConstant : kImpreciseConstant);
3880 result.push_back(type.ConstantValueHi());
3881 } else if (type.IsIntegralTypes()) {
3882 result.push_back(kIntVReg);
3883 result.push_back(0);
3884 } else if (type.IsFloat()) {
3885 result.push_back(kFloatVReg);
3886 result.push_back(0);
3887 } else if (type.IsLong()) {
3888 result.push_back(kLongLoVReg);
3889 result.push_back(0);
3890 result.push_back(kLongHiVReg);
3891 result.push_back(0);
3892 ++i;
3893 } else if (type.IsDouble()) {
3894 result.push_back(kDoubleLoVReg);
3895 result.push_back(0);
3896 result.push_back(kDoubleHiVReg);
3897 result.push_back(0);
3898 ++i;
3899 } else if (type.IsUndefined() || type.IsConflict() || type.IsHighHalf()) {
3900 result.push_back(kUndefined);
3901 result.push_back(0);
3902 } else {
Ian Rogers7b3ddd22013-02-21 15:19:52 -08003903 CHECK(type.IsNonZeroReferenceTypes());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003904 result.push_back(kReferenceVReg);
3905 result.push_back(0);
3906 }
3907 }
3908 return result;
3909}
3910
Sebastien Hertz849600b2013-12-20 10:28:08 +01003911const RegType& MethodVerifier::DetermineCat1Constant(int32_t value, bool precise) {
3912 if (precise) {
3913 // Precise constant type.
3914 return reg_types_.FromCat1Const(value, true);
3915 } else {
3916 // Imprecise constant type.
3917 if (value < -32768) {
3918 return reg_types_.IntConstant();
3919 } else if (value < -128) {
3920 return reg_types_.ShortConstant();
3921 } else if (value < 0) {
3922 return reg_types_.ByteConstant();
3923 } else if (value == 0) {
3924 return reg_types_.Zero();
3925 } else if (value == 1) {
3926 return reg_types_.One();
3927 } else if (value < 128) {
3928 return reg_types_.PosByteConstant();
3929 } else if (value < 32768) {
3930 return reg_types_.PosShortConstant();
3931 } else if (value < 65536) {
3932 return reg_types_.CharConstant();
3933 } else {
3934 return reg_types_.IntConstant();
3935 }
3936 }
3937}
3938
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003939void MethodVerifier::Init() {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08003940 art::verifier::RegTypeCache::Init();
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003941}
3942
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003943void MethodVerifier::Shutdown() {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08003944 verifier::RegTypeCache::ShutDown();
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08003945}
jeffhaod1224c72012-02-29 13:43:08 -08003946
Mathieu Chartier83c8ee02014-01-28 14:50:23 -08003947void MethodVerifier::VisitRoots(RootCallback* callback, void* arg) {
3948 reg_types_.VisitRoots(callback, arg);
Mathieu Chartierc528dba2013-11-26 12:00:11 -08003949}
3950
Ian Rogersd81871c2011-10-03 13:57:23 -07003951} // namespace verifier
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07003952} // namespace art