blob: 3c808ded9d436c894ea34e24629948c56920e998 [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070016
Mathieu Chartierc645f1d2014-03-06 18:11:53 -080017#include "method_verifier-inl.h"
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070018
Elliott Hughes1f359b02011-07-17 14:27:17 -070019#include <iostream>
20
Mathieu Chartierc7853442015-03-27 14:35:38 -070021#include "art_field-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070022#include "art_method-inl.h"
Elliott Hughes07ed66b2012-12-12 18:34:25 -080023#include "base/logging.h"
Ian Rogers637c65b2013-05-31 11:46:00 -070024#include "base/mutex-inl.h"
Vladimir Marko80afd022015-05-19 18:08:00 +010025#include "base/time_utils.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070026#include "class_linker.h"
Vladimir Marko2b5eaa22013-12-13 13:59:30 +000027#include "compiler_callbacks.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070028#include "dex_file-inl.h"
Ian Rogersd0583802013-06-01 10:51:46 -070029#include "dex_instruction-inl.h"
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080030#include "dex_instruction_utils.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070031#include "dex_instruction_visitor.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070032#include "gc/accounting/card_table-inl.h"
Ian Rogers2bcb4a42012-11-08 10:39:18 -080033#include "indenter.h"
Ian Rogers84fa0742011-10-25 18:13:30 -070034#include "intern_table.h"
Ian Rogers0571d352011-11-03 19:51:38 -070035#include "leb128.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080036#include "mirror/class.h"
37#include "mirror/class-inl.h"
Ian Rogers39ebcb82013-05-30 16:57:23 -070038#include "mirror/dex_cache-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080039#include "mirror/object-inl.h"
40#include "mirror/object_array-inl.h"
Ian Rogers7b078e82014-09-10 14:44:24 -070041#include "reg_type-inl.h"
Ian Rogers39ebcb82013-05-30 16:57:23 -070042#include "register_line-inl.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070043#include "runtime.h"
Mathieu Chartier590fee92013-09-13 13:46:47 -070044#include "scoped_thread_state_change.h"
Vladimir Marko80afd022015-05-19 18:08:00 +010045#include "utils.h"
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070046#include "handle_scope-inl.h"
Elliott Hughese222ee02012-12-13 14:41:43 -080047#include "verifier/dex_gc_map.h"
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070048
49namespace art {
Ian Rogersd81871c2011-10-03 13:57:23 -070050namespace verifier {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070051
Mathieu Chartier8e219ae2014-08-19 14:29:46 -070052static constexpr bool kTimeVerifyMethod = !kIsDebugBuild;
Ian Rogersebbdd872014-07-07 23:53:08 -070053static constexpr bool gDebugVerify = false;
Anwar Ghuloum75a43f12013-08-13 17:22:14 -070054// TODO: Add a constant to method_verifier to turn on verbose logging?
Ian Rogers2c8a8572011-10-24 17:11:36 -070055
Ian Rogers7b3ddd22013-02-21 15:19:52 -080056void PcToRegisterLineTable::Init(RegisterTrackingMode mode, InstructionFlags* flags,
Ian Rogersd81871c2011-10-03 13:57:23 -070057 uint32_t insns_size, uint16_t registers_size,
Ian Rogers776ac1f2012-04-13 23:36:36 -070058 MethodVerifier* verifier) {
Ian Rogersd81871c2011-10-03 13:57:23 -070059 DCHECK_GT(insns_size, 0U);
Ian Rogersd0fbd852013-09-24 18:17:04 -070060 register_lines_.reset(new RegisterLine*[insns_size]());
61 size_ = insns_size;
Ian Rogersd81871c2011-10-03 13:57:23 -070062 for (uint32_t i = 0; i < insns_size; i++) {
63 bool interesting = false;
64 switch (mode) {
65 case kTrackRegsAll:
66 interesting = flags[i].IsOpcode();
67 break;
Sameer Abu Asal02c42232013-04-30 12:09:45 -070068 case kTrackCompilerInterestPoints:
Brian Carlstrom02c8cc62013-07-18 15:54:44 -070069 interesting = flags[i].IsCompileTimeInfoPoint() || flags[i].IsBranchTarget();
Ian Rogersd81871c2011-10-03 13:57:23 -070070 break;
71 case kTrackRegsBranches:
72 interesting = flags[i].IsBranchTarget();
73 break;
74 default:
75 break;
76 }
77 if (interesting) {
Ian Rogersd0fbd852013-09-24 18:17:04 -070078 register_lines_[i] = RegisterLine::Create(registers_size, verifier);
79 }
80 }
81}
82
83PcToRegisterLineTable::~PcToRegisterLineTable() {
84 for (size_t i = 0; i < size_; i++) {
85 delete register_lines_[i];
86 if (kIsDebugBuild) {
87 register_lines_[i] = nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -070088 }
89 }
90}
91
Andreas Gampe7c038102014-10-27 20:08:46 -070092// Note: returns true on failure.
93ALWAYS_INLINE static inline bool FailOrAbort(MethodVerifier* verifier, bool condition,
94 const char* error_msg, uint32_t work_insn_idx) {
95 if (kIsDebugBuild) {
96 // In a debug build, abort if the error condition is wrong.
97 DCHECK(condition) << error_msg << work_insn_idx;
98 } else {
99 // In a non-debug build, just fail the class.
100 if (!condition) {
101 verifier->Fail(VERIFY_ERROR_BAD_CLASS_HARD) << error_msg << work_insn_idx;
102 return true;
103 }
104 }
105
106 return false;
107}
108
Stephen Kyle7e541c92014-12-17 17:10:02 +0000109static void SafelyMarkAllRegistersAsConflicts(MethodVerifier* verifier, RegisterLine* reg_line) {
110 if (verifier->IsConstructor()) {
111 // Before we mark all regs as conflicts, check that we don't have an uninitialized this.
112 reg_line->CheckConstructorReturn(verifier);
113 }
114 reg_line->MarkAllRegistersAsConflicts(verifier);
115}
116
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800117MethodVerifier::FailureKind MethodVerifier::VerifyMethod(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700118 ArtMethod* method, bool allow_soft_failures, std::string* error ATTRIBUTE_UNUSED) {
119 StackHandleScope<2> hs(Thread::Current());
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800120 mirror::Class* klass = method->GetDeclaringClass();
121 auto h_dex_cache(hs.NewHandle(klass->GetDexCache()));
122 auto h_class_loader(hs.NewHandle(klass->GetClassLoader()));
Mathieu Chartiere401d142015-04-22 13:56:20 -0700123 return VerifyMethod(hs.Self(), method->GetDexMethodIndex(), method->GetDexFile(), h_dex_cache,
124 h_class_loader, klass->GetClassDef(), method->GetCodeItem(), method,
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800125 method->GetAccessFlags(), allow_soft_failures, false);
126}
127
128
Ian Rogers7b078e82014-09-10 14:44:24 -0700129MethodVerifier::FailureKind MethodVerifier::VerifyClass(Thread* self,
130 mirror::Class* klass,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700131 bool allow_soft_failures,
132 std::string* error) {
jeffhaobdb76512011-09-07 11:43:16 -0700133 if (klass->IsVerified()) {
jeffhaof1e6b7c2012-06-05 18:33:30 -0700134 return kNoFailure;
jeffhaobdb76512011-09-07 11:43:16 -0700135 }
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800136 bool early_failure = false;
137 std::string failure_message;
Mathieu Chartierf8322842014-05-16 10:59:25 -0700138 const DexFile& dex_file = klass->GetDexFile();
139 const DexFile::ClassDef* class_def = klass->GetClassDef();
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800140 mirror::Class* super = klass->GetSuperClass();
Ian Rogers1ff3c982014-08-12 02:30:58 -0700141 std::string temp;
Ian Rogers7b078e82014-09-10 14:44:24 -0700142 if (super == nullptr && strcmp("Ljava/lang/Object;", klass->GetDescriptor(&temp)) != 0) {
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800143 early_failure = true;
144 failure_message = " that has no super class";
Ian Rogers7b078e82014-09-10 14:44:24 -0700145 } else if (super != nullptr && super->IsFinal()) {
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800146 early_failure = true;
147 failure_message = " that attempts to sub-class final class " + PrettyDescriptor(super);
Ian Rogers7b078e82014-09-10 14:44:24 -0700148 } else if (class_def == nullptr) {
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800149 early_failure = true;
150 failure_message = " that isn't present in dex file " + dex_file.GetLocation();
151 }
152 if (early_failure) {
153 *error = "Verifier rejected class " + PrettyDescriptor(klass) + failure_message;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800154 if (Runtime::Current()->IsAotCompiler()) {
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800155 ClassReference ref(&dex_file, klass->GetDexClassDefIndex());
Vladimir Marko2b5eaa22013-12-13 13:59:30 +0000156 Runtime::Current()->GetCompilerCallbacks()->ClassRejected(ref);
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800157 }
jeffhaof1e6b7c2012-06-05 18:33:30 -0700158 return kHardFailure;
jeffhaobdb76512011-09-07 11:43:16 -0700159 }
Ian Rogers7b078e82014-09-10 14:44:24 -0700160 StackHandleScope<2> hs(self);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700161 Handle<mirror::DexCache> dex_cache(hs.NewHandle(klass->GetDexCache()));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700162 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(klass->GetClassLoader()));
Mathieu Chartiere401d142015-04-22 13:56:20 -0700163 return VerifyClass(
164 self, &dex_file, dex_cache, class_loader, class_def, allow_soft_failures, error);
Shih-wei Liao371814f2011-10-27 16:52:10 -0700165}
166
Ian Rogers7b078e82014-09-10 14:44:24 -0700167MethodVerifier::FailureKind MethodVerifier::VerifyClass(Thread* self,
168 const DexFile* dex_file,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700169 Handle<mirror::DexCache> dex_cache,
170 Handle<mirror::ClassLoader> class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700171 const DexFile::ClassDef* class_def,
172 bool allow_soft_failures,
173 std::string* error) {
174 DCHECK(class_def != nullptr);
Andreas Gampe507cc6f2015-06-19 22:58:47 -0700175
176 // A class must not be abstract and final.
177 if ((class_def->access_flags_ & (kAccAbstract | kAccFinal)) == (kAccAbstract | kAccFinal)) {
178 *error = "Verifier rejected class ";
179 *error += PrettyDescriptor(dex_file->GetClassDescriptor(*class_def));
180 *error += ": class is abstract and final.";
181 return kHardFailure;
182 }
183
Ian Rogers13735952014-10-08 12:43:28 -0700184 const uint8_t* class_data = dex_file->GetClassData(*class_def);
Ian Rogers7b078e82014-09-10 14:44:24 -0700185 if (class_data == nullptr) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700186 // empty class, probably a marker interface
jeffhaof1e6b7c2012-06-05 18:33:30 -0700187 return kNoFailure;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700188 }
jeffhaof56197c2012-03-05 18:01:54 -0800189 ClassDataItemIterator it(*dex_file, class_data);
190 while (it.HasNextStaticField() || it.HasNextInstanceField()) {
191 it.Next();
192 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700193 size_t error_count = 0;
jeffhaof1e6b7c2012-06-05 18:33:30 -0700194 bool hard_fail = false;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700195 ClassLinker* linker = Runtime::Current()->GetClassLinker();
jeffhao9b0b1882012-10-01 16:51:22 -0700196 int64_t previous_direct_method_idx = -1;
jeffhaof56197c2012-03-05 18:01:54 -0800197 while (it.HasNextDirectMethod()) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700198 self->AllowThreadSuspension();
jeffhaof56197c2012-03-05 18:01:54 -0800199 uint32_t method_idx = it.GetMemberIndex();
jeffhao9b0b1882012-10-01 16:51:22 -0700200 if (method_idx == previous_direct_method_idx) {
201 // smali can create dex files with two encoded_methods sharing the same method_idx
202 // http://code.google.com/p/smali/issues/detail?id=119
203 it.Next();
204 continue;
205 }
206 previous_direct_method_idx = method_idx;
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700207 InvokeType type = it.GetMethodInvokeType(*class_def);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700208 ArtMethod* method = linker->ResolveMethod(
209 *dex_file, method_idx, dex_cache, class_loader, nullptr, type);
Ian Rogers7b078e82014-09-10 14:44:24 -0700210 if (method == nullptr) {
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700211 DCHECK(self->IsExceptionPending());
Ian Rogersad0b3a32012-04-16 14:50:24 -0700212 // We couldn't resolve the method, but continue regardless.
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700213 self->ClearException();
Mathieu Chartiere401d142015-04-22 13:56:20 -0700214 } else {
215 DCHECK(method->GetDeclaringClassUnchecked() != nullptr) << type;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700216 }
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700217 StackHandleScope<1> hs(self);
Ian Rogers7b078e82014-09-10 14:44:24 -0700218 MethodVerifier::FailureKind result = VerifyMethod(self,
219 method_idx,
Brian Carlstrom93c33962013-07-26 10:37:43 -0700220 dex_file,
221 dex_cache,
222 class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700223 class_def,
Brian Carlstrom93c33962013-07-26 10:37:43 -0700224 it.GetMethodCodeItem(),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700225 method, it.GetMethodAccessFlags(), allow_soft_failures, false);
jeffhaof1e6b7c2012-06-05 18:33:30 -0700226 if (result != kNoFailure) {
227 if (result == kHardFailure) {
228 hard_fail = true;
229 if (error_count > 0) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700230 *error += "\n";
jeffhaof1e6b7c2012-06-05 18:33:30 -0700231 }
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700232 *error = "Verifier rejected class ";
233 *error += PrettyDescriptor(dex_file->GetClassDescriptor(*class_def));
234 *error += " due to bad method ";
235 *error += PrettyMethod(method_idx, *dex_file);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700236 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700237 ++error_count;
jeffhaof56197c2012-03-05 18:01:54 -0800238 }
239 it.Next();
240 }
jeffhao9b0b1882012-10-01 16:51:22 -0700241 int64_t previous_virtual_method_idx = -1;
jeffhaof56197c2012-03-05 18:01:54 -0800242 while (it.HasNextVirtualMethod()) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700243 self->AllowThreadSuspension();
jeffhaof56197c2012-03-05 18:01:54 -0800244 uint32_t method_idx = it.GetMemberIndex();
jeffhao9b0b1882012-10-01 16:51:22 -0700245 if (method_idx == previous_virtual_method_idx) {
246 // smali can create dex files with two encoded_methods sharing the same method_idx
247 // http://code.google.com/p/smali/issues/detail?id=119
248 it.Next();
249 continue;
250 }
251 previous_virtual_method_idx = method_idx;
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700252 InvokeType type = it.GetMethodInvokeType(*class_def);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700253 ArtMethod* method = linker->ResolveMethod(
254 *dex_file, method_idx, dex_cache, class_loader, nullptr, type);
Ian Rogers7b078e82014-09-10 14:44:24 -0700255 if (method == nullptr) {
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700256 DCHECK(self->IsExceptionPending());
Ian Rogersad0b3a32012-04-16 14:50:24 -0700257 // We couldn't resolve the method, but continue regardless.
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700258 self->ClearException();
Ian Rogersad0b3a32012-04-16 14:50:24 -0700259 }
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700260 StackHandleScope<1> hs(self);
Ian Rogers7b078e82014-09-10 14:44:24 -0700261 MethodVerifier::FailureKind result = VerifyMethod(self,
262 method_idx,
Brian Carlstrom93c33962013-07-26 10:37:43 -0700263 dex_file,
264 dex_cache,
265 class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700266 class_def,
Brian Carlstrom93c33962013-07-26 10:37:43 -0700267 it.GetMethodCodeItem(),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700268 method, it.GetMethodAccessFlags(), allow_soft_failures, false);
jeffhaof1e6b7c2012-06-05 18:33:30 -0700269 if (result != kNoFailure) {
270 if (result == kHardFailure) {
271 hard_fail = true;
272 if (error_count > 0) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700273 *error += "\n";
jeffhaof1e6b7c2012-06-05 18:33:30 -0700274 }
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700275 *error = "Verifier rejected class ";
276 *error += PrettyDescriptor(dex_file->GetClassDescriptor(*class_def));
277 *error += " due to bad method ";
278 *error += PrettyMethod(method_idx, *dex_file);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700279 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700280 ++error_count;
jeffhaof56197c2012-03-05 18:01:54 -0800281 }
282 it.Next();
283 }
jeffhaof1e6b7c2012-06-05 18:33:30 -0700284 if (error_count == 0) {
285 return kNoFailure;
286 } else {
287 return hard_fail ? kHardFailure : kSoftFailure;
288 }
jeffhaof56197c2012-03-05 18:01:54 -0800289}
290
Andreas Gampea4f5bf62015-05-18 20:50:12 -0700291static bool IsLargeMethod(const DexFile::CodeItem* const code_item) {
Andreas Gampe3c651fc2015-05-21 14:06:46 -0700292 if (code_item == nullptr) {
293 return false;
294 }
295
Andreas Gampea4f5bf62015-05-18 20:50:12 -0700296 uint16_t registers_size = code_item->registers_size_;
297 uint32_t insns_size = code_item->insns_size_in_code_units_;
298
299 return registers_size * insns_size > 4*1024*1024;
300}
301
Ian Rogers7b078e82014-09-10 14:44:24 -0700302MethodVerifier::FailureKind MethodVerifier::VerifyMethod(Thread* self, uint32_t method_idx,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800303 const DexFile* dex_file,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700304 Handle<mirror::DexCache> dex_cache,
305 Handle<mirror::ClassLoader> class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700306 const DexFile::ClassDef* class_def,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800307 const DexFile::CodeItem* code_item,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700308 ArtMethod* method,
Jeff Haoee988952013-04-16 14:23:47 -0700309 uint32_t method_access_flags,
Ian Rogers46960fe2014-05-23 10:43:43 -0700310 bool allow_soft_failures,
311 bool need_precise_constants) {
Ian Rogersc8982582012-09-07 16:53:25 -0700312 MethodVerifier::FailureKind result = kNoFailure;
Mathieu Chartier8e219ae2014-08-19 14:29:46 -0700313 uint64_t start_ns = kTimeVerifyMethod ? NanoTime() : 0;
Ian Rogersc8982582012-09-07 16:53:25 -0700314
Ian Rogers7b078e82014-09-10 14:44:24 -0700315 MethodVerifier verifier(self, dex_file, dex_cache, class_loader, class_def, code_item,
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700316 method_idx, method, method_access_flags, true, allow_soft_failures,
Mathieu Chartier4306ef82014-12-19 18:41:47 -0800317 need_precise_constants, true);
Ian Rogers46960fe2014-05-23 10:43:43 -0700318 if (verifier.Verify()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700319 // Verification completed, however failures may be pending that didn't cause the verification
320 // to hard fail.
Ian Rogers46960fe2014-05-23 10:43:43 -0700321 CHECK(!verifier.have_pending_hard_failure_);
322 if (verifier.failures_.size() != 0) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -0700323 if (VLOG_IS_ON(verifier)) {
Ian Rogers46960fe2014-05-23 10:43:43 -0700324 verifier.DumpFailures(VLOG_STREAM(verifier) << "Soft verification failures in "
Anwar Ghuloum75a43f12013-08-13 17:22:14 -0700325 << PrettyMethod(method_idx, *dex_file) << "\n");
326 }
Ian Rogersc8982582012-09-07 16:53:25 -0700327 result = kSoftFailure;
jeffhaof56197c2012-03-05 18:01:54 -0800328 }
329 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700330 // Bad method data.
Ian Rogers46960fe2014-05-23 10:43:43 -0700331 CHECK_NE(verifier.failures_.size(), 0U);
332 CHECK(verifier.have_pending_hard_failure_);
333 verifier.DumpFailures(LOG(INFO) << "Verification error in "
Elliott Hughesc073b072012-05-24 19:29:17 -0700334 << PrettyMethod(method_idx, *dex_file) << "\n");
jeffhaof56197c2012-03-05 18:01:54 -0800335 if (gDebugVerify) {
Ian Rogers46960fe2014-05-23 10:43:43 -0700336 std::cout << "\n" << verifier.info_messages_.str();
337 verifier.Dump(std::cout);
jeffhaof56197c2012-03-05 18:01:54 -0800338 }
Ian Rogersc8982582012-09-07 16:53:25 -0700339 result = kHardFailure;
jeffhaof56197c2012-03-05 18:01:54 -0800340 }
Mathieu Chartier8e219ae2014-08-19 14:29:46 -0700341 if (kTimeVerifyMethod) {
342 uint64_t duration_ns = NanoTime() - start_ns;
343 if (duration_ns > MsToNs(100)) {
344 LOG(WARNING) << "Verification of " << PrettyMethod(method_idx, *dex_file)
Andreas Gampea4f5bf62015-05-18 20:50:12 -0700345 << " took " << PrettyDuration(duration_ns)
346 << (IsLargeMethod(code_item) ? " (large method)" : "");
Mathieu Chartier8e219ae2014-08-19 14:29:46 -0700347 }
Ian Rogersc8982582012-09-07 16:53:25 -0700348 }
349 return result;
jeffhaof56197c2012-03-05 18:01:54 -0800350}
351
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700352MethodVerifier* MethodVerifier::VerifyMethodAndDump(Thread* self, std::ostream& os, uint32_t dex_method_idx,
Mathieu Chartier590fee92013-09-13 13:46:47 -0700353 const DexFile* dex_file,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700354 Handle<mirror::DexCache> dex_cache,
355 Handle<mirror::ClassLoader> class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700356 const DexFile::ClassDef* class_def,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800357 const DexFile::CodeItem* code_item,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700358 ArtMethod* method,
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800359 uint32_t method_access_flags) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700360 MethodVerifier* verifier = new MethodVerifier(self, dex_file, dex_cache, class_loader,
361 class_def, code_item, dex_method_idx, method,
362 method_access_flags, true, true, true, true);
363 verifier->Verify();
364 verifier->DumpFailures(os);
365 os << verifier->info_messages_.str();
Andreas Gampe5cbcde22014-09-16 14:59:49 -0700366 // Only dump and return if no hard failures. Otherwise the verifier may be not fully initialized
367 // and querying any info is dangerous/can abort.
368 if (verifier->have_pending_hard_failure_) {
369 delete verifier;
370 return nullptr;
371 } else {
372 verifier->Dump(os);
373 return verifier;
374 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800375}
376
Ian Rogers7b078e82014-09-10 14:44:24 -0700377MethodVerifier::MethodVerifier(Thread* self,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700378 const DexFile* dex_file, Handle<mirror::DexCache> dex_cache,
379 Handle<mirror::ClassLoader> class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700380 const DexFile::ClassDef* class_def,
Mathieu Chartier590fee92013-09-13 13:46:47 -0700381 const DexFile::CodeItem* code_item, uint32_t dex_method_idx,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700382 ArtMethod* method, uint32_t method_access_flags,
Ian Rogers46960fe2014-05-23 10:43:43 -0700383 bool can_load_classes, bool allow_soft_failures,
Mathieu Chartier4306ef82014-12-19 18:41:47 -0800384 bool need_precise_constants, bool verify_to_dump,
385 bool allow_thread_suspension)
Ian Rogers7b078e82014-09-10 14:44:24 -0700386 : self_(self),
387 reg_types_(can_load_classes),
Elliott Hughes80537bb2013-01-04 16:37:26 -0800388 work_insn_idx_(-1),
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800389 dex_method_idx_(dex_method_idx),
Ian Rogers637c65b2013-05-31 11:46:00 -0700390 mirror_method_(method),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700391 method_access_flags_(method_access_flags),
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700392 return_type_(nullptr),
jeffhaof56197c2012-03-05 18:01:54 -0800393 dex_file_(dex_file),
394 dex_cache_(dex_cache),
395 class_loader_(class_loader),
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700396 class_def_(class_def),
jeffhaof56197c2012-03-05 18:01:54 -0800397 code_item_(code_item),
Ian Rogers7b078e82014-09-10 14:44:24 -0700398 declaring_class_(nullptr),
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700399 interesting_dex_pc_(-1),
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700400 monitor_enter_dex_pcs_(nullptr),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700401 have_pending_hard_failure_(false),
jeffhaofaf459e2012-08-31 15:32:47 -0700402 have_pending_runtime_throw_failure_(false),
jeffhaof56197c2012-03-05 18:01:54 -0800403 new_instance_count_(0),
Elliott Hughes80537bb2013-01-04 16:37:26 -0800404 monitor_enter_count_(0),
Jeff Haoee988952013-04-16 14:23:47 -0700405 can_load_classes_(can_load_classes),
Sebastien Hertz4d4adb12013-07-24 16:14:19 +0200406 allow_soft_failures_(allow_soft_failures),
Ian Rogers46960fe2014-05-23 10:43:43 -0700407 need_precise_constants_(need_precise_constants),
Sebastien Hertz4d4adb12013-07-24 16:14:19 +0200408 has_check_casts_(false),
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700409 has_virtual_or_interface_invokes_(false),
Mathieu Chartier4306ef82014-12-19 18:41:47 -0800410 verify_to_dump_(verify_to_dump),
411 allow_thread_suspension_(allow_thread_suspension) {
Mathieu Chartierd0ad2ee2015-03-31 14:59:59 -0700412 self->PushVerifier(this);
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700413 DCHECK(class_def != nullptr);
jeffhaof56197c2012-03-05 18:01:54 -0800414}
415
Mathieu Chartier590fee92013-09-13 13:46:47 -0700416MethodVerifier::~MethodVerifier() {
Mathieu Chartierd0ad2ee2015-03-31 14:59:59 -0700417 Thread::Current()->PopVerifier(this);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700418 STLDeleteElements(&failure_messages_);
419}
420
Mathieu Chartiere401d142015-04-22 13:56:20 -0700421void MethodVerifier::FindLocksAtDexPc(ArtMethod* m, uint32_t dex_pc,
Ian Rogers46960fe2014-05-23 10:43:43 -0700422 std::vector<uint32_t>* monitor_enter_dex_pcs) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700423 StackHandleScope<2> hs(Thread::Current());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700424 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache()));
425 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader()));
Mathieu Chartiere401d142015-04-22 13:56:20 -0700426 MethodVerifier verifier(hs.Self(), m->GetDexFile(), dex_cache, class_loader, &m->GetClassDef(),
427 m->GetCodeItem(), m->GetDexMethodIndex(), m, m->GetAccessFlags(),
Mathieu Chartier4306ef82014-12-19 18:41:47 -0800428 false, true, false, false);
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700429 verifier.interesting_dex_pc_ = dex_pc;
Ian Rogers46960fe2014-05-23 10:43:43 -0700430 verifier.monitor_enter_dex_pcs_ = monitor_enter_dex_pcs;
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700431 verifier.FindLocksAtDexPc();
432}
433
Andreas Gampecb3c08f2014-09-18 13:16:38 -0700434static bool HasMonitorEnterInstructions(const DexFile::CodeItem* const code_item) {
435 const Instruction* inst = Instruction::At(code_item->insns_);
436
437 uint32_t insns_size = code_item->insns_size_in_code_units_;
438 for (uint32_t dex_pc = 0; dex_pc < insns_size;) {
439 if (inst->Opcode() == Instruction::MONITOR_ENTER) {
440 return true;
441 }
442
443 dex_pc += inst->SizeInCodeUnits();
444 inst = inst->Next();
445 }
446
447 return false;
448}
449
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700450void MethodVerifier::FindLocksAtDexPc() {
Ian Rogers7b078e82014-09-10 14:44:24 -0700451 CHECK(monitor_enter_dex_pcs_ != nullptr);
452 CHECK(code_item_ != nullptr); // This only makes sense for methods with code.
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700453
Andreas Gampecb3c08f2014-09-18 13:16:38 -0700454 // Quick check whether there are any monitor_enter instructions at all.
455 if (!HasMonitorEnterInstructions(code_item_)) {
456 return;
457 }
458
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700459 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
460 // verification. In practice, the phase we want relies on data structures set up by all the
461 // earlier passes, so we just run the full method verification and bail out early when we've
462 // got what we wanted.
463 Verify();
464}
465
Mathieu Chartiere401d142015-04-22 13:56:20 -0700466ArtField* MethodVerifier::FindAccessedFieldAtDexPc(ArtMethod* m, uint32_t dex_pc) {
467 StackHandleScope<2> hs(Thread::Current());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700468 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache()));
469 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader()));
Mathieu Chartiere401d142015-04-22 13:56:20 -0700470 MethodVerifier verifier(hs.Self(), m->GetDexFile(), dex_cache, class_loader, &m->GetClassDef(),
471 m->GetCodeItem(), m->GetDexMethodIndex(), m, m->GetAccessFlags(), true,
472 true, false, true);
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200473 return verifier.FindAccessedFieldAtDexPc(dex_pc);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200474}
475
Mathieu Chartierc7853442015-03-27 14:35:38 -0700476ArtField* MethodVerifier::FindAccessedFieldAtDexPc(uint32_t dex_pc) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700477 CHECK(code_item_ != nullptr); // This only makes sense for methods with code.
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200478
479 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
480 // verification. In practice, the phase we want relies on data structures set up by all the
481 // earlier passes, so we just run the full method verification and bail out early when we've
482 // got what we wanted.
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200483 bool success = Verify();
484 if (!success) {
Ian Rogers9bc54402014-04-17 16:40:01 -0700485 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200486 }
487 RegisterLine* register_line = reg_table_.GetLine(dex_pc);
Ian Rogers7b078e82014-09-10 14:44:24 -0700488 if (register_line == nullptr) {
Ian Rogers9bc54402014-04-17 16:40:01 -0700489 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200490 }
491 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
492 return GetQuickFieldAccess(inst, register_line);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200493}
494
Mathieu Chartiere401d142015-04-22 13:56:20 -0700495ArtMethod* MethodVerifier::FindInvokedMethodAtDexPc(ArtMethod* m, uint32_t dex_pc) {
496 StackHandleScope<2> hs(Thread::Current());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700497 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache()));
498 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader()));
Mathieu Chartiere401d142015-04-22 13:56:20 -0700499 MethodVerifier verifier(hs.Self(), m->GetDexFile(), dex_cache, class_loader, &m->GetClassDef(),
500 m->GetCodeItem(), m->GetDexMethodIndex(), m, m->GetAccessFlags(), true,
501 true, false, true);
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200502 return verifier.FindInvokedMethodAtDexPc(dex_pc);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200503}
504
Mathieu Chartiere401d142015-04-22 13:56:20 -0700505ArtMethod* MethodVerifier::FindInvokedMethodAtDexPc(uint32_t dex_pc) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700506 CHECK(code_item_ != nullptr); // This only makes sense for methods with code.
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200507
508 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
509 // verification. In practice, the phase we want relies on data structures set up by all the
510 // earlier passes, so we just run the full method verification and bail out early when we've
511 // got what we wanted.
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200512 bool success = Verify();
513 if (!success) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700514 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200515 }
516 RegisterLine* register_line = reg_table_.GetLine(dex_pc);
Ian Rogers7b078e82014-09-10 14:44:24 -0700517 if (register_line == nullptr) {
518 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200519 }
520 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
521 const bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
Mathieu Chartier091d2382015-03-06 10:59:06 -0800522 return GetQuickInvokedMethod(inst, register_line, is_range, false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200523}
524
Mathieu Chartiere401d142015-04-22 13:56:20 -0700525SafeMap<uint32_t, std::set<uint32_t>> MethodVerifier::FindStringInitMap(ArtMethod* m) {
Jeff Hao848f70a2014-01-15 13:49:50 -0800526 Thread* self = Thread::Current();
Mathieu Chartiere401d142015-04-22 13:56:20 -0700527 StackHandleScope<2> hs(self);
Jeff Hao848f70a2014-01-15 13:49:50 -0800528 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache()));
529 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader()));
Jeff Hao848f70a2014-01-15 13:49:50 -0800530 MethodVerifier verifier(self, m->GetDexFile(), dex_cache, class_loader, &m->GetClassDef(),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700531 m->GetCodeItem(), m->GetDexMethodIndex(), m, m->GetAccessFlags(),
Jeff Hao848f70a2014-01-15 13:49:50 -0800532 true, true, false, true);
533 return verifier.FindStringInitMap();
534}
535
536SafeMap<uint32_t, std::set<uint32_t>>& MethodVerifier::FindStringInitMap() {
537 Verify();
538 return GetStringInitPcRegMap();
539}
540
Ian Rogersad0b3a32012-04-16 14:50:24 -0700541bool MethodVerifier::Verify() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700542 // If there aren't any instructions, make sure that's expected, then exit successfully.
Ian Rogers7b078e82014-09-10 14:44:24 -0700543 if (code_item_ == nullptr) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700544 if ((method_access_flags_ & (kAccNative | kAccAbstract)) == 0) {
jeffhaod5347e02012-03-22 17:25:05 -0700545 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "zero-length code in concrete non-native method";
jeffhaobdb76512011-09-07 11:43:16 -0700546 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -0700547 } else {
548 return true;
jeffhaobdb76512011-09-07 11:43:16 -0700549 }
jeffhaobdb76512011-09-07 11:43:16 -0700550 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700551 // Sanity-check the register counts. ins + locals = registers, so make sure that ins <= registers.
552 if (code_item_->ins_size_ > code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700553 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad register counts (ins=" << code_item_->ins_size_
554 << " regs=" << code_item_->registers_size_;
Ian Rogersd81871c2011-10-03 13:57:23 -0700555 return false;
jeffhaobdb76512011-09-07 11:43:16 -0700556 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700557 // Allocate and initialize an array to hold instruction data.
Ian Rogers7b3ddd22013-02-21 15:19:52 -0800558 insn_flags_.reset(new InstructionFlags[code_item_->insns_size_in_code_units_]());
Ian Rogersd81871c2011-10-03 13:57:23 -0700559 // Run through the instructions and see if the width checks out.
560 bool result = ComputeWidthsAndCountOps();
561 // Flag instructions guarded by a "try" block and check exception handlers.
562 result = result && ScanTryCatchBlocks();
563 // Perform static instruction verification.
564 result = result && VerifyInstructions();
Ian Rogersad0b3a32012-04-16 14:50:24 -0700565 // Perform code-flow analysis and return.
Vladimir Marko2b5eaa22013-12-13 13:59:30 +0000566 result = result && VerifyCodeFlow();
567 // Compute information for compiler.
568 if (result && Runtime::Current()->IsCompiler()) {
569 result = Runtime::Current()->GetCompilerCallbacks()->MethodVerified(this);
570 }
571 return result;
jeffhaoba5ebb92011-08-25 17:24:37 -0700572}
573
Ian Rogers776ac1f2012-04-13 23:36:36 -0700574std::ostream& MethodVerifier::Fail(VerifyError error) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700575 switch (error) {
576 case VERIFY_ERROR_NO_CLASS:
577 case VERIFY_ERROR_NO_FIELD:
578 case VERIFY_ERROR_NO_METHOD:
579 case VERIFY_ERROR_ACCESS_CLASS:
580 case VERIFY_ERROR_ACCESS_FIELD:
581 case VERIFY_ERROR_ACCESS_METHOD:
Ian Rogers08f753d2012-08-24 14:35:25 -0700582 case VERIFY_ERROR_INSTANTIATION:
583 case VERIFY_ERROR_CLASS_CHANGE:
Igor Murashkin158f35c2015-06-10 15:55:30 -0700584 case VERIFY_ERROR_FORCE_INTERPRETER:
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800585 if (Runtime::Current()->IsAotCompiler() || !can_load_classes_) {
jeffhaofaf459e2012-08-31 15:32:47 -0700586 // If we're optimistically running verification at compile time, turn NO_xxx, ACCESS_xxx,
587 // class change and instantiation errors into soft verification errors so that we re-verify
588 // at runtime. We may fail to find or to agree on access because of not yet available class
589 // loaders, or class loaders that will differ at runtime. In these cases, we don't want to
590 // affect the soundness of the code being compiled. Instead, the generated code runs "slow
591 // paths" that dynamically perform the verification and cause the behavior to be that akin
592 // to an interpreter.
593 error = VERIFY_ERROR_BAD_CLASS_SOFT;
594 } else {
Jeff Haoa3faaf42013-09-03 19:07:00 -0700595 // If we fail again at runtime, mark that this instruction would throw and force this
596 // method to be executed using the interpreter with checks.
jeffhaofaf459e2012-08-31 15:32:47 -0700597 have_pending_runtime_throw_failure_ = true;
Andreas Gamped7f8d052015-03-12 11:05:47 -0700598
599 // We need to save the work_line if the instruction wasn't throwing before. Otherwise we'll
600 // try to merge garbage.
601 // Note: this assumes that Fail is called before we do any work_line modifications.
602 const uint16_t* insns = code_item_->insns_ + work_insn_idx_;
603 const Instruction* inst = Instruction::At(insns);
604 int opcode_flags = Instruction::FlagsOf(inst->Opcode());
605
606 if ((opcode_flags & Instruction::kThrow) == 0 && CurrentInsnFlags()->IsInTry()) {
607 saved_line_->CopyFromLine(work_line_.get());
608 }
jeffhaofaf459e2012-08-31 15:32:47 -0700609 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700610 break;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700611 // Indication that verification should be retried at runtime.
612 case VERIFY_ERROR_BAD_CLASS_SOFT:
Jeff Haoee988952013-04-16 14:23:47 -0700613 if (!allow_soft_failures_) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700614 have_pending_hard_failure_ = true;
615 }
616 break;
jeffhaod5347e02012-03-22 17:25:05 -0700617 // Hard verification failures at compile time will still fail at runtime, so the class is
618 // marked as rejected to prevent it from being compiled.
Ian Rogersad0b3a32012-04-16 14:50:24 -0700619 case VERIFY_ERROR_BAD_CLASS_HARD: {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800620 if (Runtime::Current()->IsAotCompiler()) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700621 ClassReference ref(dex_file_, dex_file_->GetIndexForClassDef(*class_def_));
Vladimir Marko2b5eaa22013-12-13 13:59:30 +0000622 Runtime::Current()->GetCompilerCallbacks()->ClassRejected(ref);
jeffhaod1224c72012-02-29 13:43:08 -0800623 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700624 have_pending_hard_failure_ = true;
625 break;
Ian Rogers47a05882012-02-03 12:23:33 -0800626 }
627 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700628 failures_.push_back(error);
Elena Sayapina78480ec2014-08-15 15:52:42 +0700629 std::string location(StringPrintf("%s: [0x%X] ", PrettyMethod(dex_method_idx_, *dex_file_).c_str(),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700630 work_insn_idx_));
Elena Sayapina78480ec2014-08-15 15:52:42 +0700631 std::ostringstream* failure_message = new std::ostringstream(location, std::ostringstream::ate);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700632 failure_messages_.push_back(failure_message);
633 return *failure_message;
634}
635
Ian Rogers576ca0c2014-06-06 15:58:22 -0700636std::ostream& MethodVerifier::LogVerifyInfo() {
637 return info_messages_ << "VFY: " << PrettyMethod(dex_method_idx_, *dex_file_)
638 << '[' << reinterpret_cast<void*>(work_insn_idx_) << "] : ";
639}
640
Ian Rogersad0b3a32012-04-16 14:50:24 -0700641void MethodVerifier::PrependToLastFailMessage(std::string prepend) {
642 size_t failure_num = failure_messages_.size();
643 DCHECK_NE(failure_num, 0U);
644 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
645 prepend += last_fail_message->str();
Elena Sayapina78480ec2014-08-15 15:52:42 +0700646 failure_messages_[failure_num - 1] = new std::ostringstream(prepend, std::ostringstream::ate);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700647 delete last_fail_message;
648}
649
650void MethodVerifier::AppendToLastFailMessage(std::string append) {
651 size_t failure_num = failure_messages_.size();
652 DCHECK_NE(failure_num, 0U);
653 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
654 (*last_fail_message) << append;
Ian Rogers47a05882012-02-03 12:23:33 -0800655}
656
Ian Rogers776ac1f2012-04-13 23:36:36 -0700657bool MethodVerifier::ComputeWidthsAndCountOps() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700658 const uint16_t* insns = code_item_->insns_;
659 size_t insns_size = code_item_->insns_size_in_code_units_;
660 const Instruction* inst = Instruction::At(insns);
jeffhaobdb76512011-09-07 11:43:16 -0700661 size_t new_instance_count = 0;
662 size_t monitor_enter_count = 0;
Ian Rogersd81871c2011-10-03 13:57:23 -0700663 size_t dex_pc = 0;
jeffhaobdb76512011-09-07 11:43:16 -0700664
Ian Rogersd81871c2011-10-03 13:57:23 -0700665 while (dex_pc < insns_size) {
jeffhaobdb76512011-09-07 11:43:16 -0700666 Instruction::Code opcode = inst->Opcode();
Ian Rogersa9a82542013-10-04 11:17:26 -0700667 switch (opcode) {
668 case Instruction::APUT_OBJECT:
669 case Instruction::CHECK_CAST:
670 has_check_casts_ = true;
671 break;
672 case Instruction::INVOKE_VIRTUAL:
673 case Instruction::INVOKE_VIRTUAL_RANGE:
674 case Instruction::INVOKE_INTERFACE:
675 case Instruction::INVOKE_INTERFACE_RANGE:
676 has_virtual_or_interface_invokes_ = true;
677 break;
678 case Instruction::MONITOR_ENTER:
679 monitor_enter_count++;
680 break;
681 case Instruction::NEW_INSTANCE:
682 new_instance_count++;
683 break;
684 default:
685 break;
jeffhaobdb76512011-09-07 11:43:16 -0700686 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700687 size_t inst_size = inst->SizeInCodeUnits();
Ian Rogers7b078e82014-09-10 14:44:24 -0700688 insn_flags_[dex_pc].SetIsOpcode();
Ian Rogersd81871c2011-10-03 13:57:23 -0700689 dex_pc += inst_size;
Ian Rogers7b078e82014-09-10 14:44:24 -0700690 inst = inst->RelativeAt(inst_size);
jeffhaobdb76512011-09-07 11:43:16 -0700691 }
692
Ian Rogersd81871c2011-10-03 13:57:23 -0700693 if (dex_pc != insns_size) {
jeffhaod5347e02012-03-22 17:25:05 -0700694 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "code did not end where expected ("
695 << dex_pc << " vs. " << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700696 return false;
697 }
698
Ian Rogersd81871c2011-10-03 13:57:23 -0700699 new_instance_count_ = new_instance_count;
700 monitor_enter_count_ = monitor_enter_count;
jeffhaobdb76512011-09-07 11:43:16 -0700701 return true;
702}
703
Ian Rogers776ac1f2012-04-13 23:36:36 -0700704bool MethodVerifier::ScanTryCatchBlocks() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700705 uint32_t tries_size = code_item_->tries_size_;
jeffhaobdb76512011-09-07 11:43:16 -0700706 if (tries_size == 0) {
707 return true;
708 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700709 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Ian Rogers0571d352011-11-03 19:51:38 -0700710 const DexFile::TryItem* tries = DexFile::GetTryItems(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -0700711
712 for (uint32_t idx = 0; idx < tries_size; idx++) {
713 const DexFile::TryItem* try_item = &tries[idx];
714 uint32_t start = try_item->start_addr_;
715 uint32_t end = start + try_item->insn_count_;
jeffhaobdb76512011-09-07 11:43:16 -0700716 if ((start >= end) || (start >= insns_size) || (end > insns_size)) {
jeffhaod5347e02012-03-22 17:25:05 -0700717 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad exception entry: startAddr=" << start
718 << " endAddr=" << end << " (size=" << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700719 return false;
720 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700721 if (!insn_flags_[start].IsOpcode()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700722 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
723 << "'try' block starts inside an instruction (" << start << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700724 return false;
725 }
Ian Rogers7b078e82014-09-10 14:44:24 -0700726 uint32_t dex_pc = start;
727 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
728 while (dex_pc < end) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700729 insn_flags_[dex_pc].SetInTry();
Ian Rogers7b078e82014-09-10 14:44:24 -0700730 size_t insn_size = inst->SizeInCodeUnits();
731 dex_pc += insn_size;
732 inst = inst->RelativeAt(insn_size);
jeffhaobdb76512011-09-07 11:43:16 -0700733 }
734 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800735 // Iterate over each of the handlers to verify target addresses.
Ian Rogers13735952014-10-08 12:43:28 -0700736 const uint8_t* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -0700737 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700738 ClassLinker* linker = Runtime::Current()->GetClassLinker();
jeffhaobdb76512011-09-07 11:43:16 -0700739 for (uint32_t idx = 0; idx < handlers_size; idx++) {
Ian Rogers0571d352011-11-03 19:51:38 -0700740 CatchHandlerIterator iterator(handlers_ptr);
741 for (; iterator.HasNext(); iterator.Next()) {
742 uint32_t dex_pc= iterator.GetHandlerAddress();
Ian Rogersd81871c2011-10-03 13:57:23 -0700743 if (!insn_flags_[dex_pc].IsOpcode()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700744 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
745 << "exception handler starts at bad address (" << dex_pc << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700746 return false;
747 }
Stephen Kyle9bc61992014-09-22 13:53:15 +0100748 if (!CheckNotMoveResult(code_item_->insns_, dex_pc)) {
749 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
750 << "exception handler begins with move-result* (" << dex_pc << ")";
751 return false;
752 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700753 insn_flags_[dex_pc].SetBranchTarget();
Ian Rogers28ad40d2011-10-27 15:19:26 -0700754 // Ensure exception types are resolved so that they don't need resolution to be delivered,
755 // unresolved exception types will be ignored by exception delivery
Ian Rogers0571d352011-11-03 19:51:38 -0700756 if (iterator.GetHandlerTypeIndex() != DexFile::kDexNoIndex16) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800757 mirror::Class* exception_type = linker->ResolveType(*dex_file_,
758 iterator.GetHandlerTypeIndex(),
Mathieu Chartierbf99f772014-08-23 16:37:27 -0700759 dex_cache_, class_loader_);
Ian Rogers7b078e82014-09-10 14:44:24 -0700760 if (exception_type == nullptr) {
761 DCHECK(self_->IsExceptionPending());
762 self_->ClearException();
Ian Rogers28ad40d2011-10-27 15:19:26 -0700763 }
764 }
jeffhaobdb76512011-09-07 11:43:16 -0700765 }
Ian Rogers0571d352011-11-03 19:51:38 -0700766 handlers_ptr = iterator.EndDataPointer();
jeffhaobdb76512011-09-07 11:43:16 -0700767 }
jeffhaobdb76512011-09-07 11:43:16 -0700768 return true;
769}
770
Ian Rogers776ac1f2012-04-13 23:36:36 -0700771bool MethodVerifier::VerifyInstructions() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700772 const Instruction* inst = Instruction::At(code_item_->insns_);
jeffhaoba5ebb92011-08-25 17:24:37 -0700773
Ian Rogers0c7abda2012-09-19 13:33:42 -0700774 /* 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 -0700775 insn_flags_[0].SetBranchTarget();
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700776 insn_flags_[0].SetCompileTimeInfoPoint();
Ian Rogersd81871c2011-10-03 13:57:23 -0700777
778 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700779 for (uint32_t dex_pc = 0; dex_pc < insns_size;) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700780 if (!VerifyInstruction(inst, dex_pc)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700781 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -0700782 return false;
783 }
784 /* Flag instructions that are garbage collection points */
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700785 // All invoke points are marked as "Throw" points already.
786 // We are relying on this to also count all the invokes as interesting.
Vladimir Marko8b858e12014-11-27 14:52:37 +0000787 if (inst->IsBranch()) {
788 insn_flags_[dex_pc].SetCompileTimeInfoPoint();
789 // The compiler also needs safepoints for fall-through to loop heads.
790 // Such a loop head must be a target of a branch.
791 int32_t offset = 0;
792 bool cond, self_ok;
793 bool target_ok = GetBranchOffset(dex_pc, &offset, &cond, &self_ok);
794 DCHECK(target_ok);
795 insn_flags_[dex_pc + offset].SetCompileTimeInfoPoint();
796 } else if (inst->IsSwitch() || inst->IsThrow()) {
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700797 insn_flags_[dex_pc].SetCompileTimeInfoPoint();
Ian Rogersb8c78592013-07-25 23:52:52 +0000798 } else if (inst->IsReturn()) {
799 insn_flags_[dex_pc].SetCompileTimeInfoPointAndReturn();
Ian Rogersd81871c2011-10-03 13:57:23 -0700800 }
801 dex_pc += inst->SizeInCodeUnits();
802 inst = inst->Next();
803 }
804 return true;
805}
806
Ian Rogers776ac1f2012-04-13 23:36:36 -0700807bool MethodVerifier::VerifyInstruction(const Instruction* inst, uint32_t code_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700808 bool result = true;
809 switch (inst->GetVerifyTypeArgumentA()) {
810 case Instruction::kVerifyRegA:
Ian Rogers29a26482014-05-02 15:27:29 -0700811 result = result && CheckRegisterIndex(inst->VRegA());
Ian Rogersd81871c2011-10-03 13:57:23 -0700812 break;
813 case Instruction::kVerifyRegAWide:
Ian Rogers29a26482014-05-02 15:27:29 -0700814 result = result && CheckWideRegisterIndex(inst->VRegA());
Ian Rogersd81871c2011-10-03 13:57:23 -0700815 break;
816 }
817 switch (inst->GetVerifyTypeArgumentB()) {
818 case Instruction::kVerifyRegB:
Ian Rogers29a26482014-05-02 15:27:29 -0700819 result = result && CheckRegisterIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700820 break;
821 case Instruction::kVerifyRegBField:
Ian Rogers29a26482014-05-02 15:27:29 -0700822 result = result && CheckFieldIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700823 break;
824 case Instruction::kVerifyRegBMethod:
Ian Rogers29a26482014-05-02 15:27:29 -0700825 result = result && CheckMethodIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700826 break;
827 case Instruction::kVerifyRegBNewInstance:
Ian Rogers29a26482014-05-02 15:27:29 -0700828 result = result && CheckNewInstance(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700829 break;
830 case Instruction::kVerifyRegBString:
Ian Rogers29a26482014-05-02 15:27:29 -0700831 result = result && CheckStringIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700832 break;
833 case Instruction::kVerifyRegBType:
Ian Rogers29a26482014-05-02 15:27:29 -0700834 result = result && CheckTypeIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700835 break;
836 case Instruction::kVerifyRegBWide:
Ian Rogers29a26482014-05-02 15:27:29 -0700837 result = result && CheckWideRegisterIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700838 break;
839 }
840 switch (inst->GetVerifyTypeArgumentC()) {
841 case Instruction::kVerifyRegC:
Ian Rogers29a26482014-05-02 15:27:29 -0700842 result = result && CheckRegisterIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700843 break;
844 case Instruction::kVerifyRegCField:
Ian Rogers29a26482014-05-02 15:27:29 -0700845 result = result && CheckFieldIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700846 break;
847 case Instruction::kVerifyRegCNewArray:
Ian Rogers29a26482014-05-02 15:27:29 -0700848 result = result && CheckNewArray(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700849 break;
850 case Instruction::kVerifyRegCType:
Ian Rogers29a26482014-05-02 15:27:29 -0700851 result = result && CheckTypeIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700852 break;
853 case Instruction::kVerifyRegCWide:
Ian Rogers29a26482014-05-02 15:27:29 -0700854 result = result && CheckWideRegisterIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700855 break;
856 }
857 switch (inst->GetVerifyExtraFlags()) {
858 case Instruction::kVerifyArrayData:
859 result = result && CheckArrayData(code_offset);
860 break;
861 case Instruction::kVerifyBranchTarget:
862 result = result && CheckBranchTarget(code_offset);
863 break;
864 case Instruction::kVerifySwitchTargets:
865 result = result && CheckSwitchTargets(code_offset);
866 break;
Andreas Gampec3314312014-06-19 18:13:29 -0700867 case Instruction::kVerifyVarArgNonZero:
868 // Fall-through.
Ian Rogers29a26482014-05-02 15:27:29 -0700869 case Instruction::kVerifyVarArg: {
Taiju Tsuiki29498a22015-04-13 14:21:00 +0900870 // Instructions that can actually return a negative value shouldn't have this flag.
871 uint32_t v_a = dchecked_integral_cast<uint32_t>(inst->VRegA());
872 if ((inst->GetVerifyExtraFlags() == Instruction::kVerifyVarArgNonZero && v_a == 0) ||
873 v_a > Instruction::kMaxVarArgRegs) {
874 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << v_a << ") in "
Andreas Gampec3314312014-06-19 18:13:29 -0700875 "non-range invoke";
876 return false;
877 }
Taiju Tsuiki29498a22015-04-13 14:21:00 +0900878
Ian Rogers29a26482014-05-02 15:27:29 -0700879 uint32_t args[Instruction::kMaxVarArgRegs];
880 inst->GetVarArgs(args);
Taiju Tsuiki29498a22015-04-13 14:21:00 +0900881 result = result && CheckVarArgRegs(v_a, args);
Ian Rogersd81871c2011-10-03 13:57:23 -0700882 break;
Ian Rogers29a26482014-05-02 15:27:29 -0700883 }
Andreas Gampec3314312014-06-19 18:13:29 -0700884 case Instruction::kVerifyVarArgRangeNonZero:
885 // Fall-through.
Ian Rogersd81871c2011-10-03 13:57:23 -0700886 case Instruction::kVerifyVarArgRange:
Andreas Gampec3314312014-06-19 18:13:29 -0700887 if (inst->GetVerifyExtraFlags() == Instruction::kVerifyVarArgRangeNonZero &&
888 inst->VRegA() <= 0) {
889 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << inst->VRegA() << ") in "
890 "range invoke";
891 return false;
892 }
Ian Rogers29a26482014-05-02 15:27:29 -0700893 result = result && CheckVarArgRangeRegs(inst->VRegA(), inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700894 break;
895 case Instruction::kVerifyError:
jeffhaod5347e02012-03-22 17:25:05 -0700896 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected opcode " << inst->Name();
Ian Rogersd81871c2011-10-03 13:57:23 -0700897 result = false;
898 break;
899 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800900 if (inst->GetVerifyIsRuntimeOnly() && Runtime::Current()->IsAotCompiler() && !verify_to_dump_) {
Ian Rogers5fb22a92014-06-13 10:31:28 -0700901 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "opcode only expected at runtime " << inst->Name();
902 result = false;
903 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700904 return result;
905}
906
Ian Rogers7b078e82014-09-10 14:44:24 -0700907inline bool MethodVerifier::CheckRegisterIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700908 if (idx >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700909 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register index out of range (" << idx << " >= "
910 << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700911 return false;
912 }
913 return true;
914}
915
Ian Rogers7b078e82014-09-10 14:44:24 -0700916inline bool MethodVerifier::CheckWideRegisterIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700917 if (idx + 1 >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700918 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "wide register index out of range (" << idx
919 << "+1 >= " << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700920 return false;
921 }
922 return true;
923}
924
Ian Rogers7b078e82014-09-10 14:44:24 -0700925inline bool MethodVerifier::CheckFieldIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700926 if (idx >= dex_file_->GetHeader().field_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700927 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad field index " << idx << " (max "
928 << dex_file_->GetHeader().field_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700929 return false;
930 }
931 return true;
932}
933
Ian Rogers7b078e82014-09-10 14:44:24 -0700934inline bool MethodVerifier::CheckMethodIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700935 if (idx >= dex_file_->GetHeader().method_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700936 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad method index " << idx << " (max "
937 << dex_file_->GetHeader().method_ids_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::CheckNewInstance(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700944 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700945 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
946 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700947 return false;
948 }
949 // We don't need the actual class, just a pointer to the class name.
Ian Rogers0571d352011-11-03 19:51:38 -0700950 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -0700951 if (descriptor[0] != 'L') {
jeffhaod5347e02012-03-22 17:25:05 -0700952 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "can't call new-instance on type '" << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -0700953 return false;
954 }
955 return true;
956}
957
Ian Rogers7b078e82014-09-10 14:44:24 -0700958inline bool MethodVerifier::CheckStringIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700959 if (idx >= dex_file_->GetHeader().string_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700960 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad string index " << idx << " (max "
961 << dex_file_->GetHeader().string_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700962 return false;
963 }
964 return true;
965}
966
Ian Rogers7b078e82014-09-10 14:44:24 -0700967inline bool MethodVerifier::CheckTypeIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700968 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700969 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
970 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700971 return false;
972 }
973 return true;
974}
975
Ian Rogers776ac1f2012-04-13 23:36:36 -0700976bool MethodVerifier::CheckNewArray(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700977 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700978 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
979 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700980 return false;
981 }
982 int bracket_count = 0;
Ian Rogers0571d352011-11-03 19:51:38 -0700983 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -0700984 const char* cp = descriptor;
985 while (*cp++ == '[') {
986 bracket_count++;
987 }
988 if (bracket_count == 0) {
989 /* The given class must be an array type. */
Brian Carlstrom93c33962013-07-26 10:37:43 -0700990 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
991 << "can't new-array class '" << descriptor << "' (not an array)";
Ian Rogersd81871c2011-10-03 13:57:23 -0700992 return false;
993 } else if (bracket_count > 255) {
994 /* It is illegal to create an array of more than 255 dimensions. */
Brian Carlstrom93c33962013-07-26 10:37:43 -0700995 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
996 << "can't new-array class '" << descriptor << "' (exceeds limit)";
Ian Rogersd81871c2011-10-03 13:57:23 -0700997 return false;
998 }
999 return true;
1000}
1001
Ian Rogers776ac1f2012-04-13 23:36:36 -07001002bool MethodVerifier::CheckArrayData(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001003 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
1004 const uint16_t* insns = code_item_->insns_ + cur_offset;
1005 const uint16_t* array_data;
1006 int32_t array_data_offset;
1007
1008 DCHECK_LT(cur_offset, insn_count);
1009 /* make sure the start of the array data table is in range */
1010 array_data_offset = insns[1] | (((int32_t) insns[2]) << 16);
1011 if ((int32_t) cur_offset + array_data_offset < 0 ||
1012 cur_offset + array_data_offset + 2 >= insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -07001013 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data start: at " << cur_offset
Brian Carlstrom93c33962013-07-26 10:37:43 -07001014 << ", data offset " << array_data_offset
1015 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -07001016 return false;
1017 }
1018 /* offset to array data table is a relative branch-style offset */
1019 array_data = insns + array_data_offset;
1020 /* make sure the table is 32-bit aligned */
Ian Rogersef7d42f2014-01-06 12:55:46 -08001021 if ((reinterpret_cast<uintptr_t>(array_data) & 0x03) != 0) {
jeffhaod5347e02012-03-22 17:25:05 -07001022 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned array data table: at " << cur_offset
1023 << ", data offset " << array_data_offset;
Ian Rogersd81871c2011-10-03 13:57:23 -07001024 return false;
1025 }
1026 uint32_t value_width = array_data[1];
Elliott Hughes398f64b2012-03-26 18:05:48 -07001027 uint32_t value_count = *reinterpret_cast<const uint32_t*>(&array_data[2]);
Ian Rogersd81871c2011-10-03 13:57:23 -07001028 uint32_t table_size = 4 + (value_width * value_count + 1) / 2;
1029 /* make sure the end of the switch is in range */
1030 if (cur_offset + array_data_offset + table_size > insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -07001031 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data end: at " << cur_offset
1032 << ", data offset " << array_data_offset << ", end "
1033 << cur_offset + array_data_offset + table_size
1034 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -07001035 return false;
1036 }
1037 return true;
1038}
1039
Ian Rogers776ac1f2012-04-13 23:36:36 -07001040bool MethodVerifier::CheckBranchTarget(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001041 int32_t offset;
1042 bool isConditional, selfOkay;
1043 if (!GetBranchOffset(cur_offset, &offset, &isConditional, &selfOkay)) {
1044 return false;
1045 }
1046 if (!selfOkay && offset == 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001047 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch offset of zero not allowed at"
1048 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -07001049 return false;
1050 }
Elliott Hughes81ff3182012-03-23 20:35:56 -07001051 // Check for 32-bit overflow. This isn't strictly necessary if we can depend on the runtime
1052 // to have identical "wrap-around" behavior, but it's unwise to depend on that.
Ian Rogersd81871c2011-10-03 13:57:23 -07001053 if (((int64_t) cur_offset + (int64_t) offset) != (int64_t) (cur_offset + offset)) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001054 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch target overflow "
1055 << reinterpret_cast<void*>(cur_offset) << " +" << offset;
Ian Rogersd81871c2011-10-03 13:57:23 -07001056 return false;
1057 }
1058 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
1059 int32_t abs_offset = cur_offset + offset;
Brian Carlstrom93c33962013-07-26 10:37:43 -07001060 if (abs_offset < 0 ||
1061 (uint32_t) abs_offset >= insn_count ||
1062 !insn_flags_[abs_offset].IsOpcode()) {
jeffhaod5347e02012-03-22 17:25:05 -07001063 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid branch target " << offset << " (-> "
Elliott Hughes398f64b2012-03-26 18:05:48 -07001064 << reinterpret_cast<void*>(abs_offset) << ") at "
1065 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -07001066 return false;
1067 }
1068 insn_flags_[abs_offset].SetBranchTarget();
1069 return true;
1070}
1071
Ian Rogers776ac1f2012-04-13 23:36:36 -07001072bool MethodVerifier::GetBranchOffset(uint32_t cur_offset, int32_t* pOffset, bool* pConditional,
Ian Rogersd81871c2011-10-03 13:57:23 -07001073 bool* selfOkay) {
1074 const uint16_t* insns = code_item_->insns_ + cur_offset;
1075 *pConditional = false;
1076 *selfOkay = false;
jeffhaoba5ebb92011-08-25 17:24:37 -07001077 switch (*insns & 0xff) {
1078 case Instruction::GOTO:
1079 *pOffset = ((int16_t) *insns) >> 8;
jeffhaoba5ebb92011-08-25 17:24:37 -07001080 break;
1081 case Instruction::GOTO_32:
1082 *pOffset = insns[1] | (((uint32_t) insns[2]) << 16);
jeffhaoba5ebb92011-08-25 17:24:37 -07001083 *selfOkay = true;
1084 break;
1085 case Instruction::GOTO_16:
1086 *pOffset = (int16_t) insns[1];
jeffhaoba5ebb92011-08-25 17:24:37 -07001087 break;
1088 case Instruction::IF_EQ:
1089 case Instruction::IF_NE:
1090 case Instruction::IF_LT:
1091 case Instruction::IF_GE:
1092 case Instruction::IF_GT:
1093 case Instruction::IF_LE:
1094 case Instruction::IF_EQZ:
1095 case Instruction::IF_NEZ:
1096 case Instruction::IF_LTZ:
1097 case Instruction::IF_GEZ:
1098 case Instruction::IF_GTZ:
1099 case Instruction::IF_LEZ:
1100 *pOffset = (int16_t) insns[1];
1101 *pConditional = true;
jeffhaoba5ebb92011-08-25 17:24:37 -07001102 break;
1103 default:
1104 return false;
jeffhaoba5ebb92011-08-25 17:24:37 -07001105 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001106 return true;
1107}
1108
Ian Rogers776ac1f2012-04-13 23:36:36 -07001109bool MethodVerifier::CheckSwitchTargets(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001110 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001111 DCHECK_LT(cur_offset, insn_count);
Ian Rogersd81871c2011-10-03 13:57:23 -07001112 const uint16_t* insns = code_item_->insns_ + cur_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001113 /* make sure the start of the switch is in range */
Ian Rogersd81871c2011-10-03 13:57:23 -07001114 int32_t switch_offset = insns[1] | ((int32_t) insns[2]) << 16;
Jeff Hao9ccd1512015-03-20 18:11:45 -07001115 if ((int32_t) cur_offset + switch_offset < 0 || cur_offset + switch_offset + 2 > insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -07001116 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch start: at " << cur_offset
Brian Carlstrom93c33962013-07-26 10:37:43 -07001117 << ", switch offset " << switch_offset
1118 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001119 return false;
1120 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001121 /* offset to switch table is a relative branch-style offset */
Ian Rogersd81871c2011-10-03 13:57:23 -07001122 const uint16_t* switch_insns = insns + switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001123 /* make sure the table is 32-bit aligned */
Ian Rogersef7d42f2014-01-06 12:55:46 -08001124 if ((reinterpret_cast<uintptr_t>(switch_insns) & 0x03) != 0) {
jeffhaod5347e02012-03-22 17:25:05 -07001125 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned switch table: at " << cur_offset
1126 << ", switch offset " << switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001127 return false;
1128 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001129 uint32_t switch_count = switch_insns[1];
1130 int32_t keys_offset, targets_offset;
1131 uint16_t expected_signature;
jeffhaoba5ebb92011-08-25 17:24:37 -07001132 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
1133 /* 0=sig, 1=count, 2/3=firstKey */
1134 targets_offset = 4;
1135 keys_offset = -1;
1136 expected_signature = Instruction::kPackedSwitchSignature;
1137 } else {
1138 /* 0=sig, 1=count, 2..count*2 = keys */
1139 keys_offset = 2;
1140 targets_offset = 2 + 2 * switch_count;
1141 expected_signature = Instruction::kSparseSwitchSignature;
1142 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001143 uint32_t table_size = targets_offset + switch_count * 2;
jeffhaoba5ebb92011-08-25 17:24:37 -07001144 if (switch_insns[0] != expected_signature) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001145 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1146 << StringPrintf("wrong signature for switch table (%x, wanted %x)",
1147 switch_insns[0], expected_signature);
jeffhaoba5ebb92011-08-25 17:24:37 -07001148 return false;
1149 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001150 /* make sure the end of the switch is in range */
1151 if (cur_offset + switch_offset + table_size > (uint32_t) insn_count) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001152 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch end: at " << cur_offset
1153 << ", switch offset " << switch_offset
1154 << ", end " << (cur_offset + switch_offset + table_size)
jeffhaod5347e02012-03-22 17:25:05 -07001155 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001156 return false;
1157 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001158 /* for a sparse switch, verify the keys are in ascending order */
1159 if (keys_offset > 0 && switch_count > 1) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001160 int32_t last_key = switch_insns[keys_offset] | (switch_insns[keys_offset + 1] << 16);
1161 for (uint32_t targ = 1; targ < switch_count; targ++) {
jeffhaoba5ebb92011-08-25 17:24:37 -07001162 int32_t key = (int32_t) switch_insns[keys_offset + targ * 2] |
1163 (int32_t) (switch_insns[keys_offset + targ * 2 + 1] << 16);
1164 if (key <= last_key) {
jeffhaod5347e02012-03-22 17:25:05 -07001165 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid packed switch: last key=" << last_key
1166 << ", this=" << key;
jeffhaoba5ebb92011-08-25 17:24:37 -07001167 return false;
1168 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001169 last_key = key;
1170 }
1171 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001172 /* verify each switch target */
Ian Rogersd81871c2011-10-03 13:57:23 -07001173 for (uint32_t targ = 0; targ < switch_count; targ++) {
1174 int32_t offset = (int32_t) switch_insns[targets_offset + targ * 2] |
1175 (int32_t) (switch_insns[targets_offset + targ * 2 + 1] << 16);
1176 int32_t abs_offset = cur_offset + offset;
Brian Carlstrom93c33962013-07-26 10:37:43 -07001177 if (abs_offset < 0 ||
1178 abs_offset >= (int32_t) insn_count ||
1179 !insn_flags_[abs_offset].IsOpcode()) {
1180 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch target " << offset
1181 << " (-> " << reinterpret_cast<void*>(abs_offset) << ") at "
1182 << reinterpret_cast<void*>(cur_offset)
1183 << "[" << targ << "]";
jeffhaoba5ebb92011-08-25 17:24:37 -07001184 return false;
1185 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001186 insn_flags_[abs_offset].SetBranchTarget();
1187 }
1188 return true;
1189}
1190
Ian Rogers776ac1f2012-04-13 23:36:36 -07001191bool MethodVerifier::CheckVarArgRegs(uint32_t vA, uint32_t arg[]) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001192 uint16_t registers_size = code_item_->registers_size_;
1193 for (uint32_t idx = 0; idx < vA; idx++) {
jeffhao457cc512012-02-02 16:55:13 -08001194 if (arg[idx] >= registers_size) {
jeffhaod5347e02012-03-22 17:25:05 -07001195 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index (" << arg[idx]
1196 << ") in non-range invoke (>= " << registers_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001197 return false;
1198 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001199 }
1200
1201 return true;
1202}
1203
Ian Rogers776ac1f2012-04-13 23:36:36 -07001204bool MethodVerifier::CheckVarArgRangeRegs(uint32_t vA, uint32_t vC) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001205 uint16_t registers_size = code_item_->registers_size_;
1206 // vA/vC are unsigned 8-bit/16-bit quantities for /range instructions, so there's no risk of
1207 // integer overflow when adding them here.
1208 if (vA + vC > registers_size) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001209 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index " << vA << "+" << vC
1210 << " in range invoke (> " << registers_size << ")";
jeffhaoba5ebb92011-08-25 17:24:37 -07001211 return false;
1212 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001213 return true;
1214}
1215
Ian Rogers776ac1f2012-04-13 23:36:36 -07001216bool MethodVerifier::VerifyCodeFlow() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001217 uint16_t registers_size = code_item_->registers_size_;
1218 uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaobdb76512011-09-07 11:43:16 -07001219
Ian Rogersd81871c2011-10-03 13:57:23 -07001220 /* Create and initialize table holding register status */
Brian Carlstrom93c33962013-07-26 10:37:43 -07001221 reg_table_.Init(kTrackCompilerInterestPoints,
1222 insn_flags_.get(),
1223 insns_size,
1224 registers_size,
1225 this);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07001226
jeffhaobdb76512011-09-07 11:43:16 -07001227
Ian Rogersd0fbd852013-09-24 18:17:04 -07001228 work_line_.reset(RegisterLine::Create(registers_size, this));
1229 saved_line_.reset(RegisterLine::Create(registers_size, this));
jeffhaobdb76512011-09-07 11:43:16 -07001230
Ian Rogersd81871c2011-10-03 13:57:23 -07001231 /* Initialize register types of method arguments. */
1232 if (!SetTypesFromSignature()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001233 DCHECK_NE(failures_.size(), 0U);
1234 std::string prepend("Bad signature in ");
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001235 prepend += PrettyMethod(dex_method_idx_, *dex_file_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001236 PrependToLastFailMessage(prepend);
Ian Rogersd81871c2011-10-03 13:57:23 -07001237 return false;
1238 }
1239 /* Perform code flow verification. */
1240 if (!CodeFlowVerifyMethod()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001241 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -07001242 return false;
jeffhaobdb76512011-09-07 11:43:16 -07001243 }
jeffhaobdb76512011-09-07 11:43:16 -07001244 return true;
1245}
1246
Ian Rogersad0b3a32012-04-16 14:50:24 -07001247std::ostream& MethodVerifier::DumpFailures(std::ostream& os) {
1248 DCHECK_EQ(failures_.size(), failure_messages_.size());
Jeff Hao4137f482013-11-22 11:44:57 -08001249 for (size_t i = 0; i < failures_.size(); ++i) {
1250 os << failure_messages_[i]->str() << "\n";
Ian Rogersad0b3a32012-04-16 14:50:24 -07001251 }
1252 return os;
1253}
1254
Ian Rogers776ac1f2012-04-13 23:36:36 -07001255void MethodVerifier::Dump(std::ostream& os) {
Ian Rogers7b078e82014-09-10 14:44:24 -07001256 if (code_item_ == nullptr) {
Elliott Hughesc073b072012-05-24 19:29:17 -07001257 os << "Native method\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001258 return;
jeffhaobdb76512011-09-07 11:43:16 -07001259 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001260 {
1261 os << "Register Types:\n";
1262 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
1263 std::ostream indent_os(&indent_filter);
1264 reg_types_.Dump(indent_os);
1265 }
Ian Rogersb4903572012-10-11 11:52:56 -07001266 os << "Dumping instructions and register lines:\n";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001267 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
1268 std::ostream indent_os(&indent_filter);
Ian Rogersd81871c2011-10-03 13:57:23 -07001269 const Instruction* inst = Instruction::At(code_item_->insns_);
1270 for (size_t dex_pc = 0; dex_pc < code_item_->insns_size_in_code_units_;
Ian Rogers7b078e82014-09-10 14:44:24 -07001271 dex_pc += inst->SizeInCodeUnits()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001272 RegisterLine* reg_line = reg_table_.GetLine(dex_pc);
Ian Rogers7b078e82014-09-10 14:44:24 -07001273 if (reg_line != nullptr) {
1274 indent_os << reg_line->Dump(this) << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07001275 }
Ian Rogers7b3ddd22013-02-21 15:19:52 -08001276 indent_os << StringPrintf("0x%04zx", dex_pc) << ": " << insn_flags_[dex_pc].ToString() << " ";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001277 const bool kDumpHexOfInstruction = false;
1278 if (kDumpHexOfInstruction) {
1279 indent_os << inst->DumpHex(5) << " ";
1280 }
1281 indent_os << inst->DumpString(dex_file_) << "\n";
jeffhaoba5ebb92011-08-25 17:24:37 -07001282 inst = inst->Next();
1283 }
jeffhaobdb76512011-09-07 11:43:16 -07001284}
1285
Ian Rogersd81871c2011-10-03 13:57:23 -07001286static bool IsPrimitiveDescriptor(char descriptor) {
1287 switch (descriptor) {
jeffhaobdb76512011-09-07 11:43:16 -07001288 case 'I':
1289 case 'C':
1290 case 'S':
1291 case 'B':
1292 case 'Z':
jeffhaobdb76512011-09-07 11:43:16 -07001293 case 'F':
1294 case 'D':
1295 case 'J':
Ian Rogersd81871c2011-10-03 13:57:23 -07001296 return true;
jeffhaobdb76512011-09-07 11:43:16 -07001297 default:
1298 return false;
1299 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001300}
1301
Ian Rogers776ac1f2012-04-13 23:36:36 -07001302bool MethodVerifier::SetTypesFromSignature() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001303 RegisterLine* reg_line = reg_table_.GetLine(0);
Andreas Gampeef0b1a12015-06-19 20:37:46 -07001304
1305 // Should have been verified earlier.
1306 DCHECK_GE(code_item_->registers_size_, code_item_->ins_size_);
1307
1308 uint32_t arg_start = code_item_->registers_size_ - code_item_->ins_size_;
Ian Rogersd81871c2011-10-03 13:57:23 -07001309 size_t expected_args = code_item_->ins_size_; /* long/double count as two */
jeffhaobdb76512011-09-07 11:43:16 -07001310
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001311 // Include the "this" pointer.
Ian Rogersd81871c2011-10-03 13:57:23 -07001312 size_t cur_arg = 0;
Ian Rogersad0b3a32012-04-16 14:50:24 -07001313 if (!IsStatic()) {
Andreas Gampeef0b1a12015-06-19 20:37:46 -07001314 if (expected_args == 0) {
1315 // Expect at least a receiver.
1316 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected 0 args, but method is not static";
1317 return false;
1318 }
1319
Ian Rogersd81871c2011-10-03 13:57:23 -07001320 // If this is a constructor for a class other than java.lang.Object, mark the first ("this")
1321 // argument as uninitialized. This restricts field access until the superclass constructor is
1322 // called.
Ian Rogersd8f69b02014-09-10 21:43:52 +00001323 const RegType& declaring_class = GetDeclaringClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07001324 if (IsConstructor() && !declaring_class.IsJavaLangObject()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07001325 reg_line->SetRegisterType(this, arg_start + cur_arg,
Ian Rogersd81871c2011-10-03 13:57:23 -07001326 reg_types_.UninitializedThisArgument(declaring_class));
1327 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07001328 reg_line->SetRegisterType(this, arg_start + cur_arg, declaring_class);
jeffhaobdb76512011-09-07 11:43:16 -07001329 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001330 cur_arg++;
jeffhaobdb76512011-09-07 11:43:16 -07001331 }
1332
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001333 const DexFile::ProtoId& proto_id =
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001334 dex_file_->GetMethodPrototype(dex_file_->GetMethodId(dex_method_idx_));
Ian Rogers0571d352011-11-03 19:51:38 -07001335 DexFileParameterIterator iterator(*dex_file_, proto_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07001336
1337 for (; iterator.HasNext(); iterator.Next()) {
1338 const char* descriptor = iterator.GetDescriptor();
Ian Rogers7b078e82014-09-10 14:44:24 -07001339 if (descriptor == nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001340 LOG(FATAL) << "Null descriptor";
1341 }
1342 if (cur_arg >= expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07001343 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1344 << " args, found more (" << descriptor << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001345 return false;
1346 }
1347 switch (descriptor[0]) {
1348 case 'L':
1349 case '[':
1350 // We assume that reference arguments are initialized. The only way it could be otherwise
1351 // (assuming the caller was verified) is if the current method is <init>, but in that case
1352 // it's effectively considered initialized the instant we reach here (in the sense that we
1353 // can return without doing anything or call virtual methods).
1354 {
Ian Rogersd8f69b02014-09-10 21:43:52 +00001355 const RegType& reg_type = ResolveClassAndCheckAccess(iterator.GetTypeIdx());
Sebastien Hertz2ed76f92014-04-22 17:11:08 +02001356 if (!reg_type.IsNonZeroReferenceTypes()) {
1357 DCHECK(HasFailures());
1358 return false;
1359 }
Ian Rogers7b078e82014-09-10 14:44:24 -07001360 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001361 }
1362 break;
1363 case 'Z':
Ian Rogers7b078e82014-09-10 14:44:24 -07001364 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_types_.Boolean());
Ian Rogersd81871c2011-10-03 13:57:23 -07001365 break;
1366 case 'C':
Ian Rogers7b078e82014-09-10 14:44:24 -07001367 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_types_.Char());
Ian Rogersd81871c2011-10-03 13:57:23 -07001368 break;
1369 case 'B':
Ian Rogers7b078e82014-09-10 14:44:24 -07001370 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_types_.Byte());
Ian Rogersd81871c2011-10-03 13:57:23 -07001371 break;
1372 case 'I':
Ian Rogers7b078e82014-09-10 14:44:24 -07001373 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_types_.Integer());
Ian Rogersd81871c2011-10-03 13:57:23 -07001374 break;
1375 case 'S':
Ian Rogers7b078e82014-09-10 14:44:24 -07001376 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_types_.Short());
Ian Rogersd81871c2011-10-03 13:57:23 -07001377 break;
1378 case 'F':
Ian Rogers7b078e82014-09-10 14:44:24 -07001379 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_types_.Float());
Ian Rogersd81871c2011-10-03 13:57:23 -07001380 break;
1381 case 'J':
1382 case 'D': {
Andreas Gampe77cd4d62014-06-19 17:29:48 -07001383 if (cur_arg + 1 >= expected_args) {
1384 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1385 << " args, found more (" << descriptor << ")";
1386 return false;
1387 }
1388
Ian Rogers7b078e82014-09-10 14:44:24 -07001389 const RegType* lo_half;
1390 const RegType* hi_half;
1391 if (descriptor[0] == 'J') {
1392 lo_half = &reg_types_.LongLo();
1393 hi_half = &reg_types_.LongHi();
1394 } else {
1395 lo_half = &reg_types_.DoubleLo();
1396 hi_half = &reg_types_.DoubleHi();
1397 }
1398 reg_line->SetRegisterTypeWide(this, arg_start + cur_arg, *lo_half, *hi_half);
Ian Rogersd81871c2011-10-03 13:57:23 -07001399 cur_arg++;
1400 break;
1401 }
1402 default:
Brian Carlstrom93c33962013-07-26 10:37:43 -07001403 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected signature type char '"
1404 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001405 return false;
1406 }
1407 cur_arg++;
1408 }
1409 if (cur_arg != expected_args) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001410 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1411 << " arguments, found " << cur_arg;
Ian Rogersd81871c2011-10-03 13:57:23 -07001412 return false;
1413 }
1414 const char* descriptor = dex_file_->GetReturnTypeDescriptor(proto_id);
1415 // Validate return type. We don't do the type lookup; just want to make sure that it has the right
1416 // format. Only major difference from the method argument format is that 'V' is supported.
1417 bool result;
1418 if (IsPrimitiveDescriptor(descriptor[0]) || descriptor[0] == 'V') {
1419 result = descriptor[1] == '\0';
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001420 } else if (descriptor[0] == '[') { // single/multi-dimensional array of object/primitive
Ian Rogersd81871c2011-10-03 13:57:23 -07001421 size_t i = 0;
1422 do {
1423 i++;
1424 } while (descriptor[i] == '['); // process leading [
1425 if (descriptor[i] == 'L') { // object array
1426 do {
1427 i++; // find closing ;
1428 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1429 result = descriptor[i] == ';';
1430 } else { // primitive array
1431 result = IsPrimitiveDescriptor(descriptor[i]) && descriptor[i + 1] == '\0';
1432 }
1433 } else if (descriptor[0] == 'L') {
1434 // could be more thorough here, but shouldn't be required
1435 size_t i = 0;
1436 do {
1437 i++;
1438 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1439 result = descriptor[i] == ';';
1440 } else {
1441 result = false;
1442 }
1443 if (!result) {
jeffhaod5347e02012-03-22 17:25:05 -07001444 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected char in return type descriptor '"
1445 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001446 }
1447 return result;
jeffhaobdb76512011-09-07 11:43:16 -07001448}
1449
Ian Rogers776ac1f2012-04-13 23:36:36 -07001450bool MethodVerifier::CodeFlowVerifyMethod() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001451 const uint16_t* insns = code_item_->insns_;
1452 const uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaoba5ebb92011-08-25 17:24:37 -07001453
jeffhaobdb76512011-09-07 11:43:16 -07001454 /* Begin by marking the first instruction as "changed". */
Ian Rogersd81871c2011-10-03 13:57:23 -07001455 insn_flags_[0].SetChanged();
1456 uint32_t start_guess = 0;
jeffhaoba5ebb92011-08-25 17:24:37 -07001457
jeffhaobdb76512011-09-07 11:43:16 -07001458 /* Continue until no instructions are marked "changed". */
1459 while (true) {
Mathieu Chartier4306ef82014-12-19 18:41:47 -08001460 if (allow_thread_suspension_) {
1461 self_->AllowThreadSuspension();
1462 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001463 // Find the first marked one. Use "start_guess" as a way to find one quickly.
1464 uint32_t insn_idx = start_guess;
1465 for (; insn_idx < insns_size; insn_idx++) {
1466 if (insn_flags_[insn_idx].IsChanged())
jeffhaobdb76512011-09-07 11:43:16 -07001467 break;
1468 }
jeffhaobdb76512011-09-07 11:43:16 -07001469 if (insn_idx == insns_size) {
1470 if (start_guess != 0) {
1471 /* try again, starting from the top */
1472 start_guess = 0;
1473 continue;
1474 } else {
1475 /* all flags are clear */
1476 break;
1477 }
1478 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001479 // We carry the working set of registers from instruction to instruction. If this address can
1480 // be the target of a branch (or throw) instruction, or if we're skipping around chasing
1481 // "changed" flags, we need to load the set of registers from the table.
1482 // Because we always prefer to continue on to the next instruction, we should never have a
1483 // situation where we have a stray "changed" flag set on an instruction that isn't a branch
1484 // target.
1485 work_insn_idx_ = insn_idx;
1486 if (insn_flags_[insn_idx].IsBranchTarget()) {
1487 work_line_->CopyFromLine(reg_table_.GetLine(insn_idx));
Ian Rogersebbdd872014-07-07 23:53:08 -07001488 } else if (kIsDebugBuild) {
jeffhaobdb76512011-09-07 11:43:16 -07001489 /*
1490 * Sanity check: retrieve the stored register line (assuming
1491 * a full table) and make sure it actually matches.
1492 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001493 RegisterLine* register_line = reg_table_.GetLine(insn_idx);
Ian Rogers7b078e82014-09-10 14:44:24 -07001494 if (register_line != nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001495 if (work_line_->CompareLine(register_line) != 0) {
1496 Dump(std::cout);
1497 std::cout << info_messages_.str();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001498 LOG(FATAL) << "work_line diverged in " << PrettyMethod(dex_method_idx_, *dex_file_)
Elliott Hughesc073b072012-05-24 19:29:17 -07001499 << "@" << reinterpret_cast<void*>(work_insn_idx_) << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07001500 << " work_line=" << work_line_->Dump(this) << "\n"
1501 << " expected=" << register_line->Dump(this);
Ian Rogersd81871c2011-10-03 13:57:23 -07001502 }
jeffhaobdb76512011-09-07 11:43:16 -07001503 }
jeffhaobdb76512011-09-07 11:43:16 -07001504 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001505 if (!CodeFlowVerifyInstruction(&start_guess)) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001506 std::string prepend(PrettyMethod(dex_method_idx_, *dex_file_));
Ian Rogersad0b3a32012-04-16 14:50:24 -07001507 prepend += " failed to verify: ";
1508 PrependToLastFailMessage(prepend);
jeffhaoba5ebb92011-08-25 17:24:37 -07001509 return false;
1510 }
jeffhaobdb76512011-09-07 11:43:16 -07001511 /* Clear "changed" and mark as visited. */
Ian Rogersd81871c2011-10-03 13:57:23 -07001512 insn_flags_[insn_idx].SetVisited();
1513 insn_flags_[insn_idx].ClearChanged();
jeffhaobdb76512011-09-07 11:43:16 -07001514 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001515
Ian Rogers1c849e52012-06-28 14:00:33 -07001516 if (gDebugVerify) {
jeffhaobdb76512011-09-07 11:43:16 -07001517 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001518 * Scan for dead code. There's nothing "evil" about dead code
jeffhaobdb76512011-09-07 11:43:16 -07001519 * (besides the wasted space), but it indicates a flaw somewhere
1520 * down the line, possibly in the verifier.
1521 *
1522 * If we've substituted "always throw" instructions into the stream,
1523 * we are almost certainly going to have some dead code.
1524 */
1525 int dead_start = -1;
Ian Rogersd81871c2011-10-03 13:57:23 -07001526 uint32_t insn_idx = 0;
Ian Rogers7b078e82014-09-10 14:44:24 -07001527 for (; insn_idx < insns_size;
1528 insn_idx += Instruction::At(code_item_->insns_ + insn_idx)->SizeInCodeUnits()) {
jeffhaobdb76512011-09-07 11:43:16 -07001529 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001530 * Switch-statement data doesn't get "visited" by scanner. It
jeffhaobdb76512011-09-07 11:43:16 -07001531 * may or may not be preceded by a padding NOP (for alignment).
1532 */
1533 if (insns[insn_idx] == Instruction::kPackedSwitchSignature ||
1534 insns[insn_idx] == Instruction::kSparseSwitchSignature ||
1535 insns[insn_idx] == Instruction::kArrayDataSignature ||
Elliott Hughes380aaa72012-07-09 14:33:15 -07001536 (insns[insn_idx] == Instruction::NOP && (insn_idx + 1 < insns_size) &&
jeffhaobdb76512011-09-07 11:43:16 -07001537 (insns[insn_idx + 1] == Instruction::kPackedSwitchSignature ||
1538 insns[insn_idx + 1] == Instruction::kSparseSwitchSignature ||
1539 insns[insn_idx + 1] == Instruction::kArrayDataSignature))) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001540 insn_flags_[insn_idx].SetVisited();
jeffhaobdb76512011-09-07 11:43:16 -07001541 }
1542
Ian Rogersd81871c2011-10-03 13:57:23 -07001543 if (!insn_flags_[insn_idx].IsVisited()) {
jeffhaobdb76512011-09-07 11:43:16 -07001544 if (dead_start < 0)
1545 dead_start = insn_idx;
1546 } else if (dead_start >= 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001547 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start)
1548 << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaobdb76512011-09-07 11:43:16 -07001549 dead_start = -1;
1550 }
1551 }
1552 if (dead_start >= 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001553 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start)
1554 << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaoba5ebb92011-08-25 17:24:37 -07001555 }
Ian Rogersc9e463c2013-06-05 16:52:26 -07001556 // To dump the state of the verify after a method, do something like:
1557 // if (PrettyMethod(dex_method_idx_, *dex_file_) ==
1558 // "boolean java.lang.String.equals(java.lang.Object)") {
1559 // LOG(INFO) << info_messages_.str();
1560 // }
jeffhaoba5ebb92011-08-25 17:24:37 -07001561 }
jeffhaobdb76512011-09-07 11:43:16 -07001562 return true;
1563}
1564
Andreas Gampe68df3202015-06-22 11:35:46 -07001565// Returns the index of the first final instance field of the given class, or kDexNoIndex if there
1566// is no such field.
1567static uint32_t GetFirstFinalInstanceFieldIndex(const DexFile& dex_file, uint16_t type_idx) {
1568 const DexFile::ClassDef* class_def = dex_file.FindClassDef(type_idx);
1569 DCHECK(class_def != nullptr);
1570 const uint8_t* class_data = dex_file.GetClassData(*class_def);
1571 DCHECK(class_data != nullptr);
1572 ClassDataItemIterator it(dex_file, class_data);
1573 // Skip static fields.
1574 while (it.HasNextStaticField()) {
1575 it.Next();
1576 }
1577 while (it.HasNextInstanceField()) {
1578 if ((it.GetFieldAccessFlags() & kAccFinal) != 0) {
1579 return it.GetMemberIndex();
1580 }
1581 it.Next();
1582 }
1583 return DexFile::kDexNoIndex;
1584}
1585
Ian Rogers776ac1f2012-04-13 23:36:36 -07001586bool MethodVerifier::CodeFlowVerifyInstruction(uint32_t* start_guess) {
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001587 // If we're doing FindLocksAtDexPc, check whether we're at the dex pc we care about.
1588 // We want the state _before_ the instruction, for the case where the dex pc we're
1589 // interested in is itself a monitor-enter instruction (which is a likely place
1590 // for a thread to be suspended).
Ian Rogers7b078e82014-09-10 14:44:24 -07001591 if (monitor_enter_dex_pcs_ != nullptr && work_insn_idx_ == interesting_dex_pc_) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001592 monitor_enter_dex_pcs_->clear(); // The new work line is more accurate than the previous one.
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001593 for (size_t i = 0; i < work_line_->GetMonitorEnterCount(); ++i) {
1594 monitor_enter_dex_pcs_->push_back(work_line_->GetMonitorEnterDexPc(i));
1595 }
1596 }
1597
jeffhaobdb76512011-09-07 11:43:16 -07001598 /*
1599 * Once we finish decoding the instruction, we need to figure out where
jeffhaod1f0fde2011-09-08 17:25:33 -07001600 * we can go from here. There are three possible ways to transfer
jeffhaobdb76512011-09-07 11:43:16 -07001601 * control to another statement:
1602 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001603 * (1) Continue to the next instruction. Applies to all but
jeffhaobdb76512011-09-07 11:43:16 -07001604 * unconditional branches, method returns, and exception throws.
jeffhaod1f0fde2011-09-08 17:25:33 -07001605 * (2) Branch to one or more possible locations. Applies to branches
jeffhaobdb76512011-09-07 11:43:16 -07001606 * and switch statements.
jeffhaod1f0fde2011-09-08 17:25:33 -07001607 * (3) Exception handlers. Applies to any instruction that can
jeffhaobdb76512011-09-07 11:43:16 -07001608 * throw an exception that is handled by an encompassing "try"
1609 * block.
1610 *
1611 * We can also return, in which case there is no successor instruction
1612 * from this point.
1613 *
Elliott Hughesadb8c672012-03-06 16:49:32 -08001614 * The behavior can be determined from the opcode flags.
jeffhaobdb76512011-09-07 11:43:16 -07001615 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001616 const uint16_t* insns = code_item_->insns_ + work_insn_idx_;
1617 const Instruction* inst = Instruction::At(insns);
Ian Rogersa75a0132012-09-28 11:41:42 -07001618 int opcode_flags = Instruction::FlagsOf(inst->Opcode());
jeffhaobdb76512011-09-07 11:43:16 -07001619
jeffhaobdb76512011-09-07 11:43:16 -07001620 int32_t branch_target = 0;
jeffhaobdb76512011-09-07 11:43:16 -07001621 bool just_set_result = false;
Ian Rogers2c8a8572011-10-24 17:11:36 -07001622 if (gDebugVerify) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001623 // Generate processing back trace to debug verifier
Elliott Hughesc073b072012-05-24 19:29:17 -07001624 LogVerifyInfo() << "Processing " << inst->DumpString(dex_file_) << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07001625 << work_line_->Dump(this) << "\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001626 }
jeffhaobdb76512011-09-07 11:43:16 -07001627
1628 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001629 * Make a copy of the previous register state. If the instruction
jeffhaobdb76512011-09-07 11:43:16 -07001630 * can throw an exception, we will copy/merge this into the "catch"
1631 * address rather than work_line, because we don't want the result
1632 * from the "successful" code path (e.g. a check-cast that "improves"
1633 * a type) to be visible to the exception handler.
1634 */
Ian Rogers776ac1f2012-04-13 23:36:36 -07001635 if ((opcode_flags & Instruction::kThrow) != 0 && CurrentInsnFlags()->IsInTry()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001636 saved_line_->CopyFromLine(work_line_.get());
Ian Rogers1ff3c982014-08-12 02:30:58 -07001637 } else if (kIsDebugBuild) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001638 saved_line_->FillWithGarbage();
jeffhaobdb76512011-09-07 11:43:16 -07001639 }
1640
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07001641
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001642 // We need to ensure the work line is consistent while performing validation. When we spot a
1643 // peephole pattern we compute a new line for either the fallthrough instruction or the
1644 // branch target.
Ian Rogers700a4022014-05-19 16:49:03 -07001645 std::unique_ptr<RegisterLine> branch_line;
1646 std::unique_ptr<RegisterLine> fallthrough_line;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001647
Stephen Kyle7e541c92014-12-17 17:10:02 +00001648 /*
1649 * If we are in a constructor, and we currently have an UninitializedThis type
1650 * in a register somewhere, we need to make sure it isn't overwritten.
1651 */
1652 bool track_uninitialized_this = false;
1653 size_t uninitialized_this_loc = 0;
1654 if (IsConstructor()) {
1655 track_uninitialized_this = work_line_->GetUninitializedThisLoc(this, &uninitialized_this_loc);
1656 }
1657
Sebastien Hertz5243e912013-05-21 10:55:07 +02001658 switch (inst->Opcode()) {
jeffhaobdb76512011-09-07 11:43:16 -07001659 case Instruction::NOP:
1660 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001661 * A "pure" NOP has no effect on anything. Data tables start with
jeffhaobdb76512011-09-07 11:43:16 -07001662 * a signature that looks like a NOP; if we see one of these in
1663 * the course of executing code then we have a problem.
1664 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001665 if (inst->VRegA_10x() != 0) {
jeffhaod5347e02012-03-22 17:25:05 -07001666 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "encountered data table in instruction stream";
jeffhaobdb76512011-09-07 11:43:16 -07001667 }
1668 break;
1669
1670 case Instruction::MOVE:
Ian Rogers7b078e82014-09-10 14:44:24 -07001671 work_line_->CopyRegister1(this, inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategory1nr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001672 break;
jeffhaobdb76512011-09-07 11:43:16 -07001673 case Instruction::MOVE_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001674 work_line_->CopyRegister1(this, inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategory1nr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001675 break;
jeffhaobdb76512011-09-07 11:43:16 -07001676 case Instruction::MOVE_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001677 work_line_->CopyRegister1(this, inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategory1nr);
jeffhaobdb76512011-09-07 11:43:16 -07001678 break;
1679 case Instruction::MOVE_WIDE:
Ian Rogers7b078e82014-09-10 14:44:24 -07001680 work_line_->CopyRegister2(this, inst->VRegA_12x(), inst->VRegB_12x());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001681 break;
jeffhaobdb76512011-09-07 11:43:16 -07001682 case Instruction::MOVE_WIDE_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001683 work_line_->CopyRegister2(this, inst->VRegA_22x(), inst->VRegB_22x());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001684 break;
jeffhaobdb76512011-09-07 11:43:16 -07001685 case Instruction::MOVE_WIDE_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001686 work_line_->CopyRegister2(this, inst->VRegA_32x(), inst->VRegB_32x());
jeffhaobdb76512011-09-07 11:43:16 -07001687 break;
1688 case Instruction::MOVE_OBJECT:
Ian Rogers7b078e82014-09-10 14:44:24 -07001689 work_line_->CopyRegister1(this, inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategoryRef);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001690 break;
jeffhaobdb76512011-09-07 11:43:16 -07001691 case Instruction::MOVE_OBJECT_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001692 work_line_->CopyRegister1(this, inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategoryRef);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001693 break;
jeffhaobdb76512011-09-07 11:43:16 -07001694 case Instruction::MOVE_OBJECT_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001695 work_line_->CopyRegister1(this, inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategoryRef);
jeffhaobdb76512011-09-07 11:43:16 -07001696 break;
1697
1698 /*
1699 * The move-result instructions copy data out of a "pseudo-register"
jeffhaod1f0fde2011-09-08 17:25:33 -07001700 * with the results from the last method invocation. In practice we
jeffhaobdb76512011-09-07 11:43:16 -07001701 * might want to hold the result in an actual CPU register, so the
1702 * Dalvik spec requires that these only appear immediately after an
1703 * invoke or filled-new-array.
1704 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001705 * These calls invalidate the "result" register. (This is now
jeffhaobdb76512011-09-07 11:43:16 -07001706 * redundant with the reset done below, but it can make the debug info
1707 * easier to read in some cases.)
1708 */
1709 case Instruction::MOVE_RESULT:
Ian Rogers7b078e82014-09-10 14:44:24 -07001710 work_line_->CopyResultRegister1(this, inst->VRegA_11x(), false);
jeffhaobdb76512011-09-07 11:43:16 -07001711 break;
1712 case Instruction::MOVE_RESULT_WIDE:
Ian Rogers7b078e82014-09-10 14:44:24 -07001713 work_line_->CopyResultRegister2(this, inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07001714 break;
1715 case Instruction::MOVE_RESULT_OBJECT:
Ian Rogers7b078e82014-09-10 14:44:24 -07001716 work_line_->CopyResultRegister1(this, inst->VRegA_11x(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001717 break;
1718
Ian Rogersd81871c2011-10-03 13:57:23 -07001719 case Instruction::MOVE_EXCEPTION: {
Sebastien Hertz270a0e12015-01-16 19:49:09 +01001720 // We do not allow MOVE_EXCEPTION as the first instruction in a method. This is a simple case
1721 // where one entrypoint to the catch block is not actually an exception path.
1722 if (work_insn_idx_ == 0) {
1723 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "move-exception at pc 0x0";
1724 break;
1725 }
jeffhaobdb76512011-09-07 11:43:16 -07001726 /*
jeffhao60f83e32012-02-13 17:16:30 -08001727 * This statement can only appear as the first instruction in an exception handler. We verify
1728 * that as part of extracting the exception type from the catch block list.
jeffhaobdb76512011-09-07 11:43:16 -07001729 */
Ian Rogersd8f69b02014-09-10 21:43:52 +00001730 const RegType& res_type = GetCaughtExceptionType();
Ian Rogers7b078e82014-09-10 14:44:24 -07001731 work_line_->SetRegisterType(this, inst->VRegA_11x(), res_type);
jeffhaobdb76512011-09-07 11:43:16 -07001732 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001733 }
jeffhaobdb76512011-09-07 11:43:16 -07001734 case Instruction::RETURN_VOID:
Ian Rogers7b078e82014-09-10 14:44:24 -07001735 if (!IsConstructor() || work_line_->CheckConstructorReturn(this)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001736 if (!GetMethodReturnType().IsConflict()) {
jeffhaod5347e02012-03-22 17:25:05 -07001737 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001738 }
jeffhaobdb76512011-09-07 11:43:16 -07001739 }
1740 break;
1741 case Instruction::RETURN:
Ian Rogers7b078e82014-09-10 14:44:24 -07001742 if (!IsConstructor() || work_line_->CheckConstructorReturn(this)) {
jeffhaobdb76512011-09-07 11:43:16 -07001743 /* check the method signature */
Ian Rogersd8f69b02014-09-10 21:43:52 +00001744 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07001745 if (!return_type.IsCategory1Types()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001746 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected non-category 1 return type "
1747 << return_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001748 } else {
1749 // Compilers may generate synthetic functions that write byte values into boolean fields.
1750 // Also, it may use integer values for boolean, byte, short, and character return types.
Sebastien Hertz5243e912013-05-21 10:55:07 +02001751 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07001752 const RegType& src_type = work_line_->GetRegisterType(this, vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07001753 bool use_src = ((return_type.IsBoolean() && src_type.IsByte()) ||
1754 ((return_type.IsBoolean() || return_type.IsByte() ||
1755 return_type.IsShort() || return_type.IsChar()) &&
1756 src_type.IsInteger()));
1757 /* check the register contents */
Ian Rogersad0b3a32012-04-16 14:50:24 -07001758 bool success =
Ian Rogers7b078e82014-09-10 14:44:24 -07001759 work_line_->VerifyRegisterType(this, vregA, use_src ? src_type : return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001760 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001761 AppendToLastFailMessage(StringPrintf(" return-1nr on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07001762 }
jeffhaobdb76512011-09-07 11:43:16 -07001763 }
1764 }
1765 break;
1766 case Instruction::RETURN_WIDE:
Ian Rogers7b078e82014-09-10 14:44:24 -07001767 if (!IsConstructor() || work_line_->CheckConstructorReturn(this)) {
jeffhaobdb76512011-09-07 11:43:16 -07001768 /* check the method signature */
Ian Rogersd8f69b02014-09-10 21:43:52 +00001769 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07001770 if (!return_type.IsCategory2Types()) {
jeffhaod5347e02012-03-22 17:25:05 -07001771 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-wide not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001772 } else {
1773 /* check the register contents */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001774 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07001775 bool success = work_line_->VerifyRegisterType(this, vregA, return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001776 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001777 AppendToLastFailMessage(StringPrintf(" return-wide on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07001778 }
jeffhaobdb76512011-09-07 11:43:16 -07001779 }
1780 }
1781 break;
1782 case Instruction::RETURN_OBJECT:
Ian Rogers7b078e82014-09-10 14:44:24 -07001783 if (!IsConstructor() || work_line_->CheckConstructorReturn(this)) {
Ian Rogersd8f69b02014-09-10 21:43:52 +00001784 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07001785 if (!return_type.IsReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001786 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-object not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001787 } else {
1788 /* return_type is the *expected* return type, not register value */
1789 DCHECK(!return_type.IsZero());
1790 DCHECK(!return_type.IsUninitializedReference());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001791 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07001792 const RegType& reg_type = work_line_->GetRegisterType(this, vregA);
Andreas Gampea32210c2015-06-24 10:26:13 -07001793 // Disallow returning undefined, conflict & uninitialized values and verify that the
1794 // reference in vAA is an instance of the "return_type."
1795 if (reg_type.IsUndefined()) {
1796 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning undefined register";
1797 } else if (reg_type.IsConflict()) {
1798 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning register with conflict";
1799 } else if (reg_type.IsUninitializedTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001800 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "returning uninitialized object '"
1801 << reg_type << "'";
Ian Rogers9074b992011-10-26 17:41:55 -07001802 } else if (!return_type.IsAssignableFrom(reg_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07001803 if (reg_type.IsUnresolvedTypes() || return_type.IsUnresolvedTypes()) {
1804 Fail(VERIFY_ERROR_NO_CLASS) << " can't resolve returned type '" << return_type
1805 << "' or '" << reg_type << "'";
1806 } else {
Andreas Gampe16f149c2015-03-23 10:10:20 -07001807 bool soft_error = false;
1808 // Check whether arrays are involved. They will show a valid class status, even
1809 // if their components are erroneous.
1810 if (reg_type.IsArrayTypes() && return_type.IsArrayTypes()) {
1811 return_type.CanAssignArray(reg_type, reg_types_, class_loader_, &soft_error);
1812 if (soft_error) {
1813 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "array with erroneous component type: "
1814 << reg_type << " vs " << return_type;
1815 }
1816 }
1817
1818 if (!soft_error) {
1819 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning '" << reg_type
1820 << "', but expected from declaration '" << return_type << "'";
1821 }
Jeff Haoa3faaf42013-09-03 19:07:00 -07001822 }
jeffhaobdb76512011-09-07 11:43:16 -07001823 }
1824 }
1825 }
1826 break;
1827
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001828 /* could be boolean, int, float, or a null reference */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001829 case Instruction::CONST_4: {
1830 int32_t val = static_cast<int32_t>(inst->VRegB_11n() << 28) >> 28;
Ian Rogers7b078e82014-09-10 14:44:24 -07001831 work_line_->SetRegisterType(this, inst->VRegA_11n(),
Ian Rogers46960fe2014-05-23 10:43:43 -07001832 DetermineCat1Constant(val, need_precise_constants_));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001833 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02001834 }
1835 case Instruction::CONST_16: {
1836 int16_t val = static_cast<int16_t>(inst->VRegB_21s());
Ian Rogers7b078e82014-09-10 14:44:24 -07001837 work_line_->SetRegisterType(this, inst->VRegA_21s(),
Ian Rogers46960fe2014-05-23 10:43:43 -07001838 DetermineCat1Constant(val, need_precise_constants_));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001839 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02001840 }
Sebastien Hertz849600b2013-12-20 10:28:08 +01001841 case Instruction::CONST: {
1842 int32_t val = inst->VRegB_31i();
Ian Rogers7b078e82014-09-10 14:44:24 -07001843 work_line_->SetRegisterType(this, inst->VRegA_31i(),
Ian Rogers46960fe2014-05-23 10:43:43 -07001844 DetermineCat1Constant(val, need_precise_constants_));
jeffhaobdb76512011-09-07 11:43:16 -07001845 break;
Sebastien Hertz849600b2013-12-20 10:28:08 +01001846 }
1847 case Instruction::CONST_HIGH16: {
1848 int32_t val = static_cast<int32_t>(inst->VRegB_21h() << 16);
Ian Rogers7b078e82014-09-10 14:44:24 -07001849 work_line_->SetRegisterType(this, inst->VRegA_21h(),
Ian Rogers46960fe2014-05-23 10:43:43 -07001850 DetermineCat1Constant(val, need_precise_constants_));
jeffhaobdb76512011-09-07 11:43:16 -07001851 break;
Sebastien Hertz849600b2013-12-20 10:28:08 +01001852 }
jeffhaobdb76512011-09-07 11:43:16 -07001853 /* could be long or double; resolved upon use */
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001854 case Instruction::CONST_WIDE_16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001855 int64_t val = static_cast<int16_t>(inst->VRegB_21s());
Ian Rogersd8f69b02014-09-10 21:43:52 +00001856 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1857 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07001858 work_line_->SetRegisterTypeWide(this, inst->VRegA_21s(), lo, hi);
jeffhaobdb76512011-09-07 11:43:16 -07001859 break;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001860 }
1861 case Instruction::CONST_WIDE_32: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001862 int64_t val = static_cast<int32_t>(inst->VRegB_31i());
Ian Rogersd8f69b02014-09-10 21:43:52 +00001863 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1864 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07001865 work_line_->SetRegisterTypeWide(this, inst->VRegA_31i(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001866 break;
1867 }
1868 case Instruction::CONST_WIDE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001869 int64_t val = inst->VRegB_51l();
Ian Rogersd8f69b02014-09-10 21:43:52 +00001870 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1871 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07001872 work_line_->SetRegisterTypeWide(this, inst->VRegA_51l(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001873 break;
1874 }
1875 case Instruction::CONST_WIDE_HIGH16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001876 int64_t val = static_cast<uint64_t>(inst->VRegB_21h()) << 48;
Ian Rogersd8f69b02014-09-10 21:43:52 +00001877 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1878 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07001879 work_line_->SetRegisterTypeWide(this, inst->VRegA_21h(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001880 break;
1881 }
jeffhaobdb76512011-09-07 11:43:16 -07001882 case Instruction::CONST_STRING:
Ian Rogers7b078e82014-09-10 14:44:24 -07001883 work_line_->SetRegisterType(this, inst->VRegA_21c(), reg_types_.JavaLangString());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001884 break;
jeffhaobdb76512011-09-07 11:43:16 -07001885 case Instruction::CONST_STRING_JUMBO:
Ian Rogers7b078e82014-09-10 14:44:24 -07001886 work_line_->SetRegisterType(this, inst->VRegA_31c(), reg_types_.JavaLangString());
jeffhaobdb76512011-09-07 11:43:16 -07001887 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001888 case Instruction::CONST_CLASS: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001889 // Get type from instruction if unresolved then we need an access check
1890 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Ian Rogersd8f69b02014-09-10 21:43:52 +00001891 const RegType& res_type = ResolveClassAndCheckAccess(inst->VRegB_21c());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001892 // Register holds class, ie its type is class, on error it will hold Conflict.
Ian Rogers7b078e82014-09-10 14:44:24 -07001893 work_line_->SetRegisterType(this, inst->VRegA_21c(),
Ian Rogersb4903572012-10-11 11:52:56 -07001894 res_type.IsConflict() ? res_type
Ian Rogers7b078e82014-09-10 14:44:24 -07001895 : reg_types_.JavaLangClass());
jeffhaobdb76512011-09-07 11:43:16 -07001896 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001897 }
jeffhaobdb76512011-09-07 11:43:16 -07001898 case Instruction::MONITOR_ENTER:
Ian Rogers7b078e82014-09-10 14:44:24 -07001899 work_line_->PushMonitor(this, inst->VRegA_11x(), work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07001900 break;
1901 case Instruction::MONITOR_EXIT:
1902 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001903 * monitor-exit instructions are odd. They can throw exceptions,
jeffhaobdb76512011-09-07 11:43:16 -07001904 * but when they do they act as if they succeeded and the PC is
jeffhaod1f0fde2011-09-08 17:25:33 -07001905 * pointing to the following instruction. (This behavior goes back
jeffhaobdb76512011-09-07 11:43:16 -07001906 * to the need to handle asynchronous exceptions, a now-deprecated
1907 * feature that Dalvik doesn't support.)
1908 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001909 * In practice we don't need to worry about this. The only
jeffhaobdb76512011-09-07 11:43:16 -07001910 * exceptions that can be thrown from monitor-exit are for a
jeffhaod1f0fde2011-09-08 17:25:33 -07001911 * null reference and -exit without a matching -enter. If the
jeffhaobdb76512011-09-07 11:43:16 -07001912 * structured locking checks are working, the former would have
1913 * failed on the -enter instruction, and the latter is impossible.
1914 *
1915 * This is fortunate, because issue 3221411 prevents us from
1916 * chasing the "can throw" path when monitor verification is
jeffhaod1f0fde2011-09-08 17:25:33 -07001917 * enabled. If we can fully verify the locking we can ignore
jeffhaobdb76512011-09-07 11:43:16 -07001918 * some catch blocks (which will show up as "dead" code when
1919 * we skip them here); if we can't, then the code path could be
1920 * "live" so we still need to check it.
1921 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001922 opcode_flags &= ~Instruction::kThrow;
Ian Rogers7b078e82014-09-10 14:44:24 -07001923 work_line_->PopMonitor(this, inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07001924 break;
1925
Ian Rogers28ad40d2011-10-27 15:19:26 -07001926 case Instruction::CHECK_CAST:
Ian Rogersd81871c2011-10-03 13:57:23 -07001927 case Instruction::INSTANCE_OF: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001928 /*
1929 * If this instruction succeeds, we will "downcast" register vA to the type in vB. (This
1930 * could be a "upcast" -- not expected, so we don't try to address it.)
1931 *
1932 * If it fails, an exception is thrown, which we deal with later by ignoring the update to
Elliott Hughesadb8c672012-03-06 16:49:32 -08001933 * dec_insn.vA when branching to a handler.
Ian Rogers28ad40d2011-10-27 15:19:26 -07001934 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001935 const bool is_checkcast = (inst->Opcode() == Instruction::CHECK_CAST);
1936 const uint32_t type_idx = (is_checkcast) ? inst->VRegB_21c() : inst->VRegC_22c();
Ian Rogersd8f69b02014-09-10 21:43:52 +00001937 const RegType& res_type = ResolveClassAndCheckAccess(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001938 if (res_type.IsConflict()) {
Andreas Gampe00633eb2014-07-17 16:13:35 -07001939 // If this is a primitive type, fail HARD.
Mathieu Chartierbf99f772014-08-23 16:37:27 -07001940 mirror::Class* klass = dex_cache_->GetResolvedType(type_idx);
Andreas Gampe00633eb2014-07-17 16:13:35 -07001941 if (klass != nullptr && klass->IsPrimitive()) {
1942 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "using primitive type "
1943 << dex_file_->StringByTypeIdx(type_idx) << " in instanceof in "
1944 << GetDeclaringClass();
1945 break;
1946 }
1947
Ian Rogersad0b3a32012-04-16 14:50:24 -07001948 DCHECK_NE(failures_.size(), 0U);
1949 if (!is_checkcast) {
Ian Rogers7b078e82014-09-10 14:44:24 -07001950 work_line_->SetRegisterType(this, inst->VRegA_22c(), reg_types_.Boolean());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001951 }
1952 break; // bad class
Ian Rogers9f1ab122011-12-12 08:52:43 -08001953 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001954 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Sebastien Hertz5243e912013-05-21 10:55:07 +02001955 uint32_t orig_type_reg = (is_checkcast) ? inst->VRegA_21c() : inst->VRegB_22c();
Ian Rogers7b078e82014-09-10 14:44:24 -07001956 const RegType& orig_type = work_line_->GetRegisterType(this, orig_type_reg);
Ian Rogers28ad40d2011-10-27 15:19:26 -07001957 if (!res_type.IsNonZeroReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001958 if (is_checkcast) {
1959 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on unexpected class " << res_type;
1960 } else {
1961 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on unexpected class " << res_type;
1962 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001963 } else if (!orig_type.IsReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001964 if (is_checkcast) {
1965 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on non-reference in v" << orig_type_reg;
1966 } else {
1967 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on non-reference in v" << orig_type_reg;
1968 }
jeffhao2a8a90e2011-09-26 14:25:31 -07001969 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001970 if (is_checkcast) {
Ian Rogers7b078e82014-09-10 14:44:24 -07001971 work_line_->SetRegisterType(this, inst->VRegA_21c(), res_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001972 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07001973 work_line_->SetRegisterType(this, inst->VRegA_22c(), reg_types_.Boolean());
jeffhaobdb76512011-09-07 11:43:16 -07001974 }
jeffhaobdb76512011-09-07 11:43:16 -07001975 }
jeffhao2a8a90e2011-09-26 14:25:31 -07001976 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001977 }
1978 case Instruction::ARRAY_LENGTH: {
Ian Rogers7b078e82014-09-10 14:44:24 -07001979 const RegType& res_type = work_line_->GetRegisterType(this, inst->VRegB_12x());
Ian Rogers28ad40d2011-10-27 15:19:26 -07001980 if (res_type.IsReferenceTypes()) {
Ian Rogers89310de2012-02-01 13:47:30 -08001981 if (!res_type.IsArrayTypes() && !res_type.IsZero()) { // ie not an array or null
jeffhaod5347e02012-03-22 17:25:05 -07001982 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001983 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07001984 work_line_->SetRegisterType(this, inst->VRegA_12x(), reg_types_.Integer());
Ian Rogersd81871c2011-10-03 13:57:23 -07001985 }
Andreas Gampe65c9db82014-07-28 13:14:34 -07001986 } else {
1987 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001988 }
1989 break;
1990 }
1991 case Instruction::NEW_INSTANCE: {
Ian Rogersd8f69b02014-09-10 21:43:52 +00001992 const RegType& res_type = ResolveClassAndCheckAccess(inst->VRegB_21c());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001993 if (res_type.IsConflict()) {
1994 DCHECK_NE(failures_.size(), 0U);
1995 break; // bad class
jeffhao8cd6dda2012-02-22 10:15:34 -08001996 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001997 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
1998 // can't create an instance of an interface or abstract class */
1999 if (!res_type.IsInstantiableTypes()) {
2000 Fail(VERIFY_ERROR_INSTANTIATION)
2001 << "new-instance on primitive, interface or abstract class" << res_type;
Ian Rogers08f753d2012-08-24 14:35:25 -07002002 // Soft failure so carry on to set register type.
Ian Rogersd81871c2011-10-03 13:57:23 -07002003 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00002004 const RegType& uninit_type = reg_types_.Uninitialized(res_type, work_insn_idx_);
Ian Rogers08f753d2012-08-24 14:35:25 -07002005 // Any registers holding previous allocations from this address that have not yet been
2006 // initialized must be marked invalid.
Ian Rogers7b078e82014-09-10 14:44:24 -07002007 work_line_->MarkUninitRefsAsInvalid(this, uninit_type);
Ian Rogers08f753d2012-08-24 14:35:25 -07002008 // add the new uninitialized reference to the register state
Ian Rogers7b078e82014-09-10 14:44:24 -07002009 work_line_->SetRegisterType(this, inst->VRegA_21c(), uninit_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07002010 break;
2011 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08002012 case Instruction::NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002013 VerifyNewArray(inst, false, false);
jeffhaobdb76512011-09-07 11:43:16 -07002014 break;
2015 case Instruction::FILLED_NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002016 VerifyNewArray(inst, true, false);
Ian Rogers0c4a5062012-02-03 15:18:59 -08002017 just_set_result = true; // Filled new array sets result register
jeffhaobdb76512011-09-07 11:43:16 -07002018 break;
Ian Rogers0c4a5062012-02-03 15:18:59 -08002019 case Instruction::FILLED_NEW_ARRAY_RANGE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002020 VerifyNewArray(inst, true, true);
Ian Rogers0c4a5062012-02-03 15:18:59 -08002021 just_set_result = true; // Filled new array range sets result register
2022 break;
jeffhaobdb76512011-09-07 11:43:16 -07002023 case Instruction::CMPL_FLOAT:
2024 case Instruction::CMPG_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002025 if (!work_line_->VerifyRegisterType(this, inst->VRegB_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08002026 break;
2027 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002028 if (!work_line_->VerifyRegisterType(this, inst->VRegC_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08002029 break;
2030 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002031 work_line_->SetRegisterType(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002032 break;
2033 case Instruction::CMPL_DOUBLE:
2034 case Instruction::CMPG_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002035 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegB_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002036 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002037 break;
2038 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002039 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegC_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002040 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002041 break;
2042 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002043 work_line_->SetRegisterType(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002044 break;
2045 case Instruction::CMP_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002046 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegB_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002047 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002048 break;
2049 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002050 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegC_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002051 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002052 break;
2053 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002054 work_line_->SetRegisterType(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002055 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002056 case Instruction::THROW: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002057 const RegType& res_type = work_line_->GetRegisterType(this, inst->VRegA_11x());
Ian Rogersb4903572012-10-11 11:52:56 -07002058 if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(res_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07002059 Fail(res_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS : VERIFY_ERROR_BAD_CLASS_SOFT)
2060 << "thrown class " << res_type << " not instanceof Throwable";
jeffhaobdb76512011-09-07 11:43:16 -07002061 }
2062 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002063 }
jeffhaobdb76512011-09-07 11:43:16 -07002064 case Instruction::GOTO:
2065 case Instruction::GOTO_16:
2066 case Instruction::GOTO_32:
2067 /* no effect on or use of registers */
2068 break;
2069
2070 case Instruction::PACKED_SWITCH:
2071 case Instruction::SPARSE_SWITCH:
2072 /* verify that vAA is an integer, or can be converted to one */
Ian Rogers7b078e82014-09-10 14:44:24 -07002073 work_line_->VerifyRegisterType(this, inst->VRegA_31t(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002074 break;
2075
Ian Rogersd81871c2011-10-03 13:57:23 -07002076 case Instruction::FILL_ARRAY_DATA: {
2077 /* Similar to the verification done for APUT */
Ian Rogers7b078e82014-09-10 14:44:24 -07002078 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegA_31t());
Ian Rogers89310de2012-02-01 13:47:30 -08002079 /* array_type can be null if the reg type is Zero */
2080 if (!array_type.IsZero()) {
jeffhao457cc512012-02-02 16:55:13 -08002081 if (!array_type.IsArrayTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002082 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with array type "
2083 << array_type;
Ian Rogers89310de2012-02-01 13:47:30 -08002084 } else {
Ian Rogersd8f69b02014-09-10 21:43:52 +00002085 const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
Ian Rogersad0b3a32012-04-16 14:50:24 -07002086 DCHECK(!component_type.IsConflict());
jeffhao457cc512012-02-02 16:55:13 -08002087 if (component_type.IsNonZeroReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002088 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with component type "
2089 << component_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002090 } else {
jeffhao457cc512012-02-02 16:55:13 -08002091 // Now verify if the element width in the table matches the element width declared in
2092 // the array
2093 const uint16_t* array_data = insns + (insns[1] | (((int32_t) insns[2]) << 16));
2094 if (array_data[0] != Instruction::kArrayDataSignature) {
jeffhaod5347e02012-03-22 17:25:05 -07002095 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid magic for array-data";
jeffhao457cc512012-02-02 16:55:13 -08002096 } else {
2097 size_t elem_width = Primitive::ComponentSize(component_type.GetPrimitiveType());
2098 // Since we don't compress the data in Dex, expect to see equal width of data stored
2099 // in the table and expected from the array class.
2100 if (array_data[1] != elem_width) {
jeffhaod5347e02012-03-22 17:25:05 -07002101 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-data size mismatch (" << array_data[1]
2102 << " vs " << elem_width << ")";
jeffhao457cc512012-02-02 16:55:13 -08002103 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002104 }
2105 }
jeffhaobdb76512011-09-07 11:43:16 -07002106 }
2107 }
2108 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002109 }
jeffhaobdb76512011-09-07 11:43:16 -07002110 case Instruction::IF_EQ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002111 case Instruction::IF_NE: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002112 const RegType& reg_type1 = work_line_->GetRegisterType(this, inst->VRegA_22t());
2113 const RegType& reg_type2 = work_line_->GetRegisterType(this, inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002114 bool mismatch = false;
2115 if (reg_type1.IsZero()) { // zero then integral or reference expected
2116 mismatch = !reg_type2.IsReferenceTypes() && !reg_type2.IsIntegralTypes();
2117 } else if (reg_type1.IsReferenceTypes()) { // both references?
2118 mismatch = !reg_type2.IsReferenceTypes();
2119 } else { // both integral?
2120 mismatch = !reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes();
2121 }
2122 if (mismatch) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002123 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to if-eq/if-ne (" << reg_type1 << ","
2124 << reg_type2 << ") must both be references or integral";
jeffhaobdb76512011-09-07 11:43:16 -07002125 }
2126 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002127 }
jeffhaobdb76512011-09-07 11:43:16 -07002128 case Instruction::IF_LT:
2129 case Instruction::IF_GE:
2130 case Instruction::IF_GT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002131 case Instruction::IF_LE: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002132 const RegType& reg_type1 = work_line_->GetRegisterType(this, inst->VRegA_22t());
2133 const RegType& reg_type2 = work_line_->GetRegisterType(this, inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002134 if (!reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002135 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to 'if' (" << reg_type1 << ","
2136 << reg_type2 << ") must be integral";
jeffhaobdb76512011-09-07 11:43:16 -07002137 }
2138 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002139 }
jeffhaobdb76512011-09-07 11:43:16 -07002140 case Instruction::IF_EQZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002141 case Instruction::IF_NEZ: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002142 const RegType& reg_type = work_line_->GetRegisterType(this, inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002143 if (!reg_type.IsReferenceTypes() && !reg_type.IsIntegralTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002144 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
2145 << " unexpected as arg to if-eqz/if-nez";
Ian Rogersd81871c2011-10-03 13:57:23 -07002146 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002147
2148 // Find previous instruction - its existence is a precondition to peephole optimization.
Ian Rogers9b360392013-06-06 14:45:07 -07002149 uint32_t instance_of_idx = 0;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002150 if (0 != work_insn_idx_) {
Ian Rogers9b360392013-06-06 14:45:07 -07002151 instance_of_idx = work_insn_idx_ - 1;
Brian Carlstromdf629502013-07-17 22:39:56 -07002152 while (0 != instance_of_idx && !insn_flags_[instance_of_idx].IsOpcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07002153 instance_of_idx--;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002154 }
Andreas Gampe7c038102014-10-27 20:08:46 -07002155 if (FailOrAbort(this, insn_flags_[instance_of_idx].IsOpcode(),
2156 "Unable to get previous instruction of if-eqz/if-nez for work index ",
2157 work_insn_idx_)) {
2158 break;
2159 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002160 } else {
2161 break;
2162 }
2163
Ian Rogers9b360392013-06-06 14:45:07 -07002164 const Instruction* instance_of_inst = Instruction::At(code_item_->insns_ + instance_of_idx);
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002165
2166 /* Check for peep-hole pattern of:
2167 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002168 * instance-of vX, vY, T;
2169 * ifXXX vX, label ;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002170 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002171 * label:
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002172 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002173 * and sharpen the type of vY to be type T.
2174 * Note, this pattern can't be if:
2175 * - if there are other branches to this branch,
2176 * - when vX == vY.
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002177 */
Ian Rogersfae370a2013-06-05 08:33:27 -07002178 if (!CurrentInsnFlags()->IsBranchTarget() &&
Ian Rogers9b360392013-06-06 14:45:07 -07002179 (Instruction::INSTANCE_OF == instance_of_inst->Opcode()) &&
2180 (inst->VRegA_21t() == instance_of_inst->VRegA_22c()) &&
2181 (instance_of_inst->VRegA_22c() != instance_of_inst->VRegB_22c())) {
Ian Rogersebbdd872014-07-07 23:53:08 -07002182 // Check the type of the instance-of is different than that of registers type, as if they
2183 // are the same there is no work to be done here. Check that the conversion is not to or
2184 // from an unresolved type as type information is imprecise. If the instance-of is to an
2185 // interface then ignore the type information as interfaces can only be treated as Objects
2186 // and we don't want to disallow field and other operations on the object. If the value
2187 // being instance-of checked against is known null (zero) then allow the optimization as
2188 // we didn't have type information. If the merge of the instance-of type with the original
2189 // type is assignable to the original then allow optimization. This check is performed to
2190 // ensure that subsequent merges don't lose type information - such as becoming an
2191 // interface from a class that would lose information relevant to field checks.
Ian Rogers7b078e82014-09-10 14:44:24 -07002192 const RegType& orig_type = work_line_->GetRegisterType(this, instance_of_inst->VRegB_22c());
Ian Rogersd8f69b02014-09-10 21:43:52 +00002193 const RegType& cast_type = ResolveClassAndCheckAccess(instance_of_inst->VRegC_22c());
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002194
Ian Rogersebbdd872014-07-07 23:53:08 -07002195 if (!orig_type.Equals(cast_type) &&
2196 !cast_type.IsUnresolvedTypes() && !orig_type.IsUnresolvedTypes() &&
Andreas Gampe00633eb2014-07-17 16:13:35 -07002197 cast_type.HasClass() && // Could be conflict type, make sure it has a class.
Ian Rogersebbdd872014-07-07 23:53:08 -07002198 !cast_type.GetClass()->IsInterface() &&
2199 (orig_type.IsZero() ||
2200 orig_type.IsStrictlyAssignableFrom(cast_type.Merge(orig_type, &reg_types_)))) {
Ian Rogersd0fbd852013-09-24 18:17:04 -07002201 RegisterLine* update_line = RegisterLine::Create(code_item_->registers_size_, this);
Ian Rogersfae370a2013-06-05 08:33:27 -07002202 if (inst->Opcode() == Instruction::IF_EQZ) {
Ian Rogers9b360392013-06-06 14:45:07 -07002203 fallthrough_line.reset(update_line);
Ian Rogersfae370a2013-06-05 08:33:27 -07002204 } else {
Ian Rogers9b360392013-06-06 14:45:07 -07002205 branch_line.reset(update_line);
2206 }
2207 update_line->CopyFromLine(work_line_.get());
Ian Rogers7b078e82014-09-10 14:44:24 -07002208 update_line->SetRegisterType(this, instance_of_inst->VRegB_22c(), cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002209 if (!insn_flags_[instance_of_idx].IsBranchTarget() && 0 != instance_of_idx) {
2210 // See if instance-of was preceded by a move-object operation, common due to the small
2211 // register encoding space of instance-of, and propagate type information to the source
2212 // of the move-object.
2213 uint32_t move_idx = instance_of_idx - 1;
Brian Carlstromdf629502013-07-17 22:39:56 -07002214 while (0 != move_idx && !insn_flags_[move_idx].IsOpcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07002215 move_idx--;
2216 }
Andreas Gampe7c038102014-10-27 20:08:46 -07002217 if (FailOrAbort(this, insn_flags_[move_idx].IsOpcode(),
2218 "Unable to get previous instruction of if-eqz/if-nez for work index ",
2219 work_insn_idx_)) {
2220 break;
2221 }
Ian Rogers9b360392013-06-06 14:45:07 -07002222 const Instruction* move_inst = Instruction::At(code_item_->insns_ + move_idx);
2223 switch (move_inst->Opcode()) {
2224 case Instruction::MOVE_OBJECT:
2225 if (move_inst->VRegA_12x() == instance_of_inst->VRegB_22c()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002226 update_line->SetRegisterType(this, move_inst->VRegB_12x(), cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002227 }
2228 break;
2229 case Instruction::MOVE_OBJECT_FROM16:
2230 if (move_inst->VRegA_22x() == instance_of_inst->VRegB_22c()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002231 update_line->SetRegisterType(this, move_inst->VRegB_22x(), cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002232 }
2233 break;
2234 case Instruction::MOVE_OBJECT_16:
2235 if (move_inst->VRegA_32x() == instance_of_inst->VRegB_22c()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002236 update_line->SetRegisterType(this, move_inst->VRegB_32x(), cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002237 }
2238 break;
2239 default:
2240 break;
2241 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002242 }
2243 }
2244 }
2245
jeffhaobdb76512011-09-07 11:43:16 -07002246 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002247 }
jeffhaobdb76512011-09-07 11:43:16 -07002248 case Instruction::IF_LTZ:
2249 case Instruction::IF_GEZ:
2250 case Instruction::IF_GTZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002251 case Instruction::IF_LEZ: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002252 const RegType& reg_type = work_line_->GetRegisterType(this, inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002253 if (!reg_type.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002254 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
2255 << " unexpected as arg to if-ltz/if-gez/if-gtz/if-lez";
Ian Rogersd81871c2011-10-03 13:57:23 -07002256 }
jeffhaobdb76512011-09-07 11:43:16 -07002257 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002258 }
jeffhaobdb76512011-09-07 11:43:16 -07002259 case Instruction::AGET_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002260 VerifyAGet(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002261 break;
jeffhaobdb76512011-09-07 11:43:16 -07002262 case Instruction::AGET_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002263 VerifyAGet(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002264 break;
jeffhaobdb76512011-09-07 11:43:16 -07002265 case Instruction::AGET_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002266 VerifyAGet(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002267 break;
jeffhaobdb76512011-09-07 11:43:16 -07002268 case Instruction::AGET_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002269 VerifyAGet(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002270 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002271 case Instruction::AGET:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002272 VerifyAGet(inst, reg_types_.Integer(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002273 break;
jeffhaobdb76512011-09-07 11:43:16 -07002274 case Instruction::AGET_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002275 VerifyAGet(inst, reg_types_.LongLo(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002276 break;
2277 case Instruction::AGET_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002278 VerifyAGet(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07002279 break;
2280
Ian Rogersd81871c2011-10-03 13:57:23 -07002281 case Instruction::APUT_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002282 VerifyAPut(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002283 break;
2284 case Instruction::APUT_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002285 VerifyAPut(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002286 break;
2287 case Instruction::APUT_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002288 VerifyAPut(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002289 break;
2290 case Instruction::APUT_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002291 VerifyAPut(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002292 break;
2293 case Instruction::APUT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002294 VerifyAPut(inst, reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002295 break;
2296 case Instruction::APUT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002297 VerifyAPut(inst, reg_types_.LongLo(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002298 break;
2299 case Instruction::APUT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002300 VerifyAPut(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07002301 break;
2302
jeffhaobdb76512011-09-07 11:43:16 -07002303 case Instruction::IGET_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002304 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002305 break;
jeffhaobdb76512011-09-07 11:43:16 -07002306 case Instruction::IGET_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002307 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002308 break;
jeffhaobdb76512011-09-07 11:43:16 -07002309 case Instruction::IGET_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002310 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002311 break;
jeffhaobdb76512011-09-07 11:43:16 -07002312 case Instruction::IGET_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002313 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002314 break;
2315 case Instruction::IGET:
Andreas Gampe896df402014-10-20 22:25:29 -07002316 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002317 break;
2318 case Instruction::IGET_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002319 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002320 break;
2321 case Instruction::IGET_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002322 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false,
2323 false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002324 break;
jeffhaobdb76512011-09-07 11:43:16 -07002325
Ian Rogersd81871c2011-10-03 13:57:23 -07002326 case Instruction::IPUT_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002327 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002328 break;
2329 case Instruction::IPUT_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002330 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002331 break;
2332 case Instruction::IPUT_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002333 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002334 break;
2335 case Instruction::IPUT_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002336 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002337 break;
2338 case Instruction::IPUT:
Andreas Gampe896df402014-10-20 22:25:29 -07002339 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002340 break;
2341 case Instruction::IPUT_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002342 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002343 break;
jeffhaobdb76512011-09-07 11:43:16 -07002344 case Instruction::IPUT_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002345 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false,
2346 false);
jeffhaobdb76512011-09-07 11:43:16 -07002347 break;
2348
jeffhaobdb76512011-09-07 11:43:16 -07002349 case Instruction::SGET_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002350 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002351 break;
jeffhaobdb76512011-09-07 11:43:16 -07002352 case Instruction::SGET_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002353 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002354 break;
jeffhaobdb76512011-09-07 11:43:16 -07002355 case Instruction::SGET_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002356 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002357 break;
jeffhaobdb76512011-09-07 11:43:16 -07002358 case Instruction::SGET_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002359 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002360 break;
2361 case Instruction::SGET:
Andreas Gampe896df402014-10-20 22:25:29 -07002362 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002363 break;
2364 case Instruction::SGET_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002365 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002366 break;
2367 case Instruction::SGET_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002368 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false,
2369 true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002370 break;
2371
2372 case Instruction::SPUT_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002373 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002374 break;
2375 case Instruction::SPUT_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002376 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002377 break;
2378 case Instruction::SPUT_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002379 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002380 break;
2381 case Instruction::SPUT_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002382 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002383 break;
2384 case Instruction::SPUT:
Andreas Gampe896df402014-10-20 22:25:29 -07002385 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002386 break;
2387 case Instruction::SPUT_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002388 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002389 break;
2390 case Instruction::SPUT_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002391 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false,
2392 true);
jeffhaobdb76512011-09-07 11:43:16 -07002393 break;
2394
2395 case Instruction::INVOKE_VIRTUAL:
2396 case Instruction::INVOKE_VIRTUAL_RANGE:
2397 case Instruction::INVOKE_SUPER:
Ian Rogersd81871c2011-10-03 13:57:23 -07002398 case Instruction::INVOKE_SUPER_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002399 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE ||
2400 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002401 bool is_super = (inst->Opcode() == Instruction::INVOKE_SUPER ||
2402 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002403 ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_VIRTUAL, is_range, is_super);
Ian Rogersd8f69b02014-09-10 21:43:52 +00002404 const RegType* return_type = nullptr;
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002405 if (called_method != nullptr) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002406 StackHandleScope<1> hs(self_);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002407 mirror::Class* return_type_class = called_method->GetReturnType(can_load_classes_);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002408 if (return_type_class != nullptr) {
Andreas Gampef23f33d2015-06-23 14:18:17 -07002409 return_type = &FromClass(called_method->GetReturnTypeDescriptor(),
2410 return_type_class,
2411 return_type_class->CannotBeAssignedFromOtherTypes());
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002412 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07002413 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
2414 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002415 }
2416 }
2417 if (return_type == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002418 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002419 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2420 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002421 const char* descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Mathieu Chartierbf99f772014-08-23 16:37:27 -07002422 return_type = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
jeffhaobdb76512011-09-07 11:43:16 -07002423 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002424 if (!return_type->IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002425 work_line_->SetResultRegisterType(this, *return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002426 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002427 work_line_->SetResultRegisterTypeWide(*return_type, return_type->HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002428 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002429 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002430 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002431 }
jeffhaobdb76512011-09-07 11:43:16 -07002432 case Instruction::INVOKE_DIRECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002433 case Instruction::INVOKE_DIRECT_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002434 bool is_range = (inst->Opcode() == Instruction::INVOKE_DIRECT_RANGE);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002435 ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_DIRECT, is_range, false);
Ian Rogers46685432012-06-03 22:26:43 -07002436 const char* return_type_descriptor;
2437 bool is_constructor;
Ian Rogersd8f69b02014-09-10 21:43:52 +00002438 const RegType* return_type = nullptr;
Ian Rogers7b078e82014-09-10 14:44:24 -07002439 if (called_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002440 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogers46685432012-06-03 22:26:43 -07002441 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
Ian Rogersdfb325e2013-10-30 01:00:44 -07002442 is_constructor = strcmp("<init>", dex_file_->StringDataByIdx(method_id.name_idx_)) == 0;
Ian Rogers46685432012-06-03 22:26:43 -07002443 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
2444 return_type_descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2445 } else {
2446 is_constructor = called_method->IsConstructor();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002447 return_type_descriptor = called_method->GetReturnTypeDescriptor();
Ian Rogers7b078e82014-09-10 14:44:24 -07002448 StackHandleScope<1> hs(self_);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002449 mirror::Class* return_type_class = called_method->GetReturnType(can_load_classes_);
Ian Rogers1ff3c982014-08-12 02:30:58 -07002450 if (return_type_class != nullptr) {
Andreas Gampef23f33d2015-06-23 14:18:17 -07002451 return_type = &FromClass(return_type_descriptor,
2452 return_type_class,
2453 return_type_class->CannotBeAssignedFromOtherTypes());
Ian Rogers1ff3c982014-08-12 02:30:58 -07002454 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07002455 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
2456 self_->ClearException();
Ian Rogers1ff3c982014-08-12 02:30:58 -07002457 }
Ian Rogers46685432012-06-03 22:26:43 -07002458 }
2459 if (is_constructor) {
jeffhaobdb76512011-09-07 11:43:16 -07002460 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002461 * Some additional checks when calling a constructor. We know from the invocation arg check
2462 * that the "this" argument is an instance of called_method->klass. Now we further restrict
2463 * that to require that called_method->klass is the same as this->klass or this->super,
2464 * allowing the latter only if the "this" argument is the same as the "this" argument to
2465 * this method (which implies that we're in a constructor ourselves).
jeffhaobdb76512011-09-07 11:43:16 -07002466 */
Ian Rogers7b078e82014-09-10 14:44:24 -07002467 const RegType& this_type = work_line_->GetInvocationThis(this, inst, is_range);
jeffhaob57e9522012-04-26 18:08:21 -07002468 if (this_type.IsConflict()) // failure.
2469 break;
jeffhaobdb76512011-09-07 11:43:16 -07002470
jeffhaob57e9522012-04-26 18:08:21 -07002471 /* no null refs allowed (?) */
2472 if (this_type.IsZero()) {
2473 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unable to initialize null ref";
2474 break;
jeffhao2a8a90e2011-09-26 14:25:31 -07002475 }
jeffhaob57e9522012-04-26 18:08:21 -07002476
2477 /* must be in same class or in superclass */
Ian Rogersd8f69b02014-09-10 21:43:52 +00002478 // const RegType& this_super_klass = this_type.GetSuperClass(&reg_types_);
Ian Rogers46685432012-06-03 22:26:43 -07002479 // TODO: re-enable constructor type verification
2480 // if (this_super_klass.IsConflict()) {
jeffhaob57e9522012-04-26 18:08:21 -07002481 // Unknown super class, fail so we re-check at runtime.
Ian Rogers46685432012-06-03 22:26:43 -07002482 // Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "super class unknown for '" << this_type << "'";
2483 // break;
2484 // }
jeffhaob57e9522012-04-26 18:08:21 -07002485
2486 /* arg must be an uninitialized reference */
2487 if (!this_type.IsUninitializedTypes()) {
2488 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Expected initialization on uninitialized reference "
2489 << this_type;
2490 break;
2491 }
2492
2493 /*
2494 * Replace the uninitialized reference with an initialized one. We need to do this for all
2495 * registers that have the same object instance in them, not just the "this" register.
2496 */
Jeff Hao848f70a2014-01-15 13:49:50 -08002497 const uint32_t this_reg = (is_range) ? inst->VRegC_3rc() : inst->VRegC_35c();
2498 work_line_->MarkRefsAsInitialized(this, this_type, this_reg, work_insn_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002499 }
Ian Rogers1ff3c982014-08-12 02:30:58 -07002500 if (return_type == nullptr) {
Mathieu Chartierbf99f772014-08-23 16:37:27 -07002501 return_type = &reg_types_.FromDescriptor(GetClassLoader(), return_type_descriptor,
2502 false);
Ian Rogers1ff3c982014-08-12 02:30:58 -07002503 }
2504 if (!return_type->IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002505 work_line_->SetResultRegisterType(this, *return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002506 } else {
Ian Rogers1ff3c982014-08-12 02:30:58 -07002507 work_line_->SetResultRegisterTypeWide(*return_type, return_type->HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002508 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002509 just_set_result = true;
2510 break;
2511 }
2512 case Instruction::INVOKE_STATIC:
2513 case Instruction::INVOKE_STATIC_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002514 bool is_range = (inst->Opcode() == Instruction::INVOKE_STATIC_RANGE);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002515 ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_STATIC, is_range, false);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002516 const char* descriptor;
Ian Rogers7b078e82014-09-10 14:44:24 -07002517 if (called_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002518 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002519 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2520 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Mathieu Chartier590fee92013-09-13 13:46:47 -07002521 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002522 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002523 descriptor = called_method->GetReturnTypeDescriptor();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002524 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00002525 const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002526 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002527 work_line_->SetResultRegisterType(this, return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002528 } else {
2529 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2530 }
jeffhaobdb76512011-09-07 11:43:16 -07002531 just_set_result = true;
2532 }
2533 break;
jeffhaobdb76512011-09-07 11:43:16 -07002534 case Instruction::INVOKE_INTERFACE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002535 case Instruction::INVOKE_INTERFACE_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002536 bool is_range = (inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002537 ArtMethod* abs_method = VerifyInvocationArgs(inst, METHOD_INTERFACE, is_range, false);
Ian Rogers7b078e82014-09-10 14:44:24 -07002538 if (abs_method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002539 mirror::Class* called_interface = abs_method->GetDeclaringClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002540 if (!called_interface->IsInterface() && !called_interface->IsObjectClass()) {
2541 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected interface class in invoke-interface '"
2542 << PrettyMethod(abs_method) << "'";
2543 break;
Ian Rogers28ad40d2011-10-27 15:19:26 -07002544 }
Ian Rogers0d604842012-04-16 14:50:24 -07002545 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002546 /* Get the type of the "this" arg, which should either be a sub-interface of called
2547 * interface or Object (see comments in RegType::JoinClass).
2548 */
Ian Rogers7b078e82014-09-10 14:44:24 -07002549 const RegType& this_type = work_line_->GetInvocationThis(this, inst, is_range);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002550 if (this_type.IsZero()) {
2551 /* null pointer always passes (and always fails at runtime) */
2552 } else {
2553 if (this_type.IsUninitializedTypes()) {
2554 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface call on uninitialized object "
2555 << this_type;
2556 break;
2557 }
2558 // In the past we have tried to assert that "called_interface" is assignable
2559 // from "this_type.GetClass()", however, as we do an imprecise Join
2560 // (RegType::JoinClass) we don't have full information on what interfaces are
2561 // implemented by "this_type". For example, two classes may implement the same
2562 // interfaces and have a common parent that doesn't implement the interface. The
2563 // join will set "this_type" to the parent class and a test that this implements
2564 // the interface will incorrectly fail.
2565 }
2566 /*
2567 * We don't have an object instance, so we can't find the concrete method. However, all of
2568 * the type information is in the abstract method, so we're good.
2569 */
2570 const char* descriptor;
Ian Rogers7b078e82014-09-10 14:44:24 -07002571 if (abs_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002572 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002573 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2574 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
2575 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2576 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002577 descriptor = abs_method->GetReturnTypeDescriptor();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002578 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00002579 const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002580 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002581 work_line_->SetResultRegisterType(this, return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002582 } else {
2583 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2584 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002585 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002586 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002587 }
jeffhaobdb76512011-09-07 11:43:16 -07002588 case Instruction::NEG_INT:
2589 case Instruction::NOT_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002590 work_line_->CheckUnaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002591 break;
2592 case Instruction::NEG_LONG:
2593 case Instruction::NOT_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002594 work_line_->CheckUnaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002595 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002596 break;
2597 case Instruction::NEG_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002598 work_line_->CheckUnaryOp(this, inst, reg_types_.Float(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002599 break;
2600 case Instruction::NEG_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002601 work_line_->CheckUnaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002602 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002603 break;
2604 case Instruction::INT_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002605 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002606 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002607 break;
2608 case Instruction::INT_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002609 work_line_->CheckUnaryOp(this, inst, reg_types_.Float(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002610 break;
2611 case Instruction::INT_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002612 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002613 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002614 break;
2615 case Instruction::LONG_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002616 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002617 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002618 break;
2619 case Instruction::LONG_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002620 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002621 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002622 break;
2623 case Instruction::LONG_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002624 work_line_->CheckUnaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002625 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002626 break;
2627 case Instruction::FLOAT_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002628 work_line_->CheckUnaryOp(this, inst, reg_types_.Integer(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002629 break;
2630 case Instruction::FLOAT_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002631 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002632 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002633 break;
2634 case Instruction::FLOAT_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002635 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002636 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002637 break;
2638 case Instruction::DOUBLE_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002639 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002640 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002641 break;
2642 case Instruction::DOUBLE_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002643 work_line_->CheckUnaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002644 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002645 break;
2646 case Instruction::DOUBLE_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002647 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002648 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002649 break;
2650 case Instruction::INT_TO_BYTE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002651 work_line_->CheckUnaryOp(this, inst, reg_types_.Byte(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002652 break;
2653 case Instruction::INT_TO_CHAR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002654 work_line_->CheckUnaryOp(this, inst, reg_types_.Char(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002655 break;
2656 case Instruction::INT_TO_SHORT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002657 work_line_->CheckUnaryOp(this, inst, reg_types_.Short(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002658 break;
2659
2660 case Instruction::ADD_INT:
2661 case Instruction::SUB_INT:
2662 case Instruction::MUL_INT:
2663 case Instruction::REM_INT:
2664 case Instruction::DIV_INT:
2665 case Instruction::SHL_INT:
2666 case Instruction::SHR_INT:
2667 case Instruction::USHR_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002668 work_line_->CheckBinaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002669 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002670 break;
2671 case Instruction::AND_INT:
2672 case Instruction::OR_INT:
2673 case Instruction::XOR_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002674 work_line_->CheckBinaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002675 reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002676 break;
2677 case Instruction::ADD_LONG:
2678 case Instruction::SUB_LONG:
2679 case Instruction::MUL_LONG:
2680 case Instruction::DIV_LONG:
2681 case Instruction::REM_LONG:
2682 case Instruction::AND_LONG:
2683 case Instruction::OR_LONG:
2684 case Instruction::XOR_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002685 work_line_->CheckBinaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002686 reg_types_.LongLo(), reg_types_.LongHi(),
2687 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002688 break;
2689 case Instruction::SHL_LONG:
2690 case Instruction::SHR_LONG:
2691 case Instruction::USHR_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002692 /* shift distance is Int, making these different from other binary operations */
Ian Rogers7b078e82014-09-10 14:44:24 -07002693 work_line_->CheckBinaryOpWideShift(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002694 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002695 break;
2696 case Instruction::ADD_FLOAT:
2697 case Instruction::SUB_FLOAT:
2698 case Instruction::MUL_FLOAT:
2699 case Instruction::DIV_FLOAT:
2700 case Instruction::REM_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002701 work_line_->CheckBinaryOp(this, inst, reg_types_.Float(), reg_types_.Float(),
2702 reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002703 break;
2704 case Instruction::ADD_DOUBLE:
2705 case Instruction::SUB_DOUBLE:
2706 case Instruction::MUL_DOUBLE:
2707 case Instruction::DIV_DOUBLE:
2708 case Instruction::REM_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002709 work_line_->CheckBinaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002710 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2711 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002712 break;
2713 case Instruction::ADD_INT_2ADDR:
2714 case Instruction::SUB_INT_2ADDR:
2715 case Instruction::MUL_INT_2ADDR:
2716 case Instruction::REM_INT_2ADDR:
2717 case Instruction::SHL_INT_2ADDR:
2718 case Instruction::SHR_INT_2ADDR:
2719 case Instruction::USHR_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002720 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
2721 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002722 break;
2723 case Instruction::AND_INT_2ADDR:
2724 case Instruction::OR_INT_2ADDR:
2725 case Instruction::XOR_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002726 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
2727 reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002728 break;
2729 case Instruction::DIV_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002730 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
2731 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002732 break;
2733 case Instruction::ADD_LONG_2ADDR:
2734 case Instruction::SUB_LONG_2ADDR:
2735 case Instruction::MUL_LONG_2ADDR:
2736 case Instruction::DIV_LONG_2ADDR:
2737 case Instruction::REM_LONG_2ADDR:
2738 case Instruction::AND_LONG_2ADDR:
2739 case Instruction::OR_LONG_2ADDR:
2740 case Instruction::XOR_LONG_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002741 work_line_->CheckBinaryOp2addrWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002742 reg_types_.LongLo(), reg_types_.LongHi(),
2743 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002744 break;
2745 case Instruction::SHL_LONG_2ADDR:
2746 case Instruction::SHR_LONG_2ADDR:
2747 case Instruction::USHR_LONG_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002748 work_line_->CheckBinaryOp2addrWideShift(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002749 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002750 break;
2751 case Instruction::ADD_FLOAT_2ADDR:
2752 case Instruction::SUB_FLOAT_2ADDR:
2753 case Instruction::MUL_FLOAT_2ADDR:
2754 case Instruction::DIV_FLOAT_2ADDR:
2755 case Instruction::REM_FLOAT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002756 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Float(), reg_types_.Float(),
2757 reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002758 break;
2759 case Instruction::ADD_DOUBLE_2ADDR:
2760 case Instruction::SUB_DOUBLE_2ADDR:
2761 case Instruction::MUL_DOUBLE_2ADDR:
2762 case Instruction::DIV_DOUBLE_2ADDR:
2763 case Instruction::REM_DOUBLE_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002764 work_line_->CheckBinaryOp2addrWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002765 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2766 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002767 break;
2768 case Instruction::ADD_INT_LIT16:
Ian Rogersf72a11d2014-10-30 15:41:08 -07002769 case Instruction::RSUB_INT_LIT16:
jeffhaobdb76512011-09-07 11:43:16 -07002770 case Instruction::MUL_INT_LIT16:
2771 case Instruction::DIV_INT_LIT16:
2772 case Instruction::REM_INT_LIT16:
Ian Rogers7b078e82014-09-10 14:44:24 -07002773 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), false,
2774 true);
jeffhaobdb76512011-09-07 11:43:16 -07002775 break;
2776 case Instruction::AND_INT_LIT16:
2777 case Instruction::OR_INT_LIT16:
2778 case Instruction::XOR_INT_LIT16:
Ian Rogers7b078e82014-09-10 14:44:24 -07002779 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), true,
2780 true);
jeffhaobdb76512011-09-07 11:43:16 -07002781 break;
2782 case Instruction::ADD_INT_LIT8:
2783 case Instruction::RSUB_INT_LIT8:
2784 case Instruction::MUL_INT_LIT8:
2785 case Instruction::DIV_INT_LIT8:
2786 case Instruction::REM_INT_LIT8:
2787 case Instruction::SHL_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002788 case Instruction::SHR_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002789 case Instruction::USHR_INT_LIT8:
Ian Rogers7b078e82014-09-10 14:44:24 -07002790 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), false,
2791 false);
jeffhaobdb76512011-09-07 11:43:16 -07002792 break;
2793 case Instruction::AND_INT_LIT8:
2794 case Instruction::OR_INT_LIT8:
2795 case Instruction::XOR_INT_LIT8:
Ian Rogers7b078e82014-09-10 14:44:24 -07002796 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), true,
2797 false);
jeffhaobdb76512011-09-07 11:43:16 -07002798 break;
2799
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002800 // Special instructions.
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07002801 case Instruction::RETURN_VOID_NO_BARRIER:
2802 if (IsConstructor() && !IsStatic()) {
2803 auto& declaring_class = GetDeclaringClass();
Andreas Gampe68df3202015-06-22 11:35:46 -07002804 if (declaring_class.IsUnresolvedReference()) {
2805 // We must iterate over the fields, even if we cannot use mirror classes to do so. Do it
2806 // manually over the underlying dex file.
2807 uint32_t first_index = GetFirstFinalInstanceFieldIndex(*dex_file_,
2808 dex_file_->GetMethodId(dex_method_idx_).class_idx_);
2809 if (first_index != DexFile::kDexNoIndex) {
2810 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void-no-barrier not expected for field "
2811 << first_index;
2812 }
2813 break;
2814 }
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07002815 auto* klass = declaring_class.GetClass();
2816 for (uint32_t i = 0, num_fields = klass->NumInstanceFields(); i < num_fields; ++i) {
2817 if (klass->GetInstanceField(i)->IsFinal()) {
Mathieu Chartiere86deef2015-03-19 13:43:37 -07002818 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void-no-barrier not expected for "
2819 << PrettyField(klass->GetInstanceField(i));
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07002820 break;
2821 }
2822 }
Sebastien Hertzcc10e0e2013-06-28 14:24:48 +02002823 }
2824 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002825 // Note: the following instructions encode offsets derived from class linking.
2826 // As such they use Class*/Field*/AbstractMethod* as these offsets only have
2827 // meaning if the class linking and resolution were successful.
2828 case Instruction::IGET_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002829 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002830 break;
2831 case Instruction::IGET_WIDE_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002832 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002833 break;
2834 case Instruction::IGET_OBJECT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002835 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002836 break;
Mathieu Chartierffc605c2014-12-10 10:35:44 -08002837 case Instruction::IGET_BOOLEAN_QUICK:
2838 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true);
2839 break;
2840 case Instruction::IGET_BYTE_QUICK:
2841 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true);
2842 break;
2843 case Instruction::IGET_CHAR_QUICK:
2844 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true);
2845 break;
2846 case Instruction::IGET_SHORT_QUICK:
2847 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true);
2848 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002849 case Instruction::IPUT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002850 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002851 break;
Fred Shih37f05ef2014-07-16 18:38:08 -07002852 case Instruction::IPUT_BOOLEAN_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002853 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07002854 break;
2855 case Instruction::IPUT_BYTE_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002856 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07002857 break;
2858 case Instruction::IPUT_CHAR_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002859 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07002860 break;
2861 case Instruction::IPUT_SHORT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002862 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07002863 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002864 case Instruction::IPUT_WIDE_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002865 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002866 break;
2867 case Instruction::IPUT_OBJECT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002868 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002869 break;
2870 case Instruction::INVOKE_VIRTUAL_QUICK:
2871 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: {
2872 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002873 ArtMethod* called_method = VerifyInvokeVirtualQuickArgs(inst, is_range);
Ian Rogers7b078e82014-09-10 14:44:24 -07002874 if (called_method != nullptr) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002875 const char* descriptor = called_method->GetReturnTypeDescriptor();
Ian Rogersd8f69b02014-09-10 21:43:52 +00002876 const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002877 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002878 work_line_->SetResultRegisterType(this, return_type);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002879 } else {
2880 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2881 }
2882 just_set_result = true;
2883 }
2884 break;
2885 }
Igor Murashkin158f35c2015-06-10 15:55:30 -07002886 case Instruction::INVOKE_LAMBDA: {
2887 // Don't bother verifying, instead the interpreter will take the slow path with access checks.
2888 // If the code would've normally hard-failed, then the interpreter will throw the
2889 // appropriate verification errors at runtime.
2890 Fail(VERIFY_ERROR_FORCE_INTERPRETER); // TODO(iam): implement invoke-lambda verification
2891 break;
2892 }
2893 case Instruction::CREATE_LAMBDA: {
2894 // Don't bother verifying, instead the interpreter will take the slow path with access checks.
2895 // If the code would've normally hard-failed, then the interpreter will throw the
2896 // appropriate verification errors at runtime.
2897 Fail(VERIFY_ERROR_FORCE_INTERPRETER); // TODO(iam): implement create-lambda verification
2898 break;
2899 }
2900
Igor Murashkin2ee54e22015-06-18 10:05:11 -07002901 case Instruction::UNUSED_F4:
2902 case Instruction::UNUSED_F5:
2903 case Instruction::UNUSED_F7: {
Igor Murashkin158f35c2015-06-10 15:55:30 -07002904 DCHECK(false); // TODO(iam): Implement opcodes for lambdas
Igor Murashkin2ee54e22015-06-18 10:05:11 -07002905 // Conservatively fail verification on release builds.
2906 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Unexpected opcode " << inst->DumpString(dex_file_);
2907 break;
2908 }
2909
2910 case Instruction::BOX_LAMBDA: {
2911 // Don't bother verifying, instead the interpreter will take the slow path with access checks.
2912 // If the code would've normally hard-failed, then the interpreter will throw the
2913 // appropriate verification errors at runtime.
2914 Fail(VERIFY_ERROR_FORCE_INTERPRETER); // TODO(iam): implement box-lambda verification
2915 break;
2916 }
2917
2918 case Instruction::UNBOX_LAMBDA: {
2919 // Don't bother verifying, instead the interpreter will take the slow path with access checks.
2920 // If the code would've normally hard-failed, then the interpreter will throw the
2921 // appropriate verification errors at runtime.
2922 Fail(VERIFY_ERROR_FORCE_INTERPRETER); // TODO(iam): implement unbox-lambda verification
2923 break;
Igor Murashkin158f35c2015-06-10 15:55:30 -07002924 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002925
Ian Rogersd81871c2011-10-03 13:57:23 -07002926 /* These should never appear during verification. */
Mathieu Chartierffc605c2014-12-10 10:35:44 -08002927 case Instruction::UNUSED_3E ... Instruction::UNUSED_43:
Igor Murashkin158f35c2015-06-10 15:55:30 -07002928 case Instruction::UNUSED_FA ... Instruction::UNUSED_FF:
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002929 case Instruction::UNUSED_79:
2930 case Instruction::UNUSED_7A:
jeffhaod5347e02012-03-22 17:25:05 -07002931 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Unexpected opcode " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07002932 break;
2933
2934 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002935 * DO NOT add a "default" clause here. Without it the compiler will
jeffhaobdb76512011-09-07 11:43:16 -07002936 * complain if an instruction is missing (which is desirable).
2937 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002938 } // end - switch (dec_insn.opcode)
jeffhaobdb76512011-09-07 11:43:16 -07002939
Stephen Kyle7e541c92014-12-17 17:10:02 +00002940 /*
2941 * If we are in a constructor, and we had an UninitializedThis type
2942 * in a register somewhere, we need to make sure it wasn't overwritten.
2943 */
2944 if (track_uninitialized_this) {
2945 bool was_invoke_direct = (inst->Opcode() == Instruction::INVOKE_DIRECT ||
2946 inst->Opcode() == Instruction::INVOKE_DIRECT_RANGE);
2947 if (work_line_->WasUninitializedThisOverwritten(this, uninitialized_this_loc,
2948 was_invoke_direct)) {
2949 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
2950 << "Constructor failed to initialize this object";
2951 }
2952 }
2953
Ian Rogersad0b3a32012-04-16 14:50:24 -07002954 if (have_pending_hard_failure_) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08002955 if (Runtime::Current()->IsAotCompiler()) {
2956 /* When AOT compiling, check that the last failure is a hard failure */
Andreas Gampeb588f4c2015-05-26 13:35:39 -07002957 if (failures_[failures_.size() - 1] != VERIFY_ERROR_BAD_CLASS_HARD) {
2958 LOG(ERROR) << "Pending failures:";
2959 for (auto& error : failures_) {
2960 LOG(ERROR) << error;
2961 }
2962 for (auto& error_msg : failure_messages_) {
2963 LOG(ERROR) << error_msg->str();
2964 }
2965 LOG(FATAL) << "Pending hard failure, but last failure not hard.";
2966 }
Ian Rogerse1758fe2012-04-19 11:31:15 -07002967 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002968 /* immediate failure, reject class */
2969 info_messages_ << "Rejecting opcode " << inst->DumpString(dex_file_);
2970 return false;
jeffhaofaf459e2012-08-31 15:32:47 -07002971 } else if (have_pending_runtime_throw_failure_) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07002972 /* checking interpreter will throw, mark following code as unreachable */
jeffhaofaf459e2012-08-31 15:32:47 -07002973 opcode_flags = Instruction::kThrow;
jeffhaobdb76512011-09-07 11:43:16 -07002974 }
jeffhaobdb76512011-09-07 11:43:16 -07002975 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002976 * If we didn't just set the result register, clear it out. This ensures that you can only use
2977 * "move-result" immediately after the result is set. (We could check this statically, but it's
2978 * not expensive and it makes our debugging output cleaner.)
jeffhaobdb76512011-09-07 11:43:16 -07002979 */
2980 if (!just_set_result) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002981 work_line_->SetResultTypeToUnknown(this);
jeffhaobdb76512011-09-07 11:43:16 -07002982 }
2983
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002984
jeffhaobdb76512011-09-07 11:43:16 -07002985
2986 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002987 * Handle "branch". Tag the branch target.
jeffhaobdb76512011-09-07 11:43:16 -07002988 *
2989 * NOTE: instructions like Instruction::EQZ provide information about the
jeffhaod1f0fde2011-09-08 17:25:33 -07002990 * state of the register when the branch is taken or not taken. For example,
jeffhaobdb76512011-09-07 11:43:16 -07002991 * somebody could get a reference field, check it for zero, and if the
2992 * branch is taken immediately store that register in a boolean field
jeffhaod1f0fde2011-09-08 17:25:33 -07002993 * since the value is known to be zero. We do not currently account for
jeffhaobdb76512011-09-07 11:43:16 -07002994 * that, and will reject the code.
2995 *
2996 * TODO: avoid re-fetching the branch target
2997 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002998 if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002999 bool isConditional, selfOkay;
Ian Rogersd81871c2011-10-03 13:57:23 -07003000 if (!GetBranchOffset(work_insn_idx_, &branch_target, &isConditional, &selfOkay)) {
jeffhaobdb76512011-09-07 11:43:16 -07003001 /* should never happen after static verification */
jeffhaod5347e02012-03-22 17:25:05 -07003002 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad branch";
jeffhaobdb76512011-09-07 11:43:16 -07003003 return false;
3004 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08003005 DCHECK_EQ(isConditional, (opcode_flags & Instruction::kContinue) != 0);
Stephen Kyle9bc61992014-09-22 13:53:15 +01003006 if (!CheckNotMoveExceptionOrMoveResult(code_item_->insns_, work_insn_idx_ + branch_target)) {
jeffhaobdb76512011-09-07 11:43:16 -07003007 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07003008 }
jeffhaobdb76512011-09-07 11:43:16 -07003009 /* update branch target, set "changed" if appropriate */
Ian Rogers7b078e82014-09-10 14:44:24 -07003010 if (nullptr != branch_line.get()) {
Ian Rogersebbdd872014-07-07 23:53:08 -07003011 if (!UpdateRegisters(work_insn_idx_ + branch_target, branch_line.get(), false)) {
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003012 return false;
3013 }
3014 } else {
Ian Rogersebbdd872014-07-07 23:53:08 -07003015 if (!UpdateRegisters(work_insn_idx_ + branch_target, work_line_.get(), false)) {
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003016 return false;
3017 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003018 }
jeffhaobdb76512011-09-07 11:43:16 -07003019 }
3020
3021 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003022 * Handle "switch". Tag all possible branch targets.
jeffhaobdb76512011-09-07 11:43:16 -07003023 *
3024 * We've already verified that the table is structurally sound, so we
3025 * just need to walk through and tag the targets.
3026 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003027 if ((opcode_flags & Instruction::kSwitch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07003028 int offset_to_switch = insns[1] | (((int32_t) insns[2]) << 16);
3029 const uint16_t* switch_insns = insns + offset_to_switch;
3030 int switch_count = switch_insns[1];
3031 int offset_to_targets, targ;
3032
3033 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
3034 /* 0 = sig, 1 = count, 2/3 = first key */
3035 offset_to_targets = 4;
3036 } else {
3037 /* 0 = sig, 1 = count, 2..count * 2 = keys */
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07003038 DCHECK((*insns & 0xff) == Instruction::SPARSE_SWITCH);
jeffhaobdb76512011-09-07 11:43:16 -07003039 offset_to_targets = 2 + 2 * switch_count;
3040 }
3041
3042 /* verify each switch target */
3043 for (targ = 0; targ < switch_count; targ++) {
3044 int offset;
3045 uint32_t abs_offset;
3046
3047 /* offsets are 32-bit, and only partly endian-swapped */
3048 offset = switch_insns[offset_to_targets + targ * 2] |
3049 (((int32_t) switch_insns[offset_to_targets + targ * 2 + 1]) << 16);
Ian Rogersd81871c2011-10-03 13:57:23 -07003050 abs_offset = work_insn_idx_ + offset;
3051 DCHECK_LT(abs_offset, code_item_->insns_size_in_code_units_);
Stephen Kyle9bc61992014-09-22 13:53:15 +01003052 if (!CheckNotMoveExceptionOrMoveResult(code_item_->insns_, abs_offset)) {
jeffhaobdb76512011-09-07 11:43:16 -07003053 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07003054 }
Ian Rogersebbdd872014-07-07 23:53:08 -07003055 if (!UpdateRegisters(abs_offset, work_line_.get(), false)) {
jeffhaobdb76512011-09-07 11:43:16 -07003056 return false;
Ian Rogersebbdd872014-07-07 23:53:08 -07003057 }
jeffhaobdb76512011-09-07 11:43:16 -07003058 }
3059 }
3060
3061 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003062 * Handle instructions that can throw and that are sitting in a "try" block. (If they're not in a
3063 * "try" block when they throw, control transfers out of the method.)
jeffhaobdb76512011-09-07 11:43:16 -07003064 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003065 if ((opcode_flags & Instruction::kThrow) != 0 && insn_flags_[work_insn_idx_].IsInTry()) {
Andreas Gampef91baf12014-07-18 15:41:00 -07003066 bool has_catch_all_handler = false;
Ian Rogers0571d352011-11-03 19:51:38 -07003067 CatchHandlerIterator iterator(*code_item_, work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07003068
Andreas Gampef91baf12014-07-18 15:41:00 -07003069 // Need the linker to try and resolve the handled class to check if it's Throwable.
3070 ClassLinker* linker = Runtime::Current()->GetClassLinker();
3071
Ian Rogers0571d352011-11-03 19:51:38 -07003072 for (; iterator.HasNext(); iterator.Next()) {
Andreas Gampef91baf12014-07-18 15:41:00 -07003073 uint16_t handler_type_idx = iterator.GetHandlerTypeIndex();
3074 if (handler_type_idx == DexFile::kDexNoIndex16) {
3075 has_catch_all_handler = true;
3076 } else {
3077 // It is also a catch-all if it is java.lang.Throwable.
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003078 mirror::Class* klass = linker->ResolveType(*dex_file_, handler_type_idx, dex_cache_,
3079 class_loader_);
Andreas Gampef91baf12014-07-18 15:41:00 -07003080 if (klass != nullptr) {
3081 if (klass == mirror::Throwable::GetJavaLangThrowable()) {
3082 has_catch_all_handler = true;
3083 }
3084 } else {
3085 // Clear exception.
Ian Rogers7b078e82014-09-10 14:44:24 -07003086 DCHECK(self_->IsExceptionPending());
3087 self_->ClearException();
Andreas Gampef91baf12014-07-18 15:41:00 -07003088 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003089 }
jeffhaobdb76512011-09-07 11:43:16 -07003090 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003091 * Merge registers into the "catch" block. We want to use the "savedRegs" rather than
3092 * "work_regs", because at runtime the exception will be thrown before the instruction
3093 * modifies any registers.
jeffhaobdb76512011-09-07 11:43:16 -07003094 */
Ian Rogersebbdd872014-07-07 23:53:08 -07003095 if (!UpdateRegisters(iterator.GetHandlerAddress(), saved_line_.get(), false)) {
jeffhaobdb76512011-09-07 11:43:16 -07003096 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07003097 }
jeffhaobdb76512011-09-07 11:43:16 -07003098 }
3099
3100 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003101 * If the monitor stack depth is nonzero, there must be a "catch all" handler for this
3102 * instruction. This does apply to monitor-exit because of async exception handling.
jeffhaobdb76512011-09-07 11:43:16 -07003103 */
Andreas Gampef91baf12014-07-18 15:41:00 -07003104 if (work_line_->MonitorStackDepth() > 0 && !has_catch_all_handler) {
jeffhaobdb76512011-09-07 11:43:16 -07003105 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003106 * The state in work_line reflects the post-execution state. If the current instruction is a
3107 * monitor-enter and the monitor stack was empty, we don't need a catch-all (if it throws,
jeffhaobdb76512011-09-07 11:43:16 -07003108 * it will do so before grabbing the lock).
3109 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02003110 if (inst->Opcode() != Instruction::MONITOR_ENTER || work_line_->MonitorStackDepth() != 1) {
jeffhaod5347e02012-03-22 17:25:05 -07003111 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
Ian Rogersd81871c2011-10-03 13:57:23 -07003112 << "expected to be within a catch-all for an instruction where a monitor is held";
jeffhaobdb76512011-09-07 11:43:16 -07003113 return false;
3114 }
3115 }
3116 }
3117
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003118 /* Handle "continue". Tag the next consecutive instruction.
3119 * Note: Keep the code handling "continue" case below the "branch" and "switch" cases,
3120 * because it changes work_line_ when performing peephole optimization
3121 * and this change should not be used in those cases.
3122 */
Ian Rogers6d376ae2013-07-23 15:12:40 -07003123 if ((opcode_flags & Instruction::kContinue) != 0) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003124 DCHECK_EQ(Instruction::At(code_item_->insns_ + work_insn_idx_), inst);
3125 uint32_t next_insn_idx = work_insn_idx_ + inst->SizeInCodeUnits();
Ian Rogers6d376ae2013-07-23 15:12:40 -07003126 if (next_insn_idx >= code_item_->insns_size_in_code_units_) {
3127 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Execution can walk off end of code area";
3128 return false;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003129 }
Ian Rogers6d376ae2013-07-23 15:12:40 -07003130 // The only way to get to a move-exception instruction is to get thrown there. Make sure the
3131 // next instruction isn't one.
3132 if (!CheckNotMoveException(code_item_->insns_, next_insn_idx)) {
3133 return false;
3134 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003135 if (nullptr != fallthrough_line.get()) {
Ian Rogers6d376ae2013-07-23 15:12:40 -07003136 // Make workline consistent with fallthrough computed from peephole optimization.
3137 work_line_->CopyFromLine(fallthrough_line.get());
3138 }
Ian Rogersb8c78592013-07-25 23:52:52 +00003139 if (insn_flags_[next_insn_idx].IsReturn()) {
3140 // For returns we only care about the operand to the return, all other registers are dead.
3141 const Instruction* ret_inst = Instruction::At(code_item_->insns_ + next_insn_idx);
3142 Instruction::Code opcode = ret_inst->Opcode();
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07003143 if (opcode == Instruction::RETURN_VOID || opcode == Instruction::RETURN_VOID_NO_BARRIER) {
Stephen Kyle7e541c92014-12-17 17:10:02 +00003144 SafelyMarkAllRegistersAsConflicts(this, work_line_.get());
Ian Rogersb8c78592013-07-25 23:52:52 +00003145 } else {
3146 if (opcode == Instruction::RETURN_WIDE) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003147 work_line_->MarkAllRegistersAsConflictsExceptWide(this, ret_inst->VRegA_11x());
Ian Rogersb8c78592013-07-25 23:52:52 +00003148 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07003149 work_line_->MarkAllRegistersAsConflictsExcept(this, ret_inst->VRegA_11x());
Ian Rogersb8c78592013-07-25 23:52:52 +00003150 }
3151 }
3152 }
Ian Rogers6d376ae2013-07-23 15:12:40 -07003153 RegisterLine* next_line = reg_table_.GetLine(next_insn_idx);
Ian Rogers7b078e82014-09-10 14:44:24 -07003154 if (next_line != nullptr) {
Ian Rogersebbdd872014-07-07 23:53:08 -07003155 // Merge registers into what we have for the next instruction, and set the "changed" flag if
3156 // needed. If the merge changes the state of the registers then the work line will be
3157 // updated.
3158 if (!UpdateRegisters(next_insn_idx, work_line_.get(), true)) {
Ian Rogers6d376ae2013-07-23 15:12:40 -07003159 return false;
3160 }
3161 } else {
3162 /*
3163 * We're not recording register data for the next instruction, so we don't know what the
3164 * prior state was. We have to assume that something has changed and re-evaluate it.
3165 */
3166 insn_flags_[next_insn_idx].SetChanged();
3167 }
3168 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003169
jeffhaod1f0fde2011-09-08 17:25:33 -07003170 /* If we're returning from the method, make sure monitor stack is empty. */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003171 if ((opcode_flags & Instruction::kReturn) != 0) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003172 if (!work_line_->VerifyMonitorStackEmpty(this)) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003173 return false;
3174 }
jeffhaobdb76512011-09-07 11:43:16 -07003175 }
3176
3177 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003178 * Update start_guess. Advance to the next instruction of that's
3179 * possible, otherwise use the branch target if one was found. If
jeffhaobdb76512011-09-07 11:43:16 -07003180 * neither of those exists we're in a return or throw; leave start_guess
3181 * alone and let the caller sort it out.
3182 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003183 if ((opcode_flags & Instruction::kContinue) != 0) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003184 DCHECK_EQ(Instruction::At(code_item_->insns_ + work_insn_idx_), inst);
3185 *start_guess = work_insn_idx_ + inst->SizeInCodeUnits();
Elliott Hughesadb8c672012-03-06 16:49:32 -08003186 } else if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07003187 /* we're still okay if branch_target is zero */
Ian Rogersd81871c2011-10-03 13:57:23 -07003188 *start_guess = work_insn_idx_ + branch_target;
jeffhaobdb76512011-09-07 11:43:16 -07003189 }
3190
Ian Rogersd81871c2011-10-03 13:57:23 -07003191 DCHECK_LT(*start_guess, code_item_->insns_size_in_code_units_);
3192 DCHECK(insn_flags_[*start_guess].IsOpcode());
jeffhaobdb76512011-09-07 11:43:16 -07003193
3194 return true;
Brian Carlstrom7934ac22013-07-26 10:54:15 -07003195} // NOLINT(readability/fn_size)
jeffhaobdb76512011-09-07 11:43:16 -07003196
Ian Rogersd8f69b02014-09-10 21:43:52 +00003197const RegType& MethodVerifier::ResolveClassAndCheckAccess(uint32_t class_idx) {
Ian Rogers0571d352011-11-03 19:51:38 -07003198 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
Ian Rogersd8f69b02014-09-10 21:43:52 +00003199 const RegType& referrer = GetDeclaringClass();
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003200 mirror::Class* klass = dex_cache_->GetResolvedType(class_idx);
Ian Rogers7b078e82014-09-10 14:44:24 -07003201 const RegType& result = klass != nullptr ?
Andreas Gampef23f33d2015-06-23 14:18:17 -07003202 FromClass(descriptor, klass, klass->CannotBeAssignedFromOtherTypes()) :
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003203 reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003204 if (result.IsConflict()) {
3205 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "accessing broken descriptor '" << descriptor
3206 << "' in " << referrer;
3207 return result;
3208 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003209 if (klass == nullptr && !result.IsUnresolvedTypes()) {
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003210 dex_cache_->SetResolvedType(class_idx, result.GetClass());
Ian Rogerse1758fe2012-04-19 11:31:15 -07003211 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003212 // Check if access is allowed. Unresolved types use xxxWithAccessCheck to
Jeff Haob24b4a72013-07-31 13:47:31 -07003213 // check at runtime if access is allowed and so pass here. If result is
3214 // primitive, skip the access check.
3215 if (result.IsNonZeroReferenceTypes() && !result.IsUnresolvedTypes() &&
3216 !referrer.IsUnresolvedTypes() && !referrer.CanAccess(result)) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07003217 Fail(VERIFY_ERROR_ACCESS_CLASS) << "illegal class access: '"
Ian Rogersad0b3a32012-04-16 14:50:24 -07003218 << referrer << "' -> '" << result << "'";
Ian Rogers28ad40d2011-10-27 15:19:26 -07003219 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003220 return result;
Ian Rogersd81871c2011-10-03 13:57:23 -07003221}
3222
Ian Rogersd8f69b02014-09-10 21:43:52 +00003223const RegType& MethodVerifier::GetCaughtExceptionType() {
Ian Rogers7b078e82014-09-10 14:44:24 -07003224 const RegType* common_super = nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003225 if (code_item_->tries_size_ != 0) {
Ian Rogers13735952014-10-08 12:43:28 -07003226 const uint8_t* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
Ian Rogersd81871c2011-10-03 13:57:23 -07003227 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
3228 for (uint32_t i = 0; i < handlers_size; i++) {
Ian Rogers0571d352011-11-03 19:51:38 -07003229 CatchHandlerIterator iterator(handlers_ptr);
3230 for (; iterator.HasNext(); iterator.Next()) {
3231 if (iterator.GetHandlerAddress() == (uint32_t) work_insn_idx_) {
3232 if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) {
Ian Rogersb4903572012-10-11 11:52:56 -07003233 common_super = &reg_types_.JavaLangThrowable(false);
Ian Rogersd81871c2011-10-03 13:57:23 -07003234 } else {
Ian Rogersd8f69b02014-09-10 21:43:52 +00003235 const RegType& exception = ResolveClassAndCheckAccess(iterator.GetHandlerTypeIndex());
Jeff Haob878f212014-04-24 16:25:36 -07003236 if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(exception)) {
Jeff Haoc26a56c2013-11-04 12:00:47 -08003237 if (exception.IsUnresolvedTypes()) {
3238 // We don't know enough about the type. Fail here and let runtime handle it.
3239 Fail(VERIFY_ERROR_NO_CLASS) << "unresolved exception class " << exception;
3240 return exception;
3241 } else {
3242 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unexpected non-exception class " << exception;
3243 return reg_types_.Conflict();
3244 }
Jeff Haob878f212014-04-24 16:25:36 -07003245 } else if (common_super == nullptr) {
3246 common_super = &exception;
Ian Rogers28ad40d2011-10-27 15:19:26 -07003247 } else if (common_super->Equals(exception)) {
Ian Rogersc4762272012-02-01 15:55:55 -08003248 // odd case, but nothing to do
Ian Rogersd81871c2011-10-03 13:57:23 -07003249 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07003250 common_super = &common_super->Merge(exception, &reg_types_);
Andreas Gampe7c038102014-10-27 20:08:46 -07003251 if (FailOrAbort(this,
3252 reg_types_.JavaLangThrowable(false).IsAssignableFrom(*common_super),
3253 "java.lang.Throwable is not assignable-from common_super at ",
3254 work_insn_idx_)) {
3255 break;
3256 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003257 }
3258 }
3259 }
3260 }
Ian Rogers0571d352011-11-03 19:51:38 -07003261 handlers_ptr = iterator.EndDataPointer();
Ian Rogersd81871c2011-10-03 13:57:23 -07003262 }
3263 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003264 if (common_super == nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003265 /* no catch blocks, or no catches with classes we can find */
jeffhaod5347e02012-03-22 17:25:05 -07003266 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unable to find exception handler";
Ian Rogersad0b3a32012-04-16 14:50:24 -07003267 return reg_types_.Conflict();
Ian Rogersd81871c2011-10-03 13:57:23 -07003268 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07003269 return *common_super;
Ian Rogersd81871c2011-10-03 13:57:23 -07003270}
3271
Mathieu Chartiere401d142015-04-22 13:56:20 -07003272ArtMethod* MethodVerifier::ResolveMethodAndCheckAccess(
3273 uint32_t dex_method_idx, MethodType method_type) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003274 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx);
Ian Rogersd8f69b02014-09-10 21:43:52 +00003275 const RegType& klass_type = ResolveClassAndCheckAccess(method_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003276 if (klass_type.IsConflict()) {
3277 std::string append(" in attempt to access method ");
3278 append += dex_file_->GetMethodName(method_id);
3279 AppendToLastFailMessage(append);
Ian Rogers7b078e82014-09-10 14:44:24 -07003280 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08003281 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003282 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003283 return nullptr; // Can't resolve Class so no more to do here
Ian Rogers90040192011-12-16 08:54:29 -08003284 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003285 mirror::Class* klass = klass_type.GetClass();
Ian Rogersd8f69b02014-09-10 21:43:52 +00003286 const RegType& referrer = GetDeclaringClass();
Mathieu Chartiere401d142015-04-22 13:56:20 -07003287 auto* cl = Runtime::Current()->GetClassLinker();
3288 auto pointer_size = cl->GetImagePointerSize();
3289 ArtMethod* res_method = dex_cache_->GetResolvedMethod(dex_method_idx, pointer_size);
Ian Rogers7b078e82014-09-10 14:44:24 -07003290 if (res_method == nullptr) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07003291 const char* name = dex_file_->GetMethodName(method_id);
Ian Rogersd91d6d62013-09-25 20:26:14 -07003292 const Signature signature = dex_file_->GetMethodSignature(method_id);
jeffhao8cd6dda2012-02-22 10:15:34 -08003293
3294 if (method_type == METHOD_DIRECT || method_type == METHOD_STATIC) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003295 res_method = klass->FindDirectMethod(name, signature, pointer_size);
jeffhao8cd6dda2012-02-22 10:15:34 -08003296 } else if (method_type == METHOD_INTERFACE) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003297 res_method = klass->FindInterfaceMethod(name, signature, pointer_size);
Ian Rogersd81871c2011-10-03 13:57:23 -07003298 } else {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003299 res_method = klass->FindVirtualMethod(name, signature, pointer_size);
Ian Rogersd81871c2011-10-03 13:57:23 -07003300 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003301 if (res_method != nullptr) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003302 dex_cache_->SetResolvedMethod(dex_method_idx, res_method, pointer_size);
Ian Rogersd81871c2011-10-03 13:57:23 -07003303 } else {
jeffhao8cd6dda2012-02-22 10:15:34 -08003304 // If a virtual or interface method wasn't found with the expected type, look in
3305 // the direct methods. This can happen when the wrong invoke type is used or when
3306 // a class has changed, and will be flagged as an error in later checks.
3307 if (method_type == METHOD_INTERFACE || method_type == METHOD_VIRTUAL) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003308 res_method = klass->FindDirectMethod(name, signature, pointer_size);
jeffhao8cd6dda2012-02-22 10:15:34 -08003309 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003310 if (res_method == nullptr) {
jeffhao8cd6dda2012-02-22 10:15:34 -08003311 Fail(VERIFY_ERROR_NO_METHOD) << "couldn't find method "
3312 << PrettyDescriptor(klass) << "." << name
3313 << " " << signature;
Ian Rogers7b078e82014-09-10 14:44:24 -07003314 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003315 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003316 }
3317 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003318 // Make sure calls to constructors are "direct". There are additional restrictions but we don't
3319 // enforce them here.
3320 if (res_method->IsConstructor() && method_type != METHOD_DIRECT) {
jeffhaod5347e02012-03-22 17:25:05 -07003321 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting non-direct call to constructor "
3322 << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003323 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003324 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003325 // Disallow any calls to class initializers.
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003326 if (res_method->IsClassInitializer()) {
jeffhaod5347e02012-03-22 17:25:05 -07003327 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting call to class initializer "
3328 << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003329 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003330 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003331 // Check if access is allowed.
Ian Rogersad0b3a32012-04-16 14:50:24 -07003332 if (!referrer.CanAccessMember(res_method->GetDeclaringClass(), res_method->GetAccessFlags())) {
jeffhao8cd6dda2012-02-22 10:15:34 -08003333 Fail(VERIFY_ERROR_ACCESS_METHOD) << "illegal method access (call " << PrettyMethod(res_method)
Ian Rogersad0b3a32012-04-16 14:50:24 -07003334 << " from " << referrer << ")";
jeffhaob57e9522012-04-26 18:08:21 -07003335 return res_method;
jeffhao8cd6dda2012-02-22 10:15:34 -08003336 }
jeffhaode0d9c92012-02-27 13:58:13 -08003337 // Check that invoke-virtual and invoke-super are not used on private methods of the same class.
3338 if (res_method->IsPrivate() && method_type == METHOD_VIRTUAL) {
jeffhaod5347e02012-03-22 17:25:05 -07003339 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invoke-super/virtual can't be used on private method "
3340 << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003341 return nullptr;
jeffhaode0d9c92012-02-27 13:58:13 -08003342 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003343 // Check that interface methods match interface classes.
3344 if (klass->IsInterface() && method_type != METHOD_INTERFACE) {
3345 Fail(VERIFY_ERROR_CLASS_CHANGE) << "non-interface method " << PrettyMethod(res_method)
3346 << " is in an interface class " << PrettyClass(klass);
Ian Rogers7b078e82014-09-10 14:44:24 -07003347 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003348 } else if (!klass->IsInterface() && method_type == METHOD_INTERFACE) {
3349 Fail(VERIFY_ERROR_CLASS_CHANGE) << "interface method " << PrettyMethod(res_method)
3350 << " is in a non-interface class " << PrettyClass(klass);
Ian Rogers7b078e82014-09-10 14:44:24 -07003351 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003352 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003353 // See if the method type implied by the invoke instruction matches the access flags for the
3354 // target method.
Brian Carlstrombe6fa5e2014-12-09 20:15:42 -08003355 if ((method_type == METHOD_DIRECT && (!res_method->IsDirect() || res_method->IsStatic())) ||
Ian Rogersd81871c2011-10-03 13:57:23 -07003356 (method_type == METHOD_STATIC && !res_method->IsStatic()) ||
3357 ((method_type == METHOD_VIRTUAL || method_type == METHOD_INTERFACE) && res_method->IsDirect())
3358 ) {
Ian Rogers2fc14272012-08-30 10:56:57 -07003359 Fail(VERIFY_ERROR_CLASS_CHANGE) << "invoke type (" << method_type << ") does not match method "
3360 " type of " << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003361 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003362 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003363 return res_method;
3364}
3365
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003366template <class T>
Mathieu Chartiere401d142015-04-22 13:56:20 -07003367ArtMethod* MethodVerifier::VerifyInvocationArgsFromIterator(
3368 T* it, const Instruction* inst, MethodType method_type, bool is_range, ArtMethod* res_method) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003369 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
3370 // match the call to the signature. Also, we might be calling through an abstract method
3371 // definition (which doesn't have register count values).
3372 const size_t expected_args = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
3373 /* caught by static verifier */
3374 DCHECK(is_range || expected_args <= 5);
3375 if (expected_args > code_item_->outs_size_) {
3376 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
3377 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
3378 return nullptr;
3379 }
3380
3381 uint32_t arg[5];
3382 if (!is_range) {
3383 inst->GetVarArgs(arg);
3384 }
3385 uint32_t sig_registers = 0;
3386
3387 /*
3388 * Check the "this" argument, which must be an instance of the class that declared the method.
3389 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
3390 * rigorous check here (which is okay since we have to do it at runtime).
3391 */
3392 if (method_type != METHOD_STATIC) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003393 const RegType& actual_arg_type = work_line_->GetInvocationThis(this, inst, is_range);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003394 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
3395 CHECK(have_pending_hard_failure_);
3396 return nullptr;
3397 }
3398 if (actual_arg_type.IsUninitializedReference()) {
3399 if (res_method) {
3400 if (!res_method->IsConstructor()) {
3401 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
3402 return nullptr;
3403 }
3404 } else {
3405 // Check whether the name of the called method is "<init>"
3406 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Jeff Hao0d087272014-08-04 14:47:17 -07003407 if (strcmp(dex_file_->GetMethodName(dex_file_->GetMethodId(method_idx)), "<init>") != 0) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003408 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
3409 return nullptr;
3410 }
3411 }
3412 }
3413 if (method_type != METHOD_INTERFACE && !actual_arg_type.IsZero()) {
Ian Rogersd8f69b02014-09-10 21:43:52 +00003414 const RegType* res_method_class;
Andreas Gamped9e23012015-06-04 22:19:58 -07003415 // Miranda methods have the declaring interface as their declaring class, not the abstract
3416 // class. It would be wrong to use this for the type check (interface type checks are
3417 // postponed to runtime).
3418 if (res_method != nullptr && !res_method->IsMiranda()) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003419 mirror::Class* klass = res_method->GetDeclaringClass();
Ian Rogers1ff3c982014-08-12 02:30:58 -07003420 std::string temp;
Andreas Gampef23f33d2015-06-23 14:18:17 -07003421 res_method_class = &FromClass(klass->GetDescriptor(&temp), klass,
3422 klass->CannotBeAssignedFromOtherTypes());
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003423 } else {
3424 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
3425 const uint16_t class_idx = dex_file_->GetMethodId(method_idx).class_idx_;
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003426 res_method_class = &reg_types_.FromDescriptor(GetClassLoader(),
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003427 dex_file_->StringByTypeIdx(class_idx),
3428 false);
3429 }
3430 if (!res_method_class->IsAssignableFrom(actual_arg_type)) {
3431 Fail(actual_arg_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS:
3432 VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type
3433 << "' not instance of '" << *res_method_class << "'";
3434 // Continue on soft failures. We need to find possible hard failures to avoid problems in
3435 // the compiler.
3436 if (have_pending_hard_failure_) {
3437 return nullptr;
3438 }
3439 }
3440 }
3441 sig_registers = 1;
3442 }
3443
3444 for ( ; it->HasNext(); it->Next()) {
3445 if (sig_registers >= expected_args) {
3446 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, expected " << inst->VRegA() <<
3447 " arguments, found " << sig_registers << " or more.";
3448 return nullptr;
3449 }
3450
3451 const char* param_descriptor = it->GetDescriptor();
3452
3453 if (param_descriptor == nullptr) {
3454 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation because of missing signature "
3455 "component";
3456 return nullptr;
3457 }
3458
Ian Rogersd8f69b02014-09-10 21:43:52 +00003459 const RegType& reg_type = reg_types_.FromDescriptor(GetClassLoader(), param_descriptor, false);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003460 uint32_t get_reg = is_range ? inst->VRegC_3rc() + static_cast<uint32_t>(sig_registers) :
3461 arg[sig_registers];
3462 if (reg_type.IsIntegralTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003463 const RegType& src_type = work_line_->GetRegisterType(this, get_reg);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003464 if (!src_type.IsIntegralTypes()) {
3465 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register v" << get_reg << " has type " << src_type
3466 << " but expected " << reg_type;
Andreas Gampeb588f4c2015-05-26 13:35:39 -07003467 return nullptr;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003468 }
Andreas Gampeda9badb2015-06-05 20:22:12 -07003469 } else {
3470 if (!work_line_->VerifyRegisterType(this, get_reg, reg_type)) {
3471 // Continue on soft failures. We need to find possible hard failures to avoid problems in
3472 // the compiler.
3473 if (have_pending_hard_failure_) {
3474 return nullptr;
3475 }
3476 } else if (reg_type.IsLongOrDoubleTypes()) {
3477 // Check that registers are consecutive (for non-range invokes). Invokes are the only
3478 // instructions not specifying register pairs by the first component, but require them
3479 // nonetheless. Only check when there's an actual register in the parameters. If there's
3480 // none, this will fail below.
3481 if (!is_range && sig_registers + 1 < expected_args) {
3482 uint32_t second_reg = arg[sig_registers + 1];
3483 if (second_reg != get_reg + 1) {
3484 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, long or double parameter "
3485 "at index " << sig_registers << " is not a pair: " << get_reg << " + "
3486 << second_reg << ".";
3487 return nullptr;
3488 }
3489 }
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003490 }
3491 }
3492 sig_registers += reg_type.IsLongOrDoubleTypes() ? 2 : 1;
3493 }
3494 if (expected_args != sig_registers) {
3495 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, expected " << expected_args <<
3496 " arguments, found " << sig_registers;
3497 return nullptr;
3498 }
3499 return res_method;
3500}
3501
3502void MethodVerifier::VerifyInvocationArgsUnresolvedMethod(const Instruction* inst,
3503 MethodType method_type,
3504 bool is_range) {
3505 // As the method may not have been resolved, make this static check against what we expect.
3506 // The main reason for this code block is to fail hard when we find an illegal use, e.g.,
3507 // wrong number of arguments or wrong primitive types, even if the method could not be resolved.
3508 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
3509 DexFileParameterIterator it(*dex_file_,
3510 dex_file_->GetProtoId(dex_file_->GetMethodId(method_idx).proto_idx_));
3511 VerifyInvocationArgsFromIterator<DexFileParameterIterator>(&it, inst, method_type, is_range,
3512 nullptr);
3513}
3514
3515class MethodParamListDescriptorIterator {
3516 public:
Mathieu Chartiere401d142015-04-22 13:56:20 -07003517 explicit MethodParamListDescriptorIterator(ArtMethod* res_method) :
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003518 res_method_(res_method), pos_(0), params_(res_method->GetParameterTypeList()),
3519 params_size_(params_ == nullptr ? 0 : params_->Size()) {
3520 }
3521
3522 bool HasNext() {
3523 return pos_ < params_size_;
3524 }
3525
3526 void Next() {
3527 ++pos_;
3528 }
3529
3530 const char* GetDescriptor() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
3531 return res_method_->GetTypeDescriptorFromTypeIdx(params_->GetTypeItem(pos_).type_idx_);
3532 }
3533
3534 private:
Mathieu Chartiere401d142015-04-22 13:56:20 -07003535 ArtMethod* res_method_;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003536 size_t pos_;
3537 const DexFile::TypeList* params_;
3538 const size_t params_size_;
3539};
3540
Mathieu Chartiere401d142015-04-22 13:56:20 -07003541ArtMethod* MethodVerifier::VerifyInvocationArgs(
3542 const Instruction* inst, MethodType method_type, bool is_range, bool is_super) {
jeffhao8cd6dda2012-02-22 10:15:34 -08003543 // Resolve the method. This could be an abstract or concrete method depending on what sort of call
3544 // we're making.
Sebastien Hertz5243e912013-05-21 10:55:07 +02003545 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Andreas Gampeacc4d2f2014-06-12 19:35:05 -07003546
Mathieu Chartiere401d142015-04-22 13:56:20 -07003547 ArtMethod* res_method = ResolveMethodAndCheckAccess(method_idx, method_type);
Ian Rogers7b078e82014-09-10 14:44:24 -07003548 if (res_method == nullptr) { // error or class is unresolved
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003549 // Check what we can statically.
3550 if (!have_pending_hard_failure_) {
3551 VerifyInvocationArgsUnresolvedMethod(inst, method_type, is_range);
3552 }
3553 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003554 }
3555
Ian Rogersd81871c2011-10-03 13:57:23 -07003556 // If we're using invoke-super(method), make sure that the executing method's class' superclass
3557 // has a vtable entry for the target method.
3558 if (is_super) {
3559 DCHECK(method_type == METHOD_VIRTUAL);
Ian Rogersd8f69b02014-09-10 21:43:52 +00003560 const RegType& super = GetDeclaringClass().GetSuperClass(&reg_types_);
Ian Rogers529781d2012-07-23 17:24:29 -07003561 if (super.IsUnresolvedTypes()) {
jeffhao4d8df822012-04-24 17:09:36 -07003562 Fail(VERIFY_ERROR_NO_METHOD) << "unknown super class in invoke-super from "
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003563 << PrettyMethod(dex_method_idx_, *dex_file_)
jeffhao4d8df822012-04-24 17:09:36 -07003564 << " to super " << PrettyMethod(res_method);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003565 return nullptr;
jeffhao4d8df822012-04-24 17:09:36 -07003566 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003567 mirror::Class* super_klass = super.GetClass();
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003568 if (res_method->GetMethodIndex() >= super_klass->GetVTableLength()) {
jeffhao4d8df822012-04-24 17:09:36 -07003569 Fail(VERIFY_ERROR_NO_METHOD) << "invalid invoke-super from "
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003570 << PrettyMethod(dex_method_idx_, *dex_file_)
jeffhao4d8df822012-04-24 17:09:36 -07003571 << " to super " << super
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003572 << "." << res_method->GetName()
3573 << res_method->GetSignature();
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003574 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003575 }
3576 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003577
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003578 // Process the target method's signature. This signature may or may not
3579 MethodParamListDescriptorIterator it(res_method);
3580 return VerifyInvocationArgsFromIterator<MethodParamListDescriptorIterator>(&it, inst, method_type,
3581 is_range, res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07003582}
3583
Mathieu Chartiere401d142015-04-22 13:56:20 -07003584ArtMethod* MethodVerifier::GetQuickInvokedMethod(const Instruction* inst, RegisterLine* reg_line,
3585 bool is_range, bool allow_failure) {
Mathieu Chartier091d2382015-03-06 10:59:06 -08003586 if (is_range) {
3587 DCHECK_EQ(inst->Opcode(), Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
3588 } else {
3589 DCHECK_EQ(inst->Opcode(), Instruction::INVOKE_VIRTUAL_QUICK);
3590 }
3591 const RegType& actual_arg_type = reg_line->GetInvocationThis(this, inst, is_range, allow_failure);
Ian Rogers9bc54402014-04-17 16:40:01 -07003592 if (!actual_arg_type.HasClass()) {
3593 VLOG(verifier) << "Failed to get mirror::Class* from '" << actual_arg_type << "'";
Andreas Gampe63981562014-04-17 12:28:43 -07003594 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003595 }
Ian Rogersa4cf1df2014-05-07 19:47:17 -07003596 mirror::Class* klass = actual_arg_type.GetClass();
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003597 mirror::Class* dispatch_class;
Ian Rogersa4cf1df2014-05-07 19:47:17 -07003598 if (klass->IsInterface()) {
3599 // Derive Object.class from Class.class.getSuperclass().
3600 mirror::Class* object_klass = klass->GetClass()->GetSuperClass();
Andreas Gampe7c038102014-10-27 20:08:46 -07003601 if (FailOrAbort(this, object_klass->IsObjectClass(),
Mathieu Chartier091d2382015-03-06 10:59:06 -08003602 "Failed to find Object class in quickened invoke receiver", work_insn_idx_)) {
Andreas Gampe7c038102014-10-27 20:08:46 -07003603 return nullptr;
3604 }
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003605 dispatch_class = object_klass;
Ian Rogersa4cf1df2014-05-07 19:47:17 -07003606 } else {
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003607 dispatch_class = klass;
Ian Rogersa4cf1df2014-05-07 19:47:17 -07003608 }
Mathieu Chartier091d2382015-03-06 10:59:06 -08003609 if (!dispatch_class->HasVTable()) {
3610 FailOrAbort(this, allow_failure, "Receiver class has no vtable for quickened invoke at ",
3611 work_insn_idx_);
Andreas Gampe7c038102014-10-27 20:08:46 -07003612 return nullptr;
3613 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003614 uint16_t vtable_index = is_range ? inst->VRegB_3rc() : inst->VRegB_35c();
Mathieu Chartiere401d142015-04-22 13:56:20 -07003615 auto* cl = Runtime::Current()->GetClassLinker();
3616 auto pointer_size = cl->GetImagePointerSize();
Mathieu Chartier091d2382015-03-06 10:59:06 -08003617 if (static_cast<int32_t>(vtable_index) >= dispatch_class->GetVTableLength()) {
3618 FailOrAbort(this, allow_failure,
3619 "Receiver class has not enough vtable slots for quickened invoke at ",
3620 work_insn_idx_);
Andreas Gampe7c038102014-10-27 20:08:46 -07003621 return nullptr;
3622 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07003623 ArtMethod* res_method = dispatch_class->GetVTableEntry(vtable_index, pointer_size);
Mathieu Chartier091d2382015-03-06 10:59:06 -08003624 if (self_->IsExceptionPending()) {
3625 FailOrAbort(this, allow_failure, "Unexpected exception pending for quickened invoke at ",
3626 work_insn_idx_);
Andreas Gampe7c038102014-10-27 20:08:46 -07003627 return nullptr;
3628 }
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003629 return res_method;
3630}
3631
Mathieu Chartiere401d142015-04-22 13:56:20 -07003632ArtMethod* MethodVerifier::VerifyInvokeVirtualQuickArgs(const Instruction* inst, bool is_range) {
Andreas Gampe76bd8802014-12-10 16:43:58 -08003633 DCHECK(Runtime::Current()->IsStarted() || verify_to_dump_)
3634 << PrettyMethod(dex_method_idx_, *dex_file_, true) << "@" << work_insn_idx_;
3635
Mathieu Chartiere401d142015-04-22 13:56:20 -07003636 ArtMethod* res_method = GetQuickInvokedMethod(inst, work_line_.get(), is_range, false);
Ian Rogers7b078e82014-09-10 14:44:24 -07003637 if (res_method == nullptr) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003638 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer method from " << inst->Name();
Ian Rogers7b078e82014-09-10 14:44:24 -07003639 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003640 }
Andreas Gampe7c038102014-10-27 20:08:46 -07003641 if (FailOrAbort(this, !res_method->IsDirect(), "Quick-invoked method is direct at ",
3642 work_insn_idx_)) {
3643 return nullptr;
3644 }
3645 if (FailOrAbort(this, !res_method->IsStatic(), "Quick-invoked method is static at ",
3646 work_insn_idx_)) {
3647 return nullptr;
3648 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003649
3650 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
3651 // match the call to the signature. Also, we might be calling through an abstract method
3652 // definition (which doesn't have register count values).
Ian Rogers7b078e82014-09-10 14:44:24 -07003653 const RegType& actual_arg_type = work_line_->GetInvocationThis(this, inst, is_range);
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003654 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
Ian Rogers7b078e82014-09-10 14:44:24 -07003655 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003656 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003657 const size_t expected_args = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
3658 /* caught by static verifier */
3659 DCHECK(is_range || expected_args <= 5);
3660 if (expected_args > code_item_->outs_size_) {
3661 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
3662 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
Ian Rogers7b078e82014-09-10 14:44:24 -07003663 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003664 }
3665
3666 /*
3667 * Check the "this" argument, which must be an instance of the class that declared the method.
3668 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
3669 * rigorous check here (which is okay since we have to do it at runtime).
3670 */
3671 if (actual_arg_type.IsUninitializedReference() && !res_method->IsConstructor()) {
3672 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
Ian Rogers7b078e82014-09-10 14:44:24 -07003673 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003674 }
3675 if (!actual_arg_type.IsZero()) {
3676 mirror::Class* klass = res_method->GetDeclaringClass();
Ian Rogers1ff3c982014-08-12 02:30:58 -07003677 std::string temp;
Ian Rogersd8f69b02014-09-10 21:43:52 +00003678 const RegType& res_method_class =
Andreas Gampef23f33d2015-06-23 14:18:17 -07003679 FromClass(klass->GetDescriptor(&temp), klass, klass->CannotBeAssignedFromOtherTypes());
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003680 if (!res_method_class.IsAssignableFrom(actual_arg_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07003681 Fail(actual_arg_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS :
3682 VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003683 << "' not instance of '" << res_method_class << "'";
Ian Rogers7b078e82014-09-10 14:44:24 -07003684 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003685 }
3686 }
3687 /*
3688 * Process the target method's signature. This signature may or may not
3689 * have been verified, so we can't assume it's properly formed.
3690 */
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003691 const DexFile::TypeList* params = res_method->GetParameterTypeList();
Ian Rogers7b078e82014-09-10 14:44:24 -07003692 size_t params_size = params == nullptr ? 0 : params->Size();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003693 uint32_t arg[5];
3694 if (!is_range) {
Ian Rogers29a26482014-05-02 15:27:29 -07003695 inst->GetVarArgs(arg);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003696 }
3697 size_t actual_args = 1;
3698 for (size_t param_index = 0; param_index < params_size; param_index++) {
3699 if (actual_args >= expected_args) {
3700 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invalid call to '" << PrettyMethod(res_method)
Brian Carlstrom93c33962013-07-26 10:37:43 -07003701 << "'. Expected " << expected_args
3702 << " arguments, processing argument " << actual_args
3703 << " (where longs/doubles count twice).";
Ian Rogers7b078e82014-09-10 14:44:24 -07003704 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003705 }
3706 const char* descriptor =
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003707 res_method->GetTypeDescriptorFromTypeIdx(params->GetTypeItem(param_index).type_idx_);
Ian Rogers7b078e82014-09-10 14:44:24 -07003708 if (descriptor == nullptr) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003709 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003710 << " missing signature component";
Ian Rogers7b078e82014-09-10 14:44:24 -07003711 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003712 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00003713 const RegType& reg_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003714 uint32_t get_reg = is_range ? inst->VRegC_3rc() + actual_args : arg[actual_args];
Ian Rogers7b078e82014-09-10 14:44:24 -07003715 if (!work_line_->VerifyRegisterType(this, get_reg, reg_type)) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003716 return res_method;
3717 }
3718 actual_args = reg_type.IsLongOrDoubleTypes() ? actual_args + 2 : actual_args + 1;
3719 }
3720 if (actual_args != expected_args) {
3721 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
3722 << " expected " << expected_args << " arguments, found " << actual_args;
Ian Rogers7b078e82014-09-10 14:44:24 -07003723 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003724 } else {
3725 return res_method;
3726 }
3727}
3728
Ian Rogers62342ec2013-06-11 10:26:37 -07003729void MethodVerifier::VerifyNewArray(const Instruction* inst, bool is_filled, bool is_range) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003730 uint32_t type_idx;
3731 if (!is_filled) {
3732 DCHECK_EQ(inst->Opcode(), Instruction::NEW_ARRAY);
3733 type_idx = inst->VRegC_22c();
3734 } else if (!is_range) {
3735 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY);
3736 type_idx = inst->VRegB_35c();
3737 } else {
3738 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY_RANGE);
3739 type_idx = inst->VRegB_3rc();
3740 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00003741 const RegType& res_type = ResolveClassAndCheckAccess(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003742 if (res_type.IsConflict()) { // bad class
3743 DCHECK_NE(failures_.size(), 0U);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003744 } else {
3745 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
3746 if (!res_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003747 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "new-array on non-array class " << res_type;
Ian Rogers0c4a5062012-02-03 15:18:59 -08003748 } else if (!is_filled) {
3749 /* make sure "size" register is valid type */
Ian Rogers7b078e82014-09-10 14:44:24 -07003750 work_line_->VerifyRegisterType(this, inst->VRegB_22c(), reg_types_.Integer());
Ian Rogers0c4a5062012-02-03 15:18:59 -08003751 /* set register type to array class */
Ian Rogersd8f69b02014-09-10 21:43:52 +00003752 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
Ian Rogers7b078e82014-09-10 14:44:24 -07003753 work_line_->SetRegisterType(this, inst->VRegA_22c(), precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003754 } else {
3755 // Verify each register. If "arg_count" is bad, VerifyRegisterType() will run off the end of
3756 // the list and fail. It's legal, if silly, for arg_count to be zero.
Ian Rogersd8f69b02014-09-10 21:43:52 +00003757 const RegType& expected_type = reg_types_.GetComponentType(res_type, GetClassLoader());
Sebastien Hertz5243e912013-05-21 10:55:07 +02003758 uint32_t arg_count = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
3759 uint32_t arg[5];
3760 if (!is_range) {
Ian Rogers29a26482014-05-02 15:27:29 -07003761 inst->GetVarArgs(arg);
Sebastien Hertz5243e912013-05-21 10:55:07 +02003762 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08003763 for (size_t ui = 0; ui < arg_count; ui++) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003764 uint32_t get_reg = is_range ? inst->VRegC_3rc() + ui : arg[ui];
Ian Rogers7b078e82014-09-10 14:44:24 -07003765 if (!work_line_->VerifyRegisterType(this, get_reg, expected_type)) {
3766 work_line_->SetResultRegisterType(this, reg_types_.Conflict());
Ian Rogers0c4a5062012-02-03 15:18:59 -08003767 return;
3768 }
3769 }
3770 // filled-array result goes into "result" register
Ian Rogersd8f69b02014-09-10 21:43:52 +00003771 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
Ian Rogers7b078e82014-09-10 14:44:24 -07003772 work_line_->SetResultRegisterType(this, precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003773 }
3774 }
3775}
3776
Sebastien Hertz5243e912013-05-21 10:55:07 +02003777void MethodVerifier::VerifyAGet(const Instruction* inst,
Ian Rogersd8f69b02014-09-10 21:43:52 +00003778 const RegType& insn_type, bool is_primitive) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003779 const RegType& index_type = work_line_->GetRegisterType(this, inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07003780 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003781 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07003782 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07003783 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegB_23x());
Ian Rogers89310de2012-02-01 13:47:30 -08003784 if (array_type.IsZero()) {
3785 // Null array class; this code path will fail at runtime. Infer a merge-able type from the
3786 // instruction type. TODO: have a proper notion of bottom here.
3787 if (!is_primitive || insn_type.IsCategory1Types()) {
3788 // Reference or category 1
Ian Rogers7b078e82014-09-10 14:44:24 -07003789 work_line_->SetRegisterType(this, inst->VRegA_23x(), reg_types_.Zero());
Ian Rogersd81871c2011-10-03 13:57:23 -07003790 } else {
Ian Rogers89310de2012-02-01 13:47:30 -08003791 // Category 2
Ian Rogers7b078e82014-09-10 14:44:24 -07003792 work_line_->SetRegisterTypeWide(this, inst->VRegA_23x(),
3793 reg_types_.FromCat2ConstLo(0, false),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003794 reg_types_.FromCat2ConstHi(0, false));
Ian Rogers89310de2012-02-01 13:47:30 -08003795 }
jeffhaofc3144e2012-02-01 17:21:15 -08003796 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003797 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aget";
Ian Rogers89310de2012-02-01 13:47:30 -08003798 } else {
3799 /* verify the class */
Ian Rogersd8f69b02014-09-10 21:43:52 +00003800 const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
jeffhaofc3144e2012-02-01 17:21:15 -08003801 if (!component_type.IsReferenceTypes() && !is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07003802 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08003803 << " source for aget-object";
3804 } else if (component_type.IsNonZeroReferenceTypes() && is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07003805 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08003806 << " source for category 1 aget";
3807 } else if (is_primitive && !insn_type.Equals(component_type) &&
3808 !((insn_type.IsInteger() && component_type.IsFloat()) ||
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003809 (insn_type.IsLong() && component_type.IsDouble()))) {
3810 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array type " << array_type
3811 << " incompatible with aget of type " << insn_type;
Ian Rogers89310de2012-02-01 13:47:30 -08003812 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003813 // Use knowledge of the field type which is stronger than the type inferred from the
3814 // instruction, which can't differentiate object types and ints from floats, longs from
3815 // doubles.
3816 if (!component_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003817 work_line_->SetRegisterType(this, inst->VRegA_23x(), component_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003818 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07003819 work_line_->SetRegisterTypeWide(this, inst->VRegA_23x(), component_type,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003820 component_type.HighHalf(&reg_types_));
3821 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003822 }
3823 }
3824 }
3825}
3826
Ian Rogersd8f69b02014-09-10 21:43:52 +00003827void MethodVerifier::VerifyPrimitivePut(const RegType& target_type, const RegType& insn_type,
Jeff Haofe1f7c82013-08-01 14:50:24 -07003828 const uint32_t vregA) {
3829 // Primitive assignability rules are weaker than regular assignability rules.
3830 bool instruction_compatible;
3831 bool value_compatible;
Ian Rogers7b078e82014-09-10 14:44:24 -07003832 const RegType& value_type = work_line_->GetRegisterType(this, vregA);
Jeff Haofe1f7c82013-08-01 14:50:24 -07003833 if (target_type.IsIntegralTypes()) {
Jeff Haoa4647482013-08-06 15:35:47 -07003834 instruction_compatible = target_type.Equals(insn_type);
Jeff Haofe1f7c82013-08-01 14:50:24 -07003835 value_compatible = value_type.IsIntegralTypes();
3836 } else if (target_type.IsFloat()) {
3837 instruction_compatible = insn_type.IsInteger(); // no put-float, so expect put-int
3838 value_compatible = value_type.IsFloatTypes();
3839 } else if (target_type.IsLong()) {
3840 instruction_compatible = insn_type.IsLong();
Andreas Gampe376fa682014-09-07 13:06:12 -07003841 // Additional register check: this is not checked statically (as part of VerifyInstructions),
3842 // as target_type depends on the resolved type of the field.
3843 if (instruction_compatible && work_line_->NumRegs() > vregA + 1) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003844 const RegType& value_type_hi = work_line_->GetRegisterType(this, vregA + 1);
Andreas Gampe376fa682014-09-07 13:06:12 -07003845 value_compatible = value_type.IsLongTypes() && value_type.CheckWidePair(value_type_hi);
3846 } else {
3847 value_compatible = false;
3848 }
Jeff Haofe1f7c82013-08-01 14:50:24 -07003849 } else if (target_type.IsDouble()) {
3850 instruction_compatible = insn_type.IsLong(); // no put-double, so expect put-long
Andreas Gampe376fa682014-09-07 13:06:12 -07003851 // Additional register check: this is not checked statically (as part of VerifyInstructions),
3852 // as target_type depends on the resolved type of the field.
3853 if (instruction_compatible && work_line_->NumRegs() > vregA + 1) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003854 const RegType& value_type_hi = work_line_->GetRegisterType(this, vregA + 1);
Andreas Gampe376fa682014-09-07 13:06:12 -07003855 value_compatible = value_type.IsDoubleTypes() && value_type.CheckWidePair(value_type_hi);
3856 } else {
3857 value_compatible = false;
3858 }
Jeff Haofe1f7c82013-08-01 14:50:24 -07003859 } else {
3860 instruction_compatible = false; // reference with primitive store
3861 value_compatible = false; // unused
3862 }
3863 if (!instruction_compatible) {
3864 // This is a global failure rather than a class change failure as the instructions and
3865 // the descriptors for the type should have been consistent within the same file at
3866 // compile time.
3867 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "put insn has type '" << insn_type
3868 << "' but expected type '" << target_type << "'";
3869 return;
3870 }
3871 if (!value_compatible) {
3872 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
3873 << " of type " << value_type << " but expected " << target_type << " for put";
3874 return;
3875 }
3876}
3877
Sebastien Hertz5243e912013-05-21 10:55:07 +02003878void MethodVerifier::VerifyAPut(const Instruction* inst,
Ian Rogersd8f69b02014-09-10 21:43:52 +00003879 const RegType& insn_type, bool is_primitive) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003880 const RegType& index_type = work_line_->GetRegisterType(this, inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07003881 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003882 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07003883 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07003884 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegB_23x());
Ian Rogers89310de2012-02-01 13:47:30 -08003885 if (array_type.IsZero()) {
Nicolas Geoffray66389fb2015-06-19 10:35:42 +01003886 // Null array type; this code path will fail at runtime.
3887 // Still check that the given value matches the instruction's type.
3888 work_line_->VerifyRegisterType(this, inst->VRegA_23x(), insn_type);
jeffhaofc3144e2012-02-01 17:21:15 -08003889 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003890 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aput";
Ian Rogers89310de2012-02-01 13:47:30 -08003891 } else {
Ian Rogersd8f69b02014-09-10 21:43:52 +00003892 const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
Jeff Haofe1f7c82013-08-01 14:50:24 -07003893 const uint32_t vregA = inst->VRegA_23x();
Jeff Haob24b4a72013-07-31 13:47:31 -07003894 if (is_primitive) {
Jeff Haofe1f7c82013-08-01 14:50:24 -07003895 VerifyPrimitivePut(component_type, insn_type, vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07003896 } else {
Jeff Haob24b4a72013-07-31 13:47:31 -07003897 if (!component_type.IsReferenceTypes()) {
3898 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
3899 << " source for aput-object";
3900 } else {
3901 // The instruction agrees with the type of array, confirm the value to be stored does too
3902 // Note: we use the instruction type (rather than the component type) for aput-object as
3903 // incompatible classes will be caught at runtime as an array store exception
Ian Rogers7b078e82014-09-10 14:44:24 -07003904 work_line_->VerifyRegisterType(this, vregA, insn_type);
Jeff Haob24b4a72013-07-31 13:47:31 -07003905 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003906 }
3907 }
3908 }
3909}
3910
Mathieu Chartierc7853442015-03-27 14:35:38 -07003911ArtField* MethodVerifier::GetStaticField(int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08003912 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3913 // Check access to class
Ian Rogersd8f69b02014-09-10 21:43:52 +00003914 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Brian Carlstrom7934ac22013-07-26 10:54:15 -07003915 if (klass_type.IsConflict()) { // bad class
Ian Rogersad0b3a32012-04-16 14:50:24 -07003916 AppendToLastFailMessage(StringPrintf(" in attempt to access static field %d (%s) in %s",
3917 field_idx, dex_file_->GetFieldName(field_id),
3918 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers7b078e82014-09-10 14:44:24 -07003919 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08003920 }
Elliott Hughesb25c3f62012-03-26 16:35:06 -07003921 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003922 return nullptr; // Can't resolve Class so no more to do here, will do checking at runtime.
Ian Rogers90040192011-12-16 08:54:29 -08003923 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07003924 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartierc7853442015-03-27 14:35:38 -07003925 ArtField* field = class_linker->ResolveFieldJLS(*dex_file_, field_idx, dex_cache_,
3926 class_loader_);
Ian Rogers7b078e82014-09-10 14:44:24 -07003927 if (field == nullptr) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07003928 VLOG(verifier) << "Unable to resolve static field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07003929 << dex_file_->GetFieldName(field_id) << ") in "
3930 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogers7b078e82014-09-10 14:44:24 -07003931 DCHECK(self_->IsExceptionPending());
3932 self_->ClearException();
3933 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003934 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
3935 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003936 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access static field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07003937 << " from " << GetDeclaringClass();
Ian Rogers7b078e82014-09-10 14:44:24 -07003938 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003939 } else if (!field->IsStatic()) {
3940 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field) << " to be static";
Ian Rogers7b078e82014-09-10 14:44:24 -07003941 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003942 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07003943 return field;
Ian Rogersd81871c2011-10-03 13:57:23 -07003944}
3945
Mathieu Chartierc7853442015-03-27 14:35:38 -07003946ArtField* MethodVerifier::GetInstanceField(const RegType& obj_type, int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08003947 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3948 // Check access to class
Ian Rogersd8f69b02014-09-10 21:43:52 +00003949 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003950 if (klass_type.IsConflict()) {
3951 AppendToLastFailMessage(StringPrintf(" in attempt to access instance field %d (%s) in %s",
3952 field_idx, dex_file_->GetFieldName(field_id),
3953 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers7b078e82014-09-10 14:44:24 -07003954 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08003955 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003956 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003957 return nullptr; // Can't resolve Class so no more to do here
Ian Rogers90040192011-12-16 08:54:29 -08003958 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07003959 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartierc7853442015-03-27 14:35:38 -07003960 ArtField* field = class_linker->ResolveFieldJLS(*dex_file_, field_idx, dex_cache_,
3961 class_loader_);
Ian Rogers7b078e82014-09-10 14:44:24 -07003962 if (field == nullptr) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07003963 VLOG(verifier) << "Unable to resolve instance field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07003964 << dex_file_->GetFieldName(field_id) << ") in "
3965 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogers7b078e82014-09-10 14:44:24 -07003966 DCHECK(self_->IsExceptionPending());
3967 self_->ClearException();
3968 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003969 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
3970 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003971 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access instance field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07003972 << " from " << GetDeclaringClass();
Ian Rogers7b078e82014-09-10 14:44:24 -07003973 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003974 } else if (field->IsStatic()) {
3975 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field)
3976 << " to not be static";
Ian Rogers7b078e82014-09-10 14:44:24 -07003977 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003978 } else if (obj_type.IsZero()) {
3979 // Cannot infer and check type, however, access will cause null pointer exception
3980 return field;
Stephen Kyle695c5982014-08-22 15:03:07 +01003981 } else if (!obj_type.IsReferenceTypes()) {
3982 // Trying to read a field from something that isn't a reference
3983 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance field access on object that has "
3984 << "non-reference type " << obj_type;
Ian Rogers7b078e82014-09-10 14:44:24 -07003985 return nullptr;
Ian Rogerse1758fe2012-04-19 11:31:15 -07003986 } else {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003987 mirror::Class* klass = field->GetDeclaringClass();
Ian Rogersd8f69b02014-09-10 21:43:52 +00003988 const RegType& field_klass =
Andreas Gampef23f33d2015-06-23 14:18:17 -07003989 FromClass(dex_file_->GetFieldDeclaringClassDescriptor(field_id),
3990 klass, klass->CannotBeAssignedFromOtherTypes());
Ian Rogersad0b3a32012-04-16 14:50:24 -07003991 if (obj_type.IsUninitializedTypes() &&
3992 (!IsConstructor() || GetDeclaringClass().Equals(obj_type) ||
3993 !field_klass.Equals(GetDeclaringClass()))) {
3994 // Field accesses through uninitialized references are only allowable for constructors where
3995 // the field is declared in this class
3996 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "cannot access instance field " << PrettyField(field)
Brian Carlstrom93c33962013-07-26 10:37:43 -07003997 << " of a not fully initialized object within the context"
3998 << " of " << PrettyMethod(dex_method_idx_, *dex_file_);
Ian Rogers7b078e82014-09-10 14:44:24 -07003999 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004000 } else if (!field_klass.IsAssignableFrom(obj_type)) {
4001 // Trying to access C1.field1 using reference of type C2, which is neither C1 or a sub-class
4002 // of C1. For resolution to occur the declared class of the field must be compatible with
4003 // obj_type, we've discovered this wasn't so, so report the field didn't exist.
4004 Fail(VERIFY_ERROR_NO_FIELD) << "cannot access instance field " << PrettyField(field)
4005 << " from object of type " << obj_type;
Ian Rogers7b078e82014-09-10 14:44:24 -07004006 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004007 } else {
4008 return field;
4009 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004010 }
4011}
4012
Andreas Gampe896df402014-10-20 22:25:29 -07004013template <MethodVerifier::FieldAccessType kAccType>
4014void MethodVerifier::VerifyISFieldAccess(const Instruction* inst, const RegType& insn_type,
4015 bool is_primitive, bool is_static) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02004016 uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
Mathieu Chartierc7853442015-03-27 14:35:38 -07004017 ArtField* field;
Ian Rogersb94a27b2011-10-26 00:33:41 -07004018 if (is_static) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07004019 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07004020 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004021 const RegType& object_type = work_line_->GetRegisterType(this, inst->VRegB_22c());
Ian Rogersf4028cc2011-11-02 14:56:39 -07004022 field = GetInstanceField(object_type, field_idx);
Andreas Gampe896df402014-10-20 22:25:29 -07004023 if (UNLIKELY(have_pending_hard_failure_)) {
4024 return;
4025 }
Ian Rogersb94a27b2011-10-26 00:33:41 -07004026 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00004027 const RegType* field_type = nullptr;
Ian Rogers7b078e82014-09-10 14:44:24 -07004028 if (field != nullptr) {
Andreas Gampe896df402014-10-20 22:25:29 -07004029 if (kAccType == FieldAccessType::kAccPut) {
4030 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
4031 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
4032 << " from other class " << GetDeclaringClass();
4033 return;
4034 }
4035 }
4036
Mathieu Chartierc7853442015-03-27 14:35:38 -07004037 mirror::Class* field_type_class =
4038 can_load_classes_ ? field->GetType<true>() : field->GetType<false>();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004039 if (field_type_class != nullptr) {
Andreas Gampef23f33d2015-06-23 14:18:17 -07004040 field_type = &FromClass(field->GetTypeDescriptor(), field_type_class,
4041 field_type_class->CannotBeAssignedFromOtherTypes());
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08004042 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004043 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
4044 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004045 }
Ian Rogers0d604842012-04-16 14:50:24 -07004046 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004047 if (field_type == nullptr) {
4048 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
4049 const char* descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
Mathieu Chartierbf99f772014-08-23 16:37:27 -07004050 field_type = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004051 }
Sebastien Hertz757b3042014-03-28 14:34:28 +01004052 DCHECK(field_type != nullptr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02004053 const uint32_t vregA = (is_static) ? inst->VRegA_21c() : inst->VRegA_22c();
Andreas Gampe896df402014-10-20 22:25:29 -07004054 static_assert(kAccType == FieldAccessType::kAccPut || kAccType == FieldAccessType::kAccGet,
4055 "Unexpected third access type");
4056 if (kAccType == FieldAccessType::kAccPut) {
4057 // sput or iput.
4058 if (is_primitive) {
4059 VerifyPrimitivePut(*field_type, insn_type, vregA);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004060 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004061 if (!insn_type.IsAssignableFrom(*field_type)) {
4062 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
4063 << " to be compatible with type '" << insn_type
4064 << "' but found type '" << *field_type
4065 << "' in put-object";
4066 return;
4067 }
4068 work_line_->VerifyRegisterType(this, vregA, *field_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004069 }
Andreas Gampe896df402014-10-20 22:25:29 -07004070 } else if (kAccType == FieldAccessType::kAccGet) {
4071 // sget or iget.
4072 if (is_primitive) {
4073 if (field_type->Equals(insn_type) ||
4074 (field_type->IsFloat() && insn_type.IsInteger()) ||
4075 (field_type->IsDouble() && insn_type.IsLong())) {
4076 // expected that read is of the correct primitive type or that int reads are reading
4077 // floats or long reads are reading doubles
4078 } else {
4079 // This is a global failure rather than a class change failure as the instructions and
4080 // the descriptors for the type should have been consistent within the same file at
4081 // compile time
4082 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
4083 << " to be of type '" << insn_type
4084 << "' but found type '" << *field_type << "' in get";
4085 return;
4086 }
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08004087 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004088 if (!insn_type.IsAssignableFrom(*field_type)) {
4089 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
4090 << " to be compatible with type '" << insn_type
4091 << "' but found type '" << *field_type
4092 << "' in get-object";
4093 work_line_->SetRegisterType(this, vregA, reg_types_.Conflict());
4094 return;
4095 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004096 }
Andreas Gampe896df402014-10-20 22:25:29 -07004097 if (!field_type->IsLowHalf()) {
4098 work_line_->SetRegisterType(this, vregA, *field_type);
4099 } else {
4100 work_line_->SetRegisterTypeWide(this, vregA, *field_type, field_type->HighHalf(&reg_types_));
4101 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07004102 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004103 LOG(FATAL) << "Unexpected case.";
Ian Rogersd81871c2011-10-03 13:57:23 -07004104 }
4105}
4106
Mathieu Chartierc7853442015-03-27 14:35:38 -07004107ArtField* MethodVerifier::GetQuickFieldAccess(const Instruction* inst,
Ian Rogers98379392014-02-24 16:53:16 -08004108 RegisterLine* reg_line) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08004109 DCHECK(IsInstructionIGetQuickOrIPutQuick(inst->Opcode())) << inst->Opcode();
Ian Rogers7b078e82014-09-10 14:44:24 -07004110 const RegType& object_type = reg_line->GetRegisterType(this, inst->VRegB_22c());
Ian Rogers9bc54402014-04-17 16:40:01 -07004111 if (!object_type.HasClass()) {
4112 VLOG(verifier) << "Failed to get mirror::Class* from '" << object_type << "'";
4113 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004114 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004115 uint32_t field_offset = static_cast<uint32_t>(inst->VRegC_22c());
Mathieu Chartierc7853442015-03-27 14:35:38 -07004116 ArtField* const f = ArtField::FindInstanceFieldWithOffset(object_type.GetClass(), field_offset);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08004117 DCHECK_EQ(f->GetOffset().Uint32Value(), field_offset);
Sebastien Hertz479fc1e2014-04-04 17:51:34 +02004118 if (f == nullptr) {
4119 VLOG(verifier) << "Failed to find instance field at offset '" << field_offset
4120 << "' from '" << PrettyDescriptor(object_type.GetClass()) << "'";
4121 }
4122 return f;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004123}
4124
Andreas Gampe896df402014-10-20 22:25:29 -07004125template <MethodVerifier::FieldAccessType kAccType>
4126void MethodVerifier::VerifyQuickFieldAccess(const Instruction* inst, const RegType& insn_type,
4127 bool is_primitive) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07004128 DCHECK(Runtime::Current()->IsStarted() || verify_to_dump_);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004129
Mathieu Chartierc7853442015-03-27 14:35:38 -07004130 ArtField* field = GetQuickFieldAccess(inst, work_line_.get());
Ian Rogers7b078e82014-09-10 14:44:24 -07004131 if (field == nullptr) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004132 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field from " << inst->Name();
4133 return;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004134 }
Andreas Gampe896df402014-10-20 22:25:29 -07004135
4136 // For an IPUT_QUICK, we now test for final flag of the field.
4137 if (kAccType == FieldAccessType::kAccPut) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004138 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
4139 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004140 << " from other class " << GetDeclaringClass();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004141 return;
4142 }
4143 }
Andreas Gampe896df402014-10-20 22:25:29 -07004144
4145 // Get the field type.
4146 const RegType* field_type;
4147 {
Mathieu Chartierc7853442015-03-27 14:35:38 -07004148 mirror::Class* field_type_class = can_load_classes_ ? field->GetType<true>() :
4149 field->GetType<false>();
Andreas Gampe896df402014-10-20 22:25:29 -07004150
4151 if (field_type_class != nullptr) {
Andreas Gampef23f33d2015-06-23 14:18:17 -07004152 field_type = &FromClass(field->GetTypeDescriptor(), field_type_class,
4153 field_type_class->CannotBeAssignedFromOtherTypes());
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004154 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004155 Thread* self = Thread::Current();
4156 DCHECK(!can_load_classes_ || self->IsExceptionPending());
4157 self->ClearException();
4158 field_type = &reg_types_.FromDescriptor(field->GetDeclaringClass()->GetClassLoader(),
4159 field->GetTypeDescriptor(), false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004160 }
Andreas Gampe896df402014-10-20 22:25:29 -07004161 if (field_type == nullptr) {
4162 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field type from " << inst->Name();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004163 return;
4164 }
Andreas Gampe896df402014-10-20 22:25:29 -07004165 }
4166
4167 const uint32_t vregA = inst->VRegA_22c();
4168 static_assert(kAccType == FieldAccessType::kAccPut || kAccType == FieldAccessType::kAccGet,
4169 "Unexpected third access type");
4170 if (kAccType == FieldAccessType::kAccPut) {
4171 if (is_primitive) {
4172 // Primitive field assignability rules are weaker than regular assignability rules
4173 bool instruction_compatible;
4174 bool value_compatible;
4175 const RegType& value_type = work_line_->GetRegisterType(this, vregA);
4176 if (field_type->IsIntegralTypes()) {
4177 instruction_compatible = insn_type.IsIntegralTypes();
4178 value_compatible = value_type.IsIntegralTypes();
4179 } else if (field_type->IsFloat()) {
4180 instruction_compatible = insn_type.IsInteger(); // no [is]put-float, so expect [is]put-int
4181 value_compatible = value_type.IsFloatTypes();
4182 } else if (field_type->IsLong()) {
4183 instruction_compatible = insn_type.IsLong();
4184 value_compatible = value_type.IsLongTypes();
4185 } else if (field_type->IsDouble()) {
4186 instruction_compatible = insn_type.IsLong(); // no [is]put-double, so expect [is]put-long
4187 value_compatible = value_type.IsDoubleTypes();
4188 } else {
4189 instruction_compatible = false; // reference field with primitive store
4190 value_compatible = false; // unused
4191 }
4192 if (!instruction_compatible) {
4193 // This is a global failure rather than a class change failure as the instructions and
4194 // the descriptors for the type should have been consistent within the same file at
4195 // compile time
4196 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
4197 << " to be of type '" << insn_type
4198 << "' but found type '" << *field_type
4199 << "' in put";
4200 return;
4201 }
4202 if (!value_compatible) {
4203 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
4204 << " of type " << value_type
4205 << " but expected " << *field_type
4206 << " for store to " << PrettyField(field) << " in put";
4207 return;
4208 }
4209 } else {
4210 if (!insn_type.IsAssignableFrom(*field_type)) {
4211 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
4212 << " to be compatible with type '" << insn_type
4213 << "' but found type '" << *field_type
4214 << "' in put-object";
4215 return;
4216 }
4217 work_line_->VerifyRegisterType(this, vregA, *field_type);
4218 }
4219 } else if (kAccType == FieldAccessType::kAccGet) {
4220 if (is_primitive) {
4221 if (field_type->Equals(insn_type) ||
4222 (field_type->IsFloat() && insn_type.IsIntegralTypes()) ||
4223 (field_type->IsDouble() && insn_type.IsLongTypes())) {
4224 // expected that read is of the correct primitive type or that int reads are reading
4225 // floats or long reads are reading doubles
4226 } else {
4227 // This is a global failure rather than a class change failure as the instructions and
4228 // the descriptors for the type should have been consistent within the same file at
4229 // compile time
4230 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
4231 << " to be of type '" << insn_type
4232 << "' but found type '" << *field_type << "' in Get";
4233 return;
4234 }
4235 } else {
4236 if (!insn_type.IsAssignableFrom(*field_type)) {
4237 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
4238 << " to be compatible with type '" << insn_type
4239 << "' but found type '" << *field_type
4240 << "' in get-object";
4241 work_line_->SetRegisterType(this, vregA, reg_types_.Conflict());
4242 return;
4243 }
4244 }
4245 if (!field_type->IsLowHalf()) {
4246 work_line_->SetRegisterType(this, vregA, *field_type);
4247 } else {
4248 work_line_->SetRegisterTypeWide(this, vregA, *field_type, field_type->HighHalf(&reg_types_));
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004249 }
4250 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004251 LOG(FATAL) << "Unexpected case.";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004252 }
4253}
4254
Ian Rogers776ac1f2012-04-13 23:36:36 -07004255bool MethodVerifier::CheckNotMoveException(const uint16_t* insns, int insn_idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004256 if ((insns[insn_idx] & 0xff) == Instruction::MOVE_EXCEPTION) {
jeffhaod5347e02012-03-22 17:25:05 -07004257 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-exception";
Ian Rogersd81871c2011-10-03 13:57:23 -07004258 return false;
4259 }
4260 return true;
4261}
4262
Stephen Kyle9bc61992014-09-22 13:53:15 +01004263bool MethodVerifier::CheckNotMoveResult(const uint16_t* insns, int insn_idx) {
4264 if (((insns[insn_idx] & 0xff) >= Instruction::MOVE_RESULT) &&
4265 ((insns[insn_idx] & 0xff) <= Instruction::MOVE_RESULT_OBJECT)) {
4266 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-result*";
4267 return false;
4268 }
4269 return true;
4270}
4271
4272bool MethodVerifier::CheckNotMoveExceptionOrMoveResult(const uint16_t* insns, int insn_idx) {
4273 return (CheckNotMoveException(insns, insn_idx) && CheckNotMoveResult(insns, insn_idx));
4274}
4275
Ian Rogersebbdd872014-07-07 23:53:08 -07004276bool MethodVerifier::UpdateRegisters(uint32_t next_insn, RegisterLine* merge_line,
4277 bool update_merge_line) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004278 bool changed = true;
4279 RegisterLine* target_line = reg_table_.GetLine(next_insn);
4280 if (!insn_flags_[next_insn].IsVisitedOrChanged()) {
jeffhaobdb76512011-09-07 11:43:16 -07004281 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07004282 * We haven't processed this instruction before, and we haven't touched the registers here, so
4283 * there's nothing to "merge". Copy the registers over and mark it as changed. (This is the
4284 * only way a register can transition out of "unknown", so this is not just an optimization.)
jeffhaobdb76512011-09-07 11:43:16 -07004285 */
Ian Rogersb8c78592013-07-25 23:52:52 +00004286 if (!insn_flags_[next_insn].IsReturn()) {
4287 target_line->CopyFromLine(merge_line);
4288 } else {
Jeff Haob24b4a72013-07-31 13:47:31 -07004289 // Verify that the monitor stack is empty on return.
Ian Rogers7b078e82014-09-10 14:44:24 -07004290 if (!merge_line->VerifyMonitorStackEmpty(this)) {
Jeff Haob24b4a72013-07-31 13:47:31 -07004291 return false;
4292 }
Ian Rogersb8c78592013-07-25 23:52:52 +00004293 // For returns we only care about the operand to the return, all other registers are dead.
4294 // Initialize them as conflicts so they don't add to GC and deoptimization information.
4295 const Instruction* ret_inst = Instruction::At(code_item_->insns_ + next_insn);
4296 Instruction::Code opcode = ret_inst->Opcode();
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07004297 if (opcode == Instruction::RETURN_VOID || opcode == Instruction::RETURN_VOID_NO_BARRIER) {
Stephen Kyle7e541c92014-12-17 17:10:02 +00004298 SafelyMarkAllRegistersAsConflicts(this, target_line);
Ian Rogersb8c78592013-07-25 23:52:52 +00004299 } else {
4300 target_line->CopyFromLine(merge_line);
4301 if (opcode == Instruction::RETURN_WIDE) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004302 target_line->MarkAllRegistersAsConflictsExceptWide(this, ret_inst->VRegA_11x());
Ian Rogersb8c78592013-07-25 23:52:52 +00004303 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004304 target_line->MarkAllRegistersAsConflictsExcept(this, ret_inst->VRegA_11x());
Ian Rogersb8c78592013-07-25 23:52:52 +00004305 }
4306 }
4307 }
jeffhaobdb76512011-09-07 11:43:16 -07004308 } else {
Ian Rogers700a4022014-05-19 16:49:03 -07004309 std::unique_ptr<RegisterLine> copy(gDebugVerify ?
Ian Rogersd0fbd852013-09-24 18:17:04 -07004310 RegisterLine::Create(target_line->NumRegs(), this) :
Ian Rogers7b078e82014-09-10 14:44:24 -07004311 nullptr);
Ian Rogers7b0c5b42012-02-16 15:29:07 -08004312 if (gDebugVerify) {
4313 copy->CopyFromLine(target_line);
4314 }
Ian Rogers7b078e82014-09-10 14:44:24 -07004315 changed = target_line->MergeRegisters(this, merge_line);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004316 if (have_pending_hard_failure_) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004317 return false;
jeffhaobdb76512011-09-07 11:43:16 -07004318 }
Ian Rogers2c8a8572011-10-24 17:11:36 -07004319 if (gDebugVerify && changed) {
Elliott Hughes398f64b2012-03-26 18:05:48 -07004320 LogVerifyInfo() << "Merging at [" << reinterpret_cast<void*>(work_insn_idx_) << "]"
Elliott Hughesc073b072012-05-24 19:29:17 -07004321 << " to [" << reinterpret_cast<void*>(next_insn) << "]: " << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07004322 << copy->Dump(this) << " MERGE\n"
4323 << merge_line->Dump(this) << " ==\n"
4324 << target_line->Dump(this) << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07004325 }
Ian Rogersebbdd872014-07-07 23:53:08 -07004326 if (update_merge_line && changed) {
4327 merge_line->CopyFromLine(target_line);
4328 }
jeffhaobdb76512011-09-07 11:43:16 -07004329 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004330 if (changed) {
4331 insn_flags_[next_insn].SetChanged();
jeffhaobdb76512011-09-07 11:43:16 -07004332 }
4333 return true;
4334}
4335
Ian Rogers7b3ddd22013-02-21 15:19:52 -08004336InstructionFlags* MethodVerifier::CurrentInsnFlags() {
Ian Rogers776ac1f2012-04-13 23:36:36 -07004337 return &insn_flags_[work_insn_idx_];
4338}
4339
Ian Rogersd8f69b02014-09-10 21:43:52 +00004340const RegType& MethodVerifier::GetMethodReturnType() {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004341 if (return_type_ == nullptr) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07004342 if (mirror_method_ != nullptr) {
Ian Rogersded66a02014-10-28 18:12:55 -07004343 mirror::Class* return_type_class = mirror_method_->GetReturnType(can_load_classes_);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004344 if (return_type_class != nullptr) {
Andreas Gampef23f33d2015-06-23 14:18:17 -07004345 return_type_ = &FromClass(mirror_method_->GetReturnTypeDescriptor(),
4346 return_type_class,
4347 return_type_class->CannotBeAssignedFromOtherTypes());
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004348 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004349 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
4350 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004351 }
4352 }
4353 if (return_type_ == nullptr) {
4354 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
4355 const DexFile::ProtoId& proto_id = dex_file_->GetMethodPrototype(method_id);
4356 uint16_t return_type_idx = proto_id.return_type_idx_;
4357 const char* descriptor = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(return_type_idx));
Mathieu Chartierbf99f772014-08-23 16:37:27 -07004358 return_type_ = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004359 }
4360 }
4361 return *return_type_;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004362}
4363
Ian Rogersd8f69b02014-09-10 21:43:52 +00004364const RegType& MethodVerifier::GetDeclaringClass() {
Ian Rogers7b078e82014-09-10 14:44:24 -07004365 if (declaring_class_ == nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004366 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
Brian Carlstrom93c33962013-07-26 10:37:43 -07004367 const char* descriptor
4368 = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(method_id.class_idx_));
Mathieu Chartiere401d142015-04-22 13:56:20 -07004369 if (mirror_method_ != nullptr) {
Ian Rogers637c65b2013-05-31 11:46:00 -07004370 mirror::Class* klass = mirror_method_->GetDeclaringClass();
Andreas Gampef23f33d2015-06-23 14:18:17 -07004371 declaring_class_ = &FromClass(descriptor, klass,
4372 klass->CannotBeAssignedFromOtherTypes());
Ian Rogers637c65b2013-05-31 11:46:00 -07004373 } else {
Mathieu Chartierbf99f772014-08-23 16:37:27 -07004374 declaring_class_ = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers637c65b2013-05-31 11:46:00 -07004375 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07004376 }
Ian Rogers637c65b2013-05-31 11:46:00 -07004377 return *declaring_class_;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004378}
4379
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004380std::vector<int32_t> MethodVerifier::DescribeVRegs(uint32_t dex_pc) {
4381 RegisterLine* line = reg_table_.GetLine(dex_pc);
Sebastien Hertzaa0c00c2014-03-14 17:58:54 +01004382 DCHECK(line != nullptr) << "No register line at DEX pc " << StringPrintf("0x%x", dex_pc);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004383 std::vector<int32_t> result;
4384 for (size_t i = 0; i < line->NumRegs(); ++i) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004385 const RegType& type = line->GetRegisterType(this, i);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004386 if (type.IsConstant()) {
4387 result.push_back(type.IsPreciseConstant() ? kConstant : kImpreciseConstant);
Ian Rogers7b078e82014-09-10 14:44:24 -07004388 const ConstantType* const_val = down_cast<const ConstantType*>(&type);
4389 result.push_back(const_val->ConstantValue());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004390 } else if (type.IsConstantLo()) {
4391 result.push_back(type.IsPreciseConstantLo() ? kConstant : kImpreciseConstant);
Ian Rogers7b078e82014-09-10 14:44:24 -07004392 const ConstantType* const_val = down_cast<const ConstantType*>(&type);
4393 result.push_back(const_val->ConstantValueLo());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004394 } else if (type.IsConstantHi()) {
4395 result.push_back(type.IsPreciseConstantHi() ? kConstant : kImpreciseConstant);
Ian Rogers7b078e82014-09-10 14:44:24 -07004396 const ConstantType* const_val = down_cast<const ConstantType*>(&type);
4397 result.push_back(const_val->ConstantValueHi());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004398 } else if (type.IsIntegralTypes()) {
4399 result.push_back(kIntVReg);
4400 result.push_back(0);
4401 } else if (type.IsFloat()) {
4402 result.push_back(kFloatVReg);
4403 result.push_back(0);
4404 } else if (type.IsLong()) {
4405 result.push_back(kLongLoVReg);
4406 result.push_back(0);
4407 result.push_back(kLongHiVReg);
4408 result.push_back(0);
4409 ++i;
4410 } else if (type.IsDouble()) {
4411 result.push_back(kDoubleLoVReg);
4412 result.push_back(0);
4413 result.push_back(kDoubleHiVReg);
4414 result.push_back(0);
4415 ++i;
4416 } else if (type.IsUndefined() || type.IsConflict() || type.IsHighHalf()) {
4417 result.push_back(kUndefined);
4418 result.push_back(0);
4419 } else {
Ian Rogers7b3ddd22013-02-21 15:19:52 -08004420 CHECK(type.IsNonZeroReferenceTypes());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004421 result.push_back(kReferenceVReg);
4422 result.push_back(0);
4423 }
4424 }
4425 return result;
4426}
4427
Ian Rogersd8f69b02014-09-10 21:43:52 +00004428const RegType& MethodVerifier::DetermineCat1Constant(int32_t value, bool precise) {
Sebastien Hertz849600b2013-12-20 10:28:08 +01004429 if (precise) {
4430 // Precise constant type.
4431 return reg_types_.FromCat1Const(value, true);
4432 } else {
4433 // Imprecise constant type.
4434 if (value < -32768) {
4435 return reg_types_.IntConstant();
4436 } else if (value < -128) {
4437 return reg_types_.ShortConstant();
4438 } else if (value < 0) {
4439 return reg_types_.ByteConstant();
4440 } else if (value == 0) {
4441 return reg_types_.Zero();
4442 } else if (value == 1) {
4443 return reg_types_.One();
4444 } else if (value < 128) {
4445 return reg_types_.PosByteConstant();
4446 } else if (value < 32768) {
4447 return reg_types_.PosShortConstant();
4448 } else if (value < 65536) {
4449 return reg_types_.CharConstant();
4450 } else {
4451 return reg_types_.IntConstant();
4452 }
4453 }
4454}
4455
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004456void MethodVerifier::Init() {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08004457 art::verifier::RegTypeCache::Init();
Brian Carlstrome7d856b2012-01-11 18:10:55 -08004458}
4459
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004460void MethodVerifier::Shutdown() {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08004461 verifier::RegTypeCache::ShutDown();
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08004462}
jeffhaod1224c72012-02-29 13:43:08 -08004463
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -07004464void MethodVerifier::VisitStaticRoots(RootVisitor* visitor) {
4465 RegTypeCache::VisitStaticRoots(visitor);
Mathieu Chartier7c438b12014-09-12 17:01:24 -07004466}
4467
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -07004468void MethodVerifier::VisitRoots(RootVisitor* visitor, const RootInfo& root_info) {
4469 reg_types_.VisitRoots(visitor, root_info);
Mathieu Chartierc528dba2013-11-26 12:00:11 -08004470}
4471
Andreas Gampef23f33d2015-06-23 14:18:17 -07004472const RegType& MethodVerifier::FromClass(const char* descriptor,
4473 mirror::Class* klass,
4474 bool precise) {
4475 DCHECK(klass != nullptr);
4476 if (precise && !klass->IsInstantiable() && !klass->IsPrimitive()) {
4477 Fail(VerifyError::VERIFY_ERROR_NO_CLASS) << "Could not create precise reference for "
4478 << "non-instantiable klass " << descriptor;
4479 precise = false;
4480 }
4481 return reg_types_.FromClass(descriptor, klass, precise);
4482}
4483
Ian Rogersd81871c2011-10-03 13:57:23 -07004484} // namespace verifier
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07004485} // namespace art