blob: 09db7cd87da6136c81e64b9a80cf3217787344c9 [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070016
Mathieu Chartierc645f1d2014-03-06 18:11:53 -080017#include "method_verifier-inl.h"
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070018
Elliott Hughes1f359b02011-07-17 14:27:17 -070019#include <iostream>
20
Mathieu Chartierc7853442015-03-27 14:35:38 -070021#include "art_field-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070022#include "art_method-inl.h"
Elliott Hughes07ed66b2012-12-12 18:34:25 -080023#include "base/logging.h"
Ian Rogers637c65b2013-05-31 11:46:00 -070024#include "base/mutex-inl.h"
Vladimir Marko80afd022015-05-19 18:08:00 +010025#include "base/time_utils.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070026#include "class_linker.h"
Vladimir Marko2b5eaa22013-12-13 13:59:30 +000027#include "compiler_callbacks.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070028#include "dex_file-inl.h"
Ian Rogersd0583802013-06-01 10:51:46 -070029#include "dex_instruction-inl.h"
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080030#include "dex_instruction_utils.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070031#include "dex_instruction_visitor.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070032#include "gc/accounting/card_table-inl.h"
Ian Rogers2bcb4a42012-11-08 10:39:18 -080033#include "indenter.h"
Ian Rogers84fa0742011-10-25 18:13:30 -070034#include "intern_table.h"
Ian Rogers0571d352011-11-03 19:51:38 -070035#include "leb128.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080036#include "mirror/class.h"
37#include "mirror/class-inl.h"
Ian Rogers39ebcb82013-05-30 16:57:23 -070038#include "mirror/dex_cache-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080039#include "mirror/object-inl.h"
40#include "mirror/object_array-inl.h"
Ian Rogers7b078e82014-09-10 14:44:24 -070041#include "reg_type-inl.h"
Ian Rogers39ebcb82013-05-30 16:57:23 -070042#include "register_line-inl.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070043#include "runtime.h"
Mathieu Chartier590fee92013-09-13 13:46:47 -070044#include "scoped_thread_state_change.h"
Vladimir Marko80afd022015-05-19 18:08:00 +010045#include "utils.h"
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070046#include "handle_scope-inl.h"
Elliott Hughese222ee02012-12-13 14:41:43 -080047#include "verifier/dex_gc_map.h"
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070048
49namespace art {
Ian Rogersd81871c2011-10-03 13:57:23 -070050namespace verifier {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070051
Mathieu Chartier8e219ae2014-08-19 14:29:46 -070052static constexpr bool kTimeVerifyMethod = !kIsDebugBuild;
Ian Rogersebbdd872014-07-07 23:53:08 -070053static constexpr bool gDebugVerify = false;
Anwar Ghuloum75a43f12013-08-13 17:22:14 -070054// TODO: Add a constant to method_verifier to turn on verbose logging?
Ian Rogers2c8a8572011-10-24 17:11:36 -070055
Ian Rogers7b3ddd22013-02-21 15:19:52 -080056void PcToRegisterLineTable::Init(RegisterTrackingMode mode, InstructionFlags* flags,
Ian Rogersd81871c2011-10-03 13:57:23 -070057 uint32_t insns_size, uint16_t registers_size,
Ian Rogers776ac1f2012-04-13 23:36:36 -070058 MethodVerifier* verifier) {
Ian Rogersd81871c2011-10-03 13:57:23 -070059 DCHECK_GT(insns_size, 0U);
Ian Rogersd0fbd852013-09-24 18:17:04 -070060 register_lines_.reset(new RegisterLine*[insns_size]());
61 size_ = insns_size;
Ian Rogersd81871c2011-10-03 13:57:23 -070062 for (uint32_t i = 0; i < insns_size; i++) {
63 bool interesting = false;
64 switch (mode) {
65 case kTrackRegsAll:
66 interesting = flags[i].IsOpcode();
67 break;
Sameer Abu Asal02c42232013-04-30 12:09:45 -070068 case kTrackCompilerInterestPoints:
Brian Carlstrom02c8cc62013-07-18 15:54:44 -070069 interesting = flags[i].IsCompileTimeInfoPoint() || flags[i].IsBranchTarget();
Ian Rogersd81871c2011-10-03 13:57:23 -070070 break;
71 case kTrackRegsBranches:
72 interesting = flags[i].IsBranchTarget();
73 break;
74 default:
75 break;
76 }
77 if (interesting) {
Ian Rogersd0fbd852013-09-24 18:17:04 -070078 register_lines_[i] = RegisterLine::Create(registers_size, verifier);
79 }
80 }
81}
82
83PcToRegisterLineTable::~PcToRegisterLineTable() {
84 for (size_t i = 0; i < size_; i++) {
85 delete register_lines_[i];
86 if (kIsDebugBuild) {
87 register_lines_[i] = nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -070088 }
89 }
90}
91
Andreas Gampe7c038102014-10-27 20:08:46 -070092// Note: returns true on failure.
93ALWAYS_INLINE static inline bool FailOrAbort(MethodVerifier* verifier, bool condition,
94 const char* error_msg, uint32_t work_insn_idx) {
95 if (kIsDebugBuild) {
96 // In a debug build, abort if the error condition is wrong.
97 DCHECK(condition) << error_msg << work_insn_idx;
98 } else {
99 // In a non-debug build, just fail the class.
100 if (!condition) {
101 verifier->Fail(VERIFY_ERROR_BAD_CLASS_HARD) << error_msg << work_insn_idx;
102 return true;
103 }
104 }
105
106 return false;
107}
108
Stephen Kyle7e541c92014-12-17 17:10:02 +0000109static void SafelyMarkAllRegistersAsConflicts(MethodVerifier* verifier, RegisterLine* reg_line) {
110 if (verifier->IsConstructor()) {
111 // Before we mark all regs as conflicts, check that we don't have an uninitialized this.
112 reg_line->CheckConstructorReturn(verifier);
113 }
114 reg_line->MarkAllRegistersAsConflicts(verifier);
115}
116
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800117MethodVerifier::FailureKind MethodVerifier::VerifyMethod(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700118 ArtMethod* method, bool allow_soft_failures, std::string* error ATTRIBUTE_UNUSED) {
119 StackHandleScope<2> hs(Thread::Current());
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800120 mirror::Class* klass = method->GetDeclaringClass();
121 auto h_dex_cache(hs.NewHandle(klass->GetDexCache()));
122 auto h_class_loader(hs.NewHandle(klass->GetClassLoader()));
Mathieu Chartiere401d142015-04-22 13:56:20 -0700123 return VerifyMethod(hs.Self(), method->GetDexMethodIndex(), method->GetDexFile(), h_dex_cache,
124 h_class_loader, klass->GetClassDef(), method->GetCodeItem(), method,
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800125 method->GetAccessFlags(), allow_soft_failures, false);
126}
127
128
Ian Rogers7b078e82014-09-10 14:44:24 -0700129MethodVerifier::FailureKind MethodVerifier::VerifyClass(Thread* self,
130 mirror::Class* klass,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700131 bool allow_soft_failures,
132 std::string* error) {
jeffhaobdb76512011-09-07 11:43:16 -0700133 if (klass->IsVerified()) {
jeffhaof1e6b7c2012-06-05 18:33:30 -0700134 return kNoFailure;
jeffhaobdb76512011-09-07 11:43:16 -0700135 }
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800136 bool early_failure = false;
137 std::string failure_message;
Mathieu Chartierf8322842014-05-16 10:59:25 -0700138 const DexFile& dex_file = klass->GetDexFile();
139 const DexFile::ClassDef* class_def = klass->GetClassDef();
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800140 mirror::Class* super = klass->GetSuperClass();
Ian Rogers1ff3c982014-08-12 02:30:58 -0700141 std::string temp;
Ian Rogers7b078e82014-09-10 14:44:24 -0700142 if (super == nullptr && strcmp("Ljava/lang/Object;", klass->GetDescriptor(&temp)) != 0) {
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800143 early_failure = true;
144 failure_message = " that has no super class";
Ian Rogers7b078e82014-09-10 14:44:24 -0700145 } else if (super != nullptr && super->IsFinal()) {
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800146 early_failure = true;
147 failure_message = " that attempts to sub-class final class " + PrettyDescriptor(super);
Ian Rogers7b078e82014-09-10 14:44:24 -0700148 } else if (class_def == nullptr) {
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800149 early_failure = true;
150 failure_message = " that isn't present in dex file " + dex_file.GetLocation();
151 }
152 if (early_failure) {
153 *error = "Verifier rejected class " + PrettyDescriptor(klass) + failure_message;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800154 if (Runtime::Current()->IsAotCompiler()) {
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800155 ClassReference ref(&dex_file, klass->GetDexClassDefIndex());
Vladimir Marko2b5eaa22013-12-13 13:59:30 +0000156 Runtime::Current()->GetCompilerCallbacks()->ClassRejected(ref);
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800157 }
jeffhaof1e6b7c2012-06-05 18:33:30 -0700158 return kHardFailure;
jeffhaobdb76512011-09-07 11:43:16 -0700159 }
Ian Rogers7b078e82014-09-10 14:44:24 -0700160 StackHandleScope<2> hs(self);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700161 Handle<mirror::DexCache> dex_cache(hs.NewHandle(klass->GetDexCache()));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700162 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(klass->GetClassLoader()));
Mathieu Chartiere401d142015-04-22 13:56:20 -0700163 return VerifyClass(
164 self, &dex_file, dex_cache, class_loader, class_def, allow_soft_failures, error);
Shih-wei Liao371814f2011-10-27 16:52:10 -0700165}
166
Ian Rogers7b078e82014-09-10 14:44:24 -0700167MethodVerifier::FailureKind MethodVerifier::VerifyClass(Thread* self,
168 const DexFile* dex_file,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700169 Handle<mirror::DexCache> dex_cache,
170 Handle<mirror::ClassLoader> class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700171 const DexFile::ClassDef* class_def,
172 bool allow_soft_failures,
173 std::string* error) {
174 DCHECK(class_def != nullptr);
Andreas Gampe507cc6f2015-06-19 22:58:47 -0700175
176 // A class must not be abstract and final.
177 if ((class_def->access_flags_ & (kAccAbstract | kAccFinal)) == (kAccAbstract | kAccFinal)) {
178 *error = "Verifier rejected class ";
179 *error += PrettyDescriptor(dex_file->GetClassDescriptor(*class_def));
180 *error += ": class is abstract and final.";
181 return kHardFailure;
182 }
183
Ian Rogers13735952014-10-08 12:43:28 -0700184 const uint8_t* class_data = dex_file->GetClassData(*class_def);
Ian Rogers7b078e82014-09-10 14:44:24 -0700185 if (class_data == nullptr) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700186 // empty class, probably a marker interface
jeffhaof1e6b7c2012-06-05 18:33:30 -0700187 return kNoFailure;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700188 }
jeffhaof56197c2012-03-05 18:01:54 -0800189 ClassDataItemIterator it(*dex_file, class_data);
190 while (it.HasNextStaticField() || it.HasNextInstanceField()) {
191 it.Next();
192 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700193 size_t error_count = 0;
jeffhaof1e6b7c2012-06-05 18:33:30 -0700194 bool hard_fail = false;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700195 ClassLinker* linker = Runtime::Current()->GetClassLinker();
jeffhao9b0b1882012-10-01 16:51:22 -0700196 int64_t previous_direct_method_idx = -1;
jeffhaof56197c2012-03-05 18:01:54 -0800197 while (it.HasNextDirectMethod()) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700198 self->AllowThreadSuspension();
jeffhaof56197c2012-03-05 18:01:54 -0800199 uint32_t method_idx = it.GetMemberIndex();
jeffhao9b0b1882012-10-01 16:51:22 -0700200 if (method_idx == previous_direct_method_idx) {
201 // smali can create dex files with two encoded_methods sharing the same method_idx
202 // http://code.google.com/p/smali/issues/detail?id=119
203 it.Next();
204 continue;
205 }
206 previous_direct_method_idx = method_idx;
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700207 InvokeType type = it.GetMethodInvokeType(*class_def);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700208 ArtMethod* method = linker->ResolveMethod(
209 *dex_file, method_idx, dex_cache, class_loader, nullptr, type);
Ian Rogers7b078e82014-09-10 14:44:24 -0700210 if (method == nullptr) {
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700211 DCHECK(self->IsExceptionPending());
Ian Rogersad0b3a32012-04-16 14:50:24 -0700212 // We couldn't resolve the method, but continue regardless.
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700213 self->ClearException();
Mathieu Chartiere401d142015-04-22 13:56:20 -0700214 } else {
215 DCHECK(method->GetDeclaringClassUnchecked() != nullptr) << type;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700216 }
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700217 StackHandleScope<1> hs(self);
Ian Rogers7b078e82014-09-10 14:44:24 -0700218 MethodVerifier::FailureKind result = VerifyMethod(self,
219 method_idx,
Brian Carlstrom93c33962013-07-26 10:37:43 -0700220 dex_file,
221 dex_cache,
222 class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700223 class_def,
Brian Carlstrom93c33962013-07-26 10:37:43 -0700224 it.GetMethodCodeItem(),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700225 method, it.GetMethodAccessFlags(), allow_soft_failures, false);
jeffhaof1e6b7c2012-06-05 18:33:30 -0700226 if (result != kNoFailure) {
227 if (result == kHardFailure) {
228 hard_fail = true;
229 if (error_count > 0) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700230 *error += "\n";
jeffhaof1e6b7c2012-06-05 18:33:30 -0700231 }
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700232 *error = "Verifier rejected class ";
233 *error += PrettyDescriptor(dex_file->GetClassDescriptor(*class_def));
234 *error += " due to bad method ";
235 *error += PrettyMethod(method_idx, *dex_file);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700236 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700237 ++error_count;
jeffhaof56197c2012-03-05 18:01:54 -0800238 }
239 it.Next();
240 }
jeffhao9b0b1882012-10-01 16:51:22 -0700241 int64_t previous_virtual_method_idx = -1;
jeffhaof56197c2012-03-05 18:01:54 -0800242 while (it.HasNextVirtualMethod()) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700243 self->AllowThreadSuspension();
jeffhaof56197c2012-03-05 18:01:54 -0800244 uint32_t method_idx = it.GetMemberIndex();
jeffhao9b0b1882012-10-01 16:51:22 -0700245 if (method_idx == previous_virtual_method_idx) {
246 // smali can create dex files with two encoded_methods sharing the same method_idx
247 // http://code.google.com/p/smali/issues/detail?id=119
248 it.Next();
249 continue;
250 }
251 previous_virtual_method_idx = method_idx;
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700252 InvokeType type = it.GetMethodInvokeType(*class_def);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700253 ArtMethod* method = linker->ResolveMethod(
254 *dex_file, method_idx, dex_cache, class_loader, nullptr, type);
Ian Rogers7b078e82014-09-10 14:44:24 -0700255 if (method == nullptr) {
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700256 DCHECK(self->IsExceptionPending());
Ian Rogersad0b3a32012-04-16 14:50:24 -0700257 // We couldn't resolve the method, but continue regardless.
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700258 self->ClearException();
Ian Rogersad0b3a32012-04-16 14:50:24 -0700259 }
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700260 StackHandleScope<1> hs(self);
Ian Rogers7b078e82014-09-10 14:44:24 -0700261 MethodVerifier::FailureKind result = VerifyMethod(self,
262 method_idx,
Brian Carlstrom93c33962013-07-26 10:37:43 -0700263 dex_file,
264 dex_cache,
265 class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700266 class_def,
Brian Carlstrom93c33962013-07-26 10:37:43 -0700267 it.GetMethodCodeItem(),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700268 method, it.GetMethodAccessFlags(), allow_soft_failures, false);
jeffhaof1e6b7c2012-06-05 18:33:30 -0700269 if (result != kNoFailure) {
270 if (result == kHardFailure) {
271 hard_fail = true;
272 if (error_count > 0) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700273 *error += "\n";
jeffhaof1e6b7c2012-06-05 18:33:30 -0700274 }
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700275 *error = "Verifier rejected class ";
276 *error += PrettyDescriptor(dex_file->GetClassDescriptor(*class_def));
277 *error += " due to bad method ";
278 *error += PrettyMethod(method_idx, *dex_file);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700279 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700280 ++error_count;
jeffhaof56197c2012-03-05 18:01:54 -0800281 }
282 it.Next();
283 }
jeffhaof1e6b7c2012-06-05 18:33:30 -0700284 if (error_count == 0) {
285 return kNoFailure;
286 } else {
287 return hard_fail ? kHardFailure : kSoftFailure;
288 }
jeffhaof56197c2012-03-05 18:01:54 -0800289}
290
Andreas Gampea4f5bf62015-05-18 20:50:12 -0700291static bool IsLargeMethod(const DexFile::CodeItem* const code_item) {
Andreas Gampe3c651fc2015-05-21 14:06:46 -0700292 if (code_item == nullptr) {
293 return false;
294 }
295
Andreas Gampea4f5bf62015-05-18 20:50:12 -0700296 uint16_t registers_size = code_item->registers_size_;
297 uint32_t insns_size = code_item->insns_size_in_code_units_;
298
299 return registers_size * insns_size > 4*1024*1024;
300}
301
Ian Rogers7b078e82014-09-10 14:44:24 -0700302MethodVerifier::FailureKind MethodVerifier::VerifyMethod(Thread* self, uint32_t method_idx,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800303 const DexFile* dex_file,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700304 Handle<mirror::DexCache> dex_cache,
305 Handle<mirror::ClassLoader> class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700306 const DexFile::ClassDef* class_def,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800307 const DexFile::CodeItem* code_item,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700308 ArtMethod* method,
Jeff Haoee988952013-04-16 14:23:47 -0700309 uint32_t method_access_flags,
Ian Rogers46960fe2014-05-23 10:43:43 -0700310 bool allow_soft_failures,
311 bool need_precise_constants) {
Ian Rogersc8982582012-09-07 16:53:25 -0700312 MethodVerifier::FailureKind result = kNoFailure;
Mathieu Chartier8e219ae2014-08-19 14:29:46 -0700313 uint64_t start_ns = kTimeVerifyMethod ? NanoTime() : 0;
Ian Rogersc8982582012-09-07 16:53:25 -0700314
Ian Rogers7b078e82014-09-10 14:44:24 -0700315 MethodVerifier verifier(self, dex_file, dex_cache, class_loader, class_def, code_item,
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700316 method_idx, method, method_access_flags, true, allow_soft_failures,
Mathieu Chartier4306ef82014-12-19 18:41:47 -0800317 need_precise_constants, true);
Ian Rogers46960fe2014-05-23 10:43:43 -0700318 if (verifier.Verify()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700319 // Verification completed, however failures may be pending that didn't cause the verification
320 // to hard fail.
Ian Rogers46960fe2014-05-23 10:43:43 -0700321 CHECK(!verifier.have_pending_hard_failure_);
322 if (verifier.failures_.size() != 0) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -0700323 if (VLOG_IS_ON(verifier)) {
Ian Rogers46960fe2014-05-23 10:43:43 -0700324 verifier.DumpFailures(VLOG_STREAM(verifier) << "Soft verification failures in "
Anwar Ghuloum75a43f12013-08-13 17:22:14 -0700325 << PrettyMethod(method_idx, *dex_file) << "\n");
326 }
Ian Rogersc8982582012-09-07 16:53:25 -0700327 result = kSoftFailure;
jeffhaof56197c2012-03-05 18:01:54 -0800328 }
329 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700330 // Bad method data.
Ian Rogers46960fe2014-05-23 10:43:43 -0700331 CHECK_NE(verifier.failures_.size(), 0U);
332 CHECK(verifier.have_pending_hard_failure_);
333 verifier.DumpFailures(LOG(INFO) << "Verification error in "
Elliott Hughesc073b072012-05-24 19:29:17 -0700334 << PrettyMethod(method_idx, *dex_file) << "\n");
jeffhaof56197c2012-03-05 18:01:54 -0800335 if (gDebugVerify) {
Ian Rogers46960fe2014-05-23 10:43:43 -0700336 std::cout << "\n" << verifier.info_messages_.str();
337 verifier.Dump(std::cout);
jeffhaof56197c2012-03-05 18:01:54 -0800338 }
Ian Rogersc8982582012-09-07 16:53:25 -0700339 result = kHardFailure;
jeffhaof56197c2012-03-05 18:01:54 -0800340 }
Mathieu Chartier8e219ae2014-08-19 14:29:46 -0700341 if (kTimeVerifyMethod) {
342 uint64_t duration_ns = NanoTime() - start_ns;
343 if (duration_ns > MsToNs(100)) {
344 LOG(WARNING) << "Verification of " << PrettyMethod(method_idx, *dex_file)
Andreas Gampea4f5bf62015-05-18 20:50:12 -0700345 << " took " << PrettyDuration(duration_ns)
346 << (IsLargeMethod(code_item) ? " (large method)" : "");
Mathieu Chartier8e219ae2014-08-19 14:29:46 -0700347 }
Ian Rogersc8982582012-09-07 16:53:25 -0700348 }
349 return result;
jeffhaof56197c2012-03-05 18:01:54 -0800350}
351
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700352MethodVerifier* MethodVerifier::VerifyMethodAndDump(Thread* self, std::ostream& os, uint32_t dex_method_idx,
Mathieu Chartier590fee92013-09-13 13:46:47 -0700353 const DexFile* dex_file,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700354 Handle<mirror::DexCache> dex_cache,
355 Handle<mirror::ClassLoader> class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700356 const DexFile::ClassDef* class_def,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800357 const DexFile::CodeItem* code_item,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700358 ArtMethod* method,
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800359 uint32_t method_access_flags) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700360 MethodVerifier* verifier = new MethodVerifier(self, dex_file, dex_cache, class_loader,
361 class_def, code_item, dex_method_idx, method,
362 method_access_flags, true, true, true, true);
363 verifier->Verify();
364 verifier->DumpFailures(os);
365 os << verifier->info_messages_.str();
Andreas Gampe5cbcde22014-09-16 14:59:49 -0700366 // Only dump and return if no hard failures. Otherwise the verifier may be not fully initialized
367 // and querying any info is dangerous/can abort.
368 if (verifier->have_pending_hard_failure_) {
369 delete verifier;
370 return nullptr;
371 } else {
372 verifier->Dump(os);
373 return verifier;
374 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800375}
376
Ian Rogers7b078e82014-09-10 14:44:24 -0700377MethodVerifier::MethodVerifier(Thread* self,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700378 const DexFile* dex_file, Handle<mirror::DexCache> dex_cache,
379 Handle<mirror::ClassLoader> class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700380 const DexFile::ClassDef* class_def,
Mathieu Chartier590fee92013-09-13 13:46:47 -0700381 const DexFile::CodeItem* code_item, uint32_t dex_method_idx,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700382 ArtMethod* method, uint32_t method_access_flags,
Ian Rogers46960fe2014-05-23 10:43:43 -0700383 bool can_load_classes, bool allow_soft_failures,
Mathieu Chartier4306ef82014-12-19 18:41:47 -0800384 bool need_precise_constants, bool verify_to_dump,
385 bool allow_thread_suspension)
Ian Rogers7b078e82014-09-10 14:44:24 -0700386 : self_(self),
387 reg_types_(can_load_classes),
Andreas Gamped5ad72f2015-06-26 17:33:47 -0700388 work_insn_idx_(DexFile::kDexNoIndex),
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800389 dex_method_idx_(dex_method_idx),
Ian Rogers637c65b2013-05-31 11:46:00 -0700390 mirror_method_(method),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700391 method_access_flags_(method_access_flags),
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700392 return_type_(nullptr),
jeffhaof56197c2012-03-05 18:01:54 -0800393 dex_file_(dex_file),
394 dex_cache_(dex_cache),
395 class_loader_(class_loader),
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700396 class_def_(class_def),
jeffhaof56197c2012-03-05 18:01:54 -0800397 code_item_(code_item),
Ian Rogers7b078e82014-09-10 14:44:24 -0700398 declaring_class_(nullptr),
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700399 interesting_dex_pc_(-1),
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700400 monitor_enter_dex_pcs_(nullptr),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700401 have_pending_hard_failure_(false),
jeffhaofaf459e2012-08-31 15:32:47 -0700402 have_pending_runtime_throw_failure_(false),
Andreas Gamped12e7822015-06-25 10:26:40 -0700403 have_any_pending_runtime_throw_failure_(false),
jeffhaof56197c2012-03-05 18:01:54 -0800404 new_instance_count_(0),
Elliott Hughes80537bb2013-01-04 16:37:26 -0800405 monitor_enter_count_(0),
Jeff Haoee988952013-04-16 14:23:47 -0700406 can_load_classes_(can_load_classes),
Sebastien Hertz4d4adb12013-07-24 16:14:19 +0200407 allow_soft_failures_(allow_soft_failures),
Ian Rogers46960fe2014-05-23 10:43:43 -0700408 need_precise_constants_(need_precise_constants),
Sebastien Hertz4d4adb12013-07-24 16:14:19 +0200409 has_check_casts_(false),
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700410 has_virtual_or_interface_invokes_(false),
Mathieu Chartier4306ef82014-12-19 18:41:47 -0800411 verify_to_dump_(verify_to_dump),
Andreas Gamped5ad72f2015-06-26 17:33:47 -0700412 allow_thread_suspension_(allow_thread_suspension),
413 link_(nullptr) {
Mathieu Chartierd0ad2ee2015-03-31 14:59:59 -0700414 self->PushVerifier(this);
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700415 DCHECK(class_def != nullptr);
jeffhaof56197c2012-03-05 18:01:54 -0800416}
417
Mathieu Chartier590fee92013-09-13 13:46:47 -0700418MethodVerifier::~MethodVerifier() {
Mathieu Chartierd0ad2ee2015-03-31 14:59:59 -0700419 Thread::Current()->PopVerifier(this);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700420 STLDeleteElements(&failure_messages_);
421}
422
Mathieu Chartiere401d142015-04-22 13:56:20 -0700423void MethodVerifier::FindLocksAtDexPc(ArtMethod* m, uint32_t dex_pc,
Ian Rogers46960fe2014-05-23 10:43:43 -0700424 std::vector<uint32_t>* monitor_enter_dex_pcs) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700425 StackHandleScope<2> hs(Thread::Current());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700426 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache()));
427 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader()));
Mathieu Chartiere401d142015-04-22 13:56:20 -0700428 MethodVerifier verifier(hs.Self(), m->GetDexFile(), dex_cache, class_loader, &m->GetClassDef(),
429 m->GetCodeItem(), m->GetDexMethodIndex(), m, m->GetAccessFlags(),
Mathieu Chartier4306ef82014-12-19 18:41:47 -0800430 false, true, false, false);
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700431 verifier.interesting_dex_pc_ = dex_pc;
Ian Rogers46960fe2014-05-23 10:43:43 -0700432 verifier.monitor_enter_dex_pcs_ = monitor_enter_dex_pcs;
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700433 verifier.FindLocksAtDexPc();
434}
435
Andreas Gampecb3c08f2014-09-18 13:16:38 -0700436static bool HasMonitorEnterInstructions(const DexFile::CodeItem* const code_item) {
437 const Instruction* inst = Instruction::At(code_item->insns_);
438
439 uint32_t insns_size = code_item->insns_size_in_code_units_;
440 for (uint32_t dex_pc = 0; dex_pc < insns_size;) {
441 if (inst->Opcode() == Instruction::MONITOR_ENTER) {
442 return true;
443 }
444
445 dex_pc += inst->SizeInCodeUnits();
446 inst = inst->Next();
447 }
448
449 return false;
450}
451
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700452void MethodVerifier::FindLocksAtDexPc() {
Ian Rogers7b078e82014-09-10 14:44:24 -0700453 CHECK(monitor_enter_dex_pcs_ != nullptr);
454 CHECK(code_item_ != nullptr); // This only makes sense for methods with code.
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700455
Andreas Gampecb3c08f2014-09-18 13:16:38 -0700456 // Quick check whether there are any monitor_enter instructions at all.
457 if (!HasMonitorEnterInstructions(code_item_)) {
458 return;
459 }
460
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700461 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
462 // verification. In practice, the phase we want relies on data structures set up by all the
463 // earlier passes, so we just run the full method verification and bail out early when we've
464 // got what we wanted.
465 Verify();
466}
467
Mathieu Chartiere401d142015-04-22 13:56:20 -0700468ArtField* MethodVerifier::FindAccessedFieldAtDexPc(ArtMethod* m, uint32_t dex_pc) {
469 StackHandleScope<2> hs(Thread::Current());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700470 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache()));
471 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader()));
Mathieu Chartiere401d142015-04-22 13:56:20 -0700472 MethodVerifier verifier(hs.Self(), m->GetDexFile(), dex_cache, class_loader, &m->GetClassDef(),
473 m->GetCodeItem(), m->GetDexMethodIndex(), m, m->GetAccessFlags(), true,
474 true, false, true);
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200475 return verifier.FindAccessedFieldAtDexPc(dex_pc);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200476}
477
Mathieu Chartierc7853442015-03-27 14:35:38 -0700478ArtField* MethodVerifier::FindAccessedFieldAtDexPc(uint32_t dex_pc) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700479 CHECK(code_item_ != nullptr); // This only makes sense for methods with code.
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200480
481 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
482 // verification. In practice, the phase we want relies on data structures set up by all the
483 // earlier passes, so we just run the full method verification and bail out early when we've
484 // got what we wanted.
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200485 bool success = Verify();
486 if (!success) {
Ian Rogers9bc54402014-04-17 16:40:01 -0700487 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200488 }
489 RegisterLine* register_line = reg_table_.GetLine(dex_pc);
Ian Rogers7b078e82014-09-10 14:44:24 -0700490 if (register_line == nullptr) {
Ian Rogers9bc54402014-04-17 16:40:01 -0700491 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200492 }
493 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
494 return GetQuickFieldAccess(inst, register_line);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200495}
496
Mathieu Chartiere401d142015-04-22 13:56:20 -0700497ArtMethod* MethodVerifier::FindInvokedMethodAtDexPc(ArtMethod* m, uint32_t dex_pc) {
498 StackHandleScope<2> hs(Thread::Current());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700499 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache()));
500 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader()));
Mathieu Chartiere401d142015-04-22 13:56:20 -0700501 MethodVerifier verifier(hs.Self(), m->GetDexFile(), dex_cache, class_loader, &m->GetClassDef(),
502 m->GetCodeItem(), m->GetDexMethodIndex(), m, m->GetAccessFlags(), true,
503 true, false, true);
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200504 return verifier.FindInvokedMethodAtDexPc(dex_pc);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200505}
506
Mathieu Chartiere401d142015-04-22 13:56:20 -0700507ArtMethod* MethodVerifier::FindInvokedMethodAtDexPc(uint32_t dex_pc) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700508 CHECK(code_item_ != nullptr); // This only makes sense for methods with code.
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200509
510 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
511 // verification. In practice, the phase we want relies on data structures set up by all the
512 // earlier passes, so we just run the full method verification and bail out early when we've
513 // got what we wanted.
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200514 bool success = Verify();
515 if (!success) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700516 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200517 }
518 RegisterLine* register_line = reg_table_.GetLine(dex_pc);
Ian Rogers7b078e82014-09-10 14:44:24 -0700519 if (register_line == nullptr) {
520 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200521 }
522 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
523 const bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
Mathieu Chartier091d2382015-03-06 10:59:06 -0800524 return GetQuickInvokedMethod(inst, register_line, is_range, false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200525}
526
Mathieu Chartiere401d142015-04-22 13:56:20 -0700527SafeMap<uint32_t, std::set<uint32_t>> MethodVerifier::FindStringInitMap(ArtMethod* m) {
Jeff Hao848f70a2014-01-15 13:49:50 -0800528 Thread* self = Thread::Current();
Mathieu Chartiere401d142015-04-22 13:56:20 -0700529 StackHandleScope<2> hs(self);
Jeff Hao848f70a2014-01-15 13:49:50 -0800530 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache()));
531 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader()));
Jeff Hao848f70a2014-01-15 13:49:50 -0800532 MethodVerifier verifier(self, m->GetDexFile(), dex_cache, class_loader, &m->GetClassDef(),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700533 m->GetCodeItem(), m->GetDexMethodIndex(), m, m->GetAccessFlags(),
Jeff Hao848f70a2014-01-15 13:49:50 -0800534 true, true, false, true);
535 return verifier.FindStringInitMap();
536}
537
538SafeMap<uint32_t, std::set<uint32_t>>& MethodVerifier::FindStringInitMap() {
539 Verify();
540 return GetStringInitPcRegMap();
541}
542
Ian Rogersad0b3a32012-04-16 14:50:24 -0700543bool MethodVerifier::Verify() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700544 // If there aren't any instructions, make sure that's expected, then exit successfully.
Ian Rogers7b078e82014-09-10 14:44:24 -0700545 if (code_item_ == nullptr) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700546 if ((method_access_flags_ & (kAccNative | kAccAbstract)) == 0) {
jeffhaod5347e02012-03-22 17:25:05 -0700547 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "zero-length code in concrete non-native method";
jeffhaobdb76512011-09-07 11:43:16 -0700548 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -0700549 } else {
550 return true;
jeffhaobdb76512011-09-07 11:43:16 -0700551 }
jeffhaobdb76512011-09-07 11:43:16 -0700552 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700553 // Sanity-check the register counts. ins + locals = registers, so make sure that ins <= registers.
554 if (code_item_->ins_size_ > code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700555 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad register counts (ins=" << code_item_->ins_size_
556 << " regs=" << code_item_->registers_size_;
Ian Rogersd81871c2011-10-03 13:57:23 -0700557 return false;
jeffhaobdb76512011-09-07 11:43:16 -0700558 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700559 // Allocate and initialize an array to hold instruction data.
Ian Rogers7b3ddd22013-02-21 15:19:52 -0800560 insn_flags_.reset(new InstructionFlags[code_item_->insns_size_in_code_units_]());
Ian Rogersd81871c2011-10-03 13:57:23 -0700561 // Run through the instructions and see if the width checks out.
562 bool result = ComputeWidthsAndCountOps();
563 // Flag instructions guarded by a "try" block and check exception handlers.
564 result = result && ScanTryCatchBlocks();
565 // Perform static instruction verification.
566 result = result && VerifyInstructions();
Ian Rogersad0b3a32012-04-16 14:50:24 -0700567 // Perform code-flow analysis and return.
Vladimir Marko2b5eaa22013-12-13 13:59:30 +0000568 result = result && VerifyCodeFlow();
569 // Compute information for compiler.
570 if (result && Runtime::Current()->IsCompiler()) {
571 result = Runtime::Current()->GetCompilerCallbacks()->MethodVerified(this);
572 }
573 return result;
jeffhaoba5ebb92011-08-25 17:24:37 -0700574}
575
Ian Rogers776ac1f2012-04-13 23:36:36 -0700576std::ostream& MethodVerifier::Fail(VerifyError error) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700577 switch (error) {
578 case VERIFY_ERROR_NO_CLASS:
579 case VERIFY_ERROR_NO_FIELD:
580 case VERIFY_ERROR_NO_METHOD:
581 case VERIFY_ERROR_ACCESS_CLASS:
582 case VERIFY_ERROR_ACCESS_FIELD:
583 case VERIFY_ERROR_ACCESS_METHOD:
Ian Rogers08f753d2012-08-24 14:35:25 -0700584 case VERIFY_ERROR_INSTANTIATION:
585 case VERIFY_ERROR_CLASS_CHANGE:
Igor Murashkin158f35c2015-06-10 15:55:30 -0700586 case VERIFY_ERROR_FORCE_INTERPRETER:
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800587 if (Runtime::Current()->IsAotCompiler() || !can_load_classes_) {
jeffhaofaf459e2012-08-31 15:32:47 -0700588 // If we're optimistically running verification at compile time, turn NO_xxx, ACCESS_xxx,
589 // class change and instantiation errors into soft verification errors so that we re-verify
590 // at runtime. We may fail to find or to agree on access because of not yet available class
591 // loaders, or class loaders that will differ at runtime. In these cases, we don't want to
592 // affect the soundness of the code being compiled. Instead, the generated code runs "slow
593 // paths" that dynamically perform the verification and cause the behavior to be that akin
594 // to an interpreter.
595 error = VERIFY_ERROR_BAD_CLASS_SOFT;
596 } else {
Jeff Haoa3faaf42013-09-03 19:07:00 -0700597 // If we fail again at runtime, mark that this instruction would throw and force this
598 // method to be executed using the interpreter with checks.
jeffhaofaf459e2012-08-31 15:32:47 -0700599 have_pending_runtime_throw_failure_ = true;
Andreas Gamped7f8d052015-03-12 11:05:47 -0700600
601 // We need to save the work_line if the instruction wasn't throwing before. Otherwise we'll
602 // try to merge garbage.
603 // Note: this assumes that Fail is called before we do any work_line modifications.
Andreas Gamped5ad72f2015-06-26 17:33:47 -0700604 // Note: this can fail before we touch any instruction, for the signature of a method. So
605 // add a check.
606 if (work_insn_idx_ < DexFile::kDexNoIndex) {
607 const uint16_t* insns = code_item_->insns_ + work_insn_idx_;
608 const Instruction* inst = Instruction::At(insns);
609 int opcode_flags = Instruction::FlagsOf(inst->Opcode());
Andreas Gamped7f8d052015-03-12 11:05:47 -0700610
Andreas Gamped5ad72f2015-06-26 17:33:47 -0700611 if ((opcode_flags & Instruction::kThrow) == 0 && CurrentInsnFlags()->IsInTry()) {
612 saved_line_->CopyFromLine(work_line_.get());
613 }
Andreas Gamped7f8d052015-03-12 11:05:47 -0700614 }
jeffhaofaf459e2012-08-31 15:32:47 -0700615 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700616 break;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700617 // Indication that verification should be retried at runtime.
618 case VERIFY_ERROR_BAD_CLASS_SOFT:
Jeff Haoee988952013-04-16 14:23:47 -0700619 if (!allow_soft_failures_) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700620 have_pending_hard_failure_ = true;
621 }
622 break;
jeffhaod5347e02012-03-22 17:25:05 -0700623 // Hard verification failures at compile time will still fail at runtime, so the class is
624 // marked as rejected to prevent it from being compiled.
Ian Rogersad0b3a32012-04-16 14:50:24 -0700625 case VERIFY_ERROR_BAD_CLASS_HARD: {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800626 if (Runtime::Current()->IsAotCompiler()) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700627 ClassReference ref(dex_file_, dex_file_->GetIndexForClassDef(*class_def_));
Vladimir Marko2b5eaa22013-12-13 13:59:30 +0000628 Runtime::Current()->GetCompilerCallbacks()->ClassRejected(ref);
jeffhaod1224c72012-02-29 13:43:08 -0800629 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700630 have_pending_hard_failure_ = true;
631 break;
Ian Rogers47a05882012-02-03 12:23:33 -0800632 }
633 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700634 failures_.push_back(error);
Elena Sayapina78480ec2014-08-15 15:52:42 +0700635 std::string location(StringPrintf("%s: [0x%X] ", PrettyMethod(dex_method_idx_, *dex_file_).c_str(),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700636 work_insn_idx_));
Elena Sayapina78480ec2014-08-15 15:52:42 +0700637 std::ostringstream* failure_message = new std::ostringstream(location, std::ostringstream::ate);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700638 failure_messages_.push_back(failure_message);
639 return *failure_message;
640}
641
Ian Rogers576ca0c2014-06-06 15:58:22 -0700642std::ostream& MethodVerifier::LogVerifyInfo() {
643 return info_messages_ << "VFY: " << PrettyMethod(dex_method_idx_, *dex_file_)
644 << '[' << reinterpret_cast<void*>(work_insn_idx_) << "] : ";
645}
646
Ian Rogersad0b3a32012-04-16 14:50:24 -0700647void MethodVerifier::PrependToLastFailMessage(std::string prepend) {
648 size_t failure_num = failure_messages_.size();
649 DCHECK_NE(failure_num, 0U);
650 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
651 prepend += last_fail_message->str();
Elena Sayapina78480ec2014-08-15 15:52:42 +0700652 failure_messages_[failure_num - 1] = new std::ostringstream(prepend, std::ostringstream::ate);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700653 delete last_fail_message;
654}
655
656void MethodVerifier::AppendToLastFailMessage(std::string append) {
657 size_t failure_num = failure_messages_.size();
658 DCHECK_NE(failure_num, 0U);
659 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
660 (*last_fail_message) << append;
Ian Rogers47a05882012-02-03 12:23:33 -0800661}
662
Ian Rogers776ac1f2012-04-13 23:36:36 -0700663bool MethodVerifier::ComputeWidthsAndCountOps() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700664 const uint16_t* insns = code_item_->insns_;
665 size_t insns_size = code_item_->insns_size_in_code_units_;
666 const Instruction* inst = Instruction::At(insns);
jeffhaobdb76512011-09-07 11:43:16 -0700667 size_t new_instance_count = 0;
668 size_t monitor_enter_count = 0;
Ian Rogersd81871c2011-10-03 13:57:23 -0700669 size_t dex_pc = 0;
jeffhaobdb76512011-09-07 11:43:16 -0700670
Ian Rogersd81871c2011-10-03 13:57:23 -0700671 while (dex_pc < insns_size) {
jeffhaobdb76512011-09-07 11:43:16 -0700672 Instruction::Code opcode = inst->Opcode();
Ian Rogersa9a82542013-10-04 11:17:26 -0700673 switch (opcode) {
674 case Instruction::APUT_OBJECT:
675 case Instruction::CHECK_CAST:
676 has_check_casts_ = true;
677 break;
678 case Instruction::INVOKE_VIRTUAL:
679 case Instruction::INVOKE_VIRTUAL_RANGE:
680 case Instruction::INVOKE_INTERFACE:
681 case Instruction::INVOKE_INTERFACE_RANGE:
682 has_virtual_or_interface_invokes_ = true;
683 break;
684 case Instruction::MONITOR_ENTER:
685 monitor_enter_count++;
686 break;
687 case Instruction::NEW_INSTANCE:
688 new_instance_count++;
689 break;
690 default:
691 break;
jeffhaobdb76512011-09-07 11:43:16 -0700692 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700693 size_t inst_size = inst->SizeInCodeUnits();
Ian Rogers7b078e82014-09-10 14:44:24 -0700694 insn_flags_[dex_pc].SetIsOpcode();
Ian Rogersd81871c2011-10-03 13:57:23 -0700695 dex_pc += inst_size;
Ian Rogers7b078e82014-09-10 14:44:24 -0700696 inst = inst->RelativeAt(inst_size);
jeffhaobdb76512011-09-07 11:43:16 -0700697 }
698
Ian Rogersd81871c2011-10-03 13:57:23 -0700699 if (dex_pc != insns_size) {
jeffhaod5347e02012-03-22 17:25:05 -0700700 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "code did not end where expected ("
701 << dex_pc << " vs. " << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700702 return false;
703 }
704
Ian Rogersd81871c2011-10-03 13:57:23 -0700705 new_instance_count_ = new_instance_count;
706 monitor_enter_count_ = monitor_enter_count;
jeffhaobdb76512011-09-07 11:43:16 -0700707 return true;
708}
709
Ian Rogers776ac1f2012-04-13 23:36:36 -0700710bool MethodVerifier::ScanTryCatchBlocks() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700711 uint32_t tries_size = code_item_->tries_size_;
jeffhaobdb76512011-09-07 11:43:16 -0700712 if (tries_size == 0) {
713 return true;
714 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700715 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Ian Rogers0571d352011-11-03 19:51:38 -0700716 const DexFile::TryItem* tries = DexFile::GetTryItems(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -0700717
718 for (uint32_t idx = 0; idx < tries_size; idx++) {
719 const DexFile::TryItem* try_item = &tries[idx];
720 uint32_t start = try_item->start_addr_;
721 uint32_t end = start + try_item->insn_count_;
jeffhaobdb76512011-09-07 11:43:16 -0700722 if ((start >= end) || (start >= insns_size) || (end > insns_size)) {
jeffhaod5347e02012-03-22 17:25:05 -0700723 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad exception entry: startAddr=" << start
724 << " endAddr=" << end << " (size=" << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700725 return false;
726 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700727 if (!insn_flags_[start].IsOpcode()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700728 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
729 << "'try' block starts inside an instruction (" << start << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700730 return false;
731 }
Ian Rogers7b078e82014-09-10 14:44:24 -0700732 uint32_t dex_pc = start;
733 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
734 while (dex_pc < end) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700735 insn_flags_[dex_pc].SetInTry();
Ian Rogers7b078e82014-09-10 14:44:24 -0700736 size_t insn_size = inst->SizeInCodeUnits();
737 dex_pc += insn_size;
738 inst = inst->RelativeAt(insn_size);
jeffhaobdb76512011-09-07 11:43:16 -0700739 }
740 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800741 // Iterate over each of the handlers to verify target addresses.
Ian Rogers13735952014-10-08 12:43:28 -0700742 const uint8_t* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -0700743 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700744 ClassLinker* linker = Runtime::Current()->GetClassLinker();
jeffhaobdb76512011-09-07 11:43:16 -0700745 for (uint32_t idx = 0; idx < handlers_size; idx++) {
Ian Rogers0571d352011-11-03 19:51:38 -0700746 CatchHandlerIterator iterator(handlers_ptr);
747 for (; iterator.HasNext(); iterator.Next()) {
748 uint32_t dex_pc= iterator.GetHandlerAddress();
Ian Rogersd81871c2011-10-03 13:57:23 -0700749 if (!insn_flags_[dex_pc].IsOpcode()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700750 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
751 << "exception handler starts at bad address (" << dex_pc << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700752 return false;
753 }
Stephen Kyle9bc61992014-09-22 13:53:15 +0100754 if (!CheckNotMoveResult(code_item_->insns_, dex_pc)) {
755 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
756 << "exception handler begins with move-result* (" << dex_pc << ")";
757 return false;
758 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700759 insn_flags_[dex_pc].SetBranchTarget();
Ian Rogers28ad40d2011-10-27 15:19:26 -0700760 // Ensure exception types are resolved so that they don't need resolution to be delivered,
761 // unresolved exception types will be ignored by exception delivery
Ian Rogers0571d352011-11-03 19:51:38 -0700762 if (iterator.GetHandlerTypeIndex() != DexFile::kDexNoIndex16) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800763 mirror::Class* exception_type = linker->ResolveType(*dex_file_,
764 iterator.GetHandlerTypeIndex(),
Mathieu Chartierbf99f772014-08-23 16:37:27 -0700765 dex_cache_, class_loader_);
Ian Rogers7b078e82014-09-10 14:44:24 -0700766 if (exception_type == nullptr) {
767 DCHECK(self_->IsExceptionPending());
768 self_->ClearException();
Ian Rogers28ad40d2011-10-27 15:19:26 -0700769 }
770 }
jeffhaobdb76512011-09-07 11:43:16 -0700771 }
Ian Rogers0571d352011-11-03 19:51:38 -0700772 handlers_ptr = iterator.EndDataPointer();
jeffhaobdb76512011-09-07 11:43:16 -0700773 }
jeffhaobdb76512011-09-07 11:43:16 -0700774 return true;
775}
776
Ian Rogers776ac1f2012-04-13 23:36:36 -0700777bool MethodVerifier::VerifyInstructions() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700778 const Instruction* inst = Instruction::At(code_item_->insns_);
jeffhaoba5ebb92011-08-25 17:24:37 -0700779
Ian Rogers0c7abda2012-09-19 13:33:42 -0700780 /* 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 -0700781 insn_flags_[0].SetBranchTarget();
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700782 insn_flags_[0].SetCompileTimeInfoPoint();
Ian Rogersd81871c2011-10-03 13:57:23 -0700783
784 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700785 for (uint32_t dex_pc = 0; dex_pc < insns_size;) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700786 if (!VerifyInstruction(inst, dex_pc)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700787 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -0700788 return false;
789 }
790 /* Flag instructions that are garbage collection points */
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700791 // All invoke points are marked as "Throw" points already.
792 // We are relying on this to also count all the invokes as interesting.
Vladimir Marko8b858e12014-11-27 14:52:37 +0000793 if (inst->IsBranch()) {
794 insn_flags_[dex_pc].SetCompileTimeInfoPoint();
795 // The compiler also needs safepoints for fall-through to loop heads.
796 // Such a loop head must be a target of a branch.
797 int32_t offset = 0;
798 bool cond, self_ok;
799 bool target_ok = GetBranchOffset(dex_pc, &offset, &cond, &self_ok);
800 DCHECK(target_ok);
801 insn_flags_[dex_pc + offset].SetCompileTimeInfoPoint();
802 } else if (inst->IsSwitch() || inst->IsThrow()) {
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700803 insn_flags_[dex_pc].SetCompileTimeInfoPoint();
Ian Rogersb8c78592013-07-25 23:52:52 +0000804 } else if (inst->IsReturn()) {
805 insn_flags_[dex_pc].SetCompileTimeInfoPointAndReturn();
Ian Rogersd81871c2011-10-03 13:57:23 -0700806 }
807 dex_pc += inst->SizeInCodeUnits();
808 inst = inst->Next();
809 }
810 return true;
811}
812
Ian Rogers776ac1f2012-04-13 23:36:36 -0700813bool MethodVerifier::VerifyInstruction(const Instruction* inst, uint32_t code_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700814 bool result = true;
815 switch (inst->GetVerifyTypeArgumentA()) {
816 case Instruction::kVerifyRegA:
Ian Rogers29a26482014-05-02 15:27:29 -0700817 result = result && CheckRegisterIndex(inst->VRegA());
Ian Rogersd81871c2011-10-03 13:57:23 -0700818 break;
819 case Instruction::kVerifyRegAWide:
Ian Rogers29a26482014-05-02 15:27:29 -0700820 result = result && CheckWideRegisterIndex(inst->VRegA());
Ian Rogersd81871c2011-10-03 13:57:23 -0700821 break;
822 }
823 switch (inst->GetVerifyTypeArgumentB()) {
824 case Instruction::kVerifyRegB:
Ian Rogers29a26482014-05-02 15:27:29 -0700825 result = result && CheckRegisterIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700826 break;
827 case Instruction::kVerifyRegBField:
Ian Rogers29a26482014-05-02 15:27:29 -0700828 result = result && CheckFieldIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700829 break;
830 case Instruction::kVerifyRegBMethod:
Ian Rogers29a26482014-05-02 15:27:29 -0700831 result = result && CheckMethodIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700832 break;
833 case Instruction::kVerifyRegBNewInstance:
Ian Rogers29a26482014-05-02 15:27:29 -0700834 result = result && CheckNewInstance(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700835 break;
836 case Instruction::kVerifyRegBString:
Ian Rogers29a26482014-05-02 15:27:29 -0700837 result = result && CheckStringIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700838 break;
839 case Instruction::kVerifyRegBType:
Ian Rogers29a26482014-05-02 15:27:29 -0700840 result = result && CheckTypeIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700841 break;
842 case Instruction::kVerifyRegBWide:
Ian Rogers29a26482014-05-02 15:27:29 -0700843 result = result && CheckWideRegisterIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700844 break;
845 }
846 switch (inst->GetVerifyTypeArgumentC()) {
847 case Instruction::kVerifyRegC:
Ian Rogers29a26482014-05-02 15:27:29 -0700848 result = result && CheckRegisterIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700849 break;
850 case Instruction::kVerifyRegCField:
Ian Rogers29a26482014-05-02 15:27:29 -0700851 result = result && CheckFieldIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700852 break;
853 case Instruction::kVerifyRegCNewArray:
Ian Rogers29a26482014-05-02 15:27:29 -0700854 result = result && CheckNewArray(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700855 break;
856 case Instruction::kVerifyRegCType:
Ian Rogers29a26482014-05-02 15:27:29 -0700857 result = result && CheckTypeIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700858 break;
859 case Instruction::kVerifyRegCWide:
Ian Rogers29a26482014-05-02 15:27:29 -0700860 result = result && CheckWideRegisterIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700861 break;
862 }
863 switch (inst->GetVerifyExtraFlags()) {
864 case Instruction::kVerifyArrayData:
865 result = result && CheckArrayData(code_offset);
866 break;
867 case Instruction::kVerifyBranchTarget:
868 result = result && CheckBranchTarget(code_offset);
869 break;
870 case Instruction::kVerifySwitchTargets:
871 result = result && CheckSwitchTargets(code_offset);
872 break;
Andreas Gampec3314312014-06-19 18:13:29 -0700873 case Instruction::kVerifyVarArgNonZero:
874 // Fall-through.
Ian Rogers29a26482014-05-02 15:27:29 -0700875 case Instruction::kVerifyVarArg: {
Taiju Tsuiki29498a22015-04-13 14:21:00 +0900876 // Instructions that can actually return a negative value shouldn't have this flag.
877 uint32_t v_a = dchecked_integral_cast<uint32_t>(inst->VRegA());
878 if ((inst->GetVerifyExtraFlags() == Instruction::kVerifyVarArgNonZero && v_a == 0) ||
879 v_a > Instruction::kMaxVarArgRegs) {
880 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << v_a << ") in "
Andreas Gampec3314312014-06-19 18:13:29 -0700881 "non-range invoke";
882 return false;
883 }
Taiju Tsuiki29498a22015-04-13 14:21:00 +0900884
Ian Rogers29a26482014-05-02 15:27:29 -0700885 uint32_t args[Instruction::kMaxVarArgRegs];
886 inst->GetVarArgs(args);
Taiju Tsuiki29498a22015-04-13 14:21:00 +0900887 result = result && CheckVarArgRegs(v_a, args);
Ian Rogersd81871c2011-10-03 13:57:23 -0700888 break;
Ian Rogers29a26482014-05-02 15:27:29 -0700889 }
Andreas Gampec3314312014-06-19 18:13:29 -0700890 case Instruction::kVerifyVarArgRangeNonZero:
891 // Fall-through.
Ian Rogersd81871c2011-10-03 13:57:23 -0700892 case Instruction::kVerifyVarArgRange:
Andreas Gampec3314312014-06-19 18:13:29 -0700893 if (inst->GetVerifyExtraFlags() == Instruction::kVerifyVarArgRangeNonZero &&
894 inst->VRegA() <= 0) {
895 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << inst->VRegA() << ") in "
896 "range invoke";
897 return false;
898 }
Ian Rogers29a26482014-05-02 15:27:29 -0700899 result = result && CheckVarArgRangeRegs(inst->VRegA(), inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700900 break;
901 case Instruction::kVerifyError:
jeffhaod5347e02012-03-22 17:25:05 -0700902 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected opcode " << inst->Name();
Ian Rogersd81871c2011-10-03 13:57:23 -0700903 result = false;
904 break;
905 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800906 if (inst->GetVerifyIsRuntimeOnly() && Runtime::Current()->IsAotCompiler() && !verify_to_dump_) {
Ian Rogers5fb22a92014-06-13 10:31:28 -0700907 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "opcode only expected at runtime " << inst->Name();
908 result = false;
909 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700910 return result;
911}
912
Ian Rogers7b078e82014-09-10 14:44:24 -0700913inline bool MethodVerifier::CheckRegisterIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700914 if (idx >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700915 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register index out of range (" << idx << " >= "
916 << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700917 return false;
918 }
919 return true;
920}
921
Ian Rogers7b078e82014-09-10 14:44:24 -0700922inline bool MethodVerifier::CheckWideRegisterIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700923 if (idx + 1 >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700924 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "wide register index out of range (" << idx
925 << "+1 >= " << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700926 return false;
927 }
928 return true;
929}
930
Ian Rogers7b078e82014-09-10 14:44:24 -0700931inline bool MethodVerifier::CheckFieldIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700932 if (idx >= dex_file_->GetHeader().field_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700933 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad field index " << idx << " (max "
934 << dex_file_->GetHeader().field_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700935 return false;
936 }
937 return true;
938}
939
Ian Rogers7b078e82014-09-10 14:44:24 -0700940inline bool MethodVerifier::CheckMethodIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700941 if (idx >= dex_file_->GetHeader().method_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700942 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad method index " << idx << " (max "
943 << dex_file_->GetHeader().method_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700944 return false;
945 }
946 return true;
947}
948
Ian Rogers7b078e82014-09-10 14:44:24 -0700949inline bool MethodVerifier::CheckNewInstance(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700950 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700951 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
952 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700953 return false;
954 }
955 // We don't need the actual class, just a pointer to the class name.
Ian Rogers0571d352011-11-03 19:51:38 -0700956 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -0700957 if (descriptor[0] != 'L') {
jeffhaod5347e02012-03-22 17:25:05 -0700958 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "can't call new-instance on type '" << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -0700959 return false;
960 }
961 return true;
962}
963
Ian Rogers7b078e82014-09-10 14:44:24 -0700964inline bool MethodVerifier::CheckStringIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700965 if (idx >= dex_file_->GetHeader().string_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700966 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad string index " << idx << " (max "
967 << dex_file_->GetHeader().string_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700968 return false;
969 }
970 return true;
971}
972
Ian Rogers7b078e82014-09-10 14:44:24 -0700973inline bool MethodVerifier::CheckTypeIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700974 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700975 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
976 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700977 return false;
978 }
979 return true;
980}
981
Ian Rogers776ac1f2012-04-13 23:36:36 -0700982bool MethodVerifier::CheckNewArray(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700983 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700984 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
985 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700986 return false;
987 }
988 int bracket_count = 0;
Ian Rogers0571d352011-11-03 19:51:38 -0700989 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -0700990 const char* cp = descriptor;
991 while (*cp++ == '[') {
992 bracket_count++;
993 }
994 if (bracket_count == 0) {
995 /* The given class must be an array type. */
Brian Carlstrom93c33962013-07-26 10:37:43 -0700996 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
997 << "can't new-array class '" << descriptor << "' (not an array)";
Ian Rogersd81871c2011-10-03 13:57:23 -0700998 return false;
999 } else if (bracket_count > 255) {
1000 /* It is illegal to create an array of more than 255 dimensions. */
Brian Carlstrom93c33962013-07-26 10:37:43 -07001001 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1002 << "can't new-array class '" << descriptor << "' (exceeds limit)";
Ian Rogersd81871c2011-10-03 13:57:23 -07001003 return false;
1004 }
1005 return true;
1006}
1007
Ian Rogers776ac1f2012-04-13 23:36:36 -07001008bool MethodVerifier::CheckArrayData(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001009 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
1010 const uint16_t* insns = code_item_->insns_ + cur_offset;
1011 const uint16_t* array_data;
1012 int32_t array_data_offset;
1013
1014 DCHECK_LT(cur_offset, insn_count);
1015 /* make sure the start of the array data table is in range */
1016 array_data_offset = insns[1] | (((int32_t) insns[2]) << 16);
1017 if ((int32_t) cur_offset + array_data_offset < 0 ||
1018 cur_offset + array_data_offset + 2 >= insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -07001019 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data start: at " << cur_offset
Brian Carlstrom93c33962013-07-26 10:37:43 -07001020 << ", data offset " << array_data_offset
1021 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -07001022 return false;
1023 }
1024 /* offset to array data table is a relative branch-style offset */
1025 array_data = insns + array_data_offset;
Andreas Gampe57c47582015-07-01 22:05:59 -07001026 // Make sure the table is at an even dex pc, that is, 32-bit aligned.
1027 if (!IsAligned<4>(array_data)) {
jeffhaod5347e02012-03-22 17:25:05 -07001028 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned array data table: at " << cur_offset
1029 << ", data offset " << array_data_offset;
Ian Rogersd81871c2011-10-03 13:57:23 -07001030 return false;
1031 }
Andreas Gampe57c47582015-07-01 22:05:59 -07001032 // Make sure the array-data is marked as an opcode. This ensures that it was reached when
1033 // traversing the code item linearly. It is an approximation for a by-spec padding value.
1034 if (!insn_flags_[cur_offset + array_data_offset].IsOpcode()) {
1035 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array data table at " << cur_offset
1036 << ", data offset " << array_data_offset
1037 << " not correctly visited, probably bad padding.";
1038 return false;
1039 }
1040
Ian Rogersd81871c2011-10-03 13:57:23 -07001041 uint32_t value_width = array_data[1];
Elliott Hughes398f64b2012-03-26 18:05:48 -07001042 uint32_t value_count = *reinterpret_cast<const uint32_t*>(&array_data[2]);
Ian Rogersd81871c2011-10-03 13:57:23 -07001043 uint32_t table_size = 4 + (value_width * value_count + 1) / 2;
1044 /* make sure the end of the switch is in range */
1045 if (cur_offset + array_data_offset + table_size > insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -07001046 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data end: at " << cur_offset
1047 << ", data offset " << array_data_offset << ", end "
1048 << cur_offset + array_data_offset + table_size
1049 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -07001050 return false;
1051 }
1052 return true;
1053}
1054
Ian Rogers776ac1f2012-04-13 23:36:36 -07001055bool MethodVerifier::CheckBranchTarget(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001056 int32_t offset;
1057 bool isConditional, selfOkay;
1058 if (!GetBranchOffset(cur_offset, &offset, &isConditional, &selfOkay)) {
1059 return false;
1060 }
1061 if (!selfOkay && offset == 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001062 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch offset of zero not allowed at"
1063 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -07001064 return false;
1065 }
Elliott Hughes81ff3182012-03-23 20:35:56 -07001066 // Check for 32-bit overflow. This isn't strictly necessary if we can depend on the runtime
1067 // to have identical "wrap-around" behavior, but it's unwise to depend on that.
Ian Rogersd81871c2011-10-03 13:57:23 -07001068 if (((int64_t) cur_offset + (int64_t) offset) != (int64_t) (cur_offset + offset)) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001069 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch target overflow "
1070 << reinterpret_cast<void*>(cur_offset) << " +" << offset;
Ian Rogersd81871c2011-10-03 13:57:23 -07001071 return false;
1072 }
1073 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
1074 int32_t abs_offset = cur_offset + offset;
Brian Carlstrom93c33962013-07-26 10:37:43 -07001075 if (abs_offset < 0 ||
1076 (uint32_t) abs_offset >= insn_count ||
1077 !insn_flags_[abs_offset].IsOpcode()) {
jeffhaod5347e02012-03-22 17:25:05 -07001078 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid branch target " << offset << " (-> "
Elliott Hughes398f64b2012-03-26 18:05:48 -07001079 << reinterpret_cast<void*>(abs_offset) << ") at "
1080 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -07001081 return false;
1082 }
1083 insn_flags_[abs_offset].SetBranchTarget();
1084 return true;
1085}
1086
Ian Rogers776ac1f2012-04-13 23:36:36 -07001087bool MethodVerifier::GetBranchOffset(uint32_t cur_offset, int32_t* pOffset, bool* pConditional,
Ian Rogersd81871c2011-10-03 13:57:23 -07001088 bool* selfOkay) {
1089 const uint16_t* insns = code_item_->insns_ + cur_offset;
1090 *pConditional = false;
1091 *selfOkay = false;
jeffhaoba5ebb92011-08-25 17:24:37 -07001092 switch (*insns & 0xff) {
1093 case Instruction::GOTO:
1094 *pOffset = ((int16_t) *insns) >> 8;
jeffhaoba5ebb92011-08-25 17:24:37 -07001095 break;
1096 case Instruction::GOTO_32:
1097 *pOffset = insns[1] | (((uint32_t) insns[2]) << 16);
jeffhaoba5ebb92011-08-25 17:24:37 -07001098 *selfOkay = true;
1099 break;
1100 case Instruction::GOTO_16:
1101 *pOffset = (int16_t) insns[1];
jeffhaoba5ebb92011-08-25 17:24:37 -07001102 break;
1103 case Instruction::IF_EQ:
1104 case Instruction::IF_NE:
1105 case Instruction::IF_LT:
1106 case Instruction::IF_GE:
1107 case Instruction::IF_GT:
1108 case Instruction::IF_LE:
1109 case Instruction::IF_EQZ:
1110 case Instruction::IF_NEZ:
1111 case Instruction::IF_LTZ:
1112 case Instruction::IF_GEZ:
1113 case Instruction::IF_GTZ:
1114 case Instruction::IF_LEZ:
1115 *pOffset = (int16_t) insns[1];
1116 *pConditional = true;
jeffhaoba5ebb92011-08-25 17:24:37 -07001117 break;
1118 default:
1119 return false;
jeffhaoba5ebb92011-08-25 17:24:37 -07001120 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001121 return true;
1122}
1123
Ian Rogers776ac1f2012-04-13 23:36:36 -07001124bool MethodVerifier::CheckSwitchTargets(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001125 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001126 DCHECK_LT(cur_offset, insn_count);
Ian Rogersd81871c2011-10-03 13:57:23 -07001127 const uint16_t* insns = code_item_->insns_ + cur_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001128 /* make sure the start of the switch is in range */
Ian Rogersd81871c2011-10-03 13:57:23 -07001129 int32_t switch_offset = insns[1] | ((int32_t) insns[2]) << 16;
Jeff Hao9ccd1512015-03-20 18:11:45 -07001130 if ((int32_t) cur_offset + switch_offset < 0 || cur_offset + switch_offset + 2 > insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -07001131 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch start: at " << cur_offset
Brian Carlstrom93c33962013-07-26 10:37:43 -07001132 << ", switch offset " << switch_offset
1133 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001134 return false;
1135 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001136 /* offset to switch table is a relative branch-style offset */
Ian Rogersd81871c2011-10-03 13:57:23 -07001137 const uint16_t* switch_insns = insns + switch_offset;
Andreas Gampe57c47582015-07-01 22:05:59 -07001138 // Make sure the table is at an even dex pc, that is, 32-bit aligned.
1139 if (!IsAligned<4>(switch_insns)) {
jeffhaod5347e02012-03-22 17:25:05 -07001140 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned switch table: at " << cur_offset
1141 << ", switch offset " << switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001142 return false;
1143 }
Andreas Gampe57c47582015-07-01 22:05:59 -07001144 // Make sure the switch data is marked as an opcode. This ensures that it was reached when
1145 // traversing the code item linearly. It is an approximation for a by-spec padding value.
1146 if (!insn_flags_[cur_offset + switch_offset].IsOpcode()) {
1147 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "switch table at " << cur_offset
1148 << ", switch offset " << switch_offset
1149 << " not correctly visited, probably bad padding.";
1150 return false;
1151 }
1152
Ian Rogersd81871c2011-10-03 13:57:23 -07001153 uint32_t switch_count = switch_insns[1];
1154 int32_t keys_offset, targets_offset;
1155 uint16_t expected_signature;
jeffhaoba5ebb92011-08-25 17:24:37 -07001156 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
1157 /* 0=sig, 1=count, 2/3=firstKey */
1158 targets_offset = 4;
1159 keys_offset = -1;
1160 expected_signature = Instruction::kPackedSwitchSignature;
1161 } else {
1162 /* 0=sig, 1=count, 2..count*2 = keys */
1163 keys_offset = 2;
1164 targets_offset = 2 + 2 * switch_count;
1165 expected_signature = Instruction::kSparseSwitchSignature;
1166 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001167 uint32_t table_size = targets_offset + switch_count * 2;
jeffhaoba5ebb92011-08-25 17:24:37 -07001168 if (switch_insns[0] != expected_signature) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001169 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1170 << StringPrintf("wrong signature for switch table (%x, wanted %x)",
1171 switch_insns[0], expected_signature);
jeffhaoba5ebb92011-08-25 17:24:37 -07001172 return false;
1173 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001174 /* make sure the end of the switch is in range */
1175 if (cur_offset + switch_offset + table_size > (uint32_t) insn_count) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001176 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch end: at " << cur_offset
1177 << ", switch offset " << switch_offset
1178 << ", end " << (cur_offset + switch_offset + table_size)
jeffhaod5347e02012-03-22 17:25:05 -07001179 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001180 return false;
1181 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001182 /* for a sparse switch, verify the keys are in ascending order */
1183 if (keys_offset > 0 && switch_count > 1) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001184 int32_t last_key = switch_insns[keys_offset] | (switch_insns[keys_offset + 1] << 16);
1185 for (uint32_t targ = 1; targ < switch_count; targ++) {
jeffhaoba5ebb92011-08-25 17:24:37 -07001186 int32_t key = (int32_t) switch_insns[keys_offset + targ * 2] |
1187 (int32_t) (switch_insns[keys_offset + targ * 2 + 1] << 16);
1188 if (key <= last_key) {
jeffhaod5347e02012-03-22 17:25:05 -07001189 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid packed switch: last key=" << last_key
1190 << ", this=" << key;
jeffhaoba5ebb92011-08-25 17:24:37 -07001191 return false;
1192 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001193 last_key = key;
1194 }
1195 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001196 /* verify each switch target */
Ian Rogersd81871c2011-10-03 13:57:23 -07001197 for (uint32_t targ = 0; targ < switch_count; targ++) {
1198 int32_t offset = (int32_t) switch_insns[targets_offset + targ * 2] |
1199 (int32_t) (switch_insns[targets_offset + targ * 2 + 1] << 16);
1200 int32_t abs_offset = cur_offset + offset;
Brian Carlstrom93c33962013-07-26 10:37:43 -07001201 if (abs_offset < 0 ||
1202 abs_offset >= (int32_t) insn_count ||
1203 !insn_flags_[abs_offset].IsOpcode()) {
1204 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch target " << offset
1205 << " (-> " << reinterpret_cast<void*>(abs_offset) << ") at "
1206 << reinterpret_cast<void*>(cur_offset)
1207 << "[" << targ << "]";
jeffhaoba5ebb92011-08-25 17:24:37 -07001208 return false;
1209 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001210 insn_flags_[abs_offset].SetBranchTarget();
1211 }
1212 return true;
1213}
1214
Ian Rogers776ac1f2012-04-13 23:36:36 -07001215bool MethodVerifier::CheckVarArgRegs(uint32_t vA, uint32_t arg[]) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001216 uint16_t registers_size = code_item_->registers_size_;
1217 for (uint32_t idx = 0; idx < vA; idx++) {
jeffhao457cc512012-02-02 16:55:13 -08001218 if (arg[idx] >= registers_size) {
jeffhaod5347e02012-03-22 17:25:05 -07001219 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index (" << arg[idx]
1220 << ") in non-range invoke (>= " << registers_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001221 return false;
1222 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001223 }
1224
1225 return true;
1226}
1227
Ian Rogers776ac1f2012-04-13 23:36:36 -07001228bool MethodVerifier::CheckVarArgRangeRegs(uint32_t vA, uint32_t vC) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001229 uint16_t registers_size = code_item_->registers_size_;
1230 // vA/vC are unsigned 8-bit/16-bit quantities for /range instructions, so there's no risk of
1231 // integer overflow when adding them here.
1232 if (vA + vC > registers_size) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001233 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index " << vA << "+" << vC
1234 << " in range invoke (> " << registers_size << ")";
jeffhaoba5ebb92011-08-25 17:24:37 -07001235 return false;
1236 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001237 return true;
1238}
1239
Ian Rogers776ac1f2012-04-13 23:36:36 -07001240bool MethodVerifier::VerifyCodeFlow() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001241 uint16_t registers_size = code_item_->registers_size_;
1242 uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaobdb76512011-09-07 11:43:16 -07001243
Ian Rogersd81871c2011-10-03 13:57:23 -07001244 /* Create and initialize table holding register status */
Brian Carlstrom93c33962013-07-26 10:37:43 -07001245 reg_table_.Init(kTrackCompilerInterestPoints,
1246 insn_flags_.get(),
1247 insns_size,
1248 registers_size,
1249 this);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07001250
jeffhaobdb76512011-09-07 11:43:16 -07001251
Ian Rogersd0fbd852013-09-24 18:17:04 -07001252 work_line_.reset(RegisterLine::Create(registers_size, this));
1253 saved_line_.reset(RegisterLine::Create(registers_size, this));
jeffhaobdb76512011-09-07 11:43:16 -07001254
Ian Rogersd81871c2011-10-03 13:57:23 -07001255 /* Initialize register types of method arguments. */
1256 if (!SetTypesFromSignature()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001257 DCHECK_NE(failures_.size(), 0U);
1258 std::string prepend("Bad signature in ");
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001259 prepend += PrettyMethod(dex_method_idx_, *dex_file_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001260 PrependToLastFailMessage(prepend);
Ian Rogersd81871c2011-10-03 13:57:23 -07001261 return false;
1262 }
Andreas Gamped5ad72f2015-06-26 17:33:47 -07001263 // We may have a runtime failure here, clear.
1264 have_pending_runtime_throw_failure_ = false;
1265
Ian Rogersd81871c2011-10-03 13:57:23 -07001266 /* Perform code flow verification. */
1267 if (!CodeFlowVerifyMethod()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001268 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -07001269 return false;
jeffhaobdb76512011-09-07 11:43:16 -07001270 }
jeffhaobdb76512011-09-07 11:43:16 -07001271 return true;
1272}
1273
Ian Rogersad0b3a32012-04-16 14:50:24 -07001274std::ostream& MethodVerifier::DumpFailures(std::ostream& os) {
1275 DCHECK_EQ(failures_.size(), failure_messages_.size());
Jeff Hao4137f482013-11-22 11:44:57 -08001276 for (size_t i = 0; i < failures_.size(); ++i) {
1277 os << failure_messages_[i]->str() << "\n";
Ian Rogersad0b3a32012-04-16 14:50:24 -07001278 }
1279 return os;
1280}
1281
Ian Rogers776ac1f2012-04-13 23:36:36 -07001282void MethodVerifier::Dump(std::ostream& os) {
Ian Rogers7b078e82014-09-10 14:44:24 -07001283 if (code_item_ == nullptr) {
Elliott Hughesc073b072012-05-24 19:29:17 -07001284 os << "Native method\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001285 return;
jeffhaobdb76512011-09-07 11:43:16 -07001286 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001287 {
1288 os << "Register Types:\n";
1289 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
1290 std::ostream indent_os(&indent_filter);
1291 reg_types_.Dump(indent_os);
1292 }
Ian Rogersb4903572012-10-11 11:52:56 -07001293 os << "Dumping instructions and register lines:\n";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001294 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
1295 std::ostream indent_os(&indent_filter);
Ian Rogersd81871c2011-10-03 13:57:23 -07001296 const Instruction* inst = Instruction::At(code_item_->insns_);
1297 for (size_t dex_pc = 0; dex_pc < code_item_->insns_size_in_code_units_;
Ian Rogers7b078e82014-09-10 14:44:24 -07001298 dex_pc += inst->SizeInCodeUnits()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001299 RegisterLine* reg_line = reg_table_.GetLine(dex_pc);
Ian Rogers7b078e82014-09-10 14:44:24 -07001300 if (reg_line != nullptr) {
1301 indent_os << reg_line->Dump(this) << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07001302 }
Ian Rogers7b3ddd22013-02-21 15:19:52 -08001303 indent_os << StringPrintf("0x%04zx", dex_pc) << ": " << insn_flags_[dex_pc].ToString() << " ";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001304 const bool kDumpHexOfInstruction = false;
1305 if (kDumpHexOfInstruction) {
1306 indent_os << inst->DumpHex(5) << " ";
1307 }
1308 indent_os << inst->DumpString(dex_file_) << "\n";
jeffhaoba5ebb92011-08-25 17:24:37 -07001309 inst = inst->Next();
1310 }
jeffhaobdb76512011-09-07 11:43:16 -07001311}
1312
Ian Rogersd81871c2011-10-03 13:57:23 -07001313static bool IsPrimitiveDescriptor(char descriptor) {
1314 switch (descriptor) {
jeffhaobdb76512011-09-07 11:43:16 -07001315 case 'I':
1316 case 'C':
1317 case 'S':
1318 case 'B':
1319 case 'Z':
jeffhaobdb76512011-09-07 11:43:16 -07001320 case 'F':
1321 case 'D':
1322 case 'J':
Ian Rogersd81871c2011-10-03 13:57:23 -07001323 return true;
jeffhaobdb76512011-09-07 11:43:16 -07001324 default:
1325 return false;
1326 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001327}
1328
Ian Rogers776ac1f2012-04-13 23:36:36 -07001329bool MethodVerifier::SetTypesFromSignature() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001330 RegisterLine* reg_line = reg_table_.GetLine(0);
Andreas Gampeef0b1a12015-06-19 20:37:46 -07001331
1332 // Should have been verified earlier.
1333 DCHECK_GE(code_item_->registers_size_, code_item_->ins_size_);
1334
1335 uint32_t arg_start = code_item_->registers_size_ - code_item_->ins_size_;
Ian Rogersd81871c2011-10-03 13:57:23 -07001336 size_t expected_args = code_item_->ins_size_; /* long/double count as two */
jeffhaobdb76512011-09-07 11:43:16 -07001337
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001338 // Include the "this" pointer.
Ian Rogersd81871c2011-10-03 13:57:23 -07001339 size_t cur_arg = 0;
Ian Rogersad0b3a32012-04-16 14:50:24 -07001340 if (!IsStatic()) {
Andreas Gampeef0b1a12015-06-19 20:37:46 -07001341 if (expected_args == 0) {
1342 // Expect at least a receiver.
1343 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected 0 args, but method is not static";
1344 return false;
1345 }
1346
Ian Rogersd81871c2011-10-03 13:57:23 -07001347 // If this is a constructor for a class other than java.lang.Object, mark the first ("this")
1348 // argument as uninitialized. This restricts field access until the superclass constructor is
1349 // called.
Ian Rogersd8f69b02014-09-10 21:43:52 +00001350 const RegType& declaring_class = GetDeclaringClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07001351 if (IsConstructor() && !declaring_class.IsJavaLangObject()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07001352 reg_line->SetRegisterType(this, arg_start + cur_arg,
Ian Rogersd81871c2011-10-03 13:57:23 -07001353 reg_types_.UninitializedThisArgument(declaring_class));
1354 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07001355 reg_line->SetRegisterType(this, arg_start + cur_arg, declaring_class);
jeffhaobdb76512011-09-07 11:43:16 -07001356 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001357 cur_arg++;
jeffhaobdb76512011-09-07 11:43:16 -07001358 }
1359
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001360 const DexFile::ProtoId& proto_id =
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001361 dex_file_->GetMethodPrototype(dex_file_->GetMethodId(dex_method_idx_));
Ian Rogers0571d352011-11-03 19:51:38 -07001362 DexFileParameterIterator iterator(*dex_file_, proto_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07001363
1364 for (; iterator.HasNext(); iterator.Next()) {
1365 const char* descriptor = iterator.GetDescriptor();
Ian Rogers7b078e82014-09-10 14:44:24 -07001366 if (descriptor == nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001367 LOG(FATAL) << "Null descriptor";
1368 }
1369 if (cur_arg >= expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07001370 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1371 << " args, found more (" << descriptor << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001372 return false;
1373 }
1374 switch (descriptor[0]) {
1375 case 'L':
1376 case '[':
1377 // We assume that reference arguments are initialized. The only way it could be otherwise
1378 // (assuming the caller was verified) is if the current method is <init>, but in that case
1379 // it's effectively considered initialized the instant we reach here (in the sense that we
1380 // can return without doing anything or call virtual methods).
1381 {
Ian Rogersd8f69b02014-09-10 21:43:52 +00001382 const RegType& reg_type = ResolveClassAndCheckAccess(iterator.GetTypeIdx());
Sebastien Hertz2ed76f92014-04-22 17:11:08 +02001383 if (!reg_type.IsNonZeroReferenceTypes()) {
1384 DCHECK(HasFailures());
1385 return false;
1386 }
Ian Rogers7b078e82014-09-10 14:44:24 -07001387 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001388 }
1389 break;
1390 case 'Z':
Ian Rogers7b078e82014-09-10 14:44:24 -07001391 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_types_.Boolean());
Ian Rogersd81871c2011-10-03 13:57:23 -07001392 break;
1393 case 'C':
Ian Rogers7b078e82014-09-10 14:44:24 -07001394 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_types_.Char());
Ian Rogersd81871c2011-10-03 13:57:23 -07001395 break;
1396 case 'B':
Ian Rogers7b078e82014-09-10 14:44:24 -07001397 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_types_.Byte());
Ian Rogersd81871c2011-10-03 13:57:23 -07001398 break;
1399 case 'I':
Ian Rogers7b078e82014-09-10 14:44:24 -07001400 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_types_.Integer());
Ian Rogersd81871c2011-10-03 13:57:23 -07001401 break;
1402 case 'S':
Ian Rogers7b078e82014-09-10 14:44:24 -07001403 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_types_.Short());
Ian Rogersd81871c2011-10-03 13:57:23 -07001404 break;
1405 case 'F':
Ian Rogers7b078e82014-09-10 14:44:24 -07001406 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_types_.Float());
Ian Rogersd81871c2011-10-03 13:57:23 -07001407 break;
1408 case 'J':
1409 case 'D': {
Andreas Gampe77cd4d62014-06-19 17:29:48 -07001410 if (cur_arg + 1 >= expected_args) {
1411 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1412 << " args, found more (" << descriptor << ")";
1413 return false;
1414 }
1415
Ian Rogers7b078e82014-09-10 14:44:24 -07001416 const RegType* lo_half;
1417 const RegType* hi_half;
1418 if (descriptor[0] == 'J') {
1419 lo_half = &reg_types_.LongLo();
1420 hi_half = &reg_types_.LongHi();
1421 } else {
1422 lo_half = &reg_types_.DoubleLo();
1423 hi_half = &reg_types_.DoubleHi();
1424 }
1425 reg_line->SetRegisterTypeWide(this, arg_start + cur_arg, *lo_half, *hi_half);
Ian Rogersd81871c2011-10-03 13:57:23 -07001426 cur_arg++;
1427 break;
1428 }
1429 default:
Brian Carlstrom93c33962013-07-26 10:37:43 -07001430 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected signature type char '"
1431 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001432 return false;
1433 }
1434 cur_arg++;
1435 }
1436 if (cur_arg != expected_args) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001437 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1438 << " arguments, found " << cur_arg;
Ian Rogersd81871c2011-10-03 13:57:23 -07001439 return false;
1440 }
1441 const char* descriptor = dex_file_->GetReturnTypeDescriptor(proto_id);
1442 // Validate return type. We don't do the type lookup; just want to make sure that it has the right
1443 // format. Only major difference from the method argument format is that 'V' is supported.
1444 bool result;
1445 if (IsPrimitiveDescriptor(descriptor[0]) || descriptor[0] == 'V') {
1446 result = descriptor[1] == '\0';
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001447 } else if (descriptor[0] == '[') { // single/multi-dimensional array of object/primitive
Ian Rogersd81871c2011-10-03 13:57:23 -07001448 size_t i = 0;
1449 do {
1450 i++;
1451 } while (descriptor[i] == '['); // process leading [
1452 if (descriptor[i] == 'L') { // object array
1453 do {
1454 i++; // find closing ;
1455 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1456 result = descriptor[i] == ';';
1457 } else { // primitive array
1458 result = IsPrimitiveDescriptor(descriptor[i]) && descriptor[i + 1] == '\0';
1459 }
1460 } else if (descriptor[0] == 'L') {
1461 // could be more thorough here, but shouldn't be required
1462 size_t i = 0;
1463 do {
1464 i++;
1465 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1466 result = descriptor[i] == ';';
1467 } else {
1468 result = false;
1469 }
1470 if (!result) {
jeffhaod5347e02012-03-22 17:25:05 -07001471 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected char in return type descriptor '"
1472 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001473 }
1474 return result;
jeffhaobdb76512011-09-07 11:43:16 -07001475}
1476
Ian Rogers776ac1f2012-04-13 23:36:36 -07001477bool MethodVerifier::CodeFlowVerifyMethod() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001478 const uint16_t* insns = code_item_->insns_;
1479 const uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaoba5ebb92011-08-25 17:24:37 -07001480
jeffhaobdb76512011-09-07 11:43:16 -07001481 /* Begin by marking the first instruction as "changed". */
Ian Rogersd81871c2011-10-03 13:57:23 -07001482 insn_flags_[0].SetChanged();
1483 uint32_t start_guess = 0;
jeffhaoba5ebb92011-08-25 17:24:37 -07001484
jeffhaobdb76512011-09-07 11:43:16 -07001485 /* Continue until no instructions are marked "changed". */
1486 while (true) {
Mathieu Chartier4306ef82014-12-19 18:41:47 -08001487 if (allow_thread_suspension_) {
1488 self_->AllowThreadSuspension();
1489 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001490 // Find the first marked one. Use "start_guess" as a way to find one quickly.
1491 uint32_t insn_idx = start_guess;
1492 for (; insn_idx < insns_size; insn_idx++) {
1493 if (insn_flags_[insn_idx].IsChanged())
jeffhaobdb76512011-09-07 11:43:16 -07001494 break;
1495 }
jeffhaobdb76512011-09-07 11:43:16 -07001496 if (insn_idx == insns_size) {
1497 if (start_guess != 0) {
1498 /* try again, starting from the top */
1499 start_guess = 0;
1500 continue;
1501 } else {
1502 /* all flags are clear */
1503 break;
1504 }
1505 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001506 // We carry the working set of registers from instruction to instruction. If this address can
1507 // be the target of a branch (or throw) instruction, or if we're skipping around chasing
1508 // "changed" flags, we need to load the set of registers from the table.
1509 // Because we always prefer to continue on to the next instruction, we should never have a
1510 // situation where we have a stray "changed" flag set on an instruction that isn't a branch
1511 // target.
1512 work_insn_idx_ = insn_idx;
1513 if (insn_flags_[insn_idx].IsBranchTarget()) {
1514 work_line_->CopyFromLine(reg_table_.GetLine(insn_idx));
Ian Rogersebbdd872014-07-07 23:53:08 -07001515 } else if (kIsDebugBuild) {
jeffhaobdb76512011-09-07 11:43:16 -07001516 /*
1517 * Sanity check: retrieve the stored register line (assuming
1518 * a full table) and make sure it actually matches.
1519 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001520 RegisterLine* register_line = reg_table_.GetLine(insn_idx);
Ian Rogers7b078e82014-09-10 14:44:24 -07001521 if (register_line != nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001522 if (work_line_->CompareLine(register_line) != 0) {
1523 Dump(std::cout);
1524 std::cout << info_messages_.str();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001525 LOG(FATAL) << "work_line diverged in " << PrettyMethod(dex_method_idx_, *dex_file_)
Elliott Hughesc073b072012-05-24 19:29:17 -07001526 << "@" << reinterpret_cast<void*>(work_insn_idx_) << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07001527 << " work_line=" << work_line_->Dump(this) << "\n"
1528 << " expected=" << register_line->Dump(this);
Ian Rogersd81871c2011-10-03 13:57:23 -07001529 }
jeffhaobdb76512011-09-07 11:43:16 -07001530 }
jeffhaobdb76512011-09-07 11:43:16 -07001531 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001532 if (!CodeFlowVerifyInstruction(&start_guess)) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001533 std::string prepend(PrettyMethod(dex_method_idx_, *dex_file_));
Ian Rogersad0b3a32012-04-16 14:50:24 -07001534 prepend += " failed to verify: ";
1535 PrependToLastFailMessage(prepend);
jeffhaoba5ebb92011-08-25 17:24:37 -07001536 return false;
1537 }
jeffhaobdb76512011-09-07 11:43:16 -07001538 /* Clear "changed" and mark as visited. */
Ian Rogersd81871c2011-10-03 13:57:23 -07001539 insn_flags_[insn_idx].SetVisited();
1540 insn_flags_[insn_idx].ClearChanged();
jeffhaobdb76512011-09-07 11:43:16 -07001541 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001542
Ian Rogers1c849e52012-06-28 14:00:33 -07001543 if (gDebugVerify) {
jeffhaobdb76512011-09-07 11:43:16 -07001544 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001545 * Scan for dead code. There's nothing "evil" about dead code
jeffhaobdb76512011-09-07 11:43:16 -07001546 * (besides the wasted space), but it indicates a flaw somewhere
1547 * down the line, possibly in the verifier.
1548 *
1549 * If we've substituted "always throw" instructions into the stream,
1550 * we are almost certainly going to have some dead code.
1551 */
1552 int dead_start = -1;
Ian Rogersd81871c2011-10-03 13:57:23 -07001553 uint32_t insn_idx = 0;
Ian Rogers7b078e82014-09-10 14:44:24 -07001554 for (; insn_idx < insns_size;
1555 insn_idx += Instruction::At(code_item_->insns_ + insn_idx)->SizeInCodeUnits()) {
jeffhaobdb76512011-09-07 11:43:16 -07001556 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001557 * Switch-statement data doesn't get "visited" by scanner. It
jeffhaobdb76512011-09-07 11:43:16 -07001558 * may or may not be preceded by a padding NOP (for alignment).
1559 */
1560 if (insns[insn_idx] == Instruction::kPackedSwitchSignature ||
1561 insns[insn_idx] == Instruction::kSparseSwitchSignature ||
1562 insns[insn_idx] == Instruction::kArrayDataSignature ||
Elliott Hughes380aaa72012-07-09 14:33:15 -07001563 (insns[insn_idx] == Instruction::NOP && (insn_idx + 1 < insns_size) &&
jeffhaobdb76512011-09-07 11:43:16 -07001564 (insns[insn_idx + 1] == Instruction::kPackedSwitchSignature ||
1565 insns[insn_idx + 1] == Instruction::kSparseSwitchSignature ||
1566 insns[insn_idx + 1] == Instruction::kArrayDataSignature))) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001567 insn_flags_[insn_idx].SetVisited();
jeffhaobdb76512011-09-07 11:43:16 -07001568 }
1569
Ian Rogersd81871c2011-10-03 13:57:23 -07001570 if (!insn_flags_[insn_idx].IsVisited()) {
jeffhaobdb76512011-09-07 11:43:16 -07001571 if (dead_start < 0)
1572 dead_start = insn_idx;
1573 } else if (dead_start >= 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001574 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start)
1575 << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaobdb76512011-09-07 11:43:16 -07001576 dead_start = -1;
1577 }
1578 }
1579 if (dead_start >= 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001580 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start)
1581 << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaoba5ebb92011-08-25 17:24:37 -07001582 }
Ian Rogersc9e463c2013-06-05 16:52:26 -07001583 // To dump the state of the verify after a method, do something like:
1584 // if (PrettyMethod(dex_method_idx_, *dex_file_) ==
1585 // "boolean java.lang.String.equals(java.lang.Object)") {
1586 // LOG(INFO) << info_messages_.str();
1587 // }
jeffhaoba5ebb92011-08-25 17:24:37 -07001588 }
jeffhaobdb76512011-09-07 11:43:16 -07001589 return true;
1590}
1591
Andreas Gampe68df3202015-06-22 11:35:46 -07001592// Returns the index of the first final instance field of the given class, or kDexNoIndex if there
1593// is no such field.
1594static uint32_t GetFirstFinalInstanceFieldIndex(const DexFile& dex_file, uint16_t type_idx) {
1595 const DexFile::ClassDef* class_def = dex_file.FindClassDef(type_idx);
1596 DCHECK(class_def != nullptr);
1597 const uint8_t* class_data = dex_file.GetClassData(*class_def);
1598 DCHECK(class_data != nullptr);
1599 ClassDataItemIterator it(dex_file, class_data);
1600 // Skip static fields.
1601 while (it.HasNextStaticField()) {
1602 it.Next();
1603 }
1604 while (it.HasNextInstanceField()) {
1605 if ((it.GetFieldAccessFlags() & kAccFinal) != 0) {
1606 return it.GetMemberIndex();
1607 }
1608 it.Next();
1609 }
1610 return DexFile::kDexNoIndex;
1611}
1612
Ian Rogers776ac1f2012-04-13 23:36:36 -07001613bool MethodVerifier::CodeFlowVerifyInstruction(uint32_t* start_guess) {
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001614 // If we're doing FindLocksAtDexPc, check whether we're at the dex pc we care about.
1615 // We want the state _before_ the instruction, for the case where the dex pc we're
1616 // interested in is itself a monitor-enter instruction (which is a likely place
1617 // for a thread to be suspended).
Ian Rogers7b078e82014-09-10 14:44:24 -07001618 if (monitor_enter_dex_pcs_ != nullptr && work_insn_idx_ == interesting_dex_pc_) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001619 monitor_enter_dex_pcs_->clear(); // The new work line is more accurate than the previous one.
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001620 for (size_t i = 0; i < work_line_->GetMonitorEnterCount(); ++i) {
1621 monitor_enter_dex_pcs_->push_back(work_line_->GetMonitorEnterDexPc(i));
1622 }
1623 }
1624
jeffhaobdb76512011-09-07 11:43:16 -07001625 /*
1626 * Once we finish decoding the instruction, we need to figure out where
jeffhaod1f0fde2011-09-08 17:25:33 -07001627 * we can go from here. There are three possible ways to transfer
jeffhaobdb76512011-09-07 11:43:16 -07001628 * control to another statement:
1629 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001630 * (1) Continue to the next instruction. Applies to all but
jeffhaobdb76512011-09-07 11:43:16 -07001631 * unconditional branches, method returns, and exception throws.
jeffhaod1f0fde2011-09-08 17:25:33 -07001632 * (2) Branch to one or more possible locations. Applies to branches
jeffhaobdb76512011-09-07 11:43:16 -07001633 * and switch statements.
jeffhaod1f0fde2011-09-08 17:25:33 -07001634 * (3) Exception handlers. Applies to any instruction that can
jeffhaobdb76512011-09-07 11:43:16 -07001635 * throw an exception that is handled by an encompassing "try"
1636 * block.
1637 *
1638 * We can also return, in which case there is no successor instruction
1639 * from this point.
1640 *
Elliott Hughesadb8c672012-03-06 16:49:32 -08001641 * The behavior can be determined from the opcode flags.
jeffhaobdb76512011-09-07 11:43:16 -07001642 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001643 const uint16_t* insns = code_item_->insns_ + work_insn_idx_;
1644 const Instruction* inst = Instruction::At(insns);
Ian Rogersa75a0132012-09-28 11:41:42 -07001645 int opcode_flags = Instruction::FlagsOf(inst->Opcode());
jeffhaobdb76512011-09-07 11:43:16 -07001646
jeffhaobdb76512011-09-07 11:43:16 -07001647 int32_t branch_target = 0;
jeffhaobdb76512011-09-07 11:43:16 -07001648 bool just_set_result = false;
Ian Rogers2c8a8572011-10-24 17:11:36 -07001649 if (gDebugVerify) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001650 // Generate processing back trace to debug verifier
Elliott Hughesc073b072012-05-24 19:29:17 -07001651 LogVerifyInfo() << "Processing " << inst->DumpString(dex_file_) << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07001652 << work_line_->Dump(this) << "\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001653 }
jeffhaobdb76512011-09-07 11:43:16 -07001654
1655 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001656 * Make a copy of the previous register state. If the instruction
jeffhaobdb76512011-09-07 11:43:16 -07001657 * can throw an exception, we will copy/merge this into the "catch"
1658 * address rather than work_line, because we don't want the result
1659 * from the "successful" code path (e.g. a check-cast that "improves"
1660 * a type) to be visible to the exception handler.
1661 */
Ian Rogers776ac1f2012-04-13 23:36:36 -07001662 if ((opcode_flags & Instruction::kThrow) != 0 && CurrentInsnFlags()->IsInTry()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001663 saved_line_->CopyFromLine(work_line_.get());
Ian Rogers1ff3c982014-08-12 02:30:58 -07001664 } else if (kIsDebugBuild) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001665 saved_line_->FillWithGarbage();
jeffhaobdb76512011-09-07 11:43:16 -07001666 }
Andreas Gamped12e7822015-06-25 10:26:40 -07001667 DCHECK(!have_pending_runtime_throw_failure_); // Per-instruction flag, should not be set here.
jeffhaobdb76512011-09-07 11:43:16 -07001668
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07001669
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001670 // We need to ensure the work line is consistent while performing validation. When we spot a
1671 // peephole pattern we compute a new line for either the fallthrough instruction or the
1672 // branch target.
Ian Rogers700a4022014-05-19 16:49:03 -07001673 std::unique_ptr<RegisterLine> branch_line;
1674 std::unique_ptr<RegisterLine> fallthrough_line;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001675
Stephen Kyle7e541c92014-12-17 17:10:02 +00001676 /*
1677 * If we are in a constructor, and we currently have an UninitializedThis type
1678 * in a register somewhere, we need to make sure it isn't overwritten.
1679 */
1680 bool track_uninitialized_this = false;
1681 size_t uninitialized_this_loc = 0;
1682 if (IsConstructor()) {
1683 track_uninitialized_this = work_line_->GetUninitializedThisLoc(this, &uninitialized_this_loc);
1684 }
1685
Sebastien Hertz5243e912013-05-21 10:55:07 +02001686 switch (inst->Opcode()) {
jeffhaobdb76512011-09-07 11:43:16 -07001687 case Instruction::NOP:
1688 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001689 * A "pure" NOP has no effect on anything. Data tables start with
jeffhaobdb76512011-09-07 11:43:16 -07001690 * a signature that looks like a NOP; if we see one of these in
1691 * the course of executing code then we have a problem.
1692 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001693 if (inst->VRegA_10x() != 0) {
jeffhaod5347e02012-03-22 17:25:05 -07001694 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "encountered data table in instruction stream";
jeffhaobdb76512011-09-07 11:43:16 -07001695 }
1696 break;
1697
1698 case Instruction::MOVE:
Ian Rogers7b078e82014-09-10 14:44:24 -07001699 work_line_->CopyRegister1(this, inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategory1nr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001700 break;
jeffhaobdb76512011-09-07 11:43:16 -07001701 case Instruction::MOVE_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001702 work_line_->CopyRegister1(this, inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategory1nr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001703 break;
jeffhaobdb76512011-09-07 11:43:16 -07001704 case Instruction::MOVE_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001705 work_line_->CopyRegister1(this, inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategory1nr);
jeffhaobdb76512011-09-07 11:43:16 -07001706 break;
1707 case Instruction::MOVE_WIDE:
Ian Rogers7b078e82014-09-10 14:44:24 -07001708 work_line_->CopyRegister2(this, inst->VRegA_12x(), inst->VRegB_12x());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001709 break;
jeffhaobdb76512011-09-07 11:43:16 -07001710 case Instruction::MOVE_WIDE_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001711 work_line_->CopyRegister2(this, inst->VRegA_22x(), inst->VRegB_22x());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001712 break;
jeffhaobdb76512011-09-07 11:43:16 -07001713 case Instruction::MOVE_WIDE_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001714 work_line_->CopyRegister2(this, inst->VRegA_32x(), inst->VRegB_32x());
jeffhaobdb76512011-09-07 11:43:16 -07001715 break;
1716 case Instruction::MOVE_OBJECT:
Ian Rogers7b078e82014-09-10 14:44:24 -07001717 work_line_->CopyRegister1(this, inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategoryRef);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001718 break;
jeffhaobdb76512011-09-07 11:43:16 -07001719 case Instruction::MOVE_OBJECT_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001720 work_line_->CopyRegister1(this, inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategoryRef);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001721 break;
jeffhaobdb76512011-09-07 11:43:16 -07001722 case Instruction::MOVE_OBJECT_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001723 work_line_->CopyRegister1(this, inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategoryRef);
jeffhaobdb76512011-09-07 11:43:16 -07001724 break;
1725
1726 /*
1727 * The move-result instructions copy data out of a "pseudo-register"
jeffhaod1f0fde2011-09-08 17:25:33 -07001728 * with the results from the last method invocation. In practice we
jeffhaobdb76512011-09-07 11:43:16 -07001729 * might want to hold the result in an actual CPU register, so the
1730 * Dalvik spec requires that these only appear immediately after an
1731 * invoke or filled-new-array.
1732 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001733 * These calls invalidate the "result" register. (This is now
jeffhaobdb76512011-09-07 11:43:16 -07001734 * redundant with the reset done below, but it can make the debug info
1735 * easier to read in some cases.)
1736 */
1737 case Instruction::MOVE_RESULT:
Ian Rogers7b078e82014-09-10 14:44:24 -07001738 work_line_->CopyResultRegister1(this, inst->VRegA_11x(), false);
jeffhaobdb76512011-09-07 11:43:16 -07001739 break;
1740 case Instruction::MOVE_RESULT_WIDE:
Ian Rogers7b078e82014-09-10 14:44:24 -07001741 work_line_->CopyResultRegister2(this, inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07001742 break;
1743 case Instruction::MOVE_RESULT_OBJECT:
Ian Rogers7b078e82014-09-10 14:44:24 -07001744 work_line_->CopyResultRegister1(this, inst->VRegA_11x(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001745 break;
1746
Ian Rogersd81871c2011-10-03 13:57:23 -07001747 case Instruction::MOVE_EXCEPTION: {
Sebastien Hertz270a0e12015-01-16 19:49:09 +01001748 // We do not allow MOVE_EXCEPTION as the first instruction in a method. This is a simple case
1749 // where one entrypoint to the catch block is not actually an exception path.
1750 if (work_insn_idx_ == 0) {
1751 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "move-exception at pc 0x0";
1752 break;
1753 }
jeffhaobdb76512011-09-07 11:43:16 -07001754 /*
jeffhao60f83e32012-02-13 17:16:30 -08001755 * This statement can only appear as the first instruction in an exception handler. We verify
1756 * that as part of extracting the exception type from the catch block list.
jeffhaobdb76512011-09-07 11:43:16 -07001757 */
Ian Rogersd8f69b02014-09-10 21:43:52 +00001758 const RegType& res_type = GetCaughtExceptionType();
Ian Rogers7b078e82014-09-10 14:44:24 -07001759 work_line_->SetRegisterType(this, inst->VRegA_11x(), res_type);
jeffhaobdb76512011-09-07 11:43:16 -07001760 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001761 }
jeffhaobdb76512011-09-07 11:43:16 -07001762 case Instruction::RETURN_VOID:
Ian Rogers7b078e82014-09-10 14:44:24 -07001763 if (!IsConstructor() || work_line_->CheckConstructorReturn(this)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001764 if (!GetMethodReturnType().IsConflict()) {
jeffhaod5347e02012-03-22 17:25:05 -07001765 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001766 }
jeffhaobdb76512011-09-07 11:43:16 -07001767 }
1768 break;
1769 case Instruction::RETURN:
Ian Rogers7b078e82014-09-10 14:44:24 -07001770 if (!IsConstructor() || work_line_->CheckConstructorReturn(this)) {
jeffhaobdb76512011-09-07 11:43:16 -07001771 /* check the method signature */
Ian Rogersd8f69b02014-09-10 21:43:52 +00001772 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07001773 if (!return_type.IsCategory1Types()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001774 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected non-category 1 return type "
1775 << return_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001776 } else {
1777 // Compilers may generate synthetic functions that write byte values into boolean fields.
1778 // Also, it may use integer values for boolean, byte, short, and character return types.
Sebastien Hertz5243e912013-05-21 10:55:07 +02001779 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07001780 const RegType& src_type = work_line_->GetRegisterType(this, vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07001781 bool use_src = ((return_type.IsBoolean() && src_type.IsByte()) ||
1782 ((return_type.IsBoolean() || return_type.IsByte() ||
1783 return_type.IsShort() || return_type.IsChar()) &&
1784 src_type.IsInteger()));
1785 /* check the register contents */
Ian Rogersad0b3a32012-04-16 14:50:24 -07001786 bool success =
Ian Rogers7b078e82014-09-10 14:44:24 -07001787 work_line_->VerifyRegisterType(this, vregA, use_src ? src_type : return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001788 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001789 AppendToLastFailMessage(StringPrintf(" return-1nr on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07001790 }
jeffhaobdb76512011-09-07 11:43:16 -07001791 }
1792 }
1793 break;
1794 case Instruction::RETURN_WIDE:
Ian Rogers7b078e82014-09-10 14:44:24 -07001795 if (!IsConstructor() || work_line_->CheckConstructorReturn(this)) {
jeffhaobdb76512011-09-07 11:43:16 -07001796 /* check the method signature */
Ian Rogersd8f69b02014-09-10 21:43:52 +00001797 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07001798 if (!return_type.IsCategory2Types()) {
jeffhaod5347e02012-03-22 17:25:05 -07001799 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-wide not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001800 } else {
1801 /* check the register contents */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001802 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07001803 bool success = work_line_->VerifyRegisterType(this, vregA, return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001804 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001805 AppendToLastFailMessage(StringPrintf(" return-wide on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07001806 }
jeffhaobdb76512011-09-07 11:43:16 -07001807 }
1808 }
1809 break;
1810 case Instruction::RETURN_OBJECT:
Ian Rogers7b078e82014-09-10 14:44:24 -07001811 if (!IsConstructor() || work_line_->CheckConstructorReturn(this)) {
Ian Rogersd8f69b02014-09-10 21:43:52 +00001812 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07001813 if (!return_type.IsReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001814 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-object not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001815 } else {
1816 /* return_type is the *expected* return type, not register value */
1817 DCHECK(!return_type.IsZero());
1818 DCHECK(!return_type.IsUninitializedReference());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001819 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07001820 const RegType& reg_type = work_line_->GetRegisterType(this, vregA);
Andreas Gampea32210c2015-06-24 10:26:13 -07001821 // Disallow returning undefined, conflict & uninitialized values and verify that the
1822 // reference in vAA is an instance of the "return_type."
1823 if (reg_type.IsUndefined()) {
1824 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning undefined register";
1825 } else if (reg_type.IsConflict()) {
1826 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning register with conflict";
1827 } else if (reg_type.IsUninitializedTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001828 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "returning uninitialized object '"
1829 << reg_type << "'";
Ian Rogers9074b992011-10-26 17:41:55 -07001830 } else if (!return_type.IsAssignableFrom(reg_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07001831 if (reg_type.IsUnresolvedTypes() || return_type.IsUnresolvedTypes()) {
1832 Fail(VERIFY_ERROR_NO_CLASS) << " can't resolve returned type '" << return_type
1833 << "' or '" << reg_type << "'";
1834 } else {
Andreas Gampe16f149c2015-03-23 10:10:20 -07001835 bool soft_error = false;
1836 // Check whether arrays are involved. They will show a valid class status, even
1837 // if their components are erroneous.
1838 if (reg_type.IsArrayTypes() && return_type.IsArrayTypes()) {
1839 return_type.CanAssignArray(reg_type, reg_types_, class_loader_, &soft_error);
1840 if (soft_error) {
1841 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "array with erroneous component type: "
1842 << reg_type << " vs " << return_type;
1843 }
1844 }
1845
1846 if (!soft_error) {
1847 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning '" << reg_type
1848 << "', but expected from declaration '" << return_type << "'";
1849 }
Jeff Haoa3faaf42013-09-03 19:07:00 -07001850 }
jeffhaobdb76512011-09-07 11:43:16 -07001851 }
1852 }
1853 }
1854 break;
1855
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001856 /* could be boolean, int, float, or a null reference */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001857 case Instruction::CONST_4: {
1858 int32_t val = static_cast<int32_t>(inst->VRegB_11n() << 28) >> 28;
Ian Rogers7b078e82014-09-10 14:44:24 -07001859 work_line_->SetRegisterType(this, inst->VRegA_11n(),
Ian Rogers46960fe2014-05-23 10:43:43 -07001860 DetermineCat1Constant(val, need_precise_constants_));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001861 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02001862 }
1863 case Instruction::CONST_16: {
1864 int16_t val = static_cast<int16_t>(inst->VRegB_21s());
Ian Rogers7b078e82014-09-10 14:44:24 -07001865 work_line_->SetRegisterType(this, inst->VRegA_21s(),
Ian Rogers46960fe2014-05-23 10:43:43 -07001866 DetermineCat1Constant(val, need_precise_constants_));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001867 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02001868 }
Sebastien Hertz849600b2013-12-20 10:28:08 +01001869 case Instruction::CONST: {
1870 int32_t val = inst->VRegB_31i();
Ian Rogers7b078e82014-09-10 14:44:24 -07001871 work_line_->SetRegisterType(this, inst->VRegA_31i(),
Ian Rogers46960fe2014-05-23 10:43:43 -07001872 DetermineCat1Constant(val, need_precise_constants_));
jeffhaobdb76512011-09-07 11:43:16 -07001873 break;
Sebastien Hertz849600b2013-12-20 10:28:08 +01001874 }
1875 case Instruction::CONST_HIGH16: {
1876 int32_t val = static_cast<int32_t>(inst->VRegB_21h() << 16);
Ian Rogers7b078e82014-09-10 14:44:24 -07001877 work_line_->SetRegisterType(this, inst->VRegA_21h(),
Ian Rogers46960fe2014-05-23 10:43:43 -07001878 DetermineCat1Constant(val, need_precise_constants_));
jeffhaobdb76512011-09-07 11:43:16 -07001879 break;
Sebastien Hertz849600b2013-12-20 10:28:08 +01001880 }
jeffhaobdb76512011-09-07 11:43:16 -07001881 /* could be long or double; resolved upon use */
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001882 case Instruction::CONST_WIDE_16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001883 int64_t val = static_cast<int16_t>(inst->VRegB_21s());
Ian Rogersd8f69b02014-09-10 21:43:52 +00001884 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1885 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07001886 work_line_->SetRegisterTypeWide(this, inst->VRegA_21s(), lo, hi);
jeffhaobdb76512011-09-07 11:43:16 -07001887 break;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001888 }
1889 case Instruction::CONST_WIDE_32: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001890 int64_t val = static_cast<int32_t>(inst->VRegB_31i());
Ian Rogersd8f69b02014-09-10 21:43:52 +00001891 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1892 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07001893 work_line_->SetRegisterTypeWide(this, inst->VRegA_31i(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001894 break;
1895 }
1896 case Instruction::CONST_WIDE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001897 int64_t val = inst->VRegB_51l();
Ian Rogersd8f69b02014-09-10 21:43:52 +00001898 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1899 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07001900 work_line_->SetRegisterTypeWide(this, inst->VRegA_51l(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001901 break;
1902 }
1903 case Instruction::CONST_WIDE_HIGH16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001904 int64_t val = static_cast<uint64_t>(inst->VRegB_21h()) << 48;
Ian Rogersd8f69b02014-09-10 21:43:52 +00001905 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1906 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07001907 work_line_->SetRegisterTypeWide(this, inst->VRegA_21h(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001908 break;
1909 }
jeffhaobdb76512011-09-07 11:43:16 -07001910 case Instruction::CONST_STRING:
Ian Rogers7b078e82014-09-10 14:44:24 -07001911 work_line_->SetRegisterType(this, inst->VRegA_21c(), reg_types_.JavaLangString());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001912 break;
jeffhaobdb76512011-09-07 11:43:16 -07001913 case Instruction::CONST_STRING_JUMBO:
Ian Rogers7b078e82014-09-10 14:44:24 -07001914 work_line_->SetRegisterType(this, inst->VRegA_31c(), reg_types_.JavaLangString());
jeffhaobdb76512011-09-07 11:43:16 -07001915 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001916 case Instruction::CONST_CLASS: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001917 // Get type from instruction if unresolved then we need an access check
1918 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Ian Rogersd8f69b02014-09-10 21:43:52 +00001919 const RegType& res_type = ResolveClassAndCheckAccess(inst->VRegB_21c());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001920 // Register holds class, ie its type is class, on error it will hold Conflict.
Ian Rogers7b078e82014-09-10 14:44:24 -07001921 work_line_->SetRegisterType(this, inst->VRegA_21c(),
Ian Rogersb4903572012-10-11 11:52:56 -07001922 res_type.IsConflict() ? res_type
Ian Rogers7b078e82014-09-10 14:44:24 -07001923 : reg_types_.JavaLangClass());
jeffhaobdb76512011-09-07 11:43:16 -07001924 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001925 }
jeffhaobdb76512011-09-07 11:43:16 -07001926 case Instruction::MONITOR_ENTER:
Ian Rogers7b078e82014-09-10 14:44:24 -07001927 work_line_->PushMonitor(this, inst->VRegA_11x(), work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07001928 break;
1929 case Instruction::MONITOR_EXIT:
1930 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001931 * monitor-exit instructions are odd. They can throw exceptions,
jeffhaobdb76512011-09-07 11:43:16 -07001932 * but when they do they act as if they succeeded and the PC is
jeffhaod1f0fde2011-09-08 17:25:33 -07001933 * pointing to the following instruction. (This behavior goes back
jeffhaobdb76512011-09-07 11:43:16 -07001934 * to the need to handle asynchronous exceptions, a now-deprecated
1935 * feature that Dalvik doesn't support.)
1936 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001937 * In practice we don't need to worry about this. The only
jeffhaobdb76512011-09-07 11:43:16 -07001938 * exceptions that can be thrown from monitor-exit are for a
jeffhaod1f0fde2011-09-08 17:25:33 -07001939 * null reference and -exit without a matching -enter. If the
jeffhaobdb76512011-09-07 11:43:16 -07001940 * structured locking checks are working, the former would have
1941 * failed on the -enter instruction, and the latter is impossible.
1942 *
1943 * This is fortunate, because issue 3221411 prevents us from
1944 * chasing the "can throw" path when monitor verification is
jeffhaod1f0fde2011-09-08 17:25:33 -07001945 * enabled. If we can fully verify the locking we can ignore
jeffhaobdb76512011-09-07 11:43:16 -07001946 * some catch blocks (which will show up as "dead" code when
1947 * we skip them here); if we can't, then the code path could be
1948 * "live" so we still need to check it.
1949 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001950 opcode_flags &= ~Instruction::kThrow;
Ian Rogers7b078e82014-09-10 14:44:24 -07001951 work_line_->PopMonitor(this, inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07001952 break;
1953
Ian Rogers28ad40d2011-10-27 15:19:26 -07001954 case Instruction::CHECK_CAST:
Ian Rogersd81871c2011-10-03 13:57:23 -07001955 case Instruction::INSTANCE_OF: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001956 /*
1957 * If this instruction succeeds, we will "downcast" register vA to the type in vB. (This
1958 * could be a "upcast" -- not expected, so we don't try to address it.)
1959 *
1960 * If it fails, an exception is thrown, which we deal with later by ignoring the update to
Elliott Hughesadb8c672012-03-06 16:49:32 -08001961 * dec_insn.vA when branching to a handler.
Ian Rogers28ad40d2011-10-27 15:19:26 -07001962 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001963 const bool is_checkcast = (inst->Opcode() == Instruction::CHECK_CAST);
1964 const uint32_t type_idx = (is_checkcast) ? inst->VRegB_21c() : inst->VRegC_22c();
Ian Rogersd8f69b02014-09-10 21:43:52 +00001965 const RegType& res_type = ResolveClassAndCheckAccess(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001966 if (res_type.IsConflict()) {
Andreas Gampe00633eb2014-07-17 16:13:35 -07001967 // If this is a primitive type, fail HARD.
Mathieu Chartierbf99f772014-08-23 16:37:27 -07001968 mirror::Class* klass = dex_cache_->GetResolvedType(type_idx);
Andreas Gampe00633eb2014-07-17 16:13:35 -07001969 if (klass != nullptr && klass->IsPrimitive()) {
1970 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "using primitive type "
1971 << dex_file_->StringByTypeIdx(type_idx) << " in instanceof in "
1972 << GetDeclaringClass();
1973 break;
1974 }
1975
Ian Rogersad0b3a32012-04-16 14:50:24 -07001976 DCHECK_NE(failures_.size(), 0U);
1977 if (!is_checkcast) {
Ian Rogers7b078e82014-09-10 14:44:24 -07001978 work_line_->SetRegisterType(this, inst->VRegA_22c(), reg_types_.Boolean());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001979 }
1980 break; // bad class
Ian Rogers9f1ab122011-12-12 08:52:43 -08001981 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001982 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Sebastien Hertz5243e912013-05-21 10:55:07 +02001983 uint32_t orig_type_reg = (is_checkcast) ? inst->VRegA_21c() : inst->VRegB_22c();
Ian Rogers7b078e82014-09-10 14:44:24 -07001984 const RegType& orig_type = work_line_->GetRegisterType(this, orig_type_reg);
Ian Rogers28ad40d2011-10-27 15:19:26 -07001985 if (!res_type.IsNonZeroReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001986 if (is_checkcast) {
1987 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on unexpected class " << res_type;
1988 } else {
1989 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on unexpected class " << res_type;
1990 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001991 } else if (!orig_type.IsReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001992 if (is_checkcast) {
1993 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on non-reference in v" << orig_type_reg;
1994 } else {
1995 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on non-reference in v" << orig_type_reg;
1996 }
jeffhao2a8a90e2011-09-26 14:25:31 -07001997 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001998 if (is_checkcast) {
Ian Rogers7b078e82014-09-10 14:44:24 -07001999 work_line_->SetRegisterType(this, inst->VRegA_21c(), res_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07002000 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07002001 work_line_->SetRegisterType(this, inst->VRegA_22c(), reg_types_.Boolean());
jeffhaobdb76512011-09-07 11:43:16 -07002002 }
jeffhaobdb76512011-09-07 11:43:16 -07002003 }
jeffhao2a8a90e2011-09-26 14:25:31 -07002004 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002005 }
2006 case Instruction::ARRAY_LENGTH: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002007 const RegType& res_type = work_line_->GetRegisterType(this, inst->VRegB_12x());
Ian Rogers28ad40d2011-10-27 15:19:26 -07002008 if (res_type.IsReferenceTypes()) {
Ian Rogers89310de2012-02-01 13:47:30 -08002009 if (!res_type.IsArrayTypes() && !res_type.IsZero()) { // ie not an array or null
jeffhaod5347e02012-03-22 17:25:05 -07002010 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002011 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07002012 work_line_->SetRegisterType(this, inst->VRegA_12x(), reg_types_.Integer());
Ian Rogersd81871c2011-10-03 13:57:23 -07002013 }
Andreas Gampe65c9db82014-07-28 13:14:34 -07002014 } else {
2015 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002016 }
2017 break;
2018 }
2019 case Instruction::NEW_INSTANCE: {
Ian Rogersd8f69b02014-09-10 21:43:52 +00002020 const RegType& res_type = ResolveClassAndCheckAccess(inst->VRegB_21c());
Ian Rogersad0b3a32012-04-16 14:50:24 -07002021 if (res_type.IsConflict()) {
2022 DCHECK_NE(failures_.size(), 0U);
2023 break; // bad class
jeffhao8cd6dda2012-02-22 10:15:34 -08002024 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002025 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
2026 // can't create an instance of an interface or abstract class */
2027 if (!res_type.IsInstantiableTypes()) {
2028 Fail(VERIFY_ERROR_INSTANTIATION)
2029 << "new-instance on primitive, interface or abstract class" << res_type;
Ian Rogers08f753d2012-08-24 14:35:25 -07002030 // Soft failure so carry on to set register type.
Ian Rogersd81871c2011-10-03 13:57:23 -07002031 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00002032 const RegType& uninit_type = reg_types_.Uninitialized(res_type, work_insn_idx_);
Ian Rogers08f753d2012-08-24 14:35:25 -07002033 // Any registers holding previous allocations from this address that have not yet been
2034 // initialized must be marked invalid.
Ian Rogers7b078e82014-09-10 14:44:24 -07002035 work_line_->MarkUninitRefsAsInvalid(this, uninit_type);
Ian Rogers08f753d2012-08-24 14:35:25 -07002036 // add the new uninitialized reference to the register state
Ian Rogers7b078e82014-09-10 14:44:24 -07002037 work_line_->SetRegisterType(this, inst->VRegA_21c(), uninit_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07002038 break;
2039 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08002040 case Instruction::NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002041 VerifyNewArray(inst, false, false);
jeffhaobdb76512011-09-07 11:43:16 -07002042 break;
2043 case Instruction::FILLED_NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002044 VerifyNewArray(inst, true, false);
Ian Rogers0c4a5062012-02-03 15:18:59 -08002045 just_set_result = true; // Filled new array sets result register
jeffhaobdb76512011-09-07 11:43:16 -07002046 break;
Ian Rogers0c4a5062012-02-03 15:18:59 -08002047 case Instruction::FILLED_NEW_ARRAY_RANGE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002048 VerifyNewArray(inst, true, true);
Ian Rogers0c4a5062012-02-03 15:18:59 -08002049 just_set_result = true; // Filled new array range sets result register
2050 break;
jeffhaobdb76512011-09-07 11:43:16 -07002051 case Instruction::CMPL_FLOAT:
2052 case Instruction::CMPG_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002053 if (!work_line_->VerifyRegisterType(this, inst->VRegB_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08002054 break;
2055 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002056 if (!work_line_->VerifyRegisterType(this, inst->VRegC_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08002057 break;
2058 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002059 work_line_->SetRegisterType(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002060 break;
2061 case Instruction::CMPL_DOUBLE:
2062 case Instruction::CMPG_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002063 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegB_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002064 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002065 break;
2066 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002067 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegC_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002068 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002069 break;
2070 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002071 work_line_->SetRegisterType(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002072 break;
2073 case Instruction::CMP_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002074 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegB_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002075 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002076 break;
2077 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002078 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegC_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002079 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002080 break;
2081 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002082 work_line_->SetRegisterType(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002083 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002084 case Instruction::THROW: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002085 const RegType& res_type = work_line_->GetRegisterType(this, inst->VRegA_11x());
Ian Rogersb4903572012-10-11 11:52:56 -07002086 if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(res_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07002087 Fail(res_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS : VERIFY_ERROR_BAD_CLASS_SOFT)
2088 << "thrown class " << res_type << " not instanceof Throwable";
jeffhaobdb76512011-09-07 11:43:16 -07002089 }
2090 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002091 }
jeffhaobdb76512011-09-07 11:43:16 -07002092 case Instruction::GOTO:
2093 case Instruction::GOTO_16:
2094 case Instruction::GOTO_32:
2095 /* no effect on or use of registers */
2096 break;
2097
2098 case Instruction::PACKED_SWITCH:
2099 case Instruction::SPARSE_SWITCH:
2100 /* verify that vAA is an integer, or can be converted to one */
Ian Rogers7b078e82014-09-10 14:44:24 -07002101 work_line_->VerifyRegisterType(this, inst->VRegA_31t(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002102 break;
2103
Ian Rogersd81871c2011-10-03 13:57:23 -07002104 case Instruction::FILL_ARRAY_DATA: {
2105 /* Similar to the verification done for APUT */
Ian Rogers7b078e82014-09-10 14:44:24 -07002106 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegA_31t());
Ian Rogers89310de2012-02-01 13:47:30 -08002107 /* array_type can be null if the reg type is Zero */
2108 if (!array_type.IsZero()) {
jeffhao457cc512012-02-02 16:55:13 -08002109 if (!array_type.IsArrayTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002110 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with array type "
2111 << array_type;
Ian Rogers89310de2012-02-01 13:47:30 -08002112 } else {
Ian Rogersd8f69b02014-09-10 21:43:52 +00002113 const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
Ian Rogersad0b3a32012-04-16 14:50:24 -07002114 DCHECK(!component_type.IsConflict());
jeffhao457cc512012-02-02 16:55:13 -08002115 if (component_type.IsNonZeroReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002116 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with component type "
2117 << component_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002118 } else {
jeffhao457cc512012-02-02 16:55:13 -08002119 // Now verify if the element width in the table matches the element width declared in
2120 // the array
2121 const uint16_t* array_data = insns + (insns[1] | (((int32_t) insns[2]) << 16));
2122 if (array_data[0] != Instruction::kArrayDataSignature) {
jeffhaod5347e02012-03-22 17:25:05 -07002123 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid magic for array-data";
jeffhao457cc512012-02-02 16:55:13 -08002124 } else {
2125 size_t elem_width = Primitive::ComponentSize(component_type.GetPrimitiveType());
2126 // Since we don't compress the data in Dex, expect to see equal width of data stored
2127 // in the table and expected from the array class.
2128 if (array_data[1] != elem_width) {
jeffhaod5347e02012-03-22 17:25:05 -07002129 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-data size mismatch (" << array_data[1]
2130 << " vs " << elem_width << ")";
jeffhao457cc512012-02-02 16:55:13 -08002131 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002132 }
2133 }
jeffhaobdb76512011-09-07 11:43:16 -07002134 }
2135 }
2136 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002137 }
jeffhaobdb76512011-09-07 11:43:16 -07002138 case Instruction::IF_EQ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002139 case Instruction::IF_NE: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002140 const RegType& reg_type1 = work_line_->GetRegisterType(this, inst->VRegA_22t());
2141 const RegType& reg_type2 = work_line_->GetRegisterType(this, inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002142 bool mismatch = false;
2143 if (reg_type1.IsZero()) { // zero then integral or reference expected
2144 mismatch = !reg_type2.IsReferenceTypes() && !reg_type2.IsIntegralTypes();
2145 } else if (reg_type1.IsReferenceTypes()) { // both references?
2146 mismatch = !reg_type2.IsReferenceTypes();
2147 } else { // both integral?
2148 mismatch = !reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes();
2149 }
2150 if (mismatch) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002151 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to if-eq/if-ne (" << reg_type1 << ","
2152 << reg_type2 << ") must both be references or integral";
jeffhaobdb76512011-09-07 11:43:16 -07002153 }
2154 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002155 }
jeffhaobdb76512011-09-07 11:43:16 -07002156 case Instruction::IF_LT:
2157 case Instruction::IF_GE:
2158 case Instruction::IF_GT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002159 case Instruction::IF_LE: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002160 const RegType& reg_type1 = work_line_->GetRegisterType(this, inst->VRegA_22t());
2161 const RegType& reg_type2 = work_line_->GetRegisterType(this, inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002162 if (!reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002163 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to 'if' (" << reg_type1 << ","
2164 << reg_type2 << ") must be integral";
jeffhaobdb76512011-09-07 11:43:16 -07002165 }
2166 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002167 }
jeffhaobdb76512011-09-07 11:43:16 -07002168 case Instruction::IF_EQZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002169 case Instruction::IF_NEZ: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002170 const RegType& reg_type = work_line_->GetRegisterType(this, inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002171 if (!reg_type.IsReferenceTypes() && !reg_type.IsIntegralTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002172 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
2173 << " unexpected as arg to if-eqz/if-nez";
Ian Rogersd81871c2011-10-03 13:57:23 -07002174 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002175
2176 // Find previous instruction - its existence is a precondition to peephole optimization.
Ian Rogers9b360392013-06-06 14:45:07 -07002177 uint32_t instance_of_idx = 0;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002178 if (0 != work_insn_idx_) {
Ian Rogers9b360392013-06-06 14:45:07 -07002179 instance_of_idx = work_insn_idx_ - 1;
Brian Carlstromdf629502013-07-17 22:39:56 -07002180 while (0 != instance_of_idx && !insn_flags_[instance_of_idx].IsOpcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07002181 instance_of_idx--;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002182 }
Andreas Gampe7c038102014-10-27 20:08:46 -07002183 if (FailOrAbort(this, insn_flags_[instance_of_idx].IsOpcode(),
2184 "Unable to get previous instruction of if-eqz/if-nez for work index ",
2185 work_insn_idx_)) {
2186 break;
2187 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002188 } else {
2189 break;
2190 }
2191
Ian Rogers9b360392013-06-06 14:45:07 -07002192 const Instruction* instance_of_inst = Instruction::At(code_item_->insns_ + instance_of_idx);
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002193
2194 /* Check for peep-hole pattern of:
2195 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002196 * instance-of vX, vY, T;
2197 * ifXXX vX, label ;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002198 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002199 * label:
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002200 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002201 * and sharpen the type of vY to be type T.
2202 * Note, this pattern can't be if:
2203 * - if there are other branches to this branch,
2204 * - when vX == vY.
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002205 */
Ian Rogersfae370a2013-06-05 08:33:27 -07002206 if (!CurrentInsnFlags()->IsBranchTarget() &&
Ian Rogers9b360392013-06-06 14:45:07 -07002207 (Instruction::INSTANCE_OF == instance_of_inst->Opcode()) &&
2208 (inst->VRegA_21t() == instance_of_inst->VRegA_22c()) &&
2209 (instance_of_inst->VRegA_22c() != instance_of_inst->VRegB_22c())) {
Ian Rogersebbdd872014-07-07 23:53:08 -07002210 // Check the type of the instance-of is different than that of registers type, as if they
2211 // are the same there is no work to be done here. Check that the conversion is not to or
2212 // from an unresolved type as type information is imprecise. If the instance-of is to an
2213 // interface then ignore the type information as interfaces can only be treated as Objects
2214 // and we don't want to disallow field and other operations on the object. If the value
2215 // being instance-of checked against is known null (zero) then allow the optimization as
2216 // we didn't have type information. If the merge of the instance-of type with the original
2217 // type is assignable to the original then allow optimization. This check is performed to
2218 // ensure that subsequent merges don't lose type information - such as becoming an
2219 // interface from a class that would lose information relevant to field checks.
Ian Rogers7b078e82014-09-10 14:44:24 -07002220 const RegType& orig_type = work_line_->GetRegisterType(this, instance_of_inst->VRegB_22c());
Ian Rogersd8f69b02014-09-10 21:43:52 +00002221 const RegType& cast_type = ResolveClassAndCheckAccess(instance_of_inst->VRegC_22c());
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002222
Ian Rogersebbdd872014-07-07 23:53:08 -07002223 if (!orig_type.Equals(cast_type) &&
2224 !cast_type.IsUnresolvedTypes() && !orig_type.IsUnresolvedTypes() &&
Andreas Gampe00633eb2014-07-17 16:13:35 -07002225 cast_type.HasClass() && // Could be conflict type, make sure it has a class.
Ian Rogersebbdd872014-07-07 23:53:08 -07002226 !cast_type.GetClass()->IsInterface() &&
2227 (orig_type.IsZero() ||
2228 orig_type.IsStrictlyAssignableFrom(cast_type.Merge(orig_type, &reg_types_)))) {
Ian Rogersd0fbd852013-09-24 18:17:04 -07002229 RegisterLine* update_line = RegisterLine::Create(code_item_->registers_size_, this);
Ian Rogersfae370a2013-06-05 08:33:27 -07002230 if (inst->Opcode() == Instruction::IF_EQZ) {
Ian Rogers9b360392013-06-06 14:45:07 -07002231 fallthrough_line.reset(update_line);
Ian Rogersfae370a2013-06-05 08:33:27 -07002232 } else {
Ian Rogers9b360392013-06-06 14:45:07 -07002233 branch_line.reset(update_line);
2234 }
2235 update_line->CopyFromLine(work_line_.get());
Ian Rogers7b078e82014-09-10 14:44:24 -07002236 update_line->SetRegisterType(this, instance_of_inst->VRegB_22c(), cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002237 if (!insn_flags_[instance_of_idx].IsBranchTarget() && 0 != instance_of_idx) {
2238 // See if instance-of was preceded by a move-object operation, common due to the small
2239 // register encoding space of instance-of, and propagate type information to the source
2240 // of the move-object.
2241 uint32_t move_idx = instance_of_idx - 1;
Brian Carlstromdf629502013-07-17 22:39:56 -07002242 while (0 != move_idx && !insn_flags_[move_idx].IsOpcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07002243 move_idx--;
2244 }
Andreas Gampe7c038102014-10-27 20:08:46 -07002245 if (FailOrAbort(this, insn_flags_[move_idx].IsOpcode(),
2246 "Unable to get previous instruction of if-eqz/if-nez for work index ",
2247 work_insn_idx_)) {
2248 break;
2249 }
Ian Rogers9b360392013-06-06 14:45:07 -07002250 const Instruction* move_inst = Instruction::At(code_item_->insns_ + move_idx);
2251 switch (move_inst->Opcode()) {
2252 case Instruction::MOVE_OBJECT:
2253 if (move_inst->VRegA_12x() == instance_of_inst->VRegB_22c()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002254 update_line->SetRegisterType(this, move_inst->VRegB_12x(), cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002255 }
2256 break;
2257 case Instruction::MOVE_OBJECT_FROM16:
2258 if (move_inst->VRegA_22x() == instance_of_inst->VRegB_22c()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002259 update_line->SetRegisterType(this, move_inst->VRegB_22x(), cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002260 }
2261 break;
2262 case Instruction::MOVE_OBJECT_16:
2263 if (move_inst->VRegA_32x() == instance_of_inst->VRegB_22c()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002264 update_line->SetRegisterType(this, move_inst->VRegB_32x(), cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002265 }
2266 break;
2267 default:
2268 break;
2269 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002270 }
2271 }
2272 }
2273
jeffhaobdb76512011-09-07 11:43:16 -07002274 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002275 }
jeffhaobdb76512011-09-07 11:43:16 -07002276 case Instruction::IF_LTZ:
2277 case Instruction::IF_GEZ:
2278 case Instruction::IF_GTZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002279 case Instruction::IF_LEZ: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002280 const RegType& reg_type = work_line_->GetRegisterType(this, inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002281 if (!reg_type.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002282 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
2283 << " unexpected as arg to if-ltz/if-gez/if-gtz/if-lez";
Ian Rogersd81871c2011-10-03 13:57:23 -07002284 }
jeffhaobdb76512011-09-07 11:43:16 -07002285 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002286 }
jeffhaobdb76512011-09-07 11:43:16 -07002287 case Instruction::AGET_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002288 VerifyAGet(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002289 break;
jeffhaobdb76512011-09-07 11:43:16 -07002290 case Instruction::AGET_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002291 VerifyAGet(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002292 break;
jeffhaobdb76512011-09-07 11:43:16 -07002293 case Instruction::AGET_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002294 VerifyAGet(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002295 break;
jeffhaobdb76512011-09-07 11:43:16 -07002296 case Instruction::AGET_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002297 VerifyAGet(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002298 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002299 case Instruction::AGET:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002300 VerifyAGet(inst, reg_types_.Integer(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002301 break;
jeffhaobdb76512011-09-07 11:43:16 -07002302 case Instruction::AGET_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002303 VerifyAGet(inst, reg_types_.LongLo(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002304 break;
2305 case Instruction::AGET_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002306 VerifyAGet(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07002307 break;
2308
Ian Rogersd81871c2011-10-03 13:57:23 -07002309 case Instruction::APUT_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002310 VerifyAPut(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002311 break;
2312 case Instruction::APUT_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002313 VerifyAPut(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002314 break;
2315 case Instruction::APUT_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002316 VerifyAPut(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002317 break;
2318 case Instruction::APUT_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002319 VerifyAPut(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002320 break;
2321 case Instruction::APUT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002322 VerifyAPut(inst, reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002323 break;
2324 case Instruction::APUT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002325 VerifyAPut(inst, reg_types_.LongLo(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002326 break;
2327 case Instruction::APUT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002328 VerifyAPut(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07002329 break;
2330
jeffhaobdb76512011-09-07 11:43:16 -07002331 case Instruction::IGET_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002332 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002333 break;
jeffhaobdb76512011-09-07 11:43:16 -07002334 case Instruction::IGET_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002335 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002336 break;
jeffhaobdb76512011-09-07 11:43:16 -07002337 case Instruction::IGET_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002338 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002339 break;
jeffhaobdb76512011-09-07 11:43:16 -07002340 case Instruction::IGET_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002341 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002342 break;
2343 case Instruction::IGET:
Andreas Gampe896df402014-10-20 22:25:29 -07002344 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002345 break;
2346 case Instruction::IGET_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002347 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002348 break;
2349 case Instruction::IGET_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002350 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false,
2351 false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002352 break;
jeffhaobdb76512011-09-07 11:43:16 -07002353
Ian Rogersd81871c2011-10-03 13:57:23 -07002354 case Instruction::IPUT_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002355 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002356 break;
2357 case Instruction::IPUT_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002358 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002359 break;
2360 case Instruction::IPUT_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002361 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002362 break;
2363 case Instruction::IPUT_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002364 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002365 break;
2366 case Instruction::IPUT:
Andreas Gampe896df402014-10-20 22:25:29 -07002367 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002368 break;
2369 case Instruction::IPUT_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002370 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002371 break;
jeffhaobdb76512011-09-07 11:43:16 -07002372 case Instruction::IPUT_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002373 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false,
2374 false);
jeffhaobdb76512011-09-07 11:43:16 -07002375 break;
2376
jeffhaobdb76512011-09-07 11:43:16 -07002377 case Instruction::SGET_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002378 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002379 break;
jeffhaobdb76512011-09-07 11:43:16 -07002380 case Instruction::SGET_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002381 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002382 break;
jeffhaobdb76512011-09-07 11:43:16 -07002383 case Instruction::SGET_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002384 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002385 break;
jeffhaobdb76512011-09-07 11:43:16 -07002386 case Instruction::SGET_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002387 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002388 break;
2389 case Instruction::SGET:
Andreas Gampe896df402014-10-20 22:25:29 -07002390 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002391 break;
2392 case Instruction::SGET_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002393 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002394 break;
2395 case Instruction::SGET_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002396 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false,
2397 true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002398 break;
2399
2400 case Instruction::SPUT_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002401 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002402 break;
2403 case Instruction::SPUT_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002404 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002405 break;
2406 case Instruction::SPUT_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002407 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002408 break;
2409 case Instruction::SPUT_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002410 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002411 break;
2412 case Instruction::SPUT:
Andreas Gampe896df402014-10-20 22:25:29 -07002413 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002414 break;
2415 case Instruction::SPUT_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002416 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002417 break;
2418 case Instruction::SPUT_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002419 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false,
2420 true);
jeffhaobdb76512011-09-07 11:43:16 -07002421 break;
2422
2423 case Instruction::INVOKE_VIRTUAL:
2424 case Instruction::INVOKE_VIRTUAL_RANGE:
2425 case Instruction::INVOKE_SUPER:
Ian Rogersd81871c2011-10-03 13:57:23 -07002426 case Instruction::INVOKE_SUPER_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002427 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE ||
2428 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002429 bool is_super = (inst->Opcode() == Instruction::INVOKE_SUPER ||
2430 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002431 ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_VIRTUAL, is_range, is_super);
Ian Rogersd8f69b02014-09-10 21:43:52 +00002432 const RegType* return_type = nullptr;
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002433 if (called_method != nullptr) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002434 StackHandleScope<1> hs(self_);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002435 mirror::Class* return_type_class = called_method->GetReturnType(can_load_classes_);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002436 if (return_type_class != nullptr) {
Andreas Gampef23f33d2015-06-23 14:18:17 -07002437 return_type = &FromClass(called_method->GetReturnTypeDescriptor(),
2438 return_type_class,
2439 return_type_class->CannotBeAssignedFromOtherTypes());
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002440 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07002441 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
2442 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002443 }
2444 }
2445 if (return_type == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002446 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002447 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2448 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002449 const char* descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Mathieu Chartierbf99f772014-08-23 16:37:27 -07002450 return_type = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
jeffhaobdb76512011-09-07 11:43:16 -07002451 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002452 if (!return_type->IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002453 work_line_->SetResultRegisterType(this, *return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002454 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002455 work_line_->SetResultRegisterTypeWide(*return_type, return_type->HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002456 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002457 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002458 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002459 }
jeffhaobdb76512011-09-07 11:43:16 -07002460 case Instruction::INVOKE_DIRECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002461 case Instruction::INVOKE_DIRECT_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002462 bool is_range = (inst->Opcode() == Instruction::INVOKE_DIRECT_RANGE);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002463 ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_DIRECT, is_range, false);
Ian Rogers46685432012-06-03 22:26:43 -07002464 const char* return_type_descriptor;
2465 bool is_constructor;
Ian Rogersd8f69b02014-09-10 21:43:52 +00002466 const RegType* return_type = nullptr;
Ian Rogers7b078e82014-09-10 14:44:24 -07002467 if (called_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002468 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogers46685432012-06-03 22:26:43 -07002469 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
Ian Rogersdfb325e2013-10-30 01:00:44 -07002470 is_constructor = strcmp("<init>", dex_file_->StringDataByIdx(method_id.name_idx_)) == 0;
Ian Rogers46685432012-06-03 22:26:43 -07002471 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
2472 return_type_descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2473 } else {
2474 is_constructor = called_method->IsConstructor();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002475 return_type_descriptor = called_method->GetReturnTypeDescriptor();
Ian Rogers7b078e82014-09-10 14:44:24 -07002476 StackHandleScope<1> hs(self_);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002477 mirror::Class* return_type_class = called_method->GetReturnType(can_load_classes_);
Ian Rogers1ff3c982014-08-12 02:30:58 -07002478 if (return_type_class != nullptr) {
Andreas Gampef23f33d2015-06-23 14:18:17 -07002479 return_type = &FromClass(return_type_descriptor,
2480 return_type_class,
2481 return_type_class->CannotBeAssignedFromOtherTypes());
Ian Rogers1ff3c982014-08-12 02:30:58 -07002482 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07002483 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
2484 self_->ClearException();
Ian Rogers1ff3c982014-08-12 02:30:58 -07002485 }
Ian Rogers46685432012-06-03 22:26:43 -07002486 }
2487 if (is_constructor) {
jeffhaobdb76512011-09-07 11:43:16 -07002488 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002489 * Some additional checks when calling a constructor. We know from the invocation arg check
2490 * that the "this" argument is an instance of called_method->klass. Now we further restrict
2491 * that to require that called_method->klass is the same as this->klass or this->super,
2492 * allowing the latter only if the "this" argument is the same as the "this" argument to
2493 * this method (which implies that we're in a constructor ourselves).
jeffhaobdb76512011-09-07 11:43:16 -07002494 */
Ian Rogers7b078e82014-09-10 14:44:24 -07002495 const RegType& this_type = work_line_->GetInvocationThis(this, inst, is_range);
jeffhaob57e9522012-04-26 18:08:21 -07002496 if (this_type.IsConflict()) // failure.
2497 break;
jeffhaobdb76512011-09-07 11:43:16 -07002498
jeffhaob57e9522012-04-26 18:08:21 -07002499 /* no null refs allowed (?) */
2500 if (this_type.IsZero()) {
2501 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unable to initialize null ref";
2502 break;
jeffhao2a8a90e2011-09-26 14:25:31 -07002503 }
jeffhaob57e9522012-04-26 18:08:21 -07002504
2505 /* must be in same class or in superclass */
Ian Rogersd8f69b02014-09-10 21:43:52 +00002506 // const RegType& this_super_klass = this_type.GetSuperClass(&reg_types_);
Ian Rogers46685432012-06-03 22:26:43 -07002507 // TODO: re-enable constructor type verification
2508 // if (this_super_klass.IsConflict()) {
jeffhaob57e9522012-04-26 18:08:21 -07002509 // Unknown super class, fail so we re-check at runtime.
Ian Rogers46685432012-06-03 22:26:43 -07002510 // Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "super class unknown for '" << this_type << "'";
2511 // break;
2512 // }
jeffhaob57e9522012-04-26 18:08:21 -07002513
2514 /* arg must be an uninitialized reference */
2515 if (!this_type.IsUninitializedTypes()) {
2516 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Expected initialization on uninitialized reference "
2517 << this_type;
2518 break;
2519 }
2520
2521 /*
2522 * Replace the uninitialized reference with an initialized one. We need to do this for all
2523 * registers that have the same object instance in them, not just the "this" register.
2524 */
Jeff Hao848f70a2014-01-15 13:49:50 -08002525 const uint32_t this_reg = (is_range) ? inst->VRegC_3rc() : inst->VRegC_35c();
2526 work_line_->MarkRefsAsInitialized(this, this_type, this_reg, work_insn_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002527 }
Ian Rogers1ff3c982014-08-12 02:30:58 -07002528 if (return_type == nullptr) {
Mathieu Chartierbf99f772014-08-23 16:37:27 -07002529 return_type = &reg_types_.FromDescriptor(GetClassLoader(), return_type_descriptor,
2530 false);
Ian Rogers1ff3c982014-08-12 02:30:58 -07002531 }
2532 if (!return_type->IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002533 work_line_->SetResultRegisterType(this, *return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002534 } else {
Ian Rogers1ff3c982014-08-12 02:30:58 -07002535 work_line_->SetResultRegisterTypeWide(*return_type, return_type->HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002536 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002537 just_set_result = true;
2538 break;
2539 }
2540 case Instruction::INVOKE_STATIC:
2541 case Instruction::INVOKE_STATIC_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002542 bool is_range = (inst->Opcode() == Instruction::INVOKE_STATIC_RANGE);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002543 ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_STATIC, is_range, false);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002544 const char* descriptor;
Ian Rogers7b078e82014-09-10 14:44:24 -07002545 if (called_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002546 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002547 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2548 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Mathieu Chartier590fee92013-09-13 13:46:47 -07002549 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002550 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002551 descriptor = called_method->GetReturnTypeDescriptor();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002552 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00002553 const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002554 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002555 work_line_->SetResultRegisterType(this, return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002556 } else {
2557 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2558 }
jeffhaobdb76512011-09-07 11:43:16 -07002559 just_set_result = true;
2560 }
2561 break;
jeffhaobdb76512011-09-07 11:43:16 -07002562 case Instruction::INVOKE_INTERFACE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002563 case Instruction::INVOKE_INTERFACE_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002564 bool is_range = (inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002565 ArtMethod* abs_method = VerifyInvocationArgs(inst, METHOD_INTERFACE, is_range, false);
Ian Rogers7b078e82014-09-10 14:44:24 -07002566 if (abs_method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002567 mirror::Class* called_interface = abs_method->GetDeclaringClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002568 if (!called_interface->IsInterface() && !called_interface->IsObjectClass()) {
2569 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected interface class in invoke-interface '"
2570 << PrettyMethod(abs_method) << "'";
2571 break;
Ian Rogers28ad40d2011-10-27 15:19:26 -07002572 }
Ian Rogers0d604842012-04-16 14:50:24 -07002573 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002574 /* Get the type of the "this" arg, which should either be a sub-interface of called
2575 * interface or Object (see comments in RegType::JoinClass).
2576 */
Ian Rogers7b078e82014-09-10 14:44:24 -07002577 const RegType& this_type = work_line_->GetInvocationThis(this, inst, is_range);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002578 if (this_type.IsZero()) {
2579 /* null pointer always passes (and always fails at runtime) */
2580 } else {
2581 if (this_type.IsUninitializedTypes()) {
2582 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface call on uninitialized object "
2583 << this_type;
2584 break;
2585 }
2586 // In the past we have tried to assert that "called_interface" is assignable
2587 // from "this_type.GetClass()", however, as we do an imprecise Join
2588 // (RegType::JoinClass) we don't have full information on what interfaces are
2589 // implemented by "this_type". For example, two classes may implement the same
2590 // interfaces and have a common parent that doesn't implement the interface. The
2591 // join will set "this_type" to the parent class and a test that this implements
2592 // the interface will incorrectly fail.
2593 }
2594 /*
2595 * We don't have an object instance, so we can't find the concrete method. However, all of
2596 * the type information is in the abstract method, so we're good.
2597 */
2598 const char* descriptor;
Ian Rogers7b078e82014-09-10 14:44:24 -07002599 if (abs_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002600 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002601 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2602 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
2603 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2604 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002605 descriptor = abs_method->GetReturnTypeDescriptor();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002606 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00002607 const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002608 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002609 work_line_->SetResultRegisterType(this, return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002610 } else {
2611 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2612 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002613 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002614 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002615 }
jeffhaobdb76512011-09-07 11:43:16 -07002616 case Instruction::NEG_INT:
2617 case Instruction::NOT_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002618 work_line_->CheckUnaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002619 break;
2620 case Instruction::NEG_LONG:
2621 case Instruction::NOT_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002622 work_line_->CheckUnaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002623 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002624 break;
2625 case Instruction::NEG_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002626 work_line_->CheckUnaryOp(this, inst, reg_types_.Float(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002627 break;
2628 case Instruction::NEG_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002629 work_line_->CheckUnaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002630 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002631 break;
2632 case Instruction::INT_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002633 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002634 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002635 break;
2636 case Instruction::INT_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002637 work_line_->CheckUnaryOp(this, inst, reg_types_.Float(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002638 break;
2639 case Instruction::INT_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002640 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002641 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002642 break;
2643 case Instruction::LONG_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002644 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002645 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002646 break;
2647 case Instruction::LONG_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002648 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002649 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002650 break;
2651 case Instruction::LONG_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002652 work_line_->CheckUnaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002653 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002654 break;
2655 case Instruction::FLOAT_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002656 work_line_->CheckUnaryOp(this, inst, reg_types_.Integer(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002657 break;
2658 case Instruction::FLOAT_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002659 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002660 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002661 break;
2662 case Instruction::FLOAT_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002663 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002664 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002665 break;
2666 case Instruction::DOUBLE_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002667 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002668 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002669 break;
2670 case Instruction::DOUBLE_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002671 work_line_->CheckUnaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002672 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002673 break;
2674 case Instruction::DOUBLE_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002675 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002676 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002677 break;
2678 case Instruction::INT_TO_BYTE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002679 work_line_->CheckUnaryOp(this, inst, reg_types_.Byte(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002680 break;
2681 case Instruction::INT_TO_CHAR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002682 work_line_->CheckUnaryOp(this, inst, reg_types_.Char(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002683 break;
2684 case Instruction::INT_TO_SHORT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002685 work_line_->CheckUnaryOp(this, inst, reg_types_.Short(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002686 break;
2687
2688 case Instruction::ADD_INT:
2689 case Instruction::SUB_INT:
2690 case Instruction::MUL_INT:
2691 case Instruction::REM_INT:
2692 case Instruction::DIV_INT:
2693 case Instruction::SHL_INT:
2694 case Instruction::SHR_INT:
2695 case Instruction::USHR_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002696 work_line_->CheckBinaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002697 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002698 break;
2699 case Instruction::AND_INT:
2700 case Instruction::OR_INT:
2701 case Instruction::XOR_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002702 work_line_->CheckBinaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002703 reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002704 break;
2705 case Instruction::ADD_LONG:
2706 case Instruction::SUB_LONG:
2707 case Instruction::MUL_LONG:
2708 case Instruction::DIV_LONG:
2709 case Instruction::REM_LONG:
2710 case Instruction::AND_LONG:
2711 case Instruction::OR_LONG:
2712 case Instruction::XOR_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002713 work_line_->CheckBinaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002714 reg_types_.LongLo(), reg_types_.LongHi(),
2715 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002716 break;
2717 case Instruction::SHL_LONG:
2718 case Instruction::SHR_LONG:
2719 case Instruction::USHR_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002720 /* shift distance is Int, making these different from other binary operations */
Ian Rogers7b078e82014-09-10 14:44:24 -07002721 work_line_->CheckBinaryOpWideShift(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002722 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002723 break;
2724 case Instruction::ADD_FLOAT:
2725 case Instruction::SUB_FLOAT:
2726 case Instruction::MUL_FLOAT:
2727 case Instruction::DIV_FLOAT:
2728 case Instruction::REM_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002729 work_line_->CheckBinaryOp(this, inst, reg_types_.Float(), reg_types_.Float(),
2730 reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002731 break;
2732 case Instruction::ADD_DOUBLE:
2733 case Instruction::SUB_DOUBLE:
2734 case Instruction::MUL_DOUBLE:
2735 case Instruction::DIV_DOUBLE:
2736 case Instruction::REM_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002737 work_line_->CheckBinaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002738 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2739 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002740 break;
2741 case Instruction::ADD_INT_2ADDR:
2742 case Instruction::SUB_INT_2ADDR:
2743 case Instruction::MUL_INT_2ADDR:
2744 case Instruction::REM_INT_2ADDR:
2745 case Instruction::SHL_INT_2ADDR:
2746 case Instruction::SHR_INT_2ADDR:
2747 case Instruction::USHR_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002748 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
2749 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002750 break;
2751 case Instruction::AND_INT_2ADDR:
2752 case Instruction::OR_INT_2ADDR:
2753 case Instruction::XOR_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002754 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
2755 reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002756 break;
2757 case Instruction::DIV_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002758 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
2759 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002760 break;
2761 case Instruction::ADD_LONG_2ADDR:
2762 case Instruction::SUB_LONG_2ADDR:
2763 case Instruction::MUL_LONG_2ADDR:
2764 case Instruction::DIV_LONG_2ADDR:
2765 case Instruction::REM_LONG_2ADDR:
2766 case Instruction::AND_LONG_2ADDR:
2767 case Instruction::OR_LONG_2ADDR:
2768 case Instruction::XOR_LONG_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002769 work_line_->CheckBinaryOp2addrWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002770 reg_types_.LongLo(), reg_types_.LongHi(),
2771 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002772 break;
2773 case Instruction::SHL_LONG_2ADDR:
2774 case Instruction::SHR_LONG_2ADDR:
2775 case Instruction::USHR_LONG_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002776 work_line_->CheckBinaryOp2addrWideShift(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002777 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002778 break;
2779 case Instruction::ADD_FLOAT_2ADDR:
2780 case Instruction::SUB_FLOAT_2ADDR:
2781 case Instruction::MUL_FLOAT_2ADDR:
2782 case Instruction::DIV_FLOAT_2ADDR:
2783 case Instruction::REM_FLOAT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002784 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Float(), reg_types_.Float(),
2785 reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002786 break;
2787 case Instruction::ADD_DOUBLE_2ADDR:
2788 case Instruction::SUB_DOUBLE_2ADDR:
2789 case Instruction::MUL_DOUBLE_2ADDR:
2790 case Instruction::DIV_DOUBLE_2ADDR:
2791 case Instruction::REM_DOUBLE_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002792 work_line_->CheckBinaryOp2addrWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002793 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2794 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002795 break;
2796 case Instruction::ADD_INT_LIT16:
Ian Rogersf72a11d2014-10-30 15:41:08 -07002797 case Instruction::RSUB_INT_LIT16:
jeffhaobdb76512011-09-07 11:43:16 -07002798 case Instruction::MUL_INT_LIT16:
2799 case Instruction::DIV_INT_LIT16:
2800 case Instruction::REM_INT_LIT16:
Ian Rogers7b078e82014-09-10 14:44:24 -07002801 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), false,
2802 true);
jeffhaobdb76512011-09-07 11:43:16 -07002803 break;
2804 case Instruction::AND_INT_LIT16:
2805 case Instruction::OR_INT_LIT16:
2806 case Instruction::XOR_INT_LIT16:
Ian Rogers7b078e82014-09-10 14:44:24 -07002807 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), true,
2808 true);
jeffhaobdb76512011-09-07 11:43:16 -07002809 break;
2810 case Instruction::ADD_INT_LIT8:
2811 case Instruction::RSUB_INT_LIT8:
2812 case Instruction::MUL_INT_LIT8:
2813 case Instruction::DIV_INT_LIT8:
2814 case Instruction::REM_INT_LIT8:
2815 case Instruction::SHL_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002816 case Instruction::SHR_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002817 case Instruction::USHR_INT_LIT8:
Ian Rogers7b078e82014-09-10 14:44:24 -07002818 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), false,
2819 false);
jeffhaobdb76512011-09-07 11:43:16 -07002820 break;
2821 case Instruction::AND_INT_LIT8:
2822 case Instruction::OR_INT_LIT8:
2823 case Instruction::XOR_INT_LIT8:
Ian Rogers7b078e82014-09-10 14:44:24 -07002824 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), true,
2825 false);
jeffhaobdb76512011-09-07 11:43:16 -07002826 break;
2827
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002828 // Special instructions.
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07002829 case Instruction::RETURN_VOID_NO_BARRIER:
2830 if (IsConstructor() && !IsStatic()) {
2831 auto& declaring_class = GetDeclaringClass();
Andreas Gampe68df3202015-06-22 11:35:46 -07002832 if (declaring_class.IsUnresolvedReference()) {
2833 // We must iterate over the fields, even if we cannot use mirror classes to do so. Do it
2834 // manually over the underlying dex file.
2835 uint32_t first_index = GetFirstFinalInstanceFieldIndex(*dex_file_,
2836 dex_file_->GetMethodId(dex_method_idx_).class_idx_);
2837 if (first_index != DexFile::kDexNoIndex) {
2838 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void-no-barrier not expected for field "
2839 << first_index;
2840 }
2841 break;
2842 }
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07002843 auto* klass = declaring_class.GetClass();
2844 for (uint32_t i = 0, num_fields = klass->NumInstanceFields(); i < num_fields; ++i) {
2845 if (klass->GetInstanceField(i)->IsFinal()) {
Mathieu Chartiere86deef2015-03-19 13:43:37 -07002846 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void-no-barrier not expected for "
2847 << PrettyField(klass->GetInstanceField(i));
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07002848 break;
2849 }
2850 }
Sebastien Hertzcc10e0e2013-06-28 14:24:48 +02002851 }
2852 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002853 // Note: the following instructions encode offsets derived from class linking.
2854 // As such they use Class*/Field*/AbstractMethod* as these offsets only have
2855 // meaning if the class linking and resolution were successful.
2856 case Instruction::IGET_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002857 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002858 break;
2859 case Instruction::IGET_WIDE_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002860 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002861 break;
2862 case Instruction::IGET_OBJECT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002863 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002864 break;
Mathieu Chartierffc605c2014-12-10 10:35:44 -08002865 case Instruction::IGET_BOOLEAN_QUICK:
2866 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true);
2867 break;
2868 case Instruction::IGET_BYTE_QUICK:
2869 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true);
2870 break;
2871 case Instruction::IGET_CHAR_QUICK:
2872 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true);
2873 break;
2874 case Instruction::IGET_SHORT_QUICK:
2875 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true);
2876 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002877 case Instruction::IPUT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002878 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002879 break;
Fred Shih37f05ef2014-07-16 18:38:08 -07002880 case Instruction::IPUT_BOOLEAN_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002881 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07002882 break;
2883 case Instruction::IPUT_BYTE_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002884 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07002885 break;
2886 case Instruction::IPUT_CHAR_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002887 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07002888 break;
2889 case Instruction::IPUT_SHORT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002890 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07002891 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002892 case Instruction::IPUT_WIDE_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002893 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002894 break;
2895 case Instruction::IPUT_OBJECT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002896 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002897 break;
2898 case Instruction::INVOKE_VIRTUAL_QUICK:
2899 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: {
2900 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002901 ArtMethod* called_method = VerifyInvokeVirtualQuickArgs(inst, is_range);
Ian Rogers7b078e82014-09-10 14:44:24 -07002902 if (called_method != nullptr) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002903 const char* descriptor = called_method->GetReturnTypeDescriptor();
Ian Rogersd8f69b02014-09-10 21:43:52 +00002904 const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002905 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002906 work_line_->SetResultRegisterType(this, return_type);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002907 } else {
2908 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2909 }
2910 just_set_result = true;
2911 }
2912 break;
2913 }
Igor Murashkin158f35c2015-06-10 15:55:30 -07002914 case Instruction::INVOKE_LAMBDA: {
2915 // Don't bother verifying, instead the interpreter will take the slow path with access checks.
2916 // If the code would've normally hard-failed, then the interpreter will throw the
2917 // appropriate verification errors at runtime.
2918 Fail(VERIFY_ERROR_FORCE_INTERPRETER); // TODO(iam): implement invoke-lambda verification
2919 break;
2920 }
2921 case Instruction::CREATE_LAMBDA: {
2922 // Don't bother verifying, instead the interpreter will take the slow path with access checks.
2923 // If the code would've normally hard-failed, then the interpreter will throw the
2924 // appropriate verification errors at runtime.
2925 Fail(VERIFY_ERROR_FORCE_INTERPRETER); // TODO(iam): implement create-lambda verification
2926 break;
2927 }
2928
Igor Murashkin2ee54e22015-06-18 10:05:11 -07002929 case Instruction::UNUSED_F4:
2930 case Instruction::UNUSED_F5:
2931 case Instruction::UNUSED_F7: {
Igor Murashkin158f35c2015-06-10 15:55:30 -07002932 DCHECK(false); // TODO(iam): Implement opcodes for lambdas
Igor Murashkin2ee54e22015-06-18 10:05:11 -07002933 // Conservatively fail verification on release builds.
2934 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Unexpected opcode " << inst->DumpString(dex_file_);
2935 break;
2936 }
2937
2938 case Instruction::BOX_LAMBDA: {
2939 // Don't bother verifying, instead the interpreter will take the slow path with access checks.
2940 // If the code would've normally hard-failed, then the interpreter will throw the
2941 // appropriate verification errors at runtime.
2942 Fail(VERIFY_ERROR_FORCE_INTERPRETER); // TODO(iam): implement box-lambda verification
2943 break;
2944 }
2945
2946 case Instruction::UNBOX_LAMBDA: {
2947 // Don't bother verifying, instead the interpreter will take the slow path with access checks.
2948 // If the code would've normally hard-failed, then the interpreter will throw the
2949 // appropriate verification errors at runtime.
2950 Fail(VERIFY_ERROR_FORCE_INTERPRETER); // TODO(iam): implement unbox-lambda verification
2951 break;
Igor Murashkin158f35c2015-06-10 15:55:30 -07002952 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002953
Ian Rogersd81871c2011-10-03 13:57:23 -07002954 /* These should never appear during verification. */
Mathieu Chartierffc605c2014-12-10 10:35:44 -08002955 case Instruction::UNUSED_3E ... Instruction::UNUSED_43:
Igor Murashkin158f35c2015-06-10 15:55:30 -07002956 case Instruction::UNUSED_FA ... Instruction::UNUSED_FF:
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002957 case Instruction::UNUSED_79:
2958 case Instruction::UNUSED_7A:
jeffhaod5347e02012-03-22 17:25:05 -07002959 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Unexpected opcode " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07002960 break;
2961
2962 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002963 * DO NOT add a "default" clause here. Without it the compiler will
jeffhaobdb76512011-09-07 11:43:16 -07002964 * complain if an instruction is missing (which is desirable).
2965 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002966 } // end - switch (dec_insn.opcode)
jeffhaobdb76512011-09-07 11:43:16 -07002967
Stephen Kyle7e541c92014-12-17 17:10:02 +00002968 /*
2969 * If we are in a constructor, and we had an UninitializedThis type
2970 * in a register somewhere, we need to make sure it wasn't overwritten.
2971 */
2972 if (track_uninitialized_this) {
2973 bool was_invoke_direct = (inst->Opcode() == Instruction::INVOKE_DIRECT ||
2974 inst->Opcode() == Instruction::INVOKE_DIRECT_RANGE);
2975 if (work_line_->WasUninitializedThisOverwritten(this, uninitialized_this_loc,
2976 was_invoke_direct)) {
2977 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
2978 << "Constructor failed to initialize this object";
2979 }
2980 }
2981
Ian Rogersad0b3a32012-04-16 14:50:24 -07002982 if (have_pending_hard_failure_) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08002983 if (Runtime::Current()->IsAotCompiler()) {
2984 /* When AOT compiling, check that the last failure is a hard failure */
Andreas Gampeb588f4c2015-05-26 13:35:39 -07002985 if (failures_[failures_.size() - 1] != VERIFY_ERROR_BAD_CLASS_HARD) {
2986 LOG(ERROR) << "Pending failures:";
2987 for (auto& error : failures_) {
2988 LOG(ERROR) << error;
2989 }
2990 for (auto& error_msg : failure_messages_) {
2991 LOG(ERROR) << error_msg->str();
2992 }
2993 LOG(FATAL) << "Pending hard failure, but last failure not hard.";
2994 }
Ian Rogerse1758fe2012-04-19 11:31:15 -07002995 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002996 /* immediate failure, reject class */
2997 info_messages_ << "Rejecting opcode " << inst->DumpString(dex_file_);
2998 return false;
jeffhaofaf459e2012-08-31 15:32:47 -07002999 } else if (have_pending_runtime_throw_failure_) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07003000 /* checking interpreter will throw, mark following code as unreachable */
jeffhaofaf459e2012-08-31 15:32:47 -07003001 opcode_flags = Instruction::kThrow;
Andreas Gamped12e7822015-06-25 10:26:40 -07003002 have_any_pending_runtime_throw_failure_ = true;
3003 // Reset the pending_runtime_throw flag. The flag is a global to decouple Fail and is per
3004 // instruction.
3005 have_pending_runtime_throw_failure_ = false;
jeffhaobdb76512011-09-07 11:43:16 -07003006 }
jeffhaobdb76512011-09-07 11:43:16 -07003007 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003008 * If we didn't just set the result register, clear it out. This ensures that you can only use
3009 * "move-result" immediately after the result is set. (We could check this statically, but it's
3010 * not expensive and it makes our debugging output cleaner.)
jeffhaobdb76512011-09-07 11:43:16 -07003011 */
3012 if (!just_set_result) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003013 work_line_->SetResultTypeToUnknown(this);
jeffhaobdb76512011-09-07 11:43:16 -07003014 }
3015
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003016
jeffhaobdb76512011-09-07 11:43:16 -07003017
3018 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003019 * Handle "branch". Tag the branch target.
jeffhaobdb76512011-09-07 11:43:16 -07003020 *
3021 * NOTE: instructions like Instruction::EQZ provide information about the
jeffhaod1f0fde2011-09-08 17:25:33 -07003022 * state of the register when the branch is taken or not taken. For example,
jeffhaobdb76512011-09-07 11:43:16 -07003023 * somebody could get a reference field, check it for zero, and if the
3024 * branch is taken immediately store that register in a boolean field
jeffhaod1f0fde2011-09-08 17:25:33 -07003025 * since the value is known to be zero. We do not currently account for
jeffhaobdb76512011-09-07 11:43:16 -07003026 * that, and will reject the code.
3027 *
3028 * TODO: avoid re-fetching the branch target
3029 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003030 if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07003031 bool isConditional, selfOkay;
Ian Rogersd81871c2011-10-03 13:57:23 -07003032 if (!GetBranchOffset(work_insn_idx_, &branch_target, &isConditional, &selfOkay)) {
jeffhaobdb76512011-09-07 11:43:16 -07003033 /* should never happen after static verification */
jeffhaod5347e02012-03-22 17:25:05 -07003034 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad branch";
jeffhaobdb76512011-09-07 11:43:16 -07003035 return false;
3036 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08003037 DCHECK_EQ(isConditional, (opcode_flags & Instruction::kContinue) != 0);
Stephen Kyle9bc61992014-09-22 13:53:15 +01003038 if (!CheckNotMoveExceptionOrMoveResult(code_item_->insns_, work_insn_idx_ + branch_target)) {
jeffhaobdb76512011-09-07 11:43:16 -07003039 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07003040 }
jeffhaobdb76512011-09-07 11:43:16 -07003041 /* update branch target, set "changed" if appropriate */
Ian Rogers7b078e82014-09-10 14:44:24 -07003042 if (nullptr != branch_line.get()) {
Ian Rogersebbdd872014-07-07 23:53:08 -07003043 if (!UpdateRegisters(work_insn_idx_ + branch_target, branch_line.get(), false)) {
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003044 return false;
3045 }
3046 } else {
Ian Rogersebbdd872014-07-07 23:53:08 -07003047 if (!UpdateRegisters(work_insn_idx_ + branch_target, work_line_.get(), false)) {
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003048 return false;
3049 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003050 }
jeffhaobdb76512011-09-07 11:43:16 -07003051 }
3052
3053 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003054 * Handle "switch". Tag all possible branch targets.
jeffhaobdb76512011-09-07 11:43:16 -07003055 *
3056 * We've already verified that the table is structurally sound, so we
3057 * just need to walk through and tag the targets.
3058 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003059 if ((opcode_flags & Instruction::kSwitch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07003060 int offset_to_switch = insns[1] | (((int32_t) insns[2]) << 16);
3061 const uint16_t* switch_insns = insns + offset_to_switch;
3062 int switch_count = switch_insns[1];
3063 int offset_to_targets, targ;
3064
3065 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
3066 /* 0 = sig, 1 = count, 2/3 = first key */
3067 offset_to_targets = 4;
3068 } else {
3069 /* 0 = sig, 1 = count, 2..count * 2 = keys */
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07003070 DCHECK((*insns & 0xff) == Instruction::SPARSE_SWITCH);
jeffhaobdb76512011-09-07 11:43:16 -07003071 offset_to_targets = 2 + 2 * switch_count;
3072 }
3073
3074 /* verify each switch target */
3075 for (targ = 0; targ < switch_count; targ++) {
3076 int offset;
3077 uint32_t abs_offset;
3078
3079 /* offsets are 32-bit, and only partly endian-swapped */
3080 offset = switch_insns[offset_to_targets + targ * 2] |
3081 (((int32_t) switch_insns[offset_to_targets + targ * 2 + 1]) << 16);
Ian Rogersd81871c2011-10-03 13:57:23 -07003082 abs_offset = work_insn_idx_ + offset;
3083 DCHECK_LT(abs_offset, code_item_->insns_size_in_code_units_);
Stephen Kyle9bc61992014-09-22 13:53:15 +01003084 if (!CheckNotMoveExceptionOrMoveResult(code_item_->insns_, abs_offset)) {
jeffhaobdb76512011-09-07 11:43:16 -07003085 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07003086 }
Ian Rogersebbdd872014-07-07 23:53:08 -07003087 if (!UpdateRegisters(abs_offset, work_line_.get(), false)) {
jeffhaobdb76512011-09-07 11:43:16 -07003088 return false;
Ian Rogersebbdd872014-07-07 23:53:08 -07003089 }
jeffhaobdb76512011-09-07 11:43:16 -07003090 }
3091 }
3092
3093 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003094 * Handle instructions that can throw and that are sitting in a "try" block. (If they're not in a
3095 * "try" block when they throw, control transfers out of the method.)
jeffhaobdb76512011-09-07 11:43:16 -07003096 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003097 if ((opcode_flags & Instruction::kThrow) != 0 && insn_flags_[work_insn_idx_].IsInTry()) {
Andreas Gampef91baf12014-07-18 15:41:00 -07003098 bool has_catch_all_handler = false;
Ian Rogers0571d352011-11-03 19:51:38 -07003099 CatchHandlerIterator iterator(*code_item_, work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07003100
Andreas Gampef91baf12014-07-18 15:41:00 -07003101 // Need the linker to try and resolve the handled class to check if it's Throwable.
3102 ClassLinker* linker = Runtime::Current()->GetClassLinker();
3103
Ian Rogers0571d352011-11-03 19:51:38 -07003104 for (; iterator.HasNext(); iterator.Next()) {
Andreas Gampef91baf12014-07-18 15:41:00 -07003105 uint16_t handler_type_idx = iterator.GetHandlerTypeIndex();
3106 if (handler_type_idx == DexFile::kDexNoIndex16) {
3107 has_catch_all_handler = true;
3108 } else {
3109 // It is also a catch-all if it is java.lang.Throwable.
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003110 mirror::Class* klass = linker->ResolveType(*dex_file_, handler_type_idx, dex_cache_,
3111 class_loader_);
Andreas Gampef91baf12014-07-18 15:41:00 -07003112 if (klass != nullptr) {
3113 if (klass == mirror::Throwable::GetJavaLangThrowable()) {
3114 has_catch_all_handler = true;
3115 }
3116 } else {
3117 // Clear exception.
Ian Rogers7b078e82014-09-10 14:44:24 -07003118 DCHECK(self_->IsExceptionPending());
3119 self_->ClearException();
Andreas Gampef91baf12014-07-18 15:41:00 -07003120 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003121 }
jeffhaobdb76512011-09-07 11:43:16 -07003122 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003123 * Merge registers into the "catch" block. We want to use the "savedRegs" rather than
3124 * "work_regs", because at runtime the exception will be thrown before the instruction
3125 * modifies any registers.
jeffhaobdb76512011-09-07 11:43:16 -07003126 */
Ian Rogersebbdd872014-07-07 23:53:08 -07003127 if (!UpdateRegisters(iterator.GetHandlerAddress(), saved_line_.get(), false)) {
jeffhaobdb76512011-09-07 11:43:16 -07003128 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07003129 }
jeffhaobdb76512011-09-07 11:43:16 -07003130 }
3131
3132 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003133 * If the monitor stack depth is nonzero, there must be a "catch all" handler for this
3134 * instruction. This does apply to monitor-exit because of async exception handling.
jeffhaobdb76512011-09-07 11:43:16 -07003135 */
Andreas Gampef91baf12014-07-18 15:41:00 -07003136 if (work_line_->MonitorStackDepth() > 0 && !has_catch_all_handler) {
jeffhaobdb76512011-09-07 11:43:16 -07003137 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003138 * The state in work_line reflects the post-execution state. If the current instruction is a
3139 * monitor-enter and the monitor stack was empty, we don't need a catch-all (if it throws,
jeffhaobdb76512011-09-07 11:43:16 -07003140 * it will do so before grabbing the lock).
3141 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02003142 if (inst->Opcode() != Instruction::MONITOR_ENTER || work_line_->MonitorStackDepth() != 1) {
jeffhaod5347e02012-03-22 17:25:05 -07003143 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
Ian Rogersd81871c2011-10-03 13:57:23 -07003144 << "expected to be within a catch-all for an instruction where a monitor is held";
jeffhaobdb76512011-09-07 11:43:16 -07003145 return false;
3146 }
3147 }
3148 }
3149
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003150 /* Handle "continue". Tag the next consecutive instruction.
3151 * Note: Keep the code handling "continue" case below the "branch" and "switch" cases,
3152 * because it changes work_line_ when performing peephole optimization
3153 * and this change should not be used in those cases.
3154 */
Ian Rogers6d376ae2013-07-23 15:12:40 -07003155 if ((opcode_flags & Instruction::kContinue) != 0) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003156 DCHECK_EQ(Instruction::At(code_item_->insns_ + work_insn_idx_), inst);
3157 uint32_t next_insn_idx = work_insn_idx_ + inst->SizeInCodeUnits();
Ian Rogers6d376ae2013-07-23 15:12:40 -07003158 if (next_insn_idx >= code_item_->insns_size_in_code_units_) {
3159 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Execution can walk off end of code area";
3160 return false;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003161 }
Ian Rogers6d376ae2013-07-23 15:12:40 -07003162 // The only way to get to a move-exception instruction is to get thrown there. Make sure the
3163 // next instruction isn't one.
3164 if (!CheckNotMoveException(code_item_->insns_, next_insn_idx)) {
3165 return false;
3166 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003167 if (nullptr != fallthrough_line.get()) {
Ian Rogers6d376ae2013-07-23 15:12:40 -07003168 // Make workline consistent with fallthrough computed from peephole optimization.
3169 work_line_->CopyFromLine(fallthrough_line.get());
3170 }
Ian Rogersb8c78592013-07-25 23:52:52 +00003171 if (insn_flags_[next_insn_idx].IsReturn()) {
3172 // For returns we only care about the operand to the return, all other registers are dead.
3173 const Instruction* ret_inst = Instruction::At(code_item_->insns_ + next_insn_idx);
3174 Instruction::Code opcode = ret_inst->Opcode();
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07003175 if (opcode == Instruction::RETURN_VOID || opcode == Instruction::RETURN_VOID_NO_BARRIER) {
Stephen Kyle7e541c92014-12-17 17:10:02 +00003176 SafelyMarkAllRegistersAsConflicts(this, work_line_.get());
Ian Rogersb8c78592013-07-25 23:52:52 +00003177 } else {
3178 if (opcode == Instruction::RETURN_WIDE) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003179 work_line_->MarkAllRegistersAsConflictsExceptWide(this, ret_inst->VRegA_11x());
Ian Rogersb8c78592013-07-25 23:52:52 +00003180 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07003181 work_line_->MarkAllRegistersAsConflictsExcept(this, ret_inst->VRegA_11x());
Ian Rogersb8c78592013-07-25 23:52:52 +00003182 }
3183 }
3184 }
Ian Rogers6d376ae2013-07-23 15:12:40 -07003185 RegisterLine* next_line = reg_table_.GetLine(next_insn_idx);
Ian Rogers7b078e82014-09-10 14:44:24 -07003186 if (next_line != nullptr) {
Ian Rogersebbdd872014-07-07 23:53:08 -07003187 // Merge registers into what we have for the next instruction, and set the "changed" flag if
3188 // needed. If the merge changes the state of the registers then the work line will be
3189 // updated.
3190 if (!UpdateRegisters(next_insn_idx, work_line_.get(), true)) {
Ian Rogers6d376ae2013-07-23 15:12:40 -07003191 return false;
3192 }
3193 } else {
3194 /*
3195 * We're not recording register data for the next instruction, so we don't know what the
3196 * prior state was. We have to assume that something has changed and re-evaluate it.
3197 */
3198 insn_flags_[next_insn_idx].SetChanged();
3199 }
3200 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003201
jeffhaod1f0fde2011-09-08 17:25:33 -07003202 /* If we're returning from the method, make sure monitor stack is empty. */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003203 if ((opcode_flags & Instruction::kReturn) != 0) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003204 if (!work_line_->VerifyMonitorStackEmpty(this)) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003205 return false;
3206 }
jeffhaobdb76512011-09-07 11:43:16 -07003207 }
3208
3209 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003210 * Update start_guess. Advance to the next instruction of that's
3211 * possible, otherwise use the branch target if one was found. If
jeffhaobdb76512011-09-07 11:43:16 -07003212 * neither of those exists we're in a return or throw; leave start_guess
3213 * alone and let the caller sort it out.
3214 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003215 if ((opcode_flags & Instruction::kContinue) != 0) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003216 DCHECK_EQ(Instruction::At(code_item_->insns_ + work_insn_idx_), inst);
3217 *start_guess = work_insn_idx_ + inst->SizeInCodeUnits();
Elliott Hughesadb8c672012-03-06 16:49:32 -08003218 } else if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07003219 /* we're still okay if branch_target is zero */
Ian Rogersd81871c2011-10-03 13:57:23 -07003220 *start_guess = work_insn_idx_ + branch_target;
jeffhaobdb76512011-09-07 11:43:16 -07003221 }
3222
Ian Rogersd81871c2011-10-03 13:57:23 -07003223 DCHECK_LT(*start_guess, code_item_->insns_size_in_code_units_);
3224 DCHECK(insn_flags_[*start_guess].IsOpcode());
jeffhaobdb76512011-09-07 11:43:16 -07003225
3226 return true;
Brian Carlstrom7934ac22013-07-26 10:54:15 -07003227} // NOLINT(readability/fn_size)
jeffhaobdb76512011-09-07 11:43:16 -07003228
Ian Rogersd8f69b02014-09-10 21:43:52 +00003229const RegType& MethodVerifier::ResolveClassAndCheckAccess(uint32_t class_idx) {
Ian Rogers0571d352011-11-03 19:51:38 -07003230 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
Ian Rogersd8f69b02014-09-10 21:43:52 +00003231 const RegType& referrer = GetDeclaringClass();
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003232 mirror::Class* klass = dex_cache_->GetResolvedType(class_idx);
Ian Rogers7b078e82014-09-10 14:44:24 -07003233 const RegType& result = klass != nullptr ?
Andreas Gampef23f33d2015-06-23 14:18:17 -07003234 FromClass(descriptor, klass, klass->CannotBeAssignedFromOtherTypes()) :
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003235 reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003236 if (result.IsConflict()) {
3237 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "accessing broken descriptor '" << descriptor
3238 << "' in " << referrer;
3239 return result;
3240 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003241 if (klass == nullptr && !result.IsUnresolvedTypes()) {
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003242 dex_cache_->SetResolvedType(class_idx, result.GetClass());
Ian Rogerse1758fe2012-04-19 11:31:15 -07003243 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003244 // Check if access is allowed. Unresolved types use xxxWithAccessCheck to
Jeff Haob24b4a72013-07-31 13:47:31 -07003245 // check at runtime if access is allowed and so pass here. If result is
3246 // primitive, skip the access check.
3247 if (result.IsNonZeroReferenceTypes() && !result.IsUnresolvedTypes() &&
3248 !referrer.IsUnresolvedTypes() && !referrer.CanAccess(result)) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07003249 Fail(VERIFY_ERROR_ACCESS_CLASS) << "illegal class access: '"
Ian Rogersad0b3a32012-04-16 14:50:24 -07003250 << referrer << "' -> '" << result << "'";
Ian Rogers28ad40d2011-10-27 15:19:26 -07003251 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003252 return result;
Ian Rogersd81871c2011-10-03 13:57:23 -07003253}
3254
Ian Rogersd8f69b02014-09-10 21:43:52 +00003255const RegType& MethodVerifier::GetCaughtExceptionType() {
Ian Rogers7b078e82014-09-10 14:44:24 -07003256 const RegType* common_super = nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003257 if (code_item_->tries_size_ != 0) {
Ian Rogers13735952014-10-08 12:43:28 -07003258 const uint8_t* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
Ian Rogersd81871c2011-10-03 13:57:23 -07003259 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
3260 for (uint32_t i = 0; i < handlers_size; i++) {
Ian Rogers0571d352011-11-03 19:51:38 -07003261 CatchHandlerIterator iterator(handlers_ptr);
3262 for (; iterator.HasNext(); iterator.Next()) {
3263 if (iterator.GetHandlerAddress() == (uint32_t) work_insn_idx_) {
3264 if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) {
Ian Rogersb4903572012-10-11 11:52:56 -07003265 common_super = &reg_types_.JavaLangThrowable(false);
Ian Rogersd81871c2011-10-03 13:57:23 -07003266 } else {
Ian Rogersd8f69b02014-09-10 21:43:52 +00003267 const RegType& exception = ResolveClassAndCheckAccess(iterator.GetHandlerTypeIndex());
Jeff Haob878f212014-04-24 16:25:36 -07003268 if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(exception)) {
Jeff Haoc26a56c2013-11-04 12:00:47 -08003269 if (exception.IsUnresolvedTypes()) {
3270 // We don't know enough about the type. Fail here and let runtime handle it.
3271 Fail(VERIFY_ERROR_NO_CLASS) << "unresolved exception class " << exception;
3272 return exception;
3273 } else {
3274 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unexpected non-exception class " << exception;
3275 return reg_types_.Conflict();
3276 }
Jeff Haob878f212014-04-24 16:25:36 -07003277 } else if (common_super == nullptr) {
3278 common_super = &exception;
Ian Rogers28ad40d2011-10-27 15:19:26 -07003279 } else if (common_super->Equals(exception)) {
Ian Rogersc4762272012-02-01 15:55:55 -08003280 // odd case, but nothing to do
Ian Rogersd81871c2011-10-03 13:57:23 -07003281 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07003282 common_super = &common_super->Merge(exception, &reg_types_);
Andreas Gampe7c038102014-10-27 20:08:46 -07003283 if (FailOrAbort(this,
3284 reg_types_.JavaLangThrowable(false).IsAssignableFrom(*common_super),
3285 "java.lang.Throwable is not assignable-from common_super at ",
3286 work_insn_idx_)) {
3287 break;
3288 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003289 }
3290 }
3291 }
3292 }
Ian Rogers0571d352011-11-03 19:51:38 -07003293 handlers_ptr = iterator.EndDataPointer();
Ian Rogersd81871c2011-10-03 13:57:23 -07003294 }
3295 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003296 if (common_super == nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003297 /* no catch blocks, or no catches with classes we can find */
jeffhaod5347e02012-03-22 17:25:05 -07003298 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unable to find exception handler";
Ian Rogersad0b3a32012-04-16 14:50:24 -07003299 return reg_types_.Conflict();
Ian Rogersd81871c2011-10-03 13:57:23 -07003300 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07003301 return *common_super;
Ian Rogersd81871c2011-10-03 13:57:23 -07003302}
3303
Mathieu Chartiere401d142015-04-22 13:56:20 -07003304ArtMethod* MethodVerifier::ResolveMethodAndCheckAccess(
3305 uint32_t dex_method_idx, MethodType method_type) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003306 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx);
Ian Rogersd8f69b02014-09-10 21:43:52 +00003307 const RegType& klass_type = ResolveClassAndCheckAccess(method_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003308 if (klass_type.IsConflict()) {
3309 std::string append(" in attempt to access method ");
3310 append += dex_file_->GetMethodName(method_id);
3311 AppendToLastFailMessage(append);
Ian Rogers7b078e82014-09-10 14:44:24 -07003312 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08003313 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003314 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003315 return nullptr; // Can't resolve Class so no more to do here
Ian Rogers90040192011-12-16 08:54:29 -08003316 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003317 mirror::Class* klass = klass_type.GetClass();
Ian Rogersd8f69b02014-09-10 21:43:52 +00003318 const RegType& referrer = GetDeclaringClass();
Mathieu Chartiere401d142015-04-22 13:56:20 -07003319 auto* cl = Runtime::Current()->GetClassLinker();
3320 auto pointer_size = cl->GetImagePointerSize();
3321 ArtMethod* res_method = dex_cache_->GetResolvedMethod(dex_method_idx, pointer_size);
Ian Rogers7b078e82014-09-10 14:44:24 -07003322 if (res_method == nullptr) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07003323 const char* name = dex_file_->GetMethodName(method_id);
Ian Rogersd91d6d62013-09-25 20:26:14 -07003324 const Signature signature = dex_file_->GetMethodSignature(method_id);
jeffhao8cd6dda2012-02-22 10:15:34 -08003325
3326 if (method_type == METHOD_DIRECT || method_type == METHOD_STATIC) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003327 res_method = klass->FindDirectMethod(name, signature, pointer_size);
jeffhao8cd6dda2012-02-22 10:15:34 -08003328 } else if (method_type == METHOD_INTERFACE) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003329 res_method = klass->FindInterfaceMethod(name, signature, pointer_size);
Ian Rogersd81871c2011-10-03 13:57:23 -07003330 } else {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003331 res_method = klass->FindVirtualMethod(name, signature, pointer_size);
Ian Rogersd81871c2011-10-03 13:57:23 -07003332 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003333 if (res_method != nullptr) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003334 dex_cache_->SetResolvedMethod(dex_method_idx, res_method, pointer_size);
Ian Rogersd81871c2011-10-03 13:57:23 -07003335 } else {
jeffhao8cd6dda2012-02-22 10:15:34 -08003336 // If a virtual or interface method wasn't found with the expected type, look in
3337 // the direct methods. This can happen when the wrong invoke type is used or when
3338 // a class has changed, and will be flagged as an error in later checks.
3339 if (method_type == METHOD_INTERFACE || method_type == METHOD_VIRTUAL) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003340 res_method = klass->FindDirectMethod(name, signature, pointer_size);
jeffhao8cd6dda2012-02-22 10:15:34 -08003341 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003342 if (res_method == nullptr) {
jeffhao8cd6dda2012-02-22 10:15:34 -08003343 Fail(VERIFY_ERROR_NO_METHOD) << "couldn't find method "
3344 << PrettyDescriptor(klass) << "." << name
3345 << " " << signature;
Ian Rogers7b078e82014-09-10 14:44:24 -07003346 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003347 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003348 }
3349 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003350 // Make sure calls to constructors are "direct". There are additional restrictions but we don't
3351 // enforce them here.
3352 if (res_method->IsConstructor() && method_type != METHOD_DIRECT) {
jeffhaod5347e02012-03-22 17:25:05 -07003353 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting non-direct call to constructor "
3354 << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003355 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003356 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003357 // Disallow any calls to class initializers.
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003358 if (res_method->IsClassInitializer()) {
jeffhaod5347e02012-03-22 17:25:05 -07003359 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting call to class initializer "
3360 << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003361 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003362 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003363 // Check if access is allowed.
Ian Rogersad0b3a32012-04-16 14:50:24 -07003364 if (!referrer.CanAccessMember(res_method->GetDeclaringClass(), res_method->GetAccessFlags())) {
jeffhao8cd6dda2012-02-22 10:15:34 -08003365 Fail(VERIFY_ERROR_ACCESS_METHOD) << "illegal method access (call " << PrettyMethod(res_method)
Ian Rogersad0b3a32012-04-16 14:50:24 -07003366 << " from " << referrer << ")";
jeffhaob57e9522012-04-26 18:08:21 -07003367 return res_method;
jeffhao8cd6dda2012-02-22 10:15:34 -08003368 }
jeffhaode0d9c92012-02-27 13:58:13 -08003369 // Check that invoke-virtual and invoke-super are not used on private methods of the same class.
3370 if (res_method->IsPrivate() && method_type == METHOD_VIRTUAL) {
jeffhaod5347e02012-03-22 17:25:05 -07003371 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invoke-super/virtual can't be used on private method "
3372 << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003373 return nullptr;
jeffhaode0d9c92012-02-27 13:58:13 -08003374 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003375 // Check that interface methods match interface classes.
3376 if (klass->IsInterface() && method_type != METHOD_INTERFACE) {
3377 Fail(VERIFY_ERROR_CLASS_CHANGE) << "non-interface method " << PrettyMethod(res_method)
3378 << " is in an interface class " << PrettyClass(klass);
Ian Rogers7b078e82014-09-10 14:44:24 -07003379 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003380 } else if (!klass->IsInterface() && method_type == METHOD_INTERFACE) {
3381 Fail(VERIFY_ERROR_CLASS_CHANGE) << "interface method " << PrettyMethod(res_method)
3382 << " is in a non-interface class " << PrettyClass(klass);
Ian Rogers7b078e82014-09-10 14:44:24 -07003383 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003384 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003385 // See if the method type implied by the invoke instruction matches the access flags for the
3386 // target method.
Brian Carlstrombe6fa5e2014-12-09 20:15:42 -08003387 if ((method_type == METHOD_DIRECT && (!res_method->IsDirect() || res_method->IsStatic())) ||
Ian Rogersd81871c2011-10-03 13:57:23 -07003388 (method_type == METHOD_STATIC && !res_method->IsStatic()) ||
3389 ((method_type == METHOD_VIRTUAL || method_type == METHOD_INTERFACE) && res_method->IsDirect())
3390 ) {
Ian Rogers2fc14272012-08-30 10:56:57 -07003391 Fail(VERIFY_ERROR_CLASS_CHANGE) << "invoke type (" << method_type << ") does not match method "
3392 " type of " << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003393 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003394 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003395 return res_method;
3396}
3397
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003398template <class T>
Mathieu Chartiere401d142015-04-22 13:56:20 -07003399ArtMethod* MethodVerifier::VerifyInvocationArgsFromIterator(
3400 T* it, const Instruction* inst, MethodType method_type, bool is_range, ArtMethod* res_method) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003401 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
3402 // match the call to the signature. Also, we might be calling through an abstract method
3403 // definition (which doesn't have register count values).
3404 const size_t expected_args = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
3405 /* caught by static verifier */
3406 DCHECK(is_range || expected_args <= 5);
3407 if (expected_args > code_item_->outs_size_) {
3408 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
3409 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
3410 return nullptr;
3411 }
3412
3413 uint32_t arg[5];
3414 if (!is_range) {
3415 inst->GetVarArgs(arg);
3416 }
3417 uint32_t sig_registers = 0;
3418
3419 /*
3420 * Check the "this" argument, which must be an instance of the class that declared the method.
3421 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
3422 * rigorous check here (which is okay since we have to do it at runtime).
3423 */
3424 if (method_type != METHOD_STATIC) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003425 const RegType& actual_arg_type = work_line_->GetInvocationThis(this, inst, is_range);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003426 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
3427 CHECK(have_pending_hard_failure_);
3428 return nullptr;
3429 }
3430 if (actual_arg_type.IsUninitializedReference()) {
3431 if (res_method) {
3432 if (!res_method->IsConstructor()) {
3433 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
3434 return nullptr;
3435 }
3436 } else {
3437 // Check whether the name of the called method is "<init>"
3438 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Jeff Hao0d087272014-08-04 14:47:17 -07003439 if (strcmp(dex_file_->GetMethodName(dex_file_->GetMethodId(method_idx)), "<init>") != 0) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003440 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
3441 return nullptr;
3442 }
3443 }
3444 }
3445 if (method_type != METHOD_INTERFACE && !actual_arg_type.IsZero()) {
Ian Rogersd8f69b02014-09-10 21:43:52 +00003446 const RegType* res_method_class;
Andreas Gamped9e23012015-06-04 22:19:58 -07003447 // Miranda methods have the declaring interface as their declaring class, not the abstract
3448 // class. It would be wrong to use this for the type check (interface type checks are
3449 // postponed to runtime).
3450 if (res_method != nullptr && !res_method->IsMiranda()) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003451 mirror::Class* klass = res_method->GetDeclaringClass();
Ian Rogers1ff3c982014-08-12 02:30:58 -07003452 std::string temp;
Andreas Gampef23f33d2015-06-23 14:18:17 -07003453 res_method_class = &FromClass(klass->GetDescriptor(&temp), klass,
3454 klass->CannotBeAssignedFromOtherTypes());
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003455 } else {
3456 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
3457 const uint16_t class_idx = dex_file_->GetMethodId(method_idx).class_idx_;
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003458 res_method_class = &reg_types_.FromDescriptor(GetClassLoader(),
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003459 dex_file_->StringByTypeIdx(class_idx),
3460 false);
3461 }
3462 if (!res_method_class->IsAssignableFrom(actual_arg_type)) {
3463 Fail(actual_arg_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS:
3464 VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type
3465 << "' not instance of '" << *res_method_class << "'";
3466 // Continue on soft failures. We need to find possible hard failures to avoid problems in
3467 // the compiler.
3468 if (have_pending_hard_failure_) {
3469 return nullptr;
3470 }
3471 }
3472 }
3473 sig_registers = 1;
3474 }
3475
3476 for ( ; it->HasNext(); it->Next()) {
3477 if (sig_registers >= expected_args) {
3478 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, expected " << inst->VRegA() <<
3479 " arguments, found " << sig_registers << " or more.";
3480 return nullptr;
3481 }
3482
3483 const char* param_descriptor = it->GetDescriptor();
3484
3485 if (param_descriptor == nullptr) {
3486 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation because of missing signature "
3487 "component";
3488 return nullptr;
3489 }
3490
Ian Rogersd8f69b02014-09-10 21:43:52 +00003491 const RegType& reg_type = reg_types_.FromDescriptor(GetClassLoader(), param_descriptor, false);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003492 uint32_t get_reg = is_range ? inst->VRegC_3rc() + static_cast<uint32_t>(sig_registers) :
3493 arg[sig_registers];
3494 if (reg_type.IsIntegralTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003495 const RegType& src_type = work_line_->GetRegisterType(this, get_reg);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003496 if (!src_type.IsIntegralTypes()) {
3497 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register v" << get_reg << " has type " << src_type
3498 << " but expected " << reg_type;
Andreas Gampeb588f4c2015-05-26 13:35:39 -07003499 return nullptr;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003500 }
Andreas Gampeda9badb2015-06-05 20:22:12 -07003501 } else {
3502 if (!work_line_->VerifyRegisterType(this, get_reg, reg_type)) {
3503 // Continue on soft failures. We need to find possible hard failures to avoid problems in
3504 // the compiler.
3505 if (have_pending_hard_failure_) {
3506 return nullptr;
3507 }
3508 } else if (reg_type.IsLongOrDoubleTypes()) {
3509 // Check that registers are consecutive (for non-range invokes). Invokes are the only
3510 // instructions not specifying register pairs by the first component, but require them
3511 // nonetheless. Only check when there's an actual register in the parameters. If there's
3512 // none, this will fail below.
3513 if (!is_range && sig_registers + 1 < expected_args) {
3514 uint32_t second_reg = arg[sig_registers + 1];
3515 if (second_reg != get_reg + 1) {
3516 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, long or double parameter "
3517 "at index " << sig_registers << " is not a pair: " << get_reg << " + "
3518 << second_reg << ".";
3519 return nullptr;
3520 }
3521 }
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003522 }
3523 }
3524 sig_registers += reg_type.IsLongOrDoubleTypes() ? 2 : 1;
3525 }
3526 if (expected_args != sig_registers) {
3527 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, expected " << expected_args <<
3528 " arguments, found " << sig_registers;
3529 return nullptr;
3530 }
3531 return res_method;
3532}
3533
3534void MethodVerifier::VerifyInvocationArgsUnresolvedMethod(const Instruction* inst,
3535 MethodType method_type,
3536 bool is_range) {
3537 // As the method may not have been resolved, make this static check against what we expect.
3538 // The main reason for this code block is to fail hard when we find an illegal use, e.g.,
3539 // wrong number of arguments or wrong primitive types, even if the method could not be resolved.
3540 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
3541 DexFileParameterIterator it(*dex_file_,
3542 dex_file_->GetProtoId(dex_file_->GetMethodId(method_idx).proto_idx_));
3543 VerifyInvocationArgsFromIterator<DexFileParameterIterator>(&it, inst, method_type, is_range,
3544 nullptr);
3545}
3546
3547class MethodParamListDescriptorIterator {
3548 public:
Mathieu Chartiere401d142015-04-22 13:56:20 -07003549 explicit MethodParamListDescriptorIterator(ArtMethod* res_method) :
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003550 res_method_(res_method), pos_(0), params_(res_method->GetParameterTypeList()),
3551 params_size_(params_ == nullptr ? 0 : params_->Size()) {
3552 }
3553
3554 bool HasNext() {
3555 return pos_ < params_size_;
3556 }
3557
3558 void Next() {
3559 ++pos_;
3560 }
3561
3562 const char* GetDescriptor() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
3563 return res_method_->GetTypeDescriptorFromTypeIdx(params_->GetTypeItem(pos_).type_idx_);
3564 }
3565
3566 private:
Mathieu Chartiere401d142015-04-22 13:56:20 -07003567 ArtMethod* res_method_;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003568 size_t pos_;
3569 const DexFile::TypeList* params_;
3570 const size_t params_size_;
3571};
3572
Mathieu Chartiere401d142015-04-22 13:56:20 -07003573ArtMethod* MethodVerifier::VerifyInvocationArgs(
3574 const Instruction* inst, MethodType method_type, bool is_range, bool is_super) {
jeffhao8cd6dda2012-02-22 10:15:34 -08003575 // Resolve the method. This could be an abstract or concrete method depending on what sort of call
3576 // we're making.
Sebastien Hertz5243e912013-05-21 10:55:07 +02003577 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Andreas Gampeacc4d2f2014-06-12 19:35:05 -07003578
Mathieu Chartiere401d142015-04-22 13:56:20 -07003579 ArtMethod* res_method = ResolveMethodAndCheckAccess(method_idx, method_type);
Ian Rogers7b078e82014-09-10 14:44:24 -07003580 if (res_method == nullptr) { // error or class is unresolved
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003581 // Check what we can statically.
3582 if (!have_pending_hard_failure_) {
3583 VerifyInvocationArgsUnresolvedMethod(inst, method_type, is_range);
3584 }
3585 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003586 }
3587
Ian Rogersd81871c2011-10-03 13:57:23 -07003588 // If we're using invoke-super(method), make sure that the executing method's class' superclass
3589 // has a vtable entry for the target method.
3590 if (is_super) {
3591 DCHECK(method_type == METHOD_VIRTUAL);
Ian Rogersd8f69b02014-09-10 21:43:52 +00003592 const RegType& super = GetDeclaringClass().GetSuperClass(&reg_types_);
Ian Rogers529781d2012-07-23 17:24:29 -07003593 if (super.IsUnresolvedTypes()) {
jeffhao4d8df822012-04-24 17:09:36 -07003594 Fail(VERIFY_ERROR_NO_METHOD) << "unknown super class in invoke-super from "
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003595 << PrettyMethod(dex_method_idx_, *dex_file_)
jeffhao4d8df822012-04-24 17:09:36 -07003596 << " to super " << PrettyMethod(res_method);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003597 return nullptr;
jeffhao4d8df822012-04-24 17:09:36 -07003598 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003599 mirror::Class* super_klass = super.GetClass();
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003600 if (res_method->GetMethodIndex() >= super_klass->GetVTableLength()) {
jeffhao4d8df822012-04-24 17:09:36 -07003601 Fail(VERIFY_ERROR_NO_METHOD) << "invalid invoke-super from "
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003602 << PrettyMethod(dex_method_idx_, *dex_file_)
jeffhao4d8df822012-04-24 17:09:36 -07003603 << " to super " << super
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003604 << "." << res_method->GetName()
3605 << res_method->GetSignature();
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003606 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003607 }
3608 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003609
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003610 // Process the target method's signature. This signature may or may not
3611 MethodParamListDescriptorIterator it(res_method);
3612 return VerifyInvocationArgsFromIterator<MethodParamListDescriptorIterator>(&it, inst, method_type,
3613 is_range, res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07003614}
3615
Mathieu Chartiere401d142015-04-22 13:56:20 -07003616ArtMethod* MethodVerifier::GetQuickInvokedMethod(const Instruction* inst, RegisterLine* reg_line,
3617 bool is_range, bool allow_failure) {
Mathieu Chartier091d2382015-03-06 10:59:06 -08003618 if (is_range) {
3619 DCHECK_EQ(inst->Opcode(), Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
3620 } else {
3621 DCHECK_EQ(inst->Opcode(), Instruction::INVOKE_VIRTUAL_QUICK);
3622 }
3623 const RegType& actual_arg_type = reg_line->GetInvocationThis(this, inst, is_range, allow_failure);
Ian Rogers9bc54402014-04-17 16:40:01 -07003624 if (!actual_arg_type.HasClass()) {
3625 VLOG(verifier) << "Failed to get mirror::Class* from '" << actual_arg_type << "'";
Andreas Gampe63981562014-04-17 12:28:43 -07003626 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003627 }
Ian Rogersa4cf1df2014-05-07 19:47:17 -07003628 mirror::Class* klass = actual_arg_type.GetClass();
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003629 mirror::Class* dispatch_class;
Ian Rogersa4cf1df2014-05-07 19:47:17 -07003630 if (klass->IsInterface()) {
3631 // Derive Object.class from Class.class.getSuperclass().
3632 mirror::Class* object_klass = klass->GetClass()->GetSuperClass();
Andreas Gampe7c038102014-10-27 20:08:46 -07003633 if (FailOrAbort(this, object_klass->IsObjectClass(),
Mathieu Chartier091d2382015-03-06 10:59:06 -08003634 "Failed to find Object class in quickened invoke receiver", work_insn_idx_)) {
Andreas Gampe7c038102014-10-27 20:08:46 -07003635 return nullptr;
3636 }
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003637 dispatch_class = object_klass;
Ian Rogersa4cf1df2014-05-07 19:47:17 -07003638 } else {
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003639 dispatch_class = klass;
Ian Rogersa4cf1df2014-05-07 19:47:17 -07003640 }
Mathieu Chartier091d2382015-03-06 10:59:06 -08003641 if (!dispatch_class->HasVTable()) {
3642 FailOrAbort(this, allow_failure, "Receiver class has no vtable for quickened invoke at ",
3643 work_insn_idx_);
Andreas Gampe7c038102014-10-27 20:08:46 -07003644 return nullptr;
3645 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003646 uint16_t vtable_index = is_range ? inst->VRegB_3rc() : inst->VRegB_35c();
Mathieu Chartiere401d142015-04-22 13:56:20 -07003647 auto* cl = Runtime::Current()->GetClassLinker();
3648 auto pointer_size = cl->GetImagePointerSize();
Mathieu Chartier091d2382015-03-06 10:59:06 -08003649 if (static_cast<int32_t>(vtable_index) >= dispatch_class->GetVTableLength()) {
3650 FailOrAbort(this, allow_failure,
3651 "Receiver class has not enough vtable slots for quickened invoke at ",
3652 work_insn_idx_);
Andreas Gampe7c038102014-10-27 20:08:46 -07003653 return nullptr;
3654 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07003655 ArtMethod* res_method = dispatch_class->GetVTableEntry(vtable_index, pointer_size);
Mathieu Chartier091d2382015-03-06 10:59:06 -08003656 if (self_->IsExceptionPending()) {
3657 FailOrAbort(this, allow_failure, "Unexpected exception pending for quickened invoke at ",
3658 work_insn_idx_);
Andreas Gampe7c038102014-10-27 20:08:46 -07003659 return nullptr;
3660 }
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003661 return res_method;
3662}
3663
Mathieu Chartiere401d142015-04-22 13:56:20 -07003664ArtMethod* MethodVerifier::VerifyInvokeVirtualQuickArgs(const Instruction* inst, bool is_range) {
Andreas Gampe76bd8802014-12-10 16:43:58 -08003665 DCHECK(Runtime::Current()->IsStarted() || verify_to_dump_)
3666 << PrettyMethod(dex_method_idx_, *dex_file_, true) << "@" << work_insn_idx_;
3667
Mathieu Chartiere401d142015-04-22 13:56:20 -07003668 ArtMethod* res_method = GetQuickInvokedMethod(inst, work_line_.get(), is_range, false);
Ian Rogers7b078e82014-09-10 14:44:24 -07003669 if (res_method == nullptr) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003670 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer method from " << inst->Name();
Ian Rogers7b078e82014-09-10 14:44:24 -07003671 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003672 }
Andreas Gampe7c038102014-10-27 20:08:46 -07003673 if (FailOrAbort(this, !res_method->IsDirect(), "Quick-invoked method is direct at ",
3674 work_insn_idx_)) {
3675 return nullptr;
3676 }
3677 if (FailOrAbort(this, !res_method->IsStatic(), "Quick-invoked method is static at ",
3678 work_insn_idx_)) {
3679 return nullptr;
3680 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003681
3682 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
3683 // match the call to the signature. Also, we might be calling through an abstract method
3684 // definition (which doesn't have register count values).
Ian Rogers7b078e82014-09-10 14:44:24 -07003685 const RegType& actual_arg_type = work_line_->GetInvocationThis(this, inst, is_range);
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003686 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
Ian Rogers7b078e82014-09-10 14:44:24 -07003687 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003688 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003689 const size_t expected_args = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
3690 /* caught by static verifier */
3691 DCHECK(is_range || expected_args <= 5);
3692 if (expected_args > code_item_->outs_size_) {
3693 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
3694 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
Ian Rogers7b078e82014-09-10 14:44:24 -07003695 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003696 }
3697
3698 /*
3699 * Check the "this" argument, which must be an instance of the class that declared the method.
3700 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
3701 * rigorous check here (which is okay since we have to do it at runtime).
3702 */
3703 if (actual_arg_type.IsUninitializedReference() && !res_method->IsConstructor()) {
3704 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
Ian Rogers7b078e82014-09-10 14:44:24 -07003705 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003706 }
3707 if (!actual_arg_type.IsZero()) {
3708 mirror::Class* klass = res_method->GetDeclaringClass();
Ian Rogers1ff3c982014-08-12 02:30:58 -07003709 std::string temp;
Ian Rogersd8f69b02014-09-10 21:43:52 +00003710 const RegType& res_method_class =
Andreas Gampef23f33d2015-06-23 14:18:17 -07003711 FromClass(klass->GetDescriptor(&temp), klass, klass->CannotBeAssignedFromOtherTypes());
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003712 if (!res_method_class.IsAssignableFrom(actual_arg_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07003713 Fail(actual_arg_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS :
3714 VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003715 << "' not instance of '" << res_method_class << "'";
Ian Rogers7b078e82014-09-10 14:44:24 -07003716 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003717 }
3718 }
3719 /*
3720 * Process the target method's signature. This signature may or may not
3721 * have been verified, so we can't assume it's properly formed.
3722 */
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003723 const DexFile::TypeList* params = res_method->GetParameterTypeList();
Ian Rogers7b078e82014-09-10 14:44:24 -07003724 size_t params_size = params == nullptr ? 0 : params->Size();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003725 uint32_t arg[5];
3726 if (!is_range) {
Ian Rogers29a26482014-05-02 15:27:29 -07003727 inst->GetVarArgs(arg);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003728 }
3729 size_t actual_args = 1;
3730 for (size_t param_index = 0; param_index < params_size; param_index++) {
3731 if (actual_args >= expected_args) {
3732 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invalid call to '" << PrettyMethod(res_method)
Brian Carlstrom93c33962013-07-26 10:37:43 -07003733 << "'. Expected " << expected_args
3734 << " arguments, processing argument " << actual_args
3735 << " (where longs/doubles count twice).";
Ian Rogers7b078e82014-09-10 14:44:24 -07003736 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003737 }
3738 const char* descriptor =
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003739 res_method->GetTypeDescriptorFromTypeIdx(params->GetTypeItem(param_index).type_idx_);
Ian Rogers7b078e82014-09-10 14:44:24 -07003740 if (descriptor == nullptr) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003741 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003742 << " missing signature component";
Ian Rogers7b078e82014-09-10 14:44:24 -07003743 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003744 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00003745 const RegType& reg_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003746 uint32_t get_reg = is_range ? inst->VRegC_3rc() + actual_args : arg[actual_args];
Ian Rogers7b078e82014-09-10 14:44:24 -07003747 if (!work_line_->VerifyRegisterType(this, get_reg, reg_type)) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003748 return res_method;
3749 }
3750 actual_args = reg_type.IsLongOrDoubleTypes() ? actual_args + 2 : actual_args + 1;
3751 }
3752 if (actual_args != expected_args) {
3753 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
3754 << " expected " << expected_args << " arguments, found " << actual_args;
Ian Rogers7b078e82014-09-10 14:44:24 -07003755 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003756 } else {
3757 return res_method;
3758 }
3759}
3760
Ian Rogers62342ec2013-06-11 10:26:37 -07003761void MethodVerifier::VerifyNewArray(const Instruction* inst, bool is_filled, bool is_range) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003762 uint32_t type_idx;
3763 if (!is_filled) {
3764 DCHECK_EQ(inst->Opcode(), Instruction::NEW_ARRAY);
3765 type_idx = inst->VRegC_22c();
3766 } else if (!is_range) {
3767 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY);
3768 type_idx = inst->VRegB_35c();
3769 } else {
3770 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY_RANGE);
3771 type_idx = inst->VRegB_3rc();
3772 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00003773 const RegType& res_type = ResolveClassAndCheckAccess(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003774 if (res_type.IsConflict()) { // bad class
3775 DCHECK_NE(failures_.size(), 0U);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003776 } else {
3777 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
3778 if (!res_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003779 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "new-array on non-array class " << res_type;
Ian Rogers0c4a5062012-02-03 15:18:59 -08003780 } else if (!is_filled) {
3781 /* make sure "size" register is valid type */
Ian Rogers7b078e82014-09-10 14:44:24 -07003782 work_line_->VerifyRegisterType(this, inst->VRegB_22c(), reg_types_.Integer());
Ian Rogers0c4a5062012-02-03 15:18:59 -08003783 /* set register type to array class */
Ian Rogersd8f69b02014-09-10 21:43:52 +00003784 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
Ian Rogers7b078e82014-09-10 14:44:24 -07003785 work_line_->SetRegisterType(this, inst->VRegA_22c(), precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003786 } else {
3787 // Verify each register. If "arg_count" is bad, VerifyRegisterType() will run off the end of
3788 // the list and fail. It's legal, if silly, for arg_count to be zero.
Ian Rogersd8f69b02014-09-10 21:43:52 +00003789 const RegType& expected_type = reg_types_.GetComponentType(res_type, GetClassLoader());
Sebastien Hertz5243e912013-05-21 10:55:07 +02003790 uint32_t arg_count = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
3791 uint32_t arg[5];
3792 if (!is_range) {
Ian Rogers29a26482014-05-02 15:27:29 -07003793 inst->GetVarArgs(arg);
Sebastien Hertz5243e912013-05-21 10:55:07 +02003794 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08003795 for (size_t ui = 0; ui < arg_count; ui++) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003796 uint32_t get_reg = is_range ? inst->VRegC_3rc() + ui : arg[ui];
Ian Rogers7b078e82014-09-10 14:44:24 -07003797 if (!work_line_->VerifyRegisterType(this, get_reg, expected_type)) {
3798 work_line_->SetResultRegisterType(this, reg_types_.Conflict());
Ian Rogers0c4a5062012-02-03 15:18:59 -08003799 return;
3800 }
3801 }
3802 // filled-array result goes into "result" register
Ian Rogersd8f69b02014-09-10 21:43:52 +00003803 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
Ian Rogers7b078e82014-09-10 14:44:24 -07003804 work_line_->SetResultRegisterType(this, precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003805 }
3806 }
3807}
3808
Sebastien Hertz5243e912013-05-21 10:55:07 +02003809void MethodVerifier::VerifyAGet(const Instruction* inst,
Ian Rogersd8f69b02014-09-10 21:43:52 +00003810 const RegType& insn_type, bool is_primitive) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003811 const RegType& index_type = work_line_->GetRegisterType(this, inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07003812 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003813 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07003814 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07003815 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegB_23x());
Ian Rogers89310de2012-02-01 13:47:30 -08003816 if (array_type.IsZero()) {
Nicolas Geoffray4824c272015-06-24 15:53:03 +01003817 have_pending_runtime_throw_failure_ = true;
Ian Rogers89310de2012-02-01 13:47:30 -08003818 // Null array class; this code path will fail at runtime. Infer a merge-able type from the
3819 // instruction type. TODO: have a proper notion of bottom here.
3820 if (!is_primitive || insn_type.IsCategory1Types()) {
3821 // Reference or category 1
Ian Rogers7b078e82014-09-10 14:44:24 -07003822 work_line_->SetRegisterType(this, inst->VRegA_23x(), reg_types_.Zero());
Ian Rogersd81871c2011-10-03 13:57:23 -07003823 } else {
Ian Rogers89310de2012-02-01 13:47:30 -08003824 // Category 2
Ian Rogers7b078e82014-09-10 14:44:24 -07003825 work_line_->SetRegisterTypeWide(this, inst->VRegA_23x(),
3826 reg_types_.FromCat2ConstLo(0, false),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003827 reg_types_.FromCat2ConstHi(0, false));
Ian Rogers89310de2012-02-01 13:47:30 -08003828 }
jeffhaofc3144e2012-02-01 17:21:15 -08003829 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003830 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aget";
Ian Rogers89310de2012-02-01 13:47:30 -08003831 } else {
3832 /* verify the class */
Ian Rogersd8f69b02014-09-10 21:43:52 +00003833 const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
jeffhaofc3144e2012-02-01 17:21:15 -08003834 if (!component_type.IsReferenceTypes() && !is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07003835 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08003836 << " source for aget-object";
3837 } else if (component_type.IsNonZeroReferenceTypes() && is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07003838 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08003839 << " source for category 1 aget";
3840 } else if (is_primitive && !insn_type.Equals(component_type) &&
3841 !((insn_type.IsInteger() && component_type.IsFloat()) ||
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003842 (insn_type.IsLong() && component_type.IsDouble()))) {
3843 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array type " << array_type
3844 << " incompatible with aget of type " << insn_type;
Ian Rogers89310de2012-02-01 13:47:30 -08003845 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003846 // Use knowledge of the field type which is stronger than the type inferred from the
3847 // instruction, which can't differentiate object types and ints from floats, longs from
3848 // doubles.
3849 if (!component_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003850 work_line_->SetRegisterType(this, inst->VRegA_23x(), component_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003851 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07003852 work_line_->SetRegisterTypeWide(this, inst->VRegA_23x(), component_type,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003853 component_type.HighHalf(&reg_types_));
3854 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003855 }
3856 }
3857 }
3858}
3859
Ian Rogersd8f69b02014-09-10 21:43:52 +00003860void MethodVerifier::VerifyPrimitivePut(const RegType& target_type, const RegType& insn_type,
Jeff Haofe1f7c82013-08-01 14:50:24 -07003861 const uint32_t vregA) {
3862 // Primitive assignability rules are weaker than regular assignability rules.
3863 bool instruction_compatible;
3864 bool value_compatible;
Ian Rogers7b078e82014-09-10 14:44:24 -07003865 const RegType& value_type = work_line_->GetRegisterType(this, vregA);
Jeff Haofe1f7c82013-08-01 14:50:24 -07003866 if (target_type.IsIntegralTypes()) {
Jeff Haoa4647482013-08-06 15:35:47 -07003867 instruction_compatible = target_type.Equals(insn_type);
Jeff Haofe1f7c82013-08-01 14:50:24 -07003868 value_compatible = value_type.IsIntegralTypes();
3869 } else if (target_type.IsFloat()) {
3870 instruction_compatible = insn_type.IsInteger(); // no put-float, so expect put-int
3871 value_compatible = value_type.IsFloatTypes();
3872 } else if (target_type.IsLong()) {
3873 instruction_compatible = insn_type.IsLong();
Andreas Gampe376fa682014-09-07 13:06:12 -07003874 // Additional register check: this is not checked statically (as part of VerifyInstructions),
3875 // as target_type depends on the resolved type of the field.
3876 if (instruction_compatible && work_line_->NumRegs() > vregA + 1) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003877 const RegType& value_type_hi = work_line_->GetRegisterType(this, vregA + 1);
Andreas Gampe376fa682014-09-07 13:06:12 -07003878 value_compatible = value_type.IsLongTypes() && value_type.CheckWidePair(value_type_hi);
3879 } else {
3880 value_compatible = false;
3881 }
Jeff Haofe1f7c82013-08-01 14:50:24 -07003882 } else if (target_type.IsDouble()) {
3883 instruction_compatible = insn_type.IsLong(); // no put-double, so expect put-long
Andreas Gampe376fa682014-09-07 13:06:12 -07003884 // Additional register check: this is not checked statically (as part of VerifyInstructions),
3885 // as target_type depends on the resolved type of the field.
3886 if (instruction_compatible && work_line_->NumRegs() > vregA + 1) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003887 const RegType& value_type_hi = work_line_->GetRegisterType(this, vregA + 1);
Andreas Gampe376fa682014-09-07 13:06:12 -07003888 value_compatible = value_type.IsDoubleTypes() && value_type.CheckWidePair(value_type_hi);
3889 } else {
3890 value_compatible = false;
3891 }
Jeff Haofe1f7c82013-08-01 14:50:24 -07003892 } else {
3893 instruction_compatible = false; // reference with primitive store
3894 value_compatible = false; // unused
3895 }
3896 if (!instruction_compatible) {
3897 // This is a global failure rather than a class change failure as the instructions and
3898 // the descriptors for the type should have been consistent within the same file at
3899 // compile time.
3900 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "put insn has type '" << insn_type
3901 << "' but expected type '" << target_type << "'";
3902 return;
3903 }
3904 if (!value_compatible) {
3905 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
3906 << " of type " << value_type << " but expected " << target_type << " for put";
3907 return;
3908 }
3909}
3910
Sebastien Hertz5243e912013-05-21 10:55:07 +02003911void MethodVerifier::VerifyAPut(const Instruction* inst,
Ian Rogersd8f69b02014-09-10 21:43:52 +00003912 const RegType& insn_type, bool is_primitive) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003913 const RegType& index_type = work_line_->GetRegisterType(this, inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07003914 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003915 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07003916 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07003917 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegB_23x());
Ian Rogers89310de2012-02-01 13:47:30 -08003918 if (array_type.IsZero()) {
Nicolas Geoffray66389fb2015-06-19 10:35:42 +01003919 // Null array type; this code path will fail at runtime.
3920 // Still check that the given value matches the instruction's type.
3921 work_line_->VerifyRegisterType(this, inst->VRegA_23x(), insn_type);
jeffhaofc3144e2012-02-01 17:21:15 -08003922 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003923 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aput";
Ian Rogers89310de2012-02-01 13:47:30 -08003924 } else {
Ian Rogersd8f69b02014-09-10 21:43:52 +00003925 const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
Jeff Haofe1f7c82013-08-01 14:50:24 -07003926 const uint32_t vregA = inst->VRegA_23x();
Jeff Haob24b4a72013-07-31 13:47:31 -07003927 if (is_primitive) {
Jeff Haofe1f7c82013-08-01 14:50:24 -07003928 VerifyPrimitivePut(component_type, insn_type, vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07003929 } else {
Jeff Haob24b4a72013-07-31 13:47:31 -07003930 if (!component_type.IsReferenceTypes()) {
3931 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
3932 << " source for aput-object";
3933 } else {
3934 // The instruction agrees with the type of array, confirm the value to be stored does too
3935 // Note: we use the instruction type (rather than the component type) for aput-object as
3936 // incompatible classes will be caught at runtime as an array store exception
Ian Rogers7b078e82014-09-10 14:44:24 -07003937 work_line_->VerifyRegisterType(this, vregA, insn_type);
Jeff Haob24b4a72013-07-31 13:47:31 -07003938 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003939 }
3940 }
3941 }
3942}
3943
Mathieu Chartierc7853442015-03-27 14:35:38 -07003944ArtField* MethodVerifier::GetStaticField(int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08003945 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3946 // Check access to class
Ian Rogersd8f69b02014-09-10 21:43:52 +00003947 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Brian Carlstrom7934ac22013-07-26 10:54:15 -07003948 if (klass_type.IsConflict()) { // bad class
Ian Rogersad0b3a32012-04-16 14:50:24 -07003949 AppendToLastFailMessage(StringPrintf(" in attempt to access static field %d (%s) in %s",
3950 field_idx, dex_file_->GetFieldName(field_id),
3951 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers7b078e82014-09-10 14:44:24 -07003952 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08003953 }
Elliott Hughesb25c3f62012-03-26 16:35:06 -07003954 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003955 return nullptr; // Can't resolve Class so no more to do here, will do checking at runtime.
Ian Rogers90040192011-12-16 08:54:29 -08003956 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07003957 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartierc7853442015-03-27 14:35:38 -07003958 ArtField* field = class_linker->ResolveFieldJLS(*dex_file_, field_idx, dex_cache_,
3959 class_loader_);
Ian Rogers7b078e82014-09-10 14:44:24 -07003960 if (field == nullptr) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07003961 VLOG(verifier) << "Unable to resolve static field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07003962 << dex_file_->GetFieldName(field_id) << ") in "
3963 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogers7b078e82014-09-10 14:44:24 -07003964 DCHECK(self_->IsExceptionPending());
3965 self_->ClearException();
3966 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003967 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
3968 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003969 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access static field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07003970 << " from " << GetDeclaringClass();
Ian Rogers7b078e82014-09-10 14:44:24 -07003971 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003972 } else if (!field->IsStatic()) {
3973 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field) << " to be static";
Ian Rogers7b078e82014-09-10 14:44:24 -07003974 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003975 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07003976 return field;
Ian Rogersd81871c2011-10-03 13:57:23 -07003977}
3978
Mathieu Chartierc7853442015-03-27 14:35:38 -07003979ArtField* MethodVerifier::GetInstanceField(const RegType& obj_type, int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08003980 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3981 // Check access to class
Ian Rogersd8f69b02014-09-10 21:43:52 +00003982 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003983 if (klass_type.IsConflict()) {
3984 AppendToLastFailMessage(StringPrintf(" in attempt to access instance field %d (%s) in %s",
3985 field_idx, dex_file_->GetFieldName(field_id),
3986 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers7b078e82014-09-10 14:44:24 -07003987 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08003988 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003989 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003990 return nullptr; // Can't resolve Class so no more to do here
Ian Rogers90040192011-12-16 08:54:29 -08003991 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07003992 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartierc7853442015-03-27 14:35:38 -07003993 ArtField* field = class_linker->ResolveFieldJLS(*dex_file_, field_idx, dex_cache_,
3994 class_loader_);
Ian Rogers7b078e82014-09-10 14:44:24 -07003995 if (field == nullptr) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07003996 VLOG(verifier) << "Unable to resolve instance field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07003997 << dex_file_->GetFieldName(field_id) << ") in "
3998 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogers7b078e82014-09-10 14:44:24 -07003999 DCHECK(self_->IsExceptionPending());
4000 self_->ClearException();
4001 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004002 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
4003 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004004 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access instance field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07004005 << " from " << GetDeclaringClass();
Ian Rogers7b078e82014-09-10 14:44:24 -07004006 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07004007 } else if (field->IsStatic()) {
4008 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field)
4009 << " to not be static";
Ian Rogers7b078e82014-09-10 14:44:24 -07004010 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07004011 } else if (obj_type.IsZero()) {
4012 // Cannot infer and check type, however, access will cause null pointer exception
4013 return field;
Stephen Kyle695c5982014-08-22 15:03:07 +01004014 } else if (!obj_type.IsReferenceTypes()) {
4015 // Trying to read a field from something that isn't a reference
4016 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance field access on object that has "
4017 << "non-reference type " << obj_type;
Ian Rogers7b078e82014-09-10 14:44:24 -07004018 return nullptr;
Ian Rogerse1758fe2012-04-19 11:31:15 -07004019 } else {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08004020 mirror::Class* klass = field->GetDeclaringClass();
Ian Rogersd8f69b02014-09-10 21:43:52 +00004021 const RegType& field_klass =
Andreas Gampef23f33d2015-06-23 14:18:17 -07004022 FromClass(dex_file_->GetFieldDeclaringClassDescriptor(field_id),
4023 klass, klass->CannotBeAssignedFromOtherTypes());
Ian Rogersad0b3a32012-04-16 14:50:24 -07004024 if (obj_type.IsUninitializedTypes() &&
4025 (!IsConstructor() || GetDeclaringClass().Equals(obj_type) ||
4026 !field_klass.Equals(GetDeclaringClass()))) {
4027 // Field accesses through uninitialized references are only allowable for constructors where
4028 // the field is declared in this class
4029 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "cannot access instance field " << PrettyField(field)
Brian Carlstrom93c33962013-07-26 10:37:43 -07004030 << " of a not fully initialized object within the context"
4031 << " of " << PrettyMethod(dex_method_idx_, *dex_file_);
Ian Rogers7b078e82014-09-10 14:44:24 -07004032 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004033 } else if (!field_klass.IsAssignableFrom(obj_type)) {
4034 // Trying to access C1.field1 using reference of type C2, which is neither C1 or a sub-class
4035 // of C1. For resolution to occur the declared class of the field must be compatible with
4036 // obj_type, we've discovered this wasn't so, so report the field didn't exist.
4037 Fail(VERIFY_ERROR_NO_FIELD) << "cannot access instance field " << PrettyField(field)
4038 << " from object of type " << obj_type;
Ian Rogers7b078e82014-09-10 14:44:24 -07004039 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004040 } else {
4041 return field;
4042 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004043 }
4044}
4045
Andreas Gampe896df402014-10-20 22:25:29 -07004046template <MethodVerifier::FieldAccessType kAccType>
4047void MethodVerifier::VerifyISFieldAccess(const Instruction* inst, const RegType& insn_type,
4048 bool is_primitive, bool is_static) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02004049 uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
Mathieu Chartierc7853442015-03-27 14:35:38 -07004050 ArtField* field;
Ian Rogersb94a27b2011-10-26 00:33:41 -07004051 if (is_static) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07004052 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07004053 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004054 const RegType& object_type = work_line_->GetRegisterType(this, inst->VRegB_22c());
Ian Rogersf4028cc2011-11-02 14:56:39 -07004055 field = GetInstanceField(object_type, field_idx);
Andreas Gampe896df402014-10-20 22:25:29 -07004056 if (UNLIKELY(have_pending_hard_failure_)) {
4057 return;
4058 }
Ian Rogersb94a27b2011-10-26 00:33:41 -07004059 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00004060 const RegType* field_type = nullptr;
Ian Rogers7b078e82014-09-10 14:44:24 -07004061 if (field != nullptr) {
Andreas Gampe896df402014-10-20 22:25:29 -07004062 if (kAccType == FieldAccessType::kAccPut) {
4063 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
4064 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
4065 << " from other class " << GetDeclaringClass();
4066 return;
4067 }
4068 }
4069
Mathieu Chartierc7853442015-03-27 14:35:38 -07004070 mirror::Class* field_type_class =
4071 can_load_classes_ ? field->GetType<true>() : field->GetType<false>();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004072 if (field_type_class != nullptr) {
Andreas Gampef23f33d2015-06-23 14:18:17 -07004073 field_type = &FromClass(field->GetTypeDescriptor(), field_type_class,
4074 field_type_class->CannotBeAssignedFromOtherTypes());
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08004075 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004076 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
4077 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004078 }
Ian Rogers0d604842012-04-16 14:50:24 -07004079 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004080 if (field_type == nullptr) {
4081 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
4082 const char* descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
Mathieu Chartierbf99f772014-08-23 16:37:27 -07004083 field_type = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004084 }
Sebastien Hertz757b3042014-03-28 14:34:28 +01004085 DCHECK(field_type != nullptr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02004086 const uint32_t vregA = (is_static) ? inst->VRegA_21c() : inst->VRegA_22c();
Andreas Gampe896df402014-10-20 22:25:29 -07004087 static_assert(kAccType == FieldAccessType::kAccPut || kAccType == FieldAccessType::kAccGet,
4088 "Unexpected third access type");
4089 if (kAccType == FieldAccessType::kAccPut) {
4090 // sput or iput.
4091 if (is_primitive) {
4092 VerifyPrimitivePut(*field_type, insn_type, vregA);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004093 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004094 if (!insn_type.IsAssignableFrom(*field_type)) {
Vladimir Marko414000e2015-06-23 17:45:21 +01004095 // If the field type is not a reference, this is a global failure rather than
4096 // a class change failure as the instructions and the descriptors for the type
4097 // should have been consistent within the same file at compile time.
4098 VerifyError error = field_type->IsReferenceTypes() ? VERIFY_ERROR_BAD_CLASS_SOFT
4099 : VERIFY_ERROR_BAD_CLASS_HARD;
4100 Fail(error) << "expected field " << PrettyField(field)
4101 << " to be compatible with type '" << insn_type
4102 << "' but found type '" << *field_type
4103 << "' in put-object";
Andreas Gampe896df402014-10-20 22:25:29 -07004104 return;
4105 }
4106 work_line_->VerifyRegisterType(this, vregA, *field_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004107 }
Andreas Gampe896df402014-10-20 22:25:29 -07004108 } else if (kAccType == FieldAccessType::kAccGet) {
4109 // sget or iget.
4110 if (is_primitive) {
4111 if (field_type->Equals(insn_type) ||
4112 (field_type->IsFloat() && insn_type.IsInteger()) ||
4113 (field_type->IsDouble() && insn_type.IsLong())) {
4114 // expected that read is of the correct primitive type or that int reads are reading
4115 // floats or long reads are reading doubles
4116 } else {
4117 // This is a global failure rather than a class change failure as the instructions and
4118 // the descriptors for the type should have been consistent within the same file at
4119 // compile time
4120 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
4121 << " to be of type '" << insn_type
4122 << "' but found type '" << *field_type << "' in get";
4123 return;
4124 }
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08004125 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004126 if (!insn_type.IsAssignableFrom(*field_type)) {
Vladimir Marko414000e2015-06-23 17:45:21 +01004127 // If the field type is not a reference, this is a global failure rather than
4128 // a class change failure as the instructions and the descriptors for the type
4129 // should have been consistent within the same file at compile time.
4130 VerifyError error = field_type->IsReferenceTypes() ? VERIFY_ERROR_BAD_CLASS_SOFT
4131 : VERIFY_ERROR_BAD_CLASS_HARD;
4132 Fail(error) << "expected field " << PrettyField(field)
4133 << " to be compatible with type '" << insn_type
4134 << "' but found type '" << *field_type
4135 << "' in get-object";
Andreas Gampe890da292015-07-06 17:20:18 -07004136 if (error != VERIFY_ERROR_BAD_CLASS_HARD) {
4137 work_line_->SetRegisterType(this, vregA, reg_types_.Conflict());
4138 }
Andreas Gampe896df402014-10-20 22:25:29 -07004139 return;
4140 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004141 }
Andreas Gampe896df402014-10-20 22:25:29 -07004142 if (!field_type->IsLowHalf()) {
4143 work_line_->SetRegisterType(this, vregA, *field_type);
4144 } else {
4145 work_line_->SetRegisterTypeWide(this, vregA, *field_type, field_type->HighHalf(&reg_types_));
4146 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07004147 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004148 LOG(FATAL) << "Unexpected case.";
Ian Rogersd81871c2011-10-03 13:57:23 -07004149 }
4150}
4151
Mathieu Chartierc7853442015-03-27 14:35:38 -07004152ArtField* MethodVerifier::GetQuickFieldAccess(const Instruction* inst,
Ian Rogers98379392014-02-24 16:53:16 -08004153 RegisterLine* reg_line) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08004154 DCHECK(IsInstructionIGetQuickOrIPutQuick(inst->Opcode())) << inst->Opcode();
Ian Rogers7b078e82014-09-10 14:44:24 -07004155 const RegType& object_type = reg_line->GetRegisterType(this, inst->VRegB_22c());
Ian Rogers9bc54402014-04-17 16:40:01 -07004156 if (!object_type.HasClass()) {
4157 VLOG(verifier) << "Failed to get mirror::Class* from '" << object_type << "'";
4158 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004159 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004160 uint32_t field_offset = static_cast<uint32_t>(inst->VRegC_22c());
Mathieu Chartierc7853442015-03-27 14:35:38 -07004161 ArtField* const f = ArtField::FindInstanceFieldWithOffset(object_type.GetClass(), field_offset);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08004162 DCHECK_EQ(f->GetOffset().Uint32Value(), field_offset);
Sebastien Hertz479fc1e2014-04-04 17:51:34 +02004163 if (f == nullptr) {
4164 VLOG(verifier) << "Failed to find instance field at offset '" << field_offset
4165 << "' from '" << PrettyDescriptor(object_type.GetClass()) << "'";
4166 }
4167 return f;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004168}
4169
Andreas Gampe896df402014-10-20 22:25:29 -07004170template <MethodVerifier::FieldAccessType kAccType>
4171void MethodVerifier::VerifyQuickFieldAccess(const Instruction* inst, const RegType& insn_type,
4172 bool is_primitive) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07004173 DCHECK(Runtime::Current()->IsStarted() || verify_to_dump_);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004174
Mathieu Chartierc7853442015-03-27 14:35:38 -07004175 ArtField* field = GetQuickFieldAccess(inst, work_line_.get());
Ian Rogers7b078e82014-09-10 14:44:24 -07004176 if (field == nullptr) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004177 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field from " << inst->Name();
4178 return;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004179 }
Andreas Gampe896df402014-10-20 22:25:29 -07004180
4181 // For an IPUT_QUICK, we now test for final flag of the field.
4182 if (kAccType == FieldAccessType::kAccPut) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004183 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
4184 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004185 << " from other class " << GetDeclaringClass();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004186 return;
4187 }
4188 }
Andreas Gampe896df402014-10-20 22:25:29 -07004189
4190 // Get the field type.
4191 const RegType* field_type;
4192 {
Mathieu Chartierc7853442015-03-27 14:35:38 -07004193 mirror::Class* field_type_class = can_load_classes_ ? field->GetType<true>() :
4194 field->GetType<false>();
Andreas Gampe896df402014-10-20 22:25:29 -07004195
4196 if (field_type_class != nullptr) {
Andreas Gampef23f33d2015-06-23 14:18:17 -07004197 field_type = &FromClass(field->GetTypeDescriptor(), field_type_class,
4198 field_type_class->CannotBeAssignedFromOtherTypes());
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004199 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004200 Thread* self = Thread::Current();
4201 DCHECK(!can_load_classes_ || self->IsExceptionPending());
4202 self->ClearException();
4203 field_type = &reg_types_.FromDescriptor(field->GetDeclaringClass()->GetClassLoader(),
4204 field->GetTypeDescriptor(), false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004205 }
Andreas Gampe896df402014-10-20 22:25:29 -07004206 if (field_type == nullptr) {
4207 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field type from " << inst->Name();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004208 return;
4209 }
Andreas Gampe896df402014-10-20 22:25:29 -07004210 }
4211
4212 const uint32_t vregA = inst->VRegA_22c();
4213 static_assert(kAccType == FieldAccessType::kAccPut || kAccType == FieldAccessType::kAccGet,
4214 "Unexpected third access type");
4215 if (kAccType == FieldAccessType::kAccPut) {
4216 if (is_primitive) {
4217 // Primitive field assignability rules are weaker than regular assignability rules
4218 bool instruction_compatible;
4219 bool value_compatible;
4220 const RegType& value_type = work_line_->GetRegisterType(this, vregA);
4221 if (field_type->IsIntegralTypes()) {
4222 instruction_compatible = insn_type.IsIntegralTypes();
4223 value_compatible = value_type.IsIntegralTypes();
4224 } else if (field_type->IsFloat()) {
4225 instruction_compatible = insn_type.IsInteger(); // no [is]put-float, so expect [is]put-int
4226 value_compatible = value_type.IsFloatTypes();
4227 } else if (field_type->IsLong()) {
4228 instruction_compatible = insn_type.IsLong();
4229 value_compatible = value_type.IsLongTypes();
4230 } else if (field_type->IsDouble()) {
4231 instruction_compatible = insn_type.IsLong(); // no [is]put-double, so expect [is]put-long
4232 value_compatible = value_type.IsDoubleTypes();
4233 } else {
4234 instruction_compatible = false; // reference field with primitive store
4235 value_compatible = false; // unused
4236 }
4237 if (!instruction_compatible) {
4238 // This is a global failure rather than a class change failure as the instructions and
4239 // the descriptors for the type should have been consistent within the same file at
4240 // compile time
4241 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
4242 << " to be of type '" << insn_type
4243 << "' but found type '" << *field_type
4244 << "' in put";
4245 return;
4246 }
4247 if (!value_compatible) {
4248 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
4249 << " of type " << value_type
4250 << " but expected " << *field_type
4251 << " for store to " << PrettyField(field) << " in put";
4252 return;
4253 }
4254 } else {
4255 if (!insn_type.IsAssignableFrom(*field_type)) {
4256 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
4257 << " to be compatible with type '" << insn_type
4258 << "' but found type '" << *field_type
4259 << "' in put-object";
4260 return;
4261 }
4262 work_line_->VerifyRegisterType(this, vregA, *field_type);
4263 }
4264 } else if (kAccType == FieldAccessType::kAccGet) {
4265 if (is_primitive) {
4266 if (field_type->Equals(insn_type) ||
4267 (field_type->IsFloat() && insn_type.IsIntegralTypes()) ||
4268 (field_type->IsDouble() && insn_type.IsLongTypes())) {
4269 // expected that read is of the correct primitive type or that int reads are reading
4270 // floats or long reads are reading doubles
4271 } else {
4272 // This is a global failure rather than a class change failure as the instructions and
4273 // the descriptors for the type should have been consistent within the same file at
4274 // compile time
4275 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
4276 << " to be of type '" << insn_type
4277 << "' but found type '" << *field_type << "' in Get";
4278 return;
4279 }
4280 } else {
4281 if (!insn_type.IsAssignableFrom(*field_type)) {
4282 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
4283 << " to be compatible with type '" << insn_type
4284 << "' but found type '" << *field_type
4285 << "' in get-object";
4286 work_line_->SetRegisterType(this, vregA, reg_types_.Conflict());
4287 return;
4288 }
4289 }
4290 if (!field_type->IsLowHalf()) {
4291 work_line_->SetRegisterType(this, vregA, *field_type);
4292 } else {
4293 work_line_->SetRegisterTypeWide(this, vregA, *field_type, field_type->HighHalf(&reg_types_));
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004294 }
4295 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004296 LOG(FATAL) << "Unexpected case.";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004297 }
4298}
4299
Ian Rogers776ac1f2012-04-13 23:36:36 -07004300bool MethodVerifier::CheckNotMoveException(const uint16_t* insns, int insn_idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004301 if ((insns[insn_idx] & 0xff) == Instruction::MOVE_EXCEPTION) {
jeffhaod5347e02012-03-22 17:25:05 -07004302 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-exception";
Ian Rogersd81871c2011-10-03 13:57:23 -07004303 return false;
4304 }
4305 return true;
4306}
4307
Stephen Kyle9bc61992014-09-22 13:53:15 +01004308bool MethodVerifier::CheckNotMoveResult(const uint16_t* insns, int insn_idx) {
4309 if (((insns[insn_idx] & 0xff) >= Instruction::MOVE_RESULT) &&
4310 ((insns[insn_idx] & 0xff) <= Instruction::MOVE_RESULT_OBJECT)) {
4311 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-result*";
4312 return false;
4313 }
4314 return true;
4315}
4316
4317bool MethodVerifier::CheckNotMoveExceptionOrMoveResult(const uint16_t* insns, int insn_idx) {
4318 return (CheckNotMoveException(insns, insn_idx) && CheckNotMoveResult(insns, insn_idx));
4319}
4320
Ian Rogersebbdd872014-07-07 23:53:08 -07004321bool MethodVerifier::UpdateRegisters(uint32_t next_insn, RegisterLine* merge_line,
4322 bool update_merge_line) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004323 bool changed = true;
4324 RegisterLine* target_line = reg_table_.GetLine(next_insn);
4325 if (!insn_flags_[next_insn].IsVisitedOrChanged()) {
jeffhaobdb76512011-09-07 11:43:16 -07004326 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07004327 * We haven't processed this instruction before, and we haven't touched the registers here, so
4328 * there's nothing to "merge". Copy the registers over and mark it as changed. (This is the
4329 * only way a register can transition out of "unknown", so this is not just an optimization.)
jeffhaobdb76512011-09-07 11:43:16 -07004330 */
Ian Rogersb8c78592013-07-25 23:52:52 +00004331 if (!insn_flags_[next_insn].IsReturn()) {
4332 target_line->CopyFromLine(merge_line);
4333 } else {
Jeff Haob24b4a72013-07-31 13:47:31 -07004334 // Verify that the monitor stack is empty on return.
Ian Rogers7b078e82014-09-10 14:44:24 -07004335 if (!merge_line->VerifyMonitorStackEmpty(this)) {
Jeff Haob24b4a72013-07-31 13:47:31 -07004336 return false;
4337 }
Ian Rogersb8c78592013-07-25 23:52:52 +00004338 // For returns we only care about the operand to the return, all other registers are dead.
4339 // Initialize them as conflicts so they don't add to GC and deoptimization information.
4340 const Instruction* ret_inst = Instruction::At(code_item_->insns_ + next_insn);
4341 Instruction::Code opcode = ret_inst->Opcode();
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07004342 if (opcode == Instruction::RETURN_VOID || opcode == Instruction::RETURN_VOID_NO_BARRIER) {
Stephen Kyle7e541c92014-12-17 17:10:02 +00004343 SafelyMarkAllRegistersAsConflicts(this, target_line);
Ian Rogersb8c78592013-07-25 23:52:52 +00004344 } else {
4345 target_line->CopyFromLine(merge_line);
4346 if (opcode == Instruction::RETURN_WIDE) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004347 target_line->MarkAllRegistersAsConflictsExceptWide(this, ret_inst->VRegA_11x());
Ian Rogersb8c78592013-07-25 23:52:52 +00004348 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004349 target_line->MarkAllRegistersAsConflictsExcept(this, ret_inst->VRegA_11x());
Ian Rogersb8c78592013-07-25 23:52:52 +00004350 }
4351 }
4352 }
jeffhaobdb76512011-09-07 11:43:16 -07004353 } else {
Ian Rogers700a4022014-05-19 16:49:03 -07004354 std::unique_ptr<RegisterLine> copy(gDebugVerify ?
Ian Rogersd0fbd852013-09-24 18:17:04 -07004355 RegisterLine::Create(target_line->NumRegs(), this) :
Ian Rogers7b078e82014-09-10 14:44:24 -07004356 nullptr);
Ian Rogers7b0c5b42012-02-16 15:29:07 -08004357 if (gDebugVerify) {
4358 copy->CopyFromLine(target_line);
4359 }
Ian Rogers7b078e82014-09-10 14:44:24 -07004360 changed = target_line->MergeRegisters(this, merge_line);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004361 if (have_pending_hard_failure_) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004362 return false;
jeffhaobdb76512011-09-07 11:43:16 -07004363 }
Ian Rogers2c8a8572011-10-24 17:11:36 -07004364 if (gDebugVerify && changed) {
Elliott Hughes398f64b2012-03-26 18:05:48 -07004365 LogVerifyInfo() << "Merging at [" << reinterpret_cast<void*>(work_insn_idx_) << "]"
Elliott Hughesc073b072012-05-24 19:29:17 -07004366 << " to [" << reinterpret_cast<void*>(next_insn) << "]: " << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07004367 << copy->Dump(this) << " MERGE\n"
4368 << merge_line->Dump(this) << " ==\n"
4369 << target_line->Dump(this) << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07004370 }
Ian Rogersebbdd872014-07-07 23:53:08 -07004371 if (update_merge_line && changed) {
4372 merge_line->CopyFromLine(target_line);
4373 }
jeffhaobdb76512011-09-07 11:43:16 -07004374 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004375 if (changed) {
4376 insn_flags_[next_insn].SetChanged();
jeffhaobdb76512011-09-07 11:43:16 -07004377 }
4378 return true;
4379}
4380
Ian Rogers7b3ddd22013-02-21 15:19:52 -08004381InstructionFlags* MethodVerifier::CurrentInsnFlags() {
Ian Rogers776ac1f2012-04-13 23:36:36 -07004382 return &insn_flags_[work_insn_idx_];
4383}
4384
Ian Rogersd8f69b02014-09-10 21:43:52 +00004385const RegType& MethodVerifier::GetMethodReturnType() {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004386 if (return_type_ == nullptr) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07004387 if (mirror_method_ != nullptr) {
Ian Rogersded66a02014-10-28 18:12:55 -07004388 mirror::Class* return_type_class = mirror_method_->GetReturnType(can_load_classes_);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004389 if (return_type_class != nullptr) {
Andreas Gampef23f33d2015-06-23 14:18:17 -07004390 return_type_ = &FromClass(mirror_method_->GetReturnTypeDescriptor(),
4391 return_type_class,
4392 return_type_class->CannotBeAssignedFromOtherTypes());
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004393 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004394 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
4395 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004396 }
4397 }
4398 if (return_type_ == nullptr) {
4399 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
4400 const DexFile::ProtoId& proto_id = dex_file_->GetMethodPrototype(method_id);
4401 uint16_t return_type_idx = proto_id.return_type_idx_;
4402 const char* descriptor = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(return_type_idx));
Mathieu Chartierbf99f772014-08-23 16:37:27 -07004403 return_type_ = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004404 }
4405 }
4406 return *return_type_;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004407}
4408
Ian Rogersd8f69b02014-09-10 21:43:52 +00004409const RegType& MethodVerifier::GetDeclaringClass() {
Ian Rogers7b078e82014-09-10 14:44:24 -07004410 if (declaring_class_ == nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004411 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
Brian Carlstrom93c33962013-07-26 10:37:43 -07004412 const char* descriptor
4413 = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(method_id.class_idx_));
Mathieu Chartiere401d142015-04-22 13:56:20 -07004414 if (mirror_method_ != nullptr) {
Ian Rogers637c65b2013-05-31 11:46:00 -07004415 mirror::Class* klass = mirror_method_->GetDeclaringClass();
Andreas Gampef23f33d2015-06-23 14:18:17 -07004416 declaring_class_ = &FromClass(descriptor, klass,
4417 klass->CannotBeAssignedFromOtherTypes());
Ian Rogers637c65b2013-05-31 11:46:00 -07004418 } else {
Mathieu Chartierbf99f772014-08-23 16:37:27 -07004419 declaring_class_ = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers637c65b2013-05-31 11:46:00 -07004420 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07004421 }
Ian Rogers637c65b2013-05-31 11:46:00 -07004422 return *declaring_class_;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004423}
4424
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004425std::vector<int32_t> MethodVerifier::DescribeVRegs(uint32_t dex_pc) {
4426 RegisterLine* line = reg_table_.GetLine(dex_pc);
Sebastien Hertzaa0c00c2014-03-14 17:58:54 +01004427 DCHECK(line != nullptr) << "No register line at DEX pc " << StringPrintf("0x%x", dex_pc);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004428 std::vector<int32_t> result;
4429 for (size_t i = 0; i < line->NumRegs(); ++i) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004430 const RegType& type = line->GetRegisterType(this, i);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004431 if (type.IsConstant()) {
4432 result.push_back(type.IsPreciseConstant() ? kConstant : kImpreciseConstant);
Ian Rogers7b078e82014-09-10 14:44:24 -07004433 const ConstantType* const_val = down_cast<const ConstantType*>(&type);
4434 result.push_back(const_val->ConstantValue());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004435 } else if (type.IsConstantLo()) {
4436 result.push_back(type.IsPreciseConstantLo() ? kConstant : kImpreciseConstant);
Ian Rogers7b078e82014-09-10 14:44:24 -07004437 const ConstantType* const_val = down_cast<const ConstantType*>(&type);
4438 result.push_back(const_val->ConstantValueLo());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004439 } else if (type.IsConstantHi()) {
4440 result.push_back(type.IsPreciseConstantHi() ? kConstant : kImpreciseConstant);
Ian Rogers7b078e82014-09-10 14:44:24 -07004441 const ConstantType* const_val = down_cast<const ConstantType*>(&type);
4442 result.push_back(const_val->ConstantValueHi());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004443 } else if (type.IsIntegralTypes()) {
4444 result.push_back(kIntVReg);
4445 result.push_back(0);
4446 } else if (type.IsFloat()) {
4447 result.push_back(kFloatVReg);
4448 result.push_back(0);
4449 } else if (type.IsLong()) {
4450 result.push_back(kLongLoVReg);
4451 result.push_back(0);
4452 result.push_back(kLongHiVReg);
4453 result.push_back(0);
4454 ++i;
4455 } else if (type.IsDouble()) {
4456 result.push_back(kDoubleLoVReg);
4457 result.push_back(0);
4458 result.push_back(kDoubleHiVReg);
4459 result.push_back(0);
4460 ++i;
4461 } else if (type.IsUndefined() || type.IsConflict() || type.IsHighHalf()) {
4462 result.push_back(kUndefined);
4463 result.push_back(0);
4464 } else {
Ian Rogers7b3ddd22013-02-21 15:19:52 -08004465 CHECK(type.IsNonZeroReferenceTypes());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004466 result.push_back(kReferenceVReg);
4467 result.push_back(0);
4468 }
4469 }
4470 return result;
4471}
4472
Ian Rogersd8f69b02014-09-10 21:43:52 +00004473const RegType& MethodVerifier::DetermineCat1Constant(int32_t value, bool precise) {
Sebastien Hertz849600b2013-12-20 10:28:08 +01004474 if (precise) {
4475 // Precise constant type.
4476 return reg_types_.FromCat1Const(value, true);
4477 } else {
4478 // Imprecise constant type.
4479 if (value < -32768) {
4480 return reg_types_.IntConstant();
4481 } else if (value < -128) {
4482 return reg_types_.ShortConstant();
4483 } else if (value < 0) {
4484 return reg_types_.ByteConstant();
4485 } else if (value == 0) {
4486 return reg_types_.Zero();
4487 } else if (value == 1) {
4488 return reg_types_.One();
4489 } else if (value < 128) {
4490 return reg_types_.PosByteConstant();
4491 } else if (value < 32768) {
4492 return reg_types_.PosShortConstant();
4493 } else if (value < 65536) {
4494 return reg_types_.CharConstant();
4495 } else {
4496 return reg_types_.IntConstant();
4497 }
4498 }
4499}
4500
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004501void MethodVerifier::Init() {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08004502 art::verifier::RegTypeCache::Init();
Brian Carlstrome7d856b2012-01-11 18:10:55 -08004503}
4504
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004505void MethodVerifier::Shutdown() {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08004506 verifier::RegTypeCache::ShutDown();
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08004507}
jeffhaod1224c72012-02-29 13:43:08 -08004508
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -07004509void MethodVerifier::VisitStaticRoots(RootVisitor* visitor) {
4510 RegTypeCache::VisitStaticRoots(visitor);
Mathieu Chartier7c438b12014-09-12 17:01:24 -07004511}
4512
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -07004513void MethodVerifier::VisitRoots(RootVisitor* visitor, const RootInfo& root_info) {
4514 reg_types_.VisitRoots(visitor, root_info);
Mathieu Chartierc528dba2013-11-26 12:00:11 -08004515}
4516
Andreas Gampef23f33d2015-06-23 14:18:17 -07004517const RegType& MethodVerifier::FromClass(const char* descriptor,
4518 mirror::Class* klass,
4519 bool precise) {
4520 DCHECK(klass != nullptr);
4521 if (precise && !klass->IsInstantiable() && !klass->IsPrimitive()) {
4522 Fail(VerifyError::VERIFY_ERROR_NO_CLASS) << "Could not create precise reference for "
4523 << "non-instantiable klass " << descriptor;
4524 precise = false;
4525 }
4526 return reg_types_.FromClass(descriptor, klass, precise);
4527}
4528
Ian Rogersd81871c2011-10-03 13:57:23 -07004529} // namespace verifier
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07004530} // namespace art