blob: bc29aaca6da1202c824e24cb0a95101eb40563ac [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070016
Mathieu Chartierc645f1d2014-03-06 18:11:53 -080017#include "method_verifier-inl.h"
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070018
Elliott Hughes1f359b02011-07-17 14:27:17 -070019#include <iostream>
20
Andreas Gampe46ee31b2016-12-14 10:11:49 -080021#include "android-base/stringprintf.h"
22
Mathieu Chartierc7853442015-03-27 14:35:38 -070023#include "art_field-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070024#include "art_method-inl.h"
Andreas Gampe39b378c2017-12-07 15:44:13 -080025#include "base/aborting.h"
Andreas Gampe542451c2016-07-26 09:02:02 -070026#include "base/enums.h"
David Sehr67bf42e2018-02-26 16:43:04 -080027#include "base/leb128.h"
David Sehr9c4a0152018-04-05 12:23:54 -070028#include "base/indenter.h"
Andreas Gampe57943812017-12-06 21:39:13 -080029#include "base/logging.h" // For VLOG.
Ian Rogers637c65b2013-05-31 11:46:00 -070030#include "base/mutex-inl.h"
Vladimir Marko637ee0b2015-09-04 12:47:41 +010031#include "base/stl_util.h"
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -080032#include "base/systrace.h"
Vladimir Marko80afd022015-05-19 18:08:00 +010033#include "base/time_utils.h"
David Sehrc431b9d2018-03-02 12:01:51 -080034#include "base/utils.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070035#include "class_linker.h"
Vladimir Marko2b5eaa22013-12-13 13:59:30 +000036#include "compiler_callbacks.h"
David Sehrb2ec9f52018-02-21 13:20:31 -080037#include "dex/descriptors_names.h"
David Sehr9e734c72018-01-04 17:56:19 -080038#include "dex/dex_file-inl.h"
39#include "dex/dex_file_exception_helpers.h"
40#include "dex/dex_instruction-inl.h"
41#include "dex/dex_instruction_utils.h"
Alex Lighteb7c1442015-08-31 13:17:42 -070042#include "experimental_flags.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070043#include "gc/accounting/card_table-inl.h"
Andreas Gampe6d7abbd2017-04-24 13:19:09 -070044#include "handle_scope-inl.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:
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00002768 case Instruction::IGET_BOOLEAN_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002769 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002770 break;
jeffhaobdb76512011-09-07 11:43:16 -07002771 case Instruction::IGET_BYTE:
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00002772 case Instruction::IGET_BYTE_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002773 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002774 break;
jeffhaobdb76512011-09-07 11:43:16 -07002775 case Instruction::IGET_CHAR:
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00002776 case Instruction::IGET_CHAR_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002777 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002778 break;
jeffhaobdb76512011-09-07 11:43:16 -07002779 case Instruction::IGET_SHORT:
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00002780 case Instruction::IGET_SHORT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002781 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002782 break;
2783 case Instruction::IGET:
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00002784 case Instruction::IGET_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002785 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002786 break;
2787 case Instruction::IGET_WIDE:
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00002788 case Instruction::IGET_WIDE_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002789 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002790 break;
2791 case Instruction::IGET_OBJECT:
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00002792 case Instruction::IGET_OBJECT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002793 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false,
2794 false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002795 break;
jeffhaobdb76512011-09-07 11:43:16 -07002796
Ian Rogersd81871c2011-10-03 13:57:23 -07002797 case Instruction::IPUT_BOOLEAN:
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00002798 case Instruction::IPUT_BOOLEAN_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002799 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002800 break;
2801 case Instruction::IPUT_BYTE:
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00002802 case Instruction::IPUT_BYTE_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002803 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002804 break;
2805 case Instruction::IPUT_CHAR:
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00002806 case Instruction::IPUT_CHAR_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002807 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002808 break;
2809 case Instruction::IPUT_SHORT:
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00002810 case Instruction::IPUT_SHORT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002811 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002812 break;
2813 case Instruction::IPUT:
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00002814 case Instruction::IPUT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002815 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002816 break;
2817 case Instruction::IPUT_WIDE:
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00002818 case Instruction::IPUT_WIDE_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002819 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002820 break;
jeffhaobdb76512011-09-07 11:43:16 -07002821 case Instruction::IPUT_OBJECT:
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00002822 case Instruction::IPUT_OBJECT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002823 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false,
2824 false);
jeffhaobdb76512011-09-07 11:43:16 -07002825 break;
2826
jeffhaobdb76512011-09-07 11:43:16 -07002827 case Instruction::SGET_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002828 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002829 break;
jeffhaobdb76512011-09-07 11:43:16 -07002830 case Instruction::SGET_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002831 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002832 break;
jeffhaobdb76512011-09-07 11:43:16 -07002833 case Instruction::SGET_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002834 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002835 break;
jeffhaobdb76512011-09-07 11:43:16 -07002836 case Instruction::SGET_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002837 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002838 break;
2839 case Instruction::SGET:
Andreas Gampe896df402014-10-20 22:25:29 -07002840 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002841 break;
2842 case Instruction::SGET_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002843 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002844 break;
2845 case Instruction::SGET_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002846 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false,
2847 true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002848 break;
2849
2850 case Instruction::SPUT_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002851 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002852 break;
2853 case Instruction::SPUT_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002854 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002855 break;
2856 case Instruction::SPUT_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002857 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002858 break;
2859 case Instruction::SPUT_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002860 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002861 break;
2862 case Instruction::SPUT:
Andreas Gampe896df402014-10-20 22:25:29 -07002863 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002864 break;
2865 case Instruction::SPUT_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002866 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002867 break;
2868 case Instruction::SPUT_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002869 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false,
2870 true);
jeffhaobdb76512011-09-07 11:43:16 -07002871 break;
2872
2873 case Instruction::INVOKE_VIRTUAL:
2874 case Instruction::INVOKE_VIRTUAL_RANGE:
2875 case Instruction::INVOKE_SUPER:
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00002876 case Instruction::INVOKE_SUPER_RANGE:
2877 case Instruction::INVOKE_VIRTUAL_QUICK:
2878 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002879 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE ||
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00002880 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE ||
2881 inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002882 bool is_super = (inst->Opcode() == Instruction::INVOKE_SUPER ||
2883 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
Alex Light7268d472016-01-20 15:50:01 -08002884 MethodType type = is_super ? METHOD_SUPER : METHOD_VIRTUAL;
2885 ArtMethod* called_method = VerifyInvocationArgs(inst, type, is_range);
Ian Rogersd8f69b02014-09-10 21:43:52 +00002886 const RegType* return_type = nullptr;
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002887 if (called_method != nullptr) {
Vladimir Markob45528c2017-07-27 14:14:28 +01002888 ObjPtr<mirror::Class> return_type_class = can_load_classes_
2889 ? called_method->ResolveReturnType()
2890 : called_method->LookupResolvedReturnType();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002891 if (return_type_class != nullptr) {
Andreas Gampef23f33d2015-06-23 14:18:17 -07002892 return_type = &FromClass(called_method->GetReturnTypeDescriptor(),
Vladimir Markob45528c2017-07-27 14:14:28 +01002893 return_type_class.Ptr(),
Andreas Gampef23f33d2015-06-23 14:18:17 -07002894 return_type_class->CannotBeAssignedFromOtherTypes());
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002895 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07002896 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
2897 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002898 }
2899 }
2900 if (return_type == nullptr) {
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00002901 uint32_t method_idx = GetMethodIdxOfInvoke(inst);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002902 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
Andreas Gampea5b09a62016-11-17 15:21:22 -08002903 dex::TypeIndex return_type_idx =
2904 dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002905 const char* descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Mathieu Chartierbf99f772014-08-23 16:37:27 -07002906 return_type = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
jeffhaobdb76512011-09-07 11:43:16 -07002907 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002908 if (!return_type->IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002909 work_line_->SetResultRegisterType(this, *return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002910 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002911 work_line_->SetResultRegisterTypeWide(*return_type, return_type->HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002912 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002913 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002914 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002915 }
jeffhaobdb76512011-09-07 11:43:16 -07002916 case Instruction::INVOKE_DIRECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002917 case Instruction::INVOKE_DIRECT_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002918 bool is_range = (inst->Opcode() == Instruction::INVOKE_DIRECT_RANGE);
Alex Light7268d472016-01-20 15:50:01 -08002919 ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_DIRECT, is_range);
Ian Rogers46685432012-06-03 22:26:43 -07002920 const char* return_type_descriptor;
2921 bool is_constructor;
Ian Rogersd8f69b02014-09-10 21:43:52 +00002922 const RegType* return_type = nullptr;
Ian Rogers7b078e82014-09-10 14:44:24 -07002923 if (called_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002924 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogers46685432012-06-03 22:26:43 -07002925 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
Ian Rogersdfb325e2013-10-30 01:00:44 -07002926 is_constructor = strcmp("<init>", dex_file_->StringDataByIdx(method_id.name_idx_)) == 0;
Andreas Gampea5b09a62016-11-17 15:21:22 -08002927 dex::TypeIndex return_type_idx =
2928 dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogers46685432012-06-03 22:26:43 -07002929 return_type_descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2930 } else {
2931 is_constructor = called_method->IsConstructor();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002932 return_type_descriptor = called_method->GetReturnTypeDescriptor();
Vladimir Markob45528c2017-07-27 14:14:28 +01002933 ObjPtr<mirror::Class> return_type_class = can_load_classes_
2934 ? called_method->ResolveReturnType()
2935 : called_method->LookupResolvedReturnType();
Ian Rogers1ff3c982014-08-12 02:30:58 -07002936 if (return_type_class != nullptr) {
Andreas Gampef23f33d2015-06-23 14:18:17 -07002937 return_type = &FromClass(return_type_descriptor,
Vladimir Markob45528c2017-07-27 14:14:28 +01002938 return_type_class.Ptr(),
Andreas Gampef23f33d2015-06-23 14:18:17 -07002939 return_type_class->CannotBeAssignedFromOtherTypes());
Ian Rogers1ff3c982014-08-12 02:30:58 -07002940 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07002941 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
2942 self_->ClearException();
Ian Rogers1ff3c982014-08-12 02:30:58 -07002943 }
Ian Rogers46685432012-06-03 22:26:43 -07002944 }
2945 if (is_constructor) {
jeffhaobdb76512011-09-07 11:43:16 -07002946 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002947 * Some additional checks when calling a constructor. We know from the invocation arg check
2948 * that the "this" argument is an instance of called_method->klass. Now we further restrict
2949 * that to require that called_method->klass is the same as this->klass or this->super,
2950 * allowing the latter only if the "this" argument is the same as the "this" argument to
2951 * this method (which implies that we're in a constructor ourselves).
jeffhaobdb76512011-09-07 11:43:16 -07002952 */
Orion Hodsoncfa325e2016-10-13 10:25:54 +01002953 const RegType& this_type = work_line_->GetInvocationThis(this, inst);
jeffhaob57e9522012-04-26 18:08:21 -07002954 if (this_type.IsConflict()) // failure.
2955 break;
jeffhaobdb76512011-09-07 11:43:16 -07002956
jeffhaob57e9522012-04-26 18:08:21 -07002957 /* no null refs allowed (?) */
Andreas Gampeeed3a5d2017-11-29 14:58:34 -08002958 if (this_type.IsZeroOrNull()) {
jeffhaob57e9522012-04-26 18:08:21 -07002959 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unable to initialize null ref";
2960 break;
jeffhao2a8a90e2011-09-26 14:25:31 -07002961 }
jeffhaob57e9522012-04-26 18:08:21 -07002962
2963 /* must be in same class or in superclass */
Ian Rogersd8f69b02014-09-10 21:43:52 +00002964 // const RegType& this_super_klass = this_type.GetSuperClass(&reg_types_);
Ian Rogers46685432012-06-03 22:26:43 -07002965 // TODO: re-enable constructor type verification
2966 // if (this_super_klass.IsConflict()) {
jeffhaob57e9522012-04-26 18:08:21 -07002967 // Unknown super class, fail so we re-check at runtime.
Ian Rogers46685432012-06-03 22:26:43 -07002968 // Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "super class unknown for '" << this_type << "'";
2969 // break;
2970 // }
jeffhaob57e9522012-04-26 18:08:21 -07002971
2972 /* arg must be an uninitialized reference */
2973 if (!this_type.IsUninitializedTypes()) {
2974 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Expected initialization on uninitialized reference "
2975 << this_type;
2976 break;
2977 }
2978
2979 /*
2980 * Replace the uninitialized reference with an initialized one. We need to do this for all
2981 * registers that have the same object instance in them, not just the "this" register.
2982 */
Nicolas Geoffray98e6ce42016-02-16 18:42:15 +00002983 work_line_->MarkRefsAsInitialized(this, this_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002984 }
Ian Rogers1ff3c982014-08-12 02:30:58 -07002985 if (return_type == nullptr) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07002986 return_type = &reg_types_.FromDescriptor(GetClassLoader(), return_type_descriptor, false);
Ian Rogers1ff3c982014-08-12 02:30:58 -07002987 }
2988 if (!return_type->IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002989 work_line_->SetResultRegisterType(this, *return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002990 } else {
Ian Rogers1ff3c982014-08-12 02:30:58 -07002991 work_line_->SetResultRegisterTypeWide(*return_type, return_type->HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002992 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002993 just_set_result = true;
2994 break;
2995 }
2996 case Instruction::INVOKE_STATIC:
2997 case Instruction::INVOKE_STATIC_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002998 bool is_range = (inst->Opcode() == Instruction::INVOKE_STATIC_RANGE);
Alex Light7268d472016-01-20 15:50:01 -08002999 ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_STATIC, is_range);
Ian Rogers28ad40d2011-10-27 15:19:26 -07003000 const char* descriptor;
Ian Rogers7b078e82014-09-10 14:44:24 -07003001 if (called_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003002 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogers28ad40d2011-10-27 15:19:26 -07003003 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
Andreas Gampea5b09a62016-11-17 15:21:22 -08003004 dex::TypeIndex return_type_idx =
3005 dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Mathieu Chartier590fee92013-09-13 13:46:47 -07003006 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07003007 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003008 descriptor = called_method->GetReturnTypeDescriptor();
Ian Rogers28ad40d2011-10-27 15:19:26 -07003009 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00003010 const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003011 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003012 work_line_->SetResultRegisterType(this, return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003013 } else {
3014 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
3015 }
jeffhaobdb76512011-09-07 11:43:16 -07003016 just_set_result = true;
3017 }
3018 break;
jeffhaobdb76512011-09-07 11:43:16 -07003019 case Instruction::INVOKE_INTERFACE:
Ian Rogersd81871c2011-10-03 13:57:23 -07003020 case Instruction::INVOKE_INTERFACE_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003021 bool is_range = (inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE);
Alex Light7268d472016-01-20 15:50:01 -08003022 ArtMethod* abs_method = VerifyInvocationArgs(inst, METHOD_INTERFACE, is_range);
Ian Rogers7b078e82014-09-10 14:44:24 -07003023 if (abs_method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003024 mirror::Class* called_interface = abs_method->GetDeclaringClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07003025 if (!called_interface->IsInterface() && !called_interface->IsObjectClass()) {
3026 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected interface class in invoke-interface '"
David Sehr709b0702016-10-13 09:12:37 -07003027 << abs_method->PrettyMethod() << "'";
Ian Rogersad0b3a32012-04-16 14:50:24 -07003028 break;
Ian Rogers28ad40d2011-10-27 15:19:26 -07003029 }
Ian Rogers0d604842012-04-16 14:50:24 -07003030 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003031 /* Get the type of the "this" arg, which should either be a sub-interface of called
3032 * interface or Object (see comments in RegType::JoinClass).
3033 */
Orion Hodsoncfa325e2016-10-13 10:25:54 +01003034 const RegType& this_type = work_line_->GetInvocationThis(this, inst);
Andreas Gampeeed3a5d2017-11-29 14:58:34 -08003035 if (this_type.IsZeroOrNull()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003036 /* null pointer always passes (and always fails at runtime) */
3037 } else {
3038 if (this_type.IsUninitializedTypes()) {
3039 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface call on uninitialized object "
3040 << this_type;
3041 break;
3042 }
3043 // In the past we have tried to assert that "called_interface" is assignable
3044 // from "this_type.GetClass()", however, as we do an imprecise Join
3045 // (RegType::JoinClass) we don't have full information on what interfaces are
3046 // implemented by "this_type". For example, two classes may implement the same
3047 // interfaces and have a common parent that doesn't implement the interface. The
3048 // join will set "this_type" to the parent class and a test that this implements
3049 // the interface will incorrectly fail.
3050 }
3051 /*
3052 * We don't have an object instance, so we can't find the concrete method. However, all of
3053 * the type information is in the abstract method, so we're good.
3054 */
3055 const char* descriptor;
Ian Rogers7b078e82014-09-10 14:44:24 -07003056 if (abs_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003057 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07003058 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
Andreas Gampea5b09a62016-11-17 15:21:22 -08003059 dex::TypeIndex return_type_idx =
3060 dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Mathieu Chartierde40d472015-10-15 17:47:48 -07003061 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003062 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003063 descriptor = abs_method->GetReturnTypeDescriptor();
Ian Rogersad0b3a32012-04-16 14:50:24 -07003064 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00003065 const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003066 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003067 work_line_->SetResultRegisterType(this, return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003068 } else {
3069 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
3070 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003071 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07003072 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07003073 }
Narayan Kamath9823e782016-08-03 12:46:58 +01003074 case Instruction::INVOKE_POLYMORPHIC:
3075 case Instruction::INVOKE_POLYMORPHIC_RANGE: {
Orion Hodsoncfa325e2016-10-13 10:25:54 +01003076 bool is_range = (inst->Opcode() == Instruction::INVOKE_POLYMORPHIC_RANGE);
3077 ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_POLYMORPHIC, is_range);
3078 if (called_method == nullptr) {
3079 // Convert potential soft failures in VerifyInvocationArgs() to hard errors.
3080 if (failure_messages_.size() > 0) {
3081 std::string message = failure_messages_.back()->str();
3082 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << message;
3083 } else {
3084 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invoke-polymorphic verification failure.";
3085 }
3086 break;
3087 }
3088 if (!CheckSignaturePolymorphicMethod(called_method) ||
3089 !CheckSignaturePolymorphicReceiver(inst)) {
Orion Hodsonfe92d122018-01-02 10:45:17 +00003090 DCHECK(HasFailures());
Orion Hodsoncfa325e2016-10-13 10:25:54 +01003091 break;
3092 }
3093 const uint32_t proto_idx = (is_range) ? inst->VRegH_4rcc() : inst->VRegH_45cc();
Orion Hodsonac141392017-01-13 11:53:47 +00003094 const char* return_descriptor =
Orion Hodsoncfa325e2016-10-13 10:25:54 +01003095 dex_file_->GetReturnTypeDescriptor(dex_file_->GetProtoId(proto_idx));
3096 const RegType& return_type =
Orion Hodsonac141392017-01-13 11:53:47 +00003097 reg_types_.FromDescriptor(GetClassLoader(), return_descriptor, false);
Orion Hodsoncfa325e2016-10-13 10:25:54 +01003098 if (!return_type.IsLowHalf()) {
3099 work_line_->SetResultRegisterType(this, return_type);
3100 } else {
3101 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
3102 }
Orion Hodsonac141392017-01-13 11:53:47 +00003103 just_set_result = true;
Orion Hodsoncfa325e2016-10-13 10:25:54 +01003104 break;
Narayan Kamath9823e782016-08-03 12:46:58 +01003105 }
Orion Hodsonc069a302017-01-18 09:23:12 +00003106 case Instruction::INVOKE_CUSTOM:
3107 case Instruction::INVOKE_CUSTOM_RANGE: {
3108 // Verify registers based on method_type in the call site.
3109 bool is_range = (inst->Opcode() == Instruction::INVOKE_CUSTOM_RANGE);
3110
3111 // Step 1. Check the call site that produces the method handle for invocation
3112 const uint32_t call_site_idx = is_range ? inst->VRegB_3rc() : inst->VRegB_35c();
3113 if (!CheckCallSite(call_site_idx)) {
3114 DCHECK(HasFailures());
3115 break;
3116 }
3117
3118 // Step 2. Check the register arguments correspond to the expected arguments for the
3119 // method handle produced by step 1. The dex file verifier has checked ranges for
3120 // the first three arguments and CheckCallSite has checked the method handle type.
3121 CallSiteArrayValueIterator it(*dex_file_, dex_file_->GetCallSiteId(call_site_idx));
3122 it.Next(); // Skip to name.
3123 it.Next(); // Skip to method type of the method handle
3124 const uint32_t proto_idx = static_cast<uint32_t>(it.GetJavaValue().i);
3125 const DexFile::ProtoId& proto_id = dex_file_->GetProtoId(proto_idx);
3126 DexFileParameterIterator param_it(*dex_file_, proto_id);
3127 // Treat method as static as it has yet to be determined.
3128 VerifyInvocationArgsFromIterator(&param_it, inst, METHOD_STATIC, is_range, nullptr);
3129 const char* return_descriptor = dex_file_->GetReturnTypeDescriptor(proto_id);
3130
3131 // Step 3. Propagate return type information
3132 const RegType& return_type =
3133 reg_types_.FromDescriptor(GetClassLoader(), return_descriptor, false);
3134 if (!return_type.IsLowHalf()) {
3135 work_line_->SetResultRegisterType(this, return_type);
3136 } else {
3137 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
3138 }
3139 just_set_result = true;
3140 // TODO: Add compiler support for invoke-custom (b/35337872).
3141 Fail(VERIFY_ERROR_FORCE_INTERPRETER);
3142 break;
3143 }
jeffhaobdb76512011-09-07 11:43:16 -07003144 case Instruction::NEG_INT:
3145 case Instruction::NOT_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003146 work_line_->CheckUnaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003147 break;
3148 case Instruction::NEG_LONG:
3149 case Instruction::NOT_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07003150 work_line_->CheckUnaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003151 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07003152 break;
3153 case Instruction::NEG_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003154 work_line_->CheckUnaryOp(this, inst, reg_types_.Float(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07003155 break;
3156 case Instruction::NEG_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07003157 work_line_->CheckUnaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003158 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07003159 break;
3160 case Instruction::INT_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07003161 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003162 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003163 break;
3164 case Instruction::INT_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003165 work_line_->CheckUnaryOp(this, inst, reg_types_.Float(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003166 break;
3167 case Instruction::INT_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07003168 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003169 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003170 break;
3171 case Instruction::LONG_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003172 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003173 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07003174 break;
3175 case Instruction::LONG_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003176 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003177 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07003178 break;
3179 case Instruction::LONG_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07003180 work_line_->CheckUnaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003181 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07003182 break;
3183 case Instruction::FLOAT_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003184 work_line_->CheckUnaryOp(this, inst, reg_types_.Integer(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07003185 break;
3186 case Instruction::FLOAT_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07003187 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003188 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07003189 break;
3190 case Instruction::FLOAT_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07003191 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003192 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07003193 break;
3194 case Instruction::DOUBLE_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003195 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003196 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07003197 break;
3198 case Instruction::DOUBLE_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07003199 work_line_->CheckUnaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003200 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07003201 break;
3202 case Instruction::DOUBLE_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003203 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003204 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07003205 break;
3206 case Instruction::INT_TO_BYTE:
Ian Rogers7b078e82014-09-10 14:44:24 -07003207 work_line_->CheckUnaryOp(this, inst, reg_types_.Byte(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003208 break;
3209 case Instruction::INT_TO_CHAR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003210 work_line_->CheckUnaryOp(this, inst, reg_types_.Char(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003211 break;
3212 case Instruction::INT_TO_SHORT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003213 work_line_->CheckUnaryOp(this, inst, reg_types_.Short(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003214 break;
3215
3216 case Instruction::ADD_INT:
3217 case Instruction::SUB_INT:
3218 case Instruction::MUL_INT:
3219 case Instruction::REM_INT:
3220 case Instruction::DIV_INT:
3221 case Instruction::SHL_INT:
3222 case Instruction::SHR_INT:
3223 case Instruction::USHR_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003224 work_line_->CheckBinaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003225 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07003226 break;
3227 case Instruction::AND_INT:
3228 case Instruction::OR_INT:
3229 case Instruction::XOR_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003230 work_line_->CheckBinaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003231 reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07003232 break;
3233 case Instruction::ADD_LONG:
3234 case Instruction::SUB_LONG:
3235 case Instruction::MUL_LONG:
3236 case Instruction::DIV_LONG:
3237 case Instruction::REM_LONG:
3238 case Instruction::AND_LONG:
3239 case Instruction::OR_LONG:
3240 case Instruction::XOR_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07003241 work_line_->CheckBinaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003242 reg_types_.LongLo(), reg_types_.LongHi(),
3243 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07003244 break;
3245 case Instruction::SHL_LONG:
3246 case Instruction::SHR_LONG:
3247 case Instruction::USHR_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07003248 /* shift distance is Int, making these different from other binary operations */
Ian Rogers7b078e82014-09-10 14:44:24 -07003249 work_line_->CheckBinaryOpWideShift(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003250 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003251 break;
3252 case Instruction::ADD_FLOAT:
3253 case Instruction::SUB_FLOAT:
3254 case Instruction::MUL_FLOAT:
3255 case Instruction::DIV_FLOAT:
3256 case Instruction::REM_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003257 work_line_->CheckBinaryOp(this, inst, reg_types_.Float(), reg_types_.Float(),
3258 reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07003259 break;
3260 case Instruction::ADD_DOUBLE:
3261 case Instruction::SUB_DOUBLE:
3262 case Instruction::MUL_DOUBLE:
3263 case Instruction::DIV_DOUBLE:
3264 case Instruction::REM_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07003265 work_line_->CheckBinaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003266 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
3267 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07003268 break;
3269 case Instruction::ADD_INT_2ADDR:
3270 case Instruction::SUB_INT_2ADDR:
3271 case Instruction::MUL_INT_2ADDR:
3272 case Instruction::REM_INT_2ADDR:
3273 case Instruction::SHL_INT_2ADDR:
3274 case Instruction::SHR_INT_2ADDR:
3275 case Instruction::USHR_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003276 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
3277 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07003278 break;
3279 case Instruction::AND_INT_2ADDR:
3280 case Instruction::OR_INT_2ADDR:
3281 case Instruction::XOR_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003282 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
3283 reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07003284 break;
3285 case Instruction::DIV_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003286 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
3287 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07003288 break;
3289 case Instruction::ADD_LONG_2ADDR:
3290 case Instruction::SUB_LONG_2ADDR:
3291 case Instruction::MUL_LONG_2ADDR:
3292 case Instruction::DIV_LONG_2ADDR:
3293 case Instruction::REM_LONG_2ADDR:
3294 case Instruction::AND_LONG_2ADDR:
3295 case Instruction::OR_LONG_2ADDR:
3296 case Instruction::XOR_LONG_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003297 work_line_->CheckBinaryOp2addrWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003298 reg_types_.LongLo(), reg_types_.LongHi(),
3299 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07003300 break;
3301 case Instruction::SHL_LONG_2ADDR:
3302 case Instruction::SHR_LONG_2ADDR:
3303 case Instruction::USHR_LONG_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003304 work_line_->CheckBinaryOp2addrWideShift(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003305 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003306 break;
3307 case Instruction::ADD_FLOAT_2ADDR:
3308 case Instruction::SUB_FLOAT_2ADDR:
3309 case Instruction::MUL_FLOAT_2ADDR:
3310 case Instruction::DIV_FLOAT_2ADDR:
3311 case Instruction::REM_FLOAT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003312 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Float(), reg_types_.Float(),
3313 reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07003314 break;
3315 case Instruction::ADD_DOUBLE_2ADDR:
3316 case Instruction::SUB_DOUBLE_2ADDR:
3317 case Instruction::MUL_DOUBLE_2ADDR:
3318 case Instruction::DIV_DOUBLE_2ADDR:
3319 case Instruction::REM_DOUBLE_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003320 work_line_->CheckBinaryOp2addrWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003321 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
3322 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07003323 break;
3324 case Instruction::ADD_INT_LIT16:
Ian Rogersf72a11d2014-10-30 15:41:08 -07003325 case Instruction::RSUB_INT_LIT16:
jeffhaobdb76512011-09-07 11:43:16 -07003326 case Instruction::MUL_INT_LIT16:
3327 case Instruction::DIV_INT_LIT16:
3328 case Instruction::REM_INT_LIT16:
Ian Rogers7b078e82014-09-10 14:44:24 -07003329 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), false,
3330 true);
jeffhaobdb76512011-09-07 11:43:16 -07003331 break;
3332 case Instruction::AND_INT_LIT16:
3333 case Instruction::OR_INT_LIT16:
3334 case Instruction::XOR_INT_LIT16:
Ian Rogers7b078e82014-09-10 14:44:24 -07003335 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), true,
3336 true);
jeffhaobdb76512011-09-07 11:43:16 -07003337 break;
3338 case Instruction::ADD_INT_LIT8:
3339 case Instruction::RSUB_INT_LIT8:
3340 case Instruction::MUL_INT_LIT8:
3341 case Instruction::DIV_INT_LIT8:
3342 case Instruction::REM_INT_LIT8:
3343 case Instruction::SHL_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07003344 case Instruction::SHR_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07003345 case Instruction::USHR_INT_LIT8:
Ian Rogers7b078e82014-09-10 14:44:24 -07003346 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), false,
3347 false);
jeffhaobdb76512011-09-07 11:43:16 -07003348 break;
3349 case Instruction::AND_INT_LIT8:
3350 case Instruction::OR_INT_LIT8:
3351 case Instruction::XOR_INT_LIT8:
Ian Rogers7b078e82014-09-10 14:44:24 -07003352 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), true,
3353 false);
jeffhaobdb76512011-09-07 11:43:16 -07003354 break;
3355
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003356 // Special instructions.
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07003357 case Instruction::RETURN_VOID_NO_BARRIER:
3358 if (IsConstructor() && !IsStatic()) {
3359 auto& declaring_class = GetDeclaringClass();
Andreas Gampe68df3202015-06-22 11:35:46 -07003360 if (declaring_class.IsUnresolvedReference()) {
3361 // We must iterate over the fields, even if we cannot use mirror classes to do so. Do it
3362 // manually over the underlying dex file.
3363 uint32_t first_index = GetFirstFinalInstanceFieldIndex(*dex_file_,
3364 dex_file_->GetMethodId(dex_method_idx_).class_idx_);
Andreas Gampee2abbc62017-09-15 11:59:26 -07003365 if (first_index != dex::kDexNoIndex) {
Andreas Gampe68df3202015-06-22 11:35:46 -07003366 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void-no-barrier not expected for field "
3367 << first_index;
3368 }
3369 break;
3370 }
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07003371 auto* klass = declaring_class.GetClass();
3372 for (uint32_t i = 0, num_fields = klass->NumInstanceFields(); i < num_fields; ++i) {
3373 if (klass->GetInstanceField(i)->IsFinal()) {
Mathieu Chartiere86deef2015-03-19 13:43:37 -07003374 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void-no-barrier not expected for "
David Sehr709b0702016-10-13 09:12:37 -07003375 << klass->GetInstanceField(i)->PrettyField();
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07003376 break;
3377 }
3378 }
Sebastien Hertzcc10e0e2013-06-28 14:24:48 +02003379 }
Andreas Gampeb2917962015-07-31 13:36:10 -07003380 // Handle this like a RETURN_VOID now. Code is duplicated to separate standard from
3381 // quickened opcodes (otherwise this could be a fall-through).
3382 if (!IsConstructor()) {
3383 if (!GetMethodReturnType().IsConflict()) {
3384 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void not expected";
3385 }
3386 }
Sebastien Hertzcc10e0e2013-06-28 14:24:48 +02003387 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003388
Ian Rogersd81871c2011-10-03 13:57:23 -07003389 /* These should never appear during verification. */
Mathieu Chartierffc605c2014-12-10 10:35:44 -08003390 case Instruction::UNUSED_3E ... Instruction::UNUSED_43:
Narayan Kamath8ec3bd22016-08-03 12:46:23 +01003391 case Instruction::UNUSED_F3 ... Instruction::UNUSED_F9:
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003392 case Instruction::UNUSED_79:
3393 case Instruction::UNUSED_7A:
jeffhaod5347e02012-03-22 17:25:05 -07003394 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Unexpected opcode " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07003395 break;
3396
3397 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003398 * DO NOT add a "default" clause here. Without it the compiler will
jeffhaobdb76512011-09-07 11:43:16 -07003399 * complain if an instruction is missing (which is desirable).
3400 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003401 } // end - switch (dec_insn.opcode)
jeffhaobdb76512011-09-07 11:43:16 -07003402
Ian Rogersad0b3a32012-04-16 14:50:24 -07003403 if (have_pending_hard_failure_) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08003404 if (Runtime::Current()->IsAotCompiler()) {
3405 /* When AOT compiling, check that the last failure is a hard failure */
Andreas Gampeb588f4c2015-05-26 13:35:39 -07003406 if (failures_[failures_.size() - 1] != VERIFY_ERROR_BAD_CLASS_HARD) {
3407 LOG(ERROR) << "Pending failures:";
3408 for (auto& error : failures_) {
3409 LOG(ERROR) << error;
3410 }
3411 for (auto& error_msg : failure_messages_) {
3412 LOG(ERROR) << error_msg->str();
3413 }
3414 LOG(FATAL) << "Pending hard failure, but last failure not hard.";
3415 }
Ian Rogerse1758fe2012-04-19 11:31:15 -07003416 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003417 /* immediate failure, reject class */
3418 info_messages_ << "Rejecting opcode " << inst->DumpString(dex_file_);
3419 return false;
jeffhaofaf459e2012-08-31 15:32:47 -07003420 } else if (have_pending_runtime_throw_failure_) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07003421 /* checking interpreter will throw, mark following code as unreachable */
jeffhaofaf459e2012-08-31 15:32:47 -07003422 opcode_flags = Instruction::kThrow;
Andreas Gampea727e372015-08-25 09:22:37 -07003423 // Note: the flag must be reset as it is only global to decouple Fail and is semantically per
3424 // instruction. However, RETURN checking may throw LOCKING errors, so we clear at the
3425 // very end.
jeffhaobdb76512011-09-07 11:43:16 -07003426 }
jeffhaobdb76512011-09-07 11:43:16 -07003427 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003428 * If we didn't just set the result register, clear it out. This ensures that you can only use
3429 * "move-result" immediately after the result is set. (We could check this statically, but it's
3430 * not expensive and it makes our debugging output cleaner.)
jeffhaobdb76512011-09-07 11:43:16 -07003431 */
3432 if (!just_set_result) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003433 work_line_->SetResultTypeToUnknown(this);
jeffhaobdb76512011-09-07 11:43:16 -07003434 }
3435
jeffhaobdb76512011-09-07 11:43:16 -07003436 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003437 * Handle "branch". Tag the branch target.
jeffhaobdb76512011-09-07 11:43:16 -07003438 *
3439 * NOTE: instructions like Instruction::EQZ provide information about the
jeffhaod1f0fde2011-09-08 17:25:33 -07003440 * state of the register when the branch is taken or not taken. For example,
jeffhaobdb76512011-09-07 11:43:16 -07003441 * somebody could get a reference field, check it for zero, and if the
3442 * branch is taken immediately store that register in a boolean field
jeffhaod1f0fde2011-09-08 17:25:33 -07003443 * since the value is known to be zero. We do not currently account for
jeffhaobdb76512011-09-07 11:43:16 -07003444 * that, and will reject the code.
3445 *
3446 * TODO: avoid re-fetching the branch target
3447 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003448 if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07003449 bool isConditional, selfOkay;
Ian Rogersd81871c2011-10-03 13:57:23 -07003450 if (!GetBranchOffset(work_insn_idx_, &branch_target, &isConditional, &selfOkay)) {
jeffhaobdb76512011-09-07 11:43:16 -07003451 /* should never happen after static verification */
jeffhaod5347e02012-03-22 17:25:05 -07003452 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad branch";
jeffhaobdb76512011-09-07 11:43:16 -07003453 return false;
3454 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08003455 DCHECK_EQ(isConditional, (opcode_flags & Instruction::kContinue) != 0);
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08003456 if (!CheckNotMoveExceptionOrMoveResult(code_item_accessor_.Insns(),
3457 work_insn_idx_ + branch_target)) {
jeffhaobdb76512011-09-07 11:43:16 -07003458 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07003459 }
jeffhaobdb76512011-09-07 11:43:16 -07003460 /* update branch target, set "changed" if appropriate */
Mathieu Chartierde40d472015-10-15 17:47:48 -07003461 if (nullptr != branch_line) {
Ian Rogersebbdd872014-07-07 23:53:08 -07003462 if (!UpdateRegisters(work_insn_idx_ + branch_target, branch_line.get(), false)) {
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003463 return false;
3464 }
3465 } else {
Ian Rogersebbdd872014-07-07 23:53:08 -07003466 if (!UpdateRegisters(work_insn_idx_ + branch_target, work_line_.get(), false)) {
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003467 return false;
3468 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003469 }
jeffhaobdb76512011-09-07 11:43:16 -07003470 }
3471
3472 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003473 * Handle "switch". Tag all possible branch targets.
jeffhaobdb76512011-09-07 11:43:16 -07003474 *
3475 * We've already verified that the table is structurally sound, so we
3476 * just need to walk through and tag the targets.
3477 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003478 if ((opcode_flags & Instruction::kSwitch) != 0) {
Andreas Gampe53de99c2015-08-17 13:43:55 -07003479 int offset_to_switch = insns[1] | (static_cast<int32_t>(insns[2]) << 16);
jeffhaobdb76512011-09-07 11:43:16 -07003480 const uint16_t* switch_insns = insns + offset_to_switch;
3481 int switch_count = switch_insns[1];
3482 int offset_to_targets, targ;
3483
3484 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
3485 /* 0 = sig, 1 = count, 2/3 = first key */
3486 offset_to_targets = 4;
3487 } else {
3488 /* 0 = sig, 1 = count, 2..count * 2 = keys */
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07003489 DCHECK((*insns & 0xff) == Instruction::SPARSE_SWITCH);
jeffhaobdb76512011-09-07 11:43:16 -07003490 offset_to_targets = 2 + 2 * switch_count;
3491 }
3492
3493 /* verify each switch target */
3494 for (targ = 0; targ < switch_count; targ++) {
3495 int offset;
3496 uint32_t abs_offset;
3497
3498 /* offsets are 32-bit, and only partly endian-swapped */
3499 offset = switch_insns[offset_to_targets + targ * 2] |
Andreas Gampe53de99c2015-08-17 13:43:55 -07003500 (static_cast<int32_t>(switch_insns[offset_to_targets + targ * 2 + 1]) << 16);
Ian Rogersd81871c2011-10-03 13:57:23 -07003501 abs_offset = work_insn_idx_ + offset;
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08003502 DCHECK_LT(abs_offset, code_item_accessor_.InsnsSizeInCodeUnits());
3503 if (!CheckNotMoveExceptionOrMoveResult(code_item_accessor_.Insns(), abs_offset)) {
jeffhaobdb76512011-09-07 11:43:16 -07003504 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07003505 }
Ian Rogersebbdd872014-07-07 23:53:08 -07003506 if (!UpdateRegisters(abs_offset, work_line_.get(), false)) {
jeffhaobdb76512011-09-07 11:43:16 -07003507 return false;
Ian Rogersebbdd872014-07-07 23:53:08 -07003508 }
jeffhaobdb76512011-09-07 11:43:16 -07003509 }
3510 }
3511
3512 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003513 * Handle instructions that can throw and that are sitting in a "try" block. (If they're not in a
3514 * "try" block when they throw, control transfers out of the method.)
jeffhaobdb76512011-09-07 11:43:16 -07003515 */
Mathieu Chartierde40d472015-10-15 17:47:48 -07003516 if ((opcode_flags & Instruction::kThrow) != 0 && GetInstructionFlags(work_insn_idx_).IsInTry()) {
Andreas Gampef91baf12014-07-18 15:41:00 -07003517 bool has_catch_all_handler = false;
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08003518 const DexFile::TryItem* try_item = code_item_accessor_.FindTryItem(work_insn_idx_);
3519 CHECK(try_item != nullptr);
Mathieu Chartierdc578c72017-12-27 11:51:45 -08003520 CatchHandlerIterator iterator(code_item_accessor_, *try_item);
jeffhaobdb76512011-09-07 11:43:16 -07003521
Andreas Gampef91baf12014-07-18 15:41:00 -07003522 // Need the linker to try and resolve the handled class to check if it's Throwable.
3523 ClassLinker* linker = Runtime::Current()->GetClassLinker();
3524
Ian Rogers0571d352011-11-03 19:51:38 -07003525 for (; iterator.HasNext(); iterator.Next()) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08003526 dex::TypeIndex handler_type_idx = iterator.GetHandlerTypeIndex();
3527 if (!handler_type_idx.IsValid()) {
Andreas Gampef91baf12014-07-18 15:41:00 -07003528 has_catch_all_handler = true;
3529 } else {
3530 // It is also a catch-all if it is java.lang.Throwable.
Vladimir Marko28e012a2017-12-07 11:22:59 +00003531 ObjPtr<mirror::Class> klass =
Vladimir Marko666ee3d2017-12-11 18:37:36 +00003532 linker->ResolveType(handler_type_idx, dex_cache_, class_loader_);
Andreas Gampef91baf12014-07-18 15:41:00 -07003533 if (klass != nullptr) {
3534 if (klass == mirror::Throwable::GetJavaLangThrowable()) {
3535 has_catch_all_handler = true;
3536 }
3537 } else {
3538 // Clear exception.
Ian Rogers7b078e82014-09-10 14:44:24 -07003539 DCHECK(self_->IsExceptionPending());
3540 self_->ClearException();
Andreas Gampef91baf12014-07-18 15:41:00 -07003541 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003542 }
jeffhaobdb76512011-09-07 11:43:16 -07003543 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003544 * Merge registers into the "catch" block. We want to use the "savedRegs" rather than
3545 * "work_regs", because at runtime the exception will be thrown before the instruction
3546 * modifies any registers.
jeffhaobdb76512011-09-07 11:43:16 -07003547 */
Ian Rogersebbdd872014-07-07 23:53:08 -07003548 if (!UpdateRegisters(iterator.GetHandlerAddress(), saved_line_.get(), false)) {
jeffhaobdb76512011-09-07 11:43:16 -07003549 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07003550 }
jeffhaobdb76512011-09-07 11:43:16 -07003551 }
3552
3553 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003554 * If the monitor stack depth is nonzero, there must be a "catch all" handler for this
3555 * instruction. This does apply to monitor-exit because of async exception handling.
jeffhaobdb76512011-09-07 11:43:16 -07003556 */
Andreas Gampef91baf12014-07-18 15:41:00 -07003557 if (work_line_->MonitorStackDepth() > 0 && !has_catch_all_handler) {
jeffhaobdb76512011-09-07 11:43:16 -07003558 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003559 * The state in work_line reflects the post-execution state. If the current instruction is a
3560 * monitor-enter and the monitor stack was empty, we don't need a catch-all (if it throws,
jeffhaobdb76512011-09-07 11:43:16 -07003561 * it will do so before grabbing the lock).
3562 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02003563 if (inst->Opcode() != Instruction::MONITOR_ENTER || work_line_->MonitorStackDepth() != 1) {
jeffhaod5347e02012-03-22 17:25:05 -07003564 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
Ian Rogersd81871c2011-10-03 13:57:23 -07003565 << "expected to be within a catch-all for an instruction where a monitor is held";
jeffhaobdb76512011-09-07 11:43:16 -07003566 return false;
3567 }
3568 }
3569 }
3570
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003571 /* Handle "continue". Tag the next consecutive instruction.
3572 * Note: Keep the code handling "continue" case below the "branch" and "switch" cases,
3573 * because it changes work_line_ when performing peephole optimization
3574 * and this change should not be used in those cases.
3575 */
Ian Rogers6d376ae2013-07-23 15:12:40 -07003576 if ((opcode_flags & Instruction::kContinue) != 0) {
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08003577 DCHECK_EQ(&code_item_accessor_.InstructionAt(work_insn_idx_), inst);
Ian Rogers7b078e82014-09-10 14:44:24 -07003578 uint32_t next_insn_idx = work_insn_idx_ + inst->SizeInCodeUnits();
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08003579 if (next_insn_idx >= code_item_accessor_.InsnsSizeInCodeUnits()) {
Ian Rogers6d376ae2013-07-23 15:12:40 -07003580 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Execution can walk off end of code area";
3581 return false;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003582 }
Ian Rogers6d376ae2013-07-23 15:12:40 -07003583 // The only way to get to a move-exception instruction is to get thrown there. Make sure the
3584 // next instruction isn't one.
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08003585 if (!CheckNotMoveException(code_item_accessor_.Insns(), next_insn_idx)) {
Ian Rogers6d376ae2013-07-23 15:12:40 -07003586 return false;
3587 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07003588 if (nullptr != fallthrough_line) {
Ian Rogers6d376ae2013-07-23 15:12:40 -07003589 // Make workline consistent with fallthrough computed from peephole optimization.
3590 work_line_->CopyFromLine(fallthrough_line.get());
3591 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07003592 if (GetInstructionFlags(next_insn_idx).IsReturn()) {
Ian Rogersb8c78592013-07-25 23:52:52 +00003593 // For returns we only care about the operand to the return, all other registers are dead.
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08003594 const Instruction* ret_inst = &code_item_accessor_.InstructionAt(next_insn_idx);
Andreas Gampea727e372015-08-25 09:22:37 -07003595 AdjustReturnLine(this, ret_inst, work_line_.get());
Ian Rogersb8c78592013-07-25 23:52:52 +00003596 }
Ian Rogers6d376ae2013-07-23 15:12:40 -07003597 RegisterLine* next_line = reg_table_.GetLine(next_insn_idx);
Ian Rogers7b078e82014-09-10 14:44:24 -07003598 if (next_line != nullptr) {
Ian Rogersebbdd872014-07-07 23:53:08 -07003599 // Merge registers into what we have for the next instruction, and set the "changed" flag if
3600 // needed. If the merge changes the state of the registers then the work line will be
3601 // updated.
3602 if (!UpdateRegisters(next_insn_idx, work_line_.get(), true)) {
Ian Rogers6d376ae2013-07-23 15:12:40 -07003603 return false;
3604 }
3605 } else {
3606 /*
3607 * We're not recording register data for the next instruction, so we don't know what the
3608 * prior state was. We have to assume that something has changed and re-evaluate it.
3609 */
Mathieu Chartierde40d472015-10-15 17:47:48 -07003610 GetInstructionFlags(next_insn_idx).SetChanged();
Ian Rogers6d376ae2013-07-23 15:12:40 -07003611 }
3612 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003613
jeffhaod1f0fde2011-09-08 17:25:33 -07003614 /* If we're returning from the method, make sure monitor stack is empty. */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003615 if ((opcode_flags & Instruction::kReturn) != 0) {
Andreas Gampea727e372015-08-25 09:22:37 -07003616 work_line_->VerifyMonitorStackEmpty(this);
jeffhaobdb76512011-09-07 11:43:16 -07003617 }
3618
3619 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003620 * Update start_guess. Advance to the next instruction of that's
3621 * possible, otherwise use the branch target if one was found. If
jeffhaobdb76512011-09-07 11:43:16 -07003622 * neither of those exists we're in a return or throw; leave start_guess
3623 * alone and let the caller sort it out.
3624 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003625 if ((opcode_flags & Instruction::kContinue) != 0) {
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08003626 DCHECK_EQ(&code_item_accessor_.InstructionAt(work_insn_idx_), inst);
Ian Rogers7b078e82014-09-10 14:44:24 -07003627 *start_guess = work_insn_idx_ + inst->SizeInCodeUnits();
Elliott Hughesadb8c672012-03-06 16:49:32 -08003628 } else if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07003629 /* we're still okay if branch_target is zero */
Ian Rogersd81871c2011-10-03 13:57:23 -07003630 *start_guess = work_insn_idx_ + branch_target;
jeffhaobdb76512011-09-07 11:43:16 -07003631 }
3632
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08003633 DCHECK_LT(*start_guess, code_item_accessor_.InsnsSizeInCodeUnits());
Mathieu Chartierde40d472015-10-15 17:47:48 -07003634 DCHECK(GetInstructionFlags(*start_guess).IsOpcode());
jeffhaobdb76512011-09-07 11:43:16 -07003635
Andreas Gampea727e372015-08-25 09:22:37 -07003636 if (have_pending_runtime_throw_failure_) {
3637 have_any_pending_runtime_throw_failure_ = true;
3638 // Reset the pending_runtime_throw flag now.
3639 have_pending_runtime_throw_failure_ = false;
3640 }
3641
jeffhaobdb76512011-09-07 11:43:16 -07003642 return true;
Brian Carlstrom7934ac22013-07-26 10:54:15 -07003643} // NOLINT(readability/fn_size)
jeffhaobdb76512011-09-07 11:43:16 -07003644
Mathieu Chartierde40d472015-10-15 17:47:48 -07003645void MethodVerifier::UninstantiableError(const char* descriptor) {
3646 Fail(VerifyError::VERIFY_ERROR_NO_CLASS) << "Could not create precise reference for "
3647 << "non-instantiable klass " << descriptor;
3648}
3649
Vladimir Marko28e012a2017-12-07 11:22:59 +00003650inline bool MethodVerifier::IsInstantiableOrPrimitive(ObjPtr<mirror::Class> klass) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07003651 return klass->IsInstantiable() || klass->IsPrimitive();
3652}
3653
Andreas Gampe98be1a92017-08-28 08:25:45 -07003654template <MethodVerifier::CheckAccess C>
3655const RegType& MethodVerifier::ResolveClass(dex::TypeIndex class_idx) {
Vladimir Marko666ee3d2017-12-11 18:37:36 +00003656 ClassLinker* linker = Runtime::Current()->GetClassLinker();
Vladimir Marko28e012a2017-12-07 11:22:59 +00003657 ObjPtr<mirror::Class> klass = can_load_classes_
Vladimir Marko666ee3d2017-12-11 18:37:36 +00003658 ? linker->ResolveType(class_idx, dex_cache_, class_loader_)
3659 : linker->LookupResolvedType(class_idx, dex_cache_.Get(), class_loader_.Get());
Vladimir Marko8d6768d2017-03-14 10:13:21 +00003660 if (can_load_classes_ && klass == nullptr) {
3661 DCHECK(self_->IsExceptionPending());
3662 self_->ClearException();
3663 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07003664 const RegType* result = nullptr;
Vladimir Marko9cb0c462017-04-21 13:31:41 +01003665 if (klass != nullptr) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07003666 bool precise = klass->CannotBeAssignedFromOtherTypes();
3667 if (precise && !IsInstantiableOrPrimitive(klass)) {
3668 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
3669 UninstantiableError(descriptor);
3670 precise = false;
3671 }
Vladimir Marko28e012a2017-12-07 11:22:59 +00003672 result = reg_types_.FindClass(klass.Ptr(), precise);
Mathieu Chartierde40d472015-10-15 17:47:48 -07003673 if (result == nullptr) {
3674 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
Vladimir Marko28e012a2017-12-07 11:22:59 +00003675 result = reg_types_.InsertClass(descriptor, klass.Ptr(), precise);
Mathieu Chartierde40d472015-10-15 17:47:48 -07003676 }
3677 } else {
3678 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
3679 result = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003680 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07003681 DCHECK(result != nullptr);
3682 if (result->IsConflict()) {
3683 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
3684 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "accessing broken descriptor '" << descriptor
3685 << "' in " << GetDeclaringClass();
3686 return *result;
3687 }
David Brazdilca3c8c32016-09-06 14:04:48 +01003688
3689 // Record result of class resolution attempt.
Vladimir Marko28e012a2017-12-07 11:22:59 +00003690 VerifierDeps::MaybeRecordClassResolution(*dex_file_, class_idx, klass.Ptr());
David Brazdilca3c8c32016-09-06 14:04:48 +01003691
Andreas Gampe629be512017-08-25 17:09:32 -07003692 // If requested, check if access is allowed. Unresolved types are included in this check, as the
3693 // interpreter only tests whether access is allowed when a class is not pre-verified and runs in
3694 // the access-checks interpreter. If result is primitive, skip the access check.
3695 //
3696 // Note: we do this for unresolved classes to trigger re-verification at runtime.
3697 if (C == CheckAccess::kYes && result->IsNonZeroReferenceTypes()) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07003698 const RegType& referrer = GetDeclaringClass();
Andreas Gampe629be512017-08-25 17:09:32 -07003699 if (!referrer.CanAccess(*result)) {
3700 Fail(VERIFY_ERROR_ACCESS_CLASS) << "(possibly) illegal class access: '"
Nicolas Geoffray4e868fa2017-04-21 17:16:44 +01003701 << referrer << "' -> '" << *result << "'";
Mathieu Chartierde40d472015-10-15 17:47:48 -07003702 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07003703 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07003704 return *result;
Ian Rogersd81871c2011-10-03 13:57:23 -07003705}
3706
Andreas Gampedd309072017-08-31 07:47:37 -07003707// Instantiate ResolveClass variants. This is required as the -inl file has a function with a call
3708// to ResolveClass, and compilers may decide to inline, requiring a symbol.
Andreas Gampeae0b1f42017-08-30 17:36:26 -07003709template const RegType& MethodVerifier::ResolveClass<MethodVerifier::CheckAccess::kNo>(
3710 dex::TypeIndex class_idx);
3711template const RegType& MethodVerifier::ResolveClass<MethodVerifier::CheckAccess::kYes>(
3712 dex::TypeIndex class_idx);
3713
Ian Rogersd8f69b02014-09-10 21:43:52 +00003714const RegType& MethodVerifier::GetCaughtExceptionType() {
Ian Rogers7b078e82014-09-10 14:44:24 -07003715 const RegType* common_super = nullptr;
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08003716 if (code_item_accessor_.TriesSize() != 0) {
3717 const uint8_t* handlers_ptr = code_item_accessor_.GetCatchHandlerData();
Ian Rogersd81871c2011-10-03 13:57:23 -07003718 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
3719 for (uint32_t i = 0; i < handlers_size; i++) {
Ian Rogers0571d352011-11-03 19:51:38 -07003720 CatchHandlerIterator iterator(handlers_ptr);
3721 for (; iterator.HasNext(); iterator.Next()) {
3722 if (iterator.GetHandlerAddress() == (uint32_t) work_insn_idx_) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08003723 if (!iterator.GetHandlerTypeIndex().IsValid()) {
Ian Rogersb4903572012-10-11 11:52:56 -07003724 common_super = &reg_types_.JavaLangThrowable(false);
Ian Rogersd81871c2011-10-03 13:57:23 -07003725 } else {
Andreas Gampe98be1a92017-08-28 08:25:45 -07003726 const RegType& exception =
3727 ResolveClass<CheckAccess::kYes>(iterator.GetHandlerTypeIndex());
David Brazdilca3c8c32016-09-06 14:04:48 +01003728 if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(exception, this)) {
David Brazdil68b5c0b2016-01-19 14:25:29 +00003729 DCHECK(!exception.IsUninitializedTypes()); // Comes from dex, shouldn't be uninit.
Jeff Haoc26a56c2013-11-04 12:00:47 -08003730 if (exception.IsUnresolvedTypes()) {
3731 // We don't know enough about the type. Fail here and let runtime handle it.
3732 Fail(VERIFY_ERROR_NO_CLASS) << "unresolved exception class " << exception;
3733 return exception;
3734 } else {
3735 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unexpected non-exception class " << exception;
3736 return reg_types_.Conflict();
3737 }
Jeff Haob878f212014-04-24 16:25:36 -07003738 } else if (common_super == nullptr) {
3739 common_super = &exception;
Ian Rogers28ad40d2011-10-27 15:19:26 -07003740 } else if (common_super->Equals(exception)) {
Ian Rogersc4762272012-02-01 15:55:55 -08003741 // odd case, but nothing to do
Ian Rogersd81871c2011-10-03 13:57:23 -07003742 } else {
David Brazdilca3c8c32016-09-06 14:04:48 +01003743 common_super = &common_super->Merge(exception, &reg_types_, this);
Mathieu Chartierc2a2bda2018-03-12 11:21:52 -07003744 if (FailOrAbort(reg_types_.JavaLangThrowable(false).IsAssignableFrom(
David Brazdilca3c8c32016-09-06 14:04:48 +01003745 *common_super, this),
Andreas Gampe7c038102014-10-27 20:08:46 -07003746 "java.lang.Throwable is not assignable-from common_super at ",
3747 work_insn_idx_)) {
3748 break;
3749 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003750 }
3751 }
3752 }
3753 }
Ian Rogers0571d352011-11-03 19:51:38 -07003754 handlers_ptr = iterator.EndDataPointer();
Ian Rogersd81871c2011-10-03 13:57:23 -07003755 }
3756 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003757 if (common_super == nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003758 /* no catch blocks, or no catches with classes we can find */
jeffhaod5347e02012-03-22 17:25:05 -07003759 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unable to find exception handler";
Ian Rogersad0b3a32012-04-16 14:50:24 -07003760 return reg_types_.Conflict();
Ian Rogersd81871c2011-10-03 13:57:23 -07003761 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07003762 return *common_super;
Ian Rogersd81871c2011-10-03 13:57:23 -07003763}
3764
Mathieu Chartiere401d142015-04-22 13:56:20 -07003765ArtMethod* MethodVerifier::ResolveMethodAndCheckAccess(
Alex Light7268d472016-01-20 15:50:01 -08003766 uint32_t dex_method_idx, MethodType method_type) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003767 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx);
Andreas Gampe98be1a92017-08-28 08:25:45 -07003768 const RegType& klass_type = ResolveClass<CheckAccess::kYes>(method_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003769 if (klass_type.IsConflict()) {
3770 std::string append(" in attempt to access method ");
3771 append += dex_file_->GetMethodName(method_id);
3772 AppendToLastFailMessage(append);
Ian Rogers7b078e82014-09-10 14:44:24 -07003773 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08003774 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003775 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003776 return nullptr; // Can't resolve Class so no more to do here
Ian Rogers90040192011-12-16 08:54:29 -08003777 }
Vladimir Markoba118822017-06-12 15:41:56 +01003778 ObjPtr<mirror::Class> klass = klass_type.GetClass();
Ian Rogersd8f69b02014-09-10 21:43:52 +00003779 const RegType& referrer = GetDeclaringClass();
Nicolas Geoffrayea179f42018-02-08 22:30:18 +00003780 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
3781 PointerSize pointer_size = class_linker->GetImagePointerSize();
Andreas Gampe42ef8ab2015-12-03 17:27:32 -08003782
Mathieu Chartiere401d142015-04-22 13:56:20 -07003783 ArtMethod* res_method = dex_cache_->GetResolvedMethod(dex_method_idx, pointer_size);
Ian Rogers7b078e82014-09-10 14:44:24 -07003784 if (res_method == nullptr) {
Nicolas Geoffrayea179f42018-02-08 22:30:18 +00003785 res_method = class_linker->FindResolvedMethod(
3786 klass, dex_cache_.Get(), class_loader_.Get(), dex_method_idx);
Ian Rogersd81871c2011-10-03 13:57:23 -07003787 }
David Brazdilca3c8c32016-09-06 14:04:48 +01003788
Vladimir Markoba118822017-06-12 15:41:56 +01003789 // Record result of method resolution attempt. The klass resolution has recorded whether
3790 // the class is an interface or not and therefore the type of the lookup performed above.
3791 // TODO: Maybe we should not record dependency if the invoke type does not match the lookup type.
3792 VerifierDeps::MaybeRecordMethodResolution(*dex_file_, dex_method_idx, res_method);
3793
Alex Lightafb66472017-08-01 09:54:49 -07003794 bool must_fail = false;
3795 // This is traditional and helps with screwy bytecode. It will tell you that, yes, a method
3796 // exists, but that it's called incorrectly. This significantly helps debugging, as locally it's
3797 // hard to see the differences.
3798 // If we don't have res_method here we must fail. Just use this bool to make sure of that with a
3799 // DCHECK.
Vladimir Markoba118822017-06-12 15:41:56 +01003800 if (res_method == nullptr) {
Alex Lightafb66472017-08-01 09:54:49 -07003801 must_fail = true;
Vladimir Markoba118822017-06-12 15:41:56 +01003802 // Try to find the method also with the other type for better error reporting below
3803 // but do not store such bogus lookup result in the DexCache or VerifierDeps.
David Brazdil4525e0b2018-04-05 16:57:32 +01003804 res_method = class_linker->FindIncompatibleMethod(
3805 klass, dex_cache_.Get(), class_loader_.Get(), dex_method_idx);
Vladimir Markoba118822017-06-12 15:41:56 +01003806 }
David Brazdilca3c8c32016-09-06 14:04:48 +01003807
3808 if (res_method == nullptr) {
3809 Fail(VERIFY_ERROR_NO_METHOD) << "couldn't find method "
David Sehr709b0702016-10-13 09:12:37 -07003810 << klass->PrettyDescriptor() << "."
David Brazdilca3c8c32016-09-06 14:04:48 +01003811 << dex_file_->GetMethodName(method_id) << " "
3812 << dex_file_->GetMethodSignature(method_id);
3813 return nullptr;
3814 }
3815
Ian Rogersd81871c2011-10-03 13:57:23 -07003816 // Make sure calls to constructors are "direct". There are additional restrictions but we don't
3817 // enforce them here.
3818 if (res_method->IsConstructor() && method_type != METHOD_DIRECT) {
jeffhaod5347e02012-03-22 17:25:05 -07003819 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting non-direct call to constructor "
David Sehr709b0702016-10-13 09:12:37 -07003820 << res_method->PrettyMethod();
Ian Rogers7b078e82014-09-10 14:44:24 -07003821 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003822 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003823 // Disallow any calls to class initializers.
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003824 if (res_method->IsClassInitializer()) {
jeffhaod5347e02012-03-22 17:25:05 -07003825 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting call to class initializer "
David Sehr709b0702016-10-13 09:12:37 -07003826 << res_method->PrettyMethod();
Ian Rogers7b078e82014-09-10 14:44:24 -07003827 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003828 }
Andreas Gampe42ef8ab2015-12-03 17:27:32 -08003829
3830 // Check that interface methods are static or match interface classes.
3831 // We only allow statics if we don't have default methods enabled.
3832 //
3833 // Note: this check must be after the initializer check, as those are required to fail a class,
3834 // while this check implies an IncompatibleClassChangeError.
3835 if (klass->IsInterface()) {
Alex Lightb55f1ac2016-04-12 15:50:55 -07003836 // methods called on interfaces should be invoke-interface, invoke-super, invoke-direct (if
Mathieu Chartierf6e31472017-12-28 13:32:08 -08003837 // default methods are supported for the dex file), or invoke-static.
Andreas Gampe42ef8ab2015-12-03 17:27:32 -08003838 if (method_type != METHOD_INTERFACE &&
Neil Fuller9724c632016-01-07 15:42:47 +00003839 method_type != METHOD_STATIC &&
Mathieu Chartierf6e31472017-12-28 13:32:08 -08003840 (!dex_file_->SupportsDefaultMethods() ||
Alex Lightb55f1ac2016-04-12 15:50:55 -07003841 method_type != METHOD_DIRECT) &&
Neil Fuller9724c632016-01-07 15:42:47 +00003842 method_type != METHOD_SUPER) {
Andreas Gampe42ef8ab2015-12-03 17:27:32 -08003843 Fail(VERIFY_ERROR_CLASS_CHANGE)
David Sehr709b0702016-10-13 09:12:37 -07003844 << "non-interface method " << dex_file_->PrettyMethod(dex_method_idx)
3845 << " is in an interface class " << klass->PrettyClass();
Andreas Gampe42ef8ab2015-12-03 17:27:32 -08003846 return nullptr;
3847 }
3848 } else {
3849 if (method_type == METHOD_INTERFACE) {
3850 Fail(VERIFY_ERROR_CLASS_CHANGE)
David Sehr709b0702016-10-13 09:12:37 -07003851 << "interface method " << dex_file_->PrettyMethod(dex_method_idx)
3852 << " is in a non-interface class " << klass->PrettyClass();
Andreas Gampe42ef8ab2015-12-03 17:27:32 -08003853 return nullptr;
3854 }
3855 }
3856
Alex Lightafb66472017-08-01 09:54:49 -07003857 // Check specifically for non-public object methods being provided for interface dispatch. This
3858 // can occur if we failed to find a method with FindInterfaceMethod but later find one with
3859 // FindClassMethod for error message use.
3860 if (method_type == METHOD_INTERFACE &&
3861 res_method->GetDeclaringClass()->IsObjectClass() &&
3862 !res_method->IsPublic()) {
3863 Fail(VERIFY_ERROR_NO_METHOD) << "invoke-interface " << klass->PrettyDescriptor() << "."
3864 << dex_file_->GetMethodName(method_id) << " "
3865 << dex_file_->GetMethodSignature(method_id) << " resolved to "
3866 << "non-public object method " << res_method->PrettyMethod() << " "
3867 << "but non-public Object methods are excluded from interface "
3868 << "method resolution.";
3869 return nullptr;
3870 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003871 // Check if access is allowed.
Ian Rogersad0b3a32012-04-16 14:50:24 -07003872 if (!referrer.CanAccessMember(res_method->GetDeclaringClass(), res_method->GetAccessFlags())) {
David Sehr709b0702016-10-13 09:12:37 -07003873 Fail(VERIFY_ERROR_ACCESS_METHOD) << "illegal method access (call "
3874 << res_method->PrettyMethod()
Ian Rogersad0b3a32012-04-16 14:50:24 -07003875 << " from " << referrer << ")";
jeffhaob57e9522012-04-26 18:08:21 -07003876 return res_method;
jeffhao8cd6dda2012-02-22 10:15:34 -08003877 }
jeffhaode0d9c92012-02-27 13:58:13 -08003878 // Check that invoke-virtual and invoke-super are not used on private methods of the same class.
Alex Light7268d472016-01-20 15:50:01 -08003879 if (res_method->IsPrivate() && (method_type == METHOD_VIRTUAL || method_type == METHOD_SUPER)) {
jeffhaod5347e02012-03-22 17:25:05 -07003880 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invoke-super/virtual can't be used on private method "
David Sehr709b0702016-10-13 09:12:37 -07003881 << res_method->PrettyMethod();
Ian Rogers7b078e82014-09-10 14:44:24 -07003882 return nullptr;
jeffhaode0d9c92012-02-27 13:58:13 -08003883 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003884 // See if the method type implied by the invoke instruction matches the access flags for the
Orion Hodsoncfa325e2016-10-13 10:25:54 +01003885 // target method. The flags for METHOD_POLYMORPHIC are based on there being precisely two
3886 // signature polymorphic methods supported by the run-time which are native methods with variable
3887 // arguments.
Brian Carlstrombe6fa5e2014-12-09 20:15:42 -08003888 if ((method_type == METHOD_DIRECT && (!res_method->IsDirect() || res_method->IsStatic())) ||
Ian Rogersd81871c2011-10-03 13:57:23 -07003889 (method_type == METHOD_STATIC && !res_method->IsStatic()) ||
Alex Light7268d472016-01-20 15:50:01 -08003890 ((method_type == METHOD_SUPER ||
3891 method_type == METHOD_VIRTUAL ||
Orion Hodsoncfa325e2016-10-13 10:25:54 +01003892 method_type == METHOD_INTERFACE) && res_method->IsDirect()) ||
3893 ((method_type == METHOD_POLYMORPHIC) &&
3894 (!res_method->IsNative() || !res_method->IsVarargs()))) {
Ian Rogers2fc14272012-08-30 10:56:57 -07003895 Fail(VERIFY_ERROR_CLASS_CHANGE) << "invoke type (" << method_type << ") does not match method "
Orion Hodsoncfa325e2016-10-13 10:25:54 +01003896 "type of " << res_method->PrettyMethod();
Ian Rogers7b078e82014-09-10 14:44:24 -07003897 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003898 }
Alex Lightafb66472017-08-01 09:54:49 -07003899 // Make sure we weren't expecting to fail.
3900 DCHECK(!must_fail) << "invoke type (" << method_type << ")"
3901 << klass->PrettyDescriptor() << "."
3902 << dex_file_->GetMethodName(method_id) << " "
3903 << dex_file_->GetMethodSignature(method_id) << " unexpectedly resolved to "
3904 << res_method->PrettyMethod() << " without error. Initially this method was "
3905 << "not found so we were expecting to fail for some reason.";
jeffhao8cd6dda2012-02-22 10:15:34 -08003906 return res_method;
3907}
3908
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003909template <class T>
Mathieu Chartiere401d142015-04-22 13:56:20 -07003910ArtMethod* MethodVerifier::VerifyInvocationArgsFromIterator(
3911 T* it, const Instruction* inst, MethodType method_type, bool is_range, ArtMethod* res_method) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003912 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
3913 // match the call to the signature. Also, we might be calling through an abstract method
3914 // definition (which doesn't have register count values).
Orion Hodsoncfa325e2016-10-13 10:25:54 +01003915 const size_t expected_args = inst->VRegA();
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003916 /* caught by static verifier */
3917 DCHECK(is_range || expected_args <= 5);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003918
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08003919 if (expected_args > code_item_accessor_.OutsSize()) {
Orion Hodson1cda7c22017-08-10 13:06:45 +01003920 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08003921 << ") exceeds outsSize ("
3922 << code_item_accessor_.OutsSize() << ")";
Orion Hodson1cda7c22017-08-10 13:06:45 +01003923 return nullptr;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003924 }
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003925
3926 /*
3927 * Check the "this" argument, which must be an instance of the class that declared the method.
3928 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
3929 * rigorous check here (which is okay since we have to do it at runtime).
3930 */
3931 if (method_type != METHOD_STATIC) {
Orion Hodsoncfa325e2016-10-13 10:25:54 +01003932 const RegType& actual_arg_type = work_line_->GetInvocationThis(this, inst);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003933 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
3934 CHECK(have_pending_hard_failure_);
3935 return nullptr;
3936 }
David Brazdil68b5c0b2016-01-19 14:25:29 +00003937 bool is_init = false;
3938 if (actual_arg_type.IsUninitializedTypes()) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003939 if (res_method) {
3940 if (!res_method->IsConstructor()) {
3941 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
3942 return nullptr;
3943 }
3944 } else {
3945 // Check whether the name of the called method is "<init>"
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00003946 const uint32_t method_idx = GetMethodIdxOfInvoke(inst);
Jeff Hao0d087272014-08-04 14:47:17 -07003947 if (strcmp(dex_file_->GetMethodName(dex_file_->GetMethodId(method_idx)), "<init>") != 0) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003948 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
3949 return nullptr;
3950 }
3951 }
David Brazdil68b5c0b2016-01-19 14:25:29 +00003952 is_init = true;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003953 }
David Brazdil68b5c0b2016-01-19 14:25:29 +00003954 const RegType& adjusted_type = is_init
3955 ? GetRegTypeCache()->FromUninitialized(actual_arg_type)
3956 : actual_arg_type;
Andreas Gampeeed3a5d2017-11-29 14:58:34 -08003957 if (method_type != METHOD_INTERFACE && !adjusted_type.IsZeroOrNull()) {
Ian Rogersd8f69b02014-09-10 21:43:52 +00003958 const RegType* res_method_class;
Andreas Gamped9e23012015-06-04 22:19:58 -07003959 // Miranda methods have the declaring interface as their declaring class, not the abstract
3960 // class. It would be wrong to use this for the type check (interface type checks are
3961 // postponed to runtime).
3962 if (res_method != nullptr && !res_method->IsMiranda()) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003963 mirror::Class* klass = res_method->GetDeclaringClass();
Ian Rogers1ff3c982014-08-12 02:30:58 -07003964 std::string temp;
Andreas Gampef23f33d2015-06-23 14:18:17 -07003965 res_method_class = &FromClass(klass->GetDescriptor(&temp), klass,
3966 klass->CannotBeAssignedFromOtherTypes());
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003967 } else {
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00003968 const uint32_t method_idx = GetMethodIdxOfInvoke(inst);
Andreas Gampea5b09a62016-11-17 15:21:22 -08003969 const dex::TypeIndex class_idx = dex_file_->GetMethodId(method_idx).class_idx_;
Mathieu Chartierde40d472015-10-15 17:47:48 -07003970 res_method_class = &reg_types_.FromDescriptor(
3971 GetClassLoader(),
3972 dex_file_->StringByTypeIdx(class_idx),
3973 false);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003974 }
David Brazdilca3c8c32016-09-06 14:04:48 +01003975 if (!res_method_class->IsAssignableFrom(adjusted_type, this)) {
David Brazdil68b5c0b2016-01-19 14:25:29 +00003976 Fail(adjusted_type.IsUnresolvedTypes()
3977 ? VERIFY_ERROR_NO_CLASS
3978 : VERIFY_ERROR_BAD_CLASS_SOFT)
3979 << "'this' argument '" << actual_arg_type << "' not instance of '"
3980 << *res_method_class << "'";
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003981 // Continue on soft failures. We need to find possible hard failures to avoid problems in
3982 // the compiler.
3983 if (have_pending_hard_failure_) {
3984 return nullptr;
3985 }
3986 }
3987 }
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003988 }
3989
Orion Hodsoncfa325e2016-10-13 10:25:54 +01003990 uint32_t arg[5];
3991 if (!is_range) {
3992 inst->GetVarArgs(arg);
3993 }
3994 uint32_t sig_registers = (method_type == METHOD_STATIC) ? 0 : 1;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003995 for ( ; it->HasNext(); it->Next()) {
3996 if (sig_registers >= expected_args) {
3997 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, expected " << inst->VRegA() <<
Orion Hodsoncfa325e2016-10-13 10:25:54 +01003998 " argument registers, method signature has " << sig_registers + 1 << " or more";
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003999 return nullptr;
4000 }
4001
4002 const char* param_descriptor = it->GetDescriptor();
4003
4004 if (param_descriptor == nullptr) {
4005 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation because of missing signature "
4006 "component";
4007 return nullptr;
4008 }
4009
Ian Rogersd8f69b02014-09-10 21:43:52 +00004010 const RegType& reg_type = reg_types_.FromDescriptor(GetClassLoader(), param_descriptor, false);
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004011 uint32_t get_reg = is_range ? inst->VRegC() + static_cast<uint32_t>(sig_registers) :
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004012 arg[sig_registers];
4013 if (reg_type.IsIntegralTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004014 const RegType& src_type = work_line_->GetRegisterType(this, get_reg);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004015 if (!src_type.IsIntegralTypes()) {
4016 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register v" << get_reg << " has type " << src_type
4017 << " but expected " << reg_type;
Andreas Gampeb588f4c2015-05-26 13:35:39 -07004018 return nullptr;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004019 }
Andreas Gampeda9badb2015-06-05 20:22:12 -07004020 } else {
4021 if (!work_line_->VerifyRegisterType(this, get_reg, reg_type)) {
4022 // Continue on soft failures. We need to find possible hard failures to avoid problems in
4023 // the compiler.
4024 if (have_pending_hard_failure_) {
4025 return nullptr;
4026 }
4027 } else if (reg_type.IsLongOrDoubleTypes()) {
4028 // Check that registers are consecutive (for non-range invokes). Invokes are the only
4029 // instructions not specifying register pairs by the first component, but require them
4030 // nonetheless. Only check when there's an actual register in the parameters. If there's
4031 // none, this will fail below.
4032 if (!is_range && sig_registers + 1 < expected_args) {
4033 uint32_t second_reg = arg[sig_registers + 1];
4034 if (second_reg != get_reg + 1) {
4035 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, long or double parameter "
4036 "at index " << sig_registers << " is not a pair: " << get_reg << " + "
4037 << second_reg << ".";
4038 return nullptr;
4039 }
4040 }
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004041 }
4042 }
4043 sig_registers += reg_type.IsLongOrDoubleTypes() ? 2 : 1;
4044 }
4045 if (expected_args != sig_registers) {
4046 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, expected " << expected_args <<
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004047 " argument registers, method signature has " << sig_registers;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004048 return nullptr;
4049 }
4050 return res_method;
4051}
4052
4053void MethodVerifier::VerifyInvocationArgsUnresolvedMethod(const Instruction* inst,
4054 MethodType method_type,
4055 bool is_range) {
4056 // As the method may not have been resolved, make this static check against what we expect.
4057 // The main reason for this code block is to fail hard when we find an illegal use, e.g.,
4058 // wrong number of arguments or wrong primitive types, even if the method could not be resolved.
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00004059 const uint32_t method_idx = GetMethodIdxOfInvoke(inst);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004060 DexFileParameterIterator it(*dex_file_,
4061 dex_file_->GetProtoId(dex_file_->GetMethodId(method_idx).proto_idx_));
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004062 VerifyInvocationArgsFromIterator(&it, inst, method_type, is_range, nullptr);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004063}
4064
Orion Hodsonc069a302017-01-18 09:23:12 +00004065bool MethodVerifier::CheckCallSite(uint32_t call_site_idx) {
Orion Hodson3a842f52017-04-21 15:24:10 +01004066 if (call_site_idx >= dex_file_->NumCallSiteIds()) {
4067 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Bad call site id #" << call_site_idx
4068 << " >= " << dex_file_->NumCallSiteIds();
4069 return false;
4070 }
4071
Orion Hodsonc069a302017-01-18 09:23:12 +00004072 CallSiteArrayValueIterator it(*dex_file_, dex_file_->GetCallSiteId(call_site_idx));
4073 // Check essential arguments are provided. The dex file verifier has verified indicies of the
4074 // main values (method handle, name, method_type).
4075 if (it.Size() < 3) {
4076 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Call site #" << call_site_idx
4077 << " has too few arguments: "
Orion Hodsona5dca522018-02-27 12:42:11 +00004078 << it.Size() << " < 3";
Orion Hodsonc069a302017-01-18 09:23:12 +00004079 return false;
4080 }
4081
Orion Hodson3a842f52017-04-21 15:24:10 +01004082 // Get and check the first argument: the method handle (index range
4083 // checked by the dex file verifier).
Orion Hodsonc069a302017-01-18 09:23:12 +00004084 uint32_t method_handle_idx = static_cast<uint32_t>(it.GetJavaValue().i);
Orion Hodsona5dca522018-02-27 12:42:11 +00004085 if (method_handle_idx > dex_file_->NumMethodHandles()) {
4086 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Call site id #" << call_site_idx
4087 << " method handle index invalid " << method_handle_idx
4088 << " >= " << dex_file_->NumMethodHandles();
4089 return false;
4090 }
Orion Hodson3a842f52017-04-21 15:24:10 +01004091
Orion Hodsonc069a302017-01-18 09:23:12 +00004092 const DexFile::MethodHandleItem& mh = dex_file_->GetMethodHandle(method_handle_idx);
4093 if (mh.method_handle_type_ != static_cast<uint16_t>(DexFile::MethodHandleType::kInvokeStatic)) {
4094 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Call site #" << call_site_idx
Orion Hodson631827d2017-04-10 14:53:47 +01004095 << " argument 0 method handle type is not InvokeStatic: "
4096 << mh.method_handle_type_;
Orion Hodsonc069a302017-01-18 09:23:12 +00004097 return false;
4098 }
Orion Hodsonc069a302017-01-18 09:23:12 +00004099 return true;
4100}
4101
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004102class MethodParamListDescriptorIterator {
4103 public:
Mathieu Chartiere401d142015-04-22 13:56:20 -07004104 explicit MethodParamListDescriptorIterator(ArtMethod* res_method) :
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004105 res_method_(res_method), pos_(0), params_(res_method->GetParameterTypeList()),
4106 params_size_(params_ == nullptr ? 0 : params_->Size()) {
4107 }
4108
4109 bool HasNext() {
4110 return pos_ < params_size_;
4111 }
4112
4113 void Next() {
4114 ++pos_;
4115 }
4116
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07004117 const char* GetDescriptor() REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004118 return res_method_->GetTypeDescriptorFromTypeIdx(params_->GetTypeItem(pos_).type_idx_);
4119 }
4120
4121 private:
Mathieu Chartiere401d142015-04-22 13:56:20 -07004122 ArtMethod* res_method_;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004123 size_t pos_;
4124 const DexFile::TypeList* params_;
4125 const size_t params_size_;
4126};
4127
Mathieu Chartiere401d142015-04-22 13:56:20 -07004128ArtMethod* MethodVerifier::VerifyInvocationArgs(
Alex Light7268d472016-01-20 15:50:01 -08004129 const Instruction* inst, MethodType method_type, bool is_range) {
jeffhao8cd6dda2012-02-22 10:15:34 -08004130 // Resolve the method. This could be an abstract or concrete method depending on what sort of call
4131 // we're making.
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00004132 const uint32_t method_idx = GetMethodIdxOfInvoke(inst);
Alex Light7268d472016-01-20 15:50:01 -08004133 ArtMethod* res_method = ResolveMethodAndCheckAccess(method_idx, method_type);
Ian Rogers7b078e82014-09-10 14:44:24 -07004134 if (res_method == nullptr) { // error or class is unresolved
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004135 // Check what we can statically.
4136 if (!have_pending_hard_failure_) {
4137 VerifyInvocationArgsUnresolvedMethod(inst, method_type, is_range);
4138 }
4139 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08004140 }
4141
Ian Rogersd81871c2011-10-03 13:57:23 -07004142 // If we're using invoke-super(method), make sure that the executing method's class' superclass
Alex Light705ad492015-09-21 11:36:30 -07004143 // has a vtable entry for the target method. Or the target is on a interface.
Alex Light7268d472016-01-20 15:50:01 -08004144 if (method_type == METHOD_SUPER) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08004145 dex::TypeIndex class_idx = dex_file_->GetMethodId(method_idx).class_idx_;
David Brazdilca3c8c32016-09-06 14:04:48 +01004146 const RegType& reference_type = reg_types_.FromDescriptor(
4147 GetClassLoader(),
4148 dex_file_->StringByTypeIdx(class_idx),
4149 false);
4150 if (reference_type.IsUnresolvedTypes()) {
Alex Lightfedd91d2016-01-07 14:49:16 -08004151 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "Unable to find referenced class from invoke-super";
4152 return nullptr;
4153 }
David Brazdilca3c8c32016-09-06 14:04:48 +01004154 if (reference_type.GetClass()->IsInterface()) {
Alex Lightfedd91d2016-01-07 14:49:16 -08004155 // TODO Can we verify anything else.
David Brazdil15fc7292016-09-02 14:13:18 +01004156 if (class_idx == class_def_.class_idx_) {
Alex Lightfedd91d2016-01-07 14:49:16 -08004157 Fail(VERIFY_ERROR_CLASS_CHANGE) << "Cannot invoke-super on self as interface";
Alex Light55ea94d2016-03-15 09:50:26 -07004158 return nullptr;
Alex Lightfedd91d2016-01-07 14:49:16 -08004159 }
4160 // TODO Revisit whether we want to allow invoke-super on direct interfaces only like the JLS
4161 // does.
Alex Light55ea94d2016-03-15 09:50:26 -07004162 if (!GetDeclaringClass().HasClass()) {
4163 Fail(VERIFY_ERROR_NO_CLASS) << "Unable to resolve the full class of 'this' used in an"
4164 << "interface invoke-super";
4165 return nullptr;
David Brazdilca3c8c32016-09-06 14:04:48 +01004166 } else if (!reference_type.IsStrictlyAssignableFrom(GetDeclaringClass(), this)) {
Alex Lightfedd91d2016-01-07 14:49:16 -08004167 Fail(VERIFY_ERROR_CLASS_CHANGE)
David Sehr709b0702016-10-13 09:12:37 -07004168 << "invoke-super in " << mirror::Class::PrettyClass(GetDeclaringClass().GetClass())
4169 << " in method "
4170 << dex_file_->PrettyMethod(dex_method_idx_) << " to method "
4171 << dex_file_->PrettyMethod(method_idx) << " references "
4172 << "non-super-interface type " << mirror::Class::PrettyClass(reference_type.GetClass());
Alex Lightfedd91d2016-01-07 14:49:16 -08004173 return nullptr;
Alex Light705ad492015-09-21 11:36:30 -07004174 }
4175 } else {
4176 const RegType& super = GetDeclaringClass().GetSuperClass(&reg_types_);
4177 if (super.IsUnresolvedTypes()) {
4178 Fail(VERIFY_ERROR_NO_METHOD) << "unknown super class in invoke-super from "
David Sehr709b0702016-10-13 09:12:37 -07004179 << dex_file_->PrettyMethod(dex_method_idx_)
4180 << " to super " << res_method->PrettyMethod();
Alex Light705ad492015-09-21 11:36:30 -07004181 return nullptr;
4182 }
David Brazdilca3c8c32016-09-06 14:04:48 +01004183 if (!reference_type.IsStrictlyAssignableFrom(GetDeclaringClass(), this) ||
Aart Bikf663e342016-04-04 17:28:59 -07004184 (res_method->GetMethodIndex() >= super.GetClass()->GetVTableLength())) {
Alex Light705ad492015-09-21 11:36:30 -07004185 Fail(VERIFY_ERROR_NO_METHOD) << "invalid invoke-super from "
David Sehr709b0702016-10-13 09:12:37 -07004186 << dex_file_->PrettyMethod(dex_method_idx_)
Alex Light705ad492015-09-21 11:36:30 -07004187 << " to super " << super
4188 << "." << res_method->GetName()
4189 << res_method->GetSignature();
4190 return nullptr;
4191 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004192 }
4193 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08004194
Andreas Gampe74979b12017-05-16 09:28:06 -07004195 if (UNLIKELY(method_type == METHOD_POLYMORPHIC)) {
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004196 // Process the signature of the calling site that is invoking the method handle.
4197 DexFileParameterIterator it(*dex_file_, dex_file_->GetProtoId(inst->VRegH()));
4198 return VerifyInvocationArgsFromIterator(&it, inst, method_type, is_range, res_method);
4199 } else {
4200 // Process the target method's signature.
4201 MethodParamListDescriptorIterator it(res_method);
4202 return VerifyInvocationArgsFromIterator(&it, inst, method_type, is_range, res_method);
4203 }
4204}
4205
4206bool MethodVerifier::CheckSignaturePolymorphicMethod(ArtMethod* method) {
4207 mirror::Class* klass = method->GetDeclaringClass();
Orion Hodsonfe92d122018-01-02 10:45:17 +00004208 const char* method_name = method->GetName();
4209
4210 const char* expected_return_descriptor;
4211 if (klass == mirror::MethodHandle::StaticClass()) {
4212 expected_return_descriptor = mirror::MethodHandle::GetReturnTypeDescriptor(method_name);
4213 } else if (klass == mirror::VarHandle::StaticClass()) {
4214 expected_return_descriptor = mirror::VarHandle::GetReturnTypeDescriptor(method_name);
Orion Hodson928033d2018-02-07 05:30:54 +00004215 // TODO: add compiler support for VarHandle accessor methods (b/71781600)
4216 Fail(VERIFY_ERROR_FORCE_INTERPRETER);
Orion Hodsonfe92d122018-01-02 10:45:17 +00004217 } else {
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004218 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
Orion Hodsonfe92d122018-01-02 10:45:17 +00004219 << "Signature polymorphic method in unsuppported class: " << klass->PrettyDescriptor();
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004220 return false;
4221 }
4222
Orion Hodsonfe92d122018-01-02 10:45:17 +00004223 if (expected_return_descriptor == nullptr) {
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004224 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
4225 << "Signature polymorphic method name invalid: " << method_name;
4226 return false;
4227 }
4228
4229 const DexFile::TypeList* types = method->GetParameterTypeList();
4230 if (types->Size() != 1) {
4231 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
4232 << "Signature polymorphic method has too many arguments " << types->Size() << " != 1";
4233 return false;
4234 }
4235
4236 const dex::TypeIndex argument_type_index = types->GetTypeItem(0).type_idx_;
4237 const char* argument_descriptor = method->GetTypeDescriptorFromTypeIdx(argument_type_index);
4238 if (strcmp(argument_descriptor, "[Ljava/lang/Object;") != 0) {
4239 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
4240 << "Signature polymorphic method has unexpected argument type: " << argument_descriptor;
4241 return false;
4242 }
4243
4244 const char* return_descriptor = method->GetReturnTypeDescriptor();
Orion Hodsonfe92d122018-01-02 10:45:17 +00004245 if (strcmp(return_descriptor, expected_return_descriptor) != 0) {
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004246 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
Orion Hodsonfe92d122018-01-02 10:45:17 +00004247 << "Signature polymorphic method has unexpected return type: " << return_descriptor
4248 << " != " << expected_return_descriptor;
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004249 return false;
4250 }
4251
4252 return true;
4253}
4254
4255bool MethodVerifier::CheckSignaturePolymorphicReceiver(const Instruction* inst) {
4256 const RegType& this_type = work_line_->GetInvocationThis(this, inst);
Andreas Gampeeed3a5d2017-11-29 14:58:34 -08004257 if (this_type.IsZeroOrNull()) {
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004258 /* null pointer always passes (and always fails at run time) */
4259 return true;
4260 } else if (!this_type.IsNonZeroReferenceTypes()) {
4261 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
4262 << "invoke-polymorphic receiver is not a reference: "
4263 << this_type;
4264 return false;
4265 } else if (this_type.IsUninitializedReference()) {
4266 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
4267 << "invoke-polymorphic receiver is uninitialized: "
4268 << this_type;
4269 return false;
4270 } else if (!this_type.HasClass()) {
4271 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
4272 << "invoke-polymorphic receiver has no class: "
4273 << this_type;
4274 return false;
Orion Hodsonfe92d122018-01-02 10:45:17 +00004275 } else if (!this_type.GetClass()->IsSubClass(mirror::MethodHandle::StaticClass()) &&
4276 !this_type.GetClass()->IsSubClass(mirror::VarHandle::StaticClass())) {
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004277 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
Orion Hodsonfe92d122018-01-02 10:45:17 +00004278 << "invoke-polymorphic receiver is not a subclass of MethodHandle or VarHandle: "
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004279 << this_type;
4280 return false;
4281 }
4282 return true;
Ian Rogersd81871c2011-10-03 13:57:23 -07004283}
4284
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00004285uint16_t MethodVerifier::GetMethodIdxOfInvoke(const Instruction* inst) {
4286 switch (inst->Opcode()) {
4287 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK:
4288 case Instruction::INVOKE_VIRTUAL_QUICK: {
4289 DCHECK(Runtime::Current()->IsStarted() || verify_to_dump_)
4290 << dex_file_->PrettyMethod(dex_method_idx_, true) << "@" << work_insn_idx_;
4291 DCHECK(method_being_verified_ != nullptr);
4292 uint16_t method_idx = method_being_verified_->GetIndexFromQuickening(work_insn_idx_);
4293 CHECK_NE(method_idx, DexFile::kDexNoIndex16);
4294 return method_idx;
4295 }
4296 default: {
4297 return inst->VRegB();
4298 }
Mathieu Chartier091d2382015-03-06 10:59:06 -08004299 }
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004300}
4301
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00004302uint16_t MethodVerifier::GetFieldIdxOfFieldAccess(const Instruction* inst, bool is_static) {
4303 if (is_static) {
4304 return inst->VRegB_21c();
4305 } else if (inst->IsQuickened()) {
4306 DCHECK(Runtime::Current()->IsStarted() || verify_to_dump_);
4307 DCHECK(method_being_verified_ != nullptr);
4308 uint16_t field_idx = method_being_verified_->GetIndexFromQuickening(work_insn_idx_);
4309 CHECK_NE(field_idx, DexFile::kDexNoIndex16);
4310 return field_idx;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004311 } else {
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00004312 return inst->VRegC_22c();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004313 }
4314}
4315
Ian Rogers62342ec2013-06-11 10:26:37 -07004316void MethodVerifier::VerifyNewArray(const Instruction* inst, bool is_filled, bool is_range) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08004317 dex::TypeIndex type_idx;
Sebastien Hertz5243e912013-05-21 10:55:07 +02004318 if (!is_filled) {
4319 DCHECK_EQ(inst->Opcode(), Instruction::NEW_ARRAY);
Andreas Gampea5b09a62016-11-17 15:21:22 -08004320 type_idx = dex::TypeIndex(inst->VRegC_22c());
Sebastien Hertz5243e912013-05-21 10:55:07 +02004321 } else if (!is_range) {
4322 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY);
Andreas Gampea5b09a62016-11-17 15:21:22 -08004323 type_idx = dex::TypeIndex(inst->VRegB_35c());
Sebastien Hertz5243e912013-05-21 10:55:07 +02004324 } else {
4325 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY_RANGE);
Andreas Gampea5b09a62016-11-17 15:21:22 -08004326 type_idx = dex::TypeIndex(inst->VRegB_3rc());
Sebastien Hertz5243e912013-05-21 10:55:07 +02004327 }
Andreas Gampe98be1a92017-08-28 08:25:45 -07004328 const RegType& res_type = ResolveClass<CheckAccess::kYes>(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004329 if (res_type.IsConflict()) { // bad class
4330 DCHECK_NE(failures_.size(), 0U);
Ian Rogers0c4a5062012-02-03 15:18:59 -08004331 } else {
4332 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
4333 if (!res_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07004334 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "new-array on non-array class " << res_type;
Ian Rogers0c4a5062012-02-03 15:18:59 -08004335 } else if (!is_filled) {
4336 /* make sure "size" register is valid type */
Ian Rogers7b078e82014-09-10 14:44:24 -07004337 work_line_->VerifyRegisterType(this, inst->VRegB_22c(), reg_types_.Integer());
Ian Rogers0c4a5062012-02-03 15:18:59 -08004338 /* set register type to array class */
Ian Rogersd8f69b02014-09-10 21:43:52 +00004339 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
Andreas Gampead238ce2015-08-24 21:13:08 -07004340 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_22c(), precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08004341 } else {
Andreas Gampebb18a032016-03-22 20:34:25 -07004342 DCHECK(!res_type.IsUnresolvedMergedReference());
Ian Rogers0c4a5062012-02-03 15:18:59 -08004343 // Verify each register. If "arg_count" is bad, VerifyRegisterType() will run off the end of
4344 // the list and fail. It's legal, if silly, for arg_count to be zero.
Ian Rogersd8f69b02014-09-10 21:43:52 +00004345 const RegType& expected_type = reg_types_.GetComponentType(res_type, GetClassLoader());
Sebastien Hertz5243e912013-05-21 10:55:07 +02004346 uint32_t arg_count = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
4347 uint32_t arg[5];
4348 if (!is_range) {
Ian Rogers29a26482014-05-02 15:27:29 -07004349 inst->GetVarArgs(arg);
Sebastien Hertz5243e912013-05-21 10:55:07 +02004350 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08004351 for (size_t ui = 0; ui < arg_count; ui++) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02004352 uint32_t get_reg = is_range ? inst->VRegC_3rc() + ui : arg[ui];
Ian Rogers7b078e82014-09-10 14:44:24 -07004353 if (!work_line_->VerifyRegisterType(this, get_reg, expected_type)) {
4354 work_line_->SetResultRegisterType(this, reg_types_.Conflict());
Ian Rogers0c4a5062012-02-03 15:18:59 -08004355 return;
4356 }
4357 }
4358 // filled-array result goes into "result" register
Ian Rogersd8f69b02014-09-10 21:43:52 +00004359 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
Ian Rogers7b078e82014-09-10 14:44:24 -07004360 work_line_->SetResultRegisterType(this, precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08004361 }
4362 }
4363}
4364
Sebastien Hertz5243e912013-05-21 10:55:07 +02004365void MethodVerifier::VerifyAGet(const Instruction* inst,
Ian Rogersd8f69b02014-09-10 21:43:52 +00004366 const RegType& insn_type, bool is_primitive) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004367 const RegType& index_type = work_line_->GetRegisterType(this, inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07004368 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07004369 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07004370 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004371 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegB_23x());
Andreas Gampeeed3a5d2017-11-29 14:58:34 -08004372 if (array_type.IsZeroOrNull()) {
Ian Rogers89310de2012-02-01 13:47:30 -08004373 // Null array class; this code path will fail at runtime. Infer a merge-able type from the
Andreas Gampe52f205a2017-12-01 12:16:07 -08004374 // instruction type.
4375 if (!is_primitive) {
4376 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), reg_types_.Null());
4377 } else if (insn_type.IsInteger()) {
4378 // Pick a non-zero constant (to distinguish with null) that can fit in any primitive.
4379 // We cannot use 'insn_type' as it could be a float array or an int array.
4380 work_line_->SetRegisterType<LockOp::kClear>(
4381 this, inst->VRegA_23x(), DetermineCat1Constant(1, need_precise_constants_));
4382 } else if (insn_type.IsCategory1Types()) {
4383 // Category 1
4384 // The 'insn_type' is exactly the type we need.
4385 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), insn_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07004386 } else {
Ian Rogers89310de2012-02-01 13:47:30 -08004387 // Category 2
Ian Rogers7b078e82014-09-10 14:44:24 -07004388 work_line_->SetRegisterTypeWide(this, inst->VRegA_23x(),
4389 reg_types_.FromCat2ConstLo(0, false),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004390 reg_types_.FromCat2ConstHi(0, false));
Ian Rogers89310de2012-02-01 13:47:30 -08004391 }
jeffhaofc3144e2012-02-01 17:21:15 -08004392 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07004393 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aget";
Andreas Gampe8d8fc482016-03-25 16:24:20 -07004394 } else if (array_type.IsUnresolvedMergedReference()) {
Andreas Gampebb18a032016-03-22 20:34:25 -07004395 // Unresolved array types must be reference array types.
4396 if (is_primitive) {
4397 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type
4398 << " source for category 1 aget";
4399 } else {
4400 Fail(VERIFY_ERROR_NO_CLASS) << "cannot verify aget for " << array_type
4401 << " because of missing class";
Andreas Gampe8d8fc482016-03-25 16:24:20 -07004402 // Approximate with java.lang.Object[].
4403 work_line_->SetRegisterType<LockOp::kClear>(this,
4404 inst->VRegA_23x(),
4405 reg_types_.JavaLangObject(false));
Andreas Gampebb18a032016-03-22 20:34:25 -07004406 }
Ian Rogers89310de2012-02-01 13:47:30 -08004407 } else {
4408 /* verify the class */
Ian Rogersd8f69b02014-09-10 21:43:52 +00004409 const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
jeffhaofc3144e2012-02-01 17:21:15 -08004410 if (!component_type.IsReferenceTypes() && !is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07004411 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08004412 << " source for aget-object";
4413 } else if (component_type.IsNonZeroReferenceTypes() && is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07004414 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08004415 << " source for category 1 aget";
4416 } else if (is_primitive && !insn_type.Equals(component_type) &&
4417 !((insn_type.IsInteger() && component_type.IsFloat()) ||
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004418 (insn_type.IsLong() && component_type.IsDouble()))) {
4419 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array type " << array_type
4420 << " incompatible with aget of type " << insn_type;
Ian Rogers89310de2012-02-01 13:47:30 -08004421 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004422 // Use knowledge of the field type which is stronger than the type inferred from the
4423 // instruction, which can't differentiate object types and ints from floats, longs from
4424 // doubles.
4425 if (!component_type.IsLowHalf()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07004426 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), component_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004427 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004428 work_line_->SetRegisterTypeWide(this, inst->VRegA_23x(), component_type,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004429 component_type.HighHalf(&reg_types_));
4430 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004431 }
4432 }
4433 }
4434}
4435
Ian Rogersd8f69b02014-09-10 21:43:52 +00004436void MethodVerifier::VerifyPrimitivePut(const RegType& target_type, const RegType& insn_type,
Jeff Haofe1f7c82013-08-01 14:50:24 -07004437 const uint32_t vregA) {
4438 // Primitive assignability rules are weaker than regular assignability rules.
4439 bool instruction_compatible;
4440 bool value_compatible;
Ian Rogers7b078e82014-09-10 14:44:24 -07004441 const RegType& value_type = work_line_->GetRegisterType(this, vregA);
Jeff Haofe1f7c82013-08-01 14:50:24 -07004442 if (target_type.IsIntegralTypes()) {
Jeff Haoa4647482013-08-06 15:35:47 -07004443 instruction_compatible = target_type.Equals(insn_type);
Jeff Haofe1f7c82013-08-01 14:50:24 -07004444 value_compatible = value_type.IsIntegralTypes();
4445 } else if (target_type.IsFloat()) {
4446 instruction_compatible = insn_type.IsInteger(); // no put-float, so expect put-int
4447 value_compatible = value_type.IsFloatTypes();
4448 } else if (target_type.IsLong()) {
4449 instruction_compatible = insn_type.IsLong();
Andreas Gampe376fa682014-09-07 13:06:12 -07004450 // Additional register check: this is not checked statically (as part of VerifyInstructions),
4451 // as target_type depends on the resolved type of the field.
4452 if (instruction_compatible && work_line_->NumRegs() > vregA + 1) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004453 const RegType& value_type_hi = work_line_->GetRegisterType(this, vregA + 1);
Andreas Gampe376fa682014-09-07 13:06:12 -07004454 value_compatible = value_type.IsLongTypes() && value_type.CheckWidePair(value_type_hi);
4455 } else {
4456 value_compatible = false;
4457 }
Jeff Haofe1f7c82013-08-01 14:50:24 -07004458 } else if (target_type.IsDouble()) {
4459 instruction_compatible = insn_type.IsLong(); // no put-double, so expect put-long
Andreas Gampe376fa682014-09-07 13:06:12 -07004460 // Additional register check: this is not checked statically (as part of VerifyInstructions),
4461 // as target_type depends on the resolved type of the field.
4462 if (instruction_compatible && work_line_->NumRegs() > vregA + 1) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004463 const RegType& value_type_hi = work_line_->GetRegisterType(this, vregA + 1);
Andreas Gampe376fa682014-09-07 13:06:12 -07004464 value_compatible = value_type.IsDoubleTypes() && value_type.CheckWidePair(value_type_hi);
4465 } else {
4466 value_compatible = false;
4467 }
Jeff Haofe1f7c82013-08-01 14:50:24 -07004468 } else {
4469 instruction_compatible = false; // reference with primitive store
4470 value_compatible = false; // unused
4471 }
4472 if (!instruction_compatible) {
4473 // This is a global failure rather than a class change failure as the instructions and
4474 // the descriptors for the type should have been consistent within the same file at
4475 // compile time.
4476 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "put insn has type '" << insn_type
4477 << "' but expected type '" << target_type << "'";
4478 return;
4479 }
4480 if (!value_compatible) {
4481 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
4482 << " of type " << value_type << " but expected " << target_type << " for put";
4483 return;
4484 }
4485}
4486
Sebastien Hertz5243e912013-05-21 10:55:07 +02004487void MethodVerifier::VerifyAPut(const Instruction* inst,
Ian Rogersd8f69b02014-09-10 21:43:52 +00004488 const RegType& insn_type, bool is_primitive) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004489 const RegType& index_type = work_line_->GetRegisterType(this, inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07004490 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07004491 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07004492 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004493 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegB_23x());
Andreas Gampeeed3a5d2017-11-29 14:58:34 -08004494 if (array_type.IsZeroOrNull()) {
Nicolas Geoffray66389fb2015-06-19 10:35:42 +01004495 // Null array type; this code path will fail at runtime.
4496 // Still check that the given value matches the instruction's type.
Andreas Gampe4bf4c782015-08-14 14:07:43 -07004497 // Note: this is, as usual, complicated by the fact the the instruction isn't fully typed
4498 // and fits multiple register types.
4499 const RegType* modified_reg_type = &insn_type;
4500 if ((modified_reg_type == &reg_types_.Integer()) ||
4501 (modified_reg_type == &reg_types_.LongLo())) {
4502 // May be integer or float | long or double. Overwrite insn_type accordingly.
4503 const RegType& value_type = work_line_->GetRegisterType(this, inst->VRegA_23x());
4504 if (modified_reg_type == &reg_types_.Integer()) {
4505 if (&value_type == &reg_types_.Float()) {
4506 modified_reg_type = &value_type;
4507 }
4508 } else {
4509 if (&value_type == &reg_types_.DoubleLo()) {
4510 modified_reg_type = &value_type;
4511 }
4512 }
4513 }
4514 work_line_->VerifyRegisterType(this, inst->VRegA_23x(), *modified_reg_type);
jeffhaofc3144e2012-02-01 17:21:15 -08004515 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07004516 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aput";
Andreas Gampe8d8fc482016-03-25 16:24:20 -07004517 } else if (array_type.IsUnresolvedMergedReference()) {
Andreas Gampebb18a032016-03-22 20:34:25 -07004518 // Unresolved array types must be reference array types.
4519 if (is_primitive) {
4520 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "put insn has type '" << insn_type
4521 << "' but unresolved type '" << array_type << "'";
4522 } else {
4523 Fail(VERIFY_ERROR_NO_CLASS) << "cannot verify aput for " << array_type
4524 << " because of missing class";
4525 }
Ian Rogers89310de2012-02-01 13:47:30 -08004526 } else {
Ian Rogersd8f69b02014-09-10 21:43:52 +00004527 const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
Jeff Haofe1f7c82013-08-01 14:50:24 -07004528 const uint32_t vregA = inst->VRegA_23x();
Jeff Haob24b4a72013-07-31 13:47:31 -07004529 if (is_primitive) {
Jeff Haofe1f7c82013-08-01 14:50:24 -07004530 VerifyPrimitivePut(component_type, insn_type, vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07004531 } else {
Jeff Haob24b4a72013-07-31 13:47:31 -07004532 if (!component_type.IsReferenceTypes()) {
4533 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
4534 << " source for aput-object";
4535 } else {
4536 // The instruction agrees with the type of array, confirm the value to be stored does too
4537 // Note: we use the instruction type (rather than the component type) for aput-object as
4538 // incompatible classes will be caught at runtime as an array store exception
Ian Rogers7b078e82014-09-10 14:44:24 -07004539 work_line_->VerifyRegisterType(this, vregA, insn_type);
Jeff Haob24b4a72013-07-31 13:47:31 -07004540 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004541 }
4542 }
4543 }
4544}
4545
Mathieu Chartierc7853442015-03-27 14:35:38 -07004546ArtField* MethodVerifier::GetStaticField(int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08004547 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
4548 // Check access to class
Andreas Gampe98be1a92017-08-28 08:25:45 -07004549 const RegType& klass_type = ResolveClass<CheckAccess::kYes>(field_id.class_idx_);
Brian Carlstrom7934ac22013-07-26 10:54:15 -07004550 if (klass_type.IsConflict()) { // bad class
Ian Rogersad0b3a32012-04-16 14:50:24 -07004551 AppendToLastFailMessage(StringPrintf(" in attempt to access static field %d (%s) in %s",
4552 field_idx, dex_file_->GetFieldName(field_id),
4553 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers7b078e82014-09-10 14:44:24 -07004554 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08004555 }
Elliott Hughesb25c3f62012-03-26 16:35:06 -07004556 if (klass_type.IsUnresolvedTypes()) {
Andreas Gampe629be512017-08-25 17:09:32 -07004557 // Accessibility checks depend on resolved fields.
4558 DCHECK(klass_type.Equals(GetDeclaringClass()) || !failures_.empty());
4559
Ian Rogers7b078e82014-09-10 14:44:24 -07004560 return nullptr; // Can't resolve Class so no more to do here, will do checking at runtime.
Ian Rogers90040192011-12-16 08:54:29 -08004561 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07004562 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Vladimir Markoe11dd502017-12-08 14:09:45 +00004563 ArtField* field = class_linker->ResolveFieldJLS(field_idx, dex_cache_, class_loader_);
David Brazdilca3c8c32016-09-06 14:04:48 +01004564
4565 // Record result of the field resolution attempt.
4566 VerifierDeps::MaybeRecordFieldResolution(*dex_file_, field_idx, field);
4567
Ian Rogers7b078e82014-09-10 14:44:24 -07004568 if (field == nullptr) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07004569 VLOG(verifier) << "Unable to resolve static field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07004570 << dex_file_->GetFieldName(field_id) << ") in "
4571 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogers7b078e82014-09-10 14:44:24 -07004572 DCHECK(self_->IsExceptionPending());
4573 self_->ClearException();
4574 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004575 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
4576 field->GetAccessFlags())) {
David Sehr709b0702016-10-13 09:12:37 -07004577 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access static field " << field->PrettyField()
Ian Rogersad0b3a32012-04-16 14:50:24 -07004578 << " from " << GetDeclaringClass();
Ian Rogers7b078e82014-09-10 14:44:24 -07004579 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07004580 } else if (!field->IsStatic()) {
David Sehr709b0702016-10-13 09:12:37 -07004581 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << field->PrettyField() << " to be static";
Ian Rogers7b078e82014-09-10 14:44:24 -07004582 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07004583 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07004584 return field;
Ian Rogersd81871c2011-10-03 13:57:23 -07004585}
4586
Mathieu Chartierc7853442015-03-27 14:35:38 -07004587ArtField* MethodVerifier::GetInstanceField(const RegType& obj_type, int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08004588 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
Aart Bik31883642016-06-06 15:02:44 -07004589 // Check access to class.
Andreas Gampe98be1a92017-08-28 08:25:45 -07004590 const RegType& klass_type = ResolveClass<CheckAccess::kYes>(field_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004591 if (klass_type.IsConflict()) {
4592 AppendToLastFailMessage(StringPrintf(" in attempt to access instance field %d (%s) in %s",
4593 field_idx, dex_file_->GetFieldName(field_id),
4594 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers7b078e82014-09-10 14:44:24 -07004595 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08004596 }
jeffhao8cd6dda2012-02-22 10:15:34 -08004597 if (klass_type.IsUnresolvedTypes()) {
Andreas Gampe629be512017-08-25 17:09:32 -07004598 // Accessibility checks depend on resolved fields.
4599 DCHECK(klass_type.Equals(GetDeclaringClass()) || !failures_.empty());
4600
Ian Rogers7b078e82014-09-10 14:44:24 -07004601 return nullptr; // Can't resolve Class so no more to do here
Ian Rogers90040192011-12-16 08:54:29 -08004602 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07004603 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Vladimir Markoe11dd502017-12-08 14:09:45 +00004604 ArtField* field = class_linker->ResolveFieldJLS(field_idx, dex_cache_, class_loader_);
David Brazdilca3c8c32016-09-06 14:04:48 +01004605
4606 // Record result of the field resolution attempt.
4607 VerifierDeps::MaybeRecordFieldResolution(*dex_file_, field_idx, field);
4608
Ian Rogers7b078e82014-09-10 14:44:24 -07004609 if (field == nullptr) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07004610 VLOG(verifier) << "Unable to resolve instance field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07004611 << dex_file_->GetFieldName(field_id) << ") in "
4612 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogers7b078e82014-09-10 14:44:24 -07004613 DCHECK(self_->IsExceptionPending());
4614 self_->ClearException();
4615 return nullptr;
Andreas Gampeeed3a5d2017-11-29 14:58:34 -08004616 } else if (obj_type.IsZeroOrNull()) {
Aart Bik31883642016-06-06 15:02:44 -07004617 // Cannot infer and check type, however, access will cause null pointer exception.
4618 // Fall through into a few last soft failure checks below.
Stephen Kyle695c5982014-08-22 15:03:07 +01004619 } else if (!obj_type.IsReferenceTypes()) {
Aart Bik31883642016-06-06 15:02:44 -07004620 // Trying to read a field from something that isn't a reference.
Stephen Kyle695c5982014-08-22 15:03:07 +01004621 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance field access on object that has "
4622 << "non-reference type " << obj_type;
Ian Rogers7b078e82014-09-10 14:44:24 -07004623 return nullptr;
Ian Rogerse1758fe2012-04-19 11:31:15 -07004624 } else {
David Brazdil0d638bb2016-07-27 15:29:25 +01004625 std::string temp;
Mathieu Chartier3398c782016-09-30 10:27:43 -07004626 ObjPtr<mirror::Class> klass = field->GetDeclaringClass();
Ian Rogersd8f69b02014-09-10 21:43:52 +00004627 const RegType& field_klass =
Mathieu Chartier3398c782016-09-30 10:27:43 -07004628 FromClass(klass->GetDescriptor(&temp),
Mathieu Chartier1cc62e42016-10-03 18:01:28 -07004629 klass.Ptr(),
Mathieu Chartier3398c782016-09-30 10:27:43 -07004630 klass->CannotBeAssignedFromOtherTypes());
David Brazdil68b5c0b2016-01-19 14:25:29 +00004631 if (obj_type.IsUninitializedTypes()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07004632 // Field accesses through uninitialized references are only allowable for constructors where
David Brazdil68b5c0b2016-01-19 14:25:29 +00004633 // the field is declared in this class.
4634 // Note: this IsConstructor check is technically redundant, as UninitializedThis should only
4635 // appear in constructors.
4636 if (!obj_type.IsUninitializedThisReference() ||
4637 !IsConstructor() ||
4638 !field_klass.Equals(GetDeclaringClass())) {
David Sehr709b0702016-10-13 09:12:37 -07004639 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "cannot access instance field " << field->PrettyField()
David Brazdil68b5c0b2016-01-19 14:25:29 +00004640 << " of a not fully initialized object within the context"
David Sehr709b0702016-10-13 09:12:37 -07004641 << " of " << dex_file_->PrettyMethod(dex_method_idx_);
David Brazdil68b5c0b2016-01-19 14:25:29 +00004642 return nullptr;
4643 }
David Brazdilca3c8c32016-09-06 14:04:48 +01004644 } else if (!field_klass.IsAssignableFrom(obj_type, this)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07004645 // Trying to access C1.field1 using reference of type C2, which is neither C1 or a sub-class
4646 // of C1. For resolution to occur the declared class of the field must be compatible with
4647 // obj_type, we've discovered this wasn't so, so report the field didn't exist.
Andreas Gampe66596242016-04-14 10:55:04 -07004648 VerifyError type;
4649 bool is_aot = Runtime::Current()->IsAotCompiler();
4650 if (is_aot && (field_klass.IsUnresolvedTypes() || obj_type.IsUnresolvedTypes())) {
4651 // Compiler & unresolved types involved, retry at runtime.
4652 type = VerifyError::VERIFY_ERROR_NO_CLASS;
4653 } else {
Andreas Gampe8f4ade02016-04-15 10:09:16 -07004654 // Classes known (resolved; and thus assignability check is precise), or we are at runtime
4655 // and still missing classes. This is a hard failure.
Andreas Gampe66596242016-04-14 10:55:04 -07004656 type = VerifyError::VERIFY_ERROR_BAD_CLASS_HARD;
4657 }
David Sehr709b0702016-10-13 09:12:37 -07004658 Fail(type) << "cannot access instance field " << field->PrettyField()
Andreas Gampe66596242016-04-14 10:55:04 -07004659 << " from object of type " << obj_type;
Ian Rogers7b078e82014-09-10 14:44:24 -07004660 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004661 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004662 }
Aart Bik31883642016-06-06 15:02:44 -07004663
4664 // Few last soft failure checks.
4665 if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
4666 field->GetAccessFlags())) {
David Sehr709b0702016-10-13 09:12:37 -07004667 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access instance field " << field->PrettyField()
Aart Bik31883642016-06-06 15:02:44 -07004668 << " from " << GetDeclaringClass();
4669 return nullptr;
4670 } else if (field->IsStatic()) {
David Sehr709b0702016-10-13 09:12:37 -07004671 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << field->PrettyField()
Aart Bik31883642016-06-06 15:02:44 -07004672 << " to not be static";
4673 return nullptr;
4674 }
4675
4676 return field;
Ian Rogersd81871c2011-10-03 13:57:23 -07004677}
4678
Andreas Gampe896df402014-10-20 22:25:29 -07004679template <MethodVerifier::FieldAccessType kAccType>
4680void MethodVerifier::VerifyISFieldAccess(const Instruction* inst, const RegType& insn_type,
4681 bool is_primitive, bool is_static) {
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00004682 uint32_t field_idx = GetFieldIdxOfFieldAccess(inst, is_static);
Mathieu Chartierc7853442015-03-27 14:35:38 -07004683 ArtField* field;
Ian Rogersb94a27b2011-10-26 00:33:41 -07004684 if (is_static) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07004685 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07004686 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004687 const RegType& object_type = work_line_->GetRegisterType(this, inst->VRegB_22c());
David Brazdil68b5c0b2016-01-19 14:25:29 +00004688
4689 // One is not allowed to access fields on uninitialized references, except to write to
4690 // fields in the constructor (before calling another constructor).
4691 // GetInstanceField does an assignability check which will fail for uninitialized types.
4692 // We thus modify the type if the uninitialized reference is a "this" reference (this also
4693 // checks at the same time that we're verifying a constructor).
4694 bool should_adjust = (kAccType == FieldAccessType::kAccPut) &&
4695 object_type.IsUninitializedThisReference();
4696 const RegType& adjusted_type = should_adjust
4697 ? GetRegTypeCache()->FromUninitialized(object_type)
4698 : object_type;
4699 field = GetInstanceField(adjusted_type, field_idx);
Andreas Gampe896df402014-10-20 22:25:29 -07004700 if (UNLIKELY(have_pending_hard_failure_)) {
4701 return;
4702 }
Alex Light4a2c8fc2016-02-12 11:01:54 -08004703 if (should_adjust) {
4704 if (field == nullptr) {
4705 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "Might be accessing a superclass instance field prior "
4706 << "to the superclass being initialized in "
David Sehr709b0702016-10-13 09:12:37 -07004707 << dex_file_->PrettyMethod(dex_method_idx_);
Alex Light4a2c8fc2016-02-12 11:01:54 -08004708 } else if (field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
4709 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "cannot access superclass instance field "
David Sehr709b0702016-10-13 09:12:37 -07004710 << field->PrettyField() << " of a not fully initialized "
Alex Light4a2c8fc2016-02-12 11:01:54 -08004711 << "object within the context of "
David Sehr709b0702016-10-13 09:12:37 -07004712 << dex_file_->PrettyMethod(dex_method_idx_);
Alex Light4a2c8fc2016-02-12 11:01:54 -08004713 return;
4714 }
4715 }
Ian Rogersb94a27b2011-10-26 00:33:41 -07004716 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00004717 const RegType* field_type = nullptr;
Ian Rogers7b078e82014-09-10 14:44:24 -07004718 if (field != nullptr) {
Andreas Gampe896df402014-10-20 22:25:29 -07004719 if (kAccType == FieldAccessType::kAccPut) {
4720 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
David Sehr709b0702016-10-13 09:12:37 -07004721 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << field->PrettyField()
Andreas Gampe896df402014-10-20 22:25:29 -07004722 << " from other class " << GetDeclaringClass();
Aart Bikc2bc2652016-05-23 14:58:49 -07004723 // Keep hunting for possible hard fails.
Andreas Gampe896df402014-10-20 22:25:29 -07004724 }
4725 }
4726
Mathieu Chartier3398c782016-09-30 10:27:43 -07004727 ObjPtr<mirror::Class> field_type_class =
Vladimir Marko208f6702017-12-08 12:00:50 +00004728 can_load_classes_ ? field->ResolveType() : field->LookupResolvedType();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004729 if (field_type_class != nullptr) {
Mathieu Chartier3398c782016-09-30 10:27:43 -07004730 field_type = &FromClass(field->GetTypeDescriptor(),
Mathieu Chartier1cc62e42016-10-03 18:01:28 -07004731 field_type_class.Ptr(),
Andreas Gampef23f33d2015-06-23 14:18:17 -07004732 field_type_class->CannotBeAssignedFromOtherTypes());
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08004733 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004734 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
4735 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004736 }
Andreas Gampe7da4c402017-08-25 11:30:48 -07004737 } else {
4738 // If we don't have the field (it seems we failed resolution) and this is a PUT, we need to
4739 // redo verification at runtime as the field may be final, unless the field id shows it's in
4740 // the same class.
4741 //
4742 // For simplicity, it is OK to not distinguish compile-time vs runtime, and post this an
4743 // ACCESS_FIELD failure at runtime. This has the same effect as NO_FIELD - punting the class
4744 // to the access-checks interpreter.
4745 //
4746 // Note: see b/34966607. This and above may be changed in the future.
4747 if (kAccType == FieldAccessType::kAccPut) {
4748 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
4749 const char* field_class_descriptor = dex_file_->GetFieldDeclaringClassDescriptor(field_id);
4750 const RegType* field_class_type = &reg_types_.FromDescriptor(GetClassLoader(),
4751 field_class_descriptor,
4752 false);
4753 if (!field_class_type->Equals(GetDeclaringClass())) {
4754 Fail(VERIFY_ERROR_ACCESS_FIELD) << "could not check field put for final field modify of "
4755 << field_class_descriptor
4756 << "."
4757 << dex_file_->GetFieldName(field_id)
4758 << " from other class "
4759 << GetDeclaringClass();
4760 }
4761 }
Ian Rogers0d604842012-04-16 14:50:24 -07004762 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004763 if (field_type == nullptr) {
4764 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
4765 const char* descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
Mathieu Chartierbf99f772014-08-23 16:37:27 -07004766 field_type = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004767 }
Sebastien Hertz757b3042014-03-28 14:34:28 +01004768 DCHECK(field_type != nullptr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02004769 const uint32_t vregA = (is_static) ? inst->VRegA_21c() : inst->VRegA_22c();
Andreas Gampe896df402014-10-20 22:25:29 -07004770 static_assert(kAccType == FieldAccessType::kAccPut || kAccType == FieldAccessType::kAccGet,
4771 "Unexpected third access type");
4772 if (kAccType == FieldAccessType::kAccPut) {
4773 // sput or iput.
4774 if (is_primitive) {
4775 VerifyPrimitivePut(*field_type, insn_type, vregA);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004776 } else {
David Brazdilca3c8c32016-09-06 14:04:48 +01004777 if (!insn_type.IsAssignableFrom(*field_type, this)) {
Vladimir Marko414000e2015-06-23 17:45:21 +01004778 // If the field type is not a reference, this is a global failure rather than
4779 // a class change failure as the instructions and the descriptors for the type
4780 // should have been consistent within the same file at compile time.
4781 VerifyError error = field_type->IsReferenceTypes() ? VERIFY_ERROR_BAD_CLASS_SOFT
4782 : VERIFY_ERROR_BAD_CLASS_HARD;
David Sehr709b0702016-10-13 09:12:37 -07004783 Fail(error) << "expected field " << ArtField::PrettyField(field)
Vladimir Marko414000e2015-06-23 17:45:21 +01004784 << " to be compatible with type '" << insn_type
4785 << "' but found type '" << *field_type
4786 << "' in put-object";
Andreas Gampe896df402014-10-20 22:25:29 -07004787 return;
4788 }
4789 work_line_->VerifyRegisterType(this, vregA, *field_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004790 }
Andreas Gampe896df402014-10-20 22:25:29 -07004791 } else if (kAccType == FieldAccessType::kAccGet) {
4792 // sget or iget.
4793 if (is_primitive) {
4794 if (field_type->Equals(insn_type) ||
4795 (field_type->IsFloat() && insn_type.IsInteger()) ||
4796 (field_type->IsDouble() && insn_type.IsLong())) {
4797 // expected that read is of the correct primitive type or that int reads are reading
4798 // floats or long reads are reading doubles
4799 } else {
4800 // This is a global failure rather than a class change failure as the instructions and
4801 // the descriptors for the type should have been consistent within the same file at
4802 // compile time
David Sehr709b0702016-10-13 09:12:37 -07004803 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << ArtField::PrettyField(field)
Andreas Gampe896df402014-10-20 22:25:29 -07004804 << " to be of type '" << insn_type
4805 << "' but found type '" << *field_type << "' in get";
4806 return;
4807 }
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08004808 } else {
David Brazdilca3c8c32016-09-06 14:04:48 +01004809 if (!insn_type.IsAssignableFrom(*field_type, this)) {
Vladimir Marko414000e2015-06-23 17:45:21 +01004810 // If the field type is not a reference, this is a global failure rather than
4811 // a class change failure as the instructions and the descriptors for the type
4812 // should have been consistent within the same file at compile time.
4813 VerifyError error = field_type->IsReferenceTypes() ? VERIFY_ERROR_BAD_CLASS_SOFT
4814 : VERIFY_ERROR_BAD_CLASS_HARD;
David Sehr709b0702016-10-13 09:12:37 -07004815 Fail(error) << "expected field " << ArtField::PrettyField(field)
Vladimir Marko414000e2015-06-23 17:45:21 +01004816 << " to be compatible with type '" << insn_type
4817 << "' but found type '" << *field_type
4818 << "' in get-object";
Andreas Gampe890da292015-07-06 17:20:18 -07004819 if (error != VERIFY_ERROR_BAD_CLASS_HARD) {
Andreas Gampead238ce2015-08-24 21:13:08 -07004820 work_line_->SetRegisterType<LockOp::kClear>(this, vregA, reg_types_.Conflict());
Andreas Gampe890da292015-07-06 17:20:18 -07004821 }
Andreas Gampe896df402014-10-20 22:25:29 -07004822 return;
4823 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004824 }
Andreas Gampe896df402014-10-20 22:25:29 -07004825 if (!field_type->IsLowHalf()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07004826 work_line_->SetRegisterType<LockOp::kClear>(this, vregA, *field_type);
Andreas Gampe896df402014-10-20 22:25:29 -07004827 } else {
4828 work_line_->SetRegisterTypeWide(this, vregA, *field_type, field_type->HighHalf(&reg_types_));
4829 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07004830 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004831 LOG(FATAL) << "Unexpected case.";
Ian Rogersd81871c2011-10-03 13:57:23 -07004832 }
4833}
4834
Ian Rogers776ac1f2012-04-13 23:36:36 -07004835bool MethodVerifier::CheckNotMoveException(const uint16_t* insns, int insn_idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004836 if ((insns[insn_idx] & 0xff) == Instruction::MOVE_EXCEPTION) {
jeffhaod5347e02012-03-22 17:25:05 -07004837 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-exception";
Ian Rogersd81871c2011-10-03 13:57:23 -07004838 return false;
4839 }
4840 return true;
4841}
4842
Stephen Kyle9bc61992014-09-22 13:53:15 +01004843bool MethodVerifier::CheckNotMoveResult(const uint16_t* insns, int insn_idx) {
4844 if (((insns[insn_idx] & 0xff) >= Instruction::MOVE_RESULT) &&
4845 ((insns[insn_idx] & 0xff) <= Instruction::MOVE_RESULT_OBJECT)) {
4846 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-result*";
4847 return false;
4848 }
4849 return true;
4850}
4851
4852bool MethodVerifier::CheckNotMoveExceptionOrMoveResult(const uint16_t* insns, int insn_idx) {
4853 return (CheckNotMoveException(insns, insn_idx) && CheckNotMoveResult(insns, insn_idx));
4854}
4855
Ian Rogersebbdd872014-07-07 23:53:08 -07004856bool MethodVerifier::UpdateRegisters(uint32_t next_insn, RegisterLine* merge_line,
4857 bool update_merge_line) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004858 bool changed = true;
4859 RegisterLine* target_line = reg_table_.GetLine(next_insn);
Mathieu Chartierde40d472015-10-15 17:47:48 -07004860 if (!GetInstructionFlags(next_insn).IsVisitedOrChanged()) {
jeffhaobdb76512011-09-07 11:43:16 -07004861 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07004862 * We haven't processed this instruction before, and we haven't touched the registers here, so
4863 * there's nothing to "merge". Copy the registers over and mark it as changed. (This is the
4864 * only way a register can transition out of "unknown", so this is not just an optimization.)
jeffhaobdb76512011-09-07 11:43:16 -07004865 */
Andreas Gampea727e372015-08-25 09:22:37 -07004866 target_line->CopyFromLine(merge_line);
Mathieu Chartierde40d472015-10-15 17:47:48 -07004867 if (GetInstructionFlags(next_insn).IsReturn()) {
Jeff Haob24b4a72013-07-31 13:47:31 -07004868 // Verify that the monitor stack is empty on return.
Andreas Gampea727e372015-08-25 09:22:37 -07004869 merge_line->VerifyMonitorStackEmpty(this);
4870
Ian Rogersb8c78592013-07-25 23:52:52 +00004871 // For returns we only care about the operand to the return, all other registers are dead.
4872 // Initialize them as conflicts so they don't add to GC and deoptimization information.
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08004873 const Instruction* ret_inst = &code_item_accessor_.InstructionAt(next_insn);
Andreas Gampea727e372015-08-25 09:22:37 -07004874 AdjustReturnLine(this, ret_inst, target_line);
Aart Bik31883642016-06-06 15:02:44 -07004875 // Directly bail if a hard failure was found.
Aart Bikb0526322016-06-01 14:06:00 -07004876 if (have_pending_hard_failure_) {
4877 return false;
4878 }
Ian Rogersb8c78592013-07-25 23:52:52 +00004879 }
jeffhaobdb76512011-09-07 11:43:16 -07004880 } else {
Mathieu Chartier361e04a2016-02-16 14:06:35 -08004881 RegisterLineArenaUniquePtr copy;
Andreas Gampe92d77202017-12-06 20:49:00 -08004882 if (UNLIKELY(VLOG_IS_ON(verifier_debug))) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07004883 copy.reset(RegisterLine::Create(target_line->NumRegs(), this));
Ian Rogers7b0c5b42012-02-16 15:29:07 -08004884 copy->CopyFromLine(target_line);
4885 }
Ian Rogers7b078e82014-09-10 14:44:24 -07004886 changed = target_line->MergeRegisters(this, merge_line);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004887 if (have_pending_hard_failure_) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004888 return false;
jeffhaobdb76512011-09-07 11:43:16 -07004889 }
Andreas Gampe92d77202017-12-06 20:49:00 -08004890 if (UNLIKELY(VLOG_IS_ON(verifier_debug)) && changed) {
Elliott Hughes398f64b2012-03-26 18:05:48 -07004891 LogVerifyInfo() << "Merging at [" << reinterpret_cast<void*>(work_insn_idx_) << "]"
Elliott Hughesc073b072012-05-24 19:29:17 -07004892 << " to [" << reinterpret_cast<void*>(next_insn) << "]: " << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07004893 << copy->Dump(this) << " MERGE\n"
4894 << merge_line->Dump(this) << " ==\n"
4895 << target_line->Dump(this) << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07004896 }
Ian Rogersebbdd872014-07-07 23:53:08 -07004897 if (update_merge_line && changed) {
4898 merge_line->CopyFromLine(target_line);
4899 }
jeffhaobdb76512011-09-07 11:43:16 -07004900 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004901 if (changed) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07004902 GetInstructionFlags(next_insn).SetChanged();
jeffhaobdb76512011-09-07 11:43:16 -07004903 }
4904 return true;
4905}
4906
Ian Rogers7b3ddd22013-02-21 15:19:52 -08004907InstructionFlags* MethodVerifier::CurrentInsnFlags() {
Mathieu Chartierde40d472015-10-15 17:47:48 -07004908 return &GetInstructionFlags(work_insn_idx_);
Ian Rogers776ac1f2012-04-13 23:36:36 -07004909}
4910
Ian Rogersd8f69b02014-09-10 21:43:52 +00004911const RegType& MethodVerifier::GetMethodReturnType() {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004912 if (return_type_ == nullptr) {
Nicolas Geoffrayb041a402017-11-13 15:16:22 +00004913 if (method_being_verified_ != nullptr) {
Vladimir Markob45528c2017-07-27 14:14:28 +01004914 ObjPtr<mirror::Class> return_type_class = can_load_classes_
Nicolas Geoffrayb041a402017-11-13 15:16:22 +00004915 ? method_being_verified_->ResolveReturnType()
4916 : method_being_verified_->LookupResolvedReturnType();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004917 if (return_type_class != nullptr) {
Nicolas Geoffrayb041a402017-11-13 15:16:22 +00004918 return_type_ = &FromClass(method_being_verified_->GetReturnTypeDescriptor(),
Vladimir Markob45528c2017-07-27 14:14:28 +01004919 return_type_class.Ptr(),
Andreas Gampef23f33d2015-06-23 14:18:17 -07004920 return_type_class->CannotBeAssignedFromOtherTypes());
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004921 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004922 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
4923 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004924 }
4925 }
4926 if (return_type_ == nullptr) {
4927 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
4928 const DexFile::ProtoId& proto_id = dex_file_->GetMethodPrototype(method_id);
Andreas Gampea5b09a62016-11-17 15:21:22 -08004929 dex::TypeIndex return_type_idx = proto_id.return_type_idx_;
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004930 const char* descriptor = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(return_type_idx));
Mathieu Chartierbf99f772014-08-23 16:37:27 -07004931 return_type_ = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004932 }
4933 }
4934 return *return_type_;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004935}
4936
Ian Rogersd8f69b02014-09-10 21:43:52 +00004937const RegType& MethodVerifier::GetDeclaringClass() {
Ian Rogers7b078e82014-09-10 14:44:24 -07004938 if (declaring_class_ == nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004939 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
Brian Carlstrom93c33962013-07-26 10:37:43 -07004940 const char* descriptor
4941 = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(method_id.class_idx_));
Nicolas Geoffrayb041a402017-11-13 15:16:22 +00004942 if (method_being_verified_ != nullptr) {
4943 mirror::Class* klass = method_being_verified_->GetDeclaringClass();
Mathieu Chartierde40d472015-10-15 17:47:48 -07004944 declaring_class_ = &FromClass(descriptor, klass, klass->CannotBeAssignedFromOtherTypes());
Ian Rogers637c65b2013-05-31 11:46:00 -07004945 } else {
Mathieu Chartierbf99f772014-08-23 16:37:27 -07004946 declaring_class_ = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers637c65b2013-05-31 11:46:00 -07004947 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07004948 }
Ian Rogers637c65b2013-05-31 11:46:00 -07004949 return *declaring_class_;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004950}
4951
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004952std::vector<int32_t> MethodVerifier::DescribeVRegs(uint32_t dex_pc) {
4953 RegisterLine* line = reg_table_.GetLine(dex_pc);
Sebastien Hertzaa0c00c2014-03-14 17:58:54 +01004954 DCHECK(line != nullptr) << "No register line at DEX pc " << StringPrintf("0x%x", dex_pc);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004955 std::vector<int32_t> result;
4956 for (size_t i = 0; i < line->NumRegs(); ++i) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004957 const RegType& type = line->GetRegisterType(this, i);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004958 if (type.IsConstant()) {
4959 result.push_back(type.IsPreciseConstant() ? kConstant : kImpreciseConstant);
Ian Rogers7b078e82014-09-10 14:44:24 -07004960 const ConstantType* const_val = down_cast<const ConstantType*>(&type);
4961 result.push_back(const_val->ConstantValue());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004962 } else if (type.IsConstantLo()) {
4963 result.push_back(type.IsPreciseConstantLo() ? kConstant : kImpreciseConstant);
Ian Rogers7b078e82014-09-10 14:44:24 -07004964 const ConstantType* const_val = down_cast<const ConstantType*>(&type);
4965 result.push_back(const_val->ConstantValueLo());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004966 } else if (type.IsConstantHi()) {
4967 result.push_back(type.IsPreciseConstantHi() ? kConstant : kImpreciseConstant);
Ian Rogers7b078e82014-09-10 14:44:24 -07004968 const ConstantType* const_val = down_cast<const ConstantType*>(&type);
4969 result.push_back(const_val->ConstantValueHi());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004970 } else if (type.IsIntegralTypes()) {
4971 result.push_back(kIntVReg);
4972 result.push_back(0);
4973 } else if (type.IsFloat()) {
4974 result.push_back(kFloatVReg);
4975 result.push_back(0);
4976 } else if (type.IsLong()) {
4977 result.push_back(kLongLoVReg);
4978 result.push_back(0);
4979 result.push_back(kLongHiVReg);
4980 result.push_back(0);
4981 ++i;
4982 } else if (type.IsDouble()) {
4983 result.push_back(kDoubleLoVReg);
4984 result.push_back(0);
4985 result.push_back(kDoubleHiVReg);
4986 result.push_back(0);
4987 ++i;
4988 } else if (type.IsUndefined() || type.IsConflict() || type.IsHighHalf()) {
4989 result.push_back(kUndefined);
4990 result.push_back(0);
4991 } else {
Ian Rogers7b3ddd22013-02-21 15:19:52 -08004992 CHECK(type.IsNonZeroReferenceTypes());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004993 result.push_back(kReferenceVReg);
4994 result.push_back(0);
4995 }
4996 }
4997 return result;
4998}
4999
Ian Rogersd8f69b02014-09-10 21:43:52 +00005000const RegType& MethodVerifier::DetermineCat1Constant(int32_t value, bool precise) {
Sebastien Hertz849600b2013-12-20 10:28:08 +01005001 if (precise) {
5002 // Precise constant type.
5003 return reg_types_.FromCat1Const(value, true);
5004 } else {
5005 // Imprecise constant type.
5006 if (value < -32768) {
5007 return reg_types_.IntConstant();
5008 } else if (value < -128) {
5009 return reg_types_.ShortConstant();
5010 } else if (value < 0) {
5011 return reg_types_.ByteConstant();
5012 } else if (value == 0) {
5013 return reg_types_.Zero();
5014 } else if (value == 1) {
5015 return reg_types_.One();
5016 } else if (value < 128) {
5017 return reg_types_.PosByteConstant();
5018 } else if (value < 32768) {
5019 return reg_types_.PosShortConstant();
5020 } else if (value < 65536) {
5021 return reg_types_.CharConstant();
5022 } else {
5023 return reg_types_.IntConstant();
5024 }
5025 }
5026}
5027
Elliott Hughes0a1038b2012-06-14 16:24:17 -07005028void MethodVerifier::Init() {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08005029 art::verifier::RegTypeCache::Init();
Brian Carlstrome7d856b2012-01-11 18:10:55 -08005030}
5031
Elliott Hughes0a1038b2012-06-14 16:24:17 -07005032void MethodVerifier::Shutdown() {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08005033 verifier::RegTypeCache::ShutDown();
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08005034}
jeffhaod1224c72012-02-29 13:43:08 -08005035
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -07005036void MethodVerifier::VisitStaticRoots(RootVisitor* visitor) {
5037 RegTypeCache::VisitStaticRoots(visitor);
Mathieu Chartier7c438b12014-09-12 17:01:24 -07005038}
5039
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -07005040void MethodVerifier::VisitRoots(RootVisitor* visitor, const RootInfo& root_info) {
5041 reg_types_.VisitRoots(visitor, root_info);
Mathieu Chartierc528dba2013-11-26 12:00:11 -08005042}
5043
Andreas Gampef23f33d2015-06-23 14:18:17 -07005044const RegType& MethodVerifier::FromClass(const char* descriptor,
5045 mirror::Class* klass,
5046 bool precise) {
5047 DCHECK(klass != nullptr);
5048 if (precise && !klass->IsInstantiable() && !klass->IsPrimitive()) {
5049 Fail(VerifyError::VERIFY_ERROR_NO_CLASS) << "Could not create precise reference for "
5050 << "non-instantiable klass " << descriptor;
5051 precise = false;
5052 }
5053 return reg_types_.FromClass(descriptor, klass, precise);
5054}
5055
Ian Rogersd81871c2011-10-03 13:57:23 -07005056} // namespace verifier
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07005057} // namespace art