blob: be6915f04b2a5122b586e99512ab05506f2a0d72 [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"
Andreas Gampe57943812017-12-06 21:39:13 -080027#include "base/logging.h" // For VLOG.
Ian Rogers637c65b2013-05-31 11:46:00 -070028#include "base/mutex-inl.h"
Vladimir Marko637ee0b2015-09-04 12:47:41 +010029#include "base/stl_util.h"
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -080030#include "base/systrace.h"
Vladimir Marko80afd022015-05-19 18:08:00 +010031#include "base/time_utils.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070032#include "class_linker.h"
Vladimir Marko2b5eaa22013-12-13 13:59:30 +000033#include "compiler_callbacks.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070034#include "dex_file-inl.h"
Ian Rogersd0583802013-06-01 10:51:46 -070035#include "dex_instruction-inl.h"
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080036#include "dex_instruction_utils.h"
Alex Lighteb7c1442015-08-31 13:17:42 -070037#include "experimental_flags.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070038#include "gc/accounting/card_table-inl.h"
Andreas Gampe6d7abbd2017-04-24 13:19:09 -070039#include "handle_scope-inl.h"
Ian Rogers2bcb4a42012-11-08 10:39:18 -080040#include "indenter.h"
Ian Rogers84fa0742011-10-25 18:13:30 -070041#include "intern_table.h"
Ian Rogers0571d352011-11-03 19:51:38 -070042#include "leb128.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080043#include "mirror/class-inl.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070044#include "mirror/class.h"
Ian Rogers39ebcb82013-05-30 16:57:23 -070045#include "mirror/dex_cache-inl.h"
Orion Hodsoncfa325e2016-10-13 10:25:54 +010046#include "mirror/method_handle_impl.h"
Orion Hodson2e599942017-09-22 16:17:41 +010047#include "mirror/method_type.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080048#include "mirror/object-inl.h"
49#include "mirror/object_array-inl.h"
Ian Rogers7b078e82014-09-10 14:44:24 -070050#include "reg_type-inl.h"
Ian Rogers39ebcb82013-05-30 16:57:23 -070051#include "register_line-inl.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070052#include "runtime.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070053#include "scoped_thread_state_change-inl.h"
Andreas Gampe513061a2017-06-01 09:17:34 -070054#include "stack.h"
Vladimir Marko80afd022015-05-19 18:08:00 +010055#include "utils.h"
Andreas Gampe6d7abbd2017-04-24 13:19:09 -070056#include "verifier_compiler_binding.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070057#include "verifier_deps.h"
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070058
59namespace art {
Ian Rogersd81871c2011-10-03 13:57:23 -070060namespace verifier {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070061
Andreas Gampe46ee31b2016-12-14 10:11:49 -080062using android::base::StringPrintf;
63
Mathieu Chartier8e219ae2014-08-19 14:29:46 -070064static constexpr bool kTimeVerifyMethod = !kIsDebugBuild;
Ian Rogers2c8a8572011-10-24 17:11:36 -070065
Andreas Gampeebf850c2015-08-14 15:37:35 -070066// On VLOG(verifier), should we dump the whole state when we run into a hard failure?
67static constexpr bool kDumpRegLinesOnHardFailureIfVLOG = true;
68
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -080069// We print a warning blurb about "dx --no-optimize" when we find monitor-locking issues. Make
70// sure we only print this once.
71static bool gPrintedDxMonitorText = false;
72
Vladimir Marko69d310e2017-10-09 14:12:23 +010073PcToRegisterLineTable::PcToRegisterLineTable(ScopedArenaAllocator& allocator)
74 : register_lines_(allocator.Adapter(kArenaAllocVerifier)) {}
Mathieu Chartierde40d472015-10-15 17:47:48 -070075
Ian Rogers7b3ddd22013-02-21 15:19:52 -080076void PcToRegisterLineTable::Init(RegisterTrackingMode mode, InstructionFlags* flags,
Ian Rogersd81871c2011-10-03 13:57:23 -070077 uint32_t insns_size, uint16_t registers_size,
Ian Rogers776ac1f2012-04-13 23:36:36 -070078 MethodVerifier* verifier) {
Ian Rogersd81871c2011-10-03 13:57:23 -070079 DCHECK_GT(insns_size, 0U);
Mathieu Chartierde40d472015-10-15 17:47:48 -070080 register_lines_.resize(insns_size);
Ian Rogersd81871c2011-10-03 13:57:23 -070081 for (uint32_t i = 0; i < insns_size; i++) {
82 bool interesting = false;
83 switch (mode) {
84 case kTrackRegsAll:
85 interesting = flags[i].IsOpcode();
86 break;
Sameer Abu Asal02c42232013-04-30 12:09:45 -070087 case kTrackCompilerInterestPoints:
Brian Carlstrom02c8cc62013-07-18 15:54:44 -070088 interesting = flags[i].IsCompileTimeInfoPoint() || flags[i].IsBranchTarget();
Ian Rogersd81871c2011-10-03 13:57:23 -070089 break;
90 case kTrackRegsBranches:
91 interesting = flags[i].IsBranchTarget();
92 break;
93 default:
94 break;
95 }
96 if (interesting) {
Mathieu Chartierde40d472015-10-15 17:47:48 -070097 register_lines_[i].reset(RegisterLine::Create(registers_size, verifier));
Ian Rogersd0fbd852013-09-24 18:17:04 -070098 }
99 }
100}
101
Mathieu Chartierde40d472015-10-15 17:47:48 -0700102PcToRegisterLineTable::~PcToRegisterLineTable() {}
Ian Rogersd81871c2011-10-03 13:57:23 -0700103
Andreas Gampe7c038102014-10-27 20:08:46 -0700104// Note: returns true on failure.
105ALWAYS_INLINE static inline bool FailOrAbort(MethodVerifier* verifier, bool condition,
106 const char* error_msg, uint32_t work_insn_idx) {
107 if (kIsDebugBuild) {
Andreas Gampef8f36c22016-11-19 14:51:52 -0800108 // In a debug build, abort if the error condition is wrong. Only warn if
109 // we are already aborting (as this verification is likely run to print
110 // lock information).
111 if (LIKELY(gAborting == 0)) {
112 DCHECK(condition) << error_msg << work_insn_idx;
113 } else {
114 if (!condition) {
115 LOG(ERROR) << error_msg << work_insn_idx;
116 verifier->Fail(VERIFY_ERROR_BAD_CLASS_HARD) << error_msg << work_insn_idx;
117 return true;
118 }
119 }
Andreas Gampe7c038102014-10-27 20:08:46 -0700120 } else {
121 // In a non-debug build, just fail the class.
122 if (!condition) {
123 verifier->Fail(VERIFY_ERROR_BAD_CLASS_HARD) << error_msg << work_insn_idx;
124 return true;
125 }
126 }
127
128 return false;
129}
130
Stephen Kyle7e541c92014-12-17 17:10:02 +0000131static void SafelyMarkAllRegistersAsConflicts(MethodVerifier* verifier, RegisterLine* reg_line) {
Andreas Gampef10b6e12015-08-12 10:48:12 -0700132 if (verifier->IsInstanceConstructor()) {
Stephen Kyle7e541c92014-12-17 17:10:02 +0000133 // Before we mark all regs as conflicts, check that we don't have an uninitialized this.
134 reg_line->CheckConstructorReturn(verifier);
135 }
136 reg_line->MarkAllRegistersAsConflicts(verifier);
137}
138
Andreas Gampe6d7abbd2017-04-24 13:19:09 -0700139FailureKind MethodVerifier::VerifyClass(Thread* self,
140 mirror::Class* klass,
141 CompilerCallbacks* callbacks,
142 bool allow_soft_failures,
143 HardFailLogMode log_level,
144 std::string* error) {
jeffhaobdb76512011-09-07 11:43:16 -0700145 if (klass->IsVerified()) {
Andreas Gampe6d7abbd2017-04-24 13:19:09 -0700146 return FailureKind::kNoFailure;
jeffhaobdb76512011-09-07 11:43:16 -0700147 }
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800148 bool early_failure = false;
149 std::string failure_message;
Mathieu Chartierf8322842014-05-16 10:59:25 -0700150 const DexFile& dex_file = klass->GetDexFile();
151 const DexFile::ClassDef* class_def = klass->GetClassDef();
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800152 mirror::Class* super = klass->GetSuperClass();
Ian Rogers1ff3c982014-08-12 02:30:58 -0700153 std::string temp;
Ian Rogers7b078e82014-09-10 14:44:24 -0700154 if (super == nullptr && strcmp("Ljava/lang/Object;", klass->GetDescriptor(&temp)) != 0) {
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800155 early_failure = true;
156 failure_message = " that has no super class";
Ian Rogers7b078e82014-09-10 14:44:24 -0700157 } else if (super != nullptr && super->IsFinal()) {
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800158 early_failure = true;
David Sehr709b0702016-10-13 09:12:37 -0700159 failure_message = " that attempts to sub-class final class " + super->PrettyDescriptor();
Ian Rogers7b078e82014-09-10 14:44:24 -0700160 } else if (class_def == nullptr) {
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800161 early_failure = true;
162 failure_message = " that isn't present in dex file " + dex_file.GetLocation();
163 }
164 if (early_failure) {
David Sehr709b0702016-10-13 09:12:37 -0700165 *error = "Verifier rejected class " + klass->PrettyDescriptor() + failure_message;
Andreas Gampe53e32d12015-12-09 21:03:23 -0800166 if (callbacks != nullptr) {
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800167 ClassReference ref(&dex_file, klass->GetDexClassDefIndex());
Andreas Gampe53e32d12015-12-09 21:03:23 -0800168 callbacks->ClassRejected(ref);
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800169 }
Andreas Gampe6d7abbd2017-04-24 13:19:09 -0700170 return FailureKind::kHardFailure;
jeffhaobdb76512011-09-07 11:43:16 -0700171 }
Ian Rogers7b078e82014-09-10 14:44:24 -0700172 StackHandleScope<2> hs(self);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700173 Handle<mirror::DexCache> dex_cache(hs.NewHandle(klass->GetDexCache()));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700174 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(klass->GetClassLoader()));
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800175 return VerifyClass(self,
176 &dex_file,
177 dex_cache,
178 class_loader,
David Brazdil15fc7292016-09-02 14:13:18 +0100179 *class_def,
Andreas Gampe53e32d12015-12-09 21:03:23 -0800180 callbacks,
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800181 allow_soft_failures,
Andreas Gampe7fe30232016-03-25 16:58:00 -0700182 log_level,
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800183 error);
184}
185
186template <bool kDirect>
187static bool HasNextMethod(ClassDataItemIterator* it) {
188 return kDirect ? it->HasNextDirectMethod() : it->HasNextVirtualMethod();
189}
190
Andreas Gampe6d7abbd2017-04-24 13:19:09 -0700191static FailureKind FailureKindMax(FailureKind fk1, FailureKind fk2) {
192 static_assert(FailureKind::kNoFailure < FailureKind::kSoftFailure
193 && FailureKind::kSoftFailure < FailureKind::kHardFailure,
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800194 "Unexpected FailureKind order");
195 return std::max(fk1, fk2);
196}
197
198void MethodVerifier::FailureData::Merge(const MethodVerifier::FailureData& fd) {
199 kind = FailureKindMax(kind, fd.kind);
200 types |= fd.types;
201}
202
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800203template <bool kDirect>
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800204MethodVerifier::FailureData MethodVerifier::VerifyMethods(Thread* self,
205 ClassLinker* linker,
206 const DexFile* dex_file,
David Brazdil15fc7292016-09-02 14:13:18 +0100207 const DexFile::ClassDef& class_def,
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800208 ClassDataItemIterator* it,
209 Handle<mirror::DexCache> dex_cache,
210 Handle<mirror::ClassLoader> class_loader,
211 CompilerCallbacks* callbacks,
212 bool allow_soft_failures,
Andreas Gampe5fd66d02016-09-12 20:22:19 -0700213 HardFailLogMode log_level,
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800214 bool need_precise_constants,
215 std::string* error_string) {
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800216 DCHECK(it != nullptr);
217
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800218 MethodVerifier::FailureData failure_data;
219
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800220 int64_t previous_method_idx = -1;
221 while (HasNextMethod<kDirect>(it)) {
222 self->AllowThreadSuspension();
223 uint32_t method_idx = it->GetMemberIndex();
224 if (method_idx == previous_method_idx) {
225 // smali can create dex files with two encoded_methods sharing the same method_idx
226 // http://code.google.com/p/smali/issues/detail?id=119
227 it->Next();
228 continue;
229 }
230 previous_method_idx = method_idx;
David Brazdil15fc7292016-09-02 14:13:18 +0100231 InvokeType type = it->GetMethodInvokeType(class_def);
Vladimir Markoba118822017-06-12 15:41:56 +0100232 ArtMethod* method = linker->ResolveMethod<ClassLinker::ResolveMode::kNoChecks>(
Vladimir Marko89011192017-12-11 13:45:05 +0000233 method_idx, dex_cache, class_loader, /* referrer */ nullptr, type);
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800234 if (method == nullptr) {
235 DCHECK(self->IsExceptionPending());
236 // We couldn't resolve the method, but continue regardless.
237 self->ClearException();
238 } else {
239 DCHECK(method->GetDeclaringClassUnchecked() != nullptr) << type;
240 }
241 StackHandleScope<1> hs(self);
242 std::string hard_failure_msg;
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800243 MethodVerifier::FailureData result = VerifyMethod(self,
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800244 method_idx,
245 dex_file,
246 dex_cache,
247 class_loader,
248 class_def,
249 it->GetMethodCodeItem(),
250 method,
251 it->GetMethodAccessFlags(),
Andreas Gampe53e32d12015-12-09 21:03:23 -0800252 callbacks,
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800253 allow_soft_failures,
Andreas Gampe7fe30232016-03-25 16:58:00 -0700254 log_level,
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800255 need_precise_constants,
256 &hard_failure_msg);
Andreas Gampe6d7abbd2017-04-24 13:19:09 -0700257 if (result.kind == FailureKind::kHardFailure) {
258 if (failure_data.kind == FailureKind::kHardFailure) {
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800259 // If we logged an error before, we need a newline.
260 *error_string += "\n";
261 } else {
262 // If we didn't log a hard failure before, print the header of the message.
263 *error_string += "Verifier rejected class ";
David Brazdil15fc7292016-09-02 14:13:18 +0100264 *error_string += PrettyDescriptor(dex_file->GetClassDescriptor(class_def));
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800265 *error_string += ":";
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800266 }
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800267 *error_string += " ";
268 *error_string += hard_failure_msg;
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800269 }
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800270 failure_data.Merge(result);
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800271 it->Next();
272 }
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800273
274 return failure_data;
Shih-wei Liao371814f2011-10-27 16:52:10 -0700275}
276
Andreas Gampe6d7abbd2017-04-24 13:19:09 -0700277FailureKind MethodVerifier::VerifyClass(Thread* self,
278 const DexFile* dex_file,
279 Handle<mirror::DexCache> dex_cache,
280 Handle<mirror::ClassLoader> class_loader,
281 const DexFile::ClassDef& class_def,
282 CompilerCallbacks* callbacks,
283 bool allow_soft_failures,
284 HardFailLogMode log_level,
285 std::string* error) {
Andreas Gampec6548162017-12-08 12:15:22 -0800286 SCOPED_TRACE << "VerifyClass " << PrettyDescriptor(dex_file->GetClassDescriptor(class_def));
Andreas Gampe507cc6f2015-06-19 22:58:47 -0700287
288 // A class must not be abstract and final.
David Brazdil15fc7292016-09-02 14:13:18 +0100289 if ((class_def.access_flags_ & (kAccAbstract | kAccFinal)) == (kAccAbstract | kAccFinal)) {
Andreas Gampe507cc6f2015-06-19 22:58:47 -0700290 *error = "Verifier rejected class ";
David Brazdil15fc7292016-09-02 14:13:18 +0100291 *error += PrettyDescriptor(dex_file->GetClassDescriptor(class_def));
Andreas Gampe507cc6f2015-06-19 22:58:47 -0700292 *error += ": class is abstract and final.";
Andreas Gampe6d7abbd2017-04-24 13:19:09 -0700293 return FailureKind::kHardFailure;
Andreas Gampe507cc6f2015-06-19 22:58:47 -0700294 }
295
David Brazdil15fc7292016-09-02 14:13:18 +0100296 const uint8_t* class_data = dex_file->GetClassData(class_def);
Ian Rogers7b078e82014-09-10 14:44:24 -0700297 if (class_data == nullptr) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700298 // empty class, probably a marker interface
Andreas Gampe6d7abbd2017-04-24 13:19:09 -0700299 return FailureKind::kNoFailure;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700300 }
jeffhaof56197c2012-03-05 18:01:54 -0800301 ClassDataItemIterator it(*dex_file, class_data);
Mathieu Chartiere17cf242017-06-19 11:05:51 -0700302 it.SkipAllFields();
Ian Rogersad0b3a32012-04-16 14:50:24 -0700303 ClassLinker* linker = Runtime::Current()->GetClassLinker();
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800304 // Direct methods.
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800305 MethodVerifier::FailureData data1 = VerifyMethods<true>(self,
306 linker,
307 dex_file,
308 class_def,
309 &it,
310 dex_cache,
311 class_loader,
312 callbacks,
313 allow_soft_failures,
Andreas Gampe7fe30232016-03-25 16:58:00 -0700314 log_level,
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800315 false /* need precise constants */,
316 error);
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800317 // Virtual methods.
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800318 MethodVerifier::FailureData data2 = VerifyMethods<false>(self,
319 linker,
320 dex_file,
321 class_def,
322 &it,
323 dex_cache,
324 class_loader,
325 callbacks,
326 allow_soft_failures,
Andreas Gampe7fe30232016-03-25 16:58:00 -0700327 log_level,
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800328 false /* need precise constants */,
329 error);
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800330
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800331 data1.Merge(data2);
332
Andreas Gampe6d7abbd2017-04-24 13:19:09 -0700333 if (data1.kind == FailureKind::kNoFailure) {
334 return FailureKind::kNoFailure;
jeffhaof1e6b7c2012-06-05 18:33:30 -0700335 } else {
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800336 if ((data1.types & VERIFY_ERROR_LOCKING) != 0) {
337 // Print a warning about expected slow-down. Use a string temporary to print one contiguous
338 // warning.
339 std::string tmp =
340 StringPrintf("Class %s failed lock verification and will run slower.",
David Brazdil15fc7292016-09-02 14:13:18 +0100341 PrettyDescriptor(dex_file->GetClassDescriptor(class_def)).c_str());
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800342 if (!gPrintedDxMonitorText) {
343 tmp = tmp + "\nCommon causes for lock verification issues are non-optimized dex code\n"
344 "and incorrect proguard optimizations.";
345 gPrintedDxMonitorText = true;
346 }
347 LOG(WARNING) << tmp;
348 }
349 return data1.kind;
jeffhaof1e6b7c2012-06-05 18:33:30 -0700350 }
jeffhaof56197c2012-03-05 18:01:54 -0800351}
352
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800353static bool IsLargeMethod(const CodeItemDataAccessor& accessor) {
354 if (!accessor.HasCodeItem()) {
Andreas Gampe3c651fc2015-05-21 14:06:46 -0700355 return false;
356 }
357
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800358 uint16_t registers_size = accessor.RegistersSize();
359 uint32_t insns_size = accessor.InsnsSizeInCodeUnits();
Andreas Gampea4f5bf62015-05-18 20:50:12 -0700360
361 return registers_size * insns_size > 4*1024*1024;
362}
363
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800364MethodVerifier::FailureData MethodVerifier::VerifyMethod(Thread* self,
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800365 uint32_t method_idx,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800366 const DexFile* dex_file,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700367 Handle<mirror::DexCache> dex_cache,
368 Handle<mirror::ClassLoader> class_loader,
David Brazdil15fc7292016-09-02 14:13:18 +0100369 const DexFile::ClassDef& class_def,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800370 const DexFile::CodeItem* code_item,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700371 ArtMethod* method,
Jeff Haoee988952013-04-16 14:23:47 -0700372 uint32_t method_access_flags,
Andreas Gampe53e32d12015-12-09 21:03:23 -0800373 CompilerCallbacks* callbacks,
Ian Rogers46960fe2014-05-23 10:43:43 -0700374 bool allow_soft_failures,
Andreas Gampe5fd66d02016-09-12 20:22:19 -0700375 HardFailLogMode log_level,
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800376 bool need_precise_constants,
377 std::string* hard_failure_msg) {
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800378 MethodVerifier::FailureData result;
Mathieu Chartier8e219ae2014-08-19 14:29:46 -0700379 uint64_t start_ns = kTimeVerifyMethod ? NanoTime() : 0;
Ian Rogersc8982582012-09-07 16:53:25 -0700380
Andreas Gampebf9611f2016-03-25 16:58:00 -0700381 MethodVerifier verifier(self,
382 dex_file,
383 dex_cache,
384 class_loader,
385 class_def,
386 code_item,
387 method_idx,
388 method,
389 method_access_flags,
390 true /* can_load_classes */,
391 allow_soft_failures,
392 need_precise_constants,
393 false /* verify to dump */,
394 true /* allow_thread_suspension */);
Ian Rogers46960fe2014-05-23 10:43:43 -0700395 if (verifier.Verify()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700396 // Verification completed, however failures may be pending that didn't cause the verification
397 // to hard fail.
Ian Rogers46960fe2014-05-23 10:43:43 -0700398 CHECK(!verifier.have_pending_hard_failure_);
Andreas Gampe53e32d12015-12-09 21:03:23 -0800399
400 if (code_item != nullptr && callbacks != nullptr) {
401 // Let the interested party know that the method was verified.
402 callbacks->MethodVerified(&verifier);
403 }
404
Ian Rogers46960fe2014-05-23 10:43:43 -0700405 if (verifier.failures_.size() != 0) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -0700406 if (VLOG_IS_ON(verifier)) {
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800407 verifier.DumpFailures(VLOG_STREAM(verifier) << "Soft verification failures in "
David Sehr709b0702016-10-13 09:12:37 -0700408 << dex_file->PrettyMethod(method_idx) << "\n");
Anwar Ghuloum75a43f12013-08-13 17:22:14 -0700409 }
Andreas Gampe92d77202017-12-06 20:49:00 -0800410 if (VLOG_IS_ON(verifier_debug)) {
411 std::cout << "\n" << verifier.info_messages_.str();
412 verifier.Dump(std::cout);
413 }
Andreas Gampe6d7abbd2017-04-24 13:19:09 -0700414 result.kind = FailureKind::kSoftFailure;
Nicolas Geoffray250a3782016-04-20 16:27:53 +0100415 if (method != nullptr &&
416 !CanCompilerHandleVerificationFailure(verifier.encountered_failure_types_)) {
Alex Lightfcbafb32017-02-02 15:09:54 -0800417 method->SetDontCompile();
Nicolas Geoffray250a3782016-04-20 16:27:53 +0100418 }
419 }
Andreas Gampe56fdd0e2016-04-28 14:56:54 -0700420 if (method != nullptr) {
421 if (verifier.HasInstructionThatWillThrow()) {
Alex Lightfcbafb32017-02-02 15:09:54 -0800422 method->SetDontCompile();
Calin Juravle857f0582016-12-20 14:36:59 +0000423 if (Runtime::Current()->IsAotCompiler() &&
424 (callbacks != nullptr) && !callbacks->IsBootImage()) {
425 // When compiling apps, make HasInstructionThatWillThrow a soft error to trigger
426 // re-verification at runtime.
427 // The dead code after the throw is not verified and might be invalid. This may cause
428 // the JIT compiler to crash since it assumes that all the code is valid.
429 //
430 // There's a strong assumption that the entire boot image is verified and all its dex
431 // code is valid (even the dead and unverified one). As such this is done only for apps.
432 // (CompilerDriver DCHECKs in VerifyClassVisitor that methods from boot image are
433 // fully verified).
Andreas Gampe6d7abbd2017-04-24 13:19:09 -0700434 result.kind = FailureKind::kSoftFailure;
Calin Juravle857f0582016-12-20 14:36:59 +0000435 }
Andreas Gampe56fdd0e2016-04-28 14:56:54 -0700436 }
437 if ((verifier.encountered_failure_types_ & VerifyError::VERIFY_ERROR_LOCKING) != 0) {
Orion Hodsoncfcc9cf2017-09-29 15:07:27 +0100438 method->SetMustCountLocks();
Andreas Gampe56fdd0e2016-04-28 14:56:54 -0700439 }
jeffhaof56197c2012-03-05 18:01:54 -0800440 }
441 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700442 // Bad method data.
Ian Rogers46960fe2014-05-23 10:43:43 -0700443 CHECK_NE(verifier.failures_.size(), 0U);
Igor Murashkin4d7b75f2015-07-21 17:03:36 -0700444
445 if (UNLIKELY(verifier.have_pending_experimental_failure_)) {
446 // Failed due to being forced into interpreter. This is ok because
447 // we just want to skip verification.
Andreas Gampe6d7abbd2017-04-24 13:19:09 -0700448 result.kind = FailureKind::kSoftFailure;
Igor Murashkin4d7b75f2015-07-21 17:03:36 -0700449 } else {
450 CHECK(verifier.have_pending_hard_failure_);
Andreas Gampe7fe30232016-03-25 16:58:00 -0700451 if (VLOG_IS_ON(verifier)) {
Andreas Gampe5fd66d02016-09-12 20:22:19 -0700452 log_level = std::max(HardFailLogMode::kLogVerbose, log_level);
Andreas Gampe7fe30232016-03-25 16:58:00 -0700453 }
Andreas Gampe5fd66d02016-09-12 20:22:19 -0700454 if (log_level >= HardFailLogMode::kLogVerbose) {
455 LogSeverity severity;
456 switch (log_level) {
457 case HardFailLogMode::kLogVerbose:
458 severity = LogSeverity::VERBOSE;
459 break;
460 case HardFailLogMode::kLogWarning:
461 severity = LogSeverity::WARNING;
462 break;
463 case HardFailLogMode::kLogInternalFatal:
Andreas Gampe3fec9ac2016-09-13 10:47:28 -0700464 severity = LogSeverity::FATAL_WITHOUT_ABORT;
Andreas Gampe5fd66d02016-09-12 20:22:19 -0700465 break;
466 default:
467 LOG(FATAL) << "Unsupported log-level " << static_cast<uint32_t>(log_level);
468 UNREACHABLE();
469 }
Andreas Gampe3fec9ac2016-09-13 10:47:28 -0700470 verifier.DumpFailures(LOG_STREAM(severity) << "Verification error in "
David Sehr709b0702016-10-13 09:12:37 -0700471 << dex_file->PrettyMethod(method_idx)
Andreas Gampe3fec9ac2016-09-13 10:47:28 -0700472 << "\n");
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800473 }
474 if (hard_failure_msg != nullptr) {
475 CHECK(!verifier.failure_messages_.empty());
476 *hard_failure_msg =
477 verifier.failure_messages_[verifier.failure_messages_.size() - 1]->str();
478 }
Andreas Gampe6d7abbd2017-04-24 13:19:09 -0700479 result.kind = FailureKind::kHardFailure;
Andreas Gampe53e32d12015-12-09 21:03:23 -0800480
481 if (callbacks != nullptr) {
482 // Let the interested party know that we failed the class.
David Brazdil15fc7292016-09-02 14:13:18 +0100483 ClassReference ref(dex_file, dex_file->GetIndexForClassDef(class_def));
Andreas Gampe53e32d12015-12-09 21:03:23 -0800484 callbacks->ClassRejected(ref);
485 }
Igor Murashkin4d7b75f2015-07-21 17:03:36 -0700486 }
Andreas Gampe92d77202017-12-06 20:49:00 -0800487 if (VLOG_IS_ON(verifier) || VLOG_IS_ON(verifier_debug)) {
Ian Rogers46960fe2014-05-23 10:43:43 -0700488 std::cout << "\n" << verifier.info_messages_.str();
489 verifier.Dump(std::cout);
jeffhaof56197c2012-03-05 18:01:54 -0800490 }
jeffhaof56197c2012-03-05 18:01:54 -0800491 }
Mathieu Chartier8e219ae2014-08-19 14:29:46 -0700492 if (kTimeVerifyMethod) {
493 uint64_t duration_ns = NanoTime() - start_ns;
494 if (duration_ns > MsToNs(100)) {
David Sehr709b0702016-10-13 09:12:37 -0700495 LOG(WARNING) << "Verification of " << dex_file->PrettyMethod(method_idx)
Andreas Gampea4f5bf62015-05-18 20:50:12 -0700496 << " took " << PrettyDuration(duration_ns)
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800497 << (IsLargeMethod(verifier.CodeItem()) ? " (large method)" : "");
Mathieu Chartier8e219ae2014-08-19 14:29:46 -0700498 }
Ian Rogersc8982582012-09-07 16:53:25 -0700499 }
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800500 result.types = verifier.encountered_failure_types_;
Ian Rogersc8982582012-09-07 16:53:25 -0700501 return result;
jeffhaof56197c2012-03-05 18:01:54 -0800502}
503
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100504MethodVerifier* MethodVerifier::VerifyMethodAndDump(Thread* self,
505 VariableIndentationOutputStream* vios,
506 uint32_t dex_method_idx,
507 const DexFile* dex_file,
508 Handle<mirror::DexCache> dex_cache,
509 Handle<mirror::ClassLoader> class_loader,
David Brazdil15fc7292016-09-02 14:13:18 +0100510 const DexFile::ClassDef& class_def,
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100511 const DexFile::CodeItem* code_item,
512 ArtMethod* method,
513 uint32_t method_access_flags) {
Andreas Gampebf9611f2016-03-25 16:58:00 -0700514 MethodVerifier* verifier = new MethodVerifier(self,
515 dex_file,
516 dex_cache,
517 class_loader,
518 class_def,
519 code_item,
520 dex_method_idx,
521 method,
522 method_access_flags,
523 true /* can_load_classes */,
524 true /* allow_soft_failures */,
525 true /* need_precise_constants */,
526 true /* verify_to_dump */,
527 true /* allow_thread_suspension */);
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700528 verifier->Verify();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100529 verifier->DumpFailures(vios->Stream());
530 vios->Stream() << verifier->info_messages_.str();
Andreas Gampe5cbcde22014-09-16 14:59:49 -0700531 // Only dump and return if no hard failures. Otherwise the verifier may be not fully initialized
532 // and querying any info is dangerous/can abort.
533 if (verifier->have_pending_hard_failure_) {
534 delete verifier;
535 return nullptr;
536 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100537 verifier->Dump(vios);
Andreas Gampe5cbcde22014-09-16 14:59:49 -0700538 return verifier;
539 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800540}
541
Ian Rogers7b078e82014-09-10 14:44:24 -0700542MethodVerifier::MethodVerifier(Thread* self,
Andreas Gampe53e32d12015-12-09 21:03:23 -0800543 const DexFile* dex_file,
544 Handle<mirror::DexCache> dex_cache,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700545 Handle<mirror::ClassLoader> class_loader,
David Brazdil15fc7292016-09-02 14:13:18 +0100546 const DexFile::ClassDef& class_def,
Andreas Gampe53e32d12015-12-09 21:03:23 -0800547 const DexFile::CodeItem* code_item,
548 uint32_t dex_method_idx,
549 ArtMethod* method,
550 uint32_t method_access_flags,
551 bool can_load_classes,
552 bool allow_soft_failures,
553 bool need_precise_constants,
554 bool verify_to_dump,
Mathieu Chartier4306ef82014-12-19 18:41:47 -0800555 bool allow_thread_suspension)
Ian Rogers7b078e82014-09-10 14:44:24 -0700556 : self_(self),
Mathieu Chartierde40d472015-10-15 17:47:48 -0700557 arena_stack_(Runtime::Current()->GetArenaPool()),
Vladimir Marko69d310e2017-10-09 14:12:23 +0100558 allocator_(&arena_stack_),
Alex Lightd9aff132017-10-31 22:30:05 +0000559 reg_types_(can_load_classes, allocator_, allow_thread_suspension),
Vladimir Marko69d310e2017-10-09 14:12:23 +0100560 reg_table_(allocator_),
Andreas Gampee2abbc62017-09-15 11:59:26 -0700561 work_insn_idx_(dex::kDexNoIndex),
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800562 dex_method_idx_(dex_method_idx),
Ian Rogers637c65b2013-05-31 11:46:00 -0700563 mirror_method_(method),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700564 method_access_flags_(method_access_flags),
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700565 return_type_(nullptr),
jeffhaof56197c2012-03-05 18:01:54 -0800566 dex_file_(dex_file),
567 dex_cache_(dex_cache),
568 class_loader_(class_loader),
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700569 class_def_(class_def),
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800570 code_item_accessor_(dex_file, code_item),
Ian Rogers7b078e82014-09-10 14:44:24 -0700571 declaring_class_(nullptr),
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700572 interesting_dex_pc_(-1),
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700573 monitor_enter_dex_pcs_(nullptr),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700574 have_pending_hard_failure_(false),
jeffhaofaf459e2012-08-31 15:32:47 -0700575 have_pending_runtime_throw_failure_(false),
Igor Murashkin4d7b75f2015-07-21 17:03:36 -0700576 have_pending_experimental_failure_(false),
Andreas Gamped12e7822015-06-25 10:26:40 -0700577 have_any_pending_runtime_throw_failure_(false),
jeffhaof56197c2012-03-05 18:01:54 -0800578 new_instance_count_(0),
Elliott Hughes80537bb2013-01-04 16:37:26 -0800579 monitor_enter_count_(0),
Andreas Gampe0760a812015-08-26 17:12:51 -0700580 encountered_failure_types_(0),
Jeff Haoee988952013-04-16 14:23:47 -0700581 can_load_classes_(can_load_classes),
Sebastien Hertz4d4adb12013-07-24 16:14:19 +0200582 allow_soft_failures_(allow_soft_failures),
Ian Rogers46960fe2014-05-23 10:43:43 -0700583 need_precise_constants_(need_precise_constants),
Sebastien Hertz4d4adb12013-07-24 16:14:19 +0200584 has_check_casts_(false),
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700585 has_virtual_or_interface_invokes_(false),
Mathieu Chartier4306ef82014-12-19 18:41:47 -0800586 verify_to_dump_(verify_to_dump),
Andreas Gamped5ad72f2015-06-26 17:33:47 -0700587 allow_thread_suspension_(allow_thread_suspension),
Andreas Gampee6215c02015-08-31 18:54:38 -0700588 is_constructor_(false),
Andreas Gamped5ad72f2015-06-26 17:33:47 -0700589 link_(nullptr) {
Mathieu Chartierd0ad2ee2015-03-31 14:59:59 -0700590 self->PushVerifier(this);
jeffhaof56197c2012-03-05 18:01:54 -0800591}
592
Mathieu Chartier590fee92013-09-13 13:46:47 -0700593MethodVerifier::~MethodVerifier() {
Mathieu Chartierd0ad2ee2015-03-31 14:59:59 -0700594 Thread::Current()->PopVerifier(this);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700595 STLDeleteElements(&failure_messages_);
596}
597
Andreas Gampeaaf0d382017-11-27 14:10:21 -0800598void MethodVerifier::FindLocksAtDexPc(
599 ArtMethod* m,
600 uint32_t dex_pc,
601 std::vector<MethodVerifier::DexLockInfo>* monitor_enter_dex_pcs) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700602 StackHandleScope<2> hs(Thread::Current());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700603 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache()));
604 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader()));
Andreas Gampebf9611f2016-03-25 16:58:00 -0700605 MethodVerifier verifier(hs.Self(),
606 m->GetDexFile(),
607 dex_cache,
608 class_loader,
David Brazdil15fc7292016-09-02 14:13:18 +0100609 m->GetClassDef(),
Andreas Gampebf9611f2016-03-25 16:58:00 -0700610 m->GetCodeItem(),
611 m->GetDexMethodIndex(),
612 m,
613 m->GetAccessFlags(),
614 false /* can_load_classes */,
615 true /* allow_soft_failures */,
616 false /* need_precise_constants */,
617 false /* verify_to_dump */,
618 false /* allow_thread_suspension */);
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700619 verifier.interesting_dex_pc_ = dex_pc;
Ian Rogers46960fe2014-05-23 10:43:43 -0700620 verifier.monitor_enter_dex_pcs_ = monitor_enter_dex_pcs;
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700621 verifier.FindLocksAtDexPc();
622}
623
624void MethodVerifier::FindLocksAtDexPc() {
Ian Rogers7b078e82014-09-10 14:44:24 -0700625 CHECK(monitor_enter_dex_pcs_ != nullptr);
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -0800626 CHECK(code_item_accessor_.HasCodeItem()); // This only makes sense for methods with code.
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700627
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -0800628 // Quick check whether there are any monitor_enter instructions before verifying.
629 for (const DexInstructionPcPair& inst : code_item_accessor_) {
630 if (inst->Opcode() == Instruction::MONITOR_ENTER) {
631 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
632 // verification. In practice, the phase we want relies on data structures set up by all the
633 // earlier passes, so we just run the full method verification and bail out early when we've
634 // got what we wanted.
635 Verify();
636 return;
637 }
Andreas Gampecb3c08f2014-09-18 13:16:38 -0700638 }
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700639}
640
Mathieu Chartiere401d142015-04-22 13:56:20 -0700641ArtField* MethodVerifier::FindAccessedFieldAtDexPc(ArtMethod* m, uint32_t dex_pc) {
642 StackHandleScope<2> hs(Thread::Current());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700643 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache()));
644 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader()));
Andreas Gampebf9611f2016-03-25 16:58:00 -0700645 MethodVerifier verifier(hs.Self(),
646 m->GetDexFile(),
647 dex_cache,
648 class_loader,
David Brazdil15fc7292016-09-02 14:13:18 +0100649 m->GetClassDef(),
Andreas Gampebf9611f2016-03-25 16:58:00 -0700650 m->GetCodeItem(),
651 m->GetDexMethodIndex(),
652 m,
653 m->GetAccessFlags(),
654 true /* can_load_classes */,
655 true /* allow_soft_failures */,
656 false /* need_precise_constants */,
657 false /* verify_to_dump */,
658 true /* allow_thread_suspension */);
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200659 return verifier.FindAccessedFieldAtDexPc(dex_pc);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200660}
661
Mathieu Chartierc7853442015-03-27 14:35:38 -0700662ArtField* MethodVerifier::FindAccessedFieldAtDexPc(uint32_t dex_pc) {
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -0800663 CHECK(code_item_accessor_.HasCodeItem()); // This only makes sense for methods with code.
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200664
665 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
666 // verification. In practice, the phase we want relies on data structures set up by all the
667 // earlier passes, so we just run the full method verification and bail out early when we've
668 // got what we wanted.
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200669 bool success = Verify();
670 if (!success) {
Ian Rogers9bc54402014-04-17 16:40:01 -0700671 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200672 }
673 RegisterLine* register_line = reg_table_.GetLine(dex_pc);
Ian Rogers7b078e82014-09-10 14:44:24 -0700674 if (register_line == nullptr) {
Ian Rogers9bc54402014-04-17 16:40:01 -0700675 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200676 }
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -0800677 const Instruction* inst = &code_item_accessor_.InstructionAt(dex_pc);
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200678 return GetQuickFieldAccess(inst, register_line);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200679}
680
Mathieu Chartiere401d142015-04-22 13:56:20 -0700681ArtMethod* MethodVerifier::FindInvokedMethodAtDexPc(ArtMethod* m, uint32_t dex_pc) {
682 StackHandleScope<2> hs(Thread::Current());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700683 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache()));
684 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader()));
Andreas Gampebf9611f2016-03-25 16:58:00 -0700685 MethodVerifier verifier(hs.Self(),
686 m->GetDexFile(),
687 dex_cache,
688 class_loader,
David Brazdil15fc7292016-09-02 14:13:18 +0100689 m->GetClassDef(),
Andreas Gampebf9611f2016-03-25 16:58:00 -0700690 m->GetCodeItem(),
691 m->GetDexMethodIndex(),
692 m,
693 m->GetAccessFlags(),
694 true /* can_load_classes */,
695 true /* allow_soft_failures */,
696 false /* need_precise_constants */,
697 false /* verify_to_dump */,
698 true /* allow_thread_suspension */);
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200699 return verifier.FindInvokedMethodAtDexPc(dex_pc);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200700}
701
Mathieu Chartiere401d142015-04-22 13:56:20 -0700702ArtMethod* MethodVerifier::FindInvokedMethodAtDexPc(uint32_t dex_pc) {
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -0800703 CHECK(code_item_accessor_.HasCodeItem()); // This only makes sense for methods with code.
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200704
705 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
706 // verification. In practice, the phase we want relies on data structures set up by all the
707 // earlier passes, so we just run the full method verification and bail out early when we've
708 // got what we wanted.
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200709 bool success = Verify();
710 if (!success) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700711 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200712 }
713 RegisterLine* register_line = reg_table_.GetLine(dex_pc);
Ian Rogers7b078e82014-09-10 14:44:24 -0700714 if (register_line == nullptr) {
715 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200716 }
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -0800717 const Instruction* inst = &code_item_accessor_.InstructionAt(dex_pc);
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200718 const bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
Mathieu Chartier091d2382015-03-06 10:59:06 -0800719 return GetQuickInvokedMethod(inst, register_line, is_range, false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200720}
721
Ian Rogersad0b3a32012-04-16 14:50:24 -0700722bool MethodVerifier::Verify() {
Andreas Gampee6215c02015-08-31 18:54:38 -0700723 // Some older code doesn't correctly mark constructors as such. Test for this case by looking at
724 // the name.
725 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
726 const char* method_name = dex_file_->StringDataByIdx(method_id.name_idx_);
727 bool instance_constructor_by_name = strcmp("<init>", method_name) == 0;
728 bool static_constructor_by_name = strcmp("<clinit>", method_name) == 0;
729 bool constructor_by_name = instance_constructor_by_name || static_constructor_by_name;
730 // Check that only constructors are tagged, and check for bad code that doesn't tag constructors.
731 if ((method_access_flags_ & kAccConstructor) != 0) {
732 if (!constructor_by_name) {
733 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
734 << "method is marked as constructor, but not named accordingly";
jeffhaobdb76512011-09-07 11:43:16 -0700735 return false;
Andreas Gampee6215c02015-08-31 18:54:38 -0700736 }
737 is_constructor_ = true;
738 } else if (constructor_by_name) {
David Sehr709b0702016-10-13 09:12:37 -0700739 LOG(WARNING) << "Method " << dex_file_->PrettyMethod(dex_method_idx_)
Andreas Gampee6215c02015-08-31 18:54:38 -0700740 << " not marked as constructor.";
741 is_constructor_ = true;
742 }
743 // If it's a constructor, check whether IsStatic() matches the name.
744 // This should have been rejected by the dex file verifier. Only do in debug build.
745 if (kIsDebugBuild) {
746 if (IsConstructor()) {
747 if (IsStatic() ^ static_constructor_by_name) {
748 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
749 << "constructor name doesn't match static flag";
750 return false;
751 }
jeffhaobdb76512011-09-07 11:43:16 -0700752 }
jeffhaobdb76512011-09-07 11:43:16 -0700753 }
Andreas Gampee6215c02015-08-31 18:54:38 -0700754
755 // Methods may only have one of public/protected/private.
756 // This should have been rejected by the dex file verifier. Only do in debug build.
757 if (kIsDebugBuild) {
758 size_t access_mod_count =
759 (((method_access_flags_ & kAccPublic) == 0) ? 0 : 1) +
760 (((method_access_flags_ & kAccProtected) == 0) ? 0 : 1) +
761 (((method_access_flags_ & kAccPrivate) == 0) ? 0 : 1);
762 if (access_mod_count > 1) {
763 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "method has more than one of public/protected/private";
764 return false;
765 }
766 }
767
768 // If there aren't any instructions, make sure that's expected, then exit successfully.
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -0800769 if (!code_item_accessor_.HasCodeItem()) {
Andreas Gampe76ed99d2016-03-28 18:31:29 -0700770 // Only native or abstract methods may not have code.
771 if ((method_access_flags_ & (kAccNative | kAccAbstract)) == 0) {
772 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "zero-length code in concrete non-native method";
773 return false;
774 }
775
Andreas Gampee6215c02015-08-31 18:54:38 -0700776 // This should have been rejected by the dex file verifier. Only do in debug build.
Andreas Gampe76ed99d2016-03-28 18:31:29 -0700777 // Note: the above will also be rejected in the dex file verifier, starting in dex version 37.
Andreas Gampee6215c02015-08-31 18:54:38 -0700778 if (kIsDebugBuild) {
Andreas Gampee6215c02015-08-31 18:54:38 -0700779 if ((method_access_flags_ & kAccAbstract) != 0) {
780 // Abstract methods are not allowed to have the following flags.
781 static constexpr uint32_t kForbidden =
782 kAccPrivate |
783 kAccStatic |
784 kAccFinal |
785 kAccNative |
786 kAccStrict |
787 kAccSynchronized;
788 if ((method_access_flags_ & kForbidden) != 0) {
789 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
790 << "method can't be abstract and private/static/final/native/strict/synchronized";
791 return false;
792 }
793 }
David Brazdil15fc7292016-09-02 14:13:18 +0100794 if ((class_def_.GetJavaAccessFlags() & kAccInterface) != 0) {
Alex Lighteb7c1442015-08-31 13:17:42 -0700795 // Interface methods must be public and abstract (if default methods are disabled).
Neil Fuller9724c632016-01-07 15:42:47 +0000796 uint32_t kRequired = kAccPublic;
Alex Lighteb7c1442015-08-31 13:17:42 -0700797 if ((method_access_flags_ & kRequired) != kRequired) {
Neil Fuller9724c632016-01-07 15:42:47 +0000798 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface methods must be public";
Andreas Gampee6215c02015-08-31 18:54:38 -0700799 return false;
800 }
801 // In addition to the above, interface methods must not be protected.
802 static constexpr uint32_t kForbidden = kAccProtected;
803 if ((method_access_flags_ & kForbidden) != 0) {
804 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface methods can't be protected";
805 return false;
806 }
807 }
808 // We also don't allow constructors to be abstract or native.
809 if (IsConstructor()) {
810 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "constructors can't be abstract or native";
811 return false;
812 }
813 }
814 return true;
815 }
816
817 // This should have been rejected by the dex file verifier. Only do in debug build.
818 if (kIsDebugBuild) {
819 // When there's code, the method must not be native or abstract.
820 if ((method_access_flags_ & (kAccNative | kAccAbstract)) != 0) {
821 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "non-zero-length code in abstract or native method";
822 return false;
823 }
824
David Brazdil15fc7292016-09-02 14:13:18 +0100825 if ((class_def_.GetJavaAccessFlags() & kAccInterface) != 0) {
Alex Lighteb7c1442015-08-31 13:17:42 -0700826 // Interfaces may always have static initializers for their fields. If we are running with
827 // default methods enabled we also allow other public, static, non-final methods to have code.
828 // Otherwise that is the only type of method allowed.
Alex Light0db36b32015-10-27 14:06:34 -0700829 if (!(IsConstructor() && IsStatic())) {
Neil Fuller9724c632016-01-07 15:42:47 +0000830 if (IsInstanceConstructor()) {
831 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interfaces may not have non-static constructor";
832 return false;
833 } else if (method_access_flags_ & kAccFinal) {
834 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interfaces may not have final methods";
835 return false;
Alex Lightb55f1ac2016-04-12 15:50:55 -0700836 } else {
837 uint32_t access_flag_options = kAccPublic;
838 if (dex_file_->GetVersion() >= DexFile::kDefaultMethodsVersion) {
839 access_flag_options |= kAccPrivate;
840 }
841 if (!(method_access_flags_ & access_flag_options)) {
842 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
843 << "interfaces may not have protected or package-private members";
844 return false;
845 }
Alex Lighteb7c1442015-08-31 13:17:42 -0700846 }
Andreas Gampee6215c02015-08-31 18:54:38 -0700847 }
848 }
849
850 // Instance constructors must not be synchronized.
851 if (IsInstanceConstructor()) {
852 static constexpr uint32_t kForbidden = kAccSynchronized;
853 if ((method_access_flags_ & kForbidden) != 0) {
854 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "constructors can't be synchronized";
855 return false;
856 }
857 }
858 }
859
Ian Rogersd81871c2011-10-03 13:57:23 -0700860 // Sanity-check the register counts. ins + locals = registers, so make sure that ins <= registers.
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -0800861 if (code_item_accessor_.InsSize() > code_item_accessor_.RegistersSize()) {
862 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad register counts (ins="
863 << code_item_accessor_.InsSize()
864 << " regs=" << code_item_accessor_.RegistersSize();
Ian Rogersd81871c2011-10-03 13:57:23 -0700865 return false;
jeffhaobdb76512011-09-07 11:43:16 -0700866 }
Alex Lighteb7c1442015-08-31 13:17:42 -0700867
Ian Rogersd81871c2011-10-03 13:57:23 -0700868 // Allocate and initialize an array to hold instruction data.
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -0800869 insn_flags_.reset(allocator_.AllocArray<InstructionFlags>(
870 code_item_accessor_.InsnsSizeInCodeUnits()));
Mathieu Chartierde40d472015-10-15 17:47:48 -0700871 DCHECK(insn_flags_ != nullptr);
872 std::uninitialized_fill_n(insn_flags_.get(),
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -0800873 code_item_accessor_.InsnsSizeInCodeUnits(),
Mathieu Chartierde40d472015-10-15 17:47:48 -0700874 InstructionFlags());
Ian Rogersd81871c2011-10-03 13:57:23 -0700875 // Run through the instructions and see if the width checks out.
876 bool result = ComputeWidthsAndCountOps();
Andreas Gampebf1cb772017-05-15 15:39:00 -0700877 bool allow_runtime_only_instructions = !Runtime::Current()->IsAotCompiler() || verify_to_dump_;
Ian Rogersd81871c2011-10-03 13:57:23 -0700878 // Flag instructions guarded by a "try" block and check exception handlers.
879 result = result && ScanTryCatchBlocks();
880 // Perform static instruction verification.
Andreas Gampebf1cb772017-05-15 15:39:00 -0700881 result = result && (allow_runtime_only_instructions
882 ? VerifyInstructions<true>()
883 : VerifyInstructions<false>());
Ian Rogersad0b3a32012-04-16 14:50:24 -0700884 // Perform code-flow analysis and return.
Vladimir Marko2b5eaa22013-12-13 13:59:30 +0000885 result = result && VerifyCodeFlow();
Andreas Gampe53e32d12015-12-09 21:03:23 -0800886
Vladimir Marko2b5eaa22013-12-13 13:59:30 +0000887 return result;
jeffhaoba5ebb92011-08-25 17:24:37 -0700888}
889
Ian Rogers776ac1f2012-04-13 23:36:36 -0700890std::ostream& MethodVerifier::Fail(VerifyError error) {
Andreas Gampe0760a812015-08-26 17:12:51 -0700891 // Mark the error type as encountered.
Andreas Gampea727e372015-08-25 09:22:37 -0700892 encountered_failure_types_ |= static_cast<uint32_t>(error);
Andreas Gampe0760a812015-08-26 17:12:51 -0700893
Ian Rogersad0b3a32012-04-16 14:50:24 -0700894 switch (error) {
895 case VERIFY_ERROR_NO_CLASS:
896 case VERIFY_ERROR_NO_FIELD:
897 case VERIFY_ERROR_NO_METHOD:
898 case VERIFY_ERROR_ACCESS_CLASS:
899 case VERIFY_ERROR_ACCESS_FIELD:
900 case VERIFY_ERROR_ACCESS_METHOD:
Ian Rogers08f753d2012-08-24 14:35:25 -0700901 case VERIFY_ERROR_INSTANTIATION:
902 case VERIFY_ERROR_CLASS_CHANGE:
Igor Murashkin158f35c2015-06-10 15:55:30 -0700903 case VERIFY_ERROR_FORCE_INTERPRETER:
Andreas Gampea727e372015-08-25 09:22:37 -0700904 case VERIFY_ERROR_LOCKING:
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800905 if (Runtime::Current()->IsAotCompiler() || !can_load_classes_) {
jeffhaofaf459e2012-08-31 15:32:47 -0700906 // If we're optimistically running verification at compile time, turn NO_xxx, ACCESS_xxx,
907 // class change and instantiation errors into soft verification errors so that we re-verify
908 // at runtime. We may fail to find or to agree on access because of not yet available class
909 // loaders, or class loaders that will differ at runtime. In these cases, we don't want to
910 // affect the soundness of the code being compiled. Instead, the generated code runs "slow
911 // paths" that dynamically perform the verification and cause the behavior to be that akin
912 // to an interpreter.
913 error = VERIFY_ERROR_BAD_CLASS_SOFT;
914 } else {
Jeff Haoa3faaf42013-09-03 19:07:00 -0700915 // If we fail again at runtime, mark that this instruction would throw and force this
916 // method to be executed using the interpreter with checks.
jeffhaofaf459e2012-08-31 15:32:47 -0700917 have_pending_runtime_throw_failure_ = true;
Andreas Gamped7f8d052015-03-12 11:05:47 -0700918
919 // We need to save the work_line if the instruction wasn't throwing before. Otherwise we'll
920 // try to merge garbage.
921 // Note: this assumes that Fail is called before we do any work_line modifications.
Andreas Gamped5ad72f2015-06-26 17:33:47 -0700922 // Note: this can fail before we touch any instruction, for the signature of a method. So
923 // add a check.
Andreas Gampee2abbc62017-09-15 11:59:26 -0700924 if (work_insn_idx_ < dex::kDexNoIndex) {
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -0800925 const Instruction& inst = code_item_accessor_.InstructionAt(work_insn_idx_);
Mathieu Chartier1d2d4ff2017-09-23 16:11:06 -0700926 int opcode_flags = Instruction::FlagsOf(inst.Opcode());
Andreas Gamped7f8d052015-03-12 11:05:47 -0700927
Andreas Gamped5ad72f2015-06-26 17:33:47 -0700928 if ((opcode_flags & Instruction::kThrow) == 0 && CurrentInsnFlags()->IsInTry()) {
929 saved_line_->CopyFromLine(work_line_.get());
930 }
Andreas Gamped7f8d052015-03-12 11:05:47 -0700931 }
jeffhaofaf459e2012-08-31 15:32:47 -0700932 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700933 break;
Andreas Gampea727e372015-08-25 09:22:37 -0700934
Ian Rogersad0b3a32012-04-16 14:50:24 -0700935 // Indication that verification should be retried at runtime.
936 case VERIFY_ERROR_BAD_CLASS_SOFT:
Jeff Haoee988952013-04-16 14:23:47 -0700937 if (!allow_soft_failures_) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700938 have_pending_hard_failure_ = true;
939 }
940 break;
Andreas Gampea727e372015-08-25 09:22:37 -0700941
jeffhaod5347e02012-03-22 17:25:05 -0700942 // Hard verification failures at compile time will still fail at runtime, so the class is
943 // marked as rejected to prevent it from being compiled.
Ian Rogersad0b3a32012-04-16 14:50:24 -0700944 case VERIFY_ERROR_BAD_CLASS_HARD: {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700945 have_pending_hard_failure_ = true;
Andreas Gampeebf850c2015-08-14 15:37:35 -0700946 if (VLOG_IS_ON(verifier) && kDumpRegLinesOnHardFailureIfVLOG) {
947 ScopedObjectAccess soa(Thread::Current());
948 std::ostringstream oss;
949 Dump(oss);
950 LOG(ERROR) << oss.str();
951 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700952 break;
Ian Rogers47a05882012-02-03 12:23:33 -0800953 }
954 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700955 failures_.push_back(error);
David Sehr709b0702016-10-13 09:12:37 -0700956 std::string location(StringPrintf("%s: [0x%X] ", dex_file_->PrettyMethod(dex_method_idx_).c_str(),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700957 work_insn_idx_));
Elena Sayapina78480ec2014-08-15 15:52:42 +0700958 std::ostringstream* failure_message = new std::ostringstream(location, std::ostringstream::ate);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700959 failure_messages_.push_back(failure_message);
960 return *failure_message;
961}
962
Ian Rogers576ca0c2014-06-06 15:58:22 -0700963std::ostream& MethodVerifier::LogVerifyInfo() {
David Sehr709b0702016-10-13 09:12:37 -0700964 return info_messages_ << "VFY: " << dex_file_->PrettyMethod(dex_method_idx_)
Ian Rogers576ca0c2014-06-06 15:58:22 -0700965 << '[' << reinterpret_cast<void*>(work_insn_idx_) << "] : ";
966}
967
Ian Rogersad0b3a32012-04-16 14:50:24 -0700968void MethodVerifier::PrependToLastFailMessage(std::string prepend) {
969 size_t failure_num = failure_messages_.size();
970 DCHECK_NE(failure_num, 0U);
971 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
972 prepend += last_fail_message->str();
Elena Sayapina78480ec2014-08-15 15:52:42 +0700973 failure_messages_[failure_num - 1] = new std::ostringstream(prepend, std::ostringstream::ate);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700974 delete last_fail_message;
975}
976
Vladimir Marko5c657fe2016-11-03 15:12:29 +0000977void MethodVerifier::AppendToLastFailMessage(const std::string& append) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700978 size_t failure_num = failure_messages_.size();
979 DCHECK_NE(failure_num, 0U);
980 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
981 (*last_fail_message) << append;
Ian Rogers47a05882012-02-03 12:23:33 -0800982}
983
Ian Rogers776ac1f2012-04-13 23:36:36 -0700984bool MethodVerifier::ComputeWidthsAndCountOps() {
jeffhaobdb76512011-09-07 11:43:16 -0700985 size_t new_instance_count = 0;
986 size_t monitor_enter_count = 0;
jeffhaobdb76512011-09-07 11:43:16 -0700987
Mathieu Chartieraf7c9022017-10-27 09:42:46 -0700988 // We can't assume the instruction is well formed, handle the case where calculating the size
989 // goes past the end of the code item.
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -0800990 SafeDexInstructionIterator it(code_item_accessor_.begin(), code_item_accessor_.end());
991 for ( ; !it.IsErrorState() && it < code_item_accessor_.end(); ++it) {
Mathieu Chartieraf7c9022017-10-27 09:42:46 -0700992 // In case the instruction goes past the end of the code item, make sure to not process it.
993 SafeDexInstructionIterator next = it;
994 ++next;
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -0800995 if (next.IsErrorState()) {
Mathieu Chartieraf7c9022017-10-27 09:42:46 -0700996 break;
997 }
998 Instruction::Code opcode = it->Opcode();
Ian Rogersa9a82542013-10-04 11:17:26 -0700999 switch (opcode) {
1000 case Instruction::APUT_OBJECT:
1001 case Instruction::CHECK_CAST:
1002 has_check_casts_ = true;
1003 break;
1004 case Instruction::INVOKE_VIRTUAL:
1005 case Instruction::INVOKE_VIRTUAL_RANGE:
1006 case Instruction::INVOKE_INTERFACE:
1007 case Instruction::INVOKE_INTERFACE_RANGE:
1008 has_virtual_or_interface_invokes_ = true;
1009 break;
1010 case Instruction::MONITOR_ENTER:
1011 monitor_enter_count++;
1012 break;
1013 case Instruction::NEW_INSTANCE:
1014 new_instance_count++;
1015 break;
1016 default:
1017 break;
jeffhaobdb76512011-09-07 11:43:16 -07001018 }
Mathieu Chartier2b2bef22017-10-26 17:10:19 -07001019 GetInstructionFlags(it.DexPc()).SetIsOpcode();
jeffhaobdb76512011-09-07 11:43:16 -07001020 }
1021
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001022 if (it != code_item_accessor_.end()) {
1023 const size_t insns_size = code_item_accessor_.InsnsSizeInCodeUnits();
jeffhaod5347e02012-03-22 17:25:05 -07001024 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "code did not end where expected ("
Mathieu Chartier2b2bef22017-10-26 17:10:19 -07001025 << it.DexPc() << " vs. " << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -07001026 return false;
1027 }
1028
Ian Rogersd81871c2011-10-03 13:57:23 -07001029 new_instance_count_ = new_instance_count;
1030 monitor_enter_count_ = monitor_enter_count;
jeffhaobdb76512011-09-07 11:43:16 -07001031 return true;
1032}
1033
Ian Rogers776ac1f2012-04-13 23:36:36 -07001034bool MethodVerifier::ScanTryCatchBlocks() {
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001035 const uint32_t tries_size = code_item_accessor_.TriesSize();
jeffhaobdb76512011-09-07 11:43:16 -07001036 if (tries_size == 0) {
1037 return true;
1038 }
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001039 const uint32_t insns_size = code_item_accessor_.InsnsSizeInCodeUnits();
1040 for (const DexFile::TryItem& try_item : code_item_accessor_.TryItems()) {
1041 const uint32_t start = try_item.start_addr_;
1042 const uint32_t end = start + try_item.insn_count_;
jeffhaobdb76512011-09-07 11:43:16 -07001043 if ((start >= end) || (start >= insns_size) || (end > insns_size)) {
jeffhaod5347e02012-03-22 17:25:05 -07001044 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad exception entry: startAddr=" << start
1045 << " endAddr=" << end << " (size=" << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -07001046 return false;
1047 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07001048 if (!GetInstructionFlags(start).IsOpcode()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001049 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1050 << "'try' block starts inside an instruction (" << start << ")";
jeffhaobdb76512011-09-07 11:43:16 -07001051 return false;
1052 }
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001053 DexInstructionIterator end_it(code_item_accessor_.Insns(), end);
1054 for (DexInstructionIterator it(code_item_accessor_.Insns(), start); it < end_it; ++it) {
1055 GetInstructionFlags(it.DexPc()).SetInTry();
jeffhaobdb76512011-09-07 11:43:16 -07001056 }
1057 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -08001058 // Iterate over each of the handlers to verify target addresses.
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001059 const uint8_t* handlers_ptr = code_item_accessor_.GetCatchHandlerData();
1060 const uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
Ian Rogers28ad40d2011-10-27 15:19:26 -07001061 ClassLinker* linker = Runtime::Current()->GetClassLinker();
jeffhaobdb76512011-09-07 11:43:16 -07001062 for (uint32_t idx = 0; idx < handlers_size; idx++) {
Ian Rogers0571d352011-11-03 19:51:38 -07001063 CatchHandlerIterator iterator(handlers_ptr);
1064 for (; iterator.HasNext(); iterator.Next()) {
Igor Murashkin2ffb7032017-11-08 13:35:21 -08001065 uint32_t dex_pc = iterator.GetHandlerAddress();
Mathieu Chartierde40d472015-10-15 17:47:48 -07001066 if (!GetInstructionFlags(dex_pc).IsOpcode()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001067 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1068 << "exception handler starts at bad address (" << dex_pc << ")";
jeffhaobdb76512011-09-07 11:43:16 -07001069 return false;
1070 }
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001071 if (!CheckNotMoveResult(code_item_accessor_.Insns(), dex_pc)) {
Stephen Kyle9bc61992014-09-22 13:53:15 +01001072 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1073 << "exception handler begins with move-result* (" << dex_pc << ")";
1074 return false;
1075 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07001076 GetInstructionFlags(dex_pc).SetBranchTarget();
Ian Rogers28ad40d2011-10-27 15:19:26 -07001077 // Ensure exception types are resolved so that they don't need resolution to be delivered,
1078 // unresolved exception types will be ignored by exception delivery
Andreas Gampea5b09a62016-11-17 15:21:22 -08001079 if (iterator.GetHandlerTypeIndex().IsValid()) {
Vladimir Marko666ee3d2017-12-11 18:37:36 +00001080 ObjPtr<mirror::Class> exception_type =
1081 linker->ResolveType(iterator.GetHandlerTypeIndex(), dex_cache_, class_loader_);
Ian Rogers7b078e82014-09-10 14:44:24 -07001082 if (exception_type == nullptr) {
1083 DCHECK(self_->IsExceptionPending());
1084 self_->ClearException();
Ian Rogers28ad40d2011-10-27 15:19:26 -07001085 }
1086 }
jeffhaobdb76512011-09-07 11:43:16 -07001087 }
Ian Rogers0571d352011-11-03 19:51:38 -07001088 handlers_ptr = iterator.EndDataPointer();
jeffhaobdb76512011-09-07 11:43:16 -07001089 }
jeffhaobdb76512011-09-07 11:43:16 -07001090 return true;
1091}
1092
Andreas Gampebf1cb772017-05-15 15:39:00 -07001093template <bool kAllowRuntimeOnlyInstructions>
Ian Rogers776ac1f2012-04-13 23:36:36 -07001094bool MethodVerifier::VerifyInstructions() {
Ian Rogers0c7abda2012-09-19 13:33:42 -07001095 /* 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 -07001096 GetInstructionFlags(0).SetBranchTarget();
1097 GetInstructionFlags(0).SetCompileTimeInfoPoint();
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001098 for (const DexInstructionPcPair& inst : code_item_accessor_) {
Mathieu Chartier2b2bef22017-10-26 17:10:19 -07001099 const uint32_t dex_pc = inst.DexPc();
1100 if (!VerifyInstruction<kAllowRuntimeOnlyInstructions>(&inst.Inst(), dex_pc)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001101 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -07001102 return false;
1103 }
1104 /* Flag instructions that are garbage collection points */
Sameer Abu Asal02c42232013-04-30 12:09:45 -07001105 // All invoke points are marked as "Throw" points already.
1106 // We are relying on this to also count all the invokes as interesting.
Vladimir Marko8b858e12014-11-27 14:52:37 +00001107 if (inst->IsBranch()) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07001108 GetInstructionFlags(dex_pc).SetCompileTimeInfoPoint();
Vladimir Marko8b858e12014-11-27 14:52:37 +00001109 // The compiler also needs safepoints for fall-through to loop heads.
1110 // Such a loop head must be a target of a branch.
1111 int32_t offset = 0;
1112 bool cond, self_ok;
1113 bool target_ok = GetBranchOffset(dex_pc, &offset, &cond, &self_ok);
1114 DCHECK(target_ok);
Mathieu Chartierde40d472015-10-15 17:47:48 -07001115 GetInstructionFlags(dex_pc + offset).SetCompileTimeInfoPoint();
Vladimir Marko8b858e12014-11-27 14:52:37 +00001116 } else if (inst->IsSwitch() || inst->IsThrow()) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07001117 GetInstructionFlags(dex_pc).SetCompileTimeInfoPoint();
Ian Rogersb8c78592013-07-25 23:52:52 +00001118 } else if (inst->IsReturn()) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07001119 GetInstructionFlags(dex_pc).SetCompileTimeInfoPointAndReturn();
Ian Rogersd81871c2011-10-03 13:57:23 -07001120 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001121 }
1122 return true;
1123}
1124
Andreas Gampebf1cb772017-05-15 15:39:00 -07001125template <bool kAllowRuntimeOnlyInstructions>
1126bool MethodVerifier::VerifyInstruction(const Instruction* inst, uint32_t code_offset) {
Andreas Gampee05cc662017-05-15 10:17:30 -07001127 if (Instruction::kHaveExperimentalInstructions && UNLIKELY(inst->IsExperimental())) {
Igor Murashkin4d7b75f2015-07-21 17:03:36 -07001128 // Experimental instructions don't yet have verifier support implementation.
1129 // While it is possible to use them by themselves, when we try to use stable instructions
1130 // with a virtual register that was created by an experimental instruction,
1131 // the data flow analysis will fail.
1132 Fail(VERIFY_ERROR_FORCE_INTERPRETER)
1133 << "experimental instruction is not supported by verifier; skipping verification";
1134 have_pending_experimental_failure_ = true;
1135 return false;
1136 }
1137
Ian Rogersd81871c2011-10-03 13:57:23 -07001138 bool result = true;
1139 switch (inst->GetVerifyTypeArgumentA()) {
1140 case Instruction::kVerifyRegA:
Ian Rogers29a26482014-05-02 15:27:29 -07001141 result = result && CheckRegisterIndex(inst->VRegA());
Ian Rogersd81871c2011-10-03 13:57:23 -07001142 break;
1143 case Instruction::kVerifyRegAWide:
Ian Rogers29a26482014-05-02 15:27:29 -07001144 result = result && CheckWideRegisterIndex(inst->VRegA());
Ian Rogersd81871c2011-10-03 13:57:23 -07001145 break;
1146 }
1147 switch (inst->GetVerifyTypeArgumentB()) {
1148 case Instruction::kVerifyRegB:
Ian Rogers29a26482014-05-02 15:27:29 -07001149 result = result && CheckRegisterIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -07001150 break;
1151 case Instruction::kVerifyRegBField:
Ian Rogers29a26482014-05-02 15:27:29 -07001152 result = result && CheckFieldIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -07001153 break;
1154 case Instruction::kVerifyRegBMethod:
Ian Rogers29a26482014-05-02 15:27:29 -07001155 result = result && CheckMethodIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -07001156 break;
1157 case Instruction::kVerifyRegBNewInstance:
Andreas Gampea5b09a62016-11-17 15:21:22 -08001158 result = result && CheckNewInstance(dex::TypeIndex(inst->VRegB()));
Ian Rogersd81871c2011-10-03 13:57:23 -07001159 break;
1160 case Instruction::kVerifyRegBString:
Ian Rogers29a26482014-05-02 15:27:29 -07001161 result = result && CheckStringIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -07001162 break;
1163 case Instruction::kVerifyRegBType:
Andreas Gampea5b09a62016-11-17 15:21:22 -08001164 result = result && CheckTypeIndex(dex::TypeIndex(inst->VRegB()));
Ian Rogersd81871c2011-10-03 13:57:23 -07001165 break;
1166 case Instruction::kVerifyRegBWide:
Ian Rogers29a26482014-05-02 15:27:29 -07001167 result = result && CheckWideRegisterIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -07001168 break;
Orion Hodson2e599942017-09-22 16:17:41 +01001169 case Instruction::kVerifyRegBCallSite:
1170 result = result && CheckCallSiteIndex(inst->VRegB());
1171 break;
1172 case Instruction::kVerifyRegBMethodHandle:
1173 result = result && CheckMethodHandleIndex(inst->VRegB());
1174 break;
1175 case Instruction::kVerifyRegBPrototype:
1176 result = result && CheckPrototypeIndex(inst->VRegB());
1177 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001178 }
1179 switch (inst->GetVerifyTypeArgumentC()) {
1180 case Instruction::kVerifyRegC:
Ian Rogers29a26482014-05-02 15:27:29 -07001181 result = result && CheckRegisterIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -07001182 break;
1183 case Instruction::kVerifyRegCField:
Ian Rogers29a26482014-05-02 15:27:29 -07001184 result = result && CheckFieldIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -07001185 break;
1186 case Instruction::kVerifyRegCNewArray:
Andreas Gampea5b09a62016-11-17 15:21:22 -08001187 result = result && CheckNewArray(dex::TypeIndex(inst->VRegC()));
Ian Rogersd81871c2011-10-03 13:57:23 -07001188 break;
1189 case Instruction::kVerifyRegCType:
Andreas Gampea5b09a62016-11-17 15:21:22 -08001190 result = result && CheckTypeIndex(dex::TypeIndex(inst->VRegC()));
Ian Rogersd81871c2011-10-03 13:57:23 -07001191 break;
1192 case Instruction::kVerifyRegCWide:
Ian Rogers29a26482014-05-02 15:27:29 -07001193 result = result && CheckWideRegisterIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -07001194 break;
1195 }
Orion Hodsoncfa325e2016-10-13 10:25:54 +01001196 switch (inst->GetVerifyTypeArgumentH()) {
1197 case Instruction::kVerifyRegHPrototype:
1198 result = result && CheckPrototypeIndex(inst->VRegH());
1199 break;
1200 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001201 switch (inst->GetVerifyExtraFlags()) {
1202 case Instruction::kVerifyArrayData:
1203 result = result && CheckArrayData(code_offset);
1204 break;
1205 case Instruction::kVerifyBranchTarget:
1206 result = result && CheckBranchTarget(code_offset);
1207 break;
1208 case Instruction::kVerifySwitchTargets:
1209 result = result && CheckSwitchTargets(code_offset);
1210 break;
Andreas Gampec3314312014-06-19 18:13:29 -07001211 case Instruction::kVerifyVarArgNonZero:
1212 // Fall-through.
Ian Rogers29a26482014-05-02 15:27:29 -07001213 case Instruction::kVerifyVarArg: {
Taiju Tsuiki29498a22015-04-13 14:21:00 +09001214 // Instructions that can actually return a negative value shouldn't have this flag.
1215 uint32_t v_a = dchecked_integral_cast<uint32_t>(inst->VRegA());
1216 if ((inst->GetVerifyExtraFlags() == Instruction::kVerifyVarArgNonZero && v_a == 0) ||
1217 v_a > Instruction::kMaxVarArgRegs) {
1218 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << v_a << ") in "
Andreas Gampec3314312014-06-19 18:13:29 -07001219 "non-range invoke";
1220 return false;
1221 }
Taiju Tsuiki29498a22015-04-13 14:21:00 +09001222
Ian Rogers29a26482014-05-02 15:27:29 -07001223 uint32_t args[Instruction::kMaxVarArgRegs];
1224 inst->GetVarArgs(args);
Taiju Tsuiki29498a22015-04-13 14:21:00 +09001225 result = result && CheckVarArgRegs(v_a, args);
Ian Rogersd81871c2011-10-03 13:57:23 -07001226 break;
Ian Rogers29a26482014-05-02 15:27:29 -07001227 }
Andreas Gampec3314312014-06-19 18:13:29 -07001228 case Instruction::kVerifyVarArgRangeNonZero:
1229 // Fall-through.
Ian Rogersd81871c2011-10-03 13:57:23 -07001230 case Instruction::kVerifyVarArgRange:
Andreas Gampec3314312014-06-19 18:13:29 -07001231 if (inst->GetVerifyExtraFlags() == Instruction::kVerifyVarArgRangeNonZero &&
1232 inst->VRegA() <= 0) {
1233 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << inst->VRegA() << ") in "
1234 "range invoke";
1235 return false;
1236 }
Ian Rogers29a26482014-05-02 15:27:29 -07001237 result = result && CheckVarArgRangeRegs(inst->VRegA(), inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -07001238 break;
1239 case Instruction::kVerifyError:
jeffhaod5347e02012-03-22 17:25:05 -07001240 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected opcode " << inst->Name();
Ian Rogersd81871c2011-10-03 13:57:23 -07001241 result = false;
1242 break;
1243 }
Andreas Gampebf1cb772017-05-15 15:39:00 -07001244 if (!kAllowRuntimeOnlyInstructions && inst->GetVerifyIsRuntimeOnly()) {
Ian Rogers5fb22a92014-06-13 10:31:28 -07001245 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "opcode only expected at runtime " << inst->Name();
1246 result = false;
1247 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001248 return result;
1249}
1250
Ian Rogers7b078e82014-09-10 14:44:24 -07001251inline bool MethodVerifier::CheckRegisterIndex(uint32_t idx) {
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001252 if (UNLIKELY(idx >= code_item_accessor_.RegistersSize())) {
jeffhaod5347e02012-03-22 17:25:05 -07001253 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register index out of range (" << idx << " >= "
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001254 << code_item_accessor_.RegistersSize() << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001255 return false;
1256 }
1257 return true;
1258}
1259
Ian Rogers7b078e82014-09-10 14:44:24 -07001260inline bool MethodVerifier::CheckWideRegisterIndex(uint32_t idx) {
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001261 if (UNLIKELY(idx + 1 >= code_item_accessor_.RegistersSize())) {
jeffhaod5347e02012-03-22 17:25:05 -07001262 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "wide register index out of range (" << idx
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001263 << "+1 >= " << code_item_accessor_.RegistersSize() << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001264 return false;
1265 }
1266 return true;
1267}
1268
Orion Hodson2e599942017-09-22 16:17:41 +01001269inline bool MethodVerifier::CheckCallSiteIndex(uint32_t idx) {
1270 uint32_t limit = dex_file_->NumCallSiteIds();
1271 if (UNLIKELY(idx >= limit)) {
1272 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad call site index " << idx << " (max "
1273 << limit << ")";
1274 return false;
1275 }
1276 return true;
1277}
1278
Ian Rogers7b078e82014-09-10 14:44:24 -07001279inline bool MethodVerifier::CheckFieldIndex(uint32_t idx) {
Andreas Gampe29e81422017-05-15 16:29:32 -07001280 if (UNLIKELY(idx >= dex_file_->GetHeader().field_ids_size_)) {
jeffhaod5347e02012-03-22 17:25:05 -07001281 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad field index " << idx << " (max "
1282 << dex_file_->GetHeader().field_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001283 return false;
1284 }
1285 return true;
1286}
1287
Ian Rogers7b078e82014-09-10 14:44:24 -07001288inline bool MethodVerifier::CheckMethodIndex(uint32_t idx) {
Andreas Gampe29e81422017-05-15 16:29:32 -07001289 if (UNLIKELY(idx >= dex_file_->GetHeader().method_ids_size_)) {
jeffhaod5347e02012-03-22 17:25:05 -07001290 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad method index " << idx << " (max "
1291 << dex_file_->GetHeader().method_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001292 return false;
1293 }
1294 return true;
1295}
1296
Orion Hodson2e599942017-09-22 16:17:41 +01001297inline bool MethodVerifier::CheckMethodHandleIndex(uint32_t idx) {
1298 uint32_t limit = dex_file_->NumMethodHandles();
1299 if (UNLIKELY(idx >= limit)) {
1300 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad method handle index " << idx << " (max "
1301 << limit << ")";
1302 return false;
1303 }
1304 return true;
1305}
1306
Andreas Gampea5b09a62016-11-17 15:21:22 -08001307inline bool MethodVerifier::CheckNewInstance(dex::TypeIndex idx) {
Andreas Gampe29e81422017-05-15 16:29:32 -07001308 if (UNLIKELY(idx.index_ >= dex_file_->GetHeader().type_ids_size_)) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08001309 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx.index_ << " (max "
jeffhaod5347e02012-03-22 17:25:05 -07001310 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001311 return false;
1312 }
1313 // We don't need the actual class, just a pointer to the class name.
Ian Rogers0571d352011-11-03 19:51:38 -07001314 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Andreas Gampe29e81422017-05-15 16:29:32 -07001315 if (UNLIKELY(descriptor[0] != 'L')) {
jeffhaod5347e02012-03-22 17:25:05 -07001316 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "can't call new-instance on type '" << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001317 return false;
Andreas Gampe29e81422017-05-15 16:29:32 -07001318 } else if (UNLIKELY(strcmp(descriptor, "Ljava/lang/Class;") == 0)) {
Aart Bikdb698f12016-07-25 17:52:22 -07001319 // An unlikely new instance on Class is not allowed. Fall back to interpreter to ensure an
1320 // exception is thrown when this statement is executed (compiled code would not do that).
1321 Fail(VERIFY_ERROR_INSTANTIATION);
Ian Rogersd81871c2011-10-03 13:57:23 -07001322 }
1323 return true;
1324}
1325
Orion Hodsoncfa325e2016-10-13 10:25:54 +01001326inline bool MethodVerifier::CheckPrototypeIndex(uint32_t idx) {
Andreas Gampe29e81422017-05-15 16:29:32 -07001327 if (UNLIKELY(idx >= dex_file_->GetHeader().proto_ids_size_)) {
Orion Hodsoncfa325e2016-10-13 10:25:54 +01001328 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad prototype index " << idx << " (max "
1329 << dex_file_->GetHeader().proto_ids_size_ << ")";
1330 return false;
1331 }
1332 return true;
1333}
1334
Ian Rogers7b078e82014-09-10 14:44:24 -07001335inline bool MethodVerifier::CheckStringIndex(uint32_t idx) {
Andreas Gampe29e81422017-05-15 16:29:32 -07001336 if (UNLIKELY(idx >= dex_file_->GetHeader().string_ids_size_)) {
jeffhaod5347e02012-03-22 17:25:05 -07001337 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad string index " << idx << " (max "
1338 << dex_file_->GetHeader().string_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001339 return false;
1340 }
1341 return true;
1342}
1343
Andreas Gampea5b09a62016-11-17 15:21:22 -08001344inline bool MethodVerifier::CheckTypeIndex(dex::TypeIndex idx) {
Andreas Gampe29e81422017-05-15 16:29:32 -07001345 if (UNLIKELY(idx.index_ >= dex_file_->GetHeader().type_ids_size_)) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08001346 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx.index_ << " (max "
jeffhaod5347e02012-03-22 17:25:05 -07001347 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001348 return false;
1349 }
1350 return true;
1351}
1352
Andreas Gampea5b09a62016-11-17 15:21:22 -08001353bool MethodVerifier::CheckNewArray(dex::TypeIndex idx) {
Andreas Gampe29e81422017-05-15 16:29:32 -07001354 if (UNLIKELY(idx.index_ >= dex_file_->GetHeader().type_ids_size_)) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08001355 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx.index_ << " (max "
jeffhaod5347e02012-03-22 17:25:05 -07001356 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001357 return false;
1358 }
1359 int bracket_count = 0;
Ian Rogers0571d352011-11-03 19:51:38 -07001360 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -07001361 const char* cp = descriptor;
1362 while (*cp++ == '[') {
1363 bracket_count++;
1364 }
Andreas Gampe29e81422017-05-15 16:29:32 -07001365 if (UNLIKELY(bracket_count == 0)) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001366 /* The given class must be an array type. */
Brian Carlstrom93c33962013-07-26 10:37:43 -07001367 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1368 << "can't new-array class '" << descriptor << "' (not an array)";
Ian Rogersd81871c2011-10-03 13:57:23 -07001369 return false;
Andreas Gampe29e81422017-05-15 16:29:32 -07001370 } else if (UNLIKELY(bracket_count > 255)) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001371 /* It is illegal to create an array of more than 255 dimensions. */
Brian Carlstrom93c33962013-07-26 10:37:43 -07001372 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1373 << "can't new-array class '" << descriptor << "' (exceeds limit)";
Ian Rogersd81871c2011-10-03 13:57:23 -07001374 return false;
1375 }
1376 return true;
1377}
1378
Ian Rogers776ac1f2012-04-13 23:36:36 -07001379bool MethodVerifier::CheckArrayData(uint32_t cur_offset) {
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001380 const uint32_t insn_count = code_item_accessor_.InsnsSizeInCodeUnits();
1381 const uint16_t* insns = code_item_accessor_.Insns() + cur_offset;
Ian Rogersd81871c2011-10-03 13:57:23 -07001382 const uint16_t* array_data;
1383 int32_t array_data_offset;
1384
1385 DCHECK_LT(cur_offset, insn_count);
1386 /* make sure the start of the array data table is in range */
Andreas Gampe53de99c2015-08-17 13:43:55 -07001387 array_data_offset = insns[1] | (static_cast<int32_t>(insns[2]) << 16);
Andreas Gampe29e81422017-05-15 16:29:32 -07001388 if (UNLIKELY(static_cast<int32_t>(cur_offset) + array_data_offset < 0 ||
1389 cur_offset + array_data_offset + 2 >= insn_count)) {
jeffhaod5347e02012-03-22 17:25:05 -07001390 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data start: at " << cur_offset
Brian Carlstrom93c33962013-07-26 10:37:43 -07001391 << ", data offset " << array_data_offset
1392 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -07001393 return false;
1394 }
1395 /* offset to array data table is a relative branch-style offset */
1396 array_data = insns + array_data_offset;
Andreas Gampe57c47582015-07-01 22:05:59 -07001397 // Make sure the table is at an even dex pc, that is, 32-bit aligned.
Andreas Gampe29e81422017-05-15 16:29:32 -07001398 if (UNLIKELY(!IsAligned<4>(array_data))) {
jeffhaod5347e02012-03-22 17:25:05 -07001399 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned array data table: at " << cur_offset
1400 << ", data offset " << array_data_offset;
Ian Rogersd81871c2011-10-03 13:57:23 -07001401 return false;
1402 }
Andreas Gampe57c47582015-07-01 22:05:59 -07001403 // Make sure the array-data is marked as an opcode. This ensures that it was reached when
1404 // traversing the code item linearly. It is an approximation for a by-spec padding value.
Andreas Gampe29e81422017-05-15 16:29:32 -07001405 if (UNLIKELY(!GetInstructionFlags(cur_offset + array_data_offset).IsOpcode())) {
Andreas Gampe57c47582015-07-01 22:05:59 -07001406 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array data table at " << cur_offset
1407 << ", data offset " << array_data_offset
1408 << " not correctly visited, probably bad padding.";
1409 return false;
1410 }
1411
Ian Rogersd81871c2011-10-03 13:57:23 -07001412 uint32_t value_width = array_data[1];
Elliott Hughes398f64b2012-03-26 18:05:48 -07001413 uint32_t value_count = *reinterpret_cast<const uint32_t*>(&array_data[2]);
Ian Rogersd81871c2011-10-03 13:57:23 -07001414 uint32_t table_size = 4 + (value_width * value_count + 1) / 2;
1415 /* make sure the end of the switch is in range */
Andreas Gampe29e81422017-05-15 16:29:32 -07001416 if (UNLIKELY(cur_offset + array_data_offset + table_size > insn_count)) {
jeffhaod5347e02012-03-22 17:25:05 -07001417 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data end: at " << cur_offset
1418 << ", data offset " << array_data_offset << ", end "
1419 << cur_offset + array_data_offset + table_size
1420 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -07001421 return false;
1422 }
1423 return true;
1424}
1425
Ian Rogers776ac1f2012-04-13 23:36:36 -07001426bool MethodVerifier::CheckBranchTarget(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001427 int32_t offset;
1428 bool isConditional, selfOkay;
1429 if (!GetBranchOffset(cur_offset, &offset, &isConditional, &selfOkay)) {
1430 return false;
1431 }
Andreas Gampe29e81422017-05-15 16:29:32 -07001432 if (UNLIKELY(!selfOkay && offset == 0)) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001433 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch offset of zero not allowed at"
1434 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -07001435 return false;
1436 }
Elliott Hughes81ff3182012-03-23 20:35:56 -07001437 // Check for 32-bit overflow. This isn't strictly necessary if we can depend on the runtime
1438 // to have identical "wrap-around" behavior, but it's unwise to depend on that.
Andreas Gampe29e81422017-05-15 16:29:32 -07001439 if (UNLIKELY(((int64_t) cur_offset + (int64_t) offset) != (int64_t) (cur_offset + offset))) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001440 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch target overflow "
1441 << reinterpret_cast<void*>(cur_offset) << " +" << offset;
Ian Rogersd81871c2011-10-03 13:57:23 -07001442 return false;
1443 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001444 int32_t abs_offset = cur_offset + offset;
Andreas Gampe29e81422017-05-15 16:29:32 -07001445 if (UNLIKELY(abs_offset < 0 ||
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001446 (uint32_t) abs_offset >= code_item_accessor_.InsnsSizeInCodeUnits() ||
Andreas Gampe29e81422017-05-15 16:29:32 -07001447 !GetInstructionFlags(abs_offset).IsOpcode())) {
jeffhaod5347e02012-03-22 17:25:05 -07001448 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid branch target " << offset << " (-> "
Elliott Hughes398f64b2012-03-26 18:05:48 -07001449 << reinterpret_cast<void*>(abs_offset) << ") at "
1450 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -07001451 return false;
1452 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07001453 GetInstructionFlags(abs_offset).SetBranchTarget();
Ian Rogersd81871c2011-10-03 13:57:23 -07001454 return true;
1455}
1456
Ian Rogers776ac1f2012-04-13 23:36:36 -07001457bool MethodVerifier::GetBranchOffset(uint32_t cur_offset, int32_t* pOffset, bool* pConditional,
Ian Rogersd81871c2011-10-03 13:57:23 -07001458 bool* selfOkay) {
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001459 const uint16_t* insns = code_item_accessor_.Insns() + cur_offset;
Ian Rogersd81871c2011-10-03 13:57:23 -07001460 *pConditional = false;
1461 *selfOkay = false;
jeffhaoba5ebb92011-08-25 17:24:37 -07001462 switch (*insns & 0xff) {
1463 case Instruction::GOTO:
1464 *pOffset = ((int16_t) *insns) >> 8;
jeffhaoba5ebb92011-08-25 17:24:37 -07001465 break;
1466 case Instruction::GOTO_32:
1467 *pOffset = insns[1] | (((uint32_t) insns[2]) << 16);
jeffhaoba5ebb92011-08-25 17:24:37 -07001468 *selfOkay = true;
1469 break;
1470 case Instruction::GOTO_16:
1471 *pOffset = (int16_t) insns[1];
jeffhaoba5ebb92011-08-25 17:24:37 -07001472 break;
1473 case Instruction::IF_EQ:
1474 case Instruction::IF_NE:
1475 case Instruction::IF_LT:
1476 case Instruction::IF_GE:
1477 case Instruction::IF_GT:
1478 case Instruction::IF_LE:
1479 case Instruction::IF_EQZ:
1480 case Instruction::IF_NEZ:
1481 case Instruction::IF_LTZ:
1482 case Instruction::IF_GEZ:
1483 case Instruction::IF_GTZ:
1484 case Instruction::IF_LEZ:
1485 *pOffset = (int16_t) insns[1];
1486 *pConditional = true;
jeffhaoba5ebb92011-08-25 17:24:37 -07001487 break;
1488 default:
1489 return false;
jeffhaoba5ebb92011-08-25 17:24:37 -07001490 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001491 return true;
1492}
1493
Ian Rogers776ac1f2012-04-13 23:36:36 -07001494bool MethodVerifier::CheckSwitchTargets(uint32_t cur_offset) {
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001495 const uint32_t insn_count = code_item_accessor_.InsnsSizeInCodeUnits();
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001496 DCHECK_LT(cur_offset, insn_count);
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001497 const uint16_t* insns = code_item_accessor_.Insns() + cur_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001498 /* make sure the start of the switch is in range */
Andreas Gampe53de99c2015-08-17 13:43:55 -07001499 int32_t switch_offset = insns[1] | (static_cast<int32_t>(insns[2]) << 16);
Andreas Gampe29e81422017-05-15 16:29:32 -07001500 if (UNLIKELY(static_cast<int32_t>(cur_offset) + switch_offset < 0 ||
1501 cur_offset + switch_offset + 2 > insn_count)) {
jeffhaod5347e02012-03-22 17:25:05 -07001502 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch start: at " << cur_offset
Brian Carlstrom93c33962013-07-26 10:37:43 -07001503 << ", switch offset " << switch_offset
1504 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001505 return false;
1506 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001507 /* offset to switch table is a relative branch-style offset */
Ian Rogersd81871c2011-10-03 13:57:23 -07001508 const uint16_t* switch_insns = insns + switch_offset;
Andreas Gampe57c47582015-07-01 22:05:59 -07001509 // Make sure the table is at an even dex pc, that is, 32-bit aligned.
Andreas Gampe29e81422017-05-15 16:29:32 -07001510 if (UNLIKELY(!IsAligned<4>(switch_insns))) {
jeffhaod5347e02012-03-22 17:25:05 -07001511 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned switch table: at " << cur_offset
1512 << ", switch offset " << switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001513 return false;
1514 }
Andreas Gampe57c47582015-07-01 22:05:59 -07001515 // Make sure the switch data is marked as an opcode. This ensures that it was reached when
1516 // traversing the code item linearly. It is an approximation for a by-spec padding value.
Andreas Gampe29e81422017-05-15 16:29:32 -07001517 if (UNLIKELY(!GetInstructionFlags(cur_offset + switch_offset).IsOpcode())) {
Andreas Gampe57c47582015-07-01 22:05:59 -07001518 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "switch table at " << cur_offset
1519 << ", switch offset " << switch_offset
1520 << " not correctly visited, probably bad padding.";
1521 return false;
1522 }
1523
David Brazdil5469d342015-09-25 16:57:53 +01001524 bool is_packed_switch = (*insns & 0xff) == Instruction::PACKED_SWITCH;
1525
Ian Rogersd81871c2011-10-03 13:57:23 -07001526 uint32_t switch_count = switch_insns[1];
David Brazdil5469d342015-09-25 16:57:53 +01001527 int32_t targets_offset;
Ian Rogersd81871c2011-10-03 13:57:23 -07001528 uint16_t expected_signature;
David Brazdil5469d342015-09-25 16:57:53 +01001529 if (is_packed_switch) {
jeffhaoba5ebb92011-08-25 17:24:37 -07001530 /* 0=sig, 1=count, 2/3=firstKey */
1531 targets_offset = 4;
jeffhaoba5ebb92011-08-25 17:24:37 -07001532 expected_signature = Instruction::kPackedSwitchSignature;
1533 } else {
1534 /* 0=sig, 1=count, 2..count*2 = keys */
jeffhaoba5ebb92011-08-25 17:24:37 -07001535 targets_offset = 2 + 2 * switch_count;
1536 expected_signature = Instruction::kSparseSwitchSignature;
1537 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001538 uint32_t table_size = targets_offset + switch_count * 2;
Andreas Gampe29e81422017-05-15 16:29:32 -07001539 if (UNLIKELY(switch_insns[0] != expected_signature)) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001540 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1541 << StringPrintf("wrong signature for switch table (%x, wanted %x)",
1542 switch_insns[0], expected_signature);
jeffhaoba5ebb92011-08-25 17:24:37 -07001543 return false;
1544 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001545 /* make sure the end of the switch is in range */
Andreas Gampe29e81422017-05-15 16:29:32 -07001546 if (UNLIKELY(cur_offset + switch_offset + table_size > (uint32_t) insn_count)) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001547 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch end: at " << cur_offset
1548 << ", switch offset " << switch_offset
1549 << ", end " << (cur_offset + switch_offset + table_size)
jeffhaod5347e02012-03-22 17:25:05 -07001550 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001551 return false;
1552 }
David Brazdil5469d342015-09-25 16:57:53 +01001553
1554 constexpr int32_t keys_offset = 2;
1555 if (switch_count > 1) {
1556 if (is_packed_switch) {
1557 /* for a packed switch, verify that keys do not overflow int32 */
1558 int32_t first_key = switch_insns[keys_offset] | (switch_insns[keys_offset + 1] << 16);
1559 int32_t max_first_key =
1560 std::numeric_limits<int32_t>::max() - (static_cast<int32_t>(switch_count) - 1);
Andreas Gampe29e81422017-05-15 16:29:32 -07001561 if (UNLIKELY(first_key > max_first_key)) {
David Brazdil5469d342015-09-25 16:57:53 +01001562 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid packed switch: first_key=" << first_key
1563 << ", switch_count=" << switch_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001564 return false;
1565 }
David Brazdil5469d342015-09-25 16:57:53 +01001566 } else {
1567 /* for a sparse switch, verify the keys are in ascending order */
1568 int32_t last_key = switch_insns[keys_offset] | (switch_insns[keys_offset + 1] << 16);
1569 for (uint32_t targ = 1; targ < switch_count; targ++) {
1570 int32_t key =
1571 static_cast<int32_t>(switch_insns[keys_offset + targ * 2]) |
1572 static_cast<int32_t>(switch_insns[keys_offset + targ * 2 + 1] << 16);
Andreas Gampe29e81422017-05-15 16:29:32 -07001573 if (UNLIKELY(key <= last_key)) {
David Brazdil5469d342015-09-25 16:57:53 +01001574 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid sparse switch: last key=" << last_key
1575 << ", this=" << key;
1576 return false;
1577 }
1578 last_key = key;
1579 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001580 }
1581 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001582 /* verify each switch target */
Ian Rogersd81871c2011-10-03 13:57:23 -07001583 for (uint32_t targ = 0; targ < switch_count; targ++) {
Andreas Gampe53de99c2015-08-17 13:43:55 -07001584 int32_t offset = static_cast<int32_t>(switch_insns[targets_offset + targ * 2]) |
1585 static_cast<int32_t>(switch_insns[targets_offset + targ * 2 + 1] << 16);
Ian Rogersd81871c2011-10-03 13:57:23 -07001586 int32_t abs_offset = cur_offset + offset;
Andreas Gampe29e81422017-05-15 16:29:32 -07001587 if (UNLIKELY(abs_offset < 0 ||
1588 abs_offset >= static_cast<int32_t>(insn_count) ||
1589 !GetInstructionFlags(abs_offset).IsOpcode())) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001590 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch target " << offset
1591 << " (-> " << reinterpret_cast<void*>(abs_offset) << ") at "
1592 << reinterpret_cast<void*>(cur_offset)
1593 << "[" << targ << "]";
jeffhaoba5ebb92011-08-25 17:24:37 -07001594 return false;
1595 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07001596 GetInstructionFlags(abs_offset).SetBranchTarget();
Ian Rogersd81871c2011-10-03 13:57:23 -07001597 }
1598 return true;
1599}
1600
Ian Rogers776ac1f2012-04-13 23:36:36 -07001601bool MethodVerifier::CheckVarArgRegs(uint32_t vA, uint32_t arg[]) {
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001602 uint16_t registers_size = code_item_accessor_.RegistersSize();
Ian Rogersd81871c2011-10-03 13:57:23 -07001603 for (uint32_t idx = 0; idx < vA; idx++) {
Andreas Gampe29e81422017-05-15 16:29:32 -07001604 if (UNLIKELY(arg[idx] >= registers_size)) {
jeffhaod5347e02012-03-22 17:25:05 -07001605 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index (" << arg[idx]
1606 << ") in non-range invoke (>= " << registers_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001607 return false;
1608 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001609 }
1610
1611 return true;
1612}
1613
Ian Rogers776ac1f2012-04-13 23:36:36 -07001614bool MethodVerifier::CheckVarArgRangeRegs(uint32_t vA, uint32_t vC) {
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001615 uint16_t registers_size = code_item_accessor_.RegistersSize();
Ian Rogersd81871c2011-10-03 13:57:23 -07001616 // vA/vC are unsigned 8-bit/16-bit quantities for /range instructions, so there's no risk of
1617 // integer overflow when adding them here.
Andreas Gampe29e81422017-05-15 16:29:32 -07001618 if (UNLIKELY(vA + vC > registers_size)) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001619 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index " << vA << "+" << vC
1620 << " in range invoke (> " << registers_size << ")";
jeffhaoba5ebb92011-08-25 17:24:37 -07001621 return false;
1622 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001623 return true;
1624}
1625
Ian Rogers776ac1f2012-04-13 23:36:36 -07001626bool MethodVerifier::VerifyCodeFlow() {
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001627 const uint16_t registers_size = code_item_accessor_.RegistersSize();
jeffhaobdb76512011-09-07 11:43:16 -07001628
Ian Rogersd81871c2011-10-03 13:57:23 -07001629 /* Create and initialize table holding register status */
Brian Carlstrom93c33962013-07-26 10:37:43 -07001630 reg_table_.Init(kTrackCompilerInterestPoints,
1631 insn_flags_.get(),
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001632 code_item_accessor_.InsnsSizeInCodeUnits(),
Brian Carlstrom93c33962013-07-26 10:37:43 -07001633 registers_size,
1634 this);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07001635
Ian Rogersd0fbd852013-09-24 18:17:04 -07001636 work_line_.reset(RegisterLine::Create(registers_size, this));
1637 saved_line_.reset(RegisterLine::Create(registers_size, this));
jeffhaobdb76512011-09-07 11:43:16 -07001638
Ian Rogersd81871c2011-10-03 13:57:23 -07001639 /* Initialize register types of method arguments. */
1640 if (!SetTypesFromSignature()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001641 DCHECK_NE(failures_.size(), 0U);
1642 std::string prepend("Bad signature in ");
David Sehr709b0702016-10-13 09:12:37 -07001643 prepend += dex_file_->PrettyMethod(dex_method_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001644 PrependToLastFailMessage(prepend);
Ian Rogersd81871c2011-10-03 13:57:23 -07001645 return false;
1646 }
Andreas Gamped5ad72f2015-06-26 17:33:47 -07001647 // We may have a runtime failure here, clear.
1648 have_pending_runtime_throw_failure_ = false;
1649
Ian Rogersd81871c2011-10-03 13:57:23 -07001650 /* Perform code flow verification. */
1651 if (!CodeFlowVerifyMethod()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001652 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -07001653 return false;
jeffhaobdb76512011-09-07 11:43:16 -07001654 }
jeffhaobdb76512011-09-07 11:43:16 -07001655 return true;
1656}
1657
Ian Rogersad0b3a32012-04-16 14:50:24 -07001658std::ostream& MethodVerifier::DumpFailures(std::ostream& os) {
1659 DCHECK_EQ(failures_.size(), failure_messages_.size());
Jeff Hao4137f482013-11-22 11:44:57 -08001660 for (size_t i = 0; i < failures_.size(); ++i) {
1661 os << failure_messages_[i]->str() << "\n";
Ian Rogersad0b3a32012-04-16 14:50:24 -07001662 }
1663 return os;
1664}
1665
Ian Rogers776ac1f2012-04-13 23:36:36 -07001666void MethodVerifier::Dump(std::ostream& os) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001667 VariableIndentationOutputStream vios(&os);
1668 Dump(&vios);
1669}
1670
1671void MethodVerifier::Dump(VariableIndentationOutputStream* vios) {
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001672 if (!code_item_accessor_.HasCodeItem()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001673 vios->Stream() << "Native method\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001674 return;
jeffhaobdb76512011-09-07 11:43:16 -07001675 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001676 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001677 vios->Stream() << "Register Types:\n";
1678 ScopedIndentation indent1(vios);
1679 reg_types_.Dump(vios->Stream());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001680 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001681 vios->Stream() << "Dumping instructions and register lines:\n";
1682 ScopedIndentation indent1(vios);
Mathieu Chartier1d2d4ff2017-09-23 16:11:06 -07001683
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001684 for (const DexInstructionPcPair& inst : code_item_accessor_) {
Mathieu Chartier2b2bef22017-10-26 17:10:19 -07001685 const size_t dex_pc = inst.DexPc();
Ian Rogersd81871c2011-10-03 13:57:23 -07001686 RegisterLine* reg_line = reg_table_.GetLine(dex_pc);
Ian Rogers7b078e82014-09-10 14:44:24 -07001687 if (reg_line != nullptr) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001688 vios->Stream() << reg_line->Dump(this) << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07001689 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001690 vios->Stream()
Mathieu Chartierde40d472015-10-15 17:47:48 -07001691 << StringPrintf("0x%04zx", dex_pc) << ": " << GetInstructionFlags(dex_pc).ToString() << " ";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001692 const bool kDumpHexOfInstruction = false;
1693 if (kDumpHexOfInstruction) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001694 vios->Stream() << inst->DumpHex(5) << " ";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001695 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001696 vios->Stream() << inst->DumpString(dex_file_) << "\n";
jeffhaoba5ebb92011-08-25 17:24:37 -07001697 }
jeffhaobdb76512011-09-07 11:43:16 -07001698}
1699
Ian Rogersd81871c2011-10-03 13:57:23 -07001700static bool IsPrimitiveDescriptor(char descriptor) {
1701 switch (descriptor) {
jeffhaobdb76512011-09-07 11:43:16 -07001702 case 'I':
1703 case 'C':
1704 case 'S':
1705 case 'B':
1706 case 'Z':
jeffhaobdb76512011-09-07 11:43:16 -07001707 case 'F':
1708 case 'D':
1709 case 'J':
Ian Rogersd81871c2011-10-03 13:57:23 -07001710 return true;
jeffhaobdb76512011-09-07 11:43:16 -07001711 default:
1712 return false;
1713 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001714}
1715
Ian Rogers776ac1f2012-04-13 23:36:36 -07001716bool MethodVerifier::SetTypesFromSignature() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001717 RegisterLine* reg_line = reg_table_.GetLine(0);
Andreas Gampeef0b1a12015-06-19 20:37:46 -07001718
1719 // Should have been verified earlier.
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001720 DCHECK_GE(code_item_accessor_.RegistersSize(), code_item_accessor_.InsSize());
Andreas Gampeef0b1a12015-06-19 20:37:46 -07001721
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001722 uint32_t arg_start = code_item_accessor_.RegistersSize() - code_item_accessor_.InsSize();
1723 size_t expected_args = code_item_accessor_.InsSize(); /* long/double count as two */
jeffhaobdb76512011-09-07 11:43:16 -07001724
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001725 // Include the "this" pointer.
Ian Rogersd81871c2011-10-03 13:57:23 -07001726 size_t cur_arg = 0;
Ian Rogersad0b3a32012-04-16 14:50:24 -07001727 if (!IsStatic()) {
Andreas Gampeef0b1a12015-06-19 20:37:46 -07001728 if (expected_args == 0) {
1729 // Expect at least a receiver.
1730 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected 0 args, but method is not static";
1731 return false;
1732 }
1733
Ian Rogersd81871c2011-10-03 13:57:23 -07001734 // If this is a constructor for a class other than java.lang.Object, mark the first ("this")
1735 // argument as uninitialized. This restricts field access until the superclass constructor is
1736 // called.
Ian Rogersd8f69b02014-09-10 21:43:52 +00001737 const RegType& declaring_class = GetDeclaringClass();
Andreas Gampef10b6e12015-08-12 10:48:12 -07001738 if (IsConstructor()) {
1739 if (declaring_class.IsJavaLangObject()) {
1740 // "this" is implicitly initialized.
1741 reg_line->SetThisInitialized();
Andreas Gampead238ce2015-08-24 21:13:08 -07001742 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, declaring_class);
Andreas Gampef10b6e12015-08-12 10:48:12 -07001743 } else {
Andreas Gampead238ce2015-08-24 21:13:08 -07001744 reg_line->SetRegisterType<LockOp::kClear>(
1745 this,
1746 arg_start + cur_arg,
1747 reg_types_.UninitializedThisArgument(declaring_class));
Andreas Gampef10b6e12015-08-12 10:48:12 -07001748 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001749 } else {
Andreas Gampead238ce2015-08-24 21:13:08 -07001750 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, declaring_class);
jeffhaobdb76512011-09-07 11:43:16 -07001751 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001752 cur_arg++;
jeffhaobdb76512011-09-07 11:43:16 -07001753 }
1754
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001755 const DexFile::ProtoId& proto_id =
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001756 dex_file_->GetMethodPrototype(dex_file_->GetMethodId(dex_method_idx_));
Ian Rogers0571d352011-11-03 19:51:38 -07001757 DexFileParameterIterator iterator(*dex_file_, proto_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07001758
1759 for (; iterator.HasNext(); iterator.Next()) {
1760 const char* descriptor = iterator.GetDescriptor();
Ian Rogers7b078e82014-09-10 14:44:24 -07001761 if (descriptor == nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001762 LOG(FATAL) << "Null descriptor";
1763 }
1764 if (cur_arg >= expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07001765 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1766 << " args, found more (" << descriptor << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001767 return false;
1768 }
1769 switch (descriptor[0]) {
1770 case 'L':
1771 case '[':
1772 // We assume that reference arguments are initialized. The only way it could be otherwise
1773 // (assuming the caller was verified) is if the current method is <init>, but in that case
1774 // it's effectively considered initialized the instant we reach here (in the sense that we
1775 // can return without doing anything or call virtual methods).
1776 {
Andreas Gampe98be1a92017-08-28 08:25:45 -07001777 // Note: don't check access. No error would be thrown for declaring or passing an
1778 // inaccessible class. Only actual accesses to fields or methods will.
1779 const RegType& reg_type = ResolveClass<CheckAccess::kNo>(iterator.GetTypeIdx());
Sebastien Hertz2ed76f92014-04-22 17:11:08 +02001780 if (!reg_type.IsNonZeroReferenceTypes()) {
1781 DCHECK(HasFailures());
1782 return false;
1783 }
Andreas Gampead238ce2015-08-24 21:13:08 -07001784 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001785 }
1786 break;
1787 case 'Z':
Andreas Gampead238ce2015-08-24 21:13:08 -07001788 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Boolean());
Ian Rogersd81871c2011-10-03 13:57:23 -07001789 break;
1790 case 'C':
Andreas Gampead238ce2015-08-24 21:13:08 -07001791 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Char());
Ian Rogersd81871c2011-10-03 13:57:23 -07001792 break;
1793 case 'B':
Andreas Gampead238ce2015-08-24 21:13:08 -07001794 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Byte());
Ian Rogersd81871c2011-10-03 13:57:23 -07001795 break;
1796 case 'I':
Andreas Gampead238ce2015-08-24 21:13:08 -07001797 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Integer());
Ian Rogersd81871c2011-10-03 13:57:23 -07001798 break;
1799 case 'S':
Andreas Gampead238ce2015-08-24 21:13:08 -07001800 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Short());
Ian Rogersd81871c2011-10-03 13:57:23 -07001801 break;
1802 case 'F':
Andreas Gampead238ce2015-08-24 21:13:08 -07001803 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Float());
Ian Rogersd81871c2011-10-03 13:57:23 -07001804 break;
1805 case 'J':
1806 case 'D': {
Andreas Gampe77cd4d62014-06-19 17:29:48 -07001807 if (cur_arg + 1 >= expected_args) {
1808 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1809 << " args, found more (" << descriptor << ")";
1810 return false;
1811 }
1812
Ian Rogers7b078e82014-09-10 14:44:24 -07001813 const RegType* lo_half;
1814 const RegType* hi_half;
1815 if (descriptor[0] == 'J') {
1816 lo_half = &reg_types_.LongLo();
1817 hi_half = &reg_types_.LongHi();
1818 } else {
1819 lo_half = &reg_types_.DoubleLo();
1820 hi_half = &reg_types_.DoubleHi();
1821 }
1822 reg_line->SetRegisterTypeWide(this, arg_start + cur_arg, *lo_half, *hi_half);
Ian Rogersd81871c2011-10-03 13:57:23 -07001823 cur_arg++;
1824 break;
1825 }
1826 default:
Brian Carlstrom93c33962013-07-26 10:37:43 -07001827 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected signature type char '"
1828 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001829 return false;
1830 }
1831 cur_arg++;
1832 }
1833 if (cur_arg != expected_args) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001834 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1835 << " arguments, found " << cur_arg;
Ian Rogersd81871c2011-10-03 13:57:23 -07001836 return false;
1837 }
1838 const char* descriptor = dex_file_->GetReturnTypeDescriptor(proto_id);
1839 // Validate return type. We don't do the type lookup; just want to make sure that it has the right
1840 // format. Only major difference from the method argument format is that 'V' is supported.
1841 bool result;
1842 if (IsPrimitiveDescriptor(descriptor[0]) || descriptor[0] == 'V') {
1843 result = descriptor[1] == '\0';
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001844 } else if (descriptor[0] == '[') { // single/multi-dimensional array of object/primitive
Ian Rogersd81871c2011-10-03 13:57:23 -07001845 size_t i = 0;
1846 do {
1847 i++;
1848 } while (descriptor[i] == '['); // process leading [
1849 if (descriptor[i] == 'L') { // object array
1850 do {
1851 i++; // find closing ;
1852 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1853 result = descriptor[i] == ';';
1854 } else { // primitive array
1855 result = IsPrimitiveDescriptor(descriptor[i]) && descriptor[i + 1] == '\0';
1856 }
1857 } else if (descriptor[0] == 'L') {
1858 // could be more thorough here, but shouldn't be required
1859 size_t i = 0;
1860 do {
1861 i++;
1862 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1863 result = descriptor[i] == ';';
1864 } else {
1865 result = false;
1866 }
1867 if (!result) {
jeffhaod5347e02012-03-22 17:25:05 -07001868 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected char in return type descriptor '"
1869 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001870 }
1871 return result;
jeffhaobdb76512011-09-07 11:43:16 -07001872}
1873
Ian Rogers776ac1f2012-04-13 23:36:36 -07001874bool MethodVerifier::CodeFlowVerifyMethod() {
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001875 const uint16_t* insns = code_item_accessor_.Insns();
1876 const uint32_t insns_size = code_item_accessor_.InsnsSizeInCodeUnits();
jeffhaoba5ebb92011-08-25 17:24:37 -07001877
jeffhaobdb76512011-09-07 11:43:16 -07001878 /* Begin by marking the first instruction as "changed". */
Mathieu Chartierde40d472015-10-15 17:47:48 -07001879 GetInstructionFlags(0).SetChanged();
Ian Rogersd81871c2011-10-03 13:57:23 -07001880 uint32_t start_guess = 0;
jeffhaoba5ebb92011-08-25 17:24:37 -07001881
jeffhaobdb76512011-09-07 11:43:16 -07001882 /* Continue until no instructions are marked "changed". */
1883 while (true) {
Mathieu Chartier4306ef82014-12-19 18:41:47 -08001884 if (allow_thread_suspension_) {
1885 self_->AllowThreadSuspension();
1886 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001887 // Find the first marked one. Use "start_guess" as a way to find one quickly.
1888 uint32_t insn_idx = start_guess;
1889 for (; insn_idx < insns_size; insn_idx++) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07001890 if (GetInstructionFlags(insn_idx).IsChanged())
jeffhaobdb76512011-09-07 11:43:16 -07001891 break;
1892 }
jeffhaobdb76512011-09-07 11:43:16 -07001893 if (insn_idx == insns_size) {
1894 if (start_guess != 0) {
1895 /* try again, starting from the top */
1896 start_guess = 0;
1897 continue;
1898 } else {
1899 /* all flags are clear */
1900 break;
1901 }
1902 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001903 // We carry the working set of registers from instruction to instruction. If this address can
1904 // be the target of a branch (or throw) instruction, or if we're skipping around chasing
1905 // "changed" flags, we need to load the set of registers from the table.
1906 // Because we always prefer to continue on to the next instruction, we should never have a
1907 // situation where we have a stray "changed" flag set on an instruction that isn't a branch
1908 // target.
1909 work_insn_idx_ = insn_idx;
Mathieu Chartierde40d472015-10-15 17:47:48 -07001910 if (GetInstructionFlags(insn_idx).IsBranchTarget()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001911 work_line_->CopyFromLine(reg_table_.GetLine(insn_idx));
Ian Rogersebbdd872014-07-07 23:53:08 -07001912 } else if (kIsDebugBuild) {
jeffhaobdb76512011-09-07 11:43:16 -07001913 /*
1914 * Sanity check: retrieve the stored register line (assuming
1915 * a full table) and make sure it actually matches.
1916 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001917 RegisterLine* register_line = reg_table_.GetLine(insn_idx);
Ian Rogers7b078e82014-09-10 14:44:24 -07001918 if (register_line != nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001919 if (work_line_->CompareLine(register_line) != 0) {
1920 Dump(std::cout);
1921 std::cout << info_messages_.str();
David Sehr709b0702016-10-13 09:12:37 -07001922 LOG(FATAL) << "work_line diverged in " << dex_file_->PrettyMethod(dex_method_idx_)
Elliott Hughesc073b072012-05-24 19:29:17 -07001923 << "@" << reinterpret_cast<void*>(work_insn_idx_) << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07001924 << " work_line=" << work_line_->Dump(this) << "\n"
1925 << " expected=" << register_line->Dump(this);
Ian Rogersd81871c2011-10-03 13:57:23 -07001926 }
jeffhaobdb76512011-09-07 11:43:16 -07001927 }
jeffhaobdb76512011-09-07 11:43:16 -07001928 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001929 if (!CodeFlowVerifyInstruction(&start_guess)) {
David Sehr709b0702016-10-13 09:12:37 -07001930 std::string prepend(dex_file_->PrettyMethod(dex_method_idx_));
Ian Rogersad0b3a32012-04-16 14:50:24 -07001931 prepend += " failed to verify: ";
1932 PrependToLastFailMessage(prepend);
jeffhaoba5ebb92011-08-25 17:24:37 -07001933 return false;
1934 }
jeffhaobdb76512011-09-07 11:43:16 -07001935 /* Clear "changed" and mark as visited. */
Mathieu Chartierde40d472015-10-15 17:47:48 -07001936 GetInstructionFlags(insn_idx).SetVisited();
1937 GetInstructionFlags(insn_idx).ClearChanged();
jeffhaobdb76512011-09-07 11:43:16 -07001938 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001939
Andreas Gampe92d77202017-12-06 20:49:00 -08001940 if (UNLIKELY(VLOG_IS_ON(verifier_debug))) {
jeffhaobdb76512011-09-07 11:43:16 -07001941 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001942 * Scan for dead code. There's nothing "evil" about dead code
jeffhaobdb76512011-09-07 11:43:16 -07001943 * (besides the wasted space), but it indicates a flaw somewhere
1944 * down the line, possibly in the verifier.
1945 *
1946 * If we've substituted "always throw" instructions into the stream,
1947 * we are almost certainly going to have some dead code.
1948 */
1949 int dead_start = -1;
Mathieu Chartier1d2d4ff2017-09-23 16:11:06 -07001950
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001951 for (const DexInstructionPcPair& inst : code_item_accessor_) {
Mathieu Chartier2b2bef22017-10-26 17:10:19 -07001952 const uint32_t insn_idx = inst.DexPc();
jeffhaobdb76512011-09-07 11:43:16 -07001953 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001954 * Switch-statement data doesn't get "visited" by scanner. It
jeffhaobdb76512011-09-07 11:43:16 -07001955 * may or may not be preceded by a padding NOP (for alignment).
1956 */
1957 if (insns[insn_idx] == Instruction::kPackedSwitchSignature ||
1958 insns[insn_idx] == Instruction::kSparseSwitchSignature ||
1959 insns[insn_idx] == Instruction::kArrayDataSignature ||
Elliott Hughes380aaa72012-07-09 14:33:15 -07001960 (insns[insn_idx] == Instruction::NOP && (insn_idx + 1 < insns_size) &&
jeffhaobdb76512011-09-07 11:43:16 -07001961 (insns[insn_idx + 1] == Instruction::kPackedSwitchSignature ||
1962 insns[insn_idx + 1] == Instruction::kSparseSwitchSignature ||
1963 insns[insn_idx + 1] == Instruction::kArrayDataSignature))) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07001964 GetInstructionFlags(insn_idx).SetVisited();
jeffhaobdb76512011-09-07 11:43:16 -07001965 }
1966
Mathieu Chartierde40d472015-10-15 17:47:48 -07001967 if (!GetInstructionFlags(insn_idx).IsVisited()) {
Mathieu Chartier1d2d4ff2017-09-23 16:11:06 -07001968 if (dead_start < 0) {
jeffhaobdb76512011-09-07 11:43:16 -07001969 dead_start = insn_idx;
Mathieu Chartier1d2d4ff2017-09-23 16:11:06 -07001970 }
jeffhaobdb76512011-09-07 11:43:16 -07001971 } else if (dead_start >= 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001972 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start)
1973 << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaobdb76512011-09-07 11:43:16 -07001974 dead_start = -1;
1975 }
1976 }
1977 if (dead_start >= 0) {
Mathieu Chartier1d2d4ff2017-09-23 16:11:06 -07001978 LogVerifyInfo()
1979 << "dead code " << reinterpret_cast<void*>(dead_start)
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001980 << "-" << reinterpret_cast<void*>(code_item_accessor_.InsnsSizeInCodeUnits() - 1);
jeffhaoba5ebb92011-08-25 17:24:37 -07001981 }
Ian Rogersc9e463c2013-06-05 16:52:26 -07001982 // To dump the state of the verify after a method, do something like:
David Sehr709b0702016-10-13 09:12:37 -07001983 // if (dex_file_->PrettyMethod(dex_method_idx_) ==
Ian Rogersc9e463c2013-06-05 16:52:26 -07001984 // "boolean java.lang.String.equals(java.lang.Object)") {
1985 // LOG(INFO) << info_messages_.str();
1986 // }
jeffhaoba5ebb92011-08-25 17:24:37 -07001987 }
jeffhaobdb76512011-09-07 11:43:16 -07001988 return true;
1989}
1990
Andreas Gampe68df3202015-06-22 11:35:46 -07001991// Returns the index of the first final instance field of the given class, or kDexNoIndex if there
1992// is no such field.
Andreas Gampea5b09a62016-11-17 15:21:22 -08001993static uint32_t GetFirstFinalInstanceFieldIndex(const DexFile& dex_file, dex::TypeIndex type_idx) {
Andreas Gampe68df3202015-06-22 11:35:46 -07001994 const DexFile::ClassDef* class_def = dex_file.FindClassDef(type_idx);
1995 DCHECK(class_def != nullptr);
1996 const uint8_t* class_data = dex_file.GetClassData(*class_def);
1997 DCHECK(class_data != nullptr);
1998 ClassDataItemIterator it(dex_file, class_data);
Mathieu Chartiere17cf242017-06-19 11:05:51 -07001999 it.SkipStaticFields();
Andreas Gampe68df3202015-06-22 11:35:46 -07002000 while (it.HasNextInstanceField()) {
2001 if ((it.GetFieldAccessFlags() & kAccFinal) != 0) {
2002 return it.GetMemberIndex();
2003 }
2004 it.Next();
2005 }
Andreas Gampee2abbc62017-09-15 11:59:26 -07002006 return dex::kDexNoIndex;
Andreas Gampe68df3202015-06-22 11:35:46 -07002007}
2008
Andreas Gampea727e372015-08-25 09:22:37 -07002009// Setup a register line for the given return instruction.
2010static void AdjustReturnLine(MethodVerifier* verifier,
2011 const Instruction* ret_inst,
2012 RegisterLine* line) {
2013 Instruction::Code opcode = ret_inst->Opcode();
2014
2015 switch (opcode) {
2016 case Instruction::RETURN_VOID:
2017 case Instruction::RETURN_VOID_NO_BARRIER:
2018 SafelyMarkAllRegistersAsConflicts(verifier, line);
2019 break;
2020
2021 case Instruction::RETURN:
2022 case Instruction::RETURN_OBJECT:
2023 line->MarkAllRegistersAsConflictsExcept(verifier, ret_inst->VRegA_11x());
2024 break;
2025
2026 case Instruction::RETURN_WIDE:
2027 line->MarkAllRegistersAsConflictsExceptWide(verifier, ret_inst->VRegA_11x());
2028 break;
2029
2030 default:
2031 LOG(FATAL) << "Unknown return opcode " << opcode;
2032 UNREACHABLE();
2033 }
2034}
2035
Ian Rogers776ac1f2012-04-13 23:36:36 -07002036bool MethodVerifier::CodeFlowVerifyInstruction(uint32_t* start_guess) {
Elliott Hughes08fc03a2012-06-26 17:34:00 -07002037 // If we're doing FindLocksAtDexPc, check whether we're at the dex pc we care about.
2038 // We want the state _before_ the instruction, for the case where the dex pc we're
2039 // interested in is itself a monitor-enter instruction (which is a likely place
2040 // for a thread to be suspended).
Ian Rogers7b078e82014-09-10 14:44:24 -07002041 if (monitor_enter_dex_pcs_ != nullptr && work_insn_idx_ == interesting_dex_pc_) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -07002042 monitor_enter_dex_pcs_->clear(); // The new work line is more accurate than the previous one.
Andreas Gampeaaf0d382017-11-27 14:10:21 -08002043
2044 std::map<uint32_t, DexLockInfo> depth_to_lock_info;
2045 auto collector = [&](uint32_t dex_reg, uint32_t depth) {
2046 auto insert_pair = depth_to_lock_info.emplace(depth, DexLockInfo(depth));
2047 auto it = insert_pair.first;
2048 auto set_insert_pair = it->second.dex_registers.insert(dex_reg);
2049 DCHECK(set_insert_pair.second);
2050 };
2051 work_line_->IterateRegToLockDepths(collector);
2052 for (auto& pair : depth_to_lock_info) {
2053 monitor_enter_dex_pcs_->push_back(pair.second);
2054 // Map depth to dex PC.
2055 (*monitor_enter_dex_pcs_)[monitor_enter_dex_pcs_->size() - 1].dex_pc =
2056 work_line_->GetMonitorEnterDexPc(pair.second.dex_pc);
Elliott Hughes08fc03a2012-06-26 17:34:00 -07002057 }
2058 }
2059
jeffhaobdb76512011-09-07 11:43:16 -07002060 /*
2061 * Once we finish decoding the instruction, we need to figure out where
jeffhaod1f0fde2011-09-08 17:25:33 -07002062 * we can go from here. There are three possible ways to transfer
jeffhaobdb76512011-09-07 11:43:16 -07002063 * control to another statement:
2064 *
jeffhaod1f0fde2011-09-08 17:25:33 -07002065 * (1) Continue to the next instruction. Applies to all but
jeffhaobdb76512011-09-07 11:43:16 -07002066 * unconditional branches, method returns, and exception throws.
jeffhaod1f0fde2011-09-08 17:25:33 -07002067 * (2) Branch to one or more possible locations. Applies to branches
jeffhaobdb76512011-09-07 11:43:16 -07002068 * and switch statements.
jeffhaod1f0fde2011-09-08 17:25:33 -07002069 * (3) Exception handlers. Applies to any instruction that can
jeffhaobdb76512011-09-07 11:43:16 -07002070 * throw an exception that is handled by an encompassing "try"
2071 * block.
2072 *
2073 * We can also return, in which case there is no successor instruction
2074 * from this point.
2075 *
Elliott Hughesadb8c672012-03-06 16:49:32 -08002076 * The behavior can be determined from the opcode flags.
jeffhaobdb76512011-09-07 11:43:16 -07002077 */
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08002078 const uint16_t* insns = code_item_accessor_.Insns() + work_insn_idx_;
Ian Rogersd81871c2011-10-03 13:57:23 -07002079 const Instruction* inst = Instruction::At(insns);
Ian Rogersa75a0132012-09-28 11:41:42 -07002080 int opcode_flags = Instruction::FlagsOf(inst->Opcode());
jeffhaobdb76512011-09-07 11:43:16 -07002081
jeffhaobdb76512011-09-07 11:43:16 -07002082 int32_t branch_target = 0;
jeffhaobdb76512011-09-07 11:43:16 -07002083 bool just_set_result = false;
Andreas Gampe92d77202017-12-06 20:49:00 -08002084 if (UNLIKELY(VLOG_IS_ON(verifier_debug))) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002085 // Generate processing back trace to debug verifier
Elliott Hughesc073b072012-05-24 19:29:17 -07002086 LogVerifyInfo() << "Processing " << inst->DumpString(dex_file_) << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07002087 << work_line_->Dump(this) << "\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07002088 }
jeffhaobdb76512011-09-07 11:43:16 -07002089
2090 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002091 * Make a copy of the previous register state. If the instruction
jeffhaobdb76512011-09-07 11:43:16 -07002092 * can throw an exception, we will copy/merge this into the "catch"
2093 * address rather than work_line, because we don't want the result
2094 * from the "successful" code path (e.g. a check-cast that "improves"
2095 * a type) to be visible to the exception handler.
2096 */
Ian Rogers776ac1f2012-04-13 23:36:36 -07002097 if ((opcode_flags & Instruction::kThrow) != 0 && CurrentInsnFlags()->IsInTry()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002098 saved_line_->CopyFromLine(work_line_.get());
Ian Rogers1ff3c982014-08-12 02:30:58 -07002099 } else if (kIsDebugBuild) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002100 saved_line_->FillWithGarbage();
jeffhaobdb76512011-09-07 11:43:16 -07002101 }
Andreas Gamped12e7822015-06-25 10:26:40 -07002102 DCHECK(!have_pending_runtime_throw_failure_); // Per-instruction flag, should not be set here.
jeffhaobdb76512011-09-07 11:43:16 -07002103
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07002104
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002105 // We need to ensure the work line is consistent while performing validation. When we spot a
2106 // peephole pattern we compute a new line for either the fallthrough instruction or the
2107 // branch target.
Mathieu Chartier361e04a2016-02-16 14:06:35 -08002108 RegisterLineArenaUniquePtr branch_line;
2109 RegisterLineArenaUniquePtr fallthrough_line;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002110
Sebastien Hertz5243e912013-05-21 10:55:07 +02002111 switch (inst->Opcode()) {
jeffhaobdb76512011-09-07 11:43:16 -07002112 case Instruction::NOP:
2113 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002114 * A "pure" NOP has no effect on anything. Data tables start with
jeffhaobdb76512011-09-07 11:43:16 -07002115 * a signature that looks like a NOP; if we see one of these in
2116 * the course of executing code then we have a problem.
2117 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002118 if (inst->VRegA_10x() != 0) {
jeffhaod5347e02012-03-22 17:25:05 -07002119 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "encountered data table in instruction stream";
jeffhaobdb76512011-09-07 11:43:16 -07002120 }
2121 break;
2122
2123 case Instruction::MOVE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002124 work_line_->CopyRegister1(this, inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategory1nr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02002125 break;
jeffhaobdb76512011-09-07 11:43:16 -07002126 case Instruction::MOVE_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07002127 work_line_->CopyRegister1(this, inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategory1nr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02002128 break;
jeffhaobdb76512011-09-07 11:43:16 -07002129 case Instruction::MOVE_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07002130 work_line_->CopyRegister1(this, inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategory1nr);
jeffhaobdb76512011-09-07 11:43:16 -07002131 break;
2132 case Instruction::MOVE_WIDE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002133 work_line_->CopyRegister2(this, inst->VRegA_12x(), inst->VRegB_12x());
Sebastien Hertz5243e912013-05-21 10:55:07 +02002134 break;
jeffhaobdb76512011-09-07 11:43:16 -07002135 case Instruction::MOVE_WIDE_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07002136 work_line_->CopyRegister2(this, inst->VRegA_22x(), inst->VRegB_22x());
Sebastien Hertz5243e912013-05-21 10:55:07 +02002137 break;
jeffhaobdb76512011-09-07 11:43:16 -07002138 case Instruction::MOVE_WIDE_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07002139 work_line_->CopyRegister2(this, inst->VRegA_32x(), inst->VRegB_32x());
jeffhaobdb76512011-09-07 11:43:16 -07002140 break;
2141 case Instruction::MOVE_OBJECT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002142 work_line_->CopyRegister1(this, inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategoryRef);
Sebastien Hertz5243e912013-05-21 10:55:07 +02002143 break;
jeffhaobdb76512011-09-07 11:43:16 -07002144 case Instruction::MOVE_OBJECT_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07002145 work_line_->CopyRegister1(this, inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategoryRef);
Sebastien Hertz5243e912013-05-21 10:55:07 +02002146 break;
jeffhaobdb76512011-09-07 11:43:16 -07002147 case Instruction::MOVE_OBJECT_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07002148 work_line_->CopyRegister1(this, inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategoryRef);
jeffhaobdb76512011-09-07 11:43:16 -07002149 break;
2150
2151 /*
2152 * The move-result instructions copy data out of a "pseudo-register"
jeffhaod1f0fde2011-09-08 17:25:33 -07002153 * with the results from the last method invocation. In practice we
jeffhaobdb76512011-09-07 11:43:16 -07002154 * might want to hold the result in an actual CPU register, so the
2155 * Dalvik spec requires that these only appear immediately after an
2156 * invoke or filled-new-array.
2157 *
jeffhaod1f0fde2011-09-08 17:25:33 -07002158 * These calls invalidate the "result" register. (This is now
jeffhaobdb76512011-09-07 11:43:16 -07002159 * redundant with the reset done below, but it can make the debug info
2160 * easier to read in some cases.)
2161 */
2162 case Instruction::MOVE_RESULT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002163 work_line_->CopyResultRegister1(this, inst->VRegA_11x(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002164 break;
2165 case Instruction::MOVE_RESULT_WIDE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002166 work_line_->CopyResultRegister2(this, inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07002167 break;
2168 case Instruction::MOVE_RESULT_OBJECT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002169 work_line_->CopyResultRegister1(this, inst->VRegA_11x(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002170 break;
2171
Ian Rogersd81871c2011-10-03 13:57:23 -07002172 case Instruction::MOVE_EXCEPTION: {
Sebastien Hertz270a0e12015-01-16 19:49:09 +01002173 // We do not allow MOVE_EXCEPTION as the first instruction in a method. This is a simple case
2174 // where one entrypoint to the catch block is not actually an exception path.
2175 if (work_insn_idx_ == 0) {
2176 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "move-exception at pc 0x0";
2177 break;
2178 }
jeffhaobdb76512011-09-07 11:43:16 -07002179 /*
jeffhao60f83e32012-02-13 17:16:30 -08002180 * This statement can only appear as the first instruction in an exception handler. We verify
2181 * that as part of extracting the exception type from the catch block list.
jeffhaobdb76512011-09-07 11:43:16 -07002182 */
Ian Rogersd8f69b02014-09-10 21:43:52 +00002183 const RegType& res_type = GetCaughtExceptionType();
Andreas Gampead238ce2015-08-24 21:13:08 -07002184 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_11x(), res_type);
jeffhaobdb76512011-09-07 11:43:16 -07002185 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002186 }
jeffhaobdb76512011-09-07 11:43:16 -07002187 case Instruction::RETURN_VOID:
Andreas Gampef10b6e12015-08-12 10:48:12 -07002188 if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002189 if (!GetMethodReturnType().IsConflict()) {
jeffhaod5347e02012-03-22 17:25:05 -07002190 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07002191 }
jeffhaobdb76512011-09-07 11:43:16 -07002192 }
2193 break;
2194 case Instruction::RETURN:
Andreas Gampef10b6e12015-08-12 10:48:12 -07002195 if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) {
jeffhaobdb76512011-09-07 11:43:16 -07002196 /* check the method signature */
Ian Rogersd8f69b02014-09-10 21:43:52 +00002197 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07002198 if (!return_type.IsCategory1Types()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002199 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected non-category 1 return type "
2200 << return_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002201 } else {
2202 // Compilers may generate synthetic functions that write byte values into boolean fields.
2203 // Also, it may use integer values for boolean, byte, short, and character return types.
Sebastien Hertz5243e912013-05-21 10:55:07 +02002204 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07002205 const RegType& src_type = work_line_->GetRegisterType(this, vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07002206 bool use_src = ((return_type.IsBoolean() && src_type.IsByte()) ||
2207 ((return_type.IsBoolean() || return_type.IsByte() ||
2208 return_type.IsShort() || return_type.IsChar()) &&
2209 src_type.IsInteger()));
2210 /* check the register contents */
Ian Rogersad0b3a32012-04-16 14:50:24 -07002211 bool success =
Ian Rogers7b078e82014-09-10 14:44:24 -07002212 work_line_->VerifyRegisterType(this, vregA, use_src ? src_type : return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002213 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002214 AppendToLastFailMessage(StringPrintf(" return-1nr on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07002215 }
jeffhaobdb76512011-09-07 11:43:16 -07002216 }
2217 }
2218 break;
2219 case Instruction::RETURN_WIDE:
Andreas Gampef10b6e12015-08-12 10:48:12 -07002220 if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) {
jeffhaobdb76512011-09-07 11:43:16 -07002221 /* check the method signature */
Ian Rogersd8f69b02014-09-10 21:43:52 +00002222 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07002223 if (!return_type.IsCategory2Types()) {
jeffhaod5347e02012-03-22 17:25:05 -07002224 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-wide not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07002225 } else {
2226 /* check the register contents */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002227 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07002228 bool success = work_line_->VerifyRegisterType(this, vregA, return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002229 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002230 AppendToLastFailMessage(StringPrintf(" return-wide on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07002231 }
jeffhaobdb76512011-09-07 11:43:16 -07002232 }
2233 }
2234 break;
2235 case Instruction::RETURN_OBJECT:
Andreas Gampef10b6e12015-08-12 10:48:12 -07002236 if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) {
Ian Rogersd8f69b02014-09-10 21:43:52 +00002237 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07002238 if (!return_type.IsReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002239 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-object not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07002240 } else {
2241 /* return_type is the *expected* return type, not register value */
Andreas Gampeeed3a5d2017-11-29 14:58:34 -08002242 DCHECK(!return_type.IsZeroOrNull());
Ian Rogersd81871c2011-10-03 13:57:23 -07002243 DCHECK(!return_type.IsUninitializedReference());
Sebastien Hertz5243e912013-05-21 10:55:07 +02002244 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07002245 const RegType& reg_type = work_line_->GetRegisterType(this, vregA);
Andreas Gampea32210c2015-06-24 10:26:13 -07002246 // Disallow returning undefined, conflict & uninitialized values and verify that the
2247 // reference in vAA is an instance of the "return_type."
2248 if (reg_type.IsUndefined()) {
2249 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning undefined register";
2250 } else if (reg_type.IsConflict()) {
2251 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning register with conflict";
2252 } else if (reg_type.IsUninitializedTypes()) {
David Brazdil68b5c0b2016-01-19 14:25:29 +00002253 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning uninitialized object '"
Brian Carlstrom93c33962013-07-26 10:37:43 -07002254 << reg_type << "'";
Andreas Gampea4c98f22015-11-06 16:24:49 -08002255 } else if (!reg_type.IsReferenceTypes()) {
2256 // We really do expect a reference here.
2257 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-object returns a non-reference type "
2258 << reg_type;
David Brazdilca3c8c32016-09-06 14:04:48 +01002259 } else if (!return_type.IsAssignableFrom(reg_type, this)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07002260 if (reg_type.IsUnresolvedTypes() || return_type.IsUnresolvedTypes()) {
2261 Fail(VERIFY_ERROR_NO_CLASS) << " can't resolve returned type '" << return_type
2262 << "' or '" << reg_type << "'";
2263 } else {
Andreas Gampe16f149c2015-03-23 10:10:20 -07002264 bool soft_error = false;
2265 // Check whether arrays are involved. They will show a valid class status, even
2266 // if their components are erroneous.
2267 if (reg_type.IsArrayTypes() && return_type.IsArrayTypes()) {
David Brazdilca3c8c32016-09-06 14:04:48 +01002268 return_type.CanAssignArray(reg_type, reg_types_, class_loader_, this, &soft_error);
Andreas Gampe16f149c2015-03-23 10:10:20 -07002269 if (soft_error) {
2270 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "array with erroneous component type: "
2271 << reg_type << " vs " << return_type;
2272 }
2273 }
2274
2275 if (!soft_error) {
2276 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning '" << reg_type
2277 << "', but expected from declaration '" << return_type << "'";
2278 }
Jeff Haoa3faaf42013-09-03 19:07:00 -07002279 }
jeffhaobdb76512011-09-07 11:43:16 -07002280 }
2281 }
2282 }
2283 break;
2284
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07002285 /* could be boolean, int, float, or a null reference */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002286 case Instruction::CONST_4: {
2287 int32_t val = static_cast<int32_t>(inst->VRegB_11n() << 28) >> 28;
Andreas Gampead238ce2015-08-24 21:13:08 -07002288 work_line_->SetRegisterType<LockOp::kClear>(
2289 this, inst->VRegA_11n(), DetermineCat1Constant(val, need_precise_constants_));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07002290 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02002291 }
2292 case Instruction::CONST_16: {
2293 int16_t val = static_cast<int16_t>(inst->VRegB_21s());
Andreas Gampead238ce2015-08-24 21:13:08 -07002294 work_line_->SetRegisterType<LockOp::kClear>(
2295 this, inst->VRegA_21s(), DetermineCat1Constant(val, need_precise_constants_));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07002296 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02002297 }
Sebastien Hertz849600b2013-12-20 10:28:08 +01002298 case Instruction::CONST: {
2299 int32_t val = inst->VRegB_31i();
Andreas Gampead238ce2015-08-24 21:13:08 -07002300 work_line_->SetRegisterType<LockOp::kClear>(
2301 this, inst->VRegA_31i(), DetermineCat1Constant(val, need_precise_constants_));
jeffhaobdb76512011-09-07 11:43:16 -07002302 break;
Sebastien Hertz849600b2013-12-20 10:28:08 +01002303 }
2304 case Instruction::CONST_HIGH16: {
2305 int32_t val = static_cast<int32_t>(inst->VRegB_21h() << 16);
Andreas Gampead238ce2015-08-24 21:13:08 -07002306 work_line_->SetRegisterType<LockOp::kClear>(
2307 this, inst->VRegA_21h(), DetermineCat1Constant(val, need_precise_constants_));
jeffhaobdb76512011-09-07 11:43:16 -07002308 break;
Sebastien Hertz849600b2013-12-20 10:28:08 +01002309 }
jeffhaobdb76512011-09-07 11:43:16 -07002310 /* could be long or double; resolved upon use */
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002311 case Instruction::CONST_WIDE_16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002312 int64_t val = static_cast<int16_t>(inst->VRegB_21s());
Ian Rogersd8f69b02014-09-10 21:43:52 +00002313 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
2314 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07002315 work_line_->SetRegisterTypeWide(this, inst->VRegA_21s(), lo, hi);
jeffhaobdb76512011-09-07 11:43:16 -07002316 break;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002317 }
2318 case Instruction::CONST_WIDE_32: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002319 int64_t val = static_cast<int32_t>(inst->VRegB_31i());
Ian Rogersd8f69b02014-09-10 21:43:52 +00002320 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
2321 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07002322 work_line_->SetRegisterTypeWide(this, inst->VRegA_31i(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002323 break;
2324 }
2325 case Instruction::CONST_WIDE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002326 int64_t val = inst->VRegB_51l();
Ian Rogersd8f69b02014-09-10 21:43:52 +00002327 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
2328 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07002329 work_line_->SetRegisterTypeWide(this, inst->VRegA_51l(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002330 break;
2331 }
2332 case Instruction::CONST_WIDE_HIGH16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002333 int64_t val = static_cast<uint64_t>(inst->VRegB_21h()) << 48;
Ian Rogersd8f69b02014-09-10 21:43:52 +00002334 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
2335 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07002336 work_line_->SetRegisterTypeWide(this, inst->VRegA_21h(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002337 break;
2338 }
jeffhaobdb76512011-09-07 11:43:16 -07002339 case Instruction::CONST_STRING:
Andreas Gampead238ce2015-08-24 21:13:08 -07002340 work_line_->SetRegisterType<LockOp::kClear>(
2341 this, inst->VRegA_21c(), reg_types_.JavaLangString());
Sebastien Hertz5243e912013-05-21 10:55:07 +02002342 break;
jeffhaobdb76512011-09-07 11:43:16 -07002343 case Instruction::CONST_STRING_JUMBO:
Andreas Gampead238ce2015-08-24 21:13:08 -07002344 work_line_->SetRegisterType<LockOp::kClear>(
2345 this, inst->VRegA_31c(), reg_types_.JavaLangString());
jeffhaobdb76512011-09-07 11:43:16 -07002346 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002347 case Instruction::CONST_CLASS: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002348 // Get type from instruction if unresolved then we need an access check
2349 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Andreas Gampe98be1a92017-08-28 08:25:45 -07002350 const RegType& res_type = ResolveClass<CheckAccess::kYes>(dex::TypeIndex(inst->VRegB_21c()));
Ian Rogersad0b3a32012-04-16 14:50:24 -07002351 // Register holds class, ie its type is class, on error it will hold Conflict.
Andreas Gampead238ce2015-08-24 21:13:08 -07002352 work_line_->SetRegisterType<LockOp::kClear>(
2353 this, inst->VRegA_21c(), res_type.IsConflict() ? res_type
2354 : reg_types_.JavaLangClass());
jeffhaobdb76512011-09-07 11:43:16 -07002355 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002356 }
Orion Hodson2e599942017-09-22 16:17:41 +01002357 case Instruction::CONST_METHOD_HANDLE:
2358 work_line_->SetRegisterType<LockOp::kClear>(
2359 this, inst->VRegA_21c(), reg_types_.JavaLangInvokeMethodHandle());
2360 // TODO: add compiler support for const-method-{handle,type} (b/66890674)
2361 Fail(VERIFY_ERROR_FORCE_INTERPRETER);
2362 break;
2363 case Instruction::CONST_METHOD_TYPE:
2364 work_line_->SetRegisterType<LockOp::kClear>(
2365 this, inst->VRegA_21c(), reg_types_.JavaLangInvokeMethodType());
2366 // TODO: add compiler support for const-method-{handle,type} (b/66890674)
2367 Fail(VERIFY_ERROR_FORCE_INTERPRETER);
2368 break;
jeffhaobdb76512011-09-07 11:43:16 -07002369 case Instruction::MONITOR_ENTER:
Ian Rogers7b078e82014-09-10 14:44:24 -07002370 work_line_->PushMonitor(this, inst->VRegA_11x(), work_insn_idx_);
Andreas Gampec1474102015-08-18 08:57:44 -07002371 // Check whether the previous instruction is a move-object with vAA as a source, creating
2372 // untracked lock aliasing.
Mathieu Chartierde40d472015-10-15 17:47:48 -07002373 if (0 != work_insn_idx_ && !GetInstructionFlags(work_insn_idx_).IsBranchTarget()) {
Andreas Gampec1474102015-08-18 08:57:44 -07002374 uint32_t prev_idx = work_insn_idx_ - 1;
Mathieu Chartierde40d472015-10-15 17:47:48 -07002375 while (0 != prev_idx && !GetInstructionFlags(prev_idx).IsOpcode()) {
Andreas Gampec1474102015-08-18 08:57:44 -07002376 prev_idx--;
2377 }
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08002378 const Instruction& prev_inst = code_item_accessor_.InstructionAt(prev_idx);
Mathieu Chartier1d2d4ff2017-09-23 16:11:06 -07002379 switch (prev_inst.Opcode()) {
Andreas Gampec1474102015-08-18 08:57:44 -07002380 case Instruction::MOVE_OBJECT:
2381 case Instruction::MOVE_OBJECT_16:
2382 case Instruction::MOVE_OBJECT_FROM16:
Mathieu Chartier1d2d4ff2017-09-23 16:11:06 -07002383 if (prev_inst.VRegB() == inst->VRegA_11x()) {
Andreas Gampec1474102015-08-18 08:57:44 -07002384 // Redo the copy. This won't change the register types, but update the lock status
2385 // for the aliased register.
2386 work_line_->CopyRegister1(this,
Mathieu Chartier1d2d4ff2017-09-23 16:11:06 -07002387 prev_inst.VRegA(),
2388 prev_inst.VRegB(),
Andreas Gampec1474102015-08-18 08:57:44 -07002389 kTypeCategoryRef);
2390 }
2391 break;
2392
2393 default: // Other instruction types ignored.
2394 break;
2395 }
2396 }
jeffhaobdb76512011-09-07 11:43:16 -07002397 break;
2398 case Instruction::MONITOR_EXIT:
2399 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002400 * monitor-exit instructions are odd. They can throw exceptions,
jeffhaobdb76512011-09-07 11:43:16 -07002401 * but when they do they act as if they succeeded and the PC is
jeffhaod1f0fde2011-09-08 17:25:33 -07002402 * pointing to the following instruction. (This behavior goes back
jeffhaobdb76512011-09-07 11:43:16 -07002403 * to the need to handle asynchronous exceptions, a now-deprecated
2404 * feature that Dalvik doesn't support.)
2405 *
jeffhaod1f0fde2011-09-08 17:25:33 -07002406 * In practice we don't need to worry about this. The only
jeffhaobdb76512011-09-07 11:43:16 -07002407 * exceptions that can be thrown from monitor-exit are for a
jeffhaod1f0fde2011-09-08 17:25:33 -07002408 * null reference and -exit without a matching -enter. If the
jeffhaobdb76512011-09-07 11:43:16 -07002409 * structured locking checks are working, the former would have
2410 * failed on the -enter instruction, and the latter is impossible.
2411 *
2412 * This is fortunate, because issue 3221411 prevents us from
2413 * chasing the "can throw" path when monitor verification is
jeffhaod1f0fde2011-09-08 17:25:33 -07002414 * enabled. If we can fully verify the locking we can ignore
jeffhaobdb76512011-09-07 11:43:16 -07002415 * some catch blocks (which will show up as "dead" code when
2416 * we skip them here); if we can't, then the code path could be
2417 * "live" so we still need to check it.
2418 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002419 opcode_flags &= ~Instruction::kThrow;
Ian Rogers7b078e82014-09-10 14:44:24 -07002420 work_line_->PopMonitor(this, inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07002421 break;
Ian Rogers28ad40d2011-10-27 15:19:26 -07002422 case Instruction::CHECK_CAST:
Ian Rogersd81871c2011-10-03 13:57:23 -07002423 case Instruction::INSTANCE_OF: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002424 /*
2425 * If this instruction succeeds, we will "downcast" register vA to the type in vB. (This
2426 * could be a "upcast" -- not expected, so we don't try to address it.)
2427 *
2428 * If it fails, an exception is thrown, which we deal with later by ignoring the update to
Elliott Hughesadb8c672012-03-06 16:49:32 -08002429 * dec_insn.vA when branching to a handler.
Ian Rogers28ad40d2011-10-27 15:19:26 -07002430 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002431 const bool is_checkcast = (inst->Opcode() == Instruction::CHECK_CAST);
Andreas Gampea5b09a62016-11-17 15:21:22 -08002432 const dex::TypeIndex type_idx((is_checkcast) ? inst->VRegB_21c() : inst->VRegC_22c());
Andreas Gampe98be1a92017-08-28 08:25:45 -07002433 const RegType& res_type = ResolveClass<CheckAccess::kYes>(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002434 if (res_type.IsConflict()) {
Andreas Gampe00633eb2014-07-17 16:13:35 -07002435 // If this is a primitive type, fail HARD.
Vladimir Marko666ee3d2017-12-11 18:37:36 +00002436 ObjPtr<mirror::Class> klass = Runtime::Current()->GetClassLinker()->LookupResolvedType(
2437 type_idx, dex_cache_.Get(), class_loader_.Get());
Andreas Gampe00633eb2014-07-17 16:13:35 -07002438 if (klass != nullptr && klass->IsPrimitive()) {
2439 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "using primitive type "
2440 << dex_file_->StringByTypeIdx(type_idx) << " in instanceof in "
2441 << GetDeclaringClass();
2442 break;
2443 }
2444
Ian Rogersad0b3a32012-04-16 14:50:24 -07002445 DCHECK_NE(failures_.size(), 0U);
2446 if (!is_checkcast) {
Andreas Gampead238ce2015-08-24 21:13:08 -07002447 work_line_->SetRegisterType<LockOp::kClear>(this,
2448 inst->VRegA_22c(),
2449 reg_types_.Boolean());
Ian Rogersad0b3a32012-04-16 14:50:24 -07002450 }
2451 break; // bad class
Ian Rogers9f1ab122011-12-12 08:52:43 -08002452 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002453 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Sebastien Hertz5243e912013-05-21 10:55:07 +02002454 uint32_t orig_type_reg = (is_checkcast) ? inst->VRegA_21c() : inst->VRegB_22c();
Ian Rogers7b078e82014-09-10 14:44:24 -07002455 const RegType& orig_type = work_line_->GetRegisterType(this, orig_type_reg);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002456 if (!res_type.IsNonZeroReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002457 if (is_checkcast) {
2458 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on unexpected class " << res_type;
2459 } else {
2460 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on unexpected class " << res_type;
2461 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002462 } else if (!orig_type.IsReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002463 if (is_checkcast) {
2464 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on non-reference in v" << orig_type_reg;
2465 } else {
2466 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on non-reference in v" << orig_type_reg;
2467 }
David Brazdil68b5c0b2016-01-19 14:25:29 +00002468 } else if (orig_type.IsUninitializedTypes()) {
2469 if (is_checkcast) {
2470 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on uninitialized reference in v"
2471 << orig_type_reg;
2472 } else {
2473 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on uninitialized reference in v"
2474 << orig_type_reg;
2475 }
jeffhao2a8a90e2011-09-26 14:25:31 -07002476 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002477 if (is_checkcast) {
Andreas Gampead238ce2015-08-24 21:13:08 -07002478 work_line_->SetRegisterType<LockOp::kKeep>(this, inst->VRegA_21c(), res_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07002479 } else {
Andreas Gampead238ce2015-08-24 21:13:08 -07002480 work_line_->SetRegisterType<LockOp::kClear>(this,
2481 inst->VRegA_22c(),
2482 reg_types_.Boolean());
jeffhaobdb76512011-09-07 11:43:16 -07002483 }
jeffhaobdb76512011-09-07 11:43:16 -07002484 }
jeffhao2a8a90e2011-09-26 14:25:31 -07002485 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002486 }
2487 case Instruction::ARRAY_LENGTH: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002488 const RegType& res_type = work_line_->GetRegisterType(this, inst->VRegB_12x());
Ian Rogers28ad40d2011-10-27 15:19:26 -07002489 if (res_type.IsReferenceTypes()) {
Andreas Gampeeed3a5d2017-11-29 14:58:34 -08002490 if (!res_type.IsArrayTypes() && !res_type.IsZeroOrNull()) {
2491 // ie not an array or null
jeffhaod5347e02012-03-22 17:25:05 -07002492 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002493 } else {
Andreas Gampead238ce2015-08-24 21:13:08 -07002494 work_line_->SetRegisterType<LockOp::kClear>(this,
2495 inst->VRegA_12x(),
2496 reg_types_.Integer());
Ian Rogersd81871c2011-10-03 13:57:23 -07002497 }
Andreas Gampe65c9db82014-07-28 13:14:34 -07002498 } else {
2499 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002500 }
2501 break;
2502 }
2503 case Instruction::NEW_INSTANCE: {
Andreas Gampe98be1a92017-08-28 08:25:45 -07002504 const RegType& res_type = ResolveClass<CheckAccess::kYes>(dex::TypeIndex(inst->VRegB_21c()));
Ian Rogersad0b3a32012-04-16 14:50:24 -07002505 if (res_type.IsConflict()) {
2506 DCHECK_NE(failures_.size(), 0U);
2507 break; // bad class
jeffhao8cd6dda2012-02-22 10:15:34 -08002508 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002509 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
2510 // can't create an instance of an interface or abstract class */
2511 if (!res_type.IsInstantiableTypes()) {
2512 Fail(VERIFY_ERROR_INSTANTIATION)
2513 << "new-instance on primitive, interface or abstract class" << res_type;
Ian Rogers08f753d2012-08-24 14:35:25 -07002514 // Soft failure so carry on to set register type.
Ian Rogersd81871c2011-10-03 13:57:23 -07002515 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00002516 const RegType& uninit_type = reg_types_.Uninitialized(res_type, work_insn_idx_);
Ian Rogers08f753d2012-08-24 14:35:25 -07002517 // Any registers holding previous allocations from this address that have not yet been
2518 // initialized must be marked invalid.
Ian Rogers7b078e82014-09-10 14:44:24 -07002519 work_line_->MarkUninitRefsAsInvalid(this, uninit_type);
Ian Rogers08f753d2012-08-24 14:35:25 -07002520 // add the new uninitialized reference to the register state
Andreas Gampead238ce2015-08-24 21:13:08 -07002521 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_21c(), uninit_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07002522 break;
2523 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08002524 case Instruction::NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002525 VerifyNewArray(inst, false, false);
jeffhaobdb76512011-09-07 11:43:16 -07002526 break;
2527 case Instruction::FILLED_NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002528 VerifyNewArray(inst, true, false);
Ian Rogers0c4a5062012-02-03 15:18:59 -08002529 just_set_result = true; // Filled new array sets result register
jeffhaobdb76512011-09-07 11:43:16 -07002530 break;
Ian Rogers0c4a5062012-02-03 15:18:59 -08002531 case Instruction::FILLED_NEW_ARRAY_RANGE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002532 VerifyNewArray(inst, true, true);
Ian Rogers0c4a5062012-02-03 15:18:59 -08002533 just_set_result = true; // Filled new array range sets result register
2534 break;
jeffhaobdb76512011-09-07 11:43:16 -07002535 case Instruction::CMPL_FLOAT:
2536 case Instruction::CMPG_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002537 if (!work_line_->VerifyRegisterType(this, inst->VRegB_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08002538 break;
2539 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002540 if (!work_line_->VerifyRegisterType(this, inst->VRegC_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08002541 break;
2542 }
Andreas Gampead238ce2015-08-24 21:13:08 -07002543 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002544 break;
2545 case Instruction::CMPL_DOUBLE:
2546 case Instruction::CMPG_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002547 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegB_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002548 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002549 break;
2550 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002551 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegC_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002552 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002553 break;
2554 }
Andreas Gampead238ce2015-08-24 21:13:08 -07002555 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002556 break;
2557 case Instruction::CMP_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002558 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegB_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002559 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002560 break;
2561 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002562 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegC_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002563 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002564 break;
2565 }
Andreas Gampead238ce2015-08-24 21:13:08 -07002566 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002567 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002568 case Instruction::THROW: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002569 const RegType& res_type = work_line_->GetRegisterType(this, inst->VRegA_11x());
David Brazdilca3c8c32016-09-06 14:04:48 +01002570 if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(res_type, this)) {
David Brazdil68b5c0b2016-01-19 14:25:29 +00002571 if (res_type.IsUninitializedTypes()) {
2572 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "thrown exception not initialized";
Pavel Vyssotski980027c2016-02-11 20:28:11 +06002573 } else if (!res_type.IsReferenceTypes()) {
2574 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "thrown value of non-reference type " << res_type;
David Brazdil68b5c0b2016-01-19 14:25:29 +00002575 } else {
2576 Fail(res_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS : VERIFY_ERROR_BAD_CLASS_SOFT)
2577 << "thrown class " << res_type << " not instanceof Throwable";
2578 }
jeffhaobdb76512011-09-07 11:43:16 -07002579 }
2580 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002581 }
jeffhaobdb76512011-09-07 11:43:16 -07002582 case Instruction::GOTO:
2583 case Instruction::GOTO_16:
2584 case Instruction::GOTO_32:
2585 /* no effect on or use of registers */
2586 break;
2587
2588 case Instruction::PACKED_SWITCH:
2589 case Instruction::SPARSE_SWITCH:
2590 /* verify that vAA is an integer, or can be converted to one */
Ian Rogers7b078e82014-09-10 14:44:24 -07002591 work_line_->VerifyRegisterType(this, inst->VRegA_31t(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002592 break;
2593
Ian Rogersd81871c2011-10-03 13:57:23 -07002594 case Instruction::FILL_ARRAY_DATA: {
2595 /* Similar to the verification done for APUT */
Ian Rogers7b078e82014-09-10 14:44:24 -07002596 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegA_31t());
Ian Rogers89310de2012-02-01 13:47:30 -08002597 /* array_type can be null if the reg type is Zero */
Andreas Gampeeed3a5d2017-11-29 14:58:34 -08002598 if (!array_type.IsZeroOrNull()) {
jeffhao457cc512012-02-02 16:55:13 -08002599 if (!array_type.IsArrayTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002600 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with array type "
2601 << array_type;
Andreas Gampebb18a032016-03-22 20:34:25 -07002602 } else if (array_type.IsUnresolvedTypes()) {
2603 // If it's an unresolved array type, it must be non-primitive.
2604 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data for array of type "
2605 << array_type;
Ian Rogers89310de2012-02-01 13:47:30 -08002606 } else {
Ian Rogersd8f69b02014-09-10 21:43:52 +00002607 const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
Ian Rogersad0b3a32012-04-16 14:50:24 -07002608 DCHECK(!component_type.IsConflict());
jeffhao457cc512012-02-02 16:55:13 -08002609 if (component_type.IsNonZeroReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002610 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with component type "
2611 << component_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002612 } else {
jeffhao457cc512012-02-02 16:55:13 -08002613 // Now verify if the element width in the table matches the element width declared in
2614 // the array
Andreas Gampe53de99c2015-08-17 13:43:55 -07002615 const uint16_t* array_data =
2616 insns + (insns[1] | (static_cast<int32_t>(insns[2]) << 16));
jeffhao457cc512012-02-02 16:55:13 -08002617 if (array_data[0] != Instruction::kArrayDataSignature) {
jeffhaod5347e02012-03-22 17:25:05 -07002618 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid magic for array-data";
jeffhao457cc512012-02-02 16:55:13 -08002619 } else {
2620 size_t elem_width = Primitive::ComponentSize(component_type.GetPrimitiveType());
2621 // Since we don't compress the data in Dex, expect to see equal width of data stored
2622 // in the table and expected from the array class.
2623 if (array_data[1] != elem_width) {
jeffhaod5347e02012-03-22 17:25:05 -07002624 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-data size mismatch (" << array_data[1]
2625 << " vs " << elem_width << ")";
jeffhao457cc512012-02-02 16:55:13 -08002626 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002627 }
2628 }
jeffhaobdb76512011-09-07 11:43:16 -07002629 }
2630 }
2631 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002632 }
jeffhaobdb76512011-09-07 11:43:16 -07002633 case Instruction::IF_EQ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002634 case Instruction::IF_NE: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002635 const RegType& reg_type1 = work_line_->GetRegisterType(this, inst->VRegA_22t());
2636 const RegType& reg_type2 = work_line_->GetRegisterType(this, inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002637 bool mismatch = false;
Andreas Gampeeed3a5d2017-11-29 14:58:34 -08002638 if (reg_type1.IsZeroOrNull()) { // zero then integral or reference expected
Ian Rogersd81871c2011-10-03 13:57:23 -07002639 mismatch = !reg_type2.IsReferenceTypes() && !reg_type2.IsIntegralTypes();
2640 } else if (reg_type1.IsReferenceTypes()) { // both references?
2641 mismatch = !reg_type2.IsReferenceTypes();
2642 } else { // both integral?
2643 mismatch = !reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes();
2644 }
2645 if (mismatch) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002646 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to if-eq/if-ne (" << reg_type1 << ","
2647 << reg_type2 << ") must both be references or integral";
jeffhaobdb76512011-09-07 11:43:16 -07002648 }
2649 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002650 }
jeffhaobdb76512011-09-07 11:43:16 -07002651 case Instruction::IF_LT:
2652 case Instruction::IF_GE:
2653 case Instruction::IF_GT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002654 case Instruction::IF_LE: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002655 const RegType& reg_type1 = work_line_->GetRegisterType(this, inst->VRegA_22t());
2656 const RegType& reg_type2 = work_line_->GetRegisterType(this, inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002657 if (!reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002658 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to 'if' (" << reg_type1 << ","
2659 << reg_type2 << ") must be integral";
jeffhaobdb76512011-09-07 11:43:16 -07002660 }
2661 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002662 }
jeffhaobdb76512011-09-07 11:43:16 -07002663 case Instruction::IF_EQZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002664 case Instruction::IF_NEZ: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002665 const RegType& reg_type = work_line_->GetRegisterType(this, inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002666 if (!reg_type.IsReferenceTypes() && !reg_type.IsIntegralTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002667 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
2668 << " unexpected as arg to if-eqz/if-nez";
Ian Rogersd81871c2011-10-03 13:57:23 -07002669 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002670
2671 // Find previous instruction - its existence is a precondition to peephole optimization.
Ian Rogers9b360392013-06-06 14:45:07 -07002672 uint32_t instance_of_idx = 0;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002673 if (0 != work_insn_idx_) {
Ian Rogers9b360392013-06-06 14:45:07 -07002674 instance_of_idx = work_insn_idx_ - 1;
Mathieu Chartierde40d472015-10-15 17:47:48 -07002675 while (0 != instance_of_idx && !GetInstructionFlags(instance_of_idx).IsOpcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07002676 instance_of_idx--;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002677 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07002678 if (FailOrAbort(this, GetInstructionFlags(instance_of_idx).IsOpcode(),
Andreas Gampe7c038102014-10-27 20:08:46 -07002679 "Unable to get previous instruction of if-eqz/if-nez for work index ",
2680 work_insn_idx_)) {
2681 break;
2682 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002683 } else {
2684 break;
2685 }
2686
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08002687 const Instruction& instance_of_inst = code_item_accessor_.InstructionAt(instance_of_idx);
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002688
2689 /* Check for peep-hole pattern of:
2690 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002691 * instance-of vX, vY, T;
2692 * ifXXX vX, label ;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002693 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002694 * label:
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002695 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002696 * and sharpen the type of vY to be type T.
2697 * Note, this pattern can't be if:
2698 * - if there are other branches to this branch,
2699 * - when vX == vY.
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002700 */
Ian Rogersfae370a2013-06-05 08:33:27 -07002701 if (!CurrentInsnFlags()->IsBranchTarget() &&
Vladimir Markod7559b72017-09-28 13:50:37 +01002702 (Instruction::INSTANCE_OF == instance_of_inst.Opcode()) &&
2703 (inst->VRegA_21t() == instance_of_inst.VRegA_22c()) &&
2704 (instance_of_inst.VRegA_22c() != instance_of_inst.VRegB_22c())) {
Ian Rogersebbdd872014-07-07 23:53:08 -07002705 // Check the type of the instance-of is different than that of registers type, as if they
2706 // are the same there is no work to be done here. Check that the conversion is not to or
2707 // from an unresolved type as type information is imprecise. If the instance-of is to an
2708 // interface then ignore the type information as interfaces can only be treated as Objects
2709 // and we don't want to disallow field and other operations on the object. If the value
2710 // being instance-of checked against is known null (zero) then allow the optimization as
2711 // we didn't have type information. If the merge of the instance-of type with the original
2712 // type is assignable to the original then allow optimization. This check is performed to
2713 // ensure that subsequent merges don't lose type information - such as becoming an
2714 // interface from a class that would lose information relevant to field checks.
Vladimir Markod7559b72017-09-28 13:50:37 +01002715 const RegType& orig_type = work_line_->GetRegisterType(this, instance_of_inst.VRegB_22c());
Andreas Gampe98be1a92017-08-28 08:25:45 -07002716 const RegType& cast_type = ResolveClass<CheckAccess::kYes>(
Vladimir Markod7559b72017-09-28 13:50:37 +01002717 dex::TypeIndex(instance_of_inst.VRegC_22c()));
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002718
Ian Rogersebbdd872014-07-07 23:53:08 -07002719 if (!orig_type.Equals(cast_type) &&
2720 !cast_type.IsUnresolvedTypes() && !orig_type.IsUnresolvedTypes() &&
Andreas Gampe00633eb2014-07-17 16:13:35 -07002721 cast_type.HasClass() && // Could be conflict type, make sure it has a class.
Ian Rogersebbdd872014-07-07 23:53:08 -07002722 !cast_type.GetClass()->IsInterface() &&
Andreas Gampeeed3a5d2017-11-29 14:58:34 -08002723 (orig_type.IsZeroOrNull() ||
David Brazdilca3c8c32016-09-06 14:04:48 +01002724 orig_type.IsStrictlyAssignableFrom(
2725 cast_type.Merge(orig_type, &reg_types_, this), this))) {
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08002726 RegisterLine* update_line = RegisterLine::Create(code_item_accessor_.RegistersSize(),
2727 this);
Ian Rogersfae370a2013-06-05 08:33:27 -07002728 if (inst->Opcode() == Instruction::IF_EQZ) {
Ian Rogers9b360392013-06-06 14:45:07 -07002729 fallthrough_line.reset(update_line);
Ian Rogersfae370a2013-06-05 08:33:27 -07002730 } else {
Ian Rogers9b360392013-06-06 14:45:07 -07002731 branch_line.reset(update_line);
2732 }
2733 update_line->CopyFromLine(work_line_.get());
Andreas Gampead238ce2015-08-24 21:13:08 -07002734 update_line->SetRegisterType<LockOp::kKeep>(this,
Vladimir Markod7559b72017-09-28 13:50:37 +01002735 instance_of_inst.VRegB_22c(),
Andreas Gampead238ce2015-08-24 21:13:08 -07002736 cast_type);
Mathieu Chartierde40d472015-10-15 17:47:48 -07002737 if (!GetInstructionFlags(instance_of_idx).IsBranchTarget() && 0 != instance_of_idx) {
Ian Rogers9b360392013-06-06 14:45:07 -07002738 // See if instance-of was preceded by a move-object operation, common due to the small
2739 // register encoding space of instance-of, and propagate type information to the source
2740 // of the move-object.
2741 uint32_t move_idx = instance_of_idx - 1;
Mathieu Chartierde40d472015-10-15 17:47:48 -07002742 while (0 != move_idx && !GetInstructionFlags(move_idx).IsOpcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07002743 move_idx--;
2744 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07002745 if (FailOrAbort(this, GetInstructionFlags(move_idx).IsOpcode(),
Andreas Gampe7c038102014-10-27 20:08:46 -07002746 "Unable to get previous instruction of if-eqz/if-nez for work index ",
2747 work_insn_idx_)) {
2748 break;
2749 }
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08002750 const Instruction& move_inst = code_item_accessor_.InstructionAt(move_idx);
Vladimir Markod7559b72017-09-28 13:50:37 +01002751 switch (move_inst.Opcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07002752 case Instruction::MOVE_OBJECT:
Vladimir Markod7559b72017-09-28 13:50:37 +01002753 if (move_inst.VRegA_12x() == instance_of_inst.VRegB_22c()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07002754 update_line->SetRegisterType<LockOp::kKeep>(this,
Vladimir Markod7559b72017-09-28 13:50:37 +01002755 move_inst.VRegB_12x(),
Andreas Gampead238ce2015-08-24 21:13:08 -07002756 cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002757 }
2758 break;
2759 case Instruction::MOVE_OBJECT_FROM16:
Vladimir Markod7559b72017-09-28 13:50:37 +01002760 if (move_inst.VRegA_22x() == instance_of_inst.VRegB_22c()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07002761 update_line->SetRegisterType<LockOp::kKeep>(this,
Vladimir Markod7559b72017-09-28 13:50:37 +01002762 move_inst.VRegB_22x(),
Andreas Gampead238ce2015-08-24 21:13:08 -07002763 cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002764 }
2765 break;
2766 case Instruction::MOVE_OBJECT_16:
Vladimir Markod7559b72017-09-28 13:50:37 +01002767 if (move_inst.VRegA_32x() == instance_of_inst.VRegB_22c()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07002768 update_line->SetRegisterType<LockOp::kKeep>(this,
Vladimir Markod7559b72017-09-28 13:50:37 +01002769 move_inst.VRegB_32x(),
Andreas Gampead238ce2015-08-24 21:13:08 -07002770 cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002771 }
2772 break;
2773 default:
2774 break;
2775 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002776 }
2777 }
2778 }
2779
jeffhaobdb76512011-09-07 11:43:16 -07002780 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002781 }
jeffhaobdb76512011-09-07 11:43:16 -07002782 case Instruction::IF_LTZ:
2783 case Instruction::IF_GEZ:
2784 case Instruction::IF_GTZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002785 case Instruction::IF_LEZ: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002786 const RegType& reg_type = work_line_->GetRegisterType(this, inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002787 if (!reg_type.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002788 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
2789 << " unexpected as arg to if-ltz/if-gez/if-gtz/if-lez";
Ian Rogersd81871c2011-10-03 13:57:23 -07002790 }
jeffhaobdb76512011-09-07 11:43:16 -07002791 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002792 }
jeffhaobdb76512011-09-07 11:43:16 -07002793 case Instruction::AGET_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002794 VerifyAGet(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002795 break;
jeffhaobdb76512011-09-07 11:43:16 -07002796 case Instruction::AGET_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002797 VerifyAGet(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002798 break;
jeffhaobdb76512011-09-07 11:43:16 -07002799 case Instruction::AGET_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002800 VerifyAGet(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002801 break;
jeffhaobdb76512011-09-07 11:43:16 -07002802 case Instruction::AGET_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002803 VerifyAGet(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002804 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002805 case Instruction::AGET:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002806 VerifyAGet(inst, reg_types_.Integer(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002807 break;
jeffhaobdb76512011-09-07 11:43:16 -07002808 case Instruction::AGET_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002809 VerifyAGet(inst, reg_types_.LongLo(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002810 break;
2811 case Instruction::AGET_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002812 VerifyAGet(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07002813 break;
2814
Ian Rogersd81871c2011-10-03 13:57:23 -07002815 case Instruction::APUT_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002816 VerifyAPut(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002817 break;
2818 case Instruction::APUT_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002819 VerifyAPut(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002820 break;
2821 case Instruction::APUT_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002822 VerifyAPut(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002823 break;
2824 case Instruction::APUT_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002825 VerifyAPut(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002826 break;
2827 case Instruction::APUT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002828 VerifyAPut(inst, reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002829 break;
2830 case Instruction::APUT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002831 VerifyAPut(inst, reg_types_.LongLo(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002832 break;
2833 case Instruction::APUT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002834 VerifyAPut(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07002835 break;
2836
jeffhaobdb76512011-09-07 11:43:16 -07002837 case Instruction::IGET_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002838 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002839 break;
jeffhaobdb76512011-09-07 11:43:16 -07002840 case Instruction::IGET_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002841 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002842 break;
jeffhaobdb76512011-09-07 11:43:16 -07002843 case Instruction::IGET_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002844 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002845 break;
jeffhaobdb76512011-09-07 11:43:16 -07002846 case Instruction::IGET_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002847 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002848 break;
2849 case Instruction::IGET:
Andreas Gampe896df402014-10-20 22:25:29 -07002850 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002851 break;
2852 case Instruction::IGET_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002853 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002854 break;
2855 case Instruction::IGET_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002856 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false,
2857 false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002858 break;
jeffhaobdb76512011-09-07 11:43:16 -07002859
Ian Rogersd81871c2011-10-03 13:57:23 -07002860 case Instruction::IPUT_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002861 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002862 break;
2863 case Instruction::IPUT_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002864 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002865 break;
2866 case Instruction::IPUT_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002867 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002868 break;
2869 case Instruction::IPUT_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002870 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002871 break;
2872 case Instruction::IPUT:
Andreas Gampe896df402014-10-20 22:25:29 -07002873 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002874 break;
2875 case Instruction::IPUT_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002876 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002877 break;
jeffhaobdb76512011-09-07 11:43:16 -07002878 case Instruction::IPUT_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002879 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false,
2880 false);
jeffhaobdb76512011-09-07 11:43:16 -07002881 break;
2882
jeffhaobdb76512011-09-07 11:43:16 -07002883 case Instruction::SGET_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002884 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002885 break;
jeffhaobdb76512011-09-07 11:43:16 -07002886 case Instruction::SGET_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002887 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002888 break;
jeffhaobdb76512011-09-07 11:43:16 -07002889 case Instruction::SGET_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002890 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002891 break;
jeffhaobdb76512011-09-07 11:43:16 -07002892 case Instruction::SGET_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002893 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002894 break;
2895 case Instruction::SGET:
Andreas Gampe896df402014-10-20 22:25:29 -07002896 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002897 break;
2898 case Instruction::SGET_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002899 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002900 break;
2901 case Instruction::SGET_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002902 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false,
2903 true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002904 break;
2905
2906 case Instruction::SPUT_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002907 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002908 break;
2909 case Instruction::SPUT_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002910 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002911 break;
2912 case Instruction::SPUT_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002913 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002914 break;
2915 case Instruction::SPUT_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002916 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002917 break;
2918 case Instruction::SPUT:
Andreas Gampe896df402014-10-20 22:25:29 -07002919 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002920 break;
2921 case Instruction::SPUT_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002922 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002923 break;
2924 case Instruction::SPUT_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002925 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false,
2926 true);
jeffhaobdb76512011-09-07 11:43:16 -07002927 break;
2928
2929 case Instruction::INVOKE_VIRTUAL:
2930 case Instruction::INVOKE_VIRTUAL_RANGE:
2931 case Instruction::INVOKE_SUPER:
Ian Rogersd81871c2011-10-03 13:57:23 -07002932 case Instruction::INVOKE_SUPER_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002933 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE ||
2934 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002935 bool is_super = (inst->Opcode() == Instruction::INVOKE_SUPER ||
2936 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
Alex Light7268d472016-01-20 15:50:01 -08002937 MethodType type = is_super ? METHOD_SUPER : METHOD_VIRTUAL;
2938 ArtMethod* called_method = VerifyInvocationArgs(inst, type, is_range);
Ian Rogersd8f69b02014-09-10 21:43:52 +00002939 const RegType* return_type = nullptr;
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002940 if (called_method != nullptr) {
Vladimir Markob45528c2017-07-27 14:14:28 +01002941 ObjPtr<mirror::Class> return_type_class = can_load_classes_
2942 ? called_method->ResolveReturnType()
2943 : called_method->LookupResolvedReturnType();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002944 if (return_type_class != nullptr) {
Andreas Gampef23f33d2015-06-23 14:18:17 -07002945 return_type = &FromClass(called_method->GetReturnTypeDescriptor(),
Vladimir Markob45528c2017-07-27 14:14:28 +01002946 return_type_class.Ptr(),
Andreas Gampef23f33d2015-06-23 14:18:17 -07002947 return_type_class->CannotBeAssignedFromOtherTypes());
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002948 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07002949 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
2950 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002951 }
2952 }
2953 if (return_type == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002954 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002955 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
Andreas Gampea5b09a62016-11-17 15:21:22 -08002956 dex::TypeIndex return_type_idx =
2957 dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002958 const char* descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Mathieu Chartierbf99f772014-08-23 16:37:27 -07002959 return_type = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
jeffhaobdb76512011-09-07 11:43:16 -07002960 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002961 if (!return_type->IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002962 work_line_->SetResultRegisterType(this, *return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002963 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002964 work_line_->SetResultRegisterTypeWide(*return_type, return_type->HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002965 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002966 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002967 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002968 }
jeffhaobdb76512011-09-07 11:43:16 -07002969 case Instruction::INVOKE_DIRECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002970 case Instruction::INVOKE_DIRECT_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002971 bool is_range = (inst->Opcode() == Instruction::INVOKE_DIRECT_RANGE);
Alex Light7268d472016-01-20 15:50:01 -08002972 ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_DIRECT, is_range);
Ian Rogers46685432012-06-03 22:26:43 -07002973 const char* return_type_descriptor;
2974 bool is_constructor;
Ian Rogersd8f69b02014-09-10 21:43:52 +00002975 const RegType* return_type = nullptr;
Ian Rogers7b078e82014-09-10 14:44:24 -07002976 if (called_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002977 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogers46685432012-06-03 22:26:43 -07002978 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
Ian Rogersdfb325e2013-10-30 01:00:44 -07002979 is_constructor = strcmp("<init>", dex_file_->StringDataByIdx(method_id.name_idx_)) == 0;
Andreas Gampea5b09a62016-11-17 15:21:22 -08002980 dex::TypeIndex return_type_idx =
2981 dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogers46685432012-06-03 22:26:43 -07002982 return_type_descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2983 } else {
2984 is_constructor = called_method->IsConstructor();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002985 return_type_descriptor = called_method->GetReturnTypeDescriptor();
Vladimir Markob45528c2017-07-27 14:14:28 +01002986 ObjPtr<mirror::Class> return_type_class = can_load_classes_
2987 ? called_method->ResolveReturnType()
2988 : called_method->LookupResolvedReturnType();
Ian Rogers1ff3c982014-08-12 02:30:58 -07002989 if (return_type_class != nullptr) {
Andreas Gampef23f33d2015-06-23 14:18:17 -07002990 return_type = &FromClass(return_type_descriptor,
Vladimir Markob45528c2017-07-27 14:14:28 +01002991 return_type_class.Ptr(),
Andreas Gampef23f33d2015-06-23 14:18:17 -07002992 return_type_class->CannotBeAssignedFromOtherTypes());
Ian Rogers1ff3c982014-08-12 02:30:58 -07002993 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07002994 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
2995 self_->ClearException();
Ian Rogers1ff3c982014-08-12 02:30:58 -07002996 }
Ian Rogers46685432012-06-03 22:26:43 -07002997 }
2998 if (is_constructor) {
jeffhaobdb76512011-09-07 11:43:16 -07002999 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003000 * Some additional checks when calling a constructor. We know from the invocation arg check
3001 * that the "this" argument is an instance of called_method->klass. Now we further restrict
3002 * that to require that called_method->klass is the same as this->klass or this->super,
3003 * allowing the latter only if the "this" argument is the same as the "this" argument to
3004 * this method (which implies that we're in a constructor ourselves).
jeffhaobdb76512011-09-07 11:43:16 -07003005 */
Orion Hodsoncfa325e2016-10-13 10:25:54 +01003006 const RegType& this_type = work_line_->GetInvocationThis(this, inst);
jeffhaob57e9522012-04-26 18:08:21 -07003007 if (this_type.IsConflict()) // failure.
3008 break;
jeffhaobdb76512011-09-07 11:43:16 -07003009
jeffhaob57e9522012-04-26 18:08:21 -07003010 /* no null refs allowed (?) */
Andreas Gampeeed3a5d2017-11-29 14:58:34 -08003011 if (this_type.IsZeroOrNull()) {
jeffhaob57e9522012-04-26 18:08:21 -07003012 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unable to initialize null ref";
3013 break;
jeffhao2a8a90e2011-09-26 14:25:31 -07003014 }
jeffhaob57e9522012-04-26 18:08:21 -07003015
3016 /* must be in same class or in superclass */
Ian Rogersd8f69b02014-09-10 21:43:52 +00003017 // const RegType& this_super_klass = this_type.GetSuperClass(&reg_types_);
Ian Rogers46685432012-06-03 22:26:43 -07003018 // TODO: re-enable constructor type verification
3019 // if (this_super_klass.IsConflict()) {
jeffhaob57e9522012-04-26 18:08:21 -07003020 // Unknown super class, fail so we re-check at runtime.
Ian Rogers46685432012-06-03 22:26:43 -07003021 // Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "super class unknown for '" << this_type << "'";
3022 // break;
3023 // }
jeffhaob57e9522012-04-26 18:08:21 -07003024
3025 /* arg must be an uninitialized reference */
3026 if (!this_type.IsUninitializedTypes()) {
3027 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Expected initialization on uninitialized reference "
3028 << this_type;
3029 break;
3030 }
3031
3032 /*
3033 * Replace the uninitialized reference with an initialized one. We need to do this for all
3034 * registers that have the same object instance in them, not just the "this" register.
3035 */
Nicolas Geoffray98e6ce42016-02-16 18:42:15 +00003036 work_line_->MarkRefsAsInitialized(this, this_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003037 }
Ian Rogers1ff3c982014-08-12 02:30:58 -07003038 if (return_type == nullptr) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07003039 return_type = &reg_types_.FromDescriptor(GetClassLoader(), return_type_descriptor, false);
Ian Rogers1ff3c982014-08-12 02:30:58 -07003040 }
3041 if (!return_type->IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003042 work_line_->SetResultRegisterType(this, *return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003043 } else {
Ian Rogers1ff3c982014-08-12 02:30:58 -07003044 work_line_->SetResultRegisterTypeWide(*return_type, return_type->HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003045 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003046 just_set_result = true;
3047 break;
3048 }
3049 case Instruction::INVOKE_STATIC:
3050 case Instruction::INVOKE_STATIC_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003051 bool is_range = (inst->Opcode() == Instruction::INVOKE_STATIC_RANGE);
Alex Light7268d472016-01-20 15:50:01 -08003052 ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_STATIC, is_range);
Ian Rogers28ad40d2011-10-27 15:19:26 -07003053 const char* descriptor;
Ian Rogers7b078e82014-09-10 14:44:24 -07003054 if (called_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003055 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogers28ad40d2011-10-27 15:19:26 -07003056 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
Andreas Gampea5b09a62016-11-17 15:21:22 -08003057 dex::TypeIndex return_type_idx =
3058 dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Mathieu Chartier590fee92013-09-13 13:46:47 -07003059 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07003060 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003061 descriptor = called_method->GetReturnTypeDescriptor();
Ian Rogers28ad40d2011-10-27 15:19:26 -07003062 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00003063 const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003064 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003065 work_line_->SetResultRegisterType(this, return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003066 } else {
3067 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
3068 }
jeffhaobdb76512011-09-07 11:43:16 -07003069 just_set_result = true;
3070 }
3071 break;
jeffhaobdb76512011-09-07 11:43:16 -07003072 case Instruction::INVOKE_INTERFACE:
Ian Rogersd81871c2011-10-03 13:57:23 -07003073 case Instruction::INVOKE_INTERFACE_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003074 bool is_range = (inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE);
Alex Light7268d472016-01-20 15:50:01 -08003075 ArtMethod* abs_method = VerifyInvocationArgs(inst, METHOD_INTERFACE, is_range);
Ian Rogers7b078e82014-09-10 14:44:24 -07003076 if (abs_method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003077 mirror::Class* called_interface = abs_method->GetDeclaringClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07003078 if (!called_interface->IsInterface() && !called_interface->IsObjectClass()) {
3079 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected interface class in invoke-interface '"
David Sehr709b0702016-10-13 09:12:37 -07003080 << abs_method->PrettyMethod() << "'";
Ian Rogersad0b3a32012-04-16 14:50:24 -07003081 break;
Ian Rogers28ad40d2011-10-27 15:19:26 -07003082 }
Ian Rogers0d604842012-04-16 14:50:24 -07003083 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003084 /* Get the type of the "this" arg, which should either be a sub-interface of called
3085 * interface or Object (see comments in RegType::JoinClass).
3086 */
Orion Hodsoncfa325e2016-10-13 10:25:54 +01003087 const RegType& this_type = work_line_->GetInvocationThis(this, inst);
Andreas Gampeeed3a5d2017-11-29 14:58:34 -08003088 if (this_type.IsZeroOrNull()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003089 /* null pointer always passes (and always fails at runtime) */
3090 } else {
3091 if (this_type.IsUninitializedTypes()) {
3092 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface call on uninitialized object "
3093 << this_type;
3094 break;
3095 }
3096 // In the past we have tried to assert that "called_interface" is assignable
3097 // from "this_type.GetClass()", however, as we do an imprecise Join
3098 // (RegType::JoinClass) we don't have full information on what interfaces are
3099 // implemented by "this_type". For example, two classes may implement the same
3100 // interfaces and have a common parent that doesn't implement the interface. The
3101 // join will set "this_type" to the parent class and a test that this implements
3102 // the interface will incorrectly fail.
3103 }
3104 /*
3105 * We don't have an object instance, so we can't find the concrete method. However, all of
3106 * the type information is in the abstract method, so we're good.
3107 */
3108 const char* descriptor;
Ian Rogers7b078e82014-09-10 14:44:24 -07003109 if (abs_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003110 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07003111 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
Andreas Gampea5b09a62016-11-17 15:21:22 -08003112 dex::TypeIndex return_type_idx =
3113 dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Mathieu Chartierde40d472015-10-15 17:47:48 -07003114 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003115 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003116 descriptor = abs_method->GetReturnTypeDescriptor();
Ian Rogersad0b3a32012-04-16 14:50:24 -07003117 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00003118 const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003119 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003120 work_line_->SetResultRegisterType(this, return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003121 } else {
3122 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
3123 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003124 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07003125 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07003126 }
Narayan Kamath9823e782016-08-03 12:46:58 +01003127 case Instruction::INVOKE_POLYMORPHIC:
3128 case Instruction::INVOKE_POLYMORPHIC_RANGE: {
Orion Hodsoncfa325e2016-10-13 10:25:54 +01003129 bool is_range = (inst->Opcode() == Instruction::INVOKE_POLYMORPHIC_RANGE);
3130 ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_POLYMORPHIC, is_range);
3131 if (called_method == nullptr) {
3132 // Convert potential soft failures in VerifyInvocationArgs() to hard errors.
3133 if (failure_messages_.size() > 0) {
3134 std::string message = failure_messages_.back()->str();
3135 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << message;
3136 } else {
3137 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invoke-polymorphic verification failure.";
3138 }
3139 break;
3140 }
3141 if (!CheckSignaturePolymorphicMethod(called_method) ||
3142 !CheckSignaturePolymorphicReceiver(inst)) {
3143 break;
3144 }
3145 const uint32_t proto_idx = (is_range) ? inst->VRegH_4rcc() : inst->VRegH_45cc();
Orion Hodsonac141392017-01-13 11:53:47 +00003146 const char* return_descriptor =
Orion Hodsoncfa325e2016-10-13 10:25:54 +01003147 dex_file_->GetReturnTypeDescriptor(dex_file_->GetProtoId(proto_idx));
3148 const RegType& return_type =
Orion Hodsonac141392017-01-13 11:53:47 +00003149 reg_types_.FromDescriptor(GetClassLoader(), return_descriptor, false);
Orion Hodsoncfa325e2016-10-13 10:25:54 +01003150 if (!return_type.IsLowHalf()) {
3151 work_line_->SetResultRegisterType(this, return_type);
3152 } else {
3153 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
3154 }
Orion Hodsonac141392017-01-13 11:53:47 +00003155 just_set_result = true;
Orion Hodsoncfa325e2016-10-13 10:25:54 +01003156 break;
Narayan Kamath9823e782016-08-03 12:46:58 +01003157 }
Orion Hodsonc069a302017-01-18 09:23:12 +00003158 case Instruction::INVOKE_CUSTOM:
3159 case Instruction::INVOKE_CUSTOM_RANGE: {
3160 // Verify registers based on method_type in the call site.
3161 bool is_range = (inst->Opcode() == Instruction::INVOKE_CUSTOM_RANGE);
3162
3163 // Step 1. Check the call site that produces the method handle for invocation
3164 const uint32_t call_site_idx = is_range ? inst->VRegB_3rc() : inst->VRegB_35c();
3165 if (!CheckCallSite(call_site_idx)) {
3166 DCHECK(HasFailures());
3167 break;
3168 }
3169
3170 // Step 2. Check the register arguments correspond to the expected arguments for the
3171 // method handle produced by step 1. The dex file verifier has checked ranges for
3172 // the first three arguments and CheckCallSite has checked the method handle type.
3173 CallSiteArrayValueIterator it(*dex_file_, dex_file_->GetCallSiteId(call_site_idx));
3174 it.Next(); // Skip to name.
3175 it.Next(); // Skip to method type of the method handle
3176 const uint32_t proto_idx = static_cast<uint32_t>(it.GetJavaValue().i);
3177 const DexFile::ProtoId& proto_id = dex_file_->GetProtoId(proto_idx);
3178 DexFileParameterIterator param_it(*dex_file_, proto_id);
3179 // Treat method as static as it has yet to be determined.
3180 VerifyInvocationArgsFromIterator(&param_it, inst, METHOD_STATIC, is_range, nullptr);
3181 const char* return_descriptor = dex_file_->GetReturnTypeDescriptor(proto_id);
3182
3183 // Step 3. Propagate return type information
3184 const RegType& return_type =
3185 reg_types_.FromDescriptor(GetClassLoader(), return_descriptor, false);
3186 if (!return_type.IsLowHalf()) {
3187 work_line_->SetResultRegisterType(this, return_type);
3188 } else {
3189 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
3190 }
3191 just_set_result = true;
3192 // TODO: Add compiler support for invoke-custom (b/35337872).
3193 Fail(VERIFY_ERROR_FORCE_INTERPRETER);
3194 break;
3195 }
jeffhaobdb76512011-09-07 11:43:16 -07003196 case Instruction::NEG_INT:
3197 case Instruction::NOT_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003198 work_line_->CheckUnaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003199 break;
3200 case Instruction::NEG_LONG:
3201 case Instruction::NOT_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07003202 work_line_->CheckUnaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003203 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07003204 break;
3205 case Instruction::NEG_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003206 work_line_->CheckUnaryOp(this, inst, reg_types_.Float(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07003207 break;
3208 case Instruction::NEG_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07003209 work_line_->CheckUnaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003210 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07003211 break;
3212 case Instruction::INT_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07003213 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003214 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003215 break;
3216 case Instruction::INT_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003217 work_line_->CheckUnaryOp(this, inst, reg_types_.Float(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003218 break;
3219 case Instruction::INT_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07003220 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003221 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003222 break;
3223 case Instruction::LONG_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003224 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003225 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07003226 break;
3227 case Instruction::LONG_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003228 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003229 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07003230 break;
3231 case Instruction::LONG_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07003232 work_line_->CheckUnaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003233 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07003234 break;
3235 case Instruction::FLOAT_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003236 work_line_->CheckUnaryOp(this, inst, reg_types_.Integer(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07003237 break;
3238 case Instruction::FLOAT_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07003239 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003240 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07003241 break;
3242 case Instruction::FLOAT_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07003243 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003244 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07003245 break;
3246 case Instruction::DOUBLE_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003247 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003248 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07003249 break;
3250 case Instruction::DOUBLE_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07003251 work_line_->CheckUnaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003252 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07003253 break;
3254 case Instruction::DOUBLE_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003255 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003256 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07003257 break;
3258 case Instruction::INT_TO_BYTE:
Ian Rogers7b078e82014-09-10 14:44:24 -07003259 work_line_->CheckUnaryOp(this, inst, reg_types_.Byte(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003260 break;
3261 case Instruction::INT_TO_CHAR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003262 work_line_->CheckUnaryOp(this, inst, reg_types_.Char(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003263 break;
3264 case Instruction::INT_TO_SHORT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003265 work_line_->CheckUnaryOp(this, inst, reg_types_.Short(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003266 break;
3267
3268 case Instruction::ADD_INT:
3269 case Instruction::SUB_INT:
3270 case Instruction::MUL_INT:
3271 case Instruction::REM_INT:
3272 case Instruction::DIV_INT:
3273 case Instruction::SHL_INT:
3274 case Instruction::SHR_INT:
3275 case Instruction::USHR_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003276 work_line_->CheckBinaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003277 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07003278 break;
3279 case Instruction::AND_INT:
3280 case Instruction::OR_INT:
3281 case Instruction::XOR_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003282 work_line_->CheckBinaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003283 reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07003284 break;
3285 case Instruction::ADD_LONG:
3286 case Instruction::SUB_LONG:
3287 case Instruction::MUL_LONG:
3288 case Instruction::DIV_LONG:
3289 case Instruction::REM_LONG:
3290 case Instruction::AND_LONG:
3291 case Instruction::OR_LONG:
3292 case Instruction::XOR_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07003293 work_line_->CheckBinaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003294 reg_types_.LongLo(), reg_types_.LongHi(),
3295 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07003296 break;
3297 case Instruction::SHL_LONG:
3298 case Instruction::SHR_LONG:
3299 case Instruction::USHR_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07003300 /* shift distance is Int, making these different from other binary operations */
Ian Rogers7b078e82014-09-10 14:44:24 -07003301 work_line_->CheckBinaryOpWideShift(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003302 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003303 break;
3304 case Instruction::ADD_FLOAT:
3305 case Instruction::SUB_FLOAT:
3306 case Instruction::MUL_FLOAT:
3307 case Instruction::DIV_FLOAT:
3308 case Instruction::REM_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003309 work_line_->CheckBinaryOp(this, inst, reg_types_.Float(), reg_types_.Float(),
3310 reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07003311 break;
3312 case Instruction::ADD_DOUBLE:
3313 case Instruction::SUB_DOUBLE:
3314 case Instruction::MUL_DOUBLE:
3315 case Instruction::DIV_DOUBLE:
3316 case Instruction::REM_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07003317 work_line_->CheckBinaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003318 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
3319 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07003320 break;
3321 case Instruction::ADD_INT_2ADDR:
3322 case Instruction::SUB_INT_2ADDR:
3323 case Instruction::MUL_INT_2ADDR:
3324 case Instruction::REM_INT_2ADDR:
3325 case Instruction::SHL_INT_2ADDR:
3326 case Instruction::SHR_INT_2ADDR:
3327 case Instruction::USHR_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003328 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
3329 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07003330 break;
3331 case Instruction::AND_INT_2ADDR:
3332 case Instruction::OR_INT_2ADDR:
3333 case Instruction::XOR_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003334 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
3335 reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07003336 break;
3337 case Instruction::DIV_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003338 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
3339 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07003340 break;
3341 case Instruction::ADD_LONG_2ADDR:
3342 case Instruction::SUB_LONG_2ADDR:
3343 case Instruction::MUL_LONG_2ADDR:
3344 case Instruction::DIV_LONG_2ADDR:
3345 case Instruction::REM_LONG_2ADDR:
3346 case Instruction::AND_LONG_2ADDR:
3347 case Instruction::OR_LONG_2ADDR:
3348 case Instruction::XOR_LONG_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003349 work_line_->CheckBinaryOp2addrWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003350 reg_types_.LongLo(), reg_types_.LongHi(),
3351 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07003352 break;
3353 case Instruction::SHL_LONG_2ADDR:
3354 case Instruction::SHR_LONG_2ADDR:
3355 case Instruction::USHR_LONG_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003356 work_line_->CheckBinaryOp2addrWideShift(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003357 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003358 break;
3359 case Instruction::ADD_FLOAT_2ADDR:
3360 case Instruction::SUB_FLOAT_2ADDR:
3361 case Instruction::MUL_FLOAT_2ADDR:
3362 case Instruction::DIV_FLOAT_2ADDR:
3363 case Instruction::REM_FLOAT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003364 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Float(), reg_types_.Float(),
3365 reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07003366 break;
3367 case Instruction::ADD_DOUBLE_2ADDR:
3368 case Instruction::SUB_DOUBLE_2ADDR:
3369 case Instruction::MUL_DOUBLE_2ADDR:
3370 case Instruction::DIV_DOUBLE_2ADDR:
3371 case Instruction::REM_DOUBLE_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003372 work_line_->CheckBinaryOp2addrWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003373 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
3374 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07003375 break;
3376 case Instruction::ADD_INT_LIT16:
Ian Rogersf72a11d2014-10-30 15:41:08 -07003377 case Instruction::RSUB_INT_LIT16:
jeffhaobdb76512011-09-07 11:43:16 -07003378 case Instruction::MUL_INT_LIT16:
3379 case Instruction::DIV_INT_LIT16:
3380 case Instruction::REM_INT_LIT16:
Ian Rogers7b078e82014-09-10 14:44:24 -07003381 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), false,
3382 true);
jeffhaobdb76512011-09-07 11:43:16 -07003383 break;
3384 case Instruction::AND_INT_LIT16:
3385 case Instruction::OR_INT_LIT16:
3386 case Instruction::XOR_INT_LIT16:
Ian Rogers7b078e82014-09-10 14:44:24 -07003387 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), true,
3388 true);
jeffhaobdb76512011-09-07 11:43:16 -07003389 break;
3390 case Instruction::ADD_INT_LIT8:
3391 case Instruction::RSUB_INT_LIT8:
3392 case Instruction::MUL_INT_LIT8:
3393 case Instruction::DIV_INT_LIT8:
3394 case Instruction::REM_INT_LIT8:
3395 case Instruction::SHL_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07003396 case Instruction::SHR_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07003397 case Instruction::USHR_INT_LIT8:
Ian Rogers7b078e82014-09-10 14:44:24 -07003398 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), false,
3399 false);
jeffhaobdb76512011-09-07 11:43:16 -07003400 break;
3401 case Instruction::AND_INT_LIT8:
3402 case Instruction::OR_INT_LIT8:
3403 case Instruction::XOR_INT_LIT8:
Ian Rogers7b078e82014-09-10 14:44:24 -07003404 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), true,
3405 false);
jeffhaobdb76512011-09-07 11:43:16 -07003406 break;
3407
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003408 // Special instructions.
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07003409 case Instruction::RETURN_VOID_NO_BARRIER:
3410 if (IsConstructor() && !IsStatic()) {
3411 auto& declaring_class = GetDeclaringClass();
Andreas Gampe68df3202015-06-22 11:35:46 -07003412 if (declaring_class.IsUnresolvedReference()) {
3413 // We must iterate over the fields, even if we cannot use mirror classes to do so. Do it
3414 // manually over the underlying dex file.
3415 uint32_t first_index = GetFirstFinalInstanceFieldIndex(*dex_file_,
3416 dex_file_->GetMethodId(dex_method_idx_).class_idx_);
Andreas Gampee2abbc62017-09-15 11:59:26 -07003417 if (first_index != dex::kDexNoIndex) {
Andreas Gampe68df3202015-06-22 11:35:46 -07003418 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void-no-barrier not expected for field "
3419 << first_index;
3420 }
3421 break;
3422 }
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07003423 auto* klass = declaring_class.GetClass();
3424 for (uint32_t i = 0, num_fields = klass->NumInstanceFields(); i < num_fields; ++i) {
3425 if (klass->GetInstanceField(i)->IsFinal()) {
Mathieu Chartiere86deef2015-03-19 13:43:37 -07003426 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void-no-barrier not expected for "
David Sehr709b0702016-10-13 09:12:37 -07003427 << klass->GetInstanceField(i)->PrettyField();
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07003428 break;
3429 }
3430 }
Sebastien Hertzcc10e0e2013-06-28 14:24:48 +02003431 }
Andreas Gampeb2917962015-07-31 13:36:10 -07003432 // Handle this like a RETURN_VOID now. Code is duplicated to separate standard from
3433 // quickened opcodes (otherwise this could be a fall-through).
3434 if (!IsConstructor()) {
3435 if (!GetMethodReturnType().IsConflict()) {
3436 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void not expected";
3437 }
3438 }
Sebastien Hertzcc10e0e2013-06-28 14:24:48 +02003439 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003440 // Note: the following instructions encode offsets derived from class linking.
Neil Fuller0e844392016-09-08 13:43:31 +01003441 // As such they use Class*/Field*/Executable* as these offsets only have
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003442 // meaning if the class linking and resolution were successful.
3443 case Instruction::IGET_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003444 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003445 break;
3446 case Instruction::IGET_WIDE_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003447 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003448 break;
3449 case Instruction::IGET_OBJECT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003450 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003451 break;
Mathieu Chartierffc605c2014-12-10 10:35:44 -08003452 case Instruction::IGET_BOOLEAN_QUICK:
3453 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true);
3454 break;
3455 case Instruction::IGET_BYTE_QUICK:
3456 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true);
3457 break;
3458 case Instruction::IGET_CHAR_QUICK:
3459 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true);
3460 break;
3461 case Instruction::IGET_SHORT_QUICK:
3462 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true);
3463 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003464 case Instruction::IPUT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003465 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003466 break;
Fred Shih37f05ef2014-07-16 18:38:08 -07003467 case Instruction::IPUT_BOOLEAN_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003468 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07003469 break;
3470 case Instruction::IPUT_BYTE_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003471 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07003472 break;
3473 case Instruction::IPUT_CHAR_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003474 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07003475 break;
3476 case Instruction::IPUT_SHORT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003477 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07003478 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003479 case Instruction::IPUT_WIDE_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003480 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003481 break;
3482 case Instruction::IPUT_OBJECT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003483 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003484 break;
3485 case Instruction::INVOKE_VIRTUAL_QUICK:
3486 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: {
3487 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
Mathieu Chartiere401d142015-04-22 13:56:20 -07003488 ArtMethod* called_method = VerifyInvokeVirtualQuickArgs(inst, is_range);
Ian Rogers7b078e82014-09-10 14:44:24 -07003489 if (called_method != nullptr) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003490 const char* descriptor = called_method->GetReturnTypeDescriptor();
Ian Rogersd8f69b02014-09-10 21:43:52 +00003491 const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003492 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003493 work_line_->SetResultRegisterType(this, return_type);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003494 } else {
3495 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
3496 }
3497 just_set_result = true;
3498 }
3499 break;
3500 }
3501
Ian Rogersd81871c2011-10-03 13:57:23 -07003502 /* These should never appear during verification. */
Mathieu Chartierffc605c2014-12-10 10:35:44 -08003503 case Instruction::UNUSED_3E ... Instruction::UNUSED_43:
Narayan Kamath8ec3bd22016-08-03 12:46:23 +01003504 case Instruction::UNUSED_F3 ... Instruction::UNUSED_F9:
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003505 case Instruction::UNUSED_79:
3506 case Instruction::UNUSED_7A:
jeffhaod5347e02012-03-22 17:25:05 -07003507 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Unexpected opcode " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07003508 break;
3509
3510 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003511 * DO NOT add a "default" clause here. Without it the compiler will
jeffhaobdb76512011-09-07 11:43:16 -07003512 * complain if an instruction is missing (which is desirable).
3513 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003514 } // end - switch (dec_insn.opcode)
jeffhaobdb76512011-09-07 11:43:16 -07003515
Ian Rogersad0b3a32012-04-16 14:50:24 -07003516 if (have_pending_hard_failure_) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08003517 if (Runtime::Current()->IsAotCompiler()) {
3518 /* When AOT compiling, check that the last failure is a hard failure */
Andreas Gampeb588f4c2015-05-26 13:35:39 -07003519 if (failures_[failures_.size() - 1] != VERIFY_ERROR_BAD_CLASS_HARD) {
3520 LOG(ERROR) << "Pending failures:";
3521 for (auto& error : failures_) {
3522 LOG(ERROR) << error;
3523 }
3524 for (auto& error_msg : failure_messages_) {
3525 LOG(ERROR) << error_msg->str();
3526 }
3527 LOG(FATAL) << "Pending hard failure, but last failure not hard.";
3528 }
Ian Rogerse1758fe2012-04-19 11:31:15 -07003529 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003530 /* immediate failure, reject class */
3531 info_messages_ << "Rejecting opcode " << inst->DumpString(dex_file_);
3532 return false;
jeffhaofaf459e2012-08-31 15:32:47 -07003533 } else if (have_pending_runtime_throw_failure_) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07003534 /* checking interpreter will throw, mark following code as unreachable */
jeffhaofaf459e2012-08-31 15:32:47 -07003535 opcode_flags = Instruction::kThrow;
Andreas Gampea727e372015-08-25 09:22:37 -07003536 // Note: the flag must be reset as it is only global to decouple Fail and is semantically per
3537 // instruction. However, RETURN checking may throw LOCKING errors, so we clear at the
3538 // very end.
jeffhaobdb76512011-09-07 11:43:16 -07003539 }
jeffhaobdb76512011-09-07 11:43:16 -07003540 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003541 * If we didn't just set the result register, clear it out. This ensures that you can only use
3542 * "move-result" immediately after the result is set. (We could check this statically, but it's
3543 * not expensive and it makes our debugging output cleaner.)
jeffhaobdb76512011-09-07 11:43:16 -07003544 */
3545 if (!just_set_result) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003546 work_line_->SetResultTypeToUnknown(this);
jeffhaobdb76512011-09-07 11:43:16 -07003547 }
3548
jeffhaobdb76512011-09-07 11:43:16 -07003549 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003550 * Handle "branch". Tag the branch target.
jeffhaobdb76512011-09-07 11:43:16 -07003551 *
3552 * NOTE: instructions like Instruction::EQZ provide information about the
jeffhaod1f0fde2011-09-08 17:25:33 -07003553 * state of the register when the branch is taken or not taken. For example,
jeffhaobdb76512011-09-07 11:43:16 -07003554 * somebody could get a reference field, check it for zero, and if the
3555 * branch is taken immediately store that register in a boolean field
jeffhaod1f0fde2011-09-08 17:25:33 -07003556 * since the value is known to be zero. We do not currently account for
jeffhaobdb76512011-09-07 11:43:16 -07003557 * that, and will reject the code.
3558 *
3559 * TODO: avoid re-fetching the branch target
3560 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003561 if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07003562 bool isConditional, selfOkay;
Ian Rogersd81871c2011-10-03 13:57:23 -07003563 if (!GetBranchOffset(work_insn_idx_, &branch_target, &isConditional, &selfOkay)) {
jeffhaobdb76512011-09-07 11:43:16 -07003564 /* should never happen after static verification */
jeffhaod5347e02012-03-22 17:25:05 -07003565 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad branch";
jeffhaobdb76512011-09-07 11:43:16 -07003566 return false;
3567 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08003568 DCHECK_EQ(isConditional, (opcode_flags & Instruction::kContinue) != 0);
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08003569 if (!CheckNotMoveExceptionOrMoveResult(code_item_accessor_.Insns(),
3570 work_insn_idx_ + branch_target)) {
jeffhaobdb76512011-09-07 11:43:16 -07003571 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07003572 }
jeffhaobdb76512011-09-07 11:43:16 -07003573 /* update branch target, set "changed" if appropriate */
Mathieu Chartierde40d472015-10-15 17:47:48 -07003574 if (nullptr != branch_line) {
Ian Rogersebbdd872014-07-07 23:53:08 -07003575 if (!UpdateRegisters(work_insn_idx_ + branch_target, branch_line.get(), false)) {
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003576 return false;
3577 }
3578 } else {
Ian Rogersebbdd872014-07-07 23:53:08 -07003579 if (!UpdateRegisters(work_insn_idx_ + branch_target, work_line_.get(), false)) {
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003580 return false;
3581 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003582 }
jeffhaobdb76512011-09-07 11:43:16 -07003583 }
3584
3585 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003586 * Handle "switch". Tag all possible branch targets.
jeffhaobdb76512011-09-07 11:43:16 -07003587 *
3588 * We've already verified that the table is structurally sound, so we
3589 * just need to walk through and tag the targets.
3590 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003591 if ((opcode_flags & Instruction::kSwitch) != 0) {
Andreas Gampe53de99c2015-08-17 13:43:55 -07003592 int offset_to_switch = insns[1] | (static_cast<int32_t>(insns[2]) << 16);
jeffhaobdb76512011-09-07 11:43:16 -07003593 const uint16_t* switch_insns = insns + offset_to_switch;
3594 int switch_count = switch_insns[1];
3595 int offset_to_targets, targ;
3596
3597 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
3598 /* 0 = sig, 1 = count, 2/3 = first key */
3599 offset_to_targets = 4;
3600 } else {
3601 /* 0 = sig, 1 = count, 2..count * 2 = keys */
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07003602 DCHECK((*insns & 0xff) == Instruction::SPARSE_SWITCH);
jeffhaobdb76512011-09-07 11:43:16 -07003603 offset_to_targets = 2 + 2 * switch_count;
3604 }
3605
3606 /* verify each switch target */
3607 for (targ = 0; targ < switch_count; targ++) {
3608 int offset;
3609 uint32_t abs_offset;
3610
3611 /* offsets are 32-bit, and only partly endian-swapped */
3612 offset = switch_insns[offset_to_targets + targ * 2] |
Andreas Gampe53de99c2015-08-17 13:43:55 -07003613 (static_cast<int32_t>(switch_insns[offset_to_targets + targ * 2 + 1]) << 16);
Ian Rogersd81871c2011-10-03 13:57:23 -07003614 abs_offset = work_insn_idx_ + offset;
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08003615 DCHECK_LT(abs_offset, code_item_accessor_.InsnsSizeInCodeUnits());
3616 if (!CheckNotMoveExceptionOrMoveResult(code_item_accessor_.Insns(), abs_offset)) {
jeffhaobdb76512011-09-07 11:43:16 -07003617 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07003618 }
Ian Rogersebbdd872014-07-07 23:53:08 -07003619 if (!UpdateRegisters(abs_offset, work_line_.get(), false)) {
jeffhaobdb76512011-09-07 11:43:16 -07003620 return false;
Ian Rogersebbdd872014-07-07 23:53:08 -07003621 }
jeffhaobdb76512011-09-07 11:43:16 -07003622 }
3623 }
3624
3625 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003626 * Handle instructions that can throw and that are sitting in a "try" block. (If they're not in a
3627 * "try" block when they throw, control transfers out of the method.)
jeffhaobdb76512011-09-07 11:43:16 -07003628 */
Mathieu Chartierde40d472015-10-15 17:47:48 -07003629 if ((opcode_flags & Instruction::kThrow) != 0 && GetInstructionFlags(work_insn_idx_).IsInTry()) {
Andreas Gampef91baf12014-07-18 15:41:00 -07003630 bool has_catch_all_handler = false;
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08003631 const DexFile::TryItem* try_item = code_item_accessor_.FindTryItem(work_insn_idx_);
3632 CHECK(try_item != nullptr);
3633 CatchHandlerIterator iterator(code_item_accessor_.GetCatchHandlerData(try_item->handler_off_));
jeffhaobdb76512011-09-07 11:43:16 -07003634
Andreas Gampef91baf12014-07-18 15:41:00 -07003635 // Need the linker to try and resolve the handled class to check if it's Throwable.
3636 ClassLinker* linker = Runtime::Current()->GetClassLinker();
3637
Ian Rogers0571d352011-11-03 19:51:38 -07003638 for (; iterator.HasNext(); iterator.Next()) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08003639 dex::TypeIndex handler_type_idx = iterator.GetHandlerTypeIndex();
3640 if (!handler_type_idx.IsValid()) {
Andreas Gampef91baf12014-07-18 15:41:00 -07003641 has_catch_all_handler = true;
3642 } else {
3643 // It is also a catch-all if it is java.lang.Throwable.
Vladimir Marko28e012a2017-12-07 11:22:59 +00003644 ObjPtr<mirror::Class> klass =
Vladimir Marko666ee3d2017-12-11 18:37:36 +00003645 linker->ResolveType(handler_type_idx, dex_cache_, class_loader_);
Andreas Gampef91baf12014-07-18 15:41:00 -07003646 if (klass != nullptr) {
3647 if (klass == mirror::Throwable::GetJavaLangThrowable()) {
3648 has_catch_all_handler = true;
3649 }
3650 } else {
3651 // Clear exception.
Ian Rogers7b078e82014-09-10 14:44:24 -07003652 DCHECK(self_->IsExceptionPending());
3653 self_->ClearException();
Andreas Gampef91baf12014-07-18 15:41:00 -07003654 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003655 }
jeffhaobdb76512011-09-07 11:43:16 -07003656 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003657 * Merge registers into the "catch" block. We want to use the "savedRegs" rather than
3658 * "work_regs", because at runtime the exception will be thrown before the instruction
3659 * modifies any registers.
jeffhaobdb76512011-09-07 11:43:16 -07003660 */
Ian Rogersebbdd872014-07-07 23:53:08 -07003661 if (!UpdateRegisters(iterator.GetHandlerAddress(), saved_line_.get(), false)) {
jeffhaobdb76512011-09-07 11:43:16 -07003662 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07003663 }
jeffhaobdb76512011-09-07 11:43:16 -07003664 }
3665
3666 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003667 * If the monitor stack depth is nonzero, there must be a "catch all" handler for this
3668 * instruction. This does apply to monitor-exit because of async exception handling.
jeffhaobdb76512011-09-07 11:43:16 -07003669 */
Andreas Gampef91baf12014-07-18 15:41:00 -07003670 if (work_line_->MonitorStackDepth() > 0 && !has_catch_all_handler) {
jeffhaobdb76512011-09-07 11:43:16 -07003671 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003672 * The state in work_line reflects the post-execution state. If the current instruction is a
3673 * monitor-enter and the monitor stack was empty, we don't need a catch-all (if it throws,
jeffhaobdb76512011-09-07 11:43:16 -07003674 * it will do so before grabbing the lock).
3675 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02003676 if (inst->Opcode() != Instruction::MONITOR_ENTER || work_line_->MonitorStackDepth() != 1) {
jeffhaod5347e02012-03-22 17:25:05 -07003677 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
Ian Rogersd81871c2011-10-03 13:57:23 -07003678 << "expected to be within a catch-all for an instruction where a monitor is held";
jeffhaobdb76512011-09-07 11:43:16 -07003679 return false;
3680 }
3681 }
3682 }
3683
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003684 /* Handle "continue". Tag the next consecutive instruction.
3685 * Note: Keep the code handling "continue" case below the "branch" and "switch" cases,
3686 * because it changes work_line_ when performing peephole optimization
3687 * and this change should not be used in those cases.
3688 */
Ian Rogers6d376ae2013-07-23 15:12:40 -07003689 if ((opcode_flags & Instruction::kContinue) != 0) {
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08003690 DCHECK_EQ(&code_item_accessor_.InstructionAt(work_insn_idx_), inst);
Ian Rogers7b078e82014-09-10 14:44:24 -07003691 uint32_t next_insn_idx = work_insn_idx_ + inst->SizeInCodeUnits();
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08003692 if (next_insn_idx >= code_item_accessor_.InsnsSizeInCodeUnits()) {
Ian Rogers6d376ae2013-07-23 15:12:40 -07003693 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Execution can walk off end of code area";
3694 return false;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003695 }
Ian Rogers6d376ae2013-07-23 15:12:40 -07003696 // The only way to get to a move-exception instruction is to get thrown there. Make sure the
3697 // next instruction isn't one.
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08003698 if (!CheckNotMoveException(code_item_accessor_.Insns(), next_insn_idx)) {
Ian Rogers6d376ae2013-07-23 15:12:40 -07003699 return false;
3700 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07003701 if (nullptr != fallthrough_line) {
Ian Rogers6d376ae2013-07-23 15:12:40 -07003702 // Make workline consistent with fallthrough computed from peephole optimization.
3703 work_line_->CopyFromLine(fallthrough_line.get());
3704 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07003705 if (GetInstructionFlags(next_insn_idx).IsReturn()) {
Ian Rogersb8c78592013-07-25 23:52:52 +00003706 // For returns we only care about the operand to the return, all other registers are dead.
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08003707 const Instruction* ret_inst = &code_item_accessor_.InstructionAt(next_insn_idx);
Andreas Gampea727e372015-08-25 09:22:37 -07003708 AdjustReturnLine(this, ret_inst, work_line_.get());
Ian Rogersb8c78592013-07-25 23:52:52 +00003709 }
Ian Rogers6d376ae2013-07-23 15:12:40 -07003710 RegisterLine* next_line = reg_table_.GetLine(next_insn_idx);
Ian Rogers7b078e82014-09-10 14:44:24 -07003711 if (next_line != nullptr) {
Ian Rogersebbdd872014-07-07 23:53:08 -07003712 // Merge registers into what we have for the next instruction, and set the "changed" flag if
3713 // needed. If the merge changes the state of the registers then the work line will be
3714 // updated.
3715 if (!UpdateRegisters(next_insn_idx, work_line_.get(), true)) {
Ian Rogers6d376ae2013-07-23 15:12:40 -07003716 return false;
3717 }
3718 } else {
3719 /*
3720 * We're not recording register data for the next instruction, so we don't know what the
3721 * prior state was. We have to assume that something has changed and re-evaluate it.
3722 */
Mathieu Chartierde40d472015-10-15 17:47:48 -07003723 GetInstructionFlags(next_insn_idx).SetChanged();
Ian Rogers6d376ae2013-07-23 15:12:40 -07003724 }
3725 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003726
jeffhaod1f0fde2011-09-08 17:25:33 -07003727 /* If we're returning from the method, make sure monitor stack is empty. */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003728 if ((opcode_flags & Instruction::kReturn) != 0) {
Andreas Gampea727e372015-08-25 09:22:37 -07003729 work_line_->VerifyMonitorStackEmpty(this);
jeffhaobdb76512011-09-07 11:43:16 -07003730 }
3731
3732 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003733 * Update start_guess. Advance to the next instruction of that's
3734 * possible, otherwise use the branch target if one was found. If
jeffhaobdb76512011-09-07 11:43:16 -07003735 * neither of those exists we're in a return or throw; leave start_guess
3736 * alone and let the caller sort it out.
3737 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003738 if ((opcode_flags & Instruction::kContinue) != 0) {
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08003739 DCHECK_EQ(&code_item_accessor_.InstructionAt(work_insn_idx_), inst);
Ian Rogers7b078e82014-09-10 14:44:24 -07003740 *start_guess = work_insn_idx_ + inst->SizeInCodeUnits();
Elliott Hughesadb8c672012-03-06 16:49:32 -08003741 } else if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07003742 /* we're still okay if branch_target is zero */
Ian Rogersd81871c2011-10-03 13:57:23 -07003743 *start_guess = work_insn_idx_ + branch_target;
jeffhaobdb76512011-09-07 11:43:16 -07003744 }
3745
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08003746 DCHECK_LT(*start_guess, code_item_accessor_.InsnsSizeInCodeUnits());
Mathieu Chartierde40d472015-10-15 17:47:48 -07003747 DCHECK(GetInstructionFlags(*start_guess).IsOpcode());
jeffhaobdb76512011-09-07 11:43:16 -07003748
Andreas Gampea727e372015-08-25 09:22:37 -07003749 if (have_pending_runtime_throw_failure_) {
3750 have_any_pending_runtime_throw_failure_ = true;
3751 // Reset the pending_runtime_throw flag now.
3752 have_pending_runtime_throw_failure_ = false;
3753 }
3754
jeffhaobdb76512011-09-07 11:43:16 -07003755 return true;
Brian Carlstrom7934ac22013-07-26 10:54:15 -07003756} // NOLINT(readability/fn_size)
jeffhaobdb76512011-09-07 11:43:16 -07003757
Mathieu Chartierde40d472015-10-15 17:47:48 -07003758void MethodVerifier::UninstantiableError(const char* descriptor) {
3759 Fail(VerifyError::VERIFY_ERROR_NO_CLASS) << "Could not create precise reference for "
3760 << "non-instantiable klass " << descriptor;
3761}
3762
Vladimir Marko28e012a2017-12-07 11:22:59 +00003763inline bool MethodVerifier::IsInstantiableOrPrimitive(ObjPtr<mirror::Class> klass) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07003764 return klass->IsInstantiable() || klass->IsPrimitive();
3765}
3766
Andreas Gampe98be1a92017-08-28 08:25:45 -07003767template <MethodVerifier::CheckAccess C>
3768const RegType& MethodVerifier::ResolveClass(dex::TypeIndex class_idx) {
Vladimir Marko666ee3d2017-12-11 18:37:36 +00003769 ClassLinker* linker = Runtime::Current()->GetClassLinker();
Vladimir Marko28e012a2017-12-07 11:22:59 +00003770 ObjPtr<mirror::Class> klass = can_load_classes_
Vladimir Marko666ee3d2017-12-11 18:37:36 +00003771 ? linker->ResolveType(class_idx, dex_cache_, class_loader_)
3772 : linker->LookupResolvedType(class_idx, dex_cache_.Get(), class_loader_.Get());
Vladimir Marko8d6768d2017-03-14 10:13:21 +00003773 if (can_load_classes_ && klass == nullptr) {
3774 DCHECK(self_->IsExceptionPending());
3775 self_->ClearException();
3776 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07003777 const RegType* result = nullptr;
Vladimir Marko9cb0c462017-04-21 13:31:41 +01003778 if (klass != nullptr) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07003779 bool precise = klass->CannotBeAssignedFromOtherTypes();
3780 if (precise && !IsInstantiableOrPrimitive(klass)) {
3781 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
3782 UninstantiableError(descriptor);
3783 precise = false;
3784 }
Vladimir Marko28e012a2017-12-07 11:22:59 +00003785 result = reg_types_.FindClass(klass.Ptr(), precise);
Mathieu Chartierde40d472015-10-15 17:47:48 -07003786 if (result == nullptr) {
3787 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
Vladimir Marko28e012a2017-12-07 11:22:59 +00003788 result = reg_types_.InsertClass(descriptor, klass.Ptr(), precise);
Mathieu Chartierde40d472015-10-15 17:47:48 -07003789 }
3790 } else {
3791 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
3792 result = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003793 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07003794 DCHECK(result != nullptr);
3795 if (result->IsConflict()) {
3796 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
3797 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "accessing broken descriptor '" << descriptor
3798 << "' in " << GetDeclaringClass();
3799 return *result;
3800 }
David Brazdilca3c8c32016-09-06 14:04:48 +01003801
3802 // Record result of class resolution attempt.
Vladimir Marko28e012a2017-12-07 11:22:59 +00003803 VerifierDeps::MaybeRecordClassResolution(*dex_file_, class_idx, klass.Ptr());
David Brazdilca3c8c32016-09-06 14:04:48 +01003804
Andreas Gampe629be512017-08-25 17:09:32 -07003805 // If requested, check if access is allowed. Unresolved types are included in this check, as the
3806 // interpreter only tests whether access is allowed when a class is not pre-verified and runs in
3807 // the access-checks interpreter. If result is primitive, skip the access check.
3808 //
3809 // Note: we do this for unresolved classes to trigger re-verification at runtime.
3810 if (C == CheckAccess::kYes && result->IsNonZeroReferenceTypes()) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07003811 const RegType& referrer = GetDeclaringClass();
Andreas Gampe629be512017-08-25 17:09:32 -07003812 if (!referrer.CanAccess(*result)) {
3813 Fail(VERIFY_ERROR_ACCESS_CLASS) << "(possibly) illegal class access: '"
Nicolas Geoffray4e868fa2017-04-21 17:16:44 +01003814 << referrer << "' -> '" << *result << "'";
Mathieu Chartierde40d472015-10-15 17:47:48 -07003815 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07003816 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07003817 return *result;
Ian Rogersd81871c2011-10-03 13:57:23 -07003818}
3819
Andreas Gampedd309072017-08-31 07:47:37 -07003820// Instantiate ResolveClass variants. This is required as the -inl file has a function with a call
3821// to ResolveClass, and compilers may decide to inline, requiring a symbol.
Andreas Gampeae0b1f42017-08-30 17:36:26 -07003822template const RegType& MethodVerifier::ResolveClass<MethodVerifier::CheckAccess::kNo>(
3823 dex::TypeIndex class_idx);
3824template const RegType& MethodVerifier::ResolveClass<MethodVerifier::CheckAccess::kYes>(
3825 dex::TypeIndex class_idx);
3826
Ian Rogersd8f69b02014-09-10 21:43:52 +00003827const RegType& MethodVerifier::GetCaughtExceptionType() {
Ian Rogers7b078e82014-09-10 14:44:24 -07003828 const RegType* common_super = nullptr;
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08003829 if (code_item_accessor_.TriesSize() != 0) {
3830 const uint8_t* handlers_ptr = code_item_accessor_.GetCatchHandlerData();
Ian Rogersd81871c2011-10-03 13:57:23 -07003831 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
3832 for (uint32_t i = 0; i < handlers_size; i++) {
Ian Rogers0571d352011-11-03 19:51:38 -07003833 CatchHandlerIterator iterator(handlers_ptr);
3834 for (; iterator.HasNext(); iterator.Next()) {
3835 if (iterator.GetHandlerAddress() == (uint32_t) work_insn_idx_) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08003836 if (!iterator.GetHandlerTypeIndex().IsValid()) {
Ian Rogersb4903572012-10-11 11:52:56 -07003837 common_super = &reg_types_.JavaLangThrowable(false);
Ian Rogersd81871c2011-10-03 13:57:23 -07003838 } else {
Andreas Gampe98be1a92017-08-28 08:25:45 -07003839 const RegType& exception =
3840 ResolveClass<CheckAccess::kYes>(iterator.GetHandlerTypeIndex());
David Brazdilca3c8c32016-09-06 14:04:48 +01003841 if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(exception, this)) {
David Brazdil68b5c0b2016-01-19 14:25:29 +00003842 DCHECK(!exception.IsUninitializedTypes()); // Comes from dex, shouldn't be uninit.
Jeff Haoc26a56c2013-11-04 12:00:47 -08003843 if (exception.IsUnresolvedTypes()) {
3844 // We don't know enough about the type. Fail here and let runtime handle it.
3845 Fail(VERIFY_ERROR_NO_CLASS) << "unresolved exception class " << exception;
3846 return exception;
3847 } else {
3848 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unexpected non-exception class " << exception;
3849 return reg_types_.Conflict();
3850 }
Jeff Haob878f212014-04-24 16:25:36 -07003851 } else if (common_super == nullptr) {
3852 common_super = &exception;
Ian Rogers28ad40d2011-10-27 15:19:26 -07003853 } else if (common_super->Equals(exception)) {
Ian Rogersc4762272012-02-01 15:55:55 -08003854 // odd case, but nothing to do
Ian Rogersd81871c2011-10-03 13:57:23 -07003855 } else {
David Brazdilca3c8c32016-09-06 14:04:48 +01003856 common_super = &common_super->Merge(exception, &reg_types_, this);
Andreas Gampe7c038102014-10-27 20:08:46 -07003857 if (FailOrAbort(this,
David Brazdilca3c8c32016-09-06 14:04:48 +01003858 reg_types_.JavaLangThrowable(false).IsAssignableFrom(
3859 *common_super, this),
Andreas Gampe7c038102014-10-27 20:08:46 -07003860 "java.lang.Throwable is not assignable-from common_super at ",
3861 work_insn_idx_)) {
3862 break;
3863 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003864 }
3865 }
3866 }
3867 }
Ian Rogers0571d352011-11-03 19:51:38 -07003868 handlers_ptr = iterator.EndDataPointer();
Ian Rogersd81871c2011-10-03 13:57:23 -07003869 }
3870 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003871 if (common_super == nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003872 /* no catch blocks, or no catches with classes we can find */
jeffhaod5347e02012-03-22 17:25:05 -07003873 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unable to find exception handler";
Ian Rogersad0b3a32012-04-16 14:50:24 -07003874 return reg_types_.Conflict();
Ian Rogersd81871c2011-10-03 13:57:23 -07003875 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07003876 return *common_super;
Ian Rogersd81871c2011-10-03 13:57:23 -07003877}
3878
Mathieu Chartiere401d142015-04-22 13:56:20 -07003879ArtMethod* MethodVerifier::ResolveMethodAndCheckAccess(
Alex Light7268d472016-01-20 15:50:01 -08003880 uint32_t dex_method_idx, MethodType method_type) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003881 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx);
Andreas Gampe98be1a92017-08-28 08:25:45 -07003882 const RegType& klass_type = ResolveClass<CheckAccess::kYes>(method_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003883 if (klass_type.IsConflict()) {
3884 std::string append(" in attempt to access method ");
3885 append += dex_file_->GetMethodName(method_id);
3886 AppendToLastFailMessage(append);
Ian Rogers7b078e82014-09-10 14:44:24 -07003887 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08003888 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003889 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003890 return nullptr; // Can't resolve Class so no more to do here
Ian Rogers90040192011-12-16 08:54:29 -08003891 }
Vladimir Markoba118822017-06-12 15:41:56 +01003892 ObjPtr<mirror::Class> klass = klass_type.GetClass();
Ian Rogersd8f69b02014-09-10 21:43:52 +00003893 const RegType& referrer = GetDeclaringClass();
Mathieu Chartiere401d142015-04-22 13:56:20 -07003894 auto* cl = Runtime::Current()->GetClassLinker();
3895 auto pointer_size = cl->GetImagePointerSize();
Andreas Gampe42ef8ab2015-12-03 17:27:32 -08003896
Mathieu Chartiere401d142015-04-22 13:56:20 -07003897 ArtMethod* res_method = dex_cache_->GetResolvedMethod(dex_method_idx, pointer_size);
Ian Rogers7b078e82014-09-10 14:44:24 -07003898 if (res_method == nullptr) {
Vladimir Markoba118822017-06-12 15:41:56 +01003899 // Try to find the method with the appropriate lookup for the klass type (interface or not).
3900 // If this lookup does not match `method_type`, errors shall be reported below.
3901 if (klass->IsInterface()) {
3902 res_method = klass->FindInterfaceMethod(dex_cache_.Get(), dex_method_idx, pointer_size);
David Brazdilca3c8c32016-09-06 14:04:48 +01003903 } else {
Vladimir Markoba118822017-06-12 15:41:56 +01003904 res_method = klass->FindClassMethod(dex_cache_.Get(), dex_method_idx, pointer_size);
Ian Rogersd81871c2011-10-03 13:57:23 -07003905 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003906 if (res_method != nullptr) {
Vladimir Markoba118822017-06-12 15:41:56 +01003907 dex_cache_->SetResolvedMethod(dex_method_idx, res_method, pointer_size);
Ian Rogersd81871c2011-10-03 13:57:23 -07003908 }
3909 }
David Brazdilca3c8c32016-09-06 14:04:48 +01003910
Vladimir Markoba118822017-06-12 15:41:56 +01003911 // Record result of method resolution attempt. The klass resolution has recorded whether
3912 // the class is an interface or not and therefore the type of the lookup performed above.
3913 // TODO: Maybe we should not record dependency if the invoke type does not match the lookup type.
3914 VerifierDeps::MaybeRecordMethodResolution(*dex_file_, dex_method_idx, res_method);
3915
Alex Lightafb66472017-08-01 09:54:49 -07003916 bool must_fail = false;
3917 // This is traditional and helps with screwy bytecode. It will tell you that, yes, a method
3918 // exists, but that it's called incorrectly. This significantly helps debugging, as locally it's
3919 // hard to see the differences.
3920 // If we don't have res_method here we must fail. Just use this bool to make sure of that with a
3921 // DCHECK.
Vladimir Markoba118822017-06-12 15:41:56 +01003922 if (res_method == nullptr) {
Alex Lightafb66472017-08-01 09:54:49 -07003923 must_fail = true;
Vladimir Markoba118822017-06-12 15:41:56 +01003924 // Try to find the method also with the other type for better error reporting below
3925 // but do not store such bogus lookup result in the DexCache or VerifierDeps.
3926 if (klass->IsInterface()) {
Alex Lightafb66472017-08-01 09:54:49 -07003927 // NB This is normally not really allowed but we want to get any static or private object
3928 // methods for error message purposes. This will never be returned.
3929 // TODO We might want to change the verifier to not require this.
Vladimir Markoba118822017-06-12 15:41:56 +01003930 res_method = klass->FindClassMethod(dex_cache_.Get(), dex_method_idx, pointer_size);
3931 } else {
3932 // If there was an interface method with the same signature,
3933 // we would have found it also in the "copied" methods.
3934 DCHECK(klass->FindInterfaceMethod(dex_cache_.Get(), dex_method_idx, pointer_size) == nullptr);
3935 }
3936 }
David Brazdilca3c8c32016-09-06 14:04:48 +01003937
3938 if (res_method == nullptr) {
3939 Fail(VERIFY_ERROR_NO_METHOD) << "couldn't find method "
David Sehr709b0702016-10-13 09:12:37 -07003940 << klass->PrettyDescriptor() << "."
David Brazdilca3c8c32016-09-06 14:04:48 +01003941 << dex_file_->GetMethodName(method_id) << " "
3942 << dex_file_->GetMethodSignature(method_id);
3943 return nullptr;
3944 }
3945
Ian Rogersd81871c2011-10-03 13:57:23 -07003946 // Make sure calls to constructors are "direct". There are additional restrictions but we don't
3947 // enforce them here.
3948 if (res_method->IsConstructor() && method_type != METHOD_DIRECT) {
jeffhaod5347e02012-03-22 17:25:05 -07003949 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting non-direct call to constructor "
David Sehr709b0702016-10-13 09:12:37 -07003950 << res_method->PrettyMethod();
Ian Rogers7b078e82014-09-10 14:44:24 -07003951 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003952 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003953 // Disallow any calls to class initializers.
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003954 if (res_method->IsClassInitializer()) {
jeffhaod5347e02012-03-22 17:25:05 -07003955 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting call to class initializer "
David Sehr709b0702016-10-13 09:12:37 -07003956 << res_method->PrettyMethod();
Ian Rogers7b078e82014-09-10 14:44:24 -07003957 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003958 }
Andreas Gampe42ef8ab2015-12-03 17:27:32 -08003959
3960 // Check that interface methods are static or match interface classes.
3961 // We only allow statics if we don't have default methods enabled.
3962 //
3963 // Note: this check must be after the initializer check, as those are required to fail a class,
3964 // while this check implies an IncompatibleClassChangeError.
3965 if (klass->IsInterface()) {
Alex Lightb55f1ac2016-04-12 15:50:55 -07003966 // methods called on interfaces should be invoke-interface, invoke-super, invoke-direct (if
3967 // dex file version is 37 or greater), or invoke-static.
Andreas Gampe42ef8ab2015-12-03 17:27:32 -08003968 if (method_type != METHOD_INTERFACE &&
Neil Fuller9724c632016-01-07 15:42:47 +00003969 method_type != METHOD_STATIC &&
Alex Lightb55f1ac2016-04-12 15:50:55 -07003970 ((dex_file_->GetVersion() < DexFile::kDefaultMethodsVersion) ||
3971 method_type != METHOD_DIRECT) &&
Neil Fuller9724c632016-01-07 15:42:47 +00003972 method_type != METHOD_SUPER) {
Andreas Gampe42ef8ab2015-12-03 17:27:32 -08003973 Fail(VERIFY_ERROR_CLASS_CHANGE)
David Sehr709b0702016-10-13 09:12:37 -07003974 << "non-interface method " << dex_file_->PrettyMethod(dex_method_idx)
3975 << " is in an interface class " << klass->PrettyClass();
Andreas Gampe42ef8ab2015-12-03 17:27:32 -08003976 return nullptr;
3977 }
3978 } else {
3979 if (method_type == METHOD_INTERFACE) {
3980 Fail(VERIFY_ERROR_CLASS_CHANGE)
David Sehr709b0702016-10-13 09:12:37 -07003981 << "interface method " << dex_file_->PrettyMethod(dex_method_idx)
3982 << " is in a non-interface class " << klass->PrettyClass();
Andreas Gampe42ef8ab2015-12-03 17:27:32 -08003983 return nullptr;
3984 }
3985 }
3986
Alex Lightafb66472017-08-01 09:54:49 -07003987 // Check specifically for non-public object methods being provided for interface dispatch. This
3988 // can occur if we failed to find a method with FindInterfaceMethod but later find one with
3989 // FindClassMethod for error message use.
3990 if (method_type == METHOD_INTERFACE &&
3991 res_method->GetDeclaringClass()->IsObjectClass() &&
3992 !res_method->IsPublic()) {
3993 Fail(VERIFY_ERROR_NO_METHOD) << "invoke-interface " << klass->PrettyDescriptor() << "."
3994 << dex_file_->GetMethodName(method_id) << " "
3995 << dex_file_->GetMethodSignature(method_id) << " resolved to "
3996 << "non-public object method " << res_method->PrettyMethod() << " "
3997 << "but non-public Object methods are excluded from interface "
3998 << "method resolution.";
3999 return nullptr;
4000 }
jeffhao8cd6dda2012-02-22 10:15:34 -08004001 // Check if access is allowed.
Ian Rogersad0b3a32012-04-16 14:50:24 -07004002 if (!referrer.CanAccessMember(res_method->GetDeclaringClass(), res_method->GetAccessFlags())) {
David Sehr709b0702016-10-13 09:12:37 -07004003 Fail(VERIFY_ERROR_ACCESS_METHOD) << "illegal method access (call "
4004 << res_method->PrettyMethod()
Ian Rogersad0b3a32012-04-16 14:50:24 -07004005 << " from " << referrer << ")";
jeffhaob57e9522012-04-26 18:08:21 -07004006 return res_method;
jeffhao8cd6dda2012-02-22 10:15:34 -08004007 }
jeffhaode0d9c92012-02-27 13:58:13 -08004008 // Check that invoke-virtual and invoke-super are not used on private methods of the same class.
Alex Light7268d472016-01-20 15:50:01 -08004009 if (res_method->IsPrivate() && (method_type == METHOD_VIRTUAL || method_type == METHOD_SUPER)) {
jeffhaod5347e02012-03-22 17:25:05 -07004010 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invoke-super/virtual can't be used on private method "
David Sehr709b0702016-10-13 09:12:37 -07004011 << res_method->PrettyMethod();
Ian Rogers7b078e82014-09-10 14:44:24 -07004012 return nullptr;
jeffhaode0d9c92012-02-27 13:58:13 -08004013 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004014 // See if the method type implied by the invoke instruction matches the access flags for the
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004015 // target method. The flags for METHOD_POLYMORPHIC are based on there being precisely two
4016 // signature polymorphic methods supported by the run-time which are native methods with variable
4017 // arguments.
Brian Carlstrombe6fa5e2014-12-09 20:15:42 -08004018 if ((method_type == METHOD_DIRECT && (!res_method->IsDirect() || res_method->IsStatic())) ||
Ian Rogersd81871c2011-10-03 13:57:23 -07004019 (method_type == METHOD_STATIC && !res_method->IsStatic()) ||
Alex Light7268d472016-01-20 15:50:01 -08004020 ((method_type == METHOD_SUPER ||
4021 method_type == METHOD_VIRTUAL ||
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004022 method_type == METHOD_INTERFACE) && res_method->IsDirect()) ||
4023 ((method_type == METHOD_POLYMORPHIC) &&
4024 (!res_method->IsNative() || !res_method->IsVarargs()))) {
Ian Rogers2fc14272012-08-30 10:56:57 -07004025 Fail(VERIFY_ERROR_CLASS_CHANGE) << "invoke type (" << method_type << ") does not match method "
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004026 "type of " << res_method->PrettyMethod();
Ian Rogers7b078e82014-09-10 14:44:24 -07004027 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07004028 }
Alex Lightafb66472017-08-01 09:54:49 -07004029 // Make sure we weren't expecting to fail.
4030 DCHECK(!must_fail) << "invoke type (" << method_type << ")"
4031 << klass->PrettyDescriptor() << "."
4032 << dex_file_->GetMethodName(method_id) << " "
4033 << dex_file_->GetMethodSignature(method_id) << " unexpectedly resolved to "
4034 << res_method->PrettyMethod() << " without error. Initially this method was "
4035 << "not found so we were expecting to fail for some reason.";
jeffhao8cd6dda2012-02-22 10:15:34 -08004036 return res_method;
4037}
4038
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004039template <class T>
Mathieu Chartiere401d142015-04-22 13:56:20 -07004040ArtMethod* MethodVerifier::VerifyInvocationArgsFromIterator(
4041 T* it, const Instruction* inst, MethodType method_type, bool is_range, ArtMethod* res_method) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004042 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
4043 // match the call to the signature. Also, we might be calling through an abstract method
4044 // definition (which doesn't have register count values).
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004045 const size_t expected_args = inst->VRegA();
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004046 /* caught by static verifier */
4047 DCHECK(is_range || expected_args <= 5);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004048
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08004049 if (expected_args > code_item_accessor_.OutsSize()) {
Orion Hodson1cda7c22017-08-10 13:06:45 +01004050 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08004051 << ") exceeds outsSize ("
4052 << code_item_accessor_.OutsSize() << ")";
Orion Hodson1cda7c22017-08-10 13:06:45 +01004053 return nullptr;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004054 }
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004055
4056 /*
4057 * Check the "this" argument, which must be an instance of the class that declared the method.
4058 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
4059 * rigorous check here (which is okay since we have to do it at runtime).
4060 */
4061 if (method_type != METHOD_STATIC) {
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004062 const RegType& actual_arg_type = work_line_->GetInvocationThis(this, inst);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004063 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
4064 CHECK(have_pending_hard_failure_);
4065 return nullptr;
4066 }
David Brazdil68b5c0b2016-01-19 14:25:29 +00004067 bool is_init = false;
4068 if (actual_arg_type.IsUninitializedTypes()) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004069 if (res_method) {
4070 if (!res_method->IsConstructor()) {
4071 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
4072 return nullptr;
4073 }
4074 } else {
4075 // Check whether the name of the called method is "<init>"
4076 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Jeff Hao0d087272014-08-04 14:47:17 -07004077 if (strcmp(dex_file_->GetMethodName(dex_file_->GetMethodId(method_idx)), "<init>") != 0) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004078 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
4079 return nullptr;
4080 }
4081 }
David Brazdil68b5c0b2016-01-19 14:25:29 +00004082 is_init = true;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004083 }
David Brazdil68b5c0b2016-01-19 14:25:29 +00004084 const RegType& adjusted_type = is_init
4085 ? GetRegTypeCache()->FromUninitialized(actual_arg_type)
4086 : actual_arg_type;
Andreas Gampeeed3a5d2017-11-29 14:58:34 -08004087 if (method_type != METHOD_INTERFACE && !adjusted_type.IsZeroOrNull()) {
Ian Rogersd8f69b02014-09-10 21:43:52 +00004088 const RegType* res_method_class;
Andreas Gamped9e23012015-06-04 22:19:58 -07004089 // Miranda methods have the declaring interface as their declaring class, not the abstract
4090 // class. It would be wrong to use this for the type check (interface type checks are
4091 // postponed to runtime).
4092 if (res_method != nullptr && !res_method->IsMiranda()) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004093 mirror::Class* klass = res_method->GetDeclaringClass();
Ian Rogers1ff3c982014-08-12 02:30:58 -07004094 std::string temp;
Andreas Gampef23f33d2015-06-23 14:18:17 -07004095 res_method_class = &FromClass(klass->GetDescriptor(&temp), klass,
4096 klass->CannotBeAssignedFromOtherTypes());
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004097 } else {
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004098 const uint32_t method_idx = inst->VRegB();
Andreas Gampea5b09a62016-11-17 15:21:22 -08004099 const dex::TypeIndex class_idx = dex_file_->GetMethodId(method_idx).class_idx_;
Mathieu Chartierde40d472015-10-15 17:47:48 -07004100 res_method_class = &reg_types_.FromDescriptor(
4101 GetClassLoader(),
4102 dex_file_->StringByTypeIdx(class_idx),
4103 false);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004104 }
David Brazdilca3c8c32016-09-06 14:04:48 +01004105 if (!res_method_class->IsAssignableFrom(adjusted_type, this)) {
David Brazdil68b5c0b2016-01-19 14:25:29 +00004106 Fail(adjusted_type.IsUnresolvedTypes()
4107 ? VERIFY_ERROR_NO_CLASS
4108 : VERIFY_ERROR_BAD_CLASS_SOFT)
4109 << "'this' argument '" << actual_arg_type << "' not instance of '"
4110 << *res_method_class << "'";
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004111 // Continue on soft failures. We need to find possible hard failures to avoid problems in
4112 // the compiler.
4113 if (have_pending_hard_failure_) {
4114 return nullptr;
4115 }
4116 }
4117 }
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004118 }
4119
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004120 uint32_t arg[5];
4121 if (!is_range) {
4122 inst->GetVarArgs(arg);
4123 }
4124 uint32_t sig_registers = (method_type == METHOD_STATIC) ? 0 : 1;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004125 for ( ; it->HasNext(); it->Next()) {
4126 if (sig_registers >= expected_args) {
4127 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, expected " << inst->VRegA() <<
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004128 " argument registers, method signature has " << sig_registers + 1 << " or more";
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004129 return nullptr;
4130 }
4131
4132 const char* param_descriptor = it->GetDescriptor();
4133
4134 if (param_descriptor == nullptr) {
4135 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation because of missing signature "
4136 "component";
4137 return nullptr;
4138 }
4139
Ian Rogersd8f69b02014-09-10 21:43:52 +00004140 const RegType& reg_type = reg_types_.FromDescriptor(GetClassLoader(), param_descriptor, false);
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004141 uint32_t get_reg = is_range ? inst->VRegC() + static_cast<uint32_t>(sig_registers) :
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004142 arg[sig_registers];
4143 if (reg_type.IsIntegralTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004144 const RegType& src_type = work_line_->GetRegisterType(this, get_reg);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004145 if (!src_type.IsIntegralTypes()) {
4146 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register v" << get_reg << " has type " << src_type
4147 << " but expected " << reg_type;
Andreas Gampeb588f4c2015-05-26 13:35:39 -07004148 return nullptr;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004149 }
Andreas Gampeda9badb2015-06-05 20:22:12 -07004150 } else {
4151 if (!work_line_->VerifyRegisterType(this, get_reg, reg_type)) {
4152 // Continue on soft failures. We need to find possible hard failures to avoid problems in
4153 // the compiler.
4154 if (have_pending_hard_failure_) {
4155 return nullptr;
4156 }
4157 } else if (reg_type.IsLongOrDoubleTypes()) {
4158 // Check that registers are consecutive (for non-range invokes). Invokes are the only
4159 // instructions not specifying register pairs by the first component, but require them
4160 // nonetheless. Only check when there's an actual register in the parameters. If there's
4161 // none, this will fail below.
4162 if (!is_range && sig_registers + 1 < expected_args) {
4163 uint32_t second_reg = arg[sig_registers + 1];
4164 if (second_reg != get_reg + 1) {
4165 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, long or double parameter "
4166 "at index " << sig_registers << " is not a pair: " << get_reg << " + "
4167 << second_reg << ".";
4168 return nullptr;
4169 }
4170 }
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004171 }
4172 }
4173 sig_registers += reg_type.IsLongOrDoubleTypes() ? 2 : 1;
4174 }
4175 if (expected_args != sig_registers) {
4176 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, expected " << expected_args <<
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004177 " argument registers, method signature has " << sig_registers;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004178 return nullptr;
4179 }
4180 return res_method;
4181}
4182
4183void MethodVerifier::VerifyInvocationArgsUnresolvedMethod(const Instruction* inst,
4184 MethodType method_type,
4185 bool is_range) {
4186 // As the method may not have been resolved, make this static check against what we expect.
4187 // The main reason for this code block is to fail hard when we find an illegal use, e.g.,
4188 // wrong number of arguments or wrong primitive types, even if the method could not be resolved.
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004189 const uint32_t method_idx = inst->VRegB();
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004190 DexFileParameterIterator it(*dex_file_,
4191 dex_file_->GetProtoId(dex_file_->GetMethodId(method_idx).proto_idx_));
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004192 VerifyInvocationArgsFromIterator(&it, inst, method_type, is_range, nullptr);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004193}
4194
Orion Hodsonc069a302017-01-18 09:23:12 +00004195bool MethodVerifier::CheckCallSite(uint32_t call_site_idx) {
Orion Hodson3a842f52017-04-21 15:24:10 +01004196 if (call_site_idx >= dex_file_->NumCallSiteIds()) {
4197 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Bad call site id #" << call_site_idx
4198 << " >= " << dex_file_->NumCallSiteIds();
4199 return false;
4200 }
4201
Orion Hodsonc069a302017-01-18 09:23:12 +00004202 CallSiteArrayValueIterator it(*dex_file_, dex_file_->GetCallSiteId(call_site_idx));
4203 // Check essential arguments are provided. The dex file verifier has verified indicies of the
4204 // main values (method handle, name, method_type).
4205 if (it.Size() < 3) {
4206 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Call site #" << call_site_idx
4207 << " has too few arguments: "
4208 << it.Size() << "< 3";
4209 return false;
4210 }
4211
Orion Hodson3a842f52017-04-21 15:24:10 +01004212 // Get and check the first argument: the method handle (index range
4213 // checked by the dex file verifier).
Orion Hodsonc069a302017-01-18 09:23:12 +00004214 uint32_t method_handle_idx = static_cast<uint32_t>(it.GetJavaValue().i);
4215 it.Next();
Orion Hodson3a842f52017-04-21 15:24:10 +01004216
Orion Hodsonc069a302017-01-18 09:23:12 +00004217 const DexFile::MethodHandleItem& mh = dex_file_->GetMethodHandle(method_handle_idx);
4218 if (mh.method_handle_type_ != static_cast<uint16_t>(DexFile::MethodHandleType::kInvokeStatic)) {
4219 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Call site #" << call_site_idx
Orion Hodson631827d2017-04-10 14:53:47 +01004220 << " argument 0 method handle type is not InvokeStatic: "
4221 << mh.method_handle_type_;
Orion Hodsonc069a302017-01-18 09:23:12 +00004222 return false;
4223 }
4224
4225 // Skip the second argument, the name to resolve, as checked by the
4226 // dex file verifier.
4227 it.Next();
4228
4229 // Skip the third argument, the method type expected, as checked by
4230 // the dex file verifier.
4231 it.Next();
4232
4233 // Check the bootstrap method handle and remaining arguments.
4234 const DexFile::MethodId& method_id = dex_file_->GetMethodId(mh.field_or_method_idx_);
4235 uint32_t length;
4236 const char* shorty = dex_file_->GetMethodShorty(method_id, &length);
4237
4238 if (it.Size() < length - 1) {
4239 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Call site #" << call_site_idx
4240 << " too few arguments for bootstrap method: "
4241 << it.Size() << " < " << (length - 1);
4242 return false;
4243 }
4244
4245 // Check the return type and first 3 arguments are references
4246 // (CallSite, Lookup, String, MethodType). If they are not of the
4247 // expected types (or subtypes), it will trigger a
4248 // WrongMethodTypeException during execution.
4249 if (shorty[0] != 'L') {
4250 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Call site #" << call_site_idx
4251 << " bootstrap return type is not a reference";
4252 return false;
4253 }
4254
4255 for (uint32_t i = 1; i < 4; ++i) {
4256 if (shorty[i] != 'L') {
4257 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Call site #" << call_site_idx
4258 << " bootstrap method argument " << (i - 1)
4259 << " is not a reference";
4260 return false;
4261 }
4262 }
4263
4264 // Check the optional arguments.
4265 for (uint32_t i = 4; i < length; ++i, it.Next()) {
4266 bool match = false;
4267 switch (it.GetValueType()) {
4268 case EncodedArrayValueIterator::ValueType::kBoolean:
4269 case EncodedArrayValueIterator::ValueType::kByte:
4270 case EncodedArrayValueIterator::ValueType::kShort:
4271 case EncodedArrayValueIterator::ValueType::kChar:
4272 case EncodedArrayValueIterator::ValueType::kInt:
4273 // These all fit within one register and encoders do not seem
4274 // too exacting on the encoding type they use (ie using
4275 // integer for all of these).
4276 match = (strchr("ZBCSI", shorty[i]) != nullptr);
4277 break;
4278 case EncodedArrayValueIterator::ValueType::kLong:
4279 match = ('J' == shorty[i]);
4280 break;
4281 case EncodedArrayValueIterator::ValueType::kFloat:
4282 match = ('F' == shorty[i]);
4283 break;
4284 case EncodedArrayValueIterator::ValueType::kDouble:
4285 match = ('D' == shorty[i]);
4286 break;
4287 case EncodedArrayValueIterator::ValueType::kMethodType:
4288 case EncodedArrayValueIterator::ValueType::kMethodHandle:
4289 case EncodedArrayValueIterator::ValueType::kString:
4290 case EncodedArrayValueIterator::ValueType::kType:
4291 case EncodedArrayValueIterator::ValueType::kNull:
4292 match = ('L' == shorty[i]);
4293 break;
4294 case EncodedArrayValueIterator::ValueType::kField:
4295 case EncodedArrayValueIterator::ValueType::kMethod:
4296 case EncodedArrayValueIterator::ValueType::kEnum:
4297 case EncodedArrayValueIterator::ValueType::kArray:
4298 case EncodedArrayValueIterator::ValueType::kAnnotation:
4299 // Unreachable based on current EncodedArrayValueIterator::Next().
4300 UNREACHABLE();
4301 }
4302
4303 if (!match) {
4304 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Call site #" << call_site_idx
4305 << " bootstrap method argument " << (i - 1)
4306 << " expected " << shorty[i]
4307 << " got value type: " << it.GetValueType();
4308 return false;
4309 }
4310 }
4311 return true;
4312}
4313
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004314class MethodParamListDescriptorIterator {
4315 public:
Mathieu Chartiere401d142015-04-22 13:56:20 -07004316 explicit MethodParamListDescriptorIterator(ArtMethod* res_method) :
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004317 res_method_(res_method), pos_(0), params_(res_method->GetParameterTypeList()),
4318 params_size_(params_ == nullptr ? 0 : params_->Size()) {
4319 }
4320
4321 bool HasNext() {
4322 return pos_ < params_size_;
4323 }
4324
4325 void Next() {
4326 ++pos_;
4327 }
4328
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07004329 const char* GetDescriptor() REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004330 return res_method_->GetTypeDescriptorFromTypeIdx(params_->GetTypeItem(pos_).type_idx_);
4331 }
4332
4333 private:
Mathieu Chartiere401d142015-04-22 13:56:20 -07004334 ArtMethod* res_method_;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004335 size_t pos_;
4336 const DexFile::TypeList* params_;
4337 const size_t params_size_;
4338};
4339
Mathieu Chartiere401d142015-04-22 13:56:20 -07004340ArtMethod* MethodVerifier::VerifyInvocationArgs(
Alex Light7268d472016-01-20 15:50:01 -08004341 const Instruction* inst, MethodType method_type, bool is_range) {
jeffhao8cd6dda2012-02-22 10:15:34 -08004342 // Resolve the method. This could be an abstract or concrete method depending on what sort of call
4343 // we're making.
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004344 const uint32_t method_idx = inst->VRegB();
Alex Light7268d472016-01-20 15:50:01 -08004345 ArtMethod* res_method = ResolveMethodAndCheckAccess(method_idx, method_type);
Ian Rogers7b078e82014-09-10 14:44:24 -07004346 if (res_method == nullptr) { // error or class is unresolved
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004347 // Check what we can statically.
4348 if (!have_pending_hard_failure_) {
4349 VerifyInvocationArgsUnresolvedMethod(inst, method_type, is_range);
4350 }
4351 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08004352 }
4353
Ian Rogersd81871c2011-10-03 13:57:23 -07004354 // If we're using invoke-super(method), make sure that the executing method's class' superclass
Alex Light705ad492015-09-21 11:36:30 -07004355 // has a vtable entry for the target method. Or the target is on a interface.
Alex Light7268d472016-01-20 15:50:01 -08004356 if (method_type == METHOD_SUPER) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08004357 dex::TypeIndex class_idx = dex_file_->GetMethodId(method_idx).class_idx_;
David Brazdilca3c8c32016-09-06 14:04:48 +01004358 const RegType& reference_type = reg_types_.FromDescriptor(
4359 GetClassLoader(),
4360 dex_file_->StringByTypeIdx(class_idx),
4361 false);
4362 if (reference_type.IsUnresolvedTypes()) {
Alex Lightfedd91d2016-01-07 14:49:16 -08004363 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "Unable to find referenced class from invoke-super";
4364 return nullptr;
4365 }
David Brazdilca3c8c32016-09-06 14:04:48 +01004366 if (reference_type.GetClass()->IsInterface()) {
Alex Lightfedd91d2016-01-07 14:49:16 -08004367 // TODO Can we verify anything else.
David Brazdil15fc7292016-09-02 14:13:18 +01004368 if (class_idx == class_def_.class_idx_) {
Alex Lightfedd91d2016-01-07 14:49:16 -08004369 Fail(VERIFY_ERROR_CLASS_CHANGE) << "Cannot invoke-super on self as interface";
Alex Light55ea94d2016-03-15 09:50:26 -07004370 return nullptr;
Alex Lightfedd91d2016-01-07 14:49:16 -08004371 }
4372 // TODO Revisit whether we want to allow invoke-super on direct interfaces only like the JLS
4373 // does.
Alex Light55ea94d2016-03-15 09:50:26 -07004374 if (!GetDeclaringClass().HasClass()) {
4375 Fail(VERIFY_ERROR_NO_CLASS) << "Unable to resolve the full class of 'this' used in an"
4376 << "interface invoke-super";
4377 return nullptr;
David Brazdilca3c8c32016-09-06 14:04:48 +01004378 } else if (!reference_type.IsStrictlyAssignableFrom(GetDeclaringClass(), this)) {
Alex Lightfedd91d2016-01-07 14:49:16 -08004379 Fail(VERIFY_ERROR_CLASS_CHANGE)
David Sehr709b0702016-10-13 09:12:37 -07004380 << "invoke-super in " << mirror::Class::PrettyClass(GetDeclaringClass().GetClass())
4381 << " in method "
4382 << dex_file_->PrettyMethod(dex_method_idx_) << " to method "
4383 << dex_file_->PrettyMethod(method_idx) << " references "
4384 << "non-super-interface type " << mirror::Class::PrettyClass(reference_type.GetClass());
Alex Lightfedd91d2016-01-07 14:49:16 -08004385 return nullptr;
Alex Light705ad492015-09-21 11:36:30 -07004386 }
4387 } else {
4388 const RegType& super = GetDeclaringClass().GetSuperClass(&reg_types_);
4389 if (super.IsUnresolvedTypes()) {
4390 Fail(VERIFY_ERROR_NO_METHOD) << "unknown super class in invoke-super from "
David Sehr709b0702016-10-13 09:12:37 -07004391 << dex_file_->PrettyMethod(dex_method_idx_)
4392 << " to super " << res_method->PrettyMethod();
Alex Light705ad492015-09-21 11:36:30 -07004393 return nullptr;
4394 }
David Brazdilca3c8c32016-09-06 14:04:48 +01004395 if (!reference_type.IsStrictlyAssignableFrom(GetDeclaringClass(), this) ||
Aart Bikf663e342016-04-04 17:28:59 -07004396 (res_method->GetMethodIndex() >= super.GetClass()->GetVTableLength())) {
Alex Light705ad492015-09-21 11:36:30 -07004397 Fail(VERIFY_ERROR_NO_METHOD) << "invalid invoke-super from "
David Sehr709b0702016-10-13 09:12:37 -07004398 << dex_file_->PrettyMethod(dex_method_idx_)
Alex Light705ad492015-09-21 11:36:30 -07004399 << " to super " << super
4400 << "." << res_method->GetName()
4401 << res_method->GetSignature();
4402 return nullptr;
4403 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004404 }
4405 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08004406
Andreas Gampe74979b12017-05-16 09:28:06 -07004407 if (UNLIKELY(method_type == METHOD_POLYMORPHIC)) {
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004408 // Process the signature of the calling site that is invoking the method handle.
4409 DexFileParameterIterator it(*dex_file_, dex_file_->GetProtoId(inst->VRegH()));
4410 return VerifyInvocationArgsFromIterator(&it, inst, method_type, is_range, res_method);
4411 } else {
4412 // Process the target method's signature.
4413 MethodParamListDescriptorIterator it(res_method);
4414 return VerifyInvocationArgsFromIterator(&it, inst, method_type, is_range, res_method);
4415 }
4416}
4417
4418bool MethodVerifier::CheckSignaturePolymorphicMethod(ArtMethod* method) {
4419 mirror::Class* klass = method->GetDeclaringClass();
4420 if (klass != mirror::MethodHandle::StaticClass()) {
4421 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
4422 << "Signature polymorphic method must be declared in java.lang.invoke.MethodClass";
4423 return false;
4424 }
4425
4426 const char* method_name = method->GetName();
4427 if (strcmp(method_name, "invoke") != 0 && strcmp(method_name, "invokeExact") != 0) {
4428 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
4429 << "Signature polymorphic method name invalid: " << method_name;
4430 return false;
4431 }
4432
4433 const DexFile::TypeList* types = method->GetParameterTypeList();
4434 if (types->Size() != 1) {
4435 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
4436 << "Signature polymorphic method has too many arguments " << types->Size() << " != 1";
4437 return false;
4438 }
4439
4440 const dex::TypeIndex argument_type_index = types->GetTypeItem(0).type_idx_;
4441 const char* argument_descriptor = method->GetTypeDescriptorFromTypeIdx(argument_type_index);
4442 if (strcmp(argument_descriptor, "[Ljava/lang/Object;") != 0) {
4443 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
4444 << "Signature polymorphic method has unexpected argument type: " << argument_descriptor;
4445 return false;
4446 }
4447
4448 const char* return_descriptor = method->GetReturnTypeDescriptor();
4449 if (strcmp(return_descriptor, "Ljava/lang/Object;") != 0) {
4450 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
4451 << "Signature polymorphic method has unexpected return type: " << return_descriptor;
4452 return false;
4453 }
4454
4455 return true;
4456}
4457
4458bool MethodVerifier::CheckSignaturePolymorphicReceiver(const Instruction* inst) {
4459 const RegType& this_type = work_line_->GetInvocationThis(this, inst);
Andreas Gampeeed3a5d2017-11-29 14:58:34 -08004460 if (this_type.IsZeroOrNull()) {
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004461 /* null pointer always passes (and always fails at run time) */
4462 return true;
4463 } else if (!this_type.IsNonZeroReferenceTypes()) {
4464 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
4465 << "invoke-polymorphic receiver is not a reference: "
4466 << this_type;
4467 return false;
4468 } else if (this_type.IsUninitializedReference()) {
4469 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
4470 << "invoke-polymorphic receiver is uninitialized: "
4471 << this_type;
4472 return false;
4473 } else if (!this_type.HasClass()) {
4474 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
4475 << "invoke-polymorphic receiver has no class: "
4476 << this_type;
4477 return false;
4478 } else if (!this_type.GetClass()->IsSubClass(mirror::MethodHandle::StaticClass())) {
4479 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
4480 << "invoke-polymorphic receiver is not a subclass of MethodHandle: "
4481 << this_type;
4482 return false;
4483 }
4484 return true;
Ian Rogersd81871c2011-10-03 13:57:23 -07004485}
4486
Mathieu Chartiere401d142015-04-22 13:56:20 -07004487ArtMethod* MethodVerifier::GetQuickInvokedMethod(const Instruction* inst, RegisterLine* reg_line,
4488 bool is_range, bool allow_failure) {
Mathieu Chartier091d2382015-03-06 10:59:06 -08004489 if (is_range) {
4490 DCHECK_EQ(inst->Opcode(), Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
4491 } else {
4492 DCHECK_EQ(inst->Opcode(), Instruction::INVOKE_VIRTUAL_QUICK);
4493 }
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004494 const RegType& actual_arg_type = reg_line->GetInvocationThis(this, inst, allow_failure);
Ian Rogers9bc54402014-04-17 16:40:01 -07004495 if (!actual_arg_type.HasClass()) {
4496 VLOG(verifier) << "Failed to get mirror::Class* from '" << actual_arg_type << "'";
Andreas Gampe63981562014-04-17 12:28:43 -07004497 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004498 }
Ian Rogersa4cf1df2014-05-07 19:47:17 -07004499 mirror::Class* klass = actual_arg_type.GetClass();
Mingyao Yang2cdbad72014-07-16 10:44:41 -07004500 mirror::Class* dispatch_class;
Ian Rogersa4cf1df2014-05-07 19:47:17 -07004501 if (klass->IsInterface()) {
4502 // Derive Object.class from Class.class.getSuperclass().
4503 mirror::Class* object_klass = klass->GetClass()->GetSuperClass();
Andreas Gampe7c038102014-10-27 20:08:46 -07004504 if (FailOrAbort(this, object_klass->IsObjectClass(),
Mathieu Chartier091d2382015-03-06 10:59:06 -08004505 "Failed to find Object class in quickened invoke receiver", work_insn_idx_)) {
Andreas Gampe7c038102014-10-27 20:08:46 -07004506 return nullptr;
4507 }
Mingyao Yang2cdbad72014-07-16 10:44:41 -07004508 dispatch_class = object_klass;
Ian Rogersa4cf1df2014-05-07 19:47:17 -07004509 } else {
Mingyao Yang2cdbad72014-07-16 10:44:41 -07004510 dispatch_class = klass;
Ian Rogersa4cf1df2014-05-07 19:47:17 -07004511 }
Mathieu Chartier091d2382015-03-06 10:59:06 -08004512 if (!dispatch_class->HasVTable()) {
4513 FailOrAbort(this, allow_failure, "Receiver class has no vtable for quickened invoke at ",
4514 work_insn_idx_);
Andreas Gampe7c038102014-10-27 20:08:46 -07004515 return nullptr;
4516 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004517 uint16_t vtable_index = is_range ? inst->VRegB_3rc() : inst->VRegB_35c();
Mathieu Chartiere401d142015-04-22 13:56:20 -07004518 auto* cl = Runtime::Current()->GetClassLinker();
4519 auto pointer_size = cl->GetImagePointerSize();
Mathieu Chartier091d2382015-03-06 10:59:06 -08004520 if (static_cast<int32_t>(vtable_index) >= dispatch_class->GetVTableLength()) {
4521 FailOrAbort(this, allow_failure,
4522 "Receiver class has not enough vtable slots for quickened invoke at ",
4523 work_insn_idx_);
Andreas Gampe7c038102014-10-27 20:08:46 -07004524 return nullptr;
4525 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07004526 ArtMethod* res_method = dispatch_class->GetVTableEntry(vtable_index, pointer_size);
Mathieu Chartier091d2382015-03-06 10:59:06 -08004527 if (self_->IsExceptionPending()) {
4528 FailOrAbort(this, allow_failure, "Unexpected exception pending for quickened invoke at ",
4529 work_insn_idx_);
Andreas Gampe7c038102014-10-27 20:08:46 -07004530 return nullptr;
4531 }
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004532 return res_method;
4533}
4534
Mathieu Chartiere401d142015-04-22 13:56:20 -07004535ArtMethod* MethodVerifier::VerifyInvokeVirtualQuickArgs(const Instruction* inst, bool is_range) {
Andreas Gampe76bd8802014-12-10 16:43:58 -08004536 DCHECK(Runtime::Current()->IsStarted() || verify_to_dump_)
David Sehr709b0702016-10-13 09:12:37 -07004537 << dex_file_->PrettyMethod(dex_method_idx_, true) << "@" << work_insn_idx_;
Andreas Gampe76bd8802014-12-10 16:43:58 -08004538
Mathieu Chartiere401d142015-04-22 13:56:20 -07004539 ArtMethod* res_method = GetQuickInvokedMethod(inst, work_line_.get(), is_range, false);
Ian Rogers7b078e82014-09-10 14:44:24 -07004540 if (res_method == nullptr) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004541 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer method from " << inst->Name();
Ian Rogers7b078e82014-09-10 14:44:24 -07004542 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004543 }
Andreas Gampe7c038102014-10-27 20:08:46 -07004544 if (FailOrAbort(this, !res_method->IsDirect(), "Quick-invoked method is direct at ",
4545 work_insn_idx_)) {
4546 return nullptr;
4547 }
4548 if (FailOrAbort(this, !res_method->IsStatic(), "Quick-invoked method is static at ",
4549 work_insn_idx_)) {
4550 return nullptr;
4551 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004552
4553 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
4554 // match the call to the signature. Also, we might be calling through an abstract method
4555 // definition (which doesn't have register count values).
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004556 const RegType& actual_arg_type = work_line_->GetInvocationThis(this, inst);
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004557 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
Ian Rogers7b078e82014-09-10 14:44:24 -07004558 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004559 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004560 const size_t expected_args = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
4561 /* caught by static verifier */
4562 DCHECK(is_range || expected_args <= 5);
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08004563 if (expected_args > code_item_accessor_.OutsSize()) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004564 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08004565 << ") exceeds outsSize (" << code_item_accessor_.OutsSize() << ")";
Ian Rogers7b078e82014-09-10 14:44:24 -07004566 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004567 }
4568
4569 /*
4570 * Check the "this" argument, which must be an instance of the class that declared the method.
4571 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
4572 * rigorous check here (which is okay since we have to do it at runtime).
4573 */
David Brazdil68b5c0b2016-01-19 14:25:29 +00004574 // Note: given an uninitialized type, this should always fail. Constructors aren't virtual.
4575 if (actual_arg_type.IsUninitializedTypes() && !res_method->IsConstructor()) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004576 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
Ian Rogers7b078e82014-09-10 14:44:24 -07004577 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004578 }
Andreas Gampeeed3a5d2017-11-29 14:58:34 -08004579 if (!actual_arg_type.IsZeroOrNull()) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004580 mirror::Class* klass = res_method->GetDeclaringClass();
Ian Rogers1ff3c982014-08-12 02:30:58 -07004581 std::string temp;
Ian Rogersd8f69b02014-09-10 21:43:52 +00004582 const RegType& res_method_class =
Andreas Gampef23f33d2015-06-23 14:18:17 -07004583 FromClass(klass->GetDescriptor(&temp), klass, klass->CannotBeAssignedFromOtherTypes());
David Brazdilca3c8c32016-09-06 14:04:48 +01004584 if (!res_method_class.IsAssignableFrom(actual_arg_type, this)) {
David Brazdil68b5c0b2016-01-19 14:25:29 +00004585 Fail(actual_arg_type.IsUninitializedTypes() // Just overcautious - should have never
4586 ? VERIFY_ERROR_BAD_CLASS_HARD // quickened this.
4587 : actual_arg_type.IsUnresolvedTypes()
4588 ? VERIFY_ERROR_NO_CLASS
4589 : VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004590 << "' not instance of '" << res_method_class << "'";
Ian Rogers7b078e82014-09-10 14:44:24 -07004591 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004592 }
4593 }
4594 /*
4595 * Process the target method's signature. This signature may or may not
4596 * have been verified, so we can't assume it's properly formed.
4597 */
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07004598 const DexFile::TypeList* params = res_method->GetParameterTypeList();
Ian Rogers7b078e82014-09-10 14:44:24 -07004599 size_t params_size = params == nullptr ? 0 : params->Size();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004600 uint32_t arg[5];
4601 if (!is_range) {
Ian Rogers29a26482014-05-02 15:27:29 -07004602 inst->GetVarArgs(arg);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004603 }
4604 size_t actual_args = 1;
4605 for (size_t param_index = 0; param_index < params_size; param_index++) {
4606 if (actual_args >= expected_args) {
David Sehr709b0702016-10-13 09:12:37 -07004607 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invalid call to '"
4608 << res_method->PrettyMethod()
Brian Carlstrom93c33962013-07-26 10:37:43 -07004609 << "'. Expected " << expected_args
4610 << " arguments, processing argument " << actual_args
4611 << " (where longs/doubles count twice).";
Ian Rogers7b078e82014-09-10 14:44:24 -07004612 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004613 }
4614 const char* descriptor =
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07004615 res_method->GetTypeDescriptorFromTypeIdx(params->GetTypeItem(param_index).type_idx_);
Ian Rogers7b078e82014-09-10 14:44:24 -07004616 if (descriptor == nullptr) {
David Sehr709b0702016-10-13 09:12:37 -07004617 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of "
4618 << res_method->PrettyMethod()
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004619 << " missing signature component";
Ian Rogers7b078e82014-09-10 14:44:24 -07004620 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004621 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00004622 const RegType& reg_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004623 uint32_t get_reg = is_range ? inst->VRegC_3rc() + actual_args : arg[actual_args];
Ian Rogers7b078e82014-09-10 14:44:24 -07004624 if (!work_line_->VerifyRegisterType(this, get_reg, reg_type)) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004625 return res_method;
4626 }
4627 actual_args = reg_type.IsLongOrDoubleTypes() ? actual_args + 2 : actual_args + 1;
4628 }
4629 if (actual_args != expected_args) {
David Sehr709b0702016-10-13 09:12:37 -07004630 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of "
4631 << res_method->PrettyMethod() << " expected "
4632 << expected_args << " arguments, found " << actual_args;
Ian Rogers7b078e82014-09-10 14:44:24 -07004633 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004634 } else {
4635 return res_method;
4636 }
4637}
4638
Ian Rogers62342ec2013-06-11 10:26:37 -07004639void MethodVerifier::VerifyNewArray(const Instruction* inst, bool is_filled, bool is_range) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08004640 dex::TypeIndex type_idx;
Sebastien Hertz5243e912013-05-21 10:55:07 +02004641 if (!is_filled) {
4642 DCHECK_EQ(inst->Opcode(), Instruction::NEW_ARRAY);
Andreas Gampea5b09a62016-11-17 15:21:22 -08004643 type_idx = dex::TypeIndex(inst->VRegC_22c());
Sebastien Hertz5243e912013-05-21 10:55:07 +02004644 } else if (!is_range) {
4645 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY);
Andreas Gampea5b09a62016-11-17 15:21:22 -08004646 type_idx = dex::TypeIndex(inst->VRegB_35c());
Sebastien Hertz5243e912013-05-21 10:55:07 +02004647 } else {
4648 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY_RANGE);
Andreas Gampea5b09a62016-11-17 15:21:22 -08004649 type_idx = dex::TypeIndex(inst->VRegB_3rc());
Sebastien Hertz5243e912013-05-21 10:55:07 +02004650 }
Andreas Gampe98be1a92017-08-28 08:25:45 -07004651 const RegType& res_type = ResolveClass<CheckAccess::kYes>(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004652 if (res_type.IsConflict()) { // bad class
4653 DCHECK_NE(failures_.size(), 0U);
Ian Rogers0c4a5062012-02-03 15:18:59 -08004654 } else {
4655 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
4656 if (!res_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07004657 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "new-array on non-array class " << res_type;
Ian Rogers0c4a5062012-02-03 15:18:59 -08004658 } else if (!is_filled) {
4659 /* make sure "size" register is valid type */
Ian Rogers7b078e82014-09-10 14:44:24 -07004660 work_line_->VerifyRegisterType(this, inst->VRegB_22c(), reg_types_.Integer());
Ian Rogers0c4a5062012-02-03 15:18:59 -08004661 /* set register type to array class */
Ian Rogersd8f69b02014-09-10 21:43:52 +00004662 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
Andreas Gampead238ce2015-08-24 21:13:08 -07004663 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_22c(), precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08004664 } else {
Andreas Gampebb18a032016-03-22 20:34:25 -07004665 DCHECK(!res_type.IsUnresolvedMergedReference());
Ian Rogers0c4a5062012-02-03 15:18:59 -08004666 // Verify each register. If "arg_count" is bad, VerifyRegisterType() will run off the end of
4667 // the list and fail. It's legal, if silly, for arg_count to be zero.
Ian Rogersd8f69b02014-09-10 21:43:52 +00004668 const RegType& expected_type = reg_types_.GetComponentType(res_type, GetClassLoader());
Sebastien Hertz5243e912013-05-21 10:55:07 +02004669 uint32_t arg_count = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
4670 uint32_t arg[5];
4671 if (!is_range) {
Ian Rogers29a26482014-05-02 15:27:29 -07004672 inst->GetVarArgs(arg);
Sebastien Hertz5243e912013-05-21 10:55:07 +02004673 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08004674 for (size_t ui = 0; ui < arg_count; ui++) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02004675 uint32_t get_reg = is_range ? inst->VRegC_3rc() + ui : arg[ui];
Ian Rogers7b078e82014-09-10 14:44:24 -07004676 if (!work_line_->VerifyRegisterType(this, get_reg, expected_type)) {
4677 work_line_->SetResultRegisterType(this, reg_types_.Conflict());
Ian Rogers0c4a5062012-02-03 15:18:59 -08004678 return;
4679 }
4680 }
4681 // filled-array result goes into "result" register
Ian Rogersd8f69b02014-09-10 21:43:52 +00004682 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
Ian Rogers7b078e82014-09-10 14:44:24 -07004683 work_line_->SetResultRegisterType(this, precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08004684 }
4685 }
4686}
4687
Sebastien Hertz5243e912013-05-21 10:55:07 +02004688void MethodVerifier::VerifyAGet(const Instruction* inst,
Ian Rogersd8f69b02014-09-10 21:43:52 +00004689 const RegType& insn_type, bool is_primitive) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004690 const RegType& index_type = work_line_->GetRegisterType(this, inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07004691 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07004692 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07004693 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004694 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegB_23x());
Andreas Gampeeed3a5d2017-11-29 14:58:34 -08004695 if (array_type.IsZeroOrNull()) {
Ian Rogers89310de2012-02-01 13:47:30 -08004696 // Null array class; this code path will fail at runtime. Infer a merge-able type from the
Andreas Gampe52f205a2017-12-01 12:16:07 -08004697 // instruction type.
4698 if (!is_primitive) {
4699 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), reg_types_.Null());
4700 } else if (insn_type.IsInteger()) {
4701 // Pick a non-zero constant (to distinguish with null) that can fit in any primitive.
4702 // We cannot use 'insn_type' as it could be a float array or an int array.
4703 work_line_->SetRegisterType<LockOp::kClear>(
4704 this, inst->VRegA_23x(), DetermineCat1Constant(1, need_precise_constants_));
4705 } else if (insn_type.IsCategory1Types()) {
4706 // Category 1
4707 // The 'insn_type' is exactly the type we need.
4708 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), insn_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07004709 } else {
Ian Rogers89310de2012-02-01 13:47:30 -08004710 // Category 2
Ian Rogers7b078e82014-09-10 14:44:24 -07004711 work_line_->SetRegisterTypeWide(this, inst->VRegA_23x(),
4712 reg_types_.FromCat2ConstLo(0, false),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004713 reg_types_.FromCat2ConstHi(0, false));
Ian Rogers89310de2012-02-01 13:47:30 -08004714 }
jeffhaofc3144e2012-02-01 17:21:15 -08004715 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07004716 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aget";
Andreas Gampe8d8fc482016-03-25 16:24:20 -07004717 } else if (array_type.IsUnresolvedMergedReference()) {
Andreas Gampebb18a032016-03-22 20:34:25 -07004718 // Unresolved array types must be reference array types.
4719 if (is_primitive) {
4720 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type
4721 << " source for category 1 aget";
4722 } else {
4723 Fail(VERIFY_ERROR_NO_CLASS) << "cannot verify aget for " << array_type
4724 << " because of missing class";
Andreas Gampe8d8fc482016-03-25 16:24:20 -07004725 // Approximate with java.lang.Object[].
4726 work_line_->SetRegisterType<LockOp::kClear>(this,
4727 inst->VRegA_23x(),
4728 reg_types_.JavaLangObject(false));
Andreas Gampebb18a032016-03-22 20:34:25 -07004729 }
Ian Rogers89310de2012-02-01 13:47:30 -08004730 } else {
4731 /* verify the class */
Ian Rogersd8f69b02014-09-10 21:43:52 +00004732 const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
jeffhaofc3144e2012-02-01 17:21:15 -08004733 if (!component_type.IsReferenceTypes() && !is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07004734 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08004735 << " source for aget-object";
4736 } else if (component_type.IsNonZeroReferenceTypes() && is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07004737 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08004738 << " source for category 1 aget";
4739 } else if (is_primitive && !insn_type.Equals(component_type) &&
4740 !((insn_type.IsInteger() && component_type.IsFloat()) ||
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004741 (insn_type.IsLong() && component_type.IsDouble()))) {
4742 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array type " << array_type
4743 << " incompatible with aget of type " << insn_type;
Ian Rogers89310de2012-02-01 13:47:30 -08004744 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004745 // Use knowledge of the field type which is stronger than the type inferred from the
4746 // instruction, which can't differentiate object types and ints from floats, longs from
4747 // doubles.
4748 if (!component_type.IsLowHalf()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07004749 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), component_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004750 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004751 work_line_->SetRegisterTypeWide(this, inst->VRegA_23x(), component_type,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004752 component_type.HighHalf(&reg_types_));
4753 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004754 }
4755 }
4756 }
4757}
4758
Ian Rogersd8f69b02014-09-10 21:43:52 +00004759void MethodVerifier::VerifyPrimitivePut(const RegType& target_type, const RegType& insn_type,
Jeff Haofe1f7c82013-08-01 14:50:24 -07004760 const uint32_t vregA) {
4761 // Primitive assignability rules are weaker than regular assignability rules.
4762 bool instruction_compatible;
4763 bool value_compatible;
Ian Rogers7b078e82014-09-10 14:44:24 -07004764 const RegType& value_type = work_line_->GetRegisterType(this, vregA);
Jeff Haofe1f7c82013-08-01 14:50:24 -07004765 if (target_type.IsIntegralTypes()) {
Jeff Haoa4647482013-08-06 15:35:47 -07004766 instruction_compatible = target_type.Equals(insn_type);
Jeff Haofe1f7c82013-08-01 14:50:24 -07004767 value_compatible = value_type.IsIntegralTypes();
4768 } else if (target_type.IsFloat()) {
4769 instruction_compatible = insn_type.IsInteger(); // no put-float, so expect put-int
4770 value_compatible = value_type.IsFloatTypes();
4771 } else if (target_type.IsLong()) {
4772 instruction_compatible = insn_type.IsLong();
Andreas Gampe376fa682014-09-07 13:06:12 -07004773 // Additional register check: this is not checked statically (as part of VerifyInstructions),
4774 // as target_type depends on the resolved type of the field.
4775 if (instruction_compatible && work_line_->NumRegs() > vregA + 1) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004776 const RegType& value_type_hi = work_line_->GetRegisterType(this, vregA + 1);
Andreas Gampe376fa682014-09-07 13:06:12 -07004777 value_compatible = value_type.IsLongTypes() && value_type.CheckWidePair(value_type_hi);
4778 } else {
4779 value_compatible = false;
4780 }
Jeff Haofe1f7c82013-08-01 14:50:24 -07004781 } else if (target_type.IsDouble()) {
4782 instruction_compatible = insn_type.IsLong(); // no put-double, so expect put-long
Andreas Gampe376fa682014-09-07 13:06:12 -07004783 // Additional register check: this is not checked statically (as part of VerifyInstructions),
4784 // as target_type depends on the resolved type of the field.
4785 if (instruction_compatible && work_line_->NumRegs() > vregA + 1) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004786 const RegType& value_type_hi = work_line_->GetRegisterType(this, vregA + 1);
Andreas Gampe376fa682014-09-07 13:06:12 -07004787 value_compatible = value_type.IsDoubleTypes() && value_type.CheckWidePair(value_type_hi);
4788 } else {
4789 value_compatible = false;
4790 }
Jeff Haofe1f7c82013-08-01 14:50:24 -07004791 } else {
4792 instruction_compatible = false; // reference with primitive store
4793 value_compatible = false; // unused
4794 }
4795 if (!instruction_compatible) {
4796 // This is a global failure rather than a class change failure as the instructions and
4797 // the descriptors for the type should have been consistent within the same file at
4798 // compile time.
4799 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "put insn has type '" << insn_type
4800 << "' but expected type '" << target_type << "'";
4801 return;
4802 }
4803 if (!value_compatible) {
4804 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
4805 << " of type " << value_type << " but expected " << target_type << " for put";
4806 return;
4807 }
4808}
4809
Sebastien Hertz5243e912013-05-21 10:55:07 +02004810void MethodVerifier::VerifyAPut(const Instruction* inst,
Ian Rogersd8f69b02014-09-10 21:43:52 +00004811 const RegType& insn_type, bool is_primitive) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004812 const RegType& index_type = work_line_->GetRegisterType(this, inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07004813 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07004814 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07004815 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004816 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegB_23x());
Andreas Gampeeed3a5d2017-11-29 14:58:34 -08004817 if (array_type.IsZeroOrNull()) {
Nicolas Geoffray66389fb2015-06-19 10:35:42 +01004818 // Null array type; this code path will fail at runtime.
4819 // Still check that the given value matches the instruction's type.
Andreas Gampe4bf4c782015-08-14 14:07:43 -07004820 // Note: this is, as usual, complicated by the fact the the instruction isn't fully typed
4821 // and fits multiple register types.
4822 const RegType* modified_reg_type = &insn_type;
4823 if ((modified_reg_type == &reg_types_.Integer()) ||
4824 (modified_reg_type == &reg_types_.LongLo())) {
4825 // May be integer or float | long or double. Overwrite insn_type accordingly.
4826 const RegType& value_type = work_line_->GetRegisterType(this, inst->VRegA_23x());
4827 if (modified_reg_type == &reg_types_.Integer()) {
4828 if (&value_type == &reg_types_.Float()) {
4829 modified_reg_type = &value_type;
4830 }
4831 } else {
4832 if (&value_type == &reg_types_.DoubleLo()) {
4833 modified_reg_type = &value_type;
4834 }
4835 }
4836 }
4837 work_line_->VerifyRegisterType(this, inst->VRegA_23x(), *modified_reg_type);
jeffhaofc3144e2012-02-01 17:21:15 -08004838 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07004839 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aput";
Andreas Gampe8d8fc482016-03-25 16:24:20 -07004840 } else if (array_type.IsUnresolvedMergedReference()) {
Andreas Gampebb18a032016-03-22 20:34:25 -07004841 // Unresolved array types must be reference array types.
4842 if (is_primitive) {
4843 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "put insn has type '" << insn_type
4844 << "' but unresolved type '" << array_type << "'";
4845 } else {
4846 Fail(VERIFY_ERROR_NO_CLASS) << "cannot verify aput for " << array_type
4847 << " because of missing class";
4848 }
Ian Rogers89310de2012-02-01 13:47:30 -08004849 } else {
Ian Rogersd8f69b02014-09-10 21:43:52 +00004850 const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
Jeff Haofe1f7c82013-08-01 14:50:24 -07004851 const uint32_t vregA = inst->VRegA_23x();
Jeff Haob24b4a72013-07-31 13:47:31 -07004852 if (is_primitive) {
Jeff Haofe1f7c82013-08-01 14:50:24 -07004853 VerifyPrimitivePut(component_type, insn_type, vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07004854 } else {
Jeff Haob24b4a72013-07-31 13:47:31 -07004855 if (!component_type.IsReferenceTypes()) {
4856 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
4857 << " source for aput-object";
4858 } else {
4859 // The instruction agrees with the type of array, confirm the value to be stored does too
4860 // Note: we use the instruction type (rather than the component type) for aput-object as
4861 // incompatible classes will be caught at runtime as an array store exception
Ian Rogers7b078e82014-09-10 14:44:24 -07004862 work_line_->VerifyRegisterType(this, vregA, insn_type);
Jeff Haob24b4a72013-07-31 13:47:31 -07004863 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004864 }
4865 }
4866 }
4867}
4868
Mathieu Chartierc7853442015-03-27 14:35:38 -07004869ArtField* MethodVerifier::GetStaticField(int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08004870 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
4871 // Check access to class
Andreas Gampe98be1a92017-08-28 08:25:45 -07004872 const RegType& klass_type = ResolveClass<CheckAccess::kYes>(field_id.class_idx_);
Brian Carlstrom7934ac22013-07-26 10:54:15 -07004873 if (klass_type.IsConflict()) { // bad class
Ian Rogersad0b3a32012-04-16 14:50:24 -07004874 AppendToLastFailMessage(StringPrintf(" in attempt to access static field %d (%s) in %s",
4875 field_idx, dex_file_->GetFieldName(field_id),
4876 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers7b078e82014-09-10 14:44:24 -07004877 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08004878 }
Elliott Hughesb25c3f62012-03-26 16:35:06 -07004879 if (klass_type.IsUnresolvedTypes()) {
Andreas Gampe629be512017-08-25 17:09:32 -07004880 // Accessibility checks depend on resolved fields.
4881 DCHECK(klass_type.Equals(GetDeclaringClass()) || !failures_.empty());
4882
Ian Rogers7b078e82014-09-10 14:44:24 -07004883 return nullptr; // Can't resolve Class so no more to do here, will do checking at runtime.
Ian Rogers90040192011-12-16 08:54:29 -08004884 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07004885 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Vladimir Markoe11dd502017-12-08 14:09:45 +00004886 ArtField* field = class_linker->ResolveFieldJLS(field_idx, dex_cache_, class_loader_);
David Brazdilca3c8c32016-09-06 14:04:48 +01004887
4888 // Record result of the field resolution attempt.
4889 VerifierDeps::MaybeRecordFieldResolution(*dex_file_, field_idx, field);
4890
Ian Rogers7b078e82014-09-10 14:44:24 -07004891 if (field == nullptr) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07004892 VLOG(verifier) << "Unable to resolve static field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07004893 << dex_file_->GetFieldName(field_id) << ") in "
4894 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogers7b078e82014-09-10 14:44:24 -07004895 DCHECK(self_->IsExceptionPending());
4896 self_->ClearException();
4897 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004898 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
4899 field->GetAccessFlags())) {
David Sehr709b0702016-10-13 09:12:37 -07004900 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access static field " << field->PrettyField()
Ian Rogersad0b3a32012-04-16 14:50:24 -07004901 << " from " << GetDeclaringClass();
Ian Rogers7b078e82014-09-10 14:44:24 -07004902 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07004903 } else if (!field->IsStatic()) {
David Sehr709b0702016-10-13 09:12:37 -07004904 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << field->PrettyField() << " to be static";
Ian Rogers7b078e82014-09-10 14:44:24 -07004905 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07004906 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07004907 return field;
Ian Rogersd81871c2011-10-03 13:57:23 -07004908}
4909
Mathieu Chartierc7853442015-03-27 14:35:38 -07004910ArtField* MethodVerifier::GetInstanceField(const RegType& obj_type, int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08004911 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
Aart Bik31883642016-06-06 15:02:44 -07004912 // Check access to class.
Andreas Gampe98be1a92017-08-28 08:25:45 -07004913 const RegType& klass_type = ResolveClass<CheckAccess::kYes>(field_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004914 if (klass_type.IsConflict()) {
4915 AppendToLastFailMessage(StringPrintf(" in attempt to access instance field %d (%s) in %s",
4916 field_idx, dex_file_->GetFieldName(field_id),
4917 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers7b078e82014-09-10 14:44:24 -07004918 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08004919 }
jeffhao8cd6dda2012-02-22 10:15:34 -08004920 if (klass_type.IsUnresolvedTypes()) {
Andreas Gampe629be512017-08-25 17:09:32 -07004921 // Accessibility checks depend on resolved fields.
4922 DCHECK(klass_type.Equals(GetDeclaringClass()) || !failures_.empty());
4923
Ian Rogers7b078e82014-09-10 14:44:24 -07004924 return nullptr; // Can't resolve Class so no more to do here
Ian Rogers90040192011-12-16 08:54:29 -08004925 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07004926 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Vladimir Markoe11dd502017-12-08 14:09:45 +00004927 ArtField* field = class_linker->ResolveFieldJLS(field_idx, dex_cache_, class_loader_);
David Brazdilca3c8c32016-09-06 14:04:48 +01004928
4929 // Record result of the field resolution attempt.
4930 VerifierDeps::MaybeRecordFieldResolution(*dex_file_, field_idx, field);
4931
Ian Rogers7b078e82014-09-10 14:44:24 -07004932 if (field == nullptr) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07004933 VLOG(verifier) << "Unable to resolve instance field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07004934 << dex_file_->GetFieldName(field_id) << ") in "
4935 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogers7b078e82014-09-10 14:44:24 -07004936 DCHECK(self_->IsExceptionPending());
4937 self_->ClearException();
4938 return nullptr;
Andreas Gampeeed3a5d2017-11-29 14:58:34 -08004939 } else if (obj_type.IsZeroOrNull()) {
Aart Bik31883642016-06-06 15:02:44 -07004940 // Cannot infer and check type, however, access will cause null pointer exception.
4941 // Fall through into a few last soft failure checks below.
Stephen Kyle695c5982014-08-22 15:03:07 +01004942 } else if (!obj_type.IsReferenceTypes()) {
Aart Bik31883642016-06-06 15:02:44 -07004943 // Trying to read a field from something that isn't a reference.
Stephen Kyle695c5982014-08-22 15:03:07 +01004944 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance field access on object that has "
4945 << "non-reference type " << obj_type;
Ian Rogers7b078e82014-09-10 14:44:24 -07004946 return nullptr;
Ian Rogerse1758fe2012-04-19 11:31:15 -07004947 } else {
David Brazdil0d638bb2016-07-27 15:29:25 +01004948 std::string temp;
Mathieu Chartier3398c782016-09-30 10:27:43 -07004949 ObjPtr<mirror::Class> klass = field->GetDeclaringClass();
Ian Rogersd8f69b02014-09-10 21:43:52 +00004950 const RegType& field_klass =
Mathieu Chartier3398c782016-09-30 10:27:43 -07004951 FromClass(klass->GetDescriptor(&temp),
Mathieu Chartier1cc62e42016-10-03 18:01:28 -07004952 klass.Ptr(),
Mathieu Chartier3398c782016-09-30 10:27:43 -07004953 klass->CannotBeAssignedFromOtherTypes());
David Brazdil68b5c0b2016-01-19 14:25:29 +00004954 if (obj_type.IsUninitializedTypes()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07004955 // Field accesses through uninitialized references are only allowable for constructors where
David Brazdil68b5c0b2016-01-19 14:25:29 +00004956 // the field is declared in this class.
4957 // Note: this IsConstructor check is technically redundant, as UninitializedThis should only
4958 // appear in constructors.
4959 if (!obj_type.IsUninitializedThisReference() ||
4960 !IsConstructor() ||
4961 !field_klass.Equals(GetDeclaringClass())) {
David Sehr709b0702016-10-13 09:12:37 -07004962 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "cannot access instance field " << field->PrettyField()
David Brazdil68b5c0b2016-01-19 14:25:29 +00004963 << " of a not fully initialized object within the context"
David Sehr709b0702016-10-13 09:12:37 -07004964 << " of " << dex_file_->PrettyMethod(dex_method_idx_);
David Brazdil68b5c0b2016-01-19 14:25:29 +00004965 return nullptr;
4966 }
David Brazdilca3c8c32016-09-06 14:04:48 +01004967 } else if (!field_klass.IsAssignableFrom(obj_type, this)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07004968 // Trying to access C1.field1 using reference of type C2, which is neither C1 or a sub-class
4969 // of C1. For resolution to occur the declared class of the field must be compatible with
4970 // obj_type, we've discovered this wasn't so, so report the field didn't exist.
Andreas Gampe66596242016-04-14 10:55:04 -07004971 VerifyError type;
4972 bool is_aot = Runtime::Current()->IsAotCompiler();
4973 if (is_aot && (field_klass.IsUnresolvedTypes() || obj_type.IsUnresolvedTypes())) {
4974 // Compiler & unresolved types involved, retry at runtime.
4975 type = VerifyError::VERIFY_ERROR_NO_CLASS;
4976 } else {
Andreas Gampe8f4ade02016-04-15 10:09:16 -07004977 // Classes known (resolved; and thus assignability check is precise), or we are at runtime
4978 // and still missing classes. This is a hard failure.
Andreas Gampe66596242016-04-14 10:55:04 -07004979 type = VerifyError::VERIFY_ERROR_BAD_CLASS_HARD;
4980 }
David Sehr709b0702016-10-13 09:12:37 -07004981 Fail(type) << "cannot access instance field " << field->PrettyField()
Andreas Gampe66596242016-04-14 10:55:04 -07004982 << " from object of type " << obj_type;
Ian Rogers7b078e82014-09-10 14:44:24 -07004983 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004984 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004985 }
Aart Bik31883642016-06-06 15:02:44 -07004986
4987 // Few last soft failure checks.
4988 if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
4989 field->GetAccessFlags())) {
David Sehr709b0702016-10-13 09:12:37 -07004990 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access instance field " << field->PrettyField()
Aart Bik31883642016-06-06 15:02:44 -07004991 << " from " << GetDeclaringClass();
4992 return nullptr;
4993 } else if (field->IsStatic()) {
David Sehr709b0702016-10-13 09:12:37 -07004994 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << field->PrettyField()
Aart Bik31883642016-06-06 15:02:44 -07004995 << " to not be static";
4996 return nullptr;
4997 }
4998
4999 return field;
Ian Rogersd81871c2011-10-03 13:57:23 -07005000}
5001
Andreas Gampe896df402014-10-20 22:25:29 -07005002template <MethodVerifier::FieldAccessType kAccType>
5003void MethodVerifier::VerifyISFieldAccess(const Instruction* inst, const RegType& insn_type,
5004 bool is_primitive, bool is_static) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02005005 uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
Mathieu Chartierc7853442015-03-27 14:35:38 -07005006 ArtField* field;
Ian Rogersb94a27b2011-10-26 00:33:41 -07005007 if (is_static) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07005008 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07005009 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07005010 const RegType& object_type = work_line_->GetRegisterType(this, inst->VRegB_22c());
David Brazdil68b5c0b2016-01-19 14:25:29 +00005011
5012 // One is not allowed to access fields on uninitialized references, except to write to
5013 // fields in the constructor (before calling another constructor).
5014 // GetInstanceField does an assignability check which will fail for uninitialized types.
5015 // We thus modify the type if the uninitialized reference is a "this" reference (this also
5016 // checks at the same time that we're verifying a constructor).
5017 bool should_adjust = (kAccType == FieldAccessType::kAccPut) &&
5018 object_type.IsUninitializedThisReference();
5019 const RegType& adjusted_type = should_adjust
5020 ? GetRegTypeCache()->FromUninitialized(object_type)
5021 : object_type;
5022 field = GetInstanceField(adjusted_type, field_idx);
Andreas Gampe896df402014-10-20 22:25:29 -07005023 if (UNLIKELY(have_pending_hard_failure_)) {
5024 return;
5025 }
Alex Light4a2c8fc2016-02-12 11:01:54 -08005026 if (should_adjust) {
5027 if (field == nullptr) {
5028 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "Might be accessing a superclass instance field prior "
5029 << "to the superclass being initialized in "
David Sehr709b0702016-10-13 09:12:37 -07005030 << dex_file_->PrettyMethod(dex_method_idx_);
Alex Light4a2c8fc2016-02-12 11:01:54 -08005031 } else if (field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
5032 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "cannot access superclass instance field "
David Sehr709b0702016-10-13 09:12:37 -07005033 << field->PrettyField() << " of a not fully initialized "
Alex Light4a2c8fc2016-02-12 11:01:54 -08005034 << "object within the context of "
David Sehr709b0702016-10-13 09:12:37 -07005035 << dex_file_->PrettyMethod(dex_method_idx_);
Alex Light4a2c8fc2016-02-12 11:01:54 -08005036 return;
5037 }
5038 }
Ian Rogersb94a27b2011-10-26 00:33:41 -07005039 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00005040 const RegType* field_type = nullptr;
Ian Rogers7b078e82014-09-10 14:44:24 -07005041 if (field != nullptr) {
Andreas Gampe896df402014-10-20 22:25:29 -07005042 if (kAccType == FieldAccessType::kAccPut) {
5043 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
David Sehr709b0702016-10-13 09:12:37 -07005044 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << field->PrettyField()
Andreas Gampe896df402014-10-20 22:25:29 -07005045 << " from other class " << GetDeclaringClass();
Aart Bikc2bc2652016-05-23 14:58:49 -07005046 // Keep hunting for possible hard fails.
Andreas Gampe896df402014-10-20 22:25:29 -07005047 }
5048 }
5049
Mathieu Chartier3398c782016-09-30 10:27:43 -07005050 ObjPtr<mirror::Class> field_type_class =
Vladimir Marko208f6702017-12-08 12:00:50 +00005051 can_load_classes_ ? field->ResolveType() : field->LookupResolvedType();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07005052 if (field_type_class != nullptr) {
Mathieu Chartier3398c782016-09-30 10:27:43 -07005053 field_type = &FromClass(field->GetTypeDescriptor(),
Mathieu Chartier1cc62e42016-10-03 18:01:28 -07005054 field_type_class.Ptr(),
Andreas Gampef23f33d2015-06-23 14:18:17 -07005055 field_type_class->CannotBeAssignedFromOtherTypes());
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08005056 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07005057 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
5058 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07005059 }
Andreas Gampe7da4c402017-08-25 11:30:48 -07005060 } else {
5061 // If we don't have the field (it seems we failed resolution) and this is a PUT, we need to
5062 // redo verification at runtime as the field may be final, unless the field id shows it's in
5063 // the same class.
5064 //
5065 // For simplicity, it is OK to not distinguish compile-time vs runtime, and post this an
5066 // ACCESS_FIELD failure at runtime. This has the same effect as NO_FIELD - punting the class
5067 // to the access-checks interpreter.
5068 //
5069 // Note: see b/34966607. This and above may be changed in the future.
5070 if (kAccType == FieldAccessType::kAccPut) {
5071 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
5072 const char* field_class_descriptor = dex_file_->GetFieldDeclaringClassDescriptor(field_id);
5073 const RegType* field_class_type = &reg_types_.FromDescriptor(GetClassLoader(),
5074 field_class_descriptor,
5075 false);
5076 if (!field_class_type->Equals(GetDeclaringClass())) {
5077 Fail(VERIFY_ERROR_ACCESS_FIELD) << "could not check field put for final field modify of "
5078 << field_class_descriptor
5079 << "."
5080 << dex_file_->GetFieldName(field_id)
5081 << " from other class "
5082 << GetDeclaringClass();
5083 }
5084 }
Ian Rogers0d604842012-04-16 14:50:24 -07005085 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07005086 if (field_type == nullptr) {
5087 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
5088 const char* descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
Mathieu Chartierbf99f772014-08-23 16:37:27 -07005089 field_type = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07005090 }
Sebastien Hertz757b3042014-03-28 14:34:28 +01005091 DCHECK(field_type != nullptr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02005092 const uint32_t vregA = (is_static) ? inst->VRegA_21c() : inst->VRegA_22c();
Andreas Gampe896df402014-10-20 22:25:29 -07005093 static_assert(kAccType == FieldAccessType::kAccPut || kAccType == FieldAccessType::kAccGet,
5094 "Unexpected third access type");
5095 if (kAccType == FieldAccessType::kAccPut) {
5096 // sput or iput.
5097 if (is_primitive) {
5098 VerifyPrimitivePut(*field_type, insn_type, vregA);
Ian Rogersad0b3a32012-04-16 14:50:24 -07005099 } else {
David Brazdilca3c8c32016-09-06 14:04:48 +01005100 if (!insn_type.IsAssignableFrom(*field_type, this)) {
Vladimir Marko414000e2015-06-23 17:45:21 +01005101 // If the field type is not a reference, this is a global failure rather than
5102 // a class change failure as the instructions and the descriptors for the type
5103 // should have been consistent within the same file at compile time.
5104 VerifyError error = field_type->IsReferenceTypes() ? VERIFY_ERROR_BAD_CLASS_SOFT
5105 : VERIFY_ERROR_BAD_CLASS_HARD;
David Sehr709b0702016-10-13 09:12:37 -07005106 Fail(error) << "expected field " << ArtField::PrettyField(field)
Vladimir Marko414000e2015-06-23 17:45:21 +01005107 << " to be compatible with type '" << insn_type
5108 << "' but found type '" << *field_type
5109 << "' in put-object";
Andreas Gampe896df402014-10-20 22:25:29 -07005110 return;
5111 }
5112 work_line_->VerifyRegisterType(this, vregA, *field_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07005113 }
Andreas Gampe896df402014-10-20 22:25:29 -07005114 } else if (kAccType == FieldAccessType::kAccGet) {
5115 // sget or iget.
5116 if (is_primitive) {
5117 if (field_type->Equals(insn_type) ||
5118 (field_type->IsFloat() && insn_type.IsInteger()) ||
5119 (field_type->IsDouble() && insn_type.IsLong())) {
5120 // expected that read is of the correct primitive type or that int reads are reading
5121 // floats or long reads are reading doubles
5122 } else {
5123 // This is a global failure rather than a class change failure as the instructions and
5124 // the descriptors for the type should have been consistent within the same file at
5125 // compile time
David Sehr709b0702016-10-13 09:12:37 -07005126 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << ArtField::PrettyField(field)
Andreas Gampe896df402014-10-20 22:25:29 -07005127 << " to be of type '" << insn_type
5128 << "' but found type '" << *field_type << "' in get";
5129 return;
5130 }
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08005131 } else {
David Brazdilca3c8c32016-09-06 14:04:48 +01005132 if (!insn_type.IsAssignableFrom(*field_type, this)) {
Vladimir Marko414000e2015-06-23 17:45:21 +01005133 // If the field type is not a reference, this is a global failure rather than
5134 // a class change failure as the instructions and the descriptors for the type
5135 // should have been consistent within the same file at compile time.
5136 VerifyError error = field_type->IsReferenceTypes() ? VERIFY_ERROR_BAD_CLASS_SOFT
5137 : VERIFY_ERROR_BAD_CLASS_HARD;
David Sehr709b0702016-10-13 09:12:37 -07005138 Fail(error) << "expected field " << ArtField::PrettyField(field)
Vladimir Marko414000e2015-06-23 17:45:21 +01005139 << " to be compatible with type '" << insn_type
5140 << "' but found type '" << *field_type
5141 << "' in get-object";
Andreas Gampe890da292015-07-06 17:20:18 -07005142 if (error != VERIFY_ERROR_BAD_CLASS_HARD) {
Andreas Gampead238ce2015-08-24 21:13:08 -07005143 work_line_->SetRegisterType<LockOp::kClear>(this, vregA, reg_types_.Conflict());
Andreas Gampe890da292015-07-06 17:20:18 -07005144 }
Andreas Gampe896df402014-10-20 22:25:29 -07005145 return;
5146 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07005147 }
Andreas Gampe896df402014-10-20 22:25:29 -07005148 if (!field_type->IsLowHalf()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07005149 work_line_->SetRegisterType<LockOp::kClear>(this, vregA, *field_type);
Andreas Gampe896df402014-10-20 22:25:29 -07005150 } else {
5151 work_line_->SetRegisterTypeWide(this, vregA, *field_type, field_type->HighHalf(&reg_types_));
5152 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07005153 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07005154 LOG(FATAL) << "Unexpected case.";
Ian Rogersd81871c2011-10-03 13:57:23 -07005155 }
5156}
5157
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08005158ArtField* MethodVerifier::GetQuickFieldAccess(const Instruction* inst, RegisterLine* reg_line) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08005159 DCHECK(IsInstructionIGetQuickOrIPutQuick(inst->Opcode())) << inst->Opcode();
Ian Rogers7b078e82014-09-10 14:44:24 -07005160 const RegType& object_type = reg_line->GetRegisterType(this, inst->VRegB_22c());
Ian Rogers9bc54402014-04-17 16:40:01 -07005161 if (!object_type.HasClass()) {
5162 VLOG(verifier) << "Failed to get mirror::Class* from '" << object_type << "'";
5163 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02005164 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02005165 uint32_t field_offset = static_cast<uint32_t>(inst->VRegC_22c());
Mathieu Chartierc7853442015-03-27 14:35:38 -07005166 ArtField* const f = ArtField::FindInstanceFieldWithOffset(object_type.GetClass(), field_offset);
Sebastien Hertz479fc1e2014-04-04 17:51:34 +02005167 if (f == nullptr) {
5168 VLOG(verifier) << "Failed to find instance field at offset '" << field_offset
David Sehr709b0702016-10-13 09:12:37 -07005169 << "' from '" << mirror::Class::PrettyDescriptor(object_type.GetClass()) << "'";
Andreas Gampe8ddb7f52017-12-04 10:14:08 -08005170 } else {
5171 DCHECK_EQ(f->GetOffset().Uint32Value(), field_offset);
Sebastien Hertz479fc1e2014-04-04 17:51:34 +02005172 }
5173 return f;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02005174}
5175
Andreas Gampe896df402014-10-20 22:25:29 -07005176template <MethodVerifier::FieldAccessType kAccType>
5177void MethodVerifier::VerifyQuickFieldAccess(const Instruction* inst, const RegType& insn_type,
5178 bool is_primitive) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07005179 DCHECK(Runtime::Current()->IsStarted() || verify_to_dump_);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02005180
Mathieu Chartierc7853442015-03-27 14:35:38 -07005181 ArtField* field = GetQuickFieldAccess(inst, work_line_.get());
Ian Rogers7b078e82014-09-10 14:44:24 -07005182 if (field == nullptr) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02005183 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field from " << inst->Name();
5184 return;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02005185 }
Andreas Gampe896df402014-10-20 22:25:29 -07005186
5187 // For an IPUT_QUICK, we now test for final flag of the field.
5188 if (kAccType == FieldAccessType::kAccPut) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02005189 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
David Sehr709b0702016-10-13 09:12:37 -07005190 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << field->PrettyField()
Sebastien Hertzc15853b2013-06-25 17:36:27 +02005191 << " from other class " << GetDeclaringClass();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02005192 return;
5193 }
5194 }
Andreas Gampe896df402014-10-20 22:25:29 -07005195
5196 // Get the field type.
5197 const RegType* field_type;
5198 {
Vladimir Marko4098a7a2017-11-06 16:00:51 +00005199 ObjPtr<mirror::Class> field_type_class =
Vladimir Marko208f6702017-12-08 12:00:50 +00005200 can_load_classes_ ? field->ResolveType() : field->LookupResolvedType();
Andreas Gampe896df402014-10-20 22:25:29 -07005201
5202 if (field_type_class != nullptr) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07005203 field_type = &FromClass(field->GetTypeDescriptor(),
Mathieu Chartier1cc62e42016-10-03 18:01:28 -07005204 field_type_class.Ptr(),
Andreas Gampef23f33d2015-06-23 14:18:17 -07005205 field_type_class->CannotBeAssignedFromOtherTypes());
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02005206 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07005207 Thread* self = Thread::Current();
5208 DCHECK(!can_load_classes_ || self->IsExceptionPending());
5209 self->ClearException();
5210 field_type = &reg_types_.FromDescriptor(field->GetDeclaringClass()->GetClassLoader(),
Mathieu Chartierde40d472015-10-15 17:47:48 -07005211 field->GetTypeDescriptor(),
5212 false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02005213 }
Andreas Gampe896df402014-10-20 22:25:29 -07005214 if (field_type == nullptr) {
5215 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field type from " << inst->Name();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02005216 return;
5217 }
Andreas Gampe896df402014-10-20 22:25:29 -07005218 }
5219
5220 const uint32_t vregA = inst->VRegA_22c();
5221 static_assert(kAccType == FieldAccessType::kAccPut || kAccType == FieldAccessType::kAccGet,
5222 "Unexpected third access type");
5223 if (kAccType == FieldAccessType::kAccPut) {
5224 if (is_primitive) {
5225 // Primitive field assignability rules are weaker than regular assignability rules
5226 bool instruction_compatible;
5227 bool value_compatible;
5228 const RegType& value_type = work_line_->GetRegisterType(this, vregA);
5229 if (field_type->IsIntegralTypes()) {
5230 instruction_compatible = insn_type.IsIntegralTypes();
5231 value_compatible = value_type.IsIntegralTypes();
5232 } else if (field_type->IsFloat()) {
5233 instruction_compatible = insn_type.IsInteger(); // no [is]put-float, so expect [is]put-int
5234 value_compatible = value_type.IsFloatTypes();
5235 } else if (field_type->IsLong()) {
5236 instruction_compatible = insn_type.IsLong();
5237 value_compatible = value_type.IsLongTypes();
5238 } else if (field_type->IsDouble()) {
5239 instruction_compatible = insn_type.IsLong(); // no [is]put-double, so expect [is]put-long
5240 value_compatible = value_type.IsDoubleTypes();
5241 } else {
5242 instruction_compatible = false; // reference field with primitive store
5243 value_compatible = false; // unused
5244 }
5245 if (!instruction_compatible) {
5246 // This is a global failure rather than a class change failure as the instructions and
5247 // the descriptors for the type should have been consistent within the same file at
5248 // compile time
David Sehr709b0702016-10-13 09:12:37 -07005249 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << ArtField::PrettyField(field)
Andreas Gampe896df402014-10-20 22:25:29 -07005250 << " to be of type '" << insn_type
5251 << "' but found type '" << *field_type
5252 << "' in put";
5253 return;
5254 }
5255 if (!value_compatible) {
5256 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
5257 << " of type " << value_type
5258 << " but expected " << *field_type
David Sehr709b0702016-10-13 09:12:37 -07005259 << " for store to " << ArtField::PrettyField(field) << " in put";
Andreas Gampe896df402014-10-20 22:25:29 -07005260 return;
5261 }
5262 } else {
David Brazdilca3c8c32016-09-06 14:04:48 +01005263 if (!insn_type.IsAssignableFrom(*field_type, this)) {
David Sehr709b0702016-10-13 09:12:37 -07005264 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << ArtField::PrettyField(field)
Andreas Gampe896df402014-10-20 22:25:29 -07005265 << " to be compatible with type '" << insn_type
5266 << "' but found type '" << *field_type
5267 << "' in put-object";
5268 return;
5269 }
5270 work_line_->VerifyRegisterType(this, vregA, *field_type);
5271 }
5272 } else if (kAccType == FieldAccessType::kAccGet) {
5273 if (is_primitive) {
5274 if (field_type->Equals(insn_type) ||
5275 (field_type->IsFloat() && insn_type.IsIntegralTypes()) ||
5276 (field_type->IsDouble() && insn_type.IsLongTypes())) {
5277 // expected that read is of the correct primitive type or that int reads are reading
5278 // floats or long reads are reading doubles
5279 } else {
5280 // This is a global failure rather than a class change failure as the instructions and
5281 // the descriptors for the type should have been consistent within the same file at
5282 // compile time
David Sehr709b0702016-10-13 09:12:37 -07005283 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << ArtField::PrettyField(field)
Andreas Gampe896df402014-10-20 22:25:29 -07005284 << " to be of type '" << insn_type
5285 << "' but found type '" << *field_type << "' in Get";
5286 return;
5287 }
5288 } else {
David Brazdilca3c8c32016-09-06 14:04:48 +01005289 if (!insn_type.IsAssignableFrom(*field_type, this)) {
David Sehr709b0702016-10-13 09:12:37 -07005290 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << ArtField::PrettyField(field)
Andreas Gampe896df402014-10-20 22:25:29 -07005291 << " to be compatible with type '" << insn_type
5292 << "' but found type '" << *field_type
5293 << "' in get-object";
Andreas Gampead238ce2015-08-24 21:13:08 -07005294 work_line_->SetRegisterType<LockOp::kClear>(this, vregA, reg_types_.Conflict());
Andreas Gampe896df402014-10-20 22:25:29 -07005295 return;
5296 }
5297 }
5298 if (!field_type->IsLowHalf()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07005299 work_line_->SetRegisterType<LockOp::kClear>(this, vregA, *field_type);
Andreas Gampe896df402014-10-20 22:25:29 -07005300 } else {
5301 work_line_->SetRegisterTypeWide(this, vregA, *field_type, field_type->HighHalf(&reg_types_));
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02005302 }
5303 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07005304 LOG(FATAL) << "Unexpected case.";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02005305 }
5306}
5307
Ian Rogers776ac1f2012-04-13 23:36:36 -07005308bool MethodVerifier::CheckNotMoveException(const uint16_t* insns, int insn_idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07005309 if ((insns[insn_idx] & 0xff) == Instruction::MOVE_EXCEPTION) {
jeffhaod5347e02012-03-22 17:25:05 -07005310 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-exception";
Ian Rogersd81871c2011-10-03 13:57:23 -07005311 return false;
5312 }
5313 return true;
5314}
5315
Stephen Kyle9bc61992014-09-22 13:53:15 +01005316bool MethodVerifier::CheckNotMoveResult(const uint16_t* insns, int insn_idx) {
5317 if (((insns[insn_idx] & 0xff) >= Instruction::MOVE_RESULT) &&
5318 ((insns[insn_idx] & 0xff) <= Instruction::MOVE_RESULT_OBJECT)) {
5319 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-result*";
5320 return false;
5321 }
5322 return true;
5323}
5324
5325bool MethodVerifier::CheckNotMoveExceptionOrMoveResult(const uint16_t* insns, int insn_idx) {
5326 return (CheckNotMoveException(insns, insn_idx) && CheckNotMoveResult(insns, insn_idx));
5327}
5328
Ian Rogersebbdd872014-07-07 23:53:08 -07005329bool MethodVerifier::UpdateRegisters(uint32_t next_insn, RegisterLine* merge_line,
5330 bool update_merge_line) {
Ian Rogersd81871c2011-10-03 13:57:23 -07005331 bool changed = true;
5332 RegisterLine* target_line = reg_table_.GetLine(next_insn);
Mathieu Chartierde40d472015-10-15 17:47:48 -07005333 if (!GetInstructionFlags(next_insn).IsVisitedOrChanged()) {
jeffhaobdb76512011-09-07 11:43:16 -07005334 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07005335 * We haven't processed this instruction before, and we haven't touched the registers here, so
5336 * there's nothing to "merge". Copy the registers over and mark it as changed. (This is the
5337 * only way a register can transition out of "unknown", so this is not just an optimization.)
jeffhaobdb76512011-09-07 11:43:16 -07005338 */
Andreas Gampea727e372015-08-25 09:22:37 -07005339 target_line->CopyFromLine(merge_line);
Mathieu Chartierde40d472015-10-15 17:47:48 -07005340 if (GetInstructionFlags(next_insn).IsReturn()) {
Jeff Haob24b4a72013-07-31 13:47:31 -07005341 // Verify that the monitor stack is empty on return.
Andreas Gampea727e372015-08-25 09:22:37 -07005342 merge_line->VerifyMonitorStackEmpty(this);
5343
Ian Rogersb8c78592013-07-25 23:52:52 +00005344 // For returns we only care about the operand to the return, all other registers are dead.
5345 // Initialize them as conflicts so they don't add to GC and deoptimization information.
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08005346 const Instruction* ret_inst = &code_item_accessor_.InstructionAt(next_insn);
Andreas Gampea727e372015-08-25 09:22:37 -07005347 AdjustReturnLine(this, ret_inst, target_line);
Aart Bik31883642016-06-06 15:02:44 -07005348 // Directly bail if a hard failure was found.
Aart Bikb0526322016-06-01 14:06:00 -07005349 if (have_pending_hard_failure_) {
5350 return false;
5351 }
Ian Rogersb8c78592013-07-25 23:52:52 +00005352 }
jeffhaobdb76512011-09-07 11:43:16 -07005353 } else {
Mathieu Chartier361e04a2016-02-16 14:06:35 -08005354 RegisterLineArenaUniquePtr copy;
Andreas Gampe92d77202017-12-06 20:49:00 -08005355 if (UNLIKELY(VLOG_IS_ON(verifier_debug))) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07005356 copy.reset(RegisterLine::Create(target_line->NumRegs(), this));
Ian Rogers7b0c5b42012-02-16 15:29:07 -08005357 copy->CopyFromLine(target_line);
5358 }
Ian Rogers7b078e82014-09-10 14:44:24 -07005359 changed = target_line->MergeRegisters(this, merge_line);
Ian Rogersad0b3a32012-04-16 14:50:24 -07005360 if (have_pending_hard_failure_) {
Ian Rogersd81871c2011-10-03 13:57:23 -07005361 return false;
jeffhaobdb76512011-09-07 11:43:16 -07005362 }
Andreas Gampe92d77202017-12-06 20:49:00 -08005363 if (UNLIKELY(VLOG_IS_ON(verifier_debug)) && changed) {
Elliott Hughes398f64b2012-03-26 18:05:48 -07005364 LogVerifyInfo() << "Merging at [" << reinterpret_cast<void*>(work_insn_idx_) << "]"
Elliott Hughesc073b072012-05-24 19:29:17 -07005365 << " to [" << reinterpret_cast<void*>(next_insn) << "]: " << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07005366 << copy->Dump(this) << " MERGE\n"
5367 << merge_line->Dump(this) << " ==\n"
5368 << target_line->Dump(this) << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07005369 }
Ian Rogersebbdd872014-07-07 23:53:08 -07005370 if (update_merge_line && changed) {
5371 merge_line->CopyFromLine(target_line);
5372 }
jeffhaobdb76512011-09-07 11:43:16 -07005373 }
Ian Rogersd81871c2011-10-03 13:57:23 -07005374 if (changed) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07005375 GetInstructionFlags(next_insn).SetChanged();
jeffhaobdb76512011-09-07 11:43:16 -07005376 }
5377 return true;
5378}
5379
Ian Rogers7b3ddd22013-02-21 15:19:52 -08005380InstructionFlags* MethodVerifier::CurrentInsnFlags() {
Mathieu Chartierde40d472015-10-15 17:47:48 -07005381 return &GetInstructionFlags(work_insn_idx_);
Ian Rogers776ac1f2012-04-13 23:36:36 -07005382}
5383
Ian Rogersd8f69b02014-09-10 21:43:52 +00005384const RegType& MethodVerifier::GetMethodReturnType() {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07005385 if (return_type_ == nullptr) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07005386 if (mirror_method_ != nullptr) {
Vladimir Markob45528c2017-07-27 14:14:28 +01005387 ObjPtr<mirror::Class> return_type_class = can_load_classes_
5388 ? mirror_method_->ResolveReturnType()
5389 : mirror_method_->LookupResolvedReturnType();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07005390 if (return_type_class != nullptr) {
Andreas Gampef23f33d2015-06-23 14:18:17 -07005391 return_type_ = &FromClass(mirror_method_->GetReturnTypeDescriptor(),
Vladimir Markob45528c2017-07-27 14:14:28 +01005392 return_type_class.Ptr(),
Andreas Gampef23f33d2015-06-23 14:18:17 -07005393 return_type_class->CannotBeAssignedFromOtherTypes());
Ian Rogersfc0e94b2013-09-23 23:51:32 -07005394 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07005395 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
5396 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07005397 }
5398 }
5399 if (return_type_ == nullptr) {
5400 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
5401 const DexFile::ProtoId& proto_id = dex_file_->GetMethodPrototype(method_id);
Andreas Gampea5b09a62016-11-17 15:21:22 -08005402 dex::TypeIndex return_type_idx = proto_id.return_type_idx_;
Ian Rogersfc0e94b2013-09-23 23:51:32 -07005403 const char* descriptor = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(return_type_idx));
Mathieu Chartierbf99f772014-08-23 16:37:27 -07005404 return_type_ = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07005405 }
5406 }
5407 return *return_type_;
Ian Rogersad0b3a32012-04-16 14:50:24 -07005408}
5409
Ian Rogersd8f69b02014-09-10 21:43:52 +00005410const RegType& MethodVerifier::GetDeclaringClass() {
Ian Rogers7b078e82014-09-10 14:44:24 -07005411 if (declaring_class_ == nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08005412 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
Brian Carlstrom93c33962013-07-26 10:37:43 -07005413 const char* descriptor
5414 = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(method_id.class_idx_));
Mathieu Chartiere401d142015-04-22 13:56:20 -07005415 if (mirror_method_ != nullptr) {
Ian Rogers637c65b2013-05-31 11:46:00 -07005416 mirror::Class* klass = mirror_method_->GetDeclaringClass();
Mathieu Chartierde40d472015-10-15 17:47:48 -07005417 declaring_class_ = &FromClass(descriptor, klass, klass->CannotBeAssignedFromOtherTypes());
Ian Rogers637c65b2013-05-31 11:46:00 -07005418 } else {
Mathieu Chartierbf99f772014-08-23 16:37:27 -07005419 declaring_class_ = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers637c65b2013-05-31 11:46:00 -07005420 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07005421 }
Ian Rogers637c65b2013-05-31 11:46:00 -07005422 return *declaring_class_;
Ian Rogersad0b3a32012-04-16 14:50:24 -07005423}
5424
Ian Rogers2bcb4a42012-11-08 10:39:18 -08005425std::vector<int32_t> MethodVerifier::DescribeVRegs(uint32_t dex_pc) {
5426 RegisterLine* line = reg_table_.GetLine(dex_pc);
Sebastien Hertzaa0c00c2014-03-14 17:58:54 +01005427 DCHECK(line != nullptr) << "No register line at DEX pc " << StringPrintf("0x%x", dex_pc);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08005428 std::vector<int32_t> result;
5429 for (size_t i = 0; i < line->NumRegs(); ++i) {
Ian Rogers7b078e82014-09-10 14:44:24 -07005430 const RegType& type = line->GetRegisterType(this, i);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08005431 if (type.IsConstant()) {
5432 result.push_back(type.IsPreciseConstant() ? kConstant : kImpreciseConstant);
Ian Rogers7b078e82014-09-10 14:44:24 -07005433 const ConstantType* const_val = down_cast<const ConstantType*>(&type);
5434 result.push_back(const_val->ConstantValue());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08005435 } else if (type.IsConstantLo()) {
5436 result.push_back(type.IsPreciseConstantLo() ? kConstant : kImpreciseConstant);
Ian Rogers7b078e82014-09-10 14:44:24 -07005437 const ConstantType* const_val = down_cast<const ConstantType*>(&type);
5438 result.push_back(const_val->ConstantValueLo());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08005439 } else if (type.IsConstantHi()) {
5440 result.push_back(type.IsPreciseConstantHi() ? kConstant : kImpreciseConstant);
Ian Rogers7b078e82014-09-10 14:44:24 -07005441 const ConstantType* const_val = down_cast<const ConstantType*>(&type);
5442 result.push_back(const_val->ConstantValueHi());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08005443 } else if (type.IsIntegralTypes()) {
5444 result.push_back(kIntVReg);
5445 result.push_back(0);
5446 } else if (type.IsFloat()) {
5447 result.push_back(kFloatVReg);
5448 result.push_back(0);
5449 } else if (type.IsLong()) {
5450 result.push_back(kLongLoVReg);
5451 result.push_back(0);
5452 result.push_back(kLongHiVReg);
5453 result.push_back(0);
5454 ++i;
5455 } else if (type.IsDouble()) {
5456 result.push_back(kDoubleLoVReg);
5457 result.push_back(0);
5458 result.push_back(kDoubleHiVReg);
5459 result.push_back(0);
5460 ++i;
5461 } else if (type.IsUndefined() || type.IsConflict() || type.IsHighHalf()) {
5462 result.push_back(kUndefined);
5463 result.push_back(0);
5464 } else {
Ian Rogers7b3ddd22013-02-21 15:19:52 -08005465 CHECK(type.IsNonZeroReferenceTypes());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08005466 result.push_back(kReferenceVReg);
5467 result.push_back(0);
5468 }
5469 }
5470 return result;
5471}
5472
Ian Rogersd8f69b02014-09-10 21:43:52 +00005473const RegType& MethodVerifier::DetermineCat1Constant(int32_t value, bool precise) {
Sebastien Hertz849600b2013-12-20 10:28:08 +01005474 if (precise) {
5475 // Precise constant type.
5476 return reg_types_.FromCat1Const(value, true);
5477 } else {
5478 // Imprecise constant type.
5479 if (value < -32768) {
5480 return reg_types_.IntConstant();
5481 } else if (value < -128) {
5482 return reg_types_.ShortConstant();
5483 } else if (value < 0) {
5484 return reg_types_.ByteConstant();
5485 } else if (value == 0) {
5486 return reg_types_.Zero();
5487 } else if (value == 1) {
5488 return reg_types_.One();
5489 } else if (value < 128) {
5490 return reg_types_.PosByteConstant();
5491 } else if (value < 32768) {
5492 return reg_types_.PosShortConstant();
5493 } else if (value < 65536) {
5494 return reg_types_.CharConstant();
5495 } else {
5496 return reg_types_.IntConstant();
5497 }
5498 }
5499}
5500
Elliott Hughes0a1038b2012-06-14 16:24:17 -07005501void MethodVerifier::Init() {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08005502 art::verifier::RegTypeCache::Init();
Brian Carlstrome7d856b2012-01-11 18:10:55 -08005503}
5504
Elliott Hughes0a1038b2012-06-14 16:24:17 -07005505void MethodVerifier::Shutdown() {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08005506 verifier::RegTypeCache::ShutDown();
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08005507}
jeffhaod1224c72012-02-29 13:43:08 -08005508
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -07005509void MethodVerifier::VisitStaticRoots(RootVisitor* visitor) {
5510 RegTypeCache::VisitStaticRoots(visitor);
Mathieu Chartier7c438b12014-09-12 17:01:24 -07005511}
5512
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -07005513void MethodVerifier::VisitRoots(RootVisitor* visitor, const RootInfo& root_info) {
5514 reg_types_.VisitRoots(visitor, root_info);
Mathieu Chartierc528dba2013-11-26 12:00:11 -08005515}
5516
Andreas Gampef23f33d2015-06-23 14:18:17 -07005517const RegType& MethodVerifier::FromClass(const char* descriptor,
5518 mirror::Class* klass,
5519 bool precise) {
5520 DCHECK(klass != nullptr);
5521 if (precise && !klass->IsInstantiable() && !klass->IsPrimitive()) {
5522 Fail(VerifyError::VERIFY_ERROR_NO_CLASS) << "Could not create precise reference for "
5523 << "non-instantiable klass " << descriptor;
5524 precise = false;
5525 }
5526 return reg_types_.FromClass(descriptor, klass, precise);
5527}
5528
Ian Rogersd81871c2011-10-03 13:57:23 -07005529} // namespace verifier
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07005530} // namespace art