blob: 11c3e659eefd995abd27c625c4909da936092c30 [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070016
Mathieu Chartierc645f1d2014-03-06 18:11:53 -080017#include "method_verifier-inl.h"
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070018
Elliott Hughes1f359b02011-07-17 14:27:17 -070019#include <iostream>
20
Mathieu Chartierc7853442015-03-27 14:35:38 -070021#include "art_field-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070022#include "art_method-inl.h"
Elliott Hughes07ed66b2012-12-12 18:34:25 -080023#include "base/logging.h"
Ian Rogers637c65b2013-05-31 11:46:00 -070024#include "base/mutex-inl.h"
Vladimir Marko80afd022015-05-19 18:08:00 +010025#include "base/time_utils.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070026#include "class_linker.h"
Vladimir Marko2b5eaa22013-12-13 13:59:30 +000027#include "compiler_callbacks.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070028#include "dex_file-inl.h"
Ian Rogersd0583802013-06-01 10:51:46 -070029#include "dex_instruction-inl.h"
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080030#include "dex_instruction_utils.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070031#include "dex_instruction_visitor.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070032#include "gc/accounting/card_table-inl.h"
Ian Rogers2bcb4a42012-11-08 10:39:18 -080033#include "indenter.h"
Ian Rogers84fa0742011-10-25 18:13:30 -070034#include "intern_table.h"
Ian Rogers0571d352011-11-03 19:51:38 -070035#include "leb128.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080036#include "mirror/class.h"
37#include "mirror/class-inl.h"
Ian Rogers39ebcb82013-05-30 16:57:23 -070038#include "mirror/dex_cache-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080039#include "mirror/object-inl.h"
40#include "mirror/object_array-inl.h"
Ian Rogers7b078e82014-09-10 14:44:24 -070041#include "reg_type-inl.h"
Ian Rogers39ebcb82013-05-30 16:57:23 -070042#include "register_line-inl.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070043#include "runtime.h"
Mathieu Chartier590fee92013-09-13 13:46:47 -070044#include "scoped_thread_state_change.h"
Vladimir Marko80afd022015-05-19 18:08:00 +010045#include "utils.h"
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070046#include "handle_scope-inl.h"
Elliott Hughese222ee02012-12-13 14:41:43 -080047#include "verifier/dex_gc_map.h"
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070048
49namespace art {
Ian Rogersd81871c2011-10-03 13:57:23 -070050namespace verifier {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070051
Mathieu Chartier8e219ae2014-08-19 14:29:46 -070052static constexpr bool kTimeVerifyMethod = !kIsDebugBuild;
Ian Rogersebbdd872014-07-07 23:53:08 -070053static constexpr bool gDebugVerify = false;
Anwar Ghuloum75a43f12013-08-13 17:22:14 -070054// TODO: Add a constant to method_verifier to turn on verbose logging?
Ian Rogers2c8a8572011-10-24 17:11:36 -070055
Ian Rogers7b3ddd22013-02-21 15:19:52 -080056void PcToRegisterLineTable::Init(RegisterTrackingMode mode, InstructionFlags* flags,
Ian Rogersd81871c2011-10-03 13:57:23 -070057 uint32_t insns_size, uint16_t registers_size,
Ian Rogers776ac1f2012-04-13 23:36:36 -070058 MethodVerifier* verifier) {
Ian Rogersd81871c2011-10-03 13:57:23 -070059 DCHECK_GT(insns_size, 0U);
Ian Rogersd0fbd852013-09-24 18:17:04 -070060 register_lines_.reset(new RegisterLine*[insns_size]());
61 size_ = insns_size;
Ian Rogersd81871c2011-10-03 13:57:23 -070062 for (uint32_t i = 0; i < insns_size; i++) {
63 bool interesting = false;
64 switch (mode) {
65 case kTrackRegsAll:
66 interesting = flags[i].IsOpcode();
67 break;
Sameer Abu Asal02c42232013-04-30 12:09:45 -070068 case kTrackCompilerInterestPoints:
Brian Carlstrom02c8cc62013-07-18 15:54:44 -070069 interesting = flags[i].IsCompileTimeInfoPoint() || flags[i].IsBranchTarget();
Ian Rogersd81871c2011-10-03 13:57:23 -070070 break;
71 case kTrackRegsBranches:
72 interesting = flags[i].IsBranchTarget();
73 break;
74 default:
75 break;
76 }
77 if (interesting) {
Ian Rogersd0fbd852013-09-24 18:17:04 -070078 register_lines_[i] = RegisterLine::Create(registers_size, verifier);
79 }
80 }
81}
82
83PcToRegisterLineTable::~PcToRegisterLineTable() {
84 for (size_t i = 0; i < size_; i++) {
85 delete register_lines_[i];
86 if (kIsDebugBuild) {
87 register_lines_[i] = nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -070088 }
89 }
90}
91
Andreas Gampe7c038102014-10-27 20:08:46 -070092// Note: returns true on failure.
93ALWAYS_INLINE static inline bool FailOrAbort(MethodVerifier* verifier, bool condition,
94 const char* error_msg, uint32_t work_insn_idx) {
95 if (kIsDebugBuild) {
96 // In a debug build, abort if the error condition is wrong.
97 DCHECK(condition) << error_msg << work_insn_idx;
98 } else {
99 // In a non-debug build, just fail the class.
100 if (!condition) {
101 verifier->Fail(VERIFY_ERROR_BAD_CLASS_HARD) << error_msg << work_insn_idx;
102 return true;
103 }
104 }
105
106 return false;
107}
108
Stephen Kyle7e541c92014-12-17 17:10:02 +0000109static void SafelyMarkAllRegistersAsConflicts(MethodVerifier* verifier, RegisterLine* reg_line) {
110 if (verifier->IsConstructor()) {
111 // Before we mark all regs as conflicts, check that we don't have an uninitialized this.
112 reg_line->CheckConstructorReturn(verifier);
113 }
114 reg_line->MarkAllRegistersAsConflicts(verifier);
115}
116
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800117MethodVerifier::FailureKind MethodVerifier::VerifyMethod(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700118 ArtMethod* method, bool allow_soft_failures, std::string* error ATTRIBUTE_UNUSED) {
119 StackHandleScope<2> hs(Thread::Current());
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800120 mirror::Class* klass = method->GetDeclaringClass();
121 auto h_dex_cache(hs.NewHandle(klass->GetDexCache()));
122 auto h_class_loader(hs.NewHandle(klass->GetClassLoader()));
Mathieu Chartiere401d142015-04-22 13:56:20 -0700123 return VerifyMethod(hs.Self(), method->GetDexMethodIndex(), method->GetDexFile(), h_dex_cache,
124 h_class_loader, klass->GetClassDef(), method->GetCodeItem(), method,
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800125 method->GetAccessFlags(), allow_soft_failures, false);
126}
127
128
Ian Rogers7b078e82014-09-10 14:44:24 -0700129MethodVerifier::FailureKind MethodVerifier::VerifyClass(Thread* self,
130 mirror::Class* klass,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700131 bool allow_soft_failures,
132 std::string* error) {
jeffhaobdb76512011-09-07 11:43:16 -0700133 if (klass->IsVerified()) {
jeffhaof1e6b7c2012-06-05 18:33:30 -0700134 return kNoFailure;
jeffhaobdb76512011-09-07 11:43:16 -0700135 }
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800136 bool early_failure = false;
137 std::string failure_message;
Mathieu Chartierf8322842014-05-16 10:59:25 -0700138 const DexFile& dex_file = klass->GetDexFile();
139 const DexFile::ClassDef* class_def = klass->GetClassDef();
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800140 mirror::Class* super = klass->GetSuperClass();
Ian Rogers1ff3c982014-08-12 02:30:58 -0700141 std::string temp;
Ian Rogers7b078e82014-09-10 14:44:24 -0700142 if (super == nullptr && strcmp("Ljava/lang/Object;", klass->GetDescriptor(&temp)) != 0) {
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800143 early_failure = true;
144 failure_message = " that has no super class";
Ian Rogers7b078e82014-09-10 14:44:24 -0700145 } else if (super != nullptr && super->IsFinal()) {
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800146 early_failure = true;
147 failure_message = " that attempts to sub-class final class " + PrettyDescriptor(super);
Ian Rogers7b078e82014-09-10 14:44:24 -0700148 } else if (class_def == nullptr) {
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800149 early_failure = true;
150 failure_message = " that isn't present in dex file " + dex_file.GetLocation();
151 }
152 if (early_failure) {
153 *error = "Verifier rejected class " + PrettyDescriptor(klass) + failure_message;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800154 if (Runtime::Current()->IsAotCompiler()) {
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800155 ClassReference ref(&dex_file, klass->GetDexClassDefIndex());
Vladimir Marko2b5eaa22013-12-13 13:59:30 +0000156 Runtime::Current()->GetCompilerCallbacks()->ClassRejected(ref);
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800157 }
jeffhaof1e6b7c2012-06-05 18:33:30 -0700158 return kHardFailure;
jeffhaobdb76512011-09-07 11:43:16 -0700159 }
Ian Rogers7b078e82014-09-10 14:44:24 -0700160 StackHandleScope<2> hs(self);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700161 Handle<mirror::DexCache> dex_cache(hs.NewHandle(klass->GetDexCache()));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700162 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(klass->GetClassLoader()));
Mathieu Chartiere401d142015-04-22 13:56:20 -0700163 return VerifyClass(
164 self, &dex_file, dex_cache, class_loader, class_def, allow_soft_failures, error);
Shih-wei Liao371814f2011-10-27 16:52:10 -0700165}
166
Ian Rogers7b078e82014-09-10 14:44:24 -0700167MethodVerifier::FailureKind MethodVerifier::VerifyClass(Thread* self,
168 const DexFile* dex_file,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700169 Handle<mirror::DexCache> dex_cache,
170 Handle<mirror::ClassLoader> class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700171 const DexFile::ClassDef* class_def,
172 bool allow_soft_failures,
173 std::string* error) {
174 DCHECK(class_def != nullptr);
Andreas Gampe507cc6f2015-06-19 22:58:47 -0700175
176 // A class must not be abstract and final.
177 if ((class_def->access_flags_ & (kAccAbstract | kAccFinal)) == (kAccAbstract | kAccFinal)) {
178 *error = "Verifier rejected class ";
179 *error += PrettyDescriptor(dex_file->GetClassDescriptor(*class_def));
180 *error += ": class is abstract and final.";
181 return kHardFailure;
182 }
183
Ian Rogers13735952014-10-08 12:43:28 -0700184 const uint8_t* class_data = dex_file->GetClassData(*class_def);
Ian Rogers7b078e82014-09-10 14:44:24 -0700185 if (class_data == nullptr) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700186 // empty class, probably a marker interface
jeffhaof1e6b7c2012-06-05 18:33:30 -0700187 return kNoFailure;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700188 }
jeffhaof56197c2012-03-05 18:01:54 -0800189 ClassDataItemIterator it(*dex_file, class_data);
190 while (it.HasNextStaticField() || it.HasNextInstanceField()) {
191 it.Next();
192 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700193 size_t error_count = 0;
jeffhaof1e6b7c2012-06-05 18:33:30 -0700194 bool hard_fail = false;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700195 ClassLinker* linker = Runtime::Current()->GetClassLinker();
jeffhao9b0b1882012-10-01 16:51:22 -0700196 int64_t previous_direct_method_idx = -1;
jeffhaof56197c2012-03-05 18:01:54 -0800197 while (it.HasNextDirectMethod()) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700198 self->AllowThreadSuspension();
jeffhaof56197c2012-03-05 18:01:54 -0800199 uint32_t method_idx = it.GetMemberIndex();
jeffhao9b0b1882012-10-01 16:51:22 -0700200 if (method_idx == previous_direct_method_idx) {
201 // smali can create dex files with two encoded_methods sharing the same method_idx
202 // http://code.google.com/p/smali/issues/detail?id=119
203 it.Next();
204 continue;
205 }
206 previous_direct_method_idx = method_idx;
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700207 InvokeType type = it.GetMethodInvokeType(*class_def);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700208 ArtMethod* method = linker->ResolveMethod(
209 *dex_file, method_idx, dex_cache, class_loader, nullptr, type);
Ian Rogers7b078e82014-09-10 14:44:24 -0700210 if (method == nullptr) {
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700211 DCHECK(self->IsExceptionPending());
Ian Rogersad0b3a32012-04-16 14:50:24 -0700212 // We couldn't resolve the method, but continue regardless.
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700213 self->ClearException();
Mathieu Chartiere401d142015-04-22 13:56:20 -0700214 } else {
215 DCHECK(method->GetDeclaringClassUnchecked() != nullptr) << type;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700216 }
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700217 StackHandleScope<1> hs(self);
Ian Rogers7b078e82014-09-10 14:44:24 -0700218 MethodVerifier::FailureKind result = VerifyMethod(self,
219 method_idx,
Brian Carlstrom93c33962013-07-26 10:37:43 -0700220 dex_file,
221 dex_cache,
222 class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700223 class_def,
Brian Carlstrom93c33962013-07-26 10:37:43 -0700224 it.GetMethodCodeItem(),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700225 method, it.GetMethodAccessFlags(), allow_soft_failures, false);
jeffhaof1e6b7c2012-06-05 18:33:30 -0700226 if (result != kNoFailure) {
227 if (result == kHardFailure) {
228 hard_fail = true;
229 if (error_count > 0) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700230 *error += "\n";
jeffhaof1e6b7c2012-06-05 18:33:30 -0700231 }
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700232 *error = "Verifier rejected class ";
233 *error += PrettyDescriptor(dex_file->GetClassDescriptor(*class_def));
234 *error += " due to bad method ";
235 *error += PrettyMethod(method_idx, *dex_file);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700236 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700237 ++error_count;
jeffhaof56197c2012-03-05 18:01:54 -0800238 }
239 it.Next();
240 }
jeffhao9b0b1882012-10-01 16:51:22 -0700241 int64_t previous_virtual_method_idx = -1;
jeffhaof56197c2012-03-05 18:01:54 -0800242 while (it.HasNextVirtualMethod()) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700243 self->AllowThreadSuspension();
jeffhaof56197c2012-03-05 18:01:54 -0800244 uint32_t method_idx = it.GetMemberIndex();
jeffhao9b0b1882012-10-01 16:51:22 -0700245 if (method_idx == previous_virtual_method_idx) {
246 // smali can create dex files with two encoded_methods sharing the same method_idx
247 // http://code.google.com/p/smali/issues/detail?id=119
248 it.Next();
249 continue;
250 }
251 previous_virtual_method_idx = method_idx;
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700252 InvokeType type = it.GetMethodInvokeType(*class_def);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700253 ArtMethod* method = linker->ResolveMethod(
254 *dex_file, method_idx, dex_cache, class_loader, nullptr, type);
Ian Rogers7b078e82014-09-10 14:44:24 -0700255 if (method == nullptr) {
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700256 DCHECK(self->IsExceptionPending());
Ian Rogersad0b3a32012-04-16 14:50:24 -0700257 // We couldn't resolve the method, but continue regardless.
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700258 self->ClearException();
Ian Rogersad0b3a32012-04-16 14:50:24 -0700259 }
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700260 StackHandleScope<1> hs(self);
Ian Rogers7b078e82014-09-10 14:44:24 -0700261 MethodVerifier::FailureKind result = VerifyMethod(self,
262 method_idx,
Brian Carlstrom93c33962013-07-26 10:37:43 -0700263 dex_file,
264 dex_cache,
265 class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700266 class_def,
Brian Carlstrom93c33962013-07-26 10:37:43 -0700267 it.GetMethodCodeItem(),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700268 method, it.GetMethodAccessFlags(), allow_soft_failures, false);
jeffhaof1e6b7c2012-06-05 18:33:30 -0700269 if (result != kNoFailure) {
270 if (result == kHardFailure) {
271 hard_fail = true;
272 if (error_count > 0) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700273 *error += "\n";
jeffhaof1e6b7c2012-06-05 18:33:30 -0700274 }
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700275 *error = "Verifier rejected class ";
276 *error += PrettyDescriptor(dex_file->GetClassDescriptor(*class_def));
277 *error += " due to bad method ";
278 *error += PrettyMethod(method_idx, *dex_file);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700279 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700280 ++error_count;
jeffhaof56197c2012-03-05 18:01:54 -0800281 }
282 it.Next();
283 }
jeffhaof1e6b7c2012-06-05 18:33:30 -0700284 if (error_count == 0) {
285 return kNoFailure;
286 } else {
287 return hard_fail ? kHardFailure : kSoftFailure;
288 }
jeffhaof56197c2012-03-05 18:01:54 -0800289}
290
Andreas Gampea4f5bf62015-05-18 20:50:12 -0700291static bool IsLargeMethod(const DexFile::CodeItem* const code_item) {
Andreas Gampe3c651fc2015-05-21 14:06:46 -0700292 if (code_item == nullptr) {
293 return false;
294 }
295
Andreas Gampea4f5bf62015-05-18 20:50:12 -0700296 uint16_t registers_size = code_item->registers_size_;
297 uint32_t insns_size = code_item->insns_size_in_code_units_;
298
299 return registers_size * insns_size > 4*1024*1024;
300}
301
Ian Rogers7b078e82014-09-10 14:44:24 -0700302MethodVerifier::FailureKind MethodVerifier::VerifyMethod(Thread* self, uint32_t method_idx,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800303 const DexFile* dex_file,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700304 Handle<mirror::DexCache> dex_cache,
305 Handle<mirror::ClassLoader> class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700306 const DexFile::ClassDef* class_def,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800307 const DexFile::CodeItem* code_item,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700308 ArtMethod* method,
Jeff Haoee988952013-04-16 14:23:47 -0700309 uint32_t method_access_flags,
Ian Rogers46960fe2014-05-23 10:43:43 -0700310 bool allow_soft_failures,
311 bool need_precise_constants) {
Ian Rogersc8982582012-09-07 16:53:25 -0700312 MethodVerifier::FailureKind result = kNoFailure;
Mathieu Chartier8e219ae2014-08-19 14:29:46 -0700313 uint64_t start_ns = kTimeVerifyMethod ? NanoTime() : 0;
Ian Rogersc8982582012-09-07 16:53:25 -0700314
Ian Rogers7b078e82014-09-10 14:44:24 -0700315 MethodVerifier verifier(self, dex_file, dex_cache, class_loader, class_def, code_item,
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700316 method_idx, method, method_access_flags, true, allow_soft_failures,
Mathieu Chartier4306ef82014-12-19 18:41:47 -0800317 need_precise_constants, true);
Ian Rogers46960fe2014-05-23 10:43:43 -0700318 if (verifier.Verify()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700319 // Verification completed, however failures may be pending that didn't cause the verification
320 // to hard fail.
Ian Rogers46960fe2014-05-23 10:43:43 -0700321 CHECK(!verifier.have_pending_hard_failure_);
322 if (verifier.failures_.size() != 0) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -0700323 if (VLOG_IS_ON(verifier)) {
Ian Rogers46960fe2014-05-23 10:43:43 -0700324 verifier.DumpFailures(VLOG_STREAM(verifier) << "Soft verification failures in "
Anwar Ghuloum75a43f12013-08-13 17:22:14 -0700325 << PrettyMethod(method_idx, *dex_file) << "\n");
326 }
Ian Rogersc8982582012-09-07 16:53:25 -0700327 result = kSoftFailure;
jeffhaof56197c2012-03-05 18:01:54 -0800328 }
329 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700330 // Bad method data.
Ian Rogers46960fe2014-05-23 10:43:43 -0700331 CHECK_NE(verifier.failures_.size(), 0U);
332 CHECK(verifier.have_pending_hard_failure_);
333 verifier.DumpFailures(LOG(INFO) << "Verification error in "
Elliott Hughesc073b072012-05-24 19:29:17 -0700334 << PrettyMethod(method_idx, *dex_file) << "\n");
jeffhaof56197c2012-03-05 18:01:54 -0800335 if (gDebugVerify) {
Ian Rogers46960fe2014-05-23 10:43:43 -0700336 std::cout << "\n" << verifier.info_messages_.str();
337 verifier.Dump(std::cout);
jeffhaof56197c2012-03-05 18:01:54 -0800338 }
Ian Rogersc8982582012-09-07 16:53:25 -0700339 result = kHardFailure;
jeffhaof56197c2012-03-05 18:01:54 -0800340 }
Mathieu Chartier8e219ae2014-08-19 14:29:46 -0700341 if (kTimeVerifyMethod) {
342 uint64_t duration_ns = NanoTime() - start_ns;
343 if (duration_ns > MsToNs(100)) {
344 LOG(WARNING) << "Verification of " << PrettyMethod(method_idx, *dex_file)
Andreas Gampea4f5bf62015-05-18 20:50:12 -0700345 << " took " << PrettyDuration(duration_ns)
346 << (IsLargeMethod(code_item) ? " (large method)" : "");
Mathieu Chartier8e219ae2014-08-19 14:29:46 -0700347 }
Ian Rogersc8982582012-09-07 16:53:25 -0700348 }
349 return result;
jeffhaof56197c2012-03-05 18:01:54 -0800350}
351
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100352MethodVerifier* MethodVerifier::VerifyMethodAndDump(Thread* self,
353 VariableIndentationOutputStream* vios,
354 uint32_t dex_method_idx,
355 const DexFile* dex_file,
356 Handle<mirror::DexCache> dex_cache,
357 Handle<mirror::ClassLoader> class_loader,
358 const DexFile::ClassDef* class_def,
359 const DexFile::CodeItem* code_item,
360 ArtMethod* method,
361 uint32_t method_access_flags) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700362 MethodVerifier* verifier = new MethodVerifier(self, dex_file, dex_cache, class_loader,
363 class_def, code_item, dex_method_idx, method,
364 method_access_flags, true, true, true, true);
365 verifier->Verify();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100366 verifier->DumpFailures(vios->Stream());
367 vios->Stream() << verifier->info_messages_.str();
Andreas Gampe5cbcde22014-09-16 14:59:49 -0700368 // Only dump and return if no hard failures. Otherwise the verifier may be not fully initialized
369 // and querying any info is dangerous/can abort.
370 if (verifier->have_pending_hard_failure_) {
371 delete verifier;
372 return nullptr;
373 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100374 verifier->Dump(vios);
Andreas Gampe5cbcde22014-09-16 14:59:49 -0700375 return verifier;
376 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800377}
378
Ian Rogers7b078e82014-09-10 14:44:24 -0700379MethodVerifier::MethodVerifier(Thread* self,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700380 const DexFile* dex_file, Handle<mirror::DexCache> dex_cache,
381 Handle<mirror::ClassLoader> class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700382 const DexFile::ClassDef* class_def,
Mathieu Chartier590fee92013-09-13 13:46:47 -0700383 const DexFile::CodeItem* code_item, uint32_t dex_method_idx,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700384 ArtMethod* method, uint32_t method_access_flags,
Ian Rogers46960fe2014-05-23 10:43:43 -0700385 bool can_load_classes, bool allow_soft_failures,
Mathieu Chartier4306ef82014-12-19 18:41:47 -0800386 bool need_precise_constants, bool verify_to_dump,
387 bool allow_thread_suspension)
Ian Rogers7b078e82014-09-10 14:44:24 -0700388 : self_(self),
389 reg_types_(can_load_classes),
Andreas Gamped5ad72f2015-06-26 17:33:47 -0700390 work_insn_idx_(DexFile::kDexNoIndex),
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800391 dex_method_idx_(dex_method_idx),
Ian Rogers637c65b2013-05-31 11:46:00 -0700392 mirror_method_(method),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700393 method_access_flags_(method_access_flags),
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700394 return_type_(nullptr),
jeffhaof56197c2012-03-05 18:01:54 -0800395 dex_file_(dex_file),
396 dex_cache_(dex_cache),
397 class_loader_(class_loader),
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700398 class_def_(class_def),
jeffhaof56197c2012-03-05 18:01:54 -0800399 code_item_(code_item),
Ian Rogers7b078e82014-09-10 14:44:24 -0700400 declaring_class_(nullptr),
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700401 interesting_dex_pc_(-1),
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700402 monitor_enter_dex_pcs_(nullptr),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700403 have_pending_hard_failure_(false),
jeffhaofaf459e2012-08-31 15:32:47 -0700404 have_pending_runtime_throw_failure_(false),
Andreas Gamped12e7822015-06-25 10:26:40 -0700405 have_any_pending_runtime_throw_failure_(false),
jeffhaof56197c2012-03-05 18:01:54 -0800406 new_instance_count_(0),
Elliott Hughes80537bb2013-01-04 16:37:26 -0800407 monitor_enter_count_(0),
Jeff Haoee988952013-04-16 14:23:47 -0700408 can_load_classes_(can_load_classes),
Sebastien Hertz4d4adb12013-07-24 16:14:19 +0200409 allow_soft_failures_(allow_soft_failures),
Ian Rogers46960fe2014-05-23 10:43:43 -0700410 need_precise_constants_(need_precise_constants),
Sebastien Hertz4d4adb12013-07-24 16:14:19 +0200411 has_check_casts_(false),
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700412 has_virtual_or_interface_invokes_(false),
Mathieu Chartier4306ef82014-12-19 18:41:47 -0800413 verify_to_dump_(verify_to_dump),
Andreas Gamped5ad72f2015-06-26 17:33:47 -0700414 allow_thread_suspension_(allow_thread_suspension),
415 link_(nullptr) {
Mathieu Chartierd0ad2ee2015-03-31 14:59:59 -0700416 self->PushVerifier(this);
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700417 DCHECK(class_def != nullptr);
jeffhaof56197c2012-03-05 18:01:54 -0800418}
419
Mathieu Chartier590fee92013-09-13 13:46:47 -0700420MethodVerifier::~MethodVerifier() {
Mathieu Chartierd0ad2ee2015-03-31 14:59:59 -0700421 Thread::Current()->PopVerifier(this);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700422 STLDeleteElements(&failure_messages_);
423}
424
Mathieu Chartiere401d142015-04-22 13:56:20 -0700425void MethodVerifier::FindLocksAtDexPc(ArtMethod* m, uint32_t dex_pc,
Ian Rogers46960fe2014-05-23 10:43:43 -0700426 std::vector<uint32_t>* monitor_enter_dex_pcs) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700427 StackHandleScope<2> hs(Thread::Current());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700428 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache()));
429 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader()));
Mathieu Chartiere401d142015-04-22 13:56:20 -0700430 MethodVerifier verifier(hs.Self(), m->GetDexFile(), dex_cache, class_loader, &m->GetClassDef(),
431 m->GetCodeItem(), m->GetDexMethodIndex(), m, m->GetAccessFlags(),
Mathieu Chartier4306ef82014-12-19 18:41:47 -0800432 false, true, false, false);
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700433 verifier.interesting_dex_pc_ = dex_pc;
Ian Rogers46960fe2014-05-23 10:43:43 -0700434 verifier.monitor_enter_dex_pcs_ = monitor_enter_dex_pcs;
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700435 verifier.FindLocksAtDexPc();
436}
437
Andreas Gampecb3c08f2014-09-18 13:16:38 -0700438static bool HasMonitorEnterInstructions(const DexFile::CodeItem* const code_item) {
439 const Instruction* inst = Instruction::At(code_item->insns_);
440
441 uint32_t insns_size = code_item->insns_size_in_code_units_;
442 for (uint32_t dex_pc = 0; dex_pc < insns_size;) {
443 if (inst->Opcode() == Instruction::MONITOR_ENTER) {
444 return true;
445 }
446
447 dex_pc += inst->SizeInCodeUnits();
448 inst = inst->Next();
449 }
450
451 return false;
452}
453
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700454void MethodVerifier::FindLocksAtDexPc() {
Ian Rogers7b078e82014-09-10 14:44:24 -0700455 CHECK(monitor_enter_dex_pcs_ != nullptr);
456 CHECK(code_item_ != nullptr); // This only makes sense for methods with code.
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700457
Andreas Gampecb3c08f2014-09-18 13:16:38 -0700458 // Quick check whether there are any monitor_enter instructions at all.
459 if (!HasMonitorEnterInstructions(code_item_)) {
460 return;
461 }
462
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700463 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
464 // verification. In practice, the phase we want relies on data structures set up by all the
465 // earlier passes, so we just run the full method verification and bail out early when we've
466 // got what we wanted.
467 Verify();
468}
469
Mathieu Chartiere401d142015-04-22 13:56:20 -0700470ArtField* MethodVerifier::FindAccessedFieldAtDexPc(ArtMethod* m, uint32_t dex_pc) {
471 StackHandleScope<2> hs(Thread::Current());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700472 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache()));
473 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader()));
Mathieu Chartiere401d142015-04-22 13:56:20 -0700474 MethodVerifier verifier(hs.Self(), m->GetDexFile(), dex_cache, class_loader, &m->GetClassDef(),
475 m->GetCodeItem(), m->GetDexMethodIndex(), m, m->GetAccessFlags(), true,
476 true, false, true);
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200477 return verifier.FindAccessedFieldAtDexPc(dex_pc);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200478}
479
Mathieu Chartierc7853442015-03-27 14:35:38 -0700480ArtField* MethodVerifier::FindAccessedFieldAtDexPc(uint32_t dex_pc) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700481 CHECK(code_item_ != nullptr); // This only makes sense for methods with code.
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200482
483 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
484 // verification. In practice, the phase we want relies on data structures set up by all the
485 // earlier passes, so we just run the full method verification and bail out early when we've
486 // got what we wanted.
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200487 bool success = Verify();
488 if (!success) {
Ian Rogers9bc54402014-04-17 16:40:01 -0700489 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200490 }
491 RegisterLine* register_line = reg_table_.GetLine(dex_pc);
Ian Rogers7b078e82014-09-10 14:44:24 -0700492 if (register_line == nullptr) {
Ian Rogers9bc54402014-04-17 16:40:01 -0700493 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200494 }
495 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
496 return GetQuickFieldAccess(inst, register_line);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200497}
498
Mathieu Chartiere401d142015-04-22 13:56:20 -0700499ArtMethod* MethodVerifier::FindInvokedMethodAtDexPc(ArtMethod* m, uint32_t dex_pc) {
500 StackHandleScope<2> hs(Thread::Current());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700501 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache()));
502 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader()));
Mathieu Chartiere401d142015-04-22 13:56:20 -0700503 MethodVerifier verifier(hs.Self(), m->GetDexFile(), dex_cache, class_loader, &m->GetClassDef(),
504 m->GetCodeItem(), m->GetDexMethodIndex(), m, m->GetAccessFlags(), true,
505 true, false, true);
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200506 return verifier.FindInvokedMethodAtDexPc(dex_pc);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200507}
508
Mathieu Chartiere401d142015-04-22 13:56:20 -0700509ArtMethod* MethodVerifier::FindInvokedMethodAtDexPc(uint32_t dex_pc) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700510 CHECK(code_item_ != nullptr); // This only makes sense for methods with code.
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200511
512 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
513 // verification. In practice, the phase we want relies on data structures set up by all the
514 // earlier passes, so we just run the full method verification and bail out early when we've
515 // got what we wanted.
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200516 bool success = Verify();
517 if (!success) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700518 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200519 }
520 RegisterLine* register_line = reg_table_.GetLine(dex_pc);
Ian Rogers7b078e82014-09-10 14:44:24 -0700521 if (register_line == nullptr) {
522 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200523 }
524 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
525 const bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
Mathieu Chartier091d2382015-03-06 10:59:06 -0800526 return GetQuickInvokedMethod(inst, register_line, is_range, false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200527}
528
Mathieu Chartiere401d142015-04-22 13:56:20 -0700529SafeMap<uint32_t, std::set<uint32_t>> MethodVerifier::FindStringInitMap(ArtMethod* m) {
Jeff Hao848f70a2014-01-15 13:49:50 -0800530 Thread* self = Thread::Current();
Mathieu Chartiere401d142015-04-22 13:56:20 -0700531 StackHandleScope<2> hs(self);
Jeff Hao848f70a2014-01-15 13:49:50 -0800532 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache()));
533 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader()));
Jeff Hao848f70a2014-01-15 13:49:50 -0800534 MethodVerifier verifier(self, m->GetDexFile(), dex_cache, class_loader, &m->GetClassDef(),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700535 m->GetCodeItem(), m->GetDexMethodIndex(), m, m->GetAccessFlags(),
Jeff Hao848f70a2014-01-15 13:49:50 -0800536 true, true, false, true);
537 return verifier.FindStringInitMap();
538}
539
540SafeMap<uint32_t, std::set<uint32_t>>& MethodVerifier::FindStringInitMap() {
541 Verify();
542 return GetStringInitPcRegMap();
543}
544
Ian Rogersad0b3a32012-04-16 14:50:24 -0700545bool MethodVerifier::Verify() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700546 // If there aren't any instructions, make sure that's expected, then exit successfully.
Ian Rogers7b078e82014-09-10 14:44:24 -0700547 if (code_item_ == nullptr) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700548 if ((method_access_flags_ & (kAccNative | kAccAbstract)) == 0) {
jeffhaod5347e02012-03-22 17:25:05 -0700549 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "zero-length code in concrete non-native method";
jeffhaobdb76512011-09-07 11:43:16 -0700550 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -0700551 } else {
552 return true;
jeffhaobdb76512011-09-07 11:43:16 -0700553 }
jeffhaobdb76512011-09-07 11:43:16 -0700554 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700555 // Sanity-check the register counts. ins + locals = registers, so make sure that ins <= registers.
556 if (code_item_->ins_size_ > code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700557 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad register counts (ins=" << code_item_->ins_size_
558 << " regs=" << code_item_->registers_size_;
Ian Rogersd81871c2011-10-03 13:57:23 -0700559 return false;
jeffhaobdb76512011-09-07 11:43:16 -0700560 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700561 // Allocate and initialize an array to hold instruction data.
Ian Rogers7b3ddd22013-02-21 15:19:52 -0800562 insn_flags_.reset(new InstructionFlags[code_item_->insns_size_in_code_units_]());
Ian Rogersd81871c2011-10-03 13:57:23 -0700563 // Run through the instructions and see if the width checks out.
564 bool result = ComputeWidthsAndCountOps();
565 // Flag instructions guarded by a "try" block and check exception handlers.
566 result = result && ScanTryCatchBlocks();
567 // Perform static instruction verification.
568 result = result && VerifyInstructions();
Ian Rogersad0b3a32012-04-16 14:50:24 -0700569 // Perform code-flow analysis and return.
Vladimir Marko2b5eaa22013-12-13 13:59:30 +0000570 result = result && VerifyCodeFlow();
571 // Compute information for compiler.
572 if (result && Runtime::Current()->IsCompiler()) {
573 result = Runtime::Current()->GetCompilerCallbacks()->MethodVerified(this);
574 }
575 return result;
jeffhaoba5ebb92011-08-25 17:24:37 -0700576}
577
Ian Rogers776ac1f2012-04-13 23:36:36 -0700578std::ostream& MethodVerifier::Fail(VerifyError error) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700579 switch (error) {
580 case VERIFY_ERROR_NO_CLASS:
581 case VERIFY_ERROR_NO_FIELD:
582 case VERIFY_ERROR_NO_METHOD:
583 case VERIFY_ERROR_ACCESS_CLASS:
584 case VERIFY_ERROR_ACCESS_FIELD:
585 case VERIFY_ERROR_ACCESS_METHOD:
Ian Rogers08f753d2012-08-24 14:35:25 -0700586 case VERIFY_ERROR_INSTANTIATION:
587 case VERIFY_ERROR_CLASS_CHANGE:
Igor Murashkin158f35c2015-06-10 15:55:30 -0700588 case VERIFY_ERROR_FORCE_INTERPRETER:
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800589 if (Runtime::Current()->IsAotCompiler() || !can_load_classes_) {
jeffhaofaf459e2012-08-31 15:32:47 -0700590 // If we're optimistically running verification at compile time, turn NO_xxx, ACCESS_xxx,
591 // class change and instantiation errors into soft verification errors so that we re-verify
592 // at runtime. We may fail to find or to agree on access because of not yet available class
593 // loaders, or class loaders that will differ at runtime. In these cases, we don't want to
594 // affect the soundness of the code being compiled. Instead, the generated code runs "slow
595 // paths" that dynamically perform the verification and cause the behavior to be that akin
596 // to an interpreter.
597 error = VERIFY_ERROR_BAD_CLASS_SOFT;
598 } else {
Jeff Haoa3faaf42013-09-03 19:07:00 -0700599 // If we fail again at runtime, mark that this instruction would throw and force this
600 // method to be executed using the interpreter with checks.
jeffhaofaf459e2012-08-31 15:32:47 -0700601 have_pending_runtime_throw_failure_ = true;
Andreas Gamped7f8d052015-03-12 11:05:47 -0700602
603 // We need to save the work_line if the instruction wasn't throwing before. Otherwise we'll
604 // try to merge garbage.
605 // Note: this assumes that Fail is called before we do any work_line modifications.
Andreas Gamped5ad72f2015-06-26 17:33:47 -0700606 // Note: this can fail before we touch any instruction, for the signature of a method. So
607 // add a check.
608 if (work_insn_idx_ < DexFile::kDexNoIndex) {
609 const uint16_t* insns = code_item_->insns_ + work_insn_idx_;
610 const Instruction* inst = Instruction::At(insns);
611 int opcode_flags = Instruction::FlagsOf(inst->Opcode());
Andreas Gamped7f8d052015-03-12 11:05:47 -0700612
Andreas Gamped5ad72f2015-06-26 17:33:47 -0700613 if ((opcode_flags & Instruction::kThrow) == 0 && CurrentInsnFlags()->IsInTry()) {
614 saved_line_->CopyFromLine(work_line_.get());
615 }
Andreas Gamped7f8d052015-03-12 11:05:47 -0700616 }
jeffhaofaf459e2012-08-31 15:32:47 -0700617 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700618 break;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700619 // Indication that verification should be retried at runtime.
620 case VERIFY_ERROR_BAD_CLASS_SOFT:
Jeff Haoee988952013-04-16 14:23:47 -0700621 if (!allow_soft_failures_) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700622 have_pending_hard_failure_ = true;
623 }
624 break;
jeffhaod5347e02012-03-22 17:25:05 -0700625 // Hard verification failures at compile time will still fail at runtime, so the class is
626 // marked as rejected to prevent it from being compiled.
Ian Rogersad0b3a32012-04-16 14:50:24 -0700627 case VERIFY_ERROR_BAD_CLASS_HARD: {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800628 if (Runtime::Current()->IsAotCompiler()) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700629 ClassReference ref(dex_file_, dex_file_->GetIndexForClassDef(*class_def_));
Vladimir Marko2b5eaa22013-12-13 13:59:30 +0000630 Runtime::Current()->GetCompilerCallbacks()->ClassRejected(ref);
jeffhaod1224c72012-02-29 13:43:08 -0800631 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700632 have_pending_hard_failure_ = true;
633 break;
Ian Rogers47a05882012-02-03 12:23:33 -0800634 }
635 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700636 failures_.push_back(error);
Elena Sayapina78480ec2014-08-15 15:52:42 +0700637 std::string location(StringPrintf("%s: [0x%X] ", PrettyMethod(dex_method_idx_, *dex_file_).c_str(),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700638 work_insn_idx_));
Elena Sayapina78480ec2014-08-15 15:52:42 +0700639 std::ostringstream* failure_message = new std::ostringstream(location, std::ostringstream::ate);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700640 failure_messages_.push_back(failure_message);
641 return *failure_message;
642}
643
Ian Rogers576ca0c2014-06-06 15:58:22 -0700644std::ostream& MethodVerifier::LogVerifyInfo() {
645 return info_messages_ << "VFY: " << PrettyMethod(dex_method_idx_, *dex_file_)
646 << '[' << reinterpret_cast<void*>(work_insn_idx_) << "] : ";
647}
648
Ian Rogersad0b3a32012-04-16 14:50:24 -0700649void MethodVerifier::PrependToLastFailMessage(std::string prepend) {
650 size_t failure_num = failure_messages_.size();
651 DCHECK_NE(failure_num, 0U);
652 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
653 prepend += last_fail_message->str();
Elena Sayapina78480ec2014-08-15 15:52:42 +0700654 failure_messages_[failure_num - 1] = new std::ostringstream(prepend, std::ostringstream::ate);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700655 delete last_fail_message;
656}
657
658void MethodVerifier::AppendToLastFailMessage(std::string append) {
659 size_t failure_num = failure_messages_.size();
660 DCHECK_NE(failure_num, 0U);
661 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
662 (*last_fail_message) << append;
Ian Rogers47a05882012-02-03 12:23:33 -0800663}
664
Ian Rogers776ac1f2012-04-13 23:36:36 -0700665bool MethodVerifier::ComputeWidthsAndCountOps() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700666 const uint16_t* insns = code_item_->insns_;
667 size_t insns_size = code_item_->insns_size_in_code_units_;
668 const Instruction* inst = Instruction::At(insns);
jeffhaobdb76512011-09-07 11:43:16 -0700669 size_t new_instance_count = 0;
670 size_t monitor_enter_count = 0;
Ian Rogersd81871c2011-10-03 13:57:23 -0700671 size_t dex_pc = 0;
jeffhaobdb76512011-09-07 11:43:16 -0700672
Ian Rogersd81871c2011-10-03 13:57:23 -0700673 while (dex_pc < insns_size) {
jeffhaobdb76512011-09-07 11:43:16 -0700674 Instruction::Code opcode = inst->Opcode();
Ian Rogersa9a82542013-10-04 11:17:26 -0700675 switch (opcode) {
676 case Instruction::APUT_OBJECT:
677 case Instruction::CHECK_CAST:
678 has_check_casts_ = true;
679 break;
680 case Instruction::INVOKE_VIRTUAL:
681 case Instruction::INVOKE_VIRTUAL_RANGE:
682 case Instruction::INVOKE_INTERFACE:
683 case Instruction::INVOKE_INTERFACE_RANGE:
684 has_virtual_or_interface_invokes_ = true;
685 break;
686 case Instruction::MONITOR_ENTER:
687 monitor_enter_count++;
688 break;
689 case Instruction::NEW_INSTANCE:
690 new_instance_count++;
691 break;
692 default:
693 break;
jeffhaobdb76512011-09-07 11:43:16 -0700694 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700695 size_t inst_size = inst->SizeInCodeUnits();
Ian Rogers7b078e82014-09-10 14:44:24 -0700696 insn_flags_[dex_pc].SetIsOpcode();
Ian Rogersd81871c2011-10-03 13:57:23 -0700697 dex_pc += inst_size;
Ian Rogers7b078e82014-09-10 14:44:24 -0700698 inst = inst->RelativeAt(inst_size);
jeffhaobdb76512011-09-07 11:43:16 -0700699 }
700
Ian Rogersd81871c2011-10-03 13:57:23 -0700701 if (dex_pc != insns_size) {
jeffhaod5347e02012-03-22 17:25:05 -0700702 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "code did not end where expected ("
703 << dex_pc << " vs. " << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700704 return false;
705 }
706
Ian Rogersd81871c2011-10-03 13:57:23 -0700707 new_instance_count_ = new_instance_count;
708 monitor_enter_count_ = monitor_enter_count;
jeffhaobdb76512011-09-07 11:43:16 -0700709 return true;
710}
711
Ian Rogers776ac1f2012-04-13 23:36:36 -0700712bool MethodVerifier::ScanTryCatchBlocks() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700713 uint32_t tries_size = code_item_->tries_size_;
jeffhaobdb76512011-09-07 11:43:16 -0700714 if (tries_size == 0) {
715 return true;
716 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700717 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Ian Rogers0571d352011-11-03 19:51:38 -0700718 const DexFile::TryItem* tries = DexFile::GetTryItems(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -0700719
720 for (uint32_t idx = 0; idx < tries_size; idx++) {
721 const DexFile::TryItem* try_item = &tries[idx];
722 uint32_t start = try_item->start_addr_;
723 uint32_t end = start + try_item->insn_count_;
jeffhaobdb76512011-09-07 11:43:16 -0700724 if ((start >= end) || (start >= insns_size) || (end > insns_size)) {
jeffhaod5347e02012-03-22 17:25:05 -0700725 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad exception entry: startAddr=" << start
726 << " endAddr=" << end << " (size=" << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700727 return false;
728 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700729 if (!insn_flags_[start].IsOpcode()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700730 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
731 << "'try' block starts inside an instruction (" << start << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700732 return false;
733 }
Ian Rogers7b078e82014-09-10 14:44:24 -0700734 uint32_t dex_pc = start;
735 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
736 while (dex_pc < end) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700737 insn_flags_[dex_pc].SetInTry();
Ian Rogers7b078e82014-09-10 14:44:24 -0700738 size_t insn_size = inst->SizeInCodeUnits();
739 dex_pc += insn_size;
740 inst = inst->RelativeAt(insn_size);
jeffhaobdb76512011-09-07 11:43:16 -0700741 }
742 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800743 // Iterate over each of the handlers to verify target addresses.
Ian Rogers13735952014-10-08 12:43:28 -0700744 const uint8_t* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -0700745 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700746 ClassLinker* linker = Runtime::Current()->GetClassLinker();
jeffhaobdb76512011-09-07 11:43:16 -0700747 for (uint32_t idx = 0; idx < handlers_size; idx++) {
Ian Rogers0571d352011-11-03 19:51:38 -0700748 CatchHandlerIterator iterator(handlers_ptr);
749 for (; iterator.HasNext(); iterator.Next()) {
750 uint32_t dex_pc= iterator.GetHandlerAddress();
Ian Rogersd81871c2011-10-03 13:57:23 -0700751 if (!insn_flags_[dex_pc].IsOpcode()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700752 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
753 << "exception handler starts at bad address (" << dex_pc << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700754 return false;
755 }
Stephen Kyle9bc61992014-09-22 13:53:15 +0100756 if (!CheckNotMoveResult(code_item_->insns_, dex_pc)) {
757 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
758 << "exception handler begins with move-result* (" << dex_pc << ")";
759 return false;
760 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700761 insn_flags_[dex_pc].SetBranchTarget();
Ian Rogers28ad40d2011-10-27 15:19:26 -0700762 // Ensure exception types are resolved so that they don't need resolution to be delivered,
763 // unresolved exception types will be ignored by exception delivery
Ian Rogers0571d352011-11-03 19:51:38 -0700764 if (iterator.GetHandlerTypeIndex() != DexFile::kDexNoIndex16) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800765 mirror::Class* exception_type = linker->ResolveType(*dex_file_,
766 iterator.GetHandlerTypeIndex(),
Mathieu Chartierbf99f772014-08-23 16:37:27 -0700767 dex_cache_, class_loader_);
Ian Rogers7b078e82014-09-10 14:44:24 -0700768 if (exception_type == nullptr) {
769 DCHECK(self_->IsExceptionPending());
770 self_->ClearException();
Ian Rogers28ad40d2011-10-27 15:19:26 -0700771 }
772 }
jeffhaobdb76512011-09-07 11:43:16 -0700773 }
Ian Rogers0571d352011-11-03 19:51:38 -0700774 handlers_ptr = iterator.EndDataPointer();
jeffhaobdb76512011-09-07 11:43:16 -0700775 }
jeffhaobdb76512011-09-07 11:43:16 -0700776 return true;
777}
778
Ian Rogers776ac1f2012-04-13 23:36:36 -0700779bool MethodVerifier::VerifyInstructions() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700780 const Instruction* inst = Instruction::At(code_item_->insns_);
jeffhaoba5ebb92011-08-25 17:24:37 -0700781
Ian Rogers0c7abda2012-09-19 13:33:42 -0700782 /* 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 -0700783 insn_flags_[0].SetBranchTarget();
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700784 insn_flags_[0].SetCompileTimeInfoPoint();
Ian Rogersd81871c2011-10-03 13:57:23 -0700785
786 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700787 for (uint32_t dex_pc = 0; dex_pc < insns_size;) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700788 if (!VerifyInstruction(inst, dex_pc)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700789 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -0700790 return false;
791 }
792 /* Flag instructions that are garbage collection points */
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700793 // All invoke points are marked as "Throw" points already.
794 // We are relying on this to also count all the invokes as interesting.
Vladimir Marko8b858e12014-11-27 14:52:37 +0000795 if (inst->IsBranch()) {
796 insn_flags_[dex_pc].SetCompileTimeInfoPoint();
797 // The compiler also needs safepoints for fall-through to loop heads.
798 // Such a loop head must be a target of a branch.
799 int32_t offset = 0;
800 bool cond, self_ok;
801 bool target_ok = GetBranchOffset(dex_pc, &offset, &cond, &self_ok);
802 DCHECK(target_ok);
803 insn_flags_[dex_pc + offset].SetCompileTimeInfoPoint();
804 } else if (inst->IsSwitch() || inst->IsThrow()) {
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700805 insn_flags_[dex_pc].SetCompileTimeInfoPoint();
Ian Rogersb8c78592013-07-25 23:52:52 +0000806 } else if (inst->IsReturn()) {
807 insn_flags_[dex_pc].SetCompileTimeInfoPointAndReturn();
Ian Rogersd81871c2011-10-03 13:57:23 -0700808 }
809 dex_pc += inst->SizeInCodeUnits();
810 inst = inst->Next();
811 }
812 return true;
813}
814
Ian Rogers776ac1f2012-04-13 23:36:36 -0700815bool MethodVerifier::VerifyInstruction(const Instruction* inst, uint32_t code_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700816 bool result = true;
817 switch (inst->GetVerifyTypeArgumentA()) {
818 case Instruction::kVerifyRegA:
Ian Rogers29a26482014-05-02 15:27:29 -0700819 result = result && CheckRegisterIndex(inst->VRegA());
Ian Rogersd81871c2011-10-03 13:57:23 -0700820 break;
821 case Instruction::kVerifyRegAWide:
Ian Rogers29a26482014-05-02 15:27:29 -0700822 result = result && CheckWideRegisterIndex(inst->VRegA());
Ian Rogersd81871c2011-10-03 13:57:23 -0700823 break;
824 }
825 switch (inst->GetVerifyTypeArgumentB()) {
826 case Instruction::kVerifyRegB:
Ian Rogers29a26482014-05-02 15:27:29 -0700827 result = result && CheckRegisterIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700828 break;
829 case Instruction::kVerifyRegBField:
Ian Rogers29a26482014-05-02 15:27:29 -0700830 result = result && CheckFieldIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700831 break;
832 case Instruction::kVerifyRegBMethod:
Ian Rogers29a26482014-05-02 15:27:29 -0700833 result = result && CheckMethodIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700834 break;
835 case Instruction::kVerifyRegBNewInstance:
Ian Rogers29a26482014-05-02 15:27:29 -0700836 result = result && CheckNewInstance(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700837 break;
838 case Instruction::kVerifyRegBString:
Ian Rogers29a26482014-05-02 15:27:29 -0700839 result = result && CheckStringIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700840 break;
841 case Instruction::kVerifyRegBType:
Ian Rogers29a26482014-05-02 15:27:29 -0700842 result = result && CheckTypeIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700843 break;
844 case Instruction::kVerifyRegBWide:
Ian Rogers29a26482014-05-02 15:27:29 -0700845 result = result && CheckWideRegisterIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700846 break;
847 }
848 switch (inst->GetVerifyTypeArgumentC()) {
849 case Instruction::kVerifyRegC:
Ian Rogers29a26482014-05-02 15:27:29 -0700850 result = result && CheckRegisterIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700851 break;
852 case Instruction::kVerifyRegCField:
Ian Rogers29a26482014-05-02 15:27:29 -0700853 result = result && CheckFieldIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700854 break;
855 case Instruction::kVerifyRegCNewArray:
Ian Rogers29a26482014-05-02 15:27:29 -0700856 result = result && CheckNewArray(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700857 break;
858 case Instruction::kVerifyRegCType:
Ian Rogers29a26482014-05-02 15:27:29 -0700859 result = result && CheckTypeIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700860 break;
861 case Instruction::kVerifyRegCWide:
Ian Rogers29a26482014-05-02 15:27:29 -0700862 result = result && CheckWideRegisterIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700863 break;
864 }
865 switch (inst->GetVerifyExtraFlags()) {
866 case Instruction::kVerifyArrayData:
867 result = result && CheckArrayData(code_offset);
868 break;
869 case Instruction::kVerifyBranchTarget:
870 result = result && CheckBranchTarget(code_offset);
871 break;
872 case Instruction::kVerifySwitchTargets:
873 result = result && CheckSwitchTargets(code_offset);
874 break;
Andreas Gampec3314312014-06-19 18:13:29 -0700875 case Instruction::kVerifyVarArgNonZero:
876 // Fall-through.
Ian Rogers29a26482014-05-02 15:27:29 -0700877 case Instruction::kVerifyVarArg: {
Taiju Tsuiki29498a22015-04-13 14:21:00 +0900878 // Instructions that can actually return a negative value shouldn't have this flag.
879 uint32_t v_a = dchecked_integral_cast<uint32_t>(inst->VRegA());
880 if ((inst->GetVerifyExtraFlags() == Instruction::kVerifyVarArgNonZero && v_a == 0) ||
881 v_a > Instruction::kMaxVarArgRegs) {
882 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << v_a << ") in "
Andreas Gampec3314312014-06-19 18:13:29 -0700883 "non-range invoke";
884 return false;
885 }
Taiju Tsuiki29498a22015-04-13 14:21:00 +0900886
Ian Rogers29a26482014-05-02 15:27:29 -0700887 uint32_t args[Instruction::kMaxVarArgRegs];
888 inst->GetVarArgs(args);
Taiju Tsuiki29498a22015-04-13 14:21:00 +0900889 result = result && CheckVarArgRegs(v_a, args);
Ian Rogersd81871c2011-10-03 13:57:23 -0700890 break;
Ian Rogers29a26482014-05-02 15:27:29 -0700891 }
Andreas Gampec3314312014-06-19 18:13:29 -0700892 case Instruction::kVerifyVarArgRangeNonZero:
893 // Fall-through.
Ian Rogersd81871c2011-10-03 13:57:23 -0700894 case Instruction::kVerifyVarArgRange:
Andreas Gampec3314312014-06-19 18:13:29 -0700895 if (inst->GetVerifyExtraFlags() == Instruction::kVerifyVarArgRangeNonZero &&
896 inst->VRegA() <= 0) {
897 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << inst->VRegA() << ") in "
898 "range invoke";
899 return false;
900 }
Ian Rogers29a26482014-05-02 15:27:29 -0700901 result = result && CheckVarArgRangeRegs(inst->VRegA(), inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700902 break;
903 case Instruction::kVerifyError:
jeffhaod5347e02012-03-22 17:25:05 -0700904 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected opcode " << inst->Name();
Ian Rogersd81871c2011-10-03 13:57:23 -0700905 result = false;
906 break;
907 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800908 if (inst->GetVerifyIsRuntimeOnly() && Runtime::Current()->IsAotCompiler() && !verify_to_dump_) {
Ian Rogers5fb22a92014-06-13 10:31:28 -0700909 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "opcode only expected at runtime " << inst->Name();
910 result = false;
911 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700912 return result;
913}
914
Ian Rogers7b078e82014-09-10 14:44:24 -0700915inline bool MethodVerifier::CheckRegisterIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700916 if (idx >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700917 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register index out of range (" << idx << " >= "
918 << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700919 return false;
920 }
921 return true;
922}
923
Ian Rogers7b078e82014-09-10 14:44:24 -0700924inline bool MethodVerifier::CheckWideRegisterIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700925 if (idx + 1 >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700926 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "wide register index out of range (" << idx
927 << "+1 >= " << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700928 return false;
929 }
930 return true;
931}
932
Ian Rogers7b078e82014-09-10 14:44:24 -0700933inline bool MethodVerifier::CheckFieldIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700934 if (idx >= dex_file_->GetHeader().field_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700935 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad field index " << idx << " (max "
936 << dex_file_->GetHeader().field_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700937 return false;
938 }
939 return true;
940}
941
Ian Rogers7b078e82014-09-10 14:44:24 -0700942inline bool MethodVerifier::CheckMethodIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700943 if (idx >= dex_file_->GetHeader().method_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700944 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad method index " << idx << " (max "
945 << dex_file_->GetHeader().method_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700946 return false;
947 }
948 return true;
949}
950
Ian Rogers7b078e82014-09-10 14:44:24 -0700951inline bool MethodVerifier::CheckNewInstance(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700952 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700953 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
954 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700955 return false;
956 }
957 // We don't need the actual class, just a pointer to the class name.
Ian Rogers0571d352011-11-03 19:51:38 -0700958 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -0700959 if (descriptor[0] != 'L') {
jeffhaod5347e02012-03-22 17:25:05 -0700960 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "can't call new-instance on type '" << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -0700961 return false;
962 }
963 return true;
964}
965
Ian Rogers7b078e82014-09-10 14:44:24 -0700966inline bool MethodVerifier::CheckStringIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700967 if (idx >= dex_file_->GetHeader().string_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700968 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad string index " << idx << " (max "
969 << dex_file_->GetHeader().string_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700970 return false;
971 }
972 return true;
973}
974
Ian Rogers7b078e82014-09-10 14:44:24 -0700975inline bool MethodVerifier::CheckTypeIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700976 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700977 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
978 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700979 return false;
980 }
981 return true;
982}
983
Ian Rogers776ac1f2012-04-13 23:36:36 -0700984bool MethodVerifier::CheckNewArray(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700985 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700986 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
987 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700988 return false;
989 }
990 int bracket_count = 0;
Ian Rogers0571d352011-11-03 19:51:38 -0700991 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -0700992 const char* cp = descriptor;
993 while (*cp++ == '[') {
994 bracket_count++;
995 }
996 if (bracket_count == 0) {
997 /* The given class must be an array type. */
Brian Carlstrom93c33962013-07-26 10:37:43 -0700998 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
999 << "can't new-array class '" << descriptor << "' (not an array)";
Ian Rogersd81871c2011-10-03 13:57:23 -07001000 return false;
1001 } else if (bracket_count > 255) {
1002 /* It is illegal to create an array of more than 255 dimensions. */
Brian Carlstrom93c33962013-07-26 10:37:43 -07001003 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1004 << "can't new-array class '" << descriptor << "' (exceeds limit)";
Ian Rogersd81871c2011-10-03 13:57:23 -07001005 return false;
1006 }
1007 return true;
1008}
1009
Ian Rogers776ac1f2012-04-13 23:36:36 -07001010bool MethodVerifier::CheckArrayData(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001011 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
1012 const uint16_t* insns = code_item_->insns_ + cur_offset;
1013 const uint16_t* array_data;
1014 int32_t array_data_offset;
1015
1016 DCHECK_LT(cur_offset, insn_count);
1017 /* make sure the start of the array data table is in range */
1018 array_data_offset = insns[1] | (((int32_t) insns[2]) << 16);
1019 if ((int32_t) cur_offset + array_data_offset < 0 ||
1020 cur_offset + array_data_offset + 2 >= insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -07001021 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data start: at " << cur_offset
Brian Carlstrom93c33962013-07-26 10:37:43 -07001022 << ", data offset " << array_data_offset
1023 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -07001024 return false;
1025 }
1026 /* offset to array data table is a relative branch-style offset */
1027 array_data = insns + array_data_offset;
Andreas Gampe57c47582015-07-01 22:05:59 -07001028 // Make sure the table is at an even dex pc, that is, 32-bit aligned.
1029 if (!IsAligned<4>(array_data)) {
jeffhaod5347e02012-03-22 17:25:05 -07001030 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned array data table: at " << cur_offset
1031 << ", data offset " << array_data_offset;
Ian Rogersd81871c2011-10-03 13:57:23 -07001032 return false;
1033 }
Andreas Gampe57c47582015-07-01 22:05:59 -07001034 // Make sure the array-data is marked as an opcode. This ensures that it was reached when
1035 // traversing the code item linearly. It is an approximation for a by-spec padding value.
1036 if (!insn_flags_[cur_offset + array_data_offset].IsOpcode()) {
1037 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array data table at " << cur_offset
1038 << ", data offset " << array_data_offset
1039 << " not correctly visited, probably bad padding.";
1040 return false;
1041 }
1042
Ian Rogersd81871c2011-10-03 13:57:23 -07001043 uint32_t value_width = array_data[1];
Elliott Hughes398f64b2012-03-26 18:05:48 -07001044 uint32_t value_count = *reinterpret_cast<const uint32_t*>(&array_data[2]);
Ian Rogersd81871c2011-10-03 13:57:23 -07001045 uint32_t table_size = 4 + (value_width * value_count + 1) / 2;
1046 /* make sure the end of the switch is in range */
1047 if (cur_offset + array_data_offset + table_size > insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -07001048 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data end: at " << cur_offset
1049 << ", data offset " << array_data_offset << ", end "
1050 << cur_offset + array_data_offset + table_size
1051 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -07001052 return false;
1053 }
1054 return true;
1055}
1056
Ian Rogers776ac1f2012-04-13 23:36:36 -07001057bool MethodVerifier::CheckBranchTarget(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001058 int32_t offset;
1059 bool isConditional, selfOkay;
1060 if (!GetBranchOffset(cur_offset, &offset, &isConditional, &selfOkay)) {
1061 return false;
1062 }
1063 if (!selfOkay && offset == 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001064 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch offset of zero not allowed at"
1065 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -07001066 return false;
1067 }
Elliott Hughes81ff3182012-03-23 20:35:56 -07001068 // Check for 32-bit overflow. This isn't strictly necessary if we can depend on the runtime
1069 // to have identical "wrap-around" behavior, but it's unwise to depend on that.
Ian Rogersd81871c2011-10-03 13:57:23 -07001070 if (((int64_t) cur_offset + (int64_t) offset) != (int64_t) (cur_offset + offset)) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001071 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch target overflow "
1072 << reinterpret_cast<void*>(cur_offset) << " +" << offset;
Ian Rogersd81871c2011-10-03 13:57:23 -07001073 return false;
1074 }
1075 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
1076 int32_t abs_offset = cur_offset + offset;
Brian Carlstrom93c33962013-07-26 10:37:43 -07001077 if (abs_offset < 0 ||
1078 (uint32_t) abs_offset >= insn_count ||
1079 !insn_flags_[abs_offset].IsOpcode()) {
jeffhaod5347e02012-03-22 17:25:05 -07001080 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid branch target " << offset << " (-> "
Elliott Hughes398f64b2012-03-26 18:05:48 -07001081 << reinterpret_cast<void*>(abs_offset) << ") at "
1082 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -07001083 return false;
1084 }
1085 insn_flags_[abs_offset].SetBranchTarget();
1086 return true;
1087}
1088
Ian Rogers776ac1f2012-04-13 23:36:36 -07001089bool MethodVerifier::GetBranchOffset(uint32_t cur_offset, int32_t* pOffset, bool* pConditional,
Ian Rogersd81871c2011-10-03 13:57:23 -07001090 bool* selfOkay) {
1091 const uint16_t* insns = code_item_->insns_ + cur_offset;
1092 *pConditional = false;
1093 *selfOkay = false;
jeffhaoba5ebb92011-08-25 17:24:37 -07001094 switch (*insns & 0xff) {
1095 case Instruction::GOTO:
1096 *pOffset = ((int16_t) *insns) >> 8;
jeffhaoba5ebb92011-08-25 17:24:37 -07001097 break;
1098 case Instruction::GOTO_32:
1099 *pOffset = insns[1] | (((uint32_t) insns[2]) << 16);
jeffhaoba5ebb92011-08-25 17:24:37 -07001100 *selfOkay = true;
1101 break;
1102 case Instruction::GOTO_16:
1103 *pOffset = (int16_t) insns[1];
jeffhaoba5ebb92011-08-25 17:24:37 -07001104 break;
1105 case Instruction::IF_EQ:
1106 case Instruction::IF_NE:
1107 case Instruction::IF_LT:
1108 case Instruction::IF_GE:
1109 case Instruction::IF_GT:
1110 case Instruction::IF_LE:
1111 case Instruction::IF_EQZ:
1112 case Instruction::IF_NEZ:
1113 case Instruction::IF_LTZ:
1114 case Instruction::IF_GEZ:
1115 case Instruction::IF_GTZ:
1116 case Instruction::IF_LEZ:
1117 *pOffset = (int16_t) insns[1];
1118 *pConditional = true;
jeffhaoba5ebb92011-08-25 17:24:37 -07001119 break;
1120 default:
1121 return false;
jeffhaoba5ebb92011-08-25 17:24:37 -07001122 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001123 return true;
1124}
1125
Ian Rogers776ac1f2012-04-13 23:36:36 -07001126bool MethodVerifier::CheckSwitchTargets(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001127 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001128 DCHECK_LT(cur_offset, insn_count);
Ian Rogersd81871c2011-10-03 13:57:23 -07001129 const uint16_t* insns = code_item_->insns_ + cur_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001130 /* make sure the start of the switch is in range */
Ian Rogersd81871c2011-10-03 13:57:23 -07001131 int32_t switch_offset = insns[1] | ((int32_t) insns[2]) << 16;
Jeff Hao9ccd1512015-03-20 18:11:45 -07001132 if ((int32_t) cur_offset + switch_offset < 0 || cur_offset + switch_offset + 2 > insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -07001133 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch start: at " << cur_offset
Brian Carlstrom93c33962013-07-26 10:37:43 -07001134 << ", switch offset " << switch_offset
1135 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001136 return false;
1137 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001138 /* offset to switch table is a relative branch-style offset */
Ian Rogersd81871c2011-10-03 13:57:23 -07001139 const uint16_t* switch_insns = insns + switch_offset;
Andreas Gampe57c47582015-07-01 22:05:59 -07001140 // Make sure the table is at an even dex pc, that is, 32-bit aligned.
1141 if (!IsAligned<4>(switch_insns)) {
jeffhaod5347e02012-03-22 17:25:05 -07001142 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned switch table: at " << cur_offset
1143 << ", switch offset " << switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001144 return false;
1145 }
Andreas Gampe57c47582015-07-01 22:05:59 -07001146 // Make sure the switch data is marked as an opcode. This ensures that it was reached when
1147 // traversing the code item linearly. It is an approximation for a by-spec padding value.
1148 if (!insn_flags_[cur_offset + switch_offset].IsOpcode()) {
1149 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "switch table at " << cur_offset
1150 << ", switch offset " << switch_offset
1151 << " not correctly visited, probably bad padding.";
1152 return false;
1153 }
1154
Ian Rogersd81871c2011-10-03 13:57:23 -07001155 uint32_t switch_count = switch_insns[1];
1156 int32_t keys_offset, targets_offset;
1157 uint16_t expected_signature;
jeffhaoba5ebb92011-08-25 17:24:37 -07001158 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
1159 /* 0=sig, 1=count, 2/3=firstKey */
1160 targets_offset = 4;
1161 keys_offset = -1;
1162 expected_signature = Instruction::kPackedSwitchSignature;
1163 } else {
1164 /* 0=sig, 1=count, 2..count*2 = keys */
1165 keys_offset = 2;
1166 targets_offset = 2 + 2 * switch_count;
1167 expected_signature = Instruction::kSparseSwitchSignature;
1168 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001169 uint32_t table_size = targets_offset + switch_count * 2;
jeffhaoba5ebb92011-08-25 17:24:37 -07001170 if (switch_insns[0] != expected_signature) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001171 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1172 << StringPrintf("wrong signature for switch table (%x, wanted %x)",
1173 switch_insns[0], expected_signature);
jeffhaoba5ebb92011-08-25 17:24:37 -07001174 return false;
1175 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001176 /* make sure the end of the switch is in range */
1177 if (cur_offset + switch_offset + table_size > (uint32_t) insn_count) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001178 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch end: at " << cur_offset
1179 << ", switch offset " << switch_offset
1180 << ", end " << (cur_offset + switch_offset + table_size)
jeffhaod5347e02012-03-22 17:25:05 -07001181 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001182 return false;
1183 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001184 /* for a sparse switch, verify the keys are in ascending order */
1185 if (keys_offset > 0 && switch_count > 1) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001186 int32_t last_key = switch_insns[keys_offset] | (switch_insns[keys_offset + 1] << 16);
1187 for (uint32_t targ = 1; targ < switch_count; targ++) {
jeffhaoba5ebb92011-08-25 17:24:37 -07001188 int32_t key = (int32_t) switch_insns[keys_offset + targ * 2] |
1189 (int32_t) (switch_insns[keys_offset + targ * 2 + 1] << 16);
1190 if (key <= last_key) {
jeffhaod5347e02012-03-22 17:25:05 -07001191 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid packed switch: last key=" << last_key
1192 << ", this=" << key;
jeffhaoba5ebb92011-08-25 17:24:37 -07001193 return false;
1194 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001195 last_key = key;
1196 }
1197 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001198 /* verify each switch target */
Ian Rogersd81871c2011-10-03 13:57:23 -07001199 for (uint32_t targ = 0; targ < switch_count; targ++) {
1200 int32_t offset = (int32_t) switch_insns[targets_offset + targ * 2] |
1201 (int32_t) (switch_insns[targets_offset + targ * 2 + 1] << 16);
1202 int32_t abs_offset = cur_offset + offset;
Brian Carlstrom93c33962013-07-26 10:37:43 -07001203 if (abs_offset < 0 ||
1204 abs_offset >= (int32_t) insn_count ||
1205 !insn_flags_[abs_offset].IsOpcode()) {
1206 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch target " << offset
1207 << " (-> " << reinterpret_cast<void*>(abs_offset) << ") at "
1208 << reinterpret_cast<void*>(cur_offset)
1209 << "[" << targ << "]";
jeffhaoba5ebb92011-08-25 17:24:37 -07001210 return false;
1211 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001212 insn_flags_[abs_offset].SetBranchTarget();
1213 }
1214 return true;
1215}
1216
Ian Rogers776ac1f2012-04-13 23:36:36 -07001217bool MethodVerifier::CheckVarArgRegs(uint32_t vA, uint32_t arg[]) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001218 uint16_t registers_size = code_item_->registers_size_;
1219 for (uint32_t idx = 0; idx < vA; idx++) {
jeffhao457cc512012-02-02 16:55:13 -08001220 if (arg[idx] >= registers_size) {
jeffhaod5347e02012-03-22 17:25:05 -07001221 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index (" << arg[idx]
1222 << ") in non-range invoke (>= " << registers_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001223 return false;
1224 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001225 }
1226
1227 return true;
1228}
1229
Ian Rogers776ac1f2012-04-13 23:36:36 -07001230bool MethodVerifier::CheckVarArgRangeRegs(uint32_t vA, uint32_t vC) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001231 uint16_t registers_size = code_item_->registers_size_;
1232 // vA/vC are unsigned 8-bit/16-bit quantities for /range instructions, so there's no risk of
1233 // integer overflow when adding them here.
1234 if (vA + vC > registers_size) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001235 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index " << vA << "+" << vC
1236 << " in range invoke (> " << registers_size << ")";
jeffhaoba5ebb92011-08-25 17:24:37 -07001237 return false;
1238 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001239 return true;
1240}
1241
Ian Rogers776ac1f2012-04-13 23:36:36 -07001242bool MethodVerifier::VerifyCodeFlow() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001243 uint16_t registers_size = code_item_->registers_size_;
1244 uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaobdb76512011-09-07 11:43:16 -07001245
Ian Rogersd81871c2011-10-03 13:57:23 -07001246 /* Create and initialize table holding register status */
Brian Carlstrom93c33962013-07-26 10:37:43 -07001247 reg_table_.Init(kTrackCompilerInterestPoints,
1248 insn_flags_.get(),
1249 insns_size,
1250 registers_size,
1251 this);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07001252
jeffhaobdb76512011-09-07 11:43:16 -07001253
Ian Rogersd0fbd852013-09-24 18:17:04 -07001254 work_line_.reset(RegisterLine::Create(registers_size, this));
1255 saved_line_.reset(RegisterLine::Create(registers_size, this));
jeffhaobdb76512011-09-07 11:43:16 -07001256
Ian Rogersd81871c2011-10-03 13:57:23 -07001257 /* Initialize register types of method arguments. */
1258 if (!SetTypesFromSignature()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001259 DCHECK_NE(failures_.size(), 0U);
1260 std::string prepend("Bad signature in ");
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001261 prepend += PrettyMethod(dex_method_idx_, *dex_file_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001262 PrependToLastFailMessage(prepend);
Ian Rogersd81871c2011-10-03 13:57:23 -07001263 return false;
1264 }
Andreas Gamped5ad72f2015-06-26 17:33:47 -07001265 // We may have a runtime failure here, clear.
1266 have_pending_runtime_throw_failure_ = false;
1267
Ian Rogersd81871c2011-10-03 13:57:23 -07001268 /* Perform code flow verification. */
1269 if (!CodeFlowVerifyMethod()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001270 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -07001271 return false;
jeffhaobdb76512011-09-07 11:43:16 -07001272 }
jeffhaobdb76512011-09-07 11:43:16 -07001273 return true;
1274}
1275
Ian Rogersad0b3a32012-04-16 14:50:24 -07001276std::ostream& MethodVerifier::DumpFailures(std::ostream& os) {
1277 DCHECK_EQ(failures_.size(), failure_messages_.size());
Jeff Hao4137f482013-11-22 11:44:57 -08001278 for (size_t i = 0; i < failures_.size(); ++i) {
1279 os << failure_messages_[i]->str() << "\n";
Ian Rogersad0b3a32012-04-16 14:50:24 -07001280 }
1281 return os;
1282}
1283
Ian Rogers776ac1f2012-04-13 23:36:36 -07001284void MethodVerifier::Dump(std::ostream& os) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001285 VariableIndentationOutputStream vios(&os);
1286 Dump(&vios);
1287}
1288
1289void MethodVerifier::Dump(VariableIndentationOutputStream* vios) {
Ian Rogers7b078e82014-09-10 14:44:24 -07001290 if (code_item_ == nullptr) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001291 vios->Stream() << "Native method\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001292 return;
jeffhaobdb76512011-09-07 11:43:16 -07001293 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001294 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001295 vios->Stream() << "Register Types:\n";
1296 ScopedIndentation indent1(vios);
1297 reg_types_.Dump(vios->Stream());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001298 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001299 vios->Stream() << "Dumping instructions and register lines:\n";
1300 ScopedIndentation indent1(vios);
Ian Rogersd81871c2011-10-03 13:57:23 -07001301 const Instruction* inst = Instruction::At(code_item_->insns_);
1302 for (size_t dex_pc = 0; dex_pc < code_item_->insns_size_in_code_units_;
Ian Rogers7b078e82014-09-10 14:44:24 -07001303 dex_pc += inst->SizeInCodeUnits()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001304 RegisterLine* reg_line = reg_table_.GetLine(dex_pc);
Ian Rogers7b078e82014-09-10 14:44:24 -07001305 if (reg_line != nullptr) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001306 vios->Stream() << reg_line->Dump(this) << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07001307 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001308 vios->Stream()
1309 << StringPrintf("0x%04zx", dex_pc) << ": " << insn_flags_[dex_pc].ToString() << " ";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001310 const bool kDumpHexOfInstruction = false;
1311 if (kDumpHexOfInstruction) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001312 vios->Stream() << inst->DumpHex(5) << " ";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001313 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001314 vios->Stream() << inst->DumpString(dex_file_) << "\n";
jeffhaoba5ebb92011-08-25 17:24:37 -07001315 inst = inst->Next();
1316 }
jeffhaobdb76512011-09-07 11:43:16 -07001317}
1318
Ian Rogersd81871c2011-10-03 13:57:23 -07001319static bool IsPrimitiveDescriptor(char descriptor) {
1320 switch (descriptor) {
jeffhaobdb76512011-09-07 11:43:16 -07001321 case 'I':
1322 case 'C':
1323 case 'S':
1324 case 'B':
1325 case 'Z':
jeffhaobdb76512011-09-07 11:43:16 -07001326 case 'F':
1327 case 'D':
1328 case 'J':
Ian Rogersd81871c2011-10-03 13:57:23 -07001329 return true;
jeffhaobdb76512011-09-07 11:43:16 -07001330 default:
1331 return false;
1332 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001333}
1334
Ian Rogers776ac1f2012-04-13 23:36:36 -07001335bool MethodVerifier::SetTypesFromSignature() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001336 RegisterLine* reg_line = reg_table_.GetLine(0);
Andreas Gampeef0b1a12015-06-19 20:37:46 -07001337
1338 // Should have been verified earlier.
1339 DCHECK_GE(code_item_->registers_size_, code_item_->ins_size_);
1340
1341 uint32_t arg_start = code_item_->registers_size_ - code_item_->ins_size_;
Ian Rogersd81871c2011-10-03 13:57:23 -07001342 size_t expected_args = code_item_->ins_size_; /* long/double count as two */
jeffhaobdb76512011-09-07 11:43:16 -07001343
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001344 // Include the "this" pointer.
Ian Rogersd81871c2011-10-03 13:57:23 -07001345 size_t cur_arg = 0;
Ian Rogersad0b3a32012-04-16 14:50:24 -07001346 if (!IsStatic()) {
Andreas Gampeef0b1a12015-06-19 20:37:46 -07001347 if (expected_args == 0) {
1348 // Expect at least a receiver.
1349 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected 0 args, but method is not static";
1350 return false;
1351 }
1352
Ian Rogersd81871c2011-10-03 13:57:23 -07001353 // If this is a constructor for a class other than java.lang.Object, mark the first ("this")
1354 // argument as uninitialized. This restricts field access until the superclass constructor is
1355 // called.
Ian Rogersd8f69b02014-09-10 21:43:52 +00001356 const RegType& declaring_class = GetDeclaringClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07001357 if (IsConstructor() && !declaring_class.IsJavaLangObject()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07001358 reg_line->SetRegisterType(this, arg_start + cur_arg,
Ian Rogersd81871c2011-10-03 13:57:23 -07001359 reg_types_.UninitializedThisArgument(declaring_class));
1360 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07001361 reg_line->SetRegisterType(this, arg_start + cur_arg, declaring_class);
jeffhaobdb76512011-09-07 11:43:16 -07001362 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001363 cur_arg++;
jeffhaobdb76512011-09-07 11:43:16 -07001364 }
1365
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001366 const DexFile::ProtoId& proto_id =
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001367 dex_file_->GetMethodPrototype(dex_file_->GetMethodId(dex_method_idx_));
Ian Rogers0571d352011-11-03 19:51:38 -07001368 DexFileParameterIterator iterator(*dex_file_, proto_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07001369
1370 for (; iterator.HasNext(); iterator.Next()) {
1371 const char* descriptor = iterator.GetDescriptor();
Ian Rogers7b078e82014-09-10 14:44:24 -07001372 if (descriptor == nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001373 LOG(FATAL) << "Null descriptor";
1374 }
1375 if (cur_arg >= expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07001376 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1377 << " args, found more (" << descriptor << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001378 return false;
1379 }
1380 switch (descriptor[0]) {
1381 case 'L':
1382 case '[':
1383 // We assume that reference arguments are initialized. The only way it could be otherwise
1384 // (assuming the caller was verified) is if the current method is <init>, but in that case
1385 // it's effectively considered initialized the instant we reach here (in the sense that we
1386 // can return without doing anything or call virtual methods).
1387 {
Ian Rogersd8f69b02014-09-10 21:43:52 +00001388 const RegType& reg_type = ResolveClassAndCheckAccess(iterator.GetTypeIdx());
Sebastien Hertz2ed76f92014-04-22 17:11:08 +02001389 if (!reg_type.IsNonZeroReferenceTypes()) {
1390 DCHECK(HasFailures());
1391 return false;
1392 }
Ian Rogers7b078e82014-09-10 14:44:24 -07001393 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001394 }
1395 break;
1396 case 'Z':
Ian Rogers7b078e82014-09-10 14:44:24 -07001397 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_types_.Boolean());
Ian Rogersd81871c2011-10-03 13:57:23 -07001398 break;
1399 case 'C':
Ian Rogers7b078e82014-09-10 14:44:24 -07001400 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_types_.Char());
Ian Rogersd81871c2011-10-03 13:57:23 -07001401 break;
1402 case 'B':
Ian Rogers7b078e82014-09-10 14:44:24 -07001403 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_types_.Byte());
Ian Rogersd81871c2011-10-03 13:57:23 -07001404 break;
1405 case 'I':
Ian Rogers7b078e82014-09-10 14:44:24 -07001406 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_types_.Integer());
Ian Rogersd81871c2011-10-03 13:57:23 -07001407 break;
1408 case 'S':
Ian Rogers7b078e82014-09-10 14:44:24 -07001409 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_types_.Short());
Ian Rogersd81871c2011-10-03 13:57:23 -07001410 break;
1411 case 'F':
Ian Rogers7b078e82014-09-10 14:44:24 -07001412 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_types_.Float());
Ian Rogersd81871c2011-10-03 13:57:23 -07001413 break;
1414 case 'J':
1415 case 'D': {
Andreas Gampe77cd4d62014-06-19 17:29:48 -07001416 if (cur_arg + 1 >= expected_args) {
1417 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1418 << " args, found more (" << descriptor << ")";
1419 return false;
1420 }
1421
Ian Rogers7b078e82014-09-10 14:44:24 -07001422 const RegType* lo_half;
1423 const RegType* hi_half;
1424 if (descriptor[0] == 'J') {
1425 lo_half = &reg_types_.LongLo();
1426 hi_half = &reg_types_.LongHi();
1427 } else {
1428 lo_half = &reg_types_.DoubleLo();
1429 hi_half = &reg_types_.DoubleHi();
1430 }
1431 reg_line->SetRegisterTypeWide(this, arg_start + cur_arg, *lo_half, *hi_half);
Ian Rogersd81871c2011-10-03 13:57:23 -07001432 cur_arg++;
1433 break;
1434 }
1435 default:
Brian Carlstrom93c33962013-07-26 10:37:43 -07001436 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected signature type char '"
1437 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001438 return false;
1439 }
1440 cur_arg++;
1441 }
1442 if (cur_arg != expected_args) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001443 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1444 << " arguments, found " << cur_arg;
Ian Rogersd81871c2011-10-03 13:57:23 -07001445 return false;
1446 }
1447 const char* descriptor = dex_file_->GetReturnTypeDescriptor(proto_id);
1448 // Validate return type. We don't do the type lookup; just want to make sure that it has the right
1449 // format. Only major difference from the method argument format is that 'V' is supported.
1450 bool result;
1451 if (IsPrimitiveDescriptor(descriptor[0]) || descriptor[0] == 'V') {
1452 result = descriptor[1] == '\0';
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001453 } else if (descriptor[0] == '[') { // single/multi-dimensional array of object/primitive
Ian Rogersd81871c2011-10-03 13:57:23 -07001454 size_t i = 0;
1455 do {
1456 i++;
1457 } while (descriptor[i] == '['); // process leading [
1458 if (descriptor[i] == 'L') { // object array
1459 do {
1460 i++; // find closing ;
1461 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1462 result = descriptor[i] == ';';
1463 } else { // primitive array
1464 result = IsPrimitiveDescriptor(descriptor[i]) && descriptor[i + 1] == '\0';
1465 }
1466 } else if (descriptor[0] == 'L') {
1467 // could be more thorough here, but shouldn't be required
1468 size_t i = 0;
1469 do {
1470 i++;
1471 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1472 result = descriptor[i] == ';';
1473 } else {
1474 result = false;
1475 }
1476 if (!result) {
jeffhaod5347e02012-03-22 17:25:05 -07001477 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected char in return type descriptor '"
1478 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001479 }
1480 return result;
jeffhaobdb76512011-09-07 11:43:16 -07001481}
1482
Ian Rogers776ac1f2012-04-13 23:36:36 -07001483bool MethodVerifier::CodeFlowVerifyMethod() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001484 const uint16_t* insns = code_item_->insns_;
1485 const uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaoba5ebb92011-08-25 17:24:37 -07001486
jeffhaobdb76512011-09-07 11:43:16 -07001487 /* Begin by marking the first instruction as "changed". */
Ian Rogersd81871c2011-10-03 13:57:23 -07001488 insn_flags_[0].SetChanged();
1489 uint32_t start_guess = 0;
jeffhaoba5ebb92011-08-25 17:24:37 -07001490
jeffhaobdb76512011-09-07 11:43:16 -07001491 /* Continue until no instructions are marked "changed". */
1492 while (true) {
Mathieu Chartier4306ef82014-12-19 18:41:47 -08001493 if (allow_thread_suspension_) {
1494 self_->AllowThreadSuspension();
1495 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001496 // Find the first marked one. Use "start_guess" as a way to find one quickly.
1497 uint32_t insn_idx = start_guess;
1498 for (; insn_idx < insns_size; insn_idx++) {
1499 if (insn_flags_[insn_idx].IsChanged())
jeffhaobdb76512011-09-07 11:43:16 -07001500 break;
1501 }
jeffhaobdb76512011-09-07 11:43:16 -07001502 if (insn_idx == insns_size) {
1503 if (start_guess != 0) {
1504 /* try again, starting from the top */
1505 start_guess = 0;
1506 continue;
1507 } else {
1508 /* all flags are clear */
1509 break;
1510 }
1511 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001512 // We carry the working set of registers from instruction to instruction. If this address can
1513 // be the target of a branch (or throw) instruction, or if we're skipping around chasing
1514 // "changed" flags, we need to load the set of registers from the table.
1515 // Because we always prefer to continue on to the next instruction, we should never have a
1516 // situation where we have a stray "changed" flag set on an instruction that isn't a branch
1517 // target.
1518 work_insn_idx_ = insn_idx;
1519 if (insn_flags_[insn_idx].IsBranchTarget()) {
1520 work_line_->CopyFromLine(reg_table_.GetLine(insn_idx));
Ian Rogersebbdd872014-07-07 23:53:08 -07001521 } else if (kIsDebugBuild) {
jeffhaobdb76512011-09-07 11:43:16 -07001522 /*
1523 * Sanity check: retrieve the stored register line (assuming
1524 * a full table) and make sure it actually matches.
1525 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001526 RegisterLine* register_line = reg_table_.GetLine(insn_idx);
Ian Rogers7b078e82014-09-10 14:44:24 -07001527 if (register_line != nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001528 if (work_line_->CompareLine(register_line) != 0) {
1529 Dump(std::cout);
1530 std::cout << info_messages_.str();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001531 LOG(FATAL) << "work_line diverged in " << PrettyMethod(dex_method_idx_, *dex_file_)
Elliott Hughesc073b072012-05-24 19:29:17 -07001532 << "@" << reinterpret_cast<void*>(work_insn_idx_) << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07001533 << " work_line=" << work_line_->Dump(this) << "\n"
1534 << " expected=" << register_line->Dump(this);
Ian Rogersd81871c2011-10-03 13:57:23 -07001535 }
jeffhaobdb76512011-09-07 11:43:16 -07001536 }
jeffhaobdb76512011-09-07 11:43:16 -07001537 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001538 if (!CodeFlowVerifyInstruction(&start_guess)) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001539 std::string prepend(PrettyMethod(dex_method_idx_, *dex_file_));
Ian Rogersad0b3a32012-04-16 14:50:24 -07001540 prepend += " failed to verify: ";
1541 PrependToLastFailMessage(prepend);
jeffhaoba5ebb92011-08-25 17:24:37 -07001542 return false;
1543 }
jeffhaobdb76512011-09-07 11:43:16 -07001544 /* Clear "changed" and mark as visited. */
Ian Rogersd81871c2011-10-03 13:57:23 -07001545 insn_flags_[insn_idx].SetVisited();
1546 insn_flags_[insn_idx].ClearChanged();
jeffhaobdb76512011-09-07 11:43:16 -07001547 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001548
Ian Rogers1c849e52012-06-28 14:00:33 -07001549 if (gDebugVerify) {
jeffhaobdb76512011-09-07 11:43:16 -07001550 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001551 * Scan for dead code. There's nothing "evil" about dead code
jeffhaobdb76512011-09-07 11:43:16 -07001552 * (besides the wasted space), but it indicates a flaw somewhere
1553 * down the line, possibly in the verifier.
1554 *
1555 * If we've substituted "always throw" instructions into the stream,
1556 * we are almost certainly going to have some dead code.
1557 */
1558 int dead_start = -1;
Ian Rogersd81871c2011-10-03 13:57:23 -07001559 uint32_t insn_idx = 0;
Ian Rogers7b078e82014-09-10 14:44:24 -07001560 for (; insn_idx < insns_size;
1561 insn_idx += Instruction::At(code_item_->insns_ + insn_idx)->SizeInCodeUnits()) {
jeffhaobdb76512011-09-07 11:43:16 -07001562 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001563 * Switch-statement data doesn't get "visited" by scanner. It
jeffhaobdb76512011-09-07 11:43:16 -07001564 * may or may not be preceded by a padding NOP (for alignment).
1565 */
1566 if (insns[insn_idx] == Instruction::kPackedSwitchSignature ||
1567 insns[insn_idx] == Instruction::kSparseSwitchSignature ||
1568 insns[insn_idx] == Instruction::kArrayDataSignature ||
Elliott Hughes380aaa72012-07-09 14:33:15 -07001569 (insns[insn_idx] == Instruction::NOP && (insn_idx + 1 < insns_size) &&
jeffhaobdb76512011-09-07 11:43:16 -07001570 (insns[insn_idx + 1] == Instruction::kPackedSwitchSignature ||
1571 insns[insn_idx + 1] == Instruction::kSparseSwitchSignature ||
1572 insns[insn_idx + 1] == Instruction::kArrayDataSignature))) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001573 insn_flags_[insn_idx].SetVisited();
jeffhaobdb76512011-09-07 11:43:16 -07001574 }
1575
Ian Rogersd81871c2011-10-03 13:57:23 -07001576 if (!insn_flags_[insn_idx].IsVisited()) {
jeffhaobdb76512011-09-07 11:43:16 -07001577 if (dead_start < 0)
1578 dead_start = insn_idx;
1579 } else if (dead_start >= 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001580 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start)
1581 << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaobdb76512011-09-07 11:43:16 -07001582 dead_start = -1;
1583 }
1584 }
1585 if (dead_start >= 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001586 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start)
1587 << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaoba5ebb92011-08-25 17:24:37 -07001588 }
Ian Rogersc9e463c2013-06-05 16:52:26 -07001589 // To dump the state of the verify after a method, do something like:
1590 // if (PrettyMethod(dex_method_idx_, *dex_file_) ==
1591 // "boolean java.lang.String.equals(java.lang.Object)") {
1592 // LOG(INFO) << info_messages_.str();
1593 // }
jeffhaoba5ebb92011-08-25 17:24:37 -07001594 }
jeffhaobdb76512011-09-07 11:43:16 -07001595 return true;
1596}
1597
Andreas Gampe68df3202015-06-22 11:35:46 -07001598// Returns the index of the first final instance field of the given class, or kDexNoIndex if there
1599// is no such field.
1600static uint32_t GetFirstFinalInstanceFieldIndex(const DexFile& dex_file, uint16_t type_idx) {
1601 const DexFile::ClassDef* class_def = dex_file.FindClassDef(type_idx);
1602 DCHECK(class_def != nullptr);
1603 const uint8_t* class_data = dex_file.GetClassData(*class_def);
1604 DCHECK(class_data != nullptr);
1605 ClassDataItemIterator it(dex_file, class_data);
1606 // Skip static fields.
1607 while (it.HasNextStaticField()) {
1608 it.Next();
1609 }
1610 while (it.HasNextInstanceField()) {
1611 if ((it.GetFieldAccessFlags() & kAccFinal) != 0) {
1612 return it.GetMemberIndex();
1613 }
1614 it.Next();
1615 }
1616 return DexFile::kDexNoIndex;
1617}
1618
Ian Rogers776ac1f2012-04-13 23:36:36 -07001619bool MethodVerifier::CodeFlowVerifyInstruction(uint32_t* start_guess) {
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001620 // If we're doing FindLocksAtDexPc, check whether we're at the dex pc we care about.
1621 // We want the state _before_ the instruction, for the case where the dex pc we're
1622 // interested in is itself a monitor-enter instruction (which is a likely place
1623 // for a thread to be suspended).
Ian Rogers7b078e82014-09-10 14:44:24 -07001624 if (monitor_enter_dex_pcs_ != nullptr && work_insn_idx_ == interesting_dex_pc_) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001625 monitor_enter_dex_pcs_->clear(); // The new work line is more accurate than the previous one.
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001626 for (size_t i = 0; i < work_line_->GetMonitorEnterCount(); ++i) {
1627 monitor_enter_dex_pcs_->push_back(work_line_->GetMonitorEnterDexPc(i));
1628 }
1629 }
1630
jeffhaobdb76512011-09-07 11:43:16 -07001631 /*
1632 * Once we finish decoding the instruction, we need to figure out where
jeffhaod1f0fde2011-09-08 17:25:33 -07001633 * we can go from here. There are three possible ways to transfer
jeffhaobdb76512011-09-07 11:43:16 -07001634 * control to another statement:
1635 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001636 * (1) Continue to the next instruction. Applies to all but
jeffhaobdb76512011-09-07 11:43:16 -07001637 * unconditional branches, method returns, and exception throws.
jeffhaod1f0fde2011-09-08 17:25:33 -07001638 * (2) Branch to one or more possible locations. Applies to branches
jeffhaobdb76512011-09-07 11:43:16 -07001639 * and switch statements.
jeffhaod1f0fde2011-09-08 17:25:33 -07001640 * (3) Exception handlers. Applies to any instruction that can
jeffhaobdb76512011-09-07 11:43:16 -07001641 * throw an exception that is handled by an encompassing "try"
1642 * block.
1643 *
1644 * We can also return, in which case there is no successor instruction
1645 * from this point.
1646 *
Elliott Hughesadb8c672012-03-06 16:49:32 -08001647 * The behavior can be determined from the opcode flags.
jeffhaobdb76512011-09-07 11:43:16 -07001648 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001649 const uint16_t* insns = code_item_->insns_ + work_insn_idx_;
1650 const Instruction* inst = Instruction::At(insns);
Ian Rogersa75a0132012-09-28 11:41:42 -07001651 int opcode_flags = Instruction::FlagsOf(inst->Opcode());
jeffhaobdb76512011-09-07 11:43:16 -07001652
jeffhaobdb76512011-09-07 11:43:16 -07001653 int32_t branch_target = 0;
jeffhaobdb76512011-09-07 11:43:16 -07001654 bool just_set_result = false;
Ian Rogers2c8a8572011-10-24 17:11:36 -07001655 if (gDebugVerify) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001656 // Generate processing back trace to debug verifier
Elliott Hughesc073b072012-05-24 19:29:17 -07001657 LogVerifyInfo() << "Processing " << inst->DumpString(dex_file_) << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07001658 << work_line_->Dump(this) << "\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001659 }
jeffhaobdb76512011-09-07 11:43:16 -07001660
1661 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001662 * Make a copy of the previous register state. If the instruction
jeffhaobdb76512011-09-07 11:43:16 -07001663 * can throw an exception, we will copy/merge this into the "catch"
1664 * address rather than work_line, because we don't want the result
1665 * from the "successful" code path (e.g. a check-cast that "improves"
1666 * a type) to be visible to the exception handler.
1667 */
Ian Rogers776ac1f2012-04-13 23:36:36 -07001668 if ((opcode_flags & Instruction::kThrow) != 0 && CurrentInsnFlags()->IsInTry()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001669 saved_line_->CopyFromLine(work_line_.get());
Ian Rogers1ff3c982014-08-12 02:30:58 -07001670 } else if (kIsDebugBuild) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001671 saved_line_->FillWithGarbage();
jeffhaobdb76512011-09-07 11:43:16 -07001672 }
Andreas Gamped12e7822015-06-25 10:26:40 -07001673 DCHECK(!have_pending_runtime_throw_failure_); // Per-instruction flag, should not be set here.
jeffhaobdb76512011-09-07 11:43:16 -07001674
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07001675
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001676 // We need to ensure the work line is consistent while performing validation. When we spot a
1677 // peephole pattern we compute a new line for either the fallthrough instruction or the
1678 // branch target.
Ian Rogers700a4022014-05-19 16:49:03 -07001679 std::unique_ptr<RegisterLine> branch_line;
1680 std::unique_ptr<RegisterLine> fallthrough_line;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001681
Stephen Kyle7e541c92014-12-17 17:10:02 +00001682 /*
1683 * If we are in a constructor, and we currently have an UninitializedThis type
1684 * in a register somewhere, we need to make sure it isn't overwritten.
1685 */
1686 bool track_uninitialized_this = false;
1687 size_t uninitialized_this_loc = 0;
1688 if (IsConstructor()) {
1689 track_uninitialized_this = work_line_->GetUninitializedThisLoc(this, &uninitialized_this_loc);
1690 }
1691
Sebastien Hertz5243e912013-05-21 10:55:07 +02001692 switch (inst->Opcode()) {
jeffhaobdb76512011-09-07 11:43:16 -07001693 case Instruction::NOP:
1694 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001695 * A "pure" NOP has no effect on anything. Data tables start with
jeffhaobdb76512011-09-07 11:43:16 -07001696 * a signature that looks like a NOP; if we see one of these in
1697 * the course of executing code then we have a problem.
1698 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001699 if (inst->VRegA_10x() != 0) {
jeffhaod5347e02012-03-22 17:25:05 -07001700 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "encountered data table in instruction stream";
jeffhaobdb76512011-09-07 11:43:16 -07001701 }
1702 break;
1703
1704 case Instruction::MOVE:
Ian Rogers7b078e82014-09-10 14:44:24 -07001705 work_line_->CopyRegister1(this, inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategory1nr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001706 break;
jeffhaobdb76512011-09-07 11:43:16 -07001707 case Instruction::MOVE_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001708 work_line_->CopyRegister1(this, inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategory1nr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001709 break;
jeffhaobdb76512011-09-07 11:43:16 -07001710 case Instruction::MOVE_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001711 work_line_->CopyRegister1(this, inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategory1nr);
jeffhaobdb76512011-09-07 11:43:16 -07001712 break;
1713 case Instruction::MOVE_WIDE:
Ian Rogers7b078e82014-09-10 14:44:24 -07001714 work_line_->CopyRegister2(this, inst->VRegA_12x(), inst->VRegB_12x());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001715 break;
jeffhaobdb76512011-09-07 11:43:16 -07001716 case Instruction::MOVE_WIDE_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001717 work_line_->CopyRegister2(this, inst->VRegA_22x(), inst->VRegB_22x());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001718 break;
jeffhaobdb76512011-09-07 11:43:16 -07001719 case Instruction::MOVE_WIDE_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001720 work_line_->CopyRegister2(this, inst->VRegA_32x(), inst->VRegB_32x());
jeffhaobdb76512011-09-07 11:43:16 -07001721 break;
1722 case Instruction::MOVE_OBJECT:
Ian Rogers7b078e82014-09-10 14:44:24 -07001723 work_line_->CopyRegister1(this, inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategoryRef);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001724 break;
jeffhaobdb76512011-09-07 11:43:16 -07001725 case Instruction::MOVE_OBJECT_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001726 work_line_->CopyRegister1(this, inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategoryRef);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001727 break;
jeffhaobdb76512011-09-07 11:43:16 -07001728 case Instruction::MOVE_OBJECT_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001729 work_line_->CopyRegister1(this, inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategoryRef);
jeffhaobdb76512011-09-07 11:43:16 -07001730 break;
1731
1732 /*
1733 * The move-result instructions copy data out of a "pseudo-register"
jeffhaod1f0fde2011-09-08 17:25:33 -07001734 * with the results from the last method invocation. In practice we
jeffhaobdb76512011-09-07 11:43:16 -07001735 * might want to hold the result in an actual CPU register, so the
1736 * Dalvik spec requires that these only appear immediately after an
1737 * invoke or filled-new-array.
1738 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001739 * These calls invalidate the "result" register. (This is now
jeffhaobdb76512011-09-07 11:43:16 -07001740 * redundant with the reset done below, but it can make the debug info
1741 * easier to read in some cases.)
1742 */
1743 case Instruction::MOVE_RESULT:
Ian Rogers7b078e82014-09-10 14:44:24 -07001744 work_line_->CopyResultRegister1(this, inst->VRegA_11x(), false);
jeffhaobdb76512011-09-07 11:43:16 -07001745 break;
1746 case Instruction::MOVE_RESULT_WIDE:
Ian Rogers7b078e82014-09-10 14:44:24 -07001747 work_line_->CopyResultRegister2(this, inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07001748 break;
1749 case Instruction::MOVE_RESULT_OBJECT:
Ian Rogers7b078e82014-09-10 14:44:24 -07001750 work_line_->CopyResultRegister1(this, inst->VRegA_11x(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001751 break;
1752
Ian Rogersd81871c2011-10-03 13:57:23 -07001753 case Instruction::MOVE_EXCEPTION: {
Sebastien Hertz270a0e12015-01-16 19:49:09 +01001754 // We do not allow MOVE_EXCEPTION as the first instruction in a method. This is a simple case
1755 // where one entrypoint to the catch block is not actually an exception path.
1756 if (work_insn_idx_ == 0) {
1757 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "move-exception at pc 0x0";
1758 break;
1759 }
jeffhaobdb76512011-09-07 11:43:16 -07001760 /*
jeffhao60f83e32012-02-13 17:16:30 -08001761 * This statement can only appear as the first instruction in an exception handler. We verify
1762 * that as part of extracting the exception type from the catch block list.
jeffhaobdb76512011-09-07 11:43:16 -07001763 */
Ian Rogersd8f69b02014-09-10 21:43:52 +00001764 const RegType& res_type = GetCaughtExceptionType();
Ian Rogers7b078e82014-09-10 14:44:24 -07001765 work_line_->SetRegisterType(this, inst->VRegA_11x(), res_type);
jeffhaobdb76512011-09-07 11:43:16 -07001766 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001767 }
jeffhaobdb76512011-09-07 11:43:16 -07001768 case Instruction::RETURN_VOID:
Ian Rogers7b078e82014-09-10 14:44:24 -07001769 if (!IsConstructor() || work_line_->CheckConstructorReturn(this)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001770 if (!GetMethodReturnType().IsConflict()) {
jeffhaod5347e02012-03-22 17:25:05 -07001771 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001772 }
jeffhaobdb76512011-09-07 11:43:16 -07001773 }
1774 break;
1775 case Instruction::RETURN:
Ian Rogers7b078e82014-09-10 14:44:24 -07001776 if (!IsConstructor() || work_line_->CheckConstructorReturn(this)) {
jeffhaobdb76512011-09-07 11:43:16 -07001777 /* check the method signature */
Ian Rogersd8f69b02014-09-10 21:43:52 +00001778 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07001779 if (!return_type.IsCategory1Types()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001780 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected non-category 1 return type "
1781 << return_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001782 } else {
1783 // Compilers may generate synthetic functions that write byte values into boolean fields.
1784 // Also, it may use integer values for boolean, byte, short, and character return types.
Sebastien Hertz5243e912013-05-21 10:55:07 +02001785 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07001786 const RegType& src_type = work_line_->GetRegisterType(this, vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07001787 bool use_src = ((return_type.IsBoolean() && src_type.IsByte()) ||
1788 ((return_type.IsBoolean() || return_type.IsByte() ||
1789 return_type.IsShort() || return_type.IsChar()) &&
1790 src_type.IsInteger()));
1791 /* check the register contents */
Ian Rogersad0b3a32012-04-16 14:50:24 -07001792 bool success =
Ian Rogers7b078e82014-09-10 14:44:24 -07001793 work_line_->VerifyRegisterType(this, vregA, use_src ? src_type : return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001794 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001795 AppendToLastFailMessage(StringPrintf(" return-1nr on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07001796 }
jeffhaobdb76512011-09-07 11:43:16 -07001797 }
1798 }
1799 break;
1800 case Instruction::RETURN_WIDE:
Ian Rogers7b078e82014-09-10 14:44:24 -07001801 if (!IsConstructor() || work_line_->CheckConstructorReturn(this)) {
jeffhaobdb76512011-09-07 11:43:16 -07001802 /* check the method signature */
Ian Rogersd8f69b02014-09-10 21:43:52 +00001803 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07001804 if (!return_type.IsCategory2Types()) {
jeffhaod5347e02012-03-22 17:25:05 -07001805 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-wide not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001806 } else {
1807 /* check the register contents */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001808 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07001809 bool success = work_line_->VerifyRegisterType(this, vregA, return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001810 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001811 AppendToLastFailMessage(StringPrintf(" return-wide on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07001812 }
jeffhaobdb76512011-09-07 11:43:16 -07001813 }
1814 }
1815 break;
1816 case Instruction::RETURN_OBJECT:
Ian Rogers7b078e82014-09-10 14:44:24 -07001817 if (!IsConstructor() || work_line_->CheckConstructorReturn(this)) {
Ian Rogersd8f69b02014-09-10 21:43:52 +00001818 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07001819 if (!return_type.IsReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001820 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-object not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001821 } else {
1822 /* return_type is the *expected* return type, not register value */
1823 DCHECK(!return_type.IsZero());
1824 DCHECK(!return_type.IsUninitializedReference());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001825 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07001826 const RegType& reg_type = work_line_->GetRegisterType(this, vregA);
Andreas Gampea32210c2015-06-24 10:26:13 -07001827 // Disallow returning undefined, conflict & uninitialized values and verify that the
1828 // reference in vAA is an instance of the "return_type."
1829 if (reg_type.IsUndefined()) {
1830 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning undefined register";
1831 } else if (reg_type.IsConflict()) {
1832 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning register with conflict";
1833 } else if (reg_type.IsUninitializedTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001834 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "returning uninitialized object '"
1835 << reg_type << "'";
Ian Rogers9074b992011-10-26 17:41:55 -07001836 } else if (!return_type.IsAssignableFrom(reg_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07001837 if (reg_type.IsUnresolvedTypes() || return_type.IsUnresolvedTypes()) {
1838 Fail(VERIFY_ERROR_NO_CLASS) << " can't resolve returned type '" << return_type
1839 << "' or '" << reg_type << "'";
1840 } else {
Andreas Gampe16f149c2015-03-23 10:10:20 -07001841 bool soft_error = false;
1842 // Check whether arrays are involved. They will show a valid class status, even
1843 // if their components are erroneous.
1844 if (reg_type.IsArrayTypes() && return_type.IsArrayTypes()) {
1845 return_type.CanAssignArray(reg_type, reg_types_, class_loader_, &soft_error);
1846 if (soft_error) {
1847 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "array with erroneous component type: "
1848 << reg_type << " vs " << return_type;
1849 }
1850 }
1851
1852 if (!soft_error) {
1853 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning '" << reg_type
1854 << "', but expected from declaration '" << return_type << "'";
1855 }
Jeff Haoa3faaf42013-09-03 19:07:00 -07001856 }
jeffhaobdb76512011-09-07 11:43:16 -07001857 }
1858 }
1859 }
1860 break;
1861
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001862 /* could be boolean, int, float, or a null reference */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001863 case Instruction::CONST_4: {
1864 int32_t val = static_cast<int32_t>(inst->VRegB_11n() << 28) >> 28;
Ian Rogers7b078e82014-09-10 14:44:24 -07001865 work_line_->SetRegisterType(this, inst->VRegA_11n(),
Ian Rogers46960fe2014-05-23 10:43:43 -07001866 DetermineCat1Constant(val, need_precise_constants_));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001867 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02001868 }
1869 case Instruction::CONST_16: {
1870 int16_t val = static_cast<int16_t>(inst->VRegB_21s());
Ian Rogers7b078e82014-09-10 14:44:24 -07001871 work_line_->SetRegisterType(this, inst->VRegA_21s(),
Ian Rogers46960fe2014-05-23 10:43:43 -07001872 DetermineCat1Constant(val, need_precise_constants_));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001873 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02001874 }
Sebastien Hertz849600b2013-12-20 10:28:08 +01001875 case Instruction::CONST: {
1876 int32_t val = inst->VRegB_31i();
Ian Rogers7b078e82014-09-10 14:44:24 -07001877 work_line_->SetRegisterType(this, inst->VRegA_31i(),
Ian Rogers46960fe2014-05-23 10:43:43 -07001878 DetermineCat1Constant(val, need_precise_constants_));
jeffhaobdb76512011-09-07 11:43:16 -07001879 break;
Sebastien Hertz849600b2013-12-20 10:28:08 +01001880 }
1881 case Instruction::CONST_HIGH16: {
1882 int32_t val = static_cast<int32_t>(inst->VRegB_21h() << 16);
Ian Rogers7b078e82014-09-10 14:44:24 -07001883 work_line_->SetRegisterType(this, inst->VRegA_21h(),
Ian Rogers46960fe2014-05-23 10:43:43 -07001884 DetermineCat1Constant(val, need_precise_constants_));
jeffhaobdb76512011-09-07 11:43:16 -07001885 break;
Sebastien Hertz849600b2013-12-20 10:28:08 +01001886 }
jeffhaobdb76512011-09-07 11:43:16 -07001887 /* could be long or double; resolved upon use */
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001888 case Instruction::CONST_WIDE_16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001889 int64_t val = static_cast<int16_t>(inst->VRegB_21s());
Ian Rogersd8f69b02014-09-10 21:43:52 +00001890 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1891 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07001892 work_line_->SetRegisterTypeWide(this, inst->VRegA_21s(), lo, hi);
jeffhaobdb76512011-09-07 11:43:16 -07001893 break;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001894 }
1895 case Instruction::CONST_WIDE_32: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001896 int64_t val = static_cast<int32_t>(inst->VRegB_31i());
Ian Rogersd8f69b02014-09-10 21:43:52 +00001897 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1898 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07001899 work_line_->SetRegisterTypeWide(this, inst->VRegA_31i(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001900 break;
1901 }
1902 case Instruction::CONST_WIDE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001903 int64_t val = inst->VRegB_51l();
Ian Rogersd8f69b02014-09-10 21:43:52 +00001904 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1905 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07001906 work_line_->SetRegisterTypeWide(this, inst->VRegA_51l(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001907 break;
1908 }
1909 case Instruction::CONST_WIDE_HIGH16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001910 int64_t val = static_cast<uint64_t>(inst->VRegB_21h()) << 48;
Ian Rogersd8f69b02014-09-10 21:43:52 +00001911 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1912 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07001913 work_line_->SetRegisterTypeWide(this, inst->VRegA_21h(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001914 break;
1915 }
jeffhaobdb76512011-09-07 11:43:16 -07001916 case Instruction::CONST_STRING:
Ian Rogers7b078e82014-09-10 14:44:24 -07001917 work_line_->SetRegisterType(this, inst->VRegA_21c(), reg_types_.JavaLangString());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001918 break;
jeffhaobdb76512011-09-07 11:43:16 -07001919 case Instruction::CONST_STRING_JUMBO:
Ian Rogers7b078e82014-09-10 14:44:24 -07001920 work_line_->SetRegisterType(this, inst->VRegA_31c(), reg_types_.JavaLangString());
jeffhaobdb76512011-09-07 11:43:16 -07001921 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001922 case Instruction::CONST_CLASS: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001923 // Get type from instruction if unresolved then we need an access check
1924 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Ian Rogersd8f69b02014-09-10 21:43:52 +00001925 const RegType& res_type = ResolveClassAndCheckAccess(inst->VRegB_21c());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001926 // Register holds class, ie its type is class, on error it will hold Conflict.
Ian Rogers7b078e82014-09-10 14:44:24 -07001927 work_line_->SetRegisterType(this, inst->VRegA_21c(),
Ian Rogersb4903572012-10-11 11:52:56 -07001928 res_type.IsConflict() ? res_type
Ian Rogers7b078e82014-09-10 14:44:24 -07001929 : reg_types_.JavaLangClass());
jeffhaobdb76512011-09-07 11:43:16 -07001930 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001931 }
jeffhaobdb76512011-09-07 11:43:16 -07001932 case Instruction::MONITOR_ENTER:
Ian Rogers7b078e82014-09-10 14:44:24 -07001933 work_line_->PushMonitor(this, inst->VRegA_11x(), work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07001934 break;
1935 case Instruction::MONITOR_EXIT:
1936 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001937 * monitor-exit instructions are odd. They can throw exceptions,
jeffhaobdb76512011-09-07 11:43:16 -07001938 * but when they do they act as if they succeeded and the PC is
jeffhaod1f0fde2011-09-08 17:25:33 -07001939 * pointing to the following instruction. (This behavior goes back
jeffhaobdb76512011-09-07 11:43:16 -07001940 * to the need to handle asynchronous exceptions, a now-deprecated
1941 * feature that Dalvik doesn't support.)
1942 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001943 * In practice we don't need to worry about this. The only
jeffhaobdb76512011-09-07 11:43:16 -07001944 * exceptions that can be thrown from monitor-exit are for a
jeffhaod1f0fde2011-09-08 17:25:33 -07001945 * null reference and -exit without a matching -enter. If the
jeffhaobdb76512011-09-07 11:43:16 -07001946 * structured locking checks are working, the former would have
1947 * failed on the -enter instruction, and the latter is impossible.
1948 *
1949 * This is fortunate, because issue 3221411 prevents us from
1950 * chasing the "can throw" path when monitor verification is
jeffhaod1f0fde2011-09-08 17:25:33 -07001951 * enabled. If we can fully verify the locking we can ignore
jeffhaobdb76512011-09-07 11:43:16 -07001952 * some catch blocks (which will show up as "dead" code when
1953 * we skip them here); if we can't, then the code path could be
1954 * "live" so we still need to check it.
1955 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001956 opcode_flags &= ~Instruction::kThrow;
Ian Rogers7b078e82014-09-10 14:44:24 -07001957 work_line_->PopMonitor(this, inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07001958 break;
1959
Ian Rogers28ad40d2011-10-27 15:19:26 -07001960 case Instruction::CHECK_CAST:
Ian Rogersd81871c2011-10-03 13:57:23 -07001961 case Instruction::INSTANCE_OF: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001962 /*
1963 * If this instruction succeeds, we will "downcast" register vA to the type in vB. (This
1964 * could be a "upcast" -- not expected, so we don't try to address it.)
1965 *
1966 * If it fails, an exception is thrown, which we deal with later by ignoring the update to
Elliott Hughesadb8c672012-03-06 16:49:32 -08001967 * dec_insn.vA when branching to a handler.
Ian Rogers28ad40d2011-10-27 15:19:26 -07001968 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001969 const bool is_checkcast = (inst->Opcode() == Instruction::CHECK_CAST);
1970 const uint32_t type_idx = (is_checkcast) ? inst->VRegB_21c() : inst->VRegC_22c();
Ian Rogersd8f69b02014-09-10 21:43:52 +00001971 const RegType& res_type = ResolveClassAndCheckAccess(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001972 if (res_type.IsConflict()) {
Andreas Gampe00633eb2014-07-17 16:13:35 -07001973 // If this is a primitive type, fail HARD.
Mathieu Chartierbf99f772014-08-23 16:37:27 -07001974 mirror::Class* klass = dex_cache_->GetResolvedType(type_idx);
Andreas Gampe00633eb2014-07-17 16:13:35 -07001975 if (klass != nullptr && klass->IsPrimitive()) {
1976 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "using primitive type "
1977 << dex_file_->StringByTypeIdx(type_idx) << " in instanceof in "
1978 << GetDeclaringClass();
1979 break;
1980 }
1981
Ian Rogersad0b3a32012-04-16 14:50:24 -07001982 DCHECK_NE(failures_.size(), 0U);
1983 if (!is_checkcast) {
Ian Rogers7b078e82014-09-10 14:44:24 -07001984 work_line_->SetRegisterType(this, inst->VRegA_22c(), reg_types_.Boolean());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001985 }
1986 break; // bad class
Ian Rogers9f1ab122011-12-12 08:52:43 -08001987 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001988 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Sebastien Hertz5243e912013-05-21 10:55:07 +02001989 uint32_t orig_type_reg = (is_checkcast) ? inst->VRegA_21c() : inst->VRegB_22c();
Ian Rogers7b078e82014-09-10 14:44:24 -07001990 const RegType& orig_type = work_line_->GetRegisterType(this, orig_type_reg);
Ian Rogers28ad40d2011-10-27 15:19:26 -07001991 if (!res_type.IsNonZeroReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001992 if (is_checkcast) {
1993 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on unexpected class " << res_type;
1994 } else {
1995 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on unexpected class " << res_type;
1996 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001997 } else if (!orig_type.IsReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001998 if (is_checkcast) {
1999 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on non-reference in v" << orig_type_reg;
2000 } else {
2001 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on non-reference in v" << orig_type_reg;
2002 }
jeffhao2a8a90e2011-09-26 14:25:31 -07002003 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002004 if (is_checkcast) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002005 work_line_->SetRegisterType(this, inst->VRegA_21c(), res_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07002006 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07002007 work_line_->SetRegisterType(this, inst->VRegA_22c(), reg_types_.Boolean());
jeffhaobdb76512011-09-07 11:43:16 -07002008 }
jeffhaobdb76512011-09-07 11:43:16 -07002009 }
jeffhao2a8a90e2011-09-26 14:25:31 -07002010 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002011 }
2012 case Instruction::ARRAY_LENGTH: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002013 const RegType& res_type = work_line_->GetRegisterType(this, inst->VRegB_12x());
Ian Rogers28ad40d2011-10-27 15:19:26 -07002014 if (res_type.IsReferenceTypes()) {
Ian Rogers89310de2012-02-01 13:47:30 -08002015 if (!res_type.IsArrayTypes() && !res_type.IsZero()) { // ie not an array or null
jeffhaod5347e02012-03-22 17:25:05 -07002016 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002017 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07002018 work_line_->SetRegisterType(this, inst->VRegA_12x(), reg_types_.Integer());
Ian Rogersd81871c2011-10-03 13:57:23 -07002019 }
Andreas Gampe65c9db82014-07-28 13:14:34 -07002020 } else {
2021 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002022 }
2023 break;
2024 }
2025 case Instruction::NEW_INSTANCE: {
Ian Rogersd8f69b02014-09-10 21:43:52 +00002026 const RegType& res_type = ResolveClassAndCheckAccess(inst->VRegB_21c());
Ian Rogersad0b3a32012-04-16 14:50:24 -07002027 if (res_type.IsConflict()) {
2028 DCHECK_NE(failures_.size(), 0U);
2029 break; // bad class
jeffhao8cd6dda2012-02-22 10:15:34 -08002030 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002031 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
2032 // can't create an instance of an interface or abstract class */
2033 if (!res_type.IsInstantiableTypes()) {
2034 Fail(VERIFY_ERROR_INSTANTIATION)
2035 << "new-instance on primitive, interface or abstract class" << res_type;
Ian Rogers08f753d2012-08-24 14:35:25 -07002036 // Soft failure so carry on to set register type.
Ian Rogersd81871c2011-10-03 13:57:23 -07002037 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00002038 const RegType& uninit_type = reg_types_.Uninitialized(res_type, work_insn_idx_);
Ian Rogers08f753d2012-08-24 14:35:25 -07002039 // Any registers holding previous allocations from this address that have not yet been
2040 // initialized must be marked invalid.
Ian Rogers7b078e82014-09-10 14:44:24 -07002041 work_line_->MarkUninitRefsAsInvalid(this, uninit_type);
Ian Rogers08f753d2012-08-24 14:35:25 -07002042 // add the new uninitialized reference to the register state
Ian Rogers7b078e82014-09-10 14:44:24 -07002043 work_line_->SetRegisterType(this, inst->VRegA_21c(), uninit_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07002044 break;
2045 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08002046 case Instruction::NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002047 VerifyNewArray(inst, false, false);
jeffhaobdb76512011-09-07 11:43:16 -07002048 break;
2049 case Instruction::FILLED_NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002050 VerifyNewArray(inst, true, false);
Ian Rogers0c4a5062012-02-03 15:18:59 -08002051 just_set_result = true; // Filled new array sets result register
jeffhaobdb76512011-09-07 11:43:16 -07002052 break;
Ian Rogers0c4a5062012-02-03 15:18:59 -08002053 case Instruction::FILLED_NEW_ARRAY_RANGE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002054 VerifyNewArray(inst, true, true);
Ian Rogers0c4a5062012-02-03 15:18:59 -08002055 just_set_result = true; // Filled new array range sets result register
2056 break;
jeffhaobdb76512011-09-07 11:43:16 -07002057 case Instruction::CMPL_FLOAT:
2058 case Instruction::CMPG_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002059 if (!work_line_->VerifyRegisterType(this, inst->VRegB_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08002060 break;
2061 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002062 if (!work_line_->VerifyRegisterType(this, inst->VRegC_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08002063 break;
2064 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002065 work_line_->SetRegisterType(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002066 break;
2067 case Instruction::CMPL_DOUBLE:
2068 case Instruction::CMPG_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002069 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegB_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002070 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002071 break;
2072 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002073 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegC_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002074 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002075 break;
2076 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002077 work_line_->SetRegisterType(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002078 break;
2079 case Instruction::CMP_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002080 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegB_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002081 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002082 break;
2083 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002084 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegC_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002085 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002086 break;
2087 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002088 work_line_->SetRegisterType(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002089 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002090 case Instruction::THROW: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002091 const RegType& res_type = work_line_->GetRegisterType(this, inst->VRegA_11x());
Ian Rogersb4903572012-10-11 11:52:56 -07002092 if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(res_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07002093 Fail(res_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS : VERIFY_ERROR_BAD_CLASS_SOFT)
2094 << "thrown class " << res_type << " not instanceof Throwable";
jeffhaobdb76512011-09-07 11:43:16 -07002095 }
2096 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002097 }
jeffhaobdb76512011-09-07 11:43:16 -07002098 case Instruction::GOTO:
2099 case Instruction::GOTO_16:
2100 case Instruction::GOTO_32:
2101 /* no effect on or use of registers */
2102 break;
2103
2104 case Instruction::PACKED_SWITCH:
2105 case Instruction::SPARSE_SWITCH:
2106 /* verify that vAA is an integer, or can be converted to one */
Ian Rogers7b078e82014-09-10 14:44:24 -07002107 work_line_->VerifyRegisterType(this, inst->VRegA_31t(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002108 break;
2109
Ian Rogersd81871c2011-10-03 13:57:23 -07002110 case Instruction::FILL_ARRAY_DATA: {
2111 /* Similar to the verification done for APUT */
Ian Rogers7b078e82014-09-10 14:44:24 -07002112 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegA_31t());
Ian Rogers89310de2012-02-01 13:47:30 -08002113 /* array_type can be null if the reg type is Zero */
2114 if (!array_type.IsZero()) {
jeffhao457cc512012-02-02 16:55:13 -08002115 if (!array_type.IsArrayTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002116 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with array type "
2117 << array_type;
Ian Rogers89310de2012-02-01 13:47:30 -08002118 } else {
Ian Rogersd8f69b02014-09-10 21:43:52 +00002119 const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
Ian Rogersad0b3a32012-04-16 14:50:24 -07002120 DCHECK(!component_type.IsConflict());
jeffhao457cc512012-02-02 16:55:13 -08002121 if (component_type.IsNonZeroReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002122 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with component type "
2123 << component_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002124 } else {
jeffhao457cc512012-02-02 16:55:13 -08002125 // Now verify if the element width in the table matches the element width declared in
2126 // the array
2127 const uint16_t* array_data = insns + (insns[1] | (((int32_t) insns[2]) << 16));
2128 if (array_data[0] != Instruction::kArrayDataSignature) {
jeffhaod5347e02012-03-22 17:25:05 -07002129 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid magic for array-data";
jeffhao457cc512012-02-02 16:55:13 -08002130 } else {
2131 size_t elem_width = Primitive::ComponentSize(component_type.GetPrimitiveType());
2132 // Since we don't compress the data in Dex, expect to see equal width of data stored
2133 // in the table and expected from the array class.
2134 if (array_data[1] != elem_width) {
jeffhaod5347e02012-03-22 17:25:05 -07002135 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-data size mismatch (" << array_data[1]
2136 << " vs " << elem_width << ")";
jeffhao457cc512012-02-02 16:55:13 -08002137 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002138 }
2139 }
jeffhaobdb76512011-09-07 11:43:16 -07002140 }
2141 }
2142 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002143 }
jeffhaobdb76512011-09-07 11:43:16 -07002144 case Instruction::IF_EQ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002145 case Instruction::IF_NE: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002146 const RegType& reg_type1 = work_line_->GetRegisterType(this, inst->VRegA_22t());
2147 const RegType& reg_type2 = work_line_->GetRegisterType(this, inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002148 bool mismatch = false;
2149 if (reg_type1.IsZero()) { // zero then integral or reference expected
2150 mismatch = !reg_type2.IsReferenceTypes() && !reg_type2.IsIntegralTypes();
2151 } else if (reg_type1.IsReferenceTypes()) { // both references?
2152 mismatch = !reg_type2.IsReferenceTypes();
2153 } else { // both integral?
2154 mismatch = !reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes();
2155 }
2156 if (mismatch) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002157 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to if-eq/if-ne (" << reg_type1 << ","
2158 << reg_type2 << ") must both be references or integral";
jeffhaobdb76512011-09-07 11:43:16 -07002159 }
2160 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002161 }
jeffhaobdb76512011-09-07 11:43:16 -07002162 case Instruction::IF_LT:
2163 case Instruction::IF_GE:
2164 case Instruction::IF_GT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002165 case Instruction::IF_LE: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002166 const RegType& reg_type1 = work_line_->GetRegisterType(this, inst->VRegA_22t());
2167 const RegType& reg_type2 = work_line_->GetRegisterType(this, inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002168 if (!reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002169 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to 'if' (" << reg_type1 << ","
2170 << reg_type2 << ") must be integral";
jeffhaobdb76512011-09-07 11:43:16 -07002171 }
2172 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002173 }
jeffhaobdb76512011-09-07 11:43:16 -07002174 case Instruction::IF_EQZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002175 case Instruction::IF_NEZ: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002176 const RegType& reg_type = work_line_->GetRegisterType(this, inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002177 if (!reg_type.IsReferenceTypes() && !reg_type.IsIntegralTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002178 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
2179 << " unexpected as arg to if-eqz/if-nez";
Ian Rogersd81871c2011-10-03 13:57:23 -07002180 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002181
2182 // Find previous instruction - its existence is a precondition to peephole optimization.
Ian Rogers9b360392013-06-06 14:45:07 -07002183 uint32_t instance_of_idx = 0;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002184 if (0 != work_insn_idx_) {
Ian Rogers9b360392013-06-06 14:45:07 -07002185 instance_of_idx = work_insn_idx_ - 1;
Brian Carlstromdf629502013-07-17 22:39:56 -07002186 while (0 != instance_of_idx && !insn_flags_[instance_of_idx].IsOpcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07002187 instance_of_idx--;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002188 }
Andreas Gampe7c038102014-10-27 20:08:46 -07002189 if (FailOrAbort(this, insn_flags_[instance_of_idx].IsOpcode(),
2190 "Unable to get previous instruction of if-eqz/if-nez for work index ",
2191 work_insn_idx_)) {
2192 break;
2193 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002194 } else {
2195 break;
2196 }
2197
Ian Rogers9b360392013-06-06 14:45:07 -07002198 const Instruction* instance_of_inst = Instruction::At(code_item_->insns_ + instance_of_idx);
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002199
2200 /* Check for peep-hole pattern of:
2201 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002202 * instance-of vX, vY, T;
2203 * ifXXX vX, label ;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002204 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002205 * label:
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002206 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002207 * and sharpen the type of vY to be type T.
2208 * Note, this pattern can't be if:
2209 * - if there are other branches to this branch,
2210 * - when vX == vY.
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002211 */
Ian Rogersfae370a2013-06-05 08:33:27 -07002212 if (!CurrentInsnFlags()->IsBranchTarget() &&
Ian Rogers9b360392013-06-06 14:45:07 -07002213 (Instruction::INSTANCE_OF == instance_of_inst->Opcode()) &&
2214 (inst->VRegA_21t() == instance_of_inst->VRegA_22c()) &&
2215 (instance_of_inst->VRegA_22c() != instance_of_inst->VRegB_22c())) {
Ian Rogersebbdd872014-07-07 23:53:08 -07002216 // Check the type of the instance-of is different than that of registers type, as if they
2217 // are the same there is no work to be done here. Check that the conversion is not to or
2218 // from an unresolved type as type information is imprecise. If the instance-of is to an
2219 // interface then ignore the type information as interfaces can only be treated as Objects
2220 // and we don't want to disallow field and other operations on the object. If the value
2221 // being instance-of checked against is known null (zero) then allow the optimization as
2222 // we didn't have type information. If the merge of the instance-of type with the original
2223 // type is assignable to the original then allow optimization. This check is performed to
2224 // ensure that subsequent merges don't lose type information - such as becoming an
2225 // interface from a class that would lose information relevant to field checks.
Ian Rogers7b078e82014-09-10 14:44:24 -07002226 const RegType& orig_type = work_line_->GetRegisterType(this, instance_of_inst->VRegB_22c());
Ian Rogersd8f69b02014-09-10 21:43:52 +00002227 const RegType& cast_type = ResolveClassAndCheckAccess(instance_of_inst->VRegC_22c());
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002228
Ian Rogersebbdd872014-07-07 23:53:08 -07002229 if (!orig_type.Equals(cast_type) &&
2230 !cast_type.IsUnresolvedTypes() && !orig_type.IsUnresolvedTypes() &&
Andreas Gampe00633eb2014-07-17 16:13:35 -07002231 cast_type.HasClass() && // Could be conflict type, make sure it has a class.
Ian Rogersebbdd872014-07-07 23:53:08 -07002232 !cast_type.GetClass()->IsInterface() &&
2233 (orig_type.IsZero() ||
2234 orig_type.IsStrictlyAssignableFrom(cast_type.Merge(orig_type, &reg_types_)))) {
Ian Rogersd0fbd852013-09-24 18:17:04 -07002235 RegisterLine* update_line = RegisterLine::Create(code_item_->registers_size_, this);
Ian Rogersfae370a2013-06-05 08:33:27 -07002236 if (inst->Opcode() == Instruction::IF_EQZ) {
Ian Rogers9b360392013-06-06 14:45:07 -07002237 fallthrough_line.reset(update_line);
Ian Rogersfae370a2013-06-05 08:33:27 -07002238 } else {
Ian Rogers9b360392013-06-06 14:45:07 -07002239 branch_line.reset(update_line);
2240 }
2241 update_line->CopyFromLine(work_line_.get());
Ian Rogers7b078e82014-09-10 14:44:24 -07002242 update_line->SetRegisterType(this, instance_of_inst->VRegB_22c(), cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002243 if (!insn_flags_[instance_of_idx].IsBranchTarget() && 0 != instance_of_idx) {
2244 // See if instance-of was preceded by a move-object operation, common due to the small
2245 // register encoding space of instance-of, and propagate type information to the source
2246 // of the move-object.
2247 uint32_t move_idx = instance_of_idx - 1;
Brian Carlstromdf629502013-07-17 22:39:56 -07002248 while (0 != move_idx && !insn_flags_[move_idx].IsOpcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07002249 move_idx--;
2250 }
Andreas Gampe7c038102014-10-27 20:08:46 -07002251 if (FailOrAbort(this, insn_flags_[move_idx].IsOpcode(),
2252 "Unable to get previous instruction of if-eqz/if-nez for work index ",
2253 work_insn_idx_)) {
2254 break;
2255 }
Ian Rogers9b360392013-06-06 14:45:07 -07002256 const Instruction* move_inst = Instruction::At(code_item_->insns_ + move_idx);
2257 switch (move_inst->Opcode()) {
2258 case Instruction::MOVE_OBJECT:
2259 if (move_inst->VRegA_12x() == instance_of_inst->VRegB_22c()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002260 update_line->SetRegisterType(this, move_inst->VRegB_12x(), cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002261 }
2262 break;
2263 case Instruction::MOVE_OBJECT_FROM16:
2264 if (move_inst->VRegA_22x() == instance_of_inst->VRegB_22c()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002265 update_line->SetRegisterType(this, move_inst->VRegB_22x(), cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002266 }
2267 break;
2268 case Instruction::MOVE_OBJECT_16:
2269 if (move_inst->VRegA_32x() == instance_of_inst->VRegB_22c()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002270 update_line->SetRegisterType(this, move_inst->VRegB_32x(), cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002271 }
2272 break;
2273 default:
2274 break;
2275 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002276 }
2277 }
2278 }
2279
jeffhaobdb76512011-09-07 11:43:16 -07002280 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002281 }
jeffhaobdb76512011-09-07 11:43:16 -07002282 case Instruction::IF_LTZ:
2283 case Instruction::IF_GEZ:
2284 case Instruction::IF_GTZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002285 case Instruction::IF_LEZ: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002286 const RegType& reg_type = work_line_->GetRegisterType(this, inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002287 if (!reg_type.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002288 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
2289 << " unexpected as arg to if-ltz/if-gez/if-gtz/if-lez";
Ian Rogersd81871c2011-10-03 13:57:23 -07002290 }
jeffhaobdb76512011-09-07 11:43:16 -07002291 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002292 }
jeffhaobdb76512011-09-07 11:43:16 -07002293 case Instruction::AGET_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002294 VerifyAGet(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002295 break;
jeffhaobdb76512011-09-07 11:43:16 -07002296 case Instruction::AGET_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002297 VerifyAGet(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002298 break;
jeffhaobdb76512011-09-07 11:43:16 -07002299 case Instruction::AGET_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002300 VerifyAGet(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002301 break;
jeffhaobdb76512011-09-07 11:43:16 -07002302 case Instruction::AGET_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002303 VerifyAGet(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002304 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002305 case Instruction::AGET:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002306 VerifyAGet(inst, reg_types_.Integer(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002307 break;
jeffhaobdb76512011-09-07 11:43:16 -07002308 case Instruction::AGET_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002309 VerifyAGet(inst, reg_types_.LongLo(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002310 break;
2311 case Instruction::AGET_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002312 VerifyAGet(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07002313 break;
2314
Ian Rogersd81871c2011-10-03 13:57:23 -07002315 case Instruction::APUT_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002316 VerifyAPut(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002317 break;
2318 case Instruction::APUT_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002319 VerifyAPut(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002320 break;
2321 case Instruction::APUT_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002322 VerifyAPut(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002323 break;
2324 case Instruction::APUT_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002325 VerifyAPut(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002326 break;
2327 case Instruction::APUT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002328 VerifyAPut(inst, reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002329 break;
2330 case Instruction::APUT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002331 VerifyAPut(inst, reg_types_.LongLo(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002332 break;
2333 case Instruction::APUT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002334 VerifyAPut(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07002335 break;
2336
jeffhaobdb76512011-09-07 11:43:16 -07002337 case Instruction::IGET_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002338 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002339 break;
jeffhaobdb76512011-09-07 11:43:16 -07002340 case Instruction::IGET_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002341 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002342 break;
jeffhaobdb76512011-09-07 11:43:16 -07002343 case Instruction::IGET_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002344 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002345 break;
jeffhaobdb76512011-09-07 11:43:16 -07002346 case Instruction::IGET_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002347 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002348 break;
2349 case Instruction::IGET:
Andreas Gampe896df402014-10-20 22:25:29 -07002350 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002351 break;
2352 case Instruction::IGET_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002353 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002354 break;
2355 case Instruction::IGET_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002356 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false,
2357 false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002358 break;
jeffhaobdb76512011-09-07 11:43:16 -07002359
Ian Rogersd81871c2011-10-03 13:57:23 -07002360 case Instruction::IPUT_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002361 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002362 break;
2363 case Instruction::IPUT_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002364 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002365 break;
2366 case Instruction::IPUT_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002367 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002368 break;
2369 case Instruction::IPUT_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002370 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002371 break;
2372 case Instruction::IPUT:
Andreas Gampe896df402014-10-20 22:25:29 -07002373 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002374 break;
2375 case Instruction::IPUT_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002376 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002377 break;
jeffhaobdb76512011-09-07 11:43:16 -07002378 case Instruction::IPUT_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002379 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false,
2380 false);
jeffhaobdb76512011-09-07 11:43:16 -07002381 break;
2382
jeffhaobdb76512011-09-07 11:43:16 -07002383 case Instruction::SGET_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002384 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002385 break;
jeffhaobdb76512011-09-07 11:43:16 -07002386 case Instruction::SGET_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002387 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002388 break;
jeffhaobdb76512011-09-07 11:43:16 -07002389 case Instruction::SGET_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002390 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002391 break;
jeffhaobdb76512011-09-07 11:43:16 -07002392 case Instruction::SGET_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002393 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002394 break;
2395 case Instruction::SGET:
Andreas Gampe896df402014-10-20 22:25:29 -07002396 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002397 break;
2398 case Instruction::SGET_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002399 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002400 break;
2401 case Instruction::SGET_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002402 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false,
2403 true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002404 break;
2405
2406 case Instruction::SPUT_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002407 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002408 break;
2409 case Instruction::SPUT_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002410 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002411 break;
2412 case Instruction::SPUT_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002413 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002414 break;
2415 case Instruction::SPUT_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002416 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002417 break;
2418 case Instruction::SPUT:
Andreas Gampe896df402014-10-20 22:25:29 -07002419 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002420 break;
2421 case Instruction::SPUT_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002422 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002423 break;
2424 case Instruction::SPUT_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002425 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false,
2426 true);
jeffhaobdb76512011-09-07 11:43:16 -07002427 break;
2428
2429 case Instruction::INVOKE_VIRTUAL:
2430 case Instruction::INVOKE_VIRTUAL_RANGE:
2431 case Instruction::INVOKE_SUPER:
Ian Rogersd81871c2011-10-03 13:57:23 -07002432 case Instruction::INVOKE_SUPER_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002433 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE ||
2434 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002435 bool is_super = (inst->Opcode() == Instruction::INVOKE_SUPER ||
2436 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002437 ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_VIRTUAL, is_range, is_super);
Ian Rogersd8f69b02014-09-10 21:43:52 +00002438 const RegType* return_type = nullptr;
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002439 if (called_method != nullptr) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002440 StackHandleScope<1> hs(self_);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002441 mirror::Class* return_type_class = called_method->GetReturnType(can_load_classes_);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002442 if (return_type_class != nullptr) {
Andreas Gampef23f33d2015-06-23 14:18:17 -07002443 return_type = &FromClass(called_method->GetReturnTypeDescriptor(),
2444 return_type_class,
2445 return_type_class->CannotBeAssignedFromOtherTypes());
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002446 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07002447 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
2448 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002449 }
2450 }
2451 if (return_type == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002452 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002453 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2454 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002455 const char* descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Mathieu Chartierbf99f772014-08-23 16:37:27 -07002456 return_type = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
jeffhaobdb76512011-09-07 11:43:16 -07002457 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002458 if (!return_type->IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002459 work_line_->SetResultRegisterType(this, *return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002460 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002461 work_line_->SetResultRegisterTypeWide(*return_type, return_type->HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002462 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002463 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002464 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002465 }
jeffhaobdb76512011-09-07 11:43:16 -07002466 case Instruction::INVOKE_DIRECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002467 case Instruction::INVOKE_DIRECT_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002468 bool is_range = (inst->Opcode() == Instruction::INVOKE_DIRECT_RANGE);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002469 ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_DIRECT, is_range, false);
Ian Rogers46685432012-06-03 22:26:43 -07002470 const char* return_type_descriptor;
2471 bool is_constructor;
Ian Rogersd8f69b02014-09-10 21:43:52 +00002472 const RegType* return_type = nullptr;
Ian Rogers7b078e82014-09-10 14:44:24 -07002473 if (called_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002474 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogers46685432012-06-03 22:26:43 -07002475 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
Ian Rogersdfb325e2013-10-30 01:00:44 -07002476 is_constructor = strcmp("<init>", dex_file_->StringDataByIdx(method_id.name_idx_)) == 0;
Ian Rogers46685432012-06-03 22:26:43 -07002477 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
2478 return_type_descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2479 } else {
2480 is_constructor = called_method->IsConstructor();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002481 return_type_descriptor = called_method->GetReturnTypeDescriptor();
Ian Rogers7b078e82014-09-10 14:44:24 -07002482 StackHandleScope<1> hs(self_);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002483 mirror::Class* return_type_class = called_method->GetReturnType(can_load_classes_);
Ian Rogers1ff3c982014-08-12 02:30:58 -07002484 if (return_type_class != nullptr) {
Andreas Gampef23f33d2015-06-23 14:18:17 -07002485 return_type = &FromClass(return_type_descriptor,
2486 return_type_class,
2487 return_type_class->CannotBeAssignedFromOtherTypes());
Ian Rogers1ff3c982014-08-12 02:30:58 -07002488 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07002489 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
2490 self_->ClearException();
Ian Rogers1ff3c982014-08-12 02:30:58 -07002491 }
Ian Rogers46685432012-06-03 22:26:43 -07002492 }
2493 if (is_constructor) {
jeffhaobdb76512011-09-07 11:43:16 -07002494 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002495 * Some additional checks when calling a constructor. We know from the invocation arg check
2496 * that the "this" argument is an instance of called_method->klass. Now we further restrict
2497 * that to require that called_method->klass is the same as this->klass or this->super,
2498 * allowing the latter only if the "this" argument is the same as the "this" argument to
2499 * this method (which implies that we're in a constructor ourselves).
jeffhaobdb76512011-09-07 11:43:16 -07002500 */
Ian Rogers7b078e82014-09-10 14:44:24 -07002501 const RegType& this_type = work_line_->GetInvocationThis(this, inst, is_range);
jeffhaob57e9522012-04-26 18:08:21 -07002502 if (this_type.IsConflict()) // failure.
2503 break;
jeffhaobdb76512011-09-07 11:43:16 -07002504
jeffhaob57e9522012-04-26 18:08:21 -07002505 /* no null refs allowed (?) */
2506 if (this_type.IsZero()) {
2507 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unable to initialize null ref";
2508 break;
jeffhao2a8a90e2011-09-26 14:25:31 -07002509 }
jeffhaob57e9522012-04-26 18:08:21 -07002510
2511 /* must be in same class or in superclass */
Ian Rogersd8f69b02014-09-10 21:43:52 +00002512 // const RegType& this_super_klass = this_type.GetSuperClass(&reg_types_);
Ian Rogers46685432012-06-03 22:26:43 -07002513 // TODO: re-enable constructor type verification
2514 // if (this_super_klass.IsConflict()) {
jeffhaob57e9522012-04-26 18:08:21 -07002515 // Unknown super class, fail so we re-check at runtime.
Ian Rogers46685432012-06-03 22:26:43 -07002516 // Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "super class unknown for '" << this_type << "'";
2517 // break;
2518 // }
jeffhaob57e9522012-04-26 18:08:21 -07002519
2520 /* arg must be an uninitialized reference */
2521 if (!this_type.IsUninitializedTypes()) {
2522 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Expected initialization on uninitialized reference "
2523 << this_type;
2524 break;
2525 }
2526
2527 /*
2528 * Replace the uninitialized reference with an initialized one. We need to do this for all
2529 * registers that have the same object instance in them, not just the "this" register.
2530 */
Jeff Hao848f70a2014-01-15 13:49:50 -08002531 const uint32_t this_reg = (is_range) ? inst->VRegC_3rc() : inst->VRegC_35c();
2532 work_line_->MarkRefsAsInitialized(this, this_type, this_reg, work_insn_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002533 }
Ian Rogers1ff3c982014-08-12 02:30:58 -07002534 if (return_type == nullptr) {
Mathieu Chartierbf99f772014-08-23 16:37:27 -07002535 return_type = &reg_types_.FromDescriptor(GetClassLoader(), return_type_descriptor,
2536 false);
Ian Rogers1ff3c982014-08-12 02:30:58 -07002537 }
2538 if (!return_type->IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002539 work_line_->SetResultRegisterType(this, *return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002540 } else {
Ian Rogers1ff3c982014-08-12 02:30:58 -07002541 work_line_->SetResultRegisterTypeWide(*return_type, return_type->HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002542 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002543 just_set_result = true;
2544 break;
2545 }
2546 case Instruction::INVOKE_STATIC:
2547 case Instruction::INVOKE_STATIC_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002548 bool is_range = (inst->Opcode() == Instruction::INVOKE_STATIC_RANGE);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002549 ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_STATIC, is_range, false);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002550 const char* descriptor;
Ian Rogers7b078e82014-09-10 14:44:24 -07002551 if (called_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002552 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002553 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2554 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Mathieu Chartier590fee92013-09-13 13:46:47 -07002555 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002556 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002557 descriptor = called_method->GetReturnTypeDescriptor();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002558 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00002559 const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002560 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002561 work_line_->SetResultRegisterType(this, return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002562 } else {
2563 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2564 }
jeffhaobdb76512011-09-07 11:43:16 -07002565 just_set_result = true;
2566 }
2567 break;
jeffhaobdb76512011-09-07 11:43:16 -07002568 case Instruction::INVOKE_INTERFACE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002569 case Instruction::INVOKE_INTERFACE_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002570 bool is_range = (inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002571 ArtMethod* abs_method = VerifyInvocationArgs(inst, METHOD_INTERFACE, is_range, false);
Ian Rogers7b078e82014-09-10 14:44:24 -07002572 if (abs_method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002573 mirror::Class* called_interface = abs_method->GetDeclaringClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002574 if (!called_interface->IsInterface() && !called_interface->IsObjectClass()) {
2575 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected interface class in invoke-interface '"
2576 << PrettyMethod(abs_method) << "'";
2577 break;
Ian Rogers28ad40d2011-10-27 15:19:26 -07002578 }
Ian Rogers0d604842012-04-16 14:50:24 -07002579 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002580 /* Get the type of the "this" arg, which should either be a sub-interface of called
2581 * interface or Object (see comments in RegType::JoinClass).
2582 */
Ian Rogers7b078e82014-09-10 14:44:24 -07002583 const RegType& this_type = work_line_->GetInvocationThis(this, inst, is_range);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002584 if (this_type.IsZero()) {
2585 /* null pointer always passes (and always fails at runtime) */
2586 } else {
2587 if (this_type.IsUninitializedTypes()) {
2588 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface call on uninitialized object "
2589 << this_type;
2590 break;
2591 }
2592 // In the past we have tried to assert that "called_interface" is assignable
2593 // from "this_type.GetClass()", however, as we do an imprecise Join
2594 // (RegType::JoinClass) we don't have full information on what interfaces are
2595 // implemented by "this_type". For example, two classes may implement the same
2596 // interfaces and have a common parent that doesn't implement the interface. The
2597 // join will set "this_type" to the parent class and a test that this implements
2598 // the interface will incorrectly fail.
2599 }
2600 /*
2601 * We don't have an object instance, so we can't find the concrete method. However, all of
2602 * the type information is in the abstract method, so we're good.
2603 */
2604 const char* descriptor;
Ian Rogers7b078e82014-09-10 14:44:24 -07002605 if (abs_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002606 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002607 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2608 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
2609 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2610 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002611 descriptor = abs_method->GetReturnTypeDescriptor();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002612 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00002613 const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002614 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002615 work_line_->SetResultRegisterType(this, return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002616 } else {
2617 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2618 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002619 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002620 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002621 }
jeffhaobdb76512011-09-07 11:43:16 -07002622 case Instruction::NEG_INT:
2623 case Instruction::NOT_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002624 work_line_->CheckUnaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002625 break;
2626 case Instruction::NEG_LONG:
2627 case Instruction::NOT_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002628 work_line_->CheckUnaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002629 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002630 break;
2631 case Instruction::NEG_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002632 work_line_->CheckUnaryOp(this, inst, reg_types_.Float(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002633 break;
2634 case Instruction::NEG_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002635 work_line_->CheckUnaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002636 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002637 break;
2638 case Instruction::INT_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002639 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002640 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002641 break;
2642 case Instruction::INT_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002643 work_line_->CheckUnaryOp(this, inst, reg_types_.Float(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002644 break;
2645 case Instruction::INT_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002646 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002647 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002648 break;
2649 case Instruction::LONG_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002650 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002651 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002652 break;
2653 case Instruction::LONG_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002654 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002655 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002656 break;
2657 case Instruction::LONG_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002658 work_line_->CheckUnaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002659 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002660 break;
2661 case Instruction::FLOAT_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002662 work_line_->CheckUnaryOp(this, inst, reg_types_.Integer(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002663 break;
2664 case Instruction::FLOAT_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002665 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002666 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002667 break;
2668 case Instruction::FLOAT_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002669 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002670 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002671 break;
2672 case Instruction::DOUBLE_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002673 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002674 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002675 break;
2676 case Instruction::DOUBLE_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002677 work_line_->CheckUnaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002678 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002679 break;
2680 case Instruction::DOUBLE_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002681 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002682 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002683 break;
2684 case Instruction::INT_TO_BYTE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002685 work_line_->CheckUnaryOp(this, inst, reg_types_.Byte(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002686 break;
2687 case Instruction::INT_TO_CHAR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002688 work_line_->CheckUnaryOp(this, inst, reg_types_.Char(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002689 break;
2690 case Instruction::INT_TO_SHORT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002691 work_line_->CheckUnaryOp(this, inst, reg_types_.Short(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002692 break;
2693
2694 case Instruction::ADD_INT:
2695 case Instruction::SUB_INT:
2696 case Instruction::MUL_INT:
2697 case Instruction::REM_INT:
2698 case Instruction::DIV_INT:
2699 case Instruction::SHL_INT:
2700 case Instruction::SHR_INT:
2701 case Instruction::USHR_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002702 work_line_->CheckBinaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002703 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002704 break;
2705 case Instruction::AND_INT:
2706 case Instruction::OR_INT:
2707 case Instruction::XOR_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002708 work_line_->CheckBinaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002709 reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002710 break;
2711 case Instruction::ADD_LONG:
2712 case Instruction::SUB_LONG:
2713 case Instruction::MUL_LONG:
2714 case Instruction::DIV_LONG:
2715 case Instruction::REM_LONG:
2716 case Instruction::AND_LONG:
2717 case Instruction::OR_LONG:
2718 case Instruction::XOR_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002719 work_line_->CheckBinaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002720 reg_types_.LongLo(), reg_types_.LongHi(),
2721 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002722 break;
2723 case Instruction::SHL_LONG:
2724 case Instruction::SHR_LONG:
2725 case Instruction::USHR_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002726 /* shift distance is Int, making these different from other binary operations */
Ian Rogers7b078e82014-09-10 14:44:24 -07002727 work_line_->CheckBinaryOpWideShift(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002728 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002729 break;
2730 case Instruction::ADD_FLOAT:
2731 case Instruction::SUB_FLOAT:
2732 case Instruction::MUL_FLOAT:
2733 case Instruction::DIV_FLOAT:
2734 case Instruction::REM_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002735 work_line_->CheckBinaryOp(this, inst, reg_types_.Float(), reg_types_.Float(),
2736 reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002737 break;
2738 case Instruction::ADD_DOUBLE:
2739 case Instruction::SUB_DOUBLE:
2740 case Instruction::MUL_DOUBLE:
2741 case Instruction::DIV_DOUBLE:
2742 case Instruction::REM_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002743 work_line_->CheckBinaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002744 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2745 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002746 break;
2747 case Instruction::ADD_INT_2ADDR:
2748 case Instruction::SUB_INT_2ADDR:
2749 case Instruction::MUL_INT_2ADDR:
2750 case Instruction::REM_INT_2ADDR:
2751 case Instruction::SHL_INT_2ADDR:
2752 case Instruction::SHR_INT_2ADDR:
2753 case Instruction::USHR_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002754 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
2755 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002756 break;
2757 case Instruction::AND_INT_2ADDR:
2758 case Instruction::OR_INT_2ADDR:
2759 case Instruction::XOR_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002760 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
2761 reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002762 break;
2763 case Instruction::DIV_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002764 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
2765 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002766 break;
2767 case Instruction::ADD_LONG_2ADDR:
2768 case Instruction::SUB_LONG_2ADDR:
2769 case Instruction::MUL_LONG_2ADDR:
2770 case Instruction::DIV_LONG_2ADDR:
2771 case Instruction::REM_LONG_2ADDR:
2772 case Instruction::AND_LONG_2ADDR:
2773 case Instruction::OR_LONG_2ADDR:
2774 case Instruction::XOR_LONG_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002775 work_line_->CheckBinaryOp2addrWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002776 reg_types_.LongLo(), reg_types_.LongHi(),
2777 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002778 break;
2779 case Instruction::SHL_LONG_2ADDR:
2780 case Instruction::SHR_LONG_2ADDR:
2781 case Instruction::USHR_LONG_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002782 work_line_->CheckBinaryOp2addrWideShift(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002783 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002784 break;
2785 case Instruction::ADD_FLOAT_2ADDR:
2786 case Instruction::SUB_FLOAT_2ADDR:
2787 case Instruction::MUL_FLOAT_2ADDR:
2788 case Instruction::DIV_FLOAT_2ADDR:
2789 case Instruction::REM_FLOAT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002790 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Float(), reg_types_.Float(),
2791 reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002792 break;
2793 case Instruction::ADD_DOUBLE_2ADDR:
2794 case Instruction::SUB_DOUBLE_2ADDR:
2795 case Instruction::MUL_DOUBLE_2ADDR:
2796 case Instruction::DIV_DOUBLE_2ADDR:
2797 case Instruction::REM_DOUBLE_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002798 work_line_->CheckBinaryOp2addrWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002799 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2800 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002801 break;
2802 case Instruction::ADD_INT_LIT16:
Ian Rogersf72a11d2014-10-30 15:41:08 -07002803 case Instruction::RSUB_INT_LIT16:
jeffhaobdb76512011-09-07 11:43:16 -07002804 case Instruction::MUL_INT_LIT16:
2805 case Instruction::DIV_INT_LIT16:
2806 case Instruction::REM_INT_LIT16:
Ian Rogers7b078e82014-09-10 14:44:24 -07002807 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), false,
2808 true);
jeffhaobdb76512011-09-07 11:43:16 -07002809 break;
2810 case Instruction::AND_INT_LIT16:
2811 case Instruction::OR_INT_LIT16:
2812 case Instruction::XOR_INT_LIT16:
Ian Rogers7b078e82014-09-10 14:44:24 -07002813 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), true,
2814 true);
jeffhaobdb76512011-09-07 11:43:16 -07002815 break;
2816 case Instruction::ADD_INT_LIT8:
2817 case Instruction::RSUB_INT_LIT8:
2818 case Instruction::MUL_INT_LIT8:
2819 case Instruction::DIV_INT_LIT8:
2820 case Instruction::REM_INT_LIT8:
2821 case Instruction::SHL_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002822 case Instruction::SHR_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002823 case Instruction::USHR_INT_LIT8:
Ian Rogers7b078e82014-09-10 14:44:24 -07002824 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), false,
2825 false);
jeffhaobdb76512011-09-07 11:43:16 -07002826 break;
2827 case Instruction::AND_INT_LIT8:
2828 case Instruction::OR_INT_LIT8:
2829 case Instruction::XOR_INT_LIT8:
Ian Rogers7b078e82014-09-10 14:44:24 -07002830 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), true,
2831 false);
jeffhaobdb76512011-09-07 11:43:16 -07002832 break;
2833
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002834 // Special instructions.
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07002835 case Instruction::RETURN_VOID_NO_BARRIER:
2836 if (IsConstructor() && !IsStatic()) {
2837 auto& declaring_class = GetDeclaringClass();
Andreas Gampe68df3202015-06-22 11:35:46 -07002838 if (declaring_class.IsUnresolvedReference()) {
2839 // We must iterate over the fields, even if we cannot use mirror classes to do so. Do it
2840 // manually over the underlying dex file.
2841 uint32_t first_index = GetFirstFinalInstanceFieldIndex(*dex_file_,
2842 dex_file_->GetMethodId(dex_method_idx_).class_idx_);
2843 if (first_index != DexFile::kDexNoIndex) {
2844 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void-no-barrier not expected for field "
2845 << first_index;
2846 }
2847 break;
2848 }
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07002849 auto* klass = declaring_class.GetClass();
2850 for (uint32_t i = 0, num_fields = klass->NumInstanceFields(); i < num_fields; ++i) {
2851 if (klass->GetInstanceField(i)->IsFinal()) {
Mathieu Chartiere86deef2015-03-19 13:43:37 -07002852 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void-no-barrier not expected for "
2853 << PrettyField(klass->GetInstanceField(i));
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07002854 break;
2855 }
2856 }
Sebastien Hertzcc10e0e2013-06-28 14:24:48 +02002857 }
2858 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002859 // Note: the following instructions encode offsets derived from class linking.
2860 // As such they use Class*/Field*/AbstractMethod* as these offsets only have
2861 // meaning if the class linking and resolution were successful.
2862 case Instruction::IGET_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002863 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002864 break;
2865 case Instruction::IGET_WIDE_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002866 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002867 break;
2868 case Instruction::IGET_OBJECT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002869 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002870 break;
Mathieu Chartierffc605c2014-12-10 10:35:44 -08002871 case Instruction::IGET_BOOLEAN_QUICK:
2872 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true);
2873 break;
2874 case Instruction::IGET_BYTE_QUICK:
2875 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true);
2876 break;
2877 case Instruction::IGET_CHAR_QUICK:
2878 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true);
2879 break;
2880 case Instruction::IGET_SHORT_QUICK:
2881 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true);
2882 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002883 case Instruction::IPUT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002884 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002885 break;
Fred Shih37f05ef2014-07-16 18:38:08 -07002886 case Instruction::IPUT_BOOLEAN_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002887 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07002888 break;
2889 case Instruction::IPUT_BYTE_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002890 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07002891 break;
2892 case Instruction::IPUT_CHAR_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002893 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07002894 break;
2895 case Instruction::IPUT_SHORT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002896 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07002897 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002898 case Instruction::IPUT_WIDE_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002899 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002900 break;
2901 case Instruction::IPUT_OBJECT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002902 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002903 break;
2904 case Instruction::INVOKE_VIRTUAL_QUICK:
2905 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: {
2906 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002907 ArtMethod* called_method = VerifyInvokeVirtualQuickArgs(inst, is_range);
Ian Rogers7b078e82014-09-10 14:44:24 -07002908 if (called_method != nullptr) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002909 const char* descriptor = called_method->GetReturnTypeDescriptor();
Ian Rogersd8f69b02014-09-10 21:43:52 +00002910 const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002911 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002912 work_line_->SetResultRegisterType(this, return_type);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002913 } else {
2914 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2915 }
2916 just_set_result = true;
2917 }
2918 break;
2919 }
Igor Murashkin158f35c2015-06-10 15:55:30 -07002920 case Instruction::INVOKE_LAMBDA: {
2921 // Don't bother verifying, instead the interpreter will take the slow path with access checks.
2922 // If the code would've normally hard-failed, then the interpreter will throw the
2923 // appropriate verification errors at runtime.
2924 Fail(VERIFY_ERROR_FORCE_INTERPRETER); // TODO(iam): implement invoke-lambda verification
2925 break;
2926 }
2927 case Instruction::CREATE_LAMBDA: {
2928 // Don't bother verifying, instead the interpreter will take the slow path with access checks.
2929 // If the code would've normally hard-failed, then the interpreter will throw the
2930 // appropriate verification errors at runtime.
2931 Fail(VERIFY_ERROR_FORCE_INTERPRETER); // TODO(iam): implement create-lambda verification
2932 break;
2933 }
2934
Igor Murashkin2ee54e22015-06-18 10:05:11 -07002935 case Instruction::UNUSED_F4:
2936 case Instruction::UNUSED_F5:
2937 case Instruction::UNUSED_F7: {
Igor Murashkin158f35c2015-06-10 15:55:30 -07002938 DCHECK(false); // TODO(iam): Implement opcodes for lambdas
Igor Murashkin2ee54e22015-06-18 10:05:11 -07002939 // Conservatively fail verification on release builds.
2940 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Unexpected opcode " << inst->DumpString(dex_file_);
2941 break;
2942 }
2943
2944 case Instruction::BOX_LAMBDA: {
2945 // Don't bother verifying, instead the interpreter will take the slow path with access checks.
2946 // If the code would've normally hard-failed, then the interpreter will throw the
2947 // appropriate verification errors at runtime.
2948 Fail(VERIFY_ERROR_FORCE_INTERPRETER); // TODO(iam): implement box-lambda verification
2949 break;
2950 }
2951
2952 case Instruction::UNBOX_LAMBDA: {
2953 // Don't bother verifying, instead the interpreter will take the slow path with access checks.
2954 // If the code would've normally hard-failed, then the interpreter will throw the
2955 // appropriate verification errors at runtime.
2956 Fail(VERIFY_ERROR_FORCE_INTERPRETER); // TODO(iam): implement unbox-lambda verification
2957 break;
Igor Murashkin158f35c2015-06-10 15:55:30 -07002958 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002959
Ian Rogersd81871c2011-10-03 13:57:23 -07002960 /* These should never appear during verification. */
Mathieu Chartierffc605c2014-12-10 10:35:44 -08002961 case Instruction::UNUSED_3E ... Instruction::UNUSED_43:
Igor Murashkin158f35c2015-06-10 15:55:30 -07002962 case Instruction::UNUSED_FA ... Instruction::UNUSED_FF:
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002963 case Instruction::UNUSED_79:
2964 case Instruction::UNUSED_7A:
jeffhaod5347e02012-03-22 17:25:05 -07002965 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Unexpected opcode " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07002966 break;
2967
2968 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002969 * DO NOT add a "default" clause here. Without it the compiler will
jeffhaobdb76512011-09-07 11:43:16 -07002970 * complain if an instruction is missing (which is desirable).
2971 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002972 } // end - switch (dec_insn.opcode)
jeffhaobdb76512011-09-07 11:43:16 -07002973
Stephen Kyle7e541c92014-12-17 17:10:02 +00002974 /*
2975 * If we are in a constructor, and we had an UninitializedThis type
2976 * in a register somewhere, we need to make sure it wasn't overwritten.
2977 */
2978 if (track_uninitialized_this) {
2979 bool was_invoke_direct = (inst->Opcode() == Instruction::INVOKE_DIRECT ||
2980 inst->Opcode() == Instruction::INVOKE_DIRECT_RANGE);
2981 if (work_line_->WasUninitializedThisOverwritten(this, uninitialized_this_loc,
2982 was_invoke_direct)) {
2983 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
2984 << "Constructor failed to initialize this object";
2985 }
2986 }
2987
Ian Rogersad0b3a32012-04-16 14:50:24 -07002988 if (have_pending_hard_failure_) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08002989 if (Runtime::Current()->IsAotCompiler()) {
2990 /* When AOT compiling, check that the last failure is a hard failure */
Andreas Gampeb588f4c2015-05-26 13:35:39 -07002991 if (failures_[failures_.size() - 1] != VERIFY_ERROR_BAD_CLASS_HARD) {
2992 LOG(ERROR) << "Pending failures:";
2993 for (auto& error : failures_) {
2994 LOG(ERROR) << error;
2995 }
2996 for (auto& error_msg : failure_messages_) {
2997 LOG(ERROR) << error_msg->str();
2998 }
2999 LOG(FATAL) << "Pending hard failure, but last failure not hard.";
3000 }
Ian Rogerse1758fe2012-04-19 11:31:15 -07003001 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003002 /* immediate failure, reject class */
3003 info_messages_ << "Rejecting opcode " << inst->DumpString(dex_file_);
3004 return false;
jeffhaofaf459e2012-08-31 15:32:47 -07003005 } else if (have_pending_runtime_throw_failure_) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07003006 /* checking interpreter will throw, mark following code as unreachable */
jeffhaofaf459e2012-08-31 15:32:47 -07003007 opcode_flags = Instruction::kThrow;
Andreas Gamped12e7822015-06-25 10:26:40 -07003008 have_any_pending_runtime_throw_failure_ = true;
3009 // Reset the pending_runtime_throw flag. The flag is a global to decouple Fail and is per
3010 // instruction.
3011 have_pending_runtime_throw_failure_ = false;
jeffhaobdb76512011-09-07 11:43:16 -07003012 }
jeffhaobdb76512011-09-07 11:43:16 -07003013 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003014 * If we didn't just set the result register, clear it out. This ensures that you can only use
3015 * "move-result" immediately after the result is set. (We could check this statically, but it's
3016 * not expensive and it makes our debugging output cleaner.)
jeffhaobdb76512011-09-07 11:43:16 -07003017 */
3018 if (!just_set_result) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003019 work_line_->SetResultTypeToUnknown(this);
jeffhaobdb76512011-09-07 11:43:16 -07003020 }
3021
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003022
jeffhaobdb76512011-09-07 11:43:16 -07003023
3024 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003025 * Handle "branch". Tag the branch target.
jeffhaobdb76512011-09-07 11:43:16 -07003026 *
3027 * NOTE: instructions like Instruction::EQZ provide information about the
jeffhaod1f0fde2011-09-08 17:25:33 -07003028 * state of the register when the branch is taken or not taken. For example,
jeffhaobdb76512011-09-07 11:43:16 -07003029 * somebody could get a reference field, check it for zero, and if the
3030 * branch is taken immediately store that register in a boolean field
jeffhaod1f0fde2011-09-08 17:25:33 -07003031 * since the value is known to be zero. We do not currently account for
jeffhaobdb76512011-09-07 11:43:16 -07003032 * that, and will reject the code.
3033 *
3034 * TODO: avoid re-fetching the branch target
3035 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003036 if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07003037 bool isConditional, selfOkay;
Ian Rogersd81871c2011-10-03 13:57:23 -07003038 if (!GetBranchOffset(work_insn_idx_, &branch_target, &isConditional, &selfOkay)) {
jeffhaobdb76512011-09-07 11:43:16 -07003039 /* should never happen after static verification */
jeffhaod5347e02012-03-22 17:25:05 -07003040 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad branch";
jeffhaobdb76512011-09-07 11:43:16 -07003041 return false;
3042 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08003043 DCHECK_EQ(isConditional, (opcode_flags & Instruction::kContinue) != 0);
Stephen Kyle9bc61992014-09-22 13:53:15 +01003044 if (!CheckNotMoveExceptionOrMoveResult(code_item_->insns_, work_insn_idx_ + branch_target)) {
jeffhaobdb76512011-09-07 11:43:16 -07003045 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07003046 }
jeffhaobdb76512011-09-07 11:43:16 -07003047 /* update branch target, set "changed" if appropriate */
Ian Rogers7b078e82014-09-10 14:44:24 -07003048 if (nullptr != branch_line.get()) {
Ian Rogersebbdd872014-07-07 23:53:08 -07003049 if (!UpdateRegisters(work_insn_idx_ + branch_target, branch_line.get(), false)) {
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003050 return false;
3051 }
3052 } else {
Ian Rogersebbdd872014-07-07 23:53:08 -07003053 if (!UpdateRegisters(work_insn_idx_ + branch_target, work_line_.get(), false)) {
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003054 return false;
3055 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003056 }
jeffhaobdb76512011-09-07 11:43:16 -07003057 }
3058
3059 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003060 * Handle "switch". Tag all possible branch targets.
jeffhaobdb76512011-09-07 11:43:16 -07003061 *
3062 * We've already verified that the table is structurally sound, so we
3063 * just need to walk through and tag the targets.
3064 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003065 if ((opcode_flags & Instruction::kSwitch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07003066 int offset_to_switch = insns[1] | (((int32_t) insns[2]) << 16);
3067 const uint16_t* switch_insns = insns + offset_to_switch;
3068 int switch_count = switch_insns[1];
3069 int offset_to_targets, targ;
3070
3071 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
3072 /* 0 = sig, 1 = count, 2/3 = first key */
3073 offset_to_targets = 4;
3074 } else {
3075 /* 0 = sig, 1 = count, 2..count * 2 = keys */
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07003076 DCHECK((*insns & 0xff) == Instruction::SPARSE_SWITCH);
jeffhaobdb76512011-09-07 11:43:16 -07003077 offset_to_targets = 2 + 2 * switch_count;
3078 }
3079
3080 /* verify each switch target */
3081 for (targ = 0; targ < switch_count; targ++) {
3082 int offset;
3083 uint32_t abs_offset;
3084
3085 /* offsets are 32-bit, and only partly endian-swapped */
3086 offset = switch_insns[offset_to_targets + targ * 2] |
3087 (((int32_t) switch_insns[offset_to_targets + targ * 2 + 1]) << 16);
Ian Rogersd81871c2011-10-03 13:57:23 -07003088 abs_offset = work_insn_idx_ + offset;
3089 DCHECK_LT(abs_offset, code_item_->insns_size_in_code_units_);
Stephen Kyle9bc61992014-09-22 13:53:15 +01003090 if (!CheckNotMoveExceptionOrMoveResult(code_item_->insns_, abs_offset)) {
jeffhaobdb76512011-09-07 11:43:16 -07003091 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07003092 }
Ian Rogersebbdd872014-07-07 23:53:08 -07003093 if (!UpdateRegisters(abs_offset, work_line_.get(), false)) {
jeffhaobdb76512011-09-07 11:43:16 -07003094 return false;
Ian Rogersebbdd872014-07-07 23:53:08 -07003095 }
jeffhaobdb76512011-09-07 11:43:16 -07003096 }
3097 }
3098
3099 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003100 * Handle instructions that can throw and that are sitting in a "try" block. (If they're not in a
3101 * "try" block when they throw, control transfers out of the method.)
jeffhaobdb76512011-09-07 11:43:16 -07003102 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003103 if ((opcode_flags & Instruction::kThrow) != 0 && insn_flags_[work_insn_idx_].IsInTry()) {
Andreas Gampef91baf12014-07-18 15:41:00 -07003104 bool has_catch_all_handler = false;
Ian Rogers0571d352011-11-03 19:51:38 -07003105 CatchHandlerIterator iterator(*code_item_, work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07003106
Andreas Gampef91baf12014-07-18 15:41:00 -07003107 // Need the linker to try and resolve the handled class to check if it's Throwable.
3108 ClassLinker* linker = Runtime::Current()->GetClassLinker();
3109
Ian Rogers0571d352011-11-03 19:51:38 -07003110 for (; iterator.HasNext(); iterator.Next()) {
Andreas Gampef91baf12014-07-18 15:41:00 -07003111 uint16_t handler_type_idx = iterator.GetHandlerTypeIndex();
3112 if (handler_type_idx == DexFile::kDexNoIndex16) {
3113 has_catch_all_handler = true;
3114 } else {
3115 // It is also a catch-all if it is java.lang.Throwable.
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003116 mirror::Class* klass = linker->ResolveType(*dex_file_, handler_type_idx, dex_cache_,
3117 class_loader_);
Andreas Gampef91baf12014-07-18 15:41:00 -07003118 if (klass != nullptr) {
3119 if (klass == mirror::Throwable::GetJavaLangThrowable()) {
3120 has_catch_all_handler = true;
3121 }
3122 } else {
3123 // Clear exception.
Ian Rogers7b078e82014-09-10 14:44:24 -07003124 DCHECK(self_->IsExceptionPending());
3125 self_->ClearException();
Andreas Gampef91baf12014-07-18 15:41:00 -07003126 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003127 }
jeffhaobdb76512011-09-07 11:43:16 -07003128 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003129 * Merge registers into the "catch" block. We want to use the "savedRegs" rather than
3130 * "work_regs", because at runtime the exception will be thrown before the instruction
3131 * modifies any registers.
jeffhaobdb76512011-09-07 11:43:16 -07003132 */
Ian Rogersebbdd872014-07-07 23:53:08 -07003133 if (!UpdateRegisters(iterator.GetHandlerAddress(), saved_line_.get(), false)) {
jeffhaobdb76512011-09-07 11:43:16 -07003134 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07003135 }
jeffhaobdb76512011-09-07 11:43:16 -07003136 }
3137
3138 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003139 * If the monitor stack depth is nonzero, there must be a "catch all" handler for this
3140 * instruction. This does apply to monitor-exit because of async exception handling.
jeffhaobdb76512011-09-07 11:43:16 -07003141 */
Andreas Gampef91baf12014-07-18 15:41:00 -07003142 if (work_line_->MonitorStackDepth() > 0 && !has_catch_all_handler) {
jeffhaobdb76512011-09-07 11:43:16 -07003143 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003144 * The state in work_line reflects the post-execution state. If the current instruction is a
3145 * monitor-enter and the monitor stack was empty, we don't need a catch-all (if it throws,
jeffhaobdb76512011-09-07 11:43:16 -07003146 * it will do so before grabbing the lock).
3147 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02003148 if (inst->Opcode() != Instruction::MONITOR_ENTER || work_line_->MonitorStackDepth() != 1) {
jeffhaod5347e02012-03-22 17:25:05 -07003149 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
Ian Rogersd81871c2011-10-03 13:57:23 -07003150 << "expected to be within a catch-all for an instruction where a monitor is held";
jeffhaobdb76512011-09-07 11:43:16 -07003151 return false;
3152 }
3153 }
3154 }
3155
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003156 /* Handle "continue". Tag the next consecutive instruction.
3157 * Note: Keep the code handling "continue" case below the "branch" and "switch" cases,
3158 * because it changes work_line_ when performing peephole optimization
3159 * and this change should not be used in those cases.
3160 */
Ian Rogers6d376ae2013-07-23 15:12:40 -07003161 if ((opcode_flags & Instruction::kContinue) != 0) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003162 DCHECK_EQ(Instruction::At(code_item_->insns_ + work_insn_idx_), inst);
3163 uint32_t next_insn_idx = work_insn_idx_ + inst->SizeInCodeUnits();
Ian Rogers6d376ae2013-07-23 15:12:40 -07003164 if (next_insn_idx >= code_item_->insns_size_in_code_units_) {
3165 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Execution can walk off end of code area";
3166 return false;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003167 }
Ian Rogers6d376ae2013-07-23 15:12:40 -07003168 // The only way to get to a move-exception instruction is to get thrown there. Make sure the
3169 // next instruction isn't one.
3170 if (!CheckNotMoveException(code_item_->insns_, next_insn_idx)) {
3171 return false;
3172 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003173 if (nullptr != fallthrough_line.get()) {
Ian Rogers6d376ae2013-07-23 15:12:40 -07003174 // Make workline consistent with fallthrough computed from peephole optimization.
3175 work_line_->CopyFromLine(fallthrough_line.get());
3176 }
Ian Rogersb8c78592013-07-25 23:52:52 +00003177 if (insn_flags_[next_insn_idx].IsReturn()) {
3178 // For returns we only care about the operand to the return, all other registers are dead.
3179 const Instruction* ret_inst = Instruction::At(code_item_->insns_ + next_insn_idx);
3180 Instruction::Code opcode = ret_inst->Opcode();
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07003181 if (opcode == Instruction::RETURN_VOID || opcode == Instruction::RETURN_VOID_NO_BARRIER) {
Stephen Kyle7e541c92014-12-17 17:10:02 +00003182 SafelyMarkAllRegistersAsConflicts(this, work_line_.get());
Ian Rogersb8c78592013-07-25 23:52:52 +00003183 } else {
3184 if (opcode == Instruction::RETURN_WIDE) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003185 work_line_->MarkAllRegistersAsConflictsExceptWide(this, ret_inst->VRegA_11x());
Ian Rogersb8c78592013-07-25 23:52:52 +00003186 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07003187 work_line_->MarkAllRegistersAsConflictsExcept(this, ret_inst->VRegA_11x());
Ian Rogersb8c78592013-07-25 23:52:52 +00003188 }
3189 }
3190 }
Ian Rogers6d376ae2013-07-23 15:12:40 -07003191 RegisterLine* next_line = reg_table_.GetLine(next_insn_idx);
Ian Rogers7b078e82014-09-10 14:44:24 -07003192 if (next_line != nullptr) {
Ian Rogersebbdd872014-07-07 23:53:08 -07003193 // Merge registers into what we have for the next instruction, and set the "changed" flag if
3194 // needed. If the merge changes the state of the registers then the work line will be
3195 // updated.
3196 if (!UpdateRegisters(next_insn_idx, work_line_.get(), true)) {
Ian Rogers6d376ae2013-07-23 15:12:40 -07003197 return false;
3198 }
3199 } else {
3200 /*
3201 * We're not recording register data for the next instruction, so we don't know what the
3202 * prior state was. We have to assume that something has changed and re-evaluate it.
3203 */
3204 insn_flags_[next_insn_idx].SetChanged();
3205 }
3206 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003207
jeffhaod1f0fde2011-09-08 17:25:33 -07003208 /* If we're returning from the method, make sure monitor stack is empty. */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003209 if ((opcode_flags & Instruction::kReturn) != 0) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003210 if (!work_line_->VerifyMonitorStackEmpty(this)) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003211 return false;
3212 }
jeffhaobdb76512011-09-07 11:43:16 -07003213 }
3214
3215 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003216 * Update start_guess. Advance to the next instruction of that's
3217 * possible, otherwise use the branch target if one was found. If
jeffhaobdb76512011-09-07 11:43:16 -07003218 * neither of those exists we're in a return or throw; leave start_guess
3219 * alone and let the caller sort it out.
3220 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003221 if ((opcode_flags & Instruction::kContinue) != 0) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003222 DCHECK_EQ(Instruction::At(code_item_->insns_ + work_insn_idx_), inst);
3223 *start_guess = work_insn_idx_ + inst->SizeInCodeUnits();
Elliott Hughesadb8c672012-03-06 16:49:32 -08003224 } else if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07003225 /* we're still okay if branch_target is zero */
Ian Rogersd81871c2011-10-03 13:57:23 -07003226 *start_guess = work_insn_idx_ + branch_target;
jeffhaobdb76512011-09-07 11:43:16 -07003227 }
3228
Ian Rogersd81871c2011-10-03 13:57:23 -07003229 DCHECK_LT(*start_guess, code_item_->insns_size_in_code_units_);
3230 DCHECK(insn_flags_[*start_guess].IsOpcode());
jeffhaobdb76512011-09-07 11:43:16 -07003231
3232 return true;
Brian Carlstrom7934ac22013-07-26 10:54:15 -07003233} // NOLINT(readability/fn_size)
jeffhaobdb76512011-09-07 11:43:16 -07003234
Ian Rogersd8f69b02014-09-10 21:43:52 +00003235const RegType& MethodVerifier::ResolveClassAndCheckAccess(uint32_t class_idx) {
Ian Rogers0571d352011-11-03 19:51:38 -07003236 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
Ian Rogersd8f69b02014-09-10 21:43:52 +00003237 const RegType& referrer = GetDeclaringClass();
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003238 mirror::Class* klass = dex_cache_->GetResolvedType(class_idx);
Ian Rogers7b078e82014-09-10 14:44:24 -07003239 const RegType& result = klass != nullptr ?
Andreas Gampef23f33d2015-06-23 14:18:17 -07003240 FromClass(descriptor, klass, klass->CannotBeAssignedFromOtherTypes()) :
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003241 reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003242 if (result.IsConflict()) {
3243 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "accessing broken descriptor '" << descriptor
3244 << "' in " << referrer;
3245 return result;
3246 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003247 if (klass == nullptr && !result.IsUnresolvedTypes()) {
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003248 dex_cache_->SetResolvedType(class_idx, result.GetClass());
Ian Rogerse1758fe2012-04-19 11:31:15 -07003249 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003250 // Check if access is allowed. Unresolved types use xxxWithAccessCheck to
Jeff Haob24b4a72013-07-31 13:47:31 -07003251 // check at runtime if access is allowed and so pass here. If result is
3252 // primitive, skip the access check.
3253 if (result.IsNonZeroReferenceTypes() && !result.IsUnresolvedTypes() &&
3254 !referrer.IsUnresolvedTypes() && !referrer.CanAccess(result)) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07003255 Fail(VERIFY_ERROR_ACCESS_CLASS) << "illegal class access: '"
Ian Rogersad0b3a32012-04-16 14:50:24 -07003256 << referrer << "' -> '" << result << "'";
Ian Rogers28ad40d2011-10-27 15:19:26 -07003257 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003258 return result;
Ian Rogersd81871c2011-10-03 13:57:23 -07003259}
3260
Ian Rogersd8f69b02014-09-10 21:43:52 +00003261const RegType& MethodVerifier::GetCaughtExceptionType() {
Ian Rogers7b078e82014-09-10 14:44:24 -07003262 const RegType* common_super = nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003263 if (code_item_->tries_size_ != 0) {
Ian Rogers13735952014-10-08 12:43:28 -07003264 const uint8_t* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
Ian Rogersd81871c2011-10-03 13:57:23 -07003265 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
3266 for (uint32_t i = 0; i < handlers_size; i++) {
Ian Rogers0571d352011-11-03 19:51:38 -07003267 CatchHandlerIterator iterator(handlers_ptr);
3268 for (; iterator.HasNext(); iterator.Next()) {
3269 if (iterator.GetHandlerAddress() == (uint32_t) work_insn_idx_) {
3270 if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) {
Ian Rogersb4903572012-10-11 11:52:56 -07003271 common_super = &reg_types_.JavaLangThrowable(false);
Ian Rogersd81871c2011-10-03 13:57:23 -07003272 } else {
Ian Rogersd8f69b02014-09-10 21:43:52 +00003273 const RegType& exception = ResolveClassAndCheckAccess(iterator.GetHandlerTypeIndex());
Jeff Haob878f212014-04-24 16:25:36 -07003274 if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(exception)) {
Jeff Haoc26a56c2013-11-04 12:00:47 -08003275 if (exception.IsUnresolvedTypes()) {
3276 // We don't know enough about the type. Fail here and let runtime handle it.
3277 Fail(VERIFY_ERROR_NO_CLASS) << "unresolved exception class " << exception;
3278 return exception;
3279 } else {
3280 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unexpected non-exception class " << exception;
3281 return reg_types_.Conflict();
3282 }
Jeff Haob878f212014-04-24 16:25:36 -07003283 } else if (common_super == nullptr) {
3284 common_super = &exception;
Ian Rogers28ad40d2011-10-27 15:19:26 -07003285 } else if (common_super->Equals(exception)) {
Ian Rogersc4762272012-02-01 15:55:55 -08003286 // odd case, but nothing to do
Ian Rogersd81871c2011-10-03 13:57:23 -07003287 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07003288 common_super = &common_super->Merge(exception, &reg_types_);
Andreas Gampe7c038102014-10-27 20:08:46 -07003289 if (FailOrAbort(this,
3290 reg_types_.JavaLangThrowable(false).IsAssignableFrom(*common_super),
3291 "java.lang.Throwable is not assignable-from common_super at ",
3292 work_insn_idx_)) {
3293 break;
3294 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003295 }
3296 }
3297 }
3298 }
Ian Rogers0571d352011-11-03 19:51:38 -07003299 handlers_ptr = iterator.EndDataPointer();
Ian Rogersd81871c2011-10-03 13:57:23 -07003300 }
3301 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003302 if (common_super == nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003303 /* no catch blocks, or no catches with classes we can find */
jeffhaod5347e02012-03-22 17:25:05 -07003304 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unable to find exception handler";
Ian Rogersad0b3a32012-04-16 14:50:24 -07003305 return reg_types_.Conflict();
Ian Rogersd81871c2011-10-03 13:57:23 -07003306 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07003307 return *common_super;
Ian Rogersd81871c2011-10-03 13:57:23 -07003308}
3309
Mathieu Chartiere401d142015-04-22 13:56:20 -07003310ArtMethod* MethodVerifier::ResolveMethodAndCheckAccess(
3311 uint32_t dex_method_idx, MethodType method_type) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003312 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx);
Ian Rogersd8f69b02014-09-10 21:43:52 +00003313 const RegType& klass_type = ResolveClassAndCheckAccess(method_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003314 if (klass_type.IsConflict()) {
3315 std::string append(" in attempt to access method ");
3316 append += dex_file_->GetMethodName(method_id);
3317 AppendToLastFailMessage(append);
Ian Rogers7b078e82014-09-10 14:44:24 -07003318 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08003319 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003320 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003321 return nullptr; // Can't resolve Class so no more to do here
Ian Rogers90040192011-12-16 08:54:29 -08003322 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003323 mirror::Class* klass = klass_type.GetClass();
Ian Rogersd8f69b02014-09-10 21:43:52 +00003324 const RegType& referrer = GetDeclaringClass();
Mathieu Chartiere401d142015-04-22 13:56:20 -07003325 auto* cl = Runtime::Current()->GetClassLinker();
3326 auto pointer_size = cl->GetImagePointerSize();
3327 ArtMethod* res_method = dex_cache_->GetResolvedMethod(dex_method_idx, pointer_size);
Ian Rogers7b078e82014-09-10 14:44:24 -07003328 if (res_method == nullptr) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07003329 const char* name = dex_file_->GetMethodName(method_id);
Ian Rogersd91d6d62013-09-25 20:26:14 -07003330 const Signature signature = dex_file_->GetMethodSignature(method_id);
jeffhao8cd6dda2012-02-22 10:15:34 -08003331
3332 if (method_type == METHOD_DIRECT || method_type == METHOD_STATIC) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003333 res_method = klass->FindDirectMethod(name, signature, pointer_size);
jeffhao8cd6dda2012-02-22 10:15:34 -08003334 } else if (method_type == METHOD_INTERFACE) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003335 res_method = klass->FindInterfaceMethod(name, signature, pointer_size);
Ian Rogersd81871c2011-10-03 13:57:23 -07003336 } else {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003337 res_method = klass->FindVirtualMethod(name, signature, pointer_size);
Ian Rogersd81871c2011-10-03 13:57:23 -07003338 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003339 if (res_method != nullptr) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003340 dex_cache_->SetResolvedMethod(dex_method_idx, res_method, pointer_size);
Ian Rogersd81871c2011-10-03 13:57:23 -07003341 } else {
jeffhao8cd6dda2012-02-22 10:15:34 -08003342 // If a virtual or interface method wasn't found with the expected type, look in
3343 // the direct methods. This can happen when the wrong invoke type is used or when
3344 // a class has changed, and will be flagged as an error in later checks.
3345 if (method_type == METHOD_INTERFACE || method_type == METHOD_VIRTUAL) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003346 res_method = klass->FindDirectMethod(name, signature, pointer_size);
jeffhao8cd6dda2012-02-22 10:15:34 -08003347 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003348 if (res_method == nullptr) {
jeffhao8cd6dda2012-02-22 10:15:34 -08003349 Fail(VERIFY_ERROR_NO_METHOD) << "couldn't find method "
3350 << PrettyDescriptor(klass) << "." << name
3351 << " " << signature;
Ian Rogers7b078e82014-09-10 14:44:24 -07003352 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003353 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003354 }
3355 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003356 // Make sure calls to constructors are "direct". There are additional restrictions but we don't
3357 // enforce them here.
3358 if (res_method->IsConstructor() && method_type != METHOD_DIRECT) {
jeffhaod5347e02012-03-22 17:25:05 -07003359 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting non-direct call to constructor "
3360 << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003361 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003362 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003363 // Disallow any calls to class initializers.
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003364 if (res_method->IsClassInitializer()) {
jeffhaod5347e02012-03-22 17:25:05 -07003365 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting call to class initializer "
3366 << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003367 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003368 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003369 // Check if access is allowed.
Ian Rogersad0b3a32012-04-16 14:50:24 -07003370 if (!referrer.CanAccessMember(res_method->GetDeclaringClass(), res_method->GetAccessFlags())) {
jeffhao8cd6dda2012-02-22 10:15:34 -08003371 Fail(VERIFY_ERROR_ACCESS_METHOD) << "illegal method access (call " << PrettyMethod(res_method)
Ian Rogersad0b3a32012-04-16 14:50:24 -07003372 << " from " << referrer << ")";
jeffhaob57e9522012-04-26 18:08:21 -07003373 return res_method;
jeffhao8cd6dda2012-02-22 10:15:34 -08003374 }
jeffhaode0d9c92012-02-27 13:58:13 -08003375 // Check that invoke-virtual and invoke-super are not used on private methods of the same class.
3376 if (res_method->IsPrivate() && method_type == METHOD_VIRTUAL) {
jeffhaod5347e02012-03-22 17:25:05 -07003377 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invoke-super/virtual can't be used on private method "
3378 << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003379 return nullptr;
jeffhaode0d9c92012-02-27 13:58:13 -08003380 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003381 // Check that interface methods match interface classes.
3382 if (klass->IsInterface() && method_type != METHOD_INTERFACE) {
3383 Fail(VERIFY_ERROR_CLASS_CHANGE) << "non-interface method " << PrettyMethod(res_method)
3384 << " is in an interface class " << PrettyClass(klass);
Ian Rogers7b078e82014-09-10 14:44:24 -07003385 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003386 } else if (!klass->IsInterface() && method_type == METHOD_INTERFACE) {
3387 Fail(VERIFY_ERROR_CLASS_CHANGE) << "interface method " << PrettyMethod(res_method)
3388 << " is in a non-interface class " << PrettyClass(klass);
Ian Rogers7b078e82014-09-10 14:44:24 -07003389 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003390 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003391 // See if the method type implied by the invoke instruction matches the access flags for the
3392 // target method.
Brian Carlstrombe6fa5e2014-12-09 20:15:42 -08003393 if ((method_type == METHOD_DIRECT && (!res_method->IsDirect() || res_method->IsStatic())) ||
Ian Rogersd81871c2011-10-03 13:57:23 -07003394 (method_type == METHOD_STATIC && !res_method->IsStatic()) ||
3395 ((method_type == METHOD_VIRTUAL || method_type == METHOD_INTERFACE) && res_method->IsDirect())
3396 ) {
Ian Rogers2fc14272012-08-30 10:56:57 -07003397 Fail(VERIFY_ERROR_CLASS_CHANGE) << "invoke type (" << method_type << ") does not match method "
3398 " type of " << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003399 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003400 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003401 return res_method;
3402}
3403
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003404template <class T>
Mathieu Chartiere401d142015-04-22 13:56:20 -07003405ArtMethod* MethodVerifier::VerifyInvocationArgsFromIterator(
3406 T* it, const Instruction* inst, MethodType method_type, bool is_range, ArtMethod* res_method) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003407 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
3408 // match the call to the signature. Also, we might be calling through an abstract method
3409 // definition (which doesn't have register count values).
3410 const size_t expected_args = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
3411 /* caught by static verifier */
3412 DCHECK(is_range || expected_args <= 5);
3413 if (expected_args > code_item_->outs_size_) {
3414 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
3415 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
3416 return nullptr;
3417 }
3418
3419 uint32_t arg[5];
3420 if (!is_range) {
3421 inst->GetVarArgs(arg);
3422 }
3423 uint32_t sig_registers = 0;
3424
3425 /*
3426 * Check the "this" argument, which must be an instance of the class that declared the method.
3427 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
3428 * rigorous check here (which is okay since we have to do it at runtime).
3429 */
3430 if (method_type != METHOD_STATIC) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003431 const RegType& actual_arg_type = work_line_->GetInvocationThis(this, inst, is_range);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003432 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
3433 CHECK(have_pending_hard_failure_);
3434 return nullptr;
3435 }
3436 if (actual_arg_type.IsUninitializedReference()) {
3437 if (res_method) {
3438 if (!res_method->IsConstructor()) {
3439 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
3440 return nullptr;
3441 }
3442 } else {
3443 // Check whether the name of the called method is "<init>"
3444 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Jeff Hao0d087272014-08-04 14:47:17 -07003445 if (strcmp(dex_file_->GetMethodName(dex_file_->GetMethodId(method_idx)), "<init>") != 0) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003446 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
3447 return nullptr;
3448 }
3449 }
3450 }
3451 if (method_type != METHOD_INTERFACE && !actual_arg_type.IsZero()) {
Ian Rogersd8f69b02014-09-10 21:43:52 +00003452 const RegType* res_method_class;
Andreas Gamped9e23012015-06-04 22:19:58 -07003453 // Miranda methods have the declaring interface as their declaring class, not the abstract
3454 // class. It would be wrong to use this for the type check (interface type checks are
3455 // postponed to runtime).
3456 if (res_method != nullptr && !res_method->IsMiranda()) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003457 mirror::Class* klass = res_method->GetDeclaringClass();
Ian Rogers1ff3c982014-08-12 02:30:58 -07003458 std::string temp;
Andreas Gampef23f33d2015-06-23 14:18:17 -07003459 res_method_class = &FromClass(klass->GetDescriptor(&temp), klass,
3460 klass->CannotBeAssignedFromOtherTypes());
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003461 } else {
3462 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
3463 const uint16_t class_idx = dex_file_->GetMethodId(method_idx).class_idx_;
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003464 res_method_class = &reg_types_.FromDescriptor(GetClassLoader(),
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003465 dex_file_->StringByTypeIdx(class_idx),
3466 false);
3467 }
3468 if (!res_method_class->IsAssignableFrom(actual_arg_type)) {
3469 Fail(actual_arg_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS:
3470 VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type
3471 << "' not instance of '" << *res_method_class << "'";
3472 // Continue on soft failures. We need to find possible hard failures to avoid problems in
3473 // the compiler.
3474 if (have_pending_hard_failure_) {
3475 return nullptr;
3476 }
3477 }
3478 }
3479 sig_registers = 1;
3480 }
3481
3482 for ( ; it->HasNext(); it->Next()) {
3483 if (sig_registers >= expected_args) {
3484 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, expected " << inst->VRegA() <<
3485 " arguments, found " << sig_registers << " or more.";
3486 return nullptr;
3487 }
3488
3489 const char* param_descriptor = it->GetDescriptor();
3490
3491 if (param_descriptor == nullptr) {
3492 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation because of missing signature "
3493 "component";
3494 return nullptr;
3495 }
3496
Ian Rogersd8f69b02014-09-10 21:43:52 +00003497 const RegType& reg_type = reg_types_.FromDescriptor(GetClassLoader(), param_descriptor, false);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003498 uint32_t get_reg = is_range ? inst->VRegC_3rc() + static_cast<uint32_t>(sig_registers) :
3499 arg[sig_registers];
3500 if (reg_type.IsIntegralTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003501 const RegType& src_type = work_line_->GetRegisterType(this, get_reg);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003502 if (!src_type.IsIntegralTypes()) {
3503 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register v" << get_reg << " has type " << src_type
3504 << " but expected " << reg_type;
Andreas Gampeb588f4c2015-05-26 13:35:39 -07003505 return nullptr;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003506 }
Andreas Gampeda9badb2015-06-05 20:22:12 -07003507 } else {
3508 if (!work_line_->VerifyRegisterType(this, get_reg, reg_type)) {
3509 // Continue on soft failures. We need to find possible hard failures to avoid problems in
3510 // the compiler.
3511 if (have_pending_hard_failure_) {
3512 return nullptr;
3513 }
3514 } else if (reg_type.IsLongOrDoubleTypes()) {
3515 // Check that registers are consecutive (for non-range invokes). Invokes are the only
3516 // instructions not specifying register pairs by the first component, but require them
3517 // nonetheless. Only check when there's an actual register in the parameters. If there's
3518 // none, this will fail below.
3519 if (!is_range && sig_registers + 1 < expected_args) {
3520 uint32_t second_reg = arg[sig_registers + 1];
3521 if (second_reg != get_reg + 1) {
3522 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, long or double parameter "
3523 "at index " << sig_registers << " is not a pair: " << get_reg << " + "
3524 << second_reg << ".";
3525 return nullptr;
3526 }
3527 }
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003528 }
3529 }
3530 sig_registers += reg_type.IsLongOrDoubleTypes() ? 2 : 1;
3531 }
3532 if (expected_args != sig_registers) {
3533 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, expected " << expected_args <<
3534 " arguments, found " << sig_registers;
3535 return nullptr;
3536 }
3537 return res_method;
3538}
3539
3540void MethodVerifier::VerifyInvocationArgsUnresolvedMethod(const Instruction* inst,
3541 MethodType method_type,
3542 bool is_range) {
3543 // As the method may not have been resolved, make this static check against what we expect.
3544 // The main reason for this code block is to fail hard when we find an illegal use, e.g.,
3545 // wrong number of arguments or wrong primitive types, even if the method could not be resolved.
3546 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
3547 DexFileParameterIterator it(*dex_file_,
3548 dex_file_->GetProtoId(dex_file_->GetMethodId(method_idx).proto_idx_));
3549 VerifyInvocationArgsFromIterator<DexFileParameterIterator>(&it, inst, method_type, is_range,
3550 nullptr);
3551}
3552
3553class MethodParamListDescriptorIterator {
3554 public:
Mathieu Chartiere401d142015-04-22 13:56:20 -07003555 explicit MethodParamListDescriptorIterator(ArtMethod* res_method) :
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003556 res_method_(res_method), pos_(0), params_(res_method->GetParameterTypeList()),
3557 params_size_(params_ == nullptr ? 0 : params_->Size()) {
3558 }
3559
3560 bool HasNext() {
3561 return pos_ < params_size_;
3562 }
3563
3564 void Next() {
3565 ++pos_;
3566 }
3567
3568 const char* GetDescriptor() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
3569 return res_method_->GetTypeDescriptorFromTypeIdx(params_->GetTypeItem(pos_).type_idx_);
3570 }
3571
3572 private:
Mathieu Chartiere401d142015-04-22 13:56:20 -07003573 ArtMethod* res_method_;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003574 size_t pos_;
3575 const DexFile::TypeList* params_;
3576 const size_t params_size_;
3577};
3578
Mathieu Chartiere401d142015-04-22 13:56:20 -07003579ArtMethod* MethodVerifier::VerifyInvocationArgs(
3580 const Instruction* inst, MethodType method_type, bool is_range, bool is_super) {
jeffhao8cd6dda2012-02-22 10:15:34 -08003581 // Resolve the method. This could be an abstract or concrete method depending on what sort of call
3582 // we're making.
Sebastien Hertz5243e912013-05-21 10:55:07 +02003583 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Andreas Gampeacc4d2f2014-06-12 19:35:05 -07003584
Mathieu Chartiere401d142015-04-22 13:56:20 -07003585 ArtMethod* res_method = ResolveMethodAndCheckAccess(method_idx, method_type);
Ian Rogers7b078e82014-09-10 14:44:24 -07003586 if (res_method == nullptr) { // error or class is unresolved
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003587 // Check what we can statically.
3588 if (!have_pending_hard_failure_) {
3589 VerifyInvocationArgsUnresolvedMethod(inst, method_type, is_range);
3590 }
3591 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003592 }
3593
Ian Rogersd81871c2011-10-03 13:57:23 -07003594 // If we're using invoke-super(method), make sure that the executing method's class' superclass
3595 // has a vtable entry for the target method.
3596 if (is_super) {
3597 DCHECK(method_type == METHOD_VIRTUAL);
Ian Rogersd8f69b02014-09-10 21:43:52 +00003598 const RegType& super = GetDeclaringClass().GetSuperClass(&reg_types_);
Ian Rogers529781d2012-07-23 17:24:29 -07003599 if (super.IsUnresolvedTypes()) {
jeffhao4d8df822012-04-24 17:09:36 -07003600 Fail(VERIFY_ERROR_NO_METHOD) << "unknown super class in invoke-super from "
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003601 << PrettyMethod(dex_method_idx_, *dex_file_)
jeffhao4d8df822012-04-24 17:09:36 -07003602 << " to super " << PrettyMethod(res_method);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003603 return nullptr;
jeffhao4d8df822012-04-24 17:09:36 -07003604 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003605 mirror::Class* super_klass = super.GetClass();
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003606 if (res_method->GetMethodIndex() >= super_klass->GetVTableLength()) {
jeffhao4d8df822012-04-24 17:09:36 -07003607 Fail(VERIFY_ERROR_NO_METHOD) << "invalid invoke-super from "
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003608 << PrettyMethod(dex_method_idx_, *dex_file_)
jeffhao4d8df822012-04-24 17:09:36 -07003609 << " to super " << super
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003610 << "." << res_method->GetName()
3611 << res_method->GetSignature();
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003612 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003613 }
3614 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003615
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003616 // Process the target method's signature. This signature may or may not
3617 MethodParamListDescriptorIterator it(res_method);
3618 return VerifyInvocationArgsFromIterator<MethodParamListDescriptorIterator>(&it, inst, method_type,
3619 is_range, res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07003620}
3621
Mathieu Chartiere401d142015-04-22 13:56:20 -07003622ArtMethod* MethodVerifier::GetQuickInvokedMethod(const Instruction* inst, RegisterLine* reg_line,
3623 bool is_range, bool allow_failure) {
Mathieu Chartier091d2382015-03-06 10:59:06 -08003624 if (is_range) {
3625 DCHECK_EQ(inst->Opcode(), Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
3626 } else {
3627 DCHECK_EQ(inst->Opcode(), Instruction::INVOKE_VIRTUAL_QUICK);
3628 }
3629 const RegType& actual_arg_type = reg_line->GetInvocationThis(this, inst, is_range, allow_failure);
Ian Rogers9bc54402014-04-17 16:40:01 -07003630 if (!actual_arg_type.HasClass()) {
3631 VLOG(verifier) << "Failed to get mirror::Class* from '" << actual_arg_type << "'";
Andreas Gampe63981562014-04-17 12:28:43 -07003632 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003633 }
Ian Rogersa4cf1df2014-05-07 19:47:17 -07003634 mirror::Class* klass = actual_arg_type.GetClass();
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003635 mirror::Class* dispatch_class;
Ian Rogersa4cf1df2014-05-07 19:47:17 -07003636 if (klass->IsInterface()) {
3637 // Derive Object.class from Class.class.getSuperclass().
3638 mirror::Class* object_klass = klass->GetClass()->GetSuperClass();
Andreas Gampe7c038102014-10-27 20:08:46 -07003639 if (FailOrAbort(this, object_klass->IsObjectClass(),
Mathieu Chartier091d2382015-03-06 10:59:06 -08003640 "Failed to find Object class in quickened invoke receiver", work_insn_idx_)) {
Andreas Gampe7c038102014-10-27 20:08:46 -07003641 return nullptr;
3642 }
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003643 dispatch_class = object_klass;
Ian Rogersa4cf1df2014-05-07 19:47:17 -07003644 } else {
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003645 dispatch_class = klass;
Ian Rogersa4cf1df2014-05-07 19:47:17 -07003646 }
Mathieu Chartier091d2382015-03-06 10:59:06 -08003647 if (!dispatch_class->HasVTable()) {
3648 FailOrAbort(this, allow_failure, "Receiver class has no vtable for quickened invoke at ",
3649 work_insn_idx_);
Andreas Gampe7c038102014-10-27 20:08:46 -07003650 return nullptr;
3651 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003652 uint16_t vtable_index = is_range ? inst->VRegB_3rc() : inst->VRegB_35c();
Mathieu Chartiere401d142015-04-22 13:56:20 -07003653 auto* cl = Runtime::Current()->GetClassLinker();
3654 auto pointer_size = cl->GetImagePointerSize();
Mathieu Chartier091d2382015-03-06 10:59:06 -08003655 if (static_cast<int32_t>(vtable_index) >= dispatch_class->GetVTableLength()) {
3656 FailOrAbort(this, allow_failure,
3657 "Receiver class has not enough vtable slots for quickened invoke at ",
3658 work_insn_idx_);
Andreas Gampe7c038102014-10-27 20:08:46 -07003659 return nullptr;
3660 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07003661 ArtMethod* res_method = dispatch_class->GetVTableEntry(vtable_index, pointer_size);
Mathieu Chartier091d2382015-03-06 10:59:06 -08003662 if (self_->IsExceptionPending()) {
3663 FailOrAbort(this, allow_failure, "Unexpected exception pending for quickened invoke at ",
3664 work_insn_idx_);
Andreas Gampe7c038102014-10-27 20:08:46 -07003665 return nullptr;
3666 }
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003667 return res_method;
3668}
3669
Mathieu Chartiere401d142015-04-22 13:56:20 -07003670ArtMethod* MethodVerifier::VerifyInvokeVirtualQuickArgs(const Instruction* inst, bool is_range) {
Andreas Gampe76bd8802014-12-10 16:43:58 -08003671 DCHECK(Runtime::Current()->IsStarted() || verify_to_dump_)
3672 << PrettyMethod(dex_method_idx_, *dex_file_, true) << "@" << work_insn_idx_;
3673
Mathieu Chartiere401d142015-04-22 13:56:20 -07003674 ArtMethod* res_method = GetQuickInvokedMethod(inst, work_line_.get(), is_range, false);
Ian Rogers7b078e82014-09-10 14:44:24 -07003675 if (res_method == nullptr) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003676 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer method from " << inst->Name();
Ian Rogers7b078e82014-09-10 14:44:24 -07003677 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003678 }
Andreas Gampe7c038102014-10-27 20:08:46 -07003679 if (FailOrAbort(this, !res_method->IsDirect(), "Quick-invoked method is direct at ",
3680 work_insn_idx_)) {
3681 return nullptr;
3682 }
3683 if (FailOrAbort(this, !res_method->IsStatic(), "Quick-invoked method is static at ",
3684 work_insn_idx_)) {
3685 return nullptr;
3686 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003687
3688 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
3689 // match the call to the signature. Also, we might be calling through an abstract method
3690 // definition (which doesn't have register count values).
Ian Rogers7b078e82014-09-10 14:44:24 -07003691 const RegType& actual_arg_type = work_line_->GetInvocationThis(this, inst, is_range);
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003692 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
Ian Rogers7b078e82014-09-10 14:44:24 -07003693 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003694 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003695 const size_t expected_args = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
3696 /* caught by static verifier */
3697 DCHECK(is_range || expected_args <= 5);
3698 if (expected_args > code_item_->outs_size_) {
3699 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
3700 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
Ian Rogers7b078e82014-09-10 14:44:24 -07003701 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003702 }
3703
3704 /*
3705 * Check the "this" argument, which must be an instance of the class that declared the method.
3706 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
3707 * rigorous check here (which is okay since we have to do it at runtime).
3708 */
3709 if (actual_arg_type.IsUninitializedReference() && !res_method->IsConstructor()) {
3710 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
Ian Rogers7b078e82014-09-10 14:44:24 -07003711 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003712 }
3713 if (!actual_arg_type.IsZero()) {
3714 mirror::Class* klass = res_method->GetDeclaringClass();
Ian Rogers1ff3c982014-08-12 02:30:58 -07003715 std::string temp;
Ian Rogersd8f69b02014-09-10 21:43:52 +00003716 const RegType& res_method_class =
Andreas Gampef23f33d2015-06-23 14:18:17 -07003717 FromClass(klass->GetDescriptor(&temp), klass, klass->CannotBeAssignedFromOtherTypes());
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003718 if (!res_method_class.IsAssignableFrom(actual_arg_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07003719 Fail(actual_arg_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS :
3720 VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003721 << "' not instance of '" << res_method_class << "'";
Ian Rogers7b078e82014-09-10 14:44:24 -07003722 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003723 }
3724 }
3725 /*
3726 * Process the target method's signature. This signature may or may not
3727 * have been verified, so we can't assume it's properly formed.
3728 */
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003729 const DexFile::TypeList* params = res_method->GetParameterTypeList();
Ian Rogers7b078e82014-09-10 14:44:24 -07003730 size_t params_size = params == nullptr ? 0 : params->Size();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003731 uint32_t arg[5];
3732 if (!is_range) {
Ian Rogers29a26482014-05-02 15:27:29 -07003733 inst->GetVarArgs(arg);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003734 }
3735 size_t actual_args = 1;
3736 for (size_t param_index = 0; param_index < params_size; param_index++) {
3737 if (actual_args >= expected_args) {
3738 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invalid call to '" << PrettyMethod(res_method)
Brian Carlstrom93c33962013-07-26 10:37:43 -07003739 << "'. Expected " << expected_args
3740 << " arguments, processing argument " << actual_args
3741 << " (where longs/doubles count twice).";
Ian Rogers7b078e82014-09-10 14:44:24 -07003742 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003743 }
3744 const char* descriptor =
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003745 res_method->GetTypeDescriptorFromTypeIdx(params->GetTypeItem(param_index).type_idx_);
Ian Rogers7b078e82014-09-10 14:44:24 -07003746 if (descriptor == nullptr) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003747 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003748 << " missing signature component";
Ian Rogers7b078e82014-09-10 14:44:24 -07003749 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003750 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00003751 const RegType& reg_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003752 uint32_t get_reg = is_range ? inst->VRegC_3rc() + actual_args : arg[actual_args];
Ian Rogers7b078e82014-09-10 14:44:24 -07003753 if (!work_line_->VerifyRegisterType(this, get_reg, reg_type)) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003754 return res_method;
3755 }
3756 actual_args = reg_type.IsLongOrDoubleTypes() ? actual_args + 2 : actual_args + 1;
3757 }
3758 if (actual_args != expected_args) {
3759 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
3760 << " expected " << expected_args << " arguments, found " << actual_args;
Ian Rogers7b078e82014-09-10 14:44:24 -07003761 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003762 } else {
3763 return res_method;
3764 }
3765}
3766
Ian Rogers62342ec2013-06-11 10:26:37 -07003767void MethodVerifier::VerifyNewArray(const Instruction* inst, bool is_filled, bool is_range) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003768 uint32_t type_idx;
3769 if (!is_filled) {
3770 DCHECK_EQ(inst->Opcode(), Instruction::NEW_ARRAY);
3771 type_idx = inst->VRegC_22c();
3772 } else if (!is_range) {
3773 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY);
3774 type_idx = inst->VRegB_35c();
3775 } else {
3776 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY_RANGE);
3777 type_idx = inst->VRegB_3rc();
3778 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00003779 const RegType& res_type = ResolveClassAndCheckAccess(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003780 if (res_type.IsConflict()) { // bad class
3781 DCHECK_NE(failures_.size(), 0U);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003782 } else {
3783 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
3784 if (!res_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003785 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "new-array on non-array class " << res_type;
Ian Rogers0c4a5062012-02-03 15:18:59 -08003786 } else if (!is_filled) {
3787 /* make sure "size" register is valid type */
Ian Rogers7b078e82014-09-10 14:44:24 -07003788 work_line_->VerifyRegisterType(this, inst->VRegB_22c(), reg_types_.Integer());
Ian Rogers0c4a5062012-02-03 15:18:59 -08003789 /* set register type to array class */
Ian Rogersd8f69b02014-09-10 21:43:52 +00003790 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
Ian Rogers7b078e82014-09-10 14:44:24 -07003791 work_line_->SetRegisterType(this, inst->VRegA_22c(), precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003792 } else {
3793 // Verify each register. If "arg_count" is bad, VerifyRegisterType() will run off the end of
3794 // the list and fail. It's legal, if silly, for arg_count to be zero.
Ian Rogersd8f69b02014-09-10 21:43:52 +00003795 const RegType& expected_type = reg_types_.GetComponentType(res_type, GetClassLoader());
Sebastien Hertz5243e912013-05-21 10:55:07 +02003796 uint32_t arg_count = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
3797 uint32_t arg[5];
3798 if (!is_range) {
Ian Rogers29a26482014-05-02 15:27:29 -07003799 inst->GetVarArgs(arg);
Sebastien Hertz5243e912013-05-21 10:55:07 +02003800 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08003801 for (size_t ui = 0; ui < arg_count; ui++) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003802 uint32_t get_reg = is_range ? inst->VRegC_3rc() + ui : arg[ui];
Ian Rogers7b078e82014-09-10 14:44:24 -07003803 if (!work_line_->VerifyRegisterType(this, get_reg, expected_type)) {
3804 work_line_->SetResultRegisterType(this, reg_types_.Conflict());
Ian Rogers0c4a5062012-02-03 15:18:59 -08003805 return;
3806 }
3807 }
3808 // filled-array result goes into "result" register
Ian Rogersd8f69b02014-09-10 21:43:52 +00003809 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
Ian Rogers7b078e82014-09-10 14:44:24 -07003810 work_line_->SetResultRegisterType(this, precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003811 }
3812 }
3813}
3814
Sebastien Hertz5243e912013-05-21 10:55:07 +02003815void MethodVerifier::VerifyAGet(const Instruction* inst,
Ian Rogersd8f69b02014-09-10 21:43:52 +00003816 const RegType& insn_type, bool is_primitive) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003817 const RegType& index_type = work_line_->GetRegisterType(this, inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07003818 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003819 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07003820 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07003821 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegB_23x());
Ian Rogers89310de2012-02-01 13:47:30 -08003822 if (array_type.IsZero()) {
Nicolas Geoffray4824c272015-06-24 15:53:03 +01003823 have_pending_runtime_throw_failure_ = true;
Ian Rogers89310de2012-02-01 13:47:30 -08003824 // Null array class; this code path will fail at runtime. Infer a merge-able type from the
3825 // instruction type. TODO: have a proper notion of bottom here.
3826 if (!is_primitive || insn_type.IsCategory1Types()) {
3827 // Reference or category 1
Ian Rogers7b078e82014-09-10 14:44:24 -07003828 work_line_->SetRegisterType(this, inst->VRegA_23x(), reg_types_.Zero());
Ian Rogersd81871c2011-10-03 13:57:23 -07003829 } else {
Ian Rogers89310de2012-02-01 13:47:30 -08003830 // Category 2
Ian Rogers7b078e82014-09-10 14:44:24 -07003831 work_line_->SetRegisterTypeWide(this, inst->VRegA_23x(),
3832 reg_types_.FromCat2ConstLo(0, false),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003833 reg_types_.FromCat2ConstHi(0, false));
Ian Rogers89310de2012-02-01 13:47:30 -08003834 }
jeffhaofc3144e2012-02-01 17:21:15 -08003835 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003836 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aget";
Ian Rogers89310de2012-02-01 13:47:30 -08003837 } else {
3838 /* verify the class */
Ian Rogersd8f69b02014-09-10 21:43:52 +00003839 const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
jeffhaofc3144e2012-02-01 17:21:15 -08003840 if (!component_type.IsReferenceTypes() && !is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07003841 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08003842 << " source for aget-object";
3843 } else if (component_type.IsNonZeroReferenceTypes() && is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07003844 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08003845 << " source for category 1 aget";
3846 } else if (is_primitive && !insn_type.Equals(component_type) &&
3847 !((insn_type.IsInteger() && component_type.IsFloat()) ||
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003848 (insn_type.IsLong() && component_type.IsDouble()))) {
3849 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array type " << array_type
3850 << " incompatible with aget of type " << insn_type;
Ian Rogers89310de2012-02-01 13:47:30 -08003851 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003852 // Use knowledge of the field type which is stronger than the type inferred from the
3853 // instruction, which can't differentiate object types and ints from floats, longs from
3854 // doubles.
3855 if (!component_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003856 work_line_->SetRegisterType(this, inst->VRegA_23x(), component_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003857 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07003858 work_line_->SetRegisterTypeWide(this, inst->VRegA_23x(), component_type,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003859 component_type.HighHalf(&reg_types_));
3860 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003861 }
3862 }
3863 }
3864}
3865
Ian Rogersd8f69b02014-09-10 21:43:52 +00003866void MethodVerifier::VerifyPrimitivePut(const RegType& target_type, const RegType& insn_type,
Jeff Haofe1f7c82013-08-01 14:50:24 -07003867 const uint32_t vregA) {
3868 // Primitive assignability rules are weaker than regular assignability rules.
3869 bool instruction_compatible;
3870 bool value_compatible;
Ian Rogers7b078e82014-09-10 14:44:24 -07003871 const RegType& value_type = work_line_->GetRegisterType(this, vregA);
Jeff Haofe1f7c82013-08-01 14:50:24 -07003872 if (target_type.IsIntegralTypes()) {
Jeff Haoa4647482013-08-06 15:35:47 -07003873 instruction_compatible = target_type.Equals(insn_type);
Jeff Haofe1f7c82013-08-01 14:50:24 -07003874 value_compatible = value_type.IsIntegralTypes();
3875 } else if (target_type.IsFloat()) {
3876 instruction_compatible = insn_type.IsInteger(); // no put-float, so expect put-int
3877 value_compatible = value_type.IsFloatTypes();
3878 } else if (target_type.IsLong()) {
3879 instruction_compatible = insn_type.IsLong();
Andreas Gampe376fa682014-09-07 13:06:12 -07003880 // Additional register check: this is not checked statically (as part of VerifyInstructions),
3881 // as target_type depends on the resolved type of the field.
3882 if (instruction_compatible && work_line_->NumRegs() > vregA + 1) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003883 const RegType& value_type_hi = work_line_->GetRegisterType(this, vregA + 1);
Andreas Gampe376fa682014-09-07 13:06:12 -07003884 value_compatible = value_type.IsLongTypes() && value_type.CheckWidePair(value_type_hi);
3885 } else {
3886 value_compatible = false;
3887 }
Jeff Haofe1f7c82013-08-01 14:50:24 -07003888 } else if (target_type.IsDouble()) {
3889 instruction_compatible = insn_type.IsLong(); // no put-double, so expect put-long
Andreas Gampe376fa682014-09-07 13:06:12 -07003890 // Additional register check: this is not checked statically (as part of VerifyInstructions),
3891 // as target_type depends on the resolved type of the field.
3892 if (instruction_compatible && work_line_->NumRegs() > vregA + 1) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003893 const RegType& value_type_hi = work_line_->GetRegisterType(this, vregA + 1);
Andreas Gampe376fa682014-09-07 13:06:12 -07003894 value_compatible = value_type.IsDoubleTypes() && value_type.CheckWidePair(value_type_hi);
3895 } else {
3896 value_compatible = false;
3897 }
Jeff Haofe1f7c82013-08-01 14:50:24 -07003898 } else {
3899 instruction_compatible = false; // reference with primitive store
3900 value_compatible = false; // unused
3901 }
3902 if (!instruction_compatible) {
3903 // This is a global failure rather than a class change failure as the instructions and
3904 // the descriptors for the type should have been consistent within the same file at
3905 // compile time.
3906 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "put insn has type '" << insn_type
3907 << "' but expected type '" << target_type << "'";
3908 return;
3909 }
3910 if (!value_compatible) {
3911 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
3912 << " of type " << value_type << " but expected " << target_type << " for put";
3913 return;
3914 }
3915}
3916
Sebastien Hertz5243e912013-05-21 10:55:07 +02003917void MethodVerifier::VerifyAPut(const Instruction* inst,
Ian Rogersd8f69b02014-09-10 21:43:52 +00003918 const RegType& insn_type, bool is_primitive) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003919 const RegType& index_type = work_line_->GetRegisterType(this, inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07003920 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003921 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07003922 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07003923 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegB_23x());
Ian Rogers89310de2012-02-01 13:47:30 -08003924 if (array_type.IsZero()) {
Nicolas Geoffray66389fb2015-06-19 10:35:42 +01003925 // Null array type; this code path will fail at runtime.
3926 // Still check that the given value matches the instruction's type.
3927 work_line_->VerifyRegisterType(this, inst->VRegA_23x(), insn_type);
jeffhaofc3144e2012-02-01 17:21:15 -08003928 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003929 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aput";
Ian Rogers89310de2012-02-01 13:47:30 -08003930 } else {
Ian Rogersd8f69b02014-09-10 21:43:52 +00003931 const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
Jeff Haofe1f7c82013-08-01 14:50:24 -07003932 const uint32_t vregA = inst->VRegA_23x();
Jeff Haob24b4a72013-07-31 13:47:31 -07003933 if (is_primitive) {
Jeff Haofe1f7c82013-08-01 14:50:24 -07003934 VerifyPrimitivePut(component_type, insn_type, vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07003935 } else {
Jeff Haob24b4a72013-07-31 13:47:31 -07003936 if (!component_type.IsReferenceTypes()) {
3937 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
3938 << " source for aput-object";
3939 } else {
3940 // The instruction agrees with the type of array, confirm the value to be stored does too
3941 // Note: we use the instruction type (rather than the component type) for aput-object as
3942 // incompatible classes will be caught at runtime as an array store exception
Ian Rogers7b078e82014-09-10 14:44:24 -07003943 work_line_->VerifyRegisterType(this, vregA, insn_type);
Jeff Haob24b4a72013-07-31 13:47:31 -07003944 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003945 }
3946 }
3947 }
3948}
3949
Mathieu Chartierc7853442015-03-27 14:35:38 -07003950ArtField* MethodVerifier::GetStaticField(int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08003951 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3952 // Check access to class
Ian Rogersd8f69b02014-09-10 21:43:52 +00003953 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Brian Carlstrom7934ac22013-07-26 10:54:15 -07003954 if (klass_type.IsConflict()) { // bad class
Ian Rogersad0b3a32012-04-16 14:50:24 -07003955 AppendToLastFailMessage(StringPrintf(" in attempt to access static field %d (%s) in %s",
3956 field_idx, dex_file_->GetFieldName(field_id),
3957 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers7b078e82014-09-10 14:44:24 -07003958 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08003959 }
Elliott Hughesb25c3f62012-03-26 16:35:06 -07003960 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003961 return nullptr; // Can't resolve Class so no more to do here, will do checking at runtime.
Ian Rogers90040192011-12-16 08:54:29 -08003962 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07003963 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartierc7853442015-03-27 14:35:38 -07003964 ArtField* field = class_linker->ResolveFieldJLS(*dex_file_, field_idx, dex_cache_,
3965 class_loader_);
Ian Rogers7b078e82014-09-10 14:44:24 -07003966 if (field == nullptr) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07003967 VLOG(verifier) << "Unable to resolve static field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07003968 << dex_file_->GetFieldName(field_id) << ") in "
3969 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogers7b078e82014-09-10 14:44:24 -07003970 DCHECK(self_->IsExceptionPending());
3971 self_->ClearException();
3972 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003973 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
3974 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003975 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access static field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07003976 << " from " << GetDeclaringClass();
Ian Rogers7b078e82014-09-10 14:44:24 -07003977 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003978 } else if (!field->IsStatic()) {
3979 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field) << " to be static";
Ian Rogers7b078e82014-09-10 14:44:24 -07003980 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003981 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07003982 return field;
Ian Rogersd81871c2011-10-03 13:57:23 -07003983}
3984
Mathieu Chartierc7853442015-03-27 14:35:38 -07003985ArtField* MethodVerifier::GetInstanceField(const RegType& obj_type, int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08003986 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3987 // Check access to class
Ian Rogersd8f69b02014-09-10 21:43:52 +00003988 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003989 if (klass_type.IsConflict()) {
3990 AppendToLastFailMessage(StringPrintf(" in attempt to access instance field %d (%s) in %s",
3991 field_idx, dex_file_->GetFieldName(field_id),
3992 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers7b078e82014-09-10 14:44:24 -07003993 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08003994 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003995 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003996 return nullptr; // Can't resolve Class so no more to do here
Ian Rogers90040192011-12-16 08:54:29 -08003997 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07003998 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartierc7853442015-03-27 14:35:38 -07003999 ArtField* field = class_linker->ResolveFieldJLS(*dex_file_, field_idx, dex_cache_,
4000 class_loader_);
Ian Rogers7b078e82014-09-10 14:44:24 -07004001 if (field == nullptr) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07004002 VLOG(verifier) << "Unable to resolve instance field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07004003 << dex_file_->GetFieldName(field_id) << ") in "
4004 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogers7b078e82014-09-10 14:44:24 -07004005 DCHECK(self_->IsExceptionPending());
4006 self_->ClearException();
4007 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004008 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
4009 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004010 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access instance field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07004011 << " from " << GetDeclaringClass();
Ian Rogers7b078e82014-09-10 14:44:24 -07004012 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07004013 } else if (field->IsStatic()) {
4014 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field)
4015 << " to not be static";
Ian Rogers7b078e82014-09-10 14:44:24 -07004016 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07004017 } else if (obj_type.IsZero()) {
4018 // Cannot infer and check type, however, access will cause null pointer exception
4019 return field;
Stephen Kyle695c5982014-08-22 15:03:07 +01004020 } else if (!obj_type.IsReferenceTypes()) {
4021 // Trying to read a field from something that isn't a reference
4022 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance field access on object that has "
4023 << "non-reference type " << obj_type;
Ian Rogers7b078e82014-09-10 14:44:24 -07004024 return nullptr;
Ian Rogerse1758fe2012-04-19 11:31:15 -07004025 } else {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08004026 mirror::Class* klass = field->GetDeclaringClass();
Ian Rogersd8f69b02014-09-10 21:43:52 +00004027 const RegType& field_klass =
Andreas Gampef23f33d2015-06-23 14:18:17 -07004028 FromClass(dex_file_->GetFieldDeclaringClassDescriptor(field_id),
4029 klass, klass->CannotBeAssignedFromOtherTypes());
Ian Rogersad0b3a32012-04-16 14:50:24 -07004030 if (obj_type.IsUninitializedTypes() &&
4031 (!IsConstructor() || GetDeclaringClass().Equals(obj_type) ||
4032 !field_klass.Equals(GetDeclaringClass()))) {
4033 // Field accesses through uninitialized references are only allowable for constructors where
4034 // the field is declared in this class
4035 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "cannot access instance field " << PrettyField(field)
Brian Carlstrom93c33962013-07-26 10:37:43 -07004036 << " of a not fully initialized object within the context"
4037 << " of " << PrettyMethod(dex_method_idx_, *dex_file_);
Ian Rogers7b078e82014-09-10 14:44:24 -07004038 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004039 } else if (!field_klass.IsAssignableFrom(obj_type)) {
4040 // Trying to access C1.field1 using reference of type C2, which is neither C1 or a sub-class
4041 // of C1. For resolution to occur the declared class of the field must be compatible with
4042 // obj_type, we've discovered this wasn't so, so report the field didn't exist.
4043 Fail(VERIFY_ERROR_NO_FIELD) << "cannot access instance field " << PrettyField(field)
4044 << " from object of type " << obj_type;
Ian Rogers7b078e82014-09-10 14:44:24 -07004045 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004046 } else {
4047 return field;
4048 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004049 }
4050}
4051
Andreas Gampe896df402014-10-20 22:25:29 -07004052template <MethodVerifier::FieldAccessType kAccType>
4053void MethodVerifier::VerifyISFieldAccess(const Instruction* inst, const RegType& insn_type,
4054 bool is_primitive, bool is_static) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02004055 uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
Mathieu Chartierc7853442015-03-27 14:35:38 -07004056 ArtField* field;
Ian Rogersb94a27b2011-10-26 00:33:41 -07004057 if (is_static) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07004058 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07004059 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004060 const RegType& object_type = work_line_->GetRegisterType(this, inst->VRegB_22c());
Ian Rogersf4028cc2011-11-02 14:56:39 -07004061 field = GetInstanceField(object_type, field_idx);
Andreas Gampe896df402014-10-20 22:25:29 -07004062 if (UNLIKELY(have_pending_hard_failure_)) {
4063 return;
4064 }
Ian Rogersb94a27b2011-10-26 00:33:41 -07004065 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00004066 const RegType* field_type = nullptr;
Ian Rogers7b078e82014-09-10 14:44:24 -07004067 if (field != nullptr) {
Andreas Gampe896df402014-10-20 22:25:29 -07004068 if (kAccType == FieldAccessType::kAccPut) {
4069 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
4070 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
4071 << " from other class " << GetDeclaringClass();
4072 return;
4073 }
4074 }
4075
Mathieu Chartierc7853442015-03-27 14:35:38 -07004076 mirror::Class* field_type_class =
4077 can_load_classes_ ? field->GetType<true>() : field->GetType<false>();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004078 if (field_type_class != nullptr) {
Andreas Gampef23f33d2015-06-23 14:18:17 -07004079 field_type = &FromClass(field->GetTypeDescriptor(), field_type_class,
4080 field_type_class->CannotBeAssignedFromOtherTypes());
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08004081 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004082 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
4083 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004084 }
Ian Rogers0d604842012-04-16 14:50:24 -07004085 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004086 if (field_type == nullptr) {
4087 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
4088 const char* descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
Mathieu Chartierbf99f772014-08-23 16:37:27 -07004089 field_type = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004090 }
Sebastien Hertz757b3042014-03-28 14:34:28 +01004091 DCHECK(field_type != nullptr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02004092 const uint32_t vregA = (is_static) ? inst->VRegA_21c() : inst->VRegA_22c();
Andreas Gampe896df402014-10-20 22:25:29 -07004093 static_assert(kAccType == FieldAccessType::kAccPut || kAccType == FieldAccessType::kAccGet,
4094 "Unexpected third access type");
4095 if (kAccType == FieldAccessType::kAccPut) {
4096 // sput or iput.
4097 if (is_primitive) {
4098 VerifyPrimitivePut(*field_type, insn_type, vregA);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004099 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004100 if (!insn_type.IsAssignableFrom(*field_type)) {
Vladimir Marko414000e2015-06-23 17:45:21 +01004101 // If the field type is not a reference, this is a global failure rather than
4102 // a class change failure as the instructions and the descriptors for the type
4103 // should have been consistent within the same file at compile time.
4104 VerifyError error = field_type->IsReferenceTypes() ? VERIFY_ERROR_BAD_CLASS_SOFT
4105 : VERIFY_ERROR_BAD_CLASS_HARD;
4106 Fail(error) << "expected field " << PrettyField(field)
4107 << " to be compatible with type '" << insn_type
4108 << "' but found type '" << *field_type
4109 << "' in put-object";
Andreas Gampe896df402014-10-20 22:25:29 -07004110 return;
4111 }
4112 work_line_->VerifyRegisterType(this, vregA, *field_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004113 }
Andreas Gampe896df402014-10-20 22:25:29 -07004114 } else if (kAccType == FieldAccessType::kAccGet) {
4115 // sget or iget.
4116 if (is_primitive) {
4117 if (field_type->Equals(insn_type) ||
4118 (field_type->IsFloat() && insn_type.IsInteger()) ||
4119 (field_type->IsDouble() && insn_type.IsLong())) {
4120 // expected that read is of the correct primitive type or that int reads are reading
4121 // floats or long reads are reading doubles
4122 } else {
4123 // This is a global failure rather than a class change failure as the instructions and
4124 // the descriptors for the type should have been consistent within the same file at
4125 // compile time
4126 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
4127 << " to be of type '" << insn_type
4128 << "' but found type '" << *field_type << "' in get";
4129 return;
4130 }
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08004131 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004132 if (!insn_type.IsAssignableFrom(*field_type)) {
Vladimir Marko414000e2015-06-23 17:45:21 +01004133 // If the field type is not a reference, this is a global failure rather than
4134 // a class change failure as the instructions and the descriptors for the type
4135 // should have been consistent within the same file at compile time.
4136 VerifyError error = field_type->IsReferenceTypes() ? VERIFY_ERROR_BAD_CLASS_SOFT
4137 : VERIFY_ERROR_BAD_CLASS_HARD;
4138 Fail(error) << "expected field " << PrettyField(field)
4139 << " to be compatible with type '" << insn_type
4140 << "' but found type '" << *field_type
4141 << "' in get-object";
Andreas Gampe890da292015-07-06 17:20:18 -07004142 if (error != VERIFY_ERROR_BAD_CLASS_HARD) {
4143 work_line_->SetRegisterType(this, vregA, reg_types_.Conflict());
4144 }
Andreas Gampe896df402014-10-20 22:25:29 -07004145 return;
4146 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004147 }
Andreas Gampe896df402014-10-20 22:25:29 -07004148 if (!field_type->IsLowHalf()) {
4149 work_line_->SetRegisterType(this, vregA, *field_type);
4150 } else {
4151 work_line_->SetRegisterTypeWide(this, vregA, *field_type, field_type->HighHalf(&reg_types_));
4152 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07004153 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004154 LOG(FATAL) << "Unexpected case.";
Ian Rogersd81871c2011-10-03 13:57:23 -07004155 }
4156}
4157
Mathieu Chartierc7853442015-03-27 14:35:38 -07004158ArtField* MethodVerifier::GetQuickFieldAccess(const Instruction* inst,
Ian Rogers98379392014-02-24 16:53:16 -08004159 RegisterLine* reg_line) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08004160 DCHECK(IsInstructionIGetQuickOrIPutQuick(inst->Opcode())) << inst->Opcode();
Ian Rogers7b078e82014-09-10 14:44:24 -07004161 const RegType& object_type = reg_line->GetRegisterType(this, inst->VRegB_22c());
Ian Rogers9bc54402014-04-17 16:40:01 -07004162 if (!object_type.HasClass()) {
4163 VLOG(verifier) << "Failed to get mirror::Class* from '" << object_type << "'";
4164 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004165 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004166 uint32_t field_offset = static_cast<uint32_t>(inst->VRegC_22c());
Mathieu Chartierc7853442015-03-27 14:35:38 -07004167 ArtField* const f = ArtField::FindInstanceFieldWithOffset(object_type.GetClass(), field_offset);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08004168 DCHECK_EQ(f->GetOffset().Uint32Value(), field_offset);
Sebastien Hertz479fc1e2014-04-04 17:51:34 +02004169 if (f == nullptr) {
4170 VLOG(verifier) << "Failed to find instance field at offset '" << field_offset
4171 << "' from '" << PrettyDescriptor(object_type.GetClass()) << "'";
4172 }
4173 return f;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004174}
4175
Andreas Gampe896df402014-10-20 22:25:29 -07004176template <MethodVerifier::FieldAccessType kAccType>
4177void MethodVerifier::VerifyQuickFieldAccess(const Instruction* inst, const RegType& insn_type,
4178 bool is_primitive) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07004179 DCHECK(Runtime::Current()->IsStarted() || verify_to_dump_);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004180
Mathieu Chartierc7853442015-03-27 14:35:38 -07004181 ArtField* field = GetQuickFieldAccess(inst, work_line_.get());
Ian Rogers7b078e82014-09-10 14:44:24 -07004182 if (field == nullptr) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004183 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field from " << inst->Name();
4184 return;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004185 }
Andreas Gampe896df402014-10-20 22:25:29 -07004186
4187 // For an IPUT_QUICK, we now test for final flag of the field.
4188 if (kAccType == FieldAccessType::kAccPut) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004189 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
4190 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004191 << " from other class " << GetDeclaringClass();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004192 return;
4193 }
4194 }
Andreas Gampe896df402014-10-20 22:25:29 -07004195
4196 // Get the field type.
4197 const RegType* field_type;
4198 {
Mathieu Chartierc7853442015-03-27 14:35:38 -07004199 mirror::Class* field_type_class = can_load_classes_ ? field->GetType<true>() :
4200 field->GetType<false>();
Andreas Gampe896df402014-10-20 22:25:29 -07004201
4202 if (field_type_class != nullptr) {
Andreas Gampef23f33d2015-06-23 14:18:17 -07004203 field_type = &FromClass(field->GetTypeDescriptor(), field_type_class,
4204 field_type_class->CannotBeAssignedFromOtherTypes());
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004205 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004206 Thread* self = Thread::Current();
4207 DCHECK(!can_load_classes_ || self->IsExceptionPending());
4208 self->ClearException();
4209 field_type = &reg_types_.FromDescriptor(field->GetDeclaringClass()->GetClassLoader(),
4210 field->GetTypeDescriptor(), false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004211 }
Andreas Gampe896df402014-10-20 22:25:29 -07004212 if (field_type == nullptr) {
4213 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field type from " << inst->Name();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004214 return;
4215 }
Andreas Gampe896df402014-10-20 22:25:29 -07004216 }
4217
4218 const uint32_t vregA = inst->VRegA_22c();
4219 static_assert(kAccType == FieldAccessType::kAccPut || kAccType == FieldAccessType::kAccGet,
4220 "Unexpected third access type");
4221 if (kAccType == FieldAccessType::kAccPut) {
4222 if (is_primitive) {
4223 // Primitive field assignability rules are weaker than regular assignability rules
4224 bool instruction_compatible;
4225 bool value_compatible;
4226 const RegType& value_type = work_line_->GetRegisterType(this, vregA);
4227 if (field_type->IsIntegralTypes()) {
4228 instruction_compatible = insn_type.IsIntegralTypes();
4229 value_compatible = value_type.IsIntegralTypes();
4230 } else if (field_type->IsFloat()) {
4231 instruction_compatible = insn_type.IsInteger(); // no [is]put-float, so expect [is]put-int
4232 value_compatible = value_type.IsFloatTypes();
4233 } else if (field_type->IsLong()) {
4234 instruction_compatible = insn_type.IsLong();
4235 value_compatible = value_type.IsLongTypes();
4236 } else if (field_type->IsDouble()) {
4237 instruction_compatible = insn_type.IsLong(); // no [is]put-double, so expect [is]put-long
4238 value_compatible = value_type.IsDoubleTypes();
4239 } else {
4240 instruction_compatible = false; // reference field with primitive store
4241 value_compatible = false; // unused
4242 }
4243 if (!instruction_compatible) {
4244 // This is a global failure rather than a class change failure as the instructions and
4245 // the descriptors for the type should have been consistent within the same file at
4246 // compile time
4247 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
4248 << " to be of type '" << insn_type
4249 << "' but found type '" << *field_type
4250 << "' in put";
4251 return;
4252 }
4253 if (!value_compatible) {
4254 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
4255 << " of type " << value_type
4256 << " but expected " << *field_type
4257 << " for store to " << PrettyField(field) << " in put";
4258 return;
4259 }
4260 } else {
4261 if (!insn_type.IsAssignableFrom(*field_type)) {
4262 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
4263 << " to be compatible with type '" << insn_type
4264 << "' but found type '" << *field_type
4265 << "' in put-object";
4266 return;
4267 }
4268 work_line_->VerifyRegisterType(this, vregA, *field_type);
4269 }
4270 } else if (kAccType == FieldAccessType::kAccGet) {
4271 if (is_primitive) {
4272 if (field_type->Equals(insn_type) ||
4273 (field_type->IsFloat() && insn_type.IsIntegralTypes()) ||
4274 (field_type->IsDouble() && insn_type.IsLongTypes())) {
4275 // expected that read is of the correct primitive type or that int reads are reading
4276 // floats or long reads are reading doubles
4277 } else {
4278 // This is a global failure rather than a class change failure as the instructions and
4279 // the descriptors for the type should have been consistent within the same file at
4280 // compile time
4281 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
4282 << " to be of type '" << insn_type
4283 << "' but found type '" << *field_type << "' in Get";
4284 return;
4285 }
4286 } else {
4287 if (!insn_type.IsAssignableFrom(*field_type)) {
4288 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
4289 << " to be compatible with type '" << insn_type
4290 << "' but found type '" << *field_type
4291 << "' in get-object";
4292 work_line_->SetRegisterType(this, vregA, reg_types_.Conflict());
4293 return;
4294 }
4295 }
4296 if (!field_type->IsLowHalf()) {
4297 work_line_->SetRegisterType(this, vregA, *field_type);
4298 } else {
4299 work_line_->SetRegisterTypeWide(this, vregA, *field_type, field_type->HighHalf(&reg_types_));
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004300 }
4301 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004302 LOG(FATAL) << "Unexpected case.";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004303 }
4304}
4305
Ian Rogers776ac1f2012-04-13 23:36:36 -07004306bool MethodVerifier::CheckNotMoveException(const uint16_t* insns, int insn_idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004307 if ((insns[insn_idx] & 0xff) == Instruction::MOVE_EXCEPTION) {
jeffhaod5347e02012-03-22 17:25:05 -07004308 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-exception";
Ian Rogersd81871c2011-10-03 13:57:23 -07004309 return false;
4310 }
4311 return true;
4312}
4313
Stephen Kyle9bc61992014-09-22 13:53:15 +01004314bool MethodVerifier::CheckNotMoveResult(const uint16_t* insns, int insn_idx) {
4315 if (((insns[insn_idx] & 0xff) >= Instruction::MOVE_RESULT) &&
4316 ((insns[insn_idx] & 0xff) <= Instruction::MOVE_RESULT_OBJECT)) {
4317 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-result*";
4318 return false;
4319 }
4320 return true;
4321}
4322
4323bool MethodVerifier::CheckNotMoveExceptionOrMoveResult(const uint16_t* insns, int insn_idx) {
4324 return (CheckNotMoveException(insns, insn_idx) && CheckNotMoveResult(insns, insn_idx));
4325}
4326
Ian Rogersebbdd872014-07-07 23:53:08 -07004327bool MethodVerifier::UpdateRegisters(uint32_t next_insn, RegisterLine* merge_line,
4328 bool update_merge_line) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004329 bool changed = true;
4330 RegisterLine* target_line = reg_table_.GetLine(next_insn);
4331 if (!insn_flags_[next_insn].IsVisitedOrChanged()) {
jeffhaobdb76512011-09-07 11:43:16 -07004332 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07004333 * We haven't processed this instruction before, and we haven't touched the registers here, so
4334 * there's nothing to "merge". Copy the registers over and mark it as changed. (This is the
4335 * only way a register can transition out of "unknown", so this is not just an optimization.)
jeffhaobdb76512011-09-07 11:43:16 -07004336 */
Ian Rogersb8c78592013-07-25 23:52:52 +00004337 if (!insn_flags_[next_insn].IsReturn()) {
4338 target_line->CopyFromLine(merge_line);
4339 } else {
Jeff Haob24b4a72013-07-31 13:47:31 -07004340 // Verify that the monitor stack is empty on return.
Ian Rogers7b078e82014-09-10 14:44:24 -07004341 if (!merge_line->VerifyMonitorStackEmpty(this)) {
Jeff Haob24b4a72013-07-31 13:47:31 -07004342 return false;
4343 }
Ian Rogersb8c78592013-07-25 23:52:52 +00004344 // For returns we only care about the operand to the return, all other registers are dead.
4345 // Initialize them as conflicts so they don't add to GC and deoptimization information.
4346 const Instruction* ret_inst = Instruction::At(code_item_->insns_ + next_insn);
4347 Instruction::Code opcode = ret_inst->Opcode();
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07004348 if (opcode == Instruction::RETURN_VOID || opcode == Instruction::RETURN_VOID_NO_BARRIER) {
Stephen Kyle7e541c92014-12-17 17:10:02 +00004349 SafelyMarkAllRegistersAsConflicts(this, target_line);
Ian Rogersb8c78592013-07-25 23:52:52 +00004350 } else {
4351 target_line->CopyFromLine(merge_line);
4352 if (opcode == Instruction::RETURN_WIDE) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004353 target_line->MarkAllRegistersAsConflictsExceptWide(this, ret_inst->VRegA_11x());
Ian Rogersb8c78592013-07-25 23:52:52 +00004354 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004355 target_line->MarkAllRegistersAsConflictsExcept(this, ret_inst->VRegA_11x());
Ian Rogersb8c78592013-07-25 23:52:52 +00004356 }
4357 }
4358 }
jeffhaobdb76512011-09-07 11:43:16 -07004359 } else {
Ian Rogers700a4022014-05-19 16:49:03 -07004360 std::unique_ptr<RegisterLine> copy(gDebugVerify ?
Ian Rogersd0fbd852013-09-24 18:17:04 -07004361 RegisterLine::Create(target_line->NumRegs(), this) :
Ian Rogers7b078e82014-09-10 14:44:24 -07004362 nullptr);
Ian Rogers7b0c5b42012-02-16 15:29:07 -08004363 if (gDebugVerify) {
4364 copy->CopyFromLine(target_line);
4365 }
Ian Rogers7b078e82014-09-10 14:44:24 -07004366 changed = target_line->MergeRegisters(this, merge_line);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004367 if (have_pending_hard_failure_) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004368 return false;
jeffhaobdb76512011-09-07 11:43:16 -07004369 }
Ian Rogers2c8a8572011-10-24 17:11:36 -07004370 if (gDebugVerify && changed) {
Elliott Hughes398f64b2012-03-26 18:05:48 -07004371 LogVerifyInfo() << "Merging at [" << reinterpret_cast<void*>(work_insn_idx_) << "]"
Elliott Hughesc073b072012-05-24 19:29:17 -07004372 << " to [" << reinterpret_cast<void*>(next_insn) << "]: " << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07004373 << copy->Dump(this) << " MERGE\n"
4374 << merge_line->Dump(this) << " ==\n"
4375 << target_line->Dump(this) << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07004376 }
Ian Rogersebbdd872014-07-07 23:53:08 -07004377 if (update_merge_line && changed) {
4378 merge_line->CopyFromLine(target_line);
4379 }
jeffhaobdb76512011-09-07 11:43:16 -07004380 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004381 if (changed) {
4382 insn_flags_[next_insn].SetChanged();
jeffhaobdb76512011-09-07 11:43:16 -07004383 }
4384 return true;
4385}
4386
Ian Rogers7b3ddd22013-02-21 15:19:52 -08004387InstructionFlags* MethodVerifier::CurrentInsnFlags() {
Ian Rogers776ac1f2012-04-13 23:36:36 -07004388 return &insn_flags_[work_insn_idx_];
4389}
4390
Ian Rogersd8f69b02014-09-10 21:43:52 +00004391const RegType& MethodVerifier::GetMethodReturnType() {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004392 if (return_type_ == nullptr) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07004393 if (mirror_method_ != nullptr) {
Ian Rogersded66a02014-10-28 18:12:55 -07004394 mirror::Class* return_type_class = mirror_method_->GetReturnType(can_load_classes_);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004395 if (return_type_class != nullptr) {
Andreas Gampef23f33d2015-06-23 14:18:17 -07004396 return_type_ = &FromClass(mirror_method_->GetReturnTypeDescriptor(),
4397 return_type_class,
4398 return_type_class->CannotBeAssignedFromOtherTypes());
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004399 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004400 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
4401 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004402 }
4403 }
4404 if (return_type_ == nullptr) {
4405 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
4406 const DexFile::ProtoId& proto_id = dex_file_->GetMethodPrototype(method_id);
4407 uint16_t return_type_idx = proto_id.return_type_idx_;
4408 const char* descriptor = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(return_type_idx));
Mathieu Chartierbf99f772014-08-23 16:37:27 -07004409 return_type_ = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004410 }
4411 }
4412 return *return_type_;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004413}
4414
Ian Rogersd8f69b02014-09-10 21:43:52 +00004415const RegType& MethodVerifier::GetDeclaringClass() {
Ian Rogers7b078e82014-09-10 14:44:24 -07004416 if (declaring_class_ == nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004417 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
Brian Carlstrom93c33962013-07-26 10:37:43 -07004418 const char* descriptor
4419 = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(method_id.class_idx_));
Mathieu Chartiere401d142015-04-22 13:56:20 -07004420 if (mirror_method_ != nullptr) {
Ian Rogers637c65b2013-05-31 11:46:00 -07004421 mirror::Class* klass = mirror_method_->GetDeclaringClass();
Andreas Gampef23f33d2015-06-23 14:18:17 -07004422 declaring_class_ = &FromClass(descriptor, klass,
4423 klass->CannotBeAssignedFromOtherTypes());
Ian Rogers637c65b2013-05-31 11:46:00 -07004424 } else {
Mathieu Chartierbf99f772014-08-23 16:37:27 -07004425 declaring_class_ = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers637c65b2013-05-31 11:46:00 -07004426 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07004427 }
Ian Rogers637c65b2013-05-31 11:46:00 -07004428 return *declaring_class_;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004429}
4430
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004431std::vector<int32_t> MethodVerifier::DescribeVRegs(uint32_t dex_pc) {
4432 RegisterLine* line = reg_table_.GetLine(dex_pc);
Sebastien Hertzaa0c00c2014-03-14 17:58:54 +01004433 DCHECK(line != nullptr) << "No register line at DEX pc " << StringPrintf("0x%x", dex_pc);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004434 std::vector<int32_t> result;
4435 for (size_t i = 0; i < line->NumRegs(); ++i) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004436 const RegType& type = line->GetRegisterType(this, i);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004437 if (type.IsConstant()) {
4438 result.push_back(type.IsPreciseConstant() ? kConstant : kImpreciseConstant);
Ian Rogers7b078e82014-09-10 14:44:24 -07004439 const ConstantType* const_val = down_cast<const ConstantType*>(&type);
4440 result.push_back(const_val->ConstantValue());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004441 } else if (type.IsConstantLo()) {
4442 result.push_back(type.IsPreciseConstantLo() ? kConstant : kImpreciseConstant);
Ian Rogers7b078e82014-09-10 14:44:24 -07004443 const ConstantType* const_val = down_cast<const ConstantType*>(&type);
4444 result.push_back(const_val->ConstantValueLo());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004445 } else if (type.IsConstantHi()) {
4446 result.push_back(type.IsPreciseConstantHi() ? kConstant : kImpreciseConstant);
Ian Rogers7b078e82014-09-10 14:44:24 -07004447 const ConstantType* const_val = down_cast<const ConstantType*>(&type);
4448 result.push_back(const_val->ConstantValueHi());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004449 } else if (type.IsIntegralTypes()) {
4450 result.push_back(kIntVReg);
4451 result.push_back(0);
4452 } else if (type.IsFloat()) {
4453 result.push_back(kFloatVReg);
4454 result.push_back(0);
4455 } else if (type.IsLong()) {
4456 result.push_back(kLongLoVReg);
4457 result.push_back(0);
4458 result.push_back(kLongHiVReg);
4459 result.push_back(0);
4460 ++i;
4461 } else if (type.IsDouble()) {
4462 result.push_back(kDoubleLoVReg);
4463 result.push_back(0);
4464 result.push_back(kDoubleHiVReg);
4465 result.push_back(0);
4466 ++i;
4467 } else if (type.IsUndefined() || type.IsConflict() || type.IsHighHalf()) {
4468 result.push_back(kUndefined);
4469 result.push_back(0);
4470 } else {
Ian Rogers7b3ddd22013-02-21 15:19:52 -08004471 CHECK(type.IsNonZeroReferenceTypes());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004472 result.push_back(kReferenceVReg);
4473 result.push_back(0);
4474 }
4475 }
4476 return result;
4477}
4478
Ian Rogersd8f69b02014-09-10 21:43:52 +00004479const RegType& MethodVerifier::DetermineCat1Constant(int32_t value, bool precise) {
Sebastien Hertz849600b2013-12-20 10:28:08 +01004480 if (precise) {
4481 // Precise constant type.
4482 return reg_types_.FromCat1Const(value, true);
4483 } else {
4484 // Imprecise constant type.
4485 if (value < -32768) {
4486 return reg_types_.IntConstant();
4487 } else if (value < -128) {
4488 return reg_types_.ShortConstant();
4489 } else if (value < 0) {
4490 return reg_types_.ByteConstant();
4491 } else if (value == 0) {
4492 return reg_types_.Zero();
4493 } else if (value == 1) {
4494 return reg_types_.One();
4495 } else if (value < 128) {
4496 return reg_types_.PosByteConstant();
4497 } else if (value < 32768) {
4498 return reg_types_.PosShortConstant();
4499 } else if (value < 65536) {
4500 return reg_types_.CharConstant();
4501 } else {
4502 return reg_types_.IntConstant();
4503 }
4504 }
4505}
4506
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004507void MethodVerifier::Init() {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08004508 art::verifier::RegTypeCache::Init();
Brian Carlstrome7d856b2012-01-11 18:10:55 -08004509}
4510
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004511void MethodVerifier::Shutdown() {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08004512 verifier::RegTypeCache::ShutDown();
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08004513}
jeffhaod1224c72012-02-29 13:43:08 -08004514
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -07004515void MethodVerifier::VisitStaticRoots(RootVisitor* visitor) {
4516 RegTypeCache::VisitStaticRoots(visitor);
Mathieu Chartier7c438b12014-09-12 17:01:24 -07004517}
4518
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -07004519void MethodVerifier::VisitRoots(RootVisitor* visitor, const RootInfo& root_info) {
4520 reg_types_.VisitRoots(visitor, root_info);
Mathieu Chartierc528dba2013-11-26 12:00:11 -08004521}
4522
Andreas Gampef23f33d2015-06-23 14:18:17 -07004523const RegType& MethodVerifier::FromClass(const char* descriptor,
4524 mirror::Class* klass,
4525 bool precise) {
4526 DCHECK(klass != nullptr);
4527 if (precise && !klass->IsInstantiable() && !klass->IsPrimitive()) {
4528 Fail(VerifyError::VERIFY_ERROR_NO_CLASS) << "Could not create precise reference for "
4529 << "non-instantiable klass " << descriptor;
4530 precise = false;
4531 }
4532 return reg_types_.FromClass(descriptor, klass, precise);
4533}
4534
Ian Rogersd81871c2011-10-03 13:57:23 -07004535} // namespace verifier
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07004536} // namespace art