blob: 66e578f312022ad2b5cee264c70f7da4d98719b7 [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"
David Sehr9e734c72018-01-04 17:56:19 -080034#include "dex/dex_file-inl.h"
35#include "dex/dex_file_exception_helpers.h"
36#include "dex/dex_instruction-inl.h"
37#include "dex/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 Chartier698ebbc2018-01-05 11:00:42 -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();
Andreas Gampe077d9db2018-01-19 18:54:14 -08001688
1689 // Might be asked to dump before the table is initialized.
1690 if (reg_table_.IsInitialized()) {
1691 RegisterLine* reg_line = reg_table_.GetLine(dex_pc);
1692 if (reg_line != nullptr) {
1693 vios->Stream() << reg_line->Dump(this) << "\n";
1694 }
jeffhaobdb76512011-09-07 11:43:16 -07001695 }
Andreas Gampe077d9db2018-01-19 18:54:14 -08001696
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001697 vios->Stream()
Mathieu Chartierde40d472015-10-15 17:47:48 -07001698 << StringPrintf("0x%04zx", dex_pc) << ": " << GetInstructionFlags(dex_pc).ToString() << " ";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001699 const bool kDumpHexOfInstruction = false;
1700 if (kDumpHexOfInstruction) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001701 vios->Stream() << inst->DumpHex(5) << " ";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001702 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001703 vios->Stream() << inst->DumpString(dex_file_) << "\n";
jeffhaoba5ebb92011-08-25 17:24:37 -07001704 }
jeffhaobdb76512011-09-07 11:43:16 -07001705}
1706
Ian Rogersd81871c2011-10-03 13:57:23 -07001707static bool IsPrimitiveDescriptor(char descriptor) {
1708 switch (descriptor) {
jeffhaobdb76512011-09-07 11:43:16 -07001709 case 'I':
1710 case 'C':
1711 case 'S':
1712 case 'B':
1713 case 'Z':
jeffhaobdb76512011-09-07 11:43:16 -07001714 case 'F':
1715 case 'D':
1716 case 'J':
Ian Rogersd81871c2011-10-03 13:57:23 -07001717 return true;
jeffhaobdb76512011-09-07 11:43:16 -07001718 default:
1719 return false;
1720 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001721}
1722
Ian Rogers776ac1f2012-04-13 23:36:36 -07001723bool MethodVerifier::SetTypesFromSignature() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001724 RegisterLine* reg_line = reg_table_.GetLine(0);
Andreas Gampeef0b1a12015-06-19 20:37:46 -07001725
1726 // Should have been verified earlier.
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001727 DCHECK_GE(code_item_accessor_.RegistersSize(), code_item_accessor_.InsSize());
Andreas Gampeef0b1a12015-06-19 20:37:46 -07001728
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001729 uint32_t arg_start = code_item_accessor_.RegistersSize() - code_item_accessor_.InsSize();
1730 size_t expected_args = code_item_accessor_.InsSize(); /* long/double count as two */
jeffhaobdb76512011-09-07 11:43:16 -07001731
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001732 // Include the "this" pointer.
Ian Rogersd81871c2011-10-03 13:57:23 -07001733 size_t cur_arg = 0;
Ian Rogersad0b3a32012-04-16 14:50:24 -07001734 if (!IsStatic()) {
Andreas Gampeef0b1a12015-06-19 20:37:46 -07001735 if (expected_args == 0) {
1736 // Expect at least a receiver.
1737 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected 0 args, but method is not static";
1738 return false;
1739 }
1740
Ian Rogersd81871c2011-10-03 13:57:23 -07001741 // If this is a constructor for a class other than java.lang.Object, mark the first ("this")
1742 // argument as uninitialized. This restricts field access until the superclass constructor is
1743 // called.
Ian Rogersd8f69b02014-09-10 21:43:52 +00001744 const RegType& declaring_class = GetDeclaringClass();
Andreas Gampef10b6e12015-08-12 10:48:12 -07001745 if (IsConstructor()) {
1746 if (declaring_class.IsJavaLangObject()) {
1747 // "this" is implicitly initialized.
1748 reg_line->SetThisInitialized();
Andreas Gampead238ce2015-08-24 21:13:08 -07001749 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, declaring_class);
Andreas Gampef10b6e12015-08-12 10:48:12 -07001750 } else {
Andreas Gampead238ce2015-08-24 21:13:08 -07001751 reg_line->SetRegisterType<LockOp::kClear>(
1752 this,
1753 arg_start + cur_arg,
1754 reg_types_.UninitializedThisArgument(declaring_class));
Andreas Gampef10b6e12015-08-12 10:48:12 -07001755 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001756 } else {
Andreas Gampead238ce2015-08-24 21:13:08 -07001757 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, declaring_class);
jeffhaobdb76512011-09-07 11:43:16 -07001758 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001759 cur_arg++;
jeffhaobdb76512011-09-07 11:43:16 -07001760 }
1761
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001762 const DexFile::ProtoId& proto_id =
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001763 dex_file_->GetMethodPrototype(dex_file_->GetMethodId(dex_method_idx_));
Ian Rogers0571d352011-11-03 19:51:38 -07001764 DexFileParameterIterator iterator(*dex_file_, proto_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07001765
1766 for (; iterator.HasNext(); iterator.Next()) {
1767 const char* descriptor = iterator.GetDescriptor();
Ian Rogers7b078e82014-09-10 14:44:24 -07001768 if (descriptor == nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001769 LOG(FATAL) << "Null descriptor";
1770 }
1771 if (cur_arg >= expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07001772 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1773 << " args, found more (" << descriptor << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001774 return false;
1775 }
1776 switch (descriptor[0]) {
1777 case 'L':
1778 case '[':
1779 // We assume that reference arguments are initialized. The only way it could be otherwise
1780 // (assuming the caller was verified) is if the current method is <init>, but in that case
1781 // it's effectively considered initialized the instant we reach here (in the sense that we
1782 // can return without doing anything or call virtual methods).
1783 {
Andreas Gampe98be1a92017-08-28 08:25:45 -07001784 // Note: don't check access. No error would be thrown for declaring or passing an
1785 // inaccessible class. Only actual accesses to fields or methods will.
1786 const RegType& reg_type = ResolveClass<CheckAccess::kNo>(iterator.GetTypeIdx());
Sebastien Hertz2ed76f92014-04-22 17:11:08 +02001787 if (!reg_type.IsNonZeroReferenceTypes()) {
1788 DCHECK(HasFailures());
1789 return false;
1790 }
Andreas Gampead238ce2015-08-24 21:13:08 -07001791 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001792 }
1793 break;
1794 case 'Z':
Andreas Gampead238ce2015-08-24 21:13:08 -07001795 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Boolean());
Ian Rogersd81871c2011-10-03 13:57:23 -07001796 break;
1797 case 'C':
Andreas Gampead238ce2015-08-24 21:13:08 -07001798 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Char());
Ian Rogersd81871c2011-10-03 13:57:23 -07001799 break;
1800 case 'B':
Andreas Gampead238ce2015-08-24 21:13:08 -07001801 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Byte());
Ian Rogersd81871c2011-10-03 13:57:23 -07001802 break;
1803 case 'I':
Andreas Gampead238ce2015-08-24 21:13:08 -07001804 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Integer());
Ian Rogersd81871c2011-10-03 13:57:23 -07001805 break;
1806 case 'S':
Andreas Gampead238ce2015-08-24 21:13:08 -07001807 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Short());
Ian Rogersd81871c2011-10-03 13:57:23 -07001808 break;
1809 case 'F':
Andreas Gampead238ce2015-08-24 21:13:08 -07001810 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Float());
Ian Rogersd81871c2011-10-03 13:57:23 -07001811 break;
1812 case 'J':
1813 case 'D': {
Andreas Gampe77cd4d62014-06-19 17:29:48 -07001814 if (cur_arg + 1 >= expected_args) {
1815 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1816 << " args, found more (" << descriptor << ")";
1817 return false;
1818 }
1819
Ian Rogers7b078e82014-09-10 14:44:24 -07001820 const RegType* lo_half;
1821 const RegType* hi_half;
1822 if (descriptor[0] == 'J') {
1823 lo_half = &reg_types_.LongLo();
1824 hi_half = &reg_types_.LongHi();
1825 } else {
1826 lo_half = &reg_types_.DoubleLo();
1827 hi_half = &reg_types_.DoubleHi();
1828 }
1829 reg_line->SetRegisterTypeWide(this, arg_start + cur_arg, *lo_half, *hi_half);
Ian Rogersd81871c2011-10-03 13:57:23 -07001830 cur_arg++;
1831 break;
1832 }
1833 default:
Brian Carlstrom93c33962013-07-26 10:37:43 -07001834 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected signature type char '"
1835 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001836 return false;
1837 }
1838 cur_arg++;
1839 }
1840 if (cur_arg != expected_args) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001841 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1842 << " arguments, found " << cur_arg;
Ian Rogersd81871c2011-10-03 13:57:23 -07001843 return false;
1844 }
1845 const char* descriptor = dex_file_->GetReturnTypeDescriptor(proto_id);
1846 // Validate return type. We don't do the type lookup; just want to make sure that it has the right
1847 // format. Only major difference from the method argument format is that 'V' is supported.
1848 bool result;
1849 if (IsPrimitiveDescriptor(descriptor[0]) || descriptor[0] == 'V') {
1850 result = descriptor[1] == '\0';
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001851 } else if (descriptor[0] == '[') { // single/multi-dimensional array of object/primitive
Ian Rogersd81871c2011-10-03 13:57:23 -07001852 size_t i = 0;
1853 do {
1854 i++;
1855 } while (descriptor[i] == '['); // process leading [
1856 if (descriptor[i] == 'L') { // object array
1857 do {
1858 i++; // find closing ;
1859 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1860 result = descriptor[i] == ';';
1861 } else { // primitive array
1862 result = IsPrimitiveDescriptor(descriptor[i]) && descriptor[i + 1] == '\0';
1863 }
1864 } else if (descriptor[0] == 'L') {
1865 // could be more thorough here, but shouldn't be required
1866 size_t i = 0;
1867 do {
1868 i++;
1869 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1870 result = descriptor[i] == ';';
1871 } else {
1872 result = false;
1873 }
1874 if (!result) {
jeffhaod5347e02012-03-22 17:25:05 -07001875 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected char in return type descriptor '"
1876 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001877 }
1878 return result;
jeffhaobdb76512011-09-07 11:43:16 -07001879}
1880
Ian Rogers776ac1f2012-04-13 23:36:36 -07001881bool MethodVerifier::CodeFlowVerifyMethod() {
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001882 const uint16_t* insns = code_item_accessor_.Insns();
1883 const uint32_t insns_size = code_item_accessor_.InsnsSizeInCodeUnits();
jeffhaoba5ebb92011-08-25 17:24:37 -07001884
jeffhaobdb76512011-09-07 11:43:16 -07001885 /* Begin by marking the first instruction as "changed". */
Mathieu Chartierde40d472015-10-15 17:47:48 -07001886 GetInstructionFlags(0).SetChanged();
Ian Rogersd81871c2011-10-03 13:57:23 -07001887 uint32_t start_guess = 0;
jeffhaoba5ebb92011-08-25 17:24:37 -07001888
jeffhaobdb76512011-09-07 11:43:16 -07001889 /* Continue until no instructions are marked "changed". */
1890 while (true) {
Mathieu Chartier4306ef82014-12-19 18:41:47 -08001891 if (allow_thread_suspension_) {
1892 self_->AllowThreadSuspension();
1893 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001894 // Find the first marked one. Use "start_guess" as a way to find one quickly.
1895 uint32_t insn_idx = start_guess;
1896 for (; insn_idx < insns_size; insn_idx++) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07001897 if (GetInstructionFlags(insn_idx).IsChanged())
jeffhaobdb76512011-09-07 11:43:16 -07001898 break;
1899 }
jeffhaobdb76512011-09-07 11:43:16 -07001900 if (insn_idx == insns_size) {
1901 if (start_guess != 0) {
1902 /* try again, starting from the top */
1903 start_guess = 0;
1904 continue;
1905 } else {
1906 /* all flags are clear */
1907 break;
1908 }
1909 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001910 // We carry the working set of registers from instruction to instruction. If this address can
1911 // be the target of a branch (or throw) instruction, or if we're skipping around chasing
1912 // "changed" flags, we need to load the set of registers from the table.
1913 // Because we always prefer to continue on to the next instruction, we should never have a
1914 // situation where we have a stray "changed" flag set on an instruction that isn't a branch
1915 // target.
1916 work_insn_idx_ = insn_idx;
Mathieu Chartierde40d472015-10-15 17:47:48 -07001917 if (GetInstructionFlags(insn_idx).IsBranchTarget()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001918 work_line_->CopyFromLine(reg_table_.GetLine(insn_idx));
Ian Rogersebbdd872014-07-07 23:53:08 -07001919 } else if (kIsDebugBuild) {
jeffhaobdb76512011-09-07 11:43:16 -07001920 /*
1921 * Sanity check: retrieve the stored register line (assuming
1922 * a full table) and make sure it actually matches.
1923 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001924 RegisterLine* register_line = reg_table_.GetLine(insn_idx);
Ian Rogers7b078e82014-09-10 14:44:24 -07001925 if (register_line != nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001926 if (work_line_->CompareLine(register_line) != 0) {
1927 Dump(std::cout);
1928 std::cout << info_messages_.str();
David Sehr709b0702016-10-13 09:12:37 -07001929 LOG(FATAL) << "work_line diverged in " << dex_file_->PrettyMethod(dex_method_idx_)
Elliott Hughesc073b072012-05-24 19:29:17 -07001930 << "@" << reinterpret_cast<void*>(work_insn_idx_) << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07001931 << " work_line=" << work_line_->Dump(this) << "\n"
1932 << " expected=" << register_line->Dump(this);
Ian Rogersd81871c2011-10-03 13:57:23 -07001933 }
jeffhaobdb76512011-09-07 11:43:16 -07001934 }
jeffhaobdb76512011-09-07 11:43:16 -07001935 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001936 if (!CodeFlowVerifyInstruction(&start_guess)) {
David Sehr709b0702016-10-13 09:12:37 -07001937 std::string prepend(dex_file_->PrettyMethod(dex_method_idx_));
Ian Rogersad0b3a32012-04-16 14:50:24 -07001938 prepend += " failed to verify: ";
1939 PrependToLastFailMessage(prepend);
jeffhaoba5ebb92011-08-25 17:24:37 -07001940 return false;
1941 }
jeffhaobdb76512011-09-07 11:43:16 -07001942 /* Clear "changed" and mark as visited. */
Mathieu Chartierde40d472015-10-15 17:47:48 -07001943 GetInstructionFlags(insn_idx).SetVisited();
1944 GetInstructionFlags(insn_idx).ClearChanged();
jeffhaobdb76512011-09-07 11:43:16 -07001945 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001946
Andreas Gampe92d77202017-12-06 20:49:00 -08001947 if (UNLIKELY(VLOG_IS_ON(verifier_debug))) {
jeffhaobdb76512011-09-07 11:43:16 -07001948 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001949 * Scan for dead code. There's nothing "evil" about dead code
jeffhaobdb76512011-09-07 11:43:16 -07001950 * (besides the wasted space), but it indicates a flaw somewhere
1951 * down the line, possibly in the verifier.
1952 *
1953 * If we've substituted "always throw" instructions into the stream,
1954 * we are almost certainly going to have some dead code.
1955 */
1956 int dead_start = -1;
Mathieu Chartier1d2d4ff2017-09-23 16:11:06 -07001957
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001958 for (const DexInstructionPcPair& inst : code_item_accessor_) {
Mathieu Chartier2b2bef22017-10-26 17:10:19 -07001959 const uint32_t insn_idx = inst.DexPc();
jeffhaobdb76512011-09-07 11:43:16 -07001960 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001961 * Switch-statement data doesn't get "visited" by scanner. It
jeffhaobdb76512011-09-07 11:43:16 -07001962 * may or may not be preceded by a padding NOP (for alignment).
1963 */
1964 if (insns[insn_idx] == Instruction::kPackedSwitchSignature ||
1965 insns[insn_idx] == Instruction::kSparseSwitchSignature ||
1966 insns[insn_idx] == Instruction::kArrayDataSignature ||
Elliott Hughes380aaa72012-07-09 14:33:15 -07001967 (insns[insn_idx] == Instruction::NOP && (insn_idx + 1 < insns_size) &&
jeffhaobdb76512011-09-07 11:43:16 -07001968 (insns[insn_idx + 1] == Instruction::kPackedSwitchSignature ||
1969 insns[insn_idx + 1] == Instruction::kSparseSwitchSignature ||
1970 insns[insn_idx + 1] == Instruction::kArrayDataSignature))) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07001971 GetInstructionFlags(insn_idx).SetVisited();
jeffhaobdb76512011-09-07 11:43:16 -07001972 }
1973
Mathieu Chartierde40d472015-10-15 17:47:48 -07001974 if (!GetInstructionFlags(insn_idx).IsVisited()) {
Mathieu Chartier1d2d4ff2017-09-23 16:11:06 -07001975 if (dead_start < 0) {
jeffhaobdb76512011-09-07 11:43:16 -07001976 dead_start = insn_idx;
Mathieu Chartier1d2d4ff2017-09-23 16:11:06 -07001977 }
jeffhaobdb76512011-09-07 11:43:16 -07001978 } else if (dead_start >= 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001979 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start)
1980 << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaobdb76512011-09-07 11:43:16 -07001981 dead_start = -1;
1982 }
1983 }
1984 if (dead_start >= 0) {
Mathieu Chartier1d2d4ff2017-09-23 16:11:06 -07001985 LogVerifyInfo()
1986 << "dead code " << reinterpret_cast<void*>(dead_start)
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001987 << "-" << reinterpret_cast<void*>(code_item_accessor_.InsnsSizeInCodeUnits() - 1);
jeffhaoba5ebb92011-08-25 17:24:37 -07001988 }
Ian Rogersc9e463c2013-06-05 16:52:26 -07001989 // To dump the state of the verify after a method, do something like:
David Sehr709b0702016-10-13 09:12:37 -07001990 // if (dex_file_->PrettyMethod(dex_method_idx_) ==
Ian Rogersc9e463c2013-06-05 16:52:26 -07001991 // "boolean java.lang.String.equals(java.lang.Object)") {
1992 // LOG(INFO) << info_messages_.str();
1993 // }
jeffhaoba5ebb92011-08-25 17:24:37 -07001994 }
jeffhaobdb76512011-09-07 11:43:16 -07001995 return true;
1996}
1997
Andreas Gampe68df3202015-06-22 11:35:46 -07001998// Returns the index of the first final instance field of the given class, or kDexNoIndex if there
1999// is no such field.
Andreas Gampea5b09a62016-11-17 15:21:22 -08002000static uint32_t GetFirstFinalInstanceFieldIndex(const DexFile& dex_file, dex::TypeIndex type_idx) {
Andreas Gampe68df3202015-06-22 11:35:46 -07002001 const DexFile::ClassDef* class_def = dex_file.FindClassDef(type_idx);
2002 DCHECK(class_def != nullptr);
2003 const uint8_t* class_data = dex_file.GetClassData(*class_def);
2004 DCHECK(class_data != nullptr);
2005 ClassDataItemIterator it(dex_file, class_data);
Mathieu Chartiere17cf242017-06-19 11:05:51 -07002006 it.SkipStaticFields();
Andreas Gampe68df3202015-06-22 11:35:46 -07002007 while (it.HasNextInstanceField()) {
2008 if ((it.GetFieldAccessFlags() & kAccFinal) != 0) {
2009 return it.GetMemberIndex();
2010 }
2011 it.Next();
2012 }
Andreas Gampee2abbc62017-09-15 11:59:26 -07002013 return dex::kDexNoIndex;
Andreas Gampe68df3202015-06-22 11:35:46 -07002014}
2015
Andreas Gampea727e372015-08-25 09:22:37 -07002016// Setup a register line for the given return instruction.
2017static void AdjustReturnLine(MethodVerifier* verifier,
2018 const Instruction* ret_inst,
2019 RegisterLine* line) {
2020 Instruction::Code opcode = ret_inst->Opcode();
2021
2022 switch (opcode) {
2023 case Instruction::RETURN_VOID:
2024 case Instruction::RETURN_VOID_NO_BARRIER:
2025 SafelyMarkAllRegistersAsConflicts(verifier, line);
2026 break;
2027
2028 case Instruction::RETURN:
2029 case Instruction::RETURN_OBJECT:
2030 line->MarkAllRegistersAsConflictsExcept(verifier, ret_inst->VRegA_11x());
2031 break;
2032
2033 case Instruction::RETURN_WIDE:
2034 line->MarkAllRegistersAsConflictsExceptWide(verifier, ret_inst->VRegA_11x());
2035 break;
2036
2037 default:
2038 LOG(FATAL) << "Unknown return opcode " << opcode;
2039 UNREACHABLE();
2040 }
2041}
2042
Ian Rogers776ac1f2012-04-13 23:36:36 -07002043bool MethodVerifier::CodeFlowVerifyInstruction(uint32_t* start_guess) {
Elliott Hughes08fc03a2012-06-26 17:34:00 -07002044 // If we're doing FindLocksAtDexPc, check whether we're at the dex pc we care about.
2045 // We want the state _before_ the instruction, for the case where the dex pc we're
2046 // interested in is itself a monitor-enter instruction (which is a likely place
2047 // for a thread to be suspended).
Ian Rogers7b078e82014-09-10 14:44:24 -07002048 if (monitor_enter_dex_pcs_ != nullptr && work_insn_idx_ == interesting_dex_pc_) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -07002049 monitor_enter_dex_pcs_->clear(); // The new work line is more accurate than the previous one.
Andreas Gampeaaf0d382017-11-27 14:10:21 -08002050
2051 std::map<uint32_t, DexLockInfo> depth_to_lock_info;
2052 auto collector = [&](uint32_t dex_reg, uint32_t depth) {
2053 auto insert_pair = depth_to_lock_info.emplace(depth, DexLockInfo(depth));
2054 auto it = insert_pair.first;
2055 auto set_insert_pair = it->second.dex_registers.insert(dex_reg);
2056 DCHECK(set_insert_pair.second);
2057 };
2058 work_line_->IterateRegToLockDepths(collector);
2059 for (auto& pair : depth_to_lock_info) {
2060 monitor_enter_dex_pcs_->push_back(pair.second);
2061 // Map depth to dex PC.
2062 (*monitor_enter_dex_pcs_)[monitor_enter_dex_pcs_->size() - 1].dex_pc =
2063 work_line_->GetMonitorEnterDexPc(pair.second.dex_pc);
Elliott Hughes08fc03a2012-06-26 17:34:00 -07002064 }
2065 }
2066
jeffhaobdb76512011-09-07 11:43:16 -07002067 /*
2068 * Once we finish decoding the instruction, we need to figure out where
jeffhaod1f0fde2011-09-08 17:25:33 -07002069 * we can go from here. There are three possible ways to transfer
jeffhaobdb76512011-09-07 11:43:16 -07002070 * control to another statement:
2071 *
jeffhaod1f0fde2011-09-08 17:25:33 -07002072 * (1) Continue to the next instruction. Applies to all but
jeffhaobdb76512011-09-07 11:43:16 -07002073 * unconditional branches, method returns, and exception throws.
jeffhaod1f0fde2011-09-08 17:25:33 -07002074 * (2) Branch to one or more possible locations. Applies to branches
jeffhaobdb76512011-09-07 11:43:16 -07002075 * and switch statements.
jeffhaod1f0fde2011-09-08 17:25:33 -07002076 * (3) Exception handlers. Applies to any instruction that can
jeffhaobdb76512011-09-07 11:43:16 -07002077 * throw an exception that is handled by an encompassing "try"
2078 * block.
2079 *
2080 * We can also return, in which case there is no successor instruction
2081 * from this point.
2082 *
Elliott Hughesadb8c672012-03-06 16:49:32 -08002083 * The behavior can be determined from the opcode flags.
jeffhaobdb76512011-09-07 11:43:16 -07002084 */
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08002085 const uint16_t* insns = code_item_accessor_.Insns() + work_insn_idx_;
Ian Rogersd81871c2011-10-03 13:57:23 -07002086 const Instruction* inst = Instruction::At(insns);
Ian Rogersa75a0132012-09-28 11:41:42 -07002087 int opcode_flags = Instruction::FlagsOf(inst->Opcode());
jeffhaobdb76512011-09-07 11:43:16 -07002088
jeffhaobdb76512011-09-07 11:43:16 -07002089 int32_t branch_target = 0;
jeffhaobdb76512011-09-07 11:43:16 -07002090 bool just_set_result = false;
Andreas Gampe92d77202017-12-06 20:49:00 -08002091 if (UNLIKELY(VLOG_IS_ON(verifier_debug))) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002092 // Generate processing back trace to debug verifier
Elliott Hughesc073b072012-05-24 19:29:17 -07002093 LogVerifyInfo() << "Processing " << inst->DumpString(dex_file_) << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07002094 << work_line_->Dump(this) << "\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07002095 }
jeffhaobdb76512011-09-07 11:43:16 -07002096
2097 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002098 * Make a copy of the previous register state. If the instruction
jeffhaobdb76512011-09-07 11:43:16 -07002099 * can throw an exception, we will copy/merge this into the "catch"
2100 * address rather than work_line, because we don't want the result
2101 * from the "successful" code path (e.g. a check-cast that "improves"
2102 * a type) to be visible to the exception handler.
2103 */
Ian Rogers776ac1f2012-04-13 23:36:36 -07002104 if ((opcode_flags & Instruction::kThrow) != 0 && CurrentInsnFlags()->IsInTry()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002105 saved_line_->CopyFromLine(work_line_.get());
Ian Rogers1ff3c982014-08-12 02:30:58 -07002106 } else if (kIsDebugBuild) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002107 saved_line_->FillWithGarbage();
jeffhaobdb76512011-09-07 11:43:16 -07002108 }
Andreas Gamped12e7822015-06-25 10:26:40 -07002109 DCHECK(!have_pending_runtime_throw_failure_); // Per-instruction flag, should not be set here.
jeffhaobdb76512011-09-07 11:43:16 -07002110
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07002111
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002112 // We need to ensure the work line is consistent while performing validation. When we spot a
2113 // peephole pattern we compute a new line for either the fallthrough instruction or the
2114 // branch target.
Mathieu Chartier361e04a2016-02-16 14:06:35 -08002115 RegisterLineArenaUniquePtr branch_line;
2116 RegisterLineArenaUniquePtr fallthrough_line;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002117
Sebastien Hertz5243e912013-05-21 10:55:07 +02002118 switch (inst->Opcode()) {
jeffhaobdb76512011-09-07 11:43:16 -07002119 case Instruction::NOP:
2120 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002121 * A "pure" NOP has no effect on anything. Data tables start with
jeffhaobdb76512011-09-07 11:43:16 -07002122 * a signature that looks like a NOP; if we see one of these in
2123 * the course of executing code then we have a problem.
2124 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002125 if (inst->VRegA_10x() != 0) {
jeffhaod5347e02012-03-22 17:25:05 -07002126 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "encountered data table in instruction stream";
jeffhaobdb76512011-09-07 11:43:16 -07002127 }
2128 break;
2129
2130 case Instruction::MOVE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002131 work_line_->CopyRegister1(this, inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategory1nr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02002132 break;
jeffhaobdb76512011-09-07 11:43:16 -07002133 case Instruction::MOVE_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07002134 work_line_->CopyRegister1(this, inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategory1nr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02002135 break;
jeffhaobdb76512011-09-07 11:43:16 -07002136 case Instruction::MOVE_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07002137 work_line_->CopyRegister1(this, inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategory1nr);
jeffhaobdb76512011-09-07 11:43:16 -07002138 break;
2139 case Instruction::MOVE_WIDE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002140 work_line_->CopyRegister2(this, inst->VRegA_12x(), inst->VRegB_12x());
Sebastien Hertz5243e912013-05-21 10:55:07 +02002141 break;
jeffhaobdb76512011-09-07 11:43:16 -07002142 case Instruction::MOVE_WIDE_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07002143 work_line_->CopyRegister2(this, inst->VRegA_22x(), inst->VRegB_22x());
Sebastien Hertz5243e912013-05-21 10:55:07 +02002144 break;
jeffhaobdb76512011-09-07 11:43:16 -07002145 case Instruction::MOVE_WIDE_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07002146 work_line_->CopyRegister2(this, inst->VRegA_32x(), inst->VRegB_32x());
jeffhaobdb76512011-09-07 11:43:16 -07002147 break;
2148 case Instruction::MOVE_OBJECT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002149 work_line_->CopyRegister1(this, inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategoryRef);
Sebastien Hertz5243e912013-05-21 10:55:07 +02002150 break;
jeffhaobdb76512011-09-07 11:43:16 -07002151 case Instruction::MOVE_OBJECT_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07002152 work_line_->CopyRegister1(this, inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategoryRef);
Sebastien Hertz5243e912013-05-21 10:55:07 +02002153 break;
jeffhaobdb76512011-09-07 11:43:16 -07002154 case Instruction::MOVE_OBJECT_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07002155 work_line_->CopyRegister1(this, inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategoryRef);
jeffhaobdb76512011-09-07 11:43:16 -07002156 break;
2157
2158 /*
2159 * The move-result instructions copy data out of a "pseudo-register"
jeffhaod1f0fde2011-09-08 17:25:33 -07002160 * with the results from the last method invocation. In practice we
jeffhaobdb76512011-09-07 11:43:16 -07002161 * might want to hold the result in an actual CPU register, so the
2162 * Dalvik spec requires that these only appear immediately after an
2163 * invoke or filled-new-array.
2164 *
jeffhaod1f0fde2011-09-08 17:25:33 -07002165 * These calls invalidate the "result" register. (This is now
jeffhaobdb76512011-09-07 11:43:16 -07002166 * redundant with the reset done below, but it can make the debug info
2167 * easier to read in some cases.)
2168 */
2169 case Instruction::MOVE_RESULT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002170 work_line_->CopyResultRegister1(this, inst->VRegA_11x(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002171 break;
2172 case Instruction::MOVE_RESULT_WIDE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002173 work_line_->CopyResultRegister2(this, inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07002174 break;
2175 case Instruction::MOVE_RESULT_OBJECT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002176 work_line_->CopyResultRegister1(this, inst->VRegA_11x(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002177 break;
2178
Ian Rogersd81871c2011-10-03 13:57:23 -07002179 case Instruction::MOVE_EXCEPTION: {
Sebastien Hertz270a0e12015-01-16 19:49:09 +01002180 // We do not allow MOVE_EXCEPTION as the first instruction in a method. This is a simple case
2181 // where one entrypoint to the catch block is not actually an exception path.
2182 if (work_insn_idx_ == 0) {
2183 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "move-exception at pc 0x0";
2184 break;
2185 }
jeffhaobdb76512011-09-07 11:43:16 -07002186 /*
jeffhao60f83e32012-02-13 17:16:30 -08002187 * This statement can only appear as the first instruction in an exception handler. We verify
2188 * that as part of extracting the exception type from the catch block list.
jeffhaobdb76512011-09-07 11:43:16 -07002189 */
Ian Rogersd8f69b02014-09-10 21:43:52 +00002190 const RegType& res_type = GetCaughtExceptionType();
Andreas Gampead238ce2015-08-24 21:13:08 -07002191 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_11x(), res_type);
jeffhaobdb76512011-09-07 11:43:16 -07002192 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002193 }
jeffhaobdb76512011-09-07 11:43:16 -07002194 case Instruction::RETURN_VOID:
Andreas Gampef10b6e12015-08-12 10:48:12 -07002195 if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002196 if (!GetMethodReturnType().IsConflict()) {
jeffhaod5347e02012-03-22 17:25:05 -07002197 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07002198 }
jeffhaobdb76512011-09-07 11:43:16 -07002199 }
2200 break;
2201 case Instruction::RETURN:
Andreas Gampef10b6e12015-08-12 10:48:12 -07002202 if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) {
jeffhaobdb76512011-09-07 11:43:16 -07002203 /* check the method signature */
Ian Rogersd8f69b02014-09-10 21:43:52 +00002204 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07002205 if (!return_type.IsCategory1Types()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002206 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected non-category 1 return type "
2207 << return_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002208 } else {
2209 // Compilers may generate synthetic functions that write byte values into boolean fields.
2210 // Also, it may use integer values for boolean, byte, short, and character return types.
Sebastien Hertz5243e912013-05-21 10:55:07 +02002211 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07002212 const RegType& src_type = work_line_->GetRegisterType(this, vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07002213 bool use_src = ((return_type.IsBoolean() && src_type.IsByte()) ||
2214 ((return_type.IsBoolean() || return_type.IsByte() ||
2215 return_type.IsShort() || return_type.IsChar()) &&
2216 src_type.IsInteger()));
2217 /* check the register contents */
Ian Rogersad0b3a32012-04-16 14:50:24 -07002218 bool success =
Ian Rogers7b078e82014-09-10 14:44:24 -07002219 work_line_->VerifyRegisterType(this, vregA, use_src ? src_type : return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002220 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002221 AppendToLastFailMessage(StringPrintf(" return-1nr on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07002222 }
jeffhaobdb76512011-09-07 11:43:16 -07002223 }
2224 }
2225 break;
2226 case Instruction::RETURN_WIDE:
Andreas Gampef10b6e12015-08-12 10:48:12 -07002227 if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) {
jeffhaobdb76512011-09-07 11:43:16 -07002228 /* check the method signature */
Ian Rogersd8f69b02014-09-10 21:43:52 +00002229 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07002230 if (!return_type.IsCategory2Types()) {
jeffhaod5347e02012-03-22 17:25:05 -07002231 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-wide not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07002232 } else {
2233 /* check the register contents */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002234 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07002235 bool success = work_line_->VerifyRegisterType(this, vregA, return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002236 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002237 AppendToLastFailMessage(StringPrintf(" return-wide on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07002238 }
jeffhaobdb76512011-09-07 11:43:16 -07002239 }
2240 }
2241 break;
2242 case Instruction::RETURN_OBJECT:
Andreas Gampef10b6e12015-08-12 10:48:12 -07002243 if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) {
Ian Rogersd8f69b02014-09-10 21:43:52 +00002244 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07002245 if (!return_type.IsReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002246 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-object not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07002247 } else {
2248 /* return_type is the *expected* return type, not register value */
Andreas Gampeeed3a5d2017-11-29 14:58:34 -08002249 DCHECK(!return_type.IsZeroOrNull());
Ian Rogersd81871c2011-10-03 13:57:23 -07002250 DCHECK(!return_type.IsUninitializedReference());
Sebastien Hertz5243e912013-05-21 10:55:07 +02002251 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07002252 const RegType& reg_type = work_line_->GetRegisterType(this, vregA);
Andreas Gampea32210c2015-06-24 10:26:13 -07002253 // Disallow returning undefined, conflict & uninitialized values and verify that the
2254 // reference in vAA is an instance of the "return_type."
2255 if (reg_type.IsUndefined()) {
2256 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning undefined register";
2257 } else if (reg_type.IsConflict()) {
2258 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning register with conflict";
2259 } else if (reg_type.IsUninitializedTypes()) {
David Brazdil68b5c0b2016-01-19 14:25:29 +00002260 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning uninitialized object '"
Brian Carlstrom93c33962013-07-26 10:37:43 -07002261 << reg_type << "'";
Andreas Gampea4c98f22015-11-06 16:24:49 -08002262 } else if (!reg_type.IsReferenceTypes()) {
2263 // We really do expect a reference here.
2264 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-object returns a non-reference type "
2265 << reg_type;
David Brazdilca3c8c32016-09-06 14:04:48 +01002266 } else if (!return_type.IsAssignableFrom(reg_type, this)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07002267 if (reg_type.IsUnresolvedTypes() || return_type.IsUnresolvedTypes()) {
2268 Fail(VERIFY_ERROR_NO_CLASS) << " can't resolve returned type '" << return_type
2269 << "' or '" << reg_type << "'";
2270 } else {
Andreas Gampe16f149c2015-03-23 10:10:20 -07002271 bool soft_error = false;
2272 // Check whether arrays are involved. They will show a valid class status, even
2273 // if their components are erroneous.
2274 if (reg_type.IsArrayTypes() && return_type.IsArrayTypes()) {
David Brazdilca3c8c32016-09-06 14:04:48 +01002275 return_type.CanAssignArray(reg_type, reg_types_, class_loader_, this, &soft_error);
Andreas Gampe16f149c2015-03-23 10:10:20 -07002276 if (soft_error) {
2277 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "array with erroneous component type: "
2278 << reg_type << " vs " << return_type;
2279 }
2280 }
2281
2282 if (!soft_error) {
2283 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning '" << reg_type
2284 << "', but expected from declaration '" << return_type << "'";
2285 }
Jeff Haoa3faaf42013-09-03 19:07:00 -07002286 }
jeffhaobdb76512011-09-07 11:43:16 -07002287 }
2288 }
2289 }
2290 break;
2291
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07002292 /* could be boolean, int, float, or a null reference */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002293 case Instruction::CONST_4: {
2294 int32_t val = static_cast<int32_t>(inst->VRegB_11n() << 28) >> 28;
Andreas Gampead238ce2015-08-24 21:13:08 -07002295 work_line_->SetRegisterType<LockOp::kClear>(
2296 this, inst->VRegA_11n(), DetermineCat1Constant(val, need_precise_constants_));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07002297 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02002298 }
2299 case Instruction::CONST_16: {
2300 int16_t val = static_cast<int16_t>(inst->VRegB_21s());
Andreas Gampead238ce2015-08-24 21:13:08 -07002301 work_line_->SetRegisterType<LockOp::kClear>(
2302 this, inst->VRegA_21s(), DetermineCat1Constant(val, need_precise_constants_));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07002303 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02002304 }
Sebastien Hertz849600b2013-12-20 10:28:08 +01002305 case Instruction::CONST: {
2306 int32_t val = inst->VRegB_31i();
Andreas Gampead238ce2015-08-24 21:13:08 -07002307 work_line_->SetRegisterType<LockOp::kClear>(
2308 this, inst->VRegA_31i(), DetermineCat1Constant(val, need_precise_constants_));
jeffhaobdb76512011-09-07 11:43:16 -07002309 break;
Sebastien Hertz849600b2013-12-20 10:28:08 +01002310 }
2311 case Instruction::CONST_HIGH16: {
2312 int32_t val = static_cast<int32_t>(inst->VRegB_21h() << 16);
Andreas Gampead238ce2015-08-24 21:13:08 -07002313 work_line_->SetRegisterType<LockOp::kClear>(
2314 this, inst->VRegA_21h(), DetermineCat1Constant(val, need_precise_constants_));
jeffhaobdb76512011-09-07 11:43:16 -07002315 break;
Sebastien Hertz849600b2013-12-20 10:28:08 +01002316 }
jeffhaobdb76512011-09-07 11:43:16 -07002317 /* could be long or double; resolved upon use */
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002318 case Instruction::CONST_WIDE_16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002319 int64_t val = static_cast<int16_t>(inst->VRegB_21s());
Ian Rogersd8f69b02014-09-10 21:43:52 +00002320 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
2321 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07002322 work_line_->SetRegisterTypeWide(this, inst->VRegA_21s(), lo, hi);
jeffhaobdb76512011-09-07 11:43:16 -07002323 break;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002324 }
2325 case Instruction::CONST_WIDE_32: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002326 int64_t val = static_cast<int32_t>(inst->VRegB_31i());
Ian Rogersd8f69b02014-09-10 21:43:52 +00002327 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
2328 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07002329 work_line_->SetRegisterTypeWide(this, inst->VRegA_31i(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002330 break;
2331 }
2332 case Instruction::CONST_WIDE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002333 int64_t val = inst->VRegB_51l();
Ian Rogersd8f69b02014-09-10 21:43:52 +00002334 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
2335 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07002336 work_line_->SetRegisterTypeWide(this, inst->VRegA_51l(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002337 break;
2338 }
2339 case Instruction::CONST_WIDE_HIGH16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002340 int64_t val = static_cast<uint64_t>(inst->VRegB_21h()) << 48;
Ian Rogersd8f69b02014-09-10 21:43:52 +00002341 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
2342 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07002343 work_line_->SetRegisterTypeWide(this, inst->VRegA_21h(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002344 break;
2345 }
jeffhaobdb76512011-09-07 11:43:16 -07002346 case Instruction::CONST_STRING:
Andreas Gampead238ce2015-08-24 21:13:08 -07002347 work_line_->SetRegisterType<LockOp::kClear>(
2348 this, inst->VRegA_21c(), reg_types_.JavaLangString());
Sebastien Hertz5243e912013-05-21 10:55:07 +02002349 break;
jeffhaobdb76512011-09-07 11:43:16 -07002350 case Instruction::CONST_STRING_JUMBO:
Andreas Gampead238ce2015-08-24 21:13:08 -07002351 work_line_->SetRegisterType<LockOp::kClear>(
2352 this, inst->VRegA_31c(), reg_types_.JavaLangString());
jeffhaobdb76512011-09-07 11:43:16 -07002353 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002354 case Instruction::CONST_CLASS: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002355 // Get type from instruction if unresolved then we need an access check
2356 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Andreas Gampe98be1a92017-08-28 08:25:45 -07002357 const RegType& res_type = ResolveClass<CheckAccess::kYes>(dex::TypeIndex(inst->VRegB_21c()));
Ian Rogersad0b3a32012-04-16 14:50:24 -07002358 // Register holds class, ie its type is class, on error it will hold Conflict.
Andreas Gampead238ce2015-08-24 21:13:08 -07002359 work_line_->SetRegisterType<LockOp::kClear>(
2360 this, inst->VRegA_21c(), res_type.IsConflict() ? res_type
2361 : reg_types_.JavaLangClass());
jeffhaobdb76512011-09-07 11:43:16 -07002362 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002363 }
Orion Hodson2e599942017-09-22 16:17:41 +01002364 case Instruction::CONST_METHOD_HANDLE:
2365 work_line_->SetRegisterType<LockOp::kClear>(
2366 this, inst->VRegA_21c(), reg_types_.JavaLangInvokeMethodHandle());
2367 // TODO: add compiler support for const-method-{handle,type} (b/66890674)
2368 Fail(VERIFY_ERROR_FORCE_INTERPRETER);
2369 break;
2370 case Instruction::CONST_METHOD_TYPE:
2371 work_line_->SetRegisterType<LockOp::kClear>(
2372 this, inst->VRegA_21c(), reg_types_.JavaLangInvokeMethodType());
2373 // TODO: add compiler support for const-method-{handle,type} (b/66890674)
2374 Fail(VERIFY_ERROR_FORCE_INTERPRETER);
2375 break;
jeffhaobdb76512011-09-07 11:43:16 -07002376 case Instruction::MONITOR_ENTER:
Ian Rogers7b078e82014-09-10 14:44:24 -07002377 work_line_->PushMonitor(this, inst->VRegA_11x(), work_insn_idx_);
Andreas Gampec1474102015-08-18 08:57:44 -07002378 // Check whether the previous instruction is a move-object with vAA as a source, creating
2379 // untracked lock aliasing.
Mathieu Chartierde40d472015-10-15 17:47:48 -07002380 if (0 != work_insn_idx_ && !GetInstructionFlags(work_insn_idx_).IsBranchTarget()) {
Andreas Gampec1474102015-08-18 08:57:44 -07002381 uint32_t prev_idx = work_insn_idx_ - 1;
Mathieu Chartierde40d472015-10-15 17:47:48 -07002382 while (0 != prev_idx && !GetInstructionFlags(prev_idx).IsOpcode()) {
Andreas Gampec1474102015-08-18 08:57:44 -07002383 prev_idx--;
2384 }
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08002385 const Instruction& prev_inst = code_item_accessor_.InstructionAt(prev_idx);
Mathieu Chartier1d2d4ff2017-09-23 16:11:06 -07002386 switch (prev_inst.Opcode()) {
Andreas Gampec1474102015-08-18 08:57:44 -07002387 case Instruction::MOVE_OBJECT:
2388 case Instruction::MOVE_OBJECT_16:
2389 case Instruction::MOVE_OBJECT_FROM16:
Mathieu Chartier1d2d4ff2017-09-23 16:11:06 -07002390 if (prev_inst.VRegB() == inst->VRegA_11x()) {
Andreas Gampec1474102015-08-18 08:57:44 -07002391 // Redo the copy. This won't change the register types, but update the lock status
2392 // for the aliased register.
2393 work_line_->CopyRegister1(this,
Mathieu Chartier1d2d4ff2017-09-23 16:11:06 -07002394 prev_inst.VRegA(),
2395 prev_inst.VRegB(),
Andreas Gampec1474102015-08-18 08:57:44 -07002396 kTypeCategoryRef);
2397 }
2398 break;
2399
2400 default: // Other instruction types ignored.
2401 break;
2402 }
2403 }
jeffhaobdb76512011-09-07 11:43:16 -07002404 break;
2405 case Instruction::MONITOR_EXIT:
2406 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002407 * monitor-exit instructions are odd. They can throw exceptions,
jeffhaobdb76512011-09-07 11:43:16 -07002408 * but when they do they act as if they succeeded and the PC is
jeffhaod1f0fde2011-09-08 17:25:33 -07002409 * pointing to the following instruction. (This behavior goes back
jeffhaobdb76512011-09-07 11:43:16 -07002410 * to the need to handle asynchronous exceptions, a now-deprecated
2411 * feature that Dalvik doesn't support.)
2412 *
jeffhaod1f0fde2011-09-08 17:25:33 -07002413 * In practice we don't need to worry about this. The only
jeffhaobdb76512011-09-07 11:43:16 -07002414 * exceptions that can be thrown from monitor-exit are for a
jeffhaod1f0fde2011-09-08 17:25:33 -07002415 * null reference and -exit without a matching -enter. If the
jeffhaobdb76512011-09-07 11:43:16 -07002416 * structured locking checks are working, the former would have
2417 * failed on the -enter instruction, and the latter is impossible.
2418 *
2419 * This is fortunate, because issue 3221411 prevents us from
2420 * chasing the "can throw" path when monitor verification is
jeffhaod1f0fde2011-09-08 17:25:33 -07002421 * enabled. If we can fully verify the locking we can ignore
jeffhaobdb76512011-09-07 11:43:16 -07002422 * some catch blocks (which will show up as "dead" code when
2423 * we skip them here); if we can't, then the code path could be
2424 * "live" so we still need to check it.
2425 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002426 opcode_flags &= ~Instruction::kThrow;
Ian Rogers7b078e82014-09-10 14:44:24 -07002427 work_line_->PopMonitor(this, inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07002428 break;
Ian Rogers28ad40d2011-10-27 15:19:26 -07002429 case Instruction::CHECK_CAST:
Ian Rogersd81871c2011-10-03 13:57:23 -07002430 case Instruction::INSTANCE_OF: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002431 /*
2432 * If this instruction succeeds, we will "downcast" register vA to the type in vB. (This
2433 * could be a "upcast" -- not expected, so we don't try to address it.)
2434 *
2435 * If it fails, an exception is thrown, which we deal with later by ignoring the update to
Elliott Hughesadb8c672012-03-06 16:49:32 -08002436 * dec_insn.vA when branching to a handler.
Ian Rogers28ad40d2011-10-27 15:19:26 -07002437 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002438 const bool is_checkcast = (inst->Opcode() == Instruction::CHECK_CAST);
Andreas Gampea5b09a62016-11-17 15:21:22 -08002439 const dex::TypeIndex type_idx((is_checkcast) ? inst->VRegB_21c() : inst->VRegC_22c());
Andreas Gampe98be1a92017-08-28 08:25:45 -07002440 const RegType& res_type = ResolveClass<CheckAccess::kYes>(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002441 if (res_type.IsConflict()) {
Andreas Gampe00633eb2014-07-17 16:13:35 -07002442 // If this is a primitive type, fail HARD.
Vladimir Marko666ee3d2017-12-11 18:37:36 +00002443 ObjPtr<mirror::Class> klass = Runtime::Current()->GetClassLinker()->LookupResolvedType(
2444 type_idx, dex_cache_.Get(), class_loader_.Get());
Andreas Gampe00633eb2014-07-17 16:13:35 -07002445 if (klass != nullptr && klass->IsPrimitive()) {
2446 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "using primitive type "
2447 << dex_file_->StringByTypeIdx(type_idx) << " in instanceof in "
2448 << GetDeclaringClass();
2449 break;
2450 }
2451
Ian Rogersad0b3a32012-04-16 14:50:24 -07002452 DCHECK_NE(failures_.size(), 0U);
2453 if (!is_checkcast) {
Andreas Gampead238ce2015-08-24 21:13:08 -07002454 work_line_->SetRegisterType<LockOp::kClear>(this,
2455 inst->VRegA_22c(),
2456 reg_types_.Boolean());
Ian Rogersad0b3a32012-04-16 14:50:24 -07002457 }
2458 break; // bad class
Ian Rogers9f1ab122011-12-12 08:52:43 -08002459 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002460 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Sebastien Hertz5243e912013-05-21 10:55:07 +02002461 uint32_t orig_type_reg = (is_checkcast) ? inst->VRegA_21c() : inst->VRegB_22c();
Ian Rogers7b078e82014-09-10 14:44:24 -07002462 const RegType& orig_type = work_line_->GetRegisterType(this, orig_type_reg);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002463 if (!res_type.IsNonZeroReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002464 if (is_checkcast) {
2465 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on unexpected class " << res_type;
2466 } else {
2467 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on unexpected class " << res_type;
2468 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002469 } else if (!orig_type.IsReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002470 if (is_checkcast) {
2471 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on non-reference in v" << orig_type_reg;
2472 } else {
2473 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on non-reference in v" << orig_type_reg;
2474 }
David Brazdil68b5c0b2016-01-19 14:25:29 +00002475 } else if (orig_type.IsUninitializedTypes()) {
2476 if (is_checkcast) {
2477 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on uninitialized reference in v"
2478 << orig_type_reg;
2479 } else {
2480 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on uninitialized reference in v"
2481 << orig_type_reg;
2482 }
jeffhao2a8a90e2011-09-26 14:25:31 -07002483 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002484 if (is_checkcast) {
Andreas Gampead238ce2015-08-24 21:13:08 -07002485 work_line_->SetRegisterType<LockOp::kKeep>(this, inst->VRegA_21c(), res_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07002486 } else {
Andreas Gampead238ce2015-08-24 21:13:08 -07002487 work_line_->SetRegisterType<LockOp::kClear>(this,
2488 inst->VRegA_22c(),
2489 reg_types_.Boolean());
jeffhaobdb76512011-09-07 11:43:16 -07002490 }
jeffhaobdb76512011-09-07 11:43:16 -07002491 }
jeffhao2a8a90e2011-09-26 14:25:31 -07002492 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002493 }
2494 case Instruction::ARRAY_LENGTH: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002495 const RegType& res_type = work_line_->GetRegisterType(this, inst->VRegB_12x());
Ian Rogers28ad40d2011-10-27 15:19:26 -07002496 if (res_type.IsReferenceTypes()) {
Andreas Gampeeed3a5d2017-11-29 14:58:34 -08002497 if (!res_type.IsArrayTypes() && !res_type.IsZeroOrNull()) {
2498 // ie not an array or null
jeffhaod5347e02012-03-22 17:25:05 -07002499 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002500 } else {
Andreas Gampead238ce2015-08-24 21:13:08 -07002501 work_line_->SetRegisterType<LockOp::kClear>(this,
2502 inst->VRegA_12x(),
2503 reg_types_.Integer());
Ian Rogersd81871c2011-10-03 13:57:23 -07002504 }
Andreas Gampe65c9db82014-07-28 13:14:34 -07002505 } else {
2506 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002507 }
2508 break;
2509 }
2510 case Instruction::NEW_INSTANCE: {
Andreas Gampe98be1a92017-08-28 08:25:45 -07002511 const RegType& res_type = ResolveClass<CheckAccess::kYes>(dex::TypeIndex(inst->VRegB_21c()));
Ian Rogersad0b3a32012-04-16 14:50:24 -07002512 if (res_type.IsConflict()) {
2513 DCHECK_NE(failures_.size(), 0U);
2514 break; // bad class
jeffhao8cd6dda2012-02-22 10:15:34 -08002515 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002516 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
2517 // can't create an instance of an interface or abstract class */
2518 if (!res_type.IsInstantiableTypes()) {
2519 Fail(VERIFY_ERROR_INSTANTIATION)
2520 << "new-instance on primitive, interface or abstract class" << res_type;
Ian Rogers08f753d2012-08-24 14:35:25 -07002521 // Soft failure so carry on to set register type.
Ian Rogersd81871c2011-10-03 13:57:23 -07002522 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00002523 const RegType& uninit_type = reg_types_.Uninitialized(res_type, work_insn_idx_);
Ian Rogers08f753d2012-08-24 14:35:25 -07002524 // Any registers holding previous allocations from this address that have not yet been
2525 // initialized must be marked invalid.
Ian Rogers7b078e82014-09-10 14:44:24 -07002526 work_line_->MarkUninitRefsAsInvalid(this, uninit_type);
Ian Rogers08f753d2012-08-24 14:35:25 -07002527 // add the new uninitialized reference to the register state
Andreas Gampead238ce2015-08-24 21:13:08 -07002528 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_21c(), uninit_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07002529 break;
2530 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08002531 case Instruction::NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002532 VerifyNewArray(inst, false, false);
jeffhaobdb76512011-09-07 11:43:16 -07002533 break;
2534 case Instruction::FILLED_NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002535 VerifyNewArray(inst, true, false);
Ian Rogers0c4a5062012-02-03 15:18:59 -08002536 just_set_result = true; // Filled new array sets result register
jeffhaobdb76512011-09-07 11:43:16 -07002537 break;
Ian Rogers0c4a5062012-02-03 15:18:59 -08002538 case Instruction::FILLED_NEW_ARRAY_RANGE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002539 VerifyNewArray(inst, true, true);
Ian Rogers0c4a5062012-02-03 15:18:59 -08002540 just_set_result = true; // Filled new array range sets result register
2541 break;
jeffhaobdb76512011-09-07 11:43:16 -07002542 case Instruction::CMPL_FLOAT:
2543 case Instruction::CMPG_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002544 if (!work_line_->VerifyRegisterType(this, inst->VRegB_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08002545 break;
2546 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002547 if (!work_line_->VerifyRegisterType(this, inst->VRegC_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08002548 break;
2549 }
Andreas Gampead238ce2015-08-24 21:13:08 -07002550 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002551 break;
2552 case Instruction::CMPL_DOUBLE:
2553 case Instruction::CMPG_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002554 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegB_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002555 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002556 break;
2557 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002558 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegC_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002559 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002560 break;
2561 }
Andreas Gampead238ce2015-08-24 21:13:08 -07002562 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002563 break;
2564 case Instruction::CMP_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002565 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegB_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002566 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002567 break;
2568 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002569 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegC_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002570 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002571 break;
2572 }
Andreas Gampead238ce2015-08-24 21:13:08 -07002573 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002574 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002575 case Instruction::THROW: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002576 const RegType& res_type = work_line_->GetRegisterType(this, inst->VRegA_11x());
David Brazdilca3c8c32016-09-06 14:04:48 +01002577 if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(res_type, this)) {
David Brazdil68b5c0b2016-01-19 14:25:29 +00002578 if (res_type.IsUninitializedTypes()) {
2579 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "thrown exception not initialized";
Pavel Vyssotski980027c2016-02-11 20:28:11 +06002580 } else if (!res_type.IsReferenceTypes()) {
2581 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "thrown value of non-reference type " << res_type;
David Brazdil68b5c0b2016-01-19 14:25:29 +00002582 } else {
2583 Fail(res_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS : VERIFY_ERROR_BAD_CLASS_SOFT)
2584 << "thrown class " << res_type << " not instanceof Throwable";
2585 }
jeffhaobdb76512011-09-07 11:43:16 -07002586 }
2587 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002588 }
jeffhaobdb76512011-09-07 11:43:16 -07002589 case Instruction::GOTO:
2590 case Instruction::GOTO_16:
2591 case Instruction::GOTO_32:
2592 /* no effect on or use of registers */
2593 break;
2594
2595 case Instruction::PACKED_SWITCH:
2596 case Instruction::SPARSE_SWITCH:
2597 /* verify that vAA is an integer, or can be converted to one */
Ian Rogers7b078e82014-09-10 14:44:24 -07002598 work_line_->VerifyRegisterType(this, inst->VRegA_31t(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002599 break;
2600
Ian Rogersd81871c2011-10-03 13:57:23 -07002601 case Instruction::FILL_ARRAY_DATA: {
2602 /* Similar to the verification done for APUT */
Ian Rogers7b078e82014-09-10 14:44:24 -07002603 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegA_31t());
Ian Rogers89310de2012-02-01 13:47:30 -08002604 /* array_type can be null if the reg type is Zero */
Andreas Gampeeed3a5d2017-11-29 14:58:34 -08002605 if (!array_type.IsZeroOrNull()) {
jeffhao457cc512012-02-02 16:55:13 -08002606 if (!array_type.IsArrayTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002607 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with array type "
2608 << array_type;
Andreas Gampebb18a032016-03-22 20:34:25 -07002609 } else if (array_type.IsUnresolvedTypes()) {
2610 // If it's an unresolved array type, it must be non-primitive.
2611 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data for array of type "
2612 << array_type;
Ian Rogers89310de2012-02-01 13:47:30 -08002613 } else {
Ian Rogersd8f69b02014-09-10 21:43:52 +00002614 const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
Ian Rogersad0b3a32012-04-16 14:50:24 -07002615 DCHECK(!component_type.IsConflict());
jeffhao457cc512012-02-02 16:55:13 -08002616 if (component_type.IsNonZeroReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002617 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with component type "
2618 << component_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002619 } else {
jeffhao457cc512012-02-02 16:55:13 -08002620 // Now verify if the element width in the table matches the element width declared in
2621 // the array
Andreas Gampe53de99c2015-08-17 13:43:55 -07002622 const uint16_t* array_data =
2623 insns + (insns[1] | (static_cast<int32_t>(insns[2]) << 16));
jeffhao457cc512012-02-02 16:55:13 -08002624 if (array_data[0] != Instruction::kArrayDataSignature) {
jeffhaod5347e02012-03-22 17:25:05 -07002625 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid magic for array-data";
jeffhao457cc512012-02-02 16:55:13 -08002626 } else {
2627 size_t elem_width = Primitive::ComponentSize(component_type.GetPrimitiveType());
2628 // Since we don't compress the data in Dex, expect to see equal width of data stored
2629 // in the table and expected from the array class.
2630 if (array_data[1] != elem_width) {
jeffhaod5347e02012-03-22 17:25:05 -07002631 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-data size mismatch (" << array_data[1]
2632 << " vs " << elem_width << ")";
jeffhao457cc512012-02-02 16:55:13 -08002633 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002634 }
2635 }
jeffhaobdb76512011-09-07 11:43:16 -07002636 }
2637 }
2638 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002639 }
jeffhaobdb76512011-09-07 11:43:16 -07002640 case Instruction::IF_EQ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002641 case Instruction::IF_NE: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002642 const RegType& reg_type1 = work_line_->GetRegisterType(this, inst->VRegA_22t());
2643 const RegType& reg_type2 = work_line_->GetRegisterType(this, inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002644 bool mismatch = false;
Andreas Gampeeed3a5d2017-11-29 14:58:34 -08002645 if (reg_type1.IsZeroOrNull()) { // zero then integral or reference expected
Ian Rogersd81871c2011-10-03 13:57:23 -07002646 mismatch = !reg_type2.IsReferenceTypes() && !reg_type2.IsIntegralTypes();
2647 } else if (reg_type1.IsReferenceTypes()) { // both references?
2648 mismatch = !reg_type2.IsReferenceTypes();
2649 } else { // both integral?
2650 mismatch = !reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes();
2651 }
2652 if (mismatch) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002653 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to if-eq/if-ne (" << reg_type1 << ","
2654 << reg_type2 << ") must both be references or integral";
jeffhaobdb76512011-09-07 11:43:16 -07002655 }
2656 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002657 }
jeffhaobdb76512011-09-07 11:43:16 -07002658 case Instruction::IF_LT:
2659 case Instruction::IF_GE:
2660 case Instruction::IF_GT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002661 case Instruction::IF_LE: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002662 const RegType& reg_type1 = work_line_->GetRegisterType(this, inst->VRegA_22t());
2663 const RegType& reg_type2 = work_line_->GetRegisterType(this, inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002664 if (!reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002665 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to 'if' (" << reg_type1 << ","
2666 << reg_type2 << ") must be integral";
jeffhaobdb76512011-09-07 11:43:16 -07002667 }
2668 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002669 }
jeffhaobdb76512011-09-07 11:43:16 -07002670 case Instruction::IF_EQZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002671 case Instruction::IF_NEZ: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002672 const RegType& reg_type = work_line_->GetRegisterType(this, inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002673 if (!reg_type.IsReferenceTypes() && !reg_type.IsIntegralTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002674 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
2675 << " unexpected as arg to if-eqz/if-nez";
Ian Rogersd81871c2011-10-03 13:57:23 -07002676 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002677
2678 // Find previous instruction - its existence is a precondition to peephole optimization.
Ian Rogers9b360392013-06-06 14:45:07 -07002679 uint32_t instance_of_idx = 0;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002680 if (0 != work_insn_idx_) {
Ian Rogers9b360392013-06-06 14:45:07 -07002681 instance_of_idx = work_insn_idx_ - 1;
Mathieu Chartierde40d472015-10-15 17:47:48 -07002682 while (0 != instance_of_idx && !GetInstructionFlags(instance_of_idx).IsOpcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07002683 instance_of_idx--;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002684 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07002685 if (FailOrAbort(this, GetInstructionFlags(instance_of_idx).IsOpcode(),
Andreas Gampe7c038102014-10-27 20:08:46 -07002686 "Unable to get previous instruction of if-eqz/if-nez for work index ",
2687 work_insn_idx_)) {
2688 break;
2689 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002690 } else {
2691 break;
2692 }
2693
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08002694 const Instruction& instance_of_inst = code_item_accessor_.InstructionAt(instance_of_idx);
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002695
2696 /* Check for peep-hole pattern of:
2697 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002698 * instance-of vX, vY, T;
2699 * ifXXX vX, label ;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002700 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002701 * label:
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002702 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002703 * and sharpen the type of vY to be type T.
2704 * Note, this pattern can't be if:
2705 * - if there are other branches to this branch,
2706 * - when vX == vY.
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002707 */
Ian Rogersfae370a2013-06-05 08:33:27 -07002708 if (!CurrentInsnFlags()->IsBranchTarget() &&
Vladimir Markod7559b72017-09-28 13:50:37 +01002709 (Instruction::INSTANCE_OF == instance_of_inst.Opcode()) &&
2710 (inst->VRegA_21t() == instance_of_inst.VRegA_22c()) &&
2711 (instance_of_inst.VRegA_22c() != instance_of_inst.VRegB_22c())) {
Ian Rogersebbdd872014-07-07 23:53:08 -07002712 // Check the type of the instance-of is different than that of registers type, as if they
2713 // are the same there is no work to be done here. Check that the conversion is not to or
2714 // from an unresolved type as type information is imprecise. If the instance-of is to an
2715 // interface then ignore the type information as interfaces can only be treated as Objects
2716 // and we don't want to disallow field and other operations on the object. If the value
2717 // being instance-of checked against is known null (zero) then allow the optimization as
2718 // we didn't have type information. If the merge of the instance-of type with the original
2719 // type is assignable to the original then allow optimization. This check is performed to
2720 // ensure that subsequent merges don't lose type information - such as becoming an
2721 // interface from a class that would lose information relevant to field checks.
Vladimir Markod7559b72017-09-28 13:50:37 +01002722 const RegType& orig_type = work_line_->GetRegisterType(this, instance_of_inst.VRegB_22c());
Andreas Gampe98be1a92017-08-28 08:25:45 -07002723 const RegType& cast_type = ResolveClass<CheckAccess::kYes>(
Vladimir Markod7559b72017-09-28 13:50:37 +01002724 dex::TypeIndex(instance_of_inst.VRegC_22c()));
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002725
Ian Rogersebbdd872014-07-07 23:53:08 -07002726 if (!orig_type.Equals(cast_type) &&
2727 !cast_type.IsUnresolvedTypes() && !orig_type.IsUnresolvedTypes() &&
Andreas Gampe00633eb2014-07-17 16:13:35 -07002728 cast_type.HasClass() && // Could be conflict type, make sure it has a class.
Ian Rogersebbdd872014-07-07 23:53:08 -07002729 !cast_type.GetClass()->IsInterface() &&
Andreas Gampeeed3a5d2017-11-29 14:58:34 -08002730 (orig_type.IsZeroOrNull() ||
David Brazdilca3c8c32016-09-06 14:04:48 +01002731 orig_type.IsStrictlyAssignableFrom(
2732 cast_type.Merge(orig_type, &reg_types_, this), this))) {
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08002733 RegisterLine* update_line = RegisterLine::Create(code_item_accessor_.RegistersSize(),
2734 this);
Ian Rogersfae370a2013-06-05 08:33:27 -07002735 if (inst->Opcode() == Instruction::IF_EQZ) {
Ian Rogers9b360392013-06-06 14:45:07 -07002736 fallthrough_line.reset(update_line);
Ian Rogersfae370a2013-06-05 08:33:27 -07002737 } else {
Ian Rogers9b360392013-06-06 14:45:07 -07002738 branch_line.reset(update_line);
2739 }
2740 update_line->CopyFromLine(work_line_.get());
Andreas Gampead238ce2015-08-24 21:13:08 -07002741 update_line->SetRegisterType<LockOp::kKeep>(this,
Vladimir Markod7559b72017-09-28 13:50:37 +01002742 instance_of_inst.VRegB_22c(),
Andreas Gampead238ce2015-08-24 21:13:08 -07002743 cast_type);
Mathieu Chartierde40d472015-10-15 17:47:48 -07002744 if (!GetInstructionFlags(instance_of_idx).IsBranchTarget() && 0 != instance_of_idx) {
Ian Rogers9b360392013-06-06 14:45:07 -07002745 // See if instance-of was preceded by a move-object operation, common due to the small
2746 // register encoding space of instance-of, and propagate type information to the source
2747 // of the move-object.
2748 uint32_t move_idx = instance_of_idx - 1;
Mathieu Chartierde40d472015-10-15 17:47:48 -07002749 while (0 != move_idx && !GetInstructionFlags(move_idx).IsOpcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07002750 move_idx--;
2751 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07002752 if (FailOrAbort(this, GetInstructionFlags(move_idx).IsOpcode(),
Andreas Gampe7c038102014-10-27 20:08:46 -07002753 "Unable to get previous instruction of if-eqz/if-nez for work index ",
2754 work_insn_idx_)) {
2755 break;
2756 }
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08002757 const Instruction& move_inst = code_item_accessor_.InstructionAt(move_idx);
Vladimir Markod7559b72017-09-28 13:50:37 +01002758 switch (move_inst.Opcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07002759 case Instruction::MOVE_OBJECT:
Vladimir Markod7559b72017-09-28 13:50:37 +01002760 if (move_inst.VRegA_12x() == instance_of_inst.VRegB_22c()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07002761 update_line->SetRegisterType<LockOp::kKeep>(this,
Vladimir Markod7559b72017-09-28 13:50:37 +01002762 move_inst.VRegB_12x(),
Andreas Gampead238ce2015-08-24 21:13:08 -07002763 cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002764 }
2765 break;
2766 case Instruction::MOVE_OBJECT_FROM16:
Vladimir Markod7559b72017-09-28 13:50:37 +01002767 if (move_inst.VRegA_22x() == instance_of_inst.VRegB_22c()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07002768 update_line->SetRegisterType<LockOp::kKeep>(this,
Vladimir Markod7559b72017-09-28 13:50:37 +01002769 move_inst.VRegB_22x(),
Andreas Gampead238ce2015-08-24 21:13:08 -07002770 cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002771 }
2772 break;
2773 case Instruction::MOVE_OBJECT_16:
Vladimir Markod7559b72017-09-28 13:50:37 +01002774 if (move_inst.VRegA_32x() == instance_of_inst.VRegB_22c()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07002775 update_line->SetRegisterType<LockOp::kKeep>(this,
Vladimir Markod7559b72017-09-28 13:50:37 +01002776 move_inst.VRegB_32x(),
Andreas Gampead238ce2015-08-24 21:13:08 -07002777 cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002778 }
2779 break;
2780 default:
2781 break;
2782 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002783 }
2784 }
2785 }
2786
jeffhaobdb76512011-09-07 11:43:16 -07002787 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002788 }
jeffhaobdb76512011-09-07 11:43:16 -07002789 case Instruction::IF_LTZ:
2790 case Instruction::IF_GEZ:
2791 case Instruction::IF_GTZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002792 case Instruction::IF_LEZ: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002793 const RegType& reg_type = work_line_->GetRegisterType(this, inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002794 if (!reg_type.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002795 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
2796 << " unexpected as arg to if-ltz/if-gez/if-gtz/if-lez";
Ian Rogersd81871c2011-10-03 13:57:23 -07002797 }
jeffhaobdb76512011-09-07 11:43:16 -07002798 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002799 }
jeffhaobdb76512011-09-07 11:43:16 -07002800 case Instruction::AGET_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002801 VerifyAGet(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002802 break;
jeffhaobdb76512011-09-07 11:43:16 -07002803 case Instruction::AGET_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002804 VerifyAGet(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002805 break;
jeffhaobdb76512011-09-07 11:43:16 -07002806 case Instruction::AGET_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002807 VerifyAGet(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002808 break;
jeffhaobdb76512011-09-07 11:43:16 -07002809 case Instruction::AGET_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002810 VerifyAGet(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002811 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002812 case Instruction::AGET:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002813 VerifyAGet(inst, reg_types_.Integer(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002814 break;
jeffhaobdb76512011-09-07 11:43:16 -07002815 case Instruction::AGET_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002816 VerifyAGet(inst, reg_types_.LongLo(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002817 break;
2818 case Instruction::AGET_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002819 VerifyAGet(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07002820 break;
2821
Ian Rogersd81871c2011-10-03 13:57:23 -07002822 case Instruction::APUT_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002823 VerifyAPut(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002824 break;
2825 case Instruction::APUT_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002826 VerifyAPut(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002827 break;
2828 case Instruction::APUT_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002829 VerifyAPut(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002830 break;
2831 case Instruction::APUT_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002832 VerifyAPut(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002833 break;
2834 case Instruction::APUT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002835 VerifyAPut(inst, reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002836 break;
2837 case Instruction::APUT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002838 VerifyAPut(inst, reg_types_.LongLo(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002839 break;
2840 case Instruction::APUT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002841 VerifyAPut(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07002842 break;
2843
jeffhaobdb76512011-09-07 11:43:16 -07002844 case Instruction::IGET_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002845 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002846 break;
jeffhaobdb76512011-09-07 11:43:16 -07002847 case Instruction::IGET_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002848 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002849 break;
jeffhaobdb76512011-09-07 11:43:16 -07002850 case Instruction::IGET_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002851 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002852 break;
jeffhaobdb76512011-09-07 11:43:16 -07002853 case Instruction::IGET_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002854 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002855 break;
2856 case Instruction::IGET:
Andreas Gampe896df402014-10-20 22:25:29 -07002857 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002858 break;
2859 case Instruction::IGET_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002860 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002861 break;
2862 case Instruction::IGET_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002863 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false,
2864 false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002865 break;
jeffhaobdb76512011-09-07 11:43:16 -07002866
Ian Rogersd81871c2011-10-03 13:57:23 -07002867 case Instruction::IPUT_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002868 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002869 break;
2870 case Instruction::IPUT_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002871 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002872 break;
2873 case Instruction::IPUT_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002874 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002875 break;
2876 case Instruction::IPUT_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002877 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002878 break;
2879 case Instruction::IPUT:
Andreas Gampe896df402014-10-20 22:25:29 -07002880 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002881 break;
2882 case Instruction::IPUT_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002883 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002884 break;
jeffhaobdb76512011-09-07 11:43:16 -07002885 case Instruction::IPUT_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002886 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false,
2887 false);
jeffhaobdb76512011-09-07 11:43:16 -07002888 break;
2889
jeffhaobdb76512011-09-07 11:43:16 -07002890 case Instruction::SGET_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002891 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002892 break;
jeffhaobdb76512011-09-07 11:43:16 -07002893 case Instruction::SGET_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002894 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002895 break;
jeffhaobdb76512011-09-07 11:43:16 -07002896 case Instruction::SGET_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002897 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002898 break;
jeffhaobdb76512011-09-07 11:43:16 -07002899 case Instruction::SGET_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002900 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002901 break;
2902 case Instruction::SGET:
Andreas Gampe896df402014-10-20 22:25:29 -07002903 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002904 break;
2905 case Instruction::SGET_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002906 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002907 break;
2908 case Instruction::SGET_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002909 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false,
2910 true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002911 break;
2912
2913 case Instruction::SPUT_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002914 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002915 break;
2916 case Instruction::SPUT_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002917 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002918 break;
2919 case Instruction::SPUT_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002920 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002921 break;
2922 case Instruction::SPUT_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002923 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002924 break;
2925 case Instruction::SPUT:
Andreas Gampe896df402014-10-20 22:25:29 -07002926 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002927 break;
2928 case Instruction::SPUT_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002929 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002930 break;
2931 case Instruction::SPUT_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002932 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false,
2933 true);
jeffhaobdb76512011-09-07 11:43:16 -07002934 break;
2935
2936 case Instruction::INVOKE_VIRTUAL:
2937 case Instruction::INVOKE_VIRTUAL_RANGE:
2938 case Instruction::INVOKE_SUPER:
Ian Rogersd81871c2011-10-03 13:57:23 -07002939 case Instruction::INVOKE_SUPER_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002940 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE ||
2941 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002942 bool is_super = (inst->Opcode() == Instruction::INVOKE_SUPER ||
2943 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
Alex Light7268d472016-01-20 15:50:01 -08002944 MethodType type = is_super ? METHOD_SUPER : METHOD_VIRTUAL;
2945 ArtMethod* called_method = VerifyInvocationArgs(inst, type, is_range);
Ian Rogersd8f69b02014-09-10 21:43:52 +00002946 const RegType* return_type = nullptr;
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002947 if (called_method != nullptr) {
Vladimir Markob45528c2017-07-27 14:14:28 +01002948 ObjPtr<mirror::Class> return_type_class = can_load_classes_
2949 ? called_method->ResolveReturnType()
2950 : called_method->LookupResolvedReturnType();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002951 if (return_type_class != nullptr) {
Andreas Gampef23f33d2015-06-23 14:18:17 -07002952 return_type = &FromClass(called_method->GetReturnTypeDescriptor(),
Vladimir Markob45528c2017-07-27 14:14:28 +01002953 return_type_class.Ptr(),
Andreas Gampef23f33d2015-06-23 14:18:17 -07002954 return_type_class->CannotBeAssignedFromOtherTypes());
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002955 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07002956 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
2957 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002958 }
2959 }
2960 if (return_type == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002961 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002962 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
Andreas Gampea5b09a62016-11-17 15:21:22 -08002963 dex::TypeIndex return_type_idx =
2964 dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002965 const char* descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Mathieu Chartierbf99f772014-08-23 16:37:27 -07002966 return_type = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
jeffhaobdb76512011-09-07 11:43:16 -07002967 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002968 if (!return_type->IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002969 work_line_->SetResultRegisterType(this, *return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002970 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002971 work_line_->SetResultRegisterTypeWide(*return_type, return_type->HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002972 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002973 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002974 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002975 }
jeffhaobdb76512011-09-07 11:43:16 -07002976 case Instruction::INVOKE_DIRECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002977 case Instruction::INVOKE_DIRECT_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002978 bool is_range = (inst->Opcode() == Instruction::INVOKE_DIRECT_RANGE);
Alex Light7268d472016-01-20 15:50:01 -08002979 ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_DIRECT, is_range);
Ian Rogers46685432012-06-03 22:26:43 -07002980 const char* return_type_descriptor;
2981 bool is_constructor;
Ian Rogersd8f69b02014-09-10 21:43:52 +00002982 const RegType* return_type = nullptr;
Ian Rogers7b078e82014-09-10 14:44:24 -07002983 if (called_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002984 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogers46685432012-06-03 22:26:43 -07002985 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
Ian Rogersdfb325e2013-10-30 01:00:44 -07002986 is_constructor = strcmp("<init>", dex_file_->StringDataByIdx(method_id.name_idx_)) == 0;
Andreas Gampea5b09a62016-11-17 15:21:22 -08002987 dex::TypeIndex return_type_idx =
2988 dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogers46685432012-06-03 22:26:43 -07002989 return_type_descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2990 } else {
2991 is_constructor = called_method->IsConstructor();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002992 return_type_descriptor = called_method->GetReturnTypeDescriptor();
Vladimir Markob45528c2017-07-27 14:14:28 +01002993 ObjPtr<mirror::Class> return_type_class = can_load_classes_
2994 ? called_method->ResolveReturnType()
2995 : called_method->LookupResolvedReturnType();
Ian Rogers1ff3c982014-08-12 02:30:58 -07002996 if (return_type_class != nullptr) {
Andreas Gampef23f33d2015-06-23 14:18:17 -07002997 return_type = &FromClass(return_type_descriptor,
Vladimir Markob45528c2017-07-27 14:14:28 +01002998 return_type_class.Ptr(),
Andreas Gampef23f33d2015-06-23 14:18:17 -07002999 return_type_class->CannotBeAssignedFromOtherTypes());
Ian Rogers1ff3c982014-08-12 02:30:58 -07003000 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07003001 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
3002 self_->ClearException();
Ian Rogers1ff3c982014-08-12 02:30:58 -07003003 }
Ian Rogers46685432012-06-03 22:26:43 -07003004 }
3005 if (is_constructor) {
jeffhaobdb76512011-09-07 11:43:16 -07003006 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003007 * Some additional checks when calling a constructor. We know from the invocation arg check
3008 * that the "this" argument is an instance of called_method->klass. Now we further restrict
3009 * that to require that called_method->klass is the same as this->klass or this->super,
3010 * allowing the latter only if the "this" argument is the same as the "this" argument to
3011 * this method (which implies that we're in a constructor ourselves).
jeffhaobdb76512011-09-07 11:43:16 -07003012 */
Orion Hodsoncfa325e2016-10-13 10:25:54 +01003013 const RegType& this_type = work_line_->GetInvocationThis(this, inst);
jeffhaob57e9522012-04-26 18:08:21 -07003014 if (this_type.IsConflict()) // failure.
3015 break;
jeffhaobdb76512011-09-07 11:43:16 -07003016
jeffhaob57e9522012-04-26 18:08:21 -07003017 /* no null refs allowed (?) */
Andreas Gampeeed3a5d2017-11-29 14:58:34 -08003018 if (this_type.IsZeroOrNull()) {
jeffhaob57e9522012-04-26 18:08:21 -07003019 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unable to initialize null ref";
3020 break;
jeffhao2a8a90e2011-09-26 14:25:31 -07003021 }
jeffhaob57e9522012-04-26 18:08:21 -07003022
3023 /* must be in same class or in superclass */
Ian Rogersd8f69b02014-09-10 21:43:52 +00003024 // const RegType& this_super_klass = this_type.GetSuperClass(&reg_types_);
Ian Rogers46685432012-06-03 22:26:43 -07003025 // TODO: re-enable constructor type verification
3026 // if (this_super_klass.IsConflict()) {
jeffhaob57e9522012-04-26 18:08:21 -07003027 // Unknown super class, fail so we re-check at runtime.
Ian Rogers46685432012-06-03 22:26:43 -07003028 // Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "super class unknown for '" << this_type << "'";
3029 // break;
3030 // }
jeffhaob57e9522012-04-26 18:08:21 -07003031
3032 /* arg must be an uninitialized reference */
3033 if (!this_type.IsUninitializedTypes()) {
3034 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Expected initialization on uninitialized reference "
3035 << this_type;
3036 break;
3037 }
3038
3039 /*
3040 * Replace the uninitialized reference with an initialized one. We need to do this for all
3041 * registers that have the same object instance in them, not just the "this" register.
3042 */
Nicolas Geoffray98e6ce42016-02-16 18:42:15 +00003043 work_line_->MarkRefsAsInitialized(this, this_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003044 }
Ian Rogers1ff3c982014-08-12 02:30:58 -07003045 if (return_type == nullptr) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07003046 return_type = &reg_types_.FromDescriptor(GetClassLoader(), return_type_descriptor, false);
Ian Rogers1ff3c982014-08-12 02:30:58 -07003047 }
3048 if (!return_type->IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003049 work_line_->SetResultRegisterType(this, *return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003050 } else {
Ian Rogers1ff3c982014-08-12 02:30:58 -07003051 work_line_->SetResultRegisterTypeWide(*return_type, return_type->HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003052 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003053 just_set_result = true;
3054 break;
3055 }
3056 case Instruction::INVOKE_STATIC:
3057 case Instruction::INVOKE_STATIC_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003058 bool is_range = (inst->Opcode() == Instruction::INVOKE_STATIC_RANGE);
Alex Light7268d472016-01-20 15:50:01 -08003059 ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_STATIC, is_range);
Ian Rogers28ad40d2011-10-27 15:19:26 -07003060 const char* descriptor;
Ian Rogers7b078e82014-09-10 14:44:24 -07003061 if (called_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003062 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogers28ad40d2011-10-27 15:19:26 -07003063 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
Andreas Gampea5b09a62016-11-17 15:21:22 -08003064 dex::TypeIndex return_type_idx =
3065 dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Mathieu Chartier590fee92013-09-13 13:46:47 -07003066 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07003067 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003068 descriptor = called_method->GetReturnTypeDescriptor();
Ian Rogers28ad40d2011-10-27 15:19:26 -07003069 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00003070 const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003071 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003072 work_line_->SetResultRegisterType(this, return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003073 } else {
3074 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
3075 }
jeffhaobdb76512011-09-07 11:43:16 -07003076 just_set_result = true;
3077 }
3078 break;
jeffhaobdb76512011-09-07 11:43:16 -07003079 case Instruction::INVOKE_INTERFACE:
Ian Rogersd81871c2011-10-03 13:57:23 -07003080 case Instruction::INVOKE_INTERFACE_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003081 bool is_range = (inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE);
Alex Light7268d472016-01-20 15:50:01 -08003082 ArtMethod* abs_method = VerifyInvocationArgs(inst, METHOD_INTERFACE, is_range);
Ian Rogers7b078e82014-09-10 14:44:24 -07003083 if (abs_method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003084 mirror::Class* called_interface = abs_method->GetDeclaringClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07003085 if (!called_interface->IsInterface() && !called_interface->IsObjectClass()) {
3086 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected interface class in invoke-interface '"
David Sehr709b0702016-10-13 09:12:37 -07003087 << abs_method->PrettyMethod() << "'";
Ian Rogersad0b3a32012-04-16 14:50:24 -07003088 break;
Ian Rogers28ad40d2011-10-27 15:19:26 -07003089 }
Ian Rogers0d604842012-04-16 14:50:24 -07003090 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003091 /* Get the type of the "this" arg, which should either be a sub-interface of called
3092 * interface or Object (see comments in RegType::JoinClass).
3093 */
Orion Hodsoncfa325e2016-10-13 10:25:54 +01003094 const RegType& this_type = work_line_->GetInvocationThis(this, inst);
Andreas Gampeeed3a5d2017-11-29 14:58:34 -08003095 if (this_type.IsZeroOrNull()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003096 /* null pointer always passes (and always fails at runtime) */
3097 } else {
3098 if (this_type.IsUninitializedTypes()) {
3099 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface call on uninitialized object "
3100 << this_type;
3101 break;
3102 }
3103 // In the past we have tried to assert that "called_interface" is assignable
3104 // from "this_type.GetClass()", however, as we do an imprecise Join
3105 // (RegType::JoinClass) we don't have full information on what interfaces are
3106 // implemented by "this_type". For example, two classes may implement the same
3107 // interfaces and have a common parent that doesn't implement the interface. The
3108 // join will set "this_type" to the parent class and a test that this implements
3109 // the interface will incorrectly fail.
3110 }
3111 /*
3112 * We don't have an object instance, so we can't find the concrete method. However, all of
3113 * the type information is in the abstract method, so we're good.
3114 */
3115 const char* descriptor;
Ian Rogers7b078e82014-09-10 14:44:24 -07003116 if (abs_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003117 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07003118 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
Andreas Gampea5b09a62016-11-17 15:21:22 -08003119 dex::TypeIndex return_type_idx =
3120 dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Mathieu Chartierde40d472015-10-15 17:47:48 -07003121 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003122 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003123 descriptor = abs_method->GetReturnTypeDescriptor();
Ian Rogersad0b3a32012-04-16 14:50:24 -07003124 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00003125 const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003126 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003127 work_line_->SetResultRegisterType(this, return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003128 } else {
3129 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
3130 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003131 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07003132 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07003133 }
Narayan Kamath9823e782016-08-03 12:46:58 +01003134 case Instruction::INVOKE_POLYMORPHIC:
3135 case Instruction::INVOKE_POLYMORPHIC_RANGE: {
Orion Hodsoncfa325e2016-10-13 10:25:54 +01003136 bool is_range = (inst->Opcode() == Instruction::INVOKE_POLYMORPHIC_RANGE);
3137 ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_POLYMORPHIC, is_range);
3138 if (called_method == nullptr) {
3139 // Convert potential soft failures in VerifyInvocationArgs() to hard errors.
3140 if (failure_messages_.size() > 0) {
3141 std::string message = failure_messages_.back()->str();
3142 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << message;
3143 } else {
3144 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invoke-polymorphic verification failure.";
3145 }
3146 break;
3147 }
3148 if (!CheckSignaturePolymorphicMethod(called_method) ||
3149 !CheckSignaturePolymorphicReceiver(inst)) {
Orion Hodsonfe92d122018-01-02 10:45:17 +00003150 DCHECK(HasFailures());
Orion Hodsoncfa325e2016-10-13 10:25:54 +01003151 break;
3152 }
3153 const uint32_t proto_idx = (is_range) ? inst->VRegH_4rcc() : inst->VRegH_45cc();
Orion Hodsonac141392017-01-13 11:53:47 +00003154 const char* return_descriptor =
Orion Hodsoncfa325e2016-10-13 10:25:54 +01003155 dex_file_->GetReturnTypeDescriptor(dex_file_->GetProtoId(proto_idx));
3156 const RegType& return_type =
Orion Hodsonac141392017-01-13 11:53:47 +00003157 reg_types_.FromDescriptor(GetClassLoader(), return_descriptor, false);
Orion Hodsoncfa325e2016-10-13 10:25:54 +01003158 if (!return_type.IsLowHalf()) {
3159 work_line_->SetResultRegisterType(this, return_type);
3160 } else {
3161 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
3162 }
Orion Hodsonac141392017-01-13 11:53:47 +00003163 just_set_result = true;
Orion Hodsoncfa325e2016-10-13 10:25:54 +01003164 break;
Narayan Kamath9823e782016-08-03 12:46:58 +01003165 }
Orion Hodsonc069a302017-01-18 09:23:12 +00003166 case Instruction::INVOKE_CUSTOM:
3167 case Instruction::INVOKE_CUSTOM_RANGE: {
3168 // Verify registers based on method_type in the call site.
3169 bool is_range = (inst->Opcode() == Instruction::INVOKE_CUSTOM_RANGE);
3170
3171 // Step 1. Check the call site that produces the method handle for invocation
3172 const uint32_t call_site_idx = is_range ? inst->VRegB_3rc() : inst->VRegB_35c();
3173 if (!CheckCallSite(call_site_idx)) {
3174 DCHECK(HasFailures());
3175 break;
3176 }
3177
3178 // Step 2. Check the register arguments correspond to the expected arguments for the
3179 // method handle produced by step 1. The dex file verifier has checked ranges for
3180 // the first three arguments and CheckCallSite has checked the method handle type.
3181 CallSiteArrayValueIterator it(*dex_file_, dex_file_->GetCallSiteId(call_site_idx));
3182 it.Next(); // Skip to name.
3183 it.Next(); // Skip to method type of the method handle
3184 const uint32_t proto_idx = static_cast<uint32_t>(it.GetJavaValue().i);
3185 const DexFile::ProtoId& proto_id = dex_file_->GetProtoId(proto_idx);
3186 DexFileParameterIterator param_it(*dex_file_, proto_id);
3187 // Treat method as static as it has yet to be determined.
3188 VerifyInvocationArgsFromIterator(&param_it, inst, METHOD_STATIC, is_range, nullptr);
3189 const char* return_descriptor = dex_file_->GetReturnTypeDescriptor(proto_id);
3190
3191 // Step 3. Propagate return type information
3192 const RegType& return_type =
3193 reg_types_.FromDescriptor(GetClassLoader(), return_descriptor, false);
3194 if (!return_type.IsLowHalf()) {
3195 work_line_->SetResultRegisterType(this, return_type);
3196 } else {
3197 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
3198 }
3199 just_set_result = true;
3200 // TODO: Add compiler support for invoke-custom (b/35337872).
3201 Fail(VERIFY_ERROR_FORCE_INTERPRETER);
3202 break;
3203 }
jeffhaobdb76512011-09-07 11:43:16 -07003204 case Instruction::NEG_INT:
3205 case Instruction::NOT_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003206 work_line_->CheckUnaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003207 break;
3208 case Instruction::NEG_LONG:
3209 case Instruction::NOT_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07003210 work_line_->CheckUnaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003211 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07003212 break;
3213 case Instruction::NEG_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003214 work_line_->CheckUnaryOp(this, inst, reg_types_.Float(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07003215 break;
3216 case Instruction::NEG_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07003217 work_line_->CheckUnaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003218 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07003219 break;
3220 case Instruction::INT_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07003221 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003222 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003223 break;
3224 case Instruction::INT_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003225 work_line_->CheckUnaryOp(this, inst, reg_types_.Float(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003226 break;
3227 case Instruction::INT_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07003228 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003229 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003230 break;
3231 case Instruction::LONG_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003232 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003233 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07003234 break;
3235 case Instruction::LONG_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003236 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003237 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07003238 break;
3239 case Instruction::LONG_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07003240 work_line_->CheckUnaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003241 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07003242 break;
3243 case Instruction::FLOAT_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003244 work_line_->CheckUnaryOp(this, inst, reg_types_.Integer(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07003245 break;
3246 case Instruction::FLOAT_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07003247 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003248 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07003249 break;
3250 case Instruction::FLOAT_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07003251 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003252 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07003253 break;
3254 case Instruction::DOUBLE_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003255 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003256 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07003257 break;
3258 case Instruction::DOUBLE_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07003259 work_line_->CheckUnaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003260 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07003261 break;
3262 case Instruction::DOUBLE_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003263 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003264 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07003265 break;
3266 case Instruction::INT_TO_BYTE:
Ian Rogers7b078e82014-09-10 14:44:24 -07003267 work_line_->CheckUnaryOp(this, inst, reg_types_.Byte(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003268 break;
3269 case Instruction::INT_TO_CHAR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003270 work_line_->CheckUnaryOp(this, inst, reg_types_.Char(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003271 break;
3272 case Instruction::INT_TO_SHORT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003273 work_line_->CheckUnaryOp(this, inst, reg_types_.Short(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003274 break;
3275
3276 case Instruction::ADD_INT:
3277 case Instruction::SUB_INT:
3278 case Instruction::MUL_INT:
3279 case Instruction::REM_INT:
3280 case Instruction::DIV_INT:
3281 case Instruction::SHL_INT:
3282 case Instruction::SHR_INT:
3283 case Instruction::USHR_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003284 work_line_->CheckBinaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003285 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07003286 break;
3287 case Instruction::AND_INT:
3288 case Instruction::OR_INT:
3289 case Instruction::XOR_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003290 work_line_->CheckBinaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003291 reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07003292 break;
3293 case Instruction::ADD_LONG:
3294 case Instruction::SUB_LONG:
3295 case Instruction::MUL_LONG:
3296 case Instruction::DIV_LONG:
3297 case Instruction::REM_LONG:
3298 case Instruction::AND_LONG:
3299 case Instruction::OR_LONG:
3300 case Instruction::XOR_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07003301 work_line_->CheckBinaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003302 reg_types_.LongLo(), reg_types_.LongHi(),
3303 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07003304 break;
3305 case Instruction::SHL_LONG:
3306 case Instruction::SHR_LONG:
3307 case Instruction::USHR_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07003308 /* shift distance is Int, making these different from other binary operations */
Ian Rogers7b078e82014-09-10 14:44:24 -07003309 work_line_->CheckBinaryOpWideShift(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003310 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003311 break;
3312 case Instruction::ADD_FLOAT:
3313 case Instruction::SUB_FLOAT:
3314 case Instruction::MUL_FLOAT:
3315 case Instruction::DIV_FLOAT:
3316 case Instruction::REM_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003317 work_line_->CheckBinaryOp(this, inst, reg_types_.Float(), reg_types_.Float(),
3318 reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07003319 break;
3320 case Instruction::ADD_DOUBLE:
3321 case Instruction::SUB_DOUBLE:
3322 case Instruction::MUL_DOUBLE:
3323 case Instruction::DIV_DOUBLE:
3324 case Instruction::REM_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07003325 work_line_->CheckBinaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003326 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
3327 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07003328 break;
3329 case Instruction::ADD_INT_2ADDR:
3330 case Instruction::SUB_INT_2ADDR:
3331 case Instruction::MUL_INT_2ADDR:
3332 case Instruction::REM_INT_2ADDR:
3333 case Instruction::SHL_INT_2ADDR:
3334 case Instruction::SHR_INT_2ADDR:
3335 case Instruction::USHR_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003336 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
3337 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07003338 break;
3339 case Instruction::AND_INT_2ADDR:
3340 case Instruction::OR_INT_2ADDR:
3341 case Instruction::XOR_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003342 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
3343 reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07003344 break;
3345 case Instruction::DIV_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003346 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
3347 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07003348 break;
3349 case Instruction::ADD_LONG_2ADDR:
3350 case Instruction::SUB_LONG_2ADDR:
3351 case Instruction::MUL_LONG_2ADDR:
3352 case Instruction::DIV_LONG_2ADDR:
3353 case Instruction::REM_LONG_2ADDR:
3354 case Instruction::AND_LONG_2ADDR:
3355 case Instruction::OR_LONG_2ADDR:
3356 case Instruction::XOR_LONG_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003357 work_line_->CheckBinaryOp2addrWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003358 reg_types_.LongLo(), reg_types_.LongHi(),
3359 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07003360 break;
3361 case Instruction::SHL_LONG_2ADDR:
3362 case Instruction::SHR_LONG_2ADDR:
3363 case Instruction::USHR_LONG_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003364 work_line_->CheckBinaryOp2addrWideShift(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003365 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003366 break;
3367 case Instruction::ADD_FLOAT_2ADDR:
3368 case Instruction::SUB_FLOAT_2ADDR:
3369 case Instruction::MUL_FLOAT_2ADDR:
3370 case Instruction::DIV_FLOAT_2ADDR:
3371 case Instruction::REM_FLOAT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003372 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Float(), reg_types_.Float(),
3373 reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07003374 break;
3375 case Instruction::ADD_DOUBLE_2ADDR:
3376 case Instruction::SUB_DOUBLE_2ADDR:
3377 case Instruction::MUL_DOUBLE_2ADDR:
3378 case Instruction::DIV_DOUBLE_2ADDR:
3379 case Instruction::REM_DOUBLE_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003380 work_line_->CheckBinaryOp2addrWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003381 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
3382 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07003383 break;
3384 case Instruction::ADD_INT_LIT16:
Ian Rogersf72a11d2014-10-30 15:41:08 -07003385 case Instruction::RSUB_INT_LIT16:
jeffhaobdb76512011-09-07 11:43:16 -07003386 case Instruction::MUL_INT_LIT16:
3387 case Instruction::DIV_INT_LIT16:
3388 case Instruction::REM_INT_LIT16:
Ian Rogers7b078e82014-09-10 14:44:24 -07003389 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), false,
3390 true);
jeffhaobdb76512011-09-07 11:43:16 -07003391 break;
3392 case Instruction::AND_INT_LIT16:
3393 case Instruction::OR_INT_LIT16:
3394 case Instruction::XOR_INT_LIT16:
Ian Rogers7b078e82014-09-10 14:44:24 -07003395 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), true,
3396 true);
jeffhaobdb76512011-09-07 11:43:16 -07003397 break;
3398 case Instruction::ADD_INT_LIT8:
3399 case Instruction::RSUB_INT_LIT8:
3400 case Instruction::MUL_INT_LIT8:
3401 case Instruction::DIV_INT_LIT8:
3402 case Instruction::REM_INT_LIT8:
3403 case Instruction::SHL_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07003404 case Instruction::SHR_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07003405 case Instruction::USHR_INT_LIT8:
Ian Rogers7b078e82014-09-10 14:44:24 -07003406 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), false,
3407 false);
jeffhaobdb76512011-09-07 11:43:16 -07003408 break;
3409 case Instruction::AND_INT_LIT8:
3410 case Instruction::OR_INT_LIT8:
3411 case Instruction::XOR_INT_LIT8:
Ian Rogers7b078e82014-09-10 14:44:24 -07003412 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), true,
3413 false);
jeffhaobdb76512011-09-07 11:43:16 -07003414 break;
3415
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003416 // Special instructions.
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07003417 case Instruction::RETURN_VOID_NO_BARRIER:
3418 if (IsConstructor() && !IsStatic()) {
3419 auto& declaring_class = GetDeclaringClass();
Andreas Gampe68df3202015-06-22 11:35:46 -07003420 if (declaring_class.IsUnresolvedReference()) {
3421 // We must iterate over the fields, even if we cannot use mirror classes to do so. Do it
3422 // manually over the underlying dex file.
3423 uint32_t first_index = GetFirstFinalInstanceFieldIndex(*dex_file_,
3424 dex_file_->GetMethodId(dex_method_idx_).class_idx_);
Andreas Gampee2abbc62017-09-15 11:59:26 -07003425 if (first_index != dex::kDexNoIndex) {
Andreas Gampe68df3202015-06-22 11:35:46 -07003426 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void-no-barrier not expected for field "
3427 << first_index;
3428 }
3429 break;
3430 }
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07003431 auto* klass = declaring_class.GetClass();
3432 for (uint32_t i = 0, num_fields = klass->NumInstanceFields(); i < num_fields; ++i) {
3433 if (klass->GetInstanceField(i)->IsFinal()) {
Mathieu Chartiere86deef2015-03-19 13:43:37 -07003434 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void-no-barrier not expected for "
David Sehr709b0702016-10-13 09:12:37 -07003435 << klass->GetInstanceField(i)->PrettyField();
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07003436 break;
3437 }
3438 }
Sebastien Hertzcc10e0e2013-06-28 14:24:48 +02003439 }
Andreas Gampeb2917962015-07-31 13:36:10 -07003440 // Handle this like a RETURN_VOID now. Code is duplicated to separate standard from
3441 // quickened opcodes (otherwise this could be a fall-through).
3442 if (!IsConstructor()) {
3443 if (!GetMethodReturnType().IsConflict()) {
3444 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void not expected";
3445 }
3446 }
Sebastien Hertzcc10e0e2013-06-28 14:24:48 +02003447 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003448 // Note: the following instructions encode offsets derived from class linking.
Neil Fuller0e844392016-09-08 13:43:31 +01003449 // As such they use Class*/Field*/Executable* as these offsets only have
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003450 // meaning if the class linking and resolution were successful.
3451 case Instruction::IGET_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003452 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003453 break;
3454 case Instruction::IGET_WIDE_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003455 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003456 break;
3457 case Instruction::IGET_OBJECT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003458 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003459 break;
Mathieu Chartierffc605c2014-12-10 10:35:44 -08003460 case Instruction::IGET_BOOLEAN_QUICK:
3461 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true);
3462 break;
3463 case Instruction::IGET_BYTE_QUICK:
3464 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true);
3465 break;
3466 case Instruction::IGET_CHAR_QUICK:
3467 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true);
3468 break;
3469 case Instruction::IGET_SHORT_QUICK:
3470 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true);
3471 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003472 case Instruction::IPUT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003473 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003474 break;
Fred Shih37f05ef2014-07-16 18:38:08 -07003475 case Instruction::IPUT_BOOLEAN_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003476 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07003477 break;
3478 case Instruction::IPUT_BYTE_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003479 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07003480 break;
3481 case Instruction::IPUT_CHAR_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003482 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07003483 break;
3484 case Instruction::IPUT_SHORT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003485 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07003486 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003487 case Instruction::IPUT_WIDE_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003488 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003489 break;
3490 case Instruction::IPUT_OBJECT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003491 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003492 break;
3493 case Instruction::INVOKE_VIRTUAL_QUICK:
3494 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: {
3495 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
Mathieu Chartiere401d142015-04-22 13:56:20 -07003496 ArtMethod* called_method = VerifyInvokeVirtualQuickArgs(inst, is_range);
Ian Rogers7b078e82014-09-10 14:44:24 -07003497 if (called_method != nullptr) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003498 const char* descriptor = called_method->GetReturnTypeDescriptor();
Ian Rogersd8f69b02014-09-10 21:43:52 +00003499 const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003500 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003501 work_line_->SetResultRegisterType(this, return_type);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003502 } else {
3503 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
3504 }
3505 just_set_result = true;
3506 }
3507 break;
3508 }
3509
Ian Rogersd81871c2011-10-03 13:57:23 -07003510 /* These should never appear during verification. */
Mathieu Chartierffc605c2014-12-10 10:35:44 -08003511 case Instruction::UNUSED_3E ... Instruction::UNUSED_43:
Narayan Kamath8ec3bd22016-08-03 12:46:23 +01003512 case Instruction::UNUSED_F3 ... Instruction::UNUSED_F9:
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003513 case Instruction::UNUSED_79:
3514 case Instruction::UNUSED_7A:
jeffhaod5347e02012-03-22 17:25:05 -07003515 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Unexpected opcode " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07003516 break;
3517
3518 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003519 * DO NOT add a "default" clause here. Without it the compiler will
jeffhaobdb76512011-09-07 11:43:16 -07003520 * complain if an instruction is missing (which is desirable).
3521 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003522 } // end - switch (dec_insn.opcode)
jeffhaobdb76512011-09-07 11:43:16 -07003523
Ian Rogersad0b3a32012-04-16 14:50:24 -07003524 if (have_pending_hard_failure_) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08003525 if (Runtime::Current()->IsAotCompiler()) {
3526 /* When AOT compiling, check that the last failure is a hard failure */
Andreas Gampeb588f4c2015-05-26 13:35:39 -07003527 if (failures_[failures_.size() - 1] != VERIFY_ERROR_BAD_CLASS_HARD) {
3528 LOG(ERROR) << "Pending failures:";
3529 for (auto& error : failures_) {
3530 LOG(ERROR) << error;
3531 }
3532 for (auto& error_msg : failure_messages_) {
3533 LOG(ERROR) << error_msg->str();
3534 }
3535 LOG(FATAL) << "Pending hard failure, but last failure not hard.";
3536 }
Ian Rogerse1758fe2012-04-19 11:31:15 -07003537 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003538 /* immediate failure, reject class */
3539 info_messages_ << "Rejecting opcode " << inst->DumpString(dex_file_);
3540 return false;
jeffhaofaf459e2012-08-31 15:32:47 -07003541 } else if (have_pending_runtime_throw_failure_) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07003542 /* checking interpreter will throw, mark following code as unreachable */
jeffhaofaf459e2012-08-31 15:32:47 -07003543 opcode_flags = Instruction::kThrow;
Andreas Gampea727e372015-08-25 09:22:37 -07003544 // Note: the flag must be reset as it is only global to decouple Fail and is semantically per
3545 // instruction. However, RETURN checking may throw LOCKING errors, so we clear at the
3546 // very end.
jeffhaobdb76512011-09-07 11:43:16 -07003547 }
jeffhaobdb76512011-09-07 11:43:16 -07003548 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003549 * If we didn't just set the result register, clear it out. This ensures that you can only use
3550 * "move-result" immediately after the result is set. (We could check this statically, but it's
3551 * not expensive and it makes our debugging output cleaner.)
jeffhaobdb76512011-09-07 11:43:16 -07003552 */
3553 if (!just_set_result) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003554 work_line_->SetResultTypeToUnknown(this);
jeffhaobdb76512011-09-07 11:43:16 -07003555 }
3556
jeffhaobdb76512011-09-07 11:43:16 -07003557 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003558 * Handle "branch". Tag the branch target.
jeffhaobdb76512011-09-07 11:43:16 -07003559 *
3560 * NOTE: instructions like Instruction::EQZ provide information about the
jeffhaod1f0fde2011-09-08 17:25:33 -07003561 * state of the register when the branch is taken or not taken. For example,
jeffhaobdb76512011-09-07 11:43:16 -07003562 * somebody could get a reference field, check it for zero, and if the
3563 * branch is taken immediately store that register in a boolean field
jeffhaod1f0fde2011-09-08 17:25:33 -07003564 * since the value is known to be zero. We do not currently account for
jeffhaobdb76512011-09-07 11:43:16 -07003565 * that, and will reject the code.
3566 *
3567 * TODO: avoid re-fetching the branch target
3568 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003569 if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07003570 bool isConditional, selfOkay;
Ian Rogersd81871c2011-10-03 13:57:23 -07003571 if (!GetBranchOffset(work_insn_idx_, &branch_target, &isConditional, &selfOkay)) {
jeffhaobdb76512011-09-07 11:43:16 -07003572 /* should never happen after static verification */
jeffhaod5347e02012-03-22 17:25:05 -07003573 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad branch";
jeffhaobdb76512011-09-07 11:43:16 -07003574 return false;
3575 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08003576 DCHECK_EQ(isConditional, (opcode_flags & Instruction::kContinue) != 0);
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08003577 if (!CheckNotMoveExceptionOrMoveResult(code_item_accessor_.Insns(),
3578 work_insn_idx_ + branch_target)) {
jeffhaobdb76512011-09-07 11:43:16 -07003579 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07003580 }
jeffhaobdb76512011-09-07 11:43:16 -07003581 /* update branch target, set "changed" if appropriate */
Mathieu Chartierde40d472015-10-15 17:47:48 -07003582 if (nullptr != branch_line) {
Ian Rogersebbdd872014-07-07 23:53:08 -07003583 if (!UpdateRegisters(work_insn_idx_ + branch_target, branch_line.get(), false)) {
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003584 return false;
3585 }
3586 } else {
Ian Rogersebbdd872014-07-07 23:53:08 -07003587 if (!UpdateRegisters(work_insn_idx_ + branch_target, work_line_.get(), false)) {
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003588 return false;
3589 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003590 }
jeffhaobdb76512011-09-07 11:43:16 -07003591 }
3592
3593 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003594 * Handle "switch". Tag all possible branch targets.
jeffhaobdb76512011-09-07 11:43:16 -07003595 *
3596 * We've already verified that the table is structurally sound, so we
3597 * just need to walk through and tag the targets.
3598 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003599 if ((opcode_flags & Instruction::kSwitch) != 0) {
Andreas Gampe53de99c2015-08-17 13:43:55 -07003600 int offset_to_switch = insns[1] | (static_cast<int32_t>(insns[2]) << 16);
jeffhaobdb76512011-09-07 11:43:16 -07003601 const uint16_t* switch_insns = insns + offset_to_switch;
3602 int switch_count = switch_insns[1];
3603 int offset_to_targets, targ;
3604
3605 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
3606 /* 0 = sig, 1 = count, 2/3 = first key */
3607 offset_to_targets = 4;
3608 } else {
3609 /* 0 = sig, 1 = count, 2..count * 2 = keys */
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07003610 DCHECK((*insns & 0xff) == Instruction::SPARSE_SWITCH);
jeffhaobdb76512011-09-07 11:43:16 -07003611 offset_to_targets = 2 + 2 * switch_count;
3612 }
3613
3614 /* verify each switch target */
3615 for (targ = 0; targ < switch_count; targ++) {
3616 int offset;
3617 uint32_t abs_offset;
3618
3619 /* offsets are 32-bit, and only partly endian-swapped */
3620 offset = switch_insns[offset_to_targets + targ * 2] |
Andreas Gampe53de99c2015-08-17 13:43:55 -07003621 (static_cast<int32_t>(switch_insns[offset_to_targets + targ * 2 + 1]) << 16);
Ian Rogersd81871c2011-10-03 13:57:23 -07003622 abs_offset = work_insn_idx_ + offset;
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08003623 DCHECK_LT(abs_offset, code_item_accessor_.InsnsSizeInCodeUnits());
3624 if (!CheckNotMoveExceptionOrMoveResult(code_item_accessor_.Insns(), abs_offset)) {
jeffhaobdb76512011-09-07 11:43:16 -07003625 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07003626 }
Ian Rogersebbdd872014-07-07 23:53:08 -07003627 if (!UpdateRegisters(abs_offset, work_line_.get(), false)) {
jeffhaobdb76512011-09-07 11:43:16 -07003628 return false;
Ian Rogersebbdd872014-07-07 23:53:08 -07003629 }
jeffhaobdb76512011-09-07 11:43:16 -07003630 }
3631 }
3632
3633 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003634 * Handle instructions that can throw and that are sitting in a "try" block. (If they're not in a
3635 * "try" block when they throw, control transfers out of the method.)
jeffhaobdb76512011-09-07 11:43:16 -07003636 */
Mathieu Chartierde40d472015-10-15 17:47:48 -07003637 if ((opcode_flags & Instruction::kThrow) != 0 && GetInstructionFlags(work_insn_idx_).IsInTry()) {
Andreas Gampef91baf12014-07-18 15:41:00 -07003638 bool has_catch_all_handler = false;
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08003639 const DexFile::TryItem* try_item = code_item_accessor_.FindTryItem(work_insn_idx_);
3640 CHECK(try_item != nullptr);
Mathieu Chartierdc578c72017-12-27 11:51:45 -08003641 CatchHandlerIterator iterator(code_item_accessor_, *try_item);
jeffhaobdb76512011-09-07 11:43:16 -07003642
Andreas Gampef91baf12014-07-18 15:41:00 -07003643 // Need the linker to try and resolve the handled class to check if it's Throwable.
3644 ClassLinker* linker = Runtime::Current()->GetClassLinker();
3645
Ian Rogers0571d352011-11-03 19:51:38 -07003646 for (; iterator.HasNext(); iterator.Next()) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08003647 dex::TypeIndex handler_type_idx = iterator.GetHandlerTypeIndex();
3648 if (!handler_type_idx.IsValid()) {
Andreas Gampef91baf12014-07-18 15:41:00 -07003649 has_catch_all_handler = true;
3650 } else {
3651 // It is also a catch-all if it is java.lang.Throwable.
Vladimir Marko28e012a2017-12-07 11:22:59 +00003652 ObjPtr<mirror::Class> klass =
Vladimir Marko666ee3d2017-12-11 18:37:36 +00003653 linker->ResolveType(handler_type_idx, dex_cache_, class_loader_);
Andreas Gampef91baf12014-07-18 15:41:00 -07003654 if (klass != nullptr) {
3655 if (klass == mirror::Throwable::GetJavaLangThrowable()) {
3656 has_catch_all_handler = true;
3657 }
3658 } else {
3659 // Clear exception.
Ian Rogers7b078e82014-09-10 14:44:24 -07003660 DCHECK(self_->IsExceptionPending());
3661 self_->ClearException();
Andreas Gampef91baf12014-07-18 15:41:00 -07003662 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003663 }
jeffhaobdb76512011-09-07 11:43:16 -07003664 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003665 * Merge registers into the "catch" block. We want to use the "savedRegs" rather than
3666 * "work_regs", because at runtime the exception will be thrown before the instruction
3667 * modifies any registers.
jeffhaobdb76512011-09-07 11:43:16 -07003668 */
Ian Rogersebbdd872014-07-07 23:53:08 -07003669 if (!UpdateRegisters(iterator.GetHandlerAddress(), saved_line_.get(), false)) {
jeffhaobdb76512011-09-07 11:43:16 -07003670 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07003671 }
jeffhaobdb76512011-09-07 11:43:16 -07003672 }
3673
3674 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003675 * If the monitor stack depth is nonzero, there must be a "catch all" handler for this
3676 * instruction. This does apply to monitor-exit because of async exception handling.
jeffhaobdb76512011-09-07 11:43:16 -07003677 */
Andreas Gampef91baf12014-07-18 15:41:00 -07003678 if (work_line_->MonitorStackDepth() > 0 && !has_catch_all_handler) {
jeffhaobdb76512011-09-07 11:43:16 -07003679 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003680 * The state in work_line reflects the post-execution state. If the current instruction is a
3681 * monitor-enter and the monitor stack was empty, we don't need a catch-all (if it throws,
jeffhaobdb76512011-09-07 11:43:16 -07003682 * it will do so before grabbing the lock).
3683 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02003684 if (inst->Opcode() != Instruction::MONITOR_ENTER || work_line_->MonitorStackDepth() != 1) {
jeffhaod5347e02012-03-22 17:25:05 -07003685 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
Ian Rogersd81871c2011-10-03 13:57:23 -07003686 << "expected to be within a catch-all for an instruction where a monitor is held";
jeffhaobdb76512011-09-07 11:43:16 -07003687 return false;
3688 }
3689 }
3690 }
3691
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003692 /* Handle "continue". Tag the next consecutive instruction.
3693 * Note: Keep the code handling "continue" case below the "branch" and "switch" cases,
3694 * because it changes work_line_ when performing peephole optimization
3695 * and this change should not be used in those cases.
3696 */
Ian Rogers6d376ae2013-07-23 15:12:40 -07003697 if ((opcode_flags & Instruction::kContinue) != 0) {
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08003698 DCHECK_EQ(&code_item_accessor_.InstructionAt(work_insn_idx_), inst);
Ian Rogers7b078e82014-09-10 14:44:24 -07003699 uint32_t next_insn_idx = work_insn_idx_ + inst->SizeInCodeUnits();
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08003700 if (next_insn_idx >= code_item_accessor_.InsnsSizeInCodeUnits()) {
Ian Rogers6d376ae2013-07-23 15:12:40 -07003701 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Execution can walk off end of code area";
3702 return false;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003703 }
Ian Rogers6d376ae2013-07-23 15:12:40 -07003704 // The only way to get to a move-exception instruction is to get thrown there. Make sure the
3705 // next instruction isn't one.
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08003706 if (!CheckNotMoveException(code_item_accessor_.Insns(), next_insn_idx)) {
Ian Rogers6d376ae2013-07-23 15:12:40 -07003707 return false;
3708 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07003709 if (nullptr != fallthrough_line) {
Ian Rogers6d376ae2013-07-23 15:12:40 -07003710 // Make workline consistent with fallthrough computed from peephole optimization.
3711 work_line_->CopyFromLine(fallthrough_line.get());
3712 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07003713 if (GetInstructionFlags(next_insn_idx).IsReturn()) {
Ian Rogersb8c78592013-07-25 23:52:52 +00003714 // For returns we only care about the operand to the return, all other registers are dead.
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08003715 const Instruction* ret_inst = &code_item_accessor_.InstructionAt(next_insn_idx);
Andreas Gampea727e372015-08-25 09:22:37 -07003716 AdjustReturnLine(this, ret_inst, work_line_.get());
Ian Rogersb8c78592013-07-25 23:52:52 +00003717 }
Ian Rogers6d376ae2013-07-23 15:12:40 -07003718 RegisterLine* next_line = reg_table_.GetLine(next_insn_idx);
Ian Rogers7b078e82014-09-10 14:44:24 -07003719 if (next_line != nullptr) {
Ian Rogersebbdd872014-07-07 23:53:08 -07003720 // Merge registers into what we have for the next instruction, and set the "changed" flag if
3721 // needed. If the merge changes the state of the registers then the work line will be
3722 // updated.
3723 if (!UpdateRegisters(next_insn_idx, work_line_.get(), true)) {
Ian Rogers6d376ae2013-07-23 15:12:40 -07003724 return false;
3725 }
3726 } else {
3727 /*
3728 * We're not recording register data for the next instruction, so we don't know what the
3729 * prior state was. We have to assume that something has changed and re-evaluate it.
3730 */
Mathieu Chartierde40d472015-10-15 17:47:48 -07003731 GetInstructionFlags(next_insn_idx).SetChanged();
Ian Rogers6d376ae2013-07-23 15:12:40 -07003732 }
3733 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003734
jeffhaod1f0fde2011-09-08 17:25:33 -07003735 /* If we're returning from the method, make sure monitor stack is empty. */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003736 if ((opcode_flags & Instruction::kReturn) != 0) {
Andreas Gampea727e372015-08-25 09:22:37 -07003737 work_line_->VerifyMonitorStackEmpty(this);
jeffhaobdb76512011-09-07 11:43:16 -07003738 }
3739
3740 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003741 * Update start_guess. Advance to the next instruction of that's
3742 * possible, otherwise use the branch target if one was found. If
jeffhaobdb76512011-09-07 11:43:16 -07003743 * neither of those exists we're in a return or throw; leave start_guess
3744 * alone and let the caller sort it out.
3745 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003746 if ((opcode_flags & Instruction::kContinue) != 0) {
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08003747 DCHECK_EQ(&code_item_accessor_.InstructionAt(work_insn_idx_), inst);
Ian Rogers7b078e82014-09-10 14:44:24 -07003748 *start_guess = work_insn_idx_ + inst->SizeInCodeUnits();
Elliott Hughesadb8c672012-03-06 16:49:32 -08003749 } else if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07003750 /* we're still okay if branch_target is zero */
Ian Rogersd81871c2011-10-03 13:57:23 -07003751 *start_guess = work_insn_idx_ + branch_target;
jeffhaobdb76512011-09-07 11:43:16 -07003752 }
3753
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08003754 DCHECK_LT(*start_guess, code_item_accessor_.InsnsSizeInCodeUnits());
Mathieu Chartierde40d472015-10-15 17:47:48 -07003755 DCHECK(GetInstructionFlags(*start_guess).IsOpcode());
jeffhaobdb76512011-09-07 11:43:16 -07003756
Andreas Gampea727e372015-08-25 09:22:37 -07003757 if (have_pending_runtime_throw_failure_) {
3758 have_any_pending_runtime_throw_failure_ = true;
3759 // Reset the pending_runtime_throw flag now.
3760 have_pending_runtime_throw_failure_ = false;
3761 }
3762
jeffhaobdb76512011-09-07 11:43:16 -07003763 return true;
Brian Carlstrom7934ac22013-07-26 10:54:15 -07003764} // NOLINT(readability/fn_size)
jeffhaobdb76512011-09-07 11:43:16 -07003765
Mathieu Chartierde40d472015-10-15 17:47:48 -07003766void MethodVerifier::UninstantiableError(const char* descriptor) {
3767 Fail(VerifyError::VERIFY_ERROR_NO_CLASS) << "Could not create precise reference for "
3768 << "non-instantiable klass " << descriptor;
3769}
3770
Vladimir Marko28e012a2017-12-07 11:22:59 +00003771inline bool MethodVerifier::IsInstantiableOrPrimitive(ObjPtr<mirror::Class> klass) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07003772 return klass->IsInstantiable() || klass->IsPrimitive();
3773}
3774
Andreas Gampe98be1a92017-08-28 08:25:45 -07003775template <MethodVerifier::CheckAccess C>
3776const RegType& MethodVerifier::ResolveClass(dex::TypeIndex class_idx) {
Vladimir Marko666ee3d2017-12-11 18:37:36 +00003777 ClassLinker* linker = Runtime::Current()->GetClassLinker();
Vladimir Marko28e012a2017-12-07 11:22:59 +00003778 ObjPtr<mirror::Class> klass = can_load_classes_
Vladimir Marko666ee3d2017-12-11 18:37:36 +00003779 ? linker->ResolveType(class_idx, dex_cache_, class_loader_)
3780 : linker->LookupResolvedType(class_idx, dex_cache_.Get(), class_loader_.Get());
Vladimir Marko8d6768d2017-03-14 10:13:21 +00003781 if (can_load_classes_ && klass == nullptr) {
3782 DCHECK(self_->IsExceptionPending());
3783 self_->ClearException();
3784 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07003785 const RegType* result = nullptr;
Vladimir Marko9cb0c462017-04-21 13:31:41 +01003786 if (klass != nullptr) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07003787 bool precise = klass->CannotBeAssignedFromOtherTypes();
3788 if (precise && !IsInstantiableOrPrimitive(klass)) {
3789 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
3790 UninstantiableError(descriptor);
3791 precise = false;
3792 }
Vladimir Marko28e012a2017-12-07 11:22:59 +00003793 result = reg_types_.FindClass(klass.Ptr(), precise);
Mathieu Chartierde40d472015-10-15 17:47:48 -07003794 if (result == nullptr) {
3795 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
Vladimir Marko28e012a2017-12-07 11:22:59 +00003796 result = reg_types_.InsertClass(descriptor, klass.Ptr(), precise);
Mathieu Chartierde40d472015-10-15 17:47:48 -07003797 }
3798 } else {
3799 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
3800 result = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003801 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07003802 DCHECK(result != nullptr);
3803 if (result->IsConflict()) {
3804 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
3805 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "accessing broken descriptor '" << descriptor
3806 << "' in " << GetDeclaringClass();
3807 return *result;
3808 }
David Brazdilca3c8c32016-09-06 14:04:48 +01003809
3810 // Record result of class resolution attempt.
Vladimir Marko28e012a2017-12-07 11:22:59 +00003811 VerifierDeps::MaybeRecordClassResolution(*dex_file_, class_idx, klass.Ptr());
David Brazdilca3c8c32016-09-06 14:04:48 +01003812
Andreas Gampe629be512017-08-25 17:09:32 -07003813 // If requested, check if access is allowed. Unresolved types are included in this check, as the
3814 // interpreter only tests whether access is allowed when a class is not pre-verified and runs in
3815 // the access-checks interpreter. If result is primitive, skip the access check.
3816 //
3817 // Note: we do this for unresolved classes to trigger re-verification at runtime.
3818 if (C == CheckAccess::kYes && result->IsNonZeroReferenceTypes()) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07003819 const RegType& referrer = GetDeclaringClass();
Andreas Gampe629be512017-08-25 17:09:32 -07003820 if (!referrer.CanAccess(*result)) {
3821 Fail(VERIFY_ERROR_ACCESS_CLASS) << "(possibly) illegal class access: '"
Nicolas Geoffray4e868fa2017-04-21 17:16:44 +01003822 << referrer << "' -> '" << *result << "'";
Mathieu Chartierde40d472015-10-15 17:47:48 -07003823 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07003824 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07003825 return *result;
Ian Rogersd81871c2011-10-03 13:57:23 -07003826}
3827
Andreas Gampedd309072017-08-31 07:47:37 -07003828// Instantiate ResolveClass variants. This is required as the -inl file has a function with a call
3829// to ResolveClass, and compilers may decide to inline, requiring a symbol.
Andreas Gampeae0b1f42017-08-30 17:36:26 -07003830template const RegType& MethodVerifier::ResolveClass<MethodVerifier::CheckAccess::kNo>(
3831 dex::TypeIndex class_idx);
3832template const RegType& MethodVerifier::ResolveClass<MethodVerifier::CheckAccess::kYes>(
3833 dex::TypeIndex class_idx);
3834
Ian Rogersd8f69b02014-09-10 21:43:52 +00003835const RegType& MethodVerifier::GetCaughtExceptionType() {
Ian Rogers7b078e82014-09-10 14:44:24 -07003836 const RegType* common_super = nullptr;
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08003837 if (code_item_accessor_.TriesSize() != 0) {
3838 const uint8_t* handlers_ptr = code_item_accessor_.GetCatchHandlerData();
Ian Rogersd81871c2011-10-03 13:57:23 -07003839 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
3840 for (uint32_t i = 0; i < handlers_size; i++) {
Ian Rogers0571d352011-11-03 19:51:38 -07003841 CatchHandlerIterator iterator(handlers_ptr);
3842 for (; iterator.HasNext(); iterator.Next()) {
3843 if (iterator.GetHandlerAddress() == (uint32_t) work_insn_idx_) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08003844 if (!iterator.GetHandlerTypeIndex().IsValid()) {
Ian Rogersb4903572012-10-11 11:52:56 -07003845 common_super = &reg_types_.JavaLangThrowable(false);
Ian Rogersd81871c2011-10-03 13:57:23 -07003846 } else {
Andreas Gampe98be1a92017-08-28 08:25:45 -07003847 const RegType& exception =
3848 ResolveClass<CheckAccess::kYes>(iterator.GetHandlerTypeIndex());
David Brazdilca3c8c32016-09-06 14:04:48 +01003849 if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(exception, this)) {
David Brazdil68b5c0b2016-01-19 14:25:29 +00003850 DCHECK(!exception.IsUninitializedTypes()); // Comes from dex, shouldn't be uninit.
Jeff Haoc26a56c2013-11-04 12:00:47 -08003851 if (exception.IsUnresolvedTypes()) {
3852 // We don't know enough about the type. Fail here and let runtime handle it.
3853 Fail(VERIFY_ERROR_NO_CLASS) << "unresolved exception class " << exception;
3854 return exception;
3855 } else {
3856 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unexpected non-exception class " << exception;
3857 return reg_types_.Conflict();
3858 }
Jeff Haob878f212014-04-24 16:25:36 -07003859 } else if (common_super == nullptr) {
3860 common_super = &exception;
Ian Rogers28ad40d2011-10-27 15:19:26 -07003861 } else if (common_super->Equals(exception)) {
Ian Rogersc4762272012-02-01 15:55:55 -08003862 // odd case, but nothing to do
Ian Rogersd81871c2011-10-03 13:57:23 -07003863 } else {
David Brazdilca3c8c32016-09-06 14:04:48 +01003864 common_super = &common_super->Merge(exception, &reg_types_, this);
Andreas Gampe7c038102014-10-27 20:08:46 -07003865 if (FailOrAbort(this,
David Brazdilca3c8c32016-09-06 14:04:48 +01003866 reg_types_.JavaLangThrowable(false).IsAssignableFrom(
3867 *common_super, this),
Andreas Gampe7c038102014-10-27 20:08:46 -07003868 "java.lang.Throwable is not assignable-from common_super at ",
3869 work_insn_idx_)) {
3870 break;
3871 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003872 }
3873 }
3874 }
3875 }
Ian Rogers0571d352011-11-03 19:51:38 -07003876 handlers_ptr = iterator.EndDataPointer();
Ian Rogersd81871c2011-10-03 13:57:23 -07003877 }
3878 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003879 if (common_super == nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003880 /* no catch blocks, or no catches with classes we can find */
jeffhaod5347e02012-03-22 17:25:05 -07003881 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unable to find exception handler";
Ian Rogersad0b3a32012-04-16 14:50:24 -07003882 return reg_types_.Conflict();
Ian Rogersd81871c2011-10-03 13:57:23 -07003883 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07003884 return *common_super;
Ian Rogersd81871c2011-10-03 13:57:23 -07003885}
3886
Mathieu Chartiere401d142015-04-22 13:56:20 -07003887ArtMethod* MethodVerifier::ResolveMethodAndCheckAccess(
Alex Light7268d472016-01-20 15:50:01 -08003888 uint32_t dex_method_idx, MethodType method_type) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003889 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx);
Andreas Gampe98be1a92017-08-28 08:25:45 -07003890 const RegType& klass_type = ResolveClass<CheckAccess::kYes>(method_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003891 if (klass_type.IsConflict()) {
3892 std::string append(" in attempt to access method ");
3893 append += dex_file_->GetMethodName(method_id);
3894 AppendToLastFailMessage(append);
Ian Rogers7b078e82014-09-10 14:44:24 -07003895 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08003896 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003897 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003898 return nullptr; // Can't resolve Class so no more to do here
Ian Rogers90040192011-12-16 08:54:29 -08003899 }
Vladimir Markoba118822017-06-12 15:41:56 +01003900 ObjPtr<mirror::Class> klass = klass_type.GetClass();
Ian Rogersd8f69b02014-09-10 21:43:52 +00003901 const RegType& referrer = GetDeclaringClass();
Nicolas Geoffrayea179f42018-02-08 22:30:18 +00003902 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
3903 PointerSize pointer_size = class_linker->GetImagePointerSize();
Andreas Gampe42ef8ab2015-12-03 17:27:32 -08003904
Mathieu Chartiere401d142015-04-22 13:56:20 -07003905 ArtMethod* res_method = dex_cache_->GetResolvedMethod(dex_method_idx, pointer_size);
Ian Rogers7b078e82014-09-10 14:44:24 -07003906 if (res_method == nullptr) {
Nicolas Geoffrayea179f42018-02-08 22:30:18 +00003907 res_method = class_linker->FindResolvedMethod(
3908 klass, dex_cache_.Get(), class_loader_.Get(), dex_method_idx);
Ian Rogersd81871c2011-10-03 13:57:23 -07003909 }
David Brazdilca3c8c32016-09-06 14:04:48 +01003910
Vladimir Markoba118822017-06-12 15:41:56 +01003911 // Record result of method resolution attempt. The klass resolution has recorded whether
3912 // the class is an interface or not and therefore the type of the lookup performed above.
3913 // TODO: Maybe we should not record dependency if the invoke type does not match the lookup type.
3914 VerifierDeps::MaybeRecordMethodResolution(*dex_file_, dex_method_idx, res_method);
3915
Alex Lightafb66472017-08-01 09:54:49 -07003916 bool must_fail = false;
3917 // This is traditional and helps with screwy bytecode. It will tell you that, yes, a method
3918 // exists, but that it's called incorrectly. This significantly helps debugging, as locally it's
3919 // hard to see the differences.
3920 // If we don't have res_method here we must fail. Just use this bool to make sure of that with a
3921 // DCHECK.
Vladimir Markoba118822017-06-12 15:41:56 +01003922 if (res_method == nullptr) {
Alex Lightafb66472017-08-01 09:54:49 -07003923 must_fail = true;
Vladimir Markoba118822017-06-12 15:41:56 +01003924 // Try to find the method also with the other type for better error reporting below
3925 // but do not store such bogus lookup result in the DexCache or VerifierDeps.
3926 if (klass->IsInterface()) {
Alex Lightafb66472017-08-01 09:54:49 -07003927 // NB This is normally not really allowed but we want to get any static or private object
3928 // methods for error message purposes. This will never be returned.
3929 // TODO We might want to change the verifier to not require this.
Vladimir Markoba118822017-06-12 15:41:56 +01003930 res_method = klass->FindClassMethod(dex_cache_.Get(), dex_method_idx, pointer_size);
3931 } else {
3932 // If there was an interface method with the same signature,
3933 // we would have found it also in the "copied" methods.
3934 DCHECK(klass->FindInterfaceMethod(dex_cache_.Get(), dex_method_idx, pointer_size) == nullptr);
3935 }
3936 }
David Brazdilca3c8c32016-09-06 14:04:48 +01003937
3938 if (res_method == nullptr) {
3939 Fail(VERIFY_ERROR_NO_METHOD) << "couldn't find method "
David Sehr709b0702016-10-13 09:12:37 -07003940 << klass->PrettyDescriptor() << "."
David Brazdilca3c8c32016-09-06 14:04:48 +01003941 << dex_file_->GetMethodName(method_id) << " "
3942 << dex_file_->GetMethodSignature(method_id);
3943 return nullptr;
3944 }
3945
Ian Rogersd81871c2011-10-03 13:57:23 -07003946 // Make sure calls to constructors are "direct". There are additional restrictions but we don't
3947 // enforce them here.
3948 if (res_method->IsConstructor() && method_type != METHOD_DIRECT) {
jeffhaod5347e02012-03-22 17:25:05 -07003949 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting non-direct call to constructor "
David Sehr709b0702016-10-13 09:12:37 -07003950 << res_method->PrettyMethod();
Ian Rogers7b078e82014-09-10 14:44:24 -07003951 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003952 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003953 // Disallow any calls to class initializers.
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003954 if (res_method->IsClassInitializer()) {
jeffhaod5347e02012-03-22 17:25:05 -07003955 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting call to class initializer "
David Sehr709b0702016-10-13 09:12:37 -07003956 << res_method->PrettyMethod();
Ian Rogers7b078e82014-09-10 14:44:24 -07003957 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003958 }
Andreas Gampe42ef8ab2015-12-03 17:27:32 -08003959
3960 // Check that interface methods are static or match interface classes.
3961 // We only allow statics if we don't have default methods enabled.
3962 //
3963 // Note: this check must be after the initializer check, as those are required to fail a class,
3964 // while this check implies an IncompatibleClassChangeError.
3965 if (klass->IsInterface()) {
Alex Lightb55f1ac2016-04-12 15:50:55 -07003966 // methods called on interfaces should be invoke-interface, invoke-super, invoke-direct (if
Mathieu Chartierf6e31472017-12-28 13:32:08 -08003967 // default methods are supported for the dex file), or invoke-static.
Andreas Gampe42ef8ab2015-12-03 17:27:32 -08003968 if (method_type != METHOD_INTERFACE &&
Neil Fuller9724c632016-01-07 15:42:47 +00003969 method_type != METHOD_STATIC &&
Mathieu Chartierf6e31472017-12-28 13:32:08 -08003970 (!dex_file_->SupportsDefaultMethods() ||
Alex Lightb55f1ac2016-04-12 15:50:55 -07003971 method_type != METHOD_DIRECT) &&
Neil Fuller9724c632016-01-07 15:42:47 +00003972 method_type != METHOD_SUPER) {
Andreas Gampe42ef8ab2015-12-03 17:27:32 -08003973 Fail(VERIFY_ERROR_CLASS_CHANGE)
David Sehr709b0702016-10-13 09:12:37 -07003974 << "non-interface method " << dex_file_->PrettyMethod(dex_method_idx)
3975 << " is in an interface class " << klass->PrettyClass();
Andreas Gampe42ef8ab2015-12-03 17:27:32 -08003976 return nullptr;
3977 }
3978 } else {
3979 if (method_type == METHOD_INTERFACE) {
3980 Fail(VERIFY_ERROR_CLASS_CHANGE)
David Sehr709b0702016-10-13 09:12:37 -07003981 << "interface method " << dex_file_->PrettyMethod(dex_method_idx)
3982 << " is in a non-interface class " << klass->PrettyClass();
Andreas Gampe42ef8ab2015-12-03 17:27:32 -08003983 return nullptr;
3984 }
3985 }
3986
Alex Lightafb66472017-08-01 09:54:49 -07003987 // Check specifically for non-public object methods being provided for interface dispatch. This
3988 // can occur if we failed to find a method with FindInterfaceMethod but later find one with
3989 // FindClassMethod for error message use.
3990 if (method_type == METHOD_INTERFACE &&
3991 res_method->GetDeclaringClass()->IsObjectClass() &&
3992 !res_method->IsPublic()) {
3993 Fail(VERIFY_ERROR_NO_METHOD) << "invoke-interface " << klass->PrettyDescriptor() << "."
3994 << dex_file_->GetMethodName(method_id) << " "
3995 << dex_file_->GetMethodSignature(method_id) << " resolved to "
3996 << "non-public object method " << res_method->PrettyMethod() << " "
3997 << "but non-public Object methods are excluded from interface "
3998 << "method resolution.";
3999 return nullptr;
4000 }
jeffhao8cd6dda2012-02-22 10:15:34 -08004001 // Check if access is allowed.
Ian Rogersad0b3a32012-04-16 14:50:24 -07004002 if (!referrer.CanAccessMember(res_method->GetDeclaringClass(), res_method->GetAccessFlags())) {
David Sehr709b0702016-10-13 09:12:37 -07004003 Fail(VERIFY_ERROR_ACCESS_METHOD) << "illegal method access (call "
4004 << res_method->PrettyMethod()
Ian Rogersad0b3a32012-04-16 14:50:24 -07004005 << " from " << referrer << ")";
jeffhaob57e9522012-04-26 18:08:21 -07004006 return res_method;
jeffhao8cd6dda2012-02-22 10:15:34 -08004007 }
jeffhaode0d9c92012-02-27 13:58:13 -08004008 // Check that invoke-virtual and invoke-super are not used on private methods of the same class.
Alex Light7268d472016-01-20 15:50:01 -08004009 if (res_method->IsPrivate() && (method_type == METHOD_VIRTUAL || method_type == METHOD_SUPER)) {
jeffhaod5347e02012-03-22 17:25:05 -07004010 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invoke-super/virtual can't be used on private method "
David Sehr709b0702016-10-13 09:12:37 -07004011 << res_method->PrettyMethod();
Ian Rogers7b078e82014-09-10 14:44:24 -07004012 return nullptr;
jeffhaode0d9c92012-02-27 13:58:13 -08004013 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004014 // See if the method type implied by the invoke instruction matches the access flags for the
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004015 // target method. The flags for METHOD_POLYMORPHIC are based on there being precisely two
4016 // signature polymorphic methods supported by the run-time which are native methods with variable
4017 // arguments.
Brian Carlstrombe6fa5e2014-12-09 20:15:42 -08004018 if ((method_type == METHOD_DIRECT && (!res_method->IsDirect() || res_method->IsStatic())) ||
Ian Rogersd81871c2011-10-03 13:57:23 -07004019 (method_type == METHOD_STATIC && !res_method->IsStatic()) ||
Alex Light7268d472016-01-20 15:50:01 -08004020 ((method_type == METHOD_SUPER ||
4021 method_type == METHOD_VIRTUAL ||
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004022 method_type == METHOD_INTERFACE) && res_method->IsDirect()) ||
4023 ((method_type == METHOD_POLYMORPHIC) &&
4024 (!res_method->IsNative() || !res_method->IsVarargs()))) {
Ian Rogers2fc14272012-08-30 10:56:57 -07004025 Fail(VERIFY_ERROR_CLASS_CHANGE) << "invoke type (" << method_type << ") does not match method "
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004026 "type of " << res_method->PrettyMethod();
Ian Rogers7b078e82014-09-10 14:44:24 -07004027 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07004028 }
Alex Lightafb66472017-08-01 09:54:49 -07004029 // Make sure we weren't expecting to fail.
4030 DCHECK(!must_fail) << "invoke type (" << method_type << ")"
4031 << klass->PrettyDescriptor() << "."
4032 << dex_file_->GetMethodName(method_id) << " "
4033 << dex_file_->GetMethodSignature(method_id) << " unexpectedly resolved to "
4034 << res_method->PrettyMethod() << " without error. Initially this method was "
4035 << "not found so we were expecting to fail for some reason.";
jeffhao8cd6dda2012-02-22 10:15:34 -08004036 return res_method;
4037}
4038
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004039template <class T>
Mathieu Chartiere401d142015-04-22 13:56:20 -07004040ArtMethod* MethodVerifier::VerifyInvocationArgsFromIterator(
4041 T* it, const Instruction* inst, MethodType method_type, bool is_range, ArtMethod* res_method) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004042 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
4043 // match the call to the signature. Also, we might be calling through an abstract method
4044 // definition (which doesn't have register count values).
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004045 const size_t expected_args = inst->VRegA();
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004046 /* caught by static verifier */
4047 DCHECK(is_range || expected_args <= 5);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004048
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08004049 if (expected_args > code_item_accessor_.OutsSize()) {
Orion Hodson1cda7c22017-08-10 13:06:45 +01004050 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08004051 << ") exceeds outsSize ("
4052 << code_item_accessor_.OutsSize() << ")";
Orion Hodson1cda7c22017-08-10 13:06:45 +01004053 return nullptr;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004054 }
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004055
4056 /*
4057 * Check the "this" argument, which must be an instance of the class that declared the method.
4058 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
4059 * rigorous check here (which is okay since we have to do it at runtime).
4060 */
4061 if (method_type != METHOD_STATIC) {
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004062 const RegType& actual_arg_type = work_line_->GetInvocationThis(this, inst);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004063 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
4064 CHECK(have_pending_hard_failure_);
4065 return nullptr;
4066 }
David Brazdil68b5c0b2016-01-19 14:25:29 +00004067 bool is_init = false;
4068 if (actual_arg_type.IsUninitializedTypes()) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004069 if (res_method) {
4070 if (!res_method->IsConstructor()) {
4071 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
4072 return nullptr;
4073 }
4074 } else {
4075 // Check whether the name of the called method is "<init>"
4076 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Jeff Hao0d087272014-08-04 14:47:17 -07004077 if (strcmp(dex_file_->GetMethodName(dex_file_->GetMethodId(method_idx)), "<init>") != 0) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004078 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
4079 return nullptr;
4080 }
4081 }
David Brazdil68b5c0b2016-01-19 14:25:29 +00004082 is_init = true;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004083 }
David Brazdil68b5c0b2016-01-19 14:25:29 +00004084 const RegType& adjusted_type = is_init
4085 ? GetRegTypeCache()->FromUninitialized(actual_arg_type)
4086 : actual_arg_type;
Andreas Gampeeed3a5d2017-11-29 14:58:34 -08004087 if (method_type != METHOD_INTERFACE && !adjusted_type.IsZeroOrNull()) {
Ian Rogersd8f69b02014-09-10 21:43:52 +00004088 const RegType* res_method_class;
Andreas Gamped9e23012015-06-04 22:19:58 -07004089 // Miranda methods have the declaring interface as their declaring class, not the abstract
4090 // class. It would be wrong to use this for the type check (interface type checks are
4091 // postponed to runtime).
4092 if (res_method != nullptr && !res_method->IsMiranda()) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004093 mirror::Class* klass = res_method->GetDeclaringClass();
Ian Rogers1ff3c982014-08-12 02:30:58 -07004094 std::string temp;
Andreas Gampef23f33d2015-06-23 14:18:17 -07004095 res_method_class = &FromClass(klass->GetDescriptor(&temp), klass,
4096 klass->CannotBeAssignedFromOtherTypes());
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004097 } else {
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004098 const uint32_t method_idx = inst->VRegB();
Andreas Gampea5b09a62016-11-17 15:21:22 -08004099 const dex::TypeIndex class_idx = dex_file_->GetMethodId(method_idx).class_idx_;
Mathieu Chartierde40d472015-10-15 17:47:48 -07004100 res_method_class = &reg_types_.FromDescriptor(
4101 GetClassLoader(),
4102 dex_file_->StringByTypeIdx(class_idx),
4103 false);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004104 }
David Brazdilca3c8c32016-09-06 14:04:48 +01004105 if (!res_method_class->IsAssignableFrom(adjusted_type, this)) {
David Brazdil68b5c0b2016-01-19 14:25:29 +00004106 Fail(adjusted_type.IsUnresolvedTypes()
4107 ? VERIFY_ERROR_NO_CLASS
4108 : VERIFY_ERROR_BAD_CLASS_SOFT)
4109 << "'this' argument '" << actual_arg_type << "' not instance of '"
4110 << *res_method_class << "'";
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004111 // Continue on soft failures. We need to find possible hard failures to avoid problems in
4112 // the compiler.
4113 if (have_pending_hard_failure_) {
4114 return nullptr;
4115 }
4116 }
4117 }
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004118 }
4119
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004120 uint32_t arg[5];
4121 if (!is_range) {
4122 inst->GetVarArgs(arg);
4123 }
4124 uint32_t sig_registers = (method_type == METHOD_STATIC) ? 0 : 1;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004125 for ( ; it->HasNext(); it->Next()) {
4126 if (sig_registers >= expected_args) {
4127 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, expected " << inst->VRegA() <<
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004128 " argument registers, method signature has " << sig_registers + 1 << " or more";
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004129 return nullptr;
4130 }
4131
4132 const char* param_descriptor = it->GetDescriptor();
4133
4134 if (param_descriptor == nullptr) {
4135 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation because of missing signature "
4136 "component";
4137 return nullptr;
4138 }
4139
Ian Rogersd8f69b02014-09-10 21:43:52 +00004140 const RegType& reg_type = reg_types_.FromDescriptor(GetClassLoader(), param_descriptor, false);
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004141 uint32_t get_reg = is_range ? inst->VRegC() + static_cast<uint32_t>(sig_registers) :
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004142 arg[sig_registers];
4143 if (reg_type.IsIntegralTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004144 const RegType& src_type = work_line_->GetRegisterType(this, get_reg);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004145 if (!src_type.IsIntegralTypes()) {
4146 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register v" << get_reg << " has type " << src_type
4147 << " but expected " << reg_type;
Andreas Gampeb588f4c2015-05-26 13:35:39 -07004148 return nullptr;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004149 }
Andreas Gampeda9badb2015-06-05 20:22:12 -07004150 } else {
4151 if (!work_line_->VerifyRegisterType(this, get_reg, reg_type)) {
4152 // Continue on soft failures. We need to find possible hard failures to avoid problems in
4153 // the compiler.
4154 if (have_pending_hard_failure_) {
4155 return nullptr;
4156 }
4157 } else if (reg_type.IsLongOrDoubleTypes()) {
4158 // Check that registers are consecutive (for non-range invokes). Invokes are the only
4159 // instructions not specifying register pairs by the first component, but require them
4160 // nonetheless. Only check when there's an actual register in the parameters. If there's
4161 // none, this will fail below.
4162 if (!is_range && sig_registers + 1 < expected_args) {
4163 uint32_t second_reg = arg[sig_registers + 1];
4164 if (second_reg != get_reg + 1) {
4165 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, long or double parameter "
4166 "at index " << sig_registers << " is not a pair: " << get_reg << " + "
4167 << second_reg << ".";
4168 return nullptr;
4169 }
4170 }
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004171 }
4172 }
4173 sig_registers += reg_type.IsLongOrDoubleTypes() ? 2 : 1;
4174 }
4175 if (expected_args != sig_registers) {
4176 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, expected " << expected_args <<
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004177 " argument registers, method signature has " << sig_registers;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004178 return nullptr;
4179 }
4180 return res_method;
4181}
4182
4183void MethodVerifier::VerifyInvocationArgsUnresolvedMethod(const Instruction* inst,
4184 MethodType method_type,
4185 bool is_range) {
4186 // As the method may not have been resolved, make this static check against what we expect.
4187 // The main reason for this code block is to fail hard when we find an illegal use, e.g.,
4188 // wrong number of arguments or wrong primitive types, even if the method could not be resolved.
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004189 const uint32_t method_idx = inst->VRegB();
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004190 DexFileParameterIterator it(*dex_file_,
4191 dex_file_->GetProtoId(dex_file_->GetMethodId(method_idx).proto_idx_));
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004192 VerifyInvocationArgsFromIterator(&it, inst, method_type, is_range, nullptr);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004193}
4194
Orion Hodsonc069a302017-01-18 09:23:12 +00004195bool MethodVerifier::CheckCallSite(uint32_t call_site_idx) {
Orion Hodson3a842f52017-04-21 15:24:10 +01004196 if (call_site_idx >= dex_file_->NumCallSiteIds()) {
4197 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Bad call site id #" << call_site_idx
4198 << " >= " << dex_file_->NumCallSiteIds();
4199 return false;
4200 }
4201
Orion Hodsonc069a302017-01-18 09:23:12 +00004202 CallSiteArrayValueIterator it(*dex_file_, dex_file_->GetCallSiteId(call_site_idx));
4203 // Check essential arguments are provided. The dex file verifier has verified indicies of the
4204 // main values (method handle, name, method_type).
4205 if (it.Size() < 3) {
4206 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Call site #" << call_site_idx
4207 << " has too few arguments: "
4208 << it.Size() << "< 3";
4209 return false;
4210 }
4211
Orion Hodson3a842f52017-04-21 15:24:10 +01004212 // Get and check the first argument: the method handle (index range
4213 // checked by the dex file verifier).
Orion Hodsonc069a302017-01-18 09:23:12 +00004214 uint32_t method_handle_idx = static_cast<uint32_t>(it.GetJavaValue().i);
4215 it.Next();
Orion Hodson3a842f52017-04-21 15:24:10 +01004216
Orion Hodsonc069a302017-01-18 09:23:12 +00004217 const DexFile::MethodHandleItem& mh = dex_file_->GetMethodHandle(method_handle_idx);
4218 if (mh.method_handle_type_ != static_cast<uint16_t>(DexFile::MethodHandleType::kInvokeStatic)) {
4219 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Call site #" << call_site_idx
Orion Hodson631827d2017-04-10 14:53:47 +01004220 << " argument 0 method handle type is not InvokeStatic: "
4221 << mh.method_handle_type_;
Orion Hodsonc069a302017-01-18 09:23:12 +00004222 return false;
4223 }
4224
4225 // Skip the second argument, the name to resolve, as checked by the
4226 // dex file verifier.
4227 it.Next();
4228
4229 // Skip the third argument, the method type expected, as checked by
4230 // the dex file verifier.
4231 it.Next();
4232
4233 // Check the bootstrap method handle and remaining arguments.
4234 const DexFile::MethodId& method_id = dex_file_->GetMethodId(mh.field_or_method_idx_);
4235 uint32_t length;
4236 const char* shorty = dex_file_->GetMethodShorty(method_id, &length);
4237
4238 if (it.Size() < length - 1) {
4239 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Call site #" << call_site_idx
4240 << " too few arguments for bootstrap method: "
4241 << it.Size() << " < " << (length - 1);
4242 return false;
4243 }
4244
4245 // Check the return type and first 3 arguments are references
4246 // (CallSite, Lookup, String, MethodType). If they are not of the
4247 // expected types (or subtypes), it will trigger a
4248 // WrongMethodTypeException during execution.
4249 if (shorty[0] != 'L') {
4250 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Call site #" << call_site_idx
4251 << " bootstrap return type is not a reference";
4252 return false;
4253 }
4254
4255 for (uint32_t i = 1; i < 4; ++i) {
4256 if (shorty[i] != 'L') {
4257 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Call site #" << call_site_idx
4258 << " bootstrap method argument " << (i - 1)
4259 << " is not a reference";
4260 return false;
4261 }
4262 }
4263
4264 // Check the optional arguments.
4265 for (uint32_t i = 4; i < length; ++i, it.Next()) {
4266 bool match = false;
4267 switch (it.GetValueType()) {
4268 case EncodedArrayValueIterator::ValueType::kBoolean:
4269 case EncodedArrayValueIterator::ValueType::kByte:
4270 case EncodedArrayValueIterator::ValueType::kShort:
4271 case EncodedArrayValueIterator::ValueType::kChar:
4272 case EncodedArrayValueIterator::ValueType::kInt:
4273 // These all fit within one register and encoders do not seem
4274 // too exacting on the encoding type they use (ie using
4275 // integer for all of these).
4276 match = (strchr("ZBCSI", shorty[i]) != nullptr);
4277 break;
4278 case EncodedArrayValueIterator::ValueType::kLong:
4279 match = ('J' == shorty[i]);
4280 break;
4281 case EncodedArrayValueIterator::ValueType::kFloat:
4282 match = ('F' == shorty[i]);
4283 break;
4284 case EncodedArrayValueIterator::ValueType::kDouble:
4285 match = ('D' == shorty[i]);
4286 break;
4287 case EncodedArrayValueIterator::ValueType::kMethodType:
4288 case EncodedArrayValueIterator::ValueType::kMethodHandle:
4289 case EncodedArrayValueIterator::ValueType::kString:
4290 case EncodedArrayValueIterator::ValueType::kType:
4291 case EncodedArrayValueIterator::ValueType::kNull:
4292 match = ('L' == shorty[i]);
4293 break;
4294 case EncodedArrayValueIterator::ValueType::kField:
4295 case EncodedArrayValueIterator::ValueType::kMethod:
4296 case EncodedArrayValueIterator::ValueType::kEnum:
4297 case EncodedArrayValueIterator::ValueType::kArray:
4298 case EncodedArrayValueIterator::ValueType::kAnnotation:
4299 // Unreachable based on current EncodedArrayValueIterator::Next().
4300 UNREACHABLE();
4301 }
4302
4303 if (!match) {
4304 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Call site #" << call_site_idx
4305 << " bootstrap method argument " << (i - 1)
4306 << " expected " << shorty[i]
4307 << " got value type: " << it.GetValueType();
4308 return false;
4309 }
4310 }
4311 return true;
4312}
4313
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004314class MethodParamListDescriptorIterator {
4315 public:
Mathieu Chartiere401d142015-04-22 13:56:20 -07004316 explicit MethodParamListDescriptorIterator(ArtMethod* res_method) :
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004317 res_method_(res_method), pos_(0), params_(res_method->GetParameterTypeList()),
4318 params_size_(params_ == nullptr ? 0 : params_->Size()) {
4319 }
4320
4321 bool HasNext() {
4322 return pos_ < params_size_;
4323 }
4324
4325 void Next() {
4326 ++pos_;
4327 }
4328
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07004329 const char* GetDescriptor() REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004330 return res_method_->GetTypeDescriptorFromTypeIdx(params_->GetTypeItem(pos_).type_idx_);
4331 }
4332
4333 private:
Mathieu Chartiere401d142015-04-22 13:56:20 -07004334 ArtMethod* res_method_;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004335 size_t pos_;
4336 const DexFile::TypeList* params_;
4337 const size_t params_size_;
4338};
4339
Mathieu Chartiere401d142015-04-22 13:56:20 -07004340ArtMethod* MethodVerifier::VerifyInvocationArgs(
Alex Light7268d472016-01-20 15:50:01 -08004341 const Instruction* inst, MethodType method_type, bool is_range) {
jeffhao8cd6dda2012-02-22 10:15:34 -08004342 // Resolve the method. This could be an abstract or concrete method depending on what sort of call
4343 // we're making.
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004344 const uint32_t method_idx = inst->VRegB();
Alex Light7268d472016-01-20 15:50:01 -08004345 ArtMethod* res_method = ResolveMethodAndCheckAccess(method_idx, method_type);
Ian Rogers7b078e82014-09-10 14:44:24 -07004346 if (res_method == nullptr) { // error or class is unresolved
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004347 // Check what we can statically.
4348 if (!have_pending_hard_failure_) {
4349 VerifyInvocationArgsUnresolvedMethod(inst, method_type, is_range);
4350 }
4351 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08004352 }
4353
Ian Rogersd81871c2011-10-03 13:57:23 -07004354 // If we're using invoke-super(method), make sure that the executing method's class' superclass
Alex Light705ad492015-09-21 11:36:30 -07004355 // has a vtable entry for the target method. Or the target is on a interface.
Alex Light7268d472016-01-20 15:50:01 -08004356 if (method_type == METHOD_SUPER) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08004357 dex::TypeIndex class_idx = dex_file_->GetMethodId(method_idx).class_idx_;
David Brazdilca3c8c32016-09-06 14:04:48 +01004358 const RegType& reference_type = reg_types_.FromDescriptor(
4359 GetClassLoader(),
4360 dex_file_->StringByTypeIdx(class_idx),
4361 false);
4362 if (reference_type.IsUnresolvedTypes()) {
Alex Lightfedd91d2016-01-07 14:49:16 -08004363 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "Unable to find referenced class from invoke-super";
4364 return nullptr;
4365 }
David Brazdilca3c8c32016-09-06 14:04:48 +01004366 if (reference_type.GetClass()->IsInterface()) {
Alex Lightfedd91d2016-01-07 14:49:16 -08004367 // TODO Can we verify anything else.
David Brazdil15fc7292016-09-02 14:13:18 +01004368 if (class_idx == class_def_.class_idx_) {
Alex Lightfedd91d2016-01-07 14:49:16 -08004369 Fail(VERIFY_ERROR_CLASS_CHANGE) << "Cannot invoke-super on self as interface";
Alex Light55ea94d2016-03-15 09:50:26 -07004370 return nullptr;
Alex Lightfedd91d2016-01-07 14:49:16 -08004371 }
4372 // TODO Revisit whether we want to allow invoke-super on direct interfaces only like the JLS
4373 // does.
Alex Light55ea94d2016-03-15 09:50:26 -07004374 if (!GetDeclaringClass().HasClass()) {
4375 Fail(VERIFY_ERROR_NO_CLASS) << "Unable to resolve the full class of 'this' used in an"
4376 << "interface invoke-super";
4377 return nullptr;
David Brazdilca3c8c32016-09-06 14:04:48 +01004378 } else if (!reference_type.IsStrictlyAssignableFrom(GetDeclaringClass(), this)) {
Alex Lightfedd91d2016-01-07 14:49:16 -08004379 Fail(VERIFY_ERROR_CLASS_CHANGE)
David Sehr709b0702016-10-13 09:12:37 -07004380 << "invoke-super in " << mirror::Class::PrettyClass(GetDeclaringClass().GetClass())
4381 << " in method "
4382 << dex_file_->PrettyMethod(dex_method_idx_) << " to method "
4383 << dex_file_->PrettyMethod(method_idx) << " references "
4384 << "non-super-interface type " << mirror::Class::PrettyClass(reference_type.GetClass());
Alex Lightfedd91d2016-01-07 14:49:16 -08004385 return nullptr;
Alex Light705ad492015-09-21 11:36:30 -07004386 }
4387 } else {
4388 const RegType& super = GetDeclaringClass().GetSuperClass(&reg_types_);
4389 if (super.IsUnresolvedTypes()) {
4390 Fail(VERIFY_ERROR_NO_METHOD) << "unknown super class in invoke-super from "
David Sehr709b0702016-10-13 09:12:37 -07004391 << dex_file_->PrettyMethod(dex_method_idx_)
4392 << " to super " << res_method->PrettyMethod();
Alex Light705ad492015-09-21 11:36:30 -07004393 return nullptr;
4394 }
David Brazdilca3c8c32016-09-06 14:04:48 +01004395 if (!reference_type.IsStrictlyAssignableFrom(GetDeclaringClass(), this) ||
Aart Bikf663e342016-04-04 17:28:59 -07004396 (res_method->GetMethodIndex() >= super.GetClass()->GetVTableLength())) {
Alex Light705ad492015-09-21 11:36:30 -07004397 Fail(VERIFY_ERROR_NO_METHOD) << "invalid invoke-super from "
David Sehr709b0702016-10-13 09:12:37 -07004398 << dex_file_->PrettyMethod(dex_method_idx_)
Alex Light705ad492015-09-21 11:36:30 -07004399 << " to super " << super
4400 << "." << res_method->GetName()
4401 << res_method->GetSignature();
4402 return nullptr;
4403 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004404 }
4405 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08004406
Andreas Gampe74979b12017-05-16 09:28:06 -07004407 if (UNLIKELY(method_type == METHOD_POLYMORPHIC)) {
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004408 // Process the signature of the calling site that is invoking the method handle.
4409 DexFileParameterIterator it(*dex_file_, dex_file_->GetProtoId(inst->VRegH()));
4410 return VerifyInvocationArgsFromIterator(&it, inst, method_type, is_range, res_method);
4411 } else {
4412 // Process the target method's signature.
4413 MethodParamListDescriptorIterator it(res_method);
4414 return VerifyInvocationArgsFromIterator(&it, inst, method_type, is_range, res_method);
4415 }
4416}
4417
4418bool MethodVerifier::CheckSignaturePolymorphicMethod(ArtMethod* method) {
4419 mirror::Class* klass = method->GetDeclaringClass();
Orion Hodsonfe92d122018-01-02 10:45:17 +00004420 const char* method_name = method->GetName();
4421
4422 const char* expected_return_descriptor;
4423 if (klass == mirror::MethodHandle::StaticClass()) {
4424 expected_return_descriptor = mirror::MethodHandle::GetReturnTypeDescriptor(method_name);
4425 } else if (klass == mirror::VarHandle::StaticClass()) {
4426 expected_return_descriptor = mirror::VarHandle::GetReturnTypeDescriptor(method_name);
Orion Hodson928033d2018-02-07 05:30:54 +00004427 // TODO: add compiler support for VarHandle accessor methods (b/71781600)
4428 Fail(VERIFY_ERROR_FORCE_INTERPRETER);
Orion Hodsonfe92d122018-01-02 10:45:17 +00004429 } else {
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004430 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
Orion Hodsonfe92d122018-01-02 10:45:17 +00004431 << "Signature polymorphic method in unsuppported class: " << klass->PrettyDescriptor();
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004432 return false;
4433 }
4434
Orion Hodsonfe92d122018-01-02 10:45:17 +00004435 if (expected_return_descriptor == nullptr) {
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004436 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
4437 << "Signature polymorphic method name invalid: " << method_name;
4438 return false;
4439 }
4440
4441 const DexFile::TypeList* types = method->GetParameterTypeList();
4442 if (types->Size() != 1) {
4443 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
4444 << "Signature polymorphic method has too many arguments " << types->Size() << " != 1";
4445 return false;
4446 }
4447
4448 const dex::TypeIndex argument_type_index = types->GetTypeItem(0).type_idx_;
4449 const char* argument_descriptor = method->GetTypeDescriptorFromTypeIdx(argument_type_index);
4450 if (strcmp(argument_descriptor, "[Ljava/lang/Object;") != 0) {
4451 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
4452 << "Signature polymorphic method has unexpected argument type: " << argument_descriptor;
4453 return false;
4454 }
4455
4456 const char* return_descriptor = method->GetReturnTypeDescriptor();
Orion Hodsonfe92d122018-01-02 10:45:17 +00004457 if (strcmp(return_descriptor, expected_return_descriptor) != 0) {
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004458 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
Orion Hodsonfe92d122018-01-02 10:45:17 +00004459 << "Signature polymorphic method has unexpected return type: " << return_descriptor
4460 << " != " << expected_return_descriptor;
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004461 return false;
4462 }
4463
4464 return true;
4465}
4466
4467bool MethodVerifier::CheckSignaturePolymorphicReceiver(const Instruction* inst) {
4468 const RegType& this_type = work_line_->GetInvocationThis(this, inst);
Andreas Gampeeed3a5d2017-11-29 14:58:34 -08004469 if (this_type.IsZeroOrNull()) {
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004470 /* null pointer always passes (and always fails at run time) */
4471 return true;
4472 } else if (!this_type.IsNonZeroReferenceTypes()) {
4473 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
4474 << "invoke-polymorphic receiver is not a reference: "
4475 << this_type;
4476 return false;
4477 } else if (this_type.IsUninitializedReference()) {
4478 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
4479 << "invoke-polymorphic receiver is uninitialized: "
4480 << this_type;
4481 return false;
4482 } else if (!this_type.HasClass()) {
4483 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
4484 << "invoke-polymorphic receiver has no class: "
4485 << this_type;
4486 return false;
Orion Hodsonfe92d122018-01-02 10:45:17 +00004487 } else if (!this_type.GetClass()->IsSubClass(mirror::MethodHandle::StaticClass()) &&
4488 !this_type.GetClass()->IsSubClass(mirror::VarHandle::StaticClass())) {
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004489 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
Orion Hodsonfe92d122018-01-02 10:45:17 +00004490 << "invoke-polymorphic receiver is not a subclass of MethodHandle or VarHandle: "
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004491 << this_type;
4492 return false;
4493 }
4494 return true;
Ian Rogersd81871c2011-10-03 13:57:23 -07004495}
4496
Mathieu Chartiere401d142015-04-22 13:56:20 -07004497ArtMethod* MethodVerifier::GetQuickInvokedMethod(const Instruction* inst, RegisterLine* reg_line,
4498 bool is_range, bool allow_failure) {
Mathieu Chartier091d2382015-03-06 10:59:06 -08004499 if (is_range) {
4500 DCHECK_EQ(inst->Opcode(), Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
4501 } else {
4502 DCHECK_EQ(inst->Opcode(), Instruction::INVOKE_VIRTUAL_QUICK);
4503 }
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004504 const RegType& actual_arg_type = reg_line->GetInvocationThis(this, inst, allow_failure);
Ian Rogers9bc54402014-04-17 16:40:01 -07004505 if (!actual_arg_type.HasClass()) {
4506 VLOG(verifier) << "Failed to get mirror::Class* from '" << actual_arg_type << "'";
Andreas Gampe63981562014-04-17 12:28:43 -07004507 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004508 }
Ian Rogersa4cf1df2014-05-07 19:47:17 -07004509 mirror::Class* klass = actual_arg_type.GetClass();
Mingyao Yang2cdbad72014-07-16 10:44:41 -07004510 mirror::Class* dispatch_class;
Ian Rogersa4cf1df2014-05-07 19:47:17 -07004511 if (klass->IsInterface()) {
4512 // Derive Object.class from Class.class.getSuperclass().
4513 mirror::Class* object_klass = klass->GetClass()->GetSuperClass();
Andreas Gampe7c038102014-10-27 20:08:46 -07004514 if (FailOrAbort(this, object_klass->IsObjectClass(),
Mathieu Chartier091d2382015-03-06 10:59:06 -08004515 "Failed to find Object class in quickened invoke receiver", work_insn_idx_)) {
Andreas Gampe7c038102014-10-27 20:08:46 -07004516 return nullptr;
4517 }
Mingyao Yang2cdbad72014-07-16 10:44:41 -07004518 dispatch_class = object_klass;
Ian Rogersa4cf1df2014-05-07 19:47:17 -07004519 } else {
Mingyao Yang2cdbad72014-07-16 10:44:41 -07004520 dispatch_class = klass;
Ian Rogersa4cf1df2014-05-07 19:47:17 -07004521 }
Mathieu Chartier091d2382015-03-06 10:59:06 -08004522 if (!dispatch_class->HasVTable()) {
4523 FailOrAbort(this, allow_failure, "Receiver class has no vtable for quickened invoke at ",
4524 work_insn_idx_);
Andreas Gampe7c038102014-10-27 20:08:46 -07004525 return nullptr;
4526 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004527 uint16_t vtable_index = is_range ? inst->VRegB_3rc() : inst->VRegB_35c();
Mathieu Chartiere401d142015-04-22 13:56:20 -07004528 auto* cl = Runtime::Current()->GetClassLinker();
4529 auto pointer_size = cl->GetImagePointerSize();
Mathieu Chartier091d2382015-03-06 10:59:06 -08004530 if (static_cast<int32_t>(vtable_index) >= dispatch_class->GetVTableLength()) {
4531 FailOrAbort(this, allow_failure,
4532 "Receiver class has not enough vtable slots for quickened invoke at ",
4533 work_insn_idx_);
Andreas Gampe7c038102014-10-27 20:08:46 -07004534 return nullptr;
4535 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07004536 ArtMethod* res_method = dispatch_class->GetVTableEntry(vtable_index, pointer_size);
Mathieu Chartier091d2382015-03-06 10:59:06 -08004537 if (self_->IsExceptionPending()) {
4538 FailOrAbort(this, allow_failure, "Unexpected exception pending for quickened invoke at ",
4539 work_insn_idx_);
Andreas Gampe7c038102014-10-27 20:08:46 -07004540 return nullptr;
4541 }
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004542 return res_method;
4543}
4544
Mathieu Chartiere401d142015-04-22 13:56:20 -07004545ArtMethod* MethodVerifier::VerifyInvokeVirtualQuickArgs(const Instruction* inst, bool is_range) {
Andreas Gampe76bd8802014-12-10 16:43:58 -08004546 DCHECK(Runtime::Current()->IsStarted() || verify_to_dump_)
David Sehr709b0702016-10-13 09:12:37 -07004547 << dex_file_->PrettyMethod(dex_method_idx_, true) << "@" << work_insn_idx_;
Andreas Gampe76bd8802014-12-10 16:43:58 -08004548
Mathieu Chartiere401d142015-04-22 13:56:20 -07004549 ArtMethod* res_method = GetQuickInvokedMethod(inst, work_line_.get(), is_range, false);
Ian Rogers7b078e82014-09-10 14:44:24 -07004550 if (res_method == nullptr) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004551 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer method from " << inst->Name();
Ian Rogers7b078e82014-09-10 14:44:24 -07004552 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004553 }
Andreas Gampe7c038102014-10-27 20:08:46 -07004554 if (FailOrAbort(this, !res_method->IsDirect(), "Quick-invoked method is direct at ",
4555 work_insn_idx_)) {
4556 return nullptr;
4557 }
4558 if (FailOrAbort(this, !res_method->IsStatic(), "Quick-invoked method is static at ",
4559 work_insn_idx_)) {
4560 return nullptr;
4561 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004562
4563 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
4564 // match the call to the signature. Also, we might be calling through an abstract method
4565 // definition (which doesn't have register count values).
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004566 const RegType& actual_arg_type = work_line_->GetInvocationThis(this, inst);
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004567 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
Ian Rogers7b078e82014-09-10 14:44:24 -07004568 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004569 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004570 const size_t expected_args = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
4571 /* caught by static verifier */
4572 DCHECK(is_range || expected_args <= 5);
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08004573 if (expected_args > code_item_accessor_.OutsSize()) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004574 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08004575 << ") exceeds outsSize (" << code_item_accessor_.OutsSize() << ")";
Ian Rogers7b078e82014-09-10 14:44:24 -07004576 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004577 }
4578
4579 /*
4580 * Check the "this" argument, which must be an instance of the class that declared the method.
4581 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
4582 * rigorous check here (which is okay since we have to do it at runtime).
4583 */
David Brazdil68b5c0b2016-01-19 14:25:29 +00004584 // Note: given an uninitialized type, this should always fail. Constructors aren't virtual.
4585 if (actual_arg_type.IsUninitializedTypes() && !res_method->IsConstructor()) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004586 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
Ian Rogers7b078e82014-09-10 14:44:24 -07004587 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004588 }
Andreas Gampeeed3a5d2017-11-29 14:58:34 -08004589 if (!actual_arg_type.IsZeroOrNull()) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004590 mirror::Class* klass = res_method->GetDeclaringClass();
Ian Rogers1ff3c982014-08-12 02:30:58 -07004591 std::string temp;
Ian Rogersd8f69b02014-09-10 21:43:52 +00004592 const RegType& res_method_class =
Andreas Gampef23f33d2015-06-23 14:18:17 -07004593 FromClass(klass->GetDescriptor(&temp), klass, klass->CannotBeAssignedFromOtherTypes());
David Brazdilca3c8c32016-09-06 14:04:48 +01004594 if (!res_method_class.IsAssignableFrom(actual_arg_type, this)) {
David Brazdil68b5c0b2016-01-19 14:25:29 +00004595 Fail(actual_arg_type.IsUninitializedTypes() // Just overcautious - should have never
4596 ? VERIFY_ERROR_BAD_CLASS_HARD // quickened this.
4597 : actual_arg_type.IsUnresolvedTypes()
4598 ? VERIFY_ERROR_NO_CLASS
4599 : VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004600 << "' not instance of '" << res_method_class << "'";
Ian Rogers7b078e82014-09-10 14:44:24 -07004601 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004602 }
4603 }
4604 /*
4605 * Process the target method's signature. This signature may or may not
4606 * have been verified, so we can't assume it's properly formed.
4607 */
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07004608 const DexFile::TypeList* params = res_method->GetParameterTypeList();
Ian Rogers7b078e82014-09-10 14:44:24 -07004609 size_t params_size = params == nullptr ? 0 : params->Size();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004610 uint32_t arg[5];
4611 if (!is_range) {
Ian Rogers29a26482014-05-02 15:27:29 -07004612 inst->GetVarArgs(arg);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004613 }
4614 size_t actual_args = 1;
4615 for (size_t param_index = 0; param_index < params_size; param_index++) {
4616 if (actual_args >= expected_args) {
David Sehr709b0702016-10-13 09:12:37 -07004617 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invalid call to '"
4618 << res_method->PrettyMethod()
Brian Carlstrom93c33962013-07-26 10:37:43 -07004619 << "'. Expected " << expected_args
4620 << " arguments, processing argument " << actual_args
4621 << " (where longs/doubles count twice).";
Ian Rogers7b078e82014-09-10 14:44:24 -07004622 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004623 }
4624 const char* descriptor =
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07004625 res_method->GetTypeDescriptorFromTypeIdx(params->GetTypeItem(param_index).type_idx_);
Ian Rogers7b078e82014-09-10 14:44:24 -07004626 if (descriptor == nullptr) {
David Sehr709b0702016-10-13 09:12:37 -07004627 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of "
4628 << res_method->PrettyMethod()
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004629 << " missing signature component";
Ian Rogers7b078e82014-09-10 14:44:24 -07004630 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004631 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00004632 const RegType& reg_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004633 uint32_t get_reg = is_range ? inst->VRegC_3rc() + actual_args : arg[actual_args];
Ian Rogers7b078e82014-09-10 14:44:24 -07004634 if (!work_line_->VerifyRegisterType(this, get_reg, reg_type)) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004635 return res_method;
4636 }
4637 actual_args = reg_type.IsLongOrDoubleTypes() ? actual_args + 2 : actual_args + 1;
4638 }
4639 if (actual_args != expected_args) {
David Sehr709b0702016-10-13 09:12:37 -07004640 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of "
4641 << res_method->PrettyMethod() << " expected "
4642 << expected_args << " arguments, found " << actual_args;
Ian Rogers7b078e82014-09-10 14:44:24 -07004643 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004644 } else {
4645 return res_method;
4646 }
4647}
4648
Ian Rogers62342ec2013-06-11 10:26:37 -07004649void MethodVerifier::VerifyNewArray(const Instruction* inst, bool is_filled, bool is_range) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08004650 dex::TypeIndex type_idx;
Sebastien Hertz5243e912013-05-21 10:55:07 +02004651 if (!is_filled) {
4652 DCHECK_EQ(inst->Opcode(), Instruction::NEW_ARRAY);
Andreas Gampea5b09a62016-11-17 15:21:22 -08004653 type_idx = dex::TypeIndex(inst->VRegC_22c());
Sebastien Hertz5243e912013-05-21 10:55:07 +02004654 } else if (!is_range) {
4655 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY);
Andreas Gampea5b09a62016-11-17 15:21:22 -08004656 type_idx = dex::TypeIndex(inst->VRegB_35c());
Sebastien Hertz5243e912013-05-21 10:55:07 +02004657 } else {
4658 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY_RANGE);
Andreas Gampea5b09a62016-11-17 15:21:22 -08004659 type_idx = dex::TypeIndex(inst->VRegB_3rc());
Sebastien Hertz5243e912013-05-21 10:55:07 +02004660 }
Andreas Gampe98be1a92017-08-28 08:25:45 -07004661 const RegType& res_type = ResolveClass<CheckAccess::kYes>(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004662 if (res_type.IsConflict()) { // bad class
4663 DCHECK_NE(failures_.size(), 0U);
Ian Rogers0c4a5062012-02-03 15:18:59 -08004664 } else {
4665 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
4666 if (!res_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07004667 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "new-array on non-array class " << res_type;
Ian Rogers0c4a5062012-02-03 15:18:59 -08004668 } else if (!is_filled) {
4669 /* make sure "size" register is valid type */
Ian Rogers7b078e82014-09-10 14:44:24 -07004670 work_line_->VerifyRegisterType(this, inst->VRegB_22c(), reg_types_.Integer());
Ian Rogers0c4a5062012-02-03 15:18:59 -08004671 /* set register type to array class */
Ian Rogersd8f69b02014-09-10 21:43:52 +00004672 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
Andreas Gampead238ce2015-08-24 21:13:08 -07004673 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_22c(), precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08004674 } else {
Andreas Gampebb18a032016-03-22 20:34:25 -07004675 DCHECK(!res_type.IsUnresolvedMergedReference());
Ian Rogers0c4a5062012-02-03 15:18:59 -08004676 // Verify each register. If "arg_count" is bad, VerifyRegisterType() will run off the end of
4677 // the list and fail. It's legal, if silly, for arg_count to be zero.
Ian Rogersd8f69b02014-09-10 21:43:52 +00004678 const RegType& expected_type = reg_types_.GetComponentType(res_type, GetClassLoader());
Sebastien Hertz5243e912013-05-21 10:55:07 +02004679 uint32_t arg_count = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
4680 uint32_t arg[5];
4681 if (!is_range) {
Ian Rogers29a26482014-05-02 15:27:29 -07004682 inst->GetVarArgs(arg);
Sebastien Hertz5243e912013-05-21 10:55:07 +02004683 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08004684 for (size_t ui = 0; ui < arg_count; ui++) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02004685 uint32_t get_reg = is_range ? inst->VRegC_3rc() + ui : arg[ui];
Ian Rogers7b078e82014-09-10 14:44:24 -07004686 if (!work_line_->VerifyRegisterType(this, get_reg, expected_type)) {
4687 work_line_->SetResultRegisterType(this, reg_types_.Conflict());
Ian Rogers0c4a5062012-02-03 15:18:59 -08004688 return;
4689 }
4690 }
4691 // filled-array result goes into "result" register
Ian Rogersd8f69b02014-09-10 21:43:52 +00004692 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
Ian Rogers7b078e82014-09-10 14:44:24 -07004693 work_line_->SetResultRegisterType(this, precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08004694 }
4695 }
4696}
4697
Sebastien Hertz5243e912013-05-21 10:55:07 +02004698void MethodVerifier::VerifyAGet(const Instruction* inst,
Ian Rogersd8f69b02014-09-10 21:43:52 +00004699 const RegType& insn_type, bool is_primitive) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004700 const RegType& index_type = work_line_->GetRegisterType(this, inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07004701 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07004702 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07004703 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004704 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegB_23x());
Andreas Gampeeed3a5d2017-11-29 14:58:34 -08004705 if (array_type.IsZeroOrNull()) {
Ian Rogers89310de2012-02-01 13:47:30 -08004706 // Null array class; this code path will fail at runtime. Infer a merge-able type from the
Andreas Gampe52f205a2017-12-01 12:16:07 -08004707 // instruction type.
4708 if (!is_primitive) {
4709 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), reg_types_.Null());
4710 } else if (insn_type.IsInteger()) {
4711 // Pick a non-zero constant (to distinguish with null) that can fit in any primitive.
4712 // We cannot use 'insn_type' as it could be a float array or an int array.
4713 work_line_->SetRegisterType<LockOp::kClear>(
4714 this, inst->VRegA_23x(), DetermineCat1Constant(1, need_precise_constants_));
4715 } else if (insn_type.IsCategory1Types()) {
4716 // Category 1
4717 // The 'insn_type' is exactly the type we need.
4718 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), insn_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07004719 } else {
Ian Rogers89310de2012-02-01 13:47:30 -08004720 // Category 2
Ian Rogers7b078e82014-09-10 14:44:24 -07004721 work_line_->SetRegisterTypeWide(this, inst->VRegA_23x(),
4722 reg_types_.FromCat2ConstLo(0, false),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004723 reg_types_.FromCat2ConstHi(0, false));
Ian Rogers89310de2012-02-01 13:47:30 -08004724 }
jeffhaofc3144e2012-02-01 17:21:15 -08004725 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07004726 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aget";
Andreas Gampe8d8fc482016-03-25 16:24:20 -07004727 } else if (array_type.IsUnresolvedMergedReference()) {
Andreas Gampebb18a032016-03-22 20:34:25 -07004728 // Unresolved array types must be reference array types.
4729 if (is_primitive) {
4730 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type
4731 << " source for category 1 aget";
4732 } else {
4733 Fail(VERIFY_ERROR_NO_CLASS) << "cannot verify aget for " << array_type
4734 << " because of missing class";
Andreas Gampe8d8fc482016-03-25 16:24:20 -07004735 // Approximate with java.lang.Object[].
4736 work_line_->SetRegisterType<LockOp::kClear>(this,
4737 inst->VRegA_23x(),
4738 reg_types_.JavaLangObject(false));
Andreas Gampebb18a032016-03-22 20:34:25 -07004739 }
Ian Rogers89310de2012-02-01 13:47:30 -08004740 } else {
4741 /* verify the class */
Ian Rogersd8f69b02014-09-10 21:43:52 +00004742 const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
jeffhaofc3144e2012-02-01 17:21:15 -08004743 if (!component_type.IsReferenceTypes() && !is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07004744 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08004745 << " source for aget-object";
4746 } else if (component_type.IsNonZeroReferenceTypes() && is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07004747 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08004748 << " source for category 1 aget";
4749 } else if (is_primitive && !insn_type.Equals(component_type) &&
4750 !((insn_type.IsInteger() && component_type.IsFloat()) ||
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004751 (insn_type.IsLong() && component_type.IsDouble()))) {
4752 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array type " << array_type
4753 << " incompatible with aget of type " << insn_type;
Ian Rogers89310de2012-02-01 13:47:30 -08004754 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004755 // Use knowledge of the field type which is stronger than the type inferred from the
4756 // instruction, which can't differentiate object types and ints from floats, longs from
4757 // doubles.
4758 if (!component_type.IsLowHalf()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07004759 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), component_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004760 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004761 work_line_->SetRegisterTypeWide(this, inst->VRegA_23x(), component_type,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004762 component_type.HighHalf(&reg_types_));
4763 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004764 }
4765 }
4766 }
4767}
4768
Ian Rogersd8f69b02014-09-10 21:43:52 +00004769void MethodVerifier::VerifyPrimitivePut(const RegType& target_type, const RegType& insn_type,
Jeff Haofe1f7c82013-08-01 14:50:24 -07004770 const uint32_t vregA) {
4771 // Primitive assignability rules are weaker than regular assignability rules.
4772 bool instruction_compatible;
4773 bool value_compatible;
Ian Rogers7b078e82014-09-10 14:44:24 -07004774 const RegType& value_type = work_line_->GetRegisterType(this, vregA);
Jeff Haofe1f7c82013-08-01 14:50:24 -07004775 if (target_type.IsIntegralTypes()) {
Jeff Haoa4647482013-08-06 15:35:47 -07004776 instruction_compatible = target_type.Equals(insn_type);
Jeff Haofe1f7c82013-08-01 14:50:24 -07004777 value_compatible = value_type.IsIntegralTypes();
4778 } else if (target_type.IsFloat()) {
4779 instruction_compatible = insn_type.IsInteger(); // no put-float, so expect put-int
4780 value_compatible = value_type.IsFloatTypes();
4781 } else if (target_type.IsLong()) {
4782 instruction_compatible = insn_type.IsLong();
Andreas Gampe376fa682014-09-07 13:06:12 -07004783 // Additional register check: this is not checked statically (as part of VerifyInstructions),
4784 // as target_type depends on the resolved type of the field.
4785 if (instruction_compatible && work_line_->NumRegs() > vregA + 1) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004786 const RegType& value_type_hi = work_line_->GetRegisterType(this, vregA + 1);
Andreas Gampe376fa682014-09-07 13:06:12 -07004787 value_compatible = value_type.IsLongTypes() && value_type.CheckWidePair(value_type_hi);
4788 } else {
4789 value_compatible = false;
4790 }
Jeff Haofe1f7c82013-08-01 14:50:24 -07004791 } else if (target_type.IsDouble()) {
4792 instruction_compatible = insn_type.IsLong(); // no put-double, so expect put-long
Andreas Gampe376fa682014-09-07 13:06:12 -07004793 // Additional register check: this is not checked statically (as part of VerifyInstructions),
4794 // as target_type depends on the resolved type of the field.
4795 if (instruction_compatible && work_line_->NumRegs() > vregA + 1) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004796 const RegType& value_type_hi = work_line_->GetRegisterType(this, vregA + 1);
Andreas Gampe376fa682014-09-07 13:06:12 -07004797 value_compatible = value_type.IsDoubleTypes() && value_type.CheckWidePair(value_type_hi);
4798 } else {
4799 value_compatible = false;
4800 }
Jeff Haofe1f7c82013-08-01 14:50:24 -07004801 } else {
4802 instruction_compatible = false; // reference with primitive store
4803 value_compatible = false; // unused
4804 }
4805 if (!instruction_compatible) {
4806 // This is a global failure rather than a class change failure as the instructions and
4807 // the descriptors for the type should have been consistent within the same file at
4808 // compile time.
4809 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "put insn has type '" << insn_type
4810 << "' but expected type '" << target_type << "'";
4811 return;
4812 }
4813 if (!value_compatible) {
4814 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
4815 << " of type " << value_type << " but expected " << target_type << " for put";
4816 return;
4817 }
4818}
4819
Sebastien Hertz5243e912013-05-21 10:55:07 +02004820void MethodVerifier::VerifyAPut(const Instruction* inst,
Ian Rogersd8f69b02014-09-10 21:43:52 +00004821 const RegType& insn_type, bool is_primitive) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004822 const RegType& index_type = work_line_->GetRegisterType(this, inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07004823 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07004824 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07004825 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004826 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegB_23x());
Andreas Gampeeed3a5d2017-11-29 14:58:34 -08004827 if (array_type.IsZeroOrNull()) {
Nicolas Geoffray66389fb2015-06-19 10:35:42 +01004828 // Null array type; this code path will fail at runtime.
4829 // Still check that the given value matches the instruction's type.
Andreas Gampe4bf4c782015-08-14 14:07:43 -07004830 // Note: this is, as usual, complicated by the fact the the instruction isn't fully typed
4831 // and fits multiple register types.
4832 const RegType* modified_reg_type = &insn_type;
4833 if ((modified_reg_type == &reg_types_.Integer()) ||
4834 (modified_reg_type == &reg_types_.LongLo())) {
4835 // May be integer or float | long or double. Overwrite insn_type accordingly.
4836 const RegType& value_type = work_line_->GetRegisterType(this, inst->VRegA_23x());
4837 if (modified_reg_type == &reg_types_.Integer()) {
4838 if (&value_type == &reg_types_.Float()) {
4839 modified_reg_type = &value_type;
4840 }
4841 } else {
4842 if (&value_type == &reg_types_.DoubleLo()) {
4843 modified_reg_type = &value_type;
4844 }
4845 }
4846 }
4847 work_line_->VerifyRegisterType(this, inst->VRegA_23x(), *modified_reg_type);
jeffhaofc3144e2012-02-01 17:21:15 -08004848 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07004849 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aput";
Andreas Gampe8d8fc482016-03-25 16:24:20 -07004850 } else if (array_type.IsUnresolvedMergedReference()) {
Andreas Gampebb18a032016-03-22 20:34:25 -07004851 // Unresolved array types must be reference array types.
4852 if (is_primitive) {
4853 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "put insn has type '" << insn_type
4854 << "' but unresolved type '" << array_type << "'";
4855 } else {
4856 Fail(VERIFY_ERROR_NO_CLASS) << "cannot verify aput for " << array_type
4857 << " because of missing class";
4858 }
Ian Rogers89310de2012-02-01 13:47:30 -08004859 } else {
Ian Rogersd8f69b02014-09-10 21:43:52 +00004860 const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
Jeff Haofe1f7c82013-08-01 14:50:24 -07004861 const uint32_t vregA = inst->VRegA_23x();
Jeff Haob24b4a72013-07-31 13:47:31 -07004862 if (is_primitive) {
Jeff Haofe1f7c82013-08-01 14:50:24 -07004863 VerifyPrimitivePut(component_type, insn_type, vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07004864 } else {
Jeff Haob24b4a72013-07-31 13:47:31 -07004865 if (!component_type.IsReferenceTypes()) {
4866 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
4867 << " source for aput-object";
4868 } else {
4869 // The instruction agrees with the type of array, confirm the value to be stored does too
4870 // Note: we use the instruction type (rather than the component type) for aput-object as
4871 // incompatible classes will be caught at runtime as an array store exception
Ian Rogers7b078e82014-09-10 14:44:24 -07004872 work_line_->VerifyRegisterType(this, vregA, insn_type);
Jeff Haob24b4a72013-07-31 13:47:31 -07004873 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004874 }
4875 }
4876 }
4877}
4878
Mathieu Chartierc7853442015-03-27 14:35:38 -07004879ArtField* MethodVerifier::GetStaticField(int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08004880 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
4881 // Check access to class
Andreas Gampe98be1a92017-08-28 08:25:45 -07004882 const RegType& klass_type = ResolveClass<CheckAccess::kYes>(field_id.class_idx_);
Brian Carlstrom7934ac22013-07-26 10:54:15 -07004883 if (klass_type.IsConflict()) { // bad class
Ian Rogersad0b3a32012-04-16 14:50:24 -07004884 AppendToLastFailMessage(StringPrintf(" in attempt to access static field %d (%s) in %s",
4885 field_idx, dex_file_->GetFieldName(field_id),
4886 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers7b078e82014-09-10 14:44:24 -07004887 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08004888 }
Elliott Hughesb25c3f62012-03-26 16:35:06 -07004889 if (klass_type.IsUnresolvedTypes()) {
Andreas Gampe629be512017-08-25 17:09:32 -07004890 // Accessibility checks depend on resolved fields.
4891 DCHECK(klass_type.Equals(GetDeclaringClass()) || !failures_.empty());
4892
Ian Rogers7b078e82014-09-10 14:44:24 -07004893 return nullptr; // Can't resolve Class so no more to do here, will do checking at runtime.
Ian Rogers90040192011-12-16 08:54:29 -08004894 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07004895 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Vladimir Markoe11dd502017-12-08 14:09:45 +00004896 ArtField* field = class_linker->ResolveFieldJLS(field_idx, dex_cache_, class_loader_);
David Brazdilca3c8c32016-09-06 14:04:48 +01004897
4898 // Record result of the field resolution attempt.
4899 VerifierDeps::MaybeRecordFieldResolution(*dex_file_, field_idx, field);
4900
Ian Rogers7b078e82014-09-10 14:44:24 -07004901 if (field == nullptr) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07004902 VLOG(verifier) << "Unable to resolve static field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07004903 << dex_file_->GetFieldName(field_id) << ") in "
4904 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogers7b078e82014-09-10 14:44:24 -07004905 DCHECK(self_->IsExceptionPending());
4906 self_->ClearException();
4907 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004908 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
4909 field->GetAccessFlags())) {
David Sehr709b0702016-10-13 09:12:37 -07004910 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access static field " << field->PrettyField()
Ian Rogersad0b3a32012-04-16 14:50:24 -07004911 << " from " << GetDeclaringClass();
Ian Rogers7b078e82014-09-10 14:44:24 -07004912 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07004913 } else if (!field->IsStatic()) {
David Sehr709b0702016-10-13 09:12:37 -07004914 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << field->PrettyField() << " to be static";
Ian Rogers7b078e82014-09-10 14:44:24 -07004915 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07004916 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07004917 return field;
Ian Rogersd81871c2011-10-03 13:57:23 -07004918}
4919
Mathieu Chartierc7853442015-03-27 14:35:38 -07004920ArtField* MethodVerifier::GetInstanceField(const RegType& obj_type, int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08004921 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
Aart Bik31883642016-06-06 15:02:44 -07004922 // Check access to class.
Andreas Gampe98be1a92017-08-28 08:25:45 -07004923 const RegType& klass_type = ResolveClass<CheckAccess::kYes>(field_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004924 if (klass_type.IsConflict()) {
4925 AppendToLastFailMessage(StringPrintf(" in attempt to access instance field %d (%s) in %s",
4926 field_idx, dex_file_->GetFieldName(field_id),
4927 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers7b078e82014-09-10 14:44:24 -07004928 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08004929 }
jeffhao8cd6dda2012-02-22 10:15:34 -08004930 if (klass_type.IsUnresolvedTypes()) {
Andreas Gampe629be512017-08-25 17:09:32 -07004931 // Accessibility checks depend on resolved fields.
4932 DCHECK(klass_type.Equals(GetDeclaringClass()) || !failures_.empty());
4933
Ian Rogers7b078e82014-09-10 14:44:24 -07004934 return nullptr; // Can't resolve Class so no more to do here
Ian Rogers90040192011-12-16 08:54:29 -08004935 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07004936 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Vladimir Markoe11dd502017-12-08 14:09:45 +00004937 ArtField* field = class_linker->ResolveFieldJLS(field_idx, dex_cache_, class_loader_);
David Brazdilca3c8c32016-09-06 14:04:48 +01004938
4939 // Record result of the field resolution attempt.
4940 VerifierDeps::MaybeRecordFieldResolution(*dex_file_, field_idx, field);
4941
Ian Rogers7b078e82014-09-10 14:44:24 -07004942 if (field == nullptr) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07004943 VLOG(verifier) << "Unable to resolve instance field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07004944 << dex_file_->GetFieldName(field_id) << ") in "
4945 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogers7b078e82014-09-10 14:44:24 -07004946 DCHECK(self_->IsExceptionPending());
4947 self_->ClearException();
4948 return nullptr;
Andreas Gampeeed3a5d2017-11-29 14:58:34 -08004949 } else if (obj_type.IsZeroOrNull()) {
Aart Bik31883642016-06-06 15:02:44 -07004950 // Cannot infer and check type, however, access will cause null pointer exception.
4951 // Fall through into a few last soft failure checks below.
Stephen Kyle695c5982014-08-22 15:03:07 +01004952 } else if (!obj_type.IsReferenceTypes()) {
Aart Bik31883642016-06-06 15:02:44 -07004953 // Trying to read a field from something that isn't a reference.
Stephen Kyle695c5982014-08-22 15:03:07 +01004954 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance field access on object that has "
4955 << "non-reference type " << obj_type;
Ian Rogers7b078e82014-09-10 14:44:24 -07004956 return nullptr;
Ian Rogerse1758fe2012-04-19 11:31:15 -07004957 } else {
David Brazdil0d638bb2016-07-27 15:29:25 +01004958 std::string temp;
Mathieu Chartier3398c782016-09-30 10:27:43 -07004959 ObjPtr<mirror::Class> klass = field->GetDeclaringClass();
Ian Rogersd8f69b02014-09-10 21:43:52 +00004960 const RegType& field_klass =
Mathieu Chartier3398c782016-09-30 10:27:43 -07004961 FromClass(klass->GetDescriptor(&temp),
Mathieu Chartier1cc62e42016-10-03 18:01:28 -07004962 klass.Ptr(),
Mathieu Chartier3398c782016-09-30 10:27:43 -07004963 klass->CannotBeAssignedFromOtherTypes());
David Brazdil68b5c0b2016-01-19 14:25:29 +00004964 if (obj_type.IsUninitializedTypes()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07004965 // Field accesses through uninitialized references are only allowable for constructors where
David Brazdil68b5c0b2016-01-19 14:25:29 +00004966 // the field is declared in this class.
4967 // Note: this IsConstructor check is technically redundant, as UninitializedThis should only
4968 // appear in constructors.
4969 if (!obj_type.IsUninitializedThisReference() ||
4970 !IsConstructor() ||
4971 !field_klass.Equals(GetDeclaringClass())) {
David Sehr709b0702016-10-13 09:12:37 -07004972 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "cannot access instance field " << field->PrettyField()
David Brazdil68b5c0b2016-01-19 14:25:29 +00004973 << " of a not fully initialized object within the context"
David Sehr709b0702016-10-13 09:12:37 -07004974 << " of " << dex_file_->PrettyMethod(dex_method_idx_);
David Brazdil68b5c0b2016-01-19 14:25:29 +00004975 return nullptr;
4976 }
David Brazdilca3c8c32016-09-06 14:04:48 +01004977 } else if (!field_klass.IsAssignableFrom(obj_type, this)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07004978 // Trying to access C1.field1 using reference of type C2, which is neither C1 or a sub-class
4979 // of C1. For resolution to occur the declared class of the field must be compatible with
4980 // obj_type, we've discovered this wasn't so, so report the field didn't exist.
Andreas Gampe66596242016-04-14 10:55:04 -07004981 VerifyError type;
4982 bool is_aot = Runtime::Current()->IsAotCompiler();
4983 if (is_aot && (field_klass.IsUnresolvedTypes() || obj_type.IsUnresolvedTypes())) {
4984 // Compiler & unresolved types involved, retry at runtime.
4985 type = VerifyError::VERIFY_ERROR_NO_CLASS;
4986 } else {
Andreas Gampe8f4ade02016-04-15 10:09:16 -07004987 // Classes known (resolved; and thus assignability check is precise), or we are at runtime
4988 // and still missing classes. This is a hard failure.
Andreas Gampe66596242016-04-14 10:55:04 -07004989 type = VerifyError::VERIFY_ERROR_BAD_CLASS_HARD;
4990 }
David Sehr709b0702016-10-13 09:12:37 -07004991 Fail(type) << "cannot access instance field " << field->PrettyField()
Andreas Gampe66596242016-04-14 10:55:04 -07004992 << " from object of type " << obj_type;
Ian Rogers7b078e82014-09-10 14:44:24 -07004993 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004994 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004995 }
Aart Bik31883642016-06-06 15:02:44 -07004996
4997 // Few last soft failure checks.
4998 if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
4999 field->GetAccessFlags())) {
David Sehr709b0702016-10-13 09:12:37 -07005000 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access instance field " << field->PrettyField()
Aart Bik31883642016-06-06 15:02:44 -07005001 << " from " << GetDeclaringClass();
5002 return nullptr;
5003 } else if (field->IsStatic()) {
David Sehr709b0702016-10-13 09:12:37 -07005004 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << field->PrettyField()
Aart Bik31883642016-06-06 15:02:44 -07005005 << " to not be static";
5006 return nullptr;
5007 }
5008
5009 return field;
Ian Rogersd81871c2011-10-03 13:57:23 -07005010}
5011
Andreas Gampe896df402014-10-20 22:25:29 -07005012template <MethodVerifier::FieldAccessType kAccType>
5013void MethodVerifier::VerifyISFieldAccess(const Instruction* inst, const RegType& insn_type,
5014 bool is_primitive, bool is_static) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02005015 uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
Mathieu Chartierc7853442015-03-27 14:35:38 -07005016 ArtField* field;
Ian Rogersb94a27b2011-10-26 00:33:41 -07005017 if (is_static) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07005018 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07005019 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07005020 const RegType& object_type = work_line_->GetRegisterType(this, inst->VRegB_22c());
David Brazdil68b5c0b2016-01-19 14:25:29 +00005021
5022 // One is not allowed to access fields on uninitialized references, except to write to
5023 // fields in the constructor (before calling another constructor).
5024 // GetInstanceField does an assignability check which will fail for uninitialized types.
5025 // We thus modify the type if the uninitialized reference is a "this" reference (this also
5026 // checks at the same time that we're verifying a constructor).
5027 bool should_adjust = (kAccType == FieldAccessType::kAccPut) &&
5028 object_type.IsUninitializedThisReference();
5029 const RegType& adjusted_type = should_adjust
5030 ? GetRegTypeCache()->FromUninitialized(object_type)
5031 : object_type;
5032 field = GetInstanceField(adjusted_type, field_idx);
Andreas Gampe896df402014-10-20 22:25:29 -07005033 if (UNLIKELY(have_pending_hard_failure_)) {
5034 return;
5035 }
Alex Light4a2c8fc2016-02-12 11:01:54 -08005036 if (should_adjust) {
5037 if (field == nullptr) {
5038 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "Might be accessing a superclass instance field prior "
5039 << "to the superclass being initialized in "
David Sehr709b0702016-10-13 09:12:37 -07005040 << dex_file_->PrettyMethod(dex_method_idx_);
Alex Light4a2c8fc2016-02-12 11:01:54 -08005041 } else if (field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
5042 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "cannot access superclass instance field "
David Sehr709b0702016-10-13 09:12:37 -07005043 << field->PrettyField() << " of a not fully initialized "
Alex Light4a2c8fc2016-02-12 11:01:54 -08005044 << "object within the context of "
David Sehr709b0702016-10-13 09:12:37 -07005045 << dex_file_->PrettyMethod(dex_method_idx_);
Alex Light4a2c8fc2016-02-12 11:01:54 -08005046 return;
5047 }
5048 }
Ian Rogersb94a27b2011-10-26 00:33:41 -07005049 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00005050 const RegType* field_type = nullptr;
Ian Rogers7b078e82014-09-10 14:44:24 -07005051 if (field != nullptr) {
Andreas Gampe896df402014-10-20 22:25:29 -07005052 if (kAccType == FieldAccessType::kAccPut) {
5053 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
David Sehr709b0702016-10-13 09:12:37 -07005054 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << field->PrettyField()
Andreas Gampe896df402014-10-20 22:25:29 -07005055 << " from other class " << GetDeclaringClass();
Aart Bikc2bc2652016-05-23 14:58:49 -07005056 // Keep hunting for possible hard fails.
Andreas Gampe896df402014-10-20 22:25:29 -07005057 }
5058 }
5059
Mathieu Chartier3398c782016-09-30 10:27:43 -07005060 ObjPtr<mirror::Class> field_type_class =
Vladimir Marko208f6702017-12-08 12:00:50 +00005061 can_load_classes_ ? field->ResolveType() : field->LookupResolvedType();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07005062 if (field_type_class != nullptr) {
Mathieu Chartier3398c782016-09-30 10:27:43 -07005063 field_type = &FromClass(field->GetTypeDescriptor(),
Mathieu Chartier1cc62e42016-10-03 18:01:28 -07005064 field_type_class.Ptr(),
Andreas Gampef23f33d2015-06-23 14:18:17 -07005065 field_type_class->CannotBeAssignedFromOtherTypes());
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08005066 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07005067 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
5068 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07005069 }
Andreas Gampe7da4c402017-08-25 11:30:48 -07005070 } else {
5071 // If we don't have the field (it seems we failed resolution) and this is a PUT, we need to
5072 // redo verification at runtime as the field may be final, unless the field id shows it's in
5073 // the same class.
5074 //
5075 // For simplicity, it is OK to not distinguish compile-time vs runtime, and post this an
5076 // ACCESS_FIELD failure at runtime. This has the same effect as NO_FIELD - punting the class
5077 // to the access-checks interpreter.
5078 //
5079 // Note: see b/34966607. This and above may be changed in the future.
5080 if (kAccType == FieldAccessType::kAccPut) {
5081 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
5082 const char* field_class_descriptor = dex_file_->GetFieldDeclaringClassDescriptor(field_id);
5083 const RegType* field_class_type = &reg_types_.FromDescriptor(GetClassLoader(),
5084 field_class_descriptor,
5085 false);
5086 if (!field_class_type->Equals(GetDeclaringClass())) {
5087 Fail(VERIFY_ERROR_ACCESS_FIELD) << "could not check field put for final field modify of "
5088 << field_class_descriptor
5089 << "."
5090 << dex_file_->GetFieldName(field_id)
5091 << " from other class "
5092 << GetDeclaringClass();
5093 }
5094 }
Ian Rogers0d604842012-04-16 14:50:24 -07005095 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07005096 if (field_type == nullptr) {
5097 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
5098 const char* descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
Mathieu Chartierbf99f772014-08-23 16:37:27 -07005099 field_type = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07005100 }
Sebastien Hertz757b3042014-03-28 14:34:28 +01005101 DCHECK(field_type != nullptr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02005102 const uint32_t vregA = (is_static) ? inst->VRegA_21c() : inst->VRegA_22c();
Andreas Gampe896df402014-10-20 22:25:29 -07005103 static_assert(kAccType == FieldAccessType::kAccPut || kAccType == FieldAccessType::kAccGet,
5104 "Unexpected third access type");
5105 if (kAccType == FieldAccessType::kAccPut) {
5106 // sput or iput.
5107 if (is_primitive) {
5108 VerifyPrimitivePut(*field_type, insn_type, vregA);
Ian Rogersad0b3a32012-04-16 14:50:24 -07005109 } else {
David Brazdilca3c8c32016-09-06 14:04:48 +01005110 if (!insn_type.IsAssignableFrom(*field_type, this)) {
Vladimir Marko414000e2015-06-23 17:45:21 +01005111 // If the field type is not a reference, this is a global failure rather than
5112 // a class change failure as the instructions and the descriptors for the type
5113 // should have been consistent within the same file at compile time.
5114 VerifyError error = field_type->IsReferenceTypes() ? VERIFY_ERROR_BAD_CLASS_SOFT
5115 : VERIFY_ERROR_BAD_CLASS_HARD;
David Sehr709b0702016-10-13 09:12:37 -07005116 Fail(error) << "expected field " << ArtField::PrettyField(field)
Vladimir Marko414000e2015-06-23 17:45:21 +01005117 << " to be compatible with type '" << insn_type
5118 << "' but found type '" << *field_type
5119 << "' in put-object";
Andreas Gampe896df402014-10-20 22:25:29 -07005120 return;
5121 }
5122 work_line_->VerifyRegisterType(this, vregA, *field_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07005123 }
Andreas Gampe896df402014-10-20 22:25:29 -07005124 } else if (kAccType == FieldAccessType::kAccGet) {
5125 // sget or iget.
5126 if (is_primitive) {
5127 if (field_type->Equals(insn_type) ||
5128 (field_type->IsFloat() && insn_type.IsInteger()) ||
5129 (field_type->IsDouble() && insn_type.IsLong())) {
5130 // expected that read is of the correct primitive type or that int reads are reading
5131 // floats or long reads are reading doubles
5132 } else {
5133 // This is a global failure rather than a class change failure as the instructions and
5134 // the descriptors for the type should have been consistent within the same file at
5135 // compile time
David Sehr709b0702016-10-13 09:12:37 -07005136 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << ArtField::PrettyField(field)
Andreas Gampe896df402014-10-20 22:25:29 -07005137 << " to be of type '" << insn_type
5138 << "' but found type '" << *field_type << "' in get";
5139 return;
5140 }
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08005141 } else {
David Brazdilca3c8c32016-09-06 14:04:48 +01005142 if (!insn_type.IsAssignableFrom(*field_type, this)) {
Vladimir Marko414000e2015-06-23 17:45:21 +01005143 // If the field type is not a reference, this is a global failure rather than
5144 // a class change failure as the instructions and the descriptors for the type
5145 // should have been consistent within the same file at compile time.
5146 VerifyError error = field_type->IsReferenceTypes() ? VERIFY_ERROR_BAD_CLASS_SOFT
5147 : VERIFY_ERROR_BAD_CLASS_HARD;
David Sehr709b0702016-10-13 09:12:37 -07005148 Fail(error) << "expected field " << ArtField::PrettyField(field)
Vladimir Marko414000e2015-06-23 17:45:21 +01005149 << " to be compatible with type '" << insn_type
5150 << "' but found type '" << *field_type
5151 << "' in get-object";
Andreas Gampe890da292015-07-06 17:20:18 -07005152 if (error != VERIFY_ERROR_BAD_CLASS_HARD) {
Andreas Gampead238ce2015-08-24 21:13:08 -07005153 work_line_->SetRegisterType<LockOp::kClear>(this, vregA, reg_types_.Conflict());
Andreas Gampe890da292015-07-06 17:20:18 -07005154 }
Andreas Gampe896df402014-10-20 22:25:29 -07005155 return;
5156 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07005157 }
Andreas Gampe896df402014-10-20 22:25:29 -07005158 if (!field_type->IsLowHalf()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07005159 work_line_->SetRegisterType<LockOp::kClear>(this, vregA, *field_type);
Andreas Gampe896df402014-10-20 22:25:29 -07005160 } else {
5161 work_line_->SetRegisterTypeWide(this, vregA, *field_type, field_type->HighHalf(&reg_types_));
5162 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07005163 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07005164 LOG(FATAL) << "Unexpected case.";
Ian Rogersd81871c2011-10-03 13:57:23 -07005165 }
5166}
5167
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08005168ArtField* MethodVerifier::GetQuickFieldAccess(const Instruction* inst, RegisterLine* reg_line) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08005169 DCHECK(IsInstructionIGetQuickOrIPutQuick(inst->Opcode())) << inst->Opcode();
Ian Rogers7b078e82014-09-10 14:44:24 -07005170 const RegType& object_type = reg_line->GetRegisterType(this, inst->VRegB_22c());
Ian Rogers9bc54402014-04-17 16:40:01 -07005171 if (!object_type.HasClass()) {
5172 VLOG(verifier) << "Failed to get mirror::Class* from '" << object_type << "'";
5173 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02005174 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02005175 uint32_t field_offset = static_cast<uint32_t>(inst->VRegC_22c());
Mathieu Chartierc7853442015-03-27 14:35:38 -07005176 ArtField* const f = ArtField::FindInstanceFieldWithOffset(object_type.GetClass(), field_offset);
Sebastien Hertz479fc1e2014-04-04 17:51:34 +02005177 if (f == nullptr) {
5178 VLOG(verifier) << "Failed to find instance field at offset '" << field_offset
David Sehr709b0702016-10-13 09:12:37 -07005179 << "' from '" << mirror::Class::PrettyDescriptor(object_type.GetClass()) << "'";
Andreas Gampe8ddb7f52017-12-04 10:14:08 -08005180 } else {
5181 DCHECK_EQ(f->GetOffset().Uint32Value(), field_offset);
Sebastien Hertz479fc1e2014-04-04 17:51:34 +02005182 }
5183 return f;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02005184}
5185
Andreas Gampe896df402014-10-20 22:25:29 -07005186template <MethodVerifier::FieldAccessType kAccType>
5187void MethodVerifier::VerifyQuickFieldAccess(const Instruction* inst, const RegType& insn_type,
5188 bool is_primitive) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07005189 DCHECK(Runtime::Current()->IsStarted() || verify_to_dump_);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02005190
Mathieu Chartierc7853442015-03-27 14:35:38 -07005191 ArtField* field = GetQuickFieldAccess(inst, work_line_.get());
Ian Rogers7b078e82014-09-10 14:44:24 -07005192 if (field == nullptr) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02005193 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field from " << inst->Name();
5194 return;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02005195 }
Andreas Gampe896df402014-10-20 22:25:29 -07005196
5197 // For an IPUT_QUICK, we now test for final flag of the field.
5198 if (kAccType == FieldAccessType::kAccPut) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02005199 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
David Sehr709b0702016-10-13 09:12:37 -07005200 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << field->PrettyField()
Sebastien Hertzc15853b2013-06-25 17:36:27 +02005201 << " from other class " << GetDeclaringClass();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02005202 return;
5203 }
5204 }
Andreas Gampe896df402014-10-20 22:25:29 -07005205
5206 // Get the field type.
5207 const RegType* field_type;
5208 {
Vladimir Marko4098a7a2017-11-06 16:00:51 +00005209 ObjPtr<mirror::Class> field_type_class =
Vladimir Marko208f6702017-12-08 12:00:50 +00005210 can_load_classes_ ? field->ResolveType() : field->LookupResolvedType();
Andreas Gampe896df402014-10-20 22:25:29 -07005211
5212 if (field_type_class != nullptr) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07005213 field_type = &FromClass(field->GetTypeDescriptor(),
Mathieu Chartier1cc62e42016-10-03 18:01:28 -07005214 field_type_class.Ptr(),
Andreas Gampef23f33d2015-06-23 14:18:17 -07005215 field_type_class->CannotBeAssignedFromOtherTypes());
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02005216 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07005217 Thread* self = Thread::Current();
5218 DCHECK(!can_load_classes_ || self->IsExceptionPending());
5219 self->ClearException();
5220 field_type = &reg_types_.FromDescriptor(field->GetDeclaringClass()->GetClassLoader(),
Mathieu Chartierde40d472015-10-15 17:47:48 -07005221 field->GetTypeDescriptor(),
5222 false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02005223 }
Andreas Gampe896df402014-10-20 22:25:29 -07005224 if (field_type == nullptr) {
5225 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field type from " << inst->Name();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02005226 return;
5227 }
Andreas Gampe896df402014-10-20 22:25:29 -07005228 }
5229
5230 const uint32_t vregA = inst->VRegA_22c();
5231 static_assert(kAccType == FieldAccessType::kAccPut || kAccType == FieldAccessType::kAccGet,
5232 "Unexpected third access type");
5233 if (kAccType == FieldAccessType::kAccPut) {
5234 if (is_primitive) {
5235 // Primitive field assignability rules are weaker than regular assignability rules
5236 bool instruction_compatible;
5237 bool value_compatible;
5238 const RegType& value_type = work_line_->GetRegisterType(this, vregA);
5239 if (field_type->IsIntegralTypes()) {
5240 instruction_compatible = insn_type.IsIntegralTypes();
5241 value_compatible = value_type.IsIntegralTypes();
5242 } else if (field_type->IsFloat()) {
5243 instruction_compatible = insn_type.IsInteger(); // no [is]put-float, so expect [is]put-int
5244 value_compatible = value_type.IsFloatTypes();
5245 } else if (field_type->IsLong()) {
5246 instruction_compatible = insn_type.IsLong();
5247 value_compatible = value_type.IsLongTypes();
5248 } else if (field_type->IsDouble()) {
5249 instruction_compatible = insn_type.IsLong(); // no [is]put-double, so expect [is]put-long
5250 value_compatible = value_type.IsDoubleTypes();
5251 } else {
5252 instruction_compatible = false; // reference field with primitive store
5253 value_compatible = false; // unused
5254 }
5255 if (!instruction_compatible) {
5256 // This is a global failure rather than a class change failure as the instructions and
5257 // the descriptors for the type should have been consistent within the same file at
5258 // compile time
David Sehr709b0702016-10-13 09:12:37 -07005259 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << ArtField::PrettyField(field)
Andreas Gampe896df402014-10-20 22:25:29 -07005260 << " to be of type '" << insn_type
5261 << "' but found type '" << *field_type
5262 << "' in put";
5263 return;
5264 }
5265 if (!value_compatible) {
5266 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
5267 << " of type " << value_type
5268 << " but expected " << *field_type
David Sehr709b0702016-10-13 09:12:37 -07005269 << " for store to " << ArtField::PrettyField(field) << " in put";
Andreas Gampe896df402014-10-20 22:25:29 -07005270 return;
5271 }
5272 } else {
David Brazdilca3c8c32016-09-06 14:04:48 +01005273 if (!insn_type.IsAssignableFrom(*field_type, this)) {
David Sehr709b0702016-10-13 09:12:37 -07005274 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << ArtField::PrettyField(field)
Andreas Gampe896df402014-10-20 22:25:29 -07005275 << " to be compatible with type '" << insn_type
5276 << "' but found type '" << *field_type
5277 << "' in put-object";
5278 return;
5279 }
5280 work_line_->VerifyRegisterType(this, vregA, *field_type);
5281 }
5282 } else if (kAccType == FieldAccessType::kAccGet) {
5283 if (is_primitive) {
5284 if (field_type->Equals(insn_type) ||
5285 (field_type->IsFloat() && insn_type.IsIntegralTypes()) ||
5286 (field_type->IsDouble() && insn_type.IsLongTypes())) {
5287 // expected that read is of the correct primitive type or that int reads are reading
5288 // floats or long reads are reading doubles
5289 } else {
5290 // This is a global failure rather than a class change failure as the instructions and
5291 // the descriptors for the type should have been consistent within the same file at
5292 // compile time
David Sehr709b0702016-10-13 09:12:37 -07005293 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << ArtField::PrettyField(field)
Andreas Gampe896df402014-10-20 22:25:29 -07005294 << " to be of type '" << insn_type
5295 << "' but found type '" << *field_type << "' in Get";
5296 return;
5297 }
5298 } else {
David Brazdilca3c8c32016-09-06 14:04:48 +01005299 if (!insn_type.IsAssignableFrom(*field_type, this)) {
David Sehr709b0702016-10-13 09:12:37 -07005300 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << ArtField::PrettyField(field)
Andreas Gampe896df402014-10-20 22:25:29 -07005301 << " to be compatible with type '" << insn_type
5302 << "' but found type '" << *field_type
5303 << "' in get-object";
Andreas Gampead238ce2015-08-24 21:13:08 -07005304 work_line_->SetRegisterType<LockOp::kClear>(this, vregA, reg_types_.Conflict());
Andreas Gampe896df402014-10-20 22:25:29 -07005305 return;
5306 }
5307 }
5308 if (!field_type->IsLowHalf()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07005309 work_line_->SetRegisterType<LockOp::kClear>(this, vregA, *field_type);
Andreas Gampe896df402014-10-20 22:25:29 -07005310 } else {
5311 work_line_->SetRegisterTypeWide(this, vregA, *field_type, field_type->HighHalf(&reg_types_));
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02005312 }
5313 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07005314 LOG(FATAL) << "Unexpected case.";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02005315 }
5316}
5317
Ian Rogers776ac1f2012-04-13 23:36:36 -07005318bool MethodVerifier::CheckNotMoveException(const uint16_t* insns, int insn_idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07005319 if ((insns[insn_idx] & 0xff) == Instruction::MOVE_EXCEPTION) {
jeffhaod5347e02012-03-22 17:25:05 -07005320 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-exception";
Ian Rogersd81871c2011-10-03 13:57:23 -07005321 return false;
5322 }
5323 return true;
5324}
5325
Stephen Kyle9bc61992014-09-22 13:53:15 +01005326bool MethodVerifier::CheckNotMoveResult(const uint16_t* insns, int insn_idx) {
5327 if (((insns[insn_idx] & 0xff) >= Instruction::MOVE_RESULT) &&
5328 ((insns[insn_idx] & 0xff) <= Instruction::MOVE_RESULT_OBJECT)) {
5329 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-result*";
5330 return false;
5331 }
5332 return true;
5333}
5334
5335bool MethodVerifier::CheckNotMoveExceptionOrMoveResult(const uint16_t* insns, int insn_idx) {
5336 return (CheckNotMoveException(insns, insn_idx) && CheckNotMoveResult(insns, insn_idx));
5337}
5338
Ian Rogersebbdd872014-07-07 23:53:08 -07005339bool MethodVerifier::UpdateRegisters(uint32_t next_insn, RegisterLine* merge_line,
5340 bool update_merge_line) {
Ian Rogersd81871c2011-10-03 13:57:23 -07005341 bool changed = true;
5342 RegisterLine* target_line = reg_table_.GetLine(next_insn);
Mathieu Chartierde40d472015-10-15 17:47:48 -07005343 if (!GetInstructionFlags(next_insn).IsVisitedOrChanged()) {
jeffhaobdb76512011-09-07 11:43:16 -07005344 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07005345 * We haven't processed this instruction before, and we haven't touched the registers here, so
5346 * there's nothing to "merge". Copy the registers over and mark it as changed. (This is the
5347 * only way a register can transition out of "unknown", so this is not just an optimization.)
jeffhaobdb76512011-09-07 11:43:16 -07005348 */
Andreas Gampea727e372015-08-25 09:22:37 -07005349 target_line->CopyFromLine(merge_line);
Mathieu Chartierde40d472015-10-15 17:47:48 -07005350 if (GetInstructionFlags(next_insn).IsReturn()) {
Jeff Haob24b4a72013-07-31 13:47:31 -07005351 // Verify that the monitor stack is empty on return.
Andreas Gampea727e372015-08-25 09:22:37 -07005352 merge_line->VerifyMonitorStackEmpty(this);
5353
Ian Rogersb8c78592013-07-25 23:52:52 +00005354 // For returns we only care about the operand to the return, all other registers are dead.
5355 // Initialize them as conflicts so they don't add to GC and deoptimization information.
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08005356 const Instruction* ret_inst = &code_item_accessor_.InstructionAt(next_insn);
Andreas Gampea727e372015-08-25 09:22:37 -07005357 AdjustReturnLine(this, ret_inst, target_line);
Aart Bik31883642016-06-06 15:02:44 -07005358 // Directly bail if a hard failure was found.
Aart Bikb0526322016-06-01 14:06:00 -07005359 if (have_pending_hard_failure_) {
5360 return false;
5361 }
Ian Rogersb8c78592013-07-25 23:52:52 +00005362 }
jeffhaobdb76512011-09-07 11:43:16 -07005363 } else {
Mathieu Chartier361e04a2016-02-16 14:06:35 -08005364 RegisterLineArenaUniquePtr copy;
Andreas Gampe92d77202017-12-06 20:49:00 -08005365 if (UNLIKELY(VLOG_IS_ON(verifier_debug))) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07005366 copy.reset(RegisterLine::Create(target_line->NumRegs(), this));
Ian Rogers7b0c5b42012-02-16 15:29:07 -08005367 copy->CopyFromLine(target_line);
5368 }
Ian Rogers7b078e82014-09-10 14:44:24 -07005369 changed = target_line->MergeRegisters(this, merge_line);
Ian Rogersad0b3a32012-04-16 14:50:24 -07005370 if (have_pending_hard_failure_) {
Ian Rogersd81871c2011-10-03 13:57:23 -07005371 return false;
jeffhaobdb76512011-09-07 11:43:16 -07005372 }
Andreas Gampe92d77202017-12-06 20:49:00 -08005373 if (UNLIKELY(VLOG_IS_ON(verifier_debug)) && changed) {
Elliott Hughes398f64b2012-03-26 18:05:48 -07005374 LogVerifyInfo() << "Merging at [" << reinterpret_cast<void*>(work_insn_idx_) << "]"
Elliott Hughesc073b072012-05-24 19:29:17 -07005375 << " to [" << reinterpret_cast<void*>(next_insn) << "]: " << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07005376 << copy->Dump(this) << " MERGE\n"
5377 << merge_line->Dump(this) << " ==\n"
5378 << target_line->Dump(this) << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07005379 }
Ian Rogersebbdd872014-07-07 23:53:08 -07005380 if (update_merge_line && changed) {
5381 merge_line->CopyFromLine(target_line);
5382 }
jeffhaobdb76512011-09-07 11:43:16 -07005383 }
Ian Rogersd81871c2011-10-03 13:57:23 -07005384 if (changed) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07005385 GetInstructionFlags(next_insn).SetChanged();
jeffhaobdb76512011-09-07 11:43:16 -07005386 }
5387 return true;
5388}
5389
Ian Rogers7b3ddd22013-02-21 15:19:52 -08005390InstructionFlags* MethodVerifier::CurrentInsnFlags() {
Mathieu Chartierde40d472015-10-15 17:47:48 -07005391 return &GetInstructionFlags(work_insn_idx_);
Ian Rogers776ac1f2012-04-13 23:36:36 -07005392}
5393
Ian Rogersd8f69b02014-09-10 21:43:52 +00005394const RegType& MethodVerifier::GetMethodReturnType() {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07005395 if (return_type_ == nullptr) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07005396 if (mirror_method_ != nullptr) {
Vladimir Markob45528c2017-07-27 14:14:28 +01005397 ObjPtr<mirror::Class> return_type_class = can_load_classes_
5398 ? mirror_method_->ResolveReturnType()
5399 : mirror_method_->LookupResolvedReturnType();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07005400 if (return_type_class != nullptr) {
Andreas Gampef23f33d2015-06-23 14:18:17 -07005401 return_type_ = &FromClass(mirror_method_->GetReturnTypeDescriptor(),
Vladimir Markob45528c2017-07-27 14:14:28 +01005402 return_type_class.Ptr(),
Andreas Gampef23f33d2015-06-23 14:18:17 -07005403 return_type_class->CannotBeAssignedFromOtherTypes());
Ian Rogersfc0e94b2013-09-23 23:51:32 -07005404 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07005405 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
5406 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07005407 }
5408 }
5409 if (return_type_ == nullptr) {
5410 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
5411 const DexFile::ProtoId& proto_id = dex_file_->GetMethodPrototype(method_id);
Andreas Gampea5b09a62016-11-17 15:21:22 -08005412 dex::TypeIndex return_type_idx = proto_id.return_type_idx_;
Ian Rogersfc0e94b2013-09-23 23:51:32 -07005413 const char* descriptor = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(return_type_idx));
Mathieu Chartierbf99f772014-08-23 16:37:27 -07005414 return_type_ = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07005415 }
5416 }
5417 return *return_type_;
Ian Rogersad0b3a32012-04-16 14:50:24 -07005418}
5419
Ian Rogersd8f69b02014-09-10 21:43:52 +00005420const RegType& MethodVerifier::GetDeclaringClass() {
Ian Rogers7b078e82014-09-10 14:44:24 -07005421 if (declaring_class_ == nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08005422 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
Brian Carlstrom93c33962013-07-26 10:37:43 -07005423 const char* descriptor
5424 = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(method_id.class_idx_));
Mathieu Chartiere401d142015-04-22 13:56:20 -07005425 if (mirror_method_ != nullptr) {
Ian Rogers637c65b2013-05-31 11:46:00 -07005426 mirror::Class* klass = mirror_method_->GetDeclaringClass();
Mathieu Chartierde40d472015-10-15 17:47:48 -07005427 declaring_class_ = &FromClass(descriptor, klass, klass->CannotBeAssignedFromOtherTypes());
Ian Rogers637c65b2013-05-31 11:46:00 -07005428 } else {
Mathieu Chartierbf99f772014-08-23 16:37:27 -07005429 declaring_class_ = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers637c65b2013-05-31 11:46:00 -07005430 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07005431 }
Ian Rogers637c65b2013-05-31 11:46:00 -07005432 return *declaring_class_;
Ian Rogersad0b3a32012-04-16 14:50:24 -07005433}
5434
Ian Rogers2bcb4a42012-11-08 10:39:18 -08005435std::vector<int32_t> MethodVerifier::DescribeVRegs(uint32_t dex_pc) {
5436 RegisterLine* line = reg_table_.GetLine(dex_pc);
Sebastien Hertzaa0c00c2014-03-14 17:58:54 +01005437 DCHECK(line != nullptr) << "No register line at DEX pc " << StringPrintf("0x%x", dex_pc);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08005438 std::vector<int32_t> result;
5439 for (size_t i = 0; i < line->NumRegs(); ++i) {
Ian Rogers7b078e82014-09-10 14:44:24 -07005440 const RegType& type = line->GetRegisterType(this, i);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08005441 if (type.IsConstant()) {
5442 result.push_back(type.IsPreciseConstant() ? kConstant : kImpreciseConstant);
Ian Rogers7b078e82014-09-10 14:44:24 -07005443 const ConstantType* const_val = down_cast<const ConstantType*>(&type);
5444 result.push_back(const_val->ConstantValue());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08005445 } else if (type.IsConstantLo()) {
5446 result.push_back(type.IsPreciseConstantLo() ? kConstant : kImpreciseConstant);
Ian Rogers7b078e82014-09-10 14:44:24 -07005447 const ConstantType* const_val = down_cast<const ConstantType*>(&type);
5448 result.push_back(const_val->ConstantValueLo());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08005449 } else if (type.IsConstantHi()) {
5450 result.push_back(type.IsPreciseConstantHi() ? kConstant : kImpreciseConstant);
Ian Rogers7b078e82014-09-10 14:44:24 -07005451 const ConstantType* const_val = down_cast<const ConstantType*>(&type);
5452 result.push_back(const_val->ConstantValueHi());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08005453 } else if (type.IsIntegralTypes()) {
5454 result.push_back(kIntVReg);
5455 result.push_back(0);
5456 } else if (type.IsFloat()) {
5457 result.push_back(kFloatVReg);
5458 result.push_back(0);
5459 } else if (type.IsLong()) {
5460 result.push_back(kLongLoVReg);
5461 result.push_back(0);
5462 result.push_back(kLongHiVReg);
5463 result.push_back(0);
5464 ++i;
5465 } else if (type.IsDouble()) {
5466 result.push_back(kDoubleLoVReg);
5467 result.push_back(0);
5468 result.push_back(kDoubleHiVReg);
5469 result.push_back(0);
5470 ++i;
5471 } else if (type.IsUndefined() || type.IsConflict() || type.IsHighHalf()) {
5472 result.push_back(kUndefined);
5473 result.push_back(0);
5474 } else {
Ian Rogers7b3ddd22013-02-21 15:19:52 -08005475 CHECK(type.IsNonZeroReferenceTypes());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08005476 result.push_back(kReferenceVReg);
5477 result.push_back(0);
5478 }
5479 }
5480 return result;
5481}
5482
Ian Rogersd8f69b02014-09-10 21:43:52 +00005483const RegType& MethodVerifier::DetermineCat1Constant(int32_t value, bool precise) {
Sebastien Hertz849600b2013-12-20 10:28:08 +01005484 if (precise) {
5485 // Precise constant type.
5486 return reg_types_.FromCat1Const(value, true);
5487 } else {
5488 // Imprecise constant type.
5489 if (value < -32768) {
5490 return reg_types_.IntConstant();
5491 } else if (value < -128) {
5492 return reg_types_.ShortConstant();
5493 } else if (value < 0) {
5494 return reg_types_.ByteConstant();
5495 } else if (value == 0) {
5496 return reg_types_.Zero();
5497 } else if (value == 1) {
5498 return reg_types_.One();
5499 } else if (value < 128) {
5500 return reg_types_.PosByteConstant();
5501 } else if (value < 32768) {
5502 return reg_types_.PosShortConstant();
5503 } else if (value < 65536) {
5504 return reg_types_.CharConstant();
5505 } else {
5506 return reg_types_.IntConstant();
5507 }
5508 }
5509}
5510
Elliott Hughes0a1038b2012-06-14 16:24:17 -07005511void MethodVerifier::Init() {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08005512 art::verifier::RegTypeCache::Init();
Brian Carlstrome7d856b2012-01-11 18:10:55 -08005513}
5514
Elliott Hughes0a1038b2012-06-14 16:24:17 -07005515void MethodVerifier::Shutdown() {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08005516 verifier::RegTypeCache::ShutDown();
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08005517}
jeffhaod1224c72012-02-29 13:43:08 -08005518
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -07005519void MethodVerifier::VisitStaticRoots(RootVisitor* visitor) {
5520 RegTypeCache::VisitStaticRoots(visitor);
Mathieu Chartier7c438b12014-09-12 17:01:24 -07005521}
5522
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -07005523void MethodVerifier::VisitRoots(RootVisitor* visitor, const RootInfo& root_info) {
5524 reg_types_.VisitRoots(visitor, root_info);
Mathieu Chartierc528dba2013-11-26 12:00:11 -08005525}
5526
Andreas Gampef23f33d2015-06-23 14:18:17 -07005527const RegType& MethodVerifier::FromClass(const char* descriptor,
5528 mirror::Class* klass,
5529 bool precise) {
5530 DCHECK(klass != nullptr);
5531 if (precise && !klass->IsInstantiable() && !klass->IsPrimitive()) {
5532 Fail(VerifyError::VERIFY_ERROR_NO_CLASS) << "Could not create precise reference for "
5533 << "non-instantiable klass " << descriptor;
5534 precise = false;
5535 }
5536 return reg_types_.FromClass(descriptor, klass, precise);
5537}
5538
Ian Rogersd81871c2011-10-03 13:57:23 -07005539} // namespace verifier
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07005540} // namespace art