blob: 02929e82cc32af675fc45f59f2582e219a1428be [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),
Andreas Gamped12e7822015-06-25 10:26:40 -0700403 have_any_pending_runtime_throw_failure_(false),
jeffhaof56197c2012-03-05 18:01:54 -0800404 new_instance_count_(0),
Elliott Hughes80537bb2013-01-04 16:37:26 -0800405 monitor_enter_count_(0),
Jeff Haoee988952013-04-16 14:23:47 -0700406 can_load_classes_(can_load_classes),
Sebastien Hertz4d4adb12013-07-24 16:14:19 +0200407 allow_soft_failures_(allow_soft_failures),
Ian Rogers46960fe2014-05-23 10:43:43 -0700408 need_precise_constants_(need_precise_constants),
Sebastien Hertz4d4adb12013-07-24 16:14:19 +0200409 has_check_casts_(false),
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700410 has_virtual_or_interface_invokes_(false),
Mathieu Chartier4306ef82014-12-19 18:41:47 -0800411 verify_to_dump_(verify_to_dump),
412 allow_thread_suspension_(allow_thread_suspension) {
Mathieu Chartierd0ad2ee2015-03-31 14:59:59 -0700413 self->PushVerifier(this);
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700414 DCHECK(class_def != nullptr);
jeffhaof56197c2012-03-05 18:01:54 -0800415}
416
Mathieu Chartier590fee92013-09-13 13:46:47 -0700417MethodVerifier::~MethodVerifier() {
Mathieu Chartierd0ad2ee2015-03-31 14:59:59 -0700418 Thread::Current()->PopVerifier(this);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700419 STLDeleteElements(&failure_messages_);
420}
421
Mathieu Chartiere401d142015-04-22 13:56:20 -0700422void MethodVerifier::FindLocksAtDexPc(ArtMethod* m, uint32_t dex_pc,
Ian Rogers46960fe2014-05-23 10:43:43 -0700423 std::vector<uint32_t>* monitor_enter_dex_pcs) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700424 StackHandleScope<2> hs(Thread::Current());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700425 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache()));
426 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader()));
Mathieu Chartiere401d142015-04-22 13:56:20 -0700427 MethodVerifier verifier(hs.Self(), m->GetDexFile(), dex_cache, class_loader, &m->GetClassDef(),
428 m->GetCodeItem(), m->GetDexMethodIndex(), m, m->GetAccessFlags(),
Mathieu Chartier4306ef82014-12-19 18:41:47 -0800429 false, true, false, false);
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700430 verifier.interesting_dex_pc_ = dex_pc;
Ian Rogers46960fe2014-05-23 10:43:43 -0700431 verifier.monitor_enter_dex_pcs_ = monitor_enter_dex_pcs;
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700432 verifier.FindLocksAtDexPc();
433}
434
Andreas Gampecb3c08f2014-09-18 13:16:38 -0700435static bool HasMonitorEnterInstructions(const DexFile::CodeItem* const code_item) {
436 const Instruction* inst = Instruction::At(code_item->insns_);
437
438 uint32_t insns_size = code_item->insns_size_in_code_units_;
439 for (uint32_t dex_pc = 0; dex_pc < insns_size;) {
440 if (inst->Opcode() == Instruction::MONITOR_ENTER) {
441 return true;
442 }
443
444 dex_pc += inst->SizeInCodeUnits();
445 inst = inst->Next();
446 }
447
448 return false;
449}
450
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700451void MethodVerifier::FindLocksAtDexPc() {
Ian Rogers7b078e82014-09-10 14:44:24 -0700452 CHECK(monitor_enter_dex_pcs_ != nullptr);
453 CHECK(code_item_ != nullptr); // This only makes sense for methods with code.
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700454
Andreas Gampecb3c08f2014-09-18 13:16:38 -0700455 // Quick check whether there are any monitor_enter instructions at all.
456 if (!HasMonitorEnterInstructions(code_item_)) {
457 return;
458 }
459
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700460 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
461 // verification. In practice, the phase we want relies on data structures set up by all the
462 // earlier passes, so we just run the full method verification and bail out early when we've
463 // got what we wanted.
464 Verify();
465}
466
Mathieu Chartiere401d142015-04-22 13:56:20 -0700467ArtField* MethodVerifier::FindAccessedFieldAtDexPc(ArtMethod* m, uint32_t dex_pc) {
468 StackHandleScope<2> hs(Thread::Current());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700469 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache()));
470 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader()));
Mathieu Chartiere401d142015-04-22 13:56:20 -0700471 MethodVerifier verifier(hs.Self(), m->GetDexFile(), dex_cache, class_loader, &m->GetClassDef(),
472 m->GetCodeItem(), m->GetDexMethodIndex(), m, m->GetAccessFlags(), true,
473 true, false, true);
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200474 return verifier.FindAccessedFieldAtDexPc(dex_pc);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200475}
476
Mathieu Chartierc7853442015-03-27 14:35:38 -0700477ArtField* MethodVerifier::FindAccessedFieldAtDexPc(uint32_t dex_pc) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700478 CHECK(code_item_ != nullptr); // This only makes sense for methods with code.
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200479
480 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
481 // verification. In practice, the phase we want relies on data structures set up by all the
482 // earlier passes, so we just run the full method verification and bail out early when we've
483 // got what we wanted.
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200484 bool success = Verify();
485 if (!success) {
Ian Rogers9bc54402014-04-17 16:40:01 -0700486 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200487 }
488 RegisterLine* register_line = reg_table_.GetLine(dex_pc);
Ian Rogers7b078e82014-09-10 14:44:24 -0700489 if (register_line == nullptr) {
Ian Rogers9bc54402014-04-17 16:40:01 -0700490 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200491 }
492 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
493 return GetQuickFieldAccess(inst, register_line);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200494}
495
Mathieu Chartiere401d142015-04-22 13:56:20 -0700496ArtMethod* MethodVerifier::FindInvokedMethodAtDexPc(ArtMethod* m, uint32_t dex_pc) {
497 StackHandleScope<2> hs(Thread::Current());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700498 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache()));
499 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader()));
Mathieu Chartiere401d142015-04-22 13:56:20 -0700500 MethodVerifier verifier(hs.Self(), m->GetDexFile(), dex_cache, class_loader, &m->GetClassDef(),
501 m->GetCodeItem(), m->GetDexMethodIndex(), m, m->GetAccessFlags(), true,
502 true, false, true);
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200503 return verifier.FindInvokedMethodAtDexPc(dex_pc);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200504}
505
Mathieu Chartiere401d142015-04-22 13:56:20 -0700506ArtMethod* MethodVerifier::FindInvokedMethodAtDexPc(uint32_t dex_pc) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700507 CHECK(code_item_ != nullptr); // This only makes sense for methods with code.
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200508
509 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
510 // verification. In practice, the phase we want relies on data structures set up by all the
511 // earlier passes, so we just run the full method verification and bail out early when we've
512 // got what we wanted.
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200513 bool success = Verify();
514 if (!success) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700515 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200516 }
517 RegisterLine* register_line = reg_table_.GetLine(dex_pc);
Ian Rogers7b078e82014-09-10 14:44:24 -0700518 if (register_line == nullptr) {
519 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200520 }
521 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
522 const bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
Mathieu Chartier091d2382015-03-06 10:59:06 -0800523 return GetQuickInvokedMethod(inst, register_line, is_range, false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200524}
525
Mathieu Chartiere401d142015-04-22 13:56:20 -0700526SafeMap<uint32_t, std::set<uint32_t>> MethodVerifier::FindStringInitMap(ArtMethod* m) {
Jeff Hao848f70a2014-01-15 13:49:50 -0800527 Thread* self = Thread::Current();
Mathieu Chartiere401d142015-04-22 13:56:20 -0700528 StackHandleScope<2> hs(self);
Jeff Hao848f70a2014-01-15 13:49:50 -0800529 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache()));
530 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader()));
Jeff Hao848f70a2014-01-15 13:49:50 -0800531 MethodVerifier verifier(self, m->GetDexFile(), dex_cache, class_loader, &m->GetClassDef(),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700532 m->GetCodeItem(), m->GetDexMethodIndex(), m, m->GetAccessFlags(),
Jeff Hao848f70a2014-01-15 13:49:50 -0800533 true, true, false, true);
534 return verifier.FindStringInitMap();
535}
536
537SafeMap<uint32_t, std::set<uint32_t>>& MethodVerifier::FindStringInitMap() {
538 Verify();
539 return GetStringInitPcRegMap();
540}
541
Ian Rogersad0b3a32012-04-16 14:50:24 -0700542bool MethodVerifier::Verify() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700543 // If there aren't any instructions, make sure that's expected, then exit successfully.
Ian Rogers7b078e82014-09-10 14:44:24 -0700544 if (code_item_ == nullptr) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700545 if ((method_access_flags_ & (kAccNative | kAccAbstract)) == 0) {
jeffhaod5347e02012-03-22 17:25:05 -0700546 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "zero-length code in concrete non-native method";
jeffhaobdb76512011-09-07 11:43:16 -0700547 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -0700548 } else {
549 return true;
jeffhaobdb76512011-09-07 11:43:16 -0700550 }
jeffhaobdb76512011-09-07 11:43:16 -0700551 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700552 // Sanity-check the register counts. ins + locals = registers, so make sure that ins <= registers.
553 if (code_item_->ins_size_ > code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700554 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad register counts (ins=" << code_item_->ins_size_
555 << " regs=" << code_item_->registers_size_;
Ian Rogersd81871c2011-10-03 13:57:23 -0700556 return false;
jeffhaobdb76512011-09-07 11:43:16 -0700557 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700558 // Allocate and initialize an array to hold instruction data.
Ian Rogers7b3ddd22013-02-21 15:19:52 -0800559 insn_flags_.reset(new InstructionFlags[code_item_->insns_size_in_code_units_]());
Ian Rogersd81871c2011-10-03 13:57:23 -0700560 // Run through the instructions and see if the width checks out.
561 bool result = ComputeWidthsAndCountOps();
562 // Flag instructions guarded by a "try" block and check exception handlers.
563 result = result && ScanTryCatchBlocks();
564 // Perform static instruction verification.
565 result = result && VerifyInstructions();
Ian Rogersad0b3a32012-04-16 14:50:24 -0700566 // Perform code-flow analysis and return.
Vladimir Marko2b5eaa22013-12-13 13:59:30 +0000567 result = result && VerifyCodeFlow();
568 // Compute information for compiler.
569 if (result && Runtime::Current()->IsCompiler()) {
570 result = Runtime::Current()->GetCompilerCallbacks()->MethodVerified(this);
571 }
572 return result;
jeffhaoba5ebb92011-08-25 17:24:37 -0700573}
574
Ian Rogers776ac1f2012-04-13 23:36:36 -0700575std::ostream& MethodVerifier::Fail(VerifyError error) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700576 switch (error) {
577 case VERIFY_ERROR_NO_CLASS:
578 case VERIFY_ERROR_NO_FIELD:
579 case VERIFY_ERROR_NO_METHOD:
580 case VERIFY_ERROR_ACCESS_CLASS:
581 case VERIFY_ERROR_ACCESS_FIELD:
582 case VERIFY_ERROR_ACCESS_METHOD:
Ian Rogers08f753d2012-08-24 14:35:25 -0700583 case VERIFY_ERROR_INSTANTIATION:
584 case VERIFY_ERROR_CLASS_CHANGE:
Igor Murashkin158f35c2015-06-10 15:55:30 -0700585 case VERIFY_ERROR_FORCE_INTERPRETER:
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800586 if (Runtime::Current()->IsAotCompiler() || !can_load_classes_) {
jeffhaofaf459e2012-08-31 15:32:47 -0700587 // If we're optimistically running verification at compile time, turn NO_xxx, ACCESS_xxx,
588 // class change and instantiation errors into soft verification errors so that we re-verify
589 // at runtime. We may fail to find or to agree on access because of not yet available class
590 // loaders, or class loaders that will differ at runtime. In these cases, we don't want to
591 // affect the soundness of the code being compiled. Instead, the generated code runs "slow
592 // paths" that dynamically perform the verification and cause the behavior to be that akin
593 // to an interpreter.
594 error = VERIFY_ERROR_BAD_CLASS_SOFT;
595 } else {
Jeff Haoa3faaf42013-09-03 19:07:00 -0700596 // If we fail again at runtime, mark that this instruction would throw and force this
597 // method to be executed using the interpreter with checks.
jeffhaofaf459e2012-08-31 15:32:47 -0700598 have_pending_runtime_throw_failure_ = true;
Andreas Gamped7f8d052015-03-12 11:05:47 -0700599
600 // We need to save the work_line if the instruction wasn't throwing before. Otherwise we'll
601 // try to merge garbage.
602 // Note: this assumes that Fail is called before we do any work_line modifications.
603 const uint16_t* insns = code_item_->insns_ + work_insn_idx_;
604 const Instruction* inst = Instruction::At(insns);
605 int opcode_flags = Instruction::FlagsOf(inst->Opcode());
606
607 if ((opcode_flags & Instruction::kThrow) == 0 && CurrentInsnFlags()->IsInTry()) {
608 saved_line_->CopyFromLine(work_line_.get());
609 }
jeffhaofaf459e2012-08-31 15:32:47 -0700610 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700611 break;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700612 // Indication that verification should be retried at runtime.
613 case VERIFY_ERROR_BAD_CLASS_SOFT:
Jeff Haoee988952013-04-16 14:23:47 -0700614 if (!allow_soft_failures_) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700615 have_pending_hard_failure_ = true;
616 }
617 break;
jeffhaod5347e02012-03-22 17:25:05 -0700618 // Hard verification failures at compile time will still fail at runtime, so the class is
619 // marked as rejected to prevent it from being compiled.
Ian Rogersad0b3a32012-04-16 14:50:24 -0700620 case VERIFY_ERROR_BAD_CLASS_HARD: {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800621 if (Runtime::Current()->IsAotCompiler()) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700622 ClassReference ref(dex_file_, dex_file_->GetIndexForClassDef(*class_def_));
Vladimir Marko2b5eaa22013-12-13 13:59:30 +0000623 Runtime::Current()->GetCompilerCallbacks()->ClassRejected(ref);
jeffhaod1224c72012-02-29 13:43:08 -0800624 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700625 have_pending_hard_failure_ = true;
626 break;
Ian Rogers47a05882012-02-03 12:23:33 -0800627 }
628 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700629 failures_.push_back(error);
Elena Sayapina78480ec2014-08-15 15:52:42 +0700630 std::string location(StringPrintf("%s: [0x%X] ", PrettyMethod(dex_method_idx_, *dex_file_).c_str(),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700631 work_insn_idx_));
Elena Sayapina78480ec2014-08-15 15:52:42 +0700632 std::ostringstream* failure_message = new std::ostringstream(location, std::ostringstream::ate);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700633 failure_messages_.push_back(failure_message);
634 return *failure_message;
635}
636
Ian Rogers576ca0c2014-06-06 15:58:22 -0700637std::ostream& MethodVerifier::LogVerifyInfo() {
638 return info_messages_ << "VFY: " << PrettyMethod(dex_method_idx_, *dex_file_)
639 << '[' << reinterpret_cast<void*>(work_insn_idx_) << "] : ";
640}
641
Ian Rogersad0b3a32012-04-16 14:50:24 -0700642void MethodVerifier::PrependToLastFailMessage(std::string prepend) {
643 size_t failure_num = failure_messages_.size();
644 DCHECK_NE(failure_num, 0U);
645 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
646 prepend += last_fail_message->str();
Elena Sayapina78480ec2014-08-15 15:52:42 +0700647 failure_messages_[failure_num - 1] = new std::ostringstream(prepend, std::ostringstream::ate);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700648 delete last_fail_message;
649}
650
651void MethodVerifier::AppendToLastFailMessage(std::string append) {
652 size_t failure_num = failure_messages_.size();
653 DCHECK_NE(failure_num, 0U);
654 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
655 (*last_fail_message) << append;
Ian Rogers47a05882012-02-03 12:23:33 -0800656}
657
Ian Rogers776ac1f2012-04-13 23:36:36 -0700658bool MethodVerifier::ComputeWidthsAndCountOps() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700659 const uint16_t* insns = code_item_->insns_;
660 size_t insns_size = code_item_->insns_size_in_code_units_;
661 const Instruction* inst = Instruction::At(insns);
jeffhaobdb76512011-09-07 11:43:16 -0700662 size_t new_instance_count = 0;
663 size_t monitor_enter_count = 0;
Ian Rogersd81871c2011-10-03 13:57:23 -0700664 size_t dex_pc = 0;
jeffhaobdb76512011-09-07 11:43:16 -0700665
Ian Rogersd81871c2011-10-03 13:57:23 -0700666 while (dex_pc < insns_size) {
jeffhaobdb76512011-09-07 11:43:16 -0700667 Instruction::Code opcode = inst->Opcode();
Ian Rogersa9a82542013-10-04 11:17:26 -0700668 switch (opcode) {
669 case Instruction::APUT_OBJECT:
670 case Instruction::CHECK_CAST:
671 has_check_casts_ = true;
672 break;
673 case Instruction::INVOKE_VIRTUAL:
674 case Instruction::INVOKE_VIRTUAL_RANGE:
675 case Instruction::INVOKE_INTERFACE:
676 case Instruction::INVOKE_INTERFACE_RANGE:
677 has_virtual_or_interface_invokes_ = true;
678 break;
679 case Instruction::MONITOR_ENTER:
680 monitor_enter_count++;
681 break;
682 case Instruction::NEW_INSTANCE:
683 new_instance_count++;
684 break;
685 default:
686 break;
jeffhaobdb76512011-09-07 11:43:16 -0700687 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700688 size_t inst_size = inst->SizeInCodeUnits();
Ian Rogers7b078e82014-09-10 14:44:24 -0700689 insn_flags_[dex_pc].SetIsOpcode();
Ian Rogersd81871c2011-10-03 13:57:23 -0700690 dex_pc += inst_size;
Ian Rogers7b078e82014-09-10 14:44:24 -0700691 inst = inst->RelativeAt(inst_size);
jeffhaobdb76512011-09-07 11:43:16 -0700692 }
693
Ian Rogersd81871c2011-10-03 13:57:23 -0700694 if (dex_pc != insns_size) {
jeffhaod5347e02012-03-22 17:25:05 -0700695 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "code did not end where expected ("
696 << dex_pc << " vs. " << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700697 return false;
698 }
699
Ian Rogersd81871c2011-10-03 13:57:23 -0700700 new_instance_count_ = new_instance_count;
701 monitor_enter_count_ = monitor_enter_count;
jeffhaobdb76512011-09-07 11:43:16 -0700702 return true;
703}
704
Ian Rogers776ac1f2012-04-13 23:36:36 -0700705bool MethodVerifier::ScanTryCatchBlocks() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700706 uint32_t tries_size = code_item_->tries_size_;
jeffhaobdb76512011-09-07 11:43:16 -0700707 if (tries_size == 0) {
708 return true;
709 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700710 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Ian Rogers0571d352011-11-03 19:51:38 -0700711 const DexFile::TryItem* tries = DexFile::GetTryItems(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -0700712
713 for (uint32_t idx = 0; idx < tries_size; idx++) {
714 const DexFile::TryItem* try_item = &tries[idx];
715 uint32_t start = try_item->start_addr_;
716 uint32_t end = start + try_item->insn_count_;
jeffhaobdb76512011-09-07 11:43:16 -0700717 if ((start >= end) || (start >= insns_size) || (end > insns_size)) {
jeffhaod5347e02012-03-22 17:25:05 -0700718 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad exception entry: startAddr=" << start
719 << " endAddr=" << end << " (size=" << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700720 return false;
721 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700722 if (!insn_flags_[start].IsOpcode()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700723 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
724 << "'try' block starts inside an instruction (" << start << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700725 return false;
726 }
Ian Rogers7b078e82014-09-10 14:44:24 -0700727 uint32_t dex_pc = start;
728 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
729 while (dex_pc < end) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700730 insn_flags_[dex_pc].SetInTry();
Ian Rogers7b078e82014-09-10 14:44:24 -0700731 size_t insn_size = inst->SizeInCodeUnits();
732 dex_pc += insn_size;
733 inst = inst->RelativeAt(insn_size);
jeffhaobdb76512011-09-07 11:43:16 -0700734 }
735 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800736 // Iterate over each of the handlers to verify target addresses.
Ian Rogers13735952014-10-08 12:43:28 -0700737 const uint8_t* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -0700738 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700739 ClassLinker* linker = Runtime::Current()->GetClassLinker();
jeffhaobdb76512011-09-07 11:43:16 -0700740 for (uint32_t idx = 0; idx < handlers_size; idx++) {
Ian Rogers0571d352011-11-03 19:51:38 -0700741 CatchHandlerIterator iterator(handlers_ptr);
742 for (; iterator.HasNext(); iterator.Next()) {
743 uint32_t dex_pc= iterator.GetHandlerAddress();
Ian Rogersd81871c2011-10-03 13:57:23 -0700744 if (!insn_flags_[dex_pc].IsOpcode()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700745 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
746 << "exception handler starts at bad address (" << dex_pc << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700747 return false;
748 }
Stephen Kyle9bc61992014-09-22 13:53:15 +0100749 if (!CheckNotMoveResult(code_item_->insns_, dex_pc)) {
750 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
751 << "exception handler begins with move-result* (" << dex_pc << ")";
752 return false;
753 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700754 insn_flags_[dex_pc].SetBranchTarget();
Ian Rogers28ad40d2011-10-27 15:19:26 -0700755 // Ensure exception types are resolved so that they don't need resolution to be delivered,
756 // unresolved exception types will be ignored by exception delivery
Ian Rogers0571d352011-11-03 19:51:38 -0700757 if (iterator.GetHandlerTypeIndex() != DexFile::kDexNoIndex16) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800758 mirror::Class* exception_type = linker->ResolveType(*dex_file_,
759 iterator.GetHandlerTypeIndex(),
Mathieu Chartierbf99f772014-08-23 16:37:27 -0700760 dex_cache_, class_loader_);
Ian Rogers7b078e82014-09-10 14:44:24 -0700761 if (exception_type == nullptr) {
762 DCHECK(self_->IsExceptionPending());
763 self_->ClearException();
Ian Rogers28ad40d2011-10-27 15:19:26 -0700764 }
765 }
jeffhaobdb76512011-09-07 11:43:16 -0700766 }
Ian Rogers0571d352011-11-03 19:51:38 -0700767 handlers_ptr = iterator.EndDataPointer();
jeffhaobdb76512011-09-07 11:43:16 -0700768 }
jeffhaobdb76512011-09-07 11:43:16 -0700769 return true;
770}
771
Ian Rogers776ac1f2012-04-13 23:36:36 -0700772bool MethodVerifier::VerifyInstructions() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700773 const Instruction* inst = Instruction::At(code_item_->insns_);
jeffhaoba5ebb92011-08-25 17:24:37 -0700774
Ian Rogers0c7abda2012-09-19 13:33:42 -0700775 /* 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 -0700776 insn_flags_[0].SetBranchTarget();
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700777 insn_flags_[0].SetCompileTimeInfoPoint();
Ian Rogersd81871c2011-10-03 13:57:23 -0700778
779 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700780 for (uint32_t dex_pc = 0; dex_pc < insns_size;) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700781 if (!VerifyInstruction(inst, dex_pc)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700782 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -0700783 return false;
784 }
785 /* Flag instructions that are garbage collection points */
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700786 // All invoke points are marked as "Throw" points already.
787 // We are relying on this to also count all the invokes as interesting.
Vladimir Marko8b858e12014-11-27 14:52:37 +0000788 if (inst->IsBranch()) {
789 insn_flags_[dex_pc].SetCompileTimeInfoPoint();
790 // The compiler also needs safepoints for fall-through to loop heads.
791 // Such a loop head must be a target of a branch.
792 int32_t offset = 0;
793 bool cond, self_ok;
794 bool target_ok = GetBranchOffset(dex_pc, &offset, &cond, &self_ok);
795 DCHECK(target_ok);
796 insn_flags_[dex_pc + offset].SetCompileTimeInfoPoint();
797 } else if (inst->IsSwitch() || inst->IsThrow()) {
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700798 insn_flags_[dex_pc].SetCompileTimeInfoPoint();
Ian Rogersb8c78592013-07-25 23:52:52 +0000799 } else if (inst->IsReturn()) {
800 insn_flags_[dex_pc].SetCompileTimeInfoPointAndReturn();
Ian Rogersd81871c2011-10-03 13:57:23 -0700801 }
802 dex_pc += inst->SizeInCodeUnits();
803 inst = inst->Next();
804 }
805 return true;
806}
807
Ian Rogers776ac1f2012-04-13 23:36:36 -0700808bool MethodVerifier::VerifyInstruction(const Instruction* inst, uint32_t code_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700809 bool result = true;
810 switch (inst->GetVerifyTypeArgumentA()) {
811 case Instruction::kVerifyRegA:
Ian Rogers29a26482014-05-02 15:27:29 -0700812 result = result && CheckRegisterIndex(inst->VRegA());
Ian Rogersd81871c2011-10-03 13:57:23 -0700813 break;
814 case Instruction::kVerifyRegAWide:
Ian Rogers29a26482014-05-02 15:27:29 -0700815 result = result && CheckWideRegisterIndex(inst->VRegA());
Ian Rogersd81871c2011-10-03 13:57:23 -0700816 break;
817 }
818 switch (inst->GetVerifyTypeArgumentB()) {
819 case Instruction::kVerifyRegB:
Ian Rogers29a26482014-05-02 15:27:29 -0700820 result = result && CheckRegisterIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700821 break;
822 case Instruction::kVerifyRegBField:
Ian Rogers29a26482014-05-02 15:27:29 -0700823 result = result && CheckFieldIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700824 break;
825 case Instruction::kVerifyRegBMethod:
Ian Rogers29a26482014-05-02 15:27:29 -0700826 result = result && CheckMethodIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700827 break;
828 case Instruction::kVerifyRegBNewInstance:
Ian Rogers29a26482014-05-02 15:27:29 -0700829 result = result && CheckNewInstance(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700830 break;
831 case Instruction::kVerifyRegBString:
Ian Rogers29a26482014-05-02 15:27:29 -0700832 result = result && CheckStringIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700833 break;
834 case Instruction::kVerifyRegBType:
Ian Rogers29a26482014-05-02 15:27:29 -0700835 result = result && CheckTypeIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700836 break;
837 case Instruction::kVerifyRegBWide:
Ian Rogers29a26482014-05-02 15:27:29 -0700838 result = result && CheckWideRegisterIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700839 break;
840 }
841 switch (inst->GetVerifyTypeArgumentC()) {
842 case Instruction::kVerifyRegC:
Ian Rogers29a26482014-05-02 15:27:29 -0700843 result = result && CheckRegisterIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700844 break;
845 case Instruction::kVerifyRegCField:
Ian Rogers29a26482014-05-02 15:27:29 -0700846 result = result && CheckFieldIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700847 break;
848 case Instruction::kVerifyRegCNewArray:
Ian Rogers29a26482014-05-02 15:27:29 -0700849 result = result && CheckNewArray(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700850 break;
851 case Instruction::kVerifyRegCType:
Ian Rogers29a26482014-05-02 15:27:29 -0700852 result = result && CheckTypeIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700853 break;
854 case Instruction::kVerifyRegCWide:
Ian Rogers29a26482014-05-02 15:27:29 -0700855 result = result && CheckWideRegisterIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700856 break;
857 }
858 switch (inst->GetVerifyExtraFlags()) {
859 case Instruction::kVerifyArrayData:
860 result = result && CheckArrayData(code_offset);
861 break;
862 case Instruction::kVerifyBranchTarget:
863 result = result && CheckBranchTarget(code_offset);
864 break;
865 case Instruction::kVerifySwitchTargets:
866 result = result && CheckSwitchTargets(code_offset);
867 break;
Andreas Gampec3314312014-06-19 18:13:29 -0700868 case Instruction::kVerifyVarArgNonZero:
869 // Fall-through.
Ian Rogers29a26482014-05-02 15:27:29 -0700870 case Instruction::kVerifyVarArg: {
Taiju Tsuiki29498a22015-04-13 14:21:00 +0900871 // Instructions that can actually return a negative value shouldn't have this flag.
872 uint32_t v_a = dchecked_integral_cast<uint32_t>(inst->VRegA());
873 if ((inst->GetVerifyExtraFlags() == Instruction::kVerifyVarArgNonZero && v_a == 0) ||
874 v_a > Instruction::kMaxVarArgRegs) {
875 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << v_a << ") in "
Andreas Gampec3314312014-06-19 18:13:29 -0700876 "non-range invoke";
877 return false;
878 }
Taiju Tsuiki29498a22015-04-13 14:21:00 +0900879
Ian Rogers29a26482014-05-02 15:27:29 -0700880 uint32_t args[Instruction::kMaxVarArgRegs];
881 inst->GetVarArgs(args);
Taiju Tsuiki29498a22015-04-13 14:21:00 +0900882 result = result && CheckVarArgRegs(v_a, args);
Ian Rogersd81871c2011-10-03 13:57:23 -0700883 break;
Ian Rogers29a26482014-05-02 15:27:29 -0700884 }
Andreas Gampec3314312014-06-19 18:13:29 -0700885 case Instruction::kVerifyVarArgRangeNonZero:
886 // Fall-through.
Ian Rogersd81871c2011-10-03 13:57:23 -0700887 case Instruction::kVerifyVarArgRange:
Andreas Gampec3314312014-06-19 18:13:29 -0700888 if (inst->GetVerifyExtraFlags() == Instruction::kVerifyVarArgRangeNonZero &&
889 inst->VRegA() <= 0) {
890 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << inst->VRegA() << ") in "
891 "range invoke";
892 return false;
893 }
Ian Rogers29a26482014-05-02 15:27:29 -0700894 result = result && CheckVarArgRangeRegs(inst->VRegA(), inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700895 break;
896 case Instruction::kVerifyError:
jeffhaod5347e02012-03-22 17:25:05 -0700897 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected opcode " << inst->Name();
Ian Rogersd81871c2011-10-03 13:57:23 -0700898 result = false;
899 break;
900 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800901 if (inst->GetVerifyIsRuntimeOnly() && Runtime::Current()->IsAotCompiler() && !verify_to_dump_) {
Ian Rogers5fb22a92014-06-13 10:31:28 -0700902 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "opcode only expected at runtime " << inst->Name();
903 result = false;
904 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700905 return result;
906}
907
Ian Rogers7b078e82014-09-10 14:44:24 -0700908inline bool MethodVerifier::CheckRegisterIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700909 if (idx >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700910 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register index out of range (" << idx << " >= "
911 << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700912 return false;
913 }
914 return true;
915}
916
Ian Rogers7b078e82014-09-10 14:44:24 -0700917inline bool MethodVerifier::CheckWideRegisterIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700918 if (idx + 1 >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700919 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "wide register index out of range (" << idx
920 << "+1 >= " << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700921 return false;
922 }
923 return true;
924}
925
Ian Rogers7b078e82014-09-10 14:44:24 -0700926inline bool MethodVerifier::CheckFieldIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700927 if (idx >= dex_file_->GetHeader().field_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700928 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad field index " << idx << " (max "
929 << dex_file_->GetHeader().field_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700930 return false;
931 }
932 return true;
933}
934
Ian Rogers7b078e82014-09-10 14:44:24 -0700935inline bool MethodVerifier::CheckMethodIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700936 if (idx >= dex_file_->GetHeader().method_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700937 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad method index " << idx << " (max "
938 << dex_file_->GetHeader().method_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700939 return false;
940 }
941 return true;
942}
943
Ian Rogers7b078e82014-09-10 14:44:24 -0700944inline bool MethodVerifier::CheckNewInstance(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700945 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700946 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
947 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700948 return false;
949 }
950 // We don't need the actual class, just a pointer to the class name.
Ian Rogers0571d352011-11-03 19:51:38 -0700951 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -0700952 if (descriptor[0] != 'L') {
jeffhaod5347e02012-03-22 17:25:05 -0700953 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "can't call new-instance on type '" << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -0700954 return false;
955 }
956 return true;
957}
958
Ian Rogers7b078e82014-09-10 14:44:24 -0700959inline bool MethodVerifier::CheckStringIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700960 if (idx >= dex_file_->GetHeader().string_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700961 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad string index " << idx << " (max "
962 << dex_file_->GetHeader().string_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700963 return false;
964 }
965 return true;
966}
967
Ian Rogers7b078e82014-09-10 14:44:24 -0700968inline bool MethodVerifier::CheckTypeIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700969 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700970 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
971 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700972 return false;
973 }
974 return true;
975}
976
Ian Rogers776ac1f2012-04-13 23:36:36 -0700977bool MethodVerifier::CheckNewArray(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700978 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700979 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
980 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700981 return false;
982 }
983 int bracket_count = 0;
Ian Rogers0571d352011-11-03 19:51:38 -0700984 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -0700985 const char* cp = descriptor;
986 while (*cp++ == '[') {
987 bracket_count++;
988 }
989 if (bracket_count == 0) {
990 /* The given class must be an array type. */
Brian Carlstrom93c33962013-07-26 10:37:43 -0700991 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
992 << "can't new-array class '" << descriptor << "' (not an array)";
Ian Rogersd81871c2011-10-03 13:57:23 -0700993 return false;
994 } else if (bracket_count > 255) {
995 /* It is illegal to create an array of more than 255 dimensions. */
Brian Carlstrom93c33962013-07-26 10:37:43 -0700996 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
997 << "can't new-array class '" << descriptor << "' (exceeds limit)";
Ian Rogersd81871c2011-10-03 13:57:23 -0700998 return false;
999 }
1000 return true;
1001}
1002
Ian Rogers776ac1f2012-04-13 23:36:36 -07001003bool MethodVerifier::CheckArrayData(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001004 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
1005 const uint16_t* insns = code_item_->insns_ + cur_offset;
1006 const uint16_t* array_data;
1007 int32_t array_data_offset;
1008
1009 DCHECK_LT(cur_offset, insn_count);
1010 /* make sure the start of the array data table is in range */
1011 array_data_offset = insns[1] | (((int32_t) insns[2]) << 16);
1012 if ((int32_t) cur_offset + array_data_offset < 0 ||
1013 cur_offset + array_data_offset + 2 >= insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -07001014 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data start: at " << cur_offset
Brian Carlstrom93c33962013-07-26 10:37:43 -07001015 << ", data offset " << array_data_offset
1016 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -07001017 return false;
1018 }
1019 /* offset to array data table is a relative branch-style offset */
1020 array_data = insns + array_data_offset;
1021 /* make sure the table is 32-bit aligned */
Ian Rogersef7d42f2014-01-06 12:55:46 -08001022 if ((reinterpret_cast<uintptr_t>(array_data) & 0x03) != 0) {
jeffhaod5347e02012-03-22 17:25:05 -07001023 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned array data table: at " << cur_offset
1024 << ", data offset " << array_data_offset;
Ian Rogersd81871c2011-10-03 13:57:23 -07001025 return false;
1026 }
1027 uint32_t value_width = array_data[1];
Elliott Hughes398f64b2012-03-26 18:05:48 -07001028 uint32_t value_count = *reinterpret_cast<const uint32_t*>(&array_data[2]);
Ian Rogersd81871c2011-10-03 13:57:23 -07001029 uint32_t table_size = 4 + (value_width * value_count + 1) / 2;
1030 /* make sure the end of the switch is in range */
1031 if (cur_offset + array_data_offset + table_size > insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -07001032 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data end: at " << cur_offset
1033 << ", data offset " << array_data_offset << ", end "
1034 << cur_offset + array_data_offset + table_size
1035 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -07001036 return false;
1037 }
1038 return true;
1039}
1040
Ian Rogers776ac1f2012-04-13 23:36:36 -07001041bool MethodVerifier::CheckBranchTarget(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001042 int32_t offset;
1043 bool isConditional, selfOkay;
1044 if (!GetBranchOffset(cur_offset, &offset, &isConditional, &selfOkay)) {
1045 return false;
1046 }
1047 if (!selfOkay && offset == 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001048 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch offset of zero not allowed at"
1049 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -07001050 return false;
1051 }
Elliott Hughes81ff3182012-03-23 20:35:56 -07001052 // Check for 32-bit overflow. This isn't strictly necessary if we can depend on the runtime
1053 // to have identical "wrap-around" behavior, but it's unwise to depend on that.
Ian Rogersd81871c2011-10-03 13:57:23 -07001054 if (((int64_t) cur_offset + (int64_t) offset) != (int64_t) (cur_offset + offset)) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001055 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch target overflow "
1056 << reinterpret_cast<void*>(cur_offset) << " +" << offset;
Ian Rogersd81871c2011-10-03 13:57:23 -07001057 return false;
1058 }
1059 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
1060 int32_t abs_offset = cur_offset + offset;
Brian Carlstrom93c33962013-07-26 10:37:43 -07001061 if (abs_offset < 0 ||
1062 (uint32_t) abs_offset >= insn_count ||
1063 !insn_flags_[abs_offset].IsOpcode()) {
jeffhaod5347e02012-03-22 17:25:05 -07001064 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid branch target " << offset << " (-> "
Elliott Hughes398f64b2012-03-26 18:05:48 -07001065 << reinterpret_cast<void*>(abs_offset) << ") at "
1066 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -07001067 return false;
1068 }
1069 insn_flags_[abs_offset].SetBranchTarget();
1070 return true;
1071}
1072
Ian Rogers776ac1f2012-04-13 23:36:36 -07001073bool MethodVerifier::GetBranchOffset(uint32_t cur_offset, int32_t* pOffset, bool* pConditional,
Ian Rogersd81871c2011-10-03 13:57:23 -07001074 bool* selfOkay) {
1075 const uint16_t* insns = code_item_->insns_ + cur_offset;
1076 *pConditional = false;
1077 *selfOkay = false;
jeffhaoba5ebb92011-08-25 17:24:37 -07001078 switch (*insns & 0xff) {
1079 case Instruction::GOTO:
1080 *pOffset = ((int16_t) *insns) >> 8;
jeffhaoba5ebb92011-08-25 17:24:37 -07001081 break;
1082 case Instruction::GOTO_32:
1083 *pOffset = insns[1] | (((uint32_t) insns[2]) << 16);
jeffhaoba5ebb92011-08-25 17:24:37 -07001084 *selfOkay = true;
1085 break;
1086 case Instruction::GOTO_16:
1087 *pOffset = (int16_t) insns[1];
jeffhaoba5ebb92011-08-25 17:24:37 -07001088 break;
1089 case Instruction::IF_EQ:
1090 case Instruction::IF_NE:
1091 case Instruction::IF_LT:
1092 case Instruction::IF_GE:
1093 case Instruction::IF_GT:
1094 case Instruction::IF_LE:
1095 case Instruction::IF_EQZ:
1096 case Instruction::IF_NEZ:
1097 case Instruction::IF_LTZ:
1098 case Instruction::IF_GEZ:
1099 case Instruction::IF_GTZ:
1100 case Instruction::IF_LEZ:
1101 *pOffset = (int16_t) insns[1];
1102 *pConditional = true;
jeffhaoba5ebb92011-08-25 17:24:37 -07001103 break;
1104 default:
1105 return false;
jeffhaoba5ebb92011-08-25 17:24:37 -07001106 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001107 return true;
1108}
1109
Ian Rogers776ac1f2012-04-13 23:36:36 -07001110bool MethodVerifier::CheckSwitchTargets(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001111 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001112 DCHECK_LT(cur_offset, insn_count);
Ian Rogersd81871c2011-10-03 13:57:23 -07001113 const uint16_t* insns = code_item_->insns_ + cur_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001114 /* make sure the start of the switch is in range */
Ian Rogersd81871c2011-10-03 13:57:23 -07001115 int32_t switch_offset = insns[1] | ((int32_t) insns[2]) << 16;
Jeff Hao9ccd1512015-03-20 18:11:45 -07001116 if ((int32_t) cur_offset + switch_offset < 0 || cur_offset + switch_offset + 2 > insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -07001117 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch start: at " << cur_offset
Brian Carlstrom93c33962013-07-26 10:37:43 -07001118 << ", switch offset " << switch_offset
1119 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001120 return false;
1121 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001122 /* offset to switch table is a relative branch-style offset */
Ian Rogersd81871c2011-10-03 13:57:23 -07001123 const uint16_t* switch_insns = insns + switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001124 /* make sure the table is 32-bit aligned */
Ian Rogersef7d42f2014-01-06 12:55:46 -08001125 if ((reinterpret_cast<uintptr_t>(switch_insns) & 0x03) != 0) {
jeffhaod5347e02012-03-22 17:25:05 -07001126 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned switch table: at " << cur_offset
1127 << ", switch offset " << switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001128 return false;
1129 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001130 uint32_t switch_count = switch_insns[1];
1131 int32_t keys_offset, targets_offset;
1132 uint16_t expected_signature;
jeffhaoba5ebb92011-08-25 17:24:37 -07001133 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
1134 /* 0=sig, 1=count, 2/3=firstKey */
1135 targets_offset = 4;
1136 keys_offset = -1;
1137 expected_signature = Instruction::kPackedSwitchSignature;
1138 } else {
1139 /* 0=sig, 1=count, 2..count*2 = keys */
1140 keys_offset = 2;
1141 targets_offset = 2 + 2 * switch_count;
1142 expected_signature = Instruction::kSparseSwitchSignature;
1143 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001144 uint32_t table_size = targets_offset + switch_count * 2;
jeffhaoba5ebb92011-08-25 17:24:37 -07001145 if (switch_insns[0] != expected_signature) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001146 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1147 << StringPrintf("wrong signature for switch table (%x, wanted %x)",
1148 switch_insns[0], expected_signature);
jeffhaoba5ebb92011-08-25 17:24:37 -07001149 return false;
1150 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001151 /* make sure the end of the switch is in range */
1152 if (cur_offset + switch_offset + table_size > (uint32_t) insn_count) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001153 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch end: at " << cur_offset
1154 << ", switch offset " << switch_offset
1155 << ", end " << (cur_offset + switch_offset + table_size)
jeffhaod5347e02012-03-22 17:25:05 -07001156 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001157 return false;
1158 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001159 /* for a sparse switch, verify the keys are in ascending order */
1160 if (keys_offset > 0 && switch_count > 1) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001161 int32_t last_key = switch_insns[keys_offset] | (switch_insns[keys_offset + 1] << 16);
1162 for (uint32_t targ = 1; targ < switch_count; targ++) {
jeffhaoba5ebb92011-08-25 17:24:37 -07001163 int32_t key = (int32_t) switch_insns[keys_offset + targ * 2] |
1164 (int32_t) (switch_insns[keys_offset + targ * 2 + 1] << 16);
1165 if (key <= last_key) {
jeffhaod5347e02012-03-22 17:25:05 -07001166 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid packed switch: last key=" << last_key
1167 << ", this=" << key;
jeffhaoba5ebb92011-08-25 17:24:37 -07001168 return false;
1169 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001170 last_key = key;
1171 }
1172 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001173 /* verify each switch target */
Ian Rogersd81871c2011-10-03 13:57:23 -07001174 for (uint32_t targ = 0; targ < switch_count; targ++) {
1175 int32_t offset = (int32_t) switch_insns[targets_offset + targ * 2] |
1176 (int32_t) (switch_insns[targets_offset + targ * 2 + 1] << 16);
1177 int32_t abs_offset = cur_offset + offset;
Brian Carlstrom93c33962013-07-26 10:37:43 -07001178 if (abs_offset < 0 ||
1179 abs_offset >= (int32_t) insn_count ||
1180 !insn_flags_[abs_offset].IsOpcode()) {
1181 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch target " << offset
1182 << " (-> " << reinterpret_cast<void*>(abs_offset) << ") at "
1183 << reinterpret_cast<void*>(cur_offset)
1184 << "[" << targ << "]";
jeffhaoba5ebb92011-08-25 17:24:37 -07001185 return false;
1186 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001187 insn_flags_[abs_offset].SetBranchTarget();
1188 }
1189 return true;
1190}
1191
Ian Rogers776ac1f2012-04-13 23:36:36 -07001192bool MethodVerifier::CheckVarArgRegs(uint32_t vA, uint32_t arg[]) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001193 uint16_t registers_size = code_item_->registers_size_;
1194 for (uint32_t idx = 0; idx < vA; idx++) {
jeffhao457cc512012-02-02 16:55:13 -08001195 if (arg[idx] >= registers_size) {
jeffhaod5347e02012-03-22 17:25:05 -07001196 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index (" << arg[idx]
1197 << ") in non-range invoke (>= " << registers_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001198 return false;
1199 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001200 }
1201
1202 return true;
1203}
1204
Ian Rogers776ac1f2012-04-13 23:36:36 -07001205bool MethodVerifier::CheckVarArgRangeRegs(uint32_t vA, uint32_t vC) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001206 uint16_t registers_size = code_item_->registers_size_;
1207 // vA/vC are unsigned 8-bit/16-bit quantities for /range instructions, so there's no risk of
1208 // integer overflow when adding them here.
1209 if (vA + vC > registers_size) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001210 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index " << vA << "+" << vC
1211 << " in range invoke (> " << registers_size << ")";
jeffhaoba5ebb92011-08-25 17:24:37 -07001212 return false;
1213 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001214 return true;
1215}
1216
Ian Rogers776ac1f2012-04-13 23:36:36 -07001217bool MethodVerifier::VerifyCodeFlow() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001218 uint16_t registers_size = code_item_->registers_size_;
1219 uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaobdb76512011-09-07 11:43:16 -07001220
Ian Rogersd81871c2011-10-03 13:57:23 -07001221 /* Create and initialize table holding register status */
Brian Carlstrom93c33962013-07-26 10:37:43 -07001222 reg_table_.Init(kTrackCompilerInterestPoints,
1223 insn_flags_.get(),
1224 insns_size,
1225 registers_size,
1226 this);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07001227
jeffhaobdb76512011-09-07 11:43:16 -07001228
Ian Rogersd0fbd852013-09-24 18:17:04 -07001229 work_line_.reset(RegisterLine::Create(registers_size, this));
1230 saved_line_.reset(RegisterLine::Create(registers_size, this));
jeffhaobdb76512011-09-07 11:43:16 -07001231
Ian Rogersd81871c2011-10-03 13:57:23 -07001232 /* Initialize register types of method arguments. */
1233 if (!SetTypesFromSignature()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001234 DCHECK_NE(failures_.size(), 0U);
1235 std::string prepend("Bad signature in ");
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001236 prepend += PrettyMethod(dex_method_idx_, *dex_file_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001237 PrependToLastFailMessage(prepend);
Ian Rogersd81871c2011-10-03 13:57:23 -07001238 return false;
1239 }
1240 /* Perform code flow verification. */
1241 if (!CodeFlowVerifyMethod()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001242 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -07001243 return false;
jeffhaobdb76512011-09-07 11:43:16 -07001244 }
jeffhaobdb76512011-09-07 11:43:16 -07001245 return true;
1246}
1247
Ian Rogersad0b3a32012-04-16 14:50:24 -07001248std::ostream& MethodVerifier::DumpFailures(std::ostream& os) {
1249 DCHECK_EQ(failures_.size(), failure_messages_.size());
Jeff Hao4137f482013-11-22 11:44:57 -08001250 for (size_t i = 0; i < failures_.size(); ++i) {
1251 os << failure_messages_[i]->str() << "\n";
Ian Rogersad0b3a32012-04-16 14:50:24 -07001252 }
1253 return os;
1254}
1255
Ian Rogers776ac1f2012-04-13 23:36:36 -07001256void MethodVerifier::Dump(std::ostream& os) {
Ian Rogers7b078e82014-09-10 14:44:24 -07001257 if (code_item_ == nullptr) {
Elliott Hughesc073b072012-05-24 19:29:17 -07001258 os << "Native method\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001259 return;
jeffhaobdb76512011-09-07 11:43:16 -07001260 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001261 {
1262 os << "Register Types:\n";
1263 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
1264 std::ostream indent_os(&indent_filter);
1265 reg_types_.Dump(indent_os);
1266 }
Ian Rogersb4903572012-10-11 11:52:56 -07001267 os << "Dumping instructions and register lines:\n";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001268 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
1269 std::ostream indent_os(&indent_filter);
Ian Rogersd81871c2011-10-03 13:57:23 -07001270 const Instruction* inst = Instruction::At(code_item_->insns_);
1271 for (size_t dex_pc = 0; dex_pc < code_item_->insns_size_in_code_units_;
Ian Rogers7b078e82014-09-10 14:44:24 -07001272 dex_pc += inst->SizeInCodeUnits()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001273 RegisterLine* reg_line = reg_table_.GetLine(dex_pc);
Ian Rogers7b078e82014-09-10 14:44:24 -07001274 if (reg_line != nullptr) {
1275 indent_os << reg_line->Dump(this) << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07001276 }
Ian Rogers7b3ddd22013-02-21 15:19:52 -08001277 indent_os << StringPrintf("0x%04zx", dex_pc) << ": " << insn_flags_[dex_pc].ToString() << " ";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001278 const bool kDumpHexOfInstruction = false;
1279 if (kDumpHexOfInstruction) {
1280 indent_os << inst->DumpHex(5) << " ";
1281 }
1282 indent_os << inst->DumpString(dex_file_) << "\n";
jeffhaoba5ebb92011-08-25 17:24:37 -07001283 inst = inst->Next();
1284 }
jeffhaobdb76512011-09-07 11:43:16 -07001285}
1286
Ian Rogersd81871c2011-10-03 13:57:23 -07001287static bool IsPrimitiveDescriptor(char descriptor) {
1288 switch (descriptor) {
jeffhaobdb76512011-09-07 11:43:16 -07001289 case 'I':
1290 case 'C':
1291 case 'S':
1292 case 'B':
1293 case 'Z':
jeffhaobdb76512011-09-07 11:43:16 -07001294 case 'F':
1295 case 'D':
1296 case 'J':
Ian Rogersd81871c2011-10-03 13:57:23 -07001297 return true;
jeffhaobdb76512011-09-07 11:43:16 -07001298 default:
1299 return false;
1300 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001301}
1302
Ian Rogers776ac1f2012-04-13 23:36:36 -07001303bool MethodVerifier::SetTypesFromSignature() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001304 RegisterLine* reg_line = reg_table_.GetLine(0);
Andreas Gampeef0b1a12015-06-19 20:37:46 -07001305
1306 // Should have been verified earlier.
1307 DCHECK_GE(code_item_->registers_size_, code_item_->ins_size_);
1308
1309 uint32_t arg_start = code_item_->registers_size_ - code_item_->ins_size_;
Ian Rogersd81871c2011-10-03 13:57:23 -07001310 size_t expected_args = code_item_->ins_size_; /* long/double count as two */
jeffhaobdb76512011-09-07 11:43:16 -07001311
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001312 // Include the "this" pointer.
Ian Rogersd81871c2011-10-03 13:57:23 -07001313 size_t cur_arg = 0;
Ian Rogersad0b3a32012-04-16 14:50:24 -07001314 if (!IsStatic()) {
Andreas Gampeef0b1a12015-06-19 20:37:46 -07001315 if (expected_args == 0) {
1316 // Expect at least a receiver.
1317 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected 0 args, but method is not static";
1318 return false;
1319 }
1320
Ian Rogersd81871c2011-10-03 13:57:23 -07001321 // If this is a constructor for a class other than java.lang.Object, mark the first ("this")
1322 // argument as uninitialized. This restricts field access until the superclass constructor is
1323 // called.
Ian Rogersd8f69b02014-09-10 21:43:52 +00001324 const RegType& declaring_class = GetDeclaringClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07001325 if (IsConstructor() && !declaring_class.IsJavaLangObject()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07001326 reg_line->SetRegisterType(this, arg_start + cur_arg,
Ian Rogersd81871c2011-10-03 13:57:23 -07001327 reg_types_.UninitializedThisArgument(declaring_class));
1328 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07001329 reg_line->SetRegisterType(this, arg_start + cur_arg, declaring_class);
jeffhaobdb76512011-09-07 11:43:16 -07001330 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001331 cur_arg++;
jeffhaobdb76512011-09-07 11:43:16 -07001332 }
1333
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001334 const DexFile::ProtoId& proto_id =
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001335 dex_file_->GetMethodPrototype(dex_file_->GetMethodId(dex_method_idx_));
Ian Rogers0571d352011-11-03 19:51:38 -07001336 DexFileParameterIterator iterator(*dex_file_, proto_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07001337
1338 for (; iterator.HasNext(); iterator.Next()) {
1339 const char* descriptor = iterator.GetDescriptor();
Ian Rogers7b078e82014-09-10 14:44:24 -07001340 if (descriptor == nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001341 LOG(FATAL) << "Null descriptor";
1342 }
1343 if (cur_arg >= expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07001344 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1345 << " args, found more (" << descriptor << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001346 return false;
1347 }
1348 switch (descriptor[0]) {
1349 case 'L':
1350 case '[':
1351 // We assume that reference arguments are initialized. The only way it could be otherwise
1352 // (assuming the caller was verified) is if the current method is <init>, but in that case
1353 // it's effectively considered initialized the instant we reach here (in the sense that we
1354 // can return without doing anything or call virtual methods).
1355 {
Ian Rogersd8f69b02014-09-10 21:43:52 +00001356 const RegType& reg_type = ResolveClassAndCheckAccess(iterator.GetTypeIdx());
Sebastien Hertz2ed76f92014-04-22 17:11:08 +02001357 if (!reg_type.IsNonZeroReferenceTypes()) {
1358 DCHECK(HasFailures());
1359 return false;
1360 }
Ian Rogers7b078e82014-09-10 14:44:24 -07001361 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001362 }
1363 break;
1364 case 'Z':
Ian Rogers7b078e82014-09-10 14:44:24 -07001365 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_types_.Boolean());
Ian Rogersd81871c2011-10-03 13:57:23 -07001366 break;
1367 case 'C':
Ian Rogers7b078e82014-09-10 14:44:24 -07001368 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_types_.Char());
Ian Rogersd81871c2011-10-03 13:57:23 -07001369 break;
1370 case 'B':
Ian Rogers7b078e82014-09-10 14:44:24 -07001371 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_types_.Byte());
Ian Rogersd81871c2011-10-03 13:57:23 -07001372 break;
1373 case 'I':
Ian Rogers7b078e82014-09-10 14:44:24 -07001374 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_types_.Integer());
Ian Rogersd81871c2011-10-03 13:57:23 -07001375 break;
1376 case 'S':
Ian Rogers7b078e82014-09-10 14:44:24 -07001377 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_types_.Short());
Ian Rogersd81871c2011-10-03 13:57:23 -07001378 break;
1379 case 'F':
Ian Rogers7b078e82014-09-10 14:44:24 -07001380 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_types_.Float());
Ian Rogersd81871c2011-10-03 13:57:23 -07001381 break;
1382 case 'J':
1383 case 'D': {
Andreas Gampe77cd4d62014-06-19 17:29:48 -07001384 if (cur_arg + 1 >= expected_args) {
1385 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1386 << " args, found more (" << descriptor << ")";
1387 return false;
1388 }
1389
Ian Rogers7b078e82014-09-10 14:44:24 -07001390 const RegType* lo_half;
1391 const RegType* hi_half;
1392 if (descriptor[0] == 'J') {
1393 lo_half = &reg_types_.LongLo();
1394 hi_half = &reg_types_.LongHi();
1395 } else {
1396 lo_half = &reg_types_.DoubleLo();
1397 hi_half = &reg_types_.DoubleHi();
1398 }
1399 reg_line->SetRegisterTypeWide(this, arg_start + cur_arg, *lo_half, *hi_half);
Ian Rogersd81871c2011-10-03 13:57:23 -07001400 cur_arg++;
1401 break;
1402 }
1403 default:
Brian Carlstrom93c33962013-07-26 10:37:43 -07001404 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected signature type char '"
1405 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001406 return false;
1407 }
1408 cur_arg++;
1409 }
1410 if (cur_arg != expected_args) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001411 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1412 << " arguments, found " << cur_arg;
Ian Rogersd81871c2011-10-03 13:57:23 -07001413 return false;
1414 }
1415 const char* descriptor = dex_file_->GetReturnTypeDescriptor(proto_id);
1416 // Validate return type. We don't do the type lookup; just want to make sure that it has the right
1417 // format. Only major difference from the method argument format is that 'V' is supported.
1418 bool result;
1419 if (IsPrimitiveDescriptor(descriptor[0]) || descriptor[0] == 'V') {
1420 result = descriptor[1] == '\0';
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001421 } else if (descriptor[0] == '[') { // single/multi-dimensional array of object/primitive
Ian Rogersd81871c2011-10-03 13:57:23 -07001422 size_t i = 0;
1423 do {
1424 i++;
1425 } while (descriptor[i] == '['); // process leading [
1426 if (descriptor[i] == 'L') { // object array
1427 do {
1428 i++; // find closing ;
1429 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1430 result = descriptor[i] == ';';
1431 } else { // primitive array
1432 result = IsPrimitiveDescriptor(descriptor[i]) && descriptor[i + 1] == '\0';
1433 }
1434 } else if (descriptor[0] == 'L') {
1435 // could be more thorough here, but shouldn't be required
1436 size_t i = 0;
1437 do {
1438 i++;
1439 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1440 result = descriptor[i] == ';';
1441 } else {
1442 result = false;
1443 }
1444 if (!result) {
jeffhaod5347e02012-03-22 17:25:05 -07001445 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected char in return type descriptor '"
1446 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001447 }
1448 return result;
jeffhaobdb76512011-09-07 11:43:16 -07001449}
1450
Ian Rogers776ac1f2012-04-13 23:36:36 -07001451bool MethodVerifier::CodeFlowVerifyMethod() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001452 const uint16_t* insns = code_item_->insns_;
1453 const uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaoba5ebb92011-08-25 17:24:37 -07001454
jeffhaobdb76512011-09-07 11:43:16 -07001455 /* Begin by marking the first instruction as "changed". */
Ian Rogersd81871c2011-10-03 13:57:23 -07001456 insn_flags_[0].SetChanged();
1457 uint32_t start_guess = 0;
jeffhaoba5ebb92011-08-25 17:24:37 -07001458
jeffhaobdb76512011-09-07 11:43:16 -07001459 /* Continue until no instructions are marked "changed". */
1460 while (true) {
Mathieu Chartier4306ef82014-12-19 18:41:47 -08001461 if (allow_thread_suspension_) {
1462 self_->AllowThreadSuspension();
1463 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001464 // Find the first marked one. Use "start_guess" as a way to find one quickly.
1465 uint32_t insn_idx = start_guess;
1466 for (; insn_idx < insns_size; insn_idx++) {
1467 if (insn_flags_[insn_idx].IsChanged())
jeffhaobdb76512011-09-07 11:43:16 -07001468 break;
1469 }
jeffhaobdb76512011-09-07 11:43:16 -07001470 if (insn_idx == insns_size) {
1471 if (start_guess != 0) {
1472 /* try again, starting from the top */
1473 start_guess = 0;
1474 continue;
1475 } else {
1476 /* all flags are clear */
1477 break;
1478 }
1479 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001480 // We carry the working set of registers from instruction to instruction. If this address can
1481 // be the target of a branch (or throw) instruction, or if we're skipping around chasing
1482 // "changed" flags, we need to load the set of registers from the table.
1483 // Because we always prefer to continue on to the next instruction, we should never have a
1484 // situation where we have a stray "changed" flag set on an instruction that isn't a branch
1485 // target.
1486 work_insn_idx_ = insn_idx;
1487 if (insn_flags_[insn_idx].IsBranchTarget()) {
1488 work_line_->CopyFromLine(reg_table_.GetLine(insn_idx));
Ian Rogersebbdd872014-07-07 23:53:08 -07001489 } else if (kIsDebugBuild) {
jeffhaobdb76512011-09-07 11:43:16 -07001490 /*
1491 * Sanity check: retrieve the stored register line (assuming
1492 * a full table) and make sure it actually matches.
1493 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001494 RegisterLine* register_line = reg_table_.GetLine(insn_idx);
Ian Rogers7b078e82014-09-10 14:44:24 -07001495 if (register_line != nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001496 if (work_line_->CompareLine(register_line) != 0) {
1497 Dump(std::cout);
1498 std::cout << info_messages_.str();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001499 LOG(FATAL) << "work_line diverged in " << PrettyMethod(dex_method_idx_, *dex_file_)
Elliott Hughesc073b072012-05-24 19:29:17 -07001500 << "@" << reinterpret_cast<void*>(work_insn_idx_) << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07001501 << " work_line=" << work_line_->Dump(this) << "\n"
1502 << " expected=" << register_line->Dump(this);
Ian Rogersd81871c2011-10-03 13:57:23 -07001503 }
jeffhaobdb76512011-09-07 11:43:16 -07001504 }
jeffhaobdb76512011-09-07 11:43:16 -07001505 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001506 if (!CodeFlowVerifyInstruction(&start_guess)) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001507 std::string prepend(PrettyMethod(dex_method_idx_, *dex_file_));
Ian Rogersad0b3a32012-04-16 14:50:24 -07001508 prepend += " failed to verify: ";
1509 PrependToLastFailMessage(prepend);
jeffhaoba5ebb92011-08-25 17:24:37 -07001510 return false;
1511 }
jeffhaobdb76512011-09-07 11:43:16 -07001512 /* Clear "changed" and mark as visited. */
Ian Rogersd81871c2011-10-03 13:57:23 -07001513 insn_flags_[insn_idx].SetVisited();
1514 insn_flags_[insn_idx].ClearChanged();
jeffhaobdb76512011-09-07 11:43:16 -07001515 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001516
Ian Rogers1c849e52012-06-28 14:00:33 -07001517 if (gDebugVerify) {
jeffhaobdb76512011-09-07 11:43:16 -07001518 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001519 * Scan for dead code. There's nothing "evil" about dead code
jeffhaobdb76512011-09-07 11:43:16 -07001520 * (besides the wasted space), but it indicates a flaw somewhere
1521 * down the line, possibly in the verifier.
1522 *
1523 * If we've substituted "always throw" instructions into the stream,
1524 * we are almost certainly going to have some dead code.
1525 */
1526 int dead_start = -1;
Ian Rogersd81871c2011-10-03 13:57:23 -07001527 uint32_t insn_idx = 0;
Ian Rogers7b078e82014-09-10 14:44:24 -07001528 for (; insn_idx < insns_size;
1529 insn_idx += Instruction::At(code_item_->insns_ + insn_idx)->SizeInCodeUnits()) {
jeffhaobdb76512011-09-07 11:43:16 -07001530 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001531 * Switch-statement data doesn't get "visited" by scanner. It
jeffhaobdb76512011-09-07 11:43:16 -07001532 * may or may not be preceded by a padding NOP (for alignment).
1533 */
1534 if (insns[insn_idx] == Instruction::kPackedSwitchSignature ||
1535 insns[insn_idx] == Instruction::kSparseSwitchSignature ||
1536 insns[insn_idx] == Instruction::kArrayDataSignature ||
Elliott Hughes380aaa72012-07-09 14:33:15 -07001537 (insns[insn_idx] == Instruction::NOP && (insn_idx + 1 < insns_size) &&
jeffhaobdb76512011-09-07 11:43:16 -07001538 (insns[insn_idx + 1] == Instruction::kPackedSwitchSignature ||
1539 insns[insn_idx + 1] == Instruction::kSparseSwitchSignature ||
1540 insns[insn_idx + 1] == Instruction::kArrayDataSignature))) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001541 insn_flags_[insn_idx].SetVisited();
jeffhaobdb76512011-09-07 11:43:16 -07001542 }
1543
Ian Rogersd81871c2011-10-03 13:57:23 -07001544 if (!insn_flags_[insn_idx].IsVisited()) {
jeffhaobdb76512011-09-07 11:43:16 -07001545 if (dead_start < 0)
1546 dead_start = insn_idx;
1547 } else if (dead_start >= 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001548 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start)
1549 << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaobdb76512011-09-07 11:43:16 -07001550 dead_start = -1;
1551 }
1552 }
1553 if (dead_start >= 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001554 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start)
1555 << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaoba5ebb92011-08-25 17:24:37 -07001556 }
Ian Rogersc9e463c2013-06-05 16:52:26 -07001557 // To dump the state of the verify after a method, do something like:
1558 // if (PrettyMethod(dex_method_idx_, *dex_file_) ==
1559 // "boolean java.lang.String.equals(java.lang.Object)") {
1560 // LOG(INFO) << info_messages_.str();
1561 // }
jeffhaoba5ebb92011-08-25 17:24:37 -07001562 }
jeffhaobdb76512011-09-07 11:43:16 -07001563 return true;
1564}
1565
Andreas Gampe68df3202015-06-22 11:35:46 -07001566// Returns the index of the first final instance field of the given class, or kDexNoIndex if there
1567// is no such field.
1568static uint32_t GetFirstFinalInstanceFieldIndex(const DexFile& dex_file, uint16_t type_idx) {
1569 const DexFile::ClassDef* class_def = dex_file.FindClassDef(type_idx);
1570 DCHECK(class_def != nullptr);
1571 const uint8_t* class_data = dex_file.GetClassData(*class_def);
1572 DCHECK(class_data != nullptr);
1573 ClassDataItemIterator it(dex_file, class_data);
1574 // Skip static fields.
1575 while (it.HasNextStaticField()) {
1576 it.Next();
1577 }
1578 while (it.HasNextInstanceField()) {
1579 if ((it.GetFieldAccessFlags() & kAccFinal) != 0) {
1580 return it.GetMemberIndex();
1581 }
1582 it.Next();
1583 }
1584 return DexFile::kDexNoIndex;
1585}
1586
Ian Rogers776ac1f2012-04-13 23:36:36 -07001587bool MethodVerifier::CodeFlowVerifyInstruction(uint32_t* start_guess) {
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001588 // If we're doing FindLocksAtDexPc, check whether we're at the dex pc we care about.
1589 // We want the state _before_ the instruction, for the case where the dex pc we're
1590 // interested in is itself a monitor-enter instruction (which is a likely place
1591 // for a thread to be suspended).
Ian Rogers7b078e82014-09-10 14:44:24 -07001592 if (monitor_enter_dex_pcs_ != nullptr && work_insn_idx_ == interesting_dex_pc_) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001593 monitor_enter_dex_pcs_->clear(); // The new work line is more accurate than the previous one.
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001594 for (size_t i = 0; i < work_line_->GetMonitorEnterCount(); ++i) {
1595 monitor_enter_dex_pcs_->push_back(work_line_->GetMonitorEnterDexPc(i));
1596 }
1597 }
1598
jeffhaobdb76512011-09-07 11:43:16 -07001599 /*
1600 * Once we finish decoding the instruction, we need to figure out where
jeffhaod1f0fde2011-09-08 17:25:33 -07001601 * we can go from here. There are three possible ways to transfer
jeffhaobdb76512011-09-07 11:43:16 -07001602 * control to another statement:
1603 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001604 * (1) Continue to the next instruction. Applies to all but
jeffhaobdb76512011-09-07 11:43:16 -07001605 * unconditional branches, method returns, and exception throws.
jeffhaod1f0fde2011-09-08 17:25:33 -07001606 * (2) Branch to one or more possible locations. Applies to branches
jeffhaobdb76512011-09-07 11:43:16 -07001607 * and switch statements.
jeffhaod1f0fde2011-09-08 17:25:33 -07001608 * (3) Exception handlers. Applies to any instruction that can
jeffhaobdb76512011-09-07 11:43:16 -07001609 * throw an exception that is handled by an encompassing "try"
1610 * block.
1611 *
1612 * We can also return, in which case there is no successor instruction
1613 * from this point.
1614 *
Elliott Hughesadb8c672012-03-06 16:49:32 -08001615 * The behavior can be determined from the opcode flags.
jeffhaobdb76512011-09-07 11:43:16 -07001616 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001617 const uint16_t* insns = code_item_->insns_ + work_insn_idx_;
1618 const Instruction* inst = Instruction::At(insns);
Ian Rogersa75a0132012-09-28 11:41:42 -07001619 int opcode_flags = Instruction::FlagsOf(inst->Opcode());
jeffhaobdb76512011-09-07 11:43:16 -07001620
jeffhaobdb76512011-09-07 11:43:16 -07001621 int32_t branch_target = 0;
jeffhaobdb76512011-09-07 11:43:16 -07001622 bool just_set_result = false;
Ian Rogers2c8a8572011-10-24 17:11:36 -07001623 if (gDebugVerify) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001624 // Generate processing back trace to debug verifier
Elliott Hughesc073b072012-05-24 19:29:17 -07001625 LogVerifyInfo() << "Processing " << inst->DumpString(dex_file_) << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07001626 << work_line_->Dump(this) << "\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001627 }
jeffhaobdb76512011-09-07 11:43:16 -07001628
1629 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001630 * Make a copy of the previous register state. If the instruction
jeffhaobdb76512011-09-07 11:43:16 -07001631 * can throw an exception, we will copy/merge this into the "catch"
1632 * address rather than work_line, because we don't want the result
1633 * from the "successful" code path (e.g. a check-cast that "improves"
1634 * a type) to be visible to the exception handler.
1635 */
Ian Rogers776ac1f2012-04-13 23:36:36 -07001636 if ((opcode_flags & Instruction::kThrow) != 0 && CurrentInsnFlags()->IsInTry()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001637 saved_line_->CopyFromLine(work_line_.get());
Ian Rogers1ff3c982014-08-12 02:30:58 -07001638 } else if (kIsDebugBuild) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001639 saved_line_->FillWithGarbage();
jeffhaobdb76512011-09-07 11:43:16 -07001640 }
Andreas Gamped12e7822015-06-25 10:26:40 -07001641 DCHECK(!have_pending_runtime_throw_failure_); // Per-instruction flag, should not be set here.
jeffhaobdb76512011-09-07 11:43:16 -07001642
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07001643
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001644 // We need to ensure the work line is consistent while performing validation. When we spot a
1645 // peephole pattern we compute a new line for either the fallthrough instruction or the
1646 // branch target.
Ian Rogers700a4022014-05-19 16:49:03 -07001647 std::unique_ptr<RegisterLine> branch_line;
1648 std::unique_ptr<RegisterLine> fallthrough_line;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001649
Stephen Kyle7e541c92014-12-17 17:10:02 +00001650 /*
1651 * If we are in a constructor, and we currently have an UninitializedThis type
1652 * in a register somewhere, we need to make sure it isn't overwritten.
1653 */
1654 bool track_uninitialized_this = false;
1655 size_t uninitialized_this_loc = 0;
1656 if (IsConstructor()) {
1657 track_uninitialized_this = work_line_->GetUninitializedThisLoc(this, &uninitialized_this_loc);
1658 }
1659
Sebastien Hertz5243e912013-05-21 10:55:07 +02001660 switch (inst->Opcode()) {
jeffhaobdb76512011-09-07 11:43:16 -07001661 case Instruction::NOP:
1662 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001663 * A "pure" NOP has no effect on anything. Data tables start with
jeffhaobdb76512011-09-07 11:43:16 -07001664 * a signature that looks like a NOP; if we see one of these in
1665 * the course of executing code then we have a problem.
1666 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001667 if (inst->VRegA_10x() != 0) {
jeffhaod5347e02012-03-22 17:25:05 -07001668 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "encountered data table in instruction stream";
jeffhaobdb76512011-09-07 11:43:16 -07001669 }
1670 break;
1671
1672 case Instruction::MOVE:
Ian Rogers7b078e82014-09-10 14:44:24 -07001673 work_line_->CopyRegister1(this, inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategory1nr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001674 break;
jeffhaobdb76512011-09-07 11:43:16 -07001675 case Instruction::MOVE_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001676 work_line_->CopyRegister1(this, inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategory1nr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001677 break;
jeffhaobdb76512011-09-07 11:43:16 -07001678 case Instruction::MOVE_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001679 work_line_->CopyRegister1(this, inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategory1nr);
jeffhaobdb76512011-09-07 11:43:16 -07001680 break;
1681 case Instruction::MOVE_WIDE:
Ian Rogers7b078e82014-09-10 14:44:24 -07001682 work_line_->CopyRegister2(this, inst->VRegA_12x(), inst->VRegB_12x());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001683 break;
jeffhaobdb76512011-09-07 11:43:16 -07001684 case Instruction::MOVE_WIDE_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001685 work_line_->CopyRegister2(this, inst->VRegA_22x(), inst->VRegB_22x());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001686 break;
jeffhaobdb76512011-09-07 11:43:16 -07001687 case Instruction::MOVE_WIDE_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001688 work_line_->CopyRegister2(this, inst->VRegA_32x(), inst->VRegB_32x());
jeffhaobdb76512011-09-07 11:43:16 -07001689 break;
1690 case Instruction::MOVE_OBJECT:
Ian Rogers7b078e82014-09-10 14:44:24 -07001691 work_line_->CopyRegister1(this, inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategoryRef);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001692 break;
jeffhaobdb76512011-09-07 11:43:16 -07001693 case Instruction::MOVE_OBJECT_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001694 work_line_->CopyRegister1(this, inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategoryRef);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001695 break;
jeffhaobdb76512011-09-07 11:43:16 -07001696 case Instruction::MOVE_OBJECT_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001697 work_line_->CopyRegister1(this, inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategoryRef);
jeffhaobdb76512011-09-07 11:43:16 -07001698 break;
1699
1700 /*
1701 * The move-result instructions copy data out of a "pseudo-register"
jeffhaod1f0fde2011-09-08 17:25:33 -07001702 * with the results from the last method invocation. In practice we
jeffhaobdb76512011-09-07 11:43:16 -07001703 * might want to hold the result in an actual CPU register, so the
1704 * Dalvik spec requires that these only appear immediately after an
1705 * invoke or filled-new-array.
1706 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001707 * These calls invalidate the "result" register. (This is now
jeffhaobdb76512011-09-07 11:43:16 -07001708 * redundant with the reset done below, but it can make the debug info
1709 * easier to read in some cases.)
1710 */
1711 case Instruction::MOVE_RESULT:
Ian Rogers7b078e82014-09-10 14:44:24 -07001712 work_line_->CopyResultRegister1(this, inst->VRegA_11x(), false);
jeffhaobdb76512011-09-07 11:43:16 -07001713 break;
1714 case Instruction::MOVE_RESULT_WIDE:
Ian Rogers7b078e82014-09-10 14:44:24 -07001715 work_line_->CopyResultRegister2(this, inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07001716 break;
1717 case Instruction::MOVE_RESULT_OBJECT:
Ian Rogers7b078e82014-09-10 14:44:24 -07001718 work_line_->CopyResultRegister1(this, inst->VRegA_11x(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001719 break;
1720
Ian Rogersd81871c2011-10-03 13:57:23 -07001721 case Instruction::MOVE_EXCEPTION: {
Sebastien Hertz270a0e12015-01-16 19:49:09 +01001722 // We do not allow MOVE_EXCEPTION as the first instruction in a method. This is a simple case
1723 // where one entrypoint to the catch block is not actually an exception path.
1724 if (work_insn_idx_ == 0) {
1725 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "move-exception at pc 0x0";
1726 break;
1727 }
jeffhaobdb76512011-09-07 11:43:16 -07001728 /*
jeffhao60f83e32012-02-13 17:16:30 -08001729 * This statement can only appear as the first instruction in an exception handler. We verify
1730 * that as part of extracting the exception type from the catch block list.
jeffhaobdb76512011-09-07 11:43:16 -07001731 */
Ian Rogersd8f69b02014-09-10 21:43:52 +00001732 const RegType& res_type = GetCaughtExceptionType();
Ian Rogers7b078e82014-09-10 14:44:24 -07001733 work_line_->SetRegisterType(this, inst->VRegA_11x(), res_type);
jeffhaobdb76512011-09-07 11:43:16 -07001734 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001735 }
jeffhaobdb76512011-09-07 11:43:16 -07001736 case Instruction::RETURN_VOID:
Ian Rogers7b078e82014-09-10 14:44:24 -07001737 if (!IsConstructor() || work_line_->CheckConstructorReturn(this)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001738 if (!GetMethodReturnType().IsConflict()) {
jeffhaod5347e02012-03-22 17:25:05 -07001739 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001740 }
jeffhaobdb76512011-09-07 11:43:16 -07001741 }
1742 break;
1743 case Instruction::RETURN:
Ian Rogers7b078e82014-09-10 14:44:24 -07001744 if (!IsConstructor() || work_line_->CheckConstructorReturn(this)) {
jeffhaobdb76512011-09-07 11:43:16 -07001745 /* check the method signature */
Ian Rogersd8f69b02014-09-10 21:43:52 +00001746 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07001747 if (!return_type.IsCategory1Types()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001748 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected non-category 1 return type "
1749 << return_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001750 } else {
1751 // Compilers may generate synthetic functions that write byte values into boolean fields.
1752 // Also, it may use integer values for boolean, byte, short, and character return types.
Sebastien Hertz5243e912013-05-21 10:55:07 +02001753 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07001754 const RegType& src_type = work_line_->GetRegisterType(this, vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07001755 bool use_src = ((return_type.IsBoolean() && src_type.IsByte()) ||
1756 ((return_type.IsBoolean() || return_type.IsByte() ||
1757 return_type.IsShort() || return_type.IsChar()) &&
1758 src_type.IsInteger()));
1759 /* check the register contents */
Ian Rogersad0b3a32012-04-16 14:50:24 -07001760 bool success =
Ian Rogers7b078e82014-09-10 14:44:24 -07001761 work_line_->VerifyRegisterType(this, vregA, use_src ? src_type : return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001762 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001763 AppendToLastFailMessage(StringPrintf(" return-1nr on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07001764 }
jeffhaobdb76512011-09-07 11:43:16 -07001765 }
1766 }
1767 break;
1768 case Instruction::RETURN_WIDE:
Ian Rogers7b078e82014-09-10 14:44:24 -07001769 if (!IsConstructor() || work_line_->CheckConstructorReturn(this)) {
jeffhaobdb76512011-09-07 11:43:16 -07001770 /* check the method signature */
Ian Rogersd8f69b02014-09-10 21:43:52 +00001771 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07001772 if (!return_type.IsCategory2Types()) {
jeffhaod5347e02012-03-22 17:25:05 -07001773 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-wide not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001774 } else {
1775 /* check the register contents */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001776 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07001777 bool success = work_line_->VerifyRegisterType(this, vregA, return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001778 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001779 AppendToLastFailMessage(StringPrintf(" return-wide on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07001780 }
jeffhaobdb76512011-09-07 11:43:16 -07001781 }
1782 }
1783 break;
1784 case Instruction::RETURN_OBJECT:
Ian Rogers7b078e82014-09-10 14:44:24 -07001785 if (!IsConstructor() || work_line_->CheckConstructorReturn(this)) {
Ian Rogersd8f69b02014-09-10 21:43:52 +00001786 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07001787 if (!return_type.IsReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001788 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-object not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001789 } else {
1790 /* return_type is the *expected* return type, not register value */
1791 DCHECK(!return_type.IsZero());
1792 DCHECK(!return_type.IsUninitializedReference());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001793 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07001794 const RegType& reg_type = work_line_->GetRegisterType(this, vregA);
Andreas Gampea32210c2015-06-24 10:26:13 -07001795 // Disallow returning undefined, conflict & uninitialized values and verify that the
1796 // reference in vAA is an instance of the "return_type."
1797 if (reg_type.IsUndefined()) {
1798 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning undefined register";
1799 } else if (reg_type.IsConflict()) {
1800 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning register with conflict";
1801 } else if (reg_type.IsUninitializedTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001802 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "returning uninitialized object '"
1803 << reg_type << "'";
Ian Rogers9074b992011-10-26 17:41:55 -07001804 } else if (!return_type.IsAssignableFrom(reg_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07001805 if (reg_type.IsUnresolvedTypes() || return_type.IsUnresolvedTypes()) {
1806 Fail(VERIFY_ERROR_NO_CLASS) << " can't resolve returned type '" << return_type
1807 << "' or '" << reg_type << "'";
1808 } else {
Andreas Gampe16f149c2015-03-23 10:10:20 -07001809 bool soft_error = false;
1810 // Check whether arrays are involved. They will show a valid class status, even
1811 // if their components are erroneous.
1812 if (reg_type.IsArrayTypes() && return_type.IsArrayTypes()) {
1813 return_type.CanAssignArray(reg_type, reg_types_, class_loader_, &soft_error);
1814 if (soft_error) {
1815 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "array with erroneous component type: "
1816 << reg_type << " vs " << return_type;
1817 }
1818 }
1819
1820 if (!soft_error) {
1821 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning '" << reg_type
1822 << "', but expected from declaration '" << return_type << "'";
1823 }
Jeff Haoa3faaf42013-09-03 19:07:00 -07001824 }
jeffhaobdb76512011-09-07 11:43:16 -07001825 }
1826 }
1827 }
1828 break;
1829
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001830 /* could be boolean, int, float, or a null reference */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001831 case Instruction::CONST_4: {
1832 int32_t val = static_cast<int32_t>(inst->VRegB_11n() << 28) >> 28;
Ian Rogers7b078e82014-09-10 14:44:24 -07001833 work_line_->SetRegisterType(this, inst->VRegA_11n(),
Ian Rogers46960fe2014-05-23 10:43:43 -07001834 DetermineCat1Constant(val, need_precise_constants_));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001835 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02001836 }
1837 case Instruction::CONST_16: {
1838 int16_t val = static_cast<int16_t>(inst->VRegB_21s());
Ian Rogers7b078e82014-09-10 14:44:24 -07001839 work_line_->SetRegisterType(this, inst->VRegA_21s(),
Ian Rogers46960fe2014-05-23 10:43:43 -07001840 DetermineCat1Constant(val, need_precise_constants_));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001841 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02001842 }
Sebastien Hertz849600b2013-12-20 10:28:08 +01001843 case Instruction::CONST: {
1844 int32_t val = inst->VRegB_31i();
Ian Rogers7b078e82014-09-10 14:44:24 -07001845 work_line_->SetRegisterType(this, inst->VRegA_31i(),
Ian Rogers46960fe2014-05-23 10:43:43 -07001846 DetermineCat1Constant(val, need_precise_constants_));
jeffhaobdb76512011-09-07 11:43:16 -07001847 break;
Sebastien Hertz849600b2013-12-20 10:28:08 +01001848 }
1849 case Instruction::CONST_HIGH16: {
1850 int32_t val = static_cast<int32_t>(inst->VRegB_21h() << 16);
Ian Rogers7b078e82014-09-10 14:44:24 -07001851 work_line_->SetRegisterType(this, inst->VRegA_21h(),
Ian Rogers46960fe2014-05-23 10:43:43 -07001852 DetermineCat1Constant(val, need_precise_constants_));
jeffhaobdb76512011-09-07 11:43:16 -07001853 break;
Sebastien Hertz849600b2013-12-20 10:28:08 +01001854 }
jeffhaobdb76512011-09-07 11:43:16 -07001855 /* could be long or double; resolved upon use */
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001856 case Instruction::CONST_WIDE_16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001857 int64_t val = static_cast<int16_t>(inst->VRegB_21s());
Ian Rogersd8f69b02014-09-10 21:43:52 +00001858 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1859 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07001860 work_line_->SetRegisterTypeWide(this, inst->VRegA_21s(), lo, hi);
jeffhaobdb76512011-09-07 11:43:16 -07001861 break;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001862 }
1863 case Instruction::CONST_WIDE_32: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001864 int64_t val = static_cast<int32_t>(inst->VRegB_31i());
Ian Rogersd8f69b02014-09-10 21:43:52 +00001865 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1866 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07001867 work_line_->SetRegisterTypeWide(this, inst->VRegA_31i(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001868 break;
1869 }
1870 case Instruction::CONST_WIDE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001871 int64_t val = inst->VRegB_51l();
Ian Rogersd8f69b02014-09-10 21:43:52 +00001872 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1873 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07001874 work_line_->SetRegisterTypeWide(this, inst->VRegA_51l(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001875 break;
1876 }
1877 case Instruction::CONST_WIDE_HIGH16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001878 int64_t val = static_cast<uint64_t>(inst->VRegB_21h()) << 48;
Ian Rogersd8f69b02014-09-10 21:43:52 +00001879 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1880 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07001881 work_line_->SetRegisterTypeWide(this, inst->VRegA_21h(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001882 break;
1883 }
jeffhaobdb76512011-09-07 11:43:16 -07001884 case Instruction::CONST_STRING:
Ian Rogers7b078e82014-09-10 14:44:24 -07001885 work_line_->SetRegisterType(this, inst->VRegA_21c(), reg_types_.JavaLangString());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001886 break;
jeffhaobdb76512011-09-07 11:43:16 -07001887 case Instruction::CONST_STRING_JUMBO:
Ian Rogers7b078e82014-09-10 14:44:24 -07001888 work_line_->SetRegisterType(this, inst->VRegA_31c(), reg_types_.JavaLangString());
jeffhaobdb76512011-09-07 11:43:16 -07001889 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001890 case Instruction::CONST_CLASS: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001891 // Get type from instruction if unresolved then we need an access check
1892 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Ian Rogersd8f69b02014-09-10 21:43:52 +00001893 const RegType& res_type = ResolveClassAndCheckAccess(inst->VRegB_21c());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001894 // Register holds class, ie its type is class, on error it will hold Conflict.
Ian Rogers7b078e82014-09-10 14:44:24 -07001895 work_line_->SetRegisterType(this, inst->VRegA_21c(),
Ian Rogersb4903572012-10-11 11:52:56 -07001896 res_type.IsConflict() ? res_type
Ian Rogers7b078e82014-09-10 14:44:24 -07001897 : reg_types_.JavaLangClass());
jeffhaobdb76512011-09-07 11:43:16 -07001898 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001899 }
jeffhaobdb76512011-09-07 11:43:16 -07001900 case Instruction::MONITOR_ENTER:
Ian Rogers7b078e82014-09-10 14:44:24 -07001901 work_line_->PushMonitor(this, inst->VRegA_11x(), work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07001902 break;
1903 case Instruction::MONITOR_EXIT:
1904 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001905 * monitor-exit instructions are odd. They can throw exceptions,
jeffhaobdb76512011-09-07 11:43:16 -07001906 * but when they do they act as if they succeeded and the PC is
jeffhaod1f0fde2011-09-08 17:25:33 -07001907 * pointing to the following instruction. (This behavior goes back
jeffhaobdb76512011-09-07 11:43:16 -07001908 * to the need to handle asynchronous exceptions, a now-deprecated
1909 * feature that Dalvik doesn't support.)
1910 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001911 * In practice we don't need to worry about this. The only
jeffhaobdb76512011-09-07 11:43:16 -07001912 * exceptions that can be thrown from monitor-exit are for a
jeffhaod1f0fde2011-09-08 17:25:33 -07001913 * null reference and -exit without a matching -enter. If the
jeffhaobdb76512011-09-07 11:43:16 -07001914 * structured locking checks are working, the former would have
1915 * failed on the -enter instruction, and the latter is impossible.
1916 *
1917 * This is fortunate, because issue 3221411 prevents us from
1918 * chasing the "can throw" path when monitor verification is
jeffhaod1f0fde2011-09-08 17:25:33 -07001919 * enabled. If we can fully verify the locking we can ignore
jeffhaobdb76512011-09-07 11:43:16 -07001920 * some catch blocks (which will show up as "dead" code when
1921 * we skip them here); if we can't, then the code path could be
1922 * "live" so we still need to check it.
1923 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001924 opcode_flags &= ~Instruction::kThrow;
Ian Rogers7b078e82014-09-10 14:44:24 -07001925 work_line_->PopMonitor(this, inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07001926 break;
1927
Ian Rogers28ad40d2011-10-27 15:19:26 -07001928 case Instruction::CHECK_CAST:
Ian Rogersd81871c2011-10-03 13:57:23 -07001929 case Instruction::INSTANCE_OF: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001930 /*
1931 * If this instruction succeeds, we will "downcast" register vA to the type in vB. (This
1932 * could be a "upcast" -- not expected, so we don't try to address it.)
1933 *
1934 * If it fails, an exception is thrown, which we deal with later by ignoring the update to
Elliott Hughesadb8c672012-03-06 16:49:32 -08001935 * dec_insn.vA when branching to a handler.
Ian Rogers28ad40d2011-10-27 15:19:26 -07001936 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001937 const bool is_checkcast = (inst->Opcode() == Instruction::CHECK_CAST);
1938 const uint32_t type_idx = (is_checkcast) ? inst->VRegB_21c() : inst->VRegC_22c();
Ian Rogersd8f69b02014-09-10 21:43:52 +00001939 const RegType& res_type = ResolveClassAndCheckAccess(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001940 if (res_type.IsConflict()) {
Andreas Gampe00633eb2014-07-17 16:13:35 -07001941 // If this is a primitive type, fail HARD.
Mathieu Chartierbf99f772014-08-23 16:37:27 -07001942 mirror::Class* klass = dex_cache_->GetResolvedType(type_idx);
Andreas Gampe00633eb2014-07-17 16:13:35 -07001943 if (klass != nullptr && klass->IsPrimitive()) {
1944 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "using primitive type "
1945 << dex_file_->StringByTypeIdx(type_idx) << " in instanceof in "
1946 << GetDeclaringClass();
1947 break;
1948 }
1949
Ian Rogersad0b3a32012-04-16 14:50:24 -07001950 DCHECK_NE(failures_.size(), 0U);
1951 if (!is_checkcast) {
Ian Rogers7b078e82014-09-10 14:44:24 -07001952 work_line_->SetRegisterType(this, inst->VRegA_22c(), reg_types_.Boolean());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001953 }
1954 break; // bad class
Ian Rogers9f1ab122011-12-12 08:52:43 -08001955 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001956 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Sebastien Hertz5243e912013-05-21 10:55:07 +02001957 uint32_t orig_type_reg = (is_checkcast) ? inst->VRegA_21c() : inst->VRegB_22c();
Ian Rogers7b078e82014-09-10 14:44:24 -07001958 const RegType& orig_type = work_line_->GetRegisterType(this, orig_type_reg);
Ian Rogers28ad40d2011-10-27 15:19:26 -07001959 if (!res_type.IsNonZeroReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001960 if (is_checkcast) {
1961 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on unexpected class " << res_type;
1962 } else {
1963 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on unexpected class " << res_type;
1964 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001965 } else if (!orig_type.IsReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001966 if (is_checkcast) {
1967 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on non-reference in v" << orig_type_reg;
1968 } else {
1969 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on non-reference in v" << orig_type_reg;
1970 }
jeffhao2a8a90e2011-09-26 14:25:31 -07001971 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001972 if (is_checkcast) {
Ian Rogers7b078e82014-09-10 14:44:24 -07001973 work_line_->SetRegisterType(this, inst->VRegA_21c(), res_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001974 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07001975 work_line_->SetRegisterType(this, inst->VRegA_22c(), reg_types_.Boolean());
jeffhaobdb76512011-09-07 11:43:16 -07001976 }
jeffhaobdb76512011-09-07 11:43:16 -07001977 }
jeffhao2a8a90e2011-09-26 14:25:31 -07001978 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001979 }
1980 case Instruction::ARRAY_LENGTH: {
Ian Rogers7b078e82014-09-10 14:44:24 -07001981 const RegType& res_type = work_line_->GetRegisterType(this, inst->VRegB_12x());
Ian Rogers28ad40d2011-10-27 15:19:26 -07001982 if (res_type.IsReferenceTypes()) {
Ian Rogers89310de2012-02-01 13:47:30 -08001983 if (!res_type.IsArrayTypes() && !res_type.IsZero()) { // ie not an array or null
jeffhaod5347e02012-03-22 17:25:05 -07001984 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001985 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07001986 work_line_->SetRegisterType(this, inst->VRegA_12x(), reg_types_.Integer());
Ian Rogersd81871c2011-10-03 13:57:23 -07001987 }
Andreas Gampe65c9db82014-07-28 13:14:34 -07001988 } else {
1989 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001990 }
1991 break;
1992 }
1993 case Instruction::NEW_INSTANCE: {
Ian Rogersd8f69b02014-09-10 21:43:52 +00001994 const RegType& res_type = ResolveClassAndCheckAccess(inst->VRegB_21c());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001995 if (res_type.IsConflict()) {
1996 DCHECK_NE(failures_.size(), 0U);
1997 break; // bad class
jeffhao8cd6dda2012-02-22 10:15:34 -08001998 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001999 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
2000 // can't create an instance of an interface or abstract class */
2001 if (!res_type.IsInstantiableTypes()) {
2002 Fail(VERIFY_ERROR_INSTANTIATION)
2003 << "new-instance on primitive, interface or abstract class" << res_type;
Ian Rogers08f753d2012-08-24 14:35:25 -07002004 // Soft failure so carry on to set register type.
Ian Rogersd81871c2011-10-03 13:57:23 -07002005 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00002006 const RegType& uninit_type = reg_types_.Uninitialized(res_type, work_insn_idx_);
Ian Rogers08f753d2012-08-24 14:35:25 -07002007 // Any registers holding previous allocations from this address that have not yet been
2008 // initialized must be marked invalid.
Ian Rogers7b078e82014-09-10 14:44:24 -07002009 work_line_->MarkUninitRefsAsInvalid(this, uninit_type);
Ian Rogers08f753d2012-08-24 14:35:25 -07002010 // add the new uninitialized reference to the register state
Ian Rogers7b078e82014-09-10 14:44:24 -07002011 work_line_->SetRegisterType(this, inst->VRegA_21c(), uninit_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07002012 break;
2013 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08002014 case Instruction::NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002015 VerifyNewArray(inst, false, false);
jeffhaobdb76512011-09-07 11:43:16 -07002016 break;
2017 case Instruction::FILLED_NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002018 VerifyNewArray(inst, true, false);
Ian Rogers0c4a5062012-02-03 15:18:59 -08002019 just_set_result = true; // Filled new array sets result register
jeffhaobdb76512011-09-07 11:43:16 -07002020 break;
Ian Rogers0c4a5062012-02-03 15:18:59 -08002021 case Instruction::FILLED_NEW_ARRAY_RANGE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002022 VerifyNewArray(inst, true, true);
Ian Rogers0c4a5062012-02-03 15:18:59 -08002023 just_set_result = true; // Filled new array range sets result register
2024 break;
jeffhaobdb76512011-09-07 11:43:16 -07002025 case Instruction::CMPL_FLOAT:
2026 case Instruction::CMPG_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002027 if (!work_line_->VerifyRegisterType(this, inst->VRegB_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08002028 break;
2029 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002030 if (!work_line_->VerifyRegisterType(this, inst->VRegC_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08002031 break;
2032 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002033 work_line_->SetRegisterType(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002034 break;
2035 case Instruction::CMPL_DOUBLE:
2036 case Instruction::CMPG_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002037 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegB_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002038 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002039 break;
2040 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002041 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegC_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002042 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002043 break;
2044 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002045 work_line_->SetRegisterType(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002046 break;
2047 case Instruction::CMP_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002048 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegB_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002049 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002050 break;
2051 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002052 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegC_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002053 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002054 break;
2055 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002056 work_line_->SetRegisterType(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002057 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002058 case Instruction::THROW: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002059 const RegType& res_type = work_line_->GetRegisterType(this, inst->VRegA_11x());
Ian Rogersb4903572012-10-11 11:52:56 -07002060 if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(res_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07002061 Fail(res_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS : VERIFY_ERROR_BAD_CLASS_SOFT)
2062 << "thrown class " << res_type << " not instanceof Throwable";
jeffhaobdb76512011-09-07 11:43:16 -07002063 }
2064 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002065 }
jeffhaobdb76512011-09-07 11:43:16 -07002066 case Instruction::GOTO:
2067 case Instruction::GOTO_16:
2068 case Instruction::GOTO_32:
2069 /* no effect on or use of registers */
2070 break;
2071
2072 case Instruction::PACKED_SWITCH:
2073 case Instruction::SPARSE_SWITCH:
2074 /* verify that vAA is an integer, or can be converted to one */
Ian Rogers7b078e82014-09-10 14:44:24 -07002075 work_line_->VerifyRegisterType(this, inst->VRegA_31t(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002076 break;
2077
Ian Rogersd81871c2011-10-03 13:57:23 -07002078 case Instruction::FILL_ARRAY_DATA: {
2079 /* Similar to the verification done for APUT */
Ian Rogers7b078e82014-09-10 14:44:24 -07002080 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegA_31t());
Ian Rogers89310de2012-02-01 13:47:30 -08002081 /* array_type can be null if the reg type is Zero */
2082 if (!array_type.IsZero()) {
jeffhao457cc512012-02-02 16:55:13 -08002083 if (!array_type.IsArrayTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002084 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with array type "
2085 << array_type;
Ian Rogers89310de2012-02-01 13:47:30 -08002086 } else {
Ian Rogersd8f69b02014-09-10 21:43:52 +00002087 const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
Ian Rogersad0b3a32012-04-16 14:50:24 -07002088 DCHECK(!component_type.IsConflict());
jeffhao457cc512012-02-02 16:55:13 -08002089 if (component_type.IsNonZeroReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002090 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with component type "
2091 << component_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002092 } else {
jeffhao457cc512012-02-02 16:55:13 -08002093 // Now verify if the element width in the table matches the element width declared in
2094 // the array
2095 const uint16_t* array_data = insns + (insns[1] | (((int32_t) insns[2]) << 16));
2096 if (array_data[0] != Instruction::kArrayDataSignature) {
jeffhaod5347e02012-03-22 17:25:05 -07002097 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid magic for array-data";
jeffhao457cc512012-02-02 16:55:13 -08002098 } else {
2099 size_t elem_width = Primitive::ComponentSize(component_type.GetPrimitiveType());
2100 // Since we don't compress the data in Dex, expect to see equal width of data stored
2101 // in the table and expected from the array class.
2102 if (array_data[1] != elem_width) {
jeffhaod5347e02012-03-22 17:25:05 -07002103 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-data size mismatch (" << array_data[1]
2104 << " vs " << elem_width << ")";
jeffhao457cc512012-02-02 16:55:13 -08002105 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002106 }
2107 }
jeffhaobdb76512011-09-07 11:43:16 -07002108 }
2109 }
2110 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002111 }
jeffhaobdb76512011-09-07 11:43:16 -07002112 case Instruction::IF_EQ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002113 case Instruction::IF_NE: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002114 const RegType& reg_type1 = work_line_->GetRegisterType(this, inst->VRegA_22t());
2115 const RegType& reg_type2 = work_line_->GetRegisterType(this, inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002116 bool mismatch = false;
2117 if (reg_type1.IsZero()) { // zero then integral or reference expected
2118 mismatch = !reg_type2.IsReferenceTypes() && !reg_type2.IsIntegralTypes();
2119 } else if (reg_type1.IsReferenceTypes()) { // both references?
2120 mismatch = !reg_type2.IsReferenceTypes();
2121 } else { // both integral?
2122 mismatch = !reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes();
2123 }
2124 if (mismatch) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002125 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to if-eq/if-ne (" << reg_type1 << ","
2126 << reg_type2 << ") must both be references or integral";
jeffhaobdb76512011-09-07 11:43:16 -07002127 }
2128 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002129 }
jeffhaobdb76512011-09-07 11:43:16 -07002130 case Instruction::IF_LT:
2131 case Instruction::IF_GE:
2132 case Instruction::IF_GT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002133 case Instruction::IF_LE: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002134 const RegType& reg_type1 = work_line_->GetRegisterType(this, inst->VRegA_22t());
2135 const RegType& reg_type2 = work_line_->GetRegisterType(this, inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002136 if (!reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002137 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to 'if' (" << reg_type1 << ","
2138 << reg_type2 << ") must be integral";
jeffhaobdb76512011-09-07 11:43:16 -07002139 }
2140 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002141 }
jeffhaobdb76512011-09-07 11:43:16 -07002142 case Instruction::IF_EQZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002143 case Instruction::IF_NEZ: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002144 const RegType& reg_type = work_line_->GetRegisterType(this, inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002145 if (!reg_type.IsReferenceTypes() && !reg_type.IsIntegralTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002146 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
2147 << " unexpected as arg to if-eqz/if-nez";
Ian Rogersd81871c2011-10-03 13:57:23 -07002148 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002149
2150 // Find previous instruction - its existence is a precondition to peephole optimization.
Ian Rogers9b360392013-06-06 14:45:07 -07002151 uint32_t instance_of_idx = 0;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002152 if (0 != work_insn_idx_) {
Ian Rogers9b360392013-06-06 14:45:07 -07002153 instance_of_idx = work_insn_idx_ - 1;
Brian Carlstromdf629502013-07-17 22:39:56 -07002154 while (0 != instance_of_idx && !insn_flags_[instance_of_idx].IsOpcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07002155 instance_of_idx--;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002156 }
Andreas Gampe7c038102014-10-27 20:08:46 -07002157 if (FailOrAbort(this, insn_flags_[instance_of_idx].IsOpcode(),
2158 "Unable to get previous instruction of if-eqz/if-nez for work index ",
2159 work_insn_idx_)) {
2160 break;
2161 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002162 } else {
2163 break;
2164 }
2165
Ian Rogers9b360392013-06-06 14:45:07 -07002166 const Instruction* instance_of_inst = Instruction::At(code_item_->insns_ + instance_of_idx);
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002167
2168 /* Check for peep-hole pattern of:
2169 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002170 * instance-of vX, vY, T;
2171 * ifXXX vX, label ;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002172 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002173 * label:
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002174 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002175 * and sharpen the type of vY to be type T.
2176 * Note, this pattern can't be if:
2177 * - if there are other branches to this branch,
2178 * - when vX == vY.
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002179 */
Ian Rogersfae370a2013-06-05 08:33:27 -07002180 if (!CurrentInsnFlags()->IsBranchTarget() &&
Ian Rogers9b360392013-06-06 14:45:07 -07002181 (Instruction::INSTANCE_OF == instance_of_inst->Opcode()) &&
2182 (inst->VRegA_21t() == instance_of_inst->VRegA_22c()) &&
2183 (instance_of_inst->VRegA_22c() != instance_of_inst->VRegB_22c())) {
Ian Rogersebbdd872014-07-07 23:53:08 -07002184 // Check the type of the instance-of is different than that of registers type, as if they
2185 // are the same there is no work to be done here. Check that the conversion is not to or
2186 // from an unresolved type as type information is imprecise. If the instance-of is to an
2187 // interface then ignore the type information as interfaces can only be treated as Objects
2188 // and we don't want to disallow field and other operations on the object. If the value
2189 // being instance-of checked against is known null (zero) then allow the optimization as
2190 // we didn't have type information. If the merge of the instance-of type with the original
2191 // type is assignable to the original then allow optimization. This check is performed to
2192 // ensure that subsequent merges don't lose type information - such as becoming an
2193 // interface from a class that would lose information relevant to field checks.
Ian Rogers7b078e82014-09-10 14:44:24 -07002194 const RegType& orig_type = work_line_->GetRegisterType(this, instance_of_inst->VRegB_22c());
Ian Rogersd8f69b02014-09-10 21:43:52 +00002195 const RegType& cast_type = ResolveClassAndCheckAccess(instance_of_inst->VRegC_22c());
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002196
Ian Rogersebbdd872014-07-07 23:53:08 -07002197 if (!orig_type.Equals(cast_type) &&
2198 !cast_type.IsUnresolvedTypes() && !orig_type.IsUnresolvedTypes() &&
Andreas Gampe00633eb2014-07-17 16:13:35 -07002199 cast_type.HasClass() && // Could be conflict type, make sure it has a class.
Ian Rogersebbdd872014-07-07 23:53:08 -07002200 !cast_type.GetClass()->IsInterface() &&
2201 (orig_type.IsZero() ||
2202 orig_type.IsStrictlyAssignableFrom(cast_type.Merge(orig_type, &reg_types_)))) {
Ian Rogersd0fbd852013-09-24 18:17:04 -07002203 RegisterLine* update_line = RegisterLine::Create(code_item_->registers_size_, this);
Ian Rogersfae370a2013-06-05 08:33:27 -07002204 if (inst->Opcode() == Instruction::IF_EQZ) {
Ian Rogers9b360392013-06-06 14:45:07 -07002205 fallthrough_line.reset(update_line);
Ian Rogersfae370a2013-06-05 08:33:27 -07002206 } else {
Ian Rogers9b360392013-06-06 14:45:07 -07002207 branch_line.reset(update_line);
2208 }
2209 update_line->CopyFromLine(work_line_.get());
Ian Rogers7b078e82014-09-10 14:44:24 -07002210 update_line->SetRegisterType(this, instance_of_inst->VRegB_22c(), cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002211 if (!insn_flags_[instance_of_idx].IsBranchTarget() && 0 != instance_of_idx) {
2212 // See if instance-of was preceded by a move-object operation, common due to the small
2213 // register encoding space of instance-of, and propagate type information to the source
2214 // of the move-object.
2215 uint32_t move_idx = instance_of_idx - 1;
Brian Carlstromdf629502013-07-17 22:39:56 -07002216 while (0 != move_idx && !insn_flags_[move_idx].IsOpcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07002217 move_idx--;
2218 }
Andreas Gampe7c038102014-10-27 20:08:46 -07002219 if (FailOrAbort(this, insn_flags_[move_idx].IsOpcode(),
2220 "Unable to get previous instruction of if-eqz/if-nez for work index ",
2221 work_insn_idx_)) {
2222 break;
2223 }
Ian Rogers9b360392013-06-06 14:45:07 -07002224 const Instruction* move_inst = Instruction::At(code_item_->insns_ + move_idx);
2225 switch (move_inst->Opcode()) {
2226 case Instruction::MOVE_OBJECT:
2227 if (move_inst->VRegA_12x() == instance_of_inst->VRegB_22c()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002228 update_line->SetRegisterType(this, move_inst->VRegB_12x(), cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002229 }
2230 break;
2231 case Instruction::MOVE_OBJECT_FROM16:
2232 if (move_inst->VRegA_22x() == instance_of_inst->VRegB_22c()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002233 update_line->SetRegisterType(this, move_inst->VRegB_22x(), cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002234 }
2235 break;
2236 case Instruction::MOVE_OBJECT_16:
2237 if (move_inst->VRegA_32x() == instance_of_inst->VRegB_22c()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002238 update_line->SetRegisterType(this, move_inst->VRegB_32x(), cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002239 }
2240 break;
2241 default:
2242 break;
2243 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002244 }
2245 }
2246 }
2247
jeffhaobdb76512011-09-07 11:43:16 -07002248 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002249 }
jeffhaobdb76512011-09-07 11:43:16 -07002250 case Instruction::IF_LTZ:
2251 case Instruction::IF_GEZ:
2252 case Instruction::IF_GTZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002253 case Instruction::IF_LEZ: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002254 const RegType& reg_type = work_line_->GetRegisterType(this, inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002255 if (!reg_type.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002256 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
2257 << " unexpected as arg to if-ltz/if-gez/if-gtz/if-lez";
Ian Rogersd81871c2011-10-03 13:57:23 -07002258 }
jeffhaobdb76512011-09-07 11:43:16 -07002259 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002260 }
jeffhaobdb76512011-09-07 11:43:16 -07002261 case Instruction::AGET_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002262 VerifyAGet(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002263 break;
jeffhaobdb76512011-09-07 11:43:16 -07002264 case Instruction::AGET_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002265 VerifyAGet(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002266 break;
jeffhaobdb76512011-09-07 11:43:16 -07002267 case Instruction::AGET_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002268 VerifyAGet(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002269 break;
jeffhaobdb76512011-09-07 11:43:16 -07002270 case Instruction::AGET_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002271 VerifyAGet(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002272 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002273 case Instruction::AGET:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002274 VerifyAGet(inst, reg_types_.Integer(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002275 break;
jeffhaobdb76512011-09-07 11:43:16 -07002276 case Instruction::AGET_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002277 VerifyAGet(inst, reg_types_.LongLo(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002278 break;
2279 case Instruction::AGET_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002280 VerifyAGet(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07002281 break;
2282
Ian Rogersd81871c2011-10-03 13:57:23 -07002283 case Instruction::APUT_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002284 VerifyAPut(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002285 break;
2286 case Instruction::APUT_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002287 VerifyAPut(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002288 break;
2289 case Instruction::APUT_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002290 VerifyAPut(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002291 break;
2292 case Instruction::APUT_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002293 VerifyAPut(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002294 break;
2295 case Instruction::APUT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002296 VerifyAPut(inst, reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002297 break;
2298 case Instruction::APUT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002299 VerifyAPut(inst, reg_types_.LongLo(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002300 break;
2301 case Instruction::APUT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002302 VerifyAPut(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07002303 break;
2304
jeffhaobdb76512011-09-07 11:43:16 -07002305 case Instruction::IGET_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002306 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002307 break;
jeffhaobdb76512011-09-07 11:43:16 -07002308 case Instruction::IGET_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002309 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002310 break;
jeffhaobdb76512011-09-07 11:43:16 -07002311 case Instruction::IGET_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002312 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002313 break;
jeffhaobdb76512011-09-07 11:43:16 -07002314 case Instruction::IGET_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002315 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002316 break;
2317 case Instruction::IGET:
Andreas Gampe896df402014-10-20 22:25:29 -07002318 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002319 break;
2320 case Instruction::IGET_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002321 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002322 break;
2323 case Instruction::IGET_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002324 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false,
2325 false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002326 break;
jeffhaobdb76512011-09-07 11:43:16 -07002327
Ian Rogersd81871c2011-10-03 13:57:23 -07002328 case Instruction::IPUT_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002329 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002330 break;
2331 case Instruction::IPUT_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002332 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002333 break;
2334 case Instruction::IPUT_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002335 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002336 break;
2337 case Instruction::IPUT_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002338 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002339 break;
2340 case Instruction::IPUT:
Andreas Gampe896df402014-10-20 22:25:29 -07002341 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002342 break;
2343 case Instruction::IPUT_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002344 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002345 break;
jeffhaobdb76512011-09-07 11:43:16 -07002346 case Instruction::IPUT_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002347 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false,
2348 false);
jeffhaobdb76512011-09-07 11:43:16 -07002349 break;
2350
jeffhaobdb76512011-09-07 11:43:16 -07002351 case Instruction::SGET_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002352 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002353 break;
jeffhaobdb76512011-09-07 11:43:16 -07002354 case Instruction::SGET_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002355 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002356 break;
jeffhaobdb76512011-09-07 11:43:16 -07002357 case Instruction::SGET_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002358 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002359 break;
jeffhaobdb76512011-09-07 11:43:16 -07002360 case Instruction::SGET_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002361 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002362 break;
2363 case Instruction::SGET:
Andreas Gampe896df402014-10-20 22:25:29 -07002364 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002365 break;
2366 case Instruction::SGET_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002367 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002368 break;
2369 case Instruction::SGET_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002370 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false,
2371 true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002372 break;
2373
2374 case Instruction::SPUT_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002375 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002376 break;
2377 case Instruction::SPUT_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002378 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002379 break;
2380 case Instruction::SPUT_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002381 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002382 break;
2383 case Instruction::SPUT_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002384 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002385 break;
2386 case Instruction::SPUT:
Andreas Gampe896df402014-10-20 22:25:29 -07002387 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002388 break;
2389 case Instruction::SPUT_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002390 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002391 break;
2392 case Instruction::SPUT_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002393 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false,
2394 true);
jeffhaobdb76512011-09-07 11:43:16 -07002395 break;
2396
2397 case Instruction::INVOKE_VIRTUAL:
2398 case Instruction::INVOKE_VIRTUAL_RANGE:
2399 case Instruction::INVOKE_SUPER:
Ian Rogersd81871c2011-10-03 13:57:23 -07002400 case Instruction::INVOKE_SUPER_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002401 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE ||
2402 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002403 bool is_super = (inst->Opcode() == Instruction::INVOKE_SUPER ||
2404 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002405 ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_VIRTUAL, is_range, is_super);
Ian Rogersd8f69b02014-09-10 21:43:52 +00002406 const RegType* return_type = nullptr;
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002407 if (called_method != nullptr) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002408 StackHandleScope<1> hs(self_);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002409 mirror::Class* return_type_class = called_method->GetReturnType(can_load_classes_);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002410 if (return_type_class != nullptr) {
Andreas Gampef23f33d2015-06-23 14:18:17 -07002411 return_type = &FromClass(called_method->GetReturnTypeDescriptor(),
2412 return_type_class,
2413 return_type_class->CannotBeAssignedFromOtherTypes());
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002414 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07002415 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
2416 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002417 }
2418 }
2419 if (return_type == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002420 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002421 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2422 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002423 const char* descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Mathieu Chartierbf99f772014-08-23 16:37:27 -07002424 return_type = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
jeffhaobdb76512011-09-07 11:43:16 -07002425 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002426 if (!return_type->IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002427 work_line_->SetResultRegisterType(this, *return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002428 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002429 work_line_->SetResultRegisterTypeWide(*return_type, return_type->HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002430 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002431 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002432 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002433 }
jeffhaobdb76512011-09-07 11:43:16 -07002434 case Instruction::INVOKE_DIRECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002435 case Instruction::INVOKE_DIRECT_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002436 bool is_range = (inst->Opcode() == Instruction::INVOKE_DIRECT_RANGE);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002437 ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_DIRECT, is_range, false);
Ian Rogers46685432012-06-03 22:26:43 -07002438 const char* return_type_descriptor;
2439 bool is_constructor;
Ian Rogersd8f69b02014-09-10 21:43:52 +00002440 const RegType* return_type = nullptr;
Ian Rogers7b078e82014-09-10 14:44:24 -07002441 if (called_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002442 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogers46685432012-06-03 22:26:43 -07002443 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
Ian Rogersdfb325e2013-10-30 01:00:44 -07002444 is_constructor = strcmp("<init>", dex_file_->StringDataByIdx(method_id.name_idx_)) == 0;
Ian Rogers46685432012-06-03 22:26:43 -07002445 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
2446 return_type_descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2447 } else {
2448 is_constructor = called_method->IsConstructor();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002449 return_type_descriptor = called_method->GetReturnTypeDescriptor();
Ian Rogers7b078e82014-09-10 14:44:24 -07002450 StackHandleScope<1> hs(self_);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002451 mirror::Class* return_type_class = called_method->GetReturnType(can_load_classes_);
Ian Rogers1ff3c982014-08-12 02:30:58 -07002452 if (return_type_class != nullptr) {
Andreas Gampef23f33d2015-06-23 14:18:17 -07002453 return_type = &FromClass(return_type_descriptor,
2454 return_type_class,
2455 return_type_class->CannotBeAssignedFromOtherTypes());
Ian Rogers1ff3c982014-08-12 02:30:58 -07002456 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07002457 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
2458 self_->ClearException();
Ian Rogers1ff3c982014-08-12 02:30:58 -07002459 }
Ian Rogers46685432012-06-03 22:26:43 -07002460 }
2461 if (is_constructor) {
jeffhaobdb76512011-09-07 11:43:16 -07002462 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002463 * Some additional checks when calling a constructor. We know from the invocation arg check
2464 * that the "this" argument is an instance of called_method->klass. Now we further restrict
2465 * that to require that called_method->klass is the same as this->klass or this->super,
2466 * allowing the latter only if the "this" argument is the same as the "this" argument to
2467 * this method (which implies that we're in a constructor ourselves).
jeffhaobdb76512011-09-07 11:43:16 -07002468 */
Ian Rogers7b078e82014-09-10 14:44:24 -07002469 const RegType& this_type = work_line_->GetInvocationThis(this, inst, is_range);
jeffhaob57e9522012-04-26 18:08:21 -07002470 if (this_type.IsConflict()) // failure.
2471 break;
jeffhaobdb76512011-09-07 11:43:16 -07002472
jeffhaob57e9522012-04-26 18:08:21 -07002473 /* no null refs allowed (?) */
2474 if (this_type.IsZero()) {
2475 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unable to initialize null ref";
2476 break;
jeffhao2a8a90e2011-09-26 14:25:31 -07002477 }
jeffhaob57e9522012-04-26 18:08:21 -07002478
2479 /* must be in same class or in superclass */
Ian Rogersd8f69b02014-09-10 21:43:52 +00002480 // const RegType& this_super_klass = this_type.GetSuperClass(&reg_types_);
Ian Rogers46685432012-06-03 22:26:43 -07002481 // TODO: re-enable constructor type verification
2482 // if (this_super_klass.IsConflict()) {
jeffhaob57e9522012-04-26 18:08:21 -07002483 // Unknown super class, fail so we re-check at runtime.
Ian Rogers46685432012-06-03 22:26:43 -07002484 // Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "super class unknown for '" << this_type << "'";
2485 // break;
2486 // }
jeffhaob57e9522012-04-26 18:08:21 -07002487
2488 /* arg must be an uninitialized reference */
2489 if (!this_type.IsUninitializedTypes()) {
2490 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Expected initialization on uninitialized reference "
2491 << this_type;
2492 break;
2493 }
2494
2495 /*
2496 * Replace the uninitialized reference with an initialized one. We need to do this for all
2497 * registers that have the same object instance in them, not just the "this" register.
2498 */
Jeff Hao848f70a2014-01-15 13:49:50 -08002499 const uint32_t this_reg = (is_range) ? inst->VRegC_3rc() : inst->VRegC_35c();
2500 work_line_->MarkRefsAsInitialized(this, this_type, this_reg, work_insn_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002501 }
Ian Rogers1ff3c982014-08-12 02:30:58 -07002502 if (return_type == nullptr) {
Mathieu Chartierbf99f772014-08-23 16:37:27 -07002503 return_type = &reg_types_.FromDescriptor(GetClassLoader(), return_type_descriptor,
2504 false);
Ian Rogers1ff3c982014-08-12 02:30:58 -07002505 }
2506 if (!return_type->IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002507 work_line_->SetResultRegisterType(this, *return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002508 } else {
Ian Rogers1ff3c982014-08-12 02:30:58 -07002509 work_line_->SetResultRegisterTypeWide(*return_type, return_type->HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002510 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002511 just_set_result = true;
2512 break;
2513 }
2514 case Instruction::INVOKE_STATIC:
2515 case Instruction::INVOKE_STATIC_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002516 bool is_range = (inst->Opcode() == Instruction::INVOKE_STATIC_RANGE);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002517 ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_STATIC, is_range, false);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002518 const char* descriptor;
Ian Rogers7b078e82014-09-10 14:44:24 -07002519 if (called_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002520 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002521 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2522 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Mathieu Chartier590fee92013-09-13 13:46:47 -07002523 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002524 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002525 descriptor = called_method->GetReturnTypeDescriptor();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002526 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00002527 const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002528 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002529 work_line_->SetResultRegisterType(this, return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002530 } else {
2531 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2532 }
jeffhaobdb76512011-09-07 11:43:16 -07002533 just_set_result = true;
2534 }
2535 break;
jeffhaobdb76512011-09-07 11:43:16 -07002536 case Instruction::INVOKE_INTERFACE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002537 case Instruction::INVOKE_INTERFACE_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002538 bool is_range = (inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002539 ArtMethod* abs_method = VerifyInvocationArgs(inst, METHOD_INTERFACE, is_range, false);
Ian Rogers7b078e82014-09-10 14:44:24 -07002540 if (abs_method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002541 mirror::Class* called_interface = abs_method->GetDeclaringClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002542 if (!called_interface->IsInterface() && !called_interface->IsObjectClass()) {
2543 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected interface class in invoke-interface '"
2544 << PrettyMethod(abs_method) << "'";
2545 break;
Ian Rogers28ad40d2011-10-27 15:19:26 -07002546 }
Ian Rogers0d604842012-04-16 14:50:24 -07002547 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002548 /* Get the type of the "this" arg, which should either be a sub-interface of called
2549 * interface or Object (see comments in RegType::JoinClass).
2550 */
Ian Rogers7b078e82014-09-10 14:44:24 -07002551 const RegType& this_type = work_line_->GetInvocationThis(this, inst, is_range);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002552 if (this_type.IsZero()) {
2553 /* null pointer always passes (and always fails at runtime) */
2554 } else {
2555 if (this_type.IsUninitializedTypes()) {
2556 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface call on uninitialized object "
2557 << this_type;
2558 break;
2559 }
2560 // In the past we have tried to assert that "called_interface" is assignable
2561 // from "this_type.GetClass()", however, as we do an imprecise Join
2562 // (RegType::JoinClass) we don't have full information on what interfaces are
2563 // implemented by "this_type". For example, two classes may implement the same
2564 // interfaces and have a common parent that doesn't implement the interface. The
2565 // join will set "this_type" to the parent class and a test that this implements
2566 // the interface will incorrectly fail.
2567 }
2568 /*
2569 * We don't have an object instance, so we can't find the concrete method. However, all of
2570 * the type information is in the abstract method, so we're good.
2571 */
2572 const char* descriptor;
Ian Rogers7b078e82014-09-10 14:44:24 -07002573 if (abs_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002574 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002575 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2576 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
2577 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2578 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002579 descriptor = abs_method->GetReturnTypeDescriptor();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002580 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00002581 const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002582 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002583 work_line_->SetResultRegisterType(this, return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002584 } else {
2585 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2586 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002587 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002588 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002589 }
jeffhaobdb76512011-09-07 11:43:16 -07002590 case Instruction::NEG_INT:
2591 case Instruction::NOT_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002592 work_line_->CheckUnaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002593 break;
2594 case Instruction::NEG_LONG:
2595 case Instruction::NOT_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002596 work_line_->CheckUnaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002597 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002598 break;
2599 case Instruction::NEG_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002600 work_line_->CheckUnaryOp(this, inst, reg_types_.Float(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002601 break;
2602 case Instruction::NEG_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002603 work_line_->CheckUnaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002604 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002605 break;
2606 case Instruction::INT_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002607 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002608 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002609 break;
2610 case Instruction::INT_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002611 work_line_->CheckUnaryOp(this, inst, reg_types_.Float(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002612 break;
2613 case Instruction::INT_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002614 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002615 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002616 break;
2617 case Instruction::LONG_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002618 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002619 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002620 break;
2621 case Instruction::LONG_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002622 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002623 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002624 break;
2625 case Instruction::LONG_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002626 work_line_->CheckUnaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002627 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002628 break;
2629 case Instruction::FLOAT_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002630 work_line_->CheckUnaryOp(this, inst, reg_types_.Integer(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002631 break;
2632 case Instruction::FLOAT_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002633 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002634 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002635 break;
2636 case Instruction::FLOAT_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002637 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002638 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002639 break;
2640 case Instruction::DOUBLE_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002641 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002642 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002643 break;
2644 case Instruction::DOUBLE_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002645 work_line_->CheckUnaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002646 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002647 break;
2648 case Instruction::DOUBLE_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002649 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002650 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002651 break;
2652 case Instruction::INT_TO_BYTE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002653 work_line_->CheckUnaryOp(this, inst, reg_types_.Byte(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002654 break;
2655 case Instruction::INT_TO_CHAR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002656 work_line_->CheckUnaryOp(this, inst, reg_types_.Char(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002657 break;
2658 case Instruction::INT_TO_SHORT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002659 work_line_->CheckUnaryOp(this, inst, reg_types_.Short(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002660 break;
2661
2662 case Instruction::ADD_INT:
2663 case Instruction::SUB_INT:
2664 case Instruction::MUL_INT:
2665 case Instruction::REM_INT:
2666 case Instruction::DIV_INT:
2667 case Instruction::SHL_INT:
2668 case Instruction::SHR_INT:
2669 case Instruction::USHR_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002670 work_line_->CheckBinaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002671 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002672 break;
2673 case Instruction::AND_INT:
2674 case Instruction::OR_INT:
2675 case Instruction::XOR_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002676 work_line_->CheckBinaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002677 reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002678 break;
2679 case Instruction::ADD_LONG:
2680 case Instruction::SUB_LONG:
2681 case Instruction::MUL_LONG:
2682 case Instruction::DIV_LONG:
2683 case Instruction::REM_LONG:
2684 case Instruction::AND_LONG:
2685 case Instruction::OR_LONG:
2686 case Instruction::XOR_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002687 work_line_->CheckBinaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002688 reg_types_.LongLo(), reg_types_.LongHi(),
2689 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002690 break;
2691 case Instruction::SHL_LONG:
2692 case Instruction::SHR_LONG:
2693 case Instruction::USHR_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002694 /* shift distance is Int, making these different from other binary operations */
Ian Rogers7b078e82014-09-10 14:44:24 -07002695 work_line_->CheckBinaryOpWideShift(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002696 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002697 break;
2698 case Instruction::ADD_FLOAT:
2699 case Instruction::SUB_FLOAT:
2700 case Instruction::MUL_FLOAT:
2701 case Instruction::DIV_FLOAT:
2702 case Instruction::REM_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002703 work_line_->CheckBinaryOp(this, inst, reg_types_.Float(), reg_types_.Float(),
2704 reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002705 break;
2706 case Instruction::ADD_DOUBLE:
2707 case Instruction::SUB_DOUBLE:
2708 case Instruction::MUL_DOUBLE:
2709 case Instruction::DIV_DOUBLE:
2710 case Instruction::REM_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002711 work_line_->CheckBinaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002712 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2713 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002714 break;
2715 case Instruction::ADD_INT_2ADDR:
2716 case Instruction::SUB_INT_2ADDR:
2717 case Instruction::MUL_INT_2ADDR:
2718 case Instruction::REM_INT_2ADDR:
2719 case Instruction::SHL_INT_2ADDR:
2720 case Instruction::SHR_INT_2ADDR:
2721 case Instruction::USHR_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002722 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
2723 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002724 break;
2725 case Instruction::AND_INT_2ADDR:
2726 case Instruction::OR_INT_2ADDR:
2727 case Instruction::XOR_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002728 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
2729 reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002730 break;
2731 case Instruction::DIV_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002732 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
2733 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002734 break;
2735 case Instruction::ADD_LONG_2ADDR:
2736 case Instruction::SUB_LONG_2ADDR:
2737 case Instruction::MUL_LONG_2ADDR:
2738 case Instruction::DIV_LONG_2ADDR:
2739 case Instruction::REM_LONG_2ADDR:
2740 case Instruction::AND_LONG_2ADDR:
2741 case Instruction::OR_LONG_2ADDR:
2742 case Instruction::XOR_LONG_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002743 work_line_->CheckBinaryOp2addrWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002744 reg_types_.LongLo(), reg_types_.LongHi(),
2745 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002746 break;
2747 case Instruction::SHL_LONG_2ADDR:
2748 case Instruction::SHR_LONG_2ADDR:
2749 case Instruction::USHR_LONG_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002750 work_line_->CheckBinaryOp2addrWideShift(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002751 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002752 break;
2753 case Instruction::ADD_FLOAT_2ADDR:
2754 case Instruction::SUB_FLOAT_2ADDR:
2755 case Instruction::MUL_FLOAT_2ADDR:
2756 case Instruction::DIV_FLOAT_2ADDR:
2757 case Instruction::REM_FLOAT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002758 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Float(), reg_types_.Float(),
2759 reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002760 break;
2761 case Instruction::ADD_DOUBLE_2ADDR:
2762 case Instruction::SUB_DOUBLE_2ADDR:
2763 case Instruction::MUL_DOUBLE_2ADDR:
2764 case Instruction::DIV_DOUBLE_2ADDR:
2765 case Instruction::REM_DOUBLE_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002766 work_line_->CheckBinaryOp2addrWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002767 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2768 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002769 break;
2770 case Instruction::ADD_INT_LIT16:
Ian Rogersf72a11d2014-10-30 15:41:08 -07002771 case Instruction::RSUB_INT_LIT16:
jeffhaobdb76512011-09-07 11:43:16 -07002772 case Instruction::MUL_INT_LIT16:
2773 case Instruction::DIV_INT_LIT16:
2774 case Instruction::REM_INT_LIT16:
Ian Rogers7b078e82014-09-10 14:44:24 -07002775 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), false,
2776 true);
jeffhaobdb76512011-09-07 11:43:16 -07002777 break;
2778 case Instruction::AND_INT_LIT16:
2779 case Instruction::OR_INT_LIT16:
2780 case Instruction::XOR_INT_LIT16:
Ian Rogers7b078e82014-09-10 14:44:24 -07002781 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), true,
2782 true);
jeffhaobdb76512011-09-07 11:43:16 -07002783 break;
2784 case Instruction::ADD_INT_LIT8:
2785 case Instruction::RSUB_INT_LIT8:
2786 case Instruction::MUL_INT_LIT8:
2787 case Instruction::DIV_INT_LIT8:
2788 case Instruction::REM_INT_LIT8:
2789 case Instruction::SHL_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002790 case Instruction::SHR_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002791 case Instruction::USHR_INT_LIT8:
Ian Rogers7b078e82014-09-10 14:44:24 -07002792 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), false,
2793 false);
jeffhaobdb76512011-09-07 11:43:16 -07002794 break;
2795 case Instruction::AND_INT_LIT8:
2796 case Instruction::OR_INT_LIT8:
2797 case Instruction::XOR_INT_LIT8:
Ian Rogers7b078e82014-09-10 14:44:24 -07002798 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), true,
2799 false);
jeffhaobdb76512011-09-07 11:43:16 -07002800 break;
2801
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002802 // Special instructions.
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07002803 case Instruction::RETURN_VOID_NO_BARRIER:
2804 if (IsConstructor() && !IsStatic()) {
2805 auto& declaring_class = GetDeclaringClass();
Andreas Gampe68df3202015-06-22 11:35:46 -07002806 if (declaring_class.IsUnresolvedReference()) {
2807 // We must iterate over the fields, even if we cannot use mirror classes to do so. Do it
2808 // manually over the underlying dex file.
2809 uint32_t first_index = GetFirstFinalInstanceFieldIndex(*dex_file_,
2810 dex_file_->GetMethodId(dex_method_idx_).class_idx_);
2811 if (first_index != DexFile::kDexNoIndex) {
2812 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void-no-barrier not expected for field "
2813 << first_index;
2814 }
2815 break;
2816 }
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07002817 auto* klass = declaring_class.GetClass();
2818 for (uint32_t i = 0, num_fields = klass->NumInstanceFields(); i < num_fields; ++i) {
2819 if (klass->GetInstanceField(i)->IsFinal()) {
Mathieu Chartiere86deef2015-03-19 13:43:37 -07002820 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void-no-barrier not expected for "
2821 << PrettyField(klass->GetInstanceField(i));
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07002822 break;
2823 }
2824 }
Sebastien Hertzcc10e0e2013-06-28 14:24:48 +02002825 }
2826 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002827 // Note: the following instructions encode offsets derived from class linking.
2828 // As such they use Class*/Field*/AbstractMethod* as these offsets only have
2829 // meaning if the class linking and resolution were successful.
2830 case Instruction::IGET_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002831 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002832 break;
2833 case Instruction::IGET_WIDE_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002834 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002835 break;
2836 case Instruction::IGET_OBJECT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002837 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002838 break;
Mathieu Chartierffc605c2014-12-10 10:35:44 -08002839 case Instruction::IGET_BOOLEAN_QUICK:
2840 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true);
2841 break;
2842 case Instruction::IGET_BYTE_QUICK:
2843 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true);
2844 break;
2845 case Instruction::IGET_CHAR_QUICK:
2846 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true);
2847 break;
2848 case Instruction::IGET_SHORT_QUICK:
2849 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true);
2850 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002851 case Instruction::IPUT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002852 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002853 break;
Fred Shih37f05ef2014-07-16 18:38:08 -07002854 case Instruction::IPUT_BOOLEAN_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002855 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07002856 break;
2857 case Instruction::IPUT_BYTE_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002858 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07002859 break;
2860 case Instruction::IPUT_CHAR_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002861 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07002862 break;
2863 case Instruction::IPUT_SHORT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002864 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07002865 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002866 case Instruction::IPUT_WIDE_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002867 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002868 break;
2869 case Instruction::IPUT_OBJECT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002870 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002871 break;
2872 case Instruction::INVOKE_VIRTUAL_QUICK:
2873 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: {
2874 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002875 ArtMethod* called_method = VerifyInvokeVirtualQuickArgs(inst, is_range);
Ian Rogers7b078e82014-09-10 14:44:24 -07002876 if (called_method != nullptr) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002877 const char* descriptor = called_method->GetReturnTypeDescriptor();
Ian Rogersd8f69b02014-09-10 21:43:52 +00002878 const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002879 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002880 work_line_->SetResultRegisterType(this, return_type);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002881 } else {
2882 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2883 }
2884 just_set_result = true;
2885 }
2886 break;
2887 }
Igor Murashkin158f35c2015-06-10 15:55:30 -07002888 case Instruction::INVOKE_LAMBDA: {
2889 // Don't bother verifying, instead the interpreter will take the slow path with access checks.
2890 // If the code would've normally hard-failed, then the interpreter will throw the
2891 // appropriate verification errors at runtime.
2892 Fail(VERIFY_ERROR_FORCE_INTERPRETER); // TODO(iam): implement invoke-lambda verification
2893 break;
2894 }
2895 case Instruction::CREATE_LAMBDA: {
2896 // Don't bother verifying, instead the interpreter will take the slow path with access checks.
2897 // If the code would've normally hard-failed, then the interpreter will throw the
2898 // appropriate verification errors at runtime.
2899 Fail(VERIFY_ERROR_FORCE_INTERPRETER); // TODO(iam): implement create-lambda verification
2900 break;
2901 }
2902
Igor Murashkin2ee54e22015-06-18 10:05:11 -07002903 case Instruction::UNUSED_F4:
2904 case Instruction::UNUSED_F5:
2905 case Instruction::UNUSED_F7: {
Igor Murashkin158f35c2015-06-10 15:55:30 -07002906 DCHECK(false); // TODO(iam): Implement opcodes for lambdas
Igor Murashkin2ee54e22015-06-18 10:05:11 -07002907 // Conservatively fail verification on release builds.
2908 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Unexpected opcode " << inst->DumpString(dex_file_);
2909 break;
2910 }
2911
2912 case Instruction::BOX_LAMBDA: {
2913 // Don't bother verifying, instead the interpreter will take the slow path with access checks.
2914 // If the code would've normally hard-failed, then the interpreter will throw the
2915 // appropriate verification errors at runtime.
2916 Fail(VERIFY_ERROR_FORCE_INTERPRETER); // TODO(iam): implement box-lambda verification
2917 break;
2918 }
2919
2920 case Instruction::UNBOX_LAMBDA: {
2921 // Don't bother verifying, instead the interpreter will take the slow path with access checks.
2922 // If the code would've normally hard-failed, then the interpreter will throw the
2923 // appropriate verification errors at runtime.
2924 Fail(VERIFY_ERROR_FORCE_INTERPRETER); // TODO(iam): implement unbox-lambda verification
2925 break;
Igor Murashkin158f35c2015-06-10 15:55:30 -07002926 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002927
Ian Rogersd81871c2011-10-03 13:57:23 -07002928 /* These should never appear during verification. */
Mathieu Chartierffc605c2014-12-10 10:35:44 -08002929 case Instruction::UNUSED_3E ... Instruction::UNUSED_43:
Igor Murashkin158f35c2015-06-10 15:55:30 -07002930 case Instruction::UNUSED_FA ... Instruction::UNUSED_FF:
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002931 case Instruction::UNUSED_79:
2932 case Instruction::UNUSED_7A:
jeffhaod5347e02012-03-22 17:25:05 -07002933 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Unexpected opcode " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07002934 break;
2935
2936 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002937 * DO NOT add a "default" clause here. Without it the compiler will
jeffhaobdb76512011-09-07 11:43:16 -07002938 * complain if an instruction is missing (which is desirable).
2939 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002940 } // end - switch (dec_insn.opcode)
jeffhaobdb76512011-09-07 11:43:16 -07002941
Stephen Kyle7e541c92014-12-17 17:10:02 +00002942 /*
2943 * If we are in a constructor, and we had an UninitializedThis type
2944 * in a register somewhere, we need to make sure it wasn't overwritten.
2945 */
2946 if (track_uninitialized_this) {
2947 bool was_invoke_direct = (inst->Opcode() == Instruction::INVOKE_DIRECT ||
2948 inst->Opcode() == Instruction::INVOKE_DIRECT_RANGE);
2949 if (work_line_->WasUninitializedThisOverwritten(this, uninitialized_this_loc,
2950 was_invoke_direct)) {
2951 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
2952 << "Constructor failed to initialize this object";
2953 }
2954 }
2955
Ian Rogersad0b3a32012-04-16 14:50:24 -07002956 if (have_pending_hard_failure_) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08002957 if (Runtime::Current()->IsAotCompiler()) {
2958 /* When AOT compiling, check that the last failure is a hard failure */
Andreas Gampeb588f4c2015-05-26 13:35:39 -07002959 if (failures_[failures_.size() - 1] != VERIFY_ERROR_BAD_CLASS_HARD) {
2960 LOG(ERROR) << "Pending failures:";
2961 for (auto& error : failures_) {
2962 LOG(ERROR) << error;
2963 }
2964 for (auto& error_msg : failure_messages_) {
2965 LOG(ERROR) << error_msg->str();
2966 }
2967 LOG(FATAL) << "Pending hard failure, but last failure not hard.";
2968 }
Ian Rogerse1758fe2012-04-19 11:31:15 -07002969 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002970 /* immediate failure, reject class */
2971 info_messages_ << "Rejecting opcode " << inst->DumpString(dex_file_);
2972 return false;
jeffhaofaf459e2012-08-31 15:32:47 -07002973 } else if (have_pending_runtime_throw_failure_) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07002974 /* checking interpreter will throw, mark following code as unreachable */
jeffhaofaf459e2012-08-31 15:32:47 -07002975 opcode_flags = Instruction::kThrow;
Andreas Gamped12e7822015-06-25 10:26:40 -07002976 have_any_pending_runtime_throw_failure_ = true;
2977 // Reset the pending_runtime_throw flag. The flag is a global to decouple Fail and is per
2978 // instruction.
2979 have_pending_runtime_throw_failure_ = false;
jeffhaobdb76512011-09-07 11:43:16 -07002980 }
jeffhaobdb76512011-09-07 11:43:16 -07002981 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002982 * If we didn't just set the result register, clear it out. This ensures that you can only use
2983 * "move-result" immediately after the result is set. (We could check this statically, but it's
2984 * not expensive and it makes our debugging output cleaner.)
jeffhaobdb76512011-09-07 11:43:16 -07002985 */
2986 if (!just_set_result) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002987 work_line_->SetResultTypeToUnknown(this);
jeffhaobdb76512011-09-07 11:43:16 -07002988 }
2989
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002990
jeffhaobdb76512011-09-07 11:43:16 -07002991
2992 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002993 * Handle "branch". Tag the branch target.
jeffhaobdb76512011-09-07 11:43:16 -07002994 *
2995 * NOTE: instructions like Instruction::EQZ provide information about the
jeffhaod1f0fde2011-09-08 17:25:33 -07002996 * state of the register when the branch is taken or not taken. For example,
jeffhaobdb76512011-09-07 11:43:16 -07002997 * somebody could get a reference field, check it for zero, and if the
2998 * branch is taken immediately store that register in a boolean field
jeffhaod1f0fde2011-09-08 17:25:33 -07002999 * since the value is known to be zero. We do not currently account for
jeffhaobdb76512011-09-07 11:43:16 -07003000 * that, and will reject the code.
3001 *
3002 * TODO: avoid re-fetching the branch target
3003 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003004 if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07003005 bool isConditional, selfOkay;
Ian Rogersd81871c2011-10-03 13:57:23 -07003006 if (!GetBranchOffset(work_insn_idx_, &branch_target, &isConditional, &selfOkay)) {
jeffhaobdb76512011-09-07 11:43:16 -07003007 /* should never happen after static verification */
jeffhaod5347e02012-03-22 17:25:05 -07003008 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad branch";
jeffhaobdb76512011-09-07 11:43:16 -07003009 return false;
3010 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08003011 DCHECK_EQ(isConditional, (opcode_flags & Instruction::kContinue) != 0);
Stephen Kyle9bc61992014-09-22 13:53:15 +01003012 if (!CheckNotMoveExceptionOrMoveResult(code_item_->insns_, work_insn_idx_ + branch_target)) {
jeffhaobdb76512011-09-07 11:43:16 -07003013 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07003014 }
jeffhaobdb76512011-09-07 11:43:16 -07003015 /* update branch target, set "changed" if appropriate */
Ian Rogers7b078e82014-09-10 14:44:24 -07003016 if (nullptr != branch_line.get()) {
Ian Rogersebbdd872014-07-07 23:53:08 -07003017 if (!UpdateRegisters(work_insn_idx_ + branch_target, branch_line.get(), false)) {
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003018 return false;
3019 }
3020 } else {
Ian Rogersebbdd872014-07-07 23:53:08 -07003021 if (!UpdateRegisters(work_insn_idx_ + branch_target, work_line_.get(), false)) {
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003022 return false;
3023 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003024 }
jeffhaobdb76512011-09-07 11:43:16 -07003025 }
3026
3027 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003028 * Handle "switch". Tag all possible branch targets.
jeffhaobdb76512011-09-07 11:43:16 -07003029 *
3030 * We've already verified that the table is structurally sound, so we
3031 * just need to walk through and tag the targets.
3032 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003033 if ((opcode_flags & Instruction::kSwitch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07003034 int offset_to_switch = insns[1] | (((int32_t) insns[2]) << 16);
3035 const uint16_t* switch_insns = insns + offset_to_switch;
3036 int switch_count = switch_insns[1];
3037 int offset_to_targets, targ;
3038
3039 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
3040 /* 0 = sig, 1 = count, 2/3 = first key */
3041 offset_to_targets = 4;
3042 } else {
3043 /* 0 = sig, 1 = count, 2..count * 2 = keys */
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07003044 DCHECK((*insns & 0xff) == Instruction::SPARSE_SWITCH);
jeffhaobdb76512011-09-07 11:43:16 -07003045 offset_to_targets = 2 + 2 * switch_count;
3046 }
3047
3048 /* verify each switch target */
3049 for (targ = 0; targ < switch_count; targ++) {
3050 int offset;
3051 uint32_t abs_offset;
3052
3053 /* offsets are 32-bit, and only partly endian-swapped */
3054 offset = switch_insns[offset_to_targets + targ * 2] |
3055 (((int32_t) switch_insns[offset_to_targets + targ * 2 + 1]) << 16);
Ian Rogersd81871c2011-10-03 13:57:23 -07003056 abs_offset = work_insn_idx_ + offset;
3057 DCHECK_LT(abs_offset, code_item_->insns_size_in_code_units_);
Stephen Kyle9bc61992014-09-22 13:53:15 +01003058 if (!CheckNotMoveExceptionOrMoveResult(code_item_->insns_, abs_offset)) {
jeffhaobdb76512011-09-07 11:43:16 -07003059 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07003060 }
Ian Rogersebbdd872014-07-07 23:53:08 -07003061 if (!UpdateRegisters(abs_offset, work_line_.get(), false)) {
jeffhaobdb76512011-09-07 11:43:16 -07003062 return false;
Ian Rogersebbdd872014-07-07 23:53:08 -07003063 }
jeffhaobdb76512011-09-07 11:43:16 -07003064 }
3065 }
3066
3067 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003068 * Handle instructions that can throw and that are sitting in a "try" block. (If they're not in a
3069 * "try" block when they throw, control transfers out of the method.)
jeffhaobdb76512011-09-07 11:43:16 -07003070 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003071 if ((opcode_flags & Instruction::kThrow) != 0 && insn_flags_[work_insn_idx_].IsInTry()) {
Andreas Gampef91baf12014-07-18 15:41:00 -07003072 bool has_catch_all_handler = false;
Ian Rogers0571d352011-11-03 19:51:38 -07003073 CatchHandlerIterator iterator(*code_item_, work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07003074
Andreas Gampef91baf12014-07-18 15:41:00 -07003075 // Need the linker to try and resolve the handled class to check if it's Throwable.
3076 ClassLinker* linker = Runtime::Current()->GetClassLinker();
3077
Ian Rogers0571d352011-11-03 19:51:38 -07003078 for (; iterator.HasNext(); iterator.Next()) {
Andreas Gampef91baf12014-07-18 15:41:00 -07003079 uint16_t handler_type_idx = iterator.GetHandlerTypeIndex();
3080 if (handler_type_idx == DexFile::kDexNoIndex16) {
3081 has_catch_all_handler = true;
3082 } else {
3083 // It is also a catch-all if it is java.lang.Throwable.
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003084 mirror::Class* klass = linker->ResolveType(*dex_file_, handler_type_idx, dex_cache_,
3085 class_loader_);
Andreas Gampef91baf12014-07-18 15:41:00 -07003086 if (klass != nullptr) {
3087 if (klass == mirror::Throwable::GetJavaLangThrowable()) {
3088 has_catch_all_handler = true;
3089 }
3090 } else {
3091 // Clear exception.
Ian Rogers7b078e82014-09-10 14:44:24 -07003092 DCHECK(self_->IsExceptionPending());
3093 self_->ClearException();
Andreas Gampef91baf12014-07-18 15:41:00 -07003094 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003095 }
jeffhaobdb76512011-09-07 11:43:16 -07003096 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003097 * Merge registers into the "catch" block. We want to use the "savedRegs" rather than
3098 * "work_regs", because at runtime the exception will be thrown before the instruction
3099 * modifies any registers.
jeffhaobdb76512011-09-07 11:43:16 -07003100 */
Ian Rogersebbdd872014-07-07 23:53:08 -07003101 if (!UpdateRegisters(iterator.GetHandlerAddress(), saved_line_.get(), false)) {
jeffhaobdb76512011-09-07 11:43:16 -07003102 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07003103 }
jeffhaobdb76512011-09-07 11:43:16 -07003104 }
3105
3106 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003107 * If the monitor stack depth is nonzero, there must be a "catch all" handler for this
3108 * instruction. This does apply to monitor-exit because of async exception handling.
jeffhaobdb76512011-09-07 11:43:16 -07003109 */
Andreas Gampef91baf12014-07-18 15:41:00 -07003110 if (work_line_->MonitorStackDepth() > 0 && !has_catch_all_handler) {
jeffhaobdb76512011-09-07 11:43:16 -07003111 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003112 * The state in work_line reflects the post-execution state. If the current instruction is a
3113 * monitor-enter and the monitor stack was empty, we don't need a catch-all (if it throws,
jeffhaobdb76512011-09-07 11:43:16 -07003114 * it will do so before grabbing the lock).
3115 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02003116 if (inst->Opcode() != Instruction::MONITOR_ENTER || work_line_->MonitorStackDepth() != 1) {
jeffhaod5347e02012-03-22 17:25:05 -07003117 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
Ian Rogersd81871c2011-10-03 13:57:23 -07003118 << "expected to be within a catch-all for an instruction where a monitor is held";
jeffhaobdb76512011-09-07 11:43:16 -07003119 return false;
3120 }
3121 }
3122 }
3123
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003124 /* Handle "continue". Tag the next consecutive instruction.
3125 * Note: Keep the code handling "continue" case below the "branch" and "switch" cases,
3126 * because it changes work_line_ when performing peephole optimization
3127 * and this change should not be used in those cases.
3128 */
Ian Rogers6d376ae2013-07-23 15:12:40 -07003129 if ((opcode_flags & Instruction::kContinue) != 0) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003130 DCHECK_EQ(Instruction::At(code_item_->insns_ + work_insn_idx_), inst);
3131 uint32_t next_insn_idx = work_insn_idx_ + inst->SizeInCodeUnits();
Ian Rogers6d376ae2013-07-23 15:12:40 -07003132 if (next_insn_idx >= code_item_->insns_size_in_code_units_) {
3133 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Execution can walk off end of code area";
3134 return false;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003135 }
Ian Rogers6d376ae2013-07-23 15:12:40 -07003136 // The only way to get to a move-exception instruction is to get thrown there. Make sure the
3137 // next instruction isn't one.
3138 if (!CheckNotMoveException(code_item_->insns_, next_insn_idx)) {
3139 return false;
3140 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003141 if (nullptr != fallthrough_line.get()) {
Ian Rogers6d376ae2013-07-23 15:12:40 -07003142 // Make workline consistent with fallthrough computed from peephole optimization.
3143 work_line_->CopyFromLine(fallthrough_line.get());
3144 }
Ian Rogersb8c78592013-07-25 23:52:52 +00003145 if (insn_flags_[next_insn_idx].IsReturn()) {
3146 // For returns we only care about the operand to the return, all other registers are dead.
3147 const Instruction* ret_inst = Instruction::At(code_item_->insns_ + next_insn_idx);
3148 Instruction::Code opcode = ret_inst->Opcode();
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07003149 if (opcode == Instruction::RETURN_VOID || opcode == Instruction::RETURN_VOID_NO_BARRIER) {
Stephen Kyle7e541c92014-12-17 17:10:02 +00003150 SafelyMarkAllRegistersAsConflicts(this, work_line_.get());
Ian Rogersb8c78592013-07-25 23:52:52 +00003151 } else {
3152 if (opcode == Instruction::RETURN_WIDE) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003153 work_line_->MarkAllRegistersAsConflictsExceptWide(this, ret_inst->VRegA_11x());
Ian Rogersb8c78592013-07-25 23:52:52 +00003154 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07003155 work_line_->MarkAllRegistersAsConflictsExcept(this, ret_inst->VRegA_11x());
Ian Rogersb8c78592013-07-25 23:52:52 +00003156 }
3157 }
3158 }
Ian Rogers6d376ae2013-07-23 15:12:40 -07003159 RegisterLine* next_line = reg_table_.GetLine(next_insn_idx);
Ian Rogers7b078e82014-09-10 14:44:24 -07003160 if (next_line != nullptr) {
Ian Rogersebbdd872014-07-07 23:53:08 -07003161 // Merge registers into what we have for the next instruction, and set the "changed" flag if
3162 // needed. If the merge changes the state of the registers then the work line will be
3163 // updated.
3164 if (!UpdateRegisters(next_insn_idx, work_line_.get(), true)) {
Ian Rogers6d376ae2013-07-23 15:12:40 -07003165 return false;
3166 }
3167 } else {
3168 /*
3169 * We're not recording register data for the next instruction, so we don't know what the
3170 * prior state was. We have to assume that something has changed and re-evaluate it.
3171 */
3172 insn_flags_[next_insn_idx].SetChanged();
3173 }
3174 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003175
jeffhaod1f0fde2011-09-08 17:25:33 -07003176 /* If we're returning from the method, make sure monitor stack is empty. */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003177 if ((opcode_flags & Instruction::kReturn) != 0) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003178 if (!work_line_->VerifyMonitorStackEmpty(this)) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003179 return false;
3180 }
jeffhaobdb76512011-09-07 11:43:16 -07003181 }
3182
3183 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003184 * Update start_guess. Advance to the next instruction of that's
3185 * possible, otherwise use the branch target if one was found. If
jeffhaobdb76512011-09-07 11:43:16 -07003186 * neither of those exists we're in a return or throw; leave start_guess
3187 * alone and let the caller sort it out.
3188 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003189 if ((opcode_flags & Instruction::kContinue) != 0) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003190 DCHECK_EQ(Instruction::At(code_item_->insns_ + work_insn_idx_), inst);
3191 *start_guess = work_insn_idx_ + inst->SizeInCodeUnits();
Elliott Hughesadb8c672012-03-06 16:49:32 -08003192 } else if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07003193 /* we're still okay if branch_target is zero */
Ian Rogersd81871c2011-10-03 13:57:23 -07003194 *start_guess = work_insn_idx_ + branch_target;
jeffhaobdb76512011-09-07 11:43:16 -07003195 }
3196
Ian Rogersd81871c2011-10-03 13:57:23 -07003197 DCHECK_LT(*start_guess, code_item_->insns_size_in_code_units_);
3198 DCHECK(insn_flags_[*start_guess].IsOpcode());
jeffhaobdb76512011-09-07 11:43:16 -07003199
3200 return true;
Brian Carlstrom7934ac22013-07-26 10:54:15 -07003201} // NOLINT(readability/fn_size)
jeffhaobdb76512011-09-07 11:43:16 -07003202
Ian Rogersd8f69b02014-09-10 21:43:52 +00003203const RegType& MethodVerifier::ResolveClassAndCheckAccess(uint32_t class_idx) {
Ian Rogers0571d352011-11-03 19:51:38 -07003204 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
Ian Rogersd8f69b02014-09-10 21:43:52 +00003205 const RegType& referrer = GetDeclaringClass();
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003206 mirror::Class* klass = dex_cache_->GetResolvedType(class_idx);
Ian Rogers7b078e82014-09-10 14:44:24 -07003207 const RegType& result = klass != nullptr ?
Andreas Gampef23f33d2015-06-23 14:18:17 -07003208 FromClass(descriptor, klass, klass->CannotBeAssignedFromOtherTypes()) :
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003209 reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003210 if (result.IsConflict()) {
3211 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "accessing broken descriptor '" << descriptor
3212 << "' in " << referrer;
3213 return result;
3214 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003215 if (klass == nullptr && !result.IsUnresolvedTypes()) {
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003216 dex_cache_->SetResolvedType(class_idx, result.GetClass());
Ian Rogerse1758fe2012-04-19 11:31:15 -07003217 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003218 // Check if access is allowed. Unresolved types use xxxWithAccessCheck to
Jeff Haob24b4a72013-07-31 13:47:31 -07003219 // check at runtime if access is allowed and so pass here. If result is
3220 // primitive, skip the access check.
3221 if (result.IsNonZeroReferenceTypes() && !result.IsUnresolvedTypes() &&
3222 !referrer.IsUnresolvedTypes() && !referrer.CanAccess(result)) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07003223 Fail(VERIFY_ERROR_ACCESS_CLASS) << "illegal class access: '"
Ian Rogersad0b3a32012-04-16 14:50:24 -07003224 << referrer << "' -> '" << result << "'";
Ian Rogers28ad40d2011-10-27 15:19:26 -07003225 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003226 return result;
Ian Rogersd81871c2011-10-03 13:57:23 -07003227}
3228
Ian Rogersd8f69b02014-09-10 21:43:52 +00003229const RegType& MethodVerifier::GetCaughtExceptionType() {
Ian Rogers7b078e82014-09-10 14:44:24 -07003230 const RegType* common_super = nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003231 if (code_item_->tries_size_ != 0) {
Ian Rogers13735952014-10-08 12:43:28 -07003232 const uint8_t* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
Ian Rogersd81871c2011-10-03 13:57:23 -07003233 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
3234 for (uint32_t i = 0; i < handlers_size; i++) {
Ian Rogers0571d352011-11-03 19:51:38 -07003235 CatchHandlerIterator iterator(handlers_ptr);
3236 for (; iterator.HasNext(); iterator.Next()) {
3237 if (iterator.GetHandlerAddress() == (uint32_t) work_insn_idx_) {
3238 if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) {
Ian Rogersb4903572012-10-11 11:52:56 -07003239 common_super = &reg_types_.JavaLangThrowable(false);
Ian Rogersd81871c2011-10-03 13:57:23 -07003240 } else {
Ian Rogersd8f69b02014-09-10 21:43:52 +00003241 const RegType& exception = ResolveClassAndCheckAccess(iterator.GetHandlerTypeIndex());
Jeff Haob878f212014-04-24 16:25:36 -07003242 if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(exception)) {
Jeff Haoc26a56c2013-11-04 12:00:47 -08003243 if (exception.IsUnresolvedTypes()) {
3244 // We don't know enough about the type. Fail here and let runtime handle it.
3245 Fail(VERIFY_ERROR_NO_CLASS) << "unresolved exception class " << exception;
3246 return exception;
3247 } else {
3248 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unexpected non-exception class " << exception;
3249 return reg_types_.Conflict();
3250 }
Jeff Haob878f212014-04-24 16:25:36 -07003251 } else if (common_super == nullptr) {
3252 common_super = &exception;
Ian Rogers28ad40d2011-10-27 15:19:26 -07003253 } else if (common_super->Equals(exception)) {
Ian Rogersc4762272012-02-01 15:55:55 -08003254 // odd case, but nothing to do
Ian Rogersd81871c2011-10-03 13:57:23 -07003255 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07003256 common_super = &common_super->Merge(exception, &reg_types_);
Andreas Gampe7c038102014-10-27 20:08:46 -07003257 if (FailOrAbort(this,
3258 reg_types_.JavaLangThrowable(false).IsAssignableFrom(*common_super),
3259 "java.lang.Throwable is not assignable-from common_super at ",
3260 work_insn_idx_)) {
3261 break;
3262 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003263 }
3264 }
3265 }
3266 }
Ian Rogers0571d352011-11-03 19:51:38 -07003267 handlers_ptr = iterator.EndDataPointer();
Ian Rogersd81871c2011-10-03 13:57:23 -07003268 }
3269 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003270 if (common_super == nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003271 /* no catch blocks, or no catches with classes we can find */
jeffhaod5347e02012-03-22 17:25:05 -07003272 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unable to find exception handler";
Ian Rogersad0b3a32012-04-16 14:50:24 -07003273 return reg_types_.Conflict();
Ian Rogersd81871c2011-10-03 13:57:23 -07003274 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07003275 return *common_super;
Ian Rogersd81871c2011-10-03 13:57:23 -07003276}
3277
Mathieu Chartiere401d142015-04-22 13:56:20 -07003278ArtMethod* MethodVerifier::ResolveMethodAndCheckAccess(
3279 uint32_t dex_method_idx, MethodType method_type) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003280 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx);
Ian Rogersd8f69b02014-09-10 21:43:52 +00003281 const RegType& klass_type = ResolveClassAndCheckAccess(method_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003282 if (klass_type.IsConflict()) {
3283 std::string append(" in attempt to access method ");
3284 append += dex_file_->GetMethodName(method_id);
3285 AppendToLastFailMessage(append);
Ian Rogers7b078e82014-09-10 14:44:24 -07003286 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08003287 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003288 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003289 return nullptr; // Can't resolve Class so no more to do here
Ian Rogers90040192011-12-16 08:54:29 -08003290 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003291 mirror::Class* klass = klass_type.GetClass();
Ian Rogersd8f69b02014-09-10 21:43:52 +00003292 const RegType& referrer = GetDeclaringClass();
Mathieu Chartiere401d142015-04-22 13:56:20 -07003293 auto* cl = Runtime::Current()->GetClassLinker();
3294 auto pointer_size = cl->GetImagePointerSize();
3295 ArtMethod* res_method = dex_cache_->GetResolvedMethod(dex_method_idx, pointer_size);
Ian Rogers7b078e82014-09-10 14:44:24 -07003296 if (res_method == nullptr) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07003297 const char* name = dex_file_->GetMethodName(method_id);
Ian Rogersd91d6d62013-09-25 20:26:14 -07003298 const Signature signature = dex_file_->GetMethodSignature(method_id);
jeffhao8cd6dda2012-02-22 10:15:34 -08003299
3300 if (method_type == METHOD_DIRECT || method_type == METHOD_STATIC) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003301 res_method = klass->FindDirectMethod(name, signature, pointer_size);
jeffhao8cd6dda2012-02-22 10:15:34 -08003302 } else if (method_type == METHOD_INTERFACE) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003303 res_method = klass->FindInterfaceMethod(name, signature, pointer_size);
Ian Rogersd81871c2011-10-03 13:57:23 -07003304 } else {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003305 res_method = klass->FindVirtualMethod(name, signature, pointer_size);
Ian Rogersd81871c2011-10-03 13:57:23 -07003306 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003307 if (res_method != nullptr) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003308 dex_cache_->SetResolvedMethod(dex_method_idx, res_method, pointer_size);
Ian Rogersd81871c2011-10-03 13:57:23 -07003309 } else {
jeffhao8cd6dda2012-02-22 10:15:34 -08003310 // If a virtual or interface method wasn't found with the expected type, look in
3311 // the direct methods. This can happen when the wrong invoke type is used or when
3312 // a class has changed, and will be flagged as an error in later checks.
3313 if (method_type == METHOD_INTERFACE || method_type == METHOD_VIRTUAL) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003314 res_method = klass->FindDirectMethod(name, signature, pointer_size);
jeffhao8cd6dda2012-02-22 10:15:34 -08003315 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003316 if (res_method == nullptr) {
jeffhao8cd6dda2012-02-22 10:15:34 -08003317 Fail(VERIFY_ERROR_NO_METHOD) << "couldn't find method "
3318 << PrettyDescriptor(klass) << "." << name
3319 << " " << signature;
Ian Rogers7b078e82014-09-10 14:44:24 -07003320 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003321 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003322 }
3323 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003324 // Make sure calls to constructors are "direct". There are additional restrictions but we don't
3325 // enforce them here.
3326 if (res_method->IsConstructor() && method_type != METHOD_DIRECT) {
jeffhaod5347e02012-03-22 17:25:05 -07003327 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting non-direct call to constructor "
3328 << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003329 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003330 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003331 // Disallow any calls to class initializers.
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003332 if (res_method->IsClassInitializer()) {
jeffhaod5347e02012-03-22 17:25:05 -07003333 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting call to class initializer "
3334 << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003335 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003336 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003337 // Check if access is allowed.
Ian Rogersad0b3a32012-04-16 14:50:24 -07003338 if (!referrer.CanAccessMember(res_method->GetDeclaringClass(), res_method->GetAccessFlags())) {
jeffhao8cd6dda2012-02-22 10:15:34 -08003339 Fail(VERIFY_ERROR_ACCESS_METHOD) << "illegal method access (call " << PrettyMethod(res_method)
Ian Rogersad0b3a32012-04-16 14:50:24 -07003340 << " from " << referrer << ")";
jeffhaob57e9522012-04-26 18:08:21 -07003341 return res_method;
jeffhao8cd6dda2012-02-22 10:15:34 -08003342 }
jeffhaode0d9c92012-02-27 13:58:13 -08003343 // Check that invoke-virtual and invoke-super are not used on private methods of the same class.
3344 if (res_method->IsPrivate() && method_type == METHOD_VIRTUAL) {
jeffhaod5347e02012-03-22 17:25:05 -07003345 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invoke-super/virtual can't be used on private method "
3346 << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003347 return nullptr;
jeffhaode0d9c92012-02-27 13:58:13 -08003348 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003349 // Check that interface methods match interface classes.
3350 if (klass->IsInterface() && method_type != METHOD_INTERFACE) {
3351 Fail(VERIFY_ERROR_CLASS_CHANGE) << "non-interface method " << PrettyMethod(res_method)
3352 << " is in an interface class " << PrettyClass(klass);
Ian Rogers7b078e82014-09-10 14:44:24 -07003353 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003354 } else if (!klass->IsInterface() && method_type == METHOD_INTERFACE) {
3355 Fail(VERIFY_ERROR_CLASS_CHANGE) << "interface method " << PrettyMethod(res_method)
3356 << " is in a non-interface class " << PrettyClass(klass);
Ian Rogers7b078e82014-09-10 14:44:24 -07003357 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003358 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003359 // See if the method type implied by the invoke instruction matches the access flags for the
3360 // target method.
Brian Carlstrombe6fa5e2014-12-09 20:15:42 -08003361 if ((method_type == METHOD_DIRECT && (!res_method->IsDirect() || res_method->IsStatic())) ||
Ian Rogersd81871c2011-10-03 13:57:23 -07003362 (method_type == METHOD_STATIC && !res_method->IsStatic()) ||
3363 ((method_type == METHOD_VIRTUAL || method_type == METHOD_INTERFACE) && res_method->IsDirect())
3364 ) {
Ian Rogers2fc14272012-08-30 10:56:57 -07003365 Fail(VERIFY_ERROR_CLASS_CHANGE) << "invoke type (" << method_type << ") does not match method "
3366 " type of " << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003367 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003368 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003369 return res_method;
3370}
3371
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003372template <class T>
Mathieu Chartiere401d142015-04-22 13:56:20 -07003373ArtMethod* MethodVerifier::VerifyInvocationArgsFromIterator(
3374 T* it, const Instruction* inst, MethodType method_type, bool is_range, ArtMethod* res_method) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003375 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
3376 // match the call to the signature. Also, we might be calling through an abstract method
3377 // definition (which doesn't have register count values).
3378 const size_t expected_args = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
3379 /* caught by static verifier */
3380 DCHECK(is_range || expected_args <= 5);
3381 if (expected_args > code_item_->outs_size_) {
3382 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
3383 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
3384 return nullptr;
3385 }
3386
3387 uint32_t arg[5];
3388 if (!is_range) {
3389 inst->GetVarArgs(arg);
3390 }
3391 uint32_t sig_registers = 0;
3392
3393 /*
3394 * Check the "this" argument, which must be an instance of the class that declared the method.
3395 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
3396 * rigorous check here (which is okay since we have to do it at runtime).
3397 */
3398 if (method_type != METHOD_STATIC) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003399 const RegType& actual_arg_type = work_line_->GetInvocationThis(this, inst, is_range);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003400 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
3401 CHECK(have_pending_hard_failure_);
3402 return nullptr;
3403 }
3404 if (actual_arg_type.IsUninitializedReference()) {
3405 if (res_method) {
3406 if (!res_method->IsConstructor()) {
3407 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
3408 return nullptr;
3409 }
3410 } else {
3411 // Check whether the name of the called method is "<init>"
3412 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Jeff Hao0d087272014-08-04 14:47:17 -07003413 if (strcmp(dex_file_->GetMethodName(dex_file_->GetMethodId(method_idx)), "<init>") != 0) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003414 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
3415 return nullptr;
3416 }
3417 }
3418 }
3419 if (method_type != METHOD_INTERFACE && !actual_arg_type.IsZero()) {
Ian Rogersd8f69b02014-09-10 21:43:52 +00003420 const RegType* res_method_class;
Andreas Gamped9e23012015-06-04 22:19:58 -07003421 // Miranda methods have the declaring interface as their declaring class, not the abstract
3422 // class. It would be wrong to use this for the type check (interface type checks are
3423 // postponed to runtime).
3424 if (res_method != nullptr && !res_method->IsMiranda()) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003425 mirror::Class* klass = res_method->GetDeclaringClass();
Ian Rogers1ff3c982014-08-12 02:30:58 -07003426 std::string temp;
Andreas Gampef23f33d2015-06-23 14:18:17 -07003427 res_method_class = &FromClass(klass->GetDescriptor(&temp), klass,
3428 klass->CannotBeAssignedFromOtherTypes());
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003429 } else {
3430 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
3431 const uint16_t class_idx = dex_file_->GetMethodId(method_idx).class_idx_;
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003432 res_method_class = &reg_types_.FromDescriptor(GetClassLoader(),
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003433 dex_file_->StringByTypeIdx(class_idx),
3434 false);
3435 }
3436 if (!res_method_class->IsAssignableFrom(actual_arg_type)) {
3437 Fail(actual_arg_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS:
3438 VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type
3439 << "' not instance of '" << *res_method_class << "'";
3440 // Continue on soft failures. We need to find possible hard failures to avoid problems in
3441 // the compiler.
3442 if (have_pending_hard_failure_) {
3443 return nullptr;
3444 }
3445 }
3446 }
3447 sig_registers = 1;
3448 }
3449
3450 for ( ; it->HasNext(); it->Next()) {
3451 if (sig_registers >= expected_args) {
3452 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, expected " << inst->VRegA() <<
3453 " arguments, found " << sig_registers << " or more.";
3454 return nullptr;
3455 }
3456
3457 const char* param_descriptor = it->GetDescriptor();
3458
3459 if (param_descriptor == nullptr) {
3460 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation because of missing signature "
3461 "component";
3462 return nullptr;
3463 }
3464
Ian Rogersd8f69b02014-09-10 21:43:52 +00003465 const RegType& reg_type = reg_types_.FromDescriptor(GetClassLoader(), param_descriptor, false);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003466 uint32_t get_reg = is_range ? inst->VRegC_3rc() + static_cast<uint32_t>(sig_registers) :
3467 arg[sig_registers];
3468 if (reg_type.IsIntegralTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003469 const RegType& src_type = work_line_->GetRegisterType(this, get_reg);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003470 if (!src_type.IsIntegralTypes()) {
3471 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register v" << get_reg << " has type " << src_type
3472 << " but expected " << reg_type;
Andreas Gampeb588f4c2015-05-26 13:35:39 -07003473 return nullptr;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003474 }
Andreas Gampeda9badb2015-06-05 20:22:12 -07003475 } else {
3476 if (!work_line_->VerifyRegisterType(this, get_reg, reg_type)) {
3477 // Continue on soft failures. We need to find possible hard failures to avoid problems in
3478 // the compiler.
3479 if (have_pending_hard_failure_) {
3480 return nullptr;
3481 }
3482 } else if (reg_type.IsLongOrDoubleTypes()) {
3483 // Check that registers are consecutive (for non-range invokes). Invokes are the only
3484 // instructions not specifying register pairs by the first component, but require them
3485 // nonetheless. Only check when there's an actual register in the parameters. If there's
3486 // none, this will fail below.
3487 if (!is_range && sig_registers + 1 < expected_args) {
3488 uint32_t second_reg = arg[sig_registers + 1];
3489 if (second_reg != get_reg + 1) {
3490 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, long or double parameter "
3491 "at index " << sig_registers << " is not a pair: " << get_reg << " + "
3492 << second_reg << ".";
3493 return nullptr;
3494 }
3495 }
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003496 }
3497 }
3498 sig_registers += reg_type.IsLongOrDoubleTypes() ? 2 : 1;
3499 }
3500 if (expected_args != sig_registers) {
3501 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, expected " << expected_args <<
3502 " arguments, found " << sig_registers;
3503 return nullptr;
3504 }
3505 return res_method;
3506}
3507
3508void MethodVerifier::VerifyInvocationArgsUnresolvedMethod(const Instruction* inst,
3509 MethodType method_type,
3510 bool is_range) {
3511 // As the method may not have been resolved, make this static check against what we expect.
3512 // The main reason for this code block is to fail hard when we find an illegal use, e.g.,
3513 // wrong number of arguments or wrong primitive types, even if the method could not be resolved.
3514 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
3515 DexFileParameterIterator it(*dex_file_,
3516 dex_file_->GetProtoId(dex_file_->GetMethodId(method_idx).proto_idx_));
3517 VerifyInvocationArgsFromIterator<DexFileParameterIterator>(&it, inst, method_type, is_range,
3518 nullptr);
3519}
3520
3521class MethodParamListDescriptorIterator {
3522 public:
Mathieu Chartiere401d142015-04-22 13:56:20 -07003523 explicit MethodParamListDescriptorIterator(ArtMethod* res_method) :
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003524 res_method_(res_method), pos_(0), params_(res_method->GetParameterTypeList()),
3525 params_size_(params_ == nullptr ? 0 : params_->Size()) {
3526 }
3527
3528 bool HasNext() {
3529 return pos_ < params_size_;
3530 }
3531
3532 void Next() {
3533 ++pos_;
3534 }
3535
3536 const char* GetDescriptor() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
3537 return res_method_->GetTypeDescriptorFromTypeIdx(params_->GetTypeItem(pos_).type_idx_);
3538 }
3539
3540 private:
Mathieu Chartiere401d142015-04-22 13:56:20 -07003541 ArtMethod* res_method_;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003542 size_t pos_;
3543 const DexFile::TypeList* params_;
3544 const size_t params_size_;
3545};
3546
Mathieu Chartiere401d142015-04-22 13:56:20 -07003547ArtMethod* MethodVerifier::VerifyInvocationArgs(
3548 const Instruction* inst, MethodType method_type, bool is_range, bool is_super) {
jeffhao8cd6dda2012-02-22 10:15:34 -08003549 // Resolve the method. This could be an abstract or concrete method depending on what sort of call
3550 // we're making.
Sebastien Hertz5243e912013-05-21 10:55:07 +02003551 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Andreas Gampeacc4d2f2014-06-12 19:35:05 -07003552
Mathieu Chartiere401d142015-04-22 13:56:20 -07003553 ArtMethod* res_method = ResolveMethodAndCheckAccess(method_idx, method_type);
Ian Rogers7b078e82014-09-10 14:44:24 -07003554 if (res_method == nullptr) { // error or class is unresolved
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003555 // Check what we can statically.
3556 if (!have_pending_hard_failure_) {
3557 VerifyInvocationArgsUnresolvedMethod(inst, method_type, is_range);
3558 }
3559 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003560 }
3561
Ian Rogersd81871c2011-10-03 13:57:23 -07003562 // If we're using invoke-super(method), make sure that the executing method's class' superclass
3563 // has a vtable entry for the target method.
3564 if (is_super) {
3565 DCHECK(method_type == METHOD_VIRTUAL);
Ian Rogersd8f69b02014-09-10 21:43:52 +00003566 const RegType& super = GetDeclaringClass().GetSuperClass(&reg_types_);
Ian Rogers529781d2012-07-23 17:24:29 -07003567 if (super.IsUnresolvedTypes()) {
jeffhao4d8df822012-04-24 17:09:36 -07003568 Fail(VERIFY_ERROR_NO_METHOD) << "unknown super class in invoke-super from "
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003569 << PrettyMethod(dex_method_idx_, *dex_file_)
jeffhao4d8df822012-04-24 17:09:36 -07003570 << " to super " << PrettyMethod(res_method);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003571 return nullptr;
jeffhao4d8df822012-04-24 17:09:36 -07003572 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003573 mirror::Class* super_klass = super.GetClass();
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003574 if (res_method->GetMethodIndex() >= super_klass->GetVTableLength()) {
jeffhao4d8df822012-04-24 17:09:36 -07003575 Fail(VERIFY_ERROR_NO_METHOD) << "invalid invoke-super from "
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003576 << PrettyMethod(dex_method_idx_, *dex_file_)
jeffhao4d8df822012-04-24 17:09:36 -07003577 << " to super " << super
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003578 << "." << res_method->GetName()
3579 << res_method->GetSignature();
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003580 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003581 }
3582 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003583
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003584 // Process the target method's signature. This signature may or may not
3585 MethodParamListDescriptorIterator it(res_method);
3586 return VerifyInvocationArgsFromIterator<MethodParamListDescriptorIterator>(&it, inst, method_type,
3587 is_range, res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07003588}
3589
Mathieu Chartiere401d142015-04-22 13:56:20 -07003590ArtMethod* MethodVerifier::GetQuickInvokedMethod(const Instruction* inst, RegisterLine* reg_line,
3591 bool is_range, bool allow_failure) {
Mathieu Chartier091d2382015-03-06 10:59:06 -08003592 if (is_range) {
3593 DCHECK_EQ(inst->Opcode(), Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
3594 } else {
3595 DCHECK_EQ(inst->Opcode(), Instruction::INVOKE_VIRTUAL_QUICK);
3596 }
3597 const RegType& actual_arg_type = reg_line->GetInvocationThis(this, inst, is_range, allow_failure);
Ian Rogers9bc54402014-04-17 16:40:01 -07003598 if (!actual_arg_type.HasClass()) {
3599 VLOG(verifier) << "Failed to get mirror::Class* from '" << actual_arg_type << "'";
Andreas Gampe63981562014-04-17 12:28:43 -07003600 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003601 }
Ian Rogersa4cf1df2014-05-07 19:47:17 -07003602 mirror::Class* klass = actual_arg_type.GetClass();
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003603 mirror::Class* dispatch_class;
Ian Rogersa4cf1df2014-05-07 19:47:17 -07003604 if (klass->IsInterface()) {
3605 // Derive Object.class from Class.class.getSuperclass().
3606 mirror::Class* object_klass = klass->GetClass()->GetSuperClass();
Andreas Gampe7c038102014-10-27 20:08:46 -07003607 if (FailOrAbort(this, object_klass->IsObjectClass(),
Mathieu Chartier091d2382015-03-06 10:59:06 -08003608 "Failed to find Object class in quickened invoke receiver", work_insn_idx_)) {
Andreas Gampe7c038102014-10-27 20:08:46 -07003609 return nullptr;
3610 }
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003611 dispatch_class = object_klass;
Ian Rogersa4cf1df2014-05-07 19:47:17 -07003612 } else {
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003613 dispatch_class = klass;
Ian Rogersa4cf1df2014-05-07 19:47:17 -07003614 }
Mathieu Chartier091d2382015-03-06 10:59:06 -08003615 if (!dispatch_class->HasVTable()) {
3616 FailOrAbort(this, allow_failure, "Receiver class has no vtable for quickened invoke at ",
3617 work_insn_idx_);
Andreas Gampe7c038102014-10-27 20:08:46 -07003618 return nullptr;
3619 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003620 uint16_t vtable_index = is_range ? inst->VRegB_3rc() : inst->VRegB_35c();
Mathieu Chartiere401d142015-04-22 13:56:20 -07003621 auto* cl = Runtime::Current()->GetClassLinker();
3622 auto pointer_size = cl->GetImagePointerSize();
Mathieu Chartier091d2382015-03-06 10:59:06 -08003623 if (static_cast<int32_t>(vtable_index) >= dispatch_class->GetVTableLength()) {
3624 FailOrAbort(this, allow_failure,
3625 "Receiver class has not enough vtable slots for quickened invoke at ",
3626 work_insn_idx_);
Andreas Gampe7c038102014-10-27 20:08:46 -07003627 return nullptr;
3628 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07003629 ArtMethod* res_method = dispatch_class->GetVTableEntry(vtable_index, pointer_size);
Mathieu Chartier091d2382015-03-06 10:59:06 -08003630 if (self_->IsExceptionPending()) {
3631 FailOrAbort(this, allow_failure, "Unexpected exception pending for quickened invoke at ",
3632 work_insn_idx_);
Andreas Gampe7c038102014-10-27 20:08:46 -07003633 return nullptr;
3634 }
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003635 return res_method;
3636}
3637
Mathieu Chartiere401d142015-04-22 13:56:20 -07003638ArtMethod* MethodVerifier::VerifyInvokeVirtualQuickArgs(const Instruction* inst, bool is_range) {
Andreas Gampe76bd8802014-12-10 16:43:58 -08003639 DCHECK(Runtime::Current()->IsStarted() || verify_to_dump_)
3640 << PrettyMethod(dex_method_idx_, *dex_file_, true) << "@" << work_insn_idx_;
3641
Mathieu Chartiere401d142015-04-22 13:56:20 -07003642 ArtMethod* res_method = GetQuickInvokedMethod(inst, work_line_.get(), is_range, false);
Ian Rogers7b078e82014-09-10 14:44:24 -07003643 if (res_method == nullptr) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003644 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer method from " << inst->Name();
Ian Rogers7b078e82014-09-10 14:44:24 -07003645 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003646 }
Andreas Gampe7c038102014-10-27 20:08:46 -07003647 if (FailOrAbort(this, !res_method->IsDirect(), "Quick-invoked method is direct at ",
3648 work_insn_idx_)) {
3649 return nullptr;
3650 }
3651 if (FailOrAbort(this, !res_method->IsStatic(), "Quick-invoked method is static at ",
3652 work_insn_idx_)) {
3653 return nullptr;
3654 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003655
3656 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
3657 // match the call to the signature. Also, we might be calling through an abstract method
3658 // definition (which doesn't have register count values).
Ian Rogers7b078e82014-09-10 14:44:24 -07003659 const RegType& actual_arg_type = work_line_->GetInvocationThis(this, inst, is_range);
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003660 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
Ian Rogers7b078e82014-09-10 14:44:24 -07003661 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003662 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003663 const size_t expected_args = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
3664 /* caught by static verifier */
3665 DCHECK(is_range || expected_args <= 5);
3666 if (expected_args > code_item_->outs_size_) {
3667 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
3668 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
Ian Rogers7b078e82014-09-10 14:44:24 -07003669 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003670 }
3671
3672 /*
3673 * Check the "this" argument, which must be an instance of the class that declared the method.
3674 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
3675 * rigorous check here (which is okay since we have to do it at runtime).
3676 */
3677 if (actual_arg_type.IsUninitializedReference() && !res_method->IsConstructor()) {
3678 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
Ian Rogers7b078e82014-09-10 14:44:24 -07003679 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003680 }
3681 if (!actual_arg_type.IsZero()) {
3682 mirror::Class* klass = res_method->GetDeclaringClass();
Ian Rogers1ff3c982014-08-12 02:30:58 -07003683 std::string temp;
Ian Rogersd8f69b02014-09-10 21:43:52 +00003684 const RegType& res_method_class =
Andreas Gampef23f33d2015-06-23 14:18:17 -07003685 FromClass(klass->GetDescriptor(&temp), klass, klass->CannotBeAssignedFromOtherTypes());
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003686 if (!res_method_class.IsAssignableFrom(actual_arg_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07003687 Fail(actual_arg_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS :
3688 VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003689 << "' not instance of '" << res_method_class << "'";
Ian Rogers7b078e82014-09-10 14:44:24 -07003690 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003691 }
3692 }
3693 /*
3694 * Process the target method's signature. This signature may or may not
3695 * have been verified, so we can't assume it's properly formed.
3696 */
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003697 const DexFile::TypeList* params = res_method->GetParameterTypeList();
Ian Rogers7b078e82014-09-10 14:44:24 -07003698 size_t params_size = params == nullptr ? 0 : params->Size();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003699 uint32_t arg[5];
3700 if (!is_range) {
Ian Rogers29a26482014-05-02 15:27:29 -07003701 inst->GetVarArgs(arg);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003702 }
3703 size_t actual_args = 1;
3704 for (size_t param_index = 0; param_index < params_size; param_index++) {
3705 if (actual_args >= expected_args) {
3706 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invalid call to '" << PrettyMethod(res_method)
Brian Carlstrom93c33962013-07-26 10:37:43 -07003707 << "'. Expected " << expected_args
3708 << " arguments, processing argument " << actual_args
3709 << " (where longs/doubles count twice).";
Ian Rogers7b078e82014-09-10 14:44:24 -07003710 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003711 }
3712 const char* descriptor =
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003713 res_method->GetTypeDescriptorFromTypeIdx(params->GetTypeItem(param_index).type_idx_);
Ian Rogers7b078e82014-09-10 14:44:24 -07003714 if (descriptor == nullptr) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003715 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003716 << " missing signature component";
Ian Rogers7b078e82014-09-10 14:44:24 -07003717 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003718 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00003719 const RegType& reg_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003720 uint32_t get_reg = is_range ? inst->VRegC_3rc() + actual_args : arg[actual_args];
Ian Rogers7b078e82014-09-10 14:44:24 -07003721 if (!work_line_->VerifyRegisterType(this, get_reg, reg_type)) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003722 return res_method;
3723 }
3724 actual_args = reg_type.IsLongOrDoubleTypes() ? actual_args + 2 : actual_args + 1;
3725 }
3726 if (actual_args != expected_args) {
3727 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
3728 << " expected " << expected_args << " arguments, found " << actual_args;
Ian Rogers7b078e82014-09-10 14:44:24 -07003729 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003730 } else {
3731 return res_method;
3732 }
3733}
3734
Ian Rogers62342ec2013-06-11 10:26:37 -07003735void MethodVerifier::VerifyNewArray(const Instruction* inst, bool is_filled, bool is_range) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003736 uint32_t type_idx;
3737 if (!is_filled) {
3738 DCHECK_EQ(inst->Opcode(), Instruction::NEW_ARRAY);
3739 type_idx = inst->VRegC_22c();
3740 } else if (!is_range) {
3741 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY);
3742 type_idx = inst->VRegB_35c();
3743 } else {
3744 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY_RANGE);
3745 type_idx = inst->VRegB_3rc();
3746 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00003747 const RegType& res_type = ResolveClassAndCheckAccess(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003748 if (res_type.IsConflict()) { // bad class
3749 DCHECK_NE(failures_.size(), 0U);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003750 } else {
3751 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
3752 if (!res_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003753 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "new-array on non-array class " << res_type;
Ian Rogers0c4a5062012-02-03 15:18:59 -08003754 } else if (!is_filled) {
3755 /* make sure "size" register is valid type */
Ian Rogers7b078e82014-09-10 14:44:24 -07003756 work_line_->VerifyRegisterType(this, inst->VRegB_22c(), reg_types_.Integer());
Ian Rogers0c4a5062012-02-03 15:18:59 -08003757 /* set register type to array class */
Ian Rogersd8f69b02014-09-10 21:43:52 +00003758 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
Ian Rogers7b078e82014-09-10 14:44:24 -07003759 work_line_->SetRegisterType(this, inst->VRegA_22c(), precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003760 } else {
3761 // Verify each register. If "arg_count" is bad, VerifyRegisterType() will run off the end of
3762 // the list and fail. It's legal, if silly, for arg_count to be zero.
Ian Rogersd8f69b02014-09-10 21:43:52 +00003763 const RegType& expected_type = reg_types_.GetComponentType(res_type, GetClassLoader());
Sebastien Hertz5243e912013-05-21 10:55:07 +02003764 uint32_t arg_count = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
3765 uint32_t arg[5];
3766 if (!is_range) {
Ian Rogers29a26482014-05-02 15:27:29 -07003767 inst->GetVarArgs(arg);
Sebastien Hertz5243e912013-05-21 10:55:07 +02003768 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08003769 for (size_t ui = 0; ui < arg_count; ui++) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003770 uint32_t get_reg = is_range ? inst->VRegC_3rc() + ui : arg[ui];
Ian Rogers7b078e82014-09-10 14:44:24 -07003771 if (!work_line_->VerifyRegisterType(this, get_reg, expected_type)) {
3772 work_line_->SetResultRegisterType(this, reg_types_.Conflict());
Ian Rogers0c4a5062012-02-03 15:18:59 -08003773 return;
3774 }
3775 }
3776 // filled-array result goes into "result" register
Ian Rogersd8f69b02014-09-10 21:43:52 +00003777 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
Ian Rogers7b078e82014-09-10 14:44:24 -07003778 work_line_->SetResultRegisterType(this, precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003779 }
3780 }
3781}
3782
Sebastien Hertz5243e912013-05-21 10:55:07 +02003783void MethodVerifier::VerifyAGet(const Instruction* inst,
Ian Rogersd8f69b02014-09-10 21:43:52 +00003784 const RegType& insn_type, bool is_primitive) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003785 const RegType& index_type = work_line_->GetRegisterType(this, inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07003786 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003787 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07003788 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07003789 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegB_23x());
Ian Rogers89310de2012-02-01 13:47:30 -08003790 if (array_type.IsZero()) {
Nicolas Geoffray4824c272015-06-24 15:53:03 +01003791 have_pending_runtime_throw_failure_ = true;
Ian Rogers89310de2012-02-01 13:47:30 -08003792 // Null array class; this code path will fail at runtime. Infer a merge-able type from the
3793 // instruction type. TODO: have a proper notion of bottom here.
3794 if (!is_primitive || insn_type.IsCategory1Types()) {
3795 // Reference or category 1
Ian Rogers7b078e82014-09-10 14:44:24 -07003796 work_line_->SetRegisterType(this, inst->VRegA_23x(), reg_types_.Zero());
Ian Rogersd81871c2011-10-03 13:57:23 -07003797 } else {
Ian Rogers89310de2012-02-01 13:47:30 -08003798 // Category 2
Ian Rogers7b078e82014-09-10 14:44:24 -07003799 work_line_->SetRegisterTypeWide(this, inst->VRegA_23x(),
3800 reg_types_.FromCat2ConstLo(0, false),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003801 reg_types_.FromCat2ConstHi(0, false));
Ian Rogers89310de2012-02-01 13:47:30 -08003802 }
jeffhaofc3144e2012-02-01 17:21:15 -08003803 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003804 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aget";
Ian Rogers89310de2012-02-01 13:47:30 -08003805 } else {
3806 /* verify the class */
Ian Rogersd8f69b02014-09-10 21:43:52 +00003807 const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
jeffhaofc3144e2012-02-01 17:21:15 -08003808 if (!component_type.IsReferenceTypes() && !is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07003809 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08003810 << " source for aget-object";
3811 } else if (component_type.IsNonZeroReferenceTypes() && is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07003812 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08003813 << " source for category 1 aget";
3814 } else if (is_primitive && !insn_type.Equals(component_type) &&
3815 !((insn_type.IsInteger() && component_type.IsFloat()) ||
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003816 (insn_type.IsLong() && component_type.IsDouble()))) {
3817 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array type " << array_type
3818 << " incompatible with aget of type " << insn_type;
Ian Rogers89310de2012-02-01 13:47:30 -08003819 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003820 // Use knowledge of the field type which is stronger than the type inferred from the
3821 // instruction, which can't differentiate object types and ints from floats, longs from
3822 // doubles.
3823 if (!component_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003824 work_line_->SetRegisterType(this, inst->VRegA_23x(), component_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003825 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07003826 work_line_->SetRegisterTypeWide(this, inst->VRegA_23x(), component_type,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003827 component_type.HighHalf(&reg_types_));
3828 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003829 }
3830 }
3831 }
3832}
3833
Ian Rogersd8f69b02014-09-10 21:43:52 +00003834void MethodVerifier::VerifyPrimitivePut(const RegType& target_type, const RegType& insn_type,
Jeff Haofe1f7c82013-08-01 14:50:24 -07003835 const uint32_t vregA) {
3836 // Primitive assignability rules are weaker than regular assignability rules.
3837 bool instruction_compatible;
3838 bool value_compatible;
Ian Rogers7b078e82014-09-10 14:44:24 -07003839 const RegType& value_type = work_line_->GetRegisterType(this, vregA);
Jeff Haofe1f7c82013-08-01 14:50:24 -07003840 if (target_type.IsIntegralTypes()) {
Jeff Haoa4647482013-08-06 15:35:47 -07003841 instruction_compatible = target_type.Equals(insn_type);
Jeff Haofe1f7c82013-08-01 14:50:24 -07003842 value_compatible = value_type.IsIntegralTypes();
3843 } else if (target_type.IsFloat()) {
3844 instruction_compatible = insn_type.IsInteger(); // no put-float, so expect put-int
3845 value_compatible = value_type.IsFloatTypes();
3846 } else if (target_type.IsLong()) {
3847 instruction_compatible = insn_type.IsLong();
Andreas Gampe376fa682014-09-07 13:06:12 -07003848 // Additional register check: this is not checked statically (as part of VerifyInstructions),
3849 // as target_type depends on the resolved type of the field.
3850 if (instruction_compatible && work_line_->NumRegs() > vregA + 1) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003851 const RegType& value_type_hi = work_line_->GetRegisterType(this, vregA + 1);
Andreas Gampe376fa682014-09-07 13:06:12 -07003852 value_compatible = value_type.IsLongTypes() && value_type.CheckWidePair(value_type_hi);
3853 } else {
3854 value_compatible = false;
3855 }
Jeff Haofe1f7c82013-08-01 14:50:24 -07003856 } else if (target_type.IsDouble()) {
3857 instruction_compatible = insn_type.IsLong(); // no put-double, so expect put-long
Andreas Gampe376fa682014-09-07 13:06:12 -07003858 // Additional register check: this is not checked statically (as part of VerifyInstructions),
3859 // as target_type depends on the resolved type of the field.
3860 if (instruction_compatible && work_line_->NumRegs() > vregA + 1) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003861 const RegType& value_type_hi = work_line_->GetRegisterType(this, vregA + 1);
Andreas Gampe376fa682014-09-07 13:06:12 -07003862 value_compatible = value_type.IsDoubleTypes() && value_type.CheckWidePair(value_type_hi);
3863 } else {
3864 value_compatible = false;
3865 }
Jeff Haofe1f7c82013-08-01 14:50:24 -07003866 } else {
3867 instruction_compatible = false; // reference with primitive store
3868 value_compatible = false; // unused
3869 }
3870 if (!instruction_compatible) {
3871 // This is a global failure rather than a class change failure as the instructions and
3872 // the descriptors for the type should have been consistent within the same file at
3873 // compile time.
3874 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "put insn has type '" << insn_type
3875 << "' but expected type '" << target_type << "'";
3876 return;
3877 }
3878 if (!value_compatible) {
3879 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
3880 << " of type " << value_type << " but expected " << target_type << " for put";
3881 return;
3882 }
3883}
3884
Sebastien Hertz5243e912013-05-21 10:55:07 +02003885void MethodVerifier::VerifyAPut(const Instruction* inst,
Ian Rogersd8f69b02014-09-10 21:43:52 +00003886 const RegType& insn_type, bool is_primitive) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003887 const RegType& index_type = work_line_->GetRegisterType(this, inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07003888 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003889 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07003890 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07003891 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegB_23x());
Ian Rogers89310de2012-02-01 13:47:30 -08003892 if (array_type.IsZero()) {
Nicolas Geoffray66389fb2015-06-19 10:35:42 +01003893 // Null array type; this code path will fail at runtime.
3894 // Still check that the given value matches the instruction's type.
3895 work_line_->VerifyRegisterType(this, inst->VRegA_23x(), insn_type);
jeffhaofc3144e2012-02-01 17:21:15 -08003896 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003897 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aput";
Ian Rogers89310de2012-02-01 13:47:30 -08003898 } else {
Ian Rogersd8f69b02014-09-10 21:43:52 +00003899 const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
Jeff Haofe1f7c82013-08-01 14:50:24 -07003900 const uint32_t vregA = inst->VRegA_23x();
Jeff Haob24b4a72013-07-31 13:47:31 -07003901 if (is_primitive) {
Jeff Haofe1f7c82013-08-01 14:50:24 -07003902 VerifyPrimitivePut(component_type, insn_type, vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07003903 } else {
Jeff Haob24b4a72013-07-31 13:47:31 -07003904 if (!component_type.IsReferenceTypes()) {
3905 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
3906 << " source for aput-object";
3907 } else {
3908 // The instruction agrees with the type of array, confirm the value to be stored does too
3909 // Note: we use the instruction type (rather than the component type) for aput-object as
3910 // incompatible classes will be caught at runtime as an array store exception
Ian Rogers7b078e82014-09-10 14:44:24 -07003911 work_line_->VerifyRegisterType(this, vregA, insn_type);
Jeff Haob24b4a72013-07-31 13:47:31 -07003912 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003913 }
3914 }
3915 }
3916}
3917
Mathieu Chartierc7853442015-03-27 14:35:38 -07003918ArtField* MethodVerifier::GetStaticField(int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08003919 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3920 // Check access to class
Ian Rogersd8f69b02014-09-10 21:43:52 +00003921 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Brian Carlstrom7934ac22013-07-26 10:54:15 -07003922 if (klass_type.IsConflict()) { // bad class
Ian Rogersad0b3a32012-04-16 14:50:24 -07003923 AppendToLastFailMessage(StringPrintf(" in attempt to access static field %d (%s) in %s",
3924 field_idx, dex_file_->GetFieldName(field_id),
3925 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers7b078e82014-09-10 14:44:24 -07003926 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08003927 }
Elliott Hughesb25c3f62012-03-26 16:35:06 -07003928 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003929 return nullptr; // Can't resolve Class so no more to do here, will do checking at runtime.
Ian Rogers90040192011-12-16 08:54:29 -08003930 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07003931 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartierc7853442015-03-27 14:35:38 -07003932 ArtField* field = class_linker->ResolveFieldJLS(*dex_file_, field_idx, dex_cache_,
3933 class_loader_);
Ian Rogers7b078e82014-09-10 14:44:24 -07003934 if (field == nullptr) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07003935 VLOG(verifier) << "Unable to resolve static field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07003936 << dex_file_->GetFieldName(field_id) << ") in "
3937 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogers7b078e82014-09-10 14:44:24 -07003938 DCHECK(self_->IsExceptionPending());
3939 self_->ClearException();
3940 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003941 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
3942 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003943 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access static field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07003944 << " from " << GetDeclaringClass();
Ian Rogers7b078e82014-09-10 14:44:24 -07003945 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003946 } else if (!field->IsStatic()) {
3947 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field) << " to be static";
Ian Rogers7b078e82014-09-10 14:44:24 -07003948 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003949 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07003950 return field;
Ian Rogersd81871c2011-10-03 13:57:23 -07003951}
3952
Mathieu Chartierc7853442015-03-27 14:35:38 -07003953ArtField* MethodVerifier::GetInstanceField(const RegType& obj_type, int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08003954 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3955 // Check access to class
Ian Rogersd8f69b02014-09-10 21:43:52 +00003956 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003957 if (klass_type.IsConflict()) {
3958 AppendToLastFailMessage(StringPrintf(" in attempt to access instance field %d (%s) in %s",
3959 field_idx, dex_file_->GetFieldName(field_id),
3960 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers7b078e82014-09-10 14:44:24 -07003961 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08003962 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003963 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003964 return nullptr; // Can't resolve Class so no more to do here
Ian Rogers90040192011-12-16 08:54:29 -08003965 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07003966 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartierc7853442015-03-27 14:35:38 -07003967 ArtField* field = class_linker->ResolveFieldJLS(*dex_file_, field_idx, dex_cache_,
3968 class_loader_);
Ian Rogers7b078e82014-09-10 14:44:24 -07003969 if (field == nullptr) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07003970 VLOG(verifier) << "Unable to resolve instance field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07003971 << dex_file_->GetFieldName(field_id) << ") in "
3972 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogers7b078e82014-09-10 14:44:24 -07003973 DCHECK(self_->IsExceptionPending());
3974 self_->ClearException();
3975 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003976 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
3977 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003978 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access instance field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07003979 << " from " << GetDeclaringClass();
Ian Rogers7b078e82014-09-10 14:44:24 -07003980 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003981 } else if (field->IsStatic()) {
3982 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field)
3983 << " to not be static";
Ian Rogers7b078e82014-09-10 14:44:24 -07003984 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003985 } else if (obj_type.IsZero()) {
3986 // Cannot infer and check type, however, access will cause null pointer exception
3987 return field;
Stephen Kyle695c5982014-08-22 15:03:07 +01003988 } else if (!obj_type.IsReferenceTypes()) {
3989 // Trying to read a field from something that isn't a reference
3990 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance field access on object that has "
3991 << "non-reference type " << obj_type;
Ian Rogers7b078e82014-09-10 14:44:24 -07003992 return nullptr;
Ian Rogerse1758fe2012-04-19 11:31:15 -07003993 } else {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003994 mirror::Class* klass = field->GetDeclaringClass();
Ian Rogersd8f69b02014-09-10 21:43:52 +00003995 const RegType& field_klass =
Andreas Gampef23f33d2015-06-23 14:18:17 -07003996 FromClass(dex_file_->GetFieldDeclaringClassDescriptor(field_id),
3997 klass, klass->CannotBeAssignedFromOtherTypes());
Ian Rogersad0b3a32012-04-16 14:50:24 -07003998 if (obj_type.IsUninitializedTypes() &&
3999 (!IsConstructor() || GetDeclaringClass().Equals(obj_type) ||
4000 !field_klass.Equals(GetDeclaringClass()))) {
4001 // Field accesses through uninitialized references are only allowable for constructors where
4002 // the field is declared in this class
4003 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "cannot access instance field " << PrettyField(field)
Brian Carlstrom93c33962013-07-26 10:37:43 -07004004 << " of a not fully initialized object within the context"
4005 << " of " << PrettyMethod(dex_method_idx_, *dex_file_);
Ian Rogers7b078e82014-09-10 14:44:24 -07004006 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004007 } else if (!field_klass.IsAssignableFrom(obj_type)) {
4008 // Trying to access C1.field1 using reference of type C2, which is neither C1 or a sub-class
4009 // of C1. For resolution to occur the declared class of the field must be compatible with
4010 // obj_type, we've discovered this wasn't so, so report the field didn't exist.
4011 Fail(VERIFY_ERROR_NO_FIELD) << "cannot access instance field " << PrettyField(field)
4012 << " from object of type " << obj_type;
Ian Rogers7b078e82014-09-10 14:44:24 -07004013 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004014 } else {
4015 return field;
4016 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004017 }
4018}
4019
Andreas Gampe896df402014-10-20 22:25:29 -07004020template <MethodVerifier::FieldAccessType kAccType>
4021void MethodVerifier::VerifyISFieldAccess(const Instruction* inst, const RegType& insn_type,
4022 bool is_primitive, bool is_static) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02004023 uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
Mathieu Chartierc7853442015-03-27 14:35:38 -07004024 ArtField* field;
Ian Rogersb94a27b2011-10-26 00:33:41 -07004025 if (is_static) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07004026 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07004027 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004028 const RegType& object_type = work_line_->GetRegisterType(this, inst->VRegB_22c());
Ian Rogersf4028cc2011-11-02 14:56:39 -07004029 field = GetInstanceField(object_type, field_idx);
Andreas Gampe896df402014-10-20 22:25:29 -07004030 if (UNLIKELY(have_pending_hard_failure_)) {
4031 return;
4032 }
Ian Rogersb94a27b2011-10-26 00:33:41 -07004033 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00004034 const RegType* field_type = nullptr;
Ian Rogers7b078e82014-09-10 14:44:24 -07004035 if (field != nullptr) {
Andreas Gampe896df402014-10-20 22:25:29 -07004036 if (kAccType == FieldAccessType::kAccPut) {
4037 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
4038 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
4039 << " from other class " << GetDeclaringClass();
4040 return;
4041 }
4042 }
4043
Mathieu Chartierc7853442015-03-27 14:35:38 -07004044 mirror::Class* field_type_class =
4045 can_load_classes_ ? field->GetType<true>() : field->GetType<false>();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004046 if (field_type_class != nullptr) {
Andreas Gampef23f33d2015-06-23 14:18:17 -07004047 field_type = &FromClass(field->GetTypeDescriptor(), field_type_class,
4048 field_type_class->CannotBeAssignedFromOtherTypes());
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08004049 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004050 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
4051 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004052 }
Ian Rogers0d604842012-04-16 14:50:24 -07004053 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004054 if (field_type == nullptr) {
4055 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
4056 const char* descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
Mathieu Chartierbf99f772014-08-23 16:37:27 -07004057 field_type = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004058 }
Sebastien Hertz757b3042014-03-28 14:34:28 +01004059 DCHECK(field_type != nullptr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02004060 const uint32_t vregA = (is_static) ? inst->VRegA_21c() : inst->VRegA_22c();
Andreas Gampe896df402014-10-20 22:25:29 -07004061 static_assert(kAccType == FieldAccessType::kAccPut || kAccType == FieldAccessType::kAccGet,
4062 "Unexpected third access type");
4063 if (kAccType == FieldAccessType::kAccPut) {
4064 // sput or iput.
4065 if (is_primitive) {
4066 VerifyPrimitivePut(*field_type, insn_type, vregA);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004067 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004068 if (!insn_type.IsAssignableFrom(*field_type)) {
Vladimir Marko414000e2015-06-23 17:45:21 +01004069 // If the field type is not a reference, this is a global failure rather than
4070 // a class change failure as the instructions and the descriptors for the type
4071 // should have been consistent within the same file at compile time.
4072 VerifyError error = field_type->IsReferenceTypes() ? VERIFY_ERROR_BAD_CLASS_SOFT
4073 : VERIFY_ERROR_BAD_CLASS_HARD;
4074 Fail(error) << "expected field " << PrettyField(field)
4075 << " to be compatible with type '" << insn_type
4076 << "' but found type '" << *field_type
4077 << "' in put-object";
Andreas Gampe896df402014-10-20 22:25:29 -07004078 return;
4079 }
4080 work_line_->VerifyRegisterType(this, vregA, *field_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004081 }
Andreas Gampe896df402014-10-20 22:25:29 -07004082 } else if (kAccType == FieldAccessType::kAccGet) {
4083 // sget or iget.
4084 if (is_primitive) {
4085 if (field_type->Equals(insn_type) ||
4086 (field_type->IsFloat() && insn_type.IsInteger()) ||
4087 (field_type->IsDouble() && insn_type.IsLong())) {
4088 // expected that read is of the correct primitive type or that int reads are reading
4089 // floats or long reads are reading doubles
4090 } else {
4091 // This is a global failure rather than a class change failure as the instructions and
4092 // the descriptors for the type should have been consistent within the same file at
4093 // compile time
4094 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
4095 << " to be of type '" << insn_type
4096 << "' but found type '" << *field_type << "' in get";
4097 return;
4098 }
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08004099 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004100 if (!insn_type.IsAssignableFrom(*field_type)) {
Vladimir Marko414000e2015-06-23 17:45:21 +01004101 // If the field type is not a reference, this is a global failure rather than
4102 // a class change failure as the instructions and the descriptors for the type
4103 // should have been consistent within the same file at compile time.
4104 VerifyError error = field_type->IsReferenceTypes() ? VERIFY_ERROR_BAD_CLASS_SOFT
4105 : VERIFY_ERROR_BAD_CLASS_HARD;
4106 Fail(error) << "expected field " << PrettyField(field)
4107 << " to be compatible with type '" << insn_type
4108 << "' but found type '" << *field_type
4109 << "' in get-object";
Andreas Gampe896df402014-10-20 22:25:29 -07004110 work_line_->SetRegisterType(this, vregA, reg_types_.Conflict());
4111 return;
4112 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004113 }
Andreas Gampe896df402014-10-20 22:25:29 -07004114 if (!field_type->IsLowHalf()) {
4115 work_line_->SetRegisterType(this, vregA, *field_type);
4116 } else {
4117 work_line_->SetRegisterTypeWide(this, vregA, *field_type, field_type->HighHalf(&reg_types_));
4118 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07004119 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004120 LOG(FATAL) << "Unexpected case.";
Ian Rogersd81871c2011-10-03 13:57:23 -07004121 }
4122}
4123
Mathieu Chartierc7853442015-03-27 14:35:38 -07004124ArtField* MethodVerifier::GetQuickFieldAccess(const Instruction* inst,
Ian Rogers98379392014-02-24 16:53:16 -08004125 RegisterLine* reg_line) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08004126 DCHECK(IsInstructionIGetQuickOrIPutQuick(inst->Opcode())) << inst->Opcode();
Ian Rogers7b078e82014-09-10 14:44:24 -07004127 const RegType& object_type = reg_line->GetRegisterType(this, inst->VRegB_22c());
Ian Rogers9bc54402014-04-17 16:40:01 -07004128 if (!object_type.HasClass()) {
4129 VLOG(verifier) << "Failed to get mirror::Class* from '" << object_type << "'";
4130 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004131 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004132 uint32_t field_offset = static_cast<uint32_t>(inst->VRegC_22c());
Mathieu Chartierc7853442015-03-27 14:35:38 -07004133 ArtField* const f = ArtField::FindInstanceFieldWithOffset(object_type.GetClass(), field_offset);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08004134 DCHECK_EQ(f->GetOffset().Uint32Value(), field_offset);
Sebastien Hertz479fc1e2014-04-04 17:51:34 +02004135 if (f == nullptr) {
4136 VLOG(verifier) << "Failed to find instance field at offset '" << field_offset
4137 << "' from '" << PrettyDescriptor(object_type.GetClass()) << "'";
4138 }
4139 return f;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004140}
4141
Andreas Gampe896df402014-10-20 22:25:29 -07004142template <MethodVerifier::FieldAccessType kAccType>
4143void MethodVerifier::VerifyQuickFieldAccess(const Instruction* inst, const RegType& insn_type,
4144 bool is_primitive) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07004145 DCHECK(Runtime::Current()->IsStarted() || verify_to_dump_);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004146
Mathieu Chartierc7853442015-03-27 14:35:38 -07004147 ArtField* field = GetQuickFieldAccess(inst, work_line_.get());
Ian Rogers7b078e82014-09-10 14:44:24 -07004148 if (field == nullptr) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004149 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field from " << inst->Name();
4150 return;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004151 }
Andreas Gampe896df402014-10-20 22:25:29 -07004152
4153 // For an IPUT_QUICK, we now test for final flag of the field.
4154 if (kAccType == FieldAccessType::kAccPut) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004155 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
4156 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004157 << " from other class " << GetDeclaringClass();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004158 return;
4159 }
4160 }
Andreas Gampe896df402014-10-20 22:25:29 -07004161
4162 // Get the field type.
4163 const RegType* field_type;
4164 {
Mathieu Chartierc7853442015-03-27 14:35:38 -07004165 mirror::Class* field_type_class = can_load_classes_ ? field->GetType<true>() :
4166 field->GetType<false>();
Andreas Gampe896df402014-10-20 22:25:29 -07004167
4168 if (field_type_class != nullptr) {
Andreas Gampef23f33d2015-06-23 14:18:17 -07004169 field_type = &FromClass(field->GetTypeDescriptor(), field_type_class,
4170 field_type_class->CannotBeAssignedFromOtherTypes());
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004171 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004172 Thread* self = Thread::Current();
4173 DCHECK(!can_load_classes_ || self->IsExceptionPending());
4174 self->ClearException();
4175 field_type = &reg_types_.FromDescriptor(field->GetDeclaringClass()->GetClassLoader(),
4176 field->GetTypeDescriptor(), false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004177 }
Andreas Gampe896df402014-10-20 22:25:29 -07004178 if (field_type == nullptr) {
4179 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field type from " << inst->Name();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004180 return;
4181 }
Andreas Gampe896df402014-10-20 22:25:29 -07004182 }
4183
4184 const uint32_t vregA = inst->VRegA_22c();
4185 static_assert(kAccType == FieldAccessType::kAccPut || kAccType == FieldAccessType::kAccGet,
4186 "Unexpected third access type");
4187 if (kAccType == FieldAccessType::kAccPut) {
4188 if (is_primitive) {
4189 // Primitive field assignability rules are weaker than regular assignability rules
4190 bool instruction_compatible;
4191 bool value_compatible;
4192 const RegType& value_type = work_line_->GetRegisterType(this, vregA);
4193 if (field_type->IsIntegralTypes()) {
4194 instruction_compatible = insn_type.IsIntegralTypes();
4195 value_compatible = value_type.IsIntegralTypes();
4196 } else if (field_type->IsFloat()) {
4197 instruction_compatible = insn_type.IsInteger(); // no [is]put-float, so expect [is]put-int
4198 value_compatible = value_type.IsFloatTypes();
4199 } else if (field_type->IsLong()) {
4200 instruction_compatible = insn_type.IsLong();
4201 value_compatible = value_type.IsLongTypes();
4202 } else if (field_type->IsDouble()) {
4203 instruction_compatible = insn_type.IsLong(); // no [is]put-double, so expect [is]put-long
4204 value_compatible = value_type.IsDoubleTypes();
4205 } else {
4206 instruction_compatible = false; // reference field with primitive store
4207 value_compatible = false; // unused
4208 }
4209 if (!instruction_compatible) {
4210 // This is a global failure rather than a class change failure as the instructions and
4211 // the descriptors for the type should have been consistent within the same file at
4212 // compile time
4213 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
4214 << " to be of type '" << insn_type
4215 << "' but found type '" << *field_type
4216 << "' in put";
4217 return;
4218 }
4219 if (!value_compatible) {
4220 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
4221 << " of type " << value_type
4222 << " but expected " << *field_type
4223 << " for store to " << PrettyField(field) << " in put";
4224 return;
4225 }
4226 } else {
4227 if (!insn_type.IsAssignableFrom(*field_type)) {
4228 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
4229 << " to be compatible with type '" << insn_type
4230 << "' but found type '" << *field_type
4231 << "' in put-object";
4232 return;
4233 }
4234 work_line_->VerifyRegisterType(this, vregA, *field_type);
4235 }
4236 } else if (kAccType == FieldAccessType::kAccGet) {
4237 if (is_primitive) {
4238 if (field_type->Equals(insn_type) ||
4239 (field_type->IsFloat() && insn_type.IsIntegralTypes()) ||
4240 (field_type->IsDouble() && insn_type.IsLongTypes())) {
4241 // expected that read is of the correct primitive type or that int reads are reading
4242 // floats or long reads are reading doubles
4243 } else {
4244 // This is a global failure rather than a class change failure as the instructions and
4245 // the descriptors for the type should have been consistent within the same file at
4246 // compile time
4247 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
4248 << " to be of type '" << insn_type
4249 << "' but found type '" << *field_type << "' in Get";
4250 return;
4251 }
4252 } else {
4253 if (!insn_type.IsAssignableFrom(*field_type)) {
4254 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
4255 << " to be compatible with type '" << insn_type
4256 << "' but found type '" << *field_type
4257 << "' in get-object";
4258 work_line_->SetRegisterType(this, vregA, reg_types_.Conflict());
4259 return;
4260 }
4261 }
4262 if (!field_type->IsLowHalf()) {
4263 work_line_->SetRegisterType(this, vregA, *field_type);
4264 } else {
4265 work_line_->SetRegisterTypeWide(this, vregA, *field_type, field_type->HighHalf(&reg_types_));
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004266 }
4267 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004268 LOG(FATAL) << "Unexpected case.";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004269 }
4270}
4271
Ian Rogers776ac1f2012-04-13 23:36:36 -07004272bool MethodVerifier::CheckNotMoveException(const uint16_t* insns, int insn_idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004273 if ((insns[insn_idx] & 0xff) == Instruction::MOVE_EXCEPTION) {
jeffhaod5347e02012-03-22 17:25:05 -07004274 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-exception";
Ian Rogersd81871c2011-10-03 13:57:23 -07004275 return false;
4276 }
4277 return true;
4278}
4279
Stephen Kyle9bc61992014-09-22 13:53:15 +01004280bool MethodVerifier::CheckNotMoveResult(const uint16_t* insns, int insn_idx) {
4281 if (((insns[insn_idx] & 0xff) >= Instruction::MOVE_RESULT) &&
4282 ((insns[insn_idx] & 0xff) <= Instruction::MOVE_RESULT_OBJECT)) {
4283 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-result*";
4284 return false;
4285 }
4286 return true;
4287}
4288
4289bool MethodVerifier::CheckNotMoveExceptionOrMoveResult(const uint16_t* insns, int insn_idx) {
4290 return (CheckNotMoveException(insns, insn_idx) && CheckNotMoveResult(insns, insn_idx));
4291}
4292
Ian Rogersebbdd872014-07-07 23:53:08 -07004293bool MethodVerifier::UpdateRegisters(uint32_t next_insn, RegisterLine* merge_line,
4294 bool update_merge_line) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004295 bool changed = true;
4296 RegisterLine* target_line = reg_table_.GetLine(next_insn);
4297 if (!insn_flags_[next_insn].IsVisitedOrChanged()) {
jeffhaobdb76512011-09-07 11:43:16 -07004298 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07004299 * We haven't processed this instruction before, and we haven't touched the registers here, so
4300 * there's nothing to "merge". Copy the registers over and mark it as changed. (This is the
4301 * only way a register can transition out of "unknown", so this is not just an optimization.)
jeffhaobdb76512011-09-07 11:43:16 -07004302 */
Ian Rogersb8c78592013-07-25 23:52:52 +00004303 if (!insn_flags_[next_insn].IsReturn()) {
4304 target_line->CopyFromLine(merge_line);
4305 } else {
Jeff Haob24b4a72013-07-31 13:47:31 -07004306 // Verify that the monitor stack is empty on return.
Ian Rogers7b078e82014-09-10 14:44:24 -07004307 if (!merge_line->VerifyMonitorStackEmpty(this)) {
Jeff Haob24b4a72013-07-31 13:47:31 -07004308 return false;
4309 }
Ian Rogersb8c78592013-07-25 23:52:52 +00004310 // For returns we only care about the operand to the return, all other registers are dead.
4311 // Initialize them as conflicts so they don't add to GC and deoptimization information.
4312 const Instruction* ret_inst = Instruction::At(code_item_->insns_ + next_insn);
4313 Instruction::Code opcode = ret_inst->Opcode();
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07004314 if (opcode == Instruction::RETURN_VOID || opcode == Instruction::RETURN_VOID_NO_BARRIER) {
Stephen Kyle7e541c92014-12-17 17:10:02 +00004315 SafelyMarkAllRegistersAsConflicts(this, target_line);
Ian Rogersb8c78592013-07-25 23:52:52 +00004316 } else {
4317 target_line->CopyFromLine(merge_line);
4318 if (opcode == Instruction::RETURN_WIDE) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004319 target_line->MarkAllRegistersAsConflictsExceptWide(this, ret_inst->VRegA_11x());
Ian Rogersb8c78592013-07-25 23:52:52 +00004320 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004321 target_line->MarkAllRegistersAsConflictsExcept(this, ret_inst->VRegA_11x());
Ian Rogersb8c78592013-07-25 23:52:52 +00004322 }
4323 }
4324 }
jeffhaobdb76512011-09-07 11:43:16 -07004325 } else {
Ian Rogers700a4022014-05-19 16:49:03 -07004326 std::unique_ptr<RegisterLine> copy(gDebugVerify ?
Ian Rogersd0fbd852013-09-24 18:17:04 -07004327 RegisterLine::Create(target_line->NumRegs(), this) :
Ian Rogers7b078e82014-09-10 14:44:24 -07004328 nullptr);
Ian Rogers7b0c5b42012-02-16 15:29:07 -08004329 if (gDebugVerify) {
4330 copy->CopyFromLine(target_line);
4331 }
Ian Rogers7b078e82014-09-10 14:44:24 -07004332 changed = target_line->MergeRegisters(this, merge_line);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004333 if (have_pending_hard_failure_) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004334 return false;
jeffhaobdb76512011-09-07 11:43:16 -07004335 }
Ian Rogers2c8a8572011-10-24 17:11:36 -07004336 if (gDebugVerify && changed) {
Elliott Hughes398f64b2012-03-26 18:05:48 -07004337 LogVerifyInfo() << "Merging at [" << reinterpret_cast<void*>(work_insn_idx_) << "]"
Elliott Hughesc073b072012-05-24 19:29:17 -07004338 << " to [" << reinterpret_cast<void*>(next_insn) << "]: " << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07004339 << copy->Dump(this) << " MERGE\n"
4340 << merge_line->Dump(this) << " ==\n"
4341 << target_line->Dump(this) << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07004342 }
Ian Rogersebbdd872014-07-07 23:53:08 -07004343 if (update_merge_line && changed) {
4344 merge_line->CopyFromLine(target_line);
4345 }
jeffhaobdb76512011-09-07 11:43:16 -07004346 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004347 if (changed) {
4348 insn_flags_[next_insn].SetChanged();
jeffhaobdb76512011-09-07 11:43:16 -07004349 }
4350 return true;
4351}
4352
Ian Rogers7b3ddd22013-02-21 15:19:52 -08004353InstructionFlags* MethodVerifier::CurrentInsnFlags() {
Ian Rogers776ac1f2012-04-13 23:36:36 -07004354 return &insn_flags_[work_insn_idx_];
4355}
4356
Ian Rogersd8f69b02014-09-10 21:43:52 +00004357const RegType& MethodVerifier::GetMethodReturnType() {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004358 if (return_type_ == nullptr) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07004359 if (mirror_method_ != nullptr) {
Ian Rogersded66a02014-10-28 18:12:55 -07004360 mirror::Class* return_type_class = mirror_method_->GetReturnType(can_load_classes_);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004361 if (return_type_class != nullptr) {
Andreas Gampef23f33d2015-06-23 14:18:17 -07004362 return_type_ = &FromClass(mirror_method_->GetReturnTypeDescriptor(),
4363 return_type_class,
4364 return_type_class->CannotBeAssignedFromOtherTypes());
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004365 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004366 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
4367 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004368 }
4369 }
4370 if (return_type_ == nullptr) {
4371 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
4372 const DexFile::ProtoId& proto_id = dex_file_->GetMethodPrototype(method_id);
4373 uint16_t return_type_idx = proto_id.return_type_idx_;
4374 const char* descriptor = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(return_type_idx));
Mathieu Chartierbf99f772014-08-23 16:37:27 -07004375 return_type_ = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004376 }
4377 }
4378 return *return_type_;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004379}
4380
Ian Rogersd8f69b02014-09-10 21:43:52 +00004381const RegType& MethodVerifier::GetDeclaringClass() {
Ian Rogers7b078e82014-09-10 14:44:24 -07004382 if (declaring_class_ == nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004383 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
Brian Carlstrom93c33962013-07-26 10:37:43 -07004384 const char* descriptor
4385 = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(method_id.class_idx_));
Mathieu Chartiere401d142015-04-22 13:56:20 -07004386 if (mirror_method_ != nullptr) {
Ian Rogers637c65b2013-05-31 11:46:00 -07004387 mirror::Class* klass = mirror_method_->GetDeclaringClass();
Andreas Gampef23f33d2015-06-23 14:18:17 -07004388 declaring_class_ = &FromClass(descriptor, klass,
4389 klass->CannotBeAssignedFromOtherTypes());
Ian Rogers637c65b2013-05-31 11:46:00 -07004390 } else {
Mathieu Chartierbf99f772014-08-23 16:37:27 -07004391 declaring_class_ = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers637c65b2013-05-31 11:46:00 -07004392 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07004393 }
Ian Rogers637c65b2013-05-31 11:46:00 -07004394 return *declaring_class_;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004395}
4396
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004397std::vector<int32_t> MethodVerifier::DescribeVRegs(uint32_t dex_pc) {
4398 RegisterLine* line = reg_table_.GetLine(dex_pc);
Sebastien Hertzaa0c00c2014-03-14 17:58:54 +01004399 DCHECK(line != nullptr) << "No register line at DEX pc " << StringPrintf("0x%x", dex_pc);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004400 std::vector<int32_t> result;
4401 for (size_t i = 0; i < line->NumRegs(); ++i) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004402 const RegType& type = line->GetRegisterType(this, i);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004403 if (type.IsConstant()) {
4404 result.push_back(type.IsPreciseConstant() ? kConstant : kImpreciseConstant);
Ian Rogers7b078e82014-09-10 14:44:24 -07004405 const ConstantType* const_val = down_cast<const ConstantType*>(&type);
4406 result.push_back(const_val->ConstantValue());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004407 } else if (type.IsConstantLo()) {
4408 result.push_back(type.IsPreciseConstantLo() ? kConstant : kImpreciseConstant);
Ian Rogers7b078e82014-09-10 14:44:24 -07004409 const ConstantType* const_val = down_cast<const ConstantType*>(&type);
4410 result.push_back(const_val->ConstantValueLo());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004411 } else if (type.IsConstantHi()) {
4412 result.push_back(type.IsPreciseConstantHi() ? kConstant : kImpreciseConstant);
Ian Rogers7b078e82014-09-10 14:44:24 -07004413 const ConstantType* const_val = down_cast<const ConstantType*>(&type);
4414 result.push_back(const_val->ConstantValueHi());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004415 } else if (type.IsIntegralTypes()) {
4416 result.push_back(kIntVReg);
4417 result.push_back(0);
4418 } else if (type.IsFloat()) {
4419 result.push_back(kFloatVReg);
4420 result.push_back(0);
4421 } else if (type.IsLong()) {
4422 result.push_back(kLongLoVReg);
4423 result.push_back(0);
4424 result.push_back(kLongHiVReg);
4425 result.push_back(0);
4426 ++i;
4427 } else if (type.IsDouble()) {
4428 result.push_back(kDoubleLoVReg);
4429 result.push_back(0);
4430 result.push_back(kDoubleHiVReg);
4431 result.push_back(0);
4432 ++i;
4433 } else if (type.IsUndefined() || type.IsConflict() || type.IsHighHalf()) {
4434 result.push_back(kUndefined);
4435 result.push_back(0);
4436 } else {
Ian Rogers7b3ddd22013-02-21 15:19:52 -08004437 CHECK(type.IsNonZeroReferenceTypes());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004438 result.push_back(kReferenceVReg);
4439 result.push_back(0);
4440 }
4441 }
4442 return result;
4443}
4444
Ian Rogersd8f69b02014-09-10 21:43:52 +00004445const RegType& MethodVerifier::DetermineCat1Constant(int32_t value, bool precise) {
Sebastien Hertz849600b2013-12-20 10:28:08 +01004446 if (precise) {
4447 // Precise constant type.
4448 return reg_types_.FromCat1Const(value, true);
4449 } else {
4450 // Imprecise constant type.
4451 if (value < -32768) {
4452 return reg_types_.IntConstant();
4453 } else if (value < -128) {
4454 return reg_types_.ShortConstant();
4455 } else if (value < 0) {
4456 return reg_types_.ByteConstant();
4457 } else if (value == 0) {
4458 return reg_types_.Zero();
4459 } else if (value == 1) {
4460 return reg_types_.One();
4461 } else if (value < 128) {
4462 return reg_types_.PosByteConstant();
4463 } else if (value < 32768) {
4464 return reg_types_.PosShortConstant();
4465 } else if (value < 65536) {
4466 return reg_types_.CharConstant();
4467 } else {
4468 return reg_types_.IntConstant();
4469 }
4470 }
4471}
4472
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004473void MethodVerifier::Init() {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08004474 art::verifier::RegTypeCache::Init();
Brian Carlstrome7d856b2012-01-11 18:10:55 -08004475}
4476
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004477void MethodVerifier::Shutdown() {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08004478 verifier::RegTypeCache::ShutDown();
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08004479}
jeffhaod1224c72012-02-29 13:43:08 -08004480
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -07004481void MethodVerifier::VisitStaticRoots(RootVisitor* visitor) {
4482 RegTypeCache::VisitStaticRoots(visitor);
Mathieu Chartier7c438b12014-09-12 17:01:24 -07004483}
4484
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -07004485void MethodVerifier::VisitRoots(RootVisitor* visitor, const RootInfo& root_info) {
4486 reg_types_.VisitRoots(visitor, root_info);
Mathieu Chartierc528dba2013-11-26 12:00:11 -08004487}
4488
Andreas Gampef23f33d2015-06-23 14:18:17 -07004489const RegType& MethodVerifier::FromClass(const char* descriptor,
4490 mirror::Class* klass,
4491 bool precise) {
4492 DCHECK(klass != nullptr);
4493 if (precise && !klass->IsInstantiable() && !klass->IsPrimitive()) {
4494 Fail(VerifyError::VERIFY_ERROR_NO_CLASS) << "Could not create precise reference for "
4495 << "non-instantiable klass " << descriptor;
4496 precise = false;
4497 }
4498 return reg_types_.FromClass(descriptor, klass, precise);
4499}
4500
Ian Rogersd81871c2011-10-03 13:57:23 -07004501} // namespace verifier
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07004502} // namespace art