blob: 167982124c7fd3b8a2d64ef760ea2d920caac97a [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070016
Mathieu Chartierc645f1d2014-03-06 18:11:53 -080017#include "method_verifier-inl.h"
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070018
Elliott Hughes1f359b02011-07-17 14:27:17 -070019#include <iostream>
20
Andreas Gampe46ee31b2016-12-14 10:11:49 -080021#include "android-base/stringprintf.h"
22
Mathieu Chartierc7853442015-03-27 14:35:38 -070023#include "art_field-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070024#include "art_method-inl.h"
Andreas Gampe39b378c2017-12-07 15:44:13 -080025#include "base/aborting.h"
Andreas Gampe542451c2016-07-26 09:02:02 -070026#include "base/enums.h"
David Sehr67bf42e2018-02-26 16:43:04 -080027#include "base/leb128.h"
David Sehr9c4a0152018-04-05 12:23:54 -070028#include "base/indenter.h"
Andreas Gampe57943812017-12-06 21:39:13 -080029#include "base/logging.h" // For VLOG.
Ian Rogers637c65b2013-05-31 11:46:00 -070030#include "base/mutex-inl.h"
David Brazdil2bb2fbd2018-11-13 18:24:26 +000031#include "base/sdk_version.h"
Vladimir Marko637ee0b2015-09-04 12:47:41 +010032#include "base/stl_util.h"
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -080033#include "base/systrace.h"
Vladimir Marko80afd022015-05-19 18:08:00 +010034#include "base/time_utils.h"
David Sehrc431b9d2018-03-02 12:01:51 -080035#include "base/utils.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070036#include "class_linker.h"
Vladimir Markoc7aa87e2018-05-24 15:19:52 +010037#include "class_root.h"
Vladimir Marko2b5eaa22013-12-13 13:59:30 +000038#include "compiler_callbacks.h"
Mathieu Chartier1f1cb9f2018-06-04 09:22:46 -070039#include "dex/class_accessor-inl.h"
David Sehrb2ec9f52018-02-21 13:20:31 -080040#include "dex/descriptors_names.h"
David Sehr9e734c72018-01-04 17:56:19 -080041#include "dex/dex_file-inl.h"
42#include "dex/dex_file_exception_helpers.h"
43#include "dex/dex_instruction-inl.h"
44#include "dex/dex_instruction_utils.h"
Alex Lighteb7c1442015-08-31 13:17:42 -070045#include "experimental_flags.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070046#include "gc/accounting/card_table-inl.h"
Andreas Gampe6d7abbd2017-04-24 13:19:09 -070047#include "handle_scope-inl.h"
Ian Rogers84fa0742011-10-25 18:13:30 -070048#include "intern_table.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080049#include "mirror/class-inl.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070050#include "mirror/class.h"
Ian Rogers39ebcb82013-05-30 16:57:23 -070051#include "mirror/dex_cache-inl.h"
Orion Hodsoncfa325e2016-10-13 10:25:54 +010052#include "mirror/method_handle_impl.h"
Orion Hodson2e599942017-09-22 16:17:41 +010053#include "mirror/method_type.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080054#include "mirror/object-inl.h"
55#include "mirror/object_array-inl.h"
Orion Hodsonfe92d122018-01-02 10:45:17 +000056#include "mirror/var_handle.h"
Ian Rogers7b078e82014-09-10 14:44:24 -070057#include "reg_type-inl.h"
Ian Rogers39ebcb82013-05-30 16:57:23 -070058#include "register_line-inl.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070059#include "runtime.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070060#include "scoped_thread_state_change-inl.h"
Andreas Gampe513061a2017-06-01 09:17:34 -070061#include "stack.h"
Nicolas Geoffrayb041a402017-11-13 15:16:22 +000062#include "vdex_file.h"
Andreas Gampe6d7abbd2017-04-24 13:19:09 -070063#include "verifier_compiler_binding.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070064#include "verifier_deps.h"
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070065
66namespace art {
Ian Rogersd81871c2011-10-03 13:57:23 -070067namespace verifier {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070068
Andreas Gampe46ee31b2016-12-14 10:11:49 -080069using android::base::StringPrintf;
70
Mathieu Chartier8e219ae2014-08-19 14:29:46 -070071static constexpr bool kTimeVerifyMethod = !kIsDebugBuild;
Ian Rogers2c8a8572011-10-24 17:11:36 -070072
Andreas Gampeebf850c2015-08-14 15:37:35 -070073// On VLOG(verifier), should we dump the whole state when we run into a hard failure?
74static constexpr bool kDumpRegLinesOnHardFailureIfVLOG = true;
75
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -080076// We print a warning blurb about "dx --no-optimize" when we find monitor-locking issues. Make
77// sure we only print this once.
78static bool gPrintedDxMonitorText = false;
79
Vladimir Marko69d310e2017-10-09 14:12:23 +010080PcToRegisterLineTable::PcToRegisterLineTable(ScopedArenaAllocator& allocator)
81 : register_lines_(allocator.Adapter(kArenaAllocVerifier)) {}
Mathieu Chartierde40d472015-10-15 17:47:48 -070082
Ian Rogers7b3ddd22013-02-21 15:19:52 -080083void PcToRegisterLineTable::Init(RegisterTrackingMode mode, InstructionFlags* flags,
Ian Rogersd81871c2011-10-03 13:57:23 -070084 uint32_t insns_size, uint16_t registers_size,
Ian Rogers776ac1f2012-04-13 23:36:36 -070085 MethodVerifier* verifier) {
Ian Rogersd81871c2011-10-03 13:57:23 -070086 DCHECK_GT(insns_size, 0U);
Mathieu Chartierde40d472015-10-15 17:47:48 -070087 register_lines_.resize(insns_size);
Ian Rogersd81871c2011-10-03 13:57:23 -070088 for (uint32_t i = 0; i < insns_size; i++) {
89 bool interesting = false;
90 switch (mode) {
91 case kTrackRegsAll:
92 interesting = flags[i].IsOpcode();
93 break;
Sameer Abu Asal02c42232013-04-30 12:09:45 -070094 case kTrackCompilerInterestPoints:
Brian Carlstrom02c8cc62013-07-18 15:54:44 -070095 interesting = flags[i].IsCompileTimeInfoPoint() || flags[i].IsBranchTarget();
Ian Rogersd81871c2011-10-03 13:57:23 -070096 break;
97 case kTrackRegsBranches:
98 interesting = flags[i].IsBranchTarget();
99 break;
100 default:
101 break;
102 }
103 if (interesting) {
Mathieu Chartierde40d472015-10-15 17:47:48 -0700104 register_lines_[i].reset(RegisterLine::Create(registers_size, verifier));
Ian Rogersd0fbd852013-09-24 18:17:04 -0700105 }
106 }
107}
108
Mathieu Chartierde40d472015-10-15 17:47:48 -0700109PcToRegisterLineTable::~PcToRegisterLineTable() {}
Ian Rogersd81871c2011-10-03 13:57:23 -0700110
Andreas Gampe7c038102014-10-27 20:08:46 -0700111// Note: returns true on failure.
Mathieu Chartierc2a2bda2018-03-12 11:21:52 -0700112inline bool MethodVerifier::FailOrAbort(bool condition,
113 const char* error_msg,
114 uint32_t work_insn_idx) {
Andreas Gampe7c038102014-10-27 20:08:46 -0700115 if (kIsDebugBuild) {
Andreas Gampef8f36c22016-11-19 14:51:52 -0800116 // In a debug build, abort if the error condition is wrong. Only warn if
117 // we are already aborting (as this verification is likely run to print
118 // lock information).
119 if (LIKELY(gAborting == 0)) {
Mathieu Chartierc2a2bda2018-03-12 11:21:52 -0700120 DCHECK(condition) << error_msg << work_insn_idx << " "
121 << dex_file_->PrettyMethod(dex_method_idx_);
Andreas Gampef8f36c22016-11-19 14:51:52 -0800122 } else {
123 if (!condition) {
124 LOG(ERROR) << error_msg << work_insn_idx;
Mathieu Chartierc2a2bda2018-03-12 11:21:52 -0700125 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << error_msg << work_insn_idx;
Andreas Gampef8f36c22016-11-19 14:51:52 -0800126 return true;
127 }
128 }
Andreas Gampe7c038102014-10-27 20:08:46 -0700129 } else {
130 // In a non-debug build, just fail the class.
131 if (!condition) {
Mathieu Chartierc2a2bda2018-03-12 11:21:52 -0700132 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << error_msg << work_insn_idx;
Andreas Gampe7c038102014-10-27 20:08:46 -0700133 return true;
134 }
135 }
136
137 return false;
138}
139
Stephen Kyle7e541c92014-12-17 17:10:02 +0000140static void SafelyMarkAllRegistersAsConflicts(MethodVerifier* verifier, RegisterLine* reg_line) {
Andreas Gampef10b6e12015-08-12 10:48:12 -0700141 if (verifier->IsInstanceConstructor()) {
Stephen Kyle7e541c92014-12-17 17:10:02 +0000142 // Before we mark all regs as conflicts, check that we don't have an uninitialized this.
143 reg_line->CheckConstructorReturn(verifier);
144 }
145 reg_line->MarkAllRegistersAsConflicts(verifier);
146}
147
Andreas Gampe6d7abbd2017-04-24 13:19:09 -0700148FailureKind MethodVerifier::VerifyClass(Thread* self,
Vladimir Markoa8bba7d2018-05-30 15:18:48 +0100149 ObjPtr<mirror::Class> klass,
Andreas Gampe6d7abbd2017-04-24 13:19:09 -0700150 CompilerCallbacks* callbacks,
151 bool allow_soft_failures,
152 HardFailLogMode log_level,
Andreas Gampe6cc23ac2018-08-24 15:22:43 -0700153 uint32_t api_level,
Andreas Gampe6d7abbd2017-04-24 13:19:09 -0700154 std::string* error) {
jeffhaobdb76512011-09-07 11:43:16 -0700155 if (klass->IsVerified()) {
Andreas Gampe6d7abbd2017-04-24 13:19:09 -0700156 return FailureKind::kNoFailure;
jeffhaobdb76512011-09-07 11:43:16 -0700157 }
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800158 bool early_failure = false;
159 std::string failure_message;
Mathieu Chartierf8322842014-05-16 10:59:25 -0700160 const DexFile& dex_file = klass->GetDexFile();
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800161 const dex::ClassDef* class_def = klass->GetClassDef();
Vladimir Markod93e3742018-07-18 10:58:13 +0100162 ObjPtr<mirror::Class> super = klass->GetSuperClass();
Ian Rogers1ff3c982014-08-12 02:30:58 -0700163 std::string temp;
Ian Rogers7b078e82014-09-10 14:44:24 -0700164 if (super == nullptr && strcmp("Ljava/lang/Object;", klass->GetDescriptor(&temp)) != 0) {
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800165 early_failure = true;
166 failure_message = " that has no super class";
Ian Rogers7b078e82014-09-10 14:44:24 -0700167 } else if (super != nullptr && super->IsFinal()) {
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800168 early_failure = true;
David Sehr709b0702016-10-13 09:12:37 -0700169 failure_message = " that attempts to sub-class final class " + super->PrettyDescriptor();
Ian Rogers7b078e82014-09-10 14:44:24 -0700170 } else if (class_def == nullptr) {
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800171 early_failure = true;
172 failure_message = " that isn't present in dex file " + dex_file.GetLocation();
173 }
174 if (early_failure) {
David Sehr709b0702016-10-13 09:12:37 -0700175 *error = "Verifier rejected class " + klass->PrettyDescriptor() + failure_message;
Andreas Gampe53e32d12015-12-09 21:03:23 -0800176 if (callbacks != nullptr) {
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800177 ClassReference ref(&dex_file, klass->GetDexClassDefIndex());
Andreas Gampe53e32d12015-12-09 21:03:23 -0800178 callbacks->ClassRejected(ref);
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800179 }
Andreas Gampe6d7abbd2017-04-24 13:19:09 -0700180 return FailureKind::kHardFailure;
jeffhaobdb76512011-09-07 11:43:16 -0700181 }
Ian Rogers7b078e82014-09-10 14:44:24 -0700182 StackHandleScope<2> hs(self);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700183 Handle<mirror::DexCache> dex_cache(hs.NewHandle(klass->GetDexCache()));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700184 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(klass->GetClassLoader()));
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800185 return VerifyClass(self,
186 &dex_file,
187 dex_cache,
188 class_loader,
David Brazdil15fc7292016-09-02 14:13:18 +0100189 *class_def,
Andreas Gampe53e32d12015-12-09 21:03:23 -0800190 callbacks,
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800191 allow_soft_failures,
Andreas Gampe7fe30232016-03-25 16:58:00 -0700192 log_level,
Andreas Gampe6cc23ac2018-08-24 15:22:43 -0700193 api_level,
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800194 error);
195}
196
Andreas Gampe6d7abbd2017-04-24 13:19:09 -0700197static FailureKind FailureKindMax(FailureKind fk1, FailureKind fk2) {
198 static_assert(FailureKind::kNoFailure < FailureKind::kSoftFailure
199 && FailureKind::kSoftFailure < FailureKind::kHardFailure,
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800200 "Unexpected FailureKind order");
201 return std::max(fk1, fk2);
202}
203
204void MethodVerifier::FailureData::Merge(const MethodVerifier::FailureData& fd) {
205 kind = FailureKindMax(kind, fd.kind);
206 types |= fd.types;
207}
208
Andreas Gampe6d7abbd2017-04-24 13:19:09 -0700209FailureKind MethodVerifier::VerifyClass(Thread* self,
210 const DexFile* dex_file,
211 Handle<mirror::DexCache> dex_cache,
212 Handle<mirror::ClassLoader> class_loader,
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800213 const dex::ClassDef& class_def,
Andreas Gampe6d7abbd2017-04-24 13:19:09 -0700214 CompilerCallbacks* callbacks,
215 bool allow_soft_failures,
216 HardFailLogMode log_level,
Andreas Gampe6cc23ac2018-08-24 15:22:43 -0700217 uint32_t api_level,
Andreas Gampe6d7abbd2017-04-24 13:19:09 -0700218 std::string* error) {
Andreas Gampe507cc6f2015-06-19 22:58:47 -0700219 // A class must not be abstract and final.
David Brazdil15fc7292016-09-02 14:13:18 +0100220 if ((class_def.access_flags_ & (kAccAbstract | kAccFinal)) == (kAccAbstract | kAccFinal)) {
Andreas Gampe507cc6f2015-06-19 22:58:47 -0700221 *error = "Verifier rejected class ";
David Brazdil15fc7292016-09-02 14:13:18 +0100222 *error += PrettyDescriptor(dex_file->GetClassDescriptor(class_def));
Andreas Gampe507cc6f2015-06-19 22:58:47 -0700223 *error += ": class is abstract and final.";
Andreas Gampe6d7abbd2017-04-24 13:19:09 -0700224 return FailureKind::kHardFailure;
Andreas Gampe507cc6f2015-06-19 22:58:47 -0700225 }
226
Mathieu Chartier1f1cb9f2018-06-04 09:22:46 -0700227 ClassAccessor accessor(*dex_file, class_def);
Mathieu Chartier18e26872018-06-04 17:19:02 -0700228 SCOPED_TRACE << "VerifyClass " << PrettyDescriptor(accessor.GetDescriptor());
Mathieu Chartier1f1cb9f2018-06-04 09:22:46 -0700229
230 int64_t previous_method_idx[2] = { -1, -1 };
231 MethodVerifier::FailureData failure_data;
232 ClassLinker* const linker = Runtime::Current()->GetClassLinker();
233
234 for (const ClassAccessor::Method& method : accessor.GetMethods()) {
235 int64_t* previous_idx = &previous_method_idx[method.IsStaticOrDirect() ? 0u : 1u];
236 self->AllowThreadSuspension();
237 const uint32_t method_idx = method.GetIndex();
238 if (method_idx == *previous_idx) {
239 // smali can create dex files with two encoded_methods sharing the same method_idx
240 // http://code.google.com/p/smali/issues/detail?id=119
241 continue;
242 }
243 *previous_idx = method_idx;
244 const InvokeType type = method.GetInvokeType(class_def.access_flags_);
245 ArtMethod* resolved_method = linker->ResolveMethod<ClassLinker::ResolveMode::kNoChecks>(
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700246 method_idx, dex_cache, class_loader, /* referrer= */ nullptr, type);
Mathieu Chartier1f1cb9f2018-06-04 09:22:46 -0700247 if (resolved_method == nullptr) {
248 DCHECK(self->IsExceptionPending());
249 // We couldn't resolve the method, but continue regardless.
250 self->ClearException();
251 } else {
252 DCHECK(resolved_method->GetDeclaringClassUnchecked() != nullptr) << type;
253 }
254 std::string hard_failure_msg;
255 MethodVerifier::FailureData result = VerifyMethod(self,
256 method_idx,
257 dex_file,
258 dex_cache,
259 class_loader,
260 class_def,
261 method.GetCodeItem(),
262 resolved_method,
263 method.GetAccessFlags(),
264 callbacks,
265 allow_soft_failures,
266 log_level,
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700267 /*need_precise_constants=*/ false,
Andreas Gampe6cc23ac2018-08-24 15:22:43 -0700268 api_level,
Mathieu Chartier1f1cb9f2018-06-04 09:22:46 -0700269 &hard_failure_msg);
270 if (result.kind == FailureKind::kHardFailure) {
271 if (failure_data.kind == FailureKind::kHardFailure) {
272 // If we logged an error before, we need a newline.
273 *error += "\n";
274 } else {
275 // If we didn't log a hard failure before, print the header of the message.
276 *error += "Verifier rejected class ";
277 *error += PrettyDescriptor(dex_file->GetClassDescriptor(class_def));
278 *error += ":";
279 }
280 *error += " ";
281 *error += hard_failure_msg;
282 }
283 failure_data.Merge(result);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700284 }
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800285
Mathieu Chartier1f1cb9f2018-06-04 09:22:46 -0700286 if (failure_data.kind == FailureKind::kNoFailure) {
Andreas Gampe6d7abbd2017-04-24 13:19:09 -0700287 return FailureKind::kNoFailure;
jeffhaof1e6b7c2012-06-05 18:33:30 -0700288 } else {
Mathieu Chartier1f1cb9f2018-06-04 09:22:46 -0700289 if ((failure_data.types & VERIFY_ERROR_LOCKING) != 0) {
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800290 // Print a warning about expected slow-down. Use a string temporary to print one contiguous
291 // warning.
292 std::string tmp =
293 StringPrintf("Class %s failed lock verification and will run slower.",
Mathieu Chartier1f1cb9f2018-06-04 09:22:46 -0700294 PrettyDescriptor(accessor.GetDescriptor()).c_str());
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800295 if (!gPrintedDxMonitorText) {
296 tmp = tmp + "\nCommon causes for lock verification issues are non-optimized dex code\n"
297 "and incorrect proguard optimizations.";
298 gPrintedDxMonitorText = true;
299 }
300 LOG(WARNING) << tmp;
301 }
Mathieu Chartier1f1cb9f2018-06-04 09:22:46 -0700302 return failure_data.kind;
jeffhaof1e6b7c2012-06-05 18:33:30 -0700303 }
jeffhaof56197c2012-03-05 18:01:54 -0800304}
305
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800306static bool IsLargeMethod(const CodeItemDataAccessor& accessor) {
307 if (!accessor.HasCodeItem()) {
Andreas Gampe3c651fc2015-05-21 14:06:46 -0700308 return false;
309 }
310
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800311 uint16_t registers_size = accessor.RegistersSize();
312 uint32_t insns_size = accessor.InsnsSizeInCodeUnits();
Andreas Gampea4f5bf62015-05-18 20:50:12 -0700313
314 return registers_size * insns_size > 4*1024*1024;
315}
316
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800317MethodVerifier::FailureData MethodVerifier::VerifyMethod(Thread* self,
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800318 uint32_t method_idx,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800319 const DexFile* dex_file,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700320 Handle<mirror::DexCache> dex_cache,
321 Handle<mirror::ClassLoader> class_loader,
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800322 const dex::ClassDef& class_def,
323 const dex::CodeItem* code_item,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700324 ArtMethod* method,
Jeff Haoee988952013-04-16 14:23:47 -0700325 uint32_t method_access_flags,
Andreas Gampe53e32d12015-12-09 21:03:23 -0800326 CompilerCallbacks* callbacks,
Ian Rogers46960fe2014-05-23 10:43:43 -0700327 bool allow_soft_failures,
Andreas Gampe5fd66d02016-09-12 20:22:19 -0700328 HardFailLogMode log_level,
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800329 bool need_precise_constants,
Andreas Gampe6cc23ac2018-08-24 15:22:43 -0700330 uint32_t api_level,
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800331 std::string* hard_failure_msg) {
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800332 MethodVerifier::FailureData result;
Mathieu Chartier8e219ae2014-08-19 14:29:46 -0700333 uint64_t start_ns = kTimeVerifyMethod ? NanoTime() : 0;
Ian Rogersc8982582012-09-07 16:53:25 -0700334
Andreas Gampebf9611f2016-03-25 16:58:00 -0700335 MethodVerifier verifier(self,
336 dex_file,
337 dex_cache,
338 class_loader,
339 class_def,
340 code_item,
341 method_idx,
342 method,
343 method_access_flags,
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700344 /* can_load_classes= */ true,
Andreas Gampebf9611f2016-03-25 16:58:00 -0700345 allow_soft_failures,
346 need_precise_constants,
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700347 /* verify to dump */ false,
348 /* allow_thread_suspension= */ true,
Andreas Gampe6cc23ac2018-08-24 15:22:43 -0700349 api_level);
Ian Rogers46960fe2014-05-23 10:43:43 -0700350 if (verifier.Verify()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700351 // Verification completed, however failures may be pending that didn't cause the verification
352 // to hard fail.
Ian Rogers46960fe2014-05-23 10:43:43 -0700353 CHECK(!verifier.have_pending_hard_failure_);
Andreas Gampe53e32d12015-12-09 21:03:23 -0800354
355 if (code_item != nullptr && callbacks != nullptr) {
356 // Let the interested party know that the method was verified.
357 callbacks->MethodVerified(&verifier);
358 }
359
Ian Rogers46960fe2014-05-23 10:43:43 -0700360 if (verifier.failures_.size() != 0) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -0700361 if (VLOG_IS_ON(verifier)) {
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800362 verifier.DumpFailures(VLOG_STREAM(verifier) << "Soft verification failures in "
David Sehr709b0702016-10-13 09:12:37 -0700363 << dex_file->PrettyMethod(method_idx) << "\n");
Anwar Ghuloum75a43f12013-08-13 17:22:14 -0700364 }
Andreas Gampe92d77202017-12-06 20:49:00 -0800365 if (VLOG_IS_ON(verifier_debug)) {
366 std::cout << "\n" << verifier.info_messages_.str();
367 verifier.Dump(std::cout);
368 }
Andreas Gampe6d7abbd2017-04-24 13:19:09 -0700369 result.kind = FailureKind::kSoftFailure;
Nicolas Geoffray250a3782016-04-20 16:27:53 +0100370 if (method != nullptr &&
371 !CanCompilerHandleVerificationFailure(verifier.encountered_failure_types_)) {
Alex Lightfcbafb32017-02-02 15:09:54 -0800372 method->SetDontCompile();
Nicolas Geoffray250a3782016-04-20 16:27:53 +0100373 }
374 }
Andreas Gampe56fdd0e2016-04-28 14:56:54 -0700375 if (method != nullptr) {
376 if (verifier.HasInstructionThatWillThrow()) {
Alex Lightfcbafb32017-02-02 15:09:54 -0800377 method->SetDontCompile();
Calin Juravle857f0582016-12-20 14:36:59 +0000378 if (Runtime::Current()->IsAotCompiler() &&
379 (callbacks != nullptr) && !callbacks->IsBootImage()) {
380 // When compiling apps, make HasInstructionThatWillThrow a soft error to trigger
381 // re-verification at runtime.
382 // The dead code after the throw is not verified and might be invalid. This may cause
383 // the JIT compiler to crash since it assumes that all the code is valid.
384 //
385 // There's a strong assumption that the entire boot image is verified and all its dex
386 // code is valid (even the dead and unverified one). As such this is done only for apps.
387 // (CompilerDriver DCHECKs in VerifyClassVisitor that methods from boot image are
388 // fully verified).
Andreas Gampe6d7abbd2017-04-24 13:19:09 -0700389 result.kind = FailureKind::kSoftFailure;
Calin Juravle857f0582016-12-20 14:36:59 +0000390 }
Andreas Gampe56fdd0e2016-04-28 14:56:54 -0700391 }
392 if ((verifier.encountered_failure_types_ & VerifyError::VERIFY_ERROR_LOCKING) != 0) {
Orion Hodsoncfcc9cf2017-09-29 15:07:27 +0100393 method->SetMustCountLocks();
Andreas Gampe56fdd0e2016-04-28 14:56:54 -0700394 }
jeffhaof56197c2012-03-05 18:01:54 -0800395 }
396 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700397 // Bad method data.
Ian Rogers46960fe2014-05-23 10:43:43 -0700398 CHECK_NE(verifier.failures_.size(), 0U);
Igor Murashkin4d7b75f2015-07-21 17:03:36 -0700399
400 if (UNLIKELY(verifier.have_pending_experimental_failure_)) {
401 // Failed due to being forced into interpreter. This is ok because
402 // we just want to skip verification.
Andreas Gampe6d7abbd2017-04-24 13:19:09 -0700403 result.kind = FailureKind::kSoftFailure;
Igor Murashkin4d7b75f2015-07-21 17:03:36 -0700404 } else {
405 CHECK(verifier.have_pending_hard_failure_);
Andreas Gampe7fe30232016-03-25 16:58:00 -0700406 if (VLOG_IS_ON(verifier)) {
Andreas Gampe5fd66d02016-09-12 20:22:19 -0700407 log_level = std::max(HardFailLogMode::kLogVerbose, log_level);
Andreas Gampe7fe30232016-03-25 16:58:00 -0700408 }
Andreas Gampe5fd66d02016-09-12 20:22:19 -0700409 if (log_level >= HardFailLogMode::kLogVerbose) {
410 LogSeverity severity;
411 switch (log_level) {
412 case HardFailLogMode::kLogVerbose:
413 severity = LogSeverity::VERBOSE;
414 break;
415 case HardFailLogMode::kLogWarning:
416 severity = LogSeverity::WARNING;
417 break;
418 case HardFailLogMode::kLogInternalFatal:
Andreas Gampe3fec9ac2016-09-13 10:47:28 -0700419 severity = LogSeverity::FATAL_WITHOUT_ABORT;
Andreas Gampe5fd66d02016-09-12 20:22:19 -0700420 break;
421 default:
422 LOG(FATAL) << "Unsupported log-level " << static_cast<uint32_t>(log_level);
423 UNREACHABLE();
424 }
Andreas Gampe3fec9ac2016-09-13 10:47:28 -0700425 verifier.DumpFailures(LOG_STREAM(severity) << "Verification error in "
David Sehr709b0702016-10-13 09:12:37 -0700426 << dex_file->PrettyMethod(method_idx)
Andreas Gampe3fec9ac2016-09-13 10:47:28 -0700427 << "\n");
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800428 }
429 if (hard_failure_msg != nullptr) {
430 CHECK(!verifier.failure_messages_.empty());
431 *hard_failure_msg =
432 verifier.failure_messages_[verifier.failure_messages_.size() - 1]->str();
433 }
Andreas Gampe6d7abbd2017-04-24 13:19:09 -0700434 result.kind = FailureKind::kHardFailure;
Andreas Gampe53e32d12015-12-09 21:03:23 -0800435
436 if (callbacks != nullptr) {
437 // Let the interested party know that we failed the class.
David Brazdil15fc7292016-09-02 14:13:18 +0100438 ClassReference ref(dex_file, dex_file->GetIndexForClassDef(class_def));
Andreas Gampe53e32d12015-12-09 21:03:23 -0800439 callbacks->ClassRejected(ref);
440 }
Igor Murashkin4d7b75f2015-07-21 17:03:36 -0700441 }
Andreas Gampe92d77202017-12-06 20:49:00 -0800442 if (VLOG_IS_ON(verifier) || VLOG_IS_ON(verifier_debug)) {
Ian Rogers46960fe2014-05-23 10:43:43 -0700443 std::cout << "\n" << verifier.info_messages_.str();
444 verifier.Dump(std::cout);
jeffhaof56197c2012-03-05 18:01:54 -0800445 }
jeffhaof56197c2012-03-05 18:01:54 -0800446 }
Mathieu Chartier8e219ae2014-08-19 14:29:46 -0700447 if (kTimeVerifyMethod) {
448 uint64_t duration_ns = NanoTime() - start_ns;
Andreas Gampe0b0ffc12018-08-01 14:41:27 -0700449 if (duration_ns > MsToNs(Runtime::Current()->GetVerifierLoggingThresholdMs())) {
David Sehr709b0702016-10-13 09:12:37 -0700450 LOG(WARNING) << "Verification of " << dex_file->PrettyMethod(method_idx)
Andreas Gampea4f5bf62015-05-18 20:50:12 -0700451 << " took " << PrettyDuration(duration_ns)
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800452 << (IsLargeMethod(verifier.CodeItem()) ? " (large method)" : "");
Mathieu Chartier8e219ae2014-08-19 14:29:46 -0700453 }
Ian Rogersc8982582012-09-07 16:53:25 -0700454 }
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800455 result.types = verifier.encountered_failure_types_;
Ian Rogersc8982582012-09-07 16:53:25 -0700456 return result;
jeffhaof56197c2012-03-05 18:01:54 -0800457}
458
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100459MethodVerifier* MethodVerifier::VerifyMethodAndDump(Thread* self,
460 VariableIndentationOutputStream* vios,
461 uint32_t dex_method_idx,
462 const DexFile* dex_file,
463 Handle<mirror::DexCache> dex_cache,
464 Handle<mirror::ClassLoader> class_loader,
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800465 const dex::ClassDef& class_def,
466 const dex::CodeItem* code_item,
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100467 ArtMethod* method,
Andreas Gampe6cc23ac2018-08-24 15:22:43 -0700468 uint32_t method_access_flags,
469 uint32_t api_level) {
Andreas Gampebf9611f2016-03-25 16:58:00 -0700470 MethodVerifier* verifier = new MethodVerifier(self,
471 dex_file,
472 dex_cache,
473 class_loader,
474 class_def,
475 code_item,
476 dex_method_idx,
477 method,
478 method_access_flags,
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700479 /* can_load_classes= */ true,
480 /* allow_soft_failures= */ true,
481 /* need_precise_constants= */ true,
482 /* verify_to_dump= */ true,
483 /* allow_thread_suspension= */ true,
Andreas Gampe6cc23ac2018-08-24 15:22:43 -0700484 api_level);
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700485 verifier->Verify();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100486 verifier->DumpFailures(vios->Stream());
487 vios->Stream() << verifier->info_messages_.str();
Andreas Gampe5cbcde22014-09-16 14:59:49 -0700488 // Only dump and return if no hard failures. Otherwise the verifier may be not fully initialized
489 // and querying any info is dangerous/can abort.
490 if (verifier->have_pending_hard_failure_) {
491 delete verifier;
492 return nullptr;
493 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100494 verifier->Dump(vios);
Andreas Gampe5cbcde22014-09-16 14:59:49 -0700495 return verifier;
496 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800497}
498
Ian Rogers7b078e82014-09-10 14:44:24 -0700499MethodVerifier::MethodVerifier(Thread* self,
Andreas Gampe53e32d12015-12-09 21:03:23 -0800500 const DexFile* dex_file,
501 Handle<mirror::DexCache> dex_cache,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700502 Handle<mirror::ClassLoader> class_loader,
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800503 const dex::ClassDef& class_def,
504 const dex::CodeItem* code_item,
Andreas Gampe53e32d12015-12-09 21:03:23 -0800505 uint32_t dex_method_idx,
506 ArtMethod* method,
507 uint32_t method_access_flags,
508 bool can_load_classes,
509 bool allow_soft_failures,
510 bool need_precise_constants,
511 bool verify_to_dump,
Andreas Gampe6cc23ac2018-08-24 15:22:43 -0700512 bool allow_thread_suspension,
513 uint32_t api_level)
Ian Rogers7b078e82014-09-10 14:44:24 -0700514 : self_(self),
Mathieu Chartierde40d472015-10-15 17:47:48 -0700515 arena_stack_(Runtime::Current()->GetArenaPool()),
Vladimir Marko69d310e2017-10-09 14:12:23 +0100516 allocator_(&arena_stack_),
Alex Lightd9aff132017-10-31 22:30:05 +0000517 reg_types_(can_load_classes, allocator_, allow_thread_suspension),
Vladimir Marko69d310e2017-10-09 14:12:23 +0100518 reg_table_(allocator_),
Andreas Gampee2abbc62017-09-15 11:59:26 -0700519 work_insn_idx_(dex::kDexNoIndex),
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800520 dex_method_idx_(dex_method_idx),
Nicolas Geoffrayb041a402017-11-13 15:16:22 +0000521 method_being_verified_(method),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700522 method_access_flags_(method_access_flags),
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700523 return_type_(nullptr),
jeffhaof56197c2012-03-05 18:01:54 -0800524 dex_file_(dex_file),
525 dex_cache_(dex_cache),
526 class_loader_(class_loader),
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700527 class_def_(class_def),
Mathieu Chartier698ebbc2018-01-05 11:00:42 -0800528 code_item_accessor_(*dex_file, code_item),
Ian Rogers7b078e82014-09-10 14:44:24 -0700529 declaring_class_(nullptr),
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700530 interesting_dex_pc_(-1),
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700531 monitor_enter_dex_pcs_(nullptr),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700532 have_pending_hard_failure_(false),
jeffhaofaf459e2012-08-31 15:32:47 -0700533 have_pending_runtime_throw_failure_(false),
Igor Murashkin4d7b75f2015-07-21 17:03:36 -0700534 have_pending_experimental_failure_(false),
Andreas Gamped12e7822015-06-25 10:26:40 -0700535 have_any_pending_runtime_throw_failure_(false),
jeffhaof56197c2012-03-05 18:01:54 -0800536 new_instance_count_(0),
Elliott Hughes80537bb2013-01-04 16:37:26 -0800537 monitor_enter_count_(0),
Andreas Gampe0760a812015-08-26 17:12:51 -0700538 encountered_failure_types_(0),
Jeff Haoee988952013-04-16 14:23:47 -0700539 can_load_classes_(can_load_classes),
Sebastien Hertz4d4adb12013-07-24 16:14:19 +0200540 allow_soft_failures_(allow_soft_failures),
Ian Rogers46960fe2014-05-23 10:43:43 -0700541 need_precise_constants_(need_precise_constants),
Sebastien Hertz4d4adb12013-07-24 16:14:19 +0200542 has_check_casts_(false),
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700543 has_virtual_or_interface_invokes_(false),
Mathieu Chartier4306ef82014-12-19 18:41:47 -0800544 verify_to_dump_(verify_to_dump),
Andreas Gamped5ad72f2015-06-26 17:33:47 -0700545 allow_thread_suspension_(allow_thread_suspension),
Andreas Gampee6215c02015-08-31 18:54:38 -0700546 is_constructor_(false),
Andreas Gampe6cc23ac2018-08-24 15:22:43 -0700547 link_(nullptr),
548 api_level_(api_level == 0 ? std::numeric_limits<uint32_t>::max() : api_level) {
Mathieu Chartierd0ad2ee2015-03-31 14:59:59 -0700549 self->PushVerifier(this);
jeffhaof56197c2012-03-05 18:01:54 -0800550}
551
Mathieu Chartier590fee92013-09-13 13:46:47 -0700552MethodVerifier::~MethodVerifier() {
Mathieu Chartierd0ad2ee2015-03-31 14:59:59 -0700553 Thread::Current()->PopVerifier(this);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700554 STLDeleteElements(&failure_messages_);
555}
556
Andreas Gampeaaf0d382017-11-27 14:10:21 -0800557void MethodVerifier::FindLocksAtDexPc(
558 ArtMethod* m,
559 uint32_t dex_pc,
Andreas Gampe6cc23ac2018-08-24 15:22:43 -0700560 std::vector<MethodVerifier::DexLockInfo>* monitor_enter_dex_pcs,
561 uint32_t api_level) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700562 StackHandleScope<2> hs(Thread::Current());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700563 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache()));
564 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader()));
Andreas Gampebf9611f2016-03-25 16:58:00 -0700565 MethodVerifier verifier(hs.Self(),
566 m->GetDexFile(),
567 dex_cache,
568 class_loader,
David Brazdil15fc7292016-09-02 14:13:18 +0100569 m->GetClassDef(),
Andreas Gampebf9611f2016-03-25 16:58:00 -0700570 m->GetCodeItem(),
571 m->GetDexMethodIndex(),
572 m,
573 m->GetAccessFlags(),
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700574 /* can_load_classes= */ false,
575 /* allow_soft_failures= */ true,
576 /* need_precise_constants= */ false,
577 /* verify_to_dump= */ false,
578 /* allow_thread_suspension= */ false,
Andreas Gampe6cc23ac2018-08-24 15:22:43 -0700579 api_level);
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700580 verifier.interesting_dex_pc_ = dex_pc;
Ian Rogers46960fe2014-05-23 10:43:43 -0700581 verifier.monitor_enter_dex_pcs_ = monitor_enter_dex_pcs;
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700582 verifier.FindLocksAtDexPc();
583}
584
585void MethodVerifier::FindLocksAtDexPc() {
Ian Rogers7b078e82014-09-10 14:44:24 -0700586 CHECK(monitor_enter_dex_pcs_ != nullptr);
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -0800587 CHECK(code_item_accessor_.HasCodeItem()); // This only makes sense for methods with code.
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700588
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -0800589 // Quick check whether there are any monitor_enter instructions before verifying.
590 for (const DexInstructionPcPair& inst : code_item_accessor_) {
591 if (inst->Opcode() == Instruction::MONITOR_ENTER) {
592 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
593 // verification. In practice, the phase we want relies on data structures set up by all the
594 // earlier passes, so we just run the full method verification and bail out early when we've
595 // got what we wanted.
596 Verify();
597 return;
598 }
Andreas Gampecb3c08f2014-09-18 13:16:38 -0700599 }
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700600}
601
Ian Rogersad0b3a32012-04-16 14:50:24 -0700602bool MethodVerifier::Verify() {
Andreas Gampee6215c02015-08-31 18:54:38 -0700603 // Some older code doesn't correctly mark constructors as such. Test for this case by looking at
604 // the name.
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800605 const dex::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
Andreas Gampee6215c02015-08-31 18:54:38 -0700606 const char* method_name = dex_file_->StringDataByIdx(method_id.name_idx_);
607 bool instance_constructor_by_name = strcmp("<init>", method_name) == 0;
608 bool static_constructor_by_name = strcmp("<clinit>", method_name) == 0;
609 bool constructor_by_name = instance_constructor_by_name || static_constructor_by_name;
610 // Check that only constructors are tagged, and check for bad code that doesn't tag constructors.
611 if ((method_access_flags_ & kAccConstructor) != 0) {
612 if (!constructor_by_name) {
613 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
614 << "method is marked as constructor, but not named accordingly";
jeffhaobdb76512011-09-07 11:43:16 -0700615 return false;
Andreas Gampee6215c02015-08-31 18:54:38 -0700616 }
617 is_constructor_ = true;
618 } else if (constructor_by_name) {
David Sehr709b0702016-10-13 09:12:37 -0700619 LOG(WARNING) << "Method " << dex_file_->PrettyMethod(dex_method_idx_)
Andreas Gampee6215c02015-08-31 18:54:38 -0700620 << " not marked as constructor.";
621 is_constructor_ = true;
622 }
623 // If it's a constructor, check whether IsStatic() matches the name.
624 // This should have been rejected by the dex file verifier. Only do in debug build.
625 if (kIsDebugBuild) {
626 if (IsConstructor()) {
627 if (IsStatic() ^ static_constructor_by_name) {
628 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
629 << "constructor name doesn't match static flag";
630 return false;
631 }
jeffhaobdb76512011-09-07 11:43:16 -0700632 }
jeffhaobdb76512011-09-07 11:43:16 -0700633 }
Andreas Gampee6215c02015-08-31 18:54:38 -0700634
635 // Methods may only have one of public/protected/private.
636 // This should have been rejected by the dex file verifier. Only do in debug build.
637 if (kIsDebugBuild) {
638 size_t access_mod_count =
639 (((method_access_flags_ & kAccPublic) == 0) ? 0 : 1) +
640 (((method_access_flags_ & kAccProtected) == 0) ? 0 : 1) +
641 (((method_access_flags_ & kAccPrivate) == 0) ? 0 : 1);
642 if (access_mod_count > 1) {
643 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "method has more than one of public/protected/private";
644 return false;
645 }
646 }
647
648 // If there aren't any instructions, make sure that's expected, then exit successfully.
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -0800649 if (!code_item_accessor_.HasCodeItem()) {
Andreas Gampe76ed99d2016-03-28 18:31:29 -0700650 // Only native or abstract methods may not have code.
651 if ((method_access_flags_ & (kAccNative | kAccAbstract)) == 0) {
652 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "zero-length code in concrete non-native method";
653 return false;
654 }
655
Andreas Gampee6215c02015-08-31 18:54:38 -0700656 // This should have been rejected by the dex file verifier. Only do in debug build.
Andreas Gampe76ed99d2016-03-28 18:31:29 -0700657 // Note: the above will also be rejected in the dex file verifier, starting in dex version 37.
Andreas Gampee6215c02015-08-31 18:54:38 -0700658 if (kIsDebugBuild) {
Andreas Gampee6215c02015-08-31 18:54:38 -0700659 if ((method_access_flags_ & kAccAbstract) != 0) {
660 // Abstract methods are not allowed to have the following flags.
661 static constexpr uint32_t kForbidden =
662 kAccPrivate |
663 kAccStatic |
664 kAccFinal |
665 kAccNative |
666 kAccStrict |
667 kAccSynchronized;
668 if ((method_access_flags_ & kForbidden) != 0) {
669 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
670 << "method can't be abstract and private/static/final/native/strict/synchronized";
671 return false;
672 }
673 }
David Brazdil15fc7292016-09-02 14:13:18 +0100674 if ((class_def_.GetJavaAccessFlags() & kAccInterface) != 0) {
Alex Lighteb7c1442015-08-31 13:17:42 -0700675 // Interface methods must be public and abstract (if default methods are disabled).
Neil Fuller9724c632016-01-07 15:42:47 +0000676 uint32_t kRequired = kAccPublic;
Alex Lighteb7c1442015-08-31 13:17:42 -0700677 if ((method_access_flags_ & kRequired) != kRequired) {
Neil Fuller9724c632016-01-07 15:42:47 +0000678 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface methods must be public";
Andreas Gampee6215c02015-08-31 18:54:38 -0700679 return false;
680 }
681 // In addition to the above, interface methods must not be protected.
682 static constexpr uint32_t kForbidden = kAccProtected;
683 if ((method_access_flags_ & kForbidden) != 0) {
684 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface methods can't be protected";
685 return false;
686 }
687 }
688 // We also don't allow constructors to be abstract or native.
689 if (IsConstructor()) {
690 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "constructors can't be abstract or native";
691 return false;
692 }
693 }
694 return true;
695 }
696
697 // This should have been rejected by the dex file verifier. Only do in debug build.
698 if (kIsDebugBuild) {
699 // When there's code, the method must not be native or abstract.
700 if ((method_access_flags_ & (kAccNative | kAccAbstract)) != 0) {
701 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "non-zero-length code in abstract or native method";
702 return false;
703 }
704
David Brazdil15fc7292016-09-02 14:13:18 +0100705 if ((class_def_.GetJavaAccessFlags() & kAccInterface) != 0) {
Alex Lighteb7c1442015-08-31 13:17:42 -0700706 // Interfaces may always have static initializers for their fields. If we are running with
707 // default methods enabled we also allow other public, static, non-final methods to have code.
708 // Otherwise that is the only type of method allowed.
Alex Light0db36b32015-10-27 14:06:34 -0700709 if (!(IsConstructor() && IsStatic())) {
Neil Fuller9724c632016-01-07 15:42:47 +0000710 if (IsInstanceConstructor()) {
711 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interfaces may not have non-static constructor";
712 return false;
713 } else if (method_access_flags_ & kAccFinal) {
714 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interfaces may not have final methods";
715 return false;
Alex Lightb55f1ac2016-04-12 15:50:55 -0700716 } else {
717 uint32_t access_flag_options = kAccPublic;
Mathieu Chartierf6e31472017-12-28 13:32:08 -0800718 if (dex_file_->SupportsDefaultMethods()) {
Alex Lightb55f1ac2016-04-12 15:50:55 -0700719 access_flag_options |= kAccPrivate;
720 }
721 if (!(method_access_flags_ & access_flag_options)) {
722 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
723 << "interfaces may not have protected or package-private members";
724 return false;
725 }
Alex Lighteb7c1442015-08-31 13:17:42 -0700726 }
Andreas Gampee6215c02015-08-31 18:54:38 -0700727 }
728 }
729
730 // Instance constructors must not be synchronized.
731 if (IsInstanceConstructor()) {
732 static constexpr uint32_t kForbidden = kAccSynchronized;
733 if ((method_access_flags_ & kForbidden) != 0) {
734 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "constructors can't be synchronized";
735 return false;
736 }
737 }
738 }
739
Ian Rogersd81871c2011-10-03 13:57:23 -0700740 // Sanity-check the register counts. ins + locals = registers, so make sure that ins <= registers.
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -0800741 if (code_item_accessor_.InsSize() > code_item_accessor_.RegistersSize()) {
742 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad register counts (ins="
743 << code_item_accessor_.InsSize()
744 << " regs=" << code_item_accessor_.RegistersSize();
Ian Rogersd81871c2011-10-03 13:57:23 -0700745 return false;
jeffhaobdb76512011-09-07 11:43:16 -0700746 }
Alex Lighteb7c1442015-08-31 13:17:42 -0700747
Ian Rogersd81871c2011-10-03 13:57:23 -0700748 // Allocate and initialize an array to hold instruction data.
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -0800749 insn_flags_.reset(allocator_.AllocArray<InstructionFlags>(
750 code_item_accessor_.InsnsSizeInCodeUnits()));
Mathieu Chartierde40d472015-10-15 17:47:48 -0700751 DCHECK(insn_flags_ != nullptr);
752 std::uninitialized_fill_n(insn_flags_.get(),
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -0800753 code_item_accessor_.InsnsSizeInCodeUnits(),
Mathieu Chartierde40d472015-10-15 17:47:48 -0700754 InstructionFlags());
Ian Rogersd81871c2011-10-03 13:57:23 -0700755 // Run through the instructions and see if the width checks out.
756 bool result = ComputeWidthsAndCountOps();
Andreas Gampebf1cb772017-05-15 15:39:00 -0700757 bool allow_runtime_only_instructions = !Runtime::Current()->IsAotCompiler() || verify_to_dump_;
Ian Rogersd81871c2011-10-03 13:57:23 -0700758 // Flag instructions guarded by a "try" block and check exception handlers.
759 result = result && ScanTryCatchBlocks();
760 // Perform static instruction verification.
Andreas Gampebf1cb772017-05-15 15:39:00 -0700761 result = result && (allow_runtime_only_instructions
762 ? VerifyInstructions<true>()
763 : VerifyInstructions<false>());
Ian Rogersad0b3a32012-04-16 14:50:24 -0700764 // Perform code-flow analysis and return.
Vladimir Marko2b5eaa22013-12-13 13:59:30 +0000765 result = result && VerifyCodeFlow();
Andreas Gampe53e32d12015-12-09 21:03:23 -0800766
Vladimir Marko2b5eaa22013-12-13 13:59:30 +0000767 return result;
jeffhaoba5ebb92011-08-25 17:24:37 -0700768}
769
Ian Rogers776ac1f2012-04-13 23:36:36 -0700770std::ostream& MethodVerifier::Fail(VerifyError error) {
Andreas Gampe0760a812015-08-26 17:12:51 -0700771 // Mark the error type as encountered.
Andreas Gampea727e372015-08-25 09:22:37 -0700772 encountered_failure_types_ |= static_cast<uint32_t>(error);
Andreas Gampe0760a812015-08-26 17:12:51 -0700773
Ian Rogersad0b3a32012-04-16 14:50:24 -0700774 switch (error) {
775 case VERIFY_ERROR_NO_CLASS:
776 case VERIFY_ERROR_NO_FIELD:
777 case VERIFY_ERROR_NO_METHOD:
778 case VERIFY_ERROR_ACCESS_CLASS:
779 case VERIFY_ERROR_ACCESS_FIELD:
780 case VERIFY_ERROR_ACCESS_METHOD:
Ian Rogers08f753d2012-08-24 14:35:25 -0700781 case VERIFY_ERROR_INSTANTIATION:
782 case VERIFY_ERROR_CLASS_CHANGE:
Igor Murashkin158f35c2015-06-10 15:55:30 -0700783 case VERIFY_ERROR_FORCE_INTERPRETER:
Andreas Gampea727e372015-08-25 09:22:37 -0700784 case VERIFY_ERROR_LOCKING:
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800785 if (Runtime::Current()->IsAotCompiler() || !can_load_classes_) {
jeffhaofaf459e2012-08-31 15:32:47 -0700786 // If we're optimistically running verification at compile time, turn NO_xxx, ACCESS_xxx,
787 // class change and instantiation errors into soft verification errors so that we re-verify
788 // at runtime. We may fail to find or to agree on access because of not yet available class
789 // loaders, or class loaders that will differ at runtime. In these cases, we don't want to
790 // affect the soundness of the code being compiled. Instead, the generated code runs "slow
791 // paths" that dynamically perform the verification and cause the behavior to be that akin
792 // to an interpreter.
793 error = VERIFY_ERROR_BAD_CLASS_SOFT;
794 } else {
Jeff Haoa3faaf42013-09-03 19:07:00 -0700795 // If we fail again at runtime, mark that this instruction would throw and force this
796 // method to be executed using the interpreter with checks.
jeffhaofaf459e2012-08-31 15:32:47 -0700797 have_pending_runtime_throw_failure_ = true;
Andreas Gamped7f8d052015-03-12 11:05:47 -0700798
799 // We need to save the work_line if the instruction wasn't throwing before. Otherwise we'll
800 // try to merge garbage.
801 // Note: this assumes that Fail is called before we do any work_line modifications.
Andreas Gamped5ad72f2015-06-26 17:33:47 -0700802 // Note: this can fail before we touch any instruction, for the signature of a method. So
803 // add a check.
Andreas Gampee2abbc62017-09-15 11:59:26 -0700804 if (work_insn_idx_ < dex::kDexNoIndex) {
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -0800805 const Instruction& inst = code_item_accessor_.InstructionAt(work_insn_idx_);
Mathieu Chartier1d2d4ff2017-09-23 16:11:06 -0700806 int opcode_flags = Instruction::FlagsOf(inst.Opcode());
Andreas Gamped7f8d052015-03-12 11:05:47 -0700807
Andreas Gamped5ad72f2015-06-26 17:33:47 -0700808 if ((opcode_flags & Instruction::kThrow) == 0 && CurrentInsnFlags()->IsInTry()) {
809 saved_line_->CopyFromLine(work_line_.get());
810 }
Andreas Gamped7f8d052015-03-12 11:05:47 -0700811 }
jeffhaofaf459e2012-08-31 15:32:47 -0700812 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700813 break;
Andreas Gampea727e372015-08-25 09:22:37 -0700814
Ian Rogersad0b3a32012-04-16 14:50:24 -0700815 // Indication that verification should be retried at runtime.
816 case VERIFY_ERROR_BAD_CLASS_SOFT:
Jeff Haoee988952013-04-16 14:23:47 -0700817 if (!allow_soft_failures_) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700818 have_pending_hard_failure_ = true;
819 }
820 break;
Andreas Gampea727e372015-08-25 09:22:37 -0700821
jeffhaod5347e02012-03-22 17:25:05 -0700822 // Hard verification failures at compile time will still fail at runtime, so the class is
823 // marked as rejected to prevent it from being compiled.
Ian Rogersad0b3a32012-04-16 14:50:24 -0700824 case VERIFY_ERROR_BAD_CLASS_HARD: {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700825 have_pending_hard_failure_ = true;
Andreas Gampeebf850c2015-08-14 15:37:35 -0700826 if (VLOG_IS_ON(verifier) && kDumpRegLinesOnHardFailureIfVLOG) {
827 ScopedObjectAccess soa(Thread::Current());
828 std::ostringstream oss;
829 Dump(oss);
830 LOG(ERROR) << oss.str();
831 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700832 break;
Ian Rogers47a05882012-02-03 12:23:33 -0800833 }
834 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700835 failures_.push_back(error);
David Sehr709b0702016-10-13 09:12:37 -0700836 std::string location(StringPrintf("%s: [0x%X] ", dex_file_->PrettyMethod(dex_method_idx_).c_str(),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700837 work_insn_idx_));
Elena Sayapina78480ec2014-08-15 15:52:42 +0700838 std::ostringstream* failure_message = new std::ostringstream(location, std::ostringstream::ate);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700839 failure_messages_.push_back(failure_message);
840 return *failure_message;
841}
842
Ian Rogers576ca0c2014-06-06 15:58:22 -0700843std::ostream& MethodVerifier::LogVerifyInfo() {
David Sehr709b0702016-10-13 09:12:37 -0700844 return info_messages_ << "VFY: " << dex_file_->PrettyMethod(dex_method_idx_)
Ian Rogers576ca0c2014-06-06 15:58:22 -0700845 << '[' << reinterpret_cast<void*>(work_insn_idx_) << "] : ";
846}
847
Ian Rogersad0b3a32012-04-16 14:50:24 -0700848void MethodVerifier::PrependToLastFailMessage(std::string prepend) {
849 size_t failure_num = failure_messages_.size();
850 DCHECK_NE(failure_num, 0U);
851 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
852 prepend += last_fail_message->str();
Elena Sayapina78480ec2014-08-15 15:52:42 +0700853 failure_messages_[failure_num - 1] = new std::ostringstream(prepend, std::ostringstream::ate);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700854 delete last_fail_message;
855}
856
Vladimir Marko5c657fe2016-11-03 15:12:29 +0000857void MethodVerifier::AppendToLastFailMessage(const std::string& append) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700858 size_t failure_num = failure_messages_.size();
859 DCHECK_NE(failure_num, 0U);
860 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
861 (*last_fail_message) << append;
Ian Rogers47a05882012-02-03 12:23:33 -0800862}
863
Ian Rogers776ac1f2012-04-13 23:36:36 -0700864bool MethodVerifier::ComputeWidthsAndCountOps() {
jeffhaobdb76512011-09-07 11:43:16 -0700865 size_t new_instance_count = 0;
866 size_t monitor_enter_count = 0;
jeffhaobdb76512011-09-07 11:43:16 -0700867
Mathieu Chartieraf7c9022017-10-27 09:42:46 -0700868 // We can't assume the instruction is well formed, handle the case where calculating the size
869 // goes past the end of the code item.
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -0800870 SafeDexInstructionIterator it(code_item_accessor_.begin(), code_item_accessor_.end());
871 for ( ; !it.IsErrorState() && it < code_item_accessor_.end(); ++it) {
Mathieu Chartieraf7c9022017-10-27 09:42:46 -0700872 // In case the instruction goes past the end of the code item, make sure to not process it.
873 SafeDexInstructionIterator next = it;
874 ++next;
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -0800875 if (next.IsErrorState()) {
Mathieu Chartieraf7c9022017-10-27 09:42:46 -0700876 break;
877 }
878 Instruction::Code opcode = it->Opcode();
Ian Rogersa9a82542013-10-04 11:17:26 -0700879 switch (opcode) {
880 case Instruction::APUT_OBJECT:
881 case Instruction::CHECK_CAST:
882 has_check_casts_ = true;
883 break;
884 case Instruction::INVOKE_VIRTUAL:
885 case Instruction::INVOKE_VIRTUAL_RANGE:
886 case Instruction::INVOKE_INTERFACE:
887 case Instruction::INVOKE_INTERFACE_RANGE:
888 has_virtual_or_interface_invokes_ = true;
889 break;
890 case Instruction::MONITOR_ENTER:
891 monitor_enter_count++;
892 break;
893 case Instruction::NEW_INSTANCE:
894 new_instance_count++;
895 break;
896 default:
897 break;
jeffhaobdb76512011-09-07 11:43:16 -0700898 }
Mathieu Chartier2b2bef22017-10-26 17:10:19 -0700899 GetInstructionFlags(it.DexPc()).SetIsOpcode();
jeffhaobdb76512011-09-07 11:43:16 -0700900 }
901
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -0800902 if (it != code_item_accessor_.end()) {
903 const size_t insns_size = code_item_accessor_.InsnsSizeInCodeUnits();
jeffhaod5347e02012-03-22 17:25:05 -0700904 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "code did not end where expected ("
Mathieu Chartier2b2bef22017-10-26 17:10:19 -0700905 << it.DexPc() << " vs. " << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700906 return false;
907 }
908
Ian Rogersd81871c2011-10-03 13:57:23 -0700909 new_instance_count_ = new_instance_count;
910 monitor_enter_count_ = monitor_enter_count;
jeffhaobdb76512011-09-07 11:43:16 -0700911 return true;
912}
913
Ian Rogers776ac1f2012-04-13 23:36:36 -0700914bool MethodVerifier::ScanTryCatchBlocks() {
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -0800915 const uint32_t tries_size = code_item_accessor_.TriesSize();
jeffhaobdb76512011-09-07 11:43:16 -0700916 if (tries_size == 0) {
917 return true;
918 }
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -0800919 const uint32_t insns_size = code_item_accessor_.InsnsSizeInCodeUnits();
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800920 for (const dex::TryItem& try_item : code_item_accessor_.TryItems()) {
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -0800921 const uint32_t start = try_item.start_addr_;
922 const uint32_t end = start + try_item.insn_count_;
jeffhaobdb76512011-09-07 11:43:16 -0700923 if ((start >= end) || (start >= insns_size) || (end > insns_size)) {
jeffhaod5347e02012-03-22 17:25:05 -0700924 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad exception entry: startAddr=" << start
925 << " endAddr=" << end << " (size=" << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700926 return false;
927 }
Mathieu Chartierde40d472015-10-15 17:47:48 -0700928 if (!GetInstructionFlags(start).IsOpcode()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700929 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
930 << "'try' block starts inside an instruction (" << start << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700931 return false;
932 }
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -0800933 DexInstructionIterator end_it(code_item_accessor_.Insns(), end);
934 for (DexInstructionIterator it(code_item_accessor_.Insns(), start); it < end_it; ++it) {
935 GetInstructionFlags(it.DexPc()).SetInTry();
jeffhaobdb76512011-09-07 11:43:16 -0700936 }
937 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800938 // Iterate over each of the handlers to verify target addresses.
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -0800939 const uint8_t* handlers_ptr = code_item_accessor_.GetCatchHandlerData();
940 const uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700941 ClassLinker* linker = Runtime::Current()->GetClassLinker();
jeffhaobdb76512011-09-07 11:43:16 -0700942 for (uint32_t idx = 0; idx < handlers_size; idx++) {
Ian Rogers0571d352011-11-03 19:51:38 -0700943 CatchHandlerIterator iterator(handlers_ptr);
944 for (; iterator.HasNext(); iterator.Next()) {
Igor Murashkin2ffb7032017-11-08 13:35:21 -0800945 uint32_t dex_pc = iterator.GetHandlerAddress();
Mathieu Chartierde40d472015-10-15 17:47:48 -0700946 if (!GetInstructionFlags(dex_pc).IsOpcode()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700947 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
948 << "exception handler starts at bad address (" << dex_pc << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700949 return false;
950 }
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -0800951 if (!CheckNotMoveResult(code_item_accessor_.Insns(), dex_pc)) {
Stephen Kyle9bc61992014-09-22 13:53:15 +0100952 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
953 << "exception handler begins with move-result* (" << dex_pc << ")";
954 return false;
955 }
Mathieu Chartierde40d472015-10-15 17:47:48 -0700956 GetInstructionFlags(dex_pc).SetBranchTarget();
Ian Rogers28ad40d2011-10-27 15:19:26 -0700957 // Ensure exception types are resolved so that they don't need resolution to be delivered,
958 // unresolved exception types will be ignored by exception delivery
Andreas Gampea5b09a62016-11-17 15:21:22 -0800959 if (iterator.GetHandlerTypeIndex().IsValid()) {
Vladimir Marko666ee3d2017-12-11 18:37:36 +0000960 ObjPtr<mirror::Class> exception_type =
961 linker->ResolveType(iterator.GetHandlerTypeIndex(), dex_cache_, class_loader_);
Ian Rogers7b078e82014-09-10 14:44:24 -0700962 if (exception_type == nullptr) {
963 DCHECK(self_->IsExceptionPending());
964 self_->ClearException();
Ian Rogers28ad40d2011-10-27 15:19:26 -0700965 }
966 }
jeffhaobdb76512011-09-07 11:43:16 -0700967 }
Ian Rogers0571d352011-11-03 19:51:38 -0700968 handlers_ptr = iterator.EndDataPointer();
jeffhaobdb76512011-09-07 11:43:16 -0700969 }
jeffhaobdb76512011-09-07 11:43:16 -0700970 return true;
971}
972
Andreas Gampebf1cb772017-05-15 15:39:00 -0700973template <bool kAllowRuntimeOnlyInstructions>
Ian Rogers776ac1f2012-04-13 23:36:36 -0700974bool MethodVerifier::VerifyInstructions() {
Ian Rogers0c7abda2012-09-19 13:33:42 -0700975 /* Flag the start of the method as a branch target, and a GC point due to stack overflow errors */
Mathieu Chartierde40d472015-10-15 17:47:48 -0700976 GetInstructionFlags(0).SetBranchTarget();
977 GetInstructionFlags(0).SetCompileTimeInfoPoint();
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -0800978 for (const DexInstructionPcPair& inst : code_item_accessor_) {
Mathieu Chartier2b2bef22017-10-26 17:10:19 -0700979 const uint32_t dex_pc = inst.DexPc();
980 if (!VerifyInstruction<kAllowRuntimeOnlyInstructions>(&inst.Inst(), dex_pc)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700981 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -0700982 return false;
983 }
984 /* Flag instructions that are garbage collection points */
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700985 // All invoke points are marked as "Throw" points already.
986 // We are relying on this to also count all the invokes as interesting.
Vladimir Marko8b858e12014-11-27 14:52:37 +0000987 if (inst->IsBranch()) {
Mathieu Chartierde40d472015-10-15 17:47:48 -0700988 GetInstructionFlags(dex_pc).SetCompileTimeInfoPoint();
Vladimir Marko8b858e12014-11-27 14:52:37 +0000989 // The compiler also needs safepoints for fall-through to loop heads.
990 // Such a loop head must be a target of a branch.
991 int32_t offset = 0;
992 bool cond, self_ok;
993 bool target_ok = GetBranchOffset(dex_pc, &offset, &cond, &self_ok);
994 DCHECK(target_ok);
Mathieu Chartierde40d472015-10-15 17:47:48 -0700995 GetInstructionFlags(dex_pc + offset).SetCompileTimeInfoPoint();
Vladimir Marko8b858e12014-11-27 14:52:37 +0000996 } else if (inst->IsSwitch() || inst->IsThrow()) {
Mathieu Chartierde40d472015-10-15 17:47:48 -0700997 GetInstructionFlags(dex_pc).SetCompileTimeInfoPoint();
Ian Rogersb8c78592013-07-25 23:52:52 +0000998 } else if (inst->IsReturn()) {
Mathieu Chartierde40d472015-10-15 17:47:48 -0700999 GetInstructionFlags(dex_pc).SetCompileTimeInfoPointAndReturn();
Ian Rogersd81871c2011-10-03 13:57:23 -07001000 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001001 }
1002 return true;
1003}
1004
Andreas Gampebf1cb772017-05-15 15:39:00 -07001005template <bool kAllowRuntimeOnlyInstructions>
1006bool MethodVerifier::VerifyInstruction(const Instruction* inst, uint32_t code_offset) {
Andreas Gampee05cc662017-05-15 10:17:30 -07001007 if (Instruction::kHaveExperimentalInstructions && UNLIKELY(inst->IsExperimental())) {
Igor Murashkin4d7b75f2015-07-21 17:03:36 -07001008 // Experimental instructions don't yet have verifier support implementation.
1009 // While it is possible to use them by themselves, when we try to use stable instructions
1010 // with a virtual register that was created by an experimental instruction,
1011 // the data flow analysis will fail.
1012 Fail(VERIFY_ERROR_FORCE_INTERPRETER)
1013 << "experimental instruction is not supported by verifier; skipping verification";
1014 have_pending_experimental_failure_ = true;
1015 return false;
1016 }
1017
Ian Rogersd81871c2011-10-03 13:57:23 -07001018 bool result = true;
1019 switch (inst->GetVerifyTypeArgumentA()) {
1020 case Instruction::kVerifyRegA:
Ian Rogers29a26482014-05-02 15:27:29 -07001021 result = result && CheckRegisterIndex(inst->VRegA());
Ian Rogersd81871c2011-10-03 13:57:23 -07001022 break;
1023 case Instruction::kVerifyRegAWide:
Ian Rogers29a26482014-05-02 15:27:29 -07001024 result = result && CheckWideRegisterIndex(inst->VRegA());
Ian Rogersd81871c2011-10-03 13:57:23 -07001025 break;
1026 }
1027 switch (inst->GetVerifyTypeArgumentB()) {
1028 case Instruction::kVerifyRegB:
Ian Rogers29a26482014-05-02 15:27:29 -07001029 result = result && CheckRegisterIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -07001030 break;
1031 case Instruction::kVerifyRegBField:
Ian Rogers29a26482014-05-02 15:27:29 -07001032 result = result && CheckFieldIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -07001033 break;
1034 case Instruction::kVerifyRegBMethod:
Ian Rogers29a26482014-05-02 15:27:29 -07001035 result = result && CheckMethodIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -07001036 break;
1037 case Instruction::kVerifyRegBNewInstance:
Andreas Gampea5b09a62016-11-17 15:21:22 -08001038 result = result && CheckNewInstance(dex::TypeIndex(inst->VRegB()));
Ian Rogersd81871c2011-10-03 13:57:23 -07001039 break;
1040 case Instruction::kVerifyRegBString:
Ian Rogers29a26482014-05-02 15:27:29 -07001041 result = result && CheckStringIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -07001042 break;
1043 case Instruction::kVerifyRegBType:
Andreas Gampea5b09a62016-11-17 15:21:22 -08001044 result = result && CheckTypeIndex(dex::TypeIndex(inst->VRegB()));
Ian Rogersd81871c2011-10-03 13:57:23 -07001045 break;
1046 case Instruction::kVerifyRegBWide:
Ian Rogers29a26482014-05-02 15:27:29 -07001047 result = result && CheckWideRegisterIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -07001048 break;
Orion Hodson2e599942017-09-22 16:17:41 +01001049 case Instruction::kVerifyRegBCallSite:
1050 result = result && CheckCallSiteIndex(inst->VRegB());
1051 break;
1052 case Instruction::kVerifyRegBMethodHandle:
1053 result = result && CheckMethodHandleIndex(inst->VRegB());
1054 break;
1055 case Instruction::kVerifyRegBPrototype:
1056 result = result && CheckPrototypeIndex(inst->VRegB());
1057 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001058 }
1059 switch (inst->GetVerifyTypeArgumentC()) {
1060 case Instruction::kVerifyRegC:
Ian Rogers29a26482014-05-02 15:27:29 -07001061 result = result && CheckRegisterIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -07001062 break;
1063 case Instruction::kVerifyRegCField:
Ian Rogers29a26482014-05-02 15:27:29 -07001064 result = result && CheckFieldIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -07001065 break;
1066 case Instruction::kVerifyRegCNewArray:
Andreas Gampea5b09a62016-11-17 15:21:22 -08001067 result = result && CheckNewArray(dex::TypeIndex(inst->VRegC()));
Ian Rogersd81871c2011-10-03 13:57:23 -07001068 break;
1069 case Instruction::kVerifyRegCType:
Andreas Gampea5b09a62016-11-17 15:21:22 -08001070 result = result && CheckTypeIndex(dex::TypeIndex(inst->VRegC()));
Ian Rogersd81871c2011-10-03 13:57:23 -07001071 break;
1072 case Instruction::kVerifyRegCWide:
Ian Rogers29a26482014-05-02 15:27:29 -07001073 result = result && CheckWideRegisterIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -07001074 break;
1075 }
Orion Hodsoncfa325e2016-10-13 10:25:54 +01001076 switch (inst->GetVerifyTypeArgumentH()) {
1077 case Instruction::kVerifyRegHPrototype:
1078 result = result && CheckPrototypeIndex(inst->VRegH());
1079 break;
1080 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001081 switch (inst->GetVerifyExtraFlags()) {
1082 case Instruction::kVerifyArrayData:
1083 result = result && CheckArrayData(code_offset);
1084 break;
1085 case Instruction::kVerifyBranchTarget:
1086 result = result && CheckBranchTarget(code_offset);
1087 break;
1088 case Instruction::kVerifySwitchTargets:
1089 result = result && CheckSwitchTargets(code_offset);
1090 break;
Andreas Gampec3314312014-06-19 18:13:29 -07001091 case Instruction::kVerifyVarArgNonZero:
1092 // Fall-through.
Ian Rogers29a26482014-05-02 15:27:29 -07001093 case Instruction::kVerifyVarArg: {
Taiju Tsuiki29498a22015-04-13 14:21:00 +09001094 // Instructions that can actually return a negative value shouldn't have this flag.
1095 uint32_t v_a = dchecked_integral_cast<uint32_t>(inst->VRegA());
1096 if ((inst->GetVerifyExtraFlags() == Instruction::kVerifyVarArgNonZero && v_a == 0) ||
1097 v_a > Instruction::kMaxVarArgRegs) {
1098 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << v_a << ") in "
Andreas Gampec3314312014-06-19 18:13:29 -07001099 "non-range invoke";
1100 return false;
1101 }
Taiju Tsuiki29498a22015-04-13 14:21:00 +09001102
Ian Rogers29a26482014-05-02 15:27:29 -07001103 uint32_t args[Instruction::kMaxVarArgRegs];
1104 inst->GetVarArgs(args);
Taiju Tsuiki29498a22015-04-13 14:21:00 +09001105 result = result && CheckVarArgRegs(v_a, args);
Ian Rogersd81871c2011-10-03 13:57:23 -07001106 break;
Ian Rogers29a26482014-05-02 15:27:29 -07001107 }
Andreas Gampec3314312014-06-19 18:13:29 -07001108 case Instruction::kVerifyVarArgRangeNonZero:
1109 // Fall-through.
Ian Rogersd81871c2011-10-03 13:57:23 -07001110 case Instruction::kVerifyVarArgRange:
Andreas Gampec3314312014-06-19 18:13:29 -07001111 if (inst->GetVerifyExtraFlags() == Instruction::kVerifyVarArgRangeNonZero &&
1112 inst->VRegA() <= 0) {
1113 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << inst->VRegA() << ") in "
1114 "range invoke";
1115 return false;
1116 }
Ian Rogers29a26482014-05-02 15:27:29 -07001117 result = result && CheckVarArgRangeRegs(inst->VRegA(), inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -07001118 break;
1119 case Instruction::kVerifyError:
jeffhaod5347e02012-03-22 17:25:05 -07001120 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected opcode " << inst->Name();
Ian Rogersd81871c2011-10-03 13:57:23 -07001121 result = false;
1122 break;
1123 }
Andreas Gampebf1cb772017-05-15 15:39:00 -07001124 if (!kAllowRuntimeOnlyInstructions && inst->GetVerifyIsRuntimeOnly()) {
Ian Rogers5fb22a92014-06-13 10:31:28 -07001125 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "opcode only expected at runtime " << inst->Name();
1126 result = false;
1127 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001128 return result;
1129}
1130
Ian Rogers7b078e82014-09-10 14:44:24 -07001131inline bool MethodVerifier::CheckRegisterIndex(uint32_t idx) {
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001132 if (UNLIKELY(idx >= code_item_accessor_.RegistersSize())) {
jeffhaod5347e02012-03-22 17:25:05 -07001133 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register index out of range (" << idx << " >= "
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001134 << code_item_accessor_.RegistersSize() << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001135 return false;
1136 }
1137 return true;
1138}
1139
Ian Rogers7b078e82014-09-10 14:44:24 -07001140inline bool MethodVerifier::CheckWideRegisterIndex(uint32_t idx) {
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001141 if (UNLIKELY(idx + 1 >= code_item_accessor_.RegistersSize())) {
jeffhaod5347e02012-03-22 17:25:05 -07001142 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "wide register index out of range (" << idx
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001143 << "+1 >= " << code_item_accessor_.RegistersSize() << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001144 return false;
1145 }
1146 return true;
1147}
1148
Orion Hodson2e599942017-09-22 16:17:41 +01001149inline bool MethodVerifier::CheckCallSiteIndex(uint32_t idx) {
1150 uint32_t limit = dex_file_->NumCallSiteIds();
1151 if (UNLIKELY(idx >= limit)) {
1152 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad call site index " << idx << " (max "
1153 << limit << ")";
1154 return false;
1155 }
1156 return true;
1157}
1158
Ian Rogers7b078e82014-09-10 14:44:24 -07001159inline bool MethodVerifier::CheckFieldIndex(uint32_t idx) {
Andreas Gampe29e81422017-05-15 16:29:32 -07001160 if (UNLIKELY(idx >= dex_file_->GetHeader().field_ids_size_)) {
jeffhaod5347e02012-03-22 17:25:05 -07001161 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad field index " << idx << " (max "
1162 << dex_file_->GetHeader().field_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001163 return false;
1164 }
1165 return true;
1166}
1167
Ian Rogers7b078e82014-09-10 14:44:24 -07001168inline bool MethodVerifier::CheckMethodIndex(uint32_t idx) {
Andreas Gampe29e81422017-05-15 16:29:32 -07001169 if (UNLIKELY(idx >= dex_file_->GetHeader().method_ids_size_)) {
jeffhaod5347e02012-03-22 17:25:05 -07001170 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad method index " << idx << " (max "
1171 << dex_file_->GetHeader().method_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001172 return false;
1173 }
1174 return true;
1175}
1176
Orion Hodson2e599942017-09-22 16:17:41 +01001177inline bool MethodVerifier::CheckMethodHandleIndex(uint32_t idx) {
1178 uint32_t limit = dex_file_->NumMethodHandles();
1179 if (UNLIKELY(idx >= limit)) {
1180 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad method handle index " << idx << " (max "
1181 << limit << ")";
1182 return false;
1183 }
1184 return true;
1185}
1186
Andreas Gampea5b09a62016-11-17 15:21:22 -08001187inline bool MethodVerifier::CheckNewInstance(dex::TypeIndex idx) {
Andreas Gampe29e81422017-05-15 16:29:32 -07001188 if (UNLIKELY(idx.index_ >= dex_file_->GetHeader().type_ids_size_)) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08001189 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx.index_ << " (max "
jeffhaod5347e02012-03-22 17:25:05 -07001190 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001191 return false;
1192 }
1193 // We don't need the actual class, just a pointer to the class name.
Ian Rogers0571d352011-11-03 19:51:38 -07001194 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Andreas Gampe29e81422017-05-15 16:29:32 -07001195 if (UNLIKELY(descriptor[0] != 'L')) {
jeffhaod5347e02012-03-22 17:25:05 -07001196 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "can't call new-instance on type '" << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001197 return false;
Andreas Gampe29e81422017-05-15 16:29:32 -07001198 } else if (UNLIKELY(strcmp(descriptor, "Ljava/lang/Class;") == 0)) {
Aart Bikdb698f12016-07-25 17:52:22 -07001199 // An unlikely new instance on Class is not allowed. Fall back to interpreter to ensure an
1200 // exception is thrown when this statement is executed (compiled code would not do that).
1201 Fail(VERIFY_ERROR_INSTANTIATION);
Ian Rogersd81871c2011-10-03 13:57:23 -07001202 }
1203 return true;
1204}
1205
Orion Hodsoncfa325e2016-10-13 10:25:54 +01001206inline bool MethodVerifier::CheckPrototypeIndex(uint32_t idx) {
Andreas Gampe29e81422017-05-15 16:29:32 -07001207 if (UNLIKELY(idx >= dex_file_->GetHeader().proto_ids_size_)) {
Orion Hodsoncfa325e2016-10-13 10:25:54 +01001208 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad prototype index " << idx << " (max "
1209 << dex_file_->GetHeader().proto_ids_size_ << ")";
1210 return false;
1211 }
1212 return true;
1213}
1214
Ian Rogers7b078e82014-09-10 14:44:24 -07001215inline bool MethodVerifier::CheckStringIndex(uint32_t idx) {
Andreas Gampe29e81422017-05-15 16:29:32 -07001216 if (UNLIKELY(idx >= dex_file_->GetHeader().string_ids_size_)) {
jeffhaod5347e02012-03-22 17:25:05 -07001217 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad string index " << idx << " (max "
1218 << dex_file_->GetHeader().string_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001219 return false;
1220 }
1221 return true;
1222}
1223
Andreas Gampea5b09a62016-11-17 15:21:22 -08001224inline bool MethodVerifier::CheckTypeIndex(dex::TypeIndex idx) {
Andreas Gampe29e81422017-05-15 16:29:32 -07001225 if (UNLIKELY(idx.index_ >= dex_file_->GetHeader().type_ids_size_)) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08001226 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx.index_ << " (max "
jeffhaod5347e02012-03-22 17:25:05 -07001227 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001228 return false;
1229 }
1230 return true;
1231}
1232
Andreas Gampea5b09a62016-11-17 15:21:22 -08001233bool MethodVerifier::CheckNewArray(dex::TypeIndex idx) {
Andreas Gampe29e81422017-05-15 16:29:32 -07001234 if (UNLIKELY(idx.index_ >= dex_file_->GetHeader().type_ids_size_)) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08001235 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx.index_ << " (max "
jeffhaod5347e02012-03-22 17:25:05 -07001236 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001237 return false;
1238 }
1239 int bracket_count = 0;
Ian Rogers0571d352011-11-03 19:51:38 -07001240 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -07001241 const char* cp = descriptor;
1242 while (*cp++ == '[') {
1243 bracket_count++;
1244 }
Andreas Gampe29e81422017-05-15 16:29:32 -07001245 if (UNLIKELY(bracket_count == 0)) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001246 /* The given class must be an array type. */
Brian Carlstrom93c33962013-07-26 10:37:43 -07001247 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1248 << "can't new-array class '" << descriptor << "' (not an array)";
Ian Rogersd81871c2011-10-03 13:57:23 -07001249 return false;
Andreas Gampe29e81422017-05-15 16:29:32 -07001250 } else if (UNLIKELY(bracket_count > 255)) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001251 /* It is illegal to create an array of more than 255 dimensions. */
Brian Carlstrom93c33962013-07-26 10:37:43 -07001252 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1253 << "can't new-array class '" << descriptor << "' (exceeds limit)";
Ian Rogersd81871c2011-10-03 13:57:23 -07001254 return false;
1255 }
1256 return true;
1257}
1258
Ian Rogers776ac1f2012-04-13 23:36:36 -07001259bool MethodVerifier::CheckArrayData(uint32_t cur_offset) {
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001260 const uint32_t insn_count = code_item_accessor_.InsnsSizeInCodeUnits();
1261 const uint16_t* insns = code_item_accessor_.Insns() + cur_offset;
Ian Rogersd81871c2011-10-03 13:57:23 -07001262 const uint16_t* array_data;
1263 int32_t array_data_offset;
1264
1265 DCHECK_LT(cur_offset, insn_count);
1266 /* make sure the start of the array data table is in range */
Andreas Gampe53de99c2015-08-17 13:43:55 -07001267 array_data_offset = insns[1] | (static_cast<int32_t>(insns[2]) << 16);
Andreas Gampe29e81422017-05-15 16:29:32 -07001268 if (UNLIKELY(static_cast<int32_t>(cur_offset) + array_data_offset < 0 ||
1269 cur_offset + array_data_offset + 2 >= insn_count)) {
jeffhaod5347e02012-03-22 17:25:05 -07001270 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data start: at " << cur_offset
Brian Carlstrom93c33962013-07-26 10:37:43 -07001271 << ", data offset " << array_data_offset
1272 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -07001273 return false;
1274 }
1275 /* offset to array data table is a relative branch-style offset */
1276 array_data = insns + array_data_offset;
Andreas Gampe57c47582015-07-01 22:05:59 -07001277 // Make sure the table is at an even dex pc, that is, 32-bit aligned.
Andreas Gampe29e81422017-05-15 16:29:32 -07001278 if (UNLIKELY(!IsAligned<4>(array_data))) {
jeffhaod5347e02012-03-22 17:25:05 -07001279 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned array data table: at " << cur_offset
1280 << ", data offset " << array_data_offset;
Ian Rogersd81871c2011-10-03 13:57:23 -07001281 return false;
1282 }
Andreas Gampe57c47582015-07-01 22:05:59 -07001283 // Make sure the array-data is marked as an opcode. This ensures that it was reached when
1284 // traversing the code item linearly. It is an approximation for a by-spec padding value.
Andreas Gampe29e81422017-05-15 16:29:32 -07001285 if (UNLIKELY(!GetInstructionFlags(cur_offset + array_data_offset).IsOpcode())) {
Andreas Gampe57c47582015-07-01 22:05:59 -07001286 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array data table at " << cur_offset
1287 << ", data offset " << array_data_offset
1288 << " not correctly visited, probably bad padding.";
1289 return false;
1290 }
1291
Ian Rogersd81871c2011-10-03 13:57:23 -07001292 uint32_t value_width = array_data[1];
Elliott Hughes398f64b2012-03-26 18:05:48 -07001293 uint32_t value_count = *reinterpret_cast<const uint32_t*>(&array_data[2]);
Ian Rogersd81871c2011-10-03 13:57:23 -07001294 uint32_t table_size = 4 + (value_width * value_count + 1) / 2;
1295 /* make sure the end of the switch is in range */
Andreas Gampe29e81422017-05-15 16:29:32 -07001296 if (UNLIKELY(cur_offset + array_data_offset + table_size > insn_count)) {
jeffhaod5347e02012-03-22 17:25:05 -07001297 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data end: at " << cur_offset
1298 << ", data offset " << array_data_offset << ", end "
1299 << cur_offset + array_data_offset + table_size
1300 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -07001301 return false;
1302 }
1303 return true;
1304}
1305
Ian Rogers776ac1f2012-04-13 23:36:36 -07001306bool MethodVerifier::CheckBranchTarget(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001307 int32_t offset;
1308 bool isConditional, selfOkay;
1309 if (!GetBranchOffset(cur_offset, &offset, &isConditional, &selfOkay)) {
1310 return false;
1311 }
Andreas Gampe29e81422017-05-15 16:29:32 -07001312 if (UNLIKELY(!selfOkay && offset == 0)) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001313 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch offset of zero not allowed at"
1314 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -07001315 return false;
1316 }
Elliott Hughes81ff3182012-03-23 20:35:56 -07001317 // Check for 32-bit overflow. This isn't strictly necessary if we can depend on the runtime
1318 // to have identical "wrap-around" behavior, but it's unwise to depend on that.
Andreas Gampe29e81422017-05-15 16:29:32 -07001319 if (UNLIKELY(((int64_t) cur_offset + (int64_t) offset) != (int64_t) (cur_offset + offset))) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001320 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch target overflow "
1321 << reinterpret_cast<void*>(cur_offset) << " +" << offset;
Ian Rogersd81871c2011-10-03 13:57:23 -07001322 return false;
1323 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001324 int32_t abs_offset = cur_offset + offset;
Andreas Gampe29e81422017-05-15 16:29:32 -07001325 if (UNLIKELY(abs_offset < 0 ||
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001326 (uint32_t) abs_offset >= code_item_accessor_.InsnsSizeInCodeUnits() ||
Andreas Gampe29e81422017-05-15 16:29:32 -07001327 !GetInstructionFlags(abs_offset).IsOpcode())) {
jeffhaod5347e02012-03-22 17:25:05 -07001328 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid branch target " << offset << " (-> "
Elliott Hughes398f64b2012-03-26 18:05:48 -07001329 << reinterpret_cast<void*>(abs_offset) << ") at "
1330 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -07001331 return false;
1332 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07001333 GetInstructionFlags(abs_offset).SetBranchTarget();
Ian Rogersd81871c2011-10-03 13:57:23 -07001334 return true;
1335}
1336
Ian Rogers776ac1f2012-04-13 23:36:36 -07001337bool MethodVerifier::GetBranchOffset(uint32_t cur_offset, int32_t* pOffset, bool* pConditional,
Ian Rogersd81871c2011-10-03 13:57:23 -07001338 bool* selfOkay) {
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001339 const uint16_t* insns = code_item_accessor_.Insns() + cur_offset;
Ian Rogersd81871c2011-10-03 13:57:23 -07001340 *pConditional = false;
1341 *selfOkay = false;
jeffhaoba5ebb92011-08-25 17:24:37 -07001342 switch (*insns & 0xff) {
1343 case Instruction::GOTO:
1344 *pOffset = ((int16_t) *insns) >> 8;
jeffhaoba5ebb92011-08-25 17:24:37 -07001345 break;
1346 case Instruction::GOTO_32:
1347 *pOffset = insns[1] | (((uint32_t) insns[2]) << 16);
jeffhaoba5ebb92011-08-25 17:24:37 -07001348 *selfOkay = true;
1349 break;
1350 case Instruction::GOTO_16:
1351 *pOffset = (int16_t) insns[1];
jeffhaoba5ebb92011-08-25 17:24:37 -07001352 break;
1353 case Instruction::IF_EQ:
1354 case Instruction::IF_NE:
1355 case Instruction::IF_LT:
1356 case Instruction::IF_GE:
1357 case Instruction::IF_GT:
1358 case Instruction::IF_LE:
1359 case Instruction::IF_EQZ:
1360 case Instruction::IF_NEZ:
1361 case Instruction::IF_LTZ:
1362 case Instruction::IF_GEZ:
1363 case Instruction::IF_GTZ:
1364 case Instruction::IF_LEZ:
1365 *pOffset = (int16_t) insns[1];
1366 *pConditional = true;
jeffhaoba5ebb92011-08-25 17:24:37 -07001367 break;
1368 default:
1369 return false;
jeffhaoba5ebb92011-08-25 17:24:37 -07001370 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001371 return true;
1372}
1373
Ian Rogers776ac1f2012-04-13 23:36:36 -07001374bool MethodVerifier::CheckSwitchTargets(uint32_t cur_offset) {
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001375 const uint32_t insn_count = code_item_accessor_.InsnsSizeInCodeUnits();
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001376 DCHECK_LT(cur_offset, insn_count);
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001377 const uint16_t* insns = code_item_accessor_.Insns() + cur_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001378 /* make sure the start of the switch is in range */
Andreas Gampe53de99c2015-08-17 13:43:55 -07001379 int32_t switch_offset = insns[1] | (static_cast<int32_t>(insns[2]) << 16);
Andreas Gampe29e81422017-05-15 16:29:32 -07001380 if (UNLIKELY(static_cast<int32_t>(cur_offset) + switch_offset < 0 ||
1381 cur_offset + switch_offset + 2 > insn_count)) {
jeffhaod5347e02012-03-22 17:25:05 -07001382 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch start: at " << cur_offset
Brian Carlstrom93c33962013-07-26 10:37:43 -07001383 << ", switch offset " << switch_offset
1384 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001385 return false;
1386 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001387 /* offset to switch table is a relative branch-style offset */
Ian Rogersd81871c2011-10-03 13:57:23 -07001388 const uint16_t* switch_insns = insns + switch_offset;
Andreas Gampe57c47582015-07-01 22:05:59 -07001389 // Make sure the table is at an even dex pc, that is, 32-bit aligned.
Andreas Gampe29e81422017-05-15 16:29:32 -07001390 if (UNLIKELY(!IsAligned<4>(switch_insns))) {
jeffhaod5347e02012-03-22 17:25:05 -07001391 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned switch table: at " << cur_offset
1392 << ", switch offset " << switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001393 return false;
1394 }
Andreas Gampe57c47582015-07-01 22:05:59 -07001395 // Make sure the switch data is marked as an opcode. This ensures that it was reached when
1396 // traversing the code item linearly. It is an approximation for a by-spec padding value.
Andreas Gampe29e81422017-05-15 16:29:32 -07001397 if (UNLIKELY(!GetInstructionFlags(cur_offset + switch_offset).IsOpcode())) {
Andreas Gampe57c47582015-07-01 22:05:59 -07001398 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "switch table at " << cur_offset
1399 << ", switch offset " << switch_offset
1400 << " not correctly visited, probably bad padding.";
1401 return false;
1402 }
1403
David Brazdil5469d342015-09-25 16:57:53 +01001404 bool is_packed_switch = (*insns & 0xff) == Instruction::PACKED_SWITCH;
1405
Ian Rogersd81871c2011-10-03 13:57:23 -07001406 uint32_t switch_count = switch_insns[1];
David Brazdil5469d342015-09-25 16:57:53 +01001407 int32_t targets_offset;
Ian Rogersd81871c2011-10-03 13:57:23 -07001408 uint16_t expected_signature;
David Brazdil5469d342015-09-25 16:57:53 +01001409 if (is_packed_switch) {
jeffhaoba5ebb92011-08-25 17:24:37 -07001410 /* 0=sig, 1=count, 2/3=firstKey */
1411 targets_offset = 4;
jeffhaoba5ebb92011-08-25 17:24:37 -07001412 expected_signature = Instruction::kPackedSwitchSignature;
1413 } else {
1414 /* 0=sig, 1=count, 2..count*2 = keys */
jeffhaoba5ebb92011-08-25 17:24:37 -07001415 targets_offset = 2 + 2 * switch_count;
1416 expected_signature = Instruction::kSparseSwitchSignature;
1417 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001418 uint32_t table_size = targets_offset + switch_count * 2;
Andreas Gampe29e81422017-05-15 16:29:32 -07001419 if (UNLIKELY(switch_insns[0] != expected_signature)) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001420 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1421 << StringPrintf("wrong signature for switch table (%x, wanted %x)",
1422 switch_insns[0], expected_signature);
jeffhaoba5ebb92011-08-25 17:24:37 -07001423 return false;
1424 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001425 /* make sure the end of the switch is in range */
Andreas Gampe29e81422017-05-15 16:29:32 -07001426 if (UNLIKELY(cur_offset + switch_offset + table_size > (uint32_t) insn_count)) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001427 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch end: at " << cur_offset
1428 << ", switch offset " << switch_offset
1429 << ", end " << (cur_offset + switch_offset + table_size)
jeffhaod5347e02012-03-22 17:25:05 -07001430 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001431 return false;
1432 }
David Brazdil5469d342015-09-25 16:57:53 +01001433
1434 constexpr int32_t keys_offset = 2;
1435 if (switch_count > 1) {
1436 if (is_packed_switch) {
1437 /* for a packed switch, verify that keys do not overflow int32 */
1438 int32_t first_key = switch_insns[keys_offset] | (switch_insns[keys_offset + 1] << 16);
1439 int32_t max_first_key =
1440 std::numeric_limits<int32_t>::max() - (static_cast<int32_t>(switch_count) - 1);
Andreas Gampe29e81422017-05-15 16:29:32 -07001441 if (UNLIKELY(first_key > max_first_key)) {
David Brazdil5469d342015-09-25 16:57:53 +01001442 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid packed switch: first_key=" << first_key
1443 << ", switch_count=" << switch_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001444 return false;
1445 }
David Brazdil5469d342015-09-25 16:57:53 +01001446 } else {
1447 /* for a sparse switch, verify the keys are in ascending order */
1448 int32_t last_key = switch_insns[keys_offset] | (switch_insns[keys_offset + 1] << 16);
1449 for (uint32_t targ = 1; targ < switch_count; targ++) {
1450 int32_t key =
1451 static_cast<int32_t>(switch_insns[keys_offset + targ * 2]) |
1452 static_cast<int32_t>(switch_insns[keys_offset + targ * 2 + 1] << 16);
Andreas Gampe29e81422017-05-15 16:29:32 -07001453 if (UNLIKELY(key <= last_key)) {
David Brazdil5469d342015-09-25 16:57:53 +01001454 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid sparse switch: last key=" << last_key
1455 << ", this=" << key;
1456 return false;
1457 }
1458 last_key = key;
1459 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001460 }
1461 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001462 /* verify each switch target */
Ian Rogersd81871c2011-10-03 13:57:23 -07001463 for (uint32_t targ = 0; targ < switch_count; targ++) {
Andreas Gampe53de99c2015-08-17 13:43:55 -07001464 int32_t offset = static_cast<int32_t>(switch_insns[targets_offset + targ * 2]) |
1465 static_cast<int32_t>(switch_insns[targets_offset + targ * 2 + 1] << 16);
Ian Rogersd81871c2011-10-03 13:57:23 -07001466 int32_t abs_offset = cur_offset + offset;
Andreas Gampe29e81422017-05-15 16:29:32 -07001467 if (UNLIKELY(abs_offset < 0 ||
1468 abs_offset >= static_cast<int32_t>(insn_count) ||
1469 !GetInstructionFlags(abs_offset).IsOpcode())) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001470 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch target " << offset
1471 << " (-> " << reinterpret_cast<void*>(abs_offset) << ") at "
1472 << reinterpret_cast<void*>(cur_offset)
1473 << "[" << targ << "]";
jeffhaoba5ebb92011-08-25 17:24:37 -07001474 return false;
1475 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07001476 GetInstructionFlags(abs_offset).SetBranchTarget();
Ian Rogersd81871c2011-10-03 13:57:23 -07001477 }
1478 return true;
1479}
1480
Ian Rogers776ac1f2012-04-13 23:36:36 -07001481bool MethodVerifier::CheckVarArgRegs(uint32_t vA, uint32_t arg[]) {
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001482 uint16_t registers_size = code_item_accessor_.RegistersSize();
Ian Rogersd81871c2011-10-03 13:57:23 -07001483 for (uint32_t idx = 0; idx < vA; idx++) {
Andreas Gampe29e81422017-05-15 16:29:32 -07001484 if (UNLIKELY(arg[idx] >= registers_size)) {
jeffhaod5347e02012-03-22 17:25:05 -07001485 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index (" << arg[idx]
1486 << ") in non-range invoke (>= " << registers_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001487 return false;
1488 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001489 }
1490
1491 return true;
1492}
1493
Ian Rogers776ac1f2012-04-13 23:36:36 -07001494bool MethodVerifier::CheckVarArgRangeRegs(uint32_t vA, uint32_t vC) {
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001495 uint16_t registers_size = code_item_accessor_.RegistersSize();
Ian Rogersd81871c2011-10-03 13:57:23 -07001496 // vA/vC are unsigned 8-bit/16-bit quantities for /range instructions, so there's no risk of
1497 // integer overflow when adding them here.
Andreas Gampe29e81422017-05-15 16:29:32 -07001498 if (UNLIKELY(vA + vC > registers_size)) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001499 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index " << vA << "+" << vC
1500 << " in range invoke (> " << registers_size << ")";
jeffhaoba5ebb92011-08-25 17:24:37 -07001501 return false;
1502 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001503 return true;
1504}
1505
Ian Rogers776ac1f2012-04-13 23:36:36 -07001506bool MethodVerifier::VerifyCodeFlow() {
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001507 const uint16_t registers_size = code_item_accessor_.RegistersSize();
jeffhaobdb76512011-09-07 11:43:16 -07001508
Ian Rogersd81871c2011-10-03 13:57:23 -07001509 /* Create and initialize table holding register status */
Brian Carlstrom93c33962013-07-26 10:37:43 -07001510 reg_table_.Init(kTrackCompilerInterestPoints,
1511 insn_flags_.get(),
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001512 code_item_accessor_.InsnsSizeInCodeUnits(),
Brian Carlstrom93c33962013-07-26 10:37:43 -07001513 registers_size,
1514 this);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07001515
Ian Rogersd0fbd852013-09-24 18:17:04 -07001516 work_line_.reset(RegisterLine::Create(registers_size, this));
1517 saved_line_.reset(RegisterLine::Create(registers_size, this));
jeffhaobdb76512011-09-07 11:43:16 -07001518
Ian Rogersd81871c2011-10-03 13:57:23 -07001519 /* Initialize register types of method arguments. */
1520 if (!SetTypesFromSignature()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001521 DCHECK_NE(failures_.size(), 0U);
1522 std::string prepend("Bad signature in ");
David Sehr709b0702016-10-13 09:12:37 -07001523 prepend += dex_file_->PrettyMethod(dex_method_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001524 PrependToLastFailMessage(prepend);
Ian Rogersd81871c2011-10-03 13:57:23 -07001525 return false;
1526 }
Andreas Gamped5ad72f2015-06-26 17:33:47 -07001527 // We may have a runtime failure here, clear.
1528 have_pending_runtime_throw_failure_ = false;
1529
Ian Rogersd81871c2011-10-03 13:57:23 -07001530 /* Perform code flow verification. */
1531 if (!CodeFlowVerifyMethod()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001532 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -07001533 return false;
jeffhaobdb76512011-09-07 11:43:16 -07001534 }
jeffhaobdb76512011-09-07 11:43:16 -07001535 return true;
1536}
1537
Ian Rogersad0b3a32012-04-16 14:50:24 -07001538std::ostream& MethodVerifier::DumpFailures(std::ostream& os) {
1539 DCHECK_EQ(failures_.size(), failure_messages_.size());
Jeff Hao4137f482013-11-22 11:44:57 -08001540 for (size_t i = 0; i < failures_.size(); ++i) {
1541 os << failure_messages_[i]->str() << "\n";
Ian Rogersad0b3a32012-04-16 14:50:24 -07001542 }
1543 return os;
1544}
1545
Ian Rogers776ac1f2012-04-13 23:36:36 -07001546void MethodVerifier::Dump(std::ostream& os) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001547 VariableIndentationOutputStream vios(&os);
1548 Dump(&vios);
1549}
1550
1551void MethodVerifier::Dump(VariableIndentationOutputStream* vios) {
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001552 if (!code_item_accessor_.HasCodeItem()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001553 vios->Stream() << "Native method\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001554 return;
jeffhaobdb76512011-09-07 11:43:16 -07001555 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001556 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001557 vios->Stream() << "Register Types:\n";
1558 ScopedIndentation indent1(vios);
1559 reg_types_.Dump(vios->Stream());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001560 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001561 vios->Stream() << "Dumping instructions and register lines:\n";
1562 ScopedIndentation indent1(vios);
Mathieu Chartier1d2d4ff2017-09-23 16:11:06 -07001563
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001564 for (const DexInstructionPcPair& inst : code_item_accessor_) {
Mathieu Chartier2b2bef22017-10-26 17:10:19 -07001565 const size_t dex_pc = inst.DexPc();
Andreas Gampe077d9db2018-01-19 18:54:14 -08001566
1567 // Might be asked to dump before the table is initialized.
1568 if (reg_table_.IsInitialized()) {
1569 RegisterLine* reg_line = reg_table_.GetLine(dex_pc);
1570 if (reg_line != nullptr) {
1571 vios->Stream() << reg_line->Dump(this) << "\n";
1572 }
jeffhaobdb76512011-09-07 11:43:16 -07001573 }
Andreas Gampe077d9db2018-01-19 18:54:14 -08001574
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001575 vios->Stream()
Mathieu Chartierde40d472015-10-15 17:47:48 -07001576 << StringPrintf("0x%04zx", dex_pc) << ": " << GetInstructionFlags(dex_pc).ToString() << " ";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001577 const bool kDumpHexOfInstruction = false;
1578 if (kDumpHexOfInstruction) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001579 vios->Stream() << inst->DumpHex(5) << " ";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001580 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001581 vios->Stream() << inst->DumpString(dex_file_) << "\n";
jeffhaoba5ebb92011-08-25 17:24:37 -07001582 }
jeffhaobdb76512011-09-07 11:43:16 -07001583}
1584
Ian Rogersd81871c2011-10-03 13:57:23 -07001585static bool IsPrimitiveDescriptor(char descriptor) {
1586 switch (descriptor) {
jeffhaobdb76512011-09-07 11:43:16 -07001587 case 'I':
1588 case 'C':
1589 case 'S':
1590 case 'B':
1591 case 'Z':
jeffhaobdb76512011-09-07 11:43:16 -07001592 case 'F':
1593 case 'D':
1594 case 'J':
Ian Rogersd81871c2011-10-03 13:57:23 -07001595 return true;
jeffhaobdb76512011-09-07 11:43:16 -07001596 default:
1597 return false;
1598 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001599}
1600
Ian Rogers776ac1f2012-04-13 23:36:36 -07001601bool MethodVerifier::SetTypesFromSignature() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001602 RegisterLine* reg_line = reg_table_.GetLine(0);
Andreas Gampeef0b1a12015-06-19 20:37:46 -07001603
1604 // Should have been verified earlier.
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001605 DCHECK_GE(code_item_accessor_.RegistersSize(), code_item_accessor_.InsSize());
Andreas Gampeef0b1a12015-06-19 20:37:46 -07001606
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001607 uint32_t arg_start = code_item_accessor_.RegistersSize() - code_item_accessor_.InsSize();
1608 size_t expected_args = code_item_accessor_.InsSize(); /* long/double count as two */
jeffhaobdb76512011-09-07 11:43:16 -07001609
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001610 // Include the "this" pointer.
Ian Rogersd81871c2011-10-03 13:57:23 -07001611 size_t cur_arg = 0;
Ian Rogersad0b3a32012-04-16 14:50:24 -07001612 if (!IsStatic()) {
Andreas Gampeef0b1a12015-06-19 20:37:46 -07001613 if (expected_args == 0) {
1614 // Expect at least a receiver.
1615 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected 0 args, but method is not static";
1616 return false;
1617 }
1618
Ian Rogersd81871c2011-10-03 13:57:23 -07001619 // If this is a constructor for a class other than java.lang.Object, mark the first ("this")
1620 // argument as uninitialized. This restricts field access until the superclass constructor is
1621 // called.
Ian Rogersd8f69b02014-09-10 21:43:52 +00001622 const RegType& declaring_class = GetDeclaringClass();
Andreas Gampef10b6e12015-08-12 10:48:12 -07001623 if (IsConstructor()) {
1624 if (declaring_class.IsJavaLangObject()) {
1625 // "this" is implicitly initialized.
1626 reg_line->SetThisInitialized();
Andreas Gampead238ce2015-08-24 21:13:08 -07001627 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, declaring_class);
Andreas Gampef10b6e12015-08-12 10:48:12 -07001628 } else {
Andreas Gampead238ce2015-08-24 21:13:08 -07001629 reg_line->SetRegisterType<LockOp::kClear>(
1630 this,
1631 arg_start + cur_arg,
1632 reg_types_.UninitializedThisArgument(declaring_class));
Andreas Gampef10b6e12015-08-12 10:48:12 -07001633 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001634 } else {
Andreas Gampead238ce2015-08-24 21:13:08 -07001635 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, declaring_class);
jeffhaobdb76512011-09-07 11:43:16 -07001636 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001637 cur_arg++;
jeffhaobdb76512011-09-07 11:43:16 -07001638 }
1639
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001640 const dex::ProtoId& proto_id =
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001641 dex_file_->GetMethodPrototype(dex_file_->GetMethodId(dex_method_idx_));
Ian Rogers0571d352011-11-03 19:51:38 -07001642 DexFileParameterIterator iterator(*dex_file_, proto_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07001643
1644 for (; iterator.HasNext(); iterator.Next()) {
1645 const char* descriptor = iterator.GetDescriptor();
Ian Rogers7b078e82014-09-10 14:44:24 -07001646 if (descriptor == nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001647 LOG(FATAL) << "Null descriptor";
1648 }
1649 if (cur_arg >= expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07001650 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1651 << " args, found more (" << descriptor << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001652 return false;
1653 }
1654 switch (descriptor[0]) {
1655 case 'L':
1656 case '[':
1657 // We assume that reference arguments are initialized. The only way it could be otherwise
1658 // (assuming the caller was verified) is if the current method is <init>, but in that case
1659 // it's effectively considered initialized the instant we reach here (in the sense that we
1660 // can return without doing anything or call virtual methods).
1661 {
Andreas Gampe98be1a92017-08-28 08:25:45 -07001662 // Note: don't check access. No error would be thrown for declaring or passing an
1663 // inaccessible class. Only actual accesses to fields or methods will.
1664 const RegType& reg_type = ResolveClass<CheckAccess::kNo>(iterator.GetTypeIdx());
Sebastien Hertz2ed76f92014-04-22 17:11:08 +02001665 if (!reg_type.IsNonZeroReferenceTypes()) {
1666 DCHECK(HasFailures());
1667 return false;
1668 }
Andreas Gampead238ce2015-08-24 21:13:08 -07001669 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001670 }
1671 break;
1672 case 'Z':
Andreas Gampead238ce2015-08-24 21:13:08 -07001673 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Boolean());
Ian Rogersd81871c2011-10-03 13:57:23 -07001674 break;
1675 case 'C':
Andreas Gampead238ce2015-08-24 21:13:08 -07001676 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Char());
Ian Rogersd81871c2011-10-03 13:57:23 -07001677 break;
1678 case 'B':
Andreas Gampead238ce2015-08-24 21:13:08 -07001679 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Byte());
Ian Rogersd81871c2011-10-03 13:57:23 -07001680 break;
1681 case 'I':
Andreas Gampead238ce2015-08-24 21:13:08 -07001682 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Integer());
Ian Rogersd81871c2011-10-03 13:57:23 -07001683 break;
1684 case 'S':
Andreas Gampead238ce2015-08-24 21:13:08 -07001685 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Short());
Ian Rogersd81871c2011-10-03 13:57:23 -07001686 break;
1687 case 'F':
Andreas Gampead238ce2015-08-24 21:13:08 -07001688 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Float());
Ian Rogersd81871c2011-10-03 13:57:23 -07001689 break;
1690 case 'J':
1691 case 'D': {
Andreas Gampe77cd4d62014-06-19 17:29:48 -07001692 if (cur_arg + 1 >= expected_args) {
1693 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1694 << " args, found more (" << descriptor << ")";
1695 return false;
1696 }
1697
Ian Rogers7b078e82014-09-10 14:44:24 -07001698 const RegType* lo_half;
1699 const RegType* hi_half;
1700 if (descriptor[0] == 'J') {
1701 lo_half = &reg_types_.LongLo();
1702 hi_half = &reg_types_.LongHi();
1703 } else {
1704 lo_half = &reg_types_.DoubleLo();
1705 hi_half = &reg_types_.DoubleHi();
1706 }
1707 reg_line->SetRegisterTypeWide(this, arg_start + cur_arg, *lo_half, *hi_half);
Ian Rogersd81871c2011-10-03 13:57:23 -07001708 cur_arg++;
1709 break;
1710 }
1711 default:
Brian Carlstrom93c33962013-07-26 10:37:43 -07001712 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected signature type char '"
1713 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001714 return false;
1715 }
1716 cur_arg++;
1717 }
1718 if (cur_arg != expected_args) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001719 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1720 << " arguments, found " << cur_arg;
Ian Rogersd81871c2011-10-03 13:57:23 -07001721 return false;
1722 }
1723 const char* descriptor = dex_file_->GetReturnTypeDescriptor(proto_id);
1724 // Validate return type. We don't do the type lookup; just want to make sure that it has the right
1725 // format. Only major difference from the method argument format is that 'V' is supported.
1726 bool result;
1727 if (IsPrimitiveDescriptor(descriptor[0]) || descriptor[0] == 'V') {
1728 result = descriptor[1] == '\0';
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001729 } else if (descriptor[0] == '[') { // single/multi-dimensional array of object/primitive
Ian Rogersd81871c2011-10-03 13:57:23 -07001730 size_t i = 0;
1731 do {
1732 i++;
1733 } while (descriptor[i] == '['); // process leading [
1734 if (descriptor[i] == 'L') { // object array
1735 do {
1736 i++; // find closing ;
1737 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1738 result = descriptor[i] == ';';
1739 } else { // primitive array
1740 result = IsPrimitiveDescriptor(descriptor[i]) && descriptor[i + 1] == '\0';
1741 }
1742 } else if (descriptor[0] == 'L') {
1743 // could be more thorough here, but shouldn't be required
1744 size_t i = 0;
1745 do {
1746 i++;
1747 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1748 result = descriptor[i] == ';';
1749 } else {
1750 result = false;
1751 }
1752 if (!result) {
jeffhaod5347e02012-03-22 17:25:05 -07001753 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected char in return type descriptor '"
1754 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001755 }
1756 return result;
jeffhaobdb76512011-09-07 11:43:16 -07001757}
1758
Ian Rogers776ac1f2012-04-13 23:36:36 -07001759bool MethodVerifier::CodeFlowVerifyMethod() {
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001760 const uint16_t* insns = code_item_accessor_.Insns();
1761 const uint32_t insns_size = code_item_accessor_.InsnsSizeInCodeUnits();
jeffhaoba5ebb92011-08-25 17:24:37 -07001762
jeffhaobdb76512011-09-07 11:43:16 -07001763 /* Begin by marking the first instruction as "changed". */
Mathieu Chartierde40d472015-10-15 17:47:48 -07001764 GetInstructionFlags(0).SetChanged();
Ian Rogersd81871c2011-10-03 13:57:23 -07001765 uint32_t start_guess = 0;
jeffhaoba5ebb92011-08-25 17:24:37 -07001766
jeffhaobdb76512011-09-07 11:43:16 -07001767 /* Continue until no instructions are marked "changed". */
1768 while (true) {
Mathieu Chartier4306ef82014-12-19 18:41:47 -08001769 if (allow_thread_suspension_) {
1770 self_->AllowThreadSuspension();
1771 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001772 // Find the first marked one. Use "start_guess" as a way to find one quickly.
1773 uint32_t insn_idx = start_guess;
1774 for (; insn_idx < insns_size; insn_idx++) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07001775 if (GetInstructionFlags(insn_idx).IsChanged())
jeffhaobdb76512011-09-07 11:43:16 -07001776 break;
1777 }
jeffhaobdb76512011-09-07 11:43:16 -07001778 if (insn_idx == insns_size) {
1779 if (start_guess != 0) {
1780 /* try again, starting from the top */
1781 start_guess = 0;
1782 continue;
1783 } else {
1784 /* all flags are clear */
1785 break;
1786 }
1787 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001788 // We carry the working set of registers from instruction to instruction. If this address can
1789 // be the target of a branch (or throw) instruction, or if we're skipping around chasing
1790 // "changed" flags, we need to load the set of registers from the table.
1791 // Because we always prefer to continue on to the next instruction, we should never have a
1792 // situation where we have a stray "changed" flag set on an instruction that isn't a branch
1793 // target.
1794 work_insn_idx_ = insn_idx;
Mathieu Chartierde40d472015-10-15 17:47:48 -07001795 if (GetInstructionFlags(insn_idx).IsBranchTarget()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001796 work_line_->CopyFromLine(reg_table_.GetLine(insn_idx));
Ian Rogersebbdd872014-07-07 23:53:08 -07001797 } else if (kIsDebugBuild) {
jeffhaobdb76512011-09-07 11:43:16 -07001798 /*
1799 * Sanity check: retrieve the stored register line (assuming
1800 * a full table) and make sure it actually matches.
1801 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001802 RegisterLine* register_line = reg_table_.GetLine(insn_idx);
Ian Rogers7b078e82014-09-10 14:44:24 -07001803 if (register_line != nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001804 if (work_line_->CompareLine(register_line) != 0) {
1805 Dump(std::cout);
1806 std::cout << info_messages_.str();
David Sehr709b0702016-10-13 09:12:37 -07001807 LOG(FATAL) << "work_line diverged in " << dex_file_->PrettyMethod(dex_method_idx_)
Elliott Hughesc073b072012-05-24 19:29:17 -07001808 << "@" << reinterpret_cast<void*>(work_insn_idx_) << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07001809 << " work_line=" << work_line_->Dump(this) << "\n"
1810 << " expected=" << register_line->Dump(this);
Ian Rogersd81871c2011-10-03 13:57:23 -07001811 }
jeffhaobdb76512011-09-07 11:43:16 -07001812 }
jeffhaobdb76512011-09-07 11:43:16 -07001813 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001814 if (!CodeFlowVerifyInstruction(&start_guess)) {
David Sehr709b0702016-10-13 09:12:37 -07001815 std::string prepend(dex_file_->PrettyMethod(dex_method_idx_));
Ian Rogersad0b3a32012-04-16 14:50:24 -07001816 prepend += " failed to verify: ";
1817 PrependToLastFailMessage(prepend);
jeffhaoba5ebb92011-08-25 17:24:37 -07001818 return false;
1819 }
jeffhaobdb76512011-09-07 11:43:16 -07001820 /* Clear "changed" and mark as visited. */
Mathieu Chartierde40d472015-10-15 17:47:48 -07001821 GetInstructionFlags(insn_idx).SetVisited();
1822 GetInstructionFlags(insn_idx).ClearChanged();
jeffhaobdb76512011-09-07 11:43:16 -07001823 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001824
Andreas Gampe92d77202017-12-06 20:49:00 -08001825 if (UNLIKELY(VLOG_IS_ON(verifier_debug))) {
jeffhaobdb76512011-09-07 11:43:16 -07001826 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001827 * Scan for dead code. There's nothing "evil" about dead code
jeffhaobdb76512011-09-07 11:43:16 -07001828 * (besides the wasted space), but it indicates a flaw somewhere
1829 * down the line, possibly in the verifier.
1830 *
1831 * If we've substituted "always throw" instructions into the stream,
1832 * we are almost certainly going to have some dead code.
1833 */
1834 int dead_start = -1;
Mathieu Chartier1d2d4ff2017-09-23 16:11:06 -07001835
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001836 for (const DexInstructionPcPair& inst : code_item_accessor_) {
Mathieu Chartier2b2bef22017-10-26 17:10:19 -07001837 const uint32_t insn_idx = inst.DexPc();
jeffhaobdb76512011-09-07 11:43:16 -07001838 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001839 * Switch-statement data doesn't get "visited" by scanner. It
jeffhaobdb76512011-09-07 11:43:16 -07001840 * may or may not be preceded by a padding NOP (for alignment).
1841 */
1842 if (insns[insn_idx] == Instruction::kPackedSwitchSignature ||
1843 insns[insn_idx] == Instruction::kSparseSwitchSignature ||
1844 insns[insn_idx] == Instruction::kArrayDataSignature ||
Elliott Hughes380aaa72012-07-09 14:33:15 -07001845 (insns[insn_idx] == Instruction::NOP && (insn_idx + 1 < insns_size) &&
jeffhaobdb76512011-09-07 11:43:16 -07001846 (insns[insn_idx + 1] == Instruction::kPackedSwitchSignature ||
1847 insns[insn_idx + 1] == Instruction::kSparseSwitchSignature ||
1848 insns[insn_idx + 1] == Instruction::kArrayDataSignature))) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07001849 GetInstructionFlags(insn_idx).SetVisited();
jeffhaobdb76512011-09-07 11:43:16 -07001850 }
1851
Mathieu Chartierde40d472015-10-15 17:47:48 -07001852 if (!GetInstructionFlags(insn_idx).IsVisited()) {
Mathieu Chartier1d2d4ff2017-09-23 16:11:06 -07001853 if (dead_start < 0) {
jeffhaobdb76512011-09-07 11:43:16 -07001854 dead_start = insn_idx;
Mathieu Chartier1d2d4ff2017-09-23 16:11:06 -07001855 }
jeffhaobdb76512011-09-07 11:43:16 -07001856 } else if (dead_start >= 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001857 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start)
1858 << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaobdb76512011-09-07 11:43:16 -07001859 dead_start = -1;
1860 }
1861 }
1862 if (dead_start >= 0) {
Mathieu Chartier1d2d4ff2017-09-23 16:11:06 -07001863 LogVerifyInfo()
1864 << "dead code " << reinterpret_cast<void*>(dead_start)
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001865 << "-" << reinterpret_cast<void*>(code_item_accessor_.InsnsSizeInCodeUnits() - 1);
jeffhaoba5ebb92011-08-25 17:24:37 -07001866 }
Ian Rogersc9e463c2013-06-05 16:52:26 -07001867 // To dump the state of the verify after a method, do something like:
David Sehr709b0702016-10-13 09:12:37 -07001868 // if (dex_file_->PrettyMethod(dex_method_idx_) ==
Ian Rogersc9e463c2013-06-05 16:52:26 -07001869 // "boolean java.lang.String.equals(java.lang.Object)") {
1870 // LOG(INFO) << info_messages_.str();
1871 // }
jeffhaoba5ebb92011-08-25 17:24:37 -07001872 }
jeffhaobdb76512011-09-07 11:43:16 -07001873 return true;
1874}
1875
Andreas Gampe68df3202015-06-22 11:35:46 -07001876// Returns the index of the first final instance field of the given class, or kDexNoIndex if there
1877// is no such field.
Andreas Gampea5b09a62016-11-17 15:21:22 -08001878static uint32_t GetFirstFinalInstanceFieldIndex(const DexFile& dex_file, dex::TypeIndex type_idx) {
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001879 const dex::ClassDef* class_def = dex_file.FindClassDef(type_idx);
Andreas Gampe68df3202015-06-22 11:35:46 -07001880 DCHECK(class_def != nullptr);
Mathieu Chartier1f1cb9f2018-06-04 09:22:46 -07001881 ClassAccessor accessor(dex_file, *class_def);
1882 for (const ClassAccessor::Field& field : accessor.GetInstanceFields()) {
1883 if (field.IsFinal()) {
1884 return field.GetIndex();
Andreas Gampe68df3202015-06-22 11:35:46 -07001885 }
Andreas Gampe68df3202015-06-22 11:35:46 -07001886 }
Andreas Gampee2abbc62017-09-15 11:59:26 -07001887 return dex::kDexNoIndex;
Andreas Gampe68df3202015-06-22 11:35:46 -07001888}
1889
Andreas Gampea727e372015-08-25 09:22:37 -07001890// Setup a register line for the given return instruction.
1891static void AdjustReturnLine(MethodVerifier* verifier,
1892 const Instruction* ret_inst,
1893 RegisterLine* line) {
1894 Instruction::Code opcode = ret_inst->Opcode();
1895
1896 switch (opcode) {
1897 case Instruction::RETURN_VOID:
1898 case Instruction::RETURN_VOID_NO_BARRIER:
1899 SafelyMarkAllRegistersAsConflicts(verifier, line);
1900 break;
1901
1902 case Instruction::RETURN:
1903 case Instruction::RETURN_OBJECT:
1904 line->MarkAllRegistersAsConflictsExcept(verifier, ret_inst->VRegA_11x());
1905 break;
1906
1907 case Instruction::RETURN_WIDE:
1908 line->MarkAllRegistersAsConflictsExceptWide(verifier, ret_inst->VRegA_11x());
1909 break;
1910
1911 default:
1912 LOG(FATAL) << "Unknown return opcode " << opcode;
1913 UNREACHABLE();
1914 }
1915}
1916
Ian Rogers776ac1f2012-04-13 23:36:36 -07001917bool MethodVerifier::CodeFlowVerifyInstruction(uint32_t* start_guess) {
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001918 // If we're doing FindLocksAtDexPc, check whether we're at the dex pc we care about.
1919 // We want the state _before_ the instruction, for the case where the dex pc we're
1920 // interested in is itself a monitor-enter instruction (which is a likely place
1921 // for a thread to be suspended).
Ian Rogers7b078e82014-09-10 14:44:24 -07001922 if (monitor_enter_dex_pcs_ != nullptr && work_insn_idx_ == interesting_dex_pc_) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001923 monitor_enter_dex_pcs_->clear(); // The new work line is more accurate than the previous one.
Andreas Gampeaaf0d382017-11-27 14:10:21 -08001924
1925 std::map<uint32_t, DexLockInfo> depth_to_lock_info;
1926 auto collector = [&](uint32_t dex_reg, uint32_t depth) {
1927 auto insert_pair = depth_to_lock_info.emplace(depth, DexLockInfo(depth));
1928 auto it = insert_pair.first;
1929 auto set_insert_pair = it->second.dex_registers.insert(dex_reg);
1930 DCHECK(set_insert_pair.second);
1931 };
1932 work_line_->IterateRegToLockDepths(collector);
1933 for (auto& pair : depth_to_lock_info) {
1934 monitor_enter_dex_pcs_->push_back(pair.second);
1935 // Map depth to dex PC.
1936 (*monitor_enter_dex_pcs_)[monitor_enter_dex_pcs_->size() - 1].dex_pc =
1937 work_line_->GetMonitorEnterDexPc(pair.second.dex_pc);
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001938 }
1939 }
1940
jeffhaobdb76512011-09-07 11:43:16 -07001941 /*
1942 * Once we finish decoding the instruction, we need to figure out where
jeffhaod1f0fde2011-09-08 17:25:33 -07001943 * we can go from here. There are three possible ways to transfer
jeffhaobdb76512011-09-07 11:43:16 -07001944 * control to another statement:
1945 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001946 * (1) Continue to the next instruction. Applies to all but
jeffhaobdb76512011-09-07 11:43:16 -07001947 * unconditional branches, method returns, and exception throws.
jeffhaod1f0fde2011-09-08 17:25:33 -07001948 * (2) Branch to one or more possible locations. Applies to branches
jeffhaobdb76512011-09-07 11:43:16 -07001949 * and switch statements.
jeffhaod1f0fde2011-09-08 17:25:33 -07001950 * (3) Exception handlers. Applies to any instruction that can
jeffhaobdb76512011-09-07 11:43:16 -07001951 * throw an exception that is handled by an encompassing "try"
1952 * block.
1953 *
1954 * We can also return, in which case there is no successor instruction
1955 * from this point.
1956 *
Elliott Hughesadb8c672012-03-06 16:49:32 -08001957 * The behavior can be determined from the opcode flags.
jeffhaobdb76512011-09-07 11:43:16 -07001958 */
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001959 const uint16_t* insns = code_item_accessor_.Insns() + work_insn_idx_;
Ian Rogersd81871c2011-10-03 13:57:23 -07001960 const Instruction* inst = Instruction::At(insns);
Ian Rogersa75a0132012-09-28 11:41:42 -07001961 int opcode_flags = Instruction::FlagsOf(inst->Opcode());
jeffhaobdb76512011-09-07 11:43:16 -07001962
jeffhaobdb76512011-09-07 11:43:16 -07001963 int32_t branch_target = 0;
jeffhaobdb76512011-09-07 11:43:16 -07001964 bool just_set_result = false;
Andreas Gampe92d77202017-12-06 20:49:00 -08001965 if (UNLIKELY(VLOG_IS_ON(verifier_debug))) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001966 // Generate processing back trace to debug verifier
Elliott Hughesc073b072012-05-24 19:29:17 -07001967 LogVerifyInfo() << "Processing " << inst->DumpString(dex_file_) << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07001968 << work_line_->Dump(this) << "\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001969 }
jeffhaobdb76512011-09-07 11:43:16 -07001970
1971 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001972 * Make a copy of the previous register state. If the instruction
jeffhaobdb76512011-09-07 11:43:16 -07001973 * can throw an exception, we will copy/merge this into the "catch"
1974 * address rather than work_line, because we don't want the result
1975 * from the "successful" code path (e.g. a check-cast that "improves"
1976 * a type) to be visible to the exception handler.
1977 */
Ian Rogers776ac1f2012-04-13 23:36:36 -07001978 if ((opcode_flags & Instruction::kThrow) != 0 && CurrentInsnFlags()->IsInTry()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001979 saved_line_->CopyFromLine(work_line_.get());
Ian Rogers1ff3c982014-08-12 02:30:58 -07001980 } else if (kIsDebugBuild) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001981 saved_line_->FillWithGarbage();
jeffhaobdb76512011-09-07 11:43:16 -07001982 }
Andreas Gamped12e7822015-06-25 10:26:40 -07001983 DCHECK(!have_pending_runtime_throw_failure_); // Per-instruction flag, should not be set here.
jeffhaobdb76512011-09-07 11:43:16 -07001984
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07001985
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001986 // We need to ensure the work line is consistent while performing validation. When we spot a
1987 // peephole pattern we compute a new line for either the fallthrough instruction or the
1988 // branch target.
Mathieu Chartier361e04a2016-02-16 14:06:35 -08001989 RegisterLineArenaUniquePtr branch_line;
1990 RegisterLineArenaUniquePtr fallthrough_line;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001991
Sebastien Hertz5243e912013-05-21 10:55:07 +02001992 switch (inst->Opcode()) {
jeffhaobdb76512011-09-07 11:43:16 -07001993 case Instruction::NOP:
1994 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001995 * A "pure" NOP has no effect on anything. Data tables start with
jeffhaobdb76512011-09-07 11:43:16 -07001996 * a signature that looks like a NOP; if we see one of these in
1997 * the course of executing code then we have a problem.
1998 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001999 if (inst->VRegA_10x() != 0) {
jeffhaod5347e02012-03-22 17:25:05 -07002000 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "encountered data table in instruction stream";
jeffhaobdb76512011-09-07 11:43:16 -07002001 }
2002 break;
2003
2004 case Instruction::MOVE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002005 work_line_->CopyRegister1(this, inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategory1nr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02002006 break;
jeffhaobdb76512011-09-07 11:43:16 -07002007 case Instruction::MOVE_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07002008 work_line_->CopyRegister1(this, inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategory1nr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02002009 break;
jeffhaobdb76512011-09-07 11:43:16 -07002010 case Instruction::MOVE_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07002011 work_line_->CopyRegister1(this, inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategory1nr);
jeffhaobdb76512011-09-07 11:43:16 -07002012 break;
2013 case Instruction::MOVE_WIDE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002014 work_line_->CopyRegister2(this, inst->VRegA_12x(), inst->VRegB_12x());
Sebastien Hertz5243e912013-05-21 10:55:07 +02002015 break;
jeffhaobdb76512011-09-07 11:43:16 -07002016 case Instruction::MOVE_WIDE_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07002017 work_line_->CopyRegister2(this, inst->VRegA_22x(), inst->VRegB_22x());
Sebastien Hertz5243e912013-05-21 10:55:07 +02002018 break;
jeffhaobdb76512011-09-07 11:43:16 -07002019 case Instruction::MOVE_WIDE_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07002020 work_line_->CopyRegister2(this, inst->VRegA_32x(), inst->VRegB_32x());
jeffhaobdb76512011-09-07 11:43:16 -07002021 break;
2022 case Instruction::MOVE_OBJECT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002023 work_line_->CopyRegister1(this, inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategoryRef);
Sebastien Hertz5243e912013-05-21 10:55:07 +02002024 break;
jeffhaobdb76512011-09-07 11:43:16 -07002025 case Instruction::MOVE_OBJECT_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07002026 work_line_->CopyRegister1(this, inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategoryRef);
Sebastien Hertz5243e912013-05-21 10:55:07 +02002027 break;
jeffhaobdb76512011-09-07 11:43:16 -07002028 case Instruction::MOVE_OBJECT_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07002029 work_line_->CopyRegister1(this, inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategoryRef);
jeffhaobdb76512011-09-07 11:43:16 -07002030 break;
2031
2032 /*
2033 * The move-result instructions copy data out of a "pseudo-register"
jeffhaod1f0fde2011-09-08 17:25:33 -07002034 * with the results from the last method invocation. In practice we
jeffhaobdb76512011-09-07 11:43:16 -07002035 * might want to hold the result in an actual CPU register, so the
2036 * Dalvik spec requires that these only appear immediately after an
2037 * invoke or filled-new-array.
2038 *
jeffhaod1f0fde2011-09-08 17:25:33 -07002039 * These calls invalidate the "result" register. (This is now
jeffhaobdb76512011-09-07 11:43:16 -07002040 * redundant with the reset done below, but it can make the debug info
2041 * easier to read in some cases.)
2042 */
2043 case Instruction::MOVE_RESULT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002044 work_line_->CopyResultRegister1(this, inst->VRegA_11x(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002045 break;
2046 case Instruction::MOVE_RESULT_WIDE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002047 work_line_->CopyResultRegister2(this, inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07002048 break;
2049 case Instruction::MOVE_RESULT_OBJECT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002050 work_line_->CopyResultRegister1(this, inst->VRegA_11x(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002051 break;
2052
Ian Rogersd81871c2011-10-03 13:57:23 -07002053 case Instruction::MOVE_EXCEPTION: {
Sebastien Hertz270a0e12015-01-16 19:49:09 +01002054 // We do not allow MOVE_EXCEPTION as the first instruction in a method. This is a simple case
2055 // where one entrypoint to the catch block is not actually an exception path.
2056 if (work_insn_idx_ == 0) {
2057 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "move-exception at pc 0x0";
2058 break;
2059 }
jeffhaobdb76512011-09-07 11:43:16 -07002060 /*
jeffhao60f83e32012-02-13 17:16:30 -08002061 * This statement can only appear as the first instruction in an exception handler. We verify
2062 * that as part of extracting the exception type from the catch block list.
jeffhaobdb76512011-09-07 11:43:16 -07002063 */
Ian Rogersd8f69b02014-09-10 21:43:52 +00002064 const RegType& res_type = GetCaughtExceptionType();
Andreas Gampead238ce2015-08-24 21:13:08 -07002065 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_11x(), res_type);
jeffhaobdb76512011-09-07 11:43:16 -07002066 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002067 }
jeffhaobdb76512011-09-07 11:43:16 -07002068 case Instruction::RETURN_VOID:
Andreas Gampef10b6e12015-08-12 10:48:12 -07002069 if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002070 if (!GetMethodReturnType().IsConflict()) {
jeffhaod5347e02012-03-22 17:25:05 -07002071 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07002072 }
jeffhaobdb76512011-09-07 11:43:16 -07002073 }
2074 break;
2075 case Instruction::RETURN:
Andreas Gampef10b6e12015-08-12 10:48:12 -07002076 if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) {
jeffhaobdb76512011-09-07 11:43:16 -07002077 /* check the method signature */
Ian Rogersd8f69b02014-09-10 21:43:52 +00002078 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07002079 if (!return_type.IsCategory1Types()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002080 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected non-category 1 return type "
2081 << return_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002082 } else {
2083 // Compilers may generate synthetic functions that write byte values into boolean fields.
2084 // Also, it may use integer values for boolean, byte, short, and character return types.
Sebastien Hertz5243e912013-05-21 10:55:07 +02002085 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07002086 const RegType& src_type = work_line_->GetRegisterType(this, vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07002087 bool use_src = ((return_type.IsBoolean() && src_type.IsByte()) ||
2088 ((return_type.IsBoolean() || return_type.IsByte() ||
2089 return_type.IsShort() || return_type.IsChar()) &&
2090 src_type.IsInteger()));
2091 /* check the register contents */
Ian Rogersad0b3a32012-04-16 14:50:24 -07002092 bool success =
Ian Rogers7b078e82014-09-10 14:44:24 -07002093 work_line_->VerifyRegisterType(this, vregA, use_src ? src_type : return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002094 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002095 AppendToLastFailMessage(StringPrintf(" return-1nr on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07002096 }
jeffhaobdb76512011-09-07 11:43:16 -07002097 }
2098 }
2099 break;
2100 case Instruction::RETURN_WIDE:
Andreas Gampef10b6e12015-08-12 10:48:12 -07002101 if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) {
jeffhaobdb76512011-09-07 11:43:16 -07002102 /* check the method signature */
Ian Rogersd8f69b02014-09-10 21:43:52 +00002103 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07002104 if (!return_type.IsCategory2Types()) {
jeffhaod5347e02012-03-22 17:25:05 -07002105 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-wide not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07002106 } else {
2107 /* check the register contents */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002108 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07002109 bool success = work_line_->VerifyRegisterType(this, vregA, return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002110 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002111 AppendToLastFailMessage(StringPrintf(" return-wide on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07002112 }
jeffhaobdb76512011-09-07 11:43:16 -07002113 }
2114 }
2115 break;
2116 case Instruction::RETURN_OBJECT:
Andreas Gampef10b6e12015-08-12 10:48:12 -07002117 if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) {
Ian Rogersd8f69b02014-09-10 21:43:52 +00002118 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07002119 if (!return_type.IsReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002120 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-object not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07002121 } else {
2122 /* return_type is the *expected* return type, not register value */
Andreas Gampeeed3a5d2017-11-29 14:58:34 -08002123 DCHECK(!return_type.IsZeroOrNull());
Ian Rogersd81871c2011-10-03 13:57:23 -07002124 DCHECK(!return_type.IsUninitializedReference());
Sebastien Hertz5243e912013-05-21 10:55:07 +02002125 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07002126 const RegType& reg_type = work_line_->GetRegisterType(this, vregA);
Andreas Gampea32210c2015-06-24 10:26:13 -07002127 // Disallow returning undefined, conflict & uninitialized values and verify that the
2128 // reference in vAA is an instance of the "return_type."
2129 if (reg_type.IsUndefined()) {
2130 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning undefined register";
2131 } else if (reg_type.IsConflict()) {
2132 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning register with conflict";
2133 } else if (reg_type.IsUninitializedTypes()) {
David Brazdil68b5c0b2016-01-19 14:25:29 +00002134 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning uninitialized object '"
Brian Carlstrom93c33962013-07-26 10:37:43 -07002135 << reg_type << "'";
Andreas Gampea4c98f22015-11-06 16:24:49 -08002136 } else if (!reg_type.IsReferenceTypes()) {
2137 // We really do expect a reference here.
2138 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-object returns a non-reference type "
2139 << reg_type;
David Brazdilca3c8c32016-09-06 14:04:48 +01002140 } else if (!return_type.IsAssignableFrom(reg_type, this)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07002141 if (reg_type.IsUnresolvedTypes() || return_type.IsUnresolvedTypes()) {
2142 Fail(VERIFY_ERROR_NO_CLASS) << " can't resolve returned type '" << return_type
2143 << "' or '" << reg_type << "'";
2144 } else {
Andreas Gampe16f149c2015-03-23 10:10:20 -07002145 bool soft_error = false;
2146 // Check whether arrays are involved. They will show a valid class status, even
2147 // if their components are erroneous.
2148 if (reg_type.IsArrayTypes() && return_type.IsArrayTypes()) {
David Brazdilca3c8c32016-09-06 14:04:48 +01002149 return_type.CanAssignArray(reg_type, reg_types_, class_loader_, this, &soft_error);
Andreas Gampe16f149c2015-03-23 10:10:20 -07002150 if (soft_error) {
2151 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "array with erroneous component type: "
2152 << reg_type << " vs " << return_type;
2153 }
2154 }
2155
2156 if (!soft_error) {
2157 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning '" << reg_type
2158 << "', but expected from declaration '" << return_type << "'";
2159 }
Jeff Haoa3faaf42013-09-03 19:07:00 -07002160 }
jeffhaobdb76512011-09-07 11:43:16 -07002161 }
2162 }
2163 }
2164 break;
2165
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07002166 /* could be boolean, int, float, or a null reference */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002167 case Instruction::CONST_4: {
2168 int32_t val = static_cast<int32_t>(inst->VRegB_11n() << 28) >> 28;
Andreas Gampead238ce2015-08-24 21:13:08 -07002169 work_line_->SetRegisterType<LockOp::kClear>(
2170 this, inst->VRegA_11n(), DetermineCat1Constant(val, need_precise_constants_));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07002171 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02002172 }
2173 case Instruction::CONST_16: {
2174 int16_t val = static_cast<int16_t>(inst->VRegB_21s());
Andreas Gampead238ce2015-08-24 21:13:08 -07002175 work_line_->SetRegisterType<LockOp::kClear>(
2176 this, inst->VRegA_21s(), DetermineCat1Constant(val, need_precise_constants_));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07002177 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02002178 }
Sebastien Hertz849600b2013-12-20 10:28:08 +01002179 case Instruction::CONST: {
2180 int32_t val = inst->VRegB_31i();
Andreas Gampead238ce2015-08-24 21:13:08 -07002181 work_line_->SetRegisterType<LockOp::kClear>(
2182 this, inst->VRegA_31i(), DetermineCat1Constant(val, need_precise_constants_));
jeffhaobdb76512011-09-07 11:43:16 -07002183 break;
Sebastien Hertz849600b2013-12-20 10:28:08 +01002184 }
2185 case Instruction::CONST_HIGH16: {
2186 int32_t val = static_cast<int32_t>(inst->VRegB_21h() << 16);
Andreas Gampead238ce2015-08-24 21:13:08 -07002187 work_line_->SetRegisterType<LockOp::kClear>(
2188 this, inst->VRegA_21h(), DetermineCat1Constant(val, need_precise_constants_));
jeffhaobdb76512011-09-07 11:43:16 -07002189 break;
Sebastien Hertz849600b2013-12-20 10:28:08 +01002190 }
jeffhaobdb76512011-09-07 11:43:16 -07002191 /* could be long or double; resolved upon use */
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002192 case Instruction::CONST_WIDE_16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002193 int64_t val = static_cast<int16_t>(inst->VRegB_21s());
Ian Rogersd8f69b02014-09-10 21:43:52 +00002194 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
2195 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07002196 work_line_->SetRegisterTypeWide(this, inst->VRegA_21s(), lo, hi);
jeffhaobdb76512011-09-07 11:43:16 -07002197 break;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002198 }
2199 case Instruction::CONST_WIDE_32: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002200 int64_t val = static_cast<int32_t>(inst->VRegB_31i());
Ian Rogersd8f69b02014-09-10 21:43:52 +00002201 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
2202 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07002203 work_line_->SetRegisterTypeWide(this, inst->VRegA_31i(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002204 break;
2205 }
2206 case Instruction::CONST_WIDE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002207 int64_t val = inst->VRegB_51l();
Ian Rogersd8f69b02014-09-10 21:43:52 +00002208 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
2209 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07002210 work_line_->SetRegisterTypeWide(this, inst->VRegA_51l(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002211 break;
2212 }
2213 case Instruction::CONST_WIDE_HIGH16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002214 int64_t val = static_cast<uint64_t>(inst->VRegB_21h()) << 48;
Ian Rogersd8f69b02014-09-10 21:43:52 +00002215 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
2216 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07002217 work_line_->SetRegisterTypeWide(this, inst->VRegA_21h(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002218 break;
2219 }
jeffhaobdb76512011-09-07 11:43:16 -07002220 case Instruction::CONST_STRING:
Andreas Gampead238ce2015-08-24 21:13:08 -07002221 work_line_->SetRegisterType<LockOp::kClear>(
2222 this, inst->VRegA_21c(), reg_types_.JavaLangString());
Sebastien Hertz5243e912013-05-21 10:55:07 +02002223 break;
jeffhaobdb76512011-09-07 11:43:16 -07002224 case Instruction::CONST_STRING_JUMBO:
Andreas Gampead238ce2015-08-24 21:13:08 -07002225 work_line_->SetRegisterType<LockOp::kClear>(
2226 this, inst->VRegA_31c(), reg_types_.JavaLangString());
jeffhaobdb76512011-09-07 11:43:16 -07002227 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002228 case Instruction::CONST_CLASS: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002229 // Get type from instruction if unresolved then we need an access check
2230 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Andreas Gampe98be1a92017-08-28 08:25:45 -07002231 const RegType& res_type = ResolveClass<CheckAccess::kYes>(dex::TypeIndex(inst->VRegB_21c()));
Ian Rogersad0b3a32012-04-16 14:50:24 -07002232 // Register holds class, ie its type is class, on error it will hold Conflict.
Andreas Gampead238ce2015-08-24 21:13:08 -07002233 work_line_->SetRegisterType<LockOp::kClear>(
2234 this, inst->VRegA_21c(), res_type.IsConflict() ? res_type
2235 : reg_types_.JavaLangClass());
jeffhaobdb76512011-09-07 11:43:16 -07002236 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002237 }
Orion Hodson2e599942017-09-22 16:17:41 +01002238 case Instruction::CONST_METHOD_HANDLE:
2239 work_line_->SetRegisterType<LockOp::kClear>(
2240 this, inst->VRegA_21c(), reg_types_.JavaLangInvokeMethodHandle());
Orion Hodson2e599942017-09-22 16:17:41 +01002241 break;
2242 case Instruction::CONST_METHOD_TYPE:
2243 work_line_->SetRegisterType<LockOp::kClear>(
2244 this, inst->VRegA_21c(), reg_types_.JavaLangInvokeMethodType());
Orion Hodson2e599942017-09-22 16:17:41 +01002245 break;
jeffhaobdb76512011-09-07 11:43:16 -07002246 case Instruction::MONITOR_ENTER:
Ian Rogers7b078e82014-09-10 14:44:24 -07002247 work_line_->PushMonitor(this, inst->VRegA_11x(), work_insn_idx_);
Andreas Gampec1474102015-08-18 08:57:44 -07002248 // Check whether the previous instruction is a move-object with vAA as a source, creating
2249 // untracked lock aliasing.
Mathieu Chartierde40d472015-10-15 17:47:48 -07002250 if (0 != work_insn_idx_ && !GetInstructionFlags(work_insn_idx_).IsBranchTarget()) {
Andreas Gampec1474102015-08-18 08:57:44 -07002251 uint32_t prev_idx = work_insn_idx_ - 1;
Mathieu Chartierde40d472015-10-15 17:47:48 -07002252 while (0 != prev_idx && !GetInstructionFlags(prev_idx).IsOpcode()) {
Andreas Gampec1474102015-08-18 08:57:44 -07002253 prev_idx--;
2254 }
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08002255 const Instruction& prev_inst = code_item_accessor_.InstructionAt(prev_idx);
Mathieu Chartier1d2d4ff2017-09-23 16:11:06 -07002256 switch (prev_inst.Opcode()) {
Andreas Gampec1474102015-08-18 08:57:44 -07002257 case Instruction::MOVE_OBJECT:
2258 case Instruction::MOVE_OBJECT_16:
2259 case Instruction::MOVE_OBJECT_FROM16:
Mathieu Chartier1d2d4ff2017-09-23 16:11:06 -07002260 if (prev_inst.VRegB() == inst->VRegA_11x()) {
Andreas Gampec1474102015-08-18 08:57:44 -07002261 // Redo the copy. This won't change the register types, but update the lock status
2262 // for the aliased register.
2263 work_line_->CopyRegister1(this,
Mathieu Chartier1d2d4ff2017-09-23 16:11:06 -07002264 prev_inst.VRegA(),
2265 prev_inst.VRegB(),
Andreas Gampec1474102015-08-18 08:57:44 -07002266 kTypeCategoryRef);
2267 }
2268 break;
2269
Alexey Grebenkince750492018-05-31 23:42:20 +03002270 // Catch a case of register aliasing when two registers are linked to the same
2271 // java.lang.Class object via two consequent const-class instructions immediately
2272 // preceding monitor-enter called on one of those registers.
2273 case Instruction::CONST_CLASS: {
2274 // Get the second previous instruction.
2275 if (prev_idx == 0 || GetInstructionFlags(prev_idx).IsBranchTarget()) {
2276 break;
2277 }
2278 prev_idx--;
2279 while (0 != prev_idx && !GetInstructionFlags(prev_idx).IsOpcode()) {
2280 prev_idx--;
2281 }
2282 const Instruction& prev2_inst = code_item_accessor_.InstructionAt(prev_idx);
2283
2284 // Match the pattern "const-class; const-class; monitor-enter;"
2285 if (prev2_inst.Opcode() != Instruction::CONST_CLASS) {
2286 break;
2287 }
2288
2289 // Ensure both const-classes are called for the same type_idx.
2290 if (prev_inst.VRegB_21c() != prev2_inst.VRegB_21c()) {
2291 break;
2292 }
2293
2294 // Update the lock status for the aliased register.
2295 if (prev_inst.VRegA() == inst->VRegA_11x()) {
2296 work_line_->CopyRegister1(this,
2297 prev2_inst.VRegA(),
2298 inst->VRegA_11x(),
2299 kTypeCategoryRef);
2300 } else if (prev2_inst.VRegA() == inst->VRegA_11x()) {
2301 work_line_->CopyRegister1(this,
2302 prev_inst.VRegA(),
2303 inst->VRegA_11x(),
2304 kTypeCategoryRef);
2305 }
2306 break;
2307 }
2308
Andreas Gampec1474102015-08-18 08:57:44 -07002309 default: // Other instruction types ignored.
2310 break;
2311 }
2312 }
jeffhaobdb76512011-09-07 11:43:16 -07002313 break;
2314 case Instruction::MONITOR_EXIT:
2315 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002316 * monitor-exit instructions are odd. They can throw exceptions,
jeffhaobdb76512011-09-07 11:43:16 -07002317 * but when they do they act as if they succeeded and the PC is
jeffhaod1f0fde2011-09-08 17:25:33 -07002318 * pointing to the following instruction. (This behavior goes back
jeffhaobdb76512011-09-07 11:43:16 -07002319 * to the need to handle asynchronous exceptions, a now-deprecated
2320 * feature that Dalvik doesn't support.)
2321 *
jeffhaod1f0fde2011-09-08 17:25:33 -07002322 * In practice we don't need to worry about this. The only
jeffhaobdb76512011-09-07 11:43:16 -07002323 * exceptions that can be thrown from monitor-exit are for a
jeffhaod1f0fde2011-09-08 17:25:33 -07002324 * null reference and -exit without a matching -enter. If the
jeffhaobdb76512011-09-07 11:43:16 -07002325 * structured locking checks are working, the former would have
2326 * failed on the -enter instruction, and the latter is impossible.
2327 *
2328 * This is fortunate, because issue 3221411 prevents us from
2329 * chasing the "can throw" path when monitor verification is
jeffhaod1f0fde2011-09-08 17:25:33 -07002330 * enabled. If we can fully verify the locking we can ignore
jeffhaobdb76512011-09-07 11:43:16 -07002331 * some catch blocks (which will show up as "dead" code when
2332 * we skip them here); if we can't, then the code path could be
2333 * "live" so we still need to check it.
2334 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002335 opcode_flags &= ~Instruction::kThrow;
Ian Rogers7b078e82014-09-10 14:44:24 -07002336 work_line_->PopMonitor(this, inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07002337 break;
Ian Rogers28ad40d2011-10-27 15:19:26 -07002338 case Instruction::CHECK_CAST:
Ian Rogersd81871c2011-10-03 13:57:23 -07002339 case Instruction::INSTANCE_OF: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002340 /*
2341 * If this instruction succeeds, we will "downcast" register vA to the type in vB. (This
2342 * could be a "upcast" -- not expected, so we don't try to address it.)
2343 *
2344 * If it fails, an exception is thrown, which we deal with later by ignoring the update to
Elliott Hughesadb8c672012-03-06 16:49:32 -08002345 * dec_insn.vA when branching to a handler.
Ian Rogers28ad40d2011-10-27 15:19:26 -07002346 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002347 const bool is_checkcast = (inst->Opcode() == Instruction::CHECK_CAST);
Andreas Gampea5b09a62016-11-17 15:21:22 -08002348 const dex::TypeIndex type_idx((is_checkcast) ? inst->VRegB_21c() : inst->VRegC_22c());
Andreas Gampe98be1a92017-08-28 08:25:45 -07002349 const RegType& res_type = ResolveClass<CheckAccess::kYes>(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002350 if (res_type.IsConflict()) {
Andreas Gampe00633eb2014-07-17 16:13:35 -07002351 // If this is a primitive type, fail HARD.
Vladimir Marko666ee3d2017-12-11 18:37:36 +00002352 ObjPtr<mirror::Class> klass = Runtime::Current()->GetClassLinker()->LookupResolvedType(
2353 type_idx, dex_cache_.Get(), class_loader_.Get());
Andreas Gampe00633eb2014-07-17 16:13:35 -07002354 if (klass != nullptr && klass->IsPrimitive()) {
2355 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "using primitive type "
2356 << dex_file_->StringByTypeIdx(type_idx) << " in instanceof in "
2357 << GetDeclaringClass();
2358 break;
2359 }
2360
Ian Rogersad0b3a32012-04-16 14:50:24 -07002361 DCHECK_NE(failures_.size(), 0U);
2362 if (!is_checkcast) {
Andreas Gampead238ce2015-08-24 21:13:08 -07002363 work_line_->SetRegisterType<LockOp::kClear>(this,
2364 inst->VRegA_22c(),
2365 reg_types_.Boolean());
Ian Rogersad0b3a32012-04-16 14:50:24 -07002366 }
2367 break; // bad class
Ian Rogers9f1ab122011-12-12 08:52:43 -08002368 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002369 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Sebastien Hertz5243e912013-05-21 10:55:07 +02002370 uint32_t orig_type_reg = (is_checkcast) ? inst->VRegA_21c() : inst->VRegB_22c();
Ian Rogers7b078e82014-09-10 14:44:24 -07002371 const RegType& orig_type = work_line_->GetRegisterType(this, orig_type_reg);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002372 if (!res_type.IsNonZeroReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002373 if (is_checkcast) {
2374 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on unexpected class " << res_type;
2375 } else {
2376 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on unexpected class " << res_type;
2377 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002378 } else if (!orig_type.IsReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002379 if (is_checkcast) {
2380 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on non-reference in v" << orig_type_reg;
2381 } else {
2382 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on non-reference in v" << orig_type_reg;
2383 }
David Brazdil68b5c0b2016-01-19 14:25:29 +00002384 } else if (orig_type.IsUninitializedTypes()) {
2385 if (is_checkcast) {
2386 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on uninitialized reference in v"
2387 << orig_type_reg;
2388 } else {
2389 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on uninitialized reference in v"
2390 << orig_type_reg;
2391 }
jeffhao2a8a90e2011-09-26 14:25:31 -07002392 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002393 if (is_checkcast) {
Andreas Gampead238ce2015-08-24 21:13:08 -07002394 work_line_->SetRegisterType<LockOp::kKeep>(this, inst->VRegA_21c(), res_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07002395 } else {
Andreas Gampead238ce2015-08-24 21:13:08 -07002396 work_line_->SetRegisterType<LockOp::kClear>(this,
2397 inst->VRegA_22c(),
2398 reg_types_.Boolean());
jeffhaobdb76512011-09-07 11:43:16 -07002399 }
jeffhaobdb76512011-09-07 11:43:16 -07002400 }
jeffhao2a8a90e2011-09-26 14:25:31 -07002401 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002402 }
2403 case Instruction::ARRAY_LENGTH: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002404 const RegType& res_type = work_line_->GetRegisterType(this, inst->VRegB_12x());
Ian Rogers28ad40d2011-10-27 15:19:26 -07002405 if (res_type.IsReferenceTypes()) {
Andreas Gampeeed3a5d2017-11-29 14:58:34 -08002406 if (!res_type.IsArrayTypes() && !res_type.IsZeroOrNull()) {
2407 // ie not an array or null
jeffhaod5347e02012-03-22 17:25:05 -07002408 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002409 } else {
Andreas Gampead238ce2015-08-24 21:13:08 -07002410 work_line_->SetRegisterType<LockOp::kClear>(this,
2411 inst->VRegA_12x(),
2412 reg_types_.Integer());
Ian Rogersd81871c2011-10-03 13:57:23 -07002413 }
Andreas Gampe65c9db82014-07-28 13:14:34 -07002414 } else {
2415 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002416 }
2417 break;
2418 }
2419 case Instruction::NEW_INSTANCE: {
Andreas Gampe98be1a92017-08-28 08:25:45 -07002420 const RegType& res_type = ResolveClass<CheckAccess::kYes>(dex::TypeIndex(inst->VRegB_21c()));
Ian Rogersad0b3a32012-04-16 14:50:24 -07002421 if (res_type.IsConflict()) {
2422 DCHECK_NE(failures_.size(), 0U);
2423 break; // bad class
jeffhao8cd6dda2012-02-22 10:15:34 -08002424 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002425 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
2426 // can't create an instance of an interface or abstract class */
2427 if (!res_type.IsInstantiableTypes()) {
2428 Fail(VERIFY_ERROR_INSTANTIATION)
2429 << "new-instance on primitive, interface or abstract class" << res_type;
Ian Rogers08f753d2012-08-24 14:35:25 -07002430 // Soft failure so carry on to set register type.
Ian Rogersd81871c2011-10-03 13:57:23 -07002431 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00002432 const RegType& uninit_type = reg_types_.Uninitialized(res_type, work_insn_idx_);
Ian Rogers08f753d2012-08-24 14:35:25 -07002433 // Any registers holding previous allocations from this address that have not yet been
2434 // initialized must be marked invalid.
Ian Rogers7b078e82014-09-10 14:44:24 -07002435 work_line_->MarkUninitRefsAsInvalid(this, uninit_type);
Ian Rogers08f753d2012-08-24 14:35:25 -07002436 // add the new uninitialized reference to the register state
Andreas Gampead238ce2015-08-24 21:13:08 -07002437 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_21c(), uninit_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07002438 break;
2439 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08002440 case Instruction::NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002441 VerifyNewArray(inst, false, false);
jeffhaobdb76512011-09-07 11:43:16 -07002442 break;
2443 case Instruction::FILLED_NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002444 VerifyNewArray(inst, true, false);
Ian Rogers0c4a5062012-02-03 15:18:59 -08002445 just_set_result = true; // Filled new array sets result register
jeffhaobdb76512011-09-07 11:43:16 -07002446 break;
Ian Rogers0c4a5062012-02-03 15:18:59 -08002447 case Instruction::FILLED_NEW_ARRAY_RANGE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002448 VerifyNewArray(inst, true, true);
Ian Rogers0c4a5062012-02-03 15:18:59 -08002449 just_set_result = true; // Filled new array range sets result register
2450 break;
jeffhaobdb76512011-09-07 11:43:16 -07002451 case Instruction::CMPL_FLOAT:
2452 case Instruction::CMPG_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002453 if (!work_line_->VerifyRegisterType(this, inst->VRegB_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08002454 break;
2455 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002456 if (!work_line_->VerifyRegisterType(this, inst->VRegC_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08002457 break;
2458 }
Andreas Gampead238ce2015-08-24 21:13:08 -07002459 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002460 break;
2461 case Instruction::CMPL_DOUBLE:
2462 case Instruction::CMPG_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002463 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegB_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002464 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002465 break;
2466 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002467 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegC_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002468 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002469 break;
2470 }
Andreas Gampead238ce2015-08-24 21:13:08 -07002471 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002472 break;
2473 case Instruction::CMP_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002474 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegB_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002475 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002476 break;
2477 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002478 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegC_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002479 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002480 break;
2481 }
Andreas Gampead238ce2015-08-24 21:13:08 -07002482 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002483 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002484 case Instruction::THROW: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002485 const RegType& res_type = work_line_->GetRegisterType(this, inst->VRegA_11x());
David Brazdilca3c8c32016-09-06 14:04:48 +01002486 if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(res_type, this)) {
David Brazdil68b5c0b2016-01-19 14:25:29 +00002487 if (res_type.IsUninitializedTypes()) {
2488 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "thrown exception not initialized";
Pavel Vyssotski980027c2016-02-11 20:28:11 +06002489 } else if (!res_type.IsReferenceTypes()) {
2490 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "thrown value of non-reference type " << res_type;
David Brazdil68b5c0b2016-01-19 14:25:29 +00002491 } else {
2492 Fail(res_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS : VERIFY_ERROR_BAD_CLASS_SOFT)
2493 << "thrown class " << res_type << " not instanceof Throwable";
2494 }
jeffhaobdb76512011-09-07 11:43:16 -07002495 }
2496 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002497 }
jeffhaobdb76512011-09-07 11:43:16 -07002498 case Instruction::GOTO:
2499 case Instruction::GOTO_16:
2500 case Instruction::GOTO_32:
2501 /* no effect on or use of registers */
2502 break;
2503
2504 case Instruction::PACKED_SWITCH:
2505 case Instruction::SPARSE_SWITCH:
2506 /* verify that vAA is an integer, or can be converted to one */
Ian Rogers7b078e82014-09-10 14:44:24 -07002507 work_line_->VerifyRegisterType(this, inst->VRegA_31t(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002508 break;
2509
Ian Rogersd81871c2011-10-03 13:57:23 -07002510 case Instruction::FILL_ARRAY_DATA: {
2511 /* Similar to the verification done for APUT */
Ian Rogers7b078e82014-09-10 14:44:24 -07002512 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegA_31t());
Ian Rogers89310de2012-02-01 13:47:30 -08002513 /* array_type can be null if the reg type is Zero */
Andreas Gampeeed3a5d2017-11-29 14:58:34 -08002514 if (!array_type.IsZeroOrNull()) {
jeffhao457cc512012-02-02 16:55:13 -08002515 if (!array_type.IsArrayTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002516 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with array type "
2517 << array_type;
Andreas Gampebb18a032016-03-22 20:34:25 -07002518 } else if (array_type.IsUnresolvedTypes()) {
2519 // If it's an unresolved array type, it must be non-primitive.
2520 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data for array of type "
2521 << array_type;
Ian Rogers89310de2012-02-01 13:47:30 -08002522 } else {
Ian Rogersd8f69b02014-09-10 21:43:52 +00002523 const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
Ian Rogersad0b3a32012-04-16 14:50:24 -07002524 DCHECK(!component_type.IsConflict());
jeffhao457cc512012-02-02 16:55:13 -08002525 if (component_type.IsNonZeroReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002526 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with component type "
2527 << component_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002528 } else {
jeffhao457cc512012-02-02 16:55:13 -08002529 // Now verify if the element width in the table matches the element width declared in
2530 // the array
Andreas Gampe53de99c2015-08-17 13:43:55 -07002531 const uint16_t* array_data =
2532 insns + (insns[1] | (static_cast<int32_t>(insns[2]) << 16));
jeffhao457cc512012-02-02 16:55:13 -08002533 if (array_data[0] != Instruction::kArrayDataSignature) {
jeffhaod5347e02012-03-22 17:25:05 -07002534 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid magic for array-data";
jeffhao457cc512012-02-02 16:55:13 -08002535 } else {
2536 size_t elem_width = Primitive::ComponentSize(component_type.GetPrimitiveType());
2537 // Since we don't compress the data in Dex, expect to see equal width of data stored
2538 // in the table and expected from the array class.
2539 if (array_data[1] != elem_width) {
jeffhaod5347e02012-03-22 17:25:05 -07002540 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-data size mismatch (" << array_data[1]
2541 << " vs " << elem_width << ")";
jeffhao457cc512012-02-02 16:55:13 -08002542 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002543 }
2544 }
jeffhaobdb76512011-09-07 11:43:16 -07002545 }
2546 }
2547 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002548 }
jeffhaobdb76512011-09-07 11:43:16 -07002549 case Instruction::IF_EQ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002550 case Instruction::IF_NE: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002551 const RegType& reg_type1 = work_line_->GetRegisterType(this, inst->VRegA_22t());
2552 const RegType& reg_type2 = work_line_->GetRegisterType(this, inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002553 bool mismatch = false;
Andreas Gampeeed3a5d2017-11-29 14:58:34 -08002554 if (reg_type1.IsZeroOrNull()) { // zero then integral or reference expected
Ian Rogersd81871c2011-10-03 13:57:23 -07002555 mismatch = !reg_type2.IsReferenceTypes() && !reg_type2.IsIntegralTypes();
2556 } else if (reg_type1.IsReferenceTypes()) { // both references?
2557 mismatch = !reg_type2.IsReferenceTypes();
2558 } else { // both integral?
2559 mismatch = !reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes();
2560 }
2561 if (mismatch) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002562 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to if-eq/if-ne (" << reg_type1 << ","
2563 << reg_type2 << ") must both be references or integral";
jeffhaobdb76512011-09-07 11:43:16 -07002564 }
2565 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002566 }
jeffhaobdb76512011-09-07 11:43:16 -07002567 case Instruction::IF_LT:
2568 case Instruction::IF_GE:
2569 case Instruction::IF_GT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002570 case Instruction::IF_LE: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002571 const RegType& reg_type1 = work_line_->GetRegisterType(this, inst->VRegA_22t());
2572 const RegType& reg_type2 = work_line_->GetRegisterType(this, inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002573 if (!reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002574 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to 'if' (" << reg_type1 << ","
2575 << reg_type2 << ") must be integral";
jeffhaobdb76512011-09-07 11:43:16 -07002576 }
2577 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002578 }
jeffhaobdb76512011-09-07 11:43:16 -07002579 case Instruction::IF_EQZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002580 case Instruction::IF_NEZ: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002581 const RegType& reg_type = work_line_->GetRegisterType(this, inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002582 if (!reg_type.IsReferenceTypes() && !reg_type.IsIntegralTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002583 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
2584 << " unexpected as arg to if-eqz/if-nez";
Ian Rogersd81871c2011-10-03 13:57:23 -07002585 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002586
2587 // Find previous instruction - its existence is a precondition to peephole optimization.
Ian Rogers9b360392013-06-06 14:45:07 -07002588 uint32_t instance_of_idx = 0;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002589 if (0 != work_insn_idx_) {
Ian Rogers9b360392013-06-06 14:45:07 -07002590 instance_of_idx = work_insn_idx_ - 1;
Mathieu Chartierde40d472015-10-15 17:47:48 -07002591 while (0 != instance_of_idx && !GetInstructionFlags(instance_of_idx).IsOpcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07002592 instance_of_idx--;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002593 }
Mathieu Chartierc2a2bda2018-03-12 11:21:52 -07002594 if (FailOrAbort(GetInstructionFlags(instance_of_idx).IsOpcode(),
Andreas Gampe7c038102014-10-27 20:08:46 -07002595 "Unable to get previous instruction of if-eqz/if-nez for work index ",
2596 work_insn_idx_)) {
2597 break;
2598 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002599 } else {
2600 break;
2601 }
2602
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08002603 const Instruction& instance_of_inst = code_item_accessor_.InstructionAt(instance_of_idx);
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002604
2605 /* Check for peep-hole pattern of:
2606 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002607 * instance-of vX, vY, T;
2608 * ifXXX vX, label ;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002609 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002610 * label:
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002611 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002612 * and sharpen the type of vY to be type T.
2613 * Note, this pattern can't be if:
2614 * - if there are other branches to this branch,
2615 * - when vX == vY.
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002616 */
Ian Rogersfae370a2013-06-05 08:33:27 -07002617 if (!CurrentInsnFlags()->IsBranchTarget() &&
Vladimir Markod7559b72017-09-28 13:50:37 +01002618 (Instruction::INSTANCE_OF == instance_of_inst.Opcode()) &&
2619 (inst->VRegA_21t() == instance_of_inst.VRegA_22c()) &&
2620 (instance_of_inst.VRegA_22c() != instance_of_inst.VRegB_22c())) {
Ian Rogersebbdd872014-07-07 23:53:08 -07002621 // Check the type of the instance-of is different than that of registers type, as if they
2622 // are the same there is no work to be done here. Check that the conversion is not to or
2623 // from an unresolved type as type information is imprecise. If the instance-of is to an
2624 // interface then ignore the type information as interfaces can only be treated as Objects
2625 // and we don't want to disallow field and other operations on the object. If the value
2626 // being instance-of checked against is known null (zero) then allow the optimization as
2627 // we didn't have type information. If the merge of the instance-of type with the original
2628 // type is assignable to the original then allow optimization. This check is performed to
2629 // ensure that subsequent merges don't lose type information - such as becoming an
2630 // interface from a class that would lose information relevant to field checks.
Vladimir Markod7559b72017-09-28 13:50:37 +01002631 const RegType& orig_type = work_line_->GetRegisterType(this, instance_of_inst.VRegB_22c());
Andreas Gampe98be1a92017-08-28 08:25:45 -07002632 const RegType& cast_type = ResolveClass<CheckAccess::kYes>(
Vladimir Markod7559b72017-09-28 13:50:37 +01002633 dex::TypeIndex(instance_of_inst.VRegC_22c()));
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002634
Ian Rogersebbdd872014-07-07 23:53:08 -07002635 if (!orig_type.Equals(cast_type) &&
2636 !cast_type.IsUnresolvedTypes() && !orig_type.IsUnresolvedTypes() &&
Andreas Gampe00633eb2014-07-17 16:13:35 -07002637 cast_type.HasClass() && // Could be conflict type, make sure it has a class.
Ian Rogersebbdd872014-07-07 23:53:08 -07002638 !cast_type.GetClass()->IsInterface() &&
Andreas Gampeeed3a5d2017-11-29 14:58:34 -08002639 (orig_type.IsZeroOrNull() ||
David Brazdilca3c8c32016-09-06 14:04:48 +01002640 orig_type.IsStrictlyAssignableFrom(
2641 cast_type.Merge(orig_type, &reg_types_, this), this))) {
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08002642 RegisterLine* update_line = RegisterLine::Create(code_item_accessor_.RegistersSize(),
2643 this);
Ian Rogersfae370a2013-06-05 08:33:27 -07002644 if (inst->Opcode() == Instruction::IF_EQZ) {
Ian Rogers9b360392013-06-06 14:45:07 -07002645 fallthrough_line.reset(update_line);
Ian Rogersfae370a2013-06-05 08:33:27 -07002646 } else {
Ian Rogers9b360392013-06-06 14:45:07 -07002647 branch_line.reset(update_line);
2648 }
2649 update_line->CopyFromLine(work_line_.get());
Andreas Gampead238ce2015-08-24 21:13:08 -07002650 update_line->SetRegisterType<LockOp::kKeep>(this,
Vladimir Markod7559b72017-09-28 13:50:37 +01002651 instance_of_inst.VRegB_22c(),
Andreas Gampead238ce2015-08-24 21:13:08 -07002652 cast_type);
Mathieu Chartierde40d472015-10-15 17:47:48 -07002653 if (!GetInstructionFlags(instance_of_idx).IsBranchTarget() && 0 != instance_of_idx) {
Ian Rogers9b360392013-06-06 14:45:07 -07002654 // See if instance-of was preceded by a move-object operation, common due to the small
2655 // register encoding space of instance-of, and propagate type information to the source
2656 // of the move-object.
2657 uint32_t move_idx = instance_of_idx - 1;
Mathieu Chartierde40d472015-10-15 17:47:48 -07002658 while (0 != move_idx && !GetInstructionFlags(move_idx).IsOpcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07002659 move_idx--;
2660 }
Mathieu Chartierc2a2bda2018-03-12 11:21:52 -07002661 if (FailOrAbort(GetInstructionFlags(move_idx).IsOpcode(),
Andreas Gampe7c038102014-10-27 20:08:46 -07002662 "Unable to get previous instruction of if-eqz/if-nez for work index ",
2663 work_insn_idx_)) {
2664 break;
2665 }
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08002666 const Instruction& move_inst = code_item_accessor_.InstructionAt(move_idx);
Vladimir Markod7559b72017-09-28 13:50:37 +01002667 switch (move_inst.Opcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07002668 case Instruction::MOVE_OBJECT:
Vladimir Markod7559b72017-09-28 13:50:37 +01002669 if (move_inst.VRegA_12x() == instance_of_inst.VRegB_22c()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07002670 update_line->SetRegisterType<LockOp::kKeep>(this,
Vladimir Markod7559b72017-09-28 13:50:37 +01002671 move_inst.VRegB_12x(),
Andreas Gampead238ce2015-08-24 21:13:08 -07002672 cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002673 }
2674 break;
2675 case Instruction::MOVE_OBJECT_FROM16:
Vladimir Markod7559b72017-09-28 13:50:37 +01002676 if (move_inst.VRegA_22x() == instance_of_inst.VRegB_22c()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07002677 update_line->SetRegisterType<LockOp::kKeep>(this,
Vladimir Markod7559b72017-09-28 13:50:37 +01002678 move_inst.VRegB_22x(),
Andreas Gampead238ce2015-08-24 21:13:08 -07002679 cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002680 }
2681 break;
2682 case Instruction::MOVE_OBJECT_16:
Vladimir Markod7559b72017-09-28 13:50:37 +01002683 if (move_inst.VRegA_32x() == instance_of_inst.VRegB_22c()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07002684 update_line->SetRegisterType<LockOp::kKeep>(this,
Vladimir Markod7559b72017-09-28 13:50:37 +01002685 move_inst.VRegB_32x(),
Andreas Gampead238ce2015-08-24 21:13:08 -07002686 cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002687 }
2688 break;
2689 default:
2690 break;
2691 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002692 }
2693 }
2694 }
2695
jeffhaobdb76512011-09-07 11:43:16 -07002696 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002697 }
jeffhaobdb76512011-09-07 11:43:16 -07002698 case Instruction::IF_LTZ:
2699 case Instruction::IF_GEZ:
2700 case Instruction::IF_GTZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002701 case Instruction::IF_LEZ: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002702 const RegType& reg_type = work_line_->GetRegisterType(this, inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002703 if (!reg_type.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002704 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
2705 << " unexpected as arg to if-ltz/if-gez/if-gtz/if-lez";
Ian Rogersd81871c2011-10-03 13:57:23 -07002706 }
jeffhaobdb76512011-09-07 11:43:16 -07002707 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002708 }
jeffhaobdb76512011-09-07 11:43:16 -07002709 case Instruction::AGET_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002710 VerifyAGet(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002711 break;
jeffhaobdb76512011-09-07 11:43:16 -07002712 case Instruction::AGET_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002713 VerifyAGet(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002714 break;
jeffhaobdb76512011-09-07 11:43:16 -07002715 case Instruction::AGET_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002716 VerifyAGet(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002717 break;
jeffhaobdb76512011-09-07 11:43:16 -07002718 case Instruction::AGET_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002719 VerifyAGet(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002720 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002721 case Instruction::AGET:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002722 VerifyAGet(inst, reg_types_.Integer(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002723 break;
jeffhaobdb76512011-09-07 11:43:16 -07002724 case Instruction::AGET_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002725 VerifyAGet(inst, reg_types_.LongLo(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002726 break;
2727 case Instruction::AGET_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002728 VerifyAGet(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07002729 break;
2730
Ian Rogersd81871c2011-10-03 13:57:23 -07002731 case Instruction::APUT_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002732 VerifyAPut(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002733 break;
2734 case Instruction::APUT_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002735 VerifyAPut(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002736 break;
2737 case Instruction::APUT_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002738 VerifyAPut(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002739 break;
2740 case Instruction::APUT_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002741 VerifyAPut(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002742 break;
2743 case Instruction::APUT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002744 VerifyAPut(inst, reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002745 break;
2746 case Instruction::APUT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002747 VerifyAPut(inst, reg_types_.LongLo(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002748 break;
2749 case Instruction::APUT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002750 VerifyAPut(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07002751 break;
2752
jeffhaobdb76512011-09-07 11:43:16 -07002753 case Instruction::IGET_BOOLEAN:
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00002754 case Instruction::IGET_BOOLEAN_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002755 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002756 break;
jeffhaobdb76512011-09-07 11:43:16 -07002757 case Instruction::IGET_BYTE:
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00002758 case Instruction::IGET_BYTE_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002759 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002760 break;
jeffhaobdb76512011-09-07 11:43:16 -07002761 case Instruction::IGET_CHAR:
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00002762 case Instruction::IGET_CHAR_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002763 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002764 break;
jeffhaobdb76512011-09-07 11:43:16 -07002765 case Instruction::IGET_SHORT:
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00002766 case Instruction::IGET_SHORT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002767 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002768 break;
2769 case Instruction::IGET:
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00002770 case Instruction::IGET_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002771 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002772 break;
2773 case Instruction::IGET_WIDE:
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00002774 case Instruction::IGET_WIDE_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002775 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002776 break;
2777 case Instruction::IGET_OBJECT:
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00002778 case Instruction::IGET_OBJECT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002779 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false,
2780 false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002781 break;
jeffhaobdb76512011-09-07 11:43:16 -07002782
Ian Rogersd81871c2011-10-03 13:57:23 -07002783 case Instruction::IPUT_BOOLEAN:
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00002784 case Instruction::IPUT_BOOLEAN_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002785 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002786 break;
2787 case Instruction::IPUT_BYTE:
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00002788 case Instruction::IPUT_BYTE_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002789 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002790 break;
2791 case Instruction::IPUT_CHAR:
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00002792 case Instruction::IPUT_CHAR_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002793 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002794 break;
2795 case Instruction::IPUT_SHORT:
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00002796 case Instruction::IPUT_SHORT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002797 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002798 break;
2799 case Instruction::IPUT:
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00002800 case Instruction::IPUT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002801 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002802 break;
2803 case Instruction::IPUT_WIDE:
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00002804 case Instruction::IPUT_WIDE_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002805 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002806 break;
jeffhaobdb76512011-09-07 11:43:16 -07002807 case Instruction::IPUT_OBJECT:
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00002808 case Instruction::IPUT_OBJECT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002809 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false,
2810 false);
jeffhaobdb76512011-09-07 11:43:16 -07002811 break;
2812
jeffhaobdb76512011-09-07 11:43:16 -07002813 case Instruction::SGET_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002814 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002815 break;
jeffhaobdb76512011-09-07 11:43:16 -07002816 case Instruction::SGET_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002817 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002818 break;
jeffhaobdb76512011-09-07 11:43:16 -07002819 case Instruction::SGET_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002820 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002821 break;
jeffhaobdb76512011-09-07 11:43:16 -07002822 case Instruction::SGET_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002823 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002824 break;
2825 case Instruction::SGET:
Andreas Gampe896df402014-10-20 22:25:29 -07002826 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002827 break;
2828 case Instruction::SGET_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002829 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002830 break;
2831 case Instruction::SGET_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002832 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false,
2833 true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002834 break;
2835
2836 case Instruction::SPUT_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002837 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002838 break;
2839 case Instruction::SPUT_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002840 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002841 break;
2842 case Instruction::SPUT_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002843 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002844 break;
2845 case Instruction::SPUT_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002846 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002847 break;
2848 case Instruction::SPUT:
Andreas Gampe896df402014-10-20 22:25:29 -07002849 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002850 break;
2851 case Instruction::SPUT_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002852 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002853 break;
2854 case Instruction::SPUT_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002855 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false,
2856 true);
jeffhaobdb76512011-09-07 11:43:16 -07002857 break;
2858
2859 case Instruction::INVOKE_VIRTUAL:
2860 case Instruction::INVOKE_VIRTUAL_RANGE:
2861 case Instruction::INVOKE_SUPER:
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00002862 case Instruction::INVOKE_SUPER_RANGE:
2863 case Instruction::INVOKE_VIRTUAL_QUICK:
2864 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002865 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE ||
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00002866 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE ||
2867 inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002868 bool is_super = (inst->Opcode() == Instruction::INVOKE_SUPER ||
2869 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
Alex Light7268d472016-01-20 15:50:01 -08002870 MethodType type = is_super ? METHOD_SUPER : METHOD_VIRTUAL;
2871 ArtMethod* called_method = VerifyInvocationArgs(inst, type, is_range);
Ian Rogersd8f69b02014-09-10 21:43:52 +00002872 const RegType* return_type = nullptr;
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002873 if (called_method != nullptr) {
Vladimir Markob45528c2017-07-27 14:14:28 +01002874 ObjPtr<mirror::Class> return_type_class = can_load_classes_
2875 ? called_method->ResolveReturnType()
2876 : called_method->LookupResolvedReturnType();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002877 if (return_type_class != nullptr) {
Andreas Gampef23f33d2015-06-23 14:18:17 -07002878 return_type = &FromClass(called_method->GetReturnTypeDescriptor(),
Vladimir Markob45528c2017-07-27 14:14:28 +01002879 return_type_class.Ptr(),
Andreas Gampef23f33d2015-06-23 14:18:17 -07002880 return_type_class->CannotBeAssignedFromOtherTypes());
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002881 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07002882 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
2883 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002884 }
2885 }
2886 if (return_type == nullptr) {
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00002887 uint32_t method_idx = GetMethodIdxOfInvoke(inst);
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08002888 const dex::MethodId& method_id = dex_file_->GetMethodId(method_idx);
Andreas Gampea5b09a62016-11-17 15:21:22 -08002889 dex::TypeIndex return_type_idx =
2890 dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002891 const char* descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Mathieu Chartierbf99f772014-08-23 16:37:27 -07002892 return_type = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
jeffhaobdb76512011-09-07 11:43:16 -07002893 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002894 if (!return_type->IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002895 work_line_->SetResultRegisterType(this, *return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002896 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002897 work_line_->SetResultRegisterTypeWide(*return_type, return_type->HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002898 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002899 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002900 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002901 }
jeffhaobdb76512011-09-07 11:43:16 -07002902 case Instruction::INVOKE_DIRECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002903 case Instruction::INVOKE_DIRECT_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002904 bool is_range = (inst->Opcode() == Instruction::INVOKE_DIRECT_RANGE);
Alex Light7268d472016-01-20 15:50:01 -08002905 ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_DIRECT, is_range);
Ian Rogers46685432012-06-03 22:26:43 -07002906 const char* return_type_descriptor;
2907 bool is_constructor;
Ian Rogersd8f69b02014-09-10 21:43:52 +00002908 const RegType* return_type = nullptr;
Ian Rogers7b078e82014-09-10 14:44:24 -07002909 if (called_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002910 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08002911 const dex::MethodId& method_id = dex_file_->GetMethodId(method_idx);
Ian Rogersdfb325e2013-10-30 01:00:44 -07002912 is_constructor = strcmp("<init>", dex_file_->StringDataByIdx(method_id.name_idx_)) == 0;
Andreas Gampea5b09a62016-11-17 15:21:22 -08002913 dex::TypeIndex return_type_idx =
2914 dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogers46685432012-06-03 22:26:43 -07002915 return_type_descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2916 } else {
2917 is_constructor = called_method->IsConstructor();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002918 return_type_descriptor = called_method->GetReturnTypeDescriptor();
Vladimir Markob45528c2017-07-27 14:14:28 +01002919 ObjPtr<mirror::Class> return_type_class = can_load_classes_
2920 ? called_method->ResolveReturnType()
2921 : called_method->LookupResolvedReturnType();
Ian Rogers1ff3c982014-08-12 02:30:58 -07002922 if (return_type_class != nullptr) {
Andreas Gampef23f33d2015-06-23 14:18:17 -07002923 return_type = &FromClass(return_type_descriptor,
Vladimir Markobcf17522018-06-01 13:14:32 +01002924 return_type_class,
Andreas Gampef23f33d2015-06-23 14:18:17 -07002925 return_type_class->CannotBeAssignedFromOtherTypes());
Ian Rogers1ff3c982014-08-12 02:30:58 -07002926 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07002927 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
2928 self_->ClearException();
Ian Rogers1ff3c982014-08-12 02:30:58 -07002929 }
Ian Rogers46685432012-06-03 22:26:43 -07002930 }
2931 if (is_constructor) {
jeffhaobdb76512011-09-07 11:43:16 -07002932 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002933 * Some additional checks when calling a constructor. We know from the invocation arg check
2934 * that the "this" argument is an instance of called_method->klass. Now we further restrict
2935 * that to require that called_method->klass is the same as this->klass or this->super,
2936 * allowing the latter only if the "this" argument is the same as the "this" argument to
2937 * this method (which implies that we're in a constructor ourselves).
jeffhaobdb76512011-09-07 11:43:16 -07002938 */
Orion Hodsoncfa325e2016-10-13 10:25:54 +01002939 const RegType& this_type = work_line_->GetInvocationThis(this, inst);
jeffhaob57e9522012-04-26 18:08:21 -07002940 if (this_type.IsConflict()) // failure.
2941 break;
jeffhaobdb76512011-09-07 11:43:16 -07002942
jeffhaob57e9522012-04-26 18:08:21 -07002943 /* no null refs allowed (?) */
Andreas Gampeeed3a5d2017-11-29 14:58:34 -08002944 if (this_type.IsZeroOrNull()) {
jeffhaob57e9522012-04-26 18:08:21 -07002945 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unable to initialize null ref";
2946 break;
jeffhao2a8a90e2011-09-26 14:25:31 -07002947 }
jeffhaob57e9522012-04-26 18:08:21 -07002948
2949 /* must be in same class or in superclass */
Ian Rogersd8f69b02014-09-10 21:43:52 +00002950 // const RegType& this_super_klass = this_type.GetSuperClass(&reg_types_);
Ian Rogers46685432012-06-03 22:26:43 -07002951 // TODO: re-enable constructor type verification
2952 // if (this_super_klass.IsConflict()) {
jeffhaob57e9522012-04-26 18:08:21 -07002953 // Unknown super class, fail so we re-check at runtime.
Ian Rogers46685432012-06-03 22:26:43 -07002954 // Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "super class unknown for '" << this_type << "'";
2955 // break;
2956 // }
jeffhaob57e9522012-04-26 18:08:21 -07002957
2958 /* arg must be an uninitialized reference */
2959 if (!this_type.IsUninitializedTypes()) {
2960 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Expected initialization on uninitialized reference "
2961 << this_type;
2962 break;
2963 }
2964
2965 /*
2966 * Replace the uninitialized reference with an initialized one. We need to do this for all
2967 * registers that have the same object instance in them, not just the "this" register.
2968 */
Nicolas Geoffray98e6ce42016-02-16 18:42:15 +00002969 work_line_->MarkRefsAsInitialized(this, this_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002970 }
Ian Rogers1ff3c982014-08-12 02:30:58 -07002971 if (return_type == nullptr) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07002972 return_type = &reg_types_.FromDescriptor(GetClassLoader(), return_type_descriptor, false);
Ian Rogers1ff3c982014-08-12 02:30:58 -07002973 }
2974 if (!return_type->IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002975 work_line_->SetResultRegisterType(this, *return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002976 } else {
Ian Rogers1ff3c982014-08-12 02:30:58 -07002977 work_line_->SetResultRegisterTypeWide(*return_type, return_type->HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002978 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002979 just_set_result = true;
2980 break;
2981 }
2982 case Instruction::INVOKE_STATIC:
2983 case Instruction::INVOKE_STATIC_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002984 bool is_range = (inst->Opcode() == Instruction::INVOKE_STATIC_RANGE);
Alex Light7268d472016-01-20 15:50:01 -08002985 ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_STATIC, is_range);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002986 const char* descriptor;
Ian Rogers7b078e82014-09-10 14:44:24 -07002987 if (called_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002988 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08002989 const dex::MethodId& method_id = dex_file_->GetMethodId(method_idx);
Andreas Gampea5b09a62016-11-17 15:21:22 -08002990 dex::TypeIndex return_type_idx =
2991 dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Mathieu Chartier590fee92013-09-13 13:46:47 -07002992 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002993 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002994 descriptor = called_method->GetReturnTypeDescriptor();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002995 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00002996 const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002997 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002998 work_line_->SetResultRegisterType(this, return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002999 } else {
3000 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
3001 }
jeffhaobdb76512011-09-07 11:43:16 -07003002 just_set_result = true;
3003 }
3004 break;
jeffhaobdb76512011-09-07 11:43:16 -07003005 case Instruction::INVOKE_INTERFACE:
Ian Rogersd81871c2011-10-03 13:57:23 -07003006 case Instruction::INVOKE_INTERFACE_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003007 bool is_range = (inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE);
Alex Light7268d472016-01-20 15:50:01 -08003008 ArtMethod* abs_method = VerifyInvocationArgs(inst, METHOD_INTERFACE, is_range);
Ian Rogers7b078e82014-09-10 14:44:24 -07003009 if (abs_method != nullptr) {
Vladimir Markod93e3742018-07-18 10:58:13 +01003010 ObjPtr<mirror::Class> called_interface = abs_method->GetDeclaringClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07003011 if (!called_interface->IsInterface() && !called_interface->IsObjectClass()) {
3012 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected interface class in invoke-interface '"
David Sehr709b0702016-10-13 09:12:37 -07003013 << abs_method->PrettyMethod() << "'";
Ian Rogersad0b3a32012-04-16 14:50:24 -07003014 break;
Ian Rogers28ad40d2011-10-27 15:19:26 -07003015 }
Ian Rogers0d604842012-04-16 14:50:24 -07003016 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003017 /* Get the type of the "this" arg, which should either be a sub-interface of called
3018 * interface or Object (see comments in RegType::JoinClass).
3019 */
Orion Hodsoncfa325e2016-10-13 10:25:54 +01003020 const RegType& this_type = work_line_->GetInvocationThis(this, inst);
Andreas Gampeeed3a5d2017-11-29 14:58:34 -08003021 if (this_type.IsZeroOrNull()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003022 /* null pointer always passes (and always fails at runtime) */
3023 } else {
3024 if (this_type.IsUninitializedTypes()) {
3025 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface call on uninitialized object "
3026 << this_type;
3027 break;
3028 }
3029 // In the past we have tried to assert that "called_interface" is assignable
3030 // from "this_type.GetClass()", however, as we do an imprecise Join
3031 // (RegType::JoinClass) we don't have full information on what interfaces are
3032 // implemented by "this_type". For example, two classes may implement the same
3033 // interfaces and have a common parent that doesn't implement the interface. The
3034 // join will set "this_type" to the parent class and a test that this implements
3035 // the interface will incorrectly fail.
3036 }
3037 /*
3038 * We don't have an object instance, so we can't find the concrete method. However, all of
3039 * the type information is in the abstract method, so we're good.
3040 */
3041 const char* descriptor;
Ian Rogers7b078e82014-09-10 14:44:24 -07003042 if (abs_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003043 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08003044 const dex::MethodId& method_id = dex_file_->GetMethodId(method_idx);
Andreas Gampea5b09a62016-11-17 15:21:22 -08003045 dex::TypeIndex return_type_idx =
3046 dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Mathieu Chartierde40d472015-10-15 17:47:48 -07003047 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003048 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003049 descriptor = abs_method->GetReturnTypeDescriptor();
Ian Rogersad0b3a32012-04-16 14:50:24 -07003050 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00003051 const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003052 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003053 work_line_->SetResultRegisterType(this, return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003054 } else {
3055 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
3056 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003057 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07003058 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07003059 }
Narayan Kamath9823e782016-08-03 12:46:58 +01003060 case Instruction::INVOKE_POLYMORPHIC:
3061 case Instruction::INVOKE_POLYMORPHIC_RANGE: {
Orion Hodsoncfa325e2016-10-13 10:25:54 +01003062 bool is_range = (inst->Opcode() == Instruction::INVOKE_POLYMORPHIC_RANGE);
3063 ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_POLYMORPHIC, is_range);
3064 if (called_method == nullptr) {
3065 // Convert potential soft failures in VerifyInvocationArgs() to hard errors.
3066 if (failure_messages_.size() > 0) {
3067 std::string message = failure_messages_.back()->str();
3068 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << message;
3069 } else {
3070 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invoke-polymorphic verification failure.";
3071 }
3072 break;
3073 }
3074 if (!CheckSignaturePolymorphicMethod(called_method) ||
3075 !CheckSignaturePolymorphicReceiver(inst)) {
Orion Hodsonfe92d122018-01-02 10:45:17 +00003076 DCHECK(HasFailures());
Orion Hodsoncfa325e2016-10-13 10:25:54 +01003077 break;
3078 }
Orion Hodson06d10a72018-05-14 08:53:38 +01003079 const uint16_t vRegH = (is_range) ? inst->VRegH_4rcc() : inst->VRegH_45cc();
3080 const dex::ProtoIndex proto_idx(vRegH);
Orion Hodsonac141392017-01-13 11:53:47 +00003081 const char* return_descriptor =
Orion Hodsoncfa325e2016-10-13 10:25:54 +01003082 dex_file_->GetReturnTypeDescriptor(dex_file_->GetProtoId(proto_idx));
3083 const RegType& return_type =
Orion Hodsonac141392017-01-13 11:53:47 +00003084 reg_types_.FromDescriptor(GetClassLoader(), return_descriptor, false);
Orion Hodsoncfa325e2016-10-13 10:25:54 +01003085 if (!return_type.IsLowHalf()) {
3086 work_line_->SetResultRegisterType(this, return_type);
3087 } else {
3088 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
3089 }
Orion Hodsonac141392017-01-13 11:53:47 +00003090 just_set_result = true;
Orion Hodsoncfa325e2016-10-13 10:25:54 +01003091 break;
Narayan Kamath9823e782016-08-03 12:46:58 +01003092 }
Orion Hodsonc069a302017-01-18 09:23:12 +00003093 case Instruction::INVOKE_CUSTOM:
3094 case Instruction::INVOKE_CUSTOM_RANGE: {
3095 // Verify registers based on method_type in the call site.
3096 bool is_range = (inst->Opcode() == Instruction::INVOKE_CUSTOM_RANGE);
3097
3098 // Step 1. Check the call site that produces the method handle for invocation
3099 const uint32_t call_site_idx = is_range ? inst->VRegB_3rc() : inst->VRegB_35c();
3100 if (!CheckCallSite(call_site_idx)) {
3101 DCHECK(HasFailures());
3102 break;
3103 }
3104
3105 // Step 2. Check the register arguments correspond to the expected arguments for the
3106 // method handle produced by step 1. The dex file verifier has checked ranges for
3107 // the first three arguments and CheckCallSite has checked the method handle type.
Orion Hodson4c8e12e2018-05-18 08:33:20 +01003108 const dex::ProtoIndex proto_idx = dex_file_->GetProtoIndexForCallSite(call_site_idx);
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08003109 const dex::ProtoId& proto_id = dex_file_->GetProtoId(proto_idx);
Orion Hodsonc069a302017-01-18 09:23:12 +00003110 DexFileParameterIterator param_it(*dex_file_, proto_id);
3111 // Treat method as static as it has yet to be determined.
3112 VerifyInvocationArgsFromIterator(&param_it, inst, METHOD_STATIC, is_range, nullptr);
3113 const char* return_descriptor = dex_file_->GetReturnTypeDescriptor(proto_id);
3114
3115 // Step 3. Propagate return type information
3116 const RegType& return_type =
3117 reg_types_.FromDescriptor(GetClassLoader(), return_descriptor, false);
3118 if (!return_type.IsLowHalf()) {
3119 work_line_->SetResultRegisterType(this, return_type);
3120 } else {
3121 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
3122 }
3123 just_set_result = true;
Orion Hodsonc069a302017-01-18 09:23:12 +00003124 break;
3125 }
jeffhaobdb76512011-09-07 11:43:16 -07003126 case Instruction::NEG_INT:
3127 case Instruction::NOT_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003128 work_line_->CheckUnaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003129 break;
3130 case Instruction::NEG_LONG:
3131 case Instruction::NOT_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07003132 work_line_->CheckUnaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003133 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07003134 break;
3135 case Instruction::NEG_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003136 work_line_->CheckUnaryOp(this, inst, reg_types_.Float(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07003137 break;
3138 case Instruction::NEG_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07003139 work_line_->CheckUnaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003140 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07003141 break;
3142 case Instruction::INT_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07003143 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003144 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003145 break;
3146 case Instruction::INT_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003147 work_line_->CheckUnaryOp(this, inst, reg_types_.Float(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003148 break;
3149 case Instruction::INT_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07003150 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003151 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003152 break;
3153 case Instruction::LONG_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003154 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003155 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07003156 break;
3157 case Instruction::LONG_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003158 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003159 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07003160 break;
3161 case Instruction::LONG_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07003162 work_line_->CheckUnaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003163 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07003164 break;
3165 case Instruction::FLOAT_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003166 work_line_->CheckUnaryOp(this, inst, reg_types_.Integer(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07003167 break;
3168 case Instruction::FLOAT_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07003169 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003170 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07003171 break;
3172 case Instruction::FLOAT_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07003173 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003174 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07003175 break;
3176 case Instruction::DOUBLE_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003177 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003178 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07003179 break;
3180 case Instruction::DOUBLE_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07003181 work_line_->CheckUnaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003182 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07003183 break;
3184 case Instruction::DOUBLE_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003185 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003186 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07003187 break;
3188 case Instruction::INT_TO_BYTE:
Ian Rogers7b078e82014-09-10 14:44:24 -07003189 work_line_->CheckUnaryOp(this, inst, reg_types_.Byte(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003190 break;
3191 case Instruction::INT_TO_CHAR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003192 work_line_->CheckUnaryOp(this, inst, reg_types_.Char(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003193 break;
3194 case Instruction::INT_TO_SHORT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003195 work_line_->CheckUnaryOp(this, inst, reg_types_.Short(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003196 break;
3197
3198 case Instruction::ADD_INT:
3199 case Instruction::SUB_INT:
3200 case Instruction::MUL_INT:
3201 case Instruction::REM_INT:
3202 case Instruction::DIV_INT:
3203 case Instruction::SHL_INT:
3204 case Instruction::SHR_INT:
3205 case Instruction::USHR_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003206 work_line_->CheckBinaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003207 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07003208 break;
3209 case Instruction::AND_INT:
3210 case Instruction::OR_INT:
3211 case Instruction::XOR_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003212 work_line_->CheckBinaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003213 reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07003214 break;
3215 case Instruction::ADD_LONG:
3216 case Instruction::SUB_LONG:
3217 case Instruction::MUL_LONG:
3218 case Instruction::DIV_LONG:
3219 case Instruction::REM_LONG:
3220 case Instruction::AND_LONG:
3221 case Instruction::OR_LONG:
3222 case Instruction::XOR_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07003223 work_line_->CheckBinaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003224 reg_types_.LongLo(), reg_types_.LongHi(),
3225 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07003226 break;
3227 case Instruction::SHL_LONG:
3228 case Instruction::SHR_LONG:
3229 case Instruction::USHR_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07003230 /* shift distance is Int, making these different from other binary operations */
Ian Rogers7b078e82014-09-10 14:44:24 -07003231 work_line_->CheckBinaryOpWideShift(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003232 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003233 break;
3234 case Instruction::ADD_FLOAT:
3235 case Instruction::SUB_FLOAT:
3236 case Instruction::MUL_FLOAT:
3237 case Instruction::DIV_FLOAT:
3238 case Instruction::REM_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003239 work_line_->CheckBinaryOp(this, inst, reg_types_.Float(), reg_types_.Float(),
3240 reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07003241 break;
3242 case Instruction::ADD_DOUBLE:
3243 case Instruction::SUB_DOUBLE:
3244 case Instruction::MUL_DOUBLE:
3245 case Instruction::DIV_DOUBLE:
3246 case Instruction::REM_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07003247 work_line_->CheckBinaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003248 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
3249 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07003250 break;
3251 case Instruction::ADD_INT_2ADDR:
3252 case Instruction::SUB_INT_2ADDR:
3253 case Instruction::MUL_INT_2ADDR:
3254 case Instruction::REM_INT_2ADDR:
3255 case Instruction::SHL_INT_2ADDR:
3256 case Instruction::SHR_INT_2ADDR:
3257 case Instruction::USHR_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003258 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
3259 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07003260 break;
3261 case Instruction::AND_INT_2ADDR:
3262 case Instruction::OR_INT_2ADDR:
3263 case Instruction::XOR_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003264 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
3265 reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07003266 break;
3267 case Instruction::DIV_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003268 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
3269 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07003270 break;
3271 case Instruction::ADD_LONG_2ADDR:
3272 case Instruction::SUB_LONG_2ADDR:
3273 case Instruction::MUL_LONG_2ADDR:
3274 case Instruction::DIV_LONG_2ADDR:
3275 case Instruction::REM_LONG_2ADDR:
3276 case Instruction::AND_LONG_2ADDR:
3277 case Instruction::OR_LONG_2ADDR:
3278 case Instruction::XOR_LONG_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003279 work_line_->CheckBinaryOp2addrWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003280 reg_types_.LongLo(), reg_types_.LongHi(),
3281 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07003282 break;
3283 case Instruction::SHL_LONG_2ADDR:
3284 case Instruction::SHR_LONG_2ADDR:
3285 case Instruction::USHR_LONG_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003286 work_line_->CheckBinaryOp2addrWideShift(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003287 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003288 break;
3289 case Instruction::ADD_FLOAT_2ADDR:
3290 case Instruction::SUB_FLOAT_2ADDR:
3291 case Instruction::MUL_FLOAT_2ADDR:
3292 case Instruction::DIV_FLOAT_2ADDR:
3293 case Instruction::REM_FLOAT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003294 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Float(), reg_types_.Float(),
3295 reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07003296 break;
3297 case Instruction::ADD_DOUBLE_2ADDR:
3298 case Instruction::SUB_DOUBLE_2ADDR:
3299 case Instruction::MUL_DOUBLE_2ADDR:
3300 case Instruction::DIV_DOUBLE_2ADDR:
3301 case Instruction::REM_DOUBLE_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003302 work_line_->CheckBinaryOp2addrWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003303 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
3304 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07003305 break;
3306 case Instruction::ADD_INT_LIT16:
Ian Rogersf72a11d2014-10-30 15:41:08 -07003307 case Instruction::RSUB_INT_LIT16:
jeffhaobdb76512011-09-07 11:43:16 -07003308 case Instruction::MUL_INT_LIT16:
3309 case Instruction::DIV_INT_LIT16:
3310 case Instruction::REM_INT_LIT16:
Ian Rogers7b078e82014-09-10 14:44:24 -07003311 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), false,
3312 true);
jeffhaobdb76512011-09-07 11:43:16 -07003313 break;
3314 case Instruction::AND_INT_LIT16:
3315 case Instruction::OR_INT_LIT16:
3316 case Instruction::XOR_INT_LIT16:
Ian Rogers7b078e82014-09-10 14:44:24 -07003317 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), true,
3318 true);
jeffhaobdb76512011-09-07 11:43:16 -07003319 break;
3320 case Instruction::ADD_INT_LIT8:
3321 case Instruction::RSUB_INT_LIT8:
3322 case Instruction::MUL_INT_LIT8:
3323 case Instruction::DIV_INT_LIT8:
3324 case Instruction::REM_INT_LIT8:
3325 case Instruction::SHL_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07003326 case Instruction::SHR_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07003327 case Instruction::USHR_INT_LIT8:
Ian Rogers7b078e82014-09-10 14:44:24 -07003328 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), false,
3329 false);
jeffhaobdb76512011-09-07 11:43:16 -07003330 break;
3331 case Instruction::AND_INT_LIT8:
3332 case Instruction::OR_INT_LIT8:
3333 case Instruction::XOR_INT_LIT8:
Ian Rogers7b078e82014-09-10 14:44:24 -07003334 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), true,
3335 false);
jeffhaobdb76512011-09-07 11:43:16 -07003336 break;
3337
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003338 // Special instructions.
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07003339 case Instruction::RETURN_VOID_NO_BARRIER:
3340 if (IsConstructor() && !IsStatic()) {
Vladimir Markod93e3742018-07-18 10:58:13 +01003341 const RegType& declaring_class = GetDeclaringClass();
Andreas Gampe68df3202015-06-22 11:35:46 -07003342 if (declaring_class.IsUnresolvedReference()) {
3343 // We must iterate over the fields, even if we cannot use mirror classes to do so. Do it
3344 // manually over the underlying dex file.
3345 uint32_t first_index = GetFirstFinalInstanceFieldIndex(*dex_file_,
3346 dex_file_->GetMethodId(dex_method_idx_).class_idx_);
Andreas Gampee2abbc62017-09-15 11:59:26 -07003347 if (first_index != dex::kDexNoIndex) {
Andreas Gampe68df3202015-06-22 11:35:46 -07003348 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void-no-barrier not expected for field "
3349 << first_index;
3350 }
3351 break;
3352 }
Vladimir Marko2cffc5d2018-05-29 15:40:56 +01003353 ObjPtr<mirror::Class> klass = declaring_class.GetClass();
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07003354 for (uint32_t i = 0, num_fields = klass->NumInstanceFields(); i < num_fields; ++i) {
3355 if (klass->GetInstanceField(i)->IsFinal()) {
Mathieu Chartiere86deef2015-03-19 13:43:37 -07003356 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void-no-barrier not expected for "
David Sehr709b0702016-10-13 09:12:37 -07003357 << klass->GetInstanceField(i)->PrettyField();
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07003358 break;
3359 }
3360 }
Sebastien Hertzcc10e0e2013-06-28 14:24:48 +02003361 }
Andreas Gampeb2917962015-07-31 13:36:10 -07003362 // Handle this like a RETURN_VOID now. Code is duplicated to separate standard from
3363 // quickened opcodes (otherwise this could be a fall-through).
3364 if (!IsConstructor()) {
3365 if (!GetMethodReturnType().IsConflict()) {
3366 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void not expected";
3367 }
3368 }
Sebastien Hertzcc10e0e2013-06-28 14:24:48 +02003369 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003370
Ian Rogersd81871c2011-10-03 13:57:23 -07003371 /* These should never appear during verification. */
Mathieu Chartierffc605c2014-12-10 10:35:44 -08003372 case Instruction::UNUSED_3E ... Instruction::UNUSED_43:
Narayan Kamath8ec3bd22016-08-03 12:46:23 +01003373 case Instruction::UNUSED_F3 ... Instruction::UNUSED_F9:
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003374 case Instruction::UNUSED_79:
3375 case Instruction::UNUSED_7A:
jeffhaod5347e02012-03-22 17:25:05 -07003376 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Unexpected opcode " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07003377 break;
3378
3379 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003380 * DO NOT add a "default" clause here. Without it the compiler will
jeffhaobdb76512011-09-07 11:43:16 -07003381 * complain if an instruction is missing (which is desirable).
3382 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003383 } // end - switch (dec_insn.opcode)
jeffhaobdb76512011-09-07 11:43:16 -07003384
Ian Rogersad0b3a32012-04-16 14:50:24 -07003385 if (have_pending_hard_failure_) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08003386 if (Runtime::Current()->IsAotCompiler()) {
3387 /* When AOT compiling, check that the last failure is a hard failure */
Andreas Gampeb588f4c2015-05-26 13:35:39 -07003388 if (failures_[failures_.size() - 1] != VERIFY_ERROR_BAD_CLASS_HARD) {
3389 LOG(ERROR) << "Pending failures:";
3390 for (auto& error : failures_) {
3391 LOG(ERROR) << error;
3392 }
3393 for (auto& error_msg : failure_messages_) {
3394 LOG(ERROR) << error_msg->str();
3395 }
3396 LOG(FATAL) << "Pending hard failure, but last failure not hard.";
3397 }
Ian Rogerse1758fe2012-04-19 11:31:15 -07003398 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003399 /* immediate failure, reject class */
3400 info_messages_ << "Rejecting opcode " << inst->DumpString(dex_file_);
3401 return false;
jeffhaofaf459e2012-08-31 15:32:47 -07003402 } else if (have_pending_runtime_throw_failure_) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07003403 /* checking interpreter will throw, mark following code as unreachable */
jeffhaofaf459e2012-08-31 15:32:47 -07003404 opcode_flags = Instruction::kThrow;
Andreas Gampea727e372015-08-25 09:22:37 -07003405 // Note: the flag must be reset as it is only global to decouple Fail and is semantically per
3406 // instruction. However, RETURN checking may throw LOCKING errors, so we clear at the
3407 // very end.
jeffhaobdb76512011-09-07 11:43:16 -07003408 }
jeffhaobdb76512011-09-07 11:43:16 -07003409 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003410 * If we didn't just set the result register, clear it out. This ensures that you can only use
3411 * "move-result" immediately after the result is set. (We could check this statically, but it's
3412 * not expensive and it makes our debugging output cleaner.)
jeffhaobdb76512011-09-07 11:43:16 -07003413 */
3414 if (!just_set_result) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003415 work_line_->SetResultTypeToUnknown(this);
jeffhaobdb76512011-09-07 11:43:16 -07003416 }
3417
jeffhaobdb76512011-09-07 11:43:16 -07003418 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003419 * Handle "branch". Tag the branch target.
jeffhaobdb76512011-09-07 11:43:16 -07003420 *
3421 * NOTE: instructions like Instruction::EQZ provide information about the
jeffhaod1f0fde2011-09-08 17:25:33 -07003422 * state of the register when the branch is taken or not taken. For example,
jeffhaobdb76512011-09-07 11:43:16 -07003423 * somebody could get a reference field, check it for zero, and if the
3424 * branch is taken immediately store that register in a boolean field
jeffhaod1f0fde2011-09-08 17:25:33 -07003425 * since the value is known to be zero. We do not currently account for
jeffhaobdb76512011-09-07 11:43:16 -07003426 * that, and will reject the code.
3427 *
3428 * TODO: avoid re-fetching the branch target
3429 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003430 if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07003431 bool isConditional, selfOkay;
Ian Rogersd81871c2011-10-03 13:57:23 -07003432 if (!GetBranchOffset(work_insn_idx_, &branch_target, &isConditional, &selfOkay)) {
jeffhaobdb76512011-09-07 11:43:16 -07003433 /* should never happen after static verification */
jeffhaod5347e02012-03-22 17:25:05 -07003434 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad branch";
jeffhaobdb76512011-09-07 11:43:16 -07003435 return false;
3436 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08003437 DCHECK_EQ(isConditional, (opcode_flags & Instruction::kContinue) != 0);
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08003438 if (!CheckNotMoveExceptionOrMoveResult(code_item_accessor_.Insns(),
3439 work_insn_idx_ + branch_target)) {
jeffhaobdb76512011-09-07 11:43:16 -07003440 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07003441 }
jeffhaobdb76512011-09-07 11:43:16 -07003442 /* update branch target, set "changed" if appropriate */
Mathieu Chartierde40d472015-10-15 17:47:48 -07003443 if (nullptr != branch_line) {
Ian Rogersebbdd872014-07-07 23:53:08 -07003444 if (!UpdateRegisters(work_insn_idx_ + branch_target, branch_line.get(), false)) {
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003445 return false;
3446 }
3447 } else {
Ian Rogersebbdd872014-07-07 23:53:08 -07003448 if (!UpdateRegisters(work_insn_idx_ + branch_target, work_line_.get(), false)) {
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003449 return false;
3450 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003451 }
jeffhaobdb76512011-09-07 11:43:16 -07003452 }
3453
3454 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003455 * Handle "switch". Tag all possible branch targets.
jeffhaobdb76512011-09-07 11:43:16 -07003456 *
3457 * We've already verified that the table is structurally sound, so we
3458 * just need to walk through and tag the targets.
3459 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003460 if ((opcode_flags & Instruction::kSwitch) != 0) {
Andreas Gampe53de99c2015-08-17 13:43:55 -07003461 int offset_to_switch = insns[1] | (static_cast<int32_t>(insns[2]) << 16);
jeffhaobdb76512011-09-07 11:43:16 -07003462 const uint16_t* switch_insns = insns + offset_to_switch;
3463 int switch_count = switch_insns[1];
3464 int offset_to_targets, targ;
3465
3466 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
3467 /* 0 = sig, 1 = count, 2/3 = first key */
3468 offset_to_targets = 4;
3469 } else {
3470 /* 0 = sig, 1 = count, 2..count * 2 = keys */
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07003471 DCHECK((*insns & 0xff) == Instruction::SPARSE_SWITCH);
jeffhaobdb76512011-09-07 11:43:16 -07003472 offset_to_targets = 2 + 2 * switch_count;
3473 }
3474
3475 /* verify each switch target */
3476 for (targ = 0; targ < switch_count; targ++) {
3477 int offset;
3478 uint32_t abs_offset;
3479
3480 /* offsets are 32-bit, and only partly endian-swapped */
3481 offset = switch_insns[offset_to_targets + targ * 2] |
Andreas Gampe53de99c2015-08-17 13:43:55 -07003482 (static_cast<int32_t>(switch_insns[offset_to_targets + targ * 2 + 1]) << 16);
Ian Rogersd81871c2011-10-03 13:57:23 -07003483 abs_offset = work_insn_idx_ + offset;
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08003484 DCHECK_LT(abs_offset, code_item_accessor_.InsnsSizeInCodeUnits());
3485 if (!CheckNotMoveExceptionOrMoveResult(code_item_accessor_.Insns(), abs_offset)) {
jeffhaobdb76512011-09-07 11:43:16 -07003486 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07003487 }
Ian Rogersebbdd872014-07-07 23:53:08 -07003488 if (!UpdateRegisters(abs_offset, work_line_.get(), false)) {
jeffhaobdb76512011-09-07 11:43:16 -07003489 return false;
Ian Rogersebbdd872014-07-07 23:53:08 -07003490 }
jeffhaobdb76512011-09-07 11:43:16 -07003491 }
3492 }
3493
3494 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003495 * Handle instructions that can throw and that are sitting in a "try" block. (If they're not in a
3496 * "try" block when they throw, control transfers out of the method.)
jeffhaobdb76512011-09-07 11:43:16 -07003497 */
Mathieu Chartierde40d472015-10-15 17:47:48 -07003498 if ((opcode_flags & Instruction::kThrow) != 0 && GetInstructionFlags(work_insn_idx_).IsInTry()) {
Andreas Gampef91baf12014-07-18 15:41:00 -07003499 bool has_catch_all_handler = false;
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08003500 const dex::TryItem* try_item = code_item_accessor_.FindTryItem(work_insn_idx_);
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08003501 CHECK(try_item != nullptr);
Mathieu Chartierdc578c72017-12-27 11:51:45 -08003502 CatchHandlerIterator iterator(code_item_accessor_, *try_item);
jeffhaobdb76512011-09-07 11:43:16 -07003503
Andreas Gampef91baf12014-07-18 15:41:00 -07003504 // Need the linker to try and resolve the handled class to check if it's Throwable.
3505 ClassLinker* linker = Runtime::Current()->GetClassLinker();
3506
Ian Rogers0571d352011-11-03 19:51:38 -07003507 for (; iterator.HasNext(); iterator.Next()) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08003508 dex::TypeIndex handler_type_idx = iterator.GetHandlerTypeIndex();
3509 if (!handler_type_idx.IsValid()) {
Andreas Gampef91baf12014-07-18 15:41:00 -07003510 has_catch_all_handler = true;
3511 } else {
3512 // It is also a catch-all if it is java.lang.Throwable.
Vladimir Marko28e012a2017-12-07 11:22:59 +00003513 ObjPtr<mirror::Class> klass =
Vladimir Marko666ee3d2017-12-11 18:37:36 +00003514 linker->ResolveType(handler_type_idx, dex_cache_, class_loader_);
Andreas Gampef91baf12014-07-18 15:41:00 -07003515 if (klass != nullptr) {
Vladimir Markoadbceb72018-05-29 14:34:14 +01003516 if (klass == GetClassRoot<mirror::Throwable>()) {
Andreas Gampef91baf12014-07-18 15:41:00 -07003517 has_catch_all_handler = true;
3518 }
3519 } else {
3520 // Clear exception.
Ian Rogers7b078e82014-09-10 14:44:24 -07003521 DCHECK(self_->IsExceptionPending());
3522 self_->ClearException();
Andreas Gampef91baf12014-07-18 15:41:00 -07003523 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003524 }
jeffhaobdb76512011-09-07 11:43:16 -07003525 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003526 * Merge registers into the "catch" block. We want to use the "savedRegs" rather than
3527 * "work_regs", because at runtime the exception will be thrown before the instruction
3528 * modifies any registers.
jeffhaobdb76512011-09-07 11:43:16 -07003529 */
Ian Rogersebbdd872014-07-07 23:53:08 -07003530 if (!UpdateRegisters(iterator.GetHandlerAddress(), saved_line_.get(), false)) {
jeffhaobdb76512011-09-07 11:43:16 -07003531 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07003532 }
jeffhaobdb76512011-09-07 11:43:16 -07003533 }
3534
3535 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003536 * If the monitor stack depth is nonzero, there must be a "catch all" handler for this
3537 * instruction. This does apply to monitor-exit because of async exception handling.
jeffhaobdb76512011-09-07 11:43:16 -07003538 */
Andreas Gampef91baf12014-07-18 15:41:00 -07003539 if (work_line_->MonitorStackDepth() > 0 && !has_catch_all_handler) {
jeffhaobdb76512011-09-07 11:43:16 -07003540 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003541 * The state in work_line reflects the post-execution state. If the current instruction is a
3542 * monitor-enter and the monitor stack was empty, we don't need a catch-all (if it throws,
jeffhaobdb76512011-09-07 11:43:16 -07003543 * it will do so before grabbing the lock).
3544 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02003545 if (inst->Opcode() != Instruction::MONITOR_ENTER || work_line_->MonitorStackDepth() != 1) {
jeffhaod5347e02012-03-22 17:25:05 -07003546 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
Ian Rogersd81871c2011-10-03 13:57:23 -07003547 << "expected to be within a catch-all for an instruction where a monitor is held";
jeffhaobdb76512011-09-07 11:43:16 -07003548 return false;
3549 }
3550 }
3551 }
3552
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003553 /* Handle "continue". Tag the next consecutive instruction.
3554 * Note: Keep the code handling "continue" case below the "branch" and "switch" cases,
3555 * because it changes work_line_ when performing peephole optimization
3556 * and this change should not be used in those cases.
3557 */
Ian Rogers6d376ae2013-07-23 15:12:40 -07003558 if ((opcode_flags & Instruction::kContinue) != 0) {
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08003559 DCHECK_EQ(&code_item_accessor_.InstructionAt(work_insn_idx_), inst);
Ian Rogers7b078e82014-09-10 14:44:24 -07003560 uint32_t next_insn_idx = work_insn_idx_ + inst->SizeInCodeUnits();
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08003561 if (next_insn_idx >= code_item_accessor_.InsnsSizeInCodeUnits()) {
Ian Rogers6d376ae2013-07-23 15:12:40 -07003562 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Execution can walk off end of code area";
3563 return false;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003564 }
Ian Rogers6d376ae2013-07-23 15:12:40 -07003565 // The only way to get to a move-exception instruction is to get thrown there. Make sure the
3566 // next instruction isn't one.
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08003567 if (!CheckNotMoveException(code_item_accessor_.Insns(), next_insn_idx)) {
Ian Rogers6d376ae2013-07-23 15:12:40 -07003568 return false;
3569 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07003570 if (nullptr != fallthrough_line) {
Ian Rogers6d376ae2013-07-23 15:12:40 -07003571 // Make workline consistent with fallthrough computed from peephole optimization.
3572 work_line_->CopyFromLine(fallthrough_line.get());
3573 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07003574 if (GetInstructionFlags(next_insn_idx).IsReturn()) {
Ian Rogersb8c78592013-07-25 23:52:52 +00003575 // For returns we only care about the operand to the return, all other registers are dead.
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08003576 const Instruction* ret_inst = &code_item_accessor_.InstructionAt(next_insn_idx);
Andreas Gampea727e372015-08-25 09:22:37 -07003577 AdjustReturnLine(this, ret_inst, work_line_.get());
Ian Rogersb8c78592013-07-25 23:52:52 +00003578 }
Ian Rogers6d376ae2013-07-23 15:12:40 -07003579 RegisterLine* next_line = reg_table_.GetLine(next_insn_idx);
Ian Rogers7b078e82014-09-10 14:44:24 -07003580 if (next_line != nullptr) {
Ian Rogersebbdd872014-07-07 23:53:08 -07003581 // Merge registers into what we have for the next instruction, and set the "changed" flag if
3582 // needed. If the merge changes the state of the registers then the work line will be
3583 // updated.
3584 if (!UpdateRegisters(next_insn_idx, work_line_.get(), true)) {
Ian Rogers6d376ae2013-07-23 15:12:40 -07003585 return false;
3586 }
3587 } else {
3588 /*
3589 * We're not recording register data for the next instruction, so we don't know what the
3590 * prior state was. We have to assume that something has changed and re-evaluate it.
3591 */
Mathieu Chartierde40d472015-10-15 17:47:48 -07003592 GetInstructionFlags(next_insn_idx).SetChanged();
Ian Rogers6d376ae2013-07-23 15:12:40 -07003593 }
3594 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003595
jeffhaod1f0fde2011-09-08 17:25:33 -07003596 /* If we're returning from the method, make sure monitor stack is empty. */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003597 if ((opcode_flags & Instruction::kReturn) != 0) {
Andreas Gampea727e372015-08-25 09:22:37 -07003598 work_line_->VerifyMonitorStackEmpty(this);
jeffhaobdb76512011-09-07 11:43:16 -07003599 }
3600
3601 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003602 * Update start_guess. Advance to the next instruction of that's
3603 * possible, otherwise use the branch target if one was found. If
jeffhaobdb76512011-09-07 11:43:16 -07003604 * neither of those exists we're in a return or throw; leave start_guess
3605 * alone and let the caller sort it out.
3606 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003607 if ((opcode_flags & Instruction::kContinue) != 0) {
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08003608 DCHECK_EQ(&code_item_accessor_.InstructionAt(work_insn_idx_), inst);
Ian Rogers7b078e82014-09-10 14:44:24 -07003609 *start_guess = work_insn_idx_ + inst->SizeInCodeUnits();
Elliott Hughesadb8c672012-03-06 16:49:32 -08003610 } else if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07003611 /* we're still okay if branch_target is zero */
Ian Rogersd81871c2011-10-03 13:57:23 -07003612 *start_guess = work_insn_idx_ + branch_target;
jeffhaobdb76512011-09-07 11:43:16 -07003613 }
3614
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08003615 DCHECK_LT(*start_guess, code_item_accessor_.InsnsSizeInCodeUnits());
Mathieu Chartierde40d472015-10-15 17:47:48 -07003616 DCHECK(GetInstructionFlags(*start_guess).IsOpcode());
jeffhaobdb76512011-09-07 11:43:16 -07003617
Andreas Gampea727e372015-08-25 09:22:37 -07003618 if (have_pending_runtime_throw_failure_) {
3619 have_any_pending_runtime_throw_failure_ = true;
3620 // Reset the pending_runtime_throw flag now.
3621 have_pending_runtime_throw_failure_ = false;
3622 }
3623
jeffhaobdb76512011-09-07 11:43:16 -07003624 return true;
Brian Carlstrom7934ac22013-07-26 10:54:15 -07003625} // NOLINT(readability/fn_size)
jeffhaobdb76512011-09-07 11:43:16 -07003626
Mathieu Chartierde40d472015-10-15 17:47:48 -07003627void MethodVerifier::UninstantiableError(const char* descriptor) {
3628 Fail(VerifyError::VERIFY_ERROR_NO_CLASS) << "Could not create precise reference for "
3629 << "non-instantiable klass " << descriptor;
3630}
3631
Vladimir Marko28e012a2017-12-07 11:22:59 +00003632inline bool MethodVerifier::IsInstantiableOrPrimitive(ObjPtr<mirror::Class> klass) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07003633 return klass->IsInstantiable() || klass->IsPrimitive();
3634}
3635
Andreas Gampe98be1a92017-08-28 08:25:45 -07003636template <MethodVerifier::CheckAccess C>
3637const RegType& MethodVerifier::ResolveClass(dex::TypeIndex class_idx) {
Vladimir Marko666ee3d2017-12-11 18:37:36 +00003638 ClassLinker* linker = Runtime::Current()->GetClassLinker();
Vladimir Marko28e012a2017-12-07 11:22:59 +00003639 ObjPtr<mirror::Class> klass = can_load_classes_
Vladimir Marko666ee3d2017-12-11 18:37:36 +00003640 ? linker->ResolveType(class_idx, dex_cache_, class_loader_)
3641 : linker->LookupResolvedType(class_idx, dex_cache_.Get(), class_loader_.Get());
Vladimir Marko8d6768d2017-03-14 10:13:21 +00003642 if (can_load_classes_ && klass == nullptr) {
3643 DCHECK(self_->IsExceptionPending());
3644 self_->ClearException();
3645 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07003646 const RegType* result = nullptr;
Vladimir Marko9cb0c462017-04-21 13:31:41 +01003647 if (klass != nullptr) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07003648 bool precise = klass->CannotBeAssignedFromOtherTypes();
3649 if (precise && !IsInstantiableOrPrimitive(klass)) {
3650 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
3651 UninstantiableError(descriptor);
3652 precise = false;
3653 }
Vladimir Marko2cffc5d2018-05-29 15:40:56 +01003654 result = reg_types_.FindClass(klass, precise);
Mathieu Chartierde40d472015-10-15 17:47:48 -07003655 if (result == nullptr) {
3656 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
Vladimir Marko2cffc5d2018-05-29 15:40:56 +01003657 result = reg_types_.InsertClass(descriptor, klass, precise);
Mathieu Chartierde40d472015-10-15 17:47:48 -07003658 }
3659 } else {
3660 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
3661 result = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003662 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07003663 DCHECK(result != nullptr);
3664 if (result->IsConflict()) {
3665 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
3666 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "accessing broken descriptor '" << descriptor
3667 << "' in " << GetDeclaringClass();
3668 return *result;
3669 }
David Brazdilca3c8c32016-09-06 14:04:48 +01003670
3671 // Record result of class resolution attempt.
Vladimir Markobcf17522018-06-01 13:14:32 +01003672 VerifierDeps::MaybeRecordClassResolution(*dex_file_, class_idx, klass);
David Brazdilca3c8c32016-09-06 14:04:48 +01003673
Andreas Gampe629be512017-08-25 17:09:32 -07003674 // If requested, check if access is allowed. Unresolved types are included in this check, as the
3675 // interpreter only tests whether access is allowed when a class is not pre-verified and runs in
3676 // the access-checks interpreter. If result is primitive, skip the access check.
3677 //
3678 // Note: we do this for unresolved classes to trigger re-verification at runtime.
Andreas Gampedc39d322018-09-04 09:26:03 -07003679 if (C == CheckAccess::kYes &&
3680 result->IsNonZeroReferenceTypes() &&
David Brazdil2bb2fbd2018-11-13 18:24:26 +00003681 (IsSdkVersionSetAndAtLeast(api_level_, SdkVersion::kP) || !result->IsUnresolvedTypes())) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07003682 const RegType& referrer = GetDeclaringClass();
David Brazdil2bb2fbd2018-11-13 18:24:26 +00003683 if ((IsSdkVersionSetAndAtLeast(api_level_, SdkVersion::kP) || !referrer.IsUnresolvedTypes()) &&
3684 !referrer.CanAccess(*result)) {
Andreas Gampe629be512017-08-25 17:09:32 -07003685 Fail(VERIFY_ERROR_ACCESS_CLASS) << "(possibly) illegal class access: '"
Nicolas Geoffray4e868fa2017-04-21 17:16:44 +01003686 << referrer << "' -> '" << *result << "'";
Mathieu Chartierde40d472015-10-15 17:47:48 -07003687 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07003688 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07003689 return *result;
Ian Rogersd81871c2011-10-03 13:57:23 -07003690}
3691
Andreas Gampedd309072017-08-31 07:47:37 -07003692// Instantiate ResolveClass variants. This is required as the -inl file has a function with a call
3693// to ResolveClass, and compilers may decide to inline, requiring a symbol.
Andreas Gampeae0b1f42017-08-30 17:36:26 -07003694template const RegType& MethodVerifier::ResolveClass<MethodVerifier::CheckAccess::kNo>(
3695 dex::TypeIndex class_idx);
3696template const RegType& MethodVerifier::ResolveClass<MethodVerifier::CheckAccess::kYes>(
3697 dex::TypeIndex class_idx);
3698
Ian Rogersd8f69b02014-09-10 21:43:52 +00003699const RegType& MethodVerifier::GetCaughtExceptionType() {
Ian Rogers7b078e82014-09-10 14:44:24 -07003700 const RegType* common_super = nullptr;
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08003701 if (code_item_accessor_.TriesSize() != 0) {
3702 const uint8_t* handlers_ptr = code_item_accessor_.GetCatchHandlerData();
Ian Rogersd81871c2011-10-03 13:57:23 -07003703 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
3704 for (uint32_t i = 0; i < handlers_size; i++) {
Ian Rogers0571d352011-11-03 19:51:38 -07003705 CatchHandlerIterator iterator(handlers_ptr);
3706 for (; iterator.HasNext(); iterator.Next()) {
3707 if (iterator.GetHandlerAddress() == (uint32_t) work_insn_idx_) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08003708 if (!iterator.GetHandlerTypeIndex().IsValid()) {
Ian Rogersb4903572012-10-11 11:52:56 -07003709 common_super = &reg_types_.JavaLangThrowable(false);
Ian Rogersd81871c2011-10-03 13:57:23 -07003710 } else {
Andreas Gampe98be1a92017-08-28 08:25:45 -07003711 const RegType& exception =
3712 ResolveClass<CheckAccess::kYes>(iterator.GetHandlerTypeIndex());
David Brazdilca3c8c32016-09-06 14:04:48 +01003713 if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(exception, this)) {
David Brazdil68b5c0b2016-01-19 14:25:29 +00003714 DCHECK(!exception.IsUninitializedTypes()); // Comes from dex, shouldn't be uninit.
Jeff Haoc26a56c2013-11-04 12:00:47 -08003715 if (exception.IsUnresolvedTypes()) {
3716 // We don't know enough about the type. Fail here and let runtime handle it.
3717 Fail(VERIFY_ERROR_NO_CLASS) << "unresolved exception class " << exception;
3718 return exception;
3719 } else {
3720 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unexpected non-exception class " << exception;
3721 return reg_types_.Conflict();
3722 }
Jeff Haob878f212014-04-24 16:25:36 -07003723 } else if (common_super == nullptr) {
3724 common_super = &exception;
Ian Rogers28ad40d2011-10-27 15:19:26 -07003725 } else if (common_super->Equals(exception)) {
Ian Rogersc4762272012-02-01 15:55:55 -08003726 // odd case, but nothing to do
Ian Rogersd81871c2011-10-03 13:57:23 -07003727 } else {
David Brazdilca3c8c32016-09-06 14:04:48 +01003728 common_super = &common_super->Merge(exception, &reg_types_, this);
Mathieu Chartierc2a2bda2018-03-12 11:21:52 -07003729 if (FailOrAbort(reg_types_.JavaLangThrowable(false).IsAssignableFrom(
David Brazdilca3c8c32016-09-06 14:04:48 +01003730 *common_super, this),
Andreas Gampe7c038102014-10-27 20:08:46 -07003731 "java.lang.Throwable is not assignable-from common_super at ",
3732 work_insn_idx_)) {
3733 break;
3734 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003735 }
3736 }
3737 }
3738 }
Ian Rogers0571d352011-11-03 19:51:38 -07003739 handlers_ptr = iterator.EndDataPointer();
Ian Rogersd81871c2011-10-03 13:57:23 -07003740 }
3741 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003742 if (common_super == nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003743 /* no catch blocks, or no catches with classes we can find */
jeffhaod5347e02012-03-22 17:25:05 -07003744 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unable to find exception handler";
Ian Rogersad0b3a32012-04-16 14:50:24 -07003745 return reg_types_.Conflict();
Ian Rogersd81871c2011-10-03 13:57:23 -07003746 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07003747 return *common_super;
Ian Rogersd81871c2011-10-03 13:57:23 -07003748}
3749
Mathieu Chartiere401d142015-04-22 13:56:20 -07003750ArtMethod* MethodVerifier::ResolveMethodAndCheckAccess(
Alex Light7268d472016-01-20 15:50:01 -08003751 uint32_t dex_method_idx, MethodType method_type) {
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08003752 const dex::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx);
Andreas Gampe98be1a92017-08-28 08:25:45 -07003753 const RegType& klass_type = ResolveClass<CheckAccess::kYes>(method_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003754 if (klass_type.IsConflict()) {
3755 std::string append(" in attempt to access method ");
3756 append += dex_file_->GetMethodName(method_id);
3757 AppendToLastFailMessage(append);
Ian Rogers7b078e82014-09-10 14:44:24 -07003758 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08003759 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003760 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003761 return nullptr; // Can't resolve Class so no more to do here
Ian Rogers90040192011-12-16 08:54:29 -08003762 }
Vladimir Markoba118822017-06-12 15:41:56 +01003763 ObjPtr<mirror::Class> klass = klass_type.GetClass();
Ian Rogersd8f69b02014-09-10 21:43:52 +00003764 const RegType& referrer = GetDeclaringClass();
Nicolas Geoffrayea179f42018-02-08 22:30:18 +00003765 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
3766 PointerSize pointer_size = class_linker->GetImagePointerSize();
Andreas Gampe42ef8ab2015-12-03 17:27:32 -08003767
Mathieu Chartiere401d142015-04-22 13:56:20 -07003768 ArtMethod* res_method = dex_cache_->GetResolvedMethod(dex_method_idx, pointer_size);
Ian Rogers7b078e82014-09-10 14:44:24 -07003769 if (res_method == nullptr) {
Nicolas Geoffrayea179f42018-02-08 22:30:18 +00003770 res_method = class_linker->FindResolvedMethod(
3771 klass, dex_cache_.Get(), class_loader_.Get(), dex_method_idx);
Ian Rogersd81871c2011-10-03 13:57:23 -07003772 }
David Brazdilca3c8c32016-09-06 14:04:48 +01003773
Vladimir Markoba118822017-06-12 15:41:56 +01003774 // Record result of method resolution attempt. The klass resolution has recorded whether
3775 // the class is an interface or not and therefore the type of the lookup performed above.
3776 // TODO: Maybe we should not record dependency if the invoke type does not match the lookup type.
3777 VerifierDeps::MaybeRecordMethodResolution(*dex_file_, dex_method_idx, res_method);
3778
Alex Lightafb66472017-08-01 09:54:49 -07003779 bool must_fail = false;
3780 // This is traditional and helps with screwy bytecode. It will tell you that, yes, a method
3781 // exists, but that it's called incorrectly. This significantly helps debugging, as locally it's
3782 // hard to see the differences.
3783 // If we don't have res_method here we must fail. Just use this bool to make sure of that with a
3784 // DCHECK.
Vladimir Markoba118822017-06-12 15:41:56 +01003785 if (res_method == nullptr) {
Alex Lightafb66472017-08-01 09:54:49 -07003786 must_fail = true;
Vladimir Markoba118822017-06-12 15:41:56 +01003787 // Try to find the method also with the other type for better error reporting below
3788 // but do not store such bogus lookup result in the DexCache or VerifierDeps.
David Brazdil4525e0b2018-04-05 16:57:32 +01003789 res_method = class_linker->FindIncompatibleMethod(
3790 klass, dex_cache_.Get(), class_loader_.Get(), dex_method_idx);
Vladimir Markoba118822017-06-12 15:41:56 +01003791 }
David Brazdilca3c8c32016-09-06 14:04:48 +01003792
3793 if (res_method == nullptr) {
3794 Fail(VERIFY_ERROR_NO_METHOD) << "couldn't find method "
David Sehr709b0702016-10-13 09:12:37 -07003795 << klass->PrettyDescriptor() << "."
David Brazdilca3c8c32016-09-06 14:04:48 +01003796 << dex_file_->GetMethodName(method_id) << " "
3797 << dex_file_->GetMethodSignature(method_id);
3798 return nullptr;
3799 }
3800
Ian Rogersd81871c2011-10-03 13:57:23 -07003801 // Make sure calls to constructors are "direct". There are additional restrictions but we don't
3802 // enforce them here.
3803 if (res_method->IsConstructor() && method_type != METHOD_DIRECT) {
jeffhaod5347e02012-03-22 17:25:05 -07003804 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting non-direct call to constructor "
David Sehr709b0702016-10-13 09:12:37 -07003805 << res_method->PrettyMethod();
Ian Rogers7b078e82014-09-10 14:44:24 -07003806 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003807 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003808 // Disallow any calls to class initializers.
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003809 if (res_method->IsClassInitializer()) {
jeffhaod5347e02012-03-22 17:25:05 -07003810 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting call to class initializer "
David Sehr709b0702016-10-13 09:12:37 -07003811 << res_method->PrettyMethod();
Ian Rogers7b078e82014-09-10 14:44:24 -07003812 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003813 }
Andreas Gampe42ef8ab2015-12-03 17:27:32 -08003814
3815 // Check that interface methods are static or match interface classes.
3816 // We only allow statics if we don't have default methods enabled.
3817 //
3818 // Note: this check must be after the initializer check, as those are required to fail a class,
3819 // while this check implies an IncompatibleClassChangeError.
3820 if (klass->IsInterface()) {
Alex Lightb55f1ac2016-04-12 15:50:55 -07003821 // methods called on interfaces should be invoke-interface, invoke-super, invoke-direct (if
Mathieu Chartierf6e31472017-12-28 13:32:08 -08003822 // default methods are supported for the dex file), or invoke-static.
Andreas Gampe42ef8ab2015-12-03 17:27:32 -08003823 if (method_type != METHOD_INTERFACE &&
Neil Fuller9724c632016-01-07 15:42:47 +00003824 method_type != METHOD_STATIC &&
Mathieu Chartierf6e31472017-12-28 13:32:08 -08003825 (!dex_file_->SupportsDefaultMethods() ||
Alex Lightb55f1ac2016-04-12 15:50:55 -07003826 method_type != METHOD_DIRECT) &&
Neil Fuller9724c632016-01-07 15:42:47 +00003827 method_type != METHOD_SUPER) {
Andreas Gampe42ef8ab2015-12-03 17:27:32 -08003828 Fail(VERIFY_ERROR_CLASS_CHANGE)
David Sehr709b0702016-10-13 09:12:37 -07003829 << "non-interface method " << dex_file_->PrettyMethod(dex_method_idx)
3830 << " is in an interface class " << klass->PrettyClass();
Andreas Gampe42ef8ab2015-12-03 17:27:32 -08003831 return nullptr;
3832 }
3833 } else {
3834 if (method_type == METHOD_INTERFACE) {
3835 Fail(VERIFY_ERROR_CLASS_CHANGE)
David Sehr709b0702016-10-13 09:12:37 -07003836 << "interface method " << dex_file_->PrettyMethod(dex_method_idx)
3837 << " is in a non-interface class " << klass->PrettyClass();
Andreas Gampe42ef8ab2015-12-03 17:27:32 -08003838 return nullptr;
3839 }
3840 }
3841
Alex Lightafb66472017-08-01 09:54:49 -07003842 // Check specifically for non-public object methods being provided for interface dispatch. This
3843 // can occur if we failed to find a method with FindInterfaceMethod but later find one with
3844 // FindClassMethod for error message use.
3845 if (method_type == METHOD_INTERFACE &&
3846 res_method->GetDeclaringClass()->IsObjectClass() &&
3847 !res_method->IsPublic()) {
3848 Fail(VERIFY_ERROR_NO_METHOD) << "invoke-interface " << klass->PrettyDescriptor() << "."
3849 << dex_file_->GetMethodName(method_id) << " "
3850 << dex_file_->GetMethodSignature(method_id) << " resolved to "
3851 << "non-public object method " << res_method->PrettyMethod() << " "
3852 << "but non-public Object methods are excluded from interface "
3853 << "method resolution.";
3854 return nullptr;
3855 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003856 // Check if access is allowed.
Ian Rogersad0b3a32012-04-16 14:50:24 -07003857 if (!referrer.CanAccessMember(res_method->GetDeclaringClass(), res_method->GetAccessFlags())) {
David Sehr709b0702016-10-13 09:12:37 -07003858 Fail(VERIFY_ERROR_ACCESS_METHOD) << "illegal method access (call "
3859 << res_method->PrettyMethod()
Ian Rogersad0b3a32012-04-16 14:50:24 -07003860 << " from " << referrer << ")";
jeffhaob57e9522012-04-26 18:08:21 -07003861 return res_method;
jeffhao8cd6dda2012-02-22 10:15:34 -08003862 }
jeffhaode0d9c92012-02-27 13:58:13 -08003863 // Check that invoke-virtual and invoke-super are not used on private methods of the same class.
Alex Light7268d472016-01-20 15:50:01 -08003864 if (res_method->IsPrivate() && (method_type == METHOD_VIRTUAL || method_type == METHOD_SUPER)) {
jeffhaod5347e02012-03-22 17:25:05 -07003865 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invoke-super/virtual can't be used on private method "
David Sehr709b0702016-10-13 09:12:37 -07003866 << res_method->PrettyMethod();
Ian Rogers7b078e82014-09-10 14:44:24 -07003867 return nullptr;
jeffhaode0d9c92012-02-27 13:58:13 -08003868 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003869 // See if the method type implied by the invoke instruction matches the access flags for the
Orion Hodsoncfa325e2016-10-13 10:25:54 +01003870 // target method. The flags for METHOD_POLYMORPHIC are based on there being precisely two
3871 // signature polymorphic methods supported by the run-time which are native methods with variable
3872 // arguments.
Brian Carlstrombe6fa5e2014-12-09 20:15:42 -08003873 if ((method_type == METHOD_DIRECT && (!res_method->IsDirect() || res_method->IsStatic())) ||
Ian Rogersd81871c2011-10-03 13:57:23 -07003874 (method_type == METHOD_STATIC && !res_method->IsStatic()) ||
Alex Light7268d472016-01-20 15:50:01 -08003875 ((method_type == METHOD_SUPER ||
3876 method_type == METHOD_VIRTUAL ||
Orion Hodsoncfa325e2016-10-13 10:25:54 +01003877 method_type == METHOD_INTERFACE) && res_method->IsDirect()) ||
3878 ((method_type == METHOD_POLYMORPHIC) &&
3879 (!res_method->IsNative() || !res_method->IsVarargs()))) {
Ian Rogers2fc14272012-08-30 10:56:57 -07003880 Fail(VERIFY_ERROR_CLASS_CHANGE) << "invoke type (" << method_type << ") does not match method "
Orion Hodsoncfa325e2016-10-13 10:25:54 +01003881 "type of " << res_method->PrettyMethod();
Ian Rogers7b078e82014-09-10 14:44:24 -07003882 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003883 }
Alex Lightafb66472017-08-01 09:54:49 -07003884 // Make sure we weren't expecting to fail.
3885 DCHECK(!must_fail) << "invoke type (" << method_type << ")"
3886 << klass->PrettyDescriptor() << "."
3887 << dex_file_->GetMethodName(method_id) << " "
3888 << dex_file_->GetMethodSignature(method_id) << " unexpectedly resolved to "
3889 << res_method->PrettyMethod() << " without error. Initially this method was "
3890 << "not found so we were expecting to fail for some reason.";
jeffhao8cd6dda2012-02-22 10:15:34 -08003891 return res_method;
3892}
3893
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003894template <class T>
Mathieu Chartiere401d142015-04-22 13:56:20 -07003895ArtMethod* MethodVerifier::VerifyInvocationArgsFromIterator(
3896 T* it, const Instruction* inst, MethodType method_type, bool is_range, ArtMethod* res_method) {
Andreas Gampee383d232018-06-19 12:29:51 -07003897 DCHECK_EQ(!is_range, inst->HasVarArgs());
3898
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003899 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
3900 // match the call to the signature. Also, we might be calling through an abstract method
3901 // definition (which doesn't have register count values).
Orion Hodsoncfa325e2016-10-13 10:25:54 +01003902 const size_t expected_args = inst->VRegA();
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003903 /* caught by static verifier */
3904 DCHECK(is_range || expected_args <= 5);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003905
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08003906 if (expected_args > code_item_accessor_.OutsSize()) {
Orion Hodson1cda7c22017-08-10 13:06:45 +01003907 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08003908 << ") exceeds outsSize ("
3909 << code_item_accessor_.OutsSize() << ")";
Orion Hodson1cda7c22017-08-10 13:06:45 +01003910 return nullptr;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003911 }
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003912
3913 /*
3914 * Check the "this" argument, which must be an instance of the class that declared the method.
3915 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
3916 * rigorous check here (which is okay since we have to do it at runtime).
3917 */
3918 if (method_type != METHOD_STATIC) {
Orion Hodsoncfa325e2016-10-13 10:25:54 +01003919 const RegType& actual_arg_type = work_line_->GetInvocationThis(this, inst);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003920 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
3921 CHECK(have_pending_hard_failure_);
3922 return nullptr;
3923 }
David Brazdil68b5c0b2016-01-19 14:25:29 +00003924 bool is_init = false;
3925 if (actual_arg_type.IsUninitializedTypes()) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003926 if (res_method) {
3927 if (!res_method->IsConstructor()) {
3928 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
3929 return nullptr;
3930 }
3931 } else {
3932 // Check whether the name of the called method is "<init>"
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00003933 const uint32_t method_idx = GetMethodIdxOfInvoke(inst);
Jeff Hao0d087272014-08-04 14:47:17 -07003934 if (strcmp(dex_file_->GetMethodName(dex_file_->GetMethodId(method_idx)), "<init>") != 0) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003935 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
3936 return nullptr;
3937 }
3938 }
David Brazdil68b5c0b2016-01-19 14:25:29 +00003939 is_init = true;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003940 }
David Brazdil68b5c0b2016-01-19 14:25:29 +00003941 const RegType& adjusted_type = is_init
3942 ? GetRegTypeCache()->FromUninitialized(actual_arg_type)
3943 : actual_arg_type;
Andreas Gampeeed3a5d2017-11-29 14:58:34 -08003944 if (method_type != METHOD_INTERFACE && !adjusted_type.IsZeroOrNull()) {
Ian Rogersd8f69b02014-09-10 21:43:52 +00003945 const RegType* res_method_class;
Andreas Gamped9e23012015-06-04 22:19:58 -07003946 // Miranda methods have the declaring interface as their declaring class, not the abstract
3947 // class. It would be wrong to use this for the type check (interface type checks are
3948 // postponed to runtime).
3949 if (res_method != nullptr && !res_method->IsMiranda()) {
Vladimir Markod93e3742018-07-18 10:58:13 +01003950 ObjPtr<mirror::Class> klass = res_method->GetDeclaringClass();
Ian Rogers1ff3c982014-08-12 02:30:58 -07003951 std::string temp;
Andreas Gampef23f33d2015-06-23 14:18:17 -07003952 res_method_class = &FromClass(klass->GetDescriptor(&temp), klass,
3953 klass->CannotBeAssignedFromOtherTypes());
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003954 } else {
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00003955 const uint32_t method_idx = GetMethodIdxOfInvoke(inst);
Andreas Gampea5b09a62016-11-17 15:21:22 -08003956 const dex::TypeIndex class_idx = dex_file_->GetMethodId(method_idx).class_idx_;
Mathieu Chartierde40d472015-10-15 17:47:48 -07003957 res_method_class = &reg_types_.FromDescriptor(
3958 GetClassLoader(),
3959 dex_file_->StringByTypeIdx(class_idx),
3960 false);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003961 }
David Brazdilca3c8c32016-09-06 14:04:48 +01003962 if (!res_method_class->IsAssignableFrom(adjusted_type, this)) {
David Brazdil68b5c0b2016-01-19 14:25:29 +00003963 Fail(adjusted_type.IsUnresolvedTypes()
3964 ? VERIFY_ERROR_NO_CLASS
3965 : VERIFY_ERROR_BAD_CLASS_SOFT)
3966 << "'this' argument '" << actual_arg_type << "' not instance of '"
3967 << *res_method_class << "'";
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003968 // Continue on soft failures. We need to find possible hard failures to avoid problems in
3969 // the compiler.
3970 if (have_pending_hard_failure_) {
3971 return nullptr;
3972 }
3973 }
3974 }
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003975 }
3976
Orion Hodsoncfa325e2016-10-13 10:25:54 +01003977 uint32_t arg[5];
3978 if (!is_range) {
3979 inst->GetVarArgs(arg);
3980 }
3981 uint32_t sig_registers = (method_type == METHOD_STATIC) ? 0 : 1;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003982 for ( ; it->HasNext(); it->Next()) {
3983 if (sig_registers >= expected_args) {
3984 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, expected " << inst->VRegA() <<
Orion Hodsoncfa325e2016-10-13 10:25:54 +01003985 " argument registers, method signature has " << sig_registers + 1 << " or more";
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003986 return nullptr;
3987 }
3988
3989 const char* param_descriptor = it->GetDescriptor();
3990
3991 if (param_descriptor == nullptr) {
3992 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation because of missing signature "
3993 "component";
3994 return nullptr;
3995 }
3996
Ian Rogersd8f69b02014-09-10 21:43:52 +00003997 const RegType& reg_type = reg_types_.FromDescriptor(GetClassLoader(), param_descriptor, false);
Orion Hodsoncfa325e2016-10-13 10:25:54 +01003998 uint32_t get_reg = is_range ? inst->VRegC() + static_cast<uint32_t>(sig_registers) :
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003999 arg[sig_registers];
4000 if (reg_type.IsIntegralTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004001 const RegType& src_type = work_line_->GetRegisterType(this, get_reg);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004002 if (!src_type.IsIntegralTypes()) {
4003 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register v" << get_reg << " has type " << src_type
4004 << " but expected " << reg_type;
Andreas Gampeb588f4c2015-05-26 13:35:39 -07004005 return nullptr;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004006 }
Andreas Gampeda9badb2015-06-05 20:22:12 -07004007 } else {
4008 if (!work_line_->VerifyRegisterType(this, get_reg, reg_type)) {
4009 // Continue on soft failures. We need to find possible hard failures to avoid problems in
4010 // the compiler.
4011 if (have_pending_hard_failure_) {
4012 return nullptr;
4013 }
4014 } else if (reg_type.IsLongOrDoubleTypes()) {
4015 // Check that registers are consecutive (for non-range invokes). Invokes are the only
4016 // instructions not specifying register pairs by the first component, but require them
4017 // nonetheless. Only check when there's an actual register in the parameters. If there's
4018 // none, this will fail below.
4019 if (!is_range && sig_registers + 1 < expected_args) {
4020 uint32_t second_reg = arg[sig_registers + 1];
4021 if (second_reg != get_reg + 1) {
4022 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, long or double parameter "
4023 "at index " << sig_registers << " is not a pair: " << get_reg << " + "
4024 << second_reg << ".";
4025 return nullptr;
4026 }
4027 }
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004028 }
4029 }
4030 sig_registers += reg_type.IsLongOrDoubleTypes() ? 2 : 1;
4031 }
4032 if (expected_args != sig_registers) {
4033 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, expected " << expected_args <<
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004034 " argument registers, method signature has " << sig_registers;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004035 return nullptr;
4036 }
4037 return res_method;
4038}
4039
4040void MethodVerifier::VerifyInvocationArgsUnresolvedMethod(const Instruction* inst,
4041 MethodType method_type,
4042 bool is_range) {
4043 // As the method may not have been resolved, make this static check against what we expect.
4044 // The main reason for this code block is to fail hard when we find an illegal use, e.g.,
4045 // wrong number of arguments or wrong primitive types, even if the method could not be resolved.
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00004046 const uint32_t method_idx = GetMethodIdxOfInvoke(inst);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004047 DexFileParameterIterator it(*dex_file_,
4048 dex_file_->GetProtoId(dex_file_->GetMethodId(method_idx).proto_idx_));
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004049 VerifyInvocationArgsFromIterator(&it, inst, method_type, is_range, nullptr);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004050}
4051
Orion Hodsonc069a302017-01-18 09:23:12 +00004052bool MethodVerifier::CheckCallSite(uint32_t call_site_idx) {
Orion Hodson3a842f52017-04-21 15:24:10 +01004053 if (call_site_idx >= dex_file_->NumCallSiteIds()) {
4054 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Bad call site id #" << call_site_idx
4055 << " >= " << dex_file_->NumCallSiteIds();
4056 return false;
4057 }
4058
Orion Hodsonc069a302017-01-18 09:23:12 +00004059 CallSiteArrayValueIterator it(*dex_file_, dex_file_->GetCallSiteId(call_site_idx));
4060 // Check essential arguments are provided. The dex file verifier has verified indicies of the
4061 // main values (method handle, name, method_type).
Orion Hodson4c8e12e2018-05-18 08:33:20 +01004062 static const size_t kRequiredArguments = 3;
4063 if (it.Size() < kRequiredArguments) {
Orion Hodsonc069a302017-01-18 09:23:12 +00004064 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Call site #" << call_site_idx
4065 << " has too few arguments: "
Orion Hodson4c8e12e2018-05-18 08:33:20 +01004066 << it.Size() << " < " << kRequiredArguments;
Orion Hodsonc069a302017-01-18 09:23:12 +00004067 return false;
4068 }
4069
Orion Hodson4c8e12e2018-05-18 08:33:20 +01004070 std::pair<const EncodedArrayValueIterator::ValueType, size_t> type_and_max[kRequiredArguments] =
4071 { { EncodedArrayValueIterator::ValueType::kMethodHandle, dex_file_->NumMethodHandles() },
4072 { EncodedArrayValueIterator::ValueType::kString, dex_file_->NumStringIds() },
4073 { EncodedArrayValueIterator::ValueType::kMethodType, dex_file_->NumProtoIds() }
4074 };
4075 uint32_t index[kRequiredArguments];
4076
4077 // Check arguments have expected types and are within permitted ranges.
4078 for (size_t i = 0; i < kRequiredArguments; ++i) {
4079 if (it.GetValueType() != type_and_max[i].first) {
4080 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Call site id #" << call_site_idx
4081 << " argument " << i << " has wrong type "
4082 << it.GetValueType() << "!=" << type_and_max[i].first;
4083 return false;
4084 }
4085 index[i] = static_cast<uint32_t>(it.GetJavaValue().i);
4086 if (index[i] >= type_and_max[i].second) {
4087 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Call site id #" << call_site_idx
4088 << " argument " << i << " bad index "
4089 << index[i] << " >= " << type_and_max[i].second;
4090 return false;
4091 }
4092 it.Next();
Orion Hodsona5dca522018-02-27 12:42:11 +00004093 }
Orion Hodson3a842f52017-04-21 15:24:10 +01004094
Orion Hodson4c8e12e2018-05-18 08:33:20 +01004095 // Check method handle kind is valid.
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08004096 const dex::MethodHandleItem& mh = dex_file_->GetMethodHandle(index[0]);
Orion Hodsonc069a302017-01-18 09:23:12 +00004097 if (mh.method_handle_type_ != static_cast<uint16_t>(DexFile::MethodHandleType::kInvokeStatic)) {
4098 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Call site #" << call_site_idx
Orion Hodson631827d2017-04-10 14:53:47 +01004099 << " argument 0 method handle type is not InvokeStatic: "
4100 << mh.method_handle_type_;
Orion Hodsonc069a302017-01-18 09:23:12 +00004101 return false;
4102 }
Orion Hodsonc069a302017-01-18 09:23:12 +00004103 return true;
4104}
4105
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004106class MethodParamListDescriptorIterator {
4107 public:
Mathieu Chartiere401d142015-04-22 13:56:20 -07004108 explicit MethodParamListDescriptorIterator(ArtMethod* res_method) :
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004109 res_method_(res_method), pos_(0), params_(res_method->GetParameterTypeList()),
4110 params_size_(params_ == nullptr ? 0 : params_->Size()) {
4111 }
4112
4113 bool HasNext() {
4114 return pos_ < params_size_;
4115 }
4116
4117 void Next() {
4118 ++pos_;
4119 }
4120
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07004121 const char* GetDescriptor() REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004122 return res_method_->GetTypeDescriptorFromTypeIdx(params_->GetTypeItem(pos_).type_idx_);
4123 }
4124
4125 private:
Mathieu Chartiere401d142015-04-22 13:56:20 -07004126 ArtMethod* res_method_;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004127 size_t pos_;
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08004128 const dex::TypeList* params_;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004129 const size_t params_size_;
4130};
4131
Mathieu Chartiere401d142015-04-22 13:56:20 -07004132ArtMethod* MethodVerifier::VerifyInvocationArgs(
Alex Light7268d472016-01-20 15:50:01 -08004133 const Instruction* inst, MethodType method_type, bool is_range) {
jeffhao8cd6dda2012-02-22 10:15:34 -08004134 // Resolve the method. This could be an abstract or concrete method depending on what sort of call
4135 // we're making.
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00004136 const uint32_t method_idx = GetMethodIdxOfInvoke(inst);
Alex Light7268d472016-01-20 15:50:01 -08004137 ArtMethod* res_method = ResolveMethodAndCheckAccess(method_idx, method_type);
Ian Rogers7b078e82014-09-10 14:44:24 -07004138 if (res_method == nullptr) { // error or class is unresolved
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004139 // Check what we can statically.
4140 if (!have_pending_hard_failure_) {
4141 VerifyInvocationArgsUnresolvedMethod(inst, method_type, is_range);
4142 }
4143 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08004144 }
4145
Ian Rogersd81871c2011-10-03 13:57:23 -07004146 // If we're using invoke-super(method), make sure that the executing method's class' superclass
Alex Light705ad492015-09-21 11:36:30 -07004147 // has a vtable entry for the target method. Or the target is on a interface.
Alex Light7268d472016-01-20 15:50:01 -08004148 if (method_type == METHOD_SUPER) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08004149 dex::TypeIndex class_idx = dex_file_->GetMethodId(method_idx).class_idx_;
David Brazdilca3c8c32016-09-06 14:04:48 +01004150 const RegType& reference_type = reg_types_.FromDescriptor(
4151 GetClassLoader(),
4152 dex_file_->StringByTypeIdx(class_idx),
4153 false);
4154 if (reference_type.IsUnresolvedTypes()) {
Alex Lightfedd91d2016-01-07 14:49:16 -08004155 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "Unable to find referenced class from invoke-super";
4156 return nullptr;
4157 }
David Brazdilca3c8c32016-09-06 14:04:48 +01004158 if (reference_type.GetClass()->IsInterface()) {
Alex Lightfedd91d2016-01-07 14:49:16 -08004159 // TODO Can we verify anything else.
David Brazdil15fc7292016-09-02 14:13:18 +01004160 if (class_idx == class_def_.class_idx_) {
Alex Lightfedd91d2016-01-07 14:49:16 -08004161 Fail(VERIFY_ERROR_CLASS_CHANGE) << "Cannot invoke-super on self as interface";
Alex Light55ea94d2016-03-15 09:50:26 -07004162 return nullptr;
Alex Lightfedd91d2016-01-07 14:49:16 -08004163 }
4164 // TODO Revisit whether we want to allow invoke-super on direct interfaces only like the JLS
4165 // does.
Alex Light55ea94d2016-03-15 09:50:26 -07004166 if (!GetDeclaringClass().HasClass()) {
4167 Fail(VERIFY_ERROR_NO_CLASS) << "Unable to resolve the full class of 'this' used in an"
4168 << "interface invoke-super";
4169 return nullptr;
David Brazdilca3c8c32016-09-06 14:04:48 +01004170 } else if (!reference_type.IsStrictlyAssignableFrom(GetDeclaringClass(), this)) {
Alex Lightfedd91d2016-01-07 14:49:16 -08004171 Fail(VERIFY_ERROR_CLASS_CHANGE)
David Sehr709b0702016-10-13 09:12:37 -07004172 << "invoke-super in " << mirror::Class::PrettyClass(GetDeclaringClass().GetClass())
4173 << " in method "
4174 << dex_file_->PrettyMethod(dex_method_idx_) << " to method "
4175 << dex_file_->PrettyMethod(method_idx) << " references "
4176 << "non-super-interface type " << mirror::Class::PrettyClass(reference_type.GetClass());
Alex Lightfedd91d2016-01-07 14:49:16 -08004177 return nullptr;
Alex Light705ad492015-09-21 11:36:30 -07004178 }
4179 } else {
4180 const RegType& super = GetDeclaringClass().GetSuperClass(&reg_types_);
4181 if (super.IsUnresolvedTypes()) {
4182 Fail(VERIFY_ERROR_NO_METHOD) << "unknown super class in invoke-super from "
David Sehr709b0702016-10-13 09:12:37 -07004183 << dex_file_->PrettyMethod(dex_method_idx_)
4184 << " to super " << res_method->PrettyMethod();
Alex Light705ad492015-09-21 11:36:30 -07004185 return nullptr;
4186 }
David Brazdilca3c8c32016-09-06 14:04:48 +01004187 if (!reference_type.IsStrictlyAssignableFrom(GetDeclaringClass(), this) ||
Aart Bikf663e342016-04-04 17:28:59 -07004188 (res_method->GetMethodIndex() >= super.GetClass()->GetVTableLength())) {
Alex Light705ad492015-09-21 11:36:30 -07004189 Fail(VERIFY_ERROR_NO_METHOD) << "invalid invoke-super from "
David Sehr709b0702016-10-13 09:12:37 -07004190 << dex_file_->PrettyMethod(dex_method_idx_)
Alex Light705ad492015-09-21 11:36:30 -07004191 << " to super " << super
4192 << "." << res_method->GetName()
4193 << res_method->GetSignature();
4194 return nullptr;
4195 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004196 }
4197 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08004198
Andreas Gampe74979b12017-05-16 09:28:06 -07004199 if (UNLIKELY(method_type == METHOD_POLYMORPHIC)) {
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004200 // Process the signature of the calling site that is invoking the method handle.
Orion Hodson06d10a72018-05-14 08:53:38 +01004201 dex::ProtoIndex proto_idx(inst->VRegH());
4202 DexFileParameterIterator it(*dex_file_, dex_file_->GetProtoId(proto_idx));
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004203 return VerifyInvocationArgsFromIterator(&it, inst, method_type, is_range, res_method);
4204 } else {
4205 // Process the target method's signature.
4206 MethodParamListDescriptorIterator it(res_method);
4207 return VerifyInvocationArgsFromIterator(&it, inst, method_type, is_range, res_method);
4208 }
4209}
4210
4211bool MethodVerifier::CheckSignaturePolymorphicMethod(ArtMethod* method) {
Vladimir Markod93e3742018-07-18 10:58:13 +01004212 ObjPtr<mirror::Class> klass = method->GetDeclaringClass();
Orion Hodsonfe92d122018-01-02 10:45:17 +00004213 const char* method_name = method->GetName();
4214
4215 const char* expected_return_descriptor;
Vladimir Markoc7aa87e2018-05-24 15:19:52 +01004216 ObjPtr<mirror::ObjectArray<mirror::Class>> class_roots =
4217 Runtime::Current()->GetClassLinker()->GetClassRoots();
4218 if (klass == GetClassRoot<mirror::MethodHandle>(class_roots)) {
Orion Hodsonfe92d122018-01-02 10:45:17 +00004219 expected_return_descriptor = mirror::MethodHandle::GetReturnTypeDescriptor(method_name);
Vladimir Markoc7aa87e2018-05-24 15:19:52 +01004220 } else if (klass == GetClassRoot<mirror::VarHandle>(class_roots)) {
Orion Hodsonfe92d122018-01-02 10:45:17 +00004221 expected_return_descriptor = mirror::VarHandle::GetReturnTypeDescriptor(method_name);
4222 } else {
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004223 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
Orion Hodsonfe92d122018-01-02 10:45:17 +00004224 << "Signature polymorphic method in unsuppported class: " << klass->PrettyDescriptor();
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004225 return false;
4226 }
4227
Orion Hodsonfe92d122018-01-02 10:45:17 +00004228 if (expected_return_descriptor == nullptr) {
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004229 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
4230 << "Signature polymorphic method name invalid: " << method_name;
4231 return false;
4232 }
4233
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08004234 const dex::TypeList* types = method->GetParameterTypeList();
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004235 if (types->Size() != 1) {
4236 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
4237 << "Signature polymorphic method has too many arguments " << types->Size() << " != 1";
4238 return false;
4239 }
4240
4241 const dex::TypeIndex argument_type_index = types->GetTypeItem(0).type_idx_;
4242 const char* argument_descriptor = method->GetTypeDescriptorFromTypeIdx(argument_type_index);
4243 if (strcmp(argument_descriptor, "[Ljava/lang/Object;") != 0) {
4244 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
4245 << "Signature polymorphic method has unexpected argument type: " << argument_descriptor;
4246 return false;
4247 }
4248
4249 const char* return_descriptor = method->GetReturnTypeDescriptor();
Orion Hodsonfe92d122018-01-02 10:45:17 +00004250 if (strcmp(return_descriptor, expected_return_descriptor) != 0) {
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004251 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
Orion Hodsonfe92d122018-01-02 10:45:17 +00004252 << "Signature polymorphic method has unexpected return type: " << return_descriptor
4253 << " != " << expected_return_descriptor;
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004254 return false;
4255 }
4256
4257 return true;
4258}
4259
4260bool MethodVerifier::CheckSignaturePolymorphicReceiver(const Instruction* inst) {
4261 const RegType& this_type = work_line_->GetInvocationThis(this, inst);
Andreas Gampeeed3a5d2017-11-29 14:58:34 -08004262 if (this_type.IsZeroOrNull()) {
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004263 /* null pointer always passes (and always fails at run time) */
4264 return true;
4265 } else if (!this_type.IsNonZeroReferenceTypes()) {
4266 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
4267 << "invoke-polymorphic receiver is not a reference: "
4268 << this_type;
4269 return false;
4270 } else if (this_type.IsUninitializedReference()) {
4271 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
4272 << "invoke-polymorphic receiver is uninitialized: "
4273 << this_type;
4274 return false;
4275 } else if (!this_type.HasClass()) {
4276 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
4277 << "invoke-polymorphic receiver has no class: "
4278 << this_type;
4279 return false;
Vladimir Markoc7aa87e2018-05-24 15:19:52 +01004280 } else {
4281 ObjPtr<mirror::ObjectArray<mirror::Class>> class_roots =
4282 Runtime::Current()->GetClassLinker()->GetClassRoots();
4283 if (!this_type.GetClass()->IsSubClass(GetClassRoot<mirror::MethodHandle>(class_roots)) &&
4284 !this_type.GetClass()->IsSubClass(GetClassRoot<mirror::VarHandle>(class_roots))) {
4285 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
4286 << "invoke-polymorphic receiver is not a subclass of MethodHandle or VarHandle: "
4287 << this_type;
4288 return false;
4289 }
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004290 }
4291 return true;
Ian Rogersd81871c2011-10-03 13:57:23 -07004292}
4293
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00004294uint16_t MethodVerifier::GetMethodIdxOfInvoke(const Instruction* inst) {
4295 switch (inst->Opcode()) {
4296 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK:
4297 case Instruction::INVOKE_VIRTUAL_QUICK: {
4298 DCHECK(Runtime::Current()->IsStarted() || verify_to_dump_)
4299 << dex_file_->PrettyMethod(dex_method_idx_, true) << "@" << work_insn_idx_;
4300 DCHECK(method_being_verified_ != nullptr);
4301 uint16_t method_idx = method_being_verified_->GetIndexFromQuickening(work_insn_idx_);
4302 CHECK_NE(method_idx, DexFile::kDexNoIndex16);
4303 return method_idx;
4304 }
4305 default: {
4306 return inst->VRegB();
4307 }
Mathieu Chartier091d2382015-03-06 10:59:06 -08004308 }
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004309}
4310
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00004311uint16_t MethodVerifier::GetFieldIdxOfFieldAccess(const Instruction* inst, bool is_static) {
4312 if (is_static) {
4313 return inst->VRegB_21c();
4314 } else if (inst->IsQuickened()) {
4315 DCHECK(Runtime::Current()->IsStarted() || verify_to_dump_);
4316 DCHECK(method_being_verified_ != nullptr);
4317 uint16_t field_idx = method_being_verified_->GetIndexFromQuickening(work_insn_idx_);
4318 CHECK_NE(field_idx, DexFile::kDexNoIndex16);
4319 return field_idx;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004320 } else {
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00004321 return inst->VRegC_22c();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004322 }
4323}
4324
Ian Rogers62342ec2013-06-11 10:26:37 -07004325void MethodVerifier::VerifyNewArray(const Instruction* inst, bool is_filled, bool is_range) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08004326 dex::TypeIndex type_idx;
Sebastien Hertz5243e912013-05-21 10:55:07 +02004327 if (!is_filled) {
4328 DCHECK_EQ(inst->Opcode(), Instruction::NEW_ARRAY);
Andreas Gampea5b09a62016-11-17 15:21:22 -08004329 type_idx = dex::TypeIndex(inst->VRegC_22c());
Sebastien Hertz5243e912013-05-21 10:55:07 +02004330 } else if (!is_range) {
4331 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY);
Andreas Gampea5b09a62016-11-17 15:21:22 -08004332 type_idx = dex::TypeIndex(inst->VRegB_35c());
Sebastien Hertz5243e912013-05-21 10:55:07 +02004333 } else {
4334 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY_RANGE);
Andreas Gampea5b09a62016-11-17 15:21:22 -08004335 type_idx = dex::TypeIndex(inst->VRegB_3rc());
Sebastien Hertz5243e912013-05-21 10:55:07 +02004336 }
Andreas Gampe98be1a92017-08-28 08:25:45 -07004337 const RegType& res_type = ResolveClass<CheckAccess::kYes>(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004338 if (res_type.IsConflict()) { // bad class
4339 DCHECK_NE(failures_.size(), 0U);
Ian Rogers0c4a5062012-02-03 15:18:59 -08004340 } else {
4341 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
4342 if (!res_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07004343 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "new-array on non-array class " << res_type;
Ian Rogers0c4a5062012-02-03 15:18:59 -08004344 } else if (!is_filled) {
4345 /* make sure "size" register is valid type */
Ian Rogers7b078e82014-09-10 14:44:24 -07004346 work_line_->VerifyRegisterType(this, inst->VRegB_22c(), reg_types_.Integer());
Ian Rogers0c4a5062012-02-03 15:18:59 -08004347 /* set register type to array class */
Ian Rogersd8f69b02014-09-10 21:43:52 +00004348 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
Andreas Gampead238ce2015-08-24 21:13:08 -07004349 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_22c(), precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08004350 } else {
Andreas Gampebb18a032016-03-22 20:34:25 -07004351 DCHECK(!res_type.IsUnresolvedMergedReference());
Ian Rogers0c4a5062012-02-03 15:18:59 -08004352 // Verify each register. If "arg_count" is bad, VerifyRegisterType() will run off the end of
4353 // the list and fail. It's legal, if silly, for arg_count to be zero.
Ian Rogersd8f69b02014-09-10 21:43:52 +00004354 const RegType& expected_type = reg_types_.GetComponentType(res_type, GetClassLoader());
Sebastien Hertz5243e912013-05-21 10:55:07 +02004355 uint32_t arg_count = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
4356 uint32_t arg[5];
4357 if (!is_range) {
Ian Rogers29a26482014-05-02 15:27:29 -07004358 inst->GetVarArgs(arg);
Sebastien Hertz5243e912013-05-21 10:55:07 +02004359 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08004360 for (size_t ui = 0; ui < arg_count; ui++) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02004361 uint32_t get_reg = is_range ? inst->VRegC_3rc() + ui : arg[ui];
Ian Rogers7b078e82014-09-10 14:44:24 -07004362 if (!work_line_->VerifyRegisterType(this, get_reg, expected_type)) {
4363 work_line_->SetResultRegisterType(this, reg_types_.Conflict());
Ian Rogers0c4a5062012-02-03 15:18:59 -08004364 return;
4365 }
4366 }
4367 // filled-array result goes into "result" register
Ian Rogersd8f69b02014-09-10 21:43:52 +00004368 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
Ian Rogers7b078e82014-09-10 14:44:24 -07004369 work_line_->SetResultRegisterType(this, precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08004370 }
4371 }
4372}
4373
Sebastien Hertz5243e912013-05-21 10:55:07 +02004374void MethodVerifier::VerifyAGet(const Instruction* inst,
Ian Rogersd8f69b02014-09-10 21:43:52 +00004375 const RegType& insn_type, bool is_primitive) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004376 const RegType& index_type = work_line_->GetRegisterType(this, inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07004377 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07004378 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07004379 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004380 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegB_23x());
Andreas Gampeeed3a5d2017-11-29 14:58:34 -08004381 if (array_type.IsZeroOrNull()) {
Ian Rogers89310de2012-02-01 13:47:30 -08004382 // Null array class; this code path will fail at runtime. Infer a merge-able type from the
Andreas Gampe52f205a2017-12-01 12:16:07 -08004383 // instruction type.
4384 if (!is_primitive) {
4385 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), reg_types_.Null());
4386 } else if (insn_type.IsInteger()) {
4387 // Pick a non-zero constant (to distinguish with null) that can fit in any primitive.
4388 // We cannot use 'insn_type' as it could be a float array or an int array.
4389 work_line_->SetRegisterType<LockOp::kClear>(
4390 this, inst->VRegA_23x(), DetermineCat1Constant(1, need_precise_constants_));
4391 } else if (insn_type.IsCategory1Types()) {
4392 // Category 1
4393 // The 'insn_type' is exactly the type we need.
4394 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), insn_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07004395 } else {
Ian Rogers89310de2012-02-01 13:47:30 -08004396 // Category 2
Ian Rogers7b078e82014-09-10 14:44:24 -07004397 work_line_->SetRegisterTypeWide(this, inst->VRegA_23x(),
4398 reg_types_.FromCat2ConstLo(0, false),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004399 reg_types_.FromCat2ConstHi(0, false));
Ian Rogers89310de2012-02-01 13:47:30 -08004400 }
jeffhaofc3144e2012-02-01 17:21:15 -08004401 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07004402 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aget";
Andreas Gampe8d8fc482016-03-25 16:24:20 -07004403 } else if (array_type.IsUnresolvedMergedReference()) {
Andreas Gampebb18a032016-03-22 20:34:25 -07004404 // Unresolved array types must be reference array types.
4405 if (is_primitive) {
4406 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type
4407 << " source for category 1 aget";
4408 } else {
4409 Fail(VERIFY_ERROR_NO_CLASS) << "cannot verify aget for " << array_type
4410 << " because of missing class";
Andreas Gampe8d8fc482016-03-25 16:24:20 -07004411 // Approximate with java.lang.Object[].
4412 work_line_->SetRegisterType<LockOp::kClear>(this,
4413 inst->VRegA_23x(),
4414 reg_types_.JavaLangObject(false));
Andreas Gampebb18a032016-03-22 20:34:25 -07004415 }
Ian Rogers89310de2012-02-01 13:47:30 -08004416 } else {
4417 /* verify the class */
Ian Rogersd8f69b02014-09-10 21:43:52 +00004418 const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
jeffhaofc3144e2012-02-01 17:21:15 -08004419 if (!component_type.IsReferenceTypes() && !is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07004420 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08004421 << " source for aget-object";
4422 } else if (component_type.IsNonZeroReferenceTypes() && is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07004423 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08004424 << " source for category 1 aget";
4425 } else if (is_primitive && !insn_type.Equals(component_type) &&
4426 !((insn_type.IsInteger() && component_type.IsFloat()) ||
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004427 (insn_type.IsLong() && component_type.IsDouble()))) {
4428 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array type " << array_type
4429 << " incompatible with aget of type " << insn_type;
Ian Rogers89310de2012-02-01 13:47:30 -08004430 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004431 // Use knowledge of the field type which is stronger than the type inferred from the
4432 // instruction, which can't differentiate object types and ints from floats, longs from
4433 // doubles.
4434 if (!component_type.IsLowHalf()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07004435 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), component_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004436 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004437 work_line_->SetRegisterTypeWide(this, inst->VRegA_23x(), component_type,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004438 component_type.HighHalf(&reg_types_));
4439 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004440 }
4441 }
4442 }
4443}
4444
Ian Rogersd8f69b02014-09-10 21:43:52 +00004445void MethodVerifier::VerifyPrimitivePut(const RegType& target_type, const RegType& insn_type,
Jeff Haofe1f7c82013-08-01 14:50:24 -07004446 const uint32_t vregA) {
4447 // Primitive assignability rules are weaker than regular assignability rules.
4448 bool instruction_compatible;
4449 bool value_compatible;
Ian Rogers7b078e82014-09-10 14:44:24 -07004450 const RegType& value_type = work_line_->GetRegisterType(this, vregA);
Jeff Haofe1f7c82013-08-01 14:50:24 -07004451 if (target_type.IsIntegralTypes()) {
Jeff Haoa4647482013-08-06 15:35:47 -07004452 instruction_compatible = target_type.Equals(insn_type);
Jeff Haofe1f7c82013-08-01 14:50:24 -07004453 value_compatible = value_type.IsIntegralTypes();
4454 } else if (target_type.IsFloat()) {
4455 instruction_compatible = insn_type.IsInteger(); // no put-float, so expect put-int
4456 value_compatible = value_type.IsFloatTypes();
4457 } else if (target_type.IsLong()) {
4458 instruction_compatible = insn_type.IsLong();
Andreas Gampe376fa682014-09-07 13:06:12 -07004459 // Additional register check: this is not checked statically (as part of VerifyInstructions),
4460 // as target_type depends on the resolved type of the field.
4461 if (instruction_compatible && work_line_->NumRegs() > vregA + 1) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004462 const RegType& value_type_hi = work_line_->GetRegisterType(this, vregA + 1);
Andreas Gampe376fa682014-09-07 13:06:12 -07004463 value_compatible = value_type.IsLongTypes() && value_type.CheckWidePair(value_type_hi);
4464 } else {
4465 value_compatible = false;
4466 }
Jeff Haofe1f7c82013-08-01 14:50:24 -07004467 } else if (target_type.IsDouble()) {
4468 instruction_compatible = insn_type.IsLong(); // no put-double, so expect put-long
Andreas Gampe376fa682014-09-07 13:06:12 -07004469 // Additional register check: this is not checked statically (as part of VerifyInstructions),
4470 // as target_type depends on the resolved type of the field.
4471 if (instruction_compatible && work_line_->NumRegs() > vregA + 1) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004472 const RegType& value_type_hi = work_line_->GetRegisterType(this, vregA + 1);
Andreas Gampe376fa682014-09-07 13:06:12 -07004473 value_compatible = value_type.IsDoubleTypes() && value_type.CheckWidePair(value_type_hi);
4474 } else {
4475 value_compatible = false;
4476 }
Jeff Haofe1f7c82013-08-01 14:50:24 -07004477 } else {
4478 instruction_compatible = false; // reference with primitive store
4479 value_compatible = false; // unused
4480 }
4481 if (!instruction_compatible) {
4482 // This is a global failure rather than a class change failure as the instructions and
4483 // the descriptors for the type should have been consistent within the same file at
4484 // compile time.
4485 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "put insn has type '" << insn_type
4486 << "' but expected type '" << target_type << "'";
4487 return;
4488 }
4489 if (!value_compatible) {
4490 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
4491 << " of type " << value_type << " but expected " << target_type << " for put";
4492 return;
4493 }
4494}
4495
Sebastien Hertz5243e912013-05-21 10:55:07 +02004496void MethodVerifier::VerifyAPut(const Instruction* inst,
Ian Rogersd8f69b02014-09-10 21:43:52 +00004497 const RegType& insn_type, bool is_primitive) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004498 const RegType& index_type = work_line_->GetRegisterType(this, inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07004499 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07004500 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07004501 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004502 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegB_23x());
Andreas Gampeeed3a5d2017-11-29 14:58:34 -08004503 if (array_type.IsZeroOrNull()) {
Nicolas Geoffray66389fb2015-06-19 10:35:42 +01004504 // Null array type; this code path will fail at runtime.
4505 // Still check that the given value matches the instruction's type.
Andreas Gampe4bf4c782015-08-14 14:07:43 -07004506 // Note: this is, as usual, complicated by the fact the the instruction isn't fully typed
4507 // and fits multiple register types.
4508 const RegType* modified_reg_type = &insn_type;
4509 if ((modified_reg_type == &reg_types_.Integer()) ||
4510 (modified_reg_type == &reg_types_.LongLo())) {
4511 // May be integer or float | long or double. Overwrite insn_type accordingly.
4512 const RegType& value_type = work_line_->GetRegisterType(this, inst->VRegA_23x());
4513 if (modified_reg_type == &reg_types_.Integer()) {
4514 if (&value_type == &reg_types_.Float()) {
4515 modified_reg_type = &value_type;
4516 }
4517 } else {
4518 if (&value_type == &reg_types_.DoubleLo()) {
4519 modified_reg_type = &value_type;
4520 }
4521 }
4522 }
4523 work_line_->VerifyRegisterType(this, inst->VRegA_23x(), *modified_reg_type);
jeffhaofc3144e2012-02-01 17:21:15 -08004524 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07004525 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aput";
Andreas Gampe8d8fc482016-03-25 16:24:20 -07004526 } else if (array_type.IsUnresolvedMergedReference()) {
Andreas Gampebb18a032016-03-22 20:34:25 -07004527 // Unresolved array types must be reference array types.
4528 if (is_primitive) {
4529 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "put insn has type '" << insn_type
4530 << "' but unresolved type '" << array_type << "'";
4531 } else {
4532 Fail(VERIFY_ERROR_NO_CLASS) << "cannot verify aput for " << array_type
4533 << " because of missing class";
4534 }
Ian Rogers89310de2012-02-01 13:47:30 -08004535 } else {
Ian Rogersd8f69b02014-09-10 21:43:52 +00004536 const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
Jeff Haofe1f7c82013-08-01 14:50:24 -07004537 const uint32_t vregA = inst->VRegA_23x();
Jeff Haob24b4a72013-07-31 13:47:31 -07004538 if (is_primitive) {
Jeff Haofe1f7c82013-08-01 14:50:24 -07004539 VerifyPrimitivePut(component_type, insn_type, vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07004540 } else {
Jeff Haob24b4a72013-07-31 13:47:31 -07004541 if (!component_type.IsReferenceTypes()) {
4542 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
4543 << " source for aput-object";
4544 } else {
4545 // The instruction agrees with the type of array, confirm the value to be stored does too
4546 // Note: we use the instruction type (rather than the component type) for aput-object as
4547 // incompatible classes will be caught at runtime as an array store exception
Ian Rogers7b078e82014-09-10 14:44:24 -07004548 work_line_->VerifyRegisterType(this, vregA, insn_type);
Jeff Haob24b4a72013-07-31 13:47:31 -07004549 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004550 }
4551 }
4552 }
4553}
4554
Mathieu Chartierc7853442015-03-27 14:35:38 -07004555ArtField* MethodVerifier::GetStaticField(int field_idx) {
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08004556 const dex::FieldId& field_id = dex_file_->GetFieldId(field_idx);
Ian Rogers90040192011-12-16 08:54:29 -08004557 // Check access to class
Andreas Gampe98be1a92017-08-28 08:25:45 -07004558 const RegType& klass_type = ResolveClass<CheckAccess::kYes>(field_id.class_idx_);
Brian Carlstrom7934ac22013-07-26 10:54:15 -07004559 if (klass_type.IsConflict()) { // bad class
Ian Rogersad0b3a32012-04-16 14:50:24 -07004560 AppendToLastFailMessage(StringPrintf(" in attempt to access static field %d (%s) in %s",
4561 field_idx, dex_file_->GetFieldName(field_id),
4562 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers7b078e82014-09-10 14:44:24 -07004563 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08004564 }
Elliott Hughesb25c3f62012-03-26 16:35:06 -07004565 if (klass_type.IsUnresolvedTypes()) {
Andreas Gampe629be512017-08-25 17:09:32 -07004566 // Accessibility checks depend on resolved fields.
David Brazdil2bb2fbd2018-11-13 18:24:26 +00004567 DCHECK(klass_type.Equals(GetDeclaringClass()) ||
4568 !failures_.empty() ||
4569 IsSdkVersionSetAndLessThan(api_level_, SdkVersion::kP));
Andreas Gampe629be512017-08-25 17:09:32 -07004570
Ian Rogers7b078e82014-09-10 14:44:24 -07004571 return nullptr; // Can't resolve Class so no more to do here, will do checking at runtime.
Ian Rogers90040192011-12-16 08:54:29 -08004572 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07004573 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Vladimir Markoe11dd502017-12-08 14:09:45 +00004574 ArtField* field = class_linker->ResolveFieldJLS(field_idx, dex_cache_, class_loader_);
David Brazdilca3c8c32016-09-06 14:04:48 +01004575
4576 // Record result of the field resolution attempt.
4577 VerifierDeps::MaybeRecordFieldResolution(*dex_file_, field_idx, field);
4578
Ian Rogers7b078e82014-09-10 14:44:24 -07004579 if (field == nullptr) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07004580 VLOG(verifier) << "Unable to resolve static field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07004581 << dex_file_->GetFieldName(field_id) << ") in "
4582 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogers7b078e82014-09-10 14:44:24 -07004583 DCHECK(self_->IsExceptionPending());
4584 self_->ClearException();
4585 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004586 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
4587 field->GetAccessFlags())) {
David Sehr709b0702016-10-13 09:12:37 -07004588 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access static field " << field->PrettyField()
Ian Rogersad0b3a32012-04-16 14:50:24 -07004589 << " from " << GetDeclaringClass();
Ian Rogers7b078e82014-09-10 14:44:24 -07004590 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07004591 } else if (!field->IsStatic()) {
David Sehr709b0702016-10-13 09:12:37 -07004592 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << field->PrettyField() << " to be static";
Ian Rogers7b078e82014-09-10 14:44:24 -07004593 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07004594 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07004595 return field;
Ian Rogersd81871c2011-10-03 13:57:23 -07004596}
4597
Mathieu Chartierc7853442015-03-27 14:35:38 -07004598ArtField* MethodVerifier::GetInstanceField(const RegType& obj_type, int field_idx) {
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08004599 const dex::FieldId& field_id = dex_file_->GetFieldId(field_idx);
Aart Bik31883642016-06-06 15:02:44 -07004600 // Check access to class.
Andreas Gampe98be1a92017-08-28 08:25:45 -07004601 const RegType& klass_type = ResolveClass<CheckAccess::kYes>(field_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004602 if (klass_type.IsConflict()) {
4603 AppendToLastFailMessage(StringPrintf(" in attempt to access instance field %d (%s) in %s",
4604 field_idx, dex_file_->GetFieldName(field_id),
4605 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers7b078e82014-09-10 14:44:24 -07004606 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08004607 }
jeffhao8cd6dda2012-02-22 10:15:34 -08004608 if (klass_type.IsUnresolvedTypes()) {
Andreas Gampe629be512017-08-25 17:09:32 -07004609 // Accessibility checks depend on resolved fields.
David Brazdil2bb2fbd2018-11-13 18:24:26 +00004610 DCHECK(klass_type.Equals(GetDeclaringClass()) ||
4611 !failures_.empty() ||
4612 IsSdkVersionSetAndLessThan(api_level_, SdkVersion::kP));
Andreas Gampe629be512017-08-25 17:09:32 -07004613
Ian Rogers7b078e82014-09-10 14:44:24 -07004614 return nullptr; // Can't resolve Class so no more to do here
Ian Rogers90040192011-12-16 08:54:29 -08004615 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07004616 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Vladimir Markoe11dd502017-12-08 14:09:45 +00004617 ArtField* field = class_linker->ResolveFieldJLS(field_idx, dex_cache_, class_loader_);
David Brazdilca3c8c32016-09-06 14:04:48 +01004618
4619 // Record result of the field resolution attempt.
4620 VerifierDeps::MaybeRecordFieldResolution(*dex_file_, field_idx, field);
4621
Ian Rogers7b078e82014-09-10 14:44:24 -07004622 if (field == nullptr) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07004623 VLOG(verifier) << "Unable to resolve instance field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07004624 << dex_file_->GetFieldName(field_id) << ") in "
4625 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogers7b078e82014-09-10 14:44:24 -07004626 DCHECK(self_->IsExceptionPending());
4627 self_->ClearException();
4628 return nullptr;
Andreas Gampeeed3a5d2017-11-29 14:58:34 -08004629 } else if (obj_type.IsZeroOrNull()) {
Aart Bik31883642016-06-06 15:02:44 -07004630 // Cannot infer and check type, however, access will cause null pointer exception.
4631 // Fall through into a few last soft failure checks below.
Stephen Kyle695c5982014-08-22 15:03:07 +01004632 } else if (!obj_type.IsReferenceTypes()) {
Aart Bik31883642016-06-06 15:02:44 -07004633 // Trying to read a field from something that isn't a reference.
Stephen Kyle695c5982014-08-22 15:03:07 +01004634 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance field access on object that has "
4635 << "non-reference type " << obj_type;
Ian Rogers7b078e82014-09-10 14:44:24 -07004636 return nullptr;
Ian Rogerse1758fe2012-04-19 11:31:15 -07004637 } else {
David Brazdil0d638bb2016-07-27 15:29:25 +01004638 std::string temp;
Mathieu Chartier3398c782016-09-30 10:27:43 -07004639 ObjPtr<mirror::Class> klass = field->GetDeclaringClass();
Ian Rogersd8f69b02014-09-10 21:43:52 +00004640 const RegType& field_klass =
Vladimir Markobcf17522018-06-01 13:14:32 +01004641 FromClass(klass->GetDescriptor(&temp), klass, klass->CannotBeAssignedFromOtherTypes());
David Brazdil68b5c0b2016-01-19 14:25:29 +00004642 if (obj_type.IsUninitializedTypes()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07004643 // Field accesses through uninitialized references are only allowable for constructors where
David Brazdil68b5c0b2016-01-19 14:25:29 +00004644 // the field is declared in this class.
4645 // Note: this IsConstructor check is technically redundant, as UninitializedThis should only
4646 // appear in constructors.
4647 if (!obj_type.IsUninitializedThisReference() ||
4648 !IsConstructor() ||
4649 !field_klass.Equals(GetDeclaringClass())) {
David Sehr709b0702016-10-13 09:12:37 -07004650 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "cannot access instance field " << field->PrettyField()
David Brazdil68b5c0b2016-01-19 14:25:29 +00004651 << " of a not fully initialized object within the context"
David Sehr709b0702016-10-13 09:12:37 -07004652 << " of " << dex_file_->PrettyMethod(dex_method_idx_);
David Brazdil68b5c0b2016-01-19 14:25:29 +00004653 return nullptr;
4654 }
David Brazdilca3c8c32016-09-06 14:04:48 +01004655 } else if (!field_klass.IsAssignableFrom(obj_type, this)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07004656 // Trying to access C1.field1 using reference of type C2, which is neither C1 or a sub-class
4657 // of C1. For resolution to occur the declared class of the field must be compatible with
4658 // obj_type, we've discovered this wasn't so, so report the field didn't exist.
Andreas Gampe66596242016-04-14 10:55:04 -07004659 VerifyError type;
4660 bool is_aot = Runtime::Current()->IsAotCompiler();
4661 if (is_aot && (field_klass.IsUnresolvedTypes() || obj_type.IsUnresolvedTypes())) {
4662 // Compiler & unresolved types involved, retry at runtime.
4663 type = VerifyError::VERIFY_ERROR_NO_CLASS;
4664 } else {
Andreas Gampe8f4ade02016-04-15 10:09:16 -07004665 // Classes known (resolved; and thus assignability check is precise), or we are at runtime
4666 // and still missing classes. This is a hard failure.
Andreas Gampe66596242016-04-14 10:55:04 -07004667 type = VerifyError::VERIFY_ERROR_BAD_CLASS_HARD;
4668 }
David Sehr709b0702016-10-13 09:12:37 -07004669 Fail(type) << "cannot access instance field " << field->PrettyField()
Andreas Gampe66596242016-04-14 10:55:04 -07004670 << " from object of type " << obj_type;
Ian Rogers7b078e82014-09-10 14:44:24 -07004671 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004672 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004673 }
Aart Bik31883642016-06-06 15:02:44 -07004674
4675 // Few last soft failure checks.
4676 if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
4677 field->GetAccessFlags())) {
David Sehr709b0702016-10-13 09:12:37 -07004678 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access instance field " << field->PrettyField()
Aart Bik31883642016-06-06 15:02:44 -07004679 << " from " << GetDeclaringClass();
4680 return nullptr;
4681 } else if (field->IsStatic()) {
David Sehr709b0702016-10-13 09:12:37 -07004682 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << field->PrettyField()
Aart Bik31883642016-06-06 15:02:44 -07004683 << " to not be static";
4684 return nullptr;
4685 }
4686
4687 return field;
Ian Rogersd81871c2011-10-03 13:57:23 -07004688}
4689
Andreas Gampe896df402014-10-20 22:25:29 -07004690template <MethodVerifier::FieldAccessType kAccType>
4691void MethodVerifier::VerifyISFieldAccess(const Instruction* inst, const RegType& insn_type,
4692 bool is_primitive, bool is_static) {
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00004693 uint32_t field_idx = GetFieldIdxOfFieldAccess(inst, is_static);
Mathieu Chartierc7853442015-03-27 14:35:38 -07004694 ArtField* field;
Ian Rogersb94a27b2011-10-26 00:33:41 -07004695 if (is_static) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07004696 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07004697 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004698 const RegType& object_type = work_line_->GetRegisterType(this, inst->VRegB_22c());
David Brazdil68b5c0b2016-01-19 14:25:29 +00004699
4700 // One is not allowed to access fields on uninitialized references, except to write to
4701 // fields in the constructor (before calling another constructor).
4702 // GetInstanceField does an assignability check which will fail for uninitialized types.
4703 // We thus modify the type if the uninitialized reference is a "this" reference (this also
4704 // checks at the same time that we're verifying a constructor).
4705 bool should_adjust = (kAccType == FieldAccessType::kAccPut) &&
4706 object_type.IsUninitializedThisReference();
4707 const RegType& adjusted_type = should_adjust
4708 ? GetRegTypeCache()->FromUninitialized(object_type)
4709 : object_type;
4710 field = GetInstanceField(adjusted_type, field_idx);
Andreas Gampe896df402014-10-20 22:25:29 -07004711 if (UNLIKELY(have_pending_hard_failure_)) {
4712 return;
4713 }
Alex Light4a2c8fc2016-02-12 11:01:54 -08004714 if (should_adjust) {
4715 if (field == nullptr) {
4716 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "Might be accessing a superclass instance field prior "
4717 << "to the superclass being initialized in "
David Sehr709b0702016-10-13 09:12:37 -07004718 << dex_file_->PrettyMethod(dex_method_idx_);
Alex Light4a2c8fc2016-02-12 11:01:54 -08004719 } else if (field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
4720 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "cannot access superclass instance field "
David Sehr709b0702016-10-13 09:12:37 -07004721 << field->PrettyField() << " of a not fully initialized "
Alex Light4a2c8fc2016-02-12 11:01:54 -08004722 << "object within the context of "
David Sehr709b0702016-10-13 09:12:37 -07004723 << dex_file_->PrettyMethod(dex_method_idx_);
Alex Light4a2c8fc2016-02-12 11:01:54 -08004724 return;
4725 }
4726 }
Ian Rogersb94a27b2011-10-26 00:33:41 -07004727 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00004728 const RegType* field_type = nullptr;
Ian Rogers7b078e82014-09-10 14:44:24 -07004729 if (field != nullptr) {
Andreas Gampe896df402014-10-20 22:25:29 -07004730 if (kAccType == FieldAccessType::kAccPut) {
4731 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
David Sehr709b0702016-10-13 09:12:37 -07004732 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << field->PrettyField()
Andreas Gampe896df402014-10-20 22:25:29 -07004733 << " from other class " << GetDeclaringClass();
Aart Bikc2bc2652016-05-23 14:58:49 -07004734 // Keep hunting for possible hard fails.
Andreas Gampe896df402014-10-20 22:25:29 -07004735 }
4736 }
4737
Mathieu Chartier3398c782016-09-30 10:27:43 -07004738 ObjPtr<mirror::Class> field_type_class =
Vladimir Marko208f6702017-12-08 12:00:50 +00004739 can_load_classes_ ? field->ResolveType() : field->LookupResolvedType();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004740 if (field_type_class != nullptr) {
Mathieu Chartier3398c782016-09-30 10:27:43 -07004741 field_type = &FromClass(field->GetTypeDescriptor(),
Vladimir Markobcf17522018-06-01 13:14:32 +01004742 field_type_class,
Andreas Gampef23f33d2015-06-23 14:18:17 -07004743 field_type_class->CannotBeAssignedFromOtherTypes());
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08004744 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004745 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
4746 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004747 }
David Brazdil2bb2fbd2018-11-13 18:24:26 +00004748 } else if (IsSdkVersionSetAndAtLeast(api_level_, SdkVersion::kP)) {
Andreas Gampe7da4c402017-08-25 11:30:48 -07004749 // If we don't have the field (it seems we failed resolution) and this is a PUT, we need to
4750 // redo verification at runtime as the field may be final, unless the field id shows it's in
4751 // the same class.
4752 //
4753 // For simplicity, it is OK to not distinguish compile-time vs runtime, and post this an
4754 // ACCESS_FIELD failure at runtime. This has the same effect as NO_FIELD - punting the class
4755 // to the access-checks interpreter.
4756 //
4757 // Note: see b/34966607. This and above may be changed in the future.
4758 if (kAccType == FieldAccessType::kAccPut) {
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08004759 const dex::FieldId& field_id = dex_file_->GetFieldId(field_idx);
Andreas Gampe7da4c402017-08-25 11:30:48 -07004760 const char* field_class_descriptor = dex_file_->GetFieldDeclaringClassDescriptor(field_id);
4761 const RegType* field_class_type = &reg_types_.FromDescriptor(GetClassLoader(),
4762 field_class_descriptor,
4763 false);
4764 if (!field_class_type->Equals(GetDeclaringClass())) {
4765 Fail(VERIFY_ERROR_ACCESS_FIELD) << "could not check field put for final field modify of "
4766 << field_class_descriptor
4767 << "."
4768 << dex_file_->GetFieldName(field_id)
4769 << " from other class "
4770 << GetDeclaringClass();
4771 }
4772 }
Ian Rogers0d604842012-04-16 14:50:24 -07004773 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004774 if (field_type == nullptr) {
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08004775 const dex::FieldId& field_id = dex_file_->GetFieldId(field_idx);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004776 const char* descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
Mathieu Chartierbf99f772014-08-23 16:37:27 -07004777 field_type = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004778 }
Sebastien Hertz757b3042014-03-28 14:34:28 +01004779 DCHECK(field_type != nullptr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02004780 const uint32_t vregA = (is_static) ? inst->VRegA_21c() : inst->VRegA_22c();
Andreas Gampe896df402014-10-20 22:25:29 -07004781 static_assert(kAccType == FieldAccessType::kAccPut || kAccType == FieldAccessType::kAccGet,
4782 "Unexpected third access type");
4783 if (kAccType == FieldAccessType::kAccPut) {
4784 // sput or iput.
4785 if (is_primitive) {
4786 VerifyPrimitivePut(*field_type, insn_type, vregA);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004787 } else {
David Brazdilca3c8c32016-09-06 14:04:48 +01004788 if (!insn_type.IsAssignableFrom(*field_type, this)) {
Vladimir Marko414000e2015-06-23 17:45:21 +01004789 // If the field type is not a reference, this is a global failure rather than
4790 // a class change failure as the instructions and the descriptors for the type
4791 // should have been consistent within the same file at compile time.
4792 VerifyError error = field_type->IsReferenceTypes() ? VERIFY_ERROR_BAD_CLASS_SOFT
4793 : VERIFY_ERROR_BAD_CLASS_HARD;
David Sehr709b0702016-10-13 09:12:37 -07004794 Fail(error) << "expected field " << ArtField::PrettyField(field)
Vladimir Marko414000e2015-06-23 17:45:21 +01004795 << " to be compatible with type '" << insn_type
4796 << "' but found type '" << *field_type
4797 << "' in put-object";
Andreas Gampe896df402014-10-20 22:25:29 -07004798 return;
4799 }
4800 work_line_->VerifyRegisterType(this, vregA, *field_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004801 }
Andreas Gampe896df402014-10-20 22:25:29 -07004802 } else if (kAccType == FieldAccessType::kAccGet) {
4803 // sget or iget.
4804 if (is_primitive) {
4805 if (field_type->Equals(insn_type) ||
4806 (field_type->IsFloat() && insn_type.IsInteger()) ||
4807 (field_type->IsDouble() && insn_type.IsLong())) {
4808 // expected that read is of the correct primitive type or that int reads are reading
4809 // floats or long reads are reading doubles
4810 } else {
4811 // This is a global failure rather than a class change failure as the instructions and
4812 // the descriptors for the type should have been consistent within the same file at
4813 // compile time
David Sehr709b0702016-10-13 09:12:37 -07004814 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << ArtField::PrettyField(field)
Andreas Gampe896df402014-10-20 22:25:29 -07004815 << " to be of type '" << insn_type
4816 << "' but found type '" << *field_type << "' in get";
4817 return;
4818 }
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08004819 } else {
David Brazdilca3c8c32016-09-06 14:04:48 +01004820 if (!insn_type.IsAssignableFrom(*field_type, this)) {
Vladimir Marko414000e2015-06-23 17:45:21 +01004821 // If the field type is not a reference, this is a global failure rather than
4822 // a class change failure as the instructions and the descriptors for the type
4823 // should have been consistent within the same file at compile time.
4824 VerifyError error = field_type->IsReferenceTypes() ? VERIFY_ERROR_BAD_CLASS_SOFT
4825 : VERIFY_ERROR_BAD_CLASS_HARD;
David Sehr709b0702016-10-13 09:12:37 -07004826 Fail(error) << "expected field " << ArtField::PrettyField(field)
Vladimir Marko414000e2015-06-23 17:45:21 +01004827 << " to be compatible with type '" << insn_type
4828 << "' but found type '" << *field_type
4829 << "' in get-object";
Andreas Gampe890da292015-07-06 17:20:18 -07004830 if (error != VERIFY_ERROR_BAD_CLASS_HARD) {
Andreas Gampead238ce2015-08-24 21:13:08 -07004831 work_line_->SetRegisterType<LockOp::kClear>(this, vregA, reg_types_.Conflict());
Andreas Gampe890da292015-07-06 17:20:18 -07004832 }
Andreas Gampe896df402014-10-20 22:25:29 -07004833 return;
4834 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004835 }
Andreas Gampe896df402014-10-20 22:25:29 -07004836 if (!field_type->IsLowHalf()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07004837 work_line_->SetRegisterType<LockOp::kClear>(this, vregA, *field_type);
Andreas Gampe896df402014-10-20 22:25:29 -07004838 } else {
4839 work_line_->SetRegisterTypeWide(this, vregA, *field_type, field_type->HighHalf(&reg_types_));
4840 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07004841 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004842 LOG(FATAL) << "Unexpected case.";
Ian Rogersd81871c2011-10-03 13:57:23 -07004843 }
4844}
4845
Ian Rogers776ac1f2012-04-13 23:36:36 -07004846bool MethodVerifier::CheckNotMoveException(const uint16_t* insns, int insn_idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004847 if ((insns[insn_idx] & 0xff) == Instruction::MOVE_EXCEPTION) {
jeffhaod5347e02012-03-22 17:25:05 -07004848 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-exception";
Ian Rogersd81871c2011-10-03 13:57:23 -07004849 return false;
4850 }
4851 return true;
4852}
4853
Stephen Kyle9bc61992014-09-22 13:53:15 +01004854bool MethodVerifier::CheckNotMoveResult(const uint16_t* insns, int insn_idx) {
4855 if (((insns[insn_idx] & 0xff) >= Instruction::MOVE_RESULT) &&
4856 ((insns[insn_idx] & 0xff) <= Instruction::MOVE_RESULT_OBJECT)) {
4857 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-result*";
4858 return false;
4859 }
4860 return true;
4861}
4862
4863bool MethodVerifier::CheckNotMoveExceptionOrMoveResult(const uint16_t* insns, int insn_idx) {
4864 return (CheckNotMoveException(insns, insn_idx) && CheckNotMoveResult(insns, insn_idx));
4865}
4866
Ian Rogersebbdd872014-07-07 23:53:08 -07004867bool MethodVerifier::UpdateRegisters(uint32_t next_insn, RegisterLine* merge_line,
4868 bool update_merge_line) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004869 bool changed = true;
4870 RegisterLine* target_line = reg_table_.GetLine(next_insn);
Mathieu Chartierde40d472015-10-15 17:47:48 -07004871 if (!GetInstructionFlags(next_insn).IsVisitedOrChanged()) {
jeffhaobdb76512011-09-07 11:43:16 -07004872 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07004873 * We haven't processed this instruction before, and we haven't touched the registers here, so
4874 * there's nothing to "merge". Copy the registers over and mark it as changed. (This is the
4875 * only way a register can transition out of "unknown", so this is not just an optimization.)
jeffhaobdb76512011-09-07 11:43:16 -07004876 */
Andreas Gampea727e372015-08-25 09:22:37 -07004877 target_line->CopyFromLine(merge_line);
Mathieu Chartierde40d472015-10-15 17:47:48 -07004878 if (GetInstructionFlags(next_insn).IsReturn()) {
Jeff Haob24b4a72013-07-31 13:47:31 -07004879 // Verify that the monitor stack is empty on return.
Andreas Gampea727e372015-08-25 09:22:37 -07004880 merge_line->VerifyMonitorStackEmpty(this);
4881
Ian Rogersb8c78592013-07-25 23:52:52 +00004882 // For returns we only care about the operand to the return, all other registers are dead.
4883 // Initialize them as conflicts so they don't add to GC and deoptimization information.
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08004884 const Instruction* ret_inst = &code_item_accessor_.InstructionAt(next_insn);
Andreas Gampea727e372015-08-25 09:22:37 -07004885 AdjustReturnLine(this, ret_inst, target_line);
Aart Bik31883642016-06-06 15:02:44 -07004886 // Directly bail if a hard failure was found.
Aart Bikb0526322016-06-01 14:06:00 -07004887 if (have_pending_hard_failure_) {
4888 return false;
4889 }
Ian Rogersb8c78592013-07-25 23:52:52 +00004890 }
jeffhaobdb76512011-09-07 11:43:16 -07004891 } else {
Mathieu Chartier361e04a2016-02-16 14:06:35 -08004892 RegisterLineArenaUniquePtr copy;
Andreas Gampe92d77202017-12-06 20:49:00 -08004893 if (UNLIKELY(VLOG_IS_ON(verifier_debug))) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07004894 copy.reset(RegisterLine::Create(target_line->NumRegs(), this));
Ian Rogers7b0c5b42012-02-16 15:29:07 -08004895 copy->CopyFromLine(target_line);
4896 }
Ian Rogers7b078e82014-09-10 14:44:24 -07004897 changed = target_line->MergeRegisters(this, merge_line);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004898 if (have_pending_hard_failure_) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004899 return false;
jeffhaobdb76512011-09-07 11:43:16 -07004900 }
Andreas Gampe92d77202017-12-06 20:49:00 -08004901 if (UNLIKELY(VLOG_IS_ON(verifier_debug)) && changed) {
Elliott Hughes398f64b2012-03-26 18:05:48 -07004902 LogVerifyInfo() << "Merging at [" << reinterpret_cast<void*>(work_insn_idx_) << "]"
Elliott Hughesc073b072012-05-24 19:29:17 -07004903 << " to [" << reinterpret_cast<void*>(next_insn) << "]: " << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07004904 << copy->Dump(this) << " MERGE\n"
4905 << merge_line->Dump(this) << " ==\n"
4906 << target_line->Dump(this) << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07004907 }
Ian Rogersebbdd872014-07-07 23:53:08 -07004908 if (update_merge_line && changed) {
4909 merge_line->CopyFromLine(target_line);
4910 }
jeffhaobdb76512011-09-07 11:43:16 -07004911 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004912 if (changed) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07004913 GetInstructionFlags(next_insn).SetChanged();
jeffhaobdb76512011-09-07 11:43:16 -07004914 }
4915 return true;
4916}
4917
Ian Rogers7b3ddd22013-02-21 15:19:52 -08004918InstructionFlags* MethodVerifier::CurrentInsnFlags() {
Mathieu Chartierde40d472015-10-15 17:47:48 -07004919 return &GetInstructionFlags(work_insn_idx_);
Ian Rogers776ac1f2012-04-13 23:36:36 -07004920}
4921
Ian Rogersd8f69b02014-09-10 21:43:52 +00004922const RegType& MethodVerifier::GetMethodReturnType() {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004923 if (return_type_ == nullptr) {
Nicolas Geoffrayb041a402017-11-13 15:16:22 +00004924 if (method_being_verified_ != nullptr) {
Vladimir Markob45528c2017-07-27 14:14:28 +01004925 ObjPtr<mirror::Class> return_type_class = can_load_classes_
Nicolas Geoffrayb041a402017-11-13 15:16:22 +00004926 ? method_being_verified_->ResolveReturnType()
4927 : method_being_verified_->LookupResolvedReturnType();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004928 if (return_type_class != nullptr) {
Nicolas Geoffrayb041a402017-11-13 15:16:22 +00004929 return_type_ = &FromClass(method_being_verified_->GetReturnTypeDescriptor(),
Vladimir Markobcf17522018-06-01 13:14:32 +01004930 return_type_class,
Andreas Gampef23f33d2015-06-23 14:18:17 -07004931 return_type_class->CannotBeAssignedFromOtherTypes());
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004932 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004933 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
4934 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004935 }
4936 }
4937 if (return_type_ == nullptr) {
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08004938 const dex::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
4939 const dex::ProtoId& proto_id = dex_file_->GetMethodPrototype(method_id);
Andreas Gampea5b09a62016-11-17 15:21:22 -08004940 dex::TypeIndex return_type_idx = proto_id.return_type_idx_;
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004941 const char* descriptor = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(return_type_idx));
Mathieu Chartierbf99f772014-08-23 16:37:27 -07004942 return_type_ = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004943 }
4944 }
4945 return *return_type_;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004946}
4947
Ian Rogersd8f69b02014-09-10 21:43:52 +00004948const RegType& MethodVerifier::GetDeclaringClass() {
Ian Rogers7b078e82014-09-10 14:44:24 -07004949 if (declaring_class_ == nullptr) {
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08004950 const dex::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
Brian Carlstrom93c33962013-07-26 10:37:43 -07004951 const char* descriptor
4952 = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(method_id.class_idx_));
Nicolas Geoffrayb041a402017-11-13 15:16:22 +00004953 if (method_being_verified_ != nullptr) {
Vladimir Marko2cffc5d2018-05-29 15:40:56 +01004954 ObjPtr<mirror::Class> klass = method_being_verified_->GetDeclaringClass();
Mathieu Chartierde40d472015-10-15 17:47:48 -07004955 declaring_class_ = &FromClass(descriptor, klass, klass->CannotBeAssignedFromOtherTypes());
Ian Rogers637c65b2013-05-31 11:46:00 -07004956 } else {
Mathieu Chartierbf99f772014-08-23 16:37:27 -07004957 declaring_class_ = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers637c65b2013-05-31 11:46:00 -07004958 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07004959 }
Ian Rogers637c65b2013-05-31 11:46:00 -07004960 return *declaring_class_;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004961}
4962
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004963std::vector<int32_t> MethodVerifier::DescribeVRegs(uint32_t dex_pc) {
4964 RegisterLine* line = reg_table_.GetLine(dex_pc);
Sebastien Hertzaa0c00c2014-03-14 17:58:54 +01004965 DCHECK(line != nullptr) << "No register line at DEX pc " << StringPrintf("0x%x", dex_pc);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004966 std::vector<int32_t> result;
4967 for (size_t i = 0; i < line->NumRegs(); ++i) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004968 const RegType& type = line->GetRegisterType(this, i);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004969 if (type.IsConstant()) {
4970 result.push_back(type.IsPreciseConstant() ? kConstant : kImpreciseConstant);
Ian Rogers7b078e82014-09-10 14:44:24 -07004971 const ConstantType* const_val = down_cast<const ConstantType*>(&type);
4972 result.push_back(const_val->ConstantValue());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004973 } else if (type.IsConstantLo()) {
4974 result.push_back(type.IsPreciseConstantLo() ? kConstant : kImpreciseConstant);
Ian Rogers7b078e82014-09-10 14:44:24 -07004975 const ConstantType* const_val = down_cast<const ConstantType*>(&type);
4976 result.push_back(const_val->ConstantValueLo());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004977 } else if (type.IsConstantHi()) {
4978 result.push_back(type.IsPreciseConstantHi() ? kConstant : kImpreciseConstant);
Ian Rogers7b078e82014-09-10 14:44:24 -07004979 const ConstantType* const_val = down_cast<const ConstantType*>(&type);
4980 result.push_back(const_val->ConstantValueHi());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004981 } else if (type.IsIntegralTypes()) {
4982 result.push_back(kIntVReg);
4983 result.push_back(0);
4984 } else if (type.IsFloat()) {
4985 result.push_back(kFloatVReg);
4986 result.push_back(0);
4987 } else if (type.IsLong()) {
4988 result.push_back(kLongLoVReg);
4989 result.push_back(0);
4990 result.push_back(kLongHiVReg);
4991 result.push_back(0);
4992 ++i;
4993 } else if (type.IsDouble()) {
4994 result.push_back(kDoubleLoVReg);
4995 result.push_back(0);
4996 result.push_back(kDoubleHiVReg);
4997 result.push_back(0);
4998 ++i;
4999 } else if (type.IsUndefined() || type.IsConflict() || type.IsHighHalf()) {
5000 result.push_back(kUndefined);
5001 result.push_back(0);
5002 } else {
Ian Rogers7b3ddd22013-02-21 15:19:52 -08005003 CHECK(type.IsNonZeroReferenceTypes());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08005004 result.push_back(kReferenceVReg);
5005 result.push_back(0);
5006 }
5007 }
5008 return result;
5009}
5010
Ian Rogersd8f69b02014-09-10 21:43:52 +00005011const RegType& MethodVerifier::DetermineCat1Constant(int32_t value, bool precise) {
Sebastien Hertz849600b2013-12-20 10:28:08 +01005012 if (precise) {
5013 // Precise constant type.
5014 return reg_types_.FromCat1Const(value, true);
5015 } else {
5016 // Imprecise constant type.
5017 if (value < -32768) {
5018 return reg_types_.IntConstant();
5019 } else if (value < -128) {
5020 return reg_types_.ShortConstant();
5021 } else if (value < 0) {
5022 return reg_types_.ByteConstant();
5023 } else if (value == 0) {
5024 return reg_types_.Zero();
5025 } else if (value == 1) {
5026 return reg_types_.One();
5027 } else if (value < 128) {
5028 return reg_types_.PosByteConstant();
5029 } else if (value < 32768) {
5030 return reg_types_.PosShortConstant();
5031 } else if (value < 65536) {
5032 return reg_types_.CharConstant();
5033 } else {
5034 return reg_types_.IntConstant();
5035 }
5036 }
5037}
5038
Elliott Hughes0a1038b2012-06-14 16:24:17 -07005039void MethodVerifier::Init() {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08005040 art::verifier::RegTypeCache::Init();
Brian Carlstrome7d856b2012-01-11 18:10:55 -08005041}
5042
Elliott Hughes0a1038b2012-06-14 16:24:17 -07005043void MethodVerifier::Shutdown() {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08005044 verifier::RegTypeCache::ShutDown();
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08005045}
jeffhaod1224c72012-02-29 13:43:08 -08005046
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -07005047void MethodVerifier::VisitStaticRoots(RootVisitor* visitor) {
5048 RegTypeCache::VisitStaticRoots(visitor);
Mathieu Chartier7c438b12014-09-12 17:01:24 -07005049}
5050
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -07005051void MethodVerifier::VisitRoots(RootVisitor* visitor, const RootInfo& root_info) {
5052 reg_types_.VisitRoots(visitor, root_info);
Mathieu Chartierc528dba2013-11-26 12:00:11 -08005053}
5054
Andreas Gampef23f33d2015-06-23 14:18:17 -07005055const RegType& MethodVerifier::FromClass(const char* descriptor,
Vladimir Marko2cffc5d2018-05-29 15:40:56 +01005056 ObjPtr<mirror::Class> klass,
Andreas Gampef23f33d2015-06-23 14:18:17 -07005057 bool precise) {
5058 DCHECK(klass != nullptr);
5059 if (precise && !klass->IsInstantiable() && !klass->IsPrimitive()) {
5060 Fail(VerifyError::VERIFY_ERROR_NO_CLASS) << "Could not create precise reference for "
5061 << "non-instantiable klass " << descriptor;
5062 precise = false;
5063 }
5064 return reg_types_.FromClass(descriptor, klass, precise);
5065}
5066
Ian Rogersd81871c2011-10-03 13:57:23 -07005067} // namespace verifier
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07005068} // namespace art