blob: b07001e5957e78d8dcfc3f5285aae3f1ca79efba [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"
Andreas Gampe57943812017-12-06 21:39:13 -080028#include "base/logging.h" // For VLOG.
Ian Rogers637c65b2013-05-31 11:46:00 -070029#include "base/mutex-inl.h"
Vladimir Marko637ee0b2015-09-04 12:47:41 +010030#include "base/stl_util.h"
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -080031#include "base/systrace.h"
Vladimir Marko80afd022015-05-19 18:08:00 +010032#include "base/time_utils.h"
David Sehrc431b9d2018-03-02 12:01:51 -080033#include "base/utils.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070034#include "class_linker.h"
Vladimir Marko2b5eaa22013-12-13 13:59:30 +000035#include "compiler_callbacks.h"
David Sehrb2ec9f52018-02-21 13:20:31 -080036#include "dex/descriptors_names.h"
David Sehr9e734c72018-01-04 17:56:19 -080037#include "dex/dex_file-inl.h"
38#include "dex/dex_file_exception_helpers.h"
39#include "dex/dex_instruction-inl.h"
40#include "dex/dex_instruction_utils.h"
Alex Lighteb7c1442015-08-31 13:17:42 -070041#include "experimental_flags.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070042#include "gc/accounting/card_table-inl.h"
Andreas Gampe6d7abbd2017-04-24 13:19:09 -070043#include "handle_scope-inl.h"
Ian Rogers2bcb4a42012-11-08 10:39:18 -080044#include "indenter.h"
Ian Rogers84fa0742011-10-25 18:13:30 -070045#include "intern_table.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080046#include "mirror/class-inl.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070047#include "mirror/class.h"
Ian Rogers39ebcb82013-05-30 16:57:23 -070048#include "mirror/dex_cache-inl.h"
Orion Hodsoncfa325e2016-10-13 10:25:54 +010049#include "mirror/method_handle_impl.h"
Orion Hodson2e599942017-09-22 16:17:41 +010050#include "mirror/method_type.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080051#include "mirror/object-inl.h"
52#include "mirror/object_array-inl.h"
Orion Hodsonfe92d122018-01-02 10:45:17 +000053#include "mirror/var_handle.h"
Ian Rogers7b078e82014-09-10 14:44:24 -070054#include "reg_type-inl.h"
Ian Rogers39ebcb82013-05-30 16:57:23 -070055#include "register_line-inl.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070056#include "runtime.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070057#include "scoped_thread_state_change-inl.h"
Andreas Gampe513061a2017-06-01 09:17:34 -070058#include "stack.h"
Nicolas Geoffrayb041a402017-11-13 15:16:22 +000059#include "vdex_file.h"
Andreas Gampe6d7abbd2017-04-24 13:19:09 -070060#include "verifier_compiler_binding.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070061#include "verifier_deps.h"
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070062
63namespace art {
Ian Rogersd81871c2011-10-03 13:57:23 -070064namespace verifier {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070065
Andreas Gampe46ee31b2016-12-14 10:11:49 -080066using android::base::StringPrintf;
67
Mathieu Chartier8e219ae2014-08-19 14:29:46 -070068static constexpr bool kTimeVerifyMethod = !kIsDebugBuild;
Ian Rogers2c8a8572011-10-24 17:11:36 -070069
Andreas Gampeebf850c2015-08-14 15:37:35 -070070// On VLOG(verifier), should we dump the whole state when we run into a hard failure?
71static constexpr bool kDumpRegLinesOnHardFailureIfVLOG = true;
72
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -080073// We print a warning blurb about "dx --no-optimize" when we find monitor-locking issues. Make
74// sure we only print this once.
75static bool gPrintedDxMonitorText = false;
76
Vladimir Marko69d310e2017-10-09 14:12:23 +010077PcToRegisterLineTable::PcToRegisterLineTable(ScopedArenaAllocator& allocator)
78 : register_lines_(allocator.Adapter(kArenaAllocVerifier)) {}
Mathieu Chartierde40d472015-10-15 17:47:48 -070079
Ian Rogers7b3ddd22013-02-21 15:19:52 -080080void PcToRegisterLineTable::Init(RegisterTrackingMode mode, InstructionFlags* flags,
Ian Rogersd81871c2011-10-03 13:57:23 -070081 uint32_t insns_size, uint16_t registers_size,
Ian Rogers776ac1f2012-04-13 23:36:36 -070082 MethodVerifier* verifier) {
Ian Rogersd81871c2011-10-03 13:57:23 -070083 DCHECK_GT(insns_size, 0U);
Mathieu Chartierde40d472015-10-15 17:47:48 -070084 register_lines_.resize(insns_size);
Ian Rogersd81871c2011-10-03 13:57:23 -070085 for (uint32_t i = 0; i < insns_size; i++) {
86 bool interesting = false;
87 switch (mode) {
88 case kTrackRegsAll:
89 interesting = flags[i].IsOpcode();
90 break;
Sameer Abu Asal02c42232013-04-30 12:09:45 -070091 case kTrackCompilerInterestPoints:
Brian Carlstrom02c8cc62013-07-18 15:54:44 -070092 interesting = flags[i].IsCompileTimeInfoPoint() || flags[i].IsBranchTarget();
Ian Rogersd81871c2011-10-03 13:57:23 -070093 break;
94 case kTrackRegsBranches:
95 interesting = flags[i].IsBranchTarget();
96 break;
97 default:
98 break;
99 }
100 if (interesting) {
Mathieu Chartierde40d472015-10-15 17:47:48 -0700101 register_lines_[i].reset(RegisterLine::Create(registers_size, verifier));
Ian Rogersd0fbd852013-09-24 18:17:04 -0700102 }
103 }
104}
105
Mathieu Chartierde40d472015-10-15 17:47:48 -0700106PcToRegisterLineTable::~PcToRegisterLineTable() {}
Ian Rogersd81871c2011-10-03 13:57:23 -0700107
Andreas Gampe7c038102014-10-27 20:08:46 -0700108// Note: returns true on failure.
Mathieu Chartierc2a2bda2018-03-12 11:21:52 -0700109inline bool MethodVerifier::FailOrAbort(bool condition,
110 const char* error_msg,
111 uint32_t work_insn_idx) {
Andreas Gampe7c038102014-10-27 20:08:46 -0700112 if (kIsDebugBuild) {
Andreas Gampef8f36c22016-11-19 14:51:52 -0800113 // In a debug build, abort if the error condition is wrong. Only warn if
114 // we are already aborting (as this verification is likely run to print
115 // lock information).
116 if (LIKELY(gAborting == 0)) {
Mathieu Chartierc2a2bda2018-03-12 11:21:52 -0700117 DCHECK(condition) << error_msg << work_insn_idx << " "
118 << dex_file_->PrettyMethod(dex_method_idx_);
Andreas Gampef8f36c22016-11-19 14:51:52 -0800119 } else {
120 if (!condition) {
121 LOG(ERROR) << error_msg << work_insn_idx;
Mathieu Chartierc2a2bda2018-03-12 11:21:52 -0700122 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << error_msg << work_insn_idx;
Andreas Gampef8f36c22016-11-19 14:51:52 -0800123 return true;
124 }
125 }
Andreas Gampe7c038102014-10-27 20:08:46 -0700126 } else {
127 // In a non-debug build, just fail the class.
128 if (!condition) {
Mathieu Chartierc2a2bda2018-03-12 11:21:52 -0700129 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << error_msg << work_insn_idx;
Andreas Gampe7c038102014-10-27 20:08:46 -0700130 return true;
131 }
132 }
133
134 return false;
135}
136
Stephen Kyle7e541c92014-12-17 17:10:02 +0000137static void SafelyMarkAllRegistersAsConflicts(MethodVerifier* verifier, RegisterLine* reg_line) {
Andreas Gampef10b6e12015-08-12 10:48:12 -0700138 if (verifier->IsInstanceConstructor()) {
Stephen Kyle7e541c92014-12-17 17:10:02 +0000139 // Before we mark all regs as conflicts, check that we don't have an uninitialized this.
140 reg_line->CheckConstructorReturn(verifier);
141 }
142 reg_line->MarkAllRegistersAsConflicts(verifier);
143}
144
Andreas Gampe6d7abbd2017-04-24 13:19:09 -0700145FailureKind MethodVerifier::VerifyClass(Thread* self,
146 mirror::Class* klass,
147 CompilerCallbacks* callbacks,
148 bool allow_soft_failures,
149 HardFailLogMode log_level,
150 std::string* error) {
jeffhaobdb76512011-09-07 11:43:16 -0700151 if (klass->IsVerified()) {
Andreas Gampe6d7abbd2017-04-24 13:19:09 -0700152 return FailureKind::kNoFailure;
jeffhaobdb76512011-09-07 11:43:16 -0700153 }
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800154 bool early_failure = false;
155 std::string failure_message;
Mathieu Chartierf8322842014-05-16 10:59:25 -0700156 const DexFile& dex_file = klass->GetDexFile();
157 const DexFile::ClassDef* class_def = klass->GetClassDef();
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800158 mirror::Class* super = klass->GetSuperClass();
Ian Rogers1ff3c982014-08-12 02:30:58 -0700159 std::string temp;
Ian Rogers7b078e82014-09-10 14:44:24 -0700160 if (super == nullptr && strcmp("Ljava/lang/Object;", klass->GetDescriptor(&temp)) != 0) {
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800161 early_failure = true;
162 failure_message = " that has no super class";
Ian Rogers7b078e82014-09-10 14:44:24 -0700163 } else if (super != nullptr && super->IsFinal()) {
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800164 early_failure = true;
David Sehr709b0702016-10-13 09:12:37 -0700165 failure_message = " that attempts to sub-class final class " + super->PrettyDescriptor();
Ian Rogers7b078e82014-09-10 14:44:24 -0700166 } else if (class_def == nullptr) {
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800167 early_failure = true;
168 failure_message = " that isn't present in dex file " + dex_file.GetLocation();
169 }
170 if (early_failure) {
David Sehr709b0702016-10-13 09:12:37 -0700171 *error = "Verifier rejected class " + klass->PrettyDescriptor() + failure_message;
Andreas Gampe53e32d12015-12-09 21:03:23 -0800172 if (callbacks != nullptr) {
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800173 ClassReference ref(&dex_file, klass->GetDexClassDefIndex());
Andreas Gampe53e32d12015-12-09 21:03:23 -0800174 callbacks->ClassRejected(ref);
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800175 }
Andreas Gampe6d7abbd2017-04-24 13:19:09 -0700176 return FailureKind::kHardFailure;
jeffhaobdb76512011-09-07 11:43:16 -0700177 }
Ian Rogers7b078e82014-09-10 14:44:24 -0700178 StackHandleScope<2> hs(self);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700179 Handle<mirror::DexCache> dex_cache(hs.NewHandle(klass->GetDexCache()));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700180 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(klass->GetClassLoader()));
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800181 return VerifyClass(self,
182 &dex_file,
183 dex_cache,
184 class_loader,
David Brazdil15fc7292016-09-02 14:13:18 +0100185 *class_def,
Andreas Gampe53e32d12015-12-09 21:03:23 -0800186 callbacks,
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800187 allow_soft_failures,
Andreas Gampe7fe30232016-03-25 16:58:00 -0700188 log_level,
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800189 error);
190}
191
192template <bool kDirect>
193static bool HasNextMethod(ClassDataItemIterator* it) {
194 return kDirect ? it->HasNextDirectMethod() : it->HasNextVirtualMethod();
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 Gampeec6e6c12015-11-05 20:39:56 -0800209template <bool kDirect>
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800210MethodVerifier::FailureData MethodVerifier::VerifyMethods(Thread* self,
211 ClassLinker* linker,
212 const DexFile* dex_file,
David Brazdil15fc7292016-09-02 14:13:18 +0100213 const DexFile::ClassDef& class_def,
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800214 ClassDataItemIterator* it,
215 Handle<mirror::DexCache> dex_cache,
216 Handle<mirror::ClassLoader> class_loader,
217 CompilerCallbacks* callbacks,
218 bool allow_soft_failures,
Andreas Gampe5fd66d02016-09-12 20:22:19 -0700219 HardFailLogMode log_level,
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800220 bool need_precise_constants,
221 std::string* error_string) {
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800222 DCHECK(it != nullptr);
223
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800224 MethodVerifier::FailureData failure_data;
225
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800226 int64_t previous_method_idx = -1;
227 while (HasNextMethod<kDirect>(it)) {
228 self->AllowThreadSuspension();
229 uint32_t method_idx = it->GetMemberIndex();
230 if (method_idx == previous_method_idx) {
231 // smali can create dex files with two encoded_methods sharing the same method_idx
232 // http://code.google.com/p/smali/issues/detail?id=119
233 it->Next();
234 continue;
235 }
236 previous_method_idx = method_idx;
David Brazdil15fc7292016-09-02 14:13:18 +0100237 InvokeType type = it->GetMethodInvokeType(class_def);
Vladimir Markoba118822017-06-12 15:41:56 +0100238 ArtMethod* method = linker->ResolveMethod<ClassLinker::ResolveMode::kNoChecks>(
Vladimir Marko89011192017-12-11 13:45:05 +0000239 method_idx, dex_cache, class_loader, /* referrer */ nullptr, type);
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800240 if (method == nullptr) {
241 DCHECK(self->IsExceptionPending());
242 // We couldn't resolve the method, but continue regardless.
243 self->ClearException();
244 } else {
245 DCHECK(method->GetDeclaringClassUnchecked() != nullptr) << type;
246 }
247 StackHandleScope<1> hs(self);
248 std::string hard_failure_msg;
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800249 MethodVerifier::FailureData result = VerifyMethod(self,
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800250 method_idx,
251 dex_file,
252 dex_cache,
253 class_loader,
254 class_def,
255 it->GetMethodCodeItem(),
256 method,
257 it->GetMethodAccessFlags(),
Andreas Gampe53e32d12015-12-09 21:03:23 -0800258 callbacks,
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800259 allow_soft_failures,
Andreas Gampe7fe30232016-03-25 16:58:00 -0700260 log_level,
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800261 need_precise_constants,
262 &hard_failure_msg);
Andreas Gampe6d7abbd2017-04-24 13:19:09 -0700263 if (result.kind == FailureKind::kHardFailure) {
264 if (failure_data.kind == FailureKind::kHardFailure) {
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800265 // If we logged an error before, we need a newline.
266 *error_string += "\n";
267 } else {
268 // If we didn't log a hard failure before, print the header of the message.
269 *error_string += "Verifier rejected class ";
David Brazdil15fc7292016-09-02 14:13:18 +0100270 *error_string += PrettyDescriptor(dex_file->GetClassDescriptor(class_def));
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800271 *error_string += ":";
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800272 }
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800273 *error_string += " ";
274 *error_string += hard_failure_msg;
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800275 }
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800276 failure_data.Merge(result);
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800277 it->Next();
278 }
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800279
280 return failure_data;
Shih-wei Liao371814f2011-10-27 16:52:10 -0700281}
282
Andreas Gampe6d7abbd2017-04-24 13:19:09 -0700283FailureKind MethodVerifier::VerifyClass(Thread* self,
284 const DexFile* dex_file,
285 Handle<mirror::DexCache> dex_cache,
286 Handle<mirror::ClassLoader> class_loader,
287 const DexFile::ClassDef& class_def,
288 CompilerCallbacks* callbacks,
289 bool allow_soft_failures,
290 HardFailLogMode log_level,
291 std::string* error) {
Andreas Gampec6548162017-12-08 12:15:22 -0800292 SCOPED_TRACE << "VerifyClass " << PrettyDescriptor(dex_file->GetClassDescriptor(class_def));
Andreas Gampe507cc6f2015-06-19 22:58:47 -0700293
294 // A class must not be abstract and final.
David Brazdil15fc7292016-09-02 14:13:18 +0100295 if ((class_def.access_flags_ & (kAccAbstract | kAccFinal)) == (kAccAbstract | kAccFinal)) {
Andreas Gampe507cc6f2015-06-19 22:58:47 -0700296 *error = "Verifier rejected class ";
David Brazdil15fc7292016-09-02 14:13:18 +0100297 *error += PrettyDescriptor(dex_file->GetClassDescriptor(class_def));
Andreas Gampe507cc6f2015-06-19 22:58:47 -0700298 *error += ": class is abstract and final.";
Andreas Gampe6d7abbd2017-04-24 13:19:09 -0700299 return FailureKind::kHardFailure;
Andreas Gampe507cc6f2015-06-19 22:58:47 -0700300 }
301
David Brazdil15fc7292016-09-02 14:13:18 +0100302 const uint8_t* class_data = dex_file->GetClassData(class_def);
Ian Rogers7b078e82014-09-10 14:44:24 -0700303 if (class_data == nullptr) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700304 // empty class, probably a marker interface
Andreas Gampe6d7abbd2017-04-24 13:19:09 -0700305 return FailureKind::kNoFailure;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700306 }
jeffhaof56197c2012-03-05 18:01:54 -0800307 ClassDataItemIterator it(*dex_file, class_data);
Mathieu Chartiere17cf242017-06-19 11:05:51 -0700308 it.SkipAllFields();
Ian Rogersad0b3a32012-04-16 14:50:24 -0700309 ClassLinker* linker = Runtime::Current()->GetClassLinker();
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800310 // Direct methods.
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800311 MethodVerifier::FailureData data1 = VerifyMethods<true>(self,
312 linker,
313 dex_file,
314 class_def,
315 &it,
316 dex_cache,
317 class_loader,
318 callbacks,
319 allow_soft_failures,
Andreas Gampe7fe30232016-03-25 16:58:00 -0700320 log_level,
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800321 false /* need precise constants */,
322 error);
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800323 // Virtual methods.
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800324 MethodVerifier::FailureData data2 = VerifyMethods<false>(self,
325 linker,
326 dex_file,
327 class_def,
328 &it,
329 dex_cache,
330 class_loader,
331 callbacks,
332 allow_soft_failures,
Andreas Gampe7fe30232016-03-25 16:58:00 -0700333 log_level,
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800334 false /* need precise constants */,
335 error);
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800336
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800337 data1.Merge(data2);
338
Andreas Gampe6d7abbd2017-04-24 13:19:09 -0700339 if (data1.kind == FailureKind::kNoFailure) {
340 return FailureKind::kNoFailure;
jeffhaof1e6b7c2012-06-05 18:33:30 -0700341 } else {
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800342 if ((data1.types & VERIFY_ERROR_LOCKING) != 0) {
343 // Print a warning about expected slow-down. Use a string temporary to print one contiguous
344 // warning.
345 std::string tmp =
346 StringPrintf("Class %s failed lock verification and will run slower.",
David Brazdil15fc7292016-09-02 14:13:18 +0100347 PrettyDescriptor(dex_file->GetClassDescriptor(class_def)).c_str());
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800348 if (!gPrintedDxMonitorText) {
349 tmp = tmp + "\nCommon causes for lock verification issues are non-optimized dex code\n"
350 "and incorrect proguard optimizations.";
351 gPrintedDxMonitorText = true;
352 }
353 LOG(WARNING) << tmp;
354 }
355 return data1.kind;
jeffhaof1e6b7c2012-06-05 18:33:30 -0700356 }
jeffhaof56197c2012-03-05 18:01:54 -0800357}
358
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800359static bool IsLargeMethod(const CodeItemDataAccessor& accessor) {
360 if (!accessor.HasCodeItem()) {
Andreas Gampe3c651fc2015-05-21 14:06:46 -0700361 return false;
362 }
363
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800364 uint16_t registers_size = accessor.RegistersSize();
365 uint32_t insns_size = accessor.InsnsSizeInCodeUnits();
Andreas Gampea4f5bf62015-05-18 20:50:12 -0700366
367 return registers_size * insns_size > 4*1024*1024;
368}
369
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800370MethodVerifier::FailureData MethodVerifier::VerifyMethod(Thread* self,
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800371 uint32_t method_idx,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800372 const DexFile* dex_file,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700373 Handle<mirror::DexCache> dex_cache,
374 Handle<mirror::ClassLoader> class_loader,
David Brazdil15fc7292016-09-02 14:13:18 +0100375 const DexFile::ClassDef& class_def,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800376 const DexFile::CodeItem* code_item,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700377 ArtMethod* method,
Jeff Haoee988952013-04-16 14:23:47 -0700378 uint32_t method_access_flags,
Andreas Gampe53e32d12015-12-09 21:03:23 -0800379 CompilerCallbacks* callbacks,
Ian Rogers46960fe2014-05-23 10:43:43 -0700380 bool allow_soft_failures,
Andreas Gampe5fd66d02016-09-12 20:22:19 -0700381 HardFailLogMode log_level,
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800382 bool need_precise_constants,
383 std::string* hard_failure_msg) {
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800384 MethodVerifier::FailureData result;
Mathieu Chartier8e219ae2014-08-19 14:29:46 -0700385 uint64_t start_ns = kTimeVerifyMethod ? NanoTime() : 0;
Ian Rogersc8982582012-09-07 16:53:25 -0700386
Andreas Gampebf9611f2016-03-25 16:58:00 -0700387 MethodVerifier verifier(self,
388 dex_file,
389 dex_cache,
390 class_loader,
391 class_def,
392 code_item,
393 method_idx,
394 method,
395 method_access_flags,
396 true /* can_load_classes */,
397 allow_soft_failures,
398 need_precise_constants,
399 false /* verify to dump */,
400 true /* allow_thread_suspension */);
Ian Rogers46960fe2014-05-23 10:43:43 -0700401 if (verifier.Verify()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700402 // Verification completed, however failures may be pending that didn't cause the verification
403 // to hard fail.
Ian Rogers46960fe2014-05-23 10:43:43 -0700404 CHECK(!verifier.have_pending_hard_failure_);
Andreas Gampe53e32d12015-12-09 21:03:23 -0800405
406 if (code_item != nullptr && callbacks != nullptr) {
407 // Let the interested party know that the method was verified.
408 callbacks->MethodVerified(&verifier);
409 }
410
Ian Rogers46960fe2014-05-23 10:43:43 -0700411 if (verifier.failures_.size() != 0) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -0700412 if (VLOG_IS_ON(verifier)) {
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800413 verifier.DumpFailures(VLOG_STREAM(verifier) << "Soft verification failures in "
David Sehr709b0702016-10-13 09:12:37 -0700414 << dex_file->PrettyMethod(method_idx) << "\n");
Anwar Ghuloum75a43f12013-08-13 17:22:14 -0700415 }
Andreas Gampe92d77202017-12-06 20:49:00 -0800416 if (VLOG_IS_ON(verifier_debug)) {
417 std::cout << "\n" << verifier.info_messages_.str();
418 verifier.Dump(std::cout);
419 }
Andreas Gampe6d7abbd2017-04-24 13:19:09 -0700420 result.kind = FailureKind::kSoftFailure;
Nicolas Geoffray250a3782016-04-20 16:27:53 +0100421 if (method != nullptr &&
422 !CanCompilerHandleVerificationFailure(verifier.encountered_failure_types_)) {
Alex Lightfcbafb32017-02-02 15:09:54 -0800423 method->SetDontCompile();
Nicolas Geoffray250a3782016-04-20 16:27:53 +0100424 }
425 }
Andreas Gampe56fdd0e2016-04-28 14:56:54 -0700426 if (method != nullptr) {
427 if (verifier.HasInstructionThatWillThrow()) {
Alex Lightfcbafb32017-02-02 15:09:54 -0800428 method->SetDontCompile();
Calin Juravle857f0582016-12-20 14:36:59 +0000429 if (Runtime::Current()->IsAotCompiler() &&
430 (callbacks != nullptr) && !callbacks->IsBootImage()) {
431 // When compiling apps, make HasInstructionThatWillThrow a soft error to trigger
432 // re-verification at runtime.
433 // The dead code after the throw is not verified and might be invalid. This may cause
434 // the JIT compiler to crash since it assumes that all the code is valid.
435 //
436 // There's a strong assumption that the entire boot image is verified and all its dex
437 // code is valid (even the dead and unverified one). As such this is done only for apps.
438 // (CompilerDriver DCHECKs in VerifyClassVisitor that methods from boot image are
439 // fully verified).
Andreas Gampe6d7abbd2017-04-24 13:19:09 -0700440 result.kind = FailureKind::kSoftFailure;
Calin Juravle857f0582016-12-20 14:36:59 +0000441 }
Andreas Gampe56fdd0e2016-04-28 14:56:54 -0700442 }
443 if ((verifier.encountered_failure_types_ & VerifyError::VERIFY_ERROR_LOCKING) != 0) {
Orion Hodsoncfcc9cf2017-09-29 15:07:27 +0100444 method->SetMustCountLocks();
Andreas Gampe56fdd0e2016-04-28 14:56:54 -0700445 }
jeffhaof56197c2012-03-05 18:01:54 -0800446 }
447 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700448 // Bad method data.
Ian Rogers46960fe2014-05-23 10:43:43 -0700449 CHECK_NE(verifier.failures_.size(), 0U);
Igor Murashkin4d7b75f2015-07-21 17:03:36 -0700450
451 if (UNLIKELY(verifier.have_pending_experimental_failure_)) {
452 // Failed due to being forced into interpreter. This is ok because
453 // we just want to skip verification.
Andreas Gampe6d7abbd2017-04-24 13:19:09 -0700454 result.kind = FailureKind::kSoftFailure;
Igor Murashkin4d7b75f2015-07-21 17:03:36 -0700455 } else {
456 CHECK(verifier.have_pending_hard_failure_);
Andreas Gampe7fe30232016-03-25 16:58:00 -0700457 if (VLOG_IS_ON(verifier)) {
Andreas Gampe5fd66d02016-09-12 20:22:19 -0700458 log_level = std::max(HardFailLogMode::kLogVerbose, log_level);
Andreas Gampe7fe30232016-03-25 16:58:00 -0700459 }
Andreas Gampe5fd66d02016-09-12 20:22:19 -0700460 if (log_level >= HardFailLogMode::kLogVerbose) {
461 LogSeverity severity;
462 switch (log_level) {
463 case HardFailLogMode::kLogVerbose:
464 severity = LogSeverity::VERBOSE;
465 break;
466 case HardFailLogMode::kLogWarning:
467 severity = LogSeverity::WARNING;
468 break;
469 case HardFailLogMode::kLogInternalFatal:
Andreas Gampe3fec9ac2016-09-13 10:47:28 -0700470 severity = LogSeverity::FATAL_WITHOUT_ABORT;
Andreas Gampe5fd66d02016-09-12 20:22:19 -0700471 break;
472 default:
473 LOG(FATAL) << "Unsupported log-level " << static_cast<uint32_t>(log_level);
474 UNREACHABLE();
475 }
Andreas Gampe3fec9ac2016-09-13 10:47:28 -0700476 verifier.DumpFailures(LOG_STREAM(severity) << "Verification error in "
David Sehr709b0702016-10-13 09:12:37 -0700477 << dex_file->PrettyMethod(method_idx)
Andreas Gampe3fec9ac2016-09-13 10:47:28 -0700478 << "\n");
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800479 }
480 if (hard_failure_msg != nullptr) {
481 CHECK(!verifier.failure_messages_.empty());
482 *hard_failure_msg =
483 verifier.failure_messages_[verifier.failure_messages_.size() - 1]->str();
484 }
Andreas Gampe6d7abbd2017-04-24 13:19:09 -0700485 result.kind = FailureKind::kHardFailure;
Andreas Gampe53e32d12015-12-09 21:03:23 -0800486
487 if (callbacks != nullptr) {
488 // Let the interested party know that we failed the class.
David Brazdil15fc7292016-09-02 14:13:18 +0100489 ClassReference ref(dex_file, dex_file->GetIndexForClassDef(class_def));
Andreas Gampe53e32d12015-12-09 21:03:23 -0800490 callbacks->ClassRejected(ref);
491 }
Igor Murashkin4d7b75f2015-07-21 17:03:36 -0700492 }
Andreas Gampe92d77202017-12-06 20:49:00 -0800493 if (VLOG_IS_ON(verifier) || VLOG_IS_ON(verifier_debug)) {
Ian Rogers46960fe2014-05-23 10:43:43 -0700494 std::cout << "\n" << verifier.info_messages_.str();
495 verifier.Dump(std::cout);
jeffhaof56197c2012-03-05 18:01:54 -0800496 }
jeffhaof56197c2012-03-05 18:01:54 -0800497 }
Mathieu Chartier8e219ae2014-08-19 14:29:46 -0700498 if (kTimeVerifyMethod) {
499 uint64_t duration_ns = NanoTime() - start_ns;
500 if (duration_ns > MsToNs(100)) {
David Sehr709b0702016-10-13 09:12:37 -0700501 LOG(WARNING) << "Verification of " << dex_file->PrettyMethod(method_idx)
Andreas Gampea4f5bf62015-05-18 20:50:12 -0700502 << " took " << PrettyDuration(duration_ns)
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800503 << (IsLargeMethod(verifier.CodeItem()) ? " (large method)" : "");
Mathieu Chartier8e219ae2014-08-19 14:29:46 -0700504 }
Ian Rogersc8982582012-09-07 16:53:25 -0700505 }
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800506 result.types = verifier.encountered_failure_types_;
Ian Rogersc8982582012-09-07 16:53:25 -0700507 return result;
jeffhaof56197c2012-03-05 18:01:54 -0800508}
509
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100510MethodVerifier* MethodVerifier::VerifyMethodAndDump(Thread* self,
511 VariableIndentationOutputStream* vios,
512 uint32_t dex_method_idx,
513 const DexFile* dex_file,
514 Handle<mirror::DexCache> dex_cache,
515 Handle<mirror::ClassLoader> class_loader,
David Brazdil15fc7292016-09-02 14:13:18 +0100516 const DexFile::ClassDef& class_def,
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100517 const DexFile::CodeItem* code_item,
518 ArtMethod* method,
519 uint32_t method_access_flags) {
Andreas Gampebf9611f2016-03-25 16:58:00 -0700520 MethodVerifier* verifier = new MethodVerifier(self,
521 dex_file,
522 dex_cache,
523 class_loader,
524 class_def,
525 code_item,
526 dex_method_idx,
527 method,
528 method_access_flags,
529 true /* can_load_classes */,
530 true /* allow_soft_failures */,
531 true /* need_precise_constants */,
532 true /* verify_to_dump */,
533 true /* allow_thread_suspension */);
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700534 verifier->Verify();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100535 verifier->DumpFailures(vios->Stream());
536 vios->Stream() << verifier->info_messages_.str();
Andreas Gampe5cbcde22014-09-16 14:59:49 -0700537 // Only dump and return if no hard failures. Otherwise the verifier may be not fully initialized
538 // and querying any info is dangerous/can abort.
539 if (verifier->have_pending_hard_failure_) {
540 delete verifier;
541 return nullptr;
542 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100543 verifier->Dump(vios);
Andreas Gampe5cbcde22014-09-16 14:59:49 -0700544 return verifier;
545 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800546}
547
Ian Rogers7b078e82014-09-10 14:44:24 -0700548MethodVerifier::MethodVerifier(Thread* self,
Andreas Gampe53e32d12015-12-09 21:03:23 -0800549 const DexFile* dex_file,
550 Handle<mirror::DexCache> dex_cache,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700551 Handle<mirror::ClassLoader> class_loader,
David Brazdil15fc7292016-09-02 14:13:18 +0100552 const DexFile::ClassDef& class_def,
Andreas Gampe53e32d12015-12-09 21:03:23 -0800553 const DexFile::CodeItem* code_item,
554 uint32_t dex_method_idx,
555 ArtMethod* method,
556 uint32_t method_access_flags,
557 bool can_load_classes,
558 bool allow_soft_failures,
559 bool need_precise_constants,
560 bool verify_to_dump,
Mathieu Chartier4306ef82014-12-19 18:41:47 -0800561 bool allow_thread_suspension)
Ian Rogers7b078e82014-09-10 14:44:24 -0700562 : self_(self),
Mathieu Chartierde40d472015-10-15 17:47:48 -0700563 arena_stack_(Runtime::Current()->GetArenaPool()),
Vladimir Marko69d310e2017-10-09 14:12:23 +0100564 allocator_(&arena_stack_),
Alex Lightd9aff132017-10-31 22:30:05 +0000565 reg_types_(can_load_classes, allocator_, allow_thread_suspension),
Vladimir Marko69d310e2017-10-09 14:12:23 +0100566 reg_table_(allocator_),
Andreas Gampee2abbc62017-09-15 11:59:26 -0700567 work_insn_idx_(dex::kDexNoIndex),
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800568 dex_method_idx_(dex_method_idx),
Nicolas Geoffrayb041a402017-11-13 15:16:22 +0000569 method_being_verified_(method),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700570 method_access_flags_(method_access_flags),
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700571 return_type_(nullptr),
jeffhaof56197c2012-03-05 18:01:54 -0800572 dex_file_(dex_file),
573 dex_cache_(dex_cache),
574 class_loader_(class_loader),
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700575 class_def_(class_def),
Mathieu Chartier698ebbc2018-01-05 11:00:42 -0800576 code_item_accessor_(*dex_file, code_item),
Ian Rogers7b078e82014-09-10 14:44:24 -0700577 declaring_class_(nullptr),
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700578 interesting_dex_pc_(-1),
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700579 monitor_enter_dex_pcs_(nullptr),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700580 have_pending_hard_failure_(false),
jeffhaofaf459e2012-08-31 15:32:47 -0700581 have_pending_runtime_throw_failure_(false),
Igor Murashkin4d7b75f2015-07-21 17:03:36 -0700582 have_pending_experimental_failure_(false),
Andreas Gamped12e7822015-06-25 10:26:40 -0700583 have_any_pending_runtime_throw_failure_(false),
jeffhaof56197c2012-03-05 18:01:54 -0800584 new_instance_count_(0),
Elliott Hughes80537bb2013-01-04 16:37:26 -0800585 monitor_enter_count_(0),
Andreas Gampe0760a812015-08-26 17:12:51 -0700586 encountered_failure_types_(0),
Jeff Haoee988952013-04-16 14:23:47 -0700587 can_load_classes_(can_load_classes),
Sebastien Hertz4d4adb12013-07-24 16:14:19 +0200588 allow_soft_failures_(allow_soft_failures),
Ian Rogers46960fe2014-05-23 10:43:43 -0700589 need_precise_constants_(need_precise_constants),
Sebastien Hertz4d4adb12013-07-24 16:14:19 +0200590 has_check_casts_(false),
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700591 has_virtual_or_interface_invokes_(false),
Mathieu Chartier4306ef82014-12-19 18:41:47 -0800592 verify_to_dump_(verify_to_dump),
Andreas Gamped5ad72f2015-06-26 17:33:47 -0700593 allow_thread_suspension_(allow_thread_suspension),
Andreas Gampee6215c02015-08-31 18:54:38 -0700594 is_constructor_(false),
Andreas Gamped5ad72f2015-06-26 17:33:47 -0700595 link_(nullptr) {
Mathieu Chartierd0ad2ee2015-03-31 14:59:59 -0700596 self->PushVerifier(this);
jeffhaof56197c2012-03-05 18:01:54 -0800597}
598
Mathieu Chartier590fee92013-09-13 13:46:47 -0700599MethodVerifier::~MethodVerifier() {
Mathieu Chartierd0ad2ee2015-03-31 14:59:59 -0700600 Thread::Current()->PopVerifier(this);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700601 STLDeleteElements(&failure_messages_);
602}
603
Andreas Gampeaaf0d382017-11-27 14:10:21 -0800604void MethodVerifier::FindLocksAtDexPc(
605 ArtMethod* m,
606 uint32_t dex_pc,
607 std::vector<MethodVerifier::DexLockInfo>* monitor_enter_dex_pcs) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700608 StackHandleScope<2> hs(Thread::Current());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700609 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache()));
610 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader()));
Andreas Gampebf9611f2016-03-25 16:58:00 -0700611 MethodVerifier verifier(hs.Self(),
612 m->GetDexFile(),
613 dex_cache,
614 class_loader,
David Brazdil15fc7292016-09-02 14:13:18 +0100615 m->GetClassDef(),
Andreas Gampebf9611f2016-03-25 16:58:00 -0700616 m->GetCodeItem(),
617 m->GetDexMethodIndex(),
618 m,
619 m->GetAccessFlags(),
620 false /* can_load_classes */,
621 true /* allow_soft_failures */,
622 false /* need_precise_constants */,
623 false /* verify_to_dump */,
624 false /* allow_thread_suspension */);
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700625 verifier.interesting_dex_pc_ = dex_pc;
Ian Rogers46960fe2014-05-23 10:43:43 -0700626 verifier.monitor_enter_dex_pcs_ = monitor_enter_dex_pcs;
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700627 verifier.FindLocksAtDexPc();
628}
629
630void MethodVerifier::FindLocksAtDexPc() {
Ian Rogers7b078e82014-09-10 14:44:24 -0700631 CHECK(monitor_enter_dex_pcs_ != nullptr);
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -0800632 CHECK(code_item_accessor_.HasCodeItem()); // This only makes sense for methods with code.
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700633
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -0800634 // Quick check whether there are any monitor_enter instructions before verifying.
635 for (const DexInstructionPcPair& inst : code_item_accessor_) {
636 if (inst->Opcode() == Instruction::MONITOR_ENTER) {
637 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
638 // verification. In practice, the phase we want relies on data structures set up by all the
639 // earlier passes, so we just run the full method verification and bail out early when we've
640 // got what we wanted.
641 Verify();
642 return;
643 }
Andreas Gampecb3c08f2014-09-18 13:16:38 -0700644 }
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700645}
646
Ian Rogersad0b3a32012-04-16 14:50:24 -0700647bool MethodVerifier::Verify() {
Andreas Gampee6215c02015-08-31 18:54:38 -0700648 // Some older code doesn't correctly mark constructors as such. Test for this case by looking at
649 // the name.
650 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
651 const char* method_name = dex_file_->StringDataByIdx(method_id.name_idx_);
652 bool instance_constructor_by_name = strcmp("<init>", method_name) == 0;
653 bool static_constructor_by_name = strcmp("<clinit>", method_name) == 0;
654 bool constructor_by_name = instance_constructor_by_name || static_constructor_by_name;
655 // Check that only constructors are tagged, and check for bad code that doesn't tag constructors.
656 if ((method_access_flags_ & kAccConstructor) != 0) {
657 if (!constructor_by_name) {
658 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
659 << "method is marked as constructor, but not named accordingly";
jeffhaobdb76512011-09-07 11:43:16 -0700660 return false;
Andreas Gampee6215c02015-08-31 18:54:38 -0700661 }
662 is_constructor_ = true;
663 } else if (constructor_by_name) {
David Sehr709b0702016-10-13 09:12:37 -0700664 LOG(WARNING) << "Method " << dex_file_->PrettyMethod(dex_method_idx_)
Andreas Gampee6215c02015-08-31 18:54:38 -0700665 << " not marked as constructor.";
666 is_constructor_ = true;
667 }
668 // If it's a constructor, check whether IsStatic() matches the name.
669 // This should have been rejected by the dex file verifier. Only do in debug build.
670 if (kIsDebugBuild) {
671 if (IsConstructor()) {
672 if (IsStatic() ^ static_constructor_by_name) {
673 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
674 << "constructor name doesn't match static flag";
675 return false;
676 }
jeffhaobdb76512011-09-07 11:43:16 -0700677 }
jeffhaobdb76512011-09-07 11:43:16 -0700678 }
Andreas Gampee6215c02015-08-31 18:54:38 -0700679
680 // Methods may only have one of public/protected/private.
681 // This should have been rejected by the dex file verifier. Only do in debug build.
682 if (kIsDebugBuild) {
683 size_t access_mod_count =
684 (((method_access_flags_ & kAccPublic) == 0) ? 0 : 1) +
685 (((method_access_flags_ & kAccProtected) == 0) ? 0 : 1) +
686 (((method_access_flags_ & kAccPrivate) == 0) ? 0 : 1);
687 if (access_mod_count > 1) {
688 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "method has more than one of public/protected/private";
689 return false;
690 }
691 }
692
693 // If there aren't any instructions, make sure that's expected, then exit successfully.
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -0800694 if (!code_item_accessor_.HasCodeItem()) {
Andreas Gampe76ed99d2016-03-28 18:31:29 -0700695 // Only native or abstract methods may not have code.
696 if ((method_access_flags_ & (kAccNative | kAccAbstract)) == 0) {
697 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "zero-length code in concrete non-native method";
698 return false;
699 }
700
Andreas Gampee6215c02015-08-31 18:54:38 -0700701 // This should have been rejected by the dex file verifier. Only do in debug build.
Andreas Gampe76ed99d2016-03-28 18:31:29 -0700702 // Note: the above will also be rejected in the dex file verifier, starting in dex version 37.
Andreas Gampee6215c02015-08-31 18:54:38 -0700703 if (kIsDebugBuild) {
Andreas Gampee6215c02015-08-31 18:54:38 -0700704 if ((method_access_flags_ & kAccAbstract) != 0) {
705 // Abstract methods are not allowed to have the following flags.
706 static constexpr uint32_t kForbidden =
707 kAccPrivate |
708 kAccStatic |
709 kAccFinal |
710 kAccNative |
711 kAccStrict |
712 kAccSynchronized;
713 if ((method_access_flags_ & kForbidden) != 0) {
714 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
715 << "method can't be abstract and private/static/final/native/strict/synchronized";
716 return false;
717 }
718 }
David Brazdil15fc7292016-09-02 14:13:18 +0100719 if ((class_def_.GetJavaAccessFlags() & kAccInterface) != 0) {
Alex Lighteb7c1442015-08-31 13:17:42 -0700720 // Interface methods must be public and abstract (if default methods are disabled).
Neil Fuller9724c632016-01-07 15:42:47 +0000721 uint32_t kRequired = kAccPublic;
Alex Lighteb7c1442015-08-31 13:17:42 -0700722 if ((method_access_flags_ & kRequired) != kRequired) {
Neil Fuller9724c632016-01-07 15:42:47 +0000723 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface methods must be public";
Andreas Gampee6215c02015-08-31 18:54:38 -0700724 return false;
725 }
726 // In addition to the above, interface methods must not be protected.
727 static constexpr uint32_t kForbidden = kAccProtected;
728 if ((method_access_flags_ & kForbidden) != 0) {
729 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface methods can't be protected";
730 return false;
731 }
732 }
733 // We also don't allow constructors to be abstract or native.
734 if (IsConstructor()) {
735 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "constructors can't be abstract or native";
736 return false;
737 }
738 }
739 return true;
740 }
741
742 // This should have been rejected by the dex file verifier. Only do in debug build.
743 if (kIsDebugBuild) {
744 // When there's code, the method must not be native or abstract.
745 if ((method_access_flags_ & (kAccNative | kAccAbstract)) != 0) {
746 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "non-zero-length code in abstract or native method";
747 return false;
748 }
749
David Brazdil15fc7292016-09-02 14:13:18 +0100750 if ((class_def_.GetJavaAccessFlags() & kAccInterface) != 0) {
Alex Lighteb7c1442015-08-31 13:17:42 -0700751 // Interfaces may always have static initializers for their fields. If we are running with
752 // default methods enabled we also allow other public, static, non-final methods to have code.
753 // Otherwise that is the only type of method allowed.
Alex Light0db36b32015-10-27 14:06:34 -0700754 if (!(IsConstructor() && IsStatic())) {
Neil Fuller9724c632016-01-07 15:42:47 +0000755 if (IsInstanceConstructor()) {
756 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interfaces may not have non-static constructor";
757 return false;
758 } else if (method_access_flags_ & kAccFinal) {
759 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interfaces may not have final methods";
760 return false;
Alex Lightb55f1ac2016-04-12 15:50:55 -0700761 } else {
762 uint32_t access_flag_options = kAccPublic;
Mathieu Chartierf6e31472017-12-28 13:32:08 -0800763 if (dex_file_->SupportsDefaultMethods()) {
Alex Lightb55f1ac2016-04-12 15:50:55 -0700764 access_flag_options |= kAccPrivate;
765 }
766 if (!(method_access_flags_ & access_flag_options)) {
767 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
768 << "interfaces may not have protected or package-private members";
769 return false;
770 }
Alex Lighteb7c1442015-08-31 13:17:42 -0700771 }
Andreas Gampee6215c02015-08-31 18:54:38 -0700772 }
773 }
774
775 // Instance constructors must not be synchronized.
776 if (IsInstanceConstructor()) {
777 static constexpr uint32_t kForbidden = kAccSynchronized;
778 if ((method_access_flags_ & kForbidden) != 0) {
779 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "constructors can't be synchronized";
780 return false;
781 }
782 }
783 }
784
Ian Rogersd81871c2011-10-03 13:57:23 -0700785 // Sanity-check the register counts. ins + locals = registers, so make sure that ins <= registers.
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -0800786 if (code_item_accessor_.InsSize() > code_item_accessor_.RegistersSize()) {
787 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad register counts (ins="
788 << code_item_accessor_.InsSize()
789 << " regs=" << code_item_accessor_.RegistersSize();
Ian Rogersd81871c2011-10-03 13:57:23 -0700790 return false;
jeffhaobdb76512011-09-07 11:43:16 -0700791 }
Alex Lighteb7c1442015-08-31 13:17:42 -0700792
Ian Rogersd81871c2011-10-03 13:57:23 -0700793 // Allocate and initialize an array to hold instruction data.
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -0800794 insn_flags_.reset(allocator_.AllocArray<InstructionFlags>(
795 code_item_accessor_.InsnsSizeInCodeUnits()));
Mathieu Chartierde40d472015-10-15 17:47:48 -0700796 DCHECK(insn_flags_ != nullptr);
797 std::uninitialized_fill_n(insn_flags_.get(),
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -0800798 code_item_accessor_.InsnsSizeInCodeUnits(),
Mathieu Chartierde40d472015-10-15 17:47:48 -0700799 InstructionFlags());
Ian Rogersd81871c2011-10-03 13:57:23 -0700800 // Run through the instructions and see if the width checks out.
801 bool result = ComputeWidthsAndCountOps();
Andreas Gampebf1cb772017-05-15 15:39:00 -0700802 bool allow_runtime_only_instructions = !Runtime::Current()->IsAotCompiler() || verify_to_dump_;
Ian Rogersd81871c2011-10-03 13:57:23 -0700803 // Flag instructions guarded by a "try" block and check exception handlers.
804 result = result && ScanTryCatchBlocks();
805 // Perform static instruction verification.
Andreas Gampebf1cb772017-05-15 15:39:00 -0700806 result = result && (allow_runtime_only_instructions
807 ? VerifyInstructions<true>()
808 : VerifyInstructions<false>());
Ian Rogersad0b3a32012-04-16 14:50:24 -0700809 // Perform code-flow analysis and return.
Vladimir Marko2b5eaa22013-12-13 13:59:30 +0000810 result = result && VerifyCodeFlow();
Andreas Gampe53e32d12015-12-09 21:03:23 -0800811
Vladimir Marko2b5eaa22013-12-13 13:59:30 +0000812 return result;
jeffhaoba5ebb92011-08-25 17:24:37 -0700813}
814
Ian Rogers776ac1f2012-04-13 23:36:36 -0700815std::ostream& MethodVerifier::Fail(VerifyError error) {
Andreas Gampe0760a812015-08-26 17:12:51 -0700816 // Mark the error type as encountered.
Andreas Gampea727e372015-08-25 09:22:37 -0700817 encountered_failure_types_ |= static_cast<uint32_t>(error);
Andreas Gampe0760a812015-08-26 17:12:51 -0700818
Ian Rogersad0b3a32012-04-16 14:50:24 -0700819 switch (error) {
820 case VERIFY_ERROR_NO_CLASS:
821 case VERIFY_ERROR_NO_FIELD:
822 case VERIFY_ERROR_NO_METHOD:
823 case VERIFY_ERROR_ACCESS_CLASS:
824 case VERIFY_ERROR_ACCESS_FIELD:
825 case VERIFY_ERROR_ACCESS_METHOD:
Ian Rogers08f753d2012-08-24 14:35:25 -0700826 case VERIFY_ERROR_INSTANTIATION:
827 case VERIFY_ERROR_CLASS_CHANGE:
Igor Murashkin158f35c2015-06-10 15:55:30 -0700828 case VERIFY_ERROR_FORCE_INTERPRETER:
Andreas Gampea727e372015-08-25 09:22:37 -0700829 case VERIFY_ERROR_LOCKING:
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800830 if (Runtime::Current()->IsAotCompiler() || !can_load_classes_) {
jeffhaofaf459e2012-08-31 15:32:47 -0700831 // If we're optimistically running verification at compile time, turn NO_xxx, ACCESS_xxx,
832 // class change and instantiation errors into soft verification errors so that we re-verify
833 // at runtime. We may fail to find or to agree on access because of not yet available class
834 // loaders, or class loaders that will differ at runtime. In these cases, we don't want to
835 // affect the soundness of the code being compiled. Instead, the generated code runs "slow
836 // paths" that dynamically perform the verification and cause the behavior to be that akin
837 // to an interpreter.
838 error = VERIFY_ERROR_BAD_CLASS_SOFT;
839 } else {
Jeff Haoa3faaf42013-09-03 19:07:00 -0700840 // If we fail again at runtime, mark that this instruction would throw and force this
841 // method to be executed using the interpreter with checks.
jeffhaofaf459e2012-08-31 15:32:47 -0700842 have_pending_runtime_throw_failure_ = true;
Andreas Gamped7f8d052015-03-12 11:05:47 -0700843
844 // We need to save the work_line if the instruction wasn't throwing before. Otherwise we'll
845 // try to merge garbage.
846 // Note: this assumes that Fail is called before we do any work_line modifications.
Andreas Gamped5ad72f2015-06-26 17:33:47 -0700847 // Note: this can fail before we touch any instruction, for the signature of a method. So
848 // add a check.
Andreas Gampee2abbc62017-09-15 11:59:26 -0700849 if (work_insn_idx_ < dex::kDexNoIndex) {
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -0800850 const Instruction& inst = code_item_accessor_.InstructionAt(work_insn_idx_);
Mathieu Chartier1d2d4ff2017-09-23 16:11:06 -0700851 int opcode_flags = Instruction::FlagsOf(inst.Opcode());
Andreas Gamped7f8d052015-03-12 11:05:47 -0700852
Andreas Gamped5ad72f2015-06-26 17:33:47 -0700853 if ((opcode_flags & Instruction::kThrow) == 0 && CurrentInsnFlags()->IsInTry()) {
854 saved_line_->CopyFromLine(work_line_.get());
855 }
Andreas Gamped7f8d052015-03-12 11:05:47 -0700856 }
jeffhaofaf459e2012-08-31 15:32:47 -0700857 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700858 break;
Andreas Gampea727e372015-08-25 09:22:37 -0700859
Ian Rogersad0b3a32012-04-16 14:50:24 -0700860 // Indication that verification should be retried at runtime.
861 case VERIFY_ERROR_BAD_CLASS_SOFT:
Jeff Haoee988952013-04-16 14:23:47 -0700862 if (!allow_soft_failures_) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700863 have_pending_hard_failure_ = true;
864 }
865 break;
Andreas Gampea727e372015-08-25 09:22:37 -0700866
jeffhaod5347e02012-03-22 17:25:05 -0700867 // Hard verification failures at compile time will still fail at runtime, so the class is
868 // marked as rejected to prevent it from being compiled.
Ian Rogersad0b3a32012-04-16 14:50:24 -0700869 case VERIFY_ERROR_BAD_CLASS_HARD: {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700870 have_pending_hard_failure_ = true;
Andreas Gampeebf850c2015-08-14 15:37:35 -0700871 if (VLOG_IS_ON(verifier) && kDumpRegLinesOnHardFailureIfVLOG) {
872 ScopedObjectAccess soa(Thread::Current());
873 std::ostringstream oss;
874 Dump(oss);
875 LOG(ERROR) << oss.str();
876 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700877 break;
Ian Rogers47a05882012-02-03 12:23:33 -0800878 }
879 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700880 failures_.push_back(error);
David Sehr709b0702016-10-13 09:12:37 -0700881 std::string location(StringPrintf("%s: [0x%X] ", dex_file_->PrettyMethod(dex_method_idx_).c_str(),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700882 work_insn_idx_));
Elena Sayapina78480ec2014-08-15 15:52:42 +0700883 std::ostringstream* failure_message = new std::ostringstream(location, std::ostringstream::ate);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700884 failure_messages_.push_back(failure_message);
885 return *failure_message;
886}
887
Ian Rogers576ca0c2014-06-06 15:58:22 -0700888std::ostream& MethodVerifier::LogVerifyInfo() {
David Sehr709b0702016-10-13 09:12:37 -0700889 return info_messages_ << "VFY: " << dex_file_->PrettyMethod(dex_method_idx_)
Ian Rogers576ca0c2014-06-06 15:58:22 -0700890 << '[' << reinterpret_cast<void*>(work_insn_idx_) << "] : ";
891}
892
Ian Rogersad0b3a32012-04-16 14:50:24 -0700893void MethodVerifier::PrependToLastFailMessage(std::string prepend) {
894 size_t failure_num = failure_messages_.size();
895 DCHECK_NE(failure_num, 0U);
896 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
897 prepend += last_fail_message->str();
Elena Sayapina78480ec2014-08-15 15:52:42 +0700898 failure_messages_[failure_num - 1] = new std::ostringstream(prepend, std::ostringstream::ate);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700899 delete last_fail_message;
900}
901
Vladimir Marko5c657fe2016-11-03 15:12:29 +0000902void MethodVerifier::AppendToLastFailMessage(const std::string& append) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700903 size_t failure_num = failure_messages_.size();
904 DCHECK_NE(failure_num, 0U);
905 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
906 (*last_fail_message) << append;
Ian Rogers47a05882012-02-03 12:23:33 -0800907}
908
Ian Rogers776ac1f2012-04-13 23:36:36 -0700909bool MethodVerifier::ComputeWidthsAndCountOps() {
jeffhaobdb76512011-09-07 11:43:16 -0700910 size_t new_instance_count = 0;
911 size_t monitor_enter_count = 0;
jeffhaobdb76512011-09-07 11:43:16 -0700912
Mathieu Chartieraf7c9022017-10-27 09:42:46 -0700913 // We can't assume the instruction is well formed, handle the case where calculating the size
914 // goes past the end of the code item.
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -0800915 SafeDexInstructionIterator it(code_item_accessor_.begin(), code_item_accessor_.end());
916 for ( ; !it.IsErrorState() && it < code_item_accessor_.end(); ++it) {
Mathieu Chartieraf7c9022017-10-27 09:42:46 -0700917 // In case the instruction goes past the end of the code item, make sure to not process it.
918 SafeDexInstructionIterator next = it;
919 ++next;
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -0800920 if (next.IsErrorState()) {
Mathieu Chartieraf7c9022017-10-27 09:42:46 -0700921 break;
922 }
923 Instruction::Code opcode = it->Opcode();
Ian Rogersa9a82542013-10-04 11:17:26 -0700924 switch (opcode) {
925 case Instruction::APUT_OBJECT:
926 case Instruction::CHECK_CAST:
927 has_check_casts_ = true;
928 break;
929 case Instruction::INVOKE_VIRTUAL:
930 case Instruction::INVOKE_VIRTUAL_RANGE:
931 case Instruction::INVOKE_INTERFACE:
932 case Instruction::INVOKE_INTERFACE_RANGE:
933 has_virtual_or_interface_invokes_ = true;
934 break;
935 case Instruction::MONITOR_ENTER:
936 monitor_enter_count++;
937 break;
938 case Instruction::NEW_INSTANCE:
939 new_instance_count++;
940 break;
941 default:
942 break;
jeffhaobdb76512011-09-07 11:43:16 -0700943 }
Mathieu Chartier2b2bef22017-10-26 17:10:19 -0700944 GetInstructionFlags(it.DexPc()).SetIsOpcode();
jeffhaobdb76512011-09-07 11:43:16 -0700945 }
946
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -0800947 if (it != code_item_accessor_.end()) {
948 const size_t insns_size = code_item_accessor_.InsnsSizeInCodeUnits();
jeffhaod5347e02012-03-22 17:25:05 -0700949 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "code did not end where expected ("
Mathieu Chartier2b2bef22017-10-26 17:10:19 -0700950 << it.DexPc() << " vs. " << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700951 return false;
952 }
953
Ian Rogersd81871c2011-10-03 13:57:23 -0700954 new_instance_count_ = new_instance_count;
955 monitor_enter_count_ = monitor_enter_count;
jeffhaobdb76512011-09-07 11:43:16 -0700956 return true;
957}
958
Ian Rogers776ac1f2012-04-13 23:36:36 -0700959bool MethodVerifier::ScanTryCatchBlocks() {
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -0800960 const uint32_t tries_size = code_item_accessor_.TriesSize();
jeffhaobdb76512011-09-07 11:43:16 -0700961 if (tries_size == 0) {
962 return true;
963 }
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -0800964 const uint32_t insns_size = code_item_accessor_.InsnsSizeInCodeUnits();
965 for (const DexFile::TryItem& try_item : code_item_accessor_.TryItems()) {
966 const uint32_t start = try_item.start_addr_;
967 const uint32_t end = start + try_item.insn_count_;
jeffhaobdb76512011-09-07 11:43:16 -0700968 if ((start >= end) || (start >= insns_size) || (end > insns_size)) {
jeffhaod5347e02012-03-22 17:25:05 -0700969 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad exception entry: startAddr=" << start
970 << " endAddr=" << end << " (size=" << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700971 return false;
972 }
Mathieu Chartierde40d472015-10-15 17:47:48 -0700973 if (!GetInstructionFlags(start).IsOpcode()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700974 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
975 << "'try' block starts inside an instruction (" << start << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700976 return false;
977 }
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -0800978 DexInstructionIterator end_it(code_item_accessor_.Insns(), end);
979 for (DexInstructionIterator it(code_item_accessor_.Insns(), start); it < end_it; ++it) {
980 GetInstructionFlags(it.DexPc()).SetInTry();
jeffhaobdb76512011-09-07 11:43:16 -0700981 }
982 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800983 // Iterate over each of the handlers to verify target addresses.
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -0800984 const uint8_t* handlers_ptr = code_item_accessor_.GetCatchHandlerData();
985 const uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700986 ClassLinker* linker = Runtime::Current()->GetClassLinker();
jeffhaobdb76512011-09-07 11:43:16 -0700987 for (uint32_t idx = 0; idx < handlers_size; idx++) {
Ian Rogers0571d352011-11-03 19:51:38 -0700988 CatchHandlerIterator iterator(handlers_ptr);
989 for (; iterator.HasNext(); iterator.Next()) {
Igor Murashkin2ffb7032017-11-08 13:35:21 -0800990 uint32_t dex_pc = iterator.GetHandlerAddress();
Mathieu Chartierde40d472015-10-15 17:47:48 -0700991 if (!GetInstructionFlags(dex_pc).IsOpcode()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700992 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
993 << "exception handler starts at bad address (" << dex_pc << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700994 return false;
995 }
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -0800996 if (!CheckNotMoveResult(code_item_accessor_.Insns(), dex_pc)) {
Stephen Kyle9bc61992014-09-22 13:53:15 +0100997 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
998 << "exception handler begins with move-result* (" << dex_pc << ")";
999 return false;
1000 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07001001 GetInstructionFlags(dex_pc).SetBranchTarget();
Ian Rogers28ad40d2011-10-27 15:19:26 -07001002 // Ensure exception types are resolved so that they don't need resolution to be delivered,
1003 // unresolved exception types will be ignored by exception delivery
Andreas Gampea5b09a62016-11-17 15:21:22 -08001004 if (iterator.GetHandlerTypeIndex().IsValid()) {
Vladimir Marko666ee3d2017-12-11 18:37:36 +00001005 ObjPtr<mirror::Class> exception_type =
1006 linker->ResolveType(iterator.GetHandlerTypeIndex(), dex_cache_, class_loader_);
Ian Rogers7b078e82014-09-10 14:44:24 -07001007 if (exception_type == nullptr) {
1008 DCHECK(self_->IsExceptionPending());
1009 self_->ClearException();
Ian Rogers28ad40d2011-10-27 15:19:26 -07001010 }
1011 }
jeffhaobdb76512011-09-07 11:43:16 -07001012 }
Ian Rogers0571d352011-11-03 19:51:38 -07001013 handlers_ptr = iterator.EndDataPointer();
jeffhaobdb76512011-09-07 11:43:16 -07001014 }
jeffhaobdb76512011-09-07 11:43:16 -07001015 return true;
1016}
1017
Andreas Gampebf1cb772017-05-15 15:39:00 -07001018template <bool kAllowRuntimeOnlyInstructions>
Ian Rogers776ac1f2012-04-13 23:36:36 -07001019bool MethodVerifier::VerifyInstructions() {
Ian Rogers0c7abda2012-09-19 13:33:42 -07001020 /* 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 -07001021 GetInstructionFlags(0).SetBranchTarget();
1022 GetInstructionFlags(0).SetCompileTimeInfoPoint();
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001023 for (const DexInstructionPcPair& inst : code_item_accessor_) {
Mathieu Chartier2b2bef22017-10-26 17:10:19 -07001024 const uint32_t dex_pc = inst.DexPc();
1025 if (!VerifyInstruction<kAllowRuntimeOnlyInstructions>(&inst.Inst(), dex_pc)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001026 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -07001027 return false;
1028 }
1029 /* Flag instructions that are garbage collection points */
Sameer Abu Asal02c42232013-04-30 12:09:45 -07001030 // All invoke points are marked as "Throw" points already.
1031 // We are relying on this to also count all the invokes as interesting.
Vladimir Marko8b858e12014-11-27 14:52:37 +00001032 if (inst->IsBranch()) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07001033 GetInstructionFlags(dex_pc).SetCompileTimeInfoPoint();
Vladimir Marko8b858e12014-11-27 14:52:37 +00001034 // The compiler also needs safepoints for fall-through to loop heads.
1035 // Such a loop head must be a target of a branch.
1036 int32_t offset = 0;
1037 bool cond, self_ok;
1038 bool target_ok = GetBranchOffset(dex_pc, &offset, &cond, &self_ok);
1039 DCHECK(target_ok);
Mathieu Chartierde40d472015-10-15 17:47:48 -07001040 GetInstructionFlags(dex_pc + offset).SetCompileTimeInfoPoint();
Vladimir Marko8b858e12014-11-27 14:52:37 +00001041 } else if (inst->IsSwitch() || inst->IsThrow()) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07001042 GetInstructionFlags(dex_pc).SetCompileTimeInfoPoint();
Ian Rogersb8c78592013-07-25 23:52:52 +00001043 } else if (inst->IsReturn()) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07001044 GetInstructionFlags(dex_pc).SetCompileTimeInfoPointAndReturn();
Ian Rogersd81871c2011-10-03 13:57:23 -07001045 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001046 }
1047 return true;
1048}
1049
Andreas Gampebf1cb772017-05-15 15:39:00 -07001050template <bool kAllowRuntimeOnlyInstructions>
1051bool MethodVerifier::VerifyInstruction(const Instruction* inst, uint32_t code_offset) {
Andreas Gampee05cc662017-05-15 10:17:30 -07001052 if (Instruction::kHaveExperimentalInstructions && UNLIKELY(inst->IsExperimental())) {
Igor Murashkin4d7b75f2015-07-21 17:03:36 -07001053 // Experimental instructions don't yet have verifier support implementation.
1054 // While it is possible to use them by themselves, when we try to use stable instructions
1055 // with a virtual register that was created by an experimental instruction,
1056 // the data flow analysis will fail.
1057 Fail(VERIFY_ERROR_FORCE_INTERPRETER)
1058 << "experimental instruction is not supported by verifier; skipping verification";
1059 have_pending_experimental_failure_ = true;
1060 return false;
1061 }
1062
Ian Rogersd81871c2011-10-03 13:57:23 -07001063 bool result = true;
1064 switch (inst->GetVerifyTypeArgumentA()) {
1065 case Instruction::kVerifyRegA:
Ian Rogers29a26482014-05-02 15:27:29 -07001066 result = result && CheckRegisterIndex(inst->VRegA());
Ian Rogersd81871c2011-10-03 13:57:23 -07001067 break;
1068 case Instruction::kVerifyRegAWide:
Ian Rogers29a26482014-05-02 15:27:29 -07001069 result = result && CheckWideRegisterIndex(inst->VRegA());
Ian Rogersd81871c2011-10-03 13:57:23 -07001070 break;
1071 }
1072 switch (inst->GetVerifyTypeArgumentB()) {
1073 case Instruction::kVerifyRegB:
Ian Rogers29a26482014-05-02 15:27:29 -07001074 result = result && CheckRegisterIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -07001075 break;
1076 case Instruction::kVerifyRegBField:
Ian Rogers29a26482014-05-02 15:27:29 -07001077 result = result && CheckFieldIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -07001078 break;
1079 case Instruction::kVerifyRegBMethod:
Ian Rogers29a26482014-05-02 15:27:29 -07001080 result = result && CheckMethodIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -07001081 break;
1082 case Instruction::kVerifyRegBNewInstance:
Andreas Gampea5b09a62016-11-17 15:21:22 -08001083 result = result && CheckNewInstance(dex::TypeIndex(inst->VRegB()));
Ian Rogersd81871c2011-10-03 13:57:23 -07001084 break;
1085 case Instruction::kVerifyRegBString:
Ian Rogers29a26482014-05-02 15:27:29 -07001086 result = result && CheckStringIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -07001087 break;
1088 case Instruction::kVerifyRegBType:
Andreas Gampea5b09a62016-11-17 15:21:22 -08001089 result = result && CheckTypeIndex(dex::TypeIndex(inst->VRegB()));
Ian Rogersd81871c2011-10-03 13:57:23 -07001090 break;
1091 case Instruction::kVerifyRegBWide:
Ian Rogers29a26482014-05-02 15:27:29 -07001092 result = result && CheckWideRegisterIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -07001093 break;
Orion Hodson2e599942017-09-22 16:17:41 +01001094 case Instruction::kVerifyRegBCallSite:
1095 result = result && CheckCallSiteIndex(inst->VRegB());
1096 break;
1097 case Instruction::kVerifyRegBMethodHandle:
1098 result = result && CheckMethodHandleIndex(inst->VRegB());
1099 break;
1100 case Instruction::kVerifyRegBPrototype:
1101 result = result && CheckPrototypeIndex(inst->VRegB());
1102 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001103 }
1104 switch (inst->GetVerifyTypeArgumentC()) {
1105 case Instruction::kVerifyRegC:
Ian Rogers29a26482014-05-02 15:27:29 -07001106 result = result && CheckRegisterIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -07001107 break;
1108 case Instruction::kVerifyRegCField:
Ian Rogers29a26482014-05-02 15:27:29 -07001109 result = result && CheckFieldIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -07001110 break;
1111 case Instruction::kVerifyRegCNewArray:
Andreas Gampea5b09a62016-11-17 15:21:22 -08001112 result = result && CheckNewArray(dex::TypeIndex(inst->VRegC()));
Ian Rogersd81871c2011-10-03 13:57:23 -07001113 break;
1114 case Instruction::kVerifyRegCType:
Andreas Gampea5b09a62016-11-17 15:21:22 -08001115 result = result && CheckTypeIndex(dex::TypeIndex(inst->VRegC()));
Ian Rogersd81871c2011-10-03 13:57:23 -07001116 break;
1117 case Instruction::kVerifyRegCWide:
Ian Rogers29a26482014-05-02 15:27:29 -07001118 result = result && CheckWideRegisterIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -07001119 break;
1120 }
Orion Hodsoncfa325e2016-10-13 10:25:54 +01001121 switch (inst->GetVerifyTypeArgumentH()) {
1122 case Instruction::kVerifyRegHPrototype:
1123 result = result && CheckPrototypeIndex(inst->VRegH());
1124 break;
1125 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001126 switch (inst->GetVerifyExtraFlags()) {
1127 case Instruction::kVerifyArrayData:
1128 result = result && CheckArrayData(code_offset);
1129 break;
1130 case Instruction::kVerifyBranchTarget:
1131 result = result && CheckBranchTarget(code_offset);
1132 break;
1133 case Instruction::kVerifySwitchTargets:
1134 result = result && CheckSwitchTargets(code_offset);
1135 break;
Andreas Gampec3314312014-06-19 18:13:29 -07001136 case Instruction::kVerifyVarArgNonZero:
1137 // Fall-through.
Ian Rogers29a26482014-05-02 15:27:29 -07001138 case Instruction::kVerifyVarArg: {
Taiju Tsuiki29498a22015-04-13 14:21:00 +09001139 // Instructions that can actually return a negative value shouldn't have this flag.
1140 uint32_t v_a = dchecked_integral_cast<uint32_t>(inst->VRegA());
1141 if ((inst->GetVerifyExtraFlags() == Instruction::kVerifyVarArgNonZero && v_a == 0) ||
1142 v_a > Instruction::kMaxVarArgRegs) {
1143 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << v_a << ") in "
Andreas Gampec3314312014-06-19 18:13:29 -07001144 "non-range invoke";
1145 return false;
1146 }
Taiju Tsuiki29498a22015-04-13 14:21:00 +09001147
Ian Rogers29a26482014-05-02 15:27:29 -07001148 uint32_t args[Instruction::kMaxVarArgRegs];
1149 inst->GetVarArgs(args);
Taiju Tsuiki29498a22015-04-13 14:21:00 +09001150 result = result && CheckVarArgRegs(v_a, args);
Ian Rogersd81871c2011-10-03 13:57:23 -07001151 break;
Ian Rogers29a26482014-05-02 15:27:29 -07001152 }
Andreas Gampec3314312014-06-19 18:13:29 -07001153 case Instruction::kVerifyVarArgRangeNonZero:
1154 // Fall-through.
Ian Rogersd81871c2011-10-03 13:57:23 -07001155 case Instruction::kVerifyVarArgRange:
Andreas Gampec3314312014-06-19 18:13:29 -07001156 if (inst->GetVerifyExtraFlags() == Instruction::kVerifyVarArgRangeNonZero &&
1157 inst->VRegA() <= 0) {
1158 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << inst->VRegA() << ") in "
1159 "range invoke";
1160 return false;
1161 }
Ian Rogers29a26482014-05-02 15:27:29 -07001162 result = result && CheckVarArgRangeRegs(inst->VRegA(), inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -07001163 break;
1164 case Instruction::kVerifyError:
jeffhaod5347e02012-03-22 17:25:05 -07001165 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected opcode " << inst->Name();
Ian Rogersd81871c2011-10-03 13:57:23 -07001166 result = false;
1167 break;
1168 }
Andreas Gampebf1cb772017-05-15 15:39:00 -07001169 if (!kAllowRuntimeOnlyInstructions && inst->GetVerifyIsRuntimeOnly()) {
Ian Rogers5fb22a92014-06-13 10:31:28 -07001170 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "opcode only expected at runtime " << inst->Name();
1171 result = false;
1172 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001173 return result;
1174}
1175
Ian Rogers7b078e82014-09-10 14:44:24 -07001176inline bool MethodVerifier::CheckRegisterIndex(uint32_t idx) {
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001177 if (UNLIKELY(idx >= code_item_accessor_.RegistersSize())) {
jeffhaod5347e02012-03-22 17:25:05 -07001178 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register index out of range (" << idx << " >= "
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001179 << code_item_accessor_.RegistersSize() << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001180 return false;
1181 }
1182 return true;
1183}
1184
Ian Rogers7b078e82014-09-10 14:44:24 -07001185inline bool MethodVerifier::CheckWideRegisterIndex(uint32_t idx) {
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001186 if (UNLIKELY(idx + 1 >= code_item_accessor_.RegistersSize())) {
jeffhaod5347e02012-03-22 17:25:05 -07001187 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "wide register index out of range (" << idx
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001188 << "+1 >= " << code_item_accessor_.RegistersSize() << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001189 return false;
1190 }
1191 return true;
1192}
1193
Orion Hodson2e599942017-09-22 16:17:41 +01001194inline bool MethodVerifier::CheckCallSiteIndex(uint32_t idx) {
1195 uint32_t limit = dex_file_->NumCallSiteIds();
1196 if (UNLIKELY(idx >= limit)) {
1197 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad call site index " << idx << " (max "
1198 << limit << ")";
1199 return false;
1200 }
1201 return true;
1202}
1203
Ian Rogers7b078e82014-09-10 14:44:24 -07001204inline bool MethodVerifier::CheckFieldIndex(uint32_t idx) {
Andreas Gampe29e81422017-05-15 16:29:32 -07001205 if (UNLIKELY(idx >= dex_file_->GetHeader().field_ids_size_)) {
jeffhaod5347e02012-03-22 17:25:05 -07001206 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad field index " << idx << " (max "
1207 << dex_file_->GetHeader().field_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001208 return false;
1209 }
1210 return true;
1211}
1212
Ian Rogers7b078e82014-09-10 14:44:24 -07001213inline bool MethodVerifier::CheckMethodIndex(uint32_t idx) {
Andreas Gampe29e81422017-05-15 16:29:32 -07001214 if (UNLIKELY(idx >= dex_file_->GetHeader().method_ids_size_)) {
jeffhaod5347e02012-03-22 17:25:05 -07001215 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad method index " << idx << " (max "
1216 << dex_file_->GetHeader().method_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001217 return false;
1218 }
1219 return true;
1220}
1221
Orion Hodson2e599942017-09-22 16:17:41 +01001222inline bool MethodVerifier::CheckMethodHandleIndex(uint32_t idx) {
1223 uint32_t limit = dex_file_->NumMethodHandles();
1224 if (UNLIKELY(idx >= limit)) {
1225 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad method handle index " << idx << " (max "
1226 << limit << ")";
1227 return false;
1228 }
1229 return true;
1230}
1231
Andreas Gampea5b09a62016-11-17 15:21:22 -08001232inline bool MethodVerifier::CheckNewInstance(dex::TypeIndex idx) {
Andreas Gampe29e81422017-05-15 16:29:32 -07001233 if (UNLIKELY(idx.index_ >= dex_file_->GetHeader().type_ids_size_)) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08001234 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx.index_ << " (max "
jeffhaod5347e02012-03-22 17:25:05 -07001235 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001236 return false;
1237 }
1238 // We don't need the actual class, just a pointer to the class name.
Ian Rogers0571d352011-11-03 19:51:38 -07001239 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Andreas Gampe29e81422017-05-15 16:29:32 -07001240 if (UNLIKELY(descriptor[0] != 'L')) {
jeffhaod5347e02012-03-22 17:25:05 -07001241 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "can't call new-instance on type '" << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001242 return false;
Andreas Gampe29e81422017-05-15 16:29:32 -07001243 } else if (UNLIKELY(strcmp(descriptor, "Ljava/lang/Class;") == 0)) {
Aart Bikdb698f12016-07-25 17:52:22 -07001244 // An unlikely new instance on Class is not allowed. Fall back to interpreter to ensure an
1245 // exception is thrown when this statement is executed (compiled code would not do that).
1246 Fail(VERIFY_ERROR_INSTANTIATION);
Ian Rogersd81871c2011-10-03 13:57:23 -07001247 }
1248 return true;
1249}
1250
Orion Hodsoncfa325e2016-10-13 10:25:54 +01001251inline bool MethodVerifier::CheckPrototypeIndex(uint32_t idx) {
Andreas Gampe29e81422017-05-15 16:29:32 -07001252 if (UNLIKELY(idx >= dex_file_->GetHeader().proto_ids_size_)) {
Orion Hodsoncfa325e2016-10-13 10:25:54 +01001253 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad prototype index " << idx << " (max "
1254 << dex_file_->GetHeader().proto_ids_size_ << ")";
1255 return false;
1256 }
1257 return true;
1258}
1259
Ian Rogers7b078e82014-09-10 14:44:24 -07001260inline bool MethodVerifier::CheckStringIndex(uint32_t idx) {
Andreas Gampe29e81422017-05-15 16:29:32 -07001261 if (UNLIKELY(idx >= dex_file_->GetHeader().string_ids_size_)) {
jeffhaod5347e02012-03-22 17:25:05 -07001262 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad string index " << idx << " (max "
1263 << dex_file_->GetHeader().string_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001264 return false;
1265 }
1266 return true;
1267}
1268
Andreas Gampea5b09a62016-11-17 15:21:22 -08001269inline bool MethodVerifier::CheckTypeIndex(dex::TypeIndex idx) {
Andreas Gampe29e81422017-05-15 16:29:32 -07001270 if (UNLIKELY(idx.index_ >= dex_file_->GetHeader().type_ids_size_)) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08001271 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx.index_ << " (max "
jeffhaod5347e02012-03-22 17:25:05 -07001272 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001273 return false;
1274 }
1275 return true;
1276}
1277
Andreas Gampea5b09a62016-11-17 15:21:22 -08001278bool MethodVerifier::CheckNewArray(dex::TypeIndex idx) {
Andreas Gampe29e81422017-05-15 16:29:32 -07001279 if (UNLIKELY(idx.index_ >= dex_file_->GetHeader().type_ids_size_)) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08001280 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx.index_ << " (max "
jeffhaod5347e02012-03-22 17:25:05 -07001281 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001282 return false;
1283 }
1284 int bracket_count = 0;
Ian Rogers0571d352011-11-03 19:51:38 -07001285 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -07001286 const char* cp = descriptor;
1287 while (*cp++ == '[') {
1288 bracket_count++;
1289 }
Andreas Gampe29e81422017-05-15 16:29:32 -07001290 if (UNLIKELY(bracket_count == 0)) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001291 /* The given class must be an array type. */
Brian Carlstrom93c33962013-07-26 10:37:43 -07001292 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1293 << "can't new-array class '" << descriptor << "' (not an array)";
Ian Rogersd81871c2011-10-03 13:57:23 -07001294 return false;
Andreas Gampe29e81422017-05-15 16:29:32 -07001295 } else if (UNLIKELY(bracket_count > 255)) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001296 /* It is illegal to create an array of more than 255 dimensions. */
Brian Carlstrom93c33962013-07-26 10:37:43 -07001297 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1298 << "can't new-array class '" << descriptor << "' (exceeds limit)";
Ian Rogersd81871c2011-10-03 13:57:23 -07001299 return false;
1300 }
1301 return true;
1302}
1303
Ian Rogers776ac1f2012-04-13 23:36:36 -07001304bool MethodVerifier::CheckArrayData(uint32_t cur_offset) {
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001305 const uint32_t insn_count = code_item_accessor_.InsnsSizeInCodeUnits();
1306 const uint16_t* insns = code_item_accessor_.Insns() + cur_offset;
Ian Rogersd81871c2011-10-03 13:57:23 -07001307 const uint16_t* array_data;
1308 int32_t array_data_offset;
1309
1310 DCHECK_LT(cur_offset, insn_count);
1311 /* make sure the start of the array data table is in range */
Andreas Gampe53de99c2015-08-17 13:43:55 -07001312 array_data_offset = insns[1] | (static_cast<int32_t>(insns[2]) << 16);
Andreas Gampe29e81422017-05-15 16:29:32 -07001313 if (UNLIKELY(static_cast<int32_t>(cur_offset) + array_data_offset < 0 ||
1314 cur_offset + array_data_offset + 2 >= insn_count)) {
jeffhaod5347e02012-03-22 17:25:05 -07001315 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data start: at " << cur_offset
Brian Carlstrom93c33962013-07-26 10:37:43 -07001316 << ", data offset " << array_data_offset
1317 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -07001318 return false;
1319 }
1320 /* offset to array data table is a relative branch-style offset */
1321 array_data = insns + array_data_offset;
Andreas Gampe57c47582015-07-01 22:05:59 -07001322 // Make sure the table is at an even dex pc, that is, 32-bit aligned.
Andreas Gampe29e81422017-05-15 16:29:32 -07001323 if (UNLIKELY(!IsAligned<4>(array_data))) {
jeffhaod5347e02012-03-22 17:25:05 -07001324 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned array data table: at " << cur_offset
1325 << ", data offset " << array_data_offset;
Ian Rogersd81871c2011-10-03 13:57:23 -07001326 return false;
1327 }
Andreas Gampe57c47582015-07-01 22:05:59 -07001328 // Make sure the array-data is marked as an opcode. This ensures that it was reached when
1329 // traversing the code item linearly. It is an approximation for a by-spec padding value.
Andreas Gampe29e81422017-05-15 16:29:32 -07001330 if (UNLIKELY(!GetInstructionFlags(cur_offset + array_data_offset).IsOpcode())) {
Andreas Gampe57c47582015-07-01 22:05:59 -07001331 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array data table at " << cur_offset
1332 << ", data offset " << array_data_offset
1333 << " not correctly visited, probably bad padding.";
1334 return false;
1335 }
1336
Ian Rogersd81871c2011-10-03 13:57:23 -07001337 uint32_t value_width = array_data[1];
Elliott Hughes398f64b2012-03-26 18:05:48 -07001338 uint32_t value_count = *reinterpret_cast<const uint32_t*>(&array_data[2]);
Ian Rogersd81871c2011-10-03 13:57:23 -07001339 uint32_t table_size = 4 + (value_width * value_count + 1) / 2;
1340 /* make sure the end of the switch is in range */
Andreas Gampe29e81422017-05-15 16:29:32 -07001341 if (UNLIKELY(cur_offset + array_data_offset + table_size > insn_count)) {
jeffhaod5347e02012-03-22 17:25:05 -07001342 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data end: at " << cur_offset
1343 << ", data offset " << array_data_offset << ", end "
1344 << cur_offset + array_data_offset + table_size
1345 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -07001346 return false;
1347 }
1348 return true;
1349}
1350
Ian Rogers776ac1f2012-04-13 23:36:36 -07001351bool MethodVerifier::CheckBranchTarget(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001352 int32_t offset;
1353 bool isConditional, selfOkay;
1354 if (!GetBranchOffset(cur_offset, &offset, &isConditional, &selfOkay)) {
1355 return false;
1356 }
Andreas Gampe29e81422017-05-15 16:29:32 -07001357 if (UNLIKELY(!selfOkay && offset == 0)) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001358 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch offset of zero not allowed at"
1359 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -07001360 return false;
1361 }
Elliott Hughes81ff3182012-03-23 20:35:56 -07001362 // Check for 32-bit overflow. This isn't strictly necessary if we can depend on the runtime
1363 // to have identical "wrap-around" behavior, but it's unwise to depend on that.
Andreas Gampe29e81422017-05-15 16:29:32 -07001364 if (UNLIKELY(((int64_t) cur_offset + (int64_t) offset) != (int64_t) (cur_offset + offset))) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001365 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch target overflow "
1366 << reinterpret_cast<void*>(cur_offset) << " +" << offset;
Ian Rogersd81871c2011-10-03 13:57:23 -07001367 return false;
1368 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001369 int32_t abs_offset = cur_offset + offset;
Andreas Gampe29e81422017-05-15 16:29:32 -07001370 if (UNLIKELY(abs_offset < 0 ||
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001371 (uint32_t) abs_offset >= code_item_accessor_.InsnsSizeInCodeUnits() ||
Andreas Gampe29e81422017-05-15 16:29:32 -07001372 !GetInstructionFlags(abs_offset).IsOpcode())) {
jeffhaod5347e02012-03-22 17:25:05 -07001373 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid branch target " << offset << " (-> "
Elliott Hughes398f64b2012-03-26 18:05:48 -07001374 << reinterpret_cast<void*>(abs_offset) << ") at "
1375 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -07001376 return false;
1377 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07001378 GetInstructionFlags(abs_offset).SetBranchTarget();
Ian Rogersd81871c2011-10-03 13:57:23 -07001379 return true;
1380}
1381
Ian Rogers776ac1f2012-04-13 23:36:36 -07001382bool MethodVerifier::GetBranchOffset(uint32_t cur_offset, int32_t* pOffset, bool* pConditional,
Ian Rogersd81871c2011-10-03 13:57:23 -07001383 bool* selfOkay) {
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001384 const uint16_t* insns = code_item_accessor_.Insns() + cur_offset;
Ian Rogersd81871c2011-10-03 13:57:23 -07001385 *pConditional = false;
1386 *selfOkay = false;
jeffhaoba5ebb92011-08-25 17:24:37 -07001387 switch (*insns & 0xff) {
1388 case Instruction::GOTO:
1389 *pOffset = ((int16_t) *insns) >> 8;
jeffhaoba5ebb92011-08-25 17:24:37 -07001390 break;
1391 case Instruction::GOTO_32:
1392 *pOffset = insns[1] | (((uint32_t) insns[2]) << 16);
jeffhaoba5ebb92011-08-25 17:24:37 -07001393 *selfOkay = true;
1394 break;
1395 case Instruction::GOTO_16:
1396 *pOffset = (int16_t) insns[1];
jeffhaoba5ebb92011-08-25 17:24:37 -07001397 break;
1398 case Instruction::IF_EQ:
1399 case Instruction::IF_NE:
1400 case Instruction::IF_LT:
1401 case Instruction::IF_GE:
1402 case Instruction::IF_GT:
1403 case Instruction::IF_LE:
1404 case Instruction::IF_EQZ:
1405 case Instruction::IF_NEZ:
1406 case Instruction::IF_LTZ:
1407 case Instruction::IF_GEZ:
1408 case Instruction::IF_GTZ:
1409 case Instruction::IF_LEZ:
1410 *pOffset = (int16_t) insns[1];
1411 *pConditional = true;
jeffhaoba5ebb92011-08-25 17:24:37 -07001412 break;
1413 default:
1414 return false;
jeffhaoba5ebb92011-08-25 17:24:37 -07001415 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001416 return true;
1417}
1418
Ian Rogers776ac1f2012-04-13 23:36:36 -07001419bool MethodVerifier::CheckSwitchTargets(uint32_t cur_offset) {
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001420 const uint32_t insn_count = code_item_accessor_.InsnsSizeInCodeUnits();
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001421 DCHECK_LT(cur_offset, insn_count);
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001422 const uint16_t* insns = code_item_accessor_.Insns() + cur_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001423 /* make sure the start of the switch is in range */
Andreas Gampe53de99c2015-08-17 13:43:55 -07001424 int32_t switch_offset = insns[1] | (static_cast<int32_t>(insns[2]) << 16);
Andreas Gampe29e81422017-05-15 16:29:32 -07001425 if (UNLIKELY(static_cast<int32_t>(cur_offset) + switch_offset < 0 ||
1426 cur_offset + switch_offset + 2 > insn_count)) {
jeffhaod5347e02012-03-22 17:25:05 -07001427 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch start: at " << cur_offset
Brian Carlstrom93c33962013-07-26 10:37:43 -07001428 << ", switch offset " << switch_offset
1429 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001430 return false;
1431 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001432 /* offset to switch table is a relative branch-style offset */
Ian Rogersd81871c2011-10-03 13:57:23 -07001433 const uint16_t* switch_insns = insns + switch_offset;
Andreas Gampe57c47582015-07-01 22:05:59 -07001434 // Make sure the table is at an even dex pc, that is, 32-bit aligned.
Andreas Gampe29e81422017-05-15 16:29:32 -07001435 if (UNLIKELY(!IsAligned<4>(switch_insns))) {
jeffhaod5347e02012-03-22 17:25:05 -07001436 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned switch table: at " << cur_offset
1437 << ", switch offset " << switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001438 return false;
1439 }
Andreas Gampe57c47582015-07-01 22:05:59 -07001440 // Make sure the switch data is marked as an opcode. This ensures that it was reached when
1441 // traversing the code item linearly. It is an approximation for a by-spec padding value.
Andreas Gampe29e81422017-05-15 16:29:32 -07001442 if (UNLIKELY(!GetInstructionFlags(cur_offset + switch_offset).IsOpcode())) {
Andreas Gampe57c47582015-07-01 22:05:59 -07001443 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "switch table at " << cur_offset
1444 << ", switch offset " << switch_offset
1445 << " not correctly visited, probably bad padding.";
1446 return false;
1447 }
1448
David Brazdil5469d342015-09-25 16:57:53 +01001449 bool is_packed_switch = (*insns & 0xff) == Instruction::PACKED_SWITCH;
1450
Ian Rogersd81871c2011-10-03 13:57:23 -07001451 uint32_t switch_count = switch_insns[1];
David Brazdil5469d342015-09-25 16:57:53 +01001452 int32_t targets_offset;
Ian Rogersd81871c2011-10-03 13:57:23 -07001453 uint16_t expected_signature;
David Brazdil5469d342015-09-25 16:57:53 +01001454 if (is_packed_switch) {
jeffhaoba5ebb92011-08-25 17:24:37 -07001455 /* 0=sig, 1=count, 2/3=firstKey */
1456 targets_offset = 4;
jeffhaoba5ebb92011-08-25 17:24:37 -07001457 expected_signature = Instruction::kPackedSwitchSignature;
1458 } else {
1459 /* 0=sig, 1=count, 2..count*2 = keys */
jeffhaoba5ebb92011-08-25 17:24:37 -07001460 targets_offset = 2 + 2 * switch_count;
1461 expected_signature = Instruction::kSparseSwitchSignature;
1462 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001463 uint32_t table_size = targets_offset + switch_count * 2;
Andreas Gampe29e81422017-05-15 16:29:32 -07001464 if (UNLIKELY(switch_insns[0] != expected_signature)) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001465 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1466 << StringPrintf("wrong signature for switch table (%x, wanted %x)",
1467 switch_insns[0], expected_signature);
jeffhaoba5ebb92011-08-25 17:24:37 -07001468 return false;
1469 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001470 /* make sure the end of the switch is in range */
Andreas Gampe29e81422017-05-15 16:29:32 -07001471 if (UNLIKELY(cur_offset + switch_offset + table_size > (uint32_t) insn_count)) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001472 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch end: at " << cur_offset
1473 << ", switch offset " << switch_offset
1474 << ", end " << (cur_offset + switch_offset + table_size)
jeffhaod5347e02012-03-22 17:25:05 -07001475 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001476 return false;
1477 }
David Brazdil5469d342015-09-25 16:57:53 +01001478
1479 constexpr int32_t keys_offset = 2;
1480 if (switch_count > 1) {
1481 if (is_packed_switch) {
1482 /* for a packed switch, verify that keys do not overflow int32 */
1483 int32_t first_key = switch_insns[keys_offset] | (switch_insns[keys_offset + 1] << 16);
1484 int32_t max_first_key =
1485 std::numeric_limits<int32_t>::max() - (static_cast<int32_t>(switch_count) - 1);
Andreas Gampe29e81422017-05-15 16:29:32 -07001486 if (UNLIKELY(first_key > max_first_key)) {
David Brazdil5469d342015-09-25 16:57:53 +01001487 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid packed switch: first_key=" << first_key
1488 << ", switch_count=" << switch_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001489 return false;
1490 }
David Brazdil5469d342015-09-25 16:57:53 +01001491 } else {
1492 /* for a sparse switch, verify the keys are in ascending order */
1493 int32_t last_key = switch_insns[keys_offset] | (switch_insns[keys_offset + 1] << 16);
1494 for (uint32_t targ = 1; targ < switch_count; targ++) {
1495 int32_t key =
1496 static_cast<int32_t>(switch_insns[keys_offset + targ * 2]) |
1497 static_cast<int32_t>(switch_insns[keys_offset + targ * 2 + 1] << 16);
Andreas Gampe29e81422017-05-15 16:29:32 -07001498 if (UNLIKELY(key <= last_key)) {
David Brazdil5469d342015-09-25 16:57:53 +01001499 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid sparse switch: last key=" << last_key
1500 << ", this=" << key;
1501 return false;
1502 }
1503 last_key = key;
1504 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001505 }
1506 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001507 /* verify each switch target */
Ian Rogersd81871c2011-10-03 13:57:23 -07001508 for (uint32_t targ = 0; targ < switch_count; targ++) {
Andreas Gampe53de99c2015-08-17 13:43:55 -07001509 int32_t offset = static_cast<int32_t>(switch_insns[targets_offset + targ * 2]) |
1510 static_cast<int32_t>(switch_insns[targets_offset + targ * 2 + 1] << 16);
Ian Rogersd81871c2011-10-03 13:57:23 -07001511 int32_t abs_offset = cur_offset + offset;
Andreas Gampe29e81422017-05-15 16:29:32 -07001512 if (UNLIKELY(abs_offset < 0 ||
1513 abs_offset >= static_cast<int32_t>(insn_count) ||
1514 !GetInstructionFlags(abs_offset).IsOpcode())) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001515 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch target " << offset
1516 << " (-> " << reinterpret_cast<void*>(abs_offset) << ") at "
1517 << reinterpret_cast<void*>(cur_offset)
1518 << "[" << targ << "]";
jeffhaoba5ebb92011-08-25 17:24:37 -07001519 return false;
1520 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07001521 GetInstructionFlags(abs_offset).SetBranchTarget();
Ian Rogersd81871c2011-10-03 13:57:23 -07001522 }
1523 return true;
1524}
1525
Ian Rogers776ac1f2012-04-13 23:36:36 -07001526bool MethodVerifier::CheckVarArgRegs(uint32_t vA, uint32_t arg[]) {
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001527 uint16_t registers_size = code_item_accessor_.RegistersSize();
Ian Rogersd81871c2011-10-03 13:57:23 -07001528 for (uint32_t idx = 0; idx < vA; idx++) {
Andreas Gampe29e81422017-05-15 16:29:32 -07001529 if (UNLIKELY(arg[idx] >= registers_size)) {
jeffhaod5347e02012-03-22 17:25:05 -07001530 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index (" << arg[idx]
1531 << ") in non-range invoke (>= " << registers_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001532 return false;
1533 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001534 }
1535
1536 return true;
1537}
1538
Ian Rogers776ac1f2012-04-13 23:36:36 -07001539bool MethodVerifier::CheckVarArgRangeRegs(uint32_t vA, uint32_t vC) {
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001540 uint16_t registers_size = code_item_accessor_.RegistersSize();
Ian Rogersd81871c2011-10-03 13:57:23 -07001541 // vA/vC are unsigned 8-bit/16-bit quantities for /range instructions, so there's no risk of
1542 // integer overflow when adding them here.
Andreas Gampe29e81422017-05-15 16:29:32 -07001543 if (UNLIKELY(vA + vC > registers_size)) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001544 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index " << vA << "+" << vC
1545 << " in range invoke (> " << registers_size << ")";
jeffhaoba5ebb92011-08-25 17:24:37 -07001546 return false;
1547 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001548 return true;
1549}
1550
Ian Rogers776ac1f2012-04-13 23:36:36 -07001551bool MethodVerifier::VerifyCodeFlow() {
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001552 const uint16_t registers_size = code_item_accessor_.RegistersSize();
jeffhaobdb76512011-09-07 11:43:16 -07001553
Ian Rogersd81871c2011-10-03 13:57:23 -07001554 /* Create and initialize table holding register status */
Brian Carlstrom93c33962013-07-26 10:37:43 -07001555 reg_table_.Init(kTrackCompilerInterestPoints,
1556 insn_flags_.get(),
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001557 code_item_accessor_.InsnsSizeInCodeUnits(),
Brian Carlstrom93c33962013-07-26 10:37:43 -07001558 registers_size,
1559 this);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07001560
Ian Rogersd0fbd852013-09-24 18:17:04 -07001561 work_line_.reset(RegisterLine::Create(registers_size, this));
1562 saved_line_.reset(RegisterLine::Create(registers_size, this));
jeffhaobdb76512011-09-07 11:43:16 -07001563
Ian Rogersd81871c2011-10-03 13:57:23 -07001564 /* Initialize register types of method arguments. */
1565 if (!SetTypesFromSignature()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001566 DCHECK_NE(failures_.size(), 0U);
1567 std::string prepend("Bad signature in ");
David Sehr709b0702016-10-13 09:12:37 -07001568 prepend += dex_file_->PrettyMethod(dex_method_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001569 PrependToLastFailMessage(prepend);
Ian Rogersd81871c2011-10-03 13:57:23 -07001570 return false;
1571 }
Andreas Gamped5ad72f2015-06-26 17:33:47 -07001572 // We may have a runtime failure here, clear.
1573 have_pending_runtime_throw_failure_ = false;
1574
Ian Rogersd81871c2011-10-03 13:57:23 -07001575 /* Perform code flow verification. */
1576 if (!CodeFlowVerifyMethod()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001577 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -07001578 return false;
jeffhaobdb76512011-09-07 11:43:16 -07001579 }
jeffhaobdb76512011-09-07 11:43:16 -07001580 return true;
1581}
1582
Ian Rogersad0b3a32012-04-16 14:50:24 -07001583std::ostream& MethodVerifier::DumpFailures(std::ostream& os) {
1584 DCHECK_EQ(failures_.size(), failure_messages_.size());
Jeff Hao4137f482013-11-22 11:44:57 -08001585 for (size_t i = 0; i < failures_.size(); ++i) {
1586 os << failure_messages_[i]->str() << "\n";
Ian Rogersad0b3a32012-04-16 14:50:24 -07001587 }
1588 return os;
1589}
1590
Ian Rogers776ac1f2012-04-13 23:36:36 -07001591void MethodVerifier::Dump(std::ostream& os) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001592 VariableIndentationOutputStream vios(&os);
1593 Dump(&vios);
1594}
1595
1596void MethodVerifier::Dump(VariableIndentationOutputStream* vios) {
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001597 if (!code_item_accessor_.HasCodeItem()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001598 vios->Stream() << "Native method\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001599 return;
jeffhaobdb76512011-09-07 11:43:16 -07001600 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001601 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001602 vios->Stream() << "Register Types:\n";
1603 ScopedIndentation indent1(vios);
1604 reg_types_.Dump(vios->Stream());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001605 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001606 vios->Stream() << "Dumping instructions and register lines:\n";
1607 ScopedIndentation indent1(vios);
Mathieu Chartier1d2d4ff2017-09-23 16:11:06 -07001608
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001609 for (const DexInstructionPcPair& inst : code_item_accessor_) {
Mathieu Chartier2b2bef22017-10-26 17:10:19 -07001610 const size_t dex_pc = inst.DexPc();
Andreas Gampe077d9db2018-01-19 18:54:14 -08001611
1612 // Might be asked to dump before the table is initialized.
1613 if (reg_table_.IsInitialized()) {
1614 RegisterLine* reg_line = reg_table_.GetLine(dex_pc);
1615 if (reg_line != nullptr) {
1616 vios->Stream() << reg_line->Dump(this) << "\n";
1617 }
jeffhaobdb76512011-09-07 11:43:16 -07001618 }
Andreas Gampe077d9db2018-01-19 18:54:14 -08001619
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001620 vios->Stream()
Mathieu Chartierde40d472015-10-15 17:47:48 -07001621 << StringPrintf("0x%04zx", dex_pc) << ": " << GetInstructionFlags(dex_pc).ToString() << " ";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001622 const bool kDumpHexOfInstruction = false;
1623 if (kDumpHexOfInstruction) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001624 vios->Stream() << inst->DumpHex(5) << " ";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001625 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001626 vios->Stream() << inst->DumpString(dex_file_) << "\n";
jeffhaoba5ebb92011-08-25 17:24:37 -07001627 }
jeffhaobdb76512011-09-07 11:43:16 -07001628}
1629
Ian Rogersd81871c2011-10-03 13:57:23 -07001630static bool IsPrimitiveDescriptor(char descriptor) {
1631 switch (descriptor) {
jeffhaobdb76512011-09-07 11:43:16 -07001632 case 'I':
1633 case 'C':
1634 case 'S':
1635 case 'B':
1636 case 'Z':
jeffhaobdb76512011-09-07 11:43:16 -07001637 case 'F':
1638 case 'D':
1639 case 'J':
Ian Rogersd81871c2011-10-03 13:57:23 -07001640 return true;
jeffhaobdb76512011-09-07 11:43:16 -07001641 default:
1642 return false;
1643 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001644}
1645
Ian Rogers776ac1f2012-04-13 23:36:36 -07001646bool MethodVerifier::SetTypesFromSignature() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001647 RegisterLine* reg_line = reg_table_.GetLine(0);
Andreas Gampeef0b1a12015-06-19 20:37:46 -07001648
1649 // Should have been verified earlier.
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001650 DCHECK_GE(code_item_accessor_.RegistersSize(), code_item_accessor_.InsSize());
Andreas Gampeef0b1a12015-06-19 20:37:46 -07001651
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001652 uint32_t arg_start = code_item_accessor_.RegistersSize() - code_item_accessor_.InsSize();
1653 size_t expected_args = code_item_accessor_.InsSize(); /* long/double count as two */
jeffhaobdb76512011-09-07 11:43:16 -07001654
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001655 // Include the "this" pointer.
Ian Rogersd81871c2011-10-03 13:57:23 -07001656 size_t cur_arg = 0;
Ian Rogersad0b3a32012-04-16 14:50:24 -07001657 if (!IsStatic()) {
Andreas Gampeef0b1a12015-06-19 20:37:46 -07001658 if (expected_args == 0) {
1659 // Expect at least a receiver.
1660 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected 0 args, but method is not static";
1661 return false;
1662 }
1663
Ian Rogersd81871c2011-10-03 13:57:23 -07001664 // If this is a constructor for a class other than java.lang.Object, mark the first ("this")
1665 // argument as uninitialized. This restricts field access until the superclass constructor is
1666 // called.
Ian Rogersd8f69b02014-09-10 21:43:52 +00001667 const RegType& declaring_class = GetDeclaringClass();
Andreas Gampef10b6e12015-08-12 10:48:12 -07001668 if (IsConstructor()) {
1669 if (declaring_class.IsJavaLangObject()) {
1670 // "this" is implicitly initialized.
1671 reg_line->SetThisInitialized();
Andreas Gampead238ce2015-08-24 21:13:08 -07001672 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, declaring_class);
Andreas Gampef10b6e12015-08-12 10:48:12 -07001673 } else {
Andreas Gampead238ce2015-08-24 21:13:08 -07001674 reg_line->SetRegisterType<LockOp::kClear>(
1675 this,
1676 arg_start + cur_arg,
1677 reg_types_.UninitializedThisArgument(declaring_class));
Andreas Gampef10b6e12015-08-12 10:48:12 -07001678 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001679 } else {
Andreas Gampead238ce2015-08-24 21:13:08 -07001680 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, declaring_class);
jeffhaobdb76512011-09-07 11:43:16 -07001681 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001682 cur_arg++;
jeffhaobdb76512011-09-07 11:43:16 -07001683 }
1684
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001685 const DexFile::ProtoId& proto_id =
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001686 dex_file_->GetMethodPrototype(dex_file_->GetMethodId(dex_method_idx_));
Ian Rogers0571d352011-11-03 19:51:38 -07001687 DexFileParameterIterator iterator(*dex_file_, proto_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07001688
1689 for (; iterator.HasNext(); iterator.Next()) {
1690 const char* descriptor = iterator.GetDescriptor();
Ian Rogers7b078e82014-09-10 14:44:24 -07001691 if (descriptor == nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001692 LOG(FATAL) << "Null descriptor";
1693 }
1694 if (cur_arg >= expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07001695 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1696 << " args, found more (" << descriptor << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001697 return false;
1698 }
1699 switch (descriptor[0]) {
1700 case 'L':
1701 case '[':
1702 // We assume that reference arguments are initialized. The only way it could be otherwise
1703 // (assuming the caller was verified) is if the current method is <init>, but in that case
1704 // it's effectively considered initialized the instant we reach here (in the sense that we
1705 // can return without doing anything or call virtual methods).
1706 {
Andreas Gampe98be1a92017-08-28 08:25:45 -07001707 // Note: don't check access. No error would be thrown for declaring or passing an
1708 // inaccessible class. Only actual accesses to fields or methods will.
1709 const RegType& reg_type = ResolveClass<CheckAccess::kNo>(iterator.GetTypeIdx());
Sebastien Hertz2ed76f92014-04-22 17:11:08 +02001710 if (!reg_type.IsNonZeroReferenceTypes()) {
1711 DCHECK(HasFailures());
1712 return false;
1713 }
Andreas Gampead238ce2015-08-24 21:13:08 -07001714 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001715 }
1716 break;
1717 case 'Z':
Andreas Gampead238ce2015-08-24 21:13:08 -07001718 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Boolean());
Ian Rogersd81871c2011-10-03 13:57:23 -07001719 break;
1720 case 'C':
Andreas Gampead238ce2015-08-24 21:13:08 -07001721 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Char());
Ian Rogersd81871c2011-10-03 13:57:23 -07001722 break;
1723 case 'B':
Andreas Gampead238ce2015-08-24 21:13:08 -07001724 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Byte());
Ian Rogersd81871c2011-10-03 13:57:23 -07001725 break;
1726 case 'I':
Andreas Gampead238ce2015-08-24 21:13:08 -07001727 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Integer());
Ian Rogersd81871c2011-10-03 13:57:23 -07001728 break;
1729 case 'S':
Andreas Gampead238ce2015-08-24 21:13:08 -07001730 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Short());
Ian Rogersd81871c2011-10-03 13:57:23 -07001731 break;
1732 case 'F':
Andreas Gampead238ce2015-08-24 21:13:08 -07001733 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Float());
Ian Rogersd81871c2011-10-03 13:57:23 -07001734 break;
1735 case 'J':
1736 case 'D': {
Andreas Gampe77cd4d62014-06-19 17:29:48 -07001737 if (cur_arg + 1 >= expected_args) {
1738 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1739 << " args, found more (" << descriptor << ")";
1740 return false;
1741 }
1742
Ian Rogers7b078e82014-09-10 14:44:24 -07001743 const RegType* lo_half;
1744 const RegType* hi_half;
1745 if (descriptor[0] == 'J') {
1746 lo_half = &reg_types_.LongLo();
1747 hi_half = &reg_types_.LongHi();
1748 } else {
1749 lo_half = &reg_types_.DoubleLo();
1750 hi_half = &reg_types_.DoubleHi();
1751 }
1752 reg_line->SetRegisterTypeWide(this, arg_start + cur_arg, *lo_half, *hi_half);
Ian Rogersd81871c2011-10-03 13:57:23 -07001753 cur_arg++;
1754 break;
1755 }
1756 default:
Brian Carlstrom93c33962013-07-26 10:37:43 -07001757 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected signature type char '"
1758 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001759 return false;
1760 }
1761 cur_arg++;
1762 }
1763 if (cur_arg != expected_args) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001764 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1765 << " arguments, found " << cur_arg;
Ian Rogersd81871c2011-10-03 13:57:23 -07001766 return false;
1767 }
1768 const char* descriptor = dex_file_->GetReturnTypeDescriptor(proto_id);
1769 // Validate return type. We don't do the type lookup; just want to make sure that it has the right
1770 // format. Only major difference from the method argument format is that 'V' is supported.
1771 bool result;
1772 if (IsPrimitiveDescriptor(descriptor[0]) || descriptor[0] == 'V') {
1773 result = descriptor[1] == '\0';
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001774 } else if (descriptor[0] == '[') { // single/multi-dimensional array of object/primitive
Ian Rogersd81871c2011-10-03 13:57:23 -07001775 size_t i = 0;
1776 do {
1777 i++;
1778 } while (descriptor[i] == '['); // process leading [
1779 if (descriptor[i] == 'L') { // object array
1780 do {
1781 i++; // find closing ;
1782 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1783 result = descriptor[i] == ';';
1784 } else { // primitive array
1785 result = IsPrimitiveDescriptor(descriptor[i]) && descriptor[i + 1] == '\0';
1786 }
1787 } else if (descriptor[0] == 'L') {
1788 // could be more thorough here, but shouldn't be required
1789 size_t i = 0;
1790 do {
1791 i++;
1792 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1793 result = descriptor[i] == ';';
1794 } else {
1795 result = false;
1796 }
1797 if (!result) {
jeffhaod5347e02012-03-22 17:25:05 -07001798 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected char in return type descriptor '"
1799 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001800 }
1801 return result;
jeffhaobdb76512011-09-07 11:43:16 -07001802}
1803
Ian Rogers776ac1f2012-04-13 23:36:36 -07001804bool MethodVerifier::CodeFlowVerifyMethod() {
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001805 const uint16_t* insns = code_item_accessor_.Insns();
1806 const uint32_t insns_size = code_item_accessor_.InsnsSizeInCodeUnits();
jeffhaoba5ebb92011-08-25 17:24:37 -07001807
jeffhaobdb76512011-09-07 11:43:16 -07001808 /* Begin by marking the first instruction as "changed". */
Mathieu Chartierde40d472015-10-15 17:47:48 -07001809 GetInstructionFlags(0).SetChanged();
Ian Rogersd81871c2011-10-03 13:57:23 -07001810 uint32_t start_guess = 0;
jeffhaoba5ebb92011-08-25 17:24:37 -07001811
jeffhaobdb76512011-09-07 11:43:16 -07001812 /* Continue until no instructions are marked "changed". */
1813 while (true) {
Mathieu Chartier4306ef82014-12-19 18:41:47 -08001814 if (allow_thread_suspension_) {
1815 self_->AllowThreadSuspension();
1816 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001817 // Find the first marked one. Use "start_guess" as a way to find one quickly.
1818 uint32_t insn_idx = start_guess;
1819 for (; insn_idx < insns_size; insn_idx++) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07001820 if (GetInstructionFlags(insn_idx).IsChanged())
jeffhaobdb76512011-09-07 11:43:16 -07001821 break;
1822 }
jeffhaobdb76512011-09-07 11:43:16 -07001823 if (insn_idx == insns_size) {
1824 if (start_guess != 0) {
1825 /* try again, starting from the top */
1826 start_guess = 0;
1827 continue;
1828 } else {
1829 /* all flags are clear */
1830 break;
1831 }
1832 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001833 // We carry the working set of registers from instruction to instruction. If this address can
1834 // be the target of a branch (or throw) instruction, or if we're skipping around chasing
1835 // "changed" flags, we need to load the set of registers from the table.
1836 // Because we always prefer to continue on to the next instruction, we should never have a
1837 // situation where we have a stray "changed" flag set on an instruction that isn't a branch
1838 // target.
1839 work_insn_idx_ = insn_idx;
Mathieu Chartierde40d472015-10-15 17:47:48 -07001840 if (GetInstructionFlags(insn_idx).IsBranchTarget()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001841 work_line_->CopyFromLine(reg_table_.GetLine(insn_idx));
Ian Rogersebbdd872014-07-07 23:53:08 -07001842 } else if (kIsDebugBuild) {
jeffhaobdb76512011-09-07 11:43:16 -07001843 /*
1844 * Sanity check: retrieve the stored register line (assuming
1845 * a full table) and make sure it actually matches.
1846 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001847 RegisterLine* register_line = reg_table_.GetLine(insn_idx);
Ian Rogers7b078e82014-09-10 14:44:24 -07001848 if (register_line != nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001849 if (work_line_->CompareLine(register_line) != 0) {
1850 Dump(std::cout);
1851 std::cout << info_messages_.str();
David Sehr709b0702016-10-13 09:12:37 -07001852 LOG(FATAL) << "work_line diverged in " << dex_file_->PrettyMethod(dex_method_idx_)
Elliott Hughesc073b072012-05-24 19:29:17 -07001853 << "@" << reinterpret_cast<void*>(work_insn_idx_) << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07001854 << " work_line=" << work_line_->Dump(this) << "\n"
1855 << " expected=" << register_line->Dump(this);
Ian Rogersd81871c2011-10-03 13:57:23 -07001856 }
jeffhaobdb76512011-09-07 11:43:16 -07001857 }
jeffhaobdb76512011-09-07 11:43:16 -07001858 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001859 if (!CodeFlowVerifyInstruction(&start_guess)) {
David Sehr709b0702016-10-13 09:12:37 -07001860 std::string prepend(dex_file_->PrettyMethod(dex_method_idx_));
Ian Rogersad0b3a32012-04-16 14:50:24 -07001861 prepend += " failed to verify: ";
1862 PrependToLastFailMessage(prepend);
jeffhaoba5ebb92011-08-25 17:24:37 -07001863 return false;
1864 }
jeffhaobdb76512011-09-07 11:43:16 -07001865 /* Clear "changed" and mark as visited. */
Mathieu Chartierde40d472015-10-15 17:47:48 -07001866 GetInstructionFlags(insn_idx).SetVisited();
1867 GetInstructionFlags(insn_idx).ClearChanged();
jeffhaobdb76512011-09-07 11:43:16 -07001868 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001869
Andreas Gampe92d77202017-12-06 20:49:00 -08001870 if (UNLIKELY(VLOG_IS_ON(verifier_debug))) {
jeffhaobdb76512011-09-07 11:43:16 -07001871 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001872 * Scan for dead code. There's nothing "evil" about dead code
jeffhaobdb76512011-09-07 11:43:16 -07001873 * (besides the wasted space), but it indicates a flaw somewhere
1874 * down the line, possibly in the verifier.
1875 *
1876 * If we've substituted "always throw" instructions into the stream,
1877 * we are almost certainly going to have some dead code.
1878 */
1879 int dead_start = -1;
Mathieu Chartier1d2d4ff2017-09-23 16:11:06 -07001880
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001881 for (const DexInstructionPcPair& inst : code_item_accessor_) {
Mathieu Chartier2b2bef22017-10-26 17:10:19 -07001882 const uint32_t insn_idx = inst.DexPc();
jeffhaobdb76512011-09-07 11:43:16 -07001883 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001884 * Switch-statement data doesn't get "visited" by scanner. It
jeffhaobdb76512011-09-07 11:43:16 -07001885 * may or may not be preceded by a padding NOP (for alignment).
1886 */
1887 if (insns[insn_idx] == Instruction::kPackedSwitchSignature ||
1888 insns[insn_idx] == Instruction::kSparseSwitchSignature ||
1889 insns[insn_idx] == Instruction::kArrayDataSignature ||
Elliott Hughes380aaa72012-07-09 14:33:15 -07001890 (insns[insn_idx] == Instruction::NOP && (insn_idx + 1 < insns_size) &&
jeffhaobdb76512011-09-07 11:43:16 -07001891 (insns[insn_idx + 1] == Instruction::kPackedSwitchSignature ||
1892 insns[insn_idx + 1] == Instruction::kSparseSwitchSignature ||
1893 insns[insn_idx + 1] == Instruction::kArrayDataSignature))) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07001894 GetInstructionFlags(insn_idx).SetVisited();
jeffhaobdb76512011-09-07 11:43:16 -07001895 }
1896
Mathieu Chartierde40d472015-10-15 17:47:48 -07001897 if (!GetInstructionFlags(insn_idx).IsVisited()) {
Mathieu Chartier1d2d4ff2017-09-23 16:11:06 -07001898 if (dead_start < 0) {
jeffhaobdb76512011-09-07 11:43:16 -07001899 dead_start = insn_idx;
Mathieu Chartier1d2d4ff2017-09-23 16:11:06 -07001900 }
jeffhaobdb76512011-09-07 11:43:16 -07001901 } else if (dead_start >= 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001902 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start)
1903 << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaobdb76512011-09-07 11:43:16 -07001904 dead_start = -1;
1905 }
1906 }
1907 if (dead_start >= 0) {
Mathieu Chartier1d2d4ff2017-09-23 16:11:06 -07001908 LogVerifyInfo()
1909 << "dead code " << reinterpret_cast<void*>(dead_start)
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001910 << "-" << reinterpret_cast<void*>(code_item_accessor_.InsnsSizeInCodeUnits() - 1);
jeffhaoba5ebb92011-08-25 17:24:37 -07001911 }
Ian Rogersc9e463c2013-06-05 16:52:26 -07001912 // To dump the state of the verify after a method, do something like:
David Sehr709b0702016-10-13 09:12:37 -07001913 // if (dex_file_->PrettyMethod(dex_method_idx_) ==
Ian Rogersc9e463c2013-06-05 16:52:26 -07001914 // "boolean java.lang.String.equals(java.lang.Object)") {
1915 // LOG(INFO) << info_messages_.str();
1916 // }
jeffhaoba5ebb92011-08-25 17:24:37 -07001917 }
jeffhaobdb76512011-09-07 11:43:16 -07001918 return true;
1919}
1920
Andreas Gampe68df3202015-06-22 11:35:46 -07001921// Returns the index of the first final instance field of the given class, or kDexNoIndex if there
1922// is no such field.
Andreas Gampea5b09a62016-11-17 15:21:22 -08001923static uint32_t GetFirstFinalInstanceFieldIndex(const DexFile& dex_file, dex::TypeIndex type_idx) {
Andreas Gampe68df3202015-06-22 11:35:46 -07001924 const DexFile::ClassDef* class_def = dex_file.FindClassDef(type_idx);
1925 DCHECK(class_def != nullptr);
1926 const uint8_t* class_data = dex_file.GetClassData(*class_def);
1927 DCHECK(class_data != nullptr);
1928 ClassDataItemIterator it(dex_file, class_data);
Mathieu Chartiere17cf242017-06-19 11:05:51 -07001929 it.SkipStaticFields();
Andreas Gampe68df3202015-06-22 11:35:46 -07001930 while (it.HasNextInstanceField()) {
1931 if ((it.GetFieldAccessFlags() & kAccFinal) != 0) {
1932 return it.GetMemberIndex();
1933 }
1934 it.Next();
1935 }
Andreas Gampee2abbc62017-09-15 11:59:26 -07001936 return dex::kDexNoIndex;
Andreas Gampe68df3202015-06-22 11:35:46 -07001937}
1938
Andreas Gampea727e372015-08-25 09:22:37 -07001939// Setup a register line for the given return instruction.
1940static void AdjustReturnLine(MethodVerifier* verifier,
1941 const Instruction* ret_inst,
1942 RegisterLine* line) {
1943 Instruction::Code opcode = ret_inst->Opcode();
1944
1945 switch (opcode) {
1946 case Instruction::RETURN_VOID:
1947 case Instruction::RETURN_VOID_NO_BARRIER:
1948 SafelyMarkAllRegistersAsConflicts(verifier, line);
1949 break;
1950
1951 case Instruction::RETURN:
1952 case Instruction::RETURN_OBJECT:
1953 line->MarkAllRegistersAsConflictsExcept(verifier, ret_inst->VRegA_11x());
1954 break;
1955
1956 case Instruction::RETURN_WIDE:
1957 line->MarkAllRegistersAsConflictsExceptWide(verifier, ret_inst->VRegA_11x());
1958 break;
1959
1960 default:
1961 LOG(FATAL) << "Unknown return opcode " << opcode;
1962 UNREACHABLE();
1963 }
1964}
1965
Ian Rogers776ac1f2012-04-13 23:36:36 -07001966bool MethodVerifier::CodeFlowVerifyInstruction(uint32_t* start_guess) {
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001967 // If we're doing FindLocksAtDexPc, check whether we're at the dex pc we care about.
1968 // We want the state _before_ the instruction, for the case where the dex pc we're
1969 // interested in is itself a monitor-enter instruction (which is a likely place
1970 // for a thread to be suspended).
Ian Rogers7b078e82014-09-10 14:44:24 -07001971 if (monitor_enter_dex_pcs_ != nullptr && work_insn_idx_ == interesting_dex_pc_) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001972 monitor_enter_dex_pcs_->clear(); // The new work line is more accurate than the previous one.
Andreas Gampeaaf0d382017-11-27 14:10:21 -08001973
1974 std::map<uint32_t, DexLockInfo> depth_to_lock_info;
1975 auto collector = [&](uint32_t dex_reg, uint32_t depth) {
1976 auto insert_pair = depth_to_lock_info.emplace(depth, DexLockInfo(depth));
1977 auto it = insert_pair.first;
1978 auto set_insert_pair = it->second.dex_registers.insert(dex_reg);
1979 DCHECK(set_insert_pair.second);
1980 };
1981 work_line_->IterateRegToLockDepths(collector);
1982 for (auto& pair : depth_to_lock_info) {
1983 monitor_enter_dex_pcs_->push_back(pair.second);
1984 // Map depth to dex PC.
1985 (*monitor_enter_dex_pcs_)[monitor_enter_dex_pcs_->size() - 1].dex_pc =
1986 work_line_->GetMonitorEnterDexPc(pair.second.dex_pc);
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001987 }
1988 }
1989
jeffhaobdb76512011-09-07 11:43:16 -07001990 /*
1991 * Once we finish decoding the instruction, we need to figure out where
jeffhaod1f0fde2011-09-08 17:25:33 -07001992 * we can go from here. There are three possible ways to transfer
jeffhaobdb76512011-09-07 11:43:16 -07001993 * control to another statement:
1994 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001995 * (1) Continue to the next instruction. Applies to all but
jeffhaobdb76512011-09-07 11:43:16 -07001996 * unconditional branches, method returns, and exception throws.
jeffhaod1f0fde2011-09-08 17:25:33 -07001997 * (2) Branch to one or more possible locations. Applies to branches
jeffhaobdb76512011-09-07 11:43:16 -07001998 * and switch statements.
jeffhaod1f0fde2011-09-08 17:25:33 -07001999 * (3) Exception handlers. Applies to any instruction that can
jeffhaobdb76512011-09-07 11:43:16 -07002000 * throw an exception that is handled by an encompassing "try"
2001 * block.
2002 *
2003 * We can also return, in which case there is no successor instruction
2004 * from this point.
2005 *
Elliott Hughesadb8c672012-03-06 16:49:32 -08002006 * The behavior can be determined from the opcode flags.
jeffhaobdb76512011-09-07 11:43:16 -07002007 */
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08002008 const uint16_t* insns = code_item_accessor_.Insns() + work_insn_idx_;
Ian Rogersd81871c2011-10-03 13:57:23 -07002009 const Instruction* inst = Instruction::At(insns);
Ian Rogersa75a0132012-09-28 11:41:42 -07002010 int opcode_flags = Instruction::FlagsOf(inst->Opcode());
jeffhaobdb76512011-09-07 11:43:16 -07002011
jeffhaobdb76512011-09-07 11:43:16 -07002012 int32_t branch_target = 0;
jeffhaobdb76512011-09-07 11:43:16 -07002013 bool just_set_result = false;
Andreas Gampe92d77202017-12-06 20:49:00 -08002014 if (UNLIKELY(VLOG_IS_ON(verifier_debug))) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002015 // Generate processing back trace to debug verifier
Elliott Hughesc073b072012-05-24 19:29:17 -07002016 LogVerifyInfo() << "Processing " << inst->DumpString(dex_file_) << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07002017 << work_line_->Dump(this) << "\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07002018 }
jeffhaobdb76512011-09-07 11:43:16 -07002019
2020 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002021 * Make a copy of the previous register state. If the instruction
jeffhaobdb76512011-09-07 11:43:16 -07002022 * can throw an exception, we will copy/merge this into the "catch"
2023 * address rather than work_line, because we don't want the result
2024 * from the "successful" code path (e.g. a check-cast that "improves"
2025 * a type) to be visible to the exception handler.
2026 */
Ian Rogers776ac1f2012-04-13 23:36:36 -07002027 if ((opcode_flags & Instruction::kThrow) != 0 && CurrentInsnFlags()->IsInTry()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002028 saved_line_->CopyFromLine(work_line_.get());
Ian Rogers1ff3c982014-08-12 02:30:58 -07002029 } else if (kIsDebugBuild) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002030 saved_line_->FillWithGarbage();
jeffhaobdb76512011-09-07 11:43:16 -07002031 }
Andreas Gamped12e7822015-06-25 10:26:40 -07002032 DCHECK(!have_pending_runtime_throw_failure_); // Per-instruction flag, should not be set here.
jeffhaobdb76512011-09-07 11:43:16 -07002033
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07002034
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002035 // We need to ensure the work line is consistent while performing validation. When we spot a
2036 // peephole pattern we compute a new line for either the fallthrough instruction or the
2037 // branch target.
Mathieu Chartier361e04a2016-02-16 14:06:35 -08002038 RegisterLineArenaUniquePtr branch_line;
2039 RegisterLineArenaUniquePtr fallthrough_line;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002040
Sebastien Hertz5243e912013-05-21 10:55:07 +02002041 switch (inst->Opcode()) {
jeffhaobdb76512011-09-07 11:43:16 -07002042 case Instruction::NOP:
2043 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002044 * A "pure" NOP has no effect on anything. Data tables start with
jeffhaobdb76512011-09-07 11:43:16 -07002045 * a signature that looks like a NOP; if we see one of these in
2046 * the course of executing code then we have a problem.
2047 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002048 if (inst->VRegA_10x() != 0) {
jeffhaod5347e02012-03-22 17:25:05 -07002049 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "encountered data table in instruction stream";
jeffhaobdb76512011-09-07 11:43:16 -07002050 }
2051 break;
2052
2053 case Instruction::MOVE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002054 work_line_->CopyRegister1(this, inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategory1nr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02002055 break;
jeffhaobdb76512011-09-07 11:43:16 -07002056 case Instruction::MOVE_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07002057 work_line_->CopyRegister1(this, inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategory1nr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02002058 break;
jeffhaobdb76512011-09-07 11:43:16 -07002059 case Instruction::MOVE_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07002060 work_line_->CopyRegister1(this, inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategory1nr);
jeffhaobdb76512011-09-07 11:43:16 -07002061 break;
2062 case Instruction::MOVE_WIDE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002063 work_line_->CopyRegister2(this, inst->VRegA_12x(), inst->VRegB_12x());
Sebastien Hertz5243e912013-05-21 10:55:07 +02002064 break;
jeffhaobdb76512011-09-07 11:43:16 -07002065 case Instruction::MOVE_WIDE_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07002066 work_line_->CopyRegister2(this, inst->VRegA_22x(), inst->VRegB_22x());
Sebastien Hertz5243e912013-05-21 10:55:07 +02002067 break;
jeffhaobdb76512011-09-07 11:43:16 -07002068 case Instruction::MOVE_WIDE_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07002069 work_line_->CopyRegister2(this, inst->VRegA_32x(), inst->VRegB_32x());
jeffhaobdb76512011-09-07 11:43:16 -07002070 break;
2071 case Instruction::MOVE_OBJECT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002072 work_line_->CopyRegister1(this, inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategoryRef);
Sebastien Hertz5243e912013-05-21 10:55:07 +02002073 break;
jeffhaobdb76512011-09-07 11:43:16 -07002074 case Instruction::MOVE_OBJECT_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07002075 work_line_->CopyRegister1(this, inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategoryRef);
Sebastien Hertz5243e912013-05-21 10:55:07 +02002076 break;
jeffhaobdb76512011-09-07 11:43:16 -07002077 case Instruction::MOVE_OBJECT_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07002078 work_line_->CopyRegister1(this, inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategoryRef);
jeffhaobdb76512011-09-07 11:43:16 -07002079 break;
2080
2081 /*
2082 * The move-result instructions copy data out of a "pseudo-register"
jeffhaod1f0fde2011-09-08 17:25:33 -07002083 * with the results from the last method invocation. In practice we
jeffhaobdb76512011-09-07 11:43:16 -07002084 * might want to hold the result in an actual CPU register, so the
2085 * Dalvik spec requires that these only appear immediately after an
2086 * invoke or filled-new-array.
2087 *
jeffhaod1f0fde2011-09-08 17:25:33 -07002088 * These calls invalidate the "result" register. (This is now
jeffhaobdb76512011-09-07 11:43:16 -07002089 * redundant with the reset done below, but it can make the debug info
2090 * easier to read in some cases.)
2091 */
2092 case Instruction::MOVE_RESULT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002093 work_line_->CopyResultRegister1(this, inst->VRegA_11x(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002094 break;
2095 case Instruction::MOVE_RESULT_WIDE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002096 work_line_->CopyResultRegister2(this, inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07002097 break;
2098 case Instruction::MOVE_RESULT_OBJECT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002099 work_line_->CopyResultRegister1(this, inst->VRegA_11x(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002100 break;
2101
Ian Rogersd81871c2011-10-03 13:57:23 -07002102 case Instruction::MOVE_EXCEPTION: {
Sebastien Hertz270a0e12015-01-16 19:49:09 +01002103 // We do not allow MOVE_EXCEPTION as the first instruction in a method. This is a simple case
2104 // where one entrypoint to the catch block is not actually an exception path.
2105 if (work_insn_idx_ == 0) {
2106 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "move-exception at pc 0x0";
2107 break;
2108 }
jeffhaobdb76512011-09-07 11:43:16 -07002109 /*
jeffhao60f83e32012-02-13 17:16:30 -08002110 * This statement can only appear as the first instruction in an exception handler. We verify
2111 * that as part of extracting the exception type from the catch block list.
jeffhaobdb76512011-09-07 11:43:16 -07002112 */
Ian Rogersd8f69b02014-09-10 21:43:52 +00002113 const RegType& res_type = GetCaughtExceptionType();
Andreas Gampead238ce2015-08-24 21:13:08 -07002114 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_11x(), res_type);
jeffhaobdb76512011-09-07 11:43:16 -07002115 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002116 }
jeffhaobdb76512011-09-07 11:43:16 -07002117 case Instruction::RETURN_VOID:
Andreas Gampef10b6e12015-08-12 10:48:12 -07002118 if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002119 if (!GetMethodReturnType().IsConflict()) {
jeffhaod5347e02012-03-22 17:25:05 -07002120 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07002121 }
jeffhaobdb76512011-09-07 11:43:16 -07002122 }
2123 break;
2124 case Instruction::RETURN:
Andreas Gampef10b6e12015-08-12 10:48:12 -07002125 if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) {
jeffhaobdb76512011-09-07 11:43:16 -07002126 /* check the method signature */
Ian Rogersd8f69b02014-09-10 21:43:52 +00002127 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07002128 if (!return_type.IsCategory1Types()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002129 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected non-category 1 return type "
2130 << return_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002131 } else {
2132 // Compilers may generate synthetic functions that write byte values into boolean fields.
2133 // Also, it may use integer values for boolean, byte, short, and character return types.
Sebastien Hertz5243e912013-05-21 10:55:07 +02002134 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07002135 const RegType& src_type = work_line_->GetRegisterType(this, vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07002136 bool use_src = ((return_type.IsBoolean() && src_type.IsByte()) ||
2137 ((return_type.IsBoolean() || return_type.IsByte() ||
2138 return_type.IsShort() || return_type.IsChar()) &&
2139 src_type.IsInteger()));
2140 /* check the register contents */
Ian Rogersad0b3a32012-04-16 14:50:24 -07002141 bool success =
Ian Rogers7b078e82014-09-10 14:44:24 -07002142 work_line_->VerifyRegisterType(this, vregA, use_src ? src_type : return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002143 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002144 AppendToLastFailMessage(StringPrintf(" return-1nr on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07002145 }
jeffhaobdb76512011-09-07 11:43:16 -07002146 }
2147 }
2148 break;
2149 case Instruction::RETURN_WIDE:
Andreas Gampef10b6e12015-08-12 10:48:12 -07002150 if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) {
jeffhaobdb76512011-09-07 11:43:16 -07002151 /* check the method signature */
Ian Rogersd8f69b02014-09-10 21:43:52 +00002152 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07002153 if (!return_type.IsCategory2Types()) {
jeffhaod5347e02012-03-22 17:25:05 -07002154 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-wide not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07002155 } else {
2156 /* check the register contents */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002157 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07002158 bool success = work_line_->VerifyRegisterType(this, vregA, return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002159 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002160 AppendToLastFailMessage(StringPrintf(" return-wide on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07002161 }
jeffhaobdb76512011-09-07 11:43:16 -07002162 }
2163 }
2164 break;
2165 case Instruction::RETURN_OBJECT:
Andreas Gampef10b6e12015-08-12 10:48:12 -07002166 if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) {
Ian Rogersd8f69b02014-09-10 21:43:52 +00002167 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07002168 if (!return_type.IsReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002169 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-object not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07002170 } else {
2171 /* return_type is the *expected* return type, not register value */
Andreas Gampeeed3a5d2017-11-29 14:58:34 -08002172 DCHECK(!return_type.IsZeroOrNull());
Ian Rogersd81871c2011-10-03 13:57:23 -07002173 DCHECK(!return_type.IsUninitializedReference());
Sebastien Hertz5243e912013-05-21 10:55:07 +02002174 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07002175 const RegType& reg_type = work_line_->GetRegisterType(this, vregA);
Andreas Gampea32210c2015-06-24 10:26:13 -07002176 // Disallow returning undefined, conflict & uninitialized values and verify that the
2177 // reference in vAA is an instance of the "return_type."
2178 if (reg_type.IsUndefined()) {
2179 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning undefined register";
2180 } else if (reg_type.IsConflict()) {
2181 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning register with conflict";
2182 } else if (reg_type.IsUninitializedTypes()) {
David Brazdil68b5c0b2016-01-19 14:25:29 +00002183 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning uninitialized object '"
Brian Carlstrom93c33962013-07-26 10:37:43 -07002184 << reg_type << "'";
Andreas Gampea4c98f22015-11-06 16:24:49 -08002185 } else if (!reg_type.IsReferenceTypes()) {
2186 // We really do expect a reference here.
2187 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-object returns a non-reference type "
2188 << reg_type;
David Brazdilca3c8c32016-09-06 14:04:48 +01002189 } else if (!return_type.IsAssignableFrom(reg_type, this)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07002190 if (reg_type.IsUnresolvedTypes() || return_type.IsUnresolvedTypes()) {
2191 Fail(VERIFY_ERROR_NO_CLASS) << " can't resolve returned type '" << return_type
2192 << "' or '" << reg_type << "'";
2193 } else {
Andreas Gampe16f149c2015-03-23 10:10:20 -07002194 bool soft_error = false;
2195 // Check whether arrays are involved. They will show a valid class status, even
2196 // if their components are erroneous.
2197 if (reg_type.IsArrayTypes() && return_type.IsArrayTypes()) {
David Brazdilca3c8c32016-09-06 14:04:48 +01002198 return_type.CanAssignArray(reg_type, reg_types_, class_loader_, this, &soft_error);
Andreas Gampe16f149c2015-03-23 10:10:20 -07002199 if (soft_error) {
2200 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "array with erroneous component type: "
2201 << reg_type << " vs " << return_type;
2202 }
2203 }
2204
2205 if (!soft_error) {
2206 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning '" << reg_type
2207 << "', but expected from declaration '" << return_type << "'";
2208 }
Jeff Haoa3faaf42013-09-03 19:07:00 -07002209 }
jeffhaobdb76512011-09-07 11:43:16 -07002210 }
2211 }
2212 }
2213 break;
2214
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07002215 /* could be boolean, int, float, or a null reference */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002216 case Instruction::CONST_4: {
2217 int32_t val = static_cast<int32_t>(inst->VRegB_11n() << 28) >> 28;
Andreas Gampead238ce2015-08-24 21:13:08 -07002218 work_line_->SetRegisterType<LockOp::kClear>(
2219 this, inst->VRegA_11n(), DetermineCat1Constant(val, need_precise_constants_));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07002220 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02002221 }
2222 case Instruction::CONST_16: {
2223 int16_t val = static_cast<int16_t>(inst->VRegB_21s());
Andreas Gampead238ce2015-08-24 21:13:08 -07002224 work_line_->SetRegisterType<LockOp::kClear>(
2225 this, inst->VRegA_21s(), DetermineCat1Constant(val, need_precise_constants_));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07002226 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02002227 }
Sebastien Hertz849600b2013-12-20 10:28:08 +01002228 case Instruction::CONST: {
2229 int32_t val = inst->VRegB_31i();
Andreas Gampead238ce2015-08-24 21:13:08 -07002230 work_line_->SetRegisterType<LockOp::kClear>(
2231 this, inst->VRegA_31i(), DetermineCat1Constant(val, need_precise_constants_));
jeffhaobdb76512011-09-07 11:43:16 -07002232 break;
Sebastien Hertz849600b2013-12-20 10:28:08 +01002233 }
2234 case Instruction::CONST_HIGH16: {
2235 int32_t val = static_cast<int32_t>(inst->VRegB_21h() << 16);
Andreas Gampead238ce2015-08-24 21:13:08 -07002236 work_line_->SetRegisterType<LockOp::kClear>(
2237 this, inst->VRegA_21h(), DetermineCat1Constant(val, need_precise_constants_));
jeffhaobdb76512011-09-07 11:43:16 -07002238 break;
Sebastien Hertz849600b2013-12-20 10:28:08 +01002239 }
jeffhaobdb76512011-09-07 11:43:16 -07002240 /* could be long or double; resolved upon use */
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002241 case Instruction::CONST_WIDE_16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002242 int64_t val = static_cast<int16_t>(inst->VRegB_21s());
Ian Rogersd8f69b02014-09-10 21:43:52 +00002243 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
2244 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07002245 work_line_->SetRegisterTypeWide(this, inst->VRegA_21s(), lo, hi);
jeffhaobdb76512011-09-07 11:43:16 -07002246 break;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002247 }
2248 case Instruction::CONST_WIDE_32: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002249 int64_t val = static_cast<int32_t>(inst->VRegB_31i());
Ian Rogersd8f69b02014-09-10 21:43:52 +00002250 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
2251 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07002252 work_line_->SetRegisterTypeWide(this, inst->VRegA_31i(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002253 break;
2254 }
2255 case Instruction::CONST_WIDE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002256 int64_t val = inst->VRegB_51l();
Ian Rogersd8f69b02014-09-10 21:43:52 +00002257 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
2258 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07002259 work_line_->SetRegisterTypeWide(this, inst->VRegA_51l(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002260 break;
2261 }
2262 case Instruction::CONST_WIDE_HIGH16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002263 int64_t val = static_cast<uint64_t>(inst->VRegB_21h()) << 48;
Ian Rogersd8f69b02014-09-10 21:43:52 +00002264 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
2265 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07002266 work_line_->SetRegisterTypeWide(this, inst->VRegA_21h(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002267 break;
2268 }
jeffhaobdb76512011-09-07 11:43:16 -07002269 case Instruction::CONST_STRING:
Andreas Gampead238ce2015-08-24 21:13:08 -07002270 work_line_->SetRegisterType<LockOp::kClear>(
2271 this, inst->VRegA_21c(), reg_types_.JavaLangString());
Sebastien Hertz5243e912013-05-21 10:55:07 +02002272 break;
jeffhaobdb76512011-09-07 11:43:16 -07002273 case Instruction::CONST_STRING_JUMBO:
Andreas Gampead238ce2015-08-24 21:13:08 -07002274 work_line_->SetRegisterType<LockOp::kClear>(
2275 this, inst->VRegA_31c(), reg_types_.JavaLangString());
jeffhaobdb76512011-09-07 11:43:16 -07002276 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002277 case Instruction::CONST_CLASS: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002278 // Get type from instruction if unresolved then we need an access check
2279 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Andreas Gampe98be1a92017-08-28 08:25:45 -07002280 const RegType& res_type = ResolveClass<CheckAccess::kYes>(dex::TypeIndex(inst->VRegB_21c()));
Ian Rogersad0b3a32012-04-16 14:50:24 -07002281 // Register holds class, ie its type is class, on error it will hold Conflict.
Andreas Gampead238ce2015-08-24 21:13:08 -07002282 work_line_->SetRegisterType<LockOp::kClear>(
2283 this, inst->VRegA_21c(), res_type.IsConflict() ? res_type
2284 : reg_types_.JavaLangClass());
jeffhaobdb76512011-09-07 11:43:16 -07002285 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002286 }
Orion Hodson2e599942017-09-22 16:17:41 +01002287 case Instruction::CONST_METHOD_HANDLE:
2288 work_line_->SetRegisterType<LockOp::kClear>(
2289 this, inst->VRegA_21c(), reg_types_.JavaLangInvokeMethodHandle());
2290 // TODO: add compiler support for const-method-{handle,type} (b/66890674)
2291 Fail(VERIFY_ERROR_FORCE_INTERPRETER);
2292 break;
2293 case Instruction::CONST_METHOD_TYPE:
2294 work_line_->SetRegisterType<LockOp::kClear>(
2295 this, inst->VRegA_21c(), reg_types_.JavaLangInvokeMethodType());
2296 // TODO: add compiler support for const-method-{handle,type} (b/66890674)
2297 Fail(VERIFY_ERROR_FORCE_INTERPRETER);
2298 break;
jeffhaobdb76512011-09-07 11:43:16 -07002299 case Instruction::MONITOR_ENTER:
Ian Rogers7b078e82014-09-10 14:44:24 -07002300 work_line_->PushMonitor(this, inst->VRegA_11x(), work_insn_idx_);
Andreas Gampec1474102015-08-18 08:57:44 -07002301 // Check whether the previous instruction is a move-object with vAA as a source, creating
2302 // untracked lock aliasing.
Mathieu Chartierde40d472015-10-15 17:47:48 -07002303 if (0 != work_insn_idx_ && !GetInstructionFlags(work_insn_idx_).IsBranchTarget()) {
Andreas Gampec1474102015-08-18 08:57:44 -07002304 uint32_t prev_idx = work_insn_idx_ - 1;
Mathieu Chartierde40d472015-10-15 17:47:48 -07002305 while (0 != prev_idx && !GetInstructionFlags(prev_idx).IsOpcode()) {
Andreas Gampec1474102015-08-18 08:57:44 -07002306 prev_idx--;
2307 }
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08002308 const Instruction& prev_inst = code_item_accessor_.InstructionAt(prev_idx);
Mathieu Chartier1d2d4ff2017-09-23 16:11:06 -07002309 switch (prev_inst.Opcode()) {
Andreas Gampec1474102015-08-18 08:57:44 -07002310 case Instruction::MOVE_OBJECT:
2311 case Instruction::MOVE_OBJECT_16:
2312 case Instruction::MOVE_OBJECT_FROM16:
Mathieu Chartier1d2d4ff2017-09-23 16:11:06 -07002313 if (prev_inst.VRegB() == inst->VRegA_11x()) {
Andreas Gampec1474102015-08-18 08:57:44 -07002314 // Redo the copy. This won't change the register types, but update the lock status
2315 // for the aliased register.
2316 work_line_->CopyRegister1(this,
Mathieu Chartier1d2d4ff2017-09-23 16:11:06 -07002317 prev_inst.VRegA(),
2318 prev_inst.VRegB(),
Andreas Gampec1474102015-08-18 08:57:44 -07002319 kTypeCategoryRef);
2320 }
2321 break;
2322
2323 default: // Other instruction types ignored.
2324 break;
2325 }
2326 }
jeffhaobdb76512011-09-07 11:43:16 -07002327 break;
2328 case Instruction::MONITOR_EXIT:
2329 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002330 * monitor-exit instructions are odd. They can throw exceptions,
jeffhaobdb76512011-09-07 11:43:16 -07002331 * but when they do they act as if they succeeded and the PC is
jeffhaod1f0fde2011-09-08 17:25:33 -07002332 * pointing to the following instruction. (This behavior goes back
jeffhaobdb76512011-09-07 11:43:16 -07002333 * to the need to handle asynchronous exceptions, a now-deprecated
2334 * feature that Dalvik doesn't support.)
2335 *
jeffhaod1f0fde2011-09-08 17:25:33 -07002336 * In practice we don't need to worry about this. The only
jeffhaobdb76512011-09-07 11:43:16 -07002337 * exceptions that can be thrown from monitor-exit are for a
jeffhaod1f0fde2011-09-08 17:25:33 -07002338 * null reference and -exit without a matching -enter. If the
jeffhaobdb76512011-09-07 11:43:16 -07002339 * structured locking checks are working, the former would have
2340 * failed on the -enter instruction, and the latter is impossible.
2341 *
2342 * This is fortunate, because issue 3221411 prevents us from
2343 * chasing the "can throw" path when monitor verification is
jeffhaod1f0fde2011-09-08 17:25:33 -07002344 * enabled. If we can fully verify the locking we can ignore
jeffhaobdb76512011-09-07 11:43:16 -07002345 * some catch blocks (which will show up as "dead" code when
2346 * we skip them here); if we can't, then the code path could be
2347 * "live" so we still need to check it.
2348 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002349 opcode_flags &= ~Instruction::kThrow;
Ian Rogers7b078e82014-09-10 14:44:24 -07002350 work_line_->PopMonitor(this, inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07002351 break;
Ian Rogers28ad40d2011-10-27 15:19:26 -07002352 case Instruction::CHECK_CAST:
Ian Rogersd81871c2011-10-03 13:57:23 -07002353 case Instruction::INSTANCE_OF: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002354 /*
2355 * If this instruction succeeds, we will "downcast" register vA to the type in vB. (This
2356 * could be a "upcast" -- not expected, so we don't try to address it.)
2357 *
2358 * If it fails, an exception is thrown, which we deal with later by ignoring the update to
Elliott Hughesadb8c672012-03-06 16:49:32 -08002359 * dec_insn.vA when branching to a handler.
Ian Rogers28ad40d2011-10-27 15:19:26 -07002360 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002361 const bool is_checkcast = (inst->Opcode() == Instruction::CHECK_CAST);
Andreas Gampea5b09a62016-11-17 15:21:22 -08002362 const dex::TypeIndex type_idx((is_checkcast) ? inst->VRegB_21c() : inst->VRegC_22c());
Andreas Gampe98be1a92017-08-28 08:25:45 -07002363 const RegType& res_type = ResolveClass<CheckAccess::kYes>(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002364 if (res_type.IsConflict()) {
Andreas Gampe00633eb2014-07-17 16:13:35 -07002365 // If this is a primitive type, fail HARD.
Vladimir Marko666ee3d2017-12-11 18:37:36 +00002366 ObjPtr<mirror::Class> klass = Runtime::Current()->GetClassLinker()->LookupResolvedType(
2367 type_idx, dex_cache_.Get(), class_loader_.Get());
Andreas Gampe00633eb2014-07-17 16:13:35 -07002368 if (klass != nullptr && klass->IsPrimitive()) {
2369 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "using primitive type "
2370 << dex_file_->StringByTypeIdx(type_idx) << " in instanceof in "
2371 << GetDeclaringClass();
2372 break;
2373 }
2374
Ian Rogersad0b3a32012-04-16 14:50:24 -07002375 DCHECK_NE(failures_.size(), 0U);
2376 if (!is_checkcast) {
Andreas Gampead238ce2015-08-24 21:13:08 -07002377 work_line_->SetRegisterType<LockOp::kClear>(this,
2378 inst->VRegA_22c(),
2379 reg_types_.Boolean());
Ian Rogersad0b3a32012-04-16 14:50:24 -07002380 }
2381 break; // bad class
Ian Rogers9f1ab122011-12-12 08:52:43 -08002382 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002383 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Sebastien Hertz5243e912013-05-21 10:55:07 +02002384 uint32_t orig_type_reg = (is_checkcast) ? inst->VRegA_21c() : inst->VRegB_22c();
Ian Rogers7b078e82014-09-10 14:44:24 -07002385 const RegType& orig_type = work_line_->GetRegisterType(this, orig_type_reg);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002386 if (!res_type.IsNonZeroReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002387 if (is_checkcast) {
2388 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on unexpected class " << res_type;
2389 } else {
2390 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on unexpected class " << res_type;
2391 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002392 } else if (!orig_type.IsReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002393 if (is_checkcast) {
2394 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on non-reference in v" << orig_type_reg;
2395 } else {
2396 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on non-reference in v" << orig_type_reg;
2397 }
David Brazdil68b5c0b2016-01-19 14:25:29 +00002398 } else if (orig_type.IsUninitializedTypes()) {
2399 if (is_checkcast) {
2400 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on uninitialized reference in v"
2401 << orig_type_reg;
2402 } else {
2403 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on uninitialized reference in v"
2404 << orig_type_reg;
2405 }
jeffhao2a8a90e2011-09-26 14:25:31 -07002406 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002407 if (is_checkcast) {
Andreas Gampead238ce2015-08-24 21:13:08 -07002408 work_line_->SetRegisterType<LockOp::kKeep>(this, inst->VRegA_21c(), 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_22c(),
2412 reg_types_.Boolean());
jeffhaobdb76512011-09-07 11:43:16 -07002413 }
jeffhaobdb76512011-09-07 11:43:16 -07002414 }
jeffhao2a8a90e2011-09-26 14:25:31 -07002415 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002416 }
2417 case Instruction::ARRAY_LENGTH: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002418 const RegType& res_type = work_line_->GetRegisterType(this, inst->VRegB_12x());
Ian Rogers28ad40d2011-10-27 15:19:26 -07002419 if (res_type.IsReferenceTypes()) {
Andreas Gampeeed3a5d2017-11-29 14:58:34 -08002420 if (!res_type.IsArrayTypes() && !res_type.IsZeroOrNull()) {
2421 // ie not an array or null
jeffhaod5347e02012-03-22 17:25:05 -07002422 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002423 } else {
Andreas Gampead238ce2015-08-24 21:13:08 -07002424 work_line_->SetRegisterType<LockOp::kClear>(this,
2425 inst->VRegA_12x(),
2426 reg_types_.Integer());
Ian Rogersd81871c2011-10-03 13:57:23 -07002427 }
Andreas Gampe65c9db82014-07-28 13:14:34 -07002428 } else {
2429 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002430 }
2431 break;
2432 }
2433 case Instruction::NEW_INSTANCE: {
Andreas Gampe98be1a92017-08-28 08:25:45 -07002434 const RegType& res_type = ResolveClass<CheckAccess::kYes>(dex::TypeIndex(inst->VRegB_21c()));
Ian Rogersad0b3a32012-04-16 14:50:24 -07002435 if (res_type.IsConflict()) {
2436 DCHECK_NE(failures_.size(), 0U);
2437 break; // bad class
jeffhao8cd6dda2012-02-22 10:15:34 -08002438 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002439 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
2440 // can't create an instance of an interface or abstract class */
2441 if (!res_type.IsInstantiableTypes()) {
2442 Fail(VERIFY_ERROR_INSTANTIATION)
2443 << "new-instance on primitive, interface or abstract class" << res_type;
Ian Rogers08f753d2012-08-24 14:35:25 -07002444 // Soft failure so carry on to set register type.
Ian Rogersd81871c2011-10-03 13:57:23 -07002445 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00002446 const RegType& uninit_type = reg_types_.Uninitialized(res_type, work_insn_idx_);
Ian Rogers08f753d2012-08-24 14:35:25 -07002447 // Any registers holding previous allocations from this address that have not yet been
2448 // initialized must be marked invalid.
Ian Rogers7b078e82014-09-10 14:44:24 -07002449 work_line_->MarkUninitRefsAsInvalid(this, uninit_type);
Ian Rogers08f753d2012-08-24 14:35:25 -07002450 // add the new uninitialized reference to the register state
Andreas Gampead238ce2015-08-24 21:13:08 -07002451 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_21c(), uninit_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07002452 break;
2453 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08002454 case Instruction::NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002455 VerifyNewArray(inst, false, false);
jeffhaobdb76512011-09-07 11:43:16 -07002456 break;
2457 case Instruction::FILLED_NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002458 VerifyNewArray(inst, true, false);
Ian Rogers0c4a5062012-02-03 15:18:59 -08002459 just_set_result = true; // Filled new array sets result register
jeffhaobdb76512011-09-07 11:43:16 -07002460 break;
Ian Rogers0c4a5062012-02-03 15:18:59 -08002461 case Instruction::FILLED_NEW_ARRAY_RANGE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002462 VerifyNewArray(inst, true, true);
Ian Rogers0c4a5062012-02-03 15:18:59 -08002463 just_set_result = true; // Filled new array range sets result register
2464 break;
jeffhaobdb76512011-09-07 11:43:16 -07002465 case Instruction::CMPL_FLOAT:
2466 case Instruction::CMPG_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002467 if (!work_line_->VerifyRegisterType(this, inst->VRegB_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08002468 break;
2469 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002470 if (!work_line_->VerifyRegisterType(this, inst->VRegC_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08002471 break;
2472 }
Andreas Gampead238ce2015-08-24 21:13:08 -07002473 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002474 break;
2475 case Instruction::CMPL_DOUBLE:
2476 case Instruction::CMPG_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002477 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegB_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002478 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002479 break;
2480 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002481 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegC_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002482 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002483 break;
2484 }
Andreas Gampead238ce2015-08-24 21:13:08 -07002485 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002486 break;
2487 case Instruction::CMP_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002488 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegB_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002489 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002490 break;
2491 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002492 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegC_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002493 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002494 break;
2495 }
Andreas Gampead238ce2015-08-24 21:13:08 -07002496 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002497 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002498 case Instruction::THROW: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002499 const RegType& res_type = work_line_->GetRegisterType(this, inst->VRegA_11x());
David Brazdilca3c8c32016-09-06 14:04:48 +01002500 if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(res_type, this)) {
David Brazdil68b5c0b2016-01-19 14:25:29 +00002501 if (res_type.IsUninitializedTypes()) {
2502 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "thrown exception not initialized";
Pavel Vyssotski980027c2016-02-11 20:28:11 +06002503 } else if (!res_type.IsReferenceTypes()) {
2504 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "thrown value of non-reference type " << res_type;
David Brazdil68b5c0b2016-01-19 14:25:29 +00002505 } else {
2506 Fail(res_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS : VERIFY_ERROR_BAD_CLASS_SOFT)
2507 << "thrown class " << res_type << " not instanceof Throwable";
2508 }
jeffhaobdb76512011-09-07 11:43:16 -07002509 }
2510 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002511 }
jeffhaobdb76512011-09-07 11:43:16 -07002512 case Instruction::GOTO:
2513 case Instruction::GOTO_16:
2514 case Instruction::GOTO_32:
2515 /* no effect on or use of registers */
2516 break;
2517
2518 case Instruction::PACKED_SWITCH:
2519 case Instruction::SPARSE_SWITCH:
2520 /* verify that vAA is an integer, or can be converted to one */
Ian Rogers7b078e82014-09-10 14:44:24 -07002521 work_line_->VerifyRegisterType(this, inst->VRegA_31t(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002522 break;
2523
Ian Rogersd81871c2011-10-03 13:57:23 -07002524 case Instruction::FILL_ARRAY_DATA: {
2525 /* Similar to the verification done for APUT */
Ian Rogers7b078e82014-09-10 14:44:24 -07002526 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegA_31t());
Ian Rogers89310de2012-02-01 13:47:30 -08002527 /* array_type can be null if the reg type is Zero */
Andreas Gampeeed3a5d2017-11-29 14:58:34 -08002528 if (!array_type.IsZeroOrNull()) {
jeffhao457cc512012-02-02 16:55:13 -08002529 if (!array_type.IsArrayTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002530 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with array type "
2531 << array_type;
Andreas Gampebb18a032016-03-22 20:34:25 -07002532 } else if (array_type.IsUnresolvedTypes()) {
2533 // If it's an unresolved array type, it must be non-primitive.
2534 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data for array of type "
2535 << array_type;
Ian Rogers89310de2012-02-01 13:47:30 -08002536 } else {
Ian Rogersd8f69b02014-09-10 21:43:52 +00002537 const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
Ian Rogersad0b3a32012-04-16 14:50:24 -07002538 DCHECK(!component_type.IsConflict());
jeffhao457cc512012-02-02 16:55:13 -08002539 if (component_type.IsNonZeroReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002540 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with component type "
2541 << component_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002542 } else {
jeffhao457cc512012-02-02 16:55:13 -08002543 // Now verify if the element width in the table matches the element width declared in
2544 // the array
Andreas Gampe53de99c2015-08-17 13:43:55 -07002545 const uint16_t* array_data =
2546 insns + (insns[1] | (static_cast<int32_t>(insns[2]) << 16));
jeffhao457cc512012-02-02 16:55:13 -08002547 if (array_data[0] != Instruction::kArrayDataSignature) {
jeffhaod5347e02012-03-22 17:25:05 -07002548 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid magic for array-data";
jeffhao457cc512012-02-02 16:55:13 -08002549 } else {
2550 size_t elem_width = Primitive::ComponentSize(component_type.GetPrimitiveType());
2551 // Since we don't compress the data in Dex, expect to see equal width of data stored
2552 // in the table and expected from the array class.
2553 if (array_data[1] != elem_width) {
jeffhaod5347e02012-03-22 17:25:05 -07002554 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-data size mismatch (" << array_data[1]
2555 << " vs " << elem_width << ")";
jeffhao457cc512012-02-02 16:55:13 -08002556 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002557 }
2558 }
jeffhaobdb76512011-09-07 11:43:16 -07002559 }
2560 }
2561 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002562 }
jeffhaobdb76512011-09-07 11:43:16 -07002563 case Instruction::IF_EQ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002564 case Instruction::IF_NE: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002565 const RegType& reg_type1 = work_line_->GetRegisterType(this, inst->VRegA_22t());
2566 const RegType& reg_type2 = work_line_->GetRegisterType(this, inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002567 bool mismatch = false;
Andreas Gampeeed3a5d2017-11-29 14:58:34 -08002568 if (reg_type1.IsZeroOrNull()) { // zero then integral or reference expected
Ian Rogersd81871c2011-10-03 13:57:23 -07002569 mismatch = !reg_type2.IsReferenceTypes() && !reg_type2.IsIntegralTypes();
2570 } else if (reg_type1.IsReferenceTypes()) { // both references?
2571 mismatch = !reg_type2.IsReferenceTypes();
2572 } else { // both integral?
2573 mismatch = !reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes();
2574 }
2575 if (mismatch) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002576 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to if-eq/if-ne (" << reg_type1 << ","
2577 << reg_type2 << ") must both be references or integral";
jeffhaobdb76512011-09-07 11:43:16 -07002578 }
2579 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002580 }
jeffhaobdb76512011-09-07 11:43:16 -07002581 case Instruction::IF_LT:
2582 case Instruction::IF_GE:
2583 case Instruction::IF_GT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002584 case Instruction::IF_LE: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002585 const RegType& reg_type1 = work_line_->GetRegisterType(this, inst->VRegA_22t());
2586 const RegType& reg_type2 = work_line_->GetRegisterType(this, inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002587 if (!reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002588 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to 'if' (" << reg_type1 << ","
2589 << reg_type2 << ") must be integral";
jeffhaobdb76512011-09-07 11:43:16 -07002590 }
2591 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002592 }
jeffhaobdb76512011-09-07 11:43:16 -07002593 case Instruction::IF_EQZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002594 case Instruction::IF_NEZ: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002595 const RegType& reg_type = work_line_->GetRegisterType(this, inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002596 if (!reg_type.IsReferenceTypes() && !reg_type.IsIntegralTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002597 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
2598 << " unexpected as arg to if-eqz/if-nez";
Ian Rogersd81871c2011-10-03 13:57:23 -07002599 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002600
2601 // Find previous instruction - its existence is a precondition to peephole optimization.
Ian Rogers9b360392013-06-06 14:45:07 -07002602 uint32_t instance_of_idx = 0;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002603 if (0 != work_insn_idx_) {
Ian Rogers9b360392013-06-06 14:45:07 -07002604 instance_of_idx = work_insn_idx_ - 1;
Mathieu Chartierde40d472015-10-15 17:47:48 -07002605 while (0 != instance_of_idx && !GetInstructionFlags(instance_of_idx).IsOpcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07002606 instance_of_idx--;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002607 }
Mathieu Chartierc2a2bda2018-03-12 11:21:52 -07002608 if (FailOrAbort(GetInstructionFlags(instance_of_idx).IsOpcode(),
Andreas Gampe7c038102014-10-27 20:08:46 -07002609 "Unable to get previous instruction of if-eqz/if-nez for work index ",
2610 work_insn_idx_)) {
2611 break;
2612 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002613 } else {
2614 break;
2615 }
2616
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08002617 const Instruction& instance_of_inst = code_item_accessor_.InstructionAt(instance_of_idx);
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002618
2619 /* Check for peep-hole pattern of:
2620 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002621 * instance-of vX, vY, T;
2622 * ifXXX vX, label ;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002623 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002624 * label:
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002625 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002626 * and sharpen the type of vY to be type T.
2627 * Note, this pattern can't be if:
2628 * - if there are other branches to this branch,
2629 * - when vX == vY.
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002630 */
Ian Rogersfae370a2013-06-05 08:33:27 -07002631 if (!CurrentInsnFlags()->IsBranchTarget() &&
Vladimir Markod7559b72017-09-28 13:50:37 +01002632 (Instruction::INSTANCE_OF == instance_of_inst.Opcode()) &&
2633 (inst->VRegA_21t() == instance_of_inst.VRegA_22c()) &&
2634 (instance_of_inst.VRegA_22c() != instance_of_inst.VRegB_22c())) {
Ian Rogersebbdd872014-07-07 23:53:08 -07002635 // Check the type of the instance-of is different than that of registers type, as if they
2636 // are the same there is no work to be done here. Check that the conversion is not to or
2637 // from an unresolved type as type information is imprecise. If the instance-of is to an
2638 // interface then ignore the type information as interfaces can only be treated as Objects
2639 // and we don't want to disallow field and other operations on the object. If the value
2640 // being instance-of checked against is known null (zero) then allow the optimization as
2641 // we didn't have type information. If the merge of the instance-of type with the original
2642 // type is assignable to the original then allow optimization. This check is performed to
2643 // ensure that subsequent merges don't lose type information - such as becoming an
2644 // interface from a class that would lose information relevant to field checks.
Vladimir Markod7559b72017-09-28 13:50:37 +01002645 const RegType& orig_type = work_line_->GetRegisterType(this, instance_of_inst.VRegB_22c());
Andreas Gampe98be1a92017-08-28 08:25:45 -07002646 const RegType& cast_type = ResolveClass<CheckAccess::kYes>(
Vladimir Markod7559b72017-09-28 13:50:37 +01002647 dex::TypeIndex(instance_of_inst.VRegC_22c()));
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002648
Ian Rogersebbdd872014-07-07 23:53:08 -07002649 if (!orig_type.Equals(cast_type) &&
2650 !cast_type.IsUnresolvedTypes() && !orig_type.IsUnresolvedTypes() &&
Andreas Gampe00633eb2014-07-17 16:13:35 -07002651 cast_type.HasClass() && // Could be conflict type, make sure it has a class.
Ian Rogersebbdd872014-07-07 23:53:08 -07002652 !cast_type.GetClass()->IsInterface() &&
Andreas Gampeeed3a5d2017-11-29 14:58:34 -08002653 (orig_type.IsZeroOrNull() ||
David Brazdilca3c8c32016-09-06 14:04:48 +01002654 orig_type.IsStrictlyAssignableFrom(
2655 cast_type.Merge(orig_type, &reg_types_, this), this))) {
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08002656 RegisterLine* update_line = RegisterLine::Create(code_item_accessor_.RegistersSize(),
2657 this);
Ian Rogersfae370a2013-06-05 08:33:27 -07002658 if (inst->Opcode() == Instruction::IF_EQZ) {
Ian Rogers9b360392013-06-06 14:45:07 -07002659 fallthrough_line.reset(update_line);
Ian Rogersfae370a2013-06-05 08:33:27 -07002660 } else {
Ian Rogers9b360392013-06-06 14:45:07 -07002661 branch_line.reset(update_line);
2662 }
2663 update_line->CopyFromLine(work_line_.get());
Andreas Gampead238ce2015-08-24 21:13:08 -07002664 update_line->SetRegisterType<LockOp::kKeep>(this,
Vladimir Markod7559b72017-09-28 13:50:37 +01002665 instance_of_inst.VRegB_22c(),
Andreas Gampead238ce2015-08-24 21:13:08 -07002666 cast_type);
Mathieu Chartierde40d472015-10-15 17:47:48 -07002667 if (!GetInstructionFlags(instance_of_idx).IsBranchTarget() && 0 != instance_of_idx) {
Ian Rogers9b360392013-06-06 14:45:07 -07002668 // See if instance-of was preceded by a move-object operation, common due to the small
2669 // register encoding space of instance-of, and propagate type information to the source
2670 // of the move-object.
2671 uint32_t move_idx = instance_of_idx - 1;
Mathieu Chartierde40d472015-10-15 17:47:48 -07002672 while (0 != move_idx && !GetInstructionFlags(move_idx).IsOpcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07002673 move_idx--;
2674 }
Mathieu Chartierc2a2bda2018-03-12 11:21:52 -07002675 if (FailOrAbort(GetInstructionFlags(move_idx).IsOpcode(),
Andreas Gampe7c038102014-10-27 20:08:46 -07002676 "Unable to get previous instruction of if-eqz/if-nez for work index ",
2677 work_insn_idx_)) {
2678 break;
2679 }
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08002680 const Instruction& move_inst = code_item_accessor_.InstructionAt(move_idx);
Vladimir Markod7559b72017-09-28 13:50:37 +01002681 switch (move_inst.Opcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07002682 case Instruction::MOVE_OBJECT:
Vladimir Markod7559b72017-09-28 13:50:37 +01002683 if (move_inst.VRegA_12x() == 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_12x(),
Andreas Gampead238ce2015-08-24 21:13:08 -07002686 cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002687 }
2688 break;
2689 case Instruction::MOVE_OBJECT_FROM16:
Vladimir Markod7559b72017-09-28 13:50:37 +01002690 if (move_inst.VRegA_22x() == instance_of_inst.VRegB_22c()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07002691 update_line->SetRegisterType<LockOp::kKeep>(this,
Vladimir Markod7559b72017-09-28 13:50:37 +01002692 move_inst.VRegB_22x(),
Andreas Gampead238ce2015-08-24 21:13:08 -07002693 cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002694 }
2695 break;
2696 case Instruction::MOVE_OBJECT_16:
Vladimir Markod7559b72017-09-28 13:50:37 +01002697 if (move_inst.VRegA_32x() == instance_of_inst.VRegB_22c()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07002698 update_line->SetRegisterType<LockOp::kKeep>(this,
Vladimir Markod7559b72017-09-28 13:50:37 +01002699 move_inst.VRegB_32x(),
Andreas Gampead238ce2015-08-24 21:13:08 -07002700 cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002701 }
2702 break;
2703 default:
2704 break;
2705 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002706 }
2707 }
2708 }
2709
jeffhaobdb76512011-09-07 11:43:16 -07002710 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002711 }
jeffhaobdb76512011-09-07 11:43:16 -07002712 case Instruction::IF_LTZ:
2713 case Instruction::IF_GEZ:
2714 case Instruction::IF_GTZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002715 case Instruction::IF_LEZ: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002716 const RegType& reg_type = work_line_->GetRegisterType(this, inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002717 if (!reg_type.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002718 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
2719 << " unexpected as arg to if-ltz/if-gez/if-gtz/if-lez";
Ian Rogersd81871c2011-10-03 13:57:23 -07002720 }
jeffhaobdb76512011-09-07 11:43:16 -07002721 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002722 }
jeffhaobdb76512011-09-07 11:43:16 -07002723 case Instruction::AGET_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002724 VerifyAGet(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002725 break;
jeffhaobdb76512011-09-07 11:43:16 -07002726 case Instruction::AGET_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002727 VerifyAGet(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002728 break;
jeffhaobdb76512011-09-07 11:43:16 -07002729 case Instruction::AGET_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002730 VerifyAGet(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002731 break;
jeffhaobdb76512011-09-07 11:43:16 -07002732 case Instruction::AGET_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002733 VerifyAGet(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002734 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002735 case Instruction::AGET:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002736 VerifyAGet(inst, reg_types_.Integer(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002737 break;
jeffhaobdb76512011-09-07 11:43:16 -07002738 case Instruction::AGET_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002739 VerifyAGet(inst, reg_types_.LongLo(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002740 break;
2741 case Instruction::AGET_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002742 VerifyAGet(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07002743 break;
2744
Ian Rogersd81871c2011-10-03 13:57:23 -07002745 case Instruction::APUT_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002746 VerifyAPut(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002747 break;
2748 case Instruction::APUT_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002749 VerifyAPut(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002750 break;
2751 case Instruction::APUT_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002752 VerifyAPut(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002753 break;
2754 case Instruction::APUT_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002755 VerifyAPut(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002756 break;
2757 case Instruction::APUT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002758 VerifyAPut(inst, reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002759 break;
2760 case Instruction::APUT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002761 VerifyAPut(inst, reg_types_.LongLo(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002762 break;
2763 case Instruction::APUT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002764 VerifyAPut(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07002765 break;
2766
jeffhaobdb76512011-09-07 11:43:16 -07002767 case Instruction::IGET_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002768 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002769 break;
jeffhaobdb76512011-09-07 11:43:16 -07002770 case Instruction::IGET_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002771 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002772 break;
jeffhaobdb76512011-09-07 11:43:16 -07002773 case Instruction::IGET_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002774 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002775 break;
jeffhaobdb76512011-09-07 11:43:16 -07002776 case Instruction::IGET_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002777 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002778 break;
2779 case Instruction::IGET:
Andreas Gampe896df402014-10-20 22:25:29 -07002780 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002781 break;
2782 case Instruction::IGET_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002783 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002784 break;
2785 case Instruction::IGET_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002786 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false,
2787 false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002788 break;
jeffhaobdb76512011-09-07 11:43:16 -07002789
Ian Rogersd81871c2011-10-03 13:57:23 -07002790 case Instruction::IPUT_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002791 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002792 break;
2793 case Instruction::IPUT_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002794 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002795 break;
2796 case Instruction::IPUT_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002797 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002798 break;
2799 case Instruction::IPUT_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002800 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002801 break;
2802 case Instruction::IPUT:
Andreas Gampe896df402014-10-20 22:25:29 -07002803 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002804 break;
2805 case Instruction::IPUT_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002806 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002807 break;
jeffhaobdb76512011-09-07 11:43:16 -07002808 case Instruction::IPUT_OBJECT:
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:
Ian Rogersd81871c2011-10-03 13:57:23 -07002862 case Instruction::INVOKE_SUPER_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002863 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE ||
2864 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002865 bool is_super = (inst->Opcode() == Instruction::INVOKE_SUPER ||
2866 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
Alex Light7268d472016-01-20 15:50:01 -08002867 MethodType type = is_super ? METHOD_SUPER : METHOD_VIRTUAL;
2868 ArtMethod* called_method = VerifyInvocationArgs(inst, type, is_range);
Ian Rogersd8f69b02014-09-10 21:43:52 +00002869 const RegType* return_type = nullptr;
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002870 if (called_method != nullptr) {
Vladimir Markob45528c2017-07-27 14:14:28 +01002871 ObjPtr<mirror::Class> return_type_class = can_load_classes_
2872 ? called_method->ResolveReturnType()
2873 : called_method->LookupResolvedReturnType();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002874 if (return_type_class != nullptr) {
Andreas Gampef23f33d2015-06-23 14:18:17 -07002875 return_type = &FromClass(called_method->GetReturnTypeDescriptor(),
Vladimir Markob45528c2017-07-27 14:14:28 +01002876 return_type_class.Ptr(),
Andreas Gampef23f33d2015-06-23 14:18:17 -07002877 return_type_class->CannotBeAssignedFromOtherTypes());
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002878 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07002879 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
2880 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002881 }
2882 }
2883 if (return_type == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002884 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002885 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
Andreas Gampea5b09a62016-11-17 15:21:22 -08002886 dex::TypeIndex return_type_idx =
2887 dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002888 const char* descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Mathieu Chartierbf99f772014-08-23 16:37:27 -07002889 return_type = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
jeffhaobdb76512011-09-07 11:43:16 -07002890 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002891 if (!return_type->IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002892 work_line_->SetResultRegisterType(this, *return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002893 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002894 work_line_->SetResultRegisterTypeWide(*return_type, return_type->HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002895 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002896 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002897 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002898 }
jeffhaobdb76512011-09-07 11:43:16 -07002899 case Instruction::INVOKE_DIRECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002900 case Instruction::INVOKE_DIRECT_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002901 bool is_range = (inst->Opcode() == Instruction::INVOKE_DIRECT_RANGE);
Alex Light7268d472016-01-20 15:50:01 -08002902 ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_DIRECT, is_range);
Ian Rogers46685432012-06-03 22:26:43 -07002903 const char* return_type_descriptor;
2904 bool is_constructor;
Ian Rogersd8f69b02014-09-10 21:43:52 +00002905 const RegType* return_type = nullptr;
Ian Rogers7b078e82014-09-10 14:44:24 -07002906 if (called_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002907 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogers46685432012-06-03 22:26:43 -07002908 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
Ian Rogersdfb325e2013-10-30 01:00:44 -07002909 is_constructor = strcmp("<init>", dex_file_->StringDataByIdx(method_id.name_idx_)) == 0;
Andreas Gampea5b09a62016-11-17 15:21:22 -08002910 dex::TypeIndex return_type_idx =
2911 dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogers46685432012-06-03 22:26:43 -07002912 return_type_descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2913 } else {
2914 is_constructor = called_method->IsConstructor();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002915 return_type_descriptor = called_method->GetReturnTypeDescriptor();
Vladimir Markob45528c2017-07-27 14:14:28 +01002916 ObjPtr<mirror::Class> return_type_class = can_load_classes_
2917 ? called_method->ResolveReturnType()
2918 : called_method->LookupResolvedReturnType();
Ian Rogers1ff3c982014-08-12 02:30:58 -07002919 if (return_type_class != nullptr) {
Andreas Gampef23f33d2015-06-23 14:18:17 -07002920 return_type = &FromClass(return_type_descriptor,
Vladimir Markob45528c2017-07-27 14:14:28 +01002921 return_type_class.Ptr(),
Andreas Gampef23f33d2015-06-23 14:18:17 -07002922 return_type_class->CannotBeAssignedFromOtherTypes());
Ian Rogers1ff3c982014-08-12 02:30:58 -07002923 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07002924 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
2925 self_->ClearException();
Ian Rogers1ff3c982014-08-12 02:30:58 -07002926 }
Ian Rogers46685432012-06-03 22:26:43 -07002927 }
2928 if (is_constructor) {
jeffhaobdb76512011-09-07 11:43:16 -07002929 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002930 * Some additional checks when calling a constructor. We know from the invocation arg check
2931 * that the "this" argument is an instance of called_method->klass. Now we further restrict
2932 * that to require that called_method->klass is the same as this->klass or this->super,
2933 * allowing the latter only if the "this" argument is the same as the "this" argument to
2934 * this method (which implies that we're in a constructor ourselves).
jeffhaobdb76512011-09-07 11:43:16 -07002935 */
Orion Hodsoncfa325e2016-10-13 10:25:54 +01002936 const RegType& this_type = work_line_->GetInvocationThis(this, inst);
jeffhaob57e9522012-04-26 18:08:21 -07002937 if (this_type.IsConflict()) // failure.
2938 break;
jeffhaobdb76512011-09-07 11:43:16 -07002939
jeffhaob57e9522012-04-26 18:08:21 -07002940 /* no null refs allowed (?) */
Andreas Gampeeed3a5d2017-11-29 14:58:34 -08002941 if (this_type.IsZeroOrNull()) {
jeffhaob57e9522012-04-26 18:08:21 -07002942 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unable to initialize null ref";
2943 break;
jeffhao2a8a90e2011-09-26 14:25:31 -07002944 }
jeffhaob57e9522012-04-26 18:08:21 -07002945
2946 /* must be in same class or in superclass */
Ian Rogersd8f69b02014-09-10 21:43:52 +00002947 // const RegType& this_super_klass = this_type.GetSuperClass(&reg_types_);
Ian Rogers46685432012-06-03 22:26:43 -07002948 // TODO: re-enable constructor type verification
2949 // if (this_super_klass.IsConflict()) {
jeffhaob57e9522012-04-26 18:08:21 -07002950 // Unknown super class, fail so we re-check at runtime.
Ian Rogers46685432012-06-03 22:26:43 -07002951 // Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "super class unknown for '" << this_type << "'";
2952 // break;
2953 // }
jeffhaob57e9522012-04-26 18:08:21 -07002954
2955 /* arg must be an uninitialized reference */
2956 if (!this_type.IsUninitializedTypes()) {
2957 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Expected initialization on uninitialized reference "
2958 << this_type;
2959 break;
2960 }
2961
2962 /*
2963 * Replace the uninitialized reference with an initialized one. We need to do this for all
2964 * registers that have the same object instance in them, not just the "this" register.
2965 */
Nicolas Geoffray98e6ce42016-02-16 18:42:15 +00002966 work_line_->MarkRefsAsInitialized(this, this_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002967 }
Ian Rogers1ff3c982014-08-12 02:30:58 -07002968 if (return_type == nullptr) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07002969 return_type = &reg_types_.FromDescriptor(GetClassLoader(), return_type_descriptor, false);
Ian Rogers1ff3c982014-08-12 02:30:58 -07002970 }
2971 if (!return_type->IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002972 work_line_->SetResultRegisterType(this, *return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002973 } else {
Ian Rogers1ff3c982014-08-12 02:30:58 -07002974 work_line_->SetResultRegisterTypeWide(*return_type, return_type->HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002975 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002976 just_set_result = true;
2977 break;
2978 }
2979 case Instruction::INVOKE_STATIC:
2980 case Instruction::INVOKE_STATIC_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002981 bool is_range = (inst->Opcode() == Instruction::INVOKE_STATIC_RANGE);
Alex Light7268d472016-01-20 15:50:01 -08002982 ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_STATIC, is_range);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002983 const char* descriptor;
Ian Rogers7b078e82014-09-10 14:44:24 -07002984 if (called_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002985 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002986 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
Andreas Gampea5b09a62016-11-17 15:21:22 -08002987 dex::TypeIndex return_type_idx =
2988 dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Mathieu Chartier590fee92013-09-13 13:46:47 -07002989 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002990 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002991 descriptor = called_method->GetReturnTypeDescriptor();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002992 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00002993 const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002994 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002995 work_line_->SetResultRegisterType(this, return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002996 } else {
2997 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2998 }
jeffhaobdb76512011-09-07 11:43:16 -07002999 just_set_result = true;
3000 }
3001 break;
jeffhaobdb76512011-09-07 11:43:16 -07003002 case Instruction::INVOKE_INTERFACE:
Ian Rogersd81871c2011-10-03 13:57:23 -07003003 case Instruction::INVOKE_INTERFACE_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003004 bool is_range = (inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE);
Alex Light7268d472016-01-20 15:50:01 -08003005 ArtMethod* abs_method = VerifyInvocationArgs(inst, METHOD_INTERFACE, is_range);
Ian Rogers7b078e82014-09-10 14:44:24 -07003006 if (abs_method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003007 mirror::Class* called_interface = abs_method->GetDeclaringClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07003008 if (!called_interface->IsInterface() && !called_interface->IsObjectClass()) {
3009 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected interface class in invoke-interface '"
David Sehr709b0702016-10-13 09:12:37 -07003010 << abs_method->PrettyMethod() << "'";
Ian Rogersad0b3a32012-04-16 14:50:24 -07003011 break;
Ian Rogers28ad40d2011-10-27 15:19:26 -07003012 }
Ian Rogers0d604842012-04-16 14:50:24 -07003013 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003014 /* Get the type of the "this" arg, which should either be a sub-interface of called
3015 * interface or Object (see comments in RegType::JoinClass).
3016 */
Orion Hodsoncfa325e2016-10-13 10:25:54 +01003017 const RegType& this_type = work_line_->GetInvocationThis(this, inst);
Andreas Gampeeed3a5d2017-11-29 14:58:34 -08003018 if (this_type.IsZeroOrNull()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003019 /* null pointer always passes (and always fails at runtime) */
3020 } else {
3021 if (this_type.IsUninitializedTypes()) {
3022 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface call on uninitialized object "
3023 << this_type;
3024 break;
3025 }
3026 // In the past we have tried to assert that "called_interface" is assignable
3027 // from "this_type.GetClass()", however, as we do an imprecise Join
3028 // (RegType::JoinClass) we don't have full information on what interfaces are
3029 // implemented by "this_type". For example, two classes may implement the same
3030 // interfaces and have a common parent that doesn't implement the interface. The
3031 // join will set "this_type" to the parent class and a test that this implements
3032 // the interface will incorrectly fail.
3033 }
3034 /*
3035 * We don't have an object instance, so we can't find the concrete method. However, all of
3036 * the type information is in the abstract method, so we're good.
3037 */
3038 const char* descriptor;
Ian Rogers7b078e82014-09-10 14:44:24 -07003039 if (abs_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003040 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07003041 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
Andreas Gampea5b09a62016-11-17 15:21:22 -08003042 dex::TypeIndex return_type_idx =
3043 dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Mathieu Chartierde40d472015-10-15 17:47:48 -07003044 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003045 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003046 descriptor = abs_method->GetReturnTypeDescriptor();
Ian Rogersad0b3a32012-04-16 14:50:24 -07003047 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00003048 const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003049 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003050 work_line_->SetResultRegisterType(this, return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003051 } else {
3052 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
3053 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003054 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07003055 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07003056 }
Narayan Kamath9823e782016-08-03 12:46:58 +01003057 case Instruction::INVOKE_POLYMORPHIC:
3058 case Instruction::INVOKE_POLYMORPHIC_RANGE: {
Orion Hodsoncfa325e2016-10-13 10:25:54 +01003059 bool is_range = (inst->Opcode() == Instruction::INVOKE_POLYMORPHIC_RANGE);
3060 ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_POLYMORPHIC, is_range);
3061 if (called_method == nullptr) {
3062 // Convert potential soft failures in VerifyInvocationArgs() to hard errors.
3063 if (failure_messages_.size() > 0) {
3064 std::string message = failure_messages_.back()->str();
3065 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << message;
3066 } else {
3067 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invoke-polymorphic verification failure.";
3068 }
3069 break;
3070 }
3071 if (!CheckSignaturePolymorphicMethod(called_method) ||
3072 !CheckSignaturePolymorphicReceiver(inst)) {
Orion Hodsonfe92d122018-01-02 10:45:17 +00003073 DCHECK(HasFailures());
Orion Hodsoncfa325e2016-10-13 10:25:54 +01003074 break;
3075 }
3076 const uint32_t proto_idx = (is_range) ? inst->VRegH_4rcc() : inst->VRegH_45cc();
Orion Hodsonac141392017-01-13 11:53:47 +00003077 const char* return_descriptor =
Orion Hodsoncfa325e2016-10-13 10:25:54 +01003078 dex_file_->GetReturnTypeDescriptor(dex_file_->GetProtoId(proto_idx));
3079 const RegType& return_type =
Orion Hodsonac141392017-01-13 11:53:47 +00003080 reg_types_.FromDescriptor(GetClassLoader(), return_descriptor, false);
Orion Hodsoncfa325e2016-10-13 10:25:54 +01003081 if (!return_type.IsLowHalf()) {
3082 work_line_->SetResultRegisterType(this, return_type);
3083 } else {
3084 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
3085 }
Orion Hodsonac141392017-01-13 11:53:47 +00003086 just_set_result = true;
Orion Hodsoncfa325e2016-10-13 10:25:54 +01003087 break;
Narayan Kamath9823e782016-08-03 12:46:58 +01003088 }
Orion Hodsonc069a302017-01-18 09:23:12 +00003089 case Instruction::INVOKE_CUSTOM:
3090 case Instruction::INVOKE_CUSTOM_RANGE: {
3091 // Verify registers based on method_type in the call site.
3092 bool is_range = (inst->Opcode() == Instruction::INVOKE_CUSTOM_RANGE);
3093
3094 // Step 1. Check the call site that produces the method handle for invocation
3095 const uint32_t call_site_idx = is_range ? inst->VRegB_3rc() : inst->VRegB_35c();
3096 if (!CheckCallSite(call_site_idx)) {
3097 DCHECK(HasFailures());
3098 break;
3099 }
3100
3101 // Step 2. Check the register arguments correspond to the expected arguments for the
3102 // method handle produced by step 1. The dex file verifier has checked ranges for
3103 // the first three arguments and CheckCallSite has checked the method handle type.
3104 CallSiteArrayValueIterator it(*dex_file_, dex_file_->GetCallSiteId(call_site_idx));
3105 it.Next(); // Skip to name.
3106 it.Next(); // Skip to method type of the method handle
3107 const uint32_t proto_idx = static_cast<uint32_t>(it.GetJavaValue().i);
3108 const DexFile::ProtoId& proto_id = dex_file_->GetProtoId(proto_idx);
3109 DexFileParameterIterator param_it(*dex_file_, proto_id);
3110 // Treat method as static as it has yet to be determined.
3111 VerifyInvocationArgsFromIterator(&param_it, inst, METHOD_STATIC, is_range, nullptr);
3112 const char* return_descriptor = dex_file_->GetReturnTypeDescriptor(proto_id);
3113
3114 // Step 3. Propagate return type information
3115 const RegType& return_type =
3116 reg_types_.FromDescriptor(GetClassLoader(), return_descriptor, false);
3117 if (!return_type.IsLowHalf()) {
3118 work_line_->SetResultRegisterType(this, return_type);
3119 } else {
3120 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
3121 }
3122 just_set_result = true;
3123 // TODO: Add compiler support for invoke-custom (b/35337872).
3124 Fail(VERIFY_ERROR_FORCE_INTERPRETER);
3125 break;
3126 }
jeffhaobdb76512011-09-07 11:43:16 -07003127 case Instruction::NEG_INT:
3128 case Instruction::NOT_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003129 work_line_->CheckUnaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003130 break;
3131 case Instruction::NEG_LONG:
3132 case Instruction::NOT_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07003133 work_line_->CheckUnaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003134 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07003135 break;
3136 case Instruction::NEG_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003137 work_line_->CheckUnaryOp(this, inst, reg_types_.Float(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07003138 break;
3139 case Instruction::NEG_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07003140 work_line_->CheckUnaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003141 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07003142 break;
3143 case Instruction::INT_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07003144 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003145 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003146 break;
3147 case Instruction::INT_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003148 work_line_->CheckUnaryOp(this, inst, reg_types_.Float(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003149 break;
3150 case Instruction::INT_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07003151 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003152 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003153 break;
3154 case Instruction::LONG_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003155 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003156 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07003157 break;
3158 case Instruction::LONG_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003159 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003160 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07003161 break;
3162 case Instruction::LONG_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07003163 work_line_->CheckUnaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003164 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07003165 break;
3166 case Instruction::FLOAT_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003167 work_line_->CheckUnaryOp(this, inst, reg_types_.Integer(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07003168 break;
3169 case Instruction::FLOAT_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07003170 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003171 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07003172 break;
3173 case Instruction::FLOAT_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07003174 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003175 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07003176 break;
3177 case Instruction::DOUBLE_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003178 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003179 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07003180 break;
3181 case Instruction::DOUBLE_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07003182 work_line_->CheckUnaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003183 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07003184 break;
3185 case Instruction::DOUBLE_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003186 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003187 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07003188 break;
3189 case Instruction::INT_TO_BYTE:
Ian Rogers7b078e82014-09-10 14:44:24 -07003190 work_line_->CheckUnaryOp(this, inst, reg_types_.Byte(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003191 break;
3192 case Instruction::INT_TO_CHAR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003193 work_line_->CheckUnaryOp(this, inst, reg_types_.Char(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003194 break;
3195 case Instruction::INT_TO_SHORT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003196 work_line_->CheckUnaryOp(this, inst, reg_types_.Short(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003197 break;
3198
3199 case Instruction::ADD_INT:
3200 case Instruction::SUB_INT:
3201 case Instruction::MUL_INT:
3202 case Instruction::REM_INT:
3203 case Instruction::DIV_INT:
3204 case Instruction::SHL_INT:
3205 case Instruction::SHR_INT:
3206 case Instruction::USHR_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003207 work_line_->CheckBinaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003208 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07003209 break;
3210 case Instruction::AND_INT:
3211 case Instruction::OR_INT:
3212 case Instruction::XOR_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003213 work_line_->CheckBinaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003214 reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07003215 break;
3216 case Instruction::ADD_LONG:
3217 case Instruction::SUB_LONG:
3218 case Instruction::MUL_LONG:
3219 case Instruction::DIV_LONG:
3220 case Instruction::REM_LONG:
3221 case Instruction::AND_LONG:
3222 case Instruction::OR_LONG:
3223 case Instruction::XOR_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07003224 work_line_->CheckBinaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003225 reg_types_.LongLo(), reg_types_.LongHi(),
3226 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07003227 break;
3228 case Instruction::SHL_LONG:
3229 case Instruction::SHR_LONG:
3230 case Instruction::USHR_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07003231 /* shift distance is Int, making these different from other binary operations */
Ian Rogers7b078e82014-09-10 14:44:24 -07003232 work_line_->CheckBinaryOpWideShift(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003233 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003234 break;
3235 case Instruction::ADD_FLOAT:
3236 case Instruction::SUB_FLOAT:
3237 case Instruction::MUL_FLOAT:
3238 case Instruction::DIV_FLOAT:
3239 case Instruction::REM_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003240 work_line_->CheckBinaryOp(this, inst, reg_types_.Float(), reg_types_.Float(),
3241 reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07003242 break;
3243 case Instruction::ADD_DOUBLE:
3244 case Instruction::SUB_DOUBLE:
3245 case Instruction::MUL_DOUBLE:
3246 case Instruction::DIV_DOUBLE:
3247 case Instruction::REM_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07003248 work_line_->CheckBinaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003249 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
3250 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07003251 break;
3252 case Instruction::ADD_INT_2ADDR:
3253 case Instruction::SUB_INT_2ADDR:
3254 case Instruction::MUL_INT_2ADDR:
3255 case Instruction::REM_INT_2ADDR:
3256 case Instruction::SHL_INT_2ADDR:
3257 case Instruction::SHR_INT_2ADDR:
3258 case Instruction::USHR_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003259 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
3260 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07003261 break;
3262 case Instruction::AND_INT_2ADDR:
3263 case Instruction::OR_INT_2ADDR:
3264 case Instruction::XOR_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003265 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
3266 reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07003267 break;
3268 case Instruction::DIV_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003269 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
3270 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07003271 break;
3272 case Instruction::ADD_LONG_2ADDR:
3273 case Instruction::SUB_LONG_2ADDR:
3274 case Instruction::MUL_LONG_2ADDR:
3275 case Instruction::DIV_LONG_2ADDR:
3276 case Instruction::REM_LONG_2ADDR:
3277 case Instruction::AND_LONG_2ADDR:
3278 case Instruction::OR_LONG_2ADDR:
3279 case Instruction::XOR_LONG_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003280 work_line_->CheckBinaryOp2addrWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003281 reg_types_.LongLo(), reg_types_.LongHi(),
3282 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07003283 break;
3284 case Instruction::SHL_LONG_2ADDR:
3285 case Instruction::SHR_LONG_2ADDR:
3286 case Instruction::USHR_LONG_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003287 work_line_->CheckBinaryOp2addrWideShift(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003288 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003289 break;
3290 case Instruction::ADD_FLOAT_2ADDR:
3291 case Instruction::SUB_FLOAT_2ADDR:
3292 case Instruction::MUL_FLOAT_2ADDR:
3293 case Instruction::DIV_FLOAT_2ADDR:
3294 case Instruction::REM_FLOAT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003295 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Float(), reg_types_.Float(),
3296 reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07003297 break;
3298 case Instruction::ADD_DOUBLE_2ADDR:
3299 case Instruction::SUB_DOUBLE_2ADDR:
3300 case Instruction::MUL_DOUBLE_2ADDR:
3301 case Instruction::DIV_DOUBLE_2ADDR:
3302 case Instruction::REM_DOUBLE_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003303 work_line_->CheckBinaryOp2addrWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003304 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
3305 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07003306 break;
3307 case Instruction::ADD_INT_LIT16:
Ian Rogersf72a11d2014-10-30 15:41:08 -07003308 case Instruction::RSUB_INT_LIT16:
jeffhaobdb76512011-09-07 11:43:16 -07003309 case Instruction::MUL_INT_LIT16:
3310 case Instruction::DIV_INT_LIT16:
3311 case Instruction::REM_INT_LIT16:
Ian Rogers7b078e82014-09-10 14:44:24 -07003312 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), false,
3313 true);
jeffhaobdb76512011-09-07 11:43:16 -07003314 break;
3315 case Instruction::AND_INT_LIT16:
3316 case Instruction::OR_INT_LIT16:
3317 case Instruction::XOR_INT_LIT16:
Ian Rogers7b078e82014-09-10 14:44:24 -07003318 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), true,
3319 true);
jeffhaobdb76512011-09-07 11:43:16 -07003320 break;
3321 case Instruction::ADD_INT_LIT8:
3322 case Instruction::RSUB_INT_LIT8:
3323 case Instruction::MUL_INT_LIT8:
3324 case Instruction::DIV_INT_LIT8:
3325 case Instruction::REM_INT_LIT8:
3326 case Instruction::SHL_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07003327 case Instruction::SHR_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07003328 case Instruction::USHR_INT_LIT8:
Ian Rogers7b078e82014-09-10 14:44:24 -07003329 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), false,
3330 false);
jeffhaobdb76512011-09-07 11:43:16 -07003331 break;
3332 case Instruction::AND_INT_LIT8:
3333 case Instruction::OR_INT_LIT8:
3334 case Instruction::XOR_INT_LIT8:
Ian Rogers7b078e82014-09-10 14:44:24 -07003335 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), true,
3336 false);
jeffhaobdb76512011-09-07 11:43:16 -07003337 break;
3338
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003339 // Special instructions.
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07003340 case Instruction::RETURN_VOID_NO_BARRIER:
3341 if (IsConstructor() && !IsStatic()) {
3342 auto& declaring_class = GetDeclaringClass();
Andreas Gampe68df3202015-06-22 11:35:46 -07003343 if (declaring_class.IsUnresolvedReference()) {
3344 // We must iterate over the fields, even if we cannot use mirror classes to do so. Do it
3345 // manually over the underlying dex file.
3346 uint32_t first_index = GetFirstFinalInstanceFieldIndex(*dex_file_,
3347 dex_file_->GetMethodId(dex_method_idx_).class_idx_);
Andreas Gampee2abbc62017-09-15 11:59:26 -07003348 if (first_index != dex::kDexNoIndex) {
Andreas Gampe68df3202015-06-22 11:35:46 -07003349 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void-no-barrier not expected for field "
3350 << first_index;
3351 }
3352 break;
3353 }
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07003354 auto* klass = declaring_class.GetClass();
3355 for (uint32_t i = 0, num_fields = klass->NumInstanceFields(); i < num_fields; ++i) {
3356 if (klass->GetInstanceField(i)->IsFinal()) {
Mathieu Chartiere86deef2015-03-19 13:43:37 -07003357 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void-no-barrier not expected for "
David Sehr709b0702016-10-13 09:12:37 -07003358 << klass->GetInstanceField(i)->PrettyField();
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07003359 break;
3360 }
3361 }
Sebastien Hertzcc10e0e2013-06-28 14:24:48 +02003362 }
Andreas Gampeb2917962015-07-31 13:36:10 -07003363 // Handle this like a RETURN_VOID now. Code is duplicated to separate standard from
3364 // quickened opcodes (otherwise this could be a fall-through).
3365 if (!IsConstructor()) {
3366 if (!GetMethodReturnType().IsConflict()) {
3367 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void not expected";
3368 }
3369 }
Sebastien Hertzcc10e0e2013-06-28 14:24:48 +02003370 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003371 // Note: the following instructions encode offsets derived from class linking.
Neil Fuller0e844392016-09-08 13:43:31 +01003372 // As such they use Class*/Field*/Executable* as these offsets only have
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003373 // meaning if the class linking and resolution were successful.
3374 case Instruction::IGET_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003375 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003376 break;
3377 case Instruction::IGET_WIDE_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003378 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003379 break;
3380 case Instruction::IGET_OBJECT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003381 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003382 break;
Mathieu Chartierffc605c2014-12-10 10:35:44 -08003383 case Instruction::IGET_BOOLEAN_QUICK:
3384 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true);
3385 break;
3386 case Instruction::IGET_BYTE_QUICK:
3387 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true);
3388 break;
3389 case Instruction::IGET_CHAR_QUICK:
3390 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true);
3391 break;
3392 case Instruction::IGET_SHORT_QUICK:
3393 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true);
3394 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003395 case Instruction::IPUT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003396 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003397 break;
Fred Shih37f05ef2014-07-16 18:38:08 -07003398 case Instruction::IPUT_BOOLEAN_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003399 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07003400 break;
3401 case Instruction::IPUT_BYTE_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003402 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07003403 break;
3404 case Instruction::IPUT_CHAR_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003405 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07003406 break;
3407 case Instruction::IPUT_SHORT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003408 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07003409 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003410 case Instruction::IPUT_WIDE_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003411 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003412 break;
3413 case Instruction::IPUT_OBJECT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003414 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003415 break;
3416 case Instruction::INVOKE_VIRTUAL_QUICK:
3417 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: {
3418 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
Mathieu Chartiere401d142015-04-22 13:56:20 -07003419 ArtMethod* called_method = VerifyInvokeVirtualQuickArgs(inst, is_range);
Ian Rogers7b078e82014-09-10 14:44:24 -07003420 if (called_method != nullptr) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003421 const char* descriptor = called_method->GetReturnTypeDescriptor();
Ian Rogersd8f69b02014-09-10 21:43:52 +00003422 const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003423 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003424 work_line_->SetResultRegisterType(this, return_type);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003425 } else {
3426 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
3427 }
3428 just_set_result = true;
3429 }
3430 break;
3431 }
3432
Ian Rogersd81871c2011-10-03 13:57:23 -07003433 /* These should never appear during verification. */
Mathieu Chartierffc605c2014-12-10 10:35:44 -08003434 case Instruction::UNUSED_3E ... Instruction::UNUSED_43:
Narayan Kamath8ec3bd22016-08-03 12:46:23 +01003435 case Instruction::UNUSED_F3 ... Instruction::UNUSED_F9:
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003436 case Instruction::UNUSED_79:
3437 case Instruction::UNUSED_7A:
jeffhaod5347e02012-03-22 17:25:05 -07003438 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Unexpected opcode " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07003439 break;
3440
3441 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003442 * DO NOT add a "default" clause here. Without it the compiler will
jeffhaobdb76512011-09-07 11:43:16 -07003443 * complain if an instruction is missing (which is desirable).
3444 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003445 } // end - switch (dec_insn.opcode)
jeffhaobdb76512011-09-07 11:43:16 -07003446
Ian Rogersad0b3a32012-04-16 14:50:24 -07003447 if (have_pending_hard_failure_) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08003448 if (Runtime::Current()->IsAotCompiler()) {
3449 /* When AOT compiling, check that the last failure is a hard failure */
Andreas Gampeb588f4c2015-05-26 13:35:39 -07003450 if (failures_[failures_.size() - 1] != VERIFY_ERROR_BAD_CLASS_HARD) {
3451 LOG(ERROR) << "Pending failures:";
3452 for (auto& error : failures_) {
3453 LOG(ERROR) << error;
3454 }
3455 for (auto& error_msg : failure_messages_) {
3456 LOG(ERROR) << error_msg->str();
3457 }
3458 LOG(FATAL) << "Pending hard failure, but last failure not hard.";
3459 }
Ian Rogerse1758fe2012-04-19 11:31:15 -07003460 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003461 /* immediate failure, reject class */
3462 info_messages_ << "Rejecting opcode " << inst->DumpString(dex_file_);
3463 return false;
jeffhaofaf459e2012-08-31 15:32:47 -07003464 } else if (have_pending_runtime_throw_failure_) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07003465 /* checking interpreter will throw, mark following code as unreachable */
jeffhaofaf459e2012-08-31 15:32:47 -07003466 opcode_flags = Instruction::kThrow;
Andreas Gampea727e372015-08-25 09:22:37 -07003467 // Note: the flag must be reset as it is only global to decouple Fail and is semantically per
3468 // instruction. However, RETURN checking may throw LOCKING errors, so we clear at the
3469 // very end.
jeffhaobdb76512011-09-07 11:43:16 -07003470 }
jeffhaobdb76512011-09-07 11:43:16 -07003471 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003472 * If we didn't just set the result register, clear it out. This ensures that you can only use
3473 * "move-result" immediately after the result is set. (We could check this statically, but it's
3474 * not expensive and it makes our debugging output cleaner.)
jeffhaobdb76512011-09-07 11:43:16 -07003475 */
3476 if (!just_set_result) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003477 work_line_->SetResultTypeToUnknown(this);
jeffhaobdb76512011-09-07 11:43:16 -07003478 }
3479
jeffhaobdb76512011-09-07 11:43:16 -07003480 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003481 * Handle "branch". Tag the branch target.
jeffhaobdb76512011-09-07 11:43:16 -07003482 *
3483 * NOTE: instructions like Instruction::EQZ provide information about the
jeffhaod1f0fde2011-09-08 17:25:33 -07003484 * state of the register when the branch is taken or not taken. For example,
jeffhaobdb76512011-09-07 11:43:16 -07003485 * somebody could get a reference field, check it for zero, and if the
3486 * branch is taken immediately store that register in a boolean field
jeffhaod1f0fde2011-09-08 17:25:33 -07003487 * since the value is known to be zero. We do not currently account for
jeffhaobdb76512011-09-07 11:43:16 -07003488 * that, and will reject the code.
3489 *
3490 * TODO: avoid re-fetching the branch target
3491 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003492 if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07003493 bool isConditional, selfOkay;
Ian Rogersd81871c2011-10-03 13:57:23 -07003494 if (!GetBranchOffset(work_insn_idx_, &branch_target, &isConditional, &selfOkay)) {
jeffhaobdb76512011-09-07 11:43:16 -07003495 /* should never happen after static verification */
jeffhaod5347e02012-03-22 17:25:05 -07003496 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad branch";
jeffhaobdb76512011-09-07 11:43:16 -07003497 return false;
3498 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08003499 DCHECK_EQ(isConditional, (opcode_flags & Instruction::kContinue) != 0);
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08003500 if (!CheckNotMoveExceptionOrMoveResult(code_item_accessor_.Insns(),
3501 work_insn_idx_ + branch_target)) {
jeffhaobdb76512011-09-07 11:43:16 -07003502 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07003503 }
jeffhaobdb76512011-09-07 11:43:16 -07003504 /* update branch target, set "changed" if appropriate */
Mathieu Chartierde40d472015-10-15 17:47:48 -07003505 if (nullptr != branch_line) {
Ian Rogersebbdd872014-07-07 23:53:08 -07003506 if (!UpdateRegisters(work_insn_idx_ + branch_target, branch_line.get(), false)) {
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003507 return false;
3508 }
3509 } else {
Ian Rogersebbdd872014-07-07 23:53:08 -07003510 if (!UpdateRegisters(work_insn_idx_ + branch_target, work_line_.get(), false)) {
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003511 return false;
3512 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003513 }
jeffhaobdb76512011-09-07 11:43:16 -07003514 }
3515
3516 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003517 * Handle "switch". Tag all possible branch targets.
jeffhaobdb76512011-09-07 11:43:16 -07003518 *
3519 * We've already verified that the table is structurally sound, so we
3520 * just need to walk through and tag the targets.
3521 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003522 if ((opcode_flags & Instruction::kSwitch) != 0) {
Andreas Gampe53de99c2015-08-17 13:43:55 -07003523 int offset_to_switch = insns[1] | (static_cast<int32_t>(insns[2]) << 16);
jeffhaobdb76512011-09-07 11:43:16 -07003524 const uint16_t* switch_insns = insns + offset_to_switch;
3525 int switch_count = switch_insns[1];
3526 int offset_to_targets, targ;
3527
3528 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
3529 /* 0 = sig, 1 = count, 2/3 = first key */
3530 offset_to_targets = 4;
3531 } else {
3532 /* 0 = sig, 1 = count, 2..count * 2 = keys */
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07003533 DCHECK((*insns & 0xff) == Instruction::SPARSE_SWITCH);
jeffhaobdb76512011-09-07 11:43:16 -07003534 offset_to_targets = 2 + 2 * switch_count;
3535 }
3536
3537 /* verify each switch target */
3538 for (targ = 0; targ < switch_count; targ++) {
3539 int offset;
3540 uint32_t abs_offset;
3541
3542 /* offsets are 32-bit, and only partly endian-swapped */
3543 offset = switch_insns[offset_to_targets + targ * 2] |
Andreas Gampe53de99c2015-08-17 13:43:55 -07003544 (static_cast<int32_t>(switch_insns[offset_to_targets + targ * 2 + 1]) << 16);
Ian Rogersd81871c2011-10-03 13:57:23 -07003545 abs_offset = work_insn_idx_ + offset;
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08003546 DCHECK_LT(abs_offset, code_item_accessor_.InsnsSizeInCodeUnits());
3547 if (!CheckNotMoveExceptionOrMoveResult(code_item_accessor_.Insns(), abs_offset)) {
jeffhaobdb76512011-09-07 11:43:16 -07003548 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07003549 }
Ian Rogersebbdd872014-07-07 23:53:08 -07003550 if (!UpdateRegisters(abs_offset, work_line_.get(), false)) {
jeffhaobdb76512011-09-07 11:43:16 -07003551 return false;
Ian Rogersebbdd872014-07-07 23:53:08 -07003552 }
jeffhaobdb76512011-09-07 11:43:16 -07003553 }
3554 }
3555
3556 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003557 * Handle instructions that can throw and that are sitting in a "try" block. (If they're not in a
3558 * "try" block when they throw, control transfers out of the method.)
jeffhaobdb76512011-09-07 11:43:16 -07003559 */
Mathieu Chartierde40d472015-10-15 17:47:48 -07003560 if ((opcode_flags & Instruction::kThrow) != 0 && GetInstructionFlags(work_insn_idx_).IsInTry()) {
Andreas Gampef91baf12014-07-18 15:41:00 -07003561 bool has_catch_all_handler = false;
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08003562 const DexFile::TryItem* try_item = code_item_accessor_.FindTryItem(work_insn_idx_);
3563 CHECK(try_item != nullptr);
Mathieu Chartierdc578c72017-12-27 11:51:45 -08003564 CatchHandlerIterator iterator(code_item_accessor_, *try_item);
jeffhaobdb76512011-09-07 11:43:16 -07003565
Andreas Gampef91baf12014-07-18 15:41:00 -07003566 // Need the linker to try and resolve the handled class to check if it's Throwable.
3567 ClassLinker* linker = Runtime::Current()->GetClassLinker();
3568
Ian Rogers0571d352011-11-03 19:51:38 -07003569 for (; iterator.HasNext(); iterator.Next()) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08003570 dex::TypeIndex handler_type_idx = iterator.GetHandlerTypeIndex();
3571 if (!handler_type_idx.IsValid()) {
Andreas Gampef91baf12014-07-18 15:41:00 -07003572 has_catch_all_handler = true;
3573 } else {
3574 // It is also a catch-all if it is java.lang.Throwable.
Vladimir Marko28e012a2017-12-07 11:22:59 +00003575 ObjPtr<mirror::Class> klass =
Vladimir Marko666ee3d2017-12-11 18:37:36 +00003576 linker->ResolveType(handler_type_idx, dex_cache_, class_loader_);
Andreas Gampef91baf12014-07-18 15:41:00 -07003577 if (klass != nullptr) {
3578 if (klass == mirror::Throwable::GetJavaLangThrowable()) {
3579 has_catch_all_handler = true;
3580 }
3581 } else {
3582 // Clear exception.
Ian Rogers7b078e82014-09-10 14:44:24 -07003583 DCHECK(self_->IsExceptionPending());
3584 self_->ClearException();
Andreas Gampef91baf12014-07-18 15:41:00 -07003585 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003586 }
jeffhaobdb76512011-09-07 11:43:16 -07003587 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003588 * Merge registers into the "catch" block. We want to use the "savedRegs" rather than
3589 * "work_regs", because at runtime the exception will be thrown before the instruction
3590 * modifies any registers.
jeffhaobdb76512011-09-07 11:43:16 -07003591 */
Ian Rogersebbdd872014-07-07 23:53:08 -07003592 if (!UpdateRegisters(iterator.GetHandlerAddress(), saved_line_.get(), false)) {
jeffhaobdb76512011-09-07 11:43:16 -07003593 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07003594 }
jeffhaobdb76512011-09-07 11:43:16 -07003595 }
3596
3597 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003598 * If the monitor stack depth is nonzero, there must be a "catch all" handler for this
3599 * instruction. This does apply to monitor-exit because of async exception handling.
jeffhaobdb76512011-09-07 11:43:16 -07003600 */
Andreas Gampef91baf12014-07-18 15:41:00 -07003601 if (work_line_->MonitorStackDepth() > 0 && !has_catch_all_handler) {
jeffhaobdb76512011-09-07 11:43:16 -07003602 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003603 * The state in work_line reflects the post-execution state. If the current instruction is a
3604 * monitor-enter and the monitor stack was empty, we don't need a catch-all (if it throws,
jeffhaobdb76512011-09-07 11:43:16 -07003605 * it will do so before grabbing the lock).
3606 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02003607 if (inst->Opcode() != Instruction::MONITOR_ENTER || work_line_->MonitorStackDepth() != 1) {
jeffhaod5347e02012-03-22 17:25:05 -07003608 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
Ian Rogersd81871c2011-10-03 13:57:23 -07003609 << "expected to be within a catch-all for an instruction where a monitor is held";
jeffhaobdb76512011-09-07 11:43:16 -07003610 return false;
3611 }
3612 }
3613 }
3614
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003615 /* Handle "continue". Tag the next consecutive instruction.
3616 * Note: Keep the code handling "continue" case below the "branch" and "switch" cases,
3617 * because it changes work_line_ when performing peephole optimization
3618 * and this change should not be used in those cases.
3619 */
Ian Rogers6d376ae2013-07-23 15:12:40 -07003620 if ((opcode_flags & Instruction::kContinue) != 0) {
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08003621 DCHECK_EQ(&code_item_accessor_.InstructionAt(work_insn_idx_), inst);
Ian Rogers7b078e82014-09-10 14:44:24 -07003622 uint32_t next_insn_idx = work_insn_idx_ + inst->SizeInCodeUnits();
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08003623 if (next_insn_idx >= code_item_accessor_.InsnsSizeInCodeUnits()) {
Ian Rogers6d376ae2013-07-23 15:12:40 -07003624 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Execution can walk off end of code area";
3625 return false;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003626 }
Ian Rogers6d376ae2013-07-23 15:12:40 -07003627 // The only way to get to a move-exception instruction is to get thrown there. Make sure the
3628 // next instruction isn't one.
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08003629 if (!CheckNotMoveException(code_item_accessor_.Insns(), next_insn_idx)) {
Ian Rogers6d376ae2013-07-23 15:12:40 -07003630 return false;
3631 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07003632 if (nullptr != fallthrough_line) {
Ian Rogers6d376ae2013-07-23 15:12:40 -07003633 // Make workline consistent with fallthrough computed from peephole optimization.
3634 work_line_->CopyFromLine(fallthrough_line.get());
3635 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07003636 if (GetInstructionFlags(next_insn_idx).IsReturn()) {
Ian Rogersb8c78592013-07-25 23:52:52 +00003637 // For returns we only care about the operand to the return, all other registers are dead.
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08003638 const Instruction* ret_inst = &code_item_accessor_.InstructionAt(next_insn_idx);
Andreas Gampea727e372015-08-25 09:22:37 -07003639 AdjustReturnLine(this, ret_inst, work_line_.get());
Ian Rogersb8c78592013-07-25 23:52:52 +00003640 }
Ian Rogers6d376ae2013-07-23 15:12:40 -07003641 RegisterLine* next_line = reg_table_.GetLine(next_insn_idx);
Ian Rogers7b078e82014-09-10 14:44:24 -07003642 if (next_line != nullptr) {
Ian Rogersebbdd872014-07-07 23:53:08 -07003643 // Merge registers into what we have for the next instruction, and set the "changed" flag if
3644 // needed. If the merge changes the state of the registers then the work line will be
3645 // updated.
3646 if (!UpdateRegisters(next_insn_idx, work_line_.get(), true)) {
Ian Rogers6d376ae2013-07-23 15:12:40 -07003647 return false;
3648 }
3649 } else {
3650 /*
3651 * We're not recording register data for the next instruction, so we don't know what the
3652 * prior state was. We have to assume that something has changed and re-evaluate it.
3653 */
Mathieu Chartierde40d472015-10-15 17:47:48 -07003654 GetInstructionFlags(next_insn_idx).SetChanged();
Ian Rogers6d376ae2013-07-23 15:12:40 -07003655 }
3656 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003657
jeffhaod1f0fde2011-09-08 17:25:33 -07003658 /* If we're returning from the method, make sure monitor stack is empty. */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003659 if ((opcode_flags & Instruction::kReturn) != 0) {
Andreas Gampea727e372015-08-25 09:22:37 -07003660 work_line_->VerifyMonitorStackEmpty(this);
jeffhaobdb76512011-09-07 11:43:16 -07003661 }
3662
3663 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003664 * Update start_guess. Advance to the next instruction of that's
3665 * possible, otherwise use the branch target if one was found. If
jeffhaobdb76512011-09-07 11:43:16 -07003666 * neither of those exists we're in a return or throw; leave start_guess
3667 * alone and let the caller sort it out.
3668 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003669 if ((opcode_flags & Instruction::kContinue) != 0) {
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08003670 DCHECK_EQ(&code_item_accessor_.InstructionAt(work_insn_idx_), inst);
Ian Rogers7b078e82014-09-10 14:44:24 -07003671 *start_guess = work_insn_idx_ + inst->SizeInCodeUnits();
Elliott Hughesadb8c672012-03-06 16:49:32 -08003672 } else if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07003673 /* we're still okay if branch_target is zero */
Ian Rogersd81871c2011-10-03 13:57:23 -07003674 *start_guess = work_insn_idx_ + branch_target;
jeffhaobdb76512011-09-07 11:43:16 -07003675 }
3676
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08003677 DCHECK_LT(*start_guess, code_item_accessor_.InsnsSizeInCodeUnits());
Mathieu Chartierde40d472015-10-15 17:47:48 -07003678 DCHECK(GetInstructionFlags(*start_guess).IsOpcode());
jeffhaobdb76512011-09-07 11:43:16 -07003679
Andreas Gampea727e372015-08-25 09:22:37 -07003680 if (have_pending_runtime_throw_failure_) {
3681 have_any_pending_runtime_throw_failure_ = true;
3682 // Reset the pending_runtime_throw flag now.
3683 have_pending_runtime_throw_failure_ = false;
3684 }
3685
jeffhaobdb76512011-09-07 11:43:16 -07003686 return true;
Brian Carlstrom7934ac22013-07-26 10:54:15 -07003687} // NOLINT(readability/fn_size)
jeffhaobdb76512011-09-07 11:43:16 -07003688
Mathieu Chartierde40d472015-10-15 17:47:48 -07003689void MethodVerifier::UninstantiableError(const char* descriptor) {
3690 Fail(VerifyError::VERIFY_ERROR_NO_CLASS) << "Could not create precise reference for "
3691 << "non-instantiable klass " << descriptor;
3692}
3693
Vladimir Marko28e012a2017-12-07 11:22:59 +00003694inline bool MethodVerifier::IsInstantiableOrPrimitive(ObjPtr<mirror::Class> klass) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07003695 return klass->IsInstantiable() || klass->IsPrimitive();
3696}
3697
Andreas Gampe98be1a92017-08-28 08:25:45 -07003698template <MethodVerifier::CheckAccess C>
3699const RegType& MethodVerifier::ResolveClass(dex::TypeIndex class_idx) {
Vladimir Marko666ee3d2017-12-11 18:37:36 +00003700 ClassLinker* linker = Runtime::Current()->GetClassLinker();
Vladimir Marko28e012a2017-12-07 11:22:59 +00003701 ObjPtr<mirror::Class> klass = can_load_classes_
Vladimir Marko666ee3d2017-12-11 18:37:36 +00003702 ? linker->ResolveType(class_idx, dex_cache_, class_loader_)
3703 : linker->LookupResolvedType(class_idx, dex_cache_.Get(), class_loader_.Get());
Vladimir Marko8d6768d2017-03-14 10:13:21 +00003704 if (can_load_classes_ && klass == nullptr) {
3705 DCHECK(self_->IsExceptionPending());
3706 self_->ClearException();
3707 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07003708 const RegType* result = nullptr;
Vladimir Marko9cb0c462017-04-21 13:31:41 +01003709 if (klass != nullptr) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07003710 bool precise = klass->CannotBeAssignedFromOtherTypes();
3711 if (precise && !IsInstantiableOrPrimitive(klass)) {
3712 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
3713 UninstantiableError(descriptor);
3714 precise = false;
3715 }
Vladimir Marko28e012a2017-12-07 11:22:59 +00003716 result = reg_types_.FindClass(klass.Ptr(), precise);
Mathieu Chartierde40d472015-10-15 17:47:48 -07003717 if (result == nullptr) {
3718 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
Vladimir Marko28e012a2017-12-07 11:22:59 +00003719 result = reg_types_.InsertClass(descriptor, klass.Ptr(), precise);
Mathieu Chartierde40d472015-10-15 17:47:48 -07003720 }
3721 } else {
3722 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
3723 result = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003724 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07003725 DCHECK(result != nullptr);
3726 if (result->IsConflict()) {
3727 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
3728 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "accessing broken descriptor '" << descriptor
3729 << "' in " << GetDeclaringClass();
3730 return *result;
3731 }
David Brazdilca3c8c32016-09-06 14:04:48 +01003732
3733 // Record result of class resolution attempt.
Vladimir Marko28e012a2017-12-07 11:22:59 +00003734 VerifierDeps::MaybeRecordClassResolution(*dex_file_, class_idx, klass.Ptr());
David Brazdilca3c8c32016-09-06 14:04:48 +01003735
Andreas Gampe629be512017-08-25 17:09:32 -07003736 // If requested, check if access is allowed. Unresolved types are included in this check, as the
3737 // interpreter only tests whether access is allowed when a class is not pre-verified and runs in
3738 // the access-checks interpreter. If result is primitive, skip the access check.
3739 //
3740 // Note: we do this for unresolved classes to trigger re-verification at runtime.
3741 if (C == CheckAccess::kYes && result->IsNonZeroReferenceTypes()) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07003742 const RegType& referrer = GetDeclaringClass();
Andreas Gampe629be512017-08-25 17:09:32 -07003743 if (!referrer.CanAccess(*result)) {
3744 Fail(VERIFY_ERROR_ACCESS_CLASS) << "(possibly) illegal class access: '"
Nicolas Geoffray4e868fa2017-04-21 17:16:44 +01003745 << referrer << "' -> '" << *result << "'";
Mathieu Chartierde40d472015-10-15 17:47:48 -07003746 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07003747 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07003748 return *result;
Ian Rogersd81871c2011-10-03 13:57:23 -07003749}
3750
Andreas Gampedd309072017-08-31 07:47:37 -07003751// Instantiate ResolveClass variants. This is required as the -inl file has a function with a call
3752// to ResolveClass, and compilers may decide to inline, requiring a symbol.
Andreas Gampeae0b1f42017-08-30 17:36:26 -07003753template const RegType& MethodVerifier::ResolveClass<MethodVerifier::CheckAccess::kNo>(
3754 dex::TypeIndex class_idx);
3755template const RegType& MethodVerifier::ResolveClass<MethodVerifier::CheckAccess::kYes>(
3756 dex::TypeIndex class_idx);
3757
Ian Rogersd8f69b02014-09-10 21:43:52 +00003758const RegType& MethodVerifier::GetCaughtExceptionType() {
Ian Rogers7b078e82014-09-10 14:44:24 -07003759 const RegType* common_super = nullptr;
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08003760 if (code_item_accessor_.TriesSize() != 0) {
3761 const uint8_t* handlers_ptr = code_item_accessor_.GetCatchHandlerData();
Ian Rogersd81871c2011-10-03 13:57:23 -07003762 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
3763 for (uint32_t i = 0; i < handlers_size; i++) {
Ian Rogers0571d352011-11-03 19:51:38 -07003764 CatchHandlerIterator iterator(handlers_ptr);
3765 for (; iterator.HasNext(); iterator.Next()) {
3766 if (iterator.GetHandlerAddress() == (uint32_t) work_insn_idx_) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08003767 if (!iterator.GetHandlerTypeIndex().IsValid()) {
Ian Rogersb4903572012-10-11 11:52:56 -07003768 common_super = &reg_types_.JavaLangThrowable(false);
Ian Rogersd81871c2011-10-03 13:57:23 -07003769 } else {
Andreas Gampe98be1a92017-08-28 08:25:45 -07003770 const RegType& exception =
3771 ResolveClass<CheckAccess::kYes>(iterator.GetHandlerTypeIndex());
David Brazdilca3c8c32016-09-06 14:04:48 +01003772 if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(exception, this)) {
David Brazdil68b5c0b2016-01-19 14:25:29 +00003773 DCHECK(!exception.IsUninitializedTypes()); // Comes from dex, shouldn't be uninit.
Jeff Haoc26a56c2013-11-04 12:00:47 -08003774 if (exception.IsUnresolvedTypes()) {
3775 // We don't know enough about the type. Fail here and let runtime handle it.
3776 Fail(VERIFY_ERROR_NO_CLASS) << "unresolved exception class " << exception;
3777 return exception;
3778 } else {
3779 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unexpected non-exception class " << exception;
3780 return reg_types_.Conflict();
3781 }
Jeff Haob878f212014-04-24 16:25:36 -07003782 } else if (common_super == nullptr) {
3783 common_super = &exception;
Ian Rogers28ad40d2011-10-27 15:19:26 -07003784 } else if (common_super->Equals(exception)) {
Ian Rogersc4762272012-02-01 15:55:55 -08003785 // odd case, but nothing to do
Ian Rogersd81871c2011-10-03 13:57:23 -07003786 } else {
David Brazdilca3c8c32016-09-06 14:04:48 +01003787 common_super = &common_super->Merge(exception, &reg_types_, this);
Mathieu Chartierc2a2bda2018-03-12 11:21:52 -07003788 if (FailOrAbort(reg_types_.JavaLangThrowable(false).IsAssignableFrom(
David Brazdilca3c8c32016-09-06 14:04:48 +01003789 *common_super, this),
Andreas Gampe7c038102014-10-27 20:08:46 -07003790 "java.lang.Throwable is not assignable-from common_super at ",
3791 work_insn_idx_)) {
3792 break;
3793 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003794 }
3795 }
3796 }
3797 }
Ian Rogers0571d352011-11-03 19:51:38 -07003798 handlers_ptr = iterator.EndDataPointer();
Ian Rogersd81871c2011-10-03 13:57:23 -07003799 }
3800 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003801 if (common_super == nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003802 /* no catch blocks, or no catches with classes we can find */
jeffhaod5347e02012-03-22 17:25:05 -07003803 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unable to find exception handler";
Ian Rogersad0b3a32012-04-16 14:50:24 -07003804 return reg_types_.Conflict();
Ian Rogersd81871c2011-10-03 13:57:23 -07003805 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07003806 return *common_super;
Ian Rogersd81871c2011-10-03 13:57:23 -07003807}
3808
Mathieu Chartiere401d142015-04-22 13:56:20 -07003809ArtMethod* MethodVerifier::ResolveMethodAndCheckAccess(
Alex Light7268d472016-01-20 15:50:01 -08003810 uint32_t dex_method_idx, MethodType method_type) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003811 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx);
Andreas Gampe98be1a92017-08-28 08:25:45 -07003812 const RegType& klass_type = ResolveClass<CheckAccess::kYes>(method_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003813 if (klass_type.IsConflict()) {
3814 std::string append(" in attempt to access method ");
3815 append += dex_file_->GetMethodName(method_id);
3816 AppendToLastFailMessage(append);
Ian Rogers7b078e82014-09-10 14:44:24 -07003817 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08003818 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003819 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003820 return nullptr; // Can't resolve Class so no more to do here
Ian Rogers90040192011-12-16 08:54:29 -08003821 }
Vladimir Markoba118822017-06-12 15:41:56 +01003822 ObjPtr<mirror::Class> klass = klass_type.GetClass();
Ian Rogersd8f69b02014-09-10 21:43:52 +00003823 const RegType& referrer = GetDeclaringClass();
Nicolas Geoffrayea179f42018-02-08 22:30:18 +00003824 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
3825 PointerSize pointer_size = class_linker->GetImagePointerSize();
Andreas Gampe42ef8ab2015-12-03 17:27:32 -08003826
Mathieu Chartiere401d142015-04-22 13:56:20 -07003827 ArtMethod* res_method = dex_cache_->GetResolvedMethod(dex_method_idx, pointer_size);
Ian Rogers7b078e82014-09-10 14:44:24 -07003828 if (res_method == nullptr) {
Nicolas Geoffrayea179f42018-02-08 22:30:18 +00003829 res_method = class_linker->FindResolvedMethod(
3830 klass, dex_cache_.Get(), class_loader_.Get(), dex_method_idx);
Ian Rogersd81871c2011-10-03 13:57:23 -07003831 }
David Brazdilca3c8c32016-09-06 14:04:48 +01003832
Vladimir Markoba118822017-06-12 15:41:56 +01003833 // Record result of method resolution attempt. The klass resolution has recorded whether
3834 // the class is an interface or not and therefore the type of the lookup performed above.
3835 // TODO: Maybe we should not record dependency if the invoke type does not match the lookup type.
3836 VerifierDeps::MaybeRecordMethodResolution(*dex_file_, dex_method_idx, res_method);
3837
Alex Lightafb66472017-08-01 09:54:49 -07003838 bool must_fail = false;
3839 // This is traditional and helps with screwy bytecode. It will tell you that, yes, a method
3840 // exists, but that it's called incorrectly. This significantly helps debugging, as locally it's
3841 // hard to see the differences.
3842 // If we don't have res_method here we must fail. Just use this bool to make sure of that with a
3843 // DCHECK.
Vladimir Markoba118822017-06-12 15:41:56 +01003844 if (res_method == nullptr) {
Alex Lightafb66472017-08-01 09:54:49 -07003845 must_fail = true;
Vladimir Markoba118822017-06-12 15:41:56 +01003846 // Try to find the method also with the other type for better error reporting below
3847 // but do not store such bogus lookup result in the DexCache or VerifierDeps.
3848 if (klass->IsInterface()) {
Alex Lightafb66472017-08-01 09:54:49 -07003849 // NB This is normally not really allowed but we want to get any static or private object
3850 // methods for error message purposes. This will never be returned.
3851 // TODO We might want to change the verifier to not require this.
Vladimir Markoba118822017-06-12 15:41:56 +01003852 res_method = klass->FindClassMethod(dex_cache_.Get(), dex_method_idx, pointer_size);
3853 } else {
3854 // If there was an interface method with the same signature,
3855 // we would have found it also in the "copied" methods.
3856 DCHECK(klass->FindInterfaceMethod(dex_cache_.Get(), dex_method_idx, pointer_size) == nullptr);
3857 }
3858 }
David Brazdilca3c8c32016-09-06 14:04:48 +01003859
3860 if (res_method == nullptr) {
3861 Fail(VERIFY_ERROR_NO_METHOD) << "couldn't find method "
David Sehr709b0702016-10-13 09:12:37 -07003862 << klass->PrettyDescriptor() << "."
David Brazdilca3c8c32016-09-06 14:04:48 +01003863 << dex_file_->GetMethodName(method_id) << " "
3864 << dex_file_->GetMethodSignature(method_id);
3865 return nullptr;
3866 }
3867
Ian Rogersd81871c2011-10-03 13:57:23 -07003868 // Make sure calls to constructors are "direct". There are additional restrictions but we don't
3869 // enforce them here.
3870 if (res_method->IsConstructor() && method_type != METHOD_DIRECT) {
jeffhaod5347e02012-03-22 17:25:05 -07003871 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting non-direct call to constructor "
David Sehr709b0702016-10-13 09:12:37 -07003872 << res_method->PrettyMethod();
Ian Rogers7b078e82014-09-10 14:44:24 -07003873 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003874 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003875 // Disallow any calls to class initializers.
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003876 if (res_method->IsClassInitializer()) {
jeffhaod5347e02012-03-22 17:25:05 -07003877 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting call to class initializer "
David Sehr709b0702016-10-13 09:12:37 -07003878 << res_method->PrettyMethod();
Ian Rogers7b078e82014-09-10 14:44:24 -07003879 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003880 }
Andreas Gampe42ef8ab2015-12-03 17:27:32 -08003881
3882 // Check that interface methods are static or match interface classes.
3883 // We only allow statics if we don't have default methods enabled.
3884 //
3885 // Note: this check must be after the initializer check, as those are required to fail a class,
3886 // while this check implies an IncompatibleClassChangeError.
3887 if (klass->IsInterface()) {
Alex Lightb55f1ac2016-04-12 15:50:55 -07003888 // methods called on interfaces should be invoke-interface, invoke-super, invoke-direct (if
Mathieu Chartierf6e31472017-12-28 13:32:08 -08003889 // default methods are supported for the dex file), or invoke-static.
Andreas Gampe42ef8ab2015-12-03 17:27:32 -08003890 if (method_type != METHOD_INTERFACE &&
Neil Fuller9724c632016-01-07 15:42:47 +00003891 method_type != METHOD_STATIC &&
Mathieu Chartierf6e31472017-12-28 13:32:08 -08003892 (!dex_file_->SupportsDefaultMethods() ||
Alex Lightb55f1ac2016-04-12 15:50:55 -07003893 method_type != METHOD_DIRECT) &&
Neil Fuller9724c632016-01-07 15:42:47 +00003894 method_type != METHOD_SUPER) {
Andreas Gampe42ef8ab2015-12-03 17:27:32 -08003895 Fail(VERIFY_ERROR_CLASS_CHANGE)
David Sehr709b0702016-10-13 09:12:37 -07003896 << "non-interface method " << dex_file_->PrettyMethod(dex_method_idx)
3897 << " is in an interface class " << klass->PrettyClass();
Andreas Gampe42ef8ab2015-12-03 17:27:32 -08003898 return nullptr;
3899 }
3900 } else {
3901 if (method_type == METHOD_INTERFACE) {
3902 Fail(VERIFY_ERROR_CLASS_CHANGE)
David Sehr709b0702016-10-13 09:12:37 -07003903 << "interface method " << dex_file_->PrettyMethod(dex_method_idx)
3904 << " is in a non-interface class " << klass->PrettyClass();
Andreas Gampe42ef8ab2015-12-03 17:27:32 -08003905 return nullptr;
3906 }
3907 }
3908
Alex Lightafb66472017-08-01 09:54:49 -07003909 // Check specifically for non-public object methods being provided for interface dispatch. This
3910 // can occur if we failed to find a method with FindInterfaceMethod but later find one with
3911 // FindClassMethod for error message use.
3912 if (method_type == METHOD_INTERFACE &&
3913 res_method->GetDeclaringClass()->IsObjectClass() &&
3914 !res_method->IsPublic()) {
3915 Fail(VERIFY_ERROR_NO_METHOD) << "invoke-interface " << klass->PrettyDescriptor() << "."
3916 << dex_file_->GetMethodName(method_id) << " "
3917 << dex_file_->GetMethodSignature(method_id) << " resolved to "
3918 << "non-public object method " << res_method->PrettyMethod() << " "
3919 << "but non-public Object methods are excluded from interface "
3920 << "method resolution.";
3921 return nullptr;
3922 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003923 // Check if access is allowed.
Ian Rogersad0b3a32012-04-16 14:50:24 -07003924 if (!referrer.CanAccessMember(res_method->GetDeclaringClass(), res_method->GetAccessFlags())) {
David Sehr709b0702016-10-13 09:12:37 -07003925 Fail(VERIFY_ERROR_ACCESS_METHOD) << "illegal method access (call "
3926 << res_method->PrettyMethod()
Ian Rogersad0b3a32012-04-16 14:50:24 -07003927 << " from " << referrer << ")";
jeffhaob57e9522012-04-26 18:08:21 -07003928 return res_method;
jeffhao8cd6dda2012-02-22 10:15:34 -08003929 }
jeffhaode0d9c92012-02-27 13:58:13 -08003930 // Check that invoke-virtual and invoke-super are not used on private methods of the same class.
Alex Light7268d472016-01-20 15:50:01 -08003931 if (res_method->IsPrivate() && (method_type == METHOD_VIRTUAL || method_type == METHOD_SUPER)) {
jeffhaod5347e02012-03-22 17:25:05 -07003932 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invoke-super/virtual can't be used on private method "
David Sehr709b0702016-10-13 09:12:37 -07003933 << res_method->PrettyMethod();
Ian Rogers7b078e82014-09-10 14:44:24 -07003934 return nullptr;
jeffhaode0d9c92012-02-27 13:58:13 -08003935 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003936 // See if the method type implied by the invoke instruction matches the access flags for the
Orion Hodsoncfa325e2016-10-13 10:25:54 +01003937 // target method. The flags for METHOD_POLYMORPHIC are based on there being precisely two
3938 // signature polymorphic methods supported by the run-time which are native methods with variable
3939 // arguments.
Brian Carlstrombe6fa5e2014-12-09 20:15:42 -08003940 if ((method_type == METHOD_DIRECT && (!res_method->IsDirect() || res_method->IsStatic())) ||
Ian Rogersd81871c2011-10-03 13:57:23 -07003941 (method_type == METHOD_STATIC && !res_method->IsStatic()) ||
Alex Light7268d472016-01-20 15:50:01 -08003942 ((method_type == METHOD_SUPER ||
3943 method_type == METHOD_VIRTUAL ||
Orion Hodsoncfa325e2016-10-13 10:25:54 +01003944 method_type == METHOD_INTERFACE) && res_method->IsDirect()) ||
3945 ((method_type == METHOD_POLYMORPHIC) &&
3946 (!res_method->IsNative() || !res_method->IsVarargs()))) {
Ian Rogers2fc14272012-08-30 10:56:57 -07003947 Fail(VERIFY_ERROR_CLASS_CHANGE) << "invoke type (" << method_type << ") does not match method "
Orion Hodsoncfa325e2016-10-13 10:25:54 +01003948 "type of " << res_method->PrettyMethod();
Ian Rogers7b078e82014-09-10 14:44:24 -07003949 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003950 }
Alex Lightafb66472017-08-01 09:54:49 -07003951 // Make sure we weren't expecting to fail.
3952 DCHECK(!must_fail) << "invoke type (" << method_type << ")"
3953 << klass->PrettyDescriptor() << "."
3954 << dex_file_->GetMethodName(method_id) << " "
3955 << dex_file_->GetMethodSignature(method_id) << " unexpectedly resolved to "
3956 << res_method->PrettyMethod() << " without error. Initially this method was "
3957 << "not found so we were expecting to fail for some reason.";
jeffhao8cd6dda2012-02-22 10:15:34 -08003958 return res_method;
3959}
3960
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003961template <class T>
Mathieu Chartiere401d142015-04-22 13:56:20 -07003962ArtMethod* MethodVerifier::VerifyInvocationArgsFromIterator(
3963 T* it, const Instruction* inst, MethodType method_type, bool is_range, ArtMethod* res_method) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003964 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
3965 // match the call to the signature. Also, we might be calling through an abstract method
3966 // definition (which doesn't have register count values).
Orion Hodsoncfa325e2016-10-13 10:25:54 +01003967 const size_t expected_args = inst->VRegA();
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003968 /* caught by static verifier */
3969 DCHECK(is_range || expected_args <= 5);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003970
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08003971 if (expected_args > code_item_accessor_.OutsSize()) {
Orion Hodson1cda7c22017-08-10 13:06:45 +01003972 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08003973 << ") exceeds outsSize ("
3974 << code_item_accessor_.OutsSize() << ")";
Orion Hodson1cda7c22017-08-10 13:06:45 +01003975 return nullptr;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003976 }
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003977
3978 /*
3979 * Check the "this" argument, which must be an instance of the class that declared the method.
3980 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
3981 * rigorous check here (which is okay since we have to do it at runtime).
3982 */
3983 if (method_type != METHOD_STATIC) {
Orion Hodsoncfa325e2016-10-13 10:25:54 +01003984 const RegType& actual_arg_type = work_line_->GetInvocationThis(this, inst);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003985 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
3986 CHECK(have_pending_hard_failure_);
3987 return nullptr;
3988 }
David Brazdil68b5c0b2016-01-19 14:25:29 +00003989 bool is_init = false;
3990 if (actual_arg_type.IsUninitializedTypes()) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003991 if (res_method) {
3992 if (!res_method->IsConstructor()) {
3993 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
3994 return nullptr;
3995 }
3996 } else {
3997 // Check whether the name of the called method is "<init>"
3998 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Jeff Hao0d087272014-08-04 14:47:17 -07003999 if (strcmp(dex_file_->GetMethodName(dex_file_->GetMethodId(method_idx)), "<init>") != 0) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004000 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
4001 return nullptr;
4002 }
4003 }
David Brazdil68b5c0b2016-01-19 14:25:29 +00004004 is_init = true;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004005 }
David Brazdil68b5c0b2016-01-19 14:25:29 +00004006 const RegType& adjusted_type = is_init
4007 ? GetRegTypeCache()->FromUninitialized(actual_arg_type)
4008 : actual_arg_type;
Andreas Gampeeed3a5d2017-11-29 14:58:34 -08004009 if (method_type != METHOD_INTERFACE && !adjusted_type.IsZeroOrNull()) {
Ian Rogersd8f69b02014-09-10 21:43:52 +00004010 const RegType* res_method_class;
Andreas Gamped9e23012015-06-04 22:19:58 -07004011 // Miranda methods have the declaring interface as their declaring class, not the abstract
4012 // class. It would be wrong to use this for the type check (interface type checks are
4013 // postponed to runtime).
4014 if (res_method != nullptr && !res_method->IsMiranda()) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004015 mirror::Class* klass = res_method->GetDeclaringClass();
Ian Rogers1ff3c982014-08-12 02:30:58 -07004016 std::string temp;
Andreas Gampef23f33d2015-06-23 14:18:17 -07004017 res_method_class = &FromClass(klass->GetDescriptor(&temp), klass,
4018 klass->CannotBeAssignedFromOtherTypes());
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004019 } else {
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004020 const uint32_t method_idx = inst->VRegB();
Andreas Gampea5b09a62016-11-17 15:21:22 -08004021 const dex::TypeIndex class_idx = dex_file_->GetMethodId(method_idx).class_idx_;
Mathieu Chartierde40d472015-10-15 17:47:48 -07004022 res_method_class = &reg_types_.FromDescriptor(
4023 GetClassLoader(),
4024 dex_file_->StringByTypeIdx(class_idx),
4025 false);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004026 }
David Brazdilca3c8c32016-09-06 14:04:48 +01004027 if (!res_method_class->IsAssignableFrom(adjusted_type, this)) {
David Brazdil68b5c0b2016-01-19 14:25:29 +00004028 Fail(adjusted_type.IsUnresolvedTypes()
4029 ? VERIFY_ERROR_NO_CLASS
4030 : VERIFY_ERROR_BAD_CLASS_SOFT)
4031 << "'this' argument '" << actual_arg_type << "' not instance of '"
4032 << *res_method_class << "'";
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004033 // Continue on soft failures. We need to find possible hard failures to avoid problems in
4034 // the compiler.
4035 if (have_pending_hard_failure_) {
4036 return nullptr;
4037 }
4038 }
4039 }
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004040 }
4041
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004042 uint32_t arg[5];
4043 if (!is_range) {
4044 inst->GetVarArgs(arg);
4045 }
4046 uint32_t sig_registers = (method_type == METHOD_STATIC) ? 0 : 1;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004047 for ( ; it->HasNext(); it->Next()) {
4048 if (sig_registers >= expected_args) {
4049 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, expected " << inst->VRegA() <<
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004050 " argument registers, method signature has " << sig_registers + 1 << " or more";
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004051 return nullptr;
4052 }
4053
4054 const char* param_descriptor = it->GetDescriptor();
4055
4056 if (param_descriptor == nullptr) {
4057 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation because of missing signature "
4058 "component";
4059 return nullptr;
4060 }
4061
Ian Rogersd8f69b02014-09-10 21:43:52 +00004062 const RegType& reg_type = reg_types_.FromDescriptor(GetClassLoader(), param_descriptor, false);
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004063 uint32_t get_reg = is_range ? inst->VRegC() + static_cast<uint32_t>(sig_registers) :
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004064 arg[sig_registers];
4065 if (reg_type.IsIntegralTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004066 const RegType& src_type = work_line_->GetRegisterType(this, get_reg);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004067 if (!src_type.IsIntegralTypes()) {
4068 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register v" << get_reg << " has type " << src_type
4069 << " but expected " << reg_type;
Andreas Gampeb588f4c2015-05-26 13:35:39 -07004070 return nullptr;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004071 }
Andreas Gampeda9badb2015-06-05 20:22:12 -07004072 } else {
4073 if (!work_line_->VerifyRegisterType(this, get_reg, reg_type)) {
4074 // Continue on soft failures. We need to find possible hard failures to avoid problems in
4075 // the compiler.
4076 if (have_pending_hard_failure_) {
4077 return nullptr;
4078 }
4079 } else if (reg_type.IsLongOrDoubleTypes()) {
4080 // Check that registers are consecutive (for non-range invokes). Invokes are the only
4081 // instructions not specifying register pairs by the first component, but require them
4082 // nonetheless. Only check when there's an actual register in the parameters. If there's
4083 // none, this will fail below.
4084 if (!is_range && sig_registers + 1 < expected_args) {
4085 uint32_t second_reg = arg[sig_registers + 1];
4086 if (second_reg != get_reg + 1) {
4087 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, long or double parameter "
4088 "at index " << sig_registers << " is not a pair: " << get_reg << " + "
4089 << second_reg << ".";
4090 return nullptr;
4091 }
4092 }
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004093 }
4094 }
4095 sig_registers += reg_type.IsLongOrDoubleTypes() ? 2 : 1;
4096 }
4097 if (expected_args != sig_registers) {
4098 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, expected " << expected_args <<
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004099 " argument registers, method signature has " << sig_registers;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004100 return nullptr;
4101 }
4102 return res_method;
4103}
4104
4105void MethodVerifier::VerifyInvocationArgsUnresolvedMethod(const Instruction* inst,
4106 MethodType method_type,
4107 bool is_range) {
4108 // As the method may not have been resolved, make this static check against what we expect.
4109 // The main reason for this code block is to fail hard when we find an illegal use, e.g.,
4110 // wrong number of arguments or wrong primitive types, even if the method could not be resolved.
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004111 const uint32_t method_idx = inst->VRegB();
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004112 DexFileParameterIterator it(*dex_file_,
4113 dex_file_->GetProtoId(dex_file_->GetMethodId(method_idx).proto_idx_));
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004114 VerifyInvocationArgsFromIterator(&it, inst, method_type, is_range, nullptr);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004115}
4116
Orion Hodsonc069a302017-01-18 09:23:12 +00004117bool MethodVerifier::CheckCallSite(uint32_t call_site_idx) {
Orion Hodson3a842f52017-04-21 15:24:10 +01004118 if (call_site_idx >= dex_file_->NumCallSiteIds()) {
4119 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Bad call site id #" << call_site_idx
4120 << " >= " << dex_file_->NumCallSiteIds();
4121 return false;
4122 }
4123
Orion Hodsonc069a302017-01-18 09:23:12 +00004124 CallSiteArrayValueIterator it(*dex_file_, dex_file_->GetCallSiteId(call_site_idx));
4125 // Check essential arguments are provided. The dex file verifier has verified indicies of the
4126 // main values (method handle, name, method_type).
4127 if (it.Size() < 3) {
4128 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Call site #" << call_site_idx
4129 << " has too few arguments: "
Orion Hodsona5dca522018-02-27 12:42:11 +00004130 << it.Size() << " < 3";
Orion Hodsonc069a302017-01-18 09:23:12 +00004131 return false;
4132 }
4133
Orion Hodson3a842f52017-04-21 15:24:10 +01004134 // Get and check the first argument: the method handle (index range
4135 // checked by the dex file verifier).
Orion Hodsonc069a302017-01-18 09:23:12 +00004136 uint32_t method_handle_idx = static_cast<uint32_t>(it.GetJavaValue().i);
Orion Hodsona5dca522018-02-27 12:42:11 +00004137 if (method_handle_idx > dex_file_->NumMethodHandles()) {
4138 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Call site id #" << call_site_idx
4139 << " method handle index invalid " << method_handle_idx
4140 << " >= " << dex_file_->NumMethodHandles();
4141 return false;
4142 }
Orion Hodson3a842f52017-04-21 15:24:10 +01004143
Orion Hodsonc069a302017-01-18 09:23:12 +00004144 const DexFile::MethodHandleItem& mh = dex_file_->GetMethodHandle(method_handle_idx);
4145 if (mh.method_handle_type_ != static_cast<uint16_t>(DexFile::MethodHandleType::kInvokeStatic)) {
4146 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Call site #" << call_site_idx
Orion Hodson631827d2017-04-10 14:53:47 +01004147 << " argument 0 method handle type is not InvokeStatic: "
4148 << mh.method_handle_type_;
Orion Hodsonc069a302017-01-18 09:23:12 +00004149 return false;
4150 }
Orion Hodsonc069a302017-01-18 09:23:12 +00004151 return true;
4152}
4153
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004154class MethodParamListDescriptorIterator {
4155 public:
Mathieu Chartiere401d142015-04-22 13:56:20 -07004156 explicit MethodParamListDescriptorIterator(ArtMethod* res_method) :
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004157 res_method_(res_method), pos_(0), params_(res_method->GetParameterTypeList()),
4158 params_size_(params_ == nullptr ? 0 : params_->Size()) {
4159 }
4160
4161 bool HasNext() {
4162 return pos_ < params_size_;
4163 }
4164
4165 void Next() {
4166 ++pos_;
4167 }
4168
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07004169 const char* GetDescriptor() REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004170 return res_method_->GetTypeDescriptorFromTypeIdx(params_->GetTypeItem(pos_).type_idx_);
4171 }
4172
4173 private:
Mathieu Chartiere401d142015-04-22 13:56:20 -07004174 ArtMethod* res_method_;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004175 size_t pos_;
4176 const DexFile::TypeList* params_;
4177 const size_t params_size_;
4178};
4179
Mathieu Chartiere401d142015-04-22 13:56:20 -07004180ArtMethod* MethodVerifier::VerifyInvocationArgs(
Alex Light7268d472016-01-20 15:50:01 -08004181 const Instruction* inst, MethodType method_type, bool is_range) {
jeffhao8cd6dda2012-02-22 10:15:34 -08004182 // Resolve the method. This could be an abstract or concrete method depending on what sort of call
4183 // we're making.
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004184 const uint32_t method_idx = inst->VRegB();
Alex Light7268d472016-01-20 15:50:01 -08004185 ArtMethod* res_method = ResolveMethodAndCheckAccess(method_idx, method_type);
Ian Rogers7b078e82014-09-10 14:44:24 -07004186 if (res_method == nullptr) { // error or class is unresolved
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004187 // Check what we can statically.
4188 if (!have_pending_hard_failure_) {
4189 VerifyInvocationArgsUnresolvedMethod(inst, method_type, is_range);
4190 }
4191 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08004192 }
4193
Ian Rogersd81871c2011-10-03 13:57:23 -07004194 // If we're using invoke-super(method), make sure that the executing method's class' superclass
Alex Light705ad492015-09-21 11:36:30 -07004195 // has a vtable entry for the target method. Or the target is on a interface.
Alex Light7268d472016-01-20 15:50:01 -08004196 if (method_type == METHOD_SUPER) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08004197 dex::TypeIndex class_idx = dex_file_->GetMethodId(method_idx).class_idx_;
David Brazdilca3c8c32016-09-06 14:04:48 +01004198 const RegType& reference_type = reg_types_.FromDescriptor(
4199 GetClassLoader(),
4200 dex_file_->StringByTypeIdx(class_idx),
4201 false);
4202 if (reference_type.IsUnresolvedTypes()) {
Alex Lightfedd91d2016-01-07 14:49:16 -08004203 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "Unable to find referenced class from invoke-super";
4204 return nullptr;
4205 }
David Brazdilca3c8c32016-09-06 14:04:48 +01004206 if (reference_type.GetClass()->IsInterface()) {
Alex Lightfedd91d2016-01-07 14:49:16 -08004207 // TODO Can we verify anything else.
David Brazdil15fc7292016-09-02 14:13:18 +01004208 if (class_idx == class_def_.class_idx_) {
Alex Lightfedd91d2016-01-07 14:49:16 -08004209 Fail(VERIFY_ERROR_CLASS_CHANGE) << "Cannot invoke-super on self as interface";
Alex Light55ea94d2016-03-15 09:50:26 -07004210 return nullptr;
Alex Lightfedd91d2016-01-07 14:49:16 -08004211 }
4212 // TODO Revisit whether we want to allow invoke-super on direct interfaces only like the JLS
4213 // does.
Alex Light55ea94d2016-03-15 09:50:26 -07004214 if (!GetDeclaringClass().HasClass()) {
4215 Fail(VERIFY_ERROR_NO_CLASS) << "Unable to resolve the full class of 'this' used in an"
4216 << "interface invoke-super";
4217 return nullptr;
David Brazdilca3c8c32016-09-06 14:04:48 +01004218 } else if (!reference_type.IsStrictlyAssignableFrom(GetDeclaringClass(), this)) {
Alex Lightfedd91d2016-01-07 14:49:16 -08004219 Fail(VERIFY_ERROR_CLASS_CHANGE)
David Sehr709b0702016-10-13 09:12:37 -07004220 << "invoke-super in " << mirror::Class::PrettyClass(GetDeclaringClass().GetClass())
4221 << " in method "
4222 << dex_file_->PrettyMethod(dex_method_idx_) << " to method "
4223 << dex_file_->PrettyMethod(method_idx) << " references "
4224 << "non-super-interface type " << mirror::Class::PrettyClass(reference_type.GetClass());
Alex Lightfedd91d2016-01-07 14:49:16 -08004225 return nullptr;
Alex Light705ad492015-09-21 11:36:30 -07004226 }
4227 } else {
4228 const RegType& super = GetDeclaringClass().GetSuperClass(&reg_types_);
4229 if (super.IsUnresolvedTypes()) {
4230 Fail(VERIFY_ERROR_NO_METHOD) << "unknown super class in invoke-super from "
David Sehr709b0702016-10-13 09:12:37 -07004231 << dex_file_->PrettyMethod(dex_method_idx_)
4232 << " to super " << res_method->PrettyMethod();
Alex Light705ad492015-09-21 11:36:30 -07004233 return nullptr;
4234 }
David Brazdilca3c8c32016-09-06 14:04:48 +01004235 if (!reference_type.IsStrictlyAssignableFrom(GetDeclaringClass(), this) ||
Aart Bikf663e342016-04-04 17:28:59 -07004236 (res_method->GetMethodIndex() >= super.GetClass()->GetVTableLength())) {
Alex Light705ad492015-09-21 11:36:30 -07004237 Fail(VERIFY_ERROR_NO_METHOD) << "invalid invoke-super from "
David Sehr709b0702016-10-13 09:12:37 -07004238 << dex_file_->PrettyMethod(dex_method_idx_)
Alex Light705ad492015-09-21 11:36:30 -07004239 << " to super " << super
4240 << "." << res_method->GetName()
4241 << res_method->GetSignature();
4242 return nullptr;
4243 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004244 }
4245 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08004246
Andreas Gampe74979b12017-05-16 09:28:06 -07004247 if (UNLIKELY(method_type == METHOD_POLYMORPHIC)) {
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004248 // Process the signature of the calling site that is invoking the method handle.
4249 DexFileParameterIterator it(*dex_file_, dex_file_->GetProtoId(inst->VRegH()));
4250 return VerifyInvocationArgsFromIterator(&it, inst, method_type, is_range, res_method);
4251 } else {
4252 // Process the target method's signature.
4253 MethodParamListDescriptorIterator it(res_method);
4254 return VerifyInvocationArgsFromIterator(&it, inst, method_type, is_range, res_method);
4255 }
4256}
4257
4258bool MethodVerifier::CheckSignaturePolymorphicMethod(ArtMethod* method) {
4259 mirror::Class* klass = method->GetDeclaringClass();
Orion Hodsonfe92d122018-01-02 10:45:17 +00004260 const char* method_name = method->GetName();
4261
4262 const char* expected_return_descriptor;
4263 if (klass == mirror::MethodHandle::StaticClass()) {
4264 expected_return_descriptor = mirror::MethodHandle::GetReturnTypeDescriptor(method_name);
4265 } else if (klass == mirror::VarHandle::StaticClass()) {
4266 expected_return_descriptor = mirror::VarHandle::GetReturnTypeDescriptor(method_name);
Orion Hodson928033d2018-02-07 05:30:54 +00004267 // TODO: add compiler support for VarHandle accessor methods (b/71781600)
4268 Fail(VERIFY_ERROR_FORCE_INTERPRETER);
Orion Hodsonfe92d122018-01-02 10:45:17 +00004269 } else {
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004270 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
Orion Hodsonfe92d122018-01-02 10:45:17 +00004271 << "Signature polymorphic method in unsuppported class: " << klass->PrettyDescriptor();
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004272 return false;
4273 }
4274
Orion Hodsonfe92d122018-01-02 10:45:17 +00004275 if (expected_return_descriptor == nullptr) {
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004276 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
4277 << "Signature polymorphic method name invalid: " << method_name;
4278 return false;
4279 }
4280
4281 const DexFile::TypeList* types = method->GetParameterTypeList();
4282 if (types->Size() != 1) {
4283 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
4284 << "Signature polymorphic method has too many arguments " << types->Size() << " != 1";
4285 return false;
4286 }
4287
4288 const dex::TypeIndex argument_type_index = types->GetTypeItem(0).type_idx_;
4289 const char* argument_descriptor = method->GetTypeDescriptorFromTypeIdx(argument_type_index);
4290 if (strcmp(argument_descriptor, "[Ljava/lang/Object;") != 0) {
4291 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
4292 << "Signature polymorphic method has unexpected argument type: " << argument_descriptor;
4293 return false;
4294 }
4295
4296 const char* return_descriptor = method->GetReturnTypeDescriptor();
Orion Hodsonfe92d122018-01-02 10:45:17 +00004297 if (strcmp(return_descriptor, expected_return_descriptor) != 0) {
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004298 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
Orion Hodsonfe92d122018-01-02 10:45:17 +00004299 << "Signature polymorphic method has unexpected return type: " << return_descriptor
4300 << " != " << expected_return_descriptor;
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004301 return false;
4302 }
4303
4304 return true;
4305}
4306
4307bool MethodVerifier::CheckSignaturePolymorphicReceiver(const Instruction* inst) {
4308 const RegType& this_type = work_line_->GetInvocationThis(this, inst);
Andreas Gampeeed3a5d2017-11-29 14:58:34 -08004309 if (this_type.IsZeroOrNull()) {
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004310 /* null pointer always passes (and always fails at run time) */
4311 return true;
4312 } else if (!this_type.IsNonZeroReferenceTypes()) {
4313 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
4314 << "invoke-polymorphic receiver is not a reference: "
4315 << this_type;
4316 return false;
4317 } else if (this_type.IsUninitializedReference()) {
4318 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
4319 << "invoke-polymorphic receiver is uninitialized: "
4320 << this_type;
4321 return false;
4322 } else if (!this_type.HasClass()) {
4323 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
4324 << "invoke-polymorphic receiver has no class: "
4325 << this_type;
4326 return false;
Orion Hodsonfe92d122018-01-02 10:45:17 +00004327 } else if (!this_type.GetClass()->IsSubClass(mirror::MethodHandle::StaticClass()) &&
4328 !this_type.GetClass()->IsSubClass(mirror::VarHandle::StaticClass())) {
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004329 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
Orion Hodsonfe92d122018-01-02 10:45:17 +00004330 << "invoke-polymorphic receiver is not a subclass of MethodHandle or VarHandle: "
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004331 << this_type;
4332 return false;
4333 }
4334 return true;
Ian Rogersd81871c2011-10-03 13:57:23 -07004335}
4336
Nicolas Geoffrayb041a402017-11-13 15:16:22 +00004337ArtMethod* MethodVerifier::GetQuickInvokedMethod(const Instruction* inst, bool is_range) {
Mathieu Chartier091d2382015-03-06 10:59:06 -08004338 if (is_range) {
4339 DCHECK_EQ(inst->Opcode(), Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
4340 } else {
4341 DCHECK_EQ(inst->Opcode(), Instruction::INVOKE_VIRTUAL_QUICK);
4342 }
Nicolas Geoffrayb041a402017-11-13 15:16:22 +00004343
4344 DCHECK(method_being_verified_ != nullptr);
4345 uint16_t method_idx = method_being_verified_->GetIndexFromQuickening(work_insn_idx_);
4346 CHECK_NE(method_idx, DexFile::kDexNoIndex16);
4347 return ResolveMethodAndCheckAccess(method_idx, METHOD_VIRTUAL);
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004348}
4349
Mathieu Chartiere401d142015-04-22 13:56:20 -07004350ArtMethod* MethodVerifier::VerifyInvokeVirtualQuickArgs(const Instruction* inst, bool is_range) {
Andreas Gampe76bd8802014-12-10 16:43:58 -08004351 DCHECK(Runtime::Current()->IsStarted() || verify_to_dump_)
David Sehr709b0702016-10-13 09:12:37 -07004352 << dex_file_->PrettyMethod(dex_method_idx_, true) << "@" << work_insn_idx_;
Andreas Gampe76bd8802014-12-10 16:43:58 -08004353
Nicolas Geoffrayb041a402017-11-13 15:16:22 +00004354 ArtMethod* res_method = GetQuickInvokedMethod(inst, is_range);
Ian Rogers7b078e82014-09-10 14:44:24 -07004355 if (res_method == nullptr) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004356 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer method from " << inst->Name();
Ian Rogers7b078e82014-09-10 14:44:24 -07004357 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004358 }
Mathieu Chartierc2a2bda2018-03-12 11:21:52 -07004359 if (FailOrAbort(!res_method->IsDirect(),
4360 "Quick-invoked method is direct at ",
Andreas Gampe7c038102014-10-27 20:08:46 -07004361 work_insn_idx_)) {
4362 return nullptr;
4363 }
Mathieu Chartierc2a2bda2018-03-12 11:21:52 -07004364 if (FailOrAbort(!res_method->IsStatic(),
4365 "Quick-invoked method is static at ",
Andreas Gampe7c038102014-10-27 20:08:46 -07004366 work_insn_idx_)) {
4367 return nullptr;
4368 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004369
4370 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
4371 // match the call to the signature. Also, we might be calling through an abstract method
4372 // definition (which doesn't have register count values).
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004373 const RegType& actual_arg_type = work_line_->GetInvocationThis(this, inst);
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004374 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
Ian Rogers7b078e82014-09-10 14:44:24 -07004375 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004376 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004377 const size_t expected_args = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
4378 /* caught by static verifier */
4379 DCHECK(is_range || expected_args <= 5);
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08004380 if (expected_args > code_item_accessor_.OutsSize()) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004381 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08004382 << ") exceeds outsSize (" << code_item_accessor_.OutsSize() << ")";
Ian Rogers7b078e82014-09-10 14:44:24 -07004383 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004384 }
4385
4386 /*
4387 * Check the "this" argument, which must be an instance of the class that declared the method.
4388 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
4389 * rigorous check here (which is okay since we have to do it at runtime).
4390 */
David Brazdil68b5c0b2016-01-19 14:25:29 +00004391 // Note: given an uninitialized type, this should always fail. Constructors aren't virtual.
4392 if (actual_arg_type.IsUninitializedTypes() && !res_method->IsConstructor()) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004393 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
Ian Rogers7b078e82014-09-10 14:44:24 -07004394 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004395 }
Andreas Gampeeed3a5d2017-11-29 14:58:34 -08004396 if (!actual_arg_type.IsZeroOrNull()) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004397 mirror::Class* klass = res_method->GetDeclaringClass();
Ian Rogers1ff3c982014-08-12 02:30:58 -07004398 std::string temp;
Ian Rogersd8f69b02014-09-10 21:43:52 +00004399 const RegType& res_method_class =
Andreas Gampef23f33d2015-06-23 14:18:17 -07004400 FromClass(klass->GetDescriptor(&temp), klass, klass->CannotBeAssignedFromOtherTypes());
David Brazdilca3c8c32016-09-06 14:04:48 +01004401 if (!res_method_class.IsAssignableFrom(actual_arg_type, this)) {
David Brazdil68b5c0b2016-01-19 14:25:29 +00004402 Fail(actual_arg_type.IsUninitializedTypes() // Just overcautious - should have never
4403 ? VERIFY_ERROR_BAD_CLASS_HARD // quickened this.
4404 : actual_arg_type.IsUnresolvedTypes()
4405 ? VERIFY_ERROR_NO_CLASS
4406 : VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004407 << "' not instance of '" << res_method_class << "'";
Ian Rogers7b078e82014-09-10 14:44:24 -07004408 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004409 }
4410 }
4411 /*
4412 * Process the target method's signature. This signature may or may not
4413 * have been verified, so we can't assume it's properly formed.
4414 */
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07004415 const DexFile::TypeList* params = res_method->GetParameterTypeList();
Ian Rogers7b078e82014-09-10 14:44:24 -07004416 size_t params_size = params == nullptr ? 0 : params->Size();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004417 uint32_t arg[5];
4418 if (!is_range) {
Ian Rogers29a26482014-05-02 15:27:29 -07004419 inst->GetVarArgs(arg);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004420 }
4421 size_t actual_args = 1;
4422 for (size_t param_index = 0; param_index < params_size; param_index++) {
4423 if (actual_args >= expected_args) {
David Sehr709b0702016-10-13 09:12:37 -07004424 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invalid call to '"
4425 << res_method->PrettyMethod()
Brian Carlstrom93c33962013-07-26 10:37:43 -07004426 << "'. Expected " << expected_args
4427 << " arguments, processing argument " << actual_args
4428 << " (where longs/doubles count twice).";
Ian Rogers7b078e82014-09-10 14:44:24 -07004429 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004430 }
4431 const char* descriptor =
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07004432 res_method->GetTypeDescriptorFromTypeIdx(params->GetTypeItem(param_index).type_idx_);
Ian Rogers7b078e82014-09-10 14:44:24 -07004433 if (descriptor == nullptr) {
David Sehr709b0702016-10-13 09:12:37 -07004434 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of "
4435 << res_method->PrettyMethod()
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004436 << " missing signature component";
Ian Rogers7b078e82014-09-10 14:44:24 -07004437 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004438 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00004439 const RegType& reg_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004440 uint32_t get_reg = is_range ? inst->VRegC_3rc() + actual_args : arg[actual_args];
Ian Rogers7b078e82014-09-10 14:44:24 -07004441 if (!work_line_->VerifyRegisterType(this, get_reg, reg_type)) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004442 return res_method;
4443 }
4444 actual_args = reg_type.IsLongOrDoubleTypes() ? actual_args + 2 : actual_args + 1;
4445 }
4446 if (actual_args != expected_args) {
David Sehr709b0702016-10-13 09:12:37 -07004447 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of "
4448 << res_method->PrettyMethod() << " expected "
4449 << expected_args << " arguments, found " << actual_args;
Ian Rogers7b078e82014-09-10 14:44:24 -07004450 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004451 } else {
4452 return res_method;
4453 }
4454}
4455
Ian Rogers62342ec2013-06-11 10:26:37 -07004456void MethodVerifier::VerifyNewArray(const Instruction* inst, bool is_filled, bool is_range) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08004457 dex::TypeIndex type_idx;
Sebastien Hertz5243e912013-05-21 10:55:07 +02004458 if (!is_filled) {
4459 DCHECK_EQ(inst->Opcode(), Instruction::NEW_ARRAY);
Andreas Gampea5b09a62016-11-17 15:21:22 -08004460 type_idx = dex::TypeIndex(inst->VRegC_22c());
Sebastien Hertz5243e912013-05-21 10:55:07 +02004461 } else if (!is_range) {
4462 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY);
Andreas Gampea5b09a62016-11-17 15:21:22 -08004463 type_idx = dex::TypeIndex(inst->VRegB_35c());
Sebastien Hertz5243e912013-05-21 10:55:07 +02004464 } else {
4465 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY_RANGE);
Andreas Gampea5b09a62016-11-17 15:21:22 -08004466 type_idx = dex::TypeIndex(inst->VRegB_3rc());
Sebastien Hertz5243e912013-05-21 10:55:07 +02004467 }
Andreas Gampe98be1a92017-08-28 08:25:45 -07004468 const RegType& res_type = ResolveClass<CheckAccess::kYes>(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004469 if (res_type.IsConflict()) { // bad class
4470 DCHECK_NE(failures_.size(), 0U);
Ian Rogers0c4a5062012-02-03 15:18:59 -08004471 } else {
4472 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
4473 if (!res_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07004474 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "new-array on non-array class " << res_type;
Ian Rogers0c4a5062012-02-03 15:18:59 -08004475 } else if (!is_filled) {
4476 /* make sure "size" register is valid type */
Ian Rogers7b078e82014-09-10 14:44:24 -07004477 work_line_->VerifyRegisterType(this, inst->VRegB_22c(), reg_types_.Integer());
Ian Rogers0c4a5062012-02-03 15:18:59 -08004478 /* set register type to array class */
Ian Rogersd8f69b02014-09-10 21:43:52 +00004479 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
Andreas Gampead238ce2015-08-24 21:13:08 -07004480 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_22c(), precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08004481 } else {
Andreas Gampebb18a032016-03-22 20:34:25 -07004482 DCHECK(!res_type.IsUnresolvedMergedReference());
Ian Rogers0c4a5062012-02-03 15:18:59 -08004483 // Verify each register. If "arg_count" is bad, VerifyRegisterType() will run off the end of
4484 // the list and fail. It's legal, if silly, for arg_count to be zero.
Ian Rogersd8f69b02014-09-10 21:43:52 +00004485 const RegType& expected_type = reg_types_.GetComponentType(res_type, GetClassLoader());
Sebastien Hertz5243e912013-05-21 10:55:07 +02004486 uint32_t arg_count = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
4487 uint32_t arg[5];
4488 if (!is_range) {
Ian Rogers29a26482014-05-02 15:27:29 -07004489 inst->GetVarArgs(arg);
Sebastien Hertz5243e912013-05-21 10:55:07 +02004490 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08004491 for (size_t ui = 0; ui < arg_count; ui++) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02004492 uint32_t get_reg = is_range ? inst->VRegC_3rc() + ui : arg[ui];
Ian Rogers7b078e82014-09-10 14:44:24 -07004493 if (!work_line_->VerifyRegisterType(this, get_reg, expected_type)) {
4494 work_line_->SetResultRegisterType(this, reg_types_.Conflict());
Ian Rogers0c4a5062012-02-03 15:18:59 -08004495 return;
4496 }
4497 }
4498 // filled-array result goes into "result" register
Ian Rogersd8f69b02014-09-10 21:43:52 +00004499 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
Ian Rogers7b078e82014-09-10 14:44:24 -07004500 work_line_->SetResultRegisterType(this, precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08004501 }
4502 }
4503}
4504
Sebastien Hertz5243e912013-05-21 10:55:07 +02004505void MethodVerifier::VerifyAGet(const Instruction* inst,
Ian Rogersd8f69b02014-09-10 21:43:52 +00004506 const RegType& insn_type, bool is_primitive) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004507 const RegType& index_type = work_line_->GetRegisterType(this, inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07004508 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07004509 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07004510 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004511 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegB_23x());
Andreas Gampeeed3a5d2017-11-29 14:58:34 -08004512 if (array_type.IsZeroOrNull()) {
Ian Rogers89310de2012-02-01 13:47:30 -08004513 // Null array class; this code path will fail at runtime. Infer a merge-able type from the
Andreas Gampe52f205a2017-12-01 12:16:07 -08004514 // instruction type.
4515 if (!is_primitive) {
4516 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), reg_types_.Null());
4517 } else if (insn_type.IsInteger()) {
4518 // Pick a non-zero constant (to distinguish with null) that can fit in any primitive.
4519 // We cannot use 'insn_type' as it could be a float array or an int array.
4520 work_line_->SetRegisterType<LockOp::kClear>(
4521 this, inst->VRegA_23x(), DetermineCat1Constant(1, need_precise_constants_));
4522 } else if (insn_type.IsCategory1Types()) {
4523 // Category 1
4524 // The 'insn_type' is exactly the type we need.
4525 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), insn_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07004526 } else {
Ian Rogers89310de2012-02-01 13:47:30 -08004527 // Category 2
Ian Rogers7b078e82014-09-10 14:44:24 -07004528 work_line_->SetRegisterTypeWide(this, inst->VRegA_23x(),
4529 reg_types_.FromCat2ConstLo(0, false),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004530 reg_types_.FromCat2ConstHi(0, false));
Ian Rogers89310de2012-02-01 13:47:30 -08004531 }
jeffhaofc3144e2012-02-01 17:21:15 -08004532 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07004533 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aget";
Andreas Gampe8d8fc482016-03-25 16:24:20 -07004534 } else if (array_type.IsUnresolvedMergedReference()) {
Andreas Gampebb18a032016-03-22 20:34:25 -07004535 // Unresolved array types must be reference array types.
4536 if (is_primitive) {
4537 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type
4538 << " source for category 1 aget";
4539 } else {
4540 Fail(VERIFY_ERROR_NO_CLASS) << "cannot verify aget for " << array_type
4541 << " because of missing class";
Andreas Gampe8d8fc482016-03-25 16:24:20 -07004542 // Approximate with java.lang.Object[].
4543 work_line_->SetRegisterType<LockOp::kClear>(this,
4544 inst->VRegA_23x(),
4545 reg_types_.JavaLangObject(false));
Andreas Gampebb18a032016-03-22 20:34:25 -07004546 }
Ian Rogers89310de2012-02-01 13:47:30 -08004547 } else {
4548 /* verify the class */
Ian Rogersd8f69b02014-09-10 21:43:52 +00004549 const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
jeffhaofc3144e2012-02-01 17:21:15 -08004550 if (!component_type.IsReferenceTypes() && !is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07004551 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08004552 << " source for aget-object";
4553 } else if (component_type.IsNonZeroReferenceTypes() && is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07004554 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08004555 << " source for category 1 aget";
4556 } else if (is_primitive && !insn_type.Equals(component_type) &&
4557 !((insn_type.IsInteger() && component_type.IsFloat()) ||
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004558 (insn_type.IsLong() && component_type.IsDouble()))) {
4559 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array type " << array_type
4560 << " incompatible with aget of type " << insn_type;
Ian Rogers89310de2012-02-01 13:47:30 -08004561 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004562 // Use knowledge of the field type which is stronger than the type inferred from the
4563 // instruction, which can't differentiate object types and ints from floats, longs from
4564 // doubles.
4565 if (!component_type.IsLowHalf()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07004566 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), component_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004567 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004568 work_line_->SetRegisterTypeWide(this, inst->VRegA_23x(), component_type,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004569 component_type.HighHalf(&reg_types_));
4570 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004571 }
4572 }
4573 }
4574}
4575
Ian Rogersd8f69b02014-09-10 21:43:52 +00004576void MethodVerifier::VerifyPrimitivePut(const RegType& target_type, const RegType& insn_type,
Jeff Haofe1f7c82013-08-01 14:50:24 -07004577 const uint32_t vregA) {
4578 // Primitive assignability rules are weaker than regular assignability rules.
4579 bool instruction_compatible;
4580 bool value_compatible;
Ian Rogers7b078e82014-09-10 14:44:24 -07004581 const RegType& value_type = work_line_->GetRegisterType(this, vregA);
Jeff Haofe1f7c82013-08-01 14:50:24 -07004582 if (target_type.IsIntegralTypes()) {
Jeff Haoa4647482013-08-06 15:35:47 -07004583 instruction_compatible = target_type.Equals(insn_type);
Jeff Haofe1f7c82013-08-01 14:50:24 -07004584 value_compatible = value_type.IsIntegralTypes();
4585 } else if (target_type.IsFloat()) {
4586 instruction_compatible = insn_type.IsInteger(); // no put-float, so expect put-int
4587 value_compatible = value_type.IsFloatTypes();
4588 } else if (target_type.IsLong()) {
4589 instruction_compatible = insn_type.IsLong();
Andreas Gampe376fa682014-09-07 13:06:12 -07004590 // Additional register check: this is not checked statically (as part of VerifyInstructions),
4591 // as target_type depends on the resolved type of the field.
4592 if (instruction_compatible && work_line_->NumRegs() > vregA + 1) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004593 const RegType& value_type_hi = work_line_->GetRegisterType(this, vregA + 1);
Andreas Gampe376fa682014-09-07 13:06:12 -07004594 value_compatible = value_type.IsLongTypes() && value_type.CheckWidePair(value_type_hi);
4595 } else {
4596 value_compatible = false;
4597 }
Jeff Haofe1f7c82013-08-01 14:50:24 -07004598 } else if (target_type.IsDouble()) {
4599 instruction_compatible = insn_type.IsLong(); // no put-double, so expect put-long
Andreas Gampe376fa682014-09-07 13:06:12 -07004600 // Additional register check: this is not checked statically (as part of VerifyInstructions),
4601 // as target_type depends on the resolved type of the field.
4602 if (instruction_compatible && work_line_->NumRegs() > vregA + 1) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004603 const RegType& value_type_hi = work_line_->GetRegisterType(this, vregA + 1);
Andreas Gampe376fa682014-09-07 13:06:12 -07004604 value_compatible = value_type.IsDoubleTypes() && value_type.CheckWidePair(value_type_hi);
4605 } else {
4606 value_compatible = false;
4607 }
Jeff Haofe1f7c82013-08-01 14:50:24 -07004608 } else {
4609 instruction_compatible = false; // reference with primitive store
4610 value_compatible = false; // unused
4611 }
4612 if (!instruction_compatible) {
4613 // This is a global failure rather than a class change failure as the instructions and
4614 // the descriptors for the type should have been consistent within the same file at
4615 // compile time.
4616 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "put insn has type '" << insn_type
4617 << "' but expected type '" << target_type << "'";
4618 return;
4619 }
4620 if (!value_compatible) {
4621 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
4622 << " of type " << value_type << " but expected " << target_type << " for put";
4623 return;
4624 }
4625}
4626
Sebastien Hertz5243e912013-05-21 10:55:07 +02004627void MethodVerifier::VerifyAPut(const Instruction* inst,
Ian Rogersd8f69b02014-09-10 21:43:52 +00004628 const RegType& insn_type, bool is_primitive) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004629 const RegType& index_type = work_line_->GetRegisterType(this, inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07004630 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07004631 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07004632 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004633 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegB_23x());
Andreas Gampeeed3a5d2017-11-29 14:58:34 -08004634 if (array_type.IsZeroOrNull()) {
Nicolas Geoffray66389fb2015-06-19 10:35:42 +01004635 // Null array type; this code path will fail at runtime.
4636 // Still check that the given value matches the instruction's type.
Andreas Gampe4bf4c782015-08-14 14:07:43 -07004637 // Note: this is, as usual, complicated by the fact the the instruction isn't fully typed
4638 // and fits multiple register types.
4639 const RegType* modified_reg_type = &insn_type;
4640 if ((modified_reg_type == &reg_types_.Integer()) ||
4641 (modified_reg_type == &reg_types_.LongLo())) {
4642 // May be integer or float | long or double. Overwrite insn_type accordingly.
4643 const RegType& value_type = work_line_->GetRegisterType(this, inst->VRegA_23x());
4644 if (modified_reg_type == &reg_types_.Integer()) {
4645 if (&value_type == &reg_types_.Float()) {
4646 modified_reg_type = &value_type;
4647 }
4648 } else {
4649 if (&value_type == &reg_types_.DoubleLo()) {
4650 modified_reg_type = &value_type;
4651 }
4652 }
4653 }
4654 work_line_->VerifyRegisterType(this, inst->VRegA_23x(), *modified_reg_type);
jeffhaofc3144e2012-02-01 17:21:15 -08004655 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07004656 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aput";
Andreas Gampe8d8fc482016-03-25 16:24:20 -07004657 } else if (array_type.IsUnresolvedMergedReference()) {
Andreas Gampebb18a032016-03-22 20:34:25 -07004658 // Unresolved array types must be reference array types.
4659 if (is_primitive) {
4660 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "put insn has type '" << insn_type
4661 << "' but unresolved type '" << array_type << "'";
4662 } else {
4663 Fail(VERIFY_ERROR_NO_CLASS) << "cannot verify aput for " << array_type
4664 << " because of missing class";
4665 }
Ian Rogers89310de2012-02-01 13:47:30 -08004666 } else {
Ian Rogersd8f69b02014-09-10 21:43:52 +00004667 const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
Jeff Haofe1f7c82013-08-01 14:50:24 -07004668 const uint32_t vregA = inst->VRegA_23x();
Jeff Haob24b4a72013-07-31 13:47:31 -07004669 if (is_primitive) {
Jeff Haofe1f7c82013-08-01 14:50:24 -07004670 VerifyPrimitivePut(component_type, insn_type, vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07004671 } else {
Jeff Haob24b4a72013-07-31 13:47:31 -07004672 if (!component_type.IsReferenceTypes()) {
4673 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
4674 << " source for aput-object";
4675 } else {
4676 // The instruction agrees with the type of array, confirm the value to be stored does too
4677 // Note: we use the instruction type (rather than the component type) for aput-object as
4678 // incompatible classes will be caught at runtime as an array store exception
Ian Rogers7b078e82014-09-10 14:44:24 -07004679 work_line_->VerifyRegisterType(this, vregA, insn_type);
Jeff Haob24b4a72013-07-31 13:47:31 -07004680 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004681 }
4682 }
4683 }
4684}
4685
Mathieu Chartierc7853442015-03-27 14:35:38 -07004686ArtField* MethodVerifier::GetStaticField(int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08004687 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
4688 // Check access to class
Andreas Gampe98be1a92017-08-28 08:25:45 -07004689 const RegType& klass_type = ResolveClass<CheckAccess::kYes>(field_id.class_idx_);
Brian Carlstrom7934ac22013-07-26 10:54:15 -07004690 if (klass_type.IsConflict()) { // bad class
Ian Rogersad0b3a32012-04-16 14:50:24 -07004691 AppendToLastFailMessage(StringPrintf(" in attempt to access static field %d (%s) in %s",
4692 field_idx, dex_file_->GetFieldName(field_id),
4693 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers7b078e82014-09-10 14:44:24 -07004694 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08004695 }
Elliott Hughesb25c3f62012-03-26 16:35:06 -07004696 if (klass_type.IsUnresolvedTypes()) {
Andreas Gampe629be512017-08-25 17:09:32 -07004697 // Accessibility checks depend on resolved fields.
4698 DCHECK(klass_type.Equals(GetDeclaringClass()) || !failures_.empty());
4699
Ian Rogers7b078e82014-09-10 14:44:24 -07004700 return nullptr; // Can't resolve Class so no more to do here, will do checking at runtime.
Ian Rogers90040192011-12-16 08:54:29 -08004701 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07004702 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Vladimir Markoe11dd502017-12-08 14:09:45 +00004703 ArtField* field = class_linker->ResolveFieldJLS(field_idx, dex_cache_, class_loader_);
David Brazdilca3c8c32016-09-06 14:04:48 +01004704
4705 // Record result of the field resolution attempt.
4706 VerifierDeps::MaybeRecordFieldResolution(*dex_file_, field_idx, field);
4707
Ian Rogers7b078e82014-09-10 14:44:24 -07004708 if (field == nullptr) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07004709 VLOG(verifier) << "Unable to resolve static field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07004710 << dex_file_->GetFieldName(field_id) << ") in "
4711 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogers7b078e82014-09-10 14:44:24 -07004712 DCHECK(self_->IsExceptionPending());
4713 self_->ClearException();
4714 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004715 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
4716 field->GetAccessFlags())) {
David Sehr709b0702016-10-13 09:12:37 -07004717 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access static field " << field->PrettyField()
Ian Rogersad0b3a32012-04-16 14:50:24 -07004718 << " from " << GetDeclaringClass();
Ian Rogers7b078e82014-09-10 14:44:24 -07004719 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07004720 } else if (!field->IsStatic()) {
David Sehr709b0702016-10-13 09:12:37 -07004721 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << field->PrettyField() << " to be static";
Ian Rogers7b078e82014-09-10 14:44:24 -07004722 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07004723 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07004724 return field;
Ian Rogersd81871c2011-10-03 13:57:23 -07004725}
4726
Mathieu Chartierc7853442015-03-27 14:35:38 -07004727ArtField* MethodVerifier::GetInstanceField(const RegType& obj_type, int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08004728 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
Aart Bik31883642016-06-06 15:02:44 -07004729 // Check access to class.
Andreas Gampe98be1a92017-08-28 08:25:45 -07004730 const RegType& klass_type = ResolveClass<CheckAccess::kYes>(field_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004731 if (klass_type.IsConflict()) {
4732 AppendToLastFailMessage(StringPrintf(" in attempt to access instance field %d (%s) in %s",
4733 field_idx, dex_file_->GetFieldName(field_id),
4734 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers7b078e82014-09-10 14:44:24 -07004735 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08004736 }
jeffhao8cd6dda2012-02-22 10:15:34 -08004737 if (klass_type.IsUnresolvedTypes()) {
Andreas Gampe629be512017-08-25 17:09:32 -07004738 // Accessibility checks depend on resolved fields.
4739 DCHECK(klass_type.Equals(GetDeclaringClass()) || !failures_.empty());
4740
Ian Rogers7b078e82014-09-10 14:44:24 -07004741 return nullptr; // Can't resolve Class so no more to do here
Ian Rogers90040192011-12-16 08:54:29 -08004742 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07004743 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Vladimir Markoe11dd502017-12-08 14:09:45 +00004744 ArtField* field = class_linker->ResolveFieldJLS(field_idx, dex_cache_, class_loader_);
David Brazdilca3c8c32016-09-06 14:04:48 +01004745
4746 // Record result of the field resolution attempt.
4747 VerifierDeps::MaybeRecordFieldResolution(*dex_file_, field_idx, field);
4748
Ian Rogers7b078e82014-09-10 14:44:24 -07004749 if (field == nullptr) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07004750 VLOG(verifier) << "Unable to resolve instance field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07004751 << dex_file_->GetFieldName(field_id) << ") in "
4752 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogers7b078e82014-09-10 14:44:24 -07004753 DCHECK(self_->IsExceptionPending());
4754 self_->ClearException();
4755 return nullptr;
Andreas Gampeeed3a5d2017-11-29 14:58:34 -08004756 } else if (obj_type.IsZeroOrNull()) {
Aart Bik31883642016-06-06 15:02:44 -07004757 // Cannot infer and check type, however, access will cause null pointer exception.
4758 // Fall through into a few last soft failure checks below.
Stephen Kyle695c5982014-08-22 15:03:07 +01004759 } else if (!obj_type.IsReferenceTypes()) {
Aart Bik31883642016-06-06 15:02:44 -07004760 // Trying to read a field from something that isn't a reference.
Stephen Kyle695c5982014-08-22 15:03:07 +01004761 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance field access on object that has "
4762 << "non-reference type " << obj_type;
Ian Rogers7b078e82014-09-10 14:44:24 -07004763 return nullptr;
Ian Rogerse1758fe2012-04-19 11:31:15 -07004764 } else {
David Brazdil0d638bb2016-07-27 15:29:25 +01004765 std::string temp;
Mathieu Chartier3398c782016-09-30 10:27:43 -07004766 ObjPtr<mirror::Class> klass = field->GetDeclaringClass();
Ian Rogersd8f69b02014-09-10 21:43:52 +00004767 const RegType& field_klass =
Mathieu Chartier3398c782016-09-30 10:27:43 -07004768 FromClass(klass->GetDescriptor(&temp),
Mathieu Chartier1cc62e42016-10-03 18:01:28 -07004769 klass.Ptr(),
Mathieu Chartier3398c782016-09-30 10:27:43 -07004770 klass->CannotBeAssignedFromOtherTypes());
David Brazdil68b5c0b2016-01-19 14:25:29 +00004771 if (obj_type.IsUninitializedTypes()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07004772 // Field accesses through uninitialized references are only allowable for constructors where
David Brazdil68b5c0b2016-01-19 14:25:29 +00004773 // the field is declared in this class.
4774 // Note: this IsConstructor check is technically redundant, as UninitializedThis should only
4775 // appear in constructors.
4776 if (!obj_type.IsUninitializedThisReference() ||
4777 !IsConstructor() ||
4778 !field_klass.Equals(GetDeclaringClass())) {
David Sehr709b0702016-10-13 09:12:37 -07004779 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "cannot access instance field " << field->PrettyField()
David Brazdil68b5c0b2016-01-19 14:25:29 +00004780 << " of a not fully initialized object within the context"
David Sehr709b0702016-10-13 09:12:37 -07004781 << " of " << dex_file_->PrettyMethod(dex_method_idx_);
David Brazdil68b5c0b2016-01-19 14:25:29 +00004782 return nullptr;
4783 }
David Brazdilca3c8c32016-09-06 14:04:48 +01004784 } else if (!field_klass.IsAssignableFrom(obj_type, this)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07004785 // Trying to access C1.field1 using reference of type C2, which is neither C1 or a sub-class
4786 // of C1. For resolution to occur the declared class of the field must be compatible with
4787 // obj_type, we've discovered this wasn't so, so report the field didn't exist.
Andreas Gampe66596242016-04-14 10:55:04 -07004788 VerifyError type;
4789 bool is_aot = Runtime::Current()->IsAotCompiler();
4790 if (is_aot && (field_klass.IsUnresolvedTypes() || obj_type.IsUnresolvedTypes())) {
4791 // Compiler & unresolved types involved, retry at runtime.
4792 type = VerifyError::VERIFY_ERROR_NO_CLASS;
4793 } else {
Andreas Gampe8f4ade02016-04-15 10:09:16 -07004794 // Classes known (resolved; and thus assignability check is precise), or we are at runtime
4795 // and still missing classes. This is a hard failure.
Andreas Gampe66596242016-04-14 10:55:04 -07004796 type = VerifyError::VERIFY_ERROR_BAD_CLASS_HARD;
4797 }
David Sehr709b0702016-10-13 09:12:37 -07004798 Fail(type) << "cannot access instance field " << field->PrettyField()
Andreas Gampe66596242016-04-14 10:55:04 -07004799 << " from object of type " << obj_type;
Ian Rogers7b078e82014-09-10 14:44:24 -07004800 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004801 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004802 }
Aart Bik31883642016-06-06 15:02:44 -07004803
4804 // Few last soft failure checks.
4805 if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
4806 field->GetAccessFlags())) {
David Sehr709b0702016-10-13 09:12:37 -07004807 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access instance field " << field->PrettyField()
Aart Bik31883642016-06-06 15:02:44 -07004808 << " from " << GetDeclaringClass();
4809 return nullptr;
4810 } else if (field->IsStatic()) {
David Sehr709b0702016-10-13 09:12:37 -07004811 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << field->PrettyField()
Aart Bik31883642016-06-06 15:02:44 -07004812 << " to not be static";
4813 return nullptr;
4814 }
4815
4816 return field;
Ian Rogersd81871c2011-10-03 13:57:23 -07004817}
4818
Andreas Gampe896df402014-10-20 22:25:29 -07004819template <MethodVerifier::FieldAccessType kAccType>
4820void MethodVerifier::VerifyISFieldAccess(const Instruction* inst, const RegType& insn_type,
4821 bool is_primitive, bool is_static) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02004822 uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
Mathieu Chartierc7853442015-03-27 14:35:38 -07004823 ArtField* field;
Ian Rogersb94a27b2011-10-26 00:33:41 -07004824 if (is_static) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07004825 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07004826 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004827 const RegType& object_type = work_line_->GetRegisterType(this, inst->VRegB_22c());
David Brazdil68b5c0b2016-01-19 14:25:29 +00004828
4829 // One is not allowed to access fields on uninitialized references, except to write to
4830 // fields in the constructor (before calling another constructor).
4831 // GetInstanceField does an assignability check which will fail for uninitialized types.
4832 // We thus modify the type if the uninitialized reference is a "this" reference (this also
4833 // checks at the same time that we're verifying a constructor).
4834 bool should_adjust = (kAccType == FieldAccessType::kAccPut) &&
4835 object_type.IsUninitializedThisReference();
4836 const RegType& adjusted_type = should_adjust
4837 ? GetRegTypeCache()->FromUninitialized(object_type)
4838 : object_type;
4839 field = GetInstanceField(adjusted_type, field_idx);
Andreas Gampe896df402014-10-20 22:25:29 -07004840 if (UNLIKELY(have_pending_hard_failure_)) {
4841 return;
4842 }
Alex Light4a2c8fc2016-02-12 11:01:54 -08004843 if (should_adjust) {
4844 if (field == nullptr) {
4845 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "Might be accessing a superclass instance field prior "
4846 << "to the superclass being initialized in "
David Sehr709b0702016-10-13 09:12:37 -07004847 << dex_file_->PrettyMethod(dex_method_idx_);
Alex Light4a2c8fc2016-02-12 11:01:54 -08004848 } else if (field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
4849 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "cannot access superclass instance field "
David Sehr709b0702016-10-13 09:12:37 -07004850 << field->PrettyField() << " of a not fully initialized "
Alex Light4a2c8fc2016-02-12 11:01:54 -08004851 << "object within the context of "
David Sehr709b0702016-10-13 09:12:37 -07004852 << dex_file_->PrettyMethod(dex_method_idx_);
Alex Light4a2c8fc2016-02-12 11:01:54 -08004853 return;
4854 }
4855 }
Ian Rogersb94a27b2011-10-26 00:33:41 -07004856 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00004857 const RegType* field_type = nullptr;
Ian Rogers7b078e82014-09-10 14:44:24 -07004858 if (field != nullptr) {
Andreas Gampe896df402014-10-20 22:25:29 -07004859 if (kAccType == FieldAccessType::kAccPut) {
4860 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
David Sehr709b0702016-10-13 09:12:37 -07004861 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << field->PrettyField()
Andreas Gampe896df402014-10-20 22:25:29 -07004862 << " from other class " << GetDeclaringClass();
Aart Bikc2bc2652016-05-23 14:58:49 -07004863 // Keep hunting for possible hard fails.
Andreas Gampe896df402014-10-20 22:25:29 -07004864 }
4865 }
4866
Mathieu Chartier3398c782016-09-30 10:27:43 -07004867 ObjPtr<mirror::Class> field_type_class =
Vladimir Marko208f6702017-12-08 12:00:50 +00004868 can_load_classes_ ? field->ResolveType() : field->LookupResolvedType();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004869 if (field_type_class != nullptr) {
Mathieu Chartier3398c782016-09-30 10:27:43 -07004870 field_type = &FromClass(field->GetTypeDescriptor(),
Mathieu Chartier1cc62e42016-10-03 18:01:28 -07004871 field_type_class.Ptr(),
Andreas Gampef23f33d2015-06-23 14:18:17 -07004872 field_type_class->CannotBeAssignedFromOtherTypes());
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08004873 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004874 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
4875 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004876 }
Andreas Gampe7da4c402017-08-25 11:30:48 -07004877 } else {
4878 // If we don't have the field (it seems we failed resolution) and this is a PUT, we need to
4879 // redo verification at runtime as the field may be final, unless the field id shows it's in
4880 // the same class.
4881 //
4882 // For simplicity, it is OK to not distinguish compile-time vs runtime, and post this an
4883 // ACCESS_FIELD failure at runtime. This has the same effect as NO_FIELD - punting the class
4884 // to the access-checks interpreter.
4885 //
4886 // Note: see b/34966607. This and above may be changed in the future.
4887 if (kAccType == FieldAccessType::kAccPut) {
4888 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
4889 const char* field_class_descriptor = dex_file_->GetFieldDeclaringClassDescriptor(field_id);
4890 const RegType* field_class_type = &reg_types_.FromDescriptor(GetClassLoader(),
4891 field_class_descriptor,
4892 false);
4893 if (!field_class_type->Equals(GetDeclaringClass())) {
4894 Fail(VERIFY_ERROR_ACCESS_FIELD) << "could not check field put for final field modify of "
4895 << field_class_descriptor
4896 << "."
4897 << dex_file_->GetFieldName(field_id)
4898 << " from other class "
4899 << GetDeclaringClass();
4900 }
4901 }
Ian Rogers0d604842012-04-16 14:50:24 -07004902 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004903 if (field_type == nullptr) {
4904 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
4905 const char* descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
Mathieu Chartierbf99f772014-08-23 16:37:27 -07004906 field_type = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004907 }
Sebastien Hertz757b3042014-03-28 14:34:28 +01004908 DCHECK(field_type != nullptr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02004909 const uint32_t vregA = (is_static) ? inst->VRegA_21c() : inst->VRegA_22c();
Andreas Gampe896df402014-10-20 22:25:29 -07004910 static_assert(kAccType == FieldAccessType::kAccPut || kAccType == FieldAccessType::kAccGet,
4911 "Unexpected third access type");
4912 if (kAccType == FieldAccessType::kAccPut) {
4913 // sput or iput.
4914 if (is_primitive) {
4915 VerifyPrimitivePut(*field_type, insn_type, vregA);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004916 } else {
David Brazdilca3c8c32016-09-06 14:04:48 +01004917 if (!insn_type.IsAssignableFrom(*field_type, this)) {
Vladimir Marko414000e2015-06-23 17:45:21 +01004918 // If the field type is not a reference, this is a global failure rather than
4919 // a class change failure as the instructions and the descriptors for the type
4920 // should have been consistent within the same file at compile time.
4921 VerifyError error = field_type->IsReferenceTypes() ? VERIFY_ERROR_BAD_CLASS_SOFT
4922 : VERIFY_ERROR_BAD_CLASS_HARD;
David Sehr709b0702016-10-13 09:12:37 -07004923 Fail(error) << "expected field " << ArtField::PrettyField(field)
Vladimir Marko414000e2015-06-23 17:45:21 +01004924 << " to be compatible with type '" << insn_type
4925 << "' but found type '" << *field_type
4926 << "' in put-object";
Andreas Gampe896df402014-10-20 22:25:29 -07004927 return;
4928 }
4929 work_line_->VerifyRegisterType(this, vregA, *field_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004930 }
Andreas Gampe896df402014-10-20 22:25:29 -07004931 } else if (kAccType == FieldAccessType::kAccGet) {
4932 // sget or iget.
4933 if (is_primitive) {
4934 if (field_type->Equals(insn_type) ||
4935 (field_type->IsFloat() && insn_type.IsInteger()) ||
4936 (field_type->IsDouble() && insn_type.IsLong())) {
4937 // expected that read is of the correct primitive type or that int reads are reading
4938 // floats or long reads are reading doubles
4939 } else {
4940 // This is a global failure rather than a class change failure as the instructions and
4941 // the descriptors for the type should have been consistent within the same file at
4942 // compile time
David Sehr709b0702016-10-13 09:12:37 -07004943 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << ArtField::PrettyField(field)
Andreas Gampe896df402014-10-20 22:25:29 -07004944 << " to be of type '" << insn_type
4945 << "' but found type '" << *field_type << "' in get";
4946 return;
4947 }
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08004948 } else {
David Brazdilca3c8c32016-09-06 14:04:48 +01004949 if (!insn_type.IsAssignableFrom(*field_type, this)) {
Vladimir Marko414000e2015-06-23 17:45:21 +01004950 // If the field type is not a reference, this is a global failure rather than
4951 // a class change failure as the instructions and the descriptors for the type
4952 // should have been consistent within the same file at compile time.
4953 VerifyError error = field_type->IsReferenceTypes() ? VERIFY_ERROR_BAD_CLASS_SOFT
4954 : VERIFY_ERROR_BAD_CLASS_HARD;
David Sehr709b0702016-10-13 09:12:37 -07004955 Fail(error) << "expected field " << ArtField::PrettyField(field)
Vladimir Marko414000e2015-06-23 17:45:21 +01004956 << " to be compatible with type '" << insn_type
4957 << "' but found type '" << *field_type
4958 << "' in get-object";
Andreas Gampe890da292015-07-06 17:20:18 -07004959 if (error != VERIFY_ERROR_BAD_CLASS_HARD) {
Andreas Gampead238ce2015-08-24 21:13:08 -07004960 work_line_->SetRegisterType<LockOp::kClear>(this, vregA, reg_types_.Conflict());
Andreas Gampe890da292015-07-06 17:20:18 -07004961 }
Andreas Gampe896df402014-10-20 22:25:29 -07004962 return;
4963 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004964 }
Andreas Gampe896df402014-10-20 22:25:29 -07004965 if (!field_type->IsLowHalf()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07004966 work_line_->SetRegisterType<LockOp::kClear>(this, vregA, *field_type);
Andreas Gampe896df402014-10-20 22:25:29 -07004967 } else {
4968 work_line_->SetRegisterTypeWide(this, vregA, *field_type, field_type->HighHalf(&reg_types_));
4969 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07004970 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004971 LOG(FATAL) << "Unexpected case.";
Ian Rogersd81871c2011-10-03 13:57:23 -07004972 }
4973}
4974
Nicolas Geoffrayb041a402017-11-13 15:16:22 +00004975ArtField* MethodVerifier::GetQuickAccessedField() {
4976 DCHECK(method_being_verified_ != nullptr);
4977 uint16_t field_idx = method_being_verified_->GetIndexFromQuickening(work_insn_idx_);
4978 CHECK_NE(field_idx, DexFile::kDexNoIndex16);
4979 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
4980 ArtField* field = class_linker->ResolveFieldJLS(field_idx, dex_cache_, class_loader_);
4981 if (field == nullptr) {
4982 DCHECK(self_->IsExceptionPending());
4983 self_->ClearException();
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004984 }
Nicolas Geoffrayb041a402017-11-13 15:16:22 +00004985 return field;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004986}
4987
Andreas Gampe896df402014-10-20 22:25:29 -07004988template <MethodVerifier::FieldAccessType kAccType>
4989void MethodVerifier::VerifyQuickFieldAccess(const Instruction* inst, const RegType& insn_type,
4990 bool is_primitive) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07004991 DCHECK(Runtime::Current()->IsStarted() || verify_to_dump_);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004992
Nicolas Geoffrayb041a402017-11-13 15:16:22 +00004993 ArtField* field = GetQuickAccessedField();
Ian Rogers7b078e82014-09-10 14:44:24 -07004994 if (field == nullptr) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004995 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field from " << inst->Name();
4996 return;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004997 }
Andreas Gampe896df402014-10-20 22:25:29 -07004998
4999 // For an IPUT_QUICK, we now test for final flag of the field.
5000 if (kAccType == FieldAccessType::kAccPut) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02005001 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
David Sehr709b0702016-10-13 09:12:37 -07005002 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << field->PrettyField()
Sebastien Hertzc15853b2013-06-25 17:36:27 +02005003 << " from other class " << GetDeclaringClass();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02005004 return;
5005 }
5006 }
Andreas Gampe896df402014-10-20 22:25:29 -07005007
5008 // Get the field type.
5009 const RegType* field_type;
5010 {
Vladimir Marko4098a7a2017-11-06 16:00:51 +00005011 ObjPtr<mirror::Class> field_type_class =
Vladimir Marko208f6702017-12-08 12:00:50 +00005012 can_load_classes_ ? field->ResolveType() : field->LookupResolvedType();
Andreas Gampe896df402014-10-20 22:25:29 -07005013
5014 if (field_type_class != nullptr) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07005015 field_type = &FromClass(field->GetTypeDescriptor(),
Mathieu Chartier1cc62e42016-10-03 18:01:28 -07005016 field_type_class.Ptr(),
Andreas Gampef23f33d2015-06-23 14:18:17 -07005017 field_type_class->CannotBeAssignedFromOtherTypes());
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02005018 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07005019 Thread* self = Thread::Current();
5020 DCHECK(!can_load_classes_ || self->IsExceptionPending());
5021 self->ClearException();
5022 field_type = &reg_types_.FromDescriptor(field->GetDeclaringClass()->GetClassLoader(),
Mathieu Chartierde40d472015-10-15 17:47:48 -07005023 field->GetTypeDescriptor(),
5024 false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02005025 }
Andreas Gampe896df402014-10-20 22:25:29 -07005026 if (field_type == nullptr) {
5027 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field type from " << inst->Name();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02005028 return;
5029 }
Andreas Gampe896df402014-10-20 22:25:29 -07005030 }
5031
5032 const uint32_t vregA = inst->VRegA_22c();
5033 static_assert(kAccType == FieldAccessType::kAccPut || kAccType == FieldAccessType::kAccGet,
5034 "Unexpected third access type");
5035 if (kAccType == FieldAccessType::kAccPut) {
5036 if (is_primitive) {
5037 // Primitive field assignability rules are weaker than regular assignability rules
5038 bool instruction_compatible;
5039 bool value_compatible;
5040 const RegType& value_type = work_line_->GetRegisterType(this, vregA);
5041 if (field_type->IsIntegralTypes()) {
5042 instruction_compatible = insn_type.IsIntegralTypes();
5043 value_compatible = value_type.IsIntegralTypes();
5044 } else if (field_type->IsFloat()) {
5045 instruction_compatible = insn_type.IsInteger(); // no [is]put-float, so expect [is]put-int
5046 value_compatible = value_type.IsFloatTypes();
5047 } else if (field_type->IsLong()) {
5048 instruction_compatible = insn_type.IsLong();
5049 value_compatible = value_type.IsLongTypes();
5050 } else if (field_type->IsDouble()) {
5051 instruction_compatible = insn_type.IsLong(); // no [is]put-double, so expect [is]put-long
5052 value_compatible = value_type.IsDoubleTypes();
5053 } else {
5054 instruction_compatible = false; // reference field with primitive store
5055 value_compatible = false; // unused
5056 }
5057 if (!instruction_compatible) {
5058 // This is a global failure rather than a class change failure as the instructions and
5059 // the descriptors for the type should have been consistent within the same file at
5060 // compile time
David Sehr709b0702016-10-13 09:12:37 -07005061 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << ArtField::PrettyField(field)
Andreas Gampe896df402014-10-20 22:25:29 -07005062 << " to be of type '" << insn_type
5063 << "' but found type '" << *field_type
5064 << "' in put";
5065 return;
5066 }
5067 if (!value_compatible) {
5068 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
5069 << " of type " << value_type
5070 << " but expected " << *field_type
David Sehr709b0702016-10-13 09:12:37 -07005071 << " for store to " << ArtField::PrettyField(field) << " in put";
Andreas Gampe896df402014-10-20 22:25:29 -07005072 return;
5073 }
5074 } else {
David Brazdilca3c8c32016-09-06 14:04:48 +01005075 if (!insn_type.IsAssignableFrom(*field_type, this)) {
David Sehr709b0702016-10-13 09:12:37 -07005076 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << ArtField::PrettyField(field)
Andreas Gampe896df402014-10-20 22:25:29 -07005077 << " to be compatible with type '" << insn_type
5078 << "' but found type '" << *field_type
5079 << "' in put-object";
5080 return;
5081 }
5082 work_line_->VerifyRegisterType(this, vregA, *field_type);
5083 }
5084 } else if (kAccType == FieldAccessType::kAccGet) {
5085 if (is_primitive) {
5086 if (field_type->Equals(insn_type) ||
5087 (field_type->IsFloat() && insn_type.IsIntegralTypes()) ||
5088 (field_type->IsDouble() && insn_type.IsLongTypes())) {
5089 // expected that read is of the correct primitive type or that int reads are reading
5090 // floats or long reads are reading doubles
5091 } else {
5092 // This is a global failure rather than a class change failure as the instructions and
5093 // the descriptors for the type should have been consistent within the same file at
5094 // compile time
David Sehr709b0702016-10-13 09:12:37 -07005095 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << ArtField::PrettyField(field)
Andreas Gampe896df402014-10-20 22:25:29 -07005096 << " to be of type '" << insn_type
5097 << "' but found type '" << *field_type << "' in Get";
5098 return;
5099 }
5100 } else {
David Brazdilca3c8c32016-09-06 14:04:48 +01005101 if (!insn_type.IsAssignableFrom(*field_type, this)) {
David Sehr709b0702016-10-13 09:12:37 -07005102 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << ArtField::PrettyField(field)
Andreas Gampe896df402014-10-20 22:25:29 -07005103 << " to be compatible with type '" << insn_type
5104 << "' but found type '" << *field_type
5105 << "' in get-object";
Andreas Gampead238ce2015-08-24 21:13:08 -07005106 work_line_->SetRegisterType<LockOp::kClear>(this, vregA, reg_types_.Conflict());
Andreas Gampe896df402014-10-20 22:25:29 -07005107 return;
5108 }
5109 }
5110 if (!field_type->IsLowHalf()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07005111 work_line_->SetRegisterType<LockOp::kClear>(this, vregA, *field_type);
Andreas Gampe896df402014-10-20 22:25:29 -07005112 } else {
5113 work_line_->SetRegisterTypeWide(this, vregA, *field_type, field_type->HighHalf(&reg_types_));
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02005114 }
5115 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07005116 LOG(FATAL) << "Unexpected case.";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02005117 }
5118}
5119
Ian Rogers776ac1f2012-04-13 23:36:36 -07005120bool MethodVerifier::CheckNotMoveException(const uint16_t* insns, int insn_idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07005121 if ((insns[insn_idx] & 0xff) == Instruction::MOVE_EXCEPTION) {
jeffhaod5347e02012-03-22 17:25:05 -07005122 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-exception";
Ian Rogersd81871c2011-10-03 13:57:23 -07005123 return false;
5124 }
5125 return true;
5126}
5127
Stephen Kyle9bc61992014-09-22 13:53:15 +01005128bool MethodVerifier::CheckNotMoveResult(const uint16_t* insns, int insn_idx) {
5129 if (((insns[insn_idx] & 0xff) >= Instruction::MOVE_RESULT) &&
5130 ((insns[insn_idx] & 0xff) <= Instruction::MOVE_RESULT_OBJECT)) {
5131 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-result*";
5132 return false;
5133 }
5134 return true;
5135}
5136
5137bool MethodVerifier::CheckNotMoveExceptionOrMoveResult(const uint16_t* insns, int insn_idx) {
5138 return (CheckNotMoveException(insns, insn_idx) && CheckNotMoveResult(insns, insn_idx));
5139}
5140
Ian Rogersebbdd872014-07-07 23:53:08 -07005141bool MethodVerifier::UpdateRegisters(uint32_t next_insn, RegisterLine* merge_line,
5142 bool update_merge_line) {
Ian Rogersd81871c2011-10-03 13:57:23 -07005143 bool changed = true;
5144 RegisterLine* target_line = reg_table_.GetLine(next_insn);
Mathieu Chartierde40d472015-10-15 17:47:48 -07005145 if (!GetInstructionFlags(next_insn).IsVisitedOrChanged()) {
jeffhaobdb76512011-09-07 11:43:16 -07005146 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07005147 * We haven't processed this instruction before, and we haven't touched the registers here, so
5148 * there's nothing to "merge". Copy the registers over and mark it as changed. (This is the
5149 * only way a register can transition out of "unknown", so this is not just an optimization.)
jeffhaobdb76512011-09-07 11:43:16 -07005150 */
Andreas Gampea727e372015-08-25 09:22:37 -07005151 target_line->CopyFromLine(merge_line);
Mathieu Chartierde40d472015-10-15 17:47:48 -07005152 if (GetInstructionFlags(next_insn).IsReturn()) {
Jeff Haob24b4a72013-07-31 13:47:31 -07005153 // Verify that the monitor stack is empty on return.
Andreas Gampea727e372015-08-25 09:22:37 -07005154 merge_line->VerifyMonitorStackEmpty(this);
5155
Ian Rogersb8c78592013-07-25 23:52:52 +00005156 // For returns we only care about the operand to the return, all other registers are dead.
5157 // Initialize them as conflicts so they don't add to GC and deoptimization information.
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08005158 const Instruction* ret_inst = &code_item_accessor_.InstructionAt(next_insn);
Andreas Gampea727e372015-08-25 09:22:37 -07005159 AdjustReturnLine(this, ret_inst, target_line);
Aart Bik31883642016-06-06 15:02:44 -07005160 // Directly bail if a hard failure was found.
Aart Bikb0526322016-06-01 14:06:00 -07005161 if (have_pending_hard_failure_) {
5162 return false;
5163 }
Ian Rogersb8c78592013-07-25 23:52:52 +00005164 }
jeffhaobdb76512011-09-07 11:43:16 -07005165 } else {
Mathieu Chartier361e04a2016-02-16 14:06:35 -08005166 RegisterLineArenaUniquePtr copy;
Andreas Gampe92d77202017-12-06 20:49:00 -08005167 if (UNLIKELY(VLOG_IS_ON(verifier_debug))) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07005168 copy.reset(RegisterLine::Create(target_line->NumRegs(), this));
Ian Rogers7b0c5b42012-02-16 15:29:07 -08005169 copy->CopyFromLine(target_line);
5170 }
Ian Rogers7b078e82014-09-10 14:44:24 -07005171 changed = target_line->MergeRegisters(this, merge_line);
Ian Rogersad0b3a32012-04-16 14:50:24 -07005172 if (have_pending_hard_failure_) {
Ian Rogersd81871c2011-10-03 13:57:23 -07005173 return false;
jeffhaobdb76512011-09-07 11:43:16 -07005174 }
Andreas Gampe92d77202017-12-06 20:49:00 -08005175 if (UNLIKELY(VLOG_IS_ON(verifier_debug)) && changed) {
Elliott Hughes398f64b2012-03-26 18:05:48 -07005176 LogVerifyInfo() << "Merging at [" << reinterpret_cast<void*>(work_insn_idx_) << "]"
Elliott Hughesc073b072012-05-24 19:29:17 -07005177 << " to [" << reinterpret_cast<void*>(next_insn) << "]: " << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07005178 << copy->Dump(this) << " MERGE\n"
5179 << merge_line->Dump(this) << " ==\n"
5180 << target_line->Dump(this) << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07005181 }
Ian Rogersebbdd872014-07-07 23:53:08 -07005182 if (update_merge_line && changed) {
5183 merge_line->CopyFromLine(target_line);
5184 }
jeffhaobdb76512011-09-07 11:43:16 -07005185 }
Ian Rogersd81871c2011-10-03 13:57:23 -07005186 if (changed) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07005187 GetInstructionFlags(next_insn).SetChanged();
jeffhaobdb76512011-09-07 11:43:16 -07005188 }
5189 return true;
5190}
5191
Ian Rogers7b3ddd22013-02-21 15:19:52 -08005192InstructionFlags* MethodVerifier::CurrentInsnFlags() {
Mathieu Chartierde40d472015-10-15 17:47:48 -07005193 return &GetInstructionFlags(work_insn_idx_);
Ian Rogers776ac1f2012-04-13 23:36:36 -07005194}
5195
Ian Rogersd8f69b02014-09-10 21:43:52 +00005196const RegType& MethodVerifier::GetMethodReturnType() {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07005197 if (return_type_ == nullptr) {
Nicolas Geoffrayb041a402017-11-13 15:16:22 +00005198 if (method_being_verified_ != nullptr) {
Vladimir Markob45528c2017-07-27 14:14:28 +01005199 ObjPtr<mirror::Class> return_type_class = can_load_classes_
Nicolas Geoffrayb041a402017-11-13 15:16:22 +00005200 ? method_being_verified_->ResolveReturnType()
5201 : method_being_verified_->LookupResolvedReturnType();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07005202 if (return_type_class != nullptr) {
Nicolas Geoffrayb041a402017-11-13 15:16:22 +00005203 return_type_ = &FromClass(method_being_verified_->GetReturnTypeDescriptor(),
Vladimir Markob45528c2017-07-27 14:14:28 +01005204 return_type_class.Ptr(),
Andreas Gampef23f33d2015-06-23 14:18:17 -07005205 return_type_class->CannotBeAssignedFromOtherTypes());
Ian Rogersfc0e94b2013-09-23 23:51:32 -07005206 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07005207 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
5208 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07005209 }
5210 }
5211 if (return_type_ == nullptr) {
5212 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
5213 const DexFile::ProtoId& proto_id = dex_file_->GetMethodPrototype(method_id);
Andreas Gampea5b09a62016-11-17 15:21:22 -08005214 dex::TypeIndex return_type_idx = proto_id.return_type_idx_;
Ian Rogersfc0e94b2013-09-23 23:51:32 -07005215 const char* descriptor = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(return_type_idx));
Mathieu Chartierbf99f772014-08-23 16:37:27 -07005216 return_type_ = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07005217 }
5218 }
5219 return *return_type_;
Ian Rogersad0b3a32012-04-16 14:50:24 -07005220}
5221
Ian Rogersd8f69b02014-09-10 21:43:52 +00005222const RegType& MethodVerifier::GetDeclaringClass() {
Ian Rogers7b078e82014-09-10 14:44:24 -07005223 if (declaring_class_ == nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08005224 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
Brian Carlstrom93c33962013-07-26 10:37:43 -07005225 const char* descriptor
5226 = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(method_id.class_idx_));
Nicolas Geoffrayb041a402017-11-13 15:16:22 +00005227 if (method_being_verified_ != nullptr) {
5228 mirror::Class* klass = method_being_verified_->GetDeclaringClass();
Mathieu Chartierde40d472015-10-15 17:47:48 -07005229 declaring_class_ = &FromClass(descriptor, klass, klass->CannotBeAssignedFromOtherTypes());
Ian Rogers637c65b2013-05-31 11:46:00 -07005230 } else {
Mathieu Chartierbf99f772014-08-23 16:37:27 -07005231 declaring_class_ = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers637c65b2013-05-31 11:46:00 -07005232 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07005233 }
Ian Rogers637c65b2013-05-31 11:46:00 -07005234 return *declaring_class_;
Ian Rogersad0b3a32012-04-16 14:50:24 -07005235}
5236
Ian Rogers2bcb4a42012-11-08 10:39:18 -08005237std::vector<int32_t> MethodVerifier::DescribeVRegs(uint32_t dex_pc) {
5238 RegisterLine* line = reg_table_.GetLine(dex_pc);
Sebastien Hertzaa0c00c2014-03-14 17:58:54 +01005239 DCHECK(line != nullptr) << "No register line at DEX pc " << StringPrintf("0x%x", dex_pc);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08005240 std::vector<int32_t> result;
5241 for (size_t i = 0; i < line->NumRegs(); ++i) {
Ian Rogers7b078e82014-09-10 14:44:24 -07005242 const RegType& type = line->GetRegisterType(this, i);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08005243 if (type.IsConstant()) {
5244 result.push_back(type.IsPreciseConstant() ? kConstant : kImpreciseConstant);
Ian Rogers7b078e82014-09-10 14:44:24 -07005245 const ConstantType* const_val = down_cast<const ConstantType*>(&type);
5246 result.push_back(const_val->ConstantValue());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08005247 } else if (type.IsConstantLo()) {
5248 result.push_back(type.IsPreciseConstantLo() ? kConstant : kImpreciseConstant);
Ian Rogers7b078e82014-09-10 14:44:24 -07005249 const ConstantType* const_val = down_cast<const ConstantType*>(&type);
5250 result.push_back(const_val->ConstantValueLo());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08005251 } else if (type.IsConstantHi()) {
5252 result.push_back(type.IsPreciseConstantHi() ? kConstant : kImpreciseConstant);
Ian Rogers7b078e82014-09-10 14:44:24 -07005253 const ConstantType* const_val = down_cast<const ConstantType*>(&type);
5254 result.push_back(const_val->ConstantValueHi());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08005255 } else if (type.IsIntegralTypes()) {
5256 result.push_back(kIntVReg);
5257 result.push_back(0);
5258 } else if (type.IsFloat()) {
5259 result.push_back(kFloatVReg);
5260 result.push_back(0);
5261 } else if (type.IsLong()) {
5262 result.push_back(kLongLoVReg);
5263 result.push_back(0);
5264 result.push_back(kLongHiVReg);
5265 result.push_back(0);
5266 ++i;
5267 } else if (type.IsDouble()) {
5268 result.push_back(kDoubleLoVReg);
5269 result.push_back(0);
5270 result.push_back(kDoubleHiVReg);
5271 result.push_back(0);
5272 ++i;
5273 } else if (type.IsUndefined() || type.IsConflict() || type.IsHighHalf()) {
5274 result.push_back(kUndefined);
5275 result.push_back(0);
5276 } else {
Ian Rogers7b3ddd22013-02-21 15:19:52 -08005277 CHECK(type.IsNonZeroReferenceTypes());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08005278 result.push_back(kReferenceVReg);
5279 result.push_back(0);
5280 }
5281 }
5282 return result;
5283}
5284
Ian Rogersd8f69b02014-09-10 21:43:52 +00005285const RegType& MethodVerifier::DetermineCat1Constant(int32_t value, bool precise) {
Sebastien Hertz849600b2013-12-20 10:28:08 +01005286 if (precise) {
5287 // Precise constant type.
5288 return reg_types_.FromCat1Const(value, true);
5289 } else {
5290 // Imprecise constant type.
5291 if (value < -32768) {
5292 return reg_types_.IntConstant();
5293 } else if (value < -128) {
5294 return reg_types_.ShortConstant();
5295 } else if (value < 0) {
5296 return reg_types_.ByteConstant();
5297 } else if (value == 0) {
5298 return reg_types_.Zero();
5299 } else if (value == 1) {
5300 return reg_types_.One();
5301 } else if (value < 128) {
5302 return reg_types_.PosByteConstant();
5303 } else if (value < 32768) {
5304 return reg_types_.PosShortConstant();
5305 } else if (value < 65536) {
5306 return reg_types_.CharConstant();
5307 } else {
5308 return reg_types_.IntConstant();
5309 }
5310 }
5311}
5312
Elliott Hughes0a1038b2012-06-14 16:24:17 -07005313void MethodVerifier::Init() {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08005314 art::verifier::RegTypeCache::Init();
Brian Carlstrome7d856b2012-01-11 18:10:55 -08005315}
5316
Elliott Hughes0a1038b2012-06-14 16:24:17 -07005317void MethodVerifier::Shutdown() {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08005318 verifier::RegTypeCache::ShutDown();
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08005319}
jeffhaod1224c72012-02-29 13:43:08 -08005320
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -07005321void MethodVerifier::VisitStaticRoots(RootVisitor* visitor) {
5322 RegTypeCache::VisitStaticRoots(visitor);
Mathieu Chartier7c438b12014-09-12 17:01:24 -07005323}
5324
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -07005325void MethodVerifier::VisitRoots(RootVisitor* visitor, const RootInfo& root_info) {
5326 reg_types_.VisitRoots(visitor, root_info);
Mathieu Chartierc528dba2013-11-26 12:00:11 -08005327}
5328
Andreas Gampef23f33d2015-06-23 14:18:17 -07005329const RegType& MethodVerifier::FromClass(const char* descriptor,
5330 mirror::Class* klass,
5331 bool precise) {
5332 DCHECK(klass != nullptr);
5333 if (precise && !klass->IsInstantiable() && !klass->IsPrimitive()) {
5334 Fail(VerifyError::VERIFY_ERROR_NO_CLASS) << "Could not create precise reference for "
5335 << "non-instantiable klass " << descriptor;
5336 precise = false;
5337 }
5338 return reg_types_.FromClass(descriptor, klass, precise);
5339}
5340
Ian Rogersd81871c2011-10-03 13:57:23 -07005341} // namespace verifier
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07005342} // namespace art