blob: 8d9b82e420051c5ce360d4d5cc9d325a7b2c9712 [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"
Mathieu Chartierdc578c72017-12-27 11:51:45 -080035#include "dex_file_exception_helpers.h"
Ian Rogersd0583802013-06-01 10:51:46 -070036#include "dex_instruction-inl.h"
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080037#include "dex_instruction_utils.h"
Alex Lighteb7c1442015-08-31 13:17:42 -070038#include "experimental_flags.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070039#include "gc/accounting/card_table-inl.h"
Andreas Gampe6d7abbd2017-04-24 13:19:09 -070040#include "handle_scope-inl.h"
Ian Rogers2bcb4a42012-11-08 10:39:18 -080041#include "indenter.h"
Ian Rogers84fa0742011-10-25 18:13:30 -070042#include "intern_table.h"
Ian Rogers0571d352011-11-03 19:51:38 -070043#include "leb128.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080044#include "mirror/class-inl.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070045#include "mirror/class.h"
Ian Rogers39ebcb82013-05-30 16:57:23 -070046#include "mirror/dex_cache-inl.h"
Orion Hodsoncfa325e2016-10-13 10:25:54 +010047#include "mirror/method_handle_impl.h"
Orion Hodson2e599942017-09-22 16:17:41 +010048#include "mirror/method_type.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080049#include "mirror/object-inl.h"
50#include "mirror/object_array-inl.h"
Orion Hodsonfe92d122018-01-02 10:45:17 +000051#include "mirror/var_handle.h"
Ian Rogers7b078e82014-09-10 14:44:24 -070052#include "reg_type-inl.h"
Ian Rogers39ebcb82013-05-30 16:57:23 -070053#include "register_line-inl.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070054#include "runtime.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070055#include "scoped_thread_state_change-inl.h"
Andreas Gampe513061a2017-06-01 09:17:34 -070056#include "stack.h"
Vladimir Marko80afd022015-05-19 18:08:00 +010057#include "utils.h"
Andreas Gampe6d7abbd2017-04-24 13:19:09 -070058#include "verifier_compiler_binding.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070059#include "verifier_deps.h"
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070060
61namespace art {
Ian Rogersd81871c2011-10-03 13:57:23 -070062namespace verifier {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070063
Andreas Gampe46ee31b2016-12-14 10:11:49 -080064using android::base::StringPrintf;
65
Mathieu Chartier8e219ae2014-08-19 14:29:46 -070066static constexpr bool kTimeVerifyMethod = !kIsDebugBuild;
Ian Rogers2c8a8572011-10-24 17:11:36 -070067
Andreas Gampeebf850c2015-08-14 15:37:35 -070068// On VLOG(verifier), should we dump the whole state when we run into a hard failure?
69static constexpr bool kDumpRegLinesOnHardFailureIfVLOG = true;
70
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -080071// We print a warning blurb about "dx --no-optimize" when we find monitor-locking issues. Make
72// sure we only print this once.
73static bool gPrintedDxMonitorText = false;
74
Vladimir Marko69d310e2017-10-09 14:12:23 +010075PcToRegisterLineTable::PcToRegisterLineTable(ScopedArenaAllocator& allocator)
76 : register_lines_(allocator.Adapter(kArenaAllocVerifier)) {}
Mathieu Chartierde40d472015-10-15 17:47:48 -070077
Ian Rogers7b3ddd22013-02-21 15:19:52 -080078void PcToRegisterLineTable::Init(RegisterTrackingMode mode, InstructionFlags* flags,
Ian Rogersd81871c2011-10-03 13:57:23 -070079 uint32_t insns_size, uint16_t registers_size,
Ian Rogers776ac1f2012-04-13 23:36:36 -070080 MethodVerifier* verifier) {
Ian Rogersd81871c2011-10-03 13:57:23 -070081 DCHECK_GT(insns_size, 0U);
Mathieu Chartierde40d472015-10-15 17:47:48 -070082 register_lines_.resize(insns_size);
Ian Rogersd81871c2011-10-03 13:57:23 -070083 for (uint32_t i = 0; i < insns_size; i++) {
84 bool interesting = false;
85 switch (mode) {
86 case kTrackRegsAll:
87 interesting = flags[i].IsOpcode();
88 break;
Sameer Abu Asal02c42232013-04-30 12:09:45 -070089 case kTrackCompilerInterestPoints:
Brian Carlstrom02c8cc62013-07-18 15:54:44 -070090 interesting = flags[i].IsCompileTimeInfoPoint() || flags[i].IsBranchTarget();
Ian Rogersd81871c2011-10-03 13:57:23 -070091 break;
92 case kTrackRegsBranches:
93 interesting = flags[i].IsBranchTarget();
94 break;
95 default:
96 break;
97 }
98 if (interesting) {
Mathieu Chartierde40d472015-10-15 17:47:48 -070099 register_lines_[i].reset(RegisterLine::Create(registers_size, verifier));
Ian Rogersd0fbd852013-09-24 18:17:04 -0700100 }
101 }
102}
103
Mathieu Chartierde40d472015-10-15 17:47:48 -0700104PcToRegisterLineTable::~PcToRegisterLineTable() {}
Ian Rogersd81871c2011-10-03 13:57:23 -0700105
Andreas Gampe7c038102014-10-27 20:08:46 -0700106// Note: returns true on failure.
107ALWAYS_INLINE static inline bool FailOrAbort(MethodVerifier* verifier, bool condition,
108 const char* error_msg, uint32_t work_insn_idx) {
109 if (kIsDebugBuild) {
Andreas Gampef8f36c22016-11-19 14:51:52 -0800110 // In a debug build, abort if the error condition is wrong. Only warn if
111 // we are already aborting (as this verification is likely run to print
112 // lock information).
113 if (LIKELY(gAborting == 0)) {
114 DCHECK(condition) << error_msg << work_insn_idx;
115 } else {
116 if (!condition) {
117 LOG(ERROR) << error_msg << work_insn_idx;
118 verifier->Fail(VERIFY_ERROR_BAD_CLASS_HARD) << error_msg << work_insn_idx;
119 return true;
120 }
121 }
Andreas Gampe7c038102014-10-27 20:08:46 -0700122 } else {
123 // In a non-debug build, just fail the class.
124 if (!condition) {
125 verifier->Fail(VERIFY_ERROR_BAD_CLASS_HARD) << error_msg << work_insn_idx;
126 return true;
127 }
128 }
129
130 return false;
131}
132
Stephen Kyle7e541c92014-12-17 17:10:02 +0000133static void SafelyMarkAllRegistersAsConflicts(MethodVerifier* verifier, RegisterLine* reg_line) {
Andreas Gampef10b6e12015-08-12 10:48:12 -0700134 if (verifier->IsInstanceConstructor()) {
Stephen Kyle7e541c92014-12-17 17:10:02 +0000135 // Before we mark all regs as conflicts, check that we don't have an uninitialized this.
136 reg_line->CheckConstructorReturn(verifier);
137 }
138 reg_line->MarkAllRegistersAsConflicts(verifier);
139}
140
Andreas Gampe6d7abbd2017-04-24 13:19:09 -0700141FailureKind MethodVerifier::VerifyClass(Thread* self,
142 mirror::Class* klass,
143 CompilerCallbacks* callbacks,
144 bool allow_soft_failures,
145 HardFailLogMode log_level,
146 std::string* error) {
jeffhaobdb76512011-09-07 11:43:16 -0700147 if (klass->IsVerified()) {
Andreas Gampe6d7abbd2017-04-24 13:19:09 -0700148 return FailureKind::kNoFailure;
jeffhaobdb76512011-09-07 11:43:16 -0700149 }
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800150 bool early_failure = false;
151 std::string failure_message;
Mathieu Chartierf8322842014-05-16 10:59:25 -0700152 const DexFile& dex_file = klass->GetDexFile();
153 const DexFile::ClassDef* class_def = klass->GetClassDef();
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800154 mirror::Class* super = klass->GetSuperClass();
Ian Rogers1ff3c982014-08-12 02:30:58 -0700155 std::string temp;
Ian Rogers7b078e82014-09-10 14:44:24 -0700156 if (super == nullptr && strcmp("Ljava/lang/Object;", klass->GetDescriptor(&temp)) != 0) {
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800157 early_failure = true;
158 failure_message = " that has no super class";
Ian Rogers7b078e82014-09-10 14:44:24 -0700159 } else if (super != nullptr && super->IsFinal()) {
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800160 early_failure = true;
David Sehr709b0702016-10-13 09:12:37 -0700161 failure_message = " that attempts to sub-class final class " + super->PrettyDescriptor();
Ian Rogers7b078e82014-09-10 14:44:24 -0700162 } else if (class_def == nullptr) {
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800163 early_failure = true;
164 failure_message = " that isn't present in dex file " + dex_file.GetLocation();
165 }
166 if (early_failure) {
David Sehr709b0702016-10-13 09:12:37 -0700167 *error = "Verifier rejected class " + klass->PrettyDescriptor() + failure_message;
Andreas Gampe53e32d12015-12-09 21:03:23 -0800168 if (callbacks != nullptr) {
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800169 ClassReference ref(&dex_file, klass->GetDexClassDefIndex());
Andreas Gampe53e32d12015-12-09 21:03:23 -0800170 callbacks->ClassRejected(ref);
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800171 }
Andreas Gampe6d7abbd2017-04-24 13:19:09 -0700172 return FailureKind::kHardFailure;
jeffhaobdb76512011-09-07 11:43:16 -0700173 }
Ian Rogers7b078e82014-09-10 14:44:24 -0700174 StackHandleScope<2> hs(self);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700175 Handle<mirror::DexCache> dex_cache(hs.NewHandle(klass->GetDexCache()));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700176 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(klass->GetClassLoader()));
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800177 return VerifyClass(self,
178 &dex_file,
179 dex_cache,
180 class_loader,
David Brazdil15fc7292016-09-02 14:13:18 +0100181 *class_def,
Andreas Gampe53e32d12015-12-09 21:03:23 -0800182 callbacks,
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800183 allow_soft_failures,
Andreas Gampe7fe30232016-03-25 16:58:00 -0700184 log_level,
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800185 error);
186}
187
188template <bool kDirect>
189static bool HasNextMethod(ClassDataItemIterator* it) {
190 return kDirect ? it->HasNextDirectMethod() : it->HasNextVirtualMethod();
191}
192
Andreas Gampe6d7abbd2017-04-24 13:19:09 -0700193static FailureKind FailureKindMax(FailureKind fk1, FailureKind fk2) {
194 static_assert(FailureKind::kNoFailure < FailureKind::kSoftFailure
195 && FailureKind::kSoftFailure < FailureKind::kHardFailure,
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800196 "Unexpected FailureKind order");
197 return std::max(fk1, fk2);
198}
199
200void MethodVerifier::FailureData::Merge(const MethodVerifier::FailureData& fd) {
201 kind = FailureKindMax(kind, fd.kind);
202 types |= fd.types;
203}
204
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800205template <bool kDirect>
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800206MethodVerifier::FailureData MethodVerifier::VerifyMethods(Thread* self,
207 ClassLinker* linker,
208 const DexFile* dex_file,
David Brazdil15fc7292016-09-02 14:13:18 +0100209 const DexFile::ClassDef& class_def,
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800210 ClassDataItemIterator* it,
211 Handle<mirror::DexCache> dex_cache,
212 Handle<mirror::ClassLoader> class_loader,
213 CompilerCallbacks* callbacks,
214 bool allow_soft_failures,
Andreas Gampe5fd66d02016-09-12 20:22:19 -0700215 HardFailLogMode log_level,
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800216 bool need_precise_constants,
217 std::string* error_string) {
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800218 DCHECK(it != nullptr);
219
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800220 MethodVerifier::FailureData failure_data;
221
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800222 int64_t previous_method_idx = -1;
223 while (HasNextMethod<kDirect>(it)) {
224 self->AllowThreadSuspension();
225 uint32_t method_idx = it->GetMemberIndex();
226 if (method_idx == previous_method_idx) {
227 // smali can create dex files with two encoded_methods sharing the same method_idx
228 // http://code.google.com/p/smali/issues/detail?id=119
229 it->Next();
230 continue;
231 }
232 previous_method_idx = method_idx;
David Brazdil15fc7292016-09-02 14:13:18 +0100233 InvokeType type = it->GetMethodInvokeType(class_def);
Vladimir Markoba118822017-06-12 15:41:56 +0100234 ArtMethod* method = linker->ResolveMethod<ClassLinker::ResolveMode::kNoChecks>(
Vladimir Marko89011192017-12-11 13:45:05 +0000235 method_idx, dex_cache, class_loader, /* referrer */ nullptr, type);
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800236 if (method == nullptr) {
237 DCHECK(self->IsExceptionPending());
238 // We couldn't resolve the method, but continue regardless.
239 self->ClearException();
240 } else {
241 DCHECK(method->GetDeclaringClassUnchecked() != nullptr) << type;
242 }
243 StackHandleScope<1> hs(self);
244 std::string hard_failure_msg;
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800245 MethodVerifier::FailureData result = VerifyMethod(self,
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800246 method_idx,
247 dex_file,
248 dex_cache,
249 class_loader,
250 class_def,
251 it->GetMethodCodeItem(),
252 method,
253 it->GetMethodAccessFlags(),
Andreas Gampe53e32d12015-12-09 21:03:23 -0800254 callbacks,
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800255 allow_soft_failures,
Andreas Gampe7fe30232016-03-25 16:58:00 -0700256 log_level,
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800257 need_precise_constants,
258 &hard_failure_msg);
Andreas Gampe6d7abbd2017-04-24 13:19:09 -0700259 if (result.kind == FailureKind::kHardFailure) {
260 if (failure_data.kind == FailureKind::kHardFailure) {
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800261 // If we logged an error before, we need a newline.
262 *error_string += "\n";
263 } else {
264 // If we didn't log a hard failure before, print the header of the message.
265 *error_string += "Verifier rejected class ";
David Brazdil15fc7292016-09-02 14:13:18 +0100266 *error_string += PrettyDescriptor(dex_file->GetClassDescriptor(class_def));
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800267 *error_string += ":";
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800268 }
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800269 *error_string += " ";
270 *error_string += hard_failure_msg;
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800271 }
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800272 failure_data.Merge(result);
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800273 it->Next();
274 }
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800275
276 return failure_data;
Shih-wei Liao371814f2011-10-27 16:52:10 -0700277}
278
Andreas Gampe6d7abbd2017-04-24 13:19:09 -0700279FailureKind MethodVerifier::VerifyClass(Thread* self,
280 const DexFile* dex_file,
281 Handle<mirror::DexCache> dex_cache,
282 Handle<mirror::ClassLoader> class_loader,
283 const DexFile::ClassDef& class_def,
284 CompilerCallbacks* callbacks,
285 bool allow_soft_failures,
286 HardFailLogMode log_level,
287 std::string* error) {
Andreas Gampec6548162017-12-08 12:15:22 -0800288 SCOPED_TRACE << "VerifyClass " << PrettyDescriptor(dex_file->GetClassDescriptor(class_def));
Andreas Gampe507cc6f2015-06-19 22:58:47 -0700289
290 // A class must not be abstract and final.
David Brazdil15fc7292016-09-02 14:13:18 +0100291 if ((class_def.access_flags_ & (kAccAbstract | kAccFinal)) == (kAccAbstract | kAccFinal)) {
Andreas Gampe507cc6f2015-06-19 22:58:47 -0700292 *error = "Verifier rejected class ";
David Brazdil15fc7292016-09-02 14:13:18 +0100293 *error += PrettyDescriptor(dex_file->GetClassDescriptor(class_def));
Andreas Gampe507cc6f2015-06-19 22:58:47 -0700294 *error += ": class is abstract and final.";
Andreas Gampe6d7abbd2017-04-24 13:19:09 -0700295 return FailureKind::kHardFailure;
Andreas Gampe507cc6f2015-06-19 22:58:47 -0700296 }
297
David Brazdil15fc7292016-09-02 14:13:18 +0100298 const uint8_t* class_data = dex_file->GetClassData(class_def);
Ian Rogers7b078e82014-09-10 14:44:24 -0700299 if (class_data == nullptr) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700300 // empty class, probably a marker interface
Andreas Gampe6d7abbd2017-04-24 13:19:09 -0700301 return FailureKind::kNoFailure;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700302 }
jeffhaof56197c2012-03-05 18:01:54 -0800303 ClassDataItemIterator it(*dex_file, class_data);
Mathieu Chartiere17cf242017-06-19 11:05:51 -0700304 it.SkipAllFields();
Ian Rogersad0b3a32012-04-16 14:50:24 -0700305 ClassLinker* linker = Runtime::Current()->GetClassLinker();
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800306 // Direct methods.
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800307 MethodVerifier::FailureData data1 = VerifyMethods<true>(self,
308 linker,
309 dex_file,
310 class_def,
311 &it,
312 dex_cache,
313 class_loader,
314 callbacks,
315 allow_soft_failures,
Andreas Gampe7fe30232016-03-25 16:58:00 -0700316 log_level,
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800317 false /* need precise constants */,
318 error);
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800319 // Virtual methods.
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800320 MethodVerifier::FailureData data2 = VerifyMethods<false>(self,
321 linker,
322 dex_file,
323 class_def,
324 &it,
325 dex_cache,
326 class_loader,
327 callbacks,
328 allow_soft_failures,
Andreas Gampe7fe30232016-03-25 16:58:00 -0700329 log_level,
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800330 false /* need precise constants */,
331 error);
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800332
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800333 data1.Merge(data2);
334
Andreas Gampe6d7abbd2017-04-24 13:19:09 -0700335 if (data1.kind == FailureKind::kNoFailure) {
336 return FailureKind::kNoFailure;
jeffhaof1e6b7c2012-06-05 18:33:30 -0700337 } else {
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800338 if ((data1.types & VERIFY_ERROR_LOCKING) != 0) {
339 // Print a warning about expected slow-down. Use a string temporary to print one contiguous
340 // warning.
341 std::string tmp =
342 StringPrintf("Class %s failed lock verification and will run slower.",
David Brazdil15fc7292016-09-02 14:13:18 +0100343 PrettyDescriptor(dex_file->GetClassDescriptor(class_def)).c_str());
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800344 if (!gPrintedDxMonitorText) {
345 tmp = tmp + "\nCommon causes for lock verification issues are non-optimized dex code\n"
346 "and incorrect proguard optimizations.";
347 gPrintedDxMonitorText = true;
348 }
349 LOG(WARNING) << tmp;
350 }
351 return data1.kind;
jeffhaof1e6b7c2012-06-05 18:33:30 -0700352 }
jeffhaof56197c2012-03-05 18:01:54 -0800353}
354
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800355static bool IsLargeMethod(const CodeItemDataAccessor& accessor) {
356 if (!accessor.HasCodeItem()) {
Andreas Gampe3c651fc2015-05-21 14:06:46 -0700357 return false;
358 }
359
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800360 uint16_t registers_size = accessor.RegistersSize();
361 uint32_t insns_size = accessor.InsnsSizeInCodeUnits();
Andreas Gampea4f5bf62015-05-18 20:50:12 -0700362
363 return registers_size * insns_size > 4*1024*1024;
364}
365
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800366MethodVerifier::FailureData MethodVerifier::VerifyMethod(Thread* self,
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800367 uint32_t method_idx,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800368 const DexFile* dex_file,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700369 Handle<mirror::DexCache> dex_cache,
370 Handle<mirror::ClassLoader> class_loader,
David Brazdil15fc7292016-09-02 14:13:18 +0100371 const DexFile::ClassDef& class_def,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800372 const DexFile::CodeItem* code_item,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700373 ArtMethod* method,
Jeff Haoee988952013-04-16 14:23:47 -0700374 uint32_t method_access_flags,
Andreas Gampe53e32d12015-12-09 21:03:23 -0800375 CompilerCallbacks* callbacks,
Ian Rogers46960fe2014-05-23 10:43:43 -0700376 bool allow_soft_failures,
Andreas Gampe5fd66d02016-09-12 20:22:19 -0700377 HardFailLogMode log_level,
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800378 bool need_precise_constants,
379 std::string* hard_failure_msg) {
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800380 MethodVerifier::FailureData result;
Mathieu Chartier8e219ae2014-08-19 14:29:46 -0700381 uint64_t start_ns = kTimeVerifyMethod ? NanoTime() : 0;
Ian Rogersc8982582012-09-07 16:53:25 -0700382
Andreas Gampebf9611f2016-03-25 16:58:00 -0700383 MethodVerifier verifier(self,
384 dex_file,
385 dex_cache,
386 class_loader,
387 class_def,
388 code_item,
389 method_idx,
390 method,
391 method_access_flags,
392 true /* can_load_classes */,
393 allow_soft_failures,
394 need_precise_constants,
395 false /* verify to dump */,
396 true /* allow_thread_suspension */);
Ian Rogers46960fe2014-05-23 10:43:43 -0700397 if (verifier.Verify()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700398 // Verification completed, however failures may be pending that didn't cause the verification
399 // to hard fail.
Ian Rogers46960fe2014-05-23 10:43:43 -0700400 CHECK(!verifier.have_pending_hard_failure_);
Andreas Gampe53e32d12015-12-09 21:03:23 -0800401
402 if (code_item != nullptr && callbacks != nullptr) {
403 // Let the interested party know that the method was verified.
404 callbacks->MethodVerified(&verifier);
405 }
406
Ian Rogers46960fe2014-05-23 10:43:43 -0700407 if (verifier.failures_.size() != 0) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -0700408 if (VLOG_IS_ON(verifier)) {
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800409 verifier.DumpFailures(VLOG_STREAM(verifier) << "Soft verification failures in "
David Sehr709b0702016-10-13 09:12:37 -0700410 << dex_file->PrettyMethod(method_idx) << "\n");
Anwar Ghuloum75a43f12013-08-13 17:22:14 -0700411 }
Andreas Gampe92d77202017-12-06 20:49:00 -0800412 if (VLOG_IS_ON(verifier_debug)) {
413 std::cout << "\n" << verifier.info_messages_.str();
414 verifier.Dump(std::cout);
415 }
Andreas Gampe6d7abbd2017-04-24 13:19:09 -0700416 result.kind = FailureKind::kSoftFailure;
Nicolas Geoffray250a3782016-04-20 16:27:53 +0100417 if (method != nullptr &&
418 !CanCompilerHandleVerificationFailure(verifier.encountered_failure_types_)) {
Alex Lightfcbafb32017-02-02 15:09:54 -0800419 method->SetDontCompile();
Nicolas Geoffray250a3782016-04-20 16:27:53 +0100420 }
421 }
Andreas Gampe56fdd0e2016-04-28 14:56:54 -0700422 if (method != nullptr) {
423 if (verifier.HasInstructionThatWillThrow()) {
Alex Lightfcbafb32017-02-02 15:09:54 -0800424 method->SetDontCompile();
Calin Juravle857f0582016-12-20 14:36:59 +0000425 if (Runtime::Current()->IsAotCompiler() &&
426 (callbacks != nullptr) && !callbacks->IsBootImage()) {
427 // When compiling apps, make HasInstructionThatWillThrow a soft error to trigger
428 // re-verification at runtime.
429 // The dead code after the throw is not verified and might be invalid. This may cause
430 // the JIT compiler to crash since it assumes that all the code is valid.
431 //
432 // There's a strong assumption that the entire boot image is verified and all its dex
433 // code is valid (even the dead and unverified one). As such this is done only for apps.
434 // (CompilerDriver DCHECKs in VerifyClassVisitor that methods from boot image are
435 // fully verified).
Andreas Gampe6d7abbd2017-04-24 13:19:09 -0700436 result.kind = FailureKind::kSoftFailure;
Calin Juravle857f0582016-12-20 14:36:59 +0000437 }
Andreas Gampe56fdd0e2016-04-28 14:56:54 -0700438 }
439 if ((verifier.encountered_failure_types_ & VerifyError::VERIFY_ERROR_LOCKING) != 0) {
Orion Hodsoncfcc9cf2017-09-29 15:07:27 +0100440 method->SetMustCountLocks();
Andreas Gampe56fdd0e2016-04-28 14:56:54 -0700441 }
jeffhaof56197c2012-03-05 18:01:54 -0800442 }
443 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700444 // Bad method data.
Ian Rogers46960fe2014-05-23 10:43:43 -0700445 CHECK_NE(verifier.failures_.size(), 0U);
Igor Murashkin4d7b75f2015-07-21 17:03:36 -0700446
447 if (UNLIKELY(verifier.have_pending_experimental_failure_)) {
448 // Failed due to being forced into interpreter. This is ok because
449 // we just want to skip verification.
Andreas Gampe6d7abbd2017-04-24 13:19:09 -0700450 result.kind = FailureKind::kSoftFailure;
Igor Murashkin4d7b75f2015-07-21 17:03:36 -0700451 } else {
452 CHECK(verifier.have_pending_hard_failure_);
Andreas Gampe7fe30232016-03-25 16:58:00 -0700453 if (VLOG_IS_ON(verifier)) {
Andreas Gampe5fd66d02016-09-12 20:22:19 -0700454 log_level = std::max(HardFailLogMode::kLogVerbose, log_level);
Andreas Gampe7fe30232016-03-25 16:58:00 -0700455 }
Andreas Gampe5fd66d02016-09-12 20:22:19 -0700456 if (log_level >= HardFailLogMode::kLogVerbose) {
457 LogSeverity severity;
458 switch (log_level) {
459 case HardFailLogMode::kLogVerbose:
460 severity = LogSeverity::VERBOSE;
461 break;
462 case HardFailLogMode::kLogWarning:
463 severity = LogSeverity::WARNING;
464 break;
465 case HardFailLogMode::kLogInternalFatal:
Andreas Gampe3fec9ac2016-09-13 10:47:28 -0700466 severity = LogSeverity::FATAL_WITHOUT_ABORT;
Andreas Gampe5fd66d02016-09-12 20:22:19 -0700467 break;
468 default:
469 LOG(FATAL) << "Unsupported log-level " << static_cast<uint32_t>(log_level);
470 UNREACHABLE();
471 }
Andreas Gampe3fec9ac2016-09-13 10:47:28 -0700472 verifier.DumpFailures(LOG_STREAM(severity) << "Verification error in "
David Sehr709b0702016-10-13 09:12:37 -0700473 << dex_file->PrettyMethod(method_idx)
Andreas Gampe3fec9ac2016-09-13 10:47:28 -0700474 << "\n");
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800475 }
476 if (hard_failure_msg != nullptr) {
477 CHECK(!verifier.failure_messages_.empty());
478 *hard_failure_msg =
479 verifier.failure_messages_[verifier.failure_messages_.size() - 1]->str();
480 }
Andreas Gampe6d7abbd2017-04-24 13:19:09 -0700481 result.kind = FailureKind::kHardFailure;
Andreas Gampe53e32d12015-12-09 21:03:23 -0800482
483 if (callbacks != nullptr) {
484 // Let the interested party know that we failed the class.
David Brazdil15fc7292016-09-02 14:13:18 +0100485 ClassReference ref(dex_file, dex_file->GetIndexForClassDef(class_def));
Andreas Gampe53e32d12015-12-09 21:03:23 -0800486 callbacks->ClassRejected(ref);
487 }
Igor Murashkin4d7b75f2015-07-21 17:03:36 -0700488 }
Andreas Gampe92d77202017-12-06 20:49:00 -0800489 if (VLOG_IS_ON(verifier) || VLOG_IS_ON(verifier_debug)) {
Ian Rogers46960fe2014-05-23 10:43:43 -0700490 std::cout << "\n" << verifier.info_messages_.str();
491 verifier.Dump(std::cout);
jeffhaof56197c2012-03-05 18:01:54 -0800492 }
jeffhaof56197c2012-03-05 18:01:54 -0800493 }
Mathieu Chartier8e219ae2014-08-19 14:29:46 -0700494 if (kTimeVerifyMethod) {
495 uint64_t duration_ns = NanoTime() - start_ns;
496 if (duration_ns > MsToNs(100)) {
David Sehr709b0702016-10-13 09:12:37 -0700497 LOG(WARNING) << "Verification of " << dex_file->PrettyMethod(method_idx)
Andreas Gampea4f5bf62015-05-18 20:50:12 -0700498 << " took " << PrettyDuration(duration_ns)
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800499 << (IsLargeMethod(verifier.CodeItem()) ? " (large method)" : "");
Mathieu Chartier8e219ae2014-08-19 14:29:46 -0700500 }
Ian Rogersc8982582012-09-07 16:53:25 -0700501 }
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800502 result.types = verifier.encountered_failure_types_;
Ian Rogersc8982582012-09-07 16:53:25 -0700503 return result;
jeffhaof56197c2012-03-05 18:01:54 -0800504}
505
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100506MethodVerifier* MethodVerifier::VerifyMethodAndDump(Thread* self,
507 VariableIndentationOutputStream* vios,
508 uint32_t dex_method_idx,
509 const DexFile* dex_file,
510 Handle<mirror::DexCache> dex_cache,
511 Handle<mirror::ClassLoader> class_loader,
David Brazdil15fc7292016-09-02 14:13:18 +0100512 const DexFile::ClassDef& class_def,
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100513 const DexFile::CodeItem* code_item,
514 ArtMethod* method,
515 uint32_t method_access_flags) {
Andreas Gampebf9611f2016-03-25 16:58:00 -0700516 MethodVerifier* verifier = new MethodVerifier(self,
517 dex_file,
518 dex_cache,
519 class_loader,
520 class_def,
521 code_item,
522 dex_method_idx,
523 method,
524 method_access_flags,
525 true /* can_load_classes */,
526 true /* allow_soft_failures */,
527 true /* need_precise_constants */,
528 true /* verify_to_dump */,
529 true /* allow_thread_suspension */);
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700530 verifier->Verify();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100531 verifier->DumpFailures(vios->Stream());
532 vios->Stream() << verifier->info_messages_.str();
Andreas Gampe5cbcde22014-09-16 14:59:49 -0700533 // Only dump and return if no hard failures. Otherwise the verifier may be not fully initialized
534 // and querying any info is dangerous/can abort.
535 if (verifier->have_pending_hard_failure_) {
536 delete verifier;
537 return nullptr;
538 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100539 verifier->Dump(vios);
Andreas Gampe5cbcde22014-09-16 14:59:49 -0700540 return verifier;
541 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800542}
543
Ian Rogers7b078e82014-09-10 14:44:24 -0700544MethodVerifier::MethodVerifier(Thread* self,
Andreas Gampe53e32d12015-12-09 21:03:23 -0800545 const DexFile* dex_file,
546 Handle<mirror::DexCache> dex_cache,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700547 Handle<mirror::ClassLoader> class_loader,
David Brazdil15fc7292016-09-02 14:13:18 +0100548 const DexFile::ClassDef& class_def,
Andreas Gampe53e32d12015-12-09 21:03:23 -0800549 const DexFile::CodeItem* code_item,
550 uint32_t dex_method_idx,
551 ArtMethod* method,
552 uint32_t method_access_flags,
553 bool can_load_classes,
554 bool allow_soft_failures,
555 bool need_precise_constants,
556 bool verify_to_dump,
Mathieu Chartier4306ef82014-12-19 18:41:47 -0800557 bool allow_thread_suspension)
Ian Rogers7b078e82014-09-10 14:44:24 -0700558 : self_(self),
Mathieu Chartierde40d472015-10-15 17:47:48 -0700559 arena_stack_(Runtime::Current()->GetArenaPool()),
Vladimir Marko69d310e2017-10-09 14:12:23 +0100560 allocator_(&arena_stack_),
Alex Lightd9aff132017-10-31 22:30:05 +0000561 reg_types_(can_load_classes, allocator_, allow_thread_suspension),
Vladimir Marko69d310e2017-10-09 14:12:23 +0100562 reg_table_(allocator_),
Andreas Gampee2abbc62017-09-15 11:59:26 -0700563 work_insn_idx_(dex::kDexNoIndex),
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800564 dex_method_idx_(dex_method_idx),
Ian Rogers637c65b2013-05-31 11:46:00 -0700565 mirror_method_(method),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700566 method_access_flags_(method_access_flags),
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700567 return_type_(nullptr),
jeffhaof56197c2012-03-05 18:01:54 -0800568 dex_file_(dex_file),
569 dex_cache_(dex_cache),
570 class_loader_(class_loader),
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700571 class_def_(class_def),
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800572 code_item_accessor_(dex_file, code_item),
Ian Rogers7b078e82014-09-10 14:44:24 -0700573 declaring_class_(nullptr),
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700574 interesting_dex_pc_(-1),
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700575 monitor_enter_dex_pcs_(nullptr),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700576 have_pending_hard_failure_(false),
jeffhaofaf459e2012-08-31 15:32:47 -0700577 have_pending_runtime_throw_failure_(false),
Igor Murashkin4d7b75f2015-07-21 17:03:36 -0700578 have_pending_experimental_failure_(false),
Andreas Gamped12e7822015-06-25 10:26:40 -0700579 have_any_pending_runtime_throw_failure_(false),
jeffhaof56197c2012-03-05 18:01:54 -0800580 new_instance_count_(0),
Elliott Hughes80537bb2013-01-04 16:37:26 -0800581 monitor_enter_count_(0),
Andreas Gampe0760a812015-08-26 17:12:51 -0700582 encountered_failure_types_(0),
Jeff Haoee988952013-04-16 14:23:47 -0700583 can_load_classes_(can_load_classes),
Sebastien Hertz4d4adb12013-07-24 16:14:19 +0200584 allow_soft_failures_(allow_soft_failures),
Ian Rogers46960fe2014-05-23 10:43:43 -0700585 need_precise_constants_(need_precise_constants),
Sebastien Hertz4d4adb12013-07-24 16:14:19 +0200586 has_check_casts_(false),
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700587 has_virtual_or_interface_invokes_(false),
Mathieu Chartier4306ef82014-12-19 18:41:47 -0800588 verify_to_dump_(verify_to_dump),
Andreas Gamped5ad72f2015-06-26 17:33:47 -0700589 allow_thread_suspension_(allow_thread_suspension),
Andreas Gampee6215c02015-08-31 18:54:38 -0700590 is_constructor_(false),
Andreas Gamped5ad72f2015-06-26 17:33:47 -0700591 link_(nullptr) {
Mathieu Chartierd0ad2ee2015-03-31 14:59:59 -0700592 self->PushVerifier(this);
jeffhaof56197c2012-03-05 18:01:54 -0800593}
594
Mathieu Chartier590fee92013-09-13 13:46:47 -0700595MethodVerifier::~MethodVerifier() {
Mathieu Chartierd0ad2ee2015-03-31 14:59:59 -0700596 Thread::Current()->PopVerifier(this);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700597 STLDeleteElements(&failure_messages_);
598}
599
Andreas Gampeaaf0d382017-11-27 14:10:21 -0800600void MethodVerifier::FindLocksAtDexPc(
601 ArtMethod* m,
602 uint32_t dex_pc,
603 std::vector<MethodVerifier::DexLockInfo>* monitor_enter_dex_pcs) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700604 StackHandleScope<2> hs(Thread::Current());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700605 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache()));
606 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader()));
Andreas Gampebf9611f2016-03-25 16:58:00 -0700607 MethodVerifier verifier(hs.Self(),
608 m->GetDexFile(),
609 dex_cache,
610 class_loader,
David Brazdil15fc7292016-09-02 14:13:18 +0100611 m->GetClassDef(),
Andreas Gampebf9611f2016-03-25 16:58:00 -0700612 m->GetCodeItem(),
613 m->GetDexMethodIndex(),
614 m,
615 m->GetAccessFlags(),
616 false /* can_load_classes */,
617 true /* allow_soft_failures */,
618 false /* need_precise_constants */,
619 false /* verify_to_dump */,
620 false /* allow_thread_suspension */);
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700621 verifier.interesting_dex_pc_ = dex_pc;
Ian Rogers46960fe2014-05-23 10:43:43 -0700622 verifier.monitor_enter_dex_pcs_ = monitor_enter_dex_pcs;
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700623 verifier.FindLocksAtDexPc();
624}
625
626void MethodVerifier::FindLocksAtDexPc() {
Ian Rogers7b078e82014-09-10 14:44:24 -0700627 CHECK(monitor_enter_dex_pcs_ != nullptr);
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -0800628 CHECK(code_item_accessor_.HasCodeItem()); // This only makes sense for methods with code.
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700629
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -0800630 // Quick check whether there are any monitor_enter instructions before verifying.
631 for (const DexInstructionPcPair& inst : code_item_accessor_) {
632 if (inst->Opcode() == Instruction::MONITOR_ENTER) {
633 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
634 // verification. In practice, the phase we want relies on data structures set up by all the
635 // earlier passes, so we just run the full method verification and bail out early when we've
636 // got what we wanted.
637 Verify();
638 return;
639 }
Andreas Gampecb3c08f2014-09-18 13:16:38 -0700640 }
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700641}
642
Mathieu Chartiere401d142015-04-22 13:56:20 -0700643ArtField* MethodVerifier::FindAccessedFieldAtDexPc(ArtMethod* m, uint32_t dex_pc) {
644 StackHandleScope<2> hs(Thread::Current());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700645 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache()));
646 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader()));
Andreas Gampebf9611f2016-03-25 16:58:00 -0700647 MethodVerifier verifier(hs.Self(),
648 m->GetDexFile(),
649 dex_cache,
650 class_loader,
David Brazdil15fc7292016-09-02 14:13:18 +0100651 m->GetClassDef(),
Andreas Gampebf9611f2016-03-25 16:58:00 -0700652 m->GetCodeItem(),
653 m->GetDexMethodIndex(),
654 m,
655 m->GetAccessFlags(),
656 true /* can_load_classes */,
657 true /* allow_soft_failures */,
658 false /* need_precise_constants */,
659 false /* verify_to_dump */,
660 true /* allow_thread_suspension */);
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200661 return verifier.FindAccessedFieldAtDexPc(dex_pc);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200662}
663
Mathieu Chartierc7853442015-03-27 14:35:38 -0700664ArtField* MethodVerifier::FindAccessedFieldAtDexPc(uint32_t dex_pc) {
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -0800665 CHECK(code_item_accessor_.HasCodeItem()); // This only makes sense for methods with code.
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200666
667 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
668 // verification. In practice, the phase we want relies on data structures set up by all the
669 // earlier passes, so we just run the full method verification and bail out early when we've
670 // got what we wanted.
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200671 bool success = Verify();
672 if (!success) {
Ian Rogers9bc54402014-04-17 16:40:01 -0700673 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200674 }
675 RegisterLine* register_line = reg_table_.GetLine(dex_pc);
Ian Rogers7b078e82014-09-10 14:44:24 -0700676 if (register_line == nullptr) {
Ian Rogers9bc54402014-04-17 16:40:01 -0700677 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200678 }
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -0800679 const Instruction* inst = &code_item_accessor_.InstructionAt(dex_pc);
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200680 return GetQuickFieldAccess(inst, register_line);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200681}
682
Mathieu Chartiere401d142015-04-22 13:56:20 -0700683ArtMethod* MethodVerifier::FindInvokedMethodAtDexPc(ArtMethod* m, uint32_t dex_pc) {
684 StackHandleScope<2> hs(Thread::Current());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700685 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache()));
686 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader()));
Andreas Gampebf9611f2016-03-25 16:58:00 -0700687 MethodVerifier verifier(hs.Self(),
688 m->GetDexFile(),
689 dex_cache,
690 class_loader,
David Brazdil15fc7292016-09-02 14:13:18 +0100691 m->GetClassDef(),
Andreas Gampebf9611f2016-03-25 16:58:00 -0700692 m->GetCodeItem(),
693 m->GetDexMethodIndex(),
694 m,
695 m->GetAccessFlags(),
696 true /* can_load_classes */,
697 true /* allow_soft_failures */,
698 false /* need_precise_constants */,
699 false /* verify_to_dump */,
700 true /* allow_thread_suspension */);
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200701 return verifier.FindInvokedMethodAtDexPc(dex_pc);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200702}
703
Mathieu Chartiere401d142015-04-22 13:56:20 -0700704ArtMethod* MethodVerifier::FindInvokedMethodAtDexPc(uint32_t dex_pc) {
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -0800705 CHECK(code_item_accessor_.HasCodeItem()); // This only makes sense for methods with code.
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200706
707 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
708 // verification. In practice, the phase we want relies on data structures set up by all the
709 // earlier passes, so we just run the full method verification and bail out early when we've
710 // got what we wanted.
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200711 bool success = Verify();
712 if (!success) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700713 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200714 }
715 RegisterLine* register_line = reg_table_.GetLine(dex_pc);
Ian Rogers7b078e82014-09-10 14:44:24 -0700716 if (register_line == nullptr) {
717 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200718 }
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -0800719 const Instruction* inst = &code_item_accessor_.InstructionAt(dex_pc);
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200720 const bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
Mathieu Chartier091d2382015-03-06 10:59:06 -0800721 return GetQuickInvokedMethod(inst, register_line, is_range, false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200722}
723
Ian Rogersad0b3a32012-04-16 14:50:24 -0700724bool MethodVerifier::Verify() {
Andreas Gampee6215c02015-08-31 18:54:38 -0700725 // Some older code doesn't correctly mark constructors as such. Test for this case by looking at
726 // the name.
727 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
728 const char* method_name = dex_file_->StringDataByIdx(method_id.name_idx_);
729 bool instance_constructor_by_name = strcmp("<init>", method_name) == 0;
730 bool static_constructor_by_name = strcmp("<clinit>", method_name) == 0;
731 bool constructor_by_name = instance_constructor_by_name || static_constructor_by_name;
732 // Check that only constructors are tagged, and check for bad code that doesn't tag constructors.
733 if ((method_access_flags_ & kAccConstructor) != 0) {
734 if (!constructor_by_name) {
735 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
736 << "method is marked as constructor, but not named accordingly";
jeffhaobdb76512011-09-07 11:43:16 -0700737 return false;
Andreas Gampee6215c02015-08-31 18:54:38 -0700738 }
739 is_constructor_ = true;
740 } else if (constructor_by_name) {
David Sehr709b0702016-10-13 09:12:37 -0700741 LOG(WARNING) << "Method " << dex_file_->PrettyMethod(dex_method_idx_)
Andreas Gampee6215c02015-08-31 18:54:38 -0700742 << " not marked as constructor.";
743 is_constructor_ = true;
744 }
745 // If it's a constructor, check whether IsStatic() matches the name.
746 // This should have been rejected by the dex file verifier. Only do in debug build.
747 if (kIsDebugBuild) {
748 if (IsConstructor()) {
749 if (IsStatic() ^ static_constructor_by_name) {
750 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
751 << "constructor name doesn't match static flag";
752 return false;
753 }
jeffhaobdb76512011-09-07 11:43:16 -0700754 }
jeffhaobdb76512011-09-07 11:43:16 -0700755 }
Andreas Gampee6215c02015-08-31 18:54:38 -0700756
757 // Methods may only have one of public/protected/private.
758 // This should have been rejected by the dex file verifier. Only do in debug build.
759 if (kIsDebugBuild) {
760 size_t access_mod_count =
761 (((method_access_flags_ & kAccPublic) == 0) ? 0 : 1) +
762 (((method_access_flags_ & kAccProtected) == 0) ? 0 : 1) +
763 (((method_access_flags_ & kAccPrivate) == 0) ? 0 : 1);
764 if (access_mod_count > 1) {
765 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "method has more than one of public/protected/private";
766 return false;
767 }
768 }
769
770 // If there aren't any instructions, make sure that's expected, then exit successfully.
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -0800771 if (!code_item_accessor_.HasCodeItem()) {
Andreas Gampe76ed99d2016-03-28 18:31:29 -0700772 // Only native or abstract methods may not have code.
773 if ((method_access_flags_ & (kAccNative | kAccAbstract)) == 0) {
774 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "zero-length code in concrete non-native method";
775 return false;
776 }
777
Andreas Gampee6215c02015-08-31 18:54:38 -0700778 // This should have been rejected by the dex file verifier. Only do in debug build.
Andreas Gampe76ed99d2016-03-28 18:31:29 -0700779 // Note: the above will also be rejected in the dex file verifier, starting in dex version 37.
Andreas Gampee6215c02015-08-31 18:54:38 -0700780 if (kIsDebugBuild) {
Andreas Gampee6215c02015-08-31 18:54:38 -0700781 if ((method_access_flags_ & kAccAbstract) != 0) {
782 // Abstract methods are not allowed to have the following flags.
783 static constexpr uint32_t kForbidden =
784 kAccPrivate |
785 kAccStatic |
786 kAccFinal |
787 kAccNative |
788 kAccStrict |
789 kAccSynchronized;
790 if ((method_access_flags_ & kForbidden) != 0) {
791 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
792 << "method can't be abstract and private/static/final/native/strict/synchronized";
793 return false;
794 }
795 }
David Brazdil15fc7292016-09-02 14:13:18 +0100796 if ((class_def_.GetJavaAccessFlags() & kAccInterface) != 0) {
Alex Lighteb7c1442015-08-31 13:17:42 -0700797 // Interface methods must be public and abstract (if default methods are disabled).
Neil Fuller9724c632016-01-07 15:42:47 +0000798 uint32_t kRequired = kAccPublic;
Alex Lighteb7c1442015-08-31 13:17:42 -0700799 if ((method_access_flags_ & kRequired) != kRequired) {
Neil Fuller9724c632016-01-07 15:42:47 +0000800 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface methods must be public";
Andreas Gampee6215c02015-08-31 18:54:38 -0700801 return false;
802 }
803 // In addition to the above, interface methods must not be protected.
804 static constexpr uint32_t kForbidden = kAccProtected;
805 if ((method_access_flags_ & kForbidden) != 0) {
806 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface methods can't be protected";
807 return false;
808 }
809 }
810 // We also don't allow constructors to be abstract or native.
811 if (IsConstructor()) {
812 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "constructors can't be abstract or native";
813 return false;
814 }
815 }
816 return true;
817 }
818
819 // This should have been rejected by the dex file verifier. Only do in debug build.
820 if (kIsDebugBuild) {
821 // When there's code, the method must not be native or abstract.
822 if ((method_access_flags_ & (kAccNative | kAccAbstract)) != 0) {
823 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "non-zero-length code in abstract or native method";
824 return false;
825 }
826
David Brazdil15fc7292016-09-02 14:13:18 +0100827 if ((class_def_.GetJavaAccessFlags() & kAccInterface) != 0) {
Alex Lighteb7c1442015-08-31 13:17:42 -0700828 // Interfaces may always have static initializers for their fields. If we are running with
829 // default methods enabled we also allow other public, static, non-final methods to have code.
830 // Otherwise that is the only type of method allowed.
Alex Light0db36b32015-10-27 14:06:34 -0700831 if (!(IsConstructor() && IsStatic())) {
Neil Fuller9724c632016-01-07 15:42:47 +0000832 if (IsInstanceConstructor()) {
833 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interfaces may not have non-static constructor";
834 return false;
835 } else if (method_access_flags_ & kAccFinal) {
836 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interfaces may not have final methods";
837 return false;
Alex Lightb55f1ac2016-04-12 15:50:55 -0700838 } else {
839 uint32_t access_flag_options = kAccPublic;
Mathieu Chartierf6e31472017-12-28 13:32:08 -0800840 if (dex_file_->SupportsDefaultMethods()) {
Alex Lightb55f1ac2016-04-12 15:50:55 -0700841 access_flag_options |= kAccPrivate;
842 }
843 if (!(method_access_flags_ & access_flag_options)) {
844 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
845 << "interfaces may not have protected or package-private members";
846 return false;
847 }
Alex Lighteb7c1442015-08-31 13:17:42 -0700848 }
Andreas Gampee6215c02015-08-31 18:54:38 -0700849 }
850 }
851
852 // Instance constructors must not be synchronized.
853 if (IsInstanceConstructor()) {
854 static constexpr uint32_t kForbidden = kAccSynchronized;
855 if ((method_access_flags_ & kForbidden) != 0) {
856 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "constructors can't be synchronized";
857 return false;
858 }
859 }
860 }
861
Ian Rogersd81871c2011-10-03 13:57:23 -0700862 // Sanity-check the register counts. ins + locals = registers, so make sure that ins <= registers.
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -0800863 if (code_item_accessor_.InsSize() > code_item_accessor_.RegistersSize()) {
864 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad register counts (ins="
865 << code_item_accessor_.InsSize()
866 << " regs=" << code_item_accessor_.RegistersSize();
Ian Rogersd81871c2011-10-03 13:57:23 -0700867 return false;
jeffhaobdb76512011-09-07 11:43:16 -0700868 }
Alex Lighteb7c1442015-08-31 13:17:42 -0700869
Ian Rogersd81871c2011-10-03 13:57:23 -0700870 // Allocate and initialize an array to hold instruction data.
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -0800871 insn_flags_.reset(allocator_.AllocArray<InstructionFlags>(
872 code_item_accessor_.InsnsSizeInCodeUnits()));
Mathieu Chartierde40d472015-10-15 17:47:48 -0700873 DCHECK(insn_flags_ != nullptr);
874 std::uninitialized_fill_n(insn_flags_.get(),
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -0800875 code_item_accessor_.InsnsSizeInCodeUnits(),
Mathieu Chartierde40d472015-10-15 17:47:48 -0700876 InstructionFlags());
Ian Rogersd81871c2011-10-03 13:57:23 -0700877 // Run through the instructions and see if the width checks out.
878 bool result = ComputeWidthsAndCountOps();
Andreas Gampebf1cb772017-05-15 15:39:00 -0700879 bool allow_runtime_only_instructions = !Runtime::Current()->IsAotCompiler() || verify_to_dump_;
Ian Rogersd81871c2011-10-03 13:57:23 -0700880 // Flag instructions guarded by a "try" block and check exception handlers.
881 result = result && ScanTryCatchBlocks();
882 // Perform static instruction verification.
Andreas Gampebf1cb772017-05-15 15:39:00 -0700883 result = result && (allow_runtime_only_instructions
884 ? VerifyInstructions<true>()
885 : VerifyInstructions<false>());
Ian Rogersad0b3a32012-04-16 14:50:24 -0700886 // Perform code-flow analysis and return.
Vladimir Marko2b5eaa22013-12-13 13:59:30 +0000887 result = result && VerifyCodeFlow();
Andreas Gampe53e32d12015-12-09 21:03:23 -0800888
Vladimir Marko2b5eaa22013-12-13 13:59:30 +0000889 return result;
jeffhaoba5ebb92011-08-25 17:24:37 -0700890}
891
Ian Rogers776ac1f2012-04-13 23:36:36 -0700892std::ostream& MethodVerifier::Fail(VerifyError error) {
Andreas Gampe0760a812015-08-26 17:12:51 -0700893 // Mark the error type as encountered.
Andreas Gampea727e372015-08-25 09:22:37 -0700894 encountered_failure_types_ |= static_cast<uint32_t>(error);
Andreas Gampe0760a812015-08-26 17:12:51 -0700895
Ian Rogersad0b3a32012-04-16 14:50:24 -0700896 switch (error) {
897 case VERIFY_ERROR_NO_CLASS:
898 case VERIFY_ERROR_NO_FIELD:
899 case VERIFY_ERROR_NO_METHOD:
900 case VERIFY_ERROR_ACCESS_CLASS:
901 case VERIFY_ERROR_ACCESS_FIELD:
902 case VERIFY_ERROR_ACCESS_METHOD:
Ian Rogers08f753d2012-08-24 14:35:25 -0700903 case VERIFY_ERROR_INSTANTIATION:
904 case VERIFY_ERROR_CLASS_CHANGE:
Igor Murashkin158f35c2015-06-10 15:55:30 -0700905 case VERIFY_ERROR_FORCE_INTERPRETER:
Andreas Gampea727e372015-08-25 09:22:37 -0700906 case VERIFY_ERROR_LOCKING:
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800907 if (Runtime::Current()->IsAotCompiler() || !can_load_classes_) {
jeffhaofaf459e2012-08-31 15:32:47 -0700908 // If we're optimistically running verification at compile time, turn NO_xxx, ACCESS_xxx,
909 // class change and instantiation errors into soft verification errors so that we re-verify
910 // at runtime. We may fail to find or to agree on access because of not yet available class
911 // loaders, or class loaders that will differ at runtime. In these cases, we don't want to
912 // affect the soundness of the code being compiled. Instead, the generated code runs "slow
913 // paths" that dynamically perform the verification and cause the behavior to be that akin
914 // to an interpreter.
915 error = VERIFY_ERROR_BAD_CLASS_SOFT;
916 } else {
Jeff Haoa3faaf42013-09-03 19:07:00 -0700917 // If we fail again at runtime, mark that this instruction would throw and force this
918 // method to be executed using the interpreter with checks.
jeffhaofaf459e2012-08-31 15:32:47 -0700919 have_pending_runtime_throw_failure_ = true;
Andreas Gamped7f8d052015-03-12 11:05:47 -0700920
921 // We need to save the work_line if the instruction wasn't throwing before. Otherwise we'll
922 // try to merge garbage.
923 // Note: this assumes that Fail is called before we do any work_line modifications.
Andreas Gamped5ad72f2015-06-26 17:33:47 -0700924 // Note: this can fail before we touch any instruction, for the signature of a method. So
925 // add a check.
Andreas Gampee2abbc62017-09-15 11:59:26 -0700926 if (work_insn_idx_ < dex::kDexNoIndex) {
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -0800927 const Instruction& inst = code_item_accessor_.InstructionAt(work_insn_idx_);
Mathieu Chartier1d2d4ff2017-09-23 16:11:06 -0700928 int opcode_flags = Instruction::FlagsOf(inst.Opcode());
Andreas Gamped7f8d052015-03-12 11:05:47 -0700929
Andreas Gamped5ad72f2015-06-26 17:33:47 -0700930 if ((opcode_flags & Instruction::kThrow) == 0 && CurrentInsnFlags()->IsInTry()) {
931 saved_line_->CopyFromLine(work_line_.get());
932 }
Andreas Gamped7f8d052015-03-12 11:05:47 -0700933 }
jeffhaofaf459e2012-08-31 15:32:47 -0700934 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700935 break;
Andreas Gampea727e372015-08-25 09:22:37 -0700936
Ian Rogersad0b3a32012-04-16 14:50:24 -0700937 // Indication that verification should be retried at runtime.
938 case VERIFY_ERROR_BAD_CLASS_SOFT:
Jeff Haoee988952013-04-16 14:23:47 -0700939 if (!allow_soft_failures_) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700940 have_pending_hard_failure_ = true;
941 }
942 break;
Andreas Gampea727e372015-08-25 09:22:37 -0700943
jeffhaod5347e02012-03-22 17:25:05 -0700944 // Hard verification failures at compile time will still fail at runtime, so the class is
945 // marked as rejected to prevent it from being compiled.
Ian Rogersad0b3a32012-04-16 14:50:24 -0700946 case VERIFY_ERROR_BAD_CLASS_HARD: {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700947 have_pending_hard_failure_ = true;
Andreas Gampeebf850c2015-08-14 15:37:35 -0700948 if (VLOG_IS_ON(verifier) && kDumpRegLinesOnHardFailureIfVLOG) {
949 ScopedObjectAccess soa(Thread::Current());
950 std::ostringstream oss;
951 Dump(oss);
952 LOG(ERROR) << oss.str();
953 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700954 break;
Ian Rogers47a05882012-02-03 12:23:33 -0800955 }
956 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700957 failures_.push_back(error);
David Sehr709b0702016-10-13 09:12:37 -0700958 std::string location(StringPrintf("%s: [0x%X] ", dex_file_->PrettyMethod(dex_method_idx_).c_str(),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700959 work_insn_idx_));
Elena Sayapina78480ec2014-08-15 15:52:42 +0700960 std::ostringstream* failure_message = new std::ostringstream(location, std::ostringstream::ate);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700961 failure_messages_.push_back(failure_message);
962 return *failure_message;
963}
964
Ian Rogers576ca0c2014-06-06 15:58:22 -0700965std::ostream& MethodVerifier::LogVerifyInfo() {
David Sehr709b0702016-10-13 09:12:37 -0700966 return info_messages_ << "VFY: " << dex_file_->PrettyMethod(dex_method_idx_)
Ian Rogers576ca0c2014-06-06 15:58:22 -0700967 << '[' << reinterpret_cast<void*>(work_insn_idx_) << "] : ";
968}
969
Ian Rogersad0b3a32012-04-16 14:50:24 -0700970void MethodVerifier::PrependToLastFailMessage(std::string prepend) {
971 size_t failure_num = failure_messages_.size();
972 DCHECK_NE(failure_num, 0U);
973 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
974 prepend += last_fail_message->str();
Elena Sayapina78480ec2014-08-15 15:52:42 +0700975 failure_messages_[failure_num - 1] = new std::ostringstream(prepend, std::ostringstream::ate);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700976 delete last_fail_message;
977}
978
Vladimir Marko5c657fe2016-11-03 15:12:29 +0000979void MethodVerifier::AppendToLastFailMessage(const std::string& append) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700980 size_t failure_num = failure_messages_.size();
981 DCHECK_NE(failure_num, 0U);
982 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
983 (*last_fail_message) << append;
Ian Rogers47a05882012-02-03 12:23:33 -0800984}
985
Ian Rogers776ac1f2012-04-13 23:36:36 -0700986bool MethodVerifier::ComputeWidthsAndCountOps() {
jeffhaobdb76512011-09-07 11:43:16 -0700987 size_t new_instance_count = 0;
988 size_t monitor_enter_count = 0;
jeffhaobdb76512011-09-07 11:43:16 -0700989
Mathieu Chartieraf7c9022017-10-27 09:42:46 -0700990 // We can't assume the instruction is well formed, handle the case where calculating the size
991 // goes past the end of the code item.
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -0800992 SafeDexInstructionIterator it(code_item_accessor_.begin(), code_item_accessor_.end());
993 for ( ; !it.IsErrorState() && it < code_item_accessor_.end(); ++it) {
Mathieu Chartieraf7c9022017-10-27 09:42:46 -0700994 // In case the instruction goes past the end of the code item, make sure to not process it.
995 SafeDexInstructionIterator next = it;
996 ++next;
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -0800997 if (next.IsErrorState()) {
Mathieu Chartieraf7c9022017-10-27 09:42:46 -0700998 break;
999 }
1000 Instruction::Code opcode = it->Opcode();
Ian Rogersa9a82542013-10-04 11:17:26 -07001001 switch (opcode) {
1002 case Instruction::APUT_OBJECT:
1003 case Instruction::CHECK_CAST:
1004 has_check_casts_ = true;
1005 break;
1006 case Instruction::INVOKE_VIRTUAL:
1007 case Instruction::INVOKE_VIRTUAL_RANGE:
1008 case Instruction::INVOKE_INTERFACE:
1009 case Instruction::INVOKE_INTERFACE_RANGE:
1010 has_virtual_or_interface_invokes_ = true;
1011 break;
1012 case Instruction::MONITOR_ENTER:
1013 monitor_enter_count++;
1014 break;
1015 case Instruction::NEW_INSTANCE:
1016 new_instance_count++;
1017 break;
1018 default:
1019 break;
jeffhaobdb76512011-09-07 11:43:16 -07001020 }
Mathieu Chartier2b2bef22017-10-26 17:10:19 -07001021 GetInstructionFlags(it.DexPc()).SetIsOpcode();
jeffhaobdb76512011-09-07 11:43:16 -07001022 }
1023
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001024 if (it != code_item_accessor_.end()) {
1025 const size_t insns_size = code_item_accessor_.InsnsSizeInCodeUnits();
jeffhaod5347e02012-03-22 17:25:05 -07001026 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "code did not end where expected ("
Mathieu Chartier2b2bef22017-10-26 17:10:19 -07001027 << it.DexPc() << " vs. " << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -07001028 return false;
1029 }
1030
Ian Rogersd81871c2011-10-03 13:57:23 -07001031 new_instance_count_ = new_instance_count;
1032 monitor_enter_count_ = monitor_enter_count;
jeffhaobdb76512011-09-07 11:43:16 -07001033 return true;
1034}
1035
Ian Rogers776ac1f2012-04-13 23:36:36 -07001036bool MethodVerifier::ScanTryCatchBlocks() {
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001037 const uint32_t tries_size = code_item_accessor_.TriesSize();
jeffhaobdb76512011-09-07 11:43:16 -07001038 if (tries_size == 0) {
1039 return true;
1040 }
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001041 const uint32_t insns_size = code_item_accessor_.InsnsSizeInCodeUnits();
1042 for (const DexFile::TryItem& try_item : code_item_accessor_.TryItems()) {
1043 const uint32_t start = try_item.start_addr_;
1044 const uint32_t end = start + try_item.insn_count_;
jeffhaobdb76512011-09-07 11:43:16 -07001045 if ((start >= end) || (start >= insns_size) || (end > insns_size)) {
jeffhaod5347e02012-03-22 17:25:05 -07001046 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad exception entry: startAddr=" << start
1047 << " endAddr=" << end << " (size=" << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -07001048 return false;
1049 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07001050 if (!GetInstructionFlags(start).IsOpcode()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001051 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1052 << "'try' block starts inside an instruction (" << start << ")";
jeffhaobdb76512011-09-07 11:43:16 -07001053 return false;
1054 }
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001055 DexInstructionIterator end_it(code_item_accessor_.Insns(), end);
1056 for (DexInstructionIterator it(code_item_accessor_.Insns(), start); it < end_it; ++it) {
1057 GetInstructionFlags(it.DexPc()).SetInTry();
jeffhaobdb76512011-09-07 11:43:16 -07001058 }
1059 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -08001060 // Iterate over each of the handlers to verify target addresses.
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001061 const uint8_t* handlers_ptr = code_item_accessor_.GetCatchHandlerData();
1062 const uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
Ian Rogers28ad40d2011-10-27 15:19:26 -07001063 ClassLinker* linker = Runtime::Current()->GetClassLinker();
jeffhaobdb76512011-09-07 11:43:16 -07001064 for (uint32_t idx = 0; idx < handlers_size; idx++) {
Ian Rogers0571d352011-11-03 19:51:38 -07001065 CatchHandlerIterator iterator(handlers_ptr);
1066 for (; iterator.HasNext(); iterator.Next()) {
Igor Murashkin2ffb7032017-11-08 13:35:21 -08001067 uint32_t dex_pc = iterator.GetHandlerAddress();
Mathieu Chartierde40d472015-10-15 17:47:48 -07001068 if (!GetInstructionFlags(dex_pc).IsOpcode()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001069 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1070 << "exception handler starts at bad address (" << dex_pc << ")";
jeffhaobdb76512011-09-07 11:43:16 -07001071 return false;
1072 }
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001073 if (!CheckNotMoveResult(code_item_accessor_.Insns(), dex_pc)) {
Stephen Kyle9bc61992014-09-22 13:53:15 +01001074 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1075 << "exception handler begins with move-result* (" << dex_pc << ")";
1076 return false;
1077 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07001078 GetInstructionFlags(dex_pc).SetBranchTarget();
Ian Rogers28ad40d2011-10-27 15:19:26 -07001079 // Ensure exception types are resolved so that they don't need resolution to be delivered,
1080 // unresolved exception types will be ignored by exception delivery
Andreas Gampea5b09a62016-11-17 15:21:22 -08001081 if (iterator.GetHandlerTypeIndex().IsValid()) {
Vladimir Marko666ee3d2017-12-11 18:37:36 +00001082 ObjPtr<mirror::Class> exception_type =
1083 linker->ResolveType(iterator.GetHandlerTypeIndex(), dex_cache_, class_loader_);
Ian Rogers7b078e82014-09-10 14:44:24 -07001084 if (exception_type == nullptr) {
1085 DCHECK(self_->IsExceptionPending());
1086 self_->ClearException();
Ian Rogers28ad40d2011-10-27 15:19:26 -07001087 }
1088 }
jeffhaobdb76512011-09-07 11:43:16 -07001089 }
Ian Rogers0571d352011-11-03 19:51:38 -07001090 handlers_ptr = iterator.EndDataPointer();
jeffhaobdb76512011-09-07 11:43:16 -07001091 }
jeffhaobdb76512011-09-07 11:43:16 -07001092 return true;
1093}
1094
Andreas Gampebf1cb772017-05-15 15:39:00 -07001095template <bool kAllowRuntimeOnlyInstructions>
Ian Rogers776ac1f2012-04-13 23:36:36 -07001096bool MethodVerifier::VerifyInstructions() {
Ian Rogers0c7abda2012-09-19 13:33:42 -07001097 /* 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 -07001098 GetInstructionFlags(0).SetBranchTarget();
1099 GetInstructionFlags(0).SetCompileTimeInfoPoint();
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001100 for (const DexInstructionPcPair& inst : code_item_accessor_) {
Mathieu Chartier2b2bef22017-10-26 17:10:19 -07001101 const uint32_t dex_pc = inst.DexPc();
1102 if (!VerifyInstruction<kAllowRuntimeOnlyInstructions>(&inst.Inst(), dex_pc)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001103 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -07001104 return false;
1105 }
1106 /* Flag instructions that are garbage collection points */
Sameer Abu Asal02c42232013-04-30 12:09:45 -07001107 // All invoke points are marked as "Throw" points already.
1108 // We are relying on this to also count all the invokes as interesting.
Vladimir Marko8b858e12014-11-27 14:52:37 +00001109 if (inst->IsBranch()) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07001110 GetInstructionFlags(dex_pc).SetCompileTimeInfoPoint();
Vladimir Marko8b858e12014-11-27 14:52:37 +00001111 // The compiler also needs safepoints for fall-through to loop heads.
1112 // Such a loop head must be a target of a branch.
1113 int32_t offset = 0;
1114 bool cond, self_ok;
1115 bool target_ok = GetBranchOffset(dex_pc, &offset, &cond, &self_ok);
1116 DCHECK(target_ok);
Mathieu Chartierde40d472015-10-15 17:47:48 -07001117 GetInstructionFlags(dex_pc + offset).SetCompileTimeInfoPoint();
Vladimir Marko8b858e12014-11-27 14:52:37 +00001118 } else if (inst->IsSwitch() || inst->IsThrow()) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07001119 GetInstructionFlags(dex_pc).SetCompileTimeInfoPoint();
Ian Rogersb8c78592013-07-25 23:52:52 +00001120 } else if (inst->IsReturn()) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07001121 GetInstructionFlags(dex_pc).SetCompileTimeInfoPointAndReturn();
Ian Rogersd81871c2011-10-03 13:57:23 -07001122 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001123 }
1124 return true;
1125}
1126
Andreas Gampebf1cb772017-05-15 15:39:00 -07001127template <bool kAllowRuntimeOnlyInstructions>
1128bool MethodVerifier::VerifyInstruction(const Instruction* inst, uint32_t code_offset) {
Andreas Gampee05cc662017-05-15 10:17:30 -07001129 if (Instruction::kHaveExperimentalInstructions && UNLIKELY(inst->IsExperimental())) {
Igor Murashkin4d7b75f2015-07-21 17:03:36 -07001130 // Experimental instructions don't yet have verifier support implementation.
1131 // While it is possible to use them by themselves, when we try to use stable instructions
1132 // with a virtual register that was created by an experimental instruction,
1133 // the data flow analysis will fail.
1134 Fail(VERIFY_ERROR_FORCE_INTERPRETER)
1135 << "experimental instruction is not supported by verifier; skipping verification";
1136 have_pending_experimental_failure_ = true;
1137 return false;
1138 }
1139
Ian Rogersd81871c2011-10-03 13:57:23 -07001140 bool result = true;
1141 switch (inst->GetVerifyTypeArgumentA()) {
1142 case Instruction::kVerifyRegA:
Ian Rogers29a26482014-05-02 15:27:29 -07001143 result = result && CheckRegisterIndex(inst->VRegA());
Ian Rogersd81871c2011-10-03 13:57:23 -07001144 break;
1145 case Instruction::kVerifyRegAWide:
Ian Rogers29a26482014-05-02 15:27:29 -07001146 result = result && CheckWideRegisterIndex(inst->VRegA());
Ian Rogersd81871c2011-10-03 13:57:23 -07001147 break;
1148 }
1149 switch (inst->GetVerifyTypeArgumentB()) {
1150 case Instruction::kVerifyRegB:
Ian Rogers29a26482014-05-02 15:27:29 -07001151 result = result && CheckRegisterIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -07001152 break;
1153 case Instruction::kVerifyRegBField:
Ian Rogers29a26482014-05-02 15:27:29 -07001154 result = result && CheckFieldIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -07001155 break;
1156 case Instruction::kVerifyRegBMethod:
Ian Rogers29a26482014-05-02 15:27:29 -07001157 result = result && CheckMethodIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -07001158 break;
1159 case Instruction::kVerifyRegBNewInstance:
Andreas Gampea5b09a62016-11-17 15:21:22 -08001160 result = result && CheckNewInstance(dex::TypeIndex(inst->VRegB()));
Ian Rogersd81871c2011-10-03 13:57:23 -07001161 break;
1162 case Instruction::kVerifyRegBString:
Ian Rogers29a26482014-05-02 15:27:29 -07001163 result = result && CheckStringIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -07001164 break;
1165 case Instruction::kVerifyRegBType:
Andreas Gampea5b09a62016-11-17 15:21:22 -08001166 result = result && CheckTypeIndex(dex::TypeIndex(inst->VRegB()));
Ian Rogersd81871c2011-10-03 13:57:23 -07001167 break;
1168 case Instruction::kVerifyRegBWide:
Ian Rogers29a26482014-05-02 15:27:29 -07001169 result = result && CheckWideRegisterIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -07001170 break;
Orion Hodson2e599942017-09-22 16:17:41 +01001171 case Instruction::kVerifyRegBCallSite:
1172 result = result && CheckCallSiteIndex(inst->VRegB());
1173 break;
1174 case Instruction::kVerifyRegBMethodHandle:
1175 result = result && CheckMethodHandleIndex(inst->VRegB());
1176 break;
1177 case Instruction::kVerifyRegBPrototype:
1178 result = result && CheckPrototypeIndex(inst->VRegB());
1179 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001180 }
1181 switch (inst->GetVerifyTypeArgumentC()) {
1182 case Instruction::kVerifyRegC:
Ian Rogers29a26482014-05-02 15:27:29 -07001183 result = result && CheckRegisterIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -07001184 break;
1185 case Instruction::kVerifyRegCField:
Ian Rogers29a26482014-05-02 15:27:29 -07001186 result = result && CheckFieldIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -07001187 break;
1188 case Instruction::kVerifyRegCNewArray:
Andreas Gampea5b09a62016-11-17 15:21:22 -08001189 result = result && CheckNewArray(dex::TypeIndex(inst->VRegC()));
Ian Rogersd81871c2011-10-03 13:57:23 -07001190 break;
1191 case Instruction::kVerifyRegCType:
Andreas Gampea5b09a62016-11-17 15:21:22 -08001192 result = result && CheckTypeIndex(dex::TypeIndex(inst->VRegC()));
Ian Rogersd81871c2011-10-03 13:57:23 -07001193 break;
1194 case Instruction::kVerifyRegCWide:
Ian Rogers29a26482014-05-02 15:27:29 -07001195 result = result && CheckWideRegisterIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -07001196 break;
1197 }
Orion Hodsoncfa325e2016-10-13 10:25:54 +01001198 switch (inst->GetVerifyTypeArgumentH()) {
1199 case Instruction::kVerifyRegHPrototype:
1200 result = result && CheckPrototypeIndex(inst->VRegH());
1201 break;
1202 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001203 switch (inst->GetVerifyExtraFlags()) {
1204 case Instruction::kVerifyArrayData:
1205 result = result && CheckArrayData(code_offset);
1206 break;
1207 case Instruction::kVerifyBranchTarget:
1208 result = result && CheckBranchTarget(code_offset);
1209 break;
1210 case Instruction::kVerifySwitchTargets:
1211 result = result && CheckSwitchTargets(code_offset);
1212 break;
Andreas Gampec3314312014-06-19 18:13:29 -07001213 case Instruction::kVerifyVarArgNonZero:
1214 // Fall-through.
Ian Rogers29a26482014-05-02 15:27:29 -07001215 case Instruction::kVerifyVarArg: {
Taiju Tsuiki29498a22015-04-13 14:21:00 +09001216 // Instructions that can actually return a negative value shouldn't have this flag.
1217 uint32_t v_a = dchecked_integral_cast<uint32_t>(inst->VRegA());
1218 if ((inst->GetVerifyExtraFlags() == Instruction::kVerifyVarArgNonZero && v_a == 0) ||
1219 v_a > Instruction::kMaxVarArgRegs) {
1220 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << v_a << ") in "
Andreas Gampec3314312014-06-19 18:13:29 -07001221 "non-range invoke";
1222 return false;
1223 }
Taiju Tsuiki29498a22015-04-13 14:21:00 +09001224
Ian Rogers29a26482014-05-02 15:27:29 -07001225 uint32_t args[Instruction::kMaxVarArgRegs];
1226 inst->GetVarArgs(args);
Taiju Tsuiki29498a22015-04-13 14:21:00 +09001227 result = result && CheckVarArgRegs(v_a, args);
Ian Rogersd81871c2011-10-03 13:57:23 -07001228 break;
Ian Rogers29a26482014-05-02 15:27:29 -07001229 }
Andreas Gampec3314312014-06-19 18:13:29 -07001230 case Instruction::kVerifyVarArgRangeNonZero:
1231 // Fall-through.
Ian Rogersd81871c2011-10-03 13:57:23 -07001232 case Instruction::kVerifyVarArgRange:
Andreas Gampec3314312014-06-19 18:13:29 -07001233 if (inst->GetVerifyExtraFlags() == Instruction::kVerifyVarArgRangeNonZero &&
1234 inst->VRegA() <= 0) {
1235 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << inst->VRegA() << ") in "
1236 "range invoke";
1237 return false;
1238 }
Ian Rogers29a26482014-05-02 15:27:29 -07001239 result = result && CheckVarArgRangeRegs(inst->VRegA(), inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -07001240 break;
1241 case Instruction::kVerifyError:
jeffhaod5347e02012-03-22 17:25:05 -07001242 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected opcode " << inst->Name();
Ian Rogersd81871c2011-10-03 13:57:23 -07001243 result = false;
1244 break;
1245 }
Andreas Gampebf1cb772017-05-15 15:39:00 -07001246 if (!kAllowRuntimeOnlyInstructions && inst->GetVerifyIsRuntimeOnly()) {
Ian Rogers5fb22a92014-06-13 10:31:28 -07001247 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "opcode only expected at runtime " << inst->Name();
1248 result = false;
1249 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001250 return result;
1251}
1252
Ian Rogers7b078e82014-09-10 14:44:24 -07001253inline bool MethodVerifier::CheckRegisterIndex(uint32_t idx) {
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001254 if (UNLIKELY(idx >= code_item_accessor_.RegistersSize())) {
jeffhaod5347e02012-03-22 17:25:05 -07001255 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register index out of range (" << idx << " >= "
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001256 << code_item_accessor_.RegistersSize() << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001257 return false;
1258 }
1259 return true;
1260}
1261
Ian Rogers7b078e82014-09-10 14:44:24 -07001262inline bool MethodVerifier::CheckWideRegisterIndex(uint32_t idx) {
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001263 if (UNLIKELY(idx + 1 >= code_item_accessor_.RegistersSize())) {
jeffhaod5347e02012-03-22 17:25:05 -07001264 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "wide register index out of range (" << idx
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001265 << "+1 >= " << code_item_accessor_.RegistersSize() << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001266 return false;
1267 }
1268 return true;
1269}
1270
Orion Hodson2e599942017-09-22 16:17:41 +01001271inline bool MethodVerifier::CheckCallSiteIndex(uint32_t idx) {
1272 uint32_t limit = dex_file_->NumCallSiteIds();
1273 if (UNLIKELY(idx >= limit)) {
1274 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad call site index " << idx << " (max "
1275 << limit << ")";
1276 return false;
1277 }
1278 return true;
1279}
1280
Ian Rogers7b078e82014-09-10 14:44:24 -07001281inline bool MethodVerifier::CheckFieldIndex(uint32_t idx) {
Andreas Gampe29e81422017-05-15 16:29:32 -07001282 if (UNLIKELY(idx >= dex_file_->GetHeader().field_ids_size_)) {
jeffhaod5347e02012-03-22 17:25:05 -07001283 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad field index " << idx << " (max "
1284 << dex_file_->GetHeader().field_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001285 return false;
1286 }
1287 return true;
1288}
1289
Ian Rogers7b078e82014-09-10 14:44:24 -07001290inline bool MethodVerifier::CheckMethodIndex(uint32_t idx) {
Andreas Gampe29e81422017-05-15 16:29:32 -07001291 if (UNLIKELY(idx >= dex_file_->GetHeader().method_ids_size_)) {
jeffhaod5347e02012-03-22 17:25:05 -07001292 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad method index " << idx << " (max "
1293 << dex_file_->GetHeader().method_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001294 return false;
1295 }
1296 return true;
1297}
1298
Orion Hodson2e599942017-09-22 16:17:41 +01001299inline bool MethodVerifier::CheckMethodHandleIndex(uint32_t idx) {
1300 uint32_t limit = dex_file_->NumMethodHandles();
1301 if (UNLIKELY(idx >= limit)) {
1302 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad method handle index " << idx << " (max "
1303 << limit << ")";
1304 return false;
1305 }
1306 return true;
1307}
1308
Andreas Gampea5b09a62016-11-17 15:21:22 -08001309inline bool MethodVerifier::CheckNewInstance(dex::TypeIndex idx) {
Andreas Gampe29e81422017-05-15 16:29:32 -07001310 if (UNLIKELY(idx.index_ >= dex_file_->GetHeader().type_ids_size_)) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08001311 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx.index_ << " (max "
jeffhaod5347e02012-03-22 17:25:05 -07001312 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001313 return false;
1314 }
1315 // We don't need the actual class, just a pointer to the class name.
Ian Rogers0571d352011-11-03 19:51:38 -07001316 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Andreas Gampe29e81422017-05-15 16:29:32 -07001317 if (UNLIKELY(descriptor[0] != 'L')) {
jeffhaod5347e02012-03-22 17:25:05 -07001318 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "can't call new-instance on type '" << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001319 return false;
Andreas Gampe29e81422017-05-15 16:29:32 -07001320 } else if (UNLIKELY(strcmp(descriptor, "Ljava/lang/Class;") == 0)) {
Aart Bikdb698f12016-07-25 17:52:22 -07001321 // An unlikely new instance on Class is not allowed. Fall back to interpreter to ensure an
1322 // exception is thrown when this statement is executed (compiled code would not do that).
1323 Fail(VERIFY_ERROR_INSTANTIATION);
Ian Rogersd81871c2011-10-03 13:57:23 -07001324 }
1325 return true;
1326}
1327
Orion Hodsoncfa325e2016-10-13 10:25:54 +01001328inline bool MethodVerifier::CheckPrototypeIndex(uint32_t idx) {
Andreas Gampe29e81422017-05-15 16:29:32 -07001329 if (UNLIKELY(idx >= dex_file_->GetHeader().proto_ids_size_)) {
Orion Hodsoncfa325e2016-10-13 10:25:54 +01001330 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad prototype index " << idx << " (max "
1331 << dex_file_->GetHeader().proto_ids_size_ << ")";
1332 return false;
1333 }
1334 return true;
1335}
1336
Ian Rogers7b078e82014-09-10 14:44:24 -07001337inline bool MethodVerifier::CheckStringIndex(uint32_t idx) {
Andreas Gampe29e81422017-05-15 16:29:32 -07001338 if (UNLIKELY(idx >= dex_file_->GetHeader().string_ids_size_)) {
jeffhaod5347e02012-03-22 17:25:05 -07001339 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad string index " << idx << " (max "
1340 << dex_file_->GetHeader().string_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001341 return false;
1342 }
1343 return true;
1344}
1345
Andreas Gampea5b09a62016-11-17 15:21:22 -08001346inline bool MethodVerifier::CheckTypeIndex(dex::TypeIndex idx) {
Andreas Gampe29e81422017-05-15 16:29:32 -07001347 if (UNLIKELY(idx.index_ >= dex_file_->GetHeader().type_ids_size_)) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08001348 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx.index_ << " (max "
jeffhaod5347e02012-03-22 17:25:05 -07001349 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001350 return false;
1351 }
1352 return true;
1353}
1354
Andreas Gampea5b09a62016-11-17 15:21:22 -08001355bool MethodVerifier::CheckNewArray(dex::TypeIndex idx) {
Andreas Gampe29e81422017-05-15 16:29:32 -07001356 if (UNLIKELY(idx.index_ >= dex_file_->GetHeader().type_ids_size_)) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08001357 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx.index_ << " (max "
jeffhaod5347e02012-03-22 17:25:05 -07001358 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001359 return false;
1360 }
1361 int bracket_count = 0;
Ian Rogers0571d352011-11-03 19:51:38 -07001362 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -07001363 const char* cp = descriptor;
1364 while (*cp++ == '[') {
1365 bracket_count++;
1366 }
Andreas Gampe29e81422017-05-15 16:29:32 -07001367 if (UNLIKELY(bracket_count == 0)) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001368 /* The given class must be an array type. */
Brian Carlstrom93c33962013-07-26 10:37:43 -07001369 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1370 << "can't new-array class '" << descriptor << "' (not an array)";
Ian Rogersd81871c2011-10-03 13:57:23 -07001371 return false;
Andreas Gampe29e81422017-05-15 16:29:32 -07001372 } else if (UNLIKELY(bracket_count > 255)) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001373 /* It is illegal to create an array of more than 255 dimensions. */
Brian Carlstrom93c33962013-07-26 10:37:43 -07001374 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1375 << "can't new-array class '" << descriptor << "' (exceeds limit)";
Ian Rogersd81871c2011-10-03 13:57:23 -07001376 return false;
1377 }
1378 return true;
1379}
1380
Ian Rogers776ac1f2012-04-13 23:36:36 -07001381bool MethodVerifier::CheckArrayData(uint32_t cur_offset) {
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001382 const uint32_t insn_count = code_item_accessor_.InsnsSizeInCodeUnits();
1383 const uint16_t* insns = code_item_accessor_.Insns() + cur_offset;
Ian Rogersd81871c2011-10-03 13:57:23 -07001384 const uint16_t* array_data;
1385 int32_t array_data_offset;
1386
1387 DCHECK_LT(cur_offset, insn_count);
1388 /* make sure the start of the array data table is in range */
Andreas Gampe53de99c2015-08-17 13:43:55 -07001389 array_data_offset = insns[1] | (static_cast<int32_t>(insns[2]) << 16);
Andreas Gampe29e81422017-05-15 16:29:32 -07001390 if (UNLIKELY(static_cast<int32_t>(cur_offset) + array_data_offset < 0 ||
1391 cur_offset + array_data_offset + 2 >= insn_count)) {
jeffhaod5347e02012-03-22 17:25:05 -07001392 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data start: at " << cur_offset
Brian Carlstrom93c33962013-07-26 10:37:43 -07001393 << ", data offset " << array_data_offset
1394 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -07001395 return false;
1396 }
1397 /* offset to array data table is a relative branch-style offset */
1398 array_data = insns + array_data_offset;
Andreas Gampe57c47582015-07-01 22:05:59 -07001399 // Make sure the table is at an even dex pc, that is, 32-bit aligned.
Andreas Gampe29e81422017-05-15 16:29:32 -07001400 if (UNLIKELY(!IsAligned<4>(array_data))) {
jeffhaod5347e02012-03-22 17:25:05 -07001401 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned array data table: at " << cur_offset
1402 << ", data offset " << array_data_offset;
Ian Rogersd81871c2011-10-03 13:57:23 -07001403 return false;
1404 }
Andreas Gampe57c47582015-07-01 22:05:59 -07001405 // Make sure the array-data is marked as an opcode. This ensures that it was reached when
1406 // traversing the code item linearly. It is an approximation for a by-spec padding value.
Andreas Gampe29e81422017-05-15 16:29:32 -07001407 if (UNLIKELY(!GetInstructionFlags(cur_offset + array_data_offset).IsOpcode())) {
Andreas Gampe57c47582015-07-01 22:05:59 -07001408 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array data table at " << cur_offset
1409 << ", data offset " << array_data_offset
1410 << " not correctly visited, probably bad padding.";
1411 return false;
1412 }
1413
Ian Rogersd81871c2011-10-03 13:57:23 -07001414 uint32_t value_width = array_data[1];
Elliott Hughes398f64b2012-03-26 18:05:48 -07001415 uint32_t value_count = *reinterpret_cast<const uint32_t*>(&array_data[2]);
Ian Rogersd81871c2011-10-03 13:57:23 -07001416 uint32_t table_size = 4 + (value_width * value_count + 1) / 2;
1417 /* make sure the end of the switch is in range */
Andreas Gampe29e81422017-05-15 16:29:32 -07001418 if (UNLIKELY(cur_offset + array_data_offset + table_size > insn_count)) {
jeffhaod5347e02012-03-22 17:25:05 -07001419 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data end: at " << cur_offset
1420 << ", data offset " << array_data_offset << ", end "
1421 << cur_offset + array_data_offset + table_size
1422 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -07001423 return false;
1424 }
1425 return true;
1426}
1427
Ian Rogers776ac1f2012-04-13 23:36:36 -07001428bool MethodVerifier::CheckBranchTarget(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001429 int32_t offset;
1430 bool isConditional, selfOkay;
1431 if (!GetBranchOffset(cur_offset, &offset, &isConditional, &selfOkay)) {
1432 return false;
1433 }
Andreas Gampe29e81422017-05-15 16:29:32 -07001434 if (UNLIKELY(!selfOkay && offset == 0)) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001435 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch offset of zero not allowed at"
1436 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -07001437 return false;
1438 }
Elliott Hughes81ff3182012-03-23 20:35:56 -07001439 // Check for 32-bit overflow. This isn't strictly necessary if we can depend on the runtime
1440 // to have identical "wrap-around" behavior, but it's unwise to depend on that.
Andreas Gampe29e81422017-05-15 16:29:32 -07001441 if (UNLIKELY(((int64_t) cur_offset + (int64_t) offset) != (int64_t) (cur_offset + offset))) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001442 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch target overflow "
1443 << reinterpret_cast<void*>(cur_offset) << " +" << offset;
Ian Rogersd81871c2011-10-03 13:57:23 -07001444 return false;
1445 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001446 int32_t abs_offset = cur_offset + offset;
Andreas Gampe29e81422017-05-15 16:29:32 -07001447 if (UNLIKELY(abs_offset < 0 ||
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001448 (uint32_t) abs_offset >= code_item_accessor_.InsnsSizeInCodeUnits() ||
Andreas Gampe29e81422017-05-15 16:29:32 -07001449 !GetInstructionFlags(abs_offset).IsOpcode())) {
jeffhaod5347e02012-03-22 17:25:05 -07001450 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid branch target " << offset << " (-> "
Elliott Hughes398f64b2012-03-26 18:05:48 -07001451 << reinterpret_cast<void*>(abs_offset) << ") at "
1452 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -07001453 return false;
1454 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07001455 GetInstructionFlags(abs_offset).SetBranchTarget();
Ian Rogersd81871c2011-10-03 13:57:23 -07001456 return true;
1457}
1458
Ian Rogers776ac1f2012-04-13 23:36:36 -07001459bool MethodVerifier::GetBranchOffset(uint32_t cur_offset, int32_t* pOffset, bool* pConditional,
Ian Rogersd81871c2011-10-03 13:57:23 -07001460 bool* selfOkay) {
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001461 const uint16_t* insns = code_item_accessor_.Insns() + cur_offset;
Ian Rogersd81871c2011-10-03 13:57:23 -07001462 *pConditional = false;
1463 *selfOkay = false;
jeffhaoba5ebb92011-08-25 17:24:37 -07001464 switch (*insns & 0xff) {
1465 case Instruction::GOTO:
1466 *pOffset = ((int16_t) *insns) >> 8;
jeffhaoba5ebb92011-08-25 17:24:37 -07001467 break;
1468 case Instruction::GOTO_32:
1469 *pOffset = insns[1] | (((uint32_t) insns[2]) << 16);
jeffhaoba5ebb92011-08-25 17:24:37 -07001470 *selfOkay = true;
1471 break;
1472 case Instruction::GOTO_16:
1473 *pOffset = (int16_t) insns[1];
jeffhaoba5ebb92011-08-25 17:24:37 -07001474 break;
1475 case Instruction::IF_EQ:
1476 case Instruction::IF_NE:
1477 case Instruction::IF_LT:
1478 case Instruction::IF_GE:
1479 case Instruction::IF_GT:
1480 case Instruction::IF_LE:
1481 case Instruction::IF_EQZ:
1482 case Instruction::IF_NEZ:
1483 case Instruction::IF_LTZ:
1484 case Instruction::IF_GEZ:
1485 case Instruction::IF_GTZ:
1486 case Instruction::IF_LEZ:
1487 *pOffset = (int16_t) insns[1];
1488 *pConditional = true;
jeffhaoba5ebb92011-08-25 17:24:37 -07001489 break;
1490 default:
1491 return false;
jeffhaoba5ebb92011-08-25 17:24:37 -07001492 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001493 return true;
1494}
1495
Ian Rogers776ac1f2012-04-13 23:36:36 -07001496bool MethodVerifier::CheckSwitchTargets(uint32_t cur_offset) {
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001497 const uint32_t insn_count = code_item_accessor_.InsnsSizeInCodeUnits();
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001498 DCHECK_LT(cur_offset, insn_count);
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001499 const uint16_t* insns = code_item_accessor_.Insns() + cur_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001500 /* make sure the start of the switch is in range */
Andreas Gampe53de99c2015-08-17 13:43:55 -07001501 int32_t switch_offset = insns[1] | (static_cast<int32_t>(insns[2]) << 16);
Andreas Gampe29e81422017-05-15 16:29:32 -07001502 if (UNLIKELY(static_cast<int32_t>(cur_offset) + switch_offset < 0 ||
1503 cur_offset + switch_offset + 2 > insn_count)) {
jeffhaod5347e02012-03-22 17:25:05 -07001504 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch start: at " << cur_offset
Brian Carlstrom93c33962013-07-26 10:37:43 -07001505 << ", switch offset " << switch_offset
1506 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001507 return false;
1508 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001509 /* offset to switch table is a relative branch-style offset */
Ian Rogersd81871c2011-10-03 13:57:23 -07001510 const uint16_t* switch_insns = insns + switch_offset;
Andreas Gampe57c47582015-07-01 22:05:59 -07001511 // Make sure the table is at an even dex pc, that is, 32-bit aligned.
Andreas Gampe29e81422017-05-15 16:29:32 -07001512 if (UNLIKELY(!IsAligned<4>(switch_insns))) {
jeffhaod5347e02012-03-22 17:25:05 -07001513 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned switch table: at " << cur_offset
1514 << ", switch offset " << switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001515 return false;
1516 }
Andreas Gampe57c47582015-07-01 22:05:59 -07001517 // Make sure the switch data is marked as an opcode. This ensures that it was reached when
1518 // traversing the code item linearly. It is an approximation for a by-spec padding value.
Andreas Gampe29e81422017-05-15 16:29:32 -07001519 if (UNLIKELY(!GetInstructionFlags(cur_offset + switch_offset).IsOpcode())) {
Andreas Gampe57c47582015-07-01 22:05:59 -07001520 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "switch table at " << cur_offset
1521 << ", switch offset " << switch_offset
1522 << " not correctly visited, probably bad padding.";
1523 return false;
1524 }
1525
David Brazdil5469d342015-09-25 16:57:53 +01001526 bool is_packed_switch = (*insns & 0xff) == Instruction::PACKED_SWITCH;
1527
Ian Rogersd81871c2011-10-03 13:57:23 -07001528 uint32_t switch_count = switch_insns[1];
David Brazdil5469d342015-09-25 16:57:53 +01001529 int32_t targets_offset;
Ian Rogersd81871c2011-10-03 13:57:23 -07001530 uint16_t expected_signature;
David Brazdil5469d342015-09-25 16:57:53 +01001531 if (is_packed_switch) {
jeffhaoba5ebb92011-08-25 17:24:37 -07001532 /* 0=sig, 1=count, 2/3=firstKey */
1533 targets_offset = 4;
jeffhaoba5ebb92011-08-25 17:24:37 -07001534 expected_signature = Instruction::kPackedSwitchSignature;
1535 } else {
1536 /* 0=sig, 1=count, 2..count*2 = keys */
jeffhaoba5ebb92011-08-25 17:24:37 -07001537 targets_offset = 2 + 2 * switch_count;
1538 expected_signature = Instruction::kSparseSwitchSignature;
1539 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001540 uint32_t table_size = targets_offset + switch_count * 2;
Andreas Gampe29e81422017-05-15 16:29:32 -07001541 if (UNLIKELY(switch_insns[0] != expected_signature)) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001542 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1543 << StringPrintf("wrong signature for switch table (%x, wanted %x)",
1544 switch_insns[0], expected_signature);
jeffhaoba5ebb92011-08-25 17:24:37 -07001545 return false;
1546 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001547 /* make sure the end of the switch is in range */
Andreas Gampe29e81422017-05-15 16:29:32 -07001548 if (UNLIKELY(cur_offset + switch_offset + table_size > (uint32_t) insn_count)) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001549 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch end: at " << cur_offset
1550 << ", switch offset " << switch_offset
1551 << ", end " << (cur_offset + switch_offset + table_size)
jeffhaod5347e02012-03-22 17:25:05 -07001552 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001553 return false;
1554 }
David Brazdil5469d342015-09-25 16:57:53 +01001555
1556 constexpr int32_t keys_offset = 2;
1557 if (switch_count > 1) {
1558 if (is_packed_switch) {
1559 /* for a packed switch, verify that keys do not overflow int32 */
1560 int32_t first_key = switch_insns[keys_offset] | (switch_insns[keys_offset + 1] << 16);
1561 int32_t max_first_key =
1562 std::numeric_limits<int32_t>::max() - (static_cast<int32_t>(switch_count) - 1);
Andreas Gampe29e81422017-05-15 16:29:32 -07001563 if (UNLIKELY(first_key > max_first_key)) {
David Brazdil5469d342015-09-25 16:57:53 +01001564 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid packed switch: first_key=" << first_key
1565 << ", switch_count=" << switch_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001566 return false;
1567 }
David Brazdil5469d342015-09-25 16:57:53 +01001568 } else {
1569 /* for a sparse switch, verify the keys are in ascending order */
1570 int32_t last_key = switch_insns[keys_offset] | (switch_insns[keys_offset + 1] << 16);
1571 for (uint32_t targ = 1; targ < switch_count; targ++) {
1572 int32_t key =
1573 static_cast<int32_t>(switch_insns[keys_offset + targ * 2]) |
1574 static_cast<int32_t>(switch_insns[keys_offset + targ * 2 + 1] << 16);
Andreas Gampe29e81422017-05-15 16:29:32 -07001575 if (UNLIKELY(key <= last_key)) {
David Brazdil5469d342015-09-25 16:57:53 +01001576 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid sparse switch: last key=" << last_key
1577 << ", this=" << key;
1578 return false;
1579 }
1580 last_key = key;
1581 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001582 }
1583 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001584 /* verify each switch target */
Ian Rogersd81871c2011-10-03 13:57:23 -07001585 for (uint32_t targ = 0; targ < switch_count; targ++) {
Andreas Gampe53de99c2015-08-17 13:43:55 -07001586 int32_t offset = static_cast<int32_t>(switch_insns[targets_offset + targ * 2]) |
1587 static_cast<int32_t>(switch_insns[targets_offset + targ * 2 + 1] << 16);
Ian Rogersd81871c2011-10-03 13:57:23 -07001588 int32_t abs_offset = cur_offset + offset;
Andreas Gampe29e81422017-05-15 16:29:32 -07001589 if (UNLIKELY(abs_offset < 0 ||
1590 abs_offset >= static_cast<int32_t>(insn_count) ||
1591 !GetInstructionFlags(abs_offset).IsOpcode())) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001592 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch target " << offset
1593 << " (-> " << reinterpret_cast<void*>(abs_offset) << ") at "
1594 << reinterpret_cast<void*>(cur_offset)
1595 << "[" << targ << "]";
jeffhaoba5ebb92011-08-25 17:24:37 -07001596 return false;
1597 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07001598 GetInstructionFlags(abs_offset).SetBranchTarget();
Ian Rogersd81871c2011-10-03 13:57:23 -07001599 }
1600 return true;
1601}
1602
Ian Rogers776ac1f2012-04-13 23:36:36 -07001603bool MethodVerifier::CheckVarArgRegs(uint32_t vA, uint32_t arg[]) {
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001604 uint16_t registers_size = code_item_accessor_.RegistersSize();
Ian Rogersd81871c2011-10-03 13:57:23 -07001605 for (uint32_t idx = 0; idx < vA; idx++) {
Andreas Gampe29e81422017-05-15 16:29:32 -07001606 if (UNLIKELY(arg[idx] >= registers_size)) {
jeffhaod5347e02012-03-22 17:25:05 -07001607 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index (" << arg[idx]
1608 << ") in non-range invoke (>= " << registers_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001609 return false;
1610 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001611 }
1612
1613 return true;
1614}
1615
Ian Rogers776ac1f2012-04-13 23:36:36 -07001616bool MethodVerifier::CheckVarArgRangeRegs(uint32_t vA, uint32_t vC) {
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001617 uint16_t registers_size = code_item_accessor_.RegistersSize();
Ian Rogersd81871c2011-10-03 13:57:23 -07001618 // vA/vC are unsigned 8-bit/16-bit quantities for /range instructions, so there's no risk of
1619 // integer overflow when adding them here.
Andreas Gampe29e81422017-05-15 16:29:32 -07001620 if (UNLIKELY(vA + vC > registers_size)) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001621 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index " << vA << "+" << vC
1622 << " in range invoke (> " << registers_size << ")";
jeffhaoba5ebb92011-08-25 17:24:37 -07001623 return false;
1624 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001625 return true;
1626}
1627
Ian Rogers776ac1f2012-04-13 23:36:36 -07001628bool MethodVerifier::VerifyCodeFlow() {
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001629 const uint16_t registers_size = code_item_accessor_.RegistersSize();
jeffhaobdb76512011-09-07 11:43:16 -07001630
Ian Rogersd81871c2011-10-03 13:57:23 -07001631 /* Create and initialize table holding register status */
Brian Carlstrom93c33962013-07-26 10:37:43 -07001632 reg_table_.Init(kTrackCompilerInterestPoints,
1633 insn_flags_.get(),
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001634 code_item_accessor_.InsnsSizeInCodeUnits(),
Brian Carlstrom93c33962013-07-26 10:37:43 -07001635 registers_size,
1636 this);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07001637
Ian Rogersd0fbd852013-09-24 18:17:04 -07001638 work_line_.reset(RegisterLine::Create(registers_size, this));
1639 saved_line_.reset(RegisterLine::Create(registers_size, this));
jeffhaobdb76512011-09-07 11:43:16 -07001640
Ian Rogersd81871c2011-10-03 13:57:23 -07001641 /* Initialize register types of method arguments. */
1642 if (!SetTypesFromSignature()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001643 DCHECK_NE(failures_.size(), 0U);
1644 std::string prepend("Bad signature in ");
David Sehr709b0702016-10-13 09:12:37 -07001645 prepend += dex_file_->PrettyMethod(dex_method_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001646 PrependToLastFailMessage(prepend);
Ian Rogersd81871c2011-10-03 13:57:23 -07001647 return false;
1648 }
Andreas Gamped5ad72f2015-06-26 17:33:47 -07001649 // We may have a runtime failure here, clear.
1650 have_pending_runtime_throw_failure_ = false;
1651
Ian Rogersd81871c2011-10-03 13:57:23 -07001652 /* Perform code flow verification. */
1653 if (!CodeFlowVerifyMethod()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001654 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -07001655 return false;
jeffhaobdb76512011-09-07 11:43:16 -07001656 }
jeffhaobdb76512011-09-07 11:43:16 -07001657 return true;
1658}
1659
Ian Rogersad0b3a32012-04-16 14:50:24 -07001660std::ostream& MethodVerifier::DumpFailures(std::ostream& os) {
1661 DCHECK_EQ(failures_.size(), failure_messages_.size());
Jeff Hao4137f482013-11-22 11:44:57 -08001662 for (size_t i = 0; i < failures_.size(); ++i) {
1663 os << failure_messages_[i]->str() << "\n";
Ian Rogersad0b3a32012-04-16 14:50:24 -07001664 }
1665 return os;
1666}
1667
Ian Rogers776ac1f2012-04-13 23:36:36 -07001668void MethodVerifier::Dump(std::ostream& os) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001669 VariableIndentationOutputStream vios(&os);
1670 Dump(&vios);
1671}
1672
1673void MethodVerifier::Dump(VariableIndentationOutputStream* vios) {
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001674 if (!code_item_accessor_.HasCodeItem()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001675 vios->Stream() << "Native method\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001676 return;
jeffhaobdb76512011-09-07 11:43:16 -07001677 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001678 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001679 vios->Stream() << "Register Types:\n";
1680 ScopedIndentation indent1(vios);
1681 reg_types_.Dump(vios->Stream());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001682 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001683 vios->Stream() << "Dumping instructions and register lines:\n";
1684 ScopedIndentation indent1(vios);
Mathieu Chartier1d2d4ff2017-09-23 16:11:06 -07001685
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001686 for (const DexInstructionPcPair& inst : code_item_accessor_) {
Mathieu Chartier2b2bef22017-10-26 17:10:19 -07001687 const size_t dex_pc = inst.DexPc();
Ian Rogersd81871c2011-10-03 13:57:23 -07001688 RegisterLine* reg_line = reg_table_.GetLine(dex_pc);
Ian Rogers7b078e82014-09-10 14:44:24 -07001689 if (reg_line != nullptr) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001690 vios->Stream() << reg_line->Dump(this) << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07001691 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001692 vios->Stream()
Mathieu Chartierde40d472015-10-15 17:47:48 -07001693 << StringPrintf("0x%04zx", dex_pc) << ": " << GetInstructionFlags(dex_pc).ToString() << " ";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001694 const bool kDumpHexOfInstruction = false;
1695 if (kDumpHexOfInstruction) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001696 vios->Stream() << inst->DumpHex(5) << " ";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001697 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001698 vios->Stream() << inst->DumpString(dex_file_) << "\n";
jeffhaoba5ebb92011-08-25 17:24:37 -07001699 }
jeffhaobdb76512011-09-07 11:43:16 -07001700}
1701
Ian Rogersd81871c2011-10-03 13:57:23 -07001702static bool IsPrimitiveDescriptor(char descriptor) {
1703 switch (descriptor) {
jeffhaobdb76512011-09-07 11:43:16 -07001704 case 'I':
1705 case 'C':
1706 case 'S':
1707 case 'B':
1708 case 'Z':
jeffhaobdb76512011-09-07 11:43:16 -07001709 case 'F':
1710 case 'D':
1711 case 'J':
Ian Rogersd81871c2011-10-03 13:57:23 -07001712 return true;
jeffhaobdb76512011-09-07 11:43:16 -07001713 default:
1714 return false;
1715 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001716}
1717
Ian Rogers776ac1f2012-04-13 23:36:36 -07001718bool MethodVerifier::SetTypesFromSignature() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001719 RegisterLine* reg_line = reg_table_.GetLine(0);
Andreas Gampeef0b1a12015-06-19 20:37:46 -07001720
1721 // Should have been verified earlier.
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001722 DCHECK_GE(code_item_accessor_.RegistersSize(), code_item_accessor_.InsSize());
Andreas Gampeef0b1a12015-06-19 20:37:46 -07001723
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001724 uint32_t arg_start = code_item_accessor_.RegistersSize() - code_item_accessor_.InsSize();
1725 size_t expected_args = code_item_accessor_.InsSize(); /* long/double count as two */
jeffhaobdb76512011-09-07 11:43:16 -07001726
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001727 // Include the "this" pointer.
Ian Rogersd81871c2011-10-03 13:57:23 -07001728 size_t cur_arg = 0;
Ian Rogersad0b3a32012-04-16 14:50:24 -07001729 if (!IsStatic()) {
Andreas Gampeef0b1a12015-06-19 20:37:46 -07001730 if (expected_args == 0) {
1731 // Expect at least a receiver.
1732 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected 0 args, but method is not static";
1733 return false;
1734 }
1735
Ian Rogersd81871c2011-10-03 13:57:23 -07001736 // If this is a constructor for a class other than java.lang.Object, mark the first ("this")
1737 // argument as uninitialized. This restricts field access until the superclass constructor is
1738 // called.
Ian Rogersd8f69b02014-09-10 21:43:52 +00001739 const RegType& declaring_class = GetDeclaringClass();
Andreas Gampef10b6e12015-08-12 10:48:12 -07001740 if (IsConstructor()) {
1741 if (declaring_class.IsJavaLangObject()) {
1742 // "this" is implicitly initialized.
1743 reg_line->SetThisInitialized();
Andreas Gampead238ce2015-08-24 21:13:08 -07001744 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, declaring_class);
Andreas Gampef10b6e12015-08-12 10:48:12 -07001745 } else {
Andreas Gampead238ce2015-08-24 21:13:08 -07001746 reg_line->SetRegisterType<LockOp::kClear>(
1747 this,
1748 arg_start + cur_arg,
1749 reg_types_.UninitializedThisArgument(declaring_class));
Andreas Gampef10b6e12015-08-12 10:48:12 -07001750 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001751 } else {
Andreas Gampead238ce2015-08-24 21:13:08 -07001752 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, declaring_class);
jeffhaobdb76512011-09-07 11:43:16 -07001753 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001754 cur_arg++;
jeffhaobdb76512011-09-07 11:43:16 -07001755 }
1756
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001757 const DexFile::ProtoId& proto_id =
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001758 dex_file_->GetMethodPrototype(dex_file_->GetMethodId(dex_method_idx_));
Ian Rogers0571d352011-11-03 19:51:38 -07001759 DexFileParameterIterator iterator(*dex_file_, proto_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07001760
1761 for (; iterator.HasNext(); iterator.Next()) {
1762 const char* descriptor = iterator.GetDescriptor();
Ian Rogers7b078e82014-09-10 14:44:24 -07001763 if (descriptor == nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001764 LOG(FATAL) << "Null descriptor";
1765 }
1766 if (cur_arg >= expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07001767 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1768 << " args, found more (" << descriptor << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001769 return false;
1770 }
1771 switch (descriptor[0]) {
1772 case 'L':
1773 case '[':
1774 // We assume that reference arguments are initialized. The only way it could be otherwise
1775 // (assuming the caller was verified) is if the current method is <init>, but in that case
1776 // it's effectively considered initialized the instant we reach here (in the sense that we
1777 // can return without doing anything or call virtual methods).
1778 {
Andreas Gampe98be1a92017-08-28 08:25:45 -07001779 // Note: don't check access. No error would be thrown for declaring or passing an
1780 // inaccessible class. Only actual accesses to fields or methods will.
1781 const RegType& reg_type = ResolveClass<CheckAccess::kNo>(iterator.GetTypeIdx());
Sebastien Hertz2ed76f92014-04-22 17:11:08 +02001782 if (!reg_type.IsNonZeroReferenceTypes()) {
1783 DCHECK(HasFailures());
1784 return false;
1785 }
Andreas Gampead238ce2015-08-24 21:13:08 -07001786 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001787 }
1788 break;
1789 case 'Z':
Andreas Gampead238ce2015-08-24 21:13:08 -07001790 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Boolean());
Ian Rogersd81871c2011-10-03 13:57:23 -07001791 break;
1792 case 'C':
Andreas Gampead238ce2015-08-24 21:13:08 -07001793 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Char());
Ian Rogersd81871c2011-10-03 13:57:23 -07001794 break;
1795 case 'B':
Andreas Gampead238ce2015-08-24 21:13:08 -07001796 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Byte());
Ian Rogersd81871c2011-10-03 13:57:23 -07001797 break;
1798 case 'I':
Andreas Gampead238ce2015-08-24 21:13:08 -07001799 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Integer());
Ian Rogersd81871c2011-10-03 13:57:23 -07001800 break;
1801 case 'S':
Andreas Gampead238ce2015-08-24 21:13:08 -07001802 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Short());
Ian Rogersd81871c2011-10-03 13:57:23 -07001803 break;
1804 case 'F':
Andreas Gampead238ce2015-08-24 21:13:08 -07001805 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Float());
Ian Rogersd81871c2011-10-03 13:57:23 -07001806 break;
1807 case 'J':
1808 case 'D': {
Andreas Gampe77cd4d62014-06-19 17:29:48 -07001809 if (cur_arg + 1 >= expected_args) {
1810 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1811 << " args, found more (" << descriptor << ")";
1812 return false;
1813 }
1814
Ian Rogers7b078e82014-09-10 14:44:24 -07001815 const RegType* lo_half;
1816 const RegType* hi_half;
1817 if (descriptor[0] == 'J') {
1818 lo_half = &reg_types_.LongLo();
1819 hi_half = &reg_types_.LongHi();
1820 } else {
1821 lo_half = &reg_types_.DoubleLo();
1822 hi_half = &reg_types_.DoubleHi();
1823 }
1824 reg_line->SetRegisterTypeWide(this, arg_start + cur_arg, *lo_half, *hi_half);
Ian Rogersd81871c2011-10-03 13:57:23 -07001825 cur_arg++;
1826 break;
1827 }
1828 default:
Brian Carlstrom93c33962013-07-26 10:37:43 -07001829 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected signature type char '"
1830 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001831 return false;
1832 }
1833 cur_arg++;
1834 }
1835 if (cur_arg != expected_args) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001836 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1837 << " arguments, found " << cur_arg;
Ian Rogersd81871c2011-10-03 13:57:23 -07001838 return false;
1839 }
1840 const char* descriptor = dex_file_->GetReturnTypeDescriptor(proto_id);
1841 // Validate return type. We don't do the type lookup; just want to make sure that it has the right
1842 // format. Only major difference from the method argument format is that 'V' is supported.
1843 bool result;
1844 if (IsPrimitiveDescriptor(descriptor[0]) || descriptor[0] == 'V') {
1845 result = descriptor[1] == '\0';
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001846 } else if (descriptor[0] == '[') { // single/multi-dimensional array of object/primitive
Ian Rogersd81871c2011-10-03 13:57:23 -07001847 size_t i = 0;
1848 do {
1849 i++;
1850 } while (descriptor[i] == '['); // process leading [
1851 if (descriptor[i] == 'L') { // object array
1852 do {
1853 i++; // find closing ;
1854 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1855 result = descriptor[i] == ';';
1856 } else { // primitive array
1857 result = IsPrimitiveDescriptor(descriptor[i]) && descriptor[i + 1] == '\0';
1858 }
1859 } else if (descriptor[0] == 'L') {
1860 // could be more thorough here, but shouldn't be required
1861 size_t i = 0;
1862 do {
1863 i++;
1864 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1865 result = descriptor[i] == ';';
1866 } else {
1867 result = false;
1868 }
1869 if (!result) {
jeffhaod5347e02012-03-22 17:25:05 -07001870 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected char in return type descriptor '"
1871 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001872 }
1873 return result;
jeffhaobdb76512011-09-07 11:43:16 -07001874}
1875
Ian Rogers776ac1f2012-04-13 23:36:36 -07001876bool MethodVerifier::CodeFlowVerifyMethod() {
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001877 const uint16_t* insns = code_item_accessor_.Insns();
1878 const uint32_t insns_size = code_item_accessor_.InsnsSizeInCodeUnits();
jeffhaoba5ebb92011-08-25 17:24:37 -07001879
jeffhaobdb76512011-09-07 11:43:16 -07001880 /* Begin by marking the first instruction as "changed". */
Mathieu Chartierde40d472015-10-15 17:47:48 -07001881 GetInstructionFlags(0).SetChanged();
Ian Rogersd81871c2011-10-03 13:57:23 -07001882 uint32_t start_guess = 0;
jeffhaoba5ebb92011-08-25 17:24:37 -07001883
jeffhaobdb76512011-09-07 11:43:16 -07001884 /* Continue until no instructions are marked "changed". */
1885 while (true) {
Mathieu Chartier4306ef82014-12-19 18:41:47 -08001886 if (allow_thread_suspension_) {
1887 self_->AllowThreadSuspension();
1888 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001889 // Find the first marked one. Use "start_guess" as a way to find one quickly.
1890 uint32_t insn_idx = start_guess;
1891 for (; insn_idx < insns_size; insn_idx++) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07001892 if (GetInstructionFlags(insn_idx).IsChanged())
jeffhaobdb76512011-09-07 11:43:16 -07001893 break;
1894 }
jeffhaobdb76512011-09-07 11:43:16 -07001895 if (insn_idx == insns_size) {
1896 if (start_guess != 0) {
1897 /* try again, starting from the top */
1898 start_guess = 0;
1899 continue;
1900 } else {
1901 /* all flags are clear */
1902 break;
1903 }
1904 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001905 // We carry the working set of registers from instruction to instruction. If this address can
1906 // be the target of a branch (or throw) instruction, or if we're skipping around chasing
1907 // "changed" flags, we need to load the set of registers from the table.
1908 // Because we always prefer to continue on to the next instruction, we should never have a
1909 // situation where we have a stray "changed" flag set on an instruction that isn't a branch
1910 // target.
1911 work_insn_idx_ = insn_idx;
Mathieu Chartierde40d472015-10-15 17:47:48 -07001912 if (GetInstructionFlags(insn_idx).IsBranchTarget()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001913 work_line_->CopyFromLine(reg_table_.GetLine(insn_idx));
Ian Rogersebbdd872014-07-07 23:53:08 -07001914 } else if (kIsDebugBuild) {
jeffhaobdb76512011-09-07 11:43:16 -07001915 /*
1916 * Sanity check: retrieve the stored register line (assuming
1917 * a full table) and make sure it actually matches.
1918 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001919 RegisterLine* register_line = reg_table_.GetLine(insn_idx);
Ian Rogers7b078e82014-09-10 14:44:24 -07001920 if (register_line != nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001921 if (work_line_->CompareLine(register_line) != 0) {
1922 Dump(std::cout);
1923 std::cout << info_messages_.str();
David Sehr709b0702016-10-13 09:12:37 -07001924 LOG(FATAL) << "work_line diverged in " << dex_file_->PrettyMethod(dex_method_idx_)
Elliott Hughesc073b072012-05-24 19:29:17 -07001925 << "@" << reinterpret_cast<void*>(work_insn_idx_) << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07001926 << " work_line=" << work_line_->Dump(this) << "\n"
1927 << " expected=" << register_line->Dump(this);
Ian Rogersd81871c2011-10-03 13:57:23 -07001928 }
jeffhaobdb76512011-09-07 11:43:16 -07001929 }
jeffhaobdb76512011-09-07 11:43:16 -07001930 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001931 if (!CodeFlowVerifyInstruction(&start_guess)) {
David Sehr709b0702016-10-13 09:12:37 -07001932 std::string prepend(dex_file_->PrettyMethod(dex_method_idx_));
Ian Rogersad0b3a32012-04-16 14:50:24 -07001933 prepend += " failed to verify: ";
1934 PrependToLastFailMessage(prepend);
jeffhaoba5ebb92011-08-25 17:24:37 -07001935 return false;
1936 }
jeffhaobdb76512011-09-07 11:43:16 -07001937 /* Clear "changed" and mark as visited. */
Mathieu Chartierde40d472015-10-15 17:47:48 -07001938 GetInstructionFlags(insn_idx).SetVisited();
1939 GetInstructionFlags(insn_idx).ClearChanged();
jeffhaobdb76512011-09-07 11:43:16 -07001940 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001941
Andreas Gampe92d77202017-12-06 20:49:00 -08001942 if (UNLIKELY(VLOG_IS_ON(verifier_debug))) {
jeffhaobdb76512011-09-07 11:43:16 -07001943 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001944 * Scan for dead code. There's nothing "evil" about dead code
jeffhaobdb76512011-09-07 11:43:16 -07001945 * (besides the wasted space), but it indicates a flaw somewhere
1946 * down the line, possibly in the verifier.
1947 *
1948 * If we've substituted "always throw" instructions into the stream,
1949 * we are almost certainly going to have some dead code.
1950 */
1951 int dead_start = -1;
Mathieu Chartier1d2d4ff2017-09-23 16:11:06 -07001952
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001953 for (const DexInstructionPcPair& inst : code_item_accessor_) {
Mathieu Chartier2b2bef22017-10-26 17:10:19 -07001954 const uint32_t insn_idx = inst.DexPc();
jeffhaobdb76512011-09-07 11:43:16 -07001955 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001956 * Switch-statement data doesn't get "visited" by scanner. It
jeffhaobdb76512011-09-07 11:43:16 -07001957 * may or may not be preceded by a padding NOP (for alignment).
1958 */
1959 if (insns[insn_idx] == Instruction::kPackedSwitchSignature ||
1960 insns[insn_idx] == Instruction::kSparseSwitchSignature ||
1961 insns[insn_idx] == Instruction::kArrayDataSignature ||
Elliott Hughes380aaa72012-07-09 14:33:15 -07001962 (insns[insn_idx] == Instruction::NOP && (insn_idx + 1 < insns_size) &&
jeffhaobdb76512011-09-07 11:43:16 -07001963 (insns[insn_idx + 1] == Instruction::kPackedSwitchSignature ||
1964 insns[insn_idx + 1] == Instruction::kSparseSwitchSignature ||
1965 insns[insn_idx + 1] == Instruction::kArrayDataSignature))) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07001966 GetInstructionFlags(insn_idx).SetVisited();
jeffhaobdb76512011-09-07 11:43:16 -07001967 }
1968
Mathieu Chartierde40d472015-10-15 17:47:48 -07001969 if (!GetInstructionFlags(insn_idx).IsVisited()) {
Mathieu Chartier1d2d4ff2017-09-23 16:11:06 -07001970 if (dead_start < 0) {
jeffhaobdb76512011-09-07 11:43:16 -07001971 dead_start = insn_idx;
Mathieu Chartier1d2d4ff2017-09-23 16:11:06 -07001972 }
jeffhaobdb76512011-09-07 11:43:16 -07001973 } else if (dead_start >= 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001974 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start)
1975 << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaobdb76512011-09-07 11:43:16 -07001976 dead_start = -1;
1977 }
1978 }
1979 if (dead_start >= 0) {
Mathieu Chartier1d2d4ff2017-09-23 16:11:06 -07001980 LogVerifyInfo()
1981 << "dead code " << reinterpret_cast<void*>(dead_start)
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001982 << "-" << reinterpret_cast<void*>(code_item_accessor_.InsnsSizeInCodeUnits() - 1);
jeffhaoba5ebb92011-08-25 17:24:37 -07001983 }
Ian Rogersc9e463c2013-06-05 16:52:26 -07001984 // To dump the state of the verify after a method, do something like:
David Sehr709b0702016-10-13 09:12:37 -07001985 // if (dex_file_->PrettyMethod(dex_method_idx_) ==
Ian Rogersc9e463c2013-06-05 16:52:26 -07001986 // "boolean java.lang.String.equals(java.lang.Object)") {
1987 // LOG(INFO) << info_messages_.str();
1988 // }
jeffhaoba5ebb92011-08-25 17:24:37 -07001989 }
jeffhaobdb76512011-09-07 11:43:16 -07001990 return true;
1991}
1992
Andreas Gampe68df3202015-06-22 11:35:46 -07001993// Returns the index of the first final instance field of the given class, or kDexNoIndex if there
1994// is no such field.
Andreas Gampea5b09a62016-11-17 15:21:22 -08001995static uint32_t GetFirstFinalInstanceFieldIndex(const DexFile& dex_file, dex::TypeIndex type_idx) {
Andreas Gampe68df3202015-06-22 11:35:46 -07001996 const DexFile::ClassDef* class_def = dex_file.FindClassDef(type_idx);
1997 DCHECK(class_def != nullptr);
1998 const uint8_t* class_data = dex_file.GetClassData(*class_def);
1999 DCHECK(class_data != nullptr);
2000 ClassDataItemIterator it(dex_file, class_data);
Mathieu Chartiere17cf242017-06-19 11:05:51 -07002001 it.SkipStaticFields();
Andreas Gampe68df3202015-06-22 11:35:46 -07002002 while (it.HasNextInstanceField()) {
2003 if ((it.GetFieldAccessFlags() & kAccFinal) != 0) {
2004 return it.GetMemberIndex();
2005 }
2006 it.Next();
2007 }
Andreas Gampee2abbc62017-09-15 11:59:26 -07002008 return dex::kDexNoIndex;
Andreas Gampe68df3202015-06-22 11:35:46 -07002009}
2010
Andreas Gampea727e372015-08-25 09:22:37 -07002011// Setup a register line for the given return instruction.
2012static void AdjustReturnLine(MethodVerifier* verifier,
2013 const Instruction* ret_inst,
2014 RegisterLine* line) {
2015 Instruction::Code opcode = ret_inst->Opcode();
2016
2017 switch (opcode) {
2018 case Instruction::RETURN_VOID:
2019 case Instruction::RETURN_VOID_NO_BARRIER:
2020 SafelyMarkAllRegistersAsConflicts(verifier, line);
2021 break;
2022
2023 case Instruction::RETURN:
2024 case Instruction::RETURN_OBJECT:
2025 line->MarkAllRegistersAsConflictsExcept(verifier, ret_inst->VRegA_11x());
2026 break;
2027
2028 case Instruction::RETURN_WIDE:
2029 line->MarkAllRegistersAsConflictsExceptWide(verifier, ret_inst->VRegA_11x());
2030 break;
2031
2032 default:
2033 LOG(FATAL) << "Unknown return opcode " << opcode;
2034 UNREACHABLE();
2035 }
2036}
2037
Ian Rogers776ac1f2012-04-13 23:36:36 -07002038bool MethodVerifier::CodeFlowVerifyInstruction(uint32_t* start_guess) {
Elliott Hughes08fc03a2012-06-26 17:34:00 -07002039 // If we're doing FindLocksAtDexPc, check whether we're at the dex pc we care about.
2040 // We want the state _before_ the instruction, for the case where the dex pc we're
2041 // interested in is itself a monitor-enter instruction (which is a likely place
2042 // for a thread to be suspended).
Ian Rogers7b078e82014-09-10 14:44:24 -07002043 if (monitor_enter_dex_pcs_ != nullptr && work_insn_idx_ == interesting_dex_pc_) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -07002044 monitor_enter_dex_pcs_->clear(); // The new work line is more accurate than the previous one.
Andreas Gampeaaf0d382017-11-27 14:10:21 -08002045
2046 std::map<uint32_t, DexLockInfo> depth_to_lock_info;
2047 auto collector = [&](uint32_t dex_reg, uint32_t depth) {
2048 auto insert_pair = depth_to_lock_info.emplace(depth, DexLockInfo(depth));
2049 auto it = insert_pair.first;
2050 auto set_insert_pair = it->second.dex_registers.insert(dex_reg);
2051 DCHECK(set_insert_pair.second);
2052 };
2053 work_line_->IterateRegToLockDepths(collector);
2054 for (auto& pair : depth_to_lock_info) {
2055 monitor_enter_dex_pcs_->push_back(pair.second);
2056 // Map depth to dex PC.
2057 (*monitor_enter_dex_pcs_)[monitor_enter_dex_pcs_->size() - 1].dex_pc =
2058 work_line_->GetMonitorEnterDexPc(pair.second.dex_pc);
Elliott Hughes08fc03a2012-06-26 17:34:00 -07002059 }
2060 }
2061
jeffhaobdb76512011-09-07 11:43:16 -07002062 /*
2063 * Once we finish decoding the instruction, we need to figure out where
jeffhaod1f0fde2011-09-08 17:25:33 -07002064 * we can go from here. There are three possible ways to transfer
jeffhaobdb76512011-09-07 11:43:16 -07002065 * control to another statement:
2066 *
jeffhaod1f0fde2011-09-08 17:25:33 -07002067 * (1) Continue to the next instruction. Applies to all but
jeffhaobdb76512011-09-07 11:43:16 -07002068 * unconditional branches, method returns, and exception throws.
jeffhaod1f0fde2011-09-08 17:25:33 -07002069 * (2) Branch to one or more possible locations. Applies to branches
jeffhaobdb76512011-09-07 11:43:16 -07002070 * and switch statements.
jeffhaod1f0fde2011-09-08 17:25:33 -07002071 * (3) Exception handlers. Applies to any instruction that can
jeffhaobdb76512011-09-07 11:43:16 -07002072 * throw an exception that is handled by an encompassing "try"
2073 * block.
2074 *
2075 * We can also return, in which case there is no successor instruction
2076 * from this point.
2077 *
Elliott Hughesadb8c672012-03-06 16:49:32 -08002078 * The behavior can be determined from the opcode flags.
jeffhaobdb76512011-09-07 11:43:16 -07002079 */
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08002080 const uint16_t* insns = code_item_accessor_.Insns() + work_insn_idx_;
Ian Rogersd81871c2011-10-03 13:57:23 -07002081 const Instruction* inst = Instruction::At(insns);
Ian Rogersa75a0132012-09-28 11:41:42 -07002082 int opcode_flags = Instruction::FlagsOf(inst->Opcode());
jeffhaobdb76512011-09-07 11:43:16 -07002083
jeffhaobdb76512011-09-07 11:43:16 -07002084 int32_t branch_target = 0;
jeffhaobdb76512011-09-07 11:43:16 -07002085 bool just_set_result = false;
Andreas Gampe92d77202017-12-06 20:49:00 -08002086 if (UNLIKELY(VLOG_IS_ON(verifier_debug))) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002087 // Generate processing back trace to debug verifier
Elliott Hughesc073b072012-05-24 19:29:17 -07002088 LogVerifyInfo() << "Processing " << inst->DumpString(dex_file_) << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07002089 << work_line_->Dump(this) << "\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07002090 }
jeffhaobdb76512011-09-07 11:43:16 -07002091
2092 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002093 * Make a copy of the previous register state. If the instruction
jeffhaobdb76512011-09-07 11:43:16 -07002094 * can throw an exception, we will copy/merge this into the "catch"
2095 * address rather than work_line, because we don't want the result
2096 * from the "successful" code path (e.g. a check-cast that "improves"
2097 * a type) to be visible to the exception handler.
2098 */
Ian Rogers776ac1f2012-04-13 23:36:36 -07002099 if ((opcode_flags & Instruction::kThrow) != 0 && CurrentInsnFlags()->IsInTry()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002100 saved_line_->CopyFromLine(work_line_.get());
Ian Rogers1ff3c982014-08-12 02:30:58 -07002101 } else if (kIsDebugBuild) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002102 saved_line_->FillWithGarbage();
jeffhaobdb76512011-09-07 11:43:16 -07002103 }
Andreas Gamped12e7822015-06-25 10:26:40 -07002104 DCHECK(!have_pending_runtime_throw_failure_); // Per-instruction flag, should not be set here.
jeffhaobdb76512011-09-07 11:43:16 -07002105
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07002106
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002107 // We need to ensure the work line is consistent while performing validation. When we spot a
2108 // peephole pattern we compute a new line for either the fallthrough instruction or the
2109 // branch target.
Mathieu Chartier361e04a2016-02-16 14:06:35 -08002110 RegisterLineArenaUniquePtr branch_line;
2111 RegisterLineArenaUniquePtr fallthrough_line;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002112
Sebastien Hertz5243e912013-05-21 10:55:07 +02002113 switch (inst->Opcode()) {
jeffhaobdb76512011-09-07 11:43:16 -07002114 case Instruction::NOP:
2115 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002116 * A "pure" NOP has no effect on anything. Data tables start with
jeffhaobdb76512011-09-07 11:43:16 -07002117 * a signature that looks like a NOP; if we see one of these in
2118 * the course of executing code then we have a problem.
2119 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002120 if (inst->VRegA_10x() != 0) {
jeffhaod5347e02012-03-22 17:25:05 -07002121 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "encountered data table in instruction stream";
jeffhaobdb76512011-09-07 11:43:16 -07002122 }
2123 break;
2124
2125 case Instruction::MOVE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002126 work_line_->CopyRegister1(this, inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategory1nr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02002127 break;
jeffhaobdb76512011-09-07 11:43:16 -07002128 case Instruction::MOVE_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07002129 work_line_->CopyRegister1(this, inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategory1nr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02002130 break;
jeffhaobdb76512011-09-07 11:43:16 -07002131 case Instruction::MOVE_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07002132 work_line_->CopyRegister1(this, inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategory1nr);
jeffhaobdb76512011-09-07 11:43:16 -07002133 break;
2134 case Instruction::MOVE_WIDE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002135 work_line_->CopyRegister2(this, inst->VRegA_12x(), inst->VRegB_12x());
Sebastien Hertz5243e912013-05-21 10:55:07 +02002136 break;
jeffhaobdb76512011-09-07 11:43:16 -07002137 case Instruction::MOVE_WIDE_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07002138 work_line_->CopyRegister2(this, inst->VRegA_22x(), inst->VRegB_22x());
Sebastien Hertz5243e912013-05-21 10:55:07 +02002139 break;
jeffhaobdb76512011-09-07 11:43:16 -07002140 case Instruction::MOVE_WIDE_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07002141 work_line_->CopyRegister2(this, inst->VRegA_32x(), inst->VRegB_32x());
jeffhaobdb76512011-09-07 11:43:16 -07002142 break;
2143 case Instruction::MOVE_OBJECT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002144 work_line_->CopyRegister1(this, inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategoryRef);
Sebastien Hertz5243e912013-05-21 10:55:07 +02002145 break;
jeffhaobdb76512011-09-07 11:43:16 -07002146 case Instruction::MOVE_OBJECT_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07002147 work_line_->CopyRegister1(this, inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategoryRef);
Sebastien Hertz5243e912013-05-21 10:55:07 +02002148 break;
jeffhaobdb76512011-09-07 11:43:16 -07002149 case Instruction::MOVE_OBJECT_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07002150 work_line_->CopyRegister1(this, inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategoryRef);
jeffhaobdb76512011-09-07 11:43:16 -07002151 break;
2152
2153 /*
2154 * The move-result instructions copy data out of a "pseudo-register"
jeffhaod1f0fde2011-09-08 17:25:33 -07002155 * with the results from the last method invocation. In practice we
jeffhaobdb76512011-09-07 11:43:16 -07002156 * might want to hold the result in an actual CPU register, so the
2157 * Dalvik spec requires that these only appear immediately after an
2158 * invoke or filled-new-array.
2159 *
jeffhaod1f0fde2011-09-08 17:25:33 -07002160 * These calls invalidate the "result" register. (This is now
jeffhaobdb76512011-09-07 11:43:16 -07002161 * redundant with the reset done below, but it can make the debug info
2162 * easier to read in some cases.)
2163 */
2164 case Instruction::MOVE_RESULT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002165 work_line_->CopyResultRegister1(this, inst->VRegA_11x(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002166 break;
2167 case Instruction::MOVE_RESULT_WIDE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002168 work_line_->CopyResultRegister2(this, inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07002169 break;
2170 case Instruction::MOVE_RESULT_OBJECT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002171 work_line_->CopyResultRegister1(this, inst->VRegA_11x(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002172 break;
2173
Ian Rogersd81871c2011-10-03 13:57:23 -07002174 case Instruction::MOVE_EXCEPTION: {
Sebastien Hertz270a0e12015-01-16 19:49:09 +01002175 // We do not allow MOVE_EXCEPTION as the first instruction in a method. This is a simple case
2176 // where one entrypoint to the catch block is not actually an exception path.
2177 if (work_insn_idx_ == 0) {
2178 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "move-exception at pc 0x0";
2179 break;
2180 }
jeffhaobdb76512011-09-07 11:43:16 -07002181 /*
jeffhao60f83e32012-02-13 17:16:30 -08002182 * This statement can only appear as the first instruction in an exception handler. We verify
2183 * that as part of extracting the exception type from the catch block list.
jeffhaobdb76512011-09-07 11:43:16 -07002184 */
Ian Rogersd8f69b02014-09-10 21:43:52 +00002185 const RegType& res_type = GetCaughtExceptionType();
Andreas Gampead238ce2015-08-24 21:13:08 -07002186 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_11x(), res_type);
jeffhaobdb76512011-09-07 11:43:16 -07002187 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002188 }
jeffhaobdb76512011-09-07 11:43:16 -07002189 case Instruction::RETURN_VOID:
Andreas Gampef10b6e12015-08-12 10:48:12 -07002190 if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002191 if (!GetMethodReturnType().IsConflict()) {
jeffhaod5347e02012-03-22 17:25:05 -07002192 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07002193 }
jeffhaobdb76512011-09-07 11:43:16 -07002194 }
2195 break;
2196 case Instruction::RETURN:
Andreas Gampef10b6e12015-08-12 10:48:12 -07002197 if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) {
jeffhaobdb76512011-09-07 11:43:16 -07002198 /* check the method signature */
Ian Rogersd8f69b02014-09-10 21:43:52 +00002199 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07002200 if (!return_type.IsCategory1Types()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002201 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected non-category 1 return type "
2202 << return_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002203 } else {
2204 // Compilers may generate synthetic functions that write byte values into boolean fields.
2205 // Also, it may use integer values for boolean, byte, short, and character return types.
Sebastien Hertz5243e912013-05-21 10:55:07 +02002206 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07002207 const RegType& src_type = work_line_->GetRegisterType(this, vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07002208 bool use_src = ((return_type.IsBoolean() && src_type.IsByte()) ||
2209 ((return_type.IsBoolean() || return_type.IsByte() ||
2210 return_type.IsShort() || return_type.IsChar()) &&
2211 src_type.IsInteger()));
2212 /* check the register contents */
Ian Rogersad0b3a32012-04-16 14:50:24 -07002213 bool success =
Ian Rogers7b078e82014-09-10 14:44:24 -07002214 work_line_->VerifyRegisterType(this, vregA, use_src ? src_type : return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002215 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002216 AppendToLastFailMessage(StringPrintf(" return-1nr on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07002217 }
jeffhaobdb76512011-09-07 11:43:16 -07002218 }
2219 }
2220 break;
2221 case Instruction::RETURN_WIDE:
Andreas Gampef10b6e12015-08-12 10:48:12 -07002222 if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) {
jeffhaobdb76512011-09-07 11:43:16 -07002223 /* check the method signature */
Ian Rogersd8f69b02014-09-10 21:43:52 +00002224 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07002225 if (!return_type.IsCategory2Types()) {
jeffhaod5347e02012-03-22 17:25:05 -07002226 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-wide not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07002227 } else {
2228 /* check the register contents */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002229 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07002230 bool success = work_line_->VerifyRegisterType(this, vregA, return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002231 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002232 AppendToLastFailMessage(StringPrintf(" return-wide on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07002233 }
jeffhaobdb76512011-09-07 11:43:16 -07002234 }
2235 }
2236 break;
2237 case Instruction::RETURN_OBJECT:
Andreas Gampef10b6e12015-08-12 10:48:12 -07002238 if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) {
Ian Rogersd8f69b02014-09-10 21:43:52 +00002239 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07002240 if (!return_type.IsReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002241 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-object not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07002242 } else {
2243 /* return_type is the *expected* return type, not register value */
Andreas Gampeeed3a5d2017-11-29 14:58:34 -08002244 DCHECK(!return_type.IsZeroOrNull());
Ian Rogersd81871c2011-10-03 13:57:23 -07002245 DCHECK(!return_type.IsUninitializedReference());
Sebastien Hertz5243e912013-05-21 10:55:07 +02002246 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07002247 const RegType& reg_type = work_line_->GetRegisterType(this, vregA);
Andreas Gampea32210c2015-06-24 10:26:13 -07002248 // Disallow returning undefined, conflict & uninitialized values and verify that the
2249 // reference in vAA is an instance of the "return_type."
2250 if (reg_type.IsUndefined()) {
2251 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning undefined register";
2252 } else if (reg_type.IsConflict()) {
2253 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning register with conflict";
2254 } else if (reg_type.IsUninitializedTypes()) {
David Brazdil68b5c0b2016-01-19 14:25:29 +00002255 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning uninitialized object '"
Brian Carlstrom93c33962013-07-26 10:37:43 -07002256 << reg_type << "'";
Andreas Gampea4c98f22015-11-06 16:24:49 -08002257 } else if (!reg_type.IsReferenceTypes()) {
2258 // We really do expect a reference here.
2259 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-object returns a non-reference type "
2260 << reg_type;
David Brazdilca3c8c32016-09-06 14:04:48 +01002261 } else if (!return_type.IsAssignableFrom(reg_type, this)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07002262 if (reg_type.IsUnresolvedTypes() || return_type.IsUnresolvedTypes()) {
2263 Fail(VERIFY_ERROR_NO_CLASS) << " can't resolve returned type '" << return_type
2264 << "' or '" << reg_type << "'";
2265 } else {
Andreas Gampe16f149c2015-03-23 10:10:20 -07002266 bool soft_error = false;
2267 // Check whether arrays are involved. They will show a valid class status, even
2268 // if their components are erroneous.
2269 if (reg_type.IsArrayTypes() && return_type.IsArrayTypes()) {
David Brazdilca3c8c32016-09-06 14:04:48 +01002270 return_type.CanAssignArray(reg_type, reg_types_, class_loader_, this, &soft_error);
Andreas Gampe16f149c2015-03-23 10:10:20 -07002271 if (soft_error) {
2272 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "array with erroneous component type: "
2273 << reg_type << " vs " << return_type;
2274 }
2275 }
2276
2277 if (!soft_error) {
2278 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning '" << reg_type
2279 << "', but expected from declaration '" << return_type << "'";
2280 }
Jeff Haoa3faaf42013-09-03 19:07:00 -07002281 }
jeffhaobdb76512011-09-07 11:43:16 -07002282 }
2283 }
2284 }
2285 break;
2286
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07002287 /* could be boolean, int, float, or a null reference */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002288 case Instruction::CONST_4: {
2289 int32_t val = static_cast<int32_t>(inst->VRegB_11n() << 28) >> 28;
Andreas Gampead238ce2015-08-24 21:13:08 -07002290 work_line_->SetRegisterType<LockOp::kClear>(
2291 this, inst->VRegA_11n(), DetermineCat1Constant(val, need_precise_constants_));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07002292 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02002293 }
2294 case Instruction::CONST_16: {
2295 int16_t val = static_cast<int16_t>(inst->VRegB_21s());
Andreas Gampead238ce2015-08-24 21:13:08 -07002296 work_line_->SetRegisterType<LockOp::kClear>(
2297 this, inst->VRegA_21s(), DetermineCat1Constant(val, need_precise_constants_));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07002298 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02002299 }
Sebastien Hertz849600b2013-12-20 10:28:08 +01002300 case Instruction::CONST: {
2301 int32_t val = inst->VRegB_31i();
Andreas Gampead238ce2015-08-24 21:13:08 -07002302 work_line_->SetRegisterType<LockOp::kClear>(
2303 this, inst->VRegA_31i(), DetermineCat1Constant(val, need_precise_constants_));
jeffhaobdb76512011-09-07 11:43:16 -07002304 break;
Sebastien Hertz849600b2013-12-20 10:28:08 +01002305 }
2306 case Instruction::CONST_HIGH16: {
2307 int32_t val = static_cast<int32_t>(inst->VRegB_21h() << 16);
Andreas Gampead238ce2015-08-24 21:13:08 -07002308 work_line_->SetRegisterType<LockOp::kClear>(
2309 this, inst->VRegA_21h(), DetermineCat1Constant(val, need_precise_constants_));
jeffhaobdb76512011-09-07 11:43:16 -07002310 break;
Sebastien Hertz849600b2013-12-20 10:28:08 +01002311 }
jeffhaobdb76512011-09-07 11:43:16 -07002312 /* could be long or double; resolved upon use */
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002313 case Instruction::CONST_WIDE_16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002314 int64_t val = static_cast<int16_t>(inst->VRegB_21s());
Ian Rogersd8f69b02014-09-10 21:43:52 +00002315 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
2316 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07002317 work_line_->SetRegisterTypeWide(this, inst->VRegA_21s(), lo, hi);
jeffhaobdb76512011-09-07 11:43:16 -07002318 break;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002319 }
2320 case Instruction::CONST_WIDE_32: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002321 int64_t val = static_cast<int32_t>(inst->VRegB_31i());
Ian Rogersd8f69b02014-09-10 21:43:52 +00002322 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
2323 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07002324 work_line_->SetRegisterTypeWide(this, inst->VRegA_31i(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002325 break;
2326 }
2327 case Instruction::CONST_WIDE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002328 int64_t val = inst->VRegB_51l();
Ian Rogersd8f69b02014-09-10 21:43:52 +00002329 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
2330 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07002331 work_line_->SetRegisterTypeWide(this, inst->VRegA_51l(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002332 break;
2333 }
2334 case Instruction::CONST_WIDE_HIGH16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002335 int64_t val = static_cast<uint64_t>(inst->VRegB_21h()) << 48;
Ian Rogersd8f69b02014-09-10 21:43:52 +00002336 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
2337 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07002338 work_line_->SetRegisterTypeWide(this, inst->VRegA_21h(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002339 break;
2340 }
jeffhaobdb76512011-09-07 11:43:16 -07002341 case Instruction::CONST_STRING:
Andreas Gampead238ce2015-08-24 21:13:08 -07002342 work_line_->SetRegisterType<LockOp::kClear>(
2343 this, inst->VRegA_21c(), reg_types_.JavaLangString());
Sebastien Hertz5243e912013-05-21 10:55:07 +02002344 break;
jeffhaobdb76512011-09-07 11:43:16 -07002345 case Instruction::CONST_STRING_JUMBO:
Andreas Gampead238ce2015-08-24 21:13:08 -07002346 work_line_->SetRegisterType<LockOp::kClear>(
2347 this, inst->VRegA_31c(), reg_types_.JavaLangString());
jeffhaobdb76512011-09-07 11:43:16 -07002348 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002349 case Instruction::CONST_CLASS: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002350 // Get type from instruction if unresolved then we need an access check
2351 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Andreas Gampe98be1a92017-08-28 08:25:45 -07002352 const RegType& res_type = ResolveClass<CheckAccess::kYes>(dex::TypeIndex(inst->VRegB_21c()));
Ian Rogersad0b3a32012-04-16 14:50:24 -07002353 // Register holds class, ie its type is class, on error it will hold Conflict.
Andreas Gampead238ce2015-08-24 21:13:08 -07002354 work_line_->SetRegisterType<LockOp::kClear>(
2355 this, inst->VRegA_21c(), res_type.IsConflict() ? res_type
2356 : reg_types_.JavaLangClass());
jeffhaobdb76512011-09-07 11:43:16 -07002357 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002358 }
Orion Hodson2e599942017-09-22 16:17:41 +01002359 case Instruction::CONST_METHOD_HANDLE:
2360 work_line_->SetRegisterType<LockOp::kClear>(
2361 this, inst->VRegA_21c(), reg_types_.JavaLangInvokeMethodHandle());
2362 // TODO: add compiler support for const-method-{handle,type} (b/66890674)
2363 Fail(VERIFY_ERROR_FORCE_INTERPRETER);
2364 break;
2365 case Instruction::CONST_METHOD_TYPE:
2366 work_line_->SetRegisterType<LockOp::kClear>(
2367 this, inst->VRegA_21c(), reg_types_.JavaLangInvokeMethodType());
2368 // TODO: add compiler support for const-method-{handle,type} (b/66890674)
2369 Fail(VERIFY_ERROR_FORCE_INTERPRETER);
2370 break;
jeffhaobdb76512011-09-07 11:43:16 -07002371 case Instruction::MONITOR_ENTER:
Ian Rogers7b078e82014-09-10 14:44:24 -07002372 work_line_->PushMonitor(this, inst->VRegA_11x(), work_insn_idx_);
Andreas Gampec1474102015-08-18 08:57:44 -07002373 // Check whether the previous instruction is a move-object with vAA as a source, creating
2374 // untracked lock aliasing.
Mathieu Chartierde40d472015-10-15 17:47:48 -07002375 if (0 != work_insn_idx_ && !GetInstructionFlags(work_insn_idx_).IsBranchTarget()) {
Andreas Gampec1474102015-08-18 08:57:44 -07002376 uint32_t prev_idx = work_insn_idx_ - 1;
Mathieu Chartierde40d472015-10-15 17:47:48 -07002377 while (0 != prev_idx && !GetInstructionFlags(prev_idx).IsOpcode()) {
Andreas Gampec1474102015-08-18 08:57:44 -07002378 prev_idx--;
2379 }
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08002380 const Instruction& prev_inst = code_item_accessor_.InstructionAt(prev_idx);
Mathieu Chartier1d2d4ff2017-09-23 16:11:06 -07002381 switch (prev_inst.Opcode()) {
Andreas Gampec1474102015-08-18 08:57:44 -07002382 case Instruction::MOVE_OBJECT:
2383 case Instruction::MOVE_OBJECT_16:
2384 case Instruction::MOVE_OBJECT_FROM16:
Mathieu Chartier1d2d4ff2017-09-23 16:11:06 -07002385 if (prev_inst.VRegB() == inst->VRegA_11x()) {
Andreas Gampec1474102015-08-18 08:57:44 -07002386 // Redo the copy. This won't change the register types, but update the lock status
2387 // for the aliased register.
2388 work_line_->CopyRegister1(this,
Mathieu Chartier1d2d4ff2017-09-23 16:11:06 -07002389 prev_inst.VRegA(),
2390 prev_inst.VRegB(),
Andreas Gampec1474102015-08-18 08:57:44 -07002391 kTypeCategoryRef);
2392 }
2393 break;
2394
2395 default: // Other instruction types ignored.
2396 break;
2397 }
2398 }
jeffhaobdb76512011-09-07 11:43:16 -07002399 break;
2400 case Instruction::MONITOR_EXIT:
2401 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002402 * monitor-exit instructions are odd. They can throw exceptions,
jeffhaobdb76512011-09-07 11:43:16 -07002403 * but when they do they act as if they succeeded and the PC is
jeffhaod1f0fde2011-09-08 17:25:33 -07002404 * pointing to the following instruction. (This behavior goes back
jeffhaobdb76512011-09-07 11:43:16 -07002405 * to the need to handle asynchronous exceptions, a now-deprecated
2406 * feature that Dalvik doesn't support.)
2407 *
jeffhaod1f0fde2011-09-08 17:25:33 -07002408 * In practice we don't need to worry about this. The only
jeffhaobdb76512011-09-07 11:43:16 -07002409 * exceptions that can be thrown from monitor-exit are for a
jeffhaod1f0fde2011-09-08 17:25:33 -07002410 * null reference and -exit without a matching -enter. If the
jeffhaobdb76512011-09-07 11:43:16 -07002411 * structured locking checks are working, the former would have
2412 * failed on the -enter instruction, and the latter is impossible.
2413 *
2414 * This is fortunate, because issue 3221411 prevents us from
2415 * chasing the "can throw" path when monitor verification is
jeffhaod1f0fde2011-09-08 17:25:33 -07002416 * enabled. If we can fully verify the locking we can ignore
jeffhaobdb76512011-09-07 11:43:16 -07002417 * some catch blocks (which will show up as "dead" code when
2418 * we skip them here); if we can't, then the code path could be
2419 * "live" so we still need to check it.
2420 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002421 opcode_flags &= ~Instruction::kThrow;
Ian Rogers7b078e82014-09-10 14:44:24 -07002422 work_line_->PopMonitor(this, inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07002423 break;
Ian Rogers28ad40d2011-10-27 15:19:26 -07002424 case Instruction::CHECK_CAST:
Ian Rogersd81871c2011-10-03 13:57:23 -07002425 case Instruction::INSTANCE_OF: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002426 /*
2427 * If this instruction succeeds, we will "downcast" register vA to the type in vB. (This
2428 * could be a "upcast" -- not expected, so we don't try to address it.)
2429 *
2430 * If it fails, an exception is thrown, which we deal with later by ignoring the update to
Elliott Hughesadb8c672012-03-06 16:49:32 -08002431 * dec_insn.vA when branching to a handler.
Ian Rogers28ad40d2011-10-27 15:19:26 -07002432 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002433 const bool is_checkcast = (inst->Opcode() == Instruction::CHECK_CAST);
Andreas Gampea5b09a62016-11-17 15:21:22 -08002434 const dex::TypeIndex type_idx((is_checkcast) ? inst->VRegB_21c() : inst->VRegC_22c());
Andreas Gampe98be1a92017-08-28 08:25:45 -07002435 const RegType& res_type = ResolveClass<CheckAccess::kYes>(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002436 if (res_type.IsConflict()) {
Andreas Gampe00633eb2014-07-17 16:13:35 -07002437 // If this is a primitive type, fail HARD.
Vladimir Marko666ee3d2017-12-11 18:37:36 +00002438 ObjPtr<mirror::Class> klass = Runtime::Current()->GetClassLinker()->LookupResolvedType(
2439 type_idx, dex_cache_.Get(), class_loader_.Get());
Andreas Gampe00633eb2014-07-17 16:13:35 -07002440 if (klass != nullptr && klass->IsPrimitive()) {
2441 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "using primitive type "
2442 << dex_file_->StringByTypeIdx(type_idx) << " in instanceof in "
2443 << GetDeclaringClass();
2444 break;
2445 }
2446
Ian Rogersad0b3a32012-04-16 14:50:24 -07002447 DCHECK_NE(failures_.size(), 0U);
2448 if (!is_checkcast) {
Andreas Gampead238ce2015-08-24 21:13:08 -07002449 work_line_->SetRegisterType<LockOp::kClear>(this,
2450 inst->VRegA_22c(),
2451 reg_types_.Boolean());
Ian Rogersad0b3a32012-04-16 14:50:24 -07002452 }
2453 break; // bad class
Ian Rogers9f1ab122011-12-12 08:52:43 -08002454 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002455 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Sebastien Hertz5243e912013-05-21 10:55:07 +02002456 uint32_t orig_type_reg = (is_checkcast) ? inst->VRegA_21c() : inst->VRegB_22c();
Ian Rogers7b078e82014-09-10 14:44:24 -07002457 const RegType& orig_type = work_line_->GetRegisterType(this, orig_type_reg);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002458 if (!res_type.IsNonZeroReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002459 if (is_checkcast) {
2460 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on unexpected class " << res_type;
2461 } else {
2462 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on unexpected class " << res_type;
2463 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002464 } else if (!orig_type.IsReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002465 if (is_checkcast) {
2466 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on non-reference in v" << orig_type_reg;
2467 } else {
2468 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on non-reference in v" << orig_type_reg;
2469 }
David Brazdil68b5c0b2016-01-19 14:25:29 +00002470 } else if (orig_type.IsUninitializedTypes()) {
2471 if (is_checkcast) {
2472 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on uninitialized reference in v"
2473 << orig_type_reg;
2474 } else {
2475 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on uninitialized reference in v"
2476 << orig_type_reg;
2477 }
jeffhao2a8a90e2011-09-26 14:25:31 -07002478 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002479 if (is_checkcast) {
Andreas Gampead238ce2015-08-24 21:13:08 -07002480 work_line_->SetRegisterType<LockOp::kKeep>(this, inst->VRegA_21c(), res_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07002481 } else {
Andreas Gampead238ce2015-08-24 21:13:08 -07002482 work_line_->SetRegisterType<LockOp::kClear>(this,
2483 inst->VRegA_22c(),
2484 reg_types_.Boolean());
jeffhaobdb76512011-09-07 11:43:16 -07002485 }
jeffhaobdb76512011-09-07 11:43:16 -07002486 }
jeffhao2a8a90e2011-09-26 14:25:31 -07002487 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002488 }
2489 case Instruction::ARRAY_LENGTH: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002490 const RegType& res_type = work_line_->GetRegisterType(this, inst->VRegB_12x());
Ian Rogers28ad40d2011-10-27 15:19:26 -07002491 if (res_type.IsReferenceTypes()) {
Andreas Gampeeed3a5d2017-11-29 14:58:34 -08002492 if (!res_type.IsArrayTypes() && !res_type.IsZeroOrNull()) {
2493 // ie not an array or null
jeffhaod5347e02012-03-22 17:25:05 -07002494 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002495 } else {
Andreas Gampead238ce2015-08-24 21:13:08 -07002496 work_line_->SetRegisterType<LockOp::kClear>(this,
2497 inst->VRegA_12x(),
2498 reg_types_.Integer());
Ian Rogersd81871c2011-10-03 13:57:23 -07002499 }
Andreas Gampe65c9db82014-07-28 13:14:34 -07002500 } else {
2501 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002502 }
2503 break;
2504 }
2505 case Instruction::NEW_INSTANCE: {
Andreas Gampe98be1a92017-08-28 08:25:45 -07002506 const RegType& res_type = ResolveClass<CheckAccess::kYes>(dex::TypeIndex(inst->VRegB_21c()));
Ian Rogersad0b3a32012-04-16 14:50:24 -07002507 if (res_type.IsConflict()) {
2508 DCHECK_NE(failures_.size(), 0U);
2509 break; // bad class
jeffhao8cd6dda2012-02-22 10:15:34 -08002510 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002511 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
2512 // can't create an instance of an interface or abstract class */
2513 if (!res_type.IsInstantiableTypes()) {
2514 Fail(VERIFY_ERROR_INSTANTIATION)
2515 << "new-instance on primitive, interface or abstract class" << res_type;
Ian Rogers08f753d2012-08-24 14:35:25 -07002516 // Soft failure so carry on to set register type.
Ian Rogersd81871c2011-10-03 13:57:23 -07002517 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00002518 const RegType& uninit_type = reg_types_.Uninitialized(res_type, work_insn_idx_);
Ian Rogers08f753d2012-08-24 14:35:25 -07002519 // Any registers holding previous allocations from this address that have not yet been
2520 // initialized must be marked invalid.
Ian Rogers7b078e82014-09-10 14:44:24 -07002521 work_line_->MarkUninitRefsAsInvalid(this, uninit_type);
Ian Rogers08f753d2012-08-24 14:35:25 -07002522 // add the new uninitialized reference to the register state
Andreas Gampead238ce2015-08-24 21:13:08 -07002523 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_21c(), uninit_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07002524 break;
2525 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08002526 case Instruction::NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002527 VerifyNewArray(inst, false, false);
jeffhaobdb76512011-09-07 11:43:16 -07002528 break;
2529 case Instruction::FILLED_NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002530 VerifyNewArray(inst, true, false);
Ian Rogers0c4a5062012-02-03 15:18:59 -08002531 just_set_result = true; // Filled new array sets result register
jeffhaobdb76512011-09-07 11:43:16 -07002532 break;
Ian Rogers0c4a5062012-02-03 15:18:59 -08002533 case Instruction::FILLED_NEW_ARRAY_RANGE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002534 VerifyNewArray(inst, true, true);
Ian Rogers0c4a5062012-02-03 15:18:59 -08002535 just_set_result = true; // Filled new array range sets result register
2536 break;
jeffhaobdb76512011-09-07 11:43:16 -07002537 case Instruction::CMPL_FLOAT:
2538 case Instruction::CMPG_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002539 if (!work_line_->VerifyRegisterType(this, inst->VRegB_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08002540 break;
2541 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002542 if (!work_line_->VerifyRegisterType(this, inst->VRegC_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08002543 break;
2544 }
Andreas Gampead238ce2015-08-24 21:13:08 -07002545 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002546 break;
2547 case Instruction::CMPL_DOUBLE:
2548 case Instruction::CMPG_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002549 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegB_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002550 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002551 break;
2552 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002553 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegC_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002554 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002555 break;
2556 }
Andreas Gampead238ce2015-08-24 21:13:08 -07002557 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002558 break;
2559 case Instruction::CMP_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002560 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegB_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002561 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002562 break;
2563 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002564 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegC_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002565 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002566 break;
2567 }
Andreas Gampead238ce2015-08-24 21:13:08 -07002568 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002569 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002570 case Instruction::THROW: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002571 const RegType& res_type = work_line_->GetRegisterType(this, inst->VRegA_11x());
David Brazdilca3c8c32016-09-06 14:04:48 +01002572 if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(res_type, this)) {
David Brazdil68b5c0b2016-01-19 14:25:29 +00002573 if (res_type.IsUninitializedTypes()) {
2574 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "thrown exception not initialized";
Pavel Vyssotski980027c2016-02-11 20:28:11 +06002575 } else if (!res_type.IsReferenceTypes()) {
2576 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "thrown value of non-reference type " << res_type;
David Brazdil68b5c0b2016-01-19 14:25:29 +00002577 } else {
2578 Fail(res_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS : VERIFY_ERROR_BAD_CLASS_SOFT)
2579 << "thrown class " << res_type << " not instanceof Throwable";
2580 }
jeffhaobdb76512011-09-07 11:43:16 -07002581 }
2582 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002583 }
jeffhaobdb76512011-09-07 11:43:16 -07002584 case Instruction::GOTO:
2585 case Instruction::GOTO_16:
2586 case Instruction::GOTO_32:
2587 /* no effect on or use of registers */
2588 break;
2589
2590 case Instruction::PACKED_SWITCH:
2591 case Instruction::SPARSE_SWITCH:
2592 /* verify that vAA is an integer, or can be converted to one */
Ian Rogers7b078e82014-09-10 14:44:24 -07002593 work_line_->VerifyRegisterType(this, inst->VRegA_31t(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002594 break;
2595
Ian Rogersd81871c2011-10-03 13:57:23 -07002596 case Instruction::FILL_ARRAY_DATA: {
2597 /* Similar to the verification done for APUT */
Ian Rogers7b078e82014-09-10 14:44:24 -07002598 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegA_31t());
Ian Rogers89310de2012-02-01 13:47:30 -08002599 /* array_type can be null if the reg type is Zero */
Andreas Gampeeed3a5d2017-11-29 14:58:34 -08002600 if (!array_type.IsZeroOrNull()) {
jeffhao457cc512012-02-02 16:55:13 -08002601 if (!array_type.IsArrayTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002602 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with array type "
2603 << array_type;
Andreas Gampebb18a032016-03-22 20:34:25 -07002604 } else if (array_type.IsUnresolvedTypes()) {
2605 // If it's an unresolved array type, it must be non-primitive.
2606 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data for array of type "
2607 << array_type;
Ian Rogers89310de2012-02-01 13:47:30 -08002608 } else {
Ian Rogersd8f69b02014-09-10 21:43:52 +00002609 const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
Ian Rogersad0b3a32012-04-16 14:50:24 -07002610 DCHECK(!component_type.IsConflict());
jeffhao457cc512012-02-02 16:55:13 -08002611 if (component_type.IsNonZeroReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002612 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with component type "
2613 << component_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002614 } else {
jeffhao457cc512012-02-02 16:55:13 -08002615 // Now verify if the element width in the table matches the element width declared in
2616 // the array
Andreas Gampe53de99c2015-08-17 13:43:55 -07002617 const uint16_t* array_data =
2618 insns + (insns[1] | (static_cast<int32_t>(insns[2]) << 16));
jeffhao457cc512012-02-02 16:55:13 -08002619 if (array_data[0] != Instruction::kArrayDataSignature) {
jeffhaod5347e02012-03-22 17:25:05 -07002620 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid magic for array-data";
jeffhao457cc512012-02-02 16:55:13 -08002621 } else {
2622 size_t elem_width = Primitive::ComponentSize(component_type.GetPrimitiveType());
2623 // Since we don't compress the data in Dex, expect to see equal width of data stored
2624 // in the table and expected from the array class.
2625 if (array_data[1] != elem_width) {
jeffhaod5347e02012-03-22 17:25:05 -07002626 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-data size mismatch (" << array_data[1]
2627 << " vs " << elem_width << ")";
jeffhao457cc512012-02-02 16:55:13 -08002628 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002629 }
2630 }
jeffhaobdb76512011-09-07 11:43:16 -07002631 }
2632 }
2633 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002634 }
jeffhaobdb76512011-09-07 11:43:16 -07002635 case Instruction::IF_EQ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002636 case Instruction::IF_NE: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002637 const RegType& reg_type1 = work_line_->GetRegisterType(this, inst->VRegA_22t());
2638 const RegType& reg_type2 = work_line_->GetRegisterType(this, inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002639 bool mismatch = false;
Andreas Gampeeed3a5d2017-11-29 14:58:34 -08002640 if (reg_type1.IsZeroOrNull()) { // zero then integral or reference expected
Ian Rogersd81871c2011-10-03 13:57:23 -07002641 mismatch = !reg_type2.IsReferenceTypes() && !reg_type2.IsIntegralTypes();
2642 } else if (reg_type1.IsReferenceTypes()) { // both references?
2643 mismatch = !reg_type2.IsReferenceTypes();
2644 } else { // both integral?
2645 mismatch = !reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes();
2646 }
2647 if (mismatch) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002648 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to if-eq/if-ne (" << reg_type1 << ","
2649 << reg_type2 << ") must both be references or integral";
jeffhaobdb76512011-09-07 11:43:16 -07002650 }
2651 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002652 }
jeffhaobdb76512011-09-07 11:43:16 -07002653 case Instruction::IF_LT:
2654 case Instruction::IF_GE:
2655 case Instruction::IF_GT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002656 case Instruction::IF_LE: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002657 const RegType& reg_type1 = work_line_->GetRegisterType(this, inst->VRegA_22t());
2658 const RegType& reg_type2 = work_line_->GetRegisterType(this, inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002659 if (!reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002660 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to 'if' (" << reg_type1 << ","
2661 << reg_type2 << ") must be integral";
jeffhaobdb76512011-09-07 11:43:16 -07002662 }
2663 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002664 }
jeffhaobdb76512011-09-07 11:43:16 -07002665 case Instruction::IF_EQZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002666 case Instruction::IF_NEZ: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002667 const RegType& reg_type = work_line_->GetRegisterType(this, inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002668 if (!reg_type.IsReferenceTypes() && !reg_type.IsIntegralTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002669 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
2670 << " unexpected as arg to if-eqz/if-nez";
Ian Rogersd81871c2011-10-03 13:57:23 -07002671 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002672
2673 // Find previous instruction - its existence is a precondition to peephole optimization.
Ian Rogers9b360392013-06-06 14:45:07 -07002674 uint32_t instance_of_idx = 0;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002675 if (0 != work_insn_idx_) {
Ian Rogers9b360392013-06-06 14:45:07 -07002676 instance_of_idx = work_insn_idx_ - 1;
Mathieu Chartierde40d472015-10-15 17:47:48 -07002677 while (0 != instance_of_idx && !GetInstructionFlags(instance_of_idx).IsOpcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07002678 instance_of_idx--;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002679 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07002680 if (FailOrAbort(this, GetInstructionFlags(instance_of_idx).IsOpcode(),
Andreas Gampe7c038102014-10-27 20:08:46 -07002681 "Unable to get previous instruction of if-eqz/if-nez for work index ",
2682 work_insn_idx_)) {
2683 break;
2684 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002685 } else {
2686 break;
2687 }
2688
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08002689 const Instruction& instance_of_inst = code_item_accessor_.InstructionAt(instance_of_idx);
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002690
2691 /* Check for peep-hole pattern of:
2692 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002693 * instance-of vX, vY, T;
2694 * ifXXX vX, label ;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002695 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002696 * label:
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002697 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002698 * and sharpen the type of vY to be type T.
2699 * Note, this pattern can't be if:
2700 * - if there are other branches to this branch,
2701 * - when vX == vY.
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002702 */
Ian Rogersfae370a2013-06-05 08:33:27 -07002703 if (!CurrentInsnFlags()->IsBranchTarget() &&
Vladimir Markod7559b72017-09-28 13:50:37 +01002704 (Instruction::INSTANCE_OF == instance_of_inst.Opcode()) &&
2705 (inst->VRegA_21t() == instance_of_inst.VRegA_22c()) &&
2706 (instance_of_inst.VRegA_22c() != instance_of_inst.VRegB_22c())) {
Ian Rogersebbdd872014-07-07 23:53:08 -07002707 // Check the type of the instance-of is different than that of registers type, as if they
2708 // are the same there is no work to be done here. Check that the conversion is not to or
2709 // from an unresolved type as type information is imprecise. If the instance-of is to an
2710 // interface then ignore the type information as interfaces can only be treated as Objects
2711 // and we don't want to disallow field and other operations on the object. If the value
2712 // being instance-of checked against is known null (zero) then allow the optimization as
2713 // we didn't have type information. If the merge of the instance-of type with the original
2714 // type is assignable to the original then allow optimization. This check is performed to
2715 // ensure that subsequent merges don't lose type information - such as becoming an
2716 // interface from a class that would lose information relevant to field checks.
Vladimir Markod7559b72017-09-28 13:50:37 +01002717 const RegType& orig_type = work_line_->GetRegisterType(this, instance_of_inst.VRegB_22c());
Andreas Gampe98be1a92017-08-28 08:25:45 -07002718 const RegType& cast_type = ResolveClass<CheckAccess::kYes>(
Vladimir Markod7559b72017-09-28 13:50:37 +01002719 dex::TypeIndex(instance_of_inst.VRegC_22c()));
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002720
Ian Rogersebbdd872014-07-07 23:53:08 -07002721 if (!orig_type.Equals(cast_type) &&
2722 !cast_type.IsUnresolvedTypes() && !orig_type.IsUnresolvedTypes() &&
Andreas Gampe00633eb2014-07-17 16:13:35 -07002723 cast_type.HasClass() && // Could be conflict type, make sure it has a class.
Ian Rogersebbdd872014-07-07 23:53:08 -07002724 !cast_type.GetClass()->IsInterface() &&
Andreas Gampeeed3a5d2017-11-29 14:58:34 -08002725 (orig_type.IsZeroOrNull() ||
David Brazdilca3c8c32016-09-06 14:04:48 +01002726 orig_type.IsStrictlyAssignableFrom(
2727 cast_type.Merge(orig_type, &reg_types_, this), this))) {
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08002728 RegisterLine* update_line = RegisterLine::Create(code_item_accessor_.RegistersSize(),
2729 this);
Ian Rogersfae370a2013-06-05 08:33:27 -07002730 if (inst->Opcode() == Instruction::IF_EQZ) {
Ian Rogers9b360392013-06-06 14:45:07 -07002731 fallthrough_line.reset(update_line);
Ian Rogersfae370a2013-06-05 08:33:27 -07002732 } else {
Ian Rogers9b360392013-06-06 14:45:07 -07002733 branch_line.reset(update_line);
2734 }
2735 update_line->CopyFromLine(work_line_.get());
Andreas Gampead238ce2015-08-24 21:13:08 -07002736 update_line->SetRegisterType<LockOp::kKeep>(this,
Vladimir Markod7559b72017-09-28 13:50:37 +01002737 instance_of_inst.VRegB_22c(),
Andreas Gampead238ce2015-08-24 21:13:08 -07002738 cast_type);
Mathieu Chartierde40d472015-10-15 17:47:48 -07002739 if (!GetInstructionFlags(instance_of_idx).IsBranchTarget() && 0 != instance_of_idx) {
Ian Rogers9b360392013-06-06 14:45:07 -07002740 // See if instance-of was preceded by a move-object operation, common due to the small
2741 // register encoding space of instance-of, and propagate type information to the source
2742 // of the move-object.
2743 uint32_t move_idx = instance_of_idx - 1;
Mathieu Chartierde40d472015-10-15 17:47:48 -07002744 while (0 != move_idx && !GetInstructionFlags(move_idx).IsOpcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07002745 move_idx--;
2746 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07002747 if (FailOrAbort(this, GetInstructionFlags(move_idx).IsOpcode(),
Andreas Gampe7c038102014-10-27 20:08:46 -07002748 "Unable to get previous instruction of if-eqz/if-nez for work index ",
2749 work_insn_idx_)) {
2750 break;
2751 }
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08002752 const Instruction& move_inst = code_item_accessor_.InstructionAt(move_idx);
Vladimir Markod7559b72017-09-28 13:50:37 +01002753 switch (move_inst.Opcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07002754 case Instruction::MOVE_OBJECT:
Vladimir Markod7559b72017-09-28 13:50:37 +01002755 if (move_inst.VRegA_12x() == instance_of_inst.VRegB_22c()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07002756 update_line->SetRegisterType<LockOp::kKeep>(this,
Vladimir Markod7559b72017-09-28 13:50:37 +01002757 move_inst.VRegB_12x(),
Andreas Gampead238ce2015-08-24 21:13:08 -07002758 cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002759 }
2760 break;
2761 case Instruction::MOVE_OBJECT_FROM16:
Vladimir Markod7559b72017-09-28 13:50:37 +01002762 if (move_inst.VRegA_22x() == instance_of_inst.VRegB_22c()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07002763 update_line->SetRegisterType<LockOp::kKeep>(this,
Vladimir Markod7559b72017-09-28 13:50:37 +01002764 move_inst.VRegB_22x(),
Andreas Gampead238ce2015-08-24 21:13:08 -07002765 cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002766 }
2767 break;
2768 case Instruction::MOVE_OBJECT_16:
Vladimir Markod7559b72017-09-28 13:50:37 +01002769 if (move_inst.VRegA_32x() == instance_of_inst.VRegB_22c()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07002770 update_line->SetRegisterType<LockOp::kKeep>(this,
Vladimir Markod7559b72017-09-28 13:50:37 +01002771 move_inst.VRegB_32x(),
Andreas Gampead238ce2015-08-24 21:13:08 -07002772 cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002773 }
2774 break;
2775 default:
2776 break;
2777 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002778 }
2779 }
2780 }
2781
jeffhaobdb76512011-09-07 11:43:16 -07002782 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002783 }
jeffhaobdb76512011-09-07 11:43:16 -07002784 case Instruction::IF_LTZ:
2785 case Instruction::IF_GEZ:
2786 case Instruction::IF_GTZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002787 case Instruction::IF_LEZ: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002788 const RegType& reg_type = work_line_->GetRegisterType(this, inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002789 if (!reg_type.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002790 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
2791 << " unexpected as arg to if-ltz/if-gez/if-gtz/if-lez";
Ian Rogersd81871c2011-10-03 13:57:23 -07002792 }
jeffhaobdb76512011-09-07 11:43:16 -07002793 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002794 }
jeffhaobdb76512011-09-07 11:43:16 -07002795 case Instruction::AGET_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002796 VerifyAGet(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002797 break;
jeffhaobdb76512011-09-07 11:43:16 -07002798 case Instruction::AGET_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002799 VerifyAGet(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002800 break;
jeffhaobdb76512011-09-07 11:43:16 -07002801 case Instruction::AGET_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002802 VerifyAGet(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002803 break;
jeffhaobdb76512011-09-07 11:43:16 -07002804 case Instruction::AGET_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002805 VerifyAGet(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002806 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002807 case Instruction::AGET:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002808 VerifyAGet(inst, reg_types_.Integer(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002809 break;
jeffhaobdb76512011-09-07 11:43:16 -07002810 case Instruction::AGET_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002811 VerifyAGet(inst, reg_types_.LongLo(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002812 break;
2813 case Instruction::AGET_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002814 VerifyAGet(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07002815 break;
2816
Ian Rogersd81871c2011-10-03 13:57:23 -07002817 case Instruction::APUT_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002818 VerifyAPut(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002819 break;
2820 case Instruction::APUT_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002821 VerifyAPut(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002822 break;
2823 case Instruction::APUT_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002824 VerifyAPut(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002825 break;
2826 case Instruction::APUT_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002827 VerifyAPut(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002828 break;
2829 case Instruction::APUT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002830 VerifyAPut(inst, reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002831 break;
2832 case Instruction::APUT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002833 VerifyAPut(inst, reg_types_.LongLo(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002834 break;
2835 case Instruction::APUT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002836 VerifyAPut(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07002837 break;
2838
jeffhaobdb76512011-09-07 11:43:16 -07002839 case Instruction::IGET_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002840 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002841 break;
jeffhaobdb76512011-09-07 11:43:16 -07002842 case Instruction::IGET_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002843 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002844 break;
jeffhaobdb76512011-09-07 11:43:16 -07002845 case Instruction::IGET_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002846 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002847 break;
jeffhaobdb76512011-09-07 11:43:16 -07002848 case Instruction::IGET_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002849 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002850 break;
2851 case Instruction::IGET:
Andreas Gampe896df402014-10-20 22:25:29 -07002852 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002853 break;
2854 case Instruction::IGET_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002855 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002856 break;
2857 case Instruction::IGET_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002858 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false,
2859 false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002860 break;
jeffhaobdb76512011-09-07 11:43:16 -07002861
Ian Rogersd81871c2011-10-03 13:57:23 -07002862 case Instruction::IPUT_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002863 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002864 break;
2865 case Instruction::IPUT_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002866 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002867 break;
2868 case Instruction::IPUT_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002869 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002870 break;
2871 case Instruction::IPUT_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002872 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002873 break;
2874 case Instruction::IPUT:
Andreas Gampe896df402014-10-20 22:25:29 -07002875 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002876 break;
2877 case Instruction::IPUT_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002878 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002879 break;
jeffhaobdb76512011-09-07 11:43:16 -07002880 case Instruction::IPUT_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002881 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false,
2882 false);
jeffhaobdb76512011-09-07 11:43:16 -07002883 break;
2884
jeffhaobdb76512011-09-07 11:43:16 -07002885 case Instruction::SGET_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002886 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002887 break;
jeffhaobdb76512011-09-07 11:43:16 -07002888 case Instruction::SGET_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002889 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002890 break;
jeffhaobdb76512011-09-07 11:43:16 -07002891 case Instruction::SGET_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002892 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002893 break;
jeffhaobdb76512011-09-07 11:43:16 -07002894 case Instruction::SGET_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002895 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002896 break;
2897 case Instruction::SGET:
Andreas Gampe896df402014-10-20 22:25:29 -07002898 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002899 break;
2900 case Instruction::SGET_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002901 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002902 break;
2903 case Instruction::SGET_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002904 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false,
2905 true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002906 break;
2907
2908 case Instruction::SPUT_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002909 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002910 break;
2911 case Instruction::SPUT_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002912 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002913 break;
2914 case Instruction::SPUT_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002915 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002916 break;
2917 case Instruction::SPUT_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002918 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002919 break;
2920 case Instruction::SPUT:
Andreas Gampe896df402014-10-20 22:25:29 -07002921 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002922 break;
2923 case Instruction::SPUT_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002924 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002925 break;
2926 case Instruction::SPUT_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002927 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false,
2928 true);
jeffhaobdb76512011-09-07 11:43:16 -07002929 break;
2930
2931 case Instruction::INVOKE_VIRTUAL:
2932 case Instruction::INVOKE_VIRTUAL_RANGE:
2933 case Instruction::INVOKE_SUPER:
Ian Rogersd81871c2011-10-03 13:57:23 -07002934 case Instruction::INVOKE_SUPER_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002935 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE ||
2936 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002937 bool is_super = (inst->Opcode() == Instruction::INVOKE_SUPER ||
2938 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
Alex Light7268d472016-01-20 15:50:01 -08002939 MethodType type = is_super ? METHOD_SUPER : METHOD_VIRTUAL;
2940 ArtMethod* called_method = VerifyInvocationArgs(inst, type, is_range);
Ian Rogersd8f69b02014-09-10 21:43:52 +00002941 const RegType* return_type = nullptr;
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002942 if (called_method != nullptr) {
Vladimir Markob45528c2017-07-27 14:14:28 +01002943 ObjPtr<mirror::Class> return_type_class = can_load_classes_
2944 ? called_method->ResolveReturnType()
2945 : called_method->LookupResolvedReturnType();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002946 if (return_type_class != nullptr) {
Andreas Gampef23f33d2015-06-23 14:18:17 -07002947 return_type = &FromClass(called_method->GetReturnTypeDescriptor(),
Vladimir Markob45528c2017-07-27 14:14:28 +01002948 return_type_class.Ptr(),
Andreas Gampef23f33d2015-06-23 14:18:17 -07002949 return_type_class->CannotBeAssignedFromOtherTypes());
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002950 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07002951 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
2952 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002953 }
2954 }
2955 if (return_type == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002956 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002957 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
Andreas Gampea5b09a62016-11-17 15:21:22 -08002958 dex::TypeIndex return_type_idx =
2959 dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002960 const char* descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Mathieu Chartierbf99f772014-08-23 16:37:27 -07002961 return_type = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
jeffhaobdb76512011-09-07 11:43:16 -07002962 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002963 if (!return_type->IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002964 work_line_->SetResultRegisterType(this, *return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002965 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002966 work_line_->SetResultRegisterTypeWide(*return_type, return_type->HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002967 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002968 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002969 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002970 }
jeffhaobdb76512011-09-07 11:43:16 -07002971 case Instruction::INVOKE_DIRECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002972 case Instruction::INVOKE_DIRECT_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002973 bool is_range = (inst->Opcode() == Instruction::INVOKE_DIRECT_RANGE);
Alex Light7268d472016-01-20 15:50:01 -08002974 ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_DIRECT, is_range);
Ian Rogers46685432012-06-03 22:26:43 -07002975 const char* return_type_descriptor;
2976 bool is_constructor;
Ian Rogersd8f69b02014-09-10 21:43:52 +00002977 const RegType* return_type = nullptr;
Ian Rogers7b078e82014-09-10 14:44:24 -07002978 if (called_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002979 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogers46685432012-06-03 22:26:43 -07002980 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
Ian Rogersdfb325e2013-10-30 01:00:44 -07002981 is_constructor = strcmp("<init>", dex_file_->StringDataByIdx(method_id.name_idx_)) == 0;
Andreas Gampea5b09a62016-11-17 15:21:22 -08002982 dex::TypeIndex return_type_idx =
2983 dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogers46685432012-06-03 22:26:43 -07002984 return_type_descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2985 } else {
2986 is_constructor = called_method->IsConstructor();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002987 return_type_descriptor = called_method->GetReturnTypeDescriptor();
Vladimir Markob45528c2017-07-27 14:14:28 +01002988 ObjPtr<mirror::Class> return_type_class = can_load_classes_
2989 ? called_method->ResolveReturnType()
2990 : called_method->LookupResolvedReturnType();
Ian Rogers1ff3c982014-08-12 02:30:58 -07002991 if (return_type_class != nullptr) {
Andreas Gampef23f33d2015-06-23 14:18:17 -07002992 return_type = &FromClass(return_type_descriptor,
Vladimir Markob45528c2017-07-27 14:14:28 +01002993 return_type_class.Ptr(),
Andreas Gampef23f33d2015-06-23 14:18:17 -07002994 return_type_class->CannotBeAssignedFromOtherTypes());
Ian Rogers1ff3c982014-08-12 02:30:58 -07002995 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07002996 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
2997 self_->ClearException();
Ian Rogers1ff3c982014-08-12 02:30:58 -07002998 }
Ian Rogers46685432012-06-03 22:26:43 -07002999 }
3000 if (is_constructor) {
jeffhaobdb76512011-09-07 11:43:16 -07003001 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003002 * Some additional checks when calling a constructor. We know from the invocation arg check
3003 * that the "this" argument is an instance of called_method->klass. Now we further restrict
3004 * that to require that called_method->klass is the same as this->klass or this->super,
3005 * allowing the latter only if the "this" argument is the same as the "this" argument to
3006 * this method (which implies that we're in a constructor ourselves).
jeffhaobdb76512011-09-07 11:43:16 -07003007 */
Orion Hodsoncfa325e2016-10-13 10:25:54 +01003008 const RegType& this_type = work_line_->GetInvocationThis(this, inst);
jeffhaob57e9522012-04-26 18:08:21 -07003009 if (this_type.IsConflict()) // failure.
3010 break;
jeffhaobdb76512011-09-07 11:43:16 -07003011
jeffhaob57e9522012-04-26 18:08:21 -07003012 /* no null refs allowed (?) */
Andreas Gampeeed3a5d2017-11-29 14:58:34 -08003013 if (this_type.IsZeroOrNull()) {
jeffhaob57e9522012-04-26 18:08:21 -07003014 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unable to initialize null ref";
3015 break;
jeffhao2a8a90e2011-09-26 14:25:31 -07003016 }
jeffhaob57e9522012-04-26 18:08:21 -07003017
3018 /* must be in same class or in superclass */
Ian Rogersd8f69b02014-09-10 21:43:52 +00003019 // const RegType& this_super_klass = this_type.GetSuperClass(&reg_types_);
Ian Rogers46685432012-06-03 22:26:43 -07003020 // TODO: re-enable constructor type verification
3021 // if (this_super_klass.IsConflict()) {
jeffhaob57e9522012-04-26 18:08:21 -07003022 // Unknown super class, fail so we re-check at runtime.
Ian Rogers46685432012-06-03 22:26:43 -07003023 // Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "super class unknown for '" << this_type << "'";
3024 // break;
3025 // }
jeffhaob57e9522012-04-26 18:08:21 -07003026
3027 /* arg must be an uninitialized reference */
3028 if (!this_type.IsUninitializedTypes()) {
3029 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Expected initialization on uninitialized reference "
3030 << this_type;
3031 break;
3032 }
3033
3034 /*
3035 * Replace the uninitialized reference with an initialized one. We need to do this for all
3036 * registers that have the same object instance in them, not just the "this" register.
3037 */
Nicolas Geoffray98e6ce42016-02-16 18:42:15 +00003038 work_line_->MarkRefsAsInitialized(this, this_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003039 }
Ian Rogers1ff3c982014-08-12 02:30:58 -07003040 if (return_type == nullptr) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07003041 return_type = &reg_types_.FromDescriptor(GetClassLoader(), return_type_descriptor, false);
Ian Rogers1ff3c982014-08-12 02:30:58 -07003042 }
3043 if (!return_type->IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003044 work_line_->SetResultRegisterType(this, *return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003045 } else {
Ian Rogers1ff3c982014-08-12 02:30:58 -07003046 work_line_->SetResultRegisterTypeWide(*return_type, return_type->HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003047 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003048 just_set_result = true;
3049 break;
3050 }
3051 case Instruction::INVOKE_STATIC:
3052 case Instruction::INVOKE_STATIC_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003053 bool is_range = (inst->Opcode() == Instruction::INVOKE_STATIC_RANGE);
Alex Light7268d472016-01-20 15:50:01 -08003054 ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_STATIC, is_range);
Ian Rogers28ad40d2011-10-27 15:19:26 -07003055 const char* descriptor;
Ian Rogers7b078e82014-09-10 14:44:24 -07003056 if (called_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003057 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogers28ad40d2011-10-27 15:19:26 -07003058 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
Andreas Gampea5b09a62016-11-17 15:21:22 -08003059 dex::TypeIndex return_type_idx =
3060 dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Mathieu Chartier590fee92013-09-13 13:46:47 -07003061 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07003062 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003063 descriptor = called_method->GetReturnTypeDescriptor();
Ian Rogers28ad40d2011-10-27 15:19:26 -07003064 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00003065 const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003066 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003067 work_line_->SetResultRegisterType(this, return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003068 } else {
3069 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
3070 }
jeffhaobdb76512011-09-07 11:43:16 -07003071 just_set_result = true;
3072 }
3073 break;
jeffhaobdb76512011-09-07 11:43:16 -07003074 case Instruction::INVOKE_INTERFACE:
Ian Rogersd81871c2011-10-03 13:57:23 -07003075 case Instruction::INVOKE_INTERFACE_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003076 bool is_range = (inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE);
Alex Light7268d472016-01-20 15:50:01 -08003077 ArtMethod* abs_method = VerifyInvocationArgs(inst, METHOD_INTERFACE, is_range);
Ian Rogers7b078e82014-09-10 14:44:24 -07003078 if (abs_method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003079 mirror::Class* called_interface = abs_method->GetDeclaringClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07003080 if (!called_interface->IsInterface() && !called_interface->IsObjectClass()) {
3081 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected interface class in invoke-interface '"
David Sehr709b0702016-10-13 09:12:37 -07003082 << abs_method->PrettyMethod() << "'";
Ian Rogersad0b3a32012-04-16 14:50:24 -07003083 break;
Ian Rogers28ad40d2011-10-27 15:19:26 -07003084 }
Ian Rogers0d604842012-04-16 14:50:24 -07003085 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003086 /* Get the type of the "this" arg, which should either be a sub-interface of called
3087 * interface or Object (see comments in RegType::JoinClass).
3088 */
Orion Hodsoncfa325e2016-10-13 10:25:54 +01003089 const RegType& this_type = work_line_->GetInvocationThis(this, inst);
Andreas Gampeeed3a5d2017-11-29 14:58:34 -08003090 if (this_type.IsZeroOrNull()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003091 /* null pointer always passes (and always fails at runtime) */
3092 } else {
3093 if (this_type.IsUninitializedTypes()) {
3094 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface call on uninitialized object "
3095 << this_type;
3096 break;
3097 }
3098 // In the past we have tried to assert that "called_interface" is assignable
3099 // from "this_type.GetClass()", however, as we do an imprecise Join
3100 // (RegType::JoinClass) we don't have full information on what interfaces are
3101 // implemented by "this_type". For example, two classes may implement the same
3102 // interfaces and have a common parent that doesn't implement the interface. The
3103 // join will set "this_type" to the parent class and a test that this implements
3104 // the interface will incorrectly fail.
3105 }
3106 /*
3107 * We don't have an object instance, so we can't find the concrete method. However, all of
3108 * the type information is in the abstract method, so we're good.
3109 */
3110 const char* descriptor;
Ian Rogers7b078e82014-09-10 14:44:24 -07003111 if (abs_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003112 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07003113 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
Andreas Gampea5b09a62016-11-17 15:21:22 -08003114 dex::TypeIndex return_type_idx =
3115 dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Mathieu Chartierde40d472015-10-15 17:47:48 -07003116 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003117 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003118 descriptor = abs_method->GetReturnTypeDescriptor();
Ian Rogersad0b3a32012-04-16 14:50:24 -07003119 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00003120 const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003121 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003122 work_line_->SetResultRegisterType(this, return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003123 } else {
3124 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
3125 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003126 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07003127 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07003128 }
Narayan Kamath9823e782016-08-03 12:46:58 +01003129 case Instruction::INVOKE_POLYMORPHIC:
3130 case Instruction::INVOKE_POLYMORPHIC_RANGE: {
Orion Hodsoncfa325e2016-10-13 10:25:54 +01003131 bool is_range = (inst->Opcode() == Instruction::INVOKE_POLYMORPHIC_RANGE);
3132 ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_POLYMORPHIC, is_range);
3133 if (called_method == nullptr) {
3134 // Convert potential soft failures in VerifyInvocationArgs() to hard errors.
3135 if (failure_messages_.size() > 0) {
3136 std::string message = failure_messages_.back()->str();
3137 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << message;
3138 } else {
3139 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invoke-polymorphic verification failure.";
3140 }
3141 break;
3142 }
3143 if (!CheckSignaturePolymorphicMethod(called_method) ||
3144 !CheckSignaturePolymorphicReceiver(inst)) {
Orion Hodsonfe92d122018-01-02 10:45:17 +00003145 DCHECK(HasFailures());
Orion Hodsoncfa325e2016-10-13 10:25:54 +01003146 break;
3147 }
3148 const uint32_t proto_idx = (is_range) ? inst->VRegH_4rcc() : inst->VRegH_45cc();
Orion Hodsonac141392017-01-13 11:53:47 +00003149 const char* return_descriptor =
Orion Hodsoncfa325e2016-10-13 10:25:54 +01003150 dex_file_->GetReturnTypeDescriptor(dex_file_->GetProtoId(proto_idx));
3151 const RegType& return_type =
Orion Hodsonac141392017-01-13 11:53:47 +00003152 reg_types_.FromDescriptor(GetClassLoader(), return_descriptor, false);
Orion Hodsoncfa325e2016-10-13 10:25:54 +01003153 if (!return_type.IsLowHalf()) {
3154 work_line_->SetResultRegisterType(this, return_type);
3155 } else {
3156 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
3157 }
Orion Hodsonac141392017-01-13 11:53:47 +00003158 just_set_result = true;
Orion Hodsoncfa325e2016-10-13 10:25:54 +01003159 break;
Narayan Kamath9823e782016-08-03 12:46:58 +01003160 }
Orion Hodsonc069a302017-01-18 09:23:12 +00003161 case Instruction::INVOKE_CUSTOM:
3162 case Instruction::INVOKE_CUSTOM_RANGE: {
3163 // Verify registers based on method_type in the call site.
3164 bool is_range = (inst->Opcode() == Instruction::INVOKE_CUSTOM_RANGE);
3165
3166 // Step 1. Check the call site that produces the method handle for invocation
3167 const uint32_t call_site_idx = is_range ? inst->VRegB_3rc() : inst->VRegB_35c();
3168 if (!CheckCallSite(call_site_idx)) {
3169 DCHECK(HasFailures());
3170 break;
3171 }
3172
3173 // Step 2. Check the register arguments correspond to the expected arguments for the
3174 // method handle produced by step 1. The dex file verifier has checked ranges for
3175 // the first three arguments and CheckCallSite has checked the method handle type.
3176 CallSiteArrayValueIterator it(*dex_file_, dex_file_->GetCallSiteId(call_site_idx));
3177 it.Next(); // Skip to name.
3178 it.Next(); // Skip to method type of the method handle
3179 const uint32_t proto_idx = static_cast<uint32_t>(it.GetJavaValue().i);
3180 const DexFile::ProtoId& proto_id = dex_file_->GetProtoId(proto_idx);
3181 DexFileParameterIterator param_it(*dex_file_, proto_id);
3182 // Treat method as static as it has yet to be determined.
3183 VerifyInvocationArgsFromIterator(&param_it, inst, METHOD_STATIC, is_range, nullptr);
3184 const char* return_descriptor = dex_file_->GetReturnTypeDescriptor(proto_id);
3185
3186 // Step 3. Propagate return type information
3187 const RegType& return_type =
3188 reg_types_.FromDescriptor(GetClassLoader(), return_descriptor, false);
3189 if (!return_type.IsLowHalf()) {
3190 work_line_->SetResultRegisterType(this, return_type);
3191 } else {
3192 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
3193 }
3194 just_set_result = true;
3195 // TODO: Add compiler support for invoke-custom (b/35337872).
3196 Fail(VERIFY_ERROR_FORCE_INTERPRETER);
3197 break;
3198 }
jeffhaobdb76512011-09-07 11:43:16 -07003199 case Instruction::NEG_INT:
3200 case Instruction::NOT_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003201 work_line_->CheckUnaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003202 break;
3203 case Instruction::NEG_LONG:
3204 case Instruction::NOT_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07003205 work_line_->CheckUnaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003206 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07003207 break;
3208 case Instruction::NEG_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003209 work_line_->CheckUnaryOp(this, inst, reg_types_.Float(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07003210 break;
3211 case Instruction::NEG_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07003212 work_line_->CheckUnaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003213 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07003214 break;
3215 case Instruction::INT_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07003216 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003217 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003218 break;
3219 case Instruction::INT_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003220 work_line_->CheckUnaryOp(this, inst, reg_types_.Float(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003221 break;
3222 case Instruction::INT_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07003223 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003224 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003225 break;
3226 case Instruction::LONG_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003227 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003228 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07003229 break;
3230 case Instruction::LONG_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003231 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003232 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07003233 break;
3234 case Instruction::LONG_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07003235 work_line_->CheckUnaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003236 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07003237 break;
3238 case Instruction::FLOAT_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003239 work_line_->CheckUnaryOp(this, inst, reg_types_.Integer(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07003240 break;
3241 case Instruction::FLOAT_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07003242 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003243 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07003244 break;
3245 case Instruction::FLOAT_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07003246 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003247 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07003248 break;
3249 case Instruction::DOUBLE_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003250 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003251 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07003252 break;
3253 case Instruction::DOUBLE_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07003254 work_line_->CheckUnaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003255 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07003256 break;
3257 case Instruction::DOUBLE_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003258 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003259 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07003260 break;
3261 case Instruction::INT_TO_BYTE:
Ian Rogers7b078e82014-09-10 14:44:24 -07003262 work_line_->CheckUnaryOp(this, inst, reg_types_.Byte(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003263 break;
3264 case Instruction::INT_TO_CHAR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003265 work_line_->CheckUnaryOp(this, inst, reg_types_.Char(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003266 break;
3267 case Instruction::INT_TO_SHORT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003268 work_line_->CheckUnaryOp(this, inst, reg_types_.Short(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003269 break;
3270
3271 case Instruction::ADD_INT:
3272 case Instruction::SUB_INT:
3273 case Instruction::MUL_INT:
3274 case Instruction::REM_INT:
3275 case Instruction::DIV_INT:
3276 case Instruction::SHL_INT:
3277 case Instruction::SHR_INT:
3278 case Instruction::USHR_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003279 work_line_->CheckBinaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003280 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07003281 break;
3282 case Instruction::AND_INT:
3283 case Instruction::OR_INT:
3284 case Instruction::XOR_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003285 work_line_->CheckBinaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003286 reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07003287 break;
3288 case Instruction::ADD_LONG:
3289 case Instruction::SUB_LONG:
3290 case Instruction::MUL_LONG:
3291 case Instruction::DIV_LONG:
3292 case Instruction::REM_LONG:
3293 case Instruction::AND_LONG:
3294 case Instruction::OR_LONG:
3295 case Instruction::XOR_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07003296 work_line_->CheckBinaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003297 reg_types_.LongLo(), reg_types_.LongHi(),
3298 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07003299 break;
3300 case Instruction::SHL_LONG:
3301 case Instruction::SHR_LONG:
3302 case Instruction::USHR_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07003303 /* shift distance is Int, making these different from other binary operations */
Ian Rogers7b078e82014-09-10 14:44:24 -07003304 work_line_->CheckBinaryOpWideShift(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003305 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003306 break;
3307 case Instruction::ADD_FLOAT:
3308 case Instruction::SUB_FLOAT:
3309 case Instruction::MUL_FLOAT:
3310 case Instruction::DIV_FLOAT:
3311 case Instruction::REM_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003312 work_line_->CheckBinaryOp(this, inst, reg_types_.Float(), reg_types_.Float(),
3313 reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07003314 break;
3315 case Instruction::ADD_DOUBLE:
3316 case Instruction::SUB_DOUBLE:
3317 case Instruction::MUL_DOUBLE:
3318 case Instruction::DIV_DOUBLE:
3319 case Instruction::REM_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07003320 work_line_->CheckBinaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003321 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
3322 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07003323 break;
3324 case Instruction::ADD_INT_2ADDR:
3325 case Instruction::SUB_INT_2ADDR:
3326 case Instruction::MUL_INT_2ADDR:
3327 case Instruction::REM_INT_2ADDR:
3328 case Instruction::SHL_INT_2ADDR:
3329 case Instruction::SHR_INT_2ADDR:
3330 case Instruction::USHR_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003331 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
3332 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07003333 break;
3334 case Instruction::AND_INT_2ADDR:
3335 case Instruction::OR_INT_2ADDR:
3336 case Instruction::XOR_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003337 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
3338 reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07003339 break;
3340 case Instruction::DIV_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003341 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
3342 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07003343 break;
3344 case Instruction::ADD_LONG_2ADDR:
3345 case Instruction::SUB_LONG_2ADDR:
3346 case Instruction::MUL_LONG_2ADDR:
3347 case Instruction::DIV_LONG_2ADDR:
3348 case Instruction::REM_LONG_2ADDR:
3349 case Instruction::AND_LONG_2ADDR:
3350 case Instruction::OR_LONG_2ADDR:
3351 case Instruction::XOR_LONG_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003352 work_line_->CheckBinaryOp2addrWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003353 reg_types_.LongLo(), reg_types_.LongHi(),
3354 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07003355 break;
3356 case Instruction::SHL_LONG_2ADDR:
3357 case Instruction::SHR_LONG_2ADDR:
3358 case Instruction::USHR_LONG_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003359 work_line_->CheckBinaryOp2addrWideShift(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003360 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003361 break;
3362 case Instruction::ADD_FLOAT_2ADDR:
3363 case Instruction::SUB_FLOAT_2ADDR:
3364 case Instruction::MUL_FLOAT_2ADDR:
3365 case Instruction::DIV_FLOAT_2ADDR:
3366 case Instruction::REM_FLOAT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003367 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Float(), reg_types_.Float(),
3368 reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07003369 break;
3370 case Instruction::ADD_DOUBLE_2ADDR:
3371 case Instruction::SUB_DOUBLE_2ADDR:
3372 case Instruction::MUL_DOUBLE_2ADDR:
3373 case Instruction::DIV_DOUBLE_2ADDR:
3374 case Instruction::REM_DOUBLE_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003375 work_line_->CheckBinaryOp2addrWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003376 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
3377 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07003378 break;
3379 case Instruction::ADD_INT_LIT16:
Ian Rogersf72a11d2014-10-30 15:41:08 -07003380 case Instruction::RSUB_INT_LIT16:
jeffhaobdb76512011-09-07 11:43:16 -07003381 case Instruction::MUL_INT_LIT16:
3382 case Instruction::DIV_INT_LIT16:
3383 case Instruction::REM_INT_LIT16:
Ian Rogers7b078e82014-09-10 14:44:24 -07003384 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), false,
3385 true);
jeffhaobdb76512011-09-07 11:43:16 -07003386 break;
3387 case Instruction::AND_INT_LIT16:
3388 case Instruction::OR_INT_LIT16:
3389 case Instruction::XOR_INT_LIT16:
Ian Rogers7b078e82014-09-10 14:44:24 -07003390 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), true,
3391 true);
jeffhaobdb76512011-09-07 11:43:16 -07003392 break;
3393 case Instruction::ADD_INT_LIT8:
3394 case Instruction::RSUB_INT_LIT8:
3395 case Instruction::MUL_INT_LIT8:
3396 case Instruction::DIV_INT_LIT8:
3397 case Instruction::REM_INT_LIT8:
3398 case Instruction::SHL_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07003399 case Instruction::SHR_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07003400 case Instruction::USHR_INT_LIT8:
Ian Rogers7b078e82014-09-10 14:44:24 -07003401 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), false,
3402 false);
jeffhaobdb76512011-09-07 11:43:16 -07003403 break;
3404 case Instruction::AND_INT_LIT8:
3405 case Instruction::OR_INT_LIT8:
3406 case Instruction::XOR_INT_LIT8:
Ian Rogers7b078e82014-09-10 14:44:24 -07003407 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), true,
3408 false);
jeffhaobdb76512011-09-07 11:43:16 -07003409 break;
3410
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003411 // Special instructions.
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07003412 case Instruction::RETURN_VOID_NO_BARRIER:
3413 if (IsConstructor() && !IsStatic()) {
3414 auto& declaring_class = GetDeclaringClass();
Andreas Gampe68df3202015-06-22 11:35:46 -07003415 if (declaring_class.IsUnresolvedReference()) {
3416 // We must iterate over the fields, even if we cannot use mirror classes to do so. Do it
3417 // manually over the underlying dex file.
3418 uint32_t first_index = GetFirstFinalInstanceFieldIndex(*dex_file_,
3419 dex_file_->GetMethodId(dex_method_idx_).class_idx_);
Andreas Gampee2abbc62017-09-15 11:59:26 -07003420 if (first_index != dex::kDexNoIndex) {
Andreas Gampe68df3202015-06-22 11:35:46 -07003421 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void-no-barrier not expected for field "
3422 << first_index;
3423 }
3424 break;
3425 }
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07003426 auto* klass = declaring_class.GetClass();
3427 for (uint32_t i = 0, num_fields = klass->NumInstanceFields(); i < num_fields; ++i) {
3428 if (klass->GetInstanceField(i)->IsFinal()) {
Mathieu Chartiere86deef2015-03-19 13:43:37 -07003429 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void-no-barrier not expected for "
David Sehr709b0702016-10-13 09:12:37 -07003430 << klass->GetInstanceField(i)->PrettyField();
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07003431 break;
3432 }
3433 }
Sebastien Hertzcc10e0e2013-06-28 14:24:48 +02003434 }
Andreas Gampeb2917962015-07-31 13:36:10 -07003435 // Handle this like a RETURN_VOID now. Code is duplicated to separate standard from
3436 // quickened opcodes (otherwise this could be a fall-through).
3437 if (!IsConstructor()) {
3438 if (!GetMethodReturnType().IsConflict()) {
3439 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void not expected";
3440 }
3441 }
Sebastien Hertzcc10e0e2013-06-28 14:24:48 +02003442 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003443 // Note: the following instructions encode offsets derived from class linking.
Neil Fuller0e844392016-09-08 13:43:31 +01003444 // As such they use Class*/Field*/Executable* as these offsets only have
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003445 // meaning if the class linking and resolution were successful.
3446 case Instruction::IGET_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003447 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003448 break;
3449 case Instruction::IGET_WIDE_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003450 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003451 break;
3452 case Instruction::IGET_OBJECT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003453 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003454 break;
Mathieu Chartierffc605c2014-12-10 10:35:44 -08003455 case Instruction::IGET_BOOLEAN_QUICK:
3456 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true);
3457 break;
3458 case Instruction::IGET_BYTE_QUICK:
3459 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true);
3460 break;
3461 case Instruction::IGET_CHAR_QUICK:
3462 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true);
3463 break;
3464 case Instruction::IGET_SHORT_QUICK:
3465 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true);
3466 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003467 case Instruction::IPUT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003468 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003469 break;
Fred Shih37f05ef2014-07-16 18:38:08 -07003470 case Instruction::IPUT_BOOLEAN_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003471 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07003472 break;
3473 case Instruction::IPUT_BYTE_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003474 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07003475 break;
3476 case Instruction::IPUT_CHAR_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003477 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07003478 break;
3479 case Instruction::IPUT_SHORT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003480 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07003481 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003482 case Instruction::IPUT_WIDE_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003483 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003484 break;
3485 case Instruction::IPUT_OBJECT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003486 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003487 break;
3488 case Instruction::INVOKE_VIRTUAL_QUICK:
3489 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: {
3490 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
Mathieu Chartiere401d142015-04-22 13:56:20 -07003491 ArtMethod* called_method = VerifyInvokeVirtualQuickArgs(inst, is_range);
Ian Rogers7b078e82014-09-10 14:44:24 -07003492 if (called_method != nullptr) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003493 const char* descriptor = called_method->GetReturnTypeDescriptor();
Ian Rogersd8f69b02014-09-10 21:43:52 +00003494 const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003495 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003496 work_line_->SetResultRegisterType(this, return_type);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003497 } else {
3498 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
3499 }
3500 just_set_result = true;
3501 }
3502 break;
3503 }
3504
Ian Rogersd81871c2011-10-03 13:57:23 -07003505 /* These should never appear during verification. */
Mathieu Chartierffc605c2014-12-10 10:35:44 -08003506 case Instruction::UNUSED_3E ... Instruction::UNUSED_43:
Narayan Kamath8ec3bd22016-08-03 12:46:23 +01003507 case Instruction::UNUSED_F3 ... Instruction::UNUSED_F9:
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003508 case Instruction::UNUSED_79:
3509 case Instruction::UNUSED_7A:
jeffhaod5347e02012-03-22 17:25:05 -07003510 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Unexpected opcode " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07003511 break;
3512
3513 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003514 * DO NOT add a "default" clause here. Without it the compiler will
jeffhaobdb76512011-09-07 11:43:16 -07003515 * complain if an instruction is missing (which is desirable).
3516 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003517 } // end - switch (dec_insn.opcode)
jeffhaobdb76512011-09-07 11:43:16 -07003518
Ian Rogersad0b3a32012-04-16 14:50:24 -07003519 if (have_pending_hard_failure_) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08003520 if (Runtime::Current()->IsAotCompiler()) {
3521 /* When AOT compiling, check that the last failure is a hard failure */
Andreas Gampeb588f4c2015-05-26 13:35:39 -07003522 if (failures_[failures_.size() - 1] != VERIFY_ERROR_BAD_CLASS_HARD) {
3523 LOG(ERROR) << "Pending failures:";
3524 for (auto& error : failures_) {
3525 LOG(ERROR) << error;
3526 }
3527 for (auto& error_msg : failure_messages_) {
3528 LOG(ERROR) << error_msg->str();
3529 }
3530 LOG(FATAL) << "Pending hard failure, but last failure not hard.";
3531 }
Ian Rogerse1758fe2012-04-19 11:31:15 -07003532 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003533 /* immediate failure, reject class */
3534 info_messages_ << "Rejecting opcode " << inst->DumpString(dex_file_);
3535 return false;
jeffhaofaf459e2012-08-31 15:32:47 -07003536 } else if (have_pending_runtime_throw_failure_) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07003537 /* checking interpreter will throw, mark following code as unreachable */
jeffhaofaf459e2012-08-31 15:32:47 -07003538 opcode_flags = Instruction::kThrow;
Andreas Gampea727e372015-08-25 09:22:37 -07003539 // Note: the flag must be reset as it is only global to decouple Fail and is semantically per
3540 // instruction. However, RETURN checking may throw LOCKING errors, so we clear at the
3541 // very end.
jeffhaobdb76512011-09-07 11:43:16 -07003542 }
jeffhaobdb76512011-09-07 11:43:16 -07003543 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003544 * If we didn't just set the result register, clear it out. This ensures that you can only use
3545 * "move-result" immediately after the result is set. (We could check this statically, but it's
3546 * not expensive and it makes our debugging output cleaner.)
jeffhaobdb76512011-09-07 11:43:16 -07003547 */
3548 if (!just_set_result) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003549 work_line_->SetResultTypeToUnknown(this);
jeffhaobdb76512011-09-07 11:43:16 -07003550 }
3551
jeffhaobdb76512011-09-07 11:43:16 -07003552 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003553 * Handle "branch". Tag the branch target.
jeffhaobdb76512011-09-07 11:43:16 -07003554 *
3555 * NOTE: instructions like Instruction::EQZ provide information about the
jeffhaod1f0fde2011-09-08 17:25:33 -07003556 * state of the register when the branch is taken or not taken. For example,
jeffhaobdb76512011-09-07 11:43:16 -07003557 * somebody could get a reference field, check it for zero, and if the
3558 * branch is taken immediately store that register in a boolean field
jeffhaod1f0fde2011-09-08 17:25:33 -07003559 * since the value is known to be zero. We do not currently account for
jeffhaobdb76512011-09-07 11:43:16 -07003560 * that, and will reject the code.
3561 *
3562 * TODO: avoid re-fetching the branch target
3563 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003564 if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07003565 bool isConditional, selfOkay;
Ian Rogersd81871c2011-10-03 13:57:23 -07003566 if (!GetBranchOffset(work_insn_idx_, &branch_target, &isConditional, &selfOkay)) {
jeffhaobdb76512011-09-07 11:43:16 -07003567 /* should never happen after static verification */
jeffhaod5347e02012-03-22 17:25:05 -07003568 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad branch";
jeffhaobdb76512011-09-07 11:43:16 -07003569 return false;
3570 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08003571 DCHECK_EQ(isConditional, (opcode_flags & Instruction::kContinue) != 0);
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08003572 if (!CheckNotMoveExceptionOrMoveResult(code_item_accessor_.Insns(),
3573 work_insn_idx_ + branch_target)) {
jeffhaobdb76512011-09-07 11:43:16 -07003574 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07003575 }
jeffhaobdb76512011-09-07 11:43:16 -07003576 /* update branch target, set "changed" if appropriate */
Mathieu Chartierde40d472015-10-15 17:47:48 -07003577 if (nullptr != branch_line) {
Ian Rogersebbdd872014-07-07 23:53:08 -07003578 if (!UpdateRegisters(work_insn_idx_ + branch_target, branch_line.get(), false)) {
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003579 return false;
3580 }
3581 } else {
Ian Rogersebbdd872014-07-07 23:53:08 -07003582 if (!UpdateRegisters(work_insn_idx_ + branch_target, work_line_.get(), false)) {
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003583 return false;
3584 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003585 }
jeffhaobdb76512011-09-07 11:43:16 -07003586 }
3587
3588 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003589 * Handle "switch". Tag all possible branch targets.
jeffhaobdb76512011-09-07 11:43:16 -07003590 *
3591 * We've already verified that the table is structurally sound, so we
3592 * just need to walk through and tag the targets.
3593 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003594 if ((opcode_flags & Instruction::kSwitch) != 0) {
Andreas Gampe53de99c2015-08-17 13:43:55 -07003595 int offset_to_switch = insns[1] | (static_cast<int32_t>(insns[2]) << 16);
jeffhaobdb76512011-09-07 11:43:16 -07003596 const uint16_t* switch_insns = insns + offset_to_switch;
3597 int switch_count = switch_insns[1];
3598 int offset_to_targets, targ;
3599
3600 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
3601 /* 0 = sig, 1 = count, 2/3 = first key */
3602 offset_to_targets = 4;
3603 } else {
3604 /* 0 = sig, 1 = count, 2..count * 2 = keys */
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07003605 DCHECK((*insns & 0xff) == Instruction::SPARSE_SWITCH);
jeffhaobdb76512011-09-07 11:43:16 -07003606 offset_to_targets = 2 + 2 * switch_count;
3607 }
3608
3609 /* verify each switch target */
3610 for (targ = 0; targ < switch_count; targ++) {
3611 int offset;
3612 uint32_t abs_offset;
3613
3614 /* offsets are 32-bit, and only partly endian-swapped */
3615 offset = switch_insns[offset_to_targets + targ * 2] |
Andreas Gampe53de99c2015-08-17 13:43:55 -07003616 (static_cast<int32_t>(switch_insns[offset_to_targets + targ * 2 + 1]) << 16);
Ian Rogersd81871c2011-10-03 13:57:23 -07003617 abs_offset = work_insn_idx_ + offset;
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08003618 DCHECK_LT(abs_offset, code_item_accessor_.InsnsSizeInCodeUnits());
3619 if (!CheckNotMoveExceptionOrMoveResult(code_item_accessor_.Insns(), abs_offset)) {
jeffhaobdb76512011-09-07 11:43:16 -07003620 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07003621 }
Ian Rogersebbdd872014-07-07 23:53:08 -07003622 if (!UpdateRegisters(abs_offset, work_line_.get(), false)) {
jeffhaobdb76512011-09-07 11:43:16 -07003623 return false;
Ian Rogersebbdd872014-07-07 23:53:08 -07003624 }
jeffhaobdb76512011-09-07 11:43:16 -07003625 }
3626 }
3627
3628 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003629 * Handle instructions that can throw and that are sitting in a "try" block. (If they're not in a
3630 * "try" block when they throw, control transfers out of the method.)
jeffhaobdb76512011-09-07 11:43:16 -07003631 */
Mathieu Chartierde40d472015-10-15 17:47:48 -07003632 if ((opcode_flags & Instruction::kThrow) != 0 && GetInstructionFlags(work_insn_idx_).IsInTry()) {
Andreas Gampef91baf12014-07-18 15:41:00 -07003633 bool has_catch_all_handler = false;
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08003634 const DexFile::TryItem* try_item = code_item_accessor_.FindTryItem(work_insn_idx_);
3635 CHECK(try_item != nullptr);
Mathieu Chartierdc578c72017-12-27 11:51:45 -08003636 CatchHandlerIterator iterator(code_item_accessor_, *try_item);
jeffhaobdb76512011-09-07 11:43:16 -07003637
Andreas Gampef91baf12014-07-18 15:41:00 -07003638 // Need the linker to try and resolve the handled class to check if it's Throwable.
3639 ClassLinker* linker = Runtime::Current()->GetClassLinker();
3640
Ian Rogers0571d352011-11-03 19:51:38 -07003641 for (; iterator.HasNext(); iterator.Next()) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08003642 dex::TypeIndex handler_type_idx = iterator.GetHandlerTypeIndex();
3643 if (!handler_type_idx.IsValid()) {
Andreas Gampef91baf12014-07-18 15:41:00 -07003644 has_catch_all_handler = true;
3645 } else {
3646 // It is also a catch-all if it is java.lang.Throwable.
Vladimir Marko28e012a2017-12-07 11:22:59 +00003647 ObjPtr<mirror::Class> klass =
Vladimir Marko666ee3d2017-12-11 18:37:36 +00003648 linker->ResolveType(handler_type_idx, dex_cache_, class_loader_);
Andreas Gampef91baf12014-07-18 15:41:00 -07003649 if (klass != nullptr) {
3650 if (klass == mirror::Throwable::GetJavaLangThrowable()) {
3651 has_catch_all_handler = true;
3652 }
3653 } else {
3654 // Clear exception.
Ian Rogers7b078e82014-09-10 14:44:24 -07003655 DCHECK(self_->IsExceptionPending());
3656 self_->ClearException();
Andreas Gampef91baf12014-07-18 15:41:00 -07003657 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003658 }
jeffhaobdb76512011-09-07 11:43:16 -07003659 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003660 * Merge registers into the "catch" block. We want to use the "savedRegs" rather than
3661 * "work_regs", because at runtime the exception will be thrown before the instruction
3662 * modifies any registers.
jeffhaobdb76512011-09-07 11:43:16 -07003663 */
Ian Rogersebbdd872014-07-07 23:53:08 -07003664 if (!UpdateRegisters(iterator.GetHandlerAddress(), saved_line_.get(), false)) {
jeffhaobdb76512011-09-07 11:43:16 -07003665 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07003666 }
jeffhaobdb76512011-09-07 11:43:16 -07003667 }
3668
3669 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003670 * If the monitor stack depth is nonzero, there must be a "catch all" handler for this
3671 * instruction. This does apply to monitor-exit because of async exception handling.
jeffhaobdb76512011-09-07 11:43:16 -07003672 */
Andreas Gampef91baf12014-07-18 15:41:00 -07003673 if (work_line_->MonitorStackDepth() > 0 && !has_catch_all_handler) {
jeffhaobdb76512011-09-07 11:43:16 -07003674 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003675 * The state in work_line reflects the post-execution state. If the current instruction is a
3676 * monitor-enter and the monitor stack was empty, we don't need a catch-all (if it throws,
jeffhaobdb76512011-09-07 11:43:16 -07003677 * it will do so before grabbing the lock).
3678 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02003679 if (inst->Opcode() != Instruction::MONITOR_ENTER || work_line_->MonitorStackDepth() != 1) {
jeffhaod5347e02012-03-22 17:25:05 -07003680 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
Ian Rogersd81871c2011-10-03 13:57:23 -07003681 << "expected to be within a catch-all for an instruction where a monitor is held";
jeffhaobdb76512011-09-07 11:43:16 -07003682 return false;
3683 }
3684 }
3685 }
3686
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003687 /* Handle "continue". Tag the next consecutive instruction.
3688 * Note: Keep the code handling "continue" case below the "branch" and "switch" cases,
3689 * because it changes work_line_ when performing peephole optimization
3690 * and this change should not be used in those cases.
3691 */
Ian Rogers6d376ae2013-07-23 15:12:40 -07003692 if ((opcode_flags & Instruction::kContinue) != 0) {
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08003693 DCHECK_EQ(&code_item_accessor_.InstructionAt(work_insn_idx_), inst);
Ian Rogers7b078e82014-09-10 14:44:24 -07003694 uint32_t next_insn_idx = work_insn_idx_ + inst->SizeInCodeUnits();
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08003695 if (next_insn_idx >= code_item_accessor_.InsnsSizeInCodeUnits()) {
Ian Rogers6d376ae2013-07-23 15:12:40 -07003696 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Execution can walk off end of code area";
3697 return false;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003698 }
Ian Rogers6d376ae2013-07-23 15:12:40 -07003699 // The only way to get to a move-exception instruction is to get thrown there. Make sure the
3700 // next instruction isn't one.
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08003701 if (!CheckNotMoveException(code_item_accessor_.Insns(), next_insn_idx)) {
Ian Rogers6d376ae2013-07-23 15:12:40 -07003702 return false;
3703 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07003704 if (nullptr != fallthrough_line) {
Ian Rogers6d376ae2013-07-23 15:12:40 -07003705 // Make workline consistent with fallthrough computed from peephole optimization.
3706 work_line_->CopyFromLine(fallthrough_line.get());
3707 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07003708 if (GetInstructionFlags(next_insn_idx).IsReturn()) {
Ian Rogersb8c78592013-07-25 23:52:52 +00003709 // For returns we only care about the operand to the return, all other registers are dead.
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08003710 const Instruction* ret_inst = &code_item_accessor_.InstructionAt(next_insn_idx);
Andreas Gampea727e372015-08-25 09:22:37 -07003711 AdjustReturnLine(this, ret_inst, work_line_.get());
Ian Rogersb8c78592013-07-25 23:52:52 +00003712 }
Ian Rogers6d376ae2013-07-23 15:12:40 -07003713 RegisterLine* next_line = reg_table_.GetLine(next_insn_idx);
Ian Rogers7b078e82014-09-10 14:44:24 -07003714 if (next_line != nullptr) {
Ian Rogersebbdd872014-07-07 23:53:08 -07003715 // Merge registers into what we have for the next instruction, and set the "changed" flag if
3716 // needed. If the merge changes the state of the registers then the work line will be
3717 // updated.
3718 if (!UpdateRegisters(next_insn_idx, work_line_.get(), true)) {
Ian Rogers6d376ae2013-07-23 15:12:40 -07003719 return false;
3720 }
3721 } else {
3722 /*
3723 * We're not recording register data for the next instruction, so we don't know what the
3724 * prior state was. We have to assume that something has changed and re-evaluate it.
3725 */
Mathieu Chartierde40d472015-10-15 17:47:48 -07003726 GetInstructionFlags(next_insn_idx).SetChanged();
Ian Rogers6d376ae2013-07-23 15:12:40 -07003727 }
3728 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003729
jeffhaod1f0fde2011-09-08 17:25:33 -07003730 /* If we're returning from the method, make sure monitor stack is empty. */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003731 if ((opcode_flags & Instruction::kReturn) != 0) {
Andreas Gampea727e372015-08-25 09:22:37 -07003732 work_line_->VerifyMonitorStackEmpty(this);
jeffhaobdb76512011-09-07 11:43:16 -07003733 }
3734
3735 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003736 * Update start_guess. Advance to the next instruction of that's
3737 * possible, otherwise use the branch target if one was found. If
jeffhaobdb76512011-09-07 11:43:16 -07003738 * neither of those exists we're in a return or throw; leave start_guess
3739 * alone and let the caller sort it out.
3740 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003741 if ((opcode_flags & Instruction::kContinue) != 0) {
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08003742 DCHECK_EQ(&code_item_accessor_.InstructionAt(work_insn_idx_), inst);
Ian Rogers7b078e82014-09-10 14:44:24 -07003743 *start_guess = work_insn_idx_ + inst->SizeInCodeUnits();
Elliott Hughesadb8c672012-03-06 16:49:32 -08003744 } else if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07003745 /* we're still okay if branch_target is zero */
Ian Rogersd81871c2011-10-03 13:57:23 -07003746 *start_guess = work_insn_idx_ + branch_target;
jeffhaobdb76512011-09-07 11:43:16 -07003747 }
3748
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08003749 DCHECK_LT(*start_guess, code_item_accessor_.InsnsSizeInCodeUnits());
Mathieu Chartierde40d472015-10-15 17:47:48 -07003750 DCHECK(GetInstructionFlags(*start_guess).IsOpcode());
jeffhaobdb76512011-09-07 11:43:16 -07003751
Andreas Gampea727e372015-08-25 09:22:37 -07003752 if (have_pending_runtime_throw_failure_) {
3753 have_any_pending_runtime_throw_failure_ = true;
3754 // Reset the pending_runtime_throw flag now.
3755 have_pending_runtime_throw_failure_ = false;
3756 }
3757
jeffhaobdb76512011-09-07 11:43:16 -07003758 return true;
Brian Carlstrom7934ac22013-07-26 10:54:15 -07003759} // NOLINT(readability/fn_size)
jeffhaobdb76512011-09-07 11:43:16 -07003760
Mathieu Chartierde40d472015-10-15 17:47:48 -07003761void MethodVerifier::UninstantiableError(const char* descriptor) {
3762 Fail(VerifyError::VERIFY_ERROR_NO_CLASS) << "Could not create precise reference for "
3763 << "non-instantiable klass " << descriptor;
3764}
3765
Vladimir Marko28e012a2017-12-07 11:22:59 +00003766inline bool MethodVerifier::IsInstantiableOrPrimitive(ObjPtr<mirror::Class> klass) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07003767 return klass->IsInstantiable() || klass->IsPrimitive();
3768}
3769
Andreas Gampe98be1a92017-08-28 08:25:45 -07003770template <MethodVerifier::CheckAccess C>
3771const RegType& MethodVerifier::ResolveClass(dex::TypeIndex class_idx) {
Vladimir Marko666ee3d2017-12-11 18:37:36 +00003772 ClassLinker* linker = Runtime::Current()->GetClassLinker();
Vladimir Marko28e012a2017-12-07 11:22:59 +00003773 ObjPtr<mirror::Class> klass = can_load_classes_
Vladimir Marko666ee3d2017-12-11 18:37:36 +00003774 ? linker->ResolveType(class_idx, dex_cache_, class_loader_)
3775 : linker->LookupResolvedType(class_idx, dex_cache_.Get(), class_loader_.Get());
Vladimir Marko8d6768d2017-03-14 10:13:21 +00003776 if (can_load_classes_ && klass == nullptr) {
3777 DCHECK(self_->IsExceptionPending());
3778 self_->ClearException();
3779 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07003780 const RegType* result = nullptr;
Vladimir Marko9cb0c462017-04-21 13:31:41 +01003781 if (klass != nullptr) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07003782 bool precise = klass->CannotBeAssignedFromOtherTypes();
3783 if (precise && !IsInstantiableOrPrimitive(klass)) {
3784 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
3785 UninstantiableError(descriptor);
3786 precise = false;
3787 }
Vladimir Marko28e012a2017-12-07 11:22:59 +00003788 result = reg_types_.FindClass(klass.Ptr(), precise);
Mathieu Chartierde40d472015-10-15 17:47:48 -07003789 if (result == nullptr) {
3790 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
Vladimir Marko28e012a2017-12-07 11:22:59 +00003791 result = reg_types_.InsertClass(descriptor, klass.Ptr(), precise);
Mathieu Chartierde40d472015-10-15 17:47:48 -07003792 }
3793 } else {
3794 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
3795 result = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003796 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07003797 DCHECK(result != nullptr);
3798 if (result->IsConflict()) {
3799 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
3800 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "accessing broken descriptor '" << descriptor
3801 << "' in " << GetDeclaringClass();
3802 return *result;
3803 }
David Brazdilca3c8c32016-09-06 14:04:48 +01003804
3805 // Record result of class resolution attempt.
Vladimir Marko28e012a2017-12-07 11:22:59 +00003806 VerifierDeps::MaybeRecordClassResolution(*dex_file_, class_idx, klass.Ptr());
David Brazdilca3c8c32016-09-06 14:04:48 +01003807
Andreas Gampe629be512017-08-25 17:09:32 -07003808 // If requested, check if access is allowed. Unresolved types are included in this check, as the
3809 // interpreter only tests whether access is allowed when a class is not pre-verified and runs in
3810 // the access-checks interpreter. If result is primitive, skip the access check.
3811 //
3812 // Note: we do this for unresolved classes to trigger re-verification at runtime.
3813 if (C == CheckAccess::kYes && result->IsNonZeroReferenceTypes()) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07003814 const RegType& referrer = GetDeclaringClass();
Andreas Gampe629be512017-08-25 17:09:32 -07003815 if (!referrer.CanAccess(*result)) {
3816 Fail(VERIFY_ERROR_ACCESS_CLASS) << "(possibly) illegal class access: '"
Nicolas Geoffray4e868fa2017-04-21 17:16:44 +01003817 << referrer << "' -> '" << *result << "'";
Mathieu Chartierde40d472015-10-15 17:47:48 -07003818 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07003819 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07003820 return *result;
Ian Rogersd81871c2011-10-03 13:57:23 -07003821}
3822
Andreas Gampedd309072017-08-31 07:47:37 -07003823// Instantiate ResolveClass variants. This is required as the -inl file has a function with a call
3824// to ResolveClass, and compilers may decide to inline, requiring a symbol.
Andreas Gampeae0b1f42017-08-30 17:36:26 -07003825template const RegType& MethodVerifier::ResolveClass<MethodVerifier::CheckAccess::kNo>(
3826 dex::TypeIndex class_idx);
3827template const RegType& MethodVerifier::ResolveClass<MethodVerifier::CheckAccess::kYes>(
3828 dex::TypeIndex class_idx);
3829
Ian Rogersd8f69b02014-09-10 21:43:52 +00003830const RegType& MethodVerifier::GetCaughtExceptionType() {
Ian Rogers7b078e82014-09-10 14:44:24 -07003831 const RegType* common_super = nullptr;
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08003832 if (code_item_accessor_.TriesSize() != 0) {
3833 const uint8_t* handlers_ptr = code_item_accessor_.GetCatchHandlerData();
Ian Rogersd81871c2011-10-03 13:57:23 -07003834 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
3835 for (uint32_t i = 0; i < handlers_size; i++) {
Ian Rogers0571d352011-11-03 19:51:38 -07003836 CatchHandlerIterator iterator(handlers_ptr);
3837 for (; iterator.HasNext(); iterator.Next()) {
3838 if (iterator.GetHandlerAddress() == (uint32_t) work_insn_idx_) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08003839 if (!iterator.GetHandlerTypeIndex().IsValid()) {
Ian Rogersb4903572012-10-11 11:52:56 -07003840 common_super = &reg_types_.JavaLangThrowable(false);
Ian Rogersd81871c2011-10-03 13:57:23 -07003841 } else {
Andreas Gampe98be1a92017-08-28 08:25:45 -07003842 const RegType& exception =
3843 ResolveClass<CheckAccess::kYes>(iterator.GetHandlerTypeIndex());
David Brazdilca3c8c32016-09-06 14:04:48 +01003844 if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(exception, this)) {
David Brazdil68b5c0b2016-01-19 14:25:29 +00003845 DCHECK(!exception.IsUninitializedTypes()); // Comes from dex, shouldn't be uninit.
Jeff Haoc26a56c2013-11-04 12:00:47 -08003846 if (exception.IsUnresolvedTypes()) {
3847 // We don't know enough about the type. Fail here and let runtime handle it.
3848 Fail(VERIFY_ERROR_NO_CLASS) << "unresolved exception class " << exception;
3849 return exception;
3850 } else {
3851 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unexpected non-exception class " << exception;
3852 return reg_types_.Conflict();
3853 }
Jeff Haob878f212014-04-24 16:25:36 -07003854 } else if (common_super == nullptr) {
3855 common_super = &exception;
Ian Rogers28ad40d2011-10-27 15:19:26 -07003856 } else if (common_super->Equals(exception)) {
Ian Rogersc4762272012-02-01 15:55:55 -08003857 // odd case, but nothing to do
Ian Rogersd81871c2011-10-03 13:57:23 -07003858 } else {
David Brazdilca3c8c32016-09-06 14:04:48 +01003859 common_super = &common_super->Merge(exception, &reg_types_, this);
Andreas Gampe7c038102014-10-27 20:08:46 -07003860 if (FailOrAbort(this,
David Brazdilca3c8c32016-09-06 14:04:48 +01003861 reg_types_.JavaLangThrowable(false).IsAssignableFrom(
3862 *common_super, this),
Andreas Gampe7c038102014-10-27 20:08:46 -07003863 "java.lang.Throwable is not assignable-from common_super at ",
3864 work_insn_idx_)) {
3865 break;
3866 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003867 }
3868 }
3869 }
3870 }
Ian Rogers0571d352011-11-03 19:51:38 -07003871 handlers_ptr = iterator.EndDataPointer();
Ian Rogersd81871c2011-10-03 13:57:23 -07003872 }
3873 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003874 if (common_super == nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003875 /* no catch blocks, or no catches with classes we can find */
jeffhaod5347e02012-03-22 17:25:05 -07003876 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unable to find exception handler";
Ian Rogersad0b3a32012-04-16 14:50:24 -07003877 return reg_types_.Conflict();
Ian Rogersd81871c2011-10-03 13:57:23 -07003878 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07003879 return *common_super;
Ian Rogersd81871c2011-10-03 13:57:23 -07003880}
3881
Mathieu Chartiere401d142015-04-22 13:56:20 -07003882ArtMethod* MethodVerifier::ResolveMethodAndCheckAccess(
Alex Light7268d472016-01-20 15:50:01 -08003883 uint32_t dex_method_idx, MethodType method_type) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003884 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx);
Andreas Gampe98be1a92017-08-28 08:25:45 -07003885 const RegType& klass_type = ResolveClass<CheckAccess::kYes>(method_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003886 if (klass_type.IsConflict()) {
3887 std::string append(" in attempt to access method ");
3888 append += dex_file_->GetMethodName(method_id);
3889 AppendToLastFailMessage(append);
Ian Rogers7b078e82014-09-10 14:44:24 -07003890 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08003891 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003892 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003893 return nullptr; // Can't resolve Class so no more to do here
Ian Rogers90040192011-12-16 08:54:29 -08003894 }
Vladimir Markoba118822017-06-12 15:41:56 +01003895 ObjPtr<mirror::Class> klass = klass_type.GetClass();
Ian Rogersd8f69b02014-09-10 21:43:52 +00003896 const RegType& referrer = GetDeclaringClass();
Mathieu Chartiere401d142015-04-22 13:56:20 -07003897 auto* cl = Runtime::Current()->GetClassLinker();
3898 auto pointer_size = cl->GetImagePointerSize();
Andreas Gampe42ef8ab2015-12-03 17:27:32 -08003899
Mathieu Chartiere401d142015-04-22 13:56:20 -07003900 ArtMethod* res_method = dex_cache_->GetResolvedMethod(dex_method_idx, pointer_size);
Ian Rogers7b078e82014-09-10 14:44:24 -07003901 if (res_method == nullptr) {
Vladimir Markoba118822017-06-12 15:41:56 +01003902 // Try to find the method with the appropriate lookup for the klass type (interface or not).
3903 // If this lookup does not match `method_type`, errors shall be reported below.
3904 if (klass->IsInterface()) {
3905 res_method = klass->FindInterfaceMethod(dex_cache_.Get(), dex_method_idx, pointer_size);
David Brazdilca3c8c32016-09-06 14:04:48 +01003906 } else {
Vladimir Markoba118822017-06-12 15:41:56 +01003907 res_method = klass->FindClassMethod(dex_cache_.Get(), dex_method_idx, pointer_size);
Ian Rogersd81871c2011-10-03 13:57:23 -07003908 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003909 if (res_method != nullptr) {
Vladimir Markoba118822017-06-12 15:41:56 +01003910 dex_cache_->SetResolvedMethod(dex_method_idx, res_method, pointer_size);
Ian Rogersd81871c2011-10-03 13:57:23 -07003911 }
3912 }
David Brazdilca3c8c32016-09-06 14:04:48 +01003913
Vladimir Markoba118822017-06-12 15:41:56 +01003914 // Record result of method resolution attempt. The klass resolution has recorded whether
3915 // the class is an interface or not and therefore the type of the lookup performed above.
3916 // TODO: Maybe we should not record dependency if the invoke type does not match the lookup type.
3917 VerifierDeps::MaybeRecordMethodResolution(*dex_file_, dex_method_idx, res_method);
3918
Alex Lightafb66472017-08-01 09:54:49 -07003919 bool must_fail = false;
3920 // This is traditional and helps with screwy bytecode. It will tell you that, yes, a method
3921 // exists, but that it's called incorrectly. This significantly helps debugging, as locally it's
3922 // hard to see the differences.
3923 // If we don't have res_method here we must fail. Just use this bool to make sure of that with a
3924 // DCHECK.
Vladimir Markoba118822017-06-12 15:41:56 +01003925 if (res_method == nullptr) {
Alex Lightafb66472017-08-01 09:54:49 -07003926 must_fail = true;
Vladimir Markoba118822017-06-12 15:41:56 +01003927 // Try to find the method also with the other type for better error reporting below
3928 // but do not store such bogus lookup result in the DexCache or VerifierDeps.
3929 if (klass->IsInterface()) {
Alex Lightafb66472017-08-01 09:54:49 -07003930 // NB This is normally not really allowed but we want to get any static or private object
3931 // methods for error message purposes. This will never be returned.
3932 // TODO We might want to change the verifier to not require this.
Vladimir Markoba118822017-06-12 15:41:56 +01003933 res_method = klass->FindClassMethod(dex_cache_.Get(), dex_method_idx, pointer_size);
3934 } else {
3935 // If there was an interface method with the same signature,
3936 // we would have found it also in the "copied" methods.
3937 DCHECK(klass->FindInterfaceMethod(dex_cache_.Get(), dex_method_idx, pointer_size) == nullptr);
3938 }
3939 }
David Brazdilca3c8c32016-09-06 14:04:48 +01003940
3941 if (res_method == nullptr) {
3942 Fail(VERIFY_ERROR_NO_METHOD) << "couldn't find method "
David Sehr709b0702016-10-13 09:12:37 -07003943 << klass->PrettyDescriptor() << "."
David Brazdilca3c8c32016-09-06 14:04:48 +01003944 << dex_file_->GetMethodName(method_id) << " "
3945 << dex_file_->GetMethodSignature(method_id);
3946 return nullptr;
3947 }
3948
Ian Rogersd81871c2011-10-03 13:57:23 -07003949 // Make sure calls to constructors are "direct". There are additional restrictions but we don't
3950 // enforce them here.
3951 if (res_method->IsConstructor() && method_type != METHOD_DIRECT) {
jeffhaod5347e02012-03-22 17:25:05 -07003952 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting non-direct call to constructor "
David Sehr709b0702016-10-13 09:12:37 -07003953 << res_method->PrettyMethod();
Ian Rogers7b078e82014-09-10 14:44:24 -07003954 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003955 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003956 // Disallow any calls to class initializers.
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003957 if (res_method->IsClassInitializer()) {
jeffhaod5347e02012-03-22 17:25:05 -07003958 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting call to class initializer "
David Sehr709b0702016-10-13 09:12:37 -07003959 << res_method->PrettyMethod();
Ian Rogers7b078e82014-09-10 14:44:24 -07003960 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003961 }
Andreas Gampe42ef8ab2015-12-03 17:27:32 -08003962
3963 // Check that interface methods are static or match interface classes.
3964 // We only allow statics if we don't have default methods enabled.
3965 //
3966 // Note: this check must be after the initializer check, as those are required to fail a class,
3967 // while this check implies an IncompatibleClassChangeError.
3968 if (klass->IsInterface()) {
Alex Lightb55f1ac2016-04-12 15:50:55 -07003969 // methods called on interfaces should be invoke-interface, invoke-super, invoke-direct (if
Mathieu Chartierf6e31472017-12-28 13:32:08 -08003970 // default methods are supported for the dex file), or invoke-static.
Andreas Gampe42ef8ab2015-12-03 17:27:32 -08003971 if (method_type != METHOD_INTERFACE &&
Neil Fuller9724c632016-01-07 15:42:47 +00003972 method_type != METHOD_STATIC &&
Mathieu Chartierf6e31472017-12-28 13:32:08 -08003973 (!dex_file_->SupportsDefaultMethods() ||
Alex Lightb55f1ac2016-04-12 15:50:55 -07003974 method_type != METHOD_DIRECT) &&
Neil Fuller9724c632016-01-07 15:42:47 +00003975 method_type != METHOD_SUPER) {
Andreas Gampe42ef8ab2015-12-03 17:27:32 -08003976 Fail(VERIFY_ERROR_CLASS_CHANGE)
David Sehr709b0702016-10-13 09:12:37 -07003977 << "non-interface method " << dex_file_->PrettyMethod(dex_method_idx)
3978 << " is in an interface class " << klass->PrettyClass();
Andreas Gampe42ef8ab2015-12-03 17:27:32 -08003979 return nullptr;
3980 }
3981 } else {
3982 if (method_type == METHOD_INTERFACE) {
3983 Fail(VERIFY_ERROR_CLASS_CHANGE)
David Sehr709b0702016-10-13 09:12:37 -07003984 << "interface method " << dex_file_->PrettyMethod(dex_method_idx)
3985 << " is in a non-interface class " << klass->PrettyClass();
Andreas Gampe42ef8ab2015-12-03 17:27:32 -08003986 return nullptr;
3987 }
3988 }
3989
Alex Lightafb66472017-08-01 09:54:49 -07003990 // Check specifically for non-public object methods being provided for interface dispatch. This
3991 // can occur if we failed to find a method with FindInterfaceMethod but later find one with
3992 // FindClassMethod for error message use.
3993 if (method_type == METHOD_INTERFACE &&
3994 res_method->GetDeclaringClass()->IsObjectClass() &&
3995 !res_method->IsPublic()) {
3996 Fail(VERIFY_ERROR_NO_METHOD) << "invoke-interface " << klass->PrettyDescriptor() << "."
3997 << dex_file_->GetMethodName(method_id) << " "
3998 << dex_file_->GetMethodSignature(method_id) << " resolved to "
3999 << "non-public object method " << res_method->PrettyMethod() << " "
4000 << "but non-public Object methods are excluded from interface "
4001 << "method resolution.";
4002 return nullptr;
4003 }
jeffhao8cd6dda2012-02-22 10:15:34 -08004004 // Check if access is allowed.
Ian Rogersad0b3a32012-04-16 14:50:24 -07004005 if (!referrer.CanAccessMember(res_method->GetDeclaringClass(), res_method->GetAccessFlags())) {
David Sehr709b0702016-10-13 09:12:37 -07004006 Fail(VERIFY_ERROR_ACCESS_METHOD) << "illegal method access (call "
4007 << res_method->PrettyMethod()
Ian Rogersad0b3a32012-04-16 14:50:24 -07004008 << " from " << referrer << ")";
jeffhaob57e9522012-04-26 18:08:21 -07004009 return res_method;
jeffhao8cd6dda2012-02-22 10:15:34 -08004010 }
jeffhaode0d9c92012-02-27 13:58:13 -08004011 // Check that invoke-virtual and invoke-super are not used on private methods of the same class.
Alex Light7268d472016-01-20 15:50:01 -08004012 if (res_method->IsPrivate() && (method_type == METHOD_VIRTUAL || method_type == METHOD_SUPER)) {
jeffhaod5347e02012-03-22 17:25:05 -07004013 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invoke-super/virtual can't be used on private method "
David Sehr709b0702016-10-13 09:12:37 -07004014 << res_method->PrettyMethod();
Ian Rogers7b078e82014-09-10 14:44:24 -07004015 return nullptr;
jeffhaode0d9c92012-02-27 13:58:13 -08004016 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004017 // See if the method type implied by the invoke instruction matches the access flags for the
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004018 // target method. The flags for METHOD_POLYMORPHIC are based on there being precisely two
4019 // signature polymorphic methods supported by the run-time which are native methods with variable
4020 // arguments.
Brian Carlstrombe6fa5e2014-12-09 20:15:42 -08004021 if ((method_type == METHOD_DIRECT && (!res_method->IsDirect() || res_method->IsStatic())) ||
Ian Rogersd81871c2011-10-03 13:57:23 -07004022 (method_type == METHOD_STATIC && !res_method->IsStatic()) ||
Alex Light7268d472016-01-20 15:50:01 -08004023 ((method_type == METHOD_SUPER ||
4024 method_type == METHOD_VIRTUAL ||
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004025 method_type == METHOD_INTERFACE) && res_method->IsDirect()) ||
4026 ((method_type == METHOD_POLYMORPHIC) &&
4027 (!res_method->IsNative() || !res_method->IsVarargs()))) {
Ian Rogers2fc14272012-08-30 10:56:57 -07004028 Fail(VERIFY_ERROR_CLASS_CHANGE) << "invoke type (" << method_type << ") does not match method "
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004029 "type of " << res_method->PrettyMethod();
Ian Rogers7b078e82014-09-10 14:44:24 -07004030 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07004031 }
Alex Lightafb66472017-08-01 09:54:49 -07004032 // Make sure we weren't expecting to fail.
4033 DCHECK(!must_fail) << "invoke type (" << method_type << ")"
4034 << klass->PrettyDescriptor() << "."
4035 << dex_file_->GetMethodName(method_id) << " "
4036 << dex_file_->GetMethodSignature(method_id) << " unexpectedly resolved to "
4037 << res_method->PrettyMethod() << " without error. Initially this method was "
4038 << "not found so we were expecting to fail for some reason.";
jeffhao8cd6dda2012-02-22 10:15:34 -08004039 return res_method;
4040}
4041
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004042template <class T>
Mathieu Chartiere401d142015-04-22 13:56:20 -07004043ArtMethod* MethodVerifier::VerifyInvocationArgsFromIterator(
4044 T* it, const Instruction* inst, MethodType method_type, bool is_range, ArtMethod* res_method) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004045 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
4046 // match the call to the signature. Also, we might be calling through an abstract method
4047 // definition (which doesn't have register count values).
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004048 const size_t expected_args = inst->VRegA();
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004049 /* caught by static verifier */
4050 DCHECK(is_range || expected_args <= 5);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004051
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08004052 if (expected_args > code_item_accessor_.OutsSize()) {
Orion Hodson1cda7c22017-08-10 13:06:45 +01004053 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08004054 << ") exceeds outsSize ("
4055 << code_item_accessor_.OutsSize() << ")";
Orion Hodson1cda7c22017-08-10 13:06:45 +01004056 return nullptr;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004057 }
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004058
4059 /*
4060 * Check the "this" argument, which must be an instance of the class that declared the method.
4061 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
4062 * rigorous check here (which is okay since we have to do it at runtime).
4063 */
4064 if (method_type != METHOD_STATIC) {
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004065 const RegType& actual_arg_type = work_line_->GetInvocationThis(this, inst);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004066 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
4067 CHECK(have_pending_hard_failure_);
4068 return nullptr;
4069 }
David Brazdil68b5c0b2016-01-19 14:25:29 +00004070 bool is_init = false;
4071 if (actual_arg_type.IsUninitializedTypes()) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004072 if (res_method) {
4073 if (!res_method->IsConstructor()) {
4074 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
4075 return nullptr;
4076 }
4077 } else {
4078 // Check whether the name of the called method is "<init>"
4079 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Jeff Hao0d087272014-08-04 14:47:17 -07004080 if (strcmp(dex_file_->GetMethodName(dex_file_->GetMethodId(method_idx)), "<init>") != 0) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004081 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
4082 return nullptr;
4083 }
4084 }
David Brazdil68b5c0b2016-01-19 14:25:29 +00004085 is_init = true;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004086 }
David Brazdil68b5c0b2016-01-19 14:25:29 +00004087 const RegType& adjusted_type = is_init
4088 ? GetRegTypeCache()->FromUninitialized(actual_arg_type)
4089 : actual_arg_type;
Andreas Gampeeed3a5d2017-11-29 14:58:34 -08004090 if (method_type != METHOD_INTERFACE && !adjusted_type.IsZeroOrNull()) {
Ian Rogersd8f69b02014-09-10 21:43:52 +00004091 const RegType* res_method_class;
Andreas Gamped9e23012015-06-04 22:19:58 -07004092 // Miranda methods have the declaring interface as their declaring class, not the abstract
4093 // class. It would be wrong to use this for the type check (interface type checks are
4094 // postponed to runtime).
4095 if (res_method != nullptr && !res_method->IsMiranda()) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004096 mirror::Class* klass = res_method->GetDeclaringClass();
Ian Rogers1ff3c982014-08-12 02:30:58 -07004097 std::string temp;
Andreas Gampef23f33d2015-06-23 14:18:17 -07004098 res_method_class = &FromClass(klass->GetDescriptor(&temp), klass,
4099 klass->CannotBeAssignedFromOtherTypes());
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004100 } else {
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004101 const uint32_t method_idx = inst->VRegB();
Andreas Gampea5b09a62016-11-17 15:21:22 -08004102 const dex::TypeIndex class_idx = dex_file_->GetMethodId(method_idx).class_idx_;
Mathieu Chartierde40d472015-10-15 17:47:48 -07004103 res_method_class = &reg_types_.FromDescriptor(
4104 GetClassLoader(),
4105 dex_file_->StringByTypeIdx(class_idx),
4106 false);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004107 }
David Brazdilca3c8c32016-09-06 14:04:48 +01004108 if (!res_method_class->IsAssignableFrom(adjusted_type, this)) {
David Brazdil68b5c0b2016-01-19 14:25:29 +00004109 Fail(adjusted_type.IsUnresolvedTypes()
4110 ? VERIFY_ERROR_NO_CLASS
4111 : VERIFY_ERROR_BAD_CLASS_SOFT)
4112 << "'this' argument '" << actual_arg_type << "' not instance of '"
4113 << *res_method_class << "'";
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004114 // Continue on soft failures. We need to find possible hard failures to avoid problems in
4115 // the compiler.
4116 if (have_pending_hard_failure_) {
4117 return nullptr;
4118 }
4119 }
4120 }
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004121 }
4122
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004123 uint32_t arg[5];
4124 if (!is_range) {
4125 inst->GetVarArgs(arg);
4126 }
4127 uint32_t sig_registers = (method_type == METHOD_STATIC) ? 0 : 1;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004128 for ( ; it->HasNext(); it->Next()) {
4129 if (sig_registers >= expected_args) {
4130 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, expected " << inst->VRegA() <<
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004131 " argument registers, method signature has " << sig_registers + 1 << " or more";
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004132 return nullptr;
4133 }
4134
4135 const char* param_descriptor = it->GetDescriptor();
4136
4137 if (param_descriptor == nullptr) {
4138 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation because of missing signature "
4139 "component";
4140 return nullptr;
4141 }
4142
Ian Rogersd8f69b02014-09-10 21:43:52 +00004143 const RegType& reg_type = reg_types_.FromDescriptor(GetClassLoader(), param_descriptor, false);
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004144 uint32_t get_reg = is_range ? inst->VRegC() + static_cast<uint32_t>(sig_registers) :
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004145 arg[sig_registers];
4146 if (reg_type.IsIntegralTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004147 const RegType& src_type = work_line_->GetRegisterType(this, get_reg);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004148 if (!src_type.IsIntegralTypes()) {
4149 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register v" << get_reg << " has type " << src_type
4150 << " but expected " << reg_type;
Andreas Gampeb588f4c2015-05-26 13:35:39 -07004151 return nullptr;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004152 }
Andreas Gampeda9badb2015-06-05 20:22:12 -07004153 } else {
4154 if (!work_line_->VerifyRegisterType(this, get_reg, reg_type)) {
4155 // Continue on soft failures. We need to find possible hard failures to avoid problems in
4156 // the compiler.
4157 if (have_pending_hard_failure_) {
4158 return nullptr;
4159 }
4160 } else if (reg_type.IsLongOrDoubleTypes()) {
4161 // Check that registers are consecutive (for non-range invokes). Invokes are the only
4162 // instructions not specifying register pairs by the first component, but require them
4163 // nonetheless. Only check when there's an actual register in the parameters. If there's
4164 // none, this will fail below.
4165 if (!is_range && sig_registers + 1 < expected_args) {
4166 uint32_t second_reg = arg[sig_registers + 1];
4167 if (second_reg != get_reg + 1) {
4168 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, long or double parameter "
4169 "at index " << sig_registers << " is not a pair: " << get_reg << " + "
4170 << second_reg << ".";
4171 return nullptr;
4172 }
4173 }
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004174 }
4175 }
4176 sig_registers += reg_type.IsLongOrDoubleTypes() ? 2 : 1;
4177 }
4178 if (expected_args != sig_registers) {
4179 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, expected " << expected_args <<
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004180 " argument registers, method signature has " << sig_registers;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004181 return nullptr;
4182 }
4183 return res_method;
4184}
4185
4186void MethodVerifier::VerifyInvocationArgsUnresolvedMethod(const Instruction* inst,
4187 MethodType method_type,
4188 bool is_range) {
4189 // As the method may not have been resolved, make this static check against what we expect.
4190 // The main reason for this code block is to fail hard when we find an illegal use, e.g.,
4191 // wrong number of arguments or wrong primitive types, even if the method could not be resolved.
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004192 const uint32_t method_idx = inst->VRegB();
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004193 DexFileParameterIterator it(*dex_file_,
4194 dex_file_->GetProtoId(dex_file_->GetMethodId(method_idx).proto_idx_));
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004195 VerifyInvocationArgsFromIterator(&it, inst, method_type, is_range, nullptr);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004196}
4197
Orion Hodsonc069a302017-01-18 09:23:12 +00004198bool MethodVerifier::CheckCallSite(uint32_t call_site_idx) {
Orion Hodson3a842f52017-04-21 15:24:10 +01004199 if (call_site_idx >= dex_file_->NumCallSiteIds()) {
4200 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Bad call site id #" << call_site_idx
4201 << " >= " << dex_file_->NumCallSiteIds();
4202 return false;
4203 }
4204
Orion Hodsonc069a302017-01-18 09:23:12 +00004205 CallSiteArrayValueIterator it(*dex_file_, dex_file_->GetCallSiteId(call_site_idx));
4206 // Check essential arguments are provided. The dex file verifier has verified indicies of the
4207 // main values (method handle, name, method_type).
4208 if (it.Size() < 3) {
4209 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Call site #" << call_site_idx
4210 << " has too few arguments: "
4211 << it.Size() << "< 3";
4212 return false;
4213 }
4214
Orion Hodson3a842f52017-04-21 15:24:10 +01004215 // Get and check the first argument: the method handle (index range
4216 // checked by the dex file verifier).
Orion Hodsonc069a302017-01-18 09:23:12 +00004217 uint32_t method_handle_idx = static_cast<uint32_t>(it.GetJavaValue().i);
4218 it.Next();
Orion Hodson3a842f52017-04-21 15:24:10 +01004219
Orion Hodsonc069a302017-01-18 09:23:12 +00004220 const DexFile::MethodHandleItem& mh = dex_file_->GetMethodHandle(method_handle_idx);
4221 if (mh.method_handle_type_ != static_cast<uint16_t>(DexFile::MethodHandleType::kInvokeStatic)) {
4222 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Call site #" << call_site_idx
Orion Hodson631827d2017-04-10 14:53:47 +01004223 << " argument 0 method handle type is not InvokeStatic: "
4224 << mh.method_handle_type_;
Orion Hodsonc069a302017-01-18 09:23:12 +00004225 return false;
4226 }
4227
4228 // Skip the second argument, the name to resolve, as checked by the
4229 // dex file verifier.
4230 it.Next();
4231
4232 // Skip the third argument, the method type expected, as checked by
4233 // the dex file verifier.
4234 it.Next();
4235
4236 // Check the bootstrap method handle and remaining arguments.
4237 const DexFile::MethodId& method_id = dex_file_->GetMethodId(mh.field_or_method_idx_);
4238 uint32_t length;
4239 const char* shorty = dex_file_->GetMethodShorty(method_id, &length);
4240
4241 if (it.Size() < length - 1) {
4242 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Call site #" << call_site_idx
4243 << " too few arguments for bootstrap method: "
4244 << it.Size() << " < " << (length - 1);
4245 return false;
4246 }
4247
4248 // Check the return type and first 3 arguments are references
4249 // (CallSite, Lookup, String, MethodType). If they are not of the
4250 // expected types (or subtypes), it will trigger a
4251 // WrongMethodTypeException during execution.
4252 if (shorty[0] != 'L') {
4253 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Call site #" << call_site_idx
4254 << " bootstrap return type is not a reference";
4255 return false;
4256 }
4257
4258 for (uint32_t i = 1; i < 4; ++i) {
4259 if (shorty[i] != 'L') {
4260 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Call site #" << call_site_idx
4261 << " bootstrap method argument " << (i - 1)
4262 << " is not a reference";
4263 return false;
4264 }
4265 }
4266
4267 // Check the optional arguments.
4268 for (uint32_t i = 4; i < length; ++i, it.Next()) {
4269 bool match = false;
4270 switch (it.GetValueType()) {
4271 case EncodedArrayValueIterator::ValueType::kBoolean:
4272 case EncodedArrayValueIterator::ValueType::kByte:
4273 case EncodedArrayValueIterator::ValueType::kShort:
4274 case EncodedArrayValueIterator::ValueType::kChar:
4275 case EncodedArrayValueIterator::ValueType::kInt:
4276 // These all fit within one register and encoders do not seem
4277 // too exacting on the encoding type they use (ie using
4278 // integer for all of these).
4279 match = (strchr("ZBCSI", shorty[i]) != nullptr);
4280 break;
4281 case EncodedArrayValueIterator::ValueType::kLong:
4282 match = ('J' == shorty[i]);
4283 break;
4284 case EncodedArrayValueIterator::ValueType::kFloat:
4285 match = ('F' == shorty[i]);
4286 break;
4287 case EncodedArrayValueIterator::ValueType::kDouble:
4288 match = ('D' == shorty[i]);
4289 break;
4290 case EncodedArrayValueIterator::ValueType::kMethodType:
4291 case EncodedArrayValueIterator::ValueType::kMethodHandle:
4292 case EncodedArrayValueIterator::ValueType::kString:
4293 case EncodedArrayValueIterator::ValueType::kType:
4294 case EncodedArrayValueIterator::ValueType::kNull:
4295 match = ('L' == shorty[i]);
4296 break;
4297 case EncodedArrayValueIterator::ValueType::kField:
4298 case EncodedArrayValueIterator::ValueType::kMethod:
4299 case EncodedArrayValueIterator::ValueType::kEnum:
4300 case EncodedArrayValueIterator::ValueType::kArray:
4301 case EncodedArrayValueIterator::ValueType::kAnnotation:
4302 // Unreachable based on current EncodedArrayValueIterator::Next().
4303 UNREACHABLE();
4304 }
4305
4306 if (!match) {
4307 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Call site #" << call_site_idx
4308 << " bootstrap method argument " << (i - 1)
4309 << " expected " << shorty[i]
4310 << " got value type: " << it.GetValueType();
4311 return false;
4312 }
4313 }
4314 return true;
4315}
4316
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004317class MethodParamListDescriptorIterator {
4318 public:
Mathieu Chartiere401d142015-04-22 13:56:20 -07004319 explicit MethodParamListDescriptorIterator(ArtMethod* res_method) :
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004320 res_method_(res_method), pos_(0), params_(res_method->GetParameterTypeList()),
4321 params_size_(params_ == nullptr ? 0 : params_->Size()) {
4322 }
4323
4324 bool HasNext() {
4325 return pos_ < params_size_;
4326 }
4327
4328 void Next() {
4329 ++pos_;
4330 }
4331
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07004332 const char* GetDescriptor() REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004333 return res_method_->GetTypeDescriptorFromTypeIdx(params_->GetTypeItem(pos_).type_idx_);
4334 }
4335
4336 private:
Mathieu Chartiere401d142015-04-22 13:56:20 -07004337 ArtMethod* res_method_;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004338 size_t pos_;
4339 const DexFile::TypeList* params_;
4340 const size_t params_size_;
4341};
4342
Mathieu Chartiere401d142015-04-22 13:56:20 -07004343ArtMethod* MethodVerifier::VerifyInvocationArgs(
Alex Light7268d472016-01-20 15:50:01 -08004344 const Instruction* inst, MethodType method_type, bool is_range) {
jeffhao8cd6dda2012-02-22 10:15:34 -08004345 // Resolve the method. This could be an abstract or concrete method depending on what sort of call
4346 // we're making.
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004347 const uint32_t method_idx = inst->VRegB();
Alex Light7268d472016-01-20 15:50:01 -08004348 ArtMethod* res_method = ResolveMethodAndCheckAccess(method_idx, method_type);
Ian Rogers7b078e82014-09-10 14:44:24 -07004349 if (res_method == nullptr) { // error or class is unresolved
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004350 // Check what we can statically.
4351 if (!have_pending_hard_failure_) {
4352 VerifyInvocationArgsUnresolvedMethod(inst, method_type, is_range);
4353 }
4354 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08004355 }
4356
Ian Rogersd81871c2011-10-03 13:57:23 -07004357 // If we're using invoke-super(method), make sure that the executing method's class' superclass
Alex Light705ad492015-09-21 11:36:30 -07004358 // has a vtable entry for the target method. Or the target is on a interface.
Alex Light7268d472016-01-20 15:50:01 -08004359 if (method_type == METHOD_SUPER) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08004360 dex::TypeIndex class_idx = dex_file_->GetMethodId(method_idx).class_idx_;
David Brazdilca3c8c32016-09-06 14:04:48 +01004361 const RegType& reference_type = reg_types_.FromDescriptor(
4362 GetClassLoader(),
4363 dex_file_->StringByTypeIdx(class_idx),
4364 false);
4365 if (reference_type.IsUnresolvedTypes()) {
Alex Lightfedd91d2016-01-07 14:49:16 -08004366 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "Unable to find referenced class from invoke-super";
4367 return nullptr;
4368 }
David Brazdilca3c8c32016-09-06 14:04:48 +01004369 if (reference_type.GetClass()->IsInterface()) {
Alex Lightfedd91d2016-01-07 14:49:16 -08004370 // TODO Can we verify anything else.
David Brazdil15fc7292016-09-02 14:13:18 +01004371 if (class_idx == class_def_.class_idx_) {
Alex Lightfedd91d2016-01-07 14:49:16 -08004372 Fail(VERIFY_ERROR_CLASS_CHANGE) << "Cannot invoke-super on self as interface";
Alex Light55ea94d2016-03-15 09:50:26 -07004373 return nullptr;
Alex Lightfedd91d2016-01-07 14:49:16 -08004374 }
4375 // TODO Revisit whether we want to allow invoke-super on direct interfaces only like the JLS
4376 // does.
Alex Light55ea94d2016-03-15 09:50:26 -07004377 if (!GetDeclaringClass().HasClass()) {
4378 Fail(VERIFY_ERROR_NO_CLASS) << "Unable to resolve the full class of 'this' used in an"
4379 << "interface invoke-super";
4380 return nullptr;
David Brazdilca3c8c32016-09-06 14:04:48 +01004381 } else if (!reference_type.IsStrictlyAssignableFrom(GetDeclaringClass(), this)) {
Alex Lightfedd91d2016-01-07 14:49:16 -08004382 Fail(VERIFY_ERROR_CLASS_CHANGE)
David Sehr709b0702016-10-13 09:12:37 -07004383 << "invoke-super in " << mirror::Class::PrettyClass(GetDeclaringClass().GetClass())
4384 << " in method "
4385 << dex_file_->PrettyMethod(dex_method_idx_) << " to method "
4386 << dex_file_->PrettyMethod(method_idx) << " references "
4387 << "non-super-interface type " << mirror::Class::PrettyClass(reference_type.GetClass());
Alex Lightfedd91d2016-01-07 14:49:16 -08004388 return nullptr;
Alex Light705ad492015-09-21 11:36:30 -07004389 }
4390 } else {
4391 const RegType& super = GetDeclaringClass().GetSuperClass(&reg_types_);
4392 if (super.IsUnresolvedTypes()) {
4393 Fail(VERIFY_ERROR_NO_METHOD) << "unknown super class in invoke-super from "
David Sehr709b0702016-10-13 09:12:37 -07004394 << dex_file_->PrettyMethod(dex_method_idx_)
4395 << " to super " << res_method->PrettyMethod();
Alex Light705ad492015-09-21 11:36:30 -07004396 return nullptr;
4397 }
David Brazdilca3c8c32016-09-06 14:04:48 +01004398 if (!reference_type.IsStrictlyAssignableFrom(GetDeclaringClass(), this) ||
Aart Bikf663e342016-04-04 17:28:59 -07004399 (res_method->GetMethodIndex() >= super.GetClass()->GetVTableLength())) {
Alex Light705ad492015-09-21 11:36:30 -07004400 Fail(VERIFY_ERROR_NO_METHOD) << "invalid invoke-super from "
David Sehr709b0702016-10-13 09:12:37 -07004401 << dex_file_->PrettyMethod(dex_method_idx_)
Alex Light705ad492015-09-21 11:36:30 -07004402 << " to super " << super
4403 << "." << res_method->GetName()
4404 << res_method->GetSignature();
4405 return nullptr;
4406 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004407 }
4408 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08004409
Andreas Gampe74979b12017-05-16 09:28:06 -07004410 if (UNLIKELY(method_type == METHOD_POLYMORPHIC)) {
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004411 // Process the signature of the calling site that is invoking the method handle.
4412 DexFileParameterIterator it(*dex_file_, dex_file_->GetProtoId(inst->VRegH()));
4413 return VerifyInvocationArgsFromIterator(&it, inst, method_type, is_range, res_method);
4414 } else {
4415 // Process the target method's signature.
4416 MethodParamListDescriptorIterator it(res_method);
4417 return VerifyInvocationArgsFromIterator(&it, inst, method_type, is_range, res_method);
4418 }
4419}
4420
4421bool MethodVerifier::CheckSignaturePolymorphicMethod(ArtMethod* method) {
4422 mirror::Class* klass = method->GetDeclaringClass();
Orion Hodsonfe92d122018-01-02 10:45:17 +00004423 const char* method_name = method->GetName();
4424
4425 const char* expected_return_descriptor;
4426 if (klass == mirror::MethodHandle::StaticClass()) {
4427 expected_return_descriptor = mirror::MethodHandle::GetReturnTypeDescriptor(method_name);
4428 } else if (klass == mirror::VarHandle::StaticClass()) {
4429 expected_return_descriptor = mirror::VarHandle::GetReturnTypeDescriptor(method_name);
4430 } else {
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004431 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
Orion Hodsonfe92d122018-01-02 10:45:17 +00004432 << "Signature polymorphic method in unsuppported class: " << klass->PrettyDescriptor();
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004433 return false;
4434 }
4435
Orion Hodsonfe92d122018-01-02 10:45:17 +00004436 if (expected_return_descriptor == nullptr) {
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004437 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
4438 << "Signature polymorphic method name invalid: " << method_name;
4439 return false;
4440 }
4441
4442 const DexFile::TypeList* types = method->GetParameterTypeList();
4443 if (types->Size() != 1) {
4444 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
4445 << "Signature polymorphic method has too many arguments " << types->Size() << " != 1";
4446 return false;
4447 }
4448
4449 const dex::TypeIndex argument_type_index = types->GetTypeItem(0).type_idx_;
4450 const char* argument_descriptor = method->GetTypeDescriptorFromTypeIdx(argument_type_index);
4451 if (strcmp(argument_descriptor, "[Ljava/lang/Object;") != 0) {
4452 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
4453 << "Signature polymorphic method has unexpected argument type: " << argument_descriptor;
4454 return false;
4455 }
4456
4457 const char* return_descriptor = method->GetReturnTypeDescriptor();
Orion Hodsonfe92d122018-01-02 10:45:17 +00004458 if (strcmp(return_descriptor, expected_return_descriptor) != 0) {
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004459 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
Orion Hodsonfe92d122018-01-02 10:45:17 +00004460 << "Signature polymorphic method has unexpected return type: " << return_descriptor
4461 << " != " << expected_return_descriptor;
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004462 return false;
4463 }
4464
4465 return true;
4466}
4467
4468bool MethodVerifier::CheckSignaturePolymorphicReceiver(const Instruction* inst) {
4469 const RegType& this_type = work_line_->GetInvocationThis(this, inst);
Andreas Gampeeed3a5d2017-11-29 14:58:34 -08004470 if (this_type.IsZeroOrNull()) {
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004471 /* null pointer always passes (and always fails at run time) */
4472 return true;
4473 } else if (!this_type.IsNonZeroReferenceTypes()) {
4474 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
4475 << "invoke-polymorphic receiver is not a reference: "
4476 << this_type;
4477 return false;
4478 } else if (this_type.IsUninitializedReference()) {
4479 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
4480 << "invoke-polymorphic receiver is uninitialized: "
4481 << this_type;
4482 return false;
4483 } else if (!this_type.HasClass()) {
4484 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
4485 << "invoke-polymorphic receiver has no class: "
4486 << this_type;
4487 return false;
Orion Hodsonfe92d122018-01-02 10:45:17 +00004488 } else if (!this_type.GetClass()->IsSubClass(mirror::MethodHandle::StaticClass()) &&
4489 !this_type.GetClass()->IsSubClass(mirror::VarHandle::StaticClass())) {
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004490 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
Orion Hodsonfe92d122018-01-02 10:45:17 +00004491 << "invoke-polymorphic receiver is not a subclass of MethodHandle or VarHandle: "
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004492 << this_type;
4493 return false;
4494 }
4495 return true;
Ian Rogersd81871c2011-10-03 13:57:23 -07004496}
4497
Mathieu Chartiere401d142015-04-22 13:56:20 -07004498ArtMethod* MethodVerifier::GetQuickInvokedMethod(const Instruction* inst, RegisterLine* reg_line,
4499 bool is_range, bool allow_failure) {
Mathieu Chartier091d2382015-03-06 10:59:06 -08004500 if (is_range) {
4501 DCHECK_EQ(inst->Opcode(), Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
4502 } else {
4503 DCHECK_EQ(inst->Opcode(), Instruction::INVOKE_VIRTUAL_QUICK);
4504 }
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004505 const RegType& actual_arg_type = reg_line->GetInvocationThis(this, inst, allow_failure);
Ian Rogers9bc54402014-04-17 16:40:01 -07004506 if (!actual_arg_type.HasClass()) {
4507 VLOG(verifier) << "Failed to get mirror::Class* from '" << actual_arg_type << "'";
Andreas Gampe63981562014-04-17 12:28:43 -07004508 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004509 }
Ian Rogersa4cf1df2014-05-07 19:47:17 -07004510 mirror::Class* klass = actual_arg_type.GetClass();
Mingyao Yang2cdbad72014-07-16 10:44:41 -07004511 mirror::Class* dispatch_class;
Ian Rogersa4cf1df2014-05-07 19:47:17 -07004512 if (klass->IsInterface()) {
4513 // Derive Object.class from Class.class.getSuperclass().
4514 mirror::Class* object_klass = klass->GetClass()->GetSuperClass();
Andreas Gampe7c038102014-10-27 20:08:46 -07004515 if (FailOrAbort(this, object_klass->IsObjectClass(),
Mathieu Chartier091d2382015-03-06 10:59:06 -08004516 "Failed to find Object class in quickened invoke receiver", work_insn_idx_)) {
Andreas Gampe7c038102014-10-27 20:08:46 -07004517 return nullptr;
4518 }
Mingyao Yang2cdbad72014-07-16 10:44:41 -07004519 dispatch_class = object_klass;
Ian Rogersa4cf1df2014-05-07 19:47:17 -07004520 } else {
Mingyao Yang2cdbad72014-07-16 10:44:41 -07004521 dispatch_class = klass;
Ian Rogersa4cf1df2014-05-07 19:47:17 -07004522 }
Mathieu Chartier091d2382015-03-06 10:59:06 -08004523 if (!dispatch_class->HasVTable()) {
4524 FailOrAbort(this, allow_failure, "Receiver class has no vtable for quickened invoke at ",
4525 work_insn_idx_);
Andreas Gampe7c038102014-10-27 20:08:46 -07004526 return nullptr;
4527 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004528 uint16_t vtable_index = is_range ? inst->VRegB_3rc() : inst->VRegB_35c();
Mathieu Chartiere401d142015-04-22 13:56:20 -07004529 auto* cl = Runtime::Current()->GetClassLinker();
4530 auto pointer_size = cl->GetImagePointerSize();
Mathieu Chartier091d2382015-03-06 10:59:06 -08004531 if (static_cast<int32_t>(vtable_index) >= dispatch_class->GetVTableLength()) {
4532 FailOrAbort(this, allow_failure,
4533 "Receiver class has not enough vtable slots for quickened invoke at ",
4534 work_insn_idx_);
Andreas Gampe7c038102014-10-27 20:08:46 -07004535 return nullptr;
4536 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07004537 ArtMethod* res_method = dispatch_class->GetVTableEntry(vtable_index, pointer_size);
Mathieu Chartier091d2382015-03-06 10:59:06 -08004538 if (self_->IsExceptionPending()) {
4539 FailOrAbort(this, allow_failure, "Unexpected exception pending for quickened invoke at ",
4540 work_insn_idx_);
Andreas Gampe7c038102014-10-27 20:08:46 -07004541 return nullptr;
4542 }
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004543 return res_method;
4544}
4545
Mathieu Chartiere401d142015-04-22 13:56:20 -07004546ArtMethod* MethodVerifier::VerifyInvokeVirtualQuickArgs(const Instruction* inst, bool is_range) {
Andreas Gampe76bd8802014-12-10 16:43:58 -08004547 DCHECK(Runtime::Current()->IsStarted() || verify_to_dump_)
David Sehr709b0702016-10-13 09:12:37 -07004548 << dex_file_->PrettyMethod(dex_method_idx_, true) << "@" << work_insn_idx_;
Andreas Gampe76bd8802014-12-10 16:43:58 -08004549
Mathieu Chartiere401d142015-04-22 13:56:20 -07004550 ArtMethod* res_method = GetQuickInvokedMethod(inst, work_line_.get(), is_range, false);
Ian Rogers7b078e82014-09-10 14:44:24 -07004551 if (res_method == nullptr) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004552 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer method from " << inst->Name();
Ian Rogers7b078e82014-09-10 14:44:24 -07004553 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004554 }
Andreas Gampe7c038102014-10-27 20:08:46 -07004555 if (FailOrAbort(this, !res_method->IsDirect(), "Quick-invoked method is direct at ",
4556 work_insn_idx_)) {
4557 return nullptr;
4558 }
4559 if (FailOrAbort(this, !res_method->IsStatic(), "Quick-invoked method is static at ",
4560 work_insn_idx_)) {
4561 return nullptr;
4562 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004563
4564 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
4565 // match the call to the signature. Also, we might be calling through an abstract method
4566 // definition (which doesn't have register count values).
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004567 const RegType& actual_arg_type = work_line_->GetInvocationThis(this, inst);
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004568 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
Ian Rogers7b078e82014-09-10 14:44:24 -07004569 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004570 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004571 const size_t expected_args = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
4572 /* caught by static verifier */
4573 DCHECK(is_range || expected_args <= 5);
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08004574 if (expected_args > code_item_accessor_.OutsSize()) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004575 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08004576 << ") exceeds outsSize (" << code_item_accessor_.OutsSize() << ")";
Ian Rogers7b078e82014-09-10 14:44:24 -07004577 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004578 }
4579
4580 /*
4581 * Check the "this" argument, which must be an instance of the class that declared the method.
4582 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
4583 * rigorous check here (which is okay since we have to do it at runtime).
4584 */
David Brazdil68b5c0b2016-01-19 14:25:29 +00004585 // Note: given an uninitialized type, this should always fail. Constructors aren't virtual.
4586 if (actual_arg_type.IsUninitializedTypes() && !res_method->IsConstructor()) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004587 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
Ian Rogers7b078e82014-09-10 14:44:24 -07004588 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004589 }
Andreas Gampeeed3a5d2017-11-29 14:58:34 -08004590 if (!actual_arg_type.IsZeroOrNull()) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004591 mirror::Class* klass = res_method->GetDeclaringClass();
Ian Rogers1ff3c982014-08-12 02:30:58 -07004592 std::string temp;
Ian Rogersd8f69b02014-09-10 21:43:52 +00004593 const RegType& res_method_class =
Andreas Gampef23f33d2015-06-23 14:18:17 -07004594 FromClass(klass->GetDescriptor(&temp), klass, klass->CannotBeAssignedFromOtherTypes());
David Brazdilca3c8c32016-09-06 14:04:48 +01004595 if (!res_method_class.IsAssignableFrom(actual_arg_type, this)) {
David Brazdil68b5c0b2016-01-19 14:25:29 +00004596 Fail(actual_arg_type.IsUninitializedTypes() // Just overcautious - should have never
4597 ? VERIFY_ERROR_BAD_CLASS_HARD // quickened this.
4598 : actual_arg_type.IsUnresolvedTypes()
4599 ? VERIFY_ERROR_NO_CLASS
4600 : VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004601 << "' not instance of '" << res_method_class << "'";
Ian Rogers7b078e82014-09-10 14:44:24 -07004602 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004603 }
4604 }
4605 /*
4606 * Process the target method's signature. This signature may or may not
4607 * have been verified, so we can't assume it's properly formed.
4608 */
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07004609 const DexFile::TypeList* params = res_method->GetParameterTypeList();
Ian Rogers7b078e82014-09-10 14:44:24 -07004610 size_t params_size = params == nullptr ? 0 : params->Size();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004611 uint32_t arg[5];
4612 if (!is_range) {
Ian Rogers29a26482014-05-02 15:27:29 -07004613 inst->GetVarArgs(arg);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004614 }
4615 size_t actual_args = 1;
4616 for (size_t param_index = 0; param_index < params_size; param_index++) {
4617 if (actual_args >= expected_args) {
David Sehr709b0702016-10-13 09:12:37 -07004618 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invalid call to '"
4619 << res_method->PrettyMethod()
Brian Carlstrom93c33962013-07-26 10:37:43 -07004620 << "'. Expected " << expected_args
4621 << " arguments, processing argument " << actual_args
4622 << " (where longs/doubles count twice).";
Ian Rogers7b078e82014-09-10 14:44:24 -07004623 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004624 }
4625 const char* descriptor =
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07004626 res_method->GetTypeDescriptorFromTypeIdx(params->GetTypeItem(param_index).type_idx_);
Ian Rogers7b078e82014-09-10 14:44:24 -07004627 if (descriptor == nullptr) {
David Sehr709b0702016-10-13 09:12:37 -07004628 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of "
4629 << res_method->PrettyMethod()
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004630 << " missing signature component";
Ian Rogers7b078e82014-09-10 14:44:24 -07004631 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004632 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00004633 const RegType& reg_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004634 uint32_t get_reg = is_range ? inst->VRegC_3rc() + actual_args : arg[actual_args];
Ian Rogers7b078e82014-09-10 14:44:24 -07004635 if (!work_line_->VerifyRegisterType(this, get_reg, reg_type)) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004636 return res_method;
4637 }
4638 actual_args = reg_type.IsLongOrDoubleTypes() ? actual_args + 2 : actual_args + 1;
4639 }
4640 if (actual_args != expected_args) {
David Sehr709b0702016-10-13 09:12:37 -07004641 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of "
4642 << res_method->PrettyMethod() << " expected "
4643 << expected_args << " arguments, found " << actual_args;
Ian Rogers7b078e82014-09-10 14:44:24 -07004644 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004645 } else {
4646 return res_method;
4647 }
4648}
4649
Ian Rogers62342ec2013-06-11 10:26:37 -07004650void MethodVerifier::VerifyNewArray(const Instruction* inst, bool is_filled, bool is_range) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08004651 dex::TypeIndex type_idx;
Sebastien Hertz5243e912013-05-21 10:55:07 +02004652 if (!is_filled) {
4653 DCHECK_EQ(inst->Opcode(), Instruction::NEW_ARRAY);
Andreas Gampea5b09a62016-11-17 15:21:22 -08004654 type_idx = dex::TypeIndex(inst->VRegC_22c());
Sebastien Hertz5243e912013-05-21 10:55:07 +02004655 } else if (!is_range) {
4656 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY);
Andreas Gampea5b09a62016-11-17 15:21:22 -08004657 type_idx = dex::TypeIndex(inst->VRegB_35c());
Sebastien Hertz5243e912013-05-21 10:55:07 +02004658 } else {
4659 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY_RANGE);
Andreas Gampea5b09a62016-11-17 15:21:22 -08004660 type_idx = dex::TypeIndex(inst->VRegB_3rc());
Sebastien Hertz5243e912013-05-21 10:55:07 +02004661 }
Andreas Gampe98be1a92017-08-28 08:25:45 -07004662 const RegType& res_type = ResolveClass<CheckAccess::kYes>(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004663 if (res_type.IsConflict()) { // bad class
4664 DCHECK_NE(failures_.size(), 0U);
Ian Rogers0c4a5062012-02-03 15:18:59 -08004665 } else {
4666 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
4667 if (!res_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07004668 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "new-array on non-array class " << res_type;
Ian Rogers0c4a5062012-02-03 15:18:59 -08004669 } else if (!is_filled) {
4670 /* make sure "size" register is valid type */
Ian Rogers7b078e82014-09-10 14:44:24 -07004671 work_line_->VerifyRegisterType(this, inst->VRegB_22c(), reg_types_.Integer());
Ian Rogers0c4a5062012-02-03 15:18:59 -08004672 /* set register type to array class */
Ian Rogersd8f69b02014-09-10 21:43:52 +00004673 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
Andreas Gampead238ce2015-08-24 21:13:08 -07004674 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_22c(), precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08004675 } else {
Andreas Gampebb18a032016-03-22 20:34:25 -07004676 DCHECK(!res_type.IsUnresolvedMergedReference());
Ian Rogers0c4a5062012-02-03 15:18:59 -08004677 // Verify each register. If "arg_count" is bad, VerifyRegisterType() will run off the end of
4678 // the list and fail. It's legal, if silly, for arg_count to be zero.
Ian Rogersd8f69b02014-09-10 21:43:52 +00004679 const RegType& expected_type = reg_types_.GetComponentType(res_type, GetClassLoader());
Sebastien Hertz5243e912013-05-21 10:55:07 +02004680 uint32_t arg_count = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
4681 uint32_t arg[5];
4682 if (!is_range) {
Ian Rogers29a26482014-05-02 15:27:29 -07004683 inst->GetVarArgs(arg);
Sebastien Hertz5243e912013-05-21 10:55:07 +02004684 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08004685 for (size_t ui = 0; ui < arg_count; ui++) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02004686 uint32_t get_reg = is_range ? inst->VRegC_3rc() + ui : arg[ui];
Ian Rogers7b078e82014-09-10 14:44:24 -07004687 if (!work_line_->VerifyRegisterType(this, get_reg, expected_type)) {
4688 work_line_->SetResultRegisterType(this, reg_types_.Conflict());
Ian Rogers0c4a5062012-02-03 15:18:59 -08004689 return;
4690 }
4691 }
4692 // filled-array result goes into "result" register
Ian Rogersd8f69b02014-09-10 21:43:52 +00004693 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
Ian Rogers7b078e82014-09-10 14:44:24 -07004694 work_line_->SetResultRegisterType(this, precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08004695 }
4696 }
4697}
4698
Sebastien Hertz5243e912013-05-21 10:55:07 +02004699void MethodVerifier::VerifyAGet(const Instruction* inst,
Ian Rogersd8f69b02014-09-10 21:43:52 +00004700 const RegType& insn_type, bool is_primitive) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004701 const RegType& index_type = work_line_->GetRegisterType(this, inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07004702 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07004703 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07004704 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004705 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegB_23x());
Andreas Gampeeed3a5d2017-11-29 14:58:34 -08004706 if (array_type.IsZeroOrNull()) {
Ian Rogers89310de2012-02-01 13:47:30 -08004707 // Null array class; this code path will fail at runtime. Infer a merge-able type from the
Andreas Gampe52f205a2017-12-01 12:16:07 -08004708 // instruction type.
4709 if (!is_primitive) {
4710 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), reg_types_.Null());
4711 } else if (insn_type.IsInteger()) {
4712 // Pick a non-zero constant (to distinguish with null) that can fit in any primitive.
4713 // We cannot use 'insn_type' as it could be a float array or an int array.
4714 work_line_->SetRegisterType<LockOp::kClear>(
4715 this, inst->VRegA_23x(), DetermineCat1Constant(1, need_precise_constants_));
4716 } else if (insn_type.IsCategory1Types()) {
4717 // Category 1
4718 // The 'insn_type' is exactly the type we need.
4719 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), insn_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07004720 } else {
Ian Rogers89310de2012-02-01 13:47:30 -08004721 // Category 2
Ian Rogers7b078e82014-09-10 14:44:24 -07004722 work_line_->SetRegisterTypeWide(this, inst->VRegA_23x(),
4723 reg_types_.FromCat2ConstLo(0, false),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004724 reg_types_.FromCat2ConstHi(0, false));
Ian Rogers89310de2012-02-01 13:47:30 -08004725 }
jeffhaofc3144e2012-02-01 17:21:15 -08004726 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07004727 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aget";
Andreas Gampe8d8fc482016-03-25 16:24:20 -07004728 } else if (array_type.IsUnresolvedMergedReference()) {
Andreas Gampebb18a032016-03-22 20:34:25 -07004729 // Unresolved array types must be reference array types.
4730 if (is_primitive) {
4731 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type
4732 << " source for category 1 aget";
4733 } else {
4734 Fail(VERIFY_ERROR_NO_CLASS) << "cannot verify aget for " << array_type
4735 << " because of missing class";
Andreas Gampe8d8fc482016-03-25 16:24:20 -07004736 // Approximate with java.lang.Object[].
4737 work_line_->SetRegisterType<LockOp::kClear>(this,
4738 inst->VRegA_23x(),
4739 reg_types_.JavaLangObject(false));
Andreas Gampebb18a032016-03-22 20:34:25 -07004740 }
Ian Rogers89310de2012-02-01 13:47:30 -08004741 } else {
4742 /* verify the class */
Ian Rogersd8f69b02014-09-10 21:43:52 +00004743 const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
jeffhaofc3144e2012-02-01 17:21:15 -08004744 if (!component_type.IsReferenceTypes() && !is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07004745 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08004746 << " source for aget-object";
4747 } else if (component_type.IsNonZeroReferenceTypes() && is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07004748 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08004749 << " source for category 1 aget";
4750 } else if (is_primitive && !insn_type.Equals(component_type) &&
4751 !((insn_type.IsInteger() && component_type.IsFloat()) ||
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004752 (insn_type.IsLong() && component_type.IsDouble()))) {
4753 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array type " << array_type
4754 << " incompatible with aget of type " << insn_type;
Ian Rogers89310de2012-02-01 13:47:30 -08004755 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004756 // Use knowledge of the field type which is stronger than the type inferred from the
4757 // instruction, which can't differentiate object types and ints from floats, longs from
4758 // doubles.
4759 if (!component_type.IsLowHalf()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07004760 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), component_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004761 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004762 work_line_->SetRegisterTypeWide(this, inst->VRegA_23x(), component_type,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004763 component_type.HighHalf(&reg_types_));
4764 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004765 }
4766 }
4767 }
4768}
4769
Ian Rogersd8f69b02014-09-10 21:43:52 +00004770void MethodVerifier::VerifyPrimitivePut(const RegType& target_type, const RegType& insn_type,
Jeff Haofe1f7c82013-08-01 14:50:24 -07004771 const uint32_t vregA) {
4772 // Primitive assignability rules are weaker than regular assignability rules.
4773 bool instruction_compatible;
4774 bool value_compatible;
Ian Rogers7b078e82014-09-10 14:44:24 -07004775 const RegType& value_type = work_line_->GetRegisterType(this, vregA);
Jeff Haofe1f7c82013-08-01 14:50:24 -07004776 if (target_type.IsIntegralTypes()) {
Jeff Haoa4647482013-08-06 15:35:47 -07004777 instruction_compatible = target_type.Equals(insn_type);
Jeff Haofe1f7c82013-08-01 14:50:24 -07004778 value_compatible = value_type.IsIntegralTypes();
4779 } else if (target_type.IsFloat()) {
4780 instruction_compatible = insn_type.IsInteger(); // no put-float, so expect put-int
4781 value_compatible = value_type.IsFloatTypes();
4782 } else if (target_type.IsLong()) {
4783 instruction_compatible = insn_type.IsLong();
Andreas Gampe376fa682014-09-07 13:06:12 -07004784 // Additional register check: this is not checked statically (as part of VerifyInstructions),
4785 // as target_type depends on the resolved type of the field.
4786 if (instruction_compatible && work_line_->NumRegs() > vregA + 1) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004787 const RegType& value_type_hi = work_line_->GetRegisterType(this, vregA + 1);
Andreas Gampe376fa682014-09-07 13:06:12 -07004788 value_compatible = value_type.IsLongTypes() && value_type.CheckWidePair(value_type_hi);
4789 } else {
4790 value_compatible = false;
4791 }
Jeff Haofe1f7c82013-08-01 14:50:24 -07004792 } else if (target_type.IsDouble()) {
4793 instruction_compatible = insn_type.IsLong(); // no put-double, so expect put-long
Andreas Gampe376fa682014-09-07 13:06:12 -07004794 // Additional register check: this is not checked statically (as part of VerifyInstructions),
4795 // as target_type depends on the resolved type of the field.
4796 if (instruction_compatible && work_line_->NumRegs() > vregA + 1) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004797 const RegType& value_type_hi = work_line_->GetRegisterType(this, vregA + 1);
Andreas Gampe376fa682014-09-07 13:06:12 -07004798 value_compatible = value_type.IsDoubleTypes() && value_type.CheckWidePair(value_type_hi);
4799 } else {
4800 value_compatible = false;
4801 }
Jeff Haofe1f7c82013-08-01 14:50:24 -07004802 } else {
4803 instruction_compatible = false; // reference with primitive store
4804 value_compatible = false; // unused
4805 }
4806 if (!instruction_compatible) {
4807 // This is a global failure rather than a class change failure as the instructions and
4808 // the descriptors for the type should have been consistent within the same file at
4809 // compile time.
4810 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "put insn has type '" << insn_type
4811 << "' but expected type '" << target_type << "'";
4812 return;
4813 }
4814 if (!value_compatible) {
4815 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
4816 << " of type " << value_type << " but expected " << target_type << " for put";
4817 return;
4818 }
4819}
4820
Sebastien Hertz5243e912013-05-21 10:55:07 +02004821void MethodVerifier::VerifyAPut(const Instruction* inst,
Ian Rogersd8f69b02014-09-10 21:43:52 +00004822 const RegType& insn_type, bool is_primitive) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004823 const RegType& index_type = work_line_->GetRegisterType(this, inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07004824 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07004825 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07004826 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004827 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegB_23x());
Andreas Gampeeed3a5d2017-11-29 14:58:34 -08004828 if (array_type.IsZeroOrNull()) {
Nicolas Geoffray66389fb2015-06-19 10:35:42 +01004829 // Null array type; this code path will fail at runtime.
4830 // Still check that the given value matches the instruction's type.
Andreas Gampe4bf4c782015-08-14 14:07:43 -07004831 // Note: this is, as usual, complicated by the fact the the instruction isn't fully typed
4832 // and fits multiple register types.
4833 const RegType* modified_reg_type = &insn_type;
4834 if ((modified_reg_type == &reg_types_.Integer()) ||
4835 (modified_reg_type == &reg_types_.LongLo())) {
4836 // May be integer or float | long or double. Overwrite insn_type accordingly.
4837 const RegType& value_type = work_line_->GetRegisterType(this, inst->VRegA_23x());
4838 if (modified_reg_type == &reg_types_.Integer()) {
4839 if (&value_type == &reg_types_.Float()) {
4840 modified_reg_type = &value_type;
4841 }
4842 } else {
4843 if (&value_type == &reg_types_.DoubleLo()) {
4844 modified_reg_type = &value_type;
4845 }
4846 }
4847 }
4848 work_line_->VerifyRegisterType(this, inst->VRegA_23x(), *modified_reg_type);
jeffhaofc3144e2012-02-01 17:21:15 -08004849 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07004850 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aput";
Andreas Gampe8d8fc482016-03-25 16:24:20 -07004851 } else if (array_type.IsUnresolvedMergedReference()) {
Andreas Gampebb18a032016-03-22 20:34:25 -07004852 // Unresolved array types must be reference array types.
4853 if (is_primitive) {
4854 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "put insn has type '" << insn_type
4855 << "' but unresolved type '" << array_type << "'";
4856 } else {
4857 Fail(VERIFY_ERROR_NO_CLASS) << "cannot verify aput for " << array_type
4858 << " because of missing class";
4859 }
Ian Rogers89310de2012-02-01 13:47:30 -08004860 } else {
Ian Rogersd8f69b02014-09-10 21:43:52 +00004861 const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
Jeff Haofe1f7c82013-08-01 14:50:24 -07004862 const uint32_t vregA = inst->VRegA_23x();
Jeff Haob24b4a72013-07-31 13:47:31 -07004863 if (is_primitive) {
Jeff Haofe1f7c82013-08-01 14:50:24 -07004864 VerifyPrimitivePut(component_type, insn_type, vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07004865 } else {
Jeff Haob24b4a72013-07-31 13:47:31 -07004866 if (!component_type.IsReferenceTypes()) {
4867 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
4868 << " source for aput-object";
4869 } else {
4870 // The instruction agrees with the type of array, confirm the value to be stored does too
4871 // Note: we use the instruction type (rather than the component type) for aput-object as
4872 // incompatible classes will be caught at runtime as an array store exception
Ian Rogers7b078e82014-09-10 14:44:24 -07004873 work_line_->VerifyRegisterType(this, vregA, insn_type);
Jeff Haob24b4a72013-07-31 13:47:31 -07004874 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004875 }
4876 }
4877 }
4878}
4879
Mathieu Chartierc7853442015-03-27 14:35:38 -07004880ArtField* MethodVerifier::GetStaticField(int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08004881 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
4882 // Check access to class
Andreas Gampe98be1a92017-08-28 08:25:45 -07004883 const RegType& klass_type = ResolveClass<CheckAccess::kYes>(field_id.class_idx_);
Brian Carlstrom7934ac22013-07-26 10:54:15 -07004884 if (klass_type.IsConflict()) { // bad class
Ian Rogersad0b3a32012-04-16 14:50:24 -07004885 AppendToLastFailMessage(StringPrintf(" in attempt to access static field %d (%s) in %s",
4886 field_idx, dex_file_->GetFieldName(field_id),
4887 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers7b078e82014-09-10 14:44:24 -07004888 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08004889 }
Elliott Hughesb25c3f62012-03-26 16:35:06 -07004890 if (klass_type.IsUnresolvedTypes()) {
Andreas Gampe629be512017-08-25 17:09:32 -07004891 // Accessibility checks depend on resolved fields.
4892 DCHECK(klass_type.Equals(GetDeclaringClass()) || !failures_.empty());
4893
Ian Rogers7b078e82014-09-10 14:44:24 -07004894 return nullptr; // Can't resolve Class so no more to do here, will do checking at runtime.
Ian Rogers90040192011-12-16 08:54:29 -08004895 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07004896 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Vladimir Markoe11dd502017-12-08 14:09:45 +00004897 ArtField* field = class_linker->ResolveFieldJLS(field_idx, dex_cache_, class_loader_);
David Brazdilca3c8c32016-09-06 14:04:48 +01004898
4899 // Record result of the field resolution attempt.
4900 VerifierDeps::MaybeRecordFieldResolution(*dex_file_, field_idx, field);
4901
Ian Rogers7b078e82014-09-10 14:44:24 -07004902 if (field == nullptr) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07004903 VLOG(verifier) << "Unable to resolve static field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07004904 << dex_file_->GetFieldName(field_id) << ") in "
4905 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogers7b078e82014-09-10 14:44:24 -07004906 DCHECK(self_->IsExceptionPending());
4907 self_->ClearException();
4908 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004909 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
4910 field->GetAccessFlags())) {
David Sehr709b0702016-10-13 09:12:37 -07004911 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access static field " << field->PrettyField()
Ian Rogersad0b3a32012-04-16 14:50:24 -07004912 << " from " << GetDeclaringClass();
Ian Rogers7b078e82014-09-10 14:44:24 -07004913 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07004914 } else if (!field->IsStatic()) {
David Sehr709b0702016-10-13 09:12:37 -07004915 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << field->PrettyField() << " to be static";
Ian Rogers7b078e82014-09-10 14:44:24 -07004916 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07004917 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07004918 return field;
Ian Rogersd81871c2011-10-03 13:57:23 -07004919}
4920
Mathieu Chartierc7853442015-03-27 14:35:38 -07004921ArtField* MethodVerifier::GetInstanceField(const RegType& obj_type, int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08004922 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
Aart Bik31883642016-06-06 15:02:44 -07004923 // Check access to class.
Andreas Gampe98be1a92017-08-28 08:25:45 -07004924 const RegType& klass_type = ResolveClass<CheckAccess::kYes>(field_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004925 if (klass_type.IsConflict()) {
4926 AppendToLastFailMessage(StringPrintf(" in attempt to access instance field %d (%s) in %s",
4927 field_idx, dex_file_->GetFieldName(field_id),
4928 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers7b078e82014-09-10 14:44:24 -07004929 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08004930 }
jeffhao8cd6dda2012-02-22 10:15:34 -08004931 if (klass_type.IsUnresolvedTypes()) {
Andreas Gampe629be512017-08-25 17:09:32 -07004932 // Accessibility checks depend on resolved fields.
4933 DCHECK(klass_type.Equals(GetDeclaringClass()) || !failures_.empty());
4934
Ian Rogers7b078e82014-09-10 14:44:24 -07004935 return nullptr; // Can't resolve Class so no more to do here
Ian Rogers90040192011-12-16 08:54:29 -08004936 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07004937 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Vladimir Markoe11dd502017-12-08 14:09:45 +00004938 ArtField* field = class_linker->ResolveFieldJLS(field_idx, dex_cache_, class_loader_);
David Brazdilca3c8c32016-09-06 14:04:48 +01004939
4940 // Record result of the field resolution attempt.
4941 VerifierDeps::MaybeRecordFieldResolution(*dex_file_, field_idx, field);
4942
Ian Rogers7b078e82014-09-10 14:44:24 -07004943 if (field == nullptr) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07004944 VLOG(verifier) << "Unable to resolve instance field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07004945 << dex_file_->GetFieldName(field_id) << ") in "
4946 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogers7b078e82014-09-10 14:44:24 -07004947 DCHECK(self_->IsExceptionPending());
4948 self_->ClearException();
4949 return nullptr;
Andreas Gampeeed3a5d2017-11-29 14:58:34 -08004950 } else if (obj_type.IsZeroOrNull()) {
Aart Bik31883642016-06-06 15:02:44 -07004951 // Cannot infer and check type, however, access will cause null pointer exception.
4952 // Fall through into a few last soft failure checks below.
Stephen Kyle695c5982014-08-22 15:03:07 +01004953 } else if (!obj_type.IsReferenceTypes()) {
Aart Bik31883642016-06-06 15:02:44 -07004954 // Trying to read a field from something that isn't a reference.
Stephen Kyle695c5982014-08-22 15:03:07 +01004955 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance field access on object that has "
4956 << "non-reference type " << obj_type;
Ian Rogers7b078e82014-09-10 14:44:24 -07004957 return nullptr;
Ian Rogerse1758fe2012-04-19 11:31:15 -07004958 } else {
David Brazdil0d638bb2016-07-27 15:29:25 +01004959 std::string temp;
Mathieu Chartier3398c782016-09-30 10:27:43 -07004960 ObjPtr<mirror::Class> klass = field->GetDeclaringClass();
Ian Rogersd8f69b02014-09-10 21:43:52 +00004961 const RegType& field_klass =
Mathieu Chartier3398c782016-09-30 10:27:43 -07004962 FromClass(klass->GetDescriptor(&temp),
Mathieu Chartier1cc62e42016-10-03 18:01:28 -07004963 klass.Ptr(),
Mathieu Chartier3398c782016-09-30 10:27:43 -07004964 klass->CannotBeAssignedFromOtherTypes());
David Brazdil68b5c0b2016-01-19 14:25:29 +00004965 if (obj_type.IsUninitializedTypes()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07004966 // Field accesses through uninitialized references are only allowable for constructors where
David Brazdil68b5c0b2016-01-19 14:25:29 +00004967 // the field is declared in this class.
4968 // Note: this IsConstructor check is technically redundant, as UninitializedThis should only
4969 // appear in constructors.
4970 if (!obj_type.IsUninitializedThisReference() ||
4971 !IsConstructor() ||
4972 !field_klass.Equals(GetDeclaringClass())) {
David Sehr709b0702016-10-13 09:12:37 -07004973 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "cannot access instance field " << field->PrettyField()
David Brazdil68b5c0b2016-01-19 14:25:29 +00004974 << " of a not fully initialized object within the context"
David Sehr709b0702016-10-13 09:12:37 -07004975 << " of " << dex_file_->PrettyMethod(dex_method_idx_);
David Brazdil68b5c0b2016-01-19 14:25:29 +00004976 return nullptr;
4977 }
David Brazdilca3c8c32016-09-06 14:04:48 +01004978 } else if (!field_klass.IsAssignableFrom(obj_type, this)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07004979 // Trying to access C1.field1 using reference of type C2, which is neither C1 or a sub-class
4980 // of C1. For resolution to occur the declared class of the field must be compatible with
4981 // obj_type, we've discovered this wasn't so, so report the field didn't exist.
Andreas Gampe66596242016-04-14 10:55:04 -07004982 VerifyError type;
4983 bool is_aot = Runtime::Current()->IsAotCompiler();
4984 if (is_aot && (field_klass.IsUnresolvedTypes() || obj_type.IsUnresolvedTypes())) {
4985 // Compiler & unresolved types involved, retry at runtime.
4986 type = VerifyError::VERIFY_ERROR_NO_CLASS;
4987 } else {
Andreas Gampe8f4ade02016-04-15 10:09:16 -07004988 // Classes known (resolved; and thus assignability check is precise), or we are at runtime
4989 // and still missing classes. This is a hard failure.
Andreas Gampe66596242016-04-14 10:55:04 -07004990 type = VerifyError::VERIFY_ERROR_BAD_CLASS_HARD;
4991 }
David Sehr709b0702016-10-13 09:12:37 -07004992 Fail(type) << "cannot access instance field " << field->PrettyField()
Andreas Gampe66596242016-04-14 10:55:04 -07004993 << " from object of type " << obj_type;
Ian Rogers7b078e82014-09-10 14:44:24 -07004994 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004995 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004996 }
Aart Bik31883642016-06-06 15:02:44 -07004997
4998 // Few last soft failure checks.
4999 if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
5000 field->GetAccessFlags())) {
David Sehr709b0702016-10-13 09:12:37 -07005001 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access instance field " << field->PrettyField()
Aart Bik31883642016-06-06 15:02:44 -07005002 << " from " << GetDeclaringClass();
5003 return nullptr;
5004 } else if (field->IsStatic()) {
David Sehr709b0702016-10-13 09:12:37 -07005005 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << field->PrettyField()
Aart Bik31883642016-06-06 15:02:44 -07005006 << " to not be static";
5007 return nullptr;
5008 }
5009
5010 return field;
Ian Rogersd81871c2011-10-03 13:57:23 -07005011}
5012
Andreas Gampe896df402014-10-20 22:25:29 -07005013template <MethodVerifier::FieldAccessType kAccType>
5014void MethodVerifier::VerifyISFieldAccess(const Instruction* inst, const RegType& insn_type,
5015 bool is_primitive, bool is_static) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02005016 uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
Mathieu Chartierc7853442015-03-27 14:35:38 -07005017 ArtField* field;
Ian Rogersb94a27b2011-10-26 00:33:41 -07005018 if (is_static) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07005019 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07005020 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07005021 const RegType& object_type = work_line_->GetRegisterType(this, inst->VRegB_22c());
David Brazdil68b5c0b2016-01-19 14:25:29 +00005022
5023 // One is not allowed to access fields on uninitialized references, except to write to
5024 // fields in the constructor (before calling another constructor).
5025 // GetInstanceField does an assignability check which will fail for uninitialized types.
5026 // We thus modify the type if the uninitialized reference is a "this" reference (this also
5027 // checks at the same time that we're verifying a constructor).
5028 bool should_adjust = (kAccType == FieldAccessType::kAccPut) &&
5029 object_type.IsUninitializedThisReference();
5030 const RegType& adjusted_type = should_adjust
5031 ? GetRegTypeCache()->FromUninitialized(object_type)
5032 : object_type;
5033 field = GetInstanceField(adjusted_type, field_idx);
Andreas Gampe896df402014-10-20 22:25:29 -07005034 if (UNLIKELY(have_pending_hard_failure_)) {
5035 return;
5036 }
Alex Light4a2c8fc2016-02-12 11:01:54 -08005037 if (should_adjust) {
5038 if (field == nullptr) {
5039 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "Might be accessing a superclass instance field prior "
5040 << "to the superclass being initialized in "
David Sehr709b0702016-10-13 09:12:37 -07005041 << dex_file_->PrettyMethod(dex_method_idx_);
Alex Light4a2c8fc2016-02-12 11:01:54 -08005042 } else if (field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
5043 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "cannot access superclass instance field "
David Sehr709b0702016-10-13 09:12:37 -07005044 << field->PrettyField() << " of a not fully initialized "
Alex Light4a2c8fc2016-02-12 11:01:54 -08005045 << "object within the context of "
David Sehr709b0702016-10-13 09:12:37 -07005046 << dex_file_->PrettyMethod(dex_method_idx_);
Alex Light4a2c8fc2016-02-12 11:01:54 -08005047 return;
5048 }
5049 }
Ian Rogersb94a27b2011-10-26 00:33:41 -07005050 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00005051 const RegType* field_type = nullptr;
Ian Rogers7b078e82014-09-10 14:44:24 -07005052 if (field != nullptr) {
Andreas Gampe896df402014-10-20 22:25:29 -07005053 if (kAccType == FieldAccessType::kAccPut) {
5054 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
David Sehr709b0702016-10-13 09:12:37 -07005055 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << field->PrettyField()
Andreas Gampe896df402014-10-20 22:25:29 -07005056 << " from other class " << GetDeclaringClass();
Aart Bikc2bc2652016-05-23 14:58:49 -07005057 // Keep hunting for possible hard fails.
Andreas Gampe896df402014-10-20 22:25:29 -07005058 }
5059 }
5060
Mathieu Chartier3398c782016-09-30 10:27:43 -07005061 ObjPtr<mirror::Class> field_type_class =
Vladimir Marko208f6702017-12-08 12:00:50 +00005062 can_load_classes_ ? field->ResolveType() : field->LookupResolvedType();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07005063 if (field_type_class != nullptr) {
Mathieu Chartier3398c782016-09-30 10:27:43 -07005064 field_type = &FromClass(field->GetTypeDescriptor(),
Mathieu Chartier1cc62e42016-10-03 18:01:28 -07005065 field_type_class.Ptr(),
Andreas Gampef23f33d2015-06-23 14:18:17 -07005066 field_type_class->CannotBeAssignedFromOtherTypes());
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08005067 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07005068 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
5069 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07005070 }
Andreas Gampe7da4c402017-08-25 11:30:48 -07005071 } else {
5072 // If we don't have the field (it seems we failed resolution) and this is a PUT, we need to
5073 // redo verification at runtime as the field may be final, unless the field id shows it's in
5074 // the same class.
5075 //
5076 // For simplicity, it is OK to not distinguish compile-time vs runtime, and post this an
5077 // ACCESS_FIELD failure at runtime. This has the same effect as NO_FIELD - punting the class
5078 // to the access-checks interpreter.
5079 //
5080 // Note: see b/34966607. This and above may be changed in the future.
5081 if (kAccType == FieldAccessType::kAccPut) {
5082 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
5083 const char* field_class_descriptor = dex_file_->GetFieldDeclaringClassDescriptor(field_id);
5084 const RegType* field_class_type = &reg_types_.FromDescriptor(GetClassLoader(),
5085 field_class_descriptor,
5086 false);
5087 if (!field_class_type->Equals(GetDeclaringClass())) {
5088 Fail(VERIFY_ERROR_ACCESS_FIELD) << "could not check field put for final field modify of "
5089 << field_class_descriptor
5090 << "."
5091 << dex_file_->GetFieldName(field_id)
5092 << " from other class "
5093 << GetDeclaringClass();
5094 }
5095 }
Ian Rogers0d604842012-04-16 14:50:24 -07005096 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07005097 if (field_type == nullptr) {
5098 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
5099 const char* descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
Mathieu Chartierbf99f772014-08-23 16:37:27 -07005100 field_type = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07005101 }
Sebastien Hertz757b3042014-03-28 14:34:28 +01005102 DCHECK(field_type != nullptr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02005103 const uint32_t vregA = (is_static) ? inst->VRegA_21c() : inst->VRegA_22c();
Andreas Gampe896df402014-10-20 22:25:29 -07005104 static_assert(kAccType == FieldAccessType::kAccPut || kAccType == FieldAccessType::kAccGet,
5105 "Unexpected third access type");
5106 if (kAccType == FieldAccessType::kAccPut) {
5107 // sput or iput.
5108 if (is_primitive) {
5109 VerifyPrimitivePut(*field_type, insn_type, vregA);
Ian Rogersad0b3a32012-04-16 14:50:24 -07005110 } else {
David Brazdilca3c8c32016-09-06 14:04:48 +01005111 if (!insn_type.IsAssignableFrom(*field_type, this)) {
Vladimir Marko414000e2015-06-23 17:45:21 +01005112 // If the field type is not a reference, this is a global failure rather than
5113 // a class change failure as the instructions and the descriptors for the type
5114 // should have been consistent within the same file at compile time.
5115 VerifyError error = field_type->IsReferenceTypes() ? VERIFY_ERROR_BAD_CLASS_SOFT
5116 : VERIFY_ERROR_BAD_CLASS_HARD;
David Sehr709b0702016-10-13 09:12:37 -07005117 Fail(error) << "expected field " << ArtField::PrettyField(field)
Vladimir Marko414000e2015-06-23 17:45:21 +01005118 << " to be compatible with type '" << insn_type
5119 << "' but found type '" << *field_type
5120 << "' in put-object";
Andreas Gampe896df402014-10-20 22:25:29 -07005121 return;
5122 }
5123 work_line_->VerifyRegisterType(this, vregA, *field_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07005124 }
Andreas Gampe896df402014-10-20 22:25:29 -07005125 } else if (kAccType == FieldAccessType::kAccGet) {
5126 // sget or iget.
5127 if (is_primitive) {
5128 if (field_type->Equals(insn_type) ||
5129 (field_type->IsFloat() && insn_type.IsInteger()) ||
5130 (field_type->IsDouble() && insn_type.IsLong())) {
5131 // expected that read is of the correct primitive type or that int reads are reading
5132 // floats or long reads are reading doubles
5133 } else {
5134 // This is a global failure rather than a class change failure as the instructions and
5135 // the descriptors for the type should have been consistent within the same file at
5136 // compile time
David Sehr709b0702016-10-13 09:12:37 -07005137 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << ArtField::PrettyField(field)
Andreas Gampe896df402014-10-20 22:25:29 -07005138 << " to be of type '" << insn_type
5139 << "' but found type '" << *field_type << "' in get";
5140 return;
5141 }
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08005142 } else {
David Brazdilca3c8c32016-09-06 14:04:48 +01005143 if (!insn_type.IsAssignableFrom(*field_type, this)) {
Vladimir Marko414000e2015-06-23 17:45:21 +01005144 // If the field type is not a reference, this is a global failure rather than
5145 // a class change failure as the instructions and the descriptors for the type
5146 // should have been consistent within the same file at compile time.
5147 VerifyError error = field_type->IsReferenceTypes() ? VERIFY_ERROR_BAD_CLASS_SOFT
5148 : VERIFY_ERROR_BAD_CLASS_HARD;
David Sehr709b0702016-10-13 09:12:37 -07005149 Fail(error) << "expected field " << ArtField::PrettyField(field)
Vladimir Marko414000e2015-06-23 17:45:21 +01005150 << " to be compatible with type '" << insn_type
5151 << "' but found type '" << *field_type
5152 << "' in get-object";
Andreas Gampe890da292015-07-06 17:20:18 -07005153 if (error != VERIFY_ERROR_BAD_CLASS_HARD) {
Andreas Gampead238ce2015-08-24 21:13:08 -07005154 work_line_->SetRegisterType<LockOp::kClear>(this, vregA, reg_types_.Conflict());
Andreas Gampe890da292015-07-06 17:20:18 -07005155 }
Andreas Gampe896df402014-10-20 22:25:29 -07005156 return;
5157 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07005158 }
Andreas Gampe896df402014-10-20 22:25:29 -07005159 if (!field_type->IsLowHalf()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07005160 work_line_->SetRegisterType<LockOp::kClear>(this, vregA, *field_type);
Andreas Gampe896df402014-10-20 22:25:29 -07005161 } else {
5162 work_line_->SetRegisterTypeWide(this, vregA, *field_type, field_type->HighHalf(&reg_types_));
5163 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07005164 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07005165 LOG(FATAL) << "Unexpected case.";
Ian Rogersd81871c2011-10-03 13:57:23 -07005166 }
5167}
5168
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08005169ArtField* MethodVerifier::GetQuickFieldAccess(const Instruction* inst, RegisterLine* reg_line) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08005170 DCHECK(IsInstructionIGetQuickOrIPutQuick(inst->Opcode())) << inst->Opcode();
Ian Rogers7b078e82014-09-10 14:44:24 -07005171 const RegType& object_type = reg_line->GetRegisterType(this, inst->VRegB_22c());
Ian Rogers9bc54402014-04-17 16:40:01 -07005172 if (!object_type.HasClass()) {
5173 VLOG(verifier) << "Failed to get mirror::Class* from '" << object_type << "'";
5174 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02005175 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02005176 uint32_t field_offset = static_cast<uint32_t>(inst->VRegC_22c());
Mathieu Chartierc7853442015-03-27 14:35:38 -07005177 ArtField* const f = ArtField::FindInstanceFieldWithOffset(object_type.GetClass(), field_offset);
Sebastien Hertz479fc1e2014-04-04 17:51:34 +02005178 if (f == nullptr) {
5179 VLOG(verifier) << "Failed to find instance field at offset '" << field_offset
David Sehr709b0702016-10-13 09:12:37 -07005180 << "' from '" << mirror::Class::PrettyDescriptor(object_type.GetClass()) << "'";
Andreas Gampe8ddb7f52017-12-04 10:14:08 -08005181 } else {
5182 DCHECK_EQ(f->GetOffset().Uint32Value(), field_offset);
Sebastien Hertz479fc1e2014-04-04 17:51:34 +02005183 }
5184 return f;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02005185}
5186
Andreas Gampe896df402014-10-20 22:25:29 -07005187template <MethodVerifier::FieldAccessType kAccType>
5188void MethodVerifier::VerifyQuickFieldAccess(const Instruction* inst, const RegType& insn_type,
5189 bool is_primitive) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07005190 DCHECK(Runtime::Current()->IsStarted() || verify_to_dump_);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02005191
Mathieu Chartierc7853442015-03-27 14:35:38 -07005192 ArtField* field = GetQuickFieldAccess(inst, work_line_.get());
Ian Rogers7b078e82014-09-10 14:44:24 -07005193 if (field == nullptr) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02005194 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field from " << inst->Name();
5195 return;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02005196 }
Andreas Gampe896df402014-10-20 22:25:29 -07005197
5198 // For an IPUT_QUICK, we now test for final flag of the field.
5199 if (kAccType == FieldAccessType::kAccPut) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02005200 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
David Sehr709b0702016-10-13 09:12:37 -07005201 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << field->PrettyField()
Sebastien Hertzc15853b2013-06-25 17:36:27 +02005202 << " from other class " << GetDeclaringClass();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02005203 return;
5204 }
5205 }
Andreas Gampe896df402014-10-20 22:25:29 -07005206
5207 // Get the field type.
5208 const RegType* field_type;
5209 {
Vladimir Marko4098a7a2017-11-06 16:00:51 +00005210 ObjPtr<mirror::Class> field_type_class =
Vladimir Marko208f6702017-12-08 12:00:50 +00005211 can_load_classes_ ? field->ResolveType() : field->LookupResolvedType();
Andreas Gampe896df402014-10-20 22:25:29 -07005212
5213 if (field_type_class != nullptr) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07005214 field_type = &FromClass(field->GetTypeDescriptor(),
Mathieu Chartier1cc62e42016-10-03 18:01:28 -07005215 field_type_class.Ptr(),
Andreas Gampef23f33d2015-06-23 14:18:17 -07005216 field_type_class->CannotBeAssignedFromOtherTypes());
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02005217 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07005218 Thread* self = Thread::Current();
5219 DCHECK(!can_load_classes_ || self->IsExceptionPending());
5220 self->ClearException();
5221 field_type = &reg_types_.FromDescriptor(field->GetDeclaringClass()->GetClassLoader(),
Mathieu Chartierde40d472015-10-15 17:47:48 -07005222 field->GetTypeDescriptor(),
5223 false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02005224 }
Andreas Gampe896df402014-10-20 22:25:29 -07005225 if (field_type == nullptr) {
5226 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field type from " << inst->Name();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02005227 return;
5228 }
Andreas Gampe896df402014-10-20 22:25:29 -07005229 }
5230
5231 const uint32_t vregA = inst->VRegA_22c();
5232 static_assert(kAccType == FieldAccessType::kAccPut || kAccType == FieldAccessType::kAccGet,
5233 "Unexpected third access type");
5234 if (kAccType == FieldAccessType::kAccPut) {
5235 if (is_primitive) {
5236 // Primitive field assignability rules are weaker than regular assignability rules
5237 bool instruction_compatible;
5238 bool value_compatible;
5239 const RegType& value_type = work_line_->GetRegisterType(this, vregA);
5240 if (field_type->IsIntegralTypes()) {
5241 instruction_compatible = insn_type.IsIntegralTypes();
5242 value_compatible = value_type.IsIntegralTypes();
5243 } else if (field_type->IsFloat()) {
5244 instruction_compatible = insn_type.IsInteger(); // no [is]put-float, so expect [is]put-int
5245 value_compatible = value_type.IsFloatTypes();
5246 } else if (field_type->IsLong()) {
5247 instruction_compatible = insn_type.IsLong();
5248 value_compatible = value_type.IsLongTypes();
5249 } else if (field_type->IsDouble()) {
5250 instruction_compatible = insn_type.IsLong(); // no [is]put-double, so expect [is]put-long
5251 value_compatible = value_type.IsDoubleTypes();
5252 } else {
5253 instruction_compatible = false; // reference field with primitive store
5254 value_compatible = false; // unused
5255 }
5256 if (!instruction_compatible) {
5257 // This is a global failure rather than a class change failure as the instructions and
5258 // the descriptors for the type should have been consistent within the same file at
5259 // compile time
David Sehr709b0702016-10-13 09:12:37 -07005260 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << ArtField::PrettyField(field)
Andreas Gampe896df402014-10-20 22:25:29 -07005261 << " to be of type '" << insn_type
5262 << "' but found type '" << *field_type
5263 << "' in put";
5264 return;
5265 }
5266 if (!value_compatible) {
5267 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
5268 << " of type " << value_type
5269 << " but expected " << *field_type
David Sehr709b0702016-10-13 09:12:37 -07005270 << " for store to " << ArtField::PrettyField(field) << " in put";
Andreas Gampe896df402014-10-20 22:25:29 -07005271 return;
5272 }
5273 } else {
David Brazdilca3c8c32016-09-06 14:04:48 +01005274 if (!insn_type.IsAssignableFrom(*field_type, this)) {
David Sehr709b0702016-10-13 09:12:37 -07005275 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << ArtField::PrettyField(field)
Andreas Gampe896df402014-10-20 22:25:29 -07005276 << " to be compatible with type '" << insn_type
5277 << "' but found type '" << *field_type
5278 << "' in put-object";
5279 return;
5280 }
5281 work_line_->VerifyRegisterType(this, vregA, *field_type);
5282 }
5283 } else if (kAccType == FieldAccessType::kAccGet) {
5284 if (is_primitive) {
5285 if (field_type->Equals(insn_type) ||
5286 (field_type->IsFloat() && insn_type.IsIntegralTypes()) ||
5287 (field_type->IsDouble() && insn_type.IsLongTypes())) {
5288 // expected that read is of the correct primitive type or that int reads are reading
5289 // floats or long reads are reading doubles
5290 } else {
5291 // This is a global failure rather than a class change failure as the instructions and
5292 // the descriptors for the type should have been consistent within the same file at
5293 // compile time
David Sehr709b0702016-10-13 09:12:37 -07005294 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << ArtField::PrettyField(field)
Andreas Gampe896df402014-10-20 22:25:29 -07005295 << " to be of type '" << insn_type
5296 << "' but found type '" << *field_type << "' in Get";
5297 return;
5298 }
5299 } else {
David Brazdilca3c8c32016-09-06 14:04:48 +01005300 if (!insn_type.IsAssignableFrom(*field_type, this)) {
David Sehr709b0702016-10-13 09:12:37 -07005301 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << ArtField::PrettyField(field)
Andreas Gampe896df402014-10-20 22:25:29 -07005302 << " to be compatible with type '" << insn_type
5303 << "' but found type '" << *field_type
5304 << "' in get-object";
Andreas Gampead238ce2015-08-24 21:13:08 -07005305 work_line_->SetRegisterType<LockOp::kClear>(this, vregA, reg_types_.Conflict());
Andreas Gampe896df402014-10-20 22:25:29 -07005306 return;
5307 }
5308 }
5309 if (!field_type->IsLowHalf()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07005310 work_line_->SetRegisterType<LockOp::kClear>(this, vregA, *field_type);
Andreas Gampe896df402014-10-20 22:25:29 -07005311 } else {
5312 work_line_->SetRegisterTypeWide(this, vregA, *field_type, field_type->HighHalf(&reg_types_));
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02005313 }
5314 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07005315 LOG(FATAL) << "Unexpected case.";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02005316 }
5317}
5318
Ian Rogers776ac1f2012-04-13 23:36:36 -07005319bool MethodVerifier::CheckNotMoveException(const uint16_t* insns, int insn_idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07005320 if ((insns[insn_idx] & 0xff) == Instruction::MOVE_EXCEPTION) {
jeffhaod5347e02012-03-22 17:25:05 -07005321 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-exception";
Ian Rogersd81871c2011-10-03 13:57:23 -07005322 return false;
5323 }
5324 return true;
5325}
5326
Stephen Kyle9bc61992014-09-22 13:53:15 +01005327bool MethodVerifier::CheckNotMoveResult(const uint16_t* insns, int insn_idx) {
5328 if (((insns[insn_idx] & 0xff) >= Instruction::MOVE_RESULT) &&
5329 ((insns[insn_idx] & 0xff) <= Instruction::MOVE_RESULT_OBJECT)) {
5330 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-result*";
5331 return false;
5332 }
5333 return true;
5334}
5335
5336bool MethodVerifier::CheckNotMoveExceptionOrMoveResult(const uint16_t* insns, int insn_idx) {
5337 return (CheckNotMoveException(insns, insn_idx) && CheckNotMoveResult(insns, insn_idx));
5338}
5339
Ian Rogersebbdd872014-07-07 23:53:08 -07005340bool MethodVerifier::UpdateRegisters(uint32_t next_insn, RegisterLine* merge_line,
5341 bool update_merge_line) {
Ian Rogersd81871c2011-10-03 13:57:23 -07005342 bool changed = true;
5343 RegisterLine* target_line = reg_table_.GetLine(next_insn);
Mathieu Chartierde40d472015-10-15 17:47:48 -07005344 if (!GetInstructionFlags(next_insn).IsVisitedOrChanged()) {
jeffhaobdb76512011-09-07 11:43:16 -07005345 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07005346 * We haven't processed this instruction before, and we haven't touched the registers here, so
5347 * there's nothing to "merge". Copy the registers over and mark it as changed. (This is the
5348 * only way a register can transition out of "unknown", so this is not just an optimization.)
jeffhaobdb76512011-09-07 11:43:16 -07005349 */
Andreas Gampea727e372015-08-25 09:22:37 -07005350 target_line->CopyFromLine(merge_line);
Mathieu Chartierde40d472015-10-15 17:47:48 -07005351 if (GetInstructionFlags(next_insn).IsReturn()) {
Jeff Haob24b4a72013-07-31 13:47:31 -07005352 // Verify that the monitor stack is empty on return.
Andreas Gampea727e372015-08-25 09:22:37 -07005353 merge_line->VerifyMonitorStackEmpty(this);
5354
Ian Rogersb8c78592013-07-25 23:52:52 +00005355 // For returns we only care about the operand to the return, all other registers are dead.
5356 // Initialize them as conflicts so they don't add to GC and deoptimization information.
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08005357 const Instruction* ret_inst = &code_item_accessor_.InstructionAt(next_insn);
Andreas Gampea727e372015-08-25 09:22:37 -07005358 AdjustReturnLine(this, ret_inst, target_line);
Aart Bik31883642016-06-06 15:02:44 -07005359 // Directly bail if a hard failure was found.
Aart Bikb0526322016-06-01 14:06:00 -07005360 if (have_pending_hard_failure_) {
5361 return false;
5362 }
Ian Rogersb8c78592013-07-25 23:52:52 +00005363 }
jeffhaobdb76512011-09-07 11:43:16 -07005364 } else {
Mathieu Chartier361e04a2016-02-16 14:06:35 -08005365 RegisterLineArenaUniquePtr copy;
Andreas Gampe92d77202017-12-06 20:49:00 -08005366 if (UNLIKELY(VLOG_IS_ON(verifier_debug))) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07005367 copy.reset(RegisterLine::Create(target_line->NumRegs(), this));
Ian Rogers7b0c5b42012-02-16 15:29:07 -08005368 copy->CopyFromLine(target_line);
5369 }
Ian Rogers7b078e82014-09-10 14:44:24 -07005370 changed = target_line->MergeRegisters(this, merge_line);
Ian Rogersad0b3a32012-04-16 14:50:24 -07005371 if (have_pending_hard_failure_) {
Ian Rogersd81871c2011-10-03 13:57:23 -07005372 return false;
jeffhaobdb76512011-09-07 11:43:16 -07005373 }
Andreas Gampe92d77202017-12-06 20:49:00 -08005374 if (UNLIKELY(VLOG_IS_ON(verifier_debug)) && changed) {
Elliott Hughes398f64b2012-03-26 18:05:48 -07005375 LogVerifyInfo() << "Merging at [" << reinterpret_cast<void*>(work_insn_idx_) << "]"
Elliott Hughesc073b072012-05-24 19:29:17 -07005376 << " to [" << reinterpret_cast<void*>(next_insn) << "]: " << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07005377 << copy->Dump(this) << " MERGE\n"
5378 << merge_line->Dump(this) << " ==\n"
5379 << target_line->Dump(this) << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07005380 }
Ian Rogersebbdd872014-07-07 23:53:08 -07005381 if (update_merge_line && changed) {
5382 merge_line->CopyFromLine(target_line);
5383 }
jeffhaobdb76512011-09-07 11:43:16 -07005384 }
Ian Rogersd81871c2011-10-03 13:57:23 -07005385 if (changed) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07005386 GetInstructionFlags(next_insn).SetChanged();
jeffhaobdb76512011-09-07 11:43:16 -07005387 }
5388 return true;
5389}
5390
Ian Rogers7b3ddd22013-02-21 15:19:52 -08005391InstructionFlags* MethodVerifier::CurrentInsnFlags() {
Mathieu Chartierde40d472015-10-15 17:47:48 -07005392 return &GetInstructionFlags(work_insn_idx_);
Ian Rogers776ac1f2012-04-13 23:36:36 -07005393}
5394
Ian Rogersd8f69b02014-09-10 21:43:52 +00005395const RegType& MethodVerifier::GetMethodReturnType() {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07005396 if (return_type_ == nullptr) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07005397 if (mirror_method_ != nullptr) {
Vladimir Markob45528c2017-07-27 14:14:28 +01005398 ObjPtr<mirror::Class> return_type_class = can_load_classes_
5399 ? mirror_method_->ResolveReturnType()
5400 : mirror_method_->LookupResolvedReturnType();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07005401 if (return_type_class != nullptr) {
Andreas Gampef23f33d2015-06-23 14:18:17 -07005402 return_type_ = &FromClass(mirror_method_->GetReturnTypeDescriptor(),
Vladimir Markob45528c2017-07-27 14:14:28 +01005403 return_type_class.Ptr(),
Andreas Gampef23f33d2015-06-23 14:18:17 -07005404 return_type_class->CannotBeAssignedFromOtherTypes());
Ian Rogersfc0e94b2013-09-23 23:51:32 -07005405 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07005406 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
5407 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07005408 }
5409 }
5410 if (return_type_ == nullptr) {
5411 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
5412 const DexFile::ProtoId& proto_id = dex_file_->GetMethodPrototype(method_id);
Andreas Gampea5b09a62016-11-17 15:21:22 -08005413 dex::TypeIndex return_type_idx = proto_id.return_type_idx_;
Ian Rogersfc0e94b2013-09-23 23:51:32 -07005414 const char* descriptor = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(return_type_idx));
Mathieu Chartierbf99f772014-08-23 16:37:27 -07005415 return_type_ = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07005416 }
5417 }
5418 return *return_type_;
Ian Rogersad0b3a32012-04-16 14:50:24 -07005419}
5420
Ian Rogersd8f69b02014-09-10 21:43:52 +00005421const RegType& MethodVerifier::GetDeclaringClass() {
Ian Rogers7b078e82014-09-10 14:44:24 -07005422 if (declaring_class_ == nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08005423 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
Brian Carlstrom93c33962013-07-26 10:37:43 -07005424 const char* descriptor
5425 = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(method_id.class_idx_));
Mathieu Chartiere401d142015-04-22 13:56:20 -07005426 if (mirror_method_ != nullptr) {
Ian Rogers637c65b2013-05-31 11:46:00 -07005427 mirror::Class* klass = mirror_method_->GetDeclaringClass();
Mathieu Chartierde40d472015-10-15 17:47:48 -07005428 declaring_class_ = &FromClass(descriptor, klass, klass->CannotBeAssignedFromOtherTypes());
Ian Rogers637c65b2013-05-31 11:46:00 -07005429 } else {
Mathieu Chartierbf99f772014-08-23 16:37:27 -07005430 declaring_class_ = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers637c65b2013-05-31 11:46:00 -07005431 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07005432 }
Ian Rogers637c65b2013-05-31 11:46:00 -07005433 return *declaring_class_;
Ian Rogersad0b3a32012-04-16 14:50:24 -07005434}
5435
Ian Rogers2bcb4a42012-11-08 10:39:18 -08005436std::vector<int32_t> MethodVerifier::DescribeVRegs(uint32_t dex_pc) {
5437 RegisterLine* line = reg_table_.GetLine(dex_pc);
Sebastien Hertzaa0c00c2014-03-14 17:58:54 +01005438 DCHECK(line != nullptr) << "No register line at DEX pc " << StringPrintf("0x%x", dex_pc);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08005439 std::vector<int32_t> result;
5440 for (size_t i = 0; i < line->NumRegs(); ++i) {
Ian Rogers7b078e82014-09-10 14:44:24 -07005441 const RegType& type = line->GetRegisterType(this, i);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08005442 if (type.IsConstant()) {
5443 result.push_back(type.IsPreciseConstant() ? kConstant : kImpreciseConstant);
Ian Rogers7b078e82014-09-10 14:44:24 -07005444 const ConstantType* const_val = down_cast<const ConstantType*>(&type);
5445 result.push_back(const_val->ConstantValue());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08005446 } else if (type.IsConstantLo()) {
5447 result.push_back(type.IsPreciseConstantLo() ? kConstant : kImpreciseConstant);
Ian Rogers7b078e82014-09-10 14:44:24 -07005448 const ConstantType* const_val = down_cast<const ConstantType*>(&type);
5449 result.push_back(const_val->ConstantValueLo());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08005450 } else if (type.IsConstantHi()) {
5451 result.push_back(type.IsPreciseConstantHi() ? kConstant : kImpreciseConstant);
Ian Rogers7b078e82014-09-10 14:44:24 -07005452 const ConstantType* const_val = down_cast<const ConstantType*>(&type);
5453 result.push_back(const_val->ConstantValueHi());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08005454 } else if (type.IsIntegralTypes()) {
5455 result.push_back(kIntVReg);
5456 result.push_back(0);
5457 } else if (type.IsFloat()) {
5458 result.push_back(kFloatVReg);
5459 result.push_back(0);
5460 } else if (type.IsLong()) {
5461 result.push_back(kLongLoVReg);
5462 result.push_back(0);
5463 result.push_back(kLongHiVReg);
5464 result.push_back(0);
5465 ++i;
5466 } else if (type.IsDouble()) {
5467 result.push_back(kDoubleLoVReg);
5468 result.push_back(0);
5469 result.push_back(kDoubleHiVReg);
5470 result.push_back(0);
5471 ++i;
5472 } else if (type.IsUndefined() || type.IsConflict() || type.IsHighHalf()) {
5473 result.push_back(kUndefined);
5474 result.push_back(0);
5475 } else {
Ian Rogers7b3ddd22013-02-21 15:19:52 -08005476 CHECK(type.IsNonZeroReferenceTypes());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08005477 result.push_back(kReferenceVReg);
5478 result.push_back(0);
5479 }
5480 }
5481 return result;
5482}
5483
Ian Rogersd8f69b02014-09-10 21:43:52 +00005484const RegType& MethodVerifier::DetermineCat1Constant(int32_t value, bool precise) {
Sebastien Hertz849600b2013-12-20 10:28:08 +01005485 if (precise) {
5486 // Precise constant type.
5487 return reg_types_.FromCat1Const(value, true);
5488 } else {
5489 // Imprecise constant type.
5490 if (value < -32768) {
5491 return reg_types_.IntConstant();
5492 } else if (value < -128) {
5493 return reg_types_.ShortConstant();
5494 } else if (value < 0) {
5495 return reg_types_.ByteConstant();
5496 } else if (value == 0) {
5497 return reg_types_.Zero();
5498 } else if (value == 1) {
5499 return reg_types_.One();
5500 } else if (value < 128) {
5501 return reg_types_.PosByteConstant();
5502 } else if (value < 32768) {
5503 return reg_types_.PosShortConstant();
5504 } else if (value < 65536) {
5505 return reg_types_.CharConstant();
5506 } else {
5507 return reg_types_.IntConstant();
5508 }
5509 }
5510}
5511
Elliott Hughes0a1038b2012-06-14 16:24:17 -07005512void MethodVerifier::Init() {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08005513 art::verifier::RegTypeCache::Init();
Brian Carlstrome7d856b2012-01-11 18:10:55 -08005514}
5515
Elliott Hughes0a1038b2012-06-14 16:24:17 -07005516void MethodVerifier::Shutdown() {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08005517 verifier::RegTypeCache::ShutDown();
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08005518}
jeffhaod1224c72012-02-29 13:43:08 -08005519
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -07005520void MethodVerifier::VisitStaticRoots(RootVisitor* visitor) {
5521 RegTypeCache::VisitStaticRoots(visitor);
Mathieu Chartier7c438b12014-09-12 17:01:24 -07005522}
5523
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -07005524void MethodVerifier::VisitRoots(RootVisitor* visitor, const RootInfo& root_info) {
5525 reg_types_.VisitRoots(visitor, root_info);
Mathieu Chartierc528dba2013-11-26 12:00:11 -08005526}
5527
Andreas Gampef23f33d2015-06-23 14:18:17 -07005528const RegType& MethodVerifier::FromClass(const char* descriptor,
5529 mirror::Class* klass,
5530 bool precise) {
5531 DCHECK(klass != nullptr);
5532 if (precise && !klass->IsInstantiable() && !klass->IsPrimitive()) {
5533 Fail(VerifyError::VERIFY_ERROR_NO_CLASS) << "Could not create precise reference for "
5534 << "non-instantiable klass " << descriptor;
5535 precise = false;
5536 }
5537 return reg_types_.FromClass(descriptor, klass, precise);
5538}
5539
Ian Rogersd81871c2011-10-03 13:57:23 -07005540} // namespace verifier
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07005541} // namespace art