blob: 8a8b455603aee0fa1658d3465979396125a611a5 [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),
Andreas Gamped5ad72f2015-06-26 17:33:47 -0700388 work_insn_idx_(DexFile::kDexNoIndex),
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),
Andreas Gamped5ad72f2015-06-26 17:33:47 -0700412 allow_thread_suspension_(allow_thread_suspension),
413 link_(nullptr) {
Mathieu Chartierd0ad2ee2015-03-31 14:59:59 -0700414 self->PushVerifier(this);
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700415 DCHECK(class_def != nullptr);
jeffhaof56197c2012-03-05 18:01:54 -0800416}
417
Mathieu Chartier590fee92013-09-13 13:46:47 -0700418MethodVerifier::~MethodVerifier() {
Mathieu Chartierd0ad2ee2015-03-31 14:59:59 -0700419 Thread::Current()->PopVerifier(this);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700420 STLDeleteElements(&failure_messages_);
421}
422
Mathieu Chartiere401d142015-04-22 13:56:20 -0700423void MethodVerifier::FindLocksAtDexPc(ArtMethod* m, uint32_t dex_pc,
Ian Rogers46960fe2014-05-23 10:43:43 -0700424 std::vector<uint32_t>* monitor_enter_dex_pcs) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700425 StackHandleScope<2> hs(Thread::Current());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700426 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache()));
427 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader()));
Mathieu Chartiere401d142015-04-22 13:56:20 -0700428 MethodVerifier verifier(hs.Self(), m->GetDexFile(), dex_cache, class_loader, &m->GetClassDef(),
429 m->GetCodeItem(), m->GetDexMethodIndex(), m, m->GetAccessFlags(),
Mathieu Chartier4306ef82014-12-19 18:41:47 -0800430 false, true, false, false);
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700431 verifier.interesting_dex_pc_ = dex_pc;
Ian Rogers46960fe2014-05-23 10:43:43 -0700432 verifier.monitor_enter_dex_pcs_ = monitor_enter_dex_pcs;
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700433 verifier.FindLocksAtDexPc();
434}
435
Andreas Gampecb3c08f2014-09-18 13:16:38 -0700436static bool HasMonitorEnterInstructions(const DexFile::CodeItem* const code_item) {
437 const Instruction* inst = Instruction::At(code_item->insns_);
438
439 uint32_t insns_size = code_item->insns_size_in_code_units_;
440 for (uint32_t dex_pc = 0; dex_pc < insns_size;) {
441 if (inst->Opcode() == Instruction::MONITOR_ENTER) {
442 return true;
443 }
444
445 dex_pc += inst->SizeInCodeUnits();
446 inst = inst->Next();
447 }
448
449 return false;
450}
451
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700452void MethodVerifier::FindLocksAtDexPc() {
Ian Rogers7b078e82014-09-10 14:44:24 -0700453 CHECK(monitor_enter_dex_pcs_ != nullptr);
454 CHECK(code_item_ != nullptr); // This only makes sense for methods with code.
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700455
Andreas Gampecb3c08f2014-09-18 13:16:38 -0700456 // Quick check whether there are any monitor_enter instructions at all.
457 if (!HasMonitorEnterInstructions(code_item_)) {
458 return;
459 }
460
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700461 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
462 // verification. In practice, the phase we want relies on data structures set up by all the
463 // earlier passes, so we just run the full method verification and bail out early when we've
464 // got what we wanted.
465 Verify();
466}
467
Mathieu Chartiere401d142015-04-22 13:56:20 -0700468ArtField* MethodVerifier::FindAccessedFieldAtDexPc(ArtMethod* m, uint32_t dex_pc) {
469 StackHandleScope<2> hs(Thread::Current());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700470 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache()));
471 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader()));
Mathieu Chartiere401d142015-04-22 13:56:20 -0700472 MethodVerifier verifier(hs.Self(), m->GetDexFile(), dex_cache, class_loader, &m->GetClassDef(),
473 m->GetCodeItem(), m->GetDexMethodIndex(), m, m->GetAccessFlags(), true,
474 true, false, true);
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200475 return verifier.FindAccessedFieldAtDexPc(dex_pc);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200476}
477
Mathieu Chartierc7853442015-03-27 14:35:38 -0700478ArtField* MethodVerifier::FindAccessedFieldAtDexPc(uint32_t dex_pc) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700479 CHECK(code_item_ != nullptr); // This only makes sense for methods with code.
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200480
481 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
482 // verification. In practice, the phase we want relies on data structures set up by all the
483 // earlier passes, so we just run the full method verification and bail out early when we've
484 // got what we wanted.
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200485 bool success = Verify();
486 if (!success) {
Ian Rogers9bc54402014-04-17 16:40:01 -0700487 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200488 }
489 RegisterLine* register_line = reg_table_.GetLine(dex_pc);
Ian Rogers7b078e82014-09-10 14:44:24 -0700490 if (register_line == nullptr) {
Ian Rogers9bc54402014-04-17 16:40:01 -0700491 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200492 }
493 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
494 return GetQuickFieldAccess(inst, register_line);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200495}
496
Mathieu Chartiere401d142015-04-22 13:56:20 -0700497ArtMethod* MethodVerifier::FindInvokedMethodAtDexPc(ArtMethod* m, uint32_t dex_pc) {
498 StackHandleScope<2> hs(Thread::Current());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700499 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache()));
500 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader()));
Mathieu Chartiere401d142015-04-22 13:56:20 -0700501 MethodVerifier verifier(hs.Self(), m->GetDexFile(), dex_cache, class_loader, &m->GetClassDef(),
502 m->GetCodeItem(), m->GetDexMethodIndex(), m, m->GetAccessFlags(), true,
503 true, false, true);
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200504 return verifier.FindInvokedMethodAtDexPc(dex_pc);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200505}
506
Mathieu Chartiere401d142015-04-22 13:56:20 -0700507ArtMethod* MethodVerifier::FindInvokedMethodAtDexPc(uint32_t dex_pc) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700508 CHECK(code_item_ != nullptr); // This only makes sense for methods with code.
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200509
510 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
511 // verification. In practice, the phase we want relies on data structures set up by all the
512 // earlier passes, so we just run the full method verification and bail out early when we've
513 // got what we wanted.
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200514 bool success = Verify();
515 if (!success) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700516 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200517 }
518 RegisterLine* register_line = reg_table_.GetLine(dex_pc);
Ian Rogers7b078e82014-09-10 14:44:24 -0700519 if (register_line == nullptr) {
520 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200521 }
522 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
523 const bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
Mathieu Chartier091d2382015-03-06 10:59:06 -0800524 return GetQuickInvokedMethod(inst, register_line, is_range, false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200525}
526
Mathieu Chartiere401d142015-04-22 13:56:20 -0700527SafeMap<uint32_t, std::set<uint32_t>> MethodVerifier::FindStringInitMap(ArtMethod* m) {
Jeff Hao848f70a2014-01-15 13:49:50 -0800528 Thread* self = Thread::Current();
Mathieu Chartiere401d142015-04-22 13:56:20 -0700529 StackHandleScope<2> hs(self);
Jeff Hao848f70a2014-01-15 13:49:50 -0800530 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache()));
531 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader()));
Jeff Hao848f70a2014-01-15 13:49:50 -0800532 MethodVerifier verifier(self, m->GetDexFile(), dex_cache, class_loader, &m->GetClassDef(),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700533 m->GetCodeItem(), m->GetDexMethodIndex(), m, m->GetAccessFlags(),
Jeff Hao848f70a2014-01-15 13:49:50 -0800534 true, true, false, true);
535 return verifier.FindStringInitMap();
536}
537
538SafeMap<uint32_t, std::set<uint32_t>>& MethodVerifier::FindStringInitMap() {
539 Verify();
540 return GetStringInitPcRegMap();
541}
542
Ian Rogersad0b3a32012-04-16 14:50:24 -0700543bool MethodVerifier::Verify() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700544 // If there aren't any instructions, make sure that's expected, then exit successfully.
Ian Rogers7b078e82014-09-10 14:44:24 -0700545 if (code_item_ == nullptr) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700546 if ((method_access_flags_ & (kAccNative | kAccAbstract)) == 0) {
jeffhaod5347e02012-03-22 17:25:05 -0700547 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "zero-length code in concrete non-native method";
jeffhaobdb76512011-09-07 11:43:16 -0700548 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -0700549 } else {
550 return true;
jeffhaobdb76512011-09-07 11:43:16 -0700551 }
jeffhaobdb76512011-09-07 11:43:16 -0700552 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700553 // Sanity-check the register counts. ins + locals = registers, so make sure that ins <= registers.
554 if (code_item_->ins_size_ > code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700555 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad register counts (ins=" << code_item_->ins_size_
556 << " regs=" << code_item_->registers_size_;
Ian Rogersd81871c2011-10-03 13:57:23 -0700557 return false;
jeffhaobdb76512011-09-07 11:43:16 -0700558 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700559 // Allocate and initialize an array to hold instruction data.
Ian Rogers7b3ddd22013-02-21 15:19:52 -0800560 insn_flags_.reset(new InstructionFlags[code_item_->insns_size_in_code_units_]());
Ian Rogersd81871c2011-10-03 13:57:23 -0700561 // Run through the instructions and see if the width checks out.
562 bool result = ComputeWidthsAndCountOps();
563 // Flag instructions guarded by a "try" block and check exception handlers.
564 result = result && ScanTryCatchBlocks();
565 // Perform static instruction verification.
566 result = result && VerifyInstructions();
Ian Rogersad0b3a32012-04-16 14:50:24 -0700567 // Perform code-flow analysis and return.
Vladimir Marko2b5eaa22013-12-13 13:59:30 +0000568 result = result && VerifyCodeFlow();
569 // Compute information for compiler.
570 if (result && Runtime::Current()->IsCompiler()) {
571 result = Runtime::Current()->GetCompilerCallbacks()->MethodVerified(this);
572 }
573 return result;
jeffhaoba5ebb92011-08-25 17:24:37 -0700574}
575
Ian Rogers776ac1f2012-04-13 23:36:36 -0700576std::ostream& MethodVerifier::Fail(VerifyError error) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700577 switch (error) {
578 case VERIFY_ERROR_NO_CLASS:
579 case VERIFY_ERROR_NO_FIELD:
580 case VERIFY_ERROR_NO_METHOD:
581 case VERIFY_ERROR_ACCESS_CLASS:
582 case VERIFY_ERROR_ACCESS_FIELD:
583 case VERIFY_ERROR_ACCESS_METHOD:
Ian Rogers08f753d2012-08-24 14:35:25 -0700584 case VERIFY_ERROR_INSTANTIATION:
585 case VERIFY_ERROR_CLASS_CHANGE:
Igor Murashkin158f35c2015-06-10 15:55:30 -0700586 case VERIFY_ERROR_FORCE_INTERPRETER:
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800587 if (Runtime::Current()->IsAotCompiler() || !can_load_classes_) {
jeffhaofaf459e2012-08-31 15:32:47 -0700588 // If we're optimistically running verification at compile time, turn NO_xxx, ACCESS_xxx,
589 // class change and instantiation errors into soft verification errors so that we re-verify
590 // at runtime. We may fail to find or to agree on access because of not yet available class
591 // loaders, or class loaders that will differ at runtime. In these cases, we don't want to
592 // affect the soundness of the code being compiled. Instead, the generated code runs "slow
593 // paths" that dynamically perform the verification and cause the behavior to be that akin
594 // to an interpreter.
595 error = VERIFY_ERROR_BAD_CLASS_SOFT;
596 } else {
Jeff Haoa3faaf42013-09-03 19:07:00 -0700597 // If we fail again at runtime, mark that this instruction would throw and force this
598 // method to be executed using the interpreter with checks.
jeffhaofaf459e2012-08-31 15:32:47 -0700599 have_pending_runtime_throw_failure_ = true;
Andreas Gamped7f8d052015-03-12 11:05:47 -0700600
601 // We need to save the work_line if the instruction wasn't throwing before. Otherwise we'll
602 // try to merge garbage.
603 // Note: this assumes that Fail is called before we do any work_line modifications.
Andreas Gamped5ad72f2015-06-26 17:33:47 -0700604 // Note: this can fail before we touch any instruction, for the signature of a method. So
605 // add a check.
606 if (work_insn_idx_ < DexFile::kDexNoIndex) {
607 const uint16_t* insns = code_item_->insns_ + work_insn_idx_;
608 const Instruction* inst = Instruction::At(insns);
609 int opcode_flags = Instruction::FlagsOf(inst->Opcode());
Andreas Gamped7f8d052015-03-12 11:05:47 -0700610
Andreas Gamped5ad72f2015-06-26 17:33:47 -0700611 if ((opcode_flags & Instruction::kThrow) == 0 && CurrentInsnFlags()->IsInTry()) {
612 saved_line_->CopyFromLine(work_line_.get());
613 }
Andreas Gamped7f8d052015-03-12 11:05:47 -0700614 }
jeffhaofaf459e2012-08-31 15:32:47 -0700615 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700616 break;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700617 // Indication that verification should be retried at runtime.
618 case VERIFY_ERROR_BAD_CLASS_SOFT:
Jeff Haoee988952013-04-16 14:23:47 -0700619 if (!allow_soft_failures_) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700620 have_pending_hard_failure_ = true;
621 }
622 break;
jeffhaod5347e02012-03-22 17:25:05 -0700623 // Hard verification failures at compile time will still fail at runtime, so the class is
624 // marked as rejected to prevent it from being compiled.
Ian Rogersad0b3a32012-04-16 14:50:24 -0700625 case VERIFY_ERROR_BAD_CLASS_HARD: {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800626 if (Runtime::Current()->IsAotCompiler()) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700627 ClassReference ref(dex_file_, dex_file_->GetIndexForClassDef(*class_def_));
Vladimir Marko2b5eaa22013-12-13 13:59:30 +0000628 Runtime::Current()->GetCompilerCallbacks()->ClassRejected(ref);
jeffhaod1224c72012-02-29 13:43:08 -0800629 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700630 have_pending_hard_failure_ = true;
631 break;
Ian Rogers47a05882012-02-03 12:23:33 -0800632 }
633 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700634 failures_.push_back(error);
Elena Sayapina78480ec2014-08-15 15:52:42 +0700635 std::string location(StringPrintf("%s: [0x%X] ", PrettyMethod(dex_method_idx_, *dex_file_).c_str(),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700636 work_insn_idx_));
Elena Sayapina78480ec2014-08-15 15:52:42 +0700637 std::ostringstream* failure_message = new std::ostringstream(location, std::ostringstream::ate);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700638 failure_messages_.push_back(failure_message);
639 return *failure_message;
640}
641
Ian Rogers576ca0c2014-06-06 15:58:22 -0700642std::ostream& MethodVerifier::LogVerifyInfo() {
643 return info_messages_ << "VFY: " << PrettyMethod(dex_method_idx_, *dex_file_)
644 << '[' << reinterpret_cast<void*>(work_insn_idx_) << "] : ";
645}
646
Ian Rogersad0b3a32012-04-16 14:50:24 -0700647void MethodVerifier::PrependToLastFailMessage(std::string prepend) {
648 size_t failure_num = failure_messages_.size();
649 DCHECK_NE(failure_num, 0U);
650 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
651 prepend += last_fail_message->str();
Elena Sayapina78480ec2014-08-15 15:52:42 +0700652 failure_messages_[failure_num - 1] = new std::ostringstream(prepend, std::ostringstream::ate);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700653 delete last_fail_message;
654}
655
656void MethodVerifier::AppendToLastFailMessage(std::string append) {
657 size_t failure_num = failure_messages_.size();
658 DCHECK_NE(failure_num, 0U);
659 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
660 (*last_fail_message) << append;
Ian Rogers47a05882012-02-03 12:23:33 -0800661}
662
Ian Rogers776ac1f2012-04-13 23:36:36 -0700663bool MethodVerifier::ComputeWidthsAndCountOps() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700664 const uint16_t* insns = code_item_->insns_;
665 size_t insns_size = code_item_->insns_size_in_code_units_;
666 const Instruction* inst = Instruction::At(insns);
jeffhaobdb76512011-09-07 11:43:16 -0700667 size_t new_instance_count = 0;
668 size_t monitor_enter_count = 0;
Ian Rogersd81871c2011-10-03 13:57:23 -0700669 size_t dex_pc = 0;
jeffhaobdb76512011-09-07 11:43:16 -0700670
Ian Rogersd81871c2011-10-03 13:57:23 -0700671 while (dex_pc < insns_size) {
jeffhaobdb76512011-09-07 11:43:16 -0700672 Instruction::Code opcode = inst->Opcode();
Ian Rogersa9a82542013-10-04 11:17:26 -0700673 switch (opcode) {
674 case Instruction::APUT_OBJECT:
675 case Instruction::CHECK_CAST:
676 has_check_casts_ = true;
677 break;
678 case Instruction::INVOKE_VIRTUAL:
679 case Instruction::INVOKE_VIRTUAL_RANGE:
680 case Instruction::INVOKE_INTERFACE:
681 case Instruction::INVOKE_INTERFACE_RANGE:
682 has_virtual_or_interface_invokes_ = true;
683 break;
684 case Instruction::MONITOR_ENTER:
685 monitor_enter_count++;
686 break;
687 case Instruction::NEW_INSTANCE:
688 new_instance_count++;
689 break;
690 default:
691 break;
jeffhaobdb76512011-09-07 11:43:16 -0700692 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700693 size_t inst_size = inst->SizeInCodeUnits();
Ian Rogers7b078e82014-09-10 14:44:24 -0700694 insn_flags_[dex_pc].SetIsOpcode();
Ian Rogersd81871c2011-10-03 13:57:23 -0700695 dex_pc += inst_size;
Ian Rogers7b078e82014-09-10 14:44:24 -0700696 inst = inst->RelativeAt(inst_size);
jeffhaobdb76512011-09-07 11:43:16 -0700697 }
698
Ian Rogersd81871c2011-10-03 13:57:23 -0700699 if (dex_pc != insns_size) {
jeffhaod5347e02012-03-22 17:25:05 -0700700 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "code did not end where expected ("
701 << dex_pc << " vs. " << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700702 return false;
703 }
704
Ian Rogersd81871c2011-10-03 13:57:23 -0700705 new_instance_count_ = new_instance_count;
706 monitor_enter_count_ = monitor_enter_count;
jeffhaobdb76512011-09-07 11:43:16 -0700707 return true;
708}
709
Ian Rogers776ac1f2012-04-13 23:36:36 -0700710bool MethodVerifier::ScanTryCatchBlocks() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700711 uint32_t tries_size = code_item_->tries_size_;
jeffhaobdb76512011-09-07 11:43:16 -0700712 if (tries_size == 0) {
713 return true;
714 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700715 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Ian Rogers0571d352011-11-03 19:51:38 -0700716 const DexFile::TryItem* tries = DexFile::GetTryItems(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -0700717
718 for (uint32_t idx = 0; idx < tries_size; idx++) {
719 const DexFile::TryItem* try_item = &tries[idx];
720 uint32_t start = try_item->start_addr_;
721 uint32_t end = start + try_item->insn_count_;
jeffhaobdb76512011-09-07 11:43:16 -0700722 if ((start >= end) || (start >= insns_size) || (end > insns_size)) {
jeffhaod5347e02012-03-22 17:25:05 -0700723 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad exception entry: startAddr=" << start
724 << " endAddr=" << end << " (size=" << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700725 return false;
726 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700727 if (!insn_flags_[start].IsOpcode()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700728 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
729 << "'try' block starts inside an instruction (" << start << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700730 return false;
731 }
Ian Rogers7b078e82014-09-10 14:44:24 -0700732 uint32_t dex_pc = start;
733 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
734 while (dex_pc < end) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700735 insn_flags_[dex_pc].SetInTry();
Ian Rogers7b078e82014-09-10 14:44:24 -0700736 size_t insn_size = inst->SizeInCodeUnits();
737 dex_pc += insn_size;
738 inst = inst->RelativeAt(insn_size);
jeffhaobdb76512011-09-07 11:43:16 -0700739 }
740 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800741 // Iterate over each of the handlers to verify target addresses.
Ian Rogers13735952014-10-08 12:43:28 -0700742 const uint8_t* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -0700743 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700744 ClassLinker* linker = Runtime::Current()->GetClassLinker();
jeffhaobdb76512011-09-07 11:43:16 -0700745 for (uint32_t idx = 0; idx < handlers_size; idx++) {
Ian Rogers0571d352011-11-03 19:51:38 -0700746 CatchHandlerIterator iterator(handlers_ptr);
747 for (; iterator.HasNext(); iterator.Next()) {
748 uint32_t dex_pc= iterator.GetHandlerAddress();
Ian Rogersd81871c2011-10-03 13:57:23 -0700749 if (!insn_flags_[dex_pc].IsOpcode()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700750 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
751 << "exception handler starts at bad address (" << dex_pc << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700752 return false;
753 }
Stephen Kyle9bc61992014-09-22 13:53:15 +0100754 if (!CheckNotMoveResult(code_item_->insns_, dex_pc)) {
755 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
756 << "exception handler begins with move-result* (" << dex_pc << ")";
757 return false;
758 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700759 insn_flags_[dex_pc].SetBranchTarget();
Ian Rogers28ad40d2011-10-27 15:19:26 -0700760 // Ensure exception types are resolved so that they don't need resolution to be delivered,
761 // unresolved exception types will be ignored by exception delivery
Ian Rogers0571d352011-11-03 19:51:38 -0700762 if (iterator.GetHandlerTypeIndex() != DexFile::kDexNoIndex16) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800763 mirror::Class* exception_type = linker->ResolveType(*dex_file_,
764 iterator.GetHandlerTypeIndex(),
Mathieu Chartierbf99f772014-08-23 16:37:27 -0700765 dex_cache_, class_loader_);
Ian Rogers7b078e82014-09-10 14:44:24 -0700766 if (exception_type == nullptr) {
767 DCHECK(self_->IsExceptionPending());
768 self_->ClearException();
Ian Rogers28ad40d2011-10-27 15:19:26 -0700769 }
770 }
jeffhaobdb76512011-09-07 11:43:16 -0700771 }
Ian Rogers0571d352011-11-03 19:51:38 -0700772 handlers_ptr = iterator.EndDataPointer();
jeffhaobdb76512011-09-07 11:43:16 -0700773 }
jeffhaobdb76512011-09-07 11:43:16 -0700774 return true;
775}
776
Ian Rogers776ac1f2012-04-13 23:36:36 -0700777bool MethodVerifier::VerifyInstructions() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700778 const Instruction* inst = Instruction::At(code_item_->insns_);
jeffhaoba5ebb92011-08-25 17:24:37 -0700779
Ian Rogers0c7abda2012-09-19 13:33:42 -0700780 /* 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 -0700781 insn_flags_[0].SetBranchTarget();
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700782 insn_flags_[0].SetCompileTimeInfoPoint();
Ian Rogersd81871c2011-10-03 13:57:23 -0700783
784 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700785 for (uint32_t dex_pc = 0; dex_pc < insns_size;) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700786 if (!VerifyInstruction(inst, dex_pc)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700787 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -0700788 return false;
789 }
790 /* Flag instructions that are garbage collection points */
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700791 // All invoke points are marked as "Throw" points already.
792 // We are relying on this to also count all the invokes as interesting.
Vladimir Marko8b858e12014-11-27 14:52:37 +0000793 if (inst->IsBranch()) {
794 insn_flags_[dex_pc].SetCompileTimeInfoPoint();
795 // The compiler also needs safepoints for fall-through to loop heads.
796 // Such a loop head must be a target of a branch.
797 int32_t offset = 0;
798 bool cond, self_ok;
799 bool target_ok = GetBranchOffset(dex_pc, &offset, &cond, &self_ok);
800 DCHECK(target_ok);
801 insn_flags_[dex_pc + offset].SetCompileTimeInfoPoint();
802 } else if (inst->IsSwitch() || inst->IsThrow()) {
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700803 insn_flags_[dex_pc].SetCompileTimeInfoPoint();
Ian Rogersb8c78592013-07-25 23:52:52 +0000804 } else if (inst->IsReturn()) {
805 insn_flags_[dex_pc].SetCompileTimeInfoPointAndReturn();
Ian Rogersd81871c2011-10-03 13:57:23 -0700806 }
807 dex_pc += inst->SizeInCodeUnits();
808 inst = inst->Next();
809 }
810 return true;
811}
812
Ian Rogers776ac1f2012-04-13 23:36:36 -0700813bool MethodVerifier::VerifyInstruction(const Instruction* inst, uint32_t code_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700814 bool result = true;
815 switch (inst->GetVerifyTypeArgumentA()) {
816 case Instruction::kVerifyRegA:
Ian Rogers29a26482014-05-02 15:27:29 -0700817 result = result && CheckRegisterIndex(inst->VRegA());
Ian Rogersd81871c2011-10-03 13:57:23 -0700818 break;
819 case Instruction::kVerifyRegAWide:
Ian Rogers29a26482014-05-02 15:27:29 -0700820 result = result && CheckWideRegisterIndex(inst->VRegA());
Ian Rogersd81871c2011-10-03 13:57:23 -0700821 break;
822 }
823 switch (inst->GetVerifyTypeArgumentB()) {
824 case Instruction::kVerifyRegB:
Ian Rogers29a26482014-05-02 15:27:29 -0700825 result = result && CheckRegisterIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700826 break;
827 case Instruction::kVerifyRegBField:
Ian Rogers29a26482014-05-02 15:27:29 -0700828 result = result && CheckFieldIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700829 break;
830 case Instruction::kVerifyRegBMethod:
Ian Rogers29a26482014-05-02 15:27:29 -0700831 result = result && CheckMethodIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700832 break;
833 case Instruction::kVerifyRegBNewInstance:
Ian Rogers29a26482014-05-02 15:27:29 -0700834 result = result && CheckNewInstance(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700835 break;
836 case Instruction::kVerifyRegBString:
Ian Rogers29a26482014-05-02 15:27:29 -0700837 result = result && CheckStringIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700838 break;
839 case Instruction::kVerifyRegBType:
Ian Rogers29a26482014-05-02 15:27:29 -0700840 result = result && CheckTypeIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700841 break;
842 case Instruction::kVerifyRegBWide:
Ian Rogers29a26482014-05-02 15:27:29 -0700843 result = result && CheckWideRegisterIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700844 break;
845 }
846 switch (inst->GetVerifyTypeArgumentC()) {
847 case Instruction::kVerifyRegC:
Ian Rogers29a26482014-05-02 15:27:29 -0700848 result = result && CheckRegisterIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700849 break;
850 case Instruction::kVerifyRegCField:
Ian Rogers29a26482014-05-02 15:27:29 -0700851 result = result && CheckFieldIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700852 break;
853 case Instruction::kVerifyRegCNewArray:
Ian Rogers29a26482014-05-02 15:27:29 -0700854 result = result && CheckNewArray(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700855 break;
856 case Instruction::kVerifyRegCType:
Ian Rogers29a26482014-05-02 15:27:29 -0700857 result = result && CheckTypeIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700858 break;
859 case Instruction::kVerifyRegCWide:
Ian Rogers29a26482014-05-02 15:27:29 -0700860 result = result && CheckWideRegisterIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700861 break;
862 }
863 switch (inst->GetVerifyExtraFlags()) {
864 case Instruction::kVerifyArrayData:
865 result = result && CheckArrayData(code_offset);
866 break;
867 case Instruction::kVerifyBranchTarget:
868 result = result && CheckBranchTarget(code_offset);
869 break;
870 case Instruction::kVerifySwitchTargets:
871 result = result && CheckSwitchTargets(code_offset);
872 break;
Andreas Gampec3314312014-06-19 18:13:29 -0700873 case Instruction::kVerifyVarArgNonZero:
874 // Fall-through.
Ian Rogers29a26482014-05-02 15:27:29 -0700875 case Instruction::kVerifyVarArg: {
Taiju Tsuiki29498a22015-04-13 14:21:00 +0900876 // Instructions that can actually return a negative value shouldn't have this flag.
877 uint32_t v_a = dchecked_integral_cast<uint32_t>(inst->VRegA());
878 if ((inst->GetVerifyExtraFlags() == Instruction::kVerifyVarArgNonZero && v_a == 0) ||
879 v_a > Instruction::kMaxVarArgRegs) {
880 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << v_a << ") in "
Andreas Gampec3314312014-06-19 18:13:29 -0700881 "non-range invoke";
882 return false;
883 }
Taiju Tsuiki29498a22015-04-13 14:21:00 +0900884
Ian Rogers29a26482014-05-02 15:27:29 -0700885 uint32_t args[Instruction::kMaxVarArgRegs];
886 inst->GetVarArgs(args);
Taiju Tsuiki29498a22015-04-13 14:21:00 +0900887 result = result && CheckVarArgRegs(v_a, args);
Ian Rogersd81871c2011-10-03 13:57:23 -0700888 break;
Ian Rogers29a26482014-05-02 15:27:29 -0700889 }
Andreas Gampec3314312014-06-19 18:13:29 -0700890 case Instruction::kVerifyVarArgRangeNonZero:
891 // Fall-through.
Ian Rogersd81871c2011-10-03 13:57:23 -0700892 case Instruction::kVerifyVarArgRange:
Andreas Gampec3314312014-06-19 18:13:29 -0700893 if (inst->GetVerifyExtraFlags() == Instruction::kVerifyVarArgRangeNonZero &&
894 inst->VRegA() <= 0) {
895 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << inst->VRegA() << ") in "
896 "range invoke";
897 return false;
898 }
Ian Rogers29a26482014-05-02 15:27:29 -0700899 result = result && CheckVarArgRangeRegs(inst->VRegA(), inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700900 break;
901 case Instruction::kVerifyError:
jeffhaod5347e02012-03-22 17:25:05 -0700902 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected opcode " << inst->Name();
Ian Rogersd81871c2011-10-03 13:57:23 -0700903 result = false;
904 break;
905 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800906 if (inst->GetVerifyIsRuntimeOnly() && Runtime::Current()->IsAotCompiler() && !verify_to_dump_) {
Ian Rogers5fb22a92014-06-13 10:31:28 -0700907 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "opcode only expected at runtime " << inst->Name();
908 result = false;
909 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700910 return result;
911}
912
Ian Rogers7b078e82014-09-10 14:44:24 -0700913inline bool MethodVerifier::CheckRegisterIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700914 if (idx >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700915 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register index out of range (" << idx << " >= "
916 << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700917 return false;
918 }
919 return true;
920}
921
Ian Rogers7b078e82014-09-10 14:44:24 -0700922inline bool MethodVerifier::CheckWideRegisterIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700923 if (idx + 1 >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700924 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "wide register index out of range (" << idx
925 << "+1 >= " << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700926 return false;
927 }
928 return true;
929}
930
Ian Rogers7b078e82014-09-10 14:44:24 -0700931inline bool MethodVerifier::CheckFieldIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700932 if (idx >= dex_file_->GetHeader().field_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700933 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad field index " << idx << " (max "
934 << dex_file_->GetHeader().field_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700935 return false;
936 }
937 return true;
938}
939
Ian Rogers7b078e82014-09-10 14:44:24 -0700940inline bool MethodVerifier::CheckMethodIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700941 if (idx >= dex_file_->GetHeader().method_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700942 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad method index " << idx << " (max "
943 << dex_file_->GetHeader().method_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700944 return false;
945 }
946 return true;
947}
948
Ian Rogers7b078e82014-09-10 14:44:24 -0700949inline bool MethodVerifier::CheckNewInstance(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700950 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700951 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
952 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700953 return false;
954 }
955 // We don't need the actual class, just a pointer to the class name.
Ian Rogers0571d352011-11-03 19:51:38 -0700956 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -0700957 if (descriptor[0] != 'L') {
jeffhaod5347e02012-03-22 17:25:05 -0700958 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "can't call new-instance on type '" << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -0700959 return false;
960 }
961 return true;
962}
963
Ian Rogers7b078e82014-09-10 14:44:24 -0700964inline bool MethodVerifier::CheckStringIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700965 if (idx >= dex_file_->GetHeader().string_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700966 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad string index " << idx << " (max "
967 << dex_file_->GetHeader().string_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700968 return false;
969 }
970 return true;
971}
972
Ian Rogers7b078e82014-09-10 14:44:24 -0700973inline bool MethodVerifier::CheckTypeIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700974 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700975 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
976 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700977 return false;
978 }
979 return true;
980}
981
Ian Rogers776ac1f2012-04-13 23:36:36 -0700982bool MethodVerifier::CheckNewArray(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700983 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700984 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
985 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700986 return false;
987 }
988 int bracket_count = 0;
Ian Rogers0571d352011-11-03 19:51:38 -0700989 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -0700990 const char* cp = descriptor;
991 while (*cp++ == '[') {
992 bracket_count++;
993 }
994 if (bracket_count == 0) {
995 /* The given class must be an array type. */
Brian Carlstrom93c33962013-07-26 10:37:43 -0700996 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
997 << "can't new-array class '" << descriptor << "' (not an array)";
Ian Rogersd81871c2011-10-03 13:57:23 -0700998 return false;
999 } else if (bracket_count > 255) {
1000 /* It is illegal to create an array of more than 255 dimensions. */
Brian Carlstrom93c33962013-07-26 10:37:43 -07001001 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1002 << "can't new-array class '" << descriptor << "' (exceeds limit)";
Ian Rogersd81871c2011-10-03 13:57:23 -07001003 return false;
1004 }
1005 return true;
1006}
1007
Ian Rogers776ac1f2012-04-13 23:36:36 -07001008bool MethodVerifier::CheckArrayData(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001009 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
1010 const uint16_t* insns = code_item_->insns_ + cur_offset;
1011 const uint16_t* array_data;
1012 int32_t array_data_offset;
1013
1014 DCHECK_LT(cur_offset, insn_count);
1015 /* make sure the start of the array data table is in range */
1016 array_data_offset = insns[1] | (((int32_t) insns[2]) << 16);
1017 if ((int32_t) cur_offset + array_data_offset < 0 ||
1018 cur_offset + array_data_offset + 2 >= insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -07001019 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data start: at " << cur_offset
Brian Carlstrom93c33962013-07-26 10:37:43 -07001020 << ", data offset " << array_data_offset
1021 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -07001022 return false;
1023 }
1024 /* offset to array data table is a relative branch-style offset */
1025 array_data = insns + array_data_offset;
1026 /* make sure the table is 32-bit aligned */
Ian Rogersef7d42f2014-01-06 12:55:46 -08001027 if ((reinterpret_cast<uintptr_t>(array_data) & 0x03) != 0) {
jeffhaod5347e02012-03-22 17:25:05 -07001028 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned array data table: at " << cur_offset
1029 << ", data offset " << array_data_offset;
Ian Rogersd81871c2011-10-03 13:57:23 -07001030 return false;
1031 }
1032 uint32_t value_width = array_data[1];
Elliott Hughes398f64b2012-03-26 18:05:48 -07001033 uint32_t value_count = *reinterpret_cast<const uint32_t*>(&array_data[2]);
Ian Rogersd81871c2011-10-03 13:57:23 -07001034 uint32_t table_size = 4 + (value_width * value_count + 1) / 2;
1035 /* make sure the end of the switch is in range */
1036 if (cur_offset + array_data_offset + table_size > insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -07001037 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data end: at " << cur_offset
1038 << ", data offset " << array_data_offset << ", end "
1039 << cur_offset + array_data_offset + table_size
1040 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -07001041 return false;
1042 }
1043 return true;
1044}
1045
Ian Rogers776ac1f2012-04-13 23:36:36 -07001046bool MethodVerifier::CheckBranchTarget(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001047 int32_t offset;
1048 bool isConditional, selfOkay;
1049 if (!GetBranchOffset(cur_offset, &offset, &isConditional, &selfOkay)) {
1050 return false;
1051 }
1052 if (!selfOkay && offset == 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001053 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch offset of zero not allowed at"
1054 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -07001055 return false;
1056 }
Elliott Hughes81ff3182012-03-23 20:35:56 -07001057 // Check for 32-bit overflow. This isn't strictly necessary if we can depend on the runtime
1058 // to have identical "wrap-around" behavior, but it's unwise to depend on that.
Ian Rogersd81871c2011-10-03 13:57:23 -07001059 if (((int64_t) cur_offset + (int64_t) offset) != (int64_t) (cur_offset + offset)) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001060 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch target overflow "
1061 << reinterpret_cast<void*>(cur_offset) << " +" << offset;
Ian Rogersd81871c2011-10-03 13:57:23 -07001062 return false;
1063 }
1064 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
1065 int32_t abs_offset = cur_offset + offset;
Brian Carlstrom93c33962013-07-26 10:37:43 -07001066 if (abs_offset < 0 ||
1067 (uint32_t) abs_offset >= insn_count ||
1068 !insn_flags_[abs_offset].IsOpcode()) {
jeffhaod5347e02012-03-22 17:25:05 -07001069 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid branch target " << offset << " (-> "
Elliott Hughes398f64b2012-03-26 18:05:48 -07001070 << reinterpret_cast<void*>(abs_offset) << ") at "
1071 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -07001072 return false;
1073 }
1074 insn_flags_[abs_offset].SetBranchTarget();
1075 return true;
1076}
1077
Ian Rogers776ac1f2012-04-13 23:36:36 -07001078bool MethodVerifier::GetBranchOffset(uint32_t cur_offset, int32_t* pOffset, bool* pConditional,
Ian Rogersd81871c2011-10-03 13:57:23 -07001079 bool* selfOkay) {
1080 const uint16_t* insns = code_item_->insns_ + cur_offset;
1081 *pConditional = false;
1082 *selfOkay = false;
jeffhaoba5ebb92011-08-25 17:24:37 -07001083 switch (*insns & 0xff) {
1084 case Instruction::GOTO:
1085 *pOffset = ((int16_t) *insns) >> 8;
jeffhaoba5ebb92011-08-25 17:24:37 -07001086 break;
1087 case Instruction::GOTO_32:
1088 *pOffset = insns[1] | (((uint32_t) insns[2]) << 16);
jeffhaoba5ebb92011-08-25 17:24:37 -07001089 *selfOkay = true;
1090 break;
1091 case Instruction::GOTO_16:
1092 *pOffset = (int16_t) insns[1];
jeffhaoba5ebb92011-08-25 17:24:37 -07001093 break;
1094 case Instruction::IF_EQ:
1095 case Instruction::IF_NE:
1096 case Instruction::IF_LT:
1097 case Instruction::IF_GE:
1098 case Instruction::IF_GT:
1099 case Instruction::IF_LE:
1100 case Instruction::IF_EQZ:
1101 case Instruction::IF_NEZ:
1102 case Instruction::IF_LTZ:
1103 case Instruction::IF_GEZ:
1104 case Instruction::IF_GTZ:
1105 case Instruction::IF_LEZ:
1106 *pOffset = (int16_t) insns[1];
1107 *pConditional = true;
jeffhaoba5ebb92011-08-25 17:24:37 -07001108 break;
1109 default:
1110 return false;
jeffhaoba5ebb92011-08-25 17:24:37 -07001111 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001112 return true;
1113}
1114
Ian Rogers776ac1f2012-04-13 23:36:36 -07001115bool MethodVerifier::CheckSwitchTargets(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001116 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001117 DCHECK_LT(cur_offset, insn_count);
Ian Rogersd81871c2011-10-03 13:57:23 -07001118 const uint16_t* insns = code_item_->insns_ + cur_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001119 /* make sure the start of the switch is in range */
Ian Rogersd81871c2011-10-03 13:57:23 -07001120 int32_t switch_offset = insns[1] | ((int32_t) insns[2]) << 16;
Jeff Hao9ccd1512015-03-20 18:11:45 -07001121 if ((int32_t) cur_offset + switch_offset < 0 || cur_offset + switch_offset + 2 > insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -07001122 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch start: at " << cur_offset
Brian Carlstrom93c33962013-07-26 10:37:43 -07001123 << ", switch offset " << switch_offset
1124 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001125 return false;
1126 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001127 /* offset to switch table is a relative branch-style offset */
Ian Rogersd81871c2011-10-03 13:57:23 -07001128 const uint16_t* switch_insns = insns + switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001129 /* make sure the table is 32-bit aligned */
Ian Rogersef7d42f2014-01-06 12:55:46 -08001130 if ((reinterpret_cast<uintptr_t>(switch_insns) & 0x03) != 0) {
jeffhaod5347e02012-03-22 17:25:05 -07001131 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned switch table: at " << cur_offset
1132 << ", switch offset " << switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001133 return false;
1134 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001135 uint32_t switch_count = switch_insns[1];
1136 int32_t keys_offset, targets_offset;
1137 uint16_t expected_signature;
jeffhaoba5ebb92011-08-25 17:24:37 -07001138 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
1139 /* 0=sig, 1=count, 2/3=firstKey */
1140 targets_offset = 4;
1141 keys_offset = -1;
1142 expected_signature = Instruction::kPackedSwitchSignature;
1143 } else {
1144 /* 0=sig, 1=count, 2..count*2 = keys */
1145 keys_offset = 2;
1146 targets_offset = 2 + 2 * switch_count;
1147 expected_signature = Instruction::kSparseSwitchSignature;
1148 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001149 uint32_t table_size = targets_offset + switch_count * 2;
jeffhaoba5ebb92011-08-25 17:24:37 -07001150 if (switch_insns[0] != expected_signature) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001151 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1152 << StringPrintf("wrong signature for switch table (%x, wanted %x)",
1153 switch_insns[0], expected_signature);
jeffhaoba5ebb92011-08-25 17:24:37 -07001154 return false;
1155 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001156 /* make sure the end of the switch is in range */
1157 if (cur_offset + switch_offset + table_size > (uint32_t) insn_count) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001158 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch end: at " << cur_offset
1159 << ", switch offset " << switch_offset
1160 << ", end " << (cur_offset + switch_offset + table_size)
jeffhaod5347e02012-03-22 17:25:05 -07001161 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001162 return false;
1163 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001164 /* for a sparse switch, verify the keys are in ascending order */
1165 if (keys_offset > 0 && switch_count > 1) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001166 int32_t last_key = switch_insns[keys_offset] | (switch_insns[keys_offset + 1] << 16);
1167 for (uint32_t targ = 1; targ < switch_count; targ++) {
jeffhaoba5ebb92011-08-25 17:24:37 -07001168 int32_t key = (int32_t) switch_insns[keys_offset + targ * 2] |
1169 (int32_t) (switch_insns[keys_offset + targ * 2 + 1] << 16);
1170 if (key <= last_key) {
jeffhaod5347e02012-03-22 17:25:05 -07001171 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid packed switch: last key=" << last_key
1172 << ", this=" << key;
jeffhaoba5ebb92011-08-25 17:24:37 -07001173 return false;
1174 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001175 last_key = key;
1176 }
1177 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001178 /* verify each switch target */
Ian Rogersd81871c2011-10-03 13:57:23 -07001179 for (uint32_t targ = 0; targ < switch_count; targ++) {
1180 int32_t offset = (int32_t) switch_insns[targets_offset + targ * 2] |
1181 (int32_t) (switch_insns[targets_offset + targ * 2 + 1] << 16);
1182 int32_t abs_offset = cur_offset + offset;
Brian Carlstrom93c33962013-07-26 10:37:43 -07001183 if (abs_offset < 0 ||
1184 abs_offset >= (int32_t) insn_count ||
1185 !insn_flags_[abs_offset].IsOpcode()) {
1186 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch target " << offset
1187 << " (-> " << reinterpret_cast<void*>(abs_offset) << ") at "
1188 << reinterpret_cast<void*>(cur_offset)
1189 << "[" << targ << "]";
jeffhaoba5ebb92011-08-25 17:24:37 -07001190 return false;
1191 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001192 insn_flags_[abs_offset].SetBranchTarget();
1193 }
1194 return true;
1195}
1196
Ian Rogers776ac1f2012-04-13 23:36:36 -07001197bool MethodVerifier::CheckVarArgRegs(uint32_t vA, uint32_t arg[]) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001198 uint16_t registers_size = code_item_->registers_size_;
1199 for (uint32_t idx = 0; idx < vA; idx++) {
jeffhao457cc512012-02-02 16:55:13 -08001200 if (arg[idx] >= registers_size) {
jeffhaod5347e02012-03-22 17:25:05 -07001201 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index (" << arg[idx]
1202 << ") in non-range invoke (>= " << registers_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001203 return false;
1204 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001205 }
1206
1207 return true;
1208}
1209
Ian Rogers776ac1f2012-04-13 23:36:36 -07001210bool MethodVerifier::CheckVarArgRangeRegs(uint32_t vA, uint32_t vC) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001211 uint16_t registers_size = code_item_->registers_size_;
1212 // vA/vC are unsigned 8-bit/16-bit quantities for /range instructions, so there's no risk of
1213 // integer overflow when adding them here.
1214 if (vA + vC > registers_size) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001215 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index " << vA << "+" << vC
1216 << " in range invoke (> " << registers_size << ")";
jeffhaoba5ebb92011-08-25 17:24:37 -07001217 return false;
1218 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001219 return true;
1220}
1221
Ian Rogers776ac1f2012-04-13 23:36:36 -07001222bool MethodVerifier::VerifyCodeFlow() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001223 uint16_t registers_size = code_item_->registers_size_;
1224 uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaobdb76512011-09-07 11:43:16 -07001225
Ian Rogersd81871c2011-10-03 13:57:23 -07001226 /* Create and initialize table holding register status */
Brian Carlstrom93c33962013-07-26 10:37:43 -07001227 reg_table_.Init(kTrackCompilerInterestPoints,
1228 insn_flags_.get(),
1229 insns_size,
1230 registers_size,
1231 this);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07001232
jeffhaobdb76512011-09-07 11:43:16 -07001233
Ian Rogersd0fbd852013-09-24 18:17:04 -07001234 work_line_.reset(RegisterLine::Create(registers_size, this));
1235 saved_line_.reset(RegisterLine::Create(registers_size, this));
jeffhaobdb76512011-09-07 11:43:16 -07001236
Ian Rogersd81871c2011-10-03 13:57:23 -07001237 /* Initialize register types of method arguments. */
1238 if (!SetTypesFromSignature()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001239 DCHECK_NE(failures_.size(), 0U);
1240 std::string prepend("Bad signature in ");
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001241 prepend += PrettyMethod(dex_method_idx_, *dex_file_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001242 PrependToLastFailMessage(prepend);
Ian Rogersd81871c2011-10-03 13:57:23 -07001243 return false;
1244 }
Andreas Gamped5ad72f2015-06-26 17:33:47 -07001245 // We may have a runtime failure here, clear.
1246 have_pending_runtime_throw_failure_ = false;
1247
Ian Rogersd81871c2011-10-03 13:57:23 -07001248 /* Perform code flow verification. */
1249 if (!CodeFlowVerifyMethod()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001250 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -07001251 return false;
jeffhaobdb76512011-09-07 11:43:16 -07001252 }
jeffhaobdb76512011-09-07 11:43:16 -07001253 return true;
1254}
1255
Ian Rogersad0b3a32012-04-16 14:50:24 -07001256std::ostream& MethodVerifier::DumpFailures(std::ostream& os) {
1257 DCHECK_EQ(failures_.size(), failure_messages_.size());
Jeff Hao4137f482013-11-22 11:44:57 -08001258 for (size_t i = 0; i < failures_.size(); ++i) {
1259 os << failure_messages_[i]->str() << "\n";
Ian Rogersad0b3a32012-04-16 14:50:24 -07001260 }
1261 return os;
1262}
1263
Ian Rogers776ac1f2012-04-13 23:36:36 -07001264void MethodVerifier::Dump(std::ostream& os) {
Ian Rogers7b078e82014-09-10 14:44:24 -07001265 if (code_item_ == nullptr) {
Elliott Hughesc073b072012-05-24 19:29:17 -07001266 os << "Native method\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001267 return;
jeffhaobdb76512011-09-07 11:43:16 -07001268 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001269 {
1270 os << "Register Types:\n";
1271 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
1272 std::ostream indent_os(&indent_filter);
1273 reg_types_.Dump(indent_os);
1274 }
Ian Rogersb4903572012-10-11 11:52:56 -07001275 os << "Dumping instructions and register lines:\n";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001276 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
1277 std::ostream indent_os(&indent_filter);
Ian Rogersd81871c2011-10-03 13:57:23 -07001278 const Instruction* inst = Instruction::At(code_item_->insns_);
1279 for (size_t dex_pc = 0; dex_pc < code_item_->insns_size_in_code_units_;
Ian Rogers7b078e82014-09-10 14:44:24 -07001280 dex_pc += inst->SizeInCodeUnits()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001281 RegisterLine* reg_line = reg_table_.GetLine(dex_pc);
Ian Rogers7b078e82014-09-10 14:44:24 -07001282 if (reg_line != nullptr) {
1283 indent_os << reg_line->Dump(this) << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07001284 }
Ian Rogers7b3ddd22013-02-21 15:19:52 -08001285 indent_os << StringPrintf("0x%04zx", dex_pc) << ": " << insn_flags_[dex_pc].ToString() << " ";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001286 const bool kDumpHexOfInstruction = false;
1287 if (kDumpHexOfInstruction) {
1288 indent_os << inst->DumpHex(5) << " ";
1289 }
1290 indent_os << inst->DumpString(dex_file_) << "\n";
jeffhaoba5ebb92011-08-25 17:24:37 -07001291 inst = inst->Next();
1292 }
jeffhaobdb76512011-09-07 11:43:16 -07001293}
1294
Ian Rogersd81871c2011-10-03 13:57:23 -07001295static bool IsPrimitiveDescriptor(char descriptor) {
1296 switch (descriptor) {
jeffhaobdb76512011-09-07 11:43:16 -07001297 case 'I':
1298 case 'C':
1299 case 'S':
1300 case 'B':
1301 case 'Z':
jeffhaobdb76512011-09-07 11:43:16 -07001302 case 'F':
1303 case 'D':
1304 case 'J':
Ian Rogersd81871c2011-10-03 13:57:23 -07001305 return true;
jeffhaobdb76512011-09-07 11:43:16 -07001306 default:
1307 return false;
1308 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001309}
1310
Ian Rogers776ac1f2012-04-13 23:36:36 -07001311bool MethodVerifier::SetTypesFromSignature() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001312 RegisterLine* reg_line = reg_table_.GetLine(0);
Andreas Gampeef0b1a12015-06-19 20:37:46 -07001313
1314 // Should have been verified earlier.
1315 DCHECK_GE(code_item_->registers_size_, code_item_->ins_size_);
1316
1317 uint32_t arg_start = code_item_->registers_size_ - code_item_->ins_size_;
Ian Rogersd81871c2011-10-03 13:57:23 -07001318 size_t expected_args = code_item_->ins_size_; /* long/double count as two */
jeffhaobdb76512011-09-07 11:43:16 -07001319
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001320 // Include the "this" pointer.
Ian Rogersd81871c2011-10-03 13:57:23 -07001321 size_t cur_arg = 0;
Ian Rogersad0b3a32012-04-16 14:50:24 -07001322 if (!IsStatic()) {
Andreas Gampeef0b1a12015-06-19 20:37:46 -07001323 if (expected_args == 0) {
1324 // Expect at least a receiver.
1325 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected 0 args, but method is not static";
1326 return false;
1327 }
1328
Ian Rogersd81871c2011-10-03 13:57:23 -07001329 // If this is a constructor for a class other than java.lang.Object, mark the first ("this")
1330 // argument as uninitialized. This restricts field access until the superclass constructor is
1331 // called.
Ian Rogersd8f69b02014-09-10 21:43:52 +00001332 const RegType& declaring_class = GetDeclaringClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07001333 if (IsConstructor() && !declaring_class.IsJavaLangObject()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07001334 reg_line->SetRegisterType(this, arg_start + cur_arg,
Ian Rogersd81871c2011-10-03 13:57:23 -07001335 reg_types_.UninitializedThisArgument(declaring_class));
1336 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07001337 reg_line->SetRegisterType(this, arg_start + cur_arg, declaring_class);
jeffhaobdb76512011-09-07 11:43:16 -07001338 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001339 cur_arg++;
jeffhaobdb76512011-09-07 11:43:16 -07001340 }
1341
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001342 const DexFile::ProtoId& proto_id =
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001343 dex_file_->GetMethodPrototype(dex_file_->GetMethodId(dex_method_idx_));
Ian Rogers0571d352011-11-03 19:51:38 -07001344 DexFileParameterIterator iterator(*dex_file_, proto_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07001345
1346 for (; iterator.HasNext(); iterator.Next()) {
1347 const char* descriptor = iterator.GetDescriptor();
Ian Rogers7b078e82014-09-10 14:44:24 -07001348 if (descriptor == nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001349 LOG(FATAL) << "Null descriptor";
1350 }
1351 if (cur_arg >= expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07001352 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1353 << " args, found more (" << descriptor << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001354 return false;
1355 }
1356 switch (descriptor[0]) {
1357 case 'L':
1358 case '[':
1359 // We assume that reference arguments are initialized. The only way it could be otherwise
1360 // (assuming the caller was verified) is if the current method is <init>, but in that case
1361 // it's effectively considered initialized the instant we reach here (in the sense that we
1362 // can return without doing anything or call virtual methods).
1363 {
Ian Rogersd8f69b02014-09-10 21:43:52 +00001364 const RegType& reg_type = ResolveClassAndCheckAccess(iterator.GetTypeIdx());
Sebastien Hertz2ed76f92014-04-22 17:11:08 +02001365 if (!reg_type.IsNonZeroReferenceTypes()) {
1366 DCHECK(HasFailures());
1367 return false;
1368 }
Ian Rogers7b078e82014-09-10 14:44:24 -07001369 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001370 }
1371 break;
1372 case 'Z':
Ian Rogers7b078e82014-09-10 14:44:24 -07001373 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_types_.Boolean());
Ian Rogersd81871c2011-10-03 13:57:23 -07001374 break;
1375 case 'C':
Ian Rogers7b078e82014-09-10 14:44:24 -07001376 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_types_.Char());
Ian Rogersd81871c2011-10-03 13:57:23 -07001377 break;
1378 case 'B':
Ian Rogers7b078e82014-09-10 14:44:24 -07001379 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_types_.Byte());
Ian Rogersd81871c2011-10-03 13:57:23 -07001380 break;
1381 case 'I':
Ian Rogers7b078e82014-09-10 14:44:24 -07001382 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_types_.Integer());
Ian Rogersd81871c2011-10-03 13:57:23 -07001383 break;
1384 case 'S':
Ian Rogers7b078e82014-09-10 14:44:24 -07001385 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_types_.Short());
Ian Rogersd81871c2011-10-03 13:57:23 -07001386 break;
1387 case 'F':
Ian Rogers7b078e82014-09-10 14:44:24 -07001388 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_types_.Float());
Ian Rogersd81871c2011-10-03 13:57:23 -07001389 break;
1390 case 'J':
1391 case 'D': {
Andreas Gampe77cd4d62014-06-19 17:29:48 -07001392 if (cur_arg + 1 >= expected_args) {
1393 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1394 << " args, found more (" << descriptor << ")";
1395 return false;
1396 }
1397
Ian Rogers7b078e82014-09-10 14:44:24 -07001398 const RegType* lo_half;
1399 const RegType* hi_half;
1400 if (descriptor[0] == 'J') {
1401 lo_half = &reg_types_.LongLo();
1402 hi_half = &reg_types_.LongHi();
1403 } else {
1404 lo_half = &reg_types_.DoubleLo();
1405 hi_half = &reg_types_.DoubleHi();
1406 }
1407 reg_line->SetRegisterTypeWide(this, arg_start + cur_arg, *lo_half, *hi_half);
Ian Rogersd81871c2011-10-03 13:57:23 -07001408 cur_arg++;
1409 break;
1410 }
1411 default:
Brian Carlstrom93c33962013-07-26 10:37:43 -07001412 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected signature type char '"
1413 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001414 return false;
1415 }
1416 cur_arg++;
1417 }
1418 if (cur_arg != expected_args) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001419 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1420 << " arguments, found " << cur_arg;
Ian Rogersd81871c2011-10-03 13:57:23 -07001421 return false;
1422 }
1423 const char* descriptor = dex_file_->GetReturnTypeDescriptor(proto_id);
1424 // Validate return type. We don't do the type lookup; just want to make sure that it has the right
1425 // format. Only major difference from the method argument format is that 'V' is supported.
1426 bool result;
1427 if (IsPrimitiveDescriptor(descriptor[0]) || descriptor[0] == 'V') {
1428 result = descriptor[1] == '\0';
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001429 } else if (descriptor[0] == '[') { // single/multi-dimensional array of object/primitive
Ian Rogersd81871c2011-10-03 13:57:23 -07001430 size_t i = 0;
1431 do {
1432 i++;
1433 } while (descriptor[i] == '['); // process leading [
1434 if (descriptor[i] == 'L') { // object array
1435 do {
1436 i++; // find closing ;
1437 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1438 result = descriptor[i] == ';';
1439 } else { // primitive array
1440 result = IsPrimitiveDescriptor(descriptor[i]) && descriptor[i + 1] == '\0';
1441 }
1442 } else if (descriptor[0] == 'L') {
1443 // could be more thorough here, but shouldn't be required
1444 size_t i = 0;
1445 do {
1446 i++;
1447 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1448 result = descriptor[i] == ';';
1449 } else {
1450 result = false;
1451 }
1452 if (!result) {
jeffhaod5347e02012-03-22 17:25:05 -07001453 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected char in return type descriptor '"
1454 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001455 }
1456 return result;
jeffhaobdb76512011-09-07 11:43:16 -07001457}
1458
Ian Rogers776ac1f2012-04-13 23:36:36 -07001459bool MethodVerifier::CodeFlowVerifyMethod() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001460 const uint16_t* insns = code_item_->insns_;
1461 const uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaoba5ebb92011-08-25 17:24:37 -07001462
jeffhaobdb76512011-09-07 11:43:16 -07001463 /* Begin by marking the first instruction as "changed". */
Ian Rogersd81871c2011-10-03 13:57:23 -07001464 insn_flags_[0].SetChanged();
1465 uint32_t start_guess = 0;
jeffhaoba5ebb92011-08-25 17:24:37 -07001466
jeffhaobdb76512011-09-07 11:43:16 -07001467 /* Continue until no instructions are marked "changed". */
1468 while (true) {
Mathieu Chartier4306ef82014-12-19 18:41:47 -08001469 if (allow_thread_suspension_) {
1470 self_->AllowThreadSuspension();
1471 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001472 // Find the first marked one. Use "start_guess" as a way to find one quickly.
1473 uint32_t insn_idx = start_guess;
1474 for (; insn_idx < insns_size; insn_idx++) {
1475 if (insn_flags_[insn_idx].IsChanged())
jeffhaobdb76512011-09-07 11:43:16 -07001476 break;
1477 }
jeffhaobdb76512011-09-07 11:43:16 -07001478 if (insn_idx == insns_size) {
1479 if (start_guess != 0) {
1480 /* try again, starting from the top */
1481 start_guess = 0;
1482 continue;
1483 } else {
1484 /* all flags are clear */
1485 break;
1486 }
1487 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001488 // We carry the working set of registers from instruction to instruction. If this address can
1489 // be the target of a branch (or throw) instruction, or if we're skipping around chasing
1490 // "changed" flags, we need to load the set of registers from the table.
1491 // Because we always prefer to continue on to the next instruction, we should never have a
1492 // situation where we have a stray "changed" flag set on an instruction that isn't a branch
1493 // target.
1494 work_insn_idx_ = insn_idx;
1495 if (insn_flags_[insn_idx].IsBranchTarget()) {
1496 work_line_->CopyFromLine(reg_table_.GetLine(insn_idx));
Ian Rogersebbdd872014-07-07 23:53:08 -07001497 } else if (kIsDebugBuild) {
jeffhaobdb76512011-09-07 11:43:16 -07001498 /*
1499 * Sanity check: retrieve the stored register line (assuming
1500 * a full table) and make sure it actually matches.
1501 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001502 RegisterLine* register_line = reg_table_.GetLine(insn_idx);
Ian Rogers7b078e82014-09-10 14:44:24 -07001503 if (register_line != nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001504 if (work_line_->CompareLine(register_line) != 0) {
1505 Dump(std::cout);
1506 std::cout << info_messages_.str();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001507 LOG(FATAL) << "work_line diverged in " << PrettyMethod(dex_method_idx_, *dex_file_)
Elliott Hughesc073b072012-05-24 19:29:17 -07001508 << "@" << reinterpret_cast<void*>(work_insn_idx_) << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07001509 << " work_line=" << work_line_->Dump(this) << "\n"
1510 << " expected=" << register_line->Dump(this);
Ian Rogersd81871c2011-10-03 13:57:23 -07001511 }
jeffhaobdb76512011-09-07 11:43:16 -07001512 }
jeffhaobdb76512011-09-07 11:43:16 -07001513 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001514 if (!CodeFlowVerifyInstruction(&start_guess)) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001515 std::string prepend(PrettyMethod(dex_method_idx_, *dex_file_));
Ian Rogersad0b3a32012-04-16 14:50:24 -07001516 prepend += " failed to verify: ";
1517 PrependToLastFailMessage(prepend);
jeffhaoba5ebb92011-08-25 17:24:37 -07001518 return false;
1519 }
jeffhaobdb76512011-09-07 11:43:16 -07001520 /* Clear "changed" and mark as visited. */
Ian Rogersd81871c2011-10-03 13:57:23 -07001521 insn_flags_[insn_idx].SetVisited();
1522 insn_flags_[insn_idx].ClearChanged();
jeffhaobdb76512011-09-07 11:43:16 -07001523 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001524
Ian Rogers1c849e52012-06-28 14:00:33 -07001525 if (gDebugVerify) {
jeffhaobdb76512011-09-07 11:43:16 -07001526 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001527 * Scan for dead code. There's nothing "evil" about dead code
jeffhaobdb76512011-09-07 11:43:16 -07001528 * (besides the wasted space), but it indicates a flaw somewhere
1529 * down the line, possibly in the verifier.
1530 *
1531 * If we've substituted "always throw" instructions into the stream,
1532 * we are almost certainly going to have some dead code.
1533 */
1534 int dead_start = -1;
Ian Rogersd81871c2011-10-03 13:57:23 -07001535 uint32_t insn_idx = 0;
Ian Rogers7b078e82014-09-10 14:44:24 -07001536 for (; insn_idx < insns_size;
1537 insn_idx += Instruction::At(code_item_->insns_ + insn_idx)->SizeInCodeUnits()) {
jeffhaobdb76512011-09-07 11:43:16 -07001538 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001539 * Switch-statement data doesn't get "visited" by scanner. It
jeffhaobdb76512011-09-07 11:43:16 -07001540 * may or may not be preceded by a padding NOP (for alignment).
1541 */
1542 if (insns[insn_idx] == Instruction::kPackedSwitchSignature ||
1543 insns[insn_idx] == Instruction::kSparseSwitchSignature ||
1544 insns[insn_idx] == Instruction::kArrayDataSignature ||
Elliott Hughes380aaa72012-07-09 14:33:15 -07001545 (insns[insn_idx] == Instruction::NOP && (insn_idx + 1 < insns_size) &&
jeffhaobdb76512011-09-07 11:43:16 -07001546 (insns[insn_idx + 1] == Instruction::kPackedSwitchSignature ||
1547 insns[insn_idx + 1] == Instruction::kSparseSwitchSignature ||
1548 insns[insn_idx + 1] == Instruction::kArrayDataSignature))) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001549 insn_flags_[insn_idx].SetVisited();
jeffhaobdb76512011-09-07 11:43:16 -07001550 }
1551
Ian Rogersd81871c2011-10-03 13:57:23 -07001552 if (!insn_flags_[insn_idx].IsVisited()) {
jeffhaobdb76512011-09-07 11:43:16 -07001553 if (dead_start < 0)
1554 dead_start = insn_idx;
1555 } else if (dead_start >= 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001556 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start)
1557 << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaobdb76512011-09-07 11:43:16 -07001558 dead_start = -1;
1559 }
1560 }
1561 if (dead_start >= 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001562 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start)
1563 << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaoba5ebb92011-08-25 17:24:37 -07001564 }
Ian Rogersc9e463c2013-06-05 16:52:26 -07001565 // To dump the state of the verify after a method, do something like:
1566 // if (PrettyMethod(dex_method_idx_, *dex_file_) ==
1567 // "boolean java.lang.String.equals(java.lang.Object)") {
1568 // LOG(INFO) << info_messages_.str();
1569 // }
jeffhaoba5ebb92011-08-25 17:24:37 -07001570 }
jeffhaobdb76512011-09-07 11:43:16 -07001571 return true;
1572}
1573
Andreas Gampe68df3202015-06-22 11:35:46 -07001574// Returns the index of the first final instance field of the given class, or kDexNoIndex if there
1575// is no such field.
1576static uint32_t GetFirstFinalInstanceFieldIndex(const DexFile& dex_file, uint16_t type_idx) {
1577 const DexFile::ClassDef* class_def = dex_file.FindClassDef(type_idx);
1578 DCHECK(class_def != nullptr);
1579 const uint8_t* class_data = dex_file.GetClassData(*class_def);
1580 DCHECK(class_data != nullptr);
1581 ClassDataItemIterator it(dex_file, class_data);
1582 // Skip static fields.
1583 while (it.HasNextStaticField()) {
1584 it.Next();
1585 }
1586 while (it.HasNextInstanceField()) {
1587 if ((it.GetFieldAccessFlags() & kAccFinal) != 0) {
1588 return it.GetMemberIndex();
1589 }
1590 it.Next();
1591 }
1592 return DexFile::kDexNoIndex;
1593}
1594
Ian Rogers776ac1f2012-04-13 23:36:36 -07001595bool MethodVerifier::CodeFlowVerifyInstruction(uint32_t* start_guess) {
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001596 // If we're doing FindLocksAtDexPc, check whether we're at the dex pc we care about.
1597 // We want the state _before_ the instruction, for the case where the dex pc we're
1598 // interested in is itself a monitor-enter instruction (which is a likely place
1599 // for a thread to be suspended).
Ian Rogers7b078e82014-09-10 14:44:24 -07001600 if (monitor_enter_dex_pcs_ != nullptr && work_insn_idx_ == interesting_dex_pc_) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001601 monitor_enter_dex_pcs_->clear(); // The new work line is more accurate than the previous one.
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001602 for (size_t i = 0; i < work_line_->GetMonitorEnterCount(); ++i) {
1603 monitor_enter_dex_pcs_->push_back(work_line_->GetMonitorEnterDexPc(i));
1604 }
1605 }
1606
jeffhaobdb76512011-09-07 11:43:16 -07001607 /*
1608 * Once we finish decoding the instruction, we need to figure out where
jeffhaod1f0fde2011-09-08 17:25:33 -07001609 * we can go from here. There are three possible ways to transfer
jeffhaobdb76512011-09-07 11:43:16 -07001610 * control to another statement:
1611 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001612 * (1) Continue to the next instruction. Applies to all but
jeffhaobdb76512011-09-07 11:43:16 -07001613 * unconditional branches, method returns, and exception throws.
jeffhaod1f0fde2011-09-08 17:25:33 -07001614 * (2) Branch to one or more possible locations. Applies to branches
jeffhaobdb76512011-09-07 11:43:16 -07001615 * and switch statements.
jeffhaod1f0fde2011-09-08 17:25:33 -07001616 * (3) Exception handlers. Applies to any instruction that can
jeffhaobdb76512011-09-07 11:43:16 -07001617 * throw an exception that is handled by an encompassing "try"
1618 * block.
1619 *
1620 * We can also return, in which case there is no successor instruction
1621 * from this point.
1622 *
Elliott Hughesadb8c672012-03-06 16:49:32 -08001623 * The behavior can be determined from the opcode flags.
jeffhaobdb76512011-09-07 11:43:16 -07001624 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001625 const uint16_t* insns = code_item_->insns_ + work_insn_idx_;
1626 const Instruction* inst = Instruction::At(insns);
Ian Rogersa75a0132012-09-28 11:41:42 -07001627 int opcode_flags = Instruction::FlagsOf(inst->Opcode());
jeffhaobdb76512011-09-07 11:43:16 -07001628
jeffhaobdb76512011-09-07 11:43:16 -07001629 int32_t branch_target = 0;
jeffhaobdb76512011-09-07 11:43:16 -07001630 bool just_set_result = false;
Ian Rogers2c8a8572011-10-24 17:11:36 -07001631 if (gDebugVerify) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001632 // Generate processing back trace to debug verifier
Elliott Hughesc073b072012-05-24 19:29:17 -07001633 LogVerifyInfo() << "Processing " << inst->DumpString(dex_file_) << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07001634 << work_line_->Dump(this) << "\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001635 }
jeffhaobdb76512011-09-07 11:43:16 -07001636
1637 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001638 * Make a copy of the previous register state. If the instruction
jeffhaobdb76512011-09-07 11:43:16 -07001639 * can throw an exception, we will copy/merge this into the "catch"
1640 * address rather than work_line, because we don't want the result
1641 * from the "successful" code path (e.g. a check-cast that "improves"
1642 * a type) to be visible to the exception handler.
1643 */
Ian Rogers776ac1f2012-04-13 23:36:36 -07001644 if ((opcode_flags & Instruction::kThrow) != 0 && CurrentInsnFlags()->IsInTry()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001645 saved_line_->CopyFromLine(work_line_.get());
Ian Rogers1ff3c982014-08-12 02:30:58 -07001646 } else if (kIsDebugBuild) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001647 saved_line_->FillWithGarbage();
jeffhaobdb76512011-09-07 11:43:16 -07001648 }
Andreas Gamped12e7822015-06-25 10:26:40 -07001649 DCHECK(!have_pending_runtime_throw_failure_); // Per-instruction flag, should not be set here.
jeffhaobdb76512011-09-07 11:43:16 -07001650
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07001651
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001652 // We need to ensure the work line is consistent while performing validation. When we spot a
1653 // peephole pattern we compute a new line for either the fallthrough instruction or the
1654 // branch target.
Ian Rogers700a4022014-05-19 16:49:03 -07001655 std::unique_ptr<RegisterLine> branch_line;
1656 std::unique_ptr<RegisterLine> fallthrough_line;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001657
Stephen Kyle7e541c92014-12-17 17:10:02 +00001658 /*
1659 * If we are in a constructor, and we currently have an UninitializedThis type
1660 * in a register somewhere, we need to make sure it isn't overwritten.
1661 */
1662 bool track_uninitialized_this = false;
1663 size_t uninitialized_this_loc = 0;
1664 if (IsConstructor()) {
1665 track_uninitialized_this = work_line_->GetUninitializedThisLoc(this, &uninitialized_this_loc);
1666 }
1667
Sebastien Hertz5243e912013-05-21 10:55:07 +02001668 switch (inst->Opcode()) {
jeffhaobdb76512011-09-07 11:43:16 -07001669 case Instruction::NOP:
1670 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001671 * A "pure" NOP has no effect on anything. Data tables start with
jeffhaobdb76512011-09-07 11:43:16 -07001672 * a signature that looks like a NOP; if we see one of these in
1673 * the course of executing code then we have a problem.
1674 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001675 if (inst->VRegA_10x() != 0) {
jeffhaod5347e02012-03-22 17:25:05 -07001676 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "encountered data table in instruction stream";
jeffhaobdb76512011-09-07 11:43:16 -07001677 }
1678 break;
1679
1680 case Instruction::MOVE:
Ian Rogers7b078e82014-09-10 14:44:24 -07001681 work_line_->CopyRegister1(this, inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategory1nr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001682 break;
jeffhaobdb76512011-09-07 11:43:16 -07001683 case Instruction::MOVE_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001684 work_line_->CopyRegister1(this, inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategory1nr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001685 break;
jeffhaobdb76512011-09-07 11:43:16 -07001686 case Instruction::MOVE_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001687 work_line_->CopyRegister1(this, inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategory1nr);
jeffhaobdb76512011-09-07 11:43:16 -07001688 break;
1689 case Instruction::MOVE_WIDE:
Ian Rogers7b078e82014-09-10 14:44:24 -07001690 work_line_->CopyRegister2(this, inst->VRegA_12x(), inst->VRegB_12x());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001691 break;
jeffhaobdb76512011-09-07 11:43:16 -07001692 case Instruction::MOVE_WIDE_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001693 work_line_->CopyRegister2(this, inst->VRegA_22x(), inst->VRegB_22x());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001694 break;
jeffhaobdb76512011-09-07 11:43:16 -07001695 case Instruction::MOVE_WIDE_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001696 work_line_->CopyRegister2(this, inst->VRegA_32x(), inst->VRegB_32x());
jeffhaobdb76512011-09-07 11:43:16 -07001697 break;
1698 case Instruction::MOVE_OBJECT:
Ian Rogers7b078e82014-09-10 14:44:24 -07001699 work_line_->CopyRegister1(this, inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategoryRef);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001700 break;
jeffhaobdb76512011-09-07 11:43:16 -07001701 case Instruction::MOVE_OBJECT_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001702 work_line_->CopyRegister1(this, inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategoryRef);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001703 break;
jeffhaobdb76512011-09-07 11:43:16 -07001704 case Instruction::MOVE_OBJECT_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001705 work_line_->CopyRegister1(this, inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategoryRef);
jeffhaobdb76512011-09-07 11:43:16 -07001706 break;
1707
1708 /*
1709 * The move-result instructions copy data out of a "pseudo-register"
jeffhaod1f0fde2011-09-08 17:25:33 -07001710 * with the results from the last method invocation. In practice we
jeffhaobdb76512011-09-07 11:43:16 -07001711 * might want to hold the result in an actual CPU register, so the
1712 * Dalvik spec requires that these only appear immediately after an
1713 * invoke or filled-new-array.
1714 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001715 * These calls invalidate the "result" register. (This is now
jeffhaobdb76512011-09-07 11:43:16 -07001716 * redundant with the reset done below, but it can make the debug info
1717 * easier to read in some cases.)
1718 */
1719 case Instruction::MOVE_RESULT:
Ian Rogers7b078e82014-09-10 14:44:24 -07001720 work_line_->CopyResultRegister1(this, inst->VRegA_11x(), false);
jeffhaobdb76512011-09-07 11:43:16 -07001721 break;
1722 case Instruction::MOVE_RESULT_WIDE:
Ian Rogers7b078e82014-09-10 14:44:24 -07001723 work_line_->CopyResultRegister2(this, inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07001724 break;
1725 case Instruction::MOVE_RESULT_OBJECT:
Ian Rogers7b078e82014-09-10 14:44:24 -07001726 work_line_->CopyResultRegister1(this, inst->VRegA_11x(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001727 break;
1728
Ian Rogersd81871c2011-10-03 13:57:23 -07001729 case Instruction::MOVE_EXCEPTION: {
Sebastien Hertz270a0e12015-01-16 19:49:09 +01001730 // We do not allow MOVE_EXCEPTION as the first instruction in a method. This is a simple case
1731 // where one entrypoint to the catch block is not actually an exception path.
1732 if (work_insn_idx_ == 0) {
1733 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "move-exception at pc 0x0";
1734 break;
1735 }
jeffhaobdb76512011-09-07 11:43:16 -07001736 /*
jeffhao60f83e32012-02-13 17:16:30 -08001737 * This statement can only appear as the first instruction in an exception handler. We verify
1738 * that as part of extracting the exception type from the catch block list.
jeffhaobdb76512011-09-07 11:43:16 -07001739 */
Ian Rogersd8f69b02014-09-10 21:43:52 +00001740 const RegType& res_type = GetCaughtExceptionType();
Ian Rogers7b078e82014-09-10 14:44:24 -07001741 work_line_->SetRegisterType(this, inst->VRegA_11x(), res_type);
jeffhaobdb76512011-09-07 11:43:16 -07001742 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001743 }
jeffhaobdb76512011-09-07 11:43:16 -07001744 case Instruction::RETURN_VOID:
Ian Rogers7b078e82014-09-10 14:44:24 -07001745 if (!IsConstructor() || work_line_->CheckConstructorReturn(this)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001746 if (!GetMethodReturnType().IsConflict()) {
jeffhaod5347e02012-03-22 17:25:05 -07001747 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001748 }
jeffhaobdb76512011-09-07 11:43:16 -07001749 }
1750 break;
1751 case Instruction::RETURN:
Ian Rogers7b078e82014-09-10 14:44:24 -07001752 if (!IsConstructor() || work_line_->CheckConstructorReturn(this)) {
jeffhaobdb76512011-09-07 11:43:16 -07001753 /* check the method signature */
Ian Rogersd8f69b02014-09-10 21:43:52 +00001754 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07001755 if (!return_type.IsCategory1Types()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001756 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected non-category 1 return type "
1757 << return_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001758 } else {
1759 // Compilers may generate synthetic functions that write byte values into boolean fields.
1760 // Also, it may use integer values for boolean, byte, short, and character return types.
Sebastien Hertz5243e912013-05-21 10:55:07 +02001761 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07001762 const RegType& src_type = work_line_->GetRegisterType(this, vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07001763 bool use_src = ((return_type.IsBoolean() && src_type.IsByte()) ||
1764 ((return_type.IsBoolean() || return_type.IsByte() ||
1765 return_type.IsShort() || return_type.IsChar()) &&
1766 src_type.IsInteger()));
1767 /* check the register contents */
Ian Rogersad0b3a32012-04-16 14:50:24 -07001768 bool success =
Ian Rogers7b078e82014-09-10 14:44:24 -07001769 work_line_->VerifyRegisterType(this, vregA, use_src ? src_type : return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001770 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001771 AppendToLastFailMessage(StringPrintf(" return-1nr on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07001772 }
jeffhaobdb76512011-09-07 11:43:16 -07001773 }
1774 }
1775 break;
1776 case Instruction::RETURN_WIDE:
Ian Rogers7b078e82014-09-10 14:44:24 -07001777 if (!IsConstructor() || work_line_->CheckConstructorReturn(this)) {
jeffhaobdb76512011-09-07 11:43:16 -07001778 /* check the method signature */
Ian Rogersd8f69b02014-09-10 21:43:52 +00001779 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07001780 if (!return_type.IsCategory2Types()) {
jeffhaod5347e02012-03-22 17:25:05 -07001781 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-wide not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001782 } else {
1783 /* check the register contents */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001784 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07001785 bool success = work_line_->VerifyRegisterType(this, vregA, return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001786 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001787 AppendToLastFailMessage(StringPrintf(" return-wide on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07001788 }
jeffhaobdb76512011-09-07 11:43:16 -07001789 }
1790 }
1791 break;
1792 case Instruction::RETURN_OBJECT:
Ian Rogers7b078e82014-09-10 14:44:24 -07001793 if (!IsConstructor() || work_line_->CheckConstructorReturn(this)) {
Ian Rogersd8f69b02014-09-10 21:43:52 +00001794 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07001795 if (!return_type.IsReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001796 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-object not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001797 } else {
1798 /* return_type is the *expected* return type, not register value */
1799 DCHECK(!return_type.IsZero());
1800 DCHECK(!return_type.IsUninitializedReference());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001801 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07001802 const RegType& reg_type = work_line_->GetRegisterType(this, vregA);
Andreas Gampea32210c2015-06-24 10:26:13 -07001803 // Disallow returning undefined, conflict & uninitialized values and verify that the
1804 // reference in vAA is an instance of the "return_type."
1805 if (reg_type.IsUndefined()) {
1806 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning undefined register";
1807 } else if (reg_type.IsConflict()) {
1808 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning register with conflict";
1809 } else if (reg_type.IsUninitializedTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001810 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "returning uninitialized object '"
1811 << reg_type << "'";
Ian Rogers9074b992011-10-26 17:41:55 -07001812 } else if (!return_type.IsAssignableFrom(reg_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07001813 if (reg_type.IsUnresolvedTypes() || return_type.IsUnresolvedTypes()) {
1814 Fail(VERIFY_ERROR_NO_CLASS) << " can't resolve returned type '" << return_type
1815 << "' or '" << reg_type << "'";
1816 } else {
Andreas Gampe16f149c2015-03-23 10:10:20 -07001817 bool soft_error = false;
1818 // Check whether arrays are involved. They will show a valid class status, even
1819 // if their components are erroneous.
1820 if (reg_type.IsArrayTypes() && return_type.IsArrayTypes()) {
1821 return_type.CanAssignArray(reg_type, reg_types_, class_loader_, &soft_error);
1822 if (soft_error) {
1823 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "array with erroneous component type: "
1824 << reg_type << " vs " << return_type;
1825 }
1826 }
1827
1828 if (!soft_error) {
1829 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning '" << reg_type
1830 << "', but expected from declaration '" << return_type << "'";
1831 }
Jeff Haoa3faaf42013-09-03 19:07:00 -07001832 }
jeffhaobdb76512011-09-07 11:43:16 -07001833 }
1834 }
1835 }
1836 break;
1837
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001838 /* could be boolean, int, float, or a null reference */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001839 case Instruction::CONST_4: {
1840 int32_t val = static_cast<int32_t>(inst->VRegB_11n() << 28) >> 28;
Ian Rogers7b078e82014-09-10 14:44:24 -07001841 work_line_->SetRegisterType(this, inst->VRegA_11n(),
Ian Rogers46960fe2014-05-23 10:43:43 -07001842 DetermineCat1Constant(val, need_precise_constants_));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001843 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02001844 }
1845 case Instruction::CONST_16: {
1846 int16_t val = static_cast<int16_t>(inst->VRegB_21s());
Ian Rogers7b078e82014-09-10 14:44:24 -07001847 work_line_->SetRegisterType(this, inst->VRegA_21s(),
Ian Rogers46960fe2014-05-23 10:43:43 -07001848 DetermineCat1Constant(val, need_precise_constants_));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001849 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02001850 }
Sebastien Hertz849600b2013-12-20 10:28:08 +01001851 case Instruction::CONST: {
1852 int32_t val = inst->VRegB_31i();
Ian Rogers7b078e82014-09-10 14:44:24 -07001853 work_line_->SetRegisterType(this, inst->VRegA_31i(),
Ian Rogers46960fe2014-05-23 10:43:43 -07001854 DetermineCat1Constant(val, need_precise_constants_));
jeffhaobdb76512011-09-07 11:43:16 -07001855 break;
Sebastien Hertz849600b2013-12-20 10:28:08 +01001856 }
1857 case Instruction::CONST_HIGH16: {
1858 int32_t val = static_cast<int32_t>(inst->VRegB_21h() << 16);
Ian Rogers7b078e82014-09-10 14:44:24 -07001859 work_line_->SetRegisterType(this, inst->VRegA_21h(),
Ian Rogers46960fe2014-05-23 10:43:43 -07001860 DetermineCat1Constant(val, need_precise_constants_));
jeffhaobdb76512011-09-07 11:43:16 -07001861 break;
Sebastien Hertz849600b2013-12-20 10:28:08 +01001862 }
jeffhaobdb76512011-09-07 11:43:16 -07001863 /* could be long or double; resolved upon use */
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001864 case Instruction::CONST_WIDE_16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001865 int64_t val = static_cast<int16_t>(inst->VRegB_21s());
Ian Rogersd8f69b02014-09-10 21:43:52 +00001866 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1867 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07001868 work_line_->SetRegisterTypeWide(this, inst->VRegA_21s(), lo, hi);
jeffhaobdb76512011-09-07 11:43:16 -07001869 break;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001870 }
1871 case Instruction::CONST_WIDE_32: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001872 int64_t val = static_cast<int32_t>(inst->VRegB_31i());
Ian Rogersd8f69b02014-09-10 21:43:52 +00001873 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1874 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07001875 work_line_->SetRegisterTypeWide(this, inst->VRegA_31i(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001876 break;
1877 }
1878 case Instruction::CONST_WIDE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001879 int64_t val = inst->VRegB_51l();
Ian Rogersd8f69b02014-09-10 21:43:52 +00001880 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1881 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07001882 work_line_->SetRegisterTypeWide(this, inst->VRegA_51l(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001883 break;
1884 }
1885 case Instruction::CONST_WIDE_HIGH16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001886 int64_t val = static_cast<uint64_t>(inst->VRegB_21h()) << 48;
Ian Rogersd8f69b02014-09-10 21:43:52 +00001887 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1888 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07001889 work_line_->SetRegisterTypeWide(this, inst->VRegA_21h(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001890 break;
1891 }
jeffhaobdb76512011-09-07 11:43:16 -07001892 case Instruction::CONST_STRING:
Ian Rogers7b078e82014-09-10 14:44:24 -07001893 work_line_->SetRegisterType(this, inst->VRegA_21c(), reg_types_.JavaLangString());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001894 break;
jeffhaobdb76512011-09-07 11:43:16 -07001895 case Instruction::CONST_STRING_JUMBO:
Ian Rogers7b078e82014-09-10 14:44:24 -07001896 work_line_->SetRegisterType(this, inst->VRegA_31c(), reg_types_.JavaLangString());
jeffhaobdb76512011-09-07 11:43:16 -07001897 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001898 case Instruction::CONST_CLASS: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001899 // Get type from instruction if unresolved then we need an access check
1900 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Ian Rogersd8f69b02014-09-10 21:43:52 +00001901 const RegType& res_type = ResolveClassAndCheckAccess(inst->VRegB_21c());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001902 // Register holds class, ie its type is class, on error it will hold Conflict.
Ian Rogers7b078e82014-09-10 14:44:24 -07001903 work_line_->SetRegisterType(this, inst->VRegA_21c(),
Ian Rogersb4903572012-10-11 11:52:56 -07001904 res_type.IsConflict() ? res_type
Ian Rogers7b078e82014-09-10 14:44:24 -07001905 : reg_types_.JavaLangClass());
jeffhaobdb76512011-09-07 11:43:16 -07001906 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001907 }
jeffhaobdb76512011-09-07 11:43:16 -07001908 case Instruction::MONITOR_ENTER:
Ian Rogers7b078e82014-09-10 14:44:24 -07001909 work_line_->PushMonitor(this, inst->VRegA_11x(), work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07001910 break;
1911 case Instruction::MONITOR_EXIT:
1912 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001913 * monitor-exit instructions are odd. They can throw exceptions,
jeffhaobdb76512011-09-07 11:43:16 -07001914 * but when they do they act as if they succeeded and the PC is
jeffhaod1f0fde2011-09-08 17:25:33 -07001915 * pointing to the following instruction. (This behavior goes back
jeffhaobdb76512011-09-07 11:43:16 -07001916 * to the need to handle asynchronous exceptions, a now-deprecated
1917 * feature that Dalvik doesn't support.)
1918 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001919 * In practice we don't need to worry about this. The only
jeffhaobdb76512011-09-07 11:43:16 -07001920 * exceptions that can be thrown from monitor-exit are for a
jeffhaod1f0fde2011-09-08 17:25:33 -07001921 * null reference and -exit without a matching -enter. If the
jeffhaobdb76512011-09-07 11:43:16 -07001922 * structured locking checks are working, the former would have
1923 * failed on the -enter instruction, and the latter is impossible.
1924 *
1925 * This is fortunate, because issue 3221411 prevents us from
1926 * chasing the "can throw" path when monitor verification is
jeffhaod1f0fde2011-09-08 17:25:33 -07001927 * enabled. If we can fully verify the locking we can ignore
jeffhaobdb76512011-09-07 11:43:16 -07001928 * some catch blocks (which will show up as "dead" code when
1929 * we skip them here); if we can't, then the code path could be
1930 * "live" so we still need to check it.
1931 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001932 opcode_flags &= ~Instruction::kThrow;
Ian Rogers7b078e82014-09-10 14:44:24 -07001933 work_line_->PopMonitor(this, inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07001934 break;
1935
Ian Rogers28ad40d2011-10-27 15:19:26 -07001936 case Instruction::CHECK_CAST:
Ian Rogersd81871c2011-10-03 13:57:23 -07001937 case Instruction::INSTANCE_OF: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001938 /*
1939 * If this instruction succeeds, we will "downcast" register vA to the type in vB. (This
1940 * could be a "upcast" -- not expected, so we don't try to address it.)
1941 *
1942 * If it fails, an exception is thrown, which we deal with later by ignoring the update to
Elliott Hughesadb8c672012-03-06 16:49:32 -08001943 * dec_insn.vA when branching to a handler.
Ian Rogers28ad40d2011-10-27 15:19:26 -07001944 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001945 const bool is_checkcast = (inst->Opcode() == Instruction::CHECK_CAST);
1946 const uint32_t type_idx = (is_checkcast) ? inst->VRegB_21c() : inst->VRegC_22c();
Ian Rogersd8f69b02014-09-10 21:43:52 +00001947 const RegType& res_type = ResolveClassAndCheckAccess(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001948 if (res_type.IsConflict()) {
Andreas Gampe00633eb2014-07-17 16:13:35 -07001949 // If this is a primitive type, fail HARD.
Mathieu Chartierbf99f772014-08-23 16:37:27 -07001950 mirror::Class* klass = dex_cache_->GetResolvedType(type_idx);
Andreas Gampe00633eb2014-07-17 16:13:35 -07001951 if (klass != nullptr && klass->IsPrimitive()) {
1952 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "using primitive type "
1953 << dex_file_->StringByTypeIdx(type_idx) << " in instanceof in "
1954 << GetDeclaringClass();
1955 break;
1956 }
1957
Ian Rogersad0b3a32012-04-16 14:50:24 -07001958 DCHECK_NE(failures_.size(), 0U);
1959 if (!is_checkcast) {
Ian Rogers7b078e82014-09-10 14:44:24 -07001960 work_line_->SetRegisterType(this, inst->VRegA_22c(), reg_types_.Boolean());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001961 }
1962 break; // bad class
Ian Rogers9f1ab122011-12-12 08:52:43 -08001963 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001964 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Sebastien Hertz5243e912013-05-21 10:55:07 +02001965 uint32_t orig_type_reg = (is_checkcast) ? inst->VRegA_21c() : inst->VRegB_22c();
Ian Rogers7b078e82014-09-10 14:44:24 -07001966 const RegType& orig_type = work_line_->GetRegisterType(this, orig_type_reg);
Ian Rogers28ad40d2011-10-27 15:19:26 -07001967 if (!res_type.IsNonZeroReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001968 if (is_checkcast) {
1969 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on unexpected class " << res_type;
1970 } else {
1971 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on unexpected class " << res_type;
1972 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001973 } else if (!orig_type.IsReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001974 if (is_checkcast) {
1975 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on non-reference in v" << orig_type_reg;
1976 } else {
1977 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on non-reference in v" << orig_type_reg;
1978 }
jeffhao2a8a90e2011-09-26 14:25:31 -07001979 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001980 if (is_checkcast) {
Ian Rogers7b078e82014-09-10 14:44:24 -07001981 work_line_->SetRegisterType(this, inst->VRegA_21c(), res_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001982 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07001983 work_line_->SetRegisterType(this, inst->VRegA_22c(), reg_types_.Boolean());
jeffhaobdb76512011-09-07 11:43:16 -07001984 }
jeffhaobdb76512011-09-07 11:43:16 -07001985 }
jeffhao2a8a90e2011-09-26 14:25:31 -07001986 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001987 }
1988 case Instruction::ARRAY_LENGTH: {
Ian Rogers7b078e82014-09-10 14:44:24 -07001989 const RegType& res_type = work_line_->GetRegisterType(this, inst->VRegB_12x());
Ian Rogers28ad40d2011-10-27 15:19:26 -07001990 if (res_type.IsReferenceTypes()) {
Ian Rogers89310de2012-02-01 13:47:30 -08001991 if (!res_type.IsArrayTypes() && !res_type.IsZero()) { // ie not an array or null
jeffhaod5347e02012-03-22 17:25:05 -07001992 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001993 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07001994 work_line_->SetRegisterType(this, inst->VRegA_12x(), reg_types_.Integer());
Ian Rogersd81871c2011-10-03 13:57:23 -07001995 }
Andreas Gampe65c9db82014-07-28 13:14:34 -07001996 } else {
1997 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001998 }
1999 break;
2000 }
2001 case Instruction::NEW_INSTANCE: {
Ian Rogersd8f69b02014-09-10 21:43:52 +00002002 const RegType& res_type = ResolveClassAndCheckAccess(inst->VRegB_21c());
Ian Rogersad0b3a32012-04-16 14:50:24 -07002003 if (res_type.IsConflict()) {
2004 DCHECK_NE(failures_.size(), 0U);
2005 break; // bad class
jeffhao8cd6dda2012-02-22 10:15:34 -08002006 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002007 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
2008 // can't create an instance of an interface or abstract class */
2009 if (!res_type.IsInstantiableTypes()) {
2010 Fail(VERIFY_ERROR_INSTANTIATION)
2011 << "new-instance on primitive, interface or abstract class" << res_type;
Ian Rogers08f753d2012-08-24 14:35:25 -07002012 // Soft failure so carry on to set register type.
Ian Rogersd81871c2011-10-03 13:57:23 -07002013 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00002014 const RegType& uninit_type = reg_types_.Uninitialized(res_type, work_insn_idx_);
Ian Rogers08f753d2012-08-24 14:35:25 -07002015 // Any registers holding previous allocations from this address that have not yet been
2016 // initialized must be marked invalid.
Ian Rogers7b078e82014-09-10 14:44:24 -07002017 work_line_->MarkUninitRefsAsInvalid(this, uninit_type);
Ian Rogers08f753d2012-08-24 14:35:25 -07002018 // add the new uninitialized reference to the register state
Ian Rogers7b078e82014-09-10 14:44:24 -07002019 work_line_->SetRegisterType(this, inst->VRegA_21c(), uninit_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07002020 break;
2021 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08002022 case Instruction::NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002023 VerifyNewArray(inst, false, false);
jeffhaobdb76512011-09-07 11:43:16 -07002024 break;
2025 case Instruction::FILLED_NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002026 VerifyNewArray(inst, true, false);
Ian Rogers0c4a5062012-02-03 15:18:59 -08002027 just_set_result = true; // Filled new array sets result register
jeffhaobdb76512011-09-07 11:43:16 -07002028 break;
Ian Rogers0c4a5062012-02-03 15:18:59 -08002029 case Instruction::FILLED_NEW_ARRAY_RANGE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002030 VerifyNewArray(inst, true, true);
Ian Rogers0c4a5062012-02-03 15:18:59 -08002031 just_set_result = true; // Filled new array range sets result register
2032 break;
jeffhaobdb76512011-09-07 11:43:16 -07002033 case Instruction::CMPL_FLOAT:
2034 case Instruction::CMPG_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002035 if (!work_line_->VerifyRegisterType(this, inst->VRegB_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08002036 break;
2037 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002038 if (!work_line_->VerifyRegisterType(this, inst->VRegC_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08002039 break;
2040 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002041 work_line_->SetRegisterType(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002042 break;
2043 case Instruction::CMPL_DOUBLE:
2044 case Instruction::CMPG_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002045 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegB_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002046 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002047 break;
2048 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002049 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegC_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002050 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002051 break;
2052 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002053 work_line_->SetRegisterType(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002054 break;
2055 case Instruction::CMP_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002056 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegB_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002057 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002058 break;
2059 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002060 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegC_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002061 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002062 break;
2063 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002064 work_line_->SetRegisterType(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002065 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002066 case Instruction::THROW: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002067 const RegType& res_type = work_line_->GetRegisterType(this, inst->VRegA_11x());
Ian Rogersb4903572012-10-11 11:52:56 -07002068 if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(res_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07002069 Fail(res_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS : VERIFY_ERROR_BAD_CLASS_SOFT)
2070 << "thrown class " << res_type << " not instanceof Throwable";
jeffhaobdb76512011-09-07 11:43:16 -07002071 }
2072 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002073 }
jeffhaobdb76512011-09-07 11:43:16 -07002074 case Instruction::GOTO:
2075 case Instruction::GOTO_16:
2076 case Instruction::GOTO_32:
2077 /* no effect on or use of registers */
2078 break;
2079
2080 case Instruction::PACKED_SWITCH:
2081 case Instruction::SPARSE_SWITCH:
2082 /* verify that vAA is an integer, or can be converted to one */
Ian Rogers7b078e82014-09-10 14:44:24 -07002083 work_line_->VerifyRegisterType(this, inst->VRegA_31t(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002084 break;
2085
Ian Rogersd81871c2011-10-03 13:57:23 -07002086 case Instruction::FILL_ARRAY_DATA: {
2087 /* Similar to the verification done for APUT */
Ian Rogers7b078e82014-09-10 14:44:24 -07002088 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegA_31t());
Ian Rogers89310de2012-02-01 13:47:30 -08002089 /* array_type can be null if the reg type is Zero */
2090 if (!array_type.IsZero()) {
jeffhao457cc512012-02-02 16:55:13 -08002091 if (!array_type.IsArrayTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002092 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with array type "
2093 << array_type;
Ian Rogers89310de2012-02-01 13:47:30 -08002094 } else {
Ian Rogersd8f69b02014-09-10 21:43:52 +00002095 const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
Ian Rogersad0b3a32012-04-16 14:50:24 -07002096 DCHECK(!component_type.IsConflict());
jeffhao457cc512012-02-02 16:55:13 -08002097 if (component_type.IsNonZeroReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002098 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with component type "
2099 << component_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002100 } else {
jeffhao457cc512012-02-02 16:55:13 -08002101 // Now verify if the element width in the table matches the element width declared in
2102 // the array
2103 const uint16_t* array_data = insns + (insns[1] | (((int32_t) insns[2]) << 16));
2104 if (array_data[0] != Instruction::kArrayDataSignature) {
jeffhaod5347e02012-03-22 17:25:05 -07002105 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid magic for array-data";
jeffhao457cc512012-02-02 16:55:13 -08002106 } else {
2107 size_t elem_width = Primitive::ComponentSize(component_type.GetPrimitiveType());
2108 // Since we don't compress the data in Dex, expect to see equal width of data stored
2109 // in the table and expected from the array class.
2110 if (array_data[1] != elem_width) {
jeffhaod5347e02012-03-22 17:25:05 -07002111 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-data size mismatch (" << array_data[1]
2112 << " vs " << elem_width << ")";
jeffhao457cc512012-02-02 16:55:13 -08002113 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002114 }
2115 }
jeffhaobdb76512011-09-07 11:43:16 -07002116 }
2117 }
2118 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002119 }
jeffhaobdb76512011-09-07 11:43:16 -07002120 case Instruction::IF_EQ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002121 case Instruction::IF_NE: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002122 const RegType& reg_type1 = work_line_->GetRegisterType(this, inst->VRegA_22t());
2123 const RegType& reg_type2 = work_line_->GetRegisterType(this, inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002124 bool mismatch = false;
2125 if (reg_type1.IsZero()) { // zero then integral or reference expected
2126 mismatch = !reg_type2.IsReferenceTypes() && !reg_type2.IsIntegralTypes();
2127 } else if (reg_type1.IsReferenceTypes()) { // both references?
2128 mismatch = !reg_type2.IsReferenceTypes();
2129 } else { // both integral?
2130 mismatch = !reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes();
2131 }
2132 if (mismatch) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002133 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to if-eq/if-ne (" << reg_type1 << ","
2134 << reg_type2 << ") must both be references or integral";
jeffhaobdb76512011-09-07 11:43:16 -07002135 }
2136 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002137 }
jeffhaobdb76512011-09-07 11:43:16 -07002138 case Instruction::IF_LT:
2139 case Instruction::IF_GE:
2140 case Instruction::IF_GT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002141 case Instruction::IF_LE: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002142 const RegType& reg_type1 = work_line_->GetRegisterType(this, inst->VRegA_22t());
2143 const RegType& reg_type2 = work_line_->GetRegisterType(this, inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002144 if (!reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002145 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to 'if' (" << reg_type1 << ","
2146 << reg_type2 << ") must be integral";
jeffhaobdb76512011-09-07 11:43:16 -07002147 }
2148 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002149 }
jeffhaobdb76512011-09-07 11:43:16 -07002150 case Instruction::IF_EQZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002151 case Instruction::IF_NEZ: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002152 const RegType& reg_type = work_line_->GetRegisterType(this, inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002153 if (!reg_type.IsReferenceTypes() && !reg_type.IsIntegralTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002154 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
2155 << " unexpected as arg to if-eqz/if-nez";
Ian Rogersd81871c2011-10-03 13:57:23 -07002156 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002157
2158 // Find previous instruction - its existence is a precondition to peephole optimization.
Ian Rogers9b360392013-06-06 14:45:07 -07002159 uint32_t instance_of_idx = 0;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002160 if (0 != work_insn_idx_) {
Ian Rogers9b360392013-06-06 14:45:07 -07002161 instance_of_idx = work_insn_idx_ - 1;
Brian Carlstromdf629502013-07-17 22:39:56 -07002162 while (0 != instance_of_idx && !insn_flags_[instance_of_idx].IsOpcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07002163 instance_of_idx--;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002164 }
Andreas Gampe7c038102014-10-27 20:08:46 -07002165 if (FailOrAbort(this, insn_flags_[instance_of_idx].IsOpcode(),
2166 "Unable to get previous instruction of if-eqz/if-nez for work index ",
2167 work_insn_idx_)) {
2168 break;
2169 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002170 } else {
2171 break;
2172 }
2173
Ian Rogers9b360392013-06-06 14:45:07 -07002174 const Instruction* instance_of_inst = Instruction::At(code_item_->insns_ + instance_of_idx);
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002175
2176 /* Check for peep-hole pattern of:
2177 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002178 * instance-of vX, vY, T;
2179 * ifXXX vX, label ;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002180 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002181 * label:
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002182 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002183 * and sharpen the type of vY to be type T.
2184 * Note, this pattern can't be if:
2185 * - if there are other branches to this branch,
2186 * - when vX == vY.
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002187 */
Ian Rogersfae370a2013-06-05 08:33:27 -07002188 if (!CurrentInsnFlags()->IsBranchTarget() &&
Ian Rogers9b360392013-06-06 14:45:07 -07002189 (Instruction::INSTANCE_OF == instance_of_inst->Opcode()) &&
2190 (inst->VRegA_21t() == instance_of_inst->VRegA_22c()) &&
2191 (instance_of_inst->VRegA_22c() != instance_of_inst->VRegB_22c())) {
Ian Rogersebbdd872014-07-07 23:53:08 -07002192 // Check the type of the instance-of is different than that of registers type, as if they
2193 // are the same there is no work to be done here. Check that the conversion is not to or
2194 // from an unresolved type as type information is imprecise. If the instance-of is to an
2195 // interface then ignore the type information as interfaces can only be treated as Objects
2196 // and we don't want to disallow field and other operations on the object. If the value
2197 // being instance-of checked against is known null (zero) then allow the optimization as
2198 // we didn't have type information. If the merge of the instance-of type with the original
2199 // type is assignable to the original then allow optimization. This check is performed to
2200 // ensure that subsequent merges don't lose type information - such as becoming an
2201 // interface from a class that would lose information relevant to field checks.
Ian Rogers7b078e82014-09-10 14:44:24 -07002202 const RegType& orig_type = work_line_->GetRegisterType(this, instance_of_inst->VRegB_22c());
Ian Rogersd8f69b02014-09-10 21:43:52 +00002203 const RegType& cast_type = ResolveClassAndCheckAccess(instance_of_inst->VRegC_22c());
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002204
Ian Rogersebbdd872014-07-07 23:53:08 -07002205 if (!orig_type.Equals(cast_type) &&
2206 !cast_type.IsUnresolvedTypes() && !orig_type.IsUnresolvedTypes() &&
Andreas Gampe00633eb2014-07-17 16:13:35 -07002207 cast_type.HasClass() && // Could be conflict type, make sure it has a class.
Ian Rogersebbdd872014-07-07 23:53:08 -07002208 !cast_type.GetClass()->IsInterface() &&
2209 (orig_type.IsZero() ||
2210 orig_type.IsStrictlyAssignableFrom(cast_type.Merge(orig_type, &reg_types_)))) {
Ian Rogersd0fbd852013-09-24 18:17:04 -07002211 RegisterLine* update_line = RegisterLine::Create(code_item_->registers_size_, this);
Ian Rogersfae370a2013-06-05 08:33:27 -07002212 if (inst->Opcode() == Instruction::IF_EQZ) {
Ian Rogers9b360392013-06-06 14:45:07 -07002213 fallthrough_line.reset(update_line);
Ian Rogersfae370a2013-06-05 08:33:27 -07002214 } else {
Ian Rogers9b360392013-06-06 14:45:07 -07002215 branch_line.reset(update_line);
2216 }
2217 update_line->CopyFromLine(work_line_.get());
Ian Rogers7b078e82014-09-10 14:44:24 -07002218 update_line->SetRegisterType(this, instance_of_inst->VRegB_22c(), cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002219 if (!insn_flags_[instance_of_idx].IsBranchTarget() && 0 != instance_of_idx) {
2220 // See if instance-of was preceded by a move-object operation, common due to the small
2221 // register encoding space of instance-of, and propagate type information to the source
2222 // of the move-object.
2223 uint32_t move_idx = instance_of_idx - 1;
Brian Carlstromdf629502013-07-17 22:39:56 -07002224 while (0 != move_idx && !insn_flags_[move_idx].IsOpcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07002225 move_idx--;
2226 }
Andreas Gampe7c038102014-10-27 20:08:46 -07002227 if (FailOrAbort(this, insn_flags_[move_idx].IsOpcode(),
2228 "Unable to get previous instruction of if-eqz/if-nez for work index ",
2229 work_insn_idx_)) {
2230 break;
2231 }
Ian Rogers9b360392013-06-06 14:45:07 -07002232 const Instruction* move_inst = Instruction::At(code_item_->insns_ + move_idx);
2233 switch (move_inst->Opcode()) {
2234 case Instruction::MOVE_OBJECT:
2235 if (move_inst->VRegA_12x() == instance_of_inst->VRegB_22c()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002236 update_line->SetRegisterType(this, move_inst->VRegB_12x(), cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002237 }
2238 break;
2239 case Instruction::MOVE_OBJECT_FROM16:
2240 if (move_inst->VRegA_22x() == instance_of_inst->VRegB_22c()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002241 update_line->SetRegisterType(this, move_inst->VRegB_22x(), cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002242 }
2243 break;
2244 case Instruction::MOVE_OBJECT_16:
2245 if (move_inst->VRegA_32x() == instance_of_inst->VRegB_22c()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002246 update_line->SetRegisterType(this, move_inst->VRegB_32x(), cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002247 }
2248 break;
2249 default:
2250 break;
2251 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002252 }
2253 }
2254 }
2255
jeffhaobdb76512011-09-07 11:43:16 -07002256 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002257 }
jeffhaobdb76512011-09-07 11:43:16 -07002258 case Instruction::IF_LTZ:
2259 case Instruction::IF_GEZ:
2260 case Instruction::IF_GTZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002261 case Instruction::IF_LEZ: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002262 const RegType& reg_type = work_line_->GetRegisterType(this, inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002263 if (!reg_type.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002264 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
2265 << " unexpected as arg to if-ltz/if-gez/if-gtz/if-lez";
Ian Rogersd81871c2011-10-03 13:57:23 -07002266 }
jeffhaobdb76512011-09-07 11:43:16 -07002267 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002268 }
jeffhaobdb76512011-09-07 11:43:16 -07002269 case Instruction::AGET_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002270 VerifyAGet(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002271 break;
jeffhaobdb76512011-09-07 11:43:16 -07002272 case Instruction::AGET_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002273 VerifyAGet(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002274 break;
jeffhaobdb76512011-09-07 11:43:16 -07002275 case Instruction::AGET_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002276 VerifyAGet(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002277 break;
jeffhaobdb76512011-09-07 11:43:16 -07002278 case Instruction::AGET_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002279 VerifyAGet(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002280 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002281 case Instruction::AGET:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002282 VerifyAGet(inst, reg_types_.Integer(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002283 break;
jeffhaobdb76512011-09-07 11:43:16 -07002284 case Instruction::AGET_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002285 VerifyAGet(inst, reg_types_.LongLo(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002286 break;
2287 case Instruction::AGET_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002288 VerifyAGet(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07002289 break;
2290
Ian Rogersd81871c2011-10-03 13:57:23 -07002291 case Instruction::APUT_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002292 VerifyAPut(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002293 break;
2294 case Instruction::APUT_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002295 VerifyAPut(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002296 break;
2297 case Instruction::APUT_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002298 VerifyAPut(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002299 break;
2300 case Instruction::APUT_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002301 VerifyAPut(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002302 break;
2303 case Instruction::APUT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002304 VerifyAPut(inst, reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002305 break;
2306 case Instruction::APUT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002307 VerifyAPut(inst, reg_types_.LongLo(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002308 break;
2309 case Instruction::APUT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002310 VerifyAPut(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07002311 break;
2312
jeffhaobdb76512011-09-07 11:43:16 -07002313 case Instruction::IGET_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002314 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002315 break;
jeffhaobdb76512011-09-07 11:43:16 -07002316 case Instruction::IGET_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002317 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002318 break;
jeffhaobdb76512011-09-07 11:43:16 -07002319 case Instruction::IGET_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002320 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002321 break;
jeffhaobdb76512011-09-07 11:43:16 -07002322 case Instruction::IGET_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002323 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002324 break;
2325 case Instruction::IGET:
Andreas Gampe896df402014-10-20 22:25:29 -07002326 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002327 break;
2328 case Instruction::IGET_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002329 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002330 break;
2331 case Instruction::IGET_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002332 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false,
2333 false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002334 break;
jeffhaobdb76512011-09-07 11:43:16 -07002335
Ian Rogersd81871c2011-10-03 13:57:23 -07002336 case Instruction::IPUT_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002337 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002338 break;
2339 case Instruction::IPUT_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002340 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002341 break;
2342 case Instruction::IPUT_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002343 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002344 break;
2345 case Instruction::IPUT_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002346 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002347 break;
2348 case Instruction::IPUT:
Andreas Gampe896df402014-10-20 22:25:29 -07002349 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002350 break;
2351 case Instruction::IPUT_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002352 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002353 break;
jeffhaobdb76512011-09-07 11:43:16 -07002354 case Instruction::IPUT_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002355 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false,
2356 false);
jeffhaobdb76512011-09-07 11:43:16 -07002357 break;
2358
jeffhaobdb76512011-09-07 11:43:16 -07002359 case Instruction::SGET_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002360 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002361 break;
jeffhaobdb76512011-09-07 11:43:16 -07002362 case Instruction::SGET_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002363 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002364 break;
jeffhaobdb76512011-09-07 11:43:16 -07002365 case Instruction::SGET_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002366 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002367 break;
jeffhaobdb76512011-09-07 11:43:16 -07002368 case Instruction::SGET_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002369 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002370 break;
2371 case Instruction::SGET:
Andreas Gampe896df402014-10-20 22:25:29 -07002372 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002373 break;
2374 case Instruction::SGET_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002375 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002376 break;
2377 case Instruction::SGET_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002378 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false,
2379 true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002380 break;
2381
2382 case Instruction::SPUT_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002383 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002384 break;
2385 case Instruction::SPUT_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002386 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002387 break;
2388 case Instruction::SPUT_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002389 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002390 break;
2391 case Instruction::SPUT_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002392 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002393 break;
2394 case Instruction::SPUT:
Andreas Gampe896df402014-10-20 22:25:29 -07002395 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002396 break;
2397 case Instruction::SPUT_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002398 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002399 break;
2400 case Instruction::SPUT_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002401 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false,
2402 true);
jeffhaobdb76512011-09-07 11:43:16 -07002403 break;
2404
2405 case Instruction::INVOKE_VIRTUAL:
2406 case Instruction::INVOKE_VIRTUAL_RANGE:
2407 case Instruction::INVOKE_SUPER:
Ian Rogersd81871c2011-10-03 13:57:23 -07002408 case Instruction::INVOKE_SUPER_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002409 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE ||
2410 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002411 bool is_super = (inst->Opcode() == Instruction::INVOKE_SUPER ||
2412 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002413 ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_VIRTUAL, is_range, is_super);
Ian Rogersd8f69b02014-09-10 21:43:52 +00002414 const RegType* return_type = nullptr;
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002415 if (called_method != nullptr) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002416 StackHandleScope<1> hs(self_);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002417 mirror::Class* return_type_class = called_method->GetReturnType(can_load_classes_);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002418 if (return_type_class != nullptr) {
Andreas Gampef23f33d2015-06-23 14:18:17 -07002419 return_type = &FromClass(called_method->GetReturnTypeDescriptor(),
2420 return_type_class,
2421 return_type_class->CannotBeAssignedFromOtherTypes());
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002422 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07002423 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
2424 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002425 }
2426 }
2427 if (return_type == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002428 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002429 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2430 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002431 const char* descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Mathieu Chartierbf99f772014-08-23 16:37:27 -07002432 return_type = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
jeffhaobdb76512011-09-07 11:43:16 -07002433 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002434 if (!return_type->IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002435 work_line_->SetResultRegisterType(this, *return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002436 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002437 work_line_->SetResultRegisterTypeWide(*return_type, return_type->HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002438 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002439 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002440 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002441 }
jeffhaobdb76512011-09-07 11:43:16 -07002442 case Instruction::INVOKE_DIRECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002443 case Instruction::INVOKE_DIRECT_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002444 bool is_range = (inst->Opcode() == Instruction::INVOKE_DIRECT_RANGE);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002445 ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_DIRECT, is_range, false);
Ian Rogers46685432012-06-03 22:26:43 -07002446 const char* return_type_descriptor;
2447 bool is_constructor;
Ian Rogersd8f69b02014-09-10 21:43:52 +00002448 const RegType* return_type = nullptr;
Ian Rogers7b078e82014-09-10 14:44:24 -07002449 if (called_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002450 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogers46685432012-06-03 22:26:43 -07002451 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
Ian Rogersdfb325e2013-10-30 01:00:44 -07002452 is_constructor = strcmp("<init>", dex_file_->StringDataByIdx(method_id.name_idx_)) == 0;
Ian Rogers46685432012-06-03 22:26:43 -07002453 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
2454 return_type_descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2455 } else {
2456 is_constructor = called_method->IsConstructor();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002457 return_type_descriptor = called_method->GetReturnTypeDescriptor();
Ian Rogers7b078e82014-09-10 14:44:24 -07002458 StackHandleScope<1> hs(self_);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002459 mirror::Class* return_type_class = called_method->GetReturnType(can_load_classes_);
Ian Rogers1ff3c982014-08-12 02:30:58 -07002460 if (return_type_class != nullptr) {
Andreas Gampef23f33d2015-06-23 14:18:17 -07002461 return_type = &FromClass(return_type_descriptor,
2462 return_type_class,
2463 return_type_class->CannotBeAssignedFromOtherTypes());
Ian Rogers1ff3c982014-08-12 02:30:58 -07002464 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07002465 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
2466 self_->ClearException();
Ian Rogers1ff3c982014-08-12 02:30:58 -07002467 }
Ian Rogers46685432012-06-03 22:26:43 -07002468 }
2469 if (is_constructor) {
jeffhaobdb76512011-09-07 11:43:16 -07002470 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002471 * Some additional checks when calling a constructor. We know from the invocation arg check
2472 * that the "this" argument is an instance of called_method->klass. Now we further restrict
2473 * that to require that called_method->klass is the same as this->klass or this->super,
2474 * allowing the latter only if the "this" argument is the same as the "this" argument to
2475 * this method (which implies that we're in a constructor ourselves).
jeffhaobdb76512011-09-07 11:43:16 -07002476 */
Ian Rogers7b078e82014-09-10 14:44:24 -07002477 const RegType& this_type = work_line_->GetInvocationThis(this, inst, is_range);
jeffhaob57e9522012-04-26 18:08:21 -07002478 if (this_type.IsConflict()) // failure.
2479 break;
jeffhaobdb76512011-09-07 11:43:16 -07002480
jeffhaob57e9522012-04-26 18:08:21 -07002481 /* no null refs allowed (?) */
2482 if (this_type.IsZero()) {
2483 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unable to initialize null ref";
2484 break;
jeffhao2a8a90e2011-09-26 14:25:31 -07002485 }
jeffhaob57e9522012-04-26 18:08:21 -07002486
2487 /* must be in same class or in superclass */
Ian Rogersd8f69b02014-09-10 21:43:52 +00002488 // const RegType& this_super_klass = this_type.GetSuperClass(&reg_types_);
Ian Rogers46685432012-06-03 22:26:43 -07002489 // TODO: re-enable constructor type verification
2490 // if (this_super_klass.IsConflict()) {
jeffhaob57e9522012-04-26 18:08:21 -07002491 // Unknown super class, fail so we re-check at runtime.
Ian Rogers46685432012-06-03 22:26:43 -07002492 // Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "super class unknown for '" << this_type << "'";
2493 // break;
2494 // }
jeffhaob57e9522012-04-26 18:08:21 -07002495
2496 /* arg must be an uninitialized reference */
2497 if (!this_type.IsUninitializedTypes()) {
2498 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Expected initialization on uninitialized reference "
2499 << this_type;
2500 break;
2501 }
2502
2503 /*
2504 * Replace the uninitialized reference with an initialized one. We need to do this for all
2505 * registers that have the same object instance in them, not just the "this" register.
2506 */
Jeff Hao848f70a2014-01-15 13:49:50 -08002507 const uint32_t this_reg = (is_range) ? inst->VRegC_3rc() : inst->VRegC_35c();
2508 work_line_->MarkRefsAsInitialized(this, this_type, this_reg, work_insn_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002509 }
Ian Rogers1ff3c982014-08-12 02:30:58 -07002510 if (return_type == nullptr) {
Mathieu Chartierbf99f772014-08-23 16:37:27 -07002511 return_type = &reg_types_.FromDescriptor(GetClassLoader(), return_type_descriptor,
2512 false);
Ian Rogers1ff3c982014-08-12 02:30:58 -07002513 }
2514 if (!return_type->IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002515 work_line_->SetResultRegisterType(this, *return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002516 } else {
Ian Rogers1ff3c982014-08-12 02:30:58 -07002517 work_line_->SetResultRegisterTypeWide(*return_type, return_type->HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002518 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002519 just_set_result = true;
2520 break;
2521 }
2522 case Instruction::INVOKE_STATIC:
2523 case Instruction::INVOKE_STATIC_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002524 bool is_range = (inst->Opcode() == Instruction::INVOKE_STATIC_RANGE);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002525 ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_STATIC, is_range, false);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002526 const char* descriptor;
Ian Rogers7b078e82014-09-10 14:44:24 -07002527 if (called_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002528 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002529 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2530 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Mathieu Chartier590fee92013-09-13 13:46:47 -07002531 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002532 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002533 descriptor = called_method->GetReturnTypeDescriptor();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002534 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00002535 const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002536 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002537 work_line_->SetResultRegisterType(this, return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002538 } else {
2539 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2540 }
jeffhaobdb76512011-09-07 11:43:16 -07002541 just_set_result = true;
2542 }
2543 break;
jeffhaobdb76512011-09-07 11:43:16 -07002544 case Instruction::INVOKE_INTERFACE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002545 case Instruction::INVOKE_INTERFACE_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002546 bool is_range = (inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002547 ArtMethod* abs_method = VerifyInvocationArgs(inst, METHOD_INTERFACE, is_range, false);
Ian Rogers7b078e82014-09-10 14:44:24 -07002548 if (abs_method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002549 mirror::Class* called_interface = abs_method->GetDeclaringClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002550 if (!called_interface->IsInterface() && !called_interface->IsObjectClass()) {
2551 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected interface class in invoke-interface '"
2552 << PrettyMethod(abs_method) << "'";
2553 break;
Ian Rogers28ad40d2011-10-27 15:19:26 -07002554 }
Ian Rogers0d604842012-04-16 14:50:24 -07002555 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002556 /* Get the type of the "this" arg, which should either be a sub-interface of called
2557 * interface or Object (see comments in RegType::JoinClass).
2558 */
Ian Rogers7b078e82014-09-10 14:44:24 -07002559 const RegType& this_type = work_line_->GetInvocationThis(this, inst, is_range);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002560 if (this_type.IsZero()) {
2561 /* null pointer always passes (and always fails at runtime) */
2562 } else {
2563 if (this_type.IsUninitializedTypes()) {
2564 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface call on uninitialized object "
2565 << this_type;
2566 break;
2567 }
2568 // In the past we have tried to assert that "called_interface" is assignable
2569 // from "this_type.GetClass()", however, as we do an imprecise Join
2570 // (RegType::JoinClass) we don't have full information on what interfaces are
2571 // implemented by "this_type". For example, two classes may implement the same
2572 // interfaces and have a common parent that doesn't implement the interface. The
2573 // join will set "this_type" to the parent class and a test that this implements
2574 // the interface will incorrectly fail.
2575 }
2576 /*
2577 * We don't have an object instance, so we can't find the concrete method. However, all of
2578 * the type information is in the abstract method, so we're good.
2579 */
2580 const char* descriptor;
Ian Rogers7b078e82014-09-10 14:44:24 -07002581 if (abs_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002582 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002583 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2584 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
2585 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2586 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002587 descriptor = abs_method->GetReturnTypeDescriptor();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002588 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00002589 const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002590 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002591 work_line_->SetResultRegisterType(this, return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002592 } else {
2593 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2594 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002595 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002596 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002597 }
jeffhaobdb76512011-09-07 11:43:16 -07002598 case Instruction::NEG_INT:
2599 case Instruction::NOT_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002600 work_line_->CheckUnaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002601 break;
2602 case Instruction::NEG_LONG:
2603 case Instruction::NOT_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002604 work_line_->CheckUnaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002605 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002606 break;
2607 case Instruction::NEG_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002608 work_line_->CheckUnaryOp(this, inst, reg_types_.Float(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002609 break;
2610 case Instruction::NEG_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002611 work_line_->CheckUnaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002612 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002613 break;
2614 case Instruction::INT_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002615 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002616 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002617 break;
2618 case Instruction::INT_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002619 work_line_->CheckUnaryOp(this, inst, reg_types_.Float(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002620 break;
2621 case Instruction::INT_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002622 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002623 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002624 break;
2625 case Instruction::LONG_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002626 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Integer(),
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::LONG_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002630 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002631 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002632 break;
2633 case Instruction::LONG_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002634 work_line_->CheckUnaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002635 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002636 break;
2637 case Instruction::FLOAT_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002638 work_line_->CheckUnaryOp(this, inst, reg_types_.Integer(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002639 break;
2640 case Instruction::FLOAT_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002641 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002642 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002643 break;
2644 case Instruction::FLOAT_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002645 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002646 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002647 break;
2648 case Instruction::DOUBLE_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002649 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Integer(),
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::DOUBLE_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002653 work_line_->CheckUnaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002654 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002655 break;
2656 case Instruction::DOUBLE_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002657 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002658 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002659 break;
2660 case Instruction::INT_TO_BYTE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002661 work_line_->CheckUnaryOp(this, inst, reg_types_.Byte(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002662 break;
2663 case Instruction::INT_TO_CHAR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002664 work_line_->CheckUnaryOp(this, inst, reg_types_.Char(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002665 break;
2666 case Instruction::INT_TO_SHORT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002667 work_line_->CheckUnaryOp(this, inst, reg_types_.Short(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002668 break;
2669
2670 case Instruction::ADD_INT:
2671 case Instruction::SUB_INT:
2672 case Instruction::MUL_INT:
2673 case Instruction::REM_INT:
2674 case Instruction::DIV_INT:
2675 case Instruction::SHL_INT:
2676 case Instruction::SHR_INT:
2677 case Instruction::USHR_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002678 work_line_->CheckBinaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002679 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002680 break;
2681 case Instruction::AND_INT:
2682 case Instruction::OR_INT:
2683 case Instruction::XOR_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002684 work_line_->CheckBinaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002685 reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002686 break;
2687 case Instruction::ADD_LONG:
2688 case Instruction::SUB_LONG:
2689 case Instruction::MUL_LONG:
2690 case Instruction::DIV_LONG:
2691 case Instruction::REM_LONG:
2692 case Instruction::AND_LONG:
2693 case Instruction::OR_LONG:
2694 case Instruction::XOR_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002695 work_line_->CheckBinaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002696 reg_types_.LongLo(), reg_types_.LongHi(),
2697 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002698 break;
2699 case Instruction::SHL_LONG:
2700 case Instruction::SHR_LONG:
2701 case Instruction::USHR_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002702 /* shift distance is Int, making these different from other binary operations */
Ian Rogers7b078e82014-09-10 14:44:24 -07002703 work_line_->CheckBinaryOpWideShift(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002704 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002705 break;
2706 case Instruction::ADD_FLOAT:
2707 case Instruction::SUB_FLOAT:
2708 case Instruction::MUL_FLOAT:
2709 case Instruction::DIV_FLOAT:
2710 case Instruction::REM_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002711 work_line_->CheckBinaryOp(this, inst, reg_types_.Float(), reg_types_.Float(),
2712 reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002713 break;
2714 case Instruction::ADD_DOUBLE:
2715 case Instruction::SUB_DOUBLE:
2716 case Instruction::MUL_DOUBLE:
2717 case Instruction::DIV_DOUBLE:
2718 case Instruction::REM_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002719 work_line_->CheckBinaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002720 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2721 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002722 break;
2723 case Instruction::ADD_INT_2ADDR:
2724 case Instruction::SUB_INT_2ADDR:
2725 case Instruction::MUL_INT_2ADDR:
2726 case Instruction::REM_INT_2ADDR:
2727 case Instruction::SHL_INT_2ADDR:
2728 case Instruction::SHR_INT_2ADDR:
2729 case Instruction::USHR_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002730 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
2731 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002732 break;
2733 case Instruction::AND_INT_2ADDR:
2734 case Instruction::OR_INT_2ADDR:
2735 case Instruction::XOR_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002736 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
2737 reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002738 break;
2739 case Instruction::DIV_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002740 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
2741 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002742 break;
2743 case Instruction::ADD_LONG_2ADDR:
2744 case Instruction::SUB_LONG_2ADDR:
2745 case Instruction::MUL_LONG_2ADDR:
2746 case Instruction::DIV_LONG_2ADDR:
2747 case Instruction::REM_LONG_2ADDR:
2748 case Instruction::AND_LONG_2ADDR:
2749 case Instruction::OR_LONG_2ADDR:
2750 case Instruction::XOR_LONG_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002751 work_line_->CheckBinaryOp2addrWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002752 reg_types_.LongLo(), reg_types_.LongHi(),
2753 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002754 break;
2755 case Instruction::SHL_LONG_2ADDR:
2756 case Instruction::SHR_LONG_2ADDR:
2757 case Instruction::USHR_LONG_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002758 work_line_->CheckBinaryOp2addrWideShift(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002759 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002760 break;
2761 case Instruction::ADD_FLOAT_2ADDR:
2762 case Instruction::SUB_FLOAT_2ADDR:
2763 case Instruction::MUL_FLOAT_2ADDR:
2764 case Instruction::DIV_FLOAT_2ADDR:
2765 case Instruction::REM_FLOAT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002766 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Float(), reg_types_.Float(),
2767 reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002768 break;
2769 case Instruction::ADD_DOUBLE_2ADDR:
2770 case Instruction::SUB_DOUBLE_2ADDR:
2771 case Instruction::MUL_DOUBLE_2ADDR:
2772 case Instruction::DIV_DOUBLE_2ADDR:
2773 case Instruction::REM_DOUBLE_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002774 work_line_->CheckBinaryOp2addrWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002775 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2776 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002777 break;
2778 case Instruction::ADD_INT_LIT16:
Ian Rogersf72a11d2014-10-30 15:41:08 -07002779 case Instruction::RSUB_INT_LIT16:
jeffhaobdb76512011-09-07 11:43:16 -07002780 case Instruction::MUL_INT_LIT16:
2781 case Instruction::DIV_INT_LIT16:
2782 case Instruction::REM_INT_LIT16:
Ian Rogers7b078e82014-09-10 14:44:24 -07002783 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), false,
2784 true);
jeffhaobdb76512011-09-07 11:43:16 -07002785 break;
2786 case Instruction::AND_INT_LIT16:
2787 case Instruction::OR_INT_LIT16:
2788 case Instruction::XOR_INT_LIT16:
Ian Rogers7b078e82014-09-10 14:44:24 -07002789 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), true,
2790 true);
jeffhaobdb76512011-09-07 11:43:16 -07002791 break;
2792 case Instruction::ADD_INT_LIT8:
2793 case Instruction::RSUB_INT_LIT8:
2794 case Instruction::MUL_INT_LIT8:
2795 case Instruction::DIV_INT_LIT8:
2796 case Instruction::REM_INT_LIT8:
2797 case Instruction::SHL_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002798 case Instruction::SHR_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002799 case Instruction::USHR_INT_LIT8:
Ian Rogers7b078e82014-09-10 14:44:24 -07002800 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), false,
2801 false);
jeffhaobdb76512011-09-07 11:43:16 -07002802 break;
2803 case Instruction::AND_INT_LIT8:
2804 case Instruction::OR_INT_LIT8:
2805 case Instruction::XOR_INT_LIT8:
Ian Rogers7b078e82014-09-10 14:44:24 -07002806 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), true,
2807 false);
jeffhaobdb76512011-09-07 11:43:16 -07002808 break;
2809
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002810 // Special instructions.
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07002811 case Instruction::RETURN_VOID_NO_BARRIER:
2812 if (IsConstructor() && !IsStatic()) {
2813 auto& declaring_class = GetDeclaringClass();
Andreas Gampe68df3202015-06-22 11:35:46 -07002814 if (declaring_class.IsUnresolvedReference()) {
2815 // We must iterate over the fields, even if we cannot use mirror classes to do so. Do it
2816 // manually over the underlying dex file.
2817 uint32_t first_index = GetFirstFinalInstanceFieldIndex(*dex_file_,
2818 dex_file_->GetMethodId(dex_method_idx_).class_idx_);
2819 if (first_index != DexFile::kDexNoIndex) {
2820 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void-no-barrier not expected for field "
2821 << first_index;
2822 }
2823 break;
2824 }
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07002825 auto* klass = declaring_class.GetClass();
2826 for (uint32_t i = 0, num_fields = klass->NumInstanceFields(); i < num_fields; ++i) {
2827 if (klass->GetInstanceField(i)->IsFinal()) {
Mathieu Chartiere86deef2015-03-19 13:43:37 -07002828 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void-no-barrier not expected for "
2829 << PrettyField(klass->GetInstanceField(i));
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07002830 break;
2831 }
2832 }
Sebastien Hertzcc10e0e2013-06-28 14:24:48 +02002833 }
2834 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002835 // Note: the following instructions encode offsets derived from class linking.
2836 // As such they use Class*/Field*/AbstractMethod* as these offsets only have
2837 // meaning if the class linking and resolution were successful.
2838 case Instruction::IGET_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002839 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002840 break;
2841 case Instruction::IGET_WIDE_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002842 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002843 break;
2844 case Instruction::IGET_OBJECT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002845 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002846 break;
Mathieu Chartierffc605c2014-12-10 10:35:44 -08002847 case Instruction::IGET_BOOLEAN_QUICK:
2848 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true);
2849 break;
2850 case Instruction::IGET_BYTE_QUICK:
2851 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true);
2852 break;
2853 case Instruction::IGET_CHAR_QUICK:
2854 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true);
2855 break;
2856 case Instruction::IGET_SHORT_QUICK:
2857 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true);
2858 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002859 case Instruction::IPUT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002860 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002861 break;
Fred Shih37f05ef2014-07-16 18:38:08 -07002862 case Instruction::IPUT_BOOLEAN_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002863 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07002864 break;
2865 case Instruction::IPUT_BYTE_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002866 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07002867 break;
2868 case Instruction::IPUT_CHAR_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002869 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07002870 break;
2871 case Instruction::IPUT_SHORT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002872 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07002873 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002874 case Instruction::IPUT_WIDE_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002875 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002876 break;
2877 case Instruction::IPUT_OBJECT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002878 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002879 break;
2880 case Instruction::INVOKE_VIRTUAL_QUICK:
2881 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: {
2882 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002883 ArtMethod* called_method = VerifyInvokeVirtualQuickArgs(inst, is_range);
Ian Rogers7b078e82014-09-10 14:44:24 -07002884 if (called_method != nullptr) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002885 const char* descriptor = called_method->GetReturnTypeDescriptor();
Ian Rogersd8f69b02014-09-10 21:43:52 +00002886 const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002887 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002888 work_line_->SetResultRegisterType(this, return_type);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002889 } else {
2890 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2891 }
2892 just_set_result = true;
2893 }
2894 break;
2895 }
Igor Murashkin158f35c2015-06-10 15:55:30 -07002896 case Instruction::INVOKE_LAMBDA: {
2897 // Don't bother verifying, instead the interpreter will take the slow path with access checks.
2898 // If the code would've normally hard-failed, then the interpreter will throw the
2899 // appropriate verification errors at runtime.
2900 Fail(VERIFY_ERROR_FORCE_INTERPRETER); // TODO(iam): implement invoke-lambda verification
2901 break;
2902 }
2903 case Instruction::CREATE_LAMBDA: {
2904 // Don't bother verifying, instead the interpreter will take the slow path with access checks.
2905 // If the code would've normally hard-failed, then the interpreter will throw the
2906 // appropriate verification errors at runtime.
2907 Fail(VERIFY_ERROR_FORCE_INTERPRETER); // TODO(iam): implement create-lambda verification
2908 break;
2909 }
2910
Igor Murashkin2ee54e22015-06-18 10:05:11 -07002911 case Instruction::UNUSED_F4:
2912 case Instruction::UNUSED_F5:
2913 case Instruction::UNUSED_F7: {
Igor Murashkin158f35c2015-06-10 15:55:30 -07002914 DCHECK(false); // TODO(iam): Implement opcodes for lambdas
Igor Murashkin2ee54e22015-06-18 10:05:11 -07002915 // Conservatively fail verification on release builds.
2916 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Unexpected opcode " << inst->DumpString(dex_file_);
2917 break;
2918 }
2919
2920 case Instruction::BOX_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 box-lambda verification
2925 break;
2926 }
2927
2928 case Instruction::UNBOX_LAMBDA: {
2929 // Don't bother verifying, instead the interpreter will take the slow path with access checks.
2930 // If the code would've normally hard-failed, then the interpreter will throw the
2931 // appropriate verification errors at runtime.
2932 Fail(VERIFY_ERROR_FORCE_INTERPRETER); // TODO(iam): implement unbox-lambda verification
2933 break;
Igor Murashkin158f35c2015-06-10 15:55:30 -07002934 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002935
Ian Rogersd81871c2011-10-03 13:57:23 -07002936 /* These should never appear during verification. */
Mathieu Chartierffc605c2014-12-10 10:35:44 -08002937 case Instruction::UNUSED_3E ... Instruction::UNUSED_43:
Igor Murashkin158f35c2015-06-10 15:55:30 -07002938 case Instruction::UNUSED_FA ... Instruction::UNUSED_FF:
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002939 case Instruction::UNUSED_79:
2940 case Instruction::UNUSED_7A:
jeffhaod5347e02012-03-22 17:25:05 -07002941 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Unexpected opcode " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07002942 break;
2943
2944 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002945 * DO NOT add a "default" clause here. Without it the compiler will
jeffhaobdb76512011-09-07 11:43:16 -07002946 * complain if an instruction is missing (which is desirable).
2947 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002948 } // end - switch (dec_insn.opcode)
jeffhaobdb76512011-09-07 11:43:16 -07002949
Stephen Kyle7e541c92014-12-17 17:10:02 +00002950 /*
2951 * If we are in a constructor, and we had an UninitializedThis type
2952 * in a register somewhere, we need to make sure it wasn't overwritten.
2953 */
2954 if (track_uninitialized_this) {
2955 bool was_invoke_direct = (inst->Opcode() == Instruction::INVOKE_DIRECT ||
2956 inst->Opcode() == Instruction::INVOKE_DIRECT_RANGE);
2957 if (work_line_->WasUninitializedThisOverwritten(this, uninitialized_this_loc,
2958 was_invoke_direct)) {
2959 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
2960 << "Constructor failed to initialize this object";
2961 }
2962 }
2963
Ian Rogersad0b3a32012-04-16 14:50:24 -07002964 if (have_pending_hard_failure_) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08002965 if (Runtime::Current()->IsAotCompiler()) {
2966 /* When AOT compiling, check that the last failure is a hard failure */
Andreas Gampeb588f4c2015-05-26 13:35:39 -07002967 if (failures_[failures_.size() - 1] != VERIFY_ERROR_BAD_CLASS_HARD) {
2968 LOG(ERROR) << "Pending failures:";
2969 for (auto& error : failures_) {
2970 LOG(ERROR) << error;
2971 }
2972 for (auto& error_msg : failure_messages_) {
2973 LOG(ERROR) << error_msg->str();
2974 }
2975 LOG(FATAL) << "Pending hard failure, but last failure not hard.";
2976 }
Ian Rogerse1758fe2012-04-19 11:31:15 -07002977 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002978 /* immediate failure, reject class */
2979 info_messages_ << "Rejecting opcode " << inst->DumpString(dex_file_);
2980 return false;
jeffhaofaf459e2012-08-31 15:32:47 -07002981 } else if (have_pending_runtime_throw_failure_) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07002982 /* checking interpreter will throw, mark following code as unreachable */
jeffhaofaf459e2012-08-31 15:32:47 -07002983 opcode_flags = Instruction::kThrow;
Andreas Gamped12e7822015-06-25 10:26:40 -07002984 have_any_pending_runtime_throw_failure_ = true;
2985 // Reset the pending_runtime_throw flag. The flag is a global to decouple Fail and is per
2986 // instruction.
2987 have_pending_runtime_throw_failure_ = false;
jeffhaobdb76512011-09-07 11:43:16 -07002988 }
jeffhaobdb76512011-09-07 11:43:16 -07002989 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002990 * If we didn't just set the result register, clear it out. This ensures that you can only use
2991 * "move-result" immediately after the result is set. (We could check this statically, but it's
2992 * not expensive and it makes our debugging output cleaner.)
jeffhaobdb76512011-09-07 11:43:16 -07002993 */
2994 if (!just_set_result) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002995 work_line_->SetResultTypeToUnknown(this);
jeffhaobdb76512011-09-07 11:43:16 -07002996 }
2997
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002998
jeffhaobdb76512011-09-07 11:43:16 -07002999
3000 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003001 * Handle "branch". Tag the branch target.
jeffhaobdb76512011-09-07 11:43:16 -07003002 *
3003 * NOTE: instructions like Instruction::EQZ provide information about the
jeffhaod1f0fde2011-09-08 17:25:33 -07003004 * state of the register when the branch is taken or not taken. For example,
jeffhaobdb76512011-09-07 11:43:16 -07003005 * somebody could get a reference field, check it for zero, and if the
3006 * branch is taken immediately store that register in a boolean field
jeffhaod1f0fde2011-09-08 17:25:33 -07003007 * since the value is known to be zero. We do not currently account for
jeffhaobdb76512011-09-07 11:43:16 -07003008 * that, and will reject the code.
3009 *
3010 * TODO: avoid re-fetching the branch target
3011 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003012 if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07003013 bool isConditional, selfOkay;
Ian Rogersd81871c2011-10-03 13:57:23 -07003014 if (!GetBranchOffset(work_insn_idx_, &branch_target, &isConditional, &selfOkay)) {
jeffhaobdb76512011-09-07 11:43:16 -07003015 /* should never happen after static verification */
jeffhaod5347e02012-03-22 17:25:05 -07003016 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad branch";
jeffhaobdb76512011-09-07 11:43:16 -07003017 return false;
3018 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08003019 DCHECK_EQ(isConditional, (opcode_flags & Instruction::kContinue) != 0);
Stephen Kyle9bc61992014-09-22 13:53:15 +01003020 if (!CheckNotMoveExceptionOrMoveResult(code_item_->insns_, work_insn_idx_ + branch_target)) {
jeffhaobdb76512011-09-07 11:43:16 -07003021 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07003022 }
jeffhaobdb76512011-09-07 11:43:16 -07003023 /* update branch target, set "changed" if appropriate */
Ian Rogers7b078e82014-09-10 14:44:24 -07003024 if (nullptr != branch_line.get()) {
Ian Rogersebbdd872014-07-07 23:53:08 -07003025 if (!UpdateRegisters(work_insn_idx_ + branch_target, branch_line.get(), false)) {
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003026 return false;
3027 }
3028 } else {
Ian Rogersebbdd872014-07-07 23:53:08 -07003029 if (!UpdateRegisters(work_insn_idx_ + branch_target, work_line_.get(), false)) {
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003030 return false;
3031 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003032 }
jeffhaobdb76512011-09-07 11:43:16 -07003033 }
3034
3035 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003036 * Handle "switch". Tag all possible branch targets.
jeffhaobdb76512011-09-07 11:43:16 -07003037 *
3038 * We've already verified that the table is structurally sound, so we
3039 * just need to walk through and tag the targets.
3040 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003041 if ((opcode_flags & Instruction::kSwitch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07003042 int offset_to_switch = insns[1] | (((int32_t) insns[2]) << 16);
3043 const uint16_t* switch_insns = insns + offset_to_switch;
3044 int switch_count = switch_insns[1];
3045 int offset_to_targets, targ;
3046
3047 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
3048 /* 0 = sig, 1 = count, 2/3 = first key */
3049 offset_to_targets = 4;
3050 } else {
3051 /* 0 = sig, 1 = count, 2..count * 2 = keys */
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07003052 DCHECK((*insns & 0xff) == Instruction::SPARSE_SWITCH);
jeffhaobdb76512011-09-07 11:43:16 -07003053 offset_to_targets = 2 + 2 * switch_count;
3054 }
3055
3056 /* verify each switch target */
3057 for (targ = 0; targ < switch_count; targ++) {
3058 int offset;
3059 uint32_t abs_offset;
3060
3061 /* offsets are 32-bit, and only partly endian-swapped */
3062 offset = switch_insns[offset_to_targets + targ * 2] |
3063 (((int32_t) switch_insns[offset_to_targets + targ * 2 + 1]) << 16);
Ian Rogersd81871c2011-10-03 13:57:23 -07003064 abs_offset = work_insn_idx_ + offset;
3065 DCHECK_LT(abs_offset, code_item_->insns_size_in_code_units_);
Stephen Kyle9bc61992014-09-22 13:53:15 +01003066 if (!CheckNotMoveExceptionOrMoveResult(code_item_->insns_, abs_offset)) {
jeffhaobdb76512011-09-07 11:43:16 -07003067 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07003068 }
Ian Rogersebbdd872014-07-07 23:53:08 -07003069 if (!UpdateRegisters(abs_offset, work_line_.get(), false)) {
jeffhaobdb76512011-09-07 11:43:16 -07003070 return false;
Ian Rogersebbdd872014-07-07 23:53:08 -07003071 }
jeffhaobdb76512011-09-07 11:43:16 -07003072 }
3073 }
3074
3075 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003076 * Handle instructions that can throw and that are sitting in a "try" block. (If they're not in a
3077 * "try" block when they throw, control transfers out of the method.)
jeffhaobdb76512011-09-07 11:43:16 -07003078 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003079 if ((opcode_flags & Instruction::kThrow) != 0 && insn_flags_[work_insn_idx_].IsInTry()) {
Andreas Gampef91baf12014-07-18 15:41:00 -07003080 bool has_catch_all_handler = false;
Ian Rogers0571d352011-11-03 19:51:38 -07003081 CatchHandlerIterator iterator(*code_item_, work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07003082
Andreas Gampef91baf12014-07-18 15:41:00 -07003083 // Need the linker to try and resolve the handled class to check if it's Throwable.
3084 ClassLinker* linker = Runtime::Current()->GetClassLinker();
3085
Ian Rogers0571d352011-11-03 19:51:38 -07003086 for (; iterator.HasNext(); iterator.Next()) {
Andreas Gampef91baf12014-07-18 15:41:00 -07003087 uint16_t handler_type_idx = iterator.GetHandlerTypeIndex();
3088 if (handler_type_idx == DexFile::kDexNoIndex16) {
3089 has_catch_all_handler = true;
3090 } else {
3091 // It is also a catch-all if it is java.lang.Throwable.
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003092 mirror::Class* klass = linker->ResolveType(*dex_file_, handler_type_idx, dex_cache_,
3093 class_loader_);
Andreas Gampef91baf12014-07-18 15:41:00 -07003094 if (klass != nullptr) {
3095 if (klass == mirror::Throwable::GetJavaLangThrowable()) {
3096 has_catch_all_handler = true;
3097 }
3098 } else {
3099 // Clear exception.
Ian Rogers7b078e82014-09-10 14:44:24 -07003100 DCHECK(self_->IsExceptionPending());
3101 self_->ClearException();
Andreas Gampef91baf12014-07-18 15:41:00 -07003102 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003103 }
jeffhaobdb76512011-09-07 11:43:16 -07003104 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003105 * Merge registers into the "catch" block. We want to use the "savedRegs" rather than
3106 * "work_regs", because at runtime the exception will be thrown before the instruction
3107 * modifies any registers.
jeffhaobdb76512011-09-07 11:43:16 -07003108 */
Ian Rogersebbdd872014-07-07 23:53:08 -07003109 if (!UpdateRegisters(iterator.GetHandlerAddress(), saved_line_.get(), false)) {
jeffhaobdb76512011-09-07 11:43:16 -07003110 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07003111 }
jeffhaobdb76512011-09-07 11:43:16 -07003112 }
3113
3114 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003115 * If the monitor stack depth is nonzero, there must be a "catch all" handler for this
3116 * instruction. This does apply to monitor-exit because of async exception handling.
jeffhaobdb76512011-09-07 11:43:16 -07003117 */
Andreas Gampef91baf12014-07-18 15:41:00 -07003118 if (work_line_->MonitorStackDepth() > 0 && !has_catch_all_handler) {
jeffhaobdb76512011-09-07 11:43:16 -07003119 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003120 * The state in work_line reflects the post-execution state. If the current instruction is a
3121 * monitor-enter and the monitor stack was empty, we don't need a catch-all (if it throws,
jeffhaobdb76512011-09-07 11:43:16 -07003122 * it will do so before grabbing the lock).
3123 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02003124 if (inst->Opcode() != Instruction::MONITOR_ENTER || work_line_->MonitorStackDepth() != 1) {
jeffhaod5347e02012-03-22 17:25:05 -07003125 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
Ian Rogersd81871c2011-10-03 13:57:23 -07003126 << "expected to be within a catch-all for an instruction where a monitor is held";
jeffhaobdb76512011-09-07 11:43:16 -07003127 return false;
3128 }
3129 }
3130 }
3131
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003132 /* Handle "continue". Tag the next consecutive instruction.
3133 * Note: Keep the code handling "continue" case below the "branch" and "switch" cases,
3134 * because it changes work_line_ when performing peephole optimization
3135 * and this change should not be used in those cases.
3136 */
Ian Rogers6d376ae2013-07-23 15:12:40 -07003137 if ((opcode_flags & Instruction::kContinue) != 0) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003138 DCHECK_EQ(Instruction::At(code_item_->insns_ + work_insn_idx_), inst);
3139 uint32_t next_insn_idx = work_insn_idx_ + inst->SizeInCodeUnits();
Ian Rogers6d376ae2013-07-23 15:12:40 -07003140 if (next_insn_idx >= code_item_->insns_size_in_code_units_) {
3141 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Execution can walk off end of code area";
3142 return false;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003143 }
Ian Rogers6d376ae2013-07-23 15:12:40 -07003144 // The only way to get to a move-exception instruction is to get thrown there. Make sure the
3145 // next instruction isn't one.
3146 if (!CheckNotMoveException(code_item_->insns_, next_insn_idx)) {
3147 return false;
3148 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003149 if (nullptr != fallthrough_line.get()) {
Ian Rogers6d376ae2013-07-23 15:12:40 -07003150 // Make workline consistent with fallthrough computed from peephole optimization.
3151 work_line_->CopyFromLine(fallthrough_line.get());
3152 }
Ian Rogersb8c78592013-07-25 23:52:52 +00003153 if (insn_flags_[next_insn_idx].IsReturn()) {
3154 // For returns we only care about the operand to the return, all other registers are dead.
3155 const Instruction* ret_inst = Instruction::At(code_item_->insns_ + next_insn_idx);
3156 Instruction::Code opcode = ret_inst->Opcode();
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07003157 if (opcode == Instruction::RETURN_VOID || opcode == Instruction::RETURN_VOID_NO_BARRIER) {
Stephen Kyle7e541c92014-12-17 17:10:02 +00003158 SafelyMarkAllRegistersAsConflicts(this, work_line_.get());
Ian Rogersb8c78592013-07-25 23:52:52 +00003159 } else {
3160 if (opcode == Instruction::RETURN_WIDE) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003161 work_line_->MarkAllRegistersAsConflictsExceptWide(this, ret_inst->VRegA_11x());
Ian Rogersb8c78592013-07-25 23:52:52 +00003162 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07003163 work_line_->MarkAllRegistersAsConflictsExcept(this, ret_inst->VRegA_11x());
Ian Rogersb8c78592013-07-25 23:52:52 +00003164 }
3165 }
3166 }
Ian Rogers6d376ae2013-07-23 15:12:40 -07003167 RegisterLine* next_line = reg_table_.GetLine(next_insn_idx);
Ian Rogers7b078e82014-09-10 14:44:24 -07003168 if (next_line != nullptr) {
Ian Rogersebbdd872014-07-07 23:53:08 -07003169 // Merge registers into what we have for the next instruction, and set the "changed" flag if
3170 // needed. If the merge changes the state of the registers then the work line will be
3171 // updated.
3172 if (!UpdateRegisters(next_insn_idx, work_line_.get(), true)) {
Ian Rogers6d376ae2013-07-23 15:12:40 -07003173 return false;
3174 }
3175 } else {
3176 /*
3177 * We're not recording register data for the next instruction, so we don't know what the
3178 * prior state was. We have to assume that something has changed and re-evaluate it.
3179 */
3180 insn_flags_[next_insn_idx].SetChanged();
3181 }
3182 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003183
jeffhaod1f0fde2011-09-08 17:25:33 -07003184 /* If we're returning from the method, make sure monitor stack is empty. */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003185 if ((opcode_flags & Instruction::kReturn) != 0) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003186 if (!work_line_->VerifyMonitorStackEmpty(this)) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003187 return false;
3188 }
jeffhaobdb76512011-09-07 11:43:16 -07003189 }
3190
3191 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003192 * Update start_guess. Advance to the next instruction of that's
3193 * possible, otherwise use the branch target if one was found. If
jeffhaobdb76512011-09-07 11:43:16 -07003194 * neither of those exists we're in a return or throw; leave start_guess
3195 * alone and let the caller sort it out.
3196 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003197 if ((opcode_flags & Instruction::kContinue) != 0) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003198 DCHECK_EQ(Instruction::At(code_item_->insns_ + work_insn_idx_), inst);
3199 *start_guess = work_insn_idx_ + inst->SizeInCodeUnits();
Elliott Hughesadb8c672012-03-06 16:49:32 -08003200 } else if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07003201 /* we're still okay if branch_target is zero */
Ian Rogersd81871c2011-10-03 13:57:23 -07003202 *start_guess = work_insn_idx_ + branch_target;
jeffhaobdb76512011-09-07 11:43:16 -07003203 }
3204
Ian Rogersd81871c2011-10-03 13:57:23 -07003205 DCHECK_LT(*start_guess, code_item_->insns_size_in_code_units_);
3206 DCHECK(insn_flags_[*start_guess].IsOpcode());
jeffhaobdb76512011-09-07 11:43:16 -07003207
3208 return true;
Brian Carlstrom7934ac22013-07-26 10:54:15 -07003209} // NOLINT(readability/fn_size)
jeffhaobdb76512011-09-07 11:43:16 -07003210
Ian Rogersd8f69b02014-09-10 21:43:52 +00003211const RegType& MethodVerifier::ResolveClassAndCheckAccess(uint32_t class_idx) {
Ian Rogers0571d352011-11-03 19:51:38 -07003212 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
Ian Rogersd8f69b02014-09-10 21:43:52 +00003213 const RegType& referrer = GetDeclaringClass();
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003214 mirror::Class* klass = dex_cache_->GetResolvedType(class_idx);
Ian Rogers7b078e82014-09-10 14:44:24 -07003215 const RegType& result = klass != nullptr ?
Andreas Gampef23f33d2015-06-23 14:18:17 -07003216 FromClass(descriptor, klass, klass->CannotBeAssignedFromOtherTypes()) :
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003217 reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003218 if (result.IsConflict()) {
3219 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "accessing broken descriptor '" << descriptor
3220 << "' in " << referrer;
3221 return result;
3222 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003223 if (klass == nullptr && !result.IsUnresolvedTypes()) {
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003224 dex_cache_->SetResolvedType(class_idx, result.GetClass());
Ian Rogerse1758fe2012-04-19 11:31:15 -07003225 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003226 // Check if access is allowed. Unresolved types use xxxWithAccessCheck to
Jeff Haob24b4a72013-07-31 13:47:31 -07003227 // check at runtime if access is allowed and so pass here. If result is
3228 // primitive, skip the access check.
3229 if (result.IsNonZeroReferenceTypes() && !result.IsUnresolvedTypes() &&
3230 !referrer.IsUnresolvedTypes() && !referrer.CanAccess(result)) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07003231 Fail(VERIFY_ERROR_ACCESS_CLASS) << "illegal class access: '"
Ian Rogersad0b3a32012-04-16 14:50:24 -07003232 << referrer << "' -> '" << result << "'";
Ian Rogers28ad40d2011-10-27 15:19:26 -07003233 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003234 return result;
Ian Rogersd81871c2011-10-03 13:57:23 -07003235}
3236
Ian Rogersd8f69b02014-09-10 21:43:52 +00003237const RegType& MethodVerifier::GetCaughtExceptionType() {
Ian Rogers7b078e82014-09-10 14:44:24 -07003238 const RegType* common_super = nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003239 if (code_item_->tries_size_ != 0) {
Ian Rogers13735952014-10-08 12:43:28 -07003240 const uint8_t* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
Ian Rogersd81871c2011-10-03 13:57:23 -07003241 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
3242 for (uint32_t i = 0; i < handlers_size; i++) {
Ian Rogers0571d352011-11-03 19:51:38 -07003243 CatchHandlerIterator iterator(handlers_ptr);
3244 for (; iterator.HasNext(); iterator.Next()) {
3245 if (iterator.GetHandlerAddress() == (uint32_t) work_insn_idx_) {
3246 if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) {
Ian Rogersb4903572012-10-11 11:52:56 -07003247 common_super = &reg_types_.JavaLangThrowable(false);
Ian Rogersd81871c2011-10-03 13:57:23 -07003248 } else {
Ian Rogersd8f69b02014-09-10 21:43:52 +00003249 const RegType& exception = ResolveClassAndCheckAccess(iterator.GetHandlerTypeIndex());
Jeff Haob878f212014-04-24 16:25:36 -07003250 if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(exception)) {
Jeff Haoc26a56c2013-11-04 12:00:47 -08003251 if (exception.IsUnresolvedTypes()) {
3252 // We don't know enough about the type. Fail here and let runtime handle it.
3253 Fail(VERIFY_ERROR_NO_CLASS) << "unresolved exception class " << exception;
3254 return exception;
3255 } else {
3256 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unexpected non-exception class " << exception;
3257 return reg_types_.Conflict();
3258 }
Jeff Haob878f212014-04-24 16:25:36 -07003259 } else if (common_super == nullptr) {
3260 common_super = &exception;
Ian Rogers28ad40d2011-10-27 15:19:26 -07003261 } else if (common_super->Equals(exception)) {
Ian Rogersc4762272012-02-01 15:55:55 -08003262 // odd case, but nothing to do
Ian Rogersd81871c2011-10-03 13:57:23 -07003263 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07003264 common_super = &common_super->Merge(exception, &reg_types_);
Andreas Gampe7c038102014-10-27 20:08:46 -07003265 if (FailOrAbort(this,
3266 reg_types_.JavaLangThrowable(false).IsAssignableFrom(*common_super),
3267 "java.lang.Throwable is not assignable-from common_super at ",
3268 work_insn_idx_)) {
3269 break;
3270 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003271 }
3272 }
3273 }
3274 }
Ian Rogers0571d352011-11-03 19:51:38 -07003275 handlers_ptr = iterator.EndDataPointer();
Ian Rogersd81871c2011-10-03 13:57:23 -07003276 }
3277 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003278 if (common_super == nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003279 /* no catch blocks, or no catches with classes we can find */
jeffhaod5347e02012-03-22 17:25:05 -07003280 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unable to find exception handler";
Ian Rogersad0b3a32012-04-16 14:50:24 -07003281 return reg_types_.Conflict();
Ian Rogersd81871c2011-10-03 13:57:23 -07003282 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07003283 return *common_super;
Ian Rogersd81871c2011-10-03 13:57:23 -07003284}
3285
Mathieu Chartiere401d142015-04-22 13:56:20 -07003286ArtMethod* MethodVerifier::ResolveMethodAndCheckAccess(
3287 uint32_t dex_method_idx, MethodType method_type) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003288 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx);
Ian Rogersd8f69b02014-09-10 21:43:52 +00003289 const RegType& klass_type = ResolveClassAndCheckAccess(method_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003290 if (klass_type.IsConflict()) {
3291 std::string append(" in attempt to access method ");
3292 append += dex_file_->GetMethodName(method_id);
3293 AppendToLastFailMessage(append);
Ian Rogers7b078e82014-09-10 14:44:24 -07003294 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08003295 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003296 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003297 return nullptr; // Can't resolve Class so no more to do here
Ian Rogers90040192011-12-16 08:54:29 -08003298 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003299 mirror::Class* klass = klass_type.GetClass();
Ian Rogersd8f69b02014-09-10 21:43:52 +00003300 const RegType& referrer = GetDeclaringClass();
Mathieu Chartiere401d142015-04-22 13:56:20 -07003301 auto* cl = Runtime::Current()->GetClassLinker();
3302 auto pointer_size = cl->GetImagePointerSize();
3303 ArtMethod* res_method = dex_cache_->GetResolvedMethod(dex_method_idx, pointer_size);
Ian Rogers7b078e82014-09-10 14:44:24 -07003304 if (res_method == nullptr) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07003305 const char* name = dex_file_->GetMethodName(method_id);
Ian Rogersd91d6d62013-09-25 20:26:14 -07003306 const Signature signature = dex_file_->GetMethodSignature(method_id);
jeffhao8cd6dda2012-02-22 10:15:34 -08003307
3308 if (method_type == METHOD_DIRECT || method_type == METHOD_STATIC) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003309 res_method = klass->FindDirectMethod(name, signature, pointer_size);
jeffhao8cd6dda2012-02-22 10:15:34 -08003310 } else if (method_type == METHOD_INTERFACE) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003311 res_method = klass->FindInterfaceMethod(name, signature, pointer_size);
Ian Rogersd81871c2011-10-03 13:57:23 -07003312 } else {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003313 res_method = klass->FindVirtualMethod(name, signature, pointer_size);
Ian Rogersd81871c2011-10-03 13:57:23 -07003314 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003315 if (res_method != nullptr) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003316 dex_cache_->SetResolvedMethod(dex_method_idx, res_method, pointer_size);
Ian Rogersd81871c2011-10-03 13:57:23 -07003317 } else {
jeffhao8cd6dda2012-02-22 10:15:34 -08003318 // If a virtual or interface method wasn't found with the expected type, look in
3319 // the direct methods. This can happen when the wrong invoke type is used or when
3320 // a class has changed, and will be flagged as an error in later checks.
3321 if (method_type == METHOD_INTERFACE || method_type == METHOD_VIRTUAL) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003322 res_method = klass->FindDirectMethod(name, signature, pointer_size);
jeffhao8cd6dda2012-02-22 10:15:34 -08003323 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003324 if (res_method == nullptr) {
jeffhao8cd6dda2012-02-22 10:15:34 -08003325 Fail(VERIFY_ERROR_NO_METHOD) << "couldn't find method "
3326 << PrettyDescriptor(klass) << "." << name
3327 << " " << signature;
Ian Rogers7b078e82014-09-10 14:44:24 -07003328 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003329 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003330 }
3331 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003332 // Make sure calls to constructors are "direct". There are additional restrictions but we don't
3333 // enforce them here.
3334 if (res_method->IsConstructor() && method_type != METHOD_DIRECT) {
jeffhaod5347e02012-03-22 17:25:05 -07003335 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting non-direct call to constructor "
3336 << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003337 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003338 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003339 // Disallow any calls to class initializers.
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003340 if (res_method->IsClassInitializer()) {
jeffhaod5347e02012-03-22 17:25:05 -07003341 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting call to class initializer "
3342 << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003343 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003344 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003345 // Check if access is allowed.
Ian Rogersad0b3a32012-04-16 14:50:24 -07003346 if (!referrer.CanAccessMember(res_method->GetDeclaringClass(), res_method->GetAccessFlags())) {
jeffhao8cd6dda2012-02-22 10:15:34 -08003347 Fail(VERIFY_ERROR_ACCESS_METHOD) << "illegal method access (call " << PrettyMethod(res_method)
Ian Rogersad0b3a32012-04-16 14:50:24 -07003348 << " from " << referrer << ")";
jeffhaob57e9522012-04-26 18:08:21 -07003349 return res_method;
jeffhao8cd6dda2012-02-22 10:15:34 -08003350 }
jeffhaode0d9c92012-02-27 13:58:13 -08003351 // Check that invoke-virtual and invoke-super are not used on private methods of the same class.
3352 if (res_method->IsPrivate() && method_type == METHOD_VIRTUAL) {
jeffhaod5347e02012-03-22 17:25:05 -07003353 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invoke-super/virtual can't be used on private method "
3354 << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003355 return nullptr;
jeffhaode0d9c92012-02-27 13:58:13 -08003356 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003357 // Check that interface methods match interface classes.
3358 if (klass->IsInterface() && method_type != METHOD_INTERFACE) {
3359 Fail(VERIFY_ERROR_CLASS_CHANGE) << "non-interface method " << PrettyMethod(res_method)
3360 << " is in an interface class " << PrettyClass(klass);
Ian Rogers7b078e82014-09-10 14:44:24 -07003361 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003362 } else if (!klass->IsInterface() && method_type == METHOD_INTERFACE) {
3363 Fail(VERIFY_ERROR_CLASS_CHANGE) << "interface method " << PrettyMethod(res_method)
3364 << " is in a non-interface class " << PrettyClass(klass);
Ian Rogers7b078e82014-09-10 14:44:24 -07003365 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003366 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003367 // See if the method type implied by the invoke instruction matches the access flags for the
3368 // target method.
Brian Carlstrombe6fa5e2014-12-09 20:15:42 -08003369 if ((method_type == METHOD_DIRECT && (!res_method->IsDirect() || res_method->IsStatic())) ||
Ian Rogersd81871c2011-10-03 13:57:23 -07003370 (method_type == METHOD_STATIC && !res_method->IsStatic()) ||
3371 ((method_type == METHOD_VIRTUAL || method_type == METHOD_INTERFACE) && res_method->IsDirect())
3372 ) {
Ian Rogers2fc14272012-08-30 10:56:57 -07003373 Fail(VERIFY_ERROR_CLASS_CHANGE) << "invoke type (" << method_type << ") does not match method "
3374 " type of " << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003375 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003376 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003377 return res_method;
3378}
3379
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003380template <class T>
Mathieu Chartiere401d142015-04-22 13:56:20 -07003381ArtMethod* MethodVerifier::VerifyInvocationArgsFromIterator(
3382 T* it, const Instruction* inst, MethodType method_type, bool is_range, ArtMethod* res_method) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003383 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
3384 // match the call to the signature. Also, we might be calling through an abstract method
3385 // definition (which doesn't have register count values).
3386 const size_t expected_args = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
3387 /* caught by static verifier */
3388 DCHECK(is_range || expected_args <= 5);
3389 if (expected_args > code_item_->outs_size_) {
3390 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
3391 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
3392 return nullptr;
3393 }
3394
3395 uint32_t arg[5];
3396 if (!is_range) {
3397 inst->GetVarArgs(arg);
3398 }
3399 uint32_t sig_registers = 0;
3400
3401 /*
3402 * Check the "this" argument, which must be an instance of the class that declared the method.
3403 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
3404 * rigorous check here (which is okay since we have to do it at runtime).
3405 */
3406 if (method_type != METHOD_STATIC) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003407 const RegType& actual_arg_type = work_line_->GetInvocationThis(this, inst, is_range);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003408 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
3409 CHECK(have_pending_hard_failure_);
3410 return nullptr;
3411 }
3412 if (actual_arg_type.IsUninitializedReference()) {
3413 if (res_method) {
3414 if (!res_method->IsConstructor()) {
3415 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
3416 return nullptr;
3417 }
3418 } else {
3419 // Check whether the name of the called method is "<init>"
3420 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Jeff Hao0d087272014-08-04 14:47:17 -07003421 if (strcmp(dex_file_->GetMethodName(dex_file_->GetMethodId(method_idx)), "<init>") != 0) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003422 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
3423 return nullptr;
3424 }
3425 }
3426 }
3427 if (method_type != METHOD_INTERFACE && !actual_arg_type.IsZero()) {
Ian Rogersd8f69b02014-09-10 21:43:52 +00003428 const RegType* res_method_class;
Andreas Gamped9e23012015-06-04 22:19:58 -07003429 // Miranda methods have the declaring interface as their declaring class, not the abstract
3430 // class. It would be wrong to use this for the type check (interface type checks are
3431 // postponed to runtime).
3432 if (res_method != nullptr && !res_method->IsMiranda()) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003433 mirror::Class* klass = res_method->GetDeclaringClass();
Ian Rogers1ff3c982014-08-12 02:30:58 -07003434 std::string temp;
Andreas Gampef23f33d2015-06-23 14:18:17 -07003435 res_method_class = &FromClass(klass->GetDescriptor(&temp), klass,
3436 klass->CannotBeAssignedFromOtherTypes());
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003437 } else {
3438 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
3439 const uint16_t class_idx = dex_file_->GetMethodId(method_idx).class_idx_;
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003440 res_method_class = &reg_types_.FromDescriptor(GetClassLoader(),
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003441 dex_file_->StringByTypeIdx(class_idx),
3442 false);
3443 }
3444 if (!res_method_class->IsAssignableFrom(actual_arg_type)) {
3445 Fail(actual_arg_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS:
3446 VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type
3447 << "' not instance of '" << *res_method_class << "'";
3448 // Continue on soft failures. We need to find possible hard failures to avoid problems in
3449 // the compiler.
3450 if (have_pending_hard_failure_) {
3451 return nullptr;
3452 }
3453 }
3454 }
3455 sig_registers = 1;
3456 }
3457
3458 for ( ; it->HasNext(); it->Next()) {
3459 if (sig_registers >= expected_args) {
3460 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, expected " << inst->VRegA() <<
3461 " arguments, found " << sig_registers << " or more.";
3462 return nullptr;
3463 }
3464
3465 const char* param_descriptor = it->GetDescriptor();
3466
3467 if (param_descriptor == nullptr) {
3468 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation because of missing signature "
3469 "component";
3470 return nullptr;
3471 }
3472
Ian Rogersd8f69b02014-09-10 21:43:52 +00003473 const RegType& reg_type = reg_types_.FromDescriptor(GetClassLoader(), param_descriptor, false);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003474 uint32_t get_reg = is_range ? inst->VRegC_3rc() + static_cast<uint32_t>(sig_registers) :
3475 arg[sig_registers];
3476 if (reg_type.IsIntegralTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003477 const RegType& src_type = work_line_->GetRegisterType(this, get_reg);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003478 if (!src_type.IsIntegralTypes()) {
3479 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register v" << get_reg << " has type " << src_type
3480 << " but expected " << reg_type;
Andreas Gampeb588f4c2015-05-26 13:35:39 -07003481 return nullptr;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003482 }
Andreas Gampeda9badb2015-06-05 20:22:12 -07003483 } else {
3484 if (!work_line_->VerifyRegisterType(this, get_reg, reg_type)) {
3485 // Continue on soft failures. We need to find possible hard failures to avoid problems in
3486 // the compiler.
3487 if (have_pending_hard_failure_) {
3488 return nullptr;
3489 }
3490 } else if (reg_type.IsLongOrDoubleTypes()) {
3491 // Check that registers are consecutive (for non-range invokes). Invokes are the only
3492 // instructions not specifying register pairs by the first component, but require them
3493 // nonetheless. Only check when there's an actual register in the parameters. If there's
3494 // none, this will fail below.
3495 if (!is_range && sig_registers + 1 < expected_args) {
3496 uint32_t second_reg = arg[sig_registers + 1];
3497 if (second_reg != get_reg + 1) {
3498 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, long or double parameter "
3499 "at index " << sig_registers << " is not a pair: " << get_reg << " + "
3500 << second_reg << ".";
3501 return nullptr;
3502 }
3503 }
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003504 }
3505 }
3506 sig_registers += reg_type.IsLongOrDoubleTypes() ? 2 : 1;
3507 }
3508 if (expected_args != sig_registers) {
3509 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, expected " << expected_args <<
3510 " arguments, found " << sig_registers;
3511 return nullptr;
3512 }
3513 return res_method;
3514}
3515
3516void MethodVerifier::VerifyInvocationArgsUnresolvedMethod(const Instruction* inst,
3517 MethodType method_type,
3518 bool is_range) {
3519 // As the method may not have been resolved, make this static check against what we expect.
3520 // The main reason for this code block is to fail hard when we find an illegal use, e.g.,
3521 // wrong number of arguments or wrong primitive types, even if the method could not be resolved.
3522 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
3523 DexFileParameterIterator it(*dex_file_,
3524 dex_file_->GetProtoId(dex_file_->GetMethodId(method_idx).proto_idx_));
3525 VerifyInvocationArgsFromIterator<DexFileParameterIterator>(&it, inst, method_type, is_range,
3526 nullptr);
3527}
3528
3529class MethodParamListDescriptorIterator {
3530 public:
Mathieu Chartiere401d142015-04-22 13:56:20 -07003531 explicit MethodParamListDescriptorIterator(ArtMethod* res_method) :
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003532 res_method_(res_method), pos_(0), params_(res_method->GetParameterTypeList()),
3533 params_size_(params_ == nullptr ? 0 : params_->Size()) {
3534 }
3535
3536 bool HasNext() {
3537 return pos_ < params_size_;
3538 }
3539
3540 void Next() {
3541 ++pos_;
3542 }
3543
3544 const char* GetDescriptor() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
3545 return res_method_->GetTypeDescriptorFromTypeIdx(params_->GetTypeItem(pos_).type_idx_);
3546 }
3547
3548 private:
Mathieu Chartiere401d142015-04-22 13:56:20 -07003549 ArtMethod* res_method_;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003550 size_t pos_;
3551 const DexFile::TypeList* params_;
3552 const size_t params_size_;
3553};
3554
Mathieu Chartiere401d142015-04-22 13:56:20 -07003555ArtMethod* MethodVerifier::VerifyInvocationArgs(
3556 const Instruction* inst, MethodType method_type, bool is_range, bool is_super) {
jeffhao8cd6dda2012-02-22 10:15:34 -08003557 // Resolve the method. This could be an abstract or concrete method depending on what sort of call
3558 // we're making.
Sebastien Hertz5243e912013-05-21 10:55:07 +02003559 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Andreas Gampeacc4d2f2014-06-12 19:35:05 -07003560
Mathieu Chartiere401d142015-04-22 13:56:20 -07003561 ArtMethod* res_method = ResolveMethodAndCheckAccess(method_idx, method_type);
Ian Rogers7b078e82014-09-10 14:44:24 -07003562 if (res_method == nullptr) { // error or class is unresolved
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003563 // Check what we can statically.
3564 if (!have_pending_hard_failure_) {
3565 VerifyInvocationArgsUnresolvedMethod(inst, method_type, is_range);
3566 }
3567 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003568 }
3569
Ian Rogersd81871c2011-10-03 13:57:23 -07003570 // If we're using invoke-super(method), make sure that the executing method's class' superclass
3571 // has a vtable entry for the target method.
3572 if (is_super) {
3573 DCHECK(method_type == METHOD_VIRTUAL);
Ian Rogersd8f69b02014-09-10 21:43:52 +00003574 const RegType& super = GetDeclaringClass().GetSuperClass(&reg_types_);
Ian Rogers529781d2012-07-23 17:24:29 -07003575 if (super.IsUnresolvedTypes()) {
jeffhao4d8df822012-04-24 17:09:36 -07003576 Fail(VERIFY_ERROR_NO_METHOD) << "unknown super class in invoke-super from "
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003577 << PrettyMethod(dex_method_idx_, *dex_file_)
jeffhao4d8df822012-04-24 17:09:36 -07003578 << " to super " << PrettyMethod(res_method);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003579 return nullptr;
jeffhao4d8df822012-04-24 17:09:36 -07003580 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003581 mirror::Class* super_klass = super.GetClass();
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003582 if (res_method->GetMethodIndex() >= super_klass->GetVTableLength()) {
jeffhao4d8df822012-04-24 17:09:36 -07003583 Fail(VERIFY_ERROR_NO_METHOD) << "invalid invoke-super from "
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003584 << PrettyMethod(dex_method_idx_, *dex_file_)
jeffhao4d8df822012-04-24 17:09:36 -07003585 << " to super " << super
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003586 << "." << res_method->GetName()
3587 << res_method->GetSignature();
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003588 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003589 }
3590 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003591
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003592 // Process the target method's signature. This signature may or may not
3593 MethodParamListDescriptorIterator it(res_method);
3594 return VerifyInvocationArgsFromIterator<MethodParamListDescriptorIterator>(&it, inst, method_type,
3595 is_range, res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07003596}
3597
Mathieu Chartiere401d142015-04-22 13:56:20 -07003598ArtMethod* MethodVerifier::GetQuickInvokedMethod(const Instruction* inst, RegisterLine* reg_line,
3599 bool is_range, bool allow_failure) {
Mathieu Chartier091d2382015-03-06 10:59:06 -08003600 if (is_range) {
3601 DCHECK_EQ(inst->Opcode(), Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
3602 } else {
3603 DCHECK_EQ(inst->Opcode(), Instruction::INVOKE_VIRTUAL_QUICK);
3604 }
3605 const RegType& actual_arg_type = reg_line->GetInvocationThis(this, inst, is_range, allow_failure);
Ian Rogers9bc54402014-04-17 16:40:01 -07003606 if (!actual_arg_type.HasClass()) {
3607 VLOG(verifier) << "Failed to get mirror::Class* from '" << actual_arg_type << "'";
Andreas Gampe63981562014-04-17 12:28:43 -07003608 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003609 }
Ian Rogersa4cf1df2014-05-07 19:47:17 -07003610 mirror::Class* klass = actual_arg_type.GetClass();
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003611 mirror::Class* dispatch_class;
Ian Rogersa4cf1df2014-05-07 19:47:17 -07003612 if (klass->IsInterface()) {
3613 // Derive Object.class from Class.class.getSuperclass().
3614 mirror::Class* object_klass = klass->GetClass()->GetSuperClass();
Andreas Gampe7c038102014-10-27 20:08:46 -07003615 if (FailOrAbort(this, object_klass->IsObjectClass(),
Mathieu Chartier091d2382015-03-06 10:59:06 -08003616 "Failed to find Object class in quickened invoke receiver", work_insn_idx_)) {
Andreas Gampe7c038102014-10-27 20:08:46 -07003617 return nullptr;
3618 }
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003619 dispatch_class = object_klass;
Ian Rogersa4cf1df2014-05-07 19:47:17 -07003620 } else {
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003621 dispatch_class = klass;
Ian Rogersa4cf1df2014-05-07 19:47:17 -07003622 }
Mathieu Chartier091d2382015-03-06 10:59:06 -08003623 if (!dispatch_class->HasVTable()) {
3624 FailOrAbort(this, allow_failure, "Receiver class has no vtable for quickened invoke at ",
3625 work_insn_idx_);
Andreas Gampe7c038102014-10-27 20:08:46 -07003626 return nullptr;
3627 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003628 uint16_t vtable_index = is_range ? inst->VRegB_3rc() : inst->VRegB_35c();
Mathieu Chartiere401d142015-04-22 13:56:20 -07003629 auto* cl = Runtime::Current()->GetClassLinker();
3630 auto pointer_size = cl->GetImagePointerSize();
Mathieu Chartier091d2382015-03-06 10:59:06 -08003631 if (static_cast<int32_t>(vtable_index) >= dispatch_class->GetVTableLength()) {
3632 FailOrAbort(this, allow_failure,
3633 "Receiver class has not enough vtable slots for quickened invoke at ",
3634 work_insn_idx_);
Andreas Gampe7c038102014-10-27 20:08:46 -07003635 return nullptr;
3636 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07003637 ArtMethod* res_method = dispatch_class->GetVTableEntry(vtable_index, pointer_size);
Mathieu Chartier091d2382015-03-06 10:59:06 -08003638 if (self_->IsExceptionPending()) {
3639 FailOrAbort(this, allow_failure, "Unexpected exception pending for quickened invoke at ",
3640 work_insn_idx_);
Andreas Gampe7c038102014-10-27 20:08:46 -07003641 return nullptr;
3642 }
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003643 return res_method;
3644}
3645
Mathieu Chartiere401d142015-04-22 13:56:20 -07003646ArtMethod* MethodVerifier::VerifyInvokeVirtualQuickArgs(const Instruction* inst, bool is_range) {
Andreas Gampe76bd8802014-12-10 16:43:58 -08003647 DCHECK(Runtime::Current()->IsStarted() || verify_to_dump_)
3648 << PrettyMethod(dex_method_idx_, *dex_file_, true) << "@" << work_insn_idx_;
3649
Mathieu Chartiere401d142015-04-22 13:56:20 -07003650 ArtMethod* res_method = GetQuickInvokedMethod(inst, work_line_.get(), is_range, false);
Ian Rogers7b078e82014-09-10 14:44:24 -07003651 if (res_method == nullptr) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003652 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer method from " << inst->Name();
Ian Rogers7b078e82014-09-10 14:44:24 -07003653 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003654 }
Andreas Gampe7c038102014-10-27 20:08:46 -07003655 if (FailOrAbort(this, !res_method->IsDirect(), "Quick-invoked method is direct at ",
3656 work_insn_idx_)) {
3657 return nullptr;
3658 }
3659 if (FailOrAbort(this, !res_method->IsStatic(), "Quick-invoked method is static at ",
3660 work_insn_idx_)) {
3661 return nullptr;
3662 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003663
3664 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
3665 // match the call to the signature. Also, we might be calling through an abstract method
3666 // definition (which doesn't have register count values).
Ian Rogers7b078e82014-09-10 14:44:24 -07003667 const RegType& actual_arg_type = work_line_->GetInvocationThis(this, inst, is_range);
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003668 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
Ian Rogers7b078e82014-09-10 14:44:24 -07003669 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003670 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003671 const size_t expected_args = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
3672 /* caught by static verifier */
3673 DCHECK(is_range || expected_args <= 5);
3674 if (expected_args > code_item_->outs_size_) {
3675 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
3676 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
Ian Rogers7b078e82014-09-10 14:44:24 -07003677 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003678 }
3679
3680 /*
3681 * Check the "this" argument, which must be an instance of the class that declared the method.
3682 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
3683 * rigorous check here (which is okay since we have to do it at runtime).
3684 */
3685 if (actual_arg_type.IsUninitializedReference() && !res_method->IsConstructor()) {
3686 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
Ian Rogers7b078e82014-09-10 14:44:24 -07003687 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003688 }
3689 if (!actual_arg_type.IsZero()) {
3690 mirror::Class* klass = res_method->GetDeclaringClass();
Ian Rogers1ff3c982014-08-12 02:30:58 -07003691 std::string temp;
Ian Rogersd8f69b02014-09-10 21:43:52 +00003692 const RegType& res_method_class =
Andreas Gampef23f33d2015-06-23 14:18:17 -07003693 FromClass(klass->GetDescriptor(&temp), klass, klass->CannotBeAssignedFromOtherTypes());
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003694 if (!res_method_class.IsAssignableFrom(actual_arg_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07003695 Fail(actual_arg_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS :
3696 VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003697 << "' not instance of '" << res_method_class << "'";
Ian Rogers7b078e82014-09-10 14:44:24 -07003698 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003699 }
3700 }
3701 /*
3702 * Process the target method's signature. This signature may or may not
3703 * have been verified, so we can't assume it's properly formed.
3704 */
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003705 const DexFile::TypeList* params = res_method->GetParameterTypeList();
Ian Rogers7b078e82014-09-10 14:44:24 -07003706 size_t params_size = params == nullptr ? 0 : params->Size();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003707 uint32_t arg[5];
3708 if (!is_range) {
Ian Rogers29a26482014-05-02 15:27:29 -07003709 inst->GetVarArgs(arg);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003710 }
3711 size_t actual_args = 1;
3712 for (size_t param_index = 0; param_index < params_size; param_index++) {
3713 if (actual_args >= expected_args) {
3714 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invalid call to '" << PrettyMethod(res_method)
Brian Carlstrom93c33962013-07-26 10:37:43 -07003715 << "'. Expected " << expected_args
3716 << " arguments, processing argument " << actual_args
3717 << " (where longs/doubles count twice).";
Ian Rogers7b078e82014-09-10 14:44:24 -07003718 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003719 }
3720 const char* descriptor =
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003721 res_method->GetTypeDescriptorFromTypeIdx(params->GetTypeItem(param_index).type_idx_);
Ian Rogers7b078e82014-09-10 14:44:24 -07003722 if (descriptor == nullptr) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003723 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003724 << " missing signature component";
Ian Rogers7b078e82014-09-10 14:44:24 -07003725 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003726 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00003727 const RegType& reg_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003728 uint32_t get_reg = is_range ? inst->VRegC_3rc() + actual_args : arg[actual_args];
Ian Rogers7b078e82014-09-10 14:44:24 -07003729 if (!work_line_->VerifyRegisterType(this, get_reg, reg_type)) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003730 return res_method;
3731 }
3732 actual_args = reg_type.IsLongOrDoubleTypes() ? actual_args + 2 : actual_args + 1;
3733 }
3734 if (actual_args != expected_args) {
3735 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
3736 << " expected " << expected_args << " arguments, found " << actual_args;
Ian Rogers7b078e82014-09-10 14:44:24 -07003737 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003738 } else {
3739 return res_method;
3740 }
3741}
3742
Ian Rogers62342ec2013-06-11 10:26:37 -07003743void MethodVerifier::VerifyNewArray(const Instruction* inst, bool is_filled, bool is_range) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003744 uint32_t type_idx;
3745 if (!is_filled) {
3746 DCHECK_EQ(inst->Opcode(), Instruction::NEW_ARRAY);
3747 type_idx = inst->VRegC_22c();
3748 } else if (!is_range) {
3749 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY);
3750 type_idx = inst->VRegB_35c();
3751 } else {
3752 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY_RANGE);
3753 type_idx = inst->VRegB_3rc();
3754 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00003755 const RegType& res_type = ResolveClassAndCheckAccess(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003756 if (res_type.IsConflict()) { // bad class
3757 DCHECK_NE(failures_.size(), 0U);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003758 } else {
3759 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
3760 if (!res_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003761 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "new-array on non-array class " << res_type;
Ian Rogers0c4a5062012-02-03 15:18:59 -08003762 } else if (!is_filled) {
3763 /* make sure "size" register is valid type */
Ian Rogers7b078e82014-09-10 14:44:24 -07003764 work_line_->VerifyRegisterType(this, inst->VRegB_22c(), reg_types_.Integer());
Ian Rogers0c4a5062012-02-03 15:18:59 -08003765 /* set register type to array class */
Ian Rogersd8f69b02014-09-10 21:43:52 +00003766 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
Ian Rogers7b078e82014-09-10 14:44:24 -07003767 work_line_->SetRegisterType(this, inst->VRegA_22c(), precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003768 } else {
3769 // Verify each register. If "arg_count" is bad, VerifyRegisterType() will run off the end of
3770 // the list and fail. It's legal, if silly, for arg_count to be zero.
Ian Rogersd8f69b02014-09-10 21:43:52 +00003771 const RegType& expected_type = reg_types_.GetComponentType(res_type, GetClassLoader());
Sebastien Hertz5243e912013-05-21 10:55:07 +02003772 uint32_t arg_count = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
3773 uint32_t arg[5];
3774 if (!is_range) {
Ian Rogers29a26482014-05-02 15:27:29 -07003775 inst->GetVarArgs(arg);
Sebastien Hertz5243e912013-05-21 10:55:07 +02003776 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08003777 for (size_t ui = 0; ui < arg_count; ui++) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003778 uint32_t get_reg = is_range ? inst->VRegC_3rc() + ui : arg[ui];
Ian Rogers7b078e82014-09-10 14:44:24 -07003779 if (!work_line_->VerifyRegisterType(this, get_reg, expected_type)) {
3780 work_line_->SetResultRegisterType(this, reg_types_.Conflict());
Ian Rogers0c4a5062012-02-03 15:18:59 -08003781 return;
3782 }
3783 }
3784 // filled-array result goes into "result" register
Ian Rogersd8f69b02014-09-10 21:43:52 +00003785 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
Ian Rogers7b078e82014-09-10 14:44:24 -07003786 work_line_->SetResultRegisterType(this, precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003787 }
3788 }
3789}
3790
Sebastien Hertz5243e912013-05-21 10:55:07 +02003791void MethodVerifier::VerifyAGet(const Instruction* inst,
Ian Rogersd8f69b02014-09-10 21:43:52 +00003792 const RegType& insn_type, bool is_primitive) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003793 const RegType& index_type = work_line_->GetRegisterType(this, inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07003794 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003795 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07003796 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07003797 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegB_23x());
Ian Rogers89310de2012-02-01 13:47:30 -08003798 if (array_type.IsZero()) {
Nicolas Geoffray4824c272015-06-24 15:53:03 +01003799 have_pending_runtime_throw_failure_ = true;
Ian Rogers89310de2012-02-01 13:47:30 -08003800 // Null array class; this code path will fail at runtime. Infer a merge-able type from the
3801 // instruction type. TODO: have a proper notion of bottom here.
3802 if (!is_primitive || insn_type.IsCategory1Types()) {
3803 // Reference or category 1
Ian Rogers7b078e82014-09-10 14:44:24 -07003804 work_line_->SetRegisterType(this, inst->VRegA_23x(), reg_types_.Zero());
Ian Rogersd81871c2011-10-03 13:57:23 -07003805 } else {
Ian Rogers89310de2012-02-01 13:47:30 -08003806 // Category 2
Ian Rogers7b078e82014-09-10 14:44:24 -07003807 work_line_->SetRegisterTypeWide(this, inst->VRegA_23x(),
3808 reg_types_.FromCat2ConstLo(0, false),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003809 reg_types_.FromCat2ConstHi(0, false));
Ian Rogers89310de2012-02-01 13:47:30 -08003810 }
jeffhaofc3144e2012-02-01 17:21:15 -08003811 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003812 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aget";
Ian Rogers89310de2012-02-01 13:47:30 -08003813 } else {
3814 /* verify the class */
Ian Rogersd8f69b02014-09-10 21:43:52 +00003815 const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
jeffhaofc3144e2012-02-01 17:21:15 -08003816 if (!component_type.IsReferenceTypes() && !is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07003817 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08003818 << " source for aget-object";
3819 } else if (component_type.IsNonZeroReferenceTypes() && is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07003820 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08003821 << " source for category 1 aget";
3822 } else if (is_primitive && !insn_type.Equals(component_type) &&
3823 !((insn_type.IsInteger() && component_type.IsFloat()) ||
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003824 (insn_type.IsLong() && component_type.IsDouble()))) {
3825 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array type " << array_type
3826 << " incompatible with aget of type " << insn_type;
Ian Rogers89310de2012-02-01 13:47:30 -08003827 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003828 // Use knowledge of the field type which is stronger than the type inferred from the
3829 // instruction, which can't differentiate object types and ints from floats, longs from
3830 // doubles.
3831 if (!component_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003832 work_line_->SetRegisterType(this, inst->VRegA_23x(), component_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003833 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07003834 work_line_->SetRegisterTypeWide(this, inst->VRegA_23x(), component_type,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003835 component_type.HighHalf(&reg_types_));
3836 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003837 }
3838 }
3839 }
3840}
3841
Ian Rogersd8f69b02014-09-10 21:43:52 +00003842void MethodVerifier::VerifyPrimitivePut(const RegType& target_type, const RegType& insn_type,
Jeff Haofe1f7c82013-08-01 14:50:24 -07003843 const uint32_t vregA) {
3844 // Primitive assignability rules are weaker than regular assignability rules.
3845 bool instruction_compatible;
3846 bool value_compatible;
Ian Rogers7b078e82014-09-10 14:44:24 -07003847 const RegType& value_type = work_line_->GetRegisterType(this, vregA);
Jeff Haofe1f7c82013-08-01 14:50:24 -07003848 if (target_type.IsIntegralTypes()) {
Jeff Haoa4647482013-08-06 15:35:47 -07003849 instruction_compatible = target_type.Equals(insn_type);
Jeff Haofe1f7c82013-08-01 14:50:24 -07003850 value_compatible = value_type.IsIntegralTypes();
3851 } else if (target_type.IsFloat()) {
3852 instruction_compatible = insn_type.IsInteger(); // no put-float, so expect put-int
3853 value_compatible = value_type.IsFloatTypes();
3854 } else if (target_type.IsLong()) {
3855 instruction_compatible = insn_type.IsLong();
Andreas Gampe376fa682014-09-07 13:06:12 -07003856 // Additional register check: this is not checked statically (as part of VerifyInstructions),
3857 // as target_type depends on the resolved type of the field.
3858 if (instruction_compatible && work_line_->NumRegs() > vregA + 1) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003859 const RegType& value_type_hi = work_line_->GetRegisterType(this, vregA + 1);
Andreas Gampe376fa682014-09-07 13:06:12 -07003860 value_compatible = value_type.IsLongTypes() && value_type.CheckWidePair(value_type_hi);
3861 } else {
3862 value_compatible = false;
3863 }
Jeff Haofe1f7c82013-08-01 14:50:24 -07003864 } else if (target_type.IsDouble()) {
3865 instruction_compatible = insn_type.IsLong(); // no put-double, so expect put-long
Andreas Gampe376fa682014-09-07 13:06:12 -07003866 // Additional register check: this is not checked statically (as part of VerifyInstructions),
3867 // as target_type depends on the resolved type of the field.
3868 if (instruction_compatible && work_line_->NumRegs() > vregA + 1) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003869 const RegType& value_type_hi = work_line_->GetRegisterType(this, vregA + 1);
Andreas Gampe376fa682014-09-07 13:06:12 -07003870 value_compatible = value_type.IsDoubleTypes() && value_type.CheckWidePair(value_type_hi);
3871 } else {
3872 value_compatible = false;
3873 }
Jeff Haofe1f7c82013-08-01 14:50:24 -07003874 } else {
3875 instruction_compatible = false; // reference with primitive store
3876 value_compatible = false; // unused
3877 }
3878 if (!instruction_compatible) {
3879 // This is a global failure rather than a class change failure as the instructions and
3880 // the descriptors for the type should have been consistent within the same file at
3881 // compile time.
3882 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "put insn has type '" << insn_type
3883 << "' but expected type '" << target_type << "'";
3884 return;
3885 }
3886 if (!value_compatible) {
3887 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
3888 << " of type " << value_type << " but expected " << target_type << " for put";
3889 return;
3890 }
3891}
3892
Sebastien Hertz5243e912013-05-21 10:55:07 +02003893void MethodVerifier::VerifyAPut(const Instruction* inst,
Ian Rogersd8f69b02014-09-10 21:43:52 +00003894 const RegType& insn_type, bool is_primitive) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003895 const RegType& index_type = work_line_->GetRegisterType(this, inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07003896 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003897 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07003898 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07003899 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegB_23x());
Ian Rogers89310de2012-02-01 13:47:30 -08003900 if (array_type.IsZero()) {
Nicolas Geoffray66389fb2015-06-19 10:35:42 +01003901 // Null array type; this code path will fail at runtime.
3902 // Still check that the given value matches the instruction's type.
3903 work_line_->VerifyRegisterType(this, inst->VRegA_23x(), insn_type);
jeffhaofc3144e2012-02-01 17:21:15 -08003904 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003905 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aput";
Ian Rogers89310de2012-02-01 13:47:30 -08003906 } else {
Ian Rogersd8f69b02014-09-10 21:43:52 +00003907 const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
Jeff Haofe1f7c82013-08-01 14:50:24 -07003908 const uint32_t vregA = inst->VRegA_23x();
Jeff Haob24b4a72013-07-31 13:47:31 -07003909 if (is_primitive) {
Jeff Haofe1f7c82013-08-01 14:50:24 -07003910 VerifyPrimitivePut(component_type, insn_type, vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07003911 } else {
Jeff Haob24b4a72013-07-31 13:47:31 -07003912 if (!component_type.IsReferenceTypes()) {
3913 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
3914 << " source for aput-object";
3915 } else {
3916 // The instruction agrees with the type of array, confirm the value to be stored does too
3917 // Note: we use the instruction type (rather than the component type) for aput-object as
3918 // incompatible classes will be caught at runtime as an array store exception
Ian Rogers7b078e82014-09-10 14:44:24 -07003919 work_line_->VerifyRegisterType(this, vregA, insn_type);
Jeff Haob24b4a72013-07-31 13:47:31 -07003920 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003921 }
3922 }
3923 }
3924}
3925
Mathieu Chartierc7853442015-03-27 14:35:38 -07003926ArtField* MethodVerifier::GetStaticField(int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08003927 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3928 // Check access to class
Ian Rogersd8f69b02014-09-10 21:43:52 +00003929 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Brian Carlstrom7934ac22013-07-26 10:54:15 -07003930 if (klass_type.IsConflict()) { // bad class
Ian Rogersad0b3a32012-04-16 14:50:24 -07003931 AppendToLastFailMessage(StringPrintf(" in attempt to access static field %d (%s) in %s",
3932 field_idx, dex_file_->GetFieldName(field_id),
3933 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers7b078e82014-09-10 14:44:24 -07003934 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08003935 }
Elliott Hughesb25c3f62012-03-26 16:35:06 -07003936 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003937 return nullptr; // Can't resolve Class so no more to do here, will do checking at runtime.
Ian Rogers90040192011-12-16 08:54:29 -08003938 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07003939 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartierc7853442015-03-27 14:35:38 -07003940 ArtField* field = class_linker->ResolveFieldJLS(*dex_file_, field_idx, dex_cache_,
3941 class_loader_);
Ian Rogers7b078e82014-09-10 14:44:24 -07003942 if (field == nullptr) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07003943 VLOG(verifier) << "Unable to resolve static field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07003944 << dex_file_->GetFieldName(field_id) << ") in "
3945 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogers7b078e82014-09-10 14:44:24 -07003946 DCHECK(self_->IsExceptionPending());
3947 self_->ClearException();
3948 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003949 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
3950 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003951 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access static field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07003952 << " from " << GetDeclaringClass();
Ian Rogers7b078e82014-09-10 14:44:24 -07003953 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003954 } else if (!field->IsStatic()) {
3955 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field) << " to be static";
Ian Rogers7b078e82014-09-10 14:44:24 -07003956 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003957 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07003958 return field;
Ian Rogersd81871c2011-10-03 13:57:23 -07003959}
3960
Mathieu Chartierc7853442015-03-27 14:35:38 -07003961ArtField* MethodVerifier::GetInstanceField(const RegType& obj_type, int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08003962 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3963 // Check access to class
Ian Rogersd8f69b02014-09-10 21:43:52 +00003964 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003965 if (klass_type.IsConflict()) {
3966 AppendToLastFailMessage(StringPrintf(" in attempt to access instance field %d (%s) in %s",
3967 field_idx, dex_file_->GetFieldName(field_id),
3968 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers7b078e82014-09-10 14:44:24 -07003969 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08003970 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003971 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003972 return nullptr; // Can't resolve Class so no more to do here
Ian Rogers90040192011-12-16 08:54:29 -08003973 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07003974 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartierc7853442015-03-27 14:35:38 -07003975 ArtField* field = class_linker->ResolveFieldJLS(*dex_file_, field_idx, dex_cache_,
3976 class_loader_);
Ian Rogers7b078e82014-09-10 14:44:24 -07003977 if (field == nullptr) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07003978 VLOG(verifier) << "Unable to resolve instance field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07003979 << dex_file_->GetFieldName(field_id) << ") in "
3980 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogers7b078e82014-09-10 14:44:24 -07003981 DCHECK(self_->IsExceptionPending());
3982 self_->ClearException();
3983 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003984 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
3985 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003986 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access instance field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07003987 << " from " << GetDeclaringClass();
Ian Rogers7b078e82014-09-10 14:44:24 -07003988 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003989 } else if (field->IsStatic()) {
3990 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field)
3991 << " to not be static";
Ian Rogers7b078e82014-09-10 14:44:24 -07003992 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003993 } else if (obj_type.IsZero()) {
3994 // Cannot infer and check type, however, access will cause null pointer exception
3995 return field;
Stephen Kyle695c5982014-08-22 15:03:07 +01003996 } else if (!obj_type.IsReferenceTypes()) {
3997 // Trying to read a field from something that isn't a reference
3998 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance field access on object that has "
3999 << "non-reference type " << obj_type;
Ian Rogers7b078e82014-09-10 14:44:24 -07004000 return nullptr;
Ian Rogerse1758fe2012-04-19 11:31:15 -07004001 } else {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08004002 mirror::Class* klass = field->GetDeclaringClass();
Ian Rogersd8f69b02014-09-10 21:43:52 +00004003 const RegType& field_klass =
Andreas Gampef23f33d2015-06-23 14:18:17 -07004004 FromClass(dex_file_->GetFieldDeclaringClassDescriptor(field_id),
4005 klass, klass->CannotBeAssignedFromOtherTypes());
Ian Rogersad0b3a32012-04-16 14:50:24 -07004006 if (obj_type.IsUninitializedTypes() &&
4007 (!IsConstructor() || GetDeclaringClass().Equals(obj_type) ||
4008 !field_klass.Equals(GetDeclaringClass()))) {
4009 // Field accesses through uninitialized references are only allowable for constructors where
4010 // the field is declared in this class
4011 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "cannot access instance field " << PrettyField(field)
Brian Carlstrom93c33962013-07-26 10:37:43 -07004012 << " of a not fully initialized object within the context"
4013 << " of " << PrettyMethod(dex_method_idx_, *dex_file_);
Ian Rogers7b078e82014-09-10 14:44:24 -07004014 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004015 } else if (!field_klass.IsAssignableFrom(obj_type)) {
4016 // Trying to access C1.field1 using reference of type C2, which is neither C1 or a sub-class
4017 // of C1. For resolution to occur the declared class of the field must be compatible with
4018 // obj_type, we've discovered this wasn't so, so report the field didn't exist.
4019 Fail(VERIFY_ERROR_NO_FIELD) << "cannot access instance field " << PrettyField(field)
4020 << " from object of type " << obj_type;
Ian Rogers7b078e82014-09-10 14:44:24 -07004021 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004022 } else {
4023 return field;
4024 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004025 }
4026}
4027
Andreas Gampe896df402014-10-20 22:25:29 -07004028template <MethodVerifier::FieldAccessType kAccType>
4029void MethodVerifier::VerifyISFieldAccess(const Instruction* inst, const RegType& insn_type,
4030 bool is_primitive, bool is_static) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02004031 uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
Mathieu Chartierc7853442015-03-27 14:35:38 -07004032 ArtField* field;
Ian Rogersb94a27b2011-10-26 00:33:41 -07004033 if (is_static) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07004034 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07004035 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004036 const RegType& object_type = work_line_->GetRegisterType(this, inst->VRegB_22c());
Ian Rogersf4028cc2011-11-02 14:56:39 -07004037 field = GetInstanceField(object_type, field_idx);
Andreas Gampe896df402014-10-20 22:25:29 -07004038 if (UNLIKELY(have_pending_hard_failure_)) {
4039 return;
4040 }
Ian Rogersb94a27b2011-10-26 00:33:41 -07004041 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00004042 const RegType* field_type = nullptr;
Ian Rogers7b078e82014-09-10 14:44:24 -07004043 if (field != nullptr) {
Andreas Gampe896df402014-10-20 22:25:29 -07004044 if (kAccType == FieldAccessType::kAccPut) {
4045 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
4046 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
4047 << " from other class " << GetDeclaringClass();
4048 return;
4049 }
4050 }
4051
Mathieu Chartierc7853442015-03-27 14:35:38 -07004052 mirror::Class* field_type_class =
4053 can_load_classes_ ? field->GetType<true>() : field->GetType<false>();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004054 if (field_type_class != nullptr) {
Andreas Gampef23f33d2015-06-23 14:18:17 -07004055 field_type = &FromClass(field->GetTypeDescriptor(), field_type_class,
4056 field_type_class->CannotBeAssignedFromOtherTypes());
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08004057 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004058 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
4059 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004060 }
Ian Rogers0d604842012-04-16 14:50:24 -07004061 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004062 if (field_type == nullptr) {
4063 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
4064 const char* descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
Mathieu Chartierbf99f772014-08-23 16:37:27 -07004065 field_type = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004066 }
Sebastien Hertz757b3042014-03-28 14:34:28 +01004067 DCHECK(field_type != nullptr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02004068 const uint32_t vregA = (is_static) ? inst->VRegA_21c() : inst->VRegA_22c();
Andreas Gampe896df402014-10-20 22:25:29 -07004069 static_assert(kAccType == FieldAccessType::kAccPut || kAccType == FieldAccessType::kAccGet,
4070 "Unexpected third access type");
4071 if (kAccType == FieldAccessType::kAccPut) {
4072 // sput or iput.
4073 if (is_primitive) {
4074 VerifyPrimitivePut(*field_type, insn_type, vregA);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004075 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004076 if (!insn_type.IsAssignableFrom(*field_type)) {
Vladimir Marko414000e2015-06-23 17:45:21 +01004077 // If the field type is not a reference, this is a global failure rather than
4078 // a class change failure as the instructions and the descriptors for the type
4079 // should have been consistent within the same file at compile time.
4080 VerifyError error = field_type->IsReferenceTypes() ? VERIFY_ERROR_BAD_CLASS_SOFT
4081 : VERIFY_ERROR_BAD_CLASS_HARD;
4082 Fail(error) << "expected field " << PrettyField(field)
4083 << " to be compatible with type '" << insn_type
4084 << "' but found type '" << *field_type
4085 << "' in put-object";
Andreas Gampe896df402014-10-20 22:25:29 -07004086 return;
4087 }
4088 work_line_->VerifyRegisterType(this, vregA, *field_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004089 }
Andreas Gampe896df402014-10-20 22:25:29 -07004090 } else if (kAccType == FieldAccessType::kAccGet) {
4091 // sget or iget.
4092 if (is_primitive) {
4093 if (field_type->Equals(insn_type) ||
4094 (field_type->IsFloat() && insn_type.IsInteger()) ||
4095 (field_type->IsDouble() && insn_type.IsLong())) {
4096 // expected that read is of the correct primitive type or that int reads are reading
4097 // floats or long reads are reading doubles
4098 } else {
4099 // This is a global failure rather than a class change failure as the instructions and
4100 // the descriptors for the type should have been consistent within the same file at
4101 // compile time
4102 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
4103 << " to be of type '" << insn_type
4104 << "' but found type '" << *field_type << "' in get";
4105 return;
4106 }
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08004107 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004108 if (!insn_type.IsAssignableFrom(*field_type)) {
Vladimir Marko414000e2015-06-23 17:45:21 +01004109 // If the field type is not a reference, this is a global failure rather than
4110 // a class change failure as the instructions and the descriptors for the type
4111 // should have been consistent within the same file at compile time.
4112 VerifyError error = field_type->IsReferenceTypes() ? VERIFY_ERROR_BAD_CLASS_SOFT
4113 : VERIFY_ERROR_BAD_CLASS_HARD;
4114 Fail(error) << "expected field " << PrettyField(field)
4115 << " to be compatible with type '" << insn_type
4116 << "' but found type '" << *field_type
4117 << "' in get-object";
Andreas Gampe896df402014-10-20 22:25:29 -07004118 work_line_->SetRegisterType(this, vregA, reg_types_.Conflict());
4119 return;
4120 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004121 }
Andreas Gampe896df402014-10-20 22:25:29 -07004122 if (!field_type->IsLowHalf()) {
4123 work_line_->SetRegisterType(this, vregA, *field_type);
4124 } else {
4125 work_line_->SetRegisterTypeWide(this, vregA, *field_type, field_type->HighHalf(&reg_types_));
4126 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07004127 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004128 LOG(FATAL) << "Unexpected case.";
Ian Rogersd81871c2011-10-03 13:57:23 -07004129 }
4130}
4131
Mathieu Chartierc7853442015-03-27 14:35:38 -07004132ArtField* MethodVerifier::GetQuickFieldAccess(const Instruction* inst,
Ian Rogers98379392014-02-24 16:53:16 -08004133 RegisterLine* reg_line) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08004134 DCHECK(IsInstructionIGetQuickOrIPutQuick(inst->Opcode())) << inst->Opcode();
Ian Rogers7b078e82014-09-10 14:44:24 -07004135 const RegType& object_type = reg_line->GetRegisterType(this, inst->VRegB_22c());
Ian Rogers9bc54402014-04-17 16:40:01 -07004136 if (!object_type.HasClass()) {
4137 VLOG(verifier) << "Failed to get mirror::Class* from '" << object_type << "'";
4138 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004139 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004140 uint32_t field_offset = static_cast<uint32_t>(inst->VRegC_22c());
Mathieu Chartierc7853442015-03-27 14:35:38 -07004141 ArtField* const f = ArtField::FindInstanceFieldWithOffset(object_type.GetClass(), field_offset);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08004142 DCHECK_EQ(f->GetOffset().Uint32Value(), field_offset);
Sebastien Hertz479fc1e2014-04-04 17:51:34 +02004143 if (f == nullptr) {
4144 VLOG(verifier) << "Failed to find instance field at offset '" << field_offset
4145 << "' from '" << PrettyDescriptor(object_type.GetClass()) << "'";
4146 }
4147 return f;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004148}
4149
Andreas Gampe896df402014-10-20 22:25:29 -07004150template <MethodVerifier::FieldAccessType kAccType>
4151void MethodVerifier::VerifyQuickFieldAccess(const Instruction* inst, const RegType& insn_type,
4152 bool is_primitive) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07004153 DCHECK(Runtime::Current()->IsStarted() || verify_to_dump_);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004154
Mathieu Chartierc7853442015-03-27 14:35:38 -07004155 ArtField* field = GetQuickFieldAccess(inst, work_line_.get());
Ian Rogers7b078e82014-09-10 14:44:24 -07004156 if (field == nullptr) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004157 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field from " << inst->Name();
4158 return;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004159 }
Andreas Gampe896df402014-10-20 22:25:29 -07004160
4161 // For an IPUT_QUICK, we now test for final flag of the field.
4162 if (kAccType == FieldAccessType::kAccPut) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004163 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
4164 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004165 << " from other class " << GetDeclaringClass();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004166 return;
4167 }
4168 }
Andreas Gampe896df402014-10-20 22:25:29 -07004169
4170 // Get the field type.
4171 const RegType* field_type;
4172 {
Mathieu Chartierc7853442015-03-27 14:35:38 -07004173 mirror::Class* field_type_class = can_load_classes_ ? field->GetType<true>() :
4174 field->GetType<false>();
Andreas Gampe896df402014-10-20 22:25:29 -07004175
4176 if (field_type_class != nullptr) {
Andreas Gampef23f33d2015-06-23 14:18:17 -07004177 field_type = &FromClass(field->GetTypeDescriptor(), field_type_class,
4178 field_type_class->CannotBeAssignedFromOtherTypes());
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004179 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004180 Thread* self = Thread::Current();
4181 DCHECK(!can_load_classes_ || self->IsExceptionPending());
4182 self->ClearException();
4183 field_type = &reg_types_.FromDescriptor(field->GetDeclaringClass()->GetClassLoader(),
4184 field->GetTypeDescriptor(), false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004185 }
Andreas Gampe896df402014-10-20 22:25:29 -07004186 if (field_type == nullptr) {
4187 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field type from " << inst->Name();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004188 return;
4189 }
Andreas Gampe896df402014-10-20 22:25:29 -07004190 }
4191
4192 const uint32_t vregA = inst->VRegA_22c();
4193 static_assert(kAccType == FieldAccessType::kAccPut || kAccType == FieldAccessType::kAccGet,
4194 "Unexpected third access type");
4195 if (kAccType == FieldAccessType::kAccPut) {
4196 if (is_primitive) {
4197 // Primitive field assignability rules are weaker than regular assignability rules
4198 bool instruction_compatible;
4199 bool value_compatible;
4200 const RegType& value_type = work_line_->GetRegisterType(this, vregA);
4201 if (field_type->IsIntegralTypes()) {
4202 instruction_compatible = insn_type.IsIntegralTypes();
4203 value_compatible = value_type.IsIntegralTypes();
4204 } else if (field_type->IsFloat()) {
4205 instruction_compatible = insn_type.IsInteger(); // no [is]put-float, so expect [is]put-int
4206 value_compatible = value_type.IsFloatTypes();
4207 } else if (field_type->IsLong()) {
4208 instruction_compatible = insn_type.IsLong();
4209 value_compatible = value_type.IsLongTypes();
4210 } else if (field_type->IsDouble()) {
4211 instruction_compatible = insn_type.IsLong(); // no [is]put-double, so expect [is]put-long
4212 value_compatible = value_type.IsDoubleTypes();
4213 } else {
4214 instruction_compatible = false; // reference field with primitive store
4215 value_compatible = false; // unused
4216 }
4217 if (!instruction_compatible) {
4218 // This is a global failure rather than a class change failure as the instructions and
4219 // the descriptors for the type should have been consistent within the same file at
4220 // compile time
4221 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
4222 << " to be of type '" << insn_type
4223 << "' but found type '" << *field_type
4224 << "' in put";
4225 return;
4226 }
4227 if (!value_compatible) {
4228 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
4229 << " of type " << value_type
4230 << " but expected " << *field_type
4231 << " for store to " << PrettyField(field) << " in put";
4232 return;
4233 }
4234 } else {
4235 if (!insn_type.IsAssignableFrom(*field_type)) {
4236 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
4237 << " to be compatible with type '" << insn_type
4238 << "' but found type '" << *field_type
4239 << "' in put-object";
4240 return;
4241 }
4242 work_line_->VerifyRegisterType(this, vregA, *field_type);
4243 }
4244 } else if (kAccType == FieldAccessType::kAccGet) {
4245 if (is_primitive) {
4246 if (field_type->Equals(insn_type) ||
4247 (field_type->IsFloat() && insn_type.IsIntegralTypes()) ||
4248 (field_type->IsDouble() && insn_type.IsLongTypes())) {
4249 // expected that read is of the correct primitive type or that int reads are reading
4250 // floats or long reads are reading doubles
4251 } else {
4252 // This is a global failure rather than a class change failure as the instructions and
4253 // the descriptors for the type should have been consistent within the same file at
4254 // compile time
4255 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
4256 << " to be of type '" << insn_type
4257 << "' but found type '" << *field_type << "' in Get";
4258 return;
4259 }
4260 } else {
4261 if (!insn_type.IsAssignableFrom(*field_type)) {
4262 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
4263 << " to be compatible with type '" << insn_type
4264 << "' but found type '" << *field_type
4265 << "' in get-object";
4266 work_line_->SetRegisterType(this, vregA, reg_types_.Conflict());
4267 return;
4268 }
4269 }
4270 if (!field_type->IsLowHalf()) {
4271 work_line_->SetRegisterType(this, vregA, *field_type);
4272 } else {
4273 work_line_->SetRegisterTypeWide(this, vregA, *field_type, field_type->HighHalf(&reg_types_));
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004274 }
4275 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004276 LOG(FATAL) << "Unexpected case.";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004277 }
4278}
4279
Ian Rogers776ac1f2012-04-13 23:36:36 -07004280bool MethodVerifier::CheckNotMoveException(const uint16_t* insns, int insn_idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004281 if ((insns[insn_idx] & 0xff) == Instruction::MOVE_EXCEPTION) {
jeffhaod5347e02012-03-22 17:25:05 -07004282 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-exception";
Ian Rogersd81871c2011-10-03 13:57:23 -07004283 return false;
4284 }
4285 return true;
4286}
4287
Stephen Kyle9bc61992014-09-22 13:53:15 +01004288bool MethodVerifier::CheckNotMoveResult(const uint16_t* insns, int insn_idx) {
4289 if (((insns[insn_idx] & 0xff) >= Instruction::MOVE_RESULT) &&
4290 ((insns[insn_idx] & 0xff) <= Instruction::MOVE_RESULT_OBJECT)) {
4291 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-result*";
4292 return false;
4293 }
4294 return true;
4295}
4296
4297bool MethodVerifier::CheckNotMoveExceptionOrMoveResult(const uint16_t* insns, int insn_idx) {
4298 return (CheckNotMoveException(insns, insn_idx) && CheckNotMoveResult(insns, insn_idx));
4299}
4300
Ian Rogersebbdd872014-07-07 23:53:08 -07004301bool MethodVerifier::UpdateRegisters(uint32_t next_insn, RegisterLine* merge_line,
4302 bool update_merge_line) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004303 bool changed = true;
4304 RegisterLine* target_line = reg_table_.GetLine(next_insn);
4305 if (!insn_flags_[next_insn].IsVisitedOrChanged()) {
jeffhaobdb76512011-09-07 11:43:16 -07004306 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07004307 * We haven't processed this instruction before, and we haven't touched the registers here, so
4308 * there's nothing to "merge". Copy the registers over and mark it as changed. (This is the
4309 * only way a register can transition out of "unknown", so this is not just an optimization.)
jeffhaobdb76512011-09-07 11:43:16 -07004310 */
Ian Rogersb8c78592013-07-25 23:52:52 +00004311 if (!insn_flags_[next_insn].IsReturn()) {
4312 target_line->CopyFromLine(merge_line);
4313 } else {
Jeff Haob24b4a72013-07-31 13:47:31 -07004314 // Verify that the monitor stack is empty on return.
Ian Rogers7b078e82014-09-10 14:44:24 -07004315 if (!merge_line->VerifyMonitorStackEmpty(this)) {
Jeff Haob24b4a72013-07-31 13:47:31 -07004316 return false;
4317 }
Ian Rogersb8c78592013-07-25 23:52:52 +00004318 // For returns we only care about the operand to the return, all other registers are dead.
4319 // Initialize them as conflicts so they don't add to GC and deoptimization information.
4320 const Instruction* ret_inst = Instruction::At(code_item_->insns_ + next_insn);
4321 Instruction::Code opcode = ret_inst->Opcode();
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07004322 if (opcode == Instruction::RETURN_VOID || opcode == Instruction::RETURN_VOID_NO_BARRIER) {
Stephen Kyle7e541c92014-12-17 17:10:02 +00004323 SafelyMarkAllRegistersAsConflicts(this, target_line);
Ian Rogersb8c78592013-07-25 23:52:52 +00004324 } else {
4325 target_line->CopyFromLine(merge_line);
4326 if (opcode == Instruction::RETURN_WIDE) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004327 target_line->MarkAllRegistersAsConflictsExceptWide(this, ret_inst->VRegA_11x());
Ian Rogersb8c78592013-07-25 23:52:52 +00004328 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004329 target_line->MarkAllRegistersAsConflictsExcept(this, ret_inst->VRegA_11x());
Ian Rogersb8c78592013-07-25 23:52:52 +00004330 }
4331 }
4332 }
jeffhaobdb76512011-09-07 11:43:16 -07004333 } else {
Ian Rogers700a4022014-05-19 16:49:03 -07004334 std::unique_ptr<RegisterLine> copy(gDebugVerify ?
Ian Rogersd0fbd852013-09-24 18:17:04 -07004335 RegisterLine::Create(target_line->NumRegs(), this) :
Ian Rogers7b078e82014-09-10 14:44:24 -07004336 nullptr);
Ian Rogers7b0c5b42012-02-16 15:29:07 -08004337 if (gDebugVerify) {
4338 copy->CopyFromLine(target_line);
4339 }
Ian Rogers7b078e82014-09-10 14:44:24 -07004340 changed = target_line->MergeRegisters(this, merge_line);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004341 if (have_pending_hard_failure_) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004342 return false;
jeffhaobdb76512011-09-07 11:43:16 -07004343 }
Ian Rogers2c8a8572011-10-24 17:11:36 -07004344 if (gDebugVerify && changed) {
Elliott Hughes398f64b2012-03-26 18:05:48 -07004345 LogVerifyInfo() << "Merging at [" << reinterpret_cast<void*>(work_insn_idx_) << "]"
Elliott Hughesc073b072012-05-24 19:29:17 -07004346 << " to [" << reinterpret_cast<void*>(next_insn) << "]: " << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07004347 << copy->Dump(this) << " MERGE\n"
4348 << merge_line->Dump(this) << " ==\n"
4349 << target_line->Dump(this) << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07004350 }
Ian Rogersebbdd872014-07-07 23:53:08 -07004351 if (update_merge_line && changed) {
4352 merge_line->CopyFromLine(target_line);
4353 }
jeffhaobdb76512011-09-07 11:43:16 -07004354 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004355 if (changed) {
4356 insn_flags_[next_insn].SetChanged();
jeffhaobdb76512011-09-07 11:43:16 -07004357 }
4358 return true;
4359}
4360
Ian Rogers7b3ddd22013-02-21 15:19:52 -08004361InstructionFlags* MethodVerifier::CurrentInsnFlags() {
Ian Rogers776ac1f2012-04-13 23:36:36 -07004362 return &insn_flags_[work_insn_idx_];
4363}
4364
Ian Rogersd8f69b02014-09-10 21:43:52 +00004365const RegType& MethodVerifier::GetMethodReturnType() {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004366 if (return_type_ == nullptr) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07004367 if (mirror_method_ != nullptr) {
Ian Rogersded66a02014-10-28 18:12:55 -07004368 mirror::Class* return_type_class = mirror_method_->GetReturnType(can_load_classes_);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004369 if (return_type_class != nullptr) {
Andreas Gampef23f33d2015-06-23 14:18:17 -07004370 return_type_ = &FromClass(mirror_method_->GetReturnTypeDescriptor(),
4371 return_type_class,
4372 return_type_class->CannotBeAssignedFromOtherTypes());
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004373 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004374 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
4375 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004376 }
4377 }
4378 if (return_type_ == nullptr) {
4379 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
4380 const DexFile::ProtoId& proto_id = dex_file_->GetMethodPrototype(method_id);
4381 uint16_t return_type_idx = proto_id.return_type_idx_;
4382 const char* descriptor = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(return_type_idx));
Mathieu Chartierbf99f772014-08-23 16:37:27 -07004383 return_type_ = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004384 }
4385 }
4386 return *return_type_;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004387}
4388
Ian Rogersd8f69b02014-09-10 21:43:52 +00004389const RegType& MethodVerifier::GetDeclaringClass() {
Ian Rogers7b078e82014-09-10 14:44:24 -07004390 if (declaring_class_ == nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004391 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
Brian Carlstrom93c33962013-07-26 10:37:43 -07004392 const char* descriptor
4393 = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(method_id.class_idx_));
Mathieu Chartiere401d142015-04-22 13:56:20 -07004394 if (mirror_method_ != nullptr) {
Ian Rogers637c65b2013-05-31 11:46:00 -07004395 mirror::Class* klass = mirror_method_->GetDeclaringClass();
Andreas Gampef23f33d2015-06-23 14:18:17 -07004396 declaring_class_ = &FromClass(descriptor, klass,
4397 klass->CannotBeAssignedFromOtherTypes());
Ian Rogers637c65b2013-05-31 11:46:00 -07004398 } else {
Mathieu Chartierbf99f772014-08-23 16:37:27 -07004399 declaring_class_ = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers637c65b2013-05-31 11:46:00 -07004400 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07004401 }
Ian Rogers637c65b2013-05-31 11:46:00 -07004402 return *declaring_class_;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004403}
4404
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004405std::vector<int32_t> MethodVerifier::DescribeVRegs(uint32_t dex_pc) {
4406 RegisterLine* line = reg_table_.GetLine(dex_pc);
Sebastien Hertzaa0c00c2014-03-14 17:58:54 +01004407 DCHECK(line != nullptr) << "No register line at DEX pc " << StringPrintf("0x%x", dex_pc);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004408 std::vector<int32_t> result;
4409 for (size_t i = 0; i < line->NumRegs(); ++i) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004410 const RegType& type = line->GetRegisterType(this, i);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004411 if (type.IsConstant()) {
4412 result.push_back(type.IsPreciseConstant() ? 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->ConstantValue());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004415 } else if (type.IsConstantLo()) {
4416 result.push_back(type.IsPreciseConstantLo() ? kConstant : kImpreciseConstant);
Ian Rogers7b078e82014-09-10 14:44:24 -07004417 const ConstantType* const_val = down_cast<const ConstantType*>(&type);
4418 result.push_back(const_val->ConstantValueLo());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004419 } else if (type.IsConstantHi()) {
4420 result.push_back(type.IsPreciseConstantHi() ? kConstant : kImpreciseConstant);
Ian Rogers7b078e82014-09-10 14:44:24 -07004421 const ConstantType* const_val = down_cast<const ConstantType*>(&type);
4422 result.push_back(const_val->ConstantValueHi());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004423 } else if (type.IsIntegralTypes()) {
4424 result.push_back(kIntVReg);
4425 result.push_back(0);
4426 } else if (type.IsFloat()) {
4427 result.push_back(kFloatVReg);
4428 result.push_back(0);
4429 } else if (type.IsLong()) {
4430 result.push_back(kLongLoVReg);
4431 result.push_back(0);
4432 result.push_back(kLongHiVReg);
4433 result.push_back(0);
4434 ++i;
4435 } else if (type.IsDouble()) {
4436 result.push_back(kDoubleLoVReg);
4437 result.push_back(0);
4438 result.push_back(kDoubleHiVReg);
4439 result.push_back(0);
4440 ++i;
4441 } else if (type.IsUndefined() || type.IsConflict() || type.IsHighHalf()) {
4442 result.push_back(kUndefined);
4443 result.push_back(0);
4444 } else {
Ian Rogers7b3ddd22013-02-21 15:19:52 -08004445 CHECK(type.IsNonZeroReferenceTypes());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004446 result.push_back(kReferenceVReg);
4447 result.push_back(0);
4448 }
4449 }
4450 return result;
4451}
4452
Ian Rogersd8f69b02014-09-10 21:43:52 +00004453const RegType& MethodVerifier::DetermineCat1Constant(int32_t value, bool precise) {
Sebastien Hertz849600b2013-12-20 10:28:08 +01004454 if (precise) {
4455 // Precise constant type.
4456 return reg_types_.FromCat1Const(value, true);
4457 } else {
4458 // Imprecise constant type.
4459 if (value < -32768) {
4460 return reg_types_.IntConstant();
4461 } else if (value < -128) {
4462 return reg_types_.ShortConstant();
4463 } else if (value < 0) {
4464 return reg_types_.ByteConstant();
4465 } else if (value == 0) {
4466 return reg_types_.Zero();
4467 } else if (value == 1) {
4468 return reg_types_.One();
4469 } else if (value < 128) {
4470 return reg_types_.PosByteConstant();
4471 } else if (value < 32768) {
4472 return reg_types_.PosShortConstant();
4473 } else if (value < 65536) {
4474 return reg_types_.CharConstant();
4475 } else {
4476 return reg_types_.IntConstant();
4477 }
4478 }
4479}
4480
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004481void MethodVerifier::Init() {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08004482 art::verifier::RegTypeCache::Init();
Brian Carlstrome7d856b2012-01-11 18:10:55 -08004483}
4484
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004485void MethodVerifier::Shutdown() {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08004486 verifier::RegTypeCache::ShutDown();
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08004487}
jeffhaod1224c72012-02-29 13:43:08 -08004488
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -07004489void MethodVerifier::VisitStaticRoots(RootVisitor* visitor) {
4490 RegTypeCache::VisitStaticRoots(visitor);
Mathieu Chartier7c438b12014-09-12 17:01:24 -07004491}
4492
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -07004493void MethodVerifier::VisitRoots(RootVisitor* visitor, const RootInfo& root_info) {
4494 reg_types_.VisitRoots(visitor, root_info);
Mathieu Chartierc528dba2013-11-26 12:00:11 -08004495}
4496
Andreas Gampef23f33d2015-06-23 14:18:17 -07004497const RegType& MethodVerifier::FromClass(const char* descriptor,
4498 mirror::Class* klass,
4499 bool precise) {
4500 DCHECK(klass != nullptr);
4501 if (precise && !klass->IsInstantiable() && !klass->IsPrimitive()) {
4502 Fail(VerifyError::VERIFY_ERROR_NO_CLASS) << "Could not create precise reference for "
4503 << "non-instantiable klass " << descriptor;
4504 precise = false;
4505 }
4506 return reg_types_.FromClass(descriptor, klass, precise);
4507}
4508
Ian Rogersd81871c2011-10-03 13:57:23 -07004509} // namespace verifier
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07004510} // namespace art