blob: 49ca4571574a7ac8deacf8e814453886c29ee718 [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);
Ian Rogers13735952014-10-08 12:43:28 -0700175 const uint8_t* class_data = dex_file->GetClassData(*class_def);
Ian Rogers7b078e82014-09-10 14:44:24 -0700176 if (class_data == nullptr) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700177 // empty class, probably a marker interface
jeffhaof1e6b7c2012-06-05 18:33:30 -0700178 return kNoFailure;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700179 }
jeffhaof56197c2012-03-05 18:01:54 -0800180 ClassDataItemIterator it(*dex_file, class_data);
181 while (it.HasNextStaticField() || it.HasNextInstanceField()) {
182 it.Next();
183 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700184 size_t error_count = 0;
jeffhaof1e6b7c2012-06-05 18:33:30 -0700185 bool hard_fail = false;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700186 ClassLinker* linker = Runtime::Current()->GetClassLinker();
jeffhao9b0b1882012-10-01 16:51:22 -0700187 int64_t previous_direct_method_idx = -1;
jeffhaof56197c2012-03-05 18:01:54 -0800188 while (it.HasNextDirectMethod()) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700189 self->AllowThreadSuspension();
jeffhaof56197c2012-03-05 18:01:54 -0800190 uint32_t method_idx = it.GetMemberIndex();
jeffhao9b0b1882012-10-01 16:51:22 -0700191 if (method_idx == previous_direct_method_idx) {
192 // smali can create dex files with two encoded_methods sharing the same method_idx
193 // http://code.google.com/p/smali/issues/detail?id=119
194 it.Next();
195 continue;
196 }
197 previous_direct_method_idx = method_idx;
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700198 InvokeType type = it.GetMethodInvokeType(*class_def);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700199 ArtMethod* method = linker->ResolveMethod(
200 *dex_file, method_idx, dex_cache, class_loader, nullptr, type);
Ian Rogers7b078e82014-09-10 14:44:24 -0700201 if (method == nullptr) {
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700202 DCHECK(self->IsExceptionPending());
Ian Rogersad0b3a32012-04-16 14:50:24 -0700203 // We couldn't resolve the method, but continue regardless.
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700204 self->ClearException();
Mathieu Chartiere401d142015-04-22 13:56:20 -0700205 } else {
206 DCHECK(method->GetDeclaringClassUnchecked() != nullptr) << type;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700207 }
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700208 StackHandleScope<1> hs(self);
Ian Rogers7b078e82014-09-10 14:44:24 -0700209 MethodVerifier::FailureKind result = VerifyMethod(self,
210 method_idx,
Brian Carlstrom93c33962013-07-26 10:37:43 -0700211 dex_file,
212 dex_cache,
213 class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700214 class_def,
Brian Carlstrom93c33962013-07-26 10:37:43 -0700215 it.GetMethodCodeItem(),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700216 method, it.GetMethodAccessFlags(), allow_soft_failures, false);
jeffhaof1e6b7c2012-06-05 18:33:30 -0700217 if (result != kNoFailure) {
218 if (result == kHardFailure) {
219 hard_fail = true;
220 if (error_count > 0) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700221 *error += "\n";
jeffhaof1e6b7c2012-06-05 18:33:30 -0700222 }
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700223 *error = "Verifier rejected class ";
224 *error += PrettyDescriptor(dex_file->GetClassDescriptor(*class_def));
225 *error += " due to bad method ";
226 *error += PrettyMethod(method_idx, *dex_file);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700227 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700228 ++error_count;
jeffhaof56197c2012-03-05 18:01:54 -0800229 }
230 it.Next();
231 }
jeffhao9b0b1882012-10-01 16:51:22 -0700232 int64_t previous_virtual_method_idx = -1;
jeffhaof56197c2012-03-05 18:01:54 -0800233 while (it.HasNextVirtualMethod()) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700234 self->AllowThreadSuspension();
jeffhaof56197c2012-03-05 18:01:54 -0800235 uint32_t method_idx = it.GetMemberIndex();
jeffhao9b0b1882012-10-01 16:51:22 -0700236 if (method_idx == previous_virtual_method_idx) {
237 // smali can create dex files with two encoded_methods sharing the same method_idx
238 // http://code.google.com/p/smali/issues/detail?id=119
239 it.Next();
240 continue;
241 }
242 previous_virtual_method_idx = method_idx;
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700243 InvokeType type = it.GetMethodInvokeType(*class_def);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700244 ArtMethod* method = linker->ResolveMethod(
245 *dex_file, method_idx, dex_cache, class_loader, nullptr, type);
Ian Rogers7b078e82014-09-10 14:44:24 -0700246 if (method == nullptr) {
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700247 DCHECK(self->IsExceptionPending());
Ian Rogersad0b3a32012-04-16 14:50:24 -0700248 // We couldn't resolve the method, but continue regardless.
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700249 self->ClearException();
Ian Rogersad0b3a32012-04-16 14:50:24 -0700250 }
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700251 StackHandleScope<1> hs(self);
Ian Rogers7b078e82014-09-10 14:44:24 -0700252 MethodVerifier::FailureKind result = VerifyMethod(self,
253 method_idx,
Brian Carlstrom93c33962013-07-26 10:37:43 -0700254 dex_file,
255 dex_cache,
256 class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700257 class_def,
Brian Carlstrom93c33962013-07-26 10:37:43 -0700258 it.GetMethodCodeItem(),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700259 method, it.GetMethodAccessFlags(), allow_soft_failures, false);
jeffhaof1e6b7c2012-06-05 18:33:30 -0700260 if (result != kNoFailure) {
261 if (result == kHardFailure) {
262 hard_fail = true;
263 if (error_count > 0) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700264 *error += "\n";
jeffhaof1e6b7c2012-06-05 18:33:30 -0700265 }
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700266 *error = "Verifier rejected class ";
267 *error += PrettyDescriptor(dex_file->GetClassDescriptor(*class_def));
268 *error += " due to bad method ";
269 *error += PrettyMethod(method_idx, *dex_file);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700270 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700271 ++error_count;
jeffhaof56197c2012-03-05 18:01:54 -0800272 }
273 it.Next();
274 }
jeffhaof1e6b7c2012-06-05 18:33:30 -0700275 if (error_count == 0) {
276 return kNoFailure;
277 } else {
278 return hard_fail ? kHardFailure : kSoftFailure;
279 }
jeffhaof56197c2012-03-05 18:01:54 -0800280}
281
Andreas Gampea4f5bf62015-05-18 20:50:12 -0700282static bool IsLargeMethod(const DexFile::CodeItem* const code_item) {
Andreas Gampe3c651fc2015-05-21 14:06:46 -0700283 if (code_item == nullptr) {
284 return false;
285 }
286
Andreas Gampea4f5bf62015-05-18 20:50:12 -0700287 uint16_t registers_size = code_item->registers_size_;
288 uint32_t insns_size = code_item->insns_size_in_code_units_;
289
290 return registers_size * insns_size > 4*1024*1024;
291}
292
Ian Rogers7b078e82014-09-10 14:44:24 -0700293MethodVerifier::FailureKind MethodVerifier::VerifyMethod(Thread* self, uint32_t method_idx,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800294 const DexFile* dex_file,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700295 Handle<mirror::DexCache> dex_cache,
296 Handle<mirror::ClassLoader> class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700297 const DexFile::ClassDef* class_def,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800298 const DexFile::CodeItem* code_item,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700299 ArtMethod* method,
Jeff Haoee988952013-04-16 14:23:47 -0700300 uint32_t method_access_flags,
Ian Rogers46960fe2014-05-23 10:43:43 -0700301 bool allow_soft_failures,
302 bool need_precise_constants) {
Ian Rogersc8982582012-09-07 16:53:25 -0700303 MethodVerifier::FailureKind result = kNoFailure;
Mathieu Chartier8e219ae2014-08-19 14:29:46 -0700304 uint64_t start_ns = kTimeVerifyMethod ? NanoTime() : 0;
Ian Rogersc8982582012-09-07 16:53:25 -0700305
Ian Rogers7b078e82014-09-10 14:44:24 -0700306 MethodVerifier verifier(self, dex_file, dex_cache, class_loader, class_def, code_item,
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700307 method_idx, method, method_access_flags, true, allow_soft_failures,
Mathieu Chartier4306ef82014-12-19 18:41:47 -0800308 need_precise_constants, true);
Ian Rogers46960fe2014-05-23 10:43:43 -0700309 if (verifier.Verify()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700310 // Verification completed, however failures may be pending that didn't cause the verification
311 // to hard fail.
Ian Rogers46960fe2014-05-23 10:43:43 -0700312 CHECK(!verifier.have_pending_hard_failure_);
313 if (verifier.failures_.size() != 0) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -0700314 if (VLOG_IS_ON(verifier)) {
Ian Rogers46960fe2014-05-23 10:43:43 -0700315 verifier.DumpFailures(VLOG_STREAM(verifier) << "Soft verification failures in "
Anwar Ghuloum75a43f12013-08-13 17:22:14 -0700316 << PrettyMethod(method_idx, *dex_file) << "\n");
317 }
Ian Rogersc8982582012-09-07 16:53:25 -0700318 result = kSoftFailure;
jeffhaof56197c2012-03-05 18:01:54 -0800319 }
320 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700321 // Bad method data.
Ian Rogers46960fe2014-05-23 10:43:43 -0700322 CHECK_NE(verifier.failures_.size(), 0U);
323 CHECK(verifier.have_pending_hard_failure_);
324 verifier.DumpFailures(LOG(INFO) << "Verification error in "
Elliott Hughesc073b072012-05-24 19:29:17 -0700325 << PrettyMethod(method_idx, *dex_file) << "\n");
jeffhaof56197c2012-03-05 18:01:54 -0800326 if (gDebugVerify) {
Ian Rogers46960fe2014-05-23 10:43:43 -0700327 std::cout << "\n" << verifier.info_messages_.str();
328 verifier.Dump(std::cout);
jeffhaof56197c2012-03-05 18:01:54 -0800329 }
Ian Rogersc8982582012-09-07 16:53:25 -0700330 result = kHardFailure;
jeffhaof56197c2012-03-05 18:01:54 -0800331 }
Mathieu Chartier8e219ae2014-08-19 14:29:46 -0700332 if (kTimeVerifyMethod) {
333 uint64_t duration_ns = NanoTime() - start_ns;
334 if (duration_ns > MsToNs(100)) {
335 LOG(WARNING) << "Verification of " << PrettyMethod(method_idx, *dex_file)
Andreas Gampea4f5bf62015-05-18 20:50:12 -0700336 << " took " << PrettyDuration(duration_ns)
337 << (IsLargeMethod(code_item) ? " (large method)" : "");
Mathieu Chartier8e219ae2014-08-19 14:29:46 -0700338 }
Ian Rogersc8982582012-09-07 16:53:25 -0700339 }
340 return result;
jeffhaof56197c2012-03-05 18:01:54 -0800341}
342
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700343MethodVerifier* MethodVerifier::VerifyMethodAndDump(Thread* self, std::ostream& os, uint32_t dex_method_idx,
Mathieu Chartier590fee92013-09-13 13:46:47 -0700344 const DexFile* dex_file,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700345 Handle<mirror::DexCache> dex_cache,
346 Handle<mirror::ClassLoader> class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700347 const DexFile::ClassDef* class_def,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800348 const DexFile::CodeItem* code_item,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700349 ArtMethod* method,
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800350 uint32_t method_access_flags) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700351 MethodVerifier* verifier = new MethodVerifier(self, dex_file, dex_cache, class_loader,
352 class_def, code_item, dex_method_idx, method,
353 method_access_flags, true, true, true, true);
354 verifier->Verify();
355 verifier->DumpFailures(os);
356 os << verifier->info_messages_.str();
Andreas Gampe5cbcde22014-09-16 14:59:49 -0700357 // Only dump and return if no hard failures. Otherwise the verifier may be not fully initialized
358 // and querying any info is dangerous/can abort.
359 if (verifier->have_pending_hard_failure_) {
360 delete verifier;
361 return nullptr;
362 } else {
363 verifier->Dump(os);
364 return verifier;
365 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800366}
367
Ian Rogers7b078e82014-09-10 14:44:24 -0700368MethodVerifier::MethodVerifier(Thread* self,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700369 const DexFile* dex_file, Handle<mirror::DexCache> dex_cache,
370 Handle<mirror::ClassLoader> class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700371 const DexFile::ClassDef* class_def,
Mathieu Chartier590fee92013-09-13 13:46:47 -0700372 const DexFile::CodeItem* code_item, uint32_t dex_method_idx,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700373 ArtMethod* method, uint32_t method_access_flags,
Ian Rogers46960fe2014-05-23 10:43:43 -0700374 bool can_load_classes, bool allow_soft_failures,
Mathieu Chartier4306ef82014-12-19 18:41:47 -0800375 bool need_precise_constants, bool verify_to_dump,
376 bool allow_thread_suspension)
Ian Rogers7b078e82014-09-10 14:44:24 -0700377 : self_(self),
378 reg_types_(can_load_classes),
Elliott Hughes80537bb2013-01-04 16:37:26 -0800379 work_insn_idx_(-1),
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800380 dex_method_idx_(dex_method_idx),
Ian Rogers637c65b2013-05-31 11:46:00 -0700381 mirror_method_(method),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700382 method_access_flags_(method_access_flags),
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700383 return_type_(nullptr),
jeffhaof56197c2012-03-05 18:01:54 -0800384 dex_file_(dex_file),
385 dex_cache_(dex_cache),
386 class_loader_(class_loader),
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700387 class_def_(class_def),
jeffhaof56197c2012-03-05 18:01:54 -0800388 code_item_(code_item),
Ian Rogers7b078e82014-09-10 14:44:24 -0700389 declaring_class_(nullptr),
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700390 interesting_dex_pc_(-1),
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700391 monitor_enter_dex_pcs_(nullptr),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700392 have_pending_hard_failure_(false),
jeffhaofaf459e2012-08-31 15:32:47 -0700393 have_pending_runtime_throw_failure_(false),
jeffhaof56197c2012-03-05 18:01:54 -0800394 new_instance_count_(0),
Elliott Hughes80537bb2013-01-04 16:37:26 -0800395 monitor_enter_count_(0),
Jeff Haoee988952013-04-16 14:23:47 -0700396 can_load_classes_(can_load_classes),
Sebastien Hertz4d4adb12013-07-24 16:14:19 +0200397 allow_soft_failures_(allow_soft_failures),
Ian Rogers46960fe2014-05-23 10:43:43 -0700398 need_precise_constants_(need_precise_constants),
Sebastien Hertz4d4adb12013-07-24 16:14:19 +0200399 has_check_casts_(false),
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700400 has_virtual_or_interface_invokes_(false),
Mathieu Chartier4306ef82014-12-19 18:41:47 -0800401 verify_to_dump_(verify_to_dump),
402 allow_thread_suspension_(allow_thread_suspension) {
Mathieu Chartierd0ad2ee2015-03-31 14:59:59 -0700403 self->PushVerifier(this);
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700404 DCHECK(class_def != nullptr);
jeffhaof56197c2012-03-05 18:01:54 -0800405}
406
Mathieu Chartier590fee92013-09-13 13:46:47 -0700407MethodVerifier::~MethodVerifier() {
Mathieu Chartierd0ad2ee2015-03-31 14:59:59 -0700408 Thread::Current()->PopVerifier(this);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700409 STLDeleteElements(&failure_messages_);
410}
411
Mathieu Chartiere401d142015-04-22 13:56:20 -0700412void MethodVerifier::FindLocksAtDexPc(ArtMethod* m, uint32_t dex_pc,
Ian Rogers46960fe2014-05-23 10:43:43 -0700413 std::vector<uint32_t>* monitor_enter_dex_pcs) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700414 StackHandleScope<2> hs(Thread::Current());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700415 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache()));
416 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader()));
Mathieu Chartiere401d142015-04-22 13:56:20 -0700417 MethodVerifier verifier(hs.Self(), m->GetDexFile(), dex_cache, class_loader, &m->GetClassDef(),
418 m->GetCodeItem(), m->GetDexMethodIndex(), m, m->GetAccessFlags(),
Mathieu Chartier4306ef82014-12-19 18:41:47 -0800419 false, true, false, false);
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700420 verifier.interesting_dex_pc_ = dex_pc;
Ian Rogers46960fe2014-05-23 10:43:43 -0700421 verifier.monitor_enter_dex_pcs_ = monitor_enter_dex_pcs;
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700422 verifier.FindLocksAtDexPc();
423}
424
Andreas Gampecb3c08f2014-09-18 13:16:38 -0700425static bool HasMonitorEnterInstructions(const DexFile::CodeItem* const code_item) {
426 const Instruction* inst = Instruction::At(code_item->insns_);
427
428 uint32_t insns_size = code_item->insns_size_in_code_units_;
429 for (uint32_t dex_pc = 0; dex_pc < insns_size;) {
430 if (inst->Opcode() == Instruction::MONITOR_ENTER) {
431 return true;
432 }
433
434 dex_pc += inst->SizeInCodeUnits();
435 inst = inst->Next();
436 }
437
438 return false;
439}
440
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700441void MethodVerifier::FindLocksAtDexPc() {
Ian Rogers7b078e82014-09-10 14:44:24 -0700442 CHECK(monitor_enter_dex_pcs_ != nullptr);
443 CHECK(code_item_ != nullptr); // This only makes sense for methods with code.
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700444
Andreas Gampecb3c08f2014-09-18 13:16:38 -0700445 // Quick check whether there are any monitor_enter instructions at all.
446 if (!HasMonitorEnterInstructions(code_item_)) {
447 return;
448 }
449
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700450 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
451 // verification. In practice, the phase we want relies on data structures set up by all the
452 // earlier passes, so we just run the full method verification and bail out early when we've
453 // got what we wanted.
454 Verify();
455}
456
Mathieu Chartiere401d142015-04-22 13:56:20 -0700457ArtField* MethodVerifier::FindAccessedFieldAtDexPc(ArtMethod* m, uint32_t dex_pc) {
458 StackHandleScope<2> hs(Thread::Current());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700459 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache()));
460 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader()));
Mathieu Chartiere401d142015-04-22 13:56:20 -0700461 MethodVerifier verifier(hs.Self(), m->GetDexFile(), dex_cache, class_loader, &m->GetClassDef(),
462 m->GetCodeItem(), m->GetDexMethodIndex(), m, m->GetAccessFlags(), true,
463 true, false, true);
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200464 return verifier.FindAccessedFieldAtDexPc(dex_pc);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200465}
466
Mathieu Chartierc7853442015-03-27 14:35:38 -0700467ArtField* MethodVerifier::FindAccessedFieldAtDexPc(uint32_t dex_pc) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700468 CHECK(code_item_ != nullptr); // This only makes sense for methods with code.
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200469
470 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
471 // verification. In practice, the phase we want relies on data structures set up by all the
472 // earlier passes, so we just run the full method verification and bail out early when we've
473 // got what we wanted.
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200474 bool success = Verify();
475 if (!success) {
Ian Rogers9bc54402014-04-17 16:40:01 -0700476 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200477 }
478 RegisterLine* register_line = reg_table_.GetLine(dex_pc);
Ian Rogers7b078e82014-09-10 14:44:24 -0700479 if (register_line == nullptr) {
Ian Rogers9bc54402014-04-17 16:40:01 -0700480 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200481 }
482 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
483 return GetQuickFieldAccess(inst, register_line);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200484}
485
Mathieu Chartiere401d142015-04-22 13:56:20 -0700486ArtMethod* MethodVerifier::FindInvokedMethodAtDexPc(ArtMethod* m, uint32_t dex_pc) {
487 StackHandleScope<2> hs(Thread::Current());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700488 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache()));
489 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader()));
Mathieu Chartiere401d142015-04-22 13:56:20 -0700490 MethodVerifier verifier(hs.Self(), m->GetDexFile(), dex_cache, class_loader, &m->GetClassDef(),
491 m->GetCodeItem(), m->GetDexMethodIndex(), m, m->GetAccessFlags(), true,
492 true, false, true);
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200493 return verifier.FindInvokedMethodAtDexPc(dex_pc);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200494}
495
Mathieu Chartiere401d142015-04-22 13:56:20 -0700496ArtMethod* MethodVerifier::FindInvokedMethodAtDexPc(uint32_t dex_pc) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700497 CHECK(code_item_ != nullptr); // This only makes sense for methods with code.
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200498
499 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
500 // verification. In practice, the phase we want relies on data structures set up by all the
501 // earlier passes, so we just run the full method verification and bail out early when we've
502 // got what we wanted.
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200503 bool success = Verify();
504 if (!success) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700505 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200506 }
507 RegisterLine* register_line = reg_table_.GetLine(dex_pc);
Ian Rogers7b078e82014-09-10 14:44:24 -0700508 if (register_line == nullptr) {
509 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200510 }
511 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
512 const bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
Mathieu Chartier091d2382015-03-06 10:59:06 -0800513 return GetQuickInvokedMethod(inst, register_line, is_range, false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200514}
515
Mathieu Chartiere401d142015-04-22 13:56:20 -0700516SafeMap<uint32_t, std::set<uint32_t>> MethodVerifier::FindStringInitMap(ArtMethod* m) {
Jeff Hao848f70a2014-01-15 13:49:50 -0800517 Thread* self = Thread::Current();
Mathieu Chartiere401d142015-04-22 13:56:20 -0700518 StackHandleScope<2> hs(self);
Jeff Hao848f70a2014-01-15 13:49:50 -0800519 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache()));
520 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader()));
Jeff Hao848f70a2014-01-15 13:49:50 -0800521 MethodVerifier verifier(self, m->GetDexFile(), dex_cache, class_loader, &m->GetClassDef(),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700522 m->GetCodeItem(), m->GetDexMethodIndex(), m, m->GetAccessFlags(),
Jeff Hao848f70a2014-01-15 13:49:50 -0800523 true, true, false, true);
524 return verifier.FindStringInitMap();
525}
526
527SafeMap<uint32_t, std::set<uint32_t>>& MethodVerifier::FindStringInitMap() {
528 Verify();
529 return GetStringInitPcRegMap();
530}
531
Ian Rogersad0b3a32012-04-16 14:50:24 -0700532bool MethodVerifier::Verify() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700533 // If there aren't any instructions, make sure that's expected, then exit successfully.
Ian Rogers7b078e82014-09-10 14:44:24 -0700534 if (code_item_ == nullptr) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700535 if ((method_access_flags_ & (kAccNative | kAccAbstract)) == 0) {
jeffhaod5347e02012-03-22 17:25:05 -0700536 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "zero-length code in concrete non-native method";
jeffhaobdb76512011-09-07 11:43:16 -0700537 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -0700538 } else {
539 return true;
jeffhaobdb76512011-09-07 11:43:16 -0700540 }
jeffhaobdb76512011-09-07 11:43:16 -0700541 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700542 // Sanity-check the register counts. ins + locals = registers, so make sure that ins <= registers.
543 if (code_item_->ins_size_ > code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700544 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad register counts (ins=" << code_item_->ins_size_
545 << " regs=" << code_item_->registers_size_;
Ian Rogersd81871c2011-10-03 13:57:23 -0700546 return false;
jeffhaobdb76512011-09-07 11:43:16 -0700547 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700548 // Allocate and initialize an array to hold instruction data.
Ian Rogers7b3ddd22013-02-21 15:19:52 -0800549 insn_flags_.reset(new InstructionFlags[code_item_->insns_size_in_code_units_]());
Ian Rogersd81871c2011-10-03 13:57:23 -0700550 // Run through the instructions and see if the width checks out.
551 bool result = ComputeWidthsAndCountOps();
552 // Flag instructions guarded by a "try" block and check exception handlers.
553 result = result && ScanTryCatchBlocks();
554 // Perform static instruction verification.
555 result = result && VerifyInstructions();
Ian Rogersad0b3a32012-04-16 14:50:24 -0700556 // Perform code-flow analysis and return.
Vladimir Marko2b5eaa22013-12-13 13:59:30 +0000557 result = result && VerifyCodeFlow();
558 // Compute information for compiler.
559 if (result && Runtime::Current()->IsCompiler()) {
560 result = Runtime::Current()->GetCompilerCallbacks()->MethodVerified(this);
561 }
562 return result;
jeffhaoba5ebb92011-08-25 17:24:37 -0700563}
564
Ian Rogers776ac1f2012-04-13 23:36:36 -0700565std::ostream& MethodVerifier::Fail(VerifyError error) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700566 switch (error) {
567 case VERIFY_ERROR_NO_CLASS:
568 case VERIFY_ERROR_NO_FIELD:
569 case VERIFY_ERROR_NO_METHOD:
570 case VERIFY_ERROR_ACCESS_CLASS:
571 case VERIFY_ERROR_ACCESS_FIELD:
572 case VERIFY_ERROR_ACCESS_METHOD:
Ian Rogers08f753d2012-08-24 14:35:25 -0700573 case VERIFY_ERROR_INSTANTIATION:
574 case VERIFY_ERROR_CLASS_CHANGE:
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800575 if (Runtime::Current()->IsAotCompiler() || !can_load_classes_) {
jeffhaofaf459e2012-08-31 15:32:47 -0700576 // If we're optimistically running verification at compile time, turn NO_xxx, ACCESS_xxx,
577 // class change and instantiation errors into soft verification errors so that we re-verify
578 // at runtime. We may fail to find or to agree on access because of not yet available class
579 // loaders, or class loaders that will differ at runtime. In these cases, we don't want to
580 // affect the soundness of the code being compiled. Instead, the generated code runs "slow
581 // paths" that dynamically perform the verification and cause the behavior to be that akin
582 // to an interpreter.
583 error = VERIFY_ERROR_BAD_CLASS_SOFT;
584 } else {
Jeff Haoa3faaf42013-09-03 19:07:00 -0700585 // If we fail again at runtime, mark that this instruction would throw and force this
586 // method to be executed using the interpreter with checks.
jeffhaofaf459e2012-08-31 15:32:47 -0700587 have_pending_runtime_throw_failure_ = true;
Andreas Gamped7f8d052015-03-12 11:05:47 -0700588
589 // We need to save the work_line if the instruction wasn't throwing before. Otherwise we'll
590 // try to merge garbage.
591 // Note: this assumes that Fail is called before we do any work_line modifications.
592 const uint16_t* insns = code_item_->insns_ + work_insn_idx_;
593 const Instruction* inst = Instruction::At(insns);
594 int opcode_flags = Instruction::FlagsOf(inst->Opcode());
595
596 if ((opcode_flags & Instruction::kThrow) == 0 && CurrentInsnFlags()->IsInTry()) {
597 saved_line_->CopyFromLine(work_line_.get());
598 }
jeffhaofaf459e2012-08-31 15:32:47 -0700599 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700600 break;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700601 // Indication that verification should be retried at runtime.
602 case VERIFY_ERROR_BAD_CLASS_SOFT:
Jeff Haoee988952013-04-16 14:23:47 -0700603 if (!allow_soft_failures_) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700604 have_pending_hard_failure_ = true;
605 }
606 break;
jeffhaod5347e02012-03-22 17:25:05 -0700607 // Hard verification failures at compile time will still fail at runtime, so the class is
608 // marked as rejected to prevent it from being compiled.
Ian Rogersad0b3a32012-04-16 14:50:24 -0700609 case VERIFY_ERROR_BAD_CLASS_HARD: {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800610 if (Runtime::Current()->IsAotCompiler()) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700611 ClassReference ref(dex_file_, dex_file_->GetIndexForClassDef(*class_def_));
Vladimir Marko2b5eaa22013-12-13 13:59:30 +0000612 Runtime::Current()->GetCompilerCallbacks()->ClassRejected(ref);
jeffhaod1224c72012-02-29 13:43:08 -0800613 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700614 have_pending_hard_failure_ = true;
615 break;
Ian Rogers47a05882012-02-03 12:23:33 -0800616 }
617 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700618 failures_.push_back(error);
Elena Sayapina78480ec2014-08-15 15:52:42 +0700619 std::string location(StringPrintf("%s: [0x%X] ", PrettyMethod(dex_method_idx_, *dex_file_).c_str(),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700620 work_insn_idx_));
Elena Sayapina78480ec2014-08-15 15:52:42 +0700621 std::ostringstream* failure_message = new std::ostringstream(location, std::ostringstream::ate);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700622 failure_messages_.push_back(failure_message);
623 return *failure_message;
624}
625
Ian Rogers576ca0c2014-06-06 15:58:22 -0700626std::ostream& MethodVerifier::LogVerifyInfo() {
627 return info_messages_ << "VFY: " << PrettyMethod(dex_method_idx_, *dex_file_)
628 << '[' << reinterpret_cast<void*>(work_insn_idx_) << "] : ";
629}
630
Ian Rogersad0b3a32012-04-16 14:50:24 -0700631void MethodVerifier::PrependToLastFailMessage(std::string prepend) {
632 size_t failure_num = failure_messages_.size();
633 DCHECK_NE(failure_num, 0U);
634 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
635 prepend += last_fail_message->str();
Elena Sayapina78480ec2014-08-15 15:52:42 +0700636 failure_messages_[failure_num - 1] = new std::ostringstream(prepend, std::ostringstream::ate);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700637 delete last_fail_message;
638}
639
640void MethodVerifier::AppendToLastFailMessage(std::string append) {
641 size_t failure_num = failure_messages_.size();
642 DCHECK_NE(failure_num, 0U);
643 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
644 (*last_fail_message) << append;
Ian Rogers47a05882012-02-03 12:23:33 -0800645}
646
Ian Rogers776ac1f2012-04-13 23:36:36 -0700647bool MethodVerifier::ComputeWidthsAndCountOps() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700648 const uint16_t* insns = code_item_->insns_;
649 size_t insns_size = code_item_->insns_size_in_code_units_;
650 const Instruction* inst = Instruction::At(insns);
jeffhaobdb76512011-09-07 11:43:16 -0700651 size_t new_instance_count = 0;
652 size_t monitor_enter_count = 0;
Ian Rogersd81871c2011-10-03 13:57:23 -0700653 size_t dex_pc = 0;
jeffhaobdb76512011-09-07 11:43:16 -0700654
Ian Rogersd81871c2011-10-03 13:57:23 -0700655 while (dex_pc < insns_size) {
jeffhaobdb76512011-09-07 11:43:16 -0700656 Instruction::Code opcode = inst->Opcode();
Ian Rogersa9a82542013-10-04 11:17:26 -0700657 switch (opcode) {
658 case Instruction::APUT_OBJECT:
659 case Instruction::CHECK_CAST:
660 has_check_casts_ = true;
661 break;
662 case Instruction::INVOKE_VIRTUAL:
663 case Instruction::INVOKE_VIRTUAL_RANGE:
664 case Instruction::INVOKE_INTERFACE:
665 case Instruction::INVOKE_INTERFACE_RANGE:
666 has_virtual_or_interface_invokes_ = true;
667 break;
668 case Instruction::MONITOR_ENTER:
669 monitor_enter_count++;
670 break;
671 case Instruction::NEW_INSTANCE:
672 new_instance_count++;
673 break;
674 default:
675 break;
jeffhaobdb76512011-09-07 11:43:16 -0700676 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700677 size_t inst_size = inst->SizeInCodeUnits();
Ian Rogers7b078e82014-09-10 14:44:24 -0700678 insn_flags_[dex_pc].SetIsOpcode();
Ian Rogersd81871c2011-10-03 13:57:23 -0700679 dex_pc += inst_size;
Ian Rogers7b078e82014-09-10 14:44:24 -0700680 inst = inst->RelativeAt(inst_size);
jeffhaobdb76512011-09-07 11:43:16 -0700681 }
682
Ian Rogersd81871c2011-10-03 13:57:23 -0700683 if (dex_pc != insns_size) {
jeffhaod5347e02012-03-22 17:25:05 -0700684 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "code did not end where expected ("
685 << dex_pc << " vs. " << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700686 return false;
687 }
688
Ian Rogersd81871c2011-10-03 13:57:23 -0700689 new_instance_count_ = new_instance_count;
690 monitor_enter_count_ = monitor_enter_count;
jeffhaobdb76512011-09-07 11:43:16 -0700691 return true;
692}
693
Ian Rogers776ac1f2012-04-13 23:36:36 -0700694bool MethodVerifier::ScanTryCatchBlocks() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700695 uint32_t tries_size = code_item_->tries_size_;
jeffhaobdb76512011-09-07 11:43:16 -0700696 if (tries_size == 0) {
697 return true;
698 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700699 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Ian Rogers0571d352011-11-03 19:51:38 -0700700 const DexFile::TryItem* tries = DexFile::GetTryItems(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -0700701
702 for (uint32_t idx = 0; idx < tries_size; idx++) {
703 const DexFile::TryItem* try_item = &tries[idx];
704 uint32_t start = try_item->start_addr_;
705 uint32_t end = start + try_item->insn_count_;
jeffhaobdb76512011-09-07 11:43:16 -0700706 if ((start >= end) || (start >= insns_size) || (end > insns_size)) {
jeffhaod5347e02012-03-22 17:25:05 -0700707 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad exception entry: startAddr=" << start
708 << " endAddr=" << end << " (size=" << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700709 return false;
710 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700711 if (!insn_flags_[start].IsOpcode()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700712 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
713 << "'try' block starts inside an instruction (" << start << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700714 return false;
715 }
Ian Rogers7b078e82014-09-10 14:44:24 -0700716 uint32_t dex_pc = start;
717 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
718 while (dex_pc < end) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700719 insn_flags_[dex_pc].SetInTry();
Ian Rogers7b078e82014-09-10 14:44:24 -0700720 size_t insn_size = inst->SizeInCodeUnits();
721 dex_pc += insn_size;
722 inst = inst->RelativeAt(insn_size);
jeffhaobdb76512011-09-07 11:43:16 -0700723 }
724 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800725 // Iterate over each of the handlers to verify target addresses.
Ian Rogers13735952014-10-08 12:43:28 -0700726 const uint8_t* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -0700727 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700728 ClassLinker* linker = Runtime::Current()->GetClassLinker();
jeffhaobdb76512011-09-07 11:43:16 -0700729 for (uint32_t idx = 0; idx < handlers_size; idx++) {
Ian Rogers0571d352011-11-03 19:51:38 -0700730 CatchHandlerIterator iterator(handlers_ptr);
731 for (; iterator.HasNext(); iterator.Next()) {
732 uint32_t dex_pc= iterator.GetHandlerAddress();
Ian Rogersd81871c2011-10-03 13:57:23 -0700733 if (!insn_flags_[dex_pc].IsOpcode()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700734 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
735 << "exception handler starts at bad address (" << dex_pc << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700736 return false;
737 }
Stephen Kyle9bc61992014-09-22 13:53:15 +0100738 if (!CheckNotMoveResult(code_item_->insns_, dex_pc)) {
739 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
740 << "exception handler begins with move-result* (" << dex_pc << ")";
741 return false;
742 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700743 insn_flags_[dex_pc].SetBranchTarget();
Ian Rogers28ad40d2011-10-27 15:19:26 -0700744 // Ensure exception types are resolved so that they don't need resolution to be delivered,
745 // unresolved exception types will be ignored by exception delivery
Ian Rogers0571d352011-11-03 19:51:38 -0700746 if (iterator.GetHandlerTypeIndex() != DexFile::kDexNoIndex16) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800747 mirror::Class* exception_type = linker->ResolveType(*dex_file_,
748 iterator.GetHandlerTypeIndex(),
Mathieu Chartierbf99f772014-08-23 16:37:27 -0700749 dex_cache_, class_loader_);
Ian Rogers7b078e82014-09-10 14:44:24 -0700750 if (exception_type == nullptr) {
751 DCHECK(self_->IsExceptionPending());
752 self_->ClearException();
Ian Rogers28ad40d2011-10-27 15:19:26 -0700753 }
754 }
jeffhaobdb76512011-09-07 11:43:16 -0700755 }
Ian Rogers0571d352011-11-03 19:51:38 -0700756 handlers_ptr = iterator.EndDataPointer();
jeffhaobdb76512011-09-07 11:43:16 -0700757 }
jeffhaobdb76512011-09-07 11:43:16 -0700758 return true;
759}
760
Ian Rogers776ac1f2012-04-13 23:36:36 -0700761bool MethodVerifier::VerifyInstructions() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700762 const Instruction* inst = Instruction::At(code_item_->insns_);
jeffhaoba5ebb92011-08-25 17:24:37 -0700763
Ian Rogers0c7abda2012-09-19 13:33:42 -0700764 /* 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 -0700765 insn_flags_[0].SetBranchTarget();
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700766 insn_flags_[0].SetCompileTimeInfoPoint();
Ian Rogersd81871c2011-10-03 13:57:23 -0700767
768 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700769 for (uint32_t dex_pc = 0; dex_pc < insns_size;) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700770 if (!VerifyInstruction(inst, dex_pc)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700771 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -0700772 return false;
773 }
774 /* Flag instructions that are garbage collection points */
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700775 // All invoke points are marked as "Throw" points already.
776 // We are relying on this to also count all the invokes as interesting.
Vladimir Marko8b858e12014-11-27 14:52:37 +0000777 if (inst->IsBranch()) {
778 insn_flags_[dex_pc].SetCompileTimeInfoPoint();
779 // The compiler also needs safepoints for fall-through to loop heads.
780 // Such a loop head must be a target of a branch.
781 int32_t offset = 0;
782 bool cond, self_ok;
783 bool target_ok = GetBranchOffset(dex_pc, &offset, &cond, &self_ok);
784 DCHECK(target_ok);
785 insn_flags_[dex_pc + offset].SetCompileTimeInfoPoint();
786 } else if (inst->IsSwitch() || inst->IsThrow()) {
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700787 insn_flags_[dex_pc].SetCompileTimeInfoPoint();
Ian Rogersb8c78592013-07-25 23:52:52 +0000788 } else if (inst->IsReturn()) {
789 insn_flags_[dex_pc].SetCompileTimeInfoPointAndReturn();
Ian Rogersd81871c2011-10-03 13:57:23 -0700790 }
791 dex_pc += inst->SizeInCodeUnits();
792 inst = inst->Next();
793 }
794 return true;
795}
796
Ian Rogers776ac1f2012-04-13 23:36:36 -0700797bool MethodVerifier::VerifyInstruction(const Instruction* inst, uint32_t code_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700798 bool result = true;
799 switch (inst->GetVerifyTypeArgumentA()) {
800 case Instruction::kVerifyRegA:
Ian Rogers29a26482014-05-02 15:27:29 -0700801 result = result && CheckRegisterIndex(inst->VRegA());
Ian Rogersd81871c2011-10-03 13:57:23 -0700802 break;
803 case Instruction::kVerifyRegAWide:
Ian Rogers29a26482014-05-02 15:27:29 -0700804 result = result && CheckWideRegisterIndex(inst->VRegA());
Ian Rogersd81871c2011-10-03 13:57:23 -0700805 break;
806 }
807 switch (inst->GetVerifyTypeArgumentB()) {
808 case Instruction::kVerifyRegB:
Ian Rogers29a26482014-05-02 15:27:29 -0700809 result = result && CheckRegisterIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700810 break;
811 case Instruction::kVerifyRegBField:
Ian Rogers29a26482014-05-02 15:27:29 -0700812 result = result && CheckFieldIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700813 break;
814 case Instruction::kVerifyRegBMethod:
Ian Rogers29a26482014-05-02 15:27:29 -0700815 result = result && CheckMethodIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700816 break;
817 case Instruction::kVerifyRegBNewInstance:
Ian Rogers29a26482014-05-02 15:27:29 -0700818 result = result && CheckNewInstance(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700819 break;
820 case Instruction::kVerifyRegBString:
Ian Rogers29a26482014-05-02 15:27:29 -0700821 result = result && CheckStringIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700822 break;
823 case Instruction::kVerifyRegBType:
Ian Rogers29a26482014-05-02 15:27:29 -0700824 result = result && CheckTypeIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700825 break;
826 case Instruction::kVerifyRegBWide:
Ian Rogers29a26482014-05-02 15:27:29 -0700827 result = result && CheckWideRegisterIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700828 break;
829 }
830 switch (inst->GetVerifyTypeArgumentC()) {
831 case Instruction::kVerifyRegC:
Ian Rogers29a26482014-05-02 15:27:29 -0700832 result = result && CheckRegisterIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700833 break;
834 case Instruction::kVerifyRegCField:
Ian Rogers29a26482014-05-02 15:27:29 -0700835 result = result && CheckFieldIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700836 break;
837 case Instruction::kVerifyRegCNewArray:
Ian Rogers29a26482014-05-02 15:27:29 -0700838 result = result && CheckNewArray(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700839 break;
840 case Instruction::kVerifyRegCType:
Ian Rogers29a26482014-05-02 15:27:29 -0700841 result = result && CheckTypeIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700842 break;
843 case Instruction::kVerifyRegCWide:
Ian Rogers29a26482014-05-02 15:27:29 -0700844 result = result && CheckWideRegisterIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700845 break;
846 }
847 switch (inst->GetVerifyExtraFlags()) {
848 case Instruction::kVerifyArrayData:
849 result = result && CheckArrayData(code_offset);
850 break;
851 case Instruction::kVerifyBranchTarget:
852 result = result && CheckBranchTarget(code_offset);
853 break;
854 case Instruction::kVerifySwitchTargets:
855 result = result && CheckSwitchTargets(code_offset);
856 break;
Andreas Gampec3314312014-06-19 18:13:29 -0700857 case Instruction::kVerifyVarArgNonZero:
858 // Fall-through.
Ian Rogers29a26482014-05-02 15:27:29 -0700859 case Instruction::kVerifyVarArg: {
Andreas Gampec3314312014-06-19 18:13:29 -0700860 if (inst->GetVerifyExtraFlags() == Instruction::kVerifyVarArgNonZero && inst->VRegA() <= 0) {
861 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << inst->VRegA() << ") in "
862 "non-range invoke";
863 return false;
864 }
Ian Rogers29a26482014-05-02 15:27:29 -0700865 uint32_t args[Instruction::kMaxVarArgRegs];
866 inst->GetVarArgs(args);
867 result = result && CheckVarArgRegs(inst->VRegA(), args);
Ian Rogersd81871c2011-10-03 13:57:23 -0700868 break;
Ian Rogers29a26482014-05-02 15:27:29 -0700869 }
Andreas Gampec3314312014-06-19 18:13:29 -0700870 case Instruction::kVerifyVarArgRangeNonZero:
871 // Fall-through.
Ian Rogersd81871c2011-10-03 13:57:23 -0700872 case Instruction::kVerifyVarArgRange:
Andreas Gampec3314312014-06-19 18:13:29 -0700873 if (inst->GetVerifyExtraFlags() == Instruction::kVerifyVarArgRangeNonZero &&
874 inst->VRegA() <= 0) {
875 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << inst->VRegA() << ") in "
876 "range invoke";
877 return false;
878 }
Ian Rogers29a26482014-05-02 15:27:29 -0700879 result = result && CheckVarArgRangeRegs(inst->VRegA(), inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700880 break;
881 case Instruction::kVerifyError:
jeffhaod5347e02012-03-22 17:25:05 -0700882 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected opcode " << inst->Name();
Ian Rogersd81871c2011-10-03 13:57:23 -0700883 result = false;
884 break;
885 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800886 if (inst->GetVerifyIsRuntimeOnly() && Runtime::Current()->IsAotCompiler() && !verify_to_dump_) {
Ian Rogers5fb22a92014-06-13 10:31:28 -0700887 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "opcode only expected at runtime " << inst->Name();
888 result = false;
889 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700890 return result;
891}
892
Ian Rogers7b078e82014-09-10 14:44:24 -0700893inline bool MethodVerifier::CheckRegisterIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700894 if (idx >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700895 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register index out of range (" << idx << " >= "
896 << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700897 return false;
898 }
899 return true;
900}
901
Ian Rogers7b078e82014-09-10 14:44:24 -0700902inline bool MethodVerifier::CheckWideRegisterIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700903 if (idx + 1 >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700904 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "wide register index out of range (" << idx
905 << "+1 >= " << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700906 return false;
907 }
908 return true;
909}
910
Ian Rogers7b078e82014-09-10 14:44:24 -0700911inline bool MethodVerifier::CheckFieldIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700912 if (idx >= dex_file_->GetHeader().field_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700913 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad field index " << idx << " (max "
914 << dex_file_->GetHeader().field_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700915 return false;
916 }
917 return true;
918}
919
Ian Rogers7b078e82014-09-10 14:44:24 -0700920inline bool MethodVerifier::CheckMethodIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700921 if (idx >= dex_file_->GetHeader().method_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700922 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad method index " << idx << " (max "
923 << dex_file_->GetHeader().method_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700924 return false;
925 }
926 return true;
927}
928
Ian Rogers7b078e82014-09-10 14:44:24 -0700929inline bool MethodVerifier::CheckNewInstance(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700930 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700931 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
932 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700933 return false;
934 }
935 // We don't need the actual class, just a pointer to the class name.
Ian Rogers0571d352011-11-03 19:51:38 -0700936 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -0700937 if (descriptor[0] != 'L') {
jeffhaod5347e02012-03-22 17:25:05 -0700938 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "can't call new-instance on type '" << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -0700939 return false;
940 }
941 return true;
942}
943
Ian Rogers7b078e82014-09-10 14:44:24 -0700944inline bool MethodVerifier::CheckStringIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700945 if (idx >= dex_file_->GetHeader().string_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700946 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad string index " << idx << " (max "
947 << dex_file_->GetHeader().string_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700948 return false;
949 }
950 return true;
951}
952
Ian Rogers7b078e82014-09-10 14:44:24 -0700953inline bool MethodVerifier::CheckTypeIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700954 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700955 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
956 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700957 return false;
958 }
959 return true;
960}
961
Ian Rogers776ac1f2012-04-13 23:36:36 -0700962bool MethodVerifier::CheckNewArray(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700963 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700964 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
965 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700966 return false;
967 }
968 int bracket_count = 0;
Ian Rogers0571d352011-11-03 19:51:38 -0700969 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -0700970 const char* cp = descriptor;
971 while (*cp++ == '[') {
972 bracket_count++;
973 }
974 if (bracket_count == 0) {
975 /* The given class must be an array type. */
Brian Carlstrom93c33962013-07-26 10:37:43 -0700976 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
977 << "can't new-array class '" << descriptor << "' (not an array)";
Ian Rogersd81871c2011-10-03 13:57:23 -0700978 return false;
979 } else if (bracket_count > 255) {
980 /* It is illegal to create an array of more than 255 dimensions. */
Brian Carlstrom93c33962013-07-26 10:37:43 -0700981 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
982 << "can't new-array class '" << descriptor << "' (exceeds limit)";
Ian Rogersd81871c2011-10-03 13:57:23 -0700983 return false;
984 }
985 return true;
986}
987
Ian Rogers776ac1f2012-04-13 23:36:36 -0700988bool MethodVerifier::CheckArrayData(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700989 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
990 const uint16_t* insns = code_item_->insns_ + cur_offset;
991 const uint16_t* array_data;
992 int32_t array_data_offset;
993
994 DCHECK_LT(cur_offset, insn_count);
995 /* make sure the start of the array data table is in range */
996 array_data_offset = insns[1] | (((int32_t) insns[2]) << 16);
997 if ((int32_t) cur_offset + array_data_offset < 0 ||
998 cur_offset + array_data_offset + 2 >= insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -0700999 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data start: at " << cur_offset
Brian Carlstrom93c33962013-07-26 10:37:43 -07001000 << ", data offset " << array_data_offset
1001 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -07001002 return false;
1003 }
1004 /* offset to array data table is a relative branch-style offset */
1005 array_data = insns + array_data_offset;
1006 /* make sure the table is 32-bit aligned */
Ian Rogersef7d42f2014-01-06 12:55:46 -08001007 if ((reinterpret_cast<uintptr_t>(array_data) & 0x03) != 0) {
jeffhaod5347e02012-03-22 17:25:05 -07001008 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned array data table: at " << cur_offset
1009 << ", data offset " << array_data_offset;
Ian Rogersd81871c2011-10-03 13:57:23 -07001010 return false;
1011 }
1012 uint32_t value_width = array_data[1];
Elliott Hughes398f64b2012-03-26 18:05:48 -07001013 uint32_t value_count = *reinterpret_cast<const uint32_t*>(&array_data[2]);
Ian Rogersd81871c2011-10-03 13:57:23 -07001014 uint32_t table_size = 4 + (value_width * value_count + 1) / 2;
1015 /* make sure the end of the switch is in range */
1016 if (cur_offset + array_data_offset + table_size > insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -07001017 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data end: at " << cur_offset
1018 << ", data offset " << array_data_offset << ", end "
1019 << cur_offset + array_data_offset + table_size
1020 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -07001021 return false;
1022 }
1023 return true;
1024}
1025
Ian Rogers776ac1f2012-04-13 23:36:36 -07001026bool MethodVerifier::CheckBranchTarget(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001027 int32_t offset;
1028 bool isConditional, selfOkay;
1029 if (!GetBranchOffset(cur_offset, &offset, &isConditional, &selfOkay)) {
1030 return false;
1031 }
1032 if (!selfOkay && offset == 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001033 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch offset of zero not allowed at"
1034 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -07001035 return false;
1036 }
Elliott Hughes81ff3182012-03-23 20:35:56 -07001037 // Check for 32-bit overflow. This isn't strictly necessary if we can depend on the runtime
1038 // to have identical "wrap-around" behavior, but it's unwise to depend on that.
Ian Rogersd81871c2011-10-03 13:57:23 -07001039 if (((int64_t) cur_offset + (int64_t) offset) != (int64_t) (cur_offset + offset)) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001040 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch target overflow "
1041 << reinterpret_cast<void*>(cur_offset) << " +" << offset;
Ian Rogersd81871c2011-10-03 13:57:23 -07001042 return false;
1043 }
1044 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
1045 int32_t abs_offset = cur_offset + offset;
Brian Carlstrom93c33962013-07-26 10:37:43 -07001046 if (abs_offset < 0 ||
1047 (uint32_t) abs_offset >= insn_count ||
1048 !insn_flags_[abs_offset].IsOpcode()) {
jeffhaod5347e02012-03-22 17:25:05 -07001049 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid branch target " << offset << " (-> "
Elliott Hughes398f64b2012-03-26 18:05:48 -07001050 << reinterpret_cast<void*>(abs_offset) << ") at "
1051 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -07001052 return false;
1053 }
1054 insn_flags_[abs_offset].SetBranchTarget();
1055 return true;
1056}
1057
Ian Rogers776ac1f2012-04-13 23:36:36 -07001058bool MethodVerifier::GetBranchOffset(uint32_t cur_offset, int32_t* pOffset, bool* pConditional,
Ian Rogersd81871c2011-10-03 13:57:23 -07001059 bool* selfOkay) {
1060 const uint16_t* insns = code_item_->insns_ + cur_offset;
1061 *pConditional = false;
1062 *selfOkay = false;
jeffhaoba5ebb92011-08-25 17:24:37 -07001063 switch (*insns & 0xff) {
1064 case Instruction::GOTO:
1065 *pOffset = ((int16_t) *insns) >> 8;
jeffhaoba5ebb92011-08-25 17:24:37 -07001066 break;
1067 case Instruction::GOTO_32:
1068 *pOffset = insns[1] | (((uint32_t) insns[2]) << 16);
jeffhaoba5ebb92011-08-25 17:24:37 -07001069 *selfOkay = true;
1070 break;
1071 case Instruction::GOTO_16:
1072 *pOffset = (int16_t) insns[1];
jeffhaoba5ebb92011-08-25 17:24:37 -07001073 break;
1074 case Instruction::IF_EQ:
1075 case Instruction::IF_NE:
1076 case Instruction::IF_LT:
1077 case Instruction::IF_GE:
1078 case Instruction::IF_GT:
1079 case Instruction::IF_LE:
1080 case Instruction::IF_EQZ:
1081 case Instruction::IF_NEZ:
1082 case Instruction::IF_LTZ:
1083 case Instruction::IF_GEZ:
1084 case Instruction::IF_GTZ:
1085 case Instruction::IF_LEZ:
1086 *pOffset = (int16_t) insns[1];
1087 *pConditional = true;
jeffhaoba5ebb92011-08-25 17:24:37 -07001088 break;
1089 default:
1090 return false;
jeffhaoba5ebb92011-08-25 17:24:37 -07001091 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001092 return true;
1093}
1094
Ian Rogers776ac1f2012-04-13 23:36:36 -07001095bool MethodVerifier::CheckSwitchTargets(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001096 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001097 DCHECK_LT(cur_offset, insn_count);
Ian Rogersd81871c2011-10-03 13:57:23 -07001098 const uint16_t* insns = code_item_->insns_ + cur_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001099 /* make sure the start of the switch is in range */
Ian Rogersd81871c2011-10-03 13:57:23 -07001100 int32_t switch_offset = insns[1] | ((int32_t) insns[2]) << 16;
Jeff Hao9ccd1512015-03-20 18:11:45 -07001101 if ((int32_t) cur_offset + switch_offset < 0 || cur_offset + switch_offset + 2 > insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -07001102 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch start: at " << cur_offset
Brian Carlstrom93c33962013-07-26 10:37:43 -07001103 << ", switch offset " << switch_offset
1104 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001105 return false;
1106 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001107 /* offset to switch table is a relative branch-style offset */
Ian Rogersd81871c2011-10-03 13:57:23 -07001108 const uint16_t* switch_insns = insns + switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001109 /* make sure the table is 32-bit aligned */
Ian Rogersef7d42f2014-01-06 12:55:46 -08001110 if ((reinterpret_cast<uintptr_t>(switch_insns) & 0x03) != 0) {
jeffhaod5347e02012-03-22 17:25:05 -07001111 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned switch table: at " << cur_offset
1112 << ", switch offset " << switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001113 return false;
1114 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001115 uint32_t switch_count = switch_insns[1];
1116 int32_t keys_offset, targets_offset;
1117 uint16_t expected_signature;
jeffhaoba5ebb92011-08-25 17:24:37 -07001118 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
1119 /* 0=sig, 1=count, 2/3=firstKey */
1120 targets_offset = 4;
1121 keys_offset = -1;
1122 expected_signature = Instruction::kPackedSwitchSignature;
1123 } else {
1124 /* 0=sig, 1=count, 2..count*2 = keys */
1125 keys_offset = 2;
1126 targets_offset = 2 + 2 * switch_count;
1127 expected_signature = Instruction::kSparseSwitchSignature;
1128 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001129 uint32_t table_size = targets_offset + switch_count * 2;
jeffhaoba5ebb92011-08-25 17:24:37 -07001130 if (switch_insns[0] != expected_signature) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001131 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1132 << StringPrintf("wrong signature for switch table (%x, wanted %x)",
1133 switch_insns[0], expected_signature);
jeffhaoba5ebb92011-08-25 17:24:37 -07001134 return false;
1135 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001136 /* make sure the end of the switch is in range */
1137 if (cur_offset + switch_offset + table_size > (uint32_t) insn_count) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001138 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch end: at " << cur_offset
1139 << ", switch offset " << switch_offset
1140 << ", end " << (cur_offset + switch_offset + table_size)
jeffhaod5347e02012-03-22 17:25:05 -07001141 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001142 return false;
1143 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001144 /* for a sparse switch, verify the keys are in ascending order */
1145 if (keys_offset > 0 && switch_count > 1) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001146 int32_t last_key = switch_insns[keys_offset] | (switch_insns[keys_offset + 1] << 16);
1147 for (uint32_t targ = 1; targ < switch_count; targ++) {
jeffhaoba5ebb92011-08-25 17:24:37 -07001148 int32_t key = (int32_t) switch_insns[keys_offset + targ * 2] |
1149 (int32_t) (switch_insns[keys_offset + targ * 2 + 1] << 16);
1150 if (key <= last_key) {
jeffhaod5347e02012-03-22 17:25:05 -07001151 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid packed switch: last key=" << last_key
1152 << ", this=" << key;
jeffhaoba5ebb92011-08-25 17:24:37 -07001153 return false;
1154 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001155 last_key = key;
1156 }
1157 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001158 /* verify each switch target */
Ian Rogersd81871c2011-10-03 13:57:23 -07001159 for (uint32_t targ = 0; targ < switch_count; targ++) {
1160 int32_t offset = (int32_t) switch_insns[targets_offset + targ * 2] |
1161 (int32_t) (switch_insns[targets_offset + targ * 2 + 1] << 16);
1162 int32_t abs_offset = cur_offset + offset;
Brian Carlstrom93c33962013-07-26 10:37:43 -07001163 if (abs_offset < 0 ||
1164 abs_offset >= (int32_t) insn_count ||
1165 !insn_flags_[abs_offset].IsOpcode()) {
1166 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch target " << offset
1167 << " (-> " << reinterpret_cast<void*>(abs_offset) << ") at "
1168 << reinterpret_cast<void*>(cur_offset)
1169 << "[" << targ << "]";
jeffhaoba5ebb92011-08-25 17:24:37 -07001170 return false;
1171 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001172 insn_flags_[abs_offset].SetBranchTarget();
1173 }
1174 return true;
1175}
1176
Ian Rogers776ac1f2012-04-13 23:36:36 -07001177bool MethodVerifier::CheckVarArgRegs(uint32_t vA, uint32_t arg[]) {
Ian Rogers29a26482014-05-02 15:27:29 -07001178 if (vA > Instruction::kMaxVarArgRegs) {
jeffhaod5347e02012-03-22 17:25:05 -07001179 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << vA << ") in non-range invoke)";
Ian Rogersd81871c2011-10-03 13:57:23 -07001180 return false;
1181 }
1182 uint16_t registers_size = code_item_->registers_size_;
1183 for (uint32_t idx = 0; idx < vA; idx++) {
jeffhao457cc512012-02-02 16:55:13 -08001184 if (arg[idx] >= registers_size) {
jeffhaod5347e02012-03-22 17:25:05 -07001185 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index (" << arg[idx]
1186 << ") in non-range invoke (>= " << registers_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001187 return false;
1188 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001189 }
1190
1191 return true;
1192}
1193
Ian Rogers776ac1f2012-04-13 23:36:36 -07001194bool MethodVerifier::CheckVarArgRangeRegs(uint32_t vA, uint32_t vC) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001195 uint16_t registers_size = code_item_->registers_size_;
1196 // vA/vC are unsigned 8-bit/16-bit quantities for /range instructions, so there's no risk of
1197 // integer overflow when adding them here.
1198 if (vA + vC > registers_size) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001199 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index " << vA << "+" << vC
1200 << " in range invoke (> " << registers_size << ")";
jeffhaoba5ebb92011-08-25 17:24:37 -07001201 return false;
1202 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001203 return true;
1204}
1205
Ian Rogers776ac1f2012-04-13 23:36:36 -07001206bool MethodVerifier::VerifyCodeFlow() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001207 uint16_t registers_size = code_item_->registers_size_;
1208 uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaobdb76512011-09-07 11:43:16 -07001209
Ian Rogersd81871c2011-10-03 13:57:23 -07001210 /* Create and initialize table holding register status */
Brian Carlstrom93c33962013-07-26 10:37:43 -07001211 reg_table_.Init(kTrackCompilerInterestPoints,
1212 insn_flags_.get(),
1213 insns_size,
1214 registers_size,
1215 this);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07001216
jeffhaobdb76512011-09-07 11:43:16 -07001217
Ian Rogersd0fbd852013-09-24 18:17:04 -07001218 work_line_.reset(RegisterLine::Create(registers_size, this));
1219 saved_line_.reset(RegisterLine::Create(registers_size, this));
jeffhaobdb76512011-09-07 11:43:16 -07001220
Ian Rogersd81871c2011-10-03 13:57:23 -07001221 /* Initialize register types of method arguments. */
1222 if (!SetTypesFromSignature()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001223 DCHECK_NE(failures_.size(), 0U);
1224 std::string prepend("Bad signature in ");
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001225 prepend += PrettyMethod(dex_method_idx_, *dex_file_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001226 PrependToLastFailMessage(prepend);
Ian Rogersd81871c2011-10-03 13:57:23 -07001227 return false;
1228 }
1229 /* Perform code flow verification. */
1230 if (!CodeFlowVerifyMethod()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001231 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -07001232 return false;
jeffhaobdb76512011-09-07 11:43:16 -07001233 }
jeffhaobdb76512011-09-07 11:43:16 -07001234 return true;
1235}
1236
Ian Rogersad0b3a32012-04-16 14:50:24 -07001237std::ostream& MethodVerifier::DumpFailures(std::ostream& os) {
1238 DCHECK_EQ(failures_.size(), failure_messages_.size());
Jeff Hao4137f482013-11-22 11:44:57 -08001239 for (size_t i = 0; i < failures_.size(); ++i) {
1240 os << failure_messages_[i]->str() << "\n";
Ian Rogersad0b3a32012-04-16 14:50:24 -07001241 }
1242 return os;
1243}
1244
Ian Rogers776ac1f2012-04-13 23:36:36 -07001245void MethodVerifier::Dump(std::ostream& os) {
Ian Rogers7b078e82014-09-10 14:44:24 -07001246 if (code_item_ == nullptr) {
Elliott Hughesc073b072012-05-24 19:29:17 -07001247 os << "Native method\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001248 return;
jeffhaobdb76512011-09-07 11:43:16 -07001249 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001250 {
1251 os << "Register Types:\n";
1252 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
1253 std::ostream indent_os(&indent_filter);
1254 reg_types_.Dump(indent_os);
1255 }
Ian Rogersb4903572012-10-11 11:52:56 -07001256 os << "Dumping instructions and register lines:\n";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001257 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
1258 std::ostream indent_os(&indent_filter);
Ian Rogersd81871c2011-10-03 13:57:23 -07001259 const Instruction* inst = Instruction::At(code_item_->insns_);
1260 for (size_t dex_pc = 0; dex_pc < code_item_->insns_size_in_code_units_;
Ian Rogers7b078e82014-09-10 14:44:24 -07001261 dex_pc += inst->SizeInCodeUnits()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001262 RegisterLine* reg_line = reg_table_.GetLine(dex_pc);
Ian Rogers7b078e82014-09-10 14:44:24 -07001263 if (reg_line != nullptr) {
1264 indent_os << reg_line->Dump(this) << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07001265 }
Ian Rogers7b3ddd22013-02-21 15:19:52 -08001266 indent_os << StringPrintf("0x%04zx", dex_pc) << ": " << insn_flags_[dex_pc].ToString() << " ";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001267 const bool kDumpHexOfInstruction = false;
1268 if (kDumpHexOfInstruction) {
1269 indent_os << inst->DumpHex(5) << " ";
1270 }
1271 indent_os << inst->DumpString(dex_file_) << "\n";
jeffhaoba5ebb92011-08-25 17:24:37 -07001272 inst = inst->Next();
1273 }
jeffhaobdb76512011-09-07 11:43:16 -07001274}
1275
Ian Rogersd81871c2011-10-03 13:57:23 -07001276static bool IsPrimitiveDescriptor(char descriptor) {
1277 switch (descriptor) {
jeffhaobdb76512011-09-07 11:43:16 -07001278 case 'I':
1279 case 'C':
1280 case 'S':
1281 case 'B':
1282 case 'Z':
jeffhaobdb76512011-09-07 11:43:16 -07001283 case 'F':
1284 case 'D':
1285 case 'J':
Ian Rogersd81871c2011-10-03 13:57:23 -07001286 return true;
jeffhaobdb76512011-09-07 11:43:16 -07001287 default:
1288 return false;
1289 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001290}
1291
Ian Rogers776ac1f2012-04-13 23:36:36 -07001292bool MethodVerifier::SetTypesFromSignature() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001293 RegisterLine* reg_line = reg_table_.GetLine(0);
Andreas Gampeef0b1a12015-06-19 20:37:46 -07001294
1295 // Should have been verified earlier.
1296 DCHECK_GE(code_item_->registers_size_, code_item_->ins_size_);
1297
1298 uint32_t arg_start = code_item_->registers_size_ - code_item_->ins_size_;
Ian Rogersd81871c2011-10-03 13:57:23 -07001299 size_t expected_args = code_item_->ins_size_; /* long/double count as two */
jeffhaobdb76512011-09-07 11:43:16 -07001300
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001301 // Include the "this" pointer.
Ian Rogersd81871c2011-10-03 13:57:23 -07001302 size_t cur_arg = 0;
Ian Rogersad0b3a32012-04-16 14:50:24 -07001303 if (!IsStatic()) {
Andreas Gampeef0b1a12015-06-19 20:37:46 -07001304 if (expected_args == 0) {
1305 // Expect at least a receiver.
1306 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected 0 args, but method is not static";
1307 return false;
1308 }
1309
Ian Rogersd81871c2011-10-03 13:57:23 -07001310 // If this is a constructor for a class other than java.lang.Object, mark the first ("this")
1311 // argument as uninitialized. This restricts field access until the superclass constructor is
1312 // called.
Ian Rogersd8f69b02014-09-10 21:43:52 +00001313 const RegType& declaring_class = GetDeclaringClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07001314 if (IsConstructor() && !declaring_class.IsJavaLangObject()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07001315 reg_line->SetRegisterType(this, arg_start + cur_arg,
Ian Rogersd81871c2011-10-03 13:57:23 -07001316 reg_types_.UninitializedThisArgument(declaring_class));
1317 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07001318 reg_line->SetRegisterType(this, arg_start + cur_arg, declaring_class);
jeffhaobdb76512011-09-07 11:43:16 -07001319 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001320 cur_arg++;
jeffhaobdb76512011-09-07 11:43:16 -07001321 }
1322
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001323 const DexFile::ProtoId& proto_id =
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001324 dex_file_->GetMethodPrototype(dex_file_->GetMethodId(dex_method_idx_));
Ian Rogers0571d352011-11-03 19:51:38 -07001325 DexFileParameterIterator iterator(*dex_file_, proto_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07001326
1327 for (; iterator.HasNext(); iterator.Next()) {
1328 const char* descriptor = iterator.GetDescriptor();
Ian Rogers7b078e82014-09-10 14:44:24 -07001329 if (descriptor == nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001330 LOG(FATAL) << "Null descriptor";
1331 }
1332 if (cur_arg >= expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07001333 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1334 << " args, found more (" << descriptor << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001335 return false;
1336 }
1337 switch (descriptor[0]) {
1338 case 'L':
1339 case '[':
1340 // We assume that reference arguments are initialized. The only way it could be otherwise
1341 // (assuming the caller was verified) is if the current method is <init>, but in that case
1342 // it's effectively considered initialized the instant we reach here (in the sense that we
1343 // can return without doing anything or call virtual methods).
1344 {
Ian Rogersd8f69b02014-09-10 21:43:52 +00001345 const RegType& reg_type = ResolveClassAndCheckAccess(iterator.GetTypeIdx());
Sebastien Hertz2ed76f92014-04-22 17:11:08 +02001346 if (!reg_type.IsNonZeroReferenceTypes()) {
1347 DCHECK(HasFailures());
1348 return false;
1349 }
Ian Rogers7b078e82014-09-10 14:44:24 -07001350 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001351 }
1352 break;
1353 case 'Z':
Ian Rogers7b078e82014-09-10 14:44:24 -07001354 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_types_.Boolean());
Ian Rogersd81871c2011-10-03 13:57:23 -07001355 break;
1356 case 'C':
Ian Rogers7b078e82014-09-10 14:44:24 -07001357 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_types_.Char());
Ian Rogersd81871c2011-10-03 13:57:23 -07001358 break;
1359 case 'B':
Ian Rogers7b078e82014-09-10 14:44:24 -07001360 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_types_.Byte());
Ian Rogersd81871c2011-10-03 13:57:23 -07001361 break;
1362 case 'I':
Ian Rogers7b078e82014-09-10 14:44:24 -07001363 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_types_.Integer());
Ian Rogersd81871c2011-10-03 13:57:23 -07001364 break;
1365 case 'S':
Ian Rogers7b078e82014-09-10 14:44:24 -07001366 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_types_.Short());
Ian Rogersd81871c2011-10-03 13:57:23 -07001367 break;
1368 case 'F':
Ian Rogers7b078e82014-09-10 14:44:24 -07001369 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_types_.Float());
Ian Rogersd81871c2011-10-03 13:57:23 -07001370 break;
1371 case 'J':
1372 case 'D': {
Andreas Gampe77cd4d62014-06-19 17:29:48 -07001373 if (cur_arg + 1 >= expected_args) {
1374 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1375 << " args, found more (" << descriptor << ")";
1376 return false;
1377 }
1378
Ian Rogers7b078e82014-09-10 14:44:24 -07001379 const RegType* lo_half;
1380 const RegType* hi_half;
1381 if (descriptor[0] == 'J') {
1382 lo_half = &reg_types_.LongLo();
1383 hi_half = &reg_types_.LongHi();
1384 } else {
1385 lo_half = &reg_types_.DoubleLo();
1386 hi_half = &reg_types_.DoubleHi();
1387 }
1388 reg_line->SetRegisterTypeWide(this, arg_start + cur_arg, *lo_half, *hi_half);
Ian Rogersd81871c2011-10-03 13:57:23 -07001389 cur_arg++;
1390 break;
1391 }
1392 default:
Brian Carlstrom93c33962013-07-26 10:37:43 -07001393 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected signature type char '"
1394 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001395 return false;
1396 }
1397 cur_arg++;
1398 }
1399 if (cur_arg != expected_args) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001400 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1401 << " arguments, found " << cur_arg;
Ian Rogersd81871c2011-10-03 13:57:23 -07001402 return false;
1403 }
1404 const char* descriptor = dex_file_->GetReturnTypeDescriptor(proto_id);
1405 // Validate return type. We don't do the type lookup; just want to make sure that it has the right
1406 // format. Only major difference from the method argument format is that 'V' is supported.
1407 bool result;
1408 if (IsPrimitiveDescriptor(descriptor[0]) || descriptor[0] == 'V') {
1409 result = descriptor[1] == '\0';
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001410 } else if (descriptor[0] == '[') { // single/multi-dimensional array of object/primitive
Ian Rogersd81871c2011-10-03 13:57:23 -07001411 size_t i = 0;
1412 do {
1413 i++;
1414 } while (descriptor[i] == '['); // process leading [
1415 if (descriptor[i] == 'L') { // object array
1416 do {
1417 i++; // find closing ;
1418 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1419 result = descriptor[i] == ';';
1420 } else { // primitive array
1421 result = IsPrimitiveDescriptor(descriptor[i]) && descriptor[i + 1] == '\0';
1422 }
1423 } else if (descriptor[0] == 'L') {
1424 // could be more thorough here, but shouldn't be required
1425 size_t i = 0;
1426 do {
1427 i++;
1428 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1429 result = descriptor[i] == ';';
1430 } else {
1431 result = false;
1432 }
1433 if (!result) {
jeffhaod5347e02012-03-22 17:25:05 -07001434 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected char in return type descriptor '"
1435 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001436 }
1437 return result;
jeffhaobdb76512011-09-07 11:43:16 -07001438}
1439
Ian Rogers776ac1f2012-04-13 23:36:36 -07001440bool MethodVerifier::CodeFlowVerifyMethod() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001441 const uint16_t* insns = code_item_->insns_;
1442 const uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaoba5ebb92011-08-25 17:24:37 -07001443
jeffhaobdb76512011-09-07 11:43:16 -07001444 /* Begin by marking the first instruction as "changed". */
Ian Rogersd81871c2011-10-03 13:57:23 -07001445 insn_flags_[0].SetChanged();
1446 uint32_t start_guess = 0;
jeffhaoba5ebb92011-08-25 17:24:37 -07001447
jeffhaobdb76512011-09-07 11:43:16 -07001448 /* Continue until no instructions are marked "changed". */
1449 while (true) {
Mathieu Chartier4306ef82014-12-19 18:41:47 -08001450 if (allow_thread_suspension_) {
1451 self_->AllowThreadSuspension();
1452 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001453 // Find the first marked one. Use "start_guess" as a way to find one quickly.
1454 uint32_t insn_idx = start_guess;
1455 for (; insn_idx < insns_size; insn_idx++) {
1456 if (insn_flags_[insn_idx].IsChanged())
jeffhaobdb76512011-09-07 11:43:16 -07001457 break;
1458 }
jeffhaobdb76512011-09-07 11:43:16 -07001459 if (insn_idx == insns_size) {
1460 if (start_guess != 0) {
1461 /* try again, starting from the top */
1462 start_guess = 0;
1463 continue;
1464 } else {
1465 /* all flags are clear */
1466 break;
1467 }
1468 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001469 // We carry the working set of registers from instruction to instruction. If this address can
1470 // be the target of a branch (or throw) instruction, or if we're skipping around chasing
1471 // "changed" flags, we need to load the set of registers from the table.
1472 // Because we always prefer to continue on to the next instruction, we should never have a
1473 // situation where we have a stray "changed" flag set on an instruction that isn't a branch
1474 // target.
1475 work_insn_idx_ = insn_idx;
1476 if (insn_flags_[insn_idx].IsBranchTarget()) {
1477 work_line_->CopyFromLine(reg_table_.GetLine(insn_idx));
Ian Rogersebbdd872014-07-07 23:53:08 -07001478 } else if (kIsDebugBuild) {
jeffhaobdb76512011-09-07 11:43:16 -07001479 /*
1480 * Sanity check: retrieve the stored register line (assuming
1481 * a full table) and make sure it actually matches.
1482 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001483 RegisterLine* register_line = reg_table_.GetLine(insn_idx);
Ian Rogers7b078e82014-09-10 14:44:24 -07001484 if (register_line != nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001485 if (work_line_->CompareLine(register_line) != 0) {
1486 Dump(std::cout);
1487 std::cout << info_messages_.str();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001488 LOG(FATAL) << "work_line diverged in " << PrettyMethod(dex_method_idx_, *dex_file_)
Elliott Hughesc073b072012-05-24 19:29:17 -07001489 << "@" << reinterpret_cast<void*>(work_insn_idx_) << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07001490 << " work_line=" << work_line_->Dump(this) << "\n"
1491 << " expected=" << register_line->Dump(this);
Ian Rogersd81871c2011-10-03 13:57:23 -07001492 }
jeffhaobdb76512011-09-07 11:43:16 -07001493 }
jeffhaobdb76512011-09-07 11:43:16 -07001494 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001495 if (!CodeFlowVerifyInstruction(&start_guess)) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001496 std::string prepend(PrettyMethod(dex_method_idx_, *dex_file_));
Ian Rogersad0b3a32012-04-16 14:50:24 -07001497 prepend += " failed to verify: ";
1498 PrependToLastFailMessage(prepend);
jeffhaoba5ebb92011-08-25 17:24:37 -07001499 return false;
1500 }
jeffhaobdb76512011-09-07 11:43:16 -07001501 /* Clear "changed" and mark as visited. */
Ian Rogersd81871c2011-10-03 13:57:23 -07001502 insn_flags_[insn_idx].SetVisited();
1503 insn_flags_[insn_idx].ClearChanged();
jeffhaobdb76512011-09-07 11:43:16 -07001504 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001505
Ian Rogers1c849e52012-06-28 14:00:33 -07001506 if (gDebugVerify) {
jeffhaobdb76512011-09-07 11:43:16 -07001507 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001508 * Scan for dead code. There's nothing "evil" about dead code
jeffhaobdb76512011-09-07 11:43:16 -07001509 * (besides the wasted space), but it indicates a flaw somewhere
1510 * down the line, possibly in the verifier.
1511 *
1512 * If we've substituted "always throw" instructions into the stream,
1513 * we are almost certainly going to have some dead code.
1514 */
1515 int dead_start = -1;
Ian Rogersd81871c2011-10-03 13:57:23 -07001516 uint32_t insn_idx = 0;
Ian Rogers7b078e82014-09-10 14:44:24 -07001517 for (; insn_idx < insns_size;
1518 insn_idx += Instruction::At(code_item_->insns_ + insn_idx)->SizeInCodeUnits()) {
jeffhaobdb76512011-09-07 11:43:16 -07001519 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001520 * Switch-statement data doesn't get "visited" by scanner. It
jeffhaobdb76512011-09-07 11:43:16 -07001521 * may or may not be preceded by a padding NOP (for alignment).
1522 */
1523 if (insns[insn_idx] == Instruction::kPackedSwitchSignature ||
1524 insns[insn_idx] == Instruction::kSparseSwitchSignature ||
1525 insns[insn_idx] == Instruction::kArrayDataSignature ||
Elliott Hughes380aaa72012-07-09 14:33:15 -07001526 (insns[insn_idx] == Instruction::NOP && (insn_idx + 1 < insns_size) &&
jeffhaobdb76512011-09-07 11:43:16 -07001527 (insns[insn_idx + 1] == Instruction::kPackedSwitchSignature ||
1528 insns[insn_idx + 1] == Instruction::kSparseSwitchSignature ||
1529 insns[insn_idx + 1] == Instruction::kArrayDataSignature))) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001530 insn_flags_[insn_idx].SetVisited();
jeffhaobdb76512011-09-07 11:43:16 -07001531 }
1532
Ian Rogersd81871c2011-10-03 13:57:23 -07001533 if (!insn_flags_[insn_idx].IsVisited()) {
jeffhaobdb76512011-09-07 11:43:16 -07001534 if (dead_start < 0)
1535 dead_start = insn_idx;
1536 } else if (dead_start >= 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001537 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start)
1538 << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaobdb76512011-09-07 11:43:16 -07001539 dead_start = -1;
1540 }
1541 }
1542 if (dead_start >= 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001543 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start)
1544 << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaoba5ebb92011-08-25 17:24:37 -07001545 }
Ian Rogersc9e463c2013-06-05 16:52:26 -07001546 // To dump the state of the verify after a method, do something like:
1547 // if (PrettyMethod(dex_method_idx_, *dex_file_) ==
1548 // "boolean java.lang.String.equals(java.lang.Object)") {
1549 // LOG(INFO) << info_messages_.str();
1550 // }
jeffhaoba5ebb92011-08-25 17:24:37 -07001551 }
jeffhaobdb76512011-09-07 11:43:16 -07001552 return true;
1553}
1554
Ian Rogers776ac1f2012-04-13 23:36:36 -07001555bool MethodVerifier::CodeFlowVerifyInstruction(uint32_t* start_guess) {
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001556 // If we're doing FindLocksAtDexPc, check whether we're at the dex pc we care about.
1557 // We want the state _before_ the instruction, for the case where the dex pc we're
1558 // interested in is itself a monitor-enter instruction (which is a likely place
1559 // for a thread to be suspended).
Ian Rogers7b078e82014-09-10 14:44:24 -07001560 if (monitor_enter_dex_pcs_ != nullptr && work_insn_idx_ == interesting_dex_pc_) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001561 monitor_enter_dex_pcs_->clear(); // The new work line is more accurate than the previous one.
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001562 for (size_t i = 0; i < work_line_->GetMonitorEnterCount(); ++i) {
1563 monitor_enter_dex_pcs_->push_back(work_line_->GetMonitorEnterDexPc(i));
1564 }
1565 }
1566
jeffhaobdb76512011-09-07 11:43:16 -07001567 /*
1568 * Once we finish decoding the instruction, we need to figure out where
jeffhaod1f0fde2011-09-08 17:25:33 -07001569 * we can go from here. There are three possible ways to transfer
jeffhaobdb76512011-09-07 11:43:16 -07001570 * control to another statement:
1571 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001572 * (1) Continue to the next instruction. Applies to all but
jeffhaobdb76512011-09-07 11:43:16 -07001573 * unconditional branches, method returns, and exception throws.
jeffhaod1f0fde2011-09-08 17:25:33 -07001574 * (2) Branch to one or more possible locations. Applies to branches
jeffhaobdb76512011-09-07 11:43:16 -07001575 * and switch statements.
jeffhaod1f0fde2011-09-08 17:25:33 -07001576 * (3) Exception handlers. Applies to any instruction that can
jeffhaobdb76512011-09-07 11:43:16 -07001577 * throw an exception that is handled by an encompassing "try"
1578 * block.
1579 *
1580 * We can also return, in which case there is no successor instruction
1581 * from this point.
1582 *
Elliott Hughesadb8c672012-03-06 16:49:32 -08001583 * The behavior can be determined from the opcode flags.
jeffhaobdb76512011-09-07 11:43:16 -07001584 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001585 const uint16_t* insns = code_item_->insns_ + work_insn_idx_;
1586 const Instruction* inst = Instruction::At(insns);
Ian Rogersa75a0132012-09-28 11:41:42 -07001587 int opcode_flags = Instruction::FlagsOf(inst->Opcode());
jeffhaobdb76512011-09-07 11:43:16 -07001588
jeffhaobdb76512011-09-07 11:43:16 -07001589 int32_t branch_target = 0;
jeffhaobdb76512011-09-07 11:43:16 -07001590 bool just_set_result = false;
Ian Rogers2c8a8572011-10-24 17:11:36 -07001591 if (gDebugVerify) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001592 // Generate processing back trace to debug verifier
Elliott Hughesc073b072012-05-24 19:29:17 -07001593 LogVerifyInfo() << "Processing " << inst->DumpString(dex_file_) << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07001594 << work_line_->Dump(this) << "\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001595 }
jeffhaobdb76512011-09-07 11:43:16 -07001596
1597 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001598 * Make a copy of the previous register state. If the instruction
jeffhaobdb76512011-09-07 11:43:16 -07001599 * can throw an exception, we will copy/merge this into the "catch"
1600 * address rather than work_line, because we don't want the result
1601 * from the "successful" code path (e.g. a check-cast that "improves"
1602 * a type) to be visible to the exception handler.
1603 */
Ian Rogers776ac1f2012-04-13 23:36:36 -07001604 if ((opcode_flags & Instruction::kThrow) != 0 && CurrentInsnFlags()->IsInTry()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001605 saved_line_->CopyFromLine(work_line_.get());
Ian Rogers1ff3c982014-08-12 02:30:58 -07001606 } else if (kIsDebugBuild) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001607 saved_line_->FillWithGarbage();
jeffhaobdb76512011-09-07 11:43:16 -07001608 }
1609
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07001610
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001611 // We need to ensure the work line is consistent while performing validation. When we spot a
1612 // peephole pattern we compute a new line for either the fallthrough instruction or the
1613 // branch target.
Ian Rogers700a4022014-05-19 16:49:03 -07001614 std::unique_ptr<RegisterLine> branch_line;
1615 std::unique_ptr<RegisterLine> fallthrough_line;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001616
Stephen Kyle7e541c92014-12-17 17:10:02 +00001617 /*
1618 * If we are in a constructor, and we currently have an UninitializedThis type
1619 * in a register somewhere, we need to make sure it isn't overwritten.
1620 */
1621 bool track_uninitialized_this = false;
1622 size_t uninitialized_this_loc = 0;
1623 if (IsConstructor()) {
1624 track_uninitialized_this = work_line_->GetUninitializedThisLoc(this, &uninitialized_this_loc);
1625 }
1626
Sebastien Hertz5243e912013-05-21 10:55:07 +02001627 switch (inst->Opcode()) {
jeffhaobdb76512011-09-07 11:43:16 -07001628 case Instruction::NOP:
1629 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001630 * A "pure" NOP has no effect on anything. Data tables start with
jeffhaobdb76512011-09-07 11:43:16 -07001631 * a signature that looks like a NOP; if we see one of these in
1632 * the course of executing code then we have a problem.
1633 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001634 if (inst->VRegA_10x() != 0) {
jeffhaod5347e02012-03-22 17:25:05 -07001635 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "encountered data table in instruction stream";
jeffhaobdb76512011-09-07 11:43:16 -07001636 }
1637 break;
1638
1639 case Instruction::MOVE:
Ian Rogers7b078e82014-09-10 14:44:24 -07001640 work_line_->CopyRegister1(this, inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategory1nr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001641 break;
jeffhaobdb76512011-09-07 11:43:16 -07001642 case Instruction::MOVE_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001643 work_line_->CopyRegister1(this, inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategory1nr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001644 break;
jeffhaobdb76512011-09-07 11:43:16 -07001645 case Instruction::MOVE_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001646 work_line_->CopyRegister1(this, inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategory1nr);
jeffhaobdb76512011-09-07 11:43:16 -07001647 break;
1648 case Instruction::MOVE_WIDE:
Ian Rogers7b078e82014-09-10 14:44:24 -07001649 work_line_->CopyRegister2(this, inst->VRegA_12x(), inst->VRegB_12x());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001650 break;
jeffhaobdb76512011-09-07 11:43:16 -07001651 case Instruction::MOVE_WIDE_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001652 work_line_->CopyRegister2(this, inst->VRegA_22x(), inst->VRegB_22x());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001653 break;
jeffhaobdb76512011-09-07 11:43:16 -07001654 case Instruction::MOVE_WIDE_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001655 work_line_->CopyRegister2(this, inst->VRegA_32x(), inst->VRegB_32x());
jeffhaobdb76512011-09-07 11:43:16 -07001656 break;
1657 case Instruction::MOVE_OBJECT:
Ian Rogers7b078e82014-09-10 14:44:24 -07001658 work_line_->CopyRegister1(this, inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategoryRef);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001659 break;
jeffhaobdb76512011-09-07 11:43:16 -07001660 case Instruction::MOVE_OBJECT_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001661 work_line_->CopyRegister1(this, inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategoryRef);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001662 break;
jeffhaobdb76512011-09-07 11:43:16 -07001663 case Instruction::MOVE_OBJECT_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001664 work_line_->CopyRegister1(this, inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategoryRef);
jeffhaobdb76512011-09-07 11:43:16 -07001665 break;
1666
1667 /*
1668 * The move-result instructions copy data out of a "pseudo-register"
jeffhaod1f0fde2011-09-08 17:25:33 -07001669 * with the results from the last method invocation. In practice we
jeffhaobdb76512011-09-07 11:43:16 -07001670 * might want to hold the result in an actual CPU register, so the
1671 * Dalvik spec requires that these only appear immediately after an
1672 * invoke or filled-new-array.
1673 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001674 * These calls invalidate the "result" register. (This is now
jeffhaobdb76512011-09-07 11:43:16 -07001675 * redundant with the reset done below, but it can make the debug info
1676 * easier to read in some cases.)
1677 */
1678 case Instruction::MOVE_RESULT:
Ian Rogers7b078e82014-09-10 14:44:24 -07001679 work_line_->CopyResultRegister1(this, inst->VRegA_11x(), false);
jeffhaobdb76512011-09-07 11:43:16 -07001680 break;
1681 case Instruction::MOVE_RESULT_WIDE:
Ian Rogers7b078e82014-09-10 14:44:24 -07001682 work_line_->CopyResultRegister2(this, inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07001683 break;
1684 case Instruction::MOVE_RESULT_OBJECT:
Ian Rogers7b078e82014-09-10 14:44:24 -07001685 work_line_->CopyResultRegister1(this, inst->VRegA_11x(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001686 break;
1687
Ian Rogersd81871c2011-10-03 13:57:23 -07001688 case Instruction::MOVE_EXCEPTION: {
Sebastien Hertz270a0e12015-01-16 19:49:09 +01001689 // We do not allow MOVE_EXCEPTION as the first instruction in a method. This is a simple case
1690 // where one entrypoint to the catch block is not actually an exception path.
1691 if (work_insn_idx_ == 0) {
1692 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "move-exception at pc 0x0";
1693 break;
1694 }
jeffhaobdb76512011-09-07 11:43:16 -07001695 /*
jeffhao60f83e32012-02-13 17:16:30 -08001696 * This statement can only appear as the first instruction in an exception handler. We verify
1697 * that as part of extracting the exception type from the catch block list.
jeffhaobdb76512011-09-07 11:43:16 -07001698 */
Ian Rogersd8f69b02014-09-10 21:43:52 +00001699 const RegType& res_type = GetCaughtExceptionType();
Ian Rogers7b078e82014-09-10 14:44:24 -07001700 work_line_->SetRegisterType(this, inst->VRegA_11x(), res_type);
jeffhaobdb76512011-09-07 11:43:16 -07001701 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001702 }
jeffhaobdb76512011-09-07 11:43:16 -07001703 case Instruction::RETURN_VOID:
Ian Rogers7b078e82014-09-10 14:44:24 -07001704 if (!IsConstructor() || work_line_->CheckConstructorReturn(this)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001705 if (!GetMethodReturnType().IsConflict()) {
jeffhaod5347e02012-03-22 17:25:05 -07001706 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001707 }
jeffhaobdb76512011-09-07 11:43:16 -07001708 }
1709 break;
1710 case Instruction::RETURN:
Ian Rogers7b078e82014-09-10 14:44:24 -07001711 if (!IsConstructor() || work_line_->CheckConstructorReturn(this)) {
jeffhaobdb76512011-09-07 11:43:16 -07001712 /* check the method signature */
Ian Rogersd8f69b02014-09-10 21:43:52 +00001713 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07001714 if (!return_type.IsCategory1Types()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001715 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected non-category 1 return type "
1716 << return_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001717 } else {
1718 // Compilers may generate synthetic functions that write byte values into boolean fields.
1719 // Also, it may use integer values for boolean, byte, short, and character return types.
Sebastien Hertz5243e912013-05-21 10:55:07 +02001720 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07001721 const RegType& src_type = work_line_->GetRegisterType(this, vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07001722 bool use_src = ((return_type.IsBoolean() && src_type.IsByte()) ||
1723 ((return_type.IsBoolean() || return_type.IsByte() ||
1724 return_type.IsShort() || return_type.IsChar()) &&
1725 src_type.IsInteger()));
1726 /* check the register contents */
Ian Rogersad0b3a32012-04-16 14:50:24 -07001727 bool success =
Ian Rogers7b078e82014-09-10 14:44:24 -07001728 work_line_->VerifyRegisterType(this, vregA, use_src ? src_type : return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001729 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001730 AppendToLastFailMessage(StringPrintf(" return-1nr on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07001731 }
jeffhaobdb76512011-09-07 11:43:16 -07001732 }
1733 }
1734 break;
1735 case Instruction::RETURN_WIDE:
Ian Rogers7b078e82014-09-10 14:44:24 -07001736 if (!IsConstructor() || work_line_->CheckConstructorReturn(this)) {
jeffhaobdb76512011-09-07 11:43:16 -07001737 /* check the method signature */
Ian Rogersd8f69b02014-09-10 21:43:52 +00001738 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07001739 if (!return_type.IsCategory2Types()) {
jeffhaod5347e02012-03-22 17:25:05 -07001740 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-wide not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001741 } else {
1742 /* check the register contents */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001743 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07001744 bool success = work_line_->VerifyRegisterType(this, vregA, return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001745 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001746 AppendToLastFailMessage(StringPrintf(" return-wide on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07001747 }
jeffhaobdb76512011-09-07 11:43:16 -07001748 }
1749 }
1750 break;
1751 case Instruction::RETURN_OBJECT:
Ian Rogers7b078e82014-09-10 14:44:24 -07001752 if (!IsConstructor() || work_line_->CheckConstructorReturn(this)) {
Ian Rogersd8f69b02014-09-10 21:43:52 +00001753 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07001754 if (!return_type.IsReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001755 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-object not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001756 } else {
1757 /* return_type is the *expected* return type, not register value */
1758 DCHECK(!return_type.IsZero());
1759 DCHECK(!return_type.IsUninitializedReference());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001760 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07001761 const RegType& reg_type = work_line_->GetRegisterType(this, vregA);
Ian Rogers9074b992011-10-26 17:41:55 -07001762 // Disallow returning uninitialized values and verify that the reference in vAA is an
1763 // instance of the "return_type"
1764 if (reg_type.IsUninitializedTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001765 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "returning uninitialized object '"
1766 << reg_type << "'";
Ian Rogers9074b992011-10-26 17:41:55 -07001767 } else if (!return_type.IsAssignableFrom(reg_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07001768 if (reg_type.IsUnresolvedTypes() || return_type.IsUnresolvedTypes()) {
1769 Fail(VERIFY_ERROR_NO_CLASS) << " can't resolve returned type '" << return_type
1770 << "' or '" << reg_type << "'";
1771 } else {
Andreas Gampe16f149c2015-03-23 10:10:20 -07001772 bool soft_error = false;
1773 // Check whether arrays are involved. They will show a valid class status, even
1774 // if their components are erroneous.
1775 if (reg_type.IsArrayTypes() && return_type.IsArrayTypes()) {
1776 return_type.CanAssignArray(reg_type, reg_types_, class_loader_, &soft_error);
1777 if (soft_error) {
1778 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "array with erroneous component type: "
1779 << reg_type << " vs " << return_type;
1780 }
1781 }
1782
1783 if (!soft_error) {
1784 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning '" << reg_type
1785 << "', but expected from declaration '" << return_type << "'";
1786 }
Jeff Haoa3faaf42013-09-03 19:07:00 -07001787 }
jeffhaobdb76512011-09-07 11:43:16 -07001788 }
1789 }
1790 }
1791 break;
1792
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001793 /* could be boolean, int, float, or a null reference */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001794 case Instruction::CONST_4: {
1795 int32_t val = static_cast<int32_t>(inst->VRegB_11n() << 28) >> 28;
Ian Rogers7b078e82014-09-10 14:44:24 -07001796 work_line_->SetRegisterType(this, inst->VRegA_11n(),
Ian Rogers46960fe2014-05-23 10:43:43 -07001797 DetermineCat1Constant(val, need_precise_constants_));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001798 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02001799 }
1800 case Instruction::CONST_16: {
1801 int16_t val = static_cast<int16_t>(inst->VRegB_21s());
Ian Rogers7b078e82014-09-10 14:44:24 -07001802 work_line_->SetRegisterType(this, inst->VRegA_21s(),
Ian Rogers46960fe2014-05-23 10:43:43 -07001803 DetermineCat1Constant(val, need_precise_constants_));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001804 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02001805 }
Sebastien Hertz849600b2013-12-20 10:28:08 +01001806 case Instruction::CONST: {
1807 int32_t val = inst->VRegB_31i();
Ian Rogers7b078e82014-09-10 14:44:24 -07001808 work_line_->SetRegisterType(this, inst->VRegA_31i(),
Ian Rogers46960fe2014-05-23 10:43:43 -07001809 DetermineCat1Constant(val, need_precise_constants_));
jeffhaobdb76512011-09-07 11:43:16 -07001810 break;
Sebastien Hertz849600b2013-12-20 10:28:08 +01001811 }
1812 case Instruction::CONST_HIGH16: {
1813 int32_t val = static_cast<int32_t>(inst->VRegB_21h() << 16);
Ian Rogers7b078e82014-09-10 14:44:24 -07001814 work_line_->SetRegisterType(this, inst->VRegA_21h(),
Ian Rogers46960fe2014-05-23 10:43:43 -07001815 DetermineCat1Constant(val, need_precise_constants_));
jeffhaobdb76512011-09-07 11:43:16 -07001816 break;
Sebastien Hertz849600b2013-12-20 10:28:08 +01001817 }
jeffhaobdb76512011-09-07 11:43:16 -07001818 /* could be long or double; resolved upon use */
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001819 case Instruction::CONST_WIDE_16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001820 int64_t val = static_cast<int16_t>(inst->VRegB_21s());
Ian Rogersd8f69b02014-09-10 21:43:52 +00001821 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1822 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07001823 work_line_->SetRegisterTypeWide(this, inst->VRegA_21s(), lo, hi);
jeffhaobdb76512011-09-07 11:43:16 -07001824 break;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001825 }
1826 case Instruction::CONST_WIDE_32: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001827 int64_t val = static_cast<int32_t>(inst->VRegB_31i());
Ian Rogersd8f69b02014-09-10 21:43:52 +00001828 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1829 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07001830 work_line_->SetRegisterTypeWide(this, inst->VRegA_31i(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001831 break;
1832 }
1833 case Instruction::CONST_WIDE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001834 int64_t val = inst->VRegB_51l();
Ian Rogersd8f69b02014-09-10 21:43:52 +00001835 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1836 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07001837 work_line_->SetRegisterTypeWide(this, inst->VRegA_51l(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001838 break;
1839 }
1840 case Instruction::CONST_WIDE_HIGH16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001841 int64_t val = static_cast<uint64_t>(inst->VRegB_21h()) << 48;
Ian Rogersd8f69b02014-09-10 21:43:52 +00001842 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1843 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07001844 work_line_->SetRegisterTypeWide(this, inst->VRegA_21h(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001845 break;
1846 }
jeffhaobdb76512011-09-07 11:43:16 -07001847 case Instruction::CONST_STRING:
Ian Rogers7b078e82014-09-10 14:44:24 -07001848 work_line_->SetRegisterType(this, inst->VRegA_21c(), reg_types_.JavaLangString());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001849 break;
jeffhaobdb76512011-09-07 11:43:16 -07001850 case Instruction::CONST_STRING_JUMBO:
Ian Rogers7b078e82014-09-10 14:44:24 -07001851 work_line_->SetRegisterType(this, inst->VRegA_31c(), reg_types_.JavaLangString());
jeffhaobdb76512011-09-07 11:43:16 -07001852 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001853 case Instruction::CONST_CLASS: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001854 // Get type from instruction if unresolved then we need an access check
1855 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Ian Rogersd8f69b02014-09-10 21:43:52 +00001856 const RegType& res_type = ResolveClassAndCheckAccess(inst->VRegB_21c());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001857 // Register holds class, ie its type is class, on error it will hold Conflict.
Ian Rogers7b078e82014-09-10 14:44:24 -07001858 work_line_->SetRegisterType(this, inst->VRegA_21c(),
Ian Rogersb4903572012-10-11 11:52:56 -07001859 res_type.IsConflict() ? res_type
Ian Rogers7b078e82014-09-10 14:44:24 -07001860 : reg_types_.JavaLangClass());
jeffhaobdb76512011-09-07 11:43:16 -07001861 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001862 }
jeffhaobdb76512011-09-07 11:43:16 -07001863 case Instruction::MONITOR_ENTER:
Ian Rogers7b078e82014-09-10 14:44:24 -07001864 work_line_->PushMonitor(this, inst->VRegA_11x(), work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07001865 break;
1866 case Instruction::MONITOR_EXIT:
1867 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001868 * monitor-exit instructions are odd. They can throw exceptions,
jeffhaobdb76512011-09-07 11:43:16 -07001869 * but when they do they act as if they succeeded and the PC is
jeffhaod1f0fde2011-09-08 17:25:33 -07001870 * pointing to the following instruction. (This behavior goes back
jeffhaobdb76512011-09-07 11:43:16 -07001871 * to the need to handle asynchronous exceptions, a now-deprecated
1872 * feature that Dalvik doesn't support.)
1873 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001874 * In practice we don't need to worry about this. The only
jeffhaobdb76512011-09-07 11:43:16 -07001875 * exceptions that can be thrown from monitor-exit are for a
jeffhaod1f0fde2011-09-08 17:25:33 -07001876 * null reference and -exit without a matching -enter. If the
jeffhaobdb76512011-09-07 11:43:16 -07001877 * structured locking checks are working, the former would have
1878 * failed on the -enter instruction, and the latter is impossible.
1879 *
1880 * This is fortunate, because issue 3221411 prevents us from
1881 * chasing the "can throw" path when monitor verification is
jeffhaod1f0fde2011-09-08 17:25:33 -07001882 * enabled. If we can fully verify the locking we can ignore
jeffhaobdb76512011-09-07 11:43:16 -07001883 * some catch blocks (which will show up as "dead" code when
1884 * we skip them here); if we can't, then the code path could be
1885 * "live" so we still need to check it.
1886 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001887 opcode_flags &= ~Instruction::kThrow;
Ian Rogers7b078e82014-09-10 14:44:24 -07001888 work_line_->PopMonitor(this, inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07001889 break;
1890
Ian Rogers28ad40d2011-10-27 15:19:26 -07001891 case Instruction::CHECK_CAST:
Ian Rogersd81871c2011-10-03 13:57:23 -07001892 case Instruction::INSTANCE_OF: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001893 /*
1894 * If this instruction succeeds, we will "downcast" register vA to the type in vB. (This
1895 * could be a "upcast" -- not expected, so we don't try to address it.)
1896 *
1897 * If it fails, an exception is thrown, which we deal with later by ignoring the update to
Elliott Hughesadb8c672012-03-06 16:49:32 -08001898 * dec_insn.vA when branching to a handler.
Ian Rogers28ad40d2011-10-27 15:19:26 -07001899 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001900 const bool is_checkcast = (inst->Opcode() == Instruction::CHECK_CAST);
1901 const uint32_t type_idx = (is_checkcast) ? inst->VRegB_21c() : inst->VRegC_22c();
Ian Rogersd8f69b02014-09-10 21:43:52 +00001902 const RegType& res_type = ResolveClassAndCheckAccess(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001903 if (res_type.IsConflict()) {
Andreas Gampe00633eb2014-07-17 16:13:35 -07001904 // If this is a primitive type, fail HARD.
Mathieu Chartierbf99f772014-08-23 16:37:27 -07001905 mirror::Class* klass = dex_cache_->GetResolvedType(type_idx);
Andreas Gampe00633eb2014-07-17 16:13:35 -07001906 if (klass != nullptr && klass->IsPrimitive()) {
1907 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "using primitive type "
1908 << dex_file_->StringByTypeIdx(type_idx) << " in instanceof in "
1909 << GetDeclaringClass();
1910 break;
1911 }
1912
Ian Rogersad0b3a32012-04-16 14:50:24 -07001913 DCHECK_NE(failures_.size(), 0U);
1914 if (!is_checkcast) {
Ian Rogers7b078e82014-09-10 14:44:24 -07001915 work_line_->SetRegisterType(this, inst->VRegA_22c(), reg_types_.Boolean());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001916 }
1917 break; // bad class
Ian Rogers9f1ab122011-12-12 08:52:43 -08001918 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001919 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Sebastien Hertz5243e912013-05-21 10:55:07 +02001920 uint32_t orig_type_reg = (is_checkcast) ? inst->VRegA_21c() : inst->VRegB_22c();
Ian Rogers7b078e82014-09-10 14:44:24 -07001921 const RegType& orig_type = work_line_->GetRegisterType(this, orig_type_reg);
Ian Rogers28ad40d2011-10-27 15:19:26 -07001922 if (!res_type.IsNonZeroReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001923 if (is_checkcast) {
1924 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on unexpected class " << res_type;
1925 } else {
1926 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on unexpected class " << res_type;
1927 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001928 } else if (!orig_type.IsReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001929 if (is_checkcast) {
1930 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on non-reference in v" << orig_type_reg;
1931 } else {
1932 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on non-reference in v" << orig_type_reg;
1933 }
jeffhao2a8a90e2011-09-26 14:25:31 -07001934 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001935 if (is_checkcast) {
Ian Rogers7b078e82014-09-10 14:44:24 -07001936 work_line_->SetRegisterType(this, inst->VRegA_21c(), res_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001937 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07001938 work_line_->SetRegisterType(this, inst->VRegA_22c(), reg_types_.Boolean());
jeffhaobdb76512011-09-07 11:43:16 -07001939 }
jeffhaobdb76512011-09-07 11:43:16 -07001940 }
jeffhao2a8a90e2011-09-26 14:25:31 -07001941 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001942 }
1943 case Instruction::ARRAY_LENGTH: {
Ian Rogers7b078e82014-09-10 14:44:24 -07001944 const RegType& res_type = work_line_->GetRegisterType(this, inst->VRegB_12x());
Ian Rogers28ad40d2011-10-27 15:19:26 -07001945 if (res_type.IsReferenceTypes()) {
Ian Rogers89310de2012-02-01 13:47:30 -08001946 if (!res_type.IsArrayTypes() && !res_type.IsZero()) { // ie not an array or null
jeffhaod5347e02012-03-22 17:25:05 -07001947 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001948 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07001949 work_line_->SetRegisterType(this, inst->VRegA_12x(), reg_types_.Integer());
Ian Rogersd81871c2011-10-03 13:57:23 -07001950 }
Andreas Gampe65c9db82014-07-28 13:14:34 -07001951 } else {
1952 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001953 }
1954 break;
1955 }
1956 case Instruction::NEW_INSTANCE: {
Ian Rogersd8f69b02014-09-10 21:43:52 +00001957 const RegType& res_type = ResolveClassAndCheckAccess(inst->VRegB_21c());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001958 if (res_type.IsConflict()) {
1959 DCHECK_NE(failures_.size(), 0U);
1960 break; // bad class
jeffhao8cd6dda2012-02-22 10:15:34 -08001961 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001962 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
1963 // can't create an instance of an interface or abstract class */
1964 if (!res_type.IsInstantiableTypes()) {
1965 Fail(VERIFY_ERROR_INSTANTIATION)
1966 << "new-instance on primitive, interface or abstract class" << res_type;
Ian Rogers08f753d2012-08-24 14:35:25 -07001967 // Soft failure so carry on to set register type.
Ian Rogersd81871c2011-10-03 13:57:23 -07001968 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00001969 const RegType& uninit_type = reg_types_.Uninitialized(res_type, work_insn_idx_);
Ian Rogers08f753d2012-08-24 14:35:25 -07001970 // Any registers holding previous allocations from this address that have not yet been
1971 // initialized must be marked invalid.
Ian Rogers7b078e82014-09-10 14:44:24 -07001972 work_line_->MarkUninitRefsAsInvalid(this, uninit_type);
Ian Rogers08f753d2012-08-24 14:35:25 -07001973 // add the new uninitialized reference to the register state
Ian Rogers7b078e82014-09-10 14:44:24 -07001974 work_line_->SetRegisterType(this, inst->VRegA_21c(), uninit_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001975 break;
1976 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08001977 case Instruction::NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001978 VerifyNewArray(inst, false, false);
jeffhaobdb76512011-09-07 11:43:16 -07001979 break;
1980 case Instruction::FILLED_NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001981 VerifyNewArray(inst, true, false);
Ian Rogers0c4a5062012-02-03 15:18:59 -08001982 just_set_result = true; // Filled new array sets result register
jeffhaobdb76512011-09-07 11:43:16 -07001983 break;
Ian Rogers0c4a5062012-02-03 15:18:59 -08001984 case Instruction::FILLED_NEW_ARRAY_RANGE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001985 VerifyNewArray(inst, true, true);
Ian Rogers0c4a5062012-02-03 15:18:59 -08001986 just_set_result = true; // Filled new array range sets result register
1987 break;
jeffhaobdb76512011-09-07 11:43:16 -07001988 case Instruction::CMPL_FLOAT:
1989 case Instruction::CMPG_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07001990 if (!work_line_->VerifyRegisterType(this, inst->VRegB_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08001991 break;
1992 }
Ian Rogers7b078e82014-09-10 14:44:24 -07001993 if (!work_line_->VerifyRegisterType(this, inst->VRegC_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08001994 break;
1995 }
Ian Rogers7b078e82014-09-10 14:44:24 -07001996 work_line_->SetRegisterType(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001997 break;
1998 case Instruction::CMPL_DOUBLE:
1999 case Instruction::CMPG_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002000 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegB_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002001 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002002 break;
2003 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002004 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegC_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002005 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002006 break;
2007 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002008 work_line_->SetRegisterType(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002009 break;
2010 case Instruction::CMP_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002011 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegB_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002012 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002013 break;
2014 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002015 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegC_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002016 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002017 break;
2018 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002019 work_line_->SetRegisterType(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002020 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002021 case Instruction::THROW: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002022 const RegType& res_type = work_line_->GetRegisterType(this, inst->VRegA_11x());
Ian Rogersb4903572012-10-11 11:52:56 -07002023 if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(res_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07002024 Fail(res_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS : VERIFY_ERROR_BAD_CLASS_SOFT)
2025 << "thrown class " << res_type << " not instanceof Throwable";
jeffhaobdb76512011-09-07 11:43:16 -07002026 }
2027 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002028 }
jeffhaobdb76512011-09-07 11:43:16 -07002029 case Instruction::GOTO:
2030 case Instruction::GOTO_16:
2031 case Instruction::GOTO_32:
2032 /* no effect on or use of registers */
2033 break;
2034
2035 case Instruction::PACKED_SWITCH:
2036 case Instruction::SPARSE_SWITCH:
2037 /* verify that vAA is an integer, or can be converted to one */
Ian Rogers7b078e82014-09-10 14:44:24 -07002038 work_line_->VerifyRegisterType(this, inst->VRegA_31t(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002039 break;
2040
Ian Rogersd81871c2011-10-03 13:57:23 -07002041 case Instruction::FILL_ARRAY_DATA: {
2042 /* Similar to the verification done for APUT */
Ian Rogers7b078e82014-09-10 14:44:24 -07002043 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegA_31t());
Ian Rogers89310de2012-02-01 13:47:30 -08002044 /* array_type can be null if the reg type is Zero */
2045 if (!array_type.IsZero()) {
jeffhao457cc512012-02-02 16:55:13 -08002046 if (!array_type.IsArrayTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002047 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with array type "
2048 << array_type;
Ian Rogers89310de2012-02-01 13:47:30 -08002049 } else {
Ian Rogersd8f69b02014-09-10 21:43:52 +00002050 const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
Ian Rogersad0b3a32012-04-16 14:50:24 -07002051 DCHECK(!component_type.IsConflict());
jeffhao457cc512012-02-02 16:55:13 -08002052 if (component_type.IsNonZeroReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002053 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with component type "
2054 << component_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002055 } else {
jeffhao457cc512012-02-02 16:55:13 -08002056 // Now verify if the element width in the table matches the element width declared in
2057 // the array
2058 const uint16_t* array_data = insns + (insns[1] | (((int32_t) insns[2]) << 16));
2059 if (array_data[0] != Instruction::kArrayDataSignature) {
jeffhaod5347e02012-03-22 17:25:05 -07002060 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid magic for array-data";
jeffhao457cc512012-02-02 16:55:13 -08002061 } else {
2062 size_t elem_width = Primitive::ComponentSize(component_type.GetPrimitiveType());
2063 // Since we don't compress the data in Dex, expect to see equal width of data stored
2064 // in the table and expected from the array class.
2065 if (array_data[1] != elem_width) {
jeffhaod5347e02012-03-22 17:25:05 -07002066 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-data size mismatch (" << array_data[1]
2067 << " vs " << elem_width << ")";
jeffhao457cc512012-02-02 16:55:13 -08002068 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002069 }
2070 }
jeffhaobdb76512011-09-07 11:43:16 -07002071 }
2072 }
2073 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002074 }
jeffhaobdb76512011-09-07 11:43:16 -07002075 case Instruction::IF_EQ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002076 case Instruction::IF_NE: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002077 const RegType& reg_type1 = work_line_->GetRegisterType(this, inst->VRegA_22t());
2078 const RegType& reg_type2 = work_line_->GetRegisterType(this, inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002079 bool mismatch = false;
2080 if (reg_type1.IsZero()) { // zero then integral or reference expected
2081 mismatch = !reg_type2.IsReferenceTypes() && !reg_type2.IsIntegralTypes();
2082 } else if (reg_type1.IsReferenceTypes()) { // both references?
2083 mismatch = !reg_type2.IsReferenceTypes();
2084 } else { // both integral?
2085 mismatch = !reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes();
2086 }
2087 if (mismatch) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002088 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to if-eq/if-ne (" << reg_type1 << ","
2089 << reg_type2 << ") must both be references or integral";
jeffhaobdb76512011-09-07 11:43:16 -07002090 }
2091 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002092 }
jeffhaobdb76512011-09-07 11:43:16 -07002093 case Instruction::IF_LT:
2094 case Instruction::IF_GE:
2095 case Instruction::IF_GT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002096 case Instruction::IF_LE: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002097 const RegType& reg_type1 = work_line_->GetRegisterType(this, inst->VRegA_22t());
2098 const RegType& reg_type2 = work_line_->GetRegisterType(this, inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002099 if (!reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002100 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to 'if' (" << reg_type1 << ","
2101 << reg_type2 << ") must be integral";
jeffhaobdb76512011-09-07 11:43:16 -07002102 }
2103 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002104 }
jeffhaobdb76512011-09-07 11:43:16 -07002105 case Instruction::IF_EQZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002106 case Instruction::IF_NEZ: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002107 const RegType& reg_type = work_line_->GetRegisterType(this, inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002108 if (!reg_type.IsReferenceTypes() && !reg_type.IsIntegralTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002109 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
2110 << " unexpected as arg to if-eqz/if-nez";
Ian Rogersd81871c2011-10-03 13:57:23 -07002111 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002112
2113 // Find previous instruction - its existence is a precondition to peephole optimization.
Ian Rogers9b360392013-06-06 14:45:07 -07002114 uint32_t instance_of_idx = 0;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002115 if (0 != work_insn_idx_) {
Ian Rogers9b360392013-06-06 14:45:07 -07002116 instance_of_idx = work_insn_idx_ - 1;
Brian Carlstromdf629502013-07-17 22:39:56 -07002117 while (0 != instance_of_idx && !insn_flags_[instance_of_idx].IsOpcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07002118 instance_of_idx--;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002119 }
Andreas Gampe7c038102014-10-27 20:08:46 -07002120 if (FailOrAbort(this, insn_flags_[instance_of_idx].IsOpcode(),
2121 "Unable to get previous instruction of if-eqz/if-nez for work index ",
2122 work_insn_idx_)) {
2123 break;
2124 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002125 } else {
2126 break;
2127 }
2128
Ian Rogers9b360392013-06-06 14:45:07 -07002129 const Instruction* instance_of_inst = Instruction::At(code_item_->insns_ + instance_of_idx);
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002130
2131 /* Check for peep-hole pattern of:
2132 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002133 * instance-of vX, vY, T;
2134 * ifXXX vX, label ;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002135 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002136 * label:
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002137 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002138 * and sharpen the type of vY to be type T.
2139 * Note, this pattern can't be if:
2140 * - if there are other branches to this branch,
2141 * - when vX == vY.
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002142 */
Ian Rogersfae370a2013-06-05 08:33:27 -07002143 if (!CurrentInsnFlags()->IsBranchTarget() &&
Ian Rogers9b360392013-06-06 14:45:07 -07002144 (Instruction::INSTANCE_OF == instance_of_inst->Opcode()) &&
2145 (inst->VRegA_21t() == instance_of_inst->VRegA_22c()) &&
2146 (instance_of_inst->VRegA_22c() != instance_of_inst->VRegB_22c())) {
Ian Rogersebbdd872014-07-07 23:53:08 -07002147 // Check the type of the instance-of is different than that of registers type, as if they
2148 // are the same there is no work to be done here. Check that the conversion is not to or
2149 // from an unresolved type as type information is imprecise. If the instance-of is to an
2150 // interface then ignore the type information as interfaces can only be treated as Objects
2151 // and we don't want to disallow field and other operations on the object. If the value
2152 // being instance-of checked against is known null (zero) then allow the optimization as
2153 // we didn't have type information. If the merge of the instance-of type with the original
2154 // type is assignable to the original then allow optimization. This check is performed to
2155 // ensure that subsequent merges don't lose type information - such as becoming an
2156 // interface from a class that would lose information relevant to field checks.
Ian Rogers7b078e82014-09-10 14:44:24 -07002157 const RegType& orig_type = work_line_->GetRegisterType(this, instance_of_inst->VRegB_22c());
Ian Rogersd8f69b02014-09-10 21:43:52 +00002158 const RegType& cast_type = ResolveClassAndCheckAccess(instance_of_inst->VRegC_22c());
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002159
Ian Rogersebbdd872014-07-07 23:53:08 -07002160 if (!orig_type.Equals(cast_type) &&
2161 !cast_type.IsUnresolvedTypes() && !orig_type.IsUnresolvedTypes() &&
Andreas Gampe00633eb2014-07-17 16:13:35 -07002162 cast_type.HasClass() && // Could be conflict type, make sure it has a class.
Ian Rogersebbdd872014-07-07 23:53:08 -07002163 !cast_type.GetClass()->IsInterface() &&
2164 (orig_type.IsZero() ||
2165 orig_type.IsStrictlyAssignableFrom(cast_type.Merge(orig_type, &reg_types_)))) {
Ian Rogersd0fbd852013-09-24 18:17:04 -07002166 RegisterLine* update_line = RegisterLine::Create(code_item_->registers_size_, this);
Ian Rogersfae370a2013-06-05 08:33:27 -07002167 if (inst->Opcode() == Instruction::IF_EQZ) {
Ian Rogers9b360392013-06-06 14:45:07 -07002168 fallthrough_line.reset(update_line);
Ian Rogersfae370a2013-06-05 08:33:27 -07002169 } else {
Ian Rogers9b360392013-06-06 14:45:07 -07002170 branch_line.reset(update_line);
2171 }
2172 update_line->CopyFromLine(work_line_.get());
Ian Rogers7b078e82014-09-10 14:44:24 -07002173 update_line->SetRegisterType(this, instance_of_inst->VRegB_22c(), cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002174 if (!insn_flags_[instance_of_idx].IsBranchTarget() && 0 != instance_of_idx) {
2175 // See if instance-of was preceded by a move-object operation, common due to the small
2176 // register encoding space of instance-of, and propagate type information to the source
2177 // of the move-object.
2178 uint32_t move_idx = instance_of_idx - 1;
Brian Carlstromdf629502013-07-17 22:39:56 -07002179 while (0 != move_idx && !insn_flags_[move_idx].IsOpcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07002180 move_idx--;
2181 }
Andreas Gampe7c038102014-10-27 20:08:46 -07002182 if (FailOrAbort(this, insn_flags_[move_idx].IsOpcode(),
2183 "Unable to get previous instruction of if-eqz/if-nez for work index ",
2184 work_insn_idx_)) {
2185 break;
2186 }
Ian Rogers9b360392013-06-06 14:45:07 -07002187 const Instruction* move_inst = Instruction::At(code_item_->insns_ + move_idx);
2188 switch (move_inst->Opcode()) {
2189 case Instruction::MOVE_OBJECT:
2190 if (move_inst->VRegA_12x() == instance_of_inst->VRegB_22c()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002191 update_line->SetRegisterType(this, move_inst->VRegB_12x(), cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002192 }
2193 break;
2194 case Instruction::MOVE_OBJECT_FROM16:
2195 if (move_inst->VRegA_22x() == instance_of_inst->VRegB_22c()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002196 update_line->SetRegisterType(this, move_inst->VRegB_22x(), cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002197 }
2198 break;
2199 case Instruction::MOVE_OBJECT_16:
2200 if (move_inst->VRegA_32x() == instance_of_inst->VRegB_22c()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002201 update_line->SetRegisterType(this, move_inst->VRegB_32x(), cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002202 }
2203 break;
2204 default:
2205 break;
2206 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002207 }
2208 }
2209 }
2210
jeffhaobdb76512011-09-07 11:43:16 -07002211 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002212 }
jeffhaobdb76512011-09-07 11:43:16 -07002213 case Instruction::IF_LTZ:
2214 case Instruction::IF_GEZ:
2215 case Instruction::IF_GTZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002216 case Instruction::IF_LEZ: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002217 const RegType& reg_type = work_line_->GetRegisterType(this, inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002218 if (!reg_type.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002219 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
2220 << " unexpected as arg to if-ltz/if-gez/if-gtz/if-lez";
Ian Rogersd81871c2011-10-03 13:57:23 -07002221 }
jeffhaobdb76512011-09-07 11:43:16 -07002222 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002223 }
jeffhaobdb76512011-09-07 11:43:16 -07002224 case Instruction::AGET_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002225 VerifyAGet(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002226 break;
jeffhaobdb76512011-09-07 11:43:16 -07002227 case Instruction::AGET_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002228 VerifyAGet(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002229 break;
jeffhaobdb76512011-09-07 11:43:16 -07002230 case Instruction::AGET_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002231 VerifyAGet(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002232 break;
jeffhaobdb76512011-09-07 11:43:16 -07002233 case Instruction::AGET_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002234 VerifyAGet(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002235 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002236 case Instruction::AGET:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002237 VerifyAGet(inst, reg_types_.Integer(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002238 break;
jeffhaobdb76512011-09-07 11:43:16 -07002239 case Instruction::AGET_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002240 VerifyAGet(inst, reg_types_.LongLo(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002241 break;
2242 case Instruction::AGET_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002243 VerifyAGet(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07002244 break;
2245
Ian Rogersd81871c2011-10-03 13:57:23 -07002246 case Instruction::APUT_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002247 VerifyAPut(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002248 break;
2249 case Instruction::APUT_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002250 VerifyAPut(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002251 break;
2252 case Instruction::APUT_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002253 VerifyAPut(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002254 break;
2255 case Instruction::APUT_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002256 VerifyAPut(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002257 break;
2258 case Instruction::APUT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002259 VerifyAPut(inst, reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002260 break;
2261 case Instruction::APUT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002262 VerifyAPut(inst, reg_types_.LongLo(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002263 break;
2264 case Instruction::APUT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002265 VerifyAPut(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07002266 break;
2267
jeffhaobdb76512011-09-07 11:43:16 -07002268 case Instruction::IGET_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002269 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002270 break;
jeffhaobdb76512011-09-07 11:43:16 -07002271 case Instruction::IGET_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002272 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002273 break;
jeffhaobdb76512011-09-07 11:43:16 -07002274 case Instruction::IGET_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002275 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002276 break;
jeffhaobdb76512011-09-07 11:43:16 -07002277 case Instruction::IGET_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002278 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002279 break;
2280 case Instruction::IGET:
Andreas Gampe896df402014-10-20 22:25:29 -07002281 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002282 break;
2283 case Instruction::IGET_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002284 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002285 break;
2286 case Instruction::IGET_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002287 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false,
2288 false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002289 break;
jeffhaobdb76512011-09-07 11:43:16 -07002290
Ian Rogersd81871c2011-10-03 13:57:23 -07002291 case Instruction::IPUT_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002292 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002293 break;
2294 case Instruction::IPUT_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002295 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002296 break;
2297 case Instruction::IPUT_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002298 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002299 break;
2300 case Instruction::IPUT_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002301 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002302 break;
2303 case Instruction::IPUT:
Andreas Gampe896df402014-10-20 22:25:29 -07002304 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002305 break;
2306 case Instruction::IPUT_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002307 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002308 break;
jeffhaobdb76512011-09-07 11:43:16 -07002309 case Instruction::IPUT_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002310 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false,
2311 false);
jeffhaobdb76512011-09-07 11:43:16 -07002312 break;
2313
jeffhaobdb76512011-09-07 11:43:16 -07002314 case Instruction::SGET_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002315 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002316 break;
jeffhaobdb76512011-09-07 11:43:16 -07002317 case Instruction::SGET_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002318 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002319 break;
jeffhaobdb76512011-09-07 11:43:16 -07002320 case Instruction::SGET_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002321 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002322 break;
jeffhaobdb76512011-09-07 11:43:16 -07002323 case Instruction::SGET_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002324 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002325 break;
2326 case Instruction::SGET:
Andreas Gampe896df402014-10-20 22:25:29 -07002327 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002328 break;
2329 case Instruction::SGET_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002330 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002331 break;
2332 case Instruction::SGET_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002333 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false,
2334 true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002335 break;
2336
2337 case Instruction::SPUT_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002338 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002339 break;
2340 case Instruction::SPUT_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002341 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002342 break;
2343 case Instruction::SPUT_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002344 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002345 break;
2346 case Instruction::SPUT_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002347 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002348 break;
2349 case Instruction::SPUT:
Andreas Gampe896df402014-10-20 22:25:29 -07002350 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002351 break;
2352 case Instruction::SPUT_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002353 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002354 break;
2355 case Instruction::SPUT_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002356 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false,
2357 true);
jeffhaobdb76512011-09-07 11:43:16 -07002358 break;
2359
2360 case Instruction::INVOKE_VIRTUAL:
2361 case Instruction::INVOKE_VIRTUAL_RANGE:
2362 case Instruction::INVOKE_SUPER:
Ian Rogersd81871c2011-10-03 13:57:23 -07002363 case Instruction::INVOKE_SUPER_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002364 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE ||
2365 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002366 bool is_super = (inst->Opcode() == Instruction::INVOKE_SUPER ||
2367 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002368 ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_VIRTUAL, is_range, is_super);
Ian Rogersd8f69b02014-09-10 21:43:52 +00002369 const RegType* return_type = nullptr;
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002370 if (called_method != nullptr) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002371 StackHandleScope<1> hs(self_);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002372 mirror::Class* return_type_class = called_method->GetReturnType(can_load_classes_);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002373 if (return_type_class != nullptr) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07002374 return_type = &reg_types_.FromClass(called_method->GetReturnTypeDescriptor(),
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002375 return_type_class,
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002376 return_type_class->CannotBeAssignedFromOtherTypes());
2377 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07002378 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
2379 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002380 }
2381 }
2382 if (return_type == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002383 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002384 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2385 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002386 const char* descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Mathieu Chartierbf99f772014-08-23 16:37:27 -07002387 return_type = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
jeffhaobdb76512011-09-07 11:43:16 -07002388 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002389 if (!return_type->IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002390 work_line_->SetResultRegisterType(this, *return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002391 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002392 work_line_->SetResultRegisterTypeWide(*return_type, return_type->HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002393 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002394 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002395 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002396 }
jeffhaobdb76512011-09-07 11:43:16 -07002397 case Instruction::INVOKE_DIRECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002398 case Instruction::INVOKE_DIRECT_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002399 bool is_range = (inst->Opcode() == Instruction::INVOKE_DIRECT_RANGE);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002400 ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_DIRECT, is_range, false);
Ian Rogers46685432012-06-03 22:26:43 -07002401 const char* return_type_descriptor;
2402 bool is_constructor;
Ian Rogersd8f69b02014-09-10 21:43:52 +00002403 const RegType* return_type = nullptr;
Ian Rogers7b078e82014-09-10 14:44:24 -07002404 if (called_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002405 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogers46685432012-06-03 22:26:43 -07002406 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
Ian Rogersdfb325e2013-10-30 01:00:44 -07002407 is_constructor = strcmp("<init>", dex_file_->StringDataByIdx(method_id.name_idx_)) == 0;
Ian Rogers46685432012-06-03 22:26:43 -07002408 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
2409 return_type_descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2410 } else {
2411 is_constructor = called_method->IsConstructor();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002412 return_type_descriptor = called_method->GetReturnTypeDescriptor();
Ian Rogers7b078e82014-09-10 14:44:24 -07002413 StackHandleScope<1> hs(self_);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002414 mirror::Class* return_type_class = called_method->GetReturnType(can_load_classes_);
Ian Rogers1ff3c982014-08-12 02:30:58 -07002415 if (return_type_class != nullptr) {
2416 return_type = &reg_types_.FromClass(return_type_descriptor,
2417 return_type_class,
2418 return_type_class->CannotBeAssignedFromOtherTypes());
2419 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07002420 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
2421 self_->ClearException();
Ian Rogers1ff3c982014-08-12 02:30:58 -07002422 }
Ian Rogers46685432012-06-03 22:26:43 -07002423 }
2424 if (is_constructor) {
jeffhaobdb76512011-09-07 11:43:16 -07002425 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002426 * Some additional checks when calling a constructor. We know from the invocation arg check
2427 * that the "this" argument is an instance of called_method->klass. Now we further restrict
2428 * that to require that called_method->klass is the same as this->klass or this->super,
2429 * allowing the latter only if the "this" argument is the same as the "this" argument to
2430 * this method (which implies that we're in a constructor ourselves).
jeffhaobdb76512011-09-07 11:43:16 -07002431 */
Ian Rogers7b078e82014-09-10 14:44:24 -07002432 const RegType& this_type = work_line_->GetInvocationThis(this, inst, is_range);
jeffhaob57e9522012-04-26 18:08:21 -07002433 if (this_type.IsConflict()) // failure.
2434 break;
jeffhaobdb76512011-09-07 11:43:16 -07002435
jeffhaob57e9522012-04-26 18:08:21 -07002436 /* no null refs allowed (?) */
2437 if (this_type.IsZero()) {
2438 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unable to initialize null ref";
2439 break;
jeffhao2a8a90e2011-09-26 14:25:31 -07002440 }
jeffhaob57e9522012-04-26 18:08:21 -07002441
2442 /* must be in same class or in superclass */
Ian Rogersd8f69b02014-09-10 21:43:52 +00002443 // const RegType& this_super_klass = this_type.GetSuperClass(&reg_types_);
Ian Rogers46685432012-06-03 22:26:43 -07002444 // TODO: re-enable constructor type verification
2445 // if (this_super_klass.IsConflict()) {
jeffhaob57e9522012-04-26 18:08:21 -07002446 // Unknown super class, fail so we re-check at runtime.
Ian Rogers46685432012-06-03 22:26:43 -07002447 // Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "super class unknown for '" << this_type << "'";
2448 // break;
2449 // }
jeffhaob57e9522012-04-26 18:08:21 -07002450
2451 /* arg must be an uninitialized reference */
2452 if (!this_type.IsUninitializedTypes()) {
2453 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Expected initialization on uninitialized reference "
2454 << this_type;
2455 break;
2456 }
2457
2458 /*
2459 * Replace the uninitialized reference with an initialized one. We need to do this for all
2460 * registers that have the same object instance in them, not just the "this" register.
2461 */
Jeff Hao848f70a2014-01-15 13:49:50 -08002462 const uint32_t this_reg = (is_range) ? inst->VRegC_3rc() : inst->VRegC_35c();
2463 work_line_->MarkRefsAsInitialized(this, this_type, this_reg, work_insn_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002464 }
Ian Rogers1ff3c982014-08-12 02:30:58 -07002465 if (return_type == nullptr) {
Mathieu Chartierbf99f772014-08-23 16:37:27 -07002466 return_type = &reg_types_.FromDescriptor(GetClassLoader(), return_type_descriptor,
2467 false);
Ian Rogers1ff3c982014-08-12 02:30:58 -07002468 }
2469 if (!return_type->IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002470 work_line_->SetResultRegisterType(this, *return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002471 } else {
Ian Rogers1ff3c982014-08-12 02:30:58 -07002472 work_line_->SetResultRegisterTypeWide(*return_type, return_type->HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002473 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002474 just_set_result = true;
2475 break;
2476 }
2477 case Instruction::INVOKE_STATIC:
2478 case Instruction::INVOKE_STATIC_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002479 bool is_range = (inst->Opcode() == Instruction::INVOKE_STATIC_RANGE);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002480 ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_STATIC, is_range, false);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002481 const char* descriptor;
Ian Rogers7b078e82014-09-10 14:44:24 -07002482 if (called_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002483 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002484 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2485 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Mathieu Chartier590fee92013-09-13 13:46:47 -07002486 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002487 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002488 descriptor = called_method->GetReturnTypeDescriptor();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002489 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00002490 const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002491 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002492 work_line_->SetResultRegisterType(this, return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002493 } else {
2494 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2495 }
jeffhaobdb76512011-09-07 11:43:16 -07002496 just_set_result = true;
2497 }
2498 break;
jeffhaobdb76512011-09-07 11:43:16 -07002499 case Instruction::INVOKE_INTERFACE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002500 case Instruction::INVOKE_INTERFACE_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002501 bool is_range = (inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002502 ArtMethod* abs_method = VerifyInvocationArgs(inst, METHOD_INTERFACE, is_range, false);
Ian Rogers7b078e82014-09-10 14:44:24 -07002503 if (abs_method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002504 mirror::Class* called_interface = abs_method->GetDeclaringClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002505 if (!called_interface->IsInterface() && !called_interface->IsObjectClass()) {
2506 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected interface class in invoke-interface '"
2507 << PrettyMethod(abs_method) << "'";
2508 break;
Ian Rogers28ad40d2011-10-27 15:19:26 -07002509 }
Ian Rogers0d604842012-04-16 14:50:24 -07002510 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002511 /* Get the type of the "this" arg, which should either be a sub-interface of called
2512 * interface or Object (see comments in RegType::JoinClass).
2513 */
Ian Rogers7b078e82014-09-10 14:44:24 -07002514 const RegType& this_type = work_line_->GetInvocationThis(this, inst, is_range);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002515 if (this_type.IsZero()) {
2516 /* null pointer always passes (and always fails at runtime) */
2517 } else {
2518 if (this_type.IsUninitializedTypes()) {
2519 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface call on uninitialized object "
2520 << this_type;
2521 break;
2522 }
2523 // In the past we have tried to assert that "called_interface" is assignable
2524 // from "this_type.GetClass()", however, as we do an imprecise Join
2525 // (RegType::JoinClass) we don't have full information on what interfaces are
2526 // implemented by "this_type". For example, two classes may implement the same
2527 // interfaces and have a common parent that doesn't implement the interface. The
2528 // join will set "this_type" to the parent class and a test that this implements
2529 // the interface will incorrectly fail.
2530 }
2531 /*
2532 * We don't have an object instance, so we can't find the concrete method. However, all of
2533 * the type information is in the abstract method, so we're good.
2534 */
2535 const char* descriptor;
Ian Rogers7b078e82014-09-10 14:44:24 -07002536 if (abs_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002537 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002538 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2539 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
2540 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2541 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002542 descriptor = abs_method->GetReturnTypeDescriptor();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002543 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00002544 const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002545 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002546 work_line_->SetResultRegisterType(this, return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002547 } else {
2548 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2549 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002550 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002551 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002552 }
jeffhaobdb76512011-09-07 11:43:16 -07002553 case Instruction::NEG_INT:
2554 case Instruction::NOT_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002555 work_line_->CheckUnaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002556 break;
2557 case Instruction::NEG_LONG:
2558 case Instruction::NOT_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002559 work_line_->CheckUnaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002560 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002561 break;
2562 case Instruction::NEG_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002563 work_line_->CheckUnaryOp(this, inst, reg_types_.Float(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002564 break;
2565 case Instruction::NEG_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002566 work_line_->CheckUnaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002567 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002568 break;
2569 case Instruction::INT_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002570 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002571 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002572 break;
2573 case Instruction::INT_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002574 work_line_->CheckUnaryOp(this, inst, reg_types_.Float(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002575 break;
2576 case Instruction::INT_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002577 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002578 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002579 break;
2580 case Instruction::LONG_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002581 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002582 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002583 break;
2584 case Instruction::LONG_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002585 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002586 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002587 break;
2588 case Instruction::LONG_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002589 work_line_->CheckUnaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002590 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002591 break;
2592 case Instruction::FLOAT_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002593 work_line_->CheckUnaryOp(this, inst, reg_types_.Integer(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002594 break;
2595 case Instruction::FLOAT_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002596 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002597 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002598 break;
2599 case Instruction::FLOAT_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002600 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002601 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002602 break;
2603 case Instruction::DOUBLE_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002604 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002605 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002606 break;
2607 case Instruction::DOUBLE_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002608 work_line_->CheckUnaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002609 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002610 break;
2611 case Instruction::DOUBLE_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002612 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002613 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002614 break;
2615 case Instruction::INT_TO_BYTE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002616 work_line_->CheckUnaryOp(this, inst, reg_types_.Byte(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002617 break;
2618 case Instruction::INT_TO_CHAR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002619 work_line_->CheckUnaryOp(this, inst, reg_types_.Char(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002620 break;
2621 case Instruction::INT_TO_SHORT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002622 work_line_->CheckUnaryOp(this, inst, reg_types_.Short(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002623 break;
2624
2625 case Instruction::ADD_INT:
2626 case Instruction::SUB_INT:
2627 case Instruction::MUL_INT:
2628 case Instruction::REM_INT:
2629 case Instruction::DIV_INT:
2630 case Instruction::SHL_INT:
2631 case Instruction::SHR_INT:
2632 case Instruction::USHR_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002633 work_line_->CheckBinaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002634 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002635 break;
2636 case Instruction::AND_INT:
2637 case Instruction::OR_INT:
2638 case Instruction::XOR_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002639 work_line_->CheckBinaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002640 reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002641 break;
2642 case Instruction::ADD_LONG:
2643 case Instruction::SUB_LONG:
2644 case Instruction::MUL_LONG:
2645 case Instruction::DIV_LONG:
2646 case Instruction::REM_LONG:
2647 case Instruction::AND_LONG:
2648 case Instruction::OR_LONG:
2649 case Instruction::XOR_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002650 work_line_->CheckBinaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002651 reg_types_.LongLo(), reg_types_.LongHi(),
2652 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002653 break;
2654 case Instruction::SHL_LONG:
2655 case Instruction::SHR_LONG:
2656 case Instruction::USHR_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002657 /* shift distance is Int, making these different from other binary operations */
Ian Rogers7b078e82014-09-10 14:44:24 -07002658 work_line_->CheckBinaryOpWideShift(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002659 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002660 break;
2661 case Instruction::ADD_FLOAT:
2662 case Instruction::SUB_FLOAT:
2663 case Instruction::MUL_FLOAT:
2664 case Instruction::DIV_FLOAT:
2665 case Instruction::REM_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002666 work_line_->CheckBinaryOp(this, inst, reg_types_.Float(), reg_types_.Float(),
2667 reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002668 break;
2669 case Instruction::ADD_DOUBLE:
2670 case Instruction::SUB_DOUBLE:
2671 case Instruction::MUL_DOUBLE:
2672 case Instruction::DIV_DOUBLE:
2673 case Instruction::REM_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002674 work_line_->CheckBinaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002675 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2676 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002677 break;
2678 case Instruction::ADD_INT_2ADDR:
2679 case Instruction::SUB_INT_2ADDR:
2680 case Instruction::MUL_INT_2ADDR:
2681 case Instruction::REM_INT_2ADDR:
2682 case Instruction::SHL_INT_2ADDR:
2683 case Instruction::SHR_INT_2ADDR:
2684 case Instruction::USHR_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002685 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
2686 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002687 break;
2688 case Instruction::AND_INT_2ADDR:
2689 case Instruction::OR_INT_2ADDR:
2690 case Instruction::XOR_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002691 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
2692 reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002693 break;
2694 case Instruction::DIV_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002695 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
2696 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002697 break;
2698 case Instruction::ADD_LONG_2ADDR:
2699 case Instruction::SUB_LONG_2ADDR:
2700 case Instruction::MUL_LONG_2ADDR:
2701 case Instruction::DIV_LONG_2ADDR:
2702 case Instruction::REM_LONG_2ADDR:
2703 case Instruction::AND_LONG_2ADDR:
2704 case Instruction::OR_LONG_2ADDR:
2705 case Instruction::XOR_LONG_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002706 work_line_->CheckBinaryOp2addrWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002707 reg_types_.LongLo(), reg_types_.LongHi(),
2708 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002709 break;
2710 case Instruction::SHL_LONG_2ADDR:
2711 case Instruction::SHR_LONG_2ADDR:
2712 case Instruction::USHR_LONG_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002713 work_line_->CheckBinaryOp2addrWideShift(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002714 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002715 break;
2716 case Instruction::ADD_FLOAT_2ADDR:
2717 case Instruction::SUB_FLOAT_2ADDR:
2718 case Instruction::MUL_FLOAT_2ADDR:
2719 case Instruction::DIV_FLOAT_2ADDR:
2720 case Instruction::REM_FLOAT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002721 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Float(), reg_types_.Float(),
2722 reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002723 break;
2724 case Instruction::ADD_DOUBLE_2ADDR:
2725 case Instruction::SUB_DOUBLE_2ADDR:
2726 case Instruction::MUL_DOUBLE_2ADDR:
2727 case Instruction::DIV_DOUBLE_2ADDR:
2728 case Instruction::REM_DOUBLE_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002729 work_line_->CheckBinaryOp2addrWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002730 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2731 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002732 break;
2733 case Instruction::ADD_INT_LIT16:
Ian Rogersf72a11d2014-10-30 15:41:08 -07002734 case Instruction::RSUB_INT_LIT16:
jeffhaobdb76512011-09-07 11:43:16 -07002735 case Instruction::MUL_INT_LIT16:
2736 case Instruction::DIV_INT_LIT16:
2737 case Instruction::REM_INT_LIT16:
Ian Rogers7b078e82014-09-10 14:44:24 -07002738 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), false,
2739 true);
jeffhaobdb76512011-09-07 11:43:16 -07002740 break;
2741 case Instruction::AND_INT_LIT16:
2742 case Instruction::OR_INT_LIT16:
2743 case Instruction::XOR_INT_LIT16:
Ian Rogers7b078e82014-09-10 14:44:24 -07002744 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), true,
2745 true);
jeffhaobdb76512011-09-07 11:43:16 -07002746 break;
2747 case Instruction::ADD_INT_LIT8:
2748 case Instruction::RSUB_INT_LIT8:
2749 case Instruction::MUL_INT_LIT8:
2750 case Instruction::DIV_INT_LIT8:
2751 case Instruction::REM_INT_LIT8:
2752 case Instruction::SHL_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002753 case Instruction::SHR_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002754 case Instruction::USHR_INT_LIT8:
Ian Rogers7b078e82014-09-10 14:44:24 -07002755 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), false,
2756 false);
jeffhaobdb76512011-09-07 11:43:16 -07002757 break;
2758 case Instruction::AND_INT_LIT8:
2759 case Instruction::OR_INT_LIT8:
2760 case Instruction::XOR_INT_LIT8:
Ian Rogers7b078e82014-09-10 14:44:24 -07002761 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), true,
2762 false);
jeffhaobdb76512011-09-07 11:43:16 -07002763 break;
2764
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002765 // Special instructions.
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07002766 case Instruction::RETURN_VOID_NO_BARRIER:
2767 if (IsConstructor() && !IsStatic()) {
2768 auto& declaring_class = GetDeclaringClass();
2769 auto* klass = declaring_class.GetClass();
2770 for (uint32_t i = 0, num_fields = klass->NumInstanceFields(); i < num_fields; ++i) {
2771 if (klass->GetInstanceField(i)->IsFinal()) {
Mathieu Chartiere86deef2015-03-19 13:43:37 -07002772 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void-no-barrier not expected for "
2773 << PrettyField(klass->GetInstanceField(i));
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07002774 break;
2775 }
2776 }
Sebastien Hertzcc10e0e2013-06-28 14:24:48 +02002777 }
2778 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002779 // Note: the following instructions encode offsets derived from class linking.
2780 // As such they use Class*/Field*/AbstractMethod* as these offsets only have
2781 // meaning if the class linking and resolution were successful.
2782 case Instruction::IGET_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002783 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002784 break;
2785 case Instruction::IGET_WIDE_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002786 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002787 break;
2788 case Instruction::IGET_OBJECT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002789 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002790 break;
Mathieu Chartierffc605c2014-12-10 10:35:44 -08002791 case Instruction::IGET_BOOLEAN_QUICK:
2792 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true);
2793 break;
2794 case Instruction::IGET_BYTE_QUICK:
2795 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true);
2796 break;
2797 case Instruction::IGET_CHAR_QUICK:
2798 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true);
2799 break;
2800 case Instruction::IGET_SHORT_QUICK:
2801 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true);
2802 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002803 case Instruction::IPUT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002804 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002805 break;
Fred Shih37f05ef2014-07-16 18:38:08 -07002806 case Instruction::IPUT_BOOLEAN_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002807 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07002808 break;
2809 case Instruction::IPUT_BYTE_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002810 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07002811 break;
2812 case Instruction::IPUT_CHAR_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002813 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07002814 break;
2815 case Instruction::IPUT_SHORT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002816 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07002817 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002818 case Instruction::IPUT_WIDE_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002819 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002820 break;
2821 case Instruction::IPUT_OBJECT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002822 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002823 break;
2824 case Instruction::INVOKE_VIRTUAL_QUICK:
2825 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: {
2826 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002827 ArtMethod* called_method = VerifyInvokeVirtualQuickArgs(inst, is_range);
Ian Rogers7b078e82014-09-10 14:44:24 -07002828 if (called_method != nullptr) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002829 const char* descriptor = called_method->GetReturnTypeDescriptor();
Ian Rogersd8f69b02014-09-10 21:43:52 +00002830 const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002831 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002832 work_line_->SetResultRegisterType(this, return_type);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002833 } else {
2834 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2835 }
2836 just_set_result = true;
2837 }
2838 break;
2839 }
2840
Ian Rogersd81871c2011-10-03 13:57:23 -07002841 /* These should never appear during verification. */
Mathieu Chartierffc605c2014-12-10 10:35:44 -08002842 case Instruction::UNUSED_3E ... Instruction::UNUSED_43:
2843 case Instruction::UNUSED_F3 ... Instruction::UNUSED_FF:
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002844 case Instruction::UNUSED_79:
2845 case Instruction::UNUSED_7A:
jeffhaod5347e02012-03-22 17:25:05 -07002846 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Unexpected opcode " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07002847 break;
2848
2849 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002850 * DO NOT add a "default" clause here. Without it the compiler will
jeffhaobdb76512011-09-07 11:43:16 -07002851 * complain if an instruction is missing (which is desirable).
2852 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002853 } // end - switch (dec_insn.opcode)
jeffhaobdb76512011-09-07 11:43:16 -07002854
Stephen Kyle7e541c92014-12-17 17:10:02 +00002855 /*
2856 * If we are in a constructor, and we had an UninitializedThis type
2857 * in a register somewhere, we need to make sure it wasn't overwritten.
2858 */
2859 if (track_uninitialized_this) {
2860 bool was_invoke_direct = (inst->Opcode() == Instruction::INVOKE_DIRECT ||
2861 inst->Opcode() == Instruction::INVOKE_DIRECT_RANGE);
2862 if (work_line_->WasUninitializedThisOverwritten(this, uninitialized_this_loc,
2863 was_invoke_direct)) {
2864 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
2865 << "Constructor failed to initialize this object";
2866 }
2867 }
2868
Ian Rogersad0b3a32012-04-16 14:50:24 -07002869 if (have_pending_hard_failure_) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08002870 if (Runtime::Current()->IsAotCompiler()) {
2871 /* When AOT compiling, check that the last failure is a hard failure */
Andreas Gampeb588f4c2015-05-26 13:35:39 -07002872 if (failures_[failures_.size() - 1] != VERIFY_ERROR_BAD_CLASS_HARD) {
2873 LOG(ERROR) << "Pending failures:";
2874 for (auto& error : failures_) {
2875 LOG(ERROR) << error;
2876 }
2877 for (auto& error_msg : failure_messages_) {
2878 LOG(ERROR) << error_msg->str();
2879 }
2880 LOG(FATAL) << "Pending hard failure, but last failure not hard.";
2881 }
Ian Rogerse1758fe2012-04-19 11:31:15 -07002882 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002883 /* immediate failure, reject class */
2884 info_messages_ << "Rejecting opcode " << inst->DumpString(dex_file_);
2885 return false;
jeffhaofaf459e2012-08-31 15:32:47 -07002886 } else if (have_pending_runtime_throw_failure_) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07002887 /* checking interpreter will throw, mark following code as unreachable */
jeffhaofaf459e2012-08-31 15:32:47 -07002888 opcode_flags = Instruction::kThrow;
jeffhaobdb76512011-09-07 11:43:16 -07002889 }
jeffhaobdb76512011-09-07 11:43:16 -07002890 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002891 * If we didn't just set the result register, clear it out. This ensures that you can only use
2892 * "move-result" immediately after the result is set. (We could check this statically, but it's
2893 * not expensive and it makes our debugging output cleaner.)
jeffhaobdb76512011-09-07 11:43:16 -07002894 */
2895 if (!just_set_result) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002896 work_line_->SetResultTypeToUnknown(this);
jeffhaobdb76512011-09-07 11:43:16 -07002897 }
2898
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002899
jeffhaobdb76512011-09-07 11:43:16 -07002900
2901 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002902 * Handle "branch". Tag the branch target.
jeffhaobdb76512011-09-07 11:43:16 -07002903 *
2904 * NOTE: instructions like Instruction::EQZ provide information about the
jeffhaod1f0fde2011-09-08 17:25:33 -07002905 * state of the register when the branch is taken or not taken. For example,
jeffhaobdb76512011-09-07 11:43:16 -07002906 * somebody could get a reference field, check it for zero, and if the
2907 * branch is taken immediately store that register in a boolean field
jeffhaod1f0fde2011-09-08 17:25:33 -07002908 * since the value is known to be zero. We do not currently account for
jeffhaobdb76512011-09-07 11:43:16 -07002909 * that, and will reject the code.
2910 *
2911 * TODO: avoid re-fetching the branch target
2912 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002913 if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002914 bool isConditional, selfOkay;
Ian Rogersd81871c2011-10-03 13:57:23 -07002915 if (!GetBranchOffset(work_insn_idx_, &branch_target, &isConditional, &selfOkay)) {
jeffhaobdb76512011-09-07 11:43:16 -07002916 /* should never happen after static verification */
jeffhaod5347e02012-03-22 17:25:05 -07002917 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad branch";
jeffhaobdb76512011-09-07 11:43:16 -07002918 return false;
2919 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08002920 DCHECK_EQ(isConditional, (opcode_flags & Instruction::kContinue) != 0);
Stephen Kyle9bc61992014-09-22 13:53:15 +01002921 if (!CheckNotMoveExceptionOrMoveResult(code_item_->insns_, work_insn_idx_ + branch_target)) {
jeffhaobdb76512011-09-07 11:43:16 -07002922 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002923 }
jeffhaobdb76512011-09-07 11:43:16 -07002924 /* update branch target, set "changed" if appropriate */
Ian Rogers7b078e82014-09-10 14:44:24 -07002925 if (nullptr != branch_line.get()) {
Ian Rogersebbdd872014-07-07 23:53:08 -07002926 if (!UpdateRegisters(work_insn_idx_ + branch_target, branch_line.get(), false)) {
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002927 return false;
2928 }
2929 } else {
Ian Rogersebbdd872014-07-07 23:53:08 -07002930 if (!UpdateRegisters(work_insn_idx_ + branch_target, work_line_.get(), false)) {
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002931 return false;
2932 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002933 }
jeffhaobdb76512011-09-07 11:43:16 -07002934 }
2935
2936 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002937 * Handle "switch". Tag all possible branch targets.
jeffhaobdb76512011-09-07 11:43:16 -07002938 *
2939 * We've already verified that the table is structurally sound, so we
2940 * just need to walk through and tag the targets.
2941 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002942 if ((opcode_flags & Instruction::kSwitch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002943 int offset_to_switch = insns[1] | (((int32_t) insns[2]) << 16);
2944 const uint16_t* switch_insns = insns + offset_to_switch;
2945 int switch_count = switch_insns[1];
2946 int offset_to_targets, targ;
2947
2948 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
2949 /* 0 = sig, 1 = count, 2/3 = first key */
2950 offset_to_targets = 4;
2951 } else {
2952 /* 0 = sig, 1 = count, 2..count * 2 = keys */
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07002953 DCHECK((*insns & 0xff) == Instruction::SPARSE_SWITCH);
jeffhaobdb76512011-09-07 11:43:16 -07002954 offset_to_targets = 2 + 2 * switch_count;
2955 }
2956
2957 /* verify each switch target */
2958 for (targ = 0; targ < switch_count; targ++) {
2959 int offset;
2960 uint32_t abs_offset;
2961
2962 /* offsets are 32-bit, and only partly endian-swapped */
2963 offset = switch_insns[offset_to_targets + targ * 2] |
2964 (((int32_t) switch_insns[offset_to_targets + targ * 2 + 1]) << 16);
Ian Rogersd81871c2011-10-03 13:57:23 -07002965 abs_offset = work_insn_idx_ + offset;
2966 DCHECK_LT(abs_offset, code_item_->insns_size_in_code_units_);
Stephen Kyle9bc61992014-09-22 13:53:15 +01002967 if (!CheckNotMoveExceptionOrMoveResult(code_item_->insns_, abs_offset)) {
jeffhaobdb76512011-09-07 11:43:16 -07002968 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002969 }
Ian Rogersebbdd872014-07-07 23:53:08 -07002970 if (!UpdateRegisters(abs_offset, work_line_.get(), false)) {
jeffhaobdb76512011-09-07 11:43:16 -07002971 return false;
Ian Rogersebbdd872014-07-07 23:53:08 -07002972 }
jeffhaobdb76512011-09-07 11:43:16 -07002973 }
2974 }
2975
2976 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002977 * Handle instructions that can throw and that are sitting in a "try" block. (If they're not in a
2978 * "try" block when they throw, control transfers out of the method.)
jeffhaobdb76512011-09-07 11:43:16 -07002979 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002980 if ((opcode_flags & Instruction::kThrow) != 0 && insn_flags_[work_insn_idx_].IsInTry()) {
Andreas Gampef91baf12014-07-18 15:41:00 -07002981 bool has_catch_all_handler = false;
Ian Rogers0571d352011-11-03 19:51:38 -07002982 CatchHandlerIterator iterator(*code_item_, work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07002983
Andreas Gampef91baf12014-07-18 15:41:00 -07002984 // Need the linker to try and resolve the handled class to check if it's Throwable.
2985 ClassLinker* linker = Runtime::Current()->GetClassLinker();
2986
Ian Rogers0571d352011-11-03 19:51:38 -07002987 for (; iterator.HasNext(); iterator.Next()) {
Andreas Gampef91baf12014-07-18 15:41:00 -07002988 uint16_t handler_type_idx = iterator.GetHandlerTypeIndex();
2989 if (handler_type_idx == DexFile::kDexNoIndex16) {
2990 has_catch_all_handler = true;
2991 } else {
2992 // It is also a catch-all if it is java.lang.Throwable.
Mathieu Chartierbf99f772014-08-23 16:37:27 -07002993 mirror::Class* klass = linker->ResolveType(*dex_file_, handler_type_idx, dex_cache_,
2994 class_loader_);
Andreas Gampef91baf12014-07-18 15:41:00 -07002995 if (klass != nullptr) {
2996 if (klass == mirror::Throwable::GetJavaLangThrowable()) {
2997 has_catch_all_handler = true;
2998 }
2999 } else {
3000 // Clear exception.
Ian Rogers7b078e82014-09-10 14:44:24 -07003001 DCHECK(self_->IsExceptionPending());
3002 self_->ClearException();
Andreas Gampef91baf12014-07-18 15:41:00 -07003003 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003004 }
jeffhaobdb76512011-09-07 11:43:16 -07003005 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003006 * Merge registers into the "catch" block. We want to use the "savedRegs" rather than
3007 * "work_regs", because at runtime the exception will be thrown before the instruction
3008 * modifies any registers.
jeffhaobdb76512011-09-07 11:43:16 -07003009 */
Ian Rogersebbdd872014-07-07 23:53:08 -07003010 if (!UpdateRegisters(iterator.GetHandlerAddress(), saved_line_.get(), false)) {
jeffhaobdb76512011-09-07 11:43:16 -07003011 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07003012 }
jeffhaobdb76512011-09-07 11:43:16 -07003013 }
3014
3015 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003016 * If the monitor stack depth is nonzero, there must be a "catch all" handler for this
3017 * instruction. This does apply to monitor-exit because of async exception handling.
jeffhaobdb76512011-09-07 11:43:16 -07003018 */
Andreas Gampef91baf12014-07-18 15:41:00 -07003019 if (work_line_->MonitorStackDepth() > 0 && !has_catch_all_handler) {
jeffhaobdb76512011-09-07 11:43:16 -07003020 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003021 * The state in work_line reflects the post-execution state. If the current instruction is a
3022 * monitor-enter and the monitor stack was empty, we don't need a catch-all (if it throws,
jeffhaobdb76512011-09-07 11:43:16 -07003023 * it will do so before grabbing the lock).
3024 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02003025 if (inst->Opcode() != Instruction::MONITOR_ENTER || work_line_->MonitorStackDepth() != 1) {
jeffhaod5347e02012-03-22 17:25:05 -07003026 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
Ian Rogersd81871c2011-10-03 13:57:23 -07003027 << "expected to be within a catch-all for an instruction where a monitor is held";
jeffhaobdb76512011-09-07 11:43:16 -07003028 return false;
3029 }
3030 }
3031 }
3032
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003033 /* Handle "continue". Tag the next consecutive instruction.
3034 * Note: Keep the code handling "continue" case below the "branch" and "switch" cases,
3035 * because it changes work_line_ when performing peephole optimization
3036 * and this change should not be used in those cases.
3037 */
Ian Rogers6d376ae2013-07-23 15:12:40 -07003038 if ((opcode_flags & Instruction::kContinue) != 0) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003039 DCHECK_EQ(Instruction::At(code_item_->insns_ + work_insn_idx_), inst);
3040 uint32_t next_insn_idx = work_insn_idx_ + inst->SizeInCodeUnits();
Ian Rogers6d376ae2013-07-23 15:12:40 -07003041 if (next_insn_idx >= code_item_->insns_size_in_code_units_) {
3042 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Execution can walk off end of code area";
3043 return false;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003044 }
Ian Rogers6d376ae2013-07-23 15:12:40 -07003045 // The only way to get to a move-exception instruction is to get thrown there. Make sure the
3046 // next instruction isn't one.
3047 if (!CheckNotMoveException(code_item_->insns_, next_insn_idx)) {
3048 return false;
3049 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003050 if (nullptr != fallthrough_line.get()) {
Ian Rogers6d376ae2013-07-23 15:12:40 -07003051 // Make workline consistent with fallthrough computed from peephole optimization.
3052 work_line_->CopyFromLine(fallthrough_line.get());
3053 }
Ian Rogersb8c78592013-07-25 23:52:52 +00003054 if (insn_flags_[next_insn_idx].IsReturn()) {
3055 // For returns we only care about the operand to the return, all other registers are dead.
3056 const Instruction* ret_inst = Instruction::At(code_item_->insns_ + next_insn_idx);
3057 Instruction::Code opcode = ret_inst->Opcode();
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07003058 if (opcode == Instruction::RETURN_VOID || opcode == Instruction::RETURN_VOID_NO_BARRIER) {
Stephen Kyle7e541c92014-12-17 17:10:02 +00003059 SafelyMarkAllRegistersAsConflicts(this, work_line_.get());
Ian Rogersb8c78592013-07-25 23:52:52 +00003060 } else {
3061 if (opcode == Instruction::RETURN_WIDE) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003062 work_line_->MarkAllRegistersAsConflictsExceptWide(this, ret_inst->VRegA_11x());
Ian Rogersb8c78592013-07-25 23:52:52 +00003063 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07003064 work_line_->MarkAllRegistersAsConflictsExcept(this, ret_inst->VRegA_11x());
Ian Rogersb8c78592013-07-25 23:52:52 +00003065 }
3066 }
3067 }
Ian Rogers6d376ae2013-07-23 15:12:40 -07003068 RegisterLine* next_line = reg_table_.GetLine(next_insn_idx);
Ian Rogers7b078e82014-09-10 14:44:24 -07003069 if (next_line != nullptr) {
Ian Rogersebbdd872014-07-07 23:53:08 -07003070 // Merge registers into what we have for the next instruction, and set the "changed" flag if
3071 // needed. If the merge changes the state of the registers then the work line will be
3072 // updated.
3073 if (!UpdateRegisters(next_insn_idx, work_line_.get(), true)) {
Ian Rogers6d376ae2013-07-23 15:12:40 -07003074 return false;
3075 }
3076 } else {
3077 /*
3078 * We're not recording register data for the next instruction, so we don't know what the
3079 * prior state was. We have to assume that something has changed and re-evaluate it.
3080 */
3081 insn_flags_[next_insn_idx].SetChanged();
3082 }
3083 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003084
jeffhaod1f0fde2011-09-08 17:25:33 -07003085 /* If we're returning from the method, make sure monitor stack is empty. */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003086 if ((opcode_flags & Instruction::kReturn) != 0) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003087 if (!work_line_->VerifyMonitorStackEmpty(this)) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003088 return false;
3089 }
jeffhaobdb76512011-09-07 11:43:16 -07003090 }
3091
3092 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003093 * Update start_guess. Advance to the next instruction of that's
3094 * possible, otherwise use the branch target if one was found. If
jeffhaobdb76512011-09-07 11:43:16 -07003095 * neither of those exists we're in a return or throw; leave start_guess
3096 * alone and let the caller sort it out.
3097 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003098 if ((opcode_flags & Instruction::kContinue) != 0) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003099 DCHECK_EQ(Instruction::At(code_item_->insns_ + work_insn_idx_), inst);
3100 *start_guess = work_insn_idx_ + inst->SizeInCodeUnits();
Elliott Hughesadb8c672012-03-06 16:49:32 -08003101 } else if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07003102 /* we're still okay if branch_target is zero */
Ian Rogersd81871c2011-10-03 13:57:23 -07003103 *start_guess = work_insn_idx_ + branch_target;
jeffhaobdb76512011-09-07 11:43:16 -07003104 }
3105
Ian Rogersd81871c2011-10-03 13:57:23 -07003106 DCHECK_LT(*start_guess, code_item_->insns_size_in_code_units_);
3107 DCHECK(insn_flags_[*start_guess].IsOpcode());
jeffhaobdb76512011-09-07 11:43:16 -07003108
3109 return true;
Brian Carlstrom7934ac22013-07-26 10:54:15 -07003110} // NOLINT(readability/fn_size)
jeffhaobdb76512011-09-07 11:43:16 -07003111
Ian Rogersd8f69b02014-09-10 21:43:52 +00003112const RegType& MethodVerifier::ResolveClassAndCheckAccess(uint32_t class_idx) {
Ian Rogers0571d352011-11-03 19:51:38 -07003113 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
Ian Rogersd8f69b02014-09-10 21:43:52 +00003114 const RegType& referrer = GetDeclaringClass();
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003115 mirror::Class* klass = dex_cache_->GetResolvedType(class_idx);
Ian Rogers7b078e82014-09-10 14:44:24 -07003116 const RegType& result = klass != nullptr ?
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003117 reg_types_.FromClass(descriptor, klass, klass->CannotBeAssignedFromOtherTypes()) :
3118 reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003119 if (result.IsConflict()) {
3120 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "accessing broken descriptor '" << descriptor
3121 << "' in " << referrer;
3122 return result;
3123 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003124 if (klass == nullptr && !result.IsUnresolvedTypes()) {
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003125 dex_cache_->SetResolvedType(class_idx, result.GetClass());
Ian Rogerse1758fe2012-04-19 11:31:15 -07003126 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003127 // Check if access is allowed. Unresolved types use xxxWithAccessCheck to
Jeff Haob24b4a72013-07-31 13:47:31 -07003128 // check at runtime if access is allowed and so pass here. If result is
3129 // primitive, skip the access check.
3130 if (result.IsNonZeroReferenceTypes() && !result.IsUnresolvedTypes() &&
3131 !referrer.IsUnresolvedTypes() && !referrer.CanAccess(result)) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07003132 Fail(VERIFY_ERROR_ACCESS_CLASS) << "illegal class access: '"
Ian Rogersad0b3a32012-04-16 14:50:24 -07003133 << referrer << "' -> '" << result << "'";
Ian Rogers28ad40d2011-10-27 15:19:26 -07003134 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003135 return result;
Ian Rogersd81871c2011-10-03 13:57:23 -07003136}
3137
Ian Rogersd8f69b02014-09-10 21:43:52 +00003138const RegType& MethodVerifier::GetCaughtExceptionType() {
Ian Rogers7b078e82014-09-10 14:44:24 -07003139 const RegType* common_super = nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003140 if (code_item_->tries_size_ != 0) {
Ian Rogers13735952014-10-08 12:43:28 -07003141 const uint8_t* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
Ian Rogersd81871c2011-10-03 13:57:23 -07003142 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
3143 for (uint32_t i = 0; i < handlers_size; i++) {
Ian Rogers0571d352011-11-03 19:51:38 -07003144 CatchHandlerIterator iterator(handlers_ptr);
3145 for (; iterator.HasNext(); iterator.Next()) {
3146 if (iterator.GetHandlerAddress() == (uint32_t) work_insn_idx_) {
3147 if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) {
Ian Rogersb4903572012-10-11 11:52:56 -07003148 common_super = &reg_types_.JavaLangThrowable(false);
Ian Rogersd81871c2011-10-03 13:57:23 -07003149 } else {
Ian Rogersd8f69b02014-09-10 21:43:52 +00003150 const RegType& exception = ResolveClassAndCheckAccess(iterator.GetHandlerTypeIndex());
Jeff Haob878f212014-04-24 16:25:36 -07003151 if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(exception)) {
Jeff Haoc26a56c2013-11-04 12:00:47 -08003152 if (exception.IsUnresolvedTypes()) {
3153 // We don't know enough about the type. Fail here and let runtime handle it.
3154 Fail(VERIFY_ERROR_NO_CLASS) << "unresolved exception class " << exception;
3155 return exception;
3156 } else {
3157 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unexpected non-exception class " << exception;
3158 return reg_types_.Conflict();
3159 }
Jeff Haob878f212014-04-24 16:25:36 -07003160 } else if (common_super == nullptr) {
3161 common_super = &exception;
Ian Rogers28ad40d2011-10-27 15:19:26 -07003162 } else if (common_super->Equals(exception)) {
Ian Rogersc4762272012-02-01 15:55:55 -08003163 // odd case, but nothing to do
Ian Rogersd81871c2011-10-03 13:57:23 -07003164 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07003165 common_super = &common_super->Merge(exception, &reg_types_);
Andreas Gampe7c038102014-10-27 20:08:46 -07003166 if (FailOrAbort(this,
3167 reg_types_.JavaLangThrowable(false).IsAssignableFrom(*common_super),
3168 "java.lang.Throwable is not assignable-from common_super at ",
3169 work_insn_idx_)) {
3170 break;
3171 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003172 }
3173 }
3174 }
3175 }
Ian Rogers0571d352011-11-03 19:51:38 -07003176 handlers_ptr = iterator.EndDataPointer();
Ian Rogersd81871c2011-10-03 13:57:23 -07003177 }
3178 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003179 if (common_super == nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003180 /* no catch blocks, or no catches with classes we can find */
jeffhaod5347e02012-03-22 17:25:05 -07003181 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unable to find exception handler";
Ian Rogersad0b3a32012-04-16 14:50:24 -07003182 return reg_types_.Conflict();
Ian Rogersd81871c2011-10-03 13:57:23 -07003183 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07003184 return *common_super;
Ian Rogersd81871c2011-10-03 13:57:23 -07003185}
3186
Mathieu Chartiere401d142015-04-22 13:56:20 -07003187ArtMethod* MethodVerifier::ResolveMethodAndCheckAccess(
3188 uint32_t dex_method_idx, MethodType method_type) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003189 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx);
Ian Rogersd8f69b02014-09-10 21:43:52 +00003190 const RegType& klass_type = ResolveClassAndCheckAccess(method_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003191 if (klass_type.IsConflict()) {
3192 std::string append(" in attempt to access method ");
3193 append += dex_file_->GetMethodName(method_id);
3194 AppendToLastFailMessage(append);
Ian Rogers7b078e82014-09-10 14:44:24 -07003195 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08003196 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003197 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003198 return nullptr; // Can't resolve Class so no more to do here
Ian Rogers90040192011-12-16 08:54:29 -08003199 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003200 mirror::Class* klass = klass_type.GetClass();
Ian Rogersd8f69b02014-09-10 21:43:52 +00003201 const RegType& referrer = GetDeclaringClass();
Mathieu Chartiere401d142015-04-22 13:56:20 -07003202 auto* cl = Runtime::Current()->GetClassLinker();
3203 auto pointer_size = cl->GetImagePointerSize();
3204 ArtMethod* res_method = dex_cache_->GetResolvedMethod(dex_method_idx, pointer_size);
Ian Rogers7b078e82014-09-10 14:44:24 -07003205 if (res_method == nullptr) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07003206 const char* name = dex_file_->GetMethodName(method_id);
Ian Rogersd91d6d62013-09-25 20:26:14 -07003207 const Signature signature = dex_file_->GetMethodSignature(method_id);
jeffhao8cd6dda2012-02-22 10:15:34 -08003208
3209 if (method_type == METHOD_DIRECT || method_type == METHOD_STATIC) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003210 res_method = klass->FindDirectMethod(name, signature, pointer_size);
jeffhao8cd6dda2012-02-22 10:15:34 -08003211 } else if (method_type == METHOD_INTERFACE) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003212 res_method = klass->FindInterfaceMethod(name, signature, pointer_size);
Ian Rogersd81871c2011-10-03 13:57:23 -07003213 } else {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003214 res_method = klass->FindVirtualMethod(name, signature, pointer_size);
Ian Rogersd81871c2011-10-03 13:57:23 -07003215 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003216 if (res_method != nullptr) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003217 dex_cache_->SetResolvedMethod(dex_method_idx, res_method, pointer_size);
Ian Rogersd81871c2011-10-03 13:57:23 -07003218 } else {
jeffhao8cd6dda2012-02-22 10:15:34 -08003219 // If a virtual or interface method wasn't found with the expected type, look in
3220 // the direct methods. This can happen when the wrong invoke type is used or when
3221 // a class has changed, and will be flagged as an error in later checks.
3222 if (method_type == METHOD_INTERFACE || method_type == METHOD_VIRTUAL) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003223 res_method = klass->FindDirectMethod(name, signature, pointer_size);
jeffhao8cd6dda2012-02-22 10:15:34 -08003224 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003225 if (res_method == nullptr) {
jeffhao8cd6dda2012-02-22 10:15:34 -08003226 Fail(VERIFY_ERROR_NO_METHOD) << "couldn't find method "
3227 << PrettyDescriptor(klass) << "." << name
3228 << " " << signature;
Ian Rogers7b078e82014-09-10 14:44:24 -07003229 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003230 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003231 }
3232 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003233 // Make sure calls to constructors are "direct". There are additional restrictions but we don't
3234 // enforce them here.
3235 if (res_method->IsConstructor() && method_type != METHOD_DIRECT) {
jeffhaod5347e02012-03-22 17:25:05 -07003236 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting non-direct call to constructor "
3237 << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003238 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003239 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003240 // Disallow any calls to class initializers.
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003241 if (res_method->IsClassInitializer()) {
jeffhaod5347e02012-03-22 17:25:05 -07003242 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting call to class initializer "
3243 << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003244 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003245 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003246 // Check if access is allowed.
Ian Rogersad0b3a32012-04-16 14:50:24 -07003247 if (!referrer.CanAccessMember(res_method->GetDeclaringClass(), res_method->GetAccessFlags())) {
jeffhao8cd6dda2012-02-22 10:15:34 -08003248 Fail(VERIFY_ERROR_ACCESS_METHOD) << "illegal method access (call " << PrettyMethod(res_method)
Ian Rogersad0b3a32012-04-16 14:50:24 -07003249 << " from " << referrer << ")";
jeffhaob57e9522012-04-26 18:08:21 -07003250 return res_method;
jeffhao8cd6dda2012-02-22 10:15:34 -08003251 }
jeffhaode0d9c92012-02-27 13:58:13 -08003252 // Check that invoke-virtual and invoke-super are not used on private methods of the same class.
3253 if (res_method->IsPrivate() && method_type == METHOD_VIRTUAL) {
jeffhaod5347e02012-03-22 17:25:05 -07003254 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invoke-super/virtual can't be used on private method "
3255 << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003256 return nullptr;
jeffhaode0d9c92012-02-27 13:58:13 -08003257 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003258 // Check that interface methods match interface classes.
3259 if (klass->IsInterface() && method_type != METHOD_INTERFACE) {
3260 Fail(VERIFY_ERROR_CLASS_CHANGE) << "non-interface method " << PrettyMethod(res_method)
3261 << " is in an interface class " << PrettyClass(klass);
Ian Rogers7b078e82014-09-10 14:44:24 -07003262 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003263 } else if (!klass->IsInterface() && method_type == METHOD_INTERFACE) {
3264 Fail(VERIFY_ERROR_CLASS_CHANGE) << "interface method " << PrettyMethod(res_method)
3265 << " is in a non-interface class " << PrettyClass(klass);
Ian Rogers7b078e82014-09-10 14:44:24 -07003266 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003267 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003268 // See if the method type implied by the invoke instruction matches the access flags for the
3269 // target method.
Brian Carlstrombe6fa5e2014-12-09 20:15:42 -08003270 if ((method_type == METHOD_DIRECT && (!res_method->IsDirect() || res_method->IsStatic())) ||
Ian Rogersd81871c2011-10-03 13:57:23 -07003271 (method_type == METHOD_STATIC && !res_method->IsStatic()) ||
3272 ((method_type == METHOD_VIRTUAL || method_type == METHOD_INTERFACE) && res_method->IsDirect())
3273 ) {
Ian Rogers2fc14272012-08-30 10:56:57 -07003274 Fail(VERIFY_ERROR_CLASS_CHANGE) << "invoke type (" << method_type << ") does not match method "
3275 " type of " << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003276 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003277 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003278 return res_method;
3279}
3280
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003281template <class T>
Mathieu Chartiere401d142015-04-22 13:56:20 -07003282ArtMethod* MethodVerifier::VerifyInvocationArgsFromIterator(
3283 T* it, const Instruction* inst, MethodType method_type, bool is_range, ArtMethod* res_method) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003284 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
3285 // match the call to the signature. Also, we might be calling through an abstract method
3286 // definition (which doesn't have register count values).
3287 const size_t expected_args = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
3288 /* caught by static verifier */
3289 DCHECK(is_range || expected_args <= 5);
3290 if (expected_args > code_item_->outs_size_) {
3291 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
3292 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
3293 return nullptr;
3294 }
3295
3296 uint32_t arg[5];
3297 if (!is_range) {
3298 inst->GetVarArgs(arg);
3299 }
3300 uint32_t sig_registers = 0;
3301
3302 /*
3303 * Check the "this" argument, which must be an instance of the class that declared the method.
3304 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
3305 * rigorous check here (which is okay since we have to do it at runtime).
3306 */
3307 if (method_type != METHOD_STATIC) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003308 const RegType& actual_arg_type = work_line_->GetInvocationThis(this, inst, is_range);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003309 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
3310 CHECK(have_pending_hard_failure_);
3311 return nullptr;
3312 }
3313 if (actual_arg_type.IsUninitializedReference()) {
3314 if (res_method) {
3315 if (!res_method->IsConstructor()) {
3316 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
3317 return nullptr;
3318 }
3319 } else {
3320 // Check whether the name of the called method is "<init>"
3321 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Jeff Hao0d087272014-08-04 14:47:17 -07003322 if (strcmp(dex_file_->GetMethodName(dex_file_->GetMethodId(method_idx)), "<init>") != 0) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003323 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
3324 return nullptr;
3325 }
3326 }
3327 }
3328 if (method_type != METHOD_INTERFACE && !actual_arg_type.IsZero()) {
Ian Rogersd8f69b02014-09-10 21:43:52 +00003329 const RegType* res_method_class;
Andreas Gamped9e23012015-06-04 22:19:58 -07003330 // Miranda methods have the declaring interface as their declaring class, not the abstract
3331 // class. It would be wrong to use this for the type check (interface type checks are
3332 // postponed to runtime).
3333 if (res_method != nullptr && !res_method->IsMiranda()) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003334 mirror::Class* klass = res_method->GetDeclaringClass();
Ian Rogers1ff3c982014-08-12 02:30:58 -07003335 std::string temp;
3336 res_method_class = &reg_types_.FromClass(klass->GetDescriptor(&temp), klass,
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003337 klass->CannotBeAssignedFromOtherTypes());
3338 } else {
3339 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
3340 const uint16_t class_idx = dex_file_->GetMethodId(method_idx).class_idx_;
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003341 res_method_class = &reg_types_.FromDescriptor(GetClassLoader(),
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003342 dex_file_->StringByTypeIdx(class_idx),
3343 false);
3344 }
3345 if (!res_method_class->IsAssignableFrom(actual_arg_type)) {
3346 Fail(actual_arg_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS:
3347 VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type
3348 << "' not instance of '" << *res_method_class << "'";
3349 // Continue on soft failures. We need to find possible hard failures to avoid problems in
3350 // the compiler.
3351 if (have_pending_hard_failure_) {
3352 return nullptr;
3353 }
3354 }
3355 }
3356 sig_registers = 1;
3357 }
3358
3359 for ( ; it->HasNext(); it->Next()) {
3360 if (sig_registers >= expected_args) {
3361 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, expected " << inst->VRegA() <<
3362 " arguments, found " << sig_registers << " or more.";
3363 return nullptr;
3364 }
3365
3366 const char* param_descriptor = it->GetDescriptor();
3367
3368 if (param_descriptor == nullptr) {
3369 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation because of missing signature "
3370 "component";
3371 return nullptr;
3372 }
3373
Ian Rogersd8f69b02014-09-10 21:43:52 +00003374 const RegType& reg_type = reg_types_.FromDescriptor(GetClassLoader(), param_descriptor, false);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003375 uint32_t get_reg = is_range ? inst->VRegC_3rc() + static_cast<uint32_t>(sig_registers) :
3376 arg[sig_registers];
3377 if (reg_type.IsIntegralTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003378 const RegType& src_type = work_line_->GetRegisterType(this, get_reg);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003379 if (!src_type.IsIntegralTypes()) {
3380 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register v" << get_reg << " has type " << src_type
3381 << " but expected " << reg_type;
Andreas Gampeb588f4c2015-05-26 13:35:39 -07003382 return nullptr;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003383 }
Andreas Gampeda9badb2015-06-05 20:22:12 -07003384 } else {
3385 if (!work_line_->VerifyRegisterType(this, get_reg, reg_type)) {
3386 // Continue on soft failures. We need to find possible hard failures to avoid problems in
3387 // the compiler.
3388 if (have_pending_hard_failure_) {
3389 return nullptr;
3390 }
3391 } else if (reg_type.IsLongOrDoubleTypes()) {
3392 // Check that registers are consecutive (for non-range invokes). Invokes are the only
3393 // instructions not specifying register pairs by the first component, but require them
3394 // nonetheless. Only check when there's an actual register in the parameters. If there's
3395 // none, this will fail below.
3396 if (!is_range && sig_registers + 1 < expected_args) {
3397 uint32_t second_reg = arg[sig_registers + 1];
3398 if (second_reg != get_reg + 1) {
3399 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, long or double parameter "
3400 "at index " << sig_registers << " is not a pair: " << get_reg << " + "
3401 << second_reg << ".";
3402 return nullptr;
3403 }
3404 }
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003405 }
3406 }
3407 sig_registers += reg_type.IsLongOrDoubleTypes() ? 2 : 1;
3408 }
3409 if (expected_args != sig_registers) {
3410 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, expected " << expected_args <<
3411 " arguments, found " << sig_registers;
3412 return nullptr;
3413 }
3414 return res_method;
3415}
3416
3417void MethodVerifier::VerifyInvocationArgsUnresolvedMethod(const Instruction* inst,
3418 MethodType method_type,
3419 bool is_range) {
3420 // As the method may not have been resolved, make this static check against what we expect.
3421 // The main reason for this code block is to fail hard when we find an illegal use, e.g.,
3422 // wrong number of arguments or wrong primitive types, even if the method could not be resolved.
3423 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
3424 DexFileParameterIterator it(*dex_file_,
3425 dex_file_->GetProtoId(dex_file_->GetMethodId(method_idx).proto_idx_));
3426 VerifyInvocationArgsFromIterator<DexFileParameterIterator>(&it, inst, method_type, is_range,
3427 nullptr);
3428}
3429
3430class MethodParamListDescriptorIterator {
3431 public:
Mathieu Chartiere401d142015-04-22 13:56:20 -07003432 explicit MethodParamListDescriptorIterator(ArtMethod* res_method) :
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003433 res_method_(res_method), pos_(0), params_(res_method->GetParameterTypeList()),
3434 params_size_(params_ == nullptr ? 0 : params_->Size()) {
3435 }
3436
3437 bool HasNext() {
3438 return pos_ < params_size_;
3439 }
3440
3441 void Next() {
3442 ++pos_;
3443 }
3444
3445 const char* GetDescriptor() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
3446 return res_method_->GetTypeDescriptorFromTypeIdx(params_->GetTypeItem(pos_).type_idx_);
3447 }
3448
3449 private:
Mathieu Chartiere401d142015-04-22 13:56:20 -07003450 ArtMethod* res_method_;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003451 size_t pos_;
3452 const DexFile::TypeList* params_;
3453 const size_t params_size_;
3454};
3455
Mathieu Chartiere401d142015-04-22 13:56:20 -07003456ArtMethod* MethodVerifier::VerifyInvocationArgs(
3457 const Instruction* inst, MethodType method_type, bool is_range, bool is_super) {
jeffhao8cd6dda2012-02-22 10:15:34 -08003458 // Resolve the method. This could be an abstract or concrete method depending on what sort of call
3459 // we're making.
Sebastien Hertz5243e912013-05-21 10:55:07 +02003460 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Andreas Gampeacc4d2f2014-06-12 19:35:05 -07003461
Mathieu Chartiere401d142015-04-22 13:56:20 -07003462 ArtMethod* res_method = ResolveMethodAndCheckAccess(method_idx, method_type);
Ian Rogers7b078e82014-09-10 14:44:24 -07003463 if (res_method == nullptr) { // error or class is unresolved
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003464 // Check what we can statically.
3465 if (!have_pending_hard_failure_) {
3466 VerifyInvocationArgsUnresolvedMethod(inst, method_type, is_range);
3467 }
3468 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003469 }
3470
Ian Rogersd81871c2011-10-03 13:57:23 -07003471 // If we're using invoke-super(method), make sure that the executing method's class' superclass
3472 // has a vtable entry for the target method.
3473 if (is_super) {
3474 DCHECK(method_type == METHOD_VIRTUAL);
Ian Rogersd8f69b02014-09-10 21:43:52 +00003475 const RegType& super = GetDeclaringClass().GetSuperClass(&reg_types_);
Ian Rogers529781d2012-07-23 17:24:29 -07003476 if (super.IsUnresolvedTypes()) {
jeffhao4d8df822012-04-24 17:09:36 -07003477 Fail(VERIFY_ERROR_NO_METHOD) << "unknown super class in invoke-super from "
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003478 << PrettyMethod(dex_method_idx_, *dex_file_)
jeffhao4d8df822012-04-24 17:09:36 -07003479 << " to super " << PrettyMethod(res_method);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003480 return nullptr;
jeffhao4d8df822012-04-24 17:09:36 -07003481 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003482 mirror::Class* super_klass = super.GetClass();
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003483 if (res_method->GetMethodIndex() >= super_klass->GetVTableLength()) {
jeffhao4d8df822012-04-24 17:09:36 -07003484 Fail(VERIFY_ERROR_NO_METHOD) << "invalid invoke-super from "
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003485 << PrettyMethod(dex_method_idx_, *dex_file_)
jeffhao4d8df822012-04-24 17:09:36 -07003486 << " to super " << super
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003487 << "." << res_method->GetName()
3488 << res_method->GetSignature();
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003489 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003490 }
3491 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003492
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003493 // Process the target method's signature. This signature may or may not
3494 MethodParamListDescriptorIterator it(res_method);
3495 return VerifyInvocationArgsFromIterator<MethodParamListDescriptorIterator>(&it, inst, method_type,
3496 is_range, res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07003497}
3498
Mathieu Chartiere401d142015-04-22 13:56:20 -07003499ArtMethod* MethodVerifier::GetQuickInvokedMethod(const Instruction* inst, RegisterLine* reg_line,
3500 bool is_range, bool allow_failure) {
Mathieu Chartier091d2382015-03-06 10:59:06 -08003501 if (is_range) {
3502 DCHECK_EQ(inst->Opcode(), Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
3503 } else {
3504 DCHECK_EQ(inst->Opcode(), Instruction::INVOKE_VIRTUAL_QUICK);
3505 }
3506 const RegType& actual_arg_type = reg_line->GetInvocationThis(this, inst, is_range, allow_failure);
Ian Rogers9bc54402014-04-17 16:40:01 -07003507 if (!actual_arg_type.HasClass()) {
3508 VLOG(verifier) << "Failed to get mirror::Class* from '" << actual_arg_type << "'";
Andreas Gampe63981562014-04-17 12:28:43 -07003509 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003510 }
Ian Rogersa4cf1df2014-05-07 19:47:17 -07003511 mirror::Class* klass = actual_arg_type.GetClass();
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003512 mirror::Class* dispatch_class;
Ian Rogersa4cf1df2014-05-07 19:47:17 -07003513 if (klass->IsInterface()) {
3514 // Derive Object.class from Class.class.getSuperclass().
3515 mirror::Class* object_klass = klass->GetClass()->GetSuperClass();
Andreas Gampe7c038102014-10-27 20:08:46 -07003516 if (FailOrAbort(this, object_klass->IsObjectClass(),
Mathieu Chartier091d2382015-03-06 10:59:06 -08003517 "Failed to find Object class in quickened invoke receiver", work_insn_idx_)) {
Andreas Gampe7c038102014-10-27 20:08:46 -07003518 return nullptr;
3519 }
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003520 dispatch_class = object_klass;
Ian Rogersa4cf1df2014-05-07 19:47:17 -07003521 } else {
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003522 dispatch_class = klass;
Ian Rogersa4cf1df2014-05-07 19:47:17 -07003523 }
Mathieu Chartier091d2382015-03-06 10:59:06 -08003524 if (!dispatch_class->HasVTable()) {
3525 FailOrAbort(this, allow_failure, "Receiver class has no vtable for quickened invoke at ",
3526 work_insn_idx_);
Andreas Gampe7c038102014-10-27 20:08:46 -07003527 return nullptr;
3528 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003529 uint16_t vtable_index = is_range ? inst->VRegB_3rc() : inst->VRegB_35c();
Mathieu Chartiere401d142015-04-22 13:56:20 -07003530 auto* cl = Runtime::Current()->GetClassLinker();
3531 auto pointer_size = cl->GetImagePointerSize();
Mathieu Chartier091d2382015-03-06 10:59:06 -08003532 if (static_cast<int32_t>(vtable_index) >= dispatch_class->GetVTableLength()) {
3533 FailOrAbort(this, allow_failure,
3534 "Receiver class has not enough vtable slots for quickened invoke at ",
3535 work_insn_idx_);
Andreas Gampe7c038102014-10-27 20:08:46 -07003536 return nullptr;
3537 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07003538 ArtMethod* res_method = dispatch_class->GetVTableEntry(vtable_index, pointer_size);
Mathieu Chartier091d2382015-03-06 10:59:06 -08003539 if (self_->IsExceptionPending()) {
3540 FailOrAbort(this, allow_failure, "Unexpected exception pending for quickened invoke at ",
3541 work_insn_idx_);
Andreas Gampe7c038102014-10-27 20:08:46 -07003542 return nullptr;
3543 }
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003544 return res_method;
3545}
3546
Mathieu Chartiere401d142015-04-22 13:56:20 -07003547ArtMethod* MethodVerifier::VerifyInvokeVirtualQuickArgs(const Instruction* inst, bool is_range) {
Andreas Gampe76bd8802014-12-10 16:43:58 -08003548 DCHECK(Runtime::Current()->IsStarted() || verify_to_dump_)
3549 << PrettyMethod(dex_method_idx_, *dex_file_, true) << "@" << work_insn_idx_;
3550
Mathieu Chartiere401d142015-04-22 13:56:20 -07003551 ArtMethod* res_method = GetQuickInvokedMethod(inst, work_line_.get(), is_range, false);
Ian Rogers7b078e82014-09-10 14:44:24 -07003552 if (res_method == nullptr) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003553 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer method from " << inst->Name();
Ian Rogers7b078e82014-09-10 14:44:24 -07003554 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003555 }
Andreas Gampe7c038102014-10-27 20:08:46 -07003556 if (FailOrAbort(this, !res_method->IsDirect(), "Quick-invoked method is direct at ",
3557 work_insn_idx_)) {
3558 return nullptr;
3559 }
3560 if (FailOrAbort(this, !res_method->IsStatic(), "Quick-invoked method is static at ",
3561 work_insn_idx_)) {
3562 return nullptr;
3563 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003564
3565 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
3566 // match the call to the signature. Also, we might be calling through an abstract method
3567 // definition (which doesn't have register count values).
Ian Rogers7b078e82014-09-10 14:44:24 -07003568 const RegType& actual_arg_type = work_line_->GetInvocationThis(this, inst, is_range);
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003569 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
Ian Rogers7b078e82014-09-10 14:44:24 -07003570 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003571 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003572 const size_t expected_args = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
3573 /* caught by static verifier */
3574 DCHECK(is_range || expected_args <= 5);
3575 if (expected_args > code_item_->outs_size_) {
3576 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
3577 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
Ian Rogers7b078e82014-09-10 14:44:24 -07003578 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003579 }
3580
3581 /*
3582 * Check the "this" argument, which must be an instance of the class that declared the method.
3583 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
3584 * rigorous check here (which is okay since we have to do it at runtime).
3585 */
3586 if (actual_arg_type.IsUninitializedReference() && !res_method->IsConstructor()) {
3587 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
Ian Rogers7b078e82014-09-10 14:44:24 -07003588 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003589 }
3590 if (!actual_arg_type.IsZero()) {
3591 mirror::Class* klass = res_method->GetDeclaringClass();
Ian Rogers1ff3c982014-08-12 02:30:58 -07003592 std::string temp;
Ian Rogersd8f69b02014-09-10 21:43:52 +00003593 const RegType& res_method_class =
Ian Rogers1ff3c982014-08-12 02:30:58 -07003594 reg_types_.FromClass(klass->GetDescriptor(&temp), klass,
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003595 klass->CannotBeAssignedFromOtherTypes());
3596 if (!res_method_class.IsAssignableFrom(actual_arg_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07003597 Fail(actual_arg_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS :
3598 VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003599 << "' not instance of '" << res_method_class << "'";
Ian Rogers7b078e82014-09-10 14:44:24 -07003600 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003601 }
3602 }
3603 /*
3604 * Process the target method's signature. This signature may or may not
3605 * have been verified, so we can't assume it's properly formed.
3606 */
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003607 const DexFile::TypeList* params = res_method->GetParameterTypeList();
Ian Rogers7b078e82014-09-10 14:44:24 -07003608 size_t params_size = params == nullptr ? 0 : params->Size();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003609 uint32_t arg[5];
3610 if (!is_range) {
Ian Rogers29a26482014-05-02 15:27:29 -07003611 inst->GetVarArgs(arg);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003612 }
3613 size_t actual_args = 1;
3614 for (size_t param_index = 0; param_index < params_size; param_index++) {
3615 if (actual_args >= expected_args) {
3616 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invalid call to '" << PrettyMethod(res_method)
Brian Carlstrom93c33962013-07-26 10:37:43 -07003617 << "'. Expected " << expected_args
3618 << " arguments, processing argument " << actual_args
3619 << " (where longs/doubles count twice).";
Ian Rogers7b078e82014-09-10 14:44:24 -07003620 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003621 }
3622 const char* descriptor =
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003623 res_method->GetTypeDescriptorFromTypeIdx(params->GetTypeItem(param_index).type_idx_);
Ian Rogers7b078e82014-09-10 14:44:24 -07003624 if (descriptor == nullptr) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003625 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003626 << " missing signature component";
Ian Rogers7b078e82014-09-10 14:44:24 -07003627 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003628 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00003629 const RegType& reg_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003630 uint32_t get_reg = is_range ? inst->VRegC_3rc() + actual_args : arg[actual_args];
Ian Rogers7b078e82014-09-10 14:44:24 -07003631 if (!work_line_->VerifyRegisterType(this, get_reg, reg_type)) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003632 return res_method;
3633 }
3634 actual_args = reg_type.IsLongOrDoubleTypes() ? actual_args + 2 : actual_args + 1;
3635 }
3636 if (actual_args != expected_args) {
3637 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
3638 << " expected " << expected_args << " arguments, found " << actual_args;
Ian Rogers7b078e82014-09-10 14:44:24 -07003639 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003640 } else {
3641 return res_method;
3642 }
3643}
3644
Ian Rogers62342ec2013-06-11 10:26:37 -07003645void MethodVerifier::VerifyNewArray(const Instruction* inst, bool is_filled, bool is_range) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003646 uint32_t type_idx;
3647 if (!is_filled) {
3648 DCHECK_EQ(inst->Opcode(), Instruction::NEW_ARRAY);
3649 type_idx = inst->VRegC_22c();
3650 } else if (!is_range) {
3651 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY);
3652 type_idx = inst->VRegB_35c();
3653 } else {
3654 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY_RANGE);
3655 type_idx = inst->VRegB_3rc();
3656 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00003657 const RegType& res_type = ResolveClassAndCheckAccess(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003658 if (res_type.IsConflict()) { // bad class
3659 DCHECK_NE(failures_.size(), 0U);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003660 } else {
3661 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
3662 if (!res_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003663 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "new-array on non-array class " << res_type;
Ian Rogers0c4a5062012-02-03 15:18:59 -08003664 } else if (!is_filled) {
3665 /* make sure "size" register is valid type */
Ian Rogers7b078e82014-09-10 14:44:24 -07003666 work_line_->VerifyRegisterType(this, inst->VRegB_22c(), reg_types_.Integer());
Ian Rogers0c4a5062012-02-03 15:18:59 -08003667 /* set register type to array class */
Ian Rogersd8f69b02014-09-10 21:43:52 +00003668 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
Ian Rogers7b078e82014-09-10 14:44:24 -07003669 work_line_->SetRegisterType(this, inst->VRegA_22c(), precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003670 } else {
3671 // Verify each register. If "arg_count" is bad, VerifyRegisterType() will run off the end of
3672 // the list and fail. It's legal, if silly, for arg_count to be zero.
Ian Rogersd8f69b02014-09-10 21:43:52 +00003673 const RegType& expected_type = reg_types_.GetComponentType(res_type, GetClassLoader());
Sebastien Hertz5243e912013-05-21 10:55:07 +02003674 uint32_t arg_count = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
3675 uint32_t arg[5];
3676 if (!is_range) {
Ian Rogers29a26482014-05-02 15:27:29 -07003677 inst->GetVarArgs(arg);
Sebastien Hertz5243e912013-05-21 10:55:07 +02003678 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08003679 for (size_t ui = 0; ui < arg_count; ui++) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003680 uint32_t get_reg = is_range ? inst->VRegC_3rc() + ui : arg[ui];
Ian Rogers7b078e82014-09-10 14:44:24 -07003681 if (!work_line_->VerifyRegisterType(this, get_reg, expected_type)) {
3682 work_line_->SetResultRegisterType(this, reg_types_.Conflict());
Ian Rogers0c4a5062012-02-03 15:18:59 -08003683 return;
3684 }
3685 }
3686 // filled-array result goes into "result" register
Ian Rogersd8f69b02014-09-10 21:43:52 +00003687 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
Ian Rogers7b078e82014-09-10 14:44:24 -07003688 work_line_->SetResultRegisterType(this, precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003689 }
3690 }
3691}
3692
Sebastien Hertz5243e912013-05-21 10:55:07 +02003693void MethodVerifier::VerifyAGet(const Instruction* inst,
Ian Rogersd8f69b02014-09-10 21:43:52 +00003694 const RegType& insn_type, bool is_primitive) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003695 const RegType& index_type = work_line_->GetRegisterType(this, inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07003696 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003697 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07003698 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07003699 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegB_23x());
Ian Rogers89310de2012-02-01 13:47:30 -08003700 if (array_type.IsZero()) {
3701 // Null array class; this code path will fail at runtime. Infer a merge-able type from the
3702 // instruction type. TODO: have a proper notion of bottom here.
3703 if (!is_primitive || insn_type.IsCategory1Types()) {
3704 // Reference or category 1
Ian Rogers7b078e82014-09-10 14:44:24 -07003705 work_line_->SetRegisterType(this, inst->VRegA_23x(), reg_types_.Zero());
Ian Rogersd81871c2011-10-03 13:57:23 -07003706 } else {
Ian Rogers89310de2012-02-01 13:47:30 -08003707 // Category 2
Ian Rogers7b078e82014-09-10 14:44:24 -07003708 work_line_->SetRegisterTypeWide(this, inst->VRegA_23x(),
3709 reg_types_.FromCat2ConstLo(0, false),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003710 reg_types_.FromCat2ConstHi(0, false));
Ian Rogers89310de2012-02-01 13:47:30 -08003711 }
jeffhaofc3144e2012-02-01 17:21:15 -08003712 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003713 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aget";
Ian Rogers89310de2012-02-01 13:47:30 -08003714 } else {
3715 /* verify the class */
Ian Rogersd8f69b02014-09-10 21:43:52 +00003716 const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
jeffhaofc3144e2012-02-01 17:21:15 -08003717 if (!component_type.IsReferenceTypes() && !is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07003718 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08003719 << " source for aget-object";
3720 } else if (component_type.IsNonZeroReferenceTypes() && is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07003721 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08003722 << " source for category 1 aget";
3723 } else if (is_primitive && !insn_type.Equals(component_type) &&
3724 !((insn_type.IsInteger() && component_type.IsFloat()) ||
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003725 (insn_type.IsLong() && component_type.IsDouble()))) {
3726 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array type " << array_type
3727 << " incompatible with aget of type " << insn_type;
Ian Rogers89310de2012-02-01 13:47:30 -08003728 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003729 // Use knowledge of the field type which is stronger than the type inferred from the
3730 // instruction, which can't differentiate object types and ints from floats, longs from
3731 // doubles.
3732 if (!component_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003733 work_line_->SetRegisterType(this, inst->VRegA_23x(), component_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003734 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07003735 work_line_->SetRegisterTypeWide(this, inst->VRegA_23x(), component_type,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003736 component_type.HighHalf(&reg_types_));
3737 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003738 }
3739 }
3740 }
3741}
3742
Ian Rogersd8f69b02014-09-10 21:43:52 +00003743void MethodVerifier::VerifyPrimitivePut(const RegType& target_type, const RegType& insn_type,
Jeff Haofe1f7c82013-08-01 14:50:24 -07003744 const uint32_t vregA) {
3745 // Primitive assignability rules are weaker than regular assignability rules.
3746 bool instruction_compatible;
3747 bool value_compatible;
Ian Rogers7b078e82014-09-10 14:44:24 -07003748 const RegType& value_type = work_line_->GetRegisterType(this, vregA);
Jeff Haofe1f7c82013-08-01 14:50:24 -07003749 if (target_type.IsIntegralTypes()) {
Jeff Haoa4647482013-08-06 15:35:47 -07003750 instruction_compatible = target_type.Equals(insn_type);
Jeff Haofe1f7c82013-08-01 14:50:24 -07003751 value_compatible = value_type.IsIntegralTypes();
3752 } else if (target_type.IsFloat()) {
3753 instruction_compatible = insn_type.IsInteger(); // no put-float, so expect put-int
3754 value_compatible = value_type.IsFloatTypes();
3755 } else if (target_type.IsLong()) {
3756 instruction_compatible = insn_type.IsLong();
Andreas Gampe376fa682014-09-07 13:06:12 -07003757 // Additional register check: this is not checked statically (as part of VerifyInstructions),
3758 // as target_type depends on the resolved type of the field.
3759 if (instruction_compatible && work_line_->NumRegs() > vregA + 1) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003760 const RegType& value_type_hi = work_line_->GetRegisterType(this, vregA + 1);
Andreas Gampe376fa682014-09-07 13:06:12 -07003761 value_compatible = value_type.IsLongTypes() && value_type.CheckWidePair(value_type_hi);
3762 } else {
3763 value_compatible = false;
3764 }
Jeff Haofe1f7c82013-08-01 14:50:24 -07003765 } else if (target_type.IsDouble()) {
3766 instruction_compatible = insn_type.IsLong(); // no put-double, so expect put-long
Andreas Gampe376fa682014-09-07 13:06:12 -07003767 // Additional register check: this is not checked statically (as part of VerifyInstructions),
3768 // as target_type depends on the resolved type of the field.
3769 if (instruction_compatible && work_line_->NumRegs() > vregA + 1) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003770 const RegType& value_type_hi = work_line_->GetRegisterType(this, vregA + 1);
Andreas Gampe376fa682014-09-07 13:06:12 -07003771 value_compatible = value_type.IsDoubleTypes() && value_type.CheckWidePair(value_type_hi);
3772 } else {
3773 value_compatible = false;
3774 }
Jeff Haofe1f7c82013-08-01 14:50:24 -07003775 } else {
3776 instruction_compatible = false; // reference with primitive store
3777 value_compatible = false; // unused
3778 }
3779 if (!instruction_compatible) {
3780 // This is a global failure rather than a class change failure as the instructions and
3781 // the descriptors for the type should have been consistent within the same file at
3782 // compile time.
3783 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "put insn has type '" << insn_type
3784 << "' but expected type '" << target_type << "'";
3785 return;
3786 }
3787 if (!value_compatible) {
3788 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
3789 << " of type " << value_type << " but expected " << target_type << " for put";
3790 return;
3791 }
3792}
3793
Sebastien Hertz5243e912013-05-21 10:55:07 +02003794void MethodVerifier::VerifyAPut(const Instruction* inst,
Ian Rogersd8f69b02014-09-10 21:43:52 +00003795 const RegType& insn_type, bool is_primitive) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003796 const RegType& index_type = work_line_->GetRegisterType(this, inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07003797 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003798 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07003799 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07003800 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegB_23x());
Ian Rogers89310de2012-02-01 13:47:30 -08003801 if (array_type.IsZero()) {
3802 // Null array type; this code path will fail at runtime. Infer a merge-able type from the
3803 // instruction type.
jeffhaofc3144e2012-02-01 17:21:15 -08003804 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003805 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aput";
Ian Rogers89310de2012-02-01 13:47:30 -08003806 } else {
Ian Rogersd8f69b02014-09-10 21:43:52 +00003807 const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
Jeff Haofe1f7c82013-08-01 14:50:24 -07003808 const uint32_t vregA = inst->VRegA_23x();
Jeff Haob24b4a72013-07-31 13:47:31 -07003809 if (is_primitive) {
Jeff Haofe1f7c82013-08-01 14:50:24 -07003810 VerifyPrimitivePut(component_type, insn_type, vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07003811 } else {
Jeff Haob24b4a72013-07-31 13:47:31 -07003812 if (!component_type.IsReferenceTypes()) {
3813 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
3814 << " source for aput-object";
3815 } else {
3816 // The instruction agrees with the type of array, confirm the value to be stored does too
3817 // Note: we use the instruction type (rather than the component type) for aput-object as
3818 // incompatible classes will be caught at runtime as an array store exception
Ian Rogers7b078e82014-09-10 14:44:24 -07003819 work_line_->VerifyRegisterType(this, vregA, insn_type);
Jeff Haob24b4a72013-07-31 13:47:31 -07003820 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003821 }
3822 }
3823 }
3824}
3825
Mathieu Chartierc7853442015-03-27 14:35:38 -07003826ArtField* MethodVerifier::GetStaticField(int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08003827 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3828 // Check access to class
Ian Rogersd8f69b02014-09-10 21:43:52 +00003829 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Brian Carlstrom7934ac22013-07-26 10:54:15 -07003830 if (klass_type.IsConflict()) { // bad class
Ian Rogersad0b3a32012-04-16 14:50:24 -07003831 AppendToLastFailMessage(StringPrintf(" in attempt to access static field %d (%s) in %s",
3832 field_idx, dex_file_->GetFieldName(field_id),
3833 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers7b078e82014-09-10 14:44:24 -07003834 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08003835 }
Elliott Hughesb25c3f62012-03-26 16:35:06 -07003836 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003837 return nullptr; // Can't resolve Class so no more to do here, will do checking at runtime.
Ian Rogers90040192011-12-16 08:54:29 -08003838 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07003839 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartierc7853442015-03-27 14:35:38 -07003840 ArtField* field = class_linker->ResolveFieldJLS(*dex_file_, field_idx, dex_cache_,
3841 class_loader_);
Ian Rogers7b078e82014-09-10 14:44:24 -07003842 if (field == nullptr) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07003843 VLOG(verifier) << "Unable to resolve static field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07003844 << dex_file_->GetFieldName(field_id) << ") in "
3845 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogers7b078e82014-09-10 14:44:24 -07003846 DCHECK(self_->IsExceptionPending());
3847 self_->ClearException();
3848 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003849 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
3850 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003851 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access static field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07003852 << " from " << GetDeclaringClass();
Ian Rogers7b078e82014-09-10 14:44:24 -07003853 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003854 } else if (!field->IsStatic()) {
3855 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field) << " to be static";
Ian Rogers7b078e82014-09-10 14:44:24 -07003856 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003857 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07003858 return field;
Ian Rogersd81871c2011-10-03 13:57:23 -07003859}
3860
Mathieu Chartierc7853442015-03-27 14:35:38 -07003861ArtField* MethodVerifier::GetInstanceField(const RegType& obj_type, int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08003862 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3863 // Check access to class
Ian Rogersd8f69b02014-09-10 21:43:52 +00003864 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003865 if (klass_type.IsConflict()) {
3866 AppendToLastFailMessage(StringPrintf(" in attempt to access instance field %d (%s) in %s",
3867 field_idx, dex_file_->GetFieldName(field_id),
3868 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers7b078e82014-09-10 14:44:24 -07003869 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08003870 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003871 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003872 return nullptr; // Can't resolve Class so no more to do here
Ian Rogers90040192011-12-16 08:54:29 -08003873 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07003874 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartierc7853442015-03-27 14:35:38 -07003875 ArtField* field = class_linker->ResolveFieldJLS(*dex_file_, field_idx, dex_cache_,
3876 class_loader_);
Ian Rogers7b078e82014-09-10 14:44:24 -07003877 if (field == nullptr) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07003878 VLOG(verifier) << "Unable to resolve instance field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07003879 << dex_file_->GetFieldName(field_id) << ") in "
3880 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogers7b078e82014-09-10 14:44:24 -07003881 DCHECK(self_->IsExceptionPending());
3882 self_->ClearException();
3883 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003884 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
3885 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003886 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access instance field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07003887 << " from " << GetDeclaringClass();
Ian Rogers7b078e82014-09-10 14:44:24 -07003888 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003889 } else if (field->IsStatic()) {
3890 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field)
3891 << " to not be static";
Ian Rogers7b078e82014-09-10 14:44:24 -07003892 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003893 } else if (obj_type.IsZero()) {
3894 // Cannot infer and check type, however, access will cause null pointer exception
3895 return field;
Stephen Kyle695c5982014-08-22 15:03:07 +01003896 } else if (!obj_type.IsReferenceTypes()) {
3897 // Trying to read a field from something that isn't a reference
3898 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance field access on object that has "
3899 << "non-reference type " << obj_type;
Ian Rogers7b078e82014-09-10 14:44:24 -07003900 return nullptr;
Ian Rogerse1758fe2012-04-19 11:31:15 -07003901 } else {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003902 mirror::Class* klass = field->GetDeclaringClass();
Ian Rogersd8f69b02014-09-10 21:43:52 +00003903 const RegType& field_klass =
Ian Rogers637c65b2013-05-31 11:46:00 -07003904 reg_types_.FromClass(dex_file_->GetFieldDeclaringClassDescriptor(field_id),
Ian Rogers04f94f42013-06-10 15:09:26 -07003905 klass, klass->CannotBeAssignedFromOtherTypes());
Ian Rogersad0b3a32012-04-16 14:50:24 -07003906 if (obj_type.IsUninitializedTypes() &&
3907 (!IsConstructor() || GetDeclaringClass().Equals(obj_type) ||
3908 !field_klass.Equals(GetDeclaringClass()))) {
3909 // Field accesses through uninitialized references are only allowable for constructors where
3910 // the field is declared in this class
3911 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "cannot access instance field " << PrettyField(field)
Brian Carlstrom93c33962013-07-26 10:37:43 -07003912 << " of a not fully initialized object within the context"
3913 << " of " << PrettyMethod(dex_method_idx_, *dex_file_);
Ian Rogers7b078e82014-09-10 14:44:24 -07003914 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003915 } else if (!field_klass.IsAssignableFrom(obj_type)) {
3916 // Trying to access C1.field1 using reference of type C2, which is neither C1 or a sub-class
3917 // of C1. For resolution to occur the declared class of the field must be compatible with
3918 // obj_type, we've discovered this wasn't so, so report the field didn't exist.
3919 Fail(VERIFY_ERROR_NO_FIELD) << "cannot access instance field " << PrettyField(field)
3920 << " from object of type " << obj_type;
Ian Rogers7b078e82014-09-10 14:44:24 -07003921 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003922 } else {
3923 return field;
3924 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003925 }
3926}
3927
Andreas Gampe896df402014-10-20 22:25:29 -07003928template <MethodVerifier::FieldAccessType kAccType>
3929void MethodVerifier::VerifyISFieldAccess(const Instruction* inst, const RegType& insn_type,
3930 bool is_primitive, bool is_static) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003931 uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
Mathieu Chartierc7853442015-03-27 14:35:38 -07003932 ArtField* field;
Ian Rogersb94a27b2011-10-26 00:33:41 -07003933 if (is_static) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07003934 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003935 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07003936 const RegType& object_type = work_line_->GetRegisterType(this, inst->VRegB_22c());
Ian Rogersf4028cc2011-11-02 14:56:39 -07003937 field = GetInstanceField(object_type, field_idx);
Andreas Gampe896df402014-10-20 22:25:29 -07003938 if (UNLIKELY(have_pending_hard_failure_)) {
3939 return;
3940 }
Ian Rogersb94a27b2011-10-26 00:33:41 -07003941 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00003942 const RegType* field_type = nullptr;
Ian Rogers7b078e82014-09-10 14:44:24 -07003943 if (field != nullptr) {
Andreas Gampe896df402014-10-20 22:25:29 -07003944 if (kAccType == FieldAccessType::kAccPut) {
3945 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
3946 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
3947 << " from other class " << GetDeclaringClass();
3948 return;
3949 }
3950 }
3951
Mathieu Chartierc7853442015-03-27 14:35:38 -07003952 mirror::Class* field_type_class =
3953 can_load_classes_ ? field->GetType<true>() : field->GetType<false>();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003954 if (field_type_class != nullptr) {
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07003955 field_type = &reg_types_.FromClass(field->GetTypeDescriptor(), field_type_class,
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003956 field_type_class->CannotBeAssignedFromOtherTypes());
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08003957 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07003958 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
3959 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003960 }
Ian Rogers0d604842012-04-16 14:50:24 -07003961 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003962 if (field_type == nullptr) {
3963 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3964 const char* descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003965 field_type = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003966 }
Sebastien Hertz757b3042014-03-28 14:34:28 +01003967 DCHECK(field_type != nullptr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02003968 const uint32_t vregA = (is_static) ? inst->VRegA_21c() : inst->VRegA_22c();
Andreas Gampe896df402014-10-20 22:25:29 -07003969 static_assert(kAccType == FieldAccessType::kAccPut || kAccType == FieldAccessType::kAccGet,
3970 "Unexpected third access type");
3971 if (kAccType == FieldAccessType::kAccPut) {
3972 // sput or iput.
3973 if (is_primitive) {
3974 VerifyPrimitivePut(*field_type, insn_type, vregA);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003975 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07003976 if (!insn_type.IsAssignableFrom(*field_type)) {
3977 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
3978 << " to be compatible with type '" << insn_type
3979 << "' but found type '" << *field_type
3980 << "' in put-object";
3981 return;
3982 }
3983 work_line_->VerifyRegisterType(this, vregA, *field_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003984 }
Andreas Gampe896df402014-10-20 22:25:29 -07003985 } else if (kAccType == FieldAccessType::kAccGet) {
3986 // sget or iget.
3987 if (is_primitive) {
3988 if (field_type->Equals(insn_type) ||
3989 (field_type->IsFloat() && insn_type.IsInteger()) ||
3990 (field_type->IsDouble() && insn_type.IsLong())) {
3991 // expected that read is of the correct primitive type or that int reads are reading
3992 // floats or long reads are reading doubles
3993 } else {
3994 // This is a global failure rather than a class change failure as the instructions and
3995 // the descriptors for the type should have been consistent within the same file at
3996 // compile time
3997 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
3998 << " to be of type '" << insn_type
3999 << "' but found type '" << *field_type << "' in get";
4000 return;
4001 }
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08004002 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004003 if (!insn_type.IsAssignableFrom(*field_type)) {
4004 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
4005 << " to be compatible with type '" << insn_type
4006 << "' but found type '" << *field_type
4007 << "' in get-object";
4008 work_line_->SetRegisterType(this, vregA, reg_types_.Conflict());
4009 return;
4010 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004011 }
Andreas Gampe896df402014-10-20 22:25:29 -07004012 if (!field_type->IsLowHalf()) {
4013 work_line_->SetRegisterType(this, vregA, *field_type);
4014 } else {
4015 work_line_->SetRegisterTypeWide(this, vregA, *field_type, field_type->HighHalf(&reg_types_));
4016 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07004017 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004018 LOG(FATAL) << "Unexpected case.";
Ian Rogersd81871c2011-10-03 13:57:23 -07004019 }
4020}
4021
Mathieu Chartierc7853442015-03-27 14:35:38 -07004022ArtField* MethodVerifier::GetQuickFieldAccess(const Instruction* inst,
Ian Rogers98379392014-02-24 16:53:16 -08004023 RegisterLine* reg_line) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08004024 DCHECK(IsInstructionIGetQuickOrIPutQuick(inst->Opcode())) << inst->Opcode();
Ian Rogers7b078e82014-09-10 14:44:24 -07004025 const RegType& object_type = reg_line->GetRegisterType(this, inst->VRegB_22c());
Ian Rogers9bc54402014-04-17 16:40:01 -07004026 if (!object_type.HasClass()) {
4027 VLOG(verifier) << "Failed to get mirror::Class* from '" << object_type << "'";
4028 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004029 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004030 uint32_t field_offset = static_cast<uint32_t>(inst->VRegC_22c());
Mathieu Chartierc7853442015-03-27 14:35:38 -07004031 ArtField* const f = ArtField::FindInstanceFieldWithOffset(object_type.GetClass(), field_offset);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08004032 DCHECK_EQ(f->GetOffset().Uint32Value(), field_offset);
Sebastien Hertz479fc1e2014-04-04 17:51:34 +02004033 if (f == nullptr) {
4034 VLOG(verifier) << "Failed to find instance field at offset '" << field_offset
4035 << "' from '" << PrettyDescriptor(object_type.GetClass()) << "'";
4036 }
4037 return f;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004038}
4039
Andreas Gampe896df402014-10-20 22:25:29 -07004040template <MethodVerifier::FieldAccessType kAccType>
4041void MethodVerifier::VerifyQuickFieldAccess(const Instruction* inst, const RegType& insn_type,
4042 bool is_primitive) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07004043 DCHECK(Runtime::Current()->IsStarted() || verify_to_dump_);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004044
Mathieu Chartierc7853442015-03-27 14:35:38 -07004045 ArtField* field = GetQuickFieldAccess(inst, work_line_.get());
Ian Rogers7b078e82014-09-10 14:44:24 -07004046 if (field == nullptr) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004047 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field from " << inst->Name();
4048 return;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004049 }
Andreas Gampe896df402014-10-20 22:25:29 -07004050
4051 // For an IPUT_QUICK, we now test for final flag of the field.
4052 if (kAccType == FieldAccessType::kAccPut) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004053 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
4054 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004055 << " from other class " << GetDeclaringClass();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004056 return;
4057 }
4058 }
Andreas Gampe896df402014-10-20 22:25:29 -07004059
4060 // Get the field type.
4061 const RegType* field_type;
4062 {
Mathieu Chartierc7853442015-03-27 14:35:38 -07004063 mirror::Class* field_type_class = can_load_classes_ ? field->GetType<true>() :
4064 field->GetType<false>();
Andreas Gampe896df402014-10-20 22:25:29 -07004065
4066 if (field_type_class != nullptr) {
4067 field_type = &reg_types_.FromClass(field->GetTypeDescriptor(), field_type_class,
4068 field_type_class->CannotBeAssignedFromOtherTypes());
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004069 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004070 Thread* self = Thread::Current();
4071 DCHECK(!can_load_classes_ || self->IsExceptionPending());
4072 self->ClearException();
4073 field_type = &reg_types_.FromDescriptor(field->GetDeclaringClass()->GetClassLoader(),
4074 field->GetTypeDescriptor(), false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004075 }
Andreas Gampe896df402014-10-20 22:25:29 -07004076 if (field_type == nullptr) {
4077 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field type from " << inst->Name();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004078 return;
4079 }
Andreas Gampe896df402014-10-20 22:25:29 -07004080 }
4081
4082 const uint32_t vregA = inst->VRegA_22c();
4083 static_assert(kAccType == FieldAccessType::kAccPut || kAccType == FieldAccessType::kAccGet,
4084 "Unexpected third access type");
4085 if (kAccType == FieldAccessType::kAccPut) {
4086 if (is_primitive) {
4087 // Primitive field assignability rules are weaker than regular assignability rules
4088 bool instruction_compatible;
4089 bool value_compatible;
4090 const RegType& value_type = work_line_->GetRegisterType(this, vregA);
4091 if (field_type->IsIntegralTypes()) {
4092 instruction_compatible = insn_type.IsIntegralTypes();
4093 value_compatible = value_type.IsIntegralTypes();
4094 } else if (field_type->IsFloat()) {
4095 instruction_compatible = insn_type.IsInteger(); // no [is]put-float, so expect [is]put-int
4096 value_compatible = value_type.IsFloatTypes();
4097 } else if (field_type->IsLong()) {
4098 instruction_compatible = insn_type.IsLong();
4099 value_compatible = value_type.IsLongTypes();
4100 } else if (field_type->IsDouble()) {
4101 instruction_compatible = insn_type.IsLong(); // no [is]put-double, so expect [is]put-long
4102 value_compatible = value_type.IsDoubleTypes();
4103 } else {
4104 instruction_compatible = false; // reference field with primitive store
4105 value_compatible = false; // unused
4106 }
4107 if (!instruction_compatible) {
4108 // This is a global failure rather than a class change failure as the instructions and
4109 // the descriptors for the type should have been consistent within the same file at
4110 // compile time
4111 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
4112 << " to be of type '" << insn_type
4113 << "' but found type '" << *field_type
4114 << "' in put";
4115 return;
4116 }
4117 if (!value_compatible) {
4118 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
4119 << " of type " << value_type
4120 << " but expected " << *field_type
4121 << " for store to " << PrettyField(field) << " in put";
4122 return;
4123 }
4124 } else {
4125 if (!insn_type.IsAssignableFrom(*field_type)) {
4126 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
4127 << " to be compatible with type '" << insn_type
4128 << "' but found type '" << *field_type
4129 << "' in put-object";
4130 return;
4131 }
4132 work_line_->VerifyRegisterType(this, vregA, *field_type);
4133 }
4134 } else if (kAccType == FieldAccessType::kAccGet) {
4135 if (is_primitive) {
4136 if (field_type->Equals(insn_type) ||
4137 (field_type->IsFloat() && insn_type.IsIntegralTypes()) ||
4138 (field_type->IsDouble() && insn_type.IsLongTypes())) {
4139 // expected that read is of the correct primitive type or that int reads are reading
4140 // floats or long reads are reading doubles
4141 } else {
4142 // This is a global failure rather than a class change failure as the instructions and
4143 // the descriptors for the type should have been consistent within the same file at
4144 // compile time
4145 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
4146 << " to be of type '" << insn_type
4147 << "' but found type '" << *field_type << "' in Get";
4148 return;
4149 }
4150 } else {
4151 if (!insn_type.IsAssignableFrom(*field_type)) {
4152 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
4153 << " to be compatible with type '" << insn_type
4154 << "' but found type '" << *field_type
4155 << "' in get-object";
4156 work_line_->SetRegisterType(this, vregA, reg_types_.Conflict());
4157 return;
4158 }
4159 }
4160 if (!field_type->IsLowHalf()) {
4161 work_line_->SetRegisterType(this, vregA, *field_type);
4162 } else {
4163 work_line_->SetRegisterTypeWide(this, vregA, *field_type, field_type->HighHalf(&reg_types_));
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004164 }
4165 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004166 LOG(FATAL) << "Unexpected case.";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004167 }
4168}
4169
Ian Rogers776ac1f2012-04-13 23:36:36 -07004170bool MethodVerifier::CheckNotMoveException(const uint16_t* insns, int insn_idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004171 if ((insns[insn_idx] & 0xff) == Instruction::MOVE_EXCEPTION) {
jeffhaod5347e02012-03-22 17:25:05 -07004172 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-exception";
Ian Rogersd81871c2011-10-03 13:57:23 -07004173 return false;
4174 }
4175 return true;
4176}
4177
Stephen Kyle9bc61992014-09-22 13:53:15 +01004178bool MethodVerifier::CheckNotMoveResult(const uint16_t* insns, int insn_idx) {
4179 if (((insns[insn_idx] & 0xff) >= Instruction::MOVE_RESULT) &&
4180 ((insns[insn_idx] & 0xff) <= Instruction::MOVE_RESULT_OBJECT)) {
4181 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-result*";
4182 return false;
4183 }
4184 return true;
4185}
4186
4187bool MethodVerifier::CheckNotMoveExceptionOrMoveResult(const uint16_t* insns, int insn_idx) {
4188 return (CheckNotMoveException(insns, insn_idx) && CheckNotMoveResult(insns, insn_idx));
4189}
4190
Ian Rogersebbdd872014-07-07 23:53:08 -07004191bool MethodVerifier::UpdateRegisters(uint32_t next_insn, RegisterLine* merge_line,
4192 bool update_merge_line) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004193 bool changed = true;
4194 RegisterLine* target_line = reg_table_.GetLine(next_insn);
4195 if (!insn_flags_[next_insn].IsVisitedOrChanged()) {
jeffhaobdb76512011-09-07 11:43:16 -07004196 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07004197 * We haven't processed this instruction before, and we haven't touched the registers here, so
4198 * there's nothing to "merge". Copy the registers over and mark it as changed. (This is the
4199 * only way a register can transition out of "unknown", so this is not just an optimization.)
jeffhaobdb76512011-09-07 11:43:16 -07004200 */
Ian Rogersb8c78592013-07-25 23:52:52 +00004201 if (!insn_flags_[next_insn].IsReturn()) {
4202 target_line->CopyFromLine(merge_line);
4203 } else {
Jeff Haob24b4a72013-07-31 13:47:31 -07004204 // Verify that the monitor stack is empty on return.
Ian Rogers7b078e82014-09-10 14:44:24 -07004205 if (!merge_line->VerifyMonitorStackEmpty(this)) {
Jeff Haob24b4a72013-07-31 13:47:31 -07004206 return false;
4207 }
Ian Rogersb8c78592013-07-25 23:52:52 +00004208 // For returns we only care about the operand to the return, all other registers are dead.
4209 // Initialize them as conflicts so they don't add to GC and deoptimization information.
4210 const Instruction* ret_inst = Instruction::At(code_item_->insns_ + next_insn);
4211 Instruction::Code opcode = ret_inst->Opcode();
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07004212 if (opcode == Instruction::RETURN_VOID || opcode == Instruction::RETURN_VOID_NO_BARRIER) {
Stephen Kyle7e541c92014-12-17 17:10:02 +00004213 SafelyMarkAllRegistersAsConflicts(this, target_line);
Ian Rogersb8c78592013-07-25 23:52:52 +00004214 } else {
4215 target_line->CopyFromLine(merge_line);
4216 if (opcode == Instruction::RETURN_WIDE) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004217 target_line->MarkAllRegistersAsConflictsExceptWide(this, ret_inst->VRegA_11x());
Ian Rogersb8c78592013-07-25 23:52:52 +00004218 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004219 target_line->MarkAllRegistersAsConflictsExcept(this, ret_inst->VRegA_11x());
Ian Rogersb8c78592013-07-25 23:52:52 +00004220 }
4221 }
4222 }
jeffhaobdb76512011-09-07 11:43:16 -07004223 } else {
Ian Rogers700a4022014-05-19 16:49:03 -07004224 std::unique_ptr<RegisterLine> copy(gDebugVerify ?
Ian Rogersd0fbd852013-09-24 18:17:04 -07004225 RegisterLine::Create(target_line->NumRegs(), this) :
Ian Rogers7b078e82014-09-10 14:44:24 -07004226 nullptr);
Ian Rogers7b0c5b42012-02-16 15:29:07 -08004227 if (gDebugVerify) {
4228 copy->CopyFromLine(target_line);
4229 }
Ian Rogers7b078e82014-09-10 14:44:24 -07004230 changed = target_line->MergeRegisters(this, merge_line);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004231 if (have_pending_hard_failure_) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004232 return false;
jeffhaobdb76512011-09-07 11:43:16 -07004233 }
Ian Rogers2c8a8572011-10-24 17:11:36 -07004234 if (gDebugVerify && changed) {
Elliott Hughes398f64b2012-03-26 18:05:48 -07004235 LogVerifyInfo() << "Merging at [" << reinterpret_cast<void*>(work_insn_idx_) << "]"
Elliott Hughesc073b072012-05-24 19:29:17 -07004236 << " to [" << reinterpret_cast<void*>(next_insn) << "]: " << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07004237 << copy->Dump(this) << " MERGE\n"
4238 << merge_line->Dump(this) << " ==\n"
4239 << target_line->Dump(this) << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07004240 }
Ian Rogersebbdd872014-07-07 23:53:08 -07004241 if (update_merge_line && changed) {
4242 merge_line->CopyFromLine(target_line);
4243 }
jeffhaobdb76512011-09-07 11:43:16 -07004244 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004245 if (changed) {
4246 insn_flags_[next_insn].SetChanged();
jeffhaobdb76512011-09-07 11:43:16 -07004247 }
4248 return true;
4249}
4250
Ian Rogers7b3ddd22013-02-21 15:19:52 -08004251InstructionFlags* MethodVerifier::CurrentInsnFlags() {
Ian Rogers776ac1f2012-04-13 23:36:36 -07004252 return &insn_flags_[work_insn_idx_];
4253}
4254
Ian Rogersd8f69b02014-09-10 21:43:52 +00004255const RegType& MethodVerifier::GetMethodReturnType() {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004256 if (return_type_ == nullptr) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07004257 if (mirror_method_ != nullptr) {
Ian Rogersded66a02014-10-28 18:12:55 -07004258 mirror::Class* return_type_class = mirror_method_->GetReturnType(can_load_classes_);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004259 if (return_type_class != nullptr) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07004260 return_type_ = &reg_types_.FromClass(mirror_method_->GetReturnTypeDescriptor(),
4261 return_type_class,
Mathieu Chartier590fee92013-09-13 13:46:47 -07004262 return_type_class->CannotBeAssignedFromOtherTypes());
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004263 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004264 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
4265 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004266 }
4267 }
4268 if (return_type_ == nullptr) {
4269 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
4270 const DexFile::ProtoId& proto_id = dex_file_->GetMethodPrototype(method_id);
4271 uint16_t return_type_idx = proto_id.return_type_idx_;
4272 const char* descriptor = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(return_type_idx));
Mathieu Chartierbf99f772014-08-23 16:37:27 -07004273 return_type_ = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004274 }
4275 }
4276 return *return_type_;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004277}
4278
Ian Rogersd8f69b02014-09-10 21:43:52 +00004279const RegType& MethodVerifier::GetDeclaringClass() {
Ian Rogers7b078e82014-09-10 14:44:24 -07004280 if (declaring_class_ == nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004281 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
Brian Carlstrom93c33962013-07-26 10:37:43 -07004282 const char* descriptor
4283 = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(method_id.class_idx_));
Mathieu Chartiere401d142015-04-22 13:56:20 -07004284 if (mirror_method_ != nullptr) {
Ian Rogers637c65b2013-05-31 11:46:00 -07004285 mirror::Class* klass = mirror_method_->GetDeclaringClass();
Ian Rogers04f94f42013-06-10 15:09:26 -07004286 declaring_class_ = &reg_types_.FromClass(descriptor, klass,
4287 klass->CannotBeAssignedFromOtherTypes());
Ian Rogers637c65b2013-05-31 11:46:00 -07004288 } else {
Mathieu Chartierbf99f772014-08-23 16:37:27 -07004289 declaring_class_ = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers637c65b2013-05-31 11:46:00 -07004290 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07004291 }
Ian Rogers637c65b2013-05-31 11:46:00 -07004292 return *declaring_class_;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004293}
4294
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004295std::vector<int32_t> MethodVerifier::DescribeVRegs(uint32_t dex_pc) {
4296 RegisterLine* line = reg_table_.GetLine(dex_pc);
Sebastien Hertzaa0c00c2014-03-14 17:58:54 +01004297 DCHECK(line != nullptr) << "No register line at DEX pc " << StringPrintf("0x%x", dex_pc);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004298 std::vector<int32_t> result;
4299 for (size_t i = 0; i < line->NumRegs(); ++i) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004300 const RegType& type = line->GetRegisterType(this, i);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004301 if (type.IsConstant()) {
4302 result.push_back(type.IsPreciseConstant() ? kConstant : kImpreciseConstant);
Ian Rogers7b078e82014-09-10 14:44:24 -07004303 const ConstantType* const_val = down_cast<const ConstantType*>(&type);
4304 result.push_back(const_val->ConstantValue());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004305 } else if (type.IsConstantLo()) {
4306 result.push_back(type.IsPreciseConstantLo() ? kConstant : kImpreciseConstant);
Ian Rogers7b078e82014-09-10 14:44:24 -07004307 const ConstantType* const_val = down_cast<const ConstantType*>(&type);
4308 result.push_back(const_val->ConstantValueLo());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004309 } else if (type.IsConstantHi()) {
4310 result.push_back(type.IsPreciseConstantHi() ? kConstant : kImpreciseConstant);
Ian Rogers7b078e82014-09-10 14:44:24 -07004311 const ConstantType* const_val = down_cast<const ConstantType*>(&type);
4312 result.push_back(const_val->ConstantValueHi());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004313 } else if (type.IsIntegralTypes()) {
4314 result.push_back(kIntVReg);
4315 result.push_back(0);
4316 } else if (type.IsFloat()) {
4317 result.push_back(kFloatVReg);
4318 result.push_back(0);
4319 } else if (type.IsLong()) {
4320 result.push_back(kLongLoVReg);
4321 result.push_back(0);
4322 result.push_back(kLongHiVReg);
4323 result.push_back(0);
4324 ++i;
4325 } else if (type.IsDouble()) {
4326 result.push_back(kDoubleLoVReg);
4327 result.push_back(0);
4328 result.push_back(kDoubleHiVReg);
4329 result.push_back(0);
4330 ++i;
4331 } else if (type.IsUndefined() || type.IsConflict() || type.IsHighHalf()) {
4332 result.push_back(kUndefined);
4333 result.push_back(0);
4334 } else {
Ian Rogers7b3ddd22013-02-21 15:19:52 -08004335 CHECK(type.IsNonZeroReferenceTypes());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004336 result.push_back(kReferenceVReg);
4337 result.push_back(0);
4338 }
4339 }
4340 return result;
4341}
4342
Ian Rogersd8f69b02014-09-10 21:43:52 +00004343const RegType& MethodVerifier::DetermineCat1Constant(int32_t value, bool precise) {
Sebastien Hertz849600b2013-12-20 10:28:08 +01004344 if (precise) {
4345 // Precise constant type.
4346 return reg_types_.FromCat1Const(value, true);
4347 } else {
4348 // Imprecise constant type.
4349 if (value < -32768) {
4350 return reg_types_.IntConstant();
4351 } else if (value < -128) {
4352 return reg_types_.ShortConstant();
4353 } else if (value < 0) {
4354 return reg_types_.ByteConstant();
4355 } else if (value == 0) {
4356 return reg_types_.Zero();
4357 } else if (value == 1) {
4358 return reg_types_.One();
4359 } else if (value < 128) {
4360 return reg_types_.PosByteConstant();
4361 } else if (value < 32768) {
4362 return reg_types_.PosShortConstant();
4363 } else if (value < 65536) {
4364 return reg_types_.CharConstant();
4365 } else {
4366 return reg_types_.IntConstant();
4367 }
4368 }
4369}
4370
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004371void MethodVerifier::Init() {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08004372 art::verifier::RegTypeCache::Init();
Brian Carlstrome7d856b2012-01-11 18:10:55 -08004373}
4374
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004375void MethodVerifier::Shutdown() {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08004376 verifier::RegTypeCache::ShutDown();
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08004377}
jeffhaod1224c72012-02-29 13:43:08 -08004378
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -07004379void MethodVerifier::VisitStaticRoots(RootVisitor* visitor) {
4380 RegTypeCache::VisitStaticRoots(visitor);
Mathieu Chartier7c438b12014-09-12 17:01:24 -07004381}
4382
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -07004383void MethodVerifier::VisitRoots(RootVisitor* visitor, const RootInfo& root_info) {
4384 reg_types_.VisitRoots(visitor, root_info);
Mathieu Chartierc528dba2013-11-26 12:00:11 -08004385}
4386
Ian Rogersd81871c2011-10-03 13:57:23 -07004387} // namespace verifier
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07004388} // namespace art