blob: 4e0d4a64bf353af11c24152ea9c5cc2903c55aba [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070016
Mathieu Chartierc645f1d2014-03-06 18:11:53 -080017#include "method_verifier-inl.h"
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070018
Andreas Gampe4a57d5f2019-04-03 12:31:18 -070019#include <ostream>
Elliott Hughes1f359b02011-07-17 14:27:17 -070020
Andreas Gampe46ee31b2016-12-14 10:11:49 -080021#include "android-base/stringprintf.h"
22
Mathieu Chartierc7853442015-03-27 14:35:38 -070023#include "art_field-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070024#include "art_method-inl.h"
Andreas Gampe39b378c2017-12-07 15:44:13 -080025#include "base/aborting.h"
Andreas Gampe542451c2016-07-26 09:02:02 -070026#include "base/enums.h"
David Sehr67bf42e2018-02-26 16:43:04 -080027#include "base/leb128.h"
David Sehr9c4a0152018-04-05 12:23:54 -070028#include "base/indenter.h"
Andreas Gampe57943812017-12-06 21:39:13 -080029#include "base/logging.h" // For VLOG.
Ian Rogers637c65b2013-05-31 11:46:00 -070030#include "base/mutex-inl.h"
David Brazdil2bb2fbd2018-11-13 18:24:26 +000031#include "base/sdk_version.h"
Vladimir Marko637ee0b2015-09-04 12:47:41 +010032#include "base/stl_util.h"
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -080033#include "base/systrace.h"
Vladimir Marko80afd022015-05-19 18:08:00 +010034#include "base/time_utils.h"
David Sehrc431b9d2018-03-02 12:01:51 -080035#include "base/utils.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070036#include "class_linker.h"
Vladimir Markoc7aa87e2018-05-24 15:19:52 +010037#include "class_root.h"
Vladimir Marko2b5eaa22013-12-13 13:59:30 +000038#include "compiler_callbacks.h"
Mathieu Chartier1f1cb9f2018-06-04 09:22:46 -070039#include "dex/class_accessor-inl.h"
David Sehrb2ec9f52018-02-21 13:20:31 -080040#include "dex/descriptors_names.h"
David Sehr9e734c72018-01-04 17:56:19 -080041#include "dex/dex_file-inl.h"
42#include "dex/dex_file_exception_helpers.h"
43#include "dex/dex_instruction-inl.h"
44#include "dex/dex_instruction_utils.h"
Alex Lighteb7c1442015-08-31 13:17:42 -070045#include "experimental_flags.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070046#include "gc/accounting/card_table-inl.h"
Andreas Gampe6d7abbd2017-04-24 13:19:09 -070047#include "handle_scope-inl.h"
Ian Rogers84fa0742011-10-25 18:13:30 -070048#include "intern_table.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080049#include "mirror/class-inl.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070050#include "mirror/class.h"
Andreas Gampe51de69e2019-04-19 15:14:14 -070051#include "mirror/class_loader.h"
Ian Rogers39ebcb82013-05-30 16:57:23 -070052#include "mirror/dex_cache-inl.h"
Orion Hodsoncfa325e2016-10-13 10:25:54 +010053#include "mirror/method_handle_impl.h"
Orion Hodson2e599942017-09-22 16:17:41 +010054#include "mirror/method_type.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080055#include "mirror/object-inl.h"
56#include "mirror/object_array-inl.h"
Orion Hodsonfe92d122018-01-02 10:45:17 +000057#include "mirror/var_handle.h"
Andreas Gampe51de69e2019-04-19 15:14:14 -070058#include "obj_ptr-inl.h"
Ian Rogers7b078e82014-09-10 14:44:24 -070059#include "reg_type-inl.h"
Ian Rogers39ebcb82013-05-30 16:57:23 -070060#include "register_line-inl.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070061#include "runtime.h"
Andreas Gampe2ad6cce2019-04-11 16:17:39 -070062#include "scoped_newline.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070063#include "scoped_thread_state_change-inl.h"
Andreas Gampe513061a2017-06-01 09:17:34 -070064#include "stack.h"
Nicolas Geoffrayb041a402017-11-13 15:16:22 +000065#include "vdex_file.h"
Andreas Gampe6d7abbd2017-04-24 13:19:09 -070066#include "verifier_compiler_binding.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070067#include "verifier_deps.h"
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070068
69namespace art {
Ian Rogersd81871c2011-10-03 13:57:23 -070070namespace verifier {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070071
Andreas Gampe46ee31b2016-12-14 10:11:49 -080072using android::base::StringPrintf;
73
Mathieu Chartier8e219ae2014-08-19 14:29:46 -070074static constexpr bool kTimeVerifyMethod = !kIsDebugBuild;
Ian Rogers2c8a8572011-10-24 17:11:36 -070075
Andreas Gampeebf850c2015-08-14 15:37:35 -070076// On VLOG(verifier), should we dump the whole state when we run into a hard failure?
77static constexpr bool kDumpRegLinesOnHardFailureIfVLOG = true;
78
Vladimir Marko69d310e2017-10-09 14:12:23 +010079PcToRegisterLineTable::PcToRegisterLineTable(ScopedArenaAllocator& allocator)
80 : register_lines_(allocator.Adapter(kArenaAllocVerifier)) {}
Mathieu Chartierde40d472015-10-15 17:47:48 -070081
Ian Rogers7b3ddd22013-02-21 15:19:52 -080082void PcToRegisterLineTable::Init(RegisterTrackingMode mode, InstructionFlags* flags,
Ian Rogersd81871c2011-10-03 13:57:23 -070083 uint32_t insns_size, uint16_t registers_size,
Ian Rogers776ac1f2012-04-13 23:36:36 -070084 MethodVerifier* verifier) {
Ian Rogersd81871c2011-10-03 13:57:23 -070085 DCHECK_GT(insns_size, 0U);
Mathieu Chartierde40d472015-10-15 17:47:48 -070086 register_lines_.resize(insns_size);
Ian Rogersd81871c2011-10-03 13:57:23 -070087 for (uint32_t i = 0; i < insns_size; i++) {
88 bool interesting = false;
89 switch (mode) {
90 case kTrackRegsAll:
91 interesting = flags[i].IsOpcode();
92 break;
Sameer Abu Asal02c42232013-04-30 12:09:45 -070093 case kTrackCompilerInterestPoints:
Brian Carlstrom02c8cc62013-07-18 15:54:44 -070094 interesting = flags[i].IsCompileTimeInfoPoint() || flags[i].IsBranchTarget();
Ian Rogersd81871c2011-10-03 13:57:23 -070095 break;
96 case kTrackRegsBranches:
97 interesting = flags[i].IsBranchTarget();
98 break;
99 default:
100 break;
101 }
102 if (interesting) {
Mathieu Chartierde40d472015-10-15 17:47:48 -0700103 register_lines_[i].reset(RegisterLine::Create(registers_size, verifier));
Ian Rogersd0fbd852013-09-24 18:17:04 -0700104 }
105 }
106}
107
Mathieu Chartierde40d472015-10-15 17:47:48 -0700108PcToRegisterLineTable::~PcToRegisterLineTable() {}
Ian Rogersd81871c2011-10-03 13:57:23 -0700109
Andreas Gampe7c038102014-10-27 20:08:46 -0700110// Note: returns true on failure.
Mathieu Chartierc2a2bda2018-03-12 11:21:52 -0700111inline bool MethodVerifier::FailOrAbort(bool condition,
112 const char* error_msg,
113 uint32_t work_insn_idx) {
Andreas Gampe7c038102014-10-27 20:08:46 -0700114 if (kIsDebugBuild) {
Andreas Gampef8f36c22016-11-19 14:51:52 -0800115 // In a debug build, abort if the error condition is wrong. Only warn if
116 // we are already aborting (as this verification is likely run to print
117 // lock information).
118 if (LIKELY(gAborting == 0)) {
Mathieu Chartierc2a2bda2018-03-12 11:21:52 -0700119 DCHECK(condition) << error_msg << work_insn_idx << " "
120 << dex_file_->PrettyMethod(dex_method_idx_);
Andreas Gampef8f36c22016-11-19 14:51:52 -0800121 } else {
122 if (!condition) {
123 LOG(ERROR) << error_msg << work_insn_idx;
Mathieu Chartierc2a2bda2018-03-12 11:21:52 -0700124 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << error_msg << work_insn_idx;
Andreas Gampef8f36c22016-11-19 14:51:52 -0800125 return true;
126 }
127 }
Andreas Gampe7c038102014-10-27 20:08:46 -0700128 } else {
129 // In a non-debug build, just fail the class.
130 if (!condition) {
Mathieu Chartierc2a2bda2018-03-12 11:21:52 -0700131 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << error_msg << work_insn_idx;
Andreas Gampe7c038102014-10-27 20:08:46 -0700132 return true;
133 }
134 }
135
136 return false;
137}
138
Andreas Gampe6d7abbd2017-04-24 13:19:09 -0700139static FailureKind FailureKindMax(FailureKind fk1, FailureKind fk2) {
140 static_assert(FailureKind::kNoFailure < FailureKind::kSoftFailure
141 && FailureKind::kSoftFailure < FailureKind::kHardFailure,
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800142 "Unexpected FailureKind order");
143 return std::max(fk1, fk2);
144}
145
146void MethodVerifier::FailureData::Merge(const MethodVerifier::FailureData& fd) {
147 kind = FailureKindMax(kind, fd.kind);
148 types |= fd.types;
149}
150
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800151static bool IsLargeMethod(const CodeItemDataAccessor& accessor) {
152 if (!accessor.HasCodeItem()) {
Andreas Gampe3c651fc2015-05-21 14:06:46 -0700153 return false;
154 }
155
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800156 uint16_t registers_size = accessor.RegistersSize();
157 uint32_t insns_size = accessor.InsnsSizeInCodeUnits();
Andreas Gampea4f5bf62015-05-18 20:50:12 -0700158
159 return registers_size * insns_size > 4*1024*1024;
160}
161
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800162MethodVerifier::FailureData MethodVerifier::VerifyMethod(Thread* self,
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800163 uint32_t method_idx,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800164 const DexFile* dex_file,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700165 Handle<mirror::DexCache> dex_cache,
166 Handle<mirror::ClassLoader> class_loader,
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800167 const dex::ClassDef& class_def,
168 const dex::CodeItem* code_item,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700169 ArtMethod* method,
Jeff Haoee988952013-04-16 14:23:47 -0700170 uint32_t method_access_flags,
Andreas Gampe53e32d12015-12-09 21:03:23 -0800171 CompilerCallbacks* callbacks,
Ian Rogers46960fe2014-05-23 10:43:43 -0700172 bool allow_soft_failures,
Andreas Gampe5fd66d02016-09-12 20:22:19 -0700173 HardFailLogMode log_level,
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800174 bool need_precise_constants,
Andreas Gampe6cc23ac2018-08-24 15:22:43 -0700175 uint32_t api_level,
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800176 std::string* hard_failure_msg) {
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800177 MethodVerifier::FailureData result;
Mathieu Chartier8e219ae2014-08-19 14:29:46 -0700178 uint64_t start_ns = kTimeVerifyMethod ? NanoTime() : 0;
Ian Rogersc8982582012-09-07 16:53:25 -0700179
Andreas Gampebf9611f2016-03-25 16:58:00 -0700180 MethodVerifier verifier(self,
181 dex_file,
182 dex_cache,
183 class_loader,
184 class_def,
185 code_item,
186 method_idx,
187 method,
188 method_access_flags,
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700189 /* can_load_classes= */ true,
Andreas Gampebf9611f2016-03-25 16:58:00 -0700190 allow_soft_failures,
191 need_precise_constants,
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700192 /* verify to dump */ false,
193 /* allow_thread_suspension= */ true,
Andreas Gampe6cc23ac2018-08-24 15:22:43 -0700194 api_level);
Ian Rogers46960fe2014-05-23 10:43:43 -0700195 if (verifier.Verify()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700196 // Verification completed, however failures may be pending that didn't cause the verification
197 // to hard fail.
Ian Rogers46960fe2014-05-23 10:43:43 -0700198 CHECK(!verifier.have_pending_hard_failure_);
Andreas Gampe53e32d12015-12-09 21:03:23 -0800199
200 if (code_item != nullptr && callbacks != nullptr) {
201 // Let the interested party know that the method was verified.
202 callbacks->MethodVerified(&verifier);
203 }
204
Ian Rogers46960fe2014-05-23 10:43:43 -0700205 if (verifier.failures_.size() != 0) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -0700206 if (VLOG_IS_ON(verifier)) {
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800207 verifier.DumpFailures(VLOG_STREAM(verifier) << "Soft verification failures in "
David Sehr709b0702016-10-13 09:12:37 -0700208 << dex_file->PrettyMethod(method_idx) << "\n");
Anwar Ghuloum75a43f12013-08-13 17:22:14 -0700209 }
Andreas Gampe92d77202017-12-06 20:49:00 -0800210 if (VLOG_IS_ON(verifier_debug)) {
Andreas Gampe4a57d5f2019-04-03 12:31:18 -0700211 LOG(INFO) << verifier.info_messages_.str();
212 verifier.Dump(LOG_STREAM(INFO));
Andreas Gampe92d77202017-12-06 20:49:00 -0800213 }
Andreas Gampe6d7abbd2017-04-24 13:19:09 -0700214 result.kind = FailureKind::kSoftFailure;
Nicolas Geoffray250a3782016-04-20 16:27:53 +0100215 if (method != nullptr &&
216 !CanCompilerHandleVerificationFailure(verifier.encountered_failure_types_)) {
Alex Lightfcbafb32017-02-02 15:09:54 -0800217 method->SetDontCompile();
Nicolas Geoffray250a3782016-04-20 16:27:53 +0100218 }
219 }
Andreas Gampe56fdd0e2016-04-28 14:56:54 -0700220 if (method != nullptr) {
221 if (verifier.HasInstructionThatWillThrow()) {
Alex Lightfcbafb32017-02-02 15:09:54 -0800222 method->SetDontCompile();
Calin Juravle857f0582016-12-20 14:36:59 +0000223 if (Runtime::Current()->IsAotCompiler() &&
224 (callbacks != nullptr) && !callbacks->IsBootImage()) {
225 // When compiling apps, make HasInstructionThatWillThrow a soft error to trigger
226 // re-verification at runtime.
227 // The dead code after the throw is not verified and might be invalid. This may cause
228 // the JIT compiler to crash since it assumes that all the code is valid.
229 //
230 // There's a strong assumption that the entire boot image is verified and all its dex
231 // code is valid (even the dead and unverified one). As such this is done only for apps.
232 // (CompilerDriver DCHECKs in VerifyClassVisitor that methods from boot image are
233 // fully verified).
Andreas Gampe6d7abbd2017-04-24 13:19:09 -0700234 result.kind = FailureKind::kSoftFailure;
Calin Juravle857f0582016-12-20 14:36:59 +0000235 }
Andreas Gampe56fdd0e2016-04-28 14:56:54 -0700236 }
237 if ((verifier.encountered_failure_types_ & VerifyError::VERIFY_ERROR_LOCKING) != 0) {
Orion Hodsoncfcc9cf2017-09-29 15:07:27 +0100238 method->SetMustCountLocks();
Andreas Gampe56fdd0e2016-04-28 14:56:54 -0700239 }
jeffhaof56197c2012-03-05 18:01:54 -0800240 }
241 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700242 // Bad method data.
Ian Rogers46960fe2014-05-23 10:43:43 -0700243 CHECK_NE(verifier.failures_.size(), 0U);
Igor Murashkin4d7b75f2015-07-21 17:03:36 -0700244
245 if (UNLIKELY(verifier.have_pending_experimental_failure_)) {
246 // Failed due to being forced into interpreter. This is ok because
247 // we just want to skip verification.
Andreas Gampe6d7abbd2017-04-24 13:19:09 -0700248 result.kind = FailureKind::kSoftFailure;
Igor Murashkin4d7b75f2015-07-21 17:03:36 -0700249 } else {
250 CHECK(verifier.have_pending_hard_failure_);
Andreas Gampe7fe30232016-03-25 16:58:00 -0700251 if (VLOG_IS_ON(verifier)) {
Andreas Gampe5fd66d02016-09-12 20:22:19 -0700252 log_level = std::max(HardFailLogMode::kLogVerbose, log_level);
Andreas Gampe7fe30232016-03-25 16:58:00 -0700253 }
Andreas Gampe5fd66d02016-09-12 20:22:19 -0700254 if (log_level >= HardFailLogMode::kLogVerbose) {
255 LogSeverity severity;
256 switch (log_level) {
257 case HardFailLogMode::kLogVerbose:
258 severity = LogSeverity::VERBOSE;
259 break;
260 case HardFailLogMode::kLogWarning:
261 severity = LogSeverity::WARNING;
262 break;
263 case HardFailLogMode::kLogInternalFatal:
Andreas Gampe3fec9ac2016-09-13 10:47:28 -0700264 severity = LogSeverity::FATAL_WITHOUT_ABORT;
Andreas Gampe5fd66d02016-09-12 20:22:19 -0700265 break;
266 default:
267 LOG(FATAL) << "Unsupported log-level " << static_cast<uint32_t>(log_level);
268 UNREACHABLE();
269 }
Andreas Gampe3fec9ac2016-09-13 10:47:28 -0700270 verifier.DumpFailures(LOG_STREAM(severity) << "Verification error in "
David Sehr709b0702016-10-13 09:12:37 -0700271 << dex_file->PrettyMethod(method_idx)
Andreas Gampe3fec9ac2016-09-13 10:47:28 -0700272 << "\n");
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800273 }
274 if (hard_failure_msg != nullptr) {
275 CHECK(!verifier.failure_messages_.empty());
276 *hard_failure_msg =
277 verifier.failure_messages_[verifier.failure_messages_.size() - 1]->str();
278 }
Andreas Gampe6d7abbd2017-04-24 13:19:09 -0700279 result.kind = FailureKind::kHardFailure;
Andreas Gampe53e32d12015-12-09 21:03:23 -0800280
281 if (callbacks != nullptr) {
282 // Let the interested party know that we failed the class.
David Brazdil15fc7292016-09-02 14:13:18 +0100283 ClassReference ref(dex_file, dex_file->GetIndexForClassDef(class_def));
Andreas Gampe53e32d12015-12-09 21:03:23 -0800284 callbacks->ClassRejected(ref);
285 }
Igor Murashkin4d7b75f2015-07-21 17:03:36 -0700286 }
Andreas Gampe92d77202017-12-06 20:49:00 -0800287 if (VLOG_IS_ON(verifier) || VLOG_IS_ON(verifier_debug)) {
Andreas Gampe4a57d5f2019-04-03 12:31:18 -0700288 LOG(ERROR) << verifier.info_messages_.str();
289 verifier.Dump(LOG_STREAM(ERROR));
jeffhaof56197c2012-03-05 18:01:54 -0800290 }
Andreas Gampe7c887c52019-04-19 09:11:48 -0700291 // Under verifier-debug, dump the complete log into the error message.
292 if (VLOG_IS_ON(verifier_debug) && hard_failure_msg != nullptr) {
293 hard_failure_msg->append("\n");
294 hard_failure_msg->append(verifier.info_messages_.str());
295 hard_failure_msg->append("\n");
296 std::ostringstream oss;
297 verifier.Dump(oss);
298 hard_failure_msg->append(oss.str());
299 }
jeffhaof56197c2012-03-05 18:01:54 -0800300 }
Mathieu Chartier8e219ae2014-08-19 14:29:46 -0700301 if (kTimeVerifyMethod) {
302 uint64_t duration_ns = NanoTime() - start_ns;
Andreas Gampe0b0ffc12018-08-01 14:41:27 -0700303 if (duration_ns > MsToNs(Runtime::Current()->GetVerifierLoggingThresholdMs())) {
David Sehr709b0702016-10-13 09:12:37 -0700304 LOG(WARNING) << "Verification of " << dex_file->PrettyMethod(method_idx)
Andreas Gampea4f5bf62015-05-18 20:50:12 -0700305 << " took " << PrettyDuration(duration_ns)
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800306 << (IsLargeMethod(verifier.CodeItem()) ? " (large method)" : "");
Mathieu Chartier8e219ae2014-08-19 14:29:46 -0700307 }
Ian Rogersc8982582012-09-07 16:53:25 -0700308 }
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800309 result.types = verifier.encountered_failure_types_;
Ian Rogersc8982582012-09-07 16:53:25 -0700310 return result;
jeffhaof56197c2012-03-05 18:01:54 -0800311}
312
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100313MethodVerifier* MethodVerifier::VerifyMethodAndDump(Thread* self,
314 VariableIndentationOutputStream* vios,
315 uint32_t dex_method_idx,
316 const DexFile* dex_file,
317 Handle<mirror::DexCache> dex_cache,
318 Handle<mirror::ClassLoader> class_loader,
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800319 const dex::ClassDef& class_def,
320 const dex::CodeItem* code_item,
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100321 ArtMethod* method,
Andreas Gampe6cc23ac2018-08-24 15:22:43 -0700322 uint32_t method_access_flags,
323 uint32_t api_level) {
Andreas Gampebf9611f2016-03-25 16:58:00 -0700324 MethodVerifier* verifier = new MethodVerifier(self,
325 dex_file,
326 dex_cache,
327 class_loader,
328 class_def,
329 code_item,
330 dex_method_idx,
331 method,
332 method_access_flags,
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700333 /* can_load_classes= */ true,
334 /* allow_soft_failures= */ true,
335 /* need_precise_constants= */ true,
336 /* verify_to_dump= */ true,
337 /* allow_thread_suspension= */ true,
Andreas Gampe6cc23ac2018-08-24 15:22:43 -0700338 api_level);
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700339 verifier->Verify();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100340 verifier->DumpFailures(vios->Stream());
341 vios->Stream() << verifier->info_messages_.str();
Andreas Gampe5cbcde22014-09-16 14:59:49 -0700342 // Only dump and return if no hard failures. Otherwise the verifier may be not fully initialized
343 // and querying any info is dangerous/can abort.
344 if (verifier->have_pending_hard_failure_) {
345 delete verifier;
346 return nullptr;
347 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100348 verifier->Dump(vios);
Andreas Gampe5cbcde22014-09-16 14:59:49 -0700349 return verifier;
350 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800351}
352
Ian Rogers7b078e82014-09-10 14:44:24 -0700353MethodVerifier::MethodVerifier(Thread* self,
Andreas Gampe53e32d12015-12-09 21:03:23 -0800354 const DexFile* dex_file,
355 Handle<mirror::DexCache> dex_cache,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700356 Handle<mirror::ClassLoader> class_loader,
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800357 const dex::ClassDef& class_def,
358 const dex::CodeItem* code_item,
Andreas Gampe53e32d12015-12-09 21:03:23 -0800359 uint32_t dex_method_idx,
360 ArtMethod* method,
361 uint32_t method_access_flags,
362 bool can_load_classes,
363 bool allow_soft_failures,
364 bool need_precise_constants,
365 bool verify_to_dump,
Andreas Gampe6cc23ac2018-08-24 15:22:43 -0700366 bool allow_thread_suspension,
367 uint32_t api_level)
Ian Rogers7b078e82014-09-10 14:44:24 -0700368 : self_(self),
Mathieu Chartierde40d472015-10-15 17:47:48 -0700369 arena_stack_(Runtime::Current()->GetArenaPool()),
Vladimir Marko69d310e2017-10-09 14:12:23 +0100370 allocator_(&arena_stack_),
Alex Lightd9aff132017-10-31 22:30:05 +0000371 reg_types_(can_load_classes, allocator_, allow_thread_suspension),
Vladimir Marko69d310e2017-10-09 14:12:23 +0100372 reg_table_(allocator_),
Andreas Gampee2abbc62017-09-15 11:59:26 -0700373 work_insn_idx_(dex::kDexNoIndex),
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800374 dex_method_idx_(dex_method_idx),
Nicolas Geoffrayb041a402017-11-13 15:16:22 +0000375 method_being_verified_(method),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700376 method_access_flags_(method_access_flags),
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700377 return_type_(nullptr),
jeffhaof56197c2012-03-05 18:01:54 -0800378 dex_file_(dex_file),
379 dex_cache_(dex_cache),
380 class_loader_(class_loader),
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700381 class_def_(class_def),
Mathieu Chartier698ebbc2018-01-05 11:00:42 -0800382 code_item_accessor_(*dex_file, code_item),
Ian Rogers7b078e82014-09-10 14:44:24 -0700383 declaring_class_(nullptr),
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700384 interesting_dex_pc_(-1),
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700385 monitor_enter_dex_pcs_(nullptr),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700386 have_pending_hard_failure_(false),
jeffhaofaf459e2012-08-31 15:32:47 -0700387 have_pending_runtime_throw_failure_(false),
Igor Murashkin4d7b75f2015-07-21 17:03:36 -0700388 have_pending_experimental_failure_(false),
Andreas Gamped12e7822015-06-25 10:26:40 -0700389 have_any_pending_runtime_throw_failure_(false),
Andreas Gampe0760a812015-08-26 17:12:51 -0700390 encountered_failure_types_(0),
Jeff Haoee988952013-04-16 14:23:47 -0700391 can_load_classes_(can_load_classes),
Sebastien Hertz4d4adb12013-07-24 16:14:19 +0200392 allow_soft_failures_(allow_soft_failures),
Ian Rogers46960fe2014-05-23 10:43:43 -0700393 need_precise_constants_(need_precise_constants),
Sebastien Hertz4d4adb12013-07-24 16:14:19 +0200394 has_check_casts_(false),
Mathieu Chartier4306ef82014-12-19 18:41:47 -0800395 verify_to_dump_(verify_to_dump),
Andreas Gamped5ad72f2015-06-26 17:33:47 -0700396 allow_thread_suspension_(allow_thread_suspension),
Andreas Gampee6215c02015-08-31 18:54:38 -0700397 is_constructor_(false),
Andreas Gampe6cc23ac2018-08-24 15:22:43 -0700398 link_(nullptr),
399 api_level_(api_level == 0 ? std::numeric_limits<uint32_t>::max() : api_level) {
Mathieu Chartierd0ad2ee2015-03-31 14:59:59 -0700400 self->PushVerifier(this);
jeffhaof56197c2012-03-05 18:01:54 -0800401}
402
Mathieu Chartier590fee92013-09-13 13:46:47 -0700403MethodVerifier::~MethodVerifier() {
Mathieu Chartierd0ad2ee2015-03-31 14:59:59 -0700404 Thread::Current()->PopVerifier(this);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700405 STLDeleteElements(&failure_messages_);
406}
407
Andreas Gampeaaf0d382017-11-27 14:10:21 -0800408void MethodVerifier::FindLocksAtDexPc(
409 ArtMethod* m,
410 uint32_t dex_pc,
Andreas Gampe6cc23ac2018-08-24 15:22:43 -0700411 std::vector<MethodVerifier::DexLockInfo>* monitor_enter_dex_pcs,
412 uint32_t api_level) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700413 StackHandleScope<2> hs(Thread::Current());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700414 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache()));
415 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader()));
Andreas Gampebf9611f2016-03-25 16:58:00 -0700416 MethodVerifier verifier(hs.Self(),
417 m->GetDexFile(),
418 dex_cache,
419 class_loader,
David Brazdil15fc7292016-09-02 14:13:18 +0100420 m->GetClassDef(),
Andreas Gampebf9611f2016-03-25 16:58:00 -0700421 m->GetCodeItem(),
422 m->GetDexMethodIndex(),
423 m,
424 m->GetAccessFlags(),
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700425 /* can_load_classes= */ false,
426 /* allow_soft_failures= */ true,
427 /* need_precise_constants= */ false,
428 /* verify_to_dump= */ false,
429 /* allow_thread_suspension= */ false,
Andreas Gampe6cc23ac2018-08-24 15:22:43 -0700430 api_level);
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700431 verifier.interesting_dex_pc_ = dex_pc;
Ian Rogers46960fe2014-05-23 10:43:43 -0700432 verifier.monitor_enter_dex_pcs_ = monitor_enter_dex_pcs;
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700433 verifier.FindLocksAtDexPc();
434}
435
436void MethodVerifier::FindLocksAtDexPc() {
Ian Rogers7b078e82014-09-10 14:44:24 -0700437 CHECK(monitor_enter_dex_pcs_ != nullptr);
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -0800438 CHECK(code_item_accessor_.HasCodeItem()); // This only makes sense for methods with code.
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700439
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -0800440 // Quick check whether there are any monitor_enter instructions before verifying.
441 for (const DexInstructionPcPair& inst : code_item_accessor_) {
442 if (inst->Opcode() == Instruction::MONITOR_ENTER) {
443 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
444 // verification. In practice, the phase we want relies on data structures set up by all the
445 // earlier passes, so we just run the full method verification and bail out early when we've
446 // got what we wanted.
447 Verify();
448 return;
449 }
Andreas Gampecb3c08f2014-09-18 13:16:38 -0700450 }
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700451}
452
Ian Rogersad0b3a32012-04-16 14:50:24 -0700453bool MethodVerifier::Verify() {
Andreas Gampee6215c02015-08-31 18:54:38 -0700454 // Some older code doesn't correctly mark constructors as such. Test for this case by looking at
455 // the name.
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800456 const dex::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
Andreas Gampee6215c02015-08-31 18:54:38 -0700457 const char* method_name = dex_file_->StringDataByIdx(method_id.name_idx_);
458 bool instance_constructor_by_name = strcmp("<init>", method_name) == 0;
459 bool static_constructor_by_name = strcmp("<clinit>", method_name) == 0;
460 bool constructor_by_name = instance_constructor_by_name || static_constructor_by_name;
461 // Check that only constructors are tagged, and check for bad code that doesn't tag constructors.
462 if ((method_access_flags_ & kAccConstructor) != 0) {
463 if (!constructor_by_name) {
464 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
465 << "method is marked as constructor, but not named accordingly";
jeffhaobdb76512011-09-07 11:43:16 -0700466 return false;
Andreas Gampee6215c02015-08-31 18:54:38 -0700467 }
468 is_constructor_ = true;
469 } else if (constructor_by_name) {
David Sehr709b0702016-10-13 09:12:37 -0700470 LOG(WARNING) << "Method " << dex_file_->PrettyMethod(dex_method_idx_)
Andreas Gampee6215c02015-08-31 18:54:38 -0700471 << " not marked as constructor.";
472 is_constructor_ = true;
473 }
474 // If it's a constructor, check whether IsStatic() matches the name.
475 // This should have been rejected by the dex file verifier. Only do in debug build.
476 if (kIsDebugBuild) {
477 if (IsConstructor()) {
478 if (IsStatic() ^ static_constructor_by_name) {
479 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
480 << "constructor name doesn't match static flag";
481 return false;
482 }
jeffhaobdb76512011-09-07 11:43:16 -0700483 }
jeffhaobdb76512011-09-07 11:43:16 -0700484 }
Andreas Gampee6215c02015-08-31 18:54:38 -0700485
486 // Methods may only have one of public/protected/private.
487 // This should have been rejected by the dex file verifier. Only do in debug build.
488 if (kIsDebugBuild) {
489 size_t access_mod_count =
490 (((method_access_flags_ & kAccPublic) == 0) ? 0 : 1) +
491 (((method_access_flags_ & kAccProtected) == 0) ? 0 : 1) +
492 (((method_access_flags_ & kAccPrivate) == 0) ? 0 : 1);
493 if (access_mod_count > 1) {
494 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "method has more than one of public/protected/private";
495 return false;
496 }
497 }
498
499 // If there aren't any instructions, make sure that's expected, then exit successfully.
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -0800500 if (!code_item_accessor_.HasCodeItem()) {
Andreas Gampe76ed99d2016-03-28 18:31:29 -0700501 // Only native or abstract methods may not have code.
502 if ((method_access_flags_ & (kAccNative | kAccAbstract)) == 0) {
503 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "zero-length code in concrete non-native method";
504 return false;
505 }
506
Andreas Gampee6215c02015-08-31 18:54:38 -0700507 // This should have been rejected by the dex file verifier. Only do in debug build.
Andreas Gampe76ed99d2016-03-28 18:31:29 -0700508 // Note: the above will also be rejected in the dex file verifier, starting in dex version 37.
Andreas Gampee6215c02015-08-31 18:54:38 -0700509 if (kIsDebugBuild) {
Andreas Gampee6215c02015-08-31 18:54:38 -0700510 if ((method_access_flags_ & kAccAbstract) != 0) {
511 // Abstract methods are not allowed to have the following flags.
512 static constexpr uint32_t kForbidden =
513 kAccPrivate |
514 kAccStatic |
515 kAccFinal |
516 kAccNative |
517 kAccStrict |
518 kAccSynchronized;
519 if ((method_access_flags_ & kForbidden) != 0) {
520 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
521 << "method can't be abstract and private/static/final/native/strict/synchronized";
522 return false;
523 }
524 }
David Brazdil15fc7292016-09-02 14:13:18 +0100525 if ((class_def_.GetJavaAccessFlags() & kAccInterface) != 0) {
Alex Lighteb7c1442015-08-31 13:17:42 -0700526 // Interface methods must be public and abstract (if default methods are disabled).
Neil Fuller9724c632016-01-07 15:42:47 +0000527 uint32_t kRequired = kAccPublic;
Alex Lighteb7c1442015-08-31 13:17:42 -0700528 if ((method_access_flags_ & kRequired) != kRequired) {
Neil Fuller9724c632016-01-07 15:42:47 +0000529 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface methods must be public";
Andreas Gampee6215c02015-08-31 18:54:38 -0700530 return false;
531 }
532 // In addition to the above, interface methods must not be protected.
533 static constexpr uint32_t kForbidden = kAccProtected;
534 if ((method_access_flags_ & kForbidden) != 0) {
535 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface methods can't be protected";
536 return false;
537 }
538 }
539 // We also don't allow constructors to be abstract or native.
540 if (IsConstructor()) {
541 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "constructors can't be abstract or native";
542 return false;
543 }
544 }
545 return true;
546 }
547
548 // This should have been rejected by the dex file verifier. Only do in debug build.
549 if (kIsDebugBuild) {
550 // When there's code, the method must not be native or abstract.
551 if ((method_access_flags_ & (kAccNative | kAccAbstract)) != 0) {
552 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "non-zero-length code in abstract or native method";
553 return false;
554 }
555
David Brazdil15fc7292016-09-02 14:13:18 +0100556 if ((class_def_.GetJavaAccessFlags() & kAccInterface) != 0) {
Alex Lighteb7c1442015-08-31 13:17:42 -0700557 // Interfaces may always have static initializers for their fields. If we are running with
558 // default methods enabled we also allow other public, static, non-final methods to have code.
559 // Otherwise that is the only type of method allowed.
Alex Light0db36b32015-10-27 14:06:34 -0700560 if (!(IsConstructor() && IsStatic())) {
Neil Fuller9724c632016-01-07 15:42:47 +0000561 if (IsInstanceConstructor()) {
562 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interfaces may not have non-static constructor";
563 return false;
564 } else if (method_access_flags_ & kAccFinal) {
565 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interfaces may not have final methods";
566 return false;
Alex Lightb55f1ac2016-04-12 15:50:55 -0700567 } else {
568 uint32_t access_flag_options = kAccPublic;
Mathieu Chartierf6e31472017-12-28 13:32:08 -0800569 if (dex_file_->SupportsDefaultMethods()) {
Alex Lightb55f1ac2016-04-12 15:50:55 -0700570 access_flag_options |= kAccPrivate;
571 }
572 if (!(method_access_flags_ & access_flag_options)) {
573 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
574 << "interfaces may not have protected or package-private members";
575 return false;
576 }
Alex Lighteb7c1442015-08-31 13:17:42 -0700577 }
Andreas Gampee6215c02015-08-31 18:54:38 -0700578 }
579 }
580
581 // Instance constructors must not be synchronized.
582 if (IsInstanceConstructor()) {
583 static constexpr uint32_t kForbidden = kAccSynchronized;
584 if ((method_access_flags_ & kForbidden) != 0) {
585 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "constructors can't be synchronized";
586 return false;
587 }
588 }
589 }
590
Ian Rogersd81871c2011-10-03 13:57:23 -0700591 // Sanity-check the register counts. ins + locals = registers, so make sure that ins <= registers.
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -0800592 if (code_item_accessor_.InsSize() > code_item_accessor_.RegistersSize()) {
593 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad register counts (ins="
594 << code_item_accessor_.InsSize()
595 << " regs=" << code_item_accessor_.RegistersSize();
Ian Rogersd81871c2011-10-03 13:57:23 -0700596 return false;
jeffhaobdb76512011-09-07 11:43:16 -0700597 }
Alex Lighteb7c1442015-08-31 13:17:42 -0700598
Ian Rogersd81871c2011-10-03 13:57:23 -0700599 // Allocate and initialize an array to hold instruction data.
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -0800600 insn_flags_.reset(allocator_.AllocArray<InstructionFlags>(
601 code_item_accessor_.InsnsSizeInCodeUnits()));
Mathieu Chartierde40d472015-10-15 17:47:48 -0700602 DCHECK(insn_flags_ != nullptr);
603 std::uninitialized_fill_n(insn_flags_.get(),
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -0800604 code_item_accessor_.InsnsSizeInCodeUnits(),
Mathieu Chartierde40d472015-10-15 17:47:48 -0700605 InstructionFlags());
Ian Rogersd81871c2011-10-03 13:57:23 -0700606 // Run through the instructions and see if the width checks out.
607 bool result = ComputeWidthsAndCountOps();
Andreas Gampebf1cb772017-05-15 15:39:00 -0700608 bool allow_runtime_only_instructions = !Runtime::Current()->IsAotCompiler() || verify_to_dump_;
Ian Rogersd81871c2011-10-03 13:57:23 -0700609 // Flag instructions guarded by a "try" block and check exception handlers.
610 result = result && ScanTryCatchBlocks();
611 // Perform static instruction verification.
Andreas Gampebf1cb772017-05-15 15:39:00 -0700612 result = result && (allow_runtime_only_instructions
613 ? VerifyInstructions<true>()
614 : VerifyInstructions<false>());
Ian Rogersad0b3a32012-04-16 14:50:24 -0700615 // Perform code-flow analysis and return.
Vladimir Marko2b5eaa22013-12-13 13:59:30 +0000616 result = result && VerifyCodeFlow();
Andreas Gampe53e32d12015-12-09 21:03:23 -0800617
Vladimir Marko2b5eaa22013-12-13 13:59:30 +0000618 return result;
jeffhaoba5ebb92011-08-25 17:24:37 -0700619}
620
Ian Rogers776ac1f2012-04-13 23:36:36 -0700621std::ostream& MethodVerifier::Fail(VerifyError error) {
Andreas Gampe0760a812015-08-26 17:12:51 -0700622 // Mark the error type as encountered.
Andreas Gampea727e372015-08-25 09:22:37 -0700623 encountered_failure_types_ |= static_cast<uint32_t>(error);
Andreas Gampe0760a812015-08-26 17:12:51 -0700624
Ian Rogersad0b3a32012-04-16 14:50:24 -0700625 switch (error) {
626 case VERIFY_ERROR_NO_CLASS:
627 case VERIFY_ERROR_NO_FIELD:
628 case VERIFY_ERROR_NO_METHOD:
629 case VERIFY_ERROR_ACCESS_CLASS:
630 case VERIFY_ERROR_ACCESS_FIELD:
631 case VERIFY_ERROR_ACCESS_METHOD:
Ian Rogers08f753d2012-08-24 14:35:25 -0700632 case VERIFY_ERROR_INSTANTIATION:
633 case VERIFY_ERROR_CLASS_CHANGE:
Igor Murashkin158f35c2015-06-10 15:55:30 -0700634 case VERIFY_ERROR_FORCE_INTERPRETER:
Andreas Gampea727e372015-08-25 09:22:37 -0700635 case VERIFY_ERROR_LOCKING:
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800636 if (Runtime::Current()->IsAotCompiler() || !can_load_classes_) {
jeffhaofaf459e2012-08-31 15:32:47 -0700637 // If we're optimistically running verification at compile time, turn NO_xxx, ACCESS_xxx,
638 // class change and instantiation errors into soft verification errors so that we re-verify
639 // at runtime. We may fail to find or to agree on access because of not yet available class
640 // loaders, or class loaders that will differ at runtime. In these cases, we don't want to
641 // affect the soundness of the code being compiled. Instead, the generated code runs "slow
642 // paths" that dynamically perform the verification and cause the behavior to be that akin
643 // to an interpreter.
644 error = VERIFY_ERROR_BAD_CLASS_SOFT;
645 } else {
Jeff Haoa3faaf42013-09-03 19:07:00 -0700646 // If we fail again at runtime, mark that this instruction would throw and force this
647 // method to be executed using the interpreter with checks.
jeffhaofaf459e2012-08-31 15:32:47 -0700648 have_pending_runtime_throw_failure_ = true;
Andreas Gamped7f8d052015-03-12 11:05:47 -0700649
650 // We need to save the work_line if the instruction wasn't throwing before. Otherwise we'll
651 // try to merge garbage.
652 // Note: this assumes that Fail is called before we do any work_line modifications.
Andreas Gamped5ad72f2015-06-26 17:33:47 -0700653 // Note: this can fail before we touch any instruction, for the signature of a method. So
654 // add a check.
Andreas Gampee2abbc62017-09-15 11:59:26 -0700655 if (work_insn_idx_ < dex::kDexNoIndex) {
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -0800656 const Instruction& inst = code_item_accessor_.InstructionAt(work_insn_idx_);
Mathieu Chartier1d2d4ff2017-09-23 16:11:06 -0700657 int opcode_flags = Instruction::FlagsOf(inst.Opcode());
Andreas Gamped7f8d052015-03-12 11:05:47 -0700658
Andreas Gamped5ad72f2015-06-26 17:33:47 -0700659 if ((opcode_flags & Instruction::kThrow) == 0 && CurrentInsnFlags()->IsInTry()) {
660 saved_line_->CopyFromLine(work_line_.get());
661 }
Andreas Gamped7f8d052015-03-12 11:05:47 -0700662 }
jeffhaofaf459e2012-08-31 15:32:47 -0700663 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700664 break;
Andreas Gampea727e372015-08-25 09:22:37 -0700665
Ian Rogersad0b3a32012-04-16 14:50:24 -0700666 // Indication that verification should be retried at runtime.
667 case VERIFY_ERROR_BAD_CLASS_SOFT:
Jeff Haoee988952013-04-16 14:23:47 -0700668 if (!allow_soft_failures_) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700669 have_pending_hard_failure_ = true;
670 }
671 break;
Andreas Gampea727e372015-08-25 09:22:37 -0700672
jeffhaod5347e02012-03-22 17:25:05 -0700673 // Hard verification failures at compile time will still fail at runtime, so the class is
674 // marked as rejected to prevent it from being compiled.
Ian Rogersad0b3a32012-04-16 14:50:24 -0700675 case VERIFY_ERROR_BAD_CLASS_HARD: {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700676 have_pending_hard_failure_ = true;
Andreas Gampeebf850c2015-08-14 15:37:35 -0700677 if (VLOG_IS_ON(verifier) && kDumpRegLinesOnHardFailureIfVLOG) {
678 ScopedObjectAccess soa(Thread::Current());
679 std::ostringstream oss;
680 Dump(oss);
681 LOG(ERROR) << oss.str();
682 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700683 break;
Ian Rogers47a05882012-02-03 12:23:33 -0800684 }
685 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700686 failures_.push_back(error);
David Sehr709b0702016-10-13 09:12:37 -0700687 std::string location(StringPrintf("%s: [0x%X] ", dex_file_->PrettyMethod(dex_method_idx_).c_str(),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700688 work_insn_idx_));
Elena Sayapina78480ec2014-08-15 15:52:42 +0700689 std::ostringstream* failure_message = new std::ostringstream(location, std::ostringstream::ate);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700690 failure_messages_.push_back(failure_message);
691 return *failure_message;
692}
693
Andreas Gampe2ad6cce2019-04-11 16:17:39 -0700694ScopedNewLine MethodVerifier::LogVerifyInfo() {
695 ScopedNewLine ret{info_messages_};
696 ret << "VFY: " << dex_file_->PrettyMethod(dex_method_idx_)
697 << '[' << reinterpret_cast<void*>(work_insn_idx_) << "] : ";
698 return ret;
Ian Rogers576ca0c2014-06-06 15:58:22 -0700699}
700
Ian Rogersad0b3a32012-04-16 14:50:24 -0700701void MethodVerifier::PrependToLastFailMessage(std::string prepend) {
702 size_t failure_num = failure_messages_.size();
703 DCHECK_NE(failure_num, 0U);
704 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
705 prepend += last_fail_message->str();
Elena Sayapina78480ec2014-08-15 15:52:42 +0700706 failure_messages_[failure_num - 1] = new std::ostringstream(prepend, std::ostringstream::ate);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700707 delete last_fail_message;
708}
709
Vladimir Marko5c657fe2016-11-03 15:12:29 +0000710void MethodVerifier::AppendToLastFailMessage(const std::string& append) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700711 size_t failure_num = failure_messages_.size();
712 DCHECK_NE(failure_num, 0U);
713 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
714 (*last_fail_message) << append;
Ian Rogers47a05882012-02-03 12:23:33 -0800715}
716
Ian Rogers776ac1f2012-04-13 23:36:36 -0700717bool MethodVerifier::ComputeWidthsAndCountOps() {
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;
Ian Rogersa9a82542013-10-04 11:17:26 -0700734 default:
735 break;
jeffhaobdb76512011-09-07 11:43:16 -0700736 }
Andreas Gampe51de69e2019-04-19 15:14:14 -0700737 GetModifiableInstructionFlags(it.DexPc()).SetIsOpcode();
jeffhaobdb76512011-09-07 11:43:16 -0700738 }
739
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -0800740 if (it != code_item_accessor_.end()) {
741 const size_t insns_size = code_item_accessor_.InsnsSizeInCodeUnits();
jeffhaod5347e02012-03-22 17:25:05 -0700742 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "code did not end where expected ("
Mathieu Chartier2b2bef22017-10-26 17:10:19 -0700743 << it.DexPc() << " vs. " << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700744 return false;
745 }
746
jeffhaobdb76512011-09-07 11:43:16 -0700747 return true;
748}
749
Ian Rogers776ac1f2012-04-13 23:36:36 -0700750bool MethodVerifier::ScanTryCatchBlocks() {
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -0800751 const uint32_t tries_size = code_item_accessor_.TriesSize();
jeffhaobdb76512011-09-07 11:43:16 -0700752 if (tries_size == 0) {
753 return true;
754 }
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -0800755 const uint32_t insns_size = code_item_accessor_.InsnsSizeInCodeUnits();
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800756 for (const dex::TryItem& try_item : code_item_accessor_.TryItems()) {
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -0800757 const uint32_t start = try_item.start_addr_;
758 const uint32_t end = start + try_item.insn_count_;
jeffhaobdb76512011-09-07 11:43:16 -0700759 if ((start >= end) || (start >= insns_size) || (end > insns_size)) {
jeffhaod5347e02012-03-22 17:25:05 -0700760 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad exception entry: startAddr=" << start
761 << " endAddr=" << end << " (size=" << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700762 return false;
763 }
Mathieu Chartierde40d472015-10-15 17:47:48 -0700764 if (!GetInstructionFlags(start).IsOpcode()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700765 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
766 << "'try' block starts inside an instruction (" << start << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700767 return false;
768 }
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -0800769 DexInstructionIterator end_it(code_item_accessor_.Insns(), end);
770 for (DexInstructionIterator it(code_item_accessor_.Insns(), start); it < end_it; ++it) {
Andreas Gampe51de69e2019-04-19 15:14:14 -0700771 GetModifiableInstructionFlags(it.DexPc()).SetInTry();
jeffhaobdb76512011-09-07 11:43:16 -0700772 }
773 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800774 // Iterate over each of the handlers to verify target addresses.
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -0800775 const uint8_t* handlers_ptr = code_item_accessor_.GetCatchHandlerData();
776 const uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700777 ClassLinker* linker = Runtime::Current()->GetClassLinker();
jeffhaobdb76512011-09-07 11:43:16 -0700778 for (uint32_t idx = 0; idx < handlers_size; idx++) {
Ian Rogers0571d352011-11-03 19:51:38 -0700779 CatchHandlerIterator iterator(handlers_ptr);
780 for (; iterator.HasNext(); iterator.Next()) {
Igor Murashkin2ffb7032017-11-08 13:35:21 -0800781 uint32_t dex_pc = iterator.GetHandlerAddress();
Mathieu Chartierde40d472015-10-15 17:47:48 -0700782 if (!GetInstructionFlags(dex_pc).IsOpcode()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700783 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
784 << "exception handler starts at bad address (" << dex_pc << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700785 return false;
786 }
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -0800787 if (!CheckNotMoveResult(code_item_accessor_.Insns(), dex_pc)) {
Stephen Kyle9bc61992014-09-22 13:53:15 +0100788 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
789 << "exception handler begins with move-result* (" << dex_pc << ")";
790 return false;
791 }
Andreas Gampe51de69e2019-04-19 15:14:14 -0700792 GetModifiableInstructionFlags(dex_pc).SetBranchTarget();
Ian Rogers28ad40d2011-10-27 15:19:26 -0700793 // Ensure exception types are resolved so that they don't need resolution to be delivered,
794 // unresolved exception types will be ignored by exception delivery
Andreas Gampea5b09a62016-11-17 15:21:22 -0800795 if (iterator.GetHandlerTypeIndex().IsValid()) {
Vladimir Marko666ee3d2017-12-11 18:37:36 +0000796 ObjPtr<mirror::Class> exception_type =
797 linker->ResolveType(iterator.GetHandlerTypeIndex(), dex_cache_, class_loader_);
Ian Rogers7b078e82014-09-10 14:44:24 -0700798 if (exception_type == nullptr) {
799 DCHECK(self_->IsExceptionPending());
800 self_->ClearException();
Ian Rogers28ad40d2011-10-27 15:19:26 -0700801 }
802 }
jeffhaobdb76512011-09-07 11:43:16 -0700803 }
Ian Rogers0571d352011-11-03 19:51:38 -0700804 handlers_ptr = iterator.EndDataPointer();
jeffhaobdb76512011-09-07 11:43:16 -0700805 }
jeffhaobdb76512011-09-07 11:43:16 -0700806 return true;
807}
808
Andreas Gampebf1cb772017-05-15 15:39:00 -0700809template <bool kAllowRuntimeOnlyInstructions>
Ian Rogers776ac1f2012-04-13 23:36:36 -0700810bool MethodVerifier::VerifyInstructions() {
Ian Rogers0c7abda2012-09-19 13:33:42 -0700811 /* Flag the start of the method as a branch target, and a GC point due to stack overflow errors */
Andreas Gampe51de69e2019-04-19 15:14:14 -0700812 GetModifiableInstructionFlags(0).SetBranchTarget();
813 GetModifiableInstructionFlags(0).SetCompileTimeInfoPoint();
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -0800814 for (const DexInstructionPcPair& inst : code_item_accessor_) {
Mathieu Chartier2b2bef22017-10-26 17:10:19 -0700815 const uint32_t dex_pc = inst.DexPc();
816 if (!VerifyInstruction<kAllowRuntimeOnlyInstructions>(&inst.Inst(), dex_pc)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700817 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -0700818 return false;
819 }
820 /* Flag instructions that are garbage collection points */
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700821 // All invoke points are marked as "Throw" points already.
822 // We are relying on this to also count all the invokes as interesting.
Vladimir Marko8b858e12014-11-27 14:52:37 +0000823 if (inst->IsBranch()) {
Andreas Gampe51de69e2019-04-19 15:14:14 -0700824 GetModifiableInstructionFlags(dex_pc).SetCompileTimeInfoPoint();
Vladimir Marko8b858e12014-11-27 14:52:37 +0000825 // The compiler also needs safepoints for fall-through to loop heads.
826 // Such a loop head must be a target of a branch.
827 int32_t offset = 0;
828 bool cond, self_ok;
829 bool target_ok = GetBranchOffset(dex_pc, &offset, &cond, &self_ok);
830 DCHECK(target_ok);
Andreas Gampe51de69e2019-04-19 15:14:14 -0700831 GetModifiableInstructionFlags(dex_pc + offset).SetCompileTimeInfoPoint();
Vladimir Marko8b858e12014-11-27 14:52:37 +0000832 } else if (inst->IsSwitch() || inst->IsThrow()) {
Andreas Gampe51de69e2019-04-19 15:14:14 -0700833 GetModifiableInstructionFlags(dex_pc).SetCompileTimeInfoPoint();
Ian Rogersb8c78592013-07-25 23:52:52 +0000834 } else if (inst->IsReturn()) {
Andreas Gampe51de69e2019-04-19 15:14:14 -0700835 GetModifiableInstructionFlags(dex_pc).SetCompileTimeInfoPointAndReturn();
Ian Rogersd81871c2011-10-03 13:57:23 -0700836 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700837 }
838 return true;
839}
840
Andreas Gampebf1cb772017-05-15 15:39:00 -0700841template <bool kAllowRuntimeOnlyInstructions>
842bool MethodVerifier::VerifyInstruction(const Instruction* inst, uint32_t code_offset) {
Andreas Gampee05cc662017-05-15 10:17:30 -0700843 if (Instruction::kHaveExperimentalInstructions && UNLIKELY(inst->IsExperimental())) {
Igor Murashkin4d7b75f2015-07-21 17:03:36 -0700844 // Experimental instructions don't yet have verifier support implementation.
845 // While it is possible to use them by themselves, when we try to use stable instructions
846 // with a virtual register that was created by an experimental instruction,
847 // the data flow analysis will fail.
848 Fail(VERIFY_ERROR_FORCE_INTERPRETER)
849 << "experimental instruction is not supported by verifier; skipping verification";
850 have_pending_experimental_failure_ = true;
851 return false;
852 }
853
Ian Rogersd81871c2011-10-03 13:57:23 -0700854 bool result = true;
855 switch (inst->GetVerifyTypeArgumentA()) {
856 case Instruction::kVerifyRegA:
Ian Rogers29a26482014-05-02 15:27:29 -0700857 result = result && CheckRegisterIndex(inst->VRegA());
Ian Rogersd81871c2011-10-03 13:57:23 -0700858 break;
859 case Instruction::kVerifyRegAWide:
Ian Rogers29a26482014-05-02 15:27:29 -0700860 result = result && CheckWideRegisterIndex(inst->VRegA());
Ian Rogersd81871c2011-10-03 13:57:23 -0700861 break;
862 }
863 switch (inst->GetVerifyTypeArgumentB()) {
864 case Instruction::kVerifyRegB:
Ian Rogers29a26482014-05-02 15:27:29 -0700865 result = result && CheckRegisterIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700866 break;
867 case Instruction::kVerifyRegBField:
Ian Rogers29a26482014-05-02 15:27:29 -0700868 result = result && CheckFieldIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700869 break;
870 case Instruction::kVerifyRegBMethod:
Ian Rogers29a26482014-05-02 15:27:29 -0700871 result = result && CheckMethodIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700872 break;
873 case Instruction::kVerifyRegBNewInstance:
Andreas Gampea5b09a62016-11-17 15:21:22 -0800874 result = result && CheckNewInstance(dex::TypeIndex(inst->VRegB()));
Ian Rogersd81871c2011-10-03 13:57:23 -0700875 break;
876 case Instruction::kVerifyRegBString:
Ian Rogers29a26482014-05-02 15:27:29 -0700877 result = result && CheckStringIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700878 break;
879 case Instruction::kVerifyRegBType:
Andreas Gampea5b09a62016-11-17 15:21:22 -0800880 result = result && CheckTypeIndex(dex::TypeIndex(inst->VRegB()));
Ian Rogersd81871c2011-10-03 13:57:23 -0700881 break;
882 case Instruction::kVerifyRegBWide:
Ian Rogers29a26482014-05-02 15:27:29 -0700883 result = result && CheckWideRegisterIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700884 break;
Orion Hodson2e599942017-09-22 16:17:41 +0100885 case Instruction::kVerifyRegBCallSite:
886 result = result && CheckCallSiteIndex(inst->VRegB());
887 break;
888 case Instruction::kVerifyRegBMethodHandle:
889 result = result && CheckMethodHandleIndex(inst->VRegB());
890 break;
891 case Instruction::kVerifyRegBPrototype:
892 result = result && CheckPrototypeIndex(inst->VRegB());
893 break;
Ian Rogersd81871c2011-10-03 13:57:23 -0700894 }
895 switch (inst->GetVerifyTypeArgumentC()) {
896 case Instruction::kVerifyRegC:
Ian Rogers29a26482014-05-02 15:27:29 -0700897 result = result && CheckRegisterIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700898 break;
899 case Instruction::kVerifyRegCField:
Ian Rogers29a26482014-05-02 15:27:29 -0700900 result = result && CheckFieldIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700901 break;
902 case Instruction::kVerifyRegCNewArray:
Andreas Gampea5b09a62016-11-17 15:21:22 -0800903 result = result && CheckNewArray(dex::TypeIndex(inst->VRegC()));
Ian Rogersd81871c2011-10-03 13:57:23 -0700904 break;
905 case Instruction::kVerifyRegCType:
Andreas Gampea5b09a62016-11-17 15:21:22 -0800906 result = result && CheckTypeIndex(dex::TypeIndex(inst->VRegC()));
Ian Rogersd81871c2011-10-03 13:57:23 -0700907 break;
908 case Instruction::kVerifyRegCWide:
Ian Rogers29a26482014-05-02 15:27:29 -0700909 result = result && CheckWideRegisterIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700910 break;
911 }
Orion Hodsoncfa325e2016-10-13 10:25:54 +0100912 switch (inst->GetVerifyTypeArgumentH()) {
913 case Instruction::kVerifyRegHPrototype:
914 result = result && CheckPrototypeIndex(inst->VRegH());
915 break;
916 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700917 switch (inst->GetVerifyExtraFlags()) {
918 case Instruction::kVerifyArrayData:
919 result = result && CheckArrayData(code_offset);
920 break;
921 case Instruction::kVerifyBranchTarget:
922 result = result && CheckBranchTarget(code_offset);
923 break;
924 case Instruction::kVerifySwitchTargets:
925 result = result && CheckSwitchTargets(code_offset);
926 break;
Andreas Gampec3314312014-06-19 18:13:29 -0700927 case Instruction::kVerifyVarArgNonZero:
928 // Fall-through.
Ian Rogers29a26482014-05-02 15:27:29 -0700929 case Instruction::kVerifyVarArg: {
Taiju Tsuiki29498a22015-04-13 14:21:00 +0900930 // Instructions that can actually return a negative value shouldn't have this flag.
931 uint32_t v_a = dchecked_integral_cast<uint32_t>(inst->VRegA());
932 if ((inst->GetVerifyExtraFlags() == Instruction::kVerifyVarArgNonZero && v_a == 0) ||
933 v_a > Instruction::kMaxVarArgRegs) {
934 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << v_a << ") in "
Andreas Gampec3314312014-06-19 18:13:29 -0700935 "non-range invoke";
936 return false;
937 }
Taiju Tsuiki29498a22015-04-13 14:21:00 +0900938
Ian Rogers29a26482014-05-02 15:27:29 -0700939 uint32_t args[Instruction::kMaxVarArgRegs];
940 inst->GetVarArgs(args);
Taiju Tsuiki29498a22015-04-13 14:21:00 +0900941 result = result && CheckVarArgRegs(v_a, args);
Ian Rogersd81871c2011-10-03 13:57:23 -0700942 break;
Ian Rogers29a26482014-05-02 15:27:29 -0700943 }
Andreas Gampec3314312014-06-19 18:13:29 -0700944 case Instruction::kVerifyVarArgRangeNonZero:
945 // Fall-through.
Ian Rogersd81871c2011-10-03 13:57:23 -0700946 case Instruction::kVerifyVarArgRange:
Andreas Gampec3314312014-06-19 18:13:29 -0700947 if (inst->GetVerifyExtraFlags() == Instruction::kVerifyVarArgRangeNonZero &&
948 inst->VRegA() <= 0) {
949 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << inst->VRegA() << ") in "
950 "range invoke";
951 return false;
952 }
Ian Rogers29a26482014-05-02 15:27:29 -0700953 result = result && CheckVarArgRangeRegs(inst->VRegA(), inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700954 break;
955 case Instruction::kVerifyError:
jeffhaod5347e02012-03-22 17:25:05 -0700956 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected opcode " << inst->Name();
Ian Rogersd81871c2011-10-03 13:57:23 -0700957 result = false;
958 break;
959 }
Andreas Gampebf1cb772017-05-15 15:39:00 -0700960 if (!kAllowRuntimeOnlyInstructions && inst->GetVerifyIsRuntimeOnly()) {
Ian Rogers5fb22a92014-06-13 10:31:28 -0700961 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "opcode only expected at runtime " << inst->Name();
962 result = false;
963 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700964 return result;
965}
966
Ian Rogers7b078e82014-09-10 14:44:24 -0700967inline bool MethodVerifier::CheckRegisterIndex(uint32_t idx) {
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -0800968 if (UNLIKELY(idx >= code_item_accessor_.RegistersSize())) {
jeffhaod5347e02012-03-22 17:25:05 -0700969 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register index out of range (" << idx << " >= "
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -0800970 << code_item_accessor_.RegistersSize() << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700971 return false;
972 }
973 return true;
974}
975
Ian Rogers7b078e82014-09-10 14:44:24 -0700976inline bool MethodVerifier::CheckWideRegisterIndex(uint32_t idx) {
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -0800977 if (UNLIKELY(idx + 1 >= code_item_accessor_.RegistersSize())) {
jeffhaod5347e02012-03-22 17:25:05 -0700978 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "wide register index out of range (" << idx
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -0800979 << "+1 >= " << code_item_accessor_.RegistersSize() << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700980 return false;
981 }
982 return true;
983}
984
Orion Hodson2e599942017-09-22 16:17:41 +0100985inline bool MethodVerifier::CheckCallSiteIndex(uint32_t idx) {
986 uint32_t limit = dex_file_->NumCallSiteIds();
987 if (UNLIKELY(idx >= limit)) {
988 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad call site index " << idx << " (max "
989 << limit << ")";
990 return false;
991 }
992 return true;
993}
994
Ian Rogers7b078e82014-09-10 14:44:24 -0700995inline bool MethodVerifier::CheckFieldIndex(uint32_t idx) {
Andreas Gampe29e81422017-05-15 16:29:32 -0700996 if (UNLIKELY(idx >= dex_file_->GetHeader().field_ids_size_)) {
jeffhaod5347e02012-03-22 17:25:05 -0700997 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad field index " << idx << " (max "
998 << dex_file_->GetHeader().field_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700999 return false;
1000 }
1001 return true;
1002}
1003
Ian Rogers7b078e82014-09-10 14:44:24 -07001004inline bool MethodVerifier::CheckMethodIndex(uint32_t idx) {
Andreas Gampe29e81422017-05-15 16:29:32 -07001005 if (UNLIKELY(idx >= dex_file_->GetHeader().method_ids_size_)) {
jeffhaod5347e02012-03-22 17:25:05 -07001006 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad method index " << idx << " (max "
1007 << dex_file_->GetHeader().method_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001008 return false;
1009 }
1010 return true;
1011}
1012
Orion Hodson2e599942017-09-22 16:17:41 +01001013inline bool MethodVerifier::CheckMethodHandleIndex(uint32_t idx) {
1014 uint32_t limit = dex_file_->NumMethodHandles();
1015 if (UNLIKELY(idx >= limit)) {
1016 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad method handle index " << idx << " (max "
1017 << limit << ")";
1018 return false;
1019 }
1020 return true;
1021}
1022
Andreas Gampea5b09a62016-11-17 15:21:22 -08001023inline bool MethodVerifier::CheckNewInstance(dex::TypeIndex idx) {
Andreas Gampe29e81422017-05-15 16:29:32 -07001024 if (UNLIKELY(idx.index_ >= dex_file_->GetHeader().type_ids_size_)) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08001025 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx.index_ << " (max "
jeffhaod5347e02012-03-22 17:25:05 -07001026 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001027 return false;
1028 }
1029 // We don't need the actual class, just a pointer to the class name.
Ian Rogers0571d352011-11-03 19:51:38 -07001030 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Andreas Gampe29e81422017-05-15 16:29:32 -07001031 if (UNLIKELY(descriptor[0] != 'L')) {
jeffhaod5347e02012-03-22 17:25:05 -07001032 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "can't call new-instance on type '" << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001033 return false;
Andreas Gampe29e81422017-05-15 16:29:32 -07001034 } else if (UNLIKELY(strcmp(descriptor, "Ljava/lang/Class;") == 0)) {
Aart Bikdb698f12016-07-25 17:52:22 -07001035 // An unlikely new instance on Class is not allowed. Fall back to interpreter to ensure an
1036 // exception is thrown when this statement is executed (compiled code would not do that).
1037 Fail(VERIFY_ERROR_INSTANTIATION);
Ian Rogersd81871c2011-10-03 13:57:23 -07001038 }
1039 return true;
1040}
1041
Orion Hodsoncfa325e2016-10-13 10:25:54 +01001042inline bool MethodVerifier::CheckPrototypeIndex(uint32_t idx) {
Andreas Gampe29e81422017-05-15 16:29:32 -07001043 if (UNLIKELY(idx >= dex_file_->GetHeader().proto_ids_size_)) {
Orion Hodsoncfa325e2016-10-13 10:25:54 +01001044 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad prototype index " << idx << " (max "
1045 << dex_file_->GetHeader().proto_ids_size_ << ")";
1046 return false;
1047 }
1048 return true;
1049}
1050
Ian Rogers7b078e82014-09-10 14:44:24 -07001051inline bool MethodVerifier::CheckStringIndex(uint32_t idx) {
Andreas Gampe29e81422017-05-15 16:29:32 -07001052 if (UNLIKELY(idx >= dex_file_->GetHeader().string_ids_size_)) {
jeffhaod5347e02012-03-22 17:25:05 -07001053 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad string index " << idx << " (max "
1054 << dex_file_->GetHeader().string_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001055 return false;
1056 }
1057 return true;
1058}
1059
Andreas Gampea5b09a62016-11-17 15:21:22 -08001060inline bool MethodVerifier::CheckTypeIndex(dex::TypeIndex idx) {
Andreas Gampe29e81422017-05-15 16:29:32 -07001061 if (UNLIKELY(idx.index_ >= dex_file_->GetHeader().type_ids_size_)) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08001062 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx.index_ << " (max "
jeffhaod5347e02012-03-22 17:25:05 -07001063 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001064 return false;
1065 }
1066 return true;
1067}
1068
Andreas Gampea5b09a62016-11-17 15:21:22 -08001069bool MethodVerifier::CheckNewArray(dex::TypeIndex idx) {
Andreas Gampe29e81422017-05-15 16:29:32 -07001070 if (UNLIKELY(idx.index_ >= dex_file_->GetHeader().type_ids_size_)) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08001071 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx.index_ << " (max "
jeffhaod5347e02012-03-22 17:25:05 -07001072 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001073 return false;
1074 }
1075 int bracket_count = 0;
Ian Rogers0571d352011-11-03 19:51:38 -07001076 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -07001077 const char* cp = descriptor;
1078 while (*cp++ == '[') {
1079 bracket_count++;
1080 }
Andreas Gampe29e81422017-05-15 16:29:32 -07001081 if (UNLIKELY(bracket_count == 0)) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001082 /* The given class must be an array type. */
Brian Carlstrom93c33962013-07-26 10:37:43 -07001083 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1084 << "can't new-array class '" << descriptor << "' (not an array)";
Ian Rogersd81871c2011-10-03 13:57:23 -07001085 return false;
Andreas Gampe29e81422017-05-15 16:29:32 -07001086 } else if (UNLIKELY(bracket_count > 255)) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001087 /* It is illegal to create an array of more than 255 dimensions. */
Brian Carlstrom93c33962013-07-26 10:37:43 -07001088 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1089 << "can't new-array class '" << descriptor << "' (exceeds limit)";
Ian Rogersd81871c2011-10-03 13:57:23 -07001090 return false;
1091 }
1092 return true;
1093}
1094
Ian Rogers776ac1f2012-04-13 23:36:36 -07001095bool MethodVerifier::CheckArrayData(uint32_t cur_offset) {
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001096 const uint32_t insn_count = code_item_accessor_.InsnsSizeInCodeUnits();
1097 const uint16_t* insns = code_item_accessor_.Insns() + cur_offset;
Ian Rogersd81871c2011-10-03 13:57:23 -07001098 const uint16_t* array_data;
1099 int32_t array_data_offset;
1100
1101 DCHECK_LT(cur_offset, insn_count);
1102 /* make sure the start of the array data table is in range */
Andreas Gampe53de99c2015-08-17 13:43:55 -07001103 array_data_offset = insns[1] | (static_cast<int32_t>(insns[2]) << 16);
Andreas Gampe29e81422017-05-15 16:29:32 -07001104 if (UNLIKELY(static_cast<int32_t>(cur_offset) + array_data_offset < 0 ||
1105 cur_offset + array_data_offset + 2 >= insn_count)) {
jeffhaod5347e02012-03-22 17:25:05 -07001106 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data start: at " << cur_offset
Brian Carlstrom93c33962013-07-26 10:37:43 -07001107 << ", data offset " << array_data_offset
1108 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -07001109 return false;
1110 }
1111 /* offset to array data table is a relative branch-style offset */
1112 array_data = insns + array_data_offset;
Andreas Gampe57c47582015-07-01 22:05:59 -07001113 // Make sure the table is at an even dex pc, that is, 32-bit aligned.
Andreas Gampe29e81422017-05-15 16:29:32 -07001114 if (UNLIKELY(!IsAligned<4>(array_data))) {
jeffhaod5347e02012-03-22 17:25:05 -07001115 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned array data table: at " << cur_offset
1116 << ", data offset " << array_data_offset;
Ian Rogersd81871c2011-10-03 13:57:23 -07001117 return false;
1118 }
Andreas Gampe57c47582015-07-01 22:05:59 -07001119 // Make sure the array-data is marked as an opcode. This ensures that it was reached when
1120 // traversing the code item linearly. It is an approximation for a by-spec padding value.
Andreas Gampe29e81422017-05-15 16:29:32 -07001121 if (UNLIKELY(!GetInstructionFlags(cur_offset + array_data_offset).IsOpcode())) {
Andreas Gampe57c47582015-07-01 22:05:59 -07001122 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array data table at " << cur_offset
1123 << ", data offset " << array_data_offset
1124 << " not correctly visited, probably bad padding.";
1125 return false;
1126 }
1127
Ian Rogersd81871c2011-10-03 13:57:23 -07001128 uint32_t value_width = array_data[1];
Elliott Hughes398f64b2012-03-26 18:05:48 -07001129 uint32_t value_count = *reinterpret_cast<const uint32_t*>(&array_data[2]);
Ian Rogersd81871c2011-10-03 13:57:23 -07001130 uint32_t table_size = 4 + (value_width * value_count + 1) / 2;
1131 /* make sure the end of the switch is in range */
Andreas Gampe29e81422017-05-15 16:29:32 -07001132 if (UNLIKELY(cur_offset + array_data_offset + table_size > insn_count)) {
jeffhaod5347e02012-03-22 17:25:05 -07001133 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data end: at " << cur_offset
1134 << ", data offset " << array_data_offset << ", end "
1135 << cur_offset + array_data_offset + table_size
1136 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -07001137 return false;
1138 }
1139 return true;
1140}
1141
Ian Rogers776ac1f2012-04-13 23:36:36 -07001142bool MethodVerifier::CheckBranchTarget(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001143 int32_t offset;
1144 bool isConditional, selfOkay;
1145 if (!GetBranchOffset(cur_offset, &offset, &isConditional, &selfOkay)) {
1146 return false;
1147 }
Andreas Gampe29e81422017-05-15 16:29:32 -07001148 if (UNLIKELY(!selfOkay && offset == 0)) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001149 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch offset of zero not allowed at"
1150 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -07001151 return false;
1152 }
Elliott Hughes81ff3182012-03-23 20:35:56 -07001153 // Check for 32-bit overflow. This isn't strictly necessary if we can depend on the runtime
1154 // to have identical "wrap-around" behavior, but it's unwise to depend on that.
Andreas Gampe29e81422017-05-15 16:29:32 -07001155 if (UNLIKELY(((int64_t) cur_offset + (int64_t) offset) != (int64_t) (cur_offset + offset))) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001156 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch target overflow "
1157 << reinterpret_cast<void*>(cur_offset) << " +" << offset;
Ian Rogersd81871c2011-10-03 13:57:23 -07001158 return false;
1159 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001160 int32_t abs_offset = cur_offset + offset;
Andreas Gampe29e81422017-05-15 16:29:32 -07001161 if (UNLIKELY(abs_offset < 0 ||
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001162 (uint32_t) abs_offset >= code_item_accessor_.InsnsSizeInCodeUnits() ||
Andreas Gampe29e81422017-05-15 16:29:32 -07001163 !GetInstructionFlags(abs_offset).IsOpcode())) {
jeffhaod5347e02012-03-22 17:25:05 -07001164 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid branch target " << offset << " (-> "
Elliott Hughes398f64b2012-03-26 18:05:48 -07001165 << reinterpret_cast<void*>(abs_offset) << ") at "
1166 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -07001167 return false;
1168 }
Andreas Gampe51de69e2019-04-19 15:14:14 -07001169 GetModifiableInstructionFlags(abs_offset).SetBranchTarget();
Ian Rogersd81871c2011-10-03 13:57:23 -07001170 return true;
1171}
1172
Ian Rogers776ac1f2012-04-13 23:36:36 -07001173bool MethodVerifier::GetBranchOffset(uint32_t cur_offset, int32_t* pOffset, bool* pConditional,
Ian Rogersd81871c2011-10-03 13:57:23 -07001174 bool* selfOkay) {
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001175 const uint16_t* insns = code_item_accessor_.Insns() + cur_offset;
Ian Rogersd81871c2011-10-03 13:57:23 -07001176 *pConditional = false;
1177 *selfOkay = false;
jeffhaoba5ebb92011-08-25 17:24:37 -07001178 switch (*insns & 0xff) {
1179 case Instruction::GOTO:
1180 *pOffset = ((int16_t) *insns) >> 8;
jeffhaoba5ebb92011-08-25 17:24:37 -07001181 break;
1182 case Instruction::GOTO_32:
1183 *pOffset = insns[1] | (((uint32_t) insns[2]) << 16);
jeffhaoba5ebb92011-08-25 17:24:37 -07001184 *selfOkay = true;
1185 break;
1186 case Instruction::GOTO_16:
1187 *pOffset = (int16_t) insns[1];
jeffhaoba5ebb92011-08-25 17:24:37 -07001188 break;
1189 case Instruction::IF_EQ:
1190 case Instruction::IF_NE:
1191 case Instruction::IF_LT:
1192 case Instruction::IF_GE:
1193 case Instruction::IF_GT:
1194 case Instruction::IF_LE:
1195 case Instruction::IF_EQZ:
1196 case Instruction::IF_NEZ:
1197 case Instruction::IF_LTZ:
1198 case Instruction::IF_GEZ:
1199 case Instruction::IF_GTZ:
1200 case Instruction::IF_LEZ:
1201 *pOffset = (int16_t) insns[1];
1202 *pConditional = true;
jeffhaoba5ebb92011-08-25 17:24:37 -07001203 break;
1204 default:
1205 return false;
jeffhaoba5ebb92011-08-25 17:24:37 -07001206 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001207 return true;
1208}
1209
Ian Rogers776ac1f2012-04-13 23:36:36 -07001210bool MethodVerifier::CheckSwitchTargets(uint32_t cur_offset) {
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001211 const uint32_t insn_count = code_item_accessor_.InsnsSizeInCodeUnits();
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001212 DCHECK_LT(cur_offset, insn_count);
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001213 const uint16_t* insns = code_item_accessor_.Insns() + cur_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001214 /* make sure the start of the switch is in range */
Andreas Gampe53de99c2015-08-17 13:43:55 -07001215 int32_t switch_offset = insns[1] | (static_cast<int32_t>(insns[2]) << 16);
Andreas Gampe29e81422017-05-15 16:29:32 -07001216 if (UNLIKELY(static_cast<int32_t>(cur_offset) + switch_offset < 0 ||
1217 cur_offset + switch_offset + 2 > insn_count)) {
jeffhaod5347e02012-03-22 17:25:05 -07001218 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch start: at " << cur_offset
Brian Carlstrom93c33962013-07-26 10:37:43 -07001219 << ", switch offset " << switch_offset
1220 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001221 return false;
1222 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001223 /* offset to switch table is a relative branch-style offset */
Ian Rogersd81871c2011-10-03 13:57:23 -07001224 const uint16_t* switch_insns = insns + switch_offset;
Andreas Gampe57c47582015-07-01 22:05:59 -07001225 // Make sure the table is at an even dex pc, that is, 32-bit aligned.
Andreas Gampe29e81422017-05-15 16:29:32 -07001226 if (UNLIKELY(!IsAligned<4>(switch_insns))) {
jeffhaod5347e02012-03-22 17:25:05 -07001227 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned switch table: at " << cur_offset
1228 << ", switch offset " << switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001229 return false;
1230 }
Andreas Gampe57c47582015-07-01 22:05:59 -07001231 // Make sure the switch data is marked as an opcode. This ensures that it was reached when
1232 // traversing the code item linearly. It is an approximation for a by-spec padding value.
Andreas Gampe29e81422017-05-15 16:29:32 -07001233 if (UNLIKELY(!GetInstructionFlags(cur_offset + switch_offset).IsOpcode())) {
Andreas Gampe57c47582015-07-01 22:05:59 -07001234 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "switch table at " << cur_offset
1235 << ", switch offset " << switch_offset
1236 << " not correctly visited, probably bad padding.";
1237 return false;
1238 }
1239
David Brazdil5469d342015-09-25 16:57:53 +01001240 bool is_packed_switch = (*insns & 0xff) == Instruction::PACKED_SWITCH;
1241
Ian Rogersd81871c2011-10-03 13:57:23 -07001242 uint32_t switch_count = switch_insns[1];
David Brazdil5469d342015-09-25 16:57:53 +01001243 int32_t targets_offset;
Ian Rogersd81871c2011-10-03 13:57:23 -07001244 uint16_t expected_signature;
David Brazdil5469d342015-09-25 16:57:53 +01001245 if (is_packed_switch) {
jeffhaoba5ebb92011-08-25 17:24:37 -07001246 /* 0=sig, 1=count, 2/3=firstKey */
1247 targets_offset = 4;
jeffhaoba5ebb92011-08-25 17:24:37 -07001248 expected_signature = Instruction::kPackedSwitchSignature;
1249 } else {
1250 /* 0=sig, 1=count, 2..count*2 = keys */
jeffhaoba5ebb92011-08-25 17:24:37 -07001251 targets_offset = 2 + 2 * switch_count;
1252 expected_signature = Instruction::kSparseSwitchSignature;
1253 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001254 uint32_t table_size = targets_offset + switch_count * 2;
Andreas Gampe29e81422017-05-15 16:29:32 -07001255 if (UNLIKELY(switch_insns[0] != expected_signature)) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001256 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1257 << StringPrintf("wrong signature for switch table (%x, wanted %x)",
1258 switch_insns[0], expected_signature);
jeffhaoba5ebb92011-08-25 17:24:37 -07001259 return false;
1260 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001261 /* make sure the end of the switch is in range */
Andreas Gampe29e81422017-05-15 16:29:32 -07001262 if (UNLIKELY(cur_offset + switch_offset + table_size > (uint32_t) insn_count)) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001263 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch end: at " << cur_offset
1264 << ", switch offset " << switch_offset
1265 << ", end " << (cur_offset + switch_offset + table_size)
jeffhaod5347e02012-03-22 17:25:05 -07001266 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001267 return false;
1268 }
David Brazdil5469d342015-09-25 16:57:53 +01001269
1270 constexpr int32_t keys_offset = 2;
1271 if (switch_count > 1) {
1272 if (is_packed_switch) {
1273 /* for a packed switch, verify that keys do not overflow int32 */
1274 int32_t first_key = switch_insns[keys_offset] | (switch_insns[keys_offset + 1] << 16);
1275 int32_t max_first_key =
1276 std::numeric_limits<int32_t>::max() - (static_cast<int32_t>(switch_count) - 1);
Andreas Gampe29e81422017-05-15 16:29:32 -07001277 if (UNLIKELY(first_key > max_first_key)) {
David Brazdil5469d342015-09-25 16:57:53 +01001278 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid packed switch: first_key=" << first_key
1279 << ", switch_count=" << switch_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001280 return false;
1281 }
David Brazdil5469d342015-09-25 16:57:53 +01001282 } else {
1283 /* for a sparse switch, verify the keys are in ascending order */
1284 int32_t last_key = switch_insns[keys_offset] | (switch_insns[keys_offset + 1] << 16);
1285 for (uint32_t targ = 1; targ < switch_count; targ++) {
1286 int32_t key =
1287 static_cast<int32_t>(switch_insns[keys_offset + targ * 2]) |
1288 static_cast<int32_t>(switch_insns[keys_offset + targ * 2 + 1] << 16);
Andreas Gampe29e81422017-05-15 16:29:32 -07001289 if (UNLIKELY(key <= last_key)) {
David Brazdil5469d342015-09-25 16:57:53 +01001290 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid sparse switch: last key=" << last_key
1291 << ", this=" << key;
1292 return false;
1293 }
1294 last_key = key;
1295 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001296 }
1297 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001298 /* verify each switch target */
Ian Rogersd81871c2011-10-03 13:57:23 -07001299 for (uint32_t targ = 0; targ < switch_count; targ++) {
Andreas Gampe53de99c2015-08-17 13:43:55 -07001300 int32_t offset = static_cast<int32_t>(switch_insns[targets_offset + targ * 2]) |
1301 static_cast<int32_t>(switch_insns[targets_offset + targ * 2 + 1] << 16);
Ian Rogersd81871c2011-10-03 13:57:23 -07001302 int32_t abs_offset = cur_offset + offset;
Andreas Gampe29e81422017-05-15 16:29:32 -07001303 if (UNLIKELY(abs_offset < 0 ||
1304 abs_offset >= static_cast<int32_t>(insn_count) ||
1305 !GetInstructionFlags(abs_offset).IsOpcode())) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001306 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch target " << offset
1307 << " (-> " << reinterpret_cast<void*>(abs_offset) << ") at "
1308 << reinterpret_cast<void*>(cur_offset)
1309 << "[" << targ << "]";
jeffhaoba5ebb92011-08-25 17:24:37 -07001310 return false;
1311 }
Andreas Gampe51de69e2019-04-19 15:14:14 -07001312 GetModifiableInstructionFlags(abs_offset).SetBranchTarget();
Ian Rogersd81871c2011-10-03 13:57:23 -07001313 }
1314 return true;
1315}
1316
Ian Rogers776ac1f2012-04-13 23:36:36 -07001317bool MethodVerifier::CheckVarArgRegs(uint32_t vA, uint32_t arg[]) {
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001318 uint16_t registers_size = code_item_accessor_.RegistersSize();
Ian Rogersd81871c2011-10-03 13:57:23 -07001319 for (uint32_t idx = 0; idx < vA; idx++) {
Andreas Gampe29e81422017-05-15 16:29:32 -07001320 if (UNLIKELY(arg[idx] >= registers_size)) {
jeffhaod5347e02012-03-22 17:25:05 -07001321 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index (" << arg[idx]
1322 << ") in non-range invoke (>= " << registers_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001323 return false;
1324 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001325 }
1326
1327 return true;
1328}
1329
Ian Rogers776ac1f2012-04-13 23:36:36 -07001330bool MethodVerifier::CheckVarArgRangeRegs(uint32_t vA, uint32_t vC) {
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001331 uint16_t registers_size = code_item_accessor_.RegistersSize();
Ian Rogersd81871c2011-10-03 13:57:23 -07001332 // vA/vC are unsigned 8-bit/16-bit quantities for /range instructions, so there's no risk of
1333 // integer overflow when adding them here.
Andreas Gampe29e81422017-05-15 16:29:32 -07001334 if (UNLIKELY(vA + vC > registers_size)) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001335 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index " << vA << "+" << vC
1336 << " in range invoke (> " << registers_size << ")";
jeffhaoba5ebb92011-08-25 17:24:37 -07001337 return false;
1338 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001339 return true;
1340}
1341
Ian Rogers776ac1f2012-04-13 23:36:36 -07001342bool MethodVerifier::VerifyCodeFlow() {
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001343 const uint16_t registers_size = code_item_accessor_.RegistersSize();
jeffhaobdb76512011-09-07 11:43:16 -07001344
Ian Rogersd81871c2011-10-03 13:57:23 -07001345 /* Create and initialize table holding register status */
Brian Carlstrom93c33962013-07-26 10:37:43 -07001346 reg_table_.Init(kTrackCompilerInterestPoints,
1347 insn_flags_.get(),
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001348 code_item_accessor_.InsnsSizeInCodeUnits(),
Brian Carlstrom93c33962013-07-26 10:37:43 -07001349 registers_size,
1350 this);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07001351
Ian Rogersd0fbd852013-09-24 18:17:04 -07001352 work_line_.reset(RegisterLine::Create(registers_size, this));
1353 saved_line_.reset(RegisterLine::Create(registers_size, this));
jeffhaobdb76512011-09-07 11:43:16 -07001354
Ian Rogersd81871c2011-10-03 13:57:23 -07001355 /* Initialize register types of method arguments. */
1356 if (!SetTypesFromSignature()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001357 DCHECK_NE(failures_.size(), 0U);
1358 std::string prepend("Bad signature in ");
David Sehr709b0702016-10-13 09:12:37 -07001359 prepend += dex_file_->PrettyMethod(dex_method_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001360 PrependToLastFailMessage(prepend);
Ian Rogersd81871c2011-10-03 13:57:23 -07001361 return false;
1362 }
Andreas Gamped5ad72f2015-06-26 17:33:47 -07001363 // We may have a runtime failure here, clear.
1364 have_pending_runtime_throw_failure_ = false;
1365
Ian Rogersd81871c2011-10-03 13:57:23 -07001366 /* Perform code flow verification. */
1367 if (!CodeFlowVerifyMethod()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001368 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -07001369 return false;
jeffhaobdb76512011-09-07 11:43:16 -07001370 }
jeffhaobdb76512011-09-07 11:43:16 -07001371 return true;
1372}
1373
Ian Rogersad0b3a32012-04-16 14:50:24 -07001374std::ostream& MethodVerifier::DumpFailures(std::ostream& os) {
1375 DCHECK_EQ(failures_.size(), failure_messages_.size());
Jeff Hao4137f482013-11-22 11:44:57 -08001376 for (size_t i = 0; i < failures_.size(); ++i) {
1377 os << failure_messages_[i]->str() << "\n";
Ian Rogersad0b3a32012-04-16 14:50:24 -07001378 }
1379 return os;
1380}
1381
Ian Rogers776ac1f2012-04-13 23:36:36 -07001382void MethodVerifier::Dump(std::ostream& os) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001383 VariableIndentationOutputStream vios(&os);
1384 Dump(&vios);
1385}
1386
1387void MethodVerifier::Dump(VariableIndentationOutputStream* vios) {
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001388 if (!code_item_accessor_.HasCodeItem()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001389 vios->Stream() << "Native method\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001390 return;
jeffhaobdb76512011-09-07 11:43:16 -07001391 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001392 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001393 vios->Stream() << "Register Types:\n";
1394 ScopedIndentation indent1(vios);
1395 reg_types_.Dump(vios->Stream());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001396 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001397 vios->Stream() << "Dumping instructions and register lines:\n";
1398 ScopedIndentation indent1(vios);
Mathieu Chartier1d2d4ff2017-09-23 16:11:06 -07001399
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001400 for (const DexInstructionPcPair& inst : code_item_accessor_) {
Mathieu Chartier2b2bef22017-10-26 17:10:19 -07001401 const size_t dex_pc = inst.DexPc();
Andreas Gampe077d9db2018-01-19 18:54:14 -08001402
1403 // Might be asked to dump before the table is initialized.
1404 if (reg_table_.IsInitialized()) {
1405 RegisterLine* reg_line = reg_table_.GetLine(dex_pc);
1406 if (reg_line != nullptr) {
1407 vios->Stream() << reg_line->Dump(this) << "\n";
1408 }
jeffhaobdb76512011-09-07 11:43:16 -07001409 }
Andreas Gampe077d9db2018-01-19 18:54:14 -08001410
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001411 vios->Stream()
Mathieu Chartierde40d472015-10-15 17:47:48 -07001412 << StringPrintf("0x%04zx", dex_pc) << ": " << GetInstructionFlags(dex_pc).ToString() << " ";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001413 const bool kDumpHexOfInstruction = false;
1414 if (kDumpHexOfInstruction) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001415 vios->Stream() << inst->DumpHex(5) << " ";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001416 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001417 vios->Stream() << inst->DumpString(dex_file_) << "\n";
jeffhaoba5ebb92011-08-25 17:24:37 -07001418 }
jeffhaobdb76512011-09-07 11:43:16 -07001419}
1420
Ian Rogersd81871c2011-10-03 13:57:23 -07001421static bool IsPrimitiveDescriptor(char descriptor) {
1422 switch (descriptor) {
jeffhaobdb76512011-09-07 11:43:16 -07001423 case 'I':
1424 case 'C':
1425 case 'S':
1426 case 'B':
1427 case 'Z':
jeffhaobdb76512011-09-07 11:43:16 -07001428 case 'F':
1429 case 'D':
1430 case 'J':
Ian Rogersd81871c2011-10-03 13:57:23 -07001431 return true;
jeffhaobdb76512011-09-07 11:43:16 -07001432 default:
1433 return false;
1434 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001435}
1436
Ian Rogers776ac1f2012-04-13 23:36:36 -07001437bool MethodVerifier::SetTypesFromSignature() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001438 RegisterLine* reg_line = reg_table_.GetLine(0);
Andreas Gampeef0b1a12015-06-19 20:37:46 -07001439
1440 // Should have been verified earlier.
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001441 DCHECK_GE(code_item_accessor_.RegistersSize(), code_item_accessor_.InsSize());
Andreas Gampeef0b1a12015-06-19 20:37:46 -07001442
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001443 uint32_t arg_start = code_item_accessor_.RegistersSize() - code_item_accessor_.InsSize();
1444 size_t expected_args = code_item_accessor_.InsSize(); /* long/double count as two */
jeffhaobdb76512011-09-07 11:43:16 -07001445
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001446 // Include the "this" pointer.
Ian Rogersd81871c2011-10-03 13:57:23 -07001447 size_t cur_arg = 0;
Ian Rogersad0b3a32012-04-16 14:50:24 -07001448 if (!IsStatic()) {
Andreas Gampeef0b1a12015-06-19 20:37:46 -07001449 if (expected_args == 0) {
1450 // Expect at least a receiver.
1451 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected 0 args, but method is not static";
1452 return false;
1453 }
1454
Ian Rogersd81871c2011-10-03 13:57:23 -07001455 // If this is a constructor for a class other than java.lang.Object, mark the first ("this")
1456 // argument as uninitialized. This restricts field access until the superclass constructor is
1457 // called.
Ian Rogersd8f69b02014-09-10 21:43:52 +00001458 const RegType& declaring_class = GetDeclaringClass();
Andreas Gampef10b6e12015-08-12 10:48:12 -07001459 if (IsConstructor()) {
1460 if (declaring_class.IsJavaLangObject()) {
1461 // "this" is implicitly initialized.
1462 reg_line->SetThisInitialized();
Andreas Gampead238ce2015-08-24 21:13:08 -07001463 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, declaring_class);
Andreas Gampef10b6e12015-08-12 10:48:12 -07001464 } else {
Andreas Gampead238ce2015-08-24 21:13:08 -07001465 reg_line->SetRegisterType<LockOp::kClear>(
1466 this,
1467 arg_start + cur_arg,
1468 reg_types_.UninitializedThisArgument(declaring_class));
Andreas Gampef10b6e12015-08-12 10:48:12 -07001469 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001470 } else {
Andreas Gampead238ce2015-08-24 21:13:08 -07001471 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, declaring_class);
jeffhaobdb76512011-09-07 11:43:16 -07001472 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001473 cur_arg++;
jeffhaobdb76512011-09-07 11:43:16 -07001474 }
1475
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001476 const dex::ProtoId& proto_id =
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001477 dex_file_->GetMethodPrototype(dex_file_->GetMethodId(dex_method_idx_));
Ian Rogers0571d352011-11-03 19:51:38 -07001478 DexFileParameterIterator iterator(*dex_file_, proto_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07001479
1480 for (; iterator.HasNext(); iterator.Next()) {
1481 const char* descriptor = iterator.GetDescriptor();
Ian Rogers7b078e82014-09-10 14:44:24 -07001482 if (descriptor == nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001483 LOG(FATAL) << "Null descriptor";
1484 }
1485 if (cur_arg >= expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07001486 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1487 << " args, found more (" << descriptor << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001488 return false;
1489 }
1490 switch (descriptor[0]) {
1491 case 'L':
1492 case '[':
1493 // We assume that reference arguments are initialized. The only way it could be otherwise
1494 // (assuming the caller was verified) is if the current method is <init>, but in that case
1495 // it's effectively considered initialized the instant we reach here (in the sense that we
1496 // can return without doing anything or call virtual methods).
1497 {
Andreas Gampe98be1a92017-08-28 08:25:45 -07001498 // Note: don't check access. No error would be thrown for declaring or passing an
1499 // inaccessible class. Only actual accesses to fields or methods will.
1500 const RegType& reg_type = ResolveClass<CheckAccess::kNo>(iterator.GetTypeIdx());
Sebastien Hertz2ed76f92014-04-22 17:11:08 +02001501 if (!reg_type.IsNonZeroReferenceTypes()) {
1502 DCHECK(HasFailures());
1503 return false;
1504 }
Andreas Gampead238ce2015-08-24 21:13:08 -07001505 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001506 }
1507 break;
1508 case 'Z':
Andreas Gampead238ce2015-08-24 21:13:08 -07001509 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Boolean());
Ian Rogersd81871c2011-10-03 13:57:23 -07001510 break;
1511 case 'C':
Andreas Gampead238ce2015-08-24 21:13:08 -07001512 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Char());
Ian Rogersd81871c2011-10-03 13:57:23 -07001513 break;
1514 case 'B':
Andreas Gampead238ce2015-08-24 21:13:08 -07001515 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Byte());
Ian Rogersd81871c2011-10-03 13:57:23 -07001516 break;
1517 case 'I':
Andreas Gampead238ce2015-08-24 21:13:08 -07001518 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Integer());
Ian Rogersd81871c2011-10-03 13:57:23 -07001519 break;
1520 case 'S':
Andreas Gampead238ce2015-08-24 21:13:08 -07001521 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Short());
Ian Rogersd81871c2011-10-03 13:57:23 -07001522 break;
1523 case 'F':
Andreas Gampead238ce2015-08-24 21:13:08 -07001524 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Float());
Ian Rogersd81871c2011-10-03 13:57:23 -07001525 break;
1526 case 'J':
1527 case 'D': {
Andreas Gampe77cd4d62014-06-19 17:29:48 -07001528 if (cur_arg + 1 >= expected_args) {
1529 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1530 << " args, found more (" << descriptor << ")";
1531 return false;
1532 }
1533
Ian Rogers7b078e82014-09-10 14:44:24 -07001534 const RegType* lo_half;
1535 const RegType* hi_half;
1536 if (descriptor[0] == 'J') {
1537 lo_half = &reg_types_.LongLo();
1538 hi_half = &reg_types_.LongHi();
1539 } else {
1540 lo_half = &reg_types_.DoubleLo();
1541 hi_half = &reg_types_.DoubleHi();
1542 }
1543 reg_line->SetRegisterTypeWide(this, arg_start + cur_arg, *lo_half, *hi_half);
Ian Rogersd81871c2011-10-03 13:57:23 -07001544 cur_arg++;
1545 break;
1546 }
1547 default:
Brian Carlstrom93c33962013-07-26 10:37:43 -07001548 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected signature type char '"
1549 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001550 return false;
1551 }
1552 cur_arg++;
1553 }
1554 if (cur_arg != expected_args) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001555 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1556 << " arguments, found " << cur_arg;
Ian Rogersd81871c2011-10-03 13:57:23 -07001557 return false;
1558 }
1559 const char* descriptor = dex_file_->GetReturnTypeDescriptor(proto_id);
1560 // Validate return type. We don't do the type lookup; just want to make sure that it has the right
1561 // format. Only major difference from the method argument format is that 'V' is supported.
1562 bool result;
1563 if (IsPrimitiveDescriptor(descriptor[0]) || descriptor[0] == 'V') {
1564 result = descriptor[1] == '\0';
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001565 } else if (descriptor[0] == '[') { // single/multi-dimensional array of object/primitive
Ian Rogersd81871c2011-10-03 13:57:23 -07001566 size_t i = 0;
1567 do {
1568 i++;
1569 } while (descriptor[i] == '['); // process leading [
1570 if (descriptor[i] == 'L') { // object array
1571 do {
1572 i++; // find closing ;
1573 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1574 result = descriptor[i] == ';';
1575 } else { // primitive array
1576 result = IsPrimitiveDescriptor(descriptor[i]) && descriptor[i + 1] == '\0';
1577 }
1578 } else if (descriptor[0] == 'L') {
1579 // could be more thorough here, but shouldn't be required
1580 size_t i = 0;
1581 do {
1582 i++;
1583 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1584 result = descriptor[i] == ';';
1585 } else {
1586 result = false;
1587 }
1588 if (!result) {
jeffhaod5347e02012-03-22 17:25:05 -07001589 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected char in return type descriptor '"
1590 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001591 }
1592 return result;
jeffhaobdb76512011-09-07 11:43:16 -07001593}
1594
Ian Rogers776ac1f2012-04-13 23:36:36 -07001595bool MethodVerifier::CodeFlowVerifyMethod() {
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001596 const uint16_t* insns = code_item_accessor_.Insns();
1597 const uint32_t insns_size = code_item_accessor_.InsnsSizeInCodeUnits();
jeffhaoba5ebb92011-08-25 17:24:37 -07001598
jeffhaobdb76512011-09-07 11:43:16 -07001599 /* Begin by marking the first instruction as "changed". */
Andreas Gampe51de69e2019-04-19 15:14:14 -07001600 GetModifiableInstructionFlags(0).SetChanged();
Ian Rogersd81871c2011-10-03 13:57:23 -07001601 uint32_t start_guess = 0;
jeffhaoba5ebb92011-08-25 17:24:37 -07001602
jeffhaobdb76512011-09-07 11:43:16 -07001603 /* Continue until no instructions are marked "changed". */
1604 while (true) {
Mathieu Chartier4306ef82014-12-19 18:41:47 -08001605 if (allow_thread_suspension_) {
1606 self_->AllowThreadSuspension();
1607 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001608 // Find the first marked one. Use "start_guess" as a way to find one quickly.
1609 uint32_t insn_idx = start_guess;
1610 for (; insn_idx < insns_size; insn_idx++) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07001611 if (GetInstructionFlags(insn_idx).IsChanged())
jeffhaobdb76512011-09-07 11:43:16 -07001612 break;
1613 }
jeffhaobdb76512011-09-07 11:43:16 -07001614 if (insn_idx == insns_size) {
1615 if (start_guess != 0) {
1616 /* try again, starting from the top */
1617 start_guess = 0;
1618 continue;
1619 } else {
1620 /* all flags are clear */
1621 break;
1622 }
1623 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001624 // We carry the working set of registers from instruction to instruction. If this address can
1625 // be the target of a branch (or throw) instruction, or if we're skipping around chasing
1626 // "changed" flags, we need to load the set of registers from the table.
1627 // Because we always prefer to continue on to the next instruction, we should never have a
1628 // situation where we have a stray "changed" flag set on an instruction that isn't a branch
1629 // target.
1630 work_insn_idx_ = insn_idx;
Mathieu Chartierde40d472015-10-15 17:47:48 -07001631 if (GetInstructionFlags(insn_idx).IsBranchTarget()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001632 work_line_->CopyFromLine(reg_table_.GetLine(insn_idx));
Ian Rogersebbdd872014-07-07 23:53:08 -07001633 } else if (kIsDebugBuild) {
jeffhaobdb76512011-09-07 11:43:16 -07001634 /*
1635 * Sanity check: retrieve the stored register line (assuming
1636 * a full table) and make sure it actually matches.
1637 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001638 RegisterLine* register_line = reg_table_.GetLine(insn_idx);
Ian Rogers7b078e82014-09-10 14:44:24 -07001639 if (register_line != nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001640 if (work_line_->CompareLine(register_line) != 0) {
Andreas Gampe4a57d5f2019-04-03 12:31:18 -07001641 Dump(LOG_STREAM(FATAL_WITHOUT_ABORT));
1642 LOG(FATAL_WITHOUT_ABORT) << info_messages_.str();
David Sehr709b0702016-10-13 09:12:37 -07001643 LOG(FATAL) << "work_line diverged in " << dex_file_->PrettyMethod(dex_method_idx_)
Elliott Hughesc073b072012-05-24 19:29:17 -07001644 << "@" << reinterpret_cast<void*>(work_insn_idx_) << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07001645 << " work_line=" << work_line_->Dump(this) << "\n"
1646 << " expected=" << register_line->Dump(this);
Ian Rogersd81871c2011-10-03 13:57:23 -07001647 }
jeffhaobdb76512011-09-07 11:43:16 -07001648 }
jeffhaobdb76512011-09-07 11:43:16 -07001649 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001650 if (!CodeFlowVerifyInstruction(&start_guess)) {
David Sehr709b0702016-10-13 09:12:37 -07001651 std::string prepend(dex_file_->PrettyMethod(dex_method_idx_));
Ian Rogersad0b3a32012-04-16 14:50:24 -07001652 prepend += " failed to verify: ";
1653 PrependToLastFailMessage(prepend);
jeffhaoba5ebb92011-08-25 17:24:37 -07001654 return false;
1655 }
jeffhaobdb76512011-09-07 11:43:16 -07001656 /* Clear "changed" and mark as visited. */
Andreas Gampe51de69e2019-04-19 15:14:14 -07001657 GetModifiableInstructionFlags(insn_idx).SetVisited();
1658 GetModifiableInstructionFlags(insn_idx).ClearChanged();
jeffhaobdb76512011-09-07 11:43:16 -07001659 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001660
Andreas Gampe92d77202017-12-06 20:49:00 -08001661 if (UNLIKELY(VLOG_IS_ON(verifier_debug))) {
jeffhaobdb76512011-09-07 11:43:16 -07001662 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001663 * Scan for dead code. There's nothing "evil" about dead code
jeffhaobdb76512011-09-07 11:43:16 -07001664 * (besides the wasted space), but it indicates a flaw somewhere
1665 * down the line, possibly in the verifier.
1666 *
1667 * If we've substituted "always throw" instructions into the stream,
1668 * we are almost certainly going to have some dead code.
1669 */
1670 int dead_start = -1;
Mathieu Chartier1d2d4ff2017-09-23 16:11:06 -07001671
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001672 for (const DexInstructionPcPair& inst : code_item_accessor_) {
Mathieu Chartier2b2bef22017-10-26 17:10:19 -07001673 const uint32_t insn_idx = inst.DexPc();
jeffhaobdb76512011-09-07 11:43:16 -07001674 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001675 * Switch-statement data doesn't get "visited" by scanner. It
jeffhaobdb76512011-09-07 11:43:16 -07001676 * may or may not be preceded by a padding NOP (for alignment).
1677 */
1678 if (insns[insn_idx] == Instruction::kPackedSwitchSignature ||
1679 insns[insn_idx] == Instruction::kSparseSwitchSignature ||
1680 insns[insn_idx] == Instruction::kArrayDataSignature ||
Elliott Hughes380aaa72012-07-09 14:33:15 -07001681 (insns[insn_idx] == Instruction::NOP && (insn_idx + 1 < insns_size) &&
jeffhaobdb76512011-09-07 11:43:16 -07001682 (insns[insn_idx + 1] == Instruction::kPackedSwitchSignature ||
1683 insns[insn_idx + 1] == Instruction::kSparseSwitchSignature ||
1684 insns[insn_idx + 1] == Instruction::kArrayDataSignature))) {
Andreas Gampe51de69e2019-04-19 15:14:14 -07001685 GetModifiableInstructionFlags(insn_idx).SetVisited();
jeffhaobdb76512011-09-07 11:43:16 -07001686 }
1687
Mathieu Chartierde40d472015-10-15 17:47:48 -07001688 if (!GetInstructionFlags(insn_idx).IsVisited()) {
Mathieu Chartier1d2d4ff2017-09-23 16:11:06 -07001689 if (dead_start < 0) {
jeffhaobdb76512011-09-07 11:43:16 -07001690 dead_start = insn_idx;
Mathieu Chartier1d2d4ff2017-09-23 16:11:06 -07001691 }
jeffhaobdb76512011-09-07 11:43:16 -07001692 } else if (dead_start >= 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001693 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start)
1694 << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaobdb76512011-09-07 11:43:16 -07001695 dead_start = -1;
1696 }
1697 }
1698 if (dead_start >= 0) {
Mathieu Chartier1d2d4ff2017-09-23 16:11:06 -07001699 LogVerifyInfo()
1700 << "dead code " << reinterpret_cast<void*>(dead_start)
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001701 << "-" << reinterpret_cast<void*>(code_item_accessor_.InsnsSizeInCodeUnits() - 1);
jeffhaoba5ebb92011-08-25 17:24:37 -07001702 }
Ian Rogersc9e463c2013-06-05 16:52:26 -07001703 // To dump the state of the verify after a method, do something like:
David Sehr709b0702016-10-13 09:12:37 -07001704 // if (dex_file_->PrettyMethod(dex_method_idx_) ==
Ian Rogersc9e463c2013-06-05 16:52:26 -07001705 // "boolean java.lang.String.equals(java.lang.Object)") {
1706 // LOG(INFO) << info_messages_.str();
1707 // }
jeffhaoba5ebb92011-08-25 17:24:37 -07001708 }
jeffhaobdb76512011-09-07 11:43:16 -07001709 return true;
1710}
1711
Andreas Gampe68df3202015-06-22 11:35:46 -07001712// Returns the index of the first final instance field of the given class, or kDexNoIndex if there
1713// is no such field.
Andreas Gampea5b09a62016-11-17 15:21:22 -08001714static uint32_t GetFirstFinalInstanceFieldIndex(const DexFile& dex_file, dex::TypeIndex type_idx) {
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001715 const dex::ClassDef* class_def = dex_file.FindClassDef(type_idx);
Andreas Gampe68df3202015-06-22 11:35:46 -07001716 DCHECK(class_def != nullptr);
Mathieu Chartier1f1cb9f2018-06-04 09:22:46 -07001717 ClassAccessor accessor(dex_file, *class_def);
1718 for (const ClassAccessor::Field& field : accessor.GetInstanceFields()) {
1719 if (field.IsFinal()) {
1720 return field.GetIndex();
Andreas Gampe68df3202015-06-22 11:35:46 -07001721 }
Andreas Gampe68df3202015-06-22 11:35:46 -07001722 }
Andreas Gampee2abbc62017-09-15 11:59:26 -07001723 return dex::kDexNoIndex;
Andreas Gampe68df3202015-06-22 11:35:46 -07001724}
1725
Andreas Gampea727e372015-08-25 09:22:37 -07001726// Setup a register line for the given return instruction.
1727static void AdjustReturnLine(MethodVerifier* verifier,
1728 const Instruction* ret_inst,
1729 RegisterLine* line) {
1730 Instruction::Code opcode = ret_inst->Opcode();
1731
1732 switch (opcode) {
1733 case Instruction::RETURN_VOID:
1734 case Instruction::RETURN_VOID_NO_BARRIER:
Andreas Gampe51de69e2019-04-19 15:14:14 -07001735 if (verifier->IsInstanceConstructor()) {
1736 // Before we mark all regs as conflicts, check that we don't have an uninitialized this.
1737 line->CheckConstructorReturn(verifier);
1738 }
1739 line->MarkAllRegistersAsConflicts(verifier);
Andreas Gampea727e372015-08-25 09:22:37 -07001740 break;
1741
1742 case Instruction::RETURN:
1743 case Instruction::RETURN_OBJECT:
1744 line->MarkAllRegistersAsConflictsExcept(verifier, ret_inst->VRegA_11x());
1745 break;
1746
1747 case Instruction::RETURN_WIDE:
1748 line->MarkAllRegistersAsConflictsExceptWide(verifier, ret_inst->VRegA_11x());
1749 break;
1750
1751 default:
1752 LOG(FATAL) << "Unknown return opcode " << opcode;
1753 UNREACHABLE();
1754 }
1755}
1756
Ian Rogers776ac1f2012-04-13 23:36:36 -07001757bool MethodVerifier::CodeFlowVerifyInstruction(uint32_t* start_guess) {
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001758 // If we're doing FindLocksAtDexPc, check whether we're at the dex pc we care about.
1759 // We want the state _before_ the instruction, for the case where the dex pc we're
1760 // interested in is itself a monitor-enter instruction (which is a likely place
1761 // for a thread to be suspended).
Ian Rogers7b078e82014-09-10 14:44:24 -07001762 if (monitor_enter_dex_pcs_ != nullptr && work_insn_idx_ == interesting_dex_pc_) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001763 monitor_enter_dex_pcs_->clear(); // The new work line is more accurate than the previous one.
Andreas Gampeaaf0d382017-11-27 14:10:21 -08001764
1765 std::map<uint32_t, DexLockInfo> depth_to_lock_info;
1766 auto collector = [&](uint32_t dex_reg, uint32_t depth) {
1767 auto insert_pair = depth_to_lock_info.emplace(depth, DexLockInfo(depth));
1768 auto it = insert_pair.first;
1769 auto set_insert_pair = it->second.dex_registers.insert(dex_reg);
1770 DCHECK(set_insert_pair.second);
1771 };
1772 work_line_->IterateRegToLockDepths(collector);
1773 for (auto& pair : depth_to_lock_info) {
1774 monitor_enter_dex_pcs_->push_back(pair.second);
1775 // Map depth to dex PC.
1776 (*monitor_enter_dex_pcs_)[monitor_enter_dex_pcs_->size() - 1].dex_pc =
1777 work_line_->GetMonitorEnterDexPc(pair.second.dex_pc);
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001778 }
1779 }
1780
jeffhaobdb76512011-09-07 11:43:16 -07001781 /*
1782 * Once we finish decoding the instruction, we need to figure out where
jeffhaod1f0fde2011-09-08 17:25:33 -07001783 * we can go from here. There are three possible ways to transfer
jeffhaobdb76512011-09-07 11:43:16 -07001784 * control to another statement:
1785 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001786 * (1) Continue to the next instruction. Applies to all but
jeffhaobdb76512011-09-07 11:43:16 -07001787 * unconditional branches, method returns, and exception throws.
jeffhaod1f0fde2011-09-08 17:25:33 -07001788 * (2) Branch to one or more possible locations. Applies to branches
jeffhaobdb76512011-09-07 11:43:16 -07001789 * and switch statements.
jeffhaod1f0fde2011-09-08 17:25:33 -07001790 * (3) Exception handlers. Applies to any instruction that can
jeffhaobdb76512011-09-07 11:43:16 -07001791 * throw an exception that is handled by an encompassing "try"
1792 * block.
1793 *
1794 * We can also return, in which case there is no successor instruction
1795 * from this point.
1796 *
Elliott Hughesadb8c672012-03-06 16:49:32 -08001797 * The behavior can be determined from the opcode flags.
jeffhaobdb76512011-09-07 11:43:16 -07001798 */
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001799 const uint16_t* insns = code_item_accessor_.Insns() + work_insn_idx_;
Ian Rogersd81871c2011-10-03 13:57:23 -07001800 const Instruction* inst = Instruction::At(insns);
Ian Rogersa75a0132012-09-28 11:41:42 -07001801 int opcode_flags = Instruction::FlagsOf(inst->Opcode());
jeffhaobdb76512011-09-07 11:43:16 -07001802
jeffhaobdb76512011-09-07 11:43:16 -07001803 int32_t branch_target = 0;
jeffhaobdb76512011-09-07 11:43:16 -07001804 bool just_set_result = false;
Andreas Gampe92d77202017-12-06 20:49:00 -08001805 if (UNLIKELY(VLOG_IS_ON(verifier_debug))) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001806 // Generate processing back trace to debug verifier
Andreas Gampe2ad6cce2019-04-11 16:17:39 -07001807 LogVerifyInfo() << "Processing " << inst->DumpString(dex_file_) << std::endl
1808 << work_line_->Dump(this);
Ian Rogersd81871c2011-10-03 13:57:23 -07001809 }
jeffhaobdb76512011-09-07 11:43:16 -07001810
1811 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001812 * Make a copy of the previous register state. If the instruction
jeffhaobdb76512011-09-07 11:43:16 -07001813 * can throw an exception, we will copy/merge this into the "catch"
1814 * address rather than work_line, because we don't want the result
1815 * from the "successful" code path (e.g. a check-cast that "improves"
1816 * a type) to be visible to the exception handler.
1817 */
Ian Rogers776ac1f2012-04-13 23:36:36 -07001818 if ((opcode_flags & Instruction::kThrow) != 0 && CurrentInsnFlags()->IsInTry()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001819 saved_line_->CopyFromLine(work_line_.get());
Ian Rogers1ff3c982014-08-12 02:30:58 -07001820 } else if (kIsDebugBuild) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001821 saved_line_->FillWithGarbage();
jeffhaobdb76512011-09-07 11:43:16 -07001822 }
Andreas Gamped12e7822015-06-25 10:26:40 -07001823 DCHECK(!have_pending_runtime_throw_failure_); // Per-instruction flag, should not be set here.
jeffhaobdb76512011-09-07 11:43:16 -07001824
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07001825
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001826 // We need to ensure the work line is consistent while performing validation. When we spot a
1827 // peephole pattern we compute a new line for either the fallthrough instruction or the
1828 // branch target.
Mathieu Chartier361e04a2016-02-16 14:06:35 -08001829 RegisterLineArenaUniquePtr branch_line;
1830 RegisterLineArenaUniquePtr fallthrough_line;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001831
Sebastien Hertz5243e912013-05-21 10:55:07 +02001832 switch (inst->Opcode()) {
jeffhaobdb76512011-09-07 11:43:16 -07001833 case Instruction::NOP:
1834 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001835 * A "pure" NOP has no effect on anything. Data tables start with
jeffhaobdb76512011-09-07 11:43:16 -07001836 * a signature that looks like a NOP; if we see one of these in
1837 * the course of executing code then we have a problem.
1838 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001839 if (inst->VRegA_10x() != 0) {
jeffhaod5347e02012-03-22 17:25:05 -07001840 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "encountered data table in instruction stream";
jeffhaobdb76512011-09-07 11:43:16 -07001841 }
1842 break;
1843
1844 case Instruction::MOVE:
Ian Rogers7b078e82014-09-10 14:44:24 -07001845 work_line_->CopyRegister1(this, inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategory1nr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001846 break;
jeffhaobdb76512011-09-07 11:43:16 -07001847 case Instruction::MOVE_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001848 work_line_->CopyRegister1(this, inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategory1nr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001849 break;
jeffhaobdb76512011-09-07 11:43:16 -07001850 case Instruction::MOVE_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001851 work_line_->CopyRegister1(this, inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategory1nr);
jeffhaobdb76512011-09-07 11:43:16 -07001852 break;
1853 case Instruction::MOVE_WIDE:
Ian Rogers7b078e82014-09-10 14:44:24 -07001854 work_line_->CopyRegister2(this, inst->VRegA_12x(), inst->VRegB_12x());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001855 break;
jeffhaobdb76512011-09-07 11:43:16 -07001856 case Instruction::MOVE_WIDE_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001857 work_line_->CopyRegister2(this, inst->VRegA_22x(), inst->VRegB_22x());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001858 break;
jeffhaobdb76512011-09-07 11:43:16 -07001859 case Instruction::MOVE_WIDE_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001860 work_line_->CopyRegister2(this, inst->VRegA_32x(), inst->VRegB_32x());
jeffhaobdb76512011-09-07 11:43:16 -07001861 break;
1862 case Instruction::MOVE_OBJECT:
Ian Rogers7b078e82014-09-10 14:44:24 -07001863 work_line_->CopyRegister1(this, inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategoryRef);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001864 break;
jeffhaobdb76512011-09-07 11:43:16 -07001865 case Instruction::MOVE_OBJECT_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001866 work_line_->CopyRegister1(this, inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategoryRef);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001867 break;
jeffhaobdb76512011-09-07 11:43:16 -07001868 case Instruction::MOVE_OBJECT_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001869 work_line_->CopyRegister1(this, inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategoryRef);
jeffhaobdb76512011-09-07 11:43:16 -07001870 break;
1871
1872 /*
1873 * The move-result instructions copy data out of a "pseudo-register"
jeffhaod1f0fde2011-09-08 17:25:33 -07001874 * with the results from the last method invocation. In practice we
jeffhaobdb76512011-09-07 11:43:16 -07001875 * might want to hold the result in an actual CPU register, so the
1876 * Dalvik spec requires that these only appear immediately after an
1877 * invoke or filled-new-array.
1878 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001879 * These calls invalidate the "result" register. (This is now
jeffhaobdb76512011-09-07 11:43:16 -07001880 * redundant with the reset done below, but it can make the debug info
1881 * easier to read in some cases.)
1882 */
1883 case Instruction::MOVE_RESULT:
Ian Rogers7b078e82014-09-10 14:44:24 -07001884 work_line_->CopyResultRegister1(this, inst->VRegA_11x(), false);
jeffhaobdb76512011-09-07 11:43:16 -07001885 break;
1886 case Instruction::MOVE_RESULT_WIDE:
Ian Rogers7b078e82014-09-10 14:44:24 -07001887 work_line_->CopyResultRegister2(this, inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07001888 break;
1889 case Instruction::MOVE_RESULT_OBJECT:
Ian Rogers7b078e82014-09-10 14:44:24 -07001890 work_line_->CopyResultRegister1(this, inst->VRegA_11x(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001891 break;
1892
Ian Rogersd81871c2011-10-03 13:57:23 -07001893 case Instruction::MOVE_EXCEPTION: {
Sebastien Hertz270a0e12015-01-16 19:49:09 +01001894 // We do not allow MOVE_EXCEPTION as the first instruction in a method. This is a simple case
1895 // where one entrypoint to the catch block is not actually an exception path.
1896 if (work_insn_idx_ == 0) {
1897 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "move-exception at pc 0x0";
1898 break;
1899 }
jeffhaobdb76512011-09-07 11:43:16 -07001900 /*
jeffhao60f83e32012-02-13 17:16:30 -08001901 * This statement can only appear as the first instruction in an exception handler. We verify
1902 * that as part of extracting the exception type from the catch block list.
jeffhaobdb76512011-09-07 11:43:16 -07001903 */
Ian Rogersd8f69b02014-09-10 21:43:52 +00001904 const RegType& res_type = GetCaughtExceptionType();
Andreas Gampead238ce2015-08-24 21:13:08 -07001905 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_11x(), res_type);
jeffhaobdb76512011-09-07 11:43:16 -07001906 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001907 }
jeffhaobdb76512011-09-07 11:43:16 -07001908 case Instruction::RETURN_VOID:
Andreas Gampef10b6e12015-08-12 10:48:12 -07001909 if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001910 if (!GetMethodReturnType().IsConflict()) {
jeffhaod5347e02012-03-22 17:25:05 -07001911 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001912 }
jeffhaobdb76512011-09-07 11:43:16 -07001913 }
1914 break;
1915 case Instruction::RETURN:
Andreas Gampef10b6e12015-08-12 10:48:12 -07001916 if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) {
jeffhaobdb76512011-09-07 11:43:16 -07001917 /* check the method signature */
Ian Rogersd8f69b02014-09-10 21:43:52 +00001918 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07001919 if (!return_type.IsCategory1Types()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001920 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected non-category 1 return type "
1921 << return_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001922 } else {
1923 // Compilers may generate synthetic functions that write byte values into boolean fields.
1924 // Also, it may use integer values for boolean, byte, short, and character return types.
Sebastien Hertz5243e912013-05-21 10:55:07 +02001925 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07001926 const RegType& src_type = work_line_->GetRegisterType(this, vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07001927 bool use_src = ((return_type.IsBoolean() && src_type.IsByte()) ||
1928 ((return_type.IsBoolean() || return_type.IsByte() ||
1929 return_type.IsShort() || return_type.IsChar()) &&
1930 src_type.IsInteger()));
1931 /* check the register contents */
Ian Rogersad0b3a32012-04-16 14:50:24 -07001932 bool success =
Ian Rogers7b078e82014-09-10 14:44:24 -07001933 work_line_->VerifyRegisterType(this, vregA, use_src ? src_type : return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001934 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001935 AppendToLastFailMessage(StringPrintf(" return-1nr on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07001936 }
jeffhaobdb76512011-09-07 11:43:16 -07001937 }
1938 }
1939 break;
1940 case Instruction::RETURN_WIDE:
Andreas Gampef10b6e12015-08-12 10:48:12 -07001941 if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) {
jeffhaobdb76512011-09-07 11:43:16 -07001942 /* check the method signature */
Ian Rogersd8f69b02014-09-10 21:43:52 +00001943 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07001944 if (!return_type.IsCategory2Types()) {
jeffhaod5347e02012-03-22 17:25:05 -07001945 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-wide not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001946 } else {
1947 /* check the register contents */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001948 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07001949 bool success = work_line_->VerifyRegisterType(this, vregA, return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001950 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001951 AppendToLastFailMessage(StringPrintf(" return-wide on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07001952 }
jeffhaobdb76512011-09-07 11:43:16 -07001953 }
1954 }
1955 break;
1956 case Instruction::RETURN_OBJECT:
Andreas Gampef10b6e12015-08-12 10:48:12 -07001957 if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) {
Ian Rogersd8f69b02014-09-10 21:43:52 +00001958 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07001959 if (!return_type.IsReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001960 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-object not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001961 } else {
1962 /* return_type is the *expected* return type, not register value */
Andreas Gampeeed3a5d2017-11-29 14:58:34 -08001963 DCHECK(!return_type.IsZeroOrNull());
Ian Rogersd81871c2011-10-03 13:57:23 -07001964 DCHECK(!return_type.IsUninitializedReference());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001965 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07001966 const RegType& reg_type = work_line_->GetRegisterType(this, vregA);
Andreas Gampea32210c2015-06-24 10:26:13 -07001967 // Disallow returning undefined, conflict & uninitialized values and verify that the
1968 // reference in vAA is an instance of the "return_type."
1969 if (reg_type.IsUndefined()) {
1970 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning undefined register";
1971 } else if (reg_type.IsConflict()) {
1972 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning register with conflict";
1973 } else if (reg_type.IsUninitializedTypes()) {
David Brazdil68b5c0b2016-01-19 14:25:29 +00001974 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning uninitialized object '"
Brian Carlstrom93c33962013-07-26 10:37:43 -07001975 << reg_type << "'";
Andreas Gampea4c98f22015-11-06 16:24:49 -08001976 } else if (!reg_type.IsReferenceTypes()) {
1977 // We really do expect a reference here.
1978 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-object returns a non-reference type "
1979 << reg_type;
David Brazdilca3c8c32016-09-06 14:04:48 +01001980 } else if (!return_type.IsAssignableFrom(reg_type, this)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07001981 if (reg_type.IsUnresolvedTypes() || return_type.IsUnresolvedTypes()) {
1982 Fail(VERIFY_ERROR_NO_CLASS) << " can't resolve returned type '" << return_type
1983 << "' or '" << reg_type << "'";
1984 } else {
Andreas Gampe16f149c2015-03-23 10:10:20 -07001985 bool soft_error = false;
1986 // Check whether arrays are involved. They will show a valid class status, even
1987 // if their components are erroneous.
1988 if (reg_type.IsArrayTypes() && return_type.IsArrayTypes()) {
David Brazdilca3c8c32016-09-06 14:04:48 +01001989 return_type.CanAssignArray(reg_type, reg_types_, class_loader_, this, &soft_error);
Andreas Gampe16f149c2015-03-23 10:10:20 -07001990 if (soft_error) {
1991 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "array with erroneous component type: "
1992 << reg_type << " vs " << return_type;
1993 }
1994 }
1995
1996 if (!soft_error) {
1997 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning '" << reg_type
1998 << "', but expected from declaration '" << return_type << "'";
1999 }
Jeff Haoa3faaf42013-09-03 19:07:00 -07002000 }
jeffhaobdb76512011-09-07 11:43:16 -07002001 }
2002 }
2003 }
2004 break;
2005
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07002006 /* could be boolean, int, float, or a null reference */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002007 case Instruction::CONST_4: {
2008 int32_t val = static_cast<int32_t>(inst->VRegB_11n() << 28) >> 28;
Andreas Gampead238ce2015-08-24 21:13:08 -07002009 work_line_->SetRegisterType<LockOp::kClear>(
2010 this, inst->VRegA_11n(), DetermineCat1Constant(val, need_precise_constants_));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07002011 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02002012 }
2013 case Instruction::CONST_16: {
2014 int16_t val = static_cast<int16_t>(inst->VRegB_21s());
Andreas Gampead238ce2015-08-24 21:13:08 -07002015 work_line_->SetRegisterType<LockOp::kClear>(
2016 this, inst->VRegA_21s(), DetermineCat1Constant(val, need_precise_constants_));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07002017 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02002018 }
Sebastien Hertz849600b2013-12-20 10:28:08 +01002019 case Instruction::CONST: {
2020 int32_t val = inst->VRegB_31i();
Andreas Gampead238ce2015-08-24 21:13:08 -07002021 work_line_->SetRegisterType<LockOp::kClear>(
2022 this, inst->VRegA_31i(), DetermineCat1Constant(val, need_precise_constants_));
jeffhaobdb76512011-09-07 11:43:16 -07002023 break;
Sebastien Hertz849600b2013-12-20 10:28:08 +01002024 }
2025 case Instruction::CONST_HIGH16: {
2026 int32_t val = static_cast<int32_t>(inst->VRegB_21h() << 16);
Andreas Gampead238ce2015-08-24 21:13:08 -07002027 work_line_->SetRegisterType<LockOp::kClear>(
2028 this, inst->VRegA_21h(), DetermineCat1Constant(val, need_precise_constants_));
jeffhaobdb76512011-09-07 11:43:16 -07002029 break;
Sebastien Hertz849600b2013-12-20 10:28:08 +01002030 }
jeffhaobdb76512011-09-07 11:43:16 -07002031 /* could be long or double; resolved upon use */
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002032 case Instruction::CONST_WIDE_16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002033 int64_t val = static_cast<int16_t>(inst->VRegB_21s());
Ian Rogersd8f69b02014-09-10 21:43:52 +00002034 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
2035 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07002036 work_line_->SetRegisterTypeWide(this, inst->VRegA_21s(), lo, hi);
jeffhaobdb76512011-09-07 11:43:16 -07002037 break;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002038 }
2039 case Instruction::CONST_WIDE_32: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002040 int64_t val = static_cast<int32_t>(inst->VRegB_31i());
Ian Rogersd8f69b02014-09-10 21:43:52 +00002041 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
2042 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07002043 work_line_->SetRegisterTypeWide(this, inst->VRegA_31i(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002044 break;
2045 }
2046 case Instruction::CONST_WIDE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002047 int64_t val = inst->VRegB_51l();
Ian Rogersd8f69b02014-09-10 21:43:52 +00002048 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
2049 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07002050 work_line_->SetRegisterTypeWide(this, inst->VRegA_51l(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002051 break;
2052 }
2053 case Instruction::CONST_WIDE_HIGH16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002054 int64_t val = static_cast<uint64_t>(inst->VRegB_21h()) << 48;
Ian Rogersd8f69b02014-09-10 21:43:52 +00002055 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
2056 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07002057 work_line_->SetRegisterTypeWide(this, inst->VRegA_21h(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002058 break;
2059 }
jeffhaobdb76512011-09-07 11:43:16 -07002060 case Instruction::CONST_STRING:
Andreas Gampead238ce2015-08-24 21:13:08 -07002061 work_line_->SetRegisterType<LockOp::kClear>(
2062 this, inst->VRegA_21c(), reg_types_.JavaLangString());
Sebastien Hertz5243e912013-05-21 10:55:07 +02002063 break;
jeffhaobdb76512011-09-07 11:43:16 -07002064 case Instruction::CONST_STRING_JUMBO:
Andreas Gampead238ce2015-08-24 21:13:08 -07002065 work_line_->SetRegisterType<LockOp::kClear>(
2066 this, inst->VRegA_31c(), reg_types_.JavaLangString());
jeffhaobdb76512011-09-07 11:43:16 -07002067 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002068 case Instruction::CONST_CLASS: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002069 // Get type from instruction if unresolved then we need an access check
2070 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Andreas Gampe98be1a92017-08-28 08:25:45 -07002071 const RegType& res_type = ResolveClass<CheckAccess::kYes>(dex::TypeIndex(inst->VRegB_21c()));
Ian Rogersad0b3a32012-04-16 14:50:24 -07002072 // Register holds class, ie its type is class, on error it will hold Conflict.
Andreas Gampead238ce2015-08-24 21:13:08 -07002073 work_line_->SetRegisterType<LockOp::kClear>(
2074 this, inst->VRegA_21c(), res_type.IsConflict() ? res_type
2075 : reg_types_.JavaLangClass());
jeffhaobdb76512011-09-07 11:43:16 -07002076 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002077 }
Orion Hodson2e599942017-09-22 16:17:41 +01002078 case Instruction::CONST_METHOD_HANDLE:
2079 work_line_->SetRegisterType<LockOp::kClear>(
2080 this, inst->VRegA_21c(), reg_types_.JavaLangInvokeMethodHandle());
Orion Hodson2e599942017-09-22 16:17:41 +01002081 break;
2082 case Instruction::CONST_METHOD_TYPE:
2083 work_line_->SetRegisterType<LockOp::kClear>(
2084 this, inst->VRegA_21c(), reg_types_.JavaLangInvokeMethodType());
Orion Hodson2e599942017-09-22 16:17:41 +01002085 break;
jeffhaobdb76512011-09-07 11:43:16 -07002086 case Instruction::MONITOR_ENTER:
Ian Rogers7b078e82014-09-10 14:44:24 -07002087 work_line_->PushMonitor(this, inst->VRegA_11x(), work_insn_idx_);
Andreas Gampec1474102015-08-18 08:57:44 -07002088 // Check whether the previous instruction is a move-object with vAA as a source, creating
2089 // untracked lock aliasing.
Mathieu Chartierde40d472015-10-15 17:47:48 -07002090 if (0 != work_insn_idx_ && !GetInstructionFlags(work_insn_idx_).IsBranchTarget()) {
Andreas Gampec1474102015-08-18 08:57:44 -07002091 uint32_t prev_idx = work_insn_idx_ - 1;
Mathieu Chartierde40d472015-10-15 17:47:48 -07002092 while (0 != prev_idx && !GetInstructionFlags(prev_idx).IsOpcode()) {
Andreas Gampec1474102015-08-18 08:57:44 -07002093 prev_idx--;
2094 }
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08002095 const Instruction& prev_inst = code_item_accessor_.InstructionAt(prev_idx);
Mathieu Chartier1d2d4ff2017-09-23 16:11:06 -07002096 switch (prev_inst.Opcode()) {
Andreas Gampec1474102015-08-18 08:57:44 -07002097 case Instruction::MOVE_OBJECT:
2098 case Instruction::MOVE_OBJECT_16:
2099 case Instruction::MOVE_OBJECT_FROM16:
Mathieu Chartier1d2d4ff2017-09-23 16:11:06 -07002100 if (prev_inst.VRegB() == inst->VRegA_11x()) {
Andreas Gampec1474102015-08-18 08:57:44 -07002101 // Redo the copy. This won't change the register types, but update the lock status
2102 // for the aliased register.
2103 work_line_->CopyRegister1(this,
Mathieu Chartier1d2d4ff2017-09-23 16:11:06 -07002104 prev_inst.VRegA(),
2105 prev_inst.VRegB(),
Andreas Gampec1474102015-08-18 08:57:44 -07002106 kTypeCategoryRef);
2107 }
2108 break;
2109
Alexey Grebenkince750492018-05-31 23:42:20 +03002110 // Catch a case of register aliasing when two registers are linked to the same
2111 // java.lang.Class object via two consequent const-class instructions immediately
2112 // preceding monitor-enter called on one of those registers.
2113 case Instruction::CONST_CLASS: {
2114 // Get the second previous instruction.
2115 if (prev_idx == 0 || GetInstructionFlags(prev_idx).IsBranchTarget()) {
2116 break;
2117 }
2118 prev_idx--;
2119 while (0 != prev_idx && !GetInstructionFlags(prev_idx).IsOpcode()) {
2120 prev_idx--;
2121 }
2122 const Instruction& prev2_inst = code_item_accessor_.InstructionAt(prev_idx);
2123
2124 // Match the pattern "const-class; const-class; monitor-enter;"
2125 if (prev2_inst.Opcode() != Instruction::CONST_CLASS) {
2126 break;
2127 }
2128
2129 // Ensure both const-classes are called for the same type_idx.
2130 if (prev_inst.VRegB_21c() != prev2_inst.VRegB_21c()) {
2131 break;
2132 }
2133
2134 // Update the lock status for the aliased register.
2135 if (prev_inst.VRegA() == inst->VRegA_11x()) {
2136 work_line_->CopyRegister1(this,
2137 prev2_inst.VRegA(),
2138 inst->VRegA_11x(),
2139 kTypeCategoryRef);
2140 } else if (prev2_inst.VRegA() == inst->VRegA_11x()) {
2141 work_line_->CopyRegister1(this,
2142 prev_inst.VRegA(),
2143 inst->VRegA_11x(),
2144 kTypeCategoryRef);
2145 }
2146 break;
2147 }
2148
Andreas Gampec1474102015-08-18 08:57:44 -07002149 default: // Other instruction types ignored.
2150 break;
2151 }
2152 }
jeffhaobdb76512011-09-07 11:43:16 -07002153 break;
2154 case Instruction::MONITOR_EXIT:
2155 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002156 * monitor-exit instructions are odd. They can throw exceptions,
jeffhaobdb76512011-09-07 11:43:16 -07002157 * but when they do they act as if they succeeded and the PC is
jeffhaod1f0fde2011-09-08 17:25:33 -07002158 * pointing to the following instruction. (This behavior goes back
jeffhaobdb76512011-09-07 11:43:16 -07002159 * to the need to handle asynchronous exceptions, a now-deprecated
2160 * feature that Dalvik doesn't support.)
2161 *
jeffhaod1f0fde2011-09-08 17:25:33 -07002162 * In practice we don't need to worry about this. The only
jeffhaobdb76512011-09-07 11:43:16 -07002163 * exceptions that can be thrown from monitor-exit are for a
jeffhaod1f0fde2011-09-08 17:25:33 -07002164 * null reference and -exit without a matching -enter. If the
jeffhaobdb76512011-09-07 11:43:16 -07002165 * structured locking checks are working, the former would have
2166 * failed on the -enter instruction, and the latter is impossible.
2167 *
2168 * This is fortunate, because issue 3221411 prevents us from
2169 * chasing the "can throw" path when monitor verification is
jeffhaod1f0fde2011-09-08 17:25:33 -07002170 * enabled. If we can fully verify the locking we can ignore
jeffhaobdb76512011-09-07 11:43:16 -07002171 * some catch blocks (which will show up as "dead" code when
2172 * we skip them here); if we can't, then the code path could be
2173 * "live" so we still need to check it.
2174 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002175 opcode_flags &= ~Instruction::kThrow;
Ian Rogers7b078e82014-09-10 14:44:24 -07002176 work_line_->PopMonitor(this, inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07002177 break;
Ian Rogers28ad40d2011-10-27 15:19:26 -07002178 case Instruction::CHECK_CAST:
Ian Rogersd81871c2011-10-03 13:57:23 -07002179 case Instruction::INSTANCE_OF: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002180 /*
2181 * If this instruction succeeds, we will "downcast" register vA to the type in vB. (This
2182 * could be a "upcast" -- not expected, so we don't try to address it.)
2183 *
2184 * If it fails, an exception is thrown, which we deal with later by ignoring the update to
Elliott Hughesadb8c672012-03-06 16:49:32 -08002185 * dec_insn.vA when branching to a handler.
Ian Rogers28ad40d2011-10-27 15:19:26 -07002186 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002187 const bool is_checkcast = (inst->Opcode() == Instruction::CHECK_CAST);
Andreas Gampea5b09a62016-11-17 15:21:22 -08002188 const dex::TypeIndex type_idx((is_checkcast) ? inst->VRegB_21c() : inst->VRegC_22c());
Andreas Gampe98be1a92017-08-28 08:25:45 -07002189 const RegType& res_type = ResolveClass<CheckAccess::kYes>(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002190 if (res_type.IsConflict()) {
Andreas Gampe00633eb2014-07-17 16:13:35 -07002191 // If this is a primitive type, fail HARD.
Vladimir Marko666ee3d2017-12-11 18:37:36 +00002192 ObjPtr<mirror::Class> klass = Runtime::Current()->GetClassLinker()->LookupResolvedType(
2193 type_idx, dex_cache_.Get(), class_loader_.Get());
Andreas Gampe00633eb2014-07-17 16:13:35 -07002194 if (klass != nullptr && klass->IsPrimitive()) {
2195 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "using primitive type "
2196 << dex_file_->StringByTypeIdx(type_idx) << " in instanceof in "
2197 << GetDeclaringClass();
2198 break;
2199 }
2200
Ian Rogersad0b3a32012-04-16 14:50:24 -07002201 DCHECK_NE(failures_.size(), 0U);
2202 if (!is_checkcast) {
Andreas Gampead238ce2015-08-24 21:13:08 -07002203 work_line_->SetRegisterType<LockOp::kClear>(this,
2204 inst->VRegA_22c(),
2205 reg_types_.Boolean());
Ian Rogersad0b3a32012-04-16 14:50:24 -07002206 }
2207 break; // bad class
Ian Rogers9f1ab122011-12-12 08:52:43 -08002208 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002209 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Sebastien Hertz5243e912013-05-21 10:55:07 +02002210 uint32_t orig_type_reg = (is_checkcast) ? inst->VRegA_21c() : inst->VRegB_22c();
Ian Rogers7b078e82014-09-10 14:44:24 -07002211 const RegType& orig_type = work_line_->GetRegisterType(this, orig_type_reg);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002212 if (!res_type.IsNonZeroReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002213 if (is_checkcast) {
2214 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on unexpected class " << res_type;
2215 } else {
2216 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on unexpected class " << res_type;
2217 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002218 } else if (!orig_type.IsReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002219 if (is_checkcast) {
2220 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on non-reference in v" << orig_type_reg;
2221 } else {
2222 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on non-reference in v" << orig_type_reg;
2223 }
David Brazdil68b5c0b2016-01-19 14:25:29 +00002224 } else if (orig_type.IsUninitializedTypes()) {
2225 if (is_checkcast) {
2226 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on uninitialized reference in v"
2227 << orig_type_reg;
2228 } else {
2229 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on uninitialized reference in v"
2230 << orig_type_reg;
2231 }
jeffhao2a8a90e2011-09-26 14:25:31 -07002232 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002233 if (is_checkcast) {
Andreas Gampead238ce2015-08-24 21:13:08 -07002234 work_line_->SetRegisterType<LockOp::kKeep>(this, inst->VRegA_21c(), res_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07002235 } else {
Andreas Gampead238ce2015-08-24 21:13:08 -07002236 work_line_->SetRegisterType<LockOp::kClear>(this,
2237 inst->VRegA_22c(),
2238 reg_types_.Boolean());
jeffhaobdb76512011-09-07 11:43:16 -07002239 }
jeffhaobdb76512011-09-07 11:43:16 -07002240 }
jeffhao2a8a90e2011-09-26 14:25:31 -07002241 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002242 }
2243 case Instruction::ARRAY_LENGTH: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002244 const RegType& res_type = work_line_->GetRegisterType(this, inst->VRegB_12x());
Ian Rogers28ad40d2011-10-27 15:19:26 -07002245 if (res_type.IsReferenceTypes()) {
Andreas Gampeeed3a5d2017-11-29 14:58:34 -08002246 if (!res_type.IsArrayTypes() && !res_type.IsZeroOrNull()) {
2247 // ie not an array or null
jeffhaod5347e02012-03-22 17:25:05 -07002248 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002249 } else {
Andreas Gampead238ce2015-08-24 21:13:08 -07002250 work_line_->SetRegisterType<LockOp::kClear>(this,
2251 inst->VRegA_12x(),
2252 reg_types_.Integer());
Ian Rogersd81871c2011-10-03 13:57:23 -07002253 }
Andreas Gampe65c9db82014-07-28 13:14:34 -07002254 } else {
2255 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002256 }
2257 break;
2258 }
2259 case Instruction::NEW_INSTANCE: {
Andreas Gampe98be1a92017-08-28 08:25:45 -07002260 const RegType& res_type = ResolveClass<CheckAccess::kYes>(dex::TypeIndex(inst->VRegB_21c()));
Ian Rogersad0b3a32012-04-16 14:50:24 -07002261 if (res_type.IsConflict()) {
2262 DCHECK_NE(failures_.size(), 0U);
2263 break; // bad class
jeffhao8cd6dda2012-02-22 10:15:34 -08002264 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002265 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
2266 // can't create an instance of an interface or abstract class */
2267 if (!res_type.IsInstantiableTypes()) {
2268 Fail(VERIFY_ERROR_INSTANTIATION)
2269 << "new-instance on primitive, interface or abstract class" << res_type;
Ian Rogers08f753d2012-08-24 14:35:25 -07002270 // Soft failure so carry on to set register type.
Ian Rogersd81871c2011-10-03 13:57:23 -07002271 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00002272 const RegType& uninit_type = reg_types_.Uninitialized(res_type, work_insn_idx_);
Ian Rogers08f753d2012-08-24 14:35:25 -07002273 // Any registers holding previous allocations from this address that have not yet been
2274 // initialized must be marked invalid.
Ian Rogers7b078e82014-09-10 14:44:24 -07002275 work_line_->MarkUninitRefsAsInvalid(this, uninit_type);
Ian Rogers08f753d2012-08-24 14:35:25 -07002276 // add the new uninitialized reference to the register state
Andreas Gampead238ce2015-08-24 21:13:08 -07002277 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_21c(), uninit_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07002278 break;
2279 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08002280 case Instruction::NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002281 VerifyNewArray(inst, false, false);
jeffhaobdb76512011-09-07 11:43:16 -07002282 break;
2283 case Instruction::FILLED_NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002284 VerifyNewArray(inst, true, false);
Ian Rogers0c4a5062012-02-03 15:18:59 -08002285 just_set_result = true; // Filled new array sets result register
jeffhaobdb76512011-09-07 11:43:16 -07002286 break;
Ian Rogers0c4a5062012-02-03 15:18:59 -08002287 case Instruction::FILLED_NEW_ARRAY_RANGE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002288 VerifyNewArray(inst, true, true);
Ian Rogers0c4a5062012-02-03 15:18:59 -08002289 just_set_result = true; // Filled new array range sets result register
2290 break;
jeffhaobdb76512011-09-07 11:43:16 -07002291 case Instruction::CMPL_FLOAT:
2292 case Instruction::CMPG_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002293 if (!work_line_->VerifyRegisterType(this, inst->VRegB_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08002294 break;
2295 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002296 if (!work_line_->VerifyRegisterType(this, inst->VRegC_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08002297 break;
2298 }
Andreas Gampead238ce2015-08-24 21:13:08 -07002299 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002300 break;
2301 case Instruction::CMPL_DOUBLE:
2302 case Instruction::CMPG_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002303 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegB_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002304 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002305 break;
2306 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002307 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegC_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002308 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002309 break;
2310 }
Andreas Gampead238ce2015-08-24 21:13:08 -07002311 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002312 break;
2313 case Instruction::CMP_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002314 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegB_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002315 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002316 break;
2317 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002318 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegC_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002319 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002320 break;
2321 }
Andreas Gampead238ce2015-08-24 21:13:08 -07002322 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002323 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002324 case Instruction::THROW: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002325 const RegType& res_type = work_line_->GetRegisterType(this, inst->VRegA_11x());
David Brazdilca3c8c32016-09-06 14:04:48 +01002326 if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(res_type, this)) {
David Brazdil68b5c0b2016-01-19 14:25:29 +00002327 if (res_type.IsUninitializedTypes()) {
2328 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "thrown exception not initialized";
Pavel Vyssotski980027c2016-02-11 20:28:11 +06002329 } else if (!res_type.IsReferenceTypes()) {
2330 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "thrown value of non-reference type " << res_type;
David Brazdil68b5c0b2016-01-19 14:25:29 +00002331 } else {
2332 Fail(res_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS : VERIFY_ERROR_BAD_CLASS_SOFT)
2333 << "thrown class " << res_type << " not instanceof Throwable";
2334 }
jeffhaobdb76512011-09-07 11:43:16 -07002335 }
2336 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002337 }
jeffhaobdb76512011-09-07 11:43:16 -07002338 case Instruction::GOTO:
2339 case Instruction::GOTO_16:
2340 case Instruction::GOTO_32:
2341 /* no effect on or use of registers */
2342 break;
2343
2344 case Instruction::PACKED_SWITCH:
2345 case Instruction::SPARSE_SWITCH:
2346 /* verify that vAA is an integer, or can be converted to one */
Ian Rogers7b078e82014-09-10 14:44:24 -07002347 work_line_->VerifyRegisterType(this, inst->VRegA_31t(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002348 break;
2349
Ian Rogersd81871c2011-10-03 13:57:23 -07002350 case Instruction::FILL_ARRAY_DATA: {
2351 /* Similar to the verification done for APUT */
Ian Rogers7b078e82014-09-10 14:44:24 -07002352 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegA_31t());
Ian Rogers89310de2012-02-01 13:47:30 -08002353 /* array_type can be null if the reg type is Zero */
Andreas Gampeeed3a5d2017-11-29 14:58:34 -08002354 if (!array_type.IsZeroOrNull()) {
jeffhao457cc512012-02-02 16:55:13 -08002355 if (!array_type.IsArrayTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002356 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with array type "
2357 << array_type;
Andreas Gampebb18a032016-03-22 20:34:25 -07002358 } else if (array_type.IsUnresolvedTypes()) {
2359 // If it's an unresolved array type, it must be non-primitive.
2360 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data for array of type "
2361 << array_type;
Ian Rogers89310de2012-02-01 13:47:30 -08002362 } else {
Andreas Gampe51de69e2019-04-19 15:14:14 -07002363 const RegType& component_type = reg_types_.GetComponentType(array_type,
2364 class_loader_.Get());
Ian Rogersad0b3a32012-04-16 14:50:24 -07002365 DCHECK(!component_type.IsConflict());
jeffhao457cc512012-02-02 16:55:13 -08002366 if (component_type.IsNonZeroReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002367 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with component type "
2368 << component_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002369 } else {
jeffhao457cc512012-02-02 16:55:13 -08002370 // Now verify if the element width in the table matches the element width declared in
2371 // the array
Andreas Gampe53de99c2015-08-17 13:43:55 -07002372 const uint16_t* array_data =
2373 insns + (insns[1] | (static_cast<int32_t>(insns[2]) << 16));
jeffhao457cc512012-02-02 16:55:13 -08002374 if (array_data[0] != Instruction::kArrayDataSignature) {
jeffhaod5347e02012-03-22 17:25:05 -07002375 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid magic for array-data";
jeffhao457cc512012-02-02 16:55:13 -08002376 } else {
2377 size_t elem_width = Primitive::ComponentSize(component_type.GetPrimitiveType());
2378 // Since we don't compress the data in Dex, expect to see equal width of data stored
2379 // in the table and expected from the array class.
2380 if (array_data[1] != elem_width) {
jeffhaod5347e02012-03-22 17:25:05 -07002381 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-data size mismatch (" << array_data[1]
2382 << " vs " << elem_width << ")";
jeffhao457cc512012-02-02 16:55:13 -08002383 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002384 }
2385 }
jeffhaobdb76512011-09-07 11:43:16 -07002386 }
2387 }
2388 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002389 }
jeffhaobdb76512011-09-07 11:43:16 -07002390 case Instruction::IF_EQ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002391 case Instruction::IF_NE: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002392 const RegType& reg_type1 = work_line_->GetRegisterType(this, inst->VRegA_22t());
2393 const RegType& reg_type2 = work_line_->GetRegisterType(this, inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002394 bool mismatch = false;
Andreas Gampeeed3a5d2017-11-29 14:58:34 -08002395 if (reg_type1.IsZeroOrNull()) { // zero then integral or reference expected
Ian Rogersd81871c2011-10-03 13:57:23 -07002396 mismatch = !reg_type2.IsReferenceTypes() && !reg_type2.IsIntegralTypes();
2397 } else if (reg_type1.IsReferenceTypes()) { // both references?
2398 mismatch = !reg_type2.IsReferenceTypes();
2399 } else { // both integral?
2400 mismatch = !reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes();
2401 }
2402 if (mismatch) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002403 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to if-eq/if-ne (" << reg_type1 << ","
2404 << reg_type2 << ") must both be references or integral";
jeffhaobdb76512011-09-07 11:43:16 -07002405 }
2406 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002407 }
jeffhaobdb76512011-09-07 11:43:16 -07002408 case Instruction::IF_LT:
2409 case Instruction::IF_GE:
2410 case Instruction::IF_GT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002411 case Instruction::IF_LE: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002412 const RegType& reg_type1 = work_line_->GetRegisterType(this, inst->VRegA_22t());
2413 const RegType& reg_type2 = work_line_->GetRegisterType(this, inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002414 if (!reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002415 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to 'if' (" << reg_type1 << ","
2416 << reg_type2 << ") must be integral";
jeffhaobdb76512011-09-07 11:43:16 -07002417 }
2418 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002419 }
jeffhaobdb76512011-09-07 11:43:16 -07002420 case Instruction::IF_EQZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002421 case Instruction::IF_NEZ: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002422 const RegType& reg_type = work_line_->GetRegisterType(this, inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002423 if (!reg_type.IsReferenceTypes() && !reg_type.IsIntegralTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002424 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
2425 << " unexpected as arg to if-eqz/if-nez";
Ian Rogersd81871c2011-10-03 13:57:23 -07002426 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002427
2428 // Find previous instruction - its existence is a precondition to peephole optimization.
Ian Rogers9b360392013-06-06 14:45:07 -07002429 uint32_t instance_of_idx = 0;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002430 if (0 != work_insn_idx_) {
Ian Rogers9b360392013-06-06 14:45:07 -07002431 instance_of_idx = work_insn_idx_ - 1;
Mathieu Chartierde40d472015-10-15 17:47:48 -07002432 while (0 != instance_of_idx && !GetInstructionFlags(instance_of_idx).IsOpcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07002433 instance_of_idx--;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002434 }
Mathieu Chartierc2a2bda2018-03-12 11:21:52 -07002435 if (FailOrAbort(GetInstructionFlags(instance_of_idx).IsOpcode(),
Andreas Gampe7c038102014-10-27 20:08:46 -07002436 "Unable to get previous instruction of if-eqz/if-nez for work index ",
2437 work_insn_idx_)) {
2438 break;
2439 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002440 } else {
2441 break;
2442 }
2443
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08002444 const Instruction& instance_of_inst = code_item_accessor_.InstructionAt(instance_of_idx);
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002445
2446 /* Check for peep-hole pattern of:
2447 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002448 * instance-of vX, vY, T;
2449 * ifXXX vX, label ;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002450 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002451 * label:
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002452 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002453 * and sharpen the type of vY to be type T.
2454 * Note, this pattern can't be if:
2455 * - if there are other branches to this branch,
2456 * - when vX == vY.
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002457 */
Ian Rogersfae370a2013-06-05 08:33:27 -07002458 if (!CurrentInsnFlags()->IsBranchTarget() &&
Vladimir Markod7559b72017-09-28 13:50:37 +01002459 (Instruction::INSTANCE_OF == instance_of_inst.Opcode()) &&
2460 (inst->VRegA_21t() == instance_of_inst.VRegA_22c()) &&
2461 (instance_of_inst.VRegA_22c() != instance_of_inst.VRegB_22c())) {
Ian Rogersebbdd872014-07-07 23:53:08 -07002462 // Check the type of the instance-of is different than that of registers type, as if they
2463 // are the same there is no work to be done here. Check that the conversion is not to or
2464 // from an unresolved type as type information is imprecise. If the instance-of is to an
2465 // interface then ignore the type information as interfaces can only be treated as Objects
2466 // and we don't want to disallow field and other operations on the object. If the value
2467 // being instance-of checked against is known null (zero) then allow the optimization as
2468 // we didn't have type information. If the merge of the instance-of type with the original
2469 // type is assignable to the original then allow optimization. This check is performed to
2470 // ensure that subsequent merges don't lose type information - such as becoming an
2471 // interface from a class that would lose information relevant to field checks.
Vladimir Markod7559b72017-09-28 13:50:37 +01002472 const RegType& orig_type = work_line_->GetRegisterType(this, instance_of_inst.VRegB_22c());
Andreas Gampe98be1a92017-08-28 08:25:45 -07002473 const RegType& cast_type = ResolveClass<CheckAccess::kYes>(
Vladimir Markod7559b72017-09-28 13:50:37 +01002474 dex::TypeIndex(instance_of_inst.VRegC_22c()));
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002475
Ian Rogersebbdd872014-07-07 23:53:08 -07002476 if (!orig_type.Equals(cast_type) &&
2477 !cast_type.IsUnresolvedTypes() && !orig_type.IsUnresolvedTypes() &&
Andreas Gampe00633eb2014-07-17 16:13:35 -07002478 cast_type.HasClass() && // Could be conflict type, make sure it has a class.
Ian Rogersebbdd872014-07-07 23:53:08 -07002479 !cast_type.GetClass()->IsInterface() &&
Andreas Gampeeed3a5d2017-11-29 14:58:34 -08002480 (orig_type.IsZeroOrNull() ||
David Brazdilca3c8c32016-09-06 14:04:48 +01002481 orig_type.IsStrictlyAssignableFrom(
2482 cast_type.Merge(orig_type, &reg_types_, this), this))) {
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08002483 RegisterLine* update_line = RegisterLine::Create(code_item_accessor_.RegistersSize(),
2484 this);
Ian Rogersfae370a2013-06-05 08:33:27 -07002485 if (inst->Opcode() == Instruction::IF_EQZ) {
Ian Rogers9b360392013-06-06 14:45:07 -07002486 fallthrough_line.reset(update_line);
Ian Rogersfae370a2013-06-05 08:33:27 -07002487 } else {
Ian Rogers9b360392013-06-06 14:45:07 -07002488 branch_line.reset(update_line);
2489 }
2490 update_line->CopyFromLine(work_line_.get());
Andreas Gampead238ce2015-08-24 21:13:08 -07002491 update_line->SetRegisterType<LockOp::kKeep>(this,
Vladimir Markod7559b72017-09-28 13:50:37 +01002492 instance_of_inst.VRegB_22c(),
Andreas Gampead238ce2015-08-24 21:13:08 -07002493 cast_type);
Mathieu Chartierde40d472015-10-15 17:47:48 -07002494 if (!GetInstructionFlags(instance_of_idx).IsBranchTarget() && 0 != instance_of_idx) {
Ian Rogers9b360392013-06-06 14:45:07 -07002495 // See if instance-of was preceded by a move-object operation, common due to the small
2496 // register encoding space of instance-of, and propagate type information to the source
2497 // of the move-object.
Andreas Gampe43e43252019-01-08 12:06:57 -08002498 // Note: this is only valid if the move source was not clobbered.
Ian Rogers9b360392013-06-06 14:45:07 -07002499 uint32_t move_idx = instance_of_idx - 1;
Mathieu Chartierde40d472015-10-15 17:47:48 -07002500 while (0 != move_idx && !GetInstructionFlags(move_idx).IsOpcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07002501 move_idx--;
2502 }
Mathieu Chartierc2a2bda2018-03-12 11:21:52 -07002503 if (FailOrAbort(GetInstructionFlags(move_idx).IsOpcode(),
Andreas Gampe7c038102014-10-27 20:08:46 -07002504 "Unable to get previous instruction of if-eqz/if-nez for work index ",
2505 work_insn_idx_)) {
2506 break;
2507 }
Andreas Gampe43e43252019-01-08 12:06:57 -08002508 auto maybe_update_fn = [&instance_of_inst, update_line, this, &cast_type](
2509 uint16_t move_src,
2510 uint16_t move_trg)
2511 REQUIRES_SHARED(Locks::mutator_lock_) {
2512 if (move_trg == instance_of_inst.VRegB_22c() &&
2513 move_src != instance_of_inst.VRegA_22c()) {
2514 update_line->SetRegisterType<LockOp::kKeep>(this, move_src, cast_type);
2515 }
2516 };
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08002517 const Instruction& move_inst = code_item_accessor_.InstructionAt(move_idx);
Vladimir Markod7559b72017-09-28 13:50:37 +01002518 switch (move_inst.Opcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07002519 case Instruction::MOVE_OBJECT:
Andreas Gampe43e43252019-01-08 12:06:57 -08002520 maybe_update_fn(move_inst.VRegB_12x(), move_inst.VRegA_12x());
Ian Rogers9b360392013-06-06 14:45:07 -07002521 break;
2522 case Instruction::MOVE_OBJECT_FROM16:
Andreas Gampe43e43252019-01-08 12:06:57 -08002523 maybe_update_fn(move_inst.VRegB_22x(), move_inst.VRegA_22x());
Ian Rogers9b360392013-06-06 14:45:07 -07002524 break;
2525 case Instruction::MOVE_OBJECT_16:
Andreas Gampe43e43252019-01-08 12:06:57 -08002526 maybe_update_fn(move_inst.VRegB_32x(), move_inst.VRegA_32x());
Ian Rogers9b360392013-06-06 14:45:07 -07002527 break;
2528 default:
2529 break;
2530 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002531 }
2532 }
2533 }
2534
jeffhaobdb76512011-09-07 11:43:16 -07002535 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002536 }
jeffhaobdb76512011-09-07 11:43:16 -07002537 case Instruction::IF_LTZ:
2538 case Instruction::IF_GEZ:
2539 case Instruction::IF_GTZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002540 case Instruction::IF_LEZ: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002541 const RegType& reg_type = work_line_->GetRegisterType(this, inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002542 if (!reg_type.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002543 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
2544 << " unexpected as arg to if-ltz/if-gez/if-gtz/if-lez";
Ian Rogersd81871c2011-10-03 13:57:23 -07002545 }
jeffhaobdb76512011-09-07 11:43:16 -07002546 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002547 }
jeffhaobdb76512011-09-07 11:43:16 -07002548 case Instruction::AGET_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002549 VerifyAGet(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002550 break;
jeffhaobdb76512011-09-07 11:43:16 -07002551 case Instruction::AGET_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002552 VerifyAGet(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002553 break;
jeffhaobdb76512011-09-07 11:43:16 -07002554 case Instruction::AGET_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002555 VerifyAGet(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002556 break;
jeffhaobdb76512011-09-07 11:43:16 -07002557 case Instruction::AGET_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002558 VerifyAGet(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002559 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002560 case Instruction::AGET:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002561 VerifyAGet(inst, reg_types_.Integer(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002562 break;
jeffhaobdb76512011-09-07 11:43:16 -07002563 case Instruction::AGET_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002564 VerifyAGet(inst, reg_types_.LongLo(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002565 break;
2566 case Instruction::AGET_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002567 VerifyAGet(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07002568 break;
2569
Ian Rogersd81871c2011-10-03 13:57:23 -07002570 case Instruction::APUT_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002571 VerifyAPut(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002572 break;
2573 case Instruction::APUT_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002574 VerifyAPut(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002575 break;
2576 case Instruction::APUT_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002577 VerifyAPut(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002578 break;
2579 case Instruction::APUT_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002580 VerifyAPut(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002581 break;
2582 case Instruction::APUT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002583 VerifyAPut(inst, reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002584 break;
2585 case Instruction::APUT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002586 VerifyAPut(inst, reg_types_.LongLo(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002587 break;
2588 case Instruction::APUT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002589 VerifyAPut(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07002590 break;
2591
jeffhaobdb76512011-09-07 11:43:16 -07002592 case Instruction::IGET_BOOLEAN:
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00002593 case Instruction::IGET_BOOLEAN_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002594 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002595 break;
jeffhaobdb76512011-09-07 11:43:16 -07002596 case Instruction::IGET_BYTE:
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00002597 case Instruction::IGET_BYTE_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002598 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002599 break;
jeffhaobdb76512011-09-07 11:43:16 -07002600 case Instruction::IGET_CHAR:
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00002601 case Instruction::IGET_CHAR_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002602 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002603 break;
jeffhaobdb76512011-09-07 11:43:16 -07002604 case Instruction::IGET_SHORT:
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00002605 case Instruction::IGET_SHORT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002606 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002607 break;
2608 case Instruction::IGET:
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00002609 case Instruction::IGET_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002610 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002611 break;
2612 case Instruction::IGET_WIDE:
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00002613 case Instruction::IGET_WIDE_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002614 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002615 break;
2616 case Instruction::IGET_OBJECT:
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00002617 case Instruction::IGET_OBJECT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002618 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false,
2619 false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002620 break;
jeffhaobdb76512011-09-07 11:43:16 -07002621
Ian Rogersd81871c2011-10-03 13:57:23 -07002622 case Instruction::IPUT_BOOLEAN:
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00002623 case Instruction::IPUT_BOOLEAN_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002624 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002625 break;
2626 case Instruction::IPUT_BYTE:
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00002627 case Instruction::IPUT_BYTE_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002628 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002629 break;
2630 case Instruction::IPUT_CHAR:
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00002631 case Instruction::IPUT_CHAR_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002632 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002633 break;
2634 case Instruction::IPUT_SHORT:
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00002635 case Instruction::IPUT_SHORT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002636 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002637 break;
2638 case Instruction::IPUT:
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00002639 case Instruction::IPUT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002640 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002641 break;
2642 case Instruction::IPUT_WIDE:
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00002643 case Instruction::IPUT_WIDE_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002644 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002645 break;
jeffhaobdb76512011-09-07 11:43:16 -07002646 case Instruction::IPUT_OBJECT:
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00002647 case Instruction::IPUT_OBJECT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002648 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false,
2649 false);
jeffhaobdb76512011-09-07 11:43:16 -07002650 break;
2651
jeffhaobdb76512011-09-07 11:43:16 -07002652 case Instruction::SGET_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002653 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002654 break;
jeffhaobdb76512011-09-07 11:43:16 -07002655 case Instruction::SGET_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002656 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002657 break;
jeffhaobdb76512011-09-07 11:43:16 -07002658 case Instruction::SGET_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002659 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002660 break;
jeffhaobdb76512011-09-07 11:43:16 -07002661 case Instruction::SGET_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002662 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002663 break;
2664 case Instruction::SGET:
Andreas Gampe896df402014-10-20 22:25:29 -07002665 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002666 break;
2667 case Instruction::SGET_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002668 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002669 break;
2670 case Instruction::SGET_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002671 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false,
2672 true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002673 break;
2674
2675 case Instruction::SPUT_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002676 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002677 break;
2678 case Instruction::SPUT_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002679 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002680 break;
2681 case Instruction::SPUT_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002682 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002683 break;
2684 case Instruction::SPUT_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002685 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002686 break;
2687 case Instruction::SPUT:
Andreas Gampe896df402014-10-20 22:25:29 -07002688 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002689 break;
2690 case Instruction::SPUT_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002691 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002692 break;
2693 case Instruction::SPUT_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002694 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false,
2695 true);
jeffhaobdb76512011-09-07 11:43:16 -07002696 break;
2697
2698 case Instruction::INVOKE_VIRTUAL:
2699 case Instruction::INVOKE_VIRTUAL_RANGE:
2700 case Instruction::INVOKE_SUPER:
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00002701 case Instruction::INVOKE_SUPER_RANGE:
2702 case Instruction::INVOKE_VIRTUAL_QUICK:
2703 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002704 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE ||
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00002705 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE ||
2706 inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002707 bool is_super = (inst->Opcode() == Instruction::INVOKE_SUPER ||
2708 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
Alex Light7268d472016-01-20 15:50:01 -08002709 MethodType type = is_super ? METHOD_SUPER : METHOD_VIRTUAL;
2710 ArtMethod* called_method = VerifyInvocationArgs(inst, type, is_range);
Ian Rogersd8f69b02014-09-10 21:43:52 +00002711 const RegType* return_type = nullptr;
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002712 if (called_method != nullptr) {
Vladimir Markob45528c2017-07-27 14:14:28 +01002713 ObjPtr<mirror::Class> return_type_class = can_load_classes_
2714 ? called_method->ResolveReturnType()
2715 : called_method->LookupResolvedReturnType();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002716 if (return_type_class != nullptr) {
Andreas Gampef23f33d2015-06-23 14:18:17 -07002717 return_type = &FromClass(called_method->GetReturnTypeDescriptor(),
Vladimir Marko179b7c62019-03-22 13:38:57 +00002718 return_type_class,
Andreas Gampef23f33d2015-06-23 14:18:17 -07002719 return_type_class->CannotBeAssignedFromOtherTypes());
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002720 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07002721 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
2722 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002723 }
2724 }
2725 if (return_type == nullptr) {
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00002726 uint32_t method_idx = GetMethodIdxOfInvoke(inst);
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08002727 const dex::MethodId& method_id = dex_file_->GetMethodId(method_idx);
Andreas Gampea5b09a62016-11-17 15:21:22 -08002728 dex::TypeIndex return_type_idx =
2729 dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002730 const char* descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Andreas Gampe51de69e2019-04-19 15:14:14 -07002731 return_type = &reg_types_.FromDescriptor(class_loader_.Get(), descriptor, false);
jeffhaobdb76512011-09-07 11:43:16 -07002732 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002733 if (!return_type->IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002734 work_line_->SetResultRegisterType(this, *return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002735 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002736 work_line_->SetResultRegisterTypeWide(*return_type, return_type->HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002737 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002738 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002739 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002740 }
jeffhaobdb76512011-09-07 11:43:16 -07002741 case Instruction::INVOKE_DIRECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002742 case Instruction::INVOKE_DIRECT_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002743 bool is_range = (inst->Opcode() == Instruction::INVOKE_DIRECT_RANGE);
Alex Light7268d472016-01-20 15:50:01 -08002744 ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_DIRECT, is_range);
Ian Rogers46685432012-06-03 22:26:43 -07002745 const char* return_type_descriptor;
2746 bool is_constructor;
Ian Rogersd8f69b02014-09-10 21:43:52 +00002747 const RegType* return_type = nullptr;
Ian Rogers7b078e82014-09-10 14:44:24 -07002748 if (called_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002749 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08002750 const dex::MethodId& method_id = dex_file_->GetMethodId(method_idx);
Ian Rogersdfb325e2013-10-30 01:00:44 -07002751 is_constructor = strcmp("<init>", dex_file_->StringDataByIdx(method_id.name_idx_)) == 0;
Andreas Gampea5b09a62016-11-17 15:21:22 -08002752 dex::TypeIndex return_type_idx =
2753 dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogers46685432012-06-03 22:26:43 -07002754 return_type_descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2755 } else {
2756 is_constructor = called_method->IsConstructor();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002757 return_type_descriptor = called_method->GetReturnTypeDescriptor();
Vladimir Markob45528c2017-07-27 14:14:28 +01002758 ObjPtr<mirror::Class> return_type_class = can_load_classes_
2759 ? called_method->ResolveReturnType()
2760 : called_method->LookupResolvedReturnType();
Ian Rogers1ff3c982014-08-12 02:30:58 -07002761 if (return_type_class != nullptr) {
Andreas Gampef23f33d2015-06-23 14:18:17 -07002762 return_type = &FromClass(return_type_descriptor,
Vladimir Markobcf17522018-06-01 13:14:32 +01002763 return_type_class,
Andreas Gampef23f33d2015-06-23 14:18:17 -07002764 return_type_class->CannotBeAssignedFromOtherTypes());
Ian Rogers1ff3c982014-08-12 02:30:58 -07002765 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07002766 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
2767 self_->ClearException();
Ian Rogers1ff3c982014-08-12 02:30:58 -07002768 }
Ian Rogers46685432012-06-03 22:26:43 -07002769 }
2770 if (is_constructor) {
jeffhaobdb76512011-09-07 11:43:16 -07002771 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002772 * Some additional checks when calling a constructor. We know from the invocation arg check
2773 * that the "this" argument is an instance of called_method->klass. Now we further restrict
2774 * that to require that called_method->klass is the same as this->klass or this->super,
2775 * allowing the latter only if the "this" argument is the same as the "this" argument to
2776 * this method (which implies that we're in a constructor ourselves).
jeffhaobdb76512011-09-07 11:43:16 -07002777 */
Orion Hodsoncfa325e2016-10-13 10:25:54 +01002778 const RegType& this_type = work_line_->GetInvocationThis(this, inst);
jeffhaob57e9522012-04-26 18:08:21 -07002779 if (this_type.IsConflict()) // failure.
2780 break;
jeffhaobdb76512011-09-07 11:43:16 -07002781
jeffhaob57e9522012-04-26 18:08:21 -07002782 /* no null refs allowed (?) */
Andreas Gampeeed3a5d2017-11-29 14:58:34 -08002783 if (this_type.IsZeroOrNull()) {
jeffhaob57e9522012-04-26 18:08:21 -07002784 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unable to initialize null ref";
2785 break;
jeffhao2a8a90e2011-09-26 14:25:31 -07002786 }
jeffhaob57e9522012-04-26 18:08:21 -07002787
2788 /* must be in same class or in superclass */
Ian Rogersd8f69b02014-09-10 21:43:52 +00002789 // const RegType& this_super_klass = this_type.GetSuperClass(&reg_types_);
Ian Rogers46685432012-06-03 22:26:43 -07002790 // TODO: re-enable constructor type verification
2791 // if (this_super_klass.IsConflict()) {
jeffhaob57e9522012-04-26 18:08:21 -07002792 // Unknown super class, fail so we re-check at runtime.
Ian Rogers46685432012-06-03 22:26:43 -07002793 // Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "super class unknown for '" << this_type << "'";
2794 // break;
2795 // }
jeffhaob57e9522012-04-26 18:08:21 -07002796
2797 /* arg must be an uninitialized reference */
2798 if (!this_type.IsUninitializedTypes()) {
2799 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Expected initialization on uninitialized reference "
2800 << this_type;
2801 break;
2802 }
2803
2804 /*
2805 * Replace the uninitialized reference with an initialized one. We need to do this for all
2806 * registers that have the same object instance in them, not just the "this" register.
2807 */
Nicolas Geoffray98e6ce42016-02-16 18:42:15 +00002808 work_line_->MarkRefsAsInitialized(this, this_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002809 }
Ian Rogers1ff3c982014-08-12 02:30:58 -07002810 if (return_type == nullptr) {
Andreas Gampe51de69e2019-04-19 15:14:14 -07002811 return_type = &reg_types_.FromDescriptor(class_loader_.Get(),
2812 return_type_descriptor,
2813 false);
Ian Rogers1ff3c982014-08-12 02:30:58 -07002814 }
2815 if (!return_type->IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002816 work_line_->SetResultRegisterType(this, *return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002817 } else {
Ian Rogers1ff3c982014-08-12 02:30:58 -07002818 work_line_->SetResultRegisterTypeWide(*return_type, return_type->HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002819 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002820 just_set_result = true;
2821 break;
2822 }
2823 case Instruction::INVOKE_STATIC:
2824 case Instruction::INVOKE_STATIC_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002825 bool is_range = (inst->Opcode() == Instruction::INVOKE_STATIC_RANGE);
Alex Light7268d472016-01-20 15:50:01 -08002826 ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_STATIC, is_range);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002827 const char* descriptor;
Ian Rogers7b078e82014-09-10 14:44:24 -07002828 if (called_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002829 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08002830 const dex::MethodId& method_id = dex_file_->GetMethodId(method_idx);
Andreas Gampea5b09a62016-11-17 15:21:22 -08002831 dex::TypeIndex return_type_idx =
2832 dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Mathieu Chartier590fee92013-09-13 13:46:47 -07002833 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002834 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002835 descriptor = called_method->GetReturnTypeDescriptor();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002836 }
Andreas Gampe51de69e2019-04-19 15:14:14 -07002837 const RegType& return_type = reg_types_.FromDescriptor(class_loader_.Get(),
2838 descriptor,
2839 false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002840 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002841 work_line_->SetResultRegisterType(this, return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002842 } else {
2843 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2844 }
jeffhaobdb76512011-09-07 11:43:16 -07002845 just_set_result = true;
2846 }
2847 break;
jeffhaobdb76512011-09-07 11:43:16 -07002848 case Instruction::INVOKE_INTERFACE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002849 case Instruction::INVOKE_INTERFACE_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002850 bool is_range = (inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE);
Alex Light7268d472016-01-20 15:50:01 -08002851 ArtMethod* abs_method = VerifyInvocationArgs(inst, METHOD_INTERFACE, is_range);
Ian Rogers7b078e82014-09-10 14:44:24 -07002852 if (abs_method != nullptr) {
Vladimir Markod93e3742018-07-18 10:58:13 +01002853 ObjPtr<mirror::Class> called_interface = abs_method->GetDeclaringClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002854 if (!called_interface->IsInterface() && !called_interface->IsObjectClass()) {
2855 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected interface class in invoke-interface '"
David Sehr709b0702016-10-13 09:12:37 -07002856 << abs_method->PrettyMethod() << "'";
Ian Rogersad0b3a32012-04-16 14:50:24 -07002857 break;
Ian Rogers28ad40d2011-10-27 15:19:26 -07002858 }
Ian Rogers0d604842012-04-16 14:50:24 -07002859 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002860 /* Get the type of the "this" arg, which should either be a sub-interface of called
2861 * interface or Object (see comments in RegType::JoinClass).
2862 */
Orion Hodsoncfa325e2016-10-13 10:25:54 +01002863 const RegType& this_type = work_line_->GetInvocationThis(this, inst);
Andreas Gampeeed3a5d2017-11-29 14:58:34 -08002864 if (this_type.IsZeroOrNull()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002865 /* null pointer always passes (and always fails at runtime) */
2866 } else {
2867 if (this_type.IsUninitializedTypes()) {
2868 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface call on uninitialized object "
2869 << this_type;
2870 break;
2871 }
2872 // In the past we have tried to assert that "called_interface" is assignable
2873 // from "this_type.GetClass()", however, as we do an imprecise Join
2874 // (RegType::JoinClass) we don't have full information on what interfaces are
2875 // implemented by "this_type". For example, two classes may implement the same
2876 // interfaces and have a common parent that doesn't implement the interface. The
2877 // join will set "this_type" to the parent class and a test that this implements
2878 // the interface will incorrectly fail.
2879 }
2880 /*
2881 * We don't have an object instance, so we can't find the concrete method. However, all of
2882 * the type information is in the abstract method, so we're good.
2883 */
2884 const char* descriptor;
Ian Rogers7b078e82014-09-10 14:44:24 -07002885 if (abs_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002886 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08002887 const dex::MethodId& method_id = dex_file_->GetMethodId(method_idx);
Andreas Gampea5b09a62016-11-17 15:21:22 -08002888 dex::TypeIndex return_type_idx =
2889 dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Mathieu Chartierde40d472015-10-15 17:47:48 -07002890 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002891 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002892 descriptor = abs_method->GetReturnTypeDescriptor();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002893 }
Andreas Gampe51de69e2019-04-19 15:14:14 -07002894 const RegType& return_type = reg_types_.FromDescriptor(class_loader_.Get(),
2895 descriptor,
2896 false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002897 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002898 work_line_->SetResultRegisterType(this, return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002899 } else {
2900 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2901 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002902 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002903 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002904 }
Narayan Kamath9823e782016-08-03 12:46:58 +01002905 case Instruction::INVOKE_POLYMORPHIC:
2906 case Instruction::INVOKE_POLYMORPHIC_RANGE: {
Orion Hodsoncfa325e2016-10-13 10:25:54 +01002907 bool is_range = (inst->Opcode() == Instruction::INVOKE_POLYMORPHIC_RANGE);
2908 ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_POLYMORPHIC, is_range);
2909 if (called_method == nullptr) {
2910 // Convert potential soft failures in VerifyInvocationArgs() to hard errors.
2911 if (failure_messages_.size() > 0) {
2912 std::string message = failure_messages_.back()->str();
2913 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << message;
2914 } else {
2915 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invoke-polymorphic verification failure.";
2916 }
2917 break;
2918 }
2919 if (!CheckSignaturePolymorphicMethod(called_method) ||
2920 !CheckSignaturePolymorphicReceiver(inst)) {
Orion Hodsonfe92d122018-01-02 10:45:17 +00002921 DCHECK(HasFailures());
Orion Hodsoncfa325e2016-10-13 10:25:54 +01002922 break;
2923 }
Orion Hodson06d10a72018-05-14 08:53:38 +01002924 const uint16_t vRegH = (is_range) ? inst->VRegH_4rcc() : inst->VRegH_45cc();
2925 const dex::ProtoIndex proto_idx(vRegH);
Orion Hodsonac141392017-01-13 11:53:47 +00002926 const char* return_descriptor =
Orion Hodsoncfa325e2016-10-13 10:25:54 +01002927 dex_file_->GetReturnTypeDescriptor(dex_file_->GetProtoId(proto_idx));
2928 const RegType& return_type =
Andreas Gampe51de69e2019-04-19 15:14:14 -07002929 reg_types_.FromDescriptor(class_loader_.Get(), return_descriptor, false);
Orion Hodsoncfa325e2016-10-13 10:25:54 +01002930 if (!return_type.IsLowHalf()) {
2931 work_line_->SetResultRegisterType(this, return_type);
2932 } else {
2933 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2934 }
Orion Hodsonac141392017-01-13 11:53:47 +00002935 just_set_result = true;
Orion Hodsoncfa325e2016-10-13 10:25:54 +01002936 break;
Narayan Kamath9823e782016-08-03 12:46:58 +01002937 }
Orion Hodsonc069a302017-01-18 09:23:12 +00002938 case Instruction::INVOKE_CUSTOM:
2939 case Instruction::INVOKE_CUSTOM_RANGE: {
2940 // Verify registers based on method_type in the call site.
2941 bool is_range = (inst->Opcode() == Instruction::INVOKE_CUSTOM_RANGE);
2942
2943 // Step 1. Check the call site that produces the method handle for invocation
2944 const uint32_t call_site_idx = is_range ? inst->VRegB_3rc() : inst->VRegB_35c();
2945 if (!CheckCallSite(call_site_idx)) {
2946 DCHECK(HasFailures());
2947 break;
2948 }
2949
2950 // Step 2. Check the register arguments correspond to the expected arguments for the
2951 // method handle produced by step 1. The dex file verifier has checked ranges for
2952 // the first three arguments and CheckCallSite has checked the method handle type.
Orion Hodson4c8e12e2018-05-18 08:33:20 +01002953 const dex::ProtoIndex proto_idx = dex_file_->GetProtoIndexForCallSite(call_site_idx);
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08002954 const dex::ProtoId& proto_id = dex_file_->GetProtoId(proto_idx);
Orion Hodsonc069a302017-01-18 09:23:12 +00002955 DexFileParameterIterator param_it(*dex_file_, proto_id);
2956 // Treat method as static as it has yet to be determined.
2957 VerifyInvocationArgsFromIterator(&param_it, inst, METHOD_STATIC, is_range, nullptr);
2958 const char* return_descriptor = dex_file_->GetReturnTypeDescriptor(proto_id);
2959
2960 // Step 3. Propagate return type information
2961 const RegType& return_type =
Andreas Gampe51de69e2019-04-19 15:14:14 -07002962 reg_types_.FromDescriptor(class_loader_.Get(), return_descriptor, false);
Orion Hodsonc069a302017-01-18 09:23:12 +00002963 if (!return_type.IsLowHalf()) {
2964 work_line_->SetResultRegisterType(this, return_type);
2965 } else {
2966 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2967 }
2968 just_set_result = true;
Orion Hodsonc069a302017-01-18 09:23:12 +00002969 break;
2970 }
jeffhaobdb76512011-09-07 11:43:16 -07002971 case Instruction::NEG_INT:
2972 case Instruction::NOT_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002973 work_line_->CheckUnaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002974 break;
2975 case Instruction::NEG_LONG:
2976 case Instruction::NOT_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002977 work_line_->CheckUnaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002978 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002979 break;
2980 case Instruction::NEG_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002981 work_line_->CheckUnaryOp(this, inst, reg_types_.Float(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002982 break;
2983 case Instruction::NEG_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002984 work_line_->CheckUnaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002985 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002986 break;
2987 case Instruction::INT_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002988 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002989 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002990 break;
2991 case Instruction::INT_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002992 work_line_->CheckUnaryOp(this, inst, reg_types_.Float(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002993 break;
2994 case Instruction::INT_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002995 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002996 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002997 break;
2998 case Instruction::LONG_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002999 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003000 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07003001 break;
3002 case Instruction::LONG_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003003 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003004 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07003005 break;
3006 case Instruction::LONG_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07003007 work_line_->CheckUnaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003008 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07003009 break;
3010 case Instruction::FLOAT_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003011 work_line_->CheckUnaryOp(this, inst, reg_types_.Integer(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07003012 break;
3013 case Instruction::FLOAT_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07003014 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003015 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07003016 break;
3017 case Instruction::FLOAT_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07003018 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003019 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07003020 break;
3021 case Instruction::DOUBLE_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003022 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003023 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07003024 break;
3025 case Instruction::DOUBLE_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07003026 work_line_->CheckUnaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003027 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07003028 break;
3029 case Instruction::DOUBLE_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003030 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003031 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07003032 break;
3033 case Instruction::INT_TO_BYTE:
Ian Rogers7b078e82014-09-10 14:44:24 -07003034 work_line_->CheckUnaryOp(this, inst, reg_types_.Byte(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003035 break;
3036 case Instruction::INT_TO_CHAR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003037 work_line_->CheckUnaryOp(this, inst, reg_types_.Char(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003038 break;
3039 case Instruction::INT_TO_SHORT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003040 work_line_->CheckUnaryOp(this, inst, reg_types_.Short(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003041 break;
3042
3043 case Instruction::ADD_INT:
3044 case Instruction::SUB_INT:
3045 case Instruction::MUL_INT:
3046 case Instruction::REM_INT:
3047 case Instruction::DIV_INT:
3048 case Instruction::SHL_INT:
3049 case Instruction::SHR_INT:
3050 case Instruction::USHR_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003051 work_line_->CheckBinaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003052 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07003053 break;
3054 case Instruction::AND_INT:
3055 case Instruction::OR_INT:
3056 case Instruction::XOR_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003057 work_line_->CheckBinaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003058 reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07003059 break;
3060 case Instruction::ADD_LONG:
3061 case Instruction::SUB_LONG:
3062 case Instruction::MUL_LONG:
3063 case Instruction::DIV_LONG:
3064 case Instruction::REM_LONG:
3065 case Instruction::AND_LONG:
3066 case Instruction::OR_LONG:
3067 case Instruction::XOR_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07003068 work_line_->CheckBinaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003069 reg_types_.LongLo(), reg_types_.LongHi(),
3070 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07003071 break;
3072 case Instruction::SHL_LONG:
3073 case Instruction::SHR_LONG:
3074 case Instruction::USHR_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07003075 /* shift distance is Int, making these different from other binary operations */
Ian Rogers7b078e82014-09-10 14:44:24 -07003076 work_line_->CheckBinaryOpWideShift(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003077 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003078 break;
3079 case Instruction::ADD_FLOAT:
3080 case Instruction::SUB_FLOAT:
3081 case Instruction::MUL_FLOAT:
3082 case Instruction::DIV_FLOAT:
3083 case Instruction::REM_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003084 work_line_->CheckBinaryOp(this, inst, reg_types_.Float(), reg_types_.Float(),
3085 reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07003086 break;
3087 case Instruction::ADD_DOUBLE:
3088 case Instruction::SUB_DOUBLE:
3089 case Instruction::MUL_DOUBLE:
3090 case Instruction::DIV_DOUBLE:
3091 case Instruction::REM_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07003092 work_line_->CheckBinaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003093 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
3094 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07003095 break;
3096 case Instruction::ADD_INT_2ADDR:
3097 case Instruction::SUB_INT_2ADDR:
3098 case Instruction::MUL_INT_2ADDR:
3099 case Instruction::REM_INT_2ADDR:
3100 case Instruction::SHL_INT_2ADDR:
3101 case Instruction::SHR_INT_2ADDR:
3102 case Instruction::USHR_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003103 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
3104 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07003105 break;
3106 case Instruction::AND_INT_2ADDR:
3107 case Instruction::OR_INT_2ADDR:
3108 case Instruction::XOR_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003109 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
3110 reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07003111 break;
3112 case Instruction::DIV_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003113 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
3114 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07003115 break;
3116 case Instruction::ADD_LONG_2ADDR:
3117 case Instruction::SUB_LONG_2ADDR:
3118 case Instruction::MUL_LONG_2ADDR:
3119 case Instruction::DIV_LONG_2ADDR:
3120 case Instruction::REM_LONG_2ADDR:
3121 case Instruction::AND_LONG_2ADDR:
3122 case Instruction::OR_LONG_2ADDR:
3123 case Instruction::XOR_LONG_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003124 work_line_->CheckBinaryOp2addrWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003125 reg_types_.LongLo(), reg_types_.LongHi(),
3126 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07003127 break;
3128 case Instruction::SHL_LONG_2ADDR:
3129 case Instruction::SHR_LONG_2ADDR:
3130 case Instruction::USHR_LONG_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003131 work_line_->CheckBinaryOp2addrWideShift(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003132 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003133 break;
3134 case Instruction::ADD_FLOAT_2ADDR:
3135 case Instruction::SUB_FLOAT_2ADDR:
3136 case Instruction::MUL_FLOAT_2ADDR:
3137 case Instruction::DIV_FLOAT_2ADDR:
3138 case Instruction::REM_FLOAT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003139 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Float(), reg_types_.Float(),
3140 reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07003141 break;
3142 case Instruction::ADD_DOUBLE_2ADDR:
3143 case Instruction::SUB_DOUBLE_2ADDR:
3144 case Instruction::MUL_DOUBLE_2ADDR:
3145 case Instruction::DIV_DOUBLE_2ADDR:
3146 case Instruction::REM_DOUBLE_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003147 work_line_->CheckBinaryOp2addrWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003148 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
3149 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07003150 break;
3151 case Instruction::ADD_INT_LIT16:
Ian Rogersf72a11d2014-10-30 15:41:08 -07003152 case Instruction::RSUB_INT_LIT16:
jeffhaobdb76512011-09-07 11:43:16 -07003153 case Instruction::MUL_INT_LIT16:
3154 case Instruction::DIV_INT_LIT16:
3155 case Instruction::REM_INT_LIT16:
Ian Rogers7b078e82014-09-10 14:44:24 -07003156 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), false,
3157 true);
jeffhaobdb76512011-09-07 11:43:16 -07003158 break;
3159 case Instruction::AND_INT_LIT16:
3160 case Instruction::OR_INT_LIT16:
3161 case Instruction::XOR_INT_LIT16:
Ian Rogers7b078e82014-09-10 14:44:24 -07003162 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), true,
3163 true);
jeffhaobdb76512011-09-07 11:43:16 -07003164 break;
3165 case Instruction::ADD_INT_LIT8:
3166 case Instruction::RSUB_INT_LIT8:
3167 case Instruction::MUL_INT_LIT8:
3168 case Instruction::DIV_INT_LIT8:
3169 case Instruction::REM_INT_LIT8:
3170 case Instruction::SHL_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07003171 case Instruction::SHR_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07003172 case Instruction::USHR_INT_LIT8:
Ian Rogers7b078e82014-09-10 14:44:24 -07003173 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), false,
3174 false);
jeffhaobdb76512011-09-07 11:43:16 -07003175 break;
3176 case Instruction::AND_INT_LIT8:
3177 case Instruction::OR_INT_LIT8:
3178 case Instruction::XOR_INT_LIT8:
Ian Rogers7b078e82014-09-10 14:44:24 -07003179 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), true,
3180 false);
jeffhaobdb76512011-09-07 11:43:16 -07003181 break;
3182
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003183 // Special instructions.
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07003184 case Instruction::RETURN_VOID_NO_BARRIER:
3185 if (IsConstructor() && !IsStatic()) {
Vladimir Markod93e3742018-07-18 10:58:13 +01003186 const RegType& declaring_class = GetDeclaringClass();
Andreas Gampe68df3202015-06-22 11:35:46 -07003187 if (declaring_class.IsUnresolvedReference()) {
3188 // We must iterate over the fields, even if we cannot use mirror classes to do so. Do it
3189 // manually over the underlying dex file.
3190 uint32_t first_index = GetFirstFinalInstanceFieldIndex(*dex_file_,
3191 dex_file_->GetMethodId(dex_method_idx_).class_idx_);
Andreas Gampee2abbc62017-09-15 11:59:26 -07003192 if (first_index != dex::kDexNoIndex) {
Andreas Gampe68df3202015-06-22 11:35:46 -07003193 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void-no-barrier not expected for field "
3194 << first_index;
3195 }
3196 break;
3197 }
Vladimir Marko2cffc5d2018-05-29 15:40:56 +01003198 ObjPtr<mirror::Class> klass = declaring_class.GetClass();
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07003199 for (uint32_t i = 0, num_fields = klass->NumInstanceFields(); i < num_fields; ++i) {
3200 if (klass->GetInstanceField(i)->IsFinal()) {
Mathieu Chartiere86deef2015-03-19 13:43:37 -07003201 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void-no-barrier not expected for "
David Sehr709b0702016-10-13 09:12:37 -07003202 << klass->GetInstanceField(i)->PrettyField();
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07003203 break;
3204 }
3205 }
Sebastien Hertzcc10e0e2013-06-28 14:24:48 +02003206 }
Andreas Gampeb2917962015-07-31 13:36:10 -07003207 // Handle this like a RETURN_VOID now. Code is duplicated to separate standard from
3208 // quickened opcodes (otherwise this could be a fall-through).
3209 if (!IsConstructor()) {
3210 if (!GetMethodReturnType().IsConflict()) {
3211 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void not expected";
3212 }
3213 }
Sebastien Hertzcc10e0e2013-06-28 14:24:48 +02003214 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003215
Ian Rogersd81871c2011-10-03 13:57:23 -07003216 /* These should never appear during verification. */
Mathieu Chartierffc605c2014-12-10 10:35:44 -08003217 case Instruction::UNUSED_3E ... Instruction::UNUSED_43:
Narayan Kamath8ec3bd22016-08-03 12:46:23 +01003218 case Instruction::UNUSED_F3 ... Instruction::UNUSED_F9:
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003219 case Instruction::UNUSED_79:
3220 case Instruction::UNUSED_7A:
jeffhaod5347e02012-03-22 17:25:05 -07003221 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Unexpected opcode " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07003222 break;
3223
3224 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003225 * DO NOT add a "default" clause here. Without it the compiler will
jeffhaobdb76512011-09-07 11:43:16 -07003226 * complain if an instruction is missing (which is desirable).
3227 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003228 } // end - switch (dec_insn.opcode)
jeffhaobdb76512011-09-07 11:43:16 -07003229
Ian Rogersad0b3a32012-04-16 14:50:24 -07003230 if (have_pending_hard_failure_) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08003231 if (Runtime::Current()->IsAotCompiler()) {
3232 /* When AOT compiling, check that the last failure is a hard failure */
Andreas Gampeb588f4c2015-05-26 13:35:39 -07003233 if (failures_[failures_.size() - 1] != VERIFY_ERROR_BAD_CLASS_HARD) {
3234 LOG(ERROR) << "Pending failures:";
3235 for (auto& error : failures_) {
3236 LOG(ERROR) << error;
3237 }
3238 for (auto& error_msg : failure_messages_) {
3239 LOG(ERROR) << error_msg->str();
3240 }
3241 LOG(FATAL) << "Pending hard failure, but last failure not hard.";
3242 }
Ian Rogerse1758fe2012-04-19 11:31:15 -07003243 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003244 /* immediate failure, reject class */
3245 info_messages_ << "Rejecting opcode " << inst->DumpString(dex_file_);
3246 return false;
jeffhaofaf459e2012-08-31 15:32:47 -07003247 } else if (have_pending_runtime_throw_failure_) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07003248 /* checking interpreter will throw, mark following code as unreachable */
jeffhaofaf459e2012-08-31 15:32:47 -07003249 opcode_flags = Instruction::kThrow;
Andreas Gampea727e372015-08-25 09:22:37 -07003250 // Note: the flag must be reset as it is only global to decouple Fail and is semantically per
3251 // instruction. However, RETURN checking may throw LOCKING errors, so we clear at the
3252 // very end.
jeffhaobdb76512011-09-07 11:43:16 -07003253 }
jeffhaobdb76512011-09-07 11:43:16 -07003254 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003255 * If we didn't just set the result register, clear it out. This ensures that you can only use
3256 * "move-result" immediately after the result is set. (We could check this statically, but it's
3257 * not expensive and it makes our debugging output cleaner.)
jeffhaobdb76512011-09-07 11:43:16 -07003258 */
3259 if (!just_set_result) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003260 work_line_->SetResultTypeToUnknown(this);
jeffhaobdb76512011-09-07 11:43:16 -07003261 }
3262
jeffhaobdb76512011-09-07 11:43:16 -07003263 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003264 * Handle "branch". Tag the branch target.
jeffhaobdb76512011-09-07 11:43:16 -07003265 *
3266 * NOTE: instructions like Instruction::EQZ provide information about the
jeffhaod1f0fde2011-09-08 17:25:33 -07003267 * state of the register when the branch is taken or not taken. For example,
jeffhaobdb76512011-09-07 11:43:16 -07003268 * somebody could get a reference field, check it for zero, and if the
3269 * branch is taken immediately store that register in a boolean field
jeffhaod1f0fde2011-09-08 17:25:33 -07003270 * since the value is known to be zero. We do not currently account for
jeffhaobdb76512011-09-07 11:43:16 -07003271 * that, and will reject the code.
3272 *
3273 * TODO: avoid re-fetching the branch target
3274 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003275 if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07003276 bool isConditional, selfOkay;
Ian Rogersd81871c2011-10-03 13:57:23 -07003277 if (!GetBranchOffset(work_insn_idx_, &branch_target, &isConditional, &selfOkay)) {
jeffhaobdb76512011-09-07 11:43:16 -07003278 /* should never happen after static verification */
jeffhaod5347e02012-03-22 17:25:05 -07003279 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad branch";
jeffhaobdb76512011-09-07 11:43:16 -07003280 return false;
3281 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08003282 DCHECK_EQ(isConditional, (opcode_flags & Instruction::kContinue) != 0);
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08003283 if (!CheckNotMoveExceptionOrMoveResult(code_item_accessor_.Insns(),
3284 work_insn_idx_ + branch_target)) {
jeffhaobdb76512011-09-07 11:43:16 -07003285 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07003286 }
jeffhaobdb76512011-09-07 11:43:16 -07003287 /* update branch target, set "changed" if appropriate */
Mathieu Chartierde40d472015-10-15 17:47:48 -07003288 if (nullptr != branch_line) {
Ian Rogersebbdd872014-07-07 23:53:08 -07003289 if (!UpdateRegisters(work_insn_idx_ + branch_target, branch_line.get(), false)) {
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003290 return false;
3291 }
3292 } else {
Ian Rogersebbdd872014-07-07 23:53:08 -07003293 if (!UpdateRegisters(work_insn_idx_ + branch_target, work_line_.get(), false)) {
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003294 return false;
3295 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003296 }
jeffhaobdb76512011-09-07 11:43:16 -07003297 }
3298
3299 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003300 * Handle "switch". Tag all possible branch targets.
jeffhaobdb76512011-09-07 11:43:16 -07003301 *
3302 * We've already verified that the table is structurally sound, so we
3303 * just need to walk through and tag the targets.
3304 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003305 if ((opcode_flags & Instruction::kSwitch) != 0) {
Andreas Gampe53de99c2015-08-17 13:43:55 -07003306 int offset_to_switch = insns[1] | (static_cast<int32_t>(insns[2]) << 16);
jeffhaobdb76512011-09-07 11:43:16 -07003307 const uint16_t* switch_insns = insns + offset_to_switch;
3308 int switch_count = switch_insns[1];
3309 int offset_to_targets, targ;
3310
3311 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
3312 /* 0 = sig, 1 = count, 2/3 = first key */
3313 offset_to_targets = 4;
3314 } else {
3315 /* 0 = sig, 1 = count, 2..count * 2 = keys */
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07003316 DCHECK((*insns & 0xff) == Instruction::SPARSE_SWITCH);
jeffhaobdb76512011-09-07 11:43:16 -07003317 offset_to_targets = 2 + 2 * switch_count;
3318 }
3319
3320 /* verify each switch target */
3321 for (targ = 0; targ < switch_count; targ++) {
3322 int offset;
3323 uint32_t abs_offset;
3324
3325 /* offsets are 32-bit, and only partly endian-swapped */
3326 offset = switch_insns[offset_to_targets + targ * 2] |
Andreas Gampe53de99c2015-08-17 13:43:55 -07003327 (static_cast<int32_t>(switch_insns[offset_to_targets + targ * 2 + 1]) << 16);
Ian Rogersd81871c2011-10-03 13:57:23 -07003328 abs_offset = work_insn_idx_ + offset;
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08003329 DCHECK_LT(abs_offset, code_item_accessor_.InsnsSizeInCodeUnits());
3330 if (!CheckNotMoveExceptionOrMoveResult(code_item_accessor_.Insns(), abs_offset)) {
jeffhaobdb76512011-09-07 11:43:16 -07003331 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07003332 }
Ian Rogersebbdd872014-07-07 23:53:08 -07003333 if (!UpdateRegisters(abs_offset, work_line_.get(), false)) {
jeffhaobdb76512011-09-07 11:43:16 -07003334 return false;
Ian Rogersebbdd872014-07-07 23:53:08 -07003335 }
jeffhaobdb76512011-09-07 11:43:16 -07003336 }
3337 }
3338
3339 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003340 * Handle instructions that can throw and that are sitting in a "try" block. (If they're not in a
3341 * "try" block when they throw, control transfers out of the method.)
jeffhaobdb76512011-09-07 11:43:16 -07003342 */
Mathieu Chartierde40d472015-10-15 17:47:48 -07003343 if ((opcode_flags & Instruction::kThrow) != 0 && GetInstructionFlags(work_insn_idx_).IsInTry()) {
Andreas Gampef91baf12014-07-18 15:41:00 -07003344 bool has_catch_all_handler = false;
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08003345 const dex::TryItem* try_item = code_item_accessor_.FindTryItem(work_insn_idx_);
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08003346 CHECK(try_item != nullptr);
Mathieu Chartierdc578c72017-12-27 11:51:45 -08003347 CatchHandlerIterator iterator(code_item_accessor_, *try_item);
jeffhaobdb76512011-09-07 11:43:16 -07003348
Andreas Gampef91baf12014-07-18 15:41:00 -07003349 // Need the linker to try and resolve the handled class to check if it's Throwable.
3350 ClassLinker* linker = Runtime::Current()->GetClassLinker();
3351
Ian Rogers0571d352011-11-03 19:51:38 -07003352 for (; iterator.HasNext(); iterator.Next()) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08003353 dex::TypeIndex handler_type_idx = iterator.GetHandlerTypeIndex();
3354 if (!handler_type_idx.IsValid()) {
Andreas Gampef91baf12014-07-18 15:41:00 -07003355 has_catch_all_handler = true;
3356 } else {
3357 // It is also a catch-all if it is java.lang.Throwable.
Vladimir Marko28e012a2017-12-07 11:22:59 +00003358 ObjPtr<mirror::Class> klass =
Vladimir Marko666ee3d2017-12-11 18:37:36 +00003359 linker->ResolveType(handler_type_idx, dex_cache_, class_loader_);
Andreas Gampef91baf12014-07-18 15:41:00 -07003360 if (klass != nullptr) {
Vladimir Markoadbceb72018-05-29 14:34:14 +01003361 if (klass == GetClassRoot<mirror::Throwable>()) {
Andreas Gampef91baf12014-07-18 15:41:00 -07003362 has_catch_all_handler = true;
3363 }
3364 } else {
3365 // Clear exception.
Ian Rogers7b078e82014-09-10 14:44:24 -07003366 DCHECK(self_->IsExceptionPending());
3367 self_->ClearException();
Andreas Gampef91baf12014-07-18 15:41:00 -07003368 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003369 }
jeffhaobdb76512011-09-07 11:43:16 -07003370 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003371 * Merge registers into the "catch" block. We want to use the "savedRegs" rather than
3372 * "work_regs", because at runtime the exception will be thrown before the instruction
3373 * modifies any registers.
jeffhaobdb76512011-09-07 11:43:16 -07003374 */
Ian Rogersebbdd872014-07-07 23:53:08 -07003375 if (!UpdateRegisters(iterator.GetHandlerAddress(), saved_line_.get(), false)) {
jeffhaobdb76512011-09-07 11:43:16 -07003376 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07003377 }
jeffhaobdb76512011-09-07 11:43:16 -07003378 }
3379
3380 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003381 * If the monitor stack depth is nonzero, there must be a "catch all" handler for this
3382 * instruction. This does apply to monitor-exit because of async exception handling.
jeffhaobdb76512011-09-07 11:43:16 -07003383 */
Andreas Gampef91baf12014-07-18 15:41:00 -07003384 if (work_line_->MonitorStackDepth() > 0 && !has_catch_all_handler) {
jeffhaobdb76512011-09-07 11:43:16 -07003385 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003386 * The state in work_line reflects the post-execution state. If the current instruction is a
3387 * monitor-enter and the monitor stack was empty, we don't need a catch-all (if it throws,
jeffhaobdb76512011-09-07 11:43:16 -07003388 * it will do so before grabbing the lock).
3389 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02003390 if (inst->Opcode() != Instruction::MONITOR_ENTER || work_line_->MonitorStackDepth() != 1) {
jeffhaod5347e02012-03-22 17:25:05 -07003391 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
Ian Rogersd81871c2011-10-03 13:57:23 -07003392 << "expected to be within a catch-all for an instruction where a monitor is held";
jeffhaobdb76512011-09-07 11:43:16 -07003393 return false;
3394 }
3395 }
3396 }
3397
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003398 /* Handle "continue". Tag the next consecutive instruction.
3399 * Note: Keep the code handling "continue" case below the "branch" and "switch" cases,
3400 * because it changes work_line_ when performing peephole optimization
3401 * and this change should not be used in those cases.
3402 */
Ian Rogers6d376ae2013-07-23 15:12:40 -07003403 if ((opcode_flags & Instruction::kContinue) != 0) {
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08003404 DCHECK_EQ(&code_item_accessor_.InstructionAt(work_insn_idx_), inst);
Ian Rogers7b078e82014-09-10 14:44:24 -07003405 uint32_t next_insn_idx = work_insn_idx_ + inst->SizeInCodeUnits();
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08003406 if (next_insn_idx >= code_item_accessor_.InsnsSizeInCodeUnits()) {
Ian Rogers6d376ae2013-07-23 15:12:40 -07003407 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Execution can walk off end of code area";
3408 return false;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003409 }
Ian Rogers6d376ae2013-07-23 15:12:40 -07003410 // The only way to get to a move-exception instruction is to get thrown there. Make sure the
3411 // next instruction isn't one.
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08003412 if (!CheckNotMoveException(code_item_accessor_.Insns(), next_insn_idx)) {
Ian Rogers6d376ae2013-07-23 15:12:40 -07003413 return false;
3414 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07003415 if (nullptr != fallthrough_line) {
Ian Rogers6d376ae2013-07-23 15:12:40 -07003416 // Make workline consistent with fallthrough computed from peephole optimization.
3417 work_line_->CopyFromLine(fallthrough_line.get());
3418 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07003419 if (GetInstructionFlags(next_insn_idx).IsReturn()) {
Ian Rogersb8c78592013-07-25 23:52:52 +00003420 // For returns we only care about the operand to the return, all other registers are dead.
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08003421 const Instruction* ret_inst = &code_item_accessor_.InstructionAt(next_insn_idx);
Andreas Gampea727e372015-08-25 09:22:37 -07003422 AdjustReturnLine(this, ret_inst, work_line_.get());
Ian Rogersb8c78592013-07-25 23:52:52 +00003423 }
Ian Rogers6d376ae2013-07-23 15:12:40 -07003424 RegisterLine* next_line = reg_table_.GetLine(next_insn_idx);
Ian Rogers7b078e82014-09-10 14:44:24 -07003425 if (next_line != nullptr) {
Ian Rogersebbdd872014-07-07 23:53:08 -07003426 // Merge registers into what we have for the next instruction, and set the "changed" flag if
3427 // needed. If the merge changes the state of the registers then the work line will be
3428 // updated.
3429 if (!UpdateRegisters(next_insn_idx, work_line_.get(), true)) {
Ian Rogers6d376ae2013-07-23 15:12:40 -07003430 return false;
3431 }
3432 } else {
3433 /*
3434 * We're not recording register data for the next instruction, so we don't know what the
3435 * prior state was. We have to assume that something has changed and re-evaluate it.
3436 */
Andreas Gampe51de69e2019-04-19 15:14:14 -07003437 GetModifiableInstructionFlags(next_insn_idx).SetChanged();
Ian Rogers6d376ae2013-07-23 15:12:40 -07003438 }
3439 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003440
jeffhaod1f0fde2011-09-08 17:25:33 -07003441 /* If we're returning from the method, make sure monitor stack is empty. */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003442 if ((opcode_flags & Instruction::kReturn) != 0) {
Andreas Gampea727e372015-08-25 09:22:37 -07003443 work_line_->VerifyMonitorStackEmpty(this);
jeffhaobdb76512011-09-07 11:43:16 -07003444 }
3445
3446 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003447 * Update start_guess. Advance to the next instruction of that's
3448 * possible, otherwise use the branch target if one was found. If
jeffhaobdb76512011-09-07 11:43:16 -07003449 * neither of those exists we're in a return or throw; leave start_guess
3450 * alone and let the caller sort it out.
3451 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003452 if ((opcode_flags & Instruction::kContinue) != 0) {
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08003453 DCHECK_EQ(&code_item_accessor_.InstructionAt(work_insn_idx_), inst);
Ian Rogers7b078e82014-09-10 14:44:24 -07003454 *start_guess = work_insn_idx_ + inst->SizeInCodeUnits();
Elliott Hughesadb8c672012-03-06 16:49:32 -08003455 } else if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07003456 /* we're still okay if branch_target is zero */
Ian Rogersd81871c2011-10-03 13:57:23 -07003457 *start_guess = work_insn_idx_ + branch_target;
jeffhaobdb76512011-09-07 11:43:16 -07003458 }
3459
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08003460 DCHECK_LT(*start_guess, code_item_accessor_.InsnsSizeInCodeUnits());
Mathieu Chartierde40d472015-10-15 17:47:48 -07003461 DCHECK(GetInstructionFlags(*start_guess).IsOpcode());
jeffhaobdb76512011-09-07 11:43:16 -07003462
Andreas Gampea727e372015-08-25 09:22:37 -07003463 if (have_pending_runtime_throw_failure_) {
3464 have_any_pending_runtime_throw_failure_ = true;
3465 // Reset the pending_runtime_throw flag now.
3466 have_pending_runtime_throw_failure_ = false;
3467 }
3468
jeffhaobdb76512011-09-07 11:43:16 -07003469 return true;
Brian Carlstrom7934ac22013-07-26 10:54:15 -07003470} // NOLINT(readability/fn_size)
jeffhaobdb76512011-09-07 11:43:16 -07003471
Mathieu Chartierde40d472015-10-15 17:47:48 -07003472void MethodVerifier::UninstantiableError(const char* descriptor) {
3473 Fail(VerifyError::VERIFY_ERROR_NO_CLASS) << "Could not create precise reference for "
3474 << "non-instantiable klass " << descriptor;
3475}
3476
Vladimir Marko28e012a2017-12-07 11:22:59 +00003477inline bool MethodVerifier::IsInstantiableOrPrimitive(ObjPtr<mirror::Class> klass) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07003478 return klass->IsInstantiable() || klass->IsPrimitive();
3479}
3480
Andreas Gampe98be1a92017-08-28 08:25:45 -07003481template <MethodVerifier::CheckAccess C>
3482const RegType& MethodVerifier::ResolveClass(dex::TypeIndex class_idx) {
Vladimir Marko666ee3d2017-12-11 18:37:36 +00003483 ClassLinker* linker = Runtime::Current()->GetClassLinker();
Vladimir Marko28e012a2017-12-07 11:22:59 +00003484 ObjPtr<mirror::Class> klass = can_load_classes_
Vladimir Marko666ee3d2017-12-11 18:37:36 +00003485 ? linker->ResolveType(class_idx, dex_cache_, class_loader_)
3486 : linker->LookupResolvedType(class_idx, dex_cache_.Get(), class_loader_.Get());
Vladimir Marko8d6768d2017-03-14 10:13:21 +00003487 if (can_load_classes_ && klass == nullptr) {
3488 DCHECK(self_->IsExceptionPending());
3489 self_->ClearException();
3490 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07003491 const RegType* result = nullptr;
Vladimir Marko9cb0c462017-04-21 13:31:41 +01003492 if (klass != nullptr) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07003493 bool precise = klass->CannotBeAssignedFromOtherTypes();
3494 if (precise && !IsInstantiableOrPrimitive(klass)) {
3495 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
3496 UninstantiableError(descriptor);
3497 precise = false;
3498 }
Vladimir Marko2cffc5d2018-05-29 15:40:56 +01003499 result = reg_types_.FindClass(klass, precise);
Mathieu Chartierde40d472015-10-15 17:47:48 -07003500 if (result == nullptr) {
3501 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
Vladimir Marko2cffc5d2018-05-29 15:40:56 +01003502 result = reg_types_.InsertClass(descriptor, klass, precise);
Mathieu Chartierde40d472015-10-15 17:47:48 -07003503 }
3504 } else {
3505 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
Andreas Gampe51de69e2019-04-19 15:14:14 -07003506 result = &reg_types_.FromDescriptor(class_loader_.Get(), descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003507 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07003508 DCHECK(result != nullptr);
3509 if (result->IsConflict()) {
3510 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
3511 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "accessing broken descriptor '" << descriptor
3512 << "' in " << GetDeclaringClass();
3513 return *result;
3514 }
David Brazdilca3c8c32016-09-06 14:04:48 +01003515
3516 // Record result of class resolution attempt.
Vladimir Markobcf17522018-06-01 13:14:32 +01003517 VerifierDeps::MaybeRecordClassResolution(*dex_file_, class_idx, klass);
David Brazdilca3c8c32016-09-06 14:04:48 +01003518
Andreas Gampe629be512017-08-25 17:09:32 -07003519 // If requested, check if access is allowed. Unresolved types are included in this check, as the
3520 // interpreter only tests whether access is allowed when a class is not pre-verified and runs in
3521 // the access-checks interpreter. If result is primitive, skip the access check.
3522 //
3523 // Note: we do this for unresolved classes to trigger re-verification at runtime.
Andreas Gampedc39d322018-09-04 09:26:03 -07003524 if (C == CheckAccess::kYes &&
3525 result->IsNonZeroReferenceTypes() &&
David Brazdil2bb2fbd2018-11-13 18:24:26 +00003526 (IsSdkVersionSetAndAtLeast(api_level_, SdkVersion::kP) || !result->IsUnresolvedTypes())) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07003527 const RegType& referrer = GetDeclaringClass();
David Brazdil2bb2fbd2018-11-13 18:24:26 +00003528 if ((IsSdkVersionSetAndAtLeast(api_level_, SdkVersion::kP) || !referrer.IsUnresolvedTypes()) &&
3529 !referrer.CanAccess(*result)) {
Andreas Gampe629be512017-08-25 17:09:32 -07003530 Fail(VERIFY_ERROR_ACCESS_CLASS) << "(possibly) illegal class access: '"
Nicolas Geoffray4e868fa2017-04-21 17:16:44 +01003531 << referrer << "' -> '" << *result << "'";
Mathieu Chartierde40d472015-10-15 17:47:48 -07003532 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07003533 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07003534 return *result;
Ian Rogersd81871c2011-10-03 13:57:23 -07003535}
3536
Andreas Gampedd309072017-08-31 07:47:37 -07003537// Instantiate ResolveClass variants. This is required as the -inl file has a function with a call
3538// to ResolveClass, and compilers may decide to inline, requiring a symbol.
Andreas Gampeae0b1f42017-08-30 17:36:26 -07003539template const RegType& MethodVerifier::ResolveClass<MethodVerifier::CheckAccess::kNo>(
3540 dex::TypeIndex class_idx);
3541template const RegType& MethodVerifier::ResolveClass<MethodVerifier::CheckAccess::kYes>(
3542 dex::TypeIndex class_idx);
3543
Ian Rogersd8f69b02014-09-10 21:43:52 +00003544const RegType& MethodVerifier::GetCaughtExceptionType() {
Ian Rogers7b078e82014-09-10 14:44:24 -07003545 const RegType* common_super = nullptr;
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08003546 if (code_item_accessor_.TriesSize() != 0) {
3547 const uint8_t* handlers_ptr = code_item_accessor_.GetCatchHandlerData();
Ian Rogersd81871c2011-10-03 13:57:23 -07003548 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
3549 for (uint32_t i = 0; i < handlers_size; i++) {
Ian Rogers0571d352011-11-03 19:51:38 -07003550 CatchHandlerIterator iterator(handlers_ptr);
3551 for (; iterator.HasNext(); iterator.Next()) {
3552 if (iterator.GetHandlerAddress() == (uint32_t) work_insn_idx_) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08003553 if (!iterator.GetHandlerTypeIndex().IsValid()) {
Ian Rogersb4903572012-10-11 11:52:56 -07003554 common_super = &reg_types_.JavaLangThrowable(false);
Ian Rogersd81871c2011-10-03 13:57:23 -07003555 } else {
Andreas Gampe98be1a92017-08-28 08:25:45 -07003556 const RegType& exception =
3557 ResolveClass<CheckAccess::kYes>(iterator.GetHandlerTypeIndex());
David Brazdilca3c8c32016-09-06 14:04:48 +01003558 if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(exception, this)) {
David Brazdil68b5c0b2016-01-19 14:25:29 +00003559 DCHECK(!exception.IsUninitializedTypes()); // Comes from dex, shouldn't be uninit.
Jeff Haoc26a56c2013-11-04 12:00:47 -08003560 if (exception.IsUnresolvedTypes()) {
3561 // We don't know enough about the type. Fail here and let runtime handle it.
3562 Fail(VERIFY_ERROR_NO_CLASS) << "unresolved exception class " << exception;
3563 return exception;
3564 } else {
3565 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unexpected non-exception class " << exception;
3566 return reg_types_.Conflict();
3567 }
Jeff Haob878f212014-04-24 16:25:36 -07003568 } else if (common_super == nullptr) {
3569 common_super = &exception;
Ian Rogers28ad40d2011-10-27 15:19:26 -07003570 } else if (common_super->Equals(exception)) {
Ian Rogersc4762272012-02-01 15:55:55 -08003571 // odd case, but nothing to do
Ian Rogersd81871c2011-10-03 13:57:23 -07003572 } else {
David Brazdilca3c8c32016-09-06 14:04:48 +01003573 common_super = &common_super->Merge(exception, &reg_types_, this);
Mathieu Chartierc2a2bda2018-03-12 11:21:52 -07003574 if (FailOrAbort(reg_types_.JavaLangThrowable(false).IsAssignableFrom(
David Brazdilca3c8c32016-09-06 14:04:48 +01003575 *common_super, this),
Andreas Gampe7c038102014-10-27 20:08:46 -07003576 "java.lang.Throwable is not assignable-from common_super at ",
3577 work_insn_idx_)) {
3578 break;
3579 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003580 }
3581 }
3582 }
3583 }
Ian Rogers0571d352011-11-03 19:51:38 -07003584 handlers_ptr = iterator.EndDataPointer();
Ian Rogersd81871c2011-10-03 13:57:23 -07003585 }
3586 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003587 if (common_super == nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003588 /* no catch blocks, or no catches with classes we can find */
jeffhaod5347e02012-03-22 17:25:05 -07003589 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unable to find exception handler";
Ian Rogersad0b3a32012-04-16 14:50:24 -07003590 return reg_types_.Conflict();
Ian Rogersd81871c2011-10-03 13:57:23 -07003591 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07003592 return *common_super;
Ian Rogersd81871c2011-10-03 13:57:23 -07003593}
3594
Mathieu Chartiere401d142015-04-22 13:56:20 -07003595ArtMethod* MethodVerifier::ResolveMethodAndCheckAccess(
Alex Light7268d472016-01-20 15:50:01 -08003596 uint32_t dex_method_idx, MethodType method_type) {
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08003597 const dex::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx);
Andreas Gampe98be1a92017-08-28 08:25:45 -07003598 const RegType& klass_type = ResolveClass<CheckAccess::kYes>(method_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003599 if (klass_type.IsConflict()) {
3600 std::string append(" in attempt to access method ");
3601 append += dex_file_->GetMethodName(method_id);
3602 AppendToLastFailMessage(append);
Ian Rogers7b078e82014-09-10 14:44:24 -07003603 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08003604 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003605 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003606 return nullptr; // Can't resolve Class so no more to do here
Ian Rogers90040192011-12-16 08:54:29 -08003607 }
Vladimir Markoba118822017-06-12 15:41:56 +01003608 ObjPtr<mirror::Class> klass = klass_type.GetClass();
Ian Rogersd8f69b02014-09-10 21:43:52 +00003609 const RegType& referrer = GetDeclaringClass();
Nicolas Geoffrayea179f42018-02-08 22:30:18 +00003610 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
3611 PointerSize pointer_size = class_linker->GetImagePointerSize();
Andreas Gampe42ef8ab2015-12-03 17:27:32 -08003612
Mathieu Chartiere401d142015-04-22 13:56:20 -07003613 ArtMethod* res_method = dex_cache_->GetResolvedMethod(dex_method_idx, pointer_size);
Ian Rogers7b078e82014-09-10 14:44:24 -07003614 if (res_method == nullptr) {
Nicolas Geoffrayea179f42018-02-08 22:30:18 +00003615 res_method = class_linker->FindResolvedMethod(
3616 klass, dex_cache_.Get(), class_loader_.Get(), dex_method_idx);
Ian Rogersd81871c2011-10-03 13:57:23 -07003617 }
David Brazdilca3c8c32016-09-06 14:04:48 +01003618
Vladimir Markoba118822017-06-12 15:41:56 +01003619 // Record result of method resolution attempt. The klass resolution has recorded whether
3620 // the class is an interface or not and therefore the type of the lookup performed above.
3621 // TODO: Maybe we should not record dependency if the invoke type does not match the lookup type.
3622 VerifierDeps::MaybeRecordMethodResolution(*dex_file_, dex_method_idx, res_method);
3623
Alex Lightafb66472017-08-01 09:54:49 -07003624 bool must_fail = false;
3625 // This is traditional and helps with screwy bytecode. It will tell you that, yes, a method
3626 // exists, but that it's called incorrectly. This significantly helps debugging, as locally it's
3627 // hard to see the differences.
3628 // If we don't have res_method here we must fail. Just use this bool to make sure of that with a
3629 // DCHECK.
Vladimir Markoba118822017-06-12 15:41:56 +01003630 if (res_method == nullptr) {
Alex Lightafb66472017-08-01 09:54:49 -07003631 must_fail = true;
Vladimir Markoba118822017-06-12 15:41:56 +01003632 // Try to find the method also with the other type for better error reporting below
3633 // but do not store such bogus lookup result in the DexCache or VerifierDeps.
David Brazdil4525e0b2018-04-05 16:57:32 +01003634 res_method = class_linker->FindIncompatibleMethod(
3635 klass, dex_cache_.Get(), class_loader_.Get(), dex_method_idx);
Vladimir Markoba118822017-06-12 15:41:56 +01003636 }
David Brazdilca3c8c32016-09-06 14:04:48 +01003637
3638 if (res_method == nullptr) {
3639 Fail(VERIFY_ERROR_NO_METHOD) << "couldn't find method "
David Sehr709b0702016-10-13 09:12:37 -07003640 << klass->PrettyDescriptor() << "."
David Brazdilca3c8c32016-09-06 14:04:48 +01003641 << dex_file_->GetMethodName(method_id) << " "
3642 << dex_file_->GetMethodSignature(method_id);
3643 return nullptr;
3644 }
3645
Ian Rogersd81871c2011-10-03 13:57:23 -07003646 // Make sure calls to constructors are "direct". There are additional restrictions but we don't
3647 // enforce them here.
3648 if (res_method->IsConstructor() && method_type != METHOD_DIRECT) {
jeffhaod5347e02012-03-22 17:25:05 -07003649 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting non-direct call to constructor "
David Sehr709b0702016-10-13 09:12:37 -07003650 << res_method->PrettyMethod();
Ian Rogers7b078e82014-09-10 14:44:24 -07003651 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003652 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003653 // Disallow any calls to class initializers.
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003654 if (res_method->IsClassInitializer()) {
jeffhaod5347e02012-03-22 17:25:05 -07003655 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting call to class initializer "
David Sehr709b0702016-10-13 09:12:37 -07003656 << res_method->PrettyMethod();
Ian Rogers7b078e82014-09-10 14:44:24 -07003657 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003658 }
Andreas Gampe42ef8ab2015-12-03 17:27:32 -08003659
3660 // Check that interface methods are static or match interface classes.
3661 // We only allow statics if we don't have default methods enabled.
3662 //
3663 // Note: this check must be after the initializer check, as those are required to fail a class,
3664 // while this check implies an IncompatibleClassChangeError.
3665 if (klass->IsInterface()) {
Alex Lightb55f1ac2016-04-12 15:50:55 -07003666 // methods called on interfaces should be invoke-interface, invoke-super, invoke-direct (if
Mathieu Chartierf6e31472017-12-28 13:32:08 -08003667 // default methods are supported for the dex file), or invoke-static.
Andreas Gampe42ef8ab2015-12-03 17:27:32 -08003668 if (method_type != METHOD_INTERFACE &&
Neil Fuller9724c632016-01-07 15:42:47 +00003669 method_type != METHOD_STATIC &&
Mathieu Chartierf6e31472017-12-28 13:32:08 -08003670 (!dex_file_->SupportsDefaultMethods() ||
Alex Lightb55f1ac2016-04-12 15:50:55 -07003671 method_type != METHOD_DIRECT) &&
Neil Fuller9724c632016-01-07 15:42:47 +00003672 method_type != METHOD_SUPER) {
Andreas Gampe42ef8ab2015-12-03 17:27:32 -08003673 Fail(VERIFY_ERROR_CLASS_CHANGE)
David Sehr709b0702016-10-13 09:12:37 -07003674 << "non-interface method " << dex_file_->PrettyMethod(dex_method_idx)
3675 << " is in an interface class " << klass->PrettyClass();
Andreas Gampe42ef8ab2015-12-03 17:27:32 -08003676 return nullptr;
3677 }
3678 } else {
3679 if (method_type == METHOD_INTERFACE) {
3680 Fail(VERIFY_ERROR_CLASS_CHANGE)
David Sehr709b0702016-10-13 09:12:37 -07003681 << "interface method " << dex_file_->PrettyMethod(dex_method_idx)
3682 << " is in a non-interface class " << klass->PrettyClass();
Andreas Gampe42ef8ab2015-12-03 17:27:32 -08003683 return nullptr;
3684 }
3685 }
3686
Alex Lightafb66472017-08-01 09:54:49 -07003687 // Check specifically for non-public object methods being provided for interface dispatch. This
3688 // can occur if we failed to find a method with FindInterfaceMethod but later find one with
3689 // FindClassMethod for error message use.
3690 if (method_type == METHOD_INTERFACE &&
3691 res_method->GetDeclaringClass()->IsObjectClass() &&
3692 !res_method->IsPublic()) {
3693 Fail(VERIFY_ERROR_NO_METHOD) << "invoke-interface " << klass->PrettyDescriptor() << "."
3694 << dex_file_->GetMethodName(method_id) << " "
3695 << dex_file_->GetMethodSignature(method_id) << " resolved to "
3696 << "non-public object method " << res_method->PrettyMethod() << " "
3697 << "but non-public Object methods are excluded from interface "
3698 << "method resolution.";
3699 return nullptr;
3700 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003701 // Check if access is allowed.
Ian Rogersad0b3a32012-04-16 14:50:24 -07003702 if (!referrer.CanAccessMember(res_method->GetDeclaringClass(), res_method->GetAccessFlags())) {
David Sehr709b0702016-10-13 09:12:37 -07003703 Fail(VERIFY_ERROR_ACCESS_METHOD) << "illegal method access (call "
3704 << res_method->PrettyMethod()
Ian Rogersad0b3a32012-04-16 14:50:24 -07003705 << " from " << referrer << ")";
jeffhaob57e9522012-04-26 18:08:21 -07003706 return res_method;
jeffhao8cd6dda2012-02-22 10:15:34 -08003707 }
jeffhaode0d9c92012-02-27 13:58:13 -08003708 // Check that invoke-virtual and invoke-super are not used on private methods of the same class.
Alex Light7268d472016-01-20 15:50:01 -08003709 if (res_method->IsPrivate() && (method_type == METHOD_VIRTUAL || method_type == METHOD_SUPER)) {
jeffhaod5347e02012-03-22 17:25:05 -07003710 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invoke-super/virtual can't be used on private method "
David Sehr709b0702016-10-13 09:12:37 -07003711 << res_method->PrettyMethod();
Ian Rogers7b078e82014-09-10 14:44:24 -07003712 return nullptr;
jeffhaode0d9c92012-02-27 13:58:13 -08003713 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003714 // See if the method type implied by the invoke instruction matches the access flags for the
Orion Hodsoncfa325e2016-10-13 10:25:54 +01003715 // target method. The flags for METHOD_POLYMORPHIC are based on there being precisely two
3716 // signature polymorphic methods supported by the run-time which are native methods with variable
3717 // arguments.
Brian Carlstrombe6fa5e2014-12-09 20:15:42 -08003718 if ((method_type == METHOD_DIRECT && (!res_method->IsDirect() || res_method->IsStatic())) ||
Ian Rogersd81871c2011-10-03 13:57:23 -07003719 (method_type == METHOD_STATIC && !res_method->IsStatic()) ||
Alex Light7268d472016-01-20 15:50:01 -08003720 ((method_type == METHOD_SUPER ||
3721 method_type == METHOD_VIRTUAL ||
Orion Hodsoncfa325e2016-10-13 10:25:54 +01003722 method_type == METHOD_INTERFACE) && res_method->IsDirect()) ||
3723 ((method_type == METHOD_POLYMORPHIC) &&
3724 (!res_method->IsNative() || !res_method->IsVarargs()))) {
Ian Rogers2fc14272012-08-30 10:56:57 -07003725 Fail(VERIFY_ERROR_CLASS_CHANGE) << "invoke type (" << method_type << ") does not match method "
Orion Hodsoncfa325e2016-10-13 10:25:54 +01003726 "type of " << res_method->PrettyMethod();
Ian Rogers7b078e82014-09-10 14:44:24 -07003727 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003728 }
Alex Lightafb66472017-08-01 09:54:49 -07003729 // Make sure we weren't expecting to fail.
3730 DCHECK(!must_fail) << "invoke type (" << method_type << ")"
3731 << klass->PrettyDescriptor() << "."
3732 << dex_file_->GetMethodName(method_id) << " "
3733 << dex_file_->GetMethodSignature(method_id) << " unexpectedly resolved to "
3734 << res_method->PrettyMethod() << " without error. Initially this method was "
3735 << "not found so we were expecting to fail for some reason.";
jeffhao8cd6dda2012-02-22 10:15:34 -08003736 return res_method;
3737}
3738
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003739template <class T>
Mathieu Chartiere401d142015-04-22 13:56:20 -07003740ArtMethod* MethodVerifier::VerifyInvocationArgsFromIterator(
3741 T* it, const Instruction* inst, MethodType method_type, bool is_range, ArtMethod* res_method) {
Andreas Gampee383d232018-06-19 12:29:51 -07003742 DCHECK_EQ(!is_range, inst->HasVarArgs());
3743
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003744 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
3745 // match the call to the signature. Also, we might be calling through an abstract method
3746 // definition (which doesn't have register count values).
Orion Hodsoncfa325e2016-10-13 10:25:54 +01003747 const size_t expected_args = inst->VRegA();
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003748 /* caught by static verifier */
3749 DCHECK(is_range || expected_args <= 5);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003750
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08003751 if (expected_args > code_item_accessor_.OutsSize()) {
Orion Hodson1cda7c22017-08-10 13:06:45 +01003752 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08003753 << ") exceeds outsSize ("
3754 << code_item_accessor_.OutsSize() << ")";
Orion Hodson1cda7c22017-08-10 13:06:45 +01003755 return nullptr;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003756 }
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003757
3758 /*
3759 * Check the "this" argument, which must be an instance of the class that declared the method.
3760 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
3761 * rigorous check here (which is okay since we have to do it at runtime).
3762 */
3763 if (method_type != METHOD_STATIC) {
Orion Hodsoncfa325e2016-10-13 10:25:54 +01003764 const RegType& actual_arg_type = work_line_->GetInvocationThis(this, inst);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003765 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
3766 CHECK(have_pending_hard_failure_);
3767 return nullptr;
3768 }
David Brazdil68b5c0b2016-01-19 14:25:29 +00003769 bool is_init = false;
3770 if (actual_arg_type.IsUninitializedTypes()) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003771 if (res_method) {
3772 if (!res_method->IsConstructor()) {
3773 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
3774 return nullptr;
3775 }
3776 } else {
3777 // Check whether the name of the called method is "<init>"
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00003778 const uint32_t method_idx = GetMethodIdxOfInvoke(inst);
Jeff Hao0d087272014-08-04 14:47:17 -07003779 if (strcmp(dex_file_->GetMethodName(dex_file_->GetMethodId(method_idx)), "<init>") != 0) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003780 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
3781 return nullptr;
3782 }
3783 }
David Brazdil68b5c0b2016-01-19 14:25:29 +00003784 is_init = true;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003785 }
David Brazdil68b5c0b2016-01-19 14:25:29 +00003786 const RegType& adjusted_type = is_init
3787 ? GetRegTypeCache()->FromUninitialized(actual_arg_type)
3788 : actual_arg_type;
Andreas Gampeeed3a5d2017-11-29 14:58:34 -08003789 if (method_type != METHOD_INTERFACE && !adjusted_type.IsZeroOrNull()) {
Ian Rogersd8f69b02014-09-10 21:43:52 +00003790 const RegType* res_method_class;
Andreas Gamped9e23012015-06-04 22:19:58 -07003791 // Miranda methods have the declaring interface as their declaring class, not the abstract
3792 // class. It would be wrong to use this for the type check (interface type checks are
3793 // postponed to runtime).
3794 if (res_method != nullptr && !res_method->IsMiranda()) {
Vladimir Markod93e3742018-07-18 10:58:13 +01003795 ObjPtr<mirror::Class> klass = res_method->GetDeclaringClass();
Ian Rogers1ff3c982014-08-12 02:30:58 -07003796 std::string temp;
Andreas Gampef23f33d2015-06-23 14:18:17 -07003797 res_method_class = &FromClass(klass->GetDescriptor(&temp), klass,
3798 klass->CannotBeAssignedFromOtherTypes());
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003799 } else {
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00003800 const uint32_t method_idx = GetMethodIdxOfInvoke(inst);
Andreas Gampea5b09a62016-11-17 15:21:22 -08003801 const dex::TypeIndex class_idx = dex_file_->GetMethodId(method_idx).class_idx_;
Mathieu Chartierde40d472015-10-15 17:47:48 -07003802 res_method_class = &reg_types_.FromDescriptor(
Andreas Gampe51de69e2019-04-19 15:14:14 -07003803 class_loader_.Get(),
Mathieu Chartierde40d472015-10-15 17:47:48 -07003804 dex_file_->StringByTypeIdx(class_idx),
3805 false);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003806 }
David Brazdilca3c8c32016-09-06 14:04:48 +01003807 if (!res_method_class->IsAssignableFrom(adjusted_type, this)) {
David Brazdil68b5c0b2016-01-19 14:25:29 +00003808 Fail(adjusted_type.IsUnresolvedTypes()
3809 ? VERIFY_ERROR_NO_CLASS
3810 : VERIFY_ERROR_BAD_CLASS_SOFT)
3811 << "'this' argument '" << actual_arg_type << "' not instance of '"
3812 << *res_method_class << "'";
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003813 // Continue on soft failures. We need to find possible hard failures to avoid problems in
3814 // the compiler.
3815 if (have_pending_hard_failure_) {
3816 return nullptr;
3817 }
3818 }
3819 }
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003820 }
3821
Orion Hodsoncfa325e2016-10-13 10:25:54 +01003822 uint32_t arg[5];
3823 if (!is_range) {
3824 inst->GetVarArgs(arg);
3825 }
3826 uint32_t sig_registers = (method_type == METHOD_STATIC) ? 0 : 1;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003827 for ( ; it->HasNext(); it->Next()) {
3828 if (sig_registers >= expected_args) {
3829 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, expected " << inst->VRegA() <<
Orion Hodsoncfa325e2016-10-13 10:25:54 +01003830 " argument registers, method signature has " << sig_registers + 1 << " or more";
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003831 return nullptr;
3832 }
3833
3834 const char* param_descriptor = it->GetDescriptor();
3835
3836 if (param_descriptor == nullptr) {
3837 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation because of missing signature "
3838 "component";
3839 return nullptr;
3840 }
3841
Andreas Gampe51de69e2019-04-19 15:14:14 -07003842 const RegType& reg_type = reg_types_.FromDescriptor(class_loader_.Get(),
3843 param_descriptor,
3844 false);
Orion Hodsoncfa325e2016-10-13 10:25:54 +01003845 uint32_t get_reg = is_range ? inst->VRegC() + static_cast<uint32_t>(sig_registers) :
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003846 arg[sig_registers];
3847 if (reg_type.IsIntegralTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003848 const RegType& src_type = work_line_->GetRegisterType(this, get_reg);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003849 if (!src_type.IsIntegralTypes()) {
3850 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register v" << get_reg << " has type " << src_type
3851 << " but expected " << reg_type;
Andreas Gampeb588f4c2015-05-26 13:35:39 -07003852 return nullptr;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003853 }
Andreas Gampeda9badb2015-06-05 20:22:12 -07003854 } else {
3855 if (!work_line_->VerifyRegisterType(this, get_reg, reg_type)) {
3856 // Continue on soft failures. We need to find possible hard failures to avoid problems in
3857 // the compiler.
3858 if (have_pending_hard_failure_) {
3859 return nullptr;
3860 }
3861 } else if (reg_type.IsLongOrDoubleTypes()) {
3862 // Check that registers are consecutive (for non-range invokes). Invokes are the only
3863 // instructions not specifying register pairs by the first component, but require them
3864 // nonetheless. Only check when there's an actual register in the parameters. If there's
3865 // none, this will fail below.
3866 if (!is_range && sig_registers + 1 < expected_args) {
3867 uint32_t second_reg = arg[sig_registers + 1];
3868 if (second_reg != get_reg + 1) {
3869 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, long or double parameter "
3870 "at index " << sig_registers << " is not a pair: " << get_reg << " + "
3871 << second_reg << ".";
3872 return nullptr;
3873 }
3874 }
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003875 }
3876 }
3877 sig_registers += reg_type.IsLongOrDoubleTypes() ? 2 : 1;
3878 }
3879 if (expected_args != sig_registers) {
3880 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, expected " << expected_args <<
Orion Hodsoncfa325e2016-10-13 10:25:54 +01003881 " argument registers, method signature has " << sig_registers;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003882 return nullptr;
3883 }
3884 return res_method;
3885}
3886
3887void MethodVerifier::VerifyInvocationArgsUnresolvedMethod(const Instruction* inst,
3888 MethodType method_type,
3889 bool is_range) {
3890 // As the method may not have been resolved, make this static check against what we expect.
3891 // The main reason for this code block is to fail hard when we find an illegal use, e.g.,
3892 // wrong number of arguments or wrong primitive types, even if the method could not be resolved.
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00003893 const uint32_t method_idx = GetMethodIdxOfInvoke(inst);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003894 DexFileParameterIterator it(*dex_file_,
3895 dex_file_->GetProtoId(dex_file_->GetMethodId(method_idx).proto_idx_));
Orion Hodsoncfa325e2016-10-13 10:25:54 +01003896 VerifyInvocationArgsFromIterator(&it, inst, method_type, is_range, nullptr);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003897}
3898
Orion Hodsonc069a302017-01-18 09:23:12 +00003899bool MethodVerifier::CheckCallSite(uint32_t call_site_idx) {
Orion Hodson3a842f52017-04-21 15:24:10 +01003900 if (call_site_idx >= dex_file_->NumCallSiteIds()) {
3901 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Bad call site id #" << call_site_idx
3902 << " >= " << dex_file_->NumCallSiteIds();
3903 return false;
3904 }
3905
Orion Hodsonc069a302017-01-18 09:23:12 +00003906 CallSiteArrayValueIterator it(*dex_file_, dex_file_->GetCallSiteId(call_site_idx));
3907 // Check essential arguments are provided. The dex file verifier has verified indicies of the
3908 // main values (method handle, name, method_type).
Orion Hodson4c8e12e2018-05-18 08:33:20 +01003909 static const size_t kRequiredArguments = 3;
3910 if (it.Size() < kRequiredArguments) {
Orion Hodsonc069a302017-01-18 09:23:12 +00003911 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Call site #" << call_site_idx
3912 << " has too few arguments: "
Orion Hodson4c8e12e2018-05-18 08:33:20 +01003913 << it.Size() << " < " << kRequiredArguments;
Orion Hodsonc069a302017-01-18 09:23:12 +00003914 return false;
3915 }
3916
Orion Hodson4c8e12e2018-05-18 08:33:20 +01003917 std::pair<const EncodedArrayValueIterator::ValueType, size_t> type_and_max[kRequiredArguments] =
3918 { { EncodedArrayValueIterator::ValueType::kMethodHandle, dex_file_->NumMethodHandles() },
3919 { EncodedArrayValueIterator::ValueType::kString, dex_file_->NumStringIds() },
3920 { EncodedArrayValueIterator::ValueType::kMethodType, dex_file_->NumProtoIds() }
3921 };
3922 uint32_t index[kRequiredArguments];
3923
3924 // Check arguments have expected types and are within permitted ranges.
3925 for (size_t i = 0; i < kRequiredArguments; ++i) {
3926 if (it.GetValueType() != type_and_max[i].first) {
3927 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Call site id #" << call_site_idx
3928 << " argument " << i << " has wrong type "
3929 << it.GetValueType() << "!=" << type_and_max[i].first;
3930 return false;
3931 }
3932 index[i] = static_cast<uint32_t>(it.GetJavaValue().i);
3933 if (index[i] >= type_and_max[i].second) {
3934 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Call site id #" << call_site_idx
3935 << " argument " << i << " bad index "
3936 << index[i] << " >= " << type_and_max[i].second;
3937 return false;
3938 }
3939 it.Next();
Orion Hodsona5dca522018-02-27 12:42:11 +00003940 }
Orion Hodson3a842f52017-04-21 15:24:10 +01003941
Orion Hodson4c8e12e2018-05-18 08:33:20 +01003942 // Check method handle kind is valid.
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08003943 const dex::MethodHandleItem& mh = dex_file_->GetMethodHandle(index[0]);
Orion Hodsonc069a302017-01-18 09:23:12 +00003944 if (mh.method_handle_type_ != static_cast<uint16_t>(DexFile::MethodHandleType::kInvokeStatic)) {
3945 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Call site #" << call_site_idx
Orion Hodson631827d2017-04-10 14:53:47 +01003946 << " argument 0 method handle type is not InvokeStatic: "
3947 << mh.method_handle_type_;
Orion Hodsonc069a302017-01-18 09:23:12 +00003948 return false;
3949 }
Orion Hodsonc069a302017-01-18 09:23:12 +00003950 return true;
3951}
3952
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003953class MethodParamListDescriptorIterator {
3954 public:
Mathieu Chartiere401d142015-04-22 13:56:20 -07003955 explicit MethodParamListDescriptorIterator(ArtMethod* res_method) :
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003956 res_method_(res_method), pos_(0), params_(res_method->GetParameterTypeList()),
3957 params_size_(params_ == nullptr ? 0 : params_->Size()) {
3958 }
3959
3960 bool HasNext() {
3961 return pos_ < params_size_;
3962 }
3963
3964 void Next() {
3965 ++pos_;
3966 }
3967
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07003968 const char* GetDescriptor() REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003969 return res_method_->GetTypeDescriptorFromTypeIdx(params_->GetTypeItem(pos_).type_idx_);
3970 }
3971
3972 private:
Mathieu Chartiere401d142015-04-22 13:56:20 -07003973 ArtMethod* res_method_;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003974 size_t pos_;
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08003975 const dex::TypeList* params_;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003976 const size_t params_size_;
3977};
3978
Mathieu Chartiere401d142015-04-22 13:56:20 -07003979ArtMethod* MethodVerifier::VerifyInvocationArgs(
Alex Light7268d472016-01-20 15:50:01 -08003980 const Instruction* inst, MethodType method_type, bool is_range) {
jeffhao8cd6dda2012-02-22 10:15:34 -08003981 // Resolve the method. This could be an abstract or concrete method depending on what sort of call
3982 // we're making.
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00003983 const uint32_t method_idx = GetMethodIdxOfInvoke(inst);
Alex Light7268d472016-01-20 15:50:01 -08003984 ArtMethod* res_method = ResolveMethodAndCheckAccess(method_idx, method_type);
Ian Rogers7b078e82014-09-10 14:44:24 -07003985 if (res_method == nullptr) { // error or class is unresolved
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003986 // Check what we can statically.
3987 if (!have_pending_hard_failure_) {
3988 VerifyInvocationArgsUnresolvedMethod(inst, method_type, is_range);
3989 }
3990 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003991 }
3992
Ian Rogersd81871c2011-10-03 13:57:23 -07003993 // If we're using invoke-super(method), make sure that the executing method's class' superclass
Alex Light705ad492015-09-21 11:36:30 -07003994 // has a vtable entry for the target method. Or the target is on a interface.
Alex Light7268d472016-01-20 15:50:01 -08003995 if (method_type == METHOD_SUPER) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08003996 dex::TypeIndex class_idx = dex_file_->GetMethodId(method_idx).class_idx_;
David Brazdilca3c8c32016-09-06 14:04:48 +01003997 const RegType& reference_type = reg_types_.FromDescriptor(
Andreas Gampe51de69e2019-04-19 15:14:14 -07003998 class_loader_.Get(),
David Brazdilca3c8c32016-09-06 14:04:48 +01003999 dex_file_->StringByTypeIdx(class_idx),
4000 false);
4001 if (reference_type.IsUnresolvedTypes()) {
Alex Lightfedd91d2016-01-07 14:49:16 -08004002 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "Unable to find referenced class from invoke-super";
4003 return nullptr;
4004 }
David Brazdilca3c8c32016-09-06 14:04:48 +01004005 if (reference_type.GetClass()->IsInterface()) {
Alex Lightfedd91d2016-01-07 14:49:16 -08004006 // TODO Can we verify anything else.
David Brazdil15fc7292016-09-02 14:13:18 +01004007 if (class_idx == class_def_.class_idx_) {
Alex Lightfedd91d2016-01-07 14:49:16 -08004008 Fail(VERIFY_ERROR_CLASS_CHANGE) << "Cannot invoke-super on self as interface";
Alex Light55ea94d2016-03-15 09:50:26 -07004009 return nullptr;
Alex Lightfedd91d2016-01-07 14:49:16 -08004010 }
4011 // TODO Revisit whether we want to allow invoke-super on direct interfaces only like the JLS
4012 // does.
Alex Light55ea94d2016-03-15 09:50:26 -07004013 if (!GetDeclaringClass().HasClass()) {
4014 Fail(VERIFY_ERROR_NO_CLASS) << "Unable to resolve the full class of 'this' used in an"
4015 << "interface invoke-super";
4016 return nullptr;
David Brazdilca3c8c32016-09-06 14:04:48 +01004017 } else if (!reference_type.IsStrictlyAssignableFrom(GetDeclaringClass(), this)) {
Alex Lightfedd91d2016-01-07 14:49:16 -08004018 Fail(VERIFY_ERROR_CLASS_CHANGE)
David Sehr709b0702016-10-13 09:12:37 -07004019 << "invoke-super in " << mirror::Class::PrettyClass(GetDeclaringClass().GetClass())
4020 << " in method "
4021 << dex_file_->PrettyMethod(dex_method_idx_) << " to method "
4022 << dex_file_->PrettyMethod(method_idx) << " references "
4023 << "non-super-interface type " << mirror::Class::PrettyClass(reference_type.GetClass());
Alex Lightfedd91d2016-01-07 14:49:16 -08004024 return nullptr;
Alex Light705ad492015-09-21 11:36:30 -07004025 }
4026 } else {
4027 const RegType& super = GetDeclaringClass().GetSuperClass(&reg_types_);
4028 if (super.IsUnresolvedTypes()) {
4029 Fail(VERIFY_ERROR_NO_METHOD) << "unknown super class in invoke-super from "
David Sehr709b0702016-10-13 09:12:37 -07004030 << dex_file_->PrettyMethod(dex_method_idx_)
4031 << " to super " << res_method->PrettyMethod();
Alex Light705ad492015-09-21 11:36:30 -07004032 return nullptr;
4033 }
David Brazdilca3c8c32016-09-06 14:04:48 +01004034 if (!reference_type.IsStrictlyAssignableFrom(GetDeclaringClass(), this) ||
Aart Bikf663e342016-04-04 17:28:59 -07004035 (res_method->GetMethodIndex() >= super.GetClass()->GetVTableLength())) {
Alex Light705ad492015-09-21 11:36:30 -07004036 Fail(VERIFY_ERROR_NO_METHOD) << "invalid invoke-super from "
David Sehr709b0702016-10-13 09:12:37 -07004037 << dex_file_->PrettyMethod(dex_method_idx_)
Alex Light705ad492015-09-21 11:36:30 -07004038 << " to super " << super
4039 << "." << res_method->GetName()
4040 << res_method->GetSignature();
4041 return nullptr;
4042 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004043 }
4044 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08004045
Andreas Gampe74979b12017-05-16 09:28:06 -07004046 if (UNLIKELY(method_type == METHOD_POLYMORPHIC)) {
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004047 // Process the signature of the calling site that is invoking the method handle.
Orion Hodson06d10a72018-05-14 08:53:38 +01004048 dex::ProtoIndex proto_idx(inst->VRegH());
4049 DexFileParameterIterator it(*dex_file_, dex_file_->GetProtoId(proto_idx));
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004050 return VerifyInvocationArgsFromIterator(&it, inst, method_type, is_range, res_method);
4051 } else {
4052 // Process the target method's signature.
4053 MethodParamListDescriptorIterator it(res_method);
4054 return VerifyInvocationArgsFromIterator(&it, inst, method_type, is_range, res_method);
4055 }
4056}
4057
4058bool MethodVerifier::CheckSignaturePolymorphicMethod(ArtMethod* method) {
Vladimir Markod93e3742018-07-18 10:58:13 +01004059 ObjPtr<mirror::Class> klass = method->GetDeclaringClass();
Orion Hodsonfe92d122018-01-02 10:45:17 +00004060 const char* method_name = method->GetName();
4061
4062 const char* expected_return_descriptor;
Vladimir Markoc7aa87e2018-05-24 15:19:52 +01004063 ObjPtr<mirror::ObjectArray<mirror::Class>> class_roots =
4064 Runtime::Current()->GetClassLinker()->GetClassRoots();
4065 if (klass == GetClassRoot<mirror::MethodHandle>(class_roots)) {
Orion Hodsonfe92d122018-01-02 10:45:17 +00004066 expected_return_descriptor = mirror::MethodHandle::GetReturnTypeDescriptor(method_name);
Vladimir Markoc7aa87e2018-05-24 15:19:52 +01004067 } else if (klass == GetClassRoot<mirror::VarHandle>(class_roots)) {
Orion Hodsonfe92d122018-01-02 10:45:17 +00004068 expected_return_descriptor = mirror::VarHandle::GetReturnTypeDescriptor(method_name);
4069 } else {
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004070 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
Orion Hodsonfe92d122018-01-02 10:45:17 +00004071 << "Signature polymorphic method in unsuppported class: " << klass->PrettyDescriptor();
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004072 return false;
4073 }
4074
Orion Hodsonfe92d122018-01-02 10:45:17 +00004075 if (expected_return_descriptor == nullptr) {
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004076 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
4077 << "Signature polymorphic method name invalid: " << method_name;
4078 return false;
4079 }
4080
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08004081 const dex::TypeList* types = method->GetParameterTypeList();
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004082 if (types->Size() != 1) {
4083 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
4084 << "Signature polymorphic method has too many arguments " << types->Size() << " != 1";
4085 return false;
4086 }
4087
4088 const dex::TypeIndex argument_type_index = types->GetTypeItem(0).type_idx_;
4089 const char* argument_descriptor = method->GetTypeDescriptorFromTypeIdx(argument_type_index);
4090 if (strcmp(argument_descriptor, "[Ljava/lang/Object;") != 0) {
4091 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
4092 << "Signature polymorphic method has unexpected argument type: " << argument_descriptor;
4093 return false;
4094 }
4095
4096 const char* return_descriptor = method->GetReturnTypeDescriptor();
Orion Hodsonfe92d122018-01-02 10:45:17 +00004097 if (strcmp(return_descriptor, expected_return_descriptor) != 0) {
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004098 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
Orion Hodsonfe92d122018-01-02 10:45:17 +00004099 << "Signature polymorphic method has unexpected return type: " << return_descriptor
4100 << " != " << expected_return_descriptor;
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004101 return false;
4102 }
4103
4104 return true;
4105}
4106
4107bool MethodVerifier::CheckSignaturePolymorphicReceiver(const Instruction* inst) {
4108 const RegType& this_type = work_line_->GetInvocationThis(this, inst);
Andreas Gampeeed3a5d2017-11-29 14:58:34 -08004109 if (this_type.IsZeroOrNull()) {
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004110 /* null pointer always passes (and always fails at run time) */
4111 return true;
4112 } else if (!this_type.IsNonZeroReferenceTypes()) {
4113 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
4114 << "invoke-polymorphic receiver is not a reference: "
4115 << this_type;
4116 return false;
4117 } else if (this_type.IsUninitializedReference()) {
4118 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
4119 << "invoke-polymorphic receiver is uninitialized: "
4120 << this_type;
4121 return false;
4122 } else if (!this_type.HasClass()) {
4123 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
4124 << "invoke-polymorphic receiver has no class: "
4125 << this_type;
4126 return false;
Vladimir Markoc7aa87e2018-05-24 15:19:52 +01004127 } else {
4128 ObjPtr<mirror::ObjectArray<mirror::Class>> class_roots =
4129 Runtime::Current()->GetClassLinker()->GetClassRoots();
4130 if (!this_type.GetClass()->IsSubClass(GetClassRoot<mirror::MethodHandle>(class_roots)) &&
4131 !this_type.GetClass()->IsSubClass(GetClassRoot<mirror::VarHandle>(class_roots))) {
4132 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
4133 << "invoke-polymorphic receiver is not a subclass of MethodHandle or VarHandle: "
4134 << this_type;
4135 return false;
4136 }
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004137 }
4138 return true;
Ian Rogersd81871c2011-10-03 13:57:23 -07004139}
4140
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00004141uint16_t MethodVerifier::GetMethodIdxOfInvoke(const Instruction* inst) {
4142 switch (inst->Opcode()) {
4143 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK:
4144 case Instruction::INVOKE_VIRTUAL_QUICK: {
4145 DCHECK(Runtime::Current()->IsStarted() || verify_to_dump_)
4146 << dex_file_->PrettyMethod(dex_method_idx_, true) << "@" << work_insn_idx_;
4147 DCHECK(method_being_verified_ != nullptr);
4148 uint16_t method_idx = method_being_verified_->GetIndexFromQuickening(work_insn_idx_);
4149 CHECK_NE(method_idx, DexFile::kDexNoIndex16);
4150 return method_idx;
4151 }
4152 default: {
4153 return inst->VRegB();
4154 }
Mathieu Chartier091d2382015-03-06 10:59:06 -08004155 }
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004156}
4157
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00004158uint16_t MethodVerifier::GetFieldIdxOfFieldAccess(const Instruction* inst, bool is_static) {
4159 if (is_static) {
4160 return inst->VRegB_21c();
4161 } else if (inst->IsQuickened()) {
4162 DCHECK(Runtime::Current()->IsStarted() || verify_to_dump_);
4163 DCHECK(method_being_verified_ != nullptr);
4164 uint16_t field_idx = method_being_verified_->GetIndexFromQuickening(work_insn_idx_);
4165 CHECK_NE(field_idx, DexFile::kDexNoIndex16);
4166 return field_idx;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004167 } else {
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00004168 return inst->VRegC_22c();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004169 }
4170}
4171
Ian Rogers62342ec2013-06-11 10:26:37 -07004172void MethodVerifier::VerifyNewArray(const Instruction* inst, bool is_filled, bool is_range) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08004173 dex::TypeIndex type_idx;
Sebastien Hertz5243e912013-05-21 10:55:07 +02004174 if (!is_filled) {
4175 DCHECK_EQ(inst->Opcode(), Instruction::NEW_ARRAY);
Andreas Gampea5b09a62016-11-17 15:21:22 -08004176 type_idx = dex::TypeIndex(inst->VRegC_22c());
Sebastien Hertz5243e912013-05-21 10:55:07 +02004177 } else if (!is_range) {
4178 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY);
Andreas Gampea5b09a62016-11-17 15:21:22 -08004179 type_idx = dex::TypeIndex(inst->VRegB_35c());
Sebastien Hertz5243e912013-05-21 10:55:07 +02004180 } else {
4181 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY_RANGE);
Andreas Gampea5b09a62016-11-17 15:21:22 -08004182 type_idx = dex::TypeIndex(inst->VRegB_3rc());
Sebastien Hertz5243e912013-05-21 10:55:07 +02004183 }
Andreas Gampe98be1a92017-08-28 08:25:45 -07004184 const RegType& res_type = ResolveClass<CheckAccess::kYes>(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004185 if (res_type.IsConflict()) { // bad class
4186 DCHECK_NE(failures_.size(), 0U);
Ian Rogers0c4a5062012-02-03 15:18:59 -08004187 } else {
4188 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
4189 if (!res_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07004190 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "new-array on non-array class " << res_type;
Ian Rogers0c4a5062012-02-03 15:18:59 -08004191 } else if (!is_filled) {
4192 /* make sure "size" register is valid type */
Ian Rogers7b078e82014-09-10 14:44:24 -07004193 work_line_->VerifyRegisterType(this, inst->VRegB_22c(), reg_types_.Integer());
Ian Rogers0c4a5062012-02-03 15:18:59 -08004194 /* set register type to array class */
Ian Rogersd8f69b02014-09-10 21:43:52 +00004195 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
Andreas Gampead238ce2015-08-24 21:13:08 -07004196 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_22c(), precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08004197 } else {
Andreas Gampebb18a032016-03-22 20:34:25 -07004198 DCHECK(!res_type.IsUnresolvedMergedReference());
Ian Rogers0c4a5062012-02-03 15:18:59 -08004199 // Verify each register. If "arg_count" is bad, VerifyRegisterType() will run off the end of
4200 // the list and fail. It's legal, if silly, for arg_count to be zero.
Andreas Gampe51de69e2019-04-19 15:14:14 -07004201 const RegType& expected_type = reg_types_.GetComponentType(res_type, class_loader_.Get());
Sebastien Hertz5243e912013-05-21 10:55:07 +02004202 uint32_t arg_count = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
4203 uint32_t arg[5];
4204 if (!is_range) {
Ian Rogers29a26482014-05-02 15:27:29 -07004205 inst->GetVarArgs(arg);
Sebastien Hertz5243e912013-05-21 10:55:07 +02004206 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08004207 for (size_t ui = 0; ui < arg_count; ui++) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02004208 uint32_t get_reg = is_range ? inst->VRegC_3rc() + ui : arg[ui];
Ian Rogers7b078e82014-09-10 14:44:24 -07004209 if (!work_line_->VerifyRegisterType(this, get_reg, expected_type)) {
4210 work_line_->SetResultRegisterType(this, reg_types_.Conflict());
Ian Rogers0c4a5062012-02-03 15:18:59 -08004211 return;
4212 }
4213 }
4214 // filled-array result goes into "result" register
Ian Rogersd8f69b02014-09-10 21:43:52 +00004215 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
Ian Rogers7b078e82014-09-10 14:44:24 -07004216 work_line_->SetResultRegisterType(this, precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08004217 }
4218 }
4219}
4220
Sebastien Hertz5243e912013-05-21 10:55:07 +02004221void MethodVerifier::VerifyAGet(const Instruction* inst,
Ian Rogersd8f69b02014-09-10 21:43:52 +00004222 const RegType& insn_type, bool is_primitive) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004223 const RegType& index_type = work_line_->GetRegisterType(this, inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07004224 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07004225 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07004226 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004227 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegB_23x());
Andreas Gampeeed3a5d2017-11-29 14:58:34 -08004228 if (array_type.IsZeroOrNull()) {
Ian Rogers89310de2012-02-01 13:47:30 -08004229 // Null array class; this code path will fail at runtime. Infer a merge-able type from the
Andreas Gampe52f205a2017-12-01 12:16:07 -08004230 // instruction type.
4231 if (!is_primitive) {
4232 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), reg_types_.Null());
4233 } else if (insn_type.IsInteger()) {
4234 // Pick a non-zero constant (to distinguish with null) that can fit in any primitive.
4235 // We cannot use 'insn_type' as it could be a float array or an int array.
4236 work_line_->SetRegisterType<LockOp::kClear>(
4237 this, inst->VRegA_23x(), DetermineCat1Constant(1, need_precise_constants_));
4238 } else if (insn_type.IsCategory1Types()) {
4239 // Category 1
4240 // The 'insn_type' is exactly the type we need.
4241 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), insn_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07004242 } else {
Ian Rogers89310de2012-02-01 13:47:30 -08004243 // Category 2
Ian Rogers7b078e82014-09-10 14:44:24 -07004244 work_line_->SetRegisterTypeWide(this, inst->VRegA_23x(),
4245 reg_types_.FromCat2ConstLo(0, false),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004246 reg_types_.FromCat2ConstHi(0, false));
Ian Rogers89310de2012-02-01 13:47:30 -08004247 }
jeffhaofc3144e2012-02-01 17:21:15 -08004248 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07004249 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aget";
Andreas Gampe8d8fc482016-03-25 16:24:20 -07004250 } else if (array_type.IsUnresolvedMergedReference()) {
Andreas Gampebb18a032016-03-22 20:34:25 -07004251 // Unresolved array types must be reference array types.
4252 if (is_primitive) {
4253 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type
4254 << " source for category 1 aget";
4255 } else {
4256 Fail(VERIFY_ERROR_NO_CLASS) << "cannot verify aget for " << array_type
4257 << " because of missing class";
Andreas Gampe8d8fc482016-03-25 16:24:20 -07004258 // Approximate with java.lang.Object[].
4259 work_line_->SetRegisterType<LockOp::kClear>(this,
4260 inst->VRegA_23x(),
4261 reg_types_.JavaLangObject(false));
Andreas Gampebb18a032016-03-22 20:34:25 -07004262 }
Ian Rogers89310de2012-02-01 13:47:30 -08004263 } else {
4264 /* verify the class */
Andreas Gampe51de69e2019-04-19 15:14:14 -07004265 const RegType& component_type = reg_types_.GetComponentType(array_type, class_loader_.Get());
jeffhaofc3144e2012-02-01 17:21:15 -08004266 if (!component_type.IsReferenceTypes() && !is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07004267 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08004268 << " source for aget-object";
4269 } else if (component_type.IsNonZeroReferenceTypes() && is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07004270 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08004271 << " source for category 1 aget";
4272 } else if (is_primitive && !insn_type.Equals(component_type) &&
4273 !((insn_type.IsInteger() && component_type.IsFloat()) ||
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004274 (insn_type.IsLong() && component_type.IsDouble()))) {
4275 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array type " << array_type
4276 << " incompatible with aget of type " << insn_type;
Ian Rogers89310de2012-02-01 13:47:30 -08004277 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004278 // Use knowledge of the field type which is stronger than the type inferred from the
4279 // instruction, which can't differentiate object types and ints from floats, longs from
4280 // doubles.
4281 if (!component_type.IsLowHalf()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07004282 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), component_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004283 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004284 work_line_->SetRegisterTypeWide(this, inst->VRegA_23x(), component_type,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004285 component_type.HighHalf(&reg_types_));
4286 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004287 }
4288 }
4289 }
4290}
4291
Ian Rogersd8f69b02014-09-10 21:43:52 +00004292void MethodVerifier::VerifyPrimitivePut(const RegType& target_type, const RegType& insn_type,
Jeff Haofe1f7c82013-08-01 14:50:24 -07004293 const uint32_t vregA) {
4294 // Primitive assignability rules are weaker than regular assignability rules.
4295 bool instruction_compatible;
4296 bool value_compatible;
Ian Rogers7b078e82014-09-10 14:44:24 -07004297 const RegType& value_type = work_line_->GetRegisterType(this, vregA);
Jeff Haofe1f7c82013-08-01 14:50:24 -07004298 if (target_type.IsIntegralTypes()) {
Jeff Haoa4647482013-08-06 15:35:47 -07004299 instruction_compatible = target_type.Equals(insn_type);
Jeff Haofe1f7c82013-08-01 14:50:24 -07004300 value_compatible = value_type.IsIntegralTypes();
4301 } else if (target_type.IsFloat()) {
4302 instruction_compatible = insn_type.IsInteger(); // no put-float, so expect put-int
4303 value_compatible = value_type.IsFloatTypes();
4304 } else if (target_type.IsLong()) {
4305 instruction_compatible = insn_type.IsLong();
Andreas Gampe376fa682014-09-07 13:06:12 -07004306 // Additional register check: this is not checked statically (as part of VerifyInstructions),
4307 // as target_type depends on the resolved type of the field.
4308 if (instruction_compatible && work_line_->NumRegs() > vregA + 1) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004309 const RegType& value_type_hi = work_line_->GetRegisterType(this, vregA + 1);
Andreas Gampe376fa682014-09-07 13:06:12 -07004310 value_compatible = value_type.IsLongTypes() && value_type.CheckWidePair(value_type_hi);
4311 } else {
4312 value_compatible = false;
4313 }
Jeff Haofe1f7c82013-08-01 14:50:24 -07004314 } else if (target_type.IsDouble()) {
4315 instruction_compatible = insn_type.IsLong(); // no put-double, so expect put-long
Andreas Gampe376fa682014-09-07 13:06:12 -07004316 // Additional register check: this is not checked statically (as part of VerifyInstructions),
4317 // as target_type depends on the resolved type of the field.
4318 if (instruction_compatible && work_line_->NumRegs() > vregA + 1) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004319 const RegType& value_type_hi = work_line_->GetRegisterType(this, vregA + 1);
Andreas Gampe376fa682014-09-07 13:06:12 -07004320 value_compatible = value_type.IsDoubleTypes() && value_type.CheckWidePair(value_type_hi);
4321 } else {
4322 value_compatible = false;
4323 }
Jeff Haofe1f7c82013-08-01 14:50:24 -07004324 } else {
4325 instruction_compatible = false; // reference with primitive store
4326 value_compatible = false; // unused
4327 }
4328 if (!instruction_compatible) {
4329 // This is a global failure rather than a class change failure as the instructions and
4330 // the descriptors for the type should have been consistent within the same file at
4331 // compile time.
4332 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "put insn has type '" << insn_type
4333 << "' but expected type '" << target_type << "'";
4334 return;
4335 }
4336 if (!value_compatible) {
4337 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
4338 << " of type " << value_type << " but expected " << target_type << " for put";
4339 return;
4340 }
4341}
4342
Sebastien Hertz5243e912013-05-21 10:55:07 +02004343void MethodVerifier::VerifyAPut(const Instruction* inst,
Ian Rogersd8f69b02014-09-10 21:43:52 +00004344 const RegType& insn_type, bool is_primitive) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004345 const RegType& index_type = work_line_->GetRegisterType(this, inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07004346 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07004347 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07004348 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004349 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegB_23x());
Andreas Gampeeed3a5d2017-11-29 14:58:34 -08004350 if (array_type.IsZeroOrNull()) {
Nicolas Geoffray66389fb2015-06-19 10:35:42 +01004351 // Null array type; this code path will fail at runtime.
4352 // Still check that the given value matches the instruction's type.
Andreas Gampe4bf4c782015-08-14 14:07:43 -07004353 // Note: this is, as usual, complicated by the fact the the instruction isn't fully typed
4354 // and fits multiple register types.
4355 const RegType* modified_reg_type = &insn_type;
4356 if ((modified_reg_type == &reg_types_.Integer()) ||
4357 (modified_reg_type == &reg_types_.LongLo())) {
4358 // May be integer or float | long or double. Overwrite insn_type accordingly.
4359 const RegType& value_type = work_line_->GetRegisterType(this, inst->VRegA_23x());
4360 if (modified_reg_type == &reg_types_.Integer()) {
4361 if (&value_type == &reg_types_.Float()) {
4362 modified_reg_type = &value_type;
4363 }
4364 } else {
4365 if (&value_type == &reg_types_.DoubleLo()) {
4366 modified_reg_type = &value_type;
4367 }
4368 }
4369 }
4370 work_line_->VerifyRegisterType(this, inst->VRegA_23x(), *modified_reg_type);
jeffhaofc3144e2012-02-01 17:21:15 -08004371 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07004372 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aput";
Andreas Gampe8d8fc482016-03-25 16:24:20 -07004373 } else if (array_type.IsUnresolvedMergedReference()) {
Andreas Gampebb18a032016-03-22 20:34:25 -07004374 // Unresolved array types must be reference array types.
4375 if (is_primitive) {
4376 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "put insn has type '" << insn_type
4377 << "' but unresolved type '" << array_type << "'";
4378 } else {
4379 Fail(VERIFY_ERROR_NO_CLASS) << "cannot verify aput for " << array_type
4380 << " because of missing class";
4381 }
Ian Rogers89310de2012-02-01 13:47:30 -08004382 } else {
Andreas Gampe51de69e2019-04-19 15:14:14 -07004383 const RegType& component_type = reg_types_.GetComponentType(array_type, class_loader_.Get());
Jeff Haofe1f7c82013-08-01 14:50:24 -07004384 const uint32_t vregA = inst->VRegA_23x();
Jeff Haob24b4a72013-07-31 13:47:31 -07004385 if (is_primitive) {
Jeff Haofe1f7c82013-08-01 14:50:24 -07004386 VerifyPrimitivePut(component_type, insn_type, vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07004387 } else {
Jeff Haob24b4a72013-07-31 13:47:31 -07004388 if (!component_type.IsReferenceTypes()) {
4389 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
4390 << " source for aput-object";
4391 } else {
4392 // The instruction agrees with the type of array, confirm the value to be stored does too
4393 // Note: we use the instruction type (rather than the component type) for aput-object as
4394 // incompatible classes will be caught at runtime as an array store exception
Ian Rogers7b078e82014-09-10 14:44:24 -07004395 work_line_->VerifyRegisterType(this, vregA, insn_type);
Jeff Haob24b4a72013-07-31 13:47:31 -07004396 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004397 }
4398 }
4399 }
4400}
4401
Mathieu Chartierc7853442015-03-27 14:35:38 -07004402ArtField* MethodVerifier::GetStaticField(int field_idx) {
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08004403 const dex::FieldId& field_id = dex_file_->GetFieldId(field_idx);
Ian Rogers90040192011-12-16 08:54:29 -08004404 // Check access to class
Andreas Gampe98be1a92017-08-28 08:25:45 -07004405 const RegType& klass_type = ResolveClass<CheckAccess::kYes>(field_id.class_idx_);
Brian Carlstrom7934ac22013-07-26 10:54:15 -07004406 if (klass_type.IsConflict()) { // bad class
Ian Rogersad0b3a32012-04-16 14:50:24 -07004407 AppendToLastFailMessage(StringPrintf(" in attempt to access static field %d (%s) in %s",
4408 field_idx, dex_file_->GetFieldName(field_id),
4409 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers7b078e82014-09-10 14:44:24 -07004410 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08004411 }
Elliott Hughesb25c3f62012-03-26 16:35:06 -07004412 if (klass_type.IsUnresolvedTypes()) {
Andreas Gampe629be512017-08-25 17:09:32 -07004413 // Accessibility checks depend on resolved fields.
David Brazdil2bb2fbd2018-11-13 18:24:26 +00004414 DCHECK(klass_type.Equals(GetDeclaringClass()) ||
4415 !failures_.empty() ||
4416 IsSdkVersionSetAndLessThan(api_level_, SdkVersion::kP));
Andreas Gampe629be512017-08-25 17:09:32 -07004417
Ian Rogers7b078e82014-09-10 14:44:24 -07004418 return nullptr; // Can't resolve Class so no more to do here, will do checking at runtime.
Ian Rogers90040192011-12-16 08:54:29 -08004419 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07004420 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Vladimir Markoe11dd502017-12-08 14:09:45 +00004421 ArtField* field = class_linker->ResolveFieldJLS(field_idx, dex_cache_, class_loader_);
David Brazdilca3c8c32016-09-06 14:04:48 +01004422
4423 // Record result of the field resolution attempt.
4424 VerifierDeps::MaybeRecordFieldResolution(*dex_file_, field_idx, field);
4425
Ian Rogers7b078e82014-09-10 14:44:24 -07004426 if (field == nullptr) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07004427 VLOG(verifier) << "Unable to resolve static field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07004428 << dex_file_->GetFieldName(field_id) << ") in "
4429 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogers7b078e82014-09-10 14:44:24 -07004430 DCHECK(self_->IsExceptionPending());
4431 self_->ClearException();
4432 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004433 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
4434 field->GetAccessFlags())) {
David Sehr709b0702016-10-13 09:12:37 -07004435 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access static field " << field->PrettyField()
Ian Rogersad0b3a32012-04-16 14:50:24 -07004436 << " from " << GetDeclaringClass();
Ian Rogers7b078e82014-09-10 14:44:24 -07004437 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07004438 } else if (!field->IsStatic()) {
David Sehr709b0702016-10-13 09:12:37 -07004439 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << field->PrettyField() << " to be static";
Ian Rogers7b078e82014-09-10 14:44:24 -07004440 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07004441 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07004442 return field;
Ian Rogersd81871c2011-10-03 13:57:23 -07004443}
4444
Mathieu Chartierc7853442015-03-27 14:35:38 -07004445ArtField* MethodVerifier::GetInstanceField(const RegType& obj_type, int field_idx) {
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08004446 const dex::FieldId& field_id = dex_file_->GetFieldId(field_idx);
Aart Bik31883642016-06-06 15:02:44 -07004447 // Check access to class.
Andreas Gampe98be1a92017-08-28 08:25:45 -07004448 const RegType& klass_type = ResolveClass<CheckAccess::kYes>(field_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004449 if (klass_type.IsConflict()) {
4450 AppendToLastFailMessage(StringPrintf(" in attempt to access instance field %d (%s) in %s",
4451 field_idx, dex_file_->GetFieldName(field_id),
4452 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers7b078e82014-09-10 14:44:24 -07004453 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08004454 }
jeffhao8cd6dda2012-02-22 10:15:34 -08004455 if (klass_type.IsUnresolvedTypes()) {
Andreas Gampe629be512017-08-25 17:09:32 -07004456 // Accessibility checks depend on resolved fields.
David Brazdil2bb2fbd2018-11-13 18:24:26 +00004457 DCHECK(klass_type.Equals(GetDeclaringClass()) ||
4458 !failures_.empty() ||
4459 IsSdkVersionSetAndLessThan(api_level_, SdkVersion::kP));
Andreas Gampe629be512017-08-25 17:09:32 -07004460
Ian Rogers7b078e82014-09-10 14:44:24 -07004461 return nullptr; // Can't resolve Class so no more to do here
Ian Rogers90040192011-12-16 08:54:29 -08004462 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07004463 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Vladimir Markoe11dd502017-12-08 14:09:45 +00004464 ArtField* field = class_linker->ResolveFieldJLS(field_idx, dex_cache_, class_loader_);
David Brazdilca3c8c32016-09-06 14:04:48 +01004465
4466 // Record result of the field resolution attempt.
4467 VerifierDeps::MaybeRecordFieldResolution(*dex_file_, field_idx, field);
4468
Ian Rogers7b078e82014-09-10 14:44:24 -07004469 if (field == nullptr) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07004470 VLOG(verifier) << "Unable to resolve instance field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07004471 << dex_file_->GetFieldName(field_id) << ") in "
4472 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogers7b078e82014-09-10 14:44:24 -07004473 DCHECK(self_->IsExceptionPending());
4474 self_->ClearException();
4475 return nullptr;
Andreas Gampeeed3a5d2017-11-29 14:58:34 -08004476 } else if (obj_type.IsZeroOrNull()) {
Aart Bik31883642016-06-06 15:02:44 -07004477 // Cannot infer and check type, however, access will cause null pointer exception.
4478 // Fall through into a few last soft failure checks below.
Stephen Kyle695c5982014-08-22 15:03:07 +01004479 } else if (!obj_type.IsReferenceTypes()) {
Aart Bik31883642016-06-06 15:02:44 -07004480 // Trying to read a field from something that isn't a reference.
Stephen Kyle695c5982014-08-22 15:03:07 +01004481 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance field access on object that has "
4482 << "non-reference type " << obj_type;
Ian Rogers7b078e82014-09-10 14:44:24 -07004483 return nullptr;
Ian Rogerse1758fe2012-04-19 11:31:15 -07004484 } else {
David Brazdil0d638bb2016-07-27 15:29:25 +01004485 std::string temp;
Mathieu Chartier3398c782016-09-30 10:27:43 -07004486 ObjPtr<mirror::Class> klass = field->GetDeclaringClass();
Ian Rogersd8f69b02014-09-10 21:43:52 +00004487 const RegType& field_klass =
Vladimir Markobcf17522018-06-01 13:14:32 +01004488 FromClass(klass->GetDescriptor(&temp), klass, klass->CannotBeAssignedFromOtherTypes());
David Brazdil68b5c0b2016-01-19 14:25:29 +00004489 if (obj_type.IsUninitializedTypes()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07004490 // Field accesses through uninitialized references are only allowable for constructors where
David Brazdil68b5c0b2016-01-19 14:25:29 +00004491 // the field is declared in this class.
4492 // Note: this IsConstructor check is technically redundant, as UninitializedThis should only
4493 // appear in constructors.
4494 if (!obj_type.IsUninitializedThisReference() ||
4495 !IsConstructor() ||
4496 !field_klass.Equals(GetDeclaringClass())) {
David Sehr709b0702016-10-13 09:12:37 -07004497 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "cannot access instance field " << field->PrettyField()
David Brazdil68b5c0b2016-01-19 14:25:29 +00004498 << " of a not fully initialized object within the context"
David Sehr709b0702016-10-13 09:12:37 -07004499 << " of " << dex_file_->PrettyMethod(dex_method_idx_);
David Brazdil68b5c0b2016-01-19 14:25:29 +00004500 return nullptr;
4501 }
David Brazdilca3c8c32016-09-06 14:04:48 +01004502 } else if (!field_klass.IsAssignableFrom(obj_type, this)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07004503 // Trying to access C1.field1 using reference of type C2, which is neither C1 or a sub-class
4504 // of C1. For resolution to occur the declared class of the field must be compatible with
4505 // obj_type, we've discovered this wasn't so, so report the field didn't exist.
Andreas Gampe66596242016-04-14 10:55:04 -07004506 VerifyError type;
4507 bool is_aot = Runtime::Current()->IsAotCompiler();
4508 if (is_aot && (field_klass.IsUnresolvedTypes() || obj_type.IsUnresolvedTypes())) {
4509 // Compiler & unresolved types involved, retry at runtime.
4510 type = VerifyError::VERIFY_ERROR_NO_CLASS;
4511 } else {
Andreas Gampe8f4ade02016-04-15 10:09:16 -07004512 // Classes known (resolved; and thus assignability check is precise), or we are at runtime
4513 // and still missing classes. This is a hard failure.
Andreas Gampe66596242016-04-14 10:55:04 -07004514 type = VerifyError::VERIFY_ERROR_BAD_CLASS_HARD;
4515 }
David Sehr709b0702016-10-13 09:12:37 -07004516 Fail(type) << "cannot access instance field " << field->PrettyField()
Andreas Gampe66596242016-04-14 10:55:04 -07004517 << " from object of type " << obj_type;
Ian Rogers7b078e82014-09-10 14:44:24 -07004518 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004519 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004520 }
Aart Bik31883642016-06-06 15:02:44 -07004521
4522 // Few last soft failure checks.
4523 if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
4524 field->GetAccessFlags())) {
David Sehr709b0702016-10-13 09:12:37 -07004525 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access instance field " << field->PrettyField()
Aart Bik31883642016-06-06 15:02:44 -07004526 << " from " << GetDeclaringClass();
4527 return nullptr;
4528 } else if (field->IsStatic()) {
David Sehr709b0702016-10-13 09:12:37 -07004529 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << field->PrettyField()
Aart Bik31883642016-06-06 15:02:44 -07004530 << " to not be static";
4531 return nullptr;
4532 }
4533
4534 return field;
Ian Rogersd81871c2011-10-03 13:57:23 -07004535}
4536
Andreas Gampe896df402014-10-20 22:25:29 -07004537template <MethodVerifier::FieldAccessType kAccType>
4538void MethodVerifier::VerifyISFieldAccess(const Instruction* inst, const RegType& insn_type,
4539 bool is_primitive, bool is_static) {
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00004540 uint32_t field_idx = GetFieldIdxOfFieldAccess(inst, is_static);
Mathieu Chartierc7853442015-03-27 14:35:38 -07004541 ArtField* field;
Ian Rogersb94a27b2011-10-26 00:33:41 -07004542 if (is_static) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07004543 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07004544 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004545 const RegType& object_type = work_line_->GetRegisterType(this, inst->VRegB_22c());
David Brazdil68b5c0b2016-01-19 14:25:29 +00004546
4547 // One is not allowed to access fields on uninitialized references, except to write to
4548 // fields in the constructor (before calling another constructor).
4549 // GetInstanceField does an assignability check which will fail for uninitialized types.
4550 // We thus modify the type if the uninitialized reference is a "this" reference (this also
4551 // checks at the same time that we're verifying a constructor).
4552 bool should_adjust = (kAccType == FieldAccessType::kAccPut) &&
4553 object_type.IsUninitializedThisReference();
4554 const RegType& adjusted_type = should_adjust
4555 ? GetRegTypeCache()->FromUninitialized(object_type)
4556 : object_type;
4557 field = GetInstanceField(adjusted_type, field_idx);
Andreas Gampe896df402014-10-20 22:25:29 -07004558 if (UNLIKELY(have_pending_hard_failure_)) {
4559 return;
4560 }
Alex Light4a2c8fc2016-02-12 11:01:54 -08004561 if (should_adjust) {
4562 if (field == nullptr) {
4563 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "Might be accessing a superclass instance field prior "
4564 << "to the superclass being initialized in "
David Sehr709b0702016-10-13 09:12:37 -07004565 << dex_file_->PrettyMethod(dex_method_idx_);
Alex Light4a2c8fc2016-02-12 11:01:54 -08004566 } else if (field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
4567 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "cannot access superclass instance field "
David Sehr709b0702016-10-13 09:12:37 -07004568 << field->PrettyField() << " of a not fully initialized "
Alex Light4a2c8fc2016-02-12 11:01:54 -08004569 << "object within the context of "
David Sehr709b0702016-10-13 09:12:37 -07004570 << dex_file_->PrettyMethod(dex_method_idx_);
Alex Light4a2c8fc2016-02-12 11:01:54 -08004571 return;
4572 }
4573 }
Ian Rogersb94a27b2011-10-26 00:33:41 -07004574 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00004575 const RegType* field_type = nullptr;
Ian Rogers7b078e82014-09-10 14:44:24 -07004576 if (field != nullptr) {
Andreas Gampe896df402014-10-20 22:25:29 -07004577 if (kAccType == FieldAccessType::kAccPut) {
4578 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
David Sehr709b0702016-10-13 09:12:37 -07004579 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << field->PrettyField()
Andreas Gampe896df402014-10-20 22:25:29 -07004580 << " from other class " << GetDeclaringClass();
Aart Bikc2bc2652016-05-23 14:58:49 -07004581 // Keep hunting for possible hard fails.
Andreas Gampe896df402014-10-20 22:25:29 -07004582 }
4583 }
4584
Mathieu Chartier3398c782016-09-30 10:27:43 -07004585 ObjPtr<mirror::Class> field_type_class =
Vladimir Marko208f6702017-12-08 12:00:50 +00004586 can_load_classes_ ? field->ResolveType() : field->LookupResolvedType();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004587 if (field_type_class != nullptr) {
Mathieu Chartier3398c782016-09-30 10:27:43 -07004588 field_type = &FromClass(field->GetTypeDescriptor(),
Vladimir Markobcf17522018-06-01 13:14:32 +01004589 field_type_class,
Andreas Gampef23f33d2015-06-23 14:18:17 -07004590 field_type_class->CannotBeAssignedFromOtherTypes());
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08004591 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004592 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
4593 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004594 }
David Brazdil2bb2fbd2018-11-13 18:24:26 +00004595 } else if (IsSdkVersionSetAndAtLeast(api_level_, SdkVersion::kP)) {
Andreas Gampe7da4c402017-08-25 11:30:48 -07004596 // If we don't have the field (it seems we failed resolution) and this is a PUT, we need to
4597 // redo verification at runtime as the field may be final, unless the field id shows it's in
4598 // the same class.
4599 //
4600 // For simplicity, it is OK to not distinguish compile-time vs runtime, and post this an
4601 // ACCESS_FIELD failure at runtime. This has the same effect as NO_FIELD - punting the class
4602 // to the access-checks interpreter.
4603 //
4604 // Note: see b/34966607. This and above may be changed in the future.
4605 if (kAccType == FieldAccessType::kAccPut) {
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08004606 const dex::FieldId& field_id = dex_file_->GetFieldId(field_idx);
Andreas Gampe7da4c402017-08-25 11:30:48 -07004607 const char* field_class_descriptor = dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Andreas Gampe51de69e2019-04-19 15:14:14 -07004608 const RegType* field_class_type = &reg_types_.FromDescriptor(class_loader_.Get(),
Andreas Gampe7da4c402017-08-25 11:30:48 -07004609 field_class_descriptor,
4610 false);
4611 if (!field_class_type->Equals(GetDeclaringClass())) {
4612 Fail(VERIFY_ERROR_ACCESS_FIELD) << "could not check field put for final field modify of "
4613 << field_class_descriptor
4614 << "."
4615 << dex_file_->GetFieldName(field_id)
4616 << " from other class "
4617 << GetDeclaringClass();
4618 }
4619 }
Ian Rogers0d604842012-04-16 14:50:24 -07004620 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004621 if (field_type == nullptr) {
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08004622 const dex::FieldId& field_id = dex_file_->GetFieldId(field_idx);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004623 const char* descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
Andreas Gampe51de69e2019-04-19 15:14:14 -07004624 field_type = &reg_types_.FromDescriptor(class_loader_.Get(), descriptor, false);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004625 }
Sebastien Hertz757b3042014-03-28 14:34:28 +01004626 DCHECK(field_type != nullptr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02004627 const uint32_t vregA = (is_static) ? inst->VRegA_21c() : inst->VRegA_22c();
Andreas Gampe896df402014-10-20 22:25:29 -07004628 static_assert(kAccType == FieldAccessType::kAccPut || kAccType == FieldAccessType::kAccGet,
4629 "Unexpected third access type");
4630 if (kAccType == FieldAccessType::kAccPut) {
4631 // sput or iput.
4632 if (is_primitive) {
4633 VerifyPrimitivePut(*field_type, insn_type, vregA);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004634 } else {
David Brazdilca3c8c32016-09-06 14:04:48 +01004635 if (!insn_type.IsAssignableFrom(*field_type, this)) {
Vladimir Marko414000e2015-06-23 17:45:21 +01004636 // If the field type is not a reference, this is a global failure rather than
4637 // a class change failure as the instructions and the descriptors for the type
4638 // should have been consistent within the same file at compile time.
4639 VerifyError error = field_type->IsReferenceTypes() ? VERIFY_ERROR_BAD_CLASS_SOFT
4640 : VERIFY_ERROR_BAD_CLASS_HARD;
David Sehr709b0702016-10-13 09:12:37 -07004641 Fail(error) << "expected field " << ArtField::PrettyField(field)
Vladimir Marko414000e2015-06-23 17:45:21 +01004642 << " to be compatible with type '" << insn_type
4643 << "' but found type '" << *field_type
4644 << "' in put-object";
Andreas Gampe896df402014-10-20 22:25:29 -07004645 return;
4646 }
4647 work_line_->VerifyRegisterType(this, vregA, *field_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004648 }
Andreas Gampe896df402014-10-20 22:25:29 -07004649 } else if (kAccType == FieldAccessType::kAccGet) {
4650 // sget or iget.
4651 if (is_primitive) {
4652 if (field_type->Equals(insn_type) ||
4653 (field_type->IsFloat() && insn_type.IsInteger()) ||
4654 (field_type->IsDouble() && insn_type.IsLong())) {
4655 // expected that read is of the correct primitive type or that int reads are reading
4656 // floats or long reads are reading doubles
4657 } else {
4658 // This is a global failure rather than a class change failure as the instructions and
4659 // the descriptors for the type should have been consistent within the same file at
4660 // compile time
David Sehr709b0702016-10-13 09:12:37 -07004661 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << ArtField::PrettyField(field)
Andreas Gampe896df402014-10-20 22:25:29 -07004662 << " to be of type '" << insn_type
4663 << "' but found type '" << *field_type << "' in get";
4664 return;
4665 }
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08004666 } else {
David Brazdilca3c8c32016-09-06 14:04:48 +01004667 if (!insn_type.IsAssignableFrom(*field_type, this)) {
Vladimir Marko414000e2015-06-23 17:45:21 +01004668 // If the field type is not a reference, this is a global failure rather than
4669 // a class change failure as the instructions and the descriptors for the type
4670 // should have been consistent within the same file at compile time.
4671 VerifyError error = field_type->IsReferenceTypes() ? VERIFY_ERROR_BAD_CLASS_SOFT
4672 : VERIFY_ERROR_BAD_CLASS_HARD;
David Sehr709b0702016-10-13 09:12:37 -07004673 Fail(error) << "expected field " << ArtField::PrettyField(field)
Vladimir Marko414000e2015-06-23 17:45:21 +01004674 << " to be compatible with type '" << insn_type
4675 << "' but found type '" << *field_type
4676 << "' in get-object";
Andreas Gampe890da292015-07-06 17:20:18 -07004677 if (error != VERIFY_ERROR_BAD_CLASS_HARD) {
Andreas Gampead238ce2015-08-24 21:13:08 -07004678 work_line_->SetRegisterType<LockOp::kClear>(this, vregA, reg_types_.Conflict());
Andreas Gampe890da292015-07-06 17:20:18 -07004679 }
Andreas Gampe896df402014-10-20 22:25:29 -07004680 return;
4681 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004682 }
Andreas Gampe896df402014-10-20 22:25:29 -07004683 if (!field_type->IsLowHalf()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07004684 work_line_->SetRegisterType<LockOp::kClear>(this, vregA, *field_type);
Andreas Gampe896df402014-10-20 22:25:29 -07004685 } else {
4686 work_line_->SetRegisterTypeWide(this, vregA, *field_type, field_type->HighHalf(&reg_types_));
4687 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07004688 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004689 LOG(FATAL) << "Unexpected case.";
Ian Rogersd81871c2011-10-03 13:57:23 -07004690 }
4691}
4692
Ian Rogers776ac1f2012-04-13 23:36:36 -07004693bool MethodVerifier::CheckNotMoveException(const uint16_t* insns, int insn_idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004694 if ((insns[insn_idx] & 0xff) == Instruction::MOVE_EXCEPTION) {
jeffhaod5347e02012-03-22 17:25:05 -07004695 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-exception";
Ian Rogersd81871c2011-10-03 13:57:23 -07004696 return false;
4697 }
4698 return true;
4699}
4700
Stephen Kyle9bc61992014-09-22 13:53:15 +01004701bool MethodVerifier::CheckNotMoveResult(const uint16_t* insns, int insn_idx) {
4702 if (((insns[insn_idx] & 0xff) >= Instruction::MOVE_RESULT) &&
4703 ((insns[insn_idx] & 0xff) <= Instruction::MOVE_RESULT_OBJECT)) {
4704 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-result*";
4705 return false;
4706 }
4707 return true;
4708}
4709
4710bool MethodVerifier::CheckNotMoveExceptionOrMoveResult(const uint16_t* insns, int insn_idx) {
4711 return (CheckNotMoveException(insns, insn_idx) && CheckNotMoveResult(insns, insn_idx));
4712}
4713
Ian Rogersebbdd872014-07-07 23:53:08 -07004714bool MethodVerifier::UpdateRegisters(uint32_t next_insn, RegisterLine* merge_line,
4715 bool update_merge_line) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004716 bool changed = true;
4717 RegisterLine* target_line = reg_table_.GetLine(next_insn);
Mathieu Chartierde40d472015-10-15 17:47:48 -07004718 if (!GetInstructionFlags(next_insn).IsVisitedOrChanged()) {
jeffhaobdb76512011-09-07 11:43:16 -07004719 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07004720 * We haven't processed this instruction before, and we haven't touched the registers here, so
4721 * there's nothing to "merge". Copy the registers over and mark it as changed. (This is the
4722 * only way a register can transition out of "unknown", so this is not just an optimization.)
jeffhaobdb76512011-09-07 11:43:16 -07004723 */
Andreas Gampea727e372015-08-25 09:22:37 -07004724 target_line->CopyFromLine(merge_line);
Mathieu Chartierde40d472015-10-15 17:47:48 -07004725 if (GetInstructionFlags(next_insn).IsReturn()) {
Jeff Haob24b4a72013-07-31 13:47:31 -07004726 // Verify that the monitor stack is empty on return.
Andreas Gampea727e372015-08-25 09:22:37 -07004727 merge_line->VerifyMonitorStackEmpty(this);
4728
Ian Rogersb8c78592013-07-25 23:52:52 +00004729 // For returns we only care about the operand to the return, all other registers are dead.
4730 // Initialize them as conflicts so they don't add to GC and deoptimization information.
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08004731 const Instruction* ret_inst = &code_item_accessor_.InstructionAt(next_insn);
Andreas Gampea727e372015-08-25 09:22:37 -07004732 AdjustReturnLine(this, ret_inst, target_line);
Aart Bik31883642016-06-06 15:02:44 -07004733 // Directly bail if a hard failure was found.
Aart Bikb0526322016-06-01 14:06:00 -07004734 if (have_pending_hard_failure_) {
4735 return false;
4736 }
Ian Rogersb8c78592013-07-25 23:52:52 +00004737 }
jeffhaobdb76512011-09-07 11:43:16 -07004738 } else {
Mathieu Chartier361e04a2016-02-16 14:06:35 -08004739 RegisterLineArenaUniquePtr copy;
Andreas Gampe92d77202017-12-06 20:49:00 -08004740 if (UNLIKELY(VLOG_IS_ON(verifier_debug))) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07004741 copy.reset(RegisterLine::Create(target_line->NumRegs(), this));
Ian Rogers7b0c5b42012-02-16 15:29:07 -08004742 copy->CopyFromLine(target_line);
4743 }
Ian Rogers7b078e82014-09-10 14:44:24 -07004744 changed = target_line->MergeRegisters(this, merge_line);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004745 if (have_pending_hard_failure_) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004746 return false;
jeffhaobdb76512011-09-07 11:43:16 -07004747 }
Andreas Gampe92d77202017-12-06 20:49:00 -08004748 if (UNLIKELY(VLOG_IS_ON(verifier_debug)) && changed) {
Elliott Hughes398f64b2012-03-26 18:05:48 -07004749 LogVerifyInfo() << "Merging at [" << reinterpret_cast<void*>(work_insn_idx_) << "]"
Elliott Hughesc073b072012-05-24 19:29:17 -07004750 << " to [" << reinterpret_cast<void*>(next_insn) << "]: " << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07004751 << copy->Dump(this) << " MERGE\n"
4752 << merge_line->Dump(this) << " ==\n"
Andreas Gampe2ad6cce2019-04-11 16:17:39 -07004753 << target_line->Dump(this);
jeffhaobdb76512011-09-07 11:43:16 -07004754 }
Ian Rogersebbdd872014-07-07 23:53:08 -07004755 if (update_merge_line && changed) {
4756 merge_line->CopyFromLine(target_line);
4757 }
jeffhaobdb76512011-09-07 11:43:16 -07004758 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004759 if (changed) {
Andreas Gampe51de69e2019-04-19 15:14:14 -07004760 GetModifiableInstructionFlags(next_insn).SetChanged();
jeffhaobdb76512011-09-07 11:43:16 -07004761 }
4762 return true;
4763}
4764
Ian Rogers7b3ddd22013-02-21 15:19:52 -08004765InstructionFlags* MethodVerifier::CurrentInsnFlags() {
Andreas Gampe51de69e2019-04-19 15:14:14 -07004766 return &GetModifiableInstructionFlags(work_insn_idx_);
Ian Rogers776ac1f2012-04-13 23:36:36 -07004767}
4768
Ian Rogersd8f69b02014-09-10 21:43:52 +00004769const RegType& MethodVerifier::GetMethodReturnType() {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004770 if (return_type_ == nullptr) {
Nicolas Geoffrayb041a402017-11-13 15:16:22 +00004771 if (method_being_verified_ != nullptr) {
Vladimir Markob45528c2017-07-27 14:14:28 +01004772 ObjPtr<mirror::Class> return_type_class = can_load_classes_
Nicolas Geoffrayb041a402017-11-13 15:16:22 +00004773 ? method_being_verified_->ResolveReturnType()
4774 : method_being_verified_->LookupResolvedReturnType();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004775 if (return_type_class != nullptr) {
Nicolas Geoffrayb041a402017-11-13 15:16:22 +00004776 return_type_ = &FromClass(method_being_verified_->GetReturnTypeDescriptor(),
Vladimir Markobcf17522018-06-01 13:14:32 +01004777 return_type_class,
Andreas Gampef23f33d2015-06-23 14:18:17 -07004778 return_type_class->CannotBeAssignedFromOtherTypes());
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004779 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004780 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
4781 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004782 }
4783 }
4784 if (return_type_ == nullptr) {
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08004785 const dex::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
4786 const dex::ProtoId& proto_id = dex_file_->GetMethodPrototype(method_id);
Andreas Gampea5b09a62016-11-17 15:21:22 -08004787 dex::TypeIndex return_type_idx = proto_id.return_type_idx_;
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004788 const char* descriptor = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(return_type_idx));
Andreas Gampe51de69e2019-04-19 15:14:14 -07004789 return_type_ = &reg_types_.FromDescriptor(class_loader_.Get(), descriptor, false);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004790 }
4791 }
4792 return *return_type_;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004793}
4794
Ian Rogersd8f69b02014-09-10 21:43:52 +00004795const RegType& MethodVerifier::GetDeclaringClass() {
Ian Rogers7b078e82014-09-10 14:44:24 -07004796 if (declaring_class_ == nullptr) {
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08004797 const dex::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
Brian Carlstrom93c33962013-07-26 10:37:43 -07004798 const char* descriptor
4799 = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(method_id.class_idx_));
Nicolas Geoffrayb041a402017-11-13 15:16:22 +00004800 if (method_being_verified_ != nullptr) {
Vladimir Marko2cffc5d2018-05-29 15:40:56 +01004801 ObjPtr<mirror::Class> klass = method_being_verified_->GetDeclaringClass();
Mathieu Chartierde40d472015-10-15 17:47:48 -07004802 declaring_class_ = &FromClass(descriptor, klass, klass->CannotBeAssignedFromOtherTypes());
Ian Rogers637c65b2013-05-31 11:46:00 -07004803 } else {
Andreas Gampe51de69e2019-04-19 15:14:14 -07004804 declaring_class_ = &reg_types_.FromDescriptor(class_loader_.Get(), descriptor, false);
Ian Rogers637c65b2013-05-31 11:46:00 -07004805 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07004806 }
Ian Rogers637c65b2013-05-31 11:46:00 -07004807 return *declaring_class_;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004808}
4809
Ian Rogersd8f69b02014-09-10 21:43:52 +00004810const RegType& MethodVerifier::DetermineCat1Constant(int32_t value, bool precise) {
Sebastien Hertz849600b2013-12-20 10:28:08 +01004811 if (precise) {
4812 // Precise constant type.
4813 return reg_types_.FromCat1Const(value, true);
4814 } else {
4815 // Imprecise constant type.
4816 if (value < -32768) {
4817 return reg_types_.IntConstant();
4818 } else if (value < -128) {
4819 return reg_types_.ShortConstant();
4820 } else if (value < 0) {
4821 return reg_types_.ByteConstant();
4822 } else if (value == 0) {
4823 return reg_types_.Zero();
4824 } else if (value == 1) {
4825 return reg_types_.One();
4826 } else if (value < 128) {
4827 return reg_types_.PosByteConstant();
4828 } else if (value < 32768) {
4829 return reg_types_.PosShortConstant();
4830 } else if (value < 65536) {
4831 return reg_types_.CharConstant();
4832 } else {
4833 return reg_types_.IntConstant();
4834 }
4835 }
4836}
4837
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004838void MethodVerifier::Init() {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08004839 art::verifier::RegTypeCache::Init();
Brian Carlstrome7d856b2012-01-11 18:10:55 -08004840}
4841
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004842void MethodVerifier::Shutdown() {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08004843 verifier::RegTypeCache::ShutDown();
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08004844}
jeffhaod1224c72012-02-29 13:43:08 -08004845
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -07004846void MethodVerifier::VisitStaticRoots(RootVisitor* visitor) {
4847 RegTypeCache::VisitStaticRoots(visitor);
Mathieu Chartier7c438b12014-09-12 17:01:24 -07004848}
4849
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -07004850void MethodVerifier::VisitRoots(RootVisitor* visitor, const RootInfo& root_info) {
4851 reg_types_.VisitRoots(visitor, root_info);
Mathieu Chartierc528dba2013-11-26 12:00:11 -08004852}
4853
Andreas Gampef23f33d2015-06-23 14:18:17 -07004854const RegType& MethodVerifier::FromClass(const char* descriptor,
Vladimir Marko2cffc5d2018-05-29 15:40:56 +01004855 ObjPtr<mirror::Class> klass,
Andreas Gampef23f33d2015-06-23 14:18:17 -07004856 bool precise) {
4857 DCHECK(klass != nullptr);
4858 if (precise && !klass->IsInstantiable() && !klass->IsPrimitive()) {
4859 Fail(VerifyError::VERIFY_ERROR_NO_CLASS) << "Could not create precise reference for "
4860 << "non-instantiable klass " << descriptor;
4861 precise = false;
4862 }
4863 return reg_types_.FromClass(descriptor, klass, precise);
4864}
4865
Andreas Gampe51de69e2019-04-19 15:14:14 -07004866InstructionFlags& MethodVerifier::GetModifiableInstructionFlags(size_t index) {
4867 return insn_flags_[index];
4868}
4869
Ian Rogersd81871c2011-10-03 13:57:23 -07004870} // namespace verifier
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07004871} // namespace art