blob: f57f757f0bdc8a33623a3af00ef23f7c59dd1647 [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 Markoc7aa87e2018-05-24 15:19:52 +010036#include "class_root.h"
Vladimir Marko2b5eaa22013-12-13 13:59:30 +000037#include "compiler_callbacks.h"
David Sehrb2ec9f52018-02-21 13:20:31 -080038#include "dex/descriptors_names.h"
David Sehr9e734c72018-01-04 17:56:19 -080039#include "dex/dex_file-inl.h"
40#include "dex/dex_file_exception_helpers.h"
41#include "dex/dex_instruction-inl.h"
42#include "dex/dex_instruction_utils.h"
Alex Lighteb7c1442015-08-31 13:17:42 -070043#include "experimental_flags.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070044#include "gc/accounting/card_table-inl.h"
Andreas Gampe6d7abbd2017-04-24 13:19:09 -070045#include "handle_scope-inl.h"
Ian Rogers84fa0742011-10-25 18:13:30 -070046#include "intern_table.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080047#include "mirror/class-inl.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070048#include "mirror/class.h"
Ian Rogers39ebcb82013-05-30 16:57:23 -070049#include "mirror/dex_cache-inl.h"
Orion Hodsoncfa325e2016-10-13 10:25:54 +010050#include "mirror/method_handle_impl.h"
Orion Hodson2e599942017-09-22 16:17:41 +010051#include "mirror/method_type.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080052#include "mirror/object-inl.h"
53#include "mirror/object_array-inl.h"
Orion Hodsonfe92d122018-01-02 10:45:17 +000054#include "mirror/var_handle.h"
Ian Rogers7b078e82014-09-10 14:44:24 -070055#include "reg_type-inl.h"
Ian Rogers39ebcb82013-05-30 16:57:23 -070056#include "register_line-inl.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070057#include "runtime.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070058#include "scoped_thread_state_change-inl.h"
Andreas Gampe513061a2017-06-01 09:17:34 -070059#include "stack.h"
Nicolas Geoffrayb041a402017-11-13 15:16:22 +000060#include "vdex_file.h"
Andreas Gampe6d7abbd2017-04-24 13:19:09 -070061#include "verifier_compiler_binding.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070062#include "verifier_deps.h"
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070063
64namespace art {
Ian Rogersd81871c2011-10-03 13:57:23 -070065namespace verifier {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070066
Andreas Gampe46ee31b2016-12-14 10:11:49 -080067using android::base::StringPrintf;
68
Mathieu Chartier8e219ae2014-08-19 14:29:46 -070069static constexpr bool kTimeVerifyMethod = !kIsDebugBuild;
Ian Rogers2c8a8572011-10-24 17:11:36 -070070
Andreas Gampeebf850c2015-08-14 15:37:35 -070071// On VLOG(verifier), should we dump the whole state when we run into a hard failure?
72static constexpr bool kDumpRegLinesOnHardFailureIfVLOG = true;
73
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -080074// We print a warning blurb about "dx --no-optimize" when we find monitor-locking issues. Make
75// sure we only print this once.
76static bool gPrintedDxMonitorText = false;
77
Vladimir Marko69d310e2017-10-09 14:12:23 +010078PcToRegisterLineTable::PcToRegisterLineTable(ScopedArenaAllocator& allocator)
79 : register_lines_(allocator.Adapter(kArenaAllocVerifier)) {}
Mathieu Chartierde40d472015-10-15 17:47:48 -070080
Ian Rogers7b3ddd22013-02-21 15:19:52 -080081void PcToRegisterLineTable::Init(RegisterTrackingMode mode, InstructionFlags* flags,
Ian Rogersd81871c2011-10-03 13:57:23 -070082 uint32_t insns_size, uint16_t registers_size,
Ian Rogers776ac1f2012-04-13 23:36:36 -070083 MethodVerifier* verifier) {
Ian Rogersd81871c2011-10-03 13:57:23 -070084 DCHECK_GT(insns_size, 0U);
Mathieu Chartierde40d472015-10-15 17:47:48 -070085 register_lines_.resize(insns_size);
Ian Rogersd81871c2011-10-03 13:57:23 -070086 for (uint32_t i = 0; i < insns_size; i++) {
87 bool interesting = false;
88 switch (mode) {
89 case kTrackRegsAll:
90 interesting = flags[i].IsOpcode();
91 break;
Sameer Abu Asal02c42232013-04-30 12:09:45 -070092 case kTrackCompilerInterestPoints:
Brian Carlstrom02c8cc62013-07-18 15:54:44 -070093 interesting = flags[i].IsCompileTimeInfoPoint() || flags[i].IsBranchTarget();
Ian Rogersd81871c2011-10-03 13:57:23 -070094 break;
95 case kTrackRegsBranches:
96 interesting = flags[i].IsBranchTarget();
97 break;
98 default:
99 break;
100 }
101 if (interesting) {
Mathieu Chartierde40d472015-10-15 17:47:48 -0700102 register_lines_[i].reset(RegisterLine::Create(registers_size, verifier));
Ian Rogersd0fbd852013-09-24 18:17:04 -0700103 }
104 }
105}
106
Mathieu Chartierde40d472015-10-15 17:47:48 -0700107PcToRegisterLineTable::~PcToRegisterLineTable() {}
Ian Rogersd81871c2011-10-03 13:57:23 -0700108
Andreas Gampe7c038102014-10-27 20:08:46 -0700109// Note: returns true on failure.
Mathieu Chartierc2a2bda2018-03-12 11:21:52 -0700110inline bool MethodVerifier::FailOrAbort(bool condition,
111 const char* error_msg,
112 uint32_t work_insn_idx) {
Andreas Gampe7c038102014-10-27 20:08:46 -0700113 if (kIsDebugBuild) {
Andreas Gampef8f36c22016-11-19 14:51:52 -0800114 // In a debug build, abort if the error condition is wrong. Only warn if
115 // we are already aborting (as this verification is likely run to print
116 // lock information).
117 if (LIKELY(gAborting == 0)) {
Mathieu Chartierc2a2bda2018-03-12 11:21:52 -0700118 DCHECK(condition) << error_msg << work_insn_idx << " "
119 << dex_file_->PrettyMethod(dex_method_idx_);
Andreas Gampef8f36c22016-11-19 14:51:52 -0800120 } else {
121 if (!condition) {
122 LOG(ERROR) << error_msg << work_insn_idx;
Mathieu Chartierc2a2bda2018-03-12 11:21:52 -0700123 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << error_msg << work_insn_idx;
Andreas Gampef8f36c22016-11-19 14:51:52 -0800124 return true;
125 }
126 }
Andreas Gampe7c038102014-10-27 20:08:46 -0700127 } else {
128 // In a non-debug build, just fail the class.
129 if (!condition) {
Mathieu Chartierc2a2bda2018-03-12 11:21:52 -0700130 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << error_msg << work_insn_idx;
Andreas Gampe7c038102014-10-27 20:08:46 -0700131 return true;
132 }
133 }
134
135 return false;
136}
137
Stephen Kyle7e541c92014-12-17 17:10:02 +0000138static void SafelyMarkAllRegistersAsConflicts(MethodVerifier* verifier, RegisterLine* reg_line) {
Andreas Gampef10b6e12015-08-12 10:48:12 -0700139 if (verifier->IsInstanceConstructor()) {
Stephen Kyle7e541c92014-12-17 17:10:02 +0000140 // Before we mark all regs as conflicts, check that we don't have an uninitialized this.
141 reg_line->CheckConstructorReturn(verifier);
142 }
143 reg_line->MarkAllRegistersAsConflicts(verifier);
144}
145
Andreas Gampe6d7abbd2017-04-24 13:19:09 -0700146FailureKind MethodVerifier::VerifyClass(Thread* self,
Vladimir Markoa8bba7d2018-05-30 15:18:48 +0100147 ObjPtr<mirror::Class> klass,
Andreas Gampe6d7abbd2017-04-24 13:19:09 -0700148 CompilerCallbacks* callbacks,
149 bool allow_soft_failures,
150 HardFailLogMode log_level,
151 std::string* error) {
jeffhaobdb76512011-09-07 11:43:16 -0700152 if (klass->IsVerified()) {
Andreas Gampe6d7abbd2017-04-24 13:19:09 -0700153 return FailureKind::kNoFailure;
jeffhaobdb76512011-09-07 11:43:16 -0700154 }
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800155 bool early_failure = false;
156 std::string failure_message;
Mathieu Chartierf8322842014-05-16 10:59:25 -0700157 const DexFile& dex_file = klass->GetDexFile();
158 const DexFile::ClassDef* class_def = klass->GetClassDef();
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800159 mirror::Class* super = klass->GetSuperClass();
Ian Rogers1ff3c982014-08-12 02:30:58 -0700160 std::string temp;
Ian Rogers7b078e82014-09-10 14:44:24 -0700161 if (super == nullptr && strcmp("Ljava/lang/Object;", klass->GetDescriptor(&temp)) != 0) {
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800162 early_failure = true;
163 failure_message = " that has no super class";
Ian Rogers7b078e82014-09-10 14:44:24 -0700164 } else if (super != nullptr && super->IsFinal()) {
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800165 early_failure = true;
David Sehr709b0702016-10-13 09:12:37 -0700166 failure_message = " that attempts to sub-class final class " + super->PrettyDescriptor();
Ian Rogers7b078e82014-09-10 14:44:24 -0700167 } else if (class_def == nullptr) {
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800168 early_failure = true;
169 failure_message = " that isn't present in dex file " + dex_file.GetLocation();
170 }
171 if (early_failure) {
David Sehr709b0702016-10-13 09:12:37 -0700172 *error = "Verifier rejected class " + klass->PrettyDescriptor() + failure_message;
Andreas Gampe53e32d12015-12-09 21:03:23 -0800173 if (callbacks != nullptr) {
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800174 ClassReference ref(&dex_file, klass->GetDexClassDefIndex());
Andreas Gampe53e32d12015-12-09 21:03:23 -0800175 callbacks->ClassRejected(ref);
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800176 }
Andreas Gampe6d7abbd2017-04-24 13:19:09 -0700177 return FailureKind::kHardFailure;
jeffhaobdb76512011-09-07 11:43:16 -0700178 }
Ian Rogers7b078e82014-09-10 14:44:24 -0700179 StackHandleScope<2> hs(self);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700180 Handle<mirror::DexCache> dex_cache(hs.NewHandle(klass->GetDexCache()));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700181 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(klass->GetClassLoader()));
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800182 return VerifyClass(self,
183 &dex_file,
184 dex_cache,
185 class_loader,
David Brazdil15fc7292016-09-02 14:13:18 +0100186 *class_def,
Andreas Gampe53e32d12015-12-09 21:03:23 -0800187 callbacks,
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800188 allow_soft_failures,
Andreas Gampe7fe30232016-03-25 16:58:00 -0700189 log_level,
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800190 error);
191}
192
193template <bool kDirect>
194static bool HasNextMethod(ClassDataItemIterator* it) {
195 return kDirect ? it->HasNextDirectMethod() : it->HasNextVirtualMethod();
196}
197
Andreas Gampe6d7abbd2017-04-24 13:19:09 -0700198static FailureKind FailureKindMax(FailureKind fk1, FailureKind fk2) {
199 static_assert(FailureKind::kNoFailure < FailureKind::kSoftFailure
200 && FailureKind::kSoftFailure < FailureKind::kHardFailure,
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800201 "Unexpected FailureKind order");
202 return std::max(fk1, fk2);
203}
204
205void MethodVerifier::FailureData::Merge(const MethodVerifier::FailureData& fd) {
206 kind = FailureKindMax(kind, fd.kind);
207 types |= fd.types;
208}
209
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800210template <bool kDirect>
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800211MethodVerifier::FailureData MethodVerifier::VerifyMethods(Thread* self,
212 ClassLinker* linker,
213 const DexFile* dex_file,
David Brazdil15fc7292016-09-02 14:13:18 +0100214 const DexFile::ClassDef& class_def,
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800215 ClassDataItemIterator* it,
216 Handle<mirror::DexCache> dex_cache,
217 Handle<mirror::ClassLoader> class_loader,
218 CompilerCallbacks* callbacks,
219 bool allow_soft_failures,
Andreas Gampe5fd66d02016-09-12 20:22:19 -0700220 HardFailLogMode log_level,
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800221 bool need_precise_constants,
222 std::string* error_string) {
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800223 DCHECK(it != nullptr);
224
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800225 MethodVerifier::FailureData failure_data;
226
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800227 int64_t previous_method_idx = -1;
228 while (HasNextMethod<kDirect>(it)) {
229 self->AllowThreadSuspension();
230 uint32_t method_idx = it->GetMemberIndex();
231 if (method_idx == previous_method_idx) {
232 // smali can create dex files with two encoded_methods sharing the same method_idx
233 // http://code.google.com/p/smali/issues/detail?id=119
234 it->Next();
235 continue;
236 }
237 previous_method_idx = method_idx;
David Brazdil15fc7292016-09-02 14:13:18 +0100238 InvokeType type = it->GetMethodInvokeType(class_def);
Vladimir Markoba118822017-06-12 15:41:56 +0100239 ArtMethod* method = linker->ResolveMethod<ClassLinker::ResolveMode::kNoChecks>(
Vladimir Marko89011192017-12-11 13:45:05 +0000240 method_idx, dex_cache, class_loader, /* referrer */ nullptr, type);
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800241 if (method == nullptr) {
242 DCHECK(self->IsExceptionPending());
243 // We couldn't resolve the method, but continue regardless.
244 self->ClearException();
245 } else {
246 DCHECK(method->GetDeclaringClassUnchecked() != nullptr) << type;
247 }
248 StackHandleScope<1> hs(self);
249 std::string hard_failure_msg;
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800250 MethodVerifier::FailureData result = VerifyMethod(self,
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800251 method_idx,
252 dex_file,
253 dex_cache,
254 class_loader,
255 class_def,
256 it->GetMethodCodeItem(),
257 method,
258 it->GetMethodAccessFlags(),
Andreas Gampe53e32d12015-12-09 21:03:23 -0800259 callbacks,
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800260 allow_soft_failures,
Andreas Gampe7fe30232016-03-25 16:58:00 -0700261 log_level,
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800262 need_precise_constants,
263 &hard_failure_msg);
Andreas Gampe6d7abbd2017-04-24 13:19:09 -0700264 if (result.kind == FailureKind::kHardFailure) {
265 if (failure_data.kind == FailureKind::kHardFailure) {
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800266 // If we logged an error before, we need a newline.
267 *error_string += "\n";
268 } else {
269 // If we didn't log a hard failure before, print the header of the message.
270 *error_string += "Verifier rejected class ";
David Brazdil15fc7292016-09-02 14:13:18 +0100271 *error_string += PrettyDescriptor(dex_file->GetClassDescriptor(class_def));
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800272 *error_string += ":";
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800273 }
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800274 *error_string += " ";
275 *error_string += hard_failure_msg;
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800276 }
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800277 failure_data.Merge(result);
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800278 it->Next();
279 }
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800280
281 return failure_data;
Shih-wei Liao371814f2011-10-27 16:52:10 -0700282}
283
Andreas Gampe6d7abbd2017-04-24 13:19:09 -0700284FailureKind MethodVerifier::VerifyClass(Thread* self,
285 const DexFile* dex_file,
286 Handle<mirror::DexCache> dex_cache,
287 Handle<mirror::ClassLoader> class_loader,
288 const DexFile::ClassDef& class_def,
289 CompilerCallbacks* callbacks,
290 bool allow_soft_failures,
291 HardFailLogMode log_level,
292 std::string* error) {
Andreas Gampec6548162017-12-08 12:15:22 -0800293 SCOPED_TRACE << "VerifyClass " << PrettyDescriptor(dex_file->GetClassDescriptor(class_def));
Andreas Gampe507cc6f2015-06-19 22:58:47 -0700294
295 // A class must not be abstract and final.
David Brazdil15fc7292016-09-02 14:13:18 +0100296 if ((class_def.access_flags_ & (kAccAbstract | kAccFinal)) == (kAccAbstract | kAccFinal)) {
Andreas Gampe507cc6f2015-06-19 22:58:47 -0700297 *error = "Verifier rejected class ";
David Brazdil15fc7292016-09-02 14:13:18 +0100298 *error += PrettyDescriptor(dex_file->GetClassDescriptor(class_def));
Andreas Gampe507cc6f2015-06-19 22:58:47 -0700299 *error += ": class is abstract and final.";
Andreas Gampe6d7abbd2017-04-24 13:19:09 -0700300 return FailureKind::kHardFailure;
Andreas Gampe507cc6f2015-06-19 22:58:47 -0700301 }
302
David Brazdil15fc7292016-09-02 14:13:18 +0100303 const uint8_t* class_data = dex_file->GetClassData(class_def);
Ian Rogers7b078e82014-09-10 14:44:24 -0700304 if (class_data == nullptr) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700305 // empty class, probably a marker interface
Andreas Gampe6d7abbd2017-04-24 13:19:09 -0700306 return FailureKind::kNoFailure;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700307 }
jeffhaof56197c2012-03-05 18:01:54 -0800308 ClassDataItemIterator it(*dex_file, class_data);
Mathieu Chartiere17cf242017-06-19 11:05:51 -0700309 it.SkipAllFields();
Ian Rogersad0b3a32012-04-16 14:50:24 -0700310 ClassLinker* linker = Runtime::Current()->GetClassLinker();
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800311 // Direct methods.
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800312 MethodVerifier::FailureData data1 = VerifyMethods<true>(self,
313 linker,
314 dex_file,
315 class_def,
316 &it,
317 dex_cache,
318 class_loader,
319 callbacks,
320 allow_soft_failures,
Andreas Gampe7fe30232016-03-25 16:58:00 -0700321 log_level,
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800322 false /* need precise constants */,
323 error);
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800324 // Virtual methods.
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800325 MethodVerifier::FailureData data2 = VerifyMethods<false>(self,
326 linker,
327 dex_file,
328 class_def,
329 &it,
330 dex_cache,
331 class_loader,
332 callbacks,
333 allow_soft_failures,
Andreas Gampe7fe30232016-03-25 16:58:00 -0700334 log_level,
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800335 false /* need precise constants */,
336 error);
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800337
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800338 data1.Merge(data2);
339
Andreas Gampe6d7abbd2017-04-24 13:19:09 -0700340 if (data1.kind == FailureKind::kNoFailure) {
341 return FailureKind::kNoFailure;
jeffhaof1e6b7c2012-06-05 18:33:30 -0700342 } else {
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800343 if ((data1.types & VERIFY_ERROR_LOCKING) != 0) {
344 // Print a warning about expected slow-down. Use a string temporary to print one contiguous
345 // warning.
346 std::string tmp =
347 StringPrintf("Class %s failed lock verification and will run slower.",
David Brazdil15fc7292016-09-02 14:13:18 +0100348 PrettyDescriptor(dex_file->GetClassDescriptor(class_def)).c_str());
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800349 if (!gPrintedDxMonitorText) {
350 tmp = tmp + "\nCommon causes for lock verification issues are non-optimized dex code\n"
351 "and incorrect proguard optimizations.";
352 gPrintedDxMonitorText = true;
353 }
354 LOG(WARNING) << tmp;
355 }
356 return data1.kind;
jeffhaof1e6b7c2012-06-05 18:33:30 -0700357 }
jeffhaof56197c2012-03-05 18:01:54 -0800358}
359
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800360static bool IsLargeMethod(const CodeItemDataAccessor& accessor) {
361 if (!accessor.HasCodeItem()) {
Andreas Gampe3c651fc2015-05-21 14:06:46 -0700362 return false;
363 }
364
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800365 uint16_t registers_size = accessor.RegistersSize();
366 uint32_t insns_size = accessor.InsnsSizeInCodeUnits();
Andreas Gampea4f5bf62015-05-18 20:50:12 -0700367
368 return registers_size * insns_size > 4*1024*1024;
369}
370
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800371MethodVerifier::FailureData MethodVerifier::VerifyMethod(Thread* self,
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800372 uint32_t method_idx,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800373 const DexFile* dex_file,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700374 Handle<mirror::DexCache> dex_cache,
375 Handle<mirror::ClassLoader> class_loader,
David Brazdil15fc7292016-09-02 14:13:18 +0100376 const DexFile::ClassDef& class_def,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800377 const DexFile::CodeItem* code_item,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700378 ArtMethod* method,
Jeff Haoee988952013-04-16 14:23:47 -0700379 uint32_t method_access_flags,
Andreas Gampe53e32d12015-12-09 21:03:23 -0800380 CompilerCallbacks* callbacks,
Ian Rogers46960fe2014-05-23 10:43:43 -0700381 bool allow_soft_failures,
Andreas Gampe5fd66d02016-09-12 20:22:19 -0700382 HardFailLogMode log_level,
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800383 bool need_precise_constants,
384 std::string* hard_failure_msg) {
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800385 MethodVerifier::FailureData result;
Mathieu Chartier8e219ae2014-08-19 14:29:46 -0700386 uint64_t start_ns = kTimeVerifyMethod ? NanoTime() : 0;
Ian Rogersc8982582012-09-07 16:53:25 -0700387
Andreas Gampebf9611f2016-03-25 16:58:00 -0700388 MethodVerifier verifier(self,
389 dex_file,
390 dex_cache,
391 class_loader,
392 class_def,
393 code_item,
394 method_idx,
395 method,
396 method_access_flags,
397 true /* can_load_classes */,
398 allow_soft_failures,
399 need_precise_constants,
400 false /* verify to dump */,
401 true /* allow_thread_suspension */);
Ian Rogers46960fe2014-05-23 10:43:43 -0700402 if (verifier.Verify()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700403 // Verification completed, however failures may be pending that didn't cause the verification
404 // to hard fail.
Ian Rogers46960fe2014-05-23 10:43:43 -0700405 CHECK(!verifier.have_pending_hard_failure_);
Andreas Gampe53e32d12015-12-09 21:03:23 -0800406
407 if (code_item != nullptr && callbacks != nullptr) {
408 // Let the interested party know that the method was verified.
409 callbacks->MethodVerified(&verifier);
410 }
411
Ian Rogers46960fe2014-05-23 10:43:43 -0700412 if (verifier.failures_.size() != 0) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -0700413 if (VLOG_IS_ON(verifier)) {
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800414 verifier.DumpFailures(VLOG_STREAM(verifier) << "Soft verification failures in "
David Sehr709b0702016-10-13 09:12:37 -0700415 << dex_file->PrettyMethod(method_idx) << "\n");
Anwar Ghuloum75a43f12013-08-13 17:22:14 -0700416 }
Andreas Gampe92d77202017-12-06 20:49:00 -0800417 if (VLOG_IS_ON(verifier_debug)) {
418 std::cout << "\n" << verifier.info_messages_.str();
419 verifier.Dump(std::cout);
420 }
Andreas Gampe6d7abbd2017-04-24 13:19:09 -0700421 result.kind = FailureKind::kSoftFailure;
Nicolas Geoffray250a3782016-04-20 16:27:53 +0100422 if (method != nullptr &&
423 !CanCompilerHandleVerificationFailure(verifier.encountered_failure_types_)) {
Alex Lightfcbafb32017-02-02 15:09:54 -0800424 method->SetDontCompile();
Nicolas Geoffray250a3782016-04-20 16:27:53 +0100425 }
426 }
Andreas Gampe56fdd0e2016-04-28 14:56:54 -0700427 if (method != nullptr) {
428 if (verifier.HasInstructionThatWillThrow()) {
Alex Lightfcbafb32017-02-02 15:09:54 -0800429 method->SetDontCompile();
Calin Juravle857f0582016-12-20 14:36:59 +0000430 if (Runtime::Current()->IsAotCompiler() &&
431 (callbacks != nullptr) && !callbacks->IsBootImage()) {
432 // When compiling apps, make HasInstructionThatWillThrow a soft error to trigger
433 // re-verification at runtime.
434 // The dead code after the throw is not verified and might be invalid. This may cause
435 // the JIT compiler to crash since it assumes that all the code is valid.
436 //
437 // There's a strong assumption that the entire boot image is verified and all its dex
438 // code is valid (even the dead and unverified one). As such this is done only for apps.
439 // (CompilerDriver DCHECKs in VerifyClassVisitor that methods from boot image are
440 // fully verified).
Andreas Gampe6d7abbd2017-04-24 13:19:09 -0700441 result.kind = FailureKind::kSoftFailure;
Calin Juravle857f0582016-12-20 14:36:59 +0000442 }
Andreas Gampe56fdd0e2016-04-28 14:56:54 -0700443 }
444 if ((verifier.encountered_failure_types_ & VerifyError::VERIFY_ERROR_LOCKING) != 0) {
Orion Hodsoncfcc9cf2017-09-29 15:07:27 +0100445 method->SetMustCountLocks();
Andreas Gampe56fdd0e2016-04-28 14:56:54 -0700446 }
jeffhaof56197c2012-03-05 18:01:54 -0800447 }
448 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700449 // Bad method data.
Ian Rogers46960fe2014-05-23 10:43:43 -0700450 CHECK_NE(verifier.failures_.size(), 0U);
Igor Murashkin4d7b75f2015-07-21 17:03:36 -0700451
452 if (UNLIKELY(verifier.have_pending_experimental_failure_)) {
453 // Failed due to being forced into interpreter. This is ok because
454 // we just want to skip verification.
Andreas Gampe6d7abbd2017-04-24 13:19:09 -0700455 result.kind = FailureKind::kSoftFailure;
Igor Murashkin4d7b75f2015-07-21 17:03:36 -0700456 } else {
457 CHECK(verifier.have_pending_hard_failure_);
Andreas Gampe7fe30232016-03-25 16:58:00 -0700458 if (VLOG_IS_ON(verifier)) {
Andreas Gampe5fd66d02016-09-12 20:22:19 -0700459 log_level = std::max(HardFailLogMode::kLogVerbose, log_level);
Andreas Gampe7fe30232016-03-25 16:58:00 -0700460 }
Andreas Gampe5fd66d02016-09-12 20:22:19 -0700461 if (log_level >= HardFailLogMode::kLogVerbose) {
462 LogSeverity severity;
463 switch (log_level) {
464 case HardFailLogMode::kLogVerbose:
465 severity = LogSeverity::VERBOSE;
466 break;
467 case HardFailLogMode::kLogWarning:
468 severity = LogSeverity::WARNING;
469 break;
470 case HardFailLogMode::kLogInternalFatal:
Andreas Gampe3fec9ac2016-09-13 10:47:28 -0700471 severity = LogSeverity::FATAL_WITHOUT_ABORT;
Andreas Gampe5fd66d02016-09-12 20:22:19 -0700472 break;
473 default:
474 LOG(FATAL) << "Unsupported log-level " << static_cast<uint32_t>(log_level);
475 UNREACHABLE();
476 }
Andreas Gampe3fec9ac2016-09-13 10:47:28 -0700477 verifier.DumpFailures(LOG_STREAM(severity) << "Verification error in "
David Sehr709b0702016-10-13 09:12:37 -0700478 << dex_file->PrettyMethod(method_idx)
Andreas Gampe3fec9ac2016-09-13 10:47:28 -0700479 << "\n");
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800480 }
481 if (hard_failure_msg != nullptr) {
482 CHECK(!verifier.failure_messages_.empty());
483 *hard_failure_msg =
484 verifier.failure_messages_[verifier.failure_messages_.size() - 1]->str();
485 }
Andreas Gampe6d7abbd2017-04-24 13:19:09 -0700486 result.kind = FailureKind::kHardFailure;
Andreas Gampe53e32d12015-12-09 21:03:23 -0800487
488 if (callbacks != nullptr) {
489 // Let the interested party know that we failed the class.
David Brazdil15fc7292016-09-02 14:13:18 +0100490 ClassReference ref(dex_file, dex_file->GetIndexForClassDef(class_def));
Andreas Gampe53e32d12015-12-09 21:03:23 -0800491 callbacks->ClassRejected(ref);
492 }
Igor Murashkin4d7b75f2015-07-21 17:03:36 -0700493 }
Andreas Gampe92d77202017-12-06 20:49:00 -0800494 if (VLOG_IS_ON(verifier) || VLOG_IS_ON(verifier_debug)) {
Ian Rogers46960fe2014-05-23 10:43:43 -0700495 std::cout << "\n" << verifier.info_messages_.str();
496 verifier.Dump(std::cout);
jeffhaof56197c2012-03-05 18:01:54 -0800497 }
jeffhaof56197c2012-03-05 18:01:54 -0800498 }
Mathieu Chartier8e219ae2014-08-19 14:29:46 -0700499 if (kTimeVerifyMethod) {
500 uint64_t duration_ns = NanoTime() - start_ns;
501 if (duration_ns > MsToNs(100)) {
David Sehr709b0702016-10-13 09:12:37 -0700502 LOG(WARNING) << "Verification of " << dex_file->PrettyMethod(method_idx)
Andreas Gampea4f5bf62015-05-18 20:50:12 -0700503 << " took " << PrettyDuration(duration_ns)
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800504 << (IsLargeMethod(verifier.CodeItem()) ? " (large method)" : "");
Mathieu Chartier8e219ae2014-08-19 14:29:46 -0700505 }
Ian Rogersc8982582012-09-07 16:53:25 -0700506 }
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800507 result.types = verifier.encountered_failure_types_;
Ian Rogersc8982582012-09-07 16:53:25 -0700508 return result;
jeffhaof56197c2012-03-05 18:01:54 -0800509}
510
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100511MethodVerifier* MethodVerifier::VerifyMethodAndDump(Thread* self,
512 VariableIndentationOutputStream* vios,
513 uint32_t dex_method_idx,
514 const DexFile* dex_file,
515 Handle<mirror::DexCache> dex_cache,
516 Handle<mirror::ClassLoader> class_loader,
David Brazdil15fc7292016-09-02 14:13:18 +0100517 const DexFile::ClassDef& class_def,
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100518 const DexFile::CodeItem* code_item,
519 ArtMethod* method,
520 uint32_t method_access_flags) {
Andreas Gampebf9611f2016-03-25 16:58:00 -0700521 MethodVerifier* verifier = new MethodVerifier(self,
522 dex_file,
523 dex_cache,
524 class_loader,
525 class_def,
526 code_item,
527 dex_method_idx,
528 method,
529 method_access_flags,
530 true /* can_load_classes */,
531 true /* allow_soft_failures */,
532 true /* need_precise_constants */,
533 true /* verify_to_dump */,
534 true /* allow_thread_suspension */);
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700535 verifier->Verify();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100536 verifier->DumpFailures(vios->Stream());
537 vios->Stream() << verifier->info_messages_.str();
Andreas Gampe5cbcde22014-09-16 14:59:49 -0700538 // Only dump and return if no hard failures. Otherwise the verifier may be not fully initialized
539 // and querying any info is dangerous/can abort.
540 if (verifier->have_pending_hard_failure_) {
541 delete verifier;
542 return nullptr;
543 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100544 verifier->Dump(vios);
Andreas Gampe5cbcde22014-09-16 14:59:49 -0700545 return verifier;
546 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800547}
548
Ian Rogers7b078e82014-09-10 14:44:24 -0700549MethodVerifier::MethodVerifier(Thread* self,
Andreas Gampe53e32d12015-12-09 21:03:23 -0800550 const DexFile* dex_file,
551 Handle<mirror::DexCache> dex_cache,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700552 Handle<mirror::ClassLoader> class_loader,
David Brazdil15fc7292016-09-02 14:13:18 +0100553 const DexFile::ClassDef& class_def,
Andreas Gampe53e32d12015-12-09 21:03:23 -0800554 const DexFile::CodeItem* code_item,
555 uint32_t dex_method_idx,
556 ArtMethod* method,
557 uint32_t method_access_flags,
558 bool can_load_classes,
559 bool allow_soft_failures,
560 bool need_precise_constants,
561 bool verify_to_dump,
Mathieu Chartier4306ef82014-12-19 18:41:47 -0800562 bool allow_thread_suspension)
Ian Rogers7b078e82014-09-10 14:44:24 -0700563 : self_(self),
Mathieu Chartierde40d472015-10-15 17:47:48 -0700564 arena_stack_(Runtime::Current()->GetArenaPool()),
Vladimir Marko69d310e2017-10-09 14:12:23 +0100565 allocator_(&arena_stack_),
Alex Lightd9aff132017-10-31 22:30:05 +0000566 reg_types_(can_load_classes, allocator_, allow_thread_suspension),
Vladimir Marko69d310e2017-10-09 14:12:23 +0100567 reg_table_(allocator_),
Andreas Gampee2abbc62017-09-15 11:59:26 -0700568 work_insn_idx_(dex::kDexNoIndex),
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800569 dex_method_idx_(dex_method_idx),
Nicolas Geoffrayb041a402017-11-13 15:16:22 +0000570 method_being_verified_(method),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700571 method_access_flags_(method_access_flags),
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700572 return_type_(nullptr),
jeffhaof56197c2012-03-05 18:01:54 -0800573 dex_file_(dex_file),
574 dex_cache_(dex_cache),
575 class_loader_(class_loader),
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700576 class_def_(class_def),
Mathieu Chartier698ebbc2018-01-05 11:00:42 -0800577 code_item_accessor_(*dex_file, code_item),
Ian Rogers7b078e82014-09-10 14:44:24 -0700578 declaring_class_(nullptr),
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700579 interesting_dex_pc_(-1),
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700580 monitor_enter_dex_pcs_(nullptr),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700581 have_pending_hard_failure_(false),
jeffhaofaf459e2012-08-31 15:32:47 -0700582 have_pending_runtime_throw_failure_(false),
Igor Murashkin4d7b75f2015-07-21 17:03:36 -0700583 have_pending_experimental_failure_(false),
Andreas Gamped12e7822015-06-25 10:26:40 -0700584 have_any_pending_runtime_throw_failure_(false),
jeffhaof56197c2012-03-05 18:01:54 -0800585 new_instance_count_(0),
Elliott Hughes80537bb2013-01-04 16:37:26 -0800586 monitor_enter_count_(0),
Andreas Gampe0760a812015-08-26 17:12:51 -0700587 encountered_failure_types_(0),
Jeff Haoee988952013-04-16 14:23:47 -0700588 can_load_classes_(can_load_classes),
Sebastien Hertz4d4adb12013-07-24 16:14:19 +0200589 allow_soft_failures_(allow_soft_failures),
Ian Rogers46960fe2014-05-23 10:43:43 -0700590 need_precise_constants_(need_precise_constants),
Sebastien Hertz4d4adb12013-07-24 16:14:19 +0200591 has_check_casts_(false),
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700592 has_virtual_or_interface_invokes_(false),
Mathieu Chartier4306ef82014-12-19 18:41:47 -0800593 verify_to_dump_(verify_to_dump),
Andreas Gamped5ad72f2015-06-26 17:33:47 -0700594 allow_thread_suspension_(allow_thread_suspension),
Andreas Gampee6215c02015-08-31 18:54:38 -0700595 is_constructor_(false),
Andreas Gamped5ad72f2015-06-26 17:33:47 -0700596 link_(nullptr) {
Mathieu Chartierd0ad2ee2015-03-31 14:59:59 -0700597 self->PushVerifier(this);
jeffhaof56197c2012-03-05 18:01:54 -0800598}
599
Mathieu Chartier590fee92013-09-13 13:46:47 -0700600MethodVerifier::~MethodVerifier() {
Mathieu Chartierd0ad2ee2015-03-31 14:59:59 -0700601 Thread::Current()->PopVerifier(this);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700602 STLDeleteElements(&failure_messages_);
603}
604
Andreas Gampeaaf0d382017-11-27 14:10:21 -0800605void MethodVerifier::FindLocksAtDexPc(
606 ArtMethod* m,
607 uint32_t dex_pc,
608 std::vector<MethodVerifier::DexLockInfo>* monitor_enter_dex_pcs) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700609 StackHandleScope<2> hs(Thread::Current());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700610 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache()));
611 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader()));
Andreas Gampebf9611f2016-03-25 16:58:00 -0700612 MethodVerifier verifier(hs.Self(),
613 m->GetDexFile(),
614 dex_cache,
615 class_loader,
David Brazdil15fc7292016-09-02 14:13:18 +0100616 m->GetClassDef(),
Andreas Gampebf9611f2016-03-25 16:58:00 -0700617 m->GetCodeItem(),
618 m->GetDexMethodIndex(),
619 m,
620 m->GetAccessFlags(),
621 false /* can_load_classes */,
622 true /* allow_soft_failures */,
623 false /* need_precise_constants */,
624 false /* verify_to_dump */,
625 false /* allow_thread_suspension */);
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700626 verifier.interesting_dex_pc_ = dex_pc;
Ian Rogers46960fe2014-05-23 10:43:43 -0700627 verifier.monitor_enter_dex_pcs_ = monitor_enter_dex_pcs;
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700628 verifier.FindLocksAtDexPc();
629}
630
631void MethodVerifier::FindLocksAtDexPc() {
Ian Rogers7b078e82014-09-10 14:44:24 -0700632 CHECK(monitor_enter_dex_pcs_ != nullptr);
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -0800633 CHECK(code_item_accessor_.HasCodeItem()); // This only makes sense for methods with code.
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700634
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -0800635 // Quick check whether there are any monitor_enter instructions before verifying.
636 for (const DexInstructionPcPair& inst : code_item_accessor_) {
637 if (inst->Opcode() == Instruction::MONITOR_ENTER) {
638 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
639 // verification. In practice, the phase we want relies on data structures set up by all the
640 // earlier passes, so we just run the full method verification and bail out early when we've
641 // got what we wanted.
642 Verify();
643 return;
644 }
Andreas Gampecb3c08f2014-09-18 13:16:38 -0700645 }
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700646}
647
Ian Rogersad0b3a32012-04-16 14:50:24 -0700648bool MethodVerifier::Verify() {
Andreas Gampee6215c02015-08-31 18:54:38 -0700649 // Some older code doesn't correctly mark constructors as such. Test for this case by looking at
650 // the name.
651 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
652 const char* method_name = dex_file_->StringDataByIdx(method_id.name_idx_);
653 bool instance_constructor_by_name = strcmp("<init>", method_name) == 0;
654 bool static_constructor_by_name = strcmp("<clinit>", method_name) == 0;
655 bool constructor_by_name = instance_constructor_by_name || static_constructor_by_name;
656 // Check that only constructors are tagged, and check for bad code that doesn't tag constructors.
657 if ((method_access_flags_ & kAccConstructor) != 0) {
658 if (!constructor_by_name) {
659 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
660 << "method is marked as constructor, but not named accordingly";
jeffhaobdb76512011-09-07 11:43:16 -0700661 return false;
Andreas Gampee6215c02015-08-31 18:54:38 -0700662 }
663 is_constructor_ = true;
664 } else if (constructor_by_name) {
David Sehr709b0702016-10-13 09:12:37 -0700665 LOG(WARNING) << "Method " << dex_file_->PrettyMethod(dex_method_idx_)
Andreas Gampee6215c02015-08-31 18:54:38 -0700666 << " not marked as constructor.";
667 is_constructor_ = true;
668 }
669 // If it's a constructor, check whether IsStatic() matches the name.
670 // This should have been rejected by the dex file verifier. Only do in debug build.
671 if (kIsDebugBuild) {
672 if (IsConstructor()) {
673 if (IsStatic() ^ static_constructor_by_name) {
674 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
675 << "constructor name doesn't match static flag";
676 return false;
677 }
jeffhaobdb76512011-09-07 11:43:16 -0700678 }
jeffhaobdb76512011-09-07 11:43:16 -0700679 }
Andreas Gampee6215c02015-08-31 18:54:38 -0700680
681 // Methods may only have one of public/protected/private.
682 // This should have been rejected by the dex file verifier. Only do in debug build.
683 if (kIsDebugBuild) {
684 size_t access_mod_count =
685 (((method_access_flags_ & kAccPublic) == 0) ? 0 : 1) +
686 (((method_access_flags_ & kAccProtected) == 0) ? 0 : 1) +
687 (((method_access_flags_ & kAccPrivate) == 0) ? 0 : 1);
688 if (access_mod_count > 1) {
689 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "method has more than one of public/protected/private";
690 return false;
691 }
692 }
693
694 // If there aren't any instructions, make sure that's expected, then exit successfully.
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -0800695 if (!code_item_accessor_.HasCodeItem()) {
Andreas Gampe76ed99d2016-03-28 18:31:29 -0700696 // Only native or abstract methods may not have code.
697 if ((method_access_flags_ & (kAccNative | kAccAbstract)) == 0) {
698 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "zero-length code in concrete non-native method";
699 return false;
700 }
701
Andreas Gampee6215c02015-08-31 18:54:38 -0700702 // This should have been rejected by the dex file verifier. Only do in debug build.
Andreas Gampe76ed99d2016-03-28 18:31:29 -0700703 // Note: the above will also be rejected in the dex file verifier, starting in dex version 37.
Andreas Gampee6215c02015-08-31 18:54:38 -0700704 if (kIsDebugBuild) {
Andreas Gampee6215c02015-08-31 18:54:38 -0700705 if ((method_access_flags_ & kAccAbstract) != 0) {
706 // Abstract methods are not allowed to have the following flags.
707 static constexpr uint32_t kForbidden =
708 kAccPrivate |
709 kAccStatic |
710 kAccFinal |
711 kAccNative |
712 kAccStrict |
713 kAccSynchronized;
714 if ((method_access_flags_ & kForbidden) != 0) {
715 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
716 << "method can't be abstract and private/static/final/native/strict/synchronized";
717 return false;
718 }
719 }
David Brazdil15fc7292016-09-02 14:13:18 +0100720 if ((class_def_.GetJavaAccessFlags() & kAccInterface) != 0) {
Alex Lighteb7c1442015-08-31 13:17:42 -0700721 // Interface methods must be public and abstract (if default methods are disabled).
Neil Fuller9724c632016-01-07 15:42:47 +0000722 uint32_t kRequired = kAccPublic;
Alex Lighteb7c1442015-08-31 13:17:42 -0700723 if ((method_access_flags_ & kRequired) != kRequired) {
Neil Fuller9724c632016-01-07 15:42:47 +0000724 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface methods must be public";
Andreas Gampee6215c02015-08-31 18:54:38 -0700725 return false;
726 }
727 // In addition to the above, interface methods must not be protected.
728 static constexpr uint32_t kForbidden = kAccProtected;
729 if ((method_access_flags_ & kForbidden) != 0) {
730 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface methods can't be protected";
731 return false;
732 }
733 }
734 // We also don't allow constructors to be abstract or native.
735 if (IsConstructor()) {
736 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "constructors can't be abstract or native";
737 return false;
738 }
739 }
740 return true;
741 }
742
743 // This should have been rejected by the dex file verifier. Only do in debug build.
744 if (kIsDebugBuild) {
745 // When there's code, the method must not be native or abstract.
746 if ((method_access_flags_ & (kAccNative | kAccAbstract)) != 0) {
747 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "non-zero-length code in abstract or native method";
748 return false;
749 }
750
David Brazdil15fc7292016-09-02 14:13:18 +0100751 if ((class_def_.GetJavaAccessFlags() & kAccInterface) != 0) {
Alex Lighteb7c1442015-08-31 13:17:42 -0700752 // Interfaces may always have static initializers for their fields. If we are running with
753 // default methods enabled we also allow other public, static, non-final methods to have code.
754 // Otherwise that is the only type of method allowed.
Alex Light0db36b32015-10-27 14:06:34 -0700755 if (!(IsConstructor() && IsStatic())) {
Neil Fuller9724c632016-01-07 15:42:47 +0000756 if (IsInstanceConstructor()) {
757 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interfaces may not have non-static constructor";
758 return false;
759 } else if (method_access_flags_ & kAccFinal) {
760 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interfaces may not have final methods";
761 return false;
Alex Lightb55f1ac2016-04-12 15:50:55 -0700762 } else {
763 uint32_t access_flag_options = kAccPublic;
Mathieu Chartierf6e31472017-12-28 13:32:08 -0800764 if (dex_file_->SupportsDefaultMethods()) {
Alex Lightb55f1ac2016-04-12 15:50:55 -0700765 access_flag_options |= kAccPrivate;
766 }
767 if (!(method_access_flags_ & access_flag_options)) {
768 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
769 << "interfaces may not have protected or package-private members";
770 return false;
771 }
Alex Lighteb7c1442015-08-31 13:17:42 -0700772 }
Andreas Gampee6215c02015-08-31 18:54:38 -0700773 }
774 }
775
776 // Instance constructors must not be synchronized.
777 if (IsInstanceConstructor()) {
778 static constexpr uint32_t kForbidden = kAccSynchronized;
779 if ((method_access_flags_ & kForbidden) != 0) {
780 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "constructors can't be synchronized";
781 return false;
782 }
783 }
784 }
785
Ian Rogersd81871c2011-10-03 13:57:23 -0700786 // Sanity-check the register counts. ins + locals = registers, so make sure that ins <= registers.
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -0800787 if (code_item_accessor_.InsSize() > code_item_accessor_.RegistersSize()) {
788 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad register counts (ins="
789 << code_item_accessor_.InsSize()
790 << " regs=" << code_item_accessor_.RegistersSize();
Ian Rogersd81871c2011-10-03 13:57:23 -0700791 return false;
jeffhaobdb76512011-09-07 11:43:16 -0700792 }
Alex Lighteb7c1442015-08-31 13:17:42 -0700793
Ian Rogersd81871c2011-10-03 13:57:23 -0700794 // Allocate and initialize an array to hold instruction data.
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -0800795 insn_flags_.reset(allocator_.AllocArray<InstructionFlags>(
796 code_item_accessor_.InsnsSizeInCodeUnits()));
Mathieu Chartierde40d472015-10-15 17:47:48 -0700797 DCHECK(insn_flags_ != nullptr);
798 std::uninitialized_fill_n(insn_flags_.get(),
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -0800799 code_item_accessor_.InsnsSizeInCodeUnits(),
Mathieu Chartierde40d472015-10-15 17:47:48 -0700800 InstructionFlags());
Ian Rogersd81871c2011-10-03 13:57:23 -0700801 // Run through the instructions and see if the width checks out.
802 bool result = ComputeWidthsAndCountOps();
Andreas Gampebf1cb772017-05-15 15:39:00 -0700803 bool allow_runtime_only_instructions = !Runtime::Current()->IsAotCompiler() || verify_to_dump_;
Ian Rogersd81871c2011-10-03 13:57:23 -0700804 // Flag instructions guarded by a "try" block and check exception handlers.
805 result = result && ScanTryCatchBlocks();
806 // Perform static instruction verification.
Andreas Gampebf1cb772017-05-15 15:39:00 -0700807 result = result && (allow_runtime_only_instructions
808 ? VerifyInstructions<true>()
809 : VerifyInstructions<false>());
Ian Rogersad0b3a32012-04-16 14:50:24 -0700810 // Perform code-flow analysis and return.
Vladimir Marko2b5eaa22013-12-13 13:59:30 +0000811 result = result && VerifyCodeFlow();
Andreas Gampe53e32d12015-12-09 21:03:23 -0800812
Vladimir Marko2b5eaa22013-12-13 13:59:30 +0000813 return result;
jeffhaoba5ebb92011-08-25 17:24:37 -0700814}
815
Ian Rogers776ac1f2012-04-13 23:36:36 -0700816std::ostream& MethodVerifier::Fail(VerifyError error) {
Andreas Gampe0760a812015-08-26 17:12:51 -0700817 // Mark the error type as encountered.
Andreas Gampea727e372015-08-25 09:22:37 -0700818 encountered_failure_types_ |= static_cast<uint32_t>(error);
Andreas Gampe0760a812015-08-26 17:12:51 -0700819
Ian Rogersad0b3a32012-04-16 14:50:24 -0700820 switch (error) {
821 case VERIFY_ERROR_NO_CLASS:
822 case VERIFY_ERROR_NO_FIELD:
823 case VERIFY_ERROR_NO_METHOD:
824 case VERIFY_ERROR_ACCESS_CLASS:
825 case VERIFY_ERROR_ACCESS_FIELD:
826 case VERIFY_ERROR_ACCESS_METHOD:
Ian Rogers08f753d2012-08-24 14:35:25 -0700827 case VERIFY_ERROR_INSTANTIATION:
828 case VERIFY_ERROR_CLASS_CHANGE:
Igor Murashkin158f35c2015-06-10 15:55:30 -0700829 case VERIFY_ERROR_FORCE_INTERPRETER:
Andreas Gampea727e372015-08-25 09:22:37 -0700830 case VERIFY_ERROR_LOCKING:
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800831 if (Runtime::Current()->IsAotCompiler() || !can_load_classes_) {
jeffhaofaf459e2012-08-31 15:32:47 -0700832 // If we're optimistically running verification at compile time, turn NO_xxx, ACCESS_xxx,
833 // class change and instantiation errors into soft verification errors so that we re-verify
834 // at runtime. We may fail to find or to agree on access because of not yet available class
835 // loaders, or class loaders that will differ at runtime. In these cases, we don't want to
836 // affect the soundness of the code being compiled. Instead, the generated code runs "slow
837 // paths" that dynamically perform the verification and cause the behavior to be that akin
838 // to an interpreter.
839 error = VERIFY_ERROR_BAD_CLASS_SOFT;
840 } else {
Jeff Haoa3faaf42013-09-03 19:07:00 -0700841 // If we fail again at runtime, mark that this instruction would throw and force this
842 // method to be executed using the interpreter with checks.
jeffhaofaf459e2012-08-31 15:32:47 -0700843 have_pending_runtime_throw_failure_ = true;
Andreas Gamped7f8d052015-03-12 11:05:47 -0700844
845 // We need to save the work_line if the instruction wasn't throwing before. Otherwise we'll
846 // try to merge garbage.
847 // Note: this assumes that Fail is called before we do any work_line modifications.
Andreas Gamped5ad72f2015-06-26 17:33:47 -0700848 // Note: this can fail before we touch any instruction, for the signature of a method. So
849 // add a check.
Andreas Gampee2abbc62017-09-15 11:59:26 -0700850 if (work_insn_idx_ < dex::kDexNoIndex) {
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -0800851 const Instruction& inst = code_item_accessor_.InstructionAt(work_insn_idx_);
Mathieu Chartier1d2d4ff2017-09-23 16:11:06 -0700852 int opcode_flags = Instruction::FlagsOf(inst.Opcode());
Andreas Gamped7f8d052015-03-12 11:05:47 -0700853
Andreas Gamped5ad72f2015-06-26 17:33:47 -0700854 if ((opcode_flags & Instruction::kThrow) == 0 && CurrentInsnFlags()->IsInTry()) {
855 saved_line_->CopyFromLine(work_line_.get());
856 }
Andreas Gamped7f8d052015-03-12 11:05:47 -0700857 }
jeffhaofaf459e2012-08-31 15:32:47 -0700858 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700859 break;
Andreas Gampea727e372015-08-25 09:22:37 -0700860
Ian Rogersad0b3a32012-04-16 14:50:24 -0700861 // Indication that verification should be retried at runtime.
862 case VERIFY_ERROR_BAD_CLASS_SOFT:
Jeff Haoee988952013-04-16 14:23:47 -0700863 if (!allow_soft_failures_) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700864 have_pending_hard_failure_ = true;
865 }
866 break;
Andreas Gampea727e372015-08-25 09:22:37 -0700867
jeffhaod5347e02012-03-22 17:25:05 -0700868 // Hard verification failures at compile time will still fail at runtime, so the class is
869 // marked as rejected to prevent it from being compiled.
Ian Rogersad0b3a32012-04-16 14:50:24 -0700870 case VERIFY_ERROR_BAD_CLASS_HARD: {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700871 have_pending_hard_failure_ = true;
Andreas Gampeebf850c2015-08-14 15:37:35 -0700872 if (VLOG_IS_ON(verifier) && kDumpRegLinesOnHardFailureIfVLOG) {
873 ScopedObjectAccess soa(Thread::Current());
874 std::ostringstream oss;
875 Dump(oss);
876 LOG(ERROR) << oss.str();
877 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700878 break;
Ian Rogers47a05882012-02-03 12:23:33 -0800879 }
880 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700881 failures_.push_back(error);
David Sehr709b0702016-10-13 09:12:37 -0700882 std::string location(StringPrintf("%s: [0x%X] ", dex_file_->PrettyMethod(dex_method_idx_).c_str(),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700883 work_insn_idx_));
Elena Sayapina78480ec2014-08-15 15:52:42 +0700884 std::ostringstream* failure_message = new std::ostringstream(location, std::ostringstream::ate);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700885 failure_messages_.push_back(failure_message);
886 return *failure_message;
887}
888
Ian Rogers576ca0c2014-06-06 15:58:22 -0700889std::ostream& MethodVerifier::LogVerifyInfo() {
David Sehr709b0702016-10-13 09:12:37 -0700890 return info_messages_ << "VFY: " << dex_file_->PrettyMethod(dex_method_idx_)
Ian Rogers576ca0c2014-06-06 15:58:22 -0700891 << '[' << reinterpret_cast<void*>(work_insn_idx_) << "] : ";
892}
893
Ian Rogersad0b3a32012-04-16 14:50:24 -0700894void MethodVerifier::PrependToLastFailMessage(std::string prepend) {
895 size_t failure_num = failure_messages_.size();
896 DCHECK_NE(failure_num, 0U);
897 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
898 prepend += last_fail_message->str();
Elena Sayapina78480ec2014-08-15 15:52:42 +0700899 failure_messages_[failure_num - 1] = new std::ostringstream(prepend, std::ostringstream::ate);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700900 delete last_fail_message;
901}
902
Vladimir Marko5c657fe2016-11-03 15:12:29 +0000903void MethodVerifier::AppendToLastFailMessage(const std::string& append) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700904 size_t failure_num = failure_messages_.size();
905 DCHECK_NE(failure_num, 0U);
906 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
907 (*last_fail_message) << append;
Ian Rogers47a05882012-02-03 12:23:33 -0800908}
909
Ian Rogers776ac1f2012-04-13 23:36:36 -0700910bool MethodVerifier::ComputeWidthsAndCountOps() {
jeffhaobdb76512011-09-07 11:43:16 -0700911 size_t new_instance_count = 0;
912 size_t monitor_enter_count = 0;
jeffhaobdb76512011-09-07 11:43:16 -0700913
Mathieu Chartieraf7c9022017-10-27 09:42:46 -0700914 // We can't assume the instruction is well formed, handle the case where calculating the size
915 // goes past the end of the code item.
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -0800916 SafeDexInstructionIterator it(code_item_accessor_.begin(), code_item_accessor_.end());
917 for ( ; !it.IsErrorState() && it < code_item_accessor_.end(); ++it) {
Mathieu Chartieraf7c9022017-10-27 09:42:46 -0700918 // In case the instruction goes past the end of the code item, make sure to not process it.
919 SafeDexInstructionIterator next = it;
920 ++next;
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -0800921 if (next.IsErrorState()) {
Mathieu Chartieraf7c9022017-10-27 09:42:46 -0700922 break;
923 }
924 Instruction::Code opcode = it->Opcode();
Ian Rogersa9a82542013-10-04 11:17:26 -0700925 switch (opcode) {
926 case Instruction::APUT_OBJECT:
927 case Instruction::CHECK_CAST:
928 has_check_casts_ = true;
929 break;
930 case Instruction::INVOKE_VIRTUAL:
931 case Instruction::INVOKE_VIRTUAL_RANGE:
932 case Instruction::INVOKE_INTERFACE:
933 case Instruction::INVOKE_INTERFACE_RANGE:
934 has_virtual_or_interface_invokes_ = true;
935 break;
936 case Instruction::MONITOR_ENTER:
937 monitor_enter_count++;
938 break;
939 case Instruction::NEW_INSTANCE:
940 new_instance_count++;
941 break;
942 default:
943 break;
jeffhaobdb76512011-09-07 11:43:16 -0700944 }
Mathieu Chartier2b2bef22017-10-26 17:10:19 -0700945 GetInstructionFlags(it.DexPc()).SetIsOpcode();
jeffhaobdb76512011-09-07 11:43:16 -0700946 }
947
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -0800948 if (it != code_item_accessor_.end()) {
949 const size_t insns_size = code_item_accessor_.InsnsSizeInCodeUnits();
jeffhaod5347e02012-03-22 17:25:05 -0700950 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "code did not end where expected ("
Mathieu Chartier2b2bef22017-10-26 17:10:19 -0700951 << it.DexPc() << " vs. " << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700952 return false;
953 }
954
Ian Rogersd81871c2011-10-03 13:57:23 -0700955 new_instance_count_ = new_instance_count;
956 monitor_enter_count_ = monitor_enter_count;
jeffhaobdb76512011-09-07 11:43:16 -0700957 return true;
958}
959
Ian Rogers776ac1f2012-04-13 23:36:36 -0700960bool MethodVerifier::ScanTryCatchBlocks() {
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -0800961 const uint32_t tries_size = code_item_accessor_.TriesSize();
jeffhaobdb76512011-09-07 11:43:16 -0700962 if (tries_size == 0) {
963 return true;
964 }
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -0800965 const uint32_t insns_size = code_item_accessor_.InsnsSizeInCodeUnits();
966 for (const DexFile::TryItem& try_item : code_item_accessor_.TryItems()) {
967 const uint32_t start = try_item.start_addr_;
968 const uint32_t end = start + try_item.insn_count_;
jeffhaobdb76512011-09-07 11:43:16 -0700969 if ((start >= end) || (start >= insns_size) || (end > insns_size)) {
jeffhaod5347e02012-03-22 17:25:05 -0700970 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad exception entry: startAddr=" << start
971 << " endAddr=" << end << " (size=" << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700972 return false;
973 }
Mathieu Chartierde40d472015-10-15 17:47:48 -0700974 if (!GetInstructionFlags(start).IsOpcode()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700975 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
976 << "'try' block starts inside an instruction (" << start << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700977 return false;
978 }
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -0800979 DexInstructionIterator end_it(code_item_accessor_.Insns(), end);
980 for (DexInstructionIterator it(code_item_accessor_.Insns(), start); it < end_it; ++it) {
981 GetInstructionFlags(it.DexPc()).SetInTry();
jeffhaobdb76512011-09-07 11:43:16 -0700982 }
983 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800984 // Iterate over each of the handlers to verify target addresses.
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -0800985 const uint8_t* handlers_ptr = code_item_accessor_.GetCatchHandlerData();
986 const uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700987 ClassLinker* linker = Runtime::Current()->GetClassLinker();
jeffhaobdb76512011-09-07 11:43:16 -0700988 for (uint32_t idx = 0; idx < handlers_size; idx++) {
Ian Rogers0571d352011-11-03 19:51:38 -0700989 CatchHandlerIterator iterator(handlers_ptr);
990 for (; iterator.HasNext(); iterator.Next()) {
Igor Murashkin2ffb7032017-11-08 13:35:21 -0800991 uint32_t dex_pc = iterator.GetHandlerAddress();
Mathieu Chartierde40d472015-10-15 17:47:48 -0700992 if (!GetInstructionFlags(dex_pc).IsOpcode()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700993 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
994 << "exception handler starts at bad address (" << dex_pc << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700995 return false;
996 }
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -0800997 if (!CheckNotMoveResult(code_item_accessor_.Insns(), dex_pc)) {
Stephen Kyle9bc61992014-09-22 13:53:15 +0100998 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
999 << "exception handler begins with move-result* (" << dex_pc << ")";
1000 return false;
1001 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07001002 GetInstructionFlags(dex_pc).SetBranchTarget();
Ian Rogers28ad40d2011-10-27 15:19:26 -07001003 // Ensure exception types are resolved so that they don't need resolution to be delivered,
1004 // unresolved exception types will be ignored by exception delivery
Andreas Gampea5b09a62016-11-17 15:21:22 -08001005 if (iterator.GetHandlerTypeIndex().IsValid()) {
Vladimir Marko666ee3d2017-12-11 18:37:36 +00001006 ObjPtr<mirror::Class> exception_type =
1007 linker->ResolveType(iterator.GetHandlerTypeIndex(), dex_cache_, class_loader_);
Ian Rogers7b078e82014-09-10 14:44:24 -07001008 if (exception_type == nullptr) {
1009 DCHECK(self_->IsExceptionPending());
1010 self_->ClearException();
Ian Rogers28ad40d2011-10-27 15:19:26 -07001011 }
1012 }
jeffhaobdb76512011-09-07 11:43:16 -07001013 }
Ian Rogers0571d352011-11-03 19:51:38 -07001014 handlers_ptr = iterator.EndDataPointer();
jeffhaobdb76512011-09-07 11:43:16 -07001015 }
jeffhaobdb76512011-09-07 11:43:16 -07001016 return true;
1017}
1018
Andreas Gampebf1cb772017-05-15 15:39:00 -07001019template <bool kAllowRuntimeOnlyInstructions>
Ian Rogers776ac1f2012-04-13 23:36:36 -07001020bool MethodVerifier::VerifyInstructions() {
Ian Rogers0c7abda2012-09-19 13:33:42 -07001021 /* 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 -07001022 GetInstructionFlags(0).SetBranchTarget();
1023 GetInstructionFlags(0).SetCompileTimeInfoPoint();
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001024 for (const DexInstructionPcPair& inst : code_item_accessor_) {
Mathieu Chartier2b2bef22017-10-26 17:10:19 -07001025 const uint32_t dex_pc = inst.DexPc();
1026 if (!VerifyInstruction<kAllowRuntimeOnlyInstructions>(&inst.Inst(), dex_pc)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001027 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -07001028 return false;
1029 }
1030 /* Flag instructions that are garbage collection points */
Sameer Abu Asal02c42232013-04-30 12:09:45 -07001031 // All invoke points are marked as "Throw" points already.
1032 // We are relying on this to also count all the invokes as interesting.
Vladimir Marko8b858e12014-11-27 14:52:37 +00001033 if (inst->IsBranch()) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07001034 GetInstructionFlags(dex_pc).SetCompileTimeInfoPoint();
Vladimir Marko8b858e12014-11-27 14:52:37 +00001035 // The compiler also needs safepoints for fall-through to loop heads.
1036 // Such a loop head must be a target of a branch.
1037 int32_t offset = 0;
1038 bool cond, self_ok;
1039 bool target_ok = GetBranchOffset(dex_pc, &offset, &cond, &self_ok);
1040 DCHECK(target_ok);
Mathieu Chartierde40d472015-10-15 17:47:48 -07001041 GetInstructionFlags(dex_pc + offset).SetCompileTimeInfoPoint();
Vladimir Marko8b858e12014-11-27 14:52:37 +00001042 } else if (inst->IsSwitch() || inst->IsThrow()) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07001043 GetInstructionFlags(dex_pc).SetCompileTimeInfoPoint();
Ian Rogersb8c78592013-07-25 23:52:52 +00001044 } else if (inst->IsReturn()) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07001045 GetInstructionFlags(dex_pc).SetCompileTimeInfoPointAndReturn();
Ian Rogersd81871c2011-10-03 13:57:23 -07001046 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001047 }
1048 return true;
1049}
1050
Andreas Gampebf1cb772017-05-15 15:39:00 -07001051template <bool kAllowRuntimeOnlyInstructions>
1052bool MethodVerifier::VerifyInstruction(const Instruction* inst, uint32_t code_offset) {
Andreas Gampee05cc662017-05-15 10:17:30 -07001053 if (Instruction::kHaveExperimentalInstructions && UNLIKELY(inst->IsExperimental())) {
Igor Murashkin4d7b75f2015-07-21 17:03:36 -07001054 // Experimental instructions don't yet have verifier support implementation.
1055 // While it is possible to use them by themselves, when we try to use stable instructions
1056 // with a virtual register that was created by an experimental instruction,
1057 // the data flow analysis will fail.
1058 Fail(VERIFY_ERROR_FORCE_INTERPRETER)
1059 << "experimental instruction is not supported by verifier; skipping verification";
1060 have_pending_experimental_failure_ = true;
1061 return false;
1062 }
1063
Ian Rogersd81871c2011-10-03 13:57:23 -07001064 bool result = true;
1065 switch (inst->GetVerifyTypeArgumentA()) {
1066 case Instruction::kVerifyRegA:
Ian Rogers29a26482014-05-02 15:27:29 -07001067 result = result && CheckRegisterIndex(inst->VRegA());
Ian Rogersd81871c2011-10-03 13:57:23 -07001068 break;
1069 case Instruction::kVerifyRegAWide:
Ian Rogers29a26482014-05-02 15:27:29 -07001070 result = result && CheckWideRegisterIndex(inst->VRegA());
Ian Rogersd81871c2011-10-03 13:57:23 -07001071 break;
1072 }
1073 switch (inst->GetVerifyTypeArgumentB()) {
1074 case Instruction::kVerifyRegB:
Ian Rogers29a26482014-05-02 15:27:29 -07001075 result = result && CheckRegisterIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -07001076 break;
1077 case Instruction::kVerifyRegBField:
Ian Rogers29a26482014-05-02 15:27:29 -07001078 result = result && CheckFieldIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -07001079 break;
1080 case Instruction::kVerifyRegBMethod:
Ian Rogers29a26482014-05-02 15:27:29 -07001081 result = result && CheckMethodIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -07001082 break;
1083 case Instruction::kVerifyRegBNewInstance:
Andreas Gampea5b09a62016-11-17 15:21:22 -08001084 result = result && CheckNewInstance(dex::TypeIndex(inst->VRegB()));
Ian Rogersd81871c2011-10-03 13:57:23 -07001085 break;
1086 case Instruction::kVerifyRegBString:
Ian Rogers29a26482014-05-02 15:27:29 -07001087 result = result && CheckStringIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -07001088 break;
1089 case Instruction::kVerifyRegBType:
Andreas Gampea5b09a62016-11-17 15:21:22 -08001090 result = result && CheckTypeIndex(dex::TypeIndex(inst->VRegB()));
Ian Rogersd81871c2011-10-03 13:57:23 -07001091 break;
1092 case Instruction::kVerifyRegBWide:
Ian Rogers29a26482014-05-02 15:27:29 -07001093 result = result && CheckWideRegisterIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -07001094 break;
Orion Hodson2e599942017-09-22 16:17:41 +01001095 case Instruction::kVerifyRegBCallSite:
1096 result = result && CheckCallSiteIndex(inst->VRegB());
1097 break;
1098 case Instruction::kVerifyRegBMethodHandle:
1099 result = result && CheckMethodHandleIndex(inst->VRegB());
1100 break;
1101 case Instruction::kVerifyRegBPrototype:
1102 result = result && CheckPrototypeIndex(inst->VRegB());
1103 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001104 }
1105 switch (inst->GetVerifyTypeArgumentC()) {
1106 case Instruction::kVerifyRegC:
Ian Rogers29a26482014-05-02 15:27:29 -07001107 result = result && CheckRegisterIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -07001108 break;
1109 case Instruction::kVerifyRegCField:
Ian Rogers29a26482014-05-02 15:27:29 -07001110 result = result && CheckFieldIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -07001111 break;
1112 case Instruction::kVerifyRegCNewArray:
Andreas Gampea5b09a62016-11-17 15:21:22 -08001113 result = result && CheckNewArray(dex::TypeIndex(inst->VRegC()));
Ian Rogersd81871c2011-10-03 13:57:23 -07001114 break;
1115 case Instruction::kVerifyRegCType:
Andreas Gampea5b09a62016-11-17 15:21:22 -08001116 result = result && CheckTypeIndex(dex::TypeIndex(inst->VRegC()));
Ian Rogersd81871c2011-10-03 13:57:23 -07001117 break;
1118 case Instruction::kVerifyRegCWide:
Ian Rogers29a26482014-05-02 15:27:29 -07001119 result = result && CheckWideRegisterIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -07001120 break;
1121 }
Orion Hodsoncfa325e2016-10-13 10:25:54 +01001122 switch (inst->GetVerifyTypeArgumentH()) {
1123 case Instruction::kVerifyRegHPrototype:
1124 result = result && CheckPrototypeIndex(inst->VRegH());
1125 break;
1126 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001127 switch (inst->GetVerifyExtraFlags()) {
1128 case Instruction::kVerifyArrayData:
1129 result = result && CheckArrayData(code_offset);
1130 break;
1131 case Instruction::kVerifyBranchTarget:
1132 result = result && CheckBranchTarget(code_offset);
1133 break;
1134 case Instruction::kVerifySwitchTargets:
1135 result = result && CheckSwitchTargets(code_offset);
1136 break;
Andreas Gampec3314312014-06-19 18:13:29 -07001137 case Instruction::kVerifyVarArgNonZero:
1138 // Fall-through.
Ian Rogers29a26482014-05-02 15:27:29 -07001139 case Instruction::kVerifyVarArg: {
Taiju Tsuiki29498a22015-04-13 14:21:00 +09001140 // Instructions that can actually return a negative value shouldn't have this flag.
1141 uint32_t v_a = dchecked_integral_cast<uint32_t>(inst->VRegA());
1142 if ((inst->GetVerifyExtraFlags() == Instruction::kVerifyVarArgNonZero && v_a == 0) ||
1143 v_a > Instruction::kMaxVarArgRegs) {
1144 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << v_a << ") in "
Andreas Gampec3314312014-06-19 18:13:29 -07001145 "non-range invoke";
1146 return false;
1147 }
Taiju Tsuiki29498a22015-04-13 14:21:00 +09001148
Ian Rogers29a26482014-05-02 15:27:29 -07001149 uint32_t args[Instruction::kMaxVarArgRegs];
1150 inst->GetVarArgs(args);
Taiju Tsuiki29498a22015-04-13 14:21:00 +09001151 result = result && CheckVarArgRegs(v_a, args);
Ian Rogersd81871c2011-10-03 13:57:23 -07001152 break;
Ian Rogers29a26482014-05-02 15:27:29 -07001153 }
Andreas Gampec3314312014-06-19 18:13:29 -07001154 case Instruction::kVerifyVarArgRangeNonZero:
1155 // Fall-through.
Ian Rogersd81871c2011-10-03 13:57:23 -07001156 case Instruction::kVerifyVarArgRange:
Andreas Gampec3314312014-06-19 18:13:29 -07001157 if (inst->GetVerifyExtraFlags() == Instruction::kVerifyVarArgRangeNonZero &&
1158 inst->VRegA() <= 0) {
1159 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << inst->VRegA() << ") in "
1160 "range invoke";
1161 return false;
1162 }
Ian Rogers29a26482014-05-02 15:27:29 -07001163 result = result && CheckVarArgRangeRegs(inst->VRegA(), inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -07001164 break;
1165 case Instruction::kVerifyError:
jeffhaod5347e02012-03-22 17:25:05 -07001166 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected opcode " << inst->Name();
Ian Rogersd81871c2011-10-03 13:57:23 -07001167 result = false;
1168 break;
1169 }
Andreas Gampebf1cb772017-05-15 15:39:00 -07001170 if (!kAllowRuntimeOnlyInstructions && inst->GetVerifyIsRuntimeOnly()) {
Ian Rogers5fb22a92014-06-13 10:31:28 -07001171 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "opcode only expected at runtime " << inst->Name();
1172 result = false;
1173 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001174 return result;
1175}
1176
Ian Rogers7b078e82014-09-10 14:44:24 -07001177inline bool MethodVerifier::CheckRegisterIndex(uint32_t idx) {
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001178 if (UNLIKELY(idx >= code_item_accessor_.RegistersSize())) {
jeffhaod5347e02012-03-22 17:25:05 -07001179 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register index out of range (" << idx << " >= "
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001180 << code_item_accessor_.RegistersSize() << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001181 return false;
1182 }
1183 return true;
1184}
1185
Ian Rogers7b078e82014-09-10 14:44:24 -07001186inline bool MethodVerifier::CheckWideRegisterIndex(uint32_t idx) {
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001187 if (UNLIKELY(idx + 1 >= code_item_accessor_.RegistersSize())) {
jeffhaod5347e02012-03-22 17:25:05 -07001188 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "wide register index out of range (" << idx
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001189 << "+1 >= " << code_item_accessor_.RegistersSize() << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001190 return false;
1191 }
1192 return true;
1193}
1194
Orion Hodson2e599942017-09-22 16:17:41 +01001195inline bool MethodVerifier::CheckCallSiteIndex(uint32_t idx) {
1196 uint32_t limit = dex_file_->NumCallSiteIds();
1197 if (UNLIKELY(idx >= limit)) {
1198 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad call site index " << idx << " (max "
1199 << limit << ")";
1200 return false;
1201 }
1202 return true;
1203}
1204
Ian Rogers7b078e82014-09-10 14:44:24 -07001205inline bool MethodVerifier::CheckFieldIndex(uint32_t idx) {
Andreas Gampe29e81422017-05-15 16:29:32 -07001206 if (UNLIKELY(idx >= dex_file_->GetHeader().field_ids_size_)) {
jeffhaod5347e02012-03-22 17:25:05 -07001207 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad field index " << idx << " (max "
1208 << dex_file_->GetHeader().field_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001209 return false;
1210 }
1211 return true;
1212}
1213
Ian Rogers7b078e82014-09-10 14:44:24 -07001214inline bool MethodVerifier::CheckMethodIndex(uint32_t idx) {
Andreas Gampe29e81422017-05-15 16:29:32 -07001215 if (UNLIKELY(idx >= dex_file_->GetHeader().method_ids_size_)) {
jeffhaod5347e02012-03-22 17:25:05 -07001216 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad method index " << idx << " (max "
1217 << dex_file_->GetHeader().method_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001218 return false;
1219 }
1220 return true;
1221}
1222
Orion Hodson2e599942017-09-22 16:17:41 +01001223inline bool MethodVerifier::CheckMethodHandleIndex(uint32_t idx) {
1224 uint32_t limit = dex_file_->NumMethodHandles();
1225 if (UNLIKELY(idx >= limit)) {
1226 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad method handle index " << idx << " (max "
1227 << limit << ")";
1228 return false;
1229 }
1230 return true;
1231}
1232
Andreas Gampea5b09a62016-11-17 15:21:22 -08001233inline bool MethodVerifier::CheckNewInstance(dex::TypeIndex idx) {
Andreas Gampe29e81422017-05-15 16:29:32 -07001234 if (UNLIKELY(idx.index_ >= dex_file_->GetHeader().type_ids_size_)) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08001235 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx.index_ << " (max "
jeffhaod5347e02012-03-22 17:25:05 -07001236 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001237 return false;
1238 }
1239 // We don't need the actual class, just a pointer to the class name.
Ian Rogers0571d352011-11-03 19:51:38 -07001240 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Andreas Gampe29e81422017-05-15 16:29:32 -07001241 if (UNLIKELY(descriptor[0] != 'L')) {
jeffhaod5347e02012-03-22 17:25:05 -07001242 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "can't call new-instance on type '" << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001243 return false;
Andreas Gampe29e81422017-05-15 16:29:32 -07001244 } else if (UNLIKELY(strcmp(descriptor, "Ljava/lang/Class;") == 0)) {
Aart Bikdb698f12016-07-25 17:52:22 -07001245 // An unlikely new instance on Class is not allowed. Fall back to interpreter to ensure an
1246 // exception is thrown when this statement is executed (compiled code would not do that).
1247 Fail(VERIFY_ERROR_INSTANTIATION);
Ian Rogersd81871c2011-10-03 13:57:23 -07001248 }
1249 return true;
1250}
1251
Orion Hodsoncfa325e2016-10-13 10:25:54 +01001252inline bool MethodVerifier::CheckPrototypeIndex(uint32_t idx) {
Andreas Gampe29e81422017-05-15 16:29:32 -07001253 if (UNLIKELY(idx >= dex_file_->GetHeader().proto_ids_size_)) {
Orion Hodsoncfa325e2016-10-13 10:25:54 +01001254 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad prototype index " << idx << " (max "
1255 << dex_file_->GetHeader().proto_ids_size_ << ")";
1256 return false;
1257 }
1258 return true;
1259}
1260
Ian Rogers7b078e82014-09-10 14:44:24 -07001261inline bool MethodVerifier::CheckStringIndex(uint32_t idx) {
Andreas Gampe29e81422017-05-15 16:29:32 -07001262 if (UNLIKELY(idx >= dex_file_->GetHeader().string_ids_size_)) {
jeffhaod5347e02012-03-22 17:25:05 -07001263 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad string index " << idx << " (max "
1264 << dex_file_->GetHeader().string_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001265 return false;
1266 }
1267 return true;
1268}
1269
Andreas Gampea5b09a62016-11-17 15:21:22 -08001270inline bool MethodVerifier::CheckTypeIndex(dex::TypeIndex idx) {
Andreas Gampe29e81422017-05-15 16:29:32 -07001271 if (UNLIKELY(idx.index_ >= dex_file_->GetHeader().type_ids_size_)) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08001272 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx.index_ << " (max "
jeffhaod5347e02012-03-22 17:25:05 -07001273 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001274 return false;
1275 }
1276 return true;
1277}
1278
Andreas Gampea5b09a62016-11-17 15:21:22 -08001279bool MethodVerifier::CheckNewArray(dex::TypeIndex idx) {
Andreas Gampe29e81422017-05-15 16:29:32 -07001280 if (UNLIKELY(idx.index_ >= dex_file_->GetHeader().type_ids_size_)) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08001281 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx.index_ << " (max "
jeffhaod5347e02012-03-22 17:25:05 -07001282 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001283 return false;
1284 }
1285 int bracket_count = 0;
Ian Rogers0571d352011-11-03 19:51:38 -07001286 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -07001287 const char* cp = descriptor;
1288 while (*cp++ == '[') {
1289 bracket_count++;
1290 }
Andreas Gampe29e81422017-05-15 16:29:32 -07001291 if (UNLIKELY(bracket_count == 0)) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001292 /* The given class must be an array type. */
Brian Carlstrom93c33962013-07-26 10:37:43 -07001293 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1294 << "can't new-array class '" << descriptor << "' (not an array)";
Ian Rogersd81871c2011-10-03 13:57:23 -07001295 return false;
Andreas Gampe29e81422017-05-15 16:29:32 -07001296 } else if (UNLIKELY(bracket_count > 255)) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001297 /* It is illegal to create an array of more than 255 dimensions. */
Brian Carlstrom93c33962013-07-26 10:37:43 -07001298 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1299 << "can't new-array class '" << descriptor << "' (exceeds limit)";
Ian Rogersd81871c2011-10-03 13:57:23 -07001300 return false;
1301 }
1302 return true;
1303}
1304
Ian Rogers776ac1f2012-04-13 23:36:36 -07001305bool MethodVerifier::CheckArrayData(uint32_t cur_offset) {
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001306 const uint32_t insn_count = code_item_accessor_.InsnsSizeInCodeUnits();
1307 const uint16_t* insns = code_item_accessor_.Insns() + cur_offset;
Ian Rogersd81871c2011-10-03 13:57:23 -07001308 const uint16_t* array_data;
1309 int32_t array_data_offset;
1310
1311 DCHECK_LT(cur_offset, insn_count);
1312 /* make sure the start of the array data table is in range */
Andreas Gampe53de99c2015-08-17 13:43:55 -07001313 array_data_offset = insns[1] | (static_cast<int32_t>(insns[2]) << 16);
Andreas Gampe29e81422017-05-15 16:29:32 -07001314 if (UNLIKELY(static_cast<int32_t>(cur_offset) + array_data_offset < 0 ||
1315 cur_offset + array_data_offset + 2 >= insn_count)) {
jeffhaod5347e02012-03-22 17:25:05 -07001316 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data start: at " << cur_offset
Brian Carlstrom93c33962013-07-26 10:37:43 -07001317 << ", data offset " << array_data_offset
1318 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -07001319 return false;
1320 }
1321 /* offset to array data table is a relative branch-style offset */
1322 array_data = insns + array_data_offset;
Andreas Gampe57c47582015-07-01 22:05:59 -07001323 // Make sure the table is at an even dex pc, that is, 32-bit aligned.
Andreas Gampe29e81422017-05-15 16:29:32 -07001324 if (UNLIKELY(!IsAligned<4>(array_data))) {
jeffhaod5347e02012-03-22 17:25:05 -07001325 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned array data table: at " << cur_offset
1326 << ", data offset " << array_data_offset;
Ian Rogersd81871c2011-10-03 13:57:23 -07001327 return false;
1328 }
Andreas Gampe57c47582015-07-01 22:05:59 -07001329 // Make sure the array-data is marked as an opcode. This ensures that it was reached when
1330 // traversing the code item linearly. It is an approximation for a by-spec padding value.
Andreas Gampe29e81422017-05-15 16:29:32 -07001331 if (UNLIKELY(!GetInstructionFlags(cur_offset + array_data_offset).IsOpcode())) {
Andreas Gampe57c47582015-07-01 22:05:59 -07001332 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array data table at " << cur_offset
1333 << ", data offset " << array_data_offset
1334 << " not correctly visited, probably bad padding.";
1335 return false;
1336 }
1337
Ian Rogersd81871c2011-10-03 13:57:23 -07001338 uint32_t value_width = array_data[1];
Elliott Hughes398f64b2012-03-26 18:05:48 -07001339 uint32_t value_count = *reinterpret_cast<const uint32_t*>(&array_data[2]);
Ian Rogersd81871c2011-10-03 13:57:23 -07001340 uint32_t table_size = 4 + (value_width * value_count + 1) / 2;
1341 /* make sure the end of the switch is in range */
Andreas Gampe29e81422017-05-15 16:29:32 -07001342 if (UNLIKELY(cur_offset + array_data_offset + table_size > insn_count)) {
jeffhaod5347e02012-03-22 17:25:05 -07001343 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data end: at " << cur_offset
1344 << ", data offset " << array_data_offset << ", end "
1345 << cur_offset + array_data_offset + table_size
1346 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -07001347 return false;
1348 }
1349 return true;
1350}
1351
Ian Rogers776ac1f2012-04-13 23:36:36 -07001352bool MethodVerifier::CheckBranchTarget(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001353 int32_t offset;
1354 bool isConditional, selfOkay;
1355 if (!GetBranchOffset(cur_offset, &offset, &isConditional, &selfOkay)) {
1356 return false;
1357 }
Andreas Gampe29e81422017-05-15 16:29:32 -07001358 if (UNLIKELY(!selfOkay && offset == 0)) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001359 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch offset of zero not allowed at"
1360 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -07001361 return false;
1362 }
Elliott Hughes81ff3182012-03-23 20:35:56 -07001363 // Check for 32-bit overflow. This isn't strictly necessary if we can depend on the runtime
1364 // to have identical "wrap-around" behavior, but it's unwise to depend on that.
Andreas Gampe29e81422017-05-15 16:29:32 -07001365 if (UNLIKELY(((int64_t) cur_offset + (int64_t) offset) != (int64_t) (cur_offset + offset))) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001366 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch target overflow "
1367 << reinterpret_cast<void*>(cur_offset) << " +" << offset;
Ian Rogersd81871c2011-10-03 13:57:23 -07001368 return false;
1369 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001370 int32_t abs_offset = cur_offset + offset;
Andreas Gampe29e81422017-05-15 16:29:32 -07001371 if (UNLIKELY(abs_offset < 0 ||
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001372 (uint32_t) abs_offset >= code_item_accessor_.InsnsSizeInCodeUnits() ||
Andreas Gampe29e81422017-05-15 16:29:32 -07001373 !GetInstructionFlags(abs_offset).IsOpcode())) {
jeffhaod5347e02012-03-22 17:25:05 -07001374 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid branch target " << offset << " (-> "
Elliott Hughes398f64b2012-03-26 18:05:48 -07001375 << reinterpret_cast<void*>(abs_offset) << ") at "
1376 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -07001377 return false;
1378 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07001379 GetInstructionFlags(abs_offset).SetBranchTarget();
Ian Rogersd81871c2011-10-03 13:57:23 -07001380 return true;
1381}
1382
Ian Rogers776ac1f2012-04-13 23:36:36 -07001383bool MethodVerifier::GetBranchOffset(uint32_t cur_offset, int32_t* pOffset, bool* pConditional,
Ian Rogersd81871c2011-10-03 13:57:23 -07001384 bool* selfOkay) {
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001385 const uint16_t* insns = code_item_accessor_.Insns() + cur_offset;
Ian Rogersd81871c2011-10-03 13:57:23 -07001386 *pConditional = false;
1387 *selfOkay = false;
jeffhaoba5ebb92011-08-25 17:24:37 -07001388 switch (*insns & 0xff) {
1389 case Instruction::GOTO:
1390 *pOffset = ((int16_t) *insns) >> 8;
jeffhaoba5ebb92011-08-25 17:24:37 -07001391 break;
1392 case Instruction::GOTO_32:
1393 *pOffset = insns[1] | (((uint32_t) insns[2]) << 16);
jeffhaoba5ebb92011-08-25 17:24:37 -07001394 *selfOkay = true;
1395 break;
1396 case Instruction::GOTO_16:
1397 *pOffset = (int16_t) insns[1];
jeffhaoba5ebb92011-08-25 17:24:37 -07001398 break;
1399 case Instruction::IF_EQ:
1400 case Instruction::IF_NE:
1401 case Instruction::IF_LT:
1402 case Instruction::IF_GE:
1403 case Instruction::IF_GT:
1404 case Instruction::IF_LE:
1405 case Instruction::IF_EQZ:
1406 case Instruction::IF_NEZ:
1407 case Instruction::IF_LTZ:
1408 case Instruction::IF_GEZ:
1409 case Instruction::IF_GTZ:
1410 case Instruction::IF_LEZ:
1411 *pOffset = (int16_t) insns[1];
1412 *pConditional = true;
jeffhaoba5ebb92011-08-25 17:24:37 -07001413 break;
1414 default:
1415 return false;
jeffhaoba5ebb92011-08-25 17:24:37 -07001416 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001417 return true;
1418}
1419
Ian Rogers776ac1f2012-04-13 23:36:36 -07001420bool MethodVerifier::CheckSwitchTargets(uint32_t cur_offset) {
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001421 const uint32_t insn_count = code_item_accessor_.InsnsSizeInCodeUnits();
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001422 DCHECK_LT(cur_offset, insn_count);
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001423 const uint16_t* insns = code_item_accessor_.Insns() + cur_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001424 /* make sure the start of the switch is in range */
Andreas Gampe53de99c2015-08-17 13:43:55 -07001425 int32_t switch_offset = insns[1] | (static_cast<int32_t>(insns[2]) << 16);
Andreas Gampe29e81422017-05-15 16:29:32 -07001426 if (UNLIKELY(static_cast<int32_t>(cur_offset) + switch_offset < 0 ||
1427 cur_offset + switch_offset + 2 > insn_count)) {
jeffhaod5347e02012-03-22 17:25:05 -07001428 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch start: at " << cur_offset
Brian Carlstrom93c33962013-07-26 10:37:43 -07001429 << ", switch offset " << switch_offset
1430 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001431 return false;
1432 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001433 /* offset to switch table is a relative branch-style offset */
Ian Rogersd81871c2011-10-03 13:57:23 -07001434 const uint16_t* switch_insns = insns + switch_offset;
Andreas Gampe57c47582015-07-01 22:05:59 -07001435 // Make sure the table is at an even dex pc, that is, 32-bit aligned.
Andreas Gampe29e81422017-05-15 16:29:32 -07001436 if (UNLIKELY(!IsAligned<4>(switch_insns))) {
jeffhaod5347e02012-03-22 17:25:05 -07001437 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned switch table: at " << cur_offset
1438 << ", switch offset " << switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001439 return false;
1440 }
Andreas Gampe57c47582015-07-01 22:05:59 -07001441 // Make sure the switch data is marked as an opcode. This ensures that it was reached when
1442 // traversing the code item linearly. It is an approximation for a by-spec padding value.
Andreas Gampe29e81422017-05-15 16:29:32 -07001443 if (UNLIKELY(!GetInstructionFlags(cur_offset + switch_offset).IsOpcode())) {
Andreas Gampe57c47582015-07-01 22:05:59 -07001444 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "switch table at " << cur_offset
1445 << ", switch offset " << switch_offset
1446 << " not correctly visited, probably bad padding.";
1447 return false;
1448 }
1449
David Brazdil5469d342015-09-25 16:57:53 +01001450 bool is_packed_switch = (*insns & 0xff) == Instruction::PACKED_SWITCH;
1451
Ian Rogersd81871c2011-10-03 13:57:23 -07001452 uint32_t switch_count = switch_insns[1];
David Brazdil5469d342015-09-25 16:57:53 +01001453 int32_t targets_offset;
Ian Rogersd81871c2011-10-03 13:57:23 -07001454 uint16_t expected_signature;
David Brazdil5469d342015-09-25 16:57:53 +01001455 if (is_packed_switch) {
jeffhaoba5ebb92011-08-25 17:24:37 -07001456 /* 0=sig, 1=count, 2/3=firstKey */
1457 targets_offset = 4;
jeffhaoba5ebb92011-08-25 17:24:37 -07001458 expected_signature = Instruction::kPackedSwitchSignature;
1459 } else {
1460 /* 0=sig, 1=count, 2..count*2 = keys */
jeffhaoba5ebb92011-08-25 17:24:37 -07001461 targets_offset = 2 + 2 * switch_count;
1462 expected_signature = Instruction::kSparseSwitchSignature;
1463 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001464 uint32_t table_size = targets_offset + switch_count * 2;
Andreas Gampe29e81422017-05-15 16:29:32 -07001465 if (UNLIKELY(switch_insns[0] != expected_signature)) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001466 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1467 << StringPrintf("wrong signature for switch table (%x, wanted %x)",
1468 switch_insns[0], expected_signature);
jeffhaoba5ebb92011-08-25 17:24:37 -07001469 return false;
1470 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001471 /* make sure the end of the switch is in range */
Andreas Gampe29e81422017-05-15 16:29:32 -07001472 if (UNLIKELY(cur_offset + switch_offset + table_size > (uint32_t) insn_count)) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001473 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch end: at " << cur_offset
1474 << ", switch offset " << switch_offset
1475 << ", end " << (cur_offset + switch_offset + table_size)
jeffhaod5347e02012-03-22 17:25:05 -07001476 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001477 return false;
1478 }
David Brazdil5469d342015-09-25 16:57:53 +01001479
1480 constexpr int32_t keys_offset = 2;
1481 if (switch_count > 1) {
1482 if (is_packed_switch) {
1483 /* for a packed switch, verify that keys do not overflow int32 */
1484 int32_t first_key = switch_insns[keys_offset] | (switch_insns[keys_offset + 1] << 16);
1485 int32_t max_first_key =
1486 std::numeric_limits<int32_t>::max() - (static_cast<int32_t>(switch_count) - 1);
Andreas Gampe29e81422017-05-15 16:29:32 -07001487 if (UNLIKELY(first_key > max_first_key)) {
David Brazdil5469d342015-09-25 16:57:53 +01001488 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid packed switch: first_key=" << first_key
1489 << ", switch_count=" << switch_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001490 return false;
1491 }
David Brazdil5469d342015-09-25 16:57:53 +01001492 } else {
1493 /* for a sparse switch, verify the keys are in ascending order */
1494 int32_t last_key = switch_insns[keys_offset] | (switch_insns[keys_offset + 1] << 16);
1495 for (uint32_t targ = 1; targ < switch_count; targ++) {
1496 int32_t key =
1497 static_cast<int32_t>(switch_insns[keys_offset + targ * 2]) |
1498 static_cast<int32_t>(switch_insns[keys_offset + targ * 2 + 1] << 16);
Andreas Gampe29e81422017-05-15 16:29:32 -07001499 if (UNLIKELY(key <= last_key)) {
David Brazdil5469d342015-09-25 16:57:53 +01001500 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid sparse switch: last key=" << last_key
1501 << ", this=" << key;
1502 return false;
1503 }
1504 last_key = key;
1505 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001506 }
1507 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001508 /* verify each switch target */
Ian Rogersd81871c2011-10-03 13:57:23 -07001509 for (uint32_t targ = 0; targ < switch_count; targ++) {
Andreas Gampe53de99c2015-08-17 13:43:55 -07001510 int32_t offset = static_cast<int32_t>(switch_insns[targets_offset + targ * 2]) |
1511 static_cast<int32_t>(switch_insns[targets_offset + targ * 2 + 1] << 16);
Ian Rogersd81871c2011-10-03 13:57:23 -07001512 int32_t abs_offset = cur_offset + offset;
Andreas Gampe29e81422017-05-15 16:29:32 -07001513 if (UNLIKELY(abs_offset < 0 ||
1514 abs_offset >= static_cast<int32_t>(insn_count) ||
1515 !GetInstructionFlags(abs_offset).IsOpcode())) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001516 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch target " << offset
1517 << " (-> " << reinterpret_cast<void*>(abs_offset) << ") at "
1518 << reinterpret_cast<void*>(cur_offset)
1519 << "[" << targ << "]";
jeffhaoba5ebb92011-08-25 17:24:37 -07001520 return false;
1521 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07001522 GetInstructionFlags(abs_offset).SetBranchTarget();
Ian Rogersd81871c2011-10-03 13:57:23 -07001523 }
1524 return true;
1525}
1526
Ian Rogers776ac1f2012-04-13 23:36:36 -07001527bool MethodVerifier::CheckVarArgRegs(uint32_t vA, uint32_t arg[]) {
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001528 uint16_t registers_size = code_item_accessor_.RegistersSize();
Ian Rogersd81871c2011-10-03 13:57:23 -07001529 for (uint32_t idx = 0; idx < vA; idx++) {
Andreas Gampe29e81422017-05-15 16:29:32 -07001530 if (UNLIKELY(arg[idx] >= registers_size)) {
jeffhaod5347e02012-03-22 17:25:05 -07001531 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index (" << arg[idx]
1532 << ") in non-range invoke (>= " << registers_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001533 return false;
1534 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001535 }
1536
1537 return true;
1538}
1539
Ian Rogers776ac1f2012-04-13 23:36:36 -07001540bool MethodVerifier::CheckVarArgRangeRegs(uint32_t vA, uint32_t vC) {
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001541 uint16_t registers_size = code_item_accessor_.RegistersSize();
Ian Rogersd81871c2011-10-03 13:57:23 -07001542 // vA/vC are unsigned 8-bit/16-bit quantities for /range instructions, so there's no risk of
1543 // integer overflow when adding them here.
Andreas Gampe29e81422017-05-15 16:29:32 -07001544 if (UNLIKELY(vA + vC > registers_size)) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001545 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index " << vA << "+" << vC
1546 << " in range invoke (> " << registers_size << ")";
jeffhaoba5ebb92011-08-25 17:24:37 -07001547 return false;
1548 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001549 return true;
1550}
1551
Ian Rogers776ac1f2012-04-13 23:36:36 -07001552bool MethodVerifier::VerifyCodeFlow() {
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001553 const uint16_t registers_size = code_item_accessor_.RegistersSize();
jeffhaobdb76512011-09-07 11:43:16 -07001554
Ian Rogersd81871c2011-10-03 13:57:23 -07001555 /* Create and initialize table holding register status */
Brian Carlstrom93c33962013-07-26 10:37:43 -07001556 reg_table_.Init(kTrackCompilerInterestPoints,
1557 insn_flags_.get(),
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001558 code_item_accessor_.InsnsSizeInCodeUnits(),
Brian Carlstrom93c33962013-07-26 10:37:43 -07001559 registers_size,
1560 this);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07001561
Ian Rogersd0fbd852013-09-24 18:17:04 -07001562 work_line_.reset(RegisterLine::Create(registers_size, this));
1563 saved_line_.reset(RegisterLine::Create(registers_size, this));
jeffhaobdb76512011-09-07 11:43:16 -07001564
Ian Rogersd81871c2011-10-03 13:57:23 -07001565 /* Initialize register types of method arguments. */
1566 if (!SetTypesFromSignature()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001567 DCHECK_NE(failures_.size(), 0U);
1568 std::string prepend("Bad signature in ");
David Sehr709b0702016-10-13 09:12:37 -07001569 prepend += dex_file_->PrettyMethod(dex_method_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001570 PrependToLastFailMessage(prepend);
Ian Rogersd81871c2011-10-03 13:57:23 -07001571 return false;
1572 }
Andreas Gamped5ad72f2015-06-26 17:33:47 -07001573 // We may have a runtime failure here, clear.
1574 have_pending_runtime_throw_failure_ = false;
1575
Ian Rogersd81871c2011-10-03 13:57:23 -07001576 /* Perform code flow verification. */
1577 if (!CodeFlowVerifyMethod()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001578 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -07001579 return false;
jeffhaobdb76512011-09-07 11:43:16 -07001580 }
jeffhaobdb76512011-09-07 11:43:16 -07001581 return true;
1582}
1583
Ian Rogersad0b3a32012-04-16 14:50:24 -07001584std::ostream& MethodVerifier::DumpFailures(std::ostream& os) {
1585 DCHECK_EQ(failures_.size(), failure_messages_.size());
Jeff Hao4137f482013-11-22 11:44:57 -08001586 for (size_t i = 0; i < failures_.size(); ++i) {
1587 os << failure_messages_[i]->str() << "\n";
Ian Rogersad0b3a32012-04-16 14:50:24 -07001588 }
1589 return os;
1590}
1591
Ian Rogers776ac1f2012-04-13 23:36:36 -07001592void MethodVerifier::Dump(std::ostream& os) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001593 VariableIndentationOutputStream vios(&os);
1594 Dump(&vios);
1595}
1596
1597void MethodVerifier::Dump(VariableIndentationOutputStream* vios) {
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001598 if (!code_item_accessor_.HasCodeItem()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001599 vios->Stream() << "Native method\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001600 return;
jeffhaobdb76512011-09-07 11:43:16 -07001601 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001602 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001603 vios->Stream() << "Register Types:\n";
1604 ScopedIndentation indent1(vios);
1605 reg_types_.Dump(vios->Stream());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001606 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001607 vios->Stream() << "Dumping instructions and register lines:\n";
1608 ScopedIndentation indent1(vios);
Mathieu Chartier1d2d4ff2017-09-23 16:11:06 -07001609
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001610 for (const DexInstructionPcPair& inst : code_item_accessor_) {
Mathieu Chartier2b2bef22017-10-26 17:10:19 -07001611 const size_t dex_pc = inst.DexPc();
Andreas Gampe077d9db2018-01-19 18:54:14 -08001612
1613 // Might be asked to dump before the table is initialized.
1614 if (reg_table_.IsInitialized()) {
1615 RegisterLine* reg_line = reg_table_.GetLine(dex_pc);
1616 if (reg_line != nullptr) {
1617 vios->Stream() << reg_line->Dump(this) << "\n";
1618 }
jeffhaobdb76512011-09-07 11:43:16 -07001619 }
Andreas Gampe077d9db2018-01-19 18:54:14 -08001620
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001621 vios->Stream()
Mathieu Chartierde40d472015-10-15 17:47:48 -07001622 << StringPrintf("0x%04zx", dex_pc) << ": " << GetInstructionFlags(dex_pc).ToString() << " ";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001623 const bool kDumpHexOfInstruction = false;
1624 if (kDumpHexOfInstruction) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001625 vios->Stream() << inst->DumpHex(5) << " ";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001626 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001627 vios->Stream() << inst->DumpString(dex_file_) << "\n";
jeffhaoba5ebb92011-08-25 17:24:37 -07001628 }
jeffhaobdb76512011-09-07 11:43:16 -07001629}
1630
Ian Rogersd81871c2011-10-03 13:57:23 -07001631static bool IsPrimitiveDescriptor(char descriptor) {
1632 switch (descriptor) {
jeffhaobdb76512011-09-07 11:43:16 -07001633 case 'I':
1634 case 'C':
1635 case 'S':
1636 case 'B':
1637 case 'Z':
jeffhaobdb76512011-09-07 11:43:16 -07001638 case 'F':
1639 case 'D':
1640 case 'J':
Ian Rogersd81871c2011-10-03 13:57:23 -07001641 return true;
jeffhaobdb76512011-09-07 11:43:16 -07001642 default:
1643 return false;
1644 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001645}
1646
Ian Rogers776ac1f2012-04-13 23:36:36 -07001647bool MethodVerifier::SetTypesFromSignature() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001648 RegisterLine* reg_line = reg_table_.GetLine(0);
Andreas Gampeef0b1a12015-06-19 20:37:46 -07001649
1650 // Should have been verified earlier.
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001651 DCHECK_GE(code_item_accessor_.RegistersSize(), code_item_accessor_.InsSize());
Andreas Gampeef0b1a12015-06-19 20:37:46 -07001652
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001653 uint32_t arg_start = code_item_accessor_.RegistersSize() - code_item_accessor_.InsSize();
1654 size_t expected_args = code_item_accessor_.InsSize(); /* long/double count as two */
jeffhaobdb76512011-09-07 11:43:16 -07001655
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001656 // Include the "this" pointer.
Ian Rogersd81871c2011-10-03 13:57:23 -07001657 size_t cur_arg = 0;
Ian Rogersad0b3a32012-04-16 14:50:24 -07001658 if (!IsStatic()) {
Andreas Gampeef0b1a12015-06-19 20:37:46 -07001659 if (expected_args == 0) {
1660 // Expect at least a receiver.
1661 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected 0 args, but method is not static";
1662 return false;
1663 }
1664
Ian Rogersd81871c2011-10-03 13:57:23 -07001665 // If this is a constructor for a class other than java.lang.Object, mark the first ("this")
1666 // argument as uninitialized. This restricts field access until the superclass constructor is
1667 // called.
Ian Rogersd8f69b02014-09-10 21:43:52 +00001668 const RegType& declaring_class = GetDeclaringClass();
Andreas Gampef10b6e12015-08-12 10:48:12 -07001669 if (IsConstructor()) {
1670 if (declaring_class.IsJavaLangObject()) {
1671 // "this" is implicitly initialized.
1672 reg_line->SetThisInitialized();
Andreas Gampead238ce2015-08-24 21:13:08 -07001673 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, declaring_class);
Andreas Gampef10b6e12015-08-12 10:48:12 -07001674 } else {
Andreas Gampead238ce2015-08-24 21:13:08 -07001675 reg_line->SetRegisterType<LockOp::kClear>(
1676 this,
1677 arg_start + cur_arg,
1678 reg_types_.UninitializedThisArgument(declaring_class));
Andreas Gampef10b6e12015-08-12 10:48:12 -07001679 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001680 } else {
Andreas Gampead238ce2015-08-24 21:13:08 -07001681 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, declaring_class);
jeffhaobdb76512011-09-07 11:43:16 -07001682 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001683 cur_arg++;
jeffhaobdb76512011-09-07 11:43:16 -07001684 }
1685
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001686 const DexFile::ProtoId& proto_id =
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001687 dex_file_->GetMethodPrototype(dex_file_->GetMethodId(dex_method_idx_));
Ian Rogers0571d352011-11-03 19:51:38 -07001688 DexFileParameterIterator iterator(*dex_file_, proto_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07001689
1690 for (; iterator.HasNext(); iterator.Next()) {
1691 const char* descriptor = iterator.GetDescriptor();
Ian Rogers7b078e82014-09-10 14:44:24 -07001692 if (descriptor == nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001693 LOG(FATAL) << "Null descriptor";
1694 }
1695 if (cur_arg >= expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07001696 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1697 << " args, found more (" << descriptor << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001698 return false;
1699 }
1700 switch (descriptor[0]) {
1701 case 'L':
1702 case '[':
1703 // We assume that reference arguments are initialized. The only way it could be otherwise
1704 // (assuming the caller was verified) is if the current method is <init>, but in that case
1705 // it's effectively considered initialized the instant we reach here (in the sense that we
1706 // can return without doing anything or call virtual methods).
1707 {
Andreas Gampe98be1a92017-08-28 08:25:45 -07001708 // Note: don't check access. No error would be thrown for declaring or passing an
1709 // inaccessible class. Only actual accesses to fields or methods will.
1710 const RegType& reg_type = ResolveClass<CheckAccess::kNo>(iterator.GetTypeIdx());
Sebastien Hertz2ed76f92014-04-22 17:11:08 +02001711 if (!reg_type.IsNonZeroReferenceTypes()) {
1712 DCHECK(HasFailures());
1713 return false;
1714 }
Andreas Gampead238ce2015-08-24 21:13:08 -07001715 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001716 }
1717 break;
1718 case 'Z':
Andreas Gampead238ce2015-08-24 21:13:08 -07001719 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Boolean());
Ian Rogersd81871c2011-10-03 13:57:23 -07001720 break;
1721 case 'C':
Andreas Gampead238ce2015-08-24 21:13:08 -07001722 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Char());
Ian Rogersd81871c2011-10-03 13:57:23 -07001723 break;
1724 case 'B':
Andreas Gampead238ce2015-08-24 21:13:08 -07001725 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Byte());
Ian Rogersd81871c2011-10-03 13:57:23 -07001726 break;
1727 case 'I':
Andreas Gampead238ce2015-08-24 21:13:08 -07001728 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Integer());
Ian Rogersd81871c2011-10-03 13:57:23 -07001729 break;
1730 case 'S':
Andreas Gampead238ce2015-08-24 21:13:08 -07001731 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Short());
Ian Rogersd81871c2011-10-03 13:57:23 -07001732 break;
1733 case 'F':
Andreas Gampead238ce2015-08-24 21:13:08 -07001734 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Float());
Ian Rogersd81871c2011-10-03 13:57:23 -07001735 break;
1736 case 'J':
1737 case 'D': {
Andreas Gampe77cd4d62014-06-19 17:29:48 -07001738 if (cur_arg + 1 >= expected_args) {
1739 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1740 << " args, found more (" << descriptor << ")";
1741 return false;
1742 }
1743
Ian Rogers7b078e82014-09-10 14:44:24 -07001744 const RegType* lo_half;
1745 const RegType* hi_half;
1746 if (descriptor[0] == 'J') {
1747 lo_half = &reg_types_.LongLo();
1748 hi_half = &reg_types_.LongHi();
1749 } else {
1750 lo_half = &reg_types_.DoubleLo();
1751 hi_half = &reg_types_.DoubleHi();
1752 }
1753 reg_line->SetRegisterTypeWide(this, arg_start + cur_arg, *lo_half, *hi_half);
Ian Rogersd81871c2011-10-03 13:57:23 -07001754 cur_arg++;
1755 break;
1756 }
1757 default:
Brian Carlstrom93c33962013-07-26 10:37:43 -07001758 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected signature type char '"
1759 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001760 return false;
1761 }
1762 cur_arg++;
1763 }
1764 if (cur_arg != expected_args) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001765 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1766 << " arguments, found " << cur_arg;
Ian Rogersd81871c2011-10-03 13:57:23 -07001767 return false;
1768 }
1769 const char* descriptor = dex_file_->GetReturnTypeDescriptor(proto_id);
1770 // Validate return type. We don't do the type lookup; just want to make sure that it has the right
1771 // format. Only major difference from the method argument format is that 'V' is supported.
1772 bool result;
1773 if (IsPrimitiveDescriptor(descriptor[0]) || descriptor[0] == 'V') {
1774 result = descriptor[1] == '\0';
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001775 } else if (descriptor[0] == '[') { // single/multi-dimensional array of object/primitive
Ian Rogersd81871c2011-10-03 13:57:23 -07001776 size_t i = 0;
1777 do {
1778 i++;
1779 } while (descriptor[i] == '['); // process leading [
1780 if (descriptor[i] == 'L') { // object array
1781 do {
1782 i++; // find closing ;
1783 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1784 result = descriptor[i] == ';';
1785 } else { // primitive array
1786 result = IsPrimitiveDescriptor(descriptor[i]) && descriptor[i + 1] == '\0';
1787 }
1788 } else if (descriptor[0] == 'L') {
1789 // could be more thorough here, but shouldn't be required
1790 size_t i = 0;
1791 do {
1792 i++;
1793 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1794 result = descriptor[i] == ';';
1795 } else {
1796 result = false;
1797 }
1798 if (!result) {
jeffhaod5347e02012-03-22 17:25:05 -07001799 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected char in return type descriptor '"
1800 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001801 }
1802 return result;
jeffhaobdb76512011-09-07 11:43:16 -07001803}
1804
Ian Rogers776ac1f2012-04-13 23:36:36 -07001805bool MethodVerifier::CodeFlowVerifyMethod() {
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001806 const uint16_t* insns = code_item_accessor_.Insns();
1807 const uint32_t insns_size = code_item_accessor_.InsnsSizeInCodeUnits();
jeffhaoba5ebb92011-08-25 17:24:37 -07001808
jeffhaobdb76512011-09-07 11:43:16 -07001809 /* Begin by marking the first instruction as "changed". */
Mathieu Chartierde40d472015-10-15 17:47:48 -07001810 GetInstructionFlags(0).SetChanged();
Ian Rogersd81871c2011-10-03 13:57:23 -07001811 uint32_t start_guess = 0;
jeffhaoba5ebb92011-08-25 17:24:37 -07001812
jeffhaobdb76512011-09-07 11:43:16 -07001813 /* Continue until no instructions are marked "changed". */
1814 while (true) {
Mathieu Chartier4306ef82014-12-19 18:41:47 -08001815 if (allow_thread_suspension_) {
1816 self_->AllowThreadSuspension();
1817 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001818 // Find the first marked one. Use "start_guess" as a way to find one quickly.
1819 uint32_t insn_idx = start_guess;
1820 for (; insn_idx < insns_size; insn_idx++) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07001821 if (GetInstructionFlags(insn_idx).IsChanged())
jeffhaobdb76512011-09-07 11:43:16 -07001822 break;
1823 }
jeffhaobdb76512011-09-07 11:43:16 -07001824 if (insn_idx == insns_size) {
1825 if (start_guess != 0) {
1826 /* try again, starting from the top */
1827 start_guess = 0;
1828 continue;
1829 } else {
1830 /* all flags are clear */
1831 break;
1832 }
1833 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001834 // We carry the working set of registers from instruction to instruction. If this address can
1835 // be the target of a branch (or throw) instruction, or if we're skipping around chasing
1836 // "changed" flags, we need to load the set of registers from the table.
1837 // Because we always prefer to continue on to the next instruction, we should never have a
1838 // situation where we have a stray "changed" flag set on an instruction that isn't a branch
1839 // target.
1840 work_insn_idx_ = insn_idx;
Mathieu Chartierde40d472015-10-15 17:47:48 -07001841 if (GetInstructionFlags(insn_idx).IsBranchTarget()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001842 work_line_->CopyFromLine(reg_table_.GetLine(insn_idx));
Ian Rogersebbdd872014-07-07 23:53:08 -07001843 } else if (kIsDebugBuild) {
jeffhaobdb76512011-09-07 11:43:16 -07001844 /*
1845 * Sanity check: retrieve the stored register line (assuming
1846 * a full table) and make sure it actually matches.
1847 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001848 RegisterLine* register_line = reg_table_.GetLine(insn_idx);
Ian Rogers7b078e82014-09-10 14:44:24 -07001849 if (register_line != nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001850 if (work_line_->CompareLine(register_line) != 0) {
1851 Dump(std::cout);
1852 std::cout << info_messages_.str();
David Sehr709b0702016-10-13 09:12:37 -07001853 LOG(FATAL) << "work_line diverged in " << dex_file_->PrettyMethod(dex_method_idx_)
Elliott Hughesc073b072012-05-24 19:29:17 -07001854 << "@" << reinterpret_cast<void*>(work_insn_idx_) << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07001855 << " work_line=" << work_line_->Dump(this) << "\n"
1856 << " expected=" << register_line->Dump(this);
Ian Rogersd81871c2011-10-03 13:57:23 -07001857 }
jeffhaobdb76512011-09-07 11:43:16 -07001858 }
jeffhaobdb76512011-09-07 11:43:16 -07001859 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001860 if (!CodeFlowVerifyInstruction(&start_guess)) {
David Sehr709b0702016-10-13 09:12:37 -07001861 std::string prepend(dex_file_->PrettyMethod(dex_method_idx_));
Ian Rogersad0b3a32012-04-16 14:50:24 -07001862 prepend += " failed to verify: ";
1863 PrependToLastFailMessage(prepend);
jeffhaoba5ebb92011-08-25 17:24:37 -07001864 return false;
1865 }
jeffhaobdb76512011-09-07 11:43:16 -07001866 /* Clear "changed" and mark as visited. */
Mathieu Chartierde40d472015-10-15 17:47:48 -07001867 GetInstructionFlags(insn_idx).SetVisited();
1868 GetInstructionFlags(insn_idx).ClearChanged();
jeffhaobdb76512011-09-07 11:43:16 -07001869 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001870
Andreas Gampe92d77202017-12-06 20:49:00 -08001871 if (UNLIKELY(VLOG_IS_ON(verifier_debug))) {
jeffhaobdb76512011-09-07 11:43:16 -07001872 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001873 * Scan for dead code. There's nothing "evil" about dead code
jeffhaobdb76512011-09-07 11:43:16 -07001874 * (besides the wasted space), but it indicates a flaw somewhere
1875 * down the line, possibly in the verifier.
1876 *
1877 * If we've substituted "always throw" instructions into the stream,
1878 * we are almost certainly going to have some dead code.
1879 */
1880 int dead_start = -1;
Mathieu Chartier1d2d4ff2017-09-23 16:11:06 -07001881
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001882 for (const DexInstructionPcPair& inst : code_item_accessor_) {
Mathieu Chartier2b2bef22017-10-26 17:10:19 -07001883 const uint32_t insn_idx = inst.DexPc();
jeffhaobdb76512011-09-07 11:43:16 -07001884 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001885 * Switch-statement data doesn't get "visited" by scanner. It
jeffhaobdb76512011-09-07 11:43:16 -07001886 * may or may not be preceded by a padding NOP (for alignment).
1887 */
1888 if (insns[insn_idx] == Instruction::kPackedSwitchSignature ||
1889 insns[insn_idx] == Instruction::kSparseSwitchSignature ||
1890 insns[insn_idx] == Instruction::kArrayDataSignature ||
Elliott Hughes380aaa72012-07-09 14:33:15 -07001891 (insns[insn_idx] == Instruction::NOP && (insn_idx + 1 < insns_size) &&
jeffhaobdb76512011-09-07 11:43:16 -07001892 (insns[insn_idx + 1] == Instruction::kPackedSwitchSignature ||
1893 insns[insn_idx + 1] == Instruction::kSparseSwitchSignature ||
1894 insns[insn_idx + 1] == Instruction::kArrayDataSignature))) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07001895 GetInstructionFlags(insn_idx).SetVisited();
jeffhaobdb76512011-09-07 11:43:16 -07001896 }
1897
Mathieu Chartierde40d472015-10-15 17:47:48 -07001898 if (!GetInstructionFlags(insn_idx).IsVisited()) {
Mathieu Chartier1d2d4ff2017-09-23 16:11:06 -07001899 if (dead_start < 0) {
jeffhaobdb76512011-09-07 11:43:16 -07001900 dead_start = insn_idx;
Mathieu Chartier1d2d4ff2017-09-23 16:11:06 -07001901 }
jeffhaobdb76512011-09-07 11:43:16 -07001902 } else if (dead_start >= 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001903 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start)
1904 << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaobdb76512011-09-07 11:43:16 -07001905 dead_start = -1;
1906 }
1907 }
1908 if (dead_start >= 0) {
Mathieu Chartier1d2d4ff2017-09-23 16:11:06 -07001909 LogVerifyInfo()
1910 << "dead code " << reinterpret_cast<void*>(dead_start)
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001911 << "-" << reinterpret_cast<void*>(code_item_accessor_.InsnsSizeInCodeUnits() - 1);
jeffhaoba5ebb92011-08-25 17:24:37 -07001912 }
Ian Rogersc9e463c2013-06-05 16:52:26 -07001913 // To dump the state of the verify after a method, do something like:
David Sehr709b0702016-10-13 09:12:37 -07001914 // if (dex_file_->PrettyMethod(dex_method_idx_) ==
Ian Rogersc9e463c2013-06-05 16:52:26 -07001915 // "boolean java.lang.String.equals(java.lang.Object)") {
1916 // LOG(INFO) << info_messages_.str();
1917 // }
jeffhaoba5ebb92011-08-25 17:24:37 -07001918 }
jeffhaobdb76512011-09-07 11:43:16 -07001919 return true;
1920}
1921
Andreas Gampe68df3202015-06-22 11:35:46 -07001922// Returns the index of the first final instance field of the given class, or kDexNoIndex if there
1923// is no such field.
Andreas Gampea5b09a62016-11-17 15:21:22 -08001924static uint32_t GetFirstFinalInstanceFieldIndex(const DexFile& dex_file, dex::TypeIndex type_idx) {
Andreas Gampe68df3202015-06-22 11:35:46 -07001925 const DexFile::ClassDef* class_def = dex_file.FindClassDef(type_idx);
1926 DCHECK(class_def != nullptr);
1927 const uint8_t* class_data = dex_file.GetClassData(*class_def);
1928 DCHECK(class_data != nullptr);
1929 ClassDataItemIterator it(dex_file, class_data);
Mathieu Chartiere17cf242017-06-19 11:05:51 -07001930 it.SkipStaticFields();
Andreas Gampe68df3202015-06-22 11:35:46 -07001931 while (it.HasNextInstanceField()) {
1932 if ((it.GetFieldAccessFlags() & kAccFinal) != 0) {
1933 return it.GetMemberIndex();
1934 }
1935 it.Next();
1936 }
Andreas Gampee2abbc62017-09-15 11:59:26 -07001937 return dex::kDexNoIndex;
Andreas Gampe68df3202015-06-22 11:35:46 -07001938}
1939
Andreas Gampea727e372015-08-25 09:22:37 -07001940// Setup a register line for the given return instruction.
1941static void AdjustReturnLine(MethodVerifier* verifier,
1942 const Instruction* ret_inst,
1943 RegisterLine* line) {
1944 Instruction::Code opcode = ret_inst->Opcode();
1945
1946 switch (opcode) {
1947 case Instruction::RETURN_VOID:
1948 case Instruction::RETURN_VOID_NO_BARRIER:
1949 SafelyMarkAllRegistersAsConflicts(verifier, line);
1950 break;
1951
1952 case Instruction::RETURN:
1953 case Instruction::RETURN_OBJECT:
1954 line->MarkAllRegistersAsConflictsExcept(verifier, ret_inst->VRegA_11x());
1955 break;
1956
1957 case Instruction::RETURN_WIDE:
1958 line->MarkAllRegistersAsConflictsExceptWide(verifier, ret_inst->VRegA_11x());
1959 break;
1960
1961 default:
1962 LOG(FATAL) << "Unknown return opcode " << opcode;
1963 UNREACHABLE();
1964 }
1965}
1966
Ian Rogers776ac1f2012-04-13 23:36:36 -07001967bool MethodVerifier::CodeFlowVerifyInstruction(uint32_t* start_guess) {
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001968 // If we're doing FindLocksAtDexPc, check whether we're at the dex pc we care about.
1969 // We want the state _before_ the instruction, for the case where the dex pc we're
1970 // interested in is itself a monitor-enter instruction (which is a likely place
1971 // for a thread to be suspended).
Ian Rogers7b078e82014-09-10 14:44:24 -07001972 if (monitor_enter_dex_pcs_ != nullptr && work_insn_idx_ == interesting_dex_pc_) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001973 monitor_enter_dex_pcs_->clear(); // The new work line is more accurate than the previous one.
Andreas Gampeaaf0d382017-11-27 14:10:21 -08001974
1975 std::map<uint32_t, DexLockInfo> depth_to_lock_info;
1976 auto collector = [&](uint32_t dex_reg, uint32_t depth) {
1977 auto insert_pair = depth_to_lock_info.emplace(depth, DexLockInfo(depth));
1978 auto it = insert_pair.first;
1979 auto set_insert_pair = it->second.dex_registers.insert(dex_reg);
1980 DCHECK(set_insert_pair.second);
1981 };
1982 work_line_->IterateRegToLockDepths(collector);
1983 for (auto& pair : depth_to_lock_info) {
1984 monitor_enter_dex_pcs_->push_back(pair.second);
1985 // Map depth to dex PC.
1986 (*monitor_enter_dex_pcs_)[monitor_enter_dex_pcs_->size() - 1].dex_pc =
1987 work_line_->GetMonitorEnterDexPc(pair.second.dex_pc);
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001988 }
1989 }
1990
jeffhaobdb76512011-09-07 11:43:16 -07001991 /*
1992 * Once we finish decoding the instruction, we need to figure out where
jeffhaod1f0fde2011-09-08 17:25:33 -07001993 * we can go from here. There are three possible ways to transfer
jeffhaobdb76512011-09-07 11:43:16 -07001994 * control to another statement:
1995 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001996 * (1) Continue to the next instruction. Applies to all but
jeffhaobdb76512011-09-07 11:43:16 -07001997 * unconditional branches, method returns, and exception throws.
jeffhaod1f0fde2011-09-08 17:25:33 -07001998 * (2) Branch to one or more possible locations. Applies to branches
jeffhaobdb76512011-09-07 11:43:16 -07001999 * and switch statements.
jeffhaod1f0fde2011-09-08 17:25:33 -07002000 * (3) Exception handlers. Applies to any instruction that can
jeffhaobdb76512011-09-07 11:43:16 -07002001 * throw an exception that is handled by an encompassing "try"
2002 * block.
2003 *
2004 * We can also return, in which case there is no successor instruction
2005 * from this point.
2006 *
Elliott Hughesadb8c672012-03-06 16:49:32 -08002007 * The behavior can be determined from the opcode flags.
jeffhaobdb76512011-09-07 11:43:16 -07002008 */
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08002009 const uint16_t* insns = code_item_accessor_.Insns() + work_insn_idx_;
Ian Rogersd81871c2011-10-03 13:57:23 -07002010 const Instruction* inst = Instruction::At(insns);
Ian Rogersa75a0132012-09-28 11:41:42 -07002011 int opcode_flags = Instruction::FlagsOf(inst->Opcode());
jeffhaobdb76512011-09-07 11:43:16 -07002012
jeffhaobdb76512011-09-07 11:43:16 -07002013 int32_t branch_target = 0;
jeffhaobdb76512011-09-07 11:43:16 -07002014 bool just_set_result = false;
Andreas Gampe92d77202017-12-06 20:49:00 -08002015 if (UNLIKELY(VLOG_IS_ON(verifier_debug))) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002016 // Generate processing back trace to debug verifier
Elliott Hughesc073b072012-05-24 19:29:17 -07002017 LogVerifyInfo() << "Processing " << inst->DumpString(dex_file_) << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07002018 << work_line_->Dump(this) << "\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07002019 }
jeffhaobdb76512011-09-07 11:43:16 -07002020
2021 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002022 * Make a copy of the previous register state. If the instruction
jeffhaobdb76512011-09-07 11:43:16 -07002023 * can throw an exception, we will copy/merge this into the "catch"
2024 * address rather than work_line, because we don't want the result
2025 * from the "successful" code path (e.g. a check-cast that "improves"
2026 * a type) to be visible to the exception handler.
2027 */
Ian Rogers776ac1f2012-04-13 23:36:36 -07002028 if ((opcode_flags & Instruction::kThrow) != 0 && CurrentInsnFlags()->IsInTry()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002029 saved_line_->CopyFromLine(work_line_.get());
Ian Rogers1ff3c982014-08-12 02:30:58 -07002030 } else if (kIsDebugBuild) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002031 saved_line_->FillWithGarbage();
jeffhaobdb76512011-09-07 11:43:16 -07002032 }
Andreas Gamped12e7822015-06-25 10:26:40 -07002033 DCHECK(!have_pending_runtime_throw_failure_); // Per-instruction flag, should not be set here.
jeffhaobdb76512011-09-07 11:43:16 -07002034
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07002035
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002036 // We need to ensure the work line is consistent while performing validation. When we spot a
2037 // peephole pattern we compute a new line for either the fallthrough instruction or the
2038 // branch target.
Mathieu Chartier361e04a2016-02-16 14:06:35 -08002039 RegisterLineArenaUniquePtr branch_line;
2040 RegisterLineArenaUniquePtr fallthrough_line;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002041
Sebastien Hertz5243e912013-05-21 10:55:07 +02002042 switch (inst->Opcode()) {
jeffhaobdb76512011-09-07 11:43:16 -07002043 case Instruction::NOP:
2044 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002045 * A "pure" NOP has no effect on anything. Data tables start with
jeffhaobdb76512011-09-07 11:43:16 -07002046 * a signature that looks like a NOP; if we see one of these in
2047 * the course of executing code then we have a problem.
2048 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002049 if (inst->VRegA_10x() != 0) {
jeffhaod5347e02012-03-22 17:25:05 -07002050 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "encountered data table in instruction stream";
jeffhaobdb76512011-09-07 11:43:16 -07002051 }
2052 break;
2053
2054 case Instruction::MOVE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002055 work_line_->CopyRegister1(this, inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategory1nr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02002056 break;
jeffhaobdb76512011-09-07 11:43:16 -07002057 case Instruction::MOVE_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07002058 work_line_->CopyRegister1(this, inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategory1nr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02002059 break;
jeffhaobdb76512011-09-07 11:43:16 -07002060 case Instruction::MOVE_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07002061 work_line_->CopyRegister1(this, inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategory1nr);
jeffhaobdb76512011-09-07 11:43:16 -07002062 break;
2063 case Instruction::MOVE_WIDE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002064 work_line_->CopyRegister2(this, inst->VRegA_12x(), inst->VRegB_12x());
Sebastien Hertz5243e912013-05-21 10:55:07 +02002065 break;
jeffhaobdb76512011-09-07 11:43:16 -07002066 case Instruction::MOVE_WIDE_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07002067 work_line_->CopyRegister2(this, inst->VRegA_22x(), inst->VRegB_22x());
Sebastien Hertz5243e912013-05-21 10:55:07 +02002068 break;
jeffhaobdb76512011-09-07 11:43:16 -07002069 case Instruction::MOVE_WIDE_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07002070 work_line_->CopyRegister2(this, inst->VRegA_32x(), inst->VRegB_32x());
jeffhaobdb76512011-09-07 11:43:16 -07002071 break;
2072 case Instruction::MOVE_OBJECT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002073 work_line_->CopyRegister1(this, inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategoryRef);
Sebastien Hertz5243e912013-05-21 10:55:07 +02002074 break;
jeffhaobdb76512011-09-07 11:43:16 -07002075 case Instruction::MOVE_OBJECT_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07002076 work_line_->CopyRegister1(this, inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategoryRef);
Sebastien Hertz5243e912013-05-21 10:55:07 +02002077 break;
jeffhaobdb76512011-09-07 11:43:16 -07002078 case Instruction::MOVE_OBJECT_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07002079 work_line_->CopyRegister1(this, inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategoryRef);
jeffhaobdb76512011-09-07 11:43:16 -07002080 break;
2081
2082 /*
2083 * The move-result instructions copy data out of a "pseudo-register"
jeffhaod1f0fde2011-09-08 17:25:33 -07002084 * with the results from the last method invocation. In practice we
jeffhaobdb76512011-09-07 11:43:16 -07002085 * might want to hold the result in an actual CPU register, so the
2086 * Dalvik spec requires that these only appear immediately after an
2087 * invoke or filled-new-array.
2088 *
jeffhaod1f0fde2011-09-08 17:25:33 -07002089 * These calls invalidate the "result" register. (This is now
jeffhaobdb76512011-09-07 11:43:16 -07002090 * redundant with the reset done below, but it can make the debug info
2091 * easier to read in some cases.)
2092 */
2093 case Instruction::MOVE_RESULT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002094 work_line_->CopyResultRegister1(this, inst->VRegA_11x(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002095 break;
2096 case Instruction::MOVE_RESULT_WIDE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002097 work_line_->CopyResultRegister2(this, inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07002098 break;
2099 case Instruction::MOVE_RESULT_OBJECT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002100 work_line_->CopyResultRegister1(this, inst->VRegA_11x(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002101 break;
2102
Ian Rogersd81871c2011-10-03 13:57:23 -07002103 case Instruction::MOVE_EXCEPTION: {
Sebastien Hertz270a0e12015-01-16 19:49:09 +01002104 // We do not allow MOVE_EXCEPTION as the first instruction in a method. This is a simple case
2105 // where one entrypoint to the catch block is not actually an exception path.
2106 if (work_insn_idx_ == 0) {
2107 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "move-exception at pc 0x0";
2108 break;
2109 }
jeffhaobdb76512011-09-07 11:43:16 -07002110 /*
jeffhao60f83e32012-02-13 17:16:30 -08002111 * This statement can only appear as the first instruction in an exception handler. We verify
2112 * that as part of extracting the exception type from the catch block list.
jeffhaobdb76512011-09-07 11:43:16 -07002113 */
Ian Rogersd8f69b02014-09-10 21:43:52 +00002114 const RegType& res_type = GetCaughtExceptionType();
Andreas Gampead238ce2015-08-24 21:13:08 -07002115 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_11x(), res_type);
jeffhaobdb76512011-09-07 11:43:16 -07002116 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002117 }
jeffhaobdb76512011-09-07 11:43:16 -07002118 case Instruction::RETURN_VOID:
Andreas Gampef10b6e12015-08-12 10:48:12 -07002119 if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002120 if (!GetMethodReturnType().IsConflict()) {
jeffhaod5347e02012-03-22 17:25:05 -07002121 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07002122 }
jeffhaobdb76512011-09-07 11:43:16 -07002123 }
2124 break;
2125 case Instruction::RETURN:
Andreas Gampef10b6e12015-08-12 10:48:12 -07002126 if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) {
jeffhaobdb76512011-09-07 11:43:16 -07002127 /* check the method signature */
Ian Rogersd8f69b02014-09-10 21:43:52 +00002128 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07002129 if (!return_type.IsCategory1Types()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002130 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected non-category 1 return type "
2131 << return_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002132 } else {
2133 // Compilers may generate synthetic functions that write byte values into boolean fields.
2134 // Also, it may use integer values for boolean, byte, short, and character return types.
Sebastien Hertz5243e912013-05-21 10:55:07 +02002135 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07002136 const RegType& src_type = work_line_->GetRegisterType(this, vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07002137 bool use_src = ((return_type.IsBoolean() && src_type.IsByte()) ||
2138 ((return_type.IsBoolean() || return_type.IsByte() ||
2139 return_type.IsShort() || return_type.IsChar()) &&
2140 src_type.IsInteger()));
2141 /* check the register contents */
Ian Rogersad0b3a32012-04-16 14:50:24 -07002142 bool success =
Ian Rogers7b078e82014-09-10 14:44:24 -07002143 work_line_->VerifyRegisterType(this, vregA, use_src ? src_type : return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002144 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002145 AppendToLastFailMessage(StringPrintf(" return-1nr on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07002146 }
jeffhaobdb76512011-09-07 11:43:16 -07002147 }
2148 }
2149 break;
2150 case Instruction::RETURN_WIDE:
Andreas Gampef10b6e12015-08-12 10:48:12 -07002151 if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) {
jeffhaobdb76512011-09-07 11:43:16 -07002152 /* check the method signature */
Ian Rogersd8f69b02014-09-10 21:43:52 +00002153 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07002154 if (!return_type.IsCategory2Types()) {
jeffhaod5347e02012-03-22 17:25:05 -07002155 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-wide not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07002156 } else {
2157 /* check the register contents */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002158 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07002159 bool success = work_line_->VerifyRegisterType(this, vregA, return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002160 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002161 AppendToLastFailMessage(StringPrintf(" return-wide on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07002162 }
jeffhaobdb76512011-09-07 11:43:16 -07002163 }
2164 }
2165 break;
2166 case Instruction::RETURN_OBJECT:
Andreas Gampef10b6e12015-08-12 10:48:12 -07002167 if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) {
Ian Rogersd8f69b02014-09-10 21:43:52 +00002168 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07002169 if (!return_type.IsReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002170 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-object not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07002171 } else {
2172 /* return_type is the *expected* return type, not register value */
Andreas Gampeeed3a5d2017-11-29 14:58:34 -08002173 DCHECK(!return_type.IsZeroOrNull());
Ian Rogersd81871c2011-10-03 13:57:23 -07002174 DCHECK(!return_type.IsUninitializedReference());
Sebastien Hertz5243e912013-05-21 10:55:07 +02002175 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07002176 const RegType& reg_type = work_line_->GetRegisterType(this, vregA);
Andreas Gampea32210c2015-06-24 10:26:13 -07002177 // Disallow returning undefined, conflict & uninitialized values and verify that the
2178 // reference in vAA is an instance of the "return_type."
2179 if (reg_type.IsUndefined()) {
2180 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning undefined register";
2181 } else if (reg_type.IsConflict()) {
2182 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning register with conflict";
2183 } else if (reg_type.IsUninitializedTypes()) {
David Brazdil68b5c0b2016-01-19 14:25:29 +00002184 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning uninitialized object '"
Brian Carlstrom93c33962013-07-26 10:37:43 -07002185 << reg_type << "'";
Andreas Gampea4c98f22015-11-06 16:24:49 -08002186 } else if (!reg_type.IsReferenceTypes()) {
2187 // We really do expect a reference here.
2188 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-object returns a non-reference type "
2189 << reg_type;
David Brazdilca3c8c32016-09-06 14:04:48 +01002190 } else if (!return_type.IsAssignableFrom(reg_type, this)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07002191 if (reg_type.IsUnresolvedTypes() || return_type.IsUnresolvedTypes()) {
2192 Fail(VERIFY_ERROR_NO_CLASS) << " can't resolve returned type '" << return_type
2193 << "' or '" << reg_type << "'";
2194 } else {
Andreas Gampe16f149c2015-03-23 10:10:20 -07002195 bool soft_error = false;
2196 // Check whether arrays are involved. They will show a valid class status, even
2197 // if their components are erroneous.
2198 if (reg_type.IsArrayTypes() && return_type.IsArrayTypes()) {
David Brazdilca3c8c32016-09-06 14:04:48 +01002199 return_type.CanAssignArray(reg_type, reg_types_, class_loader_, this, &soft_error);
Andreas Gampe16f149c2015-03-23 10:10:20 -07002200 if (soft_error) {
2201 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "array with erroneous component type: "
2202 << reg_type << " vs " << return_type;
2203 }
2204 }
2205
2206 if (!soft_error) {
2207 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning '" << reg_type
2208 << "', but expected from declaration '" << return_type << "'";
2209 }
Jeff Haoa3faaf42013-09-03 19:07:00 -07002210 }
jeffhaobdb76512011-09-07 11:43:16 -07002211 }
2212 }
2213 }
2214 break;
2215
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07002216 /* could be boolean, int, float, or a null reference */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002217 case Instruction::CONST_4: {
2218 int32_t val = static_cast<int32_t>(inst->VRegB_11n() << 28) >> 28;
Andreas Gampead238ce2015-08-24 21:13:08 -07002219 work_line_->SetRegisterType<LockOp::kClear>(
2220 this, inst->VRegA_11n(), DetermineCat1Constant(val, need_precise_constants_));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07002221 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02002222 }
2223 case Instruction::CONST_16: {
2224 int16_t val = static_cast<int16_t>(inst->VRegB_21s());
Andreas Gampead238ce2015-08-24 21:13:08 -07002225 work_line_->SetRegisterType<LockOp::kClear>(
2226 this, inst->VRegA_21s(), DetermineCat1Constant(val, need_precise_constants_));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07002227 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02002228 }
Sebastien Hertz849600b2013-12-20 10:28:08 +01002229 case Instruction::CONST: {
2230 int32_t val = inst->VRegB_31i();
Andreas Gampead238ce2015-08-24 21:13:08 -07002231 work_line_->SetRegisterType<LockOp::kClear>(
2232 this, inst->VRegA_31i(), DetermineCat1Constant(val, need_precise_constants_));
jeffhaobdb76512011-09-07 11:43:16 -07002233 break;
Sebastien Hertz849600b2013-12-20 10:28:08 +01002234 }
2235 case Instruction::CONST_HIGH16: {
2236 int32_t val = static_cast<int32_t>(inst->VRegB_21h() << 16);
Andreas Gampead238ce2015-08-24 21:13:08 -07002237 work_line_->SetRegisterType<LockOp::kClear>(
2238 this, inst->VRegA_21h(), DetermineCat1Constant(val, need_precise_constants_));
jeffhaobdb76512011-09-07 11:43:16 -07002239 break;
Sebastien Hertz849600b2013-12-20 10:28:08 +01002240 }
jeffhaobdb76512011-09-07 11:43:16 -07002241 /* could be long or double; resolved upon use */
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002242 case Instruction::CONST_WIDE_16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002243 int64_t val = static_cast<int16_t>(inst->VRegB_21s());
Ian Rogersd8f69b02014-09-10 21:43:52 +00002244 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
2245 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07002246 work_line_->SetRegisterTypeWide(this, inst->VRegA_21s(), lo, hi);
jeffhaobdb76512011-09-07 11:43:16 -07002247 break;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002248 }
2249 case Instruction::CONST_WIDE_32: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002250 int64_t val = static_cast<int32_t>(inst->VRegB_31i());
Ian Rogersd8f69b02014-09-10 21:43:52 +00002251 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
2252 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07002253 work_line_->SetRegisterTypeWide(this, inst->VRegA_31i(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002254 break;
2255 }
2256 case Instruction::CONST_WIDE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002257 int64_t val = inst->VRegB_51l();
Ian Rogersd8f69b02014-09-10 21:43:52 +00002258 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
2259 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07002260 work_line_->SetRegisterTypeWide(this, inst->VRegA_51l(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002261 break;
2262 }
2263 case Instruction::CONST_WIDE_HIGH16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002264 int64_t val = static_cast<uint64_t>(inst->VRegB_21h()) << 48;
Ian Rogersd8f69b02014-09-10 21:43:52 +00002265 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
2266 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07002267 work_line_->SetRegisterTypeWide(this, inst->VRegA_21h(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002268 break;
2269 }
jeffhaobdb76512011-09-07 11:43:16 -07002270 case Instruction::CONST_STRING:
Andreas Gampead238ce2015-08-24 21:13:08 -07002271 work_line_->SetRegisterType<LockOp::kClear>(
2272 this, inst->VRegA_21c(), reg_types_.JavaLangString());
Sebastien Hertz5243e912013-05-21 10:55:07 +02002273 break;
jeffhaobdb76512011-09-07 11:43:16 -07002274 case Instruction::CONST_STRING_JUMBO:
Andreas Gampead238ce2015-08-24 21:13:08 -07002275 work_line_->SetRegisterType<LockOp::kClear>(
2276 this, inst->VRegA_31c(), reg_types_.JavaLangString());
jeffhaobdb76512011-09-07 11:43:16 -07002277 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002278 case Instruction::CONST_CLASS: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002279 // Get type from instruction if unresolved then we need an access check
2280 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Andreas Gampe98be1a92017-08-28 08:25:45 -07002281 const RegType& res_type = ResolveClass<CheckAccess::kYes>(dex::TypeIndex(inst->VRegB_21c()));
Ian Rogersad0b3a32012-04-16 14:50:24 -07002282 // Register holds class, ie its type is class, on error it will hold Conflict.
Andreas Gampead238ce2015-08-24 21:13:08 -07002283 work_line_->SetRegisterType<LockOp::kClear>(
2284 this, inst->VRegA_21c(), res_type.IsConflict() ? res_type
2285 : reg_types_.JavaLangClass());
jeffhaobdb76512011-09-07 11:43:16 -07002286 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002287 }
Orion Hodson2e599942017-09-22 16:17:41 +01002288 case Instruction::CONST_METHOD_HANDLE:
2289 work_line_->SetRegisterType<LockOp::kClear>(
2290 this, inst->VRegA_21c(), reg_types_.JavaLangInvokeMethodHandle());
Orion Hodson2e599942017-09-22 16:17:41 +01002291 break;
2292 case Instruction::CONST_METHOD_TYPE:
2293 work_line_->SetRegisterType<LockOp::kClear>(
2294 this, inst->VRegA_21c(), reg_types_.JavaLangInvokeMethodType());
Orion Hodson2e599942017-09-22 16:17:41 +01002295 break;
jeffhaobdb76512011-09-07 11:43:16 -07002296 case Instruction::MONITOR_ENTER:
Ian Rogers7b078e82014-09-10 14:44:24 -07002297 work_line_->PushMonitor(this, inst->VRegA_11x(), work_insn_idx_);
Andreas Gampec1474102015-08-18 08:57:44 -07002298 // Check whether the previous instruction is a move-object with vAA as a source, creating
2299 // untracked lock aliasing.
Mathieu Chartierde40d472015-10-15 17:47:48 -07002300 if (0 != work_insn_idx_ && !GetInstructionFlags(work_insn_idx_).IsBranchTarget()) {
Andreas Gampec1474102015-08-18 08:57:44 -07002301 uint32_t prev_idx = work_insn_idx_ - 1;
Mathieu Chartierde40d472015-10-15 17:47:48 -07002302 while (0 != prev_idx && !GetInstructionFlags(prev_idx).IsOpcode()) {
Andreas Gampec1474102015-08-18 08:57:44 -07002303 prev_idx--;
2304 }
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08002305 const Instruction& prev_inst = code_item_accessor_.InstructionAt(prev_idx);
Mathieu Chartier1d2d4ff2017-09-23 16:11:06 -07002306 switch (prev_inst.Opcode()) {
Andreas Gampec1474102015-08-18 08:57:44 -07002307 case Instruction::MOVE_OBJECT:
2308 case Instruction::MOVE_OBJECT_16:
2309 case Instruction::MOVE_OBJECT_FROM16:
Mathieu Chartier1d2d4ff2017-09-23 16:11:06 -07002310 if (prev_inst.VRegB() == inst->VRegA_11x()) {
Andreas Gampec1474102015-08-18 08:57:44 -07002311 // Redo the copy. This won't change the register types, but update the lock status
2312 // for the aliased register.
2313 work_line_->CopyRegister1(this,
Mathieu Chartier1d2d4ff2017-09-23 16:11:06 -07002314 prev_inst.VRegA(),
2315 prev_inst.VRegB(),
Andreas Gampec1474102015-08-18 08:57:44 -07002316 kTypeCategoryRef);
2317 }
2318 break;
2319
2320 default: // Other instruction types ignored.
2321 break;
2322 }
2323 }
jeffhaobdb76512011-09-07 11:43:16 -07002324 break;
2325 case Instruction::MONITOR_EXIT:
2326 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002327 * monitor-exit instructions are odd. They can throw exceptions,
jeffhaobdb76512011-09-07 11:43:16 -07002328 * but when they do they act as if they succeeded and the PC is
jeffhaod1f0fde2011-09-08 17:25:33 -07002329 * pointing to the following instruction. (This behavior goes back
jeffhaobdb76512011-09-07 11:43:16 -07002330 * to the need to handle asynchronous exceptions, a now-deprecated
2331 * feature that Dalvik doesn't support.)
2332 *
jeffhaod1f0fde2011-09-08 17:25:33 -07002333 * In practice we don't need to worry about this. The only
jeffhaobdb76512011-09-07 11:43:16 -07002334 * exceptions that can be thrown from monitor-exit are for a
jeffhaod1f0fde2011-09-08 17:25:33 -07002335 * null reference and -exit without a matching -enter. If the
jeffhaobdb76512011-09-07 11:43:16 -07002336 * structured locking checks are working, the former would have
2337 * failed on the -enter instruction, and the latter is impossible.
2338 *
2339 * This is fortunate, because issue 3221411 prevents us from
2340 * chasing the "can throw" path when monitor verification is
jeffhaod1f0fde2011-09-08 17:25:33 -07002341 * enabled. If we can fully verify the locking we can ignore
jeffhaobdb76512011-09-07 11:43:16 -07002342 * some catch blocks (which will show up as "dead" code when
2343 * we skip them here); if we can't, then the code path could be
2344 * "live" so we still need to check it.
2345 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002346 opcode_flags &= ~Instruction::kThrow;
Ian Rogers7b078e82014-09-10 14:44:24 -07002347 work_line_->PopMonitor(this, inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07002348 break;
Ian Rogers28ad40d2011-10-27 15:19:26 -07002349 case Instruction::CHECK_CAST:
Ian Rogersd81871c2011-10-03 13:57:23 -07002350 case Instruction::INSTANCE_OF: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002351 /*
2352 * If this instruction succeeds, we will "downcast" register vA to the type in vB. (This
2353 * could be a "upcast" -- not expected, so we don't try to address it.)
2354 *
2355 * If it fails, an exception is thrown, which we deal with later by ignoring the update to
Elliott Hughesadb8c672012-03-06 16:49:32 -08002356 * dec_insn.vA when branching to a handler.
Ian Rogers28ad40d2011-10-27 15:19:26 -07002357 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002358 const bool is_checkcast = (inst->Opcode() == Instruction::CHECK_CAST);
Andreas Gampea5b09a62016-11-17 15:21:22 -08002359 const dex::TypeIndex type_idx((is_checkcast) ? inst->VRegB_21c() : inst->VRegC_22c());
Andreas Gampe98be1a92017-08-28 08:25:45 -07002360 const RegType& res_type = ResolveClass<CheckAccess::kYes>(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002361 if (res_type.IsConflict()) {
Andreas Gampe00633eb2014-07-17 16:13:35 -07002362 // If this is a primitive type, fail HARD.
Vladimir Marko666ee3d2017-12-11 18:37:36 +00002363 ObjPtr<mirror::Class> klass = Runtime::Current()->GetClassLinker()->LookupResolvedType(
2364 type_idx, dex_cache_.Get(), class_loader_.Get());
Andreas Gampe00633eb2014-07-17 16:13:35 -07002365 if (klass != nullptr && klass->IsPrimitive()) {
2366 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "using primitive type "
2367 << dex_file_->StringByTypeIdx(type_idx) << " in instanceof in "
2368 << GetDeclaringClass();
2369 break;
2370 }
2371
Ian Rogersad0b3a32012-04-16 14:50:24 -07002372 DCHECK_NE(failures_.size(), 0U);
2373 if (!is_checkcast) {
Andreas Gampead238ce2015-08-24 21:13:08 -07002374 work_line_->SetRegisterType<LockOp::kClear>(this,
2375 inst->VRegA_22c(),
2376 reg_types_.Boolean());
Ian Rogersad0b3a32012-04-16 14:50:24 -07002377 }
2378 break; // bad class
Ian Rogers9f1ab122011-12-12 08:52:43 -08002379 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002380 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Sebastien Hertz5243e912013-05-21 10:55:07 +02002381 uint32_t orig_type_reg = (is_checkcast) ? inst->VRegA_21c() : inst->VRegB_22c();
Ian Rogers7b078e82014-09-10 14:44:24 -07002382 const RegType& orig_type = work_line_->GetRegisterType(this, orig_type_reg);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002383 if (!res_type.IsNonZeroReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002384 if (is_checkcast) {
2385 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on unexpected class " << res_type;
2386 } else {
2387 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on unexpected class " << res_type;
2388 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002389 } else if (!orig_type.IsReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002390 if (is_checkcast) {
2391 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on non-reference in v" << orig_type_reg;
2392 } else {
2393 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on non-reference in v" << orig_type_reg;
2394 }
David Brazdil68b5c0b2016-01-19 14:25:29 +00002395 } else if (orig_type.IsUninitializedTypes()) {
2396 if (is_checkcast) {
2397 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on uninitialized reference in v"
2398 << orig_type_reg;
2399 } else {
2400 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on uninitialized reference in v"
2401 << orig_type_reg;
2402 }
jeffhao2a8a90e2011-09-26 14:25:31 -07002403 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002404 if (is_checkcast) {
Andreas Gampead238ce2015-08-24 21:13:08 -07002405 work_line_->SetRegisterType<LockOp::kKeep>(this, inst->VRegA_21c(), res_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07002406 } else {
Andreas Gampead238ce2015-08-24 21:13:08 -07002407 work_line_->SetRegisterType<LockOp::kClear>(this,
2408 inst->VRegA_22c(),
2409 reg_types_.Boolean());
jeffhaobdb76512011-09-07 11:43:16 -07002410 }
jeffhaobdb76512011-09-07 11:43:16 -07002411 }
jeffhao2a8a90e2011-09-26 14:25:31 -07002412 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002413 }
2414 case Instruction::ARRAY_LENGTH: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002415 const RegType& res_type = work_line_->GetRegisterType(this, inst->VRegB_12x());
Ian Rogers28ad40d2011-10-27 15:19:26 -07002416 if (res_type.IsReferenceTypes()) {
Andreas Gampeeed3a5d2017-11-29 14:58:34 -08002417 if (!res_type.IsArrayTypes() && !res_type.IsZeroOrNull()) {
2418 // ie not an array or null
jeffhaod5347e02012-03-22 17:25:05 -07002419 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002420 } else {
Andreas Gampead238ce2015-08-24 21:13:08 -07002421 work_line_->SetRegisterType<LockOp::kClear>(this,
2422 inst->VRegA_12x(),
2423 reg_types_.Integer());
Ian Rogersd81871c2011-10-03 13:57:23 -07002424 }
Andreas Gampe65c9db82014-07-28 13:14:34 -07002425 } else {
2426 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002427 }
2428 break;
2429 }
2430 case Instruction::NEW_INSTANCE: {
Andreas Gampe98be1a92017-08-28 08:25:45 -07002431 const RegType& res_type = ResolveClass<CheckAccess::kYes>(dex::TypeIndex(inst->VRegB_21c()));
Ian Rogersad0b3a32012-04-16 14:50:24 -07002432 if (res_type.IsConflict()) {
2433 DCHECK_NE(failures_.size(), 0U);
2434 break; // bad class
jeffhao8cd6dda2012-02-22 10:15:34 -08002435 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002436 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
2437 // can't create an instance of an interface or abstract class */
2438 if (!res_type.IsInstantiableTypes()) {
2439 Fail(VERIFY_ERROR_INSTANTIATION)
2440 << "new-instance on primitive, interface or abstract class" << res_type;
Ian Rogers08f753d2012-08-24 14:35:25 -07002441 // Soft failure so carry on to set register type.
Ian Rogersd81871c2011-10-03 13:57:23 -07002442 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00002443 const RegType& uninit_type = reg_types_.Uninitialized(res_type, work_insn_idx_);
Ian Rogers08f753d2012-08-24 14:35:25 -07002444 // Any registers holding previous allocations from this address that have not yet been
2445 // initialized must be marked invalid.
Ian Rogers7b078e82014-09-10 14:44:24 -07002446 work_line_->MarkUninitRefsAsInvalid(this, uninit_type);
Ian Rogers08f753d2012-08-24 14:35:25 -07002447 // add the new uninitialized reference to the register state
Andreas Gampead238ce2015-08-24 21:13:08 -07002448 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_21c(), uninit_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07002449 break;
2450 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08002451 case Instruction::NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002452 VerifyNewArray(inst, false, false);
jeffhaobdb76512011-09-07 11:43:16 -07002453 break;
2454 case Instruction::FILLED_NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002455 VerifyNewArray(inst, true, false);
Ian Rogers0c4a5062012-02-03 15:18:59 -08002456 just_set_result = true; // Filled new array sets result register
jeffhaobdb76512011-09-07 11:43:16 -07002457 break;
Ian Rogers0c4a5062012-02-03 15:18:59 -08002458 case Instruction::FILLED_NEW_ARRAY_RANGE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002459 VerifyNewArray(inst, true, true);
Ian Rogers0c4a5062012-02-03 15:18:59 -08002460 just_set_result = true; // Filled new array range sets result register
2461 break;
jeffhaobdb76512011-09-07 11:43:16 -07002462 case Instruction::CMPL_FLOAT:
2463 case Instruction::CMPG_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002464 if (!work_line_->VerifyRegisterType(this, inst->VRegB_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08002465 break;
2466 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002467 if (!work_line_->VerifyRegisterType(this, inst->VRegC_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08002468 break;
2469 }
Andreas Gampead238ce2015-08-24 21:13:08 -07002470 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002471 break;
2472 case Instruction::CMPL_DOUBLE:
2473 case Instruction::CMPG_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002474 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegB_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002475 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002476 break;
2477 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002478 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegC_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002479 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002480 break;
2481 }
Andreas Gampead238ce2015-08-24 21:13:08 -07002482 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002483 break;
2484 case Instruction::CMP_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002485 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegB_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002486 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002487 break;
2488 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002489 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegC_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002490 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002491 break;
2492 }
Andreas Gampead238ce2015-08-24 21:13:08 -07002493 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002494 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002495 case Instruction::THROW: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002496 const RegType& res_type = work_line_->GetRegisterType(this, inst->VRegA_11x());
David Brazdilca3c8c32016-09-06 14:04:48 +01002497 if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(res_type, this)) {
David Brazdil68b5c0b2016-01-19 14:25:29 +00002498 if (res_type.IsUninitializedTypes()) {
2499 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "thrown exception not initialized";
Pavel Vyssotski980027c2016-02-11 20:28:11 +06002500 } else if (!res_type.IsReferenceTypes()) {
2501 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "thrown value of non-reference type " << res_type;
David Brazdil68b5c0b2016-01-19 14:25:29 +00002502 } else {
2503 Fail(res_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS : VERIFY_ERROR_BAD_CLASS_SOFT)
2504 << "thrown class " << res_type << " not instanceof Throwable";
2505 }
jeffhaobdb76512011-09-07 11:43:16 -07002506 }
2507 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002508 }
jeffhaobdb76512011-09-07 11:43:16 -07002509 case Instruction::GOTO:
2510 case Instruction::GOTO_16:
2511 case Instruction::GOTO_32:
2512 /* no effect on or use of registers */
2513 break;
2514
2515 case Instruction::PACKED_SWITCH:
2516 case Instruction::SPARSE_SWITCH:
2517 /* verify that vAA is an integer, or can be converted to one */
Ian Rogers7b078e82014-09-10 14:44:24 -07002518 work_line_->VerifyRegisterType(this, inst->VRegA_31t(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002519 break;
2520
Ian Rogersd81871c2011-10-03 13:57:23 -07002521 case Instruction::FILL_ARRAY_DATA: {
2522 /* Similar to the verification done for APUT */
Ian Rogers7b078e82014-09-10 14:44:24 -07002523 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegA_31t());
Ian Rogers89310de2012-02-01 13:47:30 -08002524 /* array_type can be null if the reg type is Zero */
Andreas Gampeeed3a5d2017-11-29 14:58:34 -08002525 if (!array_type.IsZeroOrNull()) {
jeffhao457cc512012-02-02 16:55:13 -08002526 if (!array_type.IsArrayTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002527 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with array type "
2528 << array_type;
Andreas Gampebb18a032016-03-22 20:34:25 -07002529 } else if (array_type.IsUnresolvedTypes()) {
2530 // If it's an unresolved array type, it must be non-primitive.
2531 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data for array of type "
2532 << array_type;
Ian Rogers89310de2012-02-01 13:47:30 -08002533 } else {
Ian Rogersd8f69b02014-09-10 21:43:52 +00002534 const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
Ian Rogersad0b3a32012-04-16 14:50:24 -07002535 DCHECK(!component_type.IsConflict());
jeffhao457cc512012-02-02 16:55:13 -08002536 if (component_type.IsNonZeroReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002537 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with component type "
2538 << component_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002539 } else {
jeffhao457cc512012-02-02 16:55:13 -08002540 // Now verify if the element width in the table matches the element width declared in
2541 // the array
Andreas Gampe53de99c2015-08-17 13:43:55 -07002542 const uint16_t* array_data =
2543 insns + (insns[1] | (static_cast<int32_t>(insns[2]) << 16));
jeffhao457cc512012-02-02 16:55:13 -08002544 if (array_data[0] != Instruction::kArrayDataSignature) {
jeffhaod5347e02012-03-22 17:25:05 -07002545 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid magic for array-data";
jeffhao457cc512012-02-02 16:55:13 -08002546 } else {
2547 size_t elem_width = Primitive::ComponentSize(component_type.GetPrimitiveType());
2548 // Since we don't compress the data in Dex, expect to see equal width of data stored
2549 // in the table and expected from the array class.
2550 if (array_data[1] != elem_width) {
jeffhaod5347e02012-03-22 17:25:05 -07002551 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-data size mismatch (" << array_data[1]
2552 << " vs " << elem_width << ")";
jeffhao457cc512012-02-02 16:55:13 -08002553 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002554 }
2555 }
jeffhaobdb76512011-09-07 11:43:16 -07002556 }
2557 }
2558 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002559 }
jeffhaobdb76512011-09-07 11:43:16 -07002560 case Instruction::IF_EQ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002561 case Instruction::IF_NE: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002562 const RegType& reg_type1 = work_line_->GetRegisterType(this, inst->VRegA_22t());
2563 const RegType& reg_type2 = work_line_->GetRegisterType(this, inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002564 bool mismatch = false;
Andreas Gampeeed3a5d2017-11-29 14:58:34 -08002565 if (reg_type1.IsZeroOrNull()) { // zero then integral or reference expected
Ian Rogersd81871c2011-10-03 13:57:23 -07002566 mismatch = !reg_type2.IsReferenceTypes() && !reg_type2.IsIntegralTypes();
2567 } else if (reg_type1.IsReferenceTypes()) { // both references?
2568 mismatch = !reg_type2.IsReferenceTypes();
2569 } else { // both integral?
2570 mismatch = !reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes();
2571 }
2572 if (mismatch) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002573 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to if-eq/if-ne (" << reg_type1 << ","
2574 << reg_type2 << ") must both be references or integral";
jeffhaobdb76512011-09-07 11:43:16 -07002575 }
2576 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002577 }
jeffhaobdb76512011-09-07 11:43:16 -07002578 case Instruction::IF_LT:
2579 case Instruction::IF_GE:
2580 case Instruction::IF_GT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002581 case Instruction::IF_LE: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002582 const RegType& reg_type1 = work_line_->GetRegisterType(this, inst->VRegA_22t());
2583 const RegType& reg_type2 = work_line_->GetRegisterType(this, inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002584 if (!reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002585 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to 'if' (" << reg_type1 << ","
2586 << reg_type2 << ") must be integral";
jeffhaobdb76512011-09-07 11:43:16 -07002587 }
2588 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002589 }
jeffhaobdb76512011-09-07 11:43:16 -07002590 case Instruction::IF_EQZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002591 case Instruction::IF_NEZ: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002592 const RegType& reg_type = work_line_->GetRegisterType(this, inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002593 if (!reg_type.IsReferenceTypes() && !reg_type.IsIntegralTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002594 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
2595 << " unexpected as arg to if-eqz/if-nez";
Ian Rogersd81871c2011-10-03 13:57:23 -07002596 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002597
2598 // Find previous instruction - its existence is a precondition to peephole optimization.
Ian Rogers9b360392013-06-06 14:45:07 -07002599 uint32_t instance_of_idx = 0;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002600 if (0 != work_insn_idx_) {
Ian Rogers9b360392013-06-06 14:45:07 -07002601 instance_of_idx = work_insn_idx_ - 1;
Mathieu Chartierde40d472015-10-15 17:47:48 -07002602 while (0 != instance_of_idx && !GetInstructionFlags(instance_of_idx).IsOpcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07002603 instance_of_idx--;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002604 }
Mathieu Chartierc2a2bda2018-03-12 11:21:52 -07002605 if (FailOrAbort(GetInstructionFlags(instance_of_idx).IsOpcode(),
Andreas Gampe7c038102014-10-27 20:08:46 -07002606 "Unable to get previous instruction of if-eqz/if-nez for work index ",
2607 work_insn_idx_)) {
2608 break;
2609 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002610 } else {
2611 break;
2612 }
2613
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08002614 const Instruction& instance_of_inst = code_item_accessor_.InstructionAt(instance_of_idx);
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002615
2616 /* Check for peep-hole pattern of:
2617 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002618 * instance-of vX, vY, T;
2619 * ifXXX vX, label ;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002620 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002621 * label:
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002622 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002623 * and sharpen the type of vY to be type T.
2624 * Note, this pattern can't be if:
2625 * - if there are other branches to this branch,
2626 * - when vX == vY.
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002627 */
Ian Rogersfae370a2013-06-05 08:33:27 -07002628 if (!CurrentInsnFlags()->IsBranchTarget() &&
Vladimir Markod7559b72017-09-28 13:50:37 +01002629 (Instruction::INSTANCE_OF == instance_of_inst.Opcode()) &&
2630 (inst->VRegA_21t() == instance_of_inst.VRegA_22c()) &&
2631 (instance_of_inst.VRegA_22c() != instance_of_inst.VRegB_22c())) {
Ian Rogersebbdd872014-07-07 23:53:08 -07002632 // Check the type of the instance-of is different than that of registers type, as if they
2633 // are the same there is no work to be done here. Check that the conversion is not to or
2634 // from an unresolved type as type information is imprecise. If the instance-of is to an
2635 // interface then ignore the type information as interfaces can only be treated as Objects
2636 // and we don't want to disallow field and other operations on the object. If the value
2637 // being instance-of checked against is known null (zero) then allow the optimization as
2638 // we didn't have type information. If the merge of the instance-of type with the original
2639 // type is assignable to the original then allow optimization. This check is performed to
2640 // ensure that subsequent merges don't lose type information - such as becoming an
2641 // interface from a class that would lose information relevant to field checks.
Vladimir Markod7559b72017-09-28 13:50:37 +01002642 const RegType& orig_type = work_line_->GetRegisterType(this, instance_of_inst.VRegB_22c());
Andreas Gampe98be1a92017-08-28 08:25:45 -07002643 const RegType& cast_type = ResolveClass<CheckAccess::kYes>(
Vladimir Markod7559b72017-09-28 13:50:37 +01002644 dex::TypeIndex(instance_of_inst.VRegC_22c()));
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002645
Ian Rogersebbdd872014-07-07 23:53:08 -07002646 if (!orig_type.Equals(cast_type) &&
2647 !cast_type.IsUnresolvedTypes() && !orig_type.IsUnresolvedTypes() &&
Andreas Gampe00633eb2014-07-17 16:13:35 -07002648 cast_type.HasClass() && // Could be conflict type, make sure it has a class.
Ian Rogersebbdd872014-07-07 23:53:08 -07002649 !cast_type.GetClass()->IsInterface() &&
Andreas Gampeeed3a5d2017-11-29 14:58:34 -08002650 (orig_type.IsZeroOrNull() ||
David Brazdilca3c8c32016-09-06 14:04:48 +01002651 orig_type.IsStrictlyAssignableFrom(
2652 cast_type.Merge(orig_type, &reg_types_, this), this))) {
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08002653 RegisterLine* update_line = RegisterLine::Create(code_item_accessor_.RegistersSize(),
2654 this);
Ian Rogersfae370a2013-06-05 08:33:27 -07002655 if (inst->Opcode() == Instruction::IF_EQZ) {
Ian Rogers9b360392013-06-06 14:45:07 -07002656 fallthrough_line.reset(update_line);
Ian Rogersfae370a2013-06-05 08:33:27 -07002657 } else {
Ian Rogers9b360392013-06-06 14:45:07 -07002658 branch_line.reset(update_line);
2659 }
2660 update_line->CopyFromLine(work_line_.get());
Andreas Gampead238ce2015-08-24 21:13:08 -07002661 update_line->SetRegisterType<LockOp::kKeep>(this,
Vladimir Markod7559b72017-09-28 13:50:37 +01002662 instance_of_inst.VRegB_22c(),
Andreas Gampead238ce2015-08-24 21:13:08 -07002663 cast_type);
Mathieu Chartierde40d472015-10-15 17:47:48 -07002664 if (!GetInstructionFlags(instance_of_idx).IsBranchTarget() && 0 != instance_of_idx) {
Ian Rogers9b360392013-06-06 14:45:07 -07002665 // See if instance-of was preceded by a move-object operation, common due to the small
2666 // register encoding space of instance-of, and propagate type information to the source
2667 // of the move-object.
2668 uint32_t move_idx = instance_of_idx - 1;
Mathieu Chartierde40d472015-10-15 17:47:48 -07002669 while (0 != move_idx && !GetInstructionFlags(move_idx).IsOpcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07002670 move_idx--;
2671 }
Mathieu Chartierc2a2bda2018-03-12 11:21:52 -07002672 if (FailOrAbort(GetInstructionFlags(move_idx).IsOpcode(),
Andreas Gampe7c038102014-10-27 20:08:46 -07002673 "Unable to get previous instruction of if-eqz/if-nez for work index ",
2674 work_insn_idx_)) {
2675 break;
2676 }
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08002677 const Instruction& move_inst = code_item_accessor_.InstructionAt(move_idx);
Vladimir Markod7559b72017-09-28 13:50:37 +01002678 switch (move_inst.Opcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07002679 case Instruction::MOVE_OBJECT:
Vladimir Markod7559b72017-09-28 13:50:37 +01002680 if (move_inst.VRegA_12x() == instance_of_inst.VRegB_22c()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07002681 update_line->SetRegisterType<LockOp::kKeep>(this,
Vladimir Markod7559b72017-09-28 13:50:37 +01002682 move_inst.VRegB_12x(),
Andreas Gampead238ce2015-08-24 21:13:08 -07002683 cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002684 }
2685 break;
2686 case Instruction::MOVE_OBJECT_FROM16:
Vladimir Markod7559b72017-09-28 13:50:37 +01002687 if (move_inst.VRegA_22x() == instance_of_inst.VRegB_22c()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07002688 update_line->SetRegisterType<LockOp::kKeep>(this,
Vladimir Markod7559b72017-09-28 13:50:37 +01002689 move_inst.VRegB_22x(),
Andreas Gampead238ce2015-08-24 21:13:08 -07002690 cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002691 }
2692 break;
2693 case Instruction::MOVE_OBJECT_16:
Vladimir Markod7559b72017-09-28 13:50:37 +01002694 if (move_inst.VRegA_32x() == instance_of_inst.VRegB_22c()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07002695 update_line->SetRegisterType<LockOp::kKeep>(this,
Vladimir Markod7559b72017-09-28 13:50:37 +01002696 move_inst.VRegB_32x(),
Andreas Gampead238ce2015-08-24 21:13:08 -07002697 cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002698 }
2699 break;
2700 default:
2701 break;
2702 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002703 }
2704 }
2705 }
2706
jeffhaobdb76512011-09-07 11:43:16 -07002707 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002708 }
jeffhaobdb76512011-09-07 11:43:16 -07002709 case Instruction::IF_LTZ:
2710 case Instruction::IF_GEZ:
2711 case Instruction::IF_GTZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002712 case Instruction::IF_LEZ: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002713 const RegType& reg_type = work_line_->GetRegisterType(this, inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002714 if (!reg_type.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002715 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
2716 << " unexpected as arg to if-ltz/if-gez/if-gtz/if-lez";
Ian Rogersd81871c2011-10-03 13:57:23 -07002717 }
jeffhaobdb76512011-09-07 11:43:16 -07002718 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002719 }
jeffhaobdb76512011-09-07 11:43:16 -07002720 case Instruction::AGET_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002721 VerifyAGet(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002722 break;
jeffhaobdb76512011-09-07 11:43:16 -07002723 case Instruction::AGET_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002724 VerifyAGet(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002725 break;
jeffhaobdb76512011-09-07 11:43:16 -07002726 case Instruction::AGET_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002727 VerifyAGet(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002728 break;
jeffhaobdb76512011-09-07 11:43:16 -07002729 case Instruction::AGET_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002730 VerifyAGet(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002731 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002732 case Instruction::AGET:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002733 VerifyAGet(inst, reg_types_.Integer(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002734 break;
jeffhaobdb76512011-09-07 11:43:16 -07002735 case Instruction::AGET_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002736 VerifyAGet(inst, reg_types_.LongLo(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002737 break;
2738 case Instruction::AGET_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002739 VerifyAGet(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07002740 break;
2741
Ian Rogersd81871c2011-10-03 13:57:23 -07002742 case Instruction::APUT_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002743 VerifyAPut(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002744 break;
2745 case Instruction::APUT_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002746 VerifyAPut(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002747 break;
2748 case Instruction::APUT_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002749 VerifyAPut(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002750 break;
2751 case Instruction::APUT_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002752 VerifyAPut(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002753 break;
2754 case Instruction::APUT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002755 VerifyAPut(inst, reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002756 break;
2757 case Instruction::APUT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002758 VerifyAPut(inst, reg_types_.LongLo(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002759 break;
2760 case Instruction::APUT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002761 VerifyAPut(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07002762 break;
2763
jeffhaobdb76512011-09-07 11:43:16 -07002764 case Instruction::IGET_BOOLEAN:
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00002765 case Instruction::IGET_BOOLEAN_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002766 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002767 break;
jeffhaobdb76512011-09-07 11:43:16 -07002768 case Instruction::IGET_BYTE:
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00002769 case Instruction::IGET_BYTE_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002770 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002771 break;
jeffhaobdb76512011-09-07 11:43:16 -07002772 case Instruction::IGET_CHAR:
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00002773 case Instruction::IGET_CHAR_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002774 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002775 break;
jeffhaobdb76512011-09-07 11:43:16 -07002776 case Instruction::IGET_SHORT:
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00002777 case Instruction::IGET_SHORT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002778 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002779 break;
2780 case Instruction::IGET:
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00002781 case Instruction::IGET_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002782 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002783 break;
2784 case Instruction::IGET_WIDE:
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00002785 case Instruction::IGET_WIDE_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002786 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002787 break;
2788 case Instruction::IGET_OBJECT:
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00002789 case Instruction::IGET_OBJECT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002790 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false,
2791 false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002792 break;
jeffhaobdb76512011-09-07 11:43:16 -07002793
Ian Rogersd81871c2011-10-03 13:57:23 -07002794 case Instruction::IPUT_BOOLEAN:
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00002795 case Instruction::IPUT_BOOLEAN_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002796 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002797 break;
2798 case Instruction::IPUT_BYTE:
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00002799 case Instruction::IPUT_BYTE_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002800 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002801 break;
2802 case Instruction::IPUT_CHAR:
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00002803 case Instruction::IPUT_CHAR_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002804 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002805 break;
2806 case Instruction::IPUT_SHORT:
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00002807 case Instruction::IPUT_SHORT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002808 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002809 break;
2810 case Instruction::IPUT:
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00002811 case Instruction::IPUT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002812 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002813 break;
2814 case Instruction::IPUT_WIDE:
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00002815 case Instruction::IPUT_WIDE_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002816 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002817 break;
jeffhaobdb76512011-09-07 11:43:16 -07002818 case Instruction::IPUT_OBJECT:
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00002819 case Instruction::IPUT_OBJECT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002820 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false,
2821 false);
jeffhaobdb76512011-09-07 11:43:16 -07002822 break;
2823
jeffhaobdb76512011-09-07 11:43:16 -07002824 case Instruction::SGET_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002825 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002826 break;
jeffhaobdb76512011-09-07 11:43:16 -07002827 case Instruction::SGET_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002828 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002829 break;
jeffhaobdb76512011-09-07 11:43:16 -07002830 case Instruction::SGET_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002831 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002832 break;
jeffhaobdb76512011-09-07 11:43:16 -07002833 case Instruction::SGET_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002834 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002835 break;
2836 case Instruction::SGET:
Andreas Gampe896df402014-10-20 22:25:29 -07002837 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002838 break;
2839 case Instruction::SGET_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002840 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002841 break;
2842 case Instruction::SGET_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002843 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false,
2844 true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002845 break;
2846
2847 case Instruction::SPUT_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002848 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002849 break;
2850 case Instruction::SPUT_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002851 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002852 break;
2853 case Instruction::SPUT_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002854 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002855 break;
2856 case Instruction::SPUT_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002857 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002858 break;
2859 case Instruction::SPUT:
Andreas Gampe896df402014-10-20 22:25:29 -07002860 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002861 break;
2862 case Instruction::SPUT_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002863 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002864 break;
2865 case Instruction::SPUT_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002866 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false,
2867 true);
jeffhaobdb76512011-09-07 11:43:16 -07002868 break;
2869
2870 case Instruction::INVOKE_VIRTUAL:
2871 case Instruction::INVOKE_VIRTUAL_RANGE:
2872 case Instruction::INVOKE_SUPER:
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00002873 case Instruction::INVOKE_SUPER_RANGE:
2874 case Instruction::INVOKE_VIRTUAL_QUICK:
2875 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002876 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE ||
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00002877 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE ||
2878 inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002879 bool is_super = (inst->Opcode() == Instruction::INVOKE_SUPER ||
2880 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
Alex Light7268d472016-01-20 15:50:01 -08002881 MethodType type = is_super ? METHOD_SUPER : METHOD_VIRTUAL;
2882 ArtMethod* called_method = VerifyInvocationArgs(inst, type, is_range);
Ian Rogersd8f69b02014-09-10 21:43:52 +00002883 const RegType* return_type = nullptr;
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002884 if (called_method != nullptr) {
Vladimir Markob45528c2017-07-27 14:14:28 +01002885 ObjPtr<mirror::Class> return_type_class = can_load_classes_
2886 ? called_method->ResolveReturnType()
2887 : called_method->LookupResolvedReturnType();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002888 if (return_type_class != nullptr) {
Andreas Gampef23f33d2015-06-23 14:18:17 -07002889 return_type = &FromClass(called_method->GetReturnTypeDescriptor(),
Vladimir Markob45528c2017-07-27 14:14:28 +01002890 return_type_class.Ptr(),
Andreas Gampef23f33d2015-06-23 14:18:17 -07002891 return_type_class->CannotBeAssignedFromOtherTypes());
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002892 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07002893 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
2894 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002895 }
2896 }
2897 if (return_type == nullptr) {
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00002898 uint32_t method_idx = GetMethodIdxOfInvoke(inst);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002899 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
Andreas Gampea5b09a62016-11-17 15:21:22 -08002900 dex::TypeIndex return_type_idx =
2901 dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002902 const char* descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Mathieu Chartierbf99f772014-08-23 16:37:27 -07002903 return_type = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
jeffhaobdb76512011-09-07 11:43:16 -07002904 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002905 if (!return_type->IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002906 work_line_->SetResultRegisterType(this, *return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002907 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002908 work_line_->SetResultRegisterTypeWide(*return_type, return_type->HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002909 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002910 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002911 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002912 }
jeffhaobdb76512011-09-07 11:43:16 -07002913 case Instruction::INVOKE_DIRECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002914 case Instruction::INVOKE_DIRECT_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002915 bool is_range = (inst->Opcode() == Instruction::INVOKE_DIRECT_RANGE);
Alex Light7268d472016-01-20 15:50:01 -08002916 ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_DIRECT, is_range);
Ian Rogers46685432012-06-03 22:26:43 -07002917 const char* return_type_descriptor;
2918 bool is_constructor;
Ian Rogersd8f69b02014-09-10 21:43:52 +00002919 const RegType* return_type = nullptr;
Ian Rogers7b078e82014-09-10 14:44:24 -07002920 if (called_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002921 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogers46685432012-06-03 22:26:43 -07002922 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
Ian Rogersdfb325e2013-10-30 01:00:44 -07002923 is_constructor = strcmp("<init>", dex_file_->StringDataByIdx(method_id.name_idx_)) == 0;
Andreas Gampea5b09a62016-11-17 15:21:22 -08002924 dex::TypeIndex return_type_idx =
2925 dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogers46685432012-06-03 22:26:43 -07002926 return_type_descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2927 } else {
2928 is_constructor = called_method->IsConstructor();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002929 return_type_descriptor = called_method->GetReturnTypeDescriptor();
Vladimir Markob45528c2017-07-27 14:14:28 +01002930 ObjPtr<mirror::Class> return_type_class = can_load_classes_
2931 ? called_method->ResolveReturnType()
2932 : called_method->LookupResolvedReturnType();
Ian Rogers1ff3c982014-08-12 02:30:58 -07002933 if (return_type_class != nullptr) {
Andreas Gampef23f33d2015-06-23 14:18:17 -07002934 return_type = &FromClass(return_type_descriptor,
Vladimir Markobcf17522018-06-01 13:14:32 +01002935 return_type_class,
Andreas Gampef23f33d2015-06-23 14:18:17 -07002936 return_type_class->CannotBeAssignedFromOtherTypes());
Ian Rogers1ff3c982014-08-12 02:30:58 -07002937 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07002938 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
2939 self_->ClearException();
Ian Rogers1ff3c982014-08-12 02:30:58 -07002940 }
Ian Rogers46685432012-06-03 22:26:43 -07002941 }
2942 if (is_constructor) {
jeffhaobdb76512011-09-07 11:43:16 -07002943 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002944 * Some additional checks when calling a constructor. We know from the invocation arg check
2945 * that the "this" argument is an instance of called_method->klass. Now we further restrict
2946 * that to require that called_method->klass is the same as this->klass or this->super,
2947 * allowing the latter only if the "this" argument is the same as the "this" argument to
2948 * this method (which implies that we're in a constructor ourselves).
jeffhaobdb76512011-09-07 11:43:16 -07002949 */
Orion Hodsoncfa325e2016-10-13 10:25:54 +01002950 const RegType& this_type = work_line_->GetInvocationThis(this, inst);
jeffhaob57e9522012-04-26 18:08:21 -07002951 if (this_type.IsConflict()) // failure.
2952 break;
jeffhaobdb76512011-09-07 11:43:16 -07002953
jeffhaob57e9522012-04-26 18:08:21 -07002954 /* no null refs allowed (?) */
Andreas Gampeeed3a5d2017-11-29 14:58:34 -08002955 if (this_type.IsZeroOrNull()) {
jeffhaob57e9522012-04-26 18:08:21 -07002956 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unable to initialize null ref";
2957 break;
jeffhao2a8a90e2011-09-26 14:25:31 -07002958 }
jeffhaob57e9522012-04-26 18:08:21 -07002959
2960 /* must be in same class or in superclass */
Ian Rogersd8f69b02014-09-10 21:43:52 +00002961 // const RegType& this_super_klass = this_type.GetSuperClass(&reg_types_);
Ian Rogers46685432012-06-03 22:26:43 -07002962 // TODO: re-enable constructor type verification
2963 // if (this_super_klass.IsConflict()) {
jeffhaob57e9522012-04-26 18:08:21 -07002964 // Unknown super class, fail so we re-check at runtime.
Ian Rogers46685432012-06-03 22:26:43 -07002965 // Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "super class unknown for '" << this_type << "'";
2966 // break;
2967 // }
jeffhaob57e9522012-04-26 18:08:21 -07002968
2969 /* arg must be an uninitialized reference */
2970 if (!this_type.IsUninitializedTypes()) {
2971 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Expected initialization on uninitialized reference "
2972 << this_type;
2973 break;
2974 }
2975
2976 /*
2977 * Replace the uninitialized reference with an initialized one. We need to do this for all
2978 * registers that have the same object instance in them, not just the "this" register.
2979 */
Nicolas Geoffray98e6ce42016-02-16 18:42:15 +00002980 work_line_->MarkRefsAsInitialized(this, this_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002981 }
Ian Rogers1ff3c982014-08-12 02:30:58 -07002982 if (return_type == nullptr) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07002983 return_type = &reg_types_.FromDescriptor(GetClassLoader(), return_type_descriptor, false);
Ian Rogers1ff3c982014-08-12 02:30:58 -07002984 }
2985 if (!return_type->IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002986 work_line_->SetResultRegisterType(this, *return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002987 } else {
Ian Rogers1ff3c982014-08-12 02:30:58 -07002988 work_line_->SetResultRegisterTypeWide(*return_type, return_type->HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002989 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002990 just_set_result = true;
2991 break;
2992 }
2993 case Instruction::INVOKE_STATIC:
2994 case Instruction::INVOKE_STATIC_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002995 bool is_range = (inst->Opcode() == Instruction::INVOKE_STATIC_RANGE);
Alex Light7268d472016-01-20 15:50:01 -08002996 ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_STATIC, is_range);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002997 const char* descriptor;
Ian Rogers7b078e82014-09-10 14:44:24 -07002998 if (called_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002999 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogers28ad40d2011-10-27 15:19:26 -07003000 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
Andreas Gampea5b09a62016-11-17 15:21:22 -08003001 dex::TypeIndex return_type_idx =
3002 dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Mathieu Chartier590fee92013-09-13 13:46:47 -07003003 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07003004 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003005 descriptor = called_method->GetReturnTypeDescriptor();
Ian Rogers28ad40d2011-10-27 15:19:26 -07003006 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00003007 const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003008 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003009 work_line_->SetResultRegisterType(this, return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003010 } else {
3011 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
3012 }
jeffhaobdb76512011-09-07 11:43:16 -07003013 just_set_result = true;
3014 }
3015 break;
jeffhaobdb76512011-09-07 11:43:16 -07003016 case Instruction::INVOKE_INTERFACE:
Ian Rogersd81871c2011-10-03 13:57:23 -07003017 case Instruction::INVOKE_INTERFACE_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003018 bool is_range = (inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE);
Alex Light7268d472016-01-20 15:50:01 -08003019 ArtMethod* abs_method = VerifyInvocationArgs(inst, METHOD_INTERFACE, is_range);
Ian Rogers7b078e82014-09-10 14:44:24 -07003020 if (abs_method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003021 mirror::Class* called_interface = abs_method->GetDeclaringClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07003022 if (!called_interface->IsInterface() && !called_interface->IsObjectClass()) {
3023 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected interface class in invoke-interface '"
David Sehr709b0702016-10-13 09:12:37 -07003024 << abs_method->PrettyMethod() << "'";
Ian Rogersad0b3a32012-04-16 14:50:24 -07003025 break;
Ian Rogers28ad40d2011-10-27 15:19:26 -07003026 }
Ian Rogers0d604842012-04-16 14:50:24 -07003027 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003028 /* Get the type of the "this" arg, which should either be a sub-interface of called
3029 * interface or Object (see comments in RegType::JoinClass).
3030 */
Orion Hodsoncfa325e2016-10-13 10:25:54 +01003031 const RegType& this_type = work_line_->GetInvocationThis(this, inst);
Andreas Gampeeed3a5d2017-11-29 14:58:34 -08003032 if (this_type.IsZeroOrNull()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003033 /* null pointer always passes (and always fails at runtime) */
3034 } else {
3035 if (this_type.IsUninitializedTypes()) {
3036 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface call on uninitialized object "
3037 << this_type;
3038 break;
3039 }
3040 // In the past we have tried to assert that "called_interface" is assignable
3041 // from "this_type.GetClass()", however, as we do an imprecise Join
3042 // (RegType::JoinClass) we don't have full information on what interfaces are
3043 // implemented by "this_type". For example, two classes may implement the same
3044 // interfaces and have a common parent that doesn't implement the interface. The
3045 // join will set "this_type" to the parent class and a test that this implements
3046 // the interface will incorrectly fail.
3047 }
3048 /*
3049 * We don't have an object instance, so we can't find the concrete method. However, all of
3050 * the type information is in the abstract method, so we're good.
3051 */
3052 const char* descriptor;
Ian Rogers7b078e82014-09-10 14:44:24 -07003053 if (abs_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003054 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07003055 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
Andreas Gampea5b09a62016-11-17 15:21:22 -08003056 dex::TypeIndex return_type_idx =
3057 dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Mathieu Chartierde40d472015-10-15 17:47:48 -07003058 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003059 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003060 descriptor = abs_method->GetReturnTypeDescriptor();
Ian Rogersad0b3a32012-04-16 14:50:24 -07003061 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00003062 const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003063 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003064 work_line_->SetResultRegisterType(this, return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003065 } else {
3066 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
3067 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003068 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07003069 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07003070 }
Narayan Kamath9823e782016-08-03 12:46:58 +01003071 case Instruction::INVOKE_POLYMORPHIC:
3072 case Instruction::INVOKE_POLYMORPHIC_RANGE: {
Orion Hodsoncfa325e2016-10-13 10:25:54 +01003073 bool is_range = (inst->Opcode() == Instruction::INVOKE_POLYMORPHIC_RANGE);
3074 ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_POLYMORPHIC, is_range);
3075 if (called_method == nullptr) {
3076 // Convert potential soft failures in VerifyInvocationArgs() to hard errors.
3077 if (failure_messages_.size() > 0) {
3078 std::string message = failure_messages_.back()->str();
3079 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << message;
3080 } else {
3081 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invoke-polymorphic verification failure.";
3082 }
3083 break;
3084 }
3085 if (!CheckSignaturePolymorphicMethod(called_method) ||
3086 !CheckSignaturePolymorphicReceiver(inst)) {
Orion Hodsonfe92d122018-01-02 10:45:17 +00003087 DCHECK(HasFailures());
Orion Hodsoncfa325e2016-10-13 10:25:54 +01003088 break;
3089 }
Orion Hodson06d10a72018-05-14 08:53:38 +01003090 const uint16_t vRegH = (is_range) ? inst->VRegH_4rcc() : inst->VRegH_45cc();
3091 const dex::ProtoIndex proto_idx(vRegH);
Orion Hodsonac141392017-01-13 11:53:47 +00003092 const char* return_descriptor =
Orion Hodsoncfa325e2016-10-13 10:25:54 +01003093 dex_file_->GetReturnTypeDescriptor(dex_file_->GetProtoId(proto_idx));
3094 const RegType& return_type =
Orion Hodsonac141392017-01-13 11:53:47 +00003095 reg_types_.FromDescriptor(GetClassLoader(), return_descriptor, false);
Orion Hodsoncfa325e2016-10-13 10:25:54 +01003096 if (!return_type.IsLowHalf()) {
3097 work_line_->SetResultRegisterType(this, return_type);
3098 } else {
3099 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
3100 }
Orion Hodsonac141392017-01-13 11:53:47 +00003101 just_set_result = true;
Orion Hodsoncfa325e2016-10-13 10:25:54 +01003102 break;
Narayan Kamath9823e782016-08-03 12:46:58 +01003103 }
Orion Hodsonc069a302017-01-18 09:23:12 +00003104 case Instruction::INVOKE_CUSTOM:
3105 case Instruction::INVOKE_CUSTOM_RANGE: {
3106 // Verify registers based on method_type in the call site.
3107 bool is_range = (inst->Opcode() == Instruction::INVOKE_CUSTOM_RANGE);
3108
3109 // Step 1. Check the call site that produces the method handle for invocation
3110 const uint32_t call_site_idx = is_range ? inst->VRegB_3rc() : inst->VRegB_35c();
3111 if (!CheckCallSite(call_site_idx)) {
3112 DCHECK(HasFailures());
3113 break;
3114 }
3115
3116 // Step 2. Check the register arguments correspond to the expected arguments for the
3117 // method handle produced by step 1. The dex file verifier has checked ranges for
3118 // the first three arguments and CheckCallSite has checked the method handle type.
3119 CallSiteArrayValueIterator it(*dex_file_, dex_file_->GetCallSiteId(call_site_idx));
3120 it.Next(); // Skip to name.
3121 it.Next(); // Skip to method type of the method handle
Orion Hodson06d10a72018-05-14 08:53:38 +01003122 const dex::ProtoIndex proto_idx(it.GetJavaValue().c);
Orion Hodsonc069a302017-01-18 09:23:12 +00003123 const DexFile::ProtoId& proto_id = dex_file_->GetProtoId(proto_idx);
3124 DexFileParameterIterator param_it(*dex_file_, proto_id);
3125 // Treat method as static as it has yet to be determined.
3126 VerifyInvocationArgsFromIterator(&param_it, inst, METHOD_STATIC, is_range, nullptr);
3127 const char* return_descriptor = dex_file_->GetReturnTypeDescriptor(proto_id);
3128
3129 // Step 3. Propagate return type information
3130 const RegType& return_type =
3131 reg_types_.FromDescriptor(GetClassLoader(), return_descriptor, false);
3132 if (!return_type.IsLowHalf()) {
3133 work_line_->SetResultRegisterType(this, return_type);
3134 } else {
3135 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
3136 }
3137 just_set_result = true;
3138 // TODO: Add compiler support for invoke-custom (b/35337872).
3139 Fail(VERIFY_ERROR_FORCE_INTERPRETER);
3140 break;
3141 }
jeffhaobdb76512011-09-07 11:43:16 -07003142 case Instruction::NEG_INT:
3143 case Instruction::NOT_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003144 work_line_->CheckUnaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003145 break;
3146 case Instruction::NEG_LONG:
3147 case Instruction::NOT_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07003148 work_line_->CheckUnaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003149 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07003150 break;
3151 case Instruction::NEG_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003152 work_line_->CheckUnaryOp(this, inst, reg_types_.Float(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07003153 break;
3154 case Instruction::NEG_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07003155 work_line_->CheckUnaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003156 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07003157 break;
3158 case Instruction::INT_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07003159 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003160 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003161 break;
3162 case Instruction::INT_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003163 work_line_->CheckUnaryOp(this, inst, reg_types_.Float(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003164 break;
3165 case Instruction::INT_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07003166 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003167 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003168 break;
3169 case Instruction::LONG_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003170 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003171 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07003172 break;
3173 case Instruction::LONG_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003174 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003175 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07003176 break;
3177 case Instruction::LONG_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07003178 work_line_->CheckUnaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003179 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07003180 break;
3181 case Instruction::FLOAT_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003182 work_line_->CheckUnaryOp(this, inst, reg_types_.Integer(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07003183 break;
3184 case Instruction::FLOAT_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07003185 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003186 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07003187 break;
3188 case Instruction::FLOAT_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07003189 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003190 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07003191 break;
3192 case Instruction::DOUBLE_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003193 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003194 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07003195 break;
3196 case Instruction::DOUBLE_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07003197 work_line_->CheckUnaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003198 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07003199 break;
3200 case Instruction::DOUBLE_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003201 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003202 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07003203 break;
3204 case Instruction::INT_TO_BYTE:
Ian Rogers7b078e82014-09-10 14:44:24 -07003205 work_line_->CheckUnaryOp(this, inst, reg_types_.Byte(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003206 break;
3207 case Instruction::INT_TO_CHAR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003208 work_line_->CheckUnaryOp(this, inst, reg_types_.Char(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003209 break;
3210 case Instruction::INT_TO_SHORT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003211 work_line_->CheckUnaryOp(this, inst, reg_types_.Short(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003212 break;
3213
3214 case Instruction::ADD_INT:
3215 case Instruction::SUB_INT:
3216 case Instruction::MUL_INT:
3217 case Instruction::REM_INT:
3218 case Instruction::DIV_INT:
3219 case Instruction::SHL_INT:
3220 case Instruction::SHR_INT:
3221 case Instruction::USHR_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003222 work_line_->CheckBinaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003223 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07003224 break;
3225 case Instruction::AND_INT:
3226 case Instruction::OR_INT:
3227 case Instruction::XOR_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003228 work_line_->CheckBinaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003229 reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07003230 break;
3231 case Instruction::ADD_LONG:
3232 case Instruction::SUB_LONG:
3233 case Instruction::MUL_LONG:
3234 case Instruction::DIV_LONG:
3235 case Instruction::REM_LONG:
3236 case Instruction::AND_LONG:
3237 case Instruction::OR_LONG:
3238 case Instruction::XOR_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07003239 work_line_->CheckBinaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003240 reg_types_.LongLo(), reg_types_.LongHi(),
3241 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07003242 break;
3243 case Instruction::SHL_LONG:
3244 case Instruction::SHR_LONG:
3245 case Instruction::USHR_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07003246 /* shift distance is Int, making these different from other binary operations */
Ian Rogers7b078e82014-09-10 14:44:24 -07003247 work_line_->CheckBinaryOpWideShift(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003248 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003249 break;
3250 case Instruction::ADD_FLOAT:
3251 case Instruction::SUB_FLOAT:
3252 case Instruction::MUL_FLOAT:
3253 case Instruction::DIV_FLOAT:
3254 case Instruction::REM_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003255 work_line_->CheckBinaryOp(this, inst, reg_types_.Float(), reg_types_.Float(),
3256 reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07003257 break;
3258 case Instruction::ADD_DOUBLE:
3259 case Instruction::SUB_DOUBLE:
3260 case Instruction::MUL_DOUBLE:
3261 case Instruction::DIV_DOUBLE:
3262 case Instruction::REM_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07003263 work_line_->CheckBinaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003264 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
3265 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07003266 break;
3267 case Instruction::ADD_INT_2ADDR:
3268 case Instruction::SUB_INT_2ADDR:
3269 case Instruction::MUL_INT_2ADDR:
3270 case Instruction::REM_INT_2ADDR:
3271 case Instruction::SHL_INT_2ADDR:
3272 case Instruction::SHR_INT_2ADDR:
3273 case Instruction::USHR_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003274 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
3275 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07003276 break;
3277 case Instruction::AND_INT_2ADDR:
3278 case Instruction::OR_INT_2ADDR:
3279 case Instruction::XOR_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003280 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
3281 reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07003282 break;
3283 case Instruction::DIV_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003284 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
3285 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07003286 break;
3287 case Instruction::ADD_LONG_2ADDR:
3288 case Instruction::SUB_LONG_2ADDR:
3289 case Instruction::MUL_LONG_2ADDR:
3290 case Instruction::DIV_LONG_2ADDR:
3291 case Instruction::REM_LONG_2ADDR:
3292 case Instruction::AND_LONG_2ADDR:
3293 case Instruction::OR_LONG_2ADDR:
3294 case Instruction::XOR_LONG_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003295 work_line_->CheckBinaryOp2addrWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003296 reg_types_.LongLo(), reg_types_.LongHi(),
3297 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07003298 break;
3299 case Instruction::SHL_LONG_2ADDR:
3300 case Instruction::SHR_LONG_2ADDR:
3301 case Instruction::USHR_LONG_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003302 work_line_->CheckBinaryOp2addrWideShift(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003303 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003304 break;
3305 case Instruction::ADD_FLOAT_2ADDR:
3306 case Instruction::SUB_FLOAT_2ADDR:
3307 case Instruction::MUL_FLOAT_2ADDR:
3308 case Instruction::DIV_FLOAT_2ADDR:
3309 case Instruction::REM_FLOAT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003310 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Float(), reg_types_.Float(),
3311 reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07003312 break;
3313 case Instruction::ADD_DOUBLE_2ADDR:
3314 case Instruction::SUB_DOUBLE_2ADDR:
3315 case Instruction::MUL_DOUBLE_2ADDR:
3316 case Instruction::DIV_DOUBLE_2ADDR:
3317 case Instruction::REM_DOUBLE_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003318 work_line_->CheckBinaryOp2addrWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003319 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
3320 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07003321 break;
3322 case Instruction::ADD_INT_LIT16:
Ian Rogersf72a11d2014-10-30 15:41:08 -07003323 case Instruction::RSUB_INT_LIT16:
jeffhaobdb76512011-09-07 11:43:16 -07003324 case Instruction::MUL_INT_LIT16:
3325 case Instruction::DIV_INT_LIT16:
3326 case Instruction::REM_INT_LIT16:
Ian Rogers7b078e82014-09-10 14:44:24 -07003327 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), false,
3328 true);
jeffhaobdb76512011-09-07 11:43:16 -07003329 break;
3330 case Instruction::AND_INT_LIT16:
3331 case Instruction::OR_INT_LIT16:
3332 case Instruction::XOR_INT_LIT16:
Ian Rogers7b078e82014-09-10 14:44:24 -07003333 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), true,
3334 true);
jeffhaobdb76512011-09-07 11:43:16 -07003335 break;
3336 case Instruction::ADD_INT_LIT8:
3337 case Instruction::RSUB_INT_LIT8:
3338 case Instruction::MUL_INT_LIT8:
3339 case Instruction::DIV_INT_LIT8:
3340 case Instruction::REM_INT_LIT8:
3341 case Instruction::SHL_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07003342 case Instruction::SHR_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07003343 case Instruction::USHR_INT_LIT8:
Ian Rogers7b078e82014-09-10 14:44:24 -07003344 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), false,
3345 false);
jeffhaobdb76512011-09-07 11:43:16 -07003346 break;
3347 case Instruction::AND_INT_LIT8:
3348 case Instruction::OR_INT_LIT8:
3349 case Instruction::XOR_INT_LIT8:
Ian Rogers7b078e82014-09-10 14:44:24 -07003350 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), true,
3351 false);
jeffhaobdb76512011-09-07 11:43:16 -07003352 break;
3353
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003354 // Special instructions.
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07003355 case Instruction::RETURN_VOID_NO_BARRIER:
3356 if (IsConstructor() && !IsStatic()) {
3357 auto& declaring_class = GetDeclaringClass();
Andreas Gampe68df3202015-06-22 11:35:46 -07003358 if (declaring_class.IsUnresolvedReference()) {
3359 // We must iterate over the fields, even if we cannot use mirror classes to do so. Do it
3360 // manually over the underlying dex file.
3361 uint32_t first_index = GetFirstFinalInstanceFieldIndex(*dex_file_,
3362 dex_file_->GetMethodId(dex_method_idx_).class_idx_);
Andreas Gampee2abbc62017-09-15 11:59:26 -07003363 if (first_index != dex::kDexNoIndex) {
Andreas Gampe68df3202015-06-22 11:35:46 -07003364 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void-no-barrier not expected for field "
3365 << first_index;
3366 }
3367 break;
3368 }
Vladimir Marko2cffc5d2018-05-29 15:40:56 +01003369 ObjPtr<mirror::Class> klass = declaring_class.GetClass();
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07003370 for (uint32_t i = 0, num_fields = klass->NumInstanceFields(); i < num_fields; ++i) {
3371 if (klass->GetInstanceField(i)->IsFinal()) {
Mathieu Chartiere86deef2015-03-19 13:43:37 -07003372 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void-no-barrier not expected for "
David Sehr709b0702016-10-13 09:12:37 -07003373 << klass->GetInstanceField(i)->PrettyField();
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07003374 break;
3375 }
3376 }
Sebastien Hertzcc10e0e2013-06-28 14:24:48 +02003377 }
Andreas Gampeb2917962015-07-31 13:36:10 -07003378 // Handle this like a RETURN_VOID now. Code is duplicated to separate standard from
3379 // quickened opcodes (otherwise this could be a fall-through).
3380 if (!IsConstructor()) {
3381 if (!GetMethodReturnType().IsConflict()) {
3382 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void not expected";
3383 }
3384 }
Sebastien Hertzcc10e0e2013-06-28 14:24:48 +02003385 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003386
Ian Rogersd81871c2011-10-03 13:57:23 -07003387 /* These should never appear during verification. */
Mathieu Chartierffc605c2014-12-10 10:35:44 -08003388 case Instruction::UNUSED_3E ... Instruction::UNUSED_43:
Narayan Kamath8ec3bd22016-08-03 12:46:23 +01003389 case Instruction::UNUSED_F3 ... Instruction::UNUSED_F9:
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003390 case Instruction::UNUSED_79:
3391 case Instruction::UNUSED_7A:
jeffhaod5347e02012-03-22 17:25:05 -07003392 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Unexpected opcode " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07003393 break;
3394
3395 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003396 * DO NOT add a "default" clause here. Without it the compiler will
jeffhaobdb76512011-09-07 11:43:16 -07003397 * complain if an instruction is missing (which is desirable).
3398 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003399 } // end - switch (dec_insn.opcode)
jeffhaobdb76512011-09-07 11:43:16 -07003400
Ian Rogersad0b3a32012-04-16 14:50:24 -07003401 if (have_pending_hard_failure_) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08003402 if (Runtime::Current()->IsAotCompiler()) {
3403 /* When AOT compiling, check that the last failure is a hard failure */
Andreas Gampeb588f4c2015-05-26 13:35:39 -07003404 if (failures_[failures_.size() - 1] != VERIFY_ERROR_BAD_CLASS_HARD) {
3405 LOG(ERROR) << "Pending failures:";
3406 for (auto& error : failures_) {
3407 LOG(ERROR) << error;
3408 }
3409 for (auto& error_msg : failure_messages_) {
3410 LOG(ERROR) << error_msg->str();
3411 }
3412 LOG(FATAL) << "Pending hard failure, but last failure not hard.";
3413 }
Ian Rogerse1758fe2012-04-19 11:31:15 -07003414 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003415 /* immediate failure, reject class */
3416 info_messages_ << "Rejecting opcode " << inst->DumpString(dex_file_);
3417 return false;
jeffhaofaf459e2012-08-31 15:32:47 -07003418 } else if (have_pending_runtime_throw_failure_) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07003419 /* checking interpreter will throw, mark following code as unreachable */
jeffhaofaf459e2012-08-31 15:32:47 -07003420 opcode_flags = Instruction::kThrow;
Andreas Gampea727e372015-08-25 09:22:37 -07003421 // Note: the flag must be reset as it is only global to decouple Fail and is semantically per
3422 // instruction. However, RETURN checking may throw LOCKING errors, so we clear at the
3423 // very end.
jeffhaobdb76512011-09-07 11:43:16 -07003424 }
jeffhaobdb76512011-09-07 11:43:16 -07003425 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003426 * If we didn't just set the result register, clear it out. This ensures that you can only use
3427 * "move-result" immediately after the result is set. (We could check this statically, but it's
3428 * not expensive and it makes our debugging output cleaner.)
jeffhaobdb76512011-09-07 11:43:16 -07003429 */
3430 if (!just_set_result) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003431 work_line_->SetResultTypeToUnknown(this);
jeffhaobdb76512011-09-07 11:43:16 -07003432 }
3433
jeffhaobdb76512011-09-07 11:43:16 -07003434 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003435 * Handle "branch". Tag the branch target.
jeffhaobdb76512011-09-07 11:43:16 -07003436 *
3437 * NOTE: instructions like Instruction::EQZ provide information about the
jeffhaod1f0fde2011-09-08 17:25:33 -07003438 * state of the register when the branch is taken or not taken. For example,
jeffhaobdb76512011-09-07 11:43:16 -07003439 * somebody could get a reference field, check it for zero, and if the
3440 * branch is taken immediately store that register in a boolean field
jeffhaod1f0fde2011-09-08 17:25:33 -07003441 * since the value is known to be zero. We do not currently account for
jeffhaobdb76512011-09-07 11:43:16 -07003442 * that, and will reject the code.
3443 *
3444 * TODO: avoid re-fetching the branch target
3445 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003446 if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07003447 bool isConditional, selfOkay;
Ian Rogersd81871c2011-10-03 13:57:23 -07003448 if (!GetBranchOffset(work_insn_idx_, &branch_target, &isConditional, &selfOkay)) {
jeffhaobdb76512011-09-07 11:43:16 -07003449 /* should never happen after static verification */
jeffhaod5347e02012-03-22 17:25:05 -07003450 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad branch";
jeffhaobdb76512011-09-07 11:43:16 -07003451 return false;
3452 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08003453 DCHECK_EQ(isConditional, (opcode_flags & Instruction::kContinue) != 0);
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08003454 if (!CheckNotMoveExceptionOrMoveResult(code_item_accessor_.Insns(),
3455 work_insn_idx_ + branch_target)) {
jeffhaobdb76512011-09-07 11:43:16 -07003456 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07003457 }
jeffhaobdb76512011-09-07 11:43:16 -07003458 /* update branch target, set "changed" if appropriate */
Mathieu Chartierde40d472015-10-15 17:47:48 -07003459 if (nullptr != branch_line) {
Ian Rogersebbdd872014-07-07 23:53:08 -07003460 if (!UpdateRegisters(work_insn_idx_ + branch_target, branch_line.get(), false)) {
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003461 return false;
3462 }
3463 } else {
Ian Rogersebbdd872014-07-07 23:53:08 -07003464 if (!UpdateRegisters(work_insn_idx_ + branch_target, work_line_.get(), false)) {
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003465 return false;
3466 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003467 }
jeffhaobdb76512011-09-07 11:43:16 -07003468 }
3469
3470 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003471 * Handle "switch". Tag all possible branch targets.
jeffhaobdb76512011-09-07 11:43:16 -07003472 *
3473 * We've already verified that the table is structurally sound, so we
3474 * just need to walk through and tag the targets.
3475 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003476 if ((opcode_flags & Instruction::kSwitch) != 0) {
Andreas Gampe53de99c2015-08-17 13:43:55 -07003477 int offset_to_switch = insns[1] | (static_cast<int32_t>(insns[2]) << 16);
jeffhaobdb76512011-09-07 11:43:16 -07003478 const uint16_t* switch_insns = insns + offset_to_switch;
3479 int switch_count = switch_insns[1];
3480 int offset_to_targets, targ;
3481
3482 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
3483 /* 0 = sig, 1 = count, 2/3 = first key */
3484 offset_to_targets = 4;
3485 } else {
3486 /* 0 = sig, 1 = count, 2..count * 2 = keys */
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07003487 DCHECK((*insns & 0xff) == Instruction::SPARSE_SWITCH);
jeffhaobdb76512011-09-07 11:43:16 -07003488 offset_to_targets = 2 + 2 * switch_count;
3489 }
3490
3491 /* verify each switch target */
3492 for (targ = 0; targ < switch_count; targ++) {
3493 int offset;
3494 uint32_t abs_offset;
3495
3496 /* offsets are 32-bit, and only partly endian-swapped */
3497 offset = switch_insns[offset_to_targets + targ * 2] |
Andreas Gampe53de99c2015-08-17 13:43:55 -07003498 (static_cast<int32_t>(switch_insns[offset_to_targets + targ * 2 + 1]) << 16);
Ian Rogersd81871c2011-10-03 13:57:23 -07003499 abs_offset = work_insn_idx_ + offset;
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08003500 DCHECK_LT(abs_offset, code_item_accessor_.InsnsSizeInCodeUnits());
3501 if (!CheckNotMoveExceptionOrMoveResult(code_item_accessor_.Insns(), abs_offset)) {
jeffhaobdb76512011-09-07 11:43:16 -07003502 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07003503 }
Ian Rogersebbdd872014-07-07 23:53:08 -07003504 if (!UpdateRegisters(abs_offset, work_line_.get(), false)) {
jeffhaobdb76512011-09-07 11:43:16 -07003505 return false;
Ian Rogersebbdd872014-07-07 23:53:08 -07003506 }
jeffhaobdb76512011-09-07 11:43:16 -07003507 }
3508 }
3509
3510 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003511 * Handle instructions that can throw and that are sitting in a "try" block. (If they're not in a
3512 * "try" block when they throw, control transfers out of the method.)
jeffhaobdb76512011-09-07 11:43:16 -07003513 */
Mathieu Chartierde40d472015-10-15 17:47:48 -07003514 if ((opcode_flags & Instruction::kThrow) != 0 && GetInstructionFlags(work_insn_idx_).IsInTry()) {
Andreas Gampef91baf12014-07-18 15:41:00 -07003515 bool has_catch_all_handler = false;
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08003516 const DexFile::TryItem* try_item = code_item_accessor_.FindTryItem(work_insn_idx_);
3517 CHECK(try_item != nullptr);
Mathieu Chartierdc578c72017-12-27 11:51:45 -08003518 CatchHandlerIterator iterator(code_item_accessor_, *try_item);
jeffhaobdb76512011-09-07 11:43:16 -07003519
Andreas Gampef91baf12014-07-18 15:41:00 -07003520 // Need the linker to try and resolve the handled class to check if it's Throwable.
3521 ClassLinker* linker = Runtime::Current()->GetClassLinker();
3522
Ian Rogers0571d352011-11-03 19:51:38 -07003523 for (; iterator.HasNext(); iterator.Next()) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08003524 dex::TypeIndex handler_type_idx = iterator.GetHandlerTypeIndex();
3525 if (!handler_type_idx.IsValid()) {
Andreas Gampef91baf12014-07-18 15:41:00 -07003526 has_catch_all_handler = true;
3527 } else {
3528 // It is also a catch-all if it is java.lang.Throwable.
Vladimir Marko28e012a2017-12-07 11:22:59 +00003529 ObjPtr<mirror::Class> klass =
Vladimir Marko666ee3d2017-12-11 18:37:36 +00003530 linker->ResolveType(handler_type_idx, dex_cache_, class_loader_);
Andreas Gampef91baf12014-07-18 15:41:00 -07003531 if (klass != nullptr) {
Vladimir Markoadbceb72018-05-29 14:34:14 +01003532 if (klass == GetClassRoot<mirror::Throwable>()) {
Andreas Gampef91baf12014-07-18 15:41:00 -07003533 has_catch_all_handler = true;
3534 }
3535 } else {
3536 // Clear exception.
Ian Rogers7b078e82014-09-10 14:44:24 -07003537 DCHECK(self_->IsExceptionPending());
3538 self_->ClearException();
Andreas Gampef91baf12014-07-18 15:41:00 -07003539 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003540 }
jeffhaobdb76512011-09-07 11:43:16 -07003541 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003542 * Merge registers into the "catch" block. We want to use the "savedRegs" rather than
3543 * "work_regs", because at runtime the exception will be thrown before the instruction
3544 * modifies any registers.
jeffhaobdb76512011-09-07 11:43:16 -07003545 */
Ian Rogersebbdd872014-07-07 23:53:08 -07003546 if (!UpdateRegisters(iterator.GetHandlerAddress(), saved_line_.get(), false)) {
jeffhaobdb76512011-09-07 11:43:16 -07003547 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07003548 }
jeffhaobdb76512011-09-07 11:43:16 -07003549 }
3550
3551 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003552 * If the monitor stack depth is nonzero, there must be a "catch all" handler for this
3553 * instruction. This does apply to monitor-exit because of async exception handling.
jeffhaobdb76512011-09-07 11:43:16 -07003554 */
Andreas Gampef91baf12014-07-18 15:41:00 -07003555 if (work_line_->MonitorStackDepth() > 0 && !has_catch_all_handler) {
jeffhaobdb76512011-09-07 11:43:16 -07003556 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003557 * The state in work_line reflects the post-execution state. If the current instruction is a
3558 * monitor-enter and the monitor stack was empty, we don't need a catch-all (if it throws,
jeffhaobdb76512011-09-07 11:43:16 -07003559 * it will do so before grabbing the lock).
3560 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02003561 if (inst->Opcode() != Instruction::MONITOR_ENTER || work_line_->MonitorStackDepth() != 1) {
jeffhaod5347e02012-03-22 17:25:05 -07003562 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
Ian Rogersd81871c2011-10-03 13:57:23 -07003563 << "expected to be within a catch-all for an instruction where a monitor is held";
jeffhaobdb76512011-09-07 11:43:16 -07003564 return false;
3565 }
3566 }
3567 }
3568
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003569 /* Handle "continue". Tag the next consecutive instruction.
3570 * Note: Keep the code handling "continue" case below the "branch" and "switch" cases,
3571 * because it changes work_line_ when performing peephole optimization
3572 * and this change should not be used in those cases.
3573 */
Ian Rogers6d376ae2013-07-23 15:12:40 -07003574 if ((opcode_flags & Instruction::kContinue) != 0) {
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08003575 DCHECK_EQ(&code_item_accessor_.InstructionAt(work_insn_idx_), inst);
Ian Rogers7b078e82014-09-10 14:44:24 -07003576 uint32_t next_insn_idx = work_insn_idx_ + inst->SizeInCodeUnits();
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08003577 if (next_insn_idx >= code_item_accessor_.InsnsSizeInCodeUnits()) {
Ian Rogers6d376ae2013-07-23 15:12:40 -07003578 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Execution can walk off end of code area";
3579 return false;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003580 }
Ian Rogers6d376ae2013-07-23 15:12:40 -07003581 // The only way to get to a move-exception instruction is to get thrown there. Make sure the
3582 // next instruction isn't one.
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08003583 if (!CheckNotMoveException(code_item_accessor_.Insns(), next_insn_idx)) {
Ian Rogers6d376ae2013-07-23 15:12:40 -07003584 return false;
3585 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07003586 if (nullptr != fallthrough_line) {
Ian Rogers6d376ae2013-07-23 15:12:40 -07003587 // Make workline consistent with fallthrough computed from peephole optimization.
3588 work_line_->CopyFromLine(fallthrough_line.get());
3589 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07003590 if (GetInstructionFlags(next_insn_idx).IsReturn()) {
Ian Rogersb8c78592013-07-25 23:52:52 +00003591 // For returns we only care about the operand to the return, all other registers are dead.
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08003592 const Instruction* ret_inst = &code_item_accessor_.InstructionAt(next_insn_idx);
Andreas Gampea727e372015-08-25 09:22:37 -07003593 AdjustReturnLine(this, ret_inst, work_line_.get());
Ian Rogersb8c78592013-07-25 23:52:52 +00003594 }
Ian Rogers6d376ae2013-07-23 15:12:40 -07003595 RegisterLine* next_line = reg_table_.GetLine(next_insn_idx);
Ian Rogers7b078e82014-09-10 14:44:24 -07003596 if (next_line != nullptr) {
Ian Rogersebbdd872014-07-07 23:53:08 -07003597 // Merge registers into what we have for the next instruction, and set the "changed" flag if
3598 // needed. If the merge changes the state of the registers then the work line will be
3599 // updated.
3600 if (!UpdateRegisters(next_insn_idx, work_line_.get(), true)) {
Ian Rogers6d376ae2013-07-23 15:12:40 -07003601 return false;
3602 }
3603 } else {
3604 /*
3605 * We're not recording register data for the next instruction, so we don't know what the
3606 * prior state was. We have to assume that something has changed and re-evaluate it.
3607 */
Mathieu Chartierde40d472015-10-15 17:47:48 -07003608 GetInstructionFlags(next_insn_idx).SetChanged();
Ian Rogers6d376ae2013-07-23 15:12:40 -07003609 }
3610 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003611
jeffhaod1f0fde2011-09-08 17:25:33 -07003612 /* If we're returning from the method, make sure monitor stack is empty. */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003613 if ((opcode_flags & Instruction::kReturn) != 0) {
Andreas Gampea727e372015-08-25 09:22:37 -07003614 work_line_->VerifyMonitorStackEmpty(this);
jeffhaobdb76512011-09-07 11:43:16 -07003615 }
3616
3617 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003618 * Update start_guess. Advance to the next instruction of that's
3619 * possible, otherwise use the branch target if one was found. If
jeffhaobdb76512011-09-07 11:43:16 -07003620 * neither of those exists we're in a return or throw; leave start_guess
3621 * alone and let the caller sort it out.
3622 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003623 if ((opcode_flags & Instruction::kContinue) != 0) {
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08003624 DCHECK_EQ(&code_item_accessor_.InstructionAt(work_insn_idx_), inst);
Ian Rogers7b078e82014-09-10 14:44:24 -07003625 *start_guess = work_insn_idx_ + inst->SizeInCodeUnits();
Elliott Hughesadb8c672012-03-06 16:49:32 -08003626 } else if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07003627 /* we're still okay if branch_target is zero */
Ian Rogersd81871c2011-10-03 13:57:23 -07003628 *start_guess = work_insn_idx_ + branch_target;
jeffhaobdb76512011-09-07 11:43:16 -07003629 }
3630
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08003631 DCHECK_LT(*start_guess, code_item_accessor_.InsnsSizeInCodeUnits());
Mathieu Chartierde40d472015-10-15 17:47:48 -07003632 DCHECK(GetInstructionFlags(*start_guess).IsOpcode());
jeffhaobdb76512011-09-07 11:43:16 -07003633
Andreas Gampea727e372015-08-25 09:22:37 -07003634 if (have_pending_runtime_throw_failure_) {
3635 have_any_pending_runtime_throw_failure_ = true;
3636 // Reset the pending_runtime_throw flag now.
3637 have_pending_runtime_throw_failure_ = false;
3638 }
3639
jeffhaobdb76512011-09-07 11:43:16 -07003640 return true;
Brian Carlstrom7934ac22013-07-26 10:54:15 -07003641} // NOLINT(readability/fn_size)
jeffhaobdb76512011-09-07 11:43:16 -07003642
Mathieu Chartierde40d472015-10-15 17:47:48 -07003643void MethodVerifier::UninstantiableError(const char* descriptor) {
3644 Fail(VerifyError::VERIFY_ERROR_NO_CLASS) << "Could not create precise reference for "
3645 << "non-instantiable klass " << descriptor;
3646}
3647
Vladimir Marko28e012a2017-12-07 11:22:59 +00003648inline bool MethodVerifier::IsInstantiableOrPrimitive(ObjPtr<mirror::Class> klass) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07003649 return klass->IsInstantiable() || klass->IsPrimitive();
3650}
3651
Andreas Gampe98be1a92017-08-28 08:25:45 -07003652template <MethodVerifier::CheckAccess C>
3653const RegType& MethodVerifier::ResolveClass(dex::TypeIndex class_idx) {
Vladimir Marko666ee3d2017-12-11 18:37:36 +00003654 ClassLinker* linker = Runtime::Current()->GetClassLinker();
Vladimir Marko28e012a2017-12-07 11:22:59 +00003655 ObjPtr<mirror::Class> klass = can_load_classes_
Vladimir Marko666ee3d2017-12-11 18:37:36 +00003656 ? linker->ResolveType(class_idx, dex_cache_, class_loader_)
3657 : linker->LookupResolvedType(class_idx, dex_cache_.Get(), class_loader_.Get());
Vladimir Marko8d6768d2017-03-14 10:13:21 +00003658 if (can_load_classes_ && klass == nullptr) {
3659 DCHECK(self_->IsExceptionPending());
3660 self_->ClearException();
3661 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07003662 const RegType* result = nullptr;
Vladimir Marko9cb0c462017-04-21 13:31:41 +01003663 if (klass != nullptr) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07003664 bool precise = klass->CannotBeAssignedFromOtherTypes();
3665 if (precise && !IsInstantiableOrPrimitive(klass)) {
3666 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
3667 UninstantiableError(descriptor);
3668 precise = false;
3669 }
Vladimir Marko2cffc5d2018-05-29 15:40:56 +01003670 result = reg_types_.FindClass(klass, precise);
Mathieu Chartierde40d472015-10-15 17:47:48 -07003671 if (result == nullptr) {
3672 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
Vladimir Marko2cffc5d2018-05-29 15:40:56 +01003673 result = reg_types_.InsertClass(descriptor, klass, precise);
Mathieu Chartierde40d472015-10-15 17:47:48 -07003674 }
3675 } else {
3676 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
3677 result = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003678 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07003679 DCHECK(result != nullptr);
3680 if (result->IsConflict()) {
3681 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
3682 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "accessing broken descriptor '" << descriptor
3683 << "' in " << GetDeclaringClass();
3684 return *result;
3685 }
David Brazdilca3c8c32016-09-06 14:04:48 +01003686
3687 // Record result of class resolution attempt.
Vladimir Markobcf17522018-06-01 13:14:32 +01003688 VerifierDeps::MaybeRecordClassResolution(*dex_file_, class_idx, klass);
David Brazdilca3c8c32016-09-06 14:04:48 +01003689
Andreas Gampe629be512017-08-25 17:09:32 -07003690 // If requested, check if access is allowed. Unresolved types are included in this check, as the
3691 // interpreter only tests whether access is allowed when a class is not pre-verified and runs in
3692 // the access-checks interpreter. If result is primitive, skip the access check.
3693 //
3694 // Note: we do this for unresolved classes to trigger re-verification at runtime.
3695 if (C == CheckAccess::kYes && result->IsNonZeroReferenceTypes()) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07003696 const RegType& referrer = GetDeclaringClass();
Andreas Gampe629be512017-08-25 17:09:32 -07003697 if (!referrer.CanAccess(*result)) {
3698 Fail(VERIFY_ERROR_ACCESS_CLASS) << "(possibly) illegal class access: '"
Nicolas Geoffray4e868fa2017-04-21 17:16:44 +01003699 << referrer << "' -> '" << *result << "'";
Mathieu Chartierde40d472015-10-15 17:47:48 -07003700 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07003701 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07003702 return *result;
Ian Rogersd81871c2011-10-03 13:57:23 -07003703}
3704
Andreas Gampedd309072017-08-31 07:47:37 -07003705// Instantiate ResolveClass variants. This is required as the -inl file has a function with a call
3706// to ResolveClass, and compilers may decide to inline, requiring a symbol.
Andreas Gampeae0b1f42017-08-30 17:36:26 -07003707template const RegType& MethodVerifier::ResolveClass<MethodVerifier::CheckAccess::kNo>(
3708 dex::TypeIndex class_idx);
3709template const RegType& MethodVerifier::ResolveClass<MethodVerifier::CheckAccess::kYes>(
3710 dex::TypeIndex class_idx);
3711
Ian Rogersd8f69b02014-09-10 21:43:52 +00003712const RegType& MethodVerifier::GetCaughtExceptionType() {
Ian Rogers7b078e82014-09-10 14:44:24 -07003713 const RegType* common_super = nullptr;
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08003714 if (code_item_accessor_.TriesSize() != 0) {
3715 const uint8_t* handlers_ptr = code_item_accessor_.GetCatchHandlerData();
Ian Rogersd81871c2011-10-03 13:57:23 -07003716 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
3717 for (uint32_t i = 0; i < handlers_size; i++) {
Ian Rogers0571d352011-11-03 19:51:38 -07003718 CatchHandlerIterator iterator(handlers_ptr);
3719 for (; iterator.HasNext(); iterator.Next()) {
3720 if (iterator.GetHandlerAddress() == (uint32_t) work_insn_idx_) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08003721 if (!iterator.GetHandlerTypeIndex().IsValid()) {
Ian Rogersb4903572012-10-11 11:52:56 -07003722 common_super = &reg_types_.JavaLangThrowable(false);
Ian Rogersd81871c2011-10-03 13:57:23 -07003723 } else {
Andreas Gampe98be1a92017-08-28 08:25:45 -07003724 const RegType& exception =
3725 ResolveClass<CheckAccess::kYes>(iterator.GetHandlerTypeIndex());
David Brazdilca3c8c32016-09-06 14:04:48 +01003726 if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(exception, this)) {
David Brazdil68b5c0b2016-01-19 14:25:29 +00003727 DCHECK(!exception.IsUninitializedTypes()); // Comes from dex, shouldn't be uninit.
Jeff Haoc26a56c2013-11-04 12:00:47 -08003728 if (exception.IsUnresolvedTypes()) {
3729 // We don't know enough about the type. Fail here and let runtime handle it.
3730 Fail(VERIFY_ERROR_NO_CLASS) << "unresolved exception class " << exception;
3731 return exception;
3732 } else {
3733 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unexpected non-exception class " << exception;
3734 return reg_types_.Conflict();
3735 }
Jeff Haob878f212014-04-24 16:25:36 -07003736 } else if (common_super == nullptr) {
3737 common_super = &exception;
Ian Rogers28ad40d2011-10-27 15:19:26 -07003738 } else if (common_super->Equals(exception)) {
Ian Rogersc4762272012-02-01 15:55:55 -08003739 // odd case, but nothing to do
Ian Rogersd81871c2011-10-03 13:57:23 -07003740 } else {
David Brazdilca3c8c32016-09-06 14:04:48 +01003741 common_super = &common_super->Merge(exception, &reg_types_, this);
Mathieu Chartierc2a2bda2018-03-12 11:21:52 -07003742 if (FailOrAbort(reg_types_.JavaLangThrowable(false).IsAssignableFrom(
David Brazdilca3c8c32016-09-06 14:04:48 +01003743 *common_super, this),
Andreas Gampe7c038102014-10-27 20:08:46 -07003744 "java.lang.Throwable is not assignable-from common_super at ",
3745 work_insn_idx_)) {
3746 break;
3747 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003748 }
3749 }
3750 }
3751 }
Ian Rogers0571d352011-11-03 19:51:38 -07003752 handlers_ptr = iterator.EndDataPointer();
Ian Rogersd81871c2011-10-03 13:57:23 -07003753 }
3754 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003755 if (common_super == nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003756 /* no catch blocks, or no catches with classes we can find */
jeffhaod5347e02012-03-22 17:25:05 -07003757 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unable to find exception handler";
Ian Rogersad0b3a32012-04-16 14:50:24 -07003758 return reg_types_.Conflict();
Ian Rogersd81871c2011-10-03 13:57:23 -07003759 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07003760 return *common_super;
Ian Rogersd81871c2011-10-03 13:57:23 -07003761}
3762
Mathieu Chartiere401d142015-04-22 13:56:20 -07003763ArtMethod* MethodVerifier::ResolveMethodAndCheckAccess(
Alex Light7268d472016-01-20 15:50:01 -08003764 uint32_t dex_method_idx, MethodType method_type) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003765 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx);
Andreas Gampe98be1a92017-08-28 08:25:45 -07003766 const RegType& klass_type = ResolveClass<CheckAccess::kYes>(method_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003767 if (klass_type.IsConflict()) {
3768 std::string append(" in attempt to access method ");
3769 append += dex_file_->GetMethodName(method_id);
3770 AppendToLastFailMessage(append);
Ian Rogers7b078e82014-09-10 14:44:24 -07003771 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08003772 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003773 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003774 return nullptr; // Can't resolve Class so no more to do here
Ian Rogers90040192011-12-16 08:54:29 -08003775 }
Vladimir Markoba118822017-06-12 15:41:56 +01003776 ObjPtr<mirror::Class> klass = klass_type.GetClass();
Ian Rogersd8f69b02014-09-10 21:43:52 +00003777 const RegType& referrer = GetDeclaringClass();
Nicolas Geoffrayea179f42018-02-08 22:30:18 +00003778 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
3779 PointerSize pointer_size = class_linker->GetImagePointerSize();
Andreas Gampe42ef8ab2015-12-03 17:27:32 -08003780
Mathieu Chartiere401d142015-04-22 13:56:20 -07003781 ArtMethod* res_method = dex_cache_->GetResolvedMethod(dex_method_idx, pointer_size);
Ian Rogers7b078e82014-09-10 14:44:24 -07003782 if (res_method == nullptr) {
Nicolas Geoffrayea179f42018-02-08 22:30:18 +00003783 res_method = class_linker->FindResolvedMethod(
3784 klass, dex_cache_.Get(), class_loader_.Get(), dex_method_idx);
Ian Rogersd81871c2011-10-03 13:57:23 -07003785 }
David Brazdilca3c8c32016-09-06 14:04:48 +01003786
Vladimir Markoba118822017-06-12 15:41:56 +01003787 // Record result of method resolution attempt. The klass resolution has recorded whether
3788 // the class is an interface or not and therefore the type of the lookup performed above.
3789 // TODO: Maybe we should not record dependency if the invoke type does not match the lookup type.
3790 VerifierDeps::MaybeRecordMethodResolution(*dex_file_, dex_method_idx, res_method);
3791
Alex Lightafb66472017-08-01 09:54:49 -07003792 bool must_fail = false;
3793 // This is traditional and helps with screwy bytecode. It will tell you that, yes, a method
3794 // exists, but that it's called incorrectly. This significantly helps debugging, as locally it's
3795 // hard to see the differences.
3796 // If we don't have res_method here we must fail. Just use this bool to make sure of that with a
3797 // DCHECK.
Vladimir Markoba118822017-06-12 15:41:56 +01003798 if (res_method == nullptr) {
Alex Lightafb66472017-08-01 09:54:49 -07003799 must_fail = true;
Vladimir Markoba118822017-06-12 15:41:56 +01003800 // Try to find the method also with the other type for better error reporting below
3801 // but do not store such bogus lookup result in the DexCache or VerifierDeps.
David Brazdil4525e0b2018-04-05 16:57:32 +01003802 res_method = class_linker->FindIncompatibleMethod(
3803 klass, dex_cache_.Get(), class_loader_.Get(), dex_method_idx);
Vladimir Markoba118822017-06-12 15:41:56 +01003804 }
David Brazdilca3c8c32016-09-06 14:04:48 +01003805
3806 if (res_method == nullptr) {
3807 Fail(VERIFY_ERROR_NO_METHOD) << "couldn't find method "
David Sehr709b0702016-10-13 09:12:37 -07003808 << klass->PrettyDescriptor() << "."
David Brazdilca3c8c32016-09-06 14:04:48 +01003809 << dex_file_->GetMethodName(method_id) << " "
3810 << dex_file_->GetMethodSignature(method_id);
3811 return nullptr;
3812 }
3813
Ian Rogersd81871c2011-10-03 13:57:23 -07003814 // Make sure calls to constructors are "direct". There are additional restrictions but we don't
3815 // enforce them here.
3816 if (res_method->IsConstructor() && method_type != METHOD_DIRECT) {
jeffhaod5347e02012-03-22 17:25:05 -07003817 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting non-direct call to constructor "
David Sehr709b0702016-10-13 09:12:37 -07003818 << res_method->PrettyMethod();
Ian Rogers7b078e82014-09-10 14:44:24 -07003819 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003820 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003821 // Disallow any calls to class initializers.
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003822 if (res_method->IsClassInitializer()) {
jeffhaod5347e02012-03-22 17:25:05 -07003823 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting call to class initializer "
David Sehr709b0702016-10-13 09:12:37 -07003824 << res_method->PrettyMethod();
Ian Rogers7b078e82014-09-10 14:44:24 -07003825 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003826 }
Andreas Gampe42ef8ab2015-12-03 17:27:32 -08003827
3828 // Check that interface methods are static or match interface classes.
3829 // We only allow statics if we don't have default methods enabled.
3830 //
3831 // Note: this check must be after the initializer check, as those are required to fail a class,
3832 // while this check implies an IncompatibleClassChangeError.
3833 if (klass->IsInterface()) {
Alex Lightb55f1ac2016-04-12 15:50:55 -07003834 // methods called on interfaces should be invoke-interface, invoke-super, invoke-direct (if
Mathieu Chartierf6e31472017-12-28 13:32:08 -08003835 // default methods are supported for the dex file), or invoke-static.
Andreas Gampe42ef8ab2015-12-03 17:27:32 -08003836 if (method_type != METHOD_INTERFACE &&
Neil Fuller9724c632016-01-07 15:42:47 +00003837 method_type != METHOD_STATIC &&
Mathieu Chartierf6e31472017-12-28 13:32:08 -08003838 (!dex_file_->SupportsDefaultMethods() ||
Alex Lightb55f1ac2016-04-12 15:50:55 -07003839 method_type != METHOD_DIRECT) &&
Neil Fuller9724c632016-01-07 15:42:47 +00003840 method_type != METHOD_SUPER) {
Andreas Gampe42ef8ab2015-12-03 17:27:32 -08003841 Fail(VERIFY_ERROR_CLASS_CHANGE)
David Sehr709b0702016-10-13 09:12:37 -07003842 << "non-interface method " << dex_file_->PrettyMethod(dex_method_idx)
3843 << " is in an interface class " << klass->PrettyClass();
Andreas Gampe42ef8ab2015-12-03 17:27:32 -08003844 return nullptr;
3845 }
3846 } else {
3847 if (method_type == METHOD_INTERFACE) {
3848 Fail(VERIFY_ERROR_CLASS_CHANGE)
David Sehr709b0702016-10-13 09:12:37 -07003849 << "interface method " << dex_file_->PrettyMethod(dex_method_idx)
3850 << " is in a non-interface class " << klass->PrettyClass();
Andreas Gampe42ef8ab2015-12-03 17:27:32 -08003851 return nullptr;
3852 }
3853 }
3854
Alex Lightafb66472017-08-01 09:54:49 -07003855 // Check specifically for non-public object methods being provided for interface dispatch. This
3856 // can occur if we failed to find a method with FindInterfaceMethod but later find one with
3857 // FindClassMethod for error message use.
3858 if (method_type == METHOD_INTERFACE &&
3859 res_method->GetDeclaringClass()->IsObjectClass() &&
3860 !res_method->IsPublic()) {
3861 Fail(VERIFY_ERROR_NO_METHOD) << "invoke-interface " << klass->PrettyDescriptor() << "."
3862 << dex_file_->GetMethodName(method_id) << " "
3863 << dex_file_->GetMethodSignature(method_id) << " resolved to "
3864 << "non-public object method " << res_method->PrettyMethod() << " "
3865 << "but non-public Object methods are excluded from interface "
3866 << "method resolution.";
3867 return nullptr;
3868 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003869 // Check if access is allowed.
Ian Rogersad0b3a32012-04-16 14:50:24 -07003870 if (!referrer.CanAccessMember(res_method->GetDeclaringClass(), res_method->GetAccessFlags())) {
David Sehr709b0702016-10-13 09:12:37 -07003871 Fail(VERIFY_ERROR_ACCESS_METHOD) << "illegal method access (call "
3872 << res_method->PrettyMethod()
Ian Rogersad0b3a32012-04-16 14:50:24 -07003873 << " from " << referrer << ")";
jeffhaob57e9522012-04-26 18:08:21 -07003874 return res_method;
jeffhao8cd6dda2012-02-22 10:15:34 -08003875 }
jeffhaode0d9c92012-02-27 13:58:13 -08003876 // Check that invoke-virtual and invoke-super are not used on private methods of the same class.
Alex Light7268d472016-01-20 15:50:01 -08003877 if (res_method->IsPrivate() && (method_type == METHOD_VIRTUAL || method_type == METHOD_SUPER)) {
jeffhaod5347e02012-03-22 17:25:05 -07003878 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invoke-super/virtual can't be used on private method "
David Sehr709b0702016-10-13 09:12:37 -07003879 << res_method->PrettyMethod();
Ian Rogers7b078e82014-09-10 14:44:24 -07003880 return nullptr;
jeffhaode0d9c92012-02-27 13:58:13 -08003881 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003882 // See if the method type implied by the invoke instruction matches the access flags for the
Orion Hodsoncfa325e2016-10-13 10:25:54 +01003883 // target method. The flags for METHOD_POLYMORPHIC are based on there being precisely two
3884 // signature polymorphic methods supported by the run-time which are native methods with variable
3885 // arguments.
Brian Carlstrombe6fa5e2014-12-09 20:15:42 -08003886 if ((method_type == METHOD_DIRECT && (!res_method->IsDirect() || res_method->IsStatic())) ||
Ian Rogersd81871c2011-10-03 13:57:23 -07003887 (method_type == METHOD_STATIC && !res_method->IsStatic()) ||
Alex Light7268d472016-01-20 15:50:01 -08003888 ((method_type == METHOD_SUPER ||
3889 method_type == METHOD_VIRTUAL ||
Orion Hodsoncfa325e2016-10-13 10:25:54 +01003890 method_type == METHOD_INTERFACE) && res_method->IsDirect()) ||
3891 ((method_type == METHOD_POLYMORPHIC) &&
3892 (!res_method->IsNative() || !res_method->IsVarargs()))) {
Ian Rogers2fc14272012-08-30 10:56:57 -07003893 Fail(VERIFY_ERROR_CLASS_CHANGE) << "invoke type (" << method_type << ") does not match method "
Orion Hodsoncfa325e2016-10-13 10:25:54 +01003894 "type of " << res_method->PrettyMethod();
Ian Rogers7b078e82014-09-10 14:44:24 -07003895 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003896 }
Alex Lightafb66472017-08-01 09:54:49 -07003897 // Make sure we weren't expecting to fail.
3898 DCHECK(!must_fail) << "invoke type (" << method_type << ")"
3899 << klass->PrettyDescriptor() << "."
3900 << dex_file_->GetMethodName(method_id) << " "
3901 << dex_file_->GetMethodSignature(method_id) << " unexpectedly resolved to "
3902 << res_method->PrettyMethod() << " without error. Initially this method was "
3903 << "not found so we were expecting to fail for some reason.";
jeffhao8cd6dda2012-02-22 10:15:34 -08003904 return res_method;
3905}
3906
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003907template <class T>
Mathieu Chartiere401d142015-04-22 13:56:20 -07003908ArtMethod* MethodVerifier::VerifyInvocationArgsFromIterator(
3909 T* it, const Instruction* inst, MethodType method_type, bool is_range, ArtMethod* res_method) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003910 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
3911 // match the call to the signature. Also, we might be calling through an abstract method
3912 // definition (which doesn't have register count values).
Orion Hodsoncfa325e2016-10-13 10:25:54 +01003913 const size_t expected_args = inst->VRegA();
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003914 /* caught by static verifier */
3915 DCHECK(is_range || expected_args <= 5);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003916
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08003917 if (expected_args > code_item_accessor_.OutsSize()) {
Orion Hodson1cda7c22017-08-10 13:06:45 +01003918 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08003919 << ") exceeds outsSize ("
3920 << code_item_accessor_.OutsSize() << ")";
Orion Hodson1cda7c22017-08-10 13:06:45 +01003921 return nullptr;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003922 }
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003923
3924 /*
3925 * Check the "this" argument, which must be an instance of the class that declared the method.
3926 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
3927 * rigorous check here (which is okay since we have to do it at runtime).
3928 */
3929 if (method_type != METHOD_STATIC) {
Orion Hodsoncfa325e2016-10-13 10:25:54 +01003930 const RegType& actual_arg_type = work_line_->GetInvocationThis(this, inst);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003931 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
3932 CHECK(have_pending_hard_failure_);
3933 return nullptr;
3934 }
David Brazdil68b5c0b2016-01-19 14:25:29 +00003935 bool is_init = false;
3936 if (actual_arg_type.IsUninitializedTypes()) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003937 if (res_method) {
3938 if (!res_method->IsConstructor()) {
3939 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
3940 return nullptr;
3941 }
3942 } else {
3943 // Check whether the name of the called method is "<init>"
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00003944 const uint32_t method_idx = GetMethodIdxOfInvoke(inst);
Jeff Hao0d087272014-08-04 14:47:17 -07003945 if (strcmp(dex_file_->GetMethodName(dex_file_->GetMethodId(method_idx)), "<init>") != 0) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003946 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
3947 return nullptr;
3948 }
3949 }
David Brazdil68b5c0b2016-01-19 14:25:29 +00003950 is_init = true;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003951 }
David Brazdil68b5c0b2016-01-19 14:25:29 +00003952 const RegType& adjusted_type = is_init
3953 ? GetRegTypeCache()->FromUninitialized(actual_arg_type)
3954 : actual_arg_type;
Andreas Gampeeed3a5d2017-11-29 14:58:34 -08003955 if (method_type != METHOD_INTERFACE && !adjusted_type.IsZeroOrNull()) {
Ian Rogersd8f69b02014-09-10 21:43:52 +00003956 const RegType* res_method_class;
Andreas Gamped9e23012015-06-04 22:19:58 -07003957 // Miranda methods have the declaring interface as their declaring class, not the abstract
3958 // class. It would be wrong to use this for the type check (interface type checks are
3959 // postponed to runtime).
3960 if (res_method != nullptr && !res_method->IsMiranda()) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003961 mirror::Class* klass = res_method->GetDeclaringClass();
Ian Rogers1ff3c982014-08-12 02:30:58 -07003962 std::string temp;
Andreas Gampef23f33d2015-06-23 14:18:17 -07003963 res_method_class = &FromClass(klass->GetDescriptor(&temp), klass,
3964 klass->CannotBeAssignedFromOtherTypes());
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003965 } else {
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00003966 const uint32_t method_idx = GetMethodIdxOfInvoke(inst);
Andreas Gampea5b09a62016-11-17 15:21:22 -08003967 const dex::TypeIndex class_idx = dex_file_->GetMethodId(method_idx).class_idx_;
Mathieu Chartierde40d472015-10-15 17:47:48 -07003968 res_method_class = &reg_types_.FromDescriptor(
3969 GetClassLoader(),
3970 dex_file_->StringByTypeIdx(class_idx),
3971 false);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003972 }
David Brazdilca3c8c32016-09-06 14:04:48 +01003973 if (!res_method_class->IsAssignableFrom(adjusted_type, this)) {
David Brazdil68b5c0b2016-01-19 14:25:29 +00003974 Fail(adjusted_type.IsUnresolvedTypes()
3975 ? VERIFY_ERROR_NO_CLASS
3976 : VERIFY_ERROR_BAD_CLASS_SOFT)
3977 << "'this' argument '" << actual_arg_type << "' not instance of '"
3978 << *res_method_class << "'";
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003979 // Continue on soft failures. We need to find possible hard failures to avoid problems in
3980 // the compiler.
3981 if (have_pending_hard_failure_) {
3982 return nullptr;
3983 }
3984 }
3985 }
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003986 }
3987
Orion Hodsoncfa325e2016-10-13 10:25:54 +01003988 uint32_t arg[5];
3989 if (!is_range) {
3990 inst->GetVarArgs(arg);
3991 }
3992 uint32_t sig_registers = (method_type == METHOD_STATIC) ? 0 : 1;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003993 for ( ; it->HasNext(); it->Next()) {
3994 if (sig_registers >= expected_args) {
3995 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, expected " << inst->VRegA() <<
Orion Hodsoncfa325e2016-10-13 10:25:54 +01003996 " argument registers, method signature has " << sig_registers + 1 << " or more";
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003997 return nullptr;
3998 }
3999
4000 const char* param_descriptor = it->GetDescriptor();
4001
4002 if (param_descriptor == nullptr) {
4003 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation because of missing signature "
4004 "component";
4005 return nullptr;
4006 }
4007
Ian Rogersd8f69b02014-09-10 21:43:52 +00004008 const RegType& reg_type = reg_types_.FromDescriptor(GetClassLoader(), param_descriptor, false);
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004009 uint32_t get_reg = is_range ? inst->VRegC() + static_cast<uint32_t>(sig_registers) :
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004010 arg[sig_registers];
4011 if (reg_type.IsIntegralTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004012 const RegType& src_type = work_line_->GetRegisterType(this, get_reg);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004013 if (!src_type.IsIntegralTypes()) {
4014 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register v" << get_reg << " has type " << src_type
4015 << " but expected " << reg_type;
Andreas Gampeb588f4c2015-05-26 13:35:39 -07004016 return nullptr;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004017 }
Andreas Gampeda9badb2015-06-05 20:22:12 -07004018 } else {
4019 if (!work_line_->VerifyRegisterType(this, get_reg, reg_type)) {
4020 // Continue on soft failures. We need to find possible hard failures to avoid problems in
4021 // the compiler.
4022 if (have_pending_hard_failure_) {
4023 return nullptr;
4024 }
4025 } else if (reg_type.IsLongOrDoubleTypes()) {
4026 // Check that registers are consecutive (for non-range invokes). Invokes are the only
4027 // instructions not specifying register pairs by the first component, but require them
4028 // nonetheless. Only check when there's an actual register in the parameters. If there's
4029 // none, this will fail below.
4030 if (!is_range && sig_registers + 1 < expected_args) {
4031 uint32_t second_reg = arg[sig_registers + 1];
4032 if (second_reg != get_reg + 1) {
4033 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, long or double parameter "
4034 "at index " << sig_registers << " is not a pair: " << get_reg << " + "
4035 << second_reg << ".";
4036 return nullptr;
4037 }
4038 }
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004039 }
4040 }
4041 sig_registers += reg_type.IsLongOrDoubleTypes() ? 2 : 1;
4042 }
4043 if (expected_args != sig_registers) {
4044 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, expected " << expected_args <<
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004045 " argument registers, method signature has " << sig_registers;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004046 return nullptr;
4047 }
4048 return res_method;
4049}
4050
4051void MethodVerifier::VerifyInvocationArgsUnresolvedMethod(const Instruction* inst,
4052 MethodType method_type,
4053 bool is_range) {
4054 // As the method may not have been resolved, make this static check against what we expect.
4055 // The main reason for this code block is to fail hard when we find an illegal use, e.g.,
4056 // wrong number of arguments or wrong primitive types, even if the method could not be resolved.
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00004057 const uint32_t method_idx = GetMethodIdxOfInvoke(inst);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004058 DexFileParameterIterator it(*dex_file_,
4059 dex_file_->GetProtoId(dex_file_->GetMethodId(method_idx).proto_idx_));
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004060 VerifyInvocationArgsFromIterator(&it, inst, method_type, is_range, nullptr);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004061}
4062
Orion Hodsonc069a302017-01-18 09:23:12 +00004063bool MethodVerifier::CheckCallSite(uint32_t call_site_idx) {
Orion Hodson3a842f52017-04-21 15:24:10 +01004064 if (call_site_idx >= dex_file_->NumCallSiteIds()) {
4065 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Bad call site id #" << call_site_idx
4066 << " >= " << dex_file_->NumCallSiteIds();
4067 return false;
4068 }
4069
Orion Hodsonc069a302017-01-18 09:23:12 +00004070 CallSiteArrayValueIterator it(*dex_file_, dex_file_->GetCallSiteId(call_site_idx));
4071 // Check essential arguments are provided. The dex file verifier has verified indicies of the
4072 // main values (method handle, name, method_type).
4073 if (it.Size() < 3) {
4074 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Call site #" << call_site_idx
4075 << " has too few arguments: "
Orion Hodsona5dca522018-02-27 12:42:11 +00004076 << it.Size() << " < 3";
Orion Hodsonc069a302017-01-18 09:23:12 +00004077 return false;
4078 }
4079
Orion Hodson3a842f52017-04-21 15:24:10 +01004080 // Get and check the first argument: the method handle (index range
4081 // checked by the dex file verifier).
Orion Hodsonc069a302017-01-18 09:23:12 +00004082 uint32_t method_handle_idx = static_cast<uint32_t>(it.GetJavaValue().i);
Orion Hodsona5dca522018-02-27 12:42:11 +00004083 if (method_handle_idx > dex_file_->NumMethodHandles()) {
4084 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Call site id #" << call_site_idx
4085 << " method handle index invalid " << method_handle_idx
4086 << " >= " << dex_file_->NumMethodHandles();
4087 return false;
4088 }
Orion Hodson3a842f52017-04-21 15:24:10 +01004089
Orion Hodsonc069a302017-01-18 09:23:12 +00004090 const DexFile::MethodHandleItem& mh = dex_file_->GetMethodHandle(method_handle_idx);
4091 if (mh.method_handle_type_ != static_cast<uint16_t>(DexFile::MethodHandleType::kInvokeStatic)) {
4092 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Call site #" << call_site_idx
Orion Hodson631827d2017-04-10 14:53:47 +01004093 << " argument 0 method handle type is not InvokeStatic: "
4094 << mh.method_handle_type_;
Orion Hodsonc069a302017-01-18 09:23:12 +00004095 return false;
4096 }
Orion Hodsonc069a302017-01-18 09:23:12 +00004097 return true;
4098}
4099
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004100class MethodParamListDescriptorIterator {
4101 public:
Mathieu Chartiere401d142015-04-22 13:56:20 -07004102 explicit MethodParamListDescriptorIterator(ArtMethod* res_method) :
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004103 res_method_(res_method), pos_(0), params_(res_method->GetParameterTypeList()),
4104 params_size_(params_ == nullptr ? 0 : params_->Size()) {
4105 }
4106
4107 bool HasNext() {
4108 return pos_ < params_size_;
4109 }
4110
4111 void Next() {
4112 ++pos_;
4113 }
4114
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07004115 const char* GetDescriptor() REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004116 return res_method_->GetTypeDescriptorFromTypeIdx(params_->GetTypeItem(pos_).type_idx_);
4117 }
4118
4119 private:
Mathieu Chartiere401d142015-04-22 13:56:20 -07004120 ArtMethod* res_method_;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004121 size_t pos_;
4122 const DexFile::TypeList* params_;
4123 const size_t params_size_;
4124};
4125
Mathieu Chartiere401d142015-04-22 13:56:20 -07004126ArtMethod* MethodVerifier::VerifyInvocationArgs(
Alex Light7268d472016-01-20 15:50:01 -08004127 const Instruction* inst, MethodType method_type, bool is_range) {
jeffhao8cd6dda2012-02-22 10:15:34 -08004128 // Resolve the method. This could be an abstract or concrete method depending on what sort of call
4129 // we're making.
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00004130 const uint32_t method_idx = GetMethodIdxOfInvoke(inst);
Alex Light7268d472016-01-20 15:50:01 -08004131 ArtMethod* res_method = ResolveMethodAndCheckAccess(method_idx, method_type);
Ian Rogers7b078e82014-09-10 14:44:24 -07004132 if (res_method == nullptr) { // error or class is unresolved
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004133 // Check what we can statically.
4134 if (!have_pending_hard_failure_) {
4135 VerifyInvocationArgsUnresolvedMethod(inst, method_type, is_range);
4136 }
4137 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08004138 }
4139
Ian Rogersd81871c2011-10-03 13:57:23 -07004140 // If we're using invoke-super(method), make sure that the executing method's class' superclass
Alex Light705ad492015-09-21 11:36:30 -07004141 // has a vtable entry for the target method. Or the target is on a interface.
Alex Light7268d472016-01-20 15:50:01 -08004142 if (method_type == METHOD_SUPER) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08004143 dex::TypeIndex class_idx = dex_file_->GetMethodId(method_idx).class_idx_;
David Brazdilca3c8c32016-09-06 14:04:48 +01004144 const RegType& reference_type = reg_types_.FromDescriptor(
4145 GetClassLoader(),
4146 dex_file_->StringByTypeIdx(class_idx),
4147 false);
4148 if (reference_type.IsUnresolvedTypes()) {
Alex Lightfedd91d2016-01-07 14:49:16 -08004149 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "Unable to find referenced class from invoke-super";
4150 return nullptr;
4151 }
David Brazdilca3c8c32016-09-06 14:04:48 +01004152 if (reference_type.GetClass()->IsInterface()) {
Alex Lightfedd91d2016-01-07 14:49:16 -08004153 // TODO Can we verify anything else.
David Brazdil15fc7292016-09-02 14:13:18 +01004154 if (class_idx == class_def_.class_idx_) {
Alex Lightfedd91d2016-01-07 14:49:16 -08004155 Fail(VERIFY_ERROR_CLASS_CHANGE) << "Cannot invoke-super on self as interface";
Alex Light55ea94d2016-03-15 09:50:26 -07004156 return nullptr;
Alex Lightfedd91d2016-01-07 14:49:16 -08004157 }
4158 // TODO Revisit whether we want to allow invoke-super on direct interfaces only like the JLS
4159 // does.
Alex Light55ea94d2016-03-15 09:50:26 -07004160 if (!GetDeclaringClass().HasClass()) {
4161 Fail(VERIFY_ERROR_NO_CLASS) << "Unable to resolve the full class of 'this' used in an"
4162 << "interface invoke-super";
4163 return nullptr;
David Brazdilca3c8c32016-09-06 14:04:48 +01004164 } else if (!reference_type.IsStrictlyAssignableFrom(GetDeclaringClass(), this)) {
Alex Lightfedd91d2016-01-07 14:49:16 -08004165 Fail(VERIFY_ERROR_CLASS_CHANGE)
David Sehr709b0702016-10-13 09:12:37 -07004166 << "invoke-super in " << mirror::Class::PrettyClass(GetDeclaringClass().GetClass())
4167 << " in method "
4168 << dex_file_->PrettyMethod(dex_method_idx_) << " to method "
4169 << dex_file_->PrettyMethod(method_idx) << " references "
4170 << "non-super-interface type " << mirror::Class::PrettyClass(reference_type.GetClass());
Alex Lightfedd91d2016-01-07 14:49:16 -08004171 return nullptr;
Alex Light705ad492015-09-21 11:36:30 -07004172 }
4173 } else {
4174 const RegType& super = GetDeclaringClass().GetSuperClass(&reg_types_);
4175 if (super.IsUnresolvedTypes()) {
4176 Fail(VERIFY_ERROR_NO_METHOD) << "unknown super class in invoke-super from "
David Sehr709b0702016-10-13 09:12:37 -07004177 << dex_file_->PrettyMethod(dex_method_idx_)
4178 << " to super " << res_method->PrettyMethod();
Alex Light705ad492015-09-21 11:36:30 -07004179 return nullptr;
4180 }
David Brazdilca3c8c32016-09-06 14:04:48 +01004181 if (!reference_type.IsStrictlyAssignableFrom(GetDeclaringClass(), this) ||
Aart Bikf663e342016-04-04 17:28:59 -07004182 (res_method->GetMethodIndex() >= super.GetClass()->GetVTableLength())) {
Alex Light705ad492015-09-21 11:36:30 -07004183 Fail(VERIFY_ERROR_NO_METHOD) << "invalid invoke-super from "
David Sehr709b0702016-10-13 09:12:37 -07004184 << dex_file_->PrettyMethod(dex_method_idx_)
Alex Light705ad492015-09-21 11:36:30 -07004185 << " to super " << super
4186 << "." << res_method->GetName()
4187 << res_method->GetSignature();
4188 return nullptr;
4189 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004190 }
4191 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08004192
Andreas Gampe74979b12017-05-16 09:28:06 -07004193 if (UNLIKELY(method_type == METHOD_POLYMORPHIC)) {
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004194 // Process the signature of the calling site that is invoking the method handle.
Orion Hodson06d10a72018-05-14 08:53:38 +01004195 dex::ProtoIndex proto_idx(inst->VRegH());
4196 DexFileParameterIterator it(*dex_file_, dex_file_->GetProtoId(proto_idx));
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004197 return VerifyInvocationArgsFromIterator(&it, inst, method_type, is_range, res_method);
4198 } else {
4199 // Process the target method's signature.
4200 MethodParamListDescriptorIterator it(res_method);
4201 return VerifyInvocationArgsFromIterator(&it, inst, method_type, is_range, res_method);
4202 }
4203}
4204
4205bool MethodVerifier::CheckSignaturePolymorphicMethod(ArtMethod* method) {
4206 mirror::Class* klass = method->GetDeclaringClass();
Orion Hodsonfe92d122018-01-02 10:45:17 +00004207 const char* method_name = method->GetName();
4208
4209 const char* expected_return_descriptor;
Vladimir Markoc7aa87e2018-05-24 15:19:52 +01004210 ObjPtr<mirror::ObjectArray<mirror::Class>> class_roots =
4211 Runtime::Current()->GetClassLinker()->GetClassRoots();
4212 if (klass == GetClassRoot<mirror::MethodHandle>(class_roots)) {
Orion Hodsonfe92d122018-01-02 10:45:17 +00004213 expected_return_descriptor = mirror::MethodHandle::GetReturnTypeDescriptor(method_name);
Vladimir Markoc7aa87e2018-05-24 15:19:52 +01004214 } else if (klass == GetClassRoot<mirror::VarHandle>(class_roots)) {
Orion Hodsonfe92d122018-01-02 10:45:17 +00004215 expected_return_descriptor = mirror::VarHandle::GetReturnTypeDescriptor(method_name);
4216 } else {
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004217 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
Orion Hodsonfe92d122018-01-02 10:45:17 +00004218 << "Signature polymorphic method in unsuppported class: " << klass->PrettyDescriptor();
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004219 return false;
4220 }
4221
Orion Hodsonfe92d122018-01-02 10:45:17 +00004222 if (expected_return_descriptor == nullptr) {
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004223 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
4224 << "Signature polymorphic method name invalid: " << method_name;
4225 return false;
4226 }
4227
4228 const DexFile::TypeList* types = method->GetParameterTypeList();
4229 if (types->Size() != 1) {
4230 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
4231 << "Signature polymorphic method has too many arguments " << types->Size() << " != 1";
4232 return false;
4233 }
4234
4235 const dex::TypeIndex argument_type_index = types->GetTypeItem(0).type_idx_;
4236 const char* argument_descriptor = method->GetTypeDescriptorFromTypeIdx(argument_type_index);
4237 if (strcmp(argument_descriptor, "[Ljava/lang/Object;") != 0) {
4238 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
4239 << "Signature polymorphic method has unexpected argument type: " << argument_descriptor;
4240 return false;
4241 }
4242
4243 const char* return_descriptor = method->GetReturnTypeDescriptor();
Orion Hodsonfe92d122018-01-02 10:45:17 +00004244 if (strcmp(return_descriptor, expected_return_descriptor) != 0) {
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004245 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
Orion Hodsonfe92d122018-01-02 10:45:17 +00004246 << "Signature polymorphic method has unexpected return type: " << return_descriptor
4247 << " != " << expected_return_descriptor;
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004248 return false;
4249 }
4250
4251 return true;
4252}
4253
4254bool MethodVerifier::CheckSignaturePolymorphicReceiver(const Instruction* inst) {
4255 const RegType& this_type = work_line_->GetInvocationThis(this, inst);
Andreas Gampeeed3a5d2017-11-29 14:58:34 -08004256 if (this_type.IsZeroOrNull()) {
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004257 /* null pointer always passes (and always fails at run time) */
4258 return true;
4259 } else if (!this_type.IsNonZeroReferenceTypes()) {
4260 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
4261 << "invoke-polymorphic receiver is not a reference: "
4262 << this_type;
4263 return false;
4264 } else if (this_type.IsUninitializedReference()) {
4265 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
4266 << "invoke-polymorphic receiver is uninitialized: "
4267 << this_type;
4268 return false;
4269 } else if (!this_type.HasClass()) {
4270 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
4271 << "invoke-polymorphic receiver has no class: "
4272 << this_type;
4273 return false;
Vladimir Markoc7aa87e2018-05-24 15:19:52 +01004274 } else {
4275 ObjPtr<mirror::ObjectArray<mirror::Class>> class_roots =
4276 Runtime::Current()->GetClassLinker()->GetClassRoots();
4277 if (!this_type.GetClass()->IsSubClass(GetClassRoot<mirror::MethodHandle>(class_roots)) &&
4278 !this_type.GetClass()->IsSubClass(GetClassRoot<mirror::VarHandle>(class_roots))) {
4279 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
4280 << "invoke-polymorphic receiver is not a subclass of MethodHandle or VarHandle: "
4281 << this_type;
4282 return false;
4283 }
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004284 }
4285 return true;
Ian Rogersd81871c2011-10-03 13:57:23 -07004286}
4287
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00004288uint16_t MethodVerifier::GetMethodIdxOfInvoke(const Instruction* inst) {
4289 switch (inst->Opcode()) {
4290 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK:
4291 case Instruction::INVOKE_VIRTUAL_QUICK: {
4292 DCHECK(Runtime::Current()->IsStarted() || verify_to_dump_)
4293 << dex_file_->PrettyMethod(dex_method_idx_, true) << "@" << work_insn_idx_;
4294 DCHECK(method_being_verified_ != nullptr);
4295 uint16_t method_idx = method_being_verified_->GetIndexFromQuickening(work_insn_idx_);
4296 CHECK_NE(method_idx, DexFile::kDexNoIndex16);
4297 return method_idx;
4298 }
4299 default: {
4300 return inst->VRegB();
4301 }
Mathieu Chartier091d2382015-03-06 10:59:06 -08004302 }
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004303}
4304
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00004305uint16_t MethodVerifier::GetFieldIdxOfFieldAccess(const Instruction* inst, bool is_static) {
4306 if (is_static) {
4307 return inst->VRegB_21c();
4308 } else if (inst->IsQuickened()) {
4309 DCHECK(Runtime::Current()->IsStarted() || verify_to_dump_);
4310 DCHECK(method_being_verified_ != nullptr);
4311 uint16_t field_idx = method_being_verified_->GetIndexFromQuickening(work_insn_idx_);
4312 CHECK_NE(field_idx, DexFile::kDexNoIndex16);
4313 return field_idx;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004314 } else {
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00004315 return inst->VRegC_22c();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004316 }
4317}
4318
Ian Rogers62342ec2013-06-11 10:26:37 -07004319void MethodVerifier::VerifyNewArray(const Instruction* inst, bool is_filled, bool is_range) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08004320 dex::TypeIndex type_idx;
Sebastien Hertz5243e912013-05-21 10:55:07 +02004321 if (!is_filled) {
4322 DCHECK_EQ(inst->Opcode(), Instruction::NEW_ARRAY);
Andreas Gampea5b09a62016-11-17 15:21:22 -08004323 type_idx = dex::TypeIndex(inst->VRegC_22c());
Sebastien Hertz5243e912013-05-21 10:55:07 +02004324 } else if (!is_range) {
4325 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY);
Andreas Gampea5b09a62016-11-17 15:21:22 -08004326 type_idx = dex::TypeIndex(inst->VRegB_35c());
Sebastien Hertz5243e912013-05-21 10:55:07 +02004327 } else {
4328 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY_RANGE);
Andreas Gampea5b09a62016-11-17 15:21:22 -08004329 type_idx = dex::TypeIndex(inst->VRegB_3rc());
Sebastien Hertz5243e912013-05-21 10:55:07 +02004330 }
Andreas Gampe98be1a92017-08-28 08:25:45 -07004331 const RegType& res_type = ResolveClass<CheckAccess::kYes>(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004332 if (res_type.IsConflict()) { // bad class
4333 DCHECK_NE(failures_.size(), 0U);
Ian Rogers0c4a5062012-02-03 15:18:59 -08004334 } else {
4335 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
4336 if (!res_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07004337 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "new-array on non-array class " << res_type;
Ian Rogers0c4a5062012-02-03 15:18:59 -08004338 } else if (!is_filled) {
4339 /* make sure "size" register is valid type */
Ian Rogers7b078e82014-09-10 14:44:24 -07004340 work_line_->VerifyRegisterType(this, inst->VRegB_22c(), reg_types_.Integer());
Ian Rogers0c4a5062012-02-03 15:18:59 -08004341 /* set register type to array class */
Ian Rogersd8f69b02014-09-10 21:43:52 +00004342 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
Andreas Gampead238ce2015-08-24 21:13:08 -07004343 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_22c(), precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08004344 } else {
Andreas Gampebb18a032016-03-22 20:34:25 -07004345 DCHECK(!res_type.IsUnresolvedMergedReference());
Ian Rogers0c4a5062012-02-03 15:18:59 -08004346 // Verify each register. If "arg_count" is bad, VerifyRegisterType() will run off the end of
4347 // the list and fail. It's legal, if silly, for arg_count to be zero.
Ian Rogersd8f69b02014-09-10 21:43:52 +00004348 const RegType& expected_type = reg_types_.GetComponentType(res_type, GetClassLoader());
Sebastien Hertz5243e912013-05-21 10:55:07 +02004349 uint32_t arg_count = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
4350 uint32_t arg[5];
4351 if (!is_range) {
Ian Rogers29a26482014-05-02 15:27:29 -07004352 inst->GetVarArgs(arg);
Sebastien Hertz5243e912013-05-21 10:55:07 +02004353 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08004354 for (size_t ui = 0; ui < arg_count; ui++) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02004355 uint32_t get_reg = is_range ? inst->VRegC_3rc() + ui : arg[ui];
Ian Rogers7b078e82014-09-10 14:44:24 -07004356 if (!work_line_->VerifyRegisterType(this, get_reg, expected_type)) {
4357 work_line_->SetResultRegisterType(this, reg_types_.Conflict());
Ian Rogers0c4a5062012-02-03 15:18:59 -08004358 return;
4359 }
4360 }
4361 // filled-array result goes into "result" register
Ian Rogersd8f69b02014-09-10 21:43:52 +00004362 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
Ian Rogers7b078e82014-09-10 14:44:24 -07004363 work_line_->SetResultRegisterType(this, precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08004364 }
4365 }
4366}
4367
Sebastien Hertz5243e912013-05-21 10:55:07 +02004368void MethodVerifier::VerifyAGet(const Instruction* inst,
Ian Rogersd8f69b02014-09-10 21:43:52 +00004369 const RegType& insn_type, bool is_primitive) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004370 const RegType& index_type = work_line_->GetRegisterType(this, inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07004371 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07004372 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07004373 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004374 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegB_23x());
Andreas Gampeeed3a5d2017-11-29 14:58:34 -08004375 if (array_type.IsZeroOrNull()) {
Ian Rogers89310de2012-02-01 13:47:30 -08004376 // Null array class; this code path will fail at runtime. Infer a merge-able type from the
Andreas Gampe52f205a2017-12-01 12:16:07 -08004377 // instruction type.
4378 if (!is_primitive) {
4379 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), reg_types_.Null());
4380 } else if (insn_type.IsInteger()) {
4381 // Pick a non-zero constant (to distinguish with null) that can fit in any primitive.
4382 // We cannot use 'insn_type' as it could be a float array or an int array.
4383 work_line_->SetRegisterType<LockOp::kClear>(
4384 this, inst->VRegA_23x(), DetermineCat1Constant(1, need_precise_constants_));
4385 } else if (insn_type.IsCategory1Types()) {
4386 // Category 1
4387 // The 'insn_type' is exactly the type we need.
4388 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), insn_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07004389 } else {
Ian Rogers89310de2012-02-01 13:47:30 -08004390 // Category 2
Ian Rogers7b078e82014-09-10 14:44:24 -07004391 work_line_->SetRegisterTypeWide(this, inst->VRegA_23x(),
4392 reg_types_.FromCat2ConstLo(0, false),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004393 reg_types_.FromCat2ConstHi(0, false));
Ian Rogers89310de2012-02-01 13:47:30 -08004394 }
jeffhaofc3144e2012-02-01 17:21:15 -08004395 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07004396 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aget";
Andreas Gampe8d8fc482016-03-25 16:24:20 -07004397 } else if (array_type.IsUnresolvedMergedReference()) {
Andreas Gampebb18a032016-03-22 20:34:25 -07004398 // Unresolved array types must be reference array types.
4399 if (is_primitive) {
4400 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type
4401 << " source for category 1 aget";
4402 } else {
4403 Fail(VERIFY_ERROR_NO_CLASS) << "cannot verify aget for " << array_type
4404 << " because of missing class";
Andreas Gampe8d8fc482016-03-25 16:24:20 -07004405 // Approximate with java.lang.Object[].
4406 work_line_->SetRegisterType<LockOp::kClear>(this,
4407 inst->VRegA_23x(),
4408 reg_types_.JavaLangObject(false));
Andreas Gampebb18a032016-03-22 20:34:25 -07004409 }
Ian Rogers89310de2012-02-01 13:47:30 -08004410 } else {
4411 /* verify the class */
Ian Rogersd8f69b02014-09-10 21:43:52 +00004412 const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
jeffhaofc3144e2012-02-01 17:21:15 -08004413 if (!component_type.IsReferenceTypes() && !is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07004414 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08004415 << " source for aget-object";
4416 } else if (component_type.IsNonZeroReferenceTypes() && is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07004417 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08004418 << " source for category 1 aget";
4419 } else if (is_primitive && !insn_type.Equals(component_type) &&
4420 !((insn_type.IsInteger() && component_type.IsFloat()) ||
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004421 (insn_type.IsLong() && component_type.IsDouble()))) {
4422 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array type " << array_type
4423 << " incompatible with aget of type " << insn_type;
Ian Rogers89310de2012-02-01 13:47:30 -08004424 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004425 // Use knowledge of the field type which is stronger than the type inferred from the
4426 // instruction, which can't differentiate object types and ints from floats, longs from
4427 // doubles.
4428 if (!component_type.IsLowHalf()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07004429 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), component_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004430 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004431 work_line_->SetRegisterTypeWide(this, inst->VRegA_23x(), component_type,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004432 component_type.HighHalf(&reg_types_));
4433 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004434 }
4435 }
4436 }
4437}
4438
Ian Rogersd8f69b02014-09-10 21:43:52 +00004439void MethodVerifier::VerifyPrimitivePut(const RegType& target_type, const RegType& insn_type,
Jeff Haofe1f7c82013-08-01 14:50:24 -07004440 const uint32_t vregA) {
4441 // Primitive assignability rules are weaker than regular assignability rules.
4442 bool instruction_compatible;
4443 bool value_compatible;
Ian Rogers7b078e82014-09-10 14:44:24 -07004444 const RegType& value_type = work_line_->GetRegisterType(this, vregA);
Jeff Haofe1f7c82013-08-01 14:50:24 -07004445 if (target_type.IsIntegralTypes()) {
Jeff Haoa4647482013-08-06 15:35:47 -07004446 instruction_compatible = target_type.Equals(insn_type);
Jeff Haofe1f7c82013-08-01 14:50:24 -07004447 value_compatible = value_type.IsIntegralTypes();
4448 } else if (target_type.IsFloat()) {
4449 instruction_compatible = insn_type.IsInteger(); // no put-float, so expect put-int
4450 value_compatible = value_type.IsFloatTypes();
4451 } else if (target_type.IsLong()) {
4452 instruction_compatible = insn_type.IsLong();
Andreas Gampe376fa682014-09-07 13:06:12 -07004453 // Additional register check: this is not checked statically (as part of VerifyInstructions),
4454 // as target_type depends on the resolved type of the field.
4455 if (instruction_compatible && work_line_->NumRegs() > vregA + 1) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004456 const RegType& value_type_hi = work_line_->GetRegisterType(this, vregA + 1);
Andreas Gampe376fa682014-09-07 13:06:12 -07004457 value_compatible = value_type.IsLongTypes() && value_type.CheckWidePair(value_type_hi);
4458 } else {
4459 value_compatible = false;
4460 }
Jeff Haofe1f7c82013-08-01 14:50:24 -07004461 } else if (target_type.IsDouble()) {
4462 instruction_compatible = insn_type.IsLong(); // no put-double, so expect put-long
Andreas Gampe376fa682014-09-07 13:06:12 -07004463 // Additional register check: this is not checked statically (as part of VerifyInstructions),
4464 // as target_type depends on the resolved type of the field.
4465 if (instruction_compatible && work_line_->NumRegs() > vregA + 1) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004466 const RegType& value_type_hi = work_line_->GetRegisterType(this, vregA + 1);
Andreas Gampe376fa682014-09-07 13:06:12 -07004467 value_compatible = value_type.IsDoubleTypes() && value_type.CheckWidePair(value_type_hi);
4468 } else {
4469 value_compatible = false;
4470 }
Jeff Haofe1f7c82013-08-01 14:50:24 -07004471 } else {
4472 instruction_compatible = false; // reference with primitive store
4473 value_compatible = false; // unused
4474 }
4475 if (!instruction_compatible) {
4476 // This is a global failure rather than a class change failure as the instructions and
4477 // the descriptors for the type should have been consistent within the same file at
4478 // compile time.
4479 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "put insn has type '" << insn_type
4480 << "' but expected type '" << target_type << "'";
4481 return;
4482 }
4483 if (!value_compatible) {
4484 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
4485 << " of type " << value_type << " but expected " << target_type << " for put";
4486 return;
4487 }
4488}
4489
Sebastien Hertz5243e912013-05-21 10:55:07 +02004490void MethodVerifier::VerifyAPut(const Instruction* inst,
Ian Rogersd8f69b02014-09-10 21:43:52 +00004491 const RegType& insn_type, bool is_primitive) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004492 const RegType& index_type = work_line_->GetRegisterType(this, inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07004493 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07004494 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07004495 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004496 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegB_23x());
Andreas Gampeeed3a5d2017-11-29 14:58:34 -08004497 if (array_type.IsZeroOrNull()) {
Nicolas Geoffray66389fb2015-06-19 10:35:42 +01004498 // Null array type; this code path will fail at runtime.
4499 // Still check that the given value matches the instruction's type.
Andreas Gampe4bf4c782015-08-14 14:07:43 -07004500 // Note: this is, as usual, complicated by the fact the the instruction isn't fully typed
4501 // and fits multiple register types.
4502 const RegType* modified_reg_type = &insn_type;
4503 if ((modified_reg_type == &reg_types_.Integer()) ||
4504 (modified_reg_type == &reg_types_.LongLo())) {
4505 // May be integer or float | long or double. Overwrite insn_type accordingly.
4506 const RegType& value_type = work_line_->GetRegisterType(this, inst->VRegA_23x());
4507 if (modified_reg_type == &reg_types_.Integer()) {
4508 if (&value_type == &reg_types_.Float()) {
4509 modified_reg_type = &value_type;
4510 }
4511 } else {
4512 if (&value_type == &reg_types_.DoubleLo()) {
4513 modified_reg_type = &value_type;
4514 }
4515 }
4516 }
4517 work_line_->VerifyRegisterType(this, inst->VRegA_23x(), *modified_reg_type);
jeffhaofc3144e2012-02-01 17:21:15 -08004518 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07004519 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aput";
Andreas Gampe8d8fc482016-03-25 16:24:20 -07004520 } else if (array_type.IsUnresolvedMergedReference()) {
Andreas Gampebb18a032016-03-22 20:34:25 -07004521 // Unresolved array types must be reference array types.
4522 if (is_primitive) {
4523 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "put insn has type '" << insn_type
4524 << "' but unresolved type '" << array_type << "'";
4525 } else {
4526 Fail(VERIFY_ERROR_NO_CLASS) << "cannot verify aput for " << array_type
4527 << " because of missing class";
4528 }
Ian Rogers89310de2012-02-01 13:47:30 -08004529 } else {
Ian Rogersd8f69b02014-09-10 21:43:52 +00004530 const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
Jeff Haofe1f7c82013-08-01 14:50:24 -07004531 const uint32_t vregA = inst->VRegA_23x();
Jeff Haob24b4a72013-07-31 13:47:31 -07004532 if (is_primitive) {
Jeff Haofe1f7c82013-08-01 14:50:24 -07004533 VerifyPrimitivePut(component_type, insn_type, vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07004534 } else {
Jeff Haob24b4a72013-07-31 13:47:31 -07004535 if (!component_type.IsReferenceTypes()) {
4536 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
4537 << " source for aput-object";
4538 } else {
4539 // The instruction agrees with the type of array, confirm the value to be stored does too
4540 // Note: we use the instruction type (rather than the component type) for aput-object as
4541 // incompatible classes will be caught at runtime as an array store exception
Ian Rogers7b078e82014-09-10 14:44:24 -07004542 work_line_->VerifyRegisterType(this, vregA, insn_type);
Jeff Haob24b4a72013-07-31 13:47:31 -07004543 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004544 }
4545 }
4546 }
4547}
4548
Mathieu Chartierc7853442015-03-27 14:35:38 -07004549ArtField* MethodVerifier::GetStaticField(int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08004550 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
4551 // Check access to class
Andreas Gampe98be1a92017-08-28 08:25:45 -07004552 const RegType& klass_type = ResolveClass<CheckAccess::kYes>(field_id.class_idx_);
Brian Carlstrom7934ac22013-07-26 10:54:15 -07004553 if (klass_type.IsConflict()) { // bad class
Ian Rogersad0b3a32012-04-16 14:50:24 -07004554 AppendToLastFailMessage(StringPrintf(" in attempt to access static field %d (%s) in %s",
4555 field_idx, dex_file_->GetFieldName(field_id),
4556 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers7b078e82014-09-10 14:44:24 -07004557 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08004558 }
Elliott Hughesb25c3f62012-03-26 16:35:06 -07004559 if (klass_type.IsUnresolvedTypes()) {
Andreas Gampe629be512017-08-25 17:09:32 -07004560 // Accessibility checks depend on resolved fields.
4561 DCHECK(klass_type.Equals(GetDeclaringClass()) || !failures_.empty());
4562
Ian Rogers7b078e82014-09-10 14:44:24 -07004563 return nullptr; // Can't resolve Class so no more to do here, will do checking at runtime.
Ian Rogers90040192011-12-16 08:54:29 -08004564 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07004565 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Vladimir Markoe11dd502017-12-08 14:09:45 +00004566 ArtField* field = class_linker->ResolveFieldJLS(field_idx, dex_cache_, class_loader_);
David Brazdilca3c8c32016-09-06 14:04:48 +01004567
4568 // Record result of the field resolution attempt.
4569 VerifierDeps::MaybeRecordFieldResolution(*dex_file_, field_idx, field);
4570
Ian Rogers7b078e82014-09-10 14:44:24 -07004571 if (field == nullptr) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07004572 VLOG(verifier) << "Unable to resolve static field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07004573 << dex_file_->GetFieldName(field_id) << ") in "
4574 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogers7b078e82014-09-10 14:44:24 -07004575 DCHECK(self_->IsExceptionPending());
4576 self_->ClearException();
4577 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004578 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
4579 field->GetAccessFlags())) {
David Sehr709b0702016-10-13 09:12:37 -07004580 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access static field " << field->PrettyField()
Ian Rogersad0b3a32012-04-16 14:50:24 -07004581 << " from " << GetDeclaringClass();
Ian Rogers7b078e82014-09-10 14:44:24 -07004582 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07004583 } else if (!field->IsStatic()) {
David Sehr709b0702016-10-13 09:12:37 -07004584 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << field->PrettyField() << " to be static";
Ian Rogers7b078e82014-09-10 14:44:24 -07004585 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07004586 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07004587 return field;
Ian Rogersd81871c2011-10-03 13:57:23 -07004588}
4589
Mathieu Chartierc7853442015-03-27 14:35:38 -07004590ArtField* MethodVerifier::GetInstanceField(const RegType& obj_type, int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08004591 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
Aart Bik31883642016-06-06 15:02:44 -07004592 // Check access to class.
Andreas Gampe98be1a92017-08-28 08:25:45 -07004593 const RegType& klass_type = ResolveClass<CheckAccess::kYes>(field_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004594 if (klass_type.IsConflict()) {
4595 AppendToLastFailMessage(StringPrintf(" in attempt to access instance field %d (%s) in %s",
4596 field_idx, dex_file_->GetFieldName(field_id),
4597 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers7b078e82014-09-10 14:44:24 -07004598 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08004599 }
jeffhao8cd6dda2012-02-22 10:15:34 -08004600 if (klass_type.IsUnresolvedTypes()) {
Andreas Gampe629be512017-08-25 17:09:32 -07004601 // Accessibility checks depend on resolved fields.
4602 DCHECK(klass_type.Equals(GetDeclaringClass()) || !failures_.empty());
4603
Ian Rogers7b078e82014-09-10 14:44:24 -07004604 return nullptr; // Can't resolve Class so no more to do here
Ian Rogers90040192011-12-16 08:54:29 -08004605 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07004606 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Vladimir Markoe11dd502017-12-08 14:09:45 +00004607 ArtField* field = class_linker->ResolveFieldJLS(field_idx, dex_cache_, class_loader_);
David Brazdilca3c8c32016-09-06 14:04:48 +01004608
4609 // Record result of the field resolution attempt.
4610 VerifierDeps::MaybeRecordFieldResolution(*dex_file_, field_idx, field);
4611
Ian Rogers7b078e82014-09-10 14:44:24 -07004612 if (field == nullptr) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07004613 VLOG(verifier) << "Unable to resolve instance field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07004614 << dex_file_->GetFieldName(field_id) << ") in "
4615 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogers7b078e82014-09-10 14:44:24 -07004616 DCHECK(self_->IsExceptionPending());
4617 self_->ClearException();
4618 return nullptr;
Andreas Gampeeed3a5d2017-11-29 14:58:34 -08004619 } else if (obj_type.IsZeroOrNull()) {
Aart Bik31883642016-06-06 15:02:44 -07004620 // Cannot infer and check type, however, access will cause null pointer exception.
4621 // Fall through into a few last soft failure checks below.
Stephen Kyle695c5982014-08-22 15:03:07 +01004622 } else if (!obj_type.IsReferenceTypes()) {
Aart Bik31883642016-06-06 15:02:44 -07004623 // Trying to read a field from something that isn't a reference.
Stephen Kyle695c5982014-08-22 15:03:07 +01004624 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance field access on object that has "
4625 << "non-reference type " << obj_type;
Ian Rogers7b078e82014-09-10 14:44:24 -07004626 return nullptr;
Ian Rogerse1758fe2012-04-19 11:31:15 -07004627 } else {
David Brazdil0d638bb2016-07-27 15:29:25 +01004628 std::string temp;
Mathieu Chartier3398c782016-09-30 10:27:43 -07004629 ObjPtr<mirror::Class> klass = field->GetDeclaringClass();
Ian Rogersd8f69b02014-09-10 21:43:52 +00004630 const RegType& field_klass =
Vladimir Markobcf17522018-06-01 13:14:32 +01004631 FromClass(klass->GetDescriptor(&temp), klass, klass->CannotBeAssignedFromOtherTypes());
David Brazdil68b5c0b2016-01-19 14:25:29 +00004632 if (obj_type.IsUninitializedTypes()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07004633 // Field accesses through uninitialized references are only allowable for constructors where
David Brazdil68b5c0b2016-01-19 14:25:29 +00004634 // the field is declared in this class.
4635 // Note: this IsConstructor check is technically redundant, as UninitializedThis should only
4636 // appear in constructors.
4637 if (!obj_type.IsUninitializedThisReference() ||
4638 !IsConstructor() ||
4639 !field_klass.Equals(GetDeclaringClass())) {
David Sehr709b0702016-10-13 09:12:37 -07004640 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "cannot access instance field " << field->PrettyField()
David Brazdil68b5c0b2016-01-19 14:25:29 +00004641 << " of a not fully initialized object within the context"
David Sehr709b0702016-10-13 09:12:37 -07004642 << " of " << dex_file_->PrettyMethod(dex_method_idx_);
David Brazdil68b5c0b2016-01-19 14:25:29 +00004643 return nullptr;
4644 }
David Brazdilca3c8c32016-09-06 14:04:48 +01004645 } else if (!field_klass.IsAssignableFrom(obj_type, this)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07004646 // Trying to access C1.field1 using reference of type C2, which is neither C1 or a sub-class
4647 // of C1. For resolution to occur the declared class of the field must be compatible with
4648 // obj_type, we've discovered this wasn't so, so report the field didn't exist.
Andreas Gampe66596242016-04-14 10:55:04 -07004649 VerifyError type;
4650 bool is_aot = Runtime::Current()->IsAotCompiler();
4651 if (is_aot && (field_klass.IsUnresolvedTypes() || obj_type.IsUnresolvedTypes())) {
4652 // Compiler & unresolved types involved, retry at runtime.
4653 type = VerifyError::VERIFY_ERROR_NO_CLASS;
4654 } else {
Andreas Gampe8f4ade02016-04-15 10:09:16 -07004655 // Classes known (resolved; and thus assignability check is precise), or we are at runtime
4656 // and still missing classes. This is a hard failure.
Andreas Gampe66596242016-04-14 10:55:04 -07004657 type = VerifyError::VERIFY_ERROR_BAD_CLASS_HARD;
4658 }
David Sehr709b0702016-10-13 09:12:37 -07004659 Fail(type) << "cannot access instance field " << field->PrettyField()
Andreas Gampe66596242016-04-14 10:55:04 -07004660 << " from object of type " << obj_type;
Ian Rogers7b078e82014-09-10 14:44:24 -07004661 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004662 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004663 }
Aart Bik31883642016-06-06 15:02:44 -07004664
4665 // Few last soft failure checks.
4666 if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
4667 field->GetAccessFlags())) {
David Sehr709b0702016-10-13 09:12:37 -07004668 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access instance field " << field->PrettyField()
Aart Bik31883642016-06-06 15:02:44 -07004669 << " from " << GetDeclaringClass();
4670 return nullptr;
4671 } else if (field->IsStatic()) {
David Sehr709b0702016-10-13 09:12:37 -07004672 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << field->PrettyField()
Aart Bik31883642016-06-06 15:02:44 -07004673 << " to not be static";
4674 return nullptr;
4675 }
4676
4677 return field;
Ian Rogersd81871c2011-10-03 13:57:23 -07004678}
4679
Andreas Gampe896df402014-10-20 22:25:29 -07004680template <MethodVerifier::FieldAccessType kAccType>
4681void MethodVerifier::VerifyISFieldAccess(const Instruction* inst, const RegType& insn_type,
4682 bool is_primitive, bool is_static) {
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00004683 uint32_t field_idx = GetFieldIdxOfFieldAccess(inst, is_static);
Mathieu Chartierc7853442015-03-27 14:35:38 -07004684 ArtField* field;
Ian Rogersb94a27b2011-10-26 00:33:41 -07004685 if (is_static) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07004686 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07004687 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004688 const RegType& object_type = work_line_->GetRegisterType(this, inst->VRegB_22c());
David Brazdil68b5c0b2016-01-19 14:25:29 +00004689
4690 // One is not allowed to access fields on uninitialized references, except to write to
4691 // fields in the constructor (before calling another constructor).
4692 // GetInstanceField does an assignability check which will fail for uninitialized types.
4693 // We thus modify the type if the uninitialized reference is a "this" reference (this also
4694 // checks at the same time that we're verifying a constructor).
4695 bool should_adjust = (kAccType == FieldAccessType::kAccPut) &&
4696 object_type.IsUninitializedThisReference();
4697 const RegType& adjusted_type = should_adjust
4698 ? GetRegTypeCache()->FromUninitialized(object_type)
4699 : object_type;
4700 field = GetInstanceField(adjusted_type, field_idx);
Andreas Gampe896df402014-10-20 22:25:29 -07004701 if (UNLIKELY(have_pending_hard_failure_)) {
4702 return;
4703 }
Alex Light4a2c8fc2016-02-12 11:01:54 -08004704 if (should_adjust) {
4705 if (field == nullptr) {
4706 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "Might be accessing a superclass instance field prior "
4707 << "to the superclass being initialized in "
David Sehr709b0702016-10-13 09:12:37 -07004708 << dex_file_->PrettyMethod(dex_method_idx_);
Alex Light4a2c8fc2016-02-12 11:01:54 -08004709 } else if (field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
4710 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "cannot access superclass instance field "
David Sehr709b0702016-10-13 09:12:37 -07004711 << field->PrettyField() << " of a not fully initialized "
Alex Light4a2c8fc2016-02-12 11:01:54 -08004712 << "object within the context of "
David Sehr709b0702016-10-13 09:12:37 -07004713 << dex_file_->PrettyMethod(dex_method_idx_);
Alex Light4a2c8fc2016-02-12 11:01:54 -08004714 return;
4715 }
4716 }
Ian Rogersb94a27b2011-10-26 00:33:41 -07004717 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00004718 const RegType* field_type = nullptr;
Ian Rogers7b078e82014-09-10 14:44:24 -07004719 if (field != nullptr) {
Andreas Gampe896df402014-10-20 22:25:29 -07004720 if (kAccType == FieldAccessType::kAccPut) {
4721 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
David Sehr709b0702016-10-13 09:12:37 -07004722 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << field->PrettyField()
Andreas Gampe896df402014-10-20 22:25:29 -07004723 << " from other class " << GetDeclaringClass();
Aart Bikc2bc2652016-05-23 14:58:49 -07004724 // Keep hunting for possible hard fails.
Andreas Gampe896df402014-10-20 22:25:29 -07004725 }
4726 }
4727
Mathieu Chartier3398c782016-09-30 10:27:43 -07004728 ObjPtr<mirror::Class> field_type_class =
Vladimir Marko208f6702017-12-08 12:00:50 +00004729 can_load_classes_ ? field->ResolveType() : field->LookupResolvedType();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004730 if (field_type_class != nullptr) {
Mathieu Chartier3398c782016-09-30 10:27:43 -07004731 field_type = &FromClass(field->GetTypeDescriptor(),
Vladimir Markobcf17522018-06-01 13:14:32 +01004732 field_type_class,
Andreas Gampef23f33d2015-06-23 14:18:17 -07004733 field_type_class->CannotBeAssignedFromOtherTypes());
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08004734 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004735 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
4736 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004737 }
Andreas Gampe7da4c402017-08-25 11:30:48 -07004738 } else {
4739 // If we don't have the field (it seems we failed resolution) and this is a PUT, we need to
4740 // redo verification at runtime as the field may be final, unless the field id shows it's in
4741 // the same class.
4742 //
4743 // For simplicity, it is OK to not distinguish compile-time vs runtime, and post this an
4744 // ACCESS_FIELD failure at runtime. This has the same effect as NO_FIELD - punting the class
4745 // to the access-checks interpreter.
4746 //
4747 // Note: see b/34966607. This and above may be changed in the future.
4748 if (kAccType == FieldAccessType::kAccPut) {
4749 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
4750 const char* field_class_descriptor = dex_file_->GetFieldDeclaringClassDescriptor(field_id);
4751 const RegType* field_class_type = &reg_types_.FromDescriptor(GetClassLoader(),
4752 field_class_descriptor,
4753 false);
4754 if (!field_class_type->Equals(GetDeclaringClass())) {
4755 Fail(VERIFY_ERROR_ACCESS_FIELD) << "could not check field put for final field modify of "
4756 << field_class_descriptor
4757 << "."
4758 << dex_file_->GetFieldName(field_id)
4759 << " from other class "
4760 << GetDeclaringClass();
4761 }
4762 }
Ian Rogers0d604842012-04-16 14:50:24 -07004763 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004764 if (field_type == nullptr) {
4765 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
4766 const char* descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
Mathieu Chartierbf99f772014-08-23 16:37:27 -07004767 field_type = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004768 }
Sebastien Hertz757b3042014-03-28 14:34:28 +01004769 DCHECK(field_type != nullptr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02004770 const uint32_t vregA = (is_static) ? inst->VRegA_21c() : inst->VRegA_22c();
Andreas Gampe896df402014-10-20 22:25:29 -07004771 static_assert(kAccType == FieldAccessType::kAccPut || kAccType == FieldAccessType::kAccGet,
4772 "Unexpected third access type");
4773 if (kAccType == FieldAccessType::kAccPut) {
4774 // sput or iput.
4775 if (is_primitive) {
4776 VerifyPrimitivePut(*field_type, insn_type, vregA);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004777 } else {
David Brazdilca3c8c32016-09-06 14:04:48 +01004778 if (!insn_type.IsAssignableFrom(*field_type, this)) {
Vladimir Marko414000e2015-06-23 17:45:21 +01004779 // If the field type is not a reference, this is a global failure rather than
4780 // a class change failure as the instructions and the descriptors for the type
4781 // should have been consistent within the same file at compile time.
4782 VerifyError error = field_type->IsReferenceTypes() ? VERIFY_ERROR_BAD_CLASS_SOFT
4783 : VERIFY_ERROR_BAD_CLASS_HARD;
David Sehr709b0702016-10-13 09:12:37 -07004784 Fail(error) << "expected field " << ArtField::PrettyField(field)
Vladimir Marko414000e2015-06-23 17:45:21 +01004785 << " to be compatible with type '" << insn_type
4786 << "' but found type '" << *field_type
4787 << "' in put-object";
Andreas Gampe896df402014-10-20 22:25:29 -07004788 return;
4789 }
4790 work_line_->VerifyRegisterType(this, vregA, *field_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004791 }
Andreas Gampe896df402014-10-20 22:25:29 -07004792 } else if (kAccType == FieldAccessType::kAccGet) {
4793 // sget or iget.
4794 if (is_primitive) {
4795 if (field_type->Equals(insn_type) ||
4796 (field_type->IsFloat() && insn_type.IsInteger()) ||
4797 (field_type->IsDouble() && insn_type.IsLong())) {
4798 // expected that read is of the correct primitive type or that int reads are reading
4799 // floats or long reads are reading doubles
4800 } else {
4801 // This is a global failure rather than a class change failure as the instructions and
4802 // the descriptors for the type should have been consistent within the same file at
4803 // compile time
David Sehr709b0702016-10-13 09:12:37 -07004804 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << ArtField::PrettyField(field)
Andreas Gampe896df402014-10-20 22:25:29 -07004805 << " to be of type '" << insn_type
4806 << "' but found type '" << *field_type << "' in get";
4807 return;
4808 }
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08004809 } else {
David Brazdilca3c8c32016-09-06 14:04:48 +01004810 if (!insn_type.IsAssignableFrom(*field_type, this)) {
Vladimir Marko414000e2015-06-23 17:45:21 +01004811 // If the field type is not a reference, this is a global failure rather than
4812 // a class change failure as the instructions and the descriptors for the type
4813 // should have been consistent within the same file at compile time.
4814 VerifyError error = field_type->IsReferenceTypes() ? VERIFY_ERROR_BAD_CLASS_SOFT
4815 : VERIFY_ERROR_BAD_CLASS_HARD;
David Sehr709b0702016-10-13 09:12:37 -07004816 Fail(error) << "expected field " << ArtField::PrettyField(field)
Vladimir Marko414000e2015-06-23 17:45:21 +01004817 << " to be compatible with type '" << insn_type
4818 << "' but found type '" << *field_type
4819 << "' in get-object";
Andreas Gampe890da292015-07-06 17:20:18 -07004820 if (error != VERIFY_ERROR_BAD_CLASS_HARD) {
Andreas Gampead238ce2015-08-24 21:13:08 -07004821 work_line_->SetRegisterType<LockOp::kClear>(this, vregA, reg_types_.Conflict());
Andreas Gampe890da292015-07-06 17:20:18 -07004822 }
Andreas Gampe896df402014-10-20 22:25:29 -07004823 return;
4824 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004825 }
Andreas Gampe896df402014-10-20 22:25:29 -07004826 if (!field_type->IsLowHalf()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07004827 work_line_->SetRegisterType<LockOp::kClear>(this, vregA, *field_type);
Andreas Gampe896df402014-10-20 22:25:29 -07004828 } else {
4829 work_line_->SetRegisterTypeWide(this, vregA, *field_type, field_type->HighHalf(&reg_types_));
4830 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07004831 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004832 LOG(FATAL) << "Unexpected case.";
Ian Rogersd81871c2011-10-03 13:57:23 -07004833 }
4834}
4835
Ian Rogers776ac1f2012-04-13 23:36:36 -07004836bool MethodVerifier::CheckNotMoveException(const uint16_t* insns, int insn_idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004837 if ((insns[insn_idx] & 0xff) == Instruction::MOVE_EXCEPTION) {
jeffhaod5347e02012-03-22 17:25:05 -07004838 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-exception";
Ian Rogersd81871c2011-10-03 13:57:23 -07004839 return false;
4840 }
4841 return true;
4842}
4843
Stephen Kyle9bc61992014-09-22 13:53:15 +01004844bool MethodVerifier::CheckNotMoveResult(const uint16_t* insns, int insn_idx) {
4845 if (((insns[insn_idx] & 0xff) >= Instruction::MOVE_RESULT) &&
4846 ((insns[insn_idx] & 0xff) <= Instruction::MOVE_RESULT_OBJECT)) {
4847 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-result*";
4848 return false;
4849 }
4850 return true;
4851}
4852
4853bool MethodVerifier::CheckNotMoveExceptionOrMoveResult(const uint16_t* insns, int insn_idx) {
4854 return (CheckNotMoveException(insns, insn_idx) && CheckNotMoveResult(insns, insn_idx));
4855}
4856
Ian Rogersebbdd872014-07-07 23:53:08 -07004857bool MethodVerifier::UpdateRegisters(uint32_t next_insn, RegisterLine* merge_line,
4858 bool update_merge_line) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004859 bool changed = true;
4860 RegisterLine* target_line = reg_table_.GetLine(next_insn);
Mathieu Chartierde40d472015-10-15 17:47:48 -07004861 if (!GetInstructionFlags(next_insn).IsVisitedOrChanged()) {
jeffhaobdb76512011-09-07 11:43:16 -07004862 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07004863 * We haven't processed this instruction before, and we haven't touched the registers here, so
4864 * there's nothing to "merge". Copy the registers over and mark it as changed. (This is the
4865 * only way a register can transition out of "unknown", so this is not just an optimization.)
jeffhaobdb76512011-09-07 11:43:16 -07004866 */
Andreas Gampea727e372015-08-25 09:22:37 -07004867 target_line->CopyFromLine(merge_line);
Mathieu Chartierde40d472015-10-15 17:47:48 -07004868 if (GetInstructionFlags(next_insn).IsReturn()) {
Jeff Haob24b4a72013-07-31 13:47:31 -07004869 // Verify that the monitor stack is empty on return.
Andreas Gampea727e372015-08-25 09:22:37 -07004870 merge_line->VerifyMonitorStackEmpty(this);
4871
Ian Rogersb8c78592013-07-25 23:52:52 +00004872 // For returns we only care about the operand to the return, all other registers are dead.
4873 // Initialize them as conflicts so they don't add to GC and deoptimization information.
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08004874 const Instruction* ret_inst = &code_item_accessor_.InstructionAt(next_insn);
Andreas Gampea727e372015-08-25 09:22:37 -07004875 AdjustReturnLine(this, ret_inst, target_line);
Aart Bik31883642016-06-06 15:02:44 -07004876 // Directly bail if a hard failure was found.
Aart Bikb0526322016-06-01 14:06:00 -07004877 if (have_pending_hard_failure_) {
4878 return false;
4879 }
Ian Rogersb8c78592013-07-25 23:52:52 +00004880 }
jeffhaobdb76512011-09-07 11:43:16 -07004881 } else {
Mathieu Chartier361e04a2016-02-16 14:06:35 -08004882 RegisterLineArenaUniquePtr copy;
Andreas Gampe92d77202017-12-06 20:49:00 -08004883 if (UNLIKELY(VLOG_IS_ON(verifier_debug))) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07004884 copy.reset(RegisterLine::Create(target_line->NumRegs(), this));
Ian Rogers7b0c5b42012-02-16 15:29:07 -08004885 copy->CopyFromLine(target_line);
4886 }
Ian Rogers7b078e82014-09-10 14:44:24 -07004887 changed = target_line->MergeRegisters(this, merge_line);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004888 if (have_pending_hard_failure_) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004889 return false;
jeffhaobdb76512011-09-07 11:43:16 -07004890 }
Andreas Gampe92d77202017-12-06 20:49:00 -08004891 if (UNLIKELY(VLOG_IS_ON(verifier_debug)) && changed) {
Elliott Hughes398f64b2012-03-26 18:05:48 -07004892 LogVerifyInfo() << "Merging at [" << reinterpret_cast<void*>(work_insn_idx_) << "]"
Elliott Hughesc073b072012-05-24 19:29:17 -07004893 << " to [" << reinterpret_cast<void*>(next_insn) << "]: " << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07004894 << copy->Dump(this) << " MERGE\n"
4895 << merge_line->Dump(this) << " ==\n"
4896 << target_line->Dump(this) << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07004897 }
Ian Rogersebbdd872014-07-07 23:53:08 -07004898 if (update_merge_line && changed) {
4899 merge_line->CopyFromLine(target_line);
4900 }
jeffhaobdb76512011-09-07 11:43:16 -07004901 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004902 if (changed) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07004903 GetInstructionFlags(next_insn).SetChanged();
jeffhaobdb76512011-09-07 11:43:16 -07004904 }
4905 return true;
4906}
4907
Ian Rogers7b3ddd22013-02-21 15:19:52 -08004908InstructionFlags* MethodVerifier::CurrentInsnFlags() {
Mathieu Chartierde40d472015-10-15 17:47:48 -07004909 return &GetInstructionFlags(work_insn_idx_);
Ian Rogers776ac1f2012-04-13 23:36:36 -07004910}
4911
Ian Rogersd8f69b02014-09-10 21:43:52 +00004912const RegType& MethodVerifier::GetMethodReturnType() {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004913 if (return_type_ == nullptr) {
Nicolas Geoffrayb041a402017-11-13 15:16:22 +00004914 if (method_being_verified_ != nullptr) {
Vladimir Markob45528c2017-07-27 14:14:28 +01004915 ObjPtr<mirror::Class> return_type_class = can_load_classes_
Nicolas Geoffrayb041a402017-11-13 15:16:22 +00004916 ? method_being_verified_->ResolveReturnType()
4917 : method_being_verified_->LookupResolvedReturnType();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004918 if (return_type_class != nullptr) {
Nicolas Geoffrayb041a402017-11-13 15:16:22 +00004919 return_type_ = &FromClass(method_being_verified_->GetReturnTypeDescriptor(),
Vladimir Markobcf17522018-06-01 13:14:32 +01004920 return_type_class,
Andreas Gampef23f33d2015-06-23 14:18:17 -07004921 return_type_class->CannotBeAssignedFromOtherTypes());
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004922 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004923 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
4924 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004925 }
4926 }
4927 if (return_type_ == nullptr) {
4928 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
4929 const DexFile::ProtoId& proto_id = dex_file_->GetMethodPrototype(method_id);
Andreas Gampea5b09a62016-11-17 15:21:22 -08004930 dex::TypeIndex return_type_idx = proto_id.return_type_idx_;
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004931 const char* descriptor = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(return_type_idx));
Mathieu Chartierbf99f772014-08-23 16:37:27 -07004932 return_type_ = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004933 }
4934 }
4935 return *return_type_;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004936}
4937
Ian Rogersd8f69b02014-09-10 21:43:52 +00004938const RegType& MethodVerifier::GetDeclaringClass() {
Ian Rogers7b078e82014-09-10 14:44:24 -07004939 if (declaring_class_ == nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004940 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
Brian Carlstrom93c33962013-07-26 10:37:43 -07004941 const char* descriptor
4942 = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(method_id.class_idx_));
Nicolas Geoffrayb041a402017-11-13 15:16:22 +00004943 if (method_being_verified_ != nullptr) {
Vladimir Marko2cffc5d2018-05-29 15:40:56 +01004944 ObjPtr<mirror::Class> klass = method_being_verified_->GetDeclaringClass();
Mathieu Chartierde40d472015-10-15 17:47:48 -07004945 declaring_class_ = &FromClass(descriptor, klass, klass->CannotBeAssignedFromOtherTypes());
Ian Rogers637c65b2013-05-31 11:46:00 -07004946 } else {
Mathieu Chartierbf99f772014-08-23 16:37:27 -07004947 declaring_class_ = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers637c65b2013-05-31 11:46:00 -07004948 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07004949 }
Ian Rogers637c65b2013-05-31 11:46:00 -07004950 return *declaring_class_;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004951}
4952
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004953std::vector<int32_t> MethodVerifier::DescribeVRegs(uint32_t dex_pc) {
4954 RegisterLine* line = reg_table_.GetLine(dex_pc);
Sebastien Hertzaa0c00c2014-03-14 17:58:54 +01004955 DCHECK(line != nullptr) << "No register line at DEX pc " << StringPrintf("0x%x", dex_pc);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004956 std::vector<int32_t> result;
4957 for (size_t i = 0; i < line->NumRegs(); ++i) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004958 const RegType& type = line->GetRegisterType(this, i);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004959 if (type.IsConstant()) {
4960 result.push_back(type.IsPreciseConstant() ? kConstant : kImpreciseConstant);
Ian Rogers7b078e82014-09-10 14:44:24 -07004961 const ConstantType* const_val = down_cast<const ConstantType*>(&type);
4962 result.push_back(const_val->ConstantValue());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004963 } else if (type.IsConstantLo()) {
4964 result.push_back(type.IsPreciseConstantLo() ? kConstant : kImpreciseConstant);
Ian Rogers7b078e82014-09-10 14:44:24 -07004965 const ConstantType* const_val = down_cast<const ConstantType*>(&type);
4966 result.push_back(const_val->ConstantValueLo());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004967 } else if (type.IsConstantHi()) {
4968 result.push_back(type.IsPreciseConstantHi() ? kConstant : kImpreciseConstant);
Ian Rogers7b078e82014-09-10 14:44:24 -07004969 const ConstantType* const_val = down_cast<const ConstantType*>(&type);
4970 result.push_back(const_val->ConstantValueHi());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004971 } else if (type.IsIntegralTypes()) {
4972 result.push_back(kIntVReg);
4973 result.push_back(0);
4974 } else if (type.IsFloat()) {
4975 result.push_back(kFloatVReg);
4976 result.push_back(0);
4977 } else if (type.IsLong()) {
4978 result.push_back(kLongLoVReg);
4979 result.push_back(0);
4980 result.push_back(kLongHiVReg);
4981 result.push_back(0);
4982 ++i;
4983 } else if (type.IsDouble()) {
4984 result.push_back(kDoubleLoVReg);
4985 result.push_back(0);
4986 result.push_back(kDoubleHiVReg);
4987 result.push_back(0);
4988 ++i;
4989 } else if (type.IsUndefined() || type.IsConflict() || type.IsHighHalf()) {
4990 result.push_back(kUndefined);
4991 result.push_back(0);
4992 } else {
Ian Rogers7b3ddd22013-02-21 15:19:52 -08004993 CHECK(type.IsNonZeroReferenceTypes());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004994 result.push_back(kReferenceVReg);
4995 result.push_back(0);
4996 }
4997 }
4998 return result;
4999}
5000
Ian Rogersd8f69b02014-09-10 21:43:52 +00005001const RegType& MethodVerifier::DetermineCat1Constant(int32_t value, bool precise) {
Sebastien Hertz849600b2013-12-20 10:28:08 +01005002 if (precise) {
5003 // Precise constant type.
5004 return reg_types_.FromCat1Const(value, true);
5005 } else {
5006 // Imprecise constant type.
5007 if (value < -32768) {
5008 return reg_types_.IntConstant();
5009 } else if (value < -128) {
5010 return reg_types_.ShortConstant();
5011 } else if (value < 0) {
5012 return reg_types_.ByteConstant();
5013 } else if (value == 0) {
5014 return reg_types_.Zero();
5015 } else if (value == 1) {
5016 return reg_types_.One();
5017 } else if (value < 128) {
5018 return reg_types_.PosByteConstant();
5019 } else if (value < 32768) {
5020 return reg_types_.PosShortConstant();
5021 } else if (value < 65536) {
5022 return reg_types_.CharConstant();
5023 } else {
5024 return reg_types_.IntConstant();
5025 }
5026 }
5027}
5028
Elliott Hughes0a1038b2012-06-14 16:24:17 -07005029void MethodVerifier::Init() {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08005030 art::verifier::RegTypeCache::Init();
Brian Carlstrome7d856b2012-01-11 18:10:55 -08005031}
5032
Elliott Hughes0a1038b2012-06-14 16:24:17 -07005033void MethodVerifier::Shutdown() {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08005034 verifier::RegTypeCache::ShutDown();
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08005035}
jeffhaod1224c72012-02-29 13:43:08 -08005036
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -07005037void MethodVerifier::VisitStaticRoots(RootVisitor* visitor) {
5038 RegTypeCache::VisitStaticRoots(visitor);
Mathieu Chartier7c438b12014-09-12 17:01:24 -07005039}
5040
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -07005041void MethodVerifier::VisitRoots(RootVisitor* visitor, const RootInfo& root_info) {
5042 reg_types_.VisitRoots(visitor, root_info);
Mathieu Chartierc528dba2013-11-26 12:00:11 -08005043}
5044
Andreas Gampef23f33d2015-06-23 14:18:17 -07005045const RegType& MethodVerifier::FromClass(const char* descriptor,
Vladimir Marko2cffc5d2018-05-29 15:40:56 +01005046 ObjPtr<mirror::Class> klass,
Andreas Gampef23f33d2015-06-23 14:18:17 -07005047 bool precise) {
5048 DCHECK(klass != nullptr);
5049 if (precise && !klass->IsInstantiable() && !klass->IsPrimitive()) {
5050 Fail(VerifyError::VERIFY_ERROR_NO_CLASS) << "Could not create precise reference for "
5051 << "non-instantiable klass " << descriptor;
5052 precise = false;
5053 }
5054 return reg_types_.FromClass(descriptor, klass, precise);
5055}
5056
Ian Rogersd81871c2011-10-03 13:57:23 -07005057} // namespace verifier
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07005058} // namespace art