blob: 34b0f488a0187c2950383f69cf319b51e6de37b4 [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070016
Mathieu Chartierc645f1d2014-03-06 18:11:53 -080017#include "method_verifier-inl.h"
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070018
Elliott Hughes1f359b02011-07-17 14:27:17 -070019#include <iostream>
20
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"
Mathieu Chartier0795f232016-09-27 18:43:30 -070060#include "scoped_thread_state_change-inl.h"
Andreas Gampe513061a2017-06-01 09:17:34 -070061#include "stack.h"
Nicolas Geoffrayb041a402017-11-13 15:16:22 +000062#include "vdex_file.h"
Andreas Gampe6d7abbd2017-04-24 13:19:09 -070063#include "verifier_compiler_binding.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070064#include "verifier_deps.h"
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070065
66namespace art {
Ian Rogersd81871c2011-10-03 13:57:23 -070067namespace verifier {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070068
Andreas Gampe46ee31b2016-12-14 10:11:49 -080069using android::base::StringPrintf;
70
Mathieu Chartier8e219ae2014-08-19 14:29:46 -070071static constexpr bool kTimeVerifyMethod = !kIsDebugBuild;
Ian Rogers2c8a8572011-10-24 17:11:36 -070072
Andreas Gampeebf850c2015-08-14 15:37:35 -070073// On VLOG(verifier), should we dump the whole state when we run into a hard failure?
74static constexpr bool kDumpRegLinesOnHardFailureIfVLOG = true;
75
Vladimir Marko69d310e2017-10-09 14:12:23 +010076PcToRegisterLineTable::PcToRegisterLineTable(ScopedArenaAllocator& allocator)
77 : register_lines_(allocator.Adapter(kArenaAllocVerifier)) {}
Mathieu Chartierde40d472015-10-15 17:47:48 -070078
Ian Rogers7b3ddd22013-02-21 15:19:52 -080079void PcToRegisterLineTable::Init(RegisterTrackingMode mode, InstructionFlags* flags,
Ian Rogersd81871c2011-10-03 13:57:23 -070080 uint32_t insns_size, uint16_t registers_size,
Ian Rogers776ac1f2012-04-13 23:36:36 -070081 MethodVerifier* verifier) {
Ian Rogersd81871c2011-10-03 13:57:23 -070082 DCHECK_GT(insns_size, 0U);
Mathieu Chartierde40d472015-10-15 17:47:48 -070083 register_lines_.resize(insns_size);
Ian Rogersd81871c2011-10-03 13:57:23 -070084 for (uint32_t i = 0; i < insns_size; i++) {
85 bool interesting = false;
86 switch (mode) {
87 case kTrackRegsAll:
88 interesting = flags[i].IsOpcode();
89 break;
Sameer Abu Asal02c42232013-04-30 12:09:45 -070090 case kTrackCompilerInterestPoints:
Brian Carlstrom02c8cc62013-07-18 15:54:44 -070091 interesting = flags[i].IsCompileTimeInfoPoint() || flags[i].IsBranchTarget();
Ian Rogersd81871c2011-10-03 13:57:23 -070092 break;
93 case kTrackRegsBranches:
94 interesting = flags[i].IsBranchTarget();
95 break;
96 default:
97 break;
98 }
99 if (interesting) {
Mathieu Chartierde40d472015-10-15 17:47:48 -0700100 register_lines_[i].reset(RegisterLine::Create(registers_size, verifier));
Ian Rogersd0fbd852013-09-24 18:17:04 -0700101 }
102 }
103}
104
Mathieu Chartierde40d472015-10-15 17:47:48 -0700105PcToRegisterLineTable::~PcToRegisterLineTable() {}
Ian Rogersd81871c2011-10-03 13:57:23 -0700106
Andreas Gampe7c038102014-10-27 20:08:46 -0700107// Note: returns true on failure.
Mathieu Chartierc2a2bda2018-03-12 11:21:52 -0700108inline bool MethodVerifier::FailOrAbort(bool condition,
109 const char* error_msg,
110 uint32_t work_insn_idx) {
Andreas Gampe7c038102014-10-27 20:08:46 -0700111 if (kIsDebugBuild) {
Andreas Gampef8f36c22016-11-19 14:51:52 -0800112 // In a debug build, abort if the error condition is wrong. Only warn if
113 // we are already aborting (as this verification is likely run to print
114 // lock information).
115 if (LIKELY(gAborting == 0)) {
Mathieu Chartierc2a2bda2018-03-12 11:21:52 -0700116 DCHECK(condition) << error_msg << work_insn_idx << " "
117 << dex_file_->PrettyMethod(dex_method_idx_);
Andreas Gampef8f36c22016-11-19 14:51:52 -0800118 } else {
119 if (!condition) {
120 LOG(ERROR) << error_msg << work_insn_idx;
Mathieu Chartierc2a2bda2018-03-12 11:21:52 -0700121 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << error_msg << work_insn_idx;
Andreas Gampef8f36c22016-11-19 14:51:52 -0800122 return true;
123 }
124 }
Andreas Gampe7c038102014-10-27 20:08:46 -0700125 } else {
126 // In a non-debug build, just fail the class.
127 if (!condition) {
Mathieu Chartierc2a2bda2018-03-12 11:21:52 -0700128 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << error_msg << work_insn_idx;
Andreas Gampe7c038102014-10-27 20:08:46 -0700129 return true;
130 }
131 }
132
133 return false;
134}
135
Stephen Kyle7e541c92014-12-17 17:10:02 +0000136static void SafelyMarkAllRegistersAsConflicts(MethodVerifier* verifier, RegisterLine* reg_line) {
Andreas Gampef10b6e12015-08-12 10:48:12 -0700137 if (verifier->IsInstanceConstructor()) {
Stephen Kyle7e541c92014-12-17 17:10:02 +0000138 // Before we mark all regs as conflicts, check that we don't have an uninitialized this.
139 reg_line->CheckConstructorReturn(verifier);
140 }
141 reg_line->MarkAllRegistersAsConflicts(verifier);
142}
143
Andreas Gampe6d7abbd2017-04-24 13:19:09 -0700144static FailureKind FailureKindMax(FailureKind fk1, FailureKind fk2) {
145 static_assert(FailureKind::kNoFailure < FailureKind::kSoftFailure
146 && FailureKind::kSoftFailure < FailureKind::kHardFailure,
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800147 "Unexpected FailureKind order");
148 return std::max(fk1, fk2);
149}
150
151void MethodVerifier::FailureData::Merge(const MethodVerifier::FailureData& fd) {
152 kind = FailureKindMax(kind, fd.kind);
153 types |= fd.types;
154}
155
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800156static bool IsLargeMethod(const CodeItemDataAccessor& accessor) {
157 if (!accessor.HasCodeItem()) {
Andreas Gampe3c651fc2015-05-21 14:06:46 -0700158 return false;
159 }
160
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800161 uint16_t registers_size = accessor.RegistersSize();
162 uint32_t insns_size = accessor.InsnsSizeInCodeUnits();
Andreas Gampea4f5bf62015-05-18 20:50:12 -0700163
164 return registers_size * insns_size > 4*1024*1024;
165}
166
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800167MethodVerifier::FailureData MethodVerifier::VerifyMethod(Thread* self,
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800168 uint32_t method_idx,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800169 const DexFile* dex_file,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700170 Handle<mirror::DexCache> dex_cache,
171 Handle<mirror::ClassLoader> class_loader,
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800172 const dex::ClassDef& class_def,
173 const dex::CodeItem* code_item,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700174 ArtMethod* method,
Jeff Haoee988952013-04-16 14:23:47 -0700175 uint32_t method_access_flags,
Andreas Gampe53e32d12015-12-09 21:03:23 -0800176 CompilerCallbacks* callbacks,
Ian Rogers46960fe2014-05-23 10:43:43 -0700177 bool allow_soft_failures,
Andreas Gampe5fd66d02016-09-12 20:22:19 -0700178 HardFailLogMode log_level,
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800179 bool need_precise_constants,
Andreas Gampe6cc23ac2018-08-24 15:22:43 -0700180 uint32_t api_level,
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800181 std::string* hard_failure_msg) {
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800182 MethodVerifier::FailureData result;
Mathieu Chartier8e219ae2014-08-19 14:29:46 -0700183 uint64_t start_ns = kTimeVerifyMethod ? NanoTime() : 0;
Ian Rogersc8982582012-09-07 16:53:25 -0700184
Andreas Gampebf9611f2016-03-25 16:58:00 -0700185 MethodVerifier verifier(self,
186 dex_file,
187 dex_cache,
188 class_loader,
189 class_def,
190 code_item,
191 method_idx,
192 method,
193 method_access_flags,
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700194 /* can_load_classes= */ true,
Andreas Gampebf9611f2016-03-25 16:58:00 -0700195 allow_soft_failures,
196 need_precise_constants,
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700197 /* verify to dump */ false,
198 /* allow_thread_suspension= */ true,
Andreas Gampe6cc23ac2018-08-24 15:22:43 -0700199 api_level);
Ian Rogers46960fe2014-05-23 10:43:43 -0700200 if (verifier.Verify()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700201 // Verification completed, however failures may be pending that didn't cause the verification
202 // to hard fail.
Ian Rogers46960fe2014-05-23 10:43:43 -0700203 CHECK(!verifier.have_pending_hard_failure_);
Andreas Gampe53e32d12015-12-09 21:03:23 -0800204
205 if (code_item != nullptr && callbacks != nullptr) {
206 // Let the interested party know that the method was verified.
207 callbacks->MethodVerified(&verifier);
208 }
209
Ian Rogers46960fe2014-05-23 10:43:43 -0700210 if (verifier.failures_.size() != 0) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -0700211 if (VLOG_IS_ON(verifier)) {
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800212 verifier.DumpFailures(VLOG_STREAM(verifier) << "Soft verification failures in "
David Sehr709b0702016-10-13 09:12:37 -0700213 << dex_file->PrettyMethod(method_idx) << "\n");
Anwar Ghuloum75a43f12013-08-13 17:22:14 -0700214 }
Andreas Gampe92d77202017-12-06 20:49:00 -0800215 if (VLOG_IS_ON(verifier_debug)) {
216 std::cout << "\n" << verifier.info_messages_.str();
217 verifier.Dump(std::cout);
218 }
Andreas Gampe6d7abbd2017-04-24 13:19:09 -0700219 result.kind = FailureKind::kSoftFailure;
Nicolas Geoffray250a3782016-04-20 16:27:53 +0100220 if (method != nullptr &&
221 !CanCompilerHandleVerificationFailure(verifier.encountered_failure_types_)) {
Alex Lightfcbafb32017-02-02 15:09:54 -0800222 method->SetDontCompile();
Nicolas Geoffray250a3782016-04-20 16:27:53 +0100223 }
224 }
Andreas Gampe56fdd0e2016-04-28 14:56:54 -0700225 if (method != nullptr) {
226 if (verifier.HasInstructionThatWillThrow()) {
Alex Lightfcbafb32017-02-02 15:09:54 -0800227 method->SetDontCompile();
Calin Juravle857f0582016-12-20 14:36:59 +0000228 if (Runtime::Current()->IsAotCompiler() &&
229 (callbacks != nullptr) && !callbacks->IsBootImage()) {
230 // When compiling apps, make HasInstructionThatWillThrow a soft error to trigger
231 // re-verification at runtime.
232 // The dead code after the throw is not verified and might be invalid. This may cause
233 // the JIT compiler to crash since it assumes that all the code is valid.
234 //
235 // There's a strong assumption that the entire boot image is verified and all its dex
236 // code is valid (even the dead and unverified one). As such this is done only for apps.
237 // (CompilerDriver DCHECKs in VerifyClassVisitor that methods from boot image are
238 // fully verified).
Andreas Gampe6d7abbd2017-04-24 13:19:09 -0700239 result.kind = FailureKind::kSoftFailure;
Calin Juravle857f0582016-12-20 14:36:59 +0000240 }
Andreas Gampe56fdd0e2016-04-28 14:56:54 -0700241 }
242 if ((verifier.encountered_failure_types_ & VerifyError::VERIFY_ERROR_LOCKING) != 0) {
Orion Hodsoncfcc9cf2017-09-29 15:07:27 +0100243 method->SetMustCountLocks();
Andreas Gampe56fdd0e2016-04-28 14:56:54 -0700244 }
jeffhaof56197c2012-03-05 18:01:54 -0800245 }
246 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700247 // Bad method data.
Ian Rogers46960fe2014-05-23 10:43:43 -0700248 CHECK_NE(verifier.failures_.size(), 0U);
Igor Murashkin4d7b75f2015-07-21 17:03:36 -0700249
250 if (UNLIKELY(verifier.have_pending_experimental_failure_)) {
251 // Failed due to being forced into interpreter. This is ok because
252 // we just want to skip verification.
Andreas Gampe6d7abbd2017-04-24 13:19:09 -0700253 result.kind = FailureKind::kSoftFailure;
Igor Murashkin4d7b75f2015-07-21 17:03:36 -0700254 } else {
255 CHECK(verifier.have_pending_hard_failure_);
Andreas Gampe7fe30232016-03-25 16:58:00 -0700256 if (VLOG_IS_ON(verifier)) {
Andreas Gampe5fd66d02016-09-12 20:22:19 -0700257 log_level = std::max(HardFailLogMode::kLogVerbose, log_level);
Andreas Gampe7fe30232016-03-25 16:58:00 -0700258 }
Andreas Gampe5fd66d02016-09-12 20:22:19 -0700259 if (log_level >= HardFailLogMode::kLogVerbose) {
260 LogSeverity severity;
261 switch (log_level) {
262 case HardFailLogMode::kLogVerbose:
263 severity = LogSeverity::VERBOSE;
264 break;
265 case HardFailLogMode::kLogWarning:
266 severity = LogSeverity::WARNING;
267 break;
268 case HardFailLogMode::kLogInternalFatal:
Andreas Gampe3fec9ac2016-09-13 10:47:28 -0700269 severity = LogSeverity::FATAL_WITHOUT_ABORT;
Andreas Gampe5fd66d02016-09-12 20:22:19 -0700270 break;
271 default:
272 LOG(FATAL) << "Unsupported log-level " << static_cast<uint32_t>(log_level);
273 UNREACHABLE();
274 }
Andreas Gampe3fec9ac2016-09-13 10:47:28 -0700275 verifier.DumpFailures(LOG_STREAM(severity) << "Verification error in "
David Sehr709b0702016-10-13 09:12:37 -0700276 << dex_file->PrettyMethod(method_idx)
Andreas Gampe3fec9ac2016-09-13 10:47:28 -0700277 << "\n");
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800278 }
279 if (hard_failure_msg != nullptr) {
280 CHECK(!verifier.failure_messages_.empty());
281 *hard_failure_msg =
282 verifier.failure_messages_[verifier.failure_messages_.size() - 1]->str();
283 }
Andreas Gampe6d7abbd2017-04-24 13:19:09 -0700284 result.kind = FailureKind::kHardFailure;
Andreas Gampe53e32d12015-12-09 21:03:23 -0800285
286 if (callbacks != nullptr) {
287 // Let the interested party know that we failed the class.
David Brazdil15fc7292016-09-02 14:13:18 +0100288 ClassReference ref(dex_file, dex_file->GetIndexForClassDef(class_def));
Andreas Gampe53e32d12015-12-09 21:03:23 -0800289 callbacks->ClassRejected(ref);
290 }
Igor Murashkin4d7b75f2015-07-21 17:03:36 -0700291 }
Andreas Gampe92d77202017-12-06 20:49:00 -0800292 if (VLOG_IS_ON(verifier) || VLOG_IS_ON(verifier_debug)) {
Ian Rogers46960fe2014-05-23 10:43:43 -0700293 std::cout << "\n" << verifier.info_messages_.str();
294 verifier.Dump(std::cout);
jeffhaof56197c2012-03-05 18:01:54 -0800295 }
jeffhaof56197c2012-03-05 18:01:54 -0800296 }
Mathieu Chartier8e219ae2014-08-19 14:29:46 -0700297 if (kTimeVerifyMethod) {
298 uint64_t duration_ns = NanoTime() - start_ns;
Andreas Gampe0b0ffc12018-08-01 14:41:27 -0700299 if (duration_ns > MsToNs(Runtime::Current()->GetVerifierLoggingThresholdMs())) {
David Sehr709b0702016-10-13 09:12:37 -0700300 LOG(WARNING) << "Verification of " << dex_file->PrettyMethod(method_idx)
Andreas Gampea4f5bf62015-05-18 20:50:12 -0700301 << " took " << PrettyDuration(duration_ns)
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800302 << (IsLargeMethod(verifier.CodeItem()) ? " (large method)" : "");
Mathieu Chartier8e219ae2014-08-19 14:29:46 -0700303 }
Ian Rogersc8982582012-09-07 16:53:25 -0700304 }
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800305 result.types = verifier.encountered_failure_types_;
Ian Rogersc8982582012-09-07 16:53:25 -0700306 return result;
jeffhaof56197c2012-03-05 18:01:54 -0800307}
308
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100309MethodVerifier* MethodVerifier::VerifyMethodAndDump(Thread* self,
310 VariableIndentationOutputStream* vios,
311 uint32_t dex_method_idx,
312 const DexFile* dex_file,
313 Handle<mirror::DexCache> dex_cache,
314 Handle<mirror::ClassLoader> class_loader,
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800315 const dex::ClassDef& class_def,
316 const dex::CodeItem* code_item,
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100317 ArtMethod* method,
Andreas Gampe6cc23ac2018-08-24 15:22:43 -0700318 uint32_t method_access_flags,
319 uint32_t api_level) {
Andreas Gampebf9611f2016-03-25 16:58:00 -0700320 MethodVerifier* verifier = new MethodVerifier(self,
321 dex_file,
322 dex_cache,
323 class_loader,
324 class_def,
325 code_item,
326 dex_method_idx,
327 method,
328 method_access_flags,
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700329 /* can_load_classes= */ true,
330 /* allow_soft_failures= */ true,
331 /* need_precise_constants= */ true,
332 /* verify_to_dump= */ true,
333 /* allow_thread_suspension= */ true,
Andreas Gampe6cc23ac2018-08-24 15:22:43 -0700334 api_level);
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700335 verifier->Verify();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100336 verifier->DumpFailures(vios->Stream());
337 vios->Stream() << verifier->info_messages_.str();
Andreas Gampe5cbcde22014-09-16 14:59:49 -0700338 // Only dump and return if no hard failures. Otherwise the verifier may be not fully initialized
339 // and querying any info is dangerous/can abort.
340 if (verifier->have_pending_hard_failure_) {
341 delete verifier;
342 return nullptr;
343 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100344 verifier->Dump(vios);
Andreas Gampe5cbcde22014-09-16 14:59:49 -0700345 return verifier;
346 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800347}
348
Ian Rogers7b078e82014-09-10 14:44:24 -0700349MethodVerifier::MethodVerifier(Thread* self,
Andreas Gampe53e32d12015-12-09 21:03:23 -0800350 const DexFile* dex_file,
351 Handle<mirror::DexCache> dex_cache,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700352 Handle<mirror::ClassLoader> class_loader,
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800353 const dex::ClassDef& class_def,
354 const dex::CodeItem* code_item,
Andreas Gampe53e32d12015-12-09 21:03:23 -0800355 uint32_t dex_method_idx,
356 ArtMethod* method,
357 uint32_t method_access_flags,
358 bool can_load_classes,
359 bool allow_soft_failures,
360 bool need_precise_constants,
361 bool verify_to_dump,
Andreas Gampe6cc23ac2018-08-24 15:22:43 -0700362 bool allow_thread_suspension,
363 uint32_t api_level)
Ian Rogers7b078e82014-09-10 14:44:24 -0700364 : self_(self),
Mathieu Chartierde40d472015-10-15 17:47:48 -0700365 arena_stack_(Runtime::Current()->GetArenaPool()),
Vladimir Marko69d310e2017-10-09 14:12:23 +0100366 allocator_(&arena_stack_),
Alex Lightd9aff132017-10-31 22:30:05 +0000367 reg_types_(can_load_classes, allocator_, allow_thread_suspension),
Vladimir Marko69d310e2017-10-09 14:12:23 +0100368 reg_table_(allocator_),
Andreas Gampee2abbc62017-09-15 11:59:26 -0700369 work_insn_idx_(dex::kDexNoIndex),
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800370 dex_method_idx_(dex_method_idx),
Nicolas Geoffrayb041a402017-11-13 15:16:22 +0000371 method_being_verified_(method),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700372 method_access_flags_(method_access_flags),
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700373 return_type_(nullptr),
jeffhaof56197c2012-03-05 18:01:54 -0800374 dex_file_(dex_file),
375 dex_cache_(dex_cache),
376 class_loader_(class_loader),
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700377 class_def_(class_def),
Mathieu Chartier698ebbc2018-01-05 11:00:42 -0800378 code_item_accessor_(*dex_file, code_item),
Ian Rogers7b078e82014-09-10 14:44:24 -0700379 declaring_class_(nullptr),
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700380 interesting_dex_pc_(-1),
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700381 monitor_enter_dex_pcs_(nullptr),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700382 have_pending_hard_failure_(false),
jeffhaofaf459e2012-08-31 15:32:47 -0700383 have_pending_runtime_throw_failure_(false),
Igor Murashkin4d7b75f2015-07-21 17:03:36 -0700384 have_pending_experimental_failure_(false),
Andreas Gamped12e7822015-06-25 10:26:40 -0700385 have_any_pending_runtime_throw_failure_(false),
jeffhaof56197c2012-03-05 18:01:54 -0800386 new_instance_count_(0),
Elliott Hughes80537bb2013-01-04 16:37:26 -0800387 monitor_enter_count_(0),
Andreas Gampe0760a812015-08-26 17:12:51 -0700388 encountered_failure_types_(0),
Jeff Haoee988952013-04-16 14:23:47 -0700389 can_load_classes_(can_load_classes),
Sebastien Hertz4d4adb12013-07-24 16:14:19 +0200390 allow_soft_failures_(allow_soft_failures),
Ian Rogers46960fe2014-05-23 10:43:43 -0700391 need_precise_constants_(need_precise_constants),
Sebastien Hertz4d4adb12013-07-24 16:14:19 +0200392 has_check_casts_(false),
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700393 has_virtual_or_interface_invokes_(false),
Mathieu Chartier4306ef82014-12-19 18:41:47 -0800394 verify_to_dump_(verify_to_dump),
Andreas Gamped5ad72f2015-06-26 17:33:47 -0700395 allow_thread_suspension_(allow_thread_suspension),
Andreas Gampee6215c02015-08-31 18:54:38 -0700396 is_constructor_(false),
Andreas Gampe6cc23ac2018-08-24 15:22:43 -0700397 link_(nullptr),
398 api_level_(api_level == 0 ? std::numeric_limits<uint32_t>::max() : api_level) {
Mathieu Chartierd0ad2ee2015-03-31 14:59:59 -0700399 self->PushVerifier(this);
jeffhaof56197c2012-03-05 18:01:54 -0800400}
401
Mathieu Chartier590fee92013-09-13 13:46:47 -0700402MethodVerifier::~MethodVerifier() {
Mathieu Chartierd0ad2ee2015-03-31 14:59:59 -0700403 Thread::Current()->PopVerifier(this);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700404 STLDeleteElements(&failure_messages_);
405}
406
Andreas Gampeaaf0d382017-11-27 14:10:21 -0800407void MethodVerifier::FindLocksAtDexPc(
408 ArtMethod* m,
409 uint32_t dex_pc,
Andreas Gampe6cc23ac2018-08-24 15:22:43 -0700410 std::vector<MethodVerifier::DexLockInfo>* monitor_enter_dex_pcs,
411 uint32_t api_level) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700412 StackHandleScope<2> hs(Thread::Current());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700413 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache()));
414 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader()));
Andreas Gampebf9611f2016-03-25 16:58:00 -0700415 MethodVerifier verifier(hs.Self(),
416 m->GetDexFile(),
417 dex_cache,
418 class_loader,
David Brazdil15fc7292016-09-02 14:13:18 +0100419 m->GetClassDef(),
Andreas Gampebf9611f2016-03-25 16:58:00 -0700420 m->GetCodeItem(),
421 m->GetDexMethodIndex(),
422 m,
423 m->GetAccessFlags(),
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700424 /* can_load_classes= */ false,
425 /* allow_soft_failures= */ true,
426 /* need_precise_constants= */ false,
427 /* verify_to_dump= */ false,
428 /* allow_thread_suspension= */ false,
Andreas Gampe6cc23ac2018-08-24 15:22:43 -0700429 api_level);
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700430 verifier.interesting_dex_pc_ = dex_pc;
Ian Rogers46960fe2014-05-23 10:43:43 -0700431 verifier.monitor_enter_dex_pcs_ = monitor_enter_dex_pcs;
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700432 verifier.FindLocksAtDexPc();
433}
434
435void MethodVerifier::FindLocksAtDexPc() {
Ian Rogers7b078e82014-09-10 14:44:24 -0700436 CHECK(monitor_enter_dex_pcs_ != nullptr);
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -0800437 CHECK(code_item_accessor_.HasCodeItem()); // This only makes sense for methods with code.
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700438
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -0800439 // Quick check whether there are any monitor_enter instructions before verifying.
440 for (const DexInstructionPcPair& inst : code_item_accessor_) {
441 if (inst->Opcode() == Instruction::MONITOR_ENTER) {
442 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
443 // verification. In practice, the phase we want relies on data structures set up by all the
444 // earlier passes, so we just run the full method verification and bail out early when we've
445 // got what we wanted.
446 Verify();
447 return;
448 }
Andreas Gampecb3c08f2014-09-18 13:16:38 -0700449 }
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700450}
451
Ian Rogersad0b3a32012-04-16 14:50:24 -0700452bool MethodVerifier::Verify() {
Andreas Gampee6215c02015-08-31 18:54:38 -0700453 // Some older code doesn't correctly mark constructors as such. Test for this case by looking at
454 // the name.
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800455 const dex::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
Andreas Gampee6215c02015-08-31 18:54:38 -0700456 const char* method_name = dex_file_->StringDataByIdx(method_id.name_idx_);
457 bool instance_constructor_by_name = strcmp("<init>", method_name) == 0;
458 bool static_constructor_by_name = strcmp("<clinit>", method_name) == 0;
459 bool constructor_by_name = instance_constructor_by_name || static_constructor_by_name;
460 // Check that only constructors are tagged, and check for bad code that doesn't tag constructors.
461 if ((method_access_flags_ & kAccConstructor) != 0) {
462 if (!constructor_by_name) {
463 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
464 << "method is marked as constructor, but not named accordingly";
jeffhaobdb76512011-09-07 11:43:16 -0700465 return false;
Andreas Gampee6215c02015-08-31 18:54:38 -0700466 }
467 is_constructor_ = true;
468 } else if (constructor_by_name) {
David Sehr709b0702016-10-13 09:12:37 -0700469 LOG(WARNING) << "Method " << dex_file_->PrettyMethod(dex_method_idx_)
Andreas Gampee6215c02015-08-31 18:54:38 -0700470 << " not marked as constructor.";
471 is_constructor_ = true;
472 }
473 // If it's a constructor, check whether IsStatic() matches the name.
474 // This should have been rejected by the dex file verifier. Only do in debug build.
475 if (kIsDebugBuild) {
476 if (IsConstructor()) {
477 if (IsStatic() ^ static_constructor_by_name) {
478 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
479 << "constructor name doesn't match static flag";
480 return false;
481 }
jeffhaobdb76512011-09-07 11:43:16 -0700482 }
jeffhaobdb76512011-09-07 11:43:16 -0700483 }
Andreas Gampee6215c02015-08-31 18:54:38 -0700484
485 // Methods may only have one of public/protected/private.
486 // This should have been rejected by the dex file verifier. Only do in debug build.
487 if (kIsDebugBuild) {
488 size_t access_mod_count =
489 (((method_access_flags_ & kAccPublic) == 0) ? 0 : 1) +
490 (((method_access_flags_ & kAccProtected) == 0) ? 0 : 1) +
491 (((method_access_flags_ & kAccPrivate) == 0) ? 0 : 1);
492 if (access_mod_count > 1) {
493 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "method has more than one of public/protected/private";
494 return false;
495 }
496 }
497
498 // If there aren't any instructions, make sure that's expected, then exit successfully.
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -0800499 if (!code_item_accessor_.HasCodeItem()) {
Andreas Gampe76ed99d2016-03-28 18:31:29 -0700500 // Only native or abstract methods may not have code.
501 if ((method_access_flags_ & (kAccNative | kAccAbstract)) == 0) {
502 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "zero-length code in concrete non-native method";
503 return false;
504 }
505
Andreas Gampee6215c02015-08-31 18:54:38 -0700506 // This should have been rejected by the dex file verifier. Only do in debug build.
Andreas Gampe76ed99d2016-03-28 18:31:29 -0700507 // Note: the above will also be rejected in the dex file verifier, starting in dex version 37.
Andreas Gampee6215c02015-08-31 18:54:38 -0700508 if (kIsDebugBuild) {
Andreas Gampee6215c02015-08-31 18:54:38 -0700509 if ((method_access_flags_ & kAccAbstract) != 0) {
510 // Abstract methods are not allowed to have the following flags.
511 static constexpr uint32_t kForbidden =
512 kAccPrivate |
513 kAccStatic |
514 kAccFinal |
515 kAccNative |
516 kAccStrict |
517 kAccSynchronized;
518 if ((method_access_flags_ & kForbidden) != 0) {
519 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
520 << "method can't be abstract and private/static/final/native/strict/synchronized";
521 return false;
522 }
523 }
David Brazdil15fc7292016-09-02 14:13:18 +0100524 if ((class_def_.GetJavaAccessFlags() & kAccInterface) != 0) {
Alex Lighteb7c1442015-08-31 13:17:42 -0700525 // Interface methods must be public and abstract (if default methods are disabled).
Neil Fuller9724c632016-01-07 15:42:47 +0000526 uint32_t kRequired = kAccPublic;
Alex Lighteb7c1442015-08-31 13:17:42 -0700527 if ((method_access_flags_ & kRequired) != kRequired) {
Neil Fuller9724c632016-01-07 15:42:47 +0000528 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface methods must be public";
Andreas Gampee6215c02015-08-31 18:54:38 -0700529 return false;
530 }
531 // In addition to the above, interface methods must not be protected.
532 static constexpr uint32_t kForbidden = kAccProtected;
533 if ((method_access_flags_ & kForbidden) != 0) {
534 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface methods can't be protected";
535 return false;
536 }
537 }
538 // We also don't allow constructors to be abstract or native.
539 if (IsConstructor()) {
540 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "constructors can't be abstract or native";
541 return false;
542 }
543 }
544 return true;
545 }
546
547 // This should have been rejected by the dex file verifier. Only do in debug build.
548 if (kIsDebugBuild) {
549 // When there's code, the method must not be native or abstract.
550 if ((method_access_flags_ & (kAccNative | kAccAbstract)) != 0) {
551 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "non-zero-length code in abstract or native method";
552 return false;
553 }
554
David Brazdil15fc7292016-09-02 14:13:18 +0100555 if ((class_def_.GetJavaAccessFlags() & kAccInterface) != 0) {
Alex Lighteb7c1442015-08-31 13:17:42 -0700556 // Interfaces may always have static initializers for their fields. If we are running with
557 // default methods enabled we also allow other public, static, non-final methods to have code.
558 // Otherwise that is the only type of method allowed.
Alex Light0db36b32015-10-27 14:06:34 -0700559 if (!(IsConstructor() && IsStatic())) {
Neil Fuller9724c632016-01-07 15:42:47 +0000560 if (IsInstanceConstructor()) {
561 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interfaces may not have non-static constructor";
562 return false;
563 } else if (method_access_flags_ & kAccFinal) {
564 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interfaces may not have final methods";
565 return false;
Alex Lightb55f1ac2016-04-12 15:50:55 -0700566 } else {
567 uint32_t access_flag_options = kAccPublic;
Mathieu Chartierf6e31472017-12-28 13:32:08 -0800568 if (dex_file_->SupportsDefaultMethods()) {
Alex Lightb55f1ac2016-04-12 15:50:55 -0700569 access_flag_options |= kAccPrivate;
570 }
571 if (!(method_access_flags_ & access_flag_options)) {
572 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
573 << "interfaces may not have protected or package-private members";
574 return false;
575 }
Alex Lighteb7c1442015-08-31 13:17:42 -0700576 }
Andreas Gampee6215c02015-08-31 18:54:38 -0700577 }
578 }
579
580 // Instance constructors must not be synchronized.
581 if (IsInstanceConstructor()) {
582 static constexpr uint32_t kForbidden = kAccSynchronized;
583 if ((method_access_flags_ & kForbidden) != 0) {
584 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "constructors can't be synchronized";
585 return false;
586 }
587 }
588 }
589
Ian Rogersd81871c2011-10-03 13:57:23 -0700590 // Sanity-check the register counts. ins + locals = registers, so make sure that ins <= registers.
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -0800591 if (code_item_accessor_.InsSize() > code_item_accessor_.RegistersSize()) {
592 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad register counts (ins="
593 << code_item_accessor_.InsSize()
594 << " regs=" << code_item_accessor_.RegistersSize();
Ian Rogersd81871c2011-10-03 13:57:23 -0700595 return false;
jeffhaobdb76512011-09-07 11:43:16 -0700596 }
Alex Lighteb7c1442015-08-31 13:17:42 -0700597
Ian Rogersd81871c2011-10-03 13:57:23 -0700598 // Allocate and initialize an array to hold instruction data.
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -0800599 insn_flags_.reset(allocator_.AllocArray<InstructionFlags>(
600 code_item_accessor_.InsnsSizeInCodeUnits()));
Mathieu Chartierde40d472015-10-15 17:47:48 -0700601 DCHECK(insn_flags_ != nullptr);
602 std::uninitialized_fill_n(insn_flags_.get(),
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -0800603 code_item_accessor_.InsnsSizeInCodeUnits(),
Mathieu Chartierde40d472015-10-15 17:47:48 -0700604 InstructionFlags());
Ian Rogersd81871c2011-10-03 13:57:23 -0700605 // Run through the instructions and see if the width checks out.
606 bool result = ComputeWidthsAndCountOps();
Andreas Gampebf1cb772017-05-15 15:39:00 -0700607 bool allow_runtime_only_instructions = !Runtime::Current()->IsAotCompiler() || verify_to_dump_;
Ian Rogersd81871c2011-10-03 13:57:23 -0700608 // Flag instructions guarded by a "try" block and check exception handlers.
609 result = result && ScanTryCatchBlocks();
610 // Perform static instruction verification.
Andreas Gampebf1cb772017-05-15 15:39:00 -0700611 result = result && (allow_runtime_only_instructions
612 ? VerifyInstructions<true>()
613 : VerifyInstructions<false>());
Ian Rogersad0b3a32012-04-16 14:50:24 -0700614 // Perform code-flow analysis and return.
Vladimir Marko2b5eaa22013-12-13 13:59:30 +0000615 result = result && VerifyCodeFlow();
Andreas Gampe53e32d12015-12-09 21:03:23 -0800616
Vladimir Marko2b5eaa22013-12-13 13:59:30 +0000617 return result;
jeffhaoba5ebb92011-08-25 17:24:37 -0700618}
619
Ian Rogers776ac1f2012-04-13 23:36:36 -0700620std::ostream& MethodVerifier::Fail(VerifyError error) {
Andreas Gampe0760a812015-08-26 17:12:51 -0700621 // Mark the error type as encountered.
Andreas Gampea727e372015-08-25 09:22:37 -0700622 encountered_failure_types_ |= static_cast<uint32_t>(error);
Andreas Gampe0760a812015-08-26 17:12:51 -0700623
Ian Rogersad0b3a32012-04-16 14:50:24 -0700624 switch (error) {
625 case VERIFY_ERROR_NO_CLASS:
626 case VERIFY_ERROR_NO_FIELD:
627 case VERIFY_ERROR_NO_METHOD:
628 case VERIFY_ERROR_ACCESS_CLASS:
629 case VERIFY_ERROR_ACCESS_FIELD:
630 case VERIFY_ERROR_ACCESS_METHOD:
Ian Rogers08f753d2012-08-24 14:35:25 -0700631 case VERIFY_ERROR_INSTANTIATION:
632 case VERIFY_ERROR_CLASS_CHANGE:
Igor Murashkin158f35c2015-06-10 15:55:30 -0700633 case VERIFY_ERROR_FORCE_INTERPRETER:
Andreas Gampea727e372015-08-25 09:22:37 -0700634 case VERIFY_ERROR_LOCKING:
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800635 if (Runtime::Current()->IsAotCompiler() || !can_load_classes_) {
jeffhaofaf459e2012-08-31 15:32:47 -0700636 // If we're optimistically running verification at compile time, turn NO_xxx, ACCESS_xxx,
637 // class change and instantiation errors into soft verification errors so that we re-verify
638 // at runtime. We may fail to find or to agree on access because of not yet available class
639 // loaders, or class loaders that will differ at runtime. In these cases, we don't want to
640 // affect the soundness of the code being compiled. Instead, the generated code runs "slow
641 // paths" that dynamically perform the verification and cause the behavior to be that akin
642 // to an interpreter.
643 error = VERIFY_ERROR_BAD_CLASS_SOFT;
644 } else {
Jeff Haoa3faaf42013-09-03 19:07:00 -0700645 // If we fail again at runtime, mark that this instruction would throw and force this
646 // method to be executed using the interpreter with checks.
jeffhaofaf459e2012-08-31 15:32:47 -0700647 have_pending_runtime_throw_failure_ = true;
Andreas Gamped7f8d052015-03-12 11:05:47 -0700648
649 // We need to save the work_line if the instruction wasn't throwing before. Otherwise we'll
650 // try to merge garbage.
651 // Note: this assumes that Fail is called before we do any work_line modifications.
Andreas Gamped5ad72f2015-06-26 17:33:47 -0700652 // Note: this can fail before we touch any instruction, for the signature of a method. So
653 // add a check.
Andreas Gampee2abbc62017-09-15 11:59:26 -0700654 if (work_insn_idx_ < dex::kDexNoIndex) {
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -0800655 const Instruction& inst = code_item_accessor_.InstructionAt(work_insn_idx_);
Mathieu Chartier1d2d4ff2017-09-23 16:11:06 -0700656 int opcode_flags = Instruction::FlagsOf(inst.Opcode());
Andreas Gamped7f8d052015-03-12 11:05:47 -0700657
Andreas Gamped5ad72f2015-06-26 17:33:47 -0700658 if ((opcode_flags & Instruction::kThrow) == 0 && CurrentInsnFlags()->IsInTry()) {
659 saved_line_->CopyFromLine(work_line_.get());
660 }
Andreas Gamped7f8d052015-03-12 11:05:47 -0700661 }
jeffhaofaf459e2012-08-31 15:32:47 -0700662 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700663 break;
Andreas Gampea727e372015-08-25 09:22:37 -0700664
Ian Rogersad0b3a32012-04-16 14:50:24 -0700665 // Indication that verification should be retried at runtime.
666 case VERIFY_ERROR_BAD_CLASS_SOFT:
Jeff Haoee988952013-04-16 14:23:47 -0700667 if (!allow_soft_failures_) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700668 have_pending_hard_failure_ = true;
669 }
670 break;
Andreas Gampea727e372015-08-25 09:22:37 -0700671
jeffhaod5347e02012-03-22 17:25:05 -0700672 // Hard verification failures at compile time will still fail at runtime, so the class is
673 // marked as rejected to prevent it from being compiled.
Ian Rogersad0b3a32012-04-16 14:50:24 -0700674 case VERIFY_ERROR_BAD_CLASS_HARD: {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700675 have_pending_hard_failure_ = true;
Andreas Gampeebf850c2015-08-14 15:37:35 -0700676 if (VLOG_IS_ON(verifier) && kDumpRegLinesOnHardFailureIfVLOG) {
677 ScopedObjectAccess soa(Thread::Current());
678 std::ostringstream oss;
679 Dump(oss);
680 LOG(ERROR) << oss.str();
681 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700682 break;
Ian Rogers47a05882012-02-03 12:23:33 -0800683 }
684 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700685 failures_.push_back(error);
David Sehr709b0702016-10-13 09:12:37 -0700686 std::string location(StringPrintf("%s: [0x%X] ", dex_file_->PrettyMethod(dex_method_idx_).c_str(),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700687 work_insn_idx_));
Elena Sayapina78480ec2014-08-15 15:52:42 +0700688 std::ostringstream* failure_message = new std::ostringstream(location, std::ostringstream::ate);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700689 failure_messages_.push_back(failure_message);
690 return *failure_message;
691}
692
Ian Rogers576ca0c2014-06-06 15:58:22 -0700693std::ostream& MethodVerifier::LogVerifyInfo() {
David Sehr709b0702016-10-13 09:12:37 -0700694 return info_messages_ << "VFY: " << dex_file_->PrettyMethod(dex_method_idx_)
Ian Rogers576ca0c2014-06-06 15:58:22 -0700695 << '[' << reinterpret_cast<void*>(work_insn_idx_) << "] : ";
696}
697
Ian Rogersad0b3a32012-04-16 14:50:24 -0700698void MethodVerifier::PrependToLastFailMessage(std::string prepend) {
699 size_t failure_num = failure_messages_.size();
700 DCHECK_NE(failure_num, 0U);
701 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
702 prepend += last_fail_message->str();
Elena Sayapina78480ec2014-08-15 15:52:42 +0700703 failure_messages_[failure_num - 1] = new std::ostringstream(prepend, std::ostringstream::ate);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700704 delete last_fail_message;
705}
706
Vladimir Marko5c657fe2016-11-03 15:12:29 +0000707void MethodVerifier::AppendToLastFailMessage(const std::string& append) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700708 size_t failure_num = failure_messages_.size();
709 DCHECK_NE(failure_num, 0U);
710 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
711 (*last_fail_message) << append;
Ian Rogers47a05882012-02-03 12:23:33 -0800712}
713
Ian Rogers776ac1f2012-04-13 23:36:36 -0700714bool MethodVerifier::ComputeWidthsAndCountOps() {
jeffhaobdb76512011-09-07 11:43:16 -0700715 size_t new_instance_count = 0;
716 size_t monitor_enter_count = 0;
jeffhaobdb76512011-09-07 11:43:16 -0700717
Mathieu Chartieraf7c9022017-10-27 09:42:46 -0700718 // We can't assume the instruction is well formed, handle the case where calculating the size
719 // goes past the end of the code item.
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -0800720 SafeDexInstructionIterator it(code_item_accessor_.begin(), code_item_accessor_.end());
721 for ( ; !it.IsErrorState() && it < code_item_accessor_.end(); ++it) {
Mathieu Chartieraf7c9022017-10-27 09:42:46 -0700722 // In case the instruction goes past the end of the code item, make sure to not process it.
723 SafeDexInstructionIterator next = it;
724 ++next;
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -0800725 if (next.IsErrorState()) {
Mathieu Chartieraf7c9022017-10-27 09:42:46 -0700726 break;
727 }
728 Instruction::Code opcode = it->Opcode();
Ian Rogersa9a82542013-10-04 11:17:26 -0700729 switch (opcode) {
730 case Instruction::APUT_OBJECT:
731 case Instruction::CHECK_CAST:
732 has_check_casts_ = true;
733 break;
734 case Instruction::INVOKE_VIRTUAL:
735 case Instruction::INVOKE_VIRTUAL_RANGE:
736 case Instruction::INVOKE_INTERFACE:
737 case Instruction::INVOKE_INTERFACE_RANGE:
738 has_virtual_or_interface_invokes_ = true;
739 break;
740 case Instruction::MONITOR_ENTER:
741 monitor_enter_count++;
742 break;
743 case Instruction::NEW_INSTANCE:
744 new_instance_count++;
745 break;
746 default:
747 break;
jeffhaobdb76512011-09-07 11:43:16 -0700748 }
Mathieu Chartier2b2bef22017-10-26 17:10:19 -0700749 GetInstructionFlags(it.DexPc()).SetIsOpcode();
jeffhaobdb76512011-09-07 11:43:16 -0700750 }
751
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -0800752 if (it != code_item_accessor_.end()) {
753 const size_t insns_size = code_item_accessor_.InsnsSizeInCodeUnits();
jeffhaod5347e02012-03-22 17:25:05 -0700754 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "code did not end where expected ("
Mathieu Chartier2b2bef22017-10-26 17:10:19 -0700755 << it.DexPc() << " vs. " << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700756 return false;
757 }
758
Ian Rogersd81871c2011-10-03 13:57:23 -0700759 new_instance_count_ = new_instance_count;
760 monitor_enter_count_ = monitor_enter_count;
jeffhaobdb76512011-09-07 11:43:16 -0700761 return true;
762}
763
Ian Rogers776ac1f2012-04-13 23:36:36 -0700764bool MethodVerifier::ScanTryCatchBlocks() {
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -0800765 const uint32_t tries_size = code_item_accessor_.TriesSize();
jeffhaobdb76512011-09-07 11:43:16 -0700766 if (tries_size == 0) {
767 return true;
768 }
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -0800769 const uint32_t insns_size = code_item_accessor_.InsnsSizeInCodeUnits();
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800770 for (const dex::TryItem& try_item : code_item_accessor_.TryItems()) {
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -0800771 const uint32_t start = try_item.start_addr_;
772 const uint32_t end = start + try_item.insn_count_;
jeffhaobdb76512011-09-07 11:43:16 -0700773 if ((start >= end) || (start >= insns_size) || (end > insns_size)) {
jeffhaod5347e02012-03-22 17:25:05 -0700774 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad exception entry: startAddr=" << start
775 << " endAddr=" << end << " (size=" << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700776 return false;
777 }
Mathieu Chartierde40d472015-10-15 17:47:48 -0700778 if (!GetInstructionFlags(start).IsOpcode()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700779 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
780 << "'try' block starts inside an instruction (" << start << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700781 return false;
782 }
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -0800783 DexInstructionIterator end_it(code_item_accessor_.Insns(), end);
784 for (DexInstructionIterator it(code_item_accessor_.Insns(), start); it < end_it; ++it) {
785 GetInstructionFlags(it.DexPc()).SetInTry();
jeffhaobdb76512011-09-07 11:43:16 -0700786 }
787 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800788 // Iterate over each of the handlers to verify target addresses.
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -0800789 const uint8_t* handlers_ptr = code_item_accessor_.GetCatchHandlerData();
790 const uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700791 ClassLinker* linker = Runtime::Current()->GetClassLinker();
jeffhaobdb76512011-09-07 11:43:16 -0700792 for (uint32_t idx = 0; idx < handlers_size; idx++) {
Ian Rogers0571d352011-11-03 19:51:38 -0700793 CatchHandlerIterator iterator(handlers_ptr);
794 for (; iterator.HasNext(); iterator.Next()) {
Igor Murashkin2ffb7032017-11-08 13:35:21 -0800795 uint32_t dex_pc = iterator.GetHandlerAddress();
Mathieu Chartierde40d472015-10-15 17:47:48 -0700796 if (!GetInstructionFlags(dex_pc).IsOpcode()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700797 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
798 << "exception handler starts at bad address (" << dex_pc << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700799 return false;
800 }
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -0800801 if (!CheckNotMoveResult(code_item_accessor_.Insns(), dex_pc)) {
Stephen Kyle9bc61992014-09-22 13:53:15 +0100802 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
803 << "exception handler begins with move-result* (" << dex_pc << ")";
804 return false;
805 }
Mathieu Chartierde40d472015-10-15 17:47:48 -0700806 GetInstructionFlags(dex_pc).SetBranchTarget();
Ian Rogers28ad40d2011-10-27 15:19:26 -0700807 // Ensure exception types are resolved so that they don't need resolution to be delivered,
808 // unresolved exception types will be ignored by exception delivery
Andreas Gampea5b09a62016-11-17 15:21:22 -0800809 if (iterator.GetHandlerTypeIndex().IsValid()) {
Vladimir Marko666ee3d2017-12-11 18:37:36 +0000810 ObjPtr<mirror::Class> exception_type =
811 linker->ResolveType(iterator.GetHandlerTypeIndex(), dex_cache_, class_loader_);
Ian Rogers7b078e82014-09-10 14:44:24 -0700812 if (exception_type == nullptr) {
813 DCHECK(self_->IsExceptionPending());
814 self_->ClearException();
Ian Rogers28ad40d2011-10-27 15:19:26 -0700815 }
816 }
jeffhaobdb76512011-09-07 11:43:16 -0700817 }
Ian Rogers0571d352011-11-03 19:51:38 -0700818 handlers_ptr = iterator.EndDataPointer();
jeffhaobdb76512011-09-07 11:43:16 -0700819 }
jeffhaobdb76512011-09-07 11:43:16 -0700820 return true;
821}
822
Andreas Gampebf1cb772017-05-15 15:39:00 -0700823template <bool kAllowRuntimeOnlyInstructions>
Ian Rogers776ac1f2012-04-13 23:36:36 -0700824bool MethodVerifier::VerifyInstructions() {
Ian Rogers0c7abda2012-09-19 13:33:42 -0700825 /* 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 -0700826 GetInstructionFlags(0).SetBranchTarget();
827 GetInstructionFlags(0).SetCompileTimeInfoPoint();
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -0800828 for (const DexInstructionPcPair& inst : code_item_accessor_) {
Mathieu Chartier2b2bef22017-10-26 17:10:19 -0700829 const uint32_t dex_pc = inst.DexPc();
830 if (!VerifyInstruction<kAllowRuntimeOnlyInstructions>(&inst.Inst(), dex_pc)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700831 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -0700832 return false;
833 }
834 /* Flag instructions that are garbage collection points */
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700835 // All invoke points are marked as "Throw" points already.
836 // We are relying on this to also count all the invokes as interesting.
Vladimir Marko8b858e12014-11-27 14:52:37 +0000837 if (inst->IsBranch()) {
Mathieu Chartierde40d472015-10-15 17:47:48 -0700838 GetInstructionFlags(dex_pc).SetCompileTimeInfoPoint();
Vladimir Marko8b858e12014-11-27 14:52:37 +0000839 // The compiler also needs safepoints for fall-through to loop heads.
840 // Such a loop head must be a target of a branch.
841 int32_t offset = 0;
842 bool cond, self_ok;
843 bool target_ok = GetBranchOffset(dex_pc, &offset, &cond, &self_ok);
844 DCHECK(target_ok);
Mathieu Chartierde40d472015-10-15 17:47:48 -0700845 GetInstructionFlags(dex_pc + offset).SetCompileTimeInfoPoint();
Vladimir Marko8b858e12014-11-27 14:52:37 +0000846 } else if (inst->IsSwitch() || inst->IsThrow()) {
Mathieu Chartierde40d472015-10-15 17:47:48 -0700847 GetInstructionFlags(dex_pc).SetCompileTimeInfoPoint();
Ian Rogersb8c78592013-07-25 23:52:52 +0000848 } else if (inst->IsReturn()) {
Mathieu Chartierde40d472015-10-15 17:47:48 -0700849 GetInstructionFlags(dex_pc).SetCompileTimeInfoPointAndReturn();
Ian Rogersd81871c2011-10-03 13:57:23 -0700850 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700851 }
852 return true;
853}
854
Andreas Gampebf1cb772017-05-15 15:39:00 -0700855template <bool kAllowRuntimeOnlyInstructions>
856bool MethodVerifier::VerifyInstruction(const Instruction* inst, uint32_t code_offset) {
Andreas Gampee05cc662017-05-15 10:17:30 -0700857 if (Instruction::kHaveExperimentalInstructions && UNLIKELY(inst->IsExperimental())) {
Igor Murashkin4d7b75f2015-07-21 17:03:36 -0700858 // Experimental instructions don't yet have verifier support implementation.
859 // While it is possible to use them by themselves, when we try to use stable instructions
860 // with a virtual register that was created by an experimental instruction,
861 // the data flow analysis will fail.
862 Fail(VERIFY_ERROR_FORCE_INTERPRETER)
863 << "experimental instruction is not supported by verifier; skipping verification";
864 have_pending_experimental_failure_ = true;
865 return false;
866 }
867
Ian Rogersd81871c2011-10-03 13:57:23 -0700868 bool result = true;
869 switch (inst->GetVerifyTypeArgumentA()) {
870 case Instruction::kVerifyRegA:
Ian Rogers29a26482014-05-02 15:27:29 -0700871 result = result && CheckRegisterIndex(inst->VRegA());
Ian Rogersd81871c2011-10-03 13:57:23 -0700872 break;
873 case Instruction::kVerifyRegAWide:
Ian Rogers29a26482014-05-02 15:27:29 -0700874 result = result && CheckWideRegisterIndex(inst->VRegA());
Ian Rogersd81871c2011-10-03 13:57:23 -0700875 break;
876 }
877 switch (inst->GetVerifyTypeArgumentB()) {
878 case Instruction::kVerifyRegB:
Ian Rogers29a26482014-05-02 15:27:29 -0700879 result = result && CheckRegisterIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700880 break;
881 case Instruction::kVerifyRegBField:
Ian Rogers29a26482014-05-02 15:27:29 -0700882 result = result && CheckFieldIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700883 break;
884 case Instruction::kVerifyRegBMethod:
Ian Rogers29a26482014-05-02 15:27:29 -0700885 result = result && CheckMethodIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700886 break;
887 case Instruction::kVerifyRegBNewInstance:
Andreas Gampea5b09a62016-11-17 15:21:22 -0800888 result = result && CheckNewInstance(dex::TypeIndex(inst->VRegB()));
Ian Rogersd81871c2011-10-03 13:57:23 -0700889 break;
890 case Instruction::kVerifyRegBString:
Ian Rogers29a26482014-05-02 15:27:29 -0700891 result = result && CheckStringIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700892 break;
893 case Instruction::kVerifyRegBType:
Andreas Gampea5b09a62016-11-17 15:21:22 -0800894 result = result && CheckTypeIndex(dex::TypeIndex(inst->VRegB()));
Ian Rogersd81871c2011-10-03 13:57:23 -0700895 break;
896 case Instruction::kVerifyRegBWide:
Ian Rogers29a26482014-05-02 15:27:29 -0700897 result = result && CheckWideRegisterIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700898 break;
Orion Hodson2e599942017-09-22 16:17:41 +0100899 case Instruction::kVerifyRegBCallSite:
900 result = result && CheckCallSiteIndex(inst->VRegB());
901 break;
902 case Instruction::kVerifyRegBMethodHandle:
903 result = result && CheckMethodHandleIndex(inst->VRegB());
904 break;
905 case Instruction::kVerifyRegBPrototype:
906 result = result && CheckPrototypeIndex(inst->VRegB());
907 break;
Ian Rogersd81871c2011-10-03 13:57:23 -0700908 }
909 switch (inst->GetVerifyTypeArgumentC()) {
910 case Instruction::kVerifyRegC:
Ian Rogers29a26482014-05-02 15:27:29 -0700911 result = result && CheckRegisterIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700912 break;
913 case Instruction::kVerifyRegCField:
Ian Rogers29a26482014-05-02 15:27:29 -0700914 result = result && CheckFieldIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700915 break;
916 case Instruction::kVerifyRegCNewArray:
Andreas Gampea5b09a62016-11-17 15:21:22 -0800917 result = result && CheckNewArray(dex::TypeIndex(inst->VRegC()));
Ian Rogersd81871c2011-10-03 13:57:23 -0700918 break;
919 case Instruction::kVerifyRegCType:
Andreas Gampea5b09a62016-11-17 15:21:22 -0800920 result = result && CheckTypeIndex(dex::TypeIndex(inst->VRegC()));
Ian Rogersd81871c2011-10-03 13:57:23 -0700921 break;
922 case Instruction::kVerifyRegCWide:
Ian Rogers29a26482014-05-02 15:27:29 -0700923 result = result && CheckWideRegisterIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700924 break;
925 }
Orion Hodsoncfa325e2016-10-13 10:25:54 +0100926 switch (inst->GetVerifyTypeArgumentH()) {
927 case Instruction::kVerifyRegHPrototype:
928 result = result && CheckPrototypeIndex(inst->VRegH());
929 break;
930 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700931 switch (inst->GetVerifyExtraFlags()) {
932 case Instruction::kVerifyArrayData:
933 result = result && CheckArrayData(code_offset);
934 break;
935 case Instruction::kVerifyBranchTarget:
936 result = result && CheckBranchTarget(code_offset);
937 break;
938 case Instruction::kVerifySwitchTargets:
939 result = result && CheckSwitchTargets(code_offset);
940 break;
Andreas Gampec3314312014-06-19 18:13:29 -0700941 case Instruction::kVerifyVarArgNonZero:
942 // Fall-through.
Ian Rogers29a26482014-05-02 15:27:29 -0700943 case Instruction::kVerifyVarArg: {
Taiju Tsuiki29498a22015-04-13 14:21:00 +0900944 // Instructions that can actually return a negative value shouldn't have this flag.
945 uint32_t v_a = dchecked_integral_cast<uint32_t>(inst->VRegA());
946 if ((inst->GetVerifyExtraFlags() == Instruction::kVerifyVarArgNonZero && v_a == 0) ||
947 v_a > Instruction::kMaxVarArgRegs) {
948 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << v_a << ") in "
Andreas Gampec3314312014-06-19 18:13:29 -0700949 "non-range invoke";
950 return false;
951 }
Taiju Tsuiki29498a22015-04-13 14:21:00 +0900952
Ian Rogers29a26482014-05-02 15:27:29 -0700953 uint32_t args[Instruction::kMaxVarArgRegs];
954 inst->GetVarArgs(args);
Taiju Tsuiki29498a22015-04-13 14:21:00 +0900955 result = result && CheckVarArgRegs(v_a, args);
Ian Rogersd81871c2011-10-03 13:57:23 -0700956 break;
Ian Rogers29a26482014-05-02 15:27:29 -0700957 }
Andreas Gampec3314312014-06-19 18:13:29 -0700958 case Instruction::kVerifyVarArgRangeNonZero:
959 // Fall-through.
Ian Rogersd81871c2011-10-03 13:57:23 -0700960 case Instruction::kVerifyVarArgRange:
Andreas Gampec3314312014-06-19 18:13:29 -0700961 if (inst->GetVerifyExtraFlags() == Instruction::kVerifyVarArgRangeNonZero &&
962 inst->VRegA() <= 0) {
963 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << inst->VRegA() << ") in "
964 "range invoke";
965 return false;
966 }
Ian Rogers29a26482014-05-02 15:27:29 -0700967 result = result && CheckVarArgRangeRegs(inst->VRegA(), inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700968 break;
969 case Instruction::kVerifyError:
jeffhaod5347e02012-03-22 17:25:05 -0700970 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected opcode " << inst->Name();
Ian Rogersd81871c2011-10-03 13:57:23 -0700971 result = false;
972 break;
973 }
Andreas Gampebf1cb772017-05-15 15:39:00 -0700974 if (!kAllowRuntimeOnlyInstructions && inst->GetVerifyIsRuntimeOnly()) {
Ian Rogers5fb22a92014-06-13 10:31:28 -0700975 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "opcode only expected at runtime " << inst->Name();
976 result = false;
977 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700978 return result;
979}
980
Ian Rogers7b078e82014-09-10 14:44:24 -0700981inline bool MethodVerifier::CheckRegisterIndex(uint32_t idx) {
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -0800982 if (UNLIKELY(idx >= code_item_accessor_.RegistersSize())) {
jeffhaod5347e02012-03-22 17:25:05 -0700983 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register index out of range (" << idx << " >= "
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -0800984 << code_item_accessor_.RegistersSize() << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700985 return false;
986 }
987 return true;
988}
989
Ian Rogers7b078e82014-09-10 14:44:24 -0700990inline bool MethodVerifier::CheckWideRegisterIndex(uint32_t idx) {
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -0800991 if (UNLIKELY(idx + 1 >= code_item_accessor_.RegistersSize())) {
jeffhaod5347e02012-03-22 17:25:05 -0700992 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "wide register index out of range (" << idx
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -0800993 << "+1 >= " << code_item_accessor_.RegistersSize() << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700994 return false;
995 }
996 return true;
997}
998
Orion Hodson2e599942017-09-22 16:17:41 +0100999inline bool MethodVerifier::CheckCallSiteIndex(uint32_t idx) {
1000 uint32_t limit = dex_file_->NumCallSiteIds();
1001 if (UNLIKELY(idx >= limit)) {
1002 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad call site index " << idx << " (max "
1003 << limit << ")";
1004 return false;
1005 }
1006 return true;
1007}
1008
Ian Rogers7b078e82014-09-10 14:44:24 -07001009inline bool MethodVerifier::CheckFieldIndex(uint32_t idx) {
Andreas Gampe29e81422017-05-15 16:29:32 -07001010 if (UNLIKELY(idx >= dex_file_->GetHeader().field_ids_size_)) {
jeffhaod5347e02012-03-22 17:25:05 -07001011 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad field index " << idx << " (max "
1012 << dex_file_->GetHeader().field_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001013 return false;
1014 }
1015 return true;
1016}
1017
Ian Rogers7b078e82014-09-10 14:44:24 -07001018inline bool MethodVerifier::CheckMethodIndex(uint32_t idx) {
Andreas Gampe29e81422017-05-15 16:29:32 -07001019 if (UNLIKELY(idx >= dex_file_->GetHeader().method_ids_size_)) {
jeffhaod5347e02012-03-22 17:25:05 -07001020 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad method index " << idx << " (max "
1021 << dex_file_->GetHeader().method_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001022 return false;
1023 }
1024 return true;
1025}
1026
Orion Hodson2e599942017-09-22 16:17:41 +01001027inline bool MethodVerifier::CheckMethodHandleIndex(uint32_t idx) {
1028 uint32_t limit = dex_file_->NumMethodHandles();
1029 if (UNLIKELY(idx >= limit)) {
1030 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad method handle index " << idx << " (max "
1031 << limit << ")";
1032 return false;
1033 }
1034 return true;
1035}
1036
Andreas Gampea5b09a62016-11-17 15:21:22 -08001037inline bool MethodVerifier::CheckNewInstance(dex::TypeIndex idx) {
Andreas Gampe29e81422017-05-15 16:29:32 -07001038 if (UNLIKELY(idx.index_ >= dex_file_->GetHeader().type_ids_size_)) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08001039 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx.index_ << " (max "
jeffhaod5347e02012-03-22 17:25:05 -07001040 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001041 return false;
1042 }
1043 // We don't need the actual class, just a pointer to the class name.
Ian Rogers0571d352011-11-03 19:51:38 -07001044 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Andreas Gampe29e81422017-05-15 16:29:32 -07001045 if (UNLIKELY(descriptor[0] != 'L')) {
jeffhaod5347e02012-03-22 17:25:05 -07001046 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "can't call new-instance on type '" << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001047 return false;
Andreas Gampe29e81422017-05-15 16:29:32 -07001048 } else if (UNLIKELY(strcmp(descriptor, "Ljava/lang/Class;") == 0)) {
Aart Bikdb698f12016-07-25 17:52:22 -07001049 // An unlikely new instance on Class is not allowed. Fall back to interpreter to ensure an
1050 // exception is thrown when this statement is executed (compiled code would not do that).
1051 Fail(VERIFY_ERROR_INSTANTIATION);
Ian Rogersd81871c2011-10-03 13:57:23 -07001052 }
1053 return true;
1054}
1055
Orion Hodsoncfa325e2016-10-13 10:25:54 +01001056inline bool MethodVerifier::CheckPrototypeIndex(uint32_t idx) {
Andreas Gampe29e81422017-05-15 16:29:32 -07001057 if (UNLIKELY(idx >= dex_file_->GetHeader().proto_ids_size_)) {
Orion Hodsoncfa325e2016-10-13 10:25:54 +01001058 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad prototype index " << idx << " (max "
1059 << dex_file_->GetHeader().proto_ids_size_ << ")";
1060 return false;
1061 }
1062 return true;
1063}
1064
Ian Rogers7b078e82014-09-10 14:44:24 -07001065inline bool MethodVerifier::CheckStringIndex(uint32_t idx) {
Andreas Gampe29e81422017-05-15 16:29:32 -07001066 if (UNLIKELY(idx >= dex_file_->GetHeader().string_ids_size_)) {
jeffhaod5347e02012-03-22 17:25:05 -07001067 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad string index " << idx << " (max "
1068 << dex_file_->GetHeader().string_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001069 return false;
1070 }
1071 return true;
1072}
1073
Andreas Gampea5b09a62016-11-17 15:21:22 -08001074inline bool MethodVerifier::CheckTypeIndex(dex::TypeIndex idx) {
Andreas Gampe29e81422017-05-15 16:29:32 -07001075 if (UNLIKELY(idx.index_ >= dex_file_->GetHeader().type_ids_size_)) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08001076 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx.index_ << " (max "
jeffhaod5347e02012-03-22 17:25:05 -07001077 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001078 return false;
1079 }
1080 return true;
1081}
1082
Andreas Gampea5b09a62016-11-17 15:21:22 -08001083bool MethodVerifier::CheckNewArray(dex::TypeIndex idx) {
Andreas Gampe29e81422017-05-15 16:29:32 -07001084 if (UNLIKELY(idx.index_ >= dex_file_->GetHeader().type_ids_size_)) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08001085 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx.index_ << " (max "
jeffhaod5347e02012-03-22 17:25:05 -07001086 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001087 return false;
1088 }
1089 int bracket_count = 0;
Ian Rogers0571d352011-11-03 19:51:38 -07001090 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -07001091 const char* cp = descriptor;
1092 while (*cp++ == '[') {
1093 bracket_count++;
1094 }
Andreas Gampe29e81422017-05-15 16:29:32 -07001095 if (UNLIKELY(bracket_count == 0)) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001096 /* The given class must be an array type. */
Brian Carlstrom93c33962013-07-26 10:37:43 -07001097 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1098 << "can't new-array class '" << descriptor << "' (not an array)";
Ian Rogersd81871c2011-10-03 13:57:23 -07001099 return false;
Andreas Gampe29e81422017-05-15 16:29:32 -07001100 } else if (UNLIKELY(bracket_count > 255)) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001101 /* It is illegal to create an array of more than 255 dimensions. */
Brian Carlstrom93c33962013-07-26 10:37:43 -07001102 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1103 << "can't new-array class '" << descriptor << "' (exceeds limit)";
Ian Rogersd81871c2011-10-03 13:57:23 -07001104 return false;
1105 }
1106 return true;
1107}
1108
Ian Rogers776ac1f2012-04-13 23:36:36 -07001109bool MethodVerifier::CheckArrayData(uint32_t cur_offset) {
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001110 const uint32_t insn_count = code_item_accessor_.InsnsSizeInCodeUnits();
1111 const uint16_t* insns = code_item_accessor_.Insns() + cur_offset;
Ian Rogersd81871c2011-10-03 13:57:23 -07001112 const uint16_t* array_data;
1113 int32_t array_data_offset;
1114
1115 DCHECK_LT(cur_offset, insn_count);
1116 /* make sure the start of the array data table is in range */
Andreas Gampe53de99c2015-08-17 13:43:55 -07001117 array_data_offset = insns[1] | (static_cast<int32_t>(insns[2]) << 16);
Andreas Gampe29e81422017-05-15 16:29:32 -07001118 if (UNLIKELY(static_cast<int32_t>(cur_offset) + array_data_offset < 0 ||
1119 cur_offset + array_data_offset + 2 >= insn_count)) {
jeffhaod5347e02012-03-22 17:25:05 -07001120 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data start: at " << cur_offset
Brian Carlstrom93c33962013-07-26 10:37:43 -07001121 << ", data offset " << array_data_offset
1122 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -07001123 return false;
1124 }
1125 /* offset to array data table is a relative branch-style offset */
1126 array_data = insns + array_data_offset;
Andreas Gampe57c47582015-07-01 22:05:59 -07001127 // Make sure the table is at an even dex pc, that is, 32-bit aligned.
Andreas Gampe29e81422017-05-15 16:29:32 -07001128 if (UNLIKELY(!IsAligned<4>(array_data))) {
jeffhaod5347e02012-03-22 17:25:05 -07001129 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned array data table: at " << cur_offset
1130 << ", data offset " << array_data_offset;
Ian Rogersd81871c2011-10-03 13:57:23 -07001131 return false;
1132 }
Andreas Gampe57c47582015-07-01 22:05:59 -07001133 // Make sure the array-data is marked as an opcode. This ensures that it was reached when
1134 // traversing the code item linearly. It is an approximation for a by-spec padding value.
Andreas Gampe29e81422017-05-15 16:29:32 -07001135 if (UNLIKELY(!GetInstructionFlags(cur_offset + array_data_offset).IsOpcode())) {
Andreas Gampe57c47582015-07-01 22:05:59 -07001136 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array data table at " << cur_offset
1137 << ", data offset " << array_data_offset
1138 << " not correctly visited, probably bad padding.";
1139 return false;
1140 }
1141
Ian Rogersd81871c2011-10-03 13:57:23 -07001142 uint32_t value_width = array_data[1];
Elliott Hughes398f64b2012-03-26 18:05:48 -07001143 uint32_t value_count = *reinterpret_cast<const uint32_t*>(&array_data[2]);
Ian Rogersd81871c2011-10-03 13:57:23 -07001144 uint32_t table_size = 4 + (value_width * value_count + 1) / 2;
1145 /* make sure the end of the switch is in range */
Andreas Gampe29e81422017-05-15 16:29:32 -07001146 if (UNLIKELY(cur_offset + array_data_offset + table_size > insn_count)) {
jeffhaod5347e02012-03-22 17:25:05 -07001147 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data end: at " << cur_offset
1148 << ", data offset " << array_data_offset << ", end "
1149 << cur_offset + array_data_offset + table_size
1150 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -07001151 return false;
1152 }
1153 return true;
1154}
1155
Ian Rogers776ac1f2012-04-13 23:36:36 -07001156bool MethodVerifier::CheckBranchTarget(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001157 int32_t offset;
1158 bool isConditional, selfOkay;
1159 if (!GetBranchOffset(cur_offset, &offset, &isConditional, &selfOkay)) {
1160 return false;
1161 }
Andreas Gampe29e81422017-05-15 16:29:32 -07001162 if (UNLIKELY(!selfOkay && offset == 0)) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001163 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch offset of zero not allowed at"
1164 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -07001165 return false;
1166 }
Elliott Hughes81ff3182012-03-23 20:35:56 -07001167 // Check for 32-bit overflow. This isn't strictly necessary if we can depend on the runtime
1168 // to have identical "wrap-around" behavior, but it's unwise to depend on that.
Andreas Gampe29e81422017-05-15 16:29:32 -07001169 if (UNLIKELY(((int64_t) cur_offset + (int64_t) offset) != (int64_t) (cur_offset + offset))) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001170 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch target overflow "
1171 << reinterpret_cast<void*>(cur_offset) << " +" << offset;
Ian Rogersd81871c2011-10-03 13:57:23 -07001172 return false;
1173 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001174 int32_t abs_offset = cur_offset + offset;
Andreas Gampe29e81422017-05-15 16:29:32 -07001175 if (UNLIKELY(abs_offset < 0 ||
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001176 (uint32_t) abs_offset >= code_item_accessor_.InsnsSizeInCodeUnits() ||
Andreas Gampe29e81422017-05-15 16:29:32 -07001177 !GetInstructionFlags(abs_offset).IsOpcode())) {
jeffhaod5347e02012-03-22 17:25:05 -07001178 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid branch target " << offset << " (-> "
Elliott Hughes398f64b2012-03-26 18:05:48 -07001179 << reinterpret_cast<void*>(abs_offset) << ") at "
1180 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -07001181 return false;
1182 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07001183 GetInstructionFlags(abs_offset).SetBranchTarget();
Ian Rogersd81871c2011-10-03 13:57:23 -07001184 return true;
1185}
1186
Ian Rogers776ac1f2012-04-13 23:36:36 -07001187bool MethodVerifier::GetBranchOffset(uint32_t cur_offset, int32_t* pOffset, bool* pConditional,
Ian Rogersd81871c2011-10-03 13:57:23 -07001188 bool* selfOkay) {
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001189 const uint16_t* insns = code_item_accessor_.Insns() + cur_offset;
Ian Rogersd81871c2011-10-03 13:57:23 -07001190 *pConditional = false;
1191 *selfOkay = false;
jeffhaoba5ebb92011-08-25 17:24:37 -07001192 switch (*insns & 0xff) {
1193 case Instruction::GOTO:
1194 *pOffset = ((int16_t) *insns) >> 8;
jeffhaoba5ebb92011-08-25 17:24:37 -07001195 break;
1196 case Instruction::GOTO_32:
1197 *pOffset = insns[1] | (((uint32_t) insns[2]) << 16);
jeffhaoba5ebb92011-08-25 17:24:37 -07001198 *selfOkay = true;
1199 break;
1200 case Instruction::GOTO_16:
1201 *pOffset = (int16_t) insns[1];
jeffhaoba5ebb92011-08-25 17:24:37 -07001202 break;
1203 case Instruction::IF_EQ:
1204 case Instruction::IF_NE:
1205 case Instruction::IF_LT:
1206 case Instruction::IF_GE:
1207 case Instruction::IF_GT:
1208 case Instruction::IF_LE:
1209 case Instruction::IF_EQZ:
1210 case Instruction::IF_NEZ:
1211 case Instruction::IF_LTZ:
1212 case Instruction::IF_GEZ:
1213 case Instruction::IF_GTZ:
1214 case Instruction::IF_LEZ:
1215 *pOffset = (int16_t) insns[1];
1216 *pConditional = true;
jeffhaoba5ebb92011-08-25 17:24:37 -07001217 break;
1218 default:
1219 return false;
jeffhaoba5ebb92011-08-25 17:24:37 -07001220 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001221 return true;
1222}
1223
Ian Rogers776ac1f2012-04-13 23:36:36 -07001224bool MethodVerifier::CheckSwitchTargets(uint32_t cur_offset) {
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001225 const uint32_t insn_count = code_item_accessor_.InsnsSizeInCodeUnits();
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001226 DCHECK_LT(cur_offset, insn_count);
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001227 const uint16_t* insns = code_item_accessor_.Insns() + cur_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001228 /* make sure the start of the switch is in range */
Andreas Gampe53de99c2015-08-17 13:43:55 -07001229 int32_t switch_offset = insns[1] | (static_cast<int32_t>(insns[2]) << 16);
Andreas Gampe29e81422017-05-15 16:29:32 -07001230 if (UNLIKELY(static_cast<int32_t>(cur_offset) + switch_offset < 0 ||
1231 cur_offset + switch_offset + 2 > insn_count)) {
jeffhaod5347e02012-03-22 17:25:05 -07001232 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch start: at " << cur_offset
Brian Carlstrom93c33962013-07-26 10:37:43 -07001233 << ", switch offset " << switch_offset
1234 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001235 return false;
1236 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001237 /* offset to switch table is a relative branch-style offset */
Ian Rogersd81871c2011-10-03 13:57:23 -07001238 const uint16_t* switch_insns = insns + switch_offset;
Andreas Gampe57c47582015-07-01 22:05:59 -07001239 // Make sure the table is at an even dex pc, that is, 32-bit aligned.
Andreas Gampe29e81422017-05-15 16:29:32 -07001240 if (UNLIKELY(!IsAligned<4>(switch_insns))) {
jeffhaod5347e02012-03-22 17:25:05 -07001241 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned switch table: at " << cur_offset
1242 << ", switch offset " << switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001243 return false;
1244 }
Andreas Gampe57c47582015-07-01 22:05:59 -07001245 // Make sure the switch data is marked as an opcode. This ensures that it was reached when
1246 // traversing the code item linearly. It is an approximation for a by-spec padding value.
Andreas Gampe29e81422017-05-15 16:29:32 -07001247 if (UNLIKELY(!GetInstructionFlags(cur_offset + switch_offset).IsOpcode())) {
Andreas Gampe57c47582015-07-01 22:05:59 -07001248 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "switch table at " << cur_offset
1249 << ", switch offset " << switch_offset
1250 << " not correctly visited, probably bad padding.";
1251 return false;
1252 }
1253
David Brazdil5469d342015-09-25 16:57:53 +01001254 bool is_packed_switch = (*insns & 0xff) == Instruction::PACKED_SWITCH;
1255
Ian Rogersd81871c2011-10-03 13:57:23 -07001256 uint32_t switch_count = switch_insns[1];
David Brazdil5469d342015-09-25 16:57:53 +01001257 int32_t targets_offset;
Ian Rogersd81871c2011-10-03 13:57:23 -07001258 uint16_t expected_signature;
David Brazdil5469d342015-09-25 16:57:53 +01001259 if (is_packed_switch) {
jeffhaoba5ebb92011-08-25 17:24:37 -07001260 /* 0=sig, 1=count, 2/3=firstKey */
1261 targets_offset = 4;
jeffhaoba5ebb92011-08-25 17:24:37 -07001262 expected_signature = Instruction::kPackedSwitchSignature;
1263 } else {
1264 /* 0=sig, 1=count, 2..count*2 = keys */
jeffhaoba5ebb92011-08-25 17:24:37 -07001265 targets_offset = 2 + 2 * switch_count;
1266 expected_signature = Instruction::kSparseSwitchSignature;
1267 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001268 uint32_t table_size = targets_offset + switch_count * 2;
Andreas Gampe29e81422017-05-15 16:29:32 -07001269 if (UNLIKELY(switch_insns[0] != expected_signature)) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001270 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1271 << StringPrintf("wrong signature for switch table (%x, wanted %x)",
1272 switch_insns[0], expected_signature);
jeffhaoba5ebb92011-08-25 17:24:37 -07001273 return false;
1274 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001275 /* make sure the end of the switch is in range */
Andreas Gampe29e81422017-05-15 16:29:32 -07001276 if (UNLIKELY(cur_offset + switch_offset + table_size > (uint32_t) insn_count)) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001277 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch end: at " << cur_offset
1278 << ", switch offset " << switch_offset
1279 << ", end " << (cur_offset + switch_offset + table_size)
jeffhaod5347e02012-03-22 17:25:05 -07001280 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001281 return false;
1282 }
David Brazdil5469d342015-09-25 16:57:53 +01001283
1284 constexpr int32_t keys_offset = 2;
1285 if (switch_count > 1) {
1286 if (is_packed_switch) {
1287 /* for a packed switch, verify that keys do not overflow int32 */
1288 int32_t first_key = switch_insns[keys_offset] | (switch_insns[keys_offset + 1] << 16);
1289 int32_t max_first_key =
1290 std::numeric_limits<int32_t>::max() - (static_cast<int32_t>(switch_count) - 1);
Andreas Gampe29e81422017-05-15 16:29:32 -07001291 if (UNLIKELY(first_key > max_first_key)) {
David Brazdil5469d342015-09-25 16:57:53 +01001292 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid packed switch: first_key=" << first_key
1293 << ", switch_count=" << switch_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001294 return false;
1295 }
David Brazdil5469d342015-09-25 16:57:53 +01001296 } else {
1297 /* for a sparse switch, verify the keys are in ascending order */
1298 int32_t last_key = switch_insns[keys_offset] | (switch_insns[keys_offset + 1] << 16);
1299 for (uint32_t targ = 1; targ < switch_count; targ++) {
1300 int32_t key =
1301 static_cast<int32_t>(switch_insns[keys_offset + targ * 2]) |
1302 static_cast<int32_t>(switch_insns[keys_offset + targ * 2 + 1] << 16);
Andreas Gampe29e81422017-05-15 16:29:32 -07001303 if (UNLIKELY(key <= last_key)) {
David Brazdil5469d342015-09-25 16:57:53 +01001304 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid sparse switch: last key=" << last_key
1305 << ", this=" << key;
1306 return false;
1307 }
1308 last_key = key;
1309 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001310 }
1311 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001312 /* verify each switch target */
Ian Rogersd81871c2011-10-03 13:57:23 -07001313 for (uint32_t targ = 0; targ < switch_count; targ++) {
Andreas Gampe53de99c2015-08-17 13:43:55 -07001314 int32_t offset = static_cast<int32_t>(switch_insns[targets_offset + targ * 2]) |
1315 static_cast<int32_t>(switch_insns[targets_offset + targ * 2 + 1] << 16);
Ian Rogersd81871c2011-10-03 13:57:23 -07001316 int32_t abs_offset = cur_offset + offset;
Andreas Gampe29e81422017-05-15 16:29:32 -07001317 if (UNLIKELY(abs_offset < 0 ||
1318 abs_offset >= static_cast<int32_t>(insn_count) ||
1319 !GetInstructionFlags(abs_offset).IsOpcode())) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001320 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch target " << offset
1321 << " (-> " << reinterpret_cast<void*>(abs_offset) << ") at "
1322 << reinterpret_cast<void*>(cur_offset)
1323 << "[" << targ << "]";
jeffhaoba5ebb92011-08-25 17:24:37 -07001324 return false;
1325 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07001326 GetInstructionFlags(abs_offset).SetBranchTarget();
Ian Rogersd81871c2011-10-03 13:57:23 -07001327 }
1328 return true;
1329}
1330
Ian Rogers776ac1f2012-04-13 23:36:36 -07001331bool MethodVerifier::CheckVarArgRegs(uint32_t vA, uint32_t arg[]) {
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001332 uint16_t registers_size = code_item_accessor_.RegistersSize();
Ian Rogersd81871c2011-10-03 13:57:23 -07001333 for (uint32_t idx = 0; idx < vA; idx++) {
Andreas Gampe29e81422017-05-15 16:29:32 -07001334 if (UNLIKELY(arg[idx] >= registers_size)) {
jeffhaod5347e02012-03-22 17:25:05 -07001335 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index (" << arg[idx]
1336 << ") in non-range invoke (>= " << registers_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001337 return false;
1338 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001339 }
1340
1341 return true;
1342}
1343
Ian Rogers776ac1f2012-04-13 23:36:36 -07001344bool MethodVerifier::CheckVarArgRangeRegs(uint32_t vA, uint32_t vC) {
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001345 uint16_t registers_size = code_item_accessor_.RegistersSize();
Ian Rogersd81871c2011-10-03 13:57:23 -07001346 // vA/vC are unsigned 8-bit/16-bit quantities for /range instructions, so there's no risk of
1347 // integer overflow when adding them here.
Andreas Gampe29e81422017-05-15 16:29:32 -07001348 if (UNLIKELY(vA + vC > registers_size)) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001349 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index " << vA << "+" << vC
1350 << " in range invoke (> " << registers_size << ")";
jeffhaoba5ebb92011-08-25 17:24:37 -07001351 return false;
1352 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001353 return true;
1354}
1355
Ian Rogers776ac1f2012-04-13 23:36:36 -07001356bool MethodVerifier::VerifyCodeFlow() {
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001357 const uint16_t registers_size = code_item_accessor_.RegistersSize();
jeffhaobdb76512011-09-07 11:43:16 -07001358
Ian Rogersd81871c2011-10-03 13:57:23 -07001359 /* Create and initialize table holding register status */
Brian Carlstrom93c33962013-07-26 10:37:43 -07001360 reg_table_.Init(kTrackCompilerInterestPoints,
1361 insn_flags_.get(),
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001362 code_item_accessor_.InsnsSizeInCodeUnits(),
Brian Carlstrom93c33962013-07-26 10:37:43 -07001363 registers_size,
1364 this);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07001365
Ian Rogersd0fbd852013-09-24 18:17:04 -07001366 work_line_.reset(RegisterLine::Create(registers_size, this));
1367 saved_line_.reset(RegisterLine::Create(registers_size, this));
jeffhaobdb76512011-09-07 11:43:16 -07001368
Ian Rogersd81871c2011-10-03 13:57:23 -07001369 /* Initialize register types of method arguments. */
1370 if (!SetTypesFromSignature()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001371 DCHECK_NE(failures_.size(), 0U);
1372 std::string prepend("Bad signature in ");
David Sehr709b0702016-10-13 09:12:37 -07001373 prepend += dex_file_->PrettyMethod(dex_method_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001374 PrependToLastFailMessage(prepend);
Ian Rogersd81871c2011-10-03 13:57:23 -07001375 return false;
1376 }
Andreas Gamped5ad72f2015-06-26 17:33:47 -07001377 // We may have a runtime failure here, clear.
1378 have_pending_runtime_throw_failure_ = false;
1379
Ian Rogersd81871c2011-10-03 13:57:23 -07001380 /* Perform code flow verification. */
1381 if (!CodeFlowVerifyMethod()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001382 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -07001383 return false;
jeffhaobdb76512011-09-07 11:43:16 -07001384 }
jeffhaobdb76512011-09-07 11:43:16 -07001385 return true;
1386}
1387
Ian Rogersad0b3a32012-04-16 14:50:24 -07001388std::ostream& MethodVerifier::DumpFailures(std::ostream& os) {
1389 DCHECK_EQ(failures_.size(), failure_messages_.size());
Jeff Hao4137f482013-11-22 11:44:57 -08001390 for (size_t i = 0; i < failures_.size(); ++i) {
1391 os << failure_messages_[i]->str() << "\n";
Ian Rogersad0b3a32012-04-16 14:50:24 -07001392 }
1393 return os;
1394}
1395
Ian Rogers776ac1f2012-04-13 23:36:36 -07001396void MethodVerifier::Dump(std::ostream& os) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001397 VariableIndentationOutputStream vios(&os);
1398 Dump(&vios);
1399}
1400
1401void MethodVerifier::Dump(VariableIndentationOutputStream* vios) {
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001402 if (!code_item_accessor_.HasCodeItem()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001403 vios->Stream() << "Native method\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001404 return;
jeffhaobdb76512011-09-07 11:43:16 -07001405 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001406 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001407 vios->Stream() << "Register Types:\n";
1408 ScopedIndentation indent1(vios);
1409 reg_types_.Dump(vios->Stream());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001410 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001411 vios->Stream() << "Dumping instructions and register lines:\n";
1412 ScopedIndentation indent1(vios);
Mathieu Chartier1d2d4ff2017-09-23 16:11:06 -07001413
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001414 for (const DexInstructionPcPair& inst : code_item_accessor_) {
Mathieu Chartier2b2bef22017-10-26 17:10:19 -07001415 const size_t dex_pc = inst.DexPc();
Andreas Gampe077d9db2018-01-19 18:54:14 -08001416
1417 // Might be asked to dump before the table is initialized.
1418 if (reg_table_.IsInitialized()) {
1419 RegisterLine* reg_line = reg_table_.GetLine(dex_pc);
1420 if (reg_line != nullptr) {
1421 vios->Stream() << reg_line->Dump(this) << "\n";
1422 }
jeffhaobdb76512011-09-07 11:43:16 -07001423 }
Andreas Gampe077d9db2018-01-19 18:54:14 -08001424
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001425 vios->Stream()
Mathieu Chartierde40d472015-10-15 17:47:48 -07001426 << StringPrintf("0x%04zx", dex_pc) << ": " << GetInstructionFlags(dex_pc).ToString() << " ";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001427 const bool kDumpHexOfInstruction = false;
1428 if (kDumpHexOfInstruction) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001429 vios->Stream() << inst->DumpHex(5) << " ";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001430 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001431 vios->Stream() << inst->DumpString(dex_file_) << "\n";
jeffhaoba5ebb92011-08-25 17:24:37 -07001432 }
jeffhaobdb76512011-09-07 11:43:16 -07001433}
1434
Ian Rogersd81871c2011-10-03 13:57:23 -07001435static bool IsPrimitiveDescriptor(char descriptor) {
1436 switch (descriptor) {
jeffhaobdb76512011-09-07 11:43:16 -07001437 case 'I':
1438 case 'C':
1439 case 'S':
1440 case 'B':
1441 case 'Z':
jeffhaobdb76512011-09-07 11:43:16 -07001442 case 'F':
1443 case 'D':
1444 case 'J':
Ian Rogersd81871c2011-10-03 13:57:23 -07001445 return true;
jeffhaobdb76512011-09-07 11:43:16 -07001446 default:
1447 return false;
1448 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001449}
1450
Ian Rogers776ac1f2012-04-13 23:36:36 -07001451bool MethodVerifier::SetTypesFromSignature() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001452 RegisterLine* reg_line = reg_table_.GetLine(0);
Andreas Gampeef0b1a12015-06-19 20:37:46 -07001453
1454 // Should have been verified earlier.
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001455 DCHECK_GE(code_item_accessor_.RegistersSize(), code_item_accessor_.InsSize());
Andreas Gampeef0b1a12015-06-19 20:37:46 -07001456
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001457 uint32_t arg_start = code_item_accessor_.RegistersSize() - code_item_accessor_.InsSize();
1458 size_t expected_args = code_item_accessor_.InsSize(); /* long/double count as two */
jeffhaobdb76512011-09-07 11:43:16 -07001459
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001460 // Include the "this" pointer.
Ian Rogersd81871c2011-10-03 13:57:23 -07001461 size_t cur_arg = 0;
Ian Rogersad0b3a32012-04-16 14:50:24 -07001462 if (!IsStatic()) {
Andreas Gampeef0b1a12015-06-19 20:37:46 -07001463 if (expected_args == 0) {
1464 // Expect at least a receiver.
1465 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected 0 args, but method is not static";
1466 return false;
1467 }
1468
Ian Rogersd81871c2011-10-03 13:57:23 -07001469 // If this is a constructor for a class other than java.lang.Object, mark the first ("this")
1470 // argument as uninitialized. This restricts field access until the superclass constructor is
1471 // called.
Ian Rogersd8f69b02014-09-10 21:43:52 +00001472 const RegType& declaring_class = GetDeclaringClass();
Andreas Gampef10b6e12015-08-12 10:48:12 -07001473 if (IsConstructor()) {
1474 if (declaring_class.IsJavaLangObject()) {
1475 // "this" is implicitly initialized.
1476 reg_line->SetThisInitialized();
Andreas Gampead238ce2015-08-24 21:13:08 -07001477 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, declaring_class);
Andreas Gampef10b6e12015-08-12 10:48:12 -07001478 } else {
Andreas Gampead238ce2015-08-24 21:13:08 -07001479 reg_line->SetRegisterType<LockOp::kClear>(
1480 this,
1481 arg_start + cur_arg,
1482 reg_types_.UninitializedThisArgument(declaring_class));
Andreas Gampef10b6e12015-08-12 10:48:12 -07001483 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001484 } else {
Andreas Gampead238ce2015-08-24 21:13:08 -07001485 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, declaring_class);
jeffhaobdb76512011-09-07 11:43:16 -07001486 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001487 cur_arg++;
jeffhaobdb76512011-09-07 11:43:16 -07001488 }
1489
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001490 const dex::ProtoId& proto_id =
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001491 dex_file_->GetMethodPrototype(dex_file_->GetMethodId(dex_method_idx_));
Ian Rogers0571d352011-11-03 19:51:38 -07001492 DexFileParameterIterator iterator(*dex_file_, proto_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07001493
1494 for (; iterator.HasNext(); iterator.Next()) {
1495 const char* descriptor = iterator.GetDescriptor();
Ian Rogers7b078e82014-09-10 14:44:24 -07001496 if (descriptor == nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001497 LOG(FATAL) << "Null descriptor";
1498 }
1499 if (cur_arg >= expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07001500 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1501 << " args, found more (" << descriptor << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001502 return false;
1503 }
1504 switch (descriptor[0]) {
1505 case 'L':
1506 case '[':
1507 // We assume that reference arguments are initialized. The only way it could be otherwise
1508 // (assuming the caller was verified) is if the current method is <init>, but in that case
1509 // it's effectively considered initialized the instant we reach here (in the sense that we
1510 // can return without doing anything or call virtual methods).
1511 {
Andreas Gampe98be1a92017-08-28 08:25:45 -07001512 // Note: don't check access. No error would be thrown for declaring or passing an
1513 // inaccessible class. Only actual accesses to fields or methods will.
1514 const RegType& reg_type = ResolveClass<CheckAccess::kNo>(iterator.GetTypeIdx());
Sebastien Hertz2ed76f92014-04-22 17:11:08 +02001515 if (!reg_type.IsNonZeroReferenceTypes()) {
1516 DCHECK(HasFailures());
1517 return false;
1518 }
Andreas Gampead238ce2015-08-24 21:13:08 -07001519 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001520 }
1521 break;
1522 case 'Z':
Andreas Gampead238ce2015-08-24 21:13:08 -07001523 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Boolean());
Ian Rogersd81871c2011-10-03 13:57:23 -07001524 break;
1525 case 'C':
Andreas Gampead238ce2015-08-24 21:13:08 -07001526 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Char());
Ian Rogersd81871c2011-10-03 13:57:23 -07001527 break;
1528 case 'B':
Andreas Gampead238ce2015-08-24 21:13:08 -07001529 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Byte());
Ian Rogersd81871c2011-10-03 13:57:23 -07001530 break;
1531 case 'I':
Andreas Gampead238ce2015-08-24 21:13:08 -07001532 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Integer());
Ian Rogersd81871c2011-10-03 13:57:23 -07001533 break;
1534 case 'S':
Andreas Gampead238ce2015-08-24 21:13:08 -07001535 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Short());
Ian Rogersd81871c2011-10-03 13:57:23 -07001536 break;
1537 case 'F':
Andreas Gampead238ce2015-08-24 21:13:08 -07001538 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Float());
Ian Rogersd81871c2011-10-03 13:57:23 -07001539 break;
1540 case 'J':
1541 case 'D': {
Andreas Gampe77cd4d62014-06-19 17:29:48 -07001542 if (cur_arg + 1 >= expected_args) {
1543 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1544 << " args, found more (" << descriptor << ")";
1545 return false;
1546 }
1547
Ian Rogers7b078e82014-09-10 14:44:24 -07001548 const RegType* lo_half;
1549 const RegType* hi_half;
1550 if (descriptor[0] == 'J') {
1551 lo_half = &reg_types_.LongLo();
1552 hi_half = &reg_types_.LongHi();
1553 } else {
1554 lo_half = &reg_types_.DoubleLo();
1555 hi_half = &reg_types_.DoubleHi();
1556 }
1557 reg_line->SetRegisterTypeWide(this, arg_start + cur_arg, *lo_half, *hi_half);
Ian Rogersd81871c2011-10-03 13:57:23 -07001558 cur_arg++;
1559 break;
1560 }
1561 default:
Brian Carlstrom93c33962013-07-26 10:37:43 -07001562 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected signature type char '"
1563 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001564 return false;
1565 }
1566 cur_arg++;
1567 }
1568 if (cur_arg != expected_args) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001569 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1570 << " arguments, found " << cur_arg;
Ian Rogersd81871c2011-10-03 13:57:23 -07001571 return false;
1572 }
1573 const char* descriptor = dex_file_->GetReturnTypeDescriptor(proto_id);
1574 // Validate return type. We don't do the type lookup; just want to make sure that it has the right
1575 // format. Only major difference from the method argument format is that 'V' is supported.
1576 bool result;
1577 if (IsPrimitiveDescriptor(descriptor[0]) || descriptor[0] == 'V') {
1578 result = descriptor[1] == '\0';
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001579 } else if (descriptor[0] == '[') { // single/multi-dimensional array of object/primitive
Ian Rogersd81871c2011-10-03 13:57:23 -07001580 size_t i = 0;
1581 do {
1582 i++;
1583 } while (descriptor[i] == '['); // process leading [
1584 if (descriptor[i] == 'L') { // object array
1585 do {
1586 i++; // find closing ;
1587 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1588 result = descriptor[i] == ';';
1589 } else { // primitive array
1590 result = IsPrimitiveDescriptor(descriptor[i]) && descriptor[i + 1] == '\0';
1591 }
1592 } else if (descriptor[0] == 'L') {
1593 // could be more thorough here, but shouldn't be required
1594 size_t i = 0;
1595 do {
1596 i++;
1597 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1598 result = descriptor[i] == ';';
1599 } else {
1600 result = false;
1601 }
1602 if (!result) {
jeffhaod5347e02012-03-22 17:25:05 -07001603 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected char in return type descriptor '"
1604 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001605 }
1606 return result;
jeffhaobdb76512011-09-07 11:43:16 -07001607}
1608
Ian Rogers776ac1f2012-04-13 23:36:36 -07001609bool MethodVerifier::CodeFlowVerifyMethod() {
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001610 const uint16_t* insns = code_item_accessor_.Insns();
1611 const uint32_t insns_size = code_item_accessor_.InsnsSizeInCodeUnits();
jeffhaoba5ebb92011-08-25 17:24:37 -07001612
jeffhaobdb76512011-09-07 11:43:16 -07001613 /* Begin by marking the first instruction as "changed". */
Mathieu Chartierde40d472015-10-15 17:47:48 -07001614 GetInstructionFlags(0).SetChanged();
Ian Rogersd81871c2011-10-03 13:57:23 -07001615 uint32_t start_guess = 0;
jeffhaoba5ebb92011-08-25 17:24:37 -07001616
jeffhaobdb76512011-09-07 11:43:16 -07001617 /* Continue until no instructions are marked "changed". */
1618 while (true) {
Mathieu Chartier4306ef82014-12-19 18:41:47 -08001619 if (allow_thread_suspension_) {
1620 self_->AllowThreadSuspension();
1621 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001622 // Find the first marked one. Use "start_guess" as a way to find one quickly.
1623 uint32_t insn_idx = start_guess;
1624 for (; insn_idx < insns_size; insn_idx++) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07001625 if (GetInstructionFlags(insn_idx).IsChanged())
jeffhaobdb76512011-09-07 11:43:16 -07001626 break;
1627 }
jeffhaobdb76512011-09-07 11:43:16 -07001628 if (insn_idx == insns_size) {
1629 if (start_guess != 0) {
1630 /* try again, starting from the top */
1631 start_guess = 0;
1632 continue;
1633 } else {
1634 /* all flags are clear */
1635 break;
1636 }
1637 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001638 // We carry the working set of registers from instruction to instruction. If this address can
1639 // be the target of a branch (or throw) instruction, or if we're skipping around chasing
1640 // "changed" flags, we need to load the set of registers from the table.
1641 // Because we always prefer to continue on to the next instruction, we should never have a
1642 // situation where we have a stray "changed" flag set on an instruction that isn't a branch
1643 // target.
1644 work_insn_idx_ = insn_idx;
Mathieu Chartierde40d472015-10-15 17:47:48 -07001645 if (GetInstructionFlags(insn_idx).IsBranchTarget()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001646 work_line_->CopyFromLine(reg_table_.GetLine(insn_idx));
Ian Rogersebbdd872014-07-07 23:53:08 -07001647 } else if (kIsDebugBuild) {
jeffhaobdb76512011-09-07 11:43:16 -07001648 /*
1649 * Sanity check: retrieve the stored register line (assuming
1650 * a full table) and make sure it actually matches.
1651 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001652 RegisterLine* register_line = reg_table_.GetLine(insn_idx);
Ian Rogers7b078e82014-09-10 14:44:24 -07001653 if (register_line != nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001654 if (work_line_->CompareLine(register_line) != 0) {
1655 Dump(std::cout);
1656 std::cout << info_messages_.str();
David Sehr709b0702016-10-13 09:12:37 -07001657 LOG(FATAL) << "work_line diverged in " << dex_file_->PrettyMethod(dex_method_idx_)
Elliott Hughesc073b072012-05-24 19:29:17 -07001658 << "@" << reinterpret_cast<void*>(work_insn_idx_) << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07001659 << " work_line=" << work_line_->Dump(this) << "\n"
1660 << " expected=" << register_line->Dump(this);
Ian Rogersd81871c2011-10-03 13:57:23 -07001661 }
jeffhaobdb76512011-09-07 11:43:16 -07001662 }
jeffhaobdb76512011-09-07 11:43:16 -07001663 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001664 if (!CodeFlowVerifyInstruction(&start_guess)) {
David Sehr709b0702016-10-13 09:12:37 -07001665 std::string prepend(dex_file_->PrettyMethod(dex_method_idx_));
Ian Rogersad0b3a32012-04-16 14:50:24 -07001666 prepend += " failed to verify: ";
1667 PrependToLastFailMessage(prepend);
jeffhaoba5ebb92011-08-25 17:24:37 -07001668 return false;
1669 }
jeffhaobdb76512011-09-07 11:43:16 -07001670 /* Clear "changed" and mark as visited. */
Mathieu Chartierde40d472015-10-15 17:47:48 -07001671 GetInstructionFlags(insn_idx).SetVisited();
1672 GetInstructionFlags(insn_idx).ClearChanged();
jeffhaobdb76512011-09-07 11:43:16 -07001673 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001674
Andreas Gampe92d77202017-12-06 20:49:00 -08001675 if (UNLIKELY(VLOG_IS_ON(verifier_debug))) {
jeffhaobdb76512011-09-07 11:43:16 -07001676 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001677 * Scan for dead code. There's nothing "evil" about dead code
jeffhaobdb76512011-09-07 11:43:16 -07001678 * (besides the wasted space), but it indicates a flaw somewhere
1679 * down the line, possibly in the verifier.
1680 *
1681 * If we've substituted "always throw" instructions into the stream,
1682 * we are almost certainly going to have some dead code.
1683 */
1684 int dead_start = -1;
Mathieu Chartier1d2d4ff2017-09-23 16:11:06 -07001685
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001686 for (const DexInstructionPcPair& inst : code_item_accessor_) {
Mathieu Chartier2b2bef22017-10-26 17:10:19 -07001687 const uint32_t insn_idx = inst.DexPc();
jeffhaobdb76512011-09-07 11:43:16 -07001688 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001689 * Switch-statement data doesn't get "visited" by scanner. It
jeffhaobdb76512011-09-07 11:43:16 -07001690 * may or may not be preceded by a padding NOP (for alignment).
1691 */
1692 if (insns[insn_idx] == Instruction::kPackedSwitchSignature ||
1693 insns[insn_idx] == Instruction::kSparseSwitchSignature ||
1694 insns[insn_idx] == Instruction::kArrayDataSignature ||
Elliott Hughes380aaa72012-07-09 14:33:15 -07001695 (insns[insn_idx] == Instruction::NOP && (insn_idx + 1 < insns_size) &&
jeffhaobdb76512011-09-07 11:43:16 -07001696 (insns[insn_idx + 1] == Instruction::kPackedSwitchSignature ||
1697 insns[insn_idx + 1] == Instruction::kSparseSwitchSignature ||
1698 insns[insn_idx + 1] == Instruction::kArrayDataSignature))) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07001699 GetInstructionFlags(insn_idx).SetVisited();
jeffhaobdb76512011-09-07 11:43:16 -07001700 }
1701
Mathieu Chartierde40d472015-10-15 17:47:48 -07001702 if (!GetInstructionFlags(insn_idx).IsVisited()) {
Mathieu Chartier1d2d4ff2017-09-23 16:11:06 -07001703 if (dead_start < 0) {
jeffhaobdb76512011-09-07 11:43:16 -07001704 dead_start = insn_idx;
Mathieu Chartier1d2d4ff2017-09-23 16:11:06 -07001705 }
jeffhaobdb76512011-09-07 11:43:16 -07001706 } else if (dead_start >= 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001707 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start)
1708 << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaobdb76512011-09-07 11:43:16 -07001709 dead_start = -1;
1710 }
1711 }
1712 if (dead_start >= 0) {
Mathieu Chartier1d2d4ff2017-09-23 16:11:06 -07001713 LogVerifyInfo()
1714 << "dead code " << reinterpret_cast<void*>(dead_start)
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001715 << "-" << reinterpret_cast<void*>(code_item_accessor_.InsnsSizeInCodeUnits() - 1);
jeffhaoba5ebb92011-08-25 17:24:37 -07001716 }
Ian Rogersc9e463c2013-06-05 16:52:26 -07001717 // To dump the state of the verify after a method, do something like:
David Sehr709b0702016-10-13 09:12:37 -07001718 // if (dex_file_->PrettyMethod(dex_method_idx_) ==
Ian Rogersc9e463c2013-06-05 16:52:26 -07001719 // "boolean java.lang.String.equals(java.lang.Object)") {
1720 // LOG(INFO) << info_messages_.str();
1721 // }
jeffhaoba5ebb92011-08-25 17:24:37 -07001722 }
jeffhaobdb76512011-09-07 11:43:16 -07001723 return true;
1724}
1725
Andreas Gampe68df3202015-06-22 11:35:46 -07001726// Returns the index of the first final instance field of the given class, or kDexNoIndex if there
1727// is no such field.
Andreas Gampea5b09a62016-11-17 15:21:22 -08001728static uint32_t GetFirstFinalInstanceFieldIndex(const DexFile& dex_file, dex::TypeIndex type_idx) {
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001729 const dex::ClassDef* class_def = dex_file.FindClassDef(type_idx);
Andreas Gampe68df3202015-06-22 11:35:46 -07001730 DCHECK(class_def != nullptr);
Mathieu Chartier1f1cb9f2018-06-04 09:22:46 -07001731 ClassAccessor accessor(dex_file, *class_def);
1732 for (const ClassAccessor::Field& field : accessor.GetInstanceFields()) {
1733 if (field.IsFinal()) {
1734 return field.GetIndex();
Andreas Gampe68df3202015-06-22 11:35:46 -07001735 }
Andreas Gampe68df3202015-06-22 11:35:46 -07001736 }
Andreas Gampee2abbc62017-09-15 11:59:26 -07001737 return dex::kDexNoIndex;
Andreas Gampe68df3202015-06-22 11:35:46 -07001738}
1739
Andreas Gampea727e372015-08-25 09:22:37 -07001740// Setup a register line for the given return instruction.
1741static void AdjustReturnLine(MethodVerifier* verifier,
1742 const Instruction* ret_inst,
1743 RegisterLine* line) {
1744 Instruction::Code opcode = ret_inst->Opcode();
1745
1746 switch (opcode) {
1747 case Instruction::RETURN_VOID:
1748 case Instruction::RETURN_VOID_NO_BARRIER:
1749 SafelyMarkAllRegistersAsConflicts(verifier, line);
1750 break;
1751
1752 case Instruction::RETURN:
1753 case Instruction::RETURN_OBJECT:
1754 line->MarkAllRegistersAsConflictsExcept(verifier, ret_inst->VRegA_11x());
1755 break;
1756
1757 case Instruction::RETURN_WIDE:
1758 line->MarkAllRegistersAsConflictsExceptWide(verifier, ret_inst->VRegA_11x());
1759 break;
1760
1761 default:
1762 LOG(FATAL) << "Unknown return opcode " << opcode;
1763 UNREACHABLE();
1764 }
1765}
1766
Ian Rogers776ac1f2012-04-13 23:36:36 -07001767bool MethodVerifier::CodeFlowVerifyInstruction(uint32_t* start_guess) {
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001768 // If we're doing FindLocksAtDexPc, check whether we're at the dex pc we care about.
1769 // We want the state _before_ the instruction, for the case where the dex pc we're
1770 // interested in is itself a monitor-enter instruction (which is a likely place
1771 // for a thread to be suspended).
Ian Rogers7b078e82014-09-10 14:44:24 -07001772 if (monitor_enter_dex_pcs_ != nullptr && work_insn_idx_ == interesting_dex_pc_) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001773 monitor_enter_dex_pcs_->clear(); // The new work line is more accurate than the previous one.
Andreas Gampeaaf0d382017-11-27 14:10:21 -08001774
1775 std::map<uint32_t, DexLockInfo> depth_to_lock_info;
1776 auto collector = [&](uint32_t dex_reg, uint32_t depth) {
1777 auto insert_pair = depth_to_lock_info.emplace(depth, DexLockInfo(depth));
1778 auto it = insert_pair.first;
1779 auto set_insert_pair = it->second.dex_registers.insert(dex_reg);
1780 DCHECK(set_insert_pair.second);
1781 };
1782 work_line_->IterateRegToLockDepths(collector);
1783 for (auto& pair : depth_to_lock_info) {
1784 monitor_enter_dex_pcs_->push_back(pair.second);
1785 // Map depth to dex PC.
1786 (*monitor_enter_dex_pcs_)[monitor_enter_dex_pcs_->size() - 1].dex_pc =
1787 work_line_->GetMonitorEnterDexPc(pair.second.dex_pc);
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001788 }
1789 }
1790
jeffhaobdb76512011-09-07 11:43:16 -07001791 /*
1792 * Once we finish decoding the instruction, we need to figure out where
jeffhaod1f0fde2011-09-08 17:25:33 -07001793 * we can go from here. There are three possible ways to transfer
jeffhaobdb76512011-09-07 11:43:16 -07001794 * control to another statement:
1795 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001796 * (1) Continue to the next instruction. Applies to all but
jeffhaobdb76512011-09-07 11:43:16 -07001797 * unconditional branches, method returns, and exception throws.
jeffhaod1f0fde2011-09-08 17:25:33 -07001798 * (2) Branch to one or more possible locations. Applies to branches
jeffhaobdb76512011-09-07 11:43:16 -07001799 * and switch statements.
jeffhaod1f0fde2011-09-08 17:25:33 -07001800 * (3) Exception handlers. Applies to any instruction that can
jeffhaobdb76512011-09-07 11:43:16 -07001801 * throw an exception that is handled by an encompassing "try"
1802 * block.
1803 *
1804 * We can also return, in which case there is no successor instruction
1805 * from this point.
1806 *
Elliott Hughesadb8c672012-03-06 16:49:32 -08001807 * The behavior can be determined from the opcode flags.
jeffhaobdb76512011-09-07 11:43:16 -07001808 */
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001809 const uint16_t* insns = code_item_accessor_.Insns() + work_insn_idx_;
Ian Rogersd81871c2011-10-03 13:57:23 -07001810 const Instruction* inst = Instruction::At(insns);
Ian Rogersa75a0132012-09-28 11:41:42 -07001811 int opcode_flags = Instruction::FlagsOf(inst->Opcode());
jeffhaobdb76512011-09-07 11:43:16 -07001812
jeffhaobdb76512011-09-07 11:43:16 -07001813 int32_t branch_target = 0;
jeffhaobdb76512011-09-07 11:43:16 -07001814 bool just_set_result = false;
Andreas Gampe92d77202017-12-06 20:49:00 -08001815 if (UNLIKELY(VLOG_IS_ON(verifier_debug))) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001816 // Generate processing back trace to debug verifier
Elliott Hughesc073b072012-05-24 19:29:17 -07001817 LogVerifyInfo() << "Processing " << inst->DumpString(dex_file_) << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07001818 << work_line_->Dump(this) << "\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001819 }
jeffhaobdb76512011-09-07 11:43:16 -07001820
1821 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001822 * Make a copy of the previous register state. If the instruction
jeffhaobdb76512011-09-07 11:43:16 -07001823 * can throw an exception, we will copy/merge this into the "catch"
1824 * address rather than work_line, because we don't want the result
1825 * from the "successful" code path (e.g. a check-cast that "improves"
1826 * a type) to be visible to the exception handler.
1827 */
Ian Rogers776ac1f2012-04-13 23:36:36 -07001828 if ((opcode_flags & Instruction::kThrow) != 0 && CurrentInsnFlags()->IsInTry()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001829 saved_line_->CopyFromLine(work_line_.get());
Ian Rogers1ff3c982014-08-12 02:30:58 -07001830 } else if (kIsDebugBuild) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001831 saved_line_->FillWithGarbage();
jeffhaobdb76512011-09-07 11:43:16 -07001832 }
Andreas Gamped12e7822015-06-25 10:26:40 -07001833 DCHECK(!have_pending_runtime_throw_failure_); // Per-instruction flag, should not be set here.
jeffhaobdb76512011-09-07 11:43:16 -07001834
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07001835
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001836 // We need to ensure the work line is consistent while performing validation. When we spot a
1837 // peephole pattern we compute a new line for either the fallthrough instruction or the
1838 // branch target.
Mathieu Chartier361e04a2016-02-16 14:06:35 -08001839 RegisterLineArenaUniquePtr branch_line;
1840 RegisterLineArenaUniquePtr fallthrough_line;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001841
Sebastien Hertz5243e912013-05-21 10:55:07 +02001842 switch (inst->Opcode()) {
jeffhaobdb76512011-09-07 11:43:16 -07001843 case Instruction::NOP:
1844 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001845 * A "pure" NOP has no effect on anything. Data tables start with
jeffhaobdb76512011-09-07 11:43:16 -07001846 * a signature that looks like a NOP; if we see one of these in
1847 * the course of executing code then we have a problem.
1848 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001849 if (inst->VRegA_10x() != 0) {
jeffhaod5347e02012-03-22 17:25:05 -07001850 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "encountered data table in instruction stream";
jeffhaobdb76512011-09-07 11:43:16 -07001851 }
1852 break;
1853
1854 case Instruction::MOVE:
Ian Rogers7b078e82014-09-10 14:44:24 -07001855 work_line_->CopyRegister1(this, inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategory1nr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001856 break;
jeffhaobdb76512011-09-07 11:43:16 -07001857 case Instruction::MOVE_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001858 work_line_->CopyRegister1(this, inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategory1nr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001859 break;
jeffhaobdb76512011-09-07 11:43:16 -07001860 case Instruction::MOVE_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001861 work_line_->CopyRegister1(this, inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategory1nr);
jeffhaobdb76512011-09-07 11:43:16 -07001862 break;
1863 case Instruction::MOVE_WIDE:
Ian Rogers7b078e82014-09-10 14:44:24 -07001864 work_line_->CopyRegister2(this, inst->VRegA_12x(), inst->VRegB_12x());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001865 break;
jeffhaobdb76512011-09-07 11:43:16 -07001866 case Instruction::MOVE_WIDE_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001867 work_line_->CopyRegister2(this, inst->VRegA_22x(), inst->VRegB_22x());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001868 break;
jeffhaobdb76512011-09-07 11:43:16 -07001869 case Instruction::MOVE_WIDE_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001870 work_line_->CopyRegister2(this, inst->VRegA_32x(), inst->VRegB_32x());
jeffhaobdb76512011-09-07 11:43:16 -07001871 break;
1872 case Instruction::MOVE_OBJECT:
Ian Rogers7b078e82014-09-10 14:44:24 -07001873 work_line_->CopyRegister1(this, inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategoryRef);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001874 break;
jeffhaobdb76512011-09-07 11:43:16 -07001875 case Instruction::MOVE_OBJECT_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001876 work_line_->CopyRegister1(this, inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategoryRef);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001877 break;
jeffhaobdb76512011-09-07 11:43:16 -07001878 case Instruction::MOVE_OBJECT_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001879 work_line_->CopyRegister1(this, inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategoryRef);
jeffhaobdb76512011-09-07 11:43:16 -07001880 break;
1881
1882 /*
1883 * The move-result instructions copy data out of a "pseudo-register"
jeffhaod1f0fde2011-09-08 17:25:33 -07001884 * with the results from the last method invocation. In practice we
jeffhaobdb76512011-09-07 11:43:16 -07001885 * might want to hold the result in an actual CPU register, so the
1886 * Dalvik spec requires that these only appear immediately after an
1887 * invoke or filled-new-array.
1888 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001889 * These calls invalidate the "result" register. (This is now
jeffhaobdb76512011-09-07 11:43:16 -07001890 * redundant with the reset done below, but it can make the debug info
1891 * easier to read in some cases.)
1892 */
1893 case Instruction::MOVE_RESULT:
Ian Rogers7b078e82014-09-10 14:44:24 -07001894 work_line_->CopyResultRegister1(this, inst->VRegA_11x(), false);
jeffhaobdb76512011-09-07 11:43:16 -07001895 break;
1896 case Instruction::MOVE_RESULT_WIDE:
Ian Rogers7b078e82014-09-10 14:44:24 -07001897 work_line_->CopyResultRegister2(this, inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07001898 break;
1899 case Instruction::MOVE_RESULT_OBJECT:
Ian Rogers7b078e82014-09-10 14:44:24 -07001900 work_line_->CopyResultRegister1(this, inst->VRegA_11x(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001901 break;
1902
Ian Rogersd81871c2011-10-03 13:57:23 -07001903 case Instruction::MOVE_EXCEPTION: {
Sebastien Hertz270a0e12015-01-16 19:49:09 +01001904 // We do not allow MOVE_EXCEPTION as the first instruction in a method. This is a simple case
1905 // where one entrypoint to the catch block is not actually an exception path.
1906 if (work_insn_idx_ == 0) {
1907 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "move-exception at pc 0x0";
1908 break;
1909 }
jeffhaobdb76512011-09-07 11:43:16 -07001910 /*
jeffhao60f83e32012-02-13 17:16:30 -08001911 * This statement can only appear as the first instruction in an exception handler. We verify
1912 * that as part of extracting the exception type from the catch block list.
jeffhaobdb76512011-09-07 11:43:16 -07001913 */
Ian Rogersd8f69b02014-09-10 21:43:52 +00001914 const RegType& res_type = GetCaughtExceptionType();
Andreas Gampead238ce2015-08-24 21:13:08 -07001915 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_11x(), res_type);
jeffhaobdb76512011-09-07 11:43:16 -07001916 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001917 }
jeffhaobdb76512011-09-07 11:43:16 -07001918 case Instruction::RETURN_VOID:
Andreas Gampef10b6e12015-08-12 10:48:12 -07001919 if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001920 if (!GetMethodReturnType().IsConflict()) {
jeffhaod5347e02012-03-22 17:25:05 -07001921 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001922 }
jeffhaobdb76512011-09-07 11:43:16 -07001923 }
1924 break;
1925 case Instruction::RETURN:
Andreas Gampef10b6e12015-08-12 10:48:12 -07001926 if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) {
jeffhaobdb76512011-09-07 11:43:16 -07001927 /* check the method signature */
Ian Rogersd8f69b02014-09-10 21:43:52 +00001928 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07001929 if (!return_type.IsCategory1Types()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001930 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected non-category 1 return type "
1931 << return_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001932 } else {
1933 // Compilers may generate synthetic functions that write byte values into boolean fields.
1934 // Also, it may use integer values for boolean, byte, short, and character return types.
Sebastien Hertz5243e912013-05-21 10:55:07 +02001935 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07001936 const RegType& src_type = work_line_->GetRegisterType(this, vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07001937 bool use_src = ((return_type.IsBoolean() && src_type.IsByte()) ||
1938 ((return_type.IsBoolean() || return_type.IsByte() ||
1939 return_type.IsShort() || return_type.IsChar()) &&
1940 src_type.IsInteger()));
1941 /* check the register contents */
Ian Rogersad0b3a32012-04-16 14:50:24 -07001942 bool success =
Ian Rogers7b078e82014-09-10 14:44:24 -07001943 work_line_->VerifyRegisterType(this, vregA, use_src ? src_type : return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001944 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001945 AppendToLastFailMessage(StringPrintf(" return-1nr on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07001946 }
jeffhaobdb76512011-09-07 11:43:16 -07001947 }
1948 }
1949 break;
1950 case Instruction::RETURN_WIDE:
Andreas Gampef10b6e12015-08-12 10:48:12 -07001951 if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) {
jeffhaobdb76512011-09-07 11:43:16 -07001952 /* check the method signature */
Ian Rogersd8f69b02014-09-10 21:43:52 +00001953 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07001954 if (!return_type.IsCategory2Types()) {
jeffhaod5347e02012-03-22 17:25:05 -07001955 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-wide not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001956 } else {
1957 /* check the register contents */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001958 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07001959 bool success = work_line_->VerifyRegisterType(this, vregA, return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001960 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001961 AppendToLastFailMessage(StringPrintf(" return-wide on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07001962 }
jeffhaobdb76512011-09-07 11:43:16 -07001963 }
1964 }
1965 break;
1966 case Instruction::RETURN_OBJECT:
Andreas Gampef10b6e12015-08-12 10:48:12 -07001967 if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) {
Ian Rogersd8f69b02014-09-10 21:43:52 +00001968 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07001969 if (!return_type.IsReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001970 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-object not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001971 } else {
1972 /* return_type is the *expected* return type, not register value */
Andreas Gampeeed3a5d2017-11-29 14:58:34 -08001973 DCHECK(!return_type.IsZeroOrNull());
Ian Rogersd81871c2011-10-03 13:57:23 -07001974 DCHECK(!return_type.IsUninitializedReference());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001975 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07001976 const RegType& reg_type = work_line_->GetRegisterType(this, vregA);
Andreas Gampea32210c2015-06-24 10:26:13 -07001977 // Disallow returning undefined, conflict & uninitialized values and verify that the
1978 // reference in vAA is an instance of the "return_type."
1979 if (reg_type.IsUndefined()) {
1980 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning undefined register";
1981 } else if (reg_type.IsConflict()) {
1982 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning register with conflict";
1983 } else if (reg_type.IsUninitializedTypes()) {
David Brazdil68b5c0b2016-01-19 14:25:29 +00001984 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning uninitialized object '"
Brian Carlstrom93c33962013-07-26 10:37:43 -07001985 << reg_type << "'";
Andreas Gampea4c98f22015-11-06 16:24:49 -08001986 } else if (!reg_type.IsReferenceTypes()) {
1987 // We really do expect a reference here.
1988 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-object returns a non-reference type "
1989 << reg_type;
David Brazdilca3c8c32016-09-06 14:04:48 +01001990 } else if (!return_type.IsAssignableFrom(reg_type, this)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07001991 if (reg_type.IsUnresolvedTypes() || return_type.IsUnresolvedTypes()) {
1992 Fail(VERIFY_ERROR_NO_CLASS) << " can't resolve returned type '" << return_type
1993 << "' or '" << reg_type << "'";
1994 } else {
Andreas Gampe16f149c2015-03-23 10:10:20 -07001995 bool soft_error = false;
1996 // Check whether arrays are involved. They will show a valid class status, even
1997 // if their components are erroneous.
1998 if (reg_type.IsArrayTypes() && return_type.IsArrayTypes()) {
David Brazdilca3c8c32016-09-06 14:04:48 +01001999 return_type.CanAssignArray(reg_type, reg_types_, class_loader_, this, &soft_error);
Andreas Gampe16f149c2015-03-23 10:10:20 -07002000 if (soft_error) {
2001 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "array with erroneous component type: "
2002 << reg_type << " vs " << return_type;
2003 }
2004 }
2005
2006 if (!soft_error) {
2007 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning '" << reg_type
2008 << "', but expected from declaration '" << return_type << "'";
2009 }
Jeff Haoa3faaf42013-09-03 19:07:00 -07002010 }
jeffhaobdb76512011-09-07 11:43:16 -07002011 }
2012 }
2013 }
2014 break;
2015
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07002016 /* could be boolean, int, float, or a null reference */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002017 case Instruction::CONST_4: {
2018 int32_t val = static_cast<int32_t>(inst->VRegB_11n() << 28) >> 28;
Andreas Gampead238ce2015-08-24 21:13:08 -07002019 work_line_->SetRegisterType<LockOp::kClear>(
2020 this, inst->VRegA_11n(), DetermineCat1Constant(val, need_precise_constants_));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07002021 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02002022 }
2023 case Instruction::CONST_16: {
2024 int16_t val = static_cast<int16_t>(inst->VRegB_21s());
Andreas Gampead238ce2015-08-24 21:13:08 -07002025 work_line_->SetRegisterType<LockOp::kClear>(
2026 this, inst->VRegA_21s(), DetermineCat1Constant(val, need_precise_constants_));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07002027 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02002028 }
Sebastien Hertz849600b2013-12-20 10:28:08 +01002029 case Instruction::CONST: {
2030 int32_t val = inst->VRegB_31i();
Andreas Gampead238ce2015-08-24 21:13:08 -07002031 work_line_->SetRegisterType<LockOp::kClear>(
2032 this, inst->VRegA_31i(), DetermineCat1Constant(val, need_precise_constants_));
jeffhaobdb76512011-09-07 11:43:16 -07002033 break;
Sebastien Hertz849600b2013-12-20 10:28:08 +01002034 }
2035 case Instruction::CONST_HIGH16: {
2036 int32_t val = static_cast<int32_t>(inst->VRegB_21h() << 16);
Andreas Gampead238ce2015-08-24 21:13:08 -07002037 work_line_->SetRegisterType<LockOp::kClear>(
2038 this, inst->VRegA_21h(), DetermineCat1Constant(val, need_precise_constants_));
jeffhaobdb76512011-09-07 11:43:16 -07002039 break;
Sebastien Hertz849600b2013-12-20 10:28:08 +01002040 }
jeffhaobdb76512011-09-07 11:43:16 -07002041 /* could be long or double; resolved upon use */
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002042 case Instruction::CONST_WIDE_16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002043 int64_t val = static_cast<int16_t>(inst->VRegB_21s());
Ian Rogersd8f69b02014-09-10 21:43:52 +00002044 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
2045 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07002046 work_line_->SetRegisterTypeWide(this, inst->VRegA_21s(), lo, hi);
jeffhaobdb76512011-09-07 11:43:16 -07002047 break;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002048 }
2049 case Instruction::CONST_WIDE_32: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002050 int64_t val = static_cast<int32_t>(inst->VRegB_31i());
Ian Rogersd8f69b02014-09-10 21:43:52 +00002051 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
2052 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07002053 work_line_->SetRegisterTypeWide(this, inst->VRegA_31i(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002054 break;
2055 }
2056 case Instruction::CONST_WIDE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002057 int64_t val = inst->VRegB_51l();
Ian Rogersd8f69b02014-09-10 21:43:52 +00002058 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
2059 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07002060 work_line_->SetRegisterTypeWide(this, inst->VRegA_51l(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002061 break;
2062 }
2063 case Instruction::CONST_WIDE_HIGH16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002064 int64_t val = static_cast<uint64_t>(inst->VRegB_21h()) << 48;
Ian Rogersd8f69b02014-09-10 21:43:52 +00002065 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
2066 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07002067 work_line_->SetRegisterTypeWide(this, inst->VRegA_21h(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002068 break;
2069 }
jeffhaobdb76512011-09-07 11:43:16 -07002070 case Instruction::CONST_STRING:
Andreas Gampead238ce2015-08-24 21:13:08 -07002071 work_line_->SetRegisterType<LockOp::kClear>(
2072 this, inst->VRegA_21c(), reg_types_.JavaLangString());
Sebastien Hertz5243e912013-05-21 10:55:07 +02002073 break;
jeffhaobdb76512011-09-07 11:43:16 -07002074 case Instruction::CONST_STRING_JUMBO:
Andreas Gampead238ce2015-08-24 21:13:08 -07002075 work_line_->SetRegisterType<LockOp::kClear>(
2076 this, inst->VRegA_31c(), reg_types_.JavaLangString());
jeffhaobdb76512011-09-07 11:43:16 -07002077 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002078 case Instruction::CONST_CLASS: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002079 // Get type from instruction if unresolved then we need an access check
2080 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Andreas Gampe98be1a92017-08-28 08:25:45 -07002081 const RegType& res_type = ResolveClass<CheckAccess::kYes>(dex::TypeIndex(inst->VRegB_21c()));
Ian Rogersad0b3a32012-04-16 14:50:24 -07002082 // Register holds class, ie its type is class, on error it will hold Conflict.
Andreas Gampead238ce2015-08-24 21:13:08 -07002083 work_line_->SetRegisterType<LockOp::kClear>(
2084 this, inst->VRegA_21c(), res_type.IsConflict() ? res_type
2085 : reg_types_.JavaLangClass());
jeffhaobdb76512011-09-07 11:43:16 -07002086 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002087 }
Orion Hodson2e599942017-09-22 16:17:41 +01002088 case Instruction::CONST_METHOD_HANDLE:
2089 work_line_->SetRegisterType<LockOp::kClear>(
2090 this, inst->VRegA_21c(), reg_types_.JavaLangInvokeMethodHandle());
Orion Hodson2e599942017-09-22 16:17:41 +01002091 break;
2092 case Instruction::CONST_METHOD_TYPE:
2093 work_line_->SetRegisterType<LockOp::kClear>(
2094 this, inst->VRegA_21c(), reg_types_.JavaLangInvokeMethodType());
Orion Hodson2e599942017-09-22 16:17:41 +01002095 break;
jeffhaobdb76512011-09-07 11:43:16 -07002096 case Instruction::MONITOR_ENTER:
Ian Rogers7b078e82014-09-10 14:44:24 -07002097 work_line_->PushMonitor(this, inst->VRegA_11x(), work_insn_idx_);
Andreas Gampec1474102015-08-18 08:57:44 -07002098 // Check whether the previous instruction is a move-object with vAA as a source, creating
2099 // untracked lock aliasing.
Mathieu Chartierde40d472015-10-15 17:47:48 -07002100 if (0 != work_insn_idx_ && !GetInstructionFlags(work_insn_idx_).IsBranchTarget()) {
Andreas Gampec1474102015-08-18 08:57:44 -07002101 uint32_t prev_idx = work_insn_idx_ - 1;
Mathieu Chartierde40d472015-10-15 17:47:48 -07002102 while (0 != prev_idx && !GetInstructionFlags(prev_idx).IsOpcode()) {
Andreas Gampec1474102015-08-18 08:57:44 -07002103 prev_idx--;
2104 }
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08002105 const Instruction& prev_inst = code_item_accessor_.InstructionAt(prev_idx);
Mathieu Chartier1d2d4ff2017-09-23 16:11:06 -07002106 switch (prev_inst.Opcode()) {
Andreas Gampec1474102015-08-18 08:57:44 -07002107 case Instruction::MOVE_OBJECT:
2108 case Instruction::MOVE_OBJECT_16:
2109 case Instruction::MOVE_OBJECT_FROM16:
Mathieu Chartier1d2d4ff2017-09-23 16:11:06 -07002110 if (prev_inst.VRegB() == inst->VRegA_11x()) {
Andreas Gampec1474102015-08-18 08:57:44 -07002111 // Redo the copy. This won't change the register types, but update the lock status
2112 // for the aliased register.
2113 work_line_->CopyRegister1(this,
Mathieu Chartier1d2d4ff2017-09-23 16:11:06 -07002114 prev_inst.VRegA(),
2115 prev_inst.VRegB(),
Andreas Gampec1474102015-08-18 08:57:44 -07002116 kTypeCategoryRef);
2117 }
2118 break;
2119
Alexey Grebenkince750492018-05-31 23:42:20 +03002120 // Catch a case of register aliasing when two registers are linked to the same
2121 // java.lang.Class object via two consequent const-class instructions immediately
2122 // preceding monitor-enter called on one of those registers.
2123 case Instruction::CONST_CLASS: {
2124 // Get the second previous instruction.
2125 if (prev_idx == 0 || GetInstructionFlags(prev_idx).IsBranchTarget()) {
2126 break;
2127 }
2128 prev_idx--;
2129 while (0 != prev_idx && !GetInstructionFlags(prev_idx).IsOpcode()) {
2130 prev_idx--;
2131 }
2132 const Instruction& prev2_inst = code_item_accessor_.InstructionAt(prev_idx);
2133
2134 // Match the pattern "const-class; const-class; monitor-enter;"
2135 if (prev2_inst.Opcode() != Instruction::CONST_CLASS) {
2136 break;
2137 }
2138
2139 // Ensure both const-classes are called for the same type_idx.
2140 if (prev_inst.VRegB_21c() != prev2_inst.VRegB_21c()) {
2141 break;
2142 }
2143
2144 // Update the lock status for the aliased register.
2145 if (prev_inst.VRegA() == inst->VRegA_11x()) {
2146 work_line_->CopyRegister1(this,
2147 prev2_inst.VRegA(),
2148 inst->VRegA_11x(),
2149 kTypeCategoryRef);
2150 } else if (prev2_inst.VRegA() == inst->VRegA_11x()) {
2151 work_line_->CopyRegister1(this,
2152 prev_inst.VRegA(),
2153 inst->VRegA_11x(),
2154 kTypeCategoryRef);
2155 }
2156 break;
2157 }
2158
Andreas Gampec1474102015-08-18 08:57:44 -07002159 default: // Other instruction types ignored.
2160 break;
2161 }
2162 }
jeffhaobdb76512011-09-07 11:43:16 -07002163 break;
2164 case Instruction::MONITOR_EXIT:
2165 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002166 * monitor-exit instructions are odd. They can throw exceptions,
jeffhaobdb76512011-09-07 11:43:16 -07002167 * but when they do they act as if they succeeded and the PC is
jeffhaod1f0fde2011-09-08 17:25:33 -07002168 * pointing to the following instruction. (This behavior goes back
jeffhaobdb76512011-09-07 11:43:16 -07002169 * to the need to handle asynchronous exceptions, a now-deprecated
2170 * feature that Dalvik doesn't support.)
2171 *
jeffhaod1f0fde2011-09-08 17:25:33 -07002172 * In practice we don't need to worry about this. The only
jeffhaobdb76512011-09-07 11:43:16 -07002173 * exceptions that can be thrown from monitor-exit are for a
jeffhaod1f0fde2011-09-08 17:25:33 -07002174 * null reference and -exit without a matching -enter. If the
jeffhaobdb76512011-09-07 11:43:16 -07002175 * structured locking checks are working, the former would have
2176 * failed on the -enter instruction, and the latter is impossible.
2177 *
2178 * This is fortunate, because issue 3221411 prevents us from
2179 * chasing the "can throw" path when monitor verification is
jeffhaod1f0fde2011-09-08 17:25:33 -07002180 * enabled. If we can fully verify the locking we can ignore
jeffhaobdb76512011-09-07 11:43:16 -07002181 * some catch blocks (which will show up as "dead" code when
2182 * we skip them here); if we can't, then the code path could be
2183 * "live" so we still need to check it.
2184 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002185 opcode_flags &= ~Instruction::kThrow;
Ian Rogers7b078e82014-09-10 14:44:24 -07002186 work_line_->PopMonitor(this, inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07002187 break;
Ian Rogers28ad40d2011-10-27 15:19:26 -07002188 case Instruction::CHECK_CAST:
Ian Rogersd81871c2011-10-03 13:57:23 -07002189 case Instruction::INSTANCE_OF: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002190 /*
2191 * If this instruction succeeds, we will "downcast" register vA to the type in vB. (This
2192 * could be a "upcast" -- not expected, so we don't try to address it.)
2193 *
2194 * If it fails, an exception is thrown, which we deal with later by ignoring the update to
Elliott Hughesadb8c672012-03-06 16:49:32 -08002195 * dec_insn.vA when branching to a handler.
Ian Rogers28ad40d2011-10-27 15:19:26 -07002196 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002197 const bool is_checkcast = (inst->Opcode() == Instruction::CHECK_CAST);
Andreas Gampea5b09a62016-11-17 15:21:22 -08002198 const dex::TypeIndex type_idx((is_checkcast) ? inst->VRegB_21c() : inst->VRegC_22c());
Andreas Gampe98be1a92017-08-28 08:25:45 -07002199 const RegType& res_type = ResolveClass<CheckAccess::kYes>(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002200 if (res_type.IsConflict()) {
Andreas Gampe00633eb2014-07-17 16:13:35 -07002201 // If this is a primitive type, fail HARD.
Vladimir Marko666ee3d2017-12-11 18:37:36 +00002202 ObjPtr<mirror::Class> klass = Runtime::Current()->GetClassLinker()->LookupResolvedType(
2203 type_idx, dex_cache_.Get(), class_loader_.Get());
Andreas Gampe00633eb2014-07-17 16:13:35 -07002204 if (klass != nullptr && klass->IsPrimitive()) {
2205 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "using primitive type "
2206 << dex_file_->StringByTypeIdx(type_idx) << " in instanceof in "
2207 << GetDeclaringClass();
2208 break;
2209 }
2210
Ian Rogersad0b3a32012-04-16 14:50:24 -07002211 DCHECK_NE(failures_.size(), 0U);
2212 if (!is_checkcast) {
Andreas Gampead238ce2015-08-24 21:13:08 -07002213 work_line_->SetRegisterType<LockOp::kClear>(this,
2214 inst->VRegA_22c(),
2215 reg_types_.Boolean());
Ian Rogersad0b3a32012-04-16 14:50:24 -07002216 }
2217 break; // bad class
Ian Rogers9f1ab122011-12-12 08:52:43 -08002218 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002219 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Sebastien Hertz5243e912013-05-21 10:55:07 +02002220 uint32_t orig_type_reg = (is_checkcast) ? inst->VRegA_21c() : inst->VRegB_22c();
Ian Rogers7b078e82014-09-10 14:44:24 -07002221 const RegType& orig_type = work_line_->GetRegisterType(this, orig_type_reg);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002222 if (!res_type.IsNonZeroReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002223 if (is_checkcast) {
2224 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on unexpected class " << res_type;
2225 } else {
2226 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on unexpected class " << res_type;
2227 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002228 } else if (!orig_type.IsReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002229 if (is_checkcast) {
2230 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on non-reference in v" << orig_type_reg;
2231 } else {
2232 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on non-reference in v" << orig_type_reg;
2233 }
David Brazdil68b5c0b2016-01-19 14:25:29 +00002234 } else if (orig_type.IsUninitializedTypes()) {
2235 if (is_checkcast) {
2236 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on uninitialized reference in v"
2237 << orig_type_reg;
2238 } else {
2239 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on uninitialized reference in v"
2240 << orig_type_reg;
2241 }
jeffhao2a8a90e2011-09-26 14:25:31 -07002242 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002243 if (is_checkcast) {
Andreas Gampead238ce2015-08-24 21:13:08 -07002244 work_line_->SetRegisterType<LockOp::kKeep>(this, inst->VRegA_21c(), res_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07002245 } else {
Andreas Gampead238ce2015-08-24 21:13:08 -07002246 work_line_->SetRegisterType<LockOp::kClear>(this,
2247 inst->VRegA_22c(),
2248 reg_types_.Boolean());
jeffhaobdb76512011-09-07 11:43:16 -07002249 }
jeffhaobdb76512011-09-07 11:43:16 -07002250 }
jeffhao2a8a90e2011-09-26 14:25:31 -07002251 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002252 }
2253 case Instruction::ARRAY_LENGTH: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002254 const RegType& res_type = work_line_->GetRegisterType(this, inst->VRegB_12x());
Ian Rogers28ad40d2011-10-27 15:19:26 -07002255 if (res_type.IsReferenceTypes()) {
Andreas Gampeeed3a5d2017-11-29 14:58:34 -08002256 if (!res_type.IsArrayTypes() && !res_type.IsZeroOrNull()) {
2257 // ie not an array or null
jeffhaod5347e02012-03-22 17:25:05 -07002258 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002259 } else {
Andreas Gampead238ce2015-08-24 21:13:08 -07002260 work_line_->SetRegisterType<LockOp::kClear>(this,
2261 inst->VRegA_12x(),
2262 reg_types_.Integer());
Ian Rogersd81871c2011-10-03 13:57:23 -07002263 }
Andreas Gampe65c9db82014-07-28 13:14:34 -07002264 } else {
2265 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002266 }
2267 break;
2268 }
2269 case Instruction::NEW_INSTANCE: {
Andreas Gampe98be1a92017-08-28 08:25:45 -07002270 const RegType& res_type = ResolveClass<CheckAccess::kYes>(dex::TypeIndex(inst->VRegB_21c()));
Ian Rogersad0b3a32012-04-16 14:50:24 -07002271 if (res_type.IsConflict()) {
2272 DCHECK_NE(failures_.size(), 0U);
2273 break; // bad class
jeffhao8cd6dda2012-02-22 10:15:34 -08002274 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002275 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
2276 // can't create an instance of an interface or abstract class */
2277 if (!res_type.IsInstantiableTypes()) {
2278 Fail(VERIFY_ERROR_INSTANTIATION)
2279 << "new-instance on primitive, interface or abstract class" << res_type;
Ian Rogers08f753d2012-08-24 14:35:25 -07002280 // Soft failure so carry on to set register type.
Ian Rogersd81871c2011-10-03 13:57:23 -07002281 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00002282 const RegType& uninit_type = reg_types_.Uninitialized(res_type, work_insn_idx_);
Ian Rogers08f753d2012-08-24 14:35:25 -07002283 // Any registers holding previous allocations from this address that have not yet been
2284 // initialized must be marked invalid.
Ian Rogers7b078e82014-09-10 14:44:24 -07002285 work_line_->MarkUninitRefsAsInvalid(this, uninit_type);
Ian Rogers08f753d2012-08-24 14:35:25 -07002286 // add the new uninitialized reference to the register state
Andreas Gampead238ce2015-08-24 21:13:08 -07002287 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_21c(), uninit_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07002288 break;
2289 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08002290 case Instruction::NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002291 VerifyNewArray(inst, false, false);
jeffhaobdb76512011-09-07 11:43:16 -07002292 break;
2293 case Instruction::FILLED_NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002294 VerifyNewArray(inst, true, false);
Ian Rogers0c4a5062012-02-03 15:18:59 -08002295 just_set_result = true; // Filled new array sets result register
jeffhaobdb76512011-09-07 11:43:16 -07002296 break;
Ian Rogers0c4a5062012-02-03 15:18:59 -08002297 case Instruction::FILLED_NEW_ARRAY_RANGE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002298 VerifyNewArray(inst, true, true);
Ian Rogers0c4a5062012-02-03 15:18:59 -08002299 just_set_result = true; // Filled new array range sets result register
2300 break;
jeffhaobdb76512011-09-07 11:43:16 -07002301 case Instruction::CMPL_FLOAT:
2302 case Instruction::CMPG_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002303 if (!work_line_->VerifyRegisterType(this, inst->VRegB_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08002304 break;
2305 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002306 if (!work_line_->VerifyRegisterType(this, inst->VRegC_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08002307 break;
2308 }
Andreas Gampead238ce2015-08-24 21:13:08 -07002309 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002310 break;
2311 case Instruction::CMPL_DOUBLE:
2312 case Instruction::CMPG_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002313 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegB_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002314 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002315 break;
2316 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002317 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegC_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002318 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002319 break;
2320 }
Andreas Gampead238ce2015-08-24 21:13:08 -07002321 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002322 break;
2323 case Instruction::CMP_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002324 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegB_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002325 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002326 break;
2327 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002328 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegC_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002329 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002330 break;
2331 }
Andreas Gampead238ce2015-08-24 21:13:08 -07002332 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002333 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002334 case Instruction::THROW: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002335 const RegType& res_type = work_line_->GetRegisterType(this, inst->VRegA_11x());
David Brazdilca3c8c32016-09-06 14:04:48 +01002336 if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(res_type, this)) {
David Brazdil68b5c0b2016-01-19 14:25:29 +00002337 if (res_type.IsUninitializedTypes()) {
2338 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "thrown exception not initialized";
Pavel Vyssotski980027c2016-02-11 20:28:11 +06002339 } else if (!res_type.IsReferenceTypes()) {
2340 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "thrown value of non-reference type " << res_type;
David Brazdil68b5c0b2016-01-19 14:25:29 +00002341 } else {
2342 Fail(res_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS : VERIFY_ERROR_BAD_CLASS_SOFT)
2343 << "thrown class " << res_type << " not instanceof Throwable";
2344 }
jeffhaobdb76512011-09-07 11:43:16 -07002345 }
2346 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002347 }
jeffhaobdb76512011-09-07 11:43:16 -07002348 case Instruction::GOTO:
2349 case Instruction::GOTO_16:
2350 case Instruction::GOTO_32:
2351 /* no effect on or use of registers */
2352 break;
2353
2354 case Instruction::PACKED_SWITCH:
2355 case Instruction::SPARSE_SWITCH:
2356 /* verify that vAA is an integer, or can be converted to one */
Ian Rogers7b078e82014-09-10 14:44:24 -07002357 work_line_->VerifyRegisterType(this, inst->VRegA_31t(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002358 break;
2359
Ian Rogersd81871c2011-10-03 13:57:23 -07002360 case Instruction::FILL_ARRAY_DATA: {
2361 /* Similar to the verification done for APUT */
Ian Rogers7b078e82014-09-10 14:44:24 -07002362 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegA_31t());
Ian Rogers89310de2012-02-01 13:47:30 -08002363 /* array_type can be null if the reg type is Zero */
Andreas Gampeeed3a5d2017-11-29 14:58:34 -08002364 if (!array_type.IsZeroOrNull()) {
jeffhao457cc512012-02-02 16:55:13 -08002365 if (!array_type.IsArrayTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002366 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with array type "
2367 << array_type;
Andreas Gampebb18a032016-03-22 20:34:25 -07002368 } else if (array_type.IsUnresolvedTypes()) {
2369 // If it's an unresolved array type, it must be non-primitive.
2370 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data for array of type "
2371 << array_type;
Ian Rogers89310de2012-02-01 13:47:30 -08002372 } else {
Ian Rogersd8f69b02014-09-10 21:43:52 +00002373 const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
Ian Rogersad0b3a32012-04-16 14:50:24 -07002374 DCHECK(!component_type.IsConflict());
jeffhao457cc512012-02-02 16:55:13 -08002375 if (component_type.IsNonZeroReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002376 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with component type "
2377 << component_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002378 } else {
jeffhao457cc512012-02-02 16:55:13 -08002379 // Now verify if the element width in the table matches the element width declared in
2380 // the array
Andreas Gampe53de99c2015-08-17 13:43:55 -07002381 const uint16_t* array_data =
2382 insns + (insns[1] | (static_cast<int32_t>(insns[2]) << 16));
jeffhao457cc512012-02-02 16:55:13 -08002383 if (array_data[0] != Instruction::kArrayDataSignature) {
jeffhaod5347e02012-03-22 17:25:05 -07002384 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid magic for array-data";
jeffhao457cc512012-02-02 16:55:13 -08002385 } else {
2386 size_t elem_width = Primitive::ComponentSize(component_type.GetPrimitiveType());
2387 // Since we don't compress the data in Dex, expect to see equal width of data stored
2388 // in the table and expected from the array class.
2389 if (array_data[1] != elem_width) {
jeffhaod5347e02012-03-22 17:25:05 -07002390 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-data size mismatch (" << array_data[1]
2391 << " vs " << elem_width << ")";
jeffhao457cc512012-02-02 16:55:13 -08002392 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002393 }
2394 }
jeffhaobdb76512011-09-07 11:43:16 -07002395 }
2396 }
2397 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002398 }
jeffhaobdb76512011-09-07 11:43:16 -07002399 case Instruction::IF_EQ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002400 case Instruction::IF_NE: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002401 const RegType& reg_type1 = work_line_->GetRegisterType(this, inst->VRegA_22t());
2402 const RegType& reg_type2 = work_line_->GetRegisterType(this, inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002403 bool mismatch = false;
Andreas Gampeeed3a5d2017-11-29 14:58:34 -08002404 if (reg_type1.IsZeroOrNull()) { // zero then integral or reference expected
Ian Rogersd81871c2011-10-03 13:57:23 -07002405 mismatch = !reg_type2.IsReferenceTypes() && !reg_type2.IsIntegralTypes();
2406 } else if (reg_type1.IsReferenceTypes()) { // both references?
2407 mismatch = !reg_type2.IsReferenceTypes();
2408 } else { // both integral?
2409 mismatch = !reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes();
2410 }
2411 if (mismatch) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002412 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to if-eq/if-ne (" << reg_type1 << ","
2413 << reg_type2 << ") must both be references or integral";
jeffhaobdb76512011-09-07 11:43:16 -07002414 }
2415 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002416 }
jeffhaobdb76512011-09-07 11:43:16 -07002417 case Instruction::IF_LT:
2418 case Instruction::IF_GE:
2419 case Instruction::IF_GT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002420 case Instruction::IF_LE: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002421 const RegType& reg_type1 = work_line_->GetRegisterType(this, inst->VRegA_22t());
2422 const RegType& reg_type2 = work_line_->GetRegisterType(this, inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002423 if (!reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002424 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to 'if' (" << reg_type1 << ","
2425 << reg_type2 << ") must be integral";
jeffhaobdb76512011-09-07 11:43:16 -07002426 }
2427 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002428 }
jeffhaobdb76512011-09-07 11:43:16 -07002429 case Instruction::IF_EQZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002430 case Instruction::IF_NEZ: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002431 const RegType& reg_type = work_line_->GetRegisterType(this, inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002432 if (!reg_type.IsReferenceTypes() && !reg_type.IsIntegralTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002433 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
2434 << " unexpected as arg to if-eqz/if-nez";
Ian Rogersd81871c2011-10-03 13:57:23 -07002435 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002436
2437 // Find previous instruction - its existence is a precondition to peephole optimization.
Ian Rogers9b360392013-06-06 14:45:07 -07002438 uint32_t instance_of_idx = 0;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002439 if (0 != work_insn_idx_) {
Ian Rogers9b360392013-06-06 14:45:07 -07002440 instance_of_idx = work_insn_idx_ - 1;
Mathieu Chartierde40d472015-10-15 17:47:48 -07002441 while (0 != instance_of_idx && !GetInstructionFlags(instance_of_idx).IsOpcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07002442 instance_of_idx--;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002443 }
Mathieu Chartierc2a2bda2018-03-12 11:21:52 -07002444 if (FailOrAbort(GetInstructionFlags(instance_of_idx).IsOpcode(),
Andreas Gampe7c038102014-10-27 20:08:46 -07002445 "Unable to get previous instruction of if-eqz/if-nez for work index ",
2446 work_insn_idx_)) {
2447 break;
2448 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002449 } else {
2450 break;
2451 }
2452
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08002453 const Instruction& instance_of_inst = code_item_accessor_.InstructionAt(instance_of_idx);
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002454
2455 /* Check for peep-hole pattern of:
2456 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002457 * instance-of vX, vY, T;
2458 * ifXXX vX, label ;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002459 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002460 * label:
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002461 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002462 * and sharpen the type of vY to be type T.
2463 * Note, this pattern can't be if:
2464 * - if there are other branches to this branch,
2465 * - when vX == vY.
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002466 */
Ian Rogersfae370a2013-06-05 08:33:27 -07002467 if (!CurrentInsnFlags()->IsBranchTarget() &&
Vladimir Markod7559b72017-09-28 13:50:37 +01002468 (Instruction::INSTANCE_OF == instance_of_inst.Opcode()) &&
2469 (inst->VRegA_21t() == instance_of_inst.VRegA_22c()) &&
2470 (instance_of_inst.VRegA_22c() != instance_of_inst.VRegB_22c())) {
Ian Rogersebbdd872014-07-07 23:53:08 -07002471 // Check the type of the instance-of is different than that of registers type, as if they
2472 // are the same there is no work to be done here. Check that the conversion is not to or
2473 // from an unresolved type as type information is imprecise. If the instance-of is to an
2474 // interface then ignore the type information as interfaces can only be treated as Objects
2475 // and we don't want to disallow field and other operations on the object. If the value
2476 // being instance-of checked against is known null (zero) then allow the optimization as
2477 // we didn't have type information. If the merge of the instance-of type with the original
2478 // type is assignable to the original then allow optimization. This check is performed to
2479 // ensure that subsequent merges don't lose type information - such as becoming an
2480 // interface from a class that would lose information relevant to field checks.
Vladimir Markod7559b72017-09-28 13:50:37 +01002481 const RegType& orig_type = work_line_->GetRegisterType(this, instance_of_inst.VRegB_22c());
Andreas Gampe98be1a92017-08-28 08:25:45 -07002482 const RegType& cast_type = ResolveClass<CheckAccess::kYes>(
Vladimir Markod7559b72017-09-28 13:50:37 +01002483 dex::TypeIndex(instance_of_inst.VRegC_22c()));
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002484
Ian Rogersebbdd872014-07-07 23:53:08 -07002485 if (!orig_type.Equals(cast_type) &&
2486 !cast_type.IsUnresolvedTypes() && !orig_type.IsUnresolvedTypes() &&
Andreas Gampe00633eb2014-07-17 16:13:35 -07002487 cast_type.HasClass() && // Could be conflict type, make sure it has a class.
Ian Rogersebbdd872014-07-07 23:53:08 -07002488 !cast_type.GetClass()->IsInterface() &&
Andreas Gampeeed3a5d2017-11-29 14:58:34 -08002489 (orig_type.IsZeroOrNull() ||
David Brazdilca3c8c32016-09-06 14:04:48 +01002490 orig_type.IsStrictlyAssignableFrom(
2491 cast_type.Merge(orig_type, &reg_types_, this), this))) {
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08002492 RegisterLine* update_line = RegisterLine::Create(code_item_accessor_.RegistersSize(),
2493 this);
Ian Rogersfae370a2013-06-05 08:33:27 -07002494 if (inst->Opcode() == Instruction::IF_EQZ) {
Ian Rogers9b360392013-06-06 14:45:07 -07002495 fallthrough_line.reset(update_line);
Ian Rogersfae370a2013-06-05 08:33:27 -07002496 } else {
Ian Rogers9b360392013-06-06 14:45:07 -07002497 branch_line.reset(update_line);
2498 }
2499 update_line->CopyFromLine(work_line_.get());
Andreas Gampead238ce2015-08-24 21:13:08 -07002500 update_line->SetRegisterType<LockOp::kKeep>(this,
Vladimir Markod7559b72017-09-28 13:50:37 +01002501 instance_of_inst.VRegB_22c(),
Andreas Gampead238ce2015-08-24 21:13:08 -07002502 cast_type);
Mathieu Chartierde40d472015-10-15 17:47:48 -07002503 if (!GetInstructionFlags(instance_of_idx).IsBranchTarget() && 0 != instance_of_idx) {
Ian Rogers9b360392013-06-06 14:45:07 -07002504 // See if instance-of was preceded by a move-object operation, common due to the small
2505 // register encoding space of instance-of, and propagate type information to the source
2506 // of the move-object.
Andreas Gampe43e43252019-01-08 12:06:57 -08002507 // Note: this is only valid if the move source was not clobbered.
Ian Rogers9b360392013-06-06 14:45:07 -07002508 uint32_t move_idx = instance_of_idx - 1;
Mathieu Chartierde40d472015-10-15 17:47:48 -07002509 while (0 != move_idx && !GetInstructionFlags(move_idx).IsOpcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07002510 move_idx--;
2511 }
Mathieu Chartierc2a2bda2018-03-12 11:21:52 -07002512 if (FailOrAbort(GetInstructionFlags(move_idx).IsOpcode(),
Andreas Gampe7c038102014-10-27 20:08:46 -07002513 "Unable to get previous instruction of if-eqz/if-nez for work index ",
2514 work_insn_idx_)) {
2515 break;
2516 }
Andreas Gampe43e43252019-01-08 12:06:57 -08002517 auto maybe_update_fn = [&instance_of_inst, update_line, this, &cast_type](
2518 uint16_t move_src,
2519 uint16_t move_trg)
2520 REQUIRES_SHARED(Locks::mutator_lock_) {
2521 if (move_trg == instance_of_inst.VRegB_22c() &&
2522 move_src != instance_of_inst.VRegA_22c()) {
2523 update_line->SetRegisterType<LockOp::kKeep>(this, move_src, cast_type);
2524 }
2525 };
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08002526 const Instruction& move_inst = code_item_accessor_.InstructionAt(move_idx);
Vladimir Markod7559b72017-09-28 13:50:37 +01002527 switch (move_inst.Opcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07002528 case Instruction::MOVE_OBJECT:
Andreas Gampe43e43252019-01-08 12:06:57 -08002529 maybe_update_fn(move_inst.VRegB_12x(), move_inst.VRegA_12x());
Ian Rogers9b360392013-06-06 14:45:07 -07002530 break;
2531 case Instruction::MOVE_OBJECT_FROM16:
Andreas Gampe43e43252019-01-08 12:06:57 -08002532 maybe_update_fn(move_inst.VRegB_22x(), move_inst.VRegA_22x());
Ian Rogers9b360392013-06-06 14:45:07 -07002533 break;
2534 case Instruction::MOVE_OBJECT_16:
Andreas Gampe43e43252019-01-08 12:06:57 -08002535 maybe_update_fn(move_inst.VRegB_32x(), move_inst.VRegA_32x());
Ian Rogers9b360392013-06-06 14:45:07 -07002536 break;
2537 default:
2538 break;
2539 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002540 }
2541 }
2542 }
2543
jeffhaobdb76512011-09-07 11:43:16 -07002544 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002545 }
jeffhaobdb76512011-09-07 11:43:16 -07002546 case Instruction::IF_LTZ:
2547 case Instruction::IF_GEZ:
2548 case Instruction::IF_GTZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002549 case Instruction::IF_LEZ: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002550 const RegType& reg_type = work_line_->GetRegisterType(this, inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002551 if (!reg_type.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002552 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
2553 << " unexpected as arg to if-ltz/if-gez/if-gtz/if-lez";
Ian Rogersd81871c2011-10-03 13:57:23 -07002554 }
jeffhaobdb76512011-09-07 11:43:16 -07002555 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002556 }
jeffhaobdb76512011-09-07 11:43:16 -07002557 case Instruction::AGET_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002558 VerifyAGet(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002559 break;
jeffhaobdb76512011-09-07 11:43:16 -07002560 case Instruction::AGET_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002561 VerifyAGet(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002562 break;
jeffhaobdb76512011-09-07 11:43:16 -07002563 case Instruction::AGET_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002564 VerifyAGet(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002565 break;
jeffhaobdb76512011-09-07 11:43:16 -07002566 case Instruction::AGET_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002567 VerifyAGet(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002568 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002569 case Instruction::AGET:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002570 VerifyAGet(inst, reg_types_.Integer(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002571 break;
jeffhaobdb76512011-09-07 11:43:16 -07002572 case Instruction::AGET_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002573 VerifyAGet(inst, reg_types_.LongLo(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002574 break;
2575 case Instruction::AGET_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002576 VerifyAGet(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07002577 break;
2578
Ian Rogersd81871c2011-10-03 13:57:23 -07002579 case Instruction::APUT_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002580 VerifyAPut(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002581 break;
2582 case Instruction::APUT_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002583 VerifyAPut(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002584 break;
2585 case Instruction::APUT_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002586 VerifyAPut(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002587 break;
2588 case Instruction::APUT_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002589 VerifyAPut(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002590 break;
2591 case Instruction::APUT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002592 VerifyAPut(inst, reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002593 break;
2594 case Instruction::APUT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002595 VerifyAPut(inst, reg_types_.LongLo(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002596 break;
2597 case Instruction::APUT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002598 VerifyAPut(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07002599 break;
2600
jeffhaobdb76512011-09-07 11:43:16 -07002601 case Instruction::IGET_BOOLEAN:
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00002602 case Instruction::IGET_BOOLEAN_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002603 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002604 break;
jeffhaobdb76512011-09-07 11:43:16 -07002605 case Instruction::IGET_BYTE:
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00002606 case Instruction::IGET_BYTE_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002607 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002608 break;
jeffhaobdb76512011-09-07 11:43:16 -07002609 case Instruction::IGET_CHAR:
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00002610 case Instruction::IGET_CHAR_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002611 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002612 break;
jeffhaobdb76512011-09-07 11:43:16 -07002613 case Instruction::IGET_SHORT:
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00002614 case Instruction::IGET_SHORT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002615 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002616 break;
2617 case Instruction::IGET:
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00002618 case Instruction::IGET_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002619 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002620 break;
2621 case Instruction::IGET_WIDE:
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00002622 case Instruction::IGET_WIDE_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002623 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002624 break;
2625 case Instruction::IGET_OBJECT:
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00002626 case Instruction::IGET_OBJECT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002627 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false,
2628 false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002629 break;
jeffhaobdb76512011-09-07 11:43:16 -07002630
Ian Rogersd81871c2011-10-03 13:57:23 -07002631 case Instruction::IPUT_BOOLEAN:
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00002632 case Instruction::IPUT_BOOLEAN_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002633 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002634 break;
2635 case Instruction::IPUT_BYTE:
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00002636 case Instruction::IPUT_BYTE_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002637 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002638 break;
2639 case Instruction::IPUT_CHAR:
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00002640 case Instruction::IPUT_CHAR_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002641 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002642 break;
2643 case Instruction::IPUT_SHORT:
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00002644 case Instruction::IPUT_SHORT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002645 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002646 break;
2647 case Instruction::IPUT:
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00002648 case Instruction::IPUT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002649 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002650 break;
2651 case Instruction::IPUT_WIDE:
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00002652 case Instruction::IPUT_WIDE_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002653 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002654 break;
jeffhaobdb76512011-09-07 11:43:16 -07002655 case Instruction::IPUT_OBJECT:
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00002656 case Instruction::IPUT_OBJECT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002657 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false,
2658 false);
jeffhaobdb76512011-09-07 11:43:16 -07002659 break;
2660
jeffhaobdb76512011-09-07 11:43:16 -07002661 case Instruction::SGET_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002662 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002663 break;
jeffhaobdb76512011-09-07 11:43:16 -07002664 case Instruction::SGET_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002665 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002666 break;
jeffhaobdb76512011-09-07 11:43:16 -07002667 case Instruction::SGET_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002668 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002669 break;
jeffhaobdb76512011-09-07 11:43:16 -07002670 case Instruction::SGET_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002671 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002672 break;
2673 case Instruction::SGET:
Andreas Gampe896df402014-10-20 22:25:29 -07002674 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002675 break;
2676 case Instruction::SGET_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002677 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002678 break;
2679 case Instruction::SGET_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002680 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false,
2681 true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002682 break;
2683
2684 case Instruction::SPUT_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002685 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002686 break;
2687 case Instruction::SPUT_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002688 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002689 break;
2690 case Instruction::SPUT_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002691 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002692 break;
2693 case Instruction::SPUT_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002694 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002695 break;
2696 case Instruction::SPUT:
Andreas Gampe896df402014-10-20 22:25:29 -07002697 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002698 break;
2699 case Instruction::SPUT_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002700 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002701 break;
2702 case Instruction::SPUT_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002703 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false,
2704 true);
jeffhaobdb76512011-09-07 11:43:16 -07002705 break;
2706
2707 case Instruction::INVOKE_VIRTUAL:
2708 case Instruction::INVOKE_VIRTUAL_RANGE:
2709 case Instruction::INVOKE_SUPER:
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00002710 case Instruction::INVOKE_SUPER_RANGE:
2711 case Instruction::INVOKE_VIRTUAL_QUICK:
2712 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002713 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE ||
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00002714 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE ||
2715 inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002716 bool is_super = (inst->Opcode() == Instruction::INVOKE_SUPER ||
2717 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
Alex Light7268d472016-01-20 15:50:01 -08002718 MethodType type = is_super ? METHOD_SUPER : METHOD_VIRTUAL;
2719 ArtMethod* called_method = VerifyInvocationArgs(inst, type, is_range);
Ian Rogersd8f69b02014-09-10 21:43:52 +00002720 const RegType* return_type = nullptr;
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002721 if (called_method != nullptr) {
Vladimir Markob45528c2017-07-27 14:14:28 +01002722 ObjPtr<mirror::Class> return_type_class = can_load_classes_
2723 ? called_method->ResolveReturnType()
2724 : called_method->LookupResolvedReturnType();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002725 if (return_type_class != nullptr) {
Andreas Gampef23f33d2015-06-23 14:18:17 -07002726 return_type = &FromClass(called_method->GetReturnTypeDescriptor(),
Vladimir Marko179b7c62019-03-22 13:38:57 +00002727 return_type_class,
Andreas Gampef23f33d2015-06-23 14:18:17 -07002728 return_type_class->CannotBeAssignedFromOtherTypes());
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002729 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07002730 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
2731 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002732 }
2733 }
2734 if (return_type == nullptr) {
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00002735 uint32_t method_idx = GetMethodIdxOfInvoke(inst);
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08002736 const dex::MethodId& method_id = dex_file_->GetMethodId(method_idx);
Andreas Gampea5b09a62016-11-17 15:21:22 -08002737 dex::TypeIndex return_type_idx =
2738 dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002739 const char* descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Mathieu Chartierbf99f772014-08-23 16:37:27 -07002740 return_type = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
jeffhaobdb76512011-09-07 11:43:16 -07002741 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002742 if (!return_type->IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002743 work_line_->SetResultRegisterType(this, *return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002744 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002745 work_line_->SetResultRegisterTypeWide(*return_type, return_type->HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002746 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002747 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002748 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002749 }
jeffhaobdb76512011-09-07 11:43:16 -07002750 case Instruction::INVOKE_DIRECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002751 case Instruction::INVOKE_DIRECT_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002752 bool is_range = (inst->Opcode() == Instruction::INVOKE_DIRECT_RANGE);
Alex Light7268d472016-01-20 15:50:01 -08002753 ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_DIRECT, is_range);
Ian Rogers46685432012-06-03 22:26:43 -07002754 const char* return_type_descriptor;
2755 bool is_constructor;
Ian Rogersd8f69b02014-09-10 21:43:52 +00002756 const RegType* return_type = nullptr;
Ian Rogers7b078e82014-09-10 14:44:24 -07002757 if (called_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002758 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08002759 const dex::MethodId& method_id = dex_file_->GetMethodId(method_idx);
Ian Rogersdfb325e2013-10-30 01:00:44 -07002760 is_constructor = strcmp("<init>", dex_file_->StringDataByIdx(method_id.name_idx_)) == 0;
Andreas Gampea5b09a62016-11-17 15:21:22 -08002761 dex::TypeIndex return_type_idx =
2762 dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogers46685432012-06-03 22:26:43 -07002763 return_type_descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2764 } else {
2765 is_constructor = called_method->IsConstructor();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002766 return_type_descriptor = called_method->GetReturnTypeDescriptor();
Vladimir Markob45528c2017-07-27 14:14:28 +01002767 ObjPtr<mirror::Class> return_type_class = can_load_classes_
2768 ? called_method->ResolveReturnType()
2769 : called_method->LookupResolvedReturnType();
Ian Rogers1ff3c982014-08-12 02:30:58 -07002770 if (return_type_class != nullptr) {
Andreas Gampef23f33d2015-06-23 14:18:17 -07002771 return_type = &FromClass(return_type_descriptor,
Vladimir Markobcf17522018-06-01 13:14:32 +01002772 return_type_class,
Andreas Gampef23f33d2015-06-23 14:18:17 -07002773 return_type_class->CannotBeAssignedFromOtherTypes());
Ian Rogers1ff3c982014-08-12 02:30:58 -07002774 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07002775 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
2776 self_->ClearException();
Ian Rogers1ff3c982014-08-12 02:30:58 -07002777 }
Ian Rogers46685432012-06-03 22:26:43 -07002778 }
2779 if (is_constructor) {
jeffhaobdb76512011-09-07 11:43:16 -07002780 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002781 * Some additional checks when calling a constructor. We know from the invocation arg check
2782 * that the "this" argument is an instance of called_method->klass. Now we further restrict
2783 * that to require that called_method->klass is the same as this->klass or this->super,
2784 * allowing the latter only if the "this" argument is the same as the "this" argument to
2785 * this method (which implies that we're in a constructor ourselves).
jeffhaobdb76512011-09-07 11:43:16 -07002786 */
Orion Hodsoncfa325e2016-10-13 10:25:54 +01002787 const RegType& this_type = work_line_->GetInvocationThis(this, inst);
jeffhaob57e9522012-04-26 18:08:21 -07002788 if (this_type.IsConflict()) // failure.
2789 break;
jeffhaobdb76512011-09-07 11:43:16 -07002790
jeffhaob57e9522012-04-26 18:08:21 -07002791 /* no null refs allowed (?) */
Andreas Gampeeed3a5d2017-11-29 14:58:34 -08002792 if (this_type.IsZeroOrNull()) {
jeffhaob57e9522012-04-26 18:08:21 -07002793 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unable to initialize null ref";
2794 break;
jeffhao2a8a90e2011-09-26 14:25:31 -07002795 }
jeffhaob57e9522012-04-26 18:08:21 -07002796
2797 /* must be in same class or in superclass */
Ian Rogersd8f69b02014-09-10 21:43:52 +00002798 // const RegType& this_super_klass = this_type.GetSuperClass(&reg_types_);
Ian Rogers46685432012-06-03 22:26:43 -07002799 // TODO: re-enable constructor type verification
2800 // if (this_super_klass.IsConflict()) {
jeffhaob57e9522012-04-26 18:08:21 -07002801 // Unknown super class, fail so we re-check at runtime.
Ian Rogers46685432012-06-03 22:26:43 -07002802 // Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "super class unknown for '" << this_type << "'";
2803 // break;
2804 // }
jeffhaob57e9522012-04-26 18:08:21 -07002805
2806 /* arg must be an uninitialized reference */
2807 if (!this_type.IsUninitializedTypes()) {
2808 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Expected initialization on uninitialized reference "
2809 << this_type;
2810 break;
2811 }
2812
2813 /*
2814 * Replace the uninitialized reference with an initialized one. We need to do this for all
2815 * registers that have the same object instance in them, not just the "this" register.
2816 */
Nicolas Geoffray98e6ce42016-02-16 18:42:15 +00002817 work_line_->MarkRefsAsInitialized(this, this_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002818 }
Ian Rogers1ff3c982014-08-12 02:30:58 -07002819 if (return_type == nullptr) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07002820 return_type = &reg_types_.FromDescriptor(GetClassLoader(), return_type_descriptor, false);
Ian Rogers1ff3c982014-08-12 02:30:58 -07002821 }
2822 if (!return_type->IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002823 work_line_->SetResultRegisterType(this, *return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002824 } else {
Ian Rogers1ff3c982014-08-12 02:30:58 -07002825 work_line_->SetResultRegisterTypeWide(*return_type, return_type->HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002826 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002827 just_set_result = true;
2828 break;
2829 }
2830 case Instruction::INVOKE_STATIC:
2831 case Instruction::INVOKE_STATIC_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002832 bool is_range = (inst->Opcode() == Instruction::INVOKE_STATIC_RANGE);
Alex Light7268d472016-01-20 15:50:01 -08002833 ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_STATIC, is_range);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002834 const char* descriptor;
Ian Rogers7b078e82014-09-10 14:44:24 -07002835 if (called_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002836 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08002837 const dex::MethodId& method_id = dex_file_->GetMethodId(method_idx);
Andreas Gampea5b09a62016-11-17 15:21:22 -08002838 dex::TypeIndex return_type_idx =
2839 dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Mathieu Chartier590fee92013-09-13 13:46:47 -07002840 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002841 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002842 descriptor = called_method->GetReturnTypeDescriptor();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002843 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00002844 const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002845 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002846 work_line_->SetResultRegisterType(this, return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002847 } else {
2848 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2849 }
jeffhaobdb76512011-09-07 11:43:16 -07002850 just_set_result = true;
2851 }
2852 break;
jeffhaobdb76512011-09-07 11:43:16 -07002853 case Instruction::INVOKE_INTERFACE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002854 case Instruction::INVOKE_INTERFACE_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002855 bool is_range = (inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE);
Alex Light7268d472016-01-20 15:50:01 -08002856 ArtMethod* abs_method = VerifyInvocationArgs(inst, METHOD_INTERFACE, is_range);
Ian Rogers7b078e82014-09-10 14:44:24 -07002857 if (abs_method != nullptr) {
Vladimir Markod93e3742018-07-18 10:58:13 +01002858 ObjPtr<mirror::Class> called_interface = abs_method->GetDeclaringClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002859 if (!called_interface->IsInterface() && !called_interface->IsObjectClass()) {
2860 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected interface class in invoke-interface '"
David Sehr709b0702016-10-13 09:12:37 -07002861 << abs_method->PrettyMethod() << "'";
Ian Rogersad0b3a32012-04-16 14:50:24 -07002862 break;
Ian Rogers28ad40d2011-10-27 15:19:26 -07002863 }
Ian Rogers0d604842012-04-16 14:50:24 -07002864 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002865 /* Get the type of the "this" arg, which should either be a sub-interface of called
2866 * interface or Object (see comments in RegType::JoinClass).
2867 */
Orion Hodsoncfa325e2016-10-13 10:25:54 +01002868 const RegType& this_type = work_line_->GetInvocationThis(this, inst);
Andreas Gampeeed3a5d2017-11-29 14:58:34 -08002869 if (this_type.IsZeroOrNull()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002870 /* null pointer always passes (and always fails at runtime) */
2871 } else {
2872 if (this_type.IsUninitializedTypes()) {
2873 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface call on uninitialized object "
2874 << this_type;
2875 break;
2876 }
2877 // In the past we have tried to assert that "called_interface" is assignable
2878 // from "this_type.GetClass()", however, as we do an imprecise Join
2879 // (RegType::JoinClass) we don't have full information on what interfaces are
2880 // implemented by "this_type". For example, two classes may implement the same
2881 // interfaces and have a common parent that doesn't implement the interface. The
2882 // join will set "this_type" to the parent class and a test that this implements
2883 // the interface will incorrectly fail.
2884 }
2885 /*
2886 * We don't have an object instance, so we can't find the concrete method. However, all of
2887 * the type information is in the abstract method, so we're good.
2888 */
2889 const char* descriptor;
Ian Rogers7b078e82014-09-10 14:44:24 -07002890 if (abs_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002891 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08002892 const dex::MethodId& method_id = dex_file_->GetMethodId(method_idx);
Andreas Gampea5b09a62016-11-17 15:21:22 -08002893 dex::TypeIndex return_type_idx =
2894 dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Mathieu Chartierde40d472015-10-15 17:47:48 -07002895 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002896 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002897 descriptor = abs_method->GetReturnTypeDescriptor();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002898 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00002899 const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002900 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002901 work_line_->SetResultRegisterType(this, return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002902 } else {
2903 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2904 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002905 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002906 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002907 }
Narayan Kamath9823e782016-08-03 12:46:58 +01002908 case Instruction::INVOKE_POLYMORPHIC:
2909 case Instruction::INVOKE_POLYMORPHIC_RANGE: {
Orion Hodsoncfa325e2016-10-13 10:25:54 +01002910 bool is_range = (inst->Opcode() == Instruction::INVOKE_POLYMORPHIC_RANGE);
2911 ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_POLYMORPHIC, is_range);
2912 if (called_method == nullptr) {
2913 // Convert potential soft failures in VerifyInvocationArgs() to hard errors.
2914 if (failure_messages_.size() > 0) {
2915 std::string message = failure_messages_.back()->str();
2916 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << message;
2917 } else {
2918 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invoke-polymorphic verification failure.";
2919 }
2920 break;
2921 }
2922 if (!CheckSignaturePolymorphicMethod(called_method) ||
2923 !CheckSignaturePolymorphicReceiver(inst)) {
Orion Hodsonfe92d122018-01-02 10:45:17 +00002924 DCHECK(HasFailures());
Orion Hodsoncfa325e2016-10-13 10:25:54 +01002925 break;
2926 }
Orion Hodson06d10a72018-05-14 08:53:38 +01002927 const uint16_t vRegH = (is_range) ? inst->VRegH_4rcc() : inst->VRegH_45cc();
2928 const dex::ProtoIndex proto_idx(vRegH);
Orion Hodsonac141392017-01-13 11:53:47 +00002929 const char* return_descriptor =
Orion Hodsoncfa325e2016-10-13 10:25:54 +01002930 dex_file_->GetReturnTypeDescriptor(dex_file_->GetProtoId(proto_idx));
2931 const RegType& return_type =
Orion Hodsonac141392017-01-13 11:53:47 +00002932 reg_types_.FromDescriptor(GetClassLoader(), return_descriptor, false);
Orion Hodsoncfa325e2016-10-13 10:25:54 +01002933 if (!return_type.IsLowHalf()) {
2934 work_line_->SetResultRegisterType(this, return_type);
2935 } else {
2936 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2937 }
Orion Hodsonac141392017-01-13 11:53:47 +00002938 just_set_result = true;
Orion Hodsoncfa325e2016-10-13 10:25:54 +01002939 break;
Narayan Kamath9823e782016-08-03 12:46:58 +01002940 }
Orion Hodsonc069a302017-01-18 09:23:12 +00002941 case Instruction::INVOKE_CUSTOM:
2942 case Instruction::INVOKE_CUSTOM_RANGE: {
2943 // Verify registers based on method_type in the call site.
2944 bool is_range = (inst->Opcode() == Instruction::INVOKE_CUSTOM_RANGE);
2945
2946 // Step 1. Check the call site that produces the method handle for invocation
2947 const uint32_t call_site_idx = is_range ? inst->VRegB_3rc() : inst->VRegB_35c();
2948 if (!CheckCallSite(call_site_idx)) {
2949 DCHECK(HasFailures());
2950 break;
2951 }
2952
2953 // Step 2. Check the register arguments correspond to the expected arguments for the
2954 // method handle produced by step 1. The dex file verifier has checked ranges for
2955 // the first three arguments and CheckCallSite has checked the method handle type.
Orion Hodson4c8e12e2018-05-18 08:33:20 +01002956 const dex::ProtoIndex proto_idx = dex_file_->GetProtoIndexForCallSite(call_site_idx);
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08002957 const dex::ProtoId& proto_id = dex_file_->GetProtoId(proto_idx);
Orion Hodsonc069a302017-01-18 09:23:12 +00002958 DexFileParameterIterator param_it(*dex_file_, proto_id);
2959 // Treat method as static as it has yet to be determined.
2960 VerifyInvocationArgsFromIterator(&param_it, inst, METHOD_STATIC, is_range, nullptr);
2961 const char* return_descriptor = dex_file_->GetReturnTypeDescriptor(proto_id);
2962
2963 // Step 3. Propagate return type information
2964 const RegType& return_type =
2965 reg_types_.FromDescriptor(GetClassLoader(), return_descriptor, false);
2966 if (!return_type.IsLowHalf()) {
2967 work_line_->SetResultRegisterType(this, return_type);
2968 } else {
2969 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2970 }
2971 just_set_result = true;
Orion Hodsonc069a302017-01-18 09:23:12 +00002972 break;
2973 }
jeffhaobdb76512011-09-07 11:43:16 -07002974 case Instruction::NEG_INT:
2975 case Instruction::NOT_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002976 work_line_->CheckUnaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002977 break;
2978 case Instruction::NEG_LONG:
2979 case Instruction::NOT_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002980 work_line_->CheckUnaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002981 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002982 break;
2983 case Instruction::NEG_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002984 work_line_->CheckUnaryOp(this, inst, reg_types_.Float(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002985 break;
2986 case Instruction::NEG_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002987 work_line_->CheckUnaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002988 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002989 break;
2990 case Instruction::INT_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002991 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002992 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002993 break;
2994 case Instruction::INT_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002995 work_line_->CheckUnaryOp(this, inst, reg_types_.Float(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002996 break;
2997 case Instruction::INT_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002998 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002999 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003000 break;
3001 case Instruction::LONG_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003002 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003003 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07003004 break;
3005 case Instruction::LONG_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003006 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003007 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07003008 break;
3009 case Instruction::LONG_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07003010 work_line_->CheckUnaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003011 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07003012 break;
3013 case Instruction::FLOAT_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003014 work_line_->CheckUnaryOp(this, inst, reg_types_.Integer(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07003015 break;
3016 case Instruction::FLOAT_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07003017 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003018 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07003019 break;
3020 case Instruction::FLOAT_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07003021 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003022 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07003023 break;
3024 case Instruction::DOUBLE_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003025 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003026 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07003027 break;
3028 case Instruction::DOUBLE_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07003029 work_line_->CheckUnaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003030 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07003031 break;
3032 case Instruction::DOUBLE_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003033 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003034 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07003035 break;
3036 case Instruction::INT_TO_BYTE:
Ian Rogers7b078e82014-09-10 14:44:24 -07003037 work_line_->CheckUnaryOp(this, inst, reg_types_.Byte(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003038 break;
3039 case Instruction::INT_TO_CHAR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003040 work_line_->CheckUnaryOp(this, inst, reg_types_.Char(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003041 break;
3042 case Instruction::INT_TO_SHORT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003043 work_line_->CheckUnaryOp(this, inst, reg_types_.Short(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003044 break;
3045
3046 case Instruction::ADD_INT:
3047 case Instruction::SUB_INT:
3048 case Instruction::MUL_INT:
3049 case Instruction::REM_INT:
3050 case Instruction::DIV_INT:
3051 case Instruction::SHL_INT:
3052 case Instruction::SHR_INT:
3053 case Instruction::USHR_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003054 work_line_->CheckBinaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003055 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07003056 break;
3057 case Instruction::AND_INT:
3058 case Instruction::OR_INT:
3059 case Instruction::XOR_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003060 work_line_->CheckBinaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003061 reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07003062 break;
3063 case Instruction::ADD_LONG:
3064 case Instruction::SUB_LONG:
3065 case Instruction::MUL_LONG:
3066 case Instruction::DIV_LONG:
3067 case Instruction::REM_LONG:
3068 case Instruction::AND_LONG:
3069 case Instruction::OR_LONG:
3070 case Instruction::XOR_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07003071 work_line_->CheckBinaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003072 reg_types_.LongLo(), reg_types_.LongHi(),
3073 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07003074 break;
3075 case Instruction::SHL_LONG:
3076 case Instruction::SHR_LONG:
3077 case Instruction::USHR_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07003078 /* shift distance is Int, making these different from other binary operations */
Ian Rogers7b078e82014-09-10 14:44:24 -07003079 work_line_->CheckBinaryOpWideShift(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003080 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003081 break;
3082 case Instruction::ADD_FLOAT:
3083 case Instruction::SUB_FLOAT:
3084 case Instruction::MUL_FLOAT:
3085 case Instruction::DIV_FLOAT:
3086 case Instruction::REM_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003087 work_line_->CheckBinaryOp(this, inst, reg_types_.Float(), reg_types_.Float(),
3088 reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07003089 break;
3090 case Instruction::ADD_DOUBLE:
3091 case Instruction::SUB_DOUBLE:
3092 case Instruction::MUL_DOUBLE:
3093 case Instruction::DIV_DOUBLE:
3094 case Instruction::REM_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07003095 work_line_->CheckBinaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003096 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
3097 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07003098 break;
3099 case Instruction::ADD_INT_2ADDR:
3100 case Instruction::SUB_INT_2ADDR:
3101 case Instruction::MUL_INT_2ADDR:
3102 case Instruction::REM_INT_2ADDR:
3103 case Instruction::SHL_INT_2ADDR:
3104 case Instruction::SHR_INT_2ADDR:
3105 case Instruction::USHR_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003106 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
3107 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07003108 break;
3109 case Instruction::AND_INT_2ADDR:
3110 case Instruction::OR_INT_2ADDR:
3111 case Instruction::XOR_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003112 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
3113 reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07003114 break;
3115 case Instruction::DIV_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003116 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
3117 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07003118 break;
3119 case Instruction::ADD_LONG_2ADDR:
3120 case Instruction::SUB_LONG_2ADDR:
3121 case Instruction::MUL_LONG_2ADDR:
3122 case Instruction::DIV_LONG_2ADDR:
3123 case Instruction::REM_LONG_2ADDR:
3124 case Instruction::AND_LONG_2ADDR:
3125 case Instruction::OR_LONG_2ADDR:
3126 case Instruction::XOR_LONG_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003127 work_line_->CheckBinaryOp2addrWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003128 reg_types_.LongLo(), reg_types_.LongHi(),
3129 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07003130 break;
3131 case Instruction::SHL_LONG_2ADDR:
3132 case Instruction::SHR_LONG_2ADDR:
3133 case Instruction::USHR_LONG_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003134 work_line_->CheckBinaryOp2addrWideShift(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003135 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003136 break;
3137 case Instruction::ADD_FLOAT_2ADDR:
3138 case Instruction::SUB_FLOAT_2ADDR:
3139 case Instruction::MUL_FLOAT_2ADDR:
3140 case Instruction::DIV_FLOAT_2ADDR:
3141 case Instruction::REM_FLOAT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003142 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Float(), reg_types_.Float(),
3143 reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07003144 break;
3145 case Instruction::ADD_DOUBLE_2ADDR:
3146 case Instruction::SUB_DOUBLE_2ADDR:
3147 case Instruction::MUL_DOUBLE_2ADDR:
3148 case Instruction::DIV_DOUBLE_2ADDR:
3149 case Instruction::REM_DOUBLE_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003150 work_line_->CheckBinaryOp2addrWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003151 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
3152 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07003153 break;
3154 case Instruction::ADD_INT_LIT16:
Ian Rogersf72a11d2014-10-30 15:41:08 -07003155 case Instruction::RSUB_INT_LIT16:
jeffhaobdb76512011-09-07 11:43:16 -07003156 case Instruction::MUL_INT_LIT16:
3157 case Instruction::DIV_INT_LIT16:
3158 case Instruction::REM_INT_LIT16:
Ian Rogers7b078e82014-09-10 14:44:24 -07003159 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), false,
3160 true);
jeffhaobdb76512011-09-07 11:43:16 -07003161 break;
3162 case Instruction::AND_INT_LIT16:
3163 case Instruction::OR_INT_LIT16:
3164 case Instruction::XOR_INT_LIT16:
Ian Rogers7b078e82014-09-10 14:44:24 -07003165 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), true,
3166 true);
jeffhaobdb76512011-09-07 11:43:16 -07003167 break;
3168 case Instruction::ADD_INT_LIT8:
3169 case Instruction::RSUB_INT_LIT8:
3170 case Instruction::MUL_INT_LIT8:
3171 case Instruction::DIV_INT_LIT8:
3172 case Instruction::REM_INT_LIT8:
3173 case Instruction::SHL_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07003174 case Instruction::SHR_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07003175 case Instruction::USHR_INT_LIT8:
Ian Rogers7b078e82014-09-10 14:44:24 -07003176 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), false,
3177 false);
jeffhaobdb76512011-09-07 11:43:16 -07003178 break;
3179 case Instruction::AND_INT_LIT8:
3180 case Instruction::OR_INT_LIT8:
3181 case Instruction::XOR_INT_LIT8:
Ian Rogers7b078e82014-09-10 14:44:24 -07003182 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), true,
3183 false);
jeffhaobdb76512011-09-07 11:43:16 -07003184 break;
3185
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003186 // Special instructions.
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07003187 case Instruction::RETURN_VOID_NO_BARRIER:
3188 if (IsConstructor() && !IsStatic()) {
Vladimir Markod93e3742018-07-18 10:58:13 +01003189 const RegType& declaring_class = GetDeclaringClass();
Andreas Gampe68df3202015-06-22 11:35:46 -07003190 if (declaring_class.IsUnresolvedReference()) {
3191 // We must iterate over the fields, even if we cannot use mirror classes to do so. Do it
3192 // manually over the underlying dex file.
3193 uint32_t first_index = GetFirstFinalInstanceFieldIndex(*dex_file_,
3194 dex_file_->GetMethodId(dex_method_idx_).class_idx_);
Andreas Gampee2abbc62017-09-15 11:59:26 -07003195 if (first_index != dex::kDexNoIndex) {
Andreas Gampe68df3202015-06-22 11:35:46 -07003196 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void-no-barrier not expected for field "
3197 << first_index;
3198 }
3199 break;
3200 }
Vladimir Marko2cffc5d2018-05-29 15:40:56 +01003201 ObjPtr<mirror::Class> klass = declaring_class.GetClass();
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07003202 for (uint32_t i = 0, num_fields = klass->NumInstanceFields(); i < num_fields; ++i) {
3203 if (klass->GetInstanceField(i)->IsFinal()) {
Mathieu Chartiere86deef2015-03-19 13:43:37 -07003204 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void-no-barrier not expected for "
David Sehr709b0702016-10-13 09:12:37 -07003205 << klass->GetInstanceField(i)->PrettyField();
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07003206 break;
3207 }
3208 }
Sebastien Hertzcc10e0e2013-06-28 14:24:48 +02003209 }
Andreas Gampeb2917962015-07-31 13:36:10 -07003210 // Handle this like a RETURN_VOID now. Code is duplicated to separate standard from
3211 // quickened opcodes (otherwise this could be a fall-through).
3212 if (!IsConstructor()) {
3213 if (!GetMethodReturnType().IsConflict()) {
3214 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void not expected";
3215 }
3216 }
Sebastien Hertzcc10e0e2013-06-28 14:24:48 +02003217 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003218
Ian Rogersd81871c2011-10-03 13:57:23 -07003219 /* These should never appear during verification. */
Mathieu Chartierffc605c2014-12-10 10:35:44 -08003220 case Instruction::UNUSED_3E ... Instruction::UNUSED_43:
Narayan Kamath8ec3bd22016-08-03 12:46:23 +01003221 case Instruction::UNUSED_F3 ... Instruction::UNUSED_F9:
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003222 case Instruction::UNUSED_79:
3223 case Instruction::UNUSED_7A:
jeffhaod5347e02012-03-22 17:25:05 -07003224 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Unexpected opcode " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07003225 break;
3226
3227 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003228 * DO NOT add a "default" clause here. Without it the compiler will
jeffhaobdb76512011-09-07 11:43:16 -07003229 * complain if an instruction is missing (which is desirable).
3230 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003231 } // end - switch (dec_insn.opcode)
jeffhaobdb76512011-09-07 11:43:16 -07003232
Ian Rogersad0b3a32012-04-16 14:50:24 -07003233 if (have_pending_hard_failure_) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08003234 if (Runtime::Current()->IsAotCompiler()) {
3235 /* When AOT compiling, check that the last failure is a hard failure */
Andreas Gampeb588f4c2015-05-26 13:35:39 -07003236 if (failures_[failures_.size() - 1] != VERIFY_ERROR_BAD_CLASS_HARD) {
3237 LOG(ERROR) << "Pending failures:";
3238 for (auto& error : failures_) {
3239 LOG(ERROR) << error;
3240 }
3241 for (auto& error_msg : failure_messages_) {
3242 LOG(ERROR) << error_msg->str();
3243 }
3244 LOG(FATAL) << "Pending hard failure, but last failure not hard.";
3245 }
Ian Rogerse1758fe2012-04-19 11:31:15 -07003246 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003247 /* immediate failure, reject class */
3248 info_messages_ << "Rejecting opcode " << inst->DumpString(dex_file_);
3249 return false;
jeffhaofaf459e2012-08-31 15:32:47 -07003250 } else if (have_pending_runtime_throw_failure_) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07003251 /* checking interpreter will throw, mark following code as unreachable */
jeffhaofaf459e2012-08-31 15:32:47 -07003252 opcode_flags = Instruction::kThrow;
Andreas Gampea727e372015-08-25 09:22:37 -07003253 // Note: the flag must be reset as it is only global to decouple Fail and is semantically per
3254 // instruction. However, RETURN checking may throw LOCKING errors, so we clear at the
3255 // very end.
jeffhaobdb76512011-09-07 11:43:16 -07003256 }
jeffhaobdb76512011-09-07 11:43:16 -07003257 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003258 * If we didn't just set the result register, clear it out. This ensures that you can only use
3259 * "move-result" immediately after the result is set. (We could check this statically, but it's
3260 * not expensive and it makes our debugging output cleaner.)
jeffhaobdb76512011-09-07 11:43:16 -07003261 */
3262 if (!just_set_result) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003263 work_line_->SetResultTypeToUnknown(this);
jeffhaobdb76512011-09-07 11:43:16 -07003264 }
3265
jeffhaobdb76512011-09-07 11:43:16 -07003266 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003267 * Handle "branch". Tag the branch target.
jeffhaobdb76512011-09-07 11:43:16 -07003268 *
3269 * NOTE: instructions like Instruction::EQZ provide information about the
jeffhaod1f0fde2011-09-08 17:25:33 -07003270 * state of the register when the branch is taken or not taken. For example,
jeffhaobdb76512011-09-07 11:43:16 -07003271 * somebody could get a reference field, check it for zero, and if the
3272 * branch is taken immediately store that register in a boolean field
jeffhaod1f0fde2011-09-08 17:25:33 -07003273 * since the value is known to be zero. We do not currently account for
jeffhaobdb76512011-09-07 11:43:16 -07003274 * that, and will reject the code.
3275 *
3276 * TODO: avoid re-fetching the branch target
3277 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003278 if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07003279 bool isConditional, selfOkay;
Ian Rogersd81871c2011-10-03 13:57:23 -07003280 if (!GetBranchOffset(work_insn_idx_, &branch_target, &isConditional, &selfOkay)) {
jeffhaobdb76512011-09-07 11:43:16 -07003281 /* should never happen after static verification */
jeffhaod5347e02012-03-22 17:25:05 -07003282 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad branch";
jeffhaobdb76512011-09-07 11:43:16 -07003283 return false;
3284 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08003285 DCHECK_EQ(isConditional, (opcode_flags & Instruction::kContinue) != 0);
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08003286 if (!CheckNotMoveExceptionOrMoveResult(code_item_accessor_.Insns(),
3287 work_insn_idx_ + branch_target)) {
jeffhaobdb76512011-09-07 11:43:16 -07003288 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07003289 }
jeffhaobdb76512011-09-07 11:43:16 -07003290 /* update branch target, set "changed" if appropriate */
Mathieu Chartierde40d472015-10-15 17:47:48 -07003291 if (nullptr != branch_line) {
Ian Rogersebbdd872014-07-07 23:53:08 -07003292 if (!UpdateRegisters(work_insn_idx_ + branch_target, branch_line.get(), false)) {
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003293 return false;
3294 }
3295 } else {
Ian Rogersebbdd872014-07-07 23:53:08 -07003296 if (!UpdateRegisters(work_insn_idx_ + branch_target, work_line_.get(), false)) {
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003297 return false;
3298 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003299 }
jeffhaobdb76512011-09-07 11:43:16 -07003300 }
3301
3302 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003303 * Handle "switch". Tag all possible branch targets.
jeffhaobdb76512011-09-07 11:43:16 -07003304 *
3305 * We've already verified that the table is structurally sound, so we
3306 * just need to walk through and tag the targets.
3307 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003308 if ((opcode_flags & Instruction::kSwitch) != 0) {
Andreas Gampe53de99c2015-08-17 13:43:55 -07003309 int offset_to_switch = insns[1] | (static_cast<int32_t>(insns[2]) << 16);
jeffhaobdb76512011-09-07 11:43:16 -07003310 const uint16_t* switch_insns = insns + offset_to_switch;
3311 int switch_count = switch_insns[1];
3312 int offset_to_targets, targ;
3313
3314 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
3315 /* 0 = sig, 1 = count, 2/3 = first key */
3316 offset_to_targets = 4;
3317 } else {
3318 /* 0 = sig, 1 = count, 2..count * 2 = keys */
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07003319 DCHECK((*insns & 0xff) == Instruction::SPARSE_SWITCH);
jeffhaobdb76512011-09-07 11:43:16 -07003320 offset_to_targets = 2 + 2 * switch_count;
3321 }
3322
3323 /* verify each switch target */
3324 for (targ = 0; targ < switch_count; targ++) {
3325 int offset;
3326 uint32_t abs_offset;
3327
3328 /* offsets are 32-bit, and only partly endian-swapped */
3329 offset = switch_insns[offset_to_targets + targ * 2] |
Andreas Gampe53de99c2015-08-17 13:43:55 -07003330 (static_cast<int32_t>(switch_insns[offset_to_targets + targ * 2 + 1]) << 16);
Ian Rogersd81871c2011-10-03 13:57:23 -07003331 abs_offset = work_insn_idx_ + offset;
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08003332 DCHECK_LT(abs_offset, code_item_accessor_.InsnsSizeInCodeUnits());
3333 if (!CheckNotMoveExceptionOrMoveResult(code_item_accessor_.Insns(), abs_offset)) {
jeffhaobdb76512011-09-07 11:43:16 -07003334 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07003335 }
Ian Rogersebbdd872014-07-07 23:53:08 -07003336 if (!UpdateRegisters(abs_offset, work_line_.get(), false)) {
jeffhaobdb76512011-09-07 11:43:16 -07003337 return false;
Ian Rogersebbdd872014-07-07 23:53:08 -07003338 }
jeffhaobdb76512011-09-07 11:43:16 -07003339 }
3340 }
3341
3342 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003343 * Handle instructions that can throw and that are sitting in a "try" block. (If they're not in a
3344 * "try" block when they throw, control transfers out of the method.)
jeffhaobdb76512011-09-07 11:43:16 -07003345 */
Mathieu Chartierde40d472015-10-15 17:47:48 -07003346 if ((opcode_flags & Instruction::kThrow) != 0 && GetInstructionFlags(work_insn_idx_).IsInTry()) {
Andreas Gampef91baf12014-07-18 15:41:00 -07003347 bool has_catch_all_handler = false;
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08003348 const dex::TryItem* try_item = code_item_accessor_.FindTryItem(work_insn_idx_);
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08003349 CHECK(try_item != nullptr);
Mathieu Chartierdc578c72017-12-27 11:51:45 -08003350 CatchHandlerIterator iterator(code_item_accessor_, *try_item);
jeffhaobdb76512011-09-07 11:43:16 -07003351
Andreas Gampef91baf12014-07-18 15:41:00 -07003352 // Need the linker to try and resolve the handled class to check if it's Throwable.
3353 ClassLinker* linker = Runtime::Current()->GetClassLinker();
3354
Ian Rogers0571d352011-11-03 19:51:38 -07003355 for (; iterator.HasNext(); iterator.Next()) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08003356 dex::TypeIndex handler_type_idx = iterator.GetHandlerTypeIndex();
3357 if (!handler_type_idx.IsValid()) {
Andreas Gampef91baf12014-07-18 15:41:00 -07003358 has_catch_all_handler = true;
3359 } else {
3360 // It is also a catch-all if it is java.lang.Throwable.
Vladimir Marko28e012a2017-12-07 11:22:59 +00003361 ObjPtr<mirror::Class> klass =
Vladimir Marko666ee3d2017-12-11 18:37:36 +00003362 linker->ResolveType(handler_type_idx, dex_cache_, class_loader_);
Andreas Gampef91baf12014-07-18 15:41:00 -07003363 if (klass != nullptr) {
Vladimir Markoadbceb72018-05-29 14:34:14 +01003364 if (klass == GetClassRoot<mirror::Throwable>()) {
Andreas Gampef91baf12014-07-18 15:41:00 -07003365 has_catch_all_handler = true;
3366 }
3367 } else {
3368 // Clear exception.
Ian Rogers7b078e82014-09-10 14:44:24 -07003369 DCHECK(self_->IsExceptionPending());
3370 self_->ClearException();
Andreas Gampef91baf12014-07-18 15:41:00 -07003371 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003372 }
jeffhaobdb76512011-09-07 11:43:16 -07003373 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003374 * Merge registers into the "catch" block. We want to use the "savedRegs" rather than
3375 * "work_regs", because at runtime the exception will be thrown before the instruction
3376 * modifies any registers.
jeffhaobdb76512011-09-07 11:43:16 -07003377 */
Ian Rogersebbdd872014-07-07 23:53:08 -07003378 if (!UpdateRegisters(iterator.GetHandlerAddress(), saved_line_.get(), false)) {
jeffhaobdb76512011-09-07 11:43:16 -07003379 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07003380 }
jeffhaobdb76512011-09-07 11:43:16 -07003381 }
3382
3383 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003384 * If the monitor stack depth is nonzero, there must be a "catch all" handler for this
3385 * instruction. This does apply to monitor-exit because of async exception handling.
jeffhaobdb76512011-09-07 11:43:16 -07003386 */
Andreas Gampef91baf12014-07-18 15:41:00 -07003387 if (work_line_->MonitorStackDepth() > 0 && !has_catch_all_handler) {
jeffhaobdb76512011-09-07 11:43:16 -07003388 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003389 * The state in work_line reflects the post-execution state. If the current instruction is a
3390 * monitor-enter and the monitor stack was empty, we don't need a catch-all (if it throws,
jeffhaobdb76512011-09-07 11:43:16 -07003391 * it will do so before grabbing the lock).
3392 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02003393 if (inst->Opcode() != Instruction::MONITOR_ENTER || work_line_->MonitorStackDepth() != 1) {
jeffhaod5347e02012-03-22 17:25:05 -07003394 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
Ian Rogersd81871c2011-10-03 13:57:23 -07003395 << "expected to be within a catch-all for an instruction where a monitor is held";
jeffhaobdb76512011-09-07 11:43:16 -07003396 return false;
3397 }
3398 }
3399 }
3400
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003401 /* Handle "continue". Tag the next consecutive instruction.
3402 * Note: Keep the code handling "continue" case below the "branch" and "switch" cases,
3403 * because it changes work_line_ when performing peephole optimization
3404 * and this change should not be used in those cases.
3405 */
Ian Rogers6d376ae2013-07-23 15:12:40 -07003406 if ((opcode_flags & Instruction::kContinue) != 0) {
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08003407 DCHECK_EQ(&code_item_accessor_.InstructionAt(work_insn_idx_), inst);
Ian Rogers7b078e82014-09-10 14:44:24 -07003408 uint32_t next_insn_idx = work_insn_idx_ + inst->SizeInCodeUnits();
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08003409 if (next_insn_idx >= code_item_accessor_.InsnsSizeInCodeUnits()) {
Ian Rogers6d376ae2013-07-23 15:12:40 -07003410 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Execution can walk off end of code area";
3411 return false;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003412 }
Ian Rogers6d376ae2013-07-23 15:12:40 -07003413 // The only way to get to a move-exception instruction is to get thrown there. Make sure the
3414 // next instruction isn't one.
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08003415 if (!CheckNotMoveException(code_item_accessor_.Insns(), next_insn_idx)) {
Ian Rogers6d376ae2013-07-23 15:12:40 -07003416 return false;
3417 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07003418 if (nullptr != fallthrough_line) {
Ian Rogers6d376ae2013-07-23 15:12:40 -07003419 // Make workline consistent with fallthrough computed from peephole optimization.
3420 work_line_->CopyFromLine(fallthrough_line.get());
3421 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07003422 if (GetInstructionFlags(next_insn_idx).IsReturn()) {
Ian Rogersb8c78592013-07-25 23:52:52 +00003423 // For returns we only care about the operand to the return, all other registers are dead.
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08003424 const Instruction* ret_inst = &code_item_accessor_.InstructionAt(next_insn_idx);
Andreas Gampea727e372015-08-25 09:22:37 -07003425 AdjustReturnLine(this, ret_inst, work_line_.get());
Ian Rogersb8c78592013-07-25 23:52:52 +00003426 }
Ian Rogers6d376ae2013-07-23 15:12:40 -07003427 RegisterLine* next_line = reg_table_.GetLine(next_insn_idx);
Ian Rogers7b078e82014-09-10 14:44:24 -07003428 if (next_line != nullptr) {
Ian Rogersebbdd872014-07-07 23:53:08 -07003429 // Merge registers into what we have for the next instruction, and set the "changed" flag if
3430 // needed. If the merge changes the state of the registers then the work line will be
3431 // updated.
3432 if (!UpdateRegisters(next_insn_idx, work_line_.get(), true)) {
Ian Rogers6d376ae2013-07-23 15:12:40 -07003433 return false;
3434 }
3435 } else {
3436 /*
3437 * We're not recording register data for the next instruction, so we don't know what the
3438 * prior state was. We have to assume that something has changed and re-evaluate it.
3439 */
Mathieu Chartierde40d472015-10-15 17:47:48 -07003440 GetInstructionFlags(next_insn_idx).SetChanged();
Ian Rogers6d376ae2013-07-23 15:12:40 -07003441 }
3442 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003443
jeffhaod1f0fde2011-09-08 17:25:33 -07003444 /* If we're returning from the method, make sure monitor stack is empty. */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003445 if ((opcode_flags & Instruction::kReturn) != 0) {
Andreas Gampea727e372015-08-25 09:22:37 -07003446 work_line_->VerifyMonitorStackEmpty(this);
jeffhaobdb76512011-09-07 11:43:16 -07003447 }
3448
3449 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003450 * Update start_guess. Advance to the next instruction of that's
3451 * possible, otherwise use the branch target if one was found. If
jeffhaobdb76512011-09-07 11:43:16 -07003452 * neither of those exists we're in a return or throw; leave start_guess
3453 * alone and let the caller sort it out.
3454 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003455 if ((opcode_flags & Instruction::kContinue) != 0) {
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08003456 DCHECK_EQ(&code_item_accessor_.InstructionAt(work_insn_idx_), inst);
Ian Rogers7b078e82014-09-10 14:44:24 -07003457 *start_guess = work_insn_idx_ + inst->SizeInCodeUnits();
Elliott Hughesadb8c672012-03-06 16:49:32 -08003458 } else if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07003459 /* we're still okay if branch_target is zero */
Ian Rogersd81871c2011-10-03 13:57:23 -07003460 *start_guess = work_insn_idx_ + branch_target;
jeffhaobdb76512011-09-07 11:43:16 -07003461 }
3462
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08003463 DCHECK_LT(*start_guess, code_item_accessor_.InsnsSizeInCodeUnits());
Mathieu Chartierde40d472015-10-15 17:47:48 -07003464 DCHECK(GetInstructionFlags(*start_guess).IsOpcode());
jeffhaobdb76512011-09-07 11:43:16 -07003465
Andreas Gampea727e372015-08-25 09:22:37 -07003466 if (have_pending_runtime_throw_failure_) {
3467 have_any_pending_runtime_throw_failure_ = true;
3468 // Reset the pending_runtime_throw flag now.
3469 have_pending_runtime_throw_failure_ = false;
3470 }
3471
jeffhaobdb76512011-09-07 11:43:16 -07003472 return true;
Brian Carlstrom7934ac22013-07-26 10:54:15 -07003473} // NOLINT(readability/fn_size)
jeffhaobdb76512011-09-07 11:43:16 -07003474
Mathieu Chartierde40d472015-10-15 17:47:48 -07003475void MethodVerifier::UninstantiableError(const char* descriptor) {
3476 Fail(VerifyError::VERIFY_ERROR_NO_CLASS) << "Could not create precise reference for "
3477 << "non-instantiable klass " << descriptor;
3478}
3479
Vladimir Marko28e012a2017-12-07 11:22:59 +00003480inline bool MethodVerifier::IsInstantiableOrPrimitive(ObjPtr<mirror::Class> klass) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07003481 return klass->IsInstantiable() || klass->IsPrimitive();
3482}
3483
Andreas Gampe98be1a92017-08-28 08:25:45 -07003484template <MethodVerifier::CheckAccess C>
3485const RegType& MethodVerifier::ResolveClass(dex::TypeIndex class_idx) {
Vladimir Marko666ee3d2017-12-11 18:37:36 +00003486 ClassLinker* linker = Runtime::Current()->GetClassLinker();
Vladimir Marko28e012a2017-12-07 11:22:59 +00003487 ObjPtr<mirror::Class> klass = can_load_classes_
Vladimir Marko666ee3d2017-12-11 18:37:36 +00003488 ? linker->ResolveType(class_idx, dex_cache_, class_loader_)
3489 : linker->LookupResolvedType(class_idx, dex_cache_.Get(), class_loader_.Get());
Vladimir Marko8d6768d2017-03-14 10:13:21 +00003490 if (can_load_classes_ && klass == nullptr) {
3491 DCHECK(self_->IsExceptionPending());
3492 self_->ClearException();
3493 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07003494 const RegType* result = nullptr;
Vladimir Marko9cb0c462017-04-21 13:31:41 +01003495 if (klass != nullptr) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07003496 bool precise = klass->CannotBeAssignedFromOtherTypes();
3497 if (precise && !IsInstantiableOrPrimitive(klass)) {
3498 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
3499 UninstantiableError(descriptor);
3500 precise = false;
3501 }
Vladimir Marko2cffc5d2018-05-29 15:40:56 +01003502 result = reg_types_.FindClass(klass, precise);
Mathieu Chartierde40d472015-10-15 17:47:48 -07003503 if (result == nullptr) {
3504 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
Vladimir Marko2cffc5d2018-05-29 15:40:56 +01003505 result = reg_types_.InsertClass(descriptor, klass, precise);
Mathieu Chartierde40d472015-10-15 17:47:48 -07003506 }
3507 } else {
3508 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
3509 result = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003510 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07003511 DCHECK(result != nullptr);
3512 if (result->IsConflict()) {
3513 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
3514 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "accessing broken descriptor '" << descriptor
3515 << "' in " << GetDeclaringClass();
3516 return *result;
3517 }
David Brazdilca3c8c32016-09-06 14:04:48 +01003518
3519 // Record result of class resolution attempt.
Vladimir Markobcf17522018-06-01 13:14:32 +01003520 VerifierDeps::MaybeRecordClassResolution(*dex_file_, class_idx, klass);
David Brazdilca3c8c32016-09-06 14:04:48 +01003521
Andreas Gampe629be512017-08-25 17:09:32 -07003522 // If requested, check if access is allowed. Unresolved types are included in this check, as the
3523 // interpreter only tests whether access is allowed when a class is not pre-verified and runs in
3524 // the access-checks interpreter. If result is primitive, skip the access check.
3525 //
3526 // Note: we do this for unresolved classes to trigger re-verification at runtime.
Andreas Gampedc39d322018-09-04 09:26:03 -07003527 if (C == CheckAccess::kYes &&
3528 result->IsNonZeroReferenceTypes() &&
David Brazdil2bb2fbd2018-11-13 18:24:26 +00003529 (IsSdkVersionSetAndAtLeast(api_level_, SdkVersion::kP) || !result->IsUnresolvedTypes())) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07003530 const RegType& referrer = GetDeclaringClass();
David Brazdil2bb2fbd2018-11-13 18:24:26 +00003531 if ((IsSdkVersionSetAndAtLeast(api_level_, SdkVersion::kP) || !referrer.IsUnresolvedTypes()) &&
3532 !referrer.CanAccess(*result)) {
Andreas Gampe629be512017-08-25 17:09:32 -07003533 Fail(VERIFY_ERROR_ACCESS_CLASS) << "(possibly) illegal class access: '"
Nicolas Geoffray4e868fa2017-04-21 17:16:44 +01003534 << referrer << "' -> '" << *result << "'";
Mathieu Chartierde40d472015-10-15 17:47:48 -07003535 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07003536 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07003537 return *result;
Ian Rogersd81871c2011-10-03 13:57:23 -07003538}
3539
Andreas Gampedd309072017-08-31 07:47:37 -07003540// Instantiate ResolveClass variants. This is required as the -inl file has a function with a call
3541// to ResolveClass, and compilers may decide to inline, requiring a symbol.
Andreas Gampeae0b1f42017-08-30 17:36:26 -07003542template const RegType& MethodVerifier::ResolveClass<MethodVerifier::CheckAccess::kNo>(
3543 dex::TypeIndex class_idx);
3544template const RegType& MethodVerifier::ResolveClass<MethodVerifier::CheckAccess::kYes>(
3545 dex::TypeIndex class_idx);
3546
Ian Rogersd8f69b02014-09-10 21:43:52 +00003547const RegType& MethodVerifier::GetCaughtExceptionType() {
Ian Rogers7b078e82014-09-10 14:44:24 -07003548 const RegType* common_super = nullptr;
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08003549 if (code_item_accessor_.TriesSize() != 0) {
3550 const uint8_t* handlers_ptr = code_item_accessor_.GetCatchHandlerData();
Ian Rogersd81871c2011-10-03 13:57:23 -07003551 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
3552 for (uint32_t i = 0; i < handlers_size; i++) {
Ian Rogers0571d352011-11-03 19:51:38 -07003553 CatchHandlerIterator iterator(handlers_ptr);
3554 for (; iterator.HasNext(); iterator.Next()) {
3555 if (iterator.GetHandlerAddress() == (uint32_t) work_insn_idx_) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08003556 if (!iterator.GetHandlerTypeIndex().IsValid()) {
Ian Rogersb4903572012-10-11 11:52:56 -07003557 common_super = &reg_types_.JavaLangThrowable(false);
Ian Rogersd81871c2011-10-03 13:57:23 -07003558 } else {
Andreas Gampe98be1a92017-08-28 08:25:45 -07003559 const RegType& exception =
3560 ResolveClass<CheckAccess::kYes>(iterator.GetHandlerTypeIndex());
David Brazdilca3c8c32016-09-06 14:04:48 +01003561 if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(exception, this)) {
David Brazdil68b5c0b2016-01-19 14:25:29 +00003562 DCHECK(!exception.IsUninitializedTypes()); // Comes from dex, shouldn't be uninit.
Jeff Haoc26a56c2013-11-04 12:00:47 -08003563 if (exception.IsUnresolvedTypes()) {
3564 // We don't know enough about the type. Fail here and let runtime handle it.
3565 Fail(VERIFY_ERROR_NO_CLASS) << "unresolved exception class " << exception;
3566 return exception;
3567 } else {
3568 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unexpected non-exception class " << exception;
3569 return reg_types_.Conflict();
3570 }
Jeff Haob878f212014-04-24 16:25:36 -07003571 } else if (common_super == nullptr) {
3572 common_super = &exception;
Ian Rogers28ad40d2011-10-27 15:19:26 -07003573 } else if (common_super->Equals(exception)) {
Ian Rogersc4762272012-02-01 15:55:55 -08003574 // odd case, but nothing to do
Ian Rogersd81871c2011-10-03 13:57:23 -07003575 } else {
David Brazdilca3c8c32016-09-06 14:04:48 +01003576 common_super = &common_super->Merge(exception, &reg_types_, this);
Mathieu Chartierc2a2bda2018-03-12 11:21:52 -07003577 if (FailOrAbort(reg_types_.JavaLangThrowable(false).IsAssignableFrom(
David Brazdilca3c8c32016-09-06 14:04:48 +01003578 *common_super, this),
Andreas Gampe7c038102014-10-27 20:08:46 -07003579 "java.lang.Throwable is not assignable-from common_super at ",
3580 work_insn_idx_)) {
3581 break;
3582 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003583 }
3584 }
3585 }
3586 }
Ian Rogers0571d352011-11-03 19:51:38 -07003587 handlers_ptr = iterator.EndDataPointer();
Ian Rogersd81871c2011-10-03 13:57:23 -07003588 }
3589 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003590 if (common_super == nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003591 /* no catch blocks, or no catches with classes we can find */
jeffhaod5347e02012-03-22 17:25:05 -07003592 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unable to find exception handler";
Ian Rogersad0b3a32012-04-16 14:50:24 -07003593 return reg_types_.Conflict();
Ian Rogersd81871c2011-10-03 13:57:23 -07003594 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07003595 return *common_super;
Ian Rogersd81871c2011-10-03 13:57:23 -07003596}
3597
Mathieu Chartiere401d142015-04-22 13:56:20 -07003598ArtMethod* MethodVerifier::ResolveMethodAndCheckAccess(
Alex Light7268d472016-01-20 15:50:01 -08003599 uint32_t dex_method_idx, MethodType method_type) {
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08003600 const dex::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx);
Andreas Gampe98be1a92017-08-28 08:25:45 -07003601 const RegType& klass_type = ResolveClass<CheckAccess::kYes>(method_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003602 if (klass_type.IsConflict()) {
3603 std::string append(" in attempt to access method ");
3604 append += dex_file_->GetMethodName(method_id);
3605 AppendToLastFailMessage(append);
Ian Rogers7b078e82014-09-10 14:44:24 -07003606 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08003607 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003608 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003609 return nullptr; // Can't resolve Class so no more to do here
Ian Rogers90040192011-12-16 08:54:29 -08003610 }
Vladimir Markoba118822017-06-12 15:41:56 +01003611 ObjPtr<mirror::Class> klass = klass_type.GetClass();
Ian Rogersd8f69b02014-09-10 21:43:52 +00003612 const RegType& referrer = GetDeclaringClass();
Nicolas Geoffrayea179f42018-02-08 22:30:18 +00003613 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
3614 PointerSize pointer_size = class_linker->GetImagePointerSize();
Andreas Gampe42ef8ab2015-12-03 17:27:32 -08003615
Mathieu Chartiere401d142015-04-22 13:56:20 -07003616 ArtMethod* res_method = dex_cache_->GetResolvedMethod(dex_method_idx, pointer_size);
Ian Rogers7b078e82014-09-10 14:44:24 -07003617 if (res_method == nullptr) {
Nicolas Geoffrayea179f42018-02-08 22:30:18 +00003618 res_method = class_linker->FindResolvedMethod(
3619 klass, dex_cache_.Get(), class_loader_.Get(), dex_method_idx);
Ian Rogersd81871c2011-10-03 13:57:23 -07003620 }
David Brazdilca3c8c32016-09-06 14:04:48 +01003621
Vladimir Markoba118822017-06-12 15:41:56 +01003622 // Record result of method resolution attempt. The klass resolution has recorded whether
3623 // the class is an interface or not and therefore the type of the lookup performed above.
3624 // TODO: Maybe we should not record dependency if the invoke type does not match the lookup type.
3625 VerifierDeps::MaybeRecordMethodResolution(*dex_file_, dex_method_idx, res_method);
3626
Alex Lightafb66472017-08-01 09:54:49 -07003627 bool must_fail = false;
3628 // This is traditional and helps with screwy bytecode. It will tell you that, yes, a method
3629 // exists, but that it's called incorrectly. This significantly helps debugging, as locally it's
3630 // hard to see the differences.
3631 // If we don't have res_method here we must fail. Just use this bool to make sure of that with a
3632 // DCHECK.
Vladimir Markoba118822017-06-12 15:41:56 +01003633 if (res_method == nullptr) {
Alex Lightafb66472017-08-01 09:54:49 -07003634 must_fail = true;
Vladimir Markoba118822017-06-12 15:41:56 +01003635 // Try to find the method also with the other type for better error reporting below
3636 // but do not store such bogus lookup result in the DexCache or VerifierDeps.
David Brazdil4525e0b2018-04-05 16:57:32 +01003637 res_method = class_linker->FindIncompatibleMethod(
3638 klass, dex_cache_.Get(), class_loader_.Get(), dex_method_idx);
Vladimir Markoba118822017-06-12 15:41:56 +01003639 }
David Brazdilca3c8c32016-09-06 14:04:48 +01003640
3641 if (res_method == nullptr) {
3642 Fail(VERIFY_ERROR_NO_METHOD) << "couldn't find method "
David Sehr709b0702016-10-13 09:12:37 -07003643 << klass->PrettyDescriptor() << "."
David Brazdilca3c8c32016-09-06 14:04:48 +01003644 << dex_file_->GetMethodName(method_id) << " "
3645 << dex_file_->GetMethodSignature(method_id);
3646 return nullptr;
3647 }
3648
Ian Rogersd81871c2011-10-03 13:57:23 -07003649 // Make sure calls to constructors are "direct". There are additional restrictions but we don't
3650 // enforce them here.
3651 if (res_method->IsConstructor() && method_type != METHOD_DIRECT) {
jeffhaod5347e02012-03-22 17:25:05 -07003652 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting non-direct call to constructor "
David Sehr709b0702016-10-13 09:12:37 -07003653 << res_method->PrettyMethod();
Ian Rogers7b078e82014-09-10 14:44:24 -07003654 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003655 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003656 // Disallow any calls to class initializers.
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003657 if (res_method->IsClassInitializer()) {
jeffhaod5347e02012-03-22 17:25:05 -07003658 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting call to class initializer "
David Sehr709b0702016-10-13 09:12:37 -07003659 << res_method->PrettyMethod();
Ian Rogers7b078e82014-09-10 14:44:24 -07003660 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003661 }
Andreas Gampe42ef8ab2015-12-03 17:27:32 -08003662
3663 // Check that interface methods are static or match interface classes.
3664 // We only allow statics if we don't have default methods enabled.
3665 //
3666 // Note: this check must be after the initializer check, as those are required to fail a class,
3667 // while this check implies an IncompatibleClassChangeError.
3668 if (klass->IsInterface()) {
Alex Lightb55f1ac2016-04-12 15:50:55 -07003669 // methods called on interfaces should be invoke-interface, invoke-super, invoke-direct (if
Mathieu Chartierf6e31472017-12-28 13:32:08 -08003670 // default methods are supported for the dex file), or invoke-static.
Andreas Gampe42ef8ab2015-12-03 17:27:32 -08003671 if (method_type != METHOD_INTERFACE &&
Neil Fuller9724c632016-01-07 15:42:47 +00003672 method_type != METHOD_STATIC &&
Mathieu Chartierf6e31472017-12-28 13:32:08 -08003673 (!dex_file_->SupportsDefaultMethods() ||
Alex Lightb55f1ac2016-04-12 15:50:55 -07003674 method_type != METHOD_DIRECT) &&
Neil Fuller9724c632016-01-07 15:42:47 +00003675 method_type != METHOD_SUPER) {
Andreas Gampe42ef8ab2015-12-03 17:27:32 -08003676 Fail(VERIFY_ERROR_CLASS_CHANGE)
David Sehr709b0702016-10-13 09:12:37 -07003677 << "non-interface method " << dex_file_->PrettyMethod(dex_method_idx)
3678 << " is in an interface class " << klass->PrettyClass();
Andreas Gampe42ef8ab2015-12-03 17:27:32 -08003679 return nullptr;
3680 }
3681 } else {
3682 if (method_type == METHOD_INTERFACE) {
3683 Fail(VERIFY_ERROR_CLASS_CHANGE)
David Sehr709b0702016-10-13 09:12:37 -07003684 << "interface method " << dex_file_->PrettyMethod(dex_method_idx)
3685 << " is in a non-interface class " << klass->PrettyClass();
Andreas Gampe42ef8ab2015-12-03 17:27:32 -08003686 return nullptr;
3687 }
3688 }
3689
Alex Lightafb66472017-08-01 09:54:49 -07003690 // Check specifically for non-public object methods being provided for interface dispatch. This
3691 // can occur if we failed to find a method with FindInterfaceMethod but later find one with
3692 // FindClassMethod for error message use.
3693 if (method_type == METHOD_INTERFACE &&
3694 res_method->GetDeclaringClass()->IsObjectClass() &&
3695 !res_method->IsPublic()) {
3696 Fail(VERIFY_ERROR_NO_METHOD) << "invoke-interface " << klass->PrettyDescriptor() << "."
3697 << dex_file_->GetMethodName(method_id) << " "
3698 << dex_file_->GetMethodSignature(method_id) << " resolved to "
3699 << "non-public object method " << res_method->PrettyMethod() << " "
3700 << "but non-public Object methods are excluded from interface "
3701 << "method resolution.";
3702 return nullptr;
3703 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003704 // Check if access is allowed.
Ian Rogersad0b3a32012-04-16 14:50:24 -07003705 if (!referrer.CanAccessMember(res_method->GetDeclaringClass(), res_method->GetAccessFlags())) {
David Sehr709b0702016-10-13 09:12:37 -07003706 Fail(VERIFY_ERROR_ACCESS_METHOD) << "illegal method access (call "
3707 << res_method->PrettyMethod()
Ian Rogersad0b3a32012-04-16 14:50:24 -07003708 << " from " << referrer << ")";
jeffhaob57e9522012-04-26 18:08:21 -07003709 return res_method;
jeffhao8cd6dda2012-02-22 10:15:34 -08003710 }
jeffhaode0d9c92012-02-27 13:58:13 -08003711 // Check that invoke-virtual and invoke-super are not used on private methods of the same class.
Alex Light7268d472016-01-20 15:50:01 -08003712 if (res_method->IsPrivate() && (method_type == METHOD_VIRTUAL || method_type == METHOD_SUPER)) {
jeffhaod5347e02012-03-22 17:25:05 -07003713 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invoke-super/virtual can't be used on private method "
David Sehr709b0702016-10-13 09:12:37 -07003714 << res_method->PrettyMethod();
Ian Rogers7b078e82014-09-10 14:44:24 -07003715 return nullptr;
jeffhaode0d9c92012-02-27 13:58:13 -08003716 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003717 // See if the method type implied by the invoke instruction matches the access flags for the
Orion Hodsoncfa325e2016-10-13 10:25:54 +01003718 // target method. The flags for METHOD_POLYMORPHIC are based on there being precisely two
3719 // signature polymorphic methods supported by the run-time which are native methods with variable
3720 // arguments.
Brian Carlstrombe6fa5e2014-12-09 20:15:42 -08003721 if ((method_type == METHOD_DIRECT && (!res_method->IsDirect() || res_method->IsStatic())) ||
Ian Rogersd81871c2011-10-03 13:57:23 -07003722 (method_type == METHOD_STATIC && !res_method->IsStatic()) ||
Alex Light7268d472016-01-20 15:50:01 -08003723 ((method_type == METHOD_SUPER ||
3724 method_type == METHOD_VIRTUAL ||
Orion Hodsoncfa325e2016-10-13 10:25:54 +01003725 method_type == METHOD_INTERFACE) && res_method->IsDirect()) ||
3726 ((method_type == METHOD_POLYMORPHIC) &&
3727 (!res_method->IsNative() || !res_method->IsVarargs()))) {
Ian Rogers2fc14272012-08-30 10:56:57 -07003728 Fail(VERIFY_ERROR_CLASS_CHANGE) << "invoke type (" << method_type << ") does not match method "
Orion Hodsoncfa325e2016-10-13 10:25:54 +01003729 "type of " << res_method->PrettyMethod();
Ian Rogers7b078e82014-09-10 14:44:24 -07003730 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003731 }
Alex Lightafb66472017-08-01 09:54:49 -07003732 // Make sure we weren't expecting to fail.
3733 DCHECK(!must_fail) << "invoke type (" << method_type << ")"
3734 << klass->PrettyDescriptor() << "."
3735 << dex_file_->GetMethodName(method_id) << " "
3736 << dex_file_->GetMethodSignature(method_id) << " unexpectedly resolved to "
3737 << res_method->PrettyMethod() << " without error. Initially this method was "
3738 << "not found so we were expecting to fail for some reason.";
jeffhao8cd6dda2012-02-22 10:15:34 -08003739 return res_method;
3740}
3741
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003742template <class T>
Mathieu Chartiere401d142015-04-22 13:56:20 -07003743ArtMethod* MethodVerifier::VerifyInvocationArgsFromIterator(
3744 T* it, const Instruction* inst, MethodType method_type, bool is_range, ArtMethod* res_method) {
Andreas Gampee383d232018-06-19 12:29:51 -07003745 DCHECK_EQ(!is_range, inst->HasVarArgs());
3746
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003747 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
3748 // match the call to the signature. Also, we might be calling through an abstract method
3749 // definition (which doesn't have register count values).
Orion Hodsoncfa325e2016-10-13 10:25:54 +01003750 const size_t expected_args = inst->VRegA();
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003751 /* caught by static verifier */
3752 DCHECK(is_range || expected_args <= 5);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003753
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08003754 if (expected_args > code_item_accessor_.OutsSize()) {
Orion Hodson1cda7c22017-08-10 13:06:45 +01003755 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08003756 << ") exceeds outsSize ("
3757 << code_item_accessor_.OutsSize() << ")";
Orion Hodson1cda7c22017-08-10 13:06:45 +01003758 return nullptr;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003759 }
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003760
3761 /*
3762 * Check the "this" argument, which must be an instance of the class that declared the method.
3763 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
3764 * rigorous check here (which is okay since we have to do it at runtime).
3765 */
3766 if (method_type != METHOD_STATIC) {
Orion Hodsoncfa325e2016-10-13 10:25:54 +01003767 const RegType& actual_arg_type = work_line_->GetInvocationThis(this, inst);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003768 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
3769 CHECK(have_pending_hard_failure_);
3770 return nullptr;
3771 }
David Brazdil68b5c0b2016-01-19 14:25:29 +00003772 bool is_init = false;
3773 if (actual_arg_type.IsUninitializedTypes()) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003774 if (res_method) {
3775 if (!res_method->IsConstructor()) {
3776 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
3777 return nullptr;
3778 }
3779 } else {
3780 // Check whether the name of the called method is "<init>"
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00003781 const uint32_t method_idx = GetMethodIdxOfInvoke(inst);
Jeff Hao0d087272014-08-04 14:47:17 -07003782 if (strcmp(dex_file_->GetMethodName(dex_file_->GetMethodId(method_idx)), "<init>") != 0) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003783 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
3784 return nullptr;
3785 }
3786 }
David Brazdil68b5c0b2016-01-19 14:25:29 +00003787 is_init = true;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003788 }
David Brazdil68b5c0b2016-01-19 14:25:29 +00003789 const RegType& adjusted_type = is_init
3790 ? GetRegTypeCache()->FromUninitialized(actual_arg_type)
3791 : actual_arg_type;
Andreas Gampeeed3a5d2017-11-29 14:58:34 -08003792 if (method_type != METHOD_INTERFACE && !adjusted_type.IsZeroOrNull()) {
Ian Rogersd8f69b02014-09-10 21:43:52 +00003793 const RegType* res_method_class;
Andreas Gamped9e23012015-06-04 22:19:58 -07003794 // Miranda methods have the declaring interface as their declaring class, not the abstract
3795 // class. It would be wrong to use this for the type check (interface type checks are
3796 // postponed to runtime).
3797 if (res_method != nullptr && !res_method->IsMiranda()) {
Vladimir Markod93e3742018-07-18 10:58:13 +01003798 ObjPtr<mirror::Class> klass = res_method->GetDeclaringClass();
Ian Rogers1ff3c982014-08-12 02:30:58 -07003799 std::string temp;
Andreas Gampef23f33d2015-06-23 14:18:17 -07003800 res_method_class = &FromClass(klass->GetDescriptor(&temp), klass,
3801 klass->CannotBeAssignedFromOtherTypes());
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003802 } else {
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00003803 const uint32_t method_idx = GetMethodIdxOfInvoke(inst);
Andreas Gampea5b09a62016-11-17 15:21:22 -08003804 const dex::TypeIndex class_idx = dex_file_->GetMethodId(method_idx).class_idx_;
Mathieu Chartierde40d472015-10-15 17:47:48 -07003805 res_method_class = &reg_types_.FromDescriptor(
3806 GetClassLoader(),
3807 dex_file_->StringByTypeIdx(class_idx),
3808 false);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003809 }
David Brazdilca3c8c32016-09-06 14:04:48 +01003810 if (!res_method_class->IsAssignableFrom(adjusted_type, this)) {
David Brazdil68b5c0b2016-01-19 14:25:29 +00003811 Fail(adjusted_type.IsUnresolvedTypes()
3812 ? VERIFY_ERROR_NO_CLASS
3813 : VERIFY_ERROR_BAD_CLASS_SOFT)
3814 << "'this' argument '" << actual_arg_type << "' not instance of '"
3815 << *res_method_class << "'";
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003816 // Continue on soft failures. We need to find possible hard failures to avoid problems in
3817 // the compiler.
3818 if (have_pending_hard_failure_) {
3819 return nullptr;
3820 }
3821 }
3822 }
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003823 }
3824
Orion Hodsoncfa325e2016-10-13 10:25:54 +01003825 uint32_t arg[5];
3826 if (!is_range) {
3827 inst->GetVarArgs(arg);
3828 }
3829 uint32_t sig_registers = (method_type == METHOD_STATIC) ? 0 : 1;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003830 for ( ; it->HasNext(); it->Next()) {
3831 if (sig_registers >= expected_args) {
3832 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, expected " << inst->VRegA() <<
Orion Hodsoncfa325e2016-10-13 10:25:54 +01003833 " argument registers, method signature has " << sig_registers + 1 << " or more";
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003834 return nullptr;
3835 }
3836
3837 const char* param_descriptor = it->GetDescriptor();
3838
3839 if (param_descriptor == nullptr) {
3840 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation because of missing signature "
3841 "component";
3842 return nullptr;
3843 }
3844
Ian Rogersd8f69b02014-09-10 21:43:52 +00003845 const RegType& reg_type = reg_types_.FromDescriptor(GetClassLoader(), param_descriptor, false);
Orion Hodsoncfa325e2016-10-13 10:25:54 +01003846 uint32_t get_reg = is_range ? inst->VRegC() + static_cast<uint32_t>(sig_registers) :
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003847 arg[sig_registers];
3848 if (reg_type.IsIntegralTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003849 const RegType& src_type = work_line_->GetRegisterType(this, get_reg);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003850 if (!src_type.IsIntegralTypes()) {
3851 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register v" << get_reg << " has type " << src_type
3852 << " but expected " << reg_type;
Andreas Gampeb588f4c2015-05-26 13:35:39 -07003853 return nullptr;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003854 }
Andreas Gampeda9badb2015-06-05 20:22:12 -07003855 } else {
3856 if (!work_line_->VerifyRegisterType(this, get_reg, reg_type)) {
3857 // Continue on soft failures. We need to find possible hard failures to avoid problems in
3858 // the compiler.
3859 if (have_pending_hard_failure_) {
3860 return nullptr;
3861 }
3862 } else if (reg_type.IsLongOrDoubleTypes()) {
3863 // Check that registers are consecutive (for non-range invokes). Invokes are the only
3864 // instructions not specifying register pairs by the first component, but require them
3865 // nonetheless. Only check when there's an actual register in the parameters. If there's
3866 // none, this will fail below.
3867 if (!is_range && sig_registers + 1 < expected_args) {
3868 uint32_t second_reg = arg[sig_registers + 1];
3869 if (second_reg != get_reg + 1) {
3870 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, long or double parameter "
3871 "at index " << sig_registers << " is not a pair: " << get_reg << " + "
3872 << second_reg << ".";
3873 return nullptr;
3874 }
3875 }
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003876 }
3877 }
3878 sig_registers += reg_type.IsLongOrDoubleTypes() ? 2 : 1;
3879 }
3880 if (expected_args != sig_registers) {
3881 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, expected " << expected_args <<
Orion Hodsoncfa325e2016-10-13 10:25:54 +01003882 " argument registers, method signature has " << sig_registers;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003883 return nullptr;
3884 }
3885 return res_method;
3886}
3887
3888void MethodVerifier::VerifyInvocationArgsUnresolvedMethod(const Instruction* inst,
3889 MethodType method_type,
3890 bool is_range) {
3891 // As the method may not have been resolved, make this static check against what we expect.
3892 // The main reason for this code block is to fail hard when we find an illegal use, e.g.,
3893 // wrong number of arguments or wrong primitive types, even if the method could not be resolved.
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00003894 const uint32_t method_idx = GetMethodIdxOfInvoke(inst);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003895 DexFileParameterIterator it(*dex_file_,
3896 dex_file_->GetProtoId(dex_file_->GetMethodId(method_idx).proto_idx_));
Orion Hodsoncfa325e2016-10-13 10:25:54 +01003897 VerifyInvocationArgsFromIterator(&it, inst, method_type, is_range, nullptr);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003898}
3899
Orion Hodsonc069a302017-01-18 09:23:12 +00003900bool MethodVerifier::CheckCallSite(uint32_t call_site_idx) {
Orion Hodson3a842f52017-04-21 15:24:10 +01003901 if (call_site_idx >= dex_file_->NumCallSiteIds()) {
3902 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Bad call site id #" << call_site_idx
3903 << " >= " << dex_file_->NumCallSiteIds();
3904 return false;
3905 }
3906
Orion Hodsonc069a302017-01-18 09:23:12 +00003907 CallSiteArrayValueIterator it(*dex_file_, dex_file_->GetCallSiteId(call_site_idx));
3908 // Check essential arguments are provided. The dex file verifier has verified indicies of the
3909 // main values (method handle, name, method_type).
Orion Hodson4c8e12e2018-05-18 08:33:20 +01003910 static const size_t kRequiredArguments = 3;
3911 if (it.Size() < kRequiredArguments) {
Orion Hodsonc069a302017-01-18 09:23:12 +00003912 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Call site #" << call_site_idx
3913 << " has too few arguments: "
Orion Hodson4c8e12e2018-05-18 08:33:20 +01003914 << it.Size() << " < " << kRequiredArguments;
Orion Hodsonc069a302017-01-18 09:23:12 +00003915 return false;
3916 }
3917
Orion Hodson4c8e12e2018-05-18 08:33:20 +01003918 std::pair<const EncodedArrayValueIterator::ValueType, size_t> type_and_max[kRequiredArguments] =
3919 { { EncodedArrayValueIterator::ValueType::kMethodHandle, dex_file_->NumMethodHandles() },
3920 { EncodedArrayValueIterator::ValueType::kString, dex_file_->NumStringIds() },
3921 { EncodedArrayValueIterator::ValueType::kMethodType, dex_file_->NumProtoIds() }
3922 };
3923 uint32_t index[kRequiredArguments];
3924
3925 // Check arguments have expected types and are within permitted ranges.
3926 for (size_t i = 0; i < kRequiredArguments; ++i) {
3927 if (it.GetValueType() != type_and_max[i].first) {
3928 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Call site id #" << call_site_idx
3929 << " argument " << i << " has wrong type "
3930 << it.GetValueType() << "!=" << type_and_max[i].first;
3931 return false;
3932 }
3933 index[i] = static_cast<uint32_t>(it.GetJavaValue().i);
3934 if (index[i] >= type_and_max[i].second) {
3935 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Call site id #" << call_site_idx
3936 << " argument " << i << " bad index "
3937 << index[i] << " >= " << type_and_max[i].second;
3938 return false;
3939 }
3940 it.Next();
Orion Hodsona5dca522018-02-27 12:42:11 +00003941 }
Orion Hodson3a842f52017-04-21 15:24:10 +01003942
Orion Hodson4c8e12e2018-05-18 08:33:20 +01003943 // Check method handle kind is valid.
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08003944 const dex::MethodHandleItem& mh = dex_file_->GetMethodHandle(index[0]);
Orion Hodsonc069a302017-01-18 09:23:12 +00003945 if (mh.method_handle_type_ != static_cast<uint16_t>(DexFile::MethodHandleType::kInvokeStatic)) {
3946 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Call site #" << call_site_idx
Orion Hodson631827d2017-04-10 14:53:47 +01003947 << " argument 0 method handle type is not InvokeStatic: "
3948 << mh.method_handle_type_;
Orion Hodsonc069a302017-01-18 09:23:12 +00003949 return false;
3950 }
Orion Hodsonc069a302017-01-18 09:23:12 +00003951 return true;
3952}
3953
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003954class MethodParamListDescriptorIterator {
3955 public:
Mathieu Chartiere401d142015-04-22 13:56:20 -07003956 explicit MethodParamListDescriptorIterator(ArtMethod* res_method) :
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003957 res_method_(res_method), pos_(0), params_(res_method->GetParameterTypeList()),
3958 params_size_(params_ == nullptr ? 0 : params_->Size()) {
3959 }
3960
3961 bool HasNext() {
3962 return pos_ < params_size_;
3963 }
3964
3965 void Next() {
3966 ++pos_;
3967 }
3968
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07003969 const char* GetDescriptor() REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003970 return res_method_->GetTypeDescriptorFromTypeIdx(params_->GetTypeItem(pos_).type_idx_);
3971 }
3972
3973 private:
Mathieu Chartiere401d142015-04-22 13:56:20 -07003974 ArtMethod* res_method_;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003975 size_t pos_;
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08003976 const dex::TypeList* params_;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003977 const size_t params_size_;
3978};
3979
Mathieu Chartiere401d142015-04-22 13:56:20 -07003980ArtMethod* MethodVerifier::VerifyInvocationArgs(
Alex Light7268d472016-01-20 15:50:01 -08003981 const Instruction* inst, MethodType method_type, bool is_range) {
jeffhao8cd6dda2012-02-22 10:15:34 -08003982 // Resolve the method. This could be an abstract or concrete method depending on what sort of call
3983 // we're making.
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00003984 const uint32_t method_idx = GetMethodIdxOfInvoke(inst);
Alex Light7268d472016-01-20 15:50:01 -08003985 ArtMethod* res_method = ResolveMethodAndCheckAccess(method_idx, method_type);
Ian Rogers7b078e82014-09-10 14:44:24 -07003986 if (res_method == nullptr) { // error or class is unresolved
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003987 // Check what we can statically.
3988 if (!have_pending_hard_failure_) {
3989 VerifyInvocationArgsUnresolvedMethod(inst, method_type, is_range);
3990 }
3991 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003992 }
3993
Ian Rogersd81871c2011-10-03 13:57:23 -07003994 // If we're using invoke-super(method), make sure that the executing method's class' superclass
Alex Light705ad492015-09-21 11:36:30 -07003995 // has a vtable entry for the target method. Or the target is on a interface.
Alex Light7268d472016-01-20 15:50:01 -08003996 if (method_type == METHOD_SUPER) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08003997 dex::TypeIndex class_idx = dex_file_->GetMethodId(method_idx).class_idx_;
David Brazdilca3c8c32016-09-06 14:04:48 +01003998 const RegType& reference_type = reg_types_.FromDescriptor(
3999 GetClassLoader(),
4000 dex_file_->StringByTypeIdx(class_idx),
4001 false);
4002 if (reference_type.IsUnresolvedTypes()) {
Alex Lightfedd91d2016-01-07 14:49:16 -08004003 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "Unable to find referenced class from invoke-super";
4004 return nullptr;
4005 }
David Brazdilca3c8c32016-09-06 14:04:48 +01004006 if (reference_type.GetClass()->IsInterface()) {
Alex Lightfedd91d2016-01-07 14:49:16 -08004007 // TODO Can we verify anything else.
David Brazdil15fc7292016-09-02 14:13:18 +01004008 if (class_idx == class_def_.class_idx_) {
Alex Lightfedd91d2016-01-07 14:49:16 -08004009 Fail(VERIFY_ERROR_CLASS_CHANGE) << "Cannot invoke-super on self as interface";
Alex Light55ea94d2016-03-15 09:50:26 -07004010 return nullptr;
Alex Lightfedd91d2016-01-07 14:49:16 -08004011 }
4012 // TODO Revisit whether we want to allow invoke-super on direct interfaces only like the JLS
4013 // does.
Alex Light55ea94d2016-03-15 09:50:26 -07004014 if (!GetDeclaringClass().HasClass()) {
4015 Fail(VERIFY_ERROR_NO_CLASS) << "Unable to resolve the full class of 'this' used in an"
4016 << "interface invoke-super";
4017 return nullptr;
David Brazdilca3c8c32016-09-06 14:04:48 +01004018 } else if (!reference_type.IsStrictlyAssignableFrom(GetDeclaringClass(), this)) {
Alex Lightfedd91d2016-01-07 14:49:16 -08004019 Fail(VERIFY_ERROR_CLASS_CHANGE)
David Sehr709b0702016-10-13 09:12:37 -07004020 << "invoke-super in " << mirror::Class::PrettyClass(GetDeclaringClass().GetClass())
4021 << " in method "
4022 << dex_file_->PrettyMethod(dex_method_idx_) << " to method "
4023 << dex_file_->PrettyMethod(method_idx) << " references "
4024 << "non-super-interface type " << mirror::Class::PrettyClass(reference_type.GetClass());
Alex Lightfedd91d2016-01-07 14:49:16 -08004025 return nullptr;
Alex Light705ad492015-09-21 11:36:30 -07004026 }
4027 } else {
4028 const RegType& super = GetDeclaringClass().GetSuperClass(&reg_types_);
4029 if (super.IsUnresolvedTypes()) {
4030 Fail(VERIFY_ERROR_NO_METHOD) << "unknown super class in invoke-super from "
David Sehr709b0702016-10-13 09:12:37 -07004031 << dex_file_->PrettyMethod(dex_method_idx_)
4032 << " to super " << res_method->PrettyMethod();
Alex Light705ad492015-09-21 11:36:30 -07004033 return nullptr;
4034 }
David Brazdilca3c8c32016-09-06 14:04:48 +01004035 if (!reference_type.IsStrictlyAssignableFrom(GetDeclaringClass(), this) ||
Aart Bikf663e342016-04-04 17:28:59 -07004036 (res_method->GetMethodIndex() >= super.GetClass()->GetVTableLength())) {
Alex Light705ad492015-09-21 11:36:30 -07004037 Fail(VERIFY_ERROR_NO_METHOD) << "invalid invoke-super from "
David Sehr709b0702016-10-13 09:12:37 -07004038 << dex_file_->PrettyMethod(dex_method_idx_)
Alex Light705ad492015-09-21 11:36:30 -07004039 << " to super " << super
4040 << "." << res_method->GetName()
4041 << res_method->GetSignature();
4042 return nullptr;
4043 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004044 }
4045 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08004046
Andreas Gampe74979b12017-05-16 09:28:06 -07004047 if (UNLIKELY(method_type == METHOD_POLYMORPHIC)) {
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004048 // Process the signature of the calling site that is invoking the method handle.
Orion Hodson06d10a72018-05-14 08:53:38 +01004049 dex::ProtoIndex proto_idx(inst->VRegH());
4050 DexFileParameterIterator it(*dex_file_, dex_file_->GetProtoId(proto_idx));
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004051 return VerifyInvocationArgsFromIterator(&it, inst, method_type, is_range, res_method);
4052 } else {
4053 // Process the target method's signature.
4054 MethodParamListDescriptorIterator it(res_method);
4055 return VerifyInvocationArgsFromIterator(&it, inst, method_type, is_range, res_method);
4056 }
4057}
4058
4059bool MethodVerifier::CheckSignaturePolymorphicMethod(ArtMethod* method) {
Vladimir Markod93e3742018-07-18 10:58:13 +01004060 ObjPtr<mirror::Class> klass = method->GetDeclaringClass();
Orion Hodsonfe92d122018-01-02 10:45:17 +00004061 const char* method_name = method->GetName();
4062
4063 const char* expected_return_descriptor;
Vladimir Markoc7aa87e2018-05-24 15:19:52 +01004064 ObjPtr<mirror::ObjectArray<mirror::Class>> class_roots =
4065 Runtime::Current()->GetClassLinker()->GetClassRoots();
4066 if (klass == GetClassRoot<mirror::MethodHandle>(class_roots)) {
Orion Hodsonfe92d122018-01-02 10:45:17 +00004067 expected_return_descriptor = mirror::MethodHandle::GetReturnTypeDescriptor(method_name);
Vladimir Markoc7aa87e2018-05-24 15:19:52 +01004068 } else if (klass == GetClassRoot<mirror::VarHandle>(class_roots)) {
Orion Hodsonfe92d122018-01-02 10:45:17 +00004069 expected_return_descriptor = mirror::VarHandle::GetReturnTypeDescriptor(method_name);
4070 } else {
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004071 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
Orion Hodsonfe92d122018-01-02 10:45:17 +00004072 << "Signature polymorphic method in unsuppported class: " << klass->PrettyDescriptor();
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004073 return false;
4074 }
4075
Orion Hodsonfe92d122018-01-02 10:45:17 +00004076 if (expected_return_descriptor == nullptr) {
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004077 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
4078 << "Signature polymorphic method name invalid: " << method_name;
4079 return false;
4080 }
4081
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08004082 const dex::TypeList* types = method->GetParameterTypeList();
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004083 if (types->Size() != 1) {
4084 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
4085 << "Signature polymorphic method has too many arguments " << types->Size() << " != 1";
4086 return false;
4087 }
4088
4089 const dex::TypeIndex argument_type_index = types->GetTypeItem(0).type_idx_;
4090 const char* argument_descriptor = method->GetTypeDescriptorFromTypeIdx(argument_type_index);
4091 if (strcmp(argument_descriptor, "[Ljava/lang/Object;") != 0) {
4092 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
4093 << "Signature polymorphic method has unexpected argument type: " << argument_descriptor;
4094 return false;
4095 }
4096
4097 const char* return_descriptor = method->GetReturnTypeDescriptor();
Orion Hodsonfe92d122018-01-02 10:45:17 +00004098 if (strcmp(return_descriptor, expected_return_descriptor) != 0) {
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004099 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
Orion Hodsonfe92d122018-01-02 10:45:17 +00004100 << "Signature polymorphic method has unexpected return type: " << return_descriptor
4101 << " != " << expected_return_descriptor;
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004102 return false;
4103 }
4104
4105 return true;
4106}
4107
4108bool MethodVerifier::CheckSignaturePolymorphicReceiver(const Instruction* inst) {
4109 const RegType& this_type = work_line_->GetInvocationThis(this, inst);
Andreas Gampeeed3a5d2017-11-29 14:58:34 -08004110 if (this_type.IsZeroOrNull()) {
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004111 /* null pointer always passes (and always fails at run time) */
4112 return true;
4113 } else if (!this_type.IsNonZeroReferenceTypes()) {
4114 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
4115 << "invoke-polymorphic receiver is not a reference: "
4116 << this_type;
4117 return false;
4118 } else if (this_type.IsUninitializedReference()) {
4119 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
4120 << "invoke-polymorphic receiver is uninitialized: "
4121 << this_type;
4122 return false;
4123 } else if (!this_type.HasClass()) {
4124 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
4125 << "invoke-polymorphic receiver has no class: "
4126 << this_type;
4127 return false;
Vladimir Markoc7aa87e2018-05-24 15:19:52 +01004128 } else {
4129 ObjPtr<mirror::ObjectArray<mirror::Class>> class_roots =
4130 Runtime::Current()->GetClassLinker()->GetClassRoots();
4131 if (!this_type.GetClass()->IsSubClass(GetClassRoot<mirror::MethodHandle>(class_roots)) &&
4132 !this_type.GetClass()->IsSubClass(GetClassRoot<mirror::VarHandle>(class_roots))) {
4133 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
4134 << "invoke-polymorphic receiver is not a subclass of MethodHandle or VarHandle: "
4135 << this_type;
4136 return false;
4137 }
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004138 }
4139 return true;
Ian Rogersd81871c2011-10-03 13:57:23 -07004140}
4141
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00004142uint16_t MethodVerifier::GetMethodIdxOfInvoke(const Instruction* inst) {
4143 switch (inst->Opcode()) {
4144 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK:
4145 case Instruction::INVOKE_VIRTUAL_QUICK: {
4146 DCHECK(Runtime::Current()->IsStarted() || verify_to_dump_)
4147 << dex_file_->PrettyMethod(dex_method_idx_, true) << "@" << work_insn_idx_;
4148 DCHECK(method_being_verified_ != nullptr);
4149 uint16_t method_idx = method_being_verified_->GetIndexFromQuickening(work_insn_idx_);
4150 CHECK_NE(method_idx, DexFile::kDexNoIndex16);
4151 return method_idx;
4152 }
4153 default: {
4154 return inst->VRegB();
4155 }
Mathieu Chartier091d2382015-03-06 10:59:06 -08004156 }
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004157}
4158
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00004159uint16_t MethodVerifier::GetFieldIdxOfFieldAccess(const Instruction* inst, bool is_static) {
4160 if (is_static) {
4161 return inst->VRegB_21c();
4162 } else if (inst->IsQuickened()) {
4163 DCHECK(Runtime::Current()->IsStarted() || verify_to_dump_);
4164 DCHECK(method_being_verified_ != nullptr);
4165 uint16_t field_idx = method_being_verified_->GetIndexFromQuickening(work_insn_idx_);
4166 CHECK_NE(field_idx, DexFile::kDexNoIndex16);
4167 return field_idx;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004168 } else {
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00004169 return inst->VRegC_22c();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004170 }
4171}
4172
Ian Rogers62342ec2013-06-11 10:26:37 -07004173void MethodVerifier::VerifyNewArray(const Instruction* inst, bool is_filled, bool is_range) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08004174 dex::TypeIndex type_idx;
Sebastien Hertz5243e912013-05-21 10:55:07 +02004175 if (!is_filled) {
4176 DCHECK_EQ(inst->Opcode(), Instruction::NEW_ARRAY);
Andreas Gampea5b09a62016-11-17 15:21:22 -08004177 type_idx = dex::TypeIndex(inst->VRegC_22c());
Sebastien Hertz5243e912013-05-21 10:55:07 +02004178 } else if (!is_range) {
4179 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY);
Andreas Gampea5b09a62016-11-17 15:21:22 -08004180 type_idx = dex::TypeIndex(inst->VRegB_35c());
Sebastien Hertz5243e912013-05-21 10:55:07 +02004181 } else {
4182 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY_RANGE);
Andreas Gampea5b09a62016-11-17 15:21:22 -08004183 type_idx = dex::TypeIndex(inst->VRegB_3rc());
Sebastien Hertz5243e912013-05-21 10:55:07 +02004184 }
Andreas Gampe98be1a92017-08-28 08:25:45 -07004185 const RegType& res_type = ResolveClass<CheckAccess::kYes>(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004186 if (res_type.IsConflict()) { // bad class
4187 DCHECK_NE(failures_.size(), 0U);
Ian Rogers0c4a5062012-02-03 15:18:59 -08004188 } else {
4189 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
4190 if (!res_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07004191 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "new-array on non-array class " << res_type;
Ian Rogers0c4a5062012-02-03 15:18:59 -08004192 } else if (!is_filled) {
4193 /* make sure "size" register is valid type */
Ian Rogers7b078e82014-09-10 14:44:24 -07004194 work_line_->VerifyRegisterType(this, inst->VRegB_22c(), reg_types_.Integer());
Ian Rogers0c4a5062012-02-03 15:18:59 -08004195 /* set register type to array class */
Ian Rogersd8f69b02014-09-10 21:43:52 +00004196 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
Andreas Gampead238ce2015-08-24 21:13:08 -07004197 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_22c(), precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08004198 } else {
Andreas Gampebb18a032016-03-22 20:34:25 -07004199 DCHECK(!res_type.IsUnresolvedMergedReference());
Ian Rogers0c4a5062012-02-03 15:18:59 -08004200 // Verify each register. If "arg_count" is bad, VerifyRegisterType() will run off the end of
4201 // the list and fail. It's legal, if silly, for arg_count to be zero.
Ian Rogersd8f69b02014-09-10 21:43:52 +00004202 const RegType& expected_type = reg_types_.GetComponentType(res_type, GetClassLoader());
Sebastien Hertz5243e912013-05-21 10:55:07 +02004203 uint32_t arg_count = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
4204 uint32_t arg[5];
4205 if (!is_range) {
Ian Rogers29a26482014-05-02 15:27:29 -07004206 inst->GetVarArgs(arg);
Sebastien Hertz5243e912013-05-21 10:55:07 +02004207 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08004208 for (size_t ui = 0; ui < arg_count; ui++) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02004209 uint32_t get_reg = is_range ? inst->VRegC_3rc() + ui : arg[ui];
Ian Rogers7b078e82014-09-10 14:44:24 -07004210 if (!work_line_->VerifyRegisterType(this, get_reg, expected_type)) {
4211 work_line_->SetResultRegisterType(this, reg_types_.Conflict());
Ian Rogers0c4a5062012-02-03 15:18:59 -08004212 return;
4213 }
4214 }
4215 // filled-array result goes into "result" register
Ian Rogersd8f69b02014-09-10 21:43:52 +00004216 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
Ian Rogers7b078e82014-09-10 14:44:24 -07004217 work_line_->SetResultRegisterType(this, precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08004218 }
4219 }
4220}
4221
Sebastien Hertz5243e912013-05-21 10:55:07 +02004222void MethodVerifier::VerifyAGet(const Instruction* inst,
Ian Rogersd8f69b02014-09-10 21:43:52 +00004223 const RegType& insn_type, bool is_primitive) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004224 const RegType& index_type = work_line_->GetRegisterType(this, inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07004225 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07004226 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07004227 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004228 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegB_23x());
Andreas Gampeeed3a5d2017-11-29 14:58:34 -08004229 if (array_type.IsZeroOrNull()) {
Ian Rogers89310de2012-02-01 13:47:30 -08004230 // Null array class; this code path will fail at runtime. Infer a merge-able type from the
Andreas Gampe52f205a2017-12-01 12:16:07 -08004231 // instruction type.
4232 if (!is_primitive) {
4233 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), reg_types_.Null());
4234 } else if (insn_type.IsInteger()) {
4235 // Pick a non-zero constant (to distinguish with null) that can fit in any primitive.
4236 // We cannot use 'insn_type' as it could be a float array or an int array.
4237 work_line_->SetRegisterType<LockOp::kClear>(
4238 this, inst->VRegA_23x(), DetermineCat1Constant(1, need_precise_constants_));
4239 } else if (insn_type.IsCategory1Types()) {
4240 // Category 1
4241 // The 'insn_type' is exactly the type we need.
4242 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), insn_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07004243 } else {
Ian Rogers89310de2012-02-01 13:47:30 -08004244 // Category 2
Ian Rogers7b078e82014-09-10 14:44:24 -07004245 work_line_->SetRegisterTypeWide(this, inst->VRegA_23x(),
4246 reg_types_.FromCat2ConstLo(0, false),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004247 reg_types_.FromCat2ConstHi(0, false));
Ian Rogers89310de2012-02-01 13:47:30 -08004248 }
jeffhaofc3144e2012-02-01 17:21:15 -08004249 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07004250 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aget";
Andreas Gampe8d8fc482016-03-25 16:24:20 -07004251 } else if (array_type.IsUnresolvedMergedReference()) {
Andreas Gampebb18a032016-03-22 20:34:25 -07004252 // Unresolved array types must be reference array types.
4253 if (is_primitive) {
4254 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type
4255 << " source for category 1 aget";
4256 } else {
4257 Fail(VERIFY_ERROR_NO_CLASS) << "cannot verify aget for " << array_type
4258 << " because of missing class";
Andreas Gampe8d8fc482016-03-25 16:24:20 -07004259 // Approximate with java.lang.Object[].
4260 work_line_->SetRegisterType<LockOp::kClear>(this,
4261 inst->VRegA_23x(),
4262 reg_types_.JavaLangObject(false));
Andreas Gampebb18a032016-03-22 20:34:25 -07004263 }
Ian Rogers89310de2012-02-01 13:47:30 -08004264 } else {
4265 /* verify the class */
Ian Rogersd8f69b02014-09-10 21:43:52 +00004266 const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
jeffhaofc3144e2012-02-01 17:21:15 -08004267 if (!component_type.IsReferenceTypes() && !is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07004268 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08004269 << " source for aget-object";
4270 } else if (component_type.IsNonZeroReferenceTypes() && is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07004271 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08004272 << " source for category 1 aget";
4273 } else if (is_primitive && !insn_type.Equals(component_type) &&
4274 !((insn_type.IsInteger() && component_type.IsFloat()) ||
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004275 (insn_type.IsLong() && component_type.IsDouble()))) {
4276 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array type " << array_type
4277 << " incompatible with aget of type " << insn_type;
Ian Rogers89310de2012-02-01 13:47:30 -08004278 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004279 // Use knowledge of the field type which is stronger than the type inferred from the
4280 // instruction, which can't differentiate object types and ints from floats, longs from
4281 // doubles.
4282 if (!component_type.IsLowHalf()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07004283 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), component_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004284 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004285 work_line_->SetRegisterTypeWide(this, inst->VRegA_23x(), component_type,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004286 component_type.HighHalf(&reg_types_));
4287 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004288 }
4289 }
4290 }
4291}
4292
Ian Rogersd8f69b02014-09-10 21:43:52 +00004293void MethodVerifier::VerifyPrimitivePut(const RegType& target_type, const RegType& insn_type,
Jeff Haofe1f7c82013-08-01 14:50:24 -07004294 const uint32_t vregA) {
4295 // Primitive assignability rules are weaker than regular assignability rules.
4296 bool instruction_compatible;
4297 bool value_compatible;
Ian Rogers7b078e82014-09-10 14:44:24 -07004298 const RegType& value_type = work_line_->GetRegisterType(this, vregA);
Jeff Haofe1f7c82013-08-01 14:50:24 -07004299 if (target_type.IsIntegralTypes()) {
Jeff Haoa4647482013-08-06 15:35:47 -07004300 instruction_compatible = target_type.Equals(insn_type);
Jeff Haofe1f7c82013-08-01 14:50:24 -07004301 value_compatible = value_type.IsIntegralTypes();
4302 } else if (target_type.IsFloat()) {
4303 instruction_compatible = insn_type.IsInteger(); // no put-float, so expect put-int
4304 value_compatible = value_type.IsFloatTypes();
4305 } else if (target_type.IsLong()) {
4306 instruction_compatible = insn_type.IsLong();
Andreas Gampe376fa682014-09-07 13:06:12 -07004307 // Additional register check: this is not checked statically (as part of VerifyInstructions),
4308 // as target_type depends on the resolved type of the field.
4309 if (instruction_compatible && work_line_->NumRegs() > vregA + 1) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004310 const RegType& value_type_hi = work_line_->GetRegisterType(this, vregA + 1);
Andreas Gampe376fa682014-09-07 13:06:12 -07004311 value_compatible = value_type.IsLongTypes() && value_type.CheckWidePair(value_type_hi);
4312 } else {
4313 value_compatible = false;
4314 }
Jeff Haofe1f7c82013-08-01 14:50:24 -07004315 } else if (target_type.IsDouble()) {
4316 instruction_compatible = insn_type.IsLong(); // no put-double, so expect put-long
Andreas Gampe376fa682014-09-07 13:06:12 -07004317 // Additional register check: this is not checked statically (as part of VerifyInstructions),
4318 // as target_type depends on the resolved type of the field.
4319 if (instruction_compatible && work_line_->NumRegs() > vregA + 1) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004320 const RegType& value_type_hi = work_line_->GetRegisterType(this, vregA + 1);
Andreas Gampe376fa682014-09-07 13:06:12 -07004321 value_compatible = value_type.IsDoubleTypes() && value_type.CheckWidePair(value_type_hi);
4322 } else {
4323 value_compatible = false;
4324 }
Jeff Haofe1f7c82013-08-01 14:50:24 -07004325 } else {
4326 instruction_compatible = false; // reference with primitive store
4327 value_compatible = false; // unused
4328 }
4329 if (!instruction_compatible) {
4330 // This is a global failure rather than a class change failure as the instructions and
4331 // the descriptors for the type should have been consistent within the same file at
4332 // compile time.
4333 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "put insn has type '" << insn_type
4334 << "' but expected type '" << target_type << "'";
4335 return;
4336 }
4337 if (!value_compatible) {
4338 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
4339 << " of type " << value_type << " but expected " << target_type << " for put";
4340 return;
4341 }
4342}
4343
Sebastien Hertz5243e912013-05-21 10:55:07 +02004344void MethodVerifier::VerifyAPut(const Instruction* inst,
Ian Rogersd8f69b02014-09-10 21:43:52 +00004345 const RegType& insn_type, bool is_primitive) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004346 const RegType& index_type = work_line_->GetRegisterType(this, inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07004347 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07004348 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07004349 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004350 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegB_23x());
Andreas Gampeeed3a5d2017-11-29 14:58:34 -08004351 if (array_type.IsZeroOrNull()) {
Nicolas Geoffray66389fb2015-06-19 10:35:42 +01004352 // Null array type; this code path will fail at runtime.
4353 // Still check that the given value matches the instruction's type.
Andreas Gampe4bf4c782015-08-14 14:07:43 -07004354 // Note: this is, as usual, complicated by the fact the the instruction isn't fully typed
4355 // and fits multiple register types.
4356 const RegType* modified_reg_type = &insn_type;
4357 if ((modified_reg_type == &reg_types_.Integer()) ||
4358 (modified_reg_type == &reg_types_.LongLo())) {
4359 // May be integer or float | long or double. Overwrite insn_type accordingly.
4360 const RegType& value_type = work_line_->GetRegisterType(this, inst->VRegA_23x());
4361 if (modified_reg_type == &reg_types_.Integer()) {
4362 if (&value_type == &reg_types_.Float()) {
4363 modified_reg_type = &value_type;
4364 }
4365 } else {
4366 if (&value_type == &reg_types_.DoubleLo()) {
4367 modified_reg_type = &value_type;
4368 }
4369 }
4370 }
4371 work_line_->VerifyRegisterType(this, inst->VRegA_23x(), *modified_reg_type);
jeffhaofc3144e2012-02-01 17:21:15 -08004372 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07004373 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aput";
Andreas Gampe8d8fc482016-03-25 16:24:20 -07004374 } else if (array_type.IsUnresolvedMergedReference()) {
Andreas Gampebb18a032016-03-22 20:34:25 -07004375 // Unresolved array types must be reference array types.
4376 if (is_primitive) {
4377 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "put insn has type '" << insn_type
4378 << "' but unresolved type '" << array_type << "'";
4379 } else {
4380 Fail(VERIFY_ERROR_NO_CLASS) << "cannot verify aput for " << array_type
4381 << " because of missing class";
4382 }
Ian Rogers89310de2012-02-01 13:47:30 -08004383 } else {
Ian Rogersd8f69b02014-09-10 21:43:52 +00004384 const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
Jeff Haofe1f7c82013-08-01 14:50:24 -07004385 const uint32_t vregA = inst->VRegA_23x();
Jeff Haob24b4a72013-07-31 13:47:31 -07004386 if (is_primitive) {
Jeff Haofe1f7c82013-08-01 14:50:24 -07004387 VerifyPrimitivePut(component_type, insn_type, vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07004388 } else {
Jeff Haob24b4a72013-07-31 13:47:31 -07004389 if (!component_type.IsReferenceTypes()) {
4390 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
4391 << " source for aput-object";
4392 } else {
4393 // The instruction agrees with the type of array, confirm the value to be stored does too
4394 // Note: we use the instruction type (rather than the component type) for aput-object as
4395 // incompatible classes will be caught at runtime as an array store exception
Ian Rogers7b078e82014-09-10 14:44:24 -07004396 work_line_->VerifyRegisterType(this, vregA, insn_type);
Jeff Haob24b4a72013-07-31 13:47:31 -07004397 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004398 }
4399 }
4400 }
4401}
4402
Mathieu Chartierc7853442015-03-27 14:35:38 -07004403ArtField* MethodVerifier::GetStaticField(int field_idx) {
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08004404 const dex::FieldId& field_id = dex_file_->GetFieldId(field_idx);
Ian Rogers90040192011-12-16 08:54:29 -08004405 // Check access to class
Andreas Gampe98be1a92017-08-28 08:25:45 -07004406 const RegType& klass_type = ResolveClass<CheckAccess::kYes>(field_id.class_idx_);
Brian Carlstrom7934ac22013-07-26 10:54:15 -07004407 if (klass_type.IsConflict()) { // bad class
Ian Rogersad0b3a32012-04-16 14:50:24 -07004408 AppendToLastFailMessage(StringPrintf(" in attempt to access static field %d (%s) in %s",
4409 field_idx, dex_file_->GetFieldName(field_id),
4410 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers7b078e82014-09-10 14:44:24 -07004411 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08004412 }
Elliott Hughesb25c3f62012-03-26 16:35:06 -07004413 if (klass_type.IsUnresolvedTypes()) {
Andreas Gampe629be512017-08-25 17:09:32 -07004414 // Accessibility checks depend on resolved fields.
David Brazdil2bb2fbd2018-11-13 18:24:26 +00004415 DCHECK(klass_type.Equals(GetDeclaringClass()) ||
4416 !failures_.empty() ||
4417 IsSdkVersionSetAndLessThan(api_level_, SdkVersion::kP));
Andreas Gampe629be512017-08-25 17:09:32 -07004418
Ian Rogers7b078e82014-09-10 14:44:24 -07004419 return nullptr; // Can't resolve Class so no more to do here, will do checking at runtime.
Ian Rogers90040192011-12-16 08:54:29 -08004420 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07004421 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Vladimir Markoe11dd502017-12-08 14:09:45 +00004422 ArtField* field = class_linker->ResolveFieldJLS(field_idx, dex_cache_, class_loader_);
David Brazdilca3c8c32016-09-06 14:04:48 +01004423
4424 // Record result of the field resolution attempt.
4425 VerifierDeps::MaybeRecordFieldResolution(*dex_file_, field_idx, field);
4426
Ian Rogers7b078e82014-09-10 14:44:24 -07004427 if (field == nullptr) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07004428 VLOG(verifier) << "Unable to resolve static field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07004429 << dex_file_->GetFieldName(field_id) << ") in "
4430 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogers7b078e82014-09-10 14:44:24 -07004431 DCHECK(self_->IsExceptionPending());
4432 self_->ClearException();
4433 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004434 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
4435 field->GetAccessFlags())) {
David Sehr709b0702016-10-13 09:12:37 -07004436 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access static field " << field->PrettyField()
Ian Rogersad0b3a32012-04-16 14:50:24 -07004437 << " from " << GetDeclaringClass();
Ian Rogers7b078e82014-09-10 14:44:24 -07004438 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07004439 } else if (!field->IsStatic()) {
David Sehr709b0702016-10-13 09:12:37 -07004440 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << field->PrettyField() << " to be static";
Ian Rogers7b078e82014-09-10 14:44:24 -07004441 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07004442 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07004443 return field;
Ian Rogersd81871c2011-10-03 13:57:23 -07004444}
4445
Mathieu Chartierc7853442015-03-27 14:35:38 -07004446ArtField* MethodVerifier::GetInstanceField(const RegType& obj_type, int field_idx) {
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08004447 const dex::FieldId& field_id = dex_file_->GetFieldId(field_idx);
Aart Bik31883642016-06-06 15:02:44 -07004448 // Check access to class.
Andreas Gampe98be1a92017-08-28 08:25:45 -07004449 const RegType& klass_type = ResolveClass<CheckAccess::kYes>(field_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004450 if (klass_type.IsConflict()) {
4451 AppendToLastFailMessage(StringPrintf(" in attempt to access instance field %d (%s) in %s",
4452 field_idx, dex_file_->GetFieldName(field_id),
4453 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers7b078e82014-09-10 14:44:24 -07004454 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08004455 }
jeffhao8cd6dda2012-02-22 10:15:34 -08004456 if (klass_type.IsUnresolvedTypes()) {
Andreas Gampe629be512017-08-25 17:09:32 -07004457 // Accessibility checks depend on resolved fields.
David Brazdil2bb2fbd2018-11-13 18:24:26 +00004458 DCHECK(klass_type.Equals(GetDeclaringClass()) ||
4459 !failures_.empty() ||
4460 IsSdkVersionSetAndLessThan(api_level_, SdkVersion::kP));
Andreas Gampe629be512017-08-25 17:09:32 -07004461
Ian Rogers7b078e82014-09-10 14:44:24 -07004462 return nullptr; // Can't resolve Class so no more to do here
Ian Rogers90040192011-12-16 08:54:29 -08004463 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07004464 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Vladimir Markoe11dd502017-12-08 14:09:45 +00004465 ArtField* field = class_linker->ResolveFieldJLS(field_idx, dex_cache_, class_loader_);
David Brazdilca3c8c32016-09-06 14:04:48 +01004466
4467 // Record result of the field resolution attempt.
4468 VerifierDeps::MaybeRecordFieldResolution(*dex_file_, field_idx, field);
4469
Ian Rogers7b078e82014-09-10 14:44:24 -07004470 if (field == nullptr) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07004471 VLOG(verifier) << "Unable to resolve instance field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07004472 << dex_file_->GetFieldName(field_id) << ") in "
4473 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogers7b078e82014-09-10 14:44:24 -07004474 DCHECK(self_->IsExceptionPending());
4475 self_->ClearException();
4476 return nullptr;
Andreas Gampeeed3a5d2017-11-29 14:58:34 -08004477 } else if (obj_type.IsZeroOrNull()) {
Aart Bik31883642016-06-06 15:02:44 -07004478 // Cannot infer and check type, however, access will cause null pointer exception.
4479 // Fall through into a few last soft failure checks below.
Stephen Kyle695c5982014-08-22 15:03:07 +01004480 } else if (!obj_type.IsReferenceTypes()) {
Aart Bik31883642016-06-06 15:02:44 -07004481 // Trying to read a field from something that isn't a reference.
Stephen Kyle695c5982014-08-22 15:03:07 +01004482 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance field access on object that has "
4483 << "non-reference type " << obj_type;
Ian Rogers7b078e82014-09-10 14:44:24 -07004484 return nullptr;
Ian Rogerse1758fe2012-04-19 11:31:15 -07004485 } else {
David Brazdil0d638bb2016-07-27 15:29:25 +01004486 std::string temp;
Mathieu Chartier3398c782016-09-30 10:27:43 -07004487 ObjPtr<mirror::Class> klass = field->GetDeclaringClass();
Ian Rogersd8f69b02014-09-10 21:43:52 +00004488 const RegType& field_klass =
Vladimir Markobcf17522018-06-01 13:14:32 +01004489 FromClass(klass->GetDescriptor(&temp), klass, klass->CannotBeAssignedFromOtherTypes());
David Brazdil68b5c0b2016-01-19 14:25:29 +00004490 if (obj_type.IsUninitializedTypes()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07004491 // Field accesses through uninitialized references are only allowable for constructors where
David Brazdil68b5c0b2016-01-19 14:25:29 +00004492 // the field is declared in this class.
4493 // Note: this IsConstructor check is technically redundant, as UninitializedThis should only
4494 // appear in constructors.
4495 if (!obj_type.IsUninitializedThisReference() ||
4496 !IsConstructor() ||
4497 !field_klass.Equals(GetDeclaringClass())) {
David Sehr709b0702016-10-13 09:12:37 -07004498 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "cannot access instance field " << field->PrettyField()
David Brazdil68b5c0b2016-01-19 14:25:29 +00004499 << " of a not fully initialized object within the context"
David Sehr709b0702016-10-13 09:12:37 -07004500 << " of " << dex_file_->PrettyMethod(dex_method_idx_);
David Brazdil68b5c0b2016-01-19 14:25:29 +00004501 return nullptr;
4502 }
David Brazdilca3c8c32016-09-06 14:04:48 +01004503 } else if (!field_klass.IsAssignableFrom(obj_type, this)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07004504 // Trying to access C1.field1 using reference of type C2, which is neither C1 or a sub-class
4505 // of C1. For resolution to occur the declared class of the field must be compatible with
4506 // obj_type, we've discovered this wasn't so, so report the field didn't exist.
Andreas Gampe66596242016-04-14 10:55:04 -07004507 VerifyError type;
4508 bool is_aot = Runtime::Current()->IsAotCompiler();
4509 if (is_aot && (field_klass.IsUnresolvedTypes() || obj_type.IsUnresolvedTypes())) {
4510 // Compiler & unresolved types involved, retry at runtime.
4511 type = VerifyError::VERIFY_ERROR_NO_CLASS;
4512 } else {
Andreas Gampe8f4ade02016-04-15 10:09:16 -07004513 // Classes known (resolved; and thus assignability check is precise), or we are at runtime
4514 // and still missing classes. This is a hard failure.
Andreas Gampe66596242016-04-14 10:55:04 -07004515 type = VerifyError::VERIFY_ERROR_BAD_CLASS_HARD;
4516 }
David Sehr709b0702016-10-13 09:12:37 -07004517 Fail(type) << "cannot access instance field " << field->PrettyField()
Andreas Gampe66596242016-04-14 10:55:04 -07004518 << " from object of type " << obj_type;
Ian Rogers7b078e82014-09-10 14:44:24 -07004519 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004520 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004521 }
Aart Bik31883642016-06-06 15:02:44 -07004522
4523 // Few last soft failure checks.
4524 if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
4525 field->GetAccessFlags())) {
David Sehr709b0702016-10-13 09:12:37 -07004526 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access instance field " << field->PrettyField()
Aart Bik31883642016-06-06 15:02:44 -07004527 << " from " << GetDeclaringClass();
4528 return nullptr;
4529 } else if (field->IsStatic()) {
David Sehr709b0702016-10-13 09:12:37 -07004530 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << field->PrettyField()
Aart Bik31883642016-06-06 15:02:44 -07004531 << " to not be static";
4532 return nullptr;
4533 }
4534
4535 return field;
Ian Rogersd81871c2011-10-03 13:57:23 -07004536}
4537
Andreas Gampe896df402014-10-20 22:25:29 -07004538template <MethodVerifier::FieldAccessType kAccType>
4539void MethodVerifier::VerifyISFieldAccess(const Instruction* inst, const RegType& insn_type,
4540 bool is_primitive, bool is_static) {
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00004541 uint32_t field_idx = GetFieldIdxOfFieldAccess(inst, is_static);
Mathieu Chartierc7853442015-03-27 14:35:38 -07004542 ArtField* field;
Ian Rogersb94a27b2011-10-26 00:33:41 -07004543 if (is_static) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07004544 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07004545 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004546 const RegType& object_type = work_line_->GetRegisterType(this, inst->VRegB_22c());
David Brazdil68b5c0b2016-01-19 14:25:29 +00004547
4548 // One is not allowed to access fields on uninitialized references, except to write to
4549 // fields in the constructor (before calling another constructor).
4550 // GetInstanceField does an assignability check which will fail for uninitialized types.
4551 // We thus modify the type if the uninitialized reference is a "this" reference (this also
4552 // checks at the same time that we're verifying a constructor).
4553 bool should_adjust = (kAccType == FieldAccessType::kAccPut) &&
4554 object_type.IsUninitializedThisReference();
4555 const RegType& adjusted_type = should_adjust
4556 ? GetRegTypeCache()->FromUninitialized(object_type)
4557 : object_type;
4558 field = GetInstanceField(adjusted_type, field_idx);
Andreas Gampe896df402014-10-20 22:25:29 -07004559 if (UNLIKELY(have_pending_hard_failure_)) {
4560 return;
4561 }
Alex Light4a2c8fc2016-02-12 11:01:54 -08004562 if (should_adjust) {
4563 if (field == nullptr) {
4564 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "Might be accessing a superclass instance field prior "
4565 << "to the superclass being initialized in "
David Sehr709b0702016-10-13 09:12:37 -07004566 << dex_file_->PrettyMethod(dex_method_idx_);
Alex Light4a2c8fc2016-02-12 11:01:54 -08004567 } else if (field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
4568 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "cannot access superclass instance field "
David Sehr709b0702016-10-13 09:12:37 -07004569 << field->PrettyField() << " of a not fully initialized "
Alex Light4a2c8fc2016-02-12 11:01:54 -08004570 << "object within the context of "
David Sehr709b0702016-10-13 09:12:37 -07004571 << dex_file_->PrettyMethod(dex_method_idx_);
Alex Light4a2c8fc2016-02-12 11:01:54 -08004572 return;
4573 }
4574 }
Ian Rogersb94a27b2011-10-26 00:33:41 -07004575 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00004576 const RegType* field_type = nullptr;
Ian Rogers7b078e82014-09-10 14:44:24 -07004577 if (field != nullptr) {
Andreas Gampe896df402014-10-20 22:25:29 -07004578 if (kAccType == FieldAccessType::kAccPut) {
4579 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
David Sehr709b0702016-10-13 09:12:37 -07004580 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << field->PrettyField()
Andreas Gampe896df402014-10-20 22:25:29 -07004581 << " from other class " << GetDeclaringClass();
Aart Bikc2bc2652016-05-23 14:58:49 -07004582 // Keep hunting for possible hard fails.
Andreas Gampe896df402014-10-20 22:25:29 -07004583 }
4584 }
4585
Mathieu Chartier3398c782016-09-30 10:27:43 -07004586 ObjPtr<mirror::Class> field_type_class =
Vladimir Marko208f6702017-12-08 12:00:50 +00004587 can_load_classes_ ? field->ResolveType() : field->LookupResolvedType();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004588 if (field_type_class != nullptr) {
Mathieu Chartier3398c782016-09-30 10:27:43 -07004589 field_type = &FromClass(field->GetTypeDescriptor(),
Vladimir Markobcf17522018-06-01 13:14:32 +01004590 field_type_class,
Andreas Gampef23f33d2015-06-23 14:18:17 -07004591 field_type_class->CannotBeAssignedFromOtherTypes());
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08004592 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004593 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
4594 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004595 }
David Brazdil2bb2fbd2018-11-13 18:24:26 +00004596 } else if (IsSdkVersionSetAndAtLeast(api_level_, SdkVersion::kP)) {
Andreas Gampe7da4c402017-08-25 11:30:48 -07004597 // If we don't have the field (it seems we failed resolution) and this is a PUT, we need to
4598 // redo verification at runtime as the field may be final, unless the field id shows it's in
4599 // the same class.
4600 //
4601 // For simplicity, it is OK to not distinguish compile-time vs runtime, and post this an
4602 // ACCESS_FIELD failure at runtime. This has the same effect as NO_FIELD - punting the class
4603 // to the access-checks interpreter.
4604 //
4605 // Note: see b/34966607. This and above may be changed in the future.
4606 if (kAccType == FieldAccessType::kAccPut) {
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08004607 const dex::FieldId& field_id = dex_file_->GetFieldId(field_idx);
Andreas Gampe7da4c402017-08-25 11:30:48 -07004608 const char* field_class_descriptor = dex_file_->GetFieldDeclaringClassDescriptor(field_id);
4609 const RegType* field_class_type = &reg_types_.FromDescriptor(GetClassLoader(),
4610 field_class_descriptor,
4611 false);
4612 if (!field_class_type->Equals(GetDeclaringClass())) {
4613 Fail(VERIFY_ERROR_ACCESS_FIELD) << "could not check field put for final field modify of "
4614 << field_class_descriptor
4615 << "."
4616 << dex_file_->GetFieldName(field_id)
4617 << " from other class "
4618 << GetDeclaringClass();
4619 }
4620 }
Ian Rogers0d604842012-04-16 14:50:24 -07004621 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004622 if (field_type == nullptr) {
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08004623 const dex::FieldId& field_id = dex_file_->GetFieldId(field_idx);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004624 const char* descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
Mathieu Chartierbf99f772014-08-23 16:37:27 -07004625 field_type = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004626 }
Sebastien Hertz757b3042014-03-28 14:34:28 +01004627 DCHECK(field_type != nullptr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02004628 const uint32_t vregA = (is_static) ? inst->VRegA_21c() : inst->VRegA_22c();
Andreas Gampe896df402014-10-20 22:25:29 -07004629 static_assert(kAccType == FieldAccessType::kAccPut || kAccType == FieldAccessType::kAccGet,
4630 "Unexpected third access type");
4631 if (kAccType == FieldAccessType::kAccPut) {
4632 // sput or iput.
4633 if (is_primitive) {
4634 VerifyPrimitivePut(*field_type, insn_type, vregA);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004635 } else {
David Brazdilca3c8c32016-09-06 14:04:48 +01004636 if (!insn_type.IsAssignableFrom(*field_type, this)) {
Vladimir Marko414000e2015-06-23 17:45:21 +01004637 // If the field type is not a reference, this is a global failure rather than
4638 // a class change failure as the instructions and the descriptors for the type
4639 // should have been consistent within the same file at compile time.
4640 VerifyError error = field_type->IsReferenceTypes() ? VERIFY_ERROR_BAD_CLASS_SOFT
4641 : VERIFY_ERROR_BAD_CLASS_HARD;
David Sehr709b0702016-10-13 09:12:37 -07004642 Fail(error) << "expected field " << ArtField::PrettyField(field)
Vladimir Marko414000e2015-06-23 17:45:21 +01004643 << " to be compatible with type '" << insn_type
4644 << "' but found type '" << *field_type
4645 << "' in put-object";
Andreas Gampe896df402014-10-20 22:25:29 -07004646 return;
4647 }
4648 work_line_->VerifyRegisterType(this, vregA, *field_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004649 }
Andreas Gampe896df402014-10-20 22:25:29 -07004650 } else if (kAccType == FieldAccessType::kAccGet) {
4651 // sget or iget.
4652 if (is_primitive) {
4653 if (field_type->Equals(insn_type) ||
4654 (field_type->IsFloat() && insn_type.IsInteger()) ||
4655 (field_type->IsDouble() && insn_type.IsLong())) {
4656 // expected that read is of the correct primitive type or that int reads are reading
4657 // floats or long reads are reading doubles
4658 } else {
4659 // This is a global failure rather than a class change failure as the instructions and
4660 // the descriptors for the type should have been consistent within the same file at
4661 // compile time
David Sehr709b0702016-10-13 09:12:37 -07004662 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << ArtField::PrettyField(field)
Andreas Gampe896df402014-10-20 22:25:29 -07004663 << " to be of type '" << insn_type
4664 << "' but found type '" << *field_type << "' in get";
4665 return;
4666 }
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08004667 } else {
David Brazdilca3c8c32016-09-06 14:04:48 +01004668 if (!insn_type.IsAssignableFrom(*field_type, this)) {
Vladimir Marko414000e2015-06-23 17:45:21 +01004669 // If the field type is not a reference, this is a global failure rather than
4670 // a class change failure as the instructions and the descriptors for the type
4671 // should have been consistent within the same file at compile time.
4672 VerifyError error = field_type->IsReferenceTypes() ? VERIFY_ERROR_BAD_CLASS_SOFT
4673 : VERIFY_ERROR_BAD_CLASS_HARD;
David Sehr709b0702016-10-13 09:12:37 -07004674 Fail(error) << "expected field " << ArtField::PrettyField(field)
Vladimir Marko414000e2015-06-23 17:45:21 +01004675 << " to be compatible with type '" << insn_type
4676 << "' but found type '" << *field_type
4677 << "' in get-object";
Andreas Gampe890da292015-07-06 17:20:18 -07004678 if (error != VERIFY_ERROR_BAD_CLASS_HARD) {
Andreas Gampead238ce2015-08-24 21:13:08 -07004679 work_line_->SetRegisterType<LockOp::kClear>(this, vregA, reg_types_.Conflict());
Andreas Gampe890da292015-07-06 17:20:18 -07004680 }
Andreas Gampe896df402014-10-20 22:25:29 -07004681 return;
4682 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004683 }
Andreas Gampe896df402014-10-20 22:25:29 -07004684 if (!field_type->IsLowHalf()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07004685 work_line_->SetRegisterType<LockOp::kClear>(this, vregA, *field_type);
Andreas Gampe896df402014-10-20 22:25:29 -07004686 } else {
4687 work_line_->SetRegisterTypeWide(this, vregA, *field_type, field_type->HighHalf(&reg_types_));
4688 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07004689 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004690 LOG(FATAL) << "Unexpected case.";
Ian Rogersd81871c2011-10-03 13:57:23 -07004691 }
4692}
4693
Ian Rogers776ac1f2012-04-13 23:36:36 -07004694bool MethodVerifier::CheckNotMoveException(const uint16_t* insns, int insn_idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004695 if ((insns[insn_idx] & 0xff) == Instruction::MOVE_EXCEPTION) {
jeffhaod5347e02012-03-22 17:25:05 -07004696 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-exception";
Ian Rogersd81871c2011-10-03 13:57:23 -07004697 return false;
4698 }
4699 return true;
4700}
4701
Stephen Kyle9bc61992014-09-22 13:53:15 +01004702bool MethodVerifier::CheckNotMoveResult(const uint16_t* insns, int insn_idx) {
4703 if (((insns[insn_idx] & 0xff) >= Instruction::MOVE_RESULT) &&
4704 ((insns[insn_idx] & 0xff) <= Instruction::MOVE_RESULT_OBJECT)) {
4705 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-result*";
4706 return false;
4707 }
4708 return true;
4709}
4710
4711bool MethodVerifier::CheckNotMoveExceptionOrMoveResult(const uint16_t* insns, int insn_idx) {
4712 return (CheckNotMoveException(insns, insn_idx) && CheckNotMoveResult(insns, insn_idx));
4713}
4714
Ian Rogersebbdd872014-07-07 23:53:08 -07004715bool MethodVerifier::UpdateRegisters(uint32_t next_insn, RegisterLine* merge_line,
4716 bool update_merge_line) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004717 bool changed = true;
4718 RegisterLine* target_line = reg_table_.GetLine(next_insn);
Mathieu Chartierde40d472015-10-15 17:47:48 -07004719 if (!GetInstructionFlags(next_insn).IsVisitedOrChanged()) {
jeffhaobdb76512011-09-07 11:43:16 -07004720 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07004721 * We haven't processed this instruction before, and we haven't touched the registers here, so
4722 * there's nothing to "merge". Copy the registers over and mark it as changed. (This is the
4723 * only way a register can transition out of "unknown", so this is not just an optimization.)
jeffhaobdb76512011-09-07 11:43:16 -07004724 */
Andreas Gampea727e372015-08-25 09:22:37 -07004725 target_line->CopyFromLine(merge_line);
Mathieu Chartierde40d472015-10-15 17:47:48 -07004726 if (GetInstructionFlags(next_insn).IsReturn()) {
Jeff Haob24b4a72013-07-31 13:47:31 -07004727 // Verify that the monitor stack is empty on return.
Andreas Gampea727e372015-08-25 09:22:37 -07004728 merge_line->VerifyMonitorStackEmpty(this);
4729
Ian Rogersb8c78592013-07-25 23:52:52 +00004730 // For returns we only care about the operand to the return, all other registers are dead.
4731 // Initialize them as conflicts so they don't add to GC and deoptimization information.
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08004732 const Instruction* ret_inst = &code_item_accessor_.InstructionAt(next_insn);
Andreas Gampea727e372015-08-25 09:22:37 -07004733 AdjustReturnLine(this, ret_inst, target_line);
Aart Bik31883642016-06-06 15:02:44 -07004734 // Directly bail if a hard failure was found.
Aart Bikb0526322016-06-01 14:06:00 -07004735 if (have_pending_hard_failure_) {
4736 return false;
4737 }
Ian Rogersb8c78592013-07-25 23:52:52 +00004738 }
jeffhaobdb76512011-09-07 11:43:16 -07004739 } else {
Mathieu Chartier361e04a2016-02-16 14:06:35 -08004740 RegisterLineArenaUniquePtr copy;
Andreas Gampe92d77202017-12-06 20:49:00 -08004741 if (UNLIKELY(VLOG_IS_ON(verifier_debug))) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07004742 copy.reset(RegisterLine::Create(target_line->NumRegs(), this));
Ian Rogers7b0c5b42012-02-16 15:29:07 -08004743 copy->CopyFromLine(target_line);
4744 }
Ian Rogers7b078e82014-09-10 14:44:24 -07004745 changed = target_line->MergeRegisters(this, merge_line);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004746 if (have_pending_hard_failure_) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004747 return false;
jeffhaobdb76512011-09-07 11:43:16 -07004748 }
Andreas Gampe92d77202017-12-06 20:49:00 -08004749 if (UNLIKELY(VLOG_IS_ON(verifier_debug)) && changed) {
Elliott Hughes398f64b2012-03-26 18:05:48 -07004750 LogVerifyInfo() << "Merging at [" << reinterpret_cast<void*>(work_insn_idx_) << "]"
Elliott Hughesc073b072012-05-24 19:29:17 -07004751 << " to [" << reinterpret_cast<void*>(next_insn) << "]: " << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07004752 << copy->Dump(this) << " MERGE\n"
4753 << merge_line->Dump(this) << " ==\n"
4754 << target_line->Dump(this) << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07004755 }
Ian Rogersebbdd872014-07-07 23:53:08 -07004756 if (update_merge_line && changed) {
4757 merge_line->CopyFromLine(target_line);
4758 }
jeffhaobdb76512011-09-07 11:43:16 -07004759 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004760 if (changed) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07004761 GetInstructionFlags(next_insn).SetChanged();
jeffhaobdb76512011-09-07 11:43:16 -07004762 }
4763 return true;
4764}
4765
Ian Rogers7b3ddd22013-02-21 15:19:52 -08004766InstructionFlags* MethodVerifier::CurrentInsnFlags() {
Mathieu Chartierde40d472015-10-15 17:47:48 -07004767 return &GetInstructionFlags(work_insn_idx_);
Ian Rogers776ac1f2012-04-13 23:36:36 -07004768}
4769
Ian Rogersd8f69b02014-09-10 21:43:52 +00004770const RegType& MethodVerifier::GetMethodReturnType() {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004771 if (return_type_ == nullptr) {
Nicolas Geoffrayb041a402017-11-13 15:16:22 +00004772 if (method_being_verified_ != nullptr) {
Vladimir Markob45528c2017-07-27 14:14:28 +01004773 ObjPtr<mirror::Class> return_type_class = can_load_classes_
Nicolas Geoffrayb041a402017-11-13 15:16:22 +00004774 ? method_being_verified_->ResolveReturnType()
4775 : method_being_verified_->LookupResolvedReturnType();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004776 if (return_type_class != nullptr) {
Nicolas Geoffrayb041a402017-11-13 15:16:22 +00004777 return_type_ = &FromClass(method_being_verified_->GetReturnTypeDescriptor(),
Vladimir Markobcf17522018-06-01 13:14:32 +01004778 return_type_class,
Andreas Gampef23f33d2015-06-23 14:18:17 -07004779 return_type_class->CannotBeAssignedFromOtherTypes());
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004780 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004781 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
4782 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004783 }
4784 }
4785 if (return_type_ == nullptr) {
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08004786 const dex::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
4787 const dex::ProtoId& proto_id = dex_file_->GetMethodPrototype(method_id);
Andreas Gampea5b09a62016-11-17 15:21:22 -08004788 dex::TypeIndex return_type_idx = proto_id.return_type_idx_;
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004789 const char* descriptor = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(return_type_idx));
Mathieu Chartierbf99f772014-08-23 16:37:27 -07004790 return_type_ = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004791 }
4792 }
4793 return *return_type_;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004794}
4795
Ian Rogersd8f69b02014-09-10 21:43:52 +00004796const RegType& MethodVerifier::GetDeclaringClass() {
Ian Rogers7b078e82014-09-10 14:44:24 -07004797 if (declaring_class_ == nullptr) {
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08004798 const dex::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
Brian Carlstrom93c33962013-07-26 10:37:43 -07004799 const char* descriptor
4800 = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(method_id.class_idx_));
Nicolas Geoffrayb041a402017-11-13 15:16:22 +00004801 if (method_being_verified_ != nullptr) {
Vladimir Marko2cffc5d2018-05-29 15:40:56 +01004802 ObjPtr<mirror::Class> klass = method_being_verified_->GetDeclaringClass();
Mathieu Chartierde40d472015-10-15 17:47:48 -07004803 declaring_class_ = &FromClass(descriptor, klass, klass->CannotBeAssignedFromOtherTypes());
Ian Rogers637c65b2013-05-31 11:46:00 -07004804 } else {
Mathieu Chartierbf99f772014-08-23 16:37:27 -07004805 declaring_class_ = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers637c65b2013-05-31 11:46:00 -07004806 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07004807 }
Ian Rogers637c65b2013-05-31 11:46:00 -07004808 return *declaring_class_;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004809}
4810
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004811std::vector<int32_t> MethodVerifier::DescribeVRegs(uint32_t dex_pc) {
4812 RegisterLine* line = reg_table_.GetLine(dex_pc);
Sebastien Hertzaa0c00c2014-03-14 17:58:54 +01004813 DCHECK(line != nullptr) << "No register line at DEX pc " << StringPrintf("0x%x", dex_pc);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004814 std::vector<int32_t> result;
4815 for (size_t i = 0; i < line->NumRegs(); ++i) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004816 const RegType& type = line->GetRegisterType(this, i);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004817 if (type.IsConstant()) {
4818 result.push_back(type.IsPreciseConstant() ? kConstant : kImpreciseConstant);
Ian Rogers7b078e82014-09-10 14:44:24 -07004819 const ConstantType* const_val = down_cast<const ConstantType*>(&type);
4820 result.push_back(const_val->ConstantValue());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004821 } else if (type.IsConstantLo()) {
4822 result.push_back(type.IsPreciseConstantLo() ? kConstant : kImpreciseConstant);
Ian Rogers7b078e82014-09-10 14:44:24 -07004823 const ConstantType* const_val = down_cast<const ConstantType*>(&type);
4824 result.push_back(const_val->ConstantValueLo());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004825 } else if (type.IsConstantHi()) {
4826 result.push_back(type.IsPreciseConstantHi() ? kConstant : kImpreciseConstant);
Ian Rogers7b078e82014-09-10 14:44:24 -07004827 const ConstantType* const_val = down_cast<const ConstantType*>(&type);
4828 result.push_back(const_val->ConstantValueHi());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004829 } else if (type.IsIntegralTypes()) {
4830 result.push_back(kIntVReg);
4831 result.push_back(0);
4832 } else if (type.IsFloat()) {
4833 result.push_back(kFloatVReg);
4834 result.push_back(0);
4835 } else if (type.IsLong()) {
4836 result.push_back(kLongLoVReg);
4837 result.push_back(0);
4838 result.push_back(kLongHiVReg);
4839 result.push_back(0);
4840 ++i;
4841 } else if (type.IsDouble()) {
4842 result.push_back(kDoubleLoVReg);
4843 result.push_back(0);
4844 result.push_back(kDoubleHiVReg);
4845 result.push_back(0);
4846 ++i;
4847 } else if (type.IsUndefined() || type.IsConflict() || type.IsHighHalf()) {
4848 result.push_back(kUndefined);
4849 result.push_back(0);
4850 } else {
Ian Rogers7b3ddd22013-02-21 15:19:52 -08004851 CHECK(type.IsNonZeroReferenceTypes());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004852 result.push_back(kReferenceVReg);
4853 result.push_back(0);
4854 }
4855 }
4856 return result;
4857}
4858
Ian Rogersd8f69b02014-09-10 21:43:52 +00004859const RegType& MethodVerifier::DetermineCat1Constant(int32_t value, bool precise) {
Sebastien Hertz849600b2013-12-20 10:28:08 +01004860 if (precise) {
4861 // Precise constant type.
4862 return reg_types_.FromCat1Const(value, true);
4863 } else {
4864 // Imprecise constant type.
4865 if (value < -32768) {
4866 return reg_types_.IntConstant();
4867 } else if (value < -128) {
4868 return reg_types_.ShortConstant();
4869 } else if (value < 0) {
4870 return reg_types_.ByteConstant();
4871 } else if (value == 0) {
4872 return reg_types_.Zero();
4873 } else if (value == 1) {
4874 return reg_types_.One();
4875 } else if (value < 128) {
4876 return reg_types_.PosByteConstant();
4877 } else if (value < 32768) {
4878 return reg_types_.PosShortConstant();
4879 } else if (value < 65536) {
4880 return reg_types_.CharConstant();
4881 } else {
4882 return reg_types_.IntConstant();
4883 }
4884 }
4885}
4886
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004887void MethodVerifier::Init() {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08004888 art::verifier::RegTypeCache::Init();
Brian Carlstrome7d856b2012-01-11 18:10:55 -08004889}
4890
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004891void MethodVerifier::Shutdown() {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08004892 verifier::RegTypeCache::ShutDown();
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08004893}
jeffhaod1224c72012-02-29 13:43:08 -08004894
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -07004895void MethodVerifier::VisitStaticRoots(RootVisitor* visitor) {
4896 RegTypeCache::VisitStaticRoots(visitor);
Mathieu Chartier7c438b12014-09-12 17:01:24 -07004897}
4898
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -07004899void MethodVerifier::VisitRoots(RootVisitor* visitor, const RootInfo& root_info) {
4900 reg_types_.VisitRoots(visitor, root_info);
Mathieu Chartierc528dba2013-11-26 12:00:11 -08004901}
4902
Andreas Gampef23f33d2015-06-23 14:18:17 -07004903const RegType& MethodVerifier::FromClass(const char* descriptor,
Vladimir Marko2cffc5d2018-05-29 15:40:56 +01004904 ObjPtr<mirror::Class> klass,
Andreas Gampef23f33d2015-06-23 14:18:17 -07004905 bool precise) {
4906 DCHECK(klass != nullptr);
4907 if (precise && !klass->IsInstantiable() && !klass->IsPrimitive()) {
4908 Fail(VerifyError::VERIFY_ERROR_NO_CLASS) << "Could not create precise reference for "
4909 << "non-instantiable klass " << descriptor;
4910 precise = false;
4911 }
4912 return reg_types_.FromClass(descriptor, klass, precise);
4913}
4914
Ian Rogersd81871c2011-10-03 13:57:23 -07004915} // namespace verifier
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07004916} // namespace art