blob: fae3af31d5463c368eacdfdae350441f786376d4 [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
Andreas Gampe4a57d5f2019-04-03 12:31:18 -070019#include <ostream>
Elliott Hughes1f359b02011-07-17 14:27:17 -070020
Andreas Gampe46ee31b2016-12-14 10:11:49 -080021#include "android-base/stringprintf.h"
22
Mathieu Chartierc7853442015-03-27 14:35:38 -070023#include "art_field-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070024#include "art_method-inl.h"
Andreas Gampe39b378c2017-12-07 15:44:13 -080025#include "base/aborting.h"
Andreas Gampe542451c2016-07-26 09:02:02 -070026#include "base/enums.h"
David Sehr67bf42e2018-02-26 16:43:04 -080027#include "base/leb128.h"
David Sehr9c4a0152018-04-05 12:23:54 -070028#include "base/indenter.h"
Andreas Gampe57943812017-12-06 21:39:13 -080029#include "base/logging.h" // For VLOG.
Ian Rogers637c65b2013-05-31 11:46:00 -070030#include "base/mutex-inl.h"
David Brazdil2bb2fbd2018-11-13 18:24:26 +000031#include "base/sdk_version.h"
Vladimir Marko637ee0b2015-09-04 12:47:41 +010032#include "base/stl_util.h"
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -080033#include "base/systrace.h"
Vladimir Marko80afd022015-05-19 18:08:00 +010034#include "base/time_utils.h"
David Sehrc431b9d2018-03-02 12:01:51 -080035#include "base/utils.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070036#include "class_linker.h"
Vladimir Markoc7aa87e2018-05-24 15:19:52 +010037#include "class_root.h"
Vladimir Marko2b5eaa22013-12-13 13:59:30 +000038#include "compiler_callbacks.h"
Mathieu Chartier1f1cb9f2018-06-04 09:22:46 -070039#include "dex/class_accessor-inl.h"
David Sehrb2ec9f52018-02-21 13:20:31 -080040#include "dex/descriptors_names.h"
David Sehr9e734c72018-01-04 17:56:19 -080041#include "dex/dex_file-inl.h"
42#include "dex/dex_file_exception_helpers.h"
43#include "dex/dex_instruction-inl.h"
44#include "dex/dex_instruction_utils.h"
Alex Lighteb7c1442015-08-31 13:17:42 -070045#include "experimental_flags.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070046#include "gc/accounting/card_table-inl.h"
Andreas Gampe6d7abbd2017-04-24 13:19:09 -070047#include "handle_scope-inl.h"
Ian Rogers84fa0742011-10-25 18:13:30 -070048#include "intern_table.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080049#include "mirror/class-inl.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070050#include "mirror/class.h"
Ian Rogers39ebcb82013-05-30 16:57:23 -070051#include "mirror/dex_cache-inl.h"
Orion Hodsoncfa325e2016-10-13 10:25:54 +010052#include "mirror/method_handle_impl.h"
Orion Hodson2e599942017-09-22 16:17:41 +010053#include "mirror/method_type.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080054#include "mirror/object-inl.h"
55#include "mirror/object_array-inl.h"
Orion Hodsonfe92d122018-01-02 10:45:17 +000056#include "mirror/var_handle.h"
Ian Rogers7b078e82014-09-10 14:44:24 -070057#include "reg_type-inl.h"
Ian Rogers39ebcb82013-05-30 16:57:23 -070058#include "register_line-inl.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070059#include "runtime.h"
Andreas Gampe2ad6cce2019-04-11 16:17:39 -070060#include "scoped_newline.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070061#include "scoped_thread_state_change-inl.h"
Andreas Gampe513061a2017-06-01 09:17:34 -070062#include "stack.h"
Nicolas Geoffrayb041a402017-11-13 15:16:22 +000063#include "vdex_file.h"
Andreas Gampe6d7abbd2017-04-24 13:19:09 -070064#include "verifier_compiler_binding.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070065#include "verifier_deps.h"
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070066
67namespace art {
Ian Rogersd81871c2011-10-03 13:57:23 -070068namespace verifier {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070069
Andreas Gampe46ee31b2016-12-14 10:11:49 -080070using android::base::StringPrintf;
71
Mathieu Chartier8e219ae2014-08-19 14:29:46 -070072static constexpr bool kTimeVerifyMethod = !kIsDebugBuild;
Ian Rogers2c8a8572011-10-24 17:11:36 -070073
Andreas Gampeebf850c2015-08-14 15:37:35 -070074// On VLOG(verifier), should we dump the whole state when we run into a hard failure?
75static constexpr bool kDumpRegLinesOnHardFailureIfVLOG = true;
76
Vladimir Marko69d310e2017-10-09 14:12:23 +010077PcToRegisterLineTable::PcToRegisterLineTable(ScopedArenaAllocator& allocator)
78 : register_lines_(allocator.Adapter(kArenaAllocVerifier)) {}
Mathieu Chartierde40d472015-10-15 17:47:48 -070079
Ian Rogers7b3ddd22013-02-21 15:19:52 -080080void PcToRegisterLineTable::Init(RegisterTrackingMode mode, InstructionFlags* flags,
Ian Rogersd81871c2011-10-03 13:57:23 -070081 uint32_t insns_size, uint16_t registers_size,
Ian Rogers776ac1f2012-04-13 23:36:36 -070082 MethodVerifier* verifier) {
Ian Rogersd81871c2011-10-03 13:57:23 -070083 DCHECK_GT(insns_size, 0U);
Mathieu Chartierde40d472015-10-15 17:47:48 -070084 register_lines_.resize(insns_size);
Ian Rogersd81871c2011-10-03 13:57:23 -070085 for (uint32_t i = 0; i < insns_size; i++) {
86 bool interesting = false;
87 switch (mode) {
88 case kTrackRegsAll:
89 interesting = flags[i].IsOpcode();
90 break;
Sameer Abu Asal02c42232013-04-30 12:09:45 -070091 case kTrackCompilerInterestPoints:
Brian Carlstrom02c8cc62013-07-18 15:54:44 -070092 interesting = flags[i].IsCompileTimeInfoPoint() || flags[i].IsBranchTarget();
Ian Rogersd81871c2011-10-03 13:57:23 -070093 break;
94 case kTrackRegsBranches:
95 interesting = flags[i].IsBranchTarget();
96 break;
97 default:
98 break;
99 }
100 if (interesting) {
Mathieu Chartierde40d472015-10-15 17:47:48 -0700101 register_lines_[i].reset(RegisterLine::Create(registers_size, verifier));
Ian Rogersd0fbd852013-09-24 18:17:04 -0700102 }
103 }
104}
105
Mathieu Chartierde40d472015-10-15 17:47:48 -0700106PcToRegisterLineTable::~PcToRegisterLineTable() {}
Ian Rogersd81871c2011-10-03 13:57:23 -0700107
Andreas Gampe7c038102014-10-27 20:08:46 -0700108// Note: returns true on failure.
Mathieu Chartierc2a2bda2018-03-12 11:21:52 -0700109inline bool MethodVerifier::FailOrAbort(bool condition,
110 const char* error_msg,
111 uint32_t work_insn_idx) {
Andreas Gampe7c038102014-10-27 20:08:46 -0700112 if (kIsDebugBuild) {
Andreas Gampef8f36c22016-11-19 14:51:52 -0800113 // In a debug build, abort if the error condition is wrong. Only warn if
114 // we are already aborting (as this verification is likely run to print
115 // lock information).
116 if (LIKELY(gAborting == 0)) {
Mathieu Chartierc2a2bda2018-03-12 11:21:52 -0700117 DCHECK(condition) << error_msg << work_insn_idx << " "
118 << dex_file_->PrettyMethod(dex_method_idx_);
Andreas Gampef8f36c22016-11-19 14:51:52 -0800119 } else {
120 if (!condition) {
121 LOG(ERROR) << error_msg << work_insn_idx;
Mathieu Chartierc2a2bda2018-03-12 11:21:52 -0700122 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << error_msg << work_insn_idx;
Andreas Gampef8f36c22016-11-19 14:51:52 -0800123 return true;
124 }
125 }
Andreas Gampe7c038102014-10-27 20:08:46 -0700126 } else {
127 // In a non-debug build, just fail the class.
128 if (!condition) {
Mathieu Chartierc2a2bda2018-03-12 11:21:52 -0700129 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << error_msg << work_insn_idx;
Andreas Gampe7c038102014-10-27 20:08:46 -0700130 return true;
131 }
132 }
133
134 return false;
135}
136
Stephen Kyle7e541c92014-12-17 17:10:02 +0000137static void SafelyMarkAllRegistersAsConflicts(MethodVerifier* verifier, RegisterLine* reg_line) {
Andreas Gampef10b6e12015-08-12 10:48:12 -0700138 if (verifier->IsInstanceConstructor()) {
Stephen Kyle7e541c92014-12-17 17:10:02 +0000139 // Before we mark all regs as conflicts, check that we don't have an uninitialized this.
140 reg_line->CheckConstructorReturn(verifier);
141 }
142 reg_line->MarkAllRegistersAsConflicts(verifier);
143}
144
Andreas Gampe6d7abbd2017-04-24 13:19:09 -0700145static FailureKind FailureKindMax(FailureKind fk1, FailureKind fk2) {
146 static_assert(FailureKind::kNoFailure < FailureKind::kSoftFailure
147 && FailureKind::kSoftFailure < FailureKind::kHardFailure,
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800148 "Unexpected FailureKind order");
149 return std::max(fk1, fk2);
150}
151
152void MethodVerifier::FailureData::Merge(const MethodVerifier::FailureData& fd) {
153 kind = FailureKindMax(kind, fd.kind);
154 types |= fd.types;
155}
156
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800157static bool IsLargeMethod(const CodeItemDataAccessor& accessor) {
158 if (!accessor.HasCodeItem()) {
Andreas Gampe3c651fc2015-05-21 14:06:46 -0700159 return false;
160 }
161
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800162 uint16_t registers_size = accessor.RegistersSize();
163 uint32_t insns_size = accessor.InsnsSizeInCodeUnits();
Andreas Gampea4f5bf62015-05-18 20:50:12 -0700164
165 return registers_size * insns_size > 4*1024*1024;
166}
167
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800168MethodVerifier::FailureData MethodVerifier::VerifyMethod(Thread* self,
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800169 uint32_t method_idx,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800170 const DexFile* dex_file,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700171 Handle<mirror::DexCache> dex_cache,
172 Handle<mirror::ClassLoader> class_loader,
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800173 const dex::ClassDef& class_def,
174 const dex::CodeItem* code_item,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700175 ArtMethod* method,
Jeff Haoee988952013-04-16 14:23:47 -0700176 uint32_t method_access_flags,
Andreas Gampe53e32d12015-12-09 21:03:23 -0800177 CompilerCallbacks* callbacks,
Ian Rogers46960fe2014-05-23 10:43:43 -0700178 bool allow_soft_failures,
Andreas Gampe5fd66d02016-09-12 20:22:19 -0700179 HardFailLogMode log_level,
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800180 bool need_precise_constants,
Andreas Gampe6cc23ac2018-08-24 15:22:43 -0700181 uint32_t api_level,
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800182 std::string* hard_failure_msg) {
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800183 MethodVerifier::FailureData result;
Mathieu Chartier8e219ae2014-08-19 14:29:46 -0700184 uint64_t start_ns = kTimeVerifyMethod ? NanoTime() : 0;
Ian Rogersc8982582012-09-07 16:53:25 -0700185
Andreas Gampebf9611f2016-03-25 16:58:00 -0700186 MethodVerifier verifier(self,
187 dex_file,
188 dex_cache,
189 class_loader,
190 class_def,
191 code_item,
192 method_idx,
193 method,
194 method_access_flags,
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700195 /* can_load_classes= */ true,
Andreas Gampebf9611f2016-03-25 16:58:00 -0700196 allow_soft_failures,
197 need_precise_constants,
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700198 /* verify to dump */ false,
199 /* allow_thread_suspension= */ true,
Andreas Gampe6cc23ac2018-08-24 15:22:43 -0700200 api_level);
Ian Rogers46960fe2014-05-23 10:43:43 -0700201 if (verifier.Verify()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700202 // Verification completed, however failures may be pending that didn't cause the verification
203 // to hard fail.
Ian Rogers46960fe2014-05-23 10:43:43 -0700204 CHECK(!verifier.have_pending_hard_failure_);
Andreas Gampe53e32d12015-12-09 21:03:23 -0800205
206 if (code_item != nullptr && callbacks != nullptr) {
207 // Let the interested party know that the method was verified.
208 callbacks->MethodVerified(&verifier);
209 }
210
Ian Rogers46960fe2014-05-23 10:43:43 -0700211 if (verifier.failures_.size() != 0) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -0700212 if (VLOG_IS_ON(verifier)) {
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800213 verifier.DumpFailures(VLOG_STREAM(verifier) << "Soft verification failures in "
David Sehr709b0702016-10-13 09:12:37 -0700214 << dex_file->PrettyMethod(method_idx) << "\n");
Anwar Ghuloum75a43f12013-08-13 17:22:14 -0700215 }
Andreas Gampe92d77202017-12-06 20:49:00 -0800216 if (VLOG_IS_ON(verifier_debug)) {
Andreas Gampe4a57d5f2019-04-03 12:31:18 -0700217 LOG(INFO) << verifier.info_messages_.str();
218 verifier.Dump(LOG_STREAM(INFO));
Andreas Gampe92d77202017-12-06 20:49:00 -0800219 }
Andreas Gampe6d7abbd2017-04-24 13:19:09 -0700220 result.kind = FailureKind::kSoftFailure;
Nicolas Geoffray250a3782016-04-20 16:27:53 +0100221 if (method != nullptr &&
222 !CanCompilerHandleVerificationFailure(verifier.encountered_failure_types_)) {
Alex Lightfcbafb32017-02-02 15:09:54 -0800223 method->SetDontCompile();
Nicolas Geoffray250a3782016-04-20 16:27:53 +0100224 }
225 }
Andreas Gampe56fdd0e2016-04-28 14:56:54 -0700226 if (method != nullptr) {
227 if (verifier.HasInstructionThatWillThrow()) {
Alex Lightfcbafb32017-02-02 15:09:54 -0800228 method->SetDontCompile();
Calin Juravle857f0582016-12-20 14:36:59 +0000229 if (Runtime::Current()->IsAotCompiler() &&
230 (callbacks != nullptr) && !callbacks->IsBootImage()) {
231 // When compiling apps, make HasInstructionThatWillThrow a soft error to trigger
232 // re-verification at runtime.
233 // The dead code after the throw is not verified and might be invalid. This may cause
234 // the JIT compiler to crash since it assumes that all the code is valid.
235 //
236 // There's a strong assumption that the entire boot image is verified and all its dex
237 // code is valid (even the dead and unverified one). As such this is done only for apps.
238 // (CompilerDriver DCHECKs in VerifyClassVisitor that methods from boot image are
239 // fully verified).
Andreas Gampe6d7abbd2017-04-24 13:19:09 -0700240 result.kind = FailureKind::kSoftFailure;
Calin Juravle857f0582016-12-20 14:36:59 +0000241 }
Andreas Gampe56fdd0e2016-04-28 14:56:54 -0700242 }
243 if ((verifier.encountered_failure_types_ & VerifyError::VERIFY_ERROR_LOCKING) != 0) {
Orion Hodsoncfcc9cf2017-09-29 15:07:27 +0100244 method->SetMustCountLocks();
Andreas Gampe56fdd0e2016-04-28 14:56:54 -0700245 }
jeffhaof56197c2012-03-05 18:01:54 -0800246 }
247 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700248 // Bad method data.
Ian Rogers46960fe2014-05-23 10:43:43 -0700249 CHECK_NE(verifier.failures_.size(), 0U);
Igor Murashkin4d7b75f2015-07-21 17:03:36 -0700250
251 if (UNLIKELY(verifier.have_pending_experimental_failure_)) {
252 // Failed due to being forced into interpreter. This is ok because
253 // we just want to skip verification.
Andreas Gampe6d7abbd2017-04-24 13:19:09 -0700254 result.kind = FailureKind::kSoftFailure;
Igor Murashkin4d7b75f2015-07-21 17:03:36 -0700255 } else {
256 CHECK(verifier.have_pending_hard_failure_);
Andreas Gampe7fe30232016-03-25 16:58:00 -0700257 if (VLOG_IS_ON(verifier)) {
Andreas Gampe5fd66d02016-09-12 20:22:19 -0700258 log_level = std::max(HardFailLogMode::kLogVerbose, log_level);
Andreas Gampe7fe30232016-03-25 16:58:00 -0700259 }
Andreas Gampe5fd66d02016-09-12 20:22:19 -0700260 if (log_level >= HardFailLogMode::kLogVerbose) {
261 LogSeverity severity;
262 switch (log_level) {
263 case HardFailLogMode::kLogVerbose:
264 severity = LogSeverity::VERBOSE;
265 break;
266 case HardFailLogMode::kLogWarning:
267 severity = LogSeverity::WARNING;
268 break;
269 case HardFailLogMode::kLogInternalFatal:
Andreas Gampe3fec9ac2016-09-13 10:47:28 -0700270 severity = LogSeverity::FATAL_WITHOUT_ABORT;
Andreas Gampe5fd66d02016-09-12 20:22:19 -0700271 break;
272 default:
273 LOG(FATAL) << "Unsupported log-level " << static_cast<uint32_t>(log_level);
274 UNREACHABLE();
275 }
Andreas Gampe3fec9ac2016-09-13 10:47:28 -0700276 verifier.DumpFailures(LOG_STREAM(severity) << "Verification error in "
David Sehr709b0702016-10-13 09:12:37 -0700277 << dex_file->PrettyMethod(method_idx)
Andreas Gampe3fec9ac2016-09-13 10:47:28 -0700278 << "\n");
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800279 }
280 if (hard_failure_msg != nullptr) {
281 CHECK(!verifier.failure_messages_.empty());
282 *hard_failure_msg =
283 verifier.failure_messages_[verifier.failure_messages_.size() - 1]->str();
284 }
Andreas Gampe6d7abbd2017-04-24 13:19:09 -0700285 result.kind = FailureKind::kHardFailure;
Andreas Gampe53e32d12015-12-09 21:03:23 -0800286
287 if (callbacks != nullptr) {
288 // Let the interested party know that we failed the class.
David Brazdil15fc7292016-09-02 14:13:18 +0100289 ClassReference ref(dex_file, dex_file->GetIndexForClassDef(class_def));
Andreas Gampe53e32d12015-12-09 21:03:23 -0800290 callbacks->ClassRejected(ref);
291 }
Igor Murashkin4d7b75f2015-07-21 17:03:36 -0700292 }
Andreas Gampe92d77202017-12-06 20:49:00 -0800293 if (VLOG_IS_ON(verifier) || VLOG_IS_ON(verifier_debug)) {
Andreas Gampe4a57d5f2019-04-03 12:31:18 -0700294 LOG(ERROR) << verifier.info_messages_.str();
295 verifier.Dump(LOG_STREAM(ERROR));
jeffhaof56197c2012-03-05 18:01:54 -0800296 }
jeffhaof56197c2012-03-05 18:01:54 -0800297 }
Mathieu Chartier8e219ae2014-08-19 14:29:46 -0700298 if (kTimeVerifyMethod) {
299 uint64_t duration_ns = NanoTime() - start_ns;
Andreas Gampe0b0ffc12018-08-01 14:41:27 -0700300 if (duration_ns > MsToNs(Runtime::Current()->GetVerifierLoggingThresholdMs())) {
David Sehr709b0702016-10-13 09:12:37 -0700301 LOG(WARNING) << "Verification of " << dex_file->PrettyMethod(method_idx)
Andreas Gampea4f5bf62015-05-18 20:50:12 -0700302 << " took " << PrettyDuration(duration_ns)
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800303 << (IsLargeMethod(verifier.CodeItem()) ? " (large method)" : "");
Mathieu Chartier8e219ae2014-08-19 14:29:46 -0700304 }
Ian Rogersc8982582012-09-07 16:53:25 -0700305 }
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800306 result.types = verifier.encountered_failure_types_;
Ian Rogersc8982582012-09-07 16:53:25 -0700307 return result;
jeffhaof56197c2012-03-05 18:01:54 -0800308}
309
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100310MethodVerifier* MethodVerifier::VerifyMethodAndDump(Thread* self,
311 VariableIndentationOutputStream* vios,
312 uint32_t dex_method_idx,
313 const DexFile* dex_file,
314 Handle<mirror::DexCache> dex_cache,
315 Handle<mirror::ClassLoader> class_loader,
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800316 const dex::ClassDef& class_def,
317 const dex::CodeItem* code_item,
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100318 ArtMethod* method,
Andreas Gampe6cc23ac2018-08-24 15:22:43 -0700319 uint32_t method_access_flags,
320 uint32_t api_level) {
Andreas Gampebf9611f2016-03-25 16:58:00 -0700321 MethodVerifier* verifier = new MethodVerifier(self,
322 dex_file,
323 dex_cache,
324 class_loader,
325 class_def,
326 code_item,
327 dex_method_idx,
328 method,
329 method_access_flags,
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700330 /* can_load_classes= */ true,
331 /* allow_soft_failures= */ true,
332 /* need_precise_constants= */ true,
333 /* verify_to_dump= */ true,
334 /* allow_thread_suspension= */ true,
Andreas Gampe6cc23ac2018-08-24 15:22:43 -0700335 api_level);
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700336 verifier->Verify();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100337 verifier->DumpFailures(vios->Stream());
338 vios->Stream() << verifier->info_messages_.str();
Andreas Gampe5cbcde22014-09-16 14:59:49 -0700339 // Only dump and return if no hard failures. Otherwise the verifier may be not fully initialized
340 // and querying any info is dangerous/can abort.
341 if (verifier->have_pending_hard_failure_) {
342 delete verifier;
343 return nullptr;
344 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100345 verifier->Dump(vios);
Andreas Gampe5cbcde22014-09-16 14:59:49 -0700346 return verifier;
347 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800348}
349
Ian Rogers7b078e82014-09-10 14:44:24 -0700350MethodVerifier::MethodVerifier(Thread* self,
Andreas Gampe53e32d12015-12-09 21:03:23 -0800351 const DexFile* dex_file,
352 Handle<mirror::DexCache> dex_cache,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700353 Handle<mirror::ClassLoader> class_loader,
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800354 const dex::ClassDef& class_def,
355 const dex::CodeItem* code_item,
Andreas Gampe53e32d12015-12-09 21:03:23 -0800356 uint32_t dex_method_idx,
357 ArtMethod* method,
358 uint32_t method_access_flags,
359 bool can_load_classes,
360 bool allow_soft_failures,
361 bool need_precise_constants,
362 bool verify_to_dump,
Andreas Gampe6cc23ac2018-08-24 15:22:43 -0700363 bool allow_thread_suspension,
364 uint32_t api_level)
Ian Rogers7b078e82014-09-10 14:44:24 -0700365 : self_(self),
Mathieu Chartierde40d472015-10-15 17:47:48 -0700366 arena_stack_(Runtime::Current()->GetArenaPool()),
Vladimir Marko69d310e2017-10-09 14:12:23 +0100367 allocator_(&arena_stack_),
Alex Lightd9aff132017-10-31 22:30:05 +0000368 reg_types_(can_load_classes, allocator_, allow_thread_suspension),
Vladimir Marko69d310e2017-10-09 14:12:23 +0100369 reg_table_(allocator_),
Andreas Gampee2abbc62017-09-15 11:59:26 -0700370 work_insn_idx_(dex::kDexNoIndex),
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800371 dex_method_idx_(dex_method_idx),
Nicolas Geoffrayb041a402017-11-13 15:16:22 +0000372 method_being_verified_(method),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700373 method_access_flags_(method_access_flags),
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700374 return_type_(nullptr),
jeffhaof56197c2012-03-05 18:01:54 -0800375 dex_file_(dex_file),
376 dex_cache_(dex_cache),
377 class_loader_(class_loader),
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700378 class_def_(class_def),
Mathieu Chartier698ebbc2018-01-05 11:00:42 -0800379 code_item_accessor_(*dex_file, code_item),
Ian Rogers7b078e82014-09-10 14:44:24 -0700380 declaring_class_(nullptr),
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700381 interesting_dex_pc_(-1),
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700382 monitor_enter_dex_pcs_(nullptr),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700383 have_pending_hard_failure_(false),
jeffhaofaf459e2012-08-31 15:32:47 -0700384 have_pending_runtime_throw_failure_(false),
Igor Murashkin4d7b75f2015-07-21 17:03:36 -0700385 have_pending_experimental_failure_(false),
Andreas Gamped12e7822015-06-25 10:26:40 -0700386 have_any_pending_runtime_throw_failure_(false),
jeffhaof56197c2012-03-05 18:01:54 -0800387 new_instance_count_(0),
Elliott Hughes80537bb2013-01-04 16:37:26 -0800388 monitor_enter_count_(0),
Andreas Gampe0760a812015-08-26 17:12:51 -0700389 encountered_failure_types_(0),
Jeff Haoee988952013-04-16 14:23:47 -0700390 can_load_classes_(can_load_classes),
Sebastien Hertz4d4adb12013-07-24 16:14:19 +0200391 allow_soft_failures_(allow_soft_failures),
Ian Rogers46960fe2014-05-23 10:43:43 -0700392 need_precise_constants_(need_precise_constants),
Sebastien Hertz4d4adb12013-07-24 16:14:19 +0200393 has_check_casts_(false),
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700394 has_virtual_or_interface_invokes_(false),
Mathieu Chartier4306ef82014-12-19 18:41:47 -0800395 verify_to_dump_(verify_to_dump),
Andreas Gamped5ad72f2015-06-26 17:33:47 -0700396 allow_thread_suspension_(allow_thread_suspension),
Andreas Gampee6215c02015-08-31 18:54:38 -0700397 is_constructor_(false),
Andreas Gampe6cc23ac2018-08-24 15:22:43 -0700398 link_(nullptr),
399 api_level_(api_level == 0 ? std::numeric_limits<uint32_t>::max() : api_level) {
Mathieu Chartierd0ad2ee2015-03-31 14:59:59 -0700400 self->PushVerifier(this);
jeffhaof56197c2012-03-05 18:01:54 -0800401}
402
Mathieu Chartier590fee92013-09-13 13:46:47 -0700403MethodVerifier::~MethodVerifier() {
Mathieu Chartierd0ad2ee2015-03-31 14:59:59 -0700404 Thread::Current()->PopVerifier(this);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700405 STLDeleteElements(&failure_messages_);
406}
407
Andreas Gampeaaf0d382017-11-27 14:10:21 -0800408void MethodVerifier::FindLocksAtDexPc(
409 ArtMethod* m,
410 uint32_t dex_pc,
Andreas Gampe6cc23ac2018-08-24 15:22:43 -0700411 std::vector<MethodVerifier::DexLockInfo>* monitor_enter_dex_pcs,
412 uint32_t api_level) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700413 StackHandleScope<2> hs(Thread::Current());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700414 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache()));
415 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader()));
Andreas Gampebf9611f2016-03-25 16:58:00 -0700416 MethodVerifier verifier(hs.Self(),
417 m->GetDexFile(),
418 dex_cache,
419 class_loader,
David Brazdil15fc7292016-09-02 14:13:18 +0100420 m->GetClassDef(),
Andreas Gampebf9611f2016-03-25 16:58:00 -0700421 m->GetCodeItem(),
422 m->GetDexMethodIndex(),
423 m,
424 m->GetAccessFlags(),
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700425 /* can_load_classes= */ false,
426 /* allow_soft_failures= */ true,
427 /* need_precise_constants= */ false,
428 /* verify_to_dump= */ false,
429 /* allow_thread_suspension= */ false,
Andreas Gampe6cc23ac2018-08-24 15:22:43 -0700430 api_level);
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700431 verifier.interesting_dex_pc_ = dex_pc;
Ian Rogers46960fe2014-05-23 10:43:43 -0700432 verifier.monitor_enter_dex_pcs_ = monitor_enter_dex_pcs;
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700433 verifier.FindLocksAtDexPc();
434}
435
436void MethodVerifier::FindLocksAtDexPc() {
Ian Rogers7b078e82014-09-10 14:44:24 -0700437 CHECK(monitor_enter_dex_pcs_ != nullptr);
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -0800438 CHECK(code_item_accessor_.HasCodeItem()); // This only makes sense for methods with code.
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700439
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -0800440 // Quick check whether there are any monitor_enter instructions before verifying.
441 for (const DexInstructionPcPair& inst : code_item_accessor_) {
442 if (inst->Opcode() == Instruction::MONITOR_ENTER) {
443 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
444 // verification. In practice, the phase we want relies on data structures set up by all the
445 // earlier passes, so we just run the full method verification and bail out early when we've
446 // got what we wanted.
447 Verify();
448 return;
449 }
Andreas Gampecb3c08f2014-09-18 13:16:38 -0700450 }
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700451}
452
Ian Rogersad0b3a32012-04-16 14:50:24 -0700453bool MethodVerifier::Verify() {
Andreas Gampee6215c02015-08-31 18:54:38 -0700454 // Some older code doesn't correctly mark constructors as such. Test for this case by looking at
455 // the name.
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800456 const dex::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
Andreas Gampee6215c02015-08-31 18:54:38 -0700457 const char* method_name = dex_file_->StringDataByIdx(method_id.name_idx_);
458 bool instance_constructor_by_name = strcmp("<init>", method_name) == 0;
459 bool static_constructor_by_name = strcmp("<clinit>", method_name) == 0;
460 bool constructor_by_name = instance_constructor_by_name || static_constructor_by_name;
461 // Check that only constructors are tagged, and check for bad code that doesn't tag constructors.
462 if ((method_access_flags_ & kAccConstructor) != 0) {
463 if (!constructor_by_name) {
464 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
465 << "method is marked as constructor, but not named accordingly";
jeffhaobdb76512011-09-07 11:43:16 -0700466 return false;
Andreas Gampee6215c02015-08-31 18:54:38 -0700467 }
468 is_constructor_ = true;
469 } else if (constructor_by_name) {
David Sehr709b0702016-10-13 09:12:37 -0700470 LOG(WARNING) << "Method " << dex_file_->PrettyMethod(dex_method_idx_)
Andreas Gampee6215c02015-08-31 18:54:38 -0700471 << " not marked as constructor.";
472 is_constructor_ = true;
473 }
474 // If it's a constructor, check whether IsStatic() matches the name.
475 // This should have been rejected by the dex file verifier. Only do in debug build.
476 if (kIsDebugBuild) {
477 if (IsConstructor()) {
478 if (IsStatic() ^ static_constructor_by_name) {
479 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
480 << "constructor name doesn't match static flag";
481 return false;
482 }
jeffhaobdb76512011-09-07 11:43:16 -0700483 }
jeffhaobdb76512011-09-07 11:43:16 -0700484 }
Andreas Gampee6215c02015-08-31 18:54:38 -0700485
486 // Methods may only have one of public/protected/private.
487 // This should have been rejected by the dex file verifier. Only do in debug build.
488 if (kIsDebugBuild) {
489 size_t access_mod_count =
490 (((method_access_flags_ & kAccPublic) == 0) ? 0 : 1) +
491 (((method_access_flags_ & kAccProtected) == 0) ? 0 : 1) +
492 (((method_access_flags_ & kAccPrivate) == 0) ? 0 : 1);
493 if (access_mod_count > 1) {
494 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "method has more than one of public/protected/private";
495 return false;
496 }
497 }
498
499 // If there aren't any instructions, make sure that's expected, then exit successfully.
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -0800500 if (!code_item_accessor_.HasCodeItem()) {
Andreas Gampe76ed99d2016-03-28 18:31:29 -0700501 // Only native or abstract methods may not have code.
502 if ((method_access_flags_ & (kAccNative | kAccAbstract)) == 0) {
503 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "zero-length code in concrete non-native method";
504 return false;
505 }
506
Andreas Gampee6215c02015-08-31 18:54:38 -0700507 // This should have been rejected by the dex file verifier. Only do in debug build.
Andreas Gampe76ed99d2016-03-28 18:31:29 -0700508 // Note: the above will also be rejected in the dex file verifier, starting in dex version 37.
Andreas Gampee6215c02015-08-31 18:54:38 -0700509 if (kIsDebugBuild) {
Andreas Gampee6215c02015-08-31 18:54:38 -0700510 if ((method_access_flags_ & kAccAbstract) != 0) {
511 // Abstract methods are not allowed to have the following flags.
512 static constexpr uint32_t kForbidden =
513 kAccPrivate |
514 kAccStatic |
515 kAccFinal |
516 kAccNative |
517 kAccStrict |
518 kAccSynchronized;
519 if ((method_access_flags_ & kForbidden) != 0) {
520 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
521 << "method can't be abstract and private/static/final/native/strict/synchronized";
522 return false;
523 }
524 }
David Brazdil15fc7292016-09-02 14:13:18 +0100525 if ((class_def_.GetJavaAccessFlags() & kAccInterface) != 0) {
Alex Lighteb7c1442015-08-31 13:17:42 -0700526 // Interface methods must be public and abstract (if default methods are disabled).
Neil Fuller9724c632016-01-07 15:42:47 +0000527 uint32_t kRequired = kAccPublic;
Alex Lighteb7c1442015-08-31 13:17:42 -0700528 if ((method_access_flags_ & kRequired) != kRequired) {
Neil Fuller9724c632016-01-07 15:42:47 +0000529 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface methods must be public";
Andreas Gampee6215c02015-08-31 18:54:38 -0700530 return false;
531 }
532 // In addition to the above, interface methods must not be protected.
533 static constexpr uint32_t kForbidden = kAccProtected;
534 if ((method_access_flags_ & kForbidden) != 0) {
535 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface methods can't be protected";
536 return false;
537 }
538 }
539 // We also don't allow constructors to be abstract or native.
540 if (IsConstructor()) {
541 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "constructors can't be abstract or native";
542 return false;
543 }
544 }
545 return true;
546 }
547
548 // This should have been rejected by the dex file verifier. Only do in debug build.
549 if (kIsDebugBuild) {
550 // When there's code, the method must not be native or abstract.
551 if ((method_access_flags_ & (kAccNative | kAccAbstract)) != 0) {
552 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "non-zero-length code in abstract or native method";
553 return false;
554 }
555
David Brazdil15fc7292016-09-02 14:13:18 +0100556 if ((class_def_.GetJavaAccessFlags() & kAccInterface) != 0) {
Alex Lighteb7c1442015-08-31 13:17:42 -0700557 // Interfaces may always have static initializers for their fields. If we are running with
558 // default methods enabled we also allow other public, static, non-final methods to have code.
559 // Otherwise that is the only type of method allowed.
Alex Light0db36b32015-10-27 14:06:34 -0700560 if (!(IsConstructor() && IsStatic())) {
Neil Fuller9724c632016-01-07 15:42:47 +0000561 if (IsInstanceConstructor()) {
562 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interfaces may not have non-static constructor";
563 return false;
564 } else if (method_access_flags_ & kAccFinal) {
565 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interfaces may not have final methods";
566 return false;
Alex Lightb55f1ac2016-04-12 15:50:55 -0700567 } else {
568 uint32_t access_flag_options = kAccPublic;
Mathieu Chartierf6e31472017-12-28 13:32:08 -0800569 if (dex_file_->SupportsDefaultMethods()) {
Alex Lightb55f1ac2016-04-12 15:50:55 -0700570 access_flag_options |= kAccPrivate;
571 }
572 if (!(method_access_flags_ & access_flag_options)) {
573 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
574 << "interfaces may not have protected or package-private members";
575 return false;
576 }
Alex Lighteb7c1442015-08-31 13:17:42 -0700577 }
Andreas Gampee6215c02015-08-31 18:54:38 -0700578 }
579 }
580
581 // Instance constructors must not be synchronized.
582 if (IsInstanceConstructor()) {
583 static constexpr uint32_t kForbidden = kAccSynchronized;
584 if ((method_access_flags_ & kForbidden) != 0) {
585 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "constructors can't be synchronized";
586 return false;
587 }
588 }
589 }
590
Ian Rogersd81871c2011-10-03 13:57:23 -0700591 // Sanity-check the register counts. ins + locals = registers, so make sure that ins <= registers.
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -0800592 if (code_item_accessor_.InsSize() > code_item_accessor_.RegistersSize()) {
593 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad register counts (ins="
594 << code_item_accessor_.InsSize()
595 << " regs=" << code_item_accessor_.RegistersSize();
Ian Rogersd81871c2011-10-03 13:57:23 -0700596 return false;
jeffhaobdb76512011-09-07 11:43:16 -0700597 }
Alex Lighteb7c1442015-08-31 13:17:42 -0700598
Ian Rogersd81871c2011-10-03 13:57:23 -0700599 // Allocate and initialize an array to hold instruction data.
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -0800600 insn_flags_.reset(allocator_.AllocArray<InstructionFlags>(
601 code_item_accessor_.InsnsSizeInCodeUnits()));
Mathieu Chartierde40d472015-10-15 17:47:48 -0700602 DCHECK(insn_flags_ != nullptr);
603 std::uninitialized_fill_n(insn_flags_.get(),
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -0800604 code_item_accessor_.InsnsSizeInCodeUnits(),
Mathieu Chartierde40d472015-10-15 17:47:48 -0700605 InstructionFlags());
Ian Rogersd81871c2011-10-03 13:57:23 -0700606 // Run through the instructions and see if the width checks out.
607 bool result = ComputeWidthsAndCountOps();
Andreas Gampebf1cb772017-05-15 15:39:00 -0700608 bool allow_runtime_only_instructions = !Runtime::Current()->IsAotCompiler() || verify_to_dump_;
Ian Rogersd81871c2011-10-03 13:57:23 -0700609 // Flag instructions guarded by a "try" block and check exception handlers.
610 result = result && ScanTryCatchBlocks();
611 // Perform static instruction verification.
Andreas Gampebf1cb772017-05-15 15:39:00 -0700612 result = result && (allow_runtime_only_instructions
613 ? VerifyInstructions<true>()
614 : VerifyInstructions<false>());
Ian Rogersad0b3a32012-04-16 14:50:24 -0700615 // Perform code-flow analysis and return.
Vladimir Marko2b5eaa22013-12-13 13:59:30 +0000616 result = result && VerifyCodeFlow();
Andreas Gampe53e32d12015-12-09 21:03:23 -0800617
Vladimir Marko2b5eaa22013-12-13 13:59:30 +0000618 return result;
jeffhaoba5ebb92011-08-25 17:24:37 -0700619}
620
Ian Rogers776ac1f2012-04-13 23:36:36 -0700621std::ostream& MethodVerifier::Fail(VerifyError error) {
Andreas Gampe0760a812015-08-26 17:12:51 -0700622 // Mark the error type as encountered.
Andreas Gampea727e372015-08-25 09:22:37 -0700623 encountered_failure_types_ |= static_cast<uint32_t>(error);
Andreas Gampe0760a812015-08-26 17:12:51 -0700624
Ian Rogersad0b3a32012-04-16 14:50:24 -0700625 switch (error) {
626 case VERIFY_ERROR_NO_CLASS:
627 case VERIFY_ERROR_NO_FIELD:
628 case VERIFY_ERROR_NO_METHOD:
629 case VERIFY_ERROR_ACCESS_CLASS:
630 case VERIFY_ERROR_ACCESS_FIELD:
631 case VERIFY_ERROR_ACCESS_METHOD:
Ian Rogers08f753d2012-08-24 14:35:25 -0700632 case VERIFY_ERROR_INSTANTIATION:
633 case VERIFY_ERROR_CLASS_CHANGE:
Igor Murashkin158f35c2015-06-10 15:55:30 -0700634 case VERIFY_ERROR_FORCE_INTERPRETER:
Andreas Gampea727e372015-08-25 09:22:37 -0700635 case VERIFY_ERROR_LOCKING:
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800636 if (Runtime::Current()->IsAotCompiler() || !can_load_classes_) {
jeffhaofaf459e2012-08-31 15:32:47 -0700637 // If we're optimistically running verification at compile time, turn NO_xxx, ACCESS_xxx,
638 // class change and instantiation errors into soft verification errors so that we re-verify
639 // at runtime. We may fail to find or to agree on access because of not yet available class
640 // loaders, or class loaders that will differ at runtime. In these cases, we don't want to
641 // affect the soundness of the code being compiled. Instead, the generated code runs "slow
642 // paths" that dynamically perform the verification and cause the behavior to be that akin
643 // to an interpreter.
644 error = VERIFY_ERROR_BAD_CLASS_SOFT;
645 } else {
Jeff Haoa3faaf42013-09-03 19:07:00 -0700646 // If we fail again at runtime, mark that this instruction would throw and force this
647 // method to be executed using the interpreter with checks.
jeffhaofaf459e2012-08-31 15:32:47 -0700648 have_pending_runtime_throw_failure_ = true;
Andreas Gamped7f8d052015-03-12 11:05:47 -0700649
650 // We need to save the work_line if the instruction wasn't throwing before. Otherwise we'll
651 // try to merge garbage.
652 // Note: this assumes that Fail is called before we do any work_line modifications.
Andreas Gamped5ad72f2015-06-26 17:33:47 -0700653 // Note: this can fail before we touch any instruction, for the signature of a method. So
654 // add a check.
Andreas Gampee2abbc62017-09-15 11:59:26 -0700655 if (work_insn_idx_ < dex::kDexNoIndex) {
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -0800656 const Instruction& inst = code_item_accessor_.InstructionAt(work_insn_idx_);
Mathieu Chartier1d2d4ff2017-09-23 16:11:06 -0700657 int opcode_flags = Instruction::FlagsOf(inst.Opcode());
Andreas Gamped7f8d052015-03-12 11:05:47 -0700658
Andreas Gamped5ad72f2015-06-26 17:33:47 -0700659 if ((opcode_flags & Instruction::kThrow) == 0 && CurrentInsnFlags()->IsInTry()) {
660 saved_line_->CopyFromLine(work_line_.get());
661 }
Andreas Gamped7f8d052015-03-12 11:05:47 -0700662 }
jeffhaofaf459e2012-08-31 15:32:47 -0700663 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700664 break;
Andreas Gampea727e372015-08-25 09:22:37 -0700665
Ian Rogersad0b3a32012-04-16 14:50:24 -0700666 // Indication that verification should be retried at runtime.
667 case VERIFY_ERROR_BAD_CLASS_SOFT:
Jeff Haoee988952013-04-16 14:23:47 -0700668 if (!allow_soft_failures_) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700669 have_pending_hard_failure_ = true;
670 }
671 break;
Andreas Gampea727e372015-08-25 09:22:37 -0700672
jeffhaod5347e02012-03-22 17:25:05 -0700673 // Hard verification failures at compile time will still fail at runtime, so the class is
674 // marked as rejected to prevent it from being compiled.
Ian Rogersad0b3a32012-04-16 14:50:24 -0700675 case VERIFY_ERROR_BAD_CLASS_HARD: {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700676 have_pending_hard_failure_ = true;
Andreas Gampeebf850c2015-08-14 15:37:35 -0700677 if (VLOG_IS_ON(verifier) && kDumpRegLinesOnHardFailureIfVLOG) {
678 ScopedObjectAccess soa(Thread::Current());
679 std::ostringstream oss;
680 Dump(oss);
681 LOG(ERROR) << oss.str();
682 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700683 break;
Ian Rogers47a05882012-02-03 12:23:33 -0800684 }
685 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700686 failures_.push_back(error);
David Sehr709b0702016-10-13 09:12:37 -0700687 std::string location(StringPrintf("%s: [0x%X] ", dex_file_->PrettyMethod(dex_method_idx_).c_str(),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700688 work_insn_idx_));
Elena Sayapina78480ec2014-08-15 15:52:42 +0700689 std::ostringstream* failure_message = new std::ostringstream(location, std::ostringstream::ate);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700690 failure_messages_.push_back(failure_message);
691 return *failure_message;
692}
693
Andreas Gampe2ad6cce2019-04-11 16:17:39 -0700694ScopedNewLine MethodVerifier::LogVerifyInfo() {
695 ScopedNewLine ret{info_messages_};
696 ret << "VFY: " << dex_file_->PrettyMethod(dex_method_idx_)
697 << '[' << reinterpret_cast<void*>(work_insn_idx_) << "] : ";
698 return ret;
Ian Rogers576ca0c2014-06-06 15:58:22 -0700699}
700
Ian Rogersad0b3a32012-04-16 14:50:24 -0700701void MethodVerifier::PrependToLastFailMessage(std::string prepend) {
702 size_t failure_num = failure_messages_.size();
703 DCHECK_NE(failure_num, 0U);
704 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
705 prepend += last_fail_message->str();
Elena Sayapina78480ec2014-08-15 15:52:42 +0700706 failure_messages_[failure_num - 1] = new std::ostringstream(prepend, std::ostringstream::ate);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700707 delete last_fail_message;
708}
709
Vladimir Marko5c657fe2016-11-03 15:12:29 +0000710void MethodVerifier::AppendToLastFailMessage(const std::string& append) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700711 size_t failure_num = failure_messages_.size();
712 DCHECK_NE(failure_num, 0U);
713 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
714 (*last_fail_message) << append;
Ian Rogers47a05882012-02-03 12:23:33 -0800715}
716
Ian Rogers776ac1f2012-04-13 23:36:36 -0700717bool MethodVerifier::ComputeWidthsAndCountOps() {
jeffhaobdb76512011-09-07 11:43:16 -0700718 size_t new_instance_count = 0;
719 size_t monitor_enter_count = 0;
jeffhaobdb76512011-09-07 11:43:16 -0700720
Mathieu Chartieraf7c9022017-10-27 09:42:46 -0700721 // We can't assume the instruction is well formed, handle the case where calculating the size
722 // goes past the end of the code item.
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -0800723 SafeDexInstructionIterator it(code_item_accessor_.begin(), code_item_accessor_.end());
724 for ( ; !it.IsErrorState() && it < code_item_accessor_.end(); ++it) {
Mathieu Chartieraf7c9022017-10-27 09:42:46 -0700725 // In case the instruction goes past the end of the code item, make sure to not process it.
726 SafeDexInstructionIterator next = it;
727 ++next;
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -0800728 if (next.IsErrorState()) {
Mathieu Chartieraf7c9022017-10-27 09:42:46 -0700729 break;
730 }
731 Instruction::Code opcode = it->Opcode();
Ian Rogersa9a82542013-10-04 11:17:26 -0700732 switch (opcode) {
733 case Instruction::APUT_OBJECT:
734 case Instruction::CHECK_CAST:
735 has_check_casts_ = true;
736 break;
737 case Instruction::INVOKE_VIRTUAL:
738 case Instruction::INVOKE_VIRTUAL_RANGE:
739 case Instruction::INVOKE_INTERFACE:
740 case Instruction::INVOKE_INTERFACE_RANGE:
741 has_virtual_or_interface_invokes_ = true;
742 break;
743 case Instruction::MONITOR_ENTER:
744 monitor_enter_count++;
745 break;
746 case Instruction::NEW_INSTANCE:
747 new_instance_count++;
748 break;
749 default:
750 break;
jeffhaobdb76512011-09-07 11:43:16 -0700751 }
Mathieu Chartier2b2bef22017-10-26 17:10:19 -0700752 GetInstructionFlags(it.DexPc()).SetIsOpcode();
jeffhaobdb76512011-09-07 11:43:16 -0700753 }
754
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -0800755 if (it != code_item_accessor_.end()) {
756 const size_t insns_size = code_item_accessor_.InsnsSizeInCodeUnits();
jeffhaod5347e02012-03-22 17:25:05 -0700757 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "code did not end where expected ("
Mathieu Chartier2b2bef22017-10-26 17:10:19 -0700758 << it.DexPc() << " vs. " << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700759 return false;
760 }
761
Ian Rogersd81871c2011-10-03 13:57:23 -0700762 new_instance_count_ = new_instance_count;
763 monitor_enter_count_ = monitor_enter_count;
jeffhaobdb76512011-09-07 11:43:16 -0700764 return true;
765}
766
Ian Rogers776ac1f2012-04-13 23:36:36 -0700767bool MethodVerifier::ScanTryCatchBlocks() {
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -0800768 const uint32_t tries_size = code_item_accessor_.TriesSize();
jeffhaobdb76512011-09-07 11:43:16 -0700769 if (tries_size == 0) {
770 return true;
771 }
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -0800772 const uint32_t insns_size = code_item_accessor_.InsnsSizeInCodeUnits();
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800773 for (const dex::TryItem& try_item : code_item_accessor_.TryItems()) {
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -0800774 const uint32_t start = try_item.start_addr_;
775 const uint32_t end = start + try_item.insn_count_;
jeffhaobdb76512011-09-07 11:43:16 -0700776 if ((start >= end) || (start >= insns_size) || (end > insns_size)) {
jeffhaod5347e02012-03-22 17:25:05 -0700777 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad exception entry: startAddr=" << start
778 << " endAddr=" << end << " (size=" << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700779 return false;
780 }
Mathieu Chartierde40d472015-10-15 17:47:48 -0700781 if (!GetInstructionFlags(start).IsOpcode()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700782 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
783 << "'try' block starts inside an instruction (" << start << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700784 return false;
785 }
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -0800786 DexInstructionIterator end_it(code_item_accessor_.Insns(), end);
787 for (DexInstructionIterator it(code_item_accessor_.Insns(), start); it < end_it; ++it) {
788 GetInstructionFlags(it.DexPc()).SetInTry();
jeffhaobdb76512011-09-07 11:43:16 -0700789 }
790 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800791 // Iterate over each of the handlers to verify target addresses.
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -0800792 const uint8_t* handlers_ptr = code_item_accessor_.GetCatchHandlerData();
793 const uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700794 ClassLinker* linker = Runtime::Current()->GetClassLinker();
jeffhaobdb76512011-09-07 11:43:16 -0700795 for (uint32_t idx = 0; idx < handlers_size; idx++) {
Ian Rogers0571d352011-11-03 19:51:38 -0700796 CatchHandlerIterator iterator(handlers_ptr);
797 for (; iterator.HasNext(); iterator.Next()) {
Igor Murashkin2ffb7032017-11-08 13:35:21 -0800798 uint32_t dex_pc = iterator.GetHandlerAddress();
Mathieu Chartierde40d472015-10-15 17:47:48 -0700799 if (!GetInstructionFlags(dex_pc).IsOpcode()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700800 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
801 << "exception handler starts at bad address (" << dex_pc << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700802 return false;
803 }
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -0800804 if (!CheckNotMoveResult(code_item_accessor_.Insns(), dex_pc)) {
Stephen Kyle9bc61992014-09-22 13:53:15 +0100805 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
806 << "exception handler begins with move-result* (" << dex_pc << ")";
807 return false;
808 }
Mathieu Chartierde40d472015-10-15 17:47:48 -0700809 GetInstructionFlags(dex_pc).SetBranchTarget();
Ian Rogers28ad40d2011-10-27 15:19:26 -0700810 // Ensure exception types are resolved so that they don't need resolution to be delivered,
811 // unresolved exception types will be ignored by exception delivery
Andreas Gampea5b09a62016-11-17 15:21:22 -0800812 if (iterator.GetHandlerTypeIndex().IsValid()) {
Vladimir Marko666ee3d2017-12-11 18:37:36 +0000813 ObjPtr<mirror::Class> exception_type =
814 linker->ResolveType(iterator.GetHandlerTypeIndex(), dex_cache_, class_loader_);
Ian Rogers7b078e82014-09-10 14:44:24 -0700815 if (exception_type == nullptr) {
816 DCHECK(self_->IsExceptionPending());
817 self_->ClearException();
Ian Rogers28ad40d2011-10-27 15:19:26 -0700818 }
819 }
jeffhaobdb76512011-09-07 11:43:16 -0700820 }
Ian Rogers0571d352011-11-03 19:51:38 -0700821 handlers_ptr = iterator.EndDataPointer();
jeffhaobdb76512011-09-07 11:43:16 -0700822 }
jeffhaobdb76512011-09-07 11:43:16 -0700823 return true;
824}
825
Andreas Gampebf1cb772017-05-15 15:39:00 -0700826template <bool kAllowRuntimeOnlyInstructions>
Ian Rogers776ac1f2012-04-13 23:36:36 -0700827bool MethodVerifier::VerifyInstructions() {
Ian Rogers0c7abda2012-09-19 13:33:42 -0700828 /* Flag the start of the method as a branch target, and a GC point due to stack overflow errors */
Mathieu Chartierde40d472015-10-15 17:47:48 -0700829 GetInstructionFlags(0).SetBranchTarget();
830 GetInstructionFlags(0).SetCompileTimeInfoPoint();
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -0800831 for (const DexInstructionPcPair& inst : code_item_accessor_) {
Mathieu Chartier2b2bef22017-10-26 17:10:19 -0700832 const uint32_t dex_pc = inst.DexPc();
833 if (!VerifyInstruction<kAllowRuntimeOnlyInstructions>(&inst.Inst(), dex_pc)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700834 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -0700835 return false;
836 }
837 /* Flag instructions that are garbage collection points */
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700838 // All invoke points are marked as "Throw" points already.
839 // We are relying on this to also count all the invokes as interesting.
Vladimir Marko8b858e12014-11-27 14:52:37 +0000840 if (inst->IsBranch()) {
Mathieu Chartierde40d472015-10-15 17:47:48 -0700841 GetInstructionFlags(dex_pc).SetCompileTimeInfoPoint();
Vladimir Marko8b858e12014-11-27 14:52:37 +0000842 // The compiler also needs safepoints for fall-through to loop heads.
843 // Such a loop head must be a target of a branch.
844 int32_t offset = 0;
845 bool cond, self_ok;
846 bool target_ok = GetBranchOffset(dex_pc, &offset, &cond, &self_ok);
847 DCHECK(target_ok);
Mathieu Chartierde40d472015-10-15 17:47:48 -0700848 GetInstructionFlags(dex_pc + offset).SetCompileTimeInfoPoint();
Vladimir Marko8b858e12014-11-27 14:52:37 +0000849 } else if (inst->IsSwitch() || inst->IsThrow()) {
Mathieu Chartierde40d472015-10-15 17:47:48 -0700850 GetInstructionFlags(dex_pc).SetCompileTimeInfoPoint();
Ian Rogersb8c78592013-07-25 23:52:52 +0000851 } else if (inst->IsReturn()) {
Mathieu Chartierde40d472015-10-15 17:47:48 -0700852 GetInstructionFlags(dex_pc).SetCompileTimeInfoPointAndReturn();
Ian Rogersd81871c2011-10-03 13:57:23 -0700853 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700854 }
855 return true;
856}
857
Andreas Gampebf1cb772017-05-15 15:39:00 -0700858template <bool kAllowRuntimeOnlyInstructions>
859bool MethodVerifier::VerifyInstruction(const Instruction* inst, uint32_t code_offset) {
Andreas Gampee05cc662017-05-15 10:17:30 -0700860 if (Instruction::kHaveExperimentalInstructions && UNLIKELY(inst->IsExperimental())) {
Igor Murashkin4d7b75f2015-07-21 17:03:36 -0700861 // Experimental instructions don't yet have verifier support implementation.
862 // While it is possible to use them by themselves, when we try to use stable instructions
863 // with a virtual register that was created by an experimental instruction,
864 // the data flow analysis will fail.
865 Fail(VERIFY_ERROR_FORCE_INTERPRETER)
866 << "experimental instruction is not supported by verifier; skipping verification";
867 have_pending_experimental_failure_ = true;
868 return false;
869 }
870
Ian Rogersd81871c2011-10-03 13:57:23 -0700871 bool result = true;
872 switch (inst->GetVerifyTypeArgumentA()) {
873 case Instruction::kVerifyRegA:
Ian Rogers29a26482014-05-02 15:27:29 -0700874 result = result && CheckRegisterIndex(inst->VRegA());
Ian Rogersd81871c2011-10-03 13:57:23 -0700875 break;
876 case Instruction::kVerifyRegAWide:
Ian Rogers29a26482014-05-02 15:27:29 -0700877 result = result && CheckWideRegisterIndex(inst->VRegA());
Ian Rogersd81871c2011-10-03 13:57:23 -0700878 break;
879 }
880 switch (inst->GetVerifyTypeArgumentB()) {
881 case Instruction::kVerifyRegB:
Ian Rogers29a26482014-05-02 15:27:29 -0700882 result = result && CheckRegisterIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700883 break;
884 case Instruction::kVerifyRegBField:
Ian Rogers29a26482014-05-02 15:27:29 -0700885 result = result && CheckFieldIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700886 break;
887 case Instruction::kVerifyRegBMethod:
Ian Rogers29a26482014-05-02 15:27:29 -0700888 result = result && CheckMethodIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700889 break;
890 case Instruction::kVerifyRegBNewInstance:
Andreas Gampea5b09a62016-11-17 15:21:22 -0800891 result = result && CheckNewInstance(dex::TypeIndex(inst->VRegB()));
Ian Rogersd81871c2011-10-03 13:57:23 -0700892 break;
893 case Instruction::kVerifyRegBString:
Ian Rogers29a26482014-05-02 15:27:29 -0700894 result = result && CheckStringIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700895 break;
896 case Instruction::kVerifyRegBType:
Andreas Gampea5b09a62016-11-17 15:21:22 -0800897 result = result && CheckTypeIndex(dex::TypeIndex(inst->VRegB()));
Ian Rogersd81871c2011-10-03 13:57:23 -0700898 break;
899 case Instruction::kVerifyRegBWide:
Ian Rogers29a26482014-05-02 15:27:29 -0700900 result = result && CheckWideRegisterIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700901 break;
Orion Hodson2e599942017-09-22 16:17:41 +0100902 case Instruction::kVerifyRegBCallSite:
903 result = result && CheckCallSiteIndex(inst->VRegB());
904 break;
905 case Instruction::kVerifyRegBMethodHandle:
906 result = result && CheckMethodHandleIndex(inst->VRegB());
907 break;
908 case Instruction::kVerifyRegBPrototype:
909 result = result && CheckPrototypeIndex(inst->VRegB());
910 break;
Ian Rogersd81871c2011-10-03 13:57:23 -0700911 }
912 switch (inst->GetVerifyTypeArgumentC()) {
913 case Instruction::kVerifyRegC:
Ian Rogers29a26482014-05-02 15:27:29 -0700914 result = result && CheckRegisterIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700915 break;
916 case Instruction::kVerifyRegCField:
Ian Rogers29a26482014-05-02 15:27:29 -0700917 result = result && CheckFieldIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700918 break;
919 case Instruction::kVerifyRegCNewArray:
Andreas Gampea5b09a62016-11-17 15:21:22 -0800920 result = result && CheckNewArray(dex::TypeIndex(inst->VRegC()));
Ian Rogersd81871c2011-10-03 13:57:23 -0700921 break;
922 case Instruction::kVerifyRegCType:
Andreas Gampea5b09a62016-11-17 15:21:22 -0800923 result = result && CheckTypeIndex(dex::TypeIndex(inst->VRegC()));
Ian Rogersd81871c2011-10-03 13:57:23 -0700924 break;
925 case Instruction::kVerifyRegCWide:
Ian Rogers29a26482014-05-02 15:27:29 -0700926 result = result && CheckWideRegisterIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700927 break;
928 }
Orion Hodsoncfa325e2016-10-13 10:25:54 +0100929 switch (inst->GetVerifyTypeArgumentH()) {
930 case Instruction::kVerifyRegHPrototype:
931 result = result && CheckPrototypeIndex(inst->VRegH());
932 break;
933 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700934 switch (inst->GetVerifyExtraFlags()) {
935 case Instruction::kVerifyArrayData:
936 result = result && CheckArrayData(code_offset);
937 break;
938 case Instruction::kVerifyBranchTarget:
939 result = result && CheckBranchTarget(code_offset);
940 break;
941 case Instruction::kVerifySwitchTargets:
942 result = result && CheckSwitchTargets(code_offset);
943 break;
Andreas Gampec3314312014-06-19 18:13:29 -0700944 case Instruction::kVerifyVarArgNonZero:
945 // Fall-through.
Ian Rogers29a26482014-05-02 15:27:29 -0700946 case Instruction::kVerifyVarArg: {
Taiju Tsuiki29498a22015-04-13 14:21:00 +0900947 // Instructions that can actually return a negative value shouldn't have this flag.
948 uint32_t v_a = dchecked_integral_cast<uint32_t>(inst->VRegA());
949 if ((inst->GetVerifyExtraFlags() == Instruction::kVerifyVarArgNonZero && v_a == 0) ||
950 v_a > Instruction::kMaxVarArgRegs) {
951 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << v_a << ") in "
Andreas Gampec3314312014-06-19 18:13:29 -0700952 "non-range invoke";
953 return false;
954 }
Taiju Tsuiki29498a22015-04-13 14:21:00 +0900955
Ian Rogers29a26482014-05-02 15:27:29 -0700956 uint32_t args[Instruction::kMaxVarArgRegs];
957 inst->GetVarArgs(args);
Taiju Tsuiki29498a22015-04-13 14:21:00 +0900958 result = result && CheckVarArgRegs(v_a, args);
Ian Rogersd81871c2011-10-03 13:57:23 -0700959 break;
Ian Rogers29a26482014-05-02 15:27:29 -0700960 }
Andreas Gampec3314312014-06-19 18:13:29 -0700961 case Instruction::kVerifyVarArgRangeNonZero:
962 // Fall-through.
Ian Rogersd81871c2011-10-03 13:57:23 -0700963 case Instruction::kVerifyVarArgRange:
Andreas Gampec3314312014-06-19 18:13:29 -0700964 if (inst->GetVerifyExtraFlags() == Instruction::kVerifyVarArgRangeNonZero &&
965 inst->VRegA() <= 0) {
966 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << inst->VRegA() << ") in "
967 "range invoke";
968 return false;
969 }
Ian Rogers29a26482014-05-02 15:27:29 -0700970 result = result && CheckVarArgRangeRegs(inst->VRegA(), inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700971 break;
972 case Instruction::kVerifyError:
jeffhaod5347e02012-03-22 17:25:05 -0700973 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected opcode " << inst->Name();
Ian Rogersd81871c2011-10-03 13:57:23 -0700974 result = false;
975 break;
976 }
Andreas Gampebf1cb772017-05-15 15:39:00 -0700977 if (!kAllowRuntimeOnlyInstructions && inst->GetVerifyIsRuntimeOnly()) {
Ian Rogers5fb22a92014-06-13 10:31:28 -0700978 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "opcode only expected at runtime " << inst->Name();
979 result = false;
980 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700981 return result;
982}
983
Ian Rogers7b078e82014-09-10 14:44:24 -0700984inline bool MethodVerifier::CheckRegisterIndex(uint32_t idx) {
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -0800985 if (UNLIKELY(idx >= code_item_accessor_.RegistersSize())) {
jeffhaod5347e02012-03-22 17:25:05 -0700986 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register index out of range (" << idx << " >= "
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -0800987 << code_item_accessor_.RegistersSize() << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700988 return false;
989 }
990 return true;
991}
992
Ian Rogers7b078e82014-09-10 14:44:24 -0700993inline bool MethodVerifier::CheckWideRegisterIndex(uint32_t idx) {
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -0800994 if (UNLIKELY(idx + 1 >= code_item_accessor_.RegistersSize())) {
jeffhaod5347e02012-03-22 17:25:05 -0700995 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "wide register index out of range (" << idx
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -0800996 << "+1 >= " << code_item_accessor_.RegistersSize() << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700997 return false;
998 }
999 return true;
1000}
1001
Orion Hodson2e599942017-09-22 16:17:41 +01001002inline bool MethodVerifier::CheckCallSiteIndex(uint32_t idx) {
1003 uint32_t limit = dex_file_->NumCallSiteIds();
1004 if (UNLIKELY(idx >= limit)) {
1005 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad call site index " << idx << " (max "
1006 << limit << ")";
1007 return false;
1008 }
1009 return true;
1010}
1011
Ian Rogers7b078e82014-09-10 14:44:24 -07001012inline bool MethodVerifier::CheckFieldIndex(uint32_t idx) {
Andreas Gampe29e81422017-05-15 16:29:32 -07001013 if (UNLIKELY(idx >= dex_file_->GetHeader().field_ids_size_)) {
jeffhaod5347e02012-03-22 17:25:05 -07001014 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad field index " << idx << " (max "
1015 << dex_file_->GetHeader().field_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001016 return false;
1017 }
1018 return true;
1019}
1020
Ian Rogers7b078e82014-09-10 14:44:24 -07001021inline bool MethodVerifier::CheckMethodIndex(uint32_t idx) {
Andreas Gampe29e81422017-05-15 16:29:32 -07001022 if (UNLIKELY(idx >= dex_file_->GetHeader().method_ids_size_)) {
jeffhaod5347e02012-03-22 17:25:05 -07001023 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad method index " << idx << " (max "
1024 << dex_file_->GetHeader().method_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001025 return false;
1026 }
1027 return true;
1028}
1029
Orion Hodson2e599942017-09-22 16:17:41 +01001030inline bool MethodVerifier::CheckMethodHandleIndex(uint32_t idx) {
1031 uint32_t limit = dex_file_->NumMethodHandles();
1032 if (UNLIKELY(idx >= limit)) {
1033 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad method handle index " << idx << " (max "
1034 << limit << ")";
1035 return false;
1036 }
1037 return true;
1038}
1039
Andreas Gampea5b09a62016-11-17 15:21:22 -08001040inline bool MethodVerifier::CheckNewInstance(dex::TypeIndex idx) {
Andreas Gampe29e81422017-05-15 16:29:32 -07001041 if (UNLIKELY(idx.index_ >= dex_file_->GetHeader().type_ids_size_)) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08001042 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx.index_ << " (max "
jeffhaod5347e02012-03-22 17:25:05 -07001043 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001044 return false;
1045 }
1046 // We don't need the actual class, just a pointer to the class name.
Ian Rogers0571d352011-11-03 19:51:38 -07001047 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Andreas Gampe29e81422017-05-15 16:29:32 -07001048 if (UNLIKELY(descriptor[0] != 'L')) {
jeffhaod5347e02012-03-22 17:25:05 -07001049 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "can't call new-instance on type '" << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001050 return false;
Andreas Gampe29e81422017-05-15 16:29:32 -07001051 } else if (UNLIKELY(strcmp(descriptor, "Ljava/lang/Class;") == 0)) {
Aart Bikdb698f12016-07-25 17:52:22 -07001052 // An unlikely new instance on Class is not allowed. Fall back to interpreter to ensure an
1053 // exception is thrown when this statement is executed (compiled code would not do that).
1054 Fail(VERIFY_ERROR_INSTANTIATION);
Ian Rogersd81871c2011-10-03 13:57:23 -07001055 }
1056 return true;
1057}
1058
Orion Hodsoncfa325e2016-10-13 10:25:54 +01001059inline bool MethodVerifier::CheckPrototypeIndex(uint32_t idx) {
Andreas Gampe29e81422017-05-15 16:29:32 -07001060 if (UNLIKELY(idx >= dex_file_->GetHeader().proto_ids_size_)) {
Orion Hodsoncfa325e2016-10-13 10:25:54 +01001061 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad prototype index " << idx << " (max "
1062 << dex_file_->GetHeader().proto_ids_size_ << ")";
1063 return false;
1064 }
1065 return true;
1066}
1067
Ian Rogers7b078e82014-09-10 14:44:24 -07001068inline bool MethodVerifier::CheckStringIndex(uint32_t idx) {
Andreas Gampe29e81422017-05-15 16:29:32 -07001069 if (UNLIKELY(idx >= dex_file_->GetHeader().string_ids_size_)) {
jeffhaod5347e02012-03-22 17:25:05 -07001070 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad string index " << idx << " (max "
1071 << dex_file_->GetHeader().string_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001072 return false;
1073 }
1074 return true;
1075}
1076
Andreas Gampea5b09a62016-11-17 15:21:22 -08001077inline bool MethodVerifier::CheckTypeIndex(dex::TypeIndex idx) {
Andreas Gampe29e81422017-05-15 16:29:32 -07001078 if (UNLIKELY(idx.index_ >= dex_file_->GetHeader().type_ids_size_)) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08001079 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx.index_ << " (max "
jeffhaod5347e02012-03-22 17:25:05 -07001080 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001081 return false;
1082 }
1083 return true;
1084}
1085
Andreas Gampea5b09a62016-11-17 15:21:22 -08001086bool MethodVerifier::CheckNewArray(dex::TypeIndex idx) {
Andreas Gampe29e81422017-05-15 16:29:32 -07001087 if (UNLIKELY(idx.index_ >= dex_file_->GetHeader().type_ids_size_)) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08001088 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx.index_ << " (max "
jeffhaod5347e02012-03-22 17:25:05 -07001089 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001090 return false;
1091 }
1092 int bracket_count = 0;
Ian Rogers0571d352011-11-03 19:51:38 -07001093 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -07001094 const char* cp = descriptor;
1095 while (*cp++ == '[') {
1096 bracket_count++;
1097 }
Andreas Gampe29e81422017-05-15 16:29:32 -07001098 if (UNLIKELY(bracket_count == 0)) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001099 /* The given class must be an array type. */
Brian Carlstrom93c33962013-07-26 10:37:43 -07001100 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1101 << "can't new-array class '" << descriptor << "' (not an array)";
Ian Rogersd81871c2011-10-03 13:57:23 -07001102 return false;
Andreas Gampe29e81422017-05-15 16:29:32 -07001103 } else if (UNLIKELY(bracket_count > 255)) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001104 /* It is illegal to create an array of more than 255 dimensions. */
Brian Carlstrom93c33962013-07-26 10:37:43 -07001105 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1106 << "can't new-array class '" << descriptor << "' (exceeds limit)";
Ian Rogersd81871c2011-10-03 13:57:23 -07001107 return false;
1108 }
1109 return true;
1110}
1111
Ian Rogers776ac1f2012-04-13 23:36:36 -07001112bool MethodVerifier::CheckArrayData(uint32_t cur_offset) {
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001113 const uint32_t insn_count = code_item_accessor_.InsnsSizeInCodeUnits();
1114 const uint16_t* insns = code_item_accessor_.Insns() + cur_offset;
Ian Rogersd81871c2011-10-03 13:57:23 -07001115 const uint16_t* array_data;
1116 int32_t array_data_offset;
1117
1118 DCHECK_LT(cur_offset, insn_count);
1119 /* make sure the start of the array data table is in range */
Andreas Gampe53de99c2015-08-17 13:43:55 -07001120 array_data_offset = insns[1] | (static_cast<int32_t>(insns[2]) << 16);
Andreas Gampe29e81422017-05-15 16:29:32 -07001121 if (UNLIKELY(static_cast<int32_t>(cur_offset) + array_data_offset < 0 ||
1122 cur_offset + array_data_offset + 2 >= insn_count)) {
jeffhaod5347e02012-03-22 17:25:05 -07001123 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data start: at " << cur_offset
Brian Carlstrom93c33962013-07-26 10:37:43 -07001124 << ", data offset " << array_data_offset
1125 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -07001126 return false;
1127 }
1128 /* offset to array data table is a relative branch-style offset */
1129 array_data = insns + array_data_offset;
Andreas Gampe57c47582015-07-01 22:05:59 -07001130 // Make sure the table is at an even dex pc, that is, 32-bit aligned.
Andreas Gampe29e81422017-05-15 16:29:32 -07001131 if (UNLIKELY(!IsAligned<4>(array_data))) {
jeffhaod5347e02012-03-22 17:25:05 -07001132 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned array data table: at " << cur_offset
1133 << ", data offset " << array_data_offset;
Ian Rogersd81871c2011-10-03 13:57:23 -07001134 return false;
1135 }
Andreas Gampe57c47582015-07-01 22:05:59 -07001136 // Make sure the array-data is marked as an opcode. This ensures that it was reached when
1137 // traversing the code item linearly. It is an approximation for a by-spec padding value.
Andreas Gampe29e81422017-05-15 16:29:32 -07001138 if (UNLIKELY(!GetInstructionFlags(cur_offset + array_data_offset).IsOpcode())) {
Andreas Gampe57c47582015-07-01 22:05:59 -07001139 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array data table at " << cur_offset
1140 << ", data offset " << array_data_offset
1141 << " not correctly visited, probably bad padding.";
1142 return false;
1143 }
1144
Ian Rogersd81871c2011-10-03 13:57:23 -07001145 uint32_t value_width = array_data[1];
Elliott Hughes398f64b2012-03-26 18:05:48 -07001146 uint32_t value_count = *reinterpret_cast<const uint32_t*>(&array_data[2]);
Ian Rogersd81871c2011-10-03 13:57:23 -07001147 uint32_t table_size = 4 + (value_width * value_count + 1) / 2;
1148 /* make sure the end of the switch is in range */
Andreas Gampe29e81422017-05-15 16:29:32 -07001149 if (UNLIKELY(cur_offset + array_data_offset + table_size > insn_count)) {
jeffhaod5347e02012-03-22 17:25:05 -07001150 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data end: at " << cur_offset
1151 << ", data offset " << array_data_offset << ", end "
1152 << cur_offset + array_data_offset + table_size
1153 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -07001154 return false;
1155 }
1156 return true;
1157}
1158
Ian Rogers776ac1f2012-04-13 23:36:36 -07001159bool MethodVerifier::CheckBranchTarget(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001160 int32_t offset;
1161 bool isConditional, selfOkay;
1162 if (!GetBranchOffset(cur_offset, &offset, &isConditional, &selfOkay)) {
1163 return false;
1164 }
Andreas Gampe29e81422017-05-15 16:29:32 -07001165 if (UNLIKELY(!selfOkay && offset == 0)) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001166 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch offset of zero not allowed at"
1167 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -07001168 return false;
1169 }
Elliott Hughes81ff3182012-03-23 20:35:56 -07001170 // Check for 32-bit overflow. This isn't strictly necessary if we can depend on the runtime
1171 // to have identical "wrap-around" behavior, but it's unwise to depend on that.
Andreas Gampe29e81422017-05-15 16:29:32 -07001172 if (UNLIKELY(((int64_t) cur_offset + (int64_t) offset) != (int64_t) (cur_offset + offset))) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001173 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch target overflow "
1174 << reinterpret_cast<void*>(cur_offset) << " +" << offset;
Ian Rogersd81871c2011-10-03 13:57:23 -07001175 return false;
1176 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001177 int32_t abs_offset = cur_offset + offset;
Andreas Gampe29e81422017-05-15 16:29:32 -07001178 if (UNLIKELY(abs_offset < 0 ||
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001179 (uint32_t) abs_offset >= code_item_accessor_.InsnsSizeInCodeUnits() ||
Andreas Gampe29e81422017-05-15 16:29:32 -07001180 !GetInstructionFlags(abs_offset).IsOpcode())) {
jeffhaod5347e02012-03-22 17:25:05 -07001181 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid branch target " << offset << " (-> "
Elliott Hughes398f64b2012-03-26 18:05:48 -07001182 << reinterpret_cast<void*>(abs_offset) << ") at "
1183 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -07001184 return false;
1185 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07001186 GetInstructionFlags(abs_offset).SetBranchTarget();
Ian Rogersd81871c2011-10-03 13:57:23 -07001187 return true;
1188}
1189
Ian Rogers776ac1f2012-04-13 23:36:36 -07001190bool MethodVerifier::GetBranchOffset(uint32_t cur_offset, int32_t* pOffset, bool* pConditional,
Ian Rogersd81871c2011-10-03 13:57:23 -07001191 bool* selfOkay) {
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001192 const uint16_t* insns = code_item_accessor_.Insns() + cur_offset;
Ian Rogersd81871c2011-10-03 13:57:23 -07001193 *pConditional = false;
1194 *selfOkay = false;
jeffhaoba5ebb92011-08-25 17:24:37 -07001195 switch (*insns & 0xff) {
1196 case Instruction::GOTO:
1197 *pOffset = ((int16_t) *insns) >> 8;
jeffhaoba5ebb92011-08-25 17:24:37 -07001198 break;
1199 case Instruction::GOTO_32:
1200 *pOffset = insns[1] | (((uint32_t) insns[2]) << 16);
jeffhaoba5ebb92011-08-25 17:24:37 -07001201 *selfOkay = true;
1202 break;
1203 case Instruction::GOTO_16:
1204 *pOffset = (int16_t) insns[1];
jeffhaoba5ebb92011-08-25 17:24:37 -07001205 break;
1206 case Instruction::IF_EQ:
1207 case Instruction::IF_NE:
1208 case Instruction::IF_LT:
1209 case Instruction::IF_GE:
1210 case Instruction::IF_GT:
1211 case Instruction::IF_LE:
1212 case Instruction::IF_EQZ:
1213 case Instruction::IF_NEZ:
1214 case Instruction::IF_LTZ:
1215 case Instruction::IF_GEZ:
1216 case Instruction::IF_GTZ:
1217 case Instruction::IF_LEZ:
1218 *pOffset = (int16_t) insns[1];
1219 *pConditional = true;
jeffhaoba5ebb92011-08-25 17:24:37 -07001220 break;
1221 default:
1222 return false;
jeffhaoba5ebb92011-08-25 17:24:37 -07001223 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001224 return true;
1225}
1226
Ian Rogers776ac1f2012-04-13 23:36:36 -07001227bool MethodVerifier::CheckSwitchTargets(uint32_t cur_offset) {
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001228 const uint32_t insn_count = code_item_accessor_.InsnsSizeInCodeUnits();
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001229 DCHECK_LT(cur_offset, insn_count);
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001230 const uint16_t* insns = code_item_accessor_.Insns() + cur_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001231 /* make sure the start of the switch is in range */
Andreas Gampe53de99c2015-08-17 13:43:55 -07001232 int32_t switch_offset = insns[1] | (static_cast<int32_t>(insns[2]) << 16);
Andreas Gampe29e81422017-05-15 16:29:32 -07001233 if (UNLIKELY(static_cast<int32_t>(cur_offset) + switch_offset < 0 ||
1234 cur_offset + switch_offset + 2 > insn_count)) {
jeffhaod5347e02012-03-22 17:25:05 -07001235 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch start: at " << cur_offset
Brian Carlstrom93c33962013-07-26 10:37:43 -07001236 << ", switch offset " << switch_offset
1237 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001238 return false;
1239 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001240 /* offset to switch table is a relative branch-style offset */
Ian Rogersd81871c2011-10-03 13:57:23 -07001241 const uint16_t* switch_insns = insns + switch_offset;
Andreas Gampe57c47582015-07-01 22:05:59 -07001242 // Make sure the table is at an even dex pc, that is, 32-bit aligned.
Andreas Gampe29e81422017-05-15 16:29:32 -07001243 if (UNLIKELY(!IsAligned<4>(switch_insns))) {
jeffhaod5347e02012-03-22 17:25:05 -07001244 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned switch table: at " << cur_offset
1245 << ", switch offset " << switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001246 return false;
1247 }
Andreas Gampe57c47582015-07-01 22:05:59 -07001248 // Make sure the switch data is marked as an opcode. This ensures that it was reached when
1249 // traversing the code item linearly. It is an approximation for a by-spec padding value.
Andreas Gampe29e81422017-05-15 16:29:32 -07001250 if (UNLIKELY(!GetInstructionFlags(cur_offset + switch_offset).IsOpcode())) {
Andreas Gampe57c47582015-07-01 22:05:59 -07001251 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "switch table at " << cur_offset
1252 << ", switch offset " << switch_offset
1253 << " not correctly visited, probably bad padding.";
1254 return false;
1255 }
1256
David Brazdil5469d342015-09-25 16:57:53 +01001257 bool is_packed_switch = (*insns & 0xff) == Instruction::PACKED_SWITCH;
1258
Ian Rogersd81871c2011-10-03 13:57:23 -07001259 uint32_t switch_count = switch_insns[1];
David Brazdil5469d342015-09-25 16:57:53 +01001260 int32_t targets_offset;
Ian Rogersd81871c2011-10-03 13:57:23 -07001261 uint16_t expected_signature;
David Brazdil5469d342015-09-25 16:57:53 +01001262 if (is_packed_switch) {
jeffhaoba5ebb92011-08-25 17:24:37 -07001263 /* 0=sig, 1=count, 2/3=firstKey */
1264 targets_offset = 4;
jeffhaoba5ebb92011-08-25 17:24:37 -07001265 expected_signature = Instruction::kPackedSwitchSignature;
1266 } else {
1267 /* 0=sig, 1=count, 2..count*2 = keys */
jeffhaoba5ebb92011-08-25 17:24:37 -07001268 targets_offset = 2 + 2 * switch_count;
1269 expected_signature = Instruction::kSparseSwitchSignature;
1270 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001271 uint32_t table_size = targets_offset + switch_count * 2;
Andreas Gampe29e81422017-05-15 16:29:32 -07001272 if (UNLIKELY(switch_insns[0] != expected_signature)) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001273 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1274 << StringPrintf("wrong signature for switch table (%x, wanted %x)",
1275 switch_insns[0], expected_signature);
jeffhaoba5ebb92011-08-25 17:24:37 -07001276 return false;
1277 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001278 /* make sure the end of the switch is in range */
Andreas Gampe29e81422017-05-15 16:29:32 -07001279 if (UNLIKELY(cur_offset + switch_offset + table_size > (uint32_t) insn_count)) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001280 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch end: at " << cur_offset
1281 << ", switch offset " << switch_offset
1282 << ", end " << (cur_offset + switch_offset + table_size)
jeffhaod5347e02012-03-22 17:25:05 -07001283 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001284 return false;
1285 }
David Brazdil5469d342015-09-25 16:57:53 +01001286
1287 constexpr int32_t keys_offset = 2;
1288 if (switch_count > 1) {
1289 if (is_packed_switch) {
1290 /* for a packed switch, verify that keys do not overflow int32 */
1291 int32_t first_key = switch_insns[keys_offset] | (switch_insns[keys_offset + 1] << 16);
1292 int32_t max_first_key =
1293 std::numeric_limits<int32_t>::max() - (static_cast<int32_t>(switch_count) - 1);
Andreas Gampe29e81422017-05-15 16:29:32 -07001294 if (UNLIKELY(first_key > max_first_key)) {
David Brazdil5469d342015-09-25 16:57:53 +01001295 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid packed switch: first_key=" << first_key
1296 << ", switch_count=" << switch_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001297 return false;
1298 }
David Brazdil5469d342015-09-25 16:57:53 +01001299 } else {
1300 /* for a sparse switch, verify the keys are in ascending order */
1301 int32_t last_key = switch_insns[keys_offset] | (switch_insns[keys_offset + 1] << 16);
1302 for (uint32_t targ = 1; targ < switch_count; targ++) {
1303 int32_t key =
1304 static_cast<int32_t>(switch_insns[keys_offset + targ * 2]) |
1305 static_cast<int32_t>(switch_insns[keys_offset + targ * 2 + 1] << 16);
Andreas Gampe29e81422017-05-15 16:29:32 -07001306 if (UNLIKELY(key <= last_key)) {
David Brazdil5469d342015-09-25 16:57:53 +01001307 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid sparse switch: last key=" << last_key
1308 << ", this=" << key;
1309 return false;
1310 }
1311 last_key = key;
1312 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001313 }
1314 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001315 /* verify each switch target */
Ian Rogersd81871c2011-10-03 13:57:23 -07001316 for (uint32_t targ = 0; targ < switch_count; targ++) {
Andreas Gampe53de99c2015-08-17 13:43:55 -07001317 int32_t offset = static_cast<int32_t>(switch_insns[targets_offset + targ * 2]) |
1318 static_cast<int32_t>(switch_insns[targets_offset + targ * 2 + 1] << 16);
Ian Rogersd81871c2011-10-03 13:57:23 -07001319 int32_t abs_offset = cur_offset + offset;
Andreas Gampe29e81422017-05-15 16:29:32 -07001320 if (UNLIKELY(abs_offset < 0 ||
1321 abs_offset >= static_cast<int32_t>(insn_count) ||
1322 !GetInstructionFlags(abs_offset).IsOpcode())) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001323 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch target " << offset
1324 << " (-> " << reinterpret_cast<void*>(abs_offset) << ") at "
1325 << reinterpret_cast<void*>(cur_offset)
1326 << "[" << targ << "]";
jeffhaoba5ebb92011-08-25 17:24:37 -07001327 return false;
1328 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07001329 GetInstructionFlags(abs_offset).SetBranchTarget();
Ian Rogersd81871c2011-10-03 13:57:23 -07001330 }
1331 return true;
1332}
1333
Ian Rogers776ac1f2012-04-13 23:36:36 -07001334bool MethodVerifier::CheckVarArgRegs(uint32_t vA, uint32_t arg[]) {
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001335 uint16_t registers_size = code_item_accessor_.RegistersSize();
Ian Rogersd81871c2011-10-03 13:57:23 -07001336 for (uint32_t idx = 0; idx < vA; idx++) {
Andreas Gampe29e81422017-05-15 16:29:32 -07001337 if (UNLIKELY(arg[idx] >= registers_size)) {
jeffhaod5347e02012-03-22 17:25:05 -07001338 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index (" << arg[idx]
1339 << ") in non-range invoke (>= " << registers_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001340 return false;
1341 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001342 }
1343
1344 return true;
1345}
1346
Ian Rogers776ac1f2012-04-13 23:36:36 -07001347bool MethodVerifier::CheckVarArgRangeRegs(uint32_t vA, uint32_t vC) {
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001348 uint16_t registers_size = code_item_accessor_.RegistersSize();
Ian Rogersd81871c2011-10-03 13:57:23 -07001349 // vA/vC are unsigned 8-bit/16-bit quantities for /range instructions, so there's no risk of
1350 // integer overflow when adding them here.
Andreas Gampe29e81422017-05-15 16:29:32 -07001351 if (UNLIKELY(vA + vC > registers_size)) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001352 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index " << vA << "+" << vC
1353 << " in range invoke (> " << registers_size << ")";
jeffhaoba5ebb92011-08-25 17:24:37 -07001354 return false;
1355 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001356 return true;
1357}
1358
Ian Rogers776ac1f2012-04-13 23:36:36 -07001359bool MethodVerifier::VerifyCodeFlow() {
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001360 const uint16_t registers_size = code_item_accessor_.RegistersSize();
jeffhaobdb76512011-09-07 11:43:16 -07001361
Ian Rogersd81871c2011-10-03 13:57:23 -07001362 /* Create and initialize table holding register status */
Brian Carlstrom93c33962013-07-26 10:37:43 -07001363 reg_table_.Init(kTrackCompilerInterestPoints,
1364 insn_flags_.get(),
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001365 code_item_accessor_.InsnsSizeInCodeUnits(),
Brian Carlstrom93c33962013-07-26 10:37:43 -07001366 registers_size,
1367 this);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07001368
Ian Rogersd0fbd852013-09-24 18:17:04 -07001369 work_line_.reset(RegisterLine::Create(registers_size, this));
1370 saved_line_.reset(RegisterLine::Create(registers_size, this));
jeffhaobdb76512011-09-07 11:43:16 -07001371
Ian Rogersd81871c2011-10-03 13:57:23 -07001372 /* Initialize register types of method arguments. */
1373 if (!SetTypesFromSignature()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001374 DCHECK_NE(failures_.size(), 0U);
1375 std::string prepend("Bad signature in ");
David Sehr709b0702016-10-13 09:12:37 -07001376 prepend += dex_file_->PrettyMethod(dex_method_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001377 PrependToLastFailMessage(prepend);
Ian Rogersd81871c2011-10-03 13:57:23 -07001378 return false;
1379 }
Andreas Gamped5ad72f2015-06-26 17:33:47 -07001380 // We may have a runtime failure here, clear.
1381 have_pending_runtime_throw_failure_ = false;
1382
Ian Rogersd81871c2011-10-03 13:57:23 -07001383 /* Perform code flow verification. */
1384 if (!CodeFlowVerifyMethod()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001385 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -07001386 return false;
jeffhaobdb76512011-09-07 11:43:16 -07001387 }
jeffhaobdb76512011-09-07 11:43:16 -07001388 return true;
1389}
1390
Ian Rogersad0b3a32012-04-16 14:50:24 -07001391std::ostream& MethodVerifier::DumpFailures(std::ostream& os) {
1392 DCHECK_EQ(failures_.size(), failure_messages_.size());
Jeff Hao4137f482013-11-22 11:44:57 -08001393 for (size_t i = 0; i < failures_.size(); ++i) {
1394 os << failure_messages_[i]->str() << "\n";
Ian Rogersad0b3a32012-04-16 14:50:24 -07001395 }
1396 return os;
1397}
1398
Ian Rogers776ac1f2012-04-13 23:36:36 -07001399void MethodVerifier::Dump(std::ostream& os) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001400 VariableIndentationOutputStream vios(&os);
1401 Dump(&vios);
1402}
1403
1404void MethodVerifier::Dump(VariableIndentationOutputStream* vios) {
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001405 if (!code_item_accessor_.HasCodeItem()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001406 vios->Stream() << "Native method\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001407 return;
jeffhaobdb76512011-09-07 11:43:16 -07001408 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001409 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001410 vios->Stream() << "Register Types:\n";
1411 ScopedIndentation indent1(vios);
1412 reg_types_.Dump(vios->Stream());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001413 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001414 vios->Stream() << "Dumping instructions and register lines:\n";
1415 ScopedIndentation indent1(vios);
Mathieu Chartier1d2d4ff2017-09-23 16:11:06 -07001416
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001417 for (const DexInstructionPcPair& inst : code_item_accessor_) {
Mathieu Chartier2b2bef22017-10-26 17:10:19 -07001418 const size_t dex_pc = inst.DexPc();
Andreas Gampe077d9db2018-01-19 18:54:14 -08001419
1420 // Might be asked to dump before the table is initialized.
1421 if (reg_table_.IsInitialized()) {
1422 RegisterLine* reg_line = reg_table_.GetLine(dex_pc);
1423 if (reg_line != nullptr) {
1424 vios->Stream() << reg_line->Dump(this) << "\n";
1425 }
jeffhaobdb76512011-09-07 11:43:16 -07001426 }
Andreas Gampe077d9db2018-01-19 18:54:14 -08001427
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001428 vios->Stream()
Mathieu Chartierde40d472015-10-15 17:47:48 -07001429 << StringPrintf("0x%04zx", dex_pc) << ": " << GetInstructionFlags(dex_pc).ToString() << " ";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001430 const bool kDumpHexOfInstruction = false;
1431 if (kDumpHexOfInstruction) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001432 vios->Stream() << inst->DumpHex(5) << " ";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001433 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001434 vios->Stream() << inst->DumpString(dex_file_) << "\n";
jeffhaoba5ebb92011-08-25 17:24:37 -07001435 }
jeffhaobdb76512011-09-07 11:43:16 -07001436}
1437
Ian Rogersd81871c2011-10-03 13:57:23 -07001438static bool IsPrimitiveDescriptor(char descriptor) {
1439 switch (descriptor) {
jeffhaobdb76512011-09-07 11:43:16 -07001440 case 'I':
1441 case 'C':
1442 case 'S':
1443 case 'B':
1444 case 'Z':
jeffhaobdb76512011-09-07 11:43:16 -07001445 case 'F':
1446 case 'D':
1447 case 'J':
Ian Rogersd81871c2011-10-03 13:57:23 -07001448 return true;
jeffhaobdb76512011-09-07 11:43:16 -07001449 default:
1450 return false;
1451 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001452}
1453
Ian Rogers776ac1f2012-04-13 23:36:36 -07001454bool MethodVerifier::SetTypesFromSignature() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001455 RegisterLine* reg_line = reg_table_.GetLine(0);
Andreas Gampeef0b1a12015-06-19 20:37:46 -07001456
1457 // Should have been verified earlier.
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001458 DCHECK_GE(code_item_accessor_.RegistersSize(), code_item_accessor_.InsSize());
Andreas Gampeef0b1a12015-06-19 20:37:46 -07001459
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001460 uint32_t arg_start = code_item_accessor_.RegistersSize() - code_item_accessor_.InsSize();
1461 size_t expected_args = code_item_accessor_.InsSize(); /* long/double count as two */
jeffhaobdb76512011-09-07 11:43:16 -07001462
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001463 // Include the "this" pointer.
Ian Rogersd81871c2011-10-03 13:57:23 -07001464 size_t cur_arg = 0;
Ian Rogersad0b3a32012-04-16 14:50:24 -07001465 if (!IsStatic()) {
Andreas Gampeef0b1a12015-06-19 20:37:46 -07001466 if (expected_args == 0) {
1467 // Expect at least a receiver.
1468 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected 0 args, but method is not static";
1469 return false;
1470 }
1471
Ian Rogersd81871c2011-10-03 13:57:23 -07001472 // If this is a constructor for a class other than java.lang.Object, mark the first ("this")
1473 // argument as uninitialized. This restricts field access until the superclass constructor is
1474 // called.
Ian Rogersd8f69b02014-09-10 21:43:52 +00001475 const RegType& declaring_class = GetDeclaringClass();
Andreas Gampef10b6e12015-08-12 10:48:12 -07001476 if (IsConstructor()) {
1477 if (declaring_class.IsJavaLangObject()) {
1478 // "this" is implicitly initialized.
1479 reg_line->SetThisInitialized();
Andreas Gampead238ce2015-08-24 21:13:08 -07001480 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, declaring_class);
Andreas Gampef10b6e12015-08-12 10:48:12 -07001481 } else {
Andreas Gampead238ce2015-08-24 21:13:08 -07001482 reg_line->SetRegisterType<LockOp::kClear>(
1483 this,
1484 arg_start + cur_arg,
1485 reg_types_.UninitializedThisArgument(declaring_class));
Andreas Gampef10b6e12015-08-12 10:48:12 -07001486 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001487 } else {
Andreas Gampead238ce2015-08-24 21:13:08 -07001488 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, declaring_class);
jeffhaobdb76512011-09-07 11:43:16 -07001489 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001490 cur_arg++;
jeffhaobdb76512011-09-07 11:43:16 -07001491 }
1492
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001493 const dex::ProtoId& proto_id =
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001494 dex_file_->GetMethodPrototype(dex_file_->GetMethodId(dex_method_idx_));
Ian Rogers0571d352011-11-03 19:51:38 -07001495 DexFileParameterIterator iterator(*dex_file_, proto_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07001496
1497 for (; iterator.HasNext(); iterator.Next()) {
1498 const char* descriptor = iterator.GetDescriptor();
Ian Rogers7b078e82014-09-10 14:44:24 -07001499 if (descriptor == nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001500 LOG(FATAL) << "Null descriptor";
1501 }
1502 if (cur_arg >= expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07001503 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1504 << " args, found more (" << descriptor << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001505 return false;
1506 }
1507 switch (descriptor[0]) {
1508 case 'L':
1509 case '[':
1510 // We assume that reference arguments are initialized. The only way it could be otherwise
1511 // (assuming the caller was verified) is if the current method is <init>, but in that case
1512 // it's effectively considered initialized the instant we reach here (in the sense that we
1513 // can return without doing anything or call virtual methods).
1514 {
Andreas Gampe98be1a92017-08-28 08:25:45 -07001515 // Note: don't check access. No error would be thrown for declaring or passing an
1516 // inaccessible class. Only actual accesses to fields or methods will.
1517 const RegType& reg_type = ResolveClass<CheckAccess::kNo>(iterator.GetTypeIdx());
Sebastien Hertz2ed76f92014-04-22 17:11:08 +02001518 if (!reg_type.IsNonZeroReferenceTypes()) {
1519 DCHECK(HasFailures());
1520 return false;
1521 }
Andreas Gampead238ce2015-08-24 21:13:08 -07001522 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001523 }
1524 break;
1525 case 'Z':
Andreas Gampead238ce2015-08-24 21:13:08 -07001526 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Boolean());
Ian Rogersd81871c2011-10-03 13:57:23 -07001527 break;
1528 case 'C':
Andreas Gampead238ce2015-08-24 21:13:08 -07001529 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Char());
Ian Rogersd81871c2011-10-03 13:57:23 -07001530 break;
1531 case 'B':
Andreas Gampead238ce2015-08-24 21:13:08 -07001532 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Byte());
Ian Rogersd81871c2011-10-03 13:57:23 -07001533 break;
1534 case 'I':
Andreas Gampead238ce2015-08-24 21:13:08 -07001535 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Integer());
Ian Rogersd81871c2011-10-03 13:57:23 -07001536 break;
1537 case 'S':
Andreas Gampead238ce2015-08-24 21:13:08 -07001538 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Short());
Ian Rogersd81871c2011-10-03 13:57:23 -07001539 break;
1540 case 'F':
Andreas Gampead238ce2015-08-24 21:13:08 -07001541 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Float());
Ian Rogersd81871c2011-10-03 13:57:23 -07001542 break;
1543 case 'J':
1544 case 'D': {
Andreas Gampe77cd4d62014-06-19 17:29:48 -07001545 if (cur_arg + 1 >= expected_args) {
1546 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1547 << " args, found more (" << descriptor << ")";
1548 return false;
1549 }
1550
Ian Rogers7b078e82014-09-10 14:44:24 -07001551 const RegType* lo_half;
1552 const RegType* hi_half;
1553 if (descriptor[0] == 'J') {
1554 lo_half = &reg_types_.LongLo();
1555 hi_half = &reg_types_.LongHi();
1556 } else {
1557 lo_half = &reg_types_.DoubleLo();
1558 hi_half = &reg_types_.DoubleHi();
1559 }
1560 reg_line->SetRegisterTypeWide(this, arg_start + cur_arg, *lo_half, *hi_half);
Ian Rogersd81871c2011-10-03 13:57:23 -07001561 cur_arg++;
1562 break;
1563 }
1564 default:
Brian Carlstrom93c33962013-07-26 10:37:43 -07001565 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected signature type char '"
1566 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001567 return false;
1568 }
1569 cur_arg++;
1570 }
1571 if (cur_arg != expected_args) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001572 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1573 << " arguments, found " << cur_arg;
Ian Rogersd81871c2011-10-03 13:57:23 -07001574 return false;
1575 }
1576 const char* descriptor = dex_file_->GetReturnTypeDescriptor(proto_id);
1577 // Validate return type. We don't do the type lookup; just want to make sure that it has the right
1578 // format. Only major difference from the method argument format is that 'V' is supported.
1579 bool result;
1580 if (IsPrimitiveDescriptor(descriptor[0]) || descriptor[0] == 'V') {
1581 result = descriptor[1] == '\0';
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001582 } else if (descriptor[0] == '[') { // single/multi-dimensional array of object/primitive
Ian Rogersd81871c2011-10-03 13:57:23 -07001583 size_t i = 0;
1584 do {
1585 i++;
1586 } while (descriptor[i] == '['); // process leading [
1587 if (descriptor[i] == 'L') { // object array
1588 do {
1589 i++; // find closing ;
1590 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1591 result = descriptor[i] == ';';
1592 } else { // primitive array
1593 result = IsPrimitiveDescriptor(descriptor[i]) && descriptor[i + 1] == '\0';
1594 }
1595 } else if (descriptor[0] == 'L') {
1596 // could be more thorough here, but shouldn't be required
1597 size_t i = 0;
1598 do {
1599 i++;
1600 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1601 result = descriptor[i] == ';';
1602 } else {
1603 result = false;
1604 }
1605 if (!result) {
jeffhaod5347e02012-03-22 17:25:05 -07001606 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected char in return type descriptor '"
1607 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001608 }
1609 return result;
jeffhaobdb76512011-09-07 11:43:16 -07001610}
1611
Ian Rogers776ac1f2012-04-13 23:36:36 -07001612bool MethodVerifier::CodeFlowVerifyMethod() {
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001613 const uint16_t* insns = code_item_accessor_.Insns();
1614 const uint32_t insns_size = code_item_accessor_.InsnsSizeInCodeUnits();
jeffhaoba5ebb92011-08-25 17:24:37 -07001615
jeffhaobdb76512011-09-07 11:43:16 -07001616 /* Begin by marking the first instruction as "changed". */
Mathieu Chartierde40d472015-10-15 17:47:48 -07001617 GetInstructionFlags(0).SetChanged();
Ian Rogersd81871c2011-10-03 13:57:23 -07001618 uint32_t start_guess = 0;
jeffhaoba5ebb92011-08-25 17:24:37 -07001619
jeffhaobdb76512011-09-07 11:43:16 -07001620 /* Continue until no instructions are marked "changed". */
1621 while (true) {
Mathieu Chartier4306ef82014-12-19 18:41:47 -08001622 if (allow_thread_suspension_) {
1623 self_->AllowThreadSuspension();
1624 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001625 // Find the first marked one. Use "start_guess" as a way to find one quickly.
1626 uint32_t insn_idx = start_guess;
1627 for (; insn_idx < insns_size; insn_idx++) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07001628 if (GetInstructionFlags(insn_idx).IsChanged())
jeffhaobdb76512011-09-07 11:43:16 -07001629 break;
1630 }
jeffhaobdb76512011-09-07 11:43:16 -07001631 if (insn_idx == insns_size) {
1632 if (start_guess != 0) {
1633 /* try again, starting from the top */
1634 start_guess = 0;
1635 continue;
1636 } else {
1637 /* all flags are clear */
1638 break;
1639 }
1640 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001641 // We carry the working set of registers from instruction to instruction. If this address can
1642 // be the target of a branch (or throw) instruction, or if we're skipping around chasing
1643 // "changed" flags, we need to load the set of registers from the table.
1644 // Because we always prefer to continue on to the next instruction, we should never have a
1645 // situation where we have a stray "changed" flag set on an instruction that isn't a branch
1646 // target.
1647 work_insn_idx_ = insn_idx;
Mathieu Chartierde40d472015-10-15 17:47:48 -07001648 if (GetInstructionFlags(insn_idx).IsBranchTarget()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001649 work_line_->CopyFromLine(reg_table_.GetLine(insn_idx));
Ian Rogersebbdd872014-07-07 23:53:08 -07001650 } else if (kIsDebugBuild) {
jeffhaobdb76512011-09-07 11:43:16 -07001651 /*
1652 * Sanity check: retrieve the stored register line (assuming
1653 * a full table) and make sure it actually matches.
1654 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001655 RegisterLine* register_line = reg_table_.GetLine(insn_idx);
Ian Rogers7b078e82014-09-10 14:44:24 -07001656 if (register_line != nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001657 if (work_line_->CompareLine(register_line) != 0) {
Andreas Gampe4a57d5f2019-04-03 12:31:18 -07001658 Dump(LOG_STREAM(FATAL_WITHOUT_ABORT));
1659 LOG(FATAL_WITHOUT_ABORT) << info_messages_.str();
David Sehr709b0702016-10-13 09:12:37 -07001660 LOG(FATAL) << "work_line diverged in " << dex_file_->PrettyMethod(dex_method_idx_)
Elliott Hughesc073b072012-05-24 19:29:17 -07001661 << "@" << reinterpret_cast<void*>(work_insn_idx_) << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07001662 << " work_line=" << work_line_->Dump(this) << "\n"
1663 << " expected=" << register_line->Dump(this);
Ian Rogersd81871c2011-10-03 13:57:23 -07001664 }
jeffhaobdb76512011-09-07 11:43:16 -07001665 }
jeffhaobdb76512011-09-07 11:43:16 -07001666 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001667 if (!CodeFlowVerifyInstruction(&start_guess)) {
David Sehr709b0702016-10-13 09:12:37 -07001668 std::string prepend(dex_file_->PrettyMethod(dex_method_idx_));
Ian Rogersad0b3a32012-04-16 14:50:24 -07001669 prepend += " failed to verify: ";
1670 PrependToLastFailMessage(prepend);
jeffhaoba5ebb92011-08-25 17:24:37 -07001671 return false;
1672 }
jeffhaobdb76512011-09-07 11:43:16 -07001673 /* Clear "changed" and mark as visited. */
Mathieu Chartierde40d472015-10-15 17:47:48 -07001674 GetInstructionFlags(insn_idx).SetVisited();
1675 GetInstructionFlags(insn_idx).ClearChanged();
jeffhaobdb76512011-09-07 11:43:16 -07001676 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001677
Andreas Gampe92d77202017-12-06 20:49:00 -08001678 if (UNLIKELY(VLOG_IS_ON(verifier_debug))) {
jeffhaobdb76512011-09-07 11:43:16 -07001679 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001680 * Scan for dead code. There's nothing "evil" about dead code
jeffhaobdb76512011-09-07 11:43:16 -07001681 * (besides the wasted space), but it indicates a flaw somewhere
1682 * down the line, possibly in the verifier.
1683 *
1684 * If we've substituted "always throw" instructions into the stream,
1685 * we are almost certainly going to have some dead code.
1686 */
1687 int dead_start = -1;
Mathieu Chartier1d2d4ff2017-09-23 16:11:06 -07001688
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001689 for (const DexInstructionPcPair& inst : code_item_accessor_) {
Mathieu Chartier2b2bef22017-10-26 17:10:19 -07001690 const uint32_t insn_idx = inst.DexPc();
jeffhaobdb76512011-09-07 11:43:16 -07001691 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001692 * Switch-statement data doesn't get "visited" by scanner. It
jeffhaobdb76512011-09-07 11:43:16 -07001693 * may or may not be preceded by a padding NOP (for alignment).
1694 */
1695 if (insns[insn_idx] == Instruction::kPackedSwitchSignature ||
1696 insns[insn_idx] == Instruction::kSparseSwitchSignature ||
1697 insns[insn_idx] == Instruction::kArrayDataSignature ||
Elliott Hughes380aaa72012-07-09 14:33:15 -07001698 (insns[insn_idx] == Instruction::NOP && (insn_idx + 1 < insns_size) &&
jeffhaobdb76512011-09-07 11:43:16 -07001699 (insns[insn_idx + 1] == Instruction::kPackedSwitchSignature ||
1700 insns[insn_idx + 1] == Instruction::kSparseSwitchSignature ||
1701 insns[insn_idx + 1] == Instruction::kArrayDataSignature))) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07001702 GetInstructionFlags(insn_idx).SetVisited();
jeffhaobdb76512011-09-07 11:43:16 -07001703 }
1704
Mathieu Chartierde40d472015-10-15 17:47:48 -07001705 if (!GetInstructionFlags(insn_idx).IsVisited()) {
Mathieu Chartier1d2d4ff2017-09-23 16:11:06 -07001706 if (dead_start < 0) {
jeffhaobdb76512011-09-07 11:43:16 -07001707 dead_start = insn_idx;
Mathieu Chartier1d2d4ff2017-09-23 16:11:06 -07001708 }
jeffhaobdb76512011-09-07 11:43:16 -07001709 } else if (dead_start >= 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001710 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start)
1711 << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaobdb76512011-09-07 11:43:16 -07001712 dead_start = -1;
1713 }
1714 }
1715 if (dead_start >= 0) {
Mathieu Chartier1d2d4ff2017-09-23 16:11:06 -07001716 LogVerifyInfo()
1717 << "dead code " << reinterpret_cast<void*>(dead_start)
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001718 << "-" << reinterpret_cast<void*>(code_item_accessor_.InsnsSizeInCodeUnits() - 1);
jeffhaoba5ebb92011-08-25 17:24:37 -07001719 }
Ian Rogersc9e463c2013-06-05 16:52:26 -07001720 // To dump the state of the verify after a method, do something like:
David Sehr709b0702016-10-13 09:12:37 -07001721 // if (dex_file_->PrettyMethod(dex_method_idx_) ==
Ian Rogersc9e463c2013-06-05 16:52:26 -07001722 // "boolean java.lang.String.equals(java.lang.Object)") {
1723 // LOG(INFO) << info_messages_.str();
1724 // }
jeffhaoba5ebb92011-08-25 17:24:37 -07001725 }
jeffhaobdb76512011-09-07 11:43:16 -07001726 return true;
1727}
1728
Andreas Gampe68df3202015-06-22 11:35:46 -07001729// Returns the index of the first final instance field of the given class, or kDexNoIndex if there
1730// is no such field.
Andreas Gampea5b09a62016-11-17 15:21:22 -08001731static uint32_t GetFirstFinalInstanceFieldIndex(const DexFile& dex_file, dex::TypeIndex type_idx) {
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001732 const dex::ClassDef* class_def = dex_file.FindClassDef(type_idx);
Andreas Gampe68df3202015-06-22 11:35:46 -07001733 DCHECK(class_def != nullptr);
Mathieu Chartier1f1cb9f2018-06-04 09:22:46 -07001734 ClassAccessor accessor(dex_file, *class_def);
1735 for (const ClassAccessor::Field& field : accessor.GetInstanceFields()) {
1736 if (field.IsFinal()) {
1737 return field.GetIndex();
Andreas Gampe68df3202015-06-22 11:35:46 -07001738 }
Andreas Gampe68df3202015-06-22 11:35:46 -07001739 }
Andreas Gampee2abbc62017-09-15 11:59:26 -07001740 return dex::kDexNoIndex;
Andreas Gampe68df3202015-06-22 11:35:46 -07001741}
1742
Andreas Gampea727e372015-08-25 09:22:37 -07001743// Setup a register line for the given return instruction.
1744static void AdjustReturnLine(MethodVerifier* verifier,
1745 const Instruction* ret_inst,
1746 RegisterLine* line) {
1747 Instruction::Code opcode = ret_inst->Opcode();
1748
1749 switch (opcode) {
1750 case Instruction::RETURN_VOID:
1751 case Instruction::RETURN_VOID_NO_BARRIER:
1752 SafelyMarkAllRegistersAsConflicts(verifier, line);
1753 break;
1754
1755 case Instruction::RETURN:
1756 case Instruction::RETURN_OBJECT:
1757 line->MarkAllRegistersAsConflictsExcept(verifier, ret_inst->VRegA_11x());
1758 break;
1759
1760 case Instruction::RETURN_WIDE:
1761 line->MarkAllRegistersAsConflictsExceptWide(verifier, ret_inst->VRegA_11x());
1762 break;
1763
1764 default:
1765 LOG(FATAL) << "Unknown return opcode " << opcode;
1766 UNREACHABLE();
1767 }
1768}
1769
Ian Rogers776ac1f2012-04-13 23:36:36 -07001770bool MethodVerifier::CodeFlowVerifyInstruction(uint32_t* start_guess) {
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001771 // If we're doing FindLocksAtDexPc, check whether we're at the dex pc we care about.
1772 // We want the state _before_ the instruction, for the case where the dex pc we're
1773 // interested in is itself a monitor-enter instruction (which is a likely place
1774 // for a thread to be suspended).
Ian Rogers7b078e82014-09-10 14:44:24 -07001775 if (monitor_enter_dex_pcs_ != nullptr && work_insn_idx_ == interesting_dex_pc_) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001776 monitor_enter_dex_pcs_->clear(); // The new work line is more accurate than the previous one.
Andreas Gampeaaf0d382017-11-27 14:10:21 -08001777
1778 std::map<uint32_t, DexLockInfo> depth_to_lock_info;
1779 auto collector = [&](uint32_t dex_reg, uint32_t depth) {
1780 auto insert_pair = depth_to_lock_info.emplace(depth, DexLockInfo(depth));
1781 auto it = insert_pair.first;
1782 auto set_insert_pair = it->second.dex_registers.insert(dex_reg);
1783 DCHECK(set_insert_pair.second);
1784 };
1785 work_line_->IterateRegToLockDepths(collector);
1786 for (auto& pair : depth_to_lock_info) {
1787 monitor_enter_dex_pcs_->push_back(pair.second);
1788 // Map depth to dex PC.
1789 (*monitor_enter_dex_pcs_)[monitor_enter_dex_pcs_->size() - 1].dex_pc =
1790 work_line_->GetMonitorEnterDexPc(pair.second.dex_pc);
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001791 }
1792 }
1793
jeffhaobdb76512011-09-07 11:43:16 -07001794 /*
1795 * Once we finish decoding the instruction, we need to figure out where
jeffhaod1f0fde2011-09-08 17:25:33 -07001796 * we can go from here. There are three possible ways to transfer
jeffhaobdb76512011-09-07 11:43:16 -07001797 * control to another statement:
1798 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001799 * (1) Continue to the next instruction. Applies to all but
jeffhaobdb76512011-09-07 11:43:16 -07001800 * unconditional branches, method returns, and exception throws.
jeffhaod1f0fde2011-09-08 17:25:33 -07001801 * (2) Branch to one or more possible locations. Applies to branches
jeffhaobdb76512011-09-07 11:43:16 -07001802 * and switch statements.
jeffhaod1f0fde2011-09-08 17:25:33 -07001803 * (3) Exception handlers. Applies to any instruction that can
jeffhaobdb76512011-09-07 11:43:16 -07001804 * throw an exception that is handled by an encompassing "try"
1805 * block.
1806 *
1807 * We can also return, in which case there is no successor instruction
1808 * from this point.
1809 *
Elliott Hughesadb8c672012-03-06 16:49:32 -08001810 * The behavior can be determined from the opcode flags.
jeffhaobdb76512011-09-07 11:43:16 -07001811 */
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001812 const uint16_t* insns = code_item_accessor_.Insns() + work_insn_idx_;
Ian Rogersd81871c2011-10-03 13:57:23 -07001813 const Instruction* inst = Instruction::At(insns);
Ian Rogersa75a0132012-09-28 11:41:42 -07001814 int opcode_flags = Instruction::FlagsOf(inst->Opcode());
jeffhaobdb76512011-09-07 11:43:16 -07001815
jeffhaobdb76512011-09-07 11:43:16 -07001816 int32_t branch_target = 0;
jeffhaobdb76512011-09-07 11:43:16 -07001817 bool just_set_result = false;
Andreas Gampe92d77202017-12-06 20:49:00 -08001818 if (UNLIKELY(VLOG_IS_ON(verifier_debug))) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001819 // Generate processing back trace to debug verifier
Andreas Gampe2ad6cce2019-04-11 16:17:39 -07001820 LogVerifyInfo() << "Processing " << inst->DumpString(dex_file_) << std::endl
1821 << work_line_->Dump(this);
Ian Rogersd81871c2011-10-03 13:57:23 -07001822 }
jeffhaobdb76512011-09-07 11:43:16 -07001823
1824 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001825 * Make a copy of the previous register state. If the instruction
jeffhaobdb76512011-09-07 11:43:16 -07001826 * can throw an exception, we will copy/merge this into the "catch"
1827 * address rather than work_line, because we don't want the result
1828 * from the "successful" code path (e.g. a check-cast that "improves"
1829 * a type) to be visible to the exception handler.
1830 */
Ian Rogers776ac1f2012-04-13 23:36:36 -07001831 if ((opcode_flags & Instruction::kThrow) != 0 && CurrentInsnFlags()->IsInTry()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001832 saved_line_->CopyFromLine(work_line_.get());
Ian Rogers1ff3c982014-08-12 02:30:58 -07001833 } else if (kIsDebugBuild) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001834 saved_line_->FillWithGarbage();
jeffhaobdb76512011-09-07 11:43:16 -07001835 }
Andreas Gamped12e7822015-06-25 10:26:40 -07001836 DCHECK(!have_pending_runtime_throw_failure_); // Per-instruction flag, should not be set here.
jeffhaobdb76512011-09-07 11:43:16 -07001837
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07001838
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001839 // We need to ensure the work line is consistent while performing validation. When we spot a
1840 // peephole pattern we compute a new line for either the fallthrough instruction or the
1841 // branch target.
Mathieu Chartier361e04a2016-02-16 14:06:35 -08001842 RegisterLineArenaUniquePtr branch_line;
1843 RegisterLineArenaUniquePtr fallthrough_line;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001844
Sebastien Hertz5243e912013-05-21 10:55:07 +02001845 switch (inst->Opcode()) {
jeffhaobdb76512011-09-07 11:43:16 -07001846 case Instruction::NOP:
1847 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001848 * A "pure" NOP has no effect on anything. Data tables start with
jeffhaobdb76512011-09-07 11:43:16 -07001849 * a signature that looks like a NOP; if we see one of these in
1850 * the course of executing code then we have a problem.
1851 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001852 if (inst->VRegA_10x() != 0) {
jeffhaod5347e02012-03-22 17:25:05 -07001853 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "encountered data table in instruction stream";
jeffhaobdb76512011-09-07 11:43:16 -07001854 }
1855 break;
1856
1857 case Instruction::MOVE:
Ian Rogers7b078e82014-09-10 14:44:24 -07001858 work_line_->CopyRegister1(this, inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategory1nr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001859 break;
jeffhaobdb76512011-09-07 11:43:16 -07001860 case Instruction::MOVE_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001861 work_line_->CopyRegister1(this, inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategory1nr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001862 break;
jeffhaobdb76512011-09-07 11:43:16 -07001863 case Instruction::MOVE_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001864 work_line_->CopyRegister1(this, inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategory1nr);
jeffhaobdb76512011-09-07 11:43:16 -07001865 break;
1866 case Instruction::MOVE_WIDE:
Ian Rogers7b078e82014-09-10 14:44:24 -07001867 work_line_->CopyRegister2(this, inst->VRegA_12x(), inst->VRegB_12x());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001868 break;
jeffhaobdb76512011-09-07 11:43:16 -07001869 case Instruction::MOVE_WIDE_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001870 work_line_->CopyRegister2(this, inst->VRegA_22x(), inst->VRegB_22x());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001871 break;
jeffhaobdb76512011-09-07 11:43:16 -07001872 case Instruction::MOVE_WIDE_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001873 work_line_->CopyRegister2(this, inst->VRegA_32x(), inst->VRegB_32x());
jeffhaobdb76512011-09-07 11:43:16 -07001874 break;
1875 case Instruction::MOVE_OBJECT:
Ian Rogers7b078e82014-09-10 14:44:24 -07001876 work_line_->CopyRegister1(this, inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategoryRef);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001877 break;
jeffhaobdb76512011-09-07 11:43:16 -07001878 case Instruction::MOVE_OBJECT_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001879 work_line_->CopyRegister1(this, inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategoryRef);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001880 break;
jeffhaobdb76512011-09-07 11:43:16 -07001881 case Instruction::MOVE_OBJECT_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001882 work_line_->CopyRegister1(this, inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategoryRef);
jeffhaobdb76512011-09-07 11:43:16 -07001883 break;
1884
1885 /*
1886 * The move-result instructions copy data out of a "pseudo-register"
jeffhaod1f0fde2011-09-08 17:25:33 -07001887 * with the results from the last method invocation. In practice we
jeffhaobdb76512011-09-07 11:43:16 -07001888 * might want to hold the result in an actual CPU register, so the
1889 * Dalvik spec requires that these only appear immediately after an
1890 * invoke or filled-new-array.
1891 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001892 * These calls invalidate the "result" register. (This is now
jeffhaobdb76512011-09-07 11:43:16 -07001893 * redundant with the reset done below, but it can make the debug info
1894 * easier to read in some cases.)
1895 */
1896 case Instruction::MOVE_RESULT:
Ian Rogers7b078e82014-09-10 14:44:24 -07001897 work_line_->CopyResultRegister1(this, inst->VRegA_11x(), false);
jeffhaobdb76512011-09-07 11:43:16 -07001898 break;
1899 case Instruction::MOVE_RESULT_WIDE:
Ian Rogers7b078e82014-09-10 14:44:24 -07001900 work_line_->CopyResultRegister2(this, inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07001901 break;
1902 case Instruction::MOVE_RESULT_OBJECT:
Ian Rogers7b078e82014-09-10 14:44:24 -07001903 work_line_->CopyResultRegister1(this, inst->VRegA_11x(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001904 break;
1905
Ian Rogersd81871c2011-10-03 13:57:23 -07001906 case Instruction::MOVE_EXCEPTION: {
Sebastien Hertz270a0e12015-01-16 19:49:09 +01001907 // We do not allow MOVE_EXCEPTION as the first instruction in a method. This is a simple case
1908 // where one entrypoint to the catch block is not actually an exception path.
1909 if (work_insn_idx_ == 0) {
1910 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "move-exception at pc 0x0";
1911 break;
1912 }
jeffhaobdb76512011-09-07 11:43:16 -07001913 /*
jeffhao60f83e32012-02-13 17:16:30 -08001914 * This statement can only appear as the first instruction in an exception handler. We verify
1915 * that as part of extracting the exception type from the catch block list.
jeffhaobdb76512011-09-07 11:43:16 -07001916 */
Ian Rogersd8f69b02014-09-10 21:43:52 +00001917 const RegType& res_type = GetCaughtExceptionType();
Andreas Gampead238ce2015-08-24 21:13:08 -07001918 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_11x(), res_type);
jeffhaobdb76512011-09-07 11:43:16 -07001919 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001920 }
jeffhaobdb76512011-09-07 11:43:16 -07001921 case Instruction::RETURN_VOID:
Andreas Gampef10b6e12015-08-12 10:48:12 -07001922 if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001923 if (!GetMethodReturnType().IsConflict()) {
jeffhaod5347e02012-03-22 17:25:05 -07001924 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001925 }
jeffhaobdb76512011-09-07 11:43:16 -07001926 }
1927 break;
1928 case Instruction::RETURN:
Andreas Gampef10b6e12015-08-12 10:48:12 -07001929 if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) {
jeffhaobdb76512011-09-07 11:43:16 -07001930 /* check the method signature */
Ian Rogersd8f69b02014-09-10 21:43:52 +00001931 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07001932 if (!return_type.IsCategory1Types()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001933 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected non-category 1 return type "
1934 << return_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001935 } else {
1936 // Compilers may generate synthetic functions that write byte values into boolean fields.
1937 // Also, it may use integer values for boolean, byte, short, and character return types.
Sebastien Hertz5243e912013-05-21 10:55:07 +02001938 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07001939 const RegType& src_type = work_line_->GetRegisterType(this, vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07001940 bool use_src = ((return_type.IsBoolean() && src_type.IsByte()) ||
1941 ((return_type.IsBoolean() || return_type.IsByte() ||
1942 return_type.IsShort() || return_type.IsChar()) &&
1943 src_type.IsInteger()));
1944 /* check the register contents */
Ian Rogersad0b3a32012-04-16 14:50:24 -07001945 bool success =
Ian Rogers7b078e82014-09-10 14:44:24 -07001946 work_line_->VerifyRegisterType(this, vregA, use_src ? src_type : return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001947 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001948 AppendToLastFailMessage(StringPrintf(" return-1nr on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07001949 }
jeffhaobdb76512011-09-07 11:43:16 -07001950 }
1951 }
1952 break;
1953 case Instruction::RETURN_WIDE:
Andreas Gampef10b6e12015-08-12 10:48:12 -07001954 if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) {
jeffhaobdb76512011-09-07 11:43:16 -07001955 /* check the method signature */
Ian Rogersd8f69b02014-09-10 21:43:52 +00001956 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07001957 if (!return_type.IsCategory2Types()) {
jeffhaod5347e02012-03-22 17:25:05 -07001958 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-wide not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001959 } else {
1960 /* check the register contents */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001961 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07001962 bool success = work_line_->VerifyRegisterType(this, vregA, return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001963 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001964 AppendToLastFailMessage(StringPrintf(" return-wide on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07001965 }
jeffhaobdb76512011-09-07 11:43:16 -07001966 }
1967 }
1968 break;
1969 case Instruction::RETURN_OBJECT:
Andreas Gampef10b6e12015-08-12 10:48:12 -07001970 if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) {
Ian Rogersd8f69b02014-09-10 21:43:52 +00001971 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07001972 if (!return_type.IsReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001973 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-object not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001974 } else {
1975 /* return_type is the *expected* return type, not register value */
Andreas Gampeeed3a5d2017-11-29 14:58:34 -08001976 DCHECK(!return_type.IsZeroOrNull());
Ian Rogersd81871c2011-10-03 13:57:23 -07001977 DCHECK(!return_type.IsUninitializedReference());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001978 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07001979 const RegType& reg_type = work_line_->GetRegisterType(this, vregA);
Andreas Gampea32210c2015-06-24 10:26:13 -07001980 // Disallow returning undefined, conflict & uninitialized values and verify that the
1981 // reference in vAA is an instance of the "return_type."
1982 if (reg_type.IsUndefined()) {
1983 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning undefined register";
1984 } else if (reg_type.IsConflict()) {
1985 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning register with conflict";
1986 } else if (reg_type.IsUninitializedTypes()) {
David Brazdil68b5c0b2016-01-19 14:25:29 +00001987 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning uninitialized object '"
Brian Carlstrom93c33962013-07-26 10:37:43 -07001988 << reg_type << "'";
Andreas Gampea4c98f22015-11-06 16:24:49 -08001989 } else if (!reg_type.IsReferenceTypes()) {
1990 // We really do expect a reference here.
1991 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-object returns a non-reference type "
1992 << reg_type;
David Brazdilca3c8c32016-09-06 14:04:48 +01001993 } else if (!return_type.IsAssignableFrom(reg_type, this)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07001994 if (reg_type.IsUnresolvedTypes() || return_type.IsUnresolvedTypes()) {
1995 Fail(VERIFY_ERROR_NO_CLASS) << " can't resolve returned type '" << return_type
1996 << "' or '" << reg_type << "'";
1997 } else {
Andreas Gampe16f149c2015-03-23 10:10:20 -07001998 bool soft_error = false;
1999 // Check whether arrays are involved. They will show a valid class status, even
2000 // if their components are erroneous.
2001 if (reg_type.IsArrayTypes() && return_type.IsArrayTypes()) {
David Brazdilca3c8c32016-09-06 14:04:48 +01002002 return_type.CanAssignArray(reg_type, reg_types_, class_loader_, this, &soft_error);
Andreas Gampe16f149c2015-03-23 10:10:20 -07002003 if (soft_error) {
2004 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "array with erroneous component type: "
2005 << reg_type << " vs " << return_type;
2006 }
2007 }
2008
2009 if (!soft_error) {
2010 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning '" << reg_type
2011 << "', but expected from declaration '" << return_type << "'";
2012 }
Jeff Haoa3faaf42013-09-03 19:07:00 -07002013 }
jeffhaobdb76512011-09-07 11:43:16 -07002014 }
2015 }
2016 }
2017 break;
2018
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07002019 /* could be boolean, int, float, or a null reference */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002020 case Instruction::CONST_4: {
2021 int32_t val = static_cast<int32_t>(inst->VRegB_11n() << 28) >> 28;
Andreas Gampead238ce2015-08-24 21:13:08 -07002022 work_line_->SetRegisterType<LockOp::kClear>(
2023 this, inst->VRegA_11n(), DetermineCat1Constant(val, need_precise_constants_));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07002024 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02002025 }
2026 case Instruction::CONST_16: {
2027 int16_t val = static_cast<int16_t>(inst->VRegB_21s());
Andreas Gampead238ce2015-08-24 21:13:08 -07002028 work_line_->SetRegisterType<LockOp::kClear>(
2029 this, inst->VRegA_21s(), DetermineCat1Constant(val, need_precise_constants_));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07002030 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02002031 }
Sebastien Hertz849600b2013-12-20 10:28:08 +01002032 case Instruction::CONST: {
2033 int32_t val = inst->VRegB_31i();
Andreas Gampead238ce2015-08-24 21:13:08 -07002034 work_line_->SetRegisterType<LockOp::kClear>(
2035 this, inst->VRegA_31i(), DetermineCat1Constant(val, need_precise_constants_));
jeffhaobdb76512011-09-07 11:43:16 -07002036 break;
Sebastien Hertz849600b2013-12-20 10:28:08 +01002037 }
2038 case Instruction::CONST_HIGH16: {
2039 int32_t val = static_cast<int32_t>(inst->VRegB_21h() << 16);
Andreas Gampead238ce2015-08-24 21:13:08 -07002040 work_line_->SetRegisterType<LockOp::kClear>(
2041 this, inst->VRegA_21h(), DetermineCat1Constant(val, need_precise_constants_));
jeffhaobdb76512011-09-07 11:43:16 -07002042 break;
Sebastien Hertz849600b2013-12-20 10:28:08 +01002043 }
jeffhaobdb76512011-09-07 11:43:16 -07002044 /* could be long or double; resolved upon use */
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002045 case Instruction::CONST_WIDE_16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002046 int64_t val = static_cast<int16_t>(inst->VRegB_21s());
Ian Rogersd8f69b02014-09-10 21:43:52 +00002047 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
2048 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07002049 work_line_->SetRegisterTypeWide(this, inst->VRegA_21s(), lo, hi);
jeffhaobdb76512011-09-07 11:43:16 -07002050 break;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002051 }
2052 case Instruction::CONST_WIDE_32: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002053 int64_t val = static_cast<int32_t>(inst->VRegB_31i());
Ian Rogersd8f69b02014-09-10 21:43:52 +00002054 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
2055 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07002056 work_line_->SetRegisterTypeWide(this, inst->VRegA_31i(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002057 break;
2058 }
2059 case Instruction::CONST_WIDE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002060 int64_t val = inst->VRegB_51l();
Ian Rogersd8f69b02014-09-10 21:43:52 +00002061 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
2062 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07002063 work_line_->SetRegisterTypeWide(this, inst->VRegA_51l(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002064 break;
2065 }
2066 case Instruction::CONST_WIDE_HIGH16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002067 int64_t val = static_cast<uint64_t>(inst->VRegB_21h()) << 48;
Ian Rogersd8f69b02014-09-10 21:43:52 +00002068 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
2069 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07002070 work_line_->SetRegisterTypeWide(this, inst->VRegA_21h(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002071 break;
2072 }
jeffhaobdb76512011-09-07 11:43:16 -07002073 case Instruction::CONST_STRING:
Andreas Gampead238ce2015-08-24 21:13:08 -07002074 work_line_->SetRegisterType<LockOp::kClear>(
2075 this, inst->VRegA_21c(), reg_types_.JavaLangString());
Sebastien Hertz5243e912013-05-21 10:55:07 +02002076 break;
jeffhaobdb76512011-09-07 11:43:16 -07002077 case Instruction::CONST_STRING_JUMBO:
Andreas Gampead238ce2015-08-24 21:13:08 -07002078 work_line_->SetRegisterType<LockOp::kClear>(
2079 this, inst->VRegA_31c(), reg_types_.JavaLangString());
jeffhaobdb76512011-09-07 11:43:16 -07002080 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002081 case Instruction::CONST_CLASS: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002082 // Get type from instruction if unresolved then we need an access check
2083 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Andreas Gampe98be1a92017-08-28 08:25:45 -07002084 const RegType& res_type = ResolveClass<CheckAccess::kYes>(dex::TypeIndex(inst->VRegB_21c()));
Ian Rogersad0b3a32012-04-16 14:50:24 -07002085 // Register holds class, ie its type is class, on error it will hold Conflict.
Andreas Gampead238ce2015-08-24 21:13:08 -07002086 work_line_->SetRegisterType<LockOp::kClear>(
2087 this, inst->VRegA_21c(), res_type.IsConflict() ? res_type
2088 : reg_types_.JavaLangClass());
jeffhaobdb76512011-09-07 11:43:16 -07002089 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002090 }
Orion Hodson2e599942017-09-22 16:17:41 +01002091 case Instruction::CONST_METHOD_HANDLE:
2092 work_line_->SetRegisterType<LockOp::kClear>(
2093 this, inst->VRegA_21c(), reg_types_.JavaLangInvokeMethodHandle());
Orion Hodson2e599942017-09-22 16:17:41 +01002094 break;
2095 case Instruction::CONST_METHOD_TYPE:
2096 work_line_->SetRegisterType<LockOp::kClear>(
2097 this, inst->VRegA_21c(), reg_types_.JavaLangInvokeMethodType());
Orion Hodson2e599942017-09-22 16:17:41 +01002098 break;
jeffhaobdb76512011-09-07 11:43:16 -07002099 case Instruction::MONITOR_ENTER:
Ian Rogers7b078e82014-09-10 14:44:24 -07002100 work_line_->PushMonitor(this, inst->VRegA_11x(), work_insn_idx_);
Andreas Gampec1474102015-08-18 08:57:44 -07002101 // Check whether the previous instruction is a move-object with vAA as a source, creating
2102 // untracked lock aliasing.
Mathieu Chartierde40d472015-10-15 17:47:48 -07002103 if (0 != work_insn_idx_ && !GetInstructionFlags(work_insn_idx_).IsBranchTarget()) {
Andreas Gampec1474102015-08-18 08:57:44 -07002104 uint32_t prev_idx = work_insn_idx_ - 1;
Mathieu Chartierde40d472015-10-15 17:47:48 -07002105 while (0 != prev_idx && !GetInstructionFlags(prev_idx).IsOpcode()) {
Andreas Gampec1474102015-08-18 08:57:44 -07002106 prev_idx--;
2107 }
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08002108 const Instruction& prev_inst = code_item_accessor_.InstructionAt(prev_idx);
Mathieu Chartier1d2d4ff2017-09-23 16:11:06 -07002109 switch (prev_inst.Opcode()) {
Andreas Gampec1474102015-08-18 08:57:44 -07002110 case Instruction::MOVE_OBJECT:
2111 case Instruction::MOVE_OBJECT_16:
2112 case Instruction::MOVE_OBJECT_FROM16:
Mathieu Chartier1d2d4ff2017-09-23 16:11:06 -07002113 if (prev_inst.VRegB() == inst->VRegA_11x()) {
Andreas Gampec1474102015-08-18 08:57:44 -07002114 // Redo the copy. This won't change the register types, but update the lock status
2115 // for the aliased register.
2116 work_line_->CopyRegister1(this,
Mathieu Chartier1d2d4ff2017-09-23 16:11:06 -07002117 prev_inst.VRegA(),
2118 prev_inst.VRegB(),
Andreas Gampec1474102015-08-18 08:57:44 -07002119 kTypeCategoryRef);
2120 }
2121 break;
2122
Alexey Grebenkince750492018-05-31 23:42:20 +03002123 // Catch a case of register aliasing when two registers are linked to the same
2124 // java.lang.Class object via two consequent const-class instructions immediately
2125 // preceding monitor-enter called on one of those registers.
2126 case Instruction::CONST_CLASS: {
2127 // Get the second previous instruction.
2128 if (prev_idx == 0 || GetInstructionFlags(prev_idx).IsBranchTarget()) {
2129 break;
2130 }
2131 prev_idx--;
2132 while (0 != prev_idx && !GetInstructionFlags(prev_idx).IsOpcode()) {
2133 prev_idx--;
2134 }
2135 const Instruction& prev2_inst = code_item_accessor_.InstructionAt(prev_idx);
2136
2137 // Match the pattern "const-class; const-class; monitor-enter;"
2138 if (prev2_inst.Opcode() != Instruction::CONST_CLASS) {
2139 break;
2140 }
2141
2142 // Ensure both const-classes are called for the same type_idx.
2143 if (prev_inst.VRegB_21c() != prev2_inst.VRegB_21c()) {
2144 break;
2145 }
2146
2147 // Update the lock status for the aliased register.
2148 if (prev_inst.VRegA() == inst->VRegA_11x()) {
2149 work_line_->CopyRegister1(this,
2150 prev2_inst.VRegA(),
2151 inst->VRegA_11x(),
2152 kTypeCategoryRef);
2153 } else if (prev2_inst.VRegA() == inst->VRegA_11x()) {
2154 work_line_->CopyRegister1(this,
2155 prev_inst.VRegA(),
2156 inst->VRegA_11x(),
2157 kTypeCategoryRef);
2158 }
2159 break;
2160 }
2161
Andreas Gampec1474102015-08-18 08:57:44 -07002162 default: // Other instruction types ignored.
2163 break;
2164 }
2165 }
jeffhaobdb76512011-09-07 11:43:16 -07002166 break;
2167 case Instruction::MONITOR_EXIT:
2168 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002169 * monitor-exit instructions are odd. They can throw exceptions,
jeffhaobdb76512011-09-07 11:43:16 -07002170 * but when they do they act as if they succeeded and the PC is
jeffhaod1f0fde2011-09-08 17:25:33 -07002171 * pointing to the following instruction. (This behavior goes back
jeffhaobdb76512011-09-07 11:43:16 -07002172 * to the need to handle asynchronous exceptions, a now-deprecated
2173 * feature that Dalvik doesn't support.)
2174 *
jeffhaod1f0fde2011-09-08 17:25:33 -07002175 * In practice we don't need to worry about this. The only
jeffhaobdb76512011-09-07 11:43:16 -07002176 * exceptions that can be thrown from monitor-exit are for a
jeffhaod1f0fde2011-09-08 17:25:33 -07002177 * null reference and -exit without a matching -enter. If the
jeffhaobdb76512011-09-07 11:43:16 -07002178 * structured locking checks are working, the former would have
2179 * failed on the -enter instruction, and the latter is impossible.
2180 *
2181 * This is fortunate, because issue 3221411 prevents us from
2182 * chasing the "can throw" path when monitor verification is
jeffhaod1f0fde2011-09-08 17:25:33 -07002183 * enabled. If we can fully verify the locking we can ignore
jeffhaobdb76512011-09-07 11:43:16 -07002184 * some catch blocks (which will show up as "dead" code when
2185 * we skip them here); if we can't, then the code path could be
2186 * "live" so we still need to check it.
2187 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002188 opcode_flags &= ~Instruction::kThrow;
Ian Rogers7b078e82014-09-10 14:44:24 -07002189 work_line_->PopMonitor(this, inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07002190 break;
Ian Rogers28ad40d2011-10-27 15:19:26 -07002191 case Instruction::CHECK_CAST:
Ian Rogersd81871c2011-10-03 13:57:23 -07002192 case Instruction::INSTANCE_OF: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002193 /*
2194 * If this instruction succeeds, we will "downcast" register vA to the type in vB. (This
2195 * could be a "upcast" -- not expected, so we don't try to address it.)
2196 *
2197 * If it fails, an exception is thrown, which we deal with later by ignoring the update to
Elliott Hughesadb8c672012-03-06 16:49:32 -08002198 * dec_insn.vA when branching to a handler.
Ian Rogers28ad40d2011-10-27 15:19:26 -07002199 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002200 const bool is_checkcast = (inst->Opcode() == Instruction::CHECK_CAST);
Andreas Gampea5b09a62016-11-17 15:21:22 -08002201 const dex::TypeIndex type_idx((is_checkcast) ? inst->VRegB_21c() : inst->VRegC_22c());
Andreas Gampe98be1a92017-08-28 08:25:45 -07002202 const RegType& res_type = ResolveClass<CheckAccess::kYes>(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002203 if (res_type.IsConflict()) {
Andreas Gampe00633eb2014-07-17 16:13:35 -07002204 // If this is a primitive type, fail HARD.
Vladimir Marko666ee3d2017-12-11 18:37:36 +00002205 ObjPtr<mirror::Class> klass = Runtime::Current()->GetClassLinker()->LookupResolvedType(
2206 type_idx, dex_cache_.Get(), class_loader_.Get());
Andreas Gampe00633eb2014-07-17 16:13:35 -07002207 if (klass != nullptr && klass->IsPrimitive()) {
2208 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "using primitive type "
2209 << dex_file_->StringByTypeIdx(type_idx) << " in instanceof in "
2210 << GetDeclaringClass();
2211 break;
2212 }
2213
Ian Rogersad0b3a32012-04-16 14:50:24 -07002214 DCHECK_NE(failures_.size(), 0U);
2215 if (!is_checkcast) {
Andreas Gampead238ce2015-08-24 21:13:08 -07002216 work_line_->SetRegisterType<LockOp::kClear>(this,
2217 inst->VRegA_22c(),
2218 reg_types_.Boolean());
Ian Rogersad0b3a32012-04-16 14:50:24 -07002219 }
2220 break; // bad class
Ian Rogers9f1ab122011-12-12 08:52:43 -08002221 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002222 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Sebastien Hertz5243e912013-05-21 10:55:07 +02002223 uint32_t orig_type_reg = (is_checkcast) ? inst->VRegA_21c() : inst->VRegB_22c();
Ian Rogers7b078e82014-09-10 14:44:24 -07002224 const RegType& orig_type = work_line_->GetRegisterType(this, orig_type_reg);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002225 if (!res_type.IsNonZeroReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002226 if (is_checkcast) {
2227 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on unexpected class " << res_type;
2228 } else {
2229 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on unexpected class " << res_type;
2230 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002231 } else if (!orig_type.IsReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002232 if (is_checkcast) {
2233 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on non-reference in v" << orig_type_reg;
2234 } else {
2235 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on non-reference in v" << orig_type_reg;
2236 }
David Brazdil68b5c0b2016-01-19 14:25:29 +00002237 } else if (orig_type.IsUninitializedTypes()) {
2238 if (is_checkcast) {
2239 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on uninitialized reference in v"
2240 << orig_type_reg;
2241 } else {
2242 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on uninitialized reference in v"
2243 << orig_type_reg;
2244 }
jeffhao2a8a90e2011-09-26 14:25:31 -07002245 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002246 if (is_checkcast) {
Andreas Gampead238ce2015-08-24 21:13:08 -07002247 work_line_->SetRegisterType<LockOp::kKeep>(this, inst->VRegA_21c(), res_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07002248 } else {
Andreas Gampead238ce2015-08-24 21:13:08 -07002249 work_line_->SetRegisterType<LockOp::kClear>(this,
2250 inst->VRegA_22c(),
2251 reg_types_.Boolean());
jeffhaobdb76512011-09-07 11:43:16 -07002252 }
jeffhaobdb76512011-09-07 11:43:16 -07002253 }
jeffhao2a8a90e2011-09-26 14:25:31 -07002254 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002255 }
2256 case Instruction::ARRAY_LENGTH: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002257 const RegType& res_type = work_line_->GetRegisterType(this, inst->VRegB_12x());
Ian Rogers28ad40d2011-10-27 15:19:26 -07002258 if (res_type.IsReferenceTypes()) {
Andreas Gampeeed3a5d2017-11-29 14:58:34 -08002259 if (!res_type.IsArrayTypes() && !res_type.IsZeroOrNull()) {
2260 // ie not an array or null
jeffhaod5347e02012-03-22 17:25:05 -07002261 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002262 } else {
Andreas Gampead238ce2015-08-24 21:13:08 -07002263 work_line_->SetRegisterType<LockOp::kClear>(this,
2264 inst->VRegA_12x(),
2265 reg_types_.Integer());
Ian Rogersd81871c2011-10-03 13:57:23 -07002266 }
Andreas Gampe65c9db82014-07-28 13:14:34 -07002267 } else {
2268 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002269 }
2270 break;
2271 }
2272 case Instruction::NEW_INSTANCE: {
Andreas Gampe98be1a92017-08-28 08:25:45 -07002273 const RegType& res_type = ResolveClass<CheckAccess::kYes>(dex::TypeIndex(inst->VRegB_21c()));
Ian Rogersad0b3a32012-04-16 14:50:24 -07002274 if (res_type.IsConflict()) {
2275 DCHECK_NE(failures_.size(), 0U);
2276 break; // bad class
jeffhao8cd6dda2012-02-22 10:15:34 -08002277 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002278 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
2279 // can't create an instance of an interface or abstract class */
2280 if (!res_type.IsInstantiableTypes()) {
2281 Fail(VERIFY_ERROR_INSTANTIATION)
2282 << "new-instance on primitive, interface or abstract class" << res_type;
Ian Rogers08f753d2012-08-24 14:35:25 -07002283 // Soft failure so carry on to set register type.
Ian Rogersd81871c2011-10-03 13:57:23 -07002284 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00002285 const RegType& uninit_type = reg_types_.Uninitialized(res_type, work_insn_idx_);
Ian Rogers08f753d2012-08-24 14:35:25 -07002286 // Any registers holding previous allocations from this address that have not yet been
2287 // initialized must be marked invalid.
Ian Rogers7b078e82014-09-10 14:44:24 -07002288 work_line_->MarkUninitRefsAsInvalid(this, uninit_type);
Ian Rogers08f753d2012-08-24 14:35:25 -07002289 // add the new uninitialized reference to the register state
Andreas Gampead238ce2015-08-24 21:13:08 -07002290 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_21c(), uninit_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07002291 break;
2292 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08002293 case Instruction::NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002294 VerifyNewArray(inst, false, false);
jeffhaobdb76512011-09-07 11:43:16 -07002295 break;
2296 case Instruction::FILLED_NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002297 VerifyNewArray(inst, true, false);
Ian Rogers0c4a5062012-02-03 15:18:59 -08002298 just_set_result = true; // Filled new array sets result register
jeffhaobdb76512011-09-07 11:43:16 -07002299 break;
Ian Rogers0c4a5062012-02-03 15:18:59 -08002300 case Instruction::FILLED_NEW_ARRAY_RANGE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002301 VerifyNewArray(inst, true, true);
Ian Rogers0c4a5062012-02-03 15:18:59 -08002302 just_set_result = true; // Filled new array range sets result register
2303 break;
jeffhaobdb76512011-09-07 11:43:16 -07002304 case Instruction::CMPL_FLOAT:
2305 case Instruction::CMPG_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002306 if (!work_line_->VerifyRegisterType(this, inst->VRegB_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08002307 break;
2308 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002309 if (!work_line_->VerifyRegisterType(this, inst->VRegC_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08002310 break;
2311 }
Andreas Gampead238ce2015-08-24 21:13:08 -07002312 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002313 break;
2314 case Instruction::CMPL_DOUBLE:
2315 case Instruction::CMPG_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002316 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegB_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002317 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002318 break;
2319 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002320 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegC_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002321 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002322 break;
2323 }
Andreas Gampead238ce2015-08-24 21:13:08 -07002324 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002325 break;
2326 case Instruction::CMP_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002327 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegB_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002328 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002329 break;
2330 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002331 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegC_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002332 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002333 break;
2334 }
Andreas Gampead238ce2015-08-24 21:13:08 -07002335 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002336 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002337 case Instruction::THROW: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002338 const RegType& res_type = work_line_->GetRegisterType(this, inst->VRegA_11x());
David Brazdilca3c8c32016-09-06 14:04:48 +01002339 if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(res_type, this)) {
David Brazdil68b5c0b2016-01-19 14:25:29 +00002340 if (res_type.IsUninitializedTypes()) {
2341 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "thrown exception not initialized";
Pavel Vyssotski980027c2016-02-11 20:28:11 +06002342 } else if (!res_type.IsReferenceTypes()) {
2343 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "thrown value of non-reference type " << res_type;
David Brazdil68b5c0b2016-01-19 14:25:29 +00002344 } else {
2345 Fail(res_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS : VERIFY_ERROR_BAD_CLASS_SOFT)
2346 << "thrown class " << res_type << " not instanceof Throwable";
2347 }
jeffhaobdb76512011-09-07 11:43:16 -07002348 }
2349 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002350 }
jeffhaobdb76512011-09-07 11:43:16 -07002351 case Instruction::GOTO:
2352 case Instruction::GOTO_16:
2353 case Instruction::GOTO_32:
2354 /* no effect on or use of registers */
2355 break;
2356
2357 case Instruction::PACKED_SWITCH:
2358 case Instruction::SPARSE_SWITCH:
2359 /* verify that vAA is an integer, or can be converted to one */
Ian Rogers7b078e82014-09-10 14:44:24 -07002360 work_line_->VerifyRegisterType(this, inst->VRegA_31t(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002361 break;
2362
Ian Rogersd81871c2011-10-03 13:57:23 -07002363 case Instruction::FILL_ARRAY_DATA: {
2364 /* Similar to the verification done for APUT */
Ian Rogers7b078e82014-09-10 14:44:24 -07002365 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegA_31t());
Ian Rogers89310de2012-02-01 13:47:30 -08002366 /* array_type can be null if the reg type is Zero */
Andreas Gampeeed3a5d2017-11-29 14:58:34 -08002367 if (!array_type.IsZeroOrNull()) {
jeffhao457cc512012-02-02 16:55:13 -08002368 if (!array_type.IsArrayTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002369 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with array type "
2370 << array_type;
Andreas Gampebb18a032016-03-22 20:34:25 -07002371 } else if (array_type.IsUnresolvedTypes()) {
2372 // If it's an unresolved array type, it must be non-primitive.
2373 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data for array of type "
2374 << array_type;
Ian Rogers89310de2012-02-01 13:47:30 -08002375 } else {
Ian Rogersd8f69b02014-09-10 21:43:52 +00002376 const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
Ian Rogersad0b3a32012-04-16 14:50:24 -07002377 DCHECK(!component_type.IsConflict());
jeffhao457cc512012-02-02 16:55:13 -08002378 if (component_type.IsNonZeroReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002379 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with component type "
2380 << component_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002381 } else {
jeffhao457cc512012-02-02 16:55:13 -08002382 // Now verify if the element width in the table matches the element width declared in
2383 // the array
Andreas Gampe53de99c2015-08-17 13:43:55 -07002384 const uint16_t* array_data =
2385 insns + (insns[1] | (static_cast<int32_t>(insns[2]) << 16));
jeffhao457cc512012-02-02 16:55:13 -08002386 if (array_data[0] != Instruction::kArrayDataSignature) {
jeffhaod5347e02012-03-22 17:25:05 -07002387 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid magic for array-data";
jeffhao457cc512012-02-02 16:55:13 -08002388 } else {
2389 size_t elem_width = Primitive::ComponentSize(component_type.GetPrimitiveType());
2390 // Since we don't compress the data in Dex, expect to see equal width of data stored
2391 // in the table and expected from the array class.
2392 if (array_data[1] != elem_width) {
jeffhaod5347e02012-03-22 17:25:05 -07002393 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-data size mismatch (" << array_data[1]
2394 << " vs " << elem_width << ")";
jeffhao457cc512012-02-02 16:55:13 -08002395 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002396 }
2397 }
jeffhaobdb76512011-09-07 11:43:16 -07002398 }
2399 }
2400 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002401 }
jeffhaobdb76512011-09-07 11:43:16 -07002402 case Instruction::IF_EQ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002403 case Instruction::IF_NE: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002404 const RegType& reg_type1 = work_line_->GetRegisterType(this, inst->VRegA_22t());
2405 const RegType& reg_type2 = work_line_->GetRegisterType(this, inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002406 bool mismatch = false;
Andreas Gampeeed3a5d2017-11-29 14:58:34 -08002407 if (reg_type1.IsZeroOrNull()) { // zero then integral or reference expected
Ian Rogersd81871c2011-10-03 13:57:23 -07002408 mismatch = !reg_type2.IsReferenceTypes() && !reg_type2.IsIntegralTypes();
2409 } else if (reg_type1.IsReferenceTypes()) { // both references?
2410 mismatch = !reg_type2.IsReferenceTypes();
2411 } else { // both integral?
2412 mismatch = !reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes();
2413 }
2414 if (mismatch) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002415 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to if-eq/if-ne (" << reg_type1 << ","
2416 << reg_type2 << ") must both be references or integral";
jeffhaobdb76512011-09-07 11:43:16 -07002417 }
2418 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002419 }
jeffhaobdb76512011-09-07 11:43:16 -07002420 case Instruction::IF_LT:
2421 case Instruction::IF_GE:
2422 case Instruction::IF_GT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002423 case Instruction::IF_LE: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002424 const RegType& reg_type1 = work_line_->GetRegisterType(this, inst->VRegA_22t());
2425 const RegType& reg_type2 = work_line_->GetRegisterType(this, inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002426 if (!reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002427 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to 'if' (" << reg_type1 << ","
2428 << reg_type2 << ") must be integral";
jeffhaobdb76512011-09-07 11:43:16 -07002429 }
2430 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002431 }
jeffhaobdb76512011-09-07 11:43:16 -07002432 case Instruction::IF_EQZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002433 case Instruction::IF_NEZ: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002434 const RegType& reg_type = work_line_->GetRegisterType(this, inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002435 if (!reg_type.IsReferenceTypes() && !reg_type.IsIntegralTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002436 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
2437 << " unexpected as arg to if-eqz/if-nez";
Ian Rogersd81871c2011-10-03 13:57:23 -07002438 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002439
2440 // Find previous instruction - its existence is a precondition to peephole optimization.
Ian Rogers9b360392013-06-06 14:45:07 -07002441 uint32_t instance_of_idx = 0;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002442 if (0 != work_insn_idx_) {
Ian Rogers9b360392013-06-06 14:45:07 -07002443 instance_of_idx = work_insn_idx_ - 1;
Mathieu Chartierde40d472015-10-15 17:47:48 -07002444 while (0 != instance_of_idx && !GetInstructionFlags(instance_of_idx).IsOpcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07002445 instance_of_idx--;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002446 }
Mathieu Chartierc2a2bda2018-03-12 11:21:52 -07002447 if (FailOrAbort(GetInstructionFlags(instance_of_idx).IsOpcode(),
Andreas Gampe7c038102014-10-27 20:08:46 -07002448 "Unable to get previous instruction of if-eqz/if-nez for work index ",
2449 work_insn_idx_)) {
2450 break;
2451 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002452 } else {
2453 break;
2454 }
2455
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08002456 const Instruction& instance_of_inst = code_item_accessor_.InstructionAt(instance_of_idx);
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002457
2458 /* Check for peep-hole pattern of:
2459 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002460 * instance-of vX, vY, T;
2461 * ifXXX vX, label ;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002462 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002463 * label:
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002464 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002465 * and sharpen the type of vY to be type T.
2466 * Note, this pattern can't be if:
2467 * - if there are other branches to this branch,
2468 * - when vX == vY.
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002469 */
Ian Rogersfae370a2013-06-05 08:33:27 -07002470 if (!CurrentInsnFlags()->IsBranchTarget() &&
Vladimir Markod7559b72017-09-28 13:50:37 +01002471 (Instruction::INSTANCE_OF == instance_of_inst.Opcode()) &&
2472 (inst->VRegA_21t() == instance_of_inst.VRegA_22c()) &&
2473 (instance_of_inst.VRegA_22c() != instance_of_inst.VRegB_22c())) {
Ian Rogersebbdd872014-07-07 23:53:08 -07002474 // Check the type of the instance-of is different than that of registers type, as if they
2475 // are the same there is no work to be done here. Check that the conversion is not to or
2476 // from an unresolved type as type information is imprecise. If the instance-of is to an
2477 // interface then ignore the type information as interfaces can only be treated as Objects
2478 // and we don't want to disallow field and other operations on the object. If the value
2479 // being instance-of checked against is known null (zero) then allow the optimization as
2480 // we didn't have type information. If the merge of the instance-of type with the original
2481 // type is assignable to the original then allow optimization. This check is performed to
2482 // ensure that subsequent merges don't lose type information - such as becoming an
2483 // interface from a class that would lose information relevant to field checks.
Vladimir Markod7559b72017-09-28 13:50:37 +01002484 const RegType& orig_type = work_line_->GetRegisterType(this, instance_of_inst.VRegB_22c());
Andreas Gampe98be1a92017-08-28 08:25:45 -07002485 const RegType& cast_type = ResolveClass<CheckAccess::kYes>(
Vladimir Markod7559b72017-09-28 13:50:37 +01002486 dex::TypeIndex(instance_of_inst.VRegC_22c()));
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002487
Ian Rogersebbdd872014-07-07 23:53:08 -07002488 if (!orig_type.Equals(cast_type) &&
2489 !cast_type.IsUnresolvedTypes() && !orig_type.IsUnresolvedTypes() &&
Andreas Gampe00633eb2014-07-17 16:13:35 -07002490 cast_type.HasClass() && // Could be conflict type, make sure it has a class.
Ian Rogersebbdd872014-07-07 23:53:08 -07002491 !cast_type.GetClass()->IsInterface() &&
Andreas Gampeeed3a5d2017-11-29 14:58:34 -08002492 (orig_type.IsZeroOrNull() ||
David Brazdilca3c8c32016-09-06 14:04:48 +01002493 orig_type.IsStrictlyAssignableFrom(
2494 cast_type.Merge(orig_type, &reg_types_, this), this))) {
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08002495 RegisterLine* update_line = RegisterLine::Create(code_item_accessor_.RegistersSize(),
2496 this);
Ian Rogersfae370a2013-06-05 08:33:27 -07002497 if (inst->Opcode() == Instruction::IF_EQZ) {
Ian Rogers9b360392013-06-06 14:45:07 -07002498 fallthrough_line.reset(update_line);
Ian Rogersfae370a2013-06-05 08:33:27 -07002499 } else {
Ian Rogers9b360392013-06-06 14:45:07 -07002500 branch_line.reset(update_line);
2501 }
2502 update_line->CopyFromLine(work_line_.get());
Andreas Gampead238ce2015-08-24 21:13:08 -07002503 update_line->SetRegisterType<LockOp::kKeep>(this,
Vladimir Markod7559b72017-09-28 13:50:37 +01002504 instance_of_inst.VRegB_22c(),
Andreas Gampead238ce2015-08-24 21:13:08 -07002505 cast_type);
Mathieu Chartierde40d472015-10-15 17:47:48 -07002506 if (!GetInstructionFlags(instance_of_idx).IsBranchTarget() && 0 != instance_of_idx) {
Ian Rogers9b360392013-06-06 14:45:07 -07002507 // See if instance-of was preceded by a move-object operation, common due to the small
2508 // register encoding space of instance-of, and propagate type information to the source
2509 // of the move-object.
Andreas Gampe43e43252019-01-08 12:06:57 -08002510 // Note: this is only valid if the move source was not clobbered.
Ian Rogers9b360392013-06-06 14:45:07 -07002511 uint32_t move_idx = instance_of_idx - 1;
Mathieu Chartierde40d472015-10-15 17:47:48 -07002512 while (0 != move_idx && !GetInstructionFlags(move_idx).IsOpcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07002513 move_idx--;
2514 }
Mathieu Chartierc2a2bda2018-03-12 11:21:52 -07002515 if (FailOrAbort(GetInstructionFlags(move_idx).IsOpcode(),
Andreas Gampe7c038102014-10-27 20:08:46 -07002516 "Unable to get previous instruction of if-eqz/if-nez for work index ",
2517 work_insn_idx_)) {
2518 break;
2519 }
Andreas Gampe43e43252019-01-08 12:06:57 -08002520 auto maybe_update_fn = [&instance_of_inst, update_line, this, &cast_type](
2521 uint16_t move_src,
2522 uint16_t move_trg)
2523 REQUIRES_SHARED(Locks::mutator_lock_) {
2524 if (move_trg == instance_of_inst.VRegB_22c() &&
2525 move_src != instance_of_inst.VRegA_22c()) {
2526 update_line->SetRegisterType<LockOp::kKeep>(this, move_src, cast_type);
2527 }
2528 };
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08002529 const Instruction& move_inst = code_item_accessor_.InstructionAt(move_idx);
Vladimir Markod7559b72017-09-28 13:50:37 +01002530 switch (move_inst.Opcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07002531 case Instruction::MOVE_OBJECT:
Andreas Gampe43e43252019-01-08 12:06:57 -08002532 maybe_update_fn(move_inst.VRegB_12x(), move_inst.VRegA_12x());
Ian Rogers9b360392013-06-06 14:45:07 -07002533 break;
2534 case Instruction::MOVE_OBJECT_FROM16:
Andreas Gampe43e43252019-01-08 12:06:57 -08002535 maybe_update_fn(move_inst.VRegB_22x(), move_inst.VRegA_22x());
Ian Rogers9b360392013-06-06 14:45:07 -07002536 break;
2537 case Instruction::MOVE_OBJECT_16:
Andreas Gampe43e43252019-01-08 12:06:57 -08002538 maybe_update_fn(move_inst.VRegB_32x(), move_inst.VRegA_32x());
Ian Rogers9b360392013-06-06 14:45:07 -07002539 break;
2540 default:
2541 break;
2542 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002543 }
2544 }
2545 }
2546
jeffhaobdb76512011-09-07 11:43:16 -07002547 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002548 }
jeffhaobdb76512011-09-07 11:43:16 -07002549 case Instruction::IF_LTZ:
2550 case Instruction::IF_GEZ:
2551 case Instruction::IF_GTZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002552 case Instruction::IF_LEZ: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002553 const RegType& reg_type = work_line_->GetRegisterType(this, inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002554 if (!reg_type.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002555 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
2556 << " unexpected as arg to if-ltz/if-gez/if-gtz/if-lez";
Ian Rogersd81871c2011-10-03 13:57:23 -07002557 }
jeffhaobdb76512011-09-07 11:43:16 -07002558 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002559 }
jeffhaobdb76512011-09-07 11:43:16 -07002560 case Instruction::AGET_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002561 VerifyAGet(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002562 break;
jeffhaobdb76512011-09-07 11:43:16 -07002563 case Instruction::AGET_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002564 VerifyAGet(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002565 break;
jeffhaobdb76512011-09-07 11:43:16 -07002566 case Instruction::AGET_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002567 VerifyAGet(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002568 break;
jeffhaobdb76512011-09-07 11:43:16 -07002569 case Instruction::AGET_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002570 VerifyAGet(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002571 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002572 case Instruction::AGET:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002573 VerifyAGet(inst, reg_types_.Integer(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002574 break;
jeffhaobdb76512011-09-07 11:43:16 -07002575 case Instruction::AGET_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002576 VerifyAGet(inst, reg_types_.LongLo(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002577 break;
2578 case Instruction::AGET_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002579 VerifyAGet(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07002580 break;
2581
Ian Rogersd81871c2011-10-03 13:57:23 -07002582 case Instruction::APUT_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002583 VerifyAPut(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002584 break;
2585 case Instruction::APUT_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002586 VerifyAPut(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002587 break;
2588 case Instruction::APUT_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002589 VerifyAPut(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002590 break;
2591 case Instruction::APUT_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002592 VerifyAPut(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002593 break;
2594 case Instruction::APUT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002595 VerifyAPut(inst, reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002596 break;
2597 case Instruction::APUT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002598 VerifyAPut(inst, reg_types_.LongLo(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002599 break;
2600 case Instruction::APUT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002601 VerifyAPut(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07002602 break;
2603
jeffhaobdb76512011-09-07 11:43:16 -07002604 case Instruction::IGET_BOOLEAN:
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00002605 case Instruction::IGET_BOOLEAN_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002606 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002607 break;
jeffhaobdb76512011-09-07 11:43:16 -07002608 case Instruction::IGET_BYTE:
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00002609 case Instruction::IGET_BYTE_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002610 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002611 break;
jeffhaobdb76512011-09-07 11:43:16 -07002612 case Instruction::IGET_CHAR:
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00002613 case Instruction::IGET_CHAR_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002614 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002615 break;
jeffhaobdb76512011-09-07 11:43:16 -07002616 case Instruction::IGET_SHORT:
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00002617 case Instruction::IGET_SHORT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002618 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002619 break;
2620 case Instruction::IGET:
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00002621 case Instruction::IGET_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002622 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002623 break;
2624 case Instruction::IGET_WIDE:
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00002625 case Instruction::IGET_WIDE_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002626 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002627 break;
2628 case Instruction::IGET_OBJECT:
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00002629 case Instruction::IGET_OBJECT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002630 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false,
2631 false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002632 break;
jeffhaobdb76512011-09-07 11:43:16 -07002633
Ian Rogersd81871c2011-10-03 13:57:23 -07002634 case Instruction::IPUT_BOOLEAN:
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00002635 case Instruction::IPUT_BOOLEAN_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002636 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002637 break;
2638 case Instruction::IPUT_BYTE:
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00002639 case Instruction::IPUT_BYTE_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002640 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002641 break;
2642 case Instruction::IPUT_CHAR:
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00002643 case Instruction::IPUT_CHAR_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002644 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002645 break;
2646 case Instruction::IPUT_SHORT:
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00002647 case Instruction::IPUT_SHORT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002648 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002649 break;
2650 case Instruction::IPUT:
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00002651 case Instruction::IPUT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002652 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002653 break;
2654 case Instruction::IPUT_WIDE:
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00002655 case Instruction::IPUT_WIDE_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002656 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002657 break;
jeffhaobdb76512011-09-07 11:43:16 -07002658 case Instruction::IPUT_OBJECT:
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00002659 case Instruction::IPUT_OBJECT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002660 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false,
2661 false);
jeffhaobdb76512011-09-07 11:43:16 -07002662 break;
2663
jeffhaobdb76512011-09-07 11:43:16 -07002664 case Instruction::SGET_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002665 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002666 break;
jeffhaobdb76512011-09-07 11:43:16 -07002667 case Instruction::SGET_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002668 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002669 break;
jeffhaobdb76512011-09-07 11:43:16 -07002670 case Instruction::SGET_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002671 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002672 break;
jeffhaobdb76512011-09-07 11:43:16 -07002673 case Instruction::SGET_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002674 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002675 break;
2676 case Instruction::SGET:
Andreas Gampe896df402014-10-20 22:25:29 -07002677 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002678 break;
2679 case Instruction::SGET_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002680 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002681 break;
2682 case Instruction::SGET_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002683 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false,
2684 true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002685 break;
2686
2687 case Instruction::SPUT_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002688 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002689 break;
2690 case Instruction::SPUT_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002691 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002692 break;
2693 case Instruction::SPUT_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002694 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002695 break;
2696 case Instruction::SPUT_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002697 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002698 break;
2699 case Instruction::SPUT:
Andreas Gampe896df402014-10-20 22:25:29 -07002700 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002701 break;
2702 case Instruction::SPUT_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002703 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002704 break;
2705 case Instruction::SPUT_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002706 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false,
2707 true);
jeffhaobdb76512011-09-07 11:43:16 -07002708 break;
2709
2710 case Instruction::INVOKE_VIRTUAL:
2711 case Instruction::INVOKE_VIRTUAL_RANGE:
2712 case Instruction::INVOKE_SUPER:
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00002713 case Instruction::INVOKE_SUPER_RANGE:
2714 case Instruction::INVOKE_VIRTUAL_QUICK:
2715 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002716 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE ||
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00002717 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE ||
2718 inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002719 bool is_super = (inst->Opcode() == Instruction::INVOKE_SUPER ||
2720 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
Alex Light7268d472016-01-20 15:50:01 -08002721 MethodType type = is_super ? METHOD_SUPER : METHOD_VIRTUAL;
2722 ArtMethod* called_method = VerifyInvocationArgs(inst, type, is_range);
Ian Rogersd8f69b02014-09-10 21:43:52 +00002723 const RegType* return_type = nullptr;
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002724 if (called_method != nullptr) {
Vladimir Markob45528c2017-07-27 14:14:28 +01002725 ObjPtr<mirror::Class> return_type_class = can_load_classes_
2726 ? called_method->ResolveReturnType()
2727 : called_method->LookupResolvedReturnType();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002728 if (return_type_class != nullptr) {
Andreas Gampef23f33d2015-06-23 14:18:17 -07002729 return_type = &FromClass(called_method->GetReturnTypeDescriptor(),
Vladimir Marko179b7c62019-03-22 13:38:57 +00002730 return_type_class,
Andreas Gampef23f33d2015-06-23 14:18:17 -07002731 return_type_class->CannotBeAssignedFromOtherTypes());
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002732 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07002733 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
2734 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002735 }
2736 }
2737 if (return_type == nullptr) {
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00002738 uint32_t method_idx = GetMethodIdxOfInvoke(inst);
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08002739 const dex::MethodId& method_id = dex_file_->GetMethodId(method_idx);
Andreas Gampea5b09a62016-11-17 15:21:22 -08002740 dex::TypeIndex return_type_idx =
2741 dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002742 const char* descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Mathieu Chartierbf99f772014-08-23 16:37:27 -07002743 return_type = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
jeffhaobdb76512011-09-07 11:43:16 -07002744 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002745 if (!return_type->IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002746 work_line_->SetResultRegisterType(this, *return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002747 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002748 work_line_->SetResultRegisterTypeWide(*return_type, return_type->HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002749 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002750 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002751 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002752 }
jeffhaobdb76512011-09-07 11:43:16 -07002753 case Instruction::INVOKE_DIRECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002754 case Instruction::INVOKE_DIRECT_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002755 bool is_range = (inst->Opcode() == Instruction::INVOKE_DIRECT_RANGE);
Alex Light7268d472016-01-20 15:50:01 -08002756 ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_DIRECT, is_range);
Ian Rogers46685432012-06-03 22:26:43 -07002757 const char* return_type_descriptor;
2758 bool is_constructor;
Ian Rogersd8f69b02014-09-10 21:43:52 +00002759 const RegType* return_type = nullptr;
Ian Rogers7b078e82014-09-10 14:44:24 -07002760 if (called_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002761 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08002762 const dex::MethodId& method_id = dex_file_->GetMethodId(method_idx);
Ian Rogersdfb325e2013-10-30 01:00:44 -07002763 is_constructor = strcmp("<init>", dex_file_->StringDataByIdx(method_id.name_idx_)) == 0;
Andreas Gampea5b09a62016-11-17 15:21:22 -08002764 dex::TypeIndex return_type_idx =
2765 dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogers46685432012-06-03 22:26:43 -07002766 return_type_descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2767 } else {
2768 is_constructor = called_method->IsConstructor();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002769 return_type_descriptor = called_method->GetReturnTypeDescriptor();
Vladimir Markob45528c2017-07-27 14:14:28 +01002770 ObjPtr<mirror::Class> return_type_class = can_load_classes_
2771 ? called_method->ResolveReturnType()
2772 : called_method->LookupResolvedReturnType();
Ian Rogers1ff3c982014-08-12 02:30:58 -07002773 if (return_type_class != nullptr) {
Andreas Gampef23f33d2015-06-23 14:18:17 -07002774 return_type = &FromClass(return_type_descriptor,
Vladimir Markobcf17522018-06-01 13:14:32 +01002775 return_type_class,
Andreas Gampef23f33d2015-06-23 14:18:17 -07002776 return_type_class->CannotBeAssignedFromOtherTypes());
Ian Rogers1ff3c982014-08-12 02:30:58 -07002777 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07002778 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
2779 self_->ClearException();
Ian Rogers1ff3c982014-08-12 02:30:58 -07002780 }
Ian Rogers46685432012-06-03 22:26:43 -07002781 }
2782 if (is_constructor) {
jeffhaobdb76512011-09-07 11:43:16 -07002783 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002784 * Some additional checks when calling a constructor. We know from the invocation arg check
2785 * that the "this" argument is an instance of called_method->klass. Now we further restrict
2786 * that to require that called_method->klass is the same as this->klass or this->super,
2787 * allowing the latter only if the "this" argument is the same as the "this" argument to
2788 * this method (which implies that we're in a constructor ourselves).
jeffhaobdb76512011-09-07 11:43:16 -07002789 */
Orion Hodsoncfa325e2016-10-13 10:25:54 +01002790 const RegType& this_type = work_line_->GetInvocationThis(this, inst);
jeffhaob57e9522012-04-26 18:08:21 -07002791 if (this_type.IsConflict()) // failure.
2792 break;
jeffhaobdb76512011-09-07 11:43:16 -07002793
jeffhaob57e9522012-04-26 18:08:21 -07002794 /* no null refs allowed (?) */
Andreas Gampeeed3a5d2017-11-29 14:58:34 -08002795 if (this_type.IsZeroOrNull()) {
jeffhaob57e9522012-04-26 18:08:21 -07002796 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unable to initialize null ref";
2797 break;
jeffhao2a8a90e2011-09-26 14:25:31 -07002798 }
jeffhaob57e9522012-04-26 18:08:21 -07002799
2800 /* must be in same class or in superclass */
Ian Rogersd8f69b02014-09-10 21:43:52 +00002801 // const RegType& this_super_klass = this_type.GetSuperClass(&reg_types_);
Ian Rogers46685432012-06-03 22:26:43 -07002802 // TODO: re-enable constructor type verification
2803 // if (this_super_klass.IsConflict()) {
jeffhaob57e9522012-04-26 18:08:21 -07002804 // Unknown super class, fail so we re-check at runtime.
Ian Rogers46685432012-06-03 22:26:43 -07002805 // Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "super class unknown for '" << this_type << "'";
2806 // break;
2807 // }
jeffhaob57e9522012-04-26 18:08:21 -07002808
2809 /* arg must be an uninitialized reference */
2810 if (!this_type.IsUninitializedTypes()) {
2811 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Expected initialization on uninitialized reference "
2812 << this_type;
2813 break;
2814 }
2815
2816 /*
2817 * Replace the uninitialized reference with an initialized one. We need to do this for all
2818 * registers that have the same object instance in them, not just the "this" register.
2819 */
Nicolas Geoffray98e6ce42016-02-16 18:42:15 +00002820 work_line_->MarkRefsAsInitialized(this, this_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002821 }
Ian Rogers1ff3c982014-08-12 02:30:58 -07002822 if (return_type == nullptr) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07002823 return_type = &reg_types_.FromDescriptor(GetClassLoader(), return_type_descriptor, false);
Ian Rogers1ff3c982014-08-12 02:30:58 -07002824 }
2825 if (!return_type->IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002826 work_line_->SetResultRegisterType(this, *return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002827 } else {
Ian Rogers1ff3c982014-08-12 02:30:58 -07002828 work_line_->SetResultRegisterTypeWide(*return_type, return_type->HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002829 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002830 just_set_result = true;
2831 break;
2832 }
2833 case Instruction::INVOKE_STATIC:
2834 case Instruction::INVOKE_STATIC_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002835 bool is_range = (inst->Opcode() == Instruction::INVOKE_STATIC_RANGE);
Alex Light7268d472016-01-20 15:50:01 -08002836 ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_STATIC, is_range);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002837 const char* descriptor;
Ian Rogers7b078e82014-09-10 14:44:24 -07002838 if (called_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002839 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08002840 const dex::MethodId& method_id = dex_file_->GetMethodId(method_idx);
Andreas Gampea5b09a62016-11-17 15:21:22 -08002841 dex::TypeIndex return_type_idx =
2842 dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Mathieu Chartier590fee92013-09-13 13:46:47 -07002843 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002844 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002845 descriptor = called_method->GetReturnTypeDescriptor();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002846 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00002847 const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002848 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002849 work_line_->SetResultRegisterType(this, return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002850 } else {
2851 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2852 }
jeffhaobdb76512011-09-07 11:43:16 -07002853 just_set_result = true;
2854 }
2855 break;
jeffhaobdb76512011-09-07 11:43:16 -07002856 case Instruction::INVOKE_INTERFACE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002857 case Instruction::INVOKE_INTERFACE_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002858 bool is_range = (inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE);
Alex Light7268d472016-01-20 15:50:01 -08002859 ArtMethod* abs_method = VerifyInvocationArgs(inst, METHOD_INTERFACE, is_range);
Ian Rogers7b078e82014-09-10 14:44:24 -07002860 if (abs_method != nullptr) {
Vladimir Markod93e3742018-07-18 10:58:13 +01002861 ObjPtr<mirror::Class> called_interface = abs_method->GetDeclaringClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002862 if (!called_interface->IsInterface() && !called_interface->IsObjectClass()) {
2863 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected interface class in invoke-interface '"
David Sehr709b0702016-10-13 09:12:37 -07002864 << abs_method->PrettyMethod() << "'";
Ian Rogersad0b3a32012-04-16 14:50:24 -07002865 break;
Ian Rogers28ad40d2011-10-27 15:19:26 -07002866 }
Ian Rogers0d604842012-04-16 14:50:24 -07002867 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002868 /* Get the type of the "this" arg, which should either be a sub-interface of called
2869 * interface or Object (see comments in RegType::JoinClass).
2870 */
Orion Hodsoncfa325e2016-10-13 10:25:54 +01002871 const RegType& this_type = work_line_->GetInvocationThis(this, inst);
Andreas Gampeeed3a5d2017-11-29 14:58:34 -08002872 if (this_type.IsZeroOrNull()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002873 /* null pointer always passes (and always fails at runtime) */
2874 } else {
2875 if (this_type.IsUninitializedTypes()) {
2876 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface call on uninitialized object "
2877 << this_type;
2878 break;
2879 }
2880 // In the past we have tried to assert that "called_interface" is assignable
2881 // from "this_type.GetClass()", however, as we do an imprecise Join
2882 // (RegType::JoinClass) we don't have full information on what interfaces are
2883 // implemented by "this_type". For example, two classes may implement the same
2884 // interfaces and have a common parent that doesn't implement the interface. The
2885 // join will set "this_type" to the parent class and a test that this implements
2886 // the interface will incorrectly fail.
2887 }
2888 /*
2889 * We don't have an object instance, so we can't find the concrete method. However, all of
2890 * the type information is in the abstract method, so we're good.
2891 */
2892 const char* descriptor;
Ian Rogers7b078e82014-09-10 14:44:24 -07002893 if (abs_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002894 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08002895 const dex::MethodId& method_id = dex_file_->GetMethodId(method_idx);
Andreas Gampea5b09a62016-11-17 15:21:22 -08002896 dex::TypeIndex return_type_idx =
2897 dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Mathieu Chartierde40d472015-10-15 17:47:48 -07002898 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002899 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002900 descriptor = abs_method->GetReturnTypeDescriptor();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002901 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00002902 const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002903 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002904 work_line_->SetResultRegisterType(this, return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002905 } else {
2906 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2907 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002908 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002909 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002910 }
Narayan Kamath9823e782016-08-03 12:46:58 +01002911 case Instruction::INVOKE_POLYMORPHIC:
2912 case Instruction::INVOKE_POLYMORPHIC_RANGE: {
Orion Hodsoncfa325e2016-10-13 10:25:54 +01002913 bool is_range = (inst->Opcode() == Instruction::INVOKE_POLYMORPHIC_RANGE);
2914 ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_POLYMORPHIC, is_range);
2915 if (called_method == nullptr) {
2916 // Convert potential soft failures in VerifyInvocationArgs() to hard errors.
2917 if (failure_messages_.size() > 0) {
2918 std::string message = failure_messages_.back()->str();
2919 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << message;
2920 } else {
2921 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invoke-polymorphic verification failure.";
2922 }
2923 break;
2924 }
2925 if (!CheckSignaturePolymorphicMethod(called_method) ||
2926 !CheckSignaturePolymorphicReceiver(inst)) {
Orion Hodsonfe92d122018-01-02 10:45:17 +00002927 DCHECK(HasFailures());
Orion Hodsoncfa325e2016-10-13 10:25:54 +01002928 break;
2929 }
Orion Hodson06d10a72018-05-14 08:53:38 +01002930 const uint16_t vRegH = (is_range) ? inst->VRegH_4rcc() : inst->VRegH_45cc();
2931 const dex::ProtoIndex proto_idx(vRegH);
Orion Hodsonac141392017-01-13 11:53:47 +00002932 const char* return_descriptor =
Orion Hodsoncfa325e2016-10-13 10:25:54 +01002933 dex_file_->GetReturnTypeDescriptor(dex_file_->GetProtoId(proto_idx));
2934 const RegType& return_type =
Orion Hodsonac141392017-01-13 11:53:47 +00002935 reg_types_.FromDescriptor(GetClassLoader(), return_descriptor, false);
Orion Hodsoncfa325e2016-10-13 10:25:54 +01002936 if (!return_type.IsLowHalf()) {
2937 work_line_->SetResultRegisterType(this, return_type);
2938 } else {
2939 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2940 }
Orion Hodsonac141392017-01-13 11:53:47 +00002941 just_set_result = true;
Orion Hodsoncfa325e2016-10-13 10:25:54 +01002942 break;
Narayan Kamath9823e782016-08-03 12:46:58 +01002943 }
Orion Hodsonc069a302017-01-18 09:23:12 +00002944 case Instruction::INVOKE_CUSTOM:
2945 case Instruction::INVOKE_CUSTOM_RANGE: {
2946 // Verify registers based on method_type in the call site.
2947 bool is_range = (inst->Opcode() == Instruction::INVOKE_CUSTOM_RANGE);
2948
2949 // Step 1. Check the call site that produces the method handle for invocation
2950 const uint32_t call_site_idx = is_range ? inst->VRegB_3rc() : inst->VRegB_35c();
2951 if (!CheckCallSite(call_site_idx)) {
2952 DCHECK(HasFailures());
2953 break;
2954 }
2955
2956 // Step 2. Check the register arguments correspond to the expected arguments for the
2957 // method handle produced by step 1. The dex file verifier has checked ranges for
2958 // the first three arguments and CheckCallSite has checked the method handle type.
Orion Hodson4c8e12e2018-05-18 08:33:20 +01002959 const dex::ProtoIndex proto_idx = dex_file_->GetProtoIndexForCallSite(call_site_idx);
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08002960 const dex::ProtoId& proto_id = dex_file_->GetProtoId(proto_idx);
Orion Hodsonc069a302017-01-18 09:23:12 +00002961 DexFileParameterIterator param_it(*dex_file_, proto_id);
2962 // Treat method as static as it has yet to be determined.
2963 VerifyInvocationArgsFromIterator(&param_it, inst, METHOD_STATIC, is_range, nullptr);
2964 const char* return_descriptor = dex_file_->GetReturnTypeDescriptor(proto_id);
2965
2966 // Step 3. Propagate return type information
2967 const RegType& return_type =
2968 reg_types_.FromDescriptor(GetClassLoader(), return_descriptor, false);
2969 if (!return_type.IsLowHalf()) {
2970 work_line_->SetResultRegisterType(this, return_type);
2971 } else {
2972 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2973 }
2974 just_set_result = true;
Orion Hodsonc069a302017-01-18 09:23:12 +00002975 break;
2976 }
jeffhaobdb76512011-09-07 11:43:16 -07002977 case Instruction::NEG_INT:
2978 case Instruction::NOT_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002979 work_line_->CheckUnaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002980 break;
2981 case Instruction::NEG_LONG:
2982 case Instruction::NOT_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002983 work_line_->CheckUnaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002984 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002985 break;
2986 case Instruction::NEG_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002987 work_line_->CheckUnaryOp(this, inst, reg_types_.Float(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002988 break;
2989 case Instruction::NEG_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002990 work_line_->CheckUnaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002991 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002992 break;
2993 case Instruction::INT_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002994 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002995 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002996 break;
2997 case Instruction::INT_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002998 work_line_->CheckUnaryOp(this, inst, reg_types_.Float(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002999 break;
3000 case Instruction::INT_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07003001 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003002 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003003 break;
3004 case Instruction::LONG_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003005 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003006 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07003007 break;
3008 case Instruction::LONG_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003009 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003010 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07003011 break;
3012 case Instruction::LONG_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07003013 work_line_->CheckUnaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003014 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07003015 break;
3016 case Instruction::FLOAT_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003017 work_line_->CheckUnaryOp(this, inst, reg_types_.Integer(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07003018 break;
3019 case Instruction::FLOAT_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07003020 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003021 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07003022 break;
3023 case Instruction::FLOAT_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07003024 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003025 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07003026 break;
3027 case Instruction::DOUBLE_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003028 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003029 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07003030 break;
3031 case Instruction::DOUBLE_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07003032 work_line_->CheckUnaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003033 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07003034 break;
3035 case Instruction::DOUBLE_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003036 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003037 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07003038 break;
3039 case Instruction::INT_TO_BYTE:
Ian Rogers7b078e82014-09-10 14:44:24 -07003040 work_line_->CheckUnaryOp(this, inst, reg_types_.Byte(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003041 break;
3042 case Instruction::INT_TO_CHAR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003043 work_line_->CheckUnaryOp(this, inst, reg_types_.Char(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003044 break;
3045 case Instruction::INT_TO_SHORT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003046 work_line_->CheckUnaryOp(this, inst, reg_types_.Short(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003047 break;
3048
3049 case Instruction::ADD_INT:
3050 case Instruction::SUB_INT:
3051 case Instruction::MUL_INT:
3052 case Instruction::REM_INT:
3053 case Instruction::DIV_INT:
3054 case Instruction::SHL_INT:
3055 case Instruction::SHR_INT:
3056 case Instruction::USHR_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003057 work_line_->CheckBinaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003058 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07003059 break;
3060 case Instruction::AND_INT:
3061 case Instruction::OR_INT:
3062 case Instruction::XOR_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003063 work_line_->CheckBinaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003064 reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07003065 break;
3066 case Instruction::ADD_LONG:
3067 case Instruction::SUB_LONG:
3068 case Instruction::MUL_LONG:
3069 case Instruction::DIV_LONG:
3070 case Instruction::REM_LONG:
3071 case Instruction::AND_LONG:
3072 case Instruction::OR_LONG:
3073 case Instruction::XOR_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07003074 work_line_->CheckBinaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003075 reg_types_.LongLo(), reg_types_.LongHi(),
3076 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07003077 break;
3078 case Instruction::SHL_LONG:
3079 case Instruction::SHR_LONG:
3080 case Instruction::USHR_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07003081 /* shift distance is Int, making these different from other binary operations */
Ian Rogers7b078e82014-09-10 14:44:24 -07003082 work_line_->CheckBinaryOpWideShift(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003083 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003084 break;
3085 case Instruction::ADD_FLOAT:
3086 case Instruction::SUB_FLOAT:
3087 case Instruction::MUL_FLOAT:
3088 case Instruction::DIV_FLOAT:
3089 case Instruction::REM_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003090 work_line_->CheckBinaryOp(this, inst, reg_types_.Float(), reg_types_.Float(),
3091 reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07003092 break;
3093 case Instruction::ADD_DOUBLE:
3094 case Instruction::SUB_DOUBLE:
3095 case Instruction::MUL_DOUBLE:
3096 case Instruction::DIV_DOUBLE:
3097 case Instruction::REM_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07003098 work_line_->CheckBinaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003099 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
3100 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07003101 break;
3102 case Instruction::ADD_INT_2ADDR:
3103 case Instruction::SUB_INT_2ADDR:
3104 case Instruction::MUL_INT_2ADDR:
3105 case Instruction::REM_INT_2ADDR:
3106 case Instruction::SHL_INT_2ADDR:
3107 case Instruction::SHR_INT_2ADDR:
3108 case Instruction::USHR_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003109 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
3110 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07003111 break;
3112 case Instruction::AND_INT_2ADDR:
3113 case Instruction::OR_INT_2ADDR:
3114 case Instruction::XOR_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003115 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
3116 reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07003117 break;
3118 case Instruction::DIV_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003119 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
3120 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07003121 break;
3122 case Instruction::ADD_LONG_2ADDR:
3123 case Instruction::SUB_LONG_2ADDR:
3124 case Instruction::MUL_LONG_2ADDR:
3125 case Instruction::DIV_LONG_2ADDR:
3126 case Instruction::REM_LONG_2ADDR:
3127 case Instruction::AND_LONG_2ADDR:
3128 case Instruction::OR_LONG_2ADDR:
3129 case Instruction::XOR_LONG_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003130 work_line_->CheckBinaryOp2addrWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003131 reg_types_.LongLo(), reg_types_.LongHi(),
3132 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07003133 break;
3134 case Instruction::SHL_LONG_2ADDR:
3135 case Instruction::SHR_LONG_2ADDR:
3136 case Instruction::USHR_LONG_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003137 work_line_->CheckBinaryOp2addrWideShift(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003138 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003139 break;
3140 case Instruction::ADD_FLOAT_2ADDR:
3141 case Instruction::SUB_FLOAT_2ADDR:
3142 case Instruction::MUL_FLOAT_2ADDR:
3143 case Instruction::DIV_FLOAT_2ADDR:
3144 case Instruction::REM_FLOAT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003145 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Float(), reg_types_.Float(),
3146 reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07003147 break;
3148 case Instruction::ADD_DOUBLE_2ADDR:
3149 case Instruction::SUB_DOUBLE_2ADDR:
3150 case Instruction::MUL_DOUBLE_2ADDR:
3151 case Instruction::DIV_DOUBLE_2ADDR:
3152 case Instruction::REM_DOUBLE_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003153 work_line_->CheckBinaryOp2addrWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003154 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
3155 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07003156 break;
3157 case Instruction::ADD_INT_LIT16:
Ian Rogersf72a11d2014-10-30 15:41:08 -07003158 case Instruction::RSUB_INT_LIT16:
jeffhaobdb76512011-09-07 11:43:16 -07003159 case Instruction::MUL_INT_LIT16:
3160 case Instruction::DIV_INT_LIT16:
3161 case Instruction::REM_INT_LIT16:
Ian Rogers7b078e82014-09-10 14:44:24 -07003162 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), false,
3163 true);
jeffhaobdb76512011-09-07 11:43:16 -07003164 break;
3165 case Instruction::AND_INT_LIT16:
3166 case Instruction::OR_INT_LIT16:
3167 case Instruction::XOR_INT_LIT16:
Ian Rogers7b078e82014-09-10 14:44:24 -07003168 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), true,
3169 true);
jeffhaobdb76512011-09-07 11:43:16 -07003170 break;
3171 case Instruction::ADD_INT_LIT8:
3172 case Instruction::RSUB_INT_LIT8:
3173 case Instruction::MUL_INT_LIT8:
3174 case Instruction::DIV_INT_LIT8:
3175 case Instruction::REM_INT_LIT8:
3176 case Instruction::SHL_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07003177 case Instruction::SHR_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07003178 case Instruction::USHR_INT_LIT8:
Ian Rogers7b078e82014-09-10 14:44:24 -07003179 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), false,
3180 false);
jeffhaobdb76512011-09-07 11:43:16 -07003181 break;
3182 case Instruction::AND_INT_LIT8:
3183 case Instruction::OR_INT_LIT8:
3184 case Instruction::XOR_INT_LIT8:
Ian Rogers7b078e82014-09-10 14:44:24 -07003185 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), true,
3186 false);
jeffhaobdb76512011-09-07 11:43:16 -07003187 break;
3188
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003189 // Special instructions.
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07003190 case Instruction::RETURN_VOID_NO_BARRIER:
3191 if (IsConstructor() && !IsStatic()) {
Vladimir Markod93e3742018-07-18 10:58:13 +01003192 const RegType& declaring_class = GetDeclaringClass();
Andreas Gampe68df3202015-06-22 11:35:46 -07003193 if (declaring_class.IsUnresolvedReference()) {
3194 // We must iterate over the fields, even if we cannot use mirror classes to do so. Do it
3195 // manually over the underlying dex file.
3196 uint32_t first_index = GetFirstFinalInstanceFieldIndex(*dex_file_,
3197 dex_file_->GetMethodId(dex_method_idx_).class_idx_);
Andreas Gampee2abbc62017-09-15 11:59:26 -07003198 if (first_index != dex::kDexNoIndex) {
Andreas Gampe68df3202015-06-22 11:35:46 -07003199 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void-no-barrier not expected for field "
3200 << first_index;
3201 }
3202 break;
3203 }
Vladimir Marko2cffc5d2018-05-29 15:40:56 +01003204 ObjPtr<mirror::Class> klass = declaring_class.GetClass();
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07003205 for (uint32_t i = 0, num_fields = klass->NumInstanceFields(); i < num_fields; ++i) {
3206 if (klass->GetInstanceField(i)->IsFinal()) {
Mathieu Chartiere86deef2015-03-19 13:43:37 -07003207 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void-no-barrier not expected for "
David Sehr709b0702016-10-13 09:12:37 -07003208 << klass->GetInstanceField(i)->PrettyField();
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07003209 break;
3210 }
3211 }
Sebastien Hertzcc10e0e2013-06-28 14:24:48 +02003212 }
Andreas Gampeb2917962015-07-31 13:36:10 -07003213 // Handle this like a RETURN_VOID now. Code is duplicated to separate standard from
3214 // quickened opcodes (otherwise this could be a fall-through).
3215 if (!IsConstructor()) {
3216 if (!GetMethodReturnType().IsConflict()) {
3217 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void not expected";
3218 }
3219 }
Sebastien Hertzcc10e0e2013-06-28 14:24:48 +02003220 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003221
Ian Rogersd81871c2011-10-03 13:57:23 -07003222 /* These should never appear during verification. */
Mathieu Chartierffc605c2014-12-10 10:35:44 -08003223 case Instruction::UNUSED_3E ... Instruction::UNUSED_43:
Narayan Kamath8ec3bd22016-08-03 12:46:23 +01003224 case Instruction::UNUSED_F3 ... Instruction::UNUSED_F9:
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003225 case Instruction::UNUSED_79:
3226 case Instruction::UNUSED_7A:
jeffhaod5347e02012-03-22 17:25:05 -07003227 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Unexpected opcode " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07003228 break;
3229
3230 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003231 * DO NOT add a "default" clause here. Without it the compiler will
jeffhaobdb76512011-09-07 11:43:16 -07003232 * complain if an instruction is missing (which is desirable).
3233 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003234 } // end - switch (dec_insn.opcode)
jeffhaobdb76512011-09-07 11:43:16 -07003235
Ian Rogersad0b3a32012-04-16 14:50:24 -07003236 if (have_pending_hard_failure_) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08003237 if (Runtime::Current()->IsAotCompiler()) {
3238 /* When AOT compiling, check that the last failure is a hard failure */
Andreas Gampeb588f4c2015-05-26 13:35:39 -07003239 if (failures_[failures_.size() - 1] != VERIFY_ERROR_BAD_CLASS_HARD) {
3240 LOG(ERROR) << "Pending failures:";
3241 for (auto& error : failures_) {
3242 LOG(ERROR) << error;
3243 }
3244 for (auto& error_msg : failure_messages_) {
3245 LOG(ERROR) << error_msg->str();
3246 }
3247 LOG(FATAL) << "Pending hard failure, but last failure not hard.";
3248 }
Ian Rogerse1758fe2012-04-19 11:31:15 -07003249 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003250 /* immediate failure, reject class */
3251 info_messages_ << "Rejecting opcode " << inst->DumpString(dex_file_);
3252 return false;
jeffhaofaf459e2012-08-31 15:32:47 -07003253 } else if (have_pending_runtime_throw_failure_) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07003254 /* checking interpreter will throw, mark following code as unreachable */
jeffhaofaf459e2012-08-31 15:32:47 -07003255 opcode_flags = Instruction::kThrow;
Andreas Gampea727e372015-08-25 09:22:37 -07003256 // Note: the flag must be reset as it is only global to decouple Fail and is semantically per
3257 // instruction. However, RETURN checking may throw LOCKING errors, so we clear at the
3258 // very end.
jeffhaobdb76512011-09-07 11:43:16 -07003259 }
jeffhaobdb76512011-09-07 11:43:16 -07003260 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003261 * If we didn't just set the result register, clear it out. This ensures that you can only use
3262 * "move-result" immediately after the result is set. (We could check this statically, but it's
3263 * not expensive and it makes our debugging output cleaner.)
jeffhaobdb76512011-09-07 11:43:16 -07003264 */
3265 if (!just_set_result) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003266 work_line_->SetResultTypeToUnknown(this);
jeffhaobdb76512011-09-07 11:43:16 -07003267 }
3268
jeffhaobdb76512011-09-07 11:43:16 -07003269 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003270 * Handle "branch". Tag the branch target.
jeffhaobdb76512011-09-07 11:43:16 -07003271 *
3272 * NOTE: instructions like Instruction::EQZ provide information about the
jeffhaod1f0fde2011-09-08 17:25:33 -07003273 * state of the register when the branch is taken or not taken. For example,
jeffhaobdb76512011-09-07 11:43:16 -07003274 * somebody could get a reference field, check it for zero, and if the
3275 * branch is taken immediately store that register in a boolean field
jeffhaod1f0fde2011-09-08 17:25:33 -07003276 * since the value is known to be zero. We do not currently account for
jeffhaobdb76512011-09-07 11:43:16 -07003277 * that, and will reject the code.
3278 *
3279 * TODO: avoid re-fetching the branch target
3280 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003281 if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07003282 bool isConditional, selfOkay;
Ian Rogersd81871c2011-10-03 13:57:23 -07003283 if (!GetBranchOffset(work_insn_idx_, &branch_target, &isConditional, &selfOkay)) {
jeffhaobdb76512011-09-07 11:43:16 -07003284 /* should never happen after static verification */
jeffhaod5347e02012-03-22 17:25:05 -07003285 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad branch";
jeffhaobdb76512011-09-07 11:43:16 -07003286 return false;
3287 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08003288 DCHECK_EQ(isConditional, (opcode_flags & Instruction::kContinue) != 0);
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08003289 if (!CheckNotMoveExceptionOrMoveResult(code_item_accessor_.Insns(),
3290 work_insn_idx_ + branch_target)) {
jeffhaobdb76512011-09-07 11:43:16 -07003291 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07003292 }
jeffhaobdb76512011-09-07 11:43:16 -07003293 /* update branch target, set "changed" if appropriate */
Mathieu Chartierde40d472015-10-15 17:47:48 -07003294 if (nullptr != branch_line) {
Ian Rogersebbdd872014-07-07 23:53:08 -07003295 if (!UpdateRegisters(work_insn_idx_ + branch_target, branch_line.get(), false)) {
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003296 return false;
3297 }
3298 } else {
Ian Rogersebbdd872014-07-07 23:53:08 -07003299 if (!UpdateRegisters(work_insn_idx_ + branch_target, work_line_.get(), false)) {
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003300 return false;
3301 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003302 }
jeffhaobdb76512011-09-07 11:43:16 -07003303 }
3304
3305 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003306 * Handle "switch". Tag all possible branch targets.
jeffhaobdb76512011-09-07 11:43:16 -07003307 *
3308 * We've already verified that the table is structurally sound, so we
3309 * just need to walk through and tag the targets.
3310 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003311 if ((opcode_flags & Instruction::kSwitch) != 0) {
Andreas Gampe53de99c2015-08-17 13:43:55 -07003312 int offset_to_switch = insns[1] | (static_cast<int32_t>(insns[2]) << 16);
jeffhaobdb76512011-09-07 11:43:16 -07003313 const uint16_t* switch_insns = insns + offset_to_switch;
3314 int switch_count = switch_insns[1];
3315 int offset_to_targets, targ;
3316
3317 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
3318 /* 0 = sig, 1 = count, 2/3 = first key */
3319 offset_to_targets = 4;
3320 } else {
3321 /* 0 = sig, 1 = count, 2..count * 2 = keys */
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07003322 DCHECK((*insns & 0xff) == Instruction::SPARSE_SWITCH);
jeffhaobdb76512011-09-07 11:43:16 -07003323 offset_to_targets = 2 + 2 * switch_count;
3324 }
3325
3326 /* verify each switch target */
3327 for (targ = 0; targ < switch_count; targ++) {
3328 int offset;
3329 uint32_t abs_offset;
3330
3331 /* offsets are 32-bit, and only partly endian-swapped */
3332 offset = switch_insns[offset_to_targets + targ * 2] |
Andreas Gampe53de99c2015-08-17 13:43:55 -07003333 (static_cast<int32_t>(switch_insns[offset_to_targets + targ * 2 + 1]) << 16);
Ian Rogersd81871c2011-10-03 13:57:23 -07003334 abs_offset = work_insn_idx_ + offset;
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08003335 DCHECK_LT(abs_offset, code_item_accessor_.InsnsSizeInCodeUnits());
3336 if (!CheckNotMoveExceptionOrMoveResult(code_item_accessor_.Insns(), abs_offset)) {
jeffhaobdb76512011-09-07 11:43:16 -07003337 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07003338 }
Ian Rogersebbdd872014-07-07 23:53:08 -07003339 if (!UpdateRegisters(abs_offset, work_line_.get(), false)) {
jeffhaobdb76512011-09-07 11:43:16 -07003340 return false;
Ian Rogersebbdd872014-07-07 23:53:08 -07003341 }
jeffhaobdb76512011-09-07 11:43:16 -07003342 }
3343 }
3344
3345 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003346 * Handle instructions that can throw and that are sitting in a "try" block. (If they're not in a
3347 * "try" block when they throw, control transfers out of the method.)
jeffhaobdb76512011-09-07 11:43:16 -07003348 */
Mathieu Chartierde40d472015-10-15 17:47:48 -07003349 if ((opcode_flags & Instruction::kThrow) != 0 && GetInstructionFlags(work_insn_idx_).IsInTry()) {
Andreas Gampef91baf12014-07-18 15:41:00 -07003350 bool has_catch_all_handler = false;
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08003351 const dex::TryItem* try_item = code_item_accessor_.FindTryItem(work_insn_idx_);
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08003352 CHECK(try_item != nullptr);
Mathieu Chartierdc578c72017-12-27 11:51:45 -08003353 CatchHandlerIterator iterator(code_item_accessor_, *try_item);
jeffhaobdb76512011-09-07 11:43:16 -07003354
Andreas Gampef91baf12014-07-18 15:41:00 -07003355 // Need the linker to try and resolve the handled class to check if it's Throwable.
3356 ClassLinker* linker = Runtime::Current()->GetClassLinker();
3357
Ian Rogers0571d352011-11-03 19:51:38 -07003358 for (; iterator.HasNext(); iterator.Next()) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08003359 dex::TypeIndex handler_type_idx = iterator.GetHandlerTypeIndex();
3360 if (!handler_type_idx.IsValid()) {
Andreas Gampef91baf12014-07-18 15:41:00 -07003361 has_catch_all_handler = true;
3362 } else {
3363 // It is also a catch-all if it is java.lang.Throwable.
Vladimir Marko28e012a2017-12-07 11:22:59 +00003364 ObjPtr<mirror::Class> klass =
Vladimir Marko666ee3d2017-12-11 18:37:36 +00003365 linker->ResolveType(handler_type_idx, dex_cache_, class_loader_);
Andreas Gampef91baf12014-07-18 15:41:00 -07003366 if (klass != nullptr) {
Vladimir Markoadbceb72018-05-29 14:34:14 +01003367 if (klass == GetClassRoot<mirror::Throwable>()) {
Andreas Gampef91baf12014-07-18 15:41:00 -07003368 has_catch_all_handler = true;
3369 }
3370 } else {
3371 // Clear exception.
Ian Rogers7b078e82014-09-10 14:44:24 -07003372 DCHECK(self_->IsExceptionPending());
3373 self_->ClearException();
Andreas Gampef91baf12014-07-18 15:41:00 -07003374 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003375 }
jeffhaobdb76512011-09-07 11:43:16 -07003376 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003377 * Merge registers into the "catch" block. We want to use the "savedRegs" rather than
3378 * "work_regs", because at runtime the exception will be thrown before the instruction
3379 * modifies any registers.
jeffhaobdb76512011-09-07 11:43:16 -07003380 */
Ian Rogersebbdd872014-07-07 23:53:08 -07003381 if (!UpdateRegisters(iterator.GetHandlerAddress(), saved_line_.get(), false)) {
jeffhaobdb76512011-09-07 11:43:16 -07003382 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07003383 }
jeffhaobdb76512011-09-07 11:43:16 -07003384 }
3385
3386 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003387 * If the monitor stack depth is nonzero, there must be a "catch all" handler for this
3388 * instruction. This does apply to monitor-exit because of async exception handling.
jeffhaobdb76512011-09-07 11:43:16 -07003389 */
Andreas Gampef91baf12014-07-18 15:41:00 -07003390 if (work_line_->MonitorStackDepth() > 0 && !has_catch_all_handler) {
jeffhaobdb76512011-09-07 11:43:16 -07003391 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003392 * The state in work_line reflects the post-execution state. If the current instruction is a
3393 * monitor-enter and the monitor stack was empty, we don't need a catch-all (if it throws,
jeffhaobdb76512011-09-07 11:43:16 -07003394 * it will do so before grabbing the lock).
3395 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02003396 if (inst->Opcode() != Instruction::MONITOR_ENTER || work_line_->MonitorStackDepth() != 1) {
jeffhaod5347e02012-03-22 17:25:05 -07003397 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
Ian Rogersd81871c2011-10-03 13:57:23 -07003398 << "expected to be within a catch-all for an instruction where a monitor is held";
jeffhaobdb76512011-09-07 11:43:16 -07003399 return false;
3400 }
3401 }
3402 }
3403
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003404 /* Handle "continue". Tag the next consecutive instruction.
3405 * Note: Keep the code handling "continue" case below the "branch" and "switch" cases,
3406 * because it changes work_line_ when performing peephole optimization
3407 * and this change should not be used in those cases.
3408 */
Ian Rogers6d376ae2013-07-23 15:12:40 -07003409 if ((opcode_flags & Instruction::kContinue) != 0) {
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08003410 DCHECK_EQ(&code_item_accessor_.InstructionAt(work_insn_idx_), inst);
Ian Rogers7b078e82014-09-10 14:44:24 -07003411 uint32_t next_insn_idx = work_insn_idx_ + inst->SizeInCodeUnits();
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08003412 if (next_insn_idx >= code_item_accessor_.InsnsSizeInCodeUnits()) {
Ian Rogers6d376ae2013-07-23 15:12:40 -07003413 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Execution can walk off end of code area";
3414 return false;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003415 }
Ian Rogers6d376ae2013-07-23 15:12:40 -07003416 // The only way to get to a move-exception instruction is to get thrown there. Make sure the
3417 // next instruction isn't one.
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08003418 if (!CheckNotMoveException(code_item_accessor_.Insns(), next_insn_idx)) {
Ian Rogers6d376ae2013-07-23 15:12:40 -07003419 return false;
3420 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07003421 if (nullptr != fallthrough_line) {
Ian Rogers6d376ae2013-07-23 15:12:40 -07003422 // Make workline consistent with fallthrough computed from peephole optimization.
3423 work_line_->CopyFromLine(fallthrough_line.get());
3424 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07003425 if (GetInstructionFlags(next_insn_idx).IsReturn()) {
Ian Rogersb8c78592013-07-25 23:52:52 +00003426 // For returns we only care about the operand to the return, all other registers are dead.
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08003427 const Instruction* ret_inst = &code_item_accessor_.InstructionAt(next_insn_idx);
Andreas Gampea727e372015-08-25 09:22:37 -07003428 AdjustReturnLine(this, ret_inst, work_line_.get());
Ian Rogersb8c78592013-07-25 23:52:52 +00003429 }
Ian Rogers6d376ae2013-07-23 15:12:40 -07003430 RegisterLine* next_line = reg_table_.GetLine(next_insn_idx);
Ian Rogers7b078e82014-09-10 14:44:24 -07003431 if (next_line != nullptr) {
Ian Rogersebbdd872014-07-07 23:53:08 -07003432 // Merge registers into what we have for the next instruction, and set the "changed" flag if
3433 // needed. If the merge changes the state of the registers then the work line will be
3434 // updated.
3435 if (!UpdateRegisters(next_insn_idx, work_line_.get(), true)) {
Ian Rogers6d376ae2013-07-23 15:12:40 -07003436 return false;
3437 }
3438 } else {
3439 /*
3440 * We're not recording register data for the next instruction, so we don't know what the
3441 * prior state was. We have to assume that something has changed and re-evaluate it.
3442 */
Mathieu Chartierde40d472015-10-15 17:47:48 -07003443 GetInstructionFlags(next_insn_idx).SetChanged();
Ian Rogers6d376ae2013-07-23 15:12:40 -07003444 }
3445 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003446
jeffhaod1f0fde2011-09-08 17:25:33 -07003447 /* If we're returning from the method, make sure monitor stack is empty. */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003448 if ((opcode_flags & Instruction::kReturn) != 0) {
Andreas Gampea727e372015-08-25 09:22:37 -07003449 work_line_->VerifyMonitorStackEmpty(this);
jeffhaobdb76512011-09-07 11:43:16 -07003450 }
3451
3452 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003453 * Update start_guess. Advance to the next instruction of that's
3454 * possible, otherwise use the branch target if one was found. If
jeffhaobdb76512011-09-07 11:43:16 -07003455 * neither of those exists we're in a return or throw; leave start_guess
3456 * alone and let the caller sort it out.
3457 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003458 if ((opcode_flags & Instruction::kContinue) != 0) {
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08003459 DCHECK_EQ(&code_item_accessor_.InstructionAt(work_insn_idx_), inst);
Ian Rogers7b078e82014-09-10 14:44:24 -07003460 *start_guess = work_insn_idx_ + inst->SizeInCodeUnits();
Elliott Hughesadb8c672012-03-06 16:49:32 -08003461 } else if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07003462 /* we're still okay if branch_target is zero */
Ian Rogersd81871c2011-10-03 13:57:23 -07003463 *start_guess = work_insn_idx_ + branch_target;
jeffhaobdb76512011-09-07 11:43:16 -07003464 }
3465
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08003466 DCHECK_LT(*start_guess, code_item_accessor_.InsnsSizeInCodeUnits());
Mathieu Chartierde40d472015-10-15 17:47:48 -07003467 DCHECK(GetInstructionFlags(*start_guess).IsOpcode());
jeffhaobdb76512011-09-07 11:43:16 -07003468
Andreas Gampea727e372015-08-25 09:22:37 -07003469 if (have_pending_runtime_throw_failure_) {
3470 have_any_pending_runtime_throw_failure_ = true;
3471 // Reset the pending_runtime_throw flag now.
3472 have_pending_runtime_throw_failure_ = false;
3473 }
3474
jeffhaobdb76512011-09-07 11:43:16 -07003475 return true;
Brian Carlstrom7934ac22013-07-26 10:54:15 -07003476} // NOLINT(readability/fn_size)
jeffhaobdb76512011-09-07 11:43:16 -07003477
Mathieu Chartierde40d472015-10-15 17:47:48 -07003478void MethodVerifier::UninstantiableError(const char* descriptor) {
3479 Fail(VerifyError::VERIFY_ERROR_NO_CLASS) << "Could not create precise reference for "
3480 << "non-instantiable klass " << descriptor;
3481}
3482
Vladimir Marko28e012a2017-12-07 11:22:59 +00003483inline bool MethodVerifier::IsInstantiableOrPrimitive(ObjPtr<mirror::Class> klass) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07003484 return klass->IsInstantiable() || klass->IsPrimitive();
3485}
3486
Andreas Gampe98be1a92017-08-28 08:25:45 -07003487template <MethodVerifier::CheckAccess C>
3488const RegType& MethodVerifier::ResolveClass(dex::TypeIndex class_idx) {
Vladimir Marko666ee3d2017-12-11 18:37:36 +00003489 ClassLinker* linker = Runtime::Current()->GetClassLinker();
Vladimir Marko28e012a2017-12-07 11:22:59 +00003490 ObjPtr<mirror::Class> klass = can_load_classes_
Vladimir Marko666ee3d2017-12-11 18:37:36 +00003491 ? linker->ResolveType(class_idx, dex_cache_, class_loader_)
3492 : linker->LookupResolvedType(class_idx, dex_cache_.Get(), class_loader_.Get());
Vladimir Marko8d6768d2017-03-14 10:13:21 +00003493 if (can_load_classes_ && klass == nullptr) {
3494 DCHECK(self_->IsExceptionPending());
3495 self_->ClearException();
3496 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07003497 const RegType* result = nullptr;
Vladimir Marko9cb0c462017-04-21 13:31:41 +01003498 if (klass != nullptr) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07003499 bool precise = klass->CannotBeAssignedFromOtherTypes();
3500 if (precise && !IsInstantiableOrPrimitive(klass)) {
3501 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
3502 UninstantiableError(descriptor);
3503 precise = false;
3504 }
Vladimir Marko2cffc5d2018-05-29 15:40:56 +01003505 result = reg_types_.FindClass(klass, precise);
Mathieu Chartierde40d472015-10-15 17:47:48 -07003506 if (result == nullptr) {
3507 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
Vladimir Marko2cffc5d2018-05-29 15:40:56 +01003508 result = reg_types_.InsertClass(descriptor, klass, precise);
Mathieu Chartierde40d472015-10-15 17:47:48 -07003509 }
3510 } else {
3511 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
3512 result = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003513 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07003514 DCHECK(result != nullptr);
3515 if (result->IsConflict()) {
3516 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
3517 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "accessing broken descriptor '" << descriptor
3518 << "' in " << GetDeclaringClass();
3519 return *result;
3520 }
David Brazdilca3c8c32016-09-06 14:04:48 +01003521
3522 // Record result of class resolution attempt.
Vladimir Markobcf17522018-06-01 13:14:32 +01003523 VerifierDeps::MaybeRecordClassResolution(*dex_file_, class_idx, klass);
David Brazdilca3c8c32016-09-06 14:04:48 +01003524
Andreas Gampe629be512017-08-25 17:09:32 -07003525 // If requested, check if access is allowed. Unresolved types are included in this check, as the
3526 // interpreter only tests whether access is allowed when a class is not pre-verified and runs in
3527 // the access-checks interpreter. If result is primitive, skip the access check.
3528 //
3529 // Note: we do this for unresolved classes to trigger re-verification at runtime.
Andreas Gampedc39d322018-09-04 09:26:03 -07003530 if (C == CheckAccess::kYes &&
3531 result->IsNonZeroReferenceTypes() &&
David Brazdil2bb2fbd2018-11-13 18:24:26 +00003532 (IsSdkVersionSetAndAtLeast(api_level_, SdkVersion::kP) || !result->IsUnresolvedTypes())) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07003533 const RegType& referrer = GetDeclaringClass();
David Brazdil2bb2fbd2018-11-13 18:24:26 +00003534 if ((IsSdkVersionSetAndAtLeast(api_level_, SdkVersion::kP) || !referrer.IsUnresolvedTypes()) &&
3535 !referrer.CanAccess(*result)) {
Andreas Gampe629be512017-08-25 17:09:32 -07003536 Fail(VERIFY_ERROR_ACCESS_CLASS) << "(possibly) illegal class access: '"
Nicolas Geoffray4e868fa2017-04-21 17:16:44 +01003537 << referrer << "' -> '" << *result << "'";
Mathieu Chartierde40d472015-10-15 17:47:48 -07003538 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07003539 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07003540 return *result;
Ian Rogersd81871c2011-10-03 13:57:23 -07003541}
3542
Andreas Gampedd309072017-08-31 07:47:37 -07003543// Instantiate ResolveClass variants. This is required as the -inl file has a function with a call
3544// to ResolveClass, and compilers may decide to inline, requiring a symbol.
Andreas Gampeae0b1f42017-08-30 17:36:26 -07003545template const RegType& MethodVerifier::ResolveClass<MethodVerifier::CheckAccess::kNo>(
3546 dex::TypeIndex class_idx);
3547template const RegType& MethodVerifier::ResolveClass<MethodVerifier::CheckAccess::kYes>(
3548 dex::TypeIndex class_idx);
3549
Ian Rogersd8f69b02014-09-10 21:43:52 +00003550const RegType& MethodVerifier::GetCaughtExceptionType() {
Ian Rogers7b078e82014-09-10 14:44:24 -07003551 const RegType* common_super = nullptr;
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08003552 if (code_item_accessor_.TriesSize() != 0) {
3553 const uint8_t* handlers_ptr = code_item_accessor_.GetCatchHandlerData();
Ian Rogersd81871c2011-10-03 13:57:23 -07003554 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
3555 for (uint32_t i = 0; i < handlers_size; i++) {
Ian Rogers0571d352011-11-03 19:51:38 -07003556 CatchHandlerIterator iterator(handlers_ptr);
3557 for (; iterator.HasNext(); iterator.Next()) {
3558 if (iterator.GetHandlerAddress() == (uint32_t) work_insn_idx_) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08003559 if (!iterator.GetHandlerTypeIndex().IsValid()) {
Ian Rogersb4903572012-10-11 11:52:56 -07003560 common_super = &reg_types_.JavaLangThrowable(false);
Ian Rogersd81871c2011-10-03 13:57:23 -07003561 } else {
Andreas Gampe98be1a92017-08-28 08:25:45 -07003562 const RegType& exception =
3563 ResolveClass<CheckAccess::kYes>(iterator.GetHandlerTypeIndex());
David Brazdilca3c8c32016-09-06 14:04:48 +01003564 if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(exception, this)) {
David Brazdil68b5c0b2016-01-19 14:25:29 +00003565 DCHECK(!exception.IsUninitializedTypes()); // Comes from dex, shouldn't be uninit.
Jeff Haoc26a56c2013-11-04 12:00:47 -08003566 if (exception.IsUnresolvedTypes()) {
3567 // We don't know enough about the type. Fail here and let runtime handle it.
3568 Fail(VERIFY_ERROR_NO_CLASS) << "unresolved exception class " << exception;
3569 return exception;
3570 } else {
3571 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unexpected non-exception class " << exception;
3572 return reg_types_.Conflict();
3573 }
Jeff Haob878f212014-04-24 16:25:36 -07003574 } else if (common_super == nullptr) {
3575 common_super = &exception;
Ian Rogers28ad40d2011-10-27 15:19:26 -07003576 } else if (common_super->Equals(exception)) {
Ian Rogersc4762272012-02-01 15:55:55 -08003577 // odd case, but nothing to do
Ian Rogersd81871c2011-10-03 13:57:23 -07003578 } else {
David Brazdilca3c8c32016-09-06 14:04:48 +01003579 common_super = &common_super->Merge(exception, &reg_types_, this);
Mathieu Chartierc2a2bda2018-03-12 11:21:52 -07003580 if (FailOrAbort(reg_types_.JavaLangThrowable(false).IsAssignableFrom(
David Brazdilca3c8c32016-09-06 14:04:48 +01003581 *common_super, this),
Andreas Gampe7c038102014-10-27 20:08:46 -07003582 "java.lang.Throwable is not assignable-from common_super at ",
3583 work_insn_idx_)) {
3584 break;
3585 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003586 }
3587 }
3588 }
3589 }
Ian Rogers0571d352011-11-03 19:51:38 -07003590 handlers_ptr = iterator.EndDataPointer();
Ian Rogersd81871c2011-10-03 13:57:23 -07003591 }
3592 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003593 if (common_super == nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003594 /* no catch blocks, or no catches with classes we can find */
jeffhaod5347e02012-03-22 17:25:05 -07003595 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unable to find exception handler";
Ian Rogersad0b3a32012-04-16 14:50:24 -07003596 return reg_types_.Conflict();
Ian Rogersd81871c2011-10-03 13:57:23 -07003597 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07003598 return *common_super;
Ian Rogersd81871c2011-10-03 13:57:23 -07003599}
3600
Mathieu Chartiere401d142015-04-22 13:56:20 -07003601ArtMethod* MethodVerifier::ResolveMethodAndCheckAccess(
Alex Light7268d472016-01-20 15:50:01 -08003602 uint32_t dex_method_idx, MethodType method_type) {
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08003603 const dex::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx);
Andreas Gampe98be1a92017-08-28 08:25:45 -07003604 const RegType& klass_type = ResolveClass<CheckAccess::kYes>(method_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003605 if (klass_type.IsConflict()) {
3606 std::string append(" in attempt to access method ");
3607 append += dex_file_->GetMethodName(method_id);
3608 AppendToLastFailMessage(append);
Ian Rogers7b078e82014-09-10 14:44:24 -07003609 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08003610 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003611 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003612 return nullptr; // Can't resolve Class so no more to do here
Ian Rogers90040192011-12-16 08:54:29 -08003613 }
Vladimir Markoba118822017-06-12 15:41:56 +01003614 ObjPtr<mirror::Class> klass = klass_type.GetClass();
Ian Rogersd8f69b02014-09-10 21:43:52 +00003615 const RegType& referrer = GetDeclaringClass();
Nicolas Geoffrayea179f42018-02-08 22:30:18 +00003616 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
3617 PointerSize pointer_size = class_linker->GetImagePointerSize();
Andreas Gampe42ef8ab2015-12-03 17:27:32 -08003618
Mathieu Chartiere401d142015-04-22 13:56:20 -07003619 ArtMethod* res_method = dex_cache_->GetResolvedMethod(dex_method_idx, pointer_size);
Ian Rogers7b078e82014-09-10 14:44:24 -07003620 if (res_method == nullptr) {
Nicolas Geoffrayea179f42018-02-08 22:30:18 +00003621 res_method = class_linker->FindResolvedMethod(
3622 klass, dex_cache_.Get(), class_loader_.Get(), dex_method_idx);
Ian Rogersd81871c2011-10-03 13:57:23 -07003623 }
David Brazdilca3c8c32016-09-06 14:04:48 +01003624
Vladimir Markoba118822017-06-12 15:41:56 +01003625 // Record result of method resolution attempt. The klass resolution has recorded whether
3626 // the class is an interface or not and therefore the type of the lookup performed above.
3627 // TODO: Maybe we should not record dependency if the invoke type does not match the lookup type.
3628 VerifierDeps::MaybeRecordMethodResolution(*dex_file_, dex_method_idx, res_method);
3629
Alex Lightafb66472017-08-01 09:54:49 -07003630 bool must_fail = false;
3631 // This is traditional and helps with screwy bytecode. It will tell you that, yes, a method
3632 // exists, but that it's called incorrectly. This significantly helps debugging, as locally it's
3633 // hard to see the differences.
3634 // If we don't have res_method here we must fail. Just use this bool to make sure of that with a
3635 // DCHECK.
Vladimir Markoba118822017-06-12 15:41:56 +01003636 if (res_method == nullptr) {
Alex Lightafb66472017-08-01 09:54:49 -07003637 must_fail = true;
Vladimir Markoba118822017-06-12 15:41:56 +01003638 // Try to find the method also with the other type for better error reporting below
3639 // but do not store such bogus lookup result in the DexCache or VerifierDeps.
David Brazdil4525e0b2018-04-05 16:57:32 +01003640 res_method = class_linker->FindIncompatibleMethod(
3641 klass, dex_cache_.Get(), class_loader_.Get(), dex_method_idx);
Vladimir Markoba118822017-06-12 15:41:56 +01003642 }
David Brazdilca3c8c32016-09-06 14:04:48 +01003643
3644 if (res_method == nullptr) {
3645 Fail(VERIFY_ERROR_NO_METHOD) << "couldn't find method "
David Sehr709b0702016-10-13 09:12:37 -07003646 << klass->PrettyDescriptor() << "."
David Brazdilca3c8c32016-09-06 14:04:48 +01003647 << dex_file_->GetMethodName(method_id) << " "
3648 << dex_file_->GetMethodSignature(method_id);
3649 return nullptr;
3650 }
3651
Ian Rogersd81871c2011-10-03 13:57:23 -07003652 // Make sure calls to constructors are "direct". There are additional restrictions but we don't
3653 // enforce them here.
3654 if (res_method->IsConstructor() && method_type != METHOD_DIRECT) {
jeffhaod5347e02012-03-22 17:25:05 -07003655 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting non-direct call to constructor "
David Sehr709b0702016-10-13 09:12:37 -07003656 << res_method->PrettyMethod();
Ian Rogers7b078e82014-09-10 14:44:24 -07003657 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003658 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003659 // Disallow any calls to class initializers.
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003660 if (res_method->IsClassInitializer()) {
jeffhaod5347e02012-03-22 17:25:05 -07003661 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting call to class initializer "
David Sehr709b0702016-10-13 09:12:37 -07003662 << res_method->PrettyMethod();
Ian Rogers7b078e82014-09-10 14:44:24 -07003663 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003664 }
Andreas Gampe42ef8ab2015-12-03 17:27:32 -08003665
3666 // Check that interface methods are static or match interface classes.
3667 // We only allow statics if we don't have default methods enabled.
3668 //
3669 // Note: this check must be after the initializer check, as those are required to fail a class,
3670 // while this check implies an IncompatibleClassChangeError.
3671 if (klass->IsInterface()) {
Alex Lightb55f1ac2016-04-12 15:50:55 -07003672 // methods called on interfaces should be invoke-interface, invoke-super, invoke-direct (if
Mathieu Chartierf6e31472017-12-28 13:32:08 -08003673 // default methods are supported for the dex file), or invoke-static.
Andreas Gampe42ef8ab2015-12-03 17:27:32 -08003674 if (method_type != METHOD_INTERFACE &&
Neil Fuller9724c632016-01-07 15:42:47 +00003675 method_type != METHOD_STATIC &&
Mathieu Chartierf6e31472017-12-28 13:32:08 -08003676 (!dex_file_->SupportsDefaultMethods() ||
Alex Lightb55f1ac2016-04-12 15:50:55 -07003677 method_type != METHOD_DIRECT) &&
Neil Fuller9724c632016-01-07 15:42:47 +00003678 method_type != METHOD_SUPER) {
Andreas Gampe42ef8ab2015-12-03 17:27:32 -08003679 Fail(VERIFY_ERROR_CLASS_CHANGE)
David Sehr709b0702016-10-13 09:12:37 -07003680 << "non-interface method " << dex_file_->PrettyMethod(dex_method_idx)
3681 << " is in an interface class " << klass->PrettyClass();
Andreas Gampe42ef8ab2015-12-03 17:27:32 -08003682 return nullptr;
3683 }
3684 } else {
3685 if (method_type == METHOD_INTERFACE) {
3686 Fail(VERIFY_ERROR_CLASS_CHANGE)
David Sehr709b0702016-10-13 09:12:37 -07003687 << "interface method " << dex_file_->PrettyMethod(dex_method_idx)
3688 << " is in a non-interface class " << klass->PrettyClass();
Andreas Gampe42ef8ab2015-12-03 17:27:32 -08003689 return nullptr;
3690 }
3691 }
3692
Alex Lightafb66472017-08-01 09:54:49 -07003693 // Check specifically for non-public object methods being provided for interface dispatch. This
3694 // can occur if we failed to find a method with FindInterfaceMethod but later find one with
3695 // FindClassMethod for error message use.
3696 if (method_type == METHOD_INTERFACE &&
3697 res_method->GetDeclaringClass()->IsObjectClass() &&
3698 !res_method->IsPublic()) {
3699 Fail(VERIFY_ERROR_NO_METHOD) << "invoke-interface " << klass->PrettyDescriptor() << "."
3700 << dex_file_->GetMethodName(method_id) << " "
3701 << dex_file_->GetMethodSignature(method_id) << " resolved to "
3702 << "non-public object method " << res_method->PrettyMethod() << " "
3703 << "but non-public Object methods are excluded from interface "
3704 << "method resolution.";
3705 return nullptr;
3706 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003707 // Check if access is allowed.
Ian Rogersad0b3a32012-04-16 14:50:24 -07003708 if (!referrer.CanAccessMember(res_method->GetDeclaringClass(), res_method->GetAccessFlags())) {
David Sehr709b0702016-10-13 09:12:37 -07003709 Fail(VERIFY_ERROR_ACCESS_METHOD) << "illegal method access (call "
3710 << res_method->PrettyMethod()
Ian Rogersad0b3a32012-04-16 14:50:24 -07003711 << " from " << referrer << ")";
jeffhaob57e9522012-04-26 18:08:21 -07003712 return res_method;
jeffhao8cd6dda2012-02-22 10:15:34 -08003713 }
jeffhaode0d9c92012-02-27 13:58:13 -08003714 // Check that invoke-virtual and invoke-super are not used on private methods of the same class.
Alex Light7268d472016-01-20 15:50:01 -08003715 if (res_method->IsPrivate() && (method_type == METHOD_VIRTUAL || method_type == METHOD_SUPER)) {
jeffhaod5347e02012-03-22 17:25:05 -07003716 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invoke-super/virtual can't be used on private method "
David Sehr709b0702016-10-13 09:12:37 -07003717 << res_method->PrettyMethod();
Ian Rogers7b078e82014-09-10 14:44:24 -07003718 return nullptr;
jeffhaode0d9c92012-02-27 13:58:13 -08003719 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003720 // See if the method type implied by the invoke instruction matches the access flags for the
Orion Hodsoncfa325e2016-10-13 10:25:54 +01003721 // target method. The flags for METHOD_POLYMORPHIC are based on there being precisely two
3722 // signature polymorphic methods supported by the run-time which are native methods with variable
3723 // arguments.
Brian Carlstrombe6fa5e2014-12-09 20:15:42 -08003724 if ((method_type == METHOD_DIRECT && (!res_method->IsDirect() || res_method->IsStatic())) ||
Ian Rogersd81871c2011-10-03 13:57:23 -07003725 (method_type == METHOD_STATIC && !res_method->IsStatic()) ||
Alex Light7268d472016-01-20 15:50:01 -08003726 ((method_type == METHOD_SUPER ||
3727 method_type == METHOD_VIRTUAL ||
Orion Hodsoncfa325e2016-10-13 10:25:54 +01003728 method_type == METHOD_INTERFACE) && res_method->IsDirect()) ||
3729 ((method_type == METHOD_POLYMORPHIC) &&
3730 (!res_method->IsNative() || !res_method->IsVarargs()))) {
Ian Rogers2fc14272012-08-30 10:56:57 -07003731 Fail(VERIFY_ERROR_CLASS_CHANGE) << "invoke type (" << method_type << ") does not match method "
Orion Hodsoncfa325e2016-10-13 10:25:54 +01003732 "type of " << res_method->PrettyMethod();
Ian Rogers7b078e82014-09-10 14:44:24 -07003733 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003734 }
Alex Lightafb66472017-08-01 09:54:49 -07003735 // Make sure we weren't expecting to fail.
3736 DCHECK(!must_fail) << "invoke type (" << method_type << ")"
3737 << klass->PrettyDescriptor() << "."
3738 << dex_file_->GetMethodName(method_id) << " "
3739 << dex_file_->GetMethodSignature(method_id) << " unexpectedly resolved to "
3740 << res_method->PrettyMethod() << " without error. Initially this method was "
3741 << "not found so we were expecting to fail for some reason.";
jeffhao8cd6dda2012-02-22 10:15:34 -08003742 return res_method;
3743}
3744
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003745template <class T>
Mathieu Chartiere401d142015-04-22 13:56:20 -07003746ArtMethod* MethodVerifier::VerifyInvocationArgsFromIterator(
3747 T* it, const Instruction* inst, MethodType method_type, bool is_range, ArtMethod* res_method) {
Andreas Gampee383d232018-06-19 12:29:51 -07003748 DCHECK_EQ(!is_range, inst->HasVarArgs());
3749
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003750 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
3751 // match the call to the signature. Also, we might be calling through an abstract method
3752 // definition (which doesn't have register count values).
Orion Hodsoncfa325e2016-10-13 10:25:54 +01003753 const size_t expected_args = inst->VRegA();
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003754 /* caught by static verifier */
3755 DCHECK(is_range || expected_args <= 5);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003756
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08003757 if (expected_args > code_item_accessor_.OutsSize()) {
Orion Hodson1cda7c22017-08-10 13:06:45 +01003758 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08003759 << ") exceeds outsSize ("
3760 << code_item_accessor_.OutsSize() << ")";
Orion Hodson1cda7c22017-08-10 13:06:45 +01003761 return nullptr;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003762 }
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003763
3764 /*
3765 * Check the "this" argument, which must be an instance of the class that declared the method.
3766 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
3767 * rigorous check here (which is okay since we have to do it at runtime).
3768 */
3769 if (method_type != METHOD_STATIC) {
Orion Hodsoncfa325e2016-10-13 10:25:54 +01003770 const RegType& actual_arg_type = work_line_->GetInvocationThis(this, inst);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003771 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
3772 CHECK(have_pending_hard_failure_);
3773 return nullptr;
3774 }
David Brazdil68b5c0b2016-01-19 14:25:29 +00003775 bool is_init = false;
3776 if (actual_arg_type.IsUninitializedTypes()) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003777 if (res_method) {
3778 if (!res_method->IsConstructor()) {
3779 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
3780 return nullptr;
3781 }
3782 } else {
3783 // Check whether the name of the called method is "<init>"
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00003784 const uint32_t method_idx = GetMethodIdxOfInvoke(inst);
Jeff Hao0d087272014-08-04 14:47:17 -07003785 if (strcmp(dex_file_->GetMethodName(dex_file_->GetMethodId(method_idx)), "<init>") != 0) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003786 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
3787 return nullptr;
3788 }
3789 }
David Brazdil68b5c0b2016-01-19 14:25:29 +00003790 is_init = true;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003791 }
David Brazdil68b5c0b2016-01-19 14:25:29 +00003792 const RegType& adjusted_type = is_init
3793 ? GetRegTypeCache()->FromUninitialized(actual_arg_type)
3794 : actual_arg_type;
Andreas Gampeeed3a5d2017-11-29 14:58:34 -08003795 if (method_type != METHOD_INTERFACE && !adjusted_type.IsZeroOrNull()) {
Ian Rogersd8f69b02014-09-10 21:43:52 +00003796 const RegType* res_method_class;
Andreas Gamped9e23012015-06-04 22:19:58 -07003797 // Miranda methods have the declaring interface as their declaring class, not the abstract
3798 // class. It would be wrong to use this for the type check (interface type checks are
3799 // postponed to runtime).
3800 if (res_method != nullptr && !res_method->IsMiranda()) {
Vladimir Markod93e3742018-07-18 10:58:13 +01003801 ObjPtr<mirror::Class> klass = res_method->GetDeclaringClass();
Ian Rogers1ff3c982014-08-12 02:30:58 -07003802 std::string temp;
Andreas Gampef23f33d2015-06-23 14:18:17 -07003803 res_method_class = &FromClass(klass->GetDescriptor(&temp), klass,
3804 klass->CannotBeAssignedFromOtherTypes());
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003805 } else {
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00003806 const uint32_t method_idx = GetMethodIdxOfInvoke(inst);
Andreas Gampea5b09a62016-11-17 15:21:22 -08003807 const dex::TypeIndex class_idx = dex_file_->GetMethodId(method_idx).class_idx_;
Mathieu Chartierde40d472015-10-15 17:47:48 -07003808 res_method_class = &reg_types_.FromDescriptor(
3809 GetClassLoader(),
3810 dex_file_->StringByTypeIdx(class_idx),
3811 false);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003812 }
David Brazdilca3c8c32016-09-06 14:04:48 +01003813 if (!res_method_class->IsAssignableFrom(adjusted_type, this)) {
David Brazdil68b5c0b2016-01-19 14:25:29 +00003814 Fail(adjusted_type.IsUnresolvedTypes()
3815 ? VERIFY_ERROR_NO_CLASS
3816 : VERIFY_ERROR_BAD_CLASS_SOFT)
3817 << "'this' argument '" << actual_arg_type << "' not instance of '"
3818 << *res_method_class << "'";
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003819 // Continue on soft failures. We need to find possible hard failures to avoid problems in
3820 // the compiler.
3821 if (have_pending_hard_failure_) {
3822 return nullptr;
3823 }
3824 }
3825 }
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003826 }
3827
Orion Hodsoncfa325e2016-10-13 10:25:54 +01003828 uint32_t arg[5];
3829 if (!is_range) {
3830 inst->GetVarArgs(arg);
3831 }
3832 uint32_t sig_registers = (method_type == METHOD_STATIC) ? 0 : 1;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003833 for ( ; it->HasNext(); it->Next()) {
3834 if (sig_registers >= expected_args) {
3835 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, expected " << inst->VRegA() <<
Orion Hodsoncfa325e2016-10-13 10:25:54 +01003836 " argument registers, method signature has " << sig_registers + 1 << " or more";
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003837 return nullptr;
3838 }
3839
3840 const char* param_descriptor = it->GetDescriptor();
3841
3842 if (param_descriptor == nullptr) {
3843 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation because of missing signature "
3844 "component";
3845 return nullptr;
3846 }
3847
Ian Rogersd8f69b02014-09-10 21:43:52 +00003848 const RegType& reg_type = reg_types_.FromDescriptor(GetClassLoader(), param_descriptor, false);
Orion Hodsoncfa325e2016-10-13 10:25:54 +01003849 uint32_t get_reg = is_range ? inst->VRegC() + static_cast<uint32_t>(sig_registers) :
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003850 arg[sig_registers];
3851 if (reg_type.IsIntegralTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003852 const RegType& src_type = work_line_->GetRegisterType(this, get_reg);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003853 if (!src_type.IsIntegralTypes()) {
3854 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register v" << get_reg << " has type " << src_type
3855 << " but expected " << reg_type;
Andreas Gampeb588f4c2015-05-26 13:35:39 -07003856 return nullptr;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003857 }
Andreas Gampeda9badb2015-06-05 20:22:12 -07003858 } else {
3859 if (!work_line_->VerifyRegisterType(this, get_reg, reg_type)) {
3860 // Continue on soft failures. We need to find possible hard failures to avoid problems in
3861 // the compiler.
3862 if (have_pending_hard_failure_) {
3863 return nullptr;
3864 }
3865 } else if (reg_type.IsLongOrDoubleTypes()) {
3866 // Check that registers are consecutive (for non-range invokes). Invokes are the only
3867 // instructions not specifying register pairs by the first component, but require them
3868 // nonetheless. Only check when there's an actual register in the parameters. If there's
3869 // none, this will fail below.
3870 if (!is_range && sig_registers + 1 < expected_args) {
3871 uint32_t second_reg = arg[sig_registers + 1];
3872 if (second_reg != get_reg + 1) {
3873 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, long or double parameter "
3874 "at index " << sig_registers << " is not a pair: " << get_reg << " + "
3875 << second_reg << ".";
3876 return nullptr;
3877 }
3878 }
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003879 }
3880 }
3881 sig_registers += reg_type.IsLongOrDoubleTypes() ? 2 : 1;
3882 }
3883 if (expected_args != sig_registers) {
3884 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, expected " << expected_args <<
Orion Hodsoncfa325e2016-10-13 10:25:54 +01003885 " argument registers, method signature has " << sig_registers;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003886 return nullptr;
3887 }
3888 return res_method;
3889}
3890
3891void MethodVerifier::VerifyInvocationArgsUnresolvedMethod(const Instruction* inst,
3892 MethodType method_type,
3893 bool is_range) {
3894 // As the method may not have been resolved, make this static check against what we expect.
3895 // The main reason for this code block is to fail hard when we find an illegal use, e.g.,
3896 // wrong number of arguments or wrong primitive types, even if the method could not be resolved.
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00003897 const uint32_t method_idx = GetMethodIdxOfInvoke(inst);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003898 DexFileParameterIterator it(*dex_file_,
3899 dex_file_->GetProtoId(dex_file_->GetMethodId(method_idx).proto_idx_));
Orion Hodsoncfa325e2016-10-13 10:25:54 +01003900 VerifyInvocationArgsFromIterator(&it, inst, method_type, is_range, nullptr);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003901}
3902
Orion Hodsonc069a302017-01-18 09:23:12 +00003903bool MethodVerifier::CheckCallSite(uint32_t call_site_idx) {
Orion Hodson3a842f52017-04-21 15:24:10 +01003904 if (call_site_idx >= dex_file_->NumCallSiteIds()) {
3905 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Bad call site id #" << call_site_idx
3906 << " >= " << dex_file_->NumCallSiteIds();
3907 return false;
3908 }
3909
Orion Hodsonc069a302017-01-18 09:23:12 +00003910 CallSiteArrayValueIterator it(*dex_file_, dex_file_->GetCallSiteId(call_site_idx));
3911 // Check essential arguments are provided. The dex file verifier has verified indicies of the
3912 // main values (method handle, name, method_type).
Orion Hodson4c8e12e2018-05-18 08:33:20 +01003913 static const size_t kRequiredArguments = 3;
3914 if (it.Size() < kRequiredArguments) {
Orion Hodsonc069a302017-01-18 09:23:12 +00003915 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Call site #" << call_site_idx
3916 << " has too few arguments: "
Orion Hodson4c8e12e2018-05-18 08:33:20 +01003917 << it.Size() << " < " << kRequiredArguments;
Orion Hodsonc069a302017-01-18 09:23:12 +00003918 return false;
3919 }
3920
Orion Hodson4c8e12e2018-05-18 08:33:20 +01003921 std::pair<const EncodedArrayValueIterator::ValueType, size_t> type_and_max[kRequiredArguments] =
3922 { { EncodedArrayValueIterator::ValueType::kMethodHandle, dex_file_->NumMethodHandles() },
3923 { EncodedArrayValueIterator::ValueType::kString, dex_file_->NumStringIds() },
3924 { EncodedArrayValueIterator::ValueType::kMethodType, dex_file_->NumProtoIds() }
3925 };
3926 uint32_t index[kRequiredArguments];
3927
3928 // Check arguments have expected types and are within permitted ranges.
3929 for (size_t i = 0; i < kRequiredArguments; ++i) {
3930 if (it.GetValueType() != type_and_max[i].first) {
3931 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Call site id #" << call_site_idx
3932 << " argument " << i << " has wrong type "
3933 << it.GetValueType() << "!=" << type_and_max[i].first;
3934 return false;
3935 }
3936 index[i] = static_cast<uint32_t>(it.GetJavaValue().i);
3937 if (index[i] >= type_and_max[i].second) {
3938 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Call site id #" << call_site_idx
3939 << " argument " << i << " bad index "
3940 << index[i] << " >= " << type_and_max[i].second;
3941 return false;
3942 }
3943 it.Next();
Orion Hodsona5dca522018-02-27 12:42:11 +00003944 }
Orion Hodson3a842f52017-04-21 15:24:10 +01003945
Orion Hodson4c8e12e2018-05-18 08:33:20 +01003946 // Check method handle kind is valid.
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08003947 const dex::MethodHandleItem& mh = dex_file_->GetMethodHandle(index[0]);
Orion Hodsonc069a302017-01-18 09:23:12 +00003948 if (mh.method_handle_type_ != static_cast<uint16_t>(DexFile::MethodHandleType::kInvokeStatic)) {
3949 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Call site #" << call_site_idx
Orion Hodson631827d2017-04-10 14:53:47 +01003950 << " argument 0 method handle type is not InvokeStatic: "
3951 << mh.method_handle_type_;
Orion Hodsonc069a302017-01-18 09:23:12 +00003952 return false;
3953 }
Orion Hodsonc069a302017-01-18 09:23:12 +00003954 return true;
3955}
3956
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003957class MethodParamListDescriptorIterator {
3958 public:
Mathieu Chartiere401d142015-04-22 13:56:20 -07003959 explicit MethodParamListDescriptorIterator(ArtMethod* res_method) :
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003960 res_method_(res_method), pos_(0), params_(res_method->GetParameterTypeList()),
3961 params_size_(params_ == nullptr ? 0 : params_->Size()) {
3962 }
3963
3964 bool HasNext() {
3965 return pos_ < params_size_;
3966 }
3967
3968 void Next() {
3969 ++pos_;
3970 }
3971
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07003972 const char* GetDescriptor() REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003973 return res_method_->GetTypeDescriptorFromTypeIdx(params_->GetTypeItem(pos_).type_idx_);
3974 }
3975
3976 private:
Mathieu Chartiere401d142015-04-22 13:56:20 -07003977 ArtMethod* res_method_;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003978 size_t pos_;
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08003979 const dex::TypeList* params_;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003980 const size_t params_size_;
3981};
3982
Mathieu Chartiere401d142015-04-22 13:56:20 -07003983ArtMethod* MethodVerifier::VerifyInvocationArgs(
Alex Light7268d472016-01-20 15:50:01 -08003984 const Instruction* inst, MethodType method_type, bool is_range) {
jeffhao8cd6dda2012-02-22 10:15:34 -08003985 // Resolve the method. This could be an abstract or concrete method depending on what sort of call
3986 // we're making.
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00003987 const uint32_t method_idx = GetMethodIdxOfInvoke(inst);
Alex Light7268d472016-01-20 15:50:01 -08003988 ArtMethod* res_method = ResolveMethodAndCheckAccess(method_idx, method_type);
Ian Rogers7b078e82014-09-10 14:44:24 -07003989 if (res_method == nullptr) { // error or class is unresolved
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003990 // Check what we can statically.
3991 if (!have_pending_hard_failure_) {
3992 VerifyInvocationArgsUnresolvedMethod(inst, method_type, is_range);
3993 }
3994 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003995 }
3996
Ian Rogersd81871c2011-10-03 13:57:23 -07003997 // If we're using invoke-super(method), make sure that the executing method's class' superclass
Alex Light705ad492015-09-21 11:36:30 -07003998 // has a vtable entry for the target method. Or the target is on a interface.
Alex Light7268d472016-01-20 15:50:01 -08003999 if (method_type == METHOD_SUPER) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08004000 dex::TypeIndex class_idx = dex_file_->GetMethodId(method_idx).class_idx_;
David Brazdilca3c8c32016-09-06 14:04:48 +01004001 const RegType& reference_type = reg_types_.FromDescriptor(
4002 GetClassLoader(),
4003 dex_file_->StringByTypeIdx(class_idx),
4004 false);
4005 if (reference_type.IsUnresolvedTypes()) {
Alex Lightfedd91d2016-01-07 14:49:16 -08004006 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "Unable to find referenced class from invoke-super";
4007 return nullptr;
4008 }
David Brazdilca3c8c32016-09-06 14:04:48 +01004009 if (reference_type.GetClass()->IsInterface()) {
Alex Lightfedd91d2016-01-07 14:49:16 -08004010 // TODO Can we verify anything else.
David Brazdil15fc7292016-09-02 14:13:18 +01004011 if (class_idx == class_def_.class_idx_) {
Alex Lightfedd91d2016-01-07 14:49:16 -08004012 Fail(VERIFY_ERROR_CLASS_CHANGE) << "Cannot invoke-super on self as interface";
Alex Light55ea94d2016-03-15 09:50:26 -07004013 return nullptr;
Alex Lightfedd91d2016-01-07 14:49:16 -08004014 }
4015 // TODO Revisit whether we want to allow invoke-super on direct interfaces only like the JLS
4016 // does.
Alex Light55ea94d2016-03-15 09:50:26 -07004017 if (!GetDeclaringClass().HasClass()) {
4018 Fail(VERIFY_ERROR_NO_CLASS) << "Unable to resolve the full class of 'this' used in an"
4019 << "interface invoke-super";
4020 return nullptr;
David Brazdilca3c8c32016-09-06 14:04:48 +01004021 } else if (!reference_type.IsStrictlyAssignableFrom(GetDeclaringClass(), this)) {
Alex Lightfedd91d2016-01-07 14:49:16 -08004022 Fail(VERIFY_ERROR_CLASS_CHANGE)
David Sehr709b0702016-10-13 09:12:37 -07004023 << "invoke-super in " << mirror::Class::PrettyClass(GetDeclaringClass().GetClass())
4024 << " in method "
4025 << dex_file_->PrettyMethod(dex_method_idx_) << " to method "
4026 << dex_file_->PrettyMethod(method_idx) << " references "
4027 << "non-super-interface type " << mirror::Class::PrettyClass(reference_type.GetClass());
Alex Lightfedd91d2016-01-07 14:49:16 -08004028 return nullptr;
Alex Light705ad492015-09-21 11:36:30 -07004029 }
4030 } else {
4031 const RegType& super = GetDeclaringClass().GetSuperClass(&reg_types_);
4032 if (super.IsUnresolvedTypes()) {
4033 Fail(VERIFY_ERROR_NO_METHOD) << "unknown super class in invoke-super from "
David Sehr709b0702016-10-13 09:12:37 -07004034 << dex_file_->PrettyMethod(dex_method_idx_)
4035 << " to super " << res_method->PrettyMethod();
Alex Light705ad492015-09-21 11:36:30 -07004036 return nullptr;
4037 }
David Brazdilca3c8c32016-09-06 14:04:48 +01004038 if (!reference_type.IsStrictlyAssignableFrom(GetDeclaringClass(), this) ||
Aart Bikf663e342016-04-04 17:28:59 -07004039 (res_method->GetMethodIndex() >= super.GetClass()->GetVTableLength())) {
Alex Light705ad492015-09-21 11:36:30 -07004040 Fail(VERIFY_ERROR_NO_METHOD) << "invalid invoke-super from "
David Sehr709b0702016-10-13 09:12:37 -07004041 << dex_file_->PrettyMethod(dex_method_idx_)
Alex Light705ad492015-09-21 11:36:30 -07004042 << " to super " << super
4043 << "." << res_method->GetName()
4044 << res_method->GetSignature();
4045 return nullptr;
4046 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004047 }
4048 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08004049
Andreas Gampe74979b12017-05-16 09:28:06 -07004050 if (UNLIKELY(method_type == METHOD_POLYMORPHIC)) {
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004051 // Process the signature of the calling site that is invoking the method handle.
Orion Hodson06d10a72018-05-14 08:53:38 +01004052 dex::ProtoIndex proto_idx(inst->VRegH());
4053 DexFileParameterIterator it(*dex_file_, dex_file_->GetProtoId(proto_idx));
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004054 return VerifyInvocationArgsFromIterator(&it, inst, method_type, is_range, res_method);
4055 } else {
4056 // Process the target method's signature.
4057 MethodParamListDescriptorIterator it(res_method);
4058 return VerifyInvocationArgsFromIterator(&it, inst, method_type, is_range, res_method);
4059 }
4060}
4061
4062bool MethodVerifier::CheckSignaturePolymorphicMethod(ArtMethod* method) {
Vladimir Markod93e3742018-07-18 10:58:13 +01004063 ObjPtr<mirror::Class> klass = method->GetDeclaringClass();
Orion Hodsonfe92d122018-01-02 10:45:17 +00004064 const char* method_name = method->GetName();
4065
4066 const char* expected_return_descriptor;
Vladimir Markoc7aa87e2018-05-24 15:19:52 +01004067 ObjPtr<mirror::ObjectArray<mirror::Class>> class_roots =
4068 Runtime::Current()->GetClassLinker()->GetClassRoots();
4069 if (klass == GetClassRoot<mirror::MethodHandle>(class_roots)) {
Orion Hodsonfe92d122018-01-02 10:45:17 +00004070 expected_return_descriptor = mirror::MethodHandle::GetReturnTypeDescriptor(method_name);
Vladimir Markoc7aa87e2018-05-24 15:19:52 +01004071 } else if (klass == GetClassRoot<mirror::VarHandle>(class_roots)) {
Orion Hodsonfe92d122018-01-02 10:45:17 +00004072 expected_return_descriptor = mirror::VarHandle::GetReturnTypeDescriptor(method_name);
4073 } else {
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004074 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
Orion Hodsonfe92d122018-01-02 10:45:17 +00004075 << "Signature polymorphic method in unsuppported class: " << klass->PrettyDescriptor();
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004076 return false;
4077 }
4078
Orion Hodsonfe92d122018-01-02 10:45:17 +00004079 if (expected_return_descriptor == nullptr) {
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004080 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
4081 << "Signature polymorphic method name invalid: " << method_name;
4082 return false;
4083 }
4084
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08004085 const dex::TypeList* types = method->GetParameterTypeList();
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004086 if (types->Size() != 1) {
4087 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
4088 << "Signature polymorphic method has too many arguments " << types->Size() << " != 1";
4089 return false;
4090 }
4091
4092 const dex::TypeIndex argument_type_index = types->GetTypeItem(0).type_idx_;
4093 const char* argument_descriptor = method->GetTypeDescriptorFromTypeIdx(argument_type_index);
4094 if (strcmp(argument_descriptor, "[Ljava/lang/Object;") != 0) {
4095 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
4096 << "Signature polymorphic method has unexpected argument type: " << argument_descriptor;
4097 return false;
4098 }
4099
4100 const char* return_descriptor = method->GetReturnTypeDescriptor();
Orion Hodsonfe92d122018-01-02 10:45:17 +00004101 if (strcmp(return_descriptor, expected_return_descriptor) != 0) {
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004102 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
Orion Hodsonfe92d122018-01-02 10:45:17 +00004103 << "Signature polymorphic method has unexpected return type: " << return_descriptor
4104 << " != " << expected_return_descriptor;
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004105 return false;
4106 }
4107
4108 return true;
4109}
4110
4111bool MethodVerifier::CheckSignaturePolymorphicReceiver(const Instruction* inst) {
4112 const RegType& this_type = work_line_->GetInvocationThis(this, inst);
Andreas Gampeeed3a5d2017-11-29 14:58:34 -08004113 if (this_type.IsZeroOrNull()) {
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004114 /* null pointer always passes (and always fails at run time) */
4115 return true;
4116 } else if (!this_type.IsNonZeroReferenceTypes()) {
4117 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
4118 << "invoke-polymorphic receiver is not a reference: "
4119 << this_type;
4120 return false;
4121 } else if (this_type.IsUninitializedReference()) {
4122 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
4123 << "invoke-polymorphic receiver is uninitialized: "
4124 << this_type;
4125 return false;
4126 } else if (!this_type.HasClass()) {
4127 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
4128 << "invoke-polymorphic receiver has no class: "
4129 << this_type;
4130 return false;
Vladimir Markoc7aa87e2018-05-24 15:19:52 +01004131 } else {
4132 ObjPtr<mirror::ObjectArray<mirror::Class>> class_roots =
4133 Runtime::Current()->GetClassLinker()->GetClassRoots();
4134 if (!this_type.GetClass()->IsSubClass(GetClassRoot<mirror::MethodHandle>(class_roots)) &&
4135 !this_type.GetClass()->IsSubClass(GetClassRoot<mirror::VarHandle>(class_roots))) {
4136 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
4137 << "invoke-polymorphic receiver is not a subclass of MethodHandle or VarHandle: "
4138 << this_type;
4139 return false;
4140 }
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004141 }
4142 return true;
Ian Rogersd81871c2011-10-03 13:57:23 -07004143}
4144
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00004145uint16_t MethodVerifier::GetMethodIdxOfInvoke(const Instruction* inst) {
4146 switch (inst->Opcode()) {
4147 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK:
4148 case Instruction::INVOKE_VIRTUAL_QUICK: {
4149 DCHECK(Runtime::Current()->IsStarted() || verify_to_dump_)
4150 << dex_file_->PrettyMethod(dex_method_idx_, true) << "@" << work_insn_idx_;
4151 DCHECK(method_being_verified_ != nullptr);
4152 uint16_t method_idx = method_being_verified_->GetIndexFromQuickening(work_insn_idx_);
4153 CHECK_NE(method_idx, DexFile::kDexNoIndex16);
4154 return method_idx;
4155 }
4156 default: {
4157 return inst->VRegB();
4158 }
Mathieu Chartier091d2382015-03-06 10:59:06 -08004159 }
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004160}
4161
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00004162uint16_t MethodVerifier::GetFieldIdxOfFieldAccess(const Instruction* inst, bool is_static) {
4163 if (is_static) {
4164 return inst->VRegB_21c();
4165 } else if (inst->IsQuickened()) {
4166 DCHECK(Runtime::Current()->IsStarted() || verify_to_dump_);
4167 DCHECK(method_being_verified_ != nullptr);
4168 uint16_t field_idx = method_being_verified_->GetIndexFromQuickening(work_insn_idx_);
4169 CHECK_NE(field_idx, DexFile::kDexNoIndex16);
4170 return field_idx;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004171 } else {
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00004172 return inst->VRegC_22c();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004173 }
4174}
4175
Ian Rogers62342ec2013-06-11 10:26:37 -07004176void MethodVerifier::VerifyNewArray(const Instruction* inst, bool is_filled, bool is_range) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08004177 dex::TypeIndex type_idx;
Sebastien Hertz5243e912013-05-21 10:55:07 +02004178 if (!is_filled) {
4179 DCHECK_EQ(inst->Opcode(), Instruction::NEW_ARRAY);
Andreas Gampea5b09a62016-11-17 15:21:22 -08004180 type_idx = dex::TypeIndex(inst->VRegC_22c());
Sebastien Hertz5243e912013-05-21 10:55:07 +02004181 } else if (!is_range) {
4182 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY);
Andreas Gampea5b09a62016-11-17 15:21:22 -08004183 type_idx = dex::TypeIndex(inst->VRegB_35c());
Sebastien Hertz5243e912013-05-21 10:55:07 +02004184 } else {
4185 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY_RANGE);
Andreas Gampea5b09a62016-11-17 15:21:22 -08004186 type_idx = dex::TypeIndex(inst->VRegB_3rc());
Sebastien Hertz5243e912013-05-21 10:55:07 +02004187 }
Andreas Gampe98be1a92017-08-28 08:25:45 -07004188 const RegType& res_type = ResolveClass<CheckAccess::kYes>(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004189 if (res_type.IsConflict()) { // bad class
4190 DCHECK_NE(failures_.size(), 0U);
Ian Rogers0c4a5062012-02-03 15:18:59 -08004191 } else {
4192 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
4193 if (!res_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07004194 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "new-array on non-array class " << res_type;
Ian Rogers0c4a5062012-02-03 15:18:59 -08004195 } else if (!is_filled) {
4196 /* make sure "size" register is valid type */
Ian Rogers7b078e82014-09-10 14:44:24 -07004197 work_line_->VerifyRegisterType(this, inst->VRegB_22c(), reg_types_.Integer());
Ian Rogers0c4a5062012-02-03 15:18:59 -08004198 /* set register type to array class */
Ian Rogersd8f69b02014-09-10 21:43:52 +00004199 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
Andreas Gampead238ce2015-08-24 21:13:08 -07004200 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_22c(), precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08004201 } else {
Andreas Gampebb18a032016-03-22 20:34:25 -07004202 DCHECK(!res_type.IsUnresolvedMergedReference());
Ian Rogers0c4a5062012-02-03 15:18:59 -08004203 // Verify each register. If "arg_count" is bad, VerifyRegisterType() will run off the end of
4204 // the list and fail. It's legal, if silly, for arg_count to be zero.
Ian Rogersd8f69b02014-09-10 21:43:52 +00004205 const RegType& expected_type = reg_types_.GetComponentType(res_type, GetClassLoader());
Sebastien Hertz5243e912013-05-21 10:55:07 +02004206 uint32_t arg_count = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
4207 uint32_t arg[5];
4208 if (!is_range) {
Ian Rogers29a26482014-05-02 15:27:29 -07004209 inst->GetVarArgs(arg);
Sebastien Hertz5243e912013-05-21 10:55:07 +02004210 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08004211 for (size_t ui = 0; ui < arg_count; ui++) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02004212 uint32_t get_reg = is_range ? inst->VRegC_3rc() + ui : arg[ui];
Ian Rogers7b078e82014-09-10 14:44:24 -07004213 if (!work_line_->VerifyRegisterType(this, get_reg, expected_type)) {
4214 work_line_->SetResultRegisterType(this, reg_types_.Conflict());
Ian Rogers0c4a5062012-02-03 15:18:59 -08004215 return;
4216 }
4217 }
4218 // filled-array result goes into "result" register
Ian Rogersd8f69b02014-09-10 21:43:52 +00004219 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
Ian Rogers7b078e82014-09-10 14:44:24 -07004220 work_line_->SetResultRegisterType(this, precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08004221 }
4222 }
4223}
4224
Sebastien Hertz5243e912013-05-21 10:55:07 +02004225void MethodVerifier::VerifyAGet(const Instruction* inst,
Ian Rogersd8f69b02014-09-10 21:43:52 +00004226 const RegType& insn_type, bool is_primitive) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004227 const RegType& index_type = work_line_->GetRegisterType(this, inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07004228 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07004229 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07004230 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004231 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegB_23x());
Andreas Gampeeed3a5d2017-11-29 14:58:34 -08004232 if (array_type.IsZeroOrNull()) {
Ian Rogers89310de2012-02-01 13:47:30 -08004233 // Null array class; this code path will fail at runtime. Infer a merge-able type from the
Andreas Gampe52f205a2017-12-01 12:16:07 -08004234 // instruction type.
4235 if (!is_primitive) {
4236 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), reg_types_.Null());
4237 } else if (insn_type.IsInteger()) {
4238 // Pick a non-zero constant (to distinguish with null) that can fit in any primitive.
4239 // We cannot use 'insn_type' as it could be a float array or an int array.
4240 work_line_->SetRegisterType<LockOp::kClear>(
4241 this, inst->VRegA_23x(), DetermineCat1Constant(1, need_precise_constants_));
4242 } else if (insn_type.IsCategory1Types()) {
4243 // Category 1
4244 // The 'insn_type' is exactly the type we need.
4245 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), insn_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07004246 } else {
Ian Rogers89310de2012-02-01 13:47:30 -08004247 // Category 2
Ian Rogers7b078e82014-09-10 14:44:24 -07004248 work_line_->SetRegisterTypeWide(this, inst->VRegA_23x(),
4249 reg_types_.FromCat2ConstLo(0, false),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004250 reg_types_.FromCat2ConstHi(0, false));
Ian Rogers89310de2012-02-01 13:47:30 -08004251 }
jeffhaofc3144e2012-02-01 17:21:15 -08004252 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07004253 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aget";
Andreas Gampe8d8fc482016-03-25 16:24:20 -07004254 } else if (array_type.IsUnresolvedMergedReference()) {
Andreas Gampebb18a032016-03-22 20:34:25 -07004255 // Unresolved array types must be reference array types.
4256 if (is_primitive) {
4257 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type
4258 << " source for category 1 aget";
4259 } else {
4260 Fail(VERIFY_ERROR_NO_CLASS) << "cannot verify aget for " << array_type
4261 << " because of missing class";
Andreas Gampe8d8fc482016-03-25 16:24:20 -07004262 // Approximate with java.lang.Object[].
4263 work_line_->SetRegisterType<LockOp::kClear>(this,
4264 inst->VRegA_23x(),
4265 reg_types_.JavaLangObject(false));
Andreas Gampebb18a032016-03-22 20:34:25 -07004266 }
Ian Rogers89310de2012-02-01 13:47:30 -08004267 } else {
4268 /* verify the class */
Ian Rogersd8f69b02014-09-10 21:43:52 +00004269 const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
jeffhaofc3144e2012-02-01 17:21:15 -08004270 if (!component_type.IsReferenceTypes() && !is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07004271 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08004272 << " source for aget-object";
4273 } else if (component_type.IsNonZeroReferenceTypes() && is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07004274 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08004275 << " source for category 1 aget";
4276 } else if (is_primitive && !insn_type.Equals(component_type) &&
4277 !((insn_type.IsInteger() && component_type.IsFloat()) ||
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004278 (insn_type.IsLong() && component_type.IsDouble()))) {
4279 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array type " << array_type
4280 << " incompatible with aget of type " << insn_type;
Ian Rogers89310de2012-02-01 13:47:30 -08004281 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004282 // Use knowledge of the field type which is stronger than the type inferred from the
4283 // instruction, which can't differentiate object types and ints from floats, longs from
4284 // doubles.
4285 if (!component_type.IsLowHalf()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07004286 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), component_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004287 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004288 work_line_->SetRegisterTypeWide(this, inst->VRegA_23x(), component_type,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004289 component_type.HighHalf(&reg_types_));
4290 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004291 }
4292 }
4293 }
4294}
4295
Ian Rogersd8f69b02014-09-10 21:43:52 +00004296void MethodVerifier::VerifyPrimitivePut(const RegType& target_type, const RegType& insn_type,
Jeff Haofe1f7c82013-08-01 14:50:24 -07004297 const uint32_t vregA) {
4298 // Primitive assignability rules are weaker than regular assignability rules.
4299 bool instruction_compatible;
4300 bool value_compatible;
Ian Rogers7b078e82014-09-10 14:44:24 -07004301 const RegType& value_type = work_line_->GetRegisterType(this, vregA);
Jeff Haofe1f7c82013-08-01 14:50:24 -07004302 if (target_type.IsIntegralTypes()) {
Jeff Haoa4647482013-08-06 15:35:47 -07004303 instruction_compatible = target_type.Equals(insn_type);
Jeff Haofe1f7c82013-08-01 14:50:24 -07004304 value_compatible = value_type.IsIntegralTypes();
4305 } else if (target_type.IsFloat()) {
4306 instruction_compatible = insn_type.IsInteger(); // no put-float, so expect put-int
4307 value_compatible = value_type.IsFloatTypes();
4308 } else if (target_type.IsLong()) {
4309 instruction_compatible = insn_type.IsLong();
Andreas Gampe376fa682014-09-07 13:06:12 -07004310 // Additional register check: this is not checked statically (as part of VerifyInstructions),
4311 // as target_type depends on the resolved type of the field.
4312 if (instruction_compatible && work_line_->NumRegs() > vregA + 1) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004313 const RegType& value_type_hi = work_line_->GetRegisterType(this, vregA + 1);
Andreas Gampe376fa682014-09-07 13:06:12 -07004314 value_compatible = value_type.IsLongTypes() && value_type.CheckWidePair(value_type_hi);
4315 } else {
4316 value_compatible = false;
4317 }
Jeff Haofe1f7c82013-08-01 14:50:24 -07004318 } else if (target_type.IsDouble()) {
4319 instruction_compatible = insn_type.IsLong(); // no put-double, so expect put-long
Andreas Gampe376fa682014-09-07 13:06:12 -07004320 // Additional register check: this is not checked statically (as part of VerifyInstructions),
4321 // as target_type depends on the resolved type of the field.
4322 if (instruction_compatible && work_line_->NumRegs() > vregA + 1) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004323 const RegType& value_type_hi = work_line_->GetRegisterType(this, vregA + 1);
Andreas Gampe376fa682014-09-07 13:06:12 -07004324 value_compatible = value_type.IsDoubleTypes() && value_type.CheckWidePair(value_type_hi);
4325 } else {
4326 value_compatible = false;
4327 }
Jeff Haofe1f7c82013-08-01 14:50:24 -07004328 } else {
4329 instruction_compatible = false; // reference with primitive store
4330 value_compatible = false; // unused
4331 }
4332 if (!instruction_compatible) {
4333 // This is a global failure rather than a class change failure as the instructions and
4334 // the descriptors for the type should have been consistent within the same file at
4335 // compile time.
4336 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "put insn has type '" << insn_type
4337 << "' but expected type '" << target_type << "'";
4338 return;
4339 }
4340 if (!value_compatible) {
4341 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
4342 << " of type " << value_type << " but expected " << target_type << " for put";
4343 return;
4344 }
4345}
4346
Sebastien Hertz5243e912013-05-21 10:55:07 +02004347void MethodVerifier::VerifyAPut(const Instruction* inst,
Ian Rogersd8f69b02014-09-10 21:43:52 +00004348 const RegType& insn_type, bool is_primitive) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004349 const RegType& index_type = work_line_->GetRegisterType(this, inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07004350 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07004351 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07004352 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004353 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegB_23x());
Andreas Gampeeed3a5d2017-11-29 14:58:34 -08004354 if (array_type.IsZeroOrNull()) {
Nicolas Geoffray66389fb2015-06-19 10:35:42 +01004355 // Null array type; this code path will fail at runtime.
4356 // Still check that the given value matches the instruction's type.
Andreas Gampe4bf4c782015-08-14 14:07:43 -07004357 // Note: this is, as usual, complicated by the fact the the instruction isn't fully typed
4358 // and fits multiple register types.
4359 const RegType* modified_reg_type = &insn_type;
4360 if ((modified_reg_type == &reg_types_.Integer()) ||
4361 (modified_reg_type == &reg_types_.LongLo())) {
4362 // May be integer or float | long or double. Overwrite insn_type accordingly.
4363 const RegType& value_type = work_line_->GetRegisterType(this, inst->VRegA_23x());
4364 if (modified_reg_type == &reg_types_.Integer()) {
4365 if (&value_type == &reg_types_.Float()) {
4366 modified_reg_type = &value_type;
4367 }
4368 } else {
4369 if (&value_type == &reg_types_.DoubleLo()) {
4370 modified_reg_type = &value_type;
4371 }
4372 }
4373 }
4374 work_line_->VerifyRegisterType(this, inst->VRegA_23x(), *modified_reg_type);
jeffhaofc3144e2012-02-01 17:21:15 -08004375 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07004376 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aput";
Andreas Gampe8d8fc482016-03-25 16:24:20 -07004377 } else if (array_type.IsUnresolvedMergedReference()) {
Andreas Gampebb18a032016-03-22 20:34:25 -07004378 // Unresolved array types must be reference array types.
4379 if (is_primitive) {
4380 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "put insn has type '" << insn_type
4381 << "' but unresolved type '" << array_type << "'";
4382 } else {
4383 Fail(VERIFY_ERROR_NO_CLASS) << "cannot verify aput for " << array_type
4384 << " because of missing class";
4385 }
Ian Rogers89310de2012-02-01 13:47:30 -08004386 } else {
Ian Rogersd8f69b02014-09-10 21:43:52 +00004387 const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
Jeff Haofe1f7c82013-08-01 14:50:24 -07004388 const uint32_t vregA = inst->VRegA_23x();
Jeff Haob24b4a72013-07-31 13:47:31 -07004389 if (is_primitive) {
Jeff Haofe1f7c82013-08-01 14:50:24 -07004390 VerifyPrimitivePut(component_type, insn_type, vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07004391 } else {
Jeff Haob24b4a72013-07-31 13:47:31 -07004392 if (!component_type.IsReferenceTypes()) {
4393 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
4394 << " source for aput-object";
4395 } else {
4396 // The instruction agrees with the type of array, confirm the value to be stored does too
4397 // Note: we use the instruction type (rather than the component type) for aput-object as
4398 // incompatible classes will be caught at runtime as an array store exception
Ian Rogers7b078e82014-09-10 14:44:24 -07004399 work_line_->VerifyRegisterType(this, vregA, insn_type);
Jeff Haob24b4a72013-07-31 13:47:31 -07004400 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004401 }
4402 }
4403 }
4404}
4405
Mathieu Chartierc7853442015-03-27 14:35:38 -07004406ArtField* MethodVerifier::GetStaticField(int field_idx) {
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08004407 const dex::FieldId& field_id = dex_file_->GetFieldId(field_idx);
Ian Rogers90040192011-12-16 08:54:29 -08004408 // Check access to class
Andreas Gampe98be1a92017-08-28 08:25:45 -07004409 const RegType& klass_type = ResolveClass<CheckAccess::kYes>(field_id.class_idx_);
Brian Carlstrom7934ac22013-07-26 10:54:15 -07004410 if (klass_type.IsConflict()) { // bad class
Ian Rogersad0b3a32012-04-16 14:50:24 -07004411 AppendToLastFailMessage(StringPrintf(" in attempt to access static field %d (%s) in %s",
4412 field_idx, dex_file_->GetFieldName(field_id),
4413 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers7b078e82014-09-10 14:44:24 -07004414 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08004415 }
Elliott Hughesb25c3f62012-03-26 16:35:06 -07004416 if (klass_type.IsUnresolvedTypes()) {
Andreas Gampe629be512017-08-25 17:09:32 -07004417 // Accessibility checks depend on resolved fields.
David Brazdil2bb2fbd2018-11-13 18:24:26 +00004418 DCHECK(klass_type.Equals(GetDeclaringClass()) ||
4419 !failures_.empty() ||
4420 IsSdkVersionSetAndLessThan(api_level_, SdkVersion::kP));
Andreas Gampe629be512017-08-25 17:09:32 -07004421
Ian Rogers7b078e82014-09-10 14:44:24 -07004422 return nullptr; // Can't resolve Class so no more to do here, will do checking at runtime.
Ian Rogers90040192011-12-16 08:54:29 -08004423 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07004424 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Vladimir Markoe11dd502017-12-08 14:09:45 +00004425 ArtField* field = class_linker->ResolveFieldJLS(field_idx, dex_cache_, class_loader_);
David Brazdilca3c8c32016-09-06 14:04:48 +01004426
4427 // Record result of the field resolution attempt.
4428 VerifierDeps::MaybeRecordFieldResolution(*dex_file_, field_idx, field);
4429
Ian Rogers7b078e82014-09-10 14:44:24 -07004430 if (field == nullptr) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07004431 VLOG(verifier) << "Unable to resolve static field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07004432 << dex_file_->GetFieldName(field_id) << ") in "
4433 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogers7b078e82014-09-10 14:44:24 -07004434 DCHECK(self_->IsExceptionPending());
4435 self_->ClearException();
4436 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004437 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
4438 field->GetAccessFlags())) {
David Sehr709b0702016-10-13 09:12:37 -07004439 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access static field " << field->PrettyField()
Ian Rogersad0b3a32012-04-16 14:50:24 -07004440 << " from " << GetDeclaringClass();
Ian Rogers7b078e82014-09-10 14:44:24 -07004441 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07004442 } else if (!field->IsStatic()) {
David Sehr709b0702016-10-13 09:12:37 -07004443 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << field->PrettyField() << " to be static";
Ian Rogers7b078e82014-09-10 14:44:24 -07004444 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07004445 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07004446 return field;
Ian Rogersd81871c2011-10-03 13:57:23 -07004447}
4448
Mathieu Chartierc7853442015-03-27 14:35:38 -07004449ArtField* MethodVerifier::GetInstanceField(const RegType& obj_type, int field_idx) {
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08004450 const dex::FieldId& field_id = dex_file_->GetFieldId(field_idx);
Aart Bik31883642016-06-06 15:02:44 -07004451 // Check access to class.
Andreas Gampe98be1a92017-08-28 08:25:45 -07004452 const RegType& klass_type = ResolveClass<CheckAccess::kYes>(field_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004453 if (klass_type.IsConflict()) {
4454 AppendToLastFailMessage(StringPrintf(" in attempt to access instance field %d (%s) in %s",
4455 field_idx, dex_file_->GetFieldName(field_id),
4456 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers7b078e82014-09-10 14:44:24 -07004457 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08004458 }
jeffhao8cd6dda2012-02-22 10:15:34 -08004459 if (klass_type.IsUnresolvedTypes()) {
Andreas Gampe629be512017-08-25 17:09:32 -07004460 // Accessibility checks depend on resolved fields.
David Brazdil2bb2fbd2018-11-13 18:24:26 +00004461 DCHECK(klass_type.Equals(GetDeclaringClass()) ||
4462 !failures_.empty() ||
4463 IsSdkVersionSetAndLessThan(api_level_, SdkVersion::kP));
Andreas Gampe629be512017-08-25 17:09:32 -07004464
Ian Rogers7b078e82014-09-10 14:44:24 -07004465 return nullptr; // Can't resolve Class so no more to do here
Ian Rogers90040192011-12-16 08:54:29 -08004466 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07004467 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Vladimir Markoe11dd502017-12-08 14:09:45 +00004468 ArtField* field = class_linker->ResolveFieldJLS(field_idx, dex_cache_, class_loader_);
David Brazdilca3c8c32016-09-06 14:04:48 +01004469
4470 // Record result of the field resolution attempt.
4471 VerifierDeps::MaybeRecordFieldResolution(*dex_file_, field_idx, field);
4472
Ian Rogers7b078e82014-09-10 14:44:24 -07004473 if (field == nullptr) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07004474 VLOG(verifier) << "Unable to resolve instance field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07004475 << dex_file_->GetFieldName(field_id) << ") in "
4476 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogers7b078e82014-09-10 14:44:24 -07004477 DCHECK(self_->IsExceptionPending());
4478 self_->ClearException();
4479 return nullptr;
Andreas Gampeeed3a5d2017-11-29 14:58:34 -08004480 } else if (obj_type.IsZeroOrNull()) {
Aart Bik31883642016-06-06 15:02:44 -07004481 // Cannot infer and check type, however, access will cause null pointer exception.
4482 // Fall through into a few last soft failure checks below.
Stephen Kyle695c5982014-08-22 15:03:07 +01004483 } else if (!obj_type.IsReferenceTypes()) {
Aart Bik31883642016-06-06 15:02:44 -07004484 // Trying to read a field from something that isn't a reference.
Stephen Kyle695c5982014-08-22 15:03:07 +01004485 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance field access on object that has "
4486 << "non-reference type " << obj_type;
Ian Rogers7b078e82014-09-10 14:44:24 -07004487 return nullptr;
Ian Rogerse1758fe2012-04-19 11:31:15 -07004488 } else {
David Brazdil0d638bb2016-07-27 15:29:25 +01004489 std::string temp;
Mathieu Chartier3398c782016-09-30 10:27:43 -07004490 ObjPtr<mirror::Class> klass = field->GetDeclaringClass();
Ian Rogersd8f69b02014-09-10 21:43:52 +00004491 const RegType& field_klass =
Vladimir Markobcf17522018-06-01 13:14:32 +01004492 FromClass(klass->GetDescriptor(&temp), klass, klass->CannotBeAssignedFromOtherTypes());
David Brazdil68b5c0b2016-01-19 14:25:29 +00004493 if (obj_type.IsUninitializedTypes()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07004494 // Field accesses through uninitialized references are only allowable for constructors where
David Brazdil68b5c0b2016-01-19 14:25:29 +00004495 // the field is declared in this class.
4496 // Note: this IsConstructor check is technically redundant, as UninitializedThis should only
4497 // appear in constructors.
4498 if (!obj_type.IsUninitializedThisReference() ||
4499 !IsConstructor() ||
4500 !field_klass.Equals(GetDeclaringClass())) {
David Sehr709b0702016-10-13 09:12:37 -07004501 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "cannot access instance field " << field->PrettyField()
David Brazdil68b5c0b2016-01-19 14:25:29 +00004502 << " of a not fully initialized object within the context"
David Sehr709b0702016-10-13 09:12:37 -07004503 << " of " << dex_file_->PrettyMethod(dex_method_idx_);
David Brazdil68b5c0b2016-01-19 14:25:29 +00004504 return nullptr;
4505 }
David Brazdilca3c8c32016-09-06 14:04:48 +01004506 } else if (!field_klass.IsAssignableFrom(obj_type, this)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07004507 // Trying to access C1.field1 using reference of type C2, which is neither C1 or a sub-class
4508 // of C1. For resolution to occur the declared class of the field must be compatible with
4509 // obj_type, we've discovered this wasn't so, so report the field didn't exist.
Andreas Gampe66596242016-04-14 10:55:04 -07004510 VerifyError type;
4511 bool is_aot = Runtime::Current()->IsAotCompiler();
4512 if (is_aot && (field_klass.IsUnresolvedTypes() || obj_type.IsUnresolvedTypes())) {
4513 // Compiler & unresolved types involved, retry at runtime.
4514 type = VerifyError::VERIFY_ERROR_NO_CLASS;
4515 } else {
Andreas Gampe8f4ade02016-04-15 10:09:16 -07004516 // Classes known (resolved; and thus assignability check is precise), or we are at runtime
4517 // and still missing classes. This is a hard failure.
Andreas Gampe66596242016-04-14 10:55:04 -07004518 type = VerifyError::VERIFY_ERROR_BAD_CLASS_HARD;
4519 }
David Sehr709b0702016-10-13 09:12:37 -07004520 Fail(type) << "cannot access instance field " << field->PrettyField()
Andreas Gampe66596242016-04-14 10:55:04 -07004521 << " from object of type " << obj_type;
Ian Rogers7b078e82014-09-10 14:44:24 -07004522 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004523 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004524 }
Aart Bik31883642016-06-06 15:02:44 -07004525
4526 // Few last soft failure checks.
4527 if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
4528 field->GetAccessFlags())) {
David Sehr709b0702016-10-13 09:12:37 -07004529 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access instance field " << field->PrettyField()
Aart Bik31883642016-06-06 15:02:44 -07004530 << " from " << GetDeclaringClass();
4531 return nullptr;
4532 } else if (field->IsStatic()) {
David Sehr709b0702016-10-13 09:12:37 -07004533 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << field->PrettyField()
Aart Bik31883642016-06-06 15:02:44 -07004534 << " to not be static";
4535 return nullptr;
4536 }
4537
4538 return field;
Ian Rogersd81871c2011-10-03 13:57:23 -07004539}
4540
Andreas Gampe896df402014-10-20 22:25:29 -07004541template <MethodVerifier::FieldAccessType kAccType>
4542void MethodVerifier::VerifyISFieldAccess(const Instruction* inst, const RegType& insn_type,
4543 bool is_primitive, bool is_static) {
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00004544 uint32_t field_idx = GetFieldIdxOfFieldAccess(inst, is_static);
Mathieu Chartierc7853442015-03-27 14:35:38 -07004545 ArtField* field;
Ian Rogersb94a27b2011-10-26 00:33:41 -07004546 if (is_static) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07004547 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07004548 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004549 const RegType& object_type = work_line_->GetRegisterType(this, inst->VRegB_22c());
David Brazdil68b5c0b2016-01-19 14:25:29 +00004550
4551 // One is not allowed to access fields on uninitialized references, except to write to
4552 // fields in the constructor (before calling another constructor).
4553 // GetInstanceField does an assignability check which will fail for uninitialized types.
4554 // We thus modify the type if the uninitialized reference is a "this" reference (this also
4555 // checks at the same time that we're verifying a constructor).
4556 bool should_adjust = (kAccType == FieldAccessType::kAccPut) &&
4557 object_type.IsUninitializedThisReference();
4558 const RegType& adjusted_type = should_adjust
4559 ? GetRegTypeCache()->FromUninitialized(object_type)
4560 : object_type;
4561 field = GetInstanceField(adjusted_type, field_idx);
Andreas Gampe896df402014-10-20 22:25:29 -07004562 if (UNLIKELY(have_pending_hard_failure_)) {
4563 return;
4564 }
Alex Light4a2c8fc2016-02-12 11:01:54 -08004565 if (should_adjust) {
4566 if (field == nullptr) {
4567 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "Might be accessing a superclass instance field prior "
4568 << "to the superclass being initialized in "
David Sehr709b0702016-10-13 09:12:37 -07004569 << dex_file_->PrettyMethod(dex_method_idx_);
Alex Light4a2c8fc2016-02-12 11:01:54 -08004570 } else if (field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
4571 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "cannot access superclass instance field "
David Sehr709b0702016-10-13 09:12:37 -07004572 << field->PrettyField() << " of a not fully initialized "
Alex Light4a2c8fc2016-02-12 11:01:54 -08004573 << "object within the context of "
David Sehr709b0702016-10-13 09:12:37 -07004574 << dex_file_->PrettyMethod(dex_method_idx_);
Alex Light4a2c8fc2016-02-12 11:01:54 -08004575 return;
4576 }
4577 }
Ian Rogersb94a27b2011-10-26 00:33:41 -07004578 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00004579 const RegType* field_type = nullptr;
Ian Rogers7b078e82014-09-10 14:44:24 -07004580 if (field != nullptr) {
Andreas Gampe896df402014-10-20 22:25:29 -07004581 if (kAccType == FieldAccessType::kAccPut) {
4582 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
David Sehr709b0702016-10-13 09:12:37 -07004583 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << field->PrettyField()
Andreas Gampe896df402014-10-20 22:25:29 -07004584 << " from other class " << GetDeclaringClass();
Aart Bikc2bc2652016-05-23 14:58:49 -07004585 // Keep hunting for possible hard fails.
Andreas Gampe896df402014-10-20 22:25:29 -07004586 }
4587 }
4588
Mathieu Chartier3398c782016-09-30 10:27:43 -07004589 ObjPtr<mirror::Class> field_type_class =
Vladimir Marko208f6702017-12-08 12:00:50 +00004590 can_load_classes_ ? field->ResolveType() : field->LookupResolvedType();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004591 if (field_type_class != nullptr) {
Mathieu Chartier3398c782016-09-30 10:27:43 -07004592 field_type = &FromClass(field->GetTypeDescriptor(),
Vladimir Markobcf17522018-06-01 13:14:32 +01004593 field_type_class,
Andreas Gampef23f33d2015-06-23 14:18:17 -07004594 field_type_class->CannotBeAssignedFromOtherTypes());
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08004595 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004596 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
4597 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004598 }
David Brazdil2bb2fbd2018-11-13 18:24:26 +00004599 } else if (IsSdkVersionSetAndAtLeast(api_level_, SdkVersion::kP)) {
Andreas Gampe7da4c402017-08-25 11:30:48 -07004600 // If we don't have the field (it seems we failed resolution) and this is a PUT, we need to
4601 // redo verification at runtime as the field may be final, unless the field id shows it's in
4602 // the same class.
4603 //
4604 // For simplicity, it is OK to not distinguish compile-time vs runtime, and post this an
4605 // ACCESS_FIELD failure at runtime. This has the same effect as NO_FIELD - punting the class
4606 // to the access-checks interpreter.
4607 //
4608 // Note: see b/34966607. This and above may be changed in the future.
4609 if (kAccType == FieldAccessType::kAccPut) {
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08004610 const dex::FieldId& field_id = dex_file_->GetFieldId(field_idx);
Andreas Gampe7da4c402017-08-25 11:30:48 -07004611 const char* field_class_descriptor = dex_file_->GetFieldDeclaringClassDescriptor(field_id);
4612 const RegType* field_class_type = &reg_types_.FromDescriptor(GetClassLoader(),
4613 field_class_descriptor,
4614 false);
4615 if (!field_class_type->Equals(GetDeclaringClass())) {
4616 Fail(VERIFY_ERROR_ACCESS_FIELD) << "could not check field put for final field modify of "
4617 << field_class_descriptor
4618 << "."
4619 << dex_file_->GetFieldName(field_id)
4620 << " from other class "
4621 << GetDeclaringClass();
4622 }
4623 }
Ian Rogers0d604842012-04-16 14:50:24 -07004624 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004625 if (field_type == nullptr) {
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08004626 const dex::FieldId& field_id = dex_file_->GetFieldId(field_idx);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004627 const char* descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
Mathieu Chartierbf99f772014-08-23 16:37:27 -07004628 field_type = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004629 }
Sebastien Hertz757b3042014-03-28 14:34:28 +01004630 DCHECK(field_type != nullptr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02004631 const uint32_t vregA = (is_static) ? inst->VRegA_21c() : inst->VRegA_22c();
Andreas Gampe896df402014-10-20 22:25:29 -07004632 static_assert(kAccType == FieldAccessType::kAccPut || kAccType == FieldAccessType::kAccGet,
4633 "Unexpected third access type");
4634 if (kAccType == FieldAccessType::kAccPut) {
4635 // sput or iput.
4636 if (is_primitive) {
4637 VerifyPrimitivePut(*field_type, insn_type, vregA);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004638 } else {
David Brazdilca3c8c32016-09-06 14:04:48 +01004639 if (!insn_type.IsAssignableFrom(*field_type, this)) {
Vladimir Marko414000e2015-06-23 17:45:21 +01004640 // If the field type is not a reference, this is a global failure rather than
4641 // a class change failure as the instructions and the descriptors for the type
4642 // should have been consistent within the same file at compile time.
4643 VerifyError error = field_type->IsReferenceTypes() ? VERIFY_ERROR_BAD_CLASS_SOFT
4644 : VERIFY_ERROR_BAD_CLASS_HARD;
David Sehr709b0702016-10-13 09:12:37 -07004645 Fail(error) << "expected field " << ArtField::PrettyField(field)
Vladimir Marko414000e2015-06-23 17:45:21 +01004646 << " to be compatible with type '" << insn_type
4647 << "' but found type '" << *field_type
4648 << "' in put-object";
Andreas Gampe896df402014-10-20 22:25:29 -07004649 return;
4650 }
4651 work_line_->VerifyRegisterType(this, vregA, *field_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004652 }
Andreas Gampe896df402014-10-20 22:25:29 -07004653 } else if (kAccType == FieldAccessType::kAccGet) {
4654 // sget or iget.
4655 if (is_primitive) {
4656 if (field_type->Equals(insn_type) ||
4657 (field_type->IsFloat() && insn_type.IsInteger()) ||
4658 (field_type->IsDouble() && insn_type.IsLong())) {
4659 // expected that read is of the correct primitive type or that int reads are reading
4660 // floats or long reads are reading doubles
4661 } else {
4662 // This is a global failure rather than a class change failure as the instructions and
4663 // the descriptors for the type should have been consistent within the same file at
4664 // compile time
David Sehr709b0702016-10-13 09:12:37 -07004665 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << ArtField::PrettyField(field)
Andreas Gampe896df402014-10-20 22:25:29 -07004666 << " to be of type '" << insn_type
4667 << "' but found type '" << *field_type << "' in get";
4668 return;
4669 }
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08004670 } else {
David Brazdilca3c8c32016-09-06 14:04:48 +01004671 if (!insn_type.IsAssignableFrom(*field_type, this)) {
Vladimir Marko414000e2015-06-23 17:45:21 +01004672 // If the field type is not a reference, this is a global failure rather than
4673 // a class change failure as the instructions and the descriptors for the type
4674 // should have been consistent within the same file at compile time.
4675 VerifyError error = field_type->IsReferenceTypes() ? VERIFY_ERROR_BAD_CLASS_SOFT
4676 : VERIFY_ERROR_BAD_CLASS_HARD;
David Sehr709b0702016-10-13 09:12:37 -07004677 Fail(error) << "expected field " << ArtField::PrettyField(field)
Vladimir Marko414000e2015-06-23 17:45:21 +01004678 << " to be compatible with type '" << insn_type
4679 << "' but found type '" << *field_type
4680 << "' in get-object";
Andreas Gampe890da292015-07-06 17:20:18 -07004681 if (error != VERIFY_ERROR_BAD_CLASS_HARD) {
Andreas Gampead238ce2015-08-24 21:13:08 -07004682 work_line_->SetRegisterType<LockOp::kClear>(this, vregA, reg_types_.Conflict());
Andreas Gampe890da292015-07-06 17:20:18 -07004683 }
Andreas Gampe896df402014-10-20 22:25:29 -07004684 return;
4685 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004686 }
Andreas Gampe896df402014-10-20 22:25:29 -07004687 if (!field_type->IsLowHalf()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07004688 work_line_->SetRegisterType<LockOp::kClear>(this, vregA, *field_type);
Andreas Gampe896df402014-10-20 22:25:29 -07004689 } else {
4690 work_line_->SetRegisterTypeWide(this, vregA, *field_type, field_type->HighHalf(&reg_types_));
4691 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07004692 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004693 LOG(FATAL) << "Unexpected case.";
Ian Rogersd81871c2011-10-03 13:57:23 -07004694 }
4695}
4696
Ian Rogers776ac1f2012-04-13 23:36:36 -07004697bool MethodVerifier::CheckNotMoveException(const uint16_t* insns, int insn_idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004698 if ((insns[insn_idx] & 0xff) == Instruction::MOVE_EXCEPTION) {
jeffhaod5347e02012-03-22 17:25:05 -07004699 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-exception";
Ian Rogersd81871c2011-10-03 13:57:23 -07004700 return false;
4701 }
4702 return true;
4703}
4704
Stephen Kyle9bc61992014-09-22 13:53:15 +01004705bool MethodVerifier::CheckNotMoveResult(const uint16_t* insns, int insn_idx) {
4706 if (((insns[insn_idx] & 0xff) >= Instruction::MOVE_RESULT) &&
4707 ((insns[insn_idx] & 0xff) <= Instruction::MOVE_RESULT_OBJECT)) {
4708 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-result*";
4709 return false;
4710 }
4711 return true;
4712}
4713
4714bool MethodVerifier::CheckNotMoveExceptionOrMoveResult(const uint16_t* insns, int insn_idx) {
4715 return (CheckNotMoveException(insns, insn_idx) && CheckNotMoveResult(insns, insn_idx));
4716}
4717
Ian Rogersebbdd872014-07-07 23:53:08 -07004718bool MethodVerifier::UpdateRegisters(uint32_t next_insn, RegisterLine* merge_line,
4719 bool update_merge_line) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004720 bool changed = true;
4721 RegisterLine* target_line = reg_table_.GetLine(next_insn);
Mathieu Chartierde40d472015-10-15 17:47:48 -07004722 if (!GetInstructionFlags(next_insn).IsVisitedOrChanged()) {
jeffhaobdb76512011-09-07 11:43:16 -07004723 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07004724 * We haven't processed this instruction before, and we haven't touched the registers here, so
4725 * there's nothing to "merge". Copy the registers over and mark it as changed. (This is the
4726 * only way a register can transition out of "unknown", so this is not just an optimization.)
jeffhaobdb76512011-09-07 11:43:16 -07004727 */
Andreas Gampea727e372015-08-25 09:22:37 -07004728 target_line->CopyFromLine(merge_line);
Mathieu Chartierde40d472015-10-15 17:47:48 -07004729 if (GetInstructionFlags(next_insn).IsReturn()) {
Jeff Haob24b4a72013-07-31 13:47:31 -07004730 // Verify that the monitor stack is empty on return.
Andreas Gampea727e372015-08-25 09:22:37 -07004731 merge_line->VerifyMonitorStackEmpty(this);
4732
Ian Rogersb8c78592013-07-25 23:52:52 +00004733 // For returns we only care about the operand to the return, all other registers are dead.
4734 // Initialize them as conflicts so they don't add to GC and deoptimization information.
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08004735 const Instruction* ret_inst = &code_item_accessor_.InstructionAt(next_insn);
Andreas Gampea727e372015-08-25 09:22:37 -07004736 AdjustReturnLine(this, ret_inst, target_line);
Aart Bik31883642016-06-06 15:02:44 -07004737 // Directly bail if a hard failure was found.
Aart Bikb0526322016-06-01 14:06:00 -07004738 if (have_pending_hard_failure_) {
4739 return false;
4740 }
Ian Rogersb8c78592013-07-25 23:52:52 +00004741 }
jeffhaobdb76512011-09-07 11:43:16 -07004742 } else {
Mathieu Chartier361e04a2016-02-16 14:06:35 -08004743 RegisterLineArenaUniquePtr copy;
Andreas Gampe92d77202017-12-06 20:49:00 -08004744 if (UNLIKELY(VLOG_IS_ON(verifier_debug))) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07004745 copy.reset(RegisterLine::Create(target_line->NumRegs(), this));
Ian Rogers7b0c5b42012-02-16 15:29:07 -08004746 copy->CopyFromLine(target_line);
4747 }
Ian Rogers7b078e82014-09-10 14:44:24 -07004748 changed = target_line->MergeRegisters(this, merge_line);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004749 if (have_pending_hard_failure_) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004750 return false;
jeffhaobdb76512011-09-07 11:43:16 -07004751 }
Andreas Gampe92d77202017-12-06 20:49:00 -08004752 if (UNLIKELY(VLOG_IS_ON(verifier_debug)) && changed) {
Elliott Hughes398f64b2012-03-26 18:05:48 -07004753 LogVerifyInfo() << "Merging at [" << reinterpret_cast<void*>(work_insn_idx_) << "]"
Elliott Hughesc073b072012-05-24 19:29:17 -07004754 << " to [" << reinterpret_cast<void*>(next_insn) << "]: " << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07004755 << copy->Dump(this) << " MERGE\n"
4756 << merge_line->Dump(this) << " ==\n"
Andreas Gampe2ad6cce2019-04-11 16:17:39 -07004757 << target_line->Dump(this);
jeffhaobdb76512011-09-07 11:43:16 -07004758 }
Ian Rogersebbdd872014-07-07 23:53:08 -07004759 if (update_merge_line && changed) {
4760 merge_line->CopyFromLine(target_line);
4761 }
jeffhaobdb76512011-09-07 11:43:16 -07004762 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004763 if (changed) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07004764 GetInstructionFlags(next_insn).SetChanged();
jeffhaobdb76512011-09-07 11:43:16 -07004765 }
4766 return true;
4767}
4768
Ian Rogers7b3ddd22013-02-21 15:19:52 -08004769InstructionFlags* MethodVerifier::CurrentInsnFlags() {
Mathieu Chartierde40d472015-10-15 17:47:48 -07004770 return &GetInstructionFlags(work_insn_idx_);
Ian Rogers776ac1f2012-04-13 23:36:36 -07004771}
4772
Ian Rogersd8f69b02014-09-10 21:43:52 +00004773const RegType& MethodVerifier::GetMethodReturnType() {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004774 if (return_type_ == nullptr) {
Nicolas Geoffrayb041a402017-11-13 15:16:22 +00004775 if (method_being_verified_ != nullptr) {
Vladimir Markob45528c2017-07-27 14:14:28 +01004776 ObjPtr<mirror::Class> return_type_class = can_load_classes_
Nicolas Geoffrayb041a402017-11-13 15:16:22 +00004777 ? method_being_verified_->ResolveReturnType()
4778 : method_being_verified_->LookupResolvedReturnType();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004779 if (return_type_class != nullptr) {
Nicolas Geoffrayb041a402017-11-13 15:16:22 +00004780 return_type_ = &FromClass(method_being_verified_->GetReturnTypeDescriptor(),
Vladimir Markobcf17522018-06-01 13:14:32 +01004781 return_type_class,
Andreas Gampef23f33d2015-06-23 14:18:17 -07004782 return_type_class->CannotBeAssignedFromOtherTypes());
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004783 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004784 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
4785 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004786 }
4787 }
4788 if (return_type_ == nullptr) {
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08004789 const dex::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
4790 const dex::ProtoId& proto_id = dex_file_->GetMethodPrototype(method_id);
Andreas Gampea5b09a62016-11-17 15:21:22 -08004791 dex::TypeIndex return_type_idx = proto_id.return_type_idx_;
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004792 const char* descriptor = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(return_type_idx));
Mathieu Chartierbf99f772014-08-23 16:37:27 -07004793 return_type_ = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004794 }
4795 }
4796 return *return_type_;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004797}
4798
Ian Rogersd8f69b02014-09-10 21:43:52 +00004799const RegType& MethodVerifier::GetDeclaringClass() {
Ian Rogers7b078e82014-09-10 14:44:24 -07004800 if (declaring_class_ == nullptr) {
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08004801 const dex::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
Brian Carlstrom93c33962013-07-26 10:37:43 -07004802 const char* descriptor
4803 = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(method_id.class_idx_));
Nicolas Geoffrayb041a402017-11-13 15:16:22 +00004804 if (method_being_verified_ != nullptr) {
Vladimir Marko2cffc5d2018-05-29 15:40:56 +01004805 ObjPtr<mirror::Class> klass = method_being_verified_->GetDeclaringClass();
Mathieu Chartierde40d472015-10-15 17:47:48 -07004806 declaring_class_ = &FromClass(descriptor, klass, klass->CannotBeAssignedFromOtherTypes());
Ian Rogers637c65b2013-05-31 11:46:00 -07004807 } else {
Mathieu Chartierbf99f772014-08-23 16:37:27 -07004808 declaring_class_ = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers637c65b2013-05-31 11:46:00 -07004809 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07004810 }
Ian Rogers637c65b2013-05-31 11:46:00 -07004811 return *declaring_class_;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004812}
4813
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004814std::vector<int32_t> MethodVerifier::DescribeVRegs(uint32_t dex_pc) {
4815 RegisterLine* line = reg_table_.GetLine(dex_pc);
Sebastien Hertzaa0c00c2014-03-14 17:58:54 +01004816 DCHECK(line != nullptr) << "No register line at DEX pc " << StringPrintf("0x%x", dex_pc);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004817 std::vector<int32_t> result;
4818 for (size_t i = 0; i < line->NumRegs(); ++i) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004819 const RegType& type = line->GetRegisterType(this, i);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004820 if (type.IsConstant()) {
4821 result.push_back(type.IsPreciseConstant() ? kConstant : kImpreciseConstant);
Ian Rogers7b078e82014-09-10 14:44:24 -07004822 const ConstantType* const_val = down_cast<const ConstantType*>(&type);
4823 result.push_back(const_val->ConstantValue());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004824 } else if (type.IsConstantLo()) {
4825 result.push_back(type.IsPreciseConstantLo() ? kConstant : kImpreciseConstant);
Ian Rogers7b078e82014-09-10 14:44:24 -07004826 const ConstantType* const_val = down_cast<const ConstantType*>(&type);
4827 result.push_back(const_val->ConstantValueLo());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004828 } else if (type.IsConstantHi()) {
4829 result.push_back(type.IsPreciseConstantHi() ? kConstant : kImpreciseConstant);
Ian Rogers7b078e82014-09-10 14:44:24 -07004830 const ConstantType* const_val = down_cast<const ConstantType*>(&type);
4831 result.push_back(const_val->ConstantValueHi());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004832 } else if (type.IsIntegralTypes()) {
4833 result.push_back(kIntVReg);
4834 result.push_back(0);
4835 } else if (type.IsFloat()) {
4836 result.push_back(kFloatVReg);
4837 result.push_back(0);
4838 } else if (type.IsLong()) {
4839 result.push_back(kLongLoVReg);
4840 result.push_back(0);
4841 result.push_back(kLongHiVReg);
4842 result.push_back(0);
4843 ++i;
4844 } else if (type.IsDouble()) {
4845 result.push_back(kDoubleLoVReg);
4846 result.push_back(0);
4847 result.push_back(kDoubleHiVReg);
4848 result.push_back(0);
4849 ++i;
4850 } else if (type.IsUndefined() || type.IsConflict() || type.IsHighHalf()) {
4851 result.push_back(kUndefined);
4852 result.push_back(0);
4853 } else {
Ian Rogers7b3ddd22013-02-21 15:19:52 -08004854 CHECK(type.IsNonZeroReferenceTypes());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004855 result.push_back(kReferenceVReg);
4856 result.push_back(0);
4857 }
4858 }
4859 return result;
4860}
4861
Ian Rogersd8f69b02014-09-10 21:43:52 +00004862const RegType& MethodVerifier::DetermineCat1Constant(int32_t value, bool precise) {
Sebastien Hertz849600b2013-12-20 10:28:08 +01004863 if (precise) {
4864 // Precise constant type.
4865 return reg_types_.FromCat1Const(value, true);
4866 } else {
4867 // Imprecise constant type.
4868 if (value < -32768) {
4869 return reg_types_.IntConstant();
4870 } else if (value < -128) {
4871 return reg_types_.ShortConstant();
4872 } else if (value < 0) {
4873 return reg_types_.ByteConstant();
4874 } else if (value == 0) {
4875 return reg_types_.Zero();
4876 } else if (value == 1) {
4877 return reg_types_.One();
4878 } else if (value < 128) {
4879 return reg_types_.PosByteConstant();
4880 } else if (value < 32768) {
4881 return reg_types_.PosShortConstant();
4882 } else if (value < 65536) {
4883 return reg_types_.CharConstant();
4884 } else {
4885 return reg_types_.IntConstant();
4886 }
4887 }
4888}
4889
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004890void MethodVerifier::Init() {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08004891 art::verifier::RegTypeCache::Init();
Brian Carlstrome7d856b2012-01-11 18:10:55 -08004892}
4893
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004894void MethodVerifier::Shutdown() {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08004895 verifier::RegTypeCache::ShutDown();
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08004896}
jeffhaod1224c72012-02-29 13:43:08 -08004897
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -07004898void MethodVerifier::VisitStaticRoots(RootVisitor* visitor) {
4899 RegTypeCache::VisitStaticRoots(visitor);
Mathieu Chartier7c438b12014-09-12 17:01:24 -07004900}
4901
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -07004902void MethodVerifier::VisitRoots(RootVisitor* visitor, const RootInfo& root_info) {
4903 reg_types_.VisitRoots(visitor, root_info);
Mathieu Chartierc528dba2013-11-26 12:00:11 -08004904}
4905
Andreas Gampef23f33d2015-06-23 14:18:17 -07004906const RegType& MethodVerifier::FromClass(const char* descriptor,
Vladimir Marko2cffc5d2018-05-29 15:40:56 +01004907 ObjPtr<mirror::Class> klass,
Andreas Gampef23f33d2015-06-23 14:18:17 -07004908 bool precise) {
4909 DCHECK(klass != nullptr);
4910 if (precise && !klass->IsInstantiable() && !klass->IsPrimitive()) {
4911 Fail(VerifyError::VERIFY_ERROR_NO_CLASS) << "Could not create precise reference for "
4912 << "non-instantiable klass " << descriptor;
4913 precise = false;
4914 }
4915 return reg_types_.FromClass(descriptor, klass, precise);
4916}
4917
Ian Rogersd81871c2011-10-03 13:57:23 -07004918} // namespace verifier
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07004919} // namespace art