blob: 5bcf260cd1f5e39a2c3ad56f6878a6268c89fa76 [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) {
Andreas Gampef10b6e12015-08-12 10:48:12 -0700110 if (verifier->IsInstanceConstructor()) {
Stephen Kyle7e541c92014-12-17 17:10:02 +0000111 // 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);
Igor Murashkin4d7b75f2015-07-21 17:03:36 -0700332
333 if (UNLIKELY(verifier.have_pending_experimental_failure_)) {
334 // Failed due to being forced into interpreter. This is ok because
335 // we just want to skip verification.
336 result = kSoftFailure;
337 } else {
338 CHECK(verifier.have_pending_hard_failure_);
339 verifier.DumpFailures(LOG(INFO) << "Verification error in "
340 << PrettyMethod(method_idx, *dex_file) << "\n");
341 result = kHardFailure;
342 }
jeffhaof56197c2012-03-05 18:01:54 -0800343 if (gDebugVerify) {
Ian Rogers46960fe2014-05-23 10:43:43 -0700344 std::cout << "\n" << verifier.info_messages_.str();
345 verifier.Dump(std::cout);
jeffhaof56197c2012-03-05 18:01:54 -0800346 }
jeffhaof56197c2012-03-05 18:01:54 -0800347 }
Mathieu Chartier8e219ae2014-08-19 14:29:46 -0700348 if (kTimeVerifyMethod) {
349 uint64_t duration_ns = NanoTime() - start_ns;
350 if (duration_ns > MsToNs(100)) {
351 LOG(WARNING) << "Verification of " << PrettyMethod(method_idx, *dex_file)
Andreas Gampea4f5bf62015-05-18 20:50:12 -0700352 << " took " << PrettyDuration(duration_ns)
353 << (IsLargeMethod(code_item) ? " (large method)" : "");
Mathieu Chartier8e219ae2014-08-19 14:29:46 -0700354 }
Ian Rogersc8982582012-09-07 16:53:25 -0700355 }
356 return result;
jeffhaof56197c2012-03-05 18:01:54 -0800357}
358
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100359MethodVerifier* MethodVerifier::VerifyMethodAndDump(Thread* self,
360 VariableIndentationOutputStream* vios,
361 uint32_t dex_method_idx,
362 const DexFile* dex_file,
363 Handle<mirror::DexCache> dex_cache,
364 Handle<mirror::ClassLoader> class_loader,
365 const DexFile::ClassDef* class_def,
366 const DexFile::CodeItem* code_item,
367 ArtMethod* method,
368 uint32_t method_access_flags) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700369 MethodVerifier* verifier = new MethodVerifier(self, dex_file, dex_cache, class_loader,
370 class_def, code_item, dex_method_idx, method,
371 method_access_flags, true, true, true, true);
372 verifier->Verify();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100373 verifier->DumpFailures(vios->Stream());
374 vios->Stream() << verifier->info_messages_.str();
Andreas Gampe5cbcde22014-09-16 14:59:49 -0700375 // Only dump and return if no hard failures. Otherwise the verifier may be not fully initialized
376 // and querying any info is dangerous/can abort.
377 if (verifier->have_pending_hard_failure_) {
378 delete verifier;
379 return nullptr;
380 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100381 verifier->Dump(vios);
Andreas Gampe5cbcde22014-09-16 14:59:49 -0700382 return verifier;
383 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800384}
385
Ian Rogers7b078e82014-09-10 14:44:24 -0700386MethodVerifier::MethodVerifier(Thread* self,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700387 const DexFile* dex_file, Handle<mirror::DexCache> dex_cache,
388 Handle<mirror::ClassLoader> class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700389 const DexFile::ClassDef* class_def,
Mathieu Chartier590fee92013-09-13 13:46:47 -0700390 const DexFile::CodeItem* code_item, uint32_t dex_method_idx,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700391 ArtMethod* method, uint32_t method_access_flags,
Ian Rogers46960fe2014-05-23 10:43:43 -0700392 bool can_load_classes, bool allow_soft_failures,
Mathieu Chartier4306ef82014-12-19 18:41:47 -0800393 bool need_precise_constants, bool verify_to_dump,
394 bool allow_thread_suspension)
Ian Rogers7b078e82014-09-10 14:44:24 -0700395 : self_(self),
396 reg_types_(can_load_classes),
Andreas Gamped5ad72f2015-06-26 17:33:47 -0700397 work_insn_idx_(DexFile::kDexNoIndex),
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800398 dex_method_idx_(dex_method_idx),
Ian Rogers637c65b2013-05-31 11:46:00 -0700399 mirror_method_(method),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700400 method_access_flags_(method_access_flags),
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700401 return_type_(nullptr),
jeffhaof56197c2012-03-05 18:01:54 -0800402 dex_file_(dex_file),
403 dex_cache_(dex_cache),
404 class_loader_(class_loader),
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700405 class_def_(class_def),
jeffhaof56197c2012-03-05 18:01:54 -0800406 code_item_(code_item),
Ian Rogers7b078e82014-09-10 14:44:24 -0700407 declaring_class_(nullptr),
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700408 interesting_dex_pc_(-1),
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700409 monitor_enter_dex_pcs_(nullptr),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700410 have_pending_hard_failure_(false),
jeffhaofaf459e2012-08-31 15:32:47 -0700411 have_pending_runtime_throw_failure_(false),
Igor Murashkin4d7b75f2015-07-21 17:03:36 -0700412 have_pending_experimental_failure_(false),
Andreas Gamped12e7822015-06-25 10:26:40 -0700413 have_any_pending_runtime_throw_failure_(false),
jeffhaof56197c2012-03-05 18:01:54 -0800414 new_instance_count_(0),
Elliott Hughes80537bb2013-01-04 16:37:26 -0800415 monitor_enter_count_(0),
Jeff Haoee988952013-04-16 14:23:47 -0700416 can_load_classes_(can_load_classes),
Sebastien Hertz4d4adb12013-07-24 16:14:19 +0200417 allow_soft_failures_(allow_soft_failures),
Ian Rogers46960fe2014-05-23 10:43:43 -0700418 need_precise_constants_(need_precise_constants),
Sebastien Hertz4d4adb12013-07-24 16:14:19 +0200419 has_check_casts_(false),
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700420 has_virtual_or_interface_invokes_(false),
Mathieu Chartier4306ef82014-12-19 18:41:47 -0800421 verify_to_dump_(verify_to_dump),
Andreas Gamped5ad72f2015-06-26 17:33:47 -0700422 allow_thread_suspension_(allow_thread_suspension),
423 link_(nullptr) {
Mathieu Chartierd0ad2ee2015-03-31 14:59:59 -0700424 self->PushVerifier(this);
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700425 DCHECK(class_def != nullptr);
jeffhaof56197c2012-03-05 18:01:54 -0800426}
427
Mathieu Chartier590fee92013-09-13 13:46:47 -0700428MethodVerifier::~MethodVerifier() {
Mathieu Chartierd0ad2ee2015-03-31 14:59:59 -0700429 Thread::Current()->PopVerifier(this);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700430 STLDeleteElements(&failure_messages_);
431}
432
Mathieu Chartiere401d142015-04-22 13:56:20 -0700433void MethodVerifier::FindLocksAtDexPc(ArtMethod* m, uint32_t dex_pc,
Ian Rogers46960fe2014-05-23 10:43:43 -0700434 std::vector<uint32_t>* monitor_enter_dex_pcs) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700435 StackHandleScope<2> hs(Thread::Current());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700436 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache()));
437 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader()));
Mathieu Chartiere401d142015-04-22 13:56:20 -0700438 MethodVerifier verifier(hs.Self(), m->GetDexFile(), dex_cache, class_loader, &m->GetClassDef(),
439 m->GetCodeItem(), m->GetDexMethodIndex(), m, m->GetAccessFlags(),
Mathieu Chartier4306ef82014-12-19 18:41:47 -0800440 false, true, false, false);
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700441 verifier.interesting_dex_pc_ = dex_pc;
Ian Rogers46960fe2014-05-23 10:43:43 -0700442 verifier.monitor_enter_dex_pcs_ = monitor_enter_dex_pcs;
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700443 verifier.FindLocksAtDexPc();
444}
445
Andreas Gampecb3c08f2014-09-18 13:16:38 -0700446static bool HasMonitorEnterInstructions(const DexFile::CodeItem* const code_item) {
447 const Instruction* inst = Instruction::At(code_item->insns_);
448
449 uint32_t insns_size = code_item->insns_size_in_code_units_;
450 for (uint32_t dex_pc = 0; dex_pc < insns_size;) {
451 if (inst->Opcode() == Instruction::MONITOR_ENTER) {
452 return true;
453 }
454
455 dex_pc += inst->SizeInCodeUnits();
456 inst = inst->Next();
457 }
458
459 return false;
460}
461
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700462void MethodVerifier::FindLocksAtDexPc() {
Ian Rogers7b078e82014-09-10 14:44:24 -0700463 CHECK(monitor_enter_dex_pcs_ != nullptr);
464 CHECK(code_item_ != nullptr); // This only makes sense for methods with code.
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700465
Andreas Gampecb3c08f2014-09-18 13:16:38 -0700466 // Quick check whether there are any monitor_enter instructions at all.
467 if (!HasMonitorEnterInstructions(code_item_)) {
468 return;
469 }
470
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700471 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
472 // verification. In practice, the phase we want relies on data structures set up by all the
473 // earlier passes, so we just run the full method verification and bail out early when we've
474 // got what we wanted.
475 Verify();
476}
477
Mathieu Chartiere401d142015-04-22 13:56:20 -0700478ArtField* MethodVerifier::FindAccessedFieldAtDexPc(ArtMethod* m, uint32_t dex_pc) {
479 StackHandleScope<2> hs(Thread::Current());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700480 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache()));
481 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader()));
Mathieu Chartiere401d142015-04-22 13:56:20 -0700482 MethodVerifier verifier(hs.Self(), m->GetDexFile(), dex_cache, class_loader, &m->GetClassDef(),
483 m->GetCodeItem(), m->GetDexMethodIndex(), m, m->GetAccessFlags(), true,
484 true, false, true);
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200485 return verifier.FindAccessedFieldAtDexPc(dex_pc);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200486}
487
Mathieu Chartierc7853442015-03-27 14:35:38 -0700488ArtField* MethodVerifier::FindAccessedFieldAtDexPc(uint32_t dex_pc) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700489 CHECK(code_item_ != nullptr); // This only makes sense for methods with code.
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200490
491 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
492 // verification. In practice, the phase we want relies on data structures set up by all the
493 // earlier passes, so we just run the full method verification and bail out early when we've
494 // got what we wanted.
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200495 bool success = Verify();
496 if (!success) {
Ian Rogers9bc54402014-04-17 16:40:01 -0700497 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200498 }
499 RegisterLine* register_line = reg_table_.GetLine(dex_pc);
Ian Rogers7b078e82014-09-10 14:44:24 -0700500 if (register_line == nullptr) {
Ian Rogers9bc54402014-04-17 16:40:01 -0700501 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200502 }
503 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
504 return GetQuickFieldAccess(inst, register_line);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200505}
506
Mathieu Chartiere401d142015-04-22 13:56:20 -0700507ArtMethod* MethodVerifier::FindInvokedMethodAtDexPc(ArtMethod* m, uint32_t dex_pc) {
508 StackHandleScope<2> hs(Thread::Current());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700509 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache()));
510 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader()));
Mathieu Chartiere401d142015-04-22 13:56:20 -0700511 MethodVerifier verifier(hs.Self(), m->GetDexFile(), dex_cache, class_loader, &m->GetClassDef(),
512 m->GetCodeItem(), m->GetDexMethodIndex(), m, m->GetAccessFlags(), true,
513 true, false, true);
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200514 return verifier.FindInvokedMethodAtDexPc(dex_pc);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200515}
516
Mathieu Chartiere401d142015-04-22 13:56:20 -0700517ArtMethod* MethodVerifier::FindInvokedMethodAtDexPc(uint32_t dex_pc) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700518 CHECK(code_item_ != nullptr); // This only makes sense for methods with code.
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200519
520 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
521 // verification. In practice, the phase we want relies on data structures set up by all the
522 // earlier passes, so we just run the full method verification and bail out early when we've
523 // got what we wanted.
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200524 bool success = Verify();
525 if (!success) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700526 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200527 }
528 RegisterLine* register_line = reg_table_.GetLine(dex_pc);
Ian Rogers7b078e82014-09-10 14:44:24 -0700529 if (register_line == nullptr) {
530 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200531 }
532 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
533 const bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
Mathieu Chartier091d2382015-03-06 10:59:06 -0800534 return GetQuickInvokedMethod(inst, register_line, is_range, false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200535}
536
Mathieu Chartiere401d142015-04-22 13:56:20 -0700537SafeMap<uint32_t, std::set<uint32_t>> MethodVerifier::FindStringInitMap(ArtMethod* m) {
Jeff Hao848f70a2014-01-15 13:49:50 -0800538 Thread* self = Thread::Current();
Mathieu Chartiere401d142015-04-22 13:56:20 -0700539 StackHandleScope<2> hs(self);
Jeff Hao848f70a2014-01-15 13:49:50 -0800540 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache()));
541 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader()));
Jeff Hao848f70a2014-01-15 13:49:50 -0800542 MethodVerifier verifier(self, m->GetDexFile(), dex_cache, class_loader, &m->GetClassDef(),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700543 m->GetCodeItem(), m->GetDexMethodIndex(), m, m->GetAccessFlags(),
Jeff Hao848f70a2014-01-15 13:49:50 -0800544 true, true, false, true);
545 return verifier.FindStringInitMap();
546}
547
548SafeMap<uint32_t, std::set<uint32_t>>& MethodVerifier::FindStringInitMap() {
549 Verify();
550 return GetStringInitPcRegMap();
551}
552
Ian Rogersad0b3a32012-04-16 14:50:24 -0700553bool MethodVerifier::Verify() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700554 // If there aren't any instructions, make sure that's expected, then exit successfully.
Ian Rogers7b078e82014-09-10 14:44:24 -0700555 if (code_item_ == nullptr) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700556 if ((method_access_flags_ & (kAccNative | kAccAbstract)) == 0) {
jeffhaod5347e02012-03-22 17:25:05 -0700557 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "zero-length code in concrete non-native method";
jeffhaobdb76512011-09-07 11:43:16 -0700558 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -0700559 } else {
560 return true;
jeffhaobdb76512011-09-07 11:43:16 -0700561 }
jeffhaobdb76512011-09-07 11:43:16 -0700562 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700563 // Sanity-check the register counts. ins + locals = registers, so make sure that ins <= registers.
564 if (code_item_->ins_size_ > code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700565 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad register counts (ins=" << code_item_->ins_size_
566 << " regs=" << code_item_->registers_size_;
Ian Rogersd81871c2011-10-03 13:57:23 -0700567 return false;
jeffhaobdb76512011-09-07 11:43:16 -0700568 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700569 // Allocate and initialize an array to hold instruction data.
Ian Rogers7b3ddd22013-02-21 15:19:52 -0800570 insn_flags_.reset(new InstructionFlags[code_item_->insns_size_in_code_units_]());
Ian Rogersd81871c2011-10-03 13:57:23 -0700571 // Run through the instructions and see if the width checks out.
572 bool result = ComputeWidthsAndCountOps();
573 // Flag instructions guarded by a "try" block and check exception handlers.
574 result = result && ScanTryCatchBlocks();
575 // Perform static instruction verification.
576 result = result && VerifyInstructions();
Ian Rogersad0b3a32012-04-16 14:50:24 -0700577 // Perform code-flow analysis and return.
Vladimir Marko2b5eaa22013-12-13 13:59:30 +0000578 result = result && VerifyCodeFlow();
579 // Compute information for compiler.
580 if (result && Runtime::Current()->IsCompiler()) {
581 result = Runtime::Current()->GetCompilerCallbacks()->MethodVerified(this);
582 }
583 return result;
jeffhaoba5ebb92011-08-25 17:24:37 -0700584}
585
Ian Rogers776ac1f2012-04-13 23:36:36 -0700586std::ostream& MethodVerifier::Fail(VerifyError error) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700587 switch (error) {
588 case VERIFY_ERROR_NO_CLASS:
589 case VERIFY_ERROR_NO_FIELD:
590 case VERIFY_ERROR_NO_METHOD:
591 case VERIFY_ERROR_ACCESS_CLASS:
592 case VERIFY_ERROR_ACCESS_FIELD:
593 case VERIFY_ERROR_ACCESS_METHOD:
Ian Rogers08f753d2012-08-24 14:35:25 -0700594 case VERIFY_ERROR_INSTANTIATION:
595 case VERIFY_ERROR_CLASS_CHANGE:
Igor Murashkin158f35c2015-06-10 15:55:30 -0700596 case VERIFY_ERROR_FORCE_INTERPRETER:
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800597 if (Runtime::Current()->IsAotCompiler() || !can_load_classes_) {
jeffhaofaf459e2012-08-31 15:32:47 -0700598 // If we're optimistically running verification at compile time, turn NO_xxx, ACCESS_xxx,
599 // class change and instantiation errors into soft verification errors so that we re-verify
600 // at runtime. We may fail to find or to agree on access because of not yet available class
601 // loaders, or class loaders that will differ at runtime. In these cases, we don't want to
602 // affect the soundness of the code being compiled. Instead, the generated code runs "slow
603 // paths" that dynamically perform the verification and cause the behavior to be that akin
604 // to an interpreter.
605 error = VERIFY_ERROR_BAD_CLASS_SOFT;
606 } else {
Jeff Haoa3faaf42013-09-03 19:07:00 -0700607 // If we fail again at runtime, mark that this instruction would throw and force this
608 // method to be executed using the interpreter with checks.
jeffhaofaf459e2012-08-31 15:32:47 -0700609 have_pending_runtime_throw_failure_ = true;
Andreas Gamped7f8d052015-03-12 11:05:47 -0700610
611 // We need to save the work_line if the instruction wasn't throwing before. Otherwise we'll
612 // try to merge garbage.
613 // Note: this assumes that Fail is called before we do any work_line modifications.
Andreas Gamped5ad72f2015-06-26 17:33:47 -0700614 // Note: this can fail before we touch any instruction, for the signature of a method. So
615 // add a check.
616 if (work_insn_idx_ < DexFile::kDexNoIndex) {
617 const uint16_t* insns = code_item_->insns_ + work_insn_idx_;
618 const Instruction* inst = Instruction::At(insns);
619 int opcode_flags = Instruction::FlagsOf(inst->Opcode());
Andreas Gamped7f8d052015-03-12 11:05:47 -0700620
Andreas Gamped5ad72f2015-06-26 17:33:47 -0700621 if ((opcode_flags & Instruction::kThrow) == 0 && CurrentInsnFlags()->IsInTry()) {
622 saved_line_->CopyFromLine(work_line_.get());
623 }
Andreas Gamped7f8d052015-03-12 11:05:47 -0700624 }
jeffhaofaf459e2012-08-31 15:32:47 -0700625 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700626 break;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700627 // Indication that verification should be retried at runtime.
628 case VERIFY_ERROR_BAD_CLASS_SOFT:
Jeff Haoee988952013-04-16 14:23:47 -0700629 if (!allow_soft_failures_) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700630 have_pending_hard_failure_ = true;
631 }
632 break;
jeffhaod5347e02012-03-22 17:25:05 -0700633 // Hard verification failures at compile time will still fail at runtime, so the class is
634 // marked as rejected to prevent it from being compiled.
Ian Rogersad0b3a32012-04-16 14:50:24 -0700635 case VERIFY_ERROR_BAD_CLASS_HARD: {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800636 if (Runtime::Current()->IsAotCompiler()) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700637 ClassReference ref(dex_file_, dex_file_->GetIndexForClassDef(*class_def_));
Vladimir Marko2b5eaa22013-12-13 13:59:30 +0000638 Runtime::Current()->GetCompilerCallbacks()->ClassRejected(ref);
jeffhaod1224c72012-02-29 13:43:08 -0800639 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700640 have_pending_hard_failure_ = true;
641 break;
Ian Rogers47a05882012-02-03 12:23:33 -0800642 }
643 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700644 failures_.push_back(error);
Elena Sayapina78480ec2014-08-15 15:52:42 +0700645 std::string location(StringPrintf("%s: [0x%X] ", PrettyMethod(dex_method_idx_, *dex_file_).c_str(),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700646 work_insn_idx_));
Elena Sayapina78480ec2014-08-15 15:52:42 +0700647 std::ostringstream* failure_message = new std::ostringstream(location, std::ostringstream::ate);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700648 failure_messages_.push_back(failure_message);
649 return *failure_message;
650}
651
Ian Rogers576ca0c2014-06-06 15:58:22 -0700652std::ostream& MethodVerifier::LogVerifyInfo() {
653 return info_messages_ << "VFY: " << PrettyMethod(dex_method_idx_, *dex_file_)
654 << '[' << reinterpret_cast<void*>(work_insn_idx_) << "] : ";
655}
656
Ian Rogersad0b3a32012-04-16 14:50:24 -0700657void MethodVerifier::PrependToLastFailMessage(std::string prepend) {
658 size_t failure_num = failure_messages_.size();
659 DCHECK_NE(failure_num, 0U);
660 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
661 prepend += last_fail_message->str();
Elena Sayapina78480ec2014-08-15 15:52:42 +0700662 failure_messages_[failure_num - 1] = new std::ostringstream(prepend, std::ostringstream::ate);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700663 delete last_fail_message;
664}
665
666void MethodVerifier::AppendToLastFailMessage(std::string append) {
667 size_t failure_num = failure_messages_.size();
668 DCHECK_NE(failure_num, 0U);
669 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
670 (*last_fail_message) << append;
Ian Rogers47a05882012-02-03 12:23:33 -0800671}
672
Ian Rogers776ac1f2012-04-13 23:36:36 -0700673bool MethodVerifier::ComputeWidthsAndCountOps() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700674 const uint16_t* insns = code_item_->insns_;
675 size_t insns_size = code_item_->insns_size_in_code_units_;
676 const Instruction* inst = Instruction::At(insns);
jeffhaobdb76512011-09-07 11:43:16 -0700677 size_t new_instance_count = 0;
678 size_t monitor_enter_count = 0;
Ian Rogersd81871c2011-10-03 13:57:23 -0700679 size_t dex_pc = 0;
jeffhaobdb76512011-09-07 11:43:16 -0700680
Ian Rogersd81871c2011-10-03 13:57:23 -0700681 while (dex_pc < insns_size) {
jeffhaobdb76512011-09-07 11:43:16 -0700682 Instruction::Code opcode = inst->Opcode();
Ian Rogersa9a82542013-10-04 11:17:26 -0700683 switch (opcode) {
684 case Instruction::APUT_OBJECT:
685 case Instruction::CHECK_CAST:
686 has_check_casts_ = true;
687 break;
688 case Instruction::INVOKE_VIRTUAL:
689 case Instruction::INVOKE_VIRTUAL_RANGE:
690 case Instruction::INVOKE_INTERFACE:
691 case Instruction::INVOKE_INTERFACE_RANGE:
692 has_virtual_or_interface_invokes_ = true;
693 break;
694 case Instruction::MONITOR_ENTER:
695 monitor_enter_count++;
696 break;
697 case Instruction::NEW_INSTANCE:
698 new_instance_count++;
699 break;
700 default:
701 break;
jeffhaobdb76512011-09-07 11:43:16 -0700702 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700703 size_t inst_size = inst->SizeInCodeUnits();
Ian Rogers7b078e82014-09-10 14:44:24 -0700704 insn_flags_[dex_pc].SetIsOpcode();
Ian Rogersd81871c2011-10-03 13:57:23 -0700705 dex_pc += inst_size;
Ian Rogers7b078e82014-09-10 14:44:24 -0700706 inst = inst->RelativeAt(inst_size);
jeffhaobdb76512011-09-07 11:43:16 -0700707 }
708
Ian Rogersd81871c2011-10-03 13:57:23 -0700709 if (dex_pc != insns_size) {
jeffhaod5347e02012-03-22 17:25:05 -0700710 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "code did not end where expected ("
711 << dex_pc << " vs. " << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700712 return false;
713 }
714
Ian Rogersd81871c2011-10-03 13:57:23 -0700715 new_instance_count_ = new_instance_count;
716 monitor_enter_count_ = monitor_enter_count;
jeffhaobdb76512011-09-07 11:43:16 -0700717 return true;
718}
719
Ian Rogers776ac1f2012-04-13 23:36:36 -0700720bool MethodVerifier::ScanTryCatchBlocks() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700721 uint32_t tries_size = code_item_->tries_size_;
jeffhaobdb76512011-09-07 11:43:16 -0700722 if (tries_size == 0) {
723 return true;
724 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700725 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Ian Rogers0571d352011-11-03 19:51:38 -0700726 const DexFile::TryItem* tries = DexFile::GetTryItems(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -0700727
728 for (uint32_t idx = 0; idx < tries_size; idx++) {
729 const DexFile::TryItem* try_item = &tries[idx];
730 uint32_t start = try_item->start_addr_;
731 uint32_t end = start + try_item->insn_count_;
jeffhaobdb76512011-09-07 11:43:16 -0700732 if ((start >= end) || (start >= insns_size) || (end > insns_size)) {
jeffhaod5347e02012-03-22 17:25:05 -0700733 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad exception entry: startAddr=" << start
734 << " endAddr=" << end << " (size=" << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700735 return false;
736 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700737 if (!insn_flags_[start].IsOpcode()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700738 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
739 << "'try' block starts inside an instruction (" << start << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700740 return false;
741 }
Ian Rogers7b078e82014-09-10 14:44:24 -0700742 uint32_t dex_pc = start;
743 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
744 while (dex_pc < end) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700745 insn_flags_[dex_pc].SetInTry();
Ian Rogers7b078e82014-09-10 14:44:24 -0700746 size_t insn_size = inst->SizeInCodeUnits();
747 dex_pc += insn_size;
748 inst = inst->RelativeAt(insn_size);
jeffhaobdb76512011-09-07 11:43:16 -0700749 }
750 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800751 // Iterate over each of the handlers to verify target addresses.
Ian Rogers13735952014-10-08 12:43:28 -0700752 const uint8_t* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -0700753 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700754 ClassLinker* linker = Runtime::Current()->GetClassLinker();
jeffhaobdb76512011-09-07 11:43:16 -0700755 for (uint32_t idx = 0; idx < handlers_size; idx++) {
Ian Rogers0571d352011-11-03 19:51:38 -0700756 CatchHandlerIterator iterator(handlers_ptr);
757 for (; iterator.HasNext(); iterator.Next()) {
758 uint32_t dex_pc= iterator.GetHandlerAddress();
Ian Rogersd81871c2011-10-03 13:57:23 -0700759 if (!insn_flags_[dex_pc].IsOpcode()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700760 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
761 << "exception handler starts at bad address (" << dex_pc << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700762 return false;
763 }
Stephen Kyle9bc61992014-09-22 13:53:15 +0100764 if (!CheckNotMoveResult(code_item_->insns_, dex_pc)) {
765 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
766 << "exception handler begins with move-result* (" << dex_pc << ")";
767 return false;
768 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700769 insn_flags_[dex_pc].SetBranchTarget();
Ian Rogers28ad40d2011-10-27 15:19:26 -0700770 // Ensure exception types are resolved so that they don't need resolution to be delivered,
771 // unresolved exception types will be ignored by exception delivery
Ian Rogers0571d352011-11-03 19:51:38 -0700772 if (iterator.GetHandlerTypeIndex() != DexFile::kDexNoIndex16) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800773 mirror::Class* exception_type = linker->ResolveType(*dex_file_,
774 iterator.GetHandlerTypeIndex(),
Mathieu Chartierbf99f772014-08-23 16:37:27 -0700775 dex_cache_, class_loader_);
Ian Rogers7b078e82014-09-10 14:44:24 -0700776 if (exception_type == nullptr) {
777 DCHECK(self_->IsExceptionPending());
778 self_->ClearException();
Ian Rogers28ad40d2011-10-27 15:19:26 -0700779 }
780 }
jeffhaobdb76512011-09-07 11:43:16 -0700781 }
Ian Rogers0571d352011-11-03 19:51:38 -0700782 handlers_ptr = iterator.EndDataPointer();
jeffhaobdb76512011-09-07 11:43:16 -0700783 }
jeffhaobdb76512011-09-07 11:43:16 -0700784 return true;
785}
786
Ian Rogers776ac1f2012-04-13 23:36:36 -0700787bool MethodVerifier::VerifyInstructions() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700788 const Instruction* inst = Instruction::At(code_item_->insns_);
jeffhaoba5ebb92011-08-25 17:24:37 -0700789
Ian Rogers0c7abda2012-09-19 13:33:42 -0700790 /* 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 -0700791 insn_flags_[0].SetBranchTarget();
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700792 insn_flags_[0].SetCompileTimeInfoPoint();
Ian Rogersd81871c2011-10-03 13:57:23 -0700793
794 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700795 for (uint32_t dex_pc = 0; dex_pc < insns_size;) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700796 if (!VerifyInstruction(inst, dex_pc)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700797 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -0700798 return false;
799 }
800 /* Flag instructions that are garbage collection points */
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700801 // All invoke points are marked as "Throw" points already.
802 // We are relying on this to also count all the invokes as interesting.
Vladimir Marko8b858e12014-11-27 14:52:37 +0000803 if (inst->IsBranch()) {
804 insn_flags_[dex_pc].SetCompileTimeInfoPoint();
805 // The compiler also needs safepoints for fall-through to loop heads.
806 // Such a loop head must be a target of a branch.
807 int32_t offset = 0;
808 bool cond, self_ok;
809 bool target_ok = GetBranchOffset(dex_pc, &offset, &cond, &self_ok);
810 DCHECK(target_ok);
811 insn_flags_[dex_pc + offset].SetCompileTimeInfoPoint();
812 } else if (inst->IsSwitch() || inst->IsThrow()) {
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700813 insn_flags_[dex_pc].SetCompileTimeInfoPoint();
Ian Rogersb8c78592013-07-25 23:52:52 +0000814 } else if (inst->IsReturn()) {
815 insn_flags_[dex_pc].SetCompileTimeInfoPointAndReturn();
Ian Rogersd81871c2011-10-03 13:57:23 -0700816 }
817 dex_pc += inst->SizeInCodeUnits();
818 inst = inst->Next();
819 }
820 return true;
821}
822
Ian Rogers776ac1f2012-04-13 23:36:36 -0700823bool MethodVerifier::VerifyInstruction(const Instruction* inst, uint32_t code_offset) {
Igor Murashkin4d7b75f2015-07-21 17:03:36 -0700824 if (UNLIKELY(inst->IsExperimental())) {
825 // Experimental instructions don't yet have verifier support implementation.
826 // While it is possible to use them by themselves, when we try to use stable instructions
827 // with a virtual register that was created by an experimental instruction,
828 // the data flow analysis will fail.
829 Fail(VERIFY_ERROR_FORCE_INTERPRETER)
830 << "experimental instruction is not supported by verifier; skipping verification";
831 have_pending_experimental_failure_ = true;
832 return false;
833 }
834
Ian Rogersd81871c2011-10-03 13:57:23 -0700835 bool result = true;
836 switch (inst->GetVerifyTypeArgumentA()) {
837 case Instruction::kVerifyRegA:
Ian Rogers29a26482014-05-02 15:27:29 -0700838 result = result && CheckRegisterIndex(inst->VRegA());
Ian Rogersd81871c2011-10-03 13:57:23 -0700839 break;
840 case Instruction::kVerifyRegAWide:
Ian Rogers29a26482014-05-02 15:27:29 -0700841 result = result && CheckWideRegisterIndex(inst->VRegA());
Ian Rogersd81871c2011-10-03 13:57:23 -0700842 break;
843 }
844 switch (inst->GetVerifyTypeArgumentB()) {
845 case Instruction::kVerifyRegB:
Ian Rogers29a26482014-05-02 15:27:29 -0700846 result = result && CheckRegisterIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700847 break;
848 case Instruction::kVerifyRegBField:
Ian Rogers29a26482014-05-02 15:27:29 -0700849 result = result && CheckFieldIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700850 break;
851 case Instruction::kVerifyRegBMethod:
Ian Rogers29a26482014-05-02 15:27:29 -0700852 result = result && CheckMethodIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700853 break;
854 case Instruction::kVerifyRegBNewInstance:
Ian Rogers29a26482014-05-02 15:27:29 -0700855 result = result && CheckNewInstance(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700856 break;
857 case Instruction::kVerifyRegBString:
Ian Rogers29a26482014-05-02 15:27:29 -0700858 result = result && CheckStringIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700859 break;
860 case Instruction::kVerifyRegBType:
Ian Rogers29a26482014-05-02 15:27:29 -0700861 result = result && CheckTypeIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700862 break;
863 case Instruction::kVerifyRegBWide:
Ian Rogers29a26482014-05-02 15:27:29 -0700864 result = result && CheckWideRegisterIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700865 break;
866 }
867 switch (inst->GetVerifyTypeArgumentC()) {
868 case Instruction::kVerifyRegC:
Ian Rogers29a26482014-05-02 15:27:29 -0700869 result = result && CheckRegisterIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700870 break;
871 case Instruction::kVerifyRegCField:
Ian Rogers29a26482014-05-02 15:27:29 -0700872 result = result && CheckFieldIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700873 break;
874 case Instruction::kVerifyRegCNewArray:
Ian Rogers29a26482014-05-02 15:27:29 -0700875 result = result && CheckNewArray(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700876 break;
877 case Instruction::kVerifyRegCType:
Ian Rogers29a26482014-05-02 15:27:29 -0700878 result = result && CheckTypeIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700879 break;
880 case Instruction::kVerifyRegCWide:
Ian Rogers29a26482014-05-02 15:27:29 -0700881 result = result && CheckWideRegisterIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700882 break;
883 }
884 switch (inst->GetVerifyExtraFlags()) {
885 case Instruction::kVerifyArrayData:
886 result = result && CheckArrayData(code_offset);
887 break;
888 case Instruction::kVerifyBranchTarget:
889 result = result && CheckBranchTarget(code_offset);
890 break;
891 case Instruction::kVerifySwitchTargets:
892 result = result && CheckSwitchTargets(code_offset);
893 break;
Andreas Gampec3314312014-06-19 18:13:29 -0700894 case Instruction::kVerifyVarArgNonZero:
895 // Fall-through.
Ian Rogers29a26482014-05-02 15:27:29 -0700896 case Instruction::kVerifyVarArg: {
Taiju Tsuiki29498a22015-04-13 14:21:00 +0900897 // Instructions that can actually return a negative value shouldn't have this flag.
898 uint32_t v_a = dchecked_integral_cast<uint32_t>(inst->VRegA());
899 if ((inst->GetVerifyExtraFlags() == Instruction::kVerifyVarArgNonZero && v_a == 0) ||
900 v_a > Instruction::kMaxVarArgRegs) {
901 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << v_a << ") in "
Andreas Gampec3314312014-06-19 18:13:29 -0700902 "non-range invoke";
903 return false;
904 }
Taiju Tsuiki29498a22015-04-13 14:21:00 +0900905
Ian Rogers29a26482014-05-02 15:27:29 -0700906 uint32_t args[Instruction::kMaxVarArgRegs];
907 inst->GetVarArgs(args);
Taiju Tsuiki29498a22015-04-13 14:21:00 +0900908 result = result && CheckVarArgRegs(v_a, args);
Ian Rogersd81871c2011-10-03 13:57:23 -0700909 break;
Ian Rogers29a26482014-05-02 15:27:29 -0700910 }
Andreas Gampec3314312014-06-19 18:13:29 -0700911 case Instruction::kVerifyVarArgRangeNonZero:
912 // Fall-through.
Ian Rogersd81871c2011-10-03 13:57:23 -0700913 case Instruction::kVerifyVarArgRange:
Andreas Gampec3314312014-06-19 18:13:29 -0700914 if (inst->GetVerifyExtraFlags() == Instruction::kVerifyVarArgRangeNonZero &&
915 inst->VRegA() <= 0) {
916 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << inst->VRegA() << ") in "
917 "range invoke";
918 return false;
919 }
Ian Rogers29a26482014-05-02 15:27:29 -0700920 result = result && CheckVarArgRangeRegs(inst->VRegA(), inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700921 break;
922 case Instruction::kVerifyError:
jeffhaod5347e02012-03-22 17:25:05 -0700923 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected opcode " << inst->Name();
Ian Rogersd81871c2011-10-03 13:57:23 -0700924 result = false;
925 break;
926 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800927 if (inst->GetVerifyIsRuntimeOnly() && Runtime::Current()->IsAotCompiler() && !verify_to_dump_) {
Ian Rogers5fb22a92014-06-13 10:31:28 -0700928 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "opcode only expected at runtime " << inst->Name();
929 result = false;
930 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700931 return result;
932}
933
Ian Rogers7b078e82014-09-10 14:44:24 -0700934inline bool MethodVerifier::CheckRegisterIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700935 if (idx >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700936 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register index out of range (" << idx << " >= "
937 << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700938 return false;
939 }
940 return true;
941}
942
Ian Rogers7b078e82014-09-10 14:44:24 -0700943inline bool MethodVerifier::CheckWideRegisterIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700944 if (idx + 1 >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700945 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "wide register index out of range (" << idx
946 << "+1 >= " << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700947 return false;
948 }
949 return true;
950}
951
Ian Rogers7b078e82014-09-10 14:44:24 -0700952inline bool MethodVerifier::CheckFieldIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700953 if (idx >= dex_file_->GetHeader().field_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700954 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad field index " << idx << " (max "
955 << dex_file_->GetHeader().field_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700956 return false;
957 }
958 return true;
959}
960
Ian Rogers7b078e82014-09-10 14:44:24 -0700961inline bool MethodVerifier::CheckMethodIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700962 if (idx >= dex_file_->GetHeader().method_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700963 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad method index " << idx << " (max "
964 << dex_file_->GetHeader().method_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700965 return false;
966 }
967 return true;
968}
969
Ian Rogers7b078e82014-09-10 14:44:24 -0700970inline bool MethodVerifier::CheckNewInstance(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700971 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700972 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
973 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700974 return false;
975 }
976 // We don't need the actual class, just a pointer to the class name.
Ian Rogers0571d352011-11-03 19:51:38 -0700977 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -0700978 if (descriptor[0] != 'L') {
jeffhaod5347e02012-03-22 17:25:05 -0700979 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "can't call new-instance on type '" << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -0700980 return false;
981 }
982 return true;
983}
984
Ian Rogers7b078e82014-09-10 14:44:24 -0700985inline bool MethodVerifier::CheckStringIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700986 if (idx >= dex_file_->GetHeader().string_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700987 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad string index " << idx << " (max "
988 << dex_file_->GetHeader().string_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700989 return false;
990 }
991 return true;
992}
993
Ian Rogers7b078e82014-09-10 14:44:24 -0700994inline bool MethodVerifier::CheckTypeIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700995 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700996 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
997 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700998 return false;
999 }
1000 return true;
1001}
1002
Ian Rogers776ac1f2012-04-13 23:36:36 -07001003bool MethodVerifier::CheckNewArray(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001004 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07001005 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
1006 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001007 return false;
1008 }
1009 int bracket_count = 0;
Ian Rogers0571d352011-11-03 19:51:38 -07001010 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -07001011 const char* cp = descriptor;
1012 while (*cp++ == '[') {
1013 bracket_count++;
1014 }
1015 if (bracket_count == 0) {
1016 /* The given class must be an array type. */
Brian Carlstrom93c33962013-07-26 10:37:43 -07001017 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1018 << "can't new-array class '" << descriptor << "' (not an array)";
Ian Rogersd81871c2011-10-03 13:57:23 -07001019 return false;
1020 } else if (bracket_count > 255) {
1021 /* It is illegal to create an array of more than 255 dimensions. */
Brian Carlstrom93c33962013-07-26 10:37:43 -07001022 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1023 << "can't new-array class '" << descriptor << "' (exceeds limit)";
Ian Rogersd81871c2011-10-03 13:57:23 -07001024 return false;
1025 }
1026 return true;
1027}
1028
Ian Rogers776ac1f2012-04-13 23:36:36 -07001029bool MethodVerifier::CheckArrayData(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001030 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
1031 const uint16_t* insns = code_item_->insns_ + cur_offset;
1032 const uint16_t* array_data;
1033 int32_t array_data_offset;
1034
1035 DCHECK_LT(cur_offset, insn_count);
1036 /* make sure the start of the array data table is in range */
1037 array_data_offset = insns[1] | (((int32_t) insns[2]) << 16);
1038 if ((int32_t) cur_offset + array_data_offset < 0 ||
1039 cur_offset + array_data_offset + 2 >= insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -07001040 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data start: at " << cur_offset
Brian Carlstrom93c33962013-07-26 10:37:43 -07001041 << ", data offset " << array_data_offset
1042 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -07001043 return false;
1044 }
1045 /* offset to array data table is a relative branch-style offset */
1046 array_data = insns + array_data_offset;
Andreas Gampe57c47582015-07-01 22:05:59 -07001047 // Make sure the table is at an even dex pc, that is, 32-bit aligned.
1048 if (!IsAligned<4>(array_data)) {
jeffhaod5347e02012-03-22 17:25:05 -07001049 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned array data table: at " << cur_offset
1050 << ", data offset " << array_data_offset;
Ian Rogersd81871c2011-10-03 13:57:23 -07001051 return false;
1052 }
Andreas Gampe57c47582015-07-01 22:05:59 -07001053 // Make sure the array-data is marked as an opcode. This ensures that it was reached when
1054 // traversing the code item linearly. It is an approximation for a by-spec padding value.
1055 if (!insn_flags_[cur_offset + array_data_offset].IsOpcode()) {
1056 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array data table at " << cur_offset
1057 << ", data offset " << array_data_offset
1058 << " not correctly visited, probably bad padding.";
1059 return false;
1060 }
1061
Ian Rogersd81871c2011-10-03 13:57:23 -07001062 uint32_t value_width = array_data[1];
Elliott Hughes398f64b2012-03-26 18:05:48 -07001063 uint32_t value_count = *reinterpret_cast<const uint32_t*>(&array_data[2]);
Ian Rogersd81871c2011-10-03 13:57:23 -07001064 uint32_t table_size = 4 + (value_width * value_count + 1) / 2;
1065 /* make sure the end of the switch is in range */
1066 if (cur_offset + array_data_offset + table_size > insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -07001067 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data end: at " << cur_offset
1068 << ", data offset " << array_data_offset << ", end "
1069 << cur_offset + array_data_offset + table_size
1070 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -07001071 return false;
1072 }
1073 return true;
1074}
1075
Ian Rogers776ac1f2012-04-13 23:36:36 -07001076bool MethodVerifier::CheckBranchTarget(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001077 int32_t offset;
1078 bool isConditional, selfOkay;
1079 if (!GetBranchOffset(cur_offset, &offset, &isConditional, &selfOkay)) {
1080 return false;
1081 }
1082 if (!selfOkay && offset == 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001083 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch offset of zero not allowed at"
1084 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -07001085 return false;
1086 }
Elliott Hughes81ff3182012-03-23 20:35:56 -07001087 // Check for 32-bit overflow. This isn't strictly necessary if we can depend on the runtime
1088 // to have identical "wrap-around" behavior, but it's unwise to depend on that.
Ian Rogersd81871c2011-10-03 13:57:23 -07001089 if (((int64_t) cur_offset + (int64_t) offset) != (int64_t) (cur_offset + offset)) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001090 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch target overflow "
1091 << reinterpret_cast<void*>(cur_offset) << " +" << offset;
Ian Rogersd81871c2011-10-03 13:57:23 -07001092 return false;
1093 }
1094 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
1095 int32_t abs_offset = cur_offset + offset;
Brian Carlstrom93c33962013-07-26 10:37:43 -07001096 if (abs_offset < 0 ||
1097 (uint32_t) abs_offset >= insn_count ||
1098 !insn_flags_[abs_offset].IsOpcode()) {
jeffhaod5347e02012-03-22 17:25:05 -07001099 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid branch target " << offset << " (-> "
Elliott Hughes398f64b2012-03-26 18:05:48 -07001100 << reinterpret_cast<void*>(abs_offset) << ") at "
1101 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -07001102 return false;
1103 }
1104 insn_flags_[abs_offset].SetBranchTarget();
1105 return true;
1106}
1107
Ian Rogers776ac1f2012-04-13 23:36:36 -07001108bool MethodVerifier::GetBranchOffset(uint32_t cur_offset, int32_t* pOffset, bool* pConditional,
Ian Rogersd81871c2011-10-03 13:57:23 -07001109 bool* selfOkay) {
1110 const uint16_t* insns = code_item_->insns_ + cur_offset;
1111 *pConditional = false;
1112 *selfOkay = false;
jeffhaoba5ebb92011-08-25 17:24:37 -07001113 switch (*insns & 0xff) {
1114 case Instruction::GOTO:
1115 *pOffset = ((int16_t) *insns) >> 8;
jeffhaoba5ebb92011-08-25 17:24:37 -07001116 break;
1117 case Instruction::GOTO_32:
1118 *pOffset = insns[1] | (((uint32_t) insns[2]) << 16);
jeffhaoba5ebb92011-08-25 17:24:37 -07001119 *selfOkay = true;
1120 break;
1121 case Instruction::GOTO_16:
1122 *pOffset = (int16_t) insns[1];
jeffhaoba5ebb92011-08-25 17:24:37 -07001123 break;
1124 case Instruction::IF_EQ:
1125 case Instruction::IF_NE:
1126 case Instruction::IF_LT:
1127 case Instruction::IF_GE:
1128 case Instruction::IF_GT:
1129 case Instruction::IF_LE:
1130 case Instruction::IF_EQZ:
1131 case Instruction::IF_NEZ:
1132 case Instruction::IF_LTZ:
1133 case Instruction::IF_GEZ:
1134 case Instruction::IF_GTZ:
1135 case Instruction::IF_LEZ:
1136 *pOffset = (int16_t) insns[1];
1137 *pConditional = true;
jeffhaoba5ebb92011-08-25 17:24:37 -07001138 break;
1139 default:
1140 return false;
jeffhaoba5ebb92011-08-25 17:24:37 -07001141 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001142 return true;
1143}
1144
Ian Rogers776ac1f2012-04-13 23:36:36 -07001145bool MethodVerifier::CheckSwitchTargets(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001146 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001147 DCHECK_LT(cur_offset, insn_count);
Ian Rogersd81871c2011-10-03 13:57:23 -07001148 const uint16_t* insns = code_item_->insns_ + cur_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001149 /* make sure the start of the switch is in range */
Ian Rogersd81871c2011-10-03 13:57:23 -07001150 int32_t switch_offset = insns[1] | ((int32_t) insns[2]) << 16;
Jeff Hao9ccd1512015-03-20 18:11:45 -07001151 if ((int32_t) cur_offset + switch_offset < 0 || cur_offset + switch_offset + 2 > insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -07001152 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch start: at " << cur_offset
Brian Carlstrom93c33962013-07-26 10:37:43 -07001153 << ", switch offset " << switch_offset
1154 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001155 return false;
1156 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001157 /* offset to switch table is a relative branch-style offset */
Ian Rogersd81871c2011-10-03 13:57:23 -07001158 const uint16_t* switch_insns = insns + switch_offset;
Andreas Gampe57c47582015-07-01 22:05:59 -07001159 // Make sure the table is at an even dex pc, that is, 32-bit aligned.
1160 if (!IsAligned<4>(switch_insns)) {
jeffhaod5347e02012-03-22 17:25:05 -07001161 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned switch table: at " << cur_offset
1162 << ", switch offset " << switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001163 return false;
1164 }
Andreas Gampe57c47582015-07-01 22:05:59 -07001165 // Make sure the switch data is marked as an opcode. This ensures that it was reached when
1166 // traversing the code item linearly. It is an approximation for a by-spec padding value.
1167 if (!insn_flags_[cur_offset + switch_offset].IsOpcode()) {
1168 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "switch table at " << cur_offset
1169 << ", switch offset " << switch_offset
1170 << " not correctly visited, probably bad padding.";
1171 return false;
1172 }
1173
Ian Rogersd81871c2011-10-03 13:57:23 -07001174 uint32_t switch_count = switch_insns[1];
1175 int32_t keys_offset, targets_offset;
1176 uint16_t expected_signature;
jeffhaoba5ebb92011-08-25 17:24:37 -07001177 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
1178 /* 0=sig, 1=count, 2/3=firstKey */
1179 targets_offset = 4;
1180 keys_offset = -1;
1181 expected_signature = Instruction::kPackedSwitchSignature;
1182 } else {
1183 /* 0=sig, 1=count, 2..count*2 = keys */
1184 keys_offset = 2;
1185 targets_offset = 2 + 2 * switch_count;
1186 expected_signature = Instruction::kSparseSwitchSignature;
1187 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001188 uint32_t table_size = targets_offset + switch_count * 2;
jeffhaoba5ebb92011-08-25 17:24:37 -07001189 if (switch_insns[0] != expected_signature) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001190 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1191 << StringPrintf("wrong signature for switch table (%x, wanted %x)",
1192 switch_insns[0], expected_signature);
jeffhaoba5ebb92011-08-25 17:24:37 -07001193 return false;
1194 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001195 /* make sure the end of the switch is in range */
1196 if (cur_offset + switch_offset + table_size > (uint32_t) insn_count) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001197 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch end: at " << cur_offset
1198 << ", switch offset " << switch_offset
1199 << ", end " << (cur_offset + switch_offset + table_size)
jeffhaod5347e02012-03-22 17:25:05 -07001200 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001201 return false;
1202 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001203 /* for a sparse switch, verify the keys are in ascending order */
1204 if (keys_offset > 0 && switch_count > 1) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001205 int32_t last_key = switch_insns[keys_offset] | (switch_insns[keys_offset + 1] << 16);
1206 for (uint32_t targ = 1; targ < switch_count; targ++) {
jeffhaoba5ebb92011-08-25 17:24:37 -07001207 int32_t key = (int32_t) switch_insns[keys_offset + targ * 2] |
1208 (int32_t) (switch_insns[keys_offset + targ * 2 + 1] << 16);
1209 if (key <= last_key) {
jeffhaod5347e02012-03-22 17:25:05 -07001210 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid packed switch: last key=" << last_key
1211 << ", this=" << key;
jeffhaoba5ebb92011-08-25 17:24:37 -07001212 return false;
1213 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001214 last_key = key;
1215 }
1216 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001217 /* verify each switch target */
Ian Rogersd81871c2011-10-03 13:57:23 -07001218 for (uint32_t targ = 0; targ < switch_count; targ++) {
1219 int32_t offset = (int32_t) switch_insns[targets_offset + targ * 2] |
1220 (int32_t) (switch_insns[targets_offset + targ * 2 + 1] << 16);
1221 int32_t abs_offset = cur_offset + offset;
Brian Carlstrom93c33962013-07-26 10:37:43 -07001222 if (abs_offset < 0 ||
1223 abs_offset >= (int32_t) insn_count ||
1224 !insn_flags_[abs_offset].IsOpcode()) {
1225 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch target " << offset
1226 << " (-> " << reinterpret_cast<void*>(abs_offset) << ") at "
1227 << reinterpret_cast<void*>(cur_offset)
1228 << "[" << targ << "]";
jeffhaoba5ebb92011-08-25 17:24:37 -07001229 return false;
1230 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001231 insn_flags_[abs_offset].SetBranchTarget();
1232 }
1233 return true;
1234}
1235
Ian Rogers776ac1f2012-04-13 23:36:36 -07001236bool MethodVerifier::CheckVarArgRegs(uint32_t vA, uint32_t arg[]) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001237 uint16_t registers_size = code_item_->registers_size_;
1238 for (uint32_t idx = 0; idx < vA; idx++) {
jeffhao457cc512012-02-02 16:55:13 -08001239 if (arg[idx] >= registers_size) {
jeffhaod5347e02012-03-22 17:25:05 -07001240 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index (" << arg[idx]
1241 << ") in non-range invoke (>= " << registers_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001242 return false;
1243 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001244 }
1245
1246 return true;
1247}
1248
Ian Rogers776ac1f2012-04-13 23:36:36 -07001249bool MethodVerifier::CheckVarArgRangeRegs(uint32_t vA, uint32_t vC) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001250 uint16_t registers_size = code_item_->registers_size_;
1251 // vA/vC are unsigned 8-bit/16-bit quantities for /range instructions, so there's no risk of
1252 // integer overflow when adding them here.
1253 if (vA + vC > registers_size) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001254 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index " << vA << "+" << vC
1255 << " in range invoke (> " << registers_size << ")";
jeffhaoba5ebb92011-08-25 17:24:37 -07001256 return false;
1257 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001258 return true;
1259}
1260
Ian Rogers776ac1f2012-04-13 23:36:36 -07001261bool MethodVerifier::VerifyCodeFlow() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001262 uint16_t registers_size = code_item_->registers_size_;
1263 uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaobdb76512011-09-07 11:43:16 -07001264
Ian Rogersd81871c2011-10-03 13:57:23 -07001265 /* Create and initialize table holding register status */
Brian Carlstrom93c33962013-07-26 10:37:43 -07001266 reg_table_.Init(kTrackCompilerInterestPoints,
1267 insn_flags_.get(),
1268 insns_size,
1269 registers_size,
1270 this);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07001271
jeffhaobdb76512011-09-07 11:43:16 -07001272
Ian Rogersd0fbd852013-09-24 18:17:04 -07001273 work_line_.reset(RegisterLine::Create(registers_size, this));
1274 saved_line_.reset(RegisterLine::Create(registers_size, this));
jeffhaobdb76512011-09-07 11:43:16 -07001275
Ian Rogersd81871c2011-10-03 13:57:23 -07001276 /* Initialize register types of method arguments. */
1277 if (!SetTypesFromSignature()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001278 DCHECK_NE(failures_.size(), 0U);
1279 std::string prepend("Bad signature in ");
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001280 prepend += PrettyMethod(dex_method_idx_, *dex_file_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001281 PrependToLastFailMessage(prepend);
Ian Rogersd81871c2011-10-03 13:57:23 -07001282 return false;
1283 }
Andreas Gamped5ad72f2015-06-26 17:33:47 -07001284 // We may have a runtime failure here, clear.
1285 have_pending_runtime_throw_failure_ = false;
1286
Ian Rogersd81871c2011-10-03 13:57:23 -07001287 /* Perform code flow verification. */
1288 if (!CodeFlowVerifyMethod()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001289 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -07001290 return false;
jeffhaobdb76512011-09-07 11:43:16 -07001291 }
jeffhaobdb76512011-09-07 11:43:16 -07001292 return true;
1293}
1294
Ian Rogersad0b3a32012-04-16 14:50:24 -07001295std::ostream& MethodVerifier::DumpFailures(std::ostream& os) {
1296 DCHECK_EQ(failures_.size(), failure_messages_.size());
Jeff Hao4137f482013-11-22 11:44:57 -08001297 for (size_t i = 0; i < failures_.size(); ++i) {
1298 os << failure_messages_[i]->str() << "\n";
Ian Rogersad0b3a32012-04-16 14:50:24 -07001299 }
1300 return os;
1301}
1302
Ian Rogers776ac1f2012-04-13 23:36:36 -07001303void MethodVerifier::Dump(std::ostream& os) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001304 VariableIndentationOutputStream vios(&os);
1305 Dump(&vios);
1306}
1307
1308void MethodVerifier::Dump(VariableIndentationOutputStream* vios) {
Ian Rogers7b078e82014-09-10 14:44:24 -07001309 if (code_item_ == nullptr) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001310 vios->Stream() << "Native method\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001311 return;
jeffhaobdb76512011-09-07 11:43:16 -07001312 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001313 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001314 vios->Stream() << "Register Types:\n";
1315 ScopedIndentation indent1(vios);
1316 reg_types_.Dump(vios->Stream());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001317 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001318 vios->Stream() << "Dumping instructions and register lines:\n";
1319 ScopedIndentation indent1(vios);
Ian Rogersd81871c2011-10-03 13:57:23 -07001320 const Instruction* inst = Instruction::At(code_item_->insns_);
1321 for (size_t dex_pc = 0; dex_pc < code_item_->insns_size_in_code_units_;
Ian Rogers7b078e82014-09-10 14:44:24 -07001322 dex_pc += inst->SizeInCodeUnits()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001323 RegisterLine* reg_line = reg_table_.GetLine(dex_pc);
Ian Rogers7b078e82014-09-10 14:44:24 -07001324 if (reg_line != nullptr) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001325 vios->Stream() << reg_line->Dump(this) << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07001326 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001327 vios->Stream()
1328 << StringPrintf("0x%04zx", dex_pc) << ": " << insn_flags_[dex_pc].ToString() << " ";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001329 const bool kDumpHexOfInstruction = false;
1330 if (kDumpHexOfInstruction) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001331 vios->Stream() << inst->DumpHex(5) << " ";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001332 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001333 vios->Stream() << inst->DumpString(dex_file_) << "\n";
jeffhaoba5ebb92011-08-25 17:24:37 -07001334 inst = inst->Next();
1335 }
jeffhaobdb76512011-09-07 11:43:16 -07001336}
1337
Ian Rogersd81871c2011-10-03 13:57:23 -07001338static bool IsPrimitiveDescriptor(char descriptor) {
1339 switch (descriptor) {
jeffhaobdb76512011-09-07 11:43:16 -07001340 case 'I':
1341 case 'C':
1342 case 'S':
1343 case 'B':
1344 case 'Z':
jeffhaobdb76512011-09-07 11:43:16 -07001345 case 'F':
1346 case 'D':
1347 case 'J':
Ian Rogersd81871c2011-10-03 13:57:23 -07001348 return true;
jeffhaobdb76512011-09-07 11:43:16 -07001349 default:
1350 return false;
1351 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001352}
1353
Ian Rogers776ac1f2012-04-13 23:36:36 -07001354bool MethodVerifier::SetTypesFromSignature() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001355 RegisterLine* reg_line = reg_table_.GetLine(0);
Andreas Gampeef0b1a12015-06-19 20:37:46 -07001356
1357 // Should have been verified earlier.
1358 DCHECK_GE(code_item_->registers_size_, code_item_->ins_size_);
1359
1360 uint32_t arg_start = code_item_->registers_size_ - code_item_->ins_size_;
Ian Rogersd81871c2011-10-03 13:57:23 -07001361 size_t expected_args = code_item_->ins_size_; /* long/double count as two */
jeffhaobdb76512011-09-07 11:43:16 -07001362
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001363 // Include the "this" pointer.
Ian Rogersd81871c2011-10-03 13:57:23 -07001364 size_t cur_arg = 0;
Ian Rogersad0b3a32012-04-16 14:50:24 -07001365 if (!IsStatic()) {
Andreas Gampeef0b1a12015-06-19 20:37:46 -07001366 if (expected_args == 0) {
1367 // Expect at least a receiver.
1368 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected 0 args, but method is not static";
1369 return false;
1370 }
1371
Ian Rogersd81871c2011-10-03 13:57:23 -07001372 // If this is a constructor for a class other than java.lang.Object, mark the first ("this")
1373 // argument as uninitialized. This restricts field access until the superclass constructor is
1374 // called.
Ian Rogersd8f69b02014-09-10 21:43:52 +00001375 const RegType& declaring_class = GetDeclaringClass();
Andreas Gampef10b6e12015-08-12 10:48:12 -07001376 if (IsConstructor()) {
1377 if (declaring_class.IsJavaLangObject()) {
1378 // "this" is implicitly initialized.
1379 reg_line->SetThisInitialized();
1380 reg_line->SetRegisterType(this, arg_start + cur_arg, declaring_class);
1381 } else {
1382 reg_line->SetRegisterType(this, arg_start + cur_arg,
1383 reg_types_.UninitializedThisArgument(declaring_class));
1384 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001385 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07001386 reg_line->SetRegisterType(this, arg_start + cur_arg, declaring_class);
jeffhaobdb76512011-09-07 11:43:16 -07001387 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001388 cur_arg++;
jeffhaobdb76512011-09-07 11:43:16 -07001389 }
1390
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001391 const DexFile::ProtoId& proto_id =
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001392 dex_file_->GetMethodPrototype(dex_file_->GetMethodId(dex_method_idx_));
Ian Rogers0571d352011-11-03 19:51:38 -07001393 DexFileParameterIterator iterator(*dex_file_, proto_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07001394
1395 for (; iterator.HasNext(); iterator.Next()) {
1396 const char* descriptor = iterator.GetDescriptor();
Ian Rogers7b078e82014-09-10 14:44:24 -07001397 if (descriptor == nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001398 LOG(FATAL) << "Null descriptor";
1399 }
1400 if (cur_arg >= expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07001401 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1402 << " args, found more (" << descriptor << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001403 return false;
1404 }
1405 switch (descriptor[0]) {
1406 case 'L':
1407 case '[':
1408 // We assume that reference arguments are initialized. The only way it could be otherwise
1409 // (assuming the caller was verified) is if the current method is <init>, but in that case
1410 // it's effectively considered initialized the instant we reach here (in the sense that we
1411 // can return without doing anything or call virtual methods).
1412 {
Ian Rogersd8f69b02014-09-10 21:43:52 +00001413 const RegType& reg_type = ResolveClassAndCheckAccess(iterator.GetTypeIdx());
Sebastien Hertz2ed76f92014-04-22 17:11:08 +02001414 if (!reg_type.IsNonZeroReferenceTypes()) {
1415 DCHECK(HasFailures());
1416 return false;
1417 }
Ian Rogers7b078e82014-09-10 14:44:24 -07001418 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001419 }
1420 break;
1421 case 'Z':
Ian Rogers7b078e82014-09-10 14:44:24 -07001422 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_types_.Boolean());
Ian Rogersd81871c2011-10-03 13:57:23 -07001423 break;
1424 case 'C':
Ian Rogers7b078e82014-09-10 14:44:24 -07001425 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_types_.Char());
Ian Rogersd81871c2011-10-03 13:57:23 -07001426 break;
1427 case 'B':
Ian Rogers7b078e82014-09-10 14:44:24 -07001428 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_types_.Byte());
Ian Rogersd81871c2011-10-03 13:57:23 -07001429 break;
1430 case 'I':
Ian Rogers7b078e82014-09-10 14:44:24 -07001431 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_types_.Integer());
Ian Rogersd81871c2011-10-03 13:57:23 -07001432 break;
1433 case 'S':
Ian Rogers7b078e82014-09-10 14:44:24 -07001434 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_types_.Short());
Ian Rogersd81871c2011-10-03 13:57:23 -07001435 break;
1436 case 'F':
Ian Rogers7b078e82014-09-10 14:44:24 -07001437 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_types_.Float());
Ian Rogersd81871c2011-10-03 13:57:23 -07001438 break;
1439 case 'J':
1440 case 'D': {
Andreas Gampe77cd4d62014-06-19 17:29:48 -07001441 if (cur_arg + 1 >= expected_args) {
1442 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1443 << " args, found more (" << descriptor << ")";
1444 return false;
1445 }
1446
Ian Rogers7b078e82014-09-10 14:44:24 -07001447 const RegType* lo_half;
1448 const RegType* hi_half;
1449 if (descriptor[0] == 'J') {
1450 lo_half = &reg_types_.LongLo();
1451 hi_half = &reg_types_.LongHi();
1452 } else {
1453 lo_half = &reg_types_.DoubleLo();
1454 hi_half = &reg_types_.DoubleHi();
1455 }
1456 reg_line->SetRegisterTypeWide(this, arg_start + cur_arg, *lo_half, *hi_half);
Ian Rogersd81871c2011-10-03 13:57:23 -07001457 cur_arg++;
1458 break;
1459 }
1460 default:
Brian Carlstrom93c33962013-07-26 10:37:43 -07001461 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected signature type char '"
1462 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001463 return false;
1464 }
1465 cur_arg++;
1466 }
1467 if (cur_arg != expected_args) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001468 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1469 << " arguments, found " << cur_arg;
Ian Rogersd81871c2011-10-03 13:57:23 -07001470 return false;
1471 }
1472 const char* descriptor = dex_file_->GetReturnTypeDescriptor(proto_id);
1473 // Validate return type. We don't do the type lookup; just want to make sure that it has the right
1474 // format. Only major difference from the method argument format is that 'V' is supported.
1475 bool result;
1476 if (IsPrimitiveDescriptor(descriptor[0]) || descriptor[0] == 'V') {
1477 result = descriptor[1] == '\0';
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001478 } else if (descriptor[0] == '[') { // single/multi-dimensional array of object/primitive
Ian Rogersd81871c2011-10-03 13:57:23 -07001479 size_t i = 0;
1480 do {
1481 i++;
1482 } while (descriptor[i] == '['); // process leading [
1483 if (descriptor[i] == 'L') { // object array
1484 do {
1485 i++; // find closing ;
1486 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1487 result = descriptor[i] == ';';
1488 } else { // primitive array
1489 result = IsPrimitiveDescriptor(descriptor[i]) && descriptor[i + 1] == '\0';
1490 }
1491 } else if (descriptor[0] == 'L') {
1492 // could be more thorough here, but shouldn't be required
1493 size_t i = 0;
1494 do {
1495 i++;
1496 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1497 result = descriptor[i] == ';';
1498 } else {
1499 result = false;
1500 }
1501 if (!result) {
jeffhaod5347e02012-03-22 17:25:05 -07001502 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected char in return type descriptor '"
1503 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001504 }
1505 return result;
jeffhaobdb76512011-09-07 11:43:16 -07001506}
1507
Ian Rogers776ac1f2012-04-13 23:36:36 -07001508bool MethodVerifier::CodeFlowVerifyMethod() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001509 const uint16_t* insns = code_item_->insns_;
1510 const uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaoba5ebb92011-08-25 17:24:37 -07001511
jeffhaobdb76512011-09-07 11:43:16 -07001512 /* Begin by marking the first instruction as "changed". */
Ian Rogersd81871c2011-10-03 13:57:23 -07001513 insn_flags_[0].SetChanged();
1514 uint32_t start_guess = 0;
jeffhaoba5ebb92011-08-25 17:24:37 -07001515
jeffhaobdb76512011-09-07 11:43:16 -07001516 /* Continue until no instructions are marked "changed". */
1517 while (true) {
Mathieu Chartier4306ef82014-12-19 18:41:47 -08001518 if (allow_thread_suspension_) {
1519 self_->AllowThreadSuspension();
1520 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001521 // Find the first marked one. Use "start_guess" as a way to find one quickly.
1522 uint32_t insn_idx = start_guess;
1523 for (; insn_idx < insns_size; insn_idx++) {
1524 if (insn_flags_[insn_idx].IsChanged())
jeffhaobdb76512011-09-07 11:43:16 -07001525 break;
1526 }
jeffhaobdb76512011-09-07 11:43:16 -07001527 if (insn_idx == insns_size) {
1528 if (start_guess != 0) {
1529 /* try again, starting from the top */
1530 start_guess = 0;
1531 continue;
1532 } else {
1533 /* all flags are clear */
1534 break;
1535 }
1536 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001537 // We carry the working set of registers from instruction to instruction. If this address can
1538 // be the target of a branch (or throw) instruction, or if we're skipping around chasing
1539 // "changed" flags, we need to load the set of registers from the table.
1540 // Because we always prefer to continue on to the next instruction, we should never have a
1541 // situation where we have a stray "changed" flag set on an instruction that isn't a branch
1542 // target.
1543 work_insn_idx_ = insn_idx;
1544 if (insn_flags_[insn_idx].IsBranchTarget()) {
1545 work_line_->CopyFromLine(reg_table_.GetLine(insn_idx));
Ian Rogersebbdd872014-07-07 23:53:08 -07001546 } else if (kIsDebugBuild) {
jeffhaobdb76512011-09-07 11:43:16 -07001547 /*
1548 * Sanity check: retrieve the stored register line (assuming
1549 * a full table) and make sure it actually matches.
1550 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001551 RegisterLine* register_line = reg_table_.GetLine(insn_idx);
Ian Rogers7b078e82014-09-10 14:44:24 -07001552 if (register_line != nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001553 if (work_line_->CompareLine(register_line) != 0) {
1554 Dump(std::cout);
1555 std::cout << info_messages_.str();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001556 LOG(FATAL) << "work_line diverged in " << PrettyMethod(dex_method_idx_, *dex_file_)
Elliott Hughesc073b072012-05-24 19:29:17 -07001557 << "@" << reinterpret_cast<void*>(work_insn_idx_) << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07001558 << " work_line=" << work_line_->Dump(this) << "\n"
1559 << " expected=" << register_line->Dump(this);
Ian Rogersd81871c2011-10-03 13:57:23 -07001560 }
jeffhaobdb76512011-09-07 11:43:16 -07001561 }
jeffhaobdb76512011-09-07 11:43:16 -07001562 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001563 if (!CodeFlowVerifyInstruction(&start_guess)) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001564 std::string prepend(PrettyMethod(dex_method_idx_, *dex_file_));
Ian Rogersad0b3a32012-04-16 14:50:24 -07001565 prepend += " failed to verify: ";
1566 PrependToLastFailMessage(prepend);
jeffhaoba5ebb92011-08-25 17:24:37 -07001567 return false;
1568 }
jeffhaobdb76512011-09-07 11:43:16 -07001569 /* Clear "changed" and mark as visited. */
Ian Rogersd81871c2011-10-03 13:57:23 -07001570 insn_flags_[insn_idx].SetVisited();
1571 insn_flags_[insn_idx].ClearChanged();
jeffhaobdb76512011-09-07 11:43:16 -07001572 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001573
Ian Rogers1c849e52012-06-28 14:00:33 -07001574 if (gDebugVerify) {
jeffhaobdb76512011-09-07 11:43:16 -07001575 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001576 * Scan for dead code. There's nothing "evil" about dead code
jeffhaobdb76512011-09-07 11:43:16 -07001577 * (besides the wasted space), but it indicates a flaw somewhere
1578 * down the line, possibly in the verifier.
1579 *
1580 * If we've substituted "always throw" instructions into the stream,
1581 * we are almost certainly going to have some dead code.
1582 */
1583 int dead_start = -1;
Ian Rogersd81871c2011-10-03 13:57:23 -07001584 uint32_t insn_idx = 0;
Ian Rogers7b078e82014-09-10 14:44:24 -07001585 for (; insn_idx < insns_size;
1586 insn_idx += Instruction::At(code_item_->insns_ + insn_idx)->SizeInCodeUnits()) {
jeffhaobdb76512011-09-07 11:43:16 -07001587 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001588 * Switch-statement data doesn't get "visited" by scanner. It
jeffhaobdb76512011-09-07 11:43:16 -07001589 * may or may not be preceded by a padding NOP (for alignment).
1590 */
1591 if (insns[insn_idx] == Instruction::kPackedSwitchSignature ||
1592 insns[insn_idx] == Instruction::kSparseSwitchSignature ||
1593 insns[insn_idx] == Instruction::kArrayDataSignature ||
Elliott Hughes380aaa72012-07-09 14:33:15 -07001594 (insns[insn_idx] == Instruction::NOP && (insn_idx + 1 < insns_size) &&
jeffhaobdb76512011-09-07 11:43:16 -07001595 (insns[insn_idx + 1] == Instruction::kPackedSwitchSignature ||
1596 insns[insn_idx + 1] == Instruction::kSparseSwitchSignature ||
1597 insns[insn_idx + 1] == Instruction::kArrayDataSignature))) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001598 insn_flags_[insn_idx].SetVisited();
jeffhaobdb76512011-09-07 11:43:16 -07001599 }
1600
Ian Rogersd81871c2011-10-03 13:57:23 -07001601 if (!insn_flags_[insn_idx].IsVisited()) {
jeffhaobdb76512011-09-07 11:43:16 -07001602 if (dead_start < 0)
1603 dead_start = insn_idx;
1604 } else if (dead_start >= 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001605 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start)
1606 << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaobdb76512011-09-07 11:43:16 -07001607 dead_start = -1;
1608 }
1609 }
1610 if (dead_start >= 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001611 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start)
1612 << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaoba5ebb92011-08-25 17:24:37 -07001613 }
Ian Rogersc9e463c2013-06-05 16:52:26 -07001614 // To dump the state of the verify after a method, do something like:
1615 // if (PrettyMethod(dex_method_idx_, *dex_file_) ==
1616 // "boolean java.lang.String.equals(java.lang.Object)") {
1617 // LOG(INFO) << info_messages_.str();
1618 // }
jeffhaoba5ebb92011-08-25 17:24:37 -07001619 }
jeffhaobdb76512011-09-07 11:43:16 -07001620 return true;
1621}
1622
Andreas Gampe68df3202015-06-22 11:35:46 -07001623// Returns the index of the first final instance field of the given class, or kDexNoIndex if there
1624// is no such field.
1625static uint32_t GetFirstFinalInstanceFieldIndex(const DexFile& dex_file, uint16_t type_idx) {
1626 const DexFile::ClassDef* class_def = dex_file.FindClassDef(type_idx);
1627 DCHECK(class_def != nullptr);
1628 const uint8_t* class_data = dex_file.GetClassData(*class_def);
1629 DCHECK(class_data != nullptr);
1630 ClassDataItemIterator it(dex_file, class_data);
1631 // Skip static fields.
1632 while (it.HasNextStaticField()) {
1633 it.Next();
1634 }
1635 while (it.HasNextInstanceField()) {
1636 if ((it.GetFieldAccessFlags() & kAccFinal) != 0) {
1637 return it.GetMemberIndex();
1638 }
1639 it.Next();
1640 }
1641 return DexFile::kDexNoIndex;
1642}
1643
Ian Rogers776ac1f2012-04-13 23:36:36 -07001644bool MethodVerifier::CodeFlowVerifyInstruction(uint32_t* start_guess) {
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001645 // If we're doing FindLocksAtDexPc, check whether we're at the dex pc we care about.
1646 // We want the state _before_ the instruction, for the case where the dex pc we're
1647 // interested in is itself a monitor-enter instruction (which is a likely place
1648 // for a thread to be suspended).
Ian Rogers7b078e82014-09-10 14:44:24 -07001649 if (monitor_enter_dex_pcs_ != nullptr && work_insn_idx_ == interesting_dex_pc_) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001650 monitor_enter_dex_pcs_->clear(); // The new work line is more accurate than the previous one.
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001651 for (size_t i = 0; i < work_line_->GetMonitorEnterCount(); ++i) {
1652 monitor_enter_dex_pcs_->push_back(work_line_->GetMonitorEnterDexPc(i));
1653 }
1654 }
1655
jeffhaobdb76512011-09-07 11:43:16 -07001656 /*
1657 * Once we finish decoding the instruction, we need to figure out where
jeffhaod1f0fde2011-09-08 17:25:33 -07001658 * we can go from here. There are three possible ways to transfer
jeffhaobdb76512011-09-07 11:43:16 -07001659 * control to another statement:
1660 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001661 * (1) Continue to the next instruction. Applies to all but
jeffhaobdb76512011-09-07 11:43:16 -07001662 * unconditional branches, method returns, and exception throws.
jeffhaod1f0fde2011-09-08 17:25:33 -07001663 * (2) Branch to one or more possible locations. Applies to branches
jeffhaobdb76512011-09-07 11:43:16 -07001664 * and switch statements.
jeffhaod1f0fde2011-09-08 17:25:33 -07001665 * (3) Exception handlers. Applies to any instruction that can
jeffhaobdb76512011-09-07 11:43:16 -07001666 * throw an exception that is handled by an encompassing "try"
1667 * block.
1668 *
1669 * We can also return, in which case there is no successor instruction
1670 * from this point.
1671 *
Elliott Hughesadb8c672012-03-06 16:49:32 -08001672 * The behavior can be determined from the opcode flags.
jeffhaobdb76512011-09-07 11:43:16 -07001673 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001674 const uint16_t* insns = code_item_->insns_ + work_insn_idx_;
1675 const Instruction* inst = Instruction::At(insns);
Ian Rogersa75a0132012-09-28 11:41:42 -07001676 int opcode_flags = Instruction::FlagsOf(inst->Opcode());
jeffhaobdb76512011-09-07 11:43:16 -07001677
jeffhaobdb76512011-09-07 11:43:16 -07001678 int32_t branch_target = 0;
jeffhaobdb76512011-09-07 11:43:16 -07001679 bool just_set_result = false;
Ian Rogers2c8a8572011-10-24 17:11:36 -07001680 if (gDebugVerify) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001681 // Generate processing back trace to debug verifier
Elliott Hughesc073b072012-05-24 19:29:17 -07001682 LogVerifyInfo() << "Processing " << inst->DumpString(dex_file_) << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07001683 << work_line_->Dump(this) << "\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001684 }
jeffhaobdb76512011-09-07 11:43:16 -07001685
1686 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001687 * Make a copy of the previous register state. If the instruction
jeffhaobdb76512011-09-07 11:43:16 -07001688 * can throw an exception, we will copy/merge this into the "catch"
1689 * address rather than work_line, because we don't want the result
1690 * from the "successful" code path (e.g. a check-cast that "improves"
1691 * a type) to be visible to the exception handler.
1692 */
Ian Rogers776ac1f2012-04-13 23:36:36 -07001693 if ((opcode_flags & Instruction::kThrow) != 0 && CurrentInsnFlags()->IsInTry()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001694 saved_line_->CopyFromLine(work_line_.get());
Ian Rogers1ff3c982014-08-12 02:30:58 -07001695 } else if (kIsDebugBuild) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001696 saved_line_->FillWithGarbage();
jeffhaobdb76512011-09-07 11:43:16 -07001697 }
Andreas Gamped12e7822015-06-25 10:26:40 -07001698 DCHECK(!have_pending_runtime_throw_failure_); // Per-instruction flag, should not be set here.
jeffhaobdb76512011-09-07 11:43:16 -07001699
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07001700
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001701 // We need to ensure the work line is consistent while performing validation. When we spot a
1702 // peephole pattern we compute a new line for either the fallthrough instruction or the
1703 // branch target.
Ian Rogers700a4022014-05-19 16:49:03 -07001704 std::unique_ptr<RegisterLine> branch_line;
1705 std::unique_ptr<RegisterLine> fallthrough_line;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001706
Sebastien Hertz5243e912013-05-21 10:55:07 +02001707 switch (inst->Opcode()) {
jeffhaobdb76512011-09-07 11:43:16 -07001708 case Instruction::NOP:
1709 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001710 * A "pure" NOP has no effect on anything. Data tables start with
jeffhaobdb76512011-09-07 11:43:16 -07001711 * a signature that looks like a NOP; if we see one of these in
1712 * the course of executing code then we have a problem.
1713 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001714 if (inst->VRegA_10x() != 0) {
jeffhaod5347e02012-03-22 17:25:05 -07001715 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "encountered data table in instruction stream";
jeffhaobdb76512011-09-07 11:43:16 -07001716 }
1717 break;
1718
1719 case Instruction::MOVE:
Ian Rogers7b078e82014-09-10 14:44:24 -07001720 work_line_->CopyRegister1(this, inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategory1nr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001721 break;
jeffhaobdb76512011-09-07 11:43:16 -07001722 case Instruction::MOVE_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001723 work_line_->CopyRegister1(this, inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategory1nr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001724 break;
jeffhaobdb76512011-09-07 11:43:16 -07001725 case Instruction::MOVE_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001726 work_line_->CopyRegister1(this, inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategory1nr);
jeffhaobdb76512011-09-07 11:43:16 -07001727 break;
1728 case Instruction::MOVE_WIDE:
Ian Rogers7b078e82014-09-10 14:44:24 -07001729 work_line_->CopyRegister2(this, inst->VRegA_12x(), inst->VRegB_12x());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001730 break;
jeffhaobdb76512011-09-07 11:43:16 -07001731 case Instruction::MOVE_WIDE_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001732 work_line_->CopyRegister2(this, inst->VRegA_22x(), inst->VRegB_22x());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001733 break;
jeffhaobdb76512011-09-07 11:43:16 -07001734 case Instruction::MOVE_WIDE_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001735 work_line_->CopyRegister2(this, inst->VRegA_32x(), inst->VRegB_32x());
jeffhaobdb76512011-09-07 11:43:16 -07001736 break;
1737 case Instruction::MOVE_OBJECT:
Ian Rogers7b078e82014-09-10 14:44:24 -07001738 work_line_->CopyRegister1(this, inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategoryRef);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001739 break;
jeffhaobdb76512011-09-07 11:43:16 -07001740 case Instruction::MOVE_OBJECT_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001741 work_line_->CopyRegister1(this, inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategoryRef);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001742 break;
jeffhaobdb76512011-09-07 11:43:16 -07001743 case Instruction::MOVE_OBJECT_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001744 work_line_->CopyRegister1(this, inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategoryRef);
jeffhaobdb76512011-09-07 11:43:16 -07001745 break;
1746
1747 /*
1748 * The move-result instructions copy data out of a "pseudo-register"
jeffhaod1f0fde2011-09-08 17:25:33 -07001749 * with the results from the last method invocation. In practice we
jeffhaobdb76512011-09-07 11:43:16 -07001750 * might want to hold the result in an actual CPU register, so the
1751 * Dalvik spec requires that these only appear immediately after an
1752 * invoke or filled-new-array.
1753 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001754 * These calls invalidate the "result" register. (This is now
jeffhaobdb76512011-09-07 11:43:16 -07001755 * redundant with the reset done below, but it can make the debug info
1756 * easier to read in some cases.)
1757 */
1758 case Instruction::MOVE_RESULT:
Ian Rogers7b078e82014-09-10 14:44:24 -07001759 work_line_->CopyResultRegister1(this, inst->VRegA_11x(), false);
jeffhaobdb76512011-09-07 11:43:16 -07001760 break;
1761 case Instruction::MOVE_RESULT_WIDE:
Ian Rogers7b078e82014-09-10 14:44:24 -07001762 work_line_->CopyResultRegister2(this, inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07001763 break;
1764 case Instruction::MOVE_RESULT_OBJECT:
Ian Rogers7b078e82014-09-10 14:44:24 -07001765 work_line_->CopyResultRegister1(this, inst->VRegA_11x(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001766 break;
1767
Ian Rogersd81871c2011-10-03 13:57:23 -07001768 case Instruction::MOVE_EXCEPTION: {
Sebastien Hertz270a0e12015-01-16 19:49:09 +01001769 // We do not allow MOVE_EXCEPTION as the first instruction in a method. This is a simple case
1770 // where one entrypoint to the catch block is not actually an exception path.
1771 if (work_insn_idx_ == 0) {
1772 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "move-exception at pc 0x0";
1773 break;
1774 }
jeffhaobdb76512011-09-07 11:43:16 -07001775 /*
jeffhao60f83e32012-02-13 17:16:30 -08001776 * This statement can only appear as the first instruction in an exception handler. We verify
1777 * that as part of extracting the exception type from the catch block list.
jeffhaobdb76512011-09-07 11:43:16 -07001778 */
Ian Rogersd8f69b02014-09-10 21:43:52 +00001779 const RegType& res_type = GetCaughtExceptionType();
Ian Rogers7b078e82014-09-10 14:44:24 -07001780 work_line_->SetRegisterType(this, inst->VRegA_11x(), res_type);
jeffhaobdb76512011-09-07 11:43:16 -07001781 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001782 }
jeffhaobdb76512011-09-07 11:43:16 -07001783 case Instruction::RETURN_VOID:
Andreas Gampef10b6e12015-08-12 10:48:12 -07001784 if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001785 if (!GetMethodReturnType().IsConflict()) {
jeffhaod5347e02012-03-22 17:25:05 -07001786 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001787 }
jeffhaobdb76512011-09-07 11:43:16 -07001788 }
1789 break;
1790 case Instruction::RETURN:
Andreas Gampef10b6e12015-08-12 10:48:12 -07001791 if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) {
jeffhaobdb76512011-09-07 11:43:16 -07001792 /* check the method signature */
Ian Rogersd8f69b02014-09-10 21:43:52 +00001793 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07001794 if (!return_type.IsCategory1Types()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001795 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected non-category 1 return type "
1796 << return_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001797 } else {
1798 // Compilers may generate synthetic functions that write byte values into boolean fields.
1799 // Also, it may use integer values for boolean, byte, short, and character return types.
Sebastien Hertz5243e912013-05-21 10:55:07 +02001800 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07001801 const RegType& src_type = work_line_->GetRegisterType(this, vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07001802 bool use_src = ((return_type.IsBoolean() && src_type.IsByte()) ||
1803 ((return_type.IsBoolean() || return_type.IsByte() ||
1804 return_type.IsShort() || return_type.IsChar()) &&
1805 src_type.IsInteger()));
1806 /* check the register contents */
Ian Rogersad0b3a32012-04-16 14:50:24 -07001807 bool success =
Ian Rogers7b078e82014-09-10 14:44:24 -07001808 work_line_->VerifyRegisterType(this, vregA, use_src ? src_type : return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001809 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001810 AppendToLastFailMessage(StringPrintf(" return-1nr on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07001811 }
jeffhaobdb76512011-09-07 11:43:16 -07001812 }
1813 }
1814 break;
1815 case Instruction::RETURN_WIDE:
Andreas Gampef10b6e12015-08-12 10:48:12 -07001816 if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) {
jeffhaobdb76512011-09-07 11:43:16 -07001817 /* check the method signature */
Ian Rogersd8f69b02014-09-10 21:43:52 +00001818 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07001819 if (!return_type.IsCategory2Types()) {
jeffhaod5347e02012-03-22 17:25:05 -07001820 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-wide not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001821 } else {
1822 /* check the register contents */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001823 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07001824 bool success = work_line_->VerifyRegisterType(this, vregA, return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001825 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001826 AppendToLastFailMessage(StringPrintf(" return-wide on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07001827 }
jeffhaobdb76512011-09-07 11:43:16 -07001828 }
1829 }
1830 break;
1831 case Instruction::RETURN_OBJECT:
Andreas Gampef10b6e12015-08-12 10:48:12 -07001832 if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) {
Ian Rogersd8f69b02014-09-10 21:43:52 +00001833 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07001834 if (!return_type.IsReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001835 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-object not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001836 } else {
1837 /* return_type is the *expected* return type, not register value */
1838 DCHECK(!return_type.IsZero());
1839 DCHECK(!return_type.IsUninitializedReference());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001840 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07001841 const RegType& reg_type = work_line_->GetRegisterType(this, vregA);
Andreas Gampea32210c2015-06-24 10:26:13 -07001842 // Disallow returning undefined, conflict & uninitialized values and verify that the
1843 // reference in vAA is an instance of the "return_type."
1844 if (reg_type.IsUndefined()) {
1845 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning undefined register";
1846 } else if (reg_type.IsConflict()) {
1847 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning register with conflict";
1848 } else if (reg_type.IsUninitializedTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001849 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "returning uninitialized object '"
1850 << reg_type << "'";
Ian Rogers9074b992011-10-26 17:41:55 -07001851 } else if (!return_type.IsAssignableFrom(reg_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07001852 if (reg_type.IsUnresolvedTypes() || return_type.IsUnresolvedTypes()) {
1853 Fail(VERIFY_ERROR_NO_CLASS) << " can't resolve returned type '" << return_type
1854 << "' or '" << reg_type << "'";
1855 } else {
Andreas Gampe16f149c2015-03-23 10:10:20 -07001856 bool soft_error = false;
1857 // Check whether arrays are involved. They will show a valid class status, even
1858 // if their components are erroneous.
1859 if (reg_type.IsArrayTypes() && return_type.IsArrayTypes()) {
1860 return_type.CanAssignArray(reg_type, reg_types_, class_loader_, &soft_error);
1861 if (soft_error) {
1862 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "array with erroneous component type: "
1863 << reg_type << " vs " << return_type;
1864 }
1865 }
1866
1867 if (!soft_error) {
1868 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning '" << reg_type
1869 << "', but expected from declaration '" << return_type << "'";
1870 }
Jeff Haoa3faaf42013-09-03 19:07:00 -07001871 }
jeffhaobdb76512011-09-07 11:43:16 -07001872 }
1873 }
1874 }
1875 break;
1876
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001877 /* could be boolean, int, float, or a null reference */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001878 case Instruction::CONST_4: {
1879 int32_t val = static_cast<int32_t>(inst->VRegB_11n() << 28) >> 28;
Ian Rogers7b078e82014-09-10 14:44:24 -07001880 work_line_->SetRegisterType(this, inst->VRegA_11n(),
Ian Rogers46960fe2014-05-23 10:43:43 -07001881 DetermineCat1Constant(val, need_precise_constants_));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001882 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02001883 }
1884 case Instruction::CONST_16: {
1885 int16_t val = static_cast<int16_t>(inst->VRegB_21s());
Ian Rogers7b078e82014-09-10 14:44:24 -07001886 work_line_->SetRegisterType(this, inst->VRegA_21s(),
Ian Rogers46960fe2014-05-23 10:43:43 -07001887 DetermineCat1Constant(val, need_precise_constants_));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001888 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02001889 }
Sebastien Hertz849600b2013-12-20 10:28:08 +01001890 case Instruction::CONST: {
1891 int32_t val = inst->VRegB_31i();
Ian Rogers7b078e82014-09-10 14:44:24 -07001892 work_line_->SetRegisterType(this, inst->VRegA_31i(),
Ian Rogers46960fe2014-05-23 10:43:43 -07001893 DetermineCat1Constant(val, need_precise_constants_));
jeffhaobdb76512011-09-07 11:43:16 -07001894 break;
Sebastien Hertz849600b2013-12-20 10:28:08 +01001895 }
1896 case Instruction::CONST_HIGH16: {
1897 int32_t val = static_cast<int32_t>(inst->VRegB_21h() << 16);
Ian Rogers7b078e82014-09-10 14:44:24 -07001898 work_line_->SetRegisterType(this, inst->VRegA_21h(),
Ian Rogers46960fe2014-05-23 10:43:43 -07001899 DetermineCat1Constant(val, need_precise_constants_));
jeffhaobdb76512011-09-07 11:43:16 -07001900 break;
Sebastien Hertz849600b2013-12-20 10:28:08 +01001901 }
jeffhaobdb76512011-09-07 11:43:16 -07001902 /* could be long or double; resolved upon use */
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001903 case Instruction::CONST_WIDE_16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001904 int64_t val = static_cast<int16_t>(inst->VRegB_21s());
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_21s(), lo, hi);
jeffhaobdb76512011-09-07 11:43:16 -07001908 break;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001909 }
1910 case Instruction::CONST_WIDE_32: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001911 int64_t val = static_cast<int32_t>(inst->VRegB_31i());
Ian Rogersd8f69b02014-09-10 21:43:52 +00001912 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1913 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07001914 work_line_->SetRegisterTypeWide(this, inst->VRegA_31i(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001915 break;
1916 }
1917 case Instruction::CONST_WIDE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001918 int64_t val = inst->VRegB_51l();
Ian Rogersd8f69b02014-09-10 21:43:52 +00001919 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1920 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07001921 work_line_->SetRegisterTypeWide(this, inst->VRegA_51l(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001922 break;
1923 }
1924 case Instruction::CONST_WIDE_HIGH16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001925 int64_t val = static_cast<uint64_t>(inst->VRegB_21h()) << 48;
Ian Rogersd8f69b02014-09-10 21:43:52 +00001926 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1927 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07001928 work_line_->SetRegisterTypeWide(this, inst->VRegA_21h(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001929 break;
1930 }
jeffhaobdb76512011-09-07 11:43:16 -07001931 case Instruction::CONST_STRING:
Ian Rogers7b078e82014-09-10 14:44:24 -07001932 work_line_->SetRegisterType(this, inst->VRegA_21c(), reg_types_.JavaLangString());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001933 break;
jeffhaobdb76512011-09-07 11:43:16 -07001934 case Instruction::CONST_STRING_JUMBO:
Ian Rogers7b078e82014-09-10 14:44:24 -07001935 work_line_->SetRegisterType(this, inst->VRegA_31c(), reg_types_.JavaLangString());
jeffhaobdb76512011-09-07 11:43:16 -07001936 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001937 case Instruction::CONST_CLASS: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001938 // Get type from instruction if unresolved then we need an access check
1939 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Ian Rogersd8f69b02014-09-10 21:43:52 +00001940 const RegType& res_type = ResolveClassAndCheckAccess(inst->VRegB_21c());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001941 // Register holds class, ie its type is class, on error it will hold Conflict.
Ian Rogers7b078e82014-09-10 14:44:24 -07001942 work_line_->SetRegisterType(this, inst->VRegA_21c(),
Ian Rogersb4903572012-10-11 11:52:56 -07001943 res_type.IsConflict() ? res_type
Ian Rogers7b078e82014-09-10 14:44:24 -07001944 : reg_types_.JavaLangClass());
jeffhaobdb76512011-09-07 11:43:16 -07001945 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001946 }
jeffhaobdb76512011-09-07 11:43:16 -07001947 case Instruction::MONITOR_ENTER:
Ian Rogers7b078e82014-09-10 14:44:24 -07001948 work_line_->PushMonitor(this, inst->VRegA_11x(), work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07001949 break;
1950 case Instruction::MONITOR_EXIT:
1951 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001952 * monitor-exit instructions are odd. They can throw exceptions,
jeffhaobdb76512011-09-07 11:43:16 -07001953 * but when they do they act as if they succeeded and the PC is
jeffhaod1f0fde2011-09-08 17:25:33 -07001954 * pointing to the following instruction. (This behavior goes back
jeffhaobdb76512011-09-07 11:43:16 -07001955 * to the need to handle asynchronous exceptions, a now-deprecated
1956 * feature that Dalvik doesn't support.)
1957 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001958 * In practice we don't need to worry about this. The only
jeffhaobdb76512011-09-07 11:43:16 -07001959 * exceptions that can be thrown from monitor-exit are for a
jeffhaod1f0fde2011-09-08 17:25:33 -07001960 * null reference and -exit without a matching -enter. If the
jeffhaobdb76512011-09-07 11:43:16 -07001961 * structured locking checks are working, the former would have
1962 * failed on the -enter instruction, and the latter is impossible.
1963 *
1964 * This is fortunate, because issue 3221411 prevents us from
1965 * chasing the "can throw" path when monitor verification is
jeffhaod1f0fde2011-09-08 17:25:33 -07001966 * enabled. If we can fully verify the locking we can ignore
jeffhaobdb76512011-09-07 11:43:16 -07001967 * some catch blocks (which will show up as "dead" code when
1968 * we skip them here); if we can't, then the code path could be
1969 * "live" so we still need to check it.
1970 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001971 opcode_flags &= ~Instruction::kThrow;
Ian Rogers7b078e82014-09-10 14:44:24 -07001972 work_line_->PopMonitor(this, inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07001973 break;
1974
Ian Rogers28ad40d2011-10-27 15:19:26 -07001975 case Instruction::CHECK_CAST:
Ian Rogersd81871c2011-10-03 13:57:23 -07001976 case Instruction::INSTANCE_OF: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001977 /*
1978 * If this instruction succeeds, we will "downcast" register vA to the type in vB. (This
1979 * could be a "upcast" -- not expected, so we don't try to address it.)
1980 *
1981 * If it fails, an exception is thrown, which we deal with later by ignoring the update to
Elliott Hughesadb8c672012-03-06 16:49:32 -08001982 * dec_insn.vA when branching to a handler.
Ian Rogers28ad40d2011-10-27 15:19:26 -07001983 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001984 const bool is_checkcast = (inst->Opcode() == Instruction::CHECK_CAST);
1985 const uint32_t type_idx = (is_checkcast) ? inst->VRegB_21c() : inst->VRegC_22c();
Ian Rogersd8f69b02014-09-10 21:43:52 +00001986 const RegType& res_type = ResolveClassAndCheckAccess(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001987 if (res_type.IsConflict()) {
Andreas Gampe00633eb2014-07-17 16:13:35 -07001988 // If this is a primitive type, fail HARD.
Mathieu Chartierbf99f772014-08-23 16:37:27 -07001989 mirror::Class* klass = dex_cache_->GetResolvedType(type_idx);
Andreas Gampe00633eb2014-07-17 16:13:35 -07001990 if (klass != nullptr && klass->IsPrimitive()) {
1991 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "using primitive type "
1992 << dex_file_->StringByTypeIdx(type_idx) << " in instanceof in "
1993 << GetDeclaringClass();
1994 break;
1995 }
1996
Ian Rogersad0b3a32012-04-16 14:50:24 -07001997 DCHECK_NE(failures_.size(), 0U);
1998 if (!is_checkcast) {
Ian Rogers7b078e82014-09-10 14:44:24 -07001999 work_line_->SetRegisterType(this, inst->VRegA_22c(), reg_types_.Boolean());
Ian Rogersad0b3a32012-04-16 14:50:24 -07002000 }
2001 break; // bad class
Ian Rogers9f1ab122011-12-12 08:52:43 -08002002 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002003 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Sebastien Hertz5243e912013-05-21 10:55:07 +02002004 uint32_t orig_type_reg = (is_checkcast) ? inst->VRegA_21c() : inst->VRegB_22c();
Ian Rogers7b078e82014-09-10 14:44:24 -07002005 const RegType& orig_type = work_line_->GetRegisterType(this, orig_type_reg);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002006 if (!res_type.IsNonZeroReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002007 if (is_checkcast) {
2008 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on unexpected class " << res_type;
2009 } else {
2010 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on unexpected class " << res_type;
2011 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002012 } else if (!orig_type.IsReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002013 if (is_checkcast) {
2014 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on non-reference in v" << orig_type_reg;
2015 } else {
2016 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on non-reference in v" << orig_type_reg;
2017 }
jeffhao2a8a90e2011-09-26 14:25:31 -07002018 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002019 if (is_checkcast) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002020 work_line_->SetRegisterType(this, inst->VRegA_21c(), res_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07002021 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07002022 work_line_->SetRegisterType(this, inst->VRegA_22c(), reg_types_.Boolean());
jeffhaobdb76512011-09-07 11:43:16 -07002023 }
jeffhaobdb76512011-09-07 11:43:16 -07002024 }
jeffhao2a8a90e2011-09-26 14:25:31 -07002025 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002026 }
2027 case Instruction::ARRAY_LENGTH: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002028 const RegType& res_type = work_line_->GetRegisterType(this, inst->VRegB_12x());
Ian Rogers28ad40d2011-10-27 15:19:26 -07002029 if (res_type.IsReferenceTypes()) {
Ian Rogers89310de2012-02-01 13:47:30 -08002030 if (!res_type.IsArrayTypes() && !res_type.IsZero()) { // ie not an array or null
jeffhaod5347e02012-03-22 17:25:05 -07002031 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002032 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07002033 work_line_->SetRegisterType(this, inst->VRegA_12x(), reg_types_.Integer());
Ian Rogersd81871c2011-10-03 13:57:23 -07002034 }
Andreas Gampe65c9db82014-07-28 13:14:34 -07002035 } else {
2036 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002037 }
2038 break;
2039 }
2040 case Instruction::NEW_INSTANCE: {
Ian Rogersd8f69b02014-09-10 21:43:52 +00002041 const RegType& res_type = ResolveClassAndCheckAccess(inst->VRegB_21c());
Ian Rogersad0b3a32012-04-16 14:50:24 -07002042 if (res_type.IsConflict()) {
2043 DCHECK_NE(failures_.size(), 0U);
2044 break; // bad class
jeffhao8cd6dda2012-02-22 10:15:34 -08002045 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002046 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
2047 // can't create an instance of an interface or abstract class */
2048 if (!res_type.IsInstantiableTypes()) {
2049 Fail(VERIFY_ERROR_INSTANTIATION)
2050 << "new-instance on primitive, interface or abstract class" << res_type;
Ian Rogers08f753d2012-08-24 14:35:25 -07002051 // Soft failure so carry on to set register type.
Ian Rogersd81871c2011-10-03 13:57:23 -07002052 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00002053 const RegType& uninit_type = reg_types_.Uninitialized(res_type, work_insn_idx_);
Ian Rogers08f753d2012-08-24 14:35:25 -07002054 // Any registers holding previous allocations from this address that have not yet been
2055 // initialized must be marked invalid.
Ian Rogers7b078e82014-09-10 14:44:24 -07002056 work_line_->MarkUninitRefsAsInvalid(this, uninit_type);
Ian Rogers08f753d2012-08-24 14:35:25 -07002057 // add the new uninitialized reference to the register state
Ian Rogers7b078e82014-09-10 14:44:24 -07002058 work_line_->SetRegisterType(this, inst->VRegA_21c(), uninit_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07002059 break;
2060 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08002061 case Instruction::NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002062 VerifyNewArray(inst, false, false);
jeffhaobdb76512011-09-07 11:43:16 -07002063 break;
2064 case Instruction::FILLED_NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002065 VerifyNewArray(inst, true, false);
Ian Rogers0c4a5062012-02-03 15:18:59 -08002066 just_set_result = true; // Filled new array sets result register
jeffhaobdb76512011-09-07 11:43:16 -07002067 break;
Ian Rogers0c4a5062012-02-03 15:18:59 -08002068 case Instruction::FILLED_NEW_ARRAY_RANGE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002069 VerifyNewArray(inst, true, true);
Ian Rogers0c4a5062012-02-03 15:18:59 -08002070 just_set_result = true; // Filled new array range sets result register
2071 break;
jeffhaobdb76512011-09-07 11:43:16 -07002072 case Instruction::CMPL_FLOAT:
2073 case Instruction::CMPG_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002074 if (!work_line_->VerifyRegisterType(this, inst->VRegB_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08002075 break;
2076 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002077 if (!work_line_->VerifyRegisterType(this, inst->VRegC_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08002078 break;
2079 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002080 work_line_->SetRegisterType(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002081 break;
2082 case Instruction::CMPL_DOUBLE:
2083 case Instruction::CMPG_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002084 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegB_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002085 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002086 break;
2087 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002088 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegC_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002089 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002090 break;
2091 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002092 work_line_->SetRegisterType(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002093 break;
2094 case Instruction::CMP_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002095 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegB_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002096 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002097 break;
2098 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002099 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegC_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002100 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002101 break;
2102 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002103 work_line_->SetRegisterType(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002104 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002105 case Instruction::THROW: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002106 const RegType& res_type = work_line_->GetRegisterType(this, inst->VRegA_11x());
Ian Rogersb4903572012-10-11 11:52:56 -07002107 if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(res_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07002108 Fail(res_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS : VERIFY_ERROR_BAD_CLASS_SOFT)
2109 << "thrown class " << res_type << " not instanceof Throwable";
jeffhaobdb76512011-09-07 11:43:16 -07002110 }
2111 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002112 }
jeffhaobdb76512011-09-07 11:43:16 -07002113 case Instruction::GOTO:
2114 case Instruction::GOTO_16:
2115 case Instruction::GOTO_32:
2116 /* no effect on or use of registers */
2117 break;
2118
2119 case Instruction::PACKED_SWITCH:
2120 case Instruction::SPARSE_SWITCH:
2121 /* verify that vAA is an integer, or can be converted to one */
Ian Rogers7b078e82014-09-10 14:44:24 -07002122 work_line_->VerifyRegisterType(this, inst->VRegA_31t(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002123 break;
2124
Ian Rogersd81871c2011-10-03 13:57:23 -07002125 case Instruction::FILL_ARRAY_DATA: {
2126 /* Similar to the verification done for APUT */
Ian Rogers7b078e82014-09-10 14:44:24 -07002127 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegA_31t());
Ian Rogers89310de2012-02-01 13:47:30 -08002128 /* array_type can be null if the reg type is Zero */
2129 if (!array_type.IsZero()) {
jeffhao457cc512012-02-02 16:55:13 -08002130 if (!array_type.IsArrayTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002131 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with array type "
2132 << array_type;
Ian Rogers89310de2012-02-01 13:47:30 -08002133 } else {
Ian Rogersd8f69b02014-09-10 21:43:52 +00002134 const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
Ian Rogersad0b3a32012-04-16 14:50:24 -07002135 DCHECK(!component_type.IsConflict());
jeffhao457cc512012-02-02 16:55:13 -08002136 if (component_type.IsNonZeroReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002137 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with component type "
2138 << component_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002139 } else {
jeffhao457cc512012-02-02 16:55:13 -08002140 // Now verify if the element width in the table matches the element width declared in
2141 // the array
2142 const uint16_t* array_data = insns + (insns[1] | (((int32_t) insns[2]) << 16));
2143 if (array_data[0] != Instruction::kArrayDataSignature) {
jeffhaod5347e02012-03-22 17:25:05 -07002144 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid magic for array-data";
jeffhao457cc512012-02-02 16:55:13 -08002145 } else {
2146 size_t elem_width = Primitive::ComponentSize(component_type.GetPrimitiveType());
2147 // Since we don't compress the data in Dex, expect to see equal width of data stored
2148 // in the table and expected from the array class.
2149 if (array_data[1] != elem_width) {
jeffhaod5347e02012-03-22 17:25:05 -07002150 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-data size mismatch (" << array_data[1]
2151 << " vs " << elem_width << ")";
jeffhao457cc512012-02-02 16:55:13 -08002152 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002153 }
2154 }
jeffhaobdb76512011-09-07 11:43:16 -07002155 }
2156 }
2157 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002158 }
jeffhaobdb76512011-09-07 11:43:16 -07002159 case Instruction::IF_EQ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002160 case Instruction::IF_NE: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002161 const RegType& reg_type1 = work_line_->GetRegisterType(this, inst->VRegA_22t());
2162 const RegType& reg_type2 = work_line_->GetRegisterType(this, inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002163 bool mismatch = false;
2164 if (reg_type1.IsZero()) { // zero then integral or reference expected
2165 mismatch = !reg_type2.IsReferenceTypes() && !reg_type2.IsIntegralTypes();
2166 } else if (reg_type1.IsReferenceTypes()) { // both references?
2167 mismatch = !reg_type2.IsReferenceTypes();
2168 } else { // both integral?
2169 mismatch = !reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes();
2170 }
2171 if (mismatch) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002172 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to if-eq/if-ne (" << reg_type1 << ","
2173 << reg_type2 << ") must both be references or integral";
jeffhaobdb76512011-09-07 11:43:16 -07002174 }
2175 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002176 }
jeffhaobdb76512011-09-07 11:43:16 -07002177 case Instruction::IF_LT:
2178 case Instruction::IF_GE:
2179 case Instruction::IF_GT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002180 case Instruction::IF_LE: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002181 const RegType& reg_type1 = work_line_->GetRegisterType(this, inst->VRegA_22t());
2182 const RegType& reg_type2 = work_line_->GetRegisterType(this, inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002183 if (!reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002184 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to 'if' (" << reg_type1 << ","
2185 << reg_type2 << ") must be integral";
jeffhaobdb76512011-09-07 11:43:16 -07002186 }
2187 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002188 }
jeffhaobdb76512011-09-07 11:43:16 -07002189 case Instruction::IF_EQZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002190 case Instruction::IF_NEZ: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002191 const RegType& reg_type = work_line_->GetRegisterType(this, inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002192 if (!reg_type.IsReferenceTypes() && !reg_type.IsIntegralTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002193 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
2194 << " unexpected as arg to if-eqz/if-nez";
Ian Rogersd81871c2011-10-03 13:57:23 -07002195 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002196
2197 // Find previous instruction - its existence is a precondition to peephole optimization.
Ian Rogers9b360392013-06-06 14:45:07 -07002198 uint32_t instance_of_idx = 0;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002199 if (0 != work_insn_idx_) {
Ian Rogers9b360392013-06-06 14:45:07 -07002200 instance_of_idx = work_insn_idx_ - 1;
Brian Carlstromdf629502013-07-17 22:39:56 -07002201 while (0 != instance_of_idx && !insn_flags_[instance_of_idx].IsOpcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07002202 instance_of_idx--;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002203 }
Andreas Gampe7c038102014-10-27 20:08:46 -07002204 if (FailOrAbort(this, insn_flags_[instance_of_idx].IsOpcode(),
2205 "Unable to get previous instruction of if-eqz/if-nez for work index ",
2206 work_insn_idx_)) {
2207 break;
2208 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002209 } else {
2210 break;
2211 }
2212
Ian Rogers9b360392013-06-06 14:45:07 -07002213 const Instruction* instance_of_inst = Instruction::At(code_item_->insns_ + instance_of_idx);
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002214
2215 /* Check for peep-hole pattern of:
2216 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002217 * instance-of vX, vY, T;
2218 * ifXXX vX, label ;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002219 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002220 * label:
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002221 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002222 * and sharpen the type of vY to be type T.
2223 * Note, this pattern can't be if:
2224 * - if there are other branches to this branch,
2225 * - when vX == vY.
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002226 */
Ian Rogersfae370a2013-06-05 08:33:27 -07002227 if (!CurrentInsnFlags()->IsBranchTarget() &&
Ian Rogers9b360392013-06-06 14:45:07 -07002228 (Instruction::INSTANCE_OF == instance_of_inst->Opcode()) &&
2229 (inst->VRegA_21t() == instance_of_inst->VRegA_22c()) &&
2230 (instance_of_inst->VRegA_22c() != instance_of_inst->VRegB_22c())) {
Ian Rogersebbdd872014-07-07 23:53:08 -07002231 // Check the type of the instance-of is different than that of registers type, as if they
2232 // are the same there is no work to be done here. Check that the conversion is not to or
2233 // from an unresolved type as type information is imprecise. If the instance-of is to an
2234 // interface then ignore the type information as interfaces can only be treated as Objects
2235 // and we don't want to disallow field and other operations on the object. If the value
2236 // being instance-of checked against is known null (zero) then allow the optimization as
2237 // we didn't have type information. If the merge of the instance-of type with the original
2238 // type is assignable to the original then allow optimization. This check is performed to
2239 // ensure that subsequent merges don't lose type information - such as becoming an
2240 // interface from a class that would lose information relevant to field checks.
Ian Rogers7b078e82014-09-10 14:44:24 -07002241 const RegType& orig_type = work_line_->GetRegisterType(this, instance_of_inst->VRegB_22c());
Ian Rogersd8f69b02014-09-10 21:43:52 +00002242 const RegType& cast_type = ResolveClassAndCheckAccess(instance_of_inst->VRegC_22c());
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002243
Ian Rogersebbdd872014-07-07 23:53:08 -07002244 if (!orig_type.Equals(cast_type) &&
2245 !cast_type.IsUnresolvedTypes() && !orig_type.IsUnresolvedTypes() &&
Andreas Gampe00633eb2014-07-17 16:13:35 -07002246 cast_type.HasClass() && // Could be conflict type, make sure it has a class.
Ian Rogersebbdd872014-07-07 23:53:08 -07002247 !cast_type.GetClass()->IsInterface() &&
2248 (orig_type.IsZero() ||
2249 orig_type.IsStrictlyAssignableFrom(cast_type.Merge(orig_type, &reg_types_)))) {
Ian Rogersd0fbd852013-09-24 18:17:04 -07002250 RegisterLine* update_line = RegisterLine::Create(code_item_->registers_size_, this);
Ian Rogersfae370a2013-06-05 08:33:27 -07002251 if (inst->Opcode() == Instruction::IF_EQZ) {
Ian Rogers9b360392013-06-06 14:45:07 -07002252 fallthrough_line.reset(update_line);
Ian Rogersfae370a2013-06-05 08:33:27 -07002253 } else {
Ian Rogers9b360392013-06-06 14:45:07 -07002254 branch_line.reset(update_line);
2255 }
2256 update_line->CopyFromLine(work_line_.get());
Ian Rogers7b078e82014-09-10 14:44:24 -07002257 update_line->SetRegisterType(this, instance_of_inst->VRegB_22c(), cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002258 if (!insn_flags_[instance_of_idx].IsBranchTarget() && 0 != instance_of_idx) {
2259 // See if instance-of was preceded by a move-object operation, common due to the small
2260 // register encoding space of instance-of, and propagate type information to the source
2261 // of the move-object.
2262 uint32_t move_idx = instance_of_idx - 1;
Brian Carlstromdf629502013-07-17 22:39:56 -07002263 while (0 != move_idx && !insn_flags_[move_idx].IsOpcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07002264 move_idx--;
2265 }
Andreas Gampe7c038102014-10-27 20:08:46 -07002266 if (FailOrAbort(this, insn_flags_[move_idx].IsOpcode(),
2267 "Unable to get previous instruction of if-eqz/if-nez for work index ",
2268 work_insn_idx_)) {
2269 break;
2270 }
Ian Rogers9b360392013-06-06 14:45:07 -07002271 const Instruction* move_inst = Instruction::At(code_item_->insns_ + move_idx);
2272 switch (move_inst->Opcode()) {
2273 case Instruction::MOVE_OBJECT:
2274 if (move_inst->VRegA_12x() == instance_of_inst->VRegB_22c()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002275 update_line->SetRegisterType(this, move_inst->VRegB_12x(), cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002276 }
2277 break;
2278 case Instruction::MOVE_OBJECT_FROM16:
2279 if (move_inst->VRegA_22x() == instance_of_inst->VRegB_22c()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002280 update_line->SetRegisterType(this, move_inst->VRegB_22x(), cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002281 }
2282 break;
2283 case Instruction::MOVE_OBJECT_16:
2284 if (move_inst->VRegA_32x() == instance_of_inst->VRegB_22c()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002285 update_line->SetRegisterType(this, move_inst->VRegB_32x(), cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002286 }
2287 break;
2288 default:
2289 break;
2290 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002291 }
2292 }
2293 }
2294
jeffhaobdb76512011-09-07 11:43:16 -07002295 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002296 }
jeffhaobdb76512011-09-07 11:43:16 -07002297 case Instruction::IF_LTZ:
2298 case Instruction::IF_GEZ:
2299 case Instruction::IF_GTZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002300 case Instruction::IF_LEZ: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002301 const RegType& reg_type = work_line_->GetRegisterType(this, inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002302 if (!reg_type.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002303 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
2304 << " unexpected as arg to if-ltz/if-gez/if-gtz/if-lez";
Ian Rogersd81871c2011-10-03 13:57:23 -07002305 }
jeffhaobdb76512011-09-07 11:43:16 -07002306 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002307 }
jeffhaobdb76512011-09-07 11:43:16 -07002308 case Instruction::AGET_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002309 VerifyAGet(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002310 break;
jeffhaobdb76512011-09-07 11:43:16 -07002311 case Instruction::AGET_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002312 VerifyAGet(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002313 break;
jeffhaobdb76512011-09-07 11:43:16 -07002314 case Instruction::AGET_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002315 VerifyAGet(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002316 break;
jeffhaobdb76512011-09-07 11:43:16 -07002317 case Instruction::AGET_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002318 VerifyAGet(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002319 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002320 case Instruction::AGET:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002321 VerifyAGet(inst, reg_types_.Integer(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002322 break;
jeffhaobdb76512011-09-07 11:43:16 -07002323 case Instruction::AGET_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002324 VerifyAGet(inst, reg_types_.LongLo(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002325 break;
2326 case Instruction::AGET_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002327 VerifyAGet(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07002328 break;
2329
Ian Rogersd81871c2011-10-03 13:57:23 -07002330 case Instruction::APUT_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002331 VerifyAPut(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002332 break;
2333 case Instruction::APUT_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002334 VerifyAPut(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002335 break;
2336 case Instruction::APUT_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002337 VerifyAPut(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002338 break;
2339 case Instruction::APUT_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002340 VerifyAPut(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002341 break;
2342 case Instruction::APUT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002343 VerifyAPut(inst, reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002344 break;
2345 case Instruction::APUT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002346 VerifyAPut(inst, reg_types_.LongLo(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002347 break;
2348 case Instruction::APUT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002349 VerifyAPut(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07002350 break;
2351
jeffhaobdb76512011-09-07 11:43:16 -07002352 case Instruction::IGET_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002353 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002354 break;
jeffhaobdb76512011-09-07 11:43:16 -07002355 case Instruction::IGET_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002356 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002357 break;
jeffhaobdb76512011-09-07 11:43:16 -07002358 case Instruction::IGET_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002359 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002360 break;
jeffhaobdb76512011-09-07 11:43:16 -07002361 case Instruction::IGET_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002362 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002363 break;
2364 case Instruction::IGET:
Andreas Gampe896df402014-10-20 22:25:29 -07002365 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002366 break;
2367 case Instruction::IGET_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002368 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002369 break;
2370 case Instruction::IGET_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002371 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false,
2372 false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002373 break;
jeffhaobdb76512011-09-07 11:43:16 -07002374
Ian Rogersd81871c2011-10-03 13:57:23 -07002375 case Instruction::IPUT_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002376 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002377 break;
2378 case Instruction::IPUT_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002379 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002380 break;
2381 case Instruction::IPUT_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002382 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002383 break;
2384 case Instruction::IPUT_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002385 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002386 break;
2387 case Instruction::IPUT:
Andreas Gampe896df402014-10-20 22:25:29 -07002388 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002389 break;
2390 case Instruction::IPUT_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002391 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002392 break;
jeffhaobdb76512011-09-07 11:43:16 -07002393 case Instruction::IPUT_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002394 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false,
2395 false);
jeffhaobdb76512011-09-07 11:43:16 -07002396 break;
2397
jeffhaobdb76512011-09-07 11:43:16 -07002398 case Instruction::SGET_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002399 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002400 break;
jeffhaobdb76512011-09-07 11:43:16 -07002401 case Instruction::SGET_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002402 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002403 break;
jeffhaobdb76512011-09-07 11:43:16 -07002404 case Instruction::SGET_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002405 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002406 break;
jeffhaobdb76512011-09-07 11:43:16 -07002407 case Instruction::SGET_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002408 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002409 break;
2410 case Instruction::SGET:
Andreas Gampe896df402014-10-20 22:25:29 -07002411 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002412 break;
2413 case Instruction::SGET_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002414 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002415 break;
2416 case Instruction::SGET_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002417 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false,
2418 true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002419 break;
2420
2421 case Instruction::SPUT_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002422 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002423 break;
2424 case Instruction::SPUT_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002425 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002426 break;
2427 case Instruction::SPUT_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002428 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002429 break;
2430 case Instruction::SPUT_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002431 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002432 break;
2433 case Instruction::SPUT:
Andreas Gampe896df402014-10-20 22:25:29 -07002434 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002435 break;
2436 case Instruction::SPUT_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002437 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002438 break;
2439 case Instruction::SPUT_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002440 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false,
2441 true);
jeffhaobdb76512011-09-07 11:43:16 -07002442 break;
2443
2444 case Instruction::INVOKE_VIRTUAL:
2445 case Instruction::INVOKE_VIRTUAL_RANGE:
2446 case Instruction::INVOKE_SUPER:
Ian Rogersd81871c2011-10-03 13:57:23 -07002447 case Instruction::INVOKE_SUPER_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002448 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE ||
2449 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002450 bool is_super = (inst->Opcode() == Instruction::INVOKE_SUPER ||
2451 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002452 ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_VIRTUAL, is_range, is_super);
Ian Rogersd8f69b02014-09-10 21:43:52 +00002453 const RegType* return_type = nullptr;
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002454 if (called_method != nullptr) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002455 StackHandleScope<1> hs(self_);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002456 mirror::Class* return_type_class = called_method->GetReturnType(can_load_classes_);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002457 if (return_type_class != nullptr) {
Andreas Gampef23f33d2015-06-23 14:18:17 -07002458 return_type = &FromClass(called_method->GetReturnTypeDescriptor(),
2459 return_type_class,
2460 return_type_class->CannotBeAssignedFromOtherTypes());
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002461 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07002462 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
2463 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002464 }
2465 }
2466 if (return_type == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002467 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002468 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2469 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002470 const char* descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Mathieu Chartierbf99f772014-08-23 16:37:27 -07002471 return_type = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
jeffhaobdb76512011-09-07 11:43:16 -07002472 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002473 if (!return_type->IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002474 work_line_->SetResultRegisterType(this, *return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002475 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002476 work_line_->SetResultRegisterTypeWide(*return_type, return_type->HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002477 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002478 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002479 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002480 }
jeffhaobdb76512011-09-07 11:43:16 -07002481 case Instruction::INVOKE_DIRECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002482 case Instruction::INVOKE_DIRECT_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002483 bool is_range = (inst->Opcode() == Instruction::INVOKE_DIRECT_RANGE);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002484 ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_DIRECT, is_range, false);
Ian Rogers46685432012-06-03 22:26:43 -07002485 const char* return_type_descriptor;
2486 bool is_constructor;
Ian Rogersd8f69b02014-09-10 21:43:52 +00002487 const RegType* return_type = nullptr;
Ian Rogers7b078e82014-09-10 14:44:24 -07002488 if (called_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002489 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogers46685432012-06-03 22:26:43 -07002490 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
Ian Rogersdfb325e2013-10-30 01:00:44 -07002491 is_constructor = strcmp("<init>", dex_file_->StringDataByIdx(method_id.name_idx_)) == 0;
Ian Rogers46685432012-06-03 22:26:43 -07002492 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
2493 return_type_descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2494 } else {
2495 is_constructor = called_method->IsConstructor();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002496 return_type_descriptor = called_method->GetReturnTypeDescriptor();
Ian Rogers7b078e82014-09-10 14:44:24 -07002497 StackHandleScope<1> hs(self_);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002498 mirror::Class* return_type_class = called_method->GetReturnType(can_load_classes_);
Ian Rogers1ff3c982014-08-12 02:30:58 -07002499 if (return_type_class != nullptr) {
Andreas Gampef23f33d2015-06-23 14:18:17 -07002500 return_type = &FromClass(return_type_descriptor,
2501 return_type_class,
2502 return_type_class->CannotBeAssignedFromOtherTypes());
Ian Rogers1ff3c982014-08-12 02:30:58 -07002503 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07002504 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
2505 self_->ClearException();
Ian Rogers1ff3c982014-08-12 02:30:58 -07002506 }
Ian Rogers46685432012-06-03 22:26:43 -07002507 }
2508 if (is_constructor) {
jeffhaobdb76512011-09-07 11:43:16 -07002509 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002510 * Some additional checks when calling a constructor. We know from the invocation arg check
2511 * that the "this" argument is an instance of called_method->klass. Now we further restrict
2512 * that to require that called_method->klass is the same as this->klass or this->super,
2513 * allowing the latter only if the "this" argument is the same as the "this" argument to
2514 * this method (which implies that we're in a constructor ourselves).
jeffhaobdb76512011-09-07 11:43:16 -07002515 */
Ian Rogers7b078e82014-09-10 14:44:24 -07002516 const RegType& this_type = work_line_->GetInvocationThis(this, inst, is_range);
jeffhaob57e9522012-04-26 18:08:21 -07002517 if (this_type.IsConflict()) // failure.
2518 break;
jeffhaobdb76512011-09-07 11:43:16 -07002519
jeffhaob57e9522012-04-26 18:08:21 -07002520 /* no null refs allowed (?) */
2521 if (this_type.IsZero()) {
2522 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unable to initialize null ref";
2523 break;
jeffhao2a8a90e2011-09-26 14:25:31 -07002524 }
jeffhaob57e9522012-04-26 18:08:21 -07002525
2526 /* must be in same class or in superclass */
Ian Rogersd8f69b02014-09-10 21:43:52 +00002527 // const RegType& this_super_klass = this_type.GetSuperClass(&reg_types_);
Ian Rogers46685432012-06-03 22:26:43 -07002528 // TODO: re-enable constructor type verification
2529 // if (this_super_klass.IsConflict()) {
jeffhaob57e9522012-04-26 18:08:21 -07002530 // Unknown super class, fail so we re-check at runtime.
Ian Rogers46685432012-06-03 22:26:43 -07002531 // Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "super class unknown for '" << this_type << "'";
2532 // break;
2533 // }
jeffhaob57e9522012-04-26 18:08:21 -07002534
2535 /* arg must be an uninitialized reference */
2536 if (!this_type.IsUninitializedTypes()) {
2537 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Expected initialization on uninitialized reference "
2538 << this_type;
2539 break;
2540 }
2541
2542 /*
2543 * Replace the uninitialized reference with an initialized one. We need to do this for all
2544 * registers that have the same object instance in them, not just the "this" register.
2545 */
Jeff Hao848f70a2014-01-15 13:49:50 -08002546 const uint32_t this_reg = (is_range) ? inst->VRegC_3rc() : inst->VRegC_35c();
2547 work_line_->MarkRefsAsInitialized(this, this_type, this_reg, work_insn_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002548 }
Ian Rogers1ff3c982014-08-12 02:30:58 -07002549 if (return_type == nullptr) {
Mathieu Chartierbf99f772014-08-23 16:37:27 -07002550 return_type = &reg_types_.FromDescriptor(GetClassLoader(), return_type_descriptor,
2551 false);
Ian Rogers1ff3c982014-08-12 02:30:58 -07002552 }
2553 if (!return_type->IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002554 work_line_->SetResultRegisterType(this, *return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002555 } else {
Ian Rogers1ff3c982014-08-12 02:30:58 -07002556 work_line_->SetResultRegisterTypeWide(*return_type, return_type->HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002557 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002558 just_set_result = true;
2559 break;
2560 }
2561 case Instruction::INVOKE_STATIC:
2562 case Instruction::INVOKE_STATIC_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002563 bool is_range = (inst->Opcode() == Instruction::INVOKE_STATIC_RANGE);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002564 ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_STATIC, is_range, false);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002565 const char* descriptor;
Ian Rogers7b078e82014-09-10 14:44:24 -07002566 if (called_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002567 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002568 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2569 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Mathieu Chartier590fee92013-09-13 13:46:47 -07002570 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002571 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002572 descriptor = called_method->GetReturnTypeDescriptor();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002573 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00002574 const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002575 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002576 work_line_->SetResultRegisterType(this, return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002577 } else {
2578 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2579 }
jeffhaobdb76512011-09-07 11:43:16 -07002580 just_set_result = true;
2581 }
2582 break;
jeffhaobdb76512011-09-07 11:43:16 -07002583 case Instruction::INVOKE_INTERFACE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002584 case Instruction::INVOKE_INTERFACE_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002585 bool is_range = (inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002586 ArtMethod* abs_method = VerifyInvocationArgs(inst, METHOD_INTERFACE, is_range, false);
Ian Rogers7b078e82014-09-10 14:44:24 -07002587 if (abs_method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002588 mirror::Class* called_interface = abs_method->GetDeclaringClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002589 if (!called_interface->IsInterface() && !called_interface->IsObjectClass()) {
2590 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected interface class in invoke-interface '"
2591 << PrettyMethod(abs_method) << "'";
2592 break;
Ian Rogers28ad40d2011-10-27 15:19:26 -07002593 }
Ian Rogers0d604842012-04-16 14:50:24 -07002594 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002595 /* Get the type of the "this" arg, which should either be a sub-interface of called
2596 * interface or Object (see comments in RegType::JoinClass).
2597 */
Ian Rogers7b078e82014-09-10 14:44:24 -07002598 const RegType& this_type = work_line_->GetInvocationThis(this, inst, is_range);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002599 if (this_type.IsZero()) {
2600 /* null pointer always passes (and always fails at runtime) */
2601 } else {
2602 if (this_type.IsUninitializedTypes()) {
2603 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface call on uninitialized object "
2604 << this_type;
2605 break;
2606 }
2607 // In the past we have tried to assert that "called_interface" is assignable
2608 // from "this_type.GetClass()", however, as we do an imprecise Join
2609 // (RegType::JoinClass) we don't have full information on what interfaces are
2610 // implemented by "this_type". For example, two classes may implement the same
2611 // interfaces and have a common parent that doesn't implement the interface. The
2612 // join will set "this_type" to the parent class and a test that this implements
2613 // the interface will incorrectly fail.
2614 }
2615 /*
2616 * We don't have an object instance, so we can't find the concrete method. However, all of
2617 * the type information is in the abstract method, so we're good.
2618 */
2619 const char* descriptor;
Ian Rogers7b078e82014-09-10 14:44:24 -07002620 if (abs_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002621 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002622 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2623 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
2624 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2625 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002626 descriptor = abs_method->GetReturnTypeDescriptor();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002627 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00002628 const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002629 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002630 work_line_->SetResultRegisterType(this, return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002631 } else {
2632 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2633 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002634 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002635 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002636 }
jeffhaobdb76512011-09-07 11:43:16 -07002637 case Instruction::NEG_INT:
2638 case Instruction::NOT_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002639 work_line_->CheckUnaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002640 break;
2641 case Instruction::NEG_LONG:
2642 case Instruction::NOT_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002643 work_line_->CheckUnaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002644 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002645 break;
2646 case Instruction::NEG_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002647 work_line_->CheckUnaryOp(this, inst, reg_types_.Float(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002648 break;
2649 case Instruction::NEG_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002650 work_line_->CheckUnaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002651 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002652 break;
2653 case Instruction::INT_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002654 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002655 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002656 break;
2657 case Instruction::INT_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002658 work_line_->CheckUnaryOp(this, inst, reg_types_.Float(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002659 break;
2660 case Instruction::INT_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002661 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002662 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002663 break;
2664 case Instruction::LONG_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002665 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002666 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002667 break;
2668 case Instruction::LONG_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002669 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002670 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002671 break;
2672 case Instruction::LONG_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002673 work_line_->CheckUnaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002674 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002675 break;
2676 case Instruction::FLOAT_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002677 work_line_->CheckUnaryOp(this, inst, reg_types_.Integer(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002678 break;
2679 case Instruction::FLOAT_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002680 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002681 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002682 break;
2683 case Instruction::FLOAT_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002684 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002685 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002686 break;
2687 case Instruction::DOUBLE_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002688 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002689 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002690 break;
2691 case Instruction::DOUBLE_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002692 work_line_->CheckUnaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002693 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002694 break;
2695 case Instruction::DOUBLE_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002696 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002697 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002698 break;
2699 case Instruction::INT_TO_BYTE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002700 work_line_->CheckUnaryOp(this, inst, reg_types_.Byte(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002701 break;
2702 case Instruction::INT_TO_CHAR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002703 work_line_->CheckUnaryOp(this, inst, reg_types_.Char(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002704 break;
2705 case Instruction::INT_TO_SHORT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002706 work_line_->CheckUnaryOp(this, inst, reg_types_.Short(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002707 break;
2708
2709 case Instruction::ADD_INT:
2710 case Instruction::SUB_INT:
2711 case Instruction::MUL_INT:
2712 case Instruction::REM_INT:
2713 case Instruction::DIV_INT:
2714 case Instruction::SHL_INT:
2715 case Instruction::SHR_INT:
2716 case Instruction::USHR_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002717 work_line_->CheckBinaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002718 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002719 break;
2720 case Instruction::AND_INT:
2721 case Instruction::OR_INT:
2722 case Instruction::XOR_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002723 work_line_->CheckBinaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002724 reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002725 break;
2726 case Instruction::ADD_LONG:
2727 case Instruction::SUB_LONG:
2728 case Instruction::MUL_LONG:
2729 case Instruction::DIV_LONG:
2730 case Instruction::REM_LONG:
2731 case Instruction::AND_LONG:
2732 case Instruction::OR_LONG:
2733 case Instruction::XOR_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002734 work_line_->CheckBinaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002735 reg_types_.LongLo(), reg_types_.LongHi(),
2736 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002737 break;
2738 case Instruction::SHL_LONG:
2739 case Instruction::SHR_LONG:
2740 case Instruction::USHR_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002741 /* shift distance is Int, making these different from other binary operations */
Ian Rogers7b078e82014-09-10 14:44:24 -07002742 work_line_->CheckBinaryOpWideShift(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002743 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002744 break;
2745 case Instruction::ADD_FLOAT:
2746 case Instruction::SUB_FLOAT:
2747 case Instruction::MUL_FLOAT:
2748 case Instruction::DIV_FLOAT:
2749 case Instruction::REM_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002750 work_line_->CheckBinaryOp(this, inst, reg_types_.Float(), reg_types_.Float(),
2751 reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002752 break;
2753 case Instruction::ADD_DOUBLE:
2754 case Instruction::SUB_DOUBLE:
2755 case Instruction::MUL_DOUBLE:
2756 case Instruction::DIV_DOUBLE:
2757 case Instruction::REM_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002758 work_line_->CheckBinaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002759 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2760 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002761 break;
2762 case Instruction::ADD_INT_2ADDR:
2763 case Instruction::SUB_INT_2ADDR:
2764 case Instruction::MUL_INT_2ADDR:
2765 case Instruction::REM_INT_2ADDR:
2766 case Instruction::SHL_INT_2ADDR:
2767 case Instruction::SHR_INT_2ADDR:
2768 case Instruction::USHR_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002769 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
2770 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002771 break;
2772 case Instruction::AND_INT_2ADDR:
2773 case Instruction::OR_INT_2ADDR:
2774 case Instruction::XOR_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002775 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
2776 reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002777 break;
2778 case Instruction::DIV_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002779 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
2780 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002781 break;
2782 case Instruction::ADD_LONG_2ADDR:
2783 case Instruction::SUB_LONG_2ADDR:
2784 case Instruction::MUL_LONG_2ADDR:
2785 case Instruction::DIV_LONG_2ADDR:
2786 case Instruction::REM_LONG_2ADDR:
2787 case Instruction::AND_LONG_2ADDR:
2788 case Instruction::OR_LONG_2ADDR:
2789 case Instruction::XOR_LONG_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002790 work_line_->CheckBinaryOp2addrWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002791 reg_types_.LongLo(), reg_types_.LongHi(),
2792 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002793 break;
2794 case Instruction::SHL_LONG_2ADDR:
2795 case Instruction::SHR_LONG_2ADDR:
2796 case Instruction::USHR_LONG_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002797 work_line_->CheckBinaryOp2addrWideShift(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002798 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002799 break;
2800 case Instruction::ADD_FLOAT_2ADDR:
2801 case Instruction::SUB_FLOAT_2ADDR:
2802 case Instruction::MUL_FLOAT_2ADDR:
2803 case Instruction::DIV_FLOAT_2ADDR:
2804 case Instruction::REM_FLOAT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002805 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Float(), reg_types_.Float(),
2806 reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002807 break;
2808 case Instruction::ADD_DOUBLE_2ADDR:
2809 case Instruction::SUB_DOUBLE_2ADDR:
2810 case Instruction::MUL_DOUBLE_2ADDR:
2811 case Instruction::DIV_DOUBLE_2ADDR:
2812 case Instruction::REM_DOUBLE_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002813 work_line_->CheckBinaryOp2addrWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002814 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2815 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002816 break;
2817 case Instruction::ADD_INT_LIT16:
Ian Rogersf72a11d2014-10-30 15:41:08 -07002818 case Instruction::RSUB_INT_LIT16:
jeffhaobdb76512011-09-07 11:43:16 -07002819 case Instruction::MUL_INT_LIT16:
2820 case Instruction::DIV_INT_LIT16:
2821 case Instruction::REM_INT_LIT16:
Ian Rogers7b078e82014-09-10 14:44:24 -07002822 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), false,
2823 true);
jeffhaobdb76512011-09-07 11:43:16 -07002824 break;
2825 case Instruction::AND_INT_LIT16:
2826 case Instruction::OR_INT_LIT16:
2827 case Instruction::XOR_INT_LIT16:
Ian Rogers7b078e82014-09-10 14:44:24 -07002828 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), true,
2829 true);
jeffhaobdb76512011-09-07 11:43:16 -07002830 break;
2831 case Instruction::ADD_INT_LIT8:
2832 case Instruction::RSUB_INT_LIT8:
2833 case Instruction::MUL_INT_LIT8:
2834 case Instruction::DIV_INT_LIT8:
2835 case Instruction::REM_INT_LIT8:
2836 case Instruction::SHL_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002837 case Instruction::SHR_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002838 case Instruction::USHR_INT_LIT8:
Ian Rogers7b078e82014-09-10 14:44:24 -07002839 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), false,
2840 false);
jeffhaobdb76512011-09-07 11:43:16 -07002841 break;
2842 case Instruction::AND_INT_LIT8:
2843 case Instruction::OR_INT_LIT8:
2844 case Instruction::XOR_INT_LIT8:
Ian Rogers7b078e82014-09-10 14:44:24 -07002845 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), true,
2846 false);
jeffhaobdb76512011-09-07 11:43:16 -07002847 break;
2848
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002849 // Special instructions.
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07002850 case Instruction::RETURN_VOID_NO_BARRIER:
2851 if (IsConstructor() && !IsStatic()) {
2852 auto& declaring_class = GetDeclaringClass();
Andreas Gampe68df3202015-06-22 11:35:46 -07002853 if (declaring_class.IsUnresolvedReference()) {
2854 // We must iterate over the fields, even if we cannot use mirror classes to do so. Do it
2855 // manually over the underlying dex file.
2856 uint32_t first_index = GetFirstFinalInstanceFieldIndex(*dex_file_,
2857 dex_file_->GetMethodId(dex_method_idx_).class_idx_);
2858 if (first_index != DexFile::kDexNoIndex) {
2859 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void-no-barrier not expected for field "
2860 << first_index;
2861 }
2862 break;
2863 }
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07002864 auto* klass = declaring_class.GetClass();
2865 for (uint32_t i = 0, num_fields = klass->NumInstanceFields(); i < num_fields; ++i) {
2866 if (klass->GetInstanceField(i)->IsFinal()) {
Mathieu Chartiere86deef2015-03-19 13:43:37 -07002867 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void-no-barrier not expected for "
2868 << PrettyField(klass->GetInstanceField(i));
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07002869 break;
2870 }
2871 }
Sebastien Hertzcc10e0e2013-06-28 14:24:48 +02002872 }
Andreas Gampeb2917962015-07-31 13:36:10 -07002873 // Handle this like a RETURN_VOID now. Code is duplicated to separate standard from
2874 // quickened opcodes (otherwise this could be a fall-through).
2875 if (!IsConstructor()) {
2876 if (!GetMethodReturnType().IsConflict()) {
2877 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void not expected";
2878 }
2879 }
Sebastien Hertzcc10e0e2013-06-28 14:24:48 +02002880 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002881 // Note: the following instructions encode offsets derived from class linking.
2882 // As such they use Class*/Field*/AbstractMethod* as these offsets only have
2883 // meaning if the class linking and resolution were successful.
2884 case Instruction::IGET_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002885 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002886 break;
2887 case Instruction::IGET_WIDE_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002888 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002889 break;
2890 case Instruction::IGET_OBJECT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002891 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002892 break;
Mathieu Chartierffc605c2014-12-10 10:35:44 -08002893 case Instruction::IGET_BOOLEAN_QUICK:
2894 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true);
2895 break;
2896 case Instruction::IGET_BYTE_QUICK:
2897 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true);
2898 break;
2899 case Instruction::IGET_CHAR_QUICK:
2900 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true);
2901 break;
2902 case Instruction::IGET_SHORT_QUICK:
2903 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true);
2904 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002905 case Instruction::IPUT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002906 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002907 break;
Fred Shih37f05ef2014-07-16 18:38:08 -07002908 case Instruction::IPUT_BOOLEAN_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002909 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07002910 break;
2911 case Instruction::IPUT_BYTE_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002912 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07002913 break;
2914 case Instruction::IPUT_CHAR_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002915 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07002916 break;
2917 case Instruction::IPUT_SHORT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002918 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07002919 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002920 case Instruction::IPUT_WIDE_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002921 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002922 break;
2923 case Instruction::IPUT_OBJECT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002924 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002925 break;
2926 case Instruction::INVOKE_VIRTUAL_QUICK:
2927 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: {
2928 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002929 ArtMethod* called_method = VerifyInvokeVirtualQuickArgs(inst, is_range);
Ian Rogers7b078e82014-09-10 14:44:24 -07002930 if (called_method != nullptr) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002931 const char* descriptor = called_method->GetReturnTypeDescriptor();
Ian Rogersd8f69b02014-09-10 21:43:52 +00002932 const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002933 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002934 work_line_->SetResultRegisterType(this, return_type);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002935 } else {
2936 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2937 }
2938 just_set_result = true;
2939 }
2940 break;
2941 }
Igor Murashkin158f35c2015-06-10 15:55:30 -07002942 case Instruction::INVOKE_LAMBDA: {
2943 // Don't bother verifying, instead the interpreter will take the slow path with access checks.
2944 // If the code would've normally hard-failed, then the interpreter will throw the
2945 // appropriate verification errors at runtime.
2946 Fail(VERIFY_ERROR_FORCE_INTERPRETER); // TODO(iam): implement invoke-lambda verification
2947 break;
2948 }
2949 case Instruction::CREATE_LAMBDA: {
2950 // Don't bother verifying, instead the interpreter will take the slow path with access checks.
2951 // If the code would've normally hard-failed, then the interpreter will throw the
2952 // appropriate verification errors at runtime.
2953 Fail(VERIFY_ERROR_FORCE_INTERPRETER); // TODO(iam): implement create-lambda verification
2954 break;
2955 }
2956
Igor Murashkin2ee54e22015-06-18 10:05:11 -07002957 case Instruction::UNUSED_F4:
2958 case Instruction::UNUSED_F5:
2959 case Instruction::UNUSED_F7: {
Igor Murashkin158f35c2015-06-10 15:55:30 -07002960 DCHECK(false); // TODO(iam): Implement opcodes for lambdas
Igor Murashkin2ee54e22015-06-18 10:05:11 -07002961 // Conservatively fail verification on release builds.
2962 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Unexpected opcode " << inst->DumpString(dex_file_);
2963 break;
2964 }
2965
2966 case Instruction::BOX_LAMBDA: {
2967 // Don't bother verifying, instead the interpreter will take the slow path with access checks.
2968 // If the code would've normally hard-failed, then the interpreter will throw the
2969 // appropriate verification errors at runtime.
2970 Fail(VERIFY_ERROR_FORCE_INTERPRETER); // TODO(iam): implement box-lambda verification
Igor Murashkine2facc52015-07-10 13:49:08 -07002971
2972 // Partial verification. Sets the resulting type to always be an object, which
2973 // is good enough for some other verification to occur without hard-failing.
2974 const uint32_t vreg_target_object = inst->VRegA_22x(); // box-lambda vA, vB
2975 const RegType& reg_type = reg_types_.JavaLangObject(need_precise_constants_);
2976 work_line_->SetRegisterType(this, vreg_target_object, reg_type);
Igor Murashkin2ee54e22015-06-18 10:05:11 -07002977 break;
2978 }
2979
2980 case Instruction::UNBOX_LAMBDA: {
2981 // Don't bother verifying, instead the interpreter will take the slow path with access checks.
2982 // If the code would've normally hard-failed, then the interpreter will throw the
2983 // appropriate verification errors at runtime.
2984 Fail(VERIFY_ERROR_FORCE_INTERPRETER); // TODO(iam): implement unbox-lambda verification
2985 break;
Igor Murashkin158f35c2015-06-10 15:55:30 -07002986 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002987
Ian Rogersd81871c2011-10-03 13:57:23 -07002988 /* These should never appear during verification. */
Mathieu Chartierffc605c2014-12-10 10:35:44 -08002989 case Instruction::UNUSED_3E ... Instruction::UNUSED_43:
Igor Murashkin158f35c2015-06-10 15:55:30 -07002990 case Instruction::UNUSED_FA ... Instruction::UNUSED_FF:
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002991 case Instruction::UNUSED_79:
2992 case Instruction::UNUSED_7A:
jeffhaod5347e02012-03-22 17:25:05 -07002993 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Unexpected opcode " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07002994 break;
2995
2996 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002997 * DO NOT add a "default" clause here. Without it the compiler will
jeffhaobdb76512011-09-07 11:43:16 -07002998 * complain if an instruction is missing (which is desirable).
2999 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003000 } // end - switch (dec_insn.opcode)
jeffhaobdb76512011-09-07 11:43:16 -07003001
Ian Rogersad0b3a32012-04-16 14:50:24 -07003002 if (have_pending_hard_failure_) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08003003 if (Runtime::Current()->IsAotCompiler()) {
3004 /* When AOT compiling, check that the last failure is a hard failure */
Andreas Gampeb588f4c2015-05-26 13:35:39 -07003005 if (failures_[failures_.size() - 1] != VERIFY_ERROR_BAD_CLASS_HARD) {
3006 LOG(ERROR) << "Pending failures:";
3007 for (auto& error : failures_) {
3008 LOG(ERROR) << error;
3009 }
3010 for (auto& error_msg : failure_messages_) {
3011 LOG(ERROR) << error_msg->str();
3012 }
3013 LOG(FATAL) << "Pending hard failure, but last failure not hard.";
3014 }
Ian Rogerse1758fe2012-04-19 11:31:15 -07003015 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003016 /* immediate failure, reject class */
3017 info_messages_ << "Rejecting opcode " << inst->DumpString(dex_file_);
3018 return false;
jeffhaofaf459e2012-08-31 15:32:47 -07003019 } else if (have_pending_runtime_throw_failure_) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07003020 /* checking interpreter will throw, mark following code as unreachable */
jeffhaofaf459e2012-08-31 15:32:47 -07003021 opcode_flags = Instruction::kThrow;
Andreas Gamped12e7822015-06-25 10:26:40 -07003022 have_any_pending_runtime_throw_failure_ = true;
3023 // Reset the pending_runtime_throw flag. The flag is a global to decouple Fail and is per
3024 // instruction.
3025 have_pending_runtime_throw_failure_ = false;
jeffhaobdb76512011-09-07 11:43:16 -07003026 }
jeffhaobdb76512011-09-07 11:43:16 -07003027 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003028 * If we didn't just set the result register, clear it out. This ensures that you can only use
3029 * "move-result" immediately after the result is set. (We could check this statically, but it's
3030 * not expensive and it makes our debugging output cleaner.)
jeffhaobdb76512011-09-07 11:43:16 -07003031 */
3032 if (!just_set_result) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003033 work_line_->SetResultTypeToUnknown(this);
jeffhaobdb76512011-09-07 11:43:16 -07003034 }
3035
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003036
jeffhaobdb76512011-09-07 11:43:16 -07003037
3038 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003039 * Handle "branch". Tag the branch target.
jeffhaobdb76512011-09-07 11:43:16 -07003040 *
3041 * NOTE: instructions like Instruction::EQZ provide information about the
jeffhaod1f0fde2011-09-08 17:25:33 -07003042 * state of the register when the branch is taken or not taken. For example,
jeffhaobdb76512011-09-07 11:43:16 -07003043 * somebody could get a reference field, check it for zero, and if the
3044 * branch is taken immediately store that register in a boolean field
jeffhaod1f0fde2011-09-08 17:25:33 -07003045 * since the value is known to be zero. We do not currently account for
jeffhaobdb76512011-09-07 11:43:16 -07003046 * that, and will reject the code.
3047 *
3048 * TODO: avoid re-fetching the branch target
3049 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003050 if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07003051 bool isConditional, selfOkay;
Ian Rogersd81871c2011-10-03 13:57:23 -07003052 if (!GetBranchOffset(work_insn_idx_, &branch_target, &isConditional, &selfOkay)) {
jeffhaobdb76512011-09-07 11:43:16 -07003053 /* should never happen after static verification */
jeffhaod5347e02012-03-22 17:25:05 -07003054 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad branch";
jeffhaobdb76512011-09-07 11:43:16 -07003055 return false;
3056 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08003057 DCHECK_EQ(isConditional, (opcode_flags & Instruction::kContinue) != 0);
Stephen Kyle9bc61992014-09-22 13:53:15 +01003058 if (!CheckNotMoveExceptionOrMoveResult(code_item_->insns_, work_insn_idx_ + branch_target)) {
jeffhaobdb76512011-09-07 11:43:16 -07003059 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07003060 }
jeffhaobdb76512011-09-07 11:43:16 -07003061 /* update branch target, set "changed" if appropriate */
Ian Rogers7b078e82014-09-10 14:44:24 -07003062 if (nullptr != branch_line.get()) {
Ian Rogersebbdd872014-07-07 23:53:08 -07003063 if (!UpdateRegisters(work_insn_idx_ + branch_target, branch_line.get(), false)) {
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003064 return false;
3065 }
3066 } else {
Ian Rogersebbdd872014-07-07 23:53:08 -07003067 if (!UpdateRegisters(work_insn_idx_ + branch_target, work_line_.get(), false)) {
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003068 return false;
3069 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003070 }
jeffhaobdb76512011-09-07 11:43:16 -07003071 }
3072
3073 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003074 * Handle "switch". Tag all possible branch targets.
jeffhaobdb76512011-09-07 11:43:16 -07003075 *
3076 * We've already verified that the table is structurally sound, so we
3077 * just need to walk through and tag the targets.
3078 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003079 if ((opcode_flags & Instruction::kSwitch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07003080 int offset_to_switch = insns[1] | (((int32_t) insns[2]) << 16);
3081 const uint16_t* switch_insns = insns + offset_to_switch;
3082 int switch_count = switch_insns[1];
3083 int offset_to_targets, targ;
3084
3085 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
3086 /* 0 = sig, 1 = count, 2/3 = first key */
3087 offset_to_targets = 4;
3088 } else {
3089 /* 0 = sig, 1 = count, 2..count * 2 = keys */
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07003090 DCHECK((*insns & 0xff) == Instruction::SPARSE_SWITCH);
jeffhaobdb76512011-09-07 11:43:16 -07003091 offset_to_targets = 2 + 2 * switch_count;
3092 }
3093
3094 /* verify each switch target */
3095 for (targ = 0; targ < switch_count; targ++) {
3096 int offset;
3097 uint32_t abs_offset;
3098
3099 /* offsets are 32-bit, and only partly endian-swapped */
3100 offset = switch_insns[offset_to_targets + targ * 2] |
3101 (((int32_t) switch_insns[offset_to_targets + targ * 2 + 1]) << 16);
Ian Rogersd81871c2011-10-03 13:57:23 -07003102 abs_offset = work_insn_idx_ + offset;
3103 DCHECK_LT(abs_offset, code_item_->insns_size_in_code_units_);
Stephen Kyle9bc61992014-09-22 13:53:15 +01003104 if (!CheckNotMoveExceptionOrMoveResult(code_item_->insns_, abs_offset)) {
jeffhaobdb76512011-09-07 11:43:16 -07003105 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07003106 }
Ian Rogersebbdd872014-07-07 23:53:08 -07003107 if (!UpdateRegisters(abs_offset, work_line_.get(), false)) {
jeffhaobdb76512011-09-07 11:43:16 -07003108 return false;
Ian Rogersebbdd872014-07-07 23:53:08 -07003109 }
jeffhaobdb76512011-09-07 11:43:16 -07003110 }
3111 }
3112
3113 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003114 * Handle instructions that can throw and that are sitting in a "try" block. (If they're not in a
3115 * "try" block when they throw, control transfers out of the method.)
jeffhaobdb76512011-09-07 11:43:16 -07003116 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003117 if ((opcode_flags & Instruction::kThrow) != 0 && insn_flags_[work_insn_idx_].IsInTry()) {
Andreas Gampef91baf12014-07-18 15:41:00 -07003118 bool has_catch_all_handler = false;
Ian Rogers0571d352011-11-03 19:51:38 -07003119 CatchHandlerIterator iterator(*code_item_, work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07003120
Andreas Gampef91baf12014-07-18 15:41:00 -07003121 // Need the linker to try and resolve the handled class to check if it's Throwable.
3122 ClassLinker* linker = Runtime::Current()->GetClassLinker();
3123
Ian Rogers0571d352011-11-03 19:51:38 -07003124 for (; iterator.HasNext(); iterator.Next()) {
Andreas Gampef91baf12014-07-18 15:41:00 -07003125 uint16_t handler_type_idx = iterator.GetHandlerTypeIndex();
3126 if (handler_type_idx == DexFile::kDexNoIndex16) {
3127 has_catch_all_handler = true;
3128 } else {
3129 // It is also a catch-all if it is java.lang.Throwable.
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003130 mirror::Class* klass = linker->ResolveType(*dex_file_, handler_type_idx, dex_cache_,
3131 class_loader_);
Andreas Gampef91baf12014-07-18 15:41:00 -07003132 if (klass != nullptr) {
3133 if (klass == mirror::Throwable::GetJavaLangThrowable()) {
3134 has_catch_all_handler = true;
3135 }
3136 } else {
3137 // Clear exception.
Ian Rogers7b078e82014-09-10 14:44:24 -07003138 DCHECK(self_->IsExceptionPending());
3139 self_->ClearException();
Andreas Gampef91baf12014-07-18 15:41:00 -07003140 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003141 }
jeffhaobdb76512011-09-07 11:43:16 -07003142 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003143 * Merge registers into the "catch" block. We want to use the "savedRegs" rather than
3144 * "work_regs", because at runtime the exception will be thrown before the instruction
3145 * modifies any registers.
jeffhaobdb76512011-09-07 11:43:16 -07003146 */
Ian Rogersebbdd872014-07-07 23:53:08 -07003147 if (!UpdateRegisters(iterator.GetHandlerAddress(), saved_line_.get(), false)) {
jeffhaobdb76512011-09-07 11:43:16 -07003148 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07003149 }
jeffhaobdb76512011-09-07 11:43:16 -07003150 }
3151
3152 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003153 * If the monitor stack depth is nonzero, there must be a "catch all" handler for this
3154 * instruction. This does apply to monitor-exit because of async exception handling.
jeffhaobdb76512011-09-07 11:43:16 -07003155 */
Andreas Gampef91baf12014-07-18 15:41:00 -07003156 if (work_line_->MonitorStackDepth() > 0 && !has_catch_all_handler) {
jeffhaobdb76512011-09-07 11:43:16 -07003157 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003158 * The state in work_line reflects the post-execution state. If the current instruction is a
3159 * monitor-enter and the monitor stack was empty, we don't need a catch-all (if it throws,
jeffhaobdb76512011-09-07 11:43:16 -07003160 * it will do so before grabbing the lock).
3161 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02003162 if (inst->Opcode() != Instruction::MONITOR_ENTER || work_line_->MonitorStackDepth() != 1) {
jeffhaod5347e02012-03-22 17:25:05 -07003163 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
Ian Rogersd81871c2011-10-03 13:57:23 -07003164 << "expected to be within a catch-all for an instruction where a monitor is held";
jeffhaobdb76512011-09-07 11:43:16 -07003165 return false;
3166 }
3167 }
3168 }
3169
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003170 /* Handle "continue". Tag the next consecutive instruction.
3171 * Note: Keep the code handling "continue" case below the "branch" and "switch" cases,
3172 * because it changes work_line_ when performing peephole optimization
3173 * and this change should not be used in those cases.
3174 */
Ian Rogers6d376ae2013-07-23 15:12:40 -07003175 if ((opcode_flags & Instruction::kContinue) != 0) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003176 DCHECK_EQ(Instruction::At(code_item_->insns_ + work_insn_idx_), inst);
3177 uint32_t next_insn_idx = work_insn_idx_ + inst->SizeInCodeUnits();
Ian Rogers6d376ae2013-07-23 15:12:40 -07003178 if (next_insn_idx >= code_item_->insns_size_in_code_units_) {
3179 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Execution can walk off end of code area";
3180 return false;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003181 }
Ian Rogers6d376ae2013-07-23 15:12:40 -07003182 // The only way to get to a move-exception instruction is to get thrown there. Make sure the
3183 // next instruction isn't one.
3184 if (!CheckNotMoveException(code_item_->insns_, next_insn_idx)) {
3185 return false;
3186 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003187 if (nullptr != fallthrough_line.get()) {
Ian Rogers6d376ae2013-07-23 15:12:40 -07003188 // Make workline consistent with fallthrough computed from peephole optimization.
3189 work_line_->CopyFromLine(fallthrough_line.get());
3190 }
Ian Rogersb8c78592013-07-25 23:52:52 +00003191 if (insn_flags_[next_insn_idx].IsReturn()) {
3192 // For returns we only care about the operand to the return, all other registers are dead.
3193 const Instruction* ret_inst = Instruction::At(code_item_->insns_ + next_insn_idx);
3194 Instruction::Code opcode = ret_inst->Opcode();
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07003195 if (opcode == Instruction::RETURN_VOID || opcode == Instruction::RETURN_VOID_NO_BARRIER) {
Stephen Kyle7e541c92014-12-17 17:10:02 +00003196 SafelyMarkAllRegistersAsConflicts(this, work_line_.get());
Ian Rogersb8c78592013-07-25 23:52:52 +00003197 } else {
3198 if (opcode == Instruction::RETURN_WIDE) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003199 work_line_->MarkAllRegistersAsConflictsExceptWide(this, ret_inst->VRegA_11x());
Ian Rogersb8c78592013-07-25 23:52:52 +00003200 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07003201 work_line_->MarkAllRegistersAsConflictsExcept(this, ret_inst->VRegA_11x());
Ian Rogersb8c78592013-07-25 23:52:52 +00003202 }
3203 }
3204 }
Ian Rogers6d376ae2013-07-23 15:12:40 -07003205 RegisterLine* next_line = reg_table_.GetLine(next_insn_idx);
Ian Rogers7b078e82014-09-10 14:44:24 -07003206 if (next_line != nullptr) {
Ian Rogersebbdd872014-07-07 23:53:08 -07003207 // Merge registers into what we have for the next instruction, and set the "changed" flag if
3208 // needed. If the merge changes the state of the registers then the work line will be
3209 // updated.
3210 if (!UpdateRegisters(next_insn_idx, work_line_.get(), true)) {
Ian Rogers6d376ae2013-07-23 15:12:40 -07003211 return false;
3212 }
3213 } else {
3214 /*
3215 * We're not recording register data for the next instruction, so we don't know what the
3216 * prior state was. We have to assume that something has changed and re-evaluate it.
3217 */
3218 insn_flags_[next_insn_idx].SetChanged();
3219 }
3220 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003221
jeffhaod1f0fde2011-09-08 17:25:33 -07003222 /* If we're returning from the method, make sure monitor stack is empty. */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003223 if ((opcode_flags & Instruction::kReturn) != 0) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003224 if (!work_line_->VerifyMonitorStackEmpty(this)) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003225 return false;
3226 }
jeffhaobdb76512011-09-07 11:43:16 -07003227 }
3228
3229 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003230 * Update start_guess. Advance to the next instruction of that's
3231 * possible, otherwise use the branch target if one was found. If
jeffhaobdb76512011-09-07 11:43:16 -07003232 * neither of those exists we're in a return or throw; leave start_guess
3233 * alone and let the caller sort it out.
3234 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003235 if ((opcode_flags & Instruction::kContinue) != 0) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003236 DCHECK_EQ(Instruction::At(code_item_->insns_ + work_insn_idx_), inst);
3237 *start_guess = work_insn_idx_ + inst->SizeInCodeUnits();
Elliott Hughesadb8c672012-03-06 16:49:32 -08003238 } else if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07003239 /* we're still okay if branch_target is zero */
Ian Rogersd81871c2011-10-03 13:57:23 -07003240 *start_guess = work_insn_idx_ + branch_target;
jeffhaobdb76512011-09-07 11:43:16 -07003241 }
3242
Ian Rogersd81871c2011-10-03 13:57:23 -07003243 DCHECK_LT(*start_guess, code_item_->insns_size_in_code_units_);
3244 DCHECK(insn_flags_[*start_guess].IsOpcode());
jeffhaobdb76512011-09-07 11:43:16 -07003245
3246 return true;
Brian Carlstrom7934ac22013-07-26 10:54:15 -07003247} // NOLINT(readability/fn_size)
jeffhaobdb76512011-09-07 11:43:16 -07003248
Ian Rogersd8f69b02014-09-10 21:43:52 +00003249const RegType& MethodVerifier::ResolveClassAndCheckAccess(uint32_t class_idx) {
Ian Rogers0571d352011-11-03 19:51:38 -07003250 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
Ian Rogersd8f69b02014-09-10 21:43:52 +00003251 const RegType& referrer = GetDeclaringClass();
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003252 mirror::Class* klass = dex_cache_->GetResolvedType(class_idx);
Ian Rogers7b078e82014-09-10 14:44:24 -07003253 const RegType& result = klass != nullptr ?
Andreas Gampef23f33d2015-06-23 14:18:17 -07003254 FromClass(descriptor, klass, klass->CannotBeAssignedFromOtherTypes()) :
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003255 reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003256 if (result.IsConflict()) {
3257 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "accessing broken descriptor '" << descriptor
3258 << "' in " << referrer;
3259 return result;
3260 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003261 if (klass == nullptr && !result.IsUnresolvedTypes()) {
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003262 dex_cache_->SetResolvedType(class_idx, result.GetClass());
Ian Rogerse1758fe2012-04-19 11:31:15 -07003263 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003264 // Check if access is allowed. Unresolved types use xxxWithAccessCheck to
Jeff Haob24b4a72013-07-31 13:47:31 -07003265 // check at runtime if access is allowed and so pass here. If result is
3266 // primitive, skip the access check.
3267 if (result.IsNonZeroReferenceTypes() && !result.IsUnresolvedTypes() &&
3268 !referrer.IsUnresolvedTypes() && !referrer.CanAccess(result)) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07003269 Fail(VERIFY_ERROR_ACCESS_CLASS) << "illegal class access: '"
Ian Rogersad0b3a32012-04-16 14:50:24 -07003270 << referrer << "' -> '" << result << "'";
Ian Rogers28ad40d2011-10-27 15:19:26 -07003271 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003272 return result;
Ian Rogersd81871c2011-10-03 13:57:23 -07003273}
3274
Ian Rogersd8f69b02014-09-10 21:43:52 +00003275const RegType& MethodVerifier::GetCaughtExceptionType() {
Ian Rogers7b078e82014-09-10 14:44:24 -07003276 const RegType* common_super = nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003277 if (code_item_->tries_size_ != 0) {
Ian Rogers13735952014-10-08 12:43:28 -07003278 const uint8_t* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
Ian Rogersd81871c2011-10-03 13:57:23 -07003279 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
3280 for (uint32_t i = 0; i < handlers_size; i++) {
Ian Rogers0571d352011-11-03 19:51:38 -07003281 CatchHandlerIterator iterator(handlers_ptr);
3282 for (; iterator.HasNext(); iterator.Next()) {
3283 if (iterator.GetHandlerAddress() == (uint32_t) work_insn_idx_) {
3284 if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) {
Ian Rogersb4903572012-10-11 11:52:56 -07003285 common_super = &reg_types_.JavaLangThrowable(false);
Ian Rogersd81871c2011-10-03 13:57:23 -07003286 } else {
Ian Rogersd8f69b02014-09-10 21:43:52 +00003287 const RegType& exception = ResolveClassAndCheckAccess(iterator.GetHandlerTypeIndex());
Jeff Haob878f212014-04-24 16:25:36 -07003288 if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(exception)) {
Jeff Haoc26a56c2013-11-04 12:00:47 -08003289 if (exception.IsUnresolvedTypes()) {
3290 // We don't know enough about the type. Fail here and let runtime handle it.
3291 Fail(VERIFY_ERROR_NO_CLASS) << "unresolved exception class " << exception;
3292 return exception;
3293 } else {
3294 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unexpected non-exception class " << exception;
3295 return reg_types_.Conflict();
3296 }
Jeff Haob878f212014-04-24 16:25:36 -07003297 } else if (common_super == nullptr) {
3298 common_super = &exception;
Ian Rogers28ad40d2011-10-27 15:19:26 -07003299 } else if (common_super->Equals(exception)) {
Ian Rogersc4762272012-02-01 15:55:55 -08003300 // odd case, but nothing to do
Ian Rogersd81871c2011-10-03 13:57:23 -07003301 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07003302 common_super = &common_super->Merge(exception, &reg_types_);
Andreas Gampe7c038102014-10-27 20:08:46 -07003303 if (FailOrAbort(this,
3304 reg_types_.JavaLangThrowable(false).IsAssignableFrom(*common_super),
3305 "java.lang.Throwable is not assignable-from common_super at ",
3306 work_insn_idx_)) {
3307 break;
3308 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003309 }
3310 }
3311 }
3312 }
Ian Rogers0571d352011-11-03 19:51:38 -07003313 handlers_ptr = iterator.EndDataPointer();
Ian Rogersd81871c2011-10-03 13:57:23 -07003314 }
3315 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003316 if (common_super == nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003317 /* no catch blocks, or no catches with classes we can find */
jeffhaod5347e02012-03-22 17:25:05 -07003318 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unable to find exception handler";
Ian Rogersad0b3a32012-04-16 14:50:24 -07003319 return reg_types_.Conflict();
Ian Rogersd81871c2011-10-03 13:57:23 -07003320 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07003321 return *common_super;
Ian Rogersd81871c2011-10-03 13:57:23 -07003322}
3323
Mathieu Chartiere401d142015-04-22 13:56:20 -07003324ArtMethod* MethodVerifier::ResolveMethodAndCheckAccess(
3325 uint32_t dex_method_idx, MethodType method_type) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003326 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx);
Ian Rogersd8f69b02014-09-10 21:43:52 +00003327 const RegType& klass_type = ResolveClassAndCheckAccess(method_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003328 if (klass_type.IsConflict()) {
3329 std::string append(" in attempt to access method ");
3330 append += dex_file_->GetMethodName(method_id);
3331 AppendToLastFailMessage(append);
Ian Rogers7b078e82014-09-10 14:44:24 -07003332 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08003333 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003334 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003335 return nullptr; // Can't resolve Class so no more to do here
Ian Rogers90040192011-12-16 08:54:29 -08003336 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003337 mirror::Class* klass = klass_type.GetClass();
Ian Rogersd8f69b02014-09-10 21:43:52 +00003338 const RegType& referrer = GetDeclaringClass();
Mathieu Chartiere401d142015-04-22 13:56:20 -07003339 auto* cl = Runtime::Current()->GetClassLinker();
3340 auto pointer_size = cl->GetImagePointerSize();
3341 ArtMethod* res_method = dex_cache_->GetResolvedMethod(dex_method_idx, pointer_size);
Ian Rogers7b078e82014-09-10 14:44:24 -07003342 if (res_method == nullptr) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07003343 const char* name = dex_file_->GetMethodName(method_id);
Ian Rogersd91d6d62013-09-25 20:26:14 -07003344 const Signature signature = dex_file_->GetMethodSignature(method_id);
jeffhao8cd6dda2012-02-22 10:15:34 -08003345
3346 if (method_type == METHOD_DIRECT || method_type == METHOD_STATIC) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003347 res_method = klass->FindDirectMethod(name, signature, pointer_size);
jeffhao8cd6dda2012-02-22 10:15:34 -08003348 } else if (method_type == METHOD_INTERFACE) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003349 res_method = klass->FindInterfaceMethod(name, signature, pointer_size);
Ian Rogersd81871c2011-10-03 13:57:23 -07003350 } else {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003351 res_method = klass->FindVirtualMethod(name, signature, pointer_size);
Ian Rogersd81871c2011-10-03 13:57:23 -07003352 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003353 if (res_method != nullptr) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003354 dex_cache_->SetResolvedMethod(dex_method_idx, res_method, pointer_size);
Ian Rogersd81871c2011-10-03 13:57:23 -07003355 } else {
jeffhao8cd6dda2012-02-22 10:15:34 -08003356 // If a virtual or interface method wasn't found with the expected type, look in
3357 // the direct methods. This can happen when the wrong invoke type is used or when
3358 // a class has changed, and will be flagged as an error in later checks.
3359 if (method_type == METHOD_INTERFACE || method_type == METHOD_VIRTUAL) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003360 res_method = klass->FindDirectMethod(name, signature, pointer_size);
jeffhao8cd6dda2012-02-22 10:15:34 -08003361 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003362 if (res_method == nullptr) {
jeffhao8cd6dda2012-02-22 10:15:34 -08003363 Fail(VERIFY_ERROR_NO_METHOD) << "couldn't find method "
3364 << PrettyDescriptor(klass) << "." << name
3365 << " " << signature;
Ian Rogers7b078e82014-09-10 14:44:24 -07003366 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003367 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003368 }
3369 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003370 // Make sure calls to constructors are "direct". There are additional restrictions but we don't
3371 // enforce them here.
3372 if (res_method->IsConstructor() && method_type != METHOD_DIRECT) {
jeffhaod5347e02012-03-22 17:25:05 -07003373 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting non-direct call to constructor "
3374 << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003375 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003376 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003377 // Disallow any calls to class initializers.
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003378 if (res_method->IsClassInitializer()) {
jeffhaod5347e02012-03-22 17:25:05 -07003379 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting call to class initializer "
3380 << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003381 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003382 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003383 // Check if access is allowed.
Ian Rogersad0b3a32012-04-16 14:50:24 -07003384 if (!referrer.CanAccessMember(res_method->GetDeclaringClass(), res_method->GetAccessFlags())) {
jeffhao8cd6dda2012-02-22 10:15:34 -08003385 Fail(VERIFY_ERROR_ACCESS_METHOD) << "illegal method access (call " << PrettyMethod(res_method)
Ian Rogersad0b3a32012-04-16 14:50:24 -07003386 << " from " << referrer << ")";
jeffhaob57e9522012-04-26 18:08:21 -07003387 return res_method;
jeffhao8cd6dda2012-02-22 10:15:34 -08003388 }
jeffhaode0d9c92012-02-27 13:58:13 -08003389 // Check that invoke-virtual and invoke-super are not used on private methods of the same class.
3390 if (res_method->IsPrivate() && method_type == METHOD_VIRTUAL) {
jeffhaod5347e02012-03-22 17:25:05 -07003391 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invoke-super/virtual can't be used on private method "
3392 << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003393 return nullptr;
jeffhaode0d9c92012-02-27 13:58:13 -08003394 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003395 // Check that interface methods match interface classes.
3396 if (klass->IsInterface() && method_type != METHOD_INTERFACE) {
3397 Fail(VERIFY_ERROR_CLASS_CHANGE) << "non-interface method " << PrettyMethod(res_method)
3398 << " is in an interface class " << PrettyClass(klass);
Ian Rogers7b078e82014-09-10 14:44:24 -07003399 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003400 } else if (!klass->IsInterface() && method_type == METHOD_INTERFACE) {
3401 Fail(VERIFY_ERROR_CLASS_CHANGE) << "interface method " << PrettyMethod(res_method)
3402 << " is in a non-interface class " << PrettyClass(klass);
Ian Rogers7b078e82014-09-10 14:44:24 -07003403 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003404 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003405 // See if the method type implied by the invoke instruction matches the access flags for the
3406 // target method.
Brian Carlstrombe6fa5e2014-12-09 20:15:42 -08003407 if ((method_type == METHOD_DIRECT && (!res_method->IsDirect() || res_method->IsStatic())) ||
Ian Rogersd81871c2011-10-03 13:57:23 -07003408 (method_type == METHOD_STATIC && !res_method->IsStatic()) ||
3409 ((method_type == METHOD_VIRTUAL || method_type == METHOD_INTERFACE) && res_method->IsDirect())
3410 ) {
Ian Rogers2fc14272012-08-30 10:56:57 -07003411 Fail(VERIFY_ERROR_CLASS_CHANGE) << "invoke type (" << method_type << ") does not match method "
3412 " type of " << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003413 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003414 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003415 return res_method;
3416}
3417
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003418template <class T>
Mathieu Chartiere401d142015-04-22 13:56:20 -07003419ArtMethod* MethodVerifier::VerifyInvocationArgsFromIterator(
3420 T* it, const Instruction* inst, MethodType method_type, bool is_range, ArtMethod* res_method) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003421 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
3422 // match the call to the signature. Also, we might be calling through an abstract method
3423 // definition (which doesn't have register count values).
3424 const size_t expected_args = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
3425 /* caught by static verifier */
3426 DCHECK(is_range || expected_args <= 5);
3427 if (expected_args > code_item_->outs_size_) {
3428 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
3429 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
3430 return nullptr;
3431 }
3432
3433 uint32_t arg[5];
3434 if (!is_range) {
3435 inst->GetVarArgs(arg);
3436 }
3437 uint32_t sig_registers = 0;
3438
3439 /*
3440 * Check the "this" argument, which must be an instance of the class that declared the method.
3441 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
3442 * rigorous check here (which is okay since we have to do it at runtime).
3443 */
3444 if (method_type != METHOD_STATIC) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003445 const RegType& actual_arg_type = work_line_->GetInvocationThis(this, inst, is_range);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003446 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
3447 CHECK(have_pending_hard_failure_);
3448 return nullptr;
3449 }
3450 if (actual_arg_type.IsUninitializedReference()) {
3451 if (res_method) {
3452 if (!res_method->IsConstructor()) {
3453 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
3454 return nullptr;
3455 }
3456 } else {
3457 // Check whether the name of the called method is "<init>"
3458 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Jeff Hao0d087272014-08-04 14:47:17 -07003459 if (strcmp(dex_file_->GetMethodName(dex_file_->GetMethodId(method_idx)), "<init>") != 0) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003460 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
3461 return nullptr;
3462 }
3463 }
3464 }
3465 if (method_type != METHOD_INTERFACE && !actual_arg_type.IsZero()) {
Ian Rogersd8f69b02014-09-10 21:43:52 +00003466 const RegType* res_method_class;
Andreas Gamped9e23012015-06-04 22:19:58 -07003467 // Miranda methods have the declaring interface as their declaring class, not the abstract
3468 // class. It would be wrong to use this for the type check (interface type checks are
3469 // postponed to runtime).
3470 if (res_method != nullptr && !res_method->IsMiranda()) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003471 mirror::Class* klass = res_method->GetDeclaringClass();
Ian Rogers1ff3c982014-08-12 02:30:58 -07003472 std::string temp;
Andreas Gampef23f33d2015-06-23 14:18:17 -07003473 res_method_class = &FromClass(klass->GetDescriptor(&temp), klass,
3474 klass->CannotBeAssignedFromOtherTypes());
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003475 } else {
3476 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
3477 const uint16_t class_idx = dex_file_->GetMethodId(method_idx).class_idx_;
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003478 res_method_class = &reg_types_.FromDescriptor(GetClassLoader(),
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003479 dex_file_->StringByTypeIdx(class_idx),
3480 false);
3481 }
3482 if (!res_method_class->IsAssignableFrom(actual_arg_type)) {
3483 Fail(actual_arg_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS:
3484 VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type
3485 << "' not instance of '" << *res_method_class << "'";
3486 // Continue on soft failures. We need to find possible hard failures to avoid problems in
3487 // the compiler.
3488 if (have_pending_hard_failure_) {
3489 return nullptr;
3490 }
3491 }
3492 }
3493 sig_registers = 1;
3494 }
3495
3496 for ( ; it->HasNext(); it->Next()) {
3497 if (sig_registers >= expected_args) {
3498 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, expected " << inst->VRegA() <<
3499 " arguments, found " << sig_registers << " or more.";
3500 return nullptr;
3501 }
3502
3503 const char* param_descriptor = it->GetDescriptor();
3504
3505 if (param_descriptor == nullptr) {
3506 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation because of missing signature "
3507 "component";
3508 return nullptr;
3509 }
3510
Ian Rogersd8f69b02014-09-10 21:43:52 +00003511 const RegType& reg_type = reg_types_.FromDescriptor(GetClassLoader(), param_descriptor, false);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003512 uint32_t get_reg = is_range ? inst->VRegC_3rc() + static_cast<uint32_t>(sig_registers) :
3513 arg[sig_registers];
3514 if (reg_type.IsIntegralTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003515 const RegType& src_type = work_line_->GetRegisterType(this, get_reg);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003516 if (!src_type.IsIntegralTypes()) {
3517 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register v" << get_reg << " has type " << src_type
3518 << " but expected " << reg_type;
Andreas Gampeb588f4c2015-05-26 13:35:39 -07003519 return nullptr;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003520 }
Andreas Gampeda9badb2015-06-05 20:22:12 -07003521 } else {
3522 if (!work_line_->VerifyRegisterType(this, get_reg, reg_type)) {
3523 // Continue on soft failures. We need to find possible hard failures to avoid problems in
3524 // the compiler.
3525 if (have_pending_hard_failure_) {
3526 return nullptr;
3527 }
3528 } else if (reg_type.IsLongOrDoubleTypes()) {
3529 // Check that registers are consecutive (for non-range invokes). Invokes are the only
3530 // instructions not specifying register pairs by the first component, but require them
3531 // nonetheless. Only check when there's an actual register in the parameters. If there's
3532 // none, this will fail below.
3533 if (!is_range && sig_registers + 1 < expected_args) {
3534 uint32_t second_reg = arg[sig_registers + 1];
3535 if (second_reg != get_reg + 1) {
3536 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, long or double parameter "
3537 "at index " << sig_registers << " is not a pair: " << get_reg << " + "
3538 << second_reg << ".";
3539 return nullptr;
3540 }
3541 }
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003542 }
3543 }
3544 sig_registers += reg_type.IsLongOrDoubleTypes() ? 2 : 1;
3545 }
3546 if (expected_args != sig_registers) {
3547 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, expected " << expected_args <<
3548 " arguments, found " << sig_registers;
3549 return nullptr;
3550 }
3551 return res_method;
3552}
3553
3554void MethodVerifier::VerifyInvocationArgsUnresolvedMethod(const Instruction* inst,
3555 MethodType method_type,
3556 bool is_range) {
3557 // As the method may not have been resolved, make this static check against what we expect.
3558 // The main reason for this code block is to fail hard when we find an illegal use, e.g.,
3559 // wrong number of arguments or wrong primitive types, even if the method could not be resolved.
3560 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
3561 DexFileParameterIterator it(*dex_file_,
3562 dex_file_->GetProtoId(dex_file_->GetMethodId(method_idx).proto_idx_));
3563 VerifyInvocationArgsFromIterator<DexFileParameterIterator>(&it, inst, method_type, is_range,
3564 nullptr);
3565}
3566
3567class MethodParamListDescriptorIterator {
3568 public:
Mathieu Chartiere401d142015-04-22 13:56:20 -07003569 explicit MethodParamListDescriptorIterator(ArtMethod* res_method) :
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003570 res_method_(res_method), pos_(0), params_(res_method->GetParameterTypeList()),
3571 params_size_(params_ == nullptr ? 0 : params_->Size()) {
3572 }
3573
3574 bool HasNext() {
3575 return pos_ < params_size_;
3576 }
3577
3578 void Next() {
3579 ++pos_;
3580 }
3581
Mathieu Chartier90443472015-07-16 20:32:27 -07003582 const char* GetDescriptor() SHARED_REQUIRES(Locks::mutator_lock_) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003583 return res_method_->GetTypeDescriptorFromTypeIdx(params_->GetTypeItem(pos_).type_idx_);
3584 }
3585
3586 private:
Mathieu Chartiere401d142015-04-22 13:56:20 -07003587 ArtMethod* res_method_;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003588 size_t pos_;
3589 const DexFile::TypeList* params_;
3590 const size_t params_size_;
3591};
3592
Mathieu Chartiere401d142015-04-22 13:56:20 -07003593ArtMethod* MethodVerifier::VerifyInvocationArgs(
3594 const Instruction* inst, MethodType method_type, bool is_range, bool is_super) {
jeffhao8cd6dda2012-02-22 10:15:34 -08003595 // Resolve the method. This could be an abstract or concrete method depending on what sort of call
3596 // we're making.
Sebastien Hertz5243e912013-05-21 10:55:07 +02003597 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Andreas Gampeacc4d2f2014-06-12 19:35:05 -07003598
Mathieu Chartiere401d142015-04-22 13:56:20 -07003599 ArtMethod* res_method = ResolveMethodAndCheckAccess(method_idx, method_type);
Ian Rogers7b078e82014-09-10 14:44:24 -07003600 if (res_method == nullptr) { // error or class is unresolved
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003601 // Check what we can statically.
3602 if (!have_pending_hard_failure_) {
3603 VerifyInvocationArgsUnresolvedMethod(inst, method_type, is_range);
3604 }
3605 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003606 }
3607
Ian Rogersd81871c2011-10-03 13:57:23 -07003608 // If we're using invoke-super(method), make sure that the executing method's class' superclass
3609 // has a vtable entry for the target method.
3610 if (is_super) {
3611 DCHECK(method_type == METHOD_VIRTUAL);
Ian Rogersd8f69b02014-09-10 21:43:52 +00003612 const RegType& super = GetDeclaringClass().GetSuperClass(&reg_types_);
Ian Rogers529781d2012-07-23 17:24:29 -07003613 if (super.IsUnresolvedTypes()) {
jeffhao4d8df822012-04-24 17:09:36 -07003614 Fail(VERIFY_ERROR_NO_METHOD) << "unknown super class in invoke-super from "
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003615 << PrettyMethod(dex_method_idx_, *dex_file_)
jeffhao4d8df822012-04-24 17:09:36 -07003616 << " to super " << PrettyMethod(res_method);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003617 return nullptr;
jeffhao4d8df822012-04-24 17:09:36 -07003618 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003619 mirror::Class* super_klass = super.GetClass();
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003620 if (res_method->GetMethodIndex() >= super_klass->GetVTableLength()) {
jeffhao4d8df822012-04-24 17:09:36 -07003621 Fail(VERIFY_ERROR_NO_METHOD) << "invalid invoke-super from "
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003622 << PrettyMethod(dex_method_idx_, *dex_file_)
jeffhao4d8df822012-04-24 17:09:36 -07003623 << " to super " << super
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003624 << "." << res_method->GetName()
3625 << res_method->GetSignature();
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003626 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003627 }
3628 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003629
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003630 // Process the target method's signature. This signature may or may not
3631 MethodParamListDescriptorIterator it(res_method);
3632 return VerifyInvocationArgsFromIterator<MethodParamListDescriptorIterator>(&it, inst, method_type,
3633 is_range, res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07003634}
3635
Mathieu Chartiere401d142015-04-22 13:56:20 -07003636ArtMethod* MethodVerifier::GetQuickInvokedMethod(const Instruction* inst, RegisterLine* reg_line,
3637 bool is_range, bool allow_failure) {
Mathieu Chartier091d2382015-03-06 10:59:06 -08003638 if (is_range) {
3639 DCHECK_EQ(inst->Opcode(), Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
3640 } else {
3641 DCHECK_EQ(inst->Opcode(), Instruction::INVOKE_VIRTUAL_QUICK);
3642 }
3643 const RegType& actual_arg_type = reg_line->GetInvocationThis(this, inst, is_range, allow_failure);
Ian Rogers9bc54402014-04-17 16:40:01 -07003644 if (!actual_arg_type.HasClass()) {
3645 VLOG(verifier) << "Failed to get mirror::Class* from '" << actual_arg_type << "'";
Andreas Gampe63981562014-04-17 12:28:43 -07003646 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003647 }
Ian Rogersa4cf1df2014-05-07 19:47:17 -07003648 mirror::Class* klass = actual_arg_type.GetClass();
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003649 mirror::Class* dispatch_class;
Ian Rogersa4cf1df2014-05-07 19:47:17 -07003650 if (klass->IsInterface()) {
3651 // Derive Object.class from Class.class.getSuperclass().
3652 mirror::Class* object_klass = klass->GetClass()->GetSuperClass();
Andreas Gampe7c038102014-10-27 20:08:46 -07003653 if (FailOrAbort(this, object_klass->IsObjectClass(),
Mathieu Chartier091d2382015-03-06 10:59:06 -08003654 "Failed to find Object class in quickened invoke receiver", work_insn_idx_)) {
Andreas Gampe7c038102014-10-27 20:08:46 -07003655 return nullptr;
3656 }
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003657 dispatch_class = object_klass;
Ian Rogersa4cf1df2014-05-07 19:47:17 -07003658 } else {
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003659 dispatch_class = klass;
Ian Rogersa4cf1df2014-05-07 19:47:17 -07003660 }
Mathieu Chartier091d2382015-03-06 10:59:06 -08003661 if (!dispatch_class->HasVTable()) {
3662 FailOrAbort(this, allow_failure, "Receiver class has no vtable for quickened invoke at ",
3663 work_insn_idx_);
Andreas Gampe7c038102014-10-27 20:08:46 -07003664 return nullptr;
3665 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003666 uint16_t vtable_index = is_range ? inst->VRegB_3rc() : inst->VRegB_35c();
Mathieu Chartiere401d142015-04-22 13:56:20 -07003667 auto* cl = Runtime::Current()->GetClassLinker();
3668 auto pointer_size = cl->GetImagePointerSize();
Mathieu Chartier091d2382015-03-06 10:59:06 -08003669 if (static_cast<int32_t>(vtable_index) >= dispatch_class->GetVTableLength()) {
3670 FailOrAbort(this, allow_failure,
3671 "Receiver class has not enough vtable slots for quickened invoke at ",
3672 work_insn_idx_);
Andreas Gampe7c038102014-10-27 20:08:46 -07003673 return nullptr;
3674 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07003675 ArtMethod* res_method = dispatch_class->GetVTableEntry(vtable_index, pointer_size);
Mathieu Chartier091d2382015-03-06 10:59:06 -08003676 if (self_->IsExceptionPending()) {
3677 FailOrAbort(this, allow_failure, "Unexpected exception pending for quickened invoke at ",
3678 work_insn_idx_);
Andreas Gampe7c038102014-10-27 20:08:46 -07003679 return nullptr;
3680 }
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003681 return res_method;
3682}
3683
Mathieu Chartiere401d142015-04-22 13:56:20 -07003684ArtMethod* MethodVerifier::VerifyInvokeVirtualQuickArgs(const Instruction* inst, bool is_range) {
Andreas Gampe76bd8802014-12-10 16:43:58 -08003685 DCHECK(Runtime::Current()->IsStarted() || verify_to_dump_)
3686 << PrettyMethod(dex_method_idx_, *dex_file_, true) << "@" << work_insn_idx_;
3687
Mathieu Chartiere401d142015-04-22 13:56:20 -07003688 ArtMethod* res_method = GetQuickInvokedMethod(inst, work_line_.get(), is_range, false);
Ian Rogers7b078e82014-09-10 14:44:24 -07003689 if (res_method == nullptr) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003690 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer method from " << inst->Name();
Ian Rogers7b078e82014-09-10 14:44:24 -07003691 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003692 }
Andreas Gampe7c038102014-10-27 20:08:46 -07003693 if (FailOrAbort(this, !res_method->IsDirect(), "Quick-invoked method is direct at ",
3694 work_insn_idx_)) {
3695 return nullptr;
3696 }
3697 if (FailOrAbort(this, !res_method->IsStatic(), "Quick-invoked method is static at ",
3698 work_insn_idx_)) {
3699 return nullptr;
3700 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003701
3702 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
3703 // match the call to the signature. Also, we might be calling through an abstract method
3704 // definition (which doesn't have register count values).
Ian Rogers7b078e82014-09-10 14:44:24 -07003705 const RegType& actual_arg_type = work_line_->GetInvocationThis(this, inst, is_range);
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003706 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
Ian Rogers7b078e82014-09-10 14:44:24 -07003707 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003708 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003709 const size_t expected_args = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
3710 /* caught by static verifier */
3711 DCHECK(is_range || expected_args <= 5);
3712 if (expected_args > code_item_->outs_size_) {
3713 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
3714 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
Ian Rogers7b078e82014-09-10 14:44:24 -07003715 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003716 }
3717
3718 /*
3719 * Check the "this" argument, which must be an instance of the class that declared the method.
3720 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
3721 * rigorous check here (which is okay since we have to do it at runtime).
3722 */
3723 if (actual_arg_type.IsUninitializedReference() && !res_method->IsConstructor()) {
3724 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
Ian Rogers7b078e82014-09-10 14:44:24 -07003725 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003726 }
3727 if (!actual_arg_type.IsZero()) {
3728 mirror::Class* klass = res_method->GetDeclaringClass();
Ian Rogers1ff3c982014-08-12 02:30:58 -07003729 std::string temp;
Ian Rogersd8f69b02014-09-10 21:43:52 +00003730 const RegType& res_method_class =
Andreas Gampef23f33d2015-06-23 14:18:17 -07003731 FromClass(klass->GetDescriptor(&temp), klass, klass->CannotBeAssignedFromOtherTypes());
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003732 if (!res_method_class.IsAssignableFrom(actual_arg_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07003733 Fail(actual_arg_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS :
3734 VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003735 << "' not instance of '" << res_method_class << "'";
Ian Rogers7b078e82014-09-10 14:44:24 -07003736 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003737 }
3738 }
3739 /*
3740 * Process the target method's signature. This signature may or may not
3741 * have been verified, so we can't assume it's properly formed.
3742 */
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003743 const DexFile::TypeList* params = res_method->GetParameterTypeList();
Ian Rogers7b078e82014-09-10 14:44:24 -07003744 size_t params_size = params == nullptr ? 0 : params->Size();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003745 uint32_t arg[5];
3746 if (!is_range) {
Ian Rogers29a26482014-05-02 15:27:29 -07003747 inst->GetVarArgs(arg);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003748 }
3749 size_t actual_args = 1;
3750 for (size_t param_index = 0; param_index < params_size; param_index++) {
3751 if (actual_args >= expected_args) {
3752 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invalid call to '" << PrettyMethod(res_method)
Brian Carlstrom93c33962013-07-26 10:37:43 -07003753 << "'. Expected " << expected_args
3754 << " arguments, processing argument " << actual_args
3755 << " (where longs/doubles count twice).";
Ian Rogers7b078e82014-09-10 14:44:24 -07003756 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003757 }
3758 const char* descriptor =
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003759 res_method->GetTypeDescriptorFromTypeIdx(params->GetTypeItem(param_index).type_idx_);
Ian Rogers7b078e82014-09-10 14:44:24 -07003760 if (descriptor == nullptr) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003761 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003762 << " missing signature component";
Ian Rogers7b078e82014-09-10 14:44:24 -07003763 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003764 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00003765 const RegType& reg_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003766 uint32_t get_reg = is_range ? inst->VRegC_3rc() + actual_args : arg[actual_args];
Ian Rogers7b078e82014-09-10 14:44:24 -07003767 if (!work_line_->VerifyRegisterType(this, get_reg, reg_type)) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003768 return res_method;
3769 }
3770 actual_args = reg_type.IsLongOrDoubleTypes() ? actual_args + 2 : actual_args + 1;
3771 }
3772 if (actual_args != expected_args) {
3773 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
3774 << " expected " << expected_args << " arguments, found " << actual_args;
Ian Rogers7b078e82014-09-10 14:44:24 -07003775 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003776 } else {
3777 return res_method;
3778 }
3779}
3780
Ian Rogers62342ec2013-06-11 10:26:37 -07003781void MethodVerifier::VerifyNewArray(const Instruction* inst, bool is_filled, bool is_range) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003782 uint32_t type_idx;
3783 if (!is_filled) {
3784 DCHECK_EQ(inst->Opcode(), Instruction::NEW_ARRAY);
3785 type_idx = inst->VRegC_22c();
3786 } else if (!is_range) {
3787 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY);
3788 type_idx = inst->VRegB_35c();
3789 } else {
3790 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY_RANGE);
3791 type_idx = inst->VRegB_3rc();
3792 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00003793 const RegType& res_type = ResolveClassAndCheckAccess(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003794 if (res_type.IsConflict()) { // bad class
3795 DCHECK_NE(failures_.size(), 0U);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003796 } else {
3797 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
3798 if (!res_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003799 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "new-array on non-array class " << res_type;
Ian Rogers0c4a5062012-02-03 15:18:59 -08003800 } else if (!is_filled) {
3801 /* make sure "size" register is valid type */
Ian Rogers7b078e82014-09-10 14:44:24 -07003802 work_line_->VerifyRegisterType(this, inst->VRegB_22c(), reg_types_.Integer());
Ian Rogers0c4a5062012-02-03 15:18:59 -08003803 /* set register type to array class */
Ian Rogersd8f69b02014-09-10 21:43:52 +00003804 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
Ian Rogers7b078e82014-09-10 14:44:24 -07003805 work_line_->SetRegisterType(this, inst->VRegA_22c(), precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003806 } else {
3807 // Verify each register. If "arg_count" is bad, VerifyRegisterType() will run off the end of
3808 // the list and fail. It's legal, if silly, for arg_count to be zero.
Ian Rogersd8f69b02014-09-10 21:43:52 +00003809 const RegType& expected_type = reg_types_.GetComponentType(res_type, GetClassLoader());
Sebastien Hertz5243e912013-05-21 10:55:07 +02003810 uint32_t arg_count = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
3811 uint32_t arg[5];
3812 if (!is_range) {
Ian Rogers29a26482014-05-02 15:27:29 -07003813 inst->GetVarArgs(arg);
Sebastien Hertz5243e912013-05-21 10:55:07 +02003814 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08003815 for (size_t ui = 0; ui < arg_count; ui++) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003816 uint32_t get_reg = is_range ? inst->VRegC_3rc() + ui : arg[ui];
Ian Rogers7b078e82014-09-10 14:44:24 -07003817 if (!work_line_->VerifyRegisterType(this, get_reg, expected_type)) {
3818 work_line_->SetResultRegisterType(this, reg_types_.Conflict());
Ian Rogers0c4a5062012-02-03 15:18:59 -08003819 return;
3820 }
3821 }
3822 // filled-array result goes into "result" register
Ian Rogersd8f69b02014-09-10 21:43:52 +00003823 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
Ian Rogers7b078e82014-09-10 14:44:24 -07003824 work_line_->SetResultRegisterType(this, precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003825 }
3826 }
3827}
3828
Sebastien Hertz5243e912013-05-21 10:55:07 +02003829void MethodVerifier::VerifyAGet(const Instruction* inst,
Ian Rogersd8f69b02014-09-10 21:43:52 +00003830 const RegType& insn_type, bool is_primitive) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003831 const RegType& index_type = work_line_->GetRegisterType(this, inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07003832 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003833 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07003834 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07003835 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegB_23x());
Ian Rogers89310de2012-02-01 13:47:30 -08003836 if (array_type.IsZero()) {
Nicolas Geoffray4824c272015-06-24 15:53:03 +01003837 have_pending_runtime_throw_failure_ = true;
Ian Rogers89310de2012-02-01 13:47:30 -08003838 // Null array class; this code path will fail at runtime. Infer a merge-able type from the
3839 // instruction type. TODO: have a proper notion of bottom here.
3840 if (!is_primitive || insn_type.IsCategory1Types()) {
3841 // Reference or category 1
Ian Rogers7b078e82014-09-10 14:44:24 -07003842 work_line_->SetRegisterType(this, inst->VRegA_23x(), reg_types_.Zero());
Ian Rogersd81871c2011-10-03 13:57:23 -07003843 } else {
Ian Rogers89310de2012-02-01 13:47:30 -08003844 // Category 2
Ian Rogers7b078e82014-09-10 14:44:24 -07003845 work_line_->SetRegisterTypeWide(this, inst->VRegA_23x(),
3846 reg_types_.FromCat2ConstLo(0, false),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003847 reg_types_.FromCat2ConstHi(0, false));
Ian Rogers89310de2012-02-01 13:47:30 -08003848 }
jeffhaofc3144e2012-02-01 17:21:15 -08003849 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003850 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aget";
Ian Rogers89310de2012-02-01 13:47:30 -08003851 } else {
3852 /* verify the class */
Ian Rogersd8f69b02014-09-10 21:43:52 +00003853 const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
jeffhaofc3144e2012-02-01 17:21:15 -08003854 if (!component_type.IsReferenceTypes() && !is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07003855 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08003856 << " source for aget-object";
3857 } else if (component_type.IsNonZeroReferenceTypes() && is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07003858 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08003859 << " source for category 1 aget";
3860 } else if (is_primitive && !insn_type.Equals(component_type) &&
3861 !((insn_type.IsInteger() && component_type.IsFloat()) ||
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003862 (insn_type.IsLong() && component_type.IsDouble()))) {
3863 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array type " << array_type
3864 << " incompatible with aget of type " << insn_type;
Ian Rogers89310de2012-02-01 13:47:30 -08003865 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003866 // Use knowledge of the field type which is stronger than the type inferred from the
3867 // instruction, which can't differentiate object types and ints from floats, longs from
3868 // doubles.
3869 if (!component_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003870 work_line_->SetRegisterType(this, inst->VRegA_23x(), component_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003871 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07003872 work_line_->SetRegisterTypeWide(this, inst->VRegA_23x(), component_type,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003873 component_type.HighHalf(&reg_types_));
3874 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003875 }
3876 }
3877 }
3878}
3879
Ian Rogersd8f69b02014-09-10 21:43:52 +00003880void MethodVerifier::VerifyPrimitivePut(const RegType& target_type, const RegType& insn_type,
Jeff Haofe1f7c82013-08-01 14:50:24 -07003881 const uint32_t vregA) {
3882 // Primitive assignability rules are weaker than regular assignability rules.
3883 bool instruction_compatible;
3884 bool value_compatible;
Ian Rogers7b078e82014-09-10 14:44:24 -07003885 const RegType& value_type = work_line_->GetRegisterType(this, vregA);
Jeff Haofe1f7c82013-08-01 14:50:24 -07003886 if (target_type.IsIntegralTypes()) {
Jeff Haoa4647482013-08-06 15:35:47 -07003887 instruction_compatible = target_type.Equals(insn_type);
Jeff Haofe1f7c82013-08-01 14:50:24 -07003888 value_compatible = value_type.IsIntegralTypes();
3889 } else if (target_type.IsFloat()) {
3890 instruction_compatible = insn_type.IsInteger(); // no put-float, so expect put-int
3891 value_compatible = value_type.IsFloatTypes();
3892 } else if (target_type.IsLong()) {
3893 instruction_compatible = insn_type.IsLong();
Andreas Gampe376fa682014-09-07 13:06:12 -07003894 // Additional register check: this is not checked statically (as part of VerifyInstructions),
3895 // as target_type depends on the resolved type of the field.
3896 if (instruction_compatible && work_line_->NumRegs() > vregA + 1) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003897 const RegType& value_type_hi = work_line_->GetRegisterType(this, vregA + 1);
Andreas Gampe376fa682014-09-07 13:06:12 -07003898 value_compatible = value_type.IsLongTypes() && value_type.CheckWidePair(value_type_hi);
3899 } else {
3900 value_compatible = false;
3901 }
Jeff Haofe1f7c82013-08-01 14:50:24 -07003902 } else if (target_type.IsDouble()) {
3903 instruction_compatible = insn_type.IsLong(); // no put-double, so expect put-long
Andreas Gampe376fa682014-09-07 13:06:12 -07003904 // Additional register check: this is not checked statically (as part of VerifyInstructions),
3905 // as target_type depends on the resolved type of the field.
3906 if (instruction_compatible && work_line_->NumRegs() > vregA + 1) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003907 const RegType& value_type_hi = work_line_->GetRegisterType(this, vregA + 1);
Andreas Gampe376fa682014-09-07 13:06:12 -07003908 value_compatible = value_type.IsDoubleTypes() && value_type.CheckWidePair(value_type_hi);
3909 } else {
3910 value_compatible = false;
3911 }
Jeff Haofe1f7c82013-08-01 14:50:24 -07003912 } else {
3913 instruction_compatible = false; // reference with primitive store
3914 value_compatible = false; // unused
3915 }
3916 if (!instruction_compatible) {
3917 // This is a global failure rather than a class change failure as the instructions and
3918 // the descriptors for the type should have been consistent within the same file at
3919 // compile time.
3920 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "put insn has type '" << insn_type
3921 << "' but expected type '" << target_type << "'";
3922 return;
3923 }
3924 if (!value_compatible) {
3925 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
3926 << " of type " << value_type << " but expected " << target_type << " for put";
3927 return;
3928 }
3929}
3930
Sebastien Hertz5243e912013-05-21 10:55:07 +02003931void MethodVerifier::VerifyAPut(const Instruction* inst,
Ian Rogersd8f69b02014-09-10 21:43:52 +00003932 const RegType& insn_type, bool is_primitive) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003933 const RegType& index_type = work_line_->GetRegisterType(this, inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07003934 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003935 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07003936 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07003937 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegB_23x());
Ian Rogers89310de2012-02-01 13:47:30 -08003938 if (array_type.IsZero()) {
Nicolas Geoffray66389fb2015-06-19 10:35:42 +01003939 // Null array type; this code path will fail at runtime.
3940 // Still check that the given value matches the instruction's type.
Andreas Gampe4bf4c782015-08-14 14:07:43 -07003941 // Note: this is, as usual, complicated by the fact the the instruction isn't fully typed
3942 // and fits multiple register types.
3943 const RegType* modified_reg_type = &insn_type;
3944 if ((modified_reg_type == &reg_types_.Integer()) ||
3945 (modified_reg_type == &reg_types_.LongLo())) {
3946 // May be integer or float | long or double. Overwrite insn_type accordingly.
3947 const RegType& value_type = work_line_->GetRegisterType(this, inst->VRegA_23x());
3948 if (modified_reg_type == &reg_types_.Integer()) {
3949 if (&value_type == &reg_types_.Float()) {
3950 modified_reg_type = &value_type;
3951 }
3952 } else {
3953 if (&value_type == &reg_types_.DoubleLo()) {
3954 modified_reg_type = &value_type;
3955 }
3956 }
3957 }
3958 work_line_->VerifyRegisterType(this, inst->VRegA_23x(), *modified_reg_type);
jeffhaofc3144e2012-02-01 17:21:15 -08003959 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003960 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aput";
Ian Rogers89310de2012-02-01 13:47:30 -08003961 } else {
Ian Rogersd8f69b02014-09-10 21:43:52 +00003962 const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
Jeff Haofe1f7c82013-08-01 14:50:24 -07003963 const uint32_t vregA = inst->VRegA_23x();
Jeff Haob24b4a72013-07-31 13:47:31 -07003964 if (is_primitive) {
Jeff Haofe1f7c82013-08-01 14:50:24 -07003965 VerifyPrimitivePut(component_type, insn_type, vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07003966 } else {
Jeff Haob24b4a72013-07-31 13:47:31 -07003967 if (!component_type.IsReferenceTypes()) {
3968 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
3969 << " source for aput-object";
3970 } else {
3971 // The instruction agrees with the type of array, confirm the value to be stored does too
3972 // Note: we use the instruction type (rather than the component type) for aput-object as
3973 // incompatible classes will be caught at runtime as an array store exception
Ian Rogers7b078e82014-09-10 14:44:24 -07003974 work_line_->VerifyRegisterType(this, vregA, insn_type);
Jeff Haob24b4a72013-07-31 13:47:31 -07003975 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003976 }
3977 }
3978 }
3979}
3980
Mathieu Chartierc7853442015-03-27 14:35:38 -07003981ArtField* MethodVerifier::GetStaticField(int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08003982 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3983 // Check access to class
Ian Rogersd8f69b02014-09-10 21:43:52 +00003984 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Brian Carlstrom7934ac22013-07-26 10:54:15 -07003985 if (klass_type.IsConflict()) { // bad class
Ian Rogersad0b3a32012-04-16 14:50:24 -07003986 AppendToLastFailMessage(StringPrintf(" in attempt to access static field %d (%s) in %s",
3987 field_idx, dex_file_->GetFieldName(field_id),
3988 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers7b078e82014-09-10 14:44:24 -07003989 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08003990 }
Elliott Hughesb25c3f62012-03-26 16:35:06 -07003991 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003992 return nullptr; // Can't resolve Class so no more to do here, will do checking at runtime.
Ian Rogers90040192011-12-16 08:54:29 -08003993 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07003994 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartierc7853442015-03-27 14:35:38 -07003995 ArtField* field = class_linker->ResolveFieldJLS(*dex_file_, field_idx, dex_cache_,
3996 class_loader_);
Ian Rogers7b078e82014-09-10 14:44:24 -07003997 if (field == nullptr) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07003998 VLOG(verifier) << "Unable to resolve static field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07003999 << dex_file_->GetFieldName(field_id) << ") in "
4000 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogers7b078e82014-09-10 14:44:24 -07004001 DCHECK(self_->IsExceptionPending());
4002 self_->ClearException();
4003 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004004 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
4005 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004006 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access static field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07004007 << " from " << GetDeclaringClass();
Ian Rogers7b078e82014-09-10 14:44:24 -07004008 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07004009 } else if (!field->IsStatic()) {
4010 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field) << " to be static";
Ian Rogers7b078e82014-09-10 14:44:24 -07004011 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07004012 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07004013 return field;
Ian Rogersd81871c2011-10-03 13:57:23 -07004014}
4015
Mathieu Chartierc7853442015-03-27 14:35:38 -07004016ArtField* MethodVerifier::GetInstanceField(const RegType& obj_type, int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08004017 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
4018 // Check access to class
Ian Rogersd8f69b02014-09-10 21:43:52 +00004019 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004020 if (klass_type.IsConflict()) {
4021 AppendToLastFailMessage(StringPrintf(" in attempt to access instance field %d (%s) in %s",
4022 field_idx, dex_file_->GetFieldName(field_id),
4023 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers7b078e82014-09-10 14:44:24 -07004024 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08004025 }
jeffhao8cd6dda2012-02-22 10:15:34 -08004026 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004027 return nullptr; // Can't resolve Class so no more to do here
Ian Rogers90040192011-12-16 08:54:29 -08004028 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07004029 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartierc7853442015-03-27 14:35:38 -07004030 ArtField* field = class_linker->ResolveFieldJLS(*dex_file_, field_idx, dex_cache_,
4031 class_loader_);
Ian Rogers7b078e82014-09-10 14:44:24 -07004032 if (field == nullptr) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07004033 VLOG(verifier) << "Unable to resolve instance field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07004034 << dex_file_->GetFieldName(field_id) << ") in "
4035 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogers7b078e82014-09-10 14:44:24 -07004036 DCHECK(self_->IsExceptionPending());
4037 self_->ClearException();
4038 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004039 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
4040 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004041 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access instance field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07004042 << " from " << GetDeclaringClass();
Ian Rogers7b078e82014-09-10 14:44:24 -07004043 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07004044 } else if (field->IsStatic()) {
4045 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field)
4046 << " to not be static";
Ian Rogers7b078e82014-09-10 14:44:24 -07004047 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07004048 } else if (obj_type.IsZero()) {
4049 // Cannot infer and check type, however, access will cause null pointer exception
4050 return field;
Stephen Kyle695c5982014-08-22 15:03:07 +01004051 } else if (!obj_type.IsReferenceTypes()) {
4052 // Trying to read a field from something that isn't a reference
4053 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance field access on object that has "
4054 << "non-reference type " << obj_type;
Ian Rogers7b078e82014-09-10 14:44:24 -07004055 return nullptr;
Ian Rogerse1758fe2012-04-19 11:31:15 -07004056 } else {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08004057 mirror::Class* klass = field->GetDeclaringClass();
Ian Rogersd8f69b02014-09-10 21:43:52 +00004058 const RegType& field_klass =
Andreas Gampef23f33d2015-06-23 14:18:17 -07004059 FromClass(dex_file_->GetFieldDeclaringClassDescriptor(field_id),
4060 klass, klass->CannotBeAssignedFromOtherTypes());
Ian Rogersad0b3a32012-04-16 14:50:24 -07004061 if (obj_type.IsUninitializedTypes() &&
4062 (!IsConstructor() || GetDeclaringClass().Equals(obj_type) ||
4063 !field_klass.Equals(GetDeclaringClass()))) {
4064 // Field accesses through uninitialized references are only allowable for constructors where
4065 // the field is declared in this class
4066 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "cannot access instance field " << PrettyField(field)
Brian Carlstrom93c33962013-07-26 10:37:43 -07004067 << " of a not fully initialized object within the context"
4068 << " of " << PrettyMethod(dex_method_idx_, *dex_file_);
Ian Rogers7b078e82014-09-10 14:44:24 -07004069 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004070 } else if (!field_klass.IsAssignableFrom(obj_type)) {
4071 // Trying to access C1.field1 using reference of type C2, which is neither C1 or a sub-class
4072 // of C1. For resolution to occur the declared class of the field must be compatible with
4073 // obj_type, we've discovered this wasn't so, so report the field didn't exist.
4074 Fail(VERIFY_ERROR_NO_FIELD) << "cannot access instance field " << PrettyField(field)
4075 << " from object of type " << obj_type;
Ian Rogers7b078e82014-09-10 14:44:24 -07004076 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004077 } else {
4078 return field;
4079 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004080 }
4081}
4082
Andreas Gampe896df402014-10-20 22:25:29 -07004083template <MethodVerifier::FieldAccessType kAccType>
4084void MethodVerifier::VerifyISFieldAccess(const Instruction* inst, const RegType& insn_type,
4085 bool is_primitive, bool is_static) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02004086 uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
Mathieu Chartierc7853442015-03-27 14:35:38 -07004087 ArtField* field;
Ian Rogersb94a27b2011-10-26 00:33:41 -07004088 if (is_static) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07004089 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07004090 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004091 const RegType& object_type = work_line_->GetRegisterType(this, inst->VRegB_22c());
Ian Rogersf4028cc2011-11-02 14:56:39 -07004092 field = GetInstanceField(object_type, field_idx);
Andreas Gampe896df402014-10-20 22:25:29 -07004093 if (UNLIKELY(have_pending_hard_failure_)) {
4094 return;
4095 }
Ian Rogersb94a27b2011-10-26 00:33:41 -07004096 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00004097 const RegType* field_type = nullptr;
Ian Rogers7b078e82014-09-10 14:44:24 -07004098 if (field != nullptr) {
Andreas Gampe896df402014-10-20 22:25:29 -07004099 if (kAccType == FieldAccessType::kAccPut) {
4100 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
4101 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
4102 << " from other class " << GetDeclaringClass();
4103 return;
4104 }
4105 }
4106
Mathieu Chartierc7853442015-03-27 14:35:38 -07004107 mirror::Class* field_type_class =
4108 can_load_classes_ ? field->GetType<true>() : field->GetType<false>();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004109 if (field_type_class != nullptr) {
Andreas Gampef23f33d2015-06-23 14:18:17 -07004110 field_type = &FromClass(field->GetTypeDescriptor(), field_type_class,
4111 field_type_class->CannotBeAssignedFromOtherTypes());
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08004112 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004113 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
4114 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004115 }
Ian Rogers0d604842012-04-16 14:50:24 -07004116 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004117 if (field_type == nullptr) {
4118 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
4119 const char* descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
Mathieu Chartierbf99f772014-08-23 16:37:27 -07004120 field_type = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004121 }
Sebastien Hertz757b3042014-03-28 14:34:28 +01004122 DCHECK(field_type != nullptr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02004123 const uint32_t vregA = (is_static) ? inst->VRegA_21c() : inst->VRegA_22c();
Andreas Gampe896df402014-10-20 22:25:29 -07004124 static_assert(kAccType == FieldAccessType::kAccPut || kAccType == FieldAccessType::kAccGet,
4125 "Unexpected third access type");
4126 if (kAccType == FieldAccessType::kAccPut) {
4127 // sput or iput.
4128 if (is_primitive) {
4129 VerifyPrimitivePut(*field_type, insn_type, vregA);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004130 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004131 if (!insn_type.IsAssignableFrom(*field_type)) {
Vladimir Marko414000e2015-06-23 17:45:21 +01004132 // If the field type is not a reference, this is a global failure rather than
4133 // a class change failure as the instructions and the descriptors for the type
4134 // should have been consistent within the same file at compile time.
4135 VerifyError error = field_type->IsReferenceTypes() ? VERIFY_ERROR_BAD_CLASS_SOFT
4136 : VERIFY_ERROR_BAD_CLASS_HARD;
4137 Fail(error) << "expected field " << PrettyField(field)
4138 << " to be compatible with type '" << insn_type
4139 << "' but found type '" << *field_type
4140 << "' in put-object";
Andreas Gampe896df402014-10-20 22:25:29 -07004141 return;
4142 }
4143 work_line_->VerifyRegisterType(this, vregA, *field_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004144 }
Andreas Gampe896df402014-10-20 22:25:29 -07004145 } else if (kAccType == FieldAccessType::kAccGet) {
4146 // sget or iget.
4147 if (is_primitive) {
4148 if (field_type->Equals(insn_type) ||
4149 (field_type->IsFloat() && insn_type.IsInteger()) ||
4150 (field_type->IsDouble() && insn_type.IsLong())) {
4151 // expected that read is of the correct primitive type or that int reads are reading
4152 // floats or long reads are reading doubles
4153 } else {
4154 // This is a global failure rather than a class change failure as the instructions and
4155 // the descriptors for the type should have been consistent within the same file at
4156 // compile time
4157 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
4158 << " to be of type '" << insn_type
4159 << "' but found type '" << *field_type << "' in get";
4160 return;
4161 }
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08004162 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004163 if (!insn_type.IsAssignableFrom(*field_type)) {
Vladimir Marko414000e2015-06-23 17:45:21 +01004164 // If the field type is not a reference, this is a global failure rather than
4165 // a class change failure as the instructions and the descriptors for the type
4166 // should have been consistent within the same file at compile time.
4167 VerifyError error = field_type->IsReferenceTypes() ? VERIFY_ERROR_BAD_CLASS_SOFT
4168 : VERIFY_ERROR_BAD_CLASS_HARD;
4169 Fail(error) << "expected field " << PrettyField(field)
4170 << " to be compatible with type '" << insn_type
4171 << "' but found type '" << *field_type
4172 << "' in get-object";
Andreas Gampe890da292015-07-06 17:20:18 -07004173 if (error != VERIFY_ERROR_BAD_CLASS_HARD) {
4174 work_line_->SetRegisterType(this, vregA, reg_types_.Conflict());
4175 }
Andreas Gampe896df402014-10-20 22:25:29 -07004176 return;
4177 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004178 }
Andreas Gampe896df402014-10-20 22:25:29 -07004179 if (!field_type->IsLowHalf()) {
4180 work_line_->SetRegisterType(this, vregA, *field_type);
4181 } else {
4182 work_line_->SetRegisterTypeWide(this, vregA, *field_type, field_type->HighHalf(&reg_types_));
4183 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07004184 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004185 LOG(FATAL) << "Unexpected case.";
Ian Rogersd81871c2011-10-03 13:57:23 -07004186 }
4187}
4188
Mathieu Chartierc7853442015-03-27 14:35:38 -07004189ArtField* MethodVerifier::GetQuickFieldAccess(const Instruction* inst,
Ian Rogers98379392014-02-24 16:53:16 -08004190 RegisterLine* reg_line) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08004191 DCHECK(IsInstructionIGetQuickOrIPutQuick(inst->Opcode())) << inst->Opcode();
Ian Rogers7b078e82014-09-10 14:44:24 -07004192 const RegType& object_type = reg_line->GetRegisterType(this, inst->VRegB_22c());
Ian Rogers9bc54402014-04-17 16:40:01 -07004193 if (!object_type.HasClass()) {
4194 VLOG(verifier) << "Failed to get mirror::Class* from '" << object_type << "'";
4195 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004196 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004197 uint32_t field_offset = static_cast<uint32_t>(inst->VRegC_22c());
Mathieu Chartierc7853442015-03-27 14:35:38 -07004198 ArtField* const f = ArtField::FindInstanceFieldWithOffset(object_type.GetClass(), field_offset);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08004199 DCHECK_EQ(f->GetOffset().Uint32Value(), field_offset);
Sebastien Hertz479fc1e2014-04-04 17:51:34 +02004200 if (f == nullptr) {
4201 VLOG(verifier) << "Failed to find instance field at offset '" << field_offset
4202 << "' from '" << PrettyDescriptor(object_type.GetClass()) << "'";
4203 }
4204 return f;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004205}
4206
Andreas Gampe896df402014-10-20 22:25:29 -07004207template <MethodVerifier::FieldAccessType kAccType>
4208void MethodVerifier::VerifyQuickFieldAccess(const Instruction* inst, const RegType& insn_type,
4209 bool is_primitive) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07004210 DCHECK(Runtime::Current()->IsStarted() || verify_to_dump_);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004211
Mathieu Chartierc7853442015-03-27 14:35:38 -07004212 ArtField* field = GetQuickFieldAccess(inst, work_line_.get());
Ian Rogers7b078e82014-09-10 14:44:24 -07004213 if (field == nullptr) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004214 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field from " << inst->Name();
4215 return;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004216 }
Andreas Gampe896df402014-10-20 22:25:29 -07004217
4218 // For an IPUT_QUICK, we now test for final flag of the field.
4219 if (kAccType == FieldAccessType::kAccPut) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004220 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
4221 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004222 << " from other class " << GetDeclaringClass();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004223 return;
4224 }
4225 }
Andreas Gampe896df402014-10-20 22:25:29 -07004226
4227 // Get the field type.
4228 const RegType* field_type;
4229 {
Mathieu Chartierc7853442015-03-27 14:35:38 -07004230 mirror::Class* field_type_class = can_load_classes_ ? field->GetType<true>() :
4231 field->GetType<false>();
Andreas Gampe896df402014-10-20 22:25:29 -07004232
4233 if (field_type_class != nullptr) {
Andreas Gampef23f33d2015-06-23 14:18:17 -07004234 field_type = &FromClass(field->GetTypeDescriptor(), field_type_class,
4235 field_type_class->CannotBeAssignedFromOtherTypes());
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004236 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004237 Thread* self = Thread::Current();
4238 DCHECK(!can_load_classes_ || self->IsExceptionPending());
4239 self->ClearException();
4240 field_type = &reg_types_.FromDescriptor(field->GetDeclaringClass()->GetClassLoader(),
4241 field->GetTypeDescriptor(), false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004242 }
Andreas Gampe896df402014-10-20 22:25:29 -07004243 if (field_type == nullptr) {
4244 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field type from " << inst->Name();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004245 return;
4246 }
Andreas Gampe896df402014-10-20 22:25:29 -07004247 }
4248
4249 const uint32_t vregA = inst->VRegA_22c();
4250 static_assert(kAccType == FieldAccessType::kAccPut || kAccType == FieldAccessType::kAccGet,
4251 "Unexpected third access type");
4252 if (kAccType == FieldAccessType::kAccPut) {
4253 if (is_primitive) {
4254 // Primitive field assignability rules are weaker than regular assignability rules
4255 bool instruction_compatible;
4256 bool value_compatible;
4257 const RegType& value_type = work_line_->GetRegisterType(this, vregA);
4258 if (field_type->IsIntegralTypes()) {
4259 instruction_compatible = insn_type.IsIntegralTypes();
4260 value_compatible = value_type.IsIntegralTypes();
4261 } else if (field_type->IsFloat()) {
4262 instruction_compatible = insn_type.IsInteger(); // no [is]put-float, so expect [is]put-int
4263 value_compatible = value_type.IsFloatTypes();
4264 } else if (field_type->IsLong()) {
4265 instruction_compatible = insn_type.IsLong();
4266 value_compatible = value_type.IsLongTypes();
4267 } else if (field_type->IsDouble()) {
4268 instruction_compatible = insn_type.IsLong(); // no [is]put-double, so expect [is]put-long
4269 value_compatible = value_type.IsDoubleTypes();
4270 } else {
4271 instruction_compatible = false; // reference field with primitive store
4272 value_compatible = false; // unused
4273 }
4274 if (!instruction_compatible) {
4275 // This is a global failure rather than a class change failure as the instructions and
4276 // the descriptors for the type should have been consistent within the same file at
4277 // compile time
4278 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
4279 << " to be of type '" << insn_type
4280 << "' but found type '" << *field_type
4281 << "' in put";
4282 return;
4283 }
4284 if (!value_compatible) {
4285 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
4286 << " of type " << value_type
4287 << " but expected " << *field_type
4288 << " for store to " << PrettyField(field) << " in put";
4289 return;
4290 }
4291 } else {
4292 if (!insn_type.IsAssignableFrom(*field_type)) {
4293 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
4294 << " to be compatible with type '" << insn_type
4295 << "' but found type '" << *field_type
4296 << "' in put-object";
4297 return;
4298 }
4299 work_line_->VerifyRegisterType(this, vregA, *field_type);
4300 }
4301 } else if (kAccType == FieldAccessType::kAccGet) {
4302 if (is_primitive) {
4303 if (field_type->Equals(insn_type) ||
4304 (field_type->IsFloat() && insn_type.IsIntegralTypes()) ||
4305 (field_type->IsDouble() && insn_type.IsLongTypes())) {
4306 // expected that read is of the correct primitive type or that int reads are reading
4307 // floats or long reads are reading doubles
4308 } else {
4309 // This is a global failure rather than a class change failure as the instructions and
4310 // the descriptors for the type should have been consistent within the same file at
4311 // compile time
4312 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
4313 << " to be of type '" << insn_type
4314 << "' but found type '" << *field_type << "' in Get";
4315 return;
4316 }
4317 } else {
4318 if (!insn_type.IsAssignableFrom(*field_type)) {
4319 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
4320 << " to be compatible with type '" << insn_type
4321 << "' but found type '" << *field_type
4322 << "' in get-object";
4323 work_line_->SetRegisterType(this, vregA, reg_types_.Conflict());
4324 return;
4325 }
4326 }
4327 if (!field_type->IsLowHalf()) {
4328 work_line_->SetRegisterType(this, vregA, *field_type);
4329 } else {
4330 work_line_->SetRegisterTypeWide(this, vregA, *field_type, field_type->HighHalf(&reg_types_));
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004331 }
4332 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004333 LOG(FATAL) << "Unexpected case.";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004334 }
4335}
4336
Ian Rogers776ac1f2012-04-13 23:36:36 -07004337bool MethodVerifier::CheckNotMoveException(const uint16_t* insns, int insn_idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004338 if ((insns[insn_idx] & 0xff) == Instruction::MOVE_EXCEPTION) {
jeffhaod5347e02012-03-22 17:25:05 -07004339 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-exception";
Ian Rogersd81871c2011-10-03 13:57:23 -07004340 return false;
4341 }
4342 return true;
4343}
4344
Stephen Kyle9bc61992014-09-22 13:53:15 +01004345bool MethodVerifier::CheckNotMoveResult(const uint16_t* insns, int insn_idx) {
4346 if (((insns[insn_idx] & 0xff) >= Instruction::MOVE_RESULT) &&
4347 ((insns[insn_idx] & 0xff) <= Instruction::MOVE_RESULT_OBJECT)) {
4348 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-result*";
4349 return false;
4350 }
4351 return true;
4352}
4353
4354bool MethodVerifier::CheckNotMoveExceptionOrMoveResult(const uint16_t* insns, int insn_idx) {
4355 return (CheckNotMoveException(insns, insn_idx) && CheckNotMoveResult(insns, insn_idx));
4356}
4357
Ian Rogersebbdd872014-07-07 23:53:08 -07004358bool MethodVerifier::UpdateRegisters(uint32_t next_insn, RegisterLine* merge_line,
4359 bool update_merge_line) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004360 bool changed = true;
4361 RegisterLine* target_line = reg_table_.GetLine(next_insn);
4362 if (!insn_flags_[next_insn].IsVisitedOrChanged()) {
jeffhaobdb76512011-09-07 11:43:16 -07004363 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07004364 * We haven't processed this instruction before, and we haven't touched the registers here, so
4365 * there's nothing to "merge". Copy the registers over and mark it as changed. (This is the
4366 * only way a register can transition out of "unknown", so this is not just an optimization.)
jeffhaobdb76512011-09-07 11:43:16 -07004367 */
Ian Rogersb8c78592013-07-25 23:52:52 +00004368 if (!insn_flags_[next_insn].IsReturn()) {
4369 target_line->CopyFromLine(merge_line);
4370 } else {
Jeff Haob24b4a72013-07-31 13:47:31 -07004371 // Verify that the monitor stack is empty on return.
Ian Rogers7b078e82014-09-10 14:44:24 -07004372 if (!merge_line->VerifyMonitorStackEmpty(this)) {
Jeff Haob24b4a72013-07-31 13:47:31 -07004373 return false;
4374 }
Ian Rogersb8c78592013-07-25 23:52:52 +00004375 // For returns we only care about the operand to the return, all other registers are dead.
4376 // Initialize them as conflicts so they don't add to GC and deoptimization information.
4377 const Instruction* ret_inst = Instruction::At(code_item_->insns_ + next_insn);
4378 Instruction::Code opcode = ret_inst->Opcode();
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07004379 if (opcode == Instruction::RETURN_VOID || opcode == Instruction::RETURN_VOID_NO_BARRIER) {
Andreas Gampef10b6e12015-08-12 10:48:12 -07004380 // Explicitly copy the this-initialized flag from the merge-line, as we didn't copy its
4381 // state. Must be done before SafelyMarkAllRegistersAsConflicts as that will do the
4382 // super-constructor-call checking.
4383 target_line->CopyThisInitialized(*merge_line);
Stephen Kyle7e541c92014-12-17 17:10:02 +00004384 SafelyMarkAllRegistersAsConflicts(this, target_line);
Ian Rogersb8c78592013-07-25 23:52:52 +00004385 } else {
4386 target_line->CopyFromLine(merge_line);
4387 if (opcode == Instruction::RETURN_WIDE) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004388 target_line->MarkAllRegistersAsConflictsExceptWide(this, ret_inst->VRegA_11x());
Ian Rogersb8c78592013-07-25 23:52:52 +00004389 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004390 target_line->MarkAllRegistersAsConflictsExcept(this, ret_inst->VRegA_11x());
Ian Rogersb8c78592013-07-25 23:52:52 +00004391 }
4392 }
4393 }
jeffhaobdb76512011-09-07 11:43:16 -07004394 } else {
Ian Rogers700a4022014-05-19 16:49:03 -07004395 std::unique_ptr<RegisterLine> copy(gDebugVerify ?
Ian Rogersd0fbd852013-09-24 18:17:04 -07004396 RegisterLine::Create(target_line->NumRegs(), this) :
Ian Rogers7b078e82014-09-10 14:44:24 -07004397 nullptr);
Ian Rogers7b0c5b42012-02-16 15:29:07 -08004398 if (gDebugVerify) {
4399 copy->CopyFromLine(target_line);
4400 }
Ian Rogers7b078e82014-09-10 14:44:24 -07004401 changed = target_line->MergeRegisters(this, merge_line);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004402 if (have_pending_hard_failure_) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004403 return false;
jeffhaobdb76512011-09-07 11:43:16 -07004404 }
Ian Rogers2c8a8572011-10-24 17:11:36 -07004405 if (gDebugVerify && changed) {
Elliott Hughes398f64b2012-03-26 18:05:48 -07004406 LogVerifyInfo() << "Merging at [" << reinterpret_cast<void*>(work_insn_idx_) << "]"
Elliott Hughesc073b072012-05-24 19:29:17 -07004407 << " to [" << reinterpret_cast<void*>(next_insn) << "]: " << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07004408 << copy->Dump(this) << " MERGE\n"
4409 << merge_line->Dump(this) << " ==\n"
4410 << target_line->Dump(this) << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07004411 }
Ian Rogersebbdd872014-07-07 23:53:08 -07004412 if (update_merge_line && changed) {
4413 merge_line->CopyFromLine(target_line);
4414 }
jeffhaobdb76512011-09-07 11:43:16 -07004415 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004416 if (changed) {
4417 insn_flags_[next_insn].SetChanged();
jeffhaobdb76512011-09-07 11:43:16 -07004418 }
4419 return true;
4420}
4421
Ian Rogers7b3ddd22013-02-21 15:19:52 -08004422InstructionFlags* MethodVerifier::CurrentInsnFlags() {
Ian Rogers776ac1f2012-04-13 23:36:36 -07004423 return &insn_flags_[work_insn_idx_];
4424}
4425
Ian Rogersd8f69b02014-09-10 21:43:52 +00004426const RegType& MethodVerifier::GetMethodReturnType() {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004427 if (return_type_ == nullptr) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07004428 if (mirror_method_ != nullptr) {
Ian Rogersded66a02014-10-28 18:12:55 -07004429 mirror::Class* return_type_class = mirror_method_->GetReturnType(can_load_classes_);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004430 if (return_type_class != nullptr) {
Andreas Gampef23f33d2015-06-23 14:18:17 -07004431 return_type_ = &FromClass(mirror_method_->GetReturnTypeDescriptor(),
4432 return_type_class,
4433 return_type_class->CannotBeAssignedFromOtherTypes());
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004434 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004435 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
4436 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004437 }
4438 }
4439 if (return_type_ == nullptr) {
4440 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
4441 const DexFile::ProtoId& proto_id = dex_file_->GetMethodPrototype(method_id);
4442 uint16_t return_type_idx = proto_id.return_type_idx_;
4443 const char* descriptor = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(return_type_idx));
Mathieu Chartierbf99f772014-08-23 16:37:27 -07004444 return_type_ = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004445 }
4446 }
4447 return *return_type_;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004448}
4449
Ian Rogersd8f69b02014-09-10 21:43:52 +00004450const RegType& MethodVerifier::GetDeclaringClass() {
Ian Rogers7b078e82014-09-10 14:44:24 -07004451 if (declaring_class_ == nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004452 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
Brian Carlstrom93c33962013-07-26 10:37:43 -07004453 const char* descriptor
4454 = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(method_id.class_idx_));
Mathieu Chartiere401d142015-04-22 13:56:20 -07004455 if (mirror_method_ != nullptr) {
Ian Rogers637c65b2013-05-31 11:46:00 -07004456 mirror::Class* klass = mirror_method_->GetDeclaringClass();
Andreas Gampef23f33d2015-06-23 14:18:17 -07004457 declaring_class_ = &FromClass(descriptor, klass,
4458 klass->CannotBeAssignedFromOtherTypes());
Ian Rogers637c65b2013-05-31 11:46:00 -07004459 } else {
Mathieu Chartierbf99f772014-08-23 16:37:27 -07004460 declaring_class_ = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers637c65b2013-05-31 11:46:00 -07004461 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07004462 }
Ian Rogers637c65b2013-05-31 11:46:00 -07004463 return *declaring_class_;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004464}
4465
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004466std::vector<int32_t> MethodVerifier::DescribeVRegs(uint32_t dex_pc) {
4467 RegisterLine* line = reg_table_.GetLine(dex_pc);
Sebastien Hertzaa0c00c2014-03-14 17:58:54 +01004468 DCHECK(line != nullptr) << "No register line at DEX pc " << StringPrintf("0x%x", dex_pc);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004469 std::vector<int32_t> result;
4470 for (size_t i = 0; i < line->NumRegs(); ++i) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004471 const RegType& type = line->GetRegisterType(this, i);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004472 if (type.IsConstant()) {
4473 result.push_back(type.IsPreciseConstant() ? kConstant : kImpreciseConstant);
Ian Rogers7b078e82014-09-10 14:44:24 -07004474 const ConstantType* const_val = down_cast<const ConstantType*>(&type);
4475 result.push_back(const_val->ConstantValue());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004476 } else if (type.IsConstantLo()) {
4477 result.push_back(type.IsPreciseConstantLo() ? kConstant : kImpreciseConstant);
Ian Rogers7b078e82014-09-10 14:44:24 -07004478 const ConstantType* const_val = down_cast<const ConstantType*>(&type);
4479 result.push_back(const_val->ConstantValueLo());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004480 } else if (type.IsConstantHi()) {
4481 result.push_back(type.IsPreciseConstantHi() ? kConstant : kImpreciseConstant);
Ian Rogers7b078e82014-09-10 14:44:24 -07004482 const ConstantType* const_val = down_cast<const ConstantType*>(&type);
4483 result.push_back(const_val->ConstantValueHi());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004484 } else if (type.IsIntegralTypes()) {
4485 result.push_back(kIntVReg);
4486 result.push_back(0);
4487 } else if (type.IsFloat()) {
4488 result.push_back(kFloatVReg);
4489 result.push_back(0);
4490 } else if (type.IsLong()) {
4491 result.push_back(kLongLoVReg);
4492 result.push_back(0);
4493 result.push_back(kLongHiVReg);
4494 result.push_back(0);
4495 ++i;
4496 } else if (type.IsDouble()) {
4497 result.push_back(kDoubleLoVReg);
4498 result.push_back(0);
4499 result.push_back(kDoubleHiVReg);
4500 result.push_back(0);
4501 ++i;
4502 } else if (type.IsUndefined() || type.IsConflict() || type.IsHighHalf()) {
4503 result.push_back(kUndefined);
4504 result.push_back(0);
4505 } else {
Ian Rogers7b3ddd22013-02-21 15:19:52 -08004506 CHECK(type.IsNonZeroReferenceTypes());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004507 result.push_back(kReferenceVReg);
4508 result.push_back(0);
4509 }
4510 }
4511 return result;
4512}
4513
Ian Rogersd8f69b02014-09-10 21:43:52 +00004514const RegType& MethodVerifier::DetermineCat1Constant(int32_t value, bool precise) {
Sebastien Hertz849600b2013-12-20 10:28:08 +01004515 if (precise) {
4516 // Precise constant type.
4517 return reg_types_.FromCat1Const(value, true);
4518 } else {
4519 // Imprecise constant type.
4520 if (value < -32768) {
4521 return reg_types_.IntConstant();
4522 } else if (value < -128) {
4523 return reg_types_.ShortConstant();
4524 } else if (value < 0) {
4525 return reg_types_.ByteConstant();
4526 } else if (value == 0) {
4527 return reg_types_.Zero();
4528 } else if (value == 1) {
4529 return reg_types_.One();
4530 } else if (value < 128) {
4531 return reg_types_.PosByteConstant();
4532 } else if (value < 32768) {
4533 return reg_types_.PosShortConstant();
4534 } else if (value < 65536) {
4535 return reg_types_.CharConstant();
4536 } else {
4537 return reg_types_.IntConstant();
4538 }
4539 }
4540}
4541
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004542void MethodVerifier::Init() {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08004543 art::verifier::RegTypeCache::Init();
Brian Carlstrome7d856b2012-01-11 18:10:55 -08004544}
4545
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004546void MethodVerifier::Shutdown() {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08004547 verifier::RegTypeCache::ShutDown();
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08004548}
jeffhaod1224c72012-02-29 13:43:08 -08004549
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -07004550void MethodVerifier::VisitStaticRoots(RootVisitor* visitor) {
4551 RegTypeCache::VisitStaticRoots(visitor);
Mathieu Chartier7c438b12014-09-12 17:01:24 -07004552}
4553
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -07004554void MethodVerifier::VisitRoots(RootVisitor* visitor, const RootInfo& root_info) {
4555 reg_types_.VisitRoots(visitor, root_info);
Mathieu Chartierc528dba2013-11-26 12:00:11 -08004556}
4557
Andreas Gampef23f33d2015-06-23 14:18:17 -07004558const RegType& MethodVerifier::FromClass(const char* descriptor,
4559 mirror::Class* klass,
4560 bool precise) {
4561 DCHECK(klass != nullptr);
4562 if (precise && !klass->IsInstantiable() && !klass->IsPrimitive()) {
4563 Fail(VerifyError::VERIFY_ERROR_NO_CLASS) << "Could not create precise reference for "
4564 << "non-instantiable klass " << descriptor;
4565 precise = false;
4566 }
4567 return reg_types_.FromClass(descriptor, klass, precise);
4568}
4569
Ian Rogersd81871c2011-10-03 13:57:23 -07004570} // namespace verifier
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07004571} // namespace art