blob: 555714f8f58e80f6486113f966b89d54d5813524 [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
Ian Rogers776ac1f2012-04-13 23:36:36 -070017#include "method_verifier.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"
Elliott Hughese222ee02012-12-13 14:41:43 -080043#include "verifier/dex_gc_map.h"
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070044
45namespace art {
Ian Rogersd81871c2011-10-03 13:57:23 -070046namespace verifier {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070047
Ian Rogers2c8a8572011-10-24 17:11:36 -070048static const bool gDebugVerify = false;
Anwar Ghuloum75a43f12013-08-13 17:22:14 -070049// TODO: Add a constant to method_verifier to turn on verbose logging?
Ian Rogers2c8a8572011-10-24 17:11:36 -070050
Ian Rogers7b3ddd22013-02-21 15:19:52 -080051void PcToRegisterLineTable::Init(RegisterTrackingMode mode, InstructionFlags* flags,
Ian Rogersd81871c2011-10-03 13:57:23 -070052 uint32_t insns_size, uint16_t registers_size,
Ian Rogers776ac1f2012-04-13 23:36:36 -070053 MethodVerifier* verifier) {
Ian Rogersd81871c2011-10-03 13:57:23 -070054 DCHECK_GT(insns_size, 0U);
Ian Rogersd0fbd852013-09-24 18:17:04 -070055 register_lines_.reset(new RegisterLine*[insns_size]());
56 size_ = insns_size;
Ian Rogersd81871c2011-10-03 13:57:23 -070057 for (uint32_t i = 0; i < insns_size; i++) {
58 bool interesting = false;
59 switch (mode) {
60 case kTrackRegsAll:
61 interesting = flags[i].IsOpcode();
62 break;
Sameer Abu Asal02c42232013-04-30 12:09:45 -070063 case kTrackCompilerInterestPoints:
Brian Carlstrom02c8cc62013-07-18 15:54:44 -070064 interesting = flags[i].IsCompileTimeInfoPoint() || flags[i].IsBranchTarget();
Ian Rogersd81871c2011-10-03 13:57:23 -070065 break;
66 case kTrackRegsBranches:
67 interesting = flags[i].IsBranchTarget();
68 break;
69 default:
70 break;
71 }
72 if (interesting) {
Ian Rogersd0fbd852013-09-24 18:17:04 -070073 register_lines_[i] = RegisterLine::Create(registers_size, verifier);
74 }
75 }
76}
77
78PcToRegisterLineTable::~PcToRegisterLineTable() {
79 for (size_t i = 0; i < size_; i++) {
80 delete register_lines_[i];
81 if (kIsDebugBuild) {
82 register_lines_[i] = nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -070083 }
84 }
85}
86
Ian Rogersef7d42f2014-01-06 12:55:46 -080087MethodVerifier::FailureKind MethodVerifier::VerifyClass(mirror::Class* klass,
Ian Rogers8b2c0b92013-09-19 02:56:49 -070088 bool allow_soft_failures,
89 std::string* error) {
jeffhaobdb76512011-09-07 11:43:16 -070090 if (klass->IsVerified()) {
jeffhaof1e6b7c2012-06-05 18:33:30 -070091 return kNoFailure;
jeffhaobdb76512011-09-07 11:43:16 -070092 }
Jeff Hao2d7e5aa2013-12-13 17:39:59 -080093 bool early_failure = false;
94 std::string failure_message;
Ian Rogersad0b3a32012-04-16 14:50:24 -070095 ClassHelper kh(klass);
96 const DexFile& dex_file = kh.GetDexFile();
Ian Rogers8b2c0b92013-09-19 02:56:49 -070097 const DexFile::ClassDef* class_def = kh.GetClassDef();
Jeff Hao2d7e5aa2013-12-13 17:39:59 -080098 mirror::Class* super = klass->GetSuperClass();
99 if (super == NULL && strcmp("Ljava/lang/Object;", kh.GetDescriptor()) != 0) {
100 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 Chartier590fee92013-09-13 13:46:47 -0700117 Thread* self = Thread::Current();
118 SirtRef<mirror::DexCache> dex_cache(self, kh.GetDexCache());
119 SirtRef<mirror::ClassLoader> class_loader(self, klass->GetClassLoader());
120 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 Chartier590fee92013-09-13 13:46:47 -0700124 SirtRef<mirror::DexCache>& dex_cache,
125 SirtRef<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 Chartier590fee92013-09-13 13:46:47 -0700235 SirtRef<mirror::DexCache>& dex_cache,
236 SirtRef<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,
280 SirtRef<mirror::DexCache>& dex_cache,
281 SirtRef<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 Chartier590fee92013-09-13 13:46:47 -0700294MethodVerifier::MethodVerifier(const DexFile* dex_file, SirtRef<mirror::DexCache>* dex_cache,
295 SirtRef<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 Chartier590fee92013-09-13 13:46:47 -0700334 Thread* self = Thread::Current();
335 SirtRef<mirror::DexCache> dex_cache(self, mh.GetDexCache());
336 SirtRef<mirror::ClassLoader> class_loader(self, mh.GetClassLoader());
337 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 Chartier590fee92013-09-13 13:46:47 -0700359 Thread* self = Thread::Current();
360 SirtRef<mirror::DexCache> dex_cache(self, mh.GetDexCache());
361 SirtRef<mirror::ClassLoader> class_loader(self, mh.GetClassLoader());
362 MethodVerifier verifier(&mh.GetDexFile(), &dex_cache, &class_loader, &mh.GetClassDef(),
363 mh.GetCodeItem(), m->GetDexMethodIndex(), m, m->GetAccessFlags(), false,
364 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) {
377 return NULL;
378 }
379 RegisterLine* register_line = reg_table_.GetLine(dex_pc);
380 if (register_line == NULL) {
381 return NULL;
382 }
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 Chartier590fee92013-09-13 13:46:47 -0700390 Thread* self = Thread::Current();
391 SirtRef<mirror::DexCache> dex_cache(self, mh.GetDexCache());
392 SirtRef<mirror::ClassLoader> class_loader(self, mh.GetClassLoader());
393 MethodVerifier verifier(&mh.GetDexFile(), &dex_cache, &class_loader, &mh.GetClassDef(),
394 mh.GetCodeItem(), m->GetDexMethodIndex(), m, m->GetAccessFlags(), false,
395 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) {
Elliott Hughesadb8c672012-03-06 16:49:32 -0800651 DecodedInstruction dec_insn(inst);
Ian Rogersd81871c2011-10-03 13:57:23 -0700652 bool result = true;
653 switch (inst->GetVerifyTypeArgumentA()) {
654 case Instruction::kVerifyRegA:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800655 result = result && CheckRegisterIndex(dec_insn.vA);
Ian Rogersd81871c2011-10-03 13:57:23 -0700656 break;
657 case Instruction::kVerifyRegAWide:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800658 result = result && CheckWideRegisterIndex(dec_insn.vA);
Ian Rogersd81871c2011-10-03 13:57:23 -0700659 break;
660 }
661 switch (inst->GetVerifyTypeArgumentB()) {
662 case Instruction::kVerifyRegB:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800663 result = result && CheckRegisterIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700664 break;
665 case Instruction::kVerifyRegBField:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800666 result = result && CheckFieldIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700667 break;
668 case Instruction::kVerifyRegBMethod:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800669 result = result && CheckMethodIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700670 break;
671 case Instruction::kVerifyRegBNewInstance:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800672 result = result && CheckNewInstance(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700673 break;
674 case Instruction::kVerifyRegBString:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800675 result = result && CheckStringIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700676 break;
677 case Instruction::kVerifyRegBType:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800678 result = result && CheckTypeIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700679 break;
680 case Instruction::kVerifyRegBWide:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800681 result = result && CheckWideRegisterIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700682 break;
683 }
684 switch (inst->GetVerifyTypeArgumentC()) {
685 case Instruction::kVerifyRegC:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800686 result = result && CheckRegisterIndex(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700687 break;
688 case Instruction::kVerifyRegCField:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800689 result = result && CheckFieldIndex(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700690 break;
691 case Instruction::kVerifyRegCNewArray:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800692 result = result && CheckNewArray(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700693 break;
694 case Instruction::kVerifyRegCType:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800695 result = result && CheckTypeIndex(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700696 break;
697 case Instruction::kVerifyRegCWide:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800698 result = result && CheckWideRegisterIndex(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700699 break;
700 }
701 switch (inst->GetVerifyExtraFlags()) {
702 case Instruction::kVerifyArrayData:
703 result = result && CheckArrayData(code_offset);
704 break;
705 case Instruction::kVerifyBranchTarget:
706 result = result && CheckBranchTarget(code_offset);
707 break;
708 case Instruction::kVerifySwitchTargets:
709 result = result && CheckSwitchTargets(code_offset);
710 break;
711 case Instruction::kVerifyVarArg:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800712 result = result && CheckVarArgRegs(dec_insn.vA, dec_insn.arg);
Ian Rogersd81871c2011-10-03 13:57:23 -0700713 break;
714 case Instruction::kVerifyVarArgRange:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800715 result = result && CheckVarArgRangeRegs(dec_insn.vA, dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700716 break;
717 case Instruction::kVerifyError:
jeffhaod5347e02012-03-22 17:25:05 -0700718 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected opcode " << inst->Name();
Ian Rogersd81871c2011-10-03 13:57:23 -0700719 result = false;
720 break;
721 }
722 return result;
723}
724
Ian Rogers776ac1f2012-04-13 23:36:36 -0700725bool MethodVerifier::CheckRegisterIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700726 if (idx >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700727 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register index out of range (" << idx << " >= "
728 << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700729 return false;
730 }
731 return true;
732}
733
Ian Rogers776ac1f2012-04-13 23:36:36 -0700734bool MethodVerifier::CheckWideRegisterIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700735 if (idx + 1 >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700736 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "wide register index out of range (" << idx
737 << "+1 >= " << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700738 return false;
739 }
740 return true;
741}
742
Ian Rogers776ac1f2012-04-13 23:36:36 -0700743bool MethodVerifier::CheckFieldIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700744 if (idx >= dex_file_->GetHeader().field_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700745 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad field index " << idx << " (max "
746 << dex_file_->GetHeader().field_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700747 return false;
748 }
749 return true;
750}
751
Ian Rogers776ac1f2012-04-13 23:36:36 -0700752bool MethodVerifier::CheckMethodIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700753 if (idx >= dex_file_->GetHeader().method_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700754 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad method index " << idx << " (max "
755 << dex_file_->GetHeader().method_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700756 return false;
757 }
758 return true;
759}
760
Ian Rogers776ac1f2012-04-13 23:36:36 -0700761bool MethodVerifier::CheckNewInstance(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700762 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700763 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
764 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700765 return false;
766 }
767 // We don't need the actual class, just a pointer to the class name.
Ian Rogers0571d352011-11-03 19:51:38 -0700768 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -0700769 if (descriptor[0] != 'L') {
jeffhaod5347e02012-03-22 17:25:05 -0700770 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "can't call new-instance on type '" << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -0700771 return false;
772 }
773 return true;
774}
775
Ian Rogers776ac1f2012-04-13 23:36:36 -0700776bool MethodVerifier::CheckStringIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700777 if (idx >= dex_file_->GetHeader().string_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700778 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad string index " << idx << " (max "
779 << dex_file_->GetHeader().string_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700780 return false;
781 }
782 return true;
783}
784
Ian Rogers776ac1f2012-04-13 23:36:36 -0700785bool MethodVerifier::CheckTypeIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700786 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700787 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
788 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700789 return false;
790 }
791 return true;
792}
793
Ian Rogers776ac1f2012-04-13 23:36:36 -0700794bool MethodVerifier::CheckNewArray(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700795 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700796 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
797 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700798 return false;
799 }
800 int bracket_count = 0;
Ian Rogers0571d352011-11-03 19:51:38 -0700801 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -0700802 const char* cp = descriptor;
803 while (*cp++ == '[') {
804 bracket_count++;
805 }
806 if (bracket_count == 0) {
807 /* The given class must be an array type. */
Brian Carlstrom93c33962013-07-26 10:37:43 -0700808 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
809 << "can't new-array class '" << descriptor << "' (not an array)";
Ian Rogersd81871c2011-10-03 13:57:23 -0700810 return false;
811 } else if (bracket_count > 255) {
812 /* It is illegal to create an array of more than 255 dimensions. */
Brian Carlstrom93c33962013-07-26 10:37:43 -0700813 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
814 << "can't new-array class '" << descriptor << "' (exceeds limit)";
Ian Rogersd81871c2011-10-03 13:57:23 -0700815 return false;
816 }
817 return true;
818}
819
Ian Rogers776ac1f2012-04-13 23:36:36 -0700820bool MethodVerifier::CheckArrayData(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700821 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
822 const uint16_t* insns = code_item_->insns_ + cur_offset;
823 const uint16_t* array_data;
824 int32_t array_data_offset;
825
826 DCHECK_LT(cur_offset, insn_count);
827 /* make sure the start of the array data table is in range */
828 array_data_offset = insns[1] | (((int32_t) insns[2]) << 16);
829 if ((int32_t) cur_offset + array_data_offset < 0 ||
830 cur_offset + array_data_offset + 2 >= insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -0700831 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data start: at " << cur_offset
Brian Carlstrom93c33962013-07-26 10:37:43 -0700832 << ", data offset " << array_data_offset
833 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -0700834 return false;
835 }
836 /* offset to array data table is a relative branch-style offset */
837 array_data = insns + array_data_offset;
838 /* make sure the table is 32-bit aligned */
Ian Rogersef7d42f2014-01-06 12:55:46 -0800839 if ((reinterpret_cast<uintptr_t>(array_data) & 0x03) != 0) {
jeffhaod5347e02012-03-22 17:25:05 -0700840 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned array data table: at " << cur_offset
841 << ", data offset " << array_data_offset;
Ian Rogersd81871c2011-10-03 13:57:23 -0700842 return false;
843 }
844 uint32_t value_width = array_data[1];
Elliott Hughes398f64b2012-03-26 18:05:48 -0700845 uint32_t value_count = *reinterpret_cast<const uint32_t*>(&array_data[2]);
Ian Rogersd81871c2011-10-03 13:57:23 -0700846 uint32_t table_size = 4 + (value_width * value_count + 1) / 2;
847 /* make sure the end of the switch is in range */
848 if (cur_offset + array_data_offset + table_size > insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -0700849 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data end: at " << cur_offset
850 << ", data offset " << array_data_offset << ", end "
851 << cur_offset + array_data_offset + table_size
852 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -0700853 return false;
854 }
855 return true;
856}
857
Ian Rogers776ac1f2012-04-13 23:36:36 -0700858bool MethodVerifier::CheckBranchTarget(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700859 int32_t offset;
860 bool isConditional, selfOkay;
861 if (!GetBranchOffset(cur_offset, &offset, &isConditional, &selfOkay)) {
862 return false;
863 }
864 if (!selfOkay && offset == 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700865 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch offset of zero not allowed at"
866 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -0700867 return false;
868 }
Elliott Hughes81ff3182012-03-23 20:35:56 -0700869 // Check for 32-bit overflow. This isn't strictly necessary if we can depend on the runtime
870 // to have identical "wrap-around" behavior, but it's unwise to depend on that.
Ian Rogersd81871c2011-10-03 13:57:23 -0700871 if (((int64_t) cur_offset + (int64_t) offset) != (int64_t) (cur_offset + offset)) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700872 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch target overflow "
873 << reinterpret_cast<void*>(cur_offset) << " +" << offset;
Ian Rogersd81871c2011-10-03 13:57:23 -0700874 return false;
875 }
876 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
877 int32_t abs_offset = cur_offset + offset;
Brian Carlstrom93c33962013-07-26 10:37:43 -0700878 if (abs_offset < 0 ||
879 (uint32_t) abs_offset >= insn_count ||
880 !insn_flags_[abs_offset].IsOpcode()) {
jeffhaod5347e02012-03-22 17:25:05 -0700881 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid branch target " << offset << " (-> "
Elliott Hughes398f64b2012-03-26 18:05:48 -0700882 << reinterpret_cast<void*>(abs_offset) << ") at "
883 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -0700884 return false;
885 }
886 insn_flags_[abs_offset].SetBranchTarget();
887 return true;
888}
889
Ian Rogers776ac1f2012-04-13 23:36:36 -0700890bool MethodVerifier::GetBranchOffset(uint32_t cur_offset, int32_t* pOffset, bool* pConditional,
Ian Rogersd81871c2011-10-03 13:57:23 -0700891 bool* selfOkay) {
892 const uint16_t* insns = code_item_->insns_ + cur_offset;
893 *pConditional = false;
894 *selfOkay = false;
jeffhaoba5ebb92011-08-25 17:24:37 -0700895 switch (*insns & 0xff) {
896 case Instruction::GOTO:
897 *pOffset = ((int16_t) *insns) >> 8;
jeffhaoba5ebb92011-08-25 17:24:37 -0700898 break;
899 case Instruction::GOTO_32:
900 *pOffset = insns[1] | (((uint32_t) insns[2]) << 16);
jeffhaoba5ebb92011-08-25 17:24:37 -0700901 *selfOkay = true;
902 break;
903 case Instruction::GOTO_16:
904 *pOffset = (int16_t) insns[1];
jeffhaoba5ebb92011-08-25 17:24:37 -0700905 break;
906 case Instruction::IF_EQ:
907 case Instruction::IF_NE:
908 case Instruction::IF_LT:
909 case Instruction::IF_GE:
910 case Instruction::IF_GT:
911 case Instruction::IF_LE:
912 case Instruction::IF_EQZ:
913 case Instruction::IF_NEZ:
914 case Instruction::IF_LTZ:
915 case Instruction::IF_GEZ:
916 case Instruction::IF_GTZ:
917 case Instruction::IF_LEZ:
918 *pOffset = (int16_t) insns[1];
919 *pConditional = true;
jeffhaoba5ebb92011-08-25 17:24:37 -0700920 break;
921 default:
922 return false;
923 break;
924 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700925 return true;
926}
927
Ian Rogers776ac1f2012-04-13 23:36:36 -0700928bool MethodVerifier::CheckSwitchTargets(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700929 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700930 DCHECK_LT(cur_offset, insn_count);
Ian Rogersd81871c2011-10-03 13:57:23 -0700931 const uint16_t* insns = code_item_->insns_ + cur_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -0700932 /* make sure the start of the switch is in range */
Ian Rogersd81871c2011-10-03 13:57:23 -0700933 int32_t switch_offset = insns[1] | ((int32_t) insns[2]) << 16;
934 if ((int32_t) cur_offset + switch_offset < 0 || cur_offset + switch_offset + 2 >= insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -0700935 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch start: at " << cur_offset
Brian Carlstrom93c33962013-07-26 10:37:43 -0700936 << ", switch offset " << switch_offset
937 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -0700938 return false;
939 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700940 /* offset to switch table is a relative branch-style offset */
Ian Rogersd81871c2011-10-03 13:57:23 -0700941 const uint16_t* switch_insns = insns + switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -0700942 /* make sure the table is 32-bit aligned */
Ian Rogersef7d42f2014-01-06 12:55:46 -0800943 if ((reinterpret_cast<uintptr_t>(switch_insns) & 0x03) != 0) {
jeffhaod5347e02012-03-22 17:25:05 -0700944 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned switch table: at " << cur_offset
945 << ", switch offset " << switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -0700946 return false;
947 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700948 uint32_t switch_count = switch_insns[1];
949 int32_t keys_offset, targets_offset;
950 uint16_t expected_signature;
jeffhaoba5ebb92011-08-25 17:24:37 -0700951 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
952 /* 0=sig, 1=count, 2/3=firstKey */
953 targets_offset = 4;
954 keys_offset = -1;
955 expected_signature = Instruction::kPackedSwitchSignature;
956 } else {
957 /* 0=sig, 1=count, 2..count*2 = keys */
958 keys_offset = 2;
959 targets_offset = 2 + 2 * switch_count;
960 expected_signature = Instruction::kSparseSwitchSignature;
961 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700962 uint32_t table_size = targets_offset + switch_count * 2;
jeffhaoba5ebb92011-08-25 17:24:37 -0700963 if (switch_insns[0] != expected_signature) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700964 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
965 << StringPrintf("wrong signature for switch table (%x, wanted %x)",
966 switch_insns[0], expected_signature);
jeffhaoba5ebb92011-08-25 17:24:37 -0700967 return false;
968 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700969 /* make sure the end of the switch is in range */
970 if (cur_offset + switch_offset + table_size > (uint32_t) insn_count) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700971 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch end: at " << cur_offset
972 << ", switch offset " << switch_offset
973 << ", end " << (cur_offset + switch_offset + table_size)
jeffhaod5347e02012-03-22 17:25:05 -0700974 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -0700975 return false;
976 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700977 /* for a sparse switch, verify the keys are in ascending order */
978 if (keys_offset > 0 && switch_count > 1) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700979 int32_t last_key = switch_insns[keys_offset] | (switch_insns[keys_offset + 1] << 16);
980 for (uint32_t targ = 1; targ < switch_count; targ++) {
jeffhaoba5ebb92011-08-25 17:24:37 -0700981 int32_t key = (int32_t) switch_insns[keys_offset + targ * 2] |
982 (int32_t) (switch_insns[keys_offset + targ * 2 + 1] << 16);
983 if (key <= last_key) {
jeffhaod5347e02012-03-22 17:25:05 -0700984 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid packed switch: last key=" << last_key
985 << ", this=" << key;
jeffhaoba5ebb92011-08-25 17:24:37 -0700986 return false;
987 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700988 last_key = key;
989 }
990 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700991 /* verify each switch target */
Ian Rogersd81871c2011-10-03 13:57:23 -0700992 for (uint32_t targ = 0; targ < switch_count; targ++) {
993 int32_t offset = (int32_t) switch_insns[targets_offset + targ * 2] |
994 (int32_t) (switch_insns[targets_offset + targ * 2 + 1] << 16);
995 int32_t abs_offset = cur_offset + offset;
Brian Carlstrom93c33962013-07-26 10:37:43 -0700996 if (abs_offset < 0 ||
997 abs_offset >= (int32_t) insn_count ||
998 !insn_flags_[abs_offset].IsOpcode()) {
999 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch target " << offset
1000 << " (-> " << reinterpret_cast<void*>(abs_offset) << ") at "
1001 << reinterpret_cast<void*>(cur_offset)
1002 << "[" << targ << "]";
jeffhaoba5ebb92011-08-25 17:24:37 -07001003 return false;
1004 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001005 insn_flags_[abs_offset].SetBranchTarget();
1006 }
1007 return true;
1008}
1009
Ian Rogers776ac1f2012-04-13 23:36:36 -07001010bool MethodVerifier::CheckVarArgRegs(uint32_t vA, uint32_t arg[]) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001011 if (vA > 5) {
jeffhaod5347e02012-03-22 17:25:05 -07001012 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << vA << ") in non-range invoke)";
Ian Rogersd81871c2011-10-03 13:57:23 -07001013 return false;
1014 }
1015 uint16_t registers_size = code_item_->registers_size_;
1016 for (uint32_t idx = 0; idx < vA; idx++) {
jeffhao457cc512012-02-02 16:55:13 -08001017 if (arg[idx] >= registers_size) {
jeffhaod5347e02012-03-22 17:25:05 -07001018 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index (" << arg[idx]
1019 << ") in non-range invoke (>= " << registers_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001020 return false;
1021 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001022 }
1023
1024 return true;
1025}
1026
Ian Rogers776ac1f2012-04-13 23:36:36 -07001027bool MethodVerifier::CheckVarArgRangeRegs(uint32_t vA, uint32_t vC) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001028 uint16_t registers_size = code_item_->registers_size_;
1029 // vA/vC are unsigned 8-bit/16-bit quantities for /range instructions, so there's no risk of
1030 // integer overflow when adding them here.
1031 if (vA + vC > registers_size) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001032 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index " << vA << "+" << vC
1033 << " in range invoke (> " << registers_size << ")";
jeffhaoba5ebb92011-08-25 17:24:37 -07001034 return false;
1035 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001036 return true;
1037}
1038
Ian Rogers776ac1f2012-04-13 23:36:36 -07001039bool MethodVerifier::VerifyCodeFlow() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001040 uint16_t registers_size = code_item_->registers_size_;
1041 uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaobdb76512011-09-07 11:43:16 -07001042
Ian Rogersd81871c2011-10-03 13:57:23 -07001043 if (registers_size * insns_size > 4*1024*1024) {
buzbee4922ef92012-02-24 14:32:20 -08001044 LOG(WARNING) << "warning: method is huge (regs=" << registers_size
1045 << " insns_size=" << insns_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001046 }
1047 /* Create and initialize table holding register status */
Brian Carlstrom93c33962013-07-26 10:37:43 -07001048 reg_table_.Init(kTrackCompilerInterestPoints,
1049 insn_flags_.get(),
1050 insns_size,
1051 registers_size,
1052 this);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07001053
jeffhaobdb76512011-09-07 11:43:16 -07001054
Ian Rogersd0fbd852013-09-24 18:17:04 -07001055 work_line_.reset(RegisterLine::Create(registers_size, this));
1056 saved_line_.reset(RegisterLine::Create(registers_size, this));
jeffhaobdb76512011-09-07 11:43:16 -07001057
Ian Rogersd81871c2011-10-03 13:57:23 -07001058 /* Initialize register types of method arguments. */
1059 if (!SetTypesFromSignature()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001060 DCHECK_NE(failures_.size(), 0U);
1061 std::string prepend("Bad signature in ");
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001062 prepend += PrettyMethod(dex_method_idx_, *dex_file_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001063 PrependToLastFailMessage(prepend);
Ian Rogersd81871c2011-10-03 13:57:23 -07001064 return false;
1065 }
1066 /* Perform code flow verification. */
1067 if (!CodeFlowVerifyMethod()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001068 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -07001069 return false;
jeffhaobdb76512011-09-07 11:43:16 -07001070 }
jeffhaobdb76512011-09-07 11:43:16 -07001071 return true;
1072}
1073
Ian Rogersad0b3a32012-04-16 14:50:24 -07001074std::ostream& MethodVerifier::DumpFailures(std::ostream& os) {
1075 DCHECK_EQ(failures_.size(), failure_messages_.size());
Jeff Hao4137f482013-11-22 11:44:57 -08001076 for (size_t i = 0; i < failures_.size(); ++i) {
1077 os << failure_messages_[i]->str() << "\n";
Ian Rogersad0b3a32012-04-16 14:50:24 -07001078 }
1079 return os;
1080}
1081
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001082extern "C" void MethodVerifierGdbDump(MethodVerifier* v)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001083 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001084 v->Dump(std::cerr);
1085}
1086
Ian Rogers776ac1f2012-04-13 23:36:36 -07001087void MethodVerifier::Dump(std::ostream& os) {
jeffhaof56197c2012-03-05 18:01:54 -08001088 if (code_item_ == NULL) {
Elliott Hughesc073b072012-05-24 19:29:17 -07001089 os << "Native method\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001090 return;
jeffhaobdb76512011-09-07 11:43:16 -07001091 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001092 {
1093 os << "Register Types:\n";
1094 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
1095 std::ostream indent_os(&indent_filter);
1096 reg_types_.Dump(indent_os);
1097 }
Ian Rogersb4903572012-10-11 11:52:56 -07001098 os << "Dumping instructions and register lines:\n";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001099 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
1100 std::ostream indent_os(&indent_filter);
Ian Rogersd81871c2011-10-03 13:57:23 -07001101 const Instruction* inst = Instruction::At(code_item_->insns_);
1102 for (size_t dex_pc = 0; dex_pc < code_item_->insns_size_in_code_units_;
1103 dex_pc += insn_flags_[dex_pc].GetLengthInCodeUnits()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001104 RegisterLine* reg_line = reg_table_.GetLine(dex_pc);
1105 if (reg_line != NULL) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001106 indent_os << reg_line->Dump() << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07001107 }
Ian Rogers7b3ddd22013-02-21 15:19:52 -08001108 indent_os << StringPrintf("0x%04zx", dex_pc) << ": " << insn_flags_[dex_pc].ToString() << " ";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001109 const bool kDumpHexOfInstruction = false;
1110 if (kDumpHexOfInstruction) {
1111 indent_os << inst->DumpHex(5) << " ";
1112 }
1113 indent_os << inst->DumpString(dex_file_) << "\n";
jeffhaoba5ebb92011-08-25 17:24:37 -07001114 inst = inst->Next();
1115 }
jeffhaobdb76512011-09-07 11:43:16 -07001116}
1117
Ian Rogersd81871c2011-10-03 13:57:23 -07001118static bool IsPrimitiveDescriptor(char descriptor) {
1119 switch (descriptor) {
jeffhaobdb76512011-09-07 11:43:16 -07001120 case 'I':
1121 case 'C':
1122 case 'S':
1123 case 'B':
1124 case 'Z':
jeffhaobdb76512011-09-07 11:43:16 -07001125 case 'F':
1126 case 'D':
1127 case 'J':
Ian Rogersd81871c2011-10-03 13:57:23 -07001128 return true;
jeffhaobdb76512011-09-07 11:43:16 -07001129 default:
1130 return false;
1131 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001132}
1133
Ian Rogers776ac1f2012-04-13 23:36:36 -07001134bool MethodVerifier::SetTypesFromSignature() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001135 RegisterLine* reg_line = reg_table_.GetLine(0);
1136 int arg_start = code_item_->registers_size_ - code_item_->ins_size_;
1137 size_t expected_args = code_item_->ins_size_; /* long/double count as two */
jeffhaobdb76512011-09-07 11:43:16 -07001138
Ian Rogersd81871c2011-10-03 13:57:23 -07001139 DCHECK_GE(arg_start, 0); /* should have been verified earlier */
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001140 // Include the "this" pointer.
Ian Rogersd81871c2011-10-03 13:57:23 -07001141 size_t cur_arg = 0;
Ian Rogersad0b3a32012-04-16 14:50:24 -07001142 if (!IsStatic()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001143 // If this is a constructor for a class other than java.lang.Object, mark the first ("this")
1144 // argument as uninitialized. This restricts field access until the superclass constructor is
1145 // called.
Ian Rogersad0b3a32012-04-16 14:50:24 -07001146 const RegType& declaring_class = GetDeclaringClass();
1147 if (IsConstructor() && !declaring_class.IsJavaLangObject()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001148 reg_line->SetRegisterType(arg_start + cur_arg,
1149 reg_types_.UninitializedThisArgument(declaring_class));
1150 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001151 reg_line->SetRegisterType(arg_start + cur_arg, declaring_class);
jeffhaobdb76512011-09-07 11:43:16 -07001152 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001153 cur_arg++;
jeffhaobdb76512011-09-07 11:43:16 -07001154 }
1155
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001156 const DexFile::ProtoId& proto_id =
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001157 dex_file_->GetMethodPrototype(dex_file_->GetMethodId(dex_method_idx_));
Ian Rogers0571d352011-11-03 19:51:38 -07001158 DexFileParameterIterator iterator(*dex_file_, proto_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07001159
1160 for (; iterator.HasNext(); iterator.Next()) {
1161 const char* descriptor = iterator.GetDescriptor();
1162 if (descriptor == NULL) {
1163 LOG(FATAL) << "Null descriptor";
1164 }
1165 if (cur_arg >= expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07001166 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1167 << " args, found more (" << descriptor << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001168 return false;
1169 }
1170 switch (descriptor[0]) {
1171 case 'L':
1172 case '[':
1173 // We assume that reference arguments are initialized. The only way it could be otherwise
1174 // (assuming the caller was verified) is if the current method is <init>, but in that case
1175 // it's effectively considered initialized the instant we reach here (in the sense that we
1176 // can return without doing anything or call virtual methods).
1177 {
Mathieu Chartier590fee92013-09-13 13:46:47 -07001178 const RegType& reg_type = reg_types_.FromDescriptor(class_loader_->get(), descriptor,
1179 false);
Ian Rogers84fa0742011-10-25 18:13:30 -07001180 reg_line->SetRegisterType(arg_start + cur_arg, reg_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001181 }
1182 break;
1183 case 'Z':
1184 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Boolean());
1185 break;
1186 case 'C':
1187 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Char());
1188 break;
1189 case 'B':
1190 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Byte());
1191 break;
1192 case 'I':
1193 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Integer());
1194 break;
1195 case 'S':
1196 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Short());
1197 break;
1198 case 'F':
1199 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Float());
1200 break;
1201 case 'J':
1202 case 'D': {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001203 const RegType& lo_half = descriptor[0] == 'J' ? reg_types_.LongLo() : reg_types_.DoubleLo();
1204 const RegType& hi_half = descriptor[0] == 'J' ? reg_types_.LongHi() : reg_types_.DoubleHi();
1205 reg_line->SetRegisterTypeWide(arg_start + cur_arg, lo_half, hi_half);
Ian Rogersd81871c2011-10-03 13:57:23 -07001206 cur_arg++;
1207 break;
1208 }
1209 default:
Brian Carlstrom93c33962013-07-26 10:37:43 -07001210 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected signature type char '"
1211 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001212 return false;
1213 }
1214 cur_arg++;
1215 }
1216 if (cur_arg != expected_args) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001217 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1218 << " arguments, found " << cur_arg;
Ian Rogersd81871c2011-10-03 13:57:23 -07001219 return false;
1220 }
1221 const char* descriptor = dex_file_->GetReturnTypeDescriptor(proto_id);
1222 // Validate return type. We don't do the type lookup; just want to make sure that it has the right
1223 // format. Only major difference from the method argument format is that 'V' is supported.
1224 bool result;
1225 if (IsPrimitiveDescriptor(descriptor[0]) || descriptor[0] == 'V') {
1226 result = descriptor[1] == '\0';
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001227 } else if (descriptor[0] == '[') { // single/multi-dimensional array of object/primitive
Ian Rogersd81871c2011-10-03 13:57:23 -07001228 size_t i = 0;
1229 do {
1230 i++;
1231 } while (descriptor[i] == '['); // process leading [
1232 if (descriptor[i] == 'L') { // object array
1233 do {
1234 i++; // find closing ;
1235 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1236 result = descriptor[i] == ';';
1237 } else { // primitive array
1238 result = IsPrimitiveDescriptor(descriptor[i]) && descriptor[i + 1] == '\0';
1239 }
1240 } else if (descriptor[0] == 'L') {
1241 // could be more thorough here, but shouldn't be required
1242 size_t i = 0;
1243 do {
1244 i++;
1245 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1246 result = descriptor[i] == ';';
1247 } else {
1248 result = false;
1249 }
1250 if (!result) {
jeffhaod5347e02012-03-22 17:25:05 -07001251 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected char in return type descriptor '"
1252 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001253 }
1254 return result;
jeffhaobdb76512011-09-07 11:43:16 -07001255}
1256
Ian Rogers776ac1f2012-04-13 23:36:36 -07001257bool MethodVerifier::CodeFlowVerifyMethod() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001258 const uint16_t* insns = code_item_->insns_;
1259 const uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaoba5ebb92011-08-25 17:24:37 -07001260
jeffhaobdb76512011-09-07 11:43:16 -07001261 /* Begin by marking the first instruction as "changed". */
Ian Rogersd81871c2011-10-03 13:57:23 -07001262 insn_flags_[0].SetChanged();
1263 uint32_t start_guess = 0;
jeffhaoba5ebb92011-08-25 17:24:37 -07001264
jeffhaobdb76512011-09-07 11:43:16 -07001265 /* Continue until no instructions are marked "changed". */
1266 while (true) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001267 // Find the first marked one. Use "start_guess" as a way to find one quickly.
1268 uint32_t insn_idx = start_guess;
1269 for (; insn_idx < insns_size; insn_idx++) {
1270 if (insn_flags_[insn_idx].IsChanged())
jeffhaobdb76512011-09-07 11:43:16 -07001271 break;
1272 }
jeffhaobdb76512011-09-07 11:43:16 -07001273 if (insn_idx == insns_size) {
1274 if (start_guess != 0) {
1275 /* try again, starting from the top */
1276 start_guess = 0;
1277 continue;
1278 } else {
1279 /* all flags are clear */
1280 break;
1281 }
1282 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001283 // We carry the working set of registers from instruction to instruction. If this address can
1284 // be the target of a branch (or throw) instruction, or if we're skipping around chasing
1285 // "changed" flags, we need to load the set of registers from the table.
1286 // Because we always prefer to continue on to the next instruction, we should never have a
1287 // situation where we have a stray "changed" flag set on an instruction that isn't a branch
1288 // target.
1289 work_insn_idx_ = insn_idx;
1290 if (insn_flags_[insn_idx].IsBranchTarget()) {
1291 work_line_->CopyFromLine(reg_table_.GetLine(insn_idx));
jeffhaobdb76512011-09-07 11:43:16 -07001292 } else {
1293#ifndef NDEBUG
1294 /*
1295 * Sanity check: retrieve the stored register line (assuming
1296 * a full table) and make sure it actually matches.
1297 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001298 RegisterLine* register_line = reg_table_.GetLine(insn_idx);
1299 if (register_line != NULL) {
1300 if (work_line_->CompareLine(register_line) != 0) {
1301 Dump(std::cout);
1302 std::cout << info_messages_.str();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001303 LOG(FATAL) << "work_line diverged in " << PrettyMethod(dex_method_idx_, *dex_file_)
Elliott Hughesc073b072012-05-24 19:29:17 -07001304 << "@" << reinterpret_cast<void*>(work_insn_idx_) << "\n"
1305 << " work_line=" << *work_line_ << "\n"
Elliott Hughes398f64b2012-03-26 18:05:48 -07001306 << " expected=" << *register_line;
Ian Rogersd81871c2011-10-03 13:57:23 -07001307 }
jeffhaobdb76512011-09-07 11:43:16 -07001308 }
1309#endif
1310 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001311 if (!CodeFlowVerifyInstruction(&start_guess)) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001312 std::string prepend(PrettyMethod(dex_method_idx_, *dex_file_));
Ian Rogersad0b3a32012-04-16 14:50:24 -07001313 prepend += " failed to verify: ";
1314 PrependToLastFailMessage(prepend);
jeffhaoba5ebb92011-08-25 17:24:37 -07001315 return false;
1316 }
jeffhaobdb76512011-09-07 11:43:16 -07001317 /* Clear "changed" and mark as visited. */
Ian Rogersd81871c2011-10-03 13:57:23 -07001318 insn_flags_[insn_idx].SetVisited();
1319 insn_flags_[insn_idx].ClearChanged();
jeffhaobdb76512011-09-07 11:43:16 -07001320 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001321
Ian Rogers1c849e52012-06-28 14:00:33 -07001322 if (gDebugVerify) {
jeffhaobdb76512011-09-07 11:43:16 -07001323 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001324 * Scan for dead code. There's nothing "evil" about dead code
jeffhaobdb76512011-09-07 11:43:16 -07001325 * (besides the wasted space), but it indicates a flaw somewhere
1326 * down the line, possibly in the verifier.
1327 *
1328 * If we've substituted "always throw" instructions into the stream,
1329 * we are almost certainly going to have some dead code.
1330 */
1331 int dead_start = -1;
Ian Rogersd81871c2011-10-03 13:57:23 -07001332 uint32_t insn_idx = 0;
1333 for (; insn_idx < insns_size; insn_idx += insn_flags_[insn_idx].GetLengthInCodeUnits()) {
jeffhaobdb76512011-09-07 11:43:16 -07001334 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001335 * Switch-statement data doesn't get "visited" by scanner. It
jeffhaobdb76512011-09-07 11:43:16 -07001336 * may or may not be preceded by a padding NOP (for alignment).
1337 */
1338 if (insns[insn_idx] == Instruction::kPackedSwitchSignature ||
1339 insns[insn_idx] == Instruction::kSparseSwitchSignature ||
1340 insns[insn_idx] == Instruction::kArrayDataSignature ||
Elliott Hughes380aaa72012-07-09 14:33:15 -07001341 (insns[insn_idx] == Instruction::NOP && (insn_idx + 1 < insns_size) &&
jeffhaobdb76512011-09-07 11:43:16 -07001342 (insns[insn_idx + 1] == Instruction::kPackedSwitchSignature ||
1343 insns[insn_idx + 1] == Instruction::kSparseSwitchSignature ||
1344 insns[insn_idx + 1] == Instruction::kArrayDataSignature))) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001345 insn_flags_[insn_idx].SetVisited();
jeffhaobdb76512011-09-07 11:43:16 -07001346 }
1347
Ian Rogersd81871c2011-10-03 13:57:23 -07001348 if (!insn_flags_[insn_idx].IsVisited()) {
jeffhaobdb76512011-09-07 11:43:16 -07001349 if (dead_start < 0)
1350 dead_start = insn_idx;
1351 } else if (dead_start >= 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001352 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start)
1353 << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaobdb76512011-09-07 11:43:16 -07001354 dead_start = -1;
1355 }
1356 }
1357 if (dead_start >= 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001358 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start)
1359 << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaoba5ebb92011-08-25 17:24:37 -07001360 }
Ian Rogersc9e463c2013-06-05 16:52:26 -07001361 // To dump the state of the verify after a method, do something like:
1362 // if (PrettyMethod(dex_method_idx_, *dex_file_) ==
1363 // "boolean java.lang.String.equals(java.lang.Object)") {
1364 // LOG(INFO) << info_messages_.str();
1365 // }
jeffhaoba5ebb92011-08-25 17:24:37 -07001366 }
jeffhaobdb76512011-09-07 11:43:16 -07001367 return true;
1368}
1369
Ian Rogers776ac1f2012-04-13 23:36:36 -07001370bool MethodVerifier::CodeFlowVerifyInstruction(uint32_t* start_guess) {
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001371 // If we're doing FindLocksAtDexPc, check whether we're at the dex pc we care about.
1372 // We want the state _before_ the instruction, for the case where the dex pc we're
1373 // interested in is itself a monitor-enter instruction (which is a likely place
1374 // for a thread to be suspended).
1375 if (monitor_enter_dex_pcs_ != NULL && work_insn_idx_ == interesting_dex_pc_) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001376 monitor_enter_dex_pcs_->clear(); // The new work line is more accurate than the previous one.
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001377 for (size_t i = 0; i < work_line_->GetMonitorEnterCount(); ++i) {
1378 monitor_enter_dex_pcs_->push_back(work_line_->GetMonitorEnterDexPc(i));
1379 }
1380 }
1381
jeffhaobdb76512011-09-07 11:43:16 -07001382 /*
1383 * Once we finish decoding the instruction, we need to figure out where
jeffhaod1f0fde2011-09-08 17:25:33 -07001384 * we can go from here. There are three possible ways to transfer
jeffhaobdb76512011-09-07 11:43:16 -07001385 * control to another statement:
1386 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001387 * (1) Continue to the next instruction. Applies to all but
jeffhaobdb76512011-09-07 11:43:16 -07001388 * unconditional branches, method returns, and exception throws.
jeffhaod1f0fde2011-09-08 17:25:33 -07001389 * (2) Branch to one or more possible locations. Applies to branches
jeffhaobdb76512011-09-07 11:43:16 -07001390 * and switch statements.
jeffhaod1f0fde2011-09-08 17:25:33 -07001391 * (3) Exception handlers. Applies to any instruction that can
jeffhaobdb76512011-09-07 11:43:16 -07001392 * throw an exception that is handled by an encompassing "try"
1393 * block.
1394 *
1395 * We can also return, in which case there is no successor instruction
1396 * from this point.
1397 *
Elliott Hughesadb8c672012-03-06 16:49:32 -08001398 * The behavior can be determined from the opcode flags.
jeffhaobdb76512011-09-07 11:43:16 -07001399 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001400 const uint16_t* insns = code_item_->insns_ + work_insn_idx_;
1401 const Instruction* inst = Instruction::At(insns);
Ian Rogersa75a0132012-09-28 11:41:42 -07001402 int opcode_flags = Instruction::FlagsOf(inst->Opcode());
jeffhaobdb76512011-09-07 11:43:16 -07001403
jeffhaobdb76512011-09-07 11:43:16 -07001404 int32_t branch_target = 0;
jeffhaobdb76512011-09-07 11:43:16 -07001405 bool just_set_result = false;
Ian Rogers2c8a8572011-10-24 17:11:36 -07001406 if (gDebugVerify) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001407 // Generate processing back trace to debug verifier
Elliott Hughesc073b072012-05-24 19:29:17 -07001408 LogVerifyInfo() << "Processing " << inst->DumpString(dex_file_) << "\n"
1409 << *work_line_.get() << "\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001410 }
jeffhaobdb76512011-09-07 11:43:16 -07001411
1412 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001413 * Make a copy of the previous register state. If the instruction
jeffhaobdb76512011-09-07 11:43:16 -07001414 * can throw an exception, we will copy/merge this into the "catch"
1415 * address rather than work_line, because we don't want the result
1416 * from the "successful" code path (e.g. a check-cast that "improves"
1417 * a type) to be visible to the exception handler.
1418 */
Ian Rogers776ac1f2012-04-13 23:36:36 -07001419 if ((opcode_flags & Instruction::kThrow) != 0 && CurrentInsnFlags()->IsInTry()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001420 saved_line_->CopyFromLine(work_line_.get());
jeffhaobdb76512011-09-07 11:43:16 -07001421 } else {
1422#ifndef NDEBUG
Ian Rogersd81871c2011-10-03 13:57:23 -07001423 saved_line_->FillWithGarbage();
jeffhaobdb76512011-09-07 11:43:16 -07001424#endif
1425 }
1426
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07001427
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001428 // We need to ensure the work line is consistent while performing validation. When we spot a
1429 // peephole pattern we compute a new line for either the fallthrough instruction or the
1430 // branch target.
1431 UniquePtr<RegisterLine> branch_line;
1432 UniquePtr<RegisterLine> fallthrough_line;
1433
Sebastien Hertz849600b2013-12-20 10:28:08 +01001434 // We need precise constant types only for deoptimization which happens at runtime.
1435 const bool need_precise_constant = !Runtime::Current()->IsCompiler();
1436
Sebastien Hertz5243e912013-05-21 10:55:07 +02001437 switch (inst->Opcode()) {
jeffhaobdb76512011-09-07 11:43:16 -07001438 case Instruction::NOP:
1439 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001440 * A "pure" NOP has no effect on anything. Data tables start with
jeffhaobdb76512011-09-07 11:43:16 -07001441 * a signature that looks like a NOP; if we see one of these in
1442 * the course of executing code then we have a problem.
1443 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001444 if (inst->VRegA_10x() != 0) {
jeffhaod5347e02012-03-22 17:25:05 -07001445 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "encountered data table in instruction stream";
jeffhaobdb76512011-09-07 11:43:16 -07001446 }
1447 break;
1448
1449 case Instruction::MOVE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001450 work_line_->CopyRegister1(inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategory1nr);
1451 break;
jeffhaobdb76512011-09-07 11:43:16 -07001452 case Instruction::MOVE_FROM16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001453 work_line_->CopyRegister1(inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategory1nr);
1454 break;
jeffhaobdb76512011-09-07 11:43:16 -07001455 case Instruction::MOVE_16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001456 work_line_->CopyRegister1(inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategory1nr);
jeffhaobdb76512011-09-07 11:43:16 -07001457 break;
1458 case Instruction::MOVE_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001459 work_line_->CopyRegister2(inst->VRegA_12x(), inst->VRegB_12x());
1460 break;
jeffhaobdb76512011-09-07 11:43:16 -07001461 case Instruction::MOVE_WIDE_FROM16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001462 work_line_->CopyRegister2(inst->VRegA_22x(), inst->VRegB_22x());
1463 break;
jeffhaobdb76512011-09-07 11:43:16 -07001464 case Instruction::MOVE_WIDE_16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001465 work_line_->CopyRegister2(inst->VRegA_32x(), inst->VRegB_32x());
jeffhaobdb76512011-09-07 11:43:16 -07001466 break;
1467 case Instruction::MOVE_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001468 work_line_->CopyRegister1(inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategoryRef);
1469 break;
jeffhaobdb76512011-09-07 11:43:16 -07001470 case Instruction::MOVE_OBJECT_FROM16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001471 work_line_->CopyRegister1(inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategoryRef);
1472 break;
jeffhaobdb76512011-09-07 11:43:16 -07001473 case Instruction::MOVE_OBJECT_16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001474 work_line_->CopyRegister1(inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategoryRef);
jeffhaobdb76512011-09-07 11:43:16 -07001475 break;
1476
1477 /*
1478 * The move-result instructions copy data out of a "pseudo-register"
jeffhaod1f0fde2011-09-08 17:25:33 -07001479 * with the results from the last method invocation. In practice we
jeffhaobdb76512011-09-07 11:43:16 -07001480 * might want to hold the result in an actual CPU register, so the
1481 * Dalvik spec requires that these only appear immediately after an
1482 * invoke or filled-new-array.
1483 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001484 * These calls invalidate the "result" register. (This is now
jeffhaobdb76512011-09-07 11:43:16 -07001485 * redundant with the reset done below, but it can make the debug info
1486 * easier to read in some cases.)
1487 */
1488 case Instruction::MOVE_RESULT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001489 work_line_->CopyResultRegister1(inst->VRegA_11x(), false);
jeffhaobdb76512011-09-07 11:43:16 -07001490 break;
1491 case Instruction::MOVE_RESULT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001492 work_line_->CopyResultRegister2(inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07001493 break;
1494 case Instruction::MOVE_RESULT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001495 work_line_->CopyResultRegister1(inst->VRegA_11x(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001496 break;
1497
Ian Rogersd81871c2011-10-03 13:57:23 -07001498 case Instruction::MOVE_EXCEPTION: {
jeffhaobdb76512011-09-07 11:43:16 -07001499 /*
jeffhao60f83e32012-02-13 17:16:30 -08001500 * This statement can only appear as the first instruction in an exception handler. We verify
1501 * that as part of extracting the exception type from the catch block list.
jeffhaobdb76512011-09-07 11:43:16 -07001502 */
Ian Rogers28ad40d2011-10-27 15:19:26 -07001503 const RegType& res_type = GetCaughtExceptionType();
Sebastien Hertz5243e912013-05-21 10:55:07 +02001504 work_line_->SetRegisterType(inst->VRegA_11x(), res_type);
jeffhaobdb76512011-09-07 11:43:16 -07001505 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001506 }
jeffhaobdb76512011-09-07 11:43:16 -07001507 case Instruction::RETURN_VOID:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001508 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
1509 if (!GetMethodReturnType().IsConflict()) {
jeffhaod5347e02012-03-22 17:25:05 -07001510 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001511 }
jeffhaobdb76512011-09-07 11:43:16 -07001512 }
1513 break;
1514 case Instruction::RETURN:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001515 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
jeffhaobdb76512011-09-07 11:43:16 -07001516 /* check the method signature */
Ian Rogersd81871c2011-10-03 13:57:23 -07001517 const RegType& return_type = GetMethodReturnType();
1518 if (!return_type.IsCategory1Types()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001519 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected non-category 1 return type "
1520 << return_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001521 } else {
1522 // Compilers may generate synthetic functions that write byte values into boolean fields.
1523 // Also, it may use integer values for boolean, byte, short, and character return types.
Sebastien Hertz5243e912013-05-21 10:55:07 +02001524 const uint32_t vregA = inst->VRegA_11x();
1525 const RegType& src_type = work_line_->GetRegisterType(vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07001526 bool use_src = ((return_type.IsBoolean() && src_type.IsByte()) ||
1527 ((return_type.IsBoolean() || return_type.IsByte() ||
1528 return_type.IsShort() || return_type.IsChar()) &&
1529 src_type.IsInteger()));
1530 /* check the register contents */
Ian Rogersad0b3a32012-04-16 14:50:24 -07001531 bool success =
Sebastien Hertz5243e912013-05-21 10:55:07 +02001532 work_line_->VerifyRegisterType(vregA, use_src ? src_type : return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001533 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001534 AppendToLastFailMessage(StringPrintf(" return-1nr on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07001535 }
jeffhaobdb76512011-09-07 11:43:16 -07001536 }
1537 }
1538 break;
1539 case Instruction::RETURN_WIDE:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001540 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
jeffhaobdb76512011-09-07 11:43:16 -07001541 /* check the method signature */
Ian Rogersd81871c2011-10-03 13:57:23 -07001542 const RegType& return_type = GetMethodReturnType();
1543 if (!return_type.IsCategory2Types()) {
jeffhaod5347e02012-03-22 17:25:05 -07001544 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-wide not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001545 } else {
1546 /* check the register contents */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001547 const uint32_t vregA = inst->VRegA_11x();
1548 bool success = work_line_->VerifyRegisterType(vregA, return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001549 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001550 AppendToLastFailMessage(StringPrintf(" return-wide on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07001551 }
jeffhaobdb76512011-09-07 11:43:16 -07001552 }
1553 }
1554 break;
1555 case Instruction::RETURN_OBJECT:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001556 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001557 const RegType& return_type = GetMethodReturnType();
1558 if (!return_type.IsReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001559 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-object not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001560 } else {
1561 /* return_type is the *expected* return type, not register value */
1562 DCHECK(!return_type.IsZero());
1563 DCHECK(!return_type.IsUninitializedReference());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001564 const uint32_t vregA = inst->VRegA_11x();
1565 const RegType& reg_type = work_line_->GetRegisterType(vregA);
Ian Rogers9074b992011-10-26 17:41:55 -07001566 // Disallow returning uninitialized values and verify that the reference in vAA is an
1567 // instance of the "return_type"
1568 if (reg_type.IsUninitializedTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001569 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "returning uninitialized object '"
1570 << reg_type << "'";
Ian Rogers9074b992011-10-26 17:41:55 -07001571 } else if (!return_type.IsAssignableFrom(reg_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07001572 if (reg_type.IsUnresolvedTypes() || return_type.IsUnresolvedTypes()) {
1573 Fail(VERIFY_ERROR_NO_CLASS) << " can't resolve returned type '" << return_type
1574 << "' or '" << reg_type << "'";
1575 } else {
1576 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning '" << reg_type
1577 << "', but expected from declaration '" << return_type << "'";
1578 }
jeffhaobdb76512011-09-07 11:43:16 -07001579 }
1580 }
1581 }
1582 break;
1583
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001584 /* could be boolean, int, float, or a null reference */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001585 case Instruction::CONST_4: {
1586 int32_t val = static_cast<int32_t>(inst->VRegB_11n() << 28) >> 28;
Sebastien Hertz849600b2013-12-20 10:28:08 +01001587 work_line_->SetRegisterType(inst->VRegA_11n(),
1588 DetermineCat1Constant(val, need_precise_constant));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001589 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02001590 }
1591 case Instruction::CONST_16: {
1592 int16_t val = static_cast<int16_t>(inst->VRegB_21s());
Sebastien Hertz849600b2013-12-20 10:28:08 +01001593 work_line_->SetRegisterType(inst->VRegA_21s(),
1594 DetermineCat1Constant(val, need_precise_constant));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001595 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02001596 }
Sebastien Hertz849600b2013-12-20 10:28:08 +01001597 case Instruction::CONST: {
1598 int32_t val = inst->VRegB_31i();
Sebastien Hertz5243e912013-05-21 10:55:07 +02001599 work_line_->SetRegisterType(inst->VRegA_31i(),
Sebastien Hertz849600b2013-12-20 10:28:08 +01001600 DetermineCat1Constant(val, need_precise_constant));
jeffhaobdb76512011-09-07 11:43:16 -07001601 break;
Sebastien Hertz849600b2013-12-20 10:28:08 +01001602 }
1603 case Instruction::CONST_HIGH16: {
1604 int32_t val = static_cast<int32_t>(inst->VRegB_21h() << 16);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001605 work_line_->SetRegisterType(inst->VRegA_21h(),
Sebastien Hertz849600b2013-12-20 10:28:08 +01001606 DetermineCat1Constant(val, need_precise_constant));
jeffhaobdb76512011-09-07 11:43:16 -07001607 break;
Sebastien Hertz849600b2013-12-20 10:28:08 +01001608 }
jeffhaobdb76512011-09-07 11:43:16 -07001609 /* could be long or double; resolved upon use */
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001610 case Instruction::CONST_WIDE_16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001611 int64_t val = static_cast<int16_t>(inst->VRegB_21s());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001612 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1613 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001614 work_line_->SetRegisterTypeWide(inst->VRegA_21s(), lo, hi);
jeffhaobdb76512011-09-07 11:43:16 -07001615 break;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001616 }
1617 case Instruction::CONST_WIDE_32: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001618 int64_t val = static_cast<int32_t>(inst->VRegB_31i());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001619 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1620 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001621 work_line_->SetRegisterTypeWide(inst->VRegA_31i(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001622 break;
1623 }
1624 case Instruction::CONST_WIDE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001625 int64_t val = inst->VRegB_51l();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001626 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1627 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001628 work_line_->SetRegisterTypeWide(inst->VRegA_51l(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001629 break;
1630 }
1631 case Instruction::CONST_WIDE_HIGH16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001632 int64_t val = static_cast<uint64_t>(inst->VRegB_21h()) << 48;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001633 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1634 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001635 work_line_->SetRegisterTypeWide(inst->VRegA_21h(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001636 break;
1637 }
jeffhaobdb76512011-09-07 11:43:16 -07001638 case Instruction::CONST_STRING:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001639 work_line_->SetRegisterType(inst->VRegA_21c(), reg_types_.JavaLangString());
1640 break;
jeffhaobdb76512011-09-07 11:43:16 -07001641 case Instruction::CONST_STRING_JUMBO:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001642 work_line_->SetRegisterType(inst->VRegA_31c(), reg_types_.JavaLangString());
jeffhaobdb76512011-09-07 11:43:16 -07001643 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001644 case Instruction::CONST_CLASS: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001645 // Get type from instruction if unresolved then we need an access check
1646 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Sebastien Hertz5243e912013-05-21 10:55:07 +02001647 const RegType& res_type = ResolveClassAndCheckAccess(inst->VRegB_21c());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001648 // Register holds class, ie its type is class, on error it will hold Conflict.
Sebastien Hertz5243e912013-05-21 10:55:07 +02001649 work_line_->SetRegisterType(inst->VRegA_21c(),
Ian Rogersb4903572012-10-11 11:52:56 -07001650 res_type.IsConflict() ? res_type
1651 : reg_types_.JavaLangClass(true));
jeffhaobdb76512011-09-07 11:43:16 -07001652 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001653 }
jeffhaobdb76512011-09-07 11:43:16 -07001654 case Instruction::MONITOR_ENTER:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001655 work_line_->PushMonitor(inst->VRegA_11x(), work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07001656 break;
1657 case Instruction::MONITOR_EXIT:
1658 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001659 * monitor-exit instructions are odd. They can throw exceptions,
jeffhaobdb76512011-09-07 11:43:16 -07001660 * but when they do they act as if they succeeded and the PC is
jeffhaod1f0fde2011-09-08 17:25:33 -07001661 * pointing to the following instruction. (This behavior goes back
jeffhaobdb76512011-09-07 11:43:16 -07001662 * to the need to handle asynchronous exceptions, a now-deprecated
1663 * feature that Dalvik doesn't support.)
1664 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001665 * In practice we don't need to worry about this. The only
jeffhaobdb76512011-09-07 11:43:16 -07001666 * exceptions that can be thrown from monitor-exit are for a
jeffhaod1f0fde2011-09-08 17:25:33 -07001667 * null reference and -exit without a matching -enter. If the
jeffhaobdb76512011-09-07 11:43:16 -07001668 * structured locking checks are working, the former would have
1669 * failed on the -enter instruction, and the latter is impossible.
1670 *
1671 * This is fortunate, because issue 3221411 prevents us from
1672 * chasing the "can throw" path when monitor verification is
jeffhaod1f0fde2011-09-08 17:25:33 -07001673 * enabled. If we can fully verify the locking we can ignore
jeffhaobdb76512011-09-07 11:43:16 -07001674 * some catch blocks (which will show up as "dead" code when
1675 * we skip them here); if we can't, then the code path could be
1676 * "live" so we still need to check it.
1677 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001678 opcode_flags &= ~Instruction::kThrow;
Sebastien Hertz5243e912013-05-21 10:55:07 +02001679 work_line_->PopMonitor(inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07001680 break;
1681
Ian Rogers28ad40d2011-10-27 15:19:26 -07001682 case Instruction::CHECK_CAST:
Ian Rogersd81871c2011-10-03 13:57:23 -07001683 case Instruction::INSTANCE_OF: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001684 /*
1685 * If this instruction succeeds, we will "downcast" register vA to the type in vB. (This
1686 * could be a "upcast" -- not expected, so we don't try to address it.)
1687 *
1688 * If it fails, an exception is thrown, which we deal with later by ignoring the update to
Elliott Hughesadb8c672012-03-06 16:49:32 -08001689 * dec_insn.vA when branching to a handler.
Ian Rogers28ad40d2011-10-27 15:19:26 -07001690 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001691 const bool is_checkcast = (inst->Opcode() == Instruction::CHECK_CAST);
1692 const uint32_t type_idx = (is_checkcast) ? inst->VRegB_21c() : inst->VRegC_22c();
1693 const RegType& res_type = ResolveClassAndCheckAccess(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001694 if (res_type.IsConflict()) {
1695 DCHECK_NE(failures_.size(), 0U);
1696 if (!is_checkcast) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001697 work_line_->SetRegisterType(inst->VRegA_22c(), reg_types_.Boolean());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001698 }
1699 break; // bad class
Ian Rogers9f1ab122011-12-12 08:52:43 -08001700 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001701 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Sebastien Hertz5243e912013-05-21 10:55:07 +02001702 uint32_t orig_type_reg = (is_checkcast) ? inst->VRegA_21c() : inst->VRegB_22c();
1703 const RegType& orig_type = work_line_->GetRegisterType(orig_type_reg);
Ian Rogers28ad40d2011-10-27 15:19:26 -07001704 if (!res_type.IsNonZeroReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001705 if (is_checkcast) {
1706 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on unexpected class " << res_type;
1707 } else {
1708 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on unexpected class " << res_type;
1709 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001710 } else if (!orig_type.IsReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001711 if (is_checkcast) {
1712 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on non-reference in v" << orig_type_reg;
1713 } else {
1714 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on non-reference in v" << orig_type_reg;
1715 }
jeffhao2a8a90e2011-09-26 14:25:31 -07001716 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001717 if (is_checkcast) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001718 work_line_->SetRegisterType(inst->VRegA_21c(), res_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001719 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001720 work_line_->SetRegisterType(inst->VRegA_22c(), reg_types_.Boolean());
jeffhaobdb76512011-09-07 11:43:16 -07001721 }
jeffhaobdb76512011-09-07 11:43:16 -07001722 }
jeffhao2a8a90e2011-09-26 14:25:31 -07001723 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001724 }
1725 case Instruction::ARRAY_LENGTH: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001726 const RegType& res_type = work_line_->GetRegisterType(inst->VRegB_12x());
Ian Rogers28ad40d2011-10-27 15:19:26 -07001727 if (res_type.IsReferenceTypes()) {
Ian Rogers89310de2012-02-01 13:47:30 -08001728 if (!res_type.IsArrayTypes() && !res_type.IsZero()) { // ie not an array or null
jeffhaod5347e02012-03-22 17:25:05 -07001729 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001730 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001731 work_line_->SetRegisterType(inst->VRegA_12x(), reg_types_.Integer());
Ian Rogersd81871c2011-10-03 13:57:23 -07001732 }
1733 }
1734 break;
1735 }
1736 case Instruction::NEW_INSTANCE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001737 const RegType& res_type = ResolveClassAndCheckAccess(inst->VRegB_21c());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001738 if (res_type.IsConflict()) {
1739 DCHECK_NE(failures_.size(), 0U);
1740 break; // bad class
jeffhao8cd6dda2012-02-22 10:15:34 -08001741 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001742 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
1743 // can't create an instance of an interface or abstract class */
1744 if (!res_type.IsInstantiableTypes()) {
1745 Fail(VERIFY_ERROR_INSTANTIATION)
1746 << "new-instance on primitive, interface or abstract class" << res_type;
Ian Rogers08f753d2012-08-24 14:35:25 -07001747 // Soft failure so carry on to set register type.
Ian Rogersd81871c2011-10-03 13:57:23 -07001748 }
Ian Rogers08f753d2012-08-24 14:35:25 -07001749 const RegType& uninit_type = reg_types_.Uninitialized(res_type, work_insn_idx_);
1750 // Any registers holding previous allocations from this address that have not yet been
1751 // initialized must be marked invalid.
1752 work_line_->MarkUninitRefsAsInvalid(uninit_type);
1753 // add the new uninitialized reference to the register state
Sebastien Hertz5243e912013-05-21 10:55:07 +02001754 work_line_->SetRegisterType(inst->VRegA_21c(), uninit_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001755 break;
1756 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08001757 case Instruction::NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001758 VerifyNewArray(inst, false, false);
jeffhaobdb76512011-09-07 11:43:16 -07001759 break;
1760 case Instruction::FILLED_NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001761 VerifyNewArray(inst, true, false);
Ian Rogers0c4a5062012-02-03 15:18:59 -08001762 just_set_result = true; // Filled new array sets result register
jeffhaobdb76512011-09-07 11:43:16 -07001763 break;
Ian Rogers0c4a5062012-02-03 15:18:59 -08001764 case Instruction::FILLED_NEW_ARRAY_RANGE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001765 VerifyNewArray(inst, true, true);
Ian Rogers0c4a5062012-02-03 15:18:59 -08001766 just_set_result = true; // Filled new array range sets result register
1767 break;
jeffhaobdb76512011-09-07 11:43:16 -07001768 case Instruction::CMPL_FLOAT:
1769 case Instruction::CMPG_FLOAT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001770 if (!work_line_->VerifyRegisterType(inst->VRegB_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08001771 break;
1772 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02001773 if (!work_line_->VerifyRegisterType(inst->VRegC_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08001774 break;
1775 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02001776 work_line_->SetRegisterType(inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001777 break;
1778 case Instruction::CMPL_DOUBLE:
1779 case Instruction::CMPG_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001780 if (!work_line_->VerifyRegisterTypeWide(inst->VRegB_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001781 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001782 break;
1783 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02001784 if (!work_line_->VerifyRegisterTypeWide(inst->VRegC_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001785 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001786 break;
1787 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02001788 work_line_->SetRegisterType(inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001789 break;
1790 case Instruction::CMP_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001791 if (!work_line_->VerifyRegisterTypeWide(inst->VRegB_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001792 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001793 break;
1794 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02001795 if (!work_line_->VerifyRegisterTypeWide(inst->VRegC_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001796 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001797 break;
1798 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02001799 work_line_->SetRegisterType(inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001800 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001801 case Instruction::THROW: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001802 const RegType& res_type = work_line_->GetRegisterType(inst->VRegA_11x());
Ian Rogersb4903572012-10-11 11:52:56 -07001803 if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(res_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07001804 Fail(res_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS : VERIFY_ERROR_BAD_CLASS_SOFT)
1805 << "thrown class " << res_type << " not instanceof Throwable";
jeffhaobdb76512011-09-07 11:43:16 -07001806 }
1807 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001808 }
jeffhaobdb76512011-09-07 11:43:16 -07001809 case Instruction::GOTO:
1810 case Instruction::GOTO_16:
1811 case Instruction::GOTO_32:
1812 /* no effect on or use of registers */
1813 break;
1814
1815 case Instruction::PACKED_SWITCH:
1816 case Instruction::SPARSE_SWITCH:
1817 /* verify that vAA is an integer, or can be converted to one */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001818 work_line_->VerifyRegisterType(inst->VRegA_31t(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001819 break;
1820
Ian Rogersd81871c2011-10-03 13:57:23 -07001821 case Instruction::FILL_ARRAY_DATA: {
1822 /* Similar to the verification done for APUT */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001823 const RegType& array_type = work_line_->GetRegisterType(inst->VRegA_31t());
Ian Rogers89310de2012-02-01 13:47:30 -08001824 /* array_type can be null if the reg type is Zero */
1825 if (!array_type.IsZero()) {
jeffhao457cc512012-02-02 16:55:13 -08001826 if (!array_type.IsArrayTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001827 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with array type "
1828 << array_type;
Ian Rogers89310de2012-02-01 13:47:30 -08001829 } else {
Mathieu Chartier590fee92013-09-13 13:46:47 -07001830 const RegType& component_type = reg_types_.GetComponentType(array_type,
1831 class_loader_->get());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001832 DCHECK(!component_type.IsConflict());
jeffhao457cc512012-02-02 16:55:13 -08001833 if (component_type.IsNonZeroReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001834 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with component type "
1835 << component_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001836 } else {
jeffhao457cc512012-02-02 16:55:13 -08001837 // Now verify if the element width in the table matches the element width declared in
1838 // the array
1839 const uint16_t* array_data = insns + (insns[1] | (((int32_t) insns[2]) << 16));
1840 if (array_data[0] != Instruction::kArrayDataSignature) {
jeffhaod5347e02012-03-22 17:25:05 -07001841 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid magic for array-data";
jeffhao457cc512012-02-02 16:55:13 -08001842 } else {
1843 size_t elem_width = Primitive::ComponentSize(component_type.GetPrimitiveType());
1844 // Since we don't compress the data in Dex, expect to see equal width of data stored
1845 // in the table and expected from the array class.
1846 if (array_data[1] != elem_width) {
jeffhaod5347e02012-03-22 17:25:05 -07001847 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-data size mismatch (" << array_data[1]
1848 << " vs " << elem_width << ")";
jeffhao457cc512012-02-02 16:55:13 -08001849 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001850 }
1851 }
jeffhaobdb76512011-09-07 11:43:16 -07001852 }
1853 }
1854 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001855 }
jeffhaobdb76512011-09-07 11:43:16 -07001856 case Instruction::IF_EQ:
Ian Rogersd81871c2011-10-03 13:57:23 -07001857 case Instruction::IF_NE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001858 const RegType& reg_type1 = work_line_->GetRegisterType(inst->VRegA_22t());
1859 const RegType& reg_type2 = work_line_->GetRegisterType(inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07001860 bool mismatch = false;
1861 if (reg_type1.IsZero()) { // zero then integral or reference expected
1862 mismatch = !reg_type2.IsReferenceTypes() && !reg_type2.IsIntegralTypes();
1863 } else if (reg_type1.IsReferenceTypes()) { // both references?
1864 mismatch = !reg_type2.IsReferenceTypes();
1865 } else { // both integral?
1866 mismatch = !reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes();
1867 }
1868 if (mismatch) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001869 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to if-eq/if-ne (" << reg_type1 << ","
1870 << reg_type2 << ") must both be references or integral";
jeffhaobdb76512011-09-07 11:43:16 -07001871 }
1872 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001873 }
jeffhaobdb76512011-09-07 11:43:16 -07001874 case Instruction::IF_LT:
1875 case Instruction::IF_GE:
1876 case Instruction::IF_GT:
Ian Rogersd81871c2011-10-03 13:57:23 -07001877 case Instruction::IF_LE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001878 const RegType& reg_type1 = work_line_->GetRegisterType(inst->VRegA_22t());
1879 const RegType& reg_type2 = work_line_->GetRegisterType(inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07001880 if (!reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001881 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to 'if' (" << reg_type1 << ","
1882 << reg_type2 << ") must be integral";
jeffhaobdb76512011-09-07 11:43:16 -07001883 }
1884 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001885 }
jeffhaobdb76512011-09-07 11:43:16 -07001886 case Instruction::IF_EQZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07001887 case Instruction::IF_NEZ: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001888 const RegType& reg_type = work_line_->GetRegisterType(inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07001889 if (!reg_type.IsReferenceTypes() && !reg_type.IsIntegralTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001890 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
1891 << " unexpected as arg to if-eqz/if-nez";
Ian Rogersd81871c2011-10-03 13:57:23 -07001892 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001893
1894 // Find previous instruction - its existence is a precondition to peephole optimization.
Ian Rogers9b360392013-06-06 14:45:07 -07001895 uint32_t instance_of_idx = 0;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001896 if (0 != work_insn_idx_) {
Ian Rogers9b360392013-06-06 14:45:07 -07001897 instance_of_idx = work_insn_idx_ - 1;
Brian Carlstromdf629502013-07-17 22:39:56 -07001898 while (0 != instance_of_idx && !insn_flags_[instance_of_idx].IsOpcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07001899 instance_of_idx--;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001900 }
Ian Rogers9b360392013-06-06 14:45:07 -07001901 CHECK(insn_flags_[instance_of_idx].IsOpcode());
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001902 } else {
1903 break;
1904 }
1905
Ian Rogers9b360392013-06-06 14:45:07 -07001906 const Instruction* instance_of_inst = Instruction::At(code_item_->insns_ + instance_of_idx);
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001907
1908 /* Check for peep-hole pattern of:
1909 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07001910 * instance-of vX, vY, T;
1911 * ifXXX vX, label ;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001912 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07001913 * label:
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001914 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07001915 * and sharpen the type of vY to be type T.
1916 * Note, this pattern can't be if:
1917 * - if there are other branches to this branch,
1918 * - when vX == vY.
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001919 */
Ian Rogersfae370a2013-06-05 08:33:27 -07001920 if (!CurrentInsnFlags()->IsBranchTarget() &&
Ian Rogers9b360392013-06-06 14:45:07 -07001921 (Instruction::INSTANCE_OF == instance_of_inst->Opcode()) &&
1922 (inst->VRegA_21t() == instance_of_inst->VRegA_22c()) &&
1923 (instance_of_inst->VRegA_22c() != instance_of_inst->VRegB_22c())) {
Ian Rogersfae370a2013-06-05 08:33:27 -07001924 // Check that the we are not attempting conversion to interface types,
1925 // which is not done because of the multiple inheritance implications.
Jeff Haoc642ec82013-09-04 16:11:55 -07001926 // Also don't change the type if it would result in an upcast.
1927 const RegType& orig_type = work_line_->GetRegisterType(instance_of_inst->VRegB_22c());
Ian Rogers9b360392013-06-06 14:45:07 -07001928 const RegType& cast_type = ResolveClassAndCheckAccess(instance_of_inst->VRegC_22c());
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001929
Jeff Haoa3faaf42013-09-03 19:07:00 -07001930 if (!cast_type.IsUnresolvedTypes() && !orig_type.IsUnresolvedTypes() &&
1931 !cast_type.GetClass()->IsInterface() && !cast_type.IsAssignableFrom(orig_type)) {
Ian Rogersd0fbd852013-09-24 18:17:04 -07001932 RegisterLine* update_line = RegisterLine::Create(code_item_->registers_size_, this);
Ian Rogersfae370a2013-06-05 08:33:27 -07001933 if (inst->Opcode() == Instruction::IF_EQZ) {
Ian Rogers9b360392013-06-06 14:45:07 -07001934 fallthrough_line.reset(update_line);
Ian Rogersfae370a2013-06-05 08:33:27 -07001935 } else {
Ian Rogers9b360392013-06-06 14:45:07 -07001936 branch_line.reset(update_line);
1937 }
1938 update_line->CopyFromLine(work_line_.get());
1939 update_line->SetRegisterType(instance_of_inst->VRegB_22c(), cast_type);
1940 if (!insn_flags_[instance_of_idx].IsBranchTarget() && 0 != instance_of_idx) {
1941 // See if instance-of was preceded by a move-object operation, common due to the small
1942 // register encoding space of instance-of, and propagate type information to the source
1943 // of the move-object.
1944 uint32_t move_idx = instance_of_idx - 1;
Brian Carlstromdf629502013-07-17 22:39:56 -07001945 while (0 != move_idx && !insn_flags_[move_idx].IsOpcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07001946 move_idx--;
1947 }
1948 CHECK(insn_flags_[move_idx].IsOpcode());
1949 const Instruction* move_inst = Instruction::At(code_item_->insns_ + move_idx);
1950 switch (move_inst->Opcode()) {
1951 case Instruction::MOVE_OBJECT:
1952 if (move_inst->VRegA_12x() == instance_of_inst->VRegB_22c()) {
1953 update_line->SetRegisterType(move_inst->VRegB_12x(), cast_type);
1954 }
1955 break;
1956 case Instruction::MOVE_OBJECT_FROM16:
1957 if (move_inst->VRegA_22x() == instance_of_inst->VRegB_22c()) {
1958 update_line->SetRegisterType(move_inst->VRegB_22x(), cast_type);
1959 }
1960 break;
1961 case Instruction::MOVE_OBJECT_16:
1962 if (move_inst->VRegA_32x() == instance_of_inst->VRegB_22c()) {
1963 update_line->SetRegisterType(move_inst->VRegB_32x(), cast_type);
1964 }
1965 break;
1966 default:
1967 break;
1968 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001969 }
1970 }
1971 }
1972
jeffhaobdb76512011-09-07 11:43:16 -07001973 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001974 }
jeffhaobdb76512011-09-07 11:43:16 -07001975 case Instruction::IF_LTZ:
1976 case Instruction::IF_GEZ:
1977 case Instruction::IF_GTZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07001978 case Instruction::IF_LEZ: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001979 const RegType& reg_type = work_line_->GetRegisterType(inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07001980 if (!reg_type.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001981 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
1982 << " unexpected as arg to if-ltz/if-gez/if-gtz/if-lez";
Ian Rogersd81871c2011-10-03 13:57:23 -07001983 }
jeffhaobdb76512011-09-07 11:43:16 -07001984 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001985 }
jeffhaobdb76512011-09-07 11:43:16 -07001986 case Instruction::AGET_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001987 VerifyAGet(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001988 break;
jeffhaobdb76512011-09-07 11:43:16 -07001989 case Instruction::AGET_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001990 VerifyAGet(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001991 break;
jeffhaobdb76512011-09-07 11:43:16 -07001992 case Instruction::AGET_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001993 VerifyAGet(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001994 break;
jeffhaobdb76512011-09-07 11:43:16 -07001995 case Instruction::AGET_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001996 VerifyAGet(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001997 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001998 case Instruction::AGET:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001999 VerifyAGet(inst, reg_types_.Integer(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002000 break;
jeffhaobdb76512011-09-07 11:43:16 -07002001 case Instruction::AGET_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002002 VerifyAGet(inst, reg_types_.LongLo(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002003 break;
2004 case Instruction::AGET_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002005 VerifyAGet(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07002006 break;
2007
Ian Rogersd81871c2011-10-03 13:57:23 -07002008 case Instruction::APUT_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002009 VerifyAPut(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002010 break;
2011 case Instruction::APUT_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002012 VerifyAPut(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002013 break;
2014 case Instruction::APUT_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002015 VerifyAPut(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002016 break;
2017 case Instruction::APUT_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002018 VerifyAPut(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002019 break;
2020 case Instruction::APUT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002021 VerifyAPut(inst, reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002022 break;
2023 case Instruction::APUT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002024 VerifyAPut(inst, reg_types_.LongLo(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002025 break;
2026 case Instruction::APUT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002027 VerifyAPut(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07002028 break;
2029
jeffhaobdb76512011-09-07 11:43:16 -07002030 case Instruction::IGET_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002031 VerifyISGet(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002032 break;
jeffhaobdb76512011-09-07 11:43:16 -07002033 case Instruction::IGET_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002034 VerifyISGet(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002035 break;
jeffhaobdb76512011-09-07 11:43:16 -07002036 case Instruction::IGET_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002037 VerifyISGet(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002038 break;
jeffhaobdb76512011-09-07 11:43:16 -07002039 case Instruction::IGET_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002040 VerifyISGet(inst, reg_types_.Short(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002041 break;
2042 case Instruction::IGET:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002043 VerifyISGet(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002044 break;
2045 case Instruction::IGET_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002046 VerifyISGet(inst, reg_types_.LongLo(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002047 break;
2048 case Instruction::IGET_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002049 VerifyISGet(inst, reg_types_.JavaLangObject(false), false, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002050 break;
jeffhaobdb76512011-09-07 11:43:16 -07002051
Ian Rogersd81871c2011-10-03 13:57:23 -07002052 case Instruction::IPUT_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002053 VerifyISPut(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002054 break;
2055 case Instruction::IPUT_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002056 VerifyISPut(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002057 break;
2058 case Instruction::IPUT_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002059 VerifyISPut(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002060 break;
2061 case Instruction::IPUT_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002062 VerifyISPut(inst, reg_types_.Short(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002063 break;
2064 case Instruction::IPUT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002065 VerifyISPut(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002066 break;
2067 case Instruction::IPUT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002068 VerifyISPut(inst, reg_types_.LongLo(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002069 break;
jeffhaobdb76512011-09-07 11:43:16 -07002070 case Instruction::IPUT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002071 VerifyISPut(inst, reg_types_.JavaLangObject(false), false, false);
jeffhaobdb76512011-09-07 11:43:16 -07002072 break;
2073
jeffhaobdb76512011-09-07 11:43:16 -07002074 case Instruction::SGET_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002075 VerifyISGet(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002076 break;
jeffhaobdb76512011-09-07 11:43:16 -07002077 case Instruction::SGET_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002078 VerifyISGet(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002079 break;
jeffhaobdb76512011-09-07 11:43:16 -07002080 case Instruction::SGET_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002081 VerifyISGet(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002082 break;
jeffhaobdb76512011-09-07 11:43:16 -07002083 case Instruction::SGET_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002084 VerifyISGet(inst, reg_types_.Short(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002085 break;
2086 case Instruction::SGET:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002087 VerifyISGet(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002088 break;
2089 case Instruction::SGET_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002090 VerifyISGet(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002091 break;
2092 case Instruction::SGET_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002093 VerifyISGet(inst, reg_types_.JavaLangObject(false), false, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002094 break;
2095
2096 case Instruction::SPUT_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002097 VerifyISPut(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002098 break;
2099 case Instruction::SPUT_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002100 VerifyISPut(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002101 break;
2102 case Instruction::SPUT_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002103 VerifyISPut(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002104 break;
2105 case Instruction::SPUT_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002106 VerifyISPut(inst, reg_types_.Short(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002107 break;
2108 case Instruction::SPUT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002109 VerifyISPut(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002110 break;
2111 case Instruction::SPUT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002112 VerifyISPut(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002113 break;
2114 case Instruction::SPUT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002115 VerifyISPut(inst, reg_types_.JavaLangObject(false), false, true);
jeffhaobdb76512011-09-07 11:43:16 -07002116 break;
2117
2118 case Instruction::INVOKE_VIRTUAL:
2119 case Instruction::INVOKE_VIRTUAL_RANGE:
2120 case Instruction::INVOKE_SUPER:
Ian Rogersd81871c2011-10-03 13:57:23 -07002121 case Instruction::INVOKE_SUPER_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002122 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE ||
2123 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
2124 bool is_super = (inst->Opcode() == Instruction::INVOKE_SUPER ||
2125 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
Brian Carlstromea46f952013-07-30 01:26:50 -07002126 mirror::ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_VIRTUAL,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002127 is_range, is_super);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002128 const RegType* return_type = nullptr;
2129 if (called_method != nullptr) {
2130 MethodHelper mh(called_method);
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08002131 mirror::Class* return_type_class = mh.GetReturnType(can_load_classes_);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002132 if (return_type_class != nullptr) {
2133 return_type = &reg_types_.FromClass(mh.GetReturnTypeDescriptor(), return_type_class,
2134 return_type_class->CannotBeAssignedFromOtherTypes());
2135 } else {
2136 Thread* self = Thread::Current();
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08002137 DCHECK(!can_load_classes_ || self->IsExceptionPending());
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002138 self->ClearException();
2139 }
2140 }
2141 if (return_type == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002142 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002143 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2144 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002145 const char* descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002146 return_type = &reg_types_.FromDescriptor(class_loader_->get(), descriptor, false);
jeffhaobdb76512011-09-07 11:43:16 -07002147 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002148 if (!return_type->IsLowHalf()) {
2149 work_line_->SetResultRegisterType(*return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002150 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002151 work_line_->SetResultRegisterTypeWide(*return_type, return_type->HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002152 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002153 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002154 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002155 }
jeffhaobdb76512011-09-07 11:43:16 -07002156 case Instruction::INVOKE_DIRECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002157 case Instruction::INVOKE_DIRECT_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002158 bool is_range = (inst->Opcode() == Instruction::INVOKE_DIRECT_RANGE);
Brian Carlstromea46f952013-07-30 01:26:50 -07002159 mirror::ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_DIRECT,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002160 is_range, false);
Ian Rogers46685432012-06-03 22:26:43 -07002161 const char* return_type_descriptor;
2162 bool is_constructor;
2163 if (called_method == NULL) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002164 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogers46685432012-06-03 22:26:43 -07002165 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
Ian Rogersdfb325e2013-10-30 01:00:44 -07002166 is_constructor = strcmp("<init>", dex_file_->StringDataByIdx(method_id.name_idx_)) == 0;
Ian Rogers46685432012-06-03 22:26:43 -07002167 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
2168 return_type_descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2169 } else {
2170 is_constructor = called_method->IsConstructor();
2171 return_type_descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
2172 }
2173 if (is_constructor) {
jeffhaobdb76512011-09-07 11:43:16 -07002174 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002175 * Some additional checks when calling a constructor. We know from the invocation arg check
2176 * that the "this" argument is an instance of called_method->klass. Now we further restrict
2177 * that to require that called_method->klass is the same as this->klass or this->super,
2178 * allowing the latter only if the "this" argument is the same as the "this" argument to
2179 * this method (which implies that we're in a constructor ourselves).
jeffhaobdb76512011-09-07 11:43:16 -07002180 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002181 const RegType& this_type = work_line_->GetInvocationThis(inst, is_range);
jeffhaob57e9522012-04-26 18:08:21 -07002182 if (this_type.IsConflict()) // failure.
2183 break;
jeffhaobdb76512011-09-07 11:43:16 -07002184
jeffhaob57e9522012-04-26 18:08:21 -07002185 /* no null refs allowed (?) */
2186 if (this_type.IsZero()) {
2187 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unable to initialize null ref";
2188 break;
jeffhao2a8a90e2011-09-26 14:25:31 -07002189 }
jeffhaob57e9522012-04-26 18:08:21 -07002190
2191 /* must be in same class or in superclass */
Ian Rogers46685432012-06-03 22:26:43 -07002192 // const RegType& this_super_klass = this_type.GetSuperClass(&reg_types_);
2193 // TODO: re-enable constructor type verification
2194 // if (this_super_klass.IsConflict()) {
jeffhaob57e9522012-04-26 18:08:21 -07002195 // Unknown super class, fail so we re-check at runtime.
Ian Rogers46685432012-06-03 22:26:43 -07002196 // Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "super class unknown for '" << this_type << "'";
2197 // break;
2198 // }
jeffhaob57e9522012-04-26 18:08:21 -07002199
2200 /* arg must be an uninitialized reference */
2201 if (!this_type.IsUninitializedTypes()) {
2202 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Expected initialization on uninitialized reference "
2203 << this_type;
2204 break;
2205 }
2206
2207 /*
2208 * Replace the uninitialized reference with an initialized one. We need to do this for all
2209 * registers that have the same object instance in them, not just the "this" register.
2210 */
2211 work_line_->MarkRefsAsInitialized(this_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002212 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07002213 const RegType& return_type = reg_types_.FromDescriptor(class_loader_->get(),
2214 return_type_descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002215 if (!return_type.IsLowHalf()) {
2216 work_line_->SetResultRegisterType(return_type);
2217 } else {
2218 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2219 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002220 just_set_result = true;
2221 break;
2222 }
2223 case Instruction::INVOKE_STATIC:
2224 case Instruction::INVOKE_STATIC_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002225 bool is_range = (inst->Opcode() == Instruction::INVOKE_STATIC_RANGE);
Brian Carlstromea46f952013-07-30 01:26:50 -07002226 mirror::ArtMethod* called_method = VerifyInvocationArgs(inst,
Brian Carlstrom93c33962013-07-26 10:37:43 -07002227 METHOD_STATIC,
2228 is_range,
2229 false);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002230 const char* descriptor;
2231 if (called_method == NULL) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002232 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002233 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2234 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Mathieu Chartier590fee92013-09-13 13:46:47 -07002235 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002236 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002237 descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002238 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07002239 const RegType& return_type = reg_types_.FromDescriptor(class_loader_->get(), descriptor,
2240 false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002241 if (!return_type.IsLowHalf()) {
2242 work_line_->SetResultRegisterType(return_type);
2243 } else {
2244 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2245 }
jeffhaobdb76512011-09-07 11:43:16 -07002246 just_set_result = true;
2247 }
2248 break;
jeffhaobdb76512011-09-07 11:43:16 -07002249 case Instruction::INVOKE_INTERFACE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002250 case Instruction::INVOKE_INTERFACE_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002251 bool is_range = (inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE);
Brian Carlstromea46f952013-07-30 01:26:50 -07002252 mirror::ArtMethod* abs_method = VerifyInvocationArgs(inst,
Brian Carlstrom93c33962013-07-26 10:37:43 -07002253 METHOD_INTERFACE,
2254 is_range,
2255 false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002256 if (abs_method != NULL) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002257 mirror::Class* called_interface = abs_method->GetDeclaringClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002258 if (!called_interface->IsInterface() && !called_interface->IsObjectClass()) {
2259 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected interface class in invoke-interface '"
2260 << PrettyMethod(abs_method) << "'";
2261 break;
Ian Rogers28ad40d2011-10-27 15:19:26 -07002262 }
Ian Rogers0d604842012-04-16 14:50:24 -07002263 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002264 /* Get the type of the "this" arg, which should either be a sub-interface of called
2265 * interface or Object (see comments in RegType::JoinClass).
2266 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002267 const RegType& this_type = work_line_->GetInvocationThis(inst, is_range);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002268 if (this_type.IsZero()) {
2269 /* null pointer always passes (and always fails at runtime) */
2270 } else {
2271 if (this_type.IsUninitializedTypes()) {
2272 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface call on uninitialized object "
2273 << this_type;
2274 break;
2275 }
2276 // In the past we have tried to assert that "called_interface" is assignable
2277 // from "this_type.GetClass()", however, as we do an imprecise Join
2278 // (RegType::JoinClass) we don't have full information on what interfaces are
2279 // implemented by "this_type". For example, two classes may implement the same
2280 // interfaces and have a common parent that doesn't implement the interface. The
2281 // join will set "this_type" to the parent class and a test that this implements
2282 // the interface will incorrectly fail.
2283 }
2284 /*
2285 * We don't have an object instance, so we can't find the concrete method. However, all of
2286 * the type information is in the abstract method, so we're good.
2287 */
2288 const char* descriptor;
2289 if (abs_method == NULL) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002290 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002291 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2292 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
2293 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2294 } else {
2295 descriptor = MethodHelper(abs_method).GetReturnTypeDescriptor();
2296 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07002297 const RegType& return_type = reg_types_.FromDescriptor(class_loader_->get(), descriptor,
2298 false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002299 if (!return_type.IsLowHalf()) {
2300 work_line_->SetResultRegisterType(return_type);
2301 } else {
2302 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2303 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002304 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002305 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002306 }
jeffhaobdb76512011-09-07 11:43:16 -07002307 case Instruction::NEG_INT:
2308 case Instruction::NOT_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002309 work_line_->CheckUnaryOp(inst, reg_types_.Integer(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002310 break;
2311 case Instruction::NEG_LONG:
2312 case Instruction::NOT_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002313 work_line_->CheckUnaryOpWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002314 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002315 break;
2316 case Instruction::NEG_FLOAT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002317 work_line_->CheckUnaryOp(inst, reg_types_.Float(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002318 break;
2319 case Instruction::NEG_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002320 work_line_->CheckUnaryOpWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002321 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002322 break;
2323 case Instruction::INT_TO_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002324 work_line_->CheckUnaryOpToWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002325 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002326 break;
2327 case Instruction::INT_TO_FLOAT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002328 work_line_->CheckUnaryOp(inst, reg_types_.Float(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002329 break;
2330 case Instruction::INT_TO_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002331 work_line_->CheckUnaryOpToWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002332 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002333 break;
2334 case Instruction::LONG_TO_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002335 work_line_->CheckUnaryOpFromWide(inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002336 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002337 break;
2338 case Instruction::LONG_TO_FLOAT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002339 work_line_->CheckUnaryOpFromWide(inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002340 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002341 break;
2342 case Instruction::LONG_TO_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002343 work_line_->CheckUnaryOpWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002344 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002345 break;
2346 case Instruction::FLOAT_TO_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002347 work_line_->CheckUnaryOp(inst, reg_types_.Integer(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002348 break;
2349 case Instruction::FLOAT_TO_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002350 work_line_->CheckUnaryOpToWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002351 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002352 break;
2353 case Instruction::FLOAT_TO_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002354 work_line_->CheckUnaryOpToWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002355 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002356 break;
2357 case Instruction::DOUBLE_TO_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002358 work_line_->CheckUnaryOpFromWide(inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002359 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002360 break;
2361 case Instruction::DOUBLE_TO_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002362 work_line_->CheckUnaryOpWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002363 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002364 break;
2365 case Instruction::DOUBLE_TO_FLOAT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002366 work_line_->CheckUnaryOpFromWide(inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002367 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002368 break;
2369 case Instruction::INT_TO_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002370 work_line_->CheckUnaryOp(inst, reg_types_.Byte(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002371 break;
2372 case Instruction::INT_TO_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002373 work_line_->CheckUnaryOp(inst, reg_types_.Char(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002374 break;
2375 case Instruction::INT_TO_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002376 work_line_->CheckUnaryOp(inst, reg_types_.Short(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002377 break;
2378
2379 case Instruction::ADD_INT:
2380 case Instruction::SUB_INT:
2381 case Instruction::MUL_INT:
2382 case Instruction::REM_INT:
2383 case Instruction::DIV_INT:
2384 case Instruction::SHL_INT:
2385 case Instruction::SHR_INT:
2386 case Instruction::USHR_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002387 work_line_->CheckBinaryOp(inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002388 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002389 break;
2390 case Instruction::AND_INT:
2391 case Instruction::OR_INT:
2392 case Instruction::XOR_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002393 work_line_->CheckBinaryOp(inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002394 reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002395 break;
2396 case Instruction::ADD_LONG:
2397 case Instruction::SUB_LONG:
2398 case Instruction::MUL_LONG:
2399 case Instruction::DIV_LONG:
2400 case Instruction::REM_LONG:
2401 case Instruction::AND_LONG:
2402 case Instruction::OR_LONG:
2403 case Instruction::XOR_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002404 work_line_->CheckBinaryOpWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002405 reg_types_.LongLo(), reg_types_.LongHi(),
2406 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002407 break;
2408 case Instruction::SHL_LONG:
2409 case Instruction::SHR_LONG:
2410 case Instruction::USHR_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002411 /* shift distance is Int, making these different from other binary operations */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002412 work_line_->CheckBinaryOpWideShift(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002413 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002414 break;
2415 case Instruction::ADD_FLOAT:
2416 case Instruction::SUB_FLOAT:
2417 case Instruction::MUL_FLOAT:
2418 case Instruction::DIV_FLOAT:
2419 case Instruction::REM_FLOAT:
Brian Carlstrom93c33962013-07-26 10:37:43 -07002420 work_line_->CheckBinaryOp(inst,
2421 reg_types_.Float(),
2422 reg_types_.Float(),
2423 reg_types_.Float(),
2424 false);
jeffhaobdb76512011-09-07 11:43:16 -07002425 break;
2426 case Instruction::ADD_DOUBLE:
2427 case Instruction::SUB_DOUBLE:
2428 case Instruction::MUL_DOUBLE:
2429 case Instruction::DIV_DOUBLE:
2430 case Instruction::REM_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002431 work_line_->CheckBinaryOpWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002432 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2433 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002434 break;
2435 case Instruction::ADD_INT_2ADDR:
2436 case Instruction::SUB_INT_2ADDR:
2437 case Instruction::MUL_INT_2ADDR:
2438 case Instruction::REM_INT_2ADDR:
2439 case Instruction::SHL_INT_2ADDR:
2440 case Instruction::SHR_INT_2ADDR:
2441 case Instruction::USHR_INT_2ADDR:
Brian Carlstrom93c33962013-07-26 10:37:43 -07002442 work_line_->CheckBinaryOp2addr(inst,
2443 reg_types_.Integer(),
2444 reg_types_.Integer(),
2445 reg_types_.Integer(),
2446 false);
jeffhaobdb76512011-09-07 11:43:16 -07002447 break;
2448 case Instruction::AND_INT_2ADDR:
2449 case Instruction::OR_INT_2ADDR:
2450 case Instruction::XOR_INT_2ADDR:
Brian Carlstrom93c33962013-07-26 10:37:43 -07002451 work_line_->CheckBinaryOp2addr(inst,
2452 reg_types_.Integer(),
2453 reg_types_.Integer(),
2454 reg_types_.Integer(),
2455 true);
jeffhaobdb76512011-09-07 11:43:16 -07002456 break;
2457 case Instruction::DIV_INT_2ADDR:
Brian Carlstrom93c33962013-07-26 10:37:43 -07002458 work_line_->CheckBinaryOp2addr(inst,
2459 reg_types_.Integer(),
2460 reg_types_.Integer(),
2461 reg_types_.Integer(),
2462 false);
jeffhaobdb76512011-09-07 11:43:16 -07002463 break;
2464 case Instruction::ADD_LONG_2ADDR:
2465 case Instruction::SUB_LONG_2ADDR:
2466 case Instruction::MUL_LONG_2ADDR:
2467 case Instruction::DIV_LONG_2ADDR:
2468 case Instruction::REM_LONG_2ADDR:
2469 case Instruction::AND_LONG_2ADDR:
2470 case Instruction::OR_LONG_2ADDR:
2471 case Instruction::XOR_LONG_2ADDR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002472 work_line_->CheckBinaryOp2addrWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002473 reg_types_.LongLo(), reg_types_.LongHi(),
2474 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002475 break;
2476 case Instruction::SHL_LONG_2ADDR:
2477 case Instruction::SHR_LONG_2ADDR:
2478 case Instruction::USHR_LONG_2ADDR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002479 work_line_->CheckBinaryOp2addrWideShift(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002480 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002481 break;
2482 case Instruction::ADD_FLOAT_2ADDR:
2483 case Instruction::SUB_FLOAT_2ADDR:
2484 case Instruction::MUL_FLOAT_2ADDR:
2485 case Instruction::DIV_FLOAT_2ADDR:
2486 case Instruction::REM_FLOAT_2ADDR:
Brian Carlstrom93c33962013-07-26 10:37:43 -07002487 work_line_->CheckBinaryOp2addr(inst,
2488 reg_types_.Float(),
2489 reg_types_.Float(),
2490 reg_types_.Float(),
2491 false);
jeffhaobdb76512011-09-07 11:43:16 -07002492 break;
2493 case Instruction::ADD_DOUBLE_2ADDR:
2494 case Instruction::SUB_DOUBLE_2ADDR:
2495 case Instruction::MUL_DOUBLE_2ADDR:
2496 case Instruction::DIV_DOUBLE_2ADDR:
2497 case Instruction::REM_DOUBLE_2ADDR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002498 work_line_->CheckBinaryOp2addrWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002499 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2500 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002501 break;
2502 case Instruction::ADD_INT_LIT16:
2503 case Instruction::RSUB_INT:
2504 case Instruction::MUL_INT_LIT16:
2505 case Instruction::DIV_INT_LIT16:
2506 case Instruction::REM_INT_LIT16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002507 work_line_->CheckLiteralOp(inst, reg_types_.Integer(), reg_types_.Integer(), false, true);
jeffhaobdb76512011-09-07 11:43:16 -07002508 break;
2509 case Instruction::AND_INT_LIT16:
2510 case Instruction::OR_INT_LIT16:
2511 case Instruction::XOR_INT_LIT16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002512 work_line_->CheckLiteralOp(inst, reg_types_.Integer(), reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002513 break;
2514 case Instruction::ADD_INT_LIT8:
2515 case Instruction::RSUB_INT_LIT8:
2516 case Instruction::MUL_INT_LIT8:
2517 case Instruction::DIV_INT_LIT8:
2518 case Instruction::REM_INT_LIT8:
2519 case Instruction::SHL_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002520 case Instruction::SHR_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002521 case Instruction::USHR_INT_LIT8:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002522 work_line_->CheckLiteralOp(inst, reg_types_.Integer(), reg_types_.Integer(), false, false);
jeffhaobdb76512011-09-07 11:43:16 -07002523 break;
2524 case Instruction::AND_INT_LIT8:
2525 case Instruction::OR_INT_LIT8:
2526 case Instruction::XOR_INT_LIT8:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002527 work_line_->CheckLiteralOp(inst, reg_types_.Integer(), reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002528 break;
2529
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002530 // Special instructions.
Sebastien Hertzcc10e0e2013-06-28 14:24:48 +02002531 case Instruction::RETURN_VOID_BARRIER:
Ian Rogers9fc16eb2013-07-31 14:49:16 -07002532 if (!IsConstructor() || IsStatic()) {
Sebastien Hertzcc10e0e2013-06-28 14:24:48 +02002533 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void-barrier not expected";
2534 }
2535 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002536 // Note: the following instructions encode offsets derived from class linking.
2537 // As such they use Class*/Field*/AbstractMethod* as these offsets only have
2538 // meaning if the class linking and resolution were successful.
2539 case Instruction::IGET_QUICK:
2540 VerifyIGetQuick(inst, reg_types_.Integer(), true);
2541 break;
2542 case Instruction::IGET_WIDE_QUICK:
2543 VerifyIGetQuick(inst, reg_types_.LongLo(), true);
2544 break;
2545 case Instruction::IGET_OBJECT_QUICK:
2546 VerifyIGetQuick(inst, reg_types_.JavaLangObject(false), false);
2547 break;
2548 case Instruction::IPUT_QUICK:
2549 VerifyIPutQuick(inst, reg_types_.Integer(), true);
2550 break;
2551 case Instruction::IPUT_WIDE_QUICK:
2552 VerifyIPutQuick(inst, reg_types_.LongLo(), true);
2553 break;
2554 case Instruction::IPUT_OBJECT_QUICK:
2555 VerifyIPutQuick(inst, reg_types_.JavaLangObject(false), false);
2556 break;
2557 case Instruction::INVOKE_VIRTUAL_QUICK:
2558 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: {
2559 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
Brian Carlstromea46f952013-07-30 01:26:50 -07002560 mirror::ArtMethod* called_method = VerifyInvokeVirtualQuickArgs(inst, is_range);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002561 if (called_method != NULL) {
2562 const char* descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
Mathieu Chartier590fee92013-09-13 13:46:47 -07002563 const RegType& return_type = reg_types_.FromDescriptor(class_loader_->get(), descriptor,
2564 false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002565 if (!return_type.IsLowHalf()) {
2566 work_line_->SetResultRegisterType(return_type);
2567 } else {
2568 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2569 }
2570 just_set_result = true;
2571 }
2572 break;
2573 }
2574
Ian Rogersd81871c2011-10-03 13:57:23 -07002575 /* These should never appear during verification. */
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002576 case Instruction::UNUSED_3E:
2577 case Instruction::UNUSED_3F:
2578 case Instruction::UNUSED_40:
2579 case Instruction::UNUSED_41:
2580 case Instruction::UNUSED_42:
2581 case Instruction::UNUSED_43:
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002582 case Instruction::UNUSED_79:
2583 case Instruction::UNUSED_7A:
2584 case Instruction::UNUSED_EB:
2585 case Instruction::UNUSED_EC:
jeffhao9a4f0032012-08-30 16:17:40 -07002586 case Instruction::UNUSED_ED:
jeffhaobdb76512011-09-07 11:43:16 -07002587 case Instruction::UNUSED_EE:
2588 case Instruction::UNUSED_EF:
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002589 case Instruction::UNUSED_F0:
2590 case Instruction::UNUSED_F1:
jeffhaobdb76512011-09-07 11:43:16 -07002591 case Instruction::UNUSED_F2:
2592 case Instruction::UNUSED_F3:
2593 case Instruction::UNUSED_F4:
2594 case Instruction::UNUSED_F5:
2595 case Instruction::UNUSED_F6:
2596 case Instruction::UNUSED_F7:
2597 case Instruction::UNUSED_F8:
2598 case Instruction::UNUSED_F9:
2599 case Instruction::UNUSED_FA:
2600 case Instruction::UNUSED_FB:
jeffhaobdb76512011-09-07 11:43:16 -07002601 case Instruction::UNUSED_FC:
jeffhaobdb76512011-09-07 11:43:16 -07002602 case Instruction::UNUSED_FD:
jeffhaobdb76512011-09-07 11:43:16 -07002603 case Instruction::UNUSED_FE:
jeffhaobdb76512011-09-07 11:43:16 -07002604 case Instruction::UNUSED_FF:
jeffhaod5347e02012-03-22 17:25:05 -07002605 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Unexpected opcode " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07002606 break;
2607
2608 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002609 * DO NOT add a "default" clause here. Without it the compiler will
jeffhaobdb76512011-09-07 11:43:16 -07002610 * complain if an instruction is missing (which is desirable).
2611 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002612 } // end - switch (dec_insn.opcode)
jeffhaobdb76512011-09-07 11:43:16 -07002613
Ian Rogersad0b3a32012-04-16 14:50:24 -07002614 if (have_pending_hard_failure_) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002615 if (Runtime::Current()->IsCompiler()) {
jeffhaob57e9522012-04-26 18:08:21 -07002616 /* When compiling, check that the last failure is a hard failure */
Ian Rogersad0b3a32012-04-16 14:50:24 -07002617 CHECK_EQ(failures_[failures_.size() - 1], VERIFY_ERROR_BAD_CLASS_HARD);
Ian Rogerse1758fe2012-04-19 11:31:15 -07002618 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002619 /* immediate failure, reject class */
2620 info_messages_ << "Rejecting opcode " << inst->DumpString(dex_file_);
2621 return false;
jeffhaofaf459e2012-08-31 15:32:47 -07002622 } else if (have_pending_runtime_throw_failure_) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07002623 /* checking interpreter will throw, mark following code as unreachable */
jeffhaofaf459e2012-08-31 15:32:47 -07002624 opcode_flags = Instruction::kThrow;
jeffhaobdb76512011-09-07 11:43:16 -07002625 }
jeffhaobdb76512011-09-07 11:43:16 -07002626 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002627 * If we didn't just set the result register, clear it out. This ensures that you can only use
2628 * "move-result" immediately after the result is set. (We could check this statically, but it's
2629 * not expensive and it makes our debugging output cleaner.)
jeffhaobdb76512011-09-07 11:43:16 -07002630 */
2631 if (!just_set_result) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002632 work_line_->SetResultTypeToUnknown();
jeffhaobdb76512011-09-07 11:43:16 -07002633 }
2634
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002635
jeffhaobdb76512011-09-07 11:43:16 -07002636
2637 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002638 * Handle "branch". Tag the branch target.
jeffhaobdb76512011-09-07 11:43:16 -07002639 *
2640 * NOTE: instructions like Instruction::EQZ provide information about the
jeffhaod1f0fde2011-09-08 17:25:33 -07002641 * state of the register when the branch is taken or not taken. For example,
jeffhaobdb76512011-09-07 11:43:16 -07002642 * somebody could get a reference field, check it for zero, and if the
2643 * branch is taken immediately store that register in a boolean field
jeffhaod1f0fde2011-09-08 17:25:33 -07002644 * since the value is known to be zero. We do not currently account for
jeffhaobdb76512011-09-07 11:43:16 -07002645 * that, and will reject the code.
2646 *
2647 * TODO: avoid re-fetching the branch target
2648 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002649 if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002650 bool isConditional, selfOkay;
Ian Rogersd81871c2011-10-03 13:57:23 -07002651 if (!GetBranchOffset(work_insn_idx_, &branch_target, &isConditional, &selfOkay)) {
jeffhaobdb76512011-09-07 11:43:16 -07002652 /* should never happen after static verification */
jeffhaod5347e02012-03-22 17:25:05 -07002653 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad branch";
jeffhaobdb76512011-09-07 11:43:16 -07002654 return false;
2655 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08002656 DCHECK_EQ(isConditional, (opcode_flags & Instruction::kContinue) != 0);
jeffhaod5347e02012-03-22 17:25:05 -07002657 if (!CheckNotMoveException(code_item_->insns_, work_insn_idx_ + branch_target)) {
jeffhaobdb76512011-09-07 11:43:16 -07002658 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002659 }
jeffhaobdb76512011-09-07 11:43:16 -07002660 /* update branch target, set "changed" if appropriate */
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002661 if (NULL != branch_line.get()) {
2662 if (!UpdateRegisters(work_insn_idx_ + branch_target, branch_line.get())) {
2663 return false;
2664 }
2665 } else {
2666 if (!UpdateRegisters(work_insn_idx_ + branch_target, work_line_.get())) {
2667 return false;
2668 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002669 }
jeffhaobdb76512011-09-07 11:43:16 -07002670 }
2671
2672 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002673 * Handle "switch". Tag all possible branch targets.
jeffhaobdb76512011-09-07 11:43:16 -07002674 *
2675 * We've already verified that the table is structurally sound, so we
2676 * just need to walk through and tag the targets.
2677 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002678 if ((opcode_flags & Instruction::kSwitch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002679 int offset_to_switch = insns[1] | (((int32_t) insns[2]) << 16);
2680 const uint16_t* switch_insns = insns + offset_to_switch;
2681 int switch_count = switch_insns[1];
2682 int offset_to_targets, targ;
2683
2684 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
2685 /* 0 = sig, 1 = count, 2/3 = first key */
2686 offset_to_targets = 4;
2687 } else {
2688 /* 0 = sig, 1 = count, 2..count * 2 = keys */
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07002689 DCHECK((*insns & 0xff) == Instruction::SPARSE_SWITCH);
jeffhaobdb76512011-09-07 11:43:16 -07002690 offset_to_targets = 2 + 2 * switch_count;
2691 }
2692
2693 /* verify each switch target */
2694 for (targ = 0; targ < switch_count; targ++) {
2695 int offset;
2696 uint32_t abs_offset;
2697
2698 /* offsets are 32-bit, and only partly endian-swapped */
2699 offset = switch_insns[offset_to_targets + targ * 2] |
2700 (((int32_t) switch_insns[offset_to_targets + targ * 2 + 1]) << 16);
Ian Rogersd81871c2011-10-03 13:57:23 -07002701 abs_offset = work_insn_idx_ + offset;
2702 DCHECK_LT(abs_offset, code_item_->insns_size_in_code_units_);
jeffhaod5347e02012-03-22 17:25:05 -07002703 if (!CheckNotMoveException(code_item_->insns_, abs_offset)) {
jeffhaobdb76512011-09-07 11:43:16 -07002704 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002705 }
2706 if (!UpdateRegisters(abs_offset, work_line_.get()))
jeffhaobdb76512011-09-07 11:43:16 -07002707 return false;
2708 }
2709 }
2710
2711 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002712 * Handle instructions that can throw and that are sitting in a "try" block. (If they're not in a
2713 * "try" block when they throw, control transfers out of the method.)
jeffhaobdb76512011-09-07 11:43:16 -07002714 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002715 if ((opcode_flags & Instruction::kThrow) != 0 && insn_flags_[work_insn_idx_].IsInTry()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002716 bool within_catch_all = false;
Ian Rogers0571d352011-11-03 19:51:38 -07002717 CatchHandlerIterator iterator(*code_item_, work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07002718
Ian Rogers0571d352011-11-03 19:51:38 -07002719 for (; iterator.HasNext(); iterator.Next()) {
2720 if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002721 within_catch_all = true;
2722 }
jeffhaobdb76512011-09-07 11:43:16 -07002723 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002724 * Merge registers into the "catch" block. We want to use the "savedRegs" rather than
2725 * "work_regs", because at runtime the exception will be thrown before the instruction
2726 * modifies any registers.
jeffhaobdb76512011-09-07 11:43:16 -07002727 */
Ian Rogers0571d352011-11-03 19:51:38 -07002728 if (!UpdateRegisters(iterator.GetHandlerAddress(), saved_line_.get())) {
jeffhaobdb76512011-09-07 11:43:16 -07002729 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002730 }
jeffhaobdb76512011-09-07 11:43:16 -07002731 }
2732
2733 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002734 * If the monitor stack depth is nonzero, there must be a "catch all" handler for this
2735 * instruction. This does apply to monitor-exit because of async exception handling.
jeffhaobdb76512011-09-07 11:43:16 -07002736 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002737 if (work_line_->MonitorStackDepth() > 0 && !within_catch_all) {
jeffhaobdb76512011-09-07 11:43:16 -07002738 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002739 * The state in work_line reflects the post-execution state. If the current instruction is a
2740 * monitor-enter and the monitor stack was empty, we don't need a catch-all (if it throws,
jeffhaobdb76512011-09-07 11:43:16 -07002741 * it will do so before grabbing the lock).
2742 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002743 if (inst->Opcode() != Instruction::MONITOR_ENTER || work_line_->MonitorStackDepth() != 1) {
jeffhaod5347e02012-03-22 17:25:05 -07002744 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
Ian Rogersd81871c2011-10-03 13:57:23 -07002745 << "expected to be within a catch-all for an instruction where a monitor is held";
jeffhaobdb76512011-09-07 11:43:16 -07002746 return false;
2747 }
2748 }
2749 }
2750
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002751 /* Handle "continue". Tag the next consecutive instruction.
2752 * Note: Keep the code handling "continue" case below the "branch" and "switch" cases,
2753 * because it changes work_line_ when performing peephole optimization
2754 * and this change should not be used in those cases.
2755 */
Ian Rogers6d376ae2013-07-23 15:12:40 -07002756 if ((opcode_flags & Instruction::kContinue) != 0) {
2757 uint32_t next_insn_idx = work_insn_idx_ + CurrentInsnFlags()->GetLengthInCodeUnits();
2758 if (next_insn_idx >= code_item_->insns_size_in_code_units_) {
2759 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Execution can walk off end of code area";
2760 return false;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002761 }
Ian Rogers6d376ae2013-07-23 15:12:40 -07002762 // The only way to get to a move-exception instruction is to get thrown there. Make sure the
2763 // next instruction isn't one.
2764 if (!CheckNotMoveException(code_item_->insns_, next_insn_idx)) {
2765 return false;
2766 }
2767 if (NULL != fallthrough_line.get()) {
2768 // Make workline consistent with fallthrough computed from peephole optimization.
2769 work_line_->CopyFromLine(fallthrough_line.get());
2770 }
Ian Rogersb8c78592013-07-25 23:52:52 +00002771 if (insn_flags_[next_insn_idx].IsReturn()) {
2772 // For returns we only care about the operand to the return, all other registers are dead.
2773 const Instruction* ret_inst = Instruction::At(code_item_->insns_ + next_insn_idx);
2774 Instruction::Code opcode = ret_inst->Opcode();
2775 if ((opcode == Instruction::RETURN_VOID) || (opcode == Instruction::RETURN_VOID_BARRIER)) {
2776 work_line_->MarkAllRegistersAsConflicts();
2777 } else {
2778 if (opcode == Instruction::RETURN_WIDE) {
2779 work_line_->MarkAllRegistersAsConflictsExceptWide(ret_inst->VRegA_11x());
2780 } else {
2781 work_line_->MarkAllRegistersAsConflictsExcept(ret_inst->VRegA_11x());
2782 }
2783 }
2784 }
Ian Rogers6d376ae2013-07-23 15:12:40 -07002785 RegisterLine* next_line = reg_table_.GetLine(next_insn_idx);
2786 if (next_line != NULL) {
2787 // Merge registers into what we have for the next instruction,
2788 // and set the "changed" flag if needed.
2789 if (!UpdateRegisters(next_insn_idx, work_line_.get())) {
2790 return false;
2791 }
2792 } else {
2793 /*
2794 * We're not recording register data for the next instruction, so we don't know what the
2795 * prior state was. We have to assume that something has changed and re-evaluate it.
2796 */
2797 insn_flags_[next_insn_idx].SetChanged();
2798 }
2799 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002800
jeffhaod1f0fde2011-09-08 17:25:33 -07002801 /* If we're returning from the method, make sure monitor stack is empty. */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002802 if ((opcode_flags & Instruction::kReturn) != 0) {
Elliott Hughesb25c3f62012-03-26 16:35:06 -07002803 if (!work_line_->VerifyMonitorStackEmpty()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002804 return false;
2805 }
jeffhaobdb76512011-09-07 11:43:16 -07002806 }
2807
2808 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002809 * Update start_guess. Advance to the next instruction of that's
2810 * possible, otherwise use the branch target if one was found. If
jeffhaobdb76512011-09-07 11:43:16 -07002811 * neither of those exists we're in a return or throw; leave start_guess
2812 * alone and let the caller sort it out.
2813 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002814 if ((opcode_flags & Instruction::kContinue) != 0) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002815 *start_guess = work_insn_idx_ + insn_flags_[work_insn_idx_].GetLengthInCodeUnits();
Elliott Hughesadb8c672012-03-06 16:49:32 -08002816 } else if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002817 /* we're still okay if branch_target is zero */
Ian Rogersd81871c2011-10-03 13:57:23 -07002818 *start_guess = work_insn_idx_ + branch_target;
jeffhaobdb76512011-09-07 11:43:16 -07002819 }
2820
Ian Rogersd81871c2011-10-03 13:57:23 -07002821 DCHECK_LT(*start_guess, code_item_->insns_size_in_code_units_);
2822 DCHECK(insn_flags_[*start_guess].IsOpcode());
jeffhaobdb76512011-09-07 11:43:16 -07002823
2824 return true;
Brian Carlstrom7934ac22013-07-26 10:54:15 -07002825} // NOLINT(readability/fn_size)
jeffhaobdb76512011-09-07 11:43:16 -07002826
Ian Rogers776ac1f2012-04-13 23:36:36 -07002827const RegType& MethodVerifier::ResolveClassAndCheckAccess(uint32_t class_idx) {
Ian Rogers0571d352011-11-03 19:51:38 -07002828 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002829 const RegType& referrer = GetDeclaringClass();
Mathieu Chartier590fee92013-09-13 13:46:47 -07002830 mirror::Class* klass = (*dex_cache_)->GetResolvedType(class_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002831 const RegType& result =
Ian Rogers04f94f42013-06-10 15:09:26 -07002832 klass != NULL ? reg_types_.FromClass(descriptor, klass,
2833 klass->CannotBeAssignedFromOtherTypes())
Mathieu Chartier590fee92013-09-13 13:46:47 -07002834 : reg_types_.FromDescriptor(class_loader_->get(), descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002835 if (result.IsConflict()) {
2836 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "accessing broken descriptor '" << descriptor
2837 << "' in " << referrer;
2838 return result;
2839 }
Ian Rogerse1758fe2012-04-19 11:31:15 -07002840 if (klass == NULL && !result.IsUnresolvedTypes()) {
Mathieu Chartier590fee92013-09-13 13:46:47 -07002841 (*dex_cache_)->SetResolvedType(class_idx, result.GetClass());
Ian Rogerse1758fe2012-04-19 11:31:15 -07002842 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002843 // Check if access is allowed. Unresolved types use xxxWithAccessCheck to
Jeff Haob24b4a72013-07-31 13:47:31 -07002844 // check at runtime if access is allowed and so pass here. If result is
2845 // primitive, skip the access check.
2846 if (result.IsNonZeroReferenceTypes() && !result.IsUnresolvedTypes() &&
2847 !referrer.IsUnresolvedTypes() && !referrer.CanAccess(result)) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002848 Fail(VERIFY_ERROR_ACCESS_CLASS) << "illegal class access: '"
Ian Rogersad0b3a32012-04-16 14:50:24 -07002849 << referrer << "' -> '" << result << "'";
Ian Rogers28ad40d2011-10-27 15:19:26 -07002850 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002851 return result;
Ian Rogersd81871c2011-10-03 13:57:23 -07002852}
2853
Ian Rogers776ac1f2012-04-13 23:36:36 -07002854const RegType& MethodVerifier::GetCaughtExceptionType() {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002855 const RegType* common_super = NULL;
Ian Rogersd81871c2011-10-03 13:57:23 -07002856 if (code_item_->tries_size_ != 0) {
Ian Rogers0571d352011-11-03 19:51:38 -07002857 const byte* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
Ian Rogersd81871c2011-10-03 13:57:23 -07002858 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
2859 for (uint32_t i = 0; i < handlers_size; i++) {
Ian Rogers0571d352011-11-03 19:51:38 -07002860 CatchHandlerIterator iterator(handlers_ptr);
2861 for (; iterator.HasNext(); iterator.Next()) {
2862 if (iterator.GetHandlerAddress() == (uint32_t) work_insn_idx_) {
2863 if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) {
Ian Rogersb4903572012-10-11 11:52:56 -07002864 common_super = &reg_types_.JavaLangThrowable(false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002865 } else {
Ian Rogers0571d352011-11-03 19:51:38 -07002866 const RegType& exception = ResolveClassAndCheckAccess(iterator.GetHandlerTypeIndex());
Ian Rogersc4762272012-02-01 15:55:55 -08002867 if (common_super == NULL) {
2868 // Unconditionally assign for the first handler. We don't assert this is a Throwable
2869 // as that is caught at runtime
2870 common_super = &exception;
Ian Rogersb4903572012-10-11 11:52:56 -07002871 } else if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(exception)) {
Jeff Haoc26a56c2013-11-04 12:00:47 -08002872 if (exception.IsUnresolvedTypes()) {
2873 // We don't know enough about the type. Fail here and let runtime handle it.
2874 Fail(VERIFY_ERROR_NO_CLASS) << "unresolved exception class " << exception;
2875 return exception;
2876 } else {
2877 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unexpected non-exception class " << exception;
2878 return reg_types_.Conflict();
2879 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002880 } else if (common_super->Equals(exception)) {
Ian Rogersc4762272012-02-01 15:55:55 -08002881 // odd case, but nothing to do
Ian Rogersd81871c2011-10-03 13:57:23 -07002882 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002883 common_super = &common_super->Merge(exception, &reg_types_);
Ian Rogersb4903572012-10-11 11:52:56 -07002884 CHECK(reg_types_.JavaLangThrowable(false).IsAssignableFrom(*common_super));
Ian Rogersd81871c2011-10-03 13:57:23 -07002885 }
2886 }
2887 }
2888 }
Ian Rogers0571d352011-11-03 19:51:38 -07002889 handlers_ptr = iterator.EndDataPointer();
Ian Rogersd81871c2011-10-03 13:57:23 -07002890 }
2891 }
2892 if (common_super == NULL) {
2893 /* no catch blocks, or no catches with classes we can find */
jeffhaod5347e02012-03-22 17:25:05 -07002894 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unable to find exception handler";
Ian Rogersad0b3a32012-04-16 14:50:24 -07002895 return reg_types_.Conflict();
Ian Rogersd81871c2011-10-03 13:57:23 -07002896 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002897 return *common_super;
Ian Rogersd81871c2011-10-03 13:57:23 -07002898}
2899
Brian Carlstromea46f952013-07-30 01:26:50 -07002900mirror::ArtMethod* MethodVerifier::ResolveMethodAndCheckAccess(uint32_t dex_method_idx,
Ian Rogersd91d6d62013-09-25 20:26:14 -07002901 MethodType method_type) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002902 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx);
Ian Rogers90040192011-12-16 08:54:29 -08002903 const RegType& klass_type = ResolveClassAndCheckAccess(method_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002904 if (klass_type.IsConflict()) {
2905 std::string append(" in attempt to access method ");
2906 append += dex_file_->GetMethodName(method_id);
2907 AppendToLastFailMessage(append);
Ian Rogers90040192011-12-16 08:54:29 -08002908 return NULL;
2909 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002910 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers90040192011-12-16 08:54:29 -08002911 return NULL; // Can't resolve Class so no more to do here
2912 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002913 mirror::Class* klass = klass_type.GetClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002914 const RegType& referrer = GetDeclaringClass();
Mathieu Chartier590fee92013-09-13 13:46:47 -07002915 mirror::ArtMethod* res_method = (*dex_cache_)->GetResolvedMethod(dex_method_idx);
Ian Rogersd81871c2011-10-03 13:57:23 -07002916 if (res_method == NULL) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07002917 const char* name = dex_file_->GetMethodName(method_id);
Ian Rogersd91d6d62013-09-25 20:26:14 -07002918 const Signature signature = dex_file_->GetMethodSignature(method_id);
jeffhao8cd6dda2012-02-22 10:15:34 -08002919
2920 if (method_type == METHOD_DIRECT || method_type == METHOD_STATIC) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002921 res_method = klass->FindDirectMethod(name, signature);
jeffhao8cd6dda2012-02-22 10:15:34 -08002922 } else if (method_type == METHOD_INTERFACE) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002923 res_method = klass->FindInterfaceMethod(name, signature);
2924 } else {
2925 res_method = klass->FindVirtualMethod(name, signature);
2926 }
2927 if (res_method != NULL) {
Mathieu Chartier590fee92013-09-13 13:46:47 -07002928 (*dex_cache_)->SetResolvedMethod(dex_method_idx, res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07002929 } else {
jeffhao8cd6dda2012-02-22 10:15:34 -08002930 // If a virtual or interface method wasn't found with the expected type, look in
2931 // the direct methods. This can happen when the wrong invoke type is used or when
2932 // a class has changed, and will be flagged as an error in later checks.
2933 if (method_type == METHOD_INTERFACE || method_type == METHOD_VIRTUAL) {
2934 res_method = klass->FindDirectMethod(name, signature);
2935 }
2936 if (res_method == NULL) {
2937 Fail(VERIFY_ERROR_NO_METHOD) << "couldn't find method "
2938 << PrettyDescriptor(klass) << "." << name
2939 << " " << signature;
2940 return NULL;
2941 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002942 }
2943 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002944 // Make sure calls to constructors are "direct". There are additional restrictions but we don't
2945 // enforce them here.
2946 if (res_method->IsConstructor() && method_type != METHOD_DIRECT) {
jeffhaod5347e02012-03-22 17:25:05 -07002947 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting non-direct call to constructor "
2948 << PrettyMethod(res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07002949 return NULL;
2950 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002951 // Disallow any calls to class initializers.
2952 if (MethodHelper(res_method).IsClassInitializer()) {
jeffhaod5347e02012-03-22 17:25:05 -07002953 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting call to class initializer "
2954 << PrettyMethod(res_method);
jeffhao8cd6dda2012-02-22 10:15:34 -08002955 return NULL;
2956 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002957 // Check if access is allowed.
Ian Rogersad0b3a32012-04-16 14:50:24 -07002958 if (!referrer.CanAccessMember(res_method->GetDeclaringClass(), res_method->GetAccessFlags())) {
jeffhao8cd6dda2012-02-22 10:15:34 -08002959 Fail(VERIFY_ERROR_ACCESS_METHOD) << "illegal method access (call " << PrettyMethod(res_method)
Ian Rogersad0b3a32012-04-16 14:50:24 -07002960 << " from " << referrer << ")";
jeffhaob57e9522012-04-26 18:08:21 -07002961 return res_method;
jeffhao8cd6dda2012-02-22 10:15:34 -08002962 }
jeffhaode0d9c92012-02-27 13:58:13 -08002963 // Check that invoke-virtual and invoke-super are not used on private methods of the same class.
2964 if (res_method->IsPrivate() && method_type == METHOD_VIRTUAL) {
jeffhaod5347e02012-03-22 17:25:05 -07002965 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invoke-super/virtual can't be used on private method "
2966 << PrettyMethod(res_method);
jeffhaode0d9c92012-02-27 13:58:13 -08002967 return NULL;
2968 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002969 // Check that interface methods match interface classes.
2970 if (klass->IsInterface() && method_type != METHOD_INTERFACE) {
2971 Fail(VERIFY_ERROR_CLASS_CHANGE) << "non-interface method " << PrettyMethod(res_method)
2972 << " is in an interface class " << PrettyClass(klass);
2973 return NULL;
2974 } else if (!klass->IsInterface() && method_type == METHOD_INTERFACE) {
2975 Fail(VERIFY_ERROR_CLASS_CHANGE) << "interface method " << PrettyMethod(res_method)
2976 << " is in a non-interface class " << PrettyClass(klass);
2977 return NULL;
2978 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002979 // See if the method type implied by the invoke instruction matches the access flags for the
2980 // target method.
2981 if ((method_type == METHOD_DIRECT && !res_method->IsDirect()) ||
2982 (method_type == METHOD_STATIC && !res_method->IsStatic()) ||
2983 ((method_type == METHOD_VIRTUAL || method_type == METHOD_INTERFACE) && res_method->IsDirect())
2984 ) {
Ian Rogers2fc14272012-08-30 10:56:57 -07002985 Fail(VERIFY_ERROR_CLASS_CHANGE) << "invoke type (" << method_type << ") does not match method "
2986 " type of " << PrettyMethod(res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07002987 return NULL;
2988 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002989 return res_method;
2990}
2991
Brian Carlstromea46f952013-07-30 01:26:50 -07002992mirror::ArtMethod* MethodVerifier::VerifyInvocationArgs(const Instruction* inst,
Sebastien Hertz5243e912013-05-21 10:55:07 +02002993 MethodType method_type,
2994 bool is_range,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002995 bool is_super) {
jeffhao8cd6dda2012-02-22 10:15:34 -08002996 // Resolve the method. This could be an abstract or concrete method depending on what sort of call
2997 // we're making.
Sebastien Hertz5243e912013-05-21 10:55:07 +02002998 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Brian Carlstromea46f952013-07-30 01:26:50 -07002999 mirror::ArtMethod* res_method = ResolveMethodAndCheckAccess(method_idx, method_type);
jeffhao8cd6dda2012-02-22 10:15:34 -08003000 if (res_method == NULL) { // error or class is unresolved
3001 return NULL;
3002 }
3003
Ian Rogersd81871c2011-10-03 13:57:23 -07003004 // If we're using invoke-super(method), make sure that the executing method's class' superclass
3005 // has a vtable entry for the target method.
3006 if (is_super) {
3007 DCHECK(method_type == METHOD_VIRTUAL);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003008 const RegType& super = GetDeclaringClass().GetSuperClass(&reg_types_);
Ian Rogers529781d2012-07-23 17:24:29 -07003009 if (super.IsUnresolvedTypes()) {
jeffhao4d8df822012-04-24 17:09:36 -07003010 Fail(VERIFY_ERROR_NO_METHOD) << "unknown super class in invoke-super from "
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003011 << PrettyMethod(dex_method_idx_, *dex_file_)
jeffhao4d8df822012-04-24 17:09:36 -07003012 << " to super " << PrettyMethod(res_method);
3013 return NULL;
3014 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003015 mirror::Class* super_klass = super.GetClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07003016 if (res_method->GetMethodIndex() >= super_klass->GetVTable()->GetLength()) {
jeffhao4d8df822012-04-24 17:09:36 -07003017 MethodHelper mh(res_method);
3018 Fail(VERIFY_ERROR_NO_METHOD) << "invalid invoke-super from "
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003019 << PrettyMethod(dex_method_idx_, *dex_file_)
jeffhao4d8df822012-04-24 17:09:36 -07003020 << " to super " << super
3021 << "." << mh.GetName()
3022 << mh.GetSignature();
Ian Rogersd81871c2011-10-03 13:57:23 -07003023 return NULL;
3024 }
3025 }
3026 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003027 // match the call to the signature. Also, we might be calling through an abstract method
Ian Rogersd81871c2011-10-03 13:57:23 -07003028 // definition (which doesn't have register count values).
Sebastien Hertz5243e912013-05-21 10:55:07 +02003029 const size_t expected_args = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
Ian Rogersd81871c2011-10-03 13:57:23 -07003030 /* caught by static verifier */
3031 DCHECK(is_range || expected_args <= 5);
3032 if (expected_args > code_item_->outs_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07003033 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
Ian Rogersd81871c2011-10-03 13:57:23 -07003034 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
3035 return NULL;
3036 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003037
jeffhaobdb76512011-09-07 11:43:16 -07003038 /*
Ian Rogersad0b3a32012-04-16 14:50:24 -07003039 * Check the "this" argument, which must be an instance of the class that declared the method.
3040 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
3041 * rigorous check here (which is okay since we have to do it at runtime).
jeffhaobdb76512011-09-07 11:43:16 -07003042 */
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003043 size_t actual_args = 0;
Ian Rogersd81871c2011-10-03 13:57:23 -07003044 if (!res_method->IsStatic()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003045 const RegType& actual_arg_type = work_line_->GetInvocationThis(inst, is_range);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003046 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
Ian Rogersd81871c2011-10-03 13:57:23 -07003047 return NULL;
3048 }
3049 if (actual_arg_type.IsUninitializedReference() && !res_method->IsConstructor()) {
jeffhaod5347e02012-03-22 17:25:05 -07003050 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
Ian Rogersd81871c2011-10-03 13:57:23 -07003051 return NULL;
3052 }
3053 if (method_type != METHOD_INTERFACE && !actual_arg_type.IsZero()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003054 mirror::Class* klass = res_method->GetDeclaringClass();
Ian Rogers04f94f42013-06-10 15:09:26 -07003055 const RegType& res_method_class =
3056 reg_types_.FromClass(ClassHelper(klass).GetDescriptor(), klass,
3057 klass->CannotBeAssignedFromOtherTypes());
Ian Rogers9074b992011-10-26 17:41:55 -07003058 if (!res_method_class.IsAssignableFrom(actual_arg_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07003059 Fail(actual_arg_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS:
3060 VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003061 << "' not instance of '" << res_method_class << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07003062 return NULL;
3063 }
3064 }
3065 actual_args++;
3066 }
3067 /*
3068 * Process the target method's signature. This signature may or may not
3069 * have been verified, so we can't assume it's properly formed.
3070 */
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003071 MethodHelper mh(res_method);
3072 const DexFile::TypeList* params = mh.GetParameterTypeList();
3073 size_t params_size = params == NULL ? 0 : params->Size();
Sebastien Hertz5243e912013-05-21 10:55:07 +02003074 uint32_t arg[5];
3075 if (!is_range) {
3076 inst->GetArgs(arg);
3077 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003078 for (size_t param_index = 0; param_index < params_size; param_index++) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003079 if (actual_args >= expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07003080 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invalid call to '" << PrettyMethod(res_method)
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003081 << "'. Expected " << expected_args << " arguments, processing argument " << actual_args
3082 << " (where longs/doubles count twice).";
Ian Rogersd81871c2011-10-03 13:57:23 -07003083 return NULL;
3084 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003085 const char* descriptor =
3086 mh.GetTypeDescriptorFromTypeIdx(params->GetTypeItem(param_index).type_idx_);
3087 if (descriptor == NULL) {
jeffhaod5347e02012-03-22 17:25:05 -07003088 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003089 << " missing signature component";
3090 return NULL;
Ian Rogersd81871c2011-10-03 13:57:23 -07003091 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07003092 const RegType& reg_type = reg_types_.FromDescriptor(class_loader_->get(), descriptor, false);
Sebastien Hertz5243e912013-05-21 10:55:07 +02003093 uint32_t get_reg = is_range ? inst->VRegC_3rc() + actual_args : arg[actual_args];
Jeff Haoa6b22c52013-10-04 14:33:22 -07003094 if (reg_type.IsIntegralTypes()) {
3095 const RegType& src_type = work_line_->GetRegisterType(get_reg);
3096 if (!src_type.IsIntegralTypes()) {
3097 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register v" << get_reg << " has type " << src_type
3098 << " but expected " << reg_type;
3099 return res_method;
3100 }
3101 } else if (!work_line_->VerifyRegisterType(get_reg, reg_type)) {
jeffhaob57e9522012-04-26 18:08:21 -07003102 return res_method;
Ian Rogersd81871c2011-10-03 13:57:23 -07003103 }
3104 actual_args = reg_type.IsLongOrDoubleTypes() ? actual_args + 2 : actual_args + 1;
3105 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003106 if (actual_args != expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07003107 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003108 << " expected " << expected_args << " arguments, found " << actual_args;
Ian Rogersd81871c2011-10-03 13:57:23 -07003109 return NULL;
3110 } else {
3111 return res_method;
3112 }
3113}
3114
Brian Carlstromea46f952013-07-30 01:26:50 -07003115mirror::ArtMethod* MethodVerifier::GetQuickInvokedMethod(const Instruction* inst,
Mathieu Chartier590fee92013-09-13 13:46:47 -07003116 RegisterLine* reg_line, bool is_range) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003117 DCHECK(inst->Opcode() == Instruction::INVOKE_VIRTUAL_QUICK ||
3118 inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
3119 const RegType& actual_arg_type = reg_line->GetInvocationThis(inst, is_range);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003120 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
3121 return NULL;
Sebastien Hertz8249b422013-10-29 17:50:55 +01003122 } else if (actual_arg_type.IsZero()) { // Invoke on "null" instance: we can't go further.
3123 return NULL;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003124 }
3125 mirror::Class* this_class = NULL;
3126 if (!actual_arg_type.IsUnresolvedTypes()) {
3127 this_class = actual_arg_type.GetClass();
3128 } else {
3129 const std::string& descriptor(actual_arg_type.GetDescriptor());
Ian Rogers98379392014-02-24 16:53:16 -08003130 Thread* self = Thread::Current();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003131 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Ian Rogers98379392014-02-24 16:53:16 -08003132 this_class = class_linker->FindClass(self, descriptor.c_str(), *class_loader_);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003133 if (this_class == NULL) {
Mathieu Chartier590fee92013-09-13 13:46:47 -07003134 Thread* self = Thread::Current();
3135 self->ClearException();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003136 // Look for a system class
Mathieu Chartier590fee92013-09-13 13:46:47 -07003137 SirtRef<mirror::ClassLoader> null_class_loader(self, nullptr);
Ian Rogers98379392014-02-24 16:53:16 -08003138 this_class = class_linker->FindClass(self, descriptor.c_str(), null_class_loader);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003139 }
3140 }
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003141 if (this_class == NULL) {
3142 return NULL;
3143 }
Brian Carlstromea46f952013-07-30 01:26:50 -07003144 mirror::ObjectArray<mirror::ArtMethod>* vtable = this_class->GetVTable();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003145 CHECK(vtable != NULL);
3146 uint16_t vtable_index = is_range ? inst->VRegB_3rc() : inst->VRegB_35c();
3147 CHECK(vtable_index < vtable->GetLength());
Brian Carlstromea46f952013-07-30 01:26:50 -07003148 mirror::ArtMethod* res_method = vtable->Get(vtable_index);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003149 CHECK(!Thread::Current()->IsExceptionPending());
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003150 return res_method;
3151}
3152
Brian Carlstromea46f952013-07-30 01:26:50 -07003153mirror::ArtMethod* MethodVerifier::VerifyInvokeVirtualQuickArgs(const Instruction* inst,
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003154 bool is_range) {
3155 DCHECK(Runtime::Current()->IsStarted());
Brian Carlstromea46f952013-07-30 01:26:50 -07003156 mirror::ArtMethod* res_method = GetQuickInvokedMethod(inst, work_line_.get(),
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003157 is_range);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003158 if (res_method == NULL) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003159 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer method from " << inst->Name();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003160 return NULL;
3161 }
3162 CHECK(!res_method->IsDirect() && !res_method->IsStatic());
3163
3164 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
3165 // match the call to the signature. Also, we might be calling through an abstract method
3166 // definition (which doesn't have register count values).
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003167 const RegType& actual_arg_type = work_line_->GetInvocationThis(inst, is_range);
3168 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
3169 return NULL;
3170 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003171 const size_t expected_args = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
3172 /* caught by static verifier */
3173 DCHECK(is_range || expected_args <= 5);
3174 if (expected_args > code_item_->outs_size_) {
3175 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
3176 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
3177 return NULL;
3178 }
3179
3180 /*
3181 * Check the "this" argument, which must be an instance of the class that declared the method.
3182 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
3183 * rigorous check here (which is okay since we have to do it at runtime).
3184 */
3185 if (actual_arg_type.IsUninitializedReference() && !res_method->IsConstructor()) {
3186 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
3187 return NULL;
3188 }
3189 if (!actual_arg_type.IsZero()) {
3190 mirror::Class* klass = res_method->GetDeclaringClass();
3191 const RegType& res_method_class =
3192 reg_types_.FromClass(ClassHelper(klass).GetDescriptor(), klass,
3193 klass->CannotBeAssignedFromOtherTypes());
3194 if (!res_method_class.IsAssignableFrom(actual_arg_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07003195 Fail(actual_arg_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS :
3196 VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003197 << "' not instance of '" << res_method_class << "'";
3198 return NULL;
3199 }
3200 }
3201 /*
3202 * Process the target method's signature. This signature may or may not
3203 * have been verified, so we can't assume it's properly formed.
3204 */
3205 MethodHelper mh(res_method);
3206 const DexFile::TypeList* params = mh.GetParameterTypeList();
3207 size_t params_size = params == NULL ? 0 : params->Size();
3208 uint32_t arg[5];
3209 if (!is_range) {
3210 inst->GetArgs(arg);
3211 }
3212 size_t actual_args = 1;
3213 for (size_t param_index = 0; param_index < params_size; param_index++) {
3214 if (actual_args >= expected_args) {
3215 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invalid call to '" << PrettyMethod(res_method)
Brian Carlstrom93c33962013-07-26 10:37:43 -07003216 << "'. Expected " << expected_args
3217 << " arguments, processing argument " << actual_args
3218 << " (where longs/doubles count twice).";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003219 return NULL;
3220 }
3221 const char* descriptor =
3222 mh.GetTypeDescriptorFromTypeIdx(params->GetTypeItem(param_index).type_idx_);
3223 if (descriptor == NULL) {
3224 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003225 << " missing signature component";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003226 return NULL;
3227 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07003228 const RegType& reg_type = reg_types_.FromDescriptor(class_loader_->get(), descriptor, false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003229 uint32_t get_reg = is_range ? inst->VRegC_3rc() + actual_args : arg[actual_args];
3230 if (!work_line_->VerifyRegisterType(get_reg, reg_type)) {
3231 return res_method;
3232 }
3233 actual_args = reg_type.IsLongOrDoubleTypes() ? actual_args + 2 : actual_args + 1;
3234 }
3235 if (actual_args != expected_args) {
3236 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
3237 << " expected " << expected_args << " arguments, found " << actual_args;
3238 return NULL;
3239 } else {
3240 return res_method;
3241 }
3242}
3243
Ian Rogers62342ec2013-06-11 10:26:37 -07003244void MethodVerifier::VerifyNewArray(const Instruction* inst, bool is_filled, bool is_range) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003245 uint32_t type_idx;
3246 if (!is_filled) {
3247 DCHECK_EQ(inst->Opcode(), Instruction::NEW_ARRAY);
3248 type_idx = inst->VRegC_22c();
3249 } else if (!is_range) {
3250 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY);
3251 type_idx = inst->VRegB_35c();
3252 } else {
3253 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY_RANGE);
3254 type_idx = inst->VRegB_3rc();
3255 }
3256 const RegType& res_type = ResolveClassAndCheckAccess(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003257 if (res_type.IsConflict()) { // bad class
3258 DCHECK_NE(failures_.size(), 0U);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003259 } else {
3260 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
3261 if (!res_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003262 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "new-array on non-array class " << res_type;
Ian Rogers0c4a5062012-02-03 15:18:59 -08003263 } else if (!is_filled) {
3264 /* make sure "size" register is valid type */
Sebastien Hertz5243e912013-05-21 10:55:07 +02003265 work_line_->VerifyRegisterType(inst->VRegB_22c(), reg_types_.Integer());
Ian Rogers0c4a5062012-02-03 15:18:59 -08003266 /* set register type to array class */
Ian Rogers62342ec2013-06-11 10:26:37 -07003267 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
3268 work_line_->SetRegisterType(inst->VRegA_22c(), precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003269 } else {
3270 // Verify each register. If "arg_count" is bad, VerifyRegisterType() will run off the end of
3271 // the list and fail. It's legal, if silly, for arg_count to be zero.
Mathieu Chartier590fee92013-09-13 13:46:47 -07003272 const RegType& expected_type = reg_types_.GetComponentType(res_type, class_loader_->get());
Sebastien Hertz5243e912013-05-21 10:55:07 +02003273 uint32_t arg_count = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
3274 uint32_t arg[5];
3275 if (!is_range) {
3276 inst->GetArgs(arg);
3277 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08003278 for (size_t ui = 0; ui < arg_count; ui++) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003279 uint32_t get_reg = is_range ? inst->VRegC_3rc() + ui : arg[ui];
Ian Rogers0c4a5062012-02-03 15:18:59 -08003280 if (!work_line_->VerifyRegisterType(get_reg, expected_type)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003281 work_line_->SetResultRegisterType(reg_types_.Conflict());
Ian Rogers0c4a5062012-02-03 15:18:59 -08003282 return;
3283 }
3284 }
3285 // filled-array result goes into "result" register
Ian Rogers62342ec2013-06-11 10:26:37 -07003286 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
3287 work_line_->SetResultRegisterType(precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003288 }
3289 }
3290}
3291
Sebastien Hertz5243e912013-05-21 10:55:07 +02003292void MethodVerifier::VerifyAGet(const Instruction* inst,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003293 const RegType& insn_type, bool is_primitive) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003294 const RegType& index_type = work_line_->GetRegisterType(inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07003295 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003296 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07003297 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003298 const RegType& array_type = work_line_->GetRegisterType(inst->VRegB_23x());
Ian Rogers89310de2012-02-01 13:47:30 -08003299 if (array_type.IsZero()) {
3300 // Null array class; this code path will fail at runtime. Infer a merge-able type from the
3301 // instruction type. TODO: have a proper notion of bottom here.
3302 if (!is_primitive || insn_type.IsCategory1Types()) {
3303 // Reference or category 1
Sebastien Hertz5243e912013-05-21 10:55:07 +02003304 work_line_->SetRegisterType(inst->VRegA_23x(), reg_types_.Zero());
Ian Rogersd81871c2011-10-03 13:57:23 -07003305 } else {
Ian Rogers89310de2012-02-01 13:47:30 -08003306 // Category 2
Sebastien Hertz5243e912013-05-21 10:55:07 +02003307 work_line_->SetRegisterTypeWide(inst->VRegA_23x(), reg_types_.FromCat2ConstLo(0, false),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003308 reg_types_.FromCat2ConstHi(0, false));
Ian Rogers89310de2012-02-01 13:47:30 -08003309 }
jeffhaofc3144e2012-02-01 17:21:15 -08003310 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003311 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aget";
Ian Rogers89310de2012-02-01 13:47:30 -08003312 } else {
3313 /* verify the class */
Mathieu Chartier590fee92013-09-13 13:46:47 -07003314 const RegType& component_type = reg_types_.GetComponentType(array_type, class_loader_->get());
jeffhaofc3144e2012-02-01 17:21:15 -08003315 if (!component_type.IsReferenceTypes() && !is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07003316 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08003317 << " source for aget-object";
3318 } else if (component_type.IsNonZeroReferenceTypes() && is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07003319 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08003320 << " source for category 1 aget";
3321 } else if (is_primitive && !insn_type.Equals(component_type) &&
3322 !((insn_type.IsInteger() && component_type.IsFloat()) ||
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003323 (insn_type.IsLong() && component_type.IsDouble()))) {
3324 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array type " << array_type
3325 << " incompatible with aget of type " << insn_type;
Ian Rogers89310de2012-02-01 13:47:30 -08003326 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003327 // Use knowledge of the field type which is stronger than the type inferred from the
3328 // instruction, which can't differentiate object types and ints from floats, longs from
3329 // doubles.
3330 if (!component_type.IsLowHalf()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003331 work_line_->SetRegisterType(inst->VRegA_23x(), component_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003332 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003333 work_line_->SetRegisterTypeWide(inst->VRegA_23x(), component_type,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003334 component_type.HighHalf(&reg_types_));
3335 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003336 }
3337 }
3338 }
3339}
3340
Jeff Haofe1f7c82013-08-01 14:50:24 -07003341void MethodVerifier::VerifyPrimitivePut(const RegType& target_type, const RegType& insn_type,
3342 const uint32_t vregA) {
3343 // Primitive assignability rules are weaker than regular assignability rules.
3344 bool instruction_compatible;
3345 bool value_compatible;
3346 const RegType& value_type = work_line_->GetRegisterType(vregA);
3347 if (target_type.IsIntegralTypes()) {
Jeff Haoa4647482013-08-06 15:35:47 -07003348 instruction_compatible = target_type.Equals(insn_type);
Jeff Haofe1f7c82013-08-01 14:50:24 -07003349 value_compatible = value_type.IsIntegralTypes();
3350 } else if (target_type.IsFloat()) {
3351 instruction_compatible = insn_type.IsInteger(); // no put-float, so expect put-int
3352 value_compatible = value_type.IsFloatTypes();
3353 } else if (target_type.IsLong()) {
3354 instruction_compatible = insn_type.IsLong();
3355 value_compatible = value_type.IsLongTypes();
3356 } else if (target_type.IsDouble()) {
3357 instruction_compatible = insn_type.IsLong(); // no put-double, so expect put-long
3358 value_compatible = value_type.IsDoubleTypes();
3359 } else {
3360 instruction_compatible = false; // reference with primitive store
3361 value_compatible = false; // unused
3362 }
3363 if (!instruction_compatible) {
3364 // This is a global failure rather than a class change failure as the instructions and
3365 // the descriptors for the type should have been consistent within the same file at
3366 // compile time.
3367 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "put insn has type '" << insn_type
3368 << "' but expected type '" << target_type << "'";
3369 return;
3370 }
3371 if (!value_compatible) {
3372 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
3373 << " of type " << value_type << " but expected " << target_type << " for put";
3374 return;
3375 }
3376}
3377
Sebastien Hertz5243e912013-05-21 10:55:07 +02003378void MethodVerifier::VerifyAPut(const Instruction* inst,
Ian Rogersd81871c2011-10-03 13:57:23 -07003379 const RegType& insn_type, bool is_primitive) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003380 const RegType& index_type = work_line_->GetRegisterType(inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07003381 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003382 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07003383 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003384 const RegType& array_type = work_line_->GetRegisterType(inst->VRegB_23x());
Ian Rogers89310de2012-02-01 13:47:30 -08003385 if (array_type.IsZero()) {
3386 // Null array type; this code path will fail at runtime. Infer a merge-able type from the
3387 // instruction type.
jeffhaofc3144e2012-02-01 17:21:15 -08003388 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003389 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aput";
Ian Rogers89310de2012-02-01 13:47:30 -08003390 } else {
Mathieu Chartier590fee92013-09-13 13:46:47 -07003391 const RegType& component_type = reg_types_.GetComponentType(array_type, class_loader_->get());
Jeff Haofe1f7c82013-08-01 14:50:24 -07003392 const uint32_t vregA = inst->VRegA_23x();
Jeff Haob24b4a72013-07-31 13:47:31 -07003393 if (is_primitive) {
Jeff Haofe1f7c82013-08-01 14:50:24 -07003394 VerifyPrimitivePut(component_type, insn_type, vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07003395 } else {
Jeff Haob24b4a72013-07-31 13:47:31 -07003396 if (!component_type.IsReferenceTypes()) {
3397 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
3398 << " source for aput-object";
3399 } else {
3400 // The instruction agrees with the type of array, confirm the value to be stored does too
3401 // Note: we use the instruction type (rather than the component type) for aput-object as
3402 // incompatible classes will be caught at runtime as an array store exception
Jeff Haofe1f7c82013-08-01 14:50:24 -07003403 work_line_->VerifyRegisterType(vregA, insn_type);
Jeff Haob24b4a72013-07-31 13:47:31 -07003404 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003405 }
3406 }
3407 }
3408}
3409
Brian Carlstromea46f952013-07-30 01:26:50 -07003410mirror::ArtField* MethodVerifier::GetStaticField(int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08003411 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3412 // Check access to class
3413 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Brian Carlstrom7934ac22013-07-26 10:54:15 -07003414 if (klass_type.IsConflict()) { // bad class
Ian Rogersad0b3a32012-04-16 14:50:24 -07003415 AppendToLastFailMessage(StringPrintf(" in attempt to access static field %d (%s) in %s",
3416 field_idx, dex_file_->GetFieldName(field_id),
3417 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers90040192011-12-16 08:54:29 -08003418 return NULL;
3419 }
Elliott Hughesb25c3f62012-03-26 16:35:06 -07003420 if (klass_type.IsUnresolvedTypes()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003421 return NULL; // Can't resolve Class so no more to do here, will do checking at runtime.
Ian Rogers90040192011-12-16 08:54:29 -08003422 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07003423 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
3424 mirror::ArtField* field = class_linker->ResolveFieldJLS(*dex_file_, field_idx, *dex_cache_,
3425 *class_loader_);
Ian Rogersd81871c2011-10-03 13:57:23 -07003426 if (field == NULL) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07003427 VLOG(verifier) << "Unable to resolve static field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07003428 << dex_file_->GetFieldName(field_id) << ") in "
3429 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07003430 DCHECK(Thread::Current()->IsExceptionPending());
3431 Thread::Current()->ClearException();
3432 return NULL;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003433 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
3434 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003435 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access static field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07003436 << " from " << GetDeclaringClass();
Ian Rogersd81871c2011-10-03 13:57:23 -07003437 return NULL;
3438 } else if (!field->IsStatic()) {
3439 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field) << " to be static";
3440 return NULL;
Ian Rogersd81871c2011-10-03 13:57:23 -07003441 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07003442 return field;
Ian Rogersd81871c2011-10-03 13:57:23 -07003443}
3444
Brian Carlstromea46f952013-07-30 01:26:50 -07003445mirror::ArtField* MethodVerifier::GetInstanceField(const RegType& obj_type, int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08003446 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3447 // Check access to class
3448 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003449 if (klass_type.IsConflict()) {
3450 AppendToLastFailMessage(StringPrintf(" in attempt to access instance field %d (%s) in %s",
3451 field_idx, dex_file_->GetFieldName(field_id),
3452 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers90040192011-12-16 08:54:29 -08003453 return NULL;
3454 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003455 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers90040192011-12-16 08:54:29 -08003456 return NULL; // Can't resolve Class so no more to do here
3457 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07003458 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
3459 mirror::ArtField* field = class_linker->ResolveFieldJLS(*dex_file_, field_idx, *dex_cache_,
3460 *class_loader_);
Ian Rogersd81871c2011-10-03 13:57:23 -07003461 if (field == NULL) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07003462 VLOG(verifier) << "Unable to resolve instance field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07003463 << dex_file_->GetFieldName(field_id) << ") in "
3464 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07003465 DCHECK(Thread::Current()->IsExceptionPending());
3466 Thread::Current()->ClearException();
3467 return NULL;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003468 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
3469 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003470 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access instance field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07003471 << " from " << GetDeclaringClass();
Ian Rogersd81871c2011-10-03 13:57:23 -07003472 return NULL;
3473 } else if (field->IsStatic()) {
3474 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field)
3475 << " to not be static";
3476 return NULL;
3477 } else if (obj_type.IsZero()) {
3478 // Cannot infer and check type, however, access will cause null pointer exception
3479 return field;
Ian Rogerse1758fe2012-04-19 11:31:15 -07003480 } else {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003481 mirror::Class* klass = field->GetDeclaringClass();
Ian Rogers637c65b2013-05-31 11:46:00 -07003482 const RegType& field_klass =
3483 reg_types_.FromClass(dex_file_->GetFieldDeclaringClassDescriptor(field_id),
Ian Rogers04f94f42013-06-10 15:09:26 -07003484 klass, klass->CannotBeAssignedFromOtherTypes());
Ian Rogersad0b3a32012-04-16 14:50:24 -07003485 if (obj_type.IsUninitializedTypes() &&
3486 (!IsConstructor() || GetDeclaringClass().Equals(obj_type) ||
3487 !field_klass.Equals(GetDeclaringClass()))) {
3488 // Field accesses through uninitialized references are only allowable for constructors where
3489 // the field is declared in this class
3490 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "cannot access instance field " << PrettyField(field)
Brian Carlstrom93c33962013-07-26 10:37:43 -07003491 << " of a not fully initialized object within the context"
3492 << " of " << PrettyMethod(dex_method_idx_, *dex_file_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003493 return NULL;
3494 } else if (!field_klass.IsAssignableFrom(obj_type)) {
3495 // Trying to access C1.field1 using reference of type C2, which is neither C1 or a sub-class
3496 // of C1. For resolution to occur the declared class of the field must be compatible with
3497 // obj_type, we've discovered this wasn't so, so report the field didn't exist.
3498 Fail(VERIFY_ERROR_NO_FIELD) << "cannot access instance field " << PrettyField(field)
3499 << " from object of type " << obj_type;
3500 return NULL;
3501 } else {
3502 return field;
3503 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003504 }
3505}
3506
Sebastien Hertz5243e912013-05-21 10:55:07 +02003507void MethodVerifier::VerifyISGet(const Instruction* inst, const RegType& insn_type,
3508 bool is_primitive, bool is_static) {
3509 uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
Brian Carlstromea46f952013-07-30 01:26:50 -07003510 mirror::ArtField* field;
Ian Rogersb94a27b2011-10-26 00:33:41 -07003511 if (is_static) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07003512 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003513 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003514 const RegType& object_type = work_line_->GetRegisterType(inst->VRegB_22c());
Ian Rogersf4028cc2011-11-02 14:56:39 -07003515 field = GetInstanceField(object_type, field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003516 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003517 const RegType* field_type = nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003518 if (field != NULL) {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003519 FieldHelper fh(field);
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08003520 mirror::Class* field_type_class = fh.GetType(can_load_classes_);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003521 if (field_type_class != nullptr) {
3522 field_type = &reg_types_.FromClass(fh.GetTypeDescriptor(), field_type_class,
3523 field_type_class->CannotBeAssignedFromOtherTypes());
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08003524 } else {
3525 Thread* self = Thread::Current();
3526 DCHECK(!can_load_classes_ || self->IsExceptionPending());
3527 self->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003528 }
Ian Rogers0d604842012-04-16 14:50:24 -07003529 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003530 if (field_type == nullptr) {
3531 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3532 const char* descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
Mathieu Chartier590fee92013-09-13 13:46:47 -07003533 field_type = &reg_types_.FromDescriptor(class_loader_->get(), descriptor, false);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003534 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02003535 const uint32_t vregA = (is_static) ? inst->VRegA_21c() : inst->VRegA_22c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07003536 if (is_primitive) {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003537 if (field_type->Equals(insn_type) ||
3538 (field_type->IsFloat() && insn_type.IsInteger()) ||
3539 (field_type->IsDouble() && insn_type.IsLong())) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003540 // expected that read is of the correct primitive type or that int reads are reading
3541 // floats or long reads are reading doubles
3542 } else {
3543 // This is a global failure rather than a class change failure as the instructions and
3544 // the descriptors for the type should have been consistent within the same file at
3545 // compile time
3546 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
3547 << " to be of type '" << insn_type
3548 << "' but found type '" << field_type << "' in get";
Ian Rogersad0b3a32012-04-16 14:50:24 -07003549 return;
3550 }
3551 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003552 if (!insn_type.IsAssignableFrom(*field_type)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003553 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
3554 << " to be compatible with type '" << insn_type
3555 << "' but found type '" << field_type
3556 << "' in get-object";
Sebastien Hertz5243e912013-05-21 10:55:07 +02003557 work_line_->SetRegisterType(vregA, reg_types_.Conflict());
Ian Rogersad0b3a32012-04-16 14:50:24 -07003558 return;
3559 }
3560 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003561 if (!field_type->IsLowHalf()) {
3562 work_line_->SetRegisterType(vregA, *field_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003563 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003564 work_line_->SetRegisterTypeWide(vregA, *field_type, field_type->HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003565 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003566}
3567
Sebastien Hertz5243e912013-05-21 10:55:07 +02003568void MethodVerifier::VerifyISPut(const Instruction* inst, const RegType& insn_type,
3569 bool is_primitive, bool is_static) {
3570 uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
Brian Carlstromea46f952013-07-30 01:26:50 -07003571 mirror::ArtField* field;
Ian Rogersb94a27b2011-10-26 00:33:41 -07003572 if (is_static) {
Ian Rogers55d249f2011-11-02 16:48:09 -07003573 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003574 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003575 const RegType& object_type = work_line_->GetRegisterType(inst->VRegB_22c());
Ian Rogers55d249f2011-11-02 16:48:09 -07003576 field = GetInstanceField(object_type, field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003577 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003578 const RegType* field_type = nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003579 if (field != NULL) {
3580 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
3581 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
3582 << " from other class " << GetDeclaringClass();
3583 return;
3584 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003585 FieldHelper fh(field);
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08003586 mirror::Class* field_type_class = fh.GetType(can_load_classes_);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003587 if (field_type_class != nullptr) {
3588 field_type = &reg_types_.FromClass(fh.GetTypeDescriptor(), field_type_class,
3589 field_type_class->CannotBeAssignedFromOtherTypes());
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08003590 } else {
3591 Thread* self = Thread::Current();
3592 DCHECK(!can_load_classes_ || self->IsExceptionPending());
3593 self->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003594 }
3595 }
3596 if (field_type == nullptr) {
3597 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3598 const char* descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
Mathieu Chartier590fee92013-09-13 13:46:47 -07003599 field_type = &reg_types_.FromDescriptor(class_loader_->get(), descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003600 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02003601 const uint32_t vregA = (is_static) ? inst->VRegA_21c() : inst->VRegA_22c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07003602 if (is_primitive) {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003603 VerifyPrimitivePut(*field_type, insn_type, vregA);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003604 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003605 if (!insn_type.IsAssignableFrom(*field_type)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003606 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
3607 << " to be compatible with type '" << insn_type
3608 << "' but found type '" << field_type
3609 << "' in put-object";
3610 return;
3611 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003612 work_line_->VerifyRegisterType(vregA, *field_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07003613 }
3614}
3615
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003616// Look for an instance field with this offset.
3617// TODO: we may speed up the search if offsets are sorted by doing a quick search.
Ian Rogersef7d42f2014-01-06 12:55:46 -08003618static mirror::ArtField* FindInstanceFieldWithOffset(mirror::Class* klass, uint32_t field_offset)
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003619 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08003620 mirror::ObjectArray<mirror::ArtField>* instance_fields = klass->GetIFields();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003621 if (instance_fields != NULL) {
3622 for (int32_t i = 0, e = instance_fields->GetLength(); i < e; ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -07003623 mirror::ArtField* field = instance_fields->Get(i);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003624 if (field->GetOffset().Uint32Value() == field_offset) {
3625 return field;
3626 }
3627 }
3628 }
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003629 // We did not find field in class: look into superclass.
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003630 if (klass->GetSuperClass() != NULL) {
3631 return FindInstanceFieldWithOffset(klass->GetSuperClass(), field_offset);
3632 } else {
3633 return NULL;
3634 }
3635}
3636
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003637// Returns the access field of a quick field access (iget/iput-quick) or NULL
3638// if it cannot be found.
Brian Carlstromea46f952013-07-30 01:26:50 -07003639mirror::ArtField* MethodVerifier::GetQuickFieldAccess(const Instruction* inst,
Ian Rogers98379392014-02-24 16:53:16 -08003640 RegisterLine* reg_line) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003641 DCHECK(inst->Opcode() == Instruction::IGET_QUICK ||
3642 inst->Opcode() == Instruction::IGET_WIDE_QUICK ||
3643 inst->Opcode() == Instruction::IGET_OBJECT_QUICK ||
3644 inst->Opcode() == Instruction::IPUT_QUICK ||
3645 inst->Opcode() == Instruction::IPUT_WIDE_QUICK ||
3646 inst->Opcode() == Instruction::IPUT_OBJECT_QUICK);
3647 const RegType& object_type = reg_line->GetRegisterType(inst->VRegB_22c());
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003648 mirror::Class* object_class = NULL;
3649 if (!object_type.IsUnresolvedTypes()) {
3650 object_class = object_type.GetClass();
3651 } else {
3652 // We need to resolve the class from its descriptor.
3653 const std::string& descriptor(object_type.GetDescriptor());
3654 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartier590fee92013-09-13 13:46:47 -07003655 Thread* self = Thread::Current();
Ian Rogers98379392014-02-24 16:53:16 -08003656 object_class = class_linker->FindClass(self, descriptor.c_str(), *class_loader_);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003657 if (object_class == NULL) {
Mathieu Chartier590fee92013-09-13 13:46:47 -07003658 self->ClearException();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003659 // Look for a system class
Mathieu Chartier590fee92013-09-13 13:46:47 -07003660 SirtRef<mirror::ClassLoader> null_class_loader(self, nullptr);
Ian Rogers98379392014-02-24 16:53:16 -08003661 object_class = class_linker->FindClass(self, descriptor.c_str(), null_class_loader);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003662 }
3663 }
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003664 if (object_class == NULL) {
3665 // Failed to get the Class* from reg type.
3666 LOG(WARNING) << "Failed to get Class* from " << object_type;
3667 return NULL;
3668 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003669 uint32_t field_offset = static_cast<uint32_t>(inst->VRegC_22c());
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003670 return FindInstanceFieldWithOffset(object_class, field_offset);
3671}
3672
3673void MethodVerifier::VerifyIGetQuick(const Instruction* inst, const RegType& insn_type,
3674 bool is_primitive) {
3675 DCHECK(Runtime::Current()->IsStarted());
Brian Carlstromea46f952013-07-30 01:26:50 -07003676 mirror::ArtField* field = GetQuickFieldAccess(inst, work_line_.get());
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003677 if (field == NULL) {
3678 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field from " << inst->Name();
3679 return;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003680 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003681 FieldHelper fh(field);
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08003682 mirror::Class* field_type_class = fh.GetType(can_load_classes_);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003683 const RegType* field_type;
3684 if (field_type_class != nullptr) {
3685 field_type = &reg_types_.FromClass(fh.GetTypeDescriptor(), field_type_class,
3686 field_type_class->CannotBeAssignedFromOtherTypes());
3687 } else {
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08003688 Thread* self = Thread::Current();
3689 DCHECK(!can_load_classes_ || self->IsExceptionPending());
3690 self->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003691 field_type = &reg_types_.FromDescriptor(field->GetDeclaringClass()->GetClassLoader(),
3692 fh.GetTypeDescriptor(), false);
3693 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003694 const uint32_t vregA = inst->VRegA_22c();
3695 if (is_primitive) {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003696 if (field_type->Equals(insn_type) ||
3697 (field_type->IsFloat() && insn_type.IsIntegralTypes()) ||
3698 (field_type->IsDouble() && insn_type.IsLongTypes())) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003699 // expected that read is of the correct primitive type or that int reads are reading
3700 // floats or long reads are reading doubles
3701 } else {
3702 // This is a global failure rather than a class change failure as the instructions and
3703 // the descriptors for the type should have been consistent within the same file at
3704 // compile time
3705 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003706 << " to be of type '" << insn_type
3707 << "' but found type '" << field_type << "' in get";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003708 return;
3709 }
3710 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003711 if (!insn_type.IsAssignableFrom(*field_type)) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003712 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003713 << " to be compatible with type '" << insn_type
3714 << "' but found type '" << field_type
3715 << "' in get-object";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003716 work_line_->SetRegisterType(vregA, reg_types_.Conflict());
3717 return;
3718 }
3719 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003720 if (!field_type->IsLowHalf()) {
3721 work_line_->SetRegisterType(vregA, *field_type);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003722 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003723 work_line_->SetRegisterTypeWide(vregA, *field_type, field_type->HighHalf(&reg_types_));
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003724 }
3725}
3726
3727void MethodVerifier::VerifyIPutQuick(const Instruction* inst, const RegType& insn_type,
3728 bool is_primitive) {
3729 DCHECK(Runtime::Current()->IsStarted());
Brian Carlstromea46f952013-07-30 01:26:50 -07003730 mirror::ArtField* field = GetQuickFieldAccess(inst, work_line_.get());
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003731 if (field == NULL) {
3732 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field from " << inst->Name();
3733 return;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003734 }
3735 const char* descriptor = FieldHelper(field).GetTypeDescriptor();
3736 mirror::ClassLoader* loader = field->GetDeclaringClass()->GetClassLoader();
3737 const RegType& field_type = reg_types_.FromDescriptor(loader, descriptor, false);
3738 if (field != NULL) {
3739 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
3740 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003741 << " from other class " << GetDeclaringClass();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003742 return;
3743 }
3744 }
3745 const uint32_t vregA = inst->VRegA_22c();
3746 if (is_primitive) {
3747 // Primitive field assignability rules are weaker than regular assignability rules
3748 bool instruction_compatible;
3749 bool value_compatible;
3750 const RegType& value_type = work_line_->GetRegisterType(vregA);
3751 if (field_type.IsIntegralTypes()) {
3752 instruction_compatible = insn_type.IsIntegralTypes();
3753 value_compatible = value_type.IsIntegralTypes();
3754 } else if (field_type.IsFloat()) {
3755 instruction_compatible = insn_type.IsInteger(); // no [is]put-float, so expect [is]put-int
3756 value_compatible = value_type.IsFloatTypes();
3757 } else if (field_type.IsLong()) {
3758 instruction_compatible = insn_type.IsLong();
3759 value_compatible = value_type.IsLongTypes();
3760 } else if (field_type.IsDouble()) {
3761 instruction_compatible = insn_type.IsLong(); // no [is]put-double, so expect [is]put-long
3762 value_compatible = value_type.IsDoubleTypes();
3763 } else {
3764 instruction_compatible = false; // reference field with primitive store
3765 value_compatible = false; // unused
3766 }
3767 if (!instruction_compatible) {
3768 // This is a global failure rather than a class change failure as the instructions and
3769 // the descriptors for the type should have been consistent within the same file at
3770 // compile time
3771 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003772 << " to be of type '" << insn_type
3773 << "' but found type '" << field_type
3774 << "' in put";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003775 return;
3776 }
3777 if (!value_compatible) {
3778 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
3779 << " of type " << value_type
3780 << " but expected " << field_type
3781 << " for store to " << PrettyField(field) << " in put";
3782 return;
3783 }
3784 } else {
3785 if (!insn_type.IsAssignableFrom(field_type)) {
3786 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003787 << " to be compatible with type '" << insn_type
3788 << "' but found type '" << field_type
3789 << "' in put-object";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003790 return;
3791 }
3792 work_line_->VerifyRegisterType(vregA, field_type);
3793 }
3794}
3795
Ian Rogers776ac1f2012-04-13 23:36:36 -07003796bool MethodVerifier::CheckNotMoveException(const uint16_t* insns, int insn_idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003797 if ((insns[insn_idx] & 0xff) == Instruction::MOVE_EXCEPTION) {
jeffhaod5347e02012-03-22 17:25:05 -07003798 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-exception";
Ian Rogersd81871c2011-10-03 13:57:23 -07003799 return false;
3800 }
3801 return true;
3802}
3803
Ian Rogers776ac1f2012-04-13 23:36:36 -07003804bool MethodVerifier::UpdateRegisters(uint32_t next_insn, const RegisterLine* merge_line) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003805 bool changed = true;
3806 RegisterLine* target_line = reg_table_.GetLine(next_insn);
3807 if (!insn_flags_[next_insn].IsVisitedOrChanged()) {
jeffhaobdb76512011-09-07 11:43:16 -07003808 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003809 * We haven't processed this instruction before, and we haven't touched the registers here, so
3810 * there's nothing to "merge". Copy the registers over and mark it as changed. (This is the
3811 * only way a register can transition out of "unknown", so this is not just an optimization.)
jeffhaobdb76512011-09-07 11:43:16 -07003812 */
Ian Rogersb8c78592013-07-25 23:52:52 +00003813 if (!insn_flags_[next_insn].IsReturn()) {
3814 target_line->CopyFromLine(merge_line);
3815 } else {
Jeff Haob24b4a72013-07-31 13:47:31 -07003816 // Verify that the monitor stack is empty on return.
3817 if (!merge_line->VerifyMonitorStackEmpty()) {
3818 return false;
3819 }
Ian Rogersb8c78592013-07-25 23:52:52 +00003820 // For returns we only care about the operand to the return, all other registers are dead.
3821 // Initialize them as conflicts so they don't add to GC and deoptimization information.
3822 const Instruction* ret_inst = Instruction::At(code_item_->insns_ + next_insn);
3823 Instruction::Code opcode = ret_inst->Opcode();
3824 if ((opcode == Instruction::RETURN_VOID) || (opcode == Instruction::RETURN_VOID_BARRIER)) {
3825 target_line->MarkAllRegistersAsConflicts();
3826 } else {
3827 target_line->CopyFromLine(merge_line);
3828 if (opcode == Instruction::RETURN_WIDE) {
3829 target_line->MarkAllRegistersAsConflictsExceptWide(ret_inst->VRegA_11x());
3830 } else {
3831 target_line->MarkAllRegistersAsConflictsExcept(ret_inst->VRegA_11x());
3832 }
3833 }
3834 }
jeffhaobdb76512011-09-07 11:43:16 -07003835 } else {
Brian Carlstrom93c33962013-07-26 10:37:43 -07003836 UniquePtr<RegisterLine> copy(gDebugVerify ?
Ian Rogersd0fbd852013-09-24 18:17:04 -07003837 RegisterLine::Create(target_line->NumRegs(), this) :
Brian Carlstrom93c33962013-07-26 10:37:43 -07003838 NULL);
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003839 if (gDebugVerify) {
3840 copy->CopyFromLine(target_line);
3841 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003842 changed = target_line->MergeRegisters(merge_line);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003843 if (have_pending_hard_failure_) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003844 return false;
jeffhaobdb76512011-09-07 11:43:16 -07003845 }
Ian Rogers2c8a8572011-10-24 17:11:36 -07003846 if (gDebugVerify && changed) {
Elliott Hughes398f64b2012-03-26 18:05:48 -07003847 LogVerifyInfo() << "Merging at [" << reinterpret_cast<void*>(work_insn_idx_) << "]"
Elliott Hughesc073b072012-05-24 19:29:17 -07003848 << " to [" << reinterpret_cast<void*>(next_insn) << "]: " << "\n"
3849 << *copy.get() << " MERGE\n"
3850 << *merge_line << " ==\n"
3851 << *target_line << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07003852 }
3853 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003854 if (changed) {
3855 insn_flags_[next_insn].SetChanged();
jeffhaobdb76512011-09-07 11:43:16 -07003856 }
3857 return true;
3858}
3859
Ian Rogers7b3ddd22013-02-21 15:19:52 -08003860InstructionFlags* MethodVerifier::CurrentInsnFlags() {
Ian Rogers776ac1f2012-04-13 23:36:36 -07003861 return &insn_flags_[work_insn_idx_];
3862}
3863
Ian Rogersad0b3a32012-04-16 14:50:24 -07003864const RegType& MethodVerifier::GetMethodReturnType() {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003865 if (return_type_ == nullptr) {
3866 if (mirror_method_ != NULL) {
3867 MethodHelper mh(mirror_method_);
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08003868 mirror::Class* return_type_class = mh.GetReturnType(can_load_classes_);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003869 if (return_type_class != nullptr) {
Mathieu Chartier590fee92013-09-13 13:46:47 -07003870 return_type_ = &reg_types_.FromClass(mh.GetReturnTypeDescriptor(), return_type_class,
3871 return_type_class->CannotBeAssignedFromOtherTypes());
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003872 } else {
3873 Thread* self = Thread::Current();
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08003874 DCHECK(!can_load_classes_ || self->IsExceptionPending());
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003875 self->ClearException();
3876 }
3877 }
3878 if (return_type_ == nullptr) {
3879 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
3880 const DexFile::ProtoId& proto_id = dex_file_->GetMethodPrototype(method_id);
3881 uint16_t return_type_idx = proto_id.return_type_idx_;
3882 const char* descriptor = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(return_type_idx));
Mathieu Chartier590fee92013-09-13 13:46:47 -07003883 return_type_ = &reg_types_.FromDescriptor(class_loader_->get(), descriptor, false);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003884 }
3885 }
3886 return *return_type_;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003887}
3888
3889const RegType& MethodVerifier::GetDeclaringClass() {
Ian Rogers637c65b2013-05-31 11:46:00 -07003890 if (declaring_class_ == NULL) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003891 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
Brian Carlstrom93c33962013-07-26 10:37:43 -07003892 const char* descriptor
3893 = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(method_id.class_idx_));
Ian Rogers637c65b2013-05-31 11:46:00 -07003894 if (mirror_method_ != NULL) {
3895 mirror::Class* klass = mirror_method_->GetDeclaringClass();
Ian Rogers04f94f42013-06-10 15:09:26 -07003896 declaring_class_ = &reg_types_.FromClass(descriptor, klass,
3897 klass->CannotBeAssignedFromOtherTypes());
Ian Rogers637c65b2013-05-31 11:46:00 -07003898 } else {
Mathieu Chartier590fee92013-09-13 13:46:47 -07003899 declaring_class_ = &reg_types_.FromDescriptor(class_loader_->get(), descriptor, false);
Ian Rogers637c65b2013-05-31 11:46:00 -07003900 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003901 }
Ian Rogers637c65b2013-05-31 11:46:00 -07003902 return *declaring_class_;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003903}
3904
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003905std::vector<int32_t> MethodVerifier::DescribeVRegs(uint32_t dex_pc) {
3906 RegisterLine* line = reg_table_.GetLine(dex_pc);
3907 std::vector<int32_t> result;
3908 for (size_t i = 0; i < line->NumRegs(); ++i) {
3909 const RegType& type = line->GetRegisterType(i);
3910 if (type.IsConstant()) {
3911 result.push_back(type.IsPreciseConstant() ? kConstant : kImpreciseConstant);
3912 result.push_back(type.ConstantValue());
3913 } else if (type.IsConstantLo()) {
3914 result.push_back(type.IsPreciseConstantLo() ? kConstant : kImpreciseConstant);
3915 result.push_back(type.ConstantValueLo());
3916 } else if (type.IsConstantHi()) {
3917 result.push_back(type.IsPreciseConstantHi() ? kConstant : kImpreciseConstant);
3918 result.push_back(type.ConstantValueHi());
3919 } else if (type.IsIntegralTypes()) {
3920 result.push_back(kIntVReg);
3921 result.push_back(0);
3922 } else if (type.IsFloat()) {
3923 result.push_back(kFloatVReg);
3924 result.push_back(0);
3925 } else if (type.IsLong()) {
3926 result.push_back(kLongLoVReg);
3927 result.push_back(0);
3928 result.push_back(kLongHiVReg);
3929 result.push_back(0);
3930 ++i;
3931 } else if (type.IsDouble()) {
3932 result.push_back(kDoubleLoVReg);
3933 result.push_back(0);
3934 result.push_back(kDoubleHiVReg);
3935 result.push_back(0);
3936 ++i;
3937 } else if (type.IsUndefined() || type.IsConflict() || type.IsHighHalf()) {
3938 result.push_back(kUndefined);
3939 result.push_back(0);
3940 } else {
Ian Rogers7b3ddd22013-02-21 15:19:52 -08003941 CHECK(type.IsNonZeroReferenceTypes());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003942 result.push_back(kReferenceVReg);
3943 result.push_back(0);
3944 }
3945 }
3946 return result;
3947}
3948
Sebastien Hertz849600b2013-12-20 10:28:08 +01003949const RegType& MethodVerifier::DetermineCat1Constant(int32_t value, bool precise) {
3950 if (precise) {
3951 // Precise constant type.
3952 return reg_types_.FromCat1Const(value, true);
3953 } else {
3954 // Imprecise constant type.
3955 if (value < -32768) {
3956 return reg_types_.IntConstant();
3957 } else if (value < -128) {
3958 return reg_types_.ShortConstant();
3959 } else if (value < 0) {
3960 return reg_types_.ByteConstant();
3961 } else if (value == 0) {
3962 return reg_types_.Zero();
3963 } else if (value == 1) {
3964 return reg_types_.One();
3965 } else if (value < 128) {
3966 return reg_types_.PosByteConstant();
3967 } else if (value < 32768) {
3968 return reg_types_.PosShortConstant();
3969 } else if (value < 65536) {
3970 return reg_types_.CharConstant();
3971 } else {
3972 return reg_types_.IntConstant();
3973 }
3974 }
3975}
3976
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003977void MethodVerifier::Init() {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08003978 art::verifier::RegTypeCache::Init();
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003979}
3980
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003981void MethodVerifier::Shutdown() {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08003982 verifier::RegTypeCache::ShutDown();
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08003983}
jeffhaod1224c72012-02-29 13:43:08 -08003984
Mathieu Chartier83c8ee02014-01-28 14:50:23 -08003985void MethodVerifier::VisitRoots(RootCallback* callback, void* arg) {
3986 reg_types_.VisitRoots(callback, arg);
Mathieu Chartierc528dba2013-11-26 12:00:11 -08003987}
3988
Ian Rogersd81871c2011-10-03 13:57:23 -07003989} // namespace verifier
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07003990} // namespace art