blob: d63b4556687e3a817be271a5624d2ad837d6bf8e [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);
Igor Murashkin4d7b75f2015-07-21 17:03:36 -0700332
333 if (UNLIKELY(verifier.have_pending_experimental_failure_)) {
334 // Failed due to being forced into interpreter. This is ok because
335 // we just want to skip verification.
336 result = kSoftFailure;
337 } else {
338 CHECK(verifier.have_pending_hard_failure_);
339 verifier.DumpFailures(LOG(INFO) << "Verification error in "
340 << PrettyMethod(method_idx, *dex_file) << "\n");
341 result = kHardFailure;
342 }
jeffhaof56197c2012-03-05 18:01:54 -0800343 if (gDebugVerify) {
Ian Rogers46960fe2014-05-23 10:43:43 -0700344 std::cout << "\n" << verifier.info_messages_.str();
345 verifier.Dump(std::cout);
jeffhaof56197c2012-03-05 18:01:54 -0800346 }
jeffhaof56197c2012-03-05 18:01:54 -0800347 }
Mathieu Chartier8e219ae2014-08-19 14:29:46 -0700348 if (kTimeVerifyMethod) {
349 uint64_t duration_ns = NanoTime() - start_ns;
350 if (duration_ns > MsToNs(100)) {
351 LOG(WARNING) << "Verification of " << PrettyMethod(method_idx, *dex_file)
Andreas Gampea4f5bf62015-05-18 20:50:12 -0700352 << " took " << PrettyDuration(duration_ns)
353 << (IsLargeMethod(code_item) ? " (large method)" : "");
Mathieu Chartier8e219ae2014-08-19 14:29:46 -0700354 }
Ian Rogersc8982582012-09-07 16:53:25 -0700355 }
356 return result;
jeffhaof56197c2012-03-05 18:01:54 -0800357}
358
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100359MethodVerifier* MethodVerifier::VerifyMethodAndDump(Thread* self,
360 VariableIndentationOutputStream* vios,
361 uint32_t dex_method_idx,
362 const DexFile* dex_file,
363 Handle<mirror::DexCache> dex_cache,
364 Handle<mirror::ClassLoader> class_loader,
365 const DexFile::ClassDef* class_def,
366 const DexFile::CodeItem* code_item,
367 ArtMethod* method,
368 uint32_t method_access_flags) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700369 MethodVerifier* verifier = new MethodVerifier(self, dex_file, dex_cache, class_loader,
370 class_def, code_item, dex_method_idx, method,
371 method_access_flags, true, true, true, true);
372 verifier->Verify();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100373 verifier->DumpFailures(vios->Stream());
374 vios->Stream() << verifier->info_messages_.str();
Andreas Gampe5cbcde22014-09-16 14:59:49 -0700375 // Only dump and return if no hard failures. Otherwise the verifier may be not fully initialized
376 // and querying any info is dangerous/can abort.
377 if (verifier->have_pending_hard_failure_) {
378 delete verifier;
379 return nullptr;
380 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100381 verifier->Dump(vios);
Andreas Gampe5cbcde22014-09-16 14:59:49 -0700382 return verifier;
383 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800384}
385
Ian Rogers7b078e82014-09-10 14:44:24 -0700386MethodVerifier::MethodVerifier(Thread* self,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700387 const DexFile* dex_file, Handle<mirror::DexCache> dex_cache,
388 Handle<mirror::ClassLoader> class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700389 const DexFile::ClassDef* class_def,
Mathieu Chartier590fee92013-09-13 13:46:47 -0700390 const DexFile::CodeItem* code_item, uint32_t dex_method_idx,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700391 ArtMethod* method, uint32_t method_access_flags,
Ian Rogers46960fe2014-05-23 10:43:43 -0700392 bool can_load_classes, bool allow_soft_failures,
Mathieu Chartier4306ef82014-12-19 18:41:47 -0800393 bool need_precise_constants, bool verify_to_dump,
394 bool allow_thread_suspension)
Ian Rogers7b078e82014-09-10 14:44:24 -0700395 : self_(self),
396 reg_types_(can_load_classes),
Andreas Gamped5ad72f2015-06-26 17:33:47 -0700397 work_insn_idx_(DexFile::kDexNoIndex),
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800398 dex_method_idx_(dex_method_idx),
Ian Rogers637c65b2013-05-31 11:46:00 -0700399 mirror_method_(method),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700400 method_access_flags_(method_access_flags),
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700401 return_type_(nullptr),
jeffhaof56197c2012-03-05 18:01:54 -0800402 dex_file_(dex_file),
403 dex_cache_(dex_cache),
404 class_loader_(class_loader),
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700405 class_def_(class_def),
jeffhaof56197c2012-03-05 18:01:54 -0800406 code_item_(code_item),
Ian Rogers7b078e82014-09-10 14:44:24 -0700407 declaring_class_(nullptr),
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700408 interesting_dex_pc_(-1),
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700409 monitor_enter_dex_pcs_(nullptr),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700410 have_pending_hard_failure_(false),
jeffhaofaf459e2012-08-31 15:32:47 -0700411 have_pending_runtime_throw_failure_(false),
Igor Murashkin4d7b75f2015-07-21 17:03:36 -0700412 have_pending_experimental_failure_(false),
Andreas Gamped12e7822015-06-25 10:26:40 -0700413 have_any_pending_runtime_throw_failure_(false),
jeffhaof56197c2012-03-05 18:01:54 -0800414 new_instance_count_(0),
Elliott Hughes80537bb2013-01-04 16:37:26 -0800415 monitor_enter_count_(0),
Jeff Haoee988952013-04-16 14:23:47 -0700416 can_load_classes_(can_load_classes),
Sebastien Hertz4d4adb12013-07-24 16:14:19 +0200417 allow_soft_failures_(allow_soft_failures),
Ian Rogers46960fe2014-05-23 10:43:43 -0700418 need_precise_constants_(need_precise_constants),
Sebastien Hertz4d4adb12013-07-24 16:14:19 +0200419 has_check_casts_(false),
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700420 has_virtual_or_interface_invokes_(false),
Mathieu Chartier4306ef82014-12-19 18:41:47 -0800421 verify_to_dump_(verify_to_dump),
Andreas Gamped5ad72f2015-06-26 17:33:47 -0700422 allow_thread_suspension_(allow_thread_suspension),
423 link_(nullptr) {
Mathieu Chartierd0ad2ee2015-03-31 14:59:59 -0700424 self->PushVerifier(this);
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700425 DCHECK(class_def != nullptr);
jeffhaof56197c2012-03-05 18:01:54 -0800426}
427
Mathieu Chartier590fee92013-09-13 13:46:47 -0700428MethodVerifier::~MethodVerifier() {
Mathieu Chartierd0ad2ee2015-03-31 14:59:59 -0700429 Thread::Current()->PopVerifier(this);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700430 STLDeleteElements(&failure_messages_);
431}
432
Mathieu Chartiere401d142015-04-22 13:56:20 -0700433void MethodVerifier::FindLocksAtDexPc(ArtMethod* m, uint32_t dex_pc,
Ian Rogers46960fe2014-05-23 10:43:43 -0700434 std::vector<uint32_t>* monitor_enter_dex_pcs) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700435 StackHandleScope<2> hs(Thread::Current());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700436 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache()));
437 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader()));
Mathieu Chartiere401d142015-04-22 13:56:20 -0700438 MethodVerifier verifier(hs.Self(), m->GetDexFile(), dex_cache, class_loader, &m->GetClassDef(),
439 m->GetCodeItem(), m->GetDexMethodIndex(), m, m->GetAccessFlags(),
Mathieu Chartier4306ef82014-12-19 18:41:47 -0800440 false, true, false, false);
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700441 verifier.interesting_dex_pc_ = dex_pc;
Ian Rogers46960fe2014-05-23 10:43:43 -0700442 verifier.monitor_enter_dex_pcs_ = monitor_enter_dex_pcs;
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700443 verifier.FindLocksAtDexPc();
444}
445
Andreas Gampecb3c08f2014-09-18 13:16:38 -0700446static bool HasMonitorEnterInstructions(const DexFile::CodeItem* const code_item) {
447 const Instruction* inst = Instruction::At(code_item->insns_);
448
449 uint32_t insns_size = code_item->insns_size_in_code_units_;
450 for (uint32_t dex_pc = 0; dex_pc < insns_size;) {
451 if (inst->Opcode() == Instruction::MONITOR_ENTER) {
452 return true;
453 }
454
455 dex_pc += inst->SizeInCodeUnits();
456 inst = inst->Next();
457 }
458
459 return false;
460}
461
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700462void MethodVerifier::FindLocksAtDexPc() {
Ian Rogers7b078e82014-09-10 14:44:24 -0700463 CHECK(monitor_enter_dex_pcs_ != nullptr);
464 CHECK(code_item_ != nullptr); // This only makes sense for methods with code.
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700465
Andreas Gampecb3c08f2014-09-18 13:16:38 -0700466 // Quick check whether there are any monitor_enter instructions at all.
467 if (!HasMonitorEnterInstructions(code_item_)) {
468 return;
469 }
470
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700471 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
472 // verification. In practice, the phase we want relies on data structures set up by all the
473 // earlier passes, so we just run the full method verification and bail out early when we've
474 // got what we wanted.
475 Verify();
476}
477
Mathieu Chartiere401d142015-04-22 13:56:20 -0700478ArtField* MethodVerifier::FindAccessedFieldAtDexPc(ArtMethod* m, uint32_t dex_pc) {
479 StackHandleScope<2> hs(Thread::Current());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700480 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache()));
481 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader()));
Mathieu Chartiere401d142015-04-22 13:56:20 -0700482 MethodVerifier verifier(hs.Self(), m->GetDexFile(), dex_cache, class_loader, &m->GetClassDef(),
483 m->GetCodeItem(), m->GetDexMethodIndex(), m, m->GetAccessFlags(), true,
484 true, false, true);
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200485 return verifier.FindAccessedFieldAtDexPc(dex_pc);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200486}
487
Mathieu Chartierc7853442015-03-27 14:35:38 -0700488ArtField* MethodVerifier::FindAccessedFieldAtDexPc(uint32_t dex_pc) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700489 CHECK(code_item_ != nullptr); // This only makes sense for methods with code.
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200490
491 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
492 // verification. In practice, the phase we want relies on data structures set up by all the
493 // earlier passes, so we just run the full method verification and bail out early when we've
494 // got what we wanted.
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200495 bool success = Verify();
496 if (!success) {
Ian Rogers9bc54402014-04-17 16:40:01 -0700497 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200498 }
499 RegisterLine* register_line = reg_table_.GetLine(dex_pc);
Ian Rogers7b078e82014-09-10 14:44:24 -0700500 if (register_line == nullptr) {
Ian Rogers9bc54402014-04-17 16:40:01 -0700501 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200502 }
503 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
504 return GetQuickFieldAccess(inst, register_line);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200505}
506
Mathieu Chartiere401d142015-04-22 13:56:20 -0700507ArtMethod* MethodVerifier::FindInvokedMethodAtDexPc(ArtMethod* m, uint32_t dex_pc) {
508 StackHandleScope<2> hs(Thread::Current());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700509 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache()));
510 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader()));
Mathieu Chartiere401d142015-04-22 13:56:20 -0700511 MethodVerifier verifier(hs.Self(), m->GetDexFile(), dex_cache, class_loader, &m->GetClassDef(),
512 m->GetCodeItem(), m->GetDexMethodIndex(), m, m->GetAccessFlags(), true,
513 true, false, true);
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200514 return verifier.FindInvokedMethodAtDexPc(dex_pc);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200515}
516
Mathieu Chartiere401d142015-04-22 13:56:20 -0700517ArtMethod* MethodVerifier::FindInvokedMethodAtDexPc(uint32_t dex_pc) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700518 CHECK(code_item_ != nullptr); // This only makes sense for methods with code.
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200519
520 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
521 // verification. In practice, the phase we want relies on data structures set up by all the
522 // earlier passes, so we just run the full method verification and bail out early when we've
523 // got what we wanted.
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200524 bool success = Verify();
525 if (!success) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700526 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200527 }
528 RegisterLine* register_line = reg_table_.GetLine(dex_pc);
Ian Rogers7b078e82014-09-10 14:44:24 -0700529 if (register_line == nullptr) {
530 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200531 }
532 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
533 const bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
Mathieu Chartier091d2382015-03-06 10:59:06 -0800534 return GetQuickInvokedMethod(inst, register_line, is_range, false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200535}
536
Mathieu Chartiere401d142015-04-22 13:56:20 -0700537SafeMap<uint32_t, std::set<uint32_t>> MethodVerifier::FindStringInitMap(ArtMethod* m) {
Jeff Hao848f70a2014-01-15 13:49:50 -0800538 Thread* self = Thread::Current();
Mathieu Chartiere401d142015-04-22 13:56:20 -0700539 StackHandleScope<2> hs(self);
Jeff Hao848f70a2014-01-15 13:49:50 -0800540 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache()));
541 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader()));
Jeff Hao848f70a2014-01-15 13:49:50 -0800542 MethodVerifier verifier(self, m->GetDexFile(), dex_cache, class_loader, &m->GetClassDef(),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700543 m->GetCodeItem(), m->GetDexMethodIndex(), m, m->GetAccessFlags(),
Jeff Hao848f70a2014-01-15 13:49:50 -0800544 true, true, false, true);
545 return verifier.FindStringInitMap();
546}
547
548SafeMap<uint32_t, std::set<uint32_t>>& MethodVerifier::FindStringInitMap() {
549 Verify();
550 return GetStringInitPcRegMap();
551}
552
Ian Rogersad0b3a32012-04-16 14:50:24 -0700553bool MethodVerifier::Verify() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700554 // If there aren't any instructions, make sure that's expected, then exit successfully.
Ian Rogers7b078e82014-09-10 14:44:24 -0700555 if (code_item_ == nullptr) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700556 if ((method_access_flags_ & (kAccNative | kAccAbstract)) == 0) {
jeffhaod5347e02012-03-22 17:25:05 -0700557 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "zero-length code in concrete non-native method";
jeffhaobdb76512011-09-07 11:43:16 -0700558 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -0700559 } else {
560 return true;
jeffhaobdb76512011-09-07 11:43:16 -0700561 }
jeffhaobdb76512011-09-07 11:43:16 -0700562 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700563 // Sanity-check the register counts. ins + locals = registers, so make sure that ins <= registers.
564 if (code_item_->ins_size_ > code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700565 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad register counts (ins=" << code_item_->ins_size_
566 << " regs=" << code_item_->registers_size_;
Ian Rogersd81871c2011-10-03 13:57:23 -0700567 return false;
jeffhaobdb76512011-09-07 11:43:16 -0700568 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700569 // Allocate and initialize an array to hold instruction data.
Ian Rogers7b3ddd22013-02-21 15:19:52 -0800570 insn_flags_.reset(new InstructionFlags[code_item_->insns_size_in_code_units_]());
Ian Rogersd81871c2011-10-03 13:57:23 -0700571 // Run through the instructions and see if the width checks out.
572 bool result = ComputeWidthsAndCountOps();
573 // Flag instructions guarded by a "try" block and check exception handlers.
574 result = result && ScanTryCatchBlocks();
575 // Perform static instruction verification.
576 result = result && VerifyInstructions();
Ian Rogersad0b3a32012-04-16 14:50:24 -0700577 // Perform code-flow analysis and return.
Vladimir Marko2b5eaa22013-12-13 13:59:30 +0000578 result = result && VerifyCodeFlow();
579 // Compute information for compiler.
580 if (result && Runtime::Current()->IsCompiler()) {
581 result = Runtime::Current()->GetCompilerCallbacks()->MethodVerified(this);
582 }
583 return result;
jeffhaoba5ebb92011-08-25 17:24:37 -0700584}
585
Ian Rogers776ac1f2012-04-13 23:36:36 -0700586std::ostream& MethodVerifier::Fail(VerifyError error) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700587 switch (error) {
588 case VERIFY_ERROR_NO_CLASS:
589 case VERIFY_ERROR_NO_FIELD:
590 case VERIFY_ERROR_NO_METHOD:
591 case VERIFY_ERROR_ACCESS_CLASS:
592 case VERIFY_ERROR_ACCESS_FIELD:
593 case VERIFY_ERROR_ACCESS_METHOD:
Ian Rogers08f753d2012-08-24 14:35:25 -0700594 case VERIFY_ERROR_INSTANTIATION:
595 case VERIFY_ERROR_CLASS_CHANGE:
Igor Murashkin158f35c2015-06-10 15:55:30 -0700596 case VERIFY_ERROR_FORCE_INTERPRETER:
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800597 if (Runtime::Current()->IsAotCompiler() || !can_load_classes_) {
jeffhaofaf459e2012-08-31 15:32:47 -0700598 // If we're optimistically running verification at compile time, turn NO_xxx, ACCESS_xxx,
599 // class change and instantiation errors into soft verification errors so that we re-verify
600 // at runtime. We may fail to find or to agree on access because of not yet available class
601 // loaders, or class loaders that will differ at runtime. In these cases, we don't want to
602 // affect the soundness of the code being compiled. Instead, the generated code runs "slow
603 // paths" that dynamically perform the verification and cause the behavior to be that akin
604 // to an interpreter.
605 error = VERIFY_ERROR_BAD_CLASS_SOFT;
606 } else {
Jeff Haoa3faaf42013-09-03 19:07:00 -0700607 // If we fail again at runtime, mark that this instruction would throw and force this
608 // method to be executed using the interpreter with checks.
jeffhaofaf459e2012-08-31 15:32:47 -0700609 have_pending_runtime_throw_failure_ = true;
Andreas Gamped7f8d052015-03-12 11:05:47 -0700610
611 // We need to save the work_line if the instruction wasn't throwing before. Otherwise we'll
612 // try to merge garbage.
613 // Note: this assumes that Fail is called before we do any work_line modifications.
Andreas Gamped5ad72f2015-06-26 17:33:47 -0700614 // Note: this can fail before we touch any instruction, for the signature of a method. So
615 // add a check.
616 if (work_insn_idx_ < DexFile::kDexNoIndex) {
617 const uint16_t* insns = code_item_->insns_ + work_insn_idx_;
618 const Instruction* inst = Instruction::At(insns);
619 int opcode_flags = Instruction::FlagsOf(inst->Opcode());
Andreas Gamped7f8d052015-03-12 11:05:47 -0700620
Andreas Gamped5ad72f2015-06-26 17:33:47 -0700621 if ((opcode_flags & Instruction::kThrow) == 0 && CurrentInsnFlags()->IsInTry()) {
622 saved_line_->CopyFromLine(work_line_.get());
623 }
Andreas Gamped7f8d052015-03-12 11:05:47 -0700624 }
jeffhaofaf459e2012-08-31 15:32:47 -0700625 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700626 break;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700627 // Indication that verification should be retried at runtime.
628 case VERIFY_ERROR_BAD_CLASS_SOFT:
Jeff Haoee988952013-04-16 14:23:47 -0700629 if (!allow_soft_failures_) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700630 have_pending_hard_failure_ = true;
631 }
632 break;
jeffhaod5347e02012-03-22 17:25:05 -0700633 // Hard verification failures at compile time will still fail at runtime, so the class is
634 // marked as rejected to prevent it from being compiled.
Ian Rogersad0b3a32012-04-16 14:50:24 -0700635 case VERIFY_ERROR_BAD_CLASS_HARD: {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800636 if (Runtime::Current()->IsAotCompiler()) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700637 ClassReference ref(dex_file_, dex_file_->GetIndexForClassDef(*class_def_));
Vladimir Marko2b5eaa22013-12-13 13:59:30 +0000638 Runtime::Current()->GetCompilerCallbacks()->ClassRejected(ref);
jeffhaod1224c72012-02-29 13:43:08 -0800639 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700640 have_pending_hard_failure_ = true;
641 break;
Ian Rogers47a05882012-02-03 12:23:33 -0800642 }
643 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700644 failures_.push_back(error);
Elena Sayapina78480ec2014-08-15 15:52:42 +0700645 std::string location(StringPrintf("%s: [0x%X] ", PrettyMethod(dex_method_idx_, *dex_file_).c_str(),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700646 work_insn_idx_));
Elena Sayapina78480ec2014-08-15 15:52:42 +0700647 std::ostringstream* failure_message = new std::ostringstream(location, std::ostringstream::ate);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700648 failure_messages_.push_back(failure_message);
649 return *failure_message;
650}
651
Ian Rogers576ca0c2014-06-06 15:58:22 -0700652std::ostream& MethodVerifier::LogVerifyInfo() {
653 return info_messages_ << "VFY: " << PrettyMethod(dex_method_idx_, *dex_file_)
654 << '[' << reinterpret_cast<void*>(work_insn_idx_) << "] : ";
655}
656
Ian Rogersad0b3a32012-04-16 14:50:24 -0700657void MethodVerifier::PrependToLastFailMessage(std::string prepend) {
658 size_t failure_num = failure_messages_.size();
659 DCHECK_NE(failure_num, 0U);
660 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
661 prepend += last_fail_message->str();
Elena Sayapina78480ec2014-08-15 15:52:42 +0700662 failure_messages_[failure_num - 1] = new std::ostringstream(prepend, std::ostringstream::ate);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700663 delete last_fail_message;
664}
665
666void MethodVerifier::AppendToLastFailMessage(std::string append) {
667 size_t failure_num = failure_messages_.size();
668 DCHECK_NE(failure_num, 0U);
669 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
670 (*last_fail_message) << append;
Ian Rogers47a05882012-02-03 12:23:33 -0800671}
672
Ian Rogers776ac1f2012-04-13 23:36:36 -0700673bool MethodVerifier::ComputeWidthsAndCountOps() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700674 const uint16_t* insns = code_item_->insns_;
675 size_t insns_size = code_item_->insns_size_in_code_units_;
676 const Instruction* inst = Instruction::At(insns);
jeffhaobdb76512011-09-07 11:43:16 -0700677 size_t new_instance_count = 0;
678 size_t monitor_enter_count = 0;
Ian Rogersd81871c2011-10-03 13:57:23 -0700679 size_t dex_pc = 0;
jeffhaobdb76512011-09-07 11:43:16 -0700680
Ian Rogersd81871c2011-10-03 13:57:23 -0700681 while (dex_pc < insns_size) {
jeffhaobdb76512011-09-07 11:43:16 -0700682 Instruction::Code opcode = inst->Opcode();
Ian Rogersa9a82542013-10-04 11:17:26 -0700683 switch (opcode) {
684 case Instruction::APUT_OBJECT:
685 case Instruction::CHECK_CAST:
686 has_check_casts_ = true;
687 break;
688 case Instruction::INVOKE_VIRTUAL:
689 case Instruction::INVOKE_VIRTUAL_RANGE:
690 case Instruction::INVOKE_INTERFACE:
691 case Instruction::INVOKE_INTERFACE_RANGE:
692 has_virtual_or_interface_invokes_ = true;
693 break;
694 case Instruction::MONITOR_ENTER:
695 monitor_enter_count++;
696 break;
697 case Instruction::NEW_INSTANCE:
698 new_instance_count++;
699 break;
700 default:
701 break;
jeffhaobdb76512011-09-07 11:43:16 -0700702 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700703 size_t inst_size = inst->SizeInCodeUnits();
Ian Rogers7b078e82014-09-10 14:44:24 -0700704 insn_flags_[dex_pc].SetIsOpcode();
Ian Rogersd81871c2011-10-03 13:57:23 -0700705 dex_pc += inst_size;
Ian Rogers7b078e82014-09-10 14:44:24 -0700706 inst = inst->RelativeAt(inst_size);
jeffhaobdb76512011-09-07 11:43:16 -0700707 }
708
Ian Rogersd81871c2011-10-03 13:57:23 -0700709 if (dex_pc != insns_size) {
jeffhaod5347e02012-03-22 17:25:05 -0700710 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "code did not end where expected ("
711 << dex_pc << " vs. " << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700712 return false;
713 }
714
Ian Rogersd81871c2011-10-03 13:57:23 -0700715 new_instance_count_ = new_instance_count;
716 monitor_enter_count_ = monitor_enter_count;
jeffhaobdb76512011-09-07 11:43:16 -0700717 return true;
718}
719
Ian Rogers776ac1f2012-04-13 23:36:36 -0700720bool MethodVerifier::ScanTryCatchBlocks() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700721 uint32_t tries_size = code_item_->tries_size_;
jeffhaobdb76512011-09-07 11:43:16 -0700722 if (tries_size == 0) {
723 return true;
724 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700725 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Ian Rogers0571d352011-11-03 19:51:38 -0700726 const DexFile::TryItem* tries = DexFile::GetTryItems(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -0700727
728 for (uint32_t idx = 0; idx < tries_size; idx++) {
729 const DexFile::TryItem* try_item = &tries[idx];
730 uint32_t start = try_item->start_addr_;
731 uint32_t end = start + try_item->insn_count_;
jeffhaobdb76512011-09-07 11:43:16 -0700732 if ((start >= end) || (start >= insns_size) || (end > insns_size)) {
jeffhaod5347e02012-03-22 17:25:05 -0700733 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad exception entry: startAddr=" << start
734 << " endAddr=" << end << " (size=" << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700735 return false;
736 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700737 if (!insn_flags_[start].IsOpcode()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700738 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
739 << "'try' block starts inside an instruction (" << start << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700740 return false;
741 }
Ian Rogers7b078e82014-09-10 14:44:24 -0700742 uint32_t dex_pc = start;
743 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
744 while (dex_pc < end) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700745 insn_flags_[dex_pc].SetInTry();
Ian Rogers7b078e82014-09-10 14:44:24 -0700746 size_t insn_size = inst->SizeInCodeUnits();
747 dex_pc += insn_size;
748 inst = inst->RelativeAt(insn_size);
jeffhaobdb76512011-09-07 11:43:16 -0700749 }
750 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800751 // Iterate over each of the handlers to verify target addresses.
Ian Rogers13735952014-10-08 12:43:28 -0700752 const uint8_t* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -0700753 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700754 ClassLinker* linker = Runtime::Current()->GetClassLinker();
jeffhaobdb76512011-09-07 11:43:16 -0700755 for (uint32_t idx = 0; idx < handlers_size; idx++) {
Ian Rogers0571d352011-11-03 19:51:38 -0700756 CatchHandlerIterator iterator(handlers_ptr);
757 for (; iterator.HasNext(); iterator.Next()) {
758 uint32_t dex_pc= iterator.GetHandlerAddress();
Ian Rogersd81871c2011-10-03 13:57:23 -0700759 if (!insn_flags_[dex_pc].IsOpcode()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700760 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
761 << "exception handler starts at bad address (" << dex_pc << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700762 return false;
763 }
Stephen Kyle9bc61992014-09-22 13:53:15 +0100764 if (!CheckNotMoveResult(code_item_->insns_, dex_pc)) {
765 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
766 << "exception handler begins with move-result* (" << dex_pc << ")";
767 return false;
768 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700769 insn_flags_[dex_pc].SetBranchTarget();
Ian Rogers28ad40d2011-10-27 15:19:26 -0700770 // Ensure exception types are resolved so that they don't need resolution to be delivered,
771 // unresolved exception types will be ignored by exception delivery
Ian Rogers0571d352011-11-03 19:51:38 -0700772 if (iterator.GetHandlerTypeIndex() != DexFile::kDexNoIndex16) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800773 mirror::Class* exception_type = linker->ResolveType(*dex_file_,
774 iterator.GetHandlerTypeIndex(),
Mathieu Chartierbf99f772014-08-23 16:37:27 -0700775 dex_cache_, class_loader_);
Ian Rogers7b078e82014-09-10 14:44:24 -0700776 if (exception_type == nullptr) {
777 DCHECK(self_->IsExceptionPending());
778 self_->ClearException();
Ian Rogers28ad40d2011-10-27 15:19:26 -0700779 }
780 }
jeffhaobdb76512011-09-07 11:43:16 -0700781 }
Ian Rogers0571d352011-11-03 19:51:38 -0700782 handlers_ptr = iterator.EndDataPointer();
jeffhaobdb76512011-09-07 11:43:16 -0700783 }
jeffhaobdb76512011-09-07 11:43:16 -0700784 return true;
785}
786
Ian Rogers776ac1f2012-04-13 23:36:36 -0700787bool MethodVerifier::VerifyInstructions() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700788 const Instruction* inst = Instruction::At(code_item_->insns_);
jeffhaoba5ebb92011-08-25 17:24:37 -0700789
Ian Rogers0c7abda2012-09-19 13:33:42 -0700790 /* Flag the start of the method as a branch target, and a GC point due to stack overflow errors */
Ian Rogersd81871c2011-10-03 13:57:23 -0700791 insn_flags_[0].SetBranchTarget();
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700792 insn_flags_[0].SetCompileTimeInfoPoint();
Ian Rogersd81871c2011-10-03 13:57:23 -0700793
794 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700795 for (uint32_t dex_pc = 0; dex_pc < insns_size;) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700796 if (!VerifyInstruction(inst, dex_pc)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700797 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -0700798 return false;
799 }
800 /* Flag instructions that are garbage collection points */
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700801 // All invoke points are marked as "Throw" points already.
802 // We are relying on this to also count all the invokes as interesting.
Vladimir Marko8b858e12014-11-27 14:52:37 +0000803 if (inst->IsBranch()) {
804 insn_flags_[dex_pc].SetCompileTimeInfoPoint();
805 // The compiler also needs safepoints for fall-through to loop heads.
806 // Such a loop head must be a target of a branch.
807 int32_t offset = 0;
808 bool cond, self_ok;
809 bool target_ok = GetBranchOffset(dex_pc, &offset, &cond, &self_ok);
810 DCHECK(target_ok);
811 insn_flags_[dex_pc + offset].SetCompileTimeInfoPoint();
812 } else if (inst->IsSwitch() || inst->IsThrow()) {
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700813 insn_flags_[dex_pc].SetCompileTimeInfoPoint();
Ian Rogersb8c78592013-07-25 23:52:52 +0000814 } else if (inst->IsReturn()) {
815 insn_flags_[dex_pc].SetCompileTimeInfoPointAndReturn();
Ian Rogersd81871c2011-10-03 13:57:23 -0700816 }
817 dex_pc += inst->SizeInCodeUnits();
818 inst = inst->Next();
819 }
820 return true;
821}
822
Ian Rogers776ac1f2012-04-13 23:36:36 -0700823bool MethodVerifier::VerifyInstruction(const Instruction* inst, uint32_t code_offset) {
Igor Murashkin4d7b75f2015-07-21 17:03:36 -0700824 if (UNLIKELY(inst->IsExperimental())) {
825 // Experimental instructions don't yet have verifier support implementation.
826 // While it is possible to use them by themselves, when we try to use stable instructions
827 // with a virtual register that was created by an experimental instruction,
828 // the data flow analysis will fail.
829 Fail(VERIFY_ERROR_FORCE_INTERPRETER)
830 << "experimental instruction is not supported by verifier; skipping verification";
831 have_pending_experimental_failure_ = true;
832 return false;
833 }
834
Ian Rogersd81871c2011-10-03 13:57:23 -0700835 bool result = true;
836 switch (inst->GetVerifyTypeArgumentA()) {
837 case Instruction::kVerifyRegA:
Ian Rogers29a26482014-05-02 15:27:29 -0700838 result = result && CheckRegisterIndex(inst->VRegA());
Ian Rogersd81871c2011-10-03 13:57:23 -0700839 break;
840 case Instruction::kVerifyRegAWide:
Ian Rogers29a26482014-05-02 15:27:29 -0700841 result = result && CheckWideRegisterIndex(inst->VRegA());
Ian Rogersd81871c2011-10-03 13:57:23 -0700842 break;
843 }
844 switch (inst->GetVerifyTypeArgumentB()) {
845 case Instruction::kVerifyRegB:
Ian Rogers29a26482014-05-02 15:27:29 -0700846 result = result && CheckRegisterIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700847 break;
848 case Instruction::kVerifyRegBField:
Ian Rogers29a26482014-05-02 15:27:29 -0700849 result = result && CheckFieldIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700850 break;
851 case Instruction::kVerifyRegBMethod:
Ian Rogers29a26482014-05-02 15:27:29 -0700852 result = result && CheckMethodIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700853 break;
854 case Instruction::kVerifyRegBNewInstance:
Ian Rogers29a26482014-05-02 15:27:29 -0700855 result = result && CheckNewInstance(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700856 break;
857 case Instruction::kVerifyRegBString:
Ian Rogers29a26482014-05-02 15:27:29 -0700858 result = result && CheckStringIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700859 break;
860 case Instruction::kVerifyRegBType:
Ian Rogers29a26482014-05-02 15:27:29 -0700861 result = result && CheckTypeIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700862 break;
863 case Instruction::kVerifyRegBWide:
Ian Rogers29a26482014-05-02 15:27:29 -0700864 result = result && CheckWideRegisterIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700865 break;
866 }
867 switch (inst->GetVerifyTypeArgumentC()) {
868 case Instruction::kVerifyRegC:
Ian Rogers29a26482014-05-02 15:27:29 -0700869 result = result && CheckRegisterIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700870 break;
871 case Instruction::kVerifyRegCField:
Ian Rogers29a26482014-05-02 15:27:29 -0700872 result = result && CheckFieldIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700873 break;
874 case Instruction::kVerifyRegCNewArray:
Ian Rogers29a26482014-05-02 15:27:29 -0700875 result = result && CheckNewArray(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700876 break;
877 case Instruction::kVerifyRegCType:
Ian Rogers29a26482014-05-02 15:27:29 -0700878 result = result && CheckTypeIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700879 break;
880 case Instruction::kVerifyRegCWide:
Ian Rogers29a26482014-05-02 15:27:29 -0700881 result = result && CheckWideRegisterIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700882 break;
883 }
884 switch (inst->GetVerifyExtraFlags()) {
885 case Instruction::kVerifyArrayData:
886 result = result && CheckArrayData(code_offset);
887 break;
888 case Instruction::kVerifyBranchTarget:
889 result = result && CheckBranchTarget(code_offset);
890 break;
891 case Instruction::kVerifySwitchTargets:
892 result = result && CheckSwitchTargets(code_offset);
893 break;
Andreas Gampec3314312014-06-19 18:13:29 -0700894 case Instruction::kVerifyVarArgNonZero:
895 // Fall-through.
Ian Rogers29a26482014-05-02 15:27:29 -0700896 case Instruction::kVerifyVarArg: {
Taiju Tsuiki29498a22015-04-13 14:21:00 +0900897 // Instructions that can actually return a negative value shouldn't have this flag.
898 uint32_t v_a = dchecked_integral_cast<uint32_t>(inst->VRegA());
899 if ((inst->GetVerifyExtraFlags() == Instruction::kVerifyVarArgNonZero && v_a == 0) ||
900 v_a > Instruction::kMaxVarArgRegs) {
901 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << v_a << ") in "
Andreas Gampec3314312014-06-19 18:13:29 -0700902 "non-range invoke";
903 return false;
904 }
Taiju Tsuiki29498a22015-04-13 14:21:00 +0900905
Ian Rogers29a26482014-05-02 15:27:29 -0700906 uint32_t args[Instruction::kMaxVarArgRegs];
907 inst->GetVarArgs(args);
Taiju Tsuiki29498a22015-04-13 14:21:00 +0900908 result = result && CheckVarArgRegs(v_a, args);
Ian Rogersd81871c2011-10-03 13:57:23 -0700909 break;
Ian Rogers29a26482014-05-02 15:27:29 -0700910 }
Andreas Gampec3314312014-06-19 18:13:29 -0700911 case Instruction::kVerifyVarArgRangeNonZero:
912 // Fall-through.
Ian Rogersd81871c2011-10-03 13:57:23 -0700913 case Instruction::kVerifyVarArgRange:
Andreas Gampec3314312014-06-19 18:13:29 -0700914 if (inst->GetVerifyExtraFlags() == Instruction::kVerifyVarArgRangeNonZero &&
915 inst->VRegA() <= 0) {
916 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << inst->VRegA() << ") in "
917 "range invoke";
918 return false;
919 }
Ian Rogers29a26482014-05-02 15:27:29 -0700920 result = result && CheckVarArgRangeRegs(inst->VRegA(), inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700921 break;
922 case Instruction::kVerifyError:
jeffhaod5347e02012-03-22 17:25:05 -0700923 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected opcode " << inst->Name();
Ian Rogersd81871c2011-10-03 13:57:23 -0700924 result = false;
925 break;
926 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800927 if (inst->GetVerifyIsRuntimeOnly() && Runtime::Current()->IsAotCompiler() && !verify_to_dump_) {
Ian Rogers5fb22a92014-06-13 10:31:28 -0700928 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "opcode only expected at runtime " << inst->Name();
929 result = false;
930 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700931 return result;
932}
933
Ian Rogers7b078e82014-09-10 14:44:24 -0700934inline bool MethodVerifier::CheckRegisterIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700935 if (idx >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700936 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register index out of range (" << idx << " >= "
937 << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700938 return false;
939 }
940 return true;
941}
942
Ian Rogers7b078e82014-09-10 14:44:24 -0700943inline bool MethodVerifier::CheckWideRegisterIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700944 if (idx + 1 >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700945 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "wide register index out of range (" << idx
946 << "+1 >= " << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700947 return false;
948 }
949 return true;
950}
951
Ian Rogers7b078e82014-09-10 14:44:24 -0700952inline bool MethodVerifier::CheckFieldIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700953 if (idx >= dex_file_->GetHeader().field_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700954 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad field index " << idx << " (max "
955 << dex_file_->GetHeader().field_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700956 return false;
957 }
958 return true;
959}
960
Ian Rogers7b078e82014-09-10 14:44:24 -0700961inline bool MethodVerifier::CheckMethodIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700962 if (idx >= dex_file_->GetHeader().method_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700963 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad method index " << idx << " (max "
964 << dex_file_->GetHeader().method_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700965 return false;
966 }
967 return true;
968}
969
Ian Rogers7b078e82014-09-10 14:44:24 -0700970inline bool MethodVerifier::CheckNewInstance(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700971 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700972 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
973 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700974 return false;
975 }
976 // We don't need the actual class, just a pointer to the class name.
Ian Rogers0571d352011-11-03 19:51:38 -0700977 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -0700978 if (descriptor[0] != 'L') {
jeffhaod5347e02012-03-22 17:25:05 -0700979 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "can't call new-instance on type '" << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -0700980 return false;
981 }
982 return true;
983}
984
Ian Rogers7b078e82014-09-10 14:44:24 -0700985inline bool MethodVerifier::CheckStringIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700986 if (idx >= dex_file_->GetHeader().string_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700987 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad string index " << idx << " (max "
988 << dex_file_->GetHeader().string_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700989 return false;
990 }
991 return true;
992}
993
Ian Rogers7b078e82014-09-10 14:44:24 -0700994inline bool MethodVerifier::CheckTypeIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700995 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700996 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
997 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700998 return false;
999 }
1000 return true;
1001}
1002
Ian Rogers776ac1f2012-04-13 23:36:36 -07001003bool MethodVerifier::CheckNewArray(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001004 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07001005 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
1006 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001007 return false;
1008 }
1009 int bracket_count = 0;
Ian Rogers0571d352011-11-03 19:51:38 -07001010 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -07001011 const char* cp = descriptor;
1012 while (*cp++ == '[') {
1013 bracket_count++;
1014 }
1015 if (bracket_count == 0) {
1016 /* The given class must be an array type. */
Brian Carlstrom93c33962013-07-26 10:37:43 -07001017 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1018 << "can't new-array class '" << descriptor << "' (not an array)";
Ian Rogersd81871c2011-10-03 13:57:23 -07001019 return false;
1020 } else if (bracket_count > 255) {
1021 /* It is illegal to create an array of more than 255 dimensions. */
Brian Carlstrom93c33962013-07-26 10:37:43 -07001022 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1023 << "can't new-array class '" << descriptor << "' (exceeds limit)";
Ian Rogersd81871c2011-10-03 13:57:23 -07001024 return false;
1025 }
1026 return true;
1027}
1028
Ian Rogers776ac1f2012-04-13 23:36:36 -07001029bool MethodVerifier::CheckArrayData(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001030 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
1031 const uint16_t* insns = code_item_->insns_ + cur_offset;
1032 const uint16_t* array_data;
1033 int32_t array_data_offset;
1034
1035 DCHECK_LT(cur_offset, insn_count);
1036 /* make sure the start of the array data table is in range */
1037 array_data_offset = insns[1] | (((int32_t) insns[2]) << 16);
1038 if ((int32_t) cur_offset + array_data_offset < 0 ||
1039 cur_offset + array_data_offset + 2 >= insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -07001040 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data start: at " << cur_offset
Brian Carlstrom93c33962013-07-26 10:37:43 -07001041 << ", data offset " << array_data_offset
1042 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -07001043 return false;
1044 }
1045 /* offset to array data table is a relative branch-style offset */
1046 array_data = insns + array_data_offset;
Andreas Gampe57c47582015-07-01 22:05:59 -07001047 // Make sure the table is at an even dex pc, that is, 32-bit aligned.
1048 if (!IsAligned<4>(array_data)) {
jeffhaod5347e02012-03-22 17:25:05 -07001049 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned array data table: at " << cur_offset
1050 << ", data offset " << array_data_offset;
Ian Rogersd81871c2011-10-03 13:57:23 -07001051 return false;
1052 }
Andreas Gampe57c47582015-07-01 22:05:59 -07001053 // Make sure the array-data is marked as an opcode. This ensures that it was reached when
1054 // traversing the code item linearly. It is an approximation for a by-spec padding value.
1055 if (!insn_flags_[cur_offset + array_data_offset].IsOpcode()) {
1056 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array data table at " << cur_offset
1057 << ", data offset " << array_data_offset
1058 << " not correctly visited, probably bad padding.";
1059 return false;
1060 }
1061
Ian Rogersd81871c2011-10-03 13:57:23 -07001062 uint32_t value_width = array_data[1];
Elliott Hughes398f64b2012-03-26 18:05:48 -07001063 uint32_t value_count = *reinterpret_cast<const uint32_t*>(&array_data[2]);
Ian Rogersd81871c2011-10-03 13:57:23 -07001064 uint32_t table_size = 4 + (value_width * value_count + 1) / 2;
1065 /* make sure the end of the switch is in range */
1066 if (cur_offset + array_data_offset + table_size > insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -07001067 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data end: at " << cur_offset
1068 << ", data offset " << array_data_offset << ", end "
1069 << cur_offset + array_data_offset + table_size
1070 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -07001071 return false;
1072 }
1073 return true;
1074}
1075
Ian Rogers776ac1f2012-04-13 23:36:36 -07001076bool MethodVerifier::CheckBranchTarget(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001077 int32_t offset;
1078 bool isConditional, selfOkay;
1079 if (!GetBranchOffset(cur_offset, &offset, &isConditional, &selfOkay)) {
1080 return false;
1081 }
1082 if (!selfOkay && offset == 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001083 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch offset of zero not allowed at"
1084 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -07001085 return false;
1086 }
Elliott Hughes81ff3182012-03-23 20:35:56 -07001087 // Check for 32-bit overflow. This isn't strictly necessary if we can depend on the runtime
1088 // to have identical "wrap-around" behavior, but it's unwise to depend on that.
Ian Rogersd81871c2011-10-03 13:57:23 -07001089 if (((int64_t) cur_offset + (int64_t) offset) != (int64_t) (cur_offset + offset)) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001090 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch target overflow "
1091 << reinterpret_cast<void*>(cur_offset) << " +" << offset;
Ian Rogersd81871c2011-10-03 13:57:23 -07001092 return false;
1093 }
1094 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
1095 int32_t abs_offset = cur_offset + offset;
Brian Carlstrom93c33962013-07-26 10:37:43 -07001096 if (abs_offset < 0 ||
1097 (uint32_t) abs_offset >= insn_count ||
1098 !insn_flags_[abs_offset].IsOpcode()) {
jeffhaod5347e02012-03-22 17:25:05 -07001099 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid branch target " << offset << " (-> "
Elliott Hughes398f64b2012-03-26 18:05:48 -07001100 << reinterpret_cast<void*>(abs_offset) << ") at "
1101 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -07001102 return false;
1103 }
1104 insn_flags_[abs_offset].SetBranchTarget();
1105 return true;
1106}
1107
Ian Rogers776ac1f2012-04-13 23:36:36 -07001108bool MethodVerifier::GetBranchOffset(uint32_t cur_offset, int32_t* pOffset, bool* pConditional,
Ian Rogersd81871c2011-10-03 13:57:23 -07001109 bool* selfOkay) {
1110 const uint16_t* insns = code_item_->insns_ + cur_offset;
1111 *pConditional = false;
1112 *selfOkay = false;
jeffhaoba5ebb92011-08-25 17:24:37 -07001113 switch (*insns & 0xff) {
1114 case Instruction::GOTO:
1115 *pOffset = ((int16_t) *insns) >> 8;
jeffhaoba5ebb92011-08-25 17:24:37 -07001116 break;
1117 case Instruction::GOTO_32:
1118 *pOffset = insns[1] | (((uint32_t) insns[2]) << 16);
jeffhaoba5ebb92011-08-25 17:24:37 -07001119 *selfOkay = true;
1120 break;
1121 case Instruction::GOTO_16:
1122 *pOffset = (int16_t) insns[1];
jeffhaoba5ebb92011-08-25 17:24:37 -07001123 break;
1124 case Instruction::IF_EQ:
1125 case Instruction::IF_NE:
1126 case Instruction::IF_LT:
1127 case Instruction::IF_GE:
1128 case Instruction::IF_GT:
1129 case Instruction::IF_LE:
1130 case Instruction::IF_EQZ:
1131 case Instruction::IF_NEZ:
1132 case Instruction::IF_LTZ:
1133 case Instruction::IF_GEZ:
1134 case Instruction::IF_GTZ:
1135 case Instruction::IF_LEZ:
1136 *pOffset = (int16_t) insns[1];
1137 *pConditional = true;
jeffhaoba5ebb92011-08-25 17:24:37 -07001138 break;
1139 default:
1140 return false;
jeffhaoba5ebb92011-08-25 17:24:37 -07001141 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001142 return true;
1143}
1144
Ian Rogers776ac1f2012-04-13 23:36:36 -07001145bool MethodVerifier::CheckSwitchTargets(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001146 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001147 DCHECK_LT(cur_offset, insn_count);
Ian Rogersd81871c2011-10-03 13:57:23 -07001148 const uint16_t* insns = code_item_->insns_ + cur_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001149 /* make sure the start of the switch is in range */
Ian Rogersd81871c2011-10-03 13:57:23 -07001150 int32_t switch_offset = insns[1] | ((int32_t) insns[2]) << 16;
Jeff Hao9ccd1512015-03-20 18:11:45 -07001151 if ((int32_t) cur_offset + switch_offset < 0 || cur_offset + switch_offset + 2 > insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -07001152 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch start: at " << cur_offset
Brian Carlstrom93c33962013-07-26 10:37:43 -07001153 << ", switch offset " << switch_offset
1154 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001155 return false;
1156 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001157 /* offset to switch table is a relative branch-style offset */
Ian Rogersd81871c2011-10-03 13:57:23 -07001158 const uint16_t* switch_insns = insns + switch_offset;
Andreas Gampe57c47582015-07-01 22:05:59 -07001159 // Make sure the table is at an even dex pc, that is, 32-bit aligned.
1160 if (!IsAligned<4>(switch_insns)) {
jeffhaod5347e02012-03-22 17:25:05 -07001161 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned switch table: at " << cur_offset
1162 << ", switch offset " << switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001163 return false;
1164 }
Andreas Gampe57c47582015-07-01 22:05:59 -07001165 // Make sure the switch data is marked as an opcode. This ensures that it was reached when
1166 // traversing the code item linearly. It is an approximation for a by-spec padding value.
1167 if (!insn_flags_[cur_offset + switch_offset].IsOpcode()) {
1168 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "switch table at " << cur_offset
1169 << ", switch offset " << switch_offset
1170 << " not correctly visited, probably bad padding.";
1171 return false;
1172 }
1173
Ian Rogersd81871c2011-10-03 13:57:23 -07001174 uint32_t switch_count = switch_insns[1];
1175 int32_t keys_offset, targets_offset;
1176 uint16_t expected_signature;
jeffhaoba5ebb92011-08-25 17:24:37 -07001177 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
1178 /* 0=sig, 1=count, 2/3=firstKey */
1179 targets_offset = 4;
1180 keys_offset = -1;
1181 expected_signature = Instruction::kPackedSwitchSignature;
1182 } else {
1183 /* 0=sig, 1=count, 2..count*2 = keys */
1184 keys_offset = 2;
1185 targets_offset = 2 + 2 * switch_count;
1186 expected_signature = Instruction::kSparseSwitchSignature;
1187 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001188 uint32_t table_size = targets_offset + switch_count * 2;
jeffhaoba5ebb92011-08-25 17:24:37 -07001189 if (switch_insns[0] != expected_signature) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001190 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1191 << StringPrintf("wrong signature for switch table (%x, wanted %x)",
1192 switch_insns[0], expected_signature);
jeffhaoba5ebb92011-08-25 17:24:37 -07001193 return false;
1194 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001195 /* make sure the end of the switch is in range */
1196 if (cur_offset + switch_offset + table_size > (uint32_t) insn_count) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001197 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch end: at " << cur_offset
1198 << ", switch offset " << switch_offset
1199 << ", end " << (cur_offset + switch_offset + table_size)
jeffhaod5347e02012-03-22 17:25:05 -07001200 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001201 return false;
1202 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001203 /* for a sparse switch, verify the keys are in ascending order */
1204 if (keys_offset > 0 && switch_count > 1) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001205 int32_t last_key = switch_insns[keys_offset] | (switch_insns[keys_offset + 1] << 16);
1206 for (uint32_t targ = 1; targ < switch_count; targ++) {
jeffhaoba5ebb92011-08-25 17:24:37 -07001207 int32_t key = (int32_t) switch_insns[keys_offset + targ * 2] |
1208 (int32_t) (switch_insns[keys_offset + targ * 2 + 1] << 16);
1209 if (key <= last_key) {
jeffhaod5347e02012-03-22 17:25:05 -07001210 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid packed switch: last key=" << last_key
1211 << ", this=" << key;
jeffhaoba5ebb92011-08-25 17:24:37 -07001212 return false;
1213 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001214 last_key = key;
1215 }
1216 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001217 /* verify each switch target */
Ian Rogersd81871c2011-10-03 13:57:23 -07001218 for (uint32_t targ = 0; targ < switch_count; targ++) {
1219 int32_t offset = (int32_t) switch_insns[targets_offset + targ * 2] |
1220 (int32_t) (switch_insns[targets_offset + targ * 2 + 1] << 16);
1221 int32_t abs_offset = cur_offset + offset;
Brian Carlstrom93c33962013-07-26 10:37:43 -07001222 if (abs_offset < 0 ||
1223 abs_offset >= (int32_t) insn_count ||
1224 !insn_flags_[abs_offset].IsOpcode()) {
1225 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch target " << offset
1226 << " (-> " << reinterpret_cast<void*>(abs_offset) << ") at "
1227 << reinterpret_cast<void*>(cur_offset)
1228 << "[" << targ << "]";
jeffhaoba5ebb92011-08-25 17:24:37 -07001229 return false;
1230 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001231 insn_flags_[abs_offset].SetBranchTarget();
1232 }
1233 return true;
1234}
1235
Ian Rogers776ac1f2012-04-13 23:36:36 -07001236bool MethodVerifier::CheckVarArgRegs(uint32_t vA, uint32_t arg[]) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001237 uint16_t registers_size = code_item_->registers_size_;
1238 for (uint32_t idx = 0; idx < vA; idx++) {
jeffhao457cc512012-02-02 16:55:13 -08001239 if (arg[idx] >= registers_size) {
jeffhaod5347e02012-03-22 17:25:05 -07001240 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index (" << arg[idx]
1241 << ") in non-range invoke (>= " << registers_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001242 return false;
1243 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001244 }
1245
1246 return true;
1247}
1248
Ian Rogers776ac1f2012-04-13 23:36:36 -07001249bool MethodVerifier::CheckVarArgRangeRegs(uint32_t vA, uint32_t vC) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001250 uint16_t registers_size = code_item_->registers_size_;
1251 // vA/vC are unsigned 8-bit/16-bit quantities for /range instructions, so there's no risk of
1252 // integer overflow when adding them here.
1253 if (vA + vC > registers_size) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001254 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index " << vA << "+" << vC
1255 << " in range invoke (> " << registers_size << ")";
jeffhaoba5ebb92011-08-25 17:24:37 -07001256 return false;
1257 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001258 return true;
1259}
1260
Ian Rogers776ac1f2012-04-13 23:36:36 -07001261bool MethodVerifier::VerifyCodeFlow() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001262 uint16_t registers_size = code_item_->registers_size_;
1263 uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaobdb76512011-09-07 11:43:16 -07001264
Ian Rogersd81871c2011-10-03 13:57:23 -07001265 /* Create and initialize table holding register status */
Brian Carlstrom93c33962013-07-26 10:37:43 -07001266 reg_table_.Init(kTrackCompilerInterestPoints,
1267 insn_flags_.get(),
1268 insns_size,
1269 registers_size,
1270 this);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07001271
jeffhaobdb76512011-09-07 11:43:16 -07001272
Ian Rogersd0fbd852013-09-24 18:17:04 -07001273 work_line_.reset(RegisterLine::Create(registers_size, this));
1274 saved_line_.reset(RegisterLine::Create(registers_size, this));
jeffhaobdb76512011-09-07 11:43:16 -07001275
Ian Rogersd81871c2011-10-03 13:57:23 -07001276 /* Initialize register types of method arguments. */
1277 if (!SetTypesFromSignature()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001278 DCHECK_NE(failures_.size(), 0U);
1279 std::string prepend("Bad signature in ");
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001280 prepend += PrettyMethod(dex_method_idx_, *dex_file_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001281 PrependToLastFailMessage(prepend);
Ian Rogersd81871c2011-10-03 13:57:23 -07001282 return false;
1283 }
Andreas Gamped5ad72f2015-06-26 17:33:47 -07001284 // We may have a runtime failure here, clear.
1285 have_pending_runtime_throw_failure_ = false;
1286
Ian Rogersd81871c2011-10-03 13:57:23 -07001287 /* Perform code flow verification. */
1288 if (!CodeFlowVerifyMethod()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001289 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -07001290 return false;
jeffhaobdb76512011-09-07 11:43:16 -07001291 }
jeffhaobdb76512011-09-07 11:43:16 -07001292 return true;
1293}
1294
Ian Rogersad0b3a32012-04-16 14:50:24 -07001295std::ostream& MethodVerifier::DumpFailures(std::ostream& os) {
1296 DCHECK_EQ(failures_.size(), failure_messages_.size());
Jeff Hao4137f482013-11-22 11:44:57 -08001297 for (size_t i = 0; i < failures_.size(); ++i) {
1298 os << failure_messages_[i]->str() << "\n";
Ian Rogersad0b3a32012-04-16 14:50:24 -07001299 }
1300 return os;
1301}
1302
Ian Rogers776ac1f2012-04-13 23:36:36 -07001303void MethodVerifier::Dump(std::ostream& os) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001304 VariableIndentationOutputStream vios(&os);
1305 Dump(&vios);
1306}
1307
1308void MethodVerifier::Dump(VariableIndentationOutputStream* vios) {
Ian Rogers7b078e82014-09-10 14:44:24 -07001309 if (code_item_ == nullptr) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001310 vios->Stream() << "Native method\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001311 return;
jeffhaobdb76512011-09-07 11:43:16 -07001312 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001313 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001314 vios->Stream() << "Register Types:\n";
1315 ScopedIndentation indent1(vios);
1316 reg_types_.Dump(vios->Stream());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001317 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001318 vios->Stream() << "Dumping instructions and register lines:\n";
1319 ScopedIndentation indent1(vios);
Ian Rogersd81871c2011-10-03 13:57:23 -07001320 const Instruction* inst = Instruction::At(code_item_->insns_);
1321 for (size_t dex_pc = 0; dex_pc < code_item_->insns_size_in_code_units_;
Ian Rogers7b078e82014-09-10 14:44:24 -07001322 dex_pc += inst->SizeInCodeUnits()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001323 RegisterLine* reg_line = reg_table_.GetLine(dex_pc);
Ian Rogers7b078e82014-09-10 14:44:24 -07001324 if (reg_line != nullptr) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001325 vios->Stream() << reg_line->Dump(this) << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07001326 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001327 vios->Stream()
1328 << StringPrintf("0x%04zx", dex_pc) << ": " << insn_flags_[dex_pc].ToString() << " ";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001329 const bool kDumpHexOfInstruction = false;
1330 if (kDumpHexOfInstruction) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001331 vios->Stream() << inst->DumpHex(5) << " ";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001332 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001333 vios->Stream() << inst->DumpString(dex_file_) << "\n";
jeffhaoba5ebb92011-08-25 17:24:37 -07001334 inst = inst->Next();
1335 }
jeffhaobdb76512011-09-07 11:43:16 -07001336}
1337
Ian Rogersd81871c2011-10-03 13:57:23 -07001338static bool IsPrimitiveDescriptor(char descriptor) {
1339 switch (descriptor) {
jeffhaobdb76512011-09-07 11:43:16 -07001340 case 'I':
1341 case 'C':
1342 case 'S':
1343 case 'B':
1344 case 'Z':
jeffhaobdb76512011-09-07 11:43:16 -07001345 case 'F':
1346 case 'D':
1347 case 'J':
Ian Rogersd81871c2011-10-03 13:57:23 -07001348 return true;
jeffhaobdb76512011-09-07 11:43:16 -07001349 default:
1350 return false;
1351 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001352}
1353
Ian Rogers776ac1f2012-04-13 23:36:36 -07001354bool MethodVerifier::SetTypesFromSignature() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001355 RegisterLine* reg_line = reg_table_.GetLine(0);
Andreas Gampeef0b1a12015-06-19 20:37:46 -07001356
1357 // Should have been verified earlier.
1358 DCHECK_GE(code_item_->registers_size_, code_item_->ins_size_);
1359
1360 uint32_t arg_start = code_item_->registers_size_ - code_item_->ins_size_;
Ian Rogersd81871c2011-10-03 13:57:23 -07001361 size_t expected_args = code_item_->ins_size_; /* long/double count as two */
jeffhaobdb76512011-09-07 11:43:16 -07001362
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001363 // Include the "this" pointer.
Ian Rogersd81871c2011-10-03 13:57:23 -07001364 size_t cur_arg = 0;
Ian Rogersad0b3a32012-04-16 14:50:24 -07001365 if (!IsStatic()) {
Andreas Gampeef0b1a12015-06-19 20:37:46 -07001366 if (expected_args == 0) {
1367 // Expect at least a receiver.
1368 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected 0 args, but method is not static";
1369 return false;
1370 }
1371
Ian Rogersd81871c2011-10-03 13:57:23 -07001372 // If this is a constructor for a class other than java.lang.Object, mark the first ("this")
1373 // argument as uninitialized. This restricts field access until the superclass constructor is
1374 // called.
Ian Rogersd8f69b02014-09-10 21:43:52 +00001375 const RegType& declaring_class = GetDeclaringClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07001376 if (IsConstructor() && !declaring_class.IsJavaLangObject()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07001377 reg_line->SetRegisterType(this, arg_start + cur_arg,
Ian Rogersd81871c2011-10-03 13:57:23 -07001378 reg_types_.UninitializedThisArgument(declaring_class));
1379 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07001380 reg_line->SetRegisterType(this, arg_start + cur_arg, declaring_class);
jeffhaobdb76512011-09-07 11:43:16 -07001381 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001382 cur_arg++;
jeffhaobdb76512011-09-07 11:43:16 -07001383 }
1384
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001385 const DexFile::ProtoId& proto_id =
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001386 dex_file_->GetMethodPrototype(dex_file_->GetMethodId(dex_method_idx_));
Ian Rogers0571d352011-11-03 19:51:38 -07001387 DexFileParameterIterator iterator(*dex_file_, proto_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07001388
1389 for (; iterator.HasNext(); iterator.Next()) {
1390 const char* descriptor = iterator.GetDescriptor();
Ian Rogers7b078e82014-09-10 14:44:24 -07001391 if (descriptor == nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001392 LOG(FATAL) << "Null descriptor";
1393 }
1394 if (cur_arg >= expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07001395 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1396 << " args, found more (" << descriptor << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001397 return false;
1398 }
1399 switch (descriptor[0]) {
1400 case 'L':
1401 case '[':
1402 // We assume that reference arguments are initialized. The only way it could be otherwise
1403 // (assuming the caller was verified) is if the current method is <init>, but in that case
1404 // it's effectively considered initialized the instant we reach here (in the sense that we
1405 // can return without doing anything or call virtual methods).
1406 {
Ian Rogersd8f69b02014-09-10 21:43:52 +00001407 const RegType& reg_type = ResolveClassAndCheckAccess(iterator.GetTypeIdx());
Sebastien Hertz2ed76f92014-04-22 17:11:08 +02001408 if (!reg_type.IsNonZeroReferenceTypes()) {
1409 DCHECK(HasFailures());
1410 return false;
1411 }
Ian Rogers7b078e82014-09-10 14:44:24 -07001412 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001413 }
1414 break;
1415 case 'Z':
Ian Rogers7b078e82014-09-10 14:44:24 -07001416 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_types_.Boolean());
Ian Rogersd81871c2011-10-03 13:57:23 -07001417 break;
1418 case 'C':
Ian Rogers7b078e82014-09-10 14:44:24 -07001419 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_types_.Char());
Ian Rogersd81871c2011-10-03 13:57:23 -07001420 break;
1421 case 'B':
Ian Rogers7b078e82014-09-10 14:44:24 -07001422 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_types_.Byte());
Ian Rogersd81871c2011-10-03 13:57:23 -07001423 break;
1424 case 'I':
Ian Rogers7b078e82014-09-10 14:44:24 -07001425 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_types_.Integer());
Ian Rogersd81871c2011-10-03 13:57:23 -07001426 break;
1427 case 'S':
Ian Rogers7b078e82014-09-10 14:44:24 -07001428 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_types_.Short());
Ian Rogersd81871c2011-10-03 13:57:23 -07001429 break;
1430 case 'F':
Ian Rogers7b078e82014-09-10 14:44:24 -07001431 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_types_.Float());
Ian Rogersd81871c2011-10-03 13:57:23 -07001432 break;
1433 case 'J':
1434 case 'D': {
Andreas Gampe77cd4d62014-06-19 17:29:48 -07001435 if (cur_arg + 1 >= expected_args) {
1436 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1437 << " args, found more (" << descriptor << ")";
1438 return false;
1439 }
1440
Ian Rogers7b078e82014-09-10 14:44:24 -07001441 const RegType* lo_half;
1442 const RegType* hi_half;
1443 if (descriptor[0] == 'J') {
1444 lo_half = &reg_types_.LongLo();
1445 hi_half = &reg_types_.LongHi();
1446 } else {
1447 lo_half = &reg_types_.DoubleLo();
1448 hi_half = &reg_types_.DoubleHi();
1449 }
1450 reg_line->SetRegisterTypeWide(this, arg_start + cur_arg, *lo_half, *hi_half);
Ian Rogersd81871c2011-10-03 13:57:23 -07001451 cur_arg++;
1452 break;
1453 }
1454 default:
Brian Carlstrom93c33962013-07-26 10:37:43 -07001455 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected signature type char '"
1456 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001457 return false;
1458 }
1459 cur_arg++;
1460 }
1461 if (cur_arg != expected_args) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001462 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1463 << " arguments, found " << cur_arg;
Ian Rogersd81871c2011-10-03 13:57:23 -07001464 return false;
1465 }
1466 const char* descriptor = dex_file_->GetReturnTypeDescriptor(proto_id);
1467 // Validate return type. We don't do the type lookup; just want to make sure that it has the right
1468 // format. Only major difference from the method argument format is that 'V' is supported.
1469 bool result;
1470 if (IsPrimitiveDescriptor(descriptor[0]) || descriptor[0] == 'V') {
1471 result = descriptor[1] == '\0';
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001472 } else if (descriptor[0] == '[') { // single/multi-dimensional array of object/primitive
Ian Rogersd81871c2011-10-03 13:57:23 -07001473 size_t i = 0;
1474 do {
1475 i++;
1476 } while (descriptor[i] == '['); // process leading [
1477 if (descriptor[i] == 'L') { // object array
1478 do {
1479 i++; // find closing ;
1480 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1481 result = descriptor[i] == ';';
1482 } else { // primitive array
1483 result = IsPrimitiveDescriptor(descriptor[i]) && descriptor[i + 1] == '\0';
1484 }
1485 } else if (descriptor[0] == 'L') {
1486 // could be more thorough here, but shouldn't be required
1487 size_t i = 0;
1488 do {
1489 i++;
1490 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1491 result = descriptor[i] == ';';
1492 } else {
1493 result = false;
1494 }
1495 if (!result) {
jeffhaod5347e02012-03-22 17:25:05 -07001496 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected char in return type descriptor '"
1497 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001498 }
1499 return result;
jeffhaobdb76512011-09-07 11:43:16 -07001500}
1501
Ian Rogers776ac1f2012-04-13 23:36:36 -07001502bool MethodVerifier::CodeFlowVerifyMethod() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001503 const uint16_t* insns = code_item_->insns_;
1504 const uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaoba5ebb92011-08-25 17:24:37 -07001505
jeffhaobdb76512011-09-07 11:43:16 -07001506 /* Begin by marking the first instruction as "changed". */
Ian Rogersd81871c2011-10-03 13:57:23 -07001507 insn_flags_[0].SetChanged();
1508 uint32_t start_guess = 0;
jeffhaoba5ebb92011-08-25 17:24:37 -07001509
jeffhaobdb76512011-09-07 11:43:16 -07001510 /* Continue until no instructions are marked "changed". */
1511 while (true) {
Mathieu Chartier4306ef82014-12-19 18:41:47 -08001512 if (allow_thread_suspension_) {
1513 self_->AllowThreadSuspension();
1514 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001515 // Find the first marked one. Use "start_guess" as a way to find one quickly.
1516 uint32_t insn_idx = start_guess;
1517 for (; insn_idx < insns_size; insn_idx++) {
1518 if (insn_flags_[insn_idx].IsChanged())
jeffhaobdb76512011-09-07 11:43:16 -07001519 break;
1520 }
jeffhaobdb76512011-09-07 11:43:16 -07001521 if (insn_idx == insns_size) {
1522 if (start_guess != 0) {
1523 /* try again, starting from the top */
1524 start_guess = 0;
1525 continue;
1526 } else {
1527 /* all flags are clear */
1528 break;
1529 }
1530 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001531 // We carry the working set of registers from instruction to instruction. If this address can
1532 // be the target of a branch (or throw) instruction, or if we're skipping around chasing
1533 // "changed" flags, we need to load the set of registers from the table.
1534 // Because we always prefer to continue on to the next instruction, we should never have a
1535 // situation where we have a stray "changed" flag set on an instruction that isn't a branch
1536 // target.
1537 work_insn_idx_ = insn_idx;
1538 if (insn_flags_[insn_idx].IsBranchTarget()) {
1539 work_line_->CopyFromLine(reg_table_.GetLine(insn_idx));
Ian Rogersebbdd872014-07-07 23:53:08 -07001540 } else if (kIsDebugBuild) {
jeffhaobdb76512011-09-07 11:43:16 -07001541 /*
1542 * Sanity check: retrieve the stored register line (assuming
1543 * a full table) and make sure it actually matches.
1544 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001545 RegisterLine* register_line = reg_table_.GetLine(insn_idx);
Ian Rogers7b078e82014-09-10 14:44:24 -07001546 if (register_line != nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001547 if (work_line_->CompareLine(register_line) != 0) {
1548 Dump(std::cout);
1549 std::cout << info_messages_.str();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001550 LOG(FATAL) << "work_line diverged in " << PrettyMethod(dex_method_idx_, *dex_file_)
Elliott Hughesc073b072012-05-24 19:29:17 -07001551 << "@" << reinterpret_cast<void*>(work_insn_idx_) << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07001552 << " work_line=" << work_line_->Dump(this) << "\n"
1553 << " expected=" << register_line->Dump(this);
Ian Rogersd81871c2011-10-03 13:57:23 -07001554 }
jeffhaobdb76512011-09-07 11:43:16 -07001555 }
jeffhaobdb76512011-09-07 11:43:16 -07001556 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001557 if (!CodeFlowVerifyInstruction(&start_guess)) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001558 std::string prepend(PrettyMethod(dex_method_idx_, *dex_file_));
Ian Rogersad0b3a32012-04-16 14:50:24 -07001559 prepend += " failed to verify: ";
1560 PrependToLastFailMessage(prepend);
jeffhaoba5ebb92011-08-25 17:24:37 -07001561 return false;
1562 }
jeffhaobdb76512011-09-07 11:43:16 -07001563 /* Clear "changed" and mark as visited. */
Ian Rogersd81871c2011-10-03 13:57:23 -07001564 insn_flags_[insn_idx].SetVisited();
1565 insn_flags_[insn_idx].ClearChanged();
jeffhaobdb76512011-09-07 11:43:16 -07001566 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001567
Ian Rogers1c849e52012-06-28 14:00:33 -07001568 if (gDebugVerify) {
jeffhaobdb76512011-09-07 11:43:16 -07001569 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001570 * Scan for dead code. There's nothing "evil" about dead code
jeffhaobdb76512011-09-07 11:43:16 -07001571 * (besides the wasted space), but it indicates a flaw somewhere
1572 * down the line, possibly in the verifier.
1573 *
1574 * If we've substituted "always throw" instructions into the stream,
1575 * we are almost certainly going to have some dead code.
1576 */
1577 int dead_start = -1;
Ian Rogersd81871c2011-10-03 13:57:23 -07001578 uint32_t insn_idx = 0;
Ian Rogers7b078e82014-09-10 14:44:24 -07001579 for (; insn_idx < insns_size;
1580 insn_idx += Instruction::At(code_item_->insns_ + insn_idx)->SizeInCodeUnits()) {
jeffhaobdb76512011-09-07 11:43:16 -07001581 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001582 * Switch-statement data doesn't get "visited" by scanner. It
jeffhaobdb76512011-09-07 11:43:16 -07001583 * may or may not be preceded by a padding NOP (for alignment).
1584 */
1585 if (insns[insn_idx] == Instruction::kPackedSwitchSignature ||
1586 insns[insn_idx] == Instruction::kSparseSwitchSignature ||
1587 insns[insn_idx] == Instruction::kArrayDataSignature ||
Elliott Hughes380aaa72012-07-09 14:33:15 -07001588 (insns[insn_idx] == Instruction::NOP && (insn_idx + 1 < insns_size) &&
jeffhaobdb76512011-09-07 11:43:16 -07001589 (insns[insn_idx + 1] == Instruction::kPackedSwitchSignature ||
1590 insns[insn_idx + 1] == Instruction::kSparseSwitchSignature ||
1591 insns[insn_idx + 1] == Instruction::kArrayDataSignature))) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001592 insn_flags_[insn_idx].SetVisited();
jeffhaobdb76512011-09-07 11:43:16 -07001593 }
1594
Ian Rogersd81871c2011-10-03 13:57:23 -07001595 if (!insn_flags_[insn_idx].IsVisited()) {
jeffhaobdb76512011-09-07 11:43:16 -07001596 if (dead_start < 0)
1597 dead_start = insn_idx;
1598 } else if (dead_start >= 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001599 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start)
1600 << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaobdb76512011-09-07 11:43:16 -07001601 dead_start = -1;
1602 }
1603 }
1604 if (dead_start >= 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001605 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start)
1606 << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaoba5ebb92011-08-25 17:24:37 -07001607 }
Ian Rogersc9e463c2013-06-05 16:52:26 -07001608 // To dump the state of the verify after a method, do something like:
1609 // if (PrettyMethod(dex_method_idx_, *dex_file_) ==
1610 // "boolean java.lang.String.equals(java.lang.Object)") {
1611 // LOG(INFO) << info_messages_.str();
1612 // }
jeffhaoba5ebb92011-08-25 17:24:37 -07001613 }
jeffhaobdb76512011-09-07 11:43:16 -07001614 return true;
1615}
1616
Andreas Gampe68df3202015-06-22 11:35:46 -07001617// Returns the index of the first final instance field of the given class, or kDexNoIndex if there
1618// is no such field.
1619static uint32_t GetFirstFinalInstanceFieldIndex(const DexFile& dex_file, uint16_t type_idx) {
1620 const DexFile::ClassDef* class_def = dex_file.FindClassDef(type_idx);
1621 DCHECK(class_def != nullptr);
1622 const uint8_t* class_data = dex_file.GetClassData(*class_def);
1623 DCHECK(class_data != nullptr);
1624 ClassDataItemIterator it(dex_file, class_data);
1625 // Skip static fields.
1626 while (it.HasNextStaticField()) {
1627 it.Next();
1628 }
1629 while (it.HasNextInstanceField()) {
1630 if ((it.GetFieldAccessFlags() & kAccFinal) != 0) {
1631 return it.GetMemberIndex();
1632 }
1633 it.Next();
1634 }
1635 return DexFile::kDexNoIndex;
1636}
1637
Ian Rogers776ac1f2012-04-13 23:36:36 -07001638bool MethodVerifier::CodeFlowVerifyInstruction(uint32_t* start_guess) {
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001639 // If we're doing FindLocksAtDexPc, check whether we're at the dex pc we care about.
1640 // We want the state _before_ the instruction, for the case where the dex pc we're
1641 // interested in is itself a monitor-enter instruction (which is a likely place
1642 // for a thread to be suspended).
Ian Rogers7b078e82014-09-10 14:44:24 -07001643 if (monitor_enter_dex_pcs_ != nullptr && work_insn_idx_ == interesting_dex_pc_) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001644 monitor_enter_dex_pcs_->clear(); // The new work line is more accurate than the previous one.
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001645 for (size_t i = 0; i < work_line_->GetMonitorEnterCount(); ++i) {
1646 monitor_enter_dex_pcs_->push_back(work_line_->GetMonitorEnterDexPc(i));
1647 }
1648 }
1649
jeffhaobdb76512011-09-07 11:43:16 -07001650 /*
1651 * Once we finish decoding the instruction, we need to figure out where
jeffhaod1f0fde2011-09-08 17:25:33 -07001652 * we can go from here. There are three possible ways to transfer
jeffhaobdb76512011-09-07 11:43:16 -07001653 * control to another statement:
1654 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001655 * (1) Continue to the next instruction. Applies to all but
jeffhaobdb76512011-09-07 11:43:16 -07001656 * unconditional branches, method returns, and exception throws.
jeffhaod1f0fde2011-09-08 17:25:33 -07001657 * (2) Branch to one or more possible locations. Applies to branches
jeffhaobdb76512011-09-07 11:43:16 -07001658 * and switch statements.
jeffhaod1f0fde2011-09-08 17:25:33 -07001659 * (3) Exception handlers. Applies to any instruction that can
jeffhaobdb76512011-09-07 11:43:16 -07001660 * throw an exception that is handled by an encompassing "try"
1661 * block.
1662 *
1663 * We can also return, in which case there is no successor instruction
1664 * from this point.
1665 *
Elliott Hughesadb8c672012-03-06 16:49:32 -08001666 * The behavior can be determined from the opcode flags.
jeffhaobdb76512011-09-07 11:43:16 -07001667 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001668 const uint16_t* insns = code_item_->insns_ + work_insn_idx_;
1669 const Instruction* inst = Instruction::At(insns);
Ian Rogersa75a0132012-09-28 11:41:42 -07001670 int opcode_flags = Instruction::FlagsOf(inst->Opcode());
jeffhaobdb76512011-09-07 11:43:16 -07001671
jeffhaobdb76512011-09-07 11:43:16 -07001672 int32_t branch_target = 0;
jeffhaobdb76512011-09-07 11:43:16 -07001673 bool just_set_result = false;
Ian Rogers2c8a8572011-10-24 17:11:36 -07001674 if (gDebugVerify) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001675 // Generate processing back trace to debug verifier
Elliott Hughesc073b072012-05-24 19:29:17 -07001676 LogVerifyInfo() << "Processing " << inst->DumpString(dex_file_) << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07001677 << work_line_->Dump(this) << "\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001678 }
jeffhaobdb76512011-09-07 11:43:16 -07001679
1680 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001681 * Make a copy of the previous register state. If the instruction
jeffhaobdb76512011-09-07 11:43:16 -07001682 * can throw an exception, we will copy/merge this into the "catch"
1683 * address rather than work_line, because we don't want the result
1684 * from the "successful" code path (e.g. a check-cast that "improves"
1685 * a type) to be visible to the exception handler.
1686 */
Ian Rogers776ac1f2012-04-13 23:36:36 -07001687 if ((opcode_flags & Instruction::kThrow) != 0 && CurrentInsnFlags()->IsInTry()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001688 saved_line_->CopyFromLine(work_line_.get());
Ian Rogers1ff3c982014-08-12 02:30:58 -07001689 } else if (kIsDebugBuild) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001690 saved_line_->FillWithGarbage();
jeffhaobdb76512011-09-07 11:43:16 -07001691 }
Andreas Gamped12e7822015-06-25 10:26:40 -07001692 DCHECK(!have_pending_runtime_throw_failure_); // Per-instruction flag, should not be set here.
jeffhaobdb76512011-09-07 11:43:16 -07001693
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07001694
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001695 // We need to ensure the work line is consistent while performing validation. When we spot a
1696 // peephole pattern we compute a new line for either the fallthrough instruction or the
1697 // branch target.
Ian Rogers700a4022014-05-19 16:49:03 -07001698 std::unique_ptr<RegisterLine> branch_line;
1699 std::unique_ptr<RegisterLine> fallthrough_line;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001700
Stephen Kyle7e541c92014-12-17 17:10:02 +00001701 /*
1702 * If we are in a constructor, and we currently have an UninitializedThis type
1703 * in a register somewhere, we need to make sure it isn't overwritten.
1704 */
1705 bool track_uninitialized_this = false;
1706 size_t uninitialized_this_loc = 0;
1707 if (IsConstructor()) {
1708 track_uninitialized_this = work_line_->GetUninitializedThisLoc(this, &uninitialized_this_loc);
1709 }
1710
Sebastien Hertz5243e912013-05-21 10:55:07 +02001711 switch (inst->Opcode()) {
jeffhaobdb76512011-09-07 11:43:16 -07001712 case Instruction::NOP:
1713 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001714 * A "pure" NOP has no effect on anything. Data tables start with
jeffhaobdb76512011-09-07 11:43:16 -07001715 * a signature that looks like a NOP; if we see one of these in
1716 * the course of executing code then we have a problem.
1717 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001718 if (inst->VRegA_10x() != 0) {
jeffhaod5347e02012-03-22 17:25:05 -07001719 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "encountered data table in instruction stream";
jeffhaobdb76512011-09-07 11:43:16 -07001720 }
1721 break;
1722
1723 case Instruction::MOVE:
Ian Rogers7b078e82014-09-10 14:44:24 -07001724 work_line_->CopyRegister1(this, inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategory1nr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001725 break;
jeffhaobdb76512011-09-07 11:43:16 -07001726 case Instruction::MOVE_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001727 work_line_->CopyRegister1(this, inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategory1nr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001728 break;
jeffhaobdb76512011-09-07 11:43:16 -07001729 case Instruction::MOVE_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001730 work_line_->CopyRegister1(this, inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategory1nr);
jeffhaobdb76512011-09-07 11:43:16 -07001731 break;
1732 case Instruction::MOVE_WIDE:
Ian Rogers7b078e82014-09-10 14:44:24 -07001733 work_line_->CopyRegister2(this, inst->VRegA_12x(), inst->VRegB_12x());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001734 break;
jeffhaobdb76512011-09-07 11:43:16 -07001735 case Instruction::MOVE_WIDE_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001736 work_line_->CopyRegister2(this, inst->VRegA_22x(), inst->VRegB_22x());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001737 break;
jeffhaobdb76512011-09-07 11:43:16 -07001738 case Instruction::MOVE_WIDE_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001739 work_line_->CopyRegister2(this, inst->VRegA_32x(), inst->VRegB_32x());
jeffhaobdb76512011-09-07 11:43:16 -07001740 break;
1741 case Instruction::MOVE_OBJECT:
Ian Rogers7b078e82014-09-10 14:44:24 -07001742 work_line_->CopyRegister1(this, inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategoryRef);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001743 break;
jeffhaobdb76512011-09-07 11:43:16 -07001744 case Instruction::MOVE_OBJECT_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001745 work_line_->CopyRegister1(this, inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategoryRef);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001746 break;
jeffhaobdb76512011-09-07 11:43:16 -07001747 case Instruction::MOVE_OBJECT_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001748 work_line_->CopyRegister1(this, inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategoryRef);
jeffhaobdb76512011-09-07 11:43:16 -07001749 break;
1750
1751 /*
1752 * The move-result instructions copy data out of a "pseudo-register"
jeffhaod1f0fde2011-09-08 17:25:33 -07001753 * with the results from the last method invocation. In practice we
jeffhaobdb76512011-09-07 11:43:16 -07001754 * might want to hold the result in an actual CPU register, so the
1755 * Dalvik spec requires that these only appear immediately after an
1756 * invoke or filled-new-array.
1757 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001758 * These calls invalidate the "result" register. (This is now
jeffhaobdb76512011-09-07 11:43:16 -07001759 * redundant with the reset done below, but it can make the debug info
1760 * easier to read in some cases.)
1761 */
1762 case Instruction::MOVE_RESULT:
Ian Rogers7b078e82014-09-10 14:44:24 -07001763 work_line_->CopyResultRegister1(this, inst->VRegA_11x(), false);
jeffhaobdb76512011-09-07 11:43:16 -07001764 break;
1765 case Instruction::MOVE_RESULT_WIDE:
Ian Rogers7b078e82014-09-10 14:44:24 -07001766 work_line_->CopyResultRegister2(this, inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07001767 break;
1768 case Instruction::MOVE_RESULT_OBJECT:
Ian Rogers7b078e82014-09-10 14:44:24 -07001769 work_line_->CopyResultRegister1(this, inst->VRegA_11x(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001770 break;
1771
Ian Rogersd81871c2011-10-03 13:57:23 -07001772 case Instruction::MOVE_EXCEPTION: {
Sebastien Hertz270a0e12015-01-16 19:49:09 +01001773 // We do not allow MOVE_EXCEPTION as the first instruction in a method. This is a simple case
1774 // where one entrypoint to the catch block is not actually an exception path.
1775 if (work_insn_idx_ == 0) {
1776 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "move-exception at pc 0x0";
1777 break;
1778 }
jeffhaobdb76512011-09-07 11:43:16 -07001779 /*
jeffhao60f83e32012-02-13 17:16:30 -08001780 * This statement can only appear as the first instruction in an exception handler. We verify
1781 * that as part of extracting the exception type from the catch block list.
jeffhaobdb76512011-09-07 11:43:16 -07001782 */
Ian Rogersd8f69b02014-09-10 21:43:52 +00001783 const RegType& res_type = GetCaughtExceptionType();
Ian Rogers7b078e82014-09-10 14:44:24 -07001784 work_line_->SetRegisterType(this, inst->VRegA_11x(), res_type);
jeffhaobdb76512011-09-07 11:43:16 -07001785 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001786 }
jeffhaobdb76512011-09-07 11:43:16 -07001787 case Instruction::RETURN_VOID:
Ian Rogers7b078e82014-09-10 14:44:24 -07001788 if (!IsConstructor() || work_line_->CheckConstructorReturn(this)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001789 if (!GetMethodReturnType().IsConflict()) {
jeffhaod5347e02012-03-22 17:25:05 -07001790 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001791 }
jeffhaobdb76512011-09-07 11:43:16 -07001792 }
1793 break;
1794 case Instruction::RETURN:
Ian Rogers7b078e82014-09-10 14:44:24 -07001795 if (!IsConstructor() || work_line_->CheckConstructorReturn(this)) {
jeffhaobdb76512011-09-07 11:43:16 -07001796 /* check the method signature */
Ian Rogersd8f69b02014-09-10 21:43:52 +00001797 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07001798 if (!return_type.IsCategory1Types()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001799 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected non-category 1 return type "
1800 << return_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001801 } else {
1802 // Compilers may generate synthetic functions that write byte values into boolean fields.
1803 // Also, it may use integer values for boolean, byte, short, and character return types.
Sebastien Hertz5243e912013-05-21 10:55:07 +02001804 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07001805 const RegType& src_type = work_line_->GetRegisterType(this, vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07001806 bool use_src = ((return_type.IsBoolean() && src_type.IsByte()) ||
1807 ((return_type.IsBoolean() || return_type.IsByte() ||
1808 return_type.IsShort() || return_type.IsChar()) &&
1809 src_type.IsInteger()));
1810 /* check the register contents */
Ian Rogersad0b3a32012-04-16 14:50:24 -07001811 bool success =
Ian Rogers7b078e82014-09-10 14:44:24 -07001812 work_line_->VerifyRegisterType(this, vregA, use_src ? src_type : return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001813 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001814 AppendToLastFailMessage(StringPrintf(" return-1nr on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07001815 }
jeffhaobdb76512011-09-07 11:43:16 -07001816 }
1817 }
1818 break;
1819 case Instruction::RETURN_WIDE:
Ian Rogers7b078e82014-09-10 14:44:24 -07001820 if (!IsConstructor() || work_line_->CheckConstructorReturn(this)) {
jeffhaobdb76512011-09-07 11:43:16 -07001821 /* check the method signature */
Ian Rogersd8f69b02014-09-10 21:43:52 +00001822 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07001823 if (!return_type.IsCategory2Types()) {
jeffhaod5347e02012-03-22 17:25:05 -07001824 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-wide not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001825 } else {
1826 /* check the register contents */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001827 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07001828 bool success = work_line_->VerifyRegisterType(this, vregA, return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001829 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001830 AppendToLastFailMessage(StringPrintf(" return-wide on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07001831 }
jeffhaobdb76512011-09-07 11:43:16 -07001832 }
1833 }
1834 break;
1835 case Instruction::RETURN_OBJECT:
Ian Rogers7b078e82014-09-10 14:44:24 -07001836 if (!IsConstructor() || work_line_->CheckConstructorReturn(this)) {
Ian Rogersd8f69b02014-09-10 21:43:52 +00001837 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07001838 if (!return_type.IsReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001839 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-object not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001840 } else {
1841 /* return_type is the *expected* return type, not register value */
1842 DCHECK(!return_type.IsZero());
1843 DCHECK(!return_type.IsUninitializedReference());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001844 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07001845 const RegType& reg_type = work_line_->GetRegisterType(this, vregA);
Andreas Gampea32210c2015-06-24 10:26:13 -07001846 // Disallow returning undefined, conflict & uninitialized values and verify that the
1847 // reference in vAA is an instance of the "return_type."
1848 if (reg_type.IsUndefined()) {
1849 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning undefined register";
1850 } else if (reg_type.IsConflict()) {
1851 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning register with conflict";
1852 } else if (reg_type.IsUninitializedTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001853 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "returning uninitialized object '"
1854 << reg_type << "'";
Ian Rogers9074b992011-10-26 17:41:55 -07001855 } else if (!return_type.IsAssignableFrom(reg_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07001856 if (reg_type.IsUnresolvedTypes() || return_type.IsUnresolvedTypes()) {
1857 Fail(VERIFY_ERROR_NO_CLASS) << " can't resolve returned type '" << return_type
1858 << "' or '" << reg_type << "'";
1859 } else {
Andreas Gampe16f149c2015-03-23 10:10:20 -07001860 bool soft_error = false;
1861 // Check whether arrays are involved. They will show a valid class status, even
1862 // if their components are erroneous.
1863 if (reg_type.IsArrayTypes() && return_type.IsArrayTypes()) {
1864 return_type.CanAssignArray(reg_type, reg_types_, class_loader_, &soft_error);
1865 if (soft_error) {
1866 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "array with erroneous component type: "
1867 << reg_type << " vs " << return_type;
1868 }
1869 }
1870
1871 if (!soft_error) {
1872 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning '" << reg_type
1873 << "', but expected from declaration '" << return_type << "'";
1874 }
Jeff Haoa3faaf42013-09-03 19:07:00 -07001875 }
jeffhaobdb76512011-09-07 11:43:16 -07001876 }
1877 }
1878 }
1879 break;
1880
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001881 /* could be boolean, int, float, or a null reference */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001882 case Instruction::CONST_4: {
1883 int32_t val = static_cast<int32_t>(inst->VRegB_11n() << 28) >> 28;
Ian Rogers7b078e82014-09-10 14:44:24 -07001884 work_line_->SetRegisterType(this, inst->VRegA_11n(),
Ian Rogers46960fe2014-05-23 10:43:43 -07001885 DetermineCat1Constant(val, need_precise_constants_));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001886 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02001887 }
1888 case Instruction::CONST_16: {
1889 int16_t val = static_cast<int16_t>(inst->VRegB_21s());
Ian Rogers7b078e82014-09-10 14:44:24 -07001890 work_line_->SetRegisterType(this, inst->VRegA_21s(),
Ian Rogers46960fe2014-05-23 10:43:43 -07001891 DetermineCat1Constant(val, need_precise_constants_));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001892 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02001893 }
Sebastien Hertz849600b2013-12-20 10:28:08 +01001894 case Instruction::CONST: {
1895 int32_t val = inst->VRegB_31i();
Ian Rogers7b078e82014-09-10 14:44:24 -07001896 work_line_->SetRegisterType(this, inst->VRegA_31i(),
Ian Rogers46960fe2014-05-23 10:43:43 -07001897 DetermineCat1Constant(val, need_precise_constants_));
jeffhaobdb76512011-09-07 11:43:16 -07001898 break;
Sebastien Hertz849600b2013-12-20 10:28:08 +01001899 }
1900 case Instruction::CONST_HIGH16: {
1901 int32_t val = static_cast<int32_t>(inst->VRegB_21h() << 16);
Ian Rogers7b078e82014-09-10 14:44:24 -07001902 work_line_->SetRegisterType(this, inst->VRegA_21h(),
Ian Rogers46960fe2014-05-23 10:43:43 -07001903 DetermineCat1Constant(val, need_precise_constants_));
jeffhaobdb76512011-09-07 11:43:16 -07001904 break;
Sebastien Hertz849600b2013-12-20 10:28:08 +01001905 }
jeffhaobdb76512011-09-07 11:43:16 -07001906 /* could be long or double; resolved upon use */
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001907 case Instruction::CONST_WIDE_16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001908 int64_t val = static_cast<int16_t>(inst->VRegB_21s());
Ian Rogersd8f69b02014-09-10 21:43:52 +00001909 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1910 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07001911 work_line_->SetRegisterTypeWide(this, inst->VRegA_21s(), lo, hi);
jeffhaobdb76512011-09-07 11:43:16 -07001912 break;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001913 }
1914 case Instruction::CONST_WIDE_32: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001915 int64_t val = static_cast<int32_t>(inst->VRegB_31i());
Ian Rogersd8f69b02014-09-10 21:43:52 +00001916 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1917 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07001918 work_line_->SetRegisterTypeWide(this, inst->VRegA_31i(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001919 break;
1920 }
1921 case Instruction::CONST_WIDE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001922 int64_t val = inst->VRegB_51l();
Ian Rogersd8f69b02014-09-10 21:43:52 +00001923 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1924 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07001925 work_line_->SetRegisterTypeWide(this, inst->VRegA_51l(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001926 break;
1927 }
1928 case Instruction::CONST_WIDE_HIGH16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001929 int64_t val = static_cast<uint64_t>(inst->VRegB_21h()) << 48;
Ian Rogersd8f69b02014-09-10 21:43:52 +00001930 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1931 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07001932 work_line_->SetRegisterTypeWide(this, inst->VRegA_21h(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001933 break;
1934 }
jeffhaobdb76512011-09-07 11:43:16 -07001935 case Instruction::CONST_STRING:
Ian Rogers7b078e82014-09-10 14:44:24 -07001936 work_line_->SetRegisterType(this, inst->VRegA_21c(), reg_types_.JavaLangString());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001937 break;
jeffhaobdb76512011-09-07 11:43:16 -07001938 case Instruction::CONST_STRING_JUMBO:
Ian Rogers7b078e82014-09-10 14:44:24 -07001939 work_line_->SetRegisterType(this, inst->VRegA_31c(), reg_types_.JavaLangString());
jeffhaobdb76512011-09-07 11:43:16 -07001940 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001941 case Instruction::CONST_CLASS: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001942 // Get type from instruction if unresolved then we need an access check
1943 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Ian Rogersd8f69b02014-09-10 21:43:52 +00001944 const RegType& res_type = ResolveClassAndCheckAccess(inst->VRegB_21c());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001945 // Register holds class, ie its type is class, on error it will hold Conflict.
Ian Rogers7b078e82014-09-10 14:44:24 -07001946 work_line_->SetRegisterType(this, inst->VRegA_21c(),
Ian Rogersb4903572012-10-11 11:52:56 -07001947 res_type.IsConflict() ? res_type
Ian Rogers7b078e82014-09-10 14:44:24 -07001948 : reg_types_.JavaLangClass());
jeffhaobdb76512011-09-07 11:43:16 -07001949 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001950 }
jeffhaobdb76512011-09-07 11:43:16 -07001951 case Instruction::MONITOR_ENTER:
Ian Rogers7b078e82014-09-10 14:44:24 -07001952 work_line_->PushMonitor(this, inst->VRegA_11x(), work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07001953 break;
1954 case Instruction::MONITOR_EXIT:
1955 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001956 * monitor-exit instructions are odd. They can throw exceptions,
jeffhaobdb76512011-09-07 11:43:16 -07001957 * but when they do they act as if they succeeded and the PC is
jeffhaod1f0fde2011-09-08 17:25:33 -07001958 * pointing to the following instruction. (This behavior goes back
jeffhaobdb76512011-09-07 11:43:16 -07001959 * to the need to handle asynchronous exceptions, a now-deprecated
1960 * feature that Dalvik doesn't support.)
1961 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001962 * In practice we don't need to worry about this. The only
jeffhaobdb76512011-09-07 11:43:16 -07001963 * exceptions that can be thrown from monitor-exit are for a
jeffhaod1f0fde2011-09-08 17:25:33 -07001964 * null reference and -exit without a matching -enter. If the
jeffhaobdb76512011-09-07 11:43:16 -07001965 * structured locking checks are working, the former would have
1966 * failed on the -enter instruction, and the latter is impossible.
1967 *
1968 * This is fortunate, because issue 3221411 prevents us from
1969 * chasing the "can throw" path when monitor verification is
jeffhaod1f0fde2011-09-08 17:25:33 -07001970 * enabled. If we can fully verify the locking we can ignore
jeffhaobdb76512011-09-07 11:43:16 -07001971 * some catch blocks (which will show up as "dead" code when
1972 * we skip them here); if we can't, then the code path could be
1973 * "live" so we still need to check it.
1974 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001975 opcode_flags &= ~Instruction::kThrow;
Ian Rogers7b078e82014-09-10 14:44:24 -07001976 work_line_->PopMonitor(this, inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07001977 break;
1978
Ian Rogers28ad40d2011-10-27 15:19:26 -07001979 case Instruction::CHECK_CAST:
Ian Rogersd81871c2011-10-03 13:57:23 -07001980 case Instruction::INSTANCE_OF: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001981 /*
1982 * If this instruction succeeds, we will "downcast" register vA to the type in vB. (This
1983 * could be a "upcast" -- not expected, so we don't try to address it.)
1984 *
1985 * If it fails, an exception is thrown, which we deal with later by ignoring the update to
Elliott Hughesadb8c672012-03-06 16:49:32 -08001986 * dec_insn.vA when branching to a handler.
Ian Rogers28ad40d2011-10-27 15:19:26 -07001987 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001988 const bool is_checkcast = (inst->Opcode() == Instruction::CHECK_CAST);
1989 const uint32_t type_idx = (is_checkcast) ? inst->VRegB_21c() : inst->VRegC_22c();
Ian Rogersd8f69b02014-09-10 21:43:52 +00001990 const RegType& res_type = ResolveClassAndCheckAccess(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001991 if (res_type.IsConflict()) {
Andreas Gampe00633eb2014-07-17 16:13:35 -07001992 // If this is a primitive type, fail HARD.
Mathieu Chartierbf99f772014-08-23 16:37:27 -07001993 mirror::Class* klass = dex_cache_->GetResolvedType(type_idx);
Andreas Gampe00633eb2014-07-17 16:13:35 -07001994 if (klass != nullptr && klass->IsPrimitive()) {
1995 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "using primitive type "
1996 << dex_file_->StringByTypeIdx(type_idx) << " in instanceof in "
1997 << GetDeclaringClass();
1998 break;
1999 }
2000
Ian Rogersad0b3a32012-04-16 14:50:24 -07002001 DCHECK_NE(failures_.size(), 0U);
2002 if (!is_checkcast) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002003 work_line_->SetRegisterType(this, inst->VRegA_22c(), reg_types_.Boolean());
Ian Rogersad0b3a32012-04-16 14:50:24 -07002004 }
2005 break; // bad class
Ian Rogers9f1ab122011-12-12 08:52:43 -08002006 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002007 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Sebastien Hertz5243e912013-05-21 10:55:07 +02002008 uint32_t orig_type_reg = (is_checkcast) ? inst->VRegA_21c() : inst->VRegB_22c();
Ian Rogers7b078e82014-09-10 14:44:24 -07002009 const RegType& orig_type = work_line_->GetRegisterType(this, orig_type_reg);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002010 if (!res_type.IsNonZeroReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002011 if (is_checkcast) {
2012 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on unexpected class " << res_type;
2013 } else {
2014 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on unexpected class " << res_type;
2015 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002016 } else if (!orig_type.IsReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002017 if (is_checkcast) {
2018 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on non-reference in v" << orig_type_reg;
2019 } else {
2020 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on non-reference in v" << orig_type_reg;
2021 }
jeffhao2a8a90e2011-09-26 14:25:31 -07002022 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002023 if (is_checkcast) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002024 work_line_->SetRegisterType(this, inst->VRegA_21c(), res_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07002025 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07002026 work_line_->SetRegisterType(this, inst->VRegA_22c(), reg_types_.Boolean());
jeffhaobdb76512011-09-07 11:43:16 -07002027 }
jeffhaobdb76512011-09-07 11:43:16 -07002028 }
jeffhao2a8a90e2011-09-26 14:25:31 -07002029 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002030 }
2031 case Instruction::ARRAY_LENGTH: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002032 const RegType& res_type = work_line_->GetRegisterType(this, inst->VRegB_12x());
Ian Rogers28ad40d2011-10-27 15:19:26 -07002033 if (res_type.IsReferenceTypes()) {
Ian Rogers89310de2012-02-01 13:47:30 -08002034 if (!res_type.IsArrayTypes() && !res_type.IsZero()) { // ie not an array or null
jeffhaod5347e02012-03-22 17:25:05 -07002035 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002036 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07002037 work_line_->SetRegisterType(this, inst->VRegA_12x(), reg_types_.Integer());
Ian Rogersd81871c2011-10-03 13:57:23 -07002038 }
Andreas Gampe65c9db82014-07-28 13:14:34 -07002039 } else {
2040 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002041 }
2042 break;
2043 }
2044 case Instruction::NEW_INSTANCE: {
Ian Rogersd8f69b02014-09-10 21:43:52 +00002045 const RegType& res_type = ResolveClassAndCheckAccess(inst->VRegB_21c());
Ian Rogersad0b3a32012-04-16 14:50:24 -07002046 if (res_type.IsConflict()) {
2047 DCHECK_NE(failures_.size(), 0U);
2048 break; // bad class
jeffhao8cd6dda2012-02-22 10:15:34 -08002049 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002050 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
2051 // can't create an instance of an interface or abstract class */
2052 if (!res_type.IsInstantiableTypes()) {
2053 Fail(VERIFY_ERROR_INSTANTIATION)
2054 << "new-instance on primitive, interface or abstract class" << res_type;
Ian Rogers08f753d2012-08-24 14:35:25 -07002055 // Soft failure so carry on to set register type.
Ian Rogersd81871c2011-10-03 13:57:23 -07002056 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00002057 const RegType& uninit_type = reg_types_.Uninitialized(res_type, work_insn_idx_);
Ian Rogers08f753d2012-08-24 14:35:25 -07002058 // Any registers holding previous allocations from this address that have not yet been
2059 // initialized must be marked invalid.
Ian Rogers7b078e82014-09-10 14:44:24 -07002060 work_line_->MarkUninitRefsAsInvalid(this, uninit_type);
Ian Rogers08f753d2012-08-24 14:35:25 -07002061 // add the new uninitialized reference to the register state
Ian Rogers7b078e82014-09-10 14:44:24 -07002062 work_line_->SetRegisterType(this, inst->VRegA_21c(), uninit_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07002063 break;
2064 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08002065 case Instruction::NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002066 VerifyNewArray(inst, false, false);
jeffhaobdb76512011-09-07 11:43:16 -07002067 break;
2068 case Instruction::FILLED_NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002069 VerifyNewArray(inst, true, false);
Ian Rogers0c4a5062012-02-03 15:18:59 -08002070 just_set_result = true; // Filled new array sets result register
jeffhaobdb76512011-09-07 11:43:16 -07002071 break;
Ian Rogers0c4a5062012-02-03 15:18:59 -08002072 case Instruction::FILLED_NEW_ARRAY_RANGE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002073 VerifyNewArray(inst, true, true);
Ian Rogers0c4a5062012-02-03 15:18:59 -08002074 just_set_result = true; // Filled new array range sets result register
2075 break;
jeffhaobdb76512011-09-07 11:43:16 -07002076 case Instruction::CMPL_FLOAT:
2077 case Instruction::CMPG_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002078 if (!work_line_->VerifyRegisterType(this, inst->VRegB_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08002079 break;
2080 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002081 if (!work_line_->VerifyRegisterType(this, inst->VRegC_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08002082 break;
2083 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002084 work_line_->SetRegisterType(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002085 break;
2086 case Instruction::CMPL_DOUBLE:
2087 case Instruction::CMPG_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002088 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegB_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002089 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002090 break;
2091 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002092 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegC_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002093 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002094 break;
2095 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002096 work_line_->SetRegisterType(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002097 break;
2098 case Instruction::CMP_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002099 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegB_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002100 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002101 break;
2102 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002103 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegC_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002104 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002105 break;
2106 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002107 work_line_->SetRegisterType(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002108 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002109 case Instruction::THROW: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002110 const RegType& res_type = work_line_->GetRegisterType(this, inst->VRegA_11x());
Ian Rogersb4903572012-10-11 11:52:56 -07002111 if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(res_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07002112 Fail(res_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS : VERIFY_ERROR_BAD_CLASS_SOFT)
2113 << "thrown class " << res_type << " not instanceof Throwable";
jeffhaobdb76512011-09-07 11:43:16 -07002114 }
2115 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002116 }
jeffhaobdb76512011-09-07 11:43:16 -07002117 case Instruction::GOTO:
2118 case Instruction::GOTO_16:
2119 case Instruction::GOTO_32:
2120 /* no effect on or use of registers */
2121 break;
2122
2123 case Instruction::PACKED_SWITCH:
2124 case Instruction::SPARSE_SWITCH:
2125 /* verify that vAA is an integer, or can be converted to one */
Ian Rogers7b078e82014-09-10 14:44:24 -07002126 work_line_->VerifyRegisterType(this, inst->VRegA_31t(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002127 break;
2128
Ian Rogersd81871c2011-10-03 13:57:23 -07002129 case Instruction::FILL_ARRAY_DATA: {
2130 /* Similar to the verification done for APUT */
Ian Rogers7b078e82014-09-10 14:44:24 -07002131 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegA_31t());
Ian Rogers89310de2012-02-01 13:47:30 -08002132 /* array_type can be null if the reg type is Zero */
2133 if (!array_type.IsZero()) {
jeffhao457cc512012-02-02 16:55:13 -08002134 if (!array_type.IsArrayTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002135 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with array type "
2136 << array_type;
Ian Rogers89310de2012-02-01 13:47:30 -08002137 } else {
Ian Rogersd8f69b02014-09-10 21:43:52 +00002138 const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
Ian Rogersad0b3a32012-04-16 14:50:24 -07002139 DCHECK(!component_type.IsConflict());
jeffhao457cc512012-02-02 16:55:13 -08002140 if (component_type.IsNonZeroReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002141 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with component type "
2142 << component_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002143 } else {
jeffhao457cc512012-02-02 16:55:13 -08002144 // Now verify if the element width in the table matches the element width declared in
2145 // the array
2146 const uint16_t* array_data = insns + (insns[1] | (((int32_t) insns[2]) << 16));
2147 if (array_data[0] != Instruction::kArrayDataSignature) {
jeffhaod5347e02012-03-22 17:25:05 -07002148 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid magic for array-data";
jeffhao457cc512012-02-02 16:55:13 -08002149 } else {
2150 size_t elem_width = Primitive::ComponentSize(component_type.GetPrimitiveType());
2151 // Since we don't compress the data in Dex, expect to see equal width of data stored
2152 // in the table and expected from the array class.
2153 if (array_data[1] != elem_width) {
jeffhaod5347e02012-03-22 17:25:05 -07002154 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-data size mismatch (" << array_data[1]
2155 << " vs " << elem_width << ")";
jeffhao457cc512012-02-02 16:55:13 -08002156 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002157 }
2158 }
jeffhaobdb76512011-09-07 11:43:16 -07002159 }
2160 }
2161 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002162 }
jeffhaobdb76512011-09-07 11:43:16 -07002163 case Instruction::IF_EQ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002164 case Instruction::IF_NE: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002165 const RegType& reg_type1 = work_line_->GetRegisterType(this, inst->VRegA_22t());
2166 const RegType& reg_type2 = work_line_->GetRegisterType(this, inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002167 bool mismatch = false;
2168 if (reg_type1.IsZero()) { // zero then integral or reference expected
2169 mismatch = !reg_type2.IsReferenceTypes() && !reg_type2.IsIntegralTypes();
2170 } else if (reg_type1.IsReferenceTypes()) { // both references?
2171 mismatch = !reg_type2.IsReferenceTypes();
2172 } else { // both integral?
2173 mismatch = !reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes();
2174 }
2175 if (mismatch) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002176 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to if-eq/if-ne (" << reg_type1 << ","
2177 << reg_type2 << ") must both be references or integral";
jeffhaobdb76512011-09-07 11:43:16 -07002178 }
2179 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002180 }
jeffhaobdb76512011-09-07 11:43:16 -07002181 case Instruction::IF_LT:
2182 case Instruction::IF_GE:
2183 case Instruction::IF_GT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002184 case Instruction::IF_LE: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002185 const RegType& reg_type1 = work_line_->GetRegisterType(this, inst->VRegA_22t());
2186 const RegType& reg_type2 = work_line_->GetRegisterType(this, inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002187 if (!reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002188 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to 'if' (" << reg_type1 << ","
2189 << reg_type2 << ") must be integral";
jeffhaobdb76512011-09-07 11:43:16 -07002190 }
2191 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002192 }
jeffhaobdb76512011-09-07 11:43:16 -07002193 case Instruction::IF_EQZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002194 case Instruction::IF_NEZ: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002195 const RegType& reg_type = work_line_->GetRegisterType(this, inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002196 if (!reg_type.IsReferenceTypes() && !reg_type.IsIntegralTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002197 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
2198 << " unexpected as arg to if-eqz/if-nez";
Ian Rogersd81871c2011-10-03 13:57:23 -07002199 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002200
2201 // Find previous instruction - its existence is a precondition to peephole optimization.
Ian Rogers9b360392013-06-06 14:45:07 -07002202 uint32_t instance_of_idx = 0;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002203 if (0 != work_insn_idx_) {
Ian Rogers9b360392013-06-06 14:45:07 -07002204 instance_of_idx = work_insn_idx_ - 1;
Brian Carlstromdf629502013-07-17 22:39:56 -07002205 while (0 != instance_of_idx && !insn_flags_[instance_of_idx].IsOpcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07002206 instance_of_idx--;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002207 }
Andreas Gampe7c038102014-10-27 20:08:46 -07002208 if (FailOrAbort(this, insn_flags_[instance_of_idx].IsOpcode(),
2209 "Unable to get previous instruction of if-eqz/if-nez for work index ",
2210 work_insn_idx_)) {
2211 break;
2212 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002213 } else {
2214 break;
2215 }
2216
Ian Rogers9b360392013-06-06 14:45:07 -07002217 const Instruction* instance_of_inst = Instruction::At(code_item_->insns_ + instance_of_idx);
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002218
2219 /* Check for peep-hole pattern of:
2220 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002221 * instance-of vX, vY, T;
2222 * ifXXX vX, label ;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002223 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002224 * label:
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002225 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002226 * and sharpen the type of vY to be type T.
2227 * Note, this pattern can't be if:
2228 * - if there are other branches to this branch,
2229 * - when vX == vY.
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002230 */
Ian Rogersfae370a2013-06-05 08:33:27 -07002231 if (!CurrentInsnFlags()->IsBranchTarget() &&
Ian Rogers9b360392013-06-06 14:45:07 -07002232 (Instruction::INSTANCE_OF == instance_of_inst->Opcode()) &&
2233 (inst->VRegA_21t() == instance_of_inst->VRegA_22c()) &&
2234 (instance_of_inst->VRegA_22c() != instance_of_inst->VRegB_22c())) {
Ian Rogersebbdd872014-07-07 23:53:08 -07002235 // Check the type of the instance-of is different than that of registers type, as if they
2236 // are the same there is no work to be done here. Check that the conversion is not to or
2237 // from an unresolved type as type information is imprecise. If the instance-of is to an
2238 // interface then ignore the type information as interfaces can only be treated as Objects
2239 // and we don't want to disallow field and other operations on the object. If the value
2240 // being instance-of checked against is known null (zero) then allow the optimization as
2241 // we didn't have type information. If the merge of the instance-of type with the original
2242 // type is assignable to the original then allow optimization. This check is performed to
2243 // ensure that subsequent merges don't lose type information - such as becoming an
2244 // interface from a class that would lose information relevant to field checks.
Ian Rogers7b078e82014-09-10 14:44:24 -07002245 const RegType& orig_type = work_line_->GetRegisterType(this, instance_of_inst->VRegB_22c());
Ian Rogersd8f69b02014-09-10 21:43:52 +00002246 const RegType& cast_type = ResolveClassAndCheckAccess(instance_of_inst->VRegC_22c());
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002247
Ian Rogersebbdd872014-07-07 23:53:08 -07002248 if (!orig_type.Equals(cast_type) &&
2249 !cast_type.IsUnresolvedTypes() && !orig_type.IsUnresolvedTypes() &&
Andreas Gampe00633eb2014-07-17 16:13:35 -07002250 cast_type.HasClass() && // Could be conflict type, make sure it has a class.
Ian Rogersebbdd872014-07-07 23:53:08 -07002251 !cast_type.GetClass()->IsInterface() &&
2252 (orig_type.IsZero() ||
2253 orig_type.IsStrictlyAssignableFrom(cast_type.Merge(orig_type, &reg_types_)))) {
Ian Rogersd0fbd852013-09-24 18:17:04 -07002254 RegisterLine* update_line = RegisterLine::Create(code_item_->registers_size_, this);
Ian Rogersfae370a2013-06-05 08:33:27 -07002255 if (inst->Opcode() == Instruction::IF_EQZ) {
Ian Rogers9b360392013-06-06 14:45:07 -07002256 fallthrough_line.reset(update_line);
Ian Rogersfae370a2013-06-05 08:33:27 -07002257 } else {
Ian Rogers9b360392013-06-06 14:45:07 -07002258 branch_line.reset(update_line);
2259 }
2260 update_line->CopyFromLine(work_line_.get());
Ian Rogers7b078e82014-09-10 14:44:24 -07002261 update_line->SetRegisterType(this, instance_of_inst->VRegB_22c(), cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002262 if (!insn_flags_[instance_of_idx].IsBranchTarget() && 0 != instance_of_idx) {
2263 // See if instance-of was preceded by a move-object operation, common due to the small
2264 // register encoding space of instance-of, and propagate type information to the source
2265 // of the move-object.
2266 uint32_t move_idx = instance_of_idx - 1;
Brian Carlstromdf629502013-07-17 22:39:56 -07002267 while (0 != move_idx && !insn_flags_[move_idx].IsOpcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07002268 move_idx--;
2269 }
Andreas Gampe7c038102014-10-27 20:08:46 -07002270 if (FailOrAbort(this, insn_flags_[move_idx].IsOpcode(),
2271 "Unable to get previous instruction of if-eqz/if-nez for work index ",
2272 work_insn_idx_)) {
2273 break;
2274 }
Ian Rogers9b360392013-06-06 14:45:07 -07002275 const Instruction* move_inst = Instruction::At(code_item_->insns_ + move_idx);
2276 switch (move_inst->Opcode()) {
2277 case Instruction::MOVE_OBJECT:
2278 if (move_inst->VRegA_12x() == instance_of_inst->VRegB_22c()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002279 update_line->SetRegisterType(this, move_inst->VRegB_12x(), cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002280 }
2281 break;
2282 case Instruction::MOVE_OBJECT_FROM16:
2283 if (move_inst->VRegA_22x() == instance_of_inst->VRegB_22c()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002284 update_line->SetRegisterType(this, move_inst->VRegB_22x(), cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002285 }
2286 break;
2287 case Instruction::MOVE_OBJECT_16:
2288 if (move_inst->VRegA_32x() == instance_of_inst->VRegB_22c()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002289 update_line->SetRegisterType(this, move_inst->VRegB_32x(), cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002290 }
2291 break;
2292 default:
2293 break;
2294 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002295 }
2296 }
2297 }
2298
jeffhaobdb76512011-09-07 11:43:16 -07002299 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002300 }
jeffhaobdb76512011-09-07 11:43:16 -07002301 case Instruction::IF_LTZ:
2302 case Instruction::IF_GEZ:
2303 case Instruction::IF_GTZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002304 case Instruction::IF_LEZ: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002305 const RegType& reg_type = work_line_->GetRegisterType(this, inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002306 if (!reg_type.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002307 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
2308 << " unexpected as arg to if-ltz/if-gez/if-gtz/if-lez";
Ian Rogersd81871c2011-10-03 13:57:23 -07002309 }
jeffhaobdb76512011-09-07 11:43:16 -07002310 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002311 }
jeffhaobdb76512011-09-07 11:43:16 -07002312 case Instruction::AGET_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002313 VerifyAGet(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002314 break;
jeffhaobdb76512011-09-07 11:43:16 -07002315 case Instruction::AGET_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002316 VerifyAGet(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002317 break;
jeffhaobdb76512011-09-07 11:43:16 -07002318 case Instruction::AGET_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002319 VerifyAGet(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002320 break;
jeffhaobdb76512011-09-07 11:43:16 -07002321 case Instruction::AGET_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002322 VerifyAGet(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002323 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002324 case Instruction::AGET:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002325 VerifyAGet(inst, reg_types_.Integer(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002326 break;
jeffhaobdb76512011-09-07 11:43:16 -07002327 case Instruction::AGET_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002328 VerifyAGet(inst, reg_types_.LongLo(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002329 break;
2330 case Instruction::AGET_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002331 VerifyAGet(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07002332 break;
2333
Ian Rogersd81871c2011-10-03 13:57:23 -07002334 case Instruction::APUT_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002335 VerifyAPut(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002336 break;
2337 case Instruction::APUT_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002338 VerifyAPut(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002339 break;
2340 case Instruction::APUT_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002341 VerifyAPut(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002342 break;
2343 case Instruction::APUT_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002344 VerifyAPut(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002345 break;
2346 case Instruction::APUT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002347 VerifyAPut(inst, reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002348 break;
2349 case Instruction::APUT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002350 VerifyAPut(inst, reg_types_.LongLo(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002351 break;
2352 case Instruction::APUT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002353 VerifyAPut(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07002354 break;
2355
jeffhaobdb76512011-09-07 11:43:16 -07002356 case Instruction::IGET_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002357 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002358 break;
jeffhaobdb76512011-09-07 11:43:16 -07002359 case Instruction::IGET_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002360 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002361 break;
jeffhaobdb76512011-09-07 11:43:16 -07002362 case Instruction::IGET_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002363 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002364 break;
jeffhaobdb76512011-09-07 11:43:16 -07002365 case Instruction::IGET_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002366 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002367 break;
2368 case Instruction::IGET:
Andreas Gampe896df402014-10-20 22:25:29 -07002369 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002370 break;
2371 case Instruction::IGET_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002372 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002373 break;
2374 case Instruction::IGET_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002375 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false,
2376 false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002377 break;
jeffhaobdb76512011-09-07 11:43:16 -07002378
Ian Rogersd81871c2011-10-03 13:57:23 -07002379 case Instruction::IPUT_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002380 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002381 break;
2382 case Instruction::IPUT_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002383 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002384 break;
2385 case Instruction::IPUT_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002386 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002387 break;
2388 case Instruction::IPUT_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002389 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002390 break;
2391 case Instruction::IPUT:
Andreas Gampe896df402014-10-20 22:25:29 -07002392 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002393 break;
2394 case Instruction::IPUT_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002395 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002396 break;
jeffhaobdb76512011-09-07 11:43:16 -07002397 case Instruction::IPUT_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002398 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false,
2399 false);
jeffhaobdb76512011-09-07 11:43:16 -07002400 break;
2401
jeffhaobdb76512011-09-07 11:43:16 -07002402 case Instruction::SGET_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002403 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002404 break;
jeffhaobdb76512011-09-07 11:43:16 -07002405 case Instruction::SGET_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002406 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002407 break;
jeffhaobdb76512011-09-07 11:43:16 -07002408 case Instruction::SGET_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002409 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002410 break;
jeffhaobdb76512011-09-07 11:43:16 -07002411 case Instruction::SGET_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002412 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002413 break;
2414 case Instruction::SGET:
Andreas Gampe896df402014-10-20 22:25:29 -07002415 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002416 break;
2417 case Instruction::SGET_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002418 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002419 break;
2420 case Instruction::SGET_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002421 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false,
2422 true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002423 break;
2424
2425 case Instruction::SPUT_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002426 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002427 break;
2428 case Instruction::SPUT_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002429 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002430 break;
2431 case Instruction::SPUT_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002432 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002433 break;
2434 case Instruction::SPUT_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002435 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002436 break;
2437 case Instruction::SPUT:
Andreas Gampe896df402014-10-20 22:25:29 -07002438 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002439 break;
2440 case Instruction::SPUT_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002441 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002442 break;
2443 case Instruction::SPUT_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002444 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false,
2445 true);
jeffhaobdb76512011-09-07 11:43:16 -07002446 break;
2447
2448 case Instruction::INVOKE_VIRTUAL:
2449 case Instruction::INVOKE_VIRTUAL_RANGE:
2450 case Instruction::INVOKE_SUPER:
Ian Rogersd81871c2011-10-03 13:57:23 -07002451 case Instruction::INVOKE_SUPER_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002452 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE ||
2453 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002454 bool is_super = (inst->Opcode() == Instruction::INVOKE_SUPER ||
2455 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002456 ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_VIRTUAL, is_range, is_super);
Ian Rogersd8f69b02014-09-10 21:43:52 +00002457 const RegType* return_type = nullptr;
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002458 if (called_method != nullptr) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002459 StackHandleScope<1> hs(self_);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002460 mirror::Class* return_type_class = called_method->GetReturnType(can_load_classes_);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002461 if (return_type_class != nullptr) {
Andreas Gampef23f33d2015-06-23 14:18:17 -07002462 return_type = &FromClass(called_method->GetReturnTypeDescriptor(),
2463 return_type_class,
2464 return_type_class->CannotBeAssignedFromOtherTypes());
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002465 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07002466 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
2467 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002468 }
2469 }
2470 if (return_type == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002471 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002472 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2473 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002474 const char* descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Mathieu Chartierbf99f772014-08-23 16:37:27 -07002475 return_type = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
jeffhaobdb76512011-09-07 11:43:16 -07002476 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002477 if (!return_type->IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002478 work_line_->SetResultRegisterType(this, *return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002479 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002480 work_line_->SetResultRegisterTypeWide(*return_type, return_type->HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002481 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002482 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002483 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002484 }
jeffhaobdb76512011-09-07 11:43:16 -07002485 case Instruction::INVOKE_DIRECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002486 case Instruction::INVOKE_DIRECT_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002487 bool is_range = (inst->Opcode() == Instruction::INVOKE_DIRECT_RANGE);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002488 ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_DIRECT, is_range, false);
Ian Rogers46685432012-06-03 22:26:43 -07002489 const char* return_type_descriptor;
2490 bool is_constructor;
Ian Rogersd8f69b02014-09-10 21:43:52 +00002491 const RegType* return_type = nullptr;
Ian Rogers7b078e82014-09-10 14:44:24 -07002492 if (called_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002493 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogers46685432012-06-03 22:26:43 -07002494 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
Ian Rogersdfb325e2013-10-30 01:00:44 -07002495 is_constructor = strcmp("<init>", dex_file_->StringDataByIdx(method_id.name_idx_)) == 0;
Ian Rogers46685432012-06-03 22:26:43 -07002496 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
2497 return_type_descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2498 } else {
2499 is_constructor = called_method->IsConstructor();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002500 return_type_descriptor = called_method->GetReturnTypeDescriptor();
Ian Rogers7b078e82014-09-10 14:44:24 -07002501 StackHandleScope<1> hs(self_);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002502 mirror::Class* return_type_class = called_method->GetReturnType(can_load_classes_);
Ian Rogers1ff3c982014-08-12 02:30:58 -07002503 if (return_type_class != nullptr) {
Andreas Gampef23f33d2015-06-23 14:18:17 -07002504 return_type = &FromClass(return_type_descriptor,
2505 return_type_class,
2506 return_type_class->CannotBeAssignedFromOtherTypes());
Ian Rogers1ff3c982014-08-12 02:30:58 -07002507 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07002508 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
2509 self_->ClearException();
Ian Rogers1ff3c982014-08-12 02:30:58 -07002510 }
Ian Rogers46685432012-06-03 22:26:43 -07002511 }
2512 if (is_constructor) {
jeffhaobdb76512011-09-07 11:43:16 -07002513 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002514 * Some additional checks when calling a constructor. We know from the invocation arg check
2515 * that the "this" argument is an instance of called_method->klass. Now we further restrict
2516 * that to require that called_method->klass is the same as this->klass or this->super,
2517 * allowing the latter only if the "this" argument is the same as the "this" argument to
2518 * this method (which implies that we're in a constructor ourselves).
jeffhaobdb76512011-09-07 11:43:16 -07002519 */
Ian Rogers7b078e82014-09-10 14:44:24 -07002520 const RegType& this_type = work_line_->GetInvocationThis(this, inst, is_range);
jeffhaob57e9522012-04-26 18:08:21 -07002521 if (this_type.IsConflict()) // failure.
2522 break;
jeffhaobdb76512011-09-07 11:43:16 -07002523
jeffhaob57e9522012-04-26 18:08:21 -07002524 /* no null refs allowed (?) */
2525 if (this_type.IsZero()) {
2526 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unable to initialize null ref";
2527 break;
jeffhao2a8a90e2011-09-26 14:25:31 -07002528 }
jeffhaob57e9522012-04-26 18:08:21 -07002529
2530 /* must be in same class or in superclass */
Ian Rogersd8f69b02014-09-10 21:43:52 +00002531 // const RegType& this_super_klass = this_type.GetSuperClass(&reg_types_);
Ian Rogers46685432012-06-03 22:26:43 -07002532 // TODO: re-enable constructor type verification
2533 // if (this_super_klass.IsConflict()) {
jeffhaob57e9522012-04-26 18:08:21 -07002534 // Unknown super class, fail so we re-check at runtime.
Ian Rogers46685432012-06-03 22:26:43 -07002535 // Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "super class unknown for '" << this_type << "'";
2536 // break;
2537 // }
jeffhaob57e9522012-04-26 18:08:21 -07002538
2539 /* arg must be an uninitialized reference */
2540 if (!this_type.IsUninitializedTypes()) {
2541 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Expected initialization on uninitialized reference "
2542 << this_type;
2543 break;
2544 }
2545
2546 /*
2547 * Replace the uninitialized reference with an initialized one. We need to do this for all
2548 * registers that have the same object instance in them, not just the "this" register.
2549 */
Jeff Hao848f70a2014-01-15 13:49:50 -08002550 const uint32_t this_reg = (is_range) ? inst->VRegC_3rc() : inst->VRegC_35c();
2551 work_line_->MarkRefsAsInitialized(this, this_type, this_reg, work_insn_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002552 }
Ian Rogers1ff3c982014-08-12 02:30:58 -07002553 if (return_type == nullptr) {
Mathieu Chartierbf99f772014-08-23 16:37:27 -07002554 return_type = &reg_types_.FromDescriptor(GetClassLoader(), return_type_descriptor,
2555 false);
Ian Rogers1ff3c982014-08-12 02:30:58 -07002556 }
2557 if (!return_type->IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002558 work_line_->SetResultRegisterType(this, *return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002559 } else {
Ian Rogers1ff3c982014-08-12 02:30:58 -07002560 work_line_->SetResultRegisterTypeWide(*return_type, return_type->HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002561 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002562 just_set_result = true;
2563 break;
2564 }
2565 case Instruction::INVOKE_STATIC:
2566 case Instruction::INVOKE_STATIC_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002567 bool is_range = (inst->Opcode() == Instruction::INVOKE_STATIC_RANGE);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002568 ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_STATIC, is_range, false);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002569 const char* descriptor;
Ian Rogers7b078e82014-09-10 14:44:24 -07002570 if (called_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002571 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002572 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2573 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Mathieu Chartier590fee92013-09-13 13:46:47 -07002574 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002575 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002576 descriptor = called_method->GetReturnTypeDescriptor();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002577 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00002578 const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002579 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002580 work_line_->SetResultRegisterType(this, return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002581 } else {
2582 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2583 }
jeffhaobdb76512011-09-07 11:43:16 -07002584 just_set_result = true;
2585 }
2586 break;
jeffhaobdb76512011-09-07 11:43:16 -07002587 case Instruction::INVOKE_INTERFACE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002588 case Instruction::INVOKE_INTERFACE_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002589 bool is_range = (inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002590 ArtMethod* abs_method = VerifyInvocationArgs(inst, METHOD_INTERFACE, is_range, false);
Ian Rogers7b078e82014-09-10 14:44:24 -07002591 if (abs_method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002592 mirror::Class* called_interface = abs_method->GetDeclaringClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002593 if (!called_interface->IsInterface() && !called_interface->IsObjectClass()) {
2594 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected interface class in invoke-interface '"
2595 << PrettyMethod(abs_method) << "'";
2596 break;
Ian Rogers28ad40d2011-10-27 15:19:26 -07002597 }
Ian Rogers0d604842012-04-16 14:50:24 -07002598 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002599 /* Get the type of the "this" arg, which should either be a sub-interface of called
2600 * interface or Object (see comments in RegType::JoinClass).
2601 */
Ian Rogers7b078e82014-09-10 14:44:24 -07002602 const RegType& this_type = work_line_->GetInvocationThis(this, inst, is_range);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002603 if (this_type.IsZero()) {
2604 /* null pointer always passes (and always fails at runtime) */
2605 } else {
2606 if (this_type.IsUninitializedTypes()) {
2607 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface call on uninitialized object "
2608 << this_type;
2609 break;
2610 }
2611 // In the past we have tried to assert that "called_interface" is assignable
2612 // from "this_type.GetClass()", however, as we do an imprecise Join
2613 // (RegType::JoinClass) we don't have full information on what interfaces are
2614 // implemented by "this_type". For example, two classes may implement the same
2615 // interfaces and have a common parent that doesn't implement the interface. The
2616 // join will set "this_type" to the parent class and a test that this implements
2617 // the interface will incorrectly fail.
2618 }
2619 /*
2620 * We don't have an object instance, so we can't find the concrete method. However, all of
2621 * the type information is in the abstract method, so we're good.
2622 */
2623 const char* descriptor;
Ian Rogers7b078e82014-09-10 14:44:24 -07002624 if (abs_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002625 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002626 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2627 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
2628 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2629 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002630 descriptor = abs_method->GetReturnTypeDescriptor();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002631 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00002632 const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002633 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002634 work_line_->SetResultRegisterType(this, return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002635 } else {
2636 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2637 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002638 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002639 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002640 }
jeffhaobdb76512011-09-07 11:43:16 -07002641 case Instruction::NEG_INT:
2642 case Instruction::NOT_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002643 work_line_->CheckUnaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002644 break;
2645 case Instruction::NEG_LONG:
2646 case Instruction::NOT_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002647 work_line_->CheckUnaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002648 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002649 break;
2650 case Instruction::NEG_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002651 work_line_->CheckUnaryOp(this, inst, reg_types_.Float(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002652 break;
2653 case Instruction::NEG_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002654 work_line_->CheckUnaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002655 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002656 break;
2657 case Instruction::INT_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002658 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002659 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002660 break;
2661 case Instruction::INT_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002662 work_line_->CheckUnaryOp(this, inst, reg_types_.Float(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002663 break;
2664 case Instruction::INT_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002665 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002666 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002667 break;
2668 case Instruction::LONG_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002669 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002670 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002671 break;
2672 case Instruction::LONG_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002673 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002674 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002675 break;
2676 case Instruction::LONG_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002677 work_line_->CheckUnaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002678 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002679 break;
2680 case Instruction::FLOAT_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002681 work_line_->CheckUnaryOp(this, inst, reg_types_.Integer(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002682 break;
2683 case Instruction::FLOAT_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002684 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002685 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002686 break;
2687 case Instruction::FLOAT_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002688 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002689 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002690 break;
2691 case Instruction::DOUBLE_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002692 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002693 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002694 break;
2695 case Instruction::DOUBLE_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002696 work_line_->CheckUnaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002697 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002698 break;
2699 case Instruction::DOUBLE_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002700 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002701 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002702 break;
2703 case Instruction::INT_TO_BYTE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002704 work_line_->CheckUnaryOp(this, inst, reg_types_.Byte(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002705 break;
2706 case Instruction::INT_TO_CHAR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002707 work_line_->CheckUnaryOp(this, inst, reg_types_.Char(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002708 break;
2709 case Instruction::INT_TO_SHORT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002710 work_line_->CheckUnaryOp(this, inst, reg_types_.Short(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002711 break;
2712
2713 case Instruction::ADD_INT:
2714 case Instruction::SUB_INT:
2715 case Instruction::MUL_INT:
2716 case Instruction::REM_INT:
2717 case Instruction::DIV_INT:
2718 case Instruction::SHL_INT:
2719 case Instruction::SHR_INT:
2720 case Instruction::USHR_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002721 work_line_->CheckBinaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002722 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002723 break;
2724 case Instruction::AND_INT:
2725 case Instruction::OR_INT:
2726 case Instruction::XOR_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002727 work_line_->CheckBinaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002728 reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002729 break;
2730 case Instruction::ADD_LONG:
2731 case Instruction::SUB_LONG:
2732 case Instruction::MUL_LONG:
2733 case Instruction::DIV_LONG:
2734 case Instruction::REM_LONG:
2735 case Instruction::AND_LONG:
2736 case Instruction::OR_LONG:
2737 case Instruction::XOR_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002738 work_line_->CheckBinaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002739 reg_types_.LongLo(), reg_types_.LongHi(),
2740 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002741 break;
2742 case Instruction::SHL_LONG:
2743 case Instruction::SHR_LONG:
2744 case Instruction::USHR_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002745 /* shift distance is Int, making these different from other binary operations */
Ian Rogers7b078e82014-09-10 14:44:24 -07002746 work_line_->CheckBinaryOpWideShift(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002747 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002748 break;
2749 case Instruction::ADD_FLOAT:
2750 case Instruction::SUB_FLOAT:
2751 case Instruction::MUL_FLOAT:
2752 case Instruction::DIV_FLOAT:
2753 case Instruction::REM_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002754 work_line_->CheckBinaryOp(this, inst, reg_types_.Float(), reg_types_.Float(),
2755 reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002756 break;
2757 case Instruction::ADD_DOUBLE:
2758 case Instruction::SUB_DOUBLE:
2759 case Instruction::MUL_DOUBLE:
2760 case Instruction::DIV_DOUBLE:
2761 case Instruction::REM_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002762 work_line_->CheckBinaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002763 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2764 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002765 break;
2766 case Instruction::ADD_INT_2ADDR:
2767 case Instruction::SUB_INT_2ADDR:
2768 case Instruction::MUL_INT_2ADDR:
2769 case Instruction::REM_INT_2ADDR:
2770 case Instruction::SHL_INT_2ADDR:
2771 case Instruction::SHR_INT_2ADDR:
2772 case Instruction::USHR_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002773 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
2774 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002775 break;
2776 case Instruction::AND_INT_2ADDR:
2777 case Instruction::OR_INT_2ADDR:
2778 case Instruction::XOR_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002779 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
2780 reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002781 break;
2782 case Instruction::DIV_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002783 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
2784 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002785 break;
2786 case Instruction::ADD_LONG_2ADDR:
2787 case Instruction::SUB_LONG_2ADDR:
2788 case Instruction::MUL_LONG_2ADDR:
2789 case Instruction::DIV_LONG_2ADDR:
2790 case Instruction::REM_LONG_2ADDR:
2791 case Instruction::AND_LONG_2ADDR:
2792 case Instruction::OR_LONG_2ADDR:
2793 case Instruction::XOR_LONG_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002794 work_line_->CheckBinaryOp2addrWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002795 reg_types_.LongLo(), reg_types_.LongHi(),
2796 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002797 break;
2798 case Instruction::SHL_LONG_2ADDR:
2799 case Instruction::SHR_LONG_2ADDR:
2800 case Instruction::USHR_LONG_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002801 work_line_->CheckBinaryOp2addrWideShift(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002802 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002803 break;
2804 case Instruction::ADD_FLOAT_2ADDR:
2805 case Instruction::SUB_FLOAT_2ADDR:
2806 case Instruction::MUL_FLOAT_2ADDR:
2807 case Instruction::DIV_FLOAT_2ADDR:
2808 case Instruction::REM_FLOAT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002809 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Float(), reg_types_.Float(),
2810 reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002811 break;
2812 case Instruction::ADD_DOUBLE_2ADDR:
2813 case Instruction::SUB_DOUBLE_2ADDR:
2814 case Instruction::MUL_DOUBLE_2ADDR:
2815 case Instruction::DIV_DOUBLE_2ADDR:
2816 case Instruction::REM_DOUBLE_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002817 work_line_->CheckBinaryOp2addrWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002818 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2819 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002820 break;
2821 case Instruction::ADD_INT_LIT16:
Ian Rogersf72a11d2014-10-30 15:41:08 -07002822 case Instruction::RSUB_INT_LIT16:
jeffhaobdb76512011-09-07 11:43:16 -07002823 case Instruction::MUL_INT_LIT16:
2824 case Instruction::DIV_INT_LIT16:
2825 case Instruction::REM_INT_LIT16:
Ian Rogers7b078e82014-09-10 14:44:24 -07002826 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), false,
2827 true);
jeffhaobdb76512011-09-07 11:43:16 -07002828 break;
2829 case Instruction::AND_INT_LIT16:
2830 case Instruction::OR_INT_LIT16:
2831 case Instruction::XOR_INT_LIT16:
Ian Rogers7b078e82014-09-10 14:44:24 -07002832 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), true,
2833 true);
jeffhaobdb76512011-09-07 11:43:16 -07002834 break;
2835 case Instruction::ADD_INT_LIT8:
2836 case Instruction::RSUB_INT_LIT8:
2837 case Instruction::MUL_INT_LIT8:
2838 case Instruction::DIV_INT_LIT8:
2839 case Instruction::REM_INT_LIT8:
2840 case Instruction::SHL_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002841 case Instruction::SHR_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002842 case Instruction::USHR_INT_LIT8:
Ian Rogers7b078e82014-09-10 14:44:24 -07002843 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), false,
2844 false);
jeffhaobdb76512011-09-07 11:43:16 -07002845 break;
2846 case Instruction::AND_INT_LIT8:
2847 case Instruction::OR_INT_LIT8:
2848 case Instruction::XOR_INT_LIT8:
Ian Rogers7b078e82014-09-10 14:44:24 -07002849 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), true,
2850 false);
jeffhaobdb76512011-09-07 11:43:16 -07002851 break;
2852
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002853 // Special instructions.
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07002854 case Instruction::RETURN_VOID_NO_BARRIER:
2855 if (IsConstructor() && !IsStatic()) {
2856 auto& declaring_class = GetDeclaringClass();
Andreas Gampe68df3202015-06-22 11:35:46 -07002857 if (declaring_class.IsUnresolvedReference()) {
2858 // We must iterate over the fields, even if we cannot use mirror classes to do so. Do it
2859 // manually over the underlying dex file.
2860 uint32_t first_index = GetFirstFinalInstanceFieldIndex(*dex_file_,
2861 dex_file_->GetMethodId(dex_method_idx_).class_idx_);
2862 if (first_index != DexFile::kDexNoIndex) {
2863 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void-no-barrier not expected for field "
2864 << first_index;
2865 }
2866 break;
2867 }
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07002868 auto* klass = declaring_class.GetClass();
2869 for (uint32_t i = 0, num_fields = klass->NumInstanceFields(); i < num_fields; ++i) {
2870 if (klass->GetInstanceField(i)->IsFinal()) {
Mathieu Chartiere86deef2015-03-19 13:43:37 -07002871 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void-no-barrier not expected for "
2872 << PrettyField(klass->GetInstanceField(i));
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07002873 break;
2874 }
2875 }
Sebastien Hertzcc10e0e2013-06-28 14:24:48 +02002876 }
2877 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002878 // Note: the following instructions encode offsets derived from class linking.
2879 // As such they use Class*/Field*/AbstractMethod* as these offsets only have
2880 // meaning if the class linking and resolution were successful.
2881 case Instruction::IGET_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002882 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002883 break;
2884 case Instruction::IGET_WIDE_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002885 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002886 break;
2887 case Instruction::IGET_OBJECT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002888 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002889 break;
Mathieu Chartierffc605c2014-12-10 10:35:44 -08002890 case Instruction::IGET_BOOLEAN_QUICK:
2891 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true);
2892 break;
2893 case Instruction::IGET_BYTE_QUICK:
2894 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true);
2895 break;
2896 case Instruction::IGET_CHAR_QUICK:
2897 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true);
2898 break;
2899 case Instruction::IGET_SHORT_QUICK:
2900 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true);
2901 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002902 case Instruction::IPUT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002903 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002904 break;
Fred Shih37f05ef2014-07-16 18:38:08 -07002905 case Instruction::IPUT_BOOLEAN_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002906 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07002907 break;
2908 case Instruction::IPUT_BYTE_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002909 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07002910 break;
2911 case Instruction::IPUT_CHAR_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002912 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07002913 break;
2914 case Instruction::IPUT_SHORT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002915 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07002916 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002917 case Instruction::IPUT_WIDE_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002918 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002919 break;
2920 case Instruction::IPUT_OBJECT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002921 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002922 break;
2923 case Instruction::INVOKE_VIRTUAL_QUICK:
2924 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: {
2925 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002926 ArtMethod* called_method = VerifyInvokeVirtualQuickArgs(inst, is_range);
Ian Rogers7b078e82014-09-10 14:44:24 -07002927 if (called_method != nullptr) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002928 const char* descriptor = called_method->GetReturnTypeDescriptor();
Ian Rogersd8f69b02014-09-10 21:43:52 +00002929 const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002930 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002931 work_line_->SetResultRegisterType(this, return_type);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002932 } else {
2933 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2934 }
2935 just_set_result = true;
2936 }
2937 break;
2938 }
Igor Murashkin158f35c2015-06-10 15:55:30 -07002939 case Instruction::INVOKE_LAMBDA: {
2940 // Don't bother verifying, instead the interpreter will take the slow path with access checks.
2941 // If the code would've normally hard-failed, then the interpreter will throw the
2942 // appropriate verification errors at runtime.
2943 Fail(VERIFY_ERROR_FORCE_INTERPRETER); // TODO(iam): implement invoke-lambda verification
2944 break;
2945 }
2946 case Instruction::CREATE_LAMBDA: {
2947 // Don't bother verifying, instead the interpreter will take the slow path with access checks.
2948 // If the code would've normally hard-failed, then the interpreter will throw the
2949 // appropriate verification errors at runtime.
2950 Fail(VERIFY_ERROR_FORCE_INTERPRETER); // TODO(iam): implement create-lambda verification
2951 break;
2952 }
2953
Igor Murashkin2ee54e22015-06-18 10:05:11 -07002954 case Instruction::UNUSED_F4:
2955 case Instruction::UNUSED_F5:
2956 case Instruction::UNUSED_F7: {
Igor Murashkin158f35c2015-06-10 15:55:30 -07002957 DCHECK(false); // TODO(iam): Implement opcodes for lambdas
Igor Murashkin2ee54e22015-06-18 10:05:11 -07002958 // Conservatively fail verification on release builds.
2959 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Unexpected opcode " << inst->DumpString(dex_file_);
2960 break;
2961 }
2962
2963 case Instruction::BOX_LAMBDA: {
2964 // Don't bother verifying, instead the interpreter will take the slow path with access checks.
2965 // If the code would've normally hard-failed, then the interpreter will throw the
2966 // appropriate verification errors at runtime.
2967 Fail(VERIFY_ERROR_FORCE_INTERPRETER); // TODO(iam): implement box-lambda verification
Igor Murashkine2facc52015-07-10 13:49:08 -07002968
2969 // Partial verification. Sets the resulting type to always be an object, which
2970 // is good enough for some other verification to occur without hard-failing.
2971 const uint32_t vreg_target_object = inst->VRegA_22x(); // box-lambda vA, vB
2972 const RegType& reg_type = reg_types_.JavaLangObject(need_precise_constants_);
2973 work_line_->SetRegisterType(this, vreg_target_object, reg_type);
Igor Murashkin2ee54e22015-06-18 10:05:11 -07002974 break;
2975 }
2976
2977 case Instruction::UNBOX_LAMBDA: {
2978 // Don't bother verifying, instead the interpreter will take the slow path with access checks.
2979 // If the code would've normally hard-failed, then the interpreter will throw the
2980 // appropriate verification errors at runtime.
2981 Fail(VERIFY_ERROR_FORCE_INTERPRETER); // TODO(iam): implement unbox-lambda verification
2982 break;
Igor Murashkin158f35c2015-06-10 15:55:30 -07002983 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002984
Ian Rogersd81871c2011-10-03 13:57:23 -07002985 /* These should never appear during verification. */
Mathieu Chartierffc605c2014-12-10 10:35:44 -08002986 case Instruction::UNUSED_3E ... Instruction::UNUSED_43:
Igor Murashkin158f35c2015-06-10 15:55:30 -07002987 case Instruction::UNUSED_FA ... Instruction::UNUSED_FF:
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002988 case Instruction::UNUSED_79:
2989 case Instruction::UNUSED_7A:
jeffhaod5347e02012-03-22 17:25:05 -07002990 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Unexpected opcode " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07002991 break;
2992
2993 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002994 * DO NOT add a "default" clause here. Without it the compiler will
jeffhaobdb76512011-09-07 11:43:16 -07002995 * complain if an instruction is missing (which is desirable).
2996 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002997 } // end - switch (dec_insn.opcode)
jeffhaobdb76512011-09-07 11:43:16 -07002998
Stephen Kyle7e541c92014-12-17 17:10:02 +00002999 /*
3000 * If we are in a constructor, and we had an UninitializedThis type
3001 * in a register somewhere, we need to make sure it wasn't overwritten.
3002 */
3003 if (track_uninitialized_this) {
3004 bool was_invoke_direct = (inst->Opcode() == Instruction::INVOKE_DIRECT ||
3005 inst->Opcode() == Instruction::INVOKE_DIRECT_RANGE);
3006 if (work_line_->WasUninitializedThisOverwritten(this, uninitialized_this_loc,
3007 was_invoke_direct)) {
3008 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
3009 << "Constructor failed to initialize this object";
3010 }
3011 }
3012
Ian Rogersad0b3a32012-04-16 14:50:24 -07003013 if (have_pending_hard_failure_) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08003014 if (Runtime::Current()->IsAotCompiler()) {
3015 /* When AOT compiling, check that the last failure is a hard failure */
Andreas Gampeb588f4c2015-05-26 13:35:39 -07003016 if (failures_[failures_.size() - 1] != VERIFY_ERROR_BAD_CLASS_HARD) {
3017 LOG(ERROR) << "Pending failures:";
3018 for (auto& error : failures_) {
3019 LOG(ERROR) << error;
3020 }
3021 for (auto& error_msg : failure_messages_) {
3022 LOG(ERROR) << error_msg->str();
3023 }
3024 LOG(FATAL) << "Pending hard failure, but last failure not hard.";
3025 }
Ian Rogerse1758fe2012-04-19 11:31:15 -07003026 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003027 /* immediate failure, reject class */
3028 info_messages_ << "Rejecting opcode " << inst->DumpString(dex_file_);
3029 return false;
jeffhaofaf459e2012-08-31 15:32:47 -07003030 } else if (have_pending_runtime_throw_failure_) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07003031 /* checking interpreter will throw, mark following code as unreachable */
jeffhaofaf459e2012-08-31 15:32:47 -07003032 opcode_flags = Instruction::kThrow;
Andreas Gamped12e7822015-06-25 10:26:40 -07003033 have_any_pending_runtime_throw_failure_ = true;
3034 // Reset the pending_runtime_throw flag. The flag is a global to decouple Fail and is per
3035 // instruction.
3036 have_pending_runtime_throw_failure_ = false;
jeffhaobdb76512011-09-07 11:43:16 -07003037 }
jeffhaobdb76512011-09-07 11:43:16 -07003038 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003039 * If we didn't just set the result register, clear it out. This ensures that you can only use
3040 * "move-result" immediately after the result is set. (We could check this statically, but it's
3041 * not expensive and it makes our debugging output cleaner.)
jeffhaobdb76512011-09-07 11:43:16 -07003042 */
3043 if (!just_set_result) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003044 work_line_->SetResultTypeToUnknown(this);
jeffhaobdb76512011-09-07 11:43:16 -07003045 }
3046
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003047
jeffhaobdb76512011-09-07 11:43:16 -07003048
3049 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003050 * Handle "branch". Tag the branch target.
jeffhaobdb76512011-09-07 11:43:16 -07003051 *
3052 * NOTE: instructions like Instruction::EQZ provide information about the
jeffhaod1f0fde2011-09-08 17:25:33 -07003053 * state of the register when the branch is taken or not taken. For example,
jeffhaobdb76512011-09-07 11:43:16 -07003054 * somebody could get a reference field, check it for zero, and if the
3055 * branch is taken immediately store that register in a boolean field
jeffhaod1f0fde2011-09-08 17:25:33 -07003056 * since the value is known to be zero. We do not currently account for
jeffhaobdb76512011-09-07 11:43:16 -07003057 * that, and will reject the code.
3058 *
3059 * TODO: avoid re-fetching the branch target
3060 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003061 if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07003062 bool isConditional, selfOkay;
Ian Rogersd81871c2011-10-03 13:57:23 -07003063 if (!GetBranchOffset(work_insn_idx_, &branch_target, &isConditional, &selfOkay)) {
jeffhaobdb76512011-09-07 11:43:16 -07003064 /* should never happen after static verification */
jeffhaod5347e02012-03-22 17:25:05 -07003065 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad branch";
jeffhaobdb76512011-09-07 11:43:16 -07003066 return false;
3067 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08003068 DCHECK_EQ(isConditional, (opcode_flags & Instruction::kContinue) != 0);
Stephen Kyle9bc61992014-09-22 13:53:15 +01003069 if (!CheckNotMoveExceptionOrMoveResult(code_item_->insns_, work_insn_idx_ + branch_target)) {
jeffhaobdb76512011-09-07 11:43:16 -07003070 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07003071 }
jeffhaobdb76512011-09-07 11:43:16 -07003072 /* update branch target, set "changed" if appropriate */
Ian Rogers7b078e82014-09-10 14:44:24 -07003073 if (nullptr != branch_line.get()) {
Ian Rogersebbdd872014-07-07 23:53:08 -07003074 if (!UpdateRegisters(work_insn_idx_ + branch_target, branch_line.get(), false)) {
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003075 return false;
3076 }
3077 } else {
Ian Rogersebbdd872014-07-07 23:53:08 -07003078 if (!UpdateRegisters(work_insn_idx_ + branch_target, work_line_.get(), false)) {
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003079 return false;
3080 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003081 }
jeffhaobdb76512011-09-07 11:43:16 -07003082 }
3083
3084 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003085 * Handle "switch". Tag all possible branch targets.
jeffhaobdb76512011-09-07 11:43:16 -07003086 *
3087 * We've already verified that the table is structurally sound, so we
3088 * just need to walk through and tag the targets.
3089 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003090 if ((opcode_flags & Instruction::kSwitch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07003091 int offset_to_switch = insns[1] | (((int32_t) insns[2]) << 16);
3092 const uint16_t* switch_insns = insns + offset_to_switch;
3093 int switch_count = switch_insns[1];
3094 int offset_to_targets, targ;
3095
3096 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
3097 /* 0 = sig, 1 = count, 2/3 = first key */
3098 offset_to_targets = 4;
3099 } else {
3100 /* 0 = sig, 1 = count, 2..count * 2 = keys */
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07003101 DCHECK((*insns & 0xff) == Instruction::SPARSE_SWITCH);
jeffhaobdb76512011-09-07 11:43:16 -07003102 offset_to_targets = 2 + 2 * switch_count;
3103 }
3104
3105 /* verify each switch target */
3106 for (targ = 0; targ < switch_count; targ++) {
3107 int offset;
3108 uint32_t abs_offset;
3109
3110 /* offsets are 32-bit, and only partly endian-swapped */
3111 offset = switch_insns[offset_to_targets + targ * 2] |
3112 (((int32_t) switch_insns[offset_to_targets + targ * 2 + 1]) << 16);
Ian Rogersd81871c2011-10-03 13:57:23 -07003113 abs_offset = work_insn_idx_ + offset;
3114 DCHECK_LT(abs_offset, code_item_->insns_size_in_code_units_);
Stephen Kyle9bc61992014-09-22 13:53:15 +01003115 if (!CheckNotMoveExceptionOrMoveResult(code_item_->insns_, abs_offset)) {
jeffhaobdb76512011-09-07 11:43:16 -07003116 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07003117 }
Ian Rogersebbdd872014-07-07 23:53:08 -07003118 if (!UpdateRegisters(abs_offset, work_line_.get(), false)) {
jeffhaobdb76512011-09-07 11:43:16 -07003119 return false;
Ian Rogersebbdd872014-07-07 23:53:08 -07003120 }
jeffhaobdb76512011-09-07 11:43:16 -07003121 }
3122 }
3123
3124 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003125 * Handle instructions that can throw and that are sitting in a "try" block. (If they're not in a
3126 * "try" block when they throw, control transfers out of the method.)
jeffhaobdb76512011-09-07 11:43:16 -07003127 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003128 if ((opcode_flags & Instruction::kThrow) != 0 && insn_flags_[work_insn_idx_].IsInTry()) {
Andreas Gampef91baf12014-07-18 15:41:00 -07003129 bool has_catch_all_handler = false;
Ian Rogers0571d352011-11-03 19:51:38 -07003130 CatchHandlerIterator iterator(*code_item_, work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07003131
Andreas Gampef91baf12014-07-18 15:41:00 -07003132 // Need the linker to try and resolve the handled class to check if it's Throwable.
3133 ClassLinker* linker = Runtime::Current()->GetClassLinker();
3134
Ian Rogers0571d352011-11-03 19:51:38 -07003135 for (; iterator.HasNext(); iterator.Next()) {
Andreas Gampef91baf12014-07-18 15:41:00 -07003136 uint16_t handler_type_idx = iterator.GetHandlerTypeIndex();
3137 if (handler_type_idx == DexFile::kDexNoIndex16) {
3138 has_catch_all_handler = true;
3139 } else {
3140 // It is also a catch-all if it is java.lang.Throwable.
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003141 mirror::Class* klass = linker->ResolveType(*dex_file_, handler_type_idx, dex_cache_,
3142 class_loader_);
Andreas Gampef91baf12014-07-18 15:41:00 -07003143 if (klass != nullptr) {
3144 if (klass == mirror::Throwable::GetJavaLangThrowable()) {
3145 has_catch_all_handler = true;
3146 }
3147 } else {
3148 // Clear exception.
Ian Rogers7b078e82014-09-10 14:44:24 -07003149 DCHECK(self_->IsExceptionPending());
3150 self_->ClearException();
Andreas Gampef91baf12014-07-18 15:41:00 -07003151 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003152 }
jeffhaobdb76512011-09-07 11:43:16 -07003153 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003154 * Merge registers into the "catch" block. We want to use the "savedRegs" rather than
3155 * "work_regs", because at runtime the exception will be thrown before the instruction
3156 * modifies any registers.
jeffhaobdb76512011-09-07 11:43:16 -07003157 */
Ian Rogersebbdd872014-07-07 23:53:08 -07003158 if (!UpdateRegisters(iterator.GetHandlerAddress(), saved_line_.get(), false)) {
jeffhaobdb76512011-09-07 11:43:16 -07003159 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07003160 }
jeffhaobdb76512011-09-07 11:43:16 -07003161 }
3162
3163 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003164 * If the monitor stack depth is nonzero, there must be a "catch all" handler for this
3165 * instruction. This does apply to monitor-exit because of async exception handling.
jeffhaobdb76512011-09-07 11:43:16 -07003166 */
Andreas Gampef91baf12014-07-18 15:41:00 -07003167 if (work_line_->MonitorStackDepth() > 0 && !has_catch_all_handler) {
jeffhaobdb76512011-09-07 11:43:16 -07003168 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003169 * The state in work_line reflects the post-execution state. If the current instruction is a
3170 * monitor-enter and the monitor stack was empty, we don't need a catch-all (if it throws,
jeffhaobdb76512011-09-07 11:43:16 -07003171 * it will do so before grabbing the lock).
3172 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02003173 if (inst->Opcode() != Instruction::MONITOR_ENTER || work_line_->MonitorStackDepth() != 1) {
jeffhaod5347e02012-03-22 17:25:05 -07003174 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
Ian Rogersd81871c2011-10-03 13:57:23 -07003175 << "expected to be within a catch-all for an instruction where a monitor is held";
jeffhaobdb76512011-09-07 11:43:16 -07003176 return false;
3177 }
3178 }
3179 }
3180
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003181 /* Handle "continue". Tag the next consecutive instruction.
3182 * Note: Keep the code handling "continue" case below the "branch" and "switch" cases,
3183 * because it changes work_line_ when performing peephole optimization
3184 * and this change should not be used in those cases.
3185 */
Ian Rogers6d376ae2013-07-23 15:12:40 -07003186 if ((opcode_flags & Instruction::kContinue) != 0) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003187 DCHECK_EQ(Instruction::At(code_item_->insns_ + work_insn_idx_), inst);
3188 uint32_t next_insn_idx = work_insn_idx_ + inst->SizeInCodeUnits();
Ian Rogers6d376ae2013-07-23 15:12:40 -07003189 if (next_insn_idx >= code_item_->insns_size_in_code_units_) {
3190 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Execution can walk off end of code area";
3191 return false;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003192 }
Ian Rogers6d376ae2013-07-23 15:12:40 -07003193 // The only way to get to a move-exception instruction is to get thrown there. Make sure the
3194 // next instruction isn't one.
3195 if (!CheckNotMoveException(code_item_->insns_, next_insn_idx)) {
3196 return false;
3197 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003198 if (nullptr != fallthrough_line.get()) {
Ian Rogers6d376ae2013-07-23 15:12:40 -07003199 // Make workline consistent with fallthrough computed from peephole optimization.
3200 work_line_->CopyFromLine(fallthrough_line.get());
3201 }
Ian Rogersb8c78592013-07-25 23:52:52 +00003202 if (insn_flags_[next_insn_idx].IsReturn()) {
3203 // For returns we only care about the operand to the return, all other registers are dead.
3204 const Instruction* ret_inst = Instruction::At(code_item_->insns_ + next_insn_idx);
3205 Instruction::Code opcode = ret_inst->Opcode();
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07003206 if (opcode == Instruction::RETURN_VOID || opcode == Instruction::RETURN_VOID_NO_BARRIER) {
Stephen Kyle7e541c92014-12-17 17:10:02 +00003207 SafelyMarkAllRegistersAsConflicts(this, work_line_.get());
Ian Rogersb8c78592013-07-25 23:52:52 +00003208 } else {
3209 if (opcode == Instruction::RETURN_WIDE) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003210 work_line_->MarkAllRegistersAsConflictsExceptWide(this, ret_inst->VRegA_11x());
Ian Rogersb8c78592013-07-25 23:52:52 +00003211 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07003212 work_line_->MarkAllRegistersAsConflictsExcept(this, ret_inst->VRegA_11x());
Ian Rogersb8c78592013-07-25 23:52:52 +00003213 }
3214 }
3215 }
Ian Rogers6d376ae2013-07-23 15:12:40 -07003216 RegisterLine* next_line = reg_table_.GetLine(next_insn_idx);
Ian Rogers7b078e82014-09-10 14:44:24 -07003217 if (next_line != nullptr) {
Ian Rogersebbdd872014-07-07 23:53:08 -07003218 // Merge registers into what we have for the next instruction, and set the "changed" flag if
3219 // needed. If the merge changes the state of the registers then the work line will be
3220 // updated.
3221 if (!UpdateRegisters(next_insn_idx, work_line_.get(), true)) {
Ian Rogers6d376ae2013-07-23 15:12:40 -07003222 return false;
3223 }
3224 } else {
3225 /*
3226 * We're not recording register data for the next instruction, so we don't know what the
3227 * prior state was. We have to assume that something has changed and re-evaluate it.
3228 */
3229 insn_flags_[next_insn_idx].SetChanged();
3230 }
3231 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003232
jeffhaod1f0fde2011-09-08 17:25:33 -07003233 /* If we're returning from the method, make sure monitor stack is empty. */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003234 if ((opcode_flags & Instruction::kReturn) != 0) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003235 if (!work_line_->VerifyMonitorStackEmpty(this)) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003236 return false;
3237 }
jeffhaobdb76512011-09-07 11:43:16 -07003238 }
3239
3240 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003241 * Update start_guess. Advance to the next instruction of that's
3242 * possible, otherwise use the branch target if one was found. If
jeffhaobdb76512011-09-07 11:43:16 -07003243 * neither of those exists we're in a return or throw; leave start_guess
3244 * alone and let the caller sort it out.
3245 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003246 if ((opcode_flags & Instruction::kContinue) != 0) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003247 DCHECK_EQ(Instruction::At(code_item_->insns_ + work_insn_idx_), inst);
3248 *start_guess = work_insn_idx_ + inst->SizeInCodeUnits();
Elliott Hughesadb8c672012-03-06 16:49:32 -08003249 } else if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07003250 /* we're still okay if branch_target is zero */
Ian Rogersd81871c2011-10-03 13:57:23 -07003251 *start_guess = work_insn_idx_ + branch_target;
jeffhaobdb76512011-09-07 11:43:16 -07003252 }
3253
Ian Rogersd81871c2011-10-03 13:57:23 -07003254 DCHECK_LT(*start_guess, code_item_->insns_size_in_code_units_);
3255 DCHECK(insn_flags_[*start_guess].IsOpcode());
jeffhaobdb76512011-09-07 11:43:16 -07003256
3257 return true;
Brian Carlstrom7934ac22013-07-26 10:54:15 -07003258} // NOLINT(readability/fn_size)
jeffhaobdb76512011-09-07 11:43:16 -07003259
Ian Rogersd8f69b02014-09-10 21:43:52 +00003260const RegType& MethodVerifier::ResolveClassAndCheckAccess(uint32_t class_idx) {
Ian Rogers0571d352011-11-03 19:51:38 -07003261 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
Ian Rogersd8f69b02014-09-10 21:43:52 +00003262 const RegType& referrer = GetDeclaringClass();
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003263 mirror::Class* klass = dex_cache_->GetResolvedType(class_idx);
Ian Rogers7b078e82014-09-10 14:44:24 -07003264 const RegType& result = klass != nullptr ?
Andreas Gampef23f33d2015-06-23 14:18:17 -07003265 FromClass(descriptor, klass, klass->CannotBeAssignedFromOtherTypes()) :
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003266 reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003267 if (result.IsConflict()) {
3268 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "accessing broken descriptor '" << descriptor
3269 << "' in " << referrer;
3270 return result;
3271 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003272 if (klass == nullptr && !result.IsUnresolvedTypes()) {
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003273 dex_cache_->SetResolvedType(class_idx, result.GetClass());
Ian Rogerse1758fe2012-04-19 11:31:15 -07003274 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003275 // Check if access is allowed. Unresolved types use xxxWithAccessCheck to
Jeff Haob24b4a72013-07-31 13:47:31 -07003276 // check at runtime if access is allowed and so pass here. If result is
3277 // primitive, skip the access check.
3278 if (result.IsNonZeroReferenceTypes() && !result.IsUnresolvedTypes() &&
3279 !referrer.IsUnresolvedTypes() && !referrer.CanAccess(result)) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07003280 Fail(VERIFY_ERROR_ACCESS_CLASS) << "illegal class access: '"
Ian Rogersad0b3a32012-04-16 14:50:24 -07003281 << referrer << "' -> '" << result << "'";
Ian Rogers28ad40d2011-10-27 15:19:26 -07003282 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003283 return result;
Ian Rogersd81871c2011-10-03 13:57:23 -07003284}
3285
Ian Rogersd8f69b02014-09-10 21:43:52 +00003286const RegType& MethodVerifier::GetCaughtExceptionType() {
Ian Rogers7b078e82014-09-10 14:44:24 -07003287 const RegType* common_super = nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003288 if (code_item_->tries_size_ != 0) {
Ian Rogers13735952014-10-08 12:43:28 -07003289 const uint8_t* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
Ian Rogersd81871c2011-10-03 13:57:23 -07003290 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
3291 for (uint32_t i = 0; i < handlers_size; i++) {
Ian Rogers0571d352011-11-03 19:51:38 -07003292 CatchHandlerIterator iterator(handlers_ptr);
3293 for (; iterator.HasNext(); iterator.Next()) {
3294 if (iterator.GetHandlerAddress() == (uint32_t) work_insn_idx_) {
3295 if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) {
Ian Rogersb4903572012-10-11 11:52:56 -07003296 common_super = &reg_types_.JavaLangThrowable(false);
Ian Rogersd81871c2011-10-03 13:57:23 -07003297 } else {
Ian Rogersd8f69b02014-09-10 21:43:52 +00003298 const RegType& exception = ResolveClassAndCheckAccess(iterator.GetHandlerTypeIndex());
Jeff Haob878f212014-04-24 16:25:36 -07003299 if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(exception)) {
Jeff Haoc26a56c2013-11-04 12:00:47 -08003300 if (exception.IsUnresolvedTypes()) {
3301 // We don't know enough about the type. Fail here and let runtime handle it.
3302 Fail(VERIFY_ERROR_NO_CLASS) << "unresolved exception class " << exception;
3303 return exception;
3304 } else {
3305 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unexpected non-exception class " << exception;
3306 return reg_types_.Conflict();
3307 }
Jeff Haob878f212014-04-24 16:25:36 -07003308 } else if (common_super == nullptr) {
3309 common_super = &exception;
Ian Rogers28ad40d2011-10-27 15:19:26 -07003310 } else if (common_super->Equals(exception)) {
Ian Rogersc4762272012-02-01 15:55:55 -08003311 // odd case, but nothing to do
Ian Rogersd81871c2011-10-03 13:57:23 -07003312 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07003313 common_super = &common_super->Merge(exception, &reg_types_);
Andreas Gampe7c038102014-10-27 20:08:46 -07003314 if (FailOrAbort(this,
3315 reg_types_.JavaLangThrowable(false).IsAssignableFrom(*common_super),
3316 "java.lang.Throwable is not assignable-from common_super at ",
3317 work_insn_idx_)) {
3318 break;
3319 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003320 }
3321 }
3322 }
3323 }
Ian Rogers0571d352011-11-03 19:51:38 -07003324 handlers_ptr = iterator.EndDataPointer();
Ian Rogersd81871c2011-10-03 13:57:23 -07003325 }
3326 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003327 if (common_super == nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003328 /* no catch blocks, or no catches with classes we can find */
jeffhaod5347e02012-03-22 17:25:05 -07003329 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unable to find exception handler";
Ian Rogersad0b3a32012-04-16 14:50:24 -07003330 return reg_types_.Conflict();
Ian Rogersd81871c2011-10-03 13:57:23 -07003331 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07003332 return *common_super;
Ian Rogersd81871c2011-10-03 13:57:23 -07003333}
3334
Mathieu Chartiere401d142015-04-22 13:56:20 -07003335ArtMethod* MethodVerifier::ResolveMethodAndCheckAccess(
3336 uint32_t dex_method_idx, MethodType method_type) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003337 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx);
Ian Rogersd8f69b02014-09-10 21:43:52 +00003338 const RegType& klass_type = ResolveClassAndCheckAccess(method_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003339 if (klass_type.IsConflict()) {
3340 std::string append(" in attempt to access method ");
3341 append += dex_file_->GetMethodName(method_id);
3342 AppendToLastFailMessage(append);
Ian Rogers7b078e82014-09-10 14:44:24 -07003343 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08003344 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003345 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003346 return nullptr; // Can't resolve Class so no more to do here
Ian Rogers90040192011-12-16 08:54:29 -08003347 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003348 mirror::Class* klass = klass_type.GetClass();
Ian Rogersd8f69b02014-09-10 21:43:52 +00003349 const RegType& referrer = GetDeclaringClass();
Mathieu Chartiere401d142015-04-22 13:56:20 -07003350 auto* cl = Runtime::Current()->GetClassLinker();
3351 auto pointer_size = cl->GetImagePointerSize();
3352 ArtMethod* res_method = dex_cache_->GetResolvedMethod(dex_method_idx, pointer_size);
Ian Rogers7b078e82014-09-10 14:44:24 -07003353 if (res_method == nullptr) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07003354 const char* name = dex_file_->GetMethodName(method_id);
Ian Rogersd91d6d62013-09-25 20:26:14 -07003355 const Signature signature = dex_file_->GetMethodSignature(method_id);
jeffhao8cd6dda2012-02-22 10:15:34 -08003356
3357 if (method_type == METHOD_DIRECT || method_type == METHOD_STATIC) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003358 res_method = klass->FindDirectMethod(name, signature, pointer_size);
jeffhao8cd6dda2012-02-22 10:15:34 -08003359 } else if (method_type == METHOD_INTERFACE) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003360 res_method = klass->FindInterfaceMethod(name, signature, pointer_size);
Ian Rogersd81871c2011-10-03 13:57:23 -07003361 } else {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003362 res_method = klass->FindVirtualMethod(name, signature, pointer_size);
Ian Rogersd81871c2011-10-03 13:57:23 -07003363 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003364 if (res_method != nullptr) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003365 dex_cache_->SetResolvedMethod(dex_method_idx, res_method, pointer_size);
Ian Rogersd81871c2011-10-03 13:57:23 -07003366 } else {
jeffhao8cd6dda2012-02-22 10:15:34 -08003367 // If a virtual or interface method wasn't found with the expected type, look in
3368 // the direct methods. This can happen when the wrong invoke type is used or when
3369 // a class has changed, and will be flagged as an error in later checks.
3370 if (method_type == METHOD_INTERFACE || method_type == METHOD_VIRTUAL) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003371 res_method = klass->FindDirectMethod(name, signature, pointer_size);
jeffhao8cd6dda2012-02-22 10:15:34 -08003372 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003373 if (res_method == nullptr) {
jeffhao8cd6dda2012-02-22 10:15:34 -08003374 Fail(VERIFY_ERROR_NO_METHOD) << "couldn't find method "
3375 << PrettyDescriptor(klass) << "." << name
3376 << " " << signature;
Ian Rogers7b078e82014-09-10 14:44:24 -07003377 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003378 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003379 }
3380 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003381 // Make sure calls to constructors are "direct". There are additional restrictions but we don't
3382 // enforce them here.
3383 if (res_method->IsConstructor() && method_type != METHOD_DIRECT) {
jeffhaod5347e02012-03-22 17:25:05 -07003384 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting non-direct call to constructor "
3385 << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003386 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003387 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003388 // Disallow any calls to class initializers.
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003389 if (res_method->IsClassInitializer()) {
jeffhaod5347e02012-03-22 17:25:05 -07003390 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting call to class initializer "
3391 << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003392 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003393 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003394 // Check if access is allowed.
Ian Rogersad0b3a32012-04-16 14:50:24 -07003395 if (!referrer.CanAccessMember(res_method->GetDeclaringClass(), res_method->GetAccessFlags())) {
jeffhao8cd6dda2012-02-22 10:15:34 -08003396 Fail(VERIFY_ERROR_ACCESS_METHOD) << "illegal method access (call " << PrettyMethod(res_method)
Ian Rogersad0b3a32012-04-16 14:50:24 -07003397 << " from " << referrer << ")";
jeffhaob57e9522012-04-26 18:08:21 -07003398 return res_method;
jeffhao8cd6dda2012-02-22 10:15:34 -08003399 }
jeffhaode0d9c92012-02-27 13:58:13 -08003400 // Check that invoke-virtual and invoke-super are not used on private methods of the same class.
3401 if (res_method->IsPrivate() && method_type == METHOD_VIRTUAL) {
jeffhaod5347e02012-03-22 17:25:05 -07003402 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invoke-super/virtual can't be used on private method "
3403 << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003404 return nullptr;
jeffhaode0d9c92012-02-27 13:58:13 -08003405 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003406 // Check that interface methods match interface classes.
3407 if (klass->IsInterface() && method_type != METHOD_INTERFACE) {
3408 Fail(VERIFY_ERROR_CLASS_CHANGE) << "non-interface method " << PrettyMethod(res_method)
3409 << " is in an interface class " << PrettyClass(klass);
Ian Rogers7b078e82014-09-10 14:44:24 -07003410 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003411 } else if (!klass->IsInterface() && method_type == METHOD_INTERFACE) {
3412 Fail(VERIFY_ERROR_CLASS_CHANGE) << "interface method " << PrettyMethod(res_method)
3413 << " is in a non-interface class " << PrettyClass(klass);
Ian Rogers7b078e82014-09-10 14:44:24 -07003414 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003415 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003416 // See if the method type implied by the invoke instruction matches the access flags for the
3417 // target method.
Brian Carlstrombe6fa5e2014-12-09 20:15:42 -08003418 if ((method_type == METHOD_DIRECT && (!res_method->IsDirect() || res_method->IsStatic())) ||
Ian Rogersd81871c2011-10-03 13:57:23 -07003419 (method_type == METHOD_STATIC && !res_method->IsStatic()) ||
3420 ((method_type == METHOD_VIRTUAL || method_type == METHOD_INTERFACE) && res_method->IsDirect())
3421 ) {
Ian Rogers2fc14272012-08-30 10:56:57 -07003422 Fail(VERIFY_ERROR_CLASS_CHANGE) << "invoke type (" << method_type << ") does not match method "
3423 " type of " << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003424 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003425 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003426 return res_method;
3427}
3428
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003429template <class T>
Mathieu Chartiere401d142015-04-22 13:56:20 -07003430ArtMethod* MethodVerifier::VerifyInvocationArgsFromIterator(
3431 T* it, const Instruction* inst, MethodType method_type, bool is_range, ArtMethod* res_method) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003432 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
3433 // match the call to the signature. Also, we might be calling through an abstract method
3434 // definition (which doesn't have register count values).
3435 const size_t expected_args = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
3436 /* caught by static verifier */
3437 DCHECK(is_range || expected_args <= 5);
3438 if (expected_args > code_item_->outs_size_) {
3439 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
3440 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
3441 return nullptr;
3442 }
3443
3444 uint32_t arg[5];
3445 if (!is_range) {
3446 inst->GetVarArgs(arg);
3447 }
3448 uint32_t sig_registers = 0;
3449
3450 /*
3451 * Check the "this" argument, which must be an instance of the class that declared the method.
3452 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
3453 * rigorous check here (which is okay since we have to do it at runtime).
3454 */
3455 if (method_type != METHOD_STATIC) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003456 const RegType& actual_arg_type = work_line_->GetInvocationThis(this, inst, is_range);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003457 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
3458 CHECK(have_pending_hard_failure_);
3459 return nullptr;
3460 }
3461 if (actual_arg_type.IsUninitializedReference()) {
3462 if (res_method) {
3463 if (!res_method->IsConstructor()) {
3464 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
3465 return nullptr;
3466 }
3467 } else {
3468 // Check whether the name of the called method is "<init>"
3469 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Jeff Hao0d087272014-08-04 14:47:17 -07003470 if (strcmp(dex_file_->GetMethodName(dex_file_->GetMethodId(method_idx)), "<init>") != 0) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003471 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
3472 return nullptr;
3473 }
3474 }
3475 }
3476 if (method_type != METHOD_INTERFACE && !actual_arg_type.IsZero()) {
Ian Rogersd8f69b02014-09-10 21:43:52 +00003477 const RegType* res_method_class;
Andreas Gamped9e23012015-06-04 22:19:58 -07003478 // Miranda methods have the declaring interface as their declaring class, not the abstract
3479 // class. It would be wrong to use this for the type check (interface type checks are
3480 // postponed to runtime).
3481 if (res_method != nullptr && !res_method->IsMiranda()) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003482 mirror::Class* klass = res_method->GetDeclaringClass();
Ian Rogers1ff3c982014-08-12 02:30:58 -07003483 std::string temp;
Andreas Gampef23f33d2015-06-23 14:18:17 -07003484 res_method_class = &FromClass(klass->GetDescriptor(&temp), klass,
3485 klass->CannotBeAssignedFromOtherTypes());
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003486 } else {
3487 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
3488 const uint16_t class_idx = dex_file_->GetMethodId(method_idx).class_idx_;
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003489 res_method_class = &reg_types_.FromDescriptor(GetClassLoader(),
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003490 dex_file_->StringByTypeIdx(class_idx),
3491 false);
3492 }
3493 if (!res_method_class->IsAssignableFrom(actual_arg_type)) {
3494 Fail(actual_arg_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS:
3495 VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type
3496 << "' not instance of '" << *res_method_class << "'";
3497 // Continue on soft failures. We need to find possible hard failures to avoid problems in
3498 // the compiler.
3499 if (have_pending_hard_failure_) {
3500 return nullptr;
3501 }
3502 }
3503 }
3504 sig_registers = 1;
3505 }
3506
3507 for ( ; it->HasNext(); it->Next()) {
3508 if (sig_registers >= expected_args) {
3509 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, expected " << inst->VRegA() <<
3510 " arguments, found " << sig_registers << " or more.";
3511 return nullptr;
3512 }
3513
3514 const char* param_descriptor = it->GetDescriptor();
3515
3516 if (param_descriptor == nullptr) {
3517 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation because of missing signature "
3518 "component";
3519 return nullptr;
3520 }
3521
Ian Rogersd8f69b02014-09-10 21:43:52 +00003522 const RegType& reg_type = reg_types_.FromDescriptor(GetClassLoader(), param_descriptor, false);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003523 uint32_t get_reg = is_range ? inst->VRegC_3rc() + static_cast<uint32_t>(sig_registers) :
3524 arg[sig_registers];
3525 if (reg_type.IsIntegralTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003526 const RegType& src_type = work_line_->GetRegisterType(this, get_reg);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003527 if (!src_type.IsIntegralTypes()) {
3528 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register v" << get_reg << " has type " << src_type
3529 << " but expected " << reg_type;
Andreas Gampeb588f4c2015-05-26 13:35:39 -07003530 return nullptr;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003531 }
Andreas Gampeda9badb2015-06-05 20:22:12 -07003532 } else {
3533 if (!work_line_->VerifyRegisterType(this, get_reg, reg_type)) {
3534 // Continue on soft failures. We need to find possible hard failures to avoid problems in
3535 // the compiler.
3536 if (have_pending_hard_failure_) {
3537 return nullptr;
3538 }
3539 } else if (reg_type.IsLongOrDoubleTypes()) {
3540 // Check that registers are consecutive (for non-range invokes). Invokes are the only
3541 // instructions not specifying register pairs by the first component, but require them
3542 // nonetheless. Only check when there's an actual register in the parameters. If there's
3543 // none, this will fail below.
3544 if (!is_range && sig_registers + 1 < expected_args) {
3545 uint32_t second_reg = arg[sig_registers + 1];
3546 if (second_reg != get_reg + 1) {
3547 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, long or double parameter "
3548 "at index " << sig_registers << " is not a pair: " << get_reg << " + "
3549 << second_reg << ".";
3550 return nullptr;
3551 }
3552 }
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003553 }
3554 }
3555 sig_registers += reg_type.IsLongOrDoubleTypes() ? 2 : 1;
3556 }
3557 if (expected_args != sig_registers) {
3558 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, expected " << expected_args <<
3559 " arguments, found " << sig_registers;
3560 return nullptr;
3561 }
3562 return res_method;
3563}
3564
3565void MethodVerifier::VerifyInvocationArgsUnresolvedMethod(const Instruction* inst,
3566 MethodType method_type,
3567 bool is_range) {
3568 // As the method may not have been resolved, make this static check against what we expect.
3569 // The main reason for this code block is to fail hard when we find an illegal use, e.g.,
3570 // wrong number of arguments or wrong primitive types, even if the method could not be resolved.
3571 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
3572 DexFileParameterIterator it(*dex_file_,
3573 dex_file_->GetProtoId(dex_file_->GetMethodId(method_idx).proto_idx_));
3574 VerifyInvocationArgsFromIterator<DexFileParameterIterator>(&it, inst, method_type, is_range,
3575 nullptr);
3576}
3577
3578class MethodParamListDescriptorIterator {
3579 public:
Mathieu Chartiere401d142015-04-22 13:56:20 -07003580 explicit MethodParamListDescriptorIterator(ArtMethod* res_method) :
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003581 res_method_(res_method), pos_(0), params_(res_method->GetParameterTypeList()),
3582 params_size_(params_ == nullptr ? 0 : params_->Size()) {
3583 }
3584
3585 bool HasNext() {
3586 return pos_ < params_size_;
3587 }
3588
3589 void Next() {
3590 ++pos_;
3591 }
3592
Mathieu Chartier90443472015-07-16 20:32:27 -07003593 const char* GetDescriptor() SHARED_REQUIRES(Locks::mutator_lock_) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003594 return res_method_->GetTypeDescriptorFromTypeIdx(params_->GetTypeItem(pos_).type_idx_);
3595 }
3596
3597 private:
Mathieu Chartiere401d142015-04-22 13:56:20 -07003598 ArtMethod* res_method_;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003599 size_t pos_;
3600 const DexFile::TypeList* params_;
3601 const size_t params_size_;
3602};
3603
Mathieu Chartiere401d142015-04-22 13:56:20 -07003604ArtMethod* MethodVerifier::VerifyInvocationArgs(
3605 const Instruction* inst, MethodType method_type, bool is_range, bool is_super) {
jeffhao8cd6dda2012-02-22 10:15:34 -08003606 // Resolve the method. This could be an abstract or concrete method depending on what sort of call
3607 // we're making.
Sebastien Hertz5243e912013-05-21 10:55:07 +02003608 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Andreas Gampeacc4d2f2014-06-12 19:35:05 -07003609
Mathieu Chartiere401d142015-04-22 13:56:20 -07003610 ArtMethod* res_method = ResolveMethodAndCheckAccess(method_idx, method_type);
Ian Rogers7b078e82014-09-10 14:44:24 -07003611 if (res_method == nullptr) { // error or class is unresolved
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003612 // Check what we can statically.
3613 if (!have_pending_hard_failure_) {
3614 VerifyInvocationArgsUnresolvedMethod(inst, method_type, is_range);
3615 }
3616 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003617 }
3618
Ian Rogersd81871c2011-10-03 13:57:23 -07003619 // If we're using invoke-super(method), make sure that the executing method's class' superclass
3620 // has a vtable entry for the target method.
3621 if (is_super) {
3622 DCHECK(method_type == METHOD_VIRTUAL);
Ian Rogersd8f69b02014-09-10 21:43:52 +00003623 const RegType& super = GetDeclaringClass().GetSuperClass(&reg_types_);
Ian Rogers529781d2012-07-23 17:24:29 -07003624 if (super.IsUnresolvedTypes()) {
jeffhao4d8df822012-04-24 17:09:36 -07003625 Fail(VERIFY_ERROR_NO_METHOD) << "unknown super class in invoke-super from "
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003626 << PrettyMethod(dex_method_idx_, *dex_file_)
jeffhao4d8df822012-04-24 17:09:36 -07003627 << " to super " << PrettyMethod(res_method);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003628 return nullptr;
jeffhao4d8df822012-04-24 17:09:36 -07003629 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003630 mirror::Class* super_klass = super.GetClass();
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003631 if (res_method->GetMethodIndex() >= super_klass->GetVTableLength()) {
jeffhao4d8df822012-04-24 17:09:36 -07003632 Fail(VERIFY_ERROR_NO_METHOD) << "invalid invoke-super from "
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003633 << PrettyMethod(dex_method_idx_, *dex_file_)
jeffhao4d8df822012-04-24 17:09:36 -07003634 << " to super " << super
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003635 << "." << res_method->GetName()
3636 << res_method->GetSignature();
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003637 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003638 }
3639 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003640
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003641 // Process the target method's signature. This signature may or may not
3642 MethodParamListDescriptorIterator it(res_method);
3643 return VerifyInvocationArgsFromIterator<MethodParamListDescriptorIterator>(&it, inst, method_type,
3644 is_range, res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07003645}
3646
Mathieu Chartiere401d142015-04-22 13:56:20 -07003647ArtMethod* MethodVerifier::GetQuickInvokedMethod(const Instruction* inst, RegisterLine* reg_line,
3648 bool is_range, bool allow_failure) {
Mathieu Chartier091d2382015-03-06 10:59:06 -08003649 if (is_range) {
3650 DCHECK_EQ(inst->Opcode(), Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
3651 } else {
3652 DCHECK_EQ(inst->Opcode(), Instruction::INVOKE_VIRTUAL_QUICK);
3653 }
3654 const RegType& actual_arg_type = reg_line->GetInvocationThis(this, inst, is_range, allow_failure);
Ian Rogers9bc54402014-04-17 16:40:01 -07003655 if (!actual_arg_type.HasClass()) {
3656 VLOG(verifier) << "Failed to get mirror::Class* from '" << actual_arg_type << "'";
Andreas Gampe63981562014-04-17 12:28:43 -07003657 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003658 }
Ian Rogersa4cf1df2014-05-07 19:47:17 -07003659 mirror::Class* klass = actual_arg_type.GetClass();
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003660 mirror::Class* dispatch_class;
Ian Rogersa4cf1df2014-05-07 19:47:17 -07003661 if (klass->IsInterface()) {
3662 // Derive Object.class from Class.class.getSuperclass().
3663 mirror::Class* object_klass = klass->GetClass()->GetSuperClass();
Andreas Gampe7c038102014-10-27 20:08:46 -07003664 if (FailOrAbort(this, object_klass->IsObjectClass(),
Mathieu Chartier091d2382015-03-06 10:59:06 -08003665 "Failed to find Object class in quickened invoke receiver", work_insn_idx_)) {
Andreas Gampe7c038102014-10-27 20:08:46 -07003666 return nullptr;
3667 }
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003668 dispatch_class = object_klass;
Ian Rogersa4cf1df2014-05-07 19:47:17 -07003669 } else {
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003670 dispatch_class = klass;
Ian Rogersa4cf1df2014-05-07 19:47:17 -07003671 }
Mathieu Chartier091d2382015-03-06 10:59:06 -08003672 if (!dispatch_class->HasVTable()) {
3673 FailOrAbort(this, allow_failure, "Receiver class has no vtable for quickened invoke at ",
3674 work_insn_idx_);
Andreas Gampe7c038102014-10-27 20:08:46 -07003675 return nullptr;
3676 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003677 uint16_t vtable_index = is_range ? inst->VRegB_3rc() : inst->VRegB_35c();
Mathieu Chartiere401d142015-04-22 13:56:20 -07003678 auto* cl = Runtime::Current()->GetClassLinker();
3679 auto pointer_size = cl->GetImagePointerSize();
Mathieu Chartier091d2382015-03-06 10:59:06 -08003680 if (static_cast<int32_t>(vtable_index) >= dispatch_class->GetVTableLength()) {
3681 FailOrAbort(this, allow_failure,
3682 "Receiver class has not enough vtable slots for quickened invoke at ",
3683 work_insn_idx_);
Andreas Gampe7c038102014-10-27 20:08:46 -07003684 return nullptr;
3685 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07003686 ArtMethod* res_method = dispatch_class->GetVTableEntry(vtable_index, pointer_size);
Mathieu Chartier091d2382015-03-06 10:59:06 -08003687 if (self_->IsExceptionPending()) {
3688 FailOrAbort(this, allow_failure, "Unexpected exception pending for quickened invoke at ",
3689 work_insn_idx_);
Andreas Gampe7c038102014-10-27 20:08:46 -07003690 return nullptr;
3691 }
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003692 return res_method;
3693}
3694
Mathieu Chartiere401d142015-04-22 13:56:20 -07003695ArtMethod* MethodVerifier::VerifyInvokeVirtualQuickArgs(const Instruction* inst, bool is_range) {
Andreas Gampe76bd8802014-12-10 16:43:58 -08003696 DCHECK(Runtime::Current()->IsStarted() || verify_to_dump_)
3697 << PrettyMethod(dex_method_idx_, *dex_file_, true) << "@" << work_insn_idx_;
3698
Mathieu Chartiere401d142015-04-22 13:56:20 -07003699 ArtMethod* res_method = GetQuickInvokedMethod(inst, work_line_.get(), is_range, false);
Ian Rogers7b078e82014-09-10 14:44:24 -07003700 if (res_method == nullptr) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003701 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer method from " << inst->Name();
Ian Rogers7b078e82014-09-10 14:44:24 -07003702 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003703 }
Andreas Gampe7c038102014-10-27 20:08:46 -07003704 if (FailOrAbort(this, !res_method->IsDirect(), "Quick-invoked method is direct at ",
3705 work_insn_idx_)) {
3706 return nullptr;
3707 }
3708 if (FailOrAbort(this, !res_method->IsStatic(), "Quick-invoked method is static at ",
3709 work_insn_idx_)) {
3710 return nullptr;
3711 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003712
3713 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
3714 // match the call to the signature. Also, we might be calling through an abstract method
3715 // definition (which doesn't have register count values).
Ian Rogers7b078e82014-09-10 14:44:24 -07003716 const RegType& actual_arg_type = work_line_->GetInvocationThis(this, inst, is_range);
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003717 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
Ian Rogers7b078e82014-09-10 14:44:24 -07003718 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003719 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003720 const size_t expected_args = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
3721 /* caught by static verifier */
3722 DCHECK(is_range || expected_args <= 5);
3723 if (expected_args > code_item_->outs_size_) {
3724 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
3725 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
Ian Rogers7b078e82014-09-10 14:44:24 -07003726 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003727 }
3728
3729 /*
3730 * Check the "this" argument, which must be an instance of the class that declared the method.
3731 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
3732 * rigorous check here (which is okay since we have to do it at runtime).
3733 */
3734 if (actual_arg_type.IsUninitializedReference() && !res_method->IsConstructor()) {
3735 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
Ian Rogers7b078e82014-09-10 14:44:24 -07003736 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003737 }
3738 if (!actual_arg_type.IsZero()) {
3739 mirror::Class* klass = res_method->GetDeclaringClass();
Ian Rogers1ff3c982014-08-12 02:30:58 -07003740 std::string temp;
Ian Rogersd8f69b02014-09-10 21:43:52 +00003741 const RegType& res_method_class =
Andreas Gampef23f33d2015-06-23 14:18:17 -07003742 FromClass(klass->GetDescriptor(&temp), klass, klass->CannotBeAssignedFromOtherTypes());
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003743 if (!res_method_class.IsAssignableFrom(actual_arg_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07003744 Fail(actual_arg_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS :
3745 VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003746 << "' not instance of '" << res_method_class << "'";
Ian Rogers7b078e82014-09-10 14:44:24 -07003747 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003748 }
3749 }
3750 /*
3751 * Process the target method's signature. This signature may or may not
3752 * have been verified, so we can't assume it's properly formed.
3753 */
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003754 const DexFile::TypeList* params = res_method->GetParameterTypeList();
Ian Rogers7b078e82014-09-10 14:44:24 -07003755 size_t params_size = params == nullptr ? 0 : params->Size();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003756 uint32_t arg[5];
3757 if (!is_range) {
Ian Rogers29a26482014-05-02 15:27:29 -07003758 inst->GetVarArgs(arg);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003759 }
3760 size_t actual_args = 1;
3761 for (size_t param_index = 0; param_index < params_size; param_index++) {
3762 if (actual_args >= expected_args) {
3763 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invalid call to '" << PrettyMethod(res_method)
Brian Carlstrom93c33962013-07-26 10:37:43 -07003764 << "'. Expected " << expected_args
3765 << " arguments, processing argument " << actual_args
3766 << " (where longs/doubles count twice).";
Ian Rogers7b078e82014-09-10 14:44:24 -07003767 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003768 }
3769 const char* descriptor =
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003770 res_method->GetTypeDescriptorFromTypeIdx(params->GetTypeItem(param_index).type_idx_);
Ian Rogers7b078e82014-09-10 14:44:24 -07003771 if (descriptor == nullptr) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003772 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003773 << " missing signature component";
Ian Rogers7b078e82014-09-10 14:44:24 -07003774 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003775 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00003776 const RegType& reg_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003777 uint32_t get_reg = is_range ? inst->VRegC_3rc() + actual_args : arg[actual_args];
Ian Rogers7b078e82014-09-10 14:44:24 -07003778 if (!work_line_->VerifyRegisterType(this, get_reg, reg_type)) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003779 return res_method;
3780 }
3781 actual_args = reg_type.IsLongOrDoubleTypes() ? actual_args + 2 : actual_args + 1;
3782 }
3783 if (actual_args != expected_args) {
3784 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
3785 << " expected " << expected_args << " arguments, found " << actual_args;
Ian Rogers7b078e82014-09-10 14:44:24 -07003786 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003787 } else {
3788 return res_method;
3789 }
3790}
3791
Ian Rogers62342ec2013-06-11 10:26:37 -07003792void MethodVerifier::VerifyNewArray(const Instruction* inst, bool is_filled, bool is_range) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003793 uint32_t type_idx;
3794 if (!is_filled) {
3795 DCHECK_EQ(inst->Opcode(), Instruction::NEW_ARRAY);
3796 type_idx = inst->VRegC_22c();
3797 } else if (!is_range) {
3798 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY);
3799 type_idx = inst->VRegB_35c();
3800 } else {
3801 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY_RANGE);
3802 type_idx = inst->VRegB_3rc();
3803 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00003804 const RegType& res_type = ResolveClassAndCheckAccess(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003805 if (res_type.IsConflict()) { // bad class
3806 DCHECK_NE(failures_.size(), 0U);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003807 } else {
3808 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
3809 if (!res_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003810 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "new-array on non-array class " << res_type;
Ian Rogers0c4a5062012-02-03 15:18:59 -08003811 } else if (!is_filled) {
3812 /* make sure "size" register is valid type */
Ian Rogers7b078e82014-09-10 14:44:24 -07003813 work_line_->VerifyRegisterType(this, inst->VRegB_22c(), reg_types_.Integer());
Ian Rogers0c4a5062012-02-03 15:18:59 -08003814 /* set register type to array class */
Ian Rogersd8f69b02014-09-10 21:43:52 +00003815 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
Ian Rogers7b078e82014-09-10 14:44:24 -07003816 work_line_->SetRegisterType(this, inst->VRegA_22c(), precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003817 } else {
3818 // Verify each register. If "arg_count" is bad, VerifyRegisterType() will run off the end of
3819 // the list and fail. It's legal, if silly, for arg_count to be zero.
Ian Rogersd8f69b02014-09-10 21:43:52 +00003820 const RegType& expected_type = reg_types_.GetComponentType(res_type, GetClassLoader());
Sebastien Hertz5243e912013-05-21 10:55:07 +02003821 uint32_t arg_count = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
3822 uint32_t arg[5];
3823 if (!is_range) {
Ian Rogers29a26482014-05-02 15:27:29 -07003824 inst->GetVarArgs(arg);
Sebastien Hertz5243e912013-05-21 10:55:07 +02003825 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08003826 for (size_t ui = 0; ui < arg_count; ui++) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003827 uint32_t get_reg = is_range ? inst->VRegC_3rc() + ui : arg[ui];
Ian Rogers7b078e82014-09-10 14:44:24 -07003828 if (!work_line_->VerifyRegisterType(this, get_reg, expected_type)) {
3829 work_line_->SetResultRegisterType(this, reg_types_.Conflict());
Ian Rogers0c4a5062012-02-03 15:18:59 -08003830 return;
3831 }
3832 }
3833 // filled-array result goes into "result" register
Ian Rogersd8f69b02014-09-10 21:43:52 +00003834 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
Ian Rogers7b078e82014-09-10 14:44:24 -07003835 work_line_->SetResultRegisterType(this, precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003836 }
3837 }
3838}
3839
Sebastien Hertz5243e912013-05-21 10:55:07 +02003840void MethodVerifier::VerifyAGet(const Instruction* inst,
Ian Rogersd8f69b02014-09-10 21:43:52 +00003841 const RegType& insn_type, bool is_primitive) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003842 const RegType& index_type = work_line_->GetRegisterType(this, inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07003843 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003844 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07003845 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07003846 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegB_23x());
Ian Rogers89310de2012-02-01 13:47:30 -08003847 if (array_type.IsZero()) {
Nicolas Geoffray4824c272015-06-24 15:53:03 +01003848 have_pending_runtime_throw_failure_ = true;
Ian Rogers89310de2012-02-01 13:47:30 -08003849 // Null array class; this code path will fail at runtime. Infer a merge-able type from the
3850 // instruction type. TODO: have a proper notion of bottom here.
3851 if (!is_primitive || insn_type.IsCategory1Types()) {
3852 // Reference or category 1
Ian Rogers7b078e82014-09-10 14:44:24 -07003853 work_line_->SetRegisterType(this, inst->VRegA_23x(), reg_types_.Zero());
Ian Rogersd81871c2011-10-03 13:57:23 -07003854 } else {
Ian Rogers89310de2012-02-01 13:47:30 -08003855 // Category 2
Ian Rogers7b078e82014-09-10 14:44:24 -07003856 work_line_->SetRegisterTypeWide(this, inst->VRegA_23x(),
3857 reg_types_.FromCat2ConstLo(0, false),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003858 reg_types_.FromCat2ConstHi(0, false));
Ian Rogers89310de2012-02-01 13:47:30 -08003859 }
jeffhaofc3144e2012-02-01 17:21:15 -08003860 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003861 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aget";
Ian Rogers89310de2012-02-01 13:47:30 -08003862 } else {
3863 /* verify the class */
Ian Rogersd8f69b02014-09-10 21:43:52 +00003864 const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
jeffhaofc3144e2012-02-01 17:21:15 -08003865 if (!component_type.IsReferenceTypes() && !is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07003866 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08003867 << " source for aget-object";
3868 } else if (component_type.IsNonZeroReferenceTypes() && is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07003869 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08003870 << " source for category 1 aget";
3871 } else if (is_primitive && !insn_type.Equals(component_type) &&
3872 !((insn_type.IsInteger() && component_type.IsFloat()) ||
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003873 (insn_type.IsLong() && component_type.IsDouble()))) {
3874 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array type " << array_type
3875 << " incompatible with aget of type " << insn_type;
Ian Rogers89310de2012-02-01 13:47:30 -08003876 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003877 // Use knowledge of the field type which is stronger than the type inferred from the
3878 // instruction, which can't differentiate object types and ints from floats, longs from
3879 // doubles.
3880 if (!component_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003881 work_line_->SetRegisterType(this, inst->VRegA_23x(), component_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003882 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07003883 work_line_->SetRegisterTypeWide(this, inst->VRegA_23x(), component_type,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003884 component_type.HighHalf(&reg_types_));
3885 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003886 }
3887 }
3888 }
3889}
3890
Ian Rogersd8f69b02014-09-10 21:43:52 +00003891void MethodVerifier::VerifyPrimitivePut(const RegType& target_type, const RegType& insn_type,
Jeff Haofe1f7c82013-08-01 14:50:24 -07003892 const uint32_t vregA) {
3893 // Primitive assignability rules are weaker than regular assignability rules.
3894 bool instruction_compatible;
3895 bool value_compatible;
Ian Rogers7b078e82014-09-10 14:44:24 -07003896 const RegType& value_type = work_line_->GetRegisterType(this, vregA);
Jeff Haofe1f7c82013-08-01 14:50:24 -07003897 if (target_type.IsIntegralTypes()) {
Jeff Haoa4647482013-08-06 15:35:47 -07003898 instruction_compatible = target_type.Equals(insn_type);
Jeff Haofe1f7c82013-08-01 14:50:24 -07003899 value_compatible = value_type.IsIntegralTypes();
3900 } else if (target_type.IsFloat()) {
3901 instruction_compatible = insn_type.IsInteger(); // no put-float, so expect put-int
3902 value_compatible = value_type.IsFloatTypes();
3903 } else if (target_type.IsLong()) {
3904 instruction_compatible = insn_type.IsLong();
Andreas Gampe376fa682014-09-07 13:06:12 -07003905 // Additional register check: this is not checked statically (as part of VerifyInstructions),
3906 // as target_type depends on the resolved type of the field.
3907 if (instruction_compatible && work_line_->NumRegs() > vregA + 1) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003908 const RegType& value_type_hi = work_line_->GetRegisterType(this, vregA + 1);
Andreas Gampe376fa682014-09-07 13:06:12 -07003909 value_compatible = value_type.IsLongTypes() && value_type.CheckWidePair(value_type_hi);
3910 } else {
3911 value_compatible = false;
3912 }
Jeff Haofe1f7c82013-08-01 14:50:24 -07003913 } else if (target_type.IsDouble()) {
3914 instruction_compatible = insn_type.IsLong(); // no put-double, so expect put-long
Andreas Gampe376fa682014-09-07 13:06:12 -07003915 // Additional register check: this is not checked statically (as part of VerifyInstructions),
3916 // as target_type depends on the resolved type of the field.
3917 if (instruction_compatible && work_line_->NumRegs() > vregA + 1) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003918 const RegType& value_type_hi = work_line_->GetRegisterType(this, vregA + 1);
Andreas Gampe376fa682014-09-07 13:06:12 -07003919 value_compatible = value_type.IsDoubleTypes() && value_type.CheckWidePair(value_type_hi);
3920 } else {
3921 value_compatible = false;
3922 }
Jeff Haofe1f7c82013-08-01 14:50:24 -07003923 } else {
3924 instruction_compatible = false; // reference with primitive store
3925 value_compatible = false; // unused
3926 }
3927 if (!instruction_compatible) {
3928 // This is a global failure rather than a class change failure as the instructions and
3929 // the descriptors for the type should have been consistent within the same file at
3930 // compile time.
3931 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "put insn has type '" << insn_type
3932 << "' but expected type '" << target_type << "'";
3933 return;
3934 }
3935 if (!value_compatible) {
3936 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
3937 << " of type " << value_type << " but expected " << target_type << " for put";
3938 return;
3939 }
3940}
3941
Sebastien Hertz5243e912013-05-21 10:55:07 +02003942void MethodVerifier::VerifyAPut(const Instruction* inst,
Ian Rogersd8f69b02014-09-10 21:43:52 +00003943 const RegType& insn_type, bool is_primitive) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003944 const RegType& index_type = work_line_->GetRegisterType(this, inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07003945 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003946 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07003947 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07003948 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegB_23x());
Ian Rogers89310de2012-02-01 13:47:30 -08003949 if (array_type.IsZero()) {
Nicolas Geoffray66389fb2015-06-19 10:35:42 +01003950 // Null array type; this code path will fail at runtime.
3951 // Still check that the given value matches the instruction's type.
3952 work_line_->VerifyRegisterType(this, inst->VRegA_23x(), insn_type);
jeffhaofc3144e2012-02-01 17:21:15 -08003953 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003954 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aput";
Ian Rogers89310de2012-02-01 13:47:30 -08003955 } else {
Ian Rogersd8f69b02014-09-10 21:43:52 +00003956 const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
Jeff Haofe1f7c82013-08-01 14:50:24 -07003957 const uint32_t vregA = inst->VRegA_23x();
Jeff Haob24b4a72013-07-31 13:47:31 -07003958 if (is_primitive) {
Jeff Haofe1f7c82013-08-01 14:50:24 -07003959 VerifyPrimitivePut(component_type, insn_type, vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07003960 } else {
Jeff Haob24b4a72013-07-31 13:47:31 -07003961 if (!component_type.IsReferenceTypes()) {
3962 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
3963 << " source for aput-object";
3964 } else {
3965 // The instruction agrees with the type of array, confirm the value to be stored does too
3966 // Note: we use the instruction type (rather than the component type) for aput-object as
3967 // incompatible classes will be caught at runtime as an array store exception
Ian Rogers7b078e82014-09-10 14:44:24 -07003968 work_line_->VerifyRegisterType(this, vregA, insn_type);
Jeff Haob24b4a72013-07-31 13:47:31 -07003969 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003970 }
3971 }
3972 }
3973}
3974
Mathieu Chartierc7853442015-03-27 14:35:38 -07003975ArtField* MethodVerifier::GetStaticField(int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08003976 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3977 // Check access to class
Ian Rogersd8f69b02014-09-10 21:43:52 +00003978 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Brian Carlstrom7934ac22013-07-26 10:54:15 -07003979 if (klass_type.IsConflict()) { // bad class
Ian Rogersad0b3a32012-04-16 14:50:24 -07003980 AppendToLastFailMessage(StringPrintf(" in attempt to access static field %d (%s) in %s",
3981 field_idx, dex_file_->GetFieldName(field_id),
3982 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers7b078e82014-09-10 14:44:24 -07003983 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08003984 }
Elliott Hughesb25c3f62012-03-26 16:35:06 -07003985 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003986 return nullptr; // Can't resolve Class so no more to do here, will do checking at runtime.
Ian Rogers90040192011-12-16 08:54:29 -08003987 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07003988 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartierc7853442015-03-27 14:35:38 -07003989 ArtField* field = class_linker->ResolveFieldJLS(*dex_file_, field_idx, dex_cache_,
3990 class_loader_);
Ian Rogers7b078e82014-09-10 14:44:24 -07003991 if (field == nullptr) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07003992 VLOG(verifier) << "Unable to resolve static field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07003993 << dex_file_->GetFieldName(field_id) << ") in "
3994 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogers7b078e82014-09-10 14:44:24 -07003995 DCHECK(self_->IsExceptionPending());
3996 self_->ClearException();
3997 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003998 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
3999 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004000 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access static field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07004001 << " from " << GetDeclaringClass();
Ian Rogers7b078e82014-09-10 14:44:24 -07004002 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07004003 } else if (!field->IsStatic()) {
4004 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field) << " to be static";
Ian Rogers7b078e82014-09-10 14:44:24 -07004005 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07004006 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07004007 return field;
Ian Rogersd81871c2011-10-03 13:57:23 -07004008}
4009
Mathieu Chartierc7853442015-03-27 14:35:38 -07004010ArtField* MethodVerifier::GetInstanceField(const RegType& obj_type, int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08004011 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
4012 // Check access to class
Ian Rogersd8f69b02014-09-10 21:43:52 +00004013 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004014 if (klass_type.IsConflict()) {
4015 AppendToLastFailMessage(StringPrintf(" in attempt to access instance field %d (%s) in %s",
4016 field_idx, dex_file_->GetFieldName(field_id),
4017 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers7b078e82014-09-10 14:44:24 -07004018 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08004019 }
jeffhao8cd6dda2012-02-22 10:15:34 -08004020 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004021 return nullptr; // Can't resolve Class so no more to do here
Ian Rogers90040192011-12-16 08:54:29 -08004022 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07004023 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartierc7853442015-03-27 14:35:38 -07004024 ArtField* field = class_linker->ResolveFieldJLS(*dex_file_, field_idx, dex_cache_,
4025 class_loader_);
Ian Rogers7b078e82014-09-10 14:44:24 -07004026 if (field == nullptr) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07004027 VLOG(verifier) << "Unable to resolve instance field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07004028 << dex_file_->GetFieldName(field_id) << ") in "
4029 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogers7b078e82014-09-10 14:44:24 -07004030 DCHECK(self_->IsExceptionPending());
4031 self_->ClearException();
4032 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004033 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
4034 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004035 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access instance field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07004036 << " from " << GetDeclaringClass();
Ian Rogers7b078e82014-09-10 14:44:24 -07004037 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07004038 } else if (field->IsStatic()) {
4039 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field)
4040 << " to not be static";
Ian Rogers7b078e82014-09-10 14:44:24 -07004041 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07004042 } else if (obj_type.IsZero()) {
4043 // Cannot infer and check type, however, access will cause null pointer exception
4044 return field;
Stephen Kyle695c5982014-08-22 15:03:07 +01004045 } else if (!obj_type.IsReferenceTypes()) {
4046 // Trying to read a field from something that isn't a reference
4047 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance field access on object that has "
4048 << "non-reference type " << obj_type;
Ian Rogers7b078e82014-09-10 14:44:24 -07004049 return nullptr;
Ian Rogerse1758fe2012-04-19 11:31:15 -07004050 } else {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08004051 mirror::Class* klass = field->GetDeclaringClass();
Ian Rogersd8f69b02014-09-10 21:43:52 +00004052 const RegType& field_klass =
Andreas Gampef23f33d2015-06-23 14:18:17 -07004053 FromClass(dex_file_->GetFieldDeclaringClassDescriptor(field_id),
4054 klass, klass->CannotBeAssignedFromOtherTypes());
Ian Rogersad0b3a32012-04-16 14:50:24 -07004055 if (obj_type.IsUninitializedTypes() &&
4056 (!IsConstructor() || GetDeclaringClass().Equals(obj_type) ||
4057 !field_klass.Equals(GetDeclaringClass()))) {
4058 // Field accesses through uninitialized references are only allowable for constructors where
4059 // the field is declared in this class
4060 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "cannot access instance field " << PrettyField(field)
Brian Carlstrom93c33962013-07-26 10:37:43 -07004061 << " of a not fully initialized object within the context"
4062 << " of " << PrettyMethod(dex_method_idx_, *dex_file_);
Ian Rogers7b078e82014-09-10 14:44:24 -07004063 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004064 } else if (!field_klass.IsAssignableFrom(obj_type)) {
4065 // Trying to access C1.field1 using reference of type C2, which is neither C1 or a sub-class
4066 // of C1. For resolution to occur the declared class of the field must be compatible with
4067 // obj_type, we've discovered this wasn't so, so report the field didn't exist.
4068 Fail(VERIFY_ERROR_NO_FIELD) << "cannot access instance field " << PrettyField(field)
4069 << " from object of type " << obj_type;
Ian Rogers7b078e82014-09-10 14:44:24 -07004070 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004071 } else {
4072 return field;
4073 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004074 }
4075}
4076
Andreas Gampe896df402014-10-20 22:25:29 -07004077template <MethodVerifier::FieldAccessType kAccType>
4078void MethodVerifier::VerifyISFieldAccess(const Instruction* inst, const RegType& insn_type,
4079 bool is_primitive, bool is_static) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02004080 uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
Mathieu Chartierc7853442015-03-27 14:35:38 -07004081 ArtField* field;
Ian Rogersb94a27b2011-10-26 00:33:41 -07004082 if (is_static) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07004083 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07004084 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004085 const RegType& object_type = work_line_->GetRegisterType(this, inst->VRegB_22c());
Ian Rogersf4028cc2011-11-02 14:56:39 -07004086 field = GetInstanceField(object_type, field_idx);
Andreas Gampe896df402014-10-20 22:25:29 -07004087 if (UNLIKELY(have_pending_hard_failure_)) {
4088 return;
4089 }
Ian Rogersb94a27b2011-10-26 00:33:41 -07004090 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00004091 const RegType* field_type = nullptr;
Ian Rogers7b078e82014-09-10 14:44:24 -07004092 if (field != nullptr) {
Andreas Gampe896df402014-10-20 22:25:29 -07004093 if (kAccType == FieldAccessType::kAccPut) {
4094 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
4095 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
4096 << " from other class " << GetDeclaringClass();
4097 return;
4098 }
4099 }
4100
Mathieu Chartierc7853442015-03-27 14:35:38 -07004101 mirror::Class* field_type_class =
4102 can_load_classes_ ? field->GetType<true>() : field->GetType<false>();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004103 if (field_type_class != nullptr) {
Andreas Gampef23f33d2015-06-23 14:18:17 -07004104 field_type = &FromClass(field->GetTypeDescriptor(), field_type_class,
4105 field_type_class->CannotBeAssignedFromOtherTypes());
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08004106 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004107 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
4108 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004109 }
Ian Rogers0d604842012-04-16 14:50:24 -07004110 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004111 if (field_type == nullptr) {
4112 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
4113 const char* descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
Mathieu Chartierbf99f772014-08-23 16:37:27 -07004114 field_type = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004115 }
Sebastien Hertz757b3042014-03-28 14:34:28 +01004116 DCHECK(field_type != nullptr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02004117 const uint32_t vregA = (is_static) ? inst->VRegA_21c() : inst->VRegA_22c();
Andreas Gampe896df402014-10-20 22:25:29 -07004118 static_assert(kAccType == FieldAccessType::kAccPut || kAccType == FieldAccessType::kAccGet,
4119 "Unexpected third access type");
4120 if (kAccType == FieldAccessType::kAccPut) {
4121 // sput or iput.
4122 if (is_primitive) {
4123 VerifyPrimitivePut(*field_type, insn_type, vregA);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004124 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004125 if (!insn_type.IsAssignableFrom(*field_type)) {
Vladimir Marko414000e2015-06-23 17:45:21 +01004126 // If the field type is not a reference, this is a global failure rather than
4127 // a class change failure as the instructions and the descriptors for the type
4128 // should have been consistent within the same file at compile time.
4129 VerifyError error = field_type->IsReferenceTypes() ? VERIFY_ERROR_BAD_CLASS_SOFT
4130 : VERIFY_ERROR_BAD_CLASS_HARD;
4131 Fail(error) << "expected field " << PrettyField(field)
4132 << " to be compatible with type '" << insn_type
4133 << "' but found type '" << *field_type
4134 << "' in put-object";
Andreas Gampe896df402014-10-20 22:25:29 -07004135 return;
4136 }
4137 work_line_->VerifyRegisterType(this, vregA, *field_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004138 }
Andreas Gampe896df402014-10-20 22:25:29 -07004139 } else if (kAccType == FieldAccessType::kAccGet) {
4140 // sget or iget.
4141 if (is_primitive) {
4142 if (field_type->Equals(insn_type) ||
4143 (field_type->IsFloat() && insn_type.IsInteger()) ||
4144 (field_type->IsDouble() && insn_type.IsLong())) {
4145 // expected that read is of the correct primitive type or that int reads are reading
4146 // floats or long reads are reading doubles
4147 } else {
4148 // This is a global failure rather than a class change failure as the instructions and
4149 // the descriptors for the type should have been consistent within the same file at
4150 // compile time
4151 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
4152 << " to be of type '" << insn_type
4153 << "' but found type '" << *field_type << "' in get";
4154 return;
4155 }
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08004156 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004157 if (!insn_type.IsAssignableFrom(*field_type)) {
Vladimir Marko414000e2015-06-23 17:45:21 +01004158 // If the field type is not a reference, this is a global failure rather than
4159 // a class change failure as the instructions and the descriptors for the type
4160 // should have been consistent within the same file at compile time.
4161 VerifyError error = field_type->IsReferenceTypes() ? VERIFY_ERROR_BAD_CLASS_SOFT
4162 : VERIFY_ERROR_BAD_CLASS_HARD;
4163 Fail(error) << "expected field " << PrettyField(field)
4164 << " to be compatible with type '" << insn_type
4165 << "' but found type '" << *field_type
4166 << "' in get-object";
Andreas Gampe890da292015-07-06 17:20:18 -07004167 if (error != VERIFY_ERROR_BAD_CLASS_HARD) {
4168 work_line_->SetRegisterType(this, vregA, reg_types_.Conflict());
4169 }
Andreas Gampe896df402014-10-20 22:25:29 -07004170 return;
4171 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004172 }
Andreas Gampe896df402014-10-20 22:25:29 -07004173 if (!field_type->IsLowHalf()) {
4174 work_line_->SetRegisterType(this, vregA, *field_type);
4175 } else {
4176 work_line_->SetRegisterTypeWide(this, vregA, *field_type, field_type->HighHalf(&reg_types_));
4177 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07004178 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004179 LOG(FATAL) << "Unexpected case.";
Ian Rogersd81871c2011-10-03 13:57:23 -07004180 }
4181}
4182
Mathieu Chartierc7853442015-03-27 14:35:38 -07004183ArtField* MethodVerifier::GetQuickFieldAccess(const Instruction* inst,
Ian Rogers98379392014-02-24 16:53:16 -08004184 RegisterLine* reg_line) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08004185 DCHECK(IsInstructionIGetQuickOrIPutQuick(inst->Opcode())) << inst->Opcode();
Ian Rogers7b078e82014-09-10 14:44:24 -07004186 const RegType& object_type = reg_line->GetRegisterType(this, inst->VRegB_22c());
Ian Rogers9bc54402014-04-17 16:40:01 -07004187 if (!object_type.HasClass()) {
4188 VLOG(verifier) << "Failed to get mirror::Class* from '" << object_type << "'";
4189 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004190 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004191 uint32_t field_offset = static_cast<uint32_t>(inst->VRegC_22c());
Mathieu Chartierc7853442015-03-27 14:35:38 -07004192 ArtField* const f = ArtField::FindInstanceFieldWithOffset(object_type.GetClass(), field_offset);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08004193 DCHECK_EQ(f->GetOffset().Uint32Value(), field_offset);
Sebastien Hertz479fc1e2014-04-04 17:51:34 +02004194 if (f == nullptr) {
4195 VLOG(verifier) << "Failed to find instance field at offset '" << field_offset
4196 << "' from '" << PrettyDescriptor(object_type.GetClass()) << "'";
4197 }
4198 return f;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004199}
4200
Andreas Gampe896df402014-10-20 22:25:29 -07004201template <MethodVerifier::FieldAccessType kAccType>
4202void MethodVerifier::VerifyQuickFieldAccess(const Instruction* inst, const RegType& insn_type,
4203 bool is_primitive) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07004204 DCHECK(Runtime::Current()->IsStarted() || verify_to_dump_);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004205
Mathieu Chartierc7853442015-03-27 14:35:38 -07004206 ArtField* field = GetQuickFieldAccess(inst, work_line_.get());
Ian Rogers7b078e82014-09-10 14:44:24 -07004207 if (field == nullptr) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004208 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field from " << inst->Name();
4209 return;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004210 }
Andreas Gampe896df402014-10-20 22:25:29 -07004211
4212 // For an IPUT_QUICK, we now test for final flag of the field.
4213 if (kAccType == FieldAccessType::kAccPut) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004214 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
4215 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004216 << " from other class " << GetDeclaringClass();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004217 return;
4218 }
4219 }
Andreas Gampe896df402014-10-20 22:25:29 -07004220
4221 // Get the field type.
4222 const RegType* field_type;
4223 {
Mathieu Chartierc7853442015-03-27 14:35:38 -07004224 mirror::Class* field_type_class = can_load_classes_ ? field->GetType<true>() :
4225 field->GetType<false>();
Andreas Gampe896df402014-10-20 22:25:29 -07004226
4227 if (field_type_class != nullptr) {
Andreas Gampef23f33d2015-06-23 14:18:17 -07004228 field_type = &FromClass(field->GetTypeDescriptor(), field_type_class,
4229 field_type_class->CannotBeAssignedFromOtherTypes());
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004230 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004231 Thread* self = Thread::Current();
4232 DCHECK(!can_load_classes_ || self->IsExceptionPending());
4233 self->ClearException();
4234 field_type = &reg_types_.FromDescriptor(field->GetDeclaringClass()->GetClassLoader(),
4235 field->GetTypeDescriptor(), false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004236 }
Andreas Gampe896df402014-10-20 22:25:29 -07004237 if (field_type == nullptr) {
4238 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field type from " << inst->Name();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004239 return;
4240 }
Andreas Gampe896df402014-10-20 22:25:29 -07004241 }
4242
4243 const uint32_t vregA = inst->VRegA_22c();
4244 static_assert(kAccType == FieldAccessType::kAccPut || kAccType == FieldAccessType::kAccGet,
4245 "Unexpected third access type");
4246 if (kAccType == FieldAccessType::kAccPut) {
4247 if (is_primitive) {
4248 // Primitive field assignability rules are weaker than regular assignability rules
4249 bool instruction_compatible;
4250 bool value_compatible;
4251 const RegType& value_type = work_line_->GetRegisterType(this, vregA);
4252 if (field_type->IsIntegralTypes()) {
4253 instruction_compatible = insn_type.IsIntegralTypes();
4254 value_compatible = value_type.IsIntegralTypes();
4255 } else if (field_type->IsFloat()) {
4256 instruction_compatible = insn_type.IsInteger(); // no [is]put-float, so expect [is]put-int
4257 value_compatible = value_type.IsFloatTypes();
4258 } else if (field_type->IsLong()) {
4259 instruction_compatible = insn_type.IsLong();
4260 value_compatible = value_type.IsLongTypes();
4261 } else if (field_type->IsDouble()) {
4262 instruction_compatible = insn_type.IsLong(); // no [is]put-double, so expect [is]put-long
4263 value_compatible = value_type.IsDoubleTypes();
4264 } else {
4265 instruction_compatible = false; // reference field with primitive store
4266 value_compatible = false; // unused
4267 }
4268 if (!instruction_compatible) {
4269 // This is a global failure rather than a class change failure as the instructions and
4270 // the descriptors for the type should have been consistent within the same file at
4271 // compile time
4272 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
4273 << " to be of type '" << insn_type
4274 << "' but found type '" << *field_type
4275 << "' in put";
4276 return;
4277 }
4278 if (!value_compatible) {
4279 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
4280 << " of type " << value_type
4281 << " but expected " << *field_type
4282 << " for store to " << PrettyField(field) << " in put";
4283 return;
4284 }
4285 } else {
4286 if (!insn_type.IsAssignableFrom(*field_type)) {
4287 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
4288 << " to be compatible with type '" << insn_type
4289 << "' but found type '" << *field_type
4290 << "' in put-object";
4291 return;
4292 }
4293 work_line_->VerifyRegisterType(this, vregA, *field_type);
4294 }
4295 } else if (kAccType == FieldAccessType::kAccGet) {
4296 if (is_primitive) {
4297 if (field_type->Equals(insn_type) ||
4298 (field_type->IsFloat() && insn_type.IsIntegralTypes()) ||
4299 (field_type->IsDouble() && insn_type.IsLongTypes())) {
4300 // expected that read is of the correct primitive type or that int reads are reading
4301 // floats or long reads are reading doubles
4302 } else {
4303 // This is a global failure rather than a class change failure as the instructions and
4304 // the descriptors for the type should have been consistent within the same file at
4305 // compile time
4306 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
4307 << " to be of type '" << insn_type
4308 << "' but found type '" << *field_type << "' in Get";
4309 return;
4310 }
4311 } else {
4312 if (!insn_type.IsAssignableFrom(*field_type)) {
4313 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
4314 << " to be compatible with type '" << insn_type
4315 << "' but found type '" << *field_type
4316 << "' in get-object";
4317 work_line_->SetRegisterType(this, vregA, reg_types_.Conflict());
4318 return;
4319 }
4320 }
4321 if (!field_type->IsLowHalf()) {
4322 work_line_->SetRegisterType(this, vregA, *field_type);
4323 } else {
4324 work_line_->SetRegisterTypeWide(this, vregA, *field_type, field_type->HighHalf(&reg_types_));
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004325 }
4326 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004327 LOG(FATAL) << "Unexpected case.";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004328 }
4329}
4330
Ian Rogers776ac1f2012-04-13 23:36:36 -07004331bool MethodVerifier::CheckNotMoveException(const uint16_t* insns, int insn_idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004332 if ((insns[insn_idx] & 0xff) == Instruction::MOVE_EXCEPTION) {
jeffhaod5347e02012-03-22 17:25:05 -07004333 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-exception";
Ian Rogersd81871c2011-10-03 13:57:23 -07004334 return false;
4335 }
4336 return true;
4337}
4338
Stephen Kyle9bc61992014-09-22 13:53:15 +01004339bool MethodVerifier::CheckNotMoveResult(const uint16_t* insns, int insn_idx) {
4340 if (((insns[insn_idx] & 0xff) >= Instruction::MOVE_RESULT) &&
4341 ((insns[insn_idx] & 0xff) <= Instruction::MOVE_RESULT_OBJECT)) {
4342 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-result*";
4343 return false;
4344 }
4345 return true;
4346}
4347
4348bool MethodVerifier::CheckNotMoveExceptionOrMoveResult(const uint16_t* insns, int insn_idx) {
4349 return (CheckNotMoveException(insns, insn_idx) && CheckNotMoveResult(insns, insn_idx));
4350}
4351
Ian Rogersebbdd872014-07-07 23:53:08 -07004352bool MethodVerifier::UpdateRegisters(uint32_t next_insn, RegisterLine* merge_line,
4353 bool update_merge_line) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004354 bool changed = true;
4355 RegisterLine* target_line = reg_table_.GetLine(next_insn);
4356 if (!insn_flags_[next_insn].IsVisitedOrChanged()) {
jeffhaobdb76512011-09-07 11:43:16 -07004357 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07004358 * We haven't processed this instruction before, and we haven't touched the registers here, so
4359 * there's nothing to "merge". Copy the registers over and mark it as changed. (This is the
4360 * only way a register can transition out of "unknown", so this is not just an optimization.)
jeffhaobdb76512011-09-07 11:43:16 -07004361 */
Ian Rogersb8c78592013-07-25 23:52:52 +00004362 if (!insn_flags_[next_insn].IsReturn()) {
4363 target_line->CopyFromLine(merge_line);
4364 } else {
Jeff Haob24b4a72013-07-31 13:47:31 -07004365 // Verify that the monitor stack is empty on return.
Ian Rogers7b078e82014-09-10 14:44:24 -07004366 if (!merge_line->VerifyMonitorStackEmpty(this)) {
Jeff Haob24b4a72013-07-31 13:47:31 -07004367 return false;
4368 }
Ian Rogersb8c78592013-07-25 23:52:52 +00004369 // For returns we only care about the operand to the return, all other registers are dead.
4370 // Initialize them as conflicts so they don't add to GC and deoptimization information.
4371 const Instruction* ret_inst = Instruction::At(code_item_->insns_ + next_insn);
4372 Instruction::Code opcode = ret_inst->Opcode();
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07004373 if (opcode == Instruction::RETURN_VOID || opcode == Instruction::RETURN_VOID_NO_BARRIER) {
Stephen Kyle7e541c92014-12-17 17:10:02 +00004374 SafelyMarkAllRegistersAsConflicts(this, target_line);
Ian Rogersb8c78592013-07-25 23:52:52 +00004375 } else {
4376 target_line->CopyFromLine(merge_line);
4377 if (opcode == Instruction::RETURN_WIDE) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004378 target_line->MarkAllRegistersAsConflictsExceptWide(this, ret_inst->VRegA_11x());
Ian Rogersb8c78592013-07-25 23:52:52 +00004379 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004380 target_line->MarkAllRegistersAsConflictsExcept(this, ret_inst->VRegA_11x());
Ian Rogersb8c78592013-07-25 23:52:52 +00004381 }
4382 }
4383 }
jeffhaobdb76512011-09-07 11:43:16 -07004384 } else {
Ian Rogers700a4022014-05-19 16:49:03 -07004385 std::unique_ptr<RegisterLine> copy(gDebugVerify ?
Ian Rogersd0fbd852013-09-24 18:17:04 -07004386 RegisterLine::Create(target_line->NumRegs(), this) :
Ian Rogers7b078e82014-09-10 14:44:24 -07004387 nullptr);
Ian Rogers7b0c5b42012-02-16 15:29:07 -08004388 if (gDebugVerify) {
4389 copy->CopyFromLine(target_line);
4390 }
Ian Rogers7b078e82014-09-10 14:44:24 -07004391 changed = target_line->MergeRegisters(this, merge_line);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004392 if (have_pending_hard_failure_) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004393 return false;
jeffhaobdb76512011-09-07 11:43:16 -07004394 }
Ian Rogers2c8a8572011-10-24 17:11:36 -07004395 if (gDebugVerify && changed) {
Elliott Hughes398f64b2012-03-26 18:05:48 -07004396 LogVerifyInfo() << "Merging at [" << reinterpret_cast<void*>(work_insn_idx_) << "]"
Elliott Hughesc073b072012-05-24 19:29:17 -07004397 << " to [" << reinterpret_cast<void*>(next_insn) << "]: " << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07004398 << copy->Dump(this) << " MERGE\n"
4399 << merge_line->Dump(this) << " ==\n"
4400 << target_line->Dump(this) << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07004401 }
Ian Rogersebbdd872014-07-07 23:53:08 -07004402 if (update_merge_line && changed) {
4403 merge_line->CopyFromLine(target_line);
4404 }
jeffhaobdb76512011-09-07 11:43:16 -07004405 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004406 if (changed) {
4407 insn_flags_[next_insn].SetChanged();
jeffhaobdb76512011-09-07 11:43:16 -07004408 }
4409 return true;
4410}
4411
Ian Rogers7b3ddd22013-02-21 15:19:52 -08004412InstructionFlags* MethodVerifier::CurrentInsnFlags() {
Ian Rogers776ac1f2012-04-13 23:36:36 -07004413 return &insn_flags_[work_insn_idx_];
4414}
4415
Ian Rogersd8f69b02014-09-10 21:43:52 +00004416const RegType& MethodVerifier::GetMethodReturnType() {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004417 if (return_type_ == nullptr) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07004418 if (mirror_method_ != nullptr) {
Ian Rogersded66a02014-10-28 18:12:55 -07004419 mirror::Class* return_type_class = mirror_method_->GetReturnType(can_load_classes_);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004420 if (return_type_class != nullptr) {
Andreas Gampef23f33d2015-06-23 14:18:17 -07004421 return_type_ = &FromClass(mirror_method_->GetReturnTypeDescriptor(),
4422 return_type_class,
4423 return_type_class->CannotBeAssignedFromOtherTypes());
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004424 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004425 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
4426 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004427 }
4428 }
4429 if (return_type_ == nullptr) {
4430 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
4431 const DexFile::ProtoId& proto_id = dex_file_->GetMethodPrototype(method_id);
4432 uint16_t return_type_idx = proto_id.return_type_idx_;
4433 const char* descriptor = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(return_type_idx));
Mathieu Chartierbf99f772014-08-23 16:37:27 -07004434 return_type_ = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004435 }
4436 }
4437 return *return_type_;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004438}
4439
Ian Rogersd8f69b02014-09-10 21:43:52 +00004440const RegType& MethodVerifier::GetDeclaringClass() {
Ian Rogers7b078e82014-09-10 14:44:24 -07004441 if (declaring_class_ == nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004442 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
Brian Carlstrom93c33962013-07-26 10:37:43 -07004443 const char* descriptor
4444 = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(method_id.class_idx_));
Mathieu Chartiere401d142015-04-22 13:56:20 -07004445 if (mirror_method_ != nullptr) {
Ian Rogers637c65b2013-05-31 11:46:00 -07004446 mirror::Class* klass = mirror_method_->GetDeclaringClass();
Andreas Gampef23f33d2015-06-23 14:18:17 -07004447 declaring_class_ = &FromClass(descriptor, klass,
4448 klass->CannotBeAssignedFromOtherTypes());
Ian Rogers637c65b2013-05-31 11:46:00 -07004449 } else {
Mathieu Chartierbf99f772014-08-23 16:37:27 -07004450 declaring_class_ = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers637c65b2013-05-31 11:46:00 -07004451 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07004452 }
Ian Rogers637c65b2013-05-31 11:46:00 -07004453 return *declaring_class_;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004454}
4455
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004456std::vector<int32_t> MethodVerifier::DescribeVRegs(uint32_t dex_pc) {
4457 RegisterLine* line = reg_table_.GetLine(dex_pc);
Sebastien Hertzaa0c00c2014-03-14 17:58:54 +01004458 DCHECK(line != nullptr) << "No register line at DEX pc " << StringPrintf("0x%x", dex_pc);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004459 std::vector<int32_t> result;
4460 for (size_t i = 0; i < line->NumRegs(); ++i) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004461 const RegType& type = line->GetRegisterType(this, i);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004462 if (type.IsConstant()) {
4463 result.push_back(type.IsPreciseConstant() ? kConstant : kImpreciseConstant);
Ian Rogers7b078e82014-09-10 14:44:24 -07004464 const ConstantType* const_val = down_cast<const ConstantType*>(&type);
4465 result.push_back(const_val->ConstantValue());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004466 } else if (type.IsConstantLo()) {
4467 result.push_back(type.IsPreciseConstantLo() ? kConstant : kImpreciseConstant);
Ian Rogers7b078e82014-09-10 14:44:24 -07004468 const ConstantType* const_val = down_cast<const ConstantType*>(&type);
4469 result.push_back(const_val->ConstantValueLo());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004470 } else if (type.IsConstantHi()) {
4471 result.push_back(type.IsPreciseConstantHi() ? kConstant : kImpreciseConstant);
Ian Rogers7b078e82014-09-10 14:44:24 -07004472 const ConstantType* const_val = down_cast<const ConstantType*>(&type);
4473 result.push_back(const_val->ConstantValueHi());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004474 } else if (type.IsIntegralTypes()) {
4475 result.push_back(kIntVReg);
4476 result.push_back(0);
4477 } else if (type.IsFloat()) {
4478 result.push_back(kFloatVReg);
4479 result.push_back(0);
4480 } else if (type.IsLong()) {
4481 result.push_back(kLongLoVReg);
4482 result.push_back(0);
4483 result.push_back(kLongHiVReg);
4484 result.push_back(0);
4485 ++i;
4486 } else if (type.IsDouble()) {
4487 result.push_back(kDoubleLoVReg);
4488 result.push_back(0);
4489 result.push_back(kDoubleHiVReg);
4490 result.push_back(0);
4491 ++i;
4492 } else if (type.IsUndefined() || type.IsConflict() || type.IsHighHalf()) {
4493 result.push_back(kUndefined);
4494 result.push_back(0);
4495 } else {
Ian Rogers7b3ddd22013-02-21 15:19:52 -08004496 CHECK(type.IsNonZeroReferenceTypes());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004497 result.push_back(kReferenceVReg);
4498 result.push_back(0);
4499 }
4500 }
4501 return result;
4502}
4503
Ian Rogersd8f69b02014-09-10 21:43:52 +00004504const RegType& MethodVerifier::DetermineCat1Constant(int32_t value, bool precise) {
Sebastien Hertz849600b2013-12-20 10:28:08 +01004505 if (precise) {
4506 // Precise constant type.
4507 return reg_types_.FromCat1Const(value, true);
4508 } else {
4509 // Imprecise constant type.
4510 if (value < -32768) {
4511 return reg_types_.IntConstant();
4512 } else if (value < -128) {
4513 return reg_types_.ShortConstant();
4514 } else if (value < 0) {
4515 return reg_types_.ByteConstant();
4516 } else if (value == 0) {
4517 return reg_types_.Zero();
4518 } else if (value == 1) {
4519 return reg_types_.One();
4520 } else if (value < 128) {
4521 return reg_types_.PosByteConstant();
4522 } else if (value < 32768) {
4523 return reg_types_.PosShortConstant();
4524 } else if (value < 65536) {
4525 return reg_types_.CharConstant();
4526 } else {
4527 return reg_types_.IntConstant();
4528 }
4529 }
4530}
4531
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004532void MethodVerifier::Init() {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08004533 art::verifier::RegTypeCache::Init();
Brian Carlstrome7d856b2012-01-11 18:10:55 -08004534}
4535
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004536void MethodVerifier::Shutdown() {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08004537 verifier::RegTypeCache::ShutDown();
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08004538}
jeffhaod1224c72012-02-29 13:43:08 -08004539
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -07004540void MethodVerifier::VisitStaticRoots(RootVisitor* visitor) {
4541 RegTypeCache::VisitStaticRoots(visitor);
Mathieu Chartier7c438b12014-09-12 17:01:24 -07004542}
4543
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -07004544void MethodVerifier::VisitRoots(RootVisitor* visitor, const RootInfo& root_info) {
4545 reg_types_.VisitRoots(visitor, root_info);
Mathieu Chartierc528dba2013-11-26 12:00:11 -08004546}
4547
Andreas Gampef23f33d2015-06-23 14:18:17 -07004548const RegType& MethodVerifier::FromClass(const char* descriptor,
4549 mirror::Class* klass,
4550 bool precise) {
4551 DCHECK(klass != nullptr);
4552 if (precise && !klass->IsInstantiable() && !klass->IsPrimitive()) {
4553 Fail(VerifyError::VERIFY_ERROR_NO_CLASS) << "Could not create precise reference for "
4554 << "non-instantiable klass " << descriptor;
4555 precise = false;
4556 }
4557 return reg_types_.FromClass(descriptor, klass, precise);
4558}
4559
Ian Rogersd81871c2011-10-03 13:57:23 -07004560} // namespace verifier
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07004561} // namespace art