blob: 6555e1435894e4709631233d815e3d7de6641aff [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 Gampe542451c2016-07-26 09:02:02 -070025#include "base/enums.h"
Elliott Hughes07ed66b2012-12-12 18:34:25 -080026#include "base/logging.h"
Ian Rogers637c65b2013-05-31 11:46:00 -070027#include "base/mutex-inl.h"
Vladimir Marko637ee0b2015-09-04 12:47:41 +010028#include "base/stl_util.h"
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -080029#include "base/systrace.h"
Vladimir Marko80afd022015-05-19 18:08:00 +010030#include "base/time_utils.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070031#include "class_linker.h"
Vladimir Marko2b5eaa22013-12-13 13:59:30 +000032#include "compiler_callbacks.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070033#include "dex_file-inl.h"
Ian Rogersd0583802013-06-01 10:51:46 -070034#include "dex_instruction-inl.h"
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080035#include "dex_instruction_utils.h"
Alex Lighteb7c1442015-08-31 13:17:42 -070036#include "experimental_flags.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070037#include "gc/accounting/card_table-inl.h"
Andreas Gampe6d7abbd2017-04-24 13:19:09 -070038#include "handle_scope-inl.h"
Ian Rogers2bcb4a42012-11-08 10:39:18 -080039#include "indenter.h"
Ian Rogers84fa0742011-10-25 18:13:30 -070040#include "intern_table.h"
Ian Rogers0571d352011-11-03 19:51:38 -070041#include "leb128.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080042#include "mirror/class-inl.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070043#include "mirror/class.h"
Ian Rogers39ebcb82013-05-30 16:57:23 -070044#include "mirror/dex_cache-inl.h"
Orion Hodsoncfa325e2016-10-13 10:25:54 +010045#include "mirror/method_handle_impl.h"
Orion Hodson2e599942017-09-22 16:17:41 +010046#include "mirror/method_type.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080047#include "mirror/object-inl.h"
48#include "mirror/object_array-inl.h"
Ian Rogers7b078e82014-09-10 14:44:24 -070049#include "reg_type-inl.h"
Ian Rogers39ebcb82013-05-30 16:57:23 -070050#include "register_line-inl.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070051#include "runtime.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070052#include "scoped_thread_state_change-inl.h"
Andreas Gampe513061a2017-06-01 09:17:34 -070053#include "stack.h"
Vladimir Marko80afd022015-05-19 18:08:00 +010054#include "utils.h"
Andreas Gampe6d7abbd2017-04-24 13:19:09 -070055#include "verifier_compiler_binding.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070056#include "verifier_deps.h"
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070057
58namespace art {
Ian Rogersd81871c2011-10-03 13:57:23 -070059namespace verifier {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070060
Andreas Gampe46ee31b2016-12-14 10:11:49 -080061using android::base::StringPrintf;
62
Mathieu Chartier8e219ae2014-08-19 14:29:46 -070063static constexpr bool kTimeVerifyMethod = !kIsDebugBuild;
Andreas Gampeec6e6c12015-11-05 20:39:56 -080064static constexpr bool kDebugVerify = false;
Anwar Ghuloum75a43f12013-08-13 17:22:14 -070065// TODO: Add a constant to method_verifier to turn on verbose logging?
Ian Rogers2c8a8572011-10-24 17:11:36 -070066
Andreas Gampeebf850c2015-08-14 15:37:35 -070067// On VLOG(verifier), should we dump the whole state when we run into a hard failure?
68static constexpr bool kDumpRegLinesOnHardFailureIfVLOG = true;
69
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -080070// We print a warning blurb about "dx --no-optimize" when we find monitor-locking issues. Make
71// sure we only print this once.
72static bool gPrintedDxMonitorText = false;
73
Vladimir Marko69d310e2017-10-09 14:12:23 +010074PcToRegisterLineTable::PcToRegisterLineTable(ScopedArenaAllocator& allocator)
75 : register_lines_(allocator.Adapter(kArenaAllocVerifier)) {}
Mathieu Chartierde40d472015-10-15 17:47:48 -070076
Ian Rogers7b3ddd22013-02-21 15:19:52 -080077void PcToRegisterLineTable::Init(RegisterTrackingMode mode, InstructionFlags* flags,
Ian Rogersd81871c2011-10-03 13:57:23 -070078 uint32_t insns_size, uint16_t registers_size,
Ian Rogers776ac1f2012-04-13 23:36:36 -070079 MethodVerifier* verifier) {
Ian Rogersd81871c2011-10-03 13:57:23 -070080 DCHECK_GT(insns_size, 0U);
Mathieu Chartierde40d472015-10-15 17:47:48 -070081 register_lines_.resize(insns_size);
Ian Rogersd81871c2011-10-03 13:57:23 -070082 for (uint32_t i = 0; i < insns_size; i++) {
83 bool interesting = false;
84 switch (mode) {
85 case kTrackRegsAll:
86 interesting = flags[i].IsOpcode();
87 break;
Sameer Abu Asal02c42232013-04-30 12:09:45 -070088 case kTrackCompilerInterestPoints:
Brian Carlstrom02c8cc62013-07-18 15:54:44 -070089 interesting = flags[i].IsCompileTimeInfoPoint() || flags[i].IsBranchTarget();
Ian Rogersd81871c2011-10-03 13:57:23 -070090 break;
91 case kTrackRegsBranches:
92 interesting = flags[i].IsBranchTarget();
93 break;
94 default:
95 break;
96 }
97 if (interesting) {
Mathieu Chartierde40d472015-10-15 17:47:48 -070098 register_lines_[i].reset(RegisterLine::Create(registers_size, verifier));
Ian Rogersd0fbd852013-09-24 18:17:04 -070099 }
100 }
101}
102
Mathieu Chartierde40d472015-10-15 17:47:48 -0700103PcToRegisterLineTable::~PcToRegisterLineTable() {}
Ian Rogersd81871c2011-10-03 13:57:23 -0700104
Andreas Gampe7c038102014-10-27 20:08:46 -0700105// Note: returns true on failure.
106ALWAYS_INLINE static inline bool FailOrAbort(MethodVerifier* verifier, bool condition,
107 const char* error_msg, uint32_t work_insn_idx) {
108 if (kIsDebugBuild) {
Andreas Gampef8f36c22016-11-19 14:51:52 -0800109 // In a debug build, abort if the error condition is wrong. Only warn if
110 // we are already aborting (as this verification is likely run to print
111 // lock information).
112 if (LIKELY(gAborting == 0)) {
113 DCHECK(condition) << error_msg << work_insn_idx;
114 } else {
115 if (!condition) {
116 LOG(ERROR) << error_msg << work_insn_idx;
117 verifier->Fail(VERIFY_ERROR_BAD_CLASS_HARD) << error_msg << work_insn_idx;
118 return true;
119 }
120 }
Andreas Gampe7c038102014-10-27 20:08:46 -0700121 } else {
122 // In a non-debug build, just fail the class.
123 if (!condition) {
124 verifier->Fail(VERIFY_ERROR_BAD_CLASS_HARD) << error_msg << work_insn_idx;
125 return true;
126 }
127 }
128
129 return false;
130}
131
Stephen Kyle7e541c92014-12-17 17:10:02 +0000132static void SafelyMarkAllRegistersAsConflicts(MethodVerifier* verifier, RegisterLine* reg_line) {
Andreas Gampef10b6e12015-08-12 10:48:12 -0700133 if (verifier->IsInstanceConstructor()) {
Stephen Kyle7e541c92014-12-17 17:10:02 +0000134 // Before we mark all regs as conflicts, check that we don't have an uninitialized this.
135 reg_line->CheckConstructorReturn(verifier);
136 }
137 reg_line->MarkAllRegistersAsConflicts(verifier);
138}
139
Andreas Gampe6d7abbd2017-04-24 13:19:09 -0700140FailureKind MethodVerifier::VerifyClass(Thread* self,
141 mirror::Class* klass,
142 CompilerCallbacks* callbacks,
143 bool allow_soft_failures,
144 HardFailLogMode log_level,
145 std::string* error) {
jeffhaobdb76512011-09-07 11:43:16 -0700146 if (klass->IsVerified()) {
Andreas Gampe6d7abbd2017-04-24 13:19:09 -0700147 return FailureKind::kNoFailure;
jeffhaobdb76512011-09-07 11:43:16 -0700148 }
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800149 bool early_failure = false;
150 std::string failure_message;
Mathieu Chartierf8322842014-05-16 10:59:25 -0700151 const DexFile& dex_file = klass->GetDexFile();
152 const DexFile::ClassDef* class_def = klass->GetClassDef();
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800153 mirror::Class* super = klass->GetSuperClass();
Ian Rogers1ff3c982014-08-12 02:30:58 -0700154 std::string temp;
Ian Rogers7b078e82014-09-10 14:44:24 -0700155 if (super == nullptr && strcmp("Ljava/lang/Object;", klass->GetDescriptor(&temp)) != 0) {
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800156 early_failure = true;
157 failure_message = " that has no super class";
Ian Rogers7b078e82014-09-10 14:44:24 -0700158 } else if (super != nullptr && super->IsFinal()) {
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800159 early_failure = true;
David Sehr709b0702016-10-13 09:12:37 -0700160 failure_message = " that attempts to sub-class final class " + super->PrettyDescriptor();
Ian Rogers7b078e82014-09-10 14:44:24 -0700161 } else if (class_def == nullptr) {
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800162 early_failure = true;
163 failure_message = " that isn't present in dex file " + dex_file.GetLocation();
164 }
165 if (early_failure) {
David Sehr709b0702016-10-13 09:12:37 -0700166 *error = "Verifier rejected class " + klass->PrettyDescriptor() + failure_message;
Andreas Gampe53e32d12015-12-09 21:03:23 -0800167 if (callbacks != nullptr) {
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800168 ClassReference ref(&dex_file, klass->GetDexClassDefIndex());
Andreas Gampe53e32d12015-12-09 21:03:23 -0800169 callbacks->ClassRejected(ref);
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800170 }
Andreas Gampe6d7abbd2017-04-24 13:19:09 -0700171 return FailureKind::kHardFailure;
jeffhaobdb76512011-09-07 11:43:16 -0700172 }
Ian Rogers7b078e82014-09-10 14:44:24 -0700173 StackHandleScope<2> hs(self);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700174 Handle<mirror::DexCache> dex_cache(hs.NewHandle(klass->GetDexCache()));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700175 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(klass->GetClassLoader()));
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800176 return VerifyClass(self,
177 &dex_file,
178 dex_cache,
179 class_loader,
David Brazdil15fc7292016-09-02 14:13:18 +0100180 *class_def,
Andreas Gampe53e32d12015-12-09 21:03:23 -0800181 callbacks,
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800182 allow_soft_failures,
Andreas Gampe7fe30232016-03-25 16:58:00 -0700183 log_level,
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800184 error);
185}
186
187template <bool kDirect>
188static bool HasNextMethod(ClassDataItemIterator* it) {
189 return kDirect ? it->HasNextDirectMethod() : it->HasNextVirtualMethod();
190}
191
Andreas Gampe6d7abbd2017-04-24 13:19:09 -0700192static FailureKind FailureKindMax(FailureKind fk1, FailureKind fk2) {
193 static_assert(FailureKind::kNoFailure < FailureKind::kSoftFailure
194 && FailureKind::kSoftFailure < FailureKind::kHardFailure,
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800195 "Unexpected FailureKind order");
196 return std::max(fk1, fk2);
197}
198
199void MethodVerifier::FailureData::Merge(const MethodVerifier::FailureData& fd) {
200 kind = FailureKindMax(kind, fd.kind);
201 types |= fd.types;
202}
203
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800204template <bool kDirect>
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800205MethodVerifier::FailureData MethodVerifier::VerifyMethods(Thread* self,
206 ClassLinker* linker,
207 const DexFile* dex_file,
David Brazdil15fc7292016-09-02 14:13:18 +0100208 const DexFile::ClassDef& class_def,
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800209 ClassDataItemIterator* it,
210 Handle<mirror::DexCache> dex_cache,
211 Handle<mirror::ClassLoader> class_loader,
212 CompilerCallbacks* callbacks,
213 bool allow_soft_failures,
Andreas Gampe5fd66d02016-09-12 20:22:19 -0700214 HardFailLogMode log_level,
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800215 bool need_precise_constants,
216 std::string* error_string) {
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800217 DCHECK(it != nullptr);
218
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800219 MethodVerifier::FailureData failure_data;
220
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800221 int64_t previous_method_idx = -1;
222 while (HasNextMethod<kDirect>(it)) {
223 self->AllowThreadSuspension();
224 uint32_t method_idx = it->GetMemberIndex();
225 if (method_idx == previous_method_idx) {
226 // smali can create dex files with two encoded_methods sharing the same method_idx
227 // http://code.google.com/p/smali/issues/detail?id=119
228 it->Next();
229 continue;
230 }
231 previous_method_idx = method_idx;
David Brazdil15fc7292016-09-02 14:13:18 +0100232 InvokeType type = it->GetMethodInvokeType(class_def);
Vladimir Markoba118822017-06-12 15:41:56 +0100233 ArtMethod* method = linker->ResolveMethod<ClassLinker::ResolveMode::kNoChecks>(
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800234 *dex_file, method_idx, dex_cache, class_loader, nullptr, type);
235 if (method == nullptr) {
236 DCHECK(self->IsExceptionPending());
237 // We couldn't resolve the method, but continue regardless.
238 self->ClearException();
239 } else {
240 DCHECK(method->GetDeclaringClassUnchecked() != nullptr) << type;
241 }
242 StackHandleScope<1> hs(self);
243 std::string hard_failure_msg;
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800244 MethodVerifier::FailureData result = VerifyMethod(self,
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800245 method_idx,
246 dex_file,
247 dex_cache,
248 class_loader,
249 class_def,
250 it->GetMethodCodeItem(),
251 method,
252 it->GetMethodAccessFlags(),
Andreas Gampe53e32d12015-12-09 21:03:23 -0800253 callbacks,
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800254 allow_soft_failures,
Andreas Gampe7fe30232016-03-25 16:58:00 -0700255 log_level,
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800256 need_precise_constants,
257 &hard_failure_msg);
Andreas Gampe6d7abbd2017-04-24 13:19:09 -0700258 if (result.kind == FailureKind::kHardFailure) {
259 if (failure_data.kind == FailureKind::kHardFailure) {
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800260 // If we logged an error before, we need a newline.
261 *error_string += "\n";
262 } else {
263 // If we didn't log a hard failure before, print the header of the message.
264 *error_string += "Verifier rejected class ";
David Brazdil15fc7292016-09-02 14:13:18 +0100265 *error_string += PrettyDescriptor(dex_file->GetClassDescriptor(class_def));
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800266 *error_string += ":";
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800267 }
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800268 *error_string += " ";
269 *error_string += hard_failure_msg;
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800270 }
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800271 failure_data.Merge(result);
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800272 it->Next();
273 }
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800274
275 return failure_data;
Shih-wei Liao371814f2011-10-27 16:52:10 -0700276}
277
Andreas Gampe6d7abbd2017-04-24 13:19:09 -0700278FailureKind MethodVerifier::VerifyClass(Thread* self,
279 const DexFile* dex_file,
280 Handle<mirror::DexCache> dex_cache,
281 Handle<mirror::ClassLoader> class_loader,
282 const DexFile::ClassDef& class_def,
283 CompilerCallbacks* callbacks,
284 bool allow_soft_failures,
285 HardFailLogMode log_level,
286 std::string* error) {
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -0800287 ScopedTrace trace(__FUNCTION__);
Andreas Gampe507cc6f2015-06-19 22:58:47 -0700288
289 // A class must not be abstract and final.
David Brazdil15fc7292016-09-02 14:13:18 +0100290 if ((class_def.access_flags_ & (kAccAbstract | kAccFinal)) == (kAccAbstract | kAccFinal)) {
Andreas Gampe507cc6f2015-06-19 22:58:47 -0700291 *error = "Verifier rejected class ";
David Brazdil15fc7292016-09-02 14:13:18 +0100292 *error += PrettyDescriptor(dex_file->GetClassDescriptor(class_def));
Andreas Gampe507cc6f2015-06-19 22:58:47 -0700293 *error += ": class is abstract and final.";
Andreas Gampe6d7abbd2017-04-24 13:19:09 -0700294 return FailureKind::kHardFailure;
Andreas Gampe507cc6f2015-06-19 22:58:47 -0700295 }
296
David Brazdil15fc7292016-09-02 14:13:18 +0100297 const uint8_t* class_data = dex_file->GetClassData(class_def);
Ian Rogers7b078e82014-09-10 14:44:24 -0700298 if (class_data == nullptr) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700299 // empty class, probably a marker interface
Andreas Gampe6d7abbd2017-04-24 13:19:09 -0700300 return FailureKind::kNoFailure;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700301 }
jeffhaof56197c2012-03-05 18:01:54 -0800302 ClassDataItemIterator it(*dex_file, class_data);
Mathieu Chartiere17cf242017-06-19 11:05:51 -0700303 it.SkipAllFields();
Ian Rogersad0b3a32012-04-16 14:50:24 -0700304 ClassLinker* linker = Runtime::Current()->GetClassLinker();
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800305 // Direct methods.
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800306 MethodVerifier::FailureData data1 = VerifyMethods<true>(self,
307 linker,
308 dex_file,
309 class_def,
310 &it,
311 dex_cache,
312 class_loader,
313 callbacks,
314 allow_soft_failures,
Andreas Gampe7fe30232016-03-25 16:58:00 -0700315 log_level,
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800316 false /* need precise constants */,
317 error);
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800318 // Virtual methods.
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800319 MethodVerifier::FailureData data2 = VerifyMethods<false>(self,
320 linker,
321 dex_file,
322 class_def,
323 &it,
324 dex_cache,
325 class_loader,
326 callbacks,
327 allow_soft_failures,
Andreas Gampe7fe30232016-03-25 16:58:00 -0700328 log_level,
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800329 false /* need precise constants */,
330 error);
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800331
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800332 data1.Merge(data2);
333
Andreas Gampe6d7abbd2017-04-24 13:19:09 -0700334 if (data1.kind == FailureKind::kNoFailure) {
335 return FailureKind::kNoFailure;
jeffhaof1e6b7c2012-06-05 18:33:30 -0700336 } else {
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800337 if ((data1.types & VERIFY_ERROR_LOCKING) != 0) {
338 // Print a warning about expected slow-down. Use a string temporary to print one contiguous
339 // warning.
340 std::string tmp =
341 StringPrintf("Class %s failed lock verification and will run slower.",
David Brazdil15fc7292016-09-02 14:13:18 +0100342 PrettyDescriptor(dex_file->GetClassDescriptor(class_def)).c_str());
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800343 if (!gPrintedDxMonitorText) {
344 tmp = tmp + "\nCommon causes for lock verification issues are non-optimized dex code\n"
345 "and incorrect proguard optimizations.";
346 gPrintedDxMonitorText = true;
347 }
348 LOG(WARNING) << tmp;
349 }
350 return data1.kind;
jeffhaof1e6b7c2012-06-05 18:33:30 -0700351 }
jeffhaof56197c2012-03-05 18:01:54 -0800352}
353
Andreas Gampea4f5bf62015-05-18 20:50:12 -0700354static bool IsLargeMethod(const DexFile::CodeItem* const code_item) {
Andreas Gampe3c651fc2015-05-21 14:06:46 -0700355 if (code_item == nullptr) {
356 return false;
357 }
358
Andreas Gampea4f5bf62015-05-18 20:50:12 -0700359 uint16_t registers_size = code_item->registers_size_;
360 uint32_t insns_size = code_item->insns_size_in_code_units_;
361
362 return registers_size * insns_size > 4*1024*1024;
363}
364
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800365MethodVerifier::FailureData MethodVerifier::VerifyMethod(Thread* self,
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800366 uint32_t method_idx,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800367 const DexFile* dex_file,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700368 Handle<mirror::DexCache> dex_cache,
369 Handle<mirror::ClassLoader> class_loader,
David Brazdil15fc7292016-09-02 14:13:18 +0100370 const DexFile::ClassDef& class_def,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800371 const DexFile::CodeItem* code_item,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700372 ArtMethod* method,
Jeff Haoee988952013-04-16 14:23:47 -0700373 uint32_t method_access_flags,
Andreas Gampe53e32d12015-12-09 21:03:23 -0800374 CompilerCallbacks* callbacks,
Ian Rogers46960fe2014-05-23 10:43:43 -0700375 bool allow_soft_failures,
Andreas Gampe5fd66d02016-09-12 20:22:19 -0700376 HardFailLogMode log_level,
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800377 bool need_precise_constants,
378 std::string* hard_failure_msg) {
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800379 MethodVerifier::FailureData result;
Mathieu Chartier8e219ae2014-08-19 14:29:46 -0700380 uint64_t start_ns = kTimeVerifyMethod ? NanoTime() : 0;
Ian Rogersc8982582012-09-07 16:53:25 -0700381
Andreas Gampebf9611f2016-03-25 16:58:00 -0700382 MethodVerifier verifier(self,
383 dex_file,
384 dex_cache,
385 class_loader,
386 class_def,
387 code_item,
388 method_idx,
389 method,
390 method_access_flags,
391 true /* can_load_classes */,
392 allow_soft_failures,
393 need_precise_constants,
394 false /* verify to dump */,
395 true /* allow_thread_suspension */);
Ian Rogers46960fe2014-05-23 10:43:43 -0700396 if (verifier.Verify()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700397 // Verification completed, however failures may be pending that didn't cause the verification
398 // to hard fail.
Ian Rogers46960fe2014-05-23 10:43:43 -0700399 CHECK(!verifier.have_pending_hard_failure_);
Andreas Gampe53e32d12015-12-09 21:03:23 -0800400
401 if (code_item != nullptr && callbacks != nullptr) {
402 // Let the interested party know that the method was verified.
403 callbacks->MethodVerified(&verifier);
404 }
405
Ian Rogers46960fe2014-05-23 10:43:43 -0700406 if (verifier.failures_.size() != 0) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -0700407 if (VLOG_IS_ON(verifier)) {
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800408 verifier.DumpFailures(VLOG_STREAM(verifier) << "Soft verification failures in "
David Sehr709b0702016-10-13 09:12:37 -0700409 << dex_file->PrettyMethod(method_idx) << "\n");
Anwar Ghuloum75a43f12013-08-13 17:22:14 -0700410 }
Andreas Gampe6d7abbd2017-04-24 13:19:09 -0700411 result.kind = FailureKind::kSoftFailure;
Nicolas Geoffray250a3782016-04-20 16:27:53 +0100412 if (method != nullptr &&
413 !CanCompilerHandleVerificationFailure(verifier.encountered_failure_types_)) {
Alex Lightfcbafb32017-02-02 15:09:54 -0800414 method->SetDontCompile();
Nicolas Geoffray250a3782016-04-20 16:27:53 +0100415 }
416 }
Andreas Gampe56fdd0e2016-04-28 14:56:54 -0700417 if (method != nullptr) {
418 if (verifier.HasInstructionThatWillThrow()) {
Alex Lightfcbafb32017-02-02 15:09:54 -0800419 method->SetDontCompile();
Calin Juravle857f0582016-12-20 14:36:59 +0000420 if (Runtime::Current()->IsAotCompiler() &&
421 (callbacks != nullptr) && !callbacks->IsBootImage()) {
422 // When compiling apps, make HasInstructionThatWillThrow a soft error to trigger
423 // re-verification at runtime.
424 // The dead code after the throw is not verified and might be invalid. This may cause
425 // the JIT compiler to crash since it assumes that all the code is valid.
426 //
427 // There's a strong assumption that the entire boot image is verified and all its dex
428 // code is valid (even the dead and unverified one). As such this is done only for apps.
429 // (CompilerDriver DCHECKs in VerifyClassVisitor that methods from boot image are
430 // fully verified).
Andreas Gampe6d7abbd2017-04-24 13:19:09 -0700431 result.kind = FailureKind::kSoftFailure;
Calin Juravle857f0582016-12-20 14:36:59 +0000432 }
Andreas Gampe56fdd0e2016-04-28 14:56:54 -0700433 }
434 if ((verifier.encountered_failure_types_ & VerifyError::VERIFY_ERROR_LOCKING) != 0) {
Orion Hodsoncfcc9cf2017-09-29 15:07:27 +0100435 method->SetMustCountLocks();
Andreas Gampe56fdd0e2016-04-28 14:56:54 -0700436 }
jeffhaof56197c2012-03-05 18:01:54 -0800437 }
438 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700439 // Bad method data.
Ian Rogers46960fe2014-05-23 10:43:43 -0700440 CHECK_NE(verifier.failures_.size(), 0U);
Igor Murashkin4d7b75f2015-07-21 17:03:36 -0700441
442 if (UNLIKELY(verifier.have_pending_experimental_failure_)) {
443 // Failed due to being forced into interpreter. This is ok because
444 // we just want to skip verification.
Andreas Gampe6d7abbd2017-04-24 13:19:09 -0700445 result.kind = FailureKind::kSoftFailure;
Igor Murashkin4d7b75f2015-07-21 17:03:36 -0700446 } else {
447 CHECK(verifier.have_pending_hard_failure_);
Andreas Gampe7fe30232016-03-25 16:58:00 -0700448 if (VLOG_IS_ON(verifier)) {
Andreas Gampe5fd66d02016-09-12 20:22:19 -0700449 log_level = std::max(HardFailLogMode::kLogVerbose, log_level);
Andreas Gampe7fe30232016-03-25 16:58:00 -0700450 }
Andreas Gampe5fd66d02016-09-12 20:22:19 -0700451 if (log_level >= HardFailLogMode::kLogVerbose) {
452 LogSeverity severity;
453 switch (log_level) {
454 case HardFailLogMode::kLogVerbose:
455 severity = LogSeverity::VERBOSE;
456 break;
457 case HardFailLogMode::kLogWarning:
458 severity = LogSeverity::WARNING;
459 break;
460 case HardFailLogMode::kLogInternalFatal:
Andreas Gampe3fec9ac2016-09-13 10:47:28 -0700461 severity = LogSeverity::FATAL_WITHOUT_ABORT;
Andreas Gampe5fd66d02016-09-12 20:22:19 -0700462 break;
463 default:
464 LOG(FATAL) << "Unsupported log-level " << static_cast<uint32_t>(log_level);
465 UNREACHABLE();
466 }
Andreas Gampe3fec9ac2016-09-13 10:47:28 -0700467 verifier.DumpFailures(LOG_STREAM(severity) << "Verification error in "
David Sehr709b0702016-10-13 09:12:37 -0700468 << dex_file->PrettyMethod(method_idx)
Andreas Gampe3fec9ac2016-09-13 10:47:28 -0700469 << "\n");
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800470 }
471 if (hard_failure_msg != nullptr) {
472 CHECK(!verifier.failure_messages_.empty());
473 *hard_failure_msg =
474 verifier.failure_messages_[verifier.failure_messages_.size() - 1]->str();
475 }
Andreas Gampe6d7abbd2017-04-24 13:19:09 -0700476 result.kind = FailureKind::kHardFailure;
Andreas Gampe53e32d12015-12-09 21:03:23 -0800477
478 if (callbacks != nullptr) {
479 // Let the interested party know that we failed the class.
David Brazdil15fc7292016-09-02 14:13:18 +0100480 ClassReference ref(dex_file, dex_file->GetIndexForClassDef(class_def));
Andreas Gampe53e32d12015-12-09 21:03:23 -0800481 callbacks->ClassRejected(ref);
482 }
Igor Murashkin4d7b75f2015-07-21 17:03:36 -0700483 }
Andreas Gampe53e32d12015-12-09 21:03:23 -0800484 if (VLOG_IS_ON(verifier)) {
Ian Rogers46960fe2014-05-23 10:43:43 -0700485 std::cout << "\n" << verifier.info_messages_.str();
486 verifier.Dump(std::cout);
jeffhaof56197c2012-03-05 18:01:54 -0800487 }
jeffhaof56197c2012-03-05 18:01:54 -0800488 }
Mathieu Chartier8e219ae2014-08-19 14:29:46 -0700489 if (kTimeVerifyMethod) {
490 uint64_t duration_ns = NanoTime() - start_ns;
491 if (duration_ns > MsToNs(100)) {
David Sehr709b0702016-10-13 09:12:37 -0700492 LOG(WARNING) << "Verification of " << dex_file->PrettyMethod(method_idx)
Andreas Gampea4f5bf62015-05-18 20:50:12 -0700493 << " took " << PrettyDuration(duration_ns)
494 << (IsLargeMethod(code_item) ? " (large method)" : "");
Mathieu Chartier8e219ae2014-08-19 14:29:46 -0700495 }
Ian Rogersc8982582012-09-07 16:53:25 -0700496 }
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800497 result.types = verifier.encountered_failure_types_;
Ian Rogersc8982582012-09-07 16:53:25 -0700498 return result;
jeffhaof56197c2012-03-05 18:01:54 -0800499}
500
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100501MethodVerifier* MethodVerifier::VerifyMethodAndDump(Thread* self,
502 VariableIndentationOutputStream* vios,
503 uint32_t dex_method_idx,
504 const DexFile* dex_file,
505 Handle<mirror::DexCache> dex_cache,
506 Handle<mirror::ClassLoader> class_loader,
David Brazdil15fc7292016-09-02 14:13:18 +0100507 const DexFile::ClassDef& class_def,
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100508 const DexFile::CodeItem* code_item,
509 ArtMethod* method,
510 uint32_t method_access_flags) {
Andreas Gampebf9611f2016-03-25 16:58:00 -0700511 MethodVerifier* verifier = new MethodVerifier(self,
512 dex_file,
513 dex_cache,
514 class_loader,
515 class_def,
516 code_item,
517 dex_method_idx,
518 method,
519 method_access_flags,
520 true /* can_load_classes */,
521 true /* allow_soft_failures */,
522 true /* need_precise_constants */,
523 true /* verify_to_dump */,
524 true /* allow_thread_suspension */);
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700525 verifier->Verify();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100526 verifier->DumpFailures(vios->Stream());
527 vios->Stream() << verifier->info_messages_.str();
Andreas Gampe5cbcde22014-09-16 14:59:49 -0700528 // Only dump and return if no hard failures. Otherwise the verifier may be not fully initialized
529 // and querying any info is dangerous/can abort.
530 if (verifier->have_pending_hard_failure_) {
531 delete verifier;
532 return nullptr;
533 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100534 verifier->Dump(vios);
Andreas Gampe5cbcde22014-09-16 14:59:49 -0700535 return verifier;
536 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800537}
538
Ian Rogers7b078e82014-09-10 14:44:24 -0700539MethodVerifier::MethodVerifier(Thread* self,
Andreas Gampe53e32d12015-12-09 21:03:23 -0800540 const DexFile* dex_file,
541 Handle<mirror::DexCache> dex_cache,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700542 Handle<mirror::ClassLoader> class_loader,
David Brazdil15fc7292016-09-02 14:13:18 +0100543 const DexFile::ClassDef& class_def,
Andreas Gampe53e32d12015-12-09 21:03:23 -0800544 const DexFile::CodeItem* code_item,
545 uint32_t dex_method_idx,
546 ArtMethod* method,
547 uint32_t method_access_flags,
548 bool can_load_classes,
549 bool allow_soft_failures,
550 bool need_precise_constants,
551 bool verify_to_dump,
Mathieu Chartier4306ef82014-12-19 18:41:47 -0800552 bool allow_thread_suspension)
Ian Rogers7b078e82014-09-10 14:44:24 -0700553 : self_(self),
Mathieu Chartierde40d472015-10-15 17:47:48 -0700554 arena_stack_(Runtime::Current()->GetArenaPool()),
Vladimir Marko69d310e2017-10-09 14:12:23 +0100555 allocator_(&arena_stack_),
Alex Lighte5179ce2017-10-31 22:28:11 +0000556 reg_types_(can_load_classes, allocator_),
Vladimir Marko69d310e2017-10-09 14:12:23 +0100557 reg_table_(allocator_),
Andreas Gampee2abbc62017-09-15 11:59:26 -0700558 work_insn_idx_(dex::kDexNoIndex),
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800559 dex_method_idx_(dex_method_idx),
Ian Rogers637c65b2013-05-31 11:46:00 -0700560 mirror_method_(method),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700561 method_access_flags_(method_access_flags),
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700562 return_type_(nullptr),
jeffhaof56197c2012-03-05 18:01:54 -0800563 dex_file_(dex_file),
564 dex_cache_(dex_cache),
565 class_loader_(class_loader),
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700566 class_def_(class_def),
jeffhaof56197c2012-03-05 18:01:54 -0800567 code_item_(code_item),
Ian Rogers7b078e82014-09-10 14:44:24 -0700568 declaring_class_(nullptr),
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700569 interesting_dex_pc_(-1),
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700570 monitor_enter_dex_pcs_(nullptr),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700571 have_pending_hard_failure_(false),
jeffhaofaf459e2012-08-31 15:32:47 -0700572 have_pending_runtime_throw_failure_(false),
Igor Murashkin4d7b75f2015-07-21 17:03:36 -0700573 have_pending_experimental_failure_(false),
Andreas Gamped12e7822015-06-25 10:26:40 -0700574 have_any_pending_runtime_throw_failure_(false),
jeffhaof56197c2012-03-05 18:01:54 -0800575 new_instance_count_(0),
Elliott Hughes80537bb2013-01-04 16:37:26 -0800576 monitor_enter_count_(0),
Andreas Gampe0760a812015-08-26 17:12:51 -0700577 encountered_failure_types_(0),
Jeff Haoee988952013-04-16 14:23:47 -0700578 can_load_classes_(can_load_classes),
Sebastien Hertz4d4adb12013-07-24 16:14:19 +0200579 allow_soft_failures_(allow_soft_failures),
Ian Rogers46960fe2014-05-23 10:43:43 -0700580 need_precise_constants_(need_precise_constants),
Sebastien Hertz4d4adb12013-07-24 16:14:19 +0200581 has_check_casts_(false),
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700582 has_virtual_or_interface_invokes_(false),
Mathieu Chartier4306ef82014-12-19 18:41:47 -0800583 verify_to_dump_(verify_to_dump),
Andreas Gamped5ad72f2015-06-26 17:33:47 -0700584 allow_thread_suspension_(allow_thread_suspension),
Andreas Gampee6215c02015-08-31 18:54:38 -0700585 is_constructor_(false),
Andreas Gamped5ad72f2015-06-26 17:33:47 -0700586 link_(nullptr) {
Mathieu Chartierd0ad2ee2015-03-31 14:59:59 -0700587 self->PushVerifier(this);
jeffhaof56197c2012-03-05 18:01:54 -0800588}
589
Mathieu Chartier590fee92013-09-13 13:46:47 -0700590MethodVerifier::~MethodVerifier() {
Mathieu Chartierd0ad2ee2015-03-31 14:59:59 -0700591 Thread::Current()->PopVerifier(this);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700592 STLDeleteElements(&failure_messages_);
593}
594
Mathieu Chartiere401d142015-04-22 13:56:20 -0700595void MethodVerifier::FindLocksAtDexPc(ArtMethod* m, uint32_t dex_pc,
Ian Rogers46960fe2014-05-23 10:43:43 -0700596 std::vector<uint32_t>* monitor_enter_dex_pcs) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700597 StackHandleScope<2> hs(Thread::Current());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700598 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache()));
599 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader()));
Andreas Gampebf9611f2016-03-25 16:58:00 -0700600 MethodVerifier verifier(hs.Self(),
601 m->GetDexFile(),
602 dex_cache,
603 class_loader,
David Brazdil15fc7292016-09-02 14:13:18 +0100604 m->GetClassDef(),
Andreas Gampebf9611f2016-03-25 16:58:00 -0700605 m->GetCodeItem(),
606 m->GetDexMethodIndex(),
607 m,
608 m->GetAccessFlags(),
609 false /* can_load_classes */,
610 true /* allow_soft_failures */,
611 false /* need_precise_constants */,
612 false /* verify_to_dump */,
613 false /* allow_thread_suspension */);
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700614 verifier.interesting_dex_pc_ = dex_pc;
Ian Rogers46960fe2014-05-23 10:43:43 -0700615 verifier.monitor_enter_dex_pcs_ = monitor_enter_dex_pcs;
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700616 verifier.FindLocksAtDexPc();
617}
618
Andreas Gampecb3c08f2014-09-18 13:16:38 -0700619static bool HasMonitorEnterInstructions(const DexFile::CodeItem* const code_item) {
Mathieu Chartier1d2d4ff2017-09-23 16:11:06 -0700620 for (const Instruction& inst : code_item->Instructions()) {
621 if (inst.Opcode() == Instruction::MONITOR_ENTER) {
Andreas Gampecb3c08f2014-09-18 13:16:38 -0700622 return true;
623 }
Andreas Gampecb3c08f2014-09-18 13:16:38 -0700624 }
Andreas Gampecb3c08f2014-09-18 13:16:38 -0700625 return false;
626}
627
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700628void MethodVerifier::FindLocksAtDexPc() {
Ian Rogers7b078e82014-09-10 14:44:24 -0700629 CHECK(monitor_enter_dex_pcs_ != nullptr);
630 CHECK(code_item_ != nullptr); // This only makes sense for methods with code.
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700631
Andreas Gampecb3c08f2014-09-18 13:16:38 -0700632 // Quick check whether there are any monitor_enter instructions at all.
633 if (!HasMonitorEnterInstructions(code_item_)) {
634 return;
635 }
636
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700637 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
638 // verification. In practice, the phase we want relies on data structures set up by all the
639 // earlier passes, so we just run the full method verification and bail out early when we've
640 // got what we wanted.
641 Verify();
642}
643
Mathieu Chartiere401d142015-04-22 13:56:20 -0700644ArtField* MethodVerifier::FindAccessedFieldAtDexPc(ArtMethod* m, uint32_t dex_pc) {
645 StackHandleScope<2> hs(Thread::Current());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700646 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache()));
647 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader()));
Andreas Gampebf9611f2016-03-25 16:58:00 -0700648 MethodVerifier verifier(hs.Self(),
649 m->GetDexFile(),
650 dex_cache,
651 class_loader,
David Brazdil15fc7292016-09-02 14:13:18 +0100652 m->GetClassDef(),
Andreas Gampebf9611f2016-03-25 16:58:00 -0700653 m->GetCodeItem(),
654 m->GetDexMethodIndex(),
655 m,
656 m->GetAccessFlags(),
657 true /* can_load_classes */,
658 true /* allow_soft_failures */,
659 false /* need_precise_constants */,
660 false /* verify_to_dump */,
661 true /* allow_thread_suspension */);
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200662 return verifier.FindAccessedFieldAtDexPc(dex_pc);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200663}
664
Mathieu Chartierc7853442015-03-27 14:35:38 -0700665ArtField* MethodVerifier::FindAccessedFieldAtDexPc(uint32_t dex_pc) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700666 CHECK(code_item_ != nullptr); // This only makes sense for methods with code.
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200667
668 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
669 // verification. In practice, the phase we want relies on data structures set up by all the
670 // earlier passes, so we just run the full method verification and bail out early when we've
671 // got what we wanted.
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200672 bool success = Verify();
673 if (!success) {
Ian Rogers9bc54402014-04-17 16:40:01 -0700674 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200675 }
676 RegisterLine* register_line = reg_table_.GetLine(dex_pc);
Ian Rogers7b078e82014-09-10 14:44:24 -0700677 if (register_line == nullptr) {
Ian Rogers9bc54402014-04-17 16:40:01 -0700678 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200679 }
Mathieu Chartier1d2d4ff2017-09-23 16:11:06 -0700680 const Instruction* inst = &code_item_->InstructionAt(dex_pc);
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200681 return GetQuickFieldAccess(inst, register_line);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200682}
683
Mathieu Chartiere401d142015-04-22 13:56:20 -0700684ArtMethod* MethodVerifier::FindInvokedMethodAtDexPc(ArtMethod* m, uint32_t dex_pc) {
685 StackHandleScope<2> hs(Thread::Current());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700686 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache()));
687 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader()));
Andreas Gampebf9611f2016-03-25 16:58:00 -0700688 MethodVerifier verifier(hs.Self(),
689 m->GetDexFile(),
690 dex_cache,
691 class_loader,
David Brazdil15fc7292016-09-02 14:13:18 +0100692 m->GetClassDef(),
Andreas Gampebf9611f2016-03-25 16:58:00 -0700693 m->GetCodeItem(),
694 m->GetDexMethodIndex(),
695 m,
696 m->GetAccessFlags(),
697 true /* can_load_classes */,
698 true /* allow_soft_failures */,
699 false /* need_precise_constants */,
700 false /* verify_to_dump */,
701 true /* allow_thread_suspension */);
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200702 return verifier.FindInvokedMethodAtDexPc(dex_pc);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200703}
704
Mathieu Chartiere401d142015-04-22 13:56:20 -0700705ArtMethod* MethodVerifier::FindInvokedMethodAtDexPc(uint32_t dex_pc) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700706 CHECK(code_item_ != nullptr); // This only makes sense for methods with code.
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200707
708 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
709 // verification. In practice, the phase we want relies on data structures set up by all the
710 // earlier passes, so we just run the full method verification and bail out early when we've
711 // got what we wanted.
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200712 bool success = Verify();
713 if (!success) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700714 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200715 }
716 RegisterLine* register_line = reg_table_.GetLine(dex_pc);
Ian Rogers7b078e82014-09-10 14:44:24 -0700717 if (register_line == nullptr) {
718 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200719 }
Mathieu Chartier1d2d4ff2017-09-23 16:11:06 -0700720 const Instruction* inst = &code_item_->InstructionAt(dex_pc);
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200721 const bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
Mathieu Chartier091d2382015-03-06 10:59:06 -0800722 return GetQuickInvokedMethod(inst, register_line, is_range, false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200723}
724
Ian Rogersad0b3a32012-04-16 14:50:24 -0700725bool MethodVerifier::Verify() {
Andreas Gampee6215c02015-08-31 18:54:38 -0700726 // Some older code doesn't correctly mark constructors as such. Test for this case by looking at
727 // the name.
728 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
729 const char* method_name = dex_file_->StringDataByIdx(method_id.name_idx_);
730 bool instance_constructor_by_name = strcmp("<init>", method_name) == 0;
731 bool static_constructor_by_name = strcmp("<clinit>", method_name) == 0;
732 bool constructor_by_name = instance_constructor_by_name || static_constructor_by_name;
733 // Check that only constructors are tagged, and check for bad code that doesn't tag constructors.
734 if ((method_access_flags_ & kAccConstructor) != 0) {
735 if (!constructor_by_name) {
736 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
737 << "method is marked as constructor, but not named accordingly";
jeffhaobdb76512011-09-07 11:43:16 -0700738 return false;
Andreas Gampee6215c02015-08-31 18:54:38 -0700739 }
740 is_constructor_ = true;
741 } else if (constructor_by_name) {
David Sehr709b0702016-10-13 09:12:37 -0700742 LOG(WARNING) << "Method " << dex_file_->PrettyMethod(dex_method_idx_)
Andreas Gampee6215c02015-08-31 18:54:38 -0700743 << " not marked as constructor.";
744 is_constructor_ = true;
745 }
746 // If it's a constructor, check whether IsStatic() matches the name.
747 // This should have been rejected by the dex file verifier. Only do in debug build.
748 if (kIsDebugBuild) {
749 if (IsConstructor()) {
750 if (IsStatic() ^ static_constructor_by_name) {
751 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
752 << "constructor name doesn't match static flag";
753 return false;
754 }
jeffhaobdb76512011-09-07 11:43:16 -0700755 }
jeffhaobdb76512011-09-07 11:43:16 -0700756 }
Andreas Gampee6215c02015-08-31 18:54:38 -0700757
758 // Methods may only have one of public/protected/private.
759 // This should have been rejected by the dex file verifier. Only do in debug build.
760 if (kIsDebugBuild) {
761 size_t access_mod_count =
762 (((method_access_flags_ & kAccPublic) == 0) ? 0 : 1) +
763 (((method_access_flags_ & kAccProtected) == 0) ? 0 : 1) +
764 (((method_access_flags_ & kAccPrivate) == 0) ? 0 : 1);
765 if (access_mod_count > 1) {
766 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "method has more than one of public/protected/private";
767 return false;
768 }
769 }
770
771 // If there aren't any instructions, make sure that's expected, then exit successfully.
772 if (code_item_ == nullptr) {
Andreas Gampe76ed99d2016-03-28 18:31:29 -0700773 // Only native or abstract methods may not have code.
774 if ((method_access_flags_ & (kAccNative | kAccAbstract)) == 0) {
775 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "zero-length code in concrete non-native method";
776 return false;
777 }
778
Andreas Gampee6215c02015-08-31 18:54:38 -0700779 // This should have been rejected by the dex file verifier. Only do in debug build.
Andreas Gampe76ed99d2016-03-28 18:31:29 -0700780 // Note: the above will also be rejected in the dex file verifier, starting in dex version 37.
Andreas Gampee6215c02015-08-31 18:54:38 -0700781 if (kIsDebugBuild) {
Andreas Gampee6215c02015-08-31 18:54:38 -0700782 if ((method_access_flags_ & kAccAbstract) != 0) {
783 // Abstract methods are not allowed to have the following flags.
784 static constexpr uint32_t kForbidden =
785 kAccPrivate |
786 kAccStatic |
787 kAccFinal |
788 kAccNative |
789 kAccStrict |
790 kAccSynchronized;
791 if ((method_access_flags_ & kForbidden) != 0) {
792 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
793 << "method can't be abstract and private/static/final/native/strict/synchronized";
794 return false;
795 }
796 }
David Brazdil15fc7292016-09-02 14:13:18 +0100797 if ((class_def_.GetJavaAccessFlags() & kAccInterface) != 0) {
Alex Lighteb7c1442015-08-31 13:17:42 -0700798 // Interface methods must be public and abstract (if default methods are disabled).
Neil Fuller9724c632016-01-07 15:42:47 +0000799 uint32_t kRequired = kAccPublic;
Alex Lighteb7c1442015-08-31 13:17:42 -0700800 if ((method_access_flags_ & kRequired) != kRequired) {
Neil Fuller9724c632016-01-07 15:42:47 +0000801 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface methods must be public";
Andreas Gampee6215c02015-08-31 18:54:38 -0700802 return false;
803 }
804 // In addition to the above, interface methods must not be protected.
805 static constexpr uint32_t kForbidden = kAccProtected;
806 if ((method_access_flags_ & kForbidden) != 0) {
807 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface methods can't be protected";
808 return false;
809 }
810 }
811 // We also don't allow constructors to be abstract or native.
812 if (IsConstructor()) {
813 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "constructors can't be abstract or native";
814 return false;
815 }
816 }
817 return true;
818 }
819
820 // This should have been rejected by the dex file verifier. Only do in debug build.
821 if (kIsDebugBuild) {
822 // When there's code, the method must not be native or abstract.
823 if ((method_access_flags_ & (kAccNative | kAccAbstract)) != 0) {
824 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "non-zero-length code in abstract or native method";
825 return false;
826 }
827
David Brazdil15fc7292016-09-02 14:13:18 +0100828 if ((class_def_.GetJavaAccessFlags() & kAccInterface) != 0) {
Alex Lighteb7c1442015-08-31 13:17:42 -0700829 // Interfaces may always have static initializers for their fields. If we are running with
830 // default methods enabled we also allow other public, static, non-final methods to have code.
831 // Otherwise that is the only type of method allowed.
Alex Light0db36b32015-10-27 14:06:34 -0700832 if (!(IsConstructor() && IsStatic())) {
Neil Fuller9724c632016-01-07 15:42:47 +0000833 if (IsInstanceConstructor()) {
834 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interfaces may not have non-static constructor";
835 return false;
836 } else if (method_access_flags_ & kAccFinal) {
837 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interfaces may not have final methods";
838 return false;
Alex Lightb55f1ac2016-04-12 15:50:55 -0700839 } else {
840 uint32_t access_flag_options = kAccPublic;
841 if (dex_file_->GetVersion() >= DexFile::kDefaultMethodsVersion) {
842 access_flag_options |= kAccPrivate;
843 }
844 if (!(method_access_flags_ & access_flag_options)) {
845 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
846 << "interfaces may not have protected or package-private members";
847 return false;
848 }
Alex Lighteb7c1442015-08-31 13:17:42 -0700849 }
Andreas Gampee6215c02015-08-31 18:54:38 -0700850 }
851 }
852
853 // Instance constructors must not be synchronized.
854 if (IsInstanceConstructor()) {
855 static constexpr uint32_t kForbidden = kAccSynchronized;
856 if ((method_access_flags_ & kForbidden) != 0) {
857 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "constructors can't be synchronized";
858 return false;
859 }
860 }
861 }
862
Ian Rogersd81871c2011-10-03 13:57:23 -0700863 // Sanity-check the register counts. ins + locals = registers, so make sure that ins <= registers.
864 if (code_item_->ins_size_ > code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700865 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad register counts (ins=" << code_item_->ins_size_
866 << " regs=" << code_item_->registers_size_;
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.
Vladimir Marko69d310e2017-10-09 14:12:23 +0100871 insn_flags_.reset(allocator_.AllocArray<InstructionFlags>(code_item_->insns_size_in_code_units_));
Mathieu Chartierde40d472015-10-15 17:47:48 -0700872 DCHECK(insn_flags_ != nullptr);
873 std::uninitialized_fill_n(insn_flags_.get(),
874 code_item_->insns_size_in_code_units_,
875 InstructionFlags());
Ian Rogersd81871c2011-10-03 13:57:23 -0700876 // Run through the instructions and see if the width checks out.
877 bool result = ComputeWidthsAndCountOps();
Andreas Gampebf1cb772017-05-15 15:39:00 -0700878 bool allow_runtime_only_instructions = !Runtime::Current()->IsAotCompiler() || verify_to_dump_;
Ian Rogersd81871c2011-10-03 13:57:23 -0700879 // Flag instructions guarded by a "try" block and check exception handlers.
880 result = result && ScanTryCatchBlocks();
881 // Perform static instruction verification.
Andreas Gampebf1cb772017-05-15 15:39:00 -0700882 result = result && (allow_runtime_only_instructions
883 ? VerifyInstructions<true>()
884 : VerifyInstructions<false>());
Ian Rogersad0b3a32012-04-16 14:50:24 -0700885 // Perform code-flow analysis and return.
Vladimir Marko2b5eaa22013-12-13 13:59:30 +0000886 result = result && VerifyCodeFlow();
Andreas Gampe53e32d12015-12-09 21:03:23 -0800887
Vladimir Marko2b5eaa22013-12-13 13:59:30 +0000888 return result;
jeffhaoba5ebb92011-08-25 17:24:37 -0700889}
890
Ian Rogers776ac1f2012-04-13 23:36:36 -0700891std::ostream& MethodVerifier::Fail(VerifyError error) {
Andreas Gampe0760a812015-08-26 17:12:51 -0700892 // Mark the error type as encountered.
Andreas Gampea727e372015-08-25 09:22:37 -0700893 encountered_failure_types_ |= static_cast<uint32_t>(error);
Andreas Gampe0760a812015-08-26 17:12:51 -0700894
Ian Rogersad0b3a32012-04-16 14:50:24 -0700895 switch (error) {
896 case VERIFY_ERROR_NO_CLASS:
897 case VERIFY_ERROR_NO_FIELD:
898 case VERIFY_ERROR_NO_METHOD:
899 case VERIFY_ERROR_ACCESS_CLASS:
900 case VERIFY_ERROR_ACCESS_FIELD:
901 case VERIFY_ERROR_ACCESS_METHOD:
Ian Rogers08f753d2012-08-24 14:35:25 -0700902 case VERIFY_ERROR_INSTANTIATION:
903 case VERIFY_ERROR_CLASS_CHANGE:
Igor Murashkin158f35c2015-06-10 15:55:30 -0700904 case VERIFY_ERROR_FORCE_INTERPRETER:
Andreas Gampea727e372015-08-25 09:22:37 -0700905 case VERIFY_ERROR_LOCKING:
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800906 if (Runtime::Current()->IsAotCompiler() || !can_load_classes_) {
jeffhaofaf459e2012-08-31 15:32:47 -0700907 // If we're optimistically running verification at compile time, turn NO_xxx, ACCESS_xxx,
908 // class change and instantiation errors into soft verification errors so that we re-verify
909 // at runtime. We may fail to find or to agree on access because of not yet available class
910 // loaders, or class loaders that will differ at runtime. In these cases, we don't want to
911 // affect the soundness of the code being compiled. Instead, the generated code runs "slow
912 // paths" that dynamically perform the verification and cause the behavior to be that akin
913 // to an interpreter.
914 error = VERIFY_ERROR_BAD_CLASS_SOFT;
915 } else {
Jeff Haoa3faaf42013-09-03 19:07:00 -0700916 // If we fail again at runtime, mark that this instruction would throw and force this
917 // method to be executed using the interpreter with checks.
jeffhaofaf459e2012-08-31 15:32:47 -0700918 have_pending_runtime_throw_failure_ = true;
Andreas Gamped7f8d052015-03-12 11:05:47 -0700919
920 // We need to save the work_line if the instruction wasn't throwing before. Otherwise we'll
921 // try to merge garbage.
922 // Note: this assumes that Fail is called before we do any work_line modifications.
Andreas Gamped5ad72f2015-06-26 17:33:47 -0700923 // Note: this can fail before we touch any instruction, for the signature of a method. So
924 // add a check.
Andreas Gampee2abbc62017-09-15 11:59:26 -0700925 if (work_insn_idx_ < dex::kDexNoIndex) {
Mathieu Chartier1d2d4ff2017-09-23 16:11:06 -0700926 const Instruction& inst = code_item_->InstructionAt(work_insn_idx_);
927 int opcode_flags = Instruction::FlagsOf(inst.Opcode());
Andreas Gamped7f8d052015-03-12 11:05:47 -0700928
Andreas Gamped5ad72f2015-06-26 17:33:47 -0700929 if ((opcode_flags & Instruction::kThrow) == 0 && CurrentInsnFlags()->IsInTry()) {
930 saved_line_->CopyFromLine(work_line_.get());
931 }
Andreas Gamped7f8d052015-03-12 11:05:47 -0700932 }
jeffhaofaf459e2012-08-31 15:32:47 -0700933 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700934 break;
Andreas Gampea727e372015-08-25 09:22:37 -0700935
Ian Rogersad0b3a32012-04-16 14:50:24 -0700936 // Indication that verification should be retried at runtime.
937 case VERIFY_ERROR_BAD_CLASS_SOFT:
Jeff Haoee988952013-04-16 14:23:47 -0700938 if (!allow_soft_failures_) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700939 have_pending_hard_failure_ = true;
940 }
941 break;
Andreas Gampea727e372015-08-25 09:22:37 -0700942
jeffhaod5347e02012-03-22 17:25:05 -0700943 // Hard verification failures at compile time will still fail at runtime, so the class is
944 // marked as rejected to prevent it from being compiled.
Ian Rogersad0b3a32012-04-16 14:50:24 -0700945 case VERIFY_ERROR_BAD_CLASS_HARD: {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700946 have_pending_hard_failure_ = true;
Andreas Gampeebf850c2015-08-14 15:37:35 -0700947 if (VLOG_IS_ON(verifier) && kDumpRegLinesOnHardFailureIfVLOG) {
948 ScopedObjectAccess soa(Thread::Current());
949 std::ostringstream oss;
950 Dump(oss);
951 LOG(ERROR) << oss.str();
952 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700953 break;
Ian Rogers47a05882012-02-03 12:23:33 -0800954 }
955 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700956 failures_.push_back(error);
David Sehr709b0702016-10-13 09:12:37 -0700957 std::string location(StringPrintf("%s: [0x%X] ", dex_file_->PrettyMethod(dex_method_idx_).c_str(),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700958 work_insn_idx_));
Elena Sayapina78480ec2014-08-15 15:52:42 +0700959 std::ostringstream* failure_message = new std::ostringstream(location, std::ostringstream::ate);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700960 failure_messages_.push_back(failure_message);
961 return *failure_message;
962}
963
Ian Rogers576ca0c2014-06-06 15:58:22 -0700964std::ostream& MethodVerifier::LogVerifyInfo() {
David Sehr709b0702016-10-13 09:12:37 -0700965 return info_messages_ << "VFY: " << dex_file_->PrettyMethod(dex_method_idx_)
Ian Rogers576ca0c2014-06-06 15:58:22 -0700966 << '[' << reinterpret_cast<void*>(work_insn_idx_) << "] : ";
967}
968
Ian Rogersad0b3a32012-04-16 14:50:24 -0700969void MethodVerifier::PrependToLastFailMessage(std::string prepend) {
970 size_t failure_num = failure_messages_.size();
971 DCHECK_NE(failure_num, 0U);
972 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
973 prepend += last_fail_message->str();
Elena Sayapina78480ec2014-08-15 15:52:42 +0700974 failure_messages_[failure_num - 1] = new std::ostringstream(prepend, std::ostringstream::ate);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700975 delete last_fail_message;
976}
977
Vladimir Marko5c657fe2016-11-03 15:12:29 +0000978void MethodVerifier::AppendToLastFailMessage(const std::string& append) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700979 size_t failure_num = failure_messages_.size();
980 DCHECK_NE(failure_num, 0U);
981 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
982 (*last_fail_message) << append;
Ian Rogers47a05882012-02-03 12:23:33 -0800983}
984
Ian Rogers776ac1f2012-04-13 23:36:36 -0700985bool MethodVerifier::ComputeWidthsAndCountOps() {
jeffhaobdb76512011-09-07 11:43:16 -0700986 size_t new_instance_count = 0;
987 size_t monitor_enter_count = 0;
jeffhaobdb76512011-09-07 11:43:16 -0700988
Mathieu Chartier1d2d4ff2017-09-23 16:11:06 -0700989 IterationRange<DexInstructionIterator> instructions = code_item_->Instructions();
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.
992 SafeDexInstructionIterator it(instructions.begin(), instructions.end());
993 for ( ; !it.IsErrorState() && it < instructions.end(); ++it) {
994 // In case the instruction goes past the end of the code item, make sure to not process it.
995 SafeDexInstructionIterator next = it;
996 ++next;
997 if (next.IsErrorState() || next > instructions.end()) {
998 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 Chartieraf7c9022017-10-27 09:42:46 -07001021 GetInstructionFlags(it.GetDexPC(instructions.begin())).SetIsOpcode();
jeffhaobdb76512011-09-07 11:43:16 -07001022 }
1023
Mathieu Chartieraf7c9022017-10-27 09:42:46 -07001024 if (it != instructions.end()) {
Mathieu Chartier1d2d4ff2017-09-23 16:11:06 -07001025 const size_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaod5347e02012-03-22 17:25:05 -07001026 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "code did not end where expected ("
Mathieu Chartieraf7c9022017-10-27 09:42:46 -07001027 << it.GetDexPC(instructions.begin()) << " vs. "
Mathieu Chartier1d2d4ff2017-09-23 16:11:06 -07001028 << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -07001029 return false;
1030 }
1031
Ian Rogersd81871c2011-10-03 13:57:23 -07001032 new_instance_count_ = new_instance_count;
1033 monitor_enter_count_ = monitor_enter_count;
jeffhaobdb76512011-09-07 11:43:16 -07001034 return true;
1035}
1036
Ian Rogers776ac1f2012-04-13 23:36:36 -07001037bool MethodVerifier::ScanTryCatchBlocks() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001038 uint32_t tries_size = code_item_->tries_size_;
jeffhaobdb76512011-09-07 11:43:16 -07001039 if (tries_size == 0) {
1040 return true;
1041 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001042 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Ian Rogers0571d352011-11-03 19:51:38 -07001043 const DexFile::TryItem* tries = DexFile::GetTryItems(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -07001044
1045 for (uint32_t idx = 0; idx < tries_size; idx++) {
1046 const DexFile::TryItem* try_item = &tries[idx];
1047 uint32_t start = try_item->start_addr_;
1048 uint32_t end = start + try_item->insn_count_;
jeffhaobdb76512011-09-07 11:43:16 -07001049 if ((start >= end) || (start >= insns_size) || (end > insns_size)) {
jeffhaod5347e02012-03-22 17:25:05 -07001050 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad exception entry: startAddr=" << start
1051 << " endAddr=" << end << " (size=" << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -07001052 return false;
1053 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07001054 if (!GetInstructionFlags(start).IsOpcode()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001055 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1056 << "'try' block starts inside an instruction (" << start << ")";
jeffhaobdb76512011-09-07 11:43:16 -07001057 return false;
1058 }
Ian Rogers7b078e82014-09-10 14:44:24 -07001059 uint32_t dex_pc = start;
1060 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
1061 while (dex_pc < end) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07001062 GetInstructionFlags(dex_pc).SetInTry();
Ian Rogers7b078e82014-09-10 14:44:24 -07001063 size_t insn_size = inst->SizeInCodeUnits();
1064 dex_pc += insn_size;
1065 inst = inst->RelativeAt(insn_size);
jeffhaobdb76512011-09-07 11:43:16 -07001066 }
1067 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -08001068 // Iterate over each of the handlers to verify target addresses.
Ian Rogers13735952014-10-08 12:43:28 -07001069 const uint8_t* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -07001070 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
Ian Rogers28ad40d2011-10-27 15:19:26 -07001071 ClassLinker* linker = Runtime::Current()->GetClassLinker();
jeffhaobdb76512011-09-07 11:43:16 -07001072 for (uint32_t idx = 0; idx < handlers_size; idx++) {
Ian Rogers0571d352011-11-03 19:51:38 -07001073 CatchHandlerIterator iterator(handlers_ptr);
1074 for (; iterator.HasNext(); iterator.Next()) {
1075 uint32_t dex_pc= iterator.GetHandlerAddress();
Mathieu Chartierde40d472015-10-15 17:47:48 -07001076 if (!GetInstructionFlags(dex_pc).IsOpcode()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001077 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1078 << "exception handler starts at bad address (" << dex_pc << ")";
jeffhaobdb76512011-09-07 11:43:16 -07001079 return false;
1080 }
Stephen Kyle9bc61992014-09-22 13:53:15 +01001081 if (!CheckNotMoveResult(code_item_->insns_, dex_pc)) {
1082 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1083 << "exception handler begins with move-result* (" << dex_pc << ")";
1084 return false;
1085 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07001086 GetInstructionFlags(dex_pc).SetBranchTarget();
Ian Rogers28ad40d2011-10-27 15:19:26 -07001087 // Ensure exception types are resolved so that they don't need resolution to be delivered,
1088 // unresolved exception types will be ignored by exception delivery
Andreas Gampea5b09a62016-11-17 15:21:22 -08001089 if (iterator.GetHandlerTypeIndex().IsValid()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001090 mirror::Class* exception_type = linker->ResolveType(*dex_file_,
1091 iterator.GetHandlerTypeIndex(),
Mathieu Chartierbf99f772014-08-23 16:37:27 -07001092 dex_cache_, class_loader_);
Ian Rogers7b078e82014-09-10 14:44:24 -07001093 if (exception_type == nullptr) {
1094 DCHECK(self_->IsExceptionPending());
1095 self_->ClearException();
Ian Rogers28ad40d2011-10-27 15:19:26 -07001096 }
1097 }
jeffhaobdb76512011-09-07 11:43:16 -07001098 }
Ian Rogers0571d352011-11-03 19:51:38 -07001099 handlers_ptr = iterator.EndDataPointer();
jeffhaobdb76512011-09-07 11:43:16 -07001100 }
jeffhaobdb76512011-09-07 11:43:16 -07001101 return true;
1102}
1103
Andreas Gampebf1cb772017-05-15 15:39:00 -07001104template <bool kAllowRuntimeOnlyInstructions>
Ian Rogers776ac1f2012-04-13 23:36:36 -07001105bool MethodVerifier::VerifyInstructions() {
Ian Rogers0c7abda2012-09-19 13:33:42 -07001106 /* 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 -07001107 GetInstructionFlags(0).SetBranchTarget();
1108 GetInstructionFlags(0).SetCompileTimeInfoPoint();
Mathieu Chartier1d2d4ff2017-09-23 16:11:06 -07001109 IterationRange<DexInstructionIterator> instructions = code_item_->Instructions();
1110 for (auto inst = instructions.begin(); inst != instructions.end(); ++inst) {
1111 const uint32_t dex_pc = inst.GetDexPC(instructions.begin());
1112 if (!VerifyInstruction<kAllowRuntimeOnlyInstructions>(&*inst, dex_pc)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001113 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -07001114 return false;
1115 }
1116 /* Flag instructions that are garbage collection points */
Sameer Abu Asal02c42232013-04-30 12:09:45 -07001117 // All invoke points are marked as "Throw" points already.
1118 // We are relying on this to also count all the invokes as interesting.
Vladimir Marko8b858e12014-11-27 14:52:37 +00001119 if (inst->IsBranch()) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07001120 GetInstructionFlags(dex_pc).SetCompileTimeInfoPoint();
Vladimir Marko8b858e12014-11-27 14:52:37 +00001121 // The compiler also needs safepoints for fall-through to loop heads.
1122 // Such a loop head must be a target of a branch.
1123 int32_t offset = 0;
1124 bool cond, self_ok;
1125 bool target_ok = GetBranchOffset(dex_pc, &offset, &cond, &self_ok);
1126 DCHECK(target_ok);
Mathieu Chartierde40d472015-10-15 17:47:48 -07001127 GetInstructionFlags(dex_pc + offset).SetCompileTimeInfoPoint();
Vladimir Marko8b858e12014-11-27 14:52:37 +00001128 } else if (inst->IsSwitch() || inst->IsThrow()) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07001129 GetInstructionFlags(dex_pc).SetCompileTimeInfoPoint();
Ian Rogersb8c78592013-07-25 23:52:52 +00001130 } else if (inst->IsReturn()) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07001131 GetInstructionFlags(dex_pc).SetCompileTimeInfoPointAndReturn();
Ian Rogersd81871c2011-10-03 13:57:23 -07001132 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001133 }
1134 return true;
1135}
1136
Andreas Gampebf1cb772017-05-15 15:39:00 -07001137template <bool kAllowRuntimeOnlyInstructions>
1138bool MethodVerifier::VerifyInstruction(const Instruction* inst, uint32_t code_offset) {
Andreas Gampee05cc662017-05-15 10:17:30 -07001139 if (Instruction::kHaveExperimentalInstructions && UNLIKELY(inst->IsExperimental())) {
Igor Murashkin4d7b75f2015-07-21 17:03:36 -07001140 // Experimental instructions don't yet have verifier support implementation.
1141 // While it is possible to use them by themselves, when we try to use stable instructions
1142 // with a virtual register that was created by an experimental instruction,
1143 // the data flow analysis will fail.
1144 Fail(VERIFY_ERROR_FORCE_INTERPRETER)
1145 << "experimental instruction is not supported by verifier; skipping verification";
1146 have_pending_experimental_failure_ = true;
1147 return false;
1148 }
1149
Ian Rogersd81871c2011-10-03 13:57:23 -07001150 bool result = true;
1151 switch (inst->GetVerifyTypeArgumentA()) {
1152 case Instruction::kVerifyRegA:
Ian Rogers29a26482014-05-02 15:27:29 -07001153 result = result && CheckRegisterIndex(inst->VRegA());
Ian Rogersd81871c2011-10-03 13:57:23 -07001154 break;
1155 case Instruction::kVerifyRegAWide:
Ian Rogers29a26482014-05-02 15:27:29 -07001156 result = result && CheckWideRegisterIndex(inst->VRegA());
Ian Rogersd81871c2011-10-03 13:57:23 -07001157 break;
1158 }
1159 switch (inst->GetVerifyTypeArgumentB()) {
1160 case Instruction::kVerifyRegB:
Ian Rogers29a26482014-05-02 15:27:29 -07001161 result = result && CheckRegisterIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -07001162 break;
1163 case Instruction::kVerifyRegBField:
Ian Rogers29a26482014-05-02 15:27:29 -07001164 result = result && CheckFieldIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -07001165 break;
1166 case Instruction::kVerifyRegBMethod:
Ian Rogers29a26482014-05-02 15:27:29 -07001167 result = result && CheckMethodIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -07001168 break;
1169 case Instruction::kVerifyRegBNewInstance:
Andreas Gampea5b09a62016-11-17 15:21:22 -08001170 result = result && CheckNewInstance(dex::TypeIndex(inst->VRegB()));
Ian Rogersd81871c2011-10-03 13:57:23 -07001171 break;
1172 case Instruction::kVerifyRegBString:
Ian Rogers29a26482014-05-02 15:27:29 -07001173 result = result && CheckStringIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -07001174 break;
1175 case Instruction::kVerifyRegBType:
Andreas Gampea5b09a62016-11-17 15:21:22 -08001176 result = result && CheckTypeIndex(dex::TypeIndex(inst->VRegB()));
Ian Rogersd81871c2011-10-03 13:57:23 -07001177 break;
1178 case Instruction::kVerifyRegBWide:
Ian Rogers29a26482014-05-02 15:27:29 -07001179 result = result && CheckWideRegisterIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -07001180 break;
Orion Hodson2e599942017-09-22 16:17:41 +01001181 case Instruction::kVerifyRegBCallSite:
1182 result = result && CheckCallSiteIndex(inst->VRegB());
1183 break;
1184 case Instruction::kVerifyRegBMethodHandle:
1185 result = result && CheckMethodHandleIndex(inst->VRegB());
1186 break;
1187 case Instruction::kVerifyRegBPrototype:
1188 result = result && CheckPrototypeIndex(inst->VRegB());
1189 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001190 }
1191 switch (inst->GetVerifyTypeArgumentC()) {
1192 case Instruction::kVerifyRegC:
Ian Rogers29a26482014-05-02 15:27:29 -07001193 result = result && CheckRegisterIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -07001194 break;
1195 case Instruction::kVerifyRegCField:
Ian Rogers29a26482014-05-02 15:27:29 -07001196 result = result && CheckFieldIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -07001197 break;
1198 case Instruction::kVerifyRegCNewArray:
Andreas Gampea5b09a62016-11-17 15:21:22 -08001199 result = result && CheckNewArray(dex::TypeIndex(inst->VRegC()));
Ian Rogersd81871c2011-10-03 13:57:23 -07001200 break;
1201 case Instruction::kVerifyRegCType:
Andreas Gampea5b09a62016-11-17 15:21:22 -08001202 result = result && CheckTypeIndex(dex::TypeIndex(inst->VRegC()));
Ian Rogersd81871c2011-10-03 13:57:23 -07001203 break;
1204 case Instruction::kVerifyRegCWide:
Ian Rogers29a26482014-05-02 15:27:29 -07001205 result = result && CheckWideRegisterIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -07001206 break;
1207 }
Orion Hodsoncfa325e2016-10-13 10:25:54 +01001208 switch (inst->GetVerifyTypeArgumentH()) {
1209 case Instruction::kVerifyRegHPrototype:
1210 result = result && CheckPrototypeIndex(inst->VRegH());
1211 break;
1212 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001213 switch (inst->GetVerifyExtraFlags()) {
1214 case Instruction::kVerifyArrayData:
1215 result = result && CheckArrayData(code_offset);
1216 break;
1217 case Instruction::kVerifyBranchTarget:
1218 result = result && CheckBranchTarget(code_offset);
1219 break;
1220 case Instruction::kVerifySwitchTargets:
1221 result = result && CheckSwitchTargets(code_offset);
1222 break;
Andreas Gampec3314312014-06-19 18:13:29 -07001223 case Instruction::kVerifyVarArgNonZero:
1224 // Fall-through.
Ian Rogers29a26482014-05-02 15:27:29 -07001225 case Instruction::kVerifyVarArg: {
Taiju Tsuiki29498a22015-04-13 14:21:00 +09001226 // Instructions that can actually return a negative value shouldn't have this flag.
1227 uint32_t v_a = dchecked_integral_cast<uint32_t>(inst->VRegA());
1228 if ((inst->GetVerifyExtraFlags() == Instruction::kVerifyVarArgNonZero && v_a == 0) ||
1229 v_a > Instruction::kMaxVarArgRegs) {
1230 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << v_a << ") in "
Andreas Gampec3314312014-06-19 18:13:29 -07001231 "non-range invoke";
1232 return false;
1233 }
Taiju Tsuiki29498a22015-04-13 14:21:00 +09001234
Ian Rogers29a26482014-05-02 15:27:29 -07001235 uint32_t args[Instruction::kMaxVarArgRegs];
1236 inst->GetVarArgs(args);
Taiju Tsuiki29498a22015-04-13 14:21:00 +09001237 result = result && CheckVarArgRegs(v_a, args);
Ian Rogersd81871c2011-10-03 13:57:23 -07001238 break;
Ian Rogers29a26482014-05-02 15:27:29 -07001239 }
Andreas Gampec3314312014-06-19 18:13:29 -07001240 case Instruction::kVerifyVarArgRangeNonZero:
1241 // Fall-through.
Ian Rogersd81871c2011-10-03 13:57:23 -07001242 case Instruction::kVerifyVarArgRange:
Andreas Gampec3314312014-06-19 18:13:29 -07001243 if (inst->GetVerifyExtraFlags() == Instruction::kVerifyVarArgRangeNonZero &&
1244 inst->VRegA() <= 0) {
1245 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << inst->VRegA() << ") in "
1246 "range invoke";
1247 return false;
1248 }
Ian Rogers29a26482014-05-02 15:27:29 -07001249 result = result && CheckVarArgRangeRegs(inst->VRegA(), inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -07001250 break;
1251 case Instruction::kVerifyError:
jeffhaod5347e02012-03-22 17:25:05 -07001252 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected opcode " << inst->Name();
Ian Rogersd81871c2011-10-03 13:57:23 -07001253 result = false;
1254 break;
1255 }
Andreas Gampebf1cb772017-05-15 15:39:00 -07001256 if (!kAllowRuntimeOnlyInstructions && inst->GetVerifyIsRuntimeOnly()) {
Ian Rogers5fb22a92014-06-13 10:31:28 -07001257 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "opcode only expected at runtime " << inst->Name();
1258 result = false;
1259 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001260 return result;
1261}
1262
Ian Rogers7b078e82014-09-10 14:44:24 -07001263inline bool MethodVerifier::CheckRegisterIndex(uint32_t idx) {
Andreas Gampe29e81422017-05-15 16:29:32 -07001264 if (UNLIKELY(idx >= code_item_->registers_size_)) {
jeffhaod5347e02012-03-22 17:25:05 -07001265 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register index out of range (" << idx << " >= "
1266 << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001267 return false;
1268 }
1269 return true;
1270}
1271
Ian Rogers7b078e82014-09-10 14:44:24 -07001272inline bool MethodVerifier::CheckWideRegisterIndex(uint32_t idx) {
Andreas Gampe29e81422017-05-15 16:29:32 -07001273 if (UNLIKELY(idx + 1 >= code_item_->registers_size_)) {
jeffhaod5347e02012-03-22 17:25:05 -07001274 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "wide register index out of range (" << idx
1275 << "+1 >= " << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001276 return false;
1277 }
1278 return true;
1279}
1280
Orion Hodson2e599942017-09-22 16:17:41 +01001281inline bool MethodVerifier::CheckCallSiteIndex(uint32_t idx) {
1282 uint32_t limit = dex_file_->NumCallSiteIds();
1283 if (UNLIKELY(idx >= limit)) {
1284 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad call site index " << idx << " (max "
1285 << limit << ")";
1286 return false;
1287 }
1288 return true;
1289}
1290
Ian Rogers7b078e82014-09-10 14:44:24 -07001291inline bool MethodVerifier::CheckFieldIndex(uint32_t idx) {
Andreas Gampe29e81422017-05-15 16:29:32 -07001292 if (UNLIKELY(idx >= dex_file_->GetHeader().field_ids_size_)) {
jeffhaod5347e02012-03-22 17:25:05 -07001293 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad field index " << idx << " (max "
1294 << dex_file_->GetHeader().field_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001295 return false;
1296 }
1297 return true;
1298}
1299
Ian Rogers7b078e82014-09-10 14:44:24 -07001300inline bool MethodVerifier::CheckMethodIndex(uint32_t idx) {
Andreas Gampe29e81422017-05-15 16:29:32 -07001301 if (UNLIKELY(idx >= dex_file_->GetHeader().method_ids_size_)) {
jeffhaod5347e02012-03-22 17:25:05 -07001302 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad method index " << idx << " (max "
1303 << dex_file_->GetHeader().method_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001304 return false;
1305 }
1306 return true;
1307}
1308
Orion Hodson2e599942017-09-22 16:17:41 +01001309inline bool MethodVerifier::CheckMethodHandleIndex(uint32_t idx) {
1310 uint32_t limit = dex_file_->NumMethodHandles();
1311 if (UNLIKELY(idx >= limit)) {
1312 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad method handle index " << idx << " (max "
1313 << limit << ")";
1314 return false;
1315 }
1316 return true;
1317}
1318
Andreas Gampea5b09a62016-11-17 15:21:22 -08001319inline bool MethodVerifier::CheckNewInstance(dex::TypeIndex idx) {
Andreas Gampe29e81422017-05-15 16:29:32 -07001320 if (UNLIKELY(idx.index_ >= dex_file_->GetHeader().type_ids_size_)) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08001321 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx.index_ << " (max "
jeffhaod5347e02012-03-22 17:25:05 -07001322 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001323 return false;
1324 }
1325 // We don't need the actual class, just a pointer to the class name.
Ian Rogers0571d352011-11-03 19:51:38 -07001326 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Andreas Gampe29e81422017-05-15 16:29:32 -07001327 if (UNLIKELY(descriptor[0] != 'L')) {
jeffhaod5347e02012-03-22 17:25:05 -07001328 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "can't call new-instance on type '" << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001329 return false;
Andreas Gampe29e81422017-05-15 16:29:32 -07001330 } else if (UNLIKELY(strcmp(descriptor, "Ljava/lang/Class;") == 0)) {
Aart Bikdb698f12016-07-25 17:52:22 -07001331 // An unlikely new instance on Class is not allowed. Fall back to interpreter to ensure an
1332 // exception is thrown when this statement is executed (compiled code would not do that).
1333 Fail(VERIFY_ERROR_INSTANTIATION);
Ian Rogersd81871c2011-10-03 13:57:23 -07001334 }
1335 return true;
1336}
1337
Orion Hodsoncfa325e2016-10-13 10:25:54 +01001338inline bool MethodVerifier::CheckPrototypeIndex(uint32_t idx) {
Andreas Gampe29e81422017-05-15 16:29:32 -07001339 if (UNLIKELY(idx >= dex_file_->GetHeader().proto_ids_size_)) {
Orion Hodsoncfa325e2016-10-13 10:25:54 +01001340 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad prototype index " << idx << " (max "
1341 << dex_file_->GetHeader().proto_ids_size_ << ")";
1342 return false;
1343 }
1344 return true;
1345}
1346
Ian Rogers7b078e82014-09-10 14:44:24 -07001347inline bool MethodVerifier::CheckStringIndex(uint32_t idx) {
Andreas Gampe29e81422017-05-15 16:29:32 -07001348 if (UNLIKELY(idx >= dex_file_->GetHeader().string_ids_size_)) {
jeffhaod5347e02012-03-22 17:25:05 -07001349 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad string index " << idx << " (max "
1350 << dex_file_->GetHeader().string_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001351 return false;
1352 }
1353 return true;
1354}
1355
Andreas Gampea5b09a62016-11-17 15:21:22 -08001356inline bool MethodVerifier::CheckTypeIndex(dex::TypeIndex idx) {
Andreas Gampe29e81422017-05-15 16:29:32 -07001357 if (UNLIKELY(idx.index_ >= dex_file_->GetHeader().type_ids_size_)) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08001358 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx.index_ << " (max "
jeffhaod5347e02012-03-22 17:25:05 -07001359 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001360 return false;
1361 }
1362 return true;
1363}
1364
Andreas Gampea5b09a62016-11-17 15:21:22 -08001365bool MethodVerifier::CheckNewArray(dex::TypeIndex idx) {
Andreas Gampe29e81422017-05-15 16:29:32 -07001366 if (UNLIKELY(idx.index_ >= dex_file_->GetHeader().type_ids_size_)) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08001367 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx.index_ << " (max "
jeffhaod5347e02012-03-22 17:25:05 -07001368 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001369 return false;
1370 }
1371 int bracket_count = 0;
Ian Rogers0571d352011-11-03 19:51:38 -07001372 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -07001373 const char* cp = descriptor;
1374 while (*cp++ == '[') {
1375 bracket_count++;
1376 }
Andreas Gampe29e81422017-05-15 16:29:32 -07001377 if (UNLIKELY(bracket_count == 0)) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001378 /* The given class must be an array type. */
Brian Carlstrom93c33962013-07-26 10:37:43 -07001379 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1380 << "can't new-array class '" << descriptor << "' (not an array)";
Ian Rogersd81871c2011-10-03 13:57:23 -07001381 return false;
Andreas Gampe29e81422017-05-15 16:29:32 -07001382 } else if (UNLIKELY(bracket_count > 255)) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001383 /* It is illegal to create an array of more than 255 dimensions. */
Brian Carlstrom93c33962013-07-26 10:37:43 -07001384 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1385 << "can't new-array class '" << descriptor << "' (exceeds limit)";
Ian Rogersd81871c2011-10-03 13:57:23 -07001386 return false;
1387 }
1388 return true;
1389}
1390
Ian Rogers776ac1f2012-04-13 23:36:36 -07001391bool MethodVerifier::CheckArrayData(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001392 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
1393 const uint16_t* insns = code_item_->insns_ + cur_offset;
1394 const uint16_t* array_data;
1395 int32_t array_data_offset;
1396
1397 DCHECK_LT(cur_offset, insn_count);
1398 /* make sure the start of the array data table is in range */
Andreas Gampe53de99c2015-08-17 13:43:55 -07001399 array_data_offset = insns[1] | (static_cast<int32_t>(insns[2]) << 16);
Andreas Gampe29e81422017-05-15 16:29:32 -07001400 if (UNLIKELY(static_cast<int32_t>(cur_offset) + array_data_offset < 0 ||
1401 cur_offset + array_data_offset + 2 >= insn_count)) {
jeffhaod5347e02012-03-22 17:25:05 -07001402 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data start: at " << cur_offset
Brian Carlstrom93c33962013-07-26 10:37:43 -07001403 << ", data offset " << array_data_offset
1404 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -07001405 return false;
1406 }
1407 /* offset to array data table is a relative branch-style offset */
1408 array_data = insns + array_data_offset;
Andreas Gampe57c47582015-07-01 22:05:59 -07001409 // Make sure the table is at an even dex pc, that is, 32-bit aligned.
Andreas Gampe29e81422017-05-15 16:29:32 -07001410 if (UNLIKELY(!IsAligned<4>(array_data))) {
jeffhaod5347e02012-03-22 17:25:05 -07001411 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned array data table: at " << cur_offset
1412 << ", data offset " << array_data_offset;
Ian Rogersd81871c2011-10-03 13:57:23 -07001413 return false;
1414 }
Andreas Gampe57c47582015-07-01 22:05:59 -07001415 // Make sure the array-data is marked as an opcode. This ensures that it was reached when
1416 // traversing the code item linearly. It is an approximation for a by-spec padding value.
Andreas Gampe29e81422017-05-15 16:29:32 -07001417 if (UNLIKELY(!GetInstructionFlags(cur_offset + array_data_offset).IsOpcode())) {
Andreas Gampe57c47582015-07-01 22:05:59 -07001418 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array data table at " << cur_offset
1419 << ", data offset " << array_data_offset
1420 << " not correctly visited, probably bad padding.";
1421 return false;
1422 }
1423
Ian Rogersd81871c2011-10-03 13:57:23 -07001424 uint32_t value_width = array_data[1];
Elliott Hughes398f64b2012-03-26 18:05:48 -07001425 uint32_t value_count = *reinterpret_cast<const uint32_t*>(&array_data[2]);
Ian Rogersd81871c2011-10-03 13:57:23 -07001426 uint32_t table_size = 4 + (value_width * value_count + 1) / 2;
1427 /* make sure the end of the switch is in range */
Andreas Gampe29e81422017-05-15 16:29:32 -07001428 if (UNLIKELY(cur_offset + array_data_offset + table_size > insn_count)) {
jeffhaod5347e02012-03-22 17:25:05 -07001429 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data end: at " << cur_offset
1430 << ", data offset " << array_data_offset << ", end "
1431 << cur_offset + array_data_offset + table_size
1432 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -07001433 return false;
1434 }
1435 return true;
1436}
1437
Ian Rogers776ac1f2012-04-13 23:36:36 -07001438bool MethodVerifier::CheckBranchTarget(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001439 int32_t offset;
1440 bool isConditional, selfOkay;
1441 if (!GetBranchOffset(cur_offset, &offset, &isConditional, &selfOkay)) {
1442 return false;
1443 }
Andreas Gampe29e81422017-05-15 16:29:32 -07001444 if (UNLIKELY(!selfOkay && offset == 0)) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001445 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch offset of zero not allowed at"
1446 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -07001447 return false;
1448 }
Elliott Hughes81ff3182012-03-23 20:35:56 -07001449 // Check for 32-bit overflow. This isn't strictly necessary if we can depend on the runtime
1450 // to have identical "wrap-around" behavior, but it's unwise to depend on that.
Andreas Gampe29e81422017-05-15 16:29:32 -07001451 if (UNLIKELY(((int64_t) cur_offset + (int64_t) offset) != (int64_t) (cur_offset + offset))) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001452 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch target overflow "
1453 << reinterpret_cast<void*>(cur_offset) << " +" << offset;
Ian Rogersd81871c2011-10-03 13:57:23 -07001454 return false;
1455 }
1456 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
1457 int32_t abs_offset = cur_offset + offset;
Andreas Gampe29e81422017-05-15 16:29:32 -07001458 if (UNLIKELY(abs_offset < 0 ||
1459 (uint32_t) abs_offset >= insn_count ||
1460 !GetInstructionFlags(abs_offset).IsOpcode())) {
jeffhaod5347e02012-03-22 17:25:05 -07001461 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid branch target " << offset << " (-> "
Elliott Hughes398f64b2012-03-26 18:05:48 -07001462 << reinterpret_cast<void*>(abs_offset) << ") at "
1463 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -07001464 return false;
1465 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07001466 GetInstructionFlags(abs_offset).SetBranchTarget();
Ian Rogersd81871c2011-10-03 13:57:23 -07001467 return true;
1468}
1469
Ian Rogers776ac1f2012-04-13 23:36:36 -07001470bool MethodVerifier::GetBranchOffset(uint32_t cur_offset, int32_t* pOffset, bool* pConditional,
Ian Rogersd81871c2011-10-03 13:57:23 -07001471 bool* selfOkay) {
1472 const uint16_t* insns = code_item_->insns_ + cur_offset;
1473 *pConditional = false;
1474 *selfOkay = false;
jeffhaoba5ebb92011-08-25 17:24:37 -07001475 switch (*insns & 0xff) {
1476 case Instruction::GOTO:
1477 *pOffset = ((int16_t) *insns) >> 8;
jeffhaoba5ebb92011-08-25 17:24:37 -07001478 break;
1479 case Instruction::GOTO_32:
1480 *pOffset = insns[1] | (((uint32_t) insns[2]) << 16);
jeffhaoba5ebb92011-08-25 17:24:37 -07001481 *selfOkay = true;
1482 break;
1483 case Instruction::GOTO_16:
1484 *pOffset = (int16_t) insns[1];
jeffhaoba5ebb92011-08-25 17:24:37 -07001485 break;
1486 case Instruction::IF_EQ:
1487 case Instruction::IF_NE:
1488 case Instruction::IF_LT:
1489 case Instruction::IF_GE:
1490 case Instruction::IF_GT:
1491 case Instruction::IF_LE:
1492 case Instruction::IF_EQZ:
1493 case Instruction::IF_NEZ:
1494 case Instruction::IF_LTZ:
1495 case Instruction::IF_GEZ:
1496 case Instruction::IF_GTZ:
1497 case Instruction::IF_LEZ:
1498 *pOffset = (int16_t) insns[1];
1499 *pConditional = true;
jeffhaoba5ebb92011-08-25 17:24:37 -07001500 break;
1501 default:
1502 return false;
jeffhaoba5ebb92011-08-25 17:24:37 -07001503 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001504 return true;
1505}
1506
Ian Rogers776ac1f2012-04-13 23:36:36 -07001507bool MethodVerifier::CheckSwitchTargets(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001508 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001509 DCHECK_LT(cur_offset, insn_count);
Ian Rogersd81871c2011-10-03 13:57:23 -07001510 const uint16_t* insns = code_item_->insns_ + cur_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001511 /* make sure the start of the switch is in range */
Andreas Gampe53de99c2015-08-17 13:43:55 -07001512 int32_t switch_offset = insns[1] | (static_cast<int32_t>(insns[2]) << 16);
Andreas Gampe29e81422017-05-15 16:29:32 -07001513 if (UNLIKELY(static_cast<int32_t>(cur_offset) + switch_offset < 0 ||
1514 cur_offset + switch_offset + 2 > insn_count)) {
jeffhaod5347e02012-03-22 17:25:05 -07001515 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch start: at " << cur_offset
Brian Carlstrom93c33962013-07-26 10:37:43 -07001516 << ", switch offset " << switch_offset
1517 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001518 return false;
1519 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001520 /* offset to switch table is a relative branch-style offset */
Ian Rogersd81871c2011-10-03 13:57:23 -07001521 const uint16_t* switch_insns = insns + switch_offset;
Andreas Gampe57c47582015-07-01 22:05:59 -07001522 // Make sure the table is at an even dex pc, that is, 32-bit aligned.
Andreas Gampe29e81422017-05-15 16:29:32 -07001523 if (UNLIKELY(!IsAligned<4>(switch_insns))) {
jeffhaod5347e02012-03-22 17:25:05 -07001524 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned switch table: at " << cur_offset
1525 << ", switch offset " << switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001526 return false;
1527 }
Andreas Gampe57c47582015-07-01 22:05:59 -07001528 // Make sure the switch data is marked as an opcode. This ensures that it was reached when
1529 // traversing the code item linearly. It is an approximation for a by-spec padding value.
Andreas Gampe29e81422017-05-15 16:29:32 -07001530 if (UNLIKELY(!GetInstructionFlags(cur_offset + switch_offset).IsOpcode())) {
Andreas Gampe57c47582015-07-01 22:05:59 -07001531 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "switch table at " << cur_offset
1532 << ", switch offset " << switch_offset
1533 << " not correctly visited, probably bad padding.";
1534 return false;
1535 }
1536
David Brazdil5469d342015-09-25 16:57:53 +01001537 bool is_packed_switch = (*insns & 0xff) == Instruction::PACKED_SWITCH;
1538
Ian Rogersd81871c2011-10-03 13:57:23 -07001539 uint32_t switch_count = switch_insns[1];
David Brazdil5469d342015-09-25 16:57:53 +01001540 int32_t targets_offset;
Ian Rogersd81871c2011-10-03 13:57:23 -07001541 uint16_t expected_signature;
David Brazdil5469d342015-09-25 16:57:53 +01001542 if (is_packed_switch) {
jeffhaoba5ebb92011-08-25 17:24:37 -07001543 /* 0=sig, 1=count, 2/3=firstKey */
1544 targets_offset = 4;
jeffhaoba5ebb92011-08-25 17:24:37 -07001545 expected_signature = Instruction::kPackedSwitchSignature;
1546 } else {
1547 /* 0=sig, 1=count, 2..count*2 = keys */
jeffhaoba5ebb92011-08-25 17:24:37 -07001548 targets_offset = 2 + 2 * switch_count;
1549 expected_signature = Instruction::kSparseSwitchSignature;
1550 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001551 uint32_t table_size = targets_offset + switch_count * 2;
Andreas Gampe29e81422017-05-15 16:29:32 -07001552 if (UNLIKELY(switch_insns[0] != expected_signature)) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001553 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1554 << StringPrintf("wrong signature for switch table (%x, wanted %x)",
1555 switch_insns[0], expected_signature);
jeffhaoba5ebb92011-08-25 17:24:37 -07001556 return false;
1557 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001558 /* make sure the end of the switch is in range */
Andreas Gampe29e81422017-05-15 16:29:32 -07001559 if (UNLIKELY(cur_offset + switch_offset + table_size > (uint32_t) insn_count)) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001560 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch end: at " << cur_offset
1561 << ", switch offset " << switch_offset
1562 << ", end " << (cur_offset + switch_offset + table_size)
jeffhaod5347e02012-03-22 17:25:05 -07001563 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001564 return false;
1565 }
David Brazdil5469d342015-09-25 16:57:53 +01001566
1567 constexpr int32_t keys_offset = 2;
1568 if (switch_count > 1) {
1569 if (is_packed_switch) {
1570 /* for a packed switch, verify that keys do not overflow int32 */
1571 int32_t first_key = switch_insns[keys_offset] | (switch_insns[keys_offset + 1] << 16);
1572 int32_t max_first_key =
1573 std::numeric_limits<int32_t>::max() - (static_cast<int32_t>(switch_count) - 1);
Andreas Gampe29e81422017-05-15 16:29:32 -07001574 if (UNLIKELY(first_key > max_first_key)) {
David Brazdil5469d342015-09-25 16:57:53 +01001575 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid packed switch: first_key=" << first_key
1576 << ", switch_count=" << switch_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001577 return false;
1578 }
David Brazdil5469d342015-09-25 16:57:53 +01001579 } else {
1580 /* for a sparse switch, verify the keys are in ascending order */
1581 int32_t last_key = switch_insns[keys_offset] | (switch_insns[keys_offset + 1] << 16);
1582 for (uint32_t targ = 1; targ < switch_count; targ++) {
1583 int32_t key =
1584 static_cast<int32_t>(switch_insns[keys_offset + targ * 2]) |
1585 static_cast<int32_t>(switch_insns[keys_offset + targ * 2 + 1] << 16);
Andreas Gampe29e81422017-05-15 16:29:32 -07001586 if (UNLIKELY(key <= last_key)) {
David Brazdil5469d342015-09-25 16:57:53 +01001587 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid sparse switch: last key=" << last_key
1588 << ", this=" << key;
1589 return false;
1590 }
1591 last_key = key;
1592 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001593 }
1594 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001595 /* verify each switch target */
Ian Rogersd81871c2011-10-03 13:57:23 -07001596 for (uint32_t targ = 0; targ < switch_count; targ++) {
Andreas Gampe53de99c2015-08-17 13:43:55 -07001597 int32_t offset = static_cast<int32_t>(switch_insns[targets_offset + targ * 2]) |
1598 static_cast<int32_t>(switch_insns[targets_offset + targ * 2 + 1] << 16);
Ian Rogersd81871c2011-10-03 13:57:23 -07001599 int32_t abs_offset = cur_offset + offset;
Andreas Gampe29e81422017-05-15 16:29:32 -07001600 if (UNLIKELY(abs_offset < 0 ||
1601 abs_offset >= static_cast<int32_t>(insn_count) ||
1602 !GetInstructionFlags(abs_offset).IsOpcode())) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001603 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch target " << offset
1604 << " (-> " << reinterpret_cast<void*>(abs_offset) << ") at "
1605 << reinterpret_cast<void*>(cur_offset)
1606 << "[" << targ << "]";
jeffhaoba5ebb92011-08-25 17:24:37 -07001607 return false;
1608 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07001609 GetInstructionFlags(abs_offset).SetBranchTarget();
Ian Rogersd81871c2011-10-03 13:57:23 -07001610 }
1611 return true;
1612}
1613
Ian Rogers776ac1f2012-04-13 23:36:36 -07001614bool MethodVerifier::CheckVarArgRegs(uint32_t vA, uint32_t arg[]) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001615 uint16_t registers_size = code_item_->registers_size_;
1616 for (uint32_t idx = 0; idx < vA; idx++) {
Andreas Gampe29e81422017-05-15 16:29:32 -07001617 if (UNLIKELY(arg[idx] >= registers_size)) {
jeffhaod5347e02012-03-22 17:25:05 -07001618 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index (" << arg[idx]
1619 << ") in non-range invoke (>= " << registers_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001620 return false;
1621 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001622 }
1623
1624 return true;
1625}
1626
Ian Rogers776ac1f2012-04-13 23:36:36 -07001627bool MethodVerifier::CheckVarArgRangeRegs(uint32_t vA, uint32_t vC) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001628 uint16_t registers_size = code_item_->registers_size_;
1629 // vA/vC are unsigned 8-bit/16-bit quantities for /range instructions, so there's no risk of
1630 // integer overflow when adding them here.
Andreas Gampe29e81422017-05-15 16:29:32 -07001631 if (UNLIKELY(vA + vC > registers_size)) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001632 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index " << vA << "+" << vC
1633 << " in range invoke (> " << registers_size << ")";
jeffhaoba5ebb92011-08-25 17:24:37 -07001634 return false;
1635 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001636 return true;
1637}
1638
Ian Rogers776ac1f2012-04-13 23:36:36 -07001639bool MethodVerifier::VerifyCodeFlow() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001640 uint16_t registers_size = code_item_->registers_size_;
1641 uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaobdb76512011-09-07 11:43:16 -07001642
Ian Rogersd81871c2011-10-03 13:57:23 -07001643 /* Create and initialize table holding register status */
Brian Carlstrom93c33962013-07-26 10:37:43 -07001644 reg_table_.Init(kTrackCompilerInterestPoints,
1645 insn_flags_.get(),
1646 insns_size,
1647 registers_size,
1648 this);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07001649
Ian Rogersd0fbd852013-09-24 18:17:04 -07001650 work_line_.reset(RegisterLine::Create(registers_size, this));
1651 saved_line_.reset(RegisterLine::Create(registers_size, this));
jeffhaobdb76512011-09-07 11:43:16 -07001652
Ian Rogersd81871c2011-10-03 13:57:23 -07001653 /* Initialize register types of method arguments. */
1654 if (!SetTypesFromSignature()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001655 DCHECK_NE(failures_.size(), 0U);
1656 std::string prepend("Bad signature in ");
David Sehr709b0702016-10-13 09:12:37 -07001657 prepend += dex_file_->PrettyMethod(dex_method_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001658 PrependToLastFailMessage(prepend);
Ian Rogersd81871c2011-10-03 13:57:23 -07001659 return false;
1660 }
Andreas Gamped5ad72f2015-06-26 17:33:47 -07001661 // We may have a runtime failure here, clear.
1662 have_pending_runtime_throw_failure_ = false;
1663
Ian Rogersd81871c2011-10-03 13:57:23 -07001664 /* Perform code flow verification. */
1665 if (!CodeFlowVerifyMethod()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001666 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -07001667 return false;
jeffhaobdb76512011-09-07 11:43:16 -07001668 }
jeffhaobdb76512011-09-07 11:43:16 -07001669 return true;
1670}
1671
Ian Rogersad0b3a32012-04-16 14:50:24 -07001672std::ostream& MethodVerifier::DumpFailures(std::ostream& os) {
1673 DCHECK_EQ(failures_.size(), failure_messages_.size());
Jeff Hao4137f482013-11-22 11:44:57 -08001674 for (size_t i = 0; i < failures_.size(); ++i) {
1675 os << failure_messages_[i]->str() << "\n";
Ian Rogersad0b3a32012-04-16 14:50:24 -07001676 }
1677 return os;
1678}
1679
Ian Rogers776ac1f2012-04-13 23:36:36 -07001680void MethodVerifier::Dump(std::ostream& os) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001681 VariableIndentationOutputStream vios(&os);
1682 Dump(&vios);
1683}
1684
1685void MethodVerifier::Dump(VariableIndentationOutputStream* vios) {
Ian Rogers7b078e82014-09-10 14:44:24 -07001686 if (code_item_ == nullptr) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001687 vios->Stream() << "Native method\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001688 return;
jeffhaobdb76512011-09-07 11:43:16 -07001689 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001690 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001691 vios->Stream() << "Register Types:\n";
1692 ScopedIndentation indent1(vios);
1693 reg_types_.Dump(vios->Stream());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001694 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001695 vios->Stream() << "Dumping instructions and register lines:\n";
1696 ScopedIndentation indent1(vios);
Mathieu Chartier1d2d4ff2017-09-23 16:11:06 -07001697
1698 IterationRange<DexInstructionIterator> instructions = code_item_->Instructions();
1699 for (auto inst = instructions.begin(); inst != instructions.end(); ++inst) {
1700 const size_t dex_pc = inst.GetDexPC(instructions.begin());
Ian Rogersd81871c2011-10-03 13:57:23 -07001701 RegisterLine* reg_line = reg_table_.GetLine(dex_pc);
Ian Rogers7b078e82014-09-10 14:44:24 -07001702 if (reg_line != nullptr) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001703 vios->Stream() << reg_line->Dump(this) << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07001704 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001705 vios->Stream()
Mathieu Chartierde40d472015-10-15 17:47:48 -07001706 << StringPrintf("0x%04zx", dex_pc) << ": " << GetInstructionFlags(dex_pc).ToString() << " ";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001707 const bool kDumpHexOfInstruction = false;
1708 if (kDumpHexOfInstruction) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001709 vios->Stream() << inst->DumpHex(5) << " ";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001710 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001711 vios->Stream() << inst->DumpString(dex_file_) << "\n";
jeffhaoba5ebb92011-08-25 17:24:37 -07001712 }
jeffhaobdb76512011-09-07 11:43:16 -07001713}
1714
Ian Rogersd81871c2011-10-03 13:57:23 -07001715static bool IsPrimitiveDescriptor(char descriptor) {
1716 switch (descriptor) {
jeffhaobdb76512011-09-07 11:43:16 -07001717 case 'I':
1718 case 'C':
1719 case 'S':
1720 case 'B':
1721 case 'Z':
jeffhaobdb76512011-09-07 11:43:16 -07001722 case 'F':
1723 case 'D':
1724 case 'J':
Ian Rogersd81871c2011-10-03 13:57:23 -07001725 return true;
jeffhaobdb76512011-09-07 11:43:16 -07001726 default:
1727 return false;
1728 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001729}
1730
Ian Rogers776ac1f2012-04-13 23:36:36 -07001731bool MethodVerifier::SetTypesFromSignature() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001732 RegisterLine* reg_line = reg_table_.GetLine(0);
Andreas Gampeef0b1a12015-06-19 20:37:46 -07001733
1734 // Should have been verified earlier.
1735 DCHECK_GE(code_item_->registers_size_, code_item_->ins_size_);
1736
1737 uint32_t arg_start = code_item_->registers_size_ - code_item_->ins_size_;
Ian Rogersd81871c2011-10-03 13:57:23 -07001738 size_t expected_args = code_item_->ins_size_; /* long/double count as two */
jeffhaobdb76512011-09-07 11:43:16 -07001739
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001740 // Include the "this" pointer.
Ian Rogersd81871c2011-10-03 13:57:23 -07001741 size_t cur_arg = 0;
Ian Rogersad0b3a32012-04-16 14:50:24 -07001742 if (!IsStatic()) {
Andreas Gampeef0b1a12015-06-19 20:37:46 -07001743 if (expected_args == 0) {
1744 // Expect at least a receiver.
1745 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected 0 args, but method is not static";
1746 return false;
1747 }
1748
Ian Rogersd81871c2011-10-03 13:57:23 -07001749 // If this is a constructor for a class other than java.lang.Object, mark the first ("this")
1750 // argument as uninitialized. This restricts field access until the superclass constructor is
1751 // called.
Ian Rogersd8f69b02014-09-10 21:43:52 +00001752 const RegType& declaring_class = GetDeclaringClass();
Andreas Gampef10b6e12015-08-12 10:48:12 -07001753 if (IsConstructor()) {
1754 if (declaring_class.IsJavaLangObject()) {
1755 // "this" is implicitly initialized.
1756 reg_line->SetThisInitialized();
Andreas Gampead238ce2015-08-24 21:13:08 -07001757 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, declaring_class);
Andreas Gampef10b6e12015-08-12 10:48:12 -07001758 } else {
Andreas Gampead238ce2015-08-24 21:13:08 -07001759 reg_line->SetRegisterType<LockOp::kClear>(
1760 this,
1761 arg_start + cur_arg,
1762 reg_types_.UninitializedThisArgument(declaring_class));
Andreas Gampef10b6e12015-08-12 10:48:12 -07001763 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001764 } else {
Andreas Gampead238ce2015-08-24 21:13:08 -07001765 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, declaring_class);
jeffhaobdb76512011-09-07 11:43:16 -07001766 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001767 cur_arg++;
jeffhaobdb76512011-09-07 11:43:16 -07001768 }
1769
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001770 const DexFile::ProtoId& proto_id =
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001771 dex_file_->GetMethodPrototype(dex_file_->GetMethodId(dex_method_idx_));
Ian Rogers0571d352011-11-03 19:51:38 -07001772 DexFileParameterIterator iterator(*dex_file_, proto_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07001773
1774 for (; iterator.HasNext(); iterator.Next()) {
1775 const char* descriptor = iterator.GetDescriptor();
Ian Rogers7b078e82014-09-10 14:44:24 -07001776 if (descriptor == nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001777 LOG(FATAL) << "Null descriptor";
1778 }
1779 if (cur_arg >= expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07001780 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1781 << " args, found more (" << descriptor << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001782 return false;
1783 }
1784 switch (descriptor[0]) {
1785 case 'L':
1786 case '[':
1787 // We assume that reference arguments are initialized. The only way it could be otherwise
1788 // (assuming the caller was verified) is if the current method is <init>, but in that case
1789 // it's effectively considered initialized the instant we reach here (in the sense that we
1790 // can return without doing anything or call virtual methods).
1791 {
Andreas Gampe98be1a92017-08-28 08:25:45 -07001792 // Note: don't check access. No error would be thrown for declaring or passing an
1793 // inaccessible class. Only actual accesses to fields or methods will.
1794 const RegType& reg_type = ResolveClass<CheckAccess::kNo>(iterator.GetTypeIdx());
Sebastien Hertz2ed76f92014-04-22 17:11:08 +02001795 if (!reg_type.IsNonZeroReferenceTypes()) {
1796 DCHECK(HasFailures());
1797 return false;
1798 }
Andreas Gampead238ce2015-08-24 21:13:08 -07001799 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001800 }
1801 break;
1802 case 'Z':
Andreas Gampead238ce2015-08-24 21:13:08 -07001803 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Boolean());
Ian Rogersd81871c2011-10-03 13:57:23 -07001804 break;
1805 case 'C':
Andreas Gampead238ce2015-08-24 21:13:08 -07001806 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Char());
Ian Rogersd81871c2011-10-03 13:57:23 -07001807 break;
1808 case 'B':
Andreas Gampead238ce2015-08-24 21:13:08 -07001809 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Byte());
Ian Rogersd81871c2011-10-03 13:57:23 -07001810 break;
1811 case 'I':
Andreas Gampead238ce2015-08-24 21:13:08 -07001812 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Integer());
Ian Rogersd81871c2011-10-03 13:57:23 -07001813 break;
1814 case 'S':
Andreas Gampead238ce2015-08-24 21:13:08 -07001815 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Short());
Ian Rogersd81871c2011-10-03 13:57:23 -07001816 break;
1817 case 'F':
Andreas Gampead238ce2015-08-24 21:13:08 -07001818 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Float());
Ian Rogersd81871c2011-10-03 13:57:23 -07001819 break;
1820 case 'J':
1821 case 'D': {
Andreas Gampe77cd4d62014-06-19 17:29:48 -07001822 if (cur_arg + 1 >= expected_args) {
1823 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1824 << " args, found more (" << descriptor << ")";
1825 return false;
1826 }
1827
Ian Rogers7b078e82014-09-10 14:44:24 -07001828 const RegType* lo_half;
1829 const RegType* hi_half;
1830 if (descriptor[0] == 'J') {
1831 lo_half = &reg_types_.LongLo();
1832 hi_half = &reg_types_.LongHi();
1833 } else {
1834 lo_half = &reg_types_.DoubleLo();
1835 hi_half = &reg_types_.DoubleHi();
1836 }
1837 reg_line->SetRegisterTypeWide(this, arg_start + cur_arg, *lo_half, *hi_half);
Ian Rogersd81871c2011-10-03 13:57:23 -07001838 cur_arg++;
1839 break;
1840 }
1841 default:
Brian Carlstrom93c33962013-07-26 10:37:43 -07001842 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected signature type char '"
1843 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001844 return false;
1845 }
1846 cur_arg++;
1847 }
1848 if (cur_arg != expected_args) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001849 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1850 << " arguments, found " << cur_arg;
Ian Rogersd81871c2011-10-03 13:57:23 -07001851 return false;
1852 }
1853 const char* descriptor = dex_file_->GetReturnTypeDescriptor(proto_id);
1854 // Validate return type. We don't do the type lookup; just want to make sure that it has the right
1855 // format. Only major difference from the method argument format is that 'V' is supported.
1856 bool result;
1857 if (IsPrimitiveDescriptor(descriptor[0]) || descriptor[0] == 'V') {
1858 result = descriptor[1] == '\0';
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001859 } else if (descriptor[0] == '[') { // single/multi-dimensional array of object/primitive
Ian Rogersd81871c2011-10-03 13:57:23 -07001860 size_t i = 0;
1861 do {
1862 i++;
1863 } while (descriptor[i] == '['); // process leading [
1864 if (descriptor[i] == 'L') { // object array
1865 do {
1866 i++; // find closing ;
1867 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1868 result = descriptor[i] == ';';
1869 } else { // primitive array
1870 result = IsPrimitiveDescriptor(descriptor[i]) && descriptor[i + 1] == '\0';
1871 }
1872 } else if (descriptor[0] == 'L') {
1873 // could be more thorough here, but shouldn't be required
1874 size_t i = 0;
1875 do {
1876 i++;
1877 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1878 result = descriptor[i] == ';';
1879 } else {
1880 result = false;
1881 }
1882 if (!result) {
jeffhaod5347e02012-03-22 17:25:05 -07001883 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected char in return type descriptor '"
1884 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001885 }
1886 return result;
jeffhaobdb76512011-09-07 11:43:16 -07001887}
1888
Ian Rogers776ac1f2012-04-13 23:36:36 -07001889bool MethodVerifier::CodeFlowVerifyMethod() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001890 const uint16_t* insns = code_item_->insns_;
1891 const uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaoba5ebb92011-08-25 17:24:37 -07001892
jeffhaobdb76512011-09-07 11:43:16 -07001893 /* Begin by marking the first instruction as "changed". */
Mathieu Chartierde40d472015-10-15 17:47:48 -07001894 GetInstructionFlags(0).SetChanged();
Ian Rogersd81871c2011-10-03 13:57:23 -07001895 uint32_t start_guess = 0;
jeffhaoba5ebb92011-08-25 17:24:37 -07001896
jeffhaobdb76512011-09-07 11:43:16 -07001897 /* Continue until no instructions are marked "changed". */
1898 while (true) {
Mathieu Chartier4306ef82014-12-19 18:41:47 -08001899 if (allow_thread_suspension_) {
1900 self_->AllowThreadSuspension();
1901 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001902 // Find the first marked one. Use "start_guess" as a way to find one quickly.
1903 uint32_t insn_idx = start_guess;
1904 for (; insn_idx < insns_size; insn_idx++) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07001905 if (GetInstructionFlags(insn_idx).IsChanged())
jeffhaobdb76512011-09-07 11:43:16 -07001906 break;
1907 }
jeffhaobdb76512011-09-07 11:43:16 -07001908 if (insn_idx == insns_size) {
1909 if (start_guess != 0) {
1910 /* try again, starting from the top */
1911 start_guess = 0;
1912 continue;
1913 } else {
1914 /* all flags are clear */
1915 break;
1916 }
1917 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001918 // We carry the working set of registers from instruction to instruction. If this address can
1919 // be the target of a branch (or throw) instruction, or if we're skipping around chasing
1920 // "changed" flags, we need to load the set of registers from the table.
1921 // Because we always prefer to continue on to the next instruction, we should never have a
1922 // situation where we have a stray "changed" flag set on an instruction that isn't a branch
1923 // target.
1924 work_insn_idx_ = insn_idx;
Mathieu Chartierde40d472015-10-15 17:47:48 -07001925 if (GetInstructionFlags(insn_idx).IsBranchTarget()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001926 work_line_->CopyFromLine(reg_table_.GetLine(insn_idx));
Ian Rogersebbdd872014-07-07 23:53:08 -07001927 } else if (kIsDebugBuild) {
jeffhaobdb76512011-09-07 11:43:16 -07001928 /*
1929 * Sanity check: retrieve the stored register line (assuming
1930 * a full table) and make sure it actually matches.
1931 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001932 RegisterLine* register_line = reg_table_.GetLine(insn_idx);
Ian Rogers7b078e82014-09-10 14:44:24 -07001933 if (register_line != nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001934 if (work_line_->CompareLine(register_line) != 0) {
1935 Dump(std::cout);
1936 std::cout << info_messages_.str();
David Sehr709b0702016-10-13 09:12:37 -07001937 LOG(FATAL) << "work_line diverged in " << dex_file_->PrettyMethod(dex_method_idx_)
Elliott Hughesc073b072012-05-24 19:29:17 -07001938 << "@" << reinterpret_cast<void*>(work_insn_idx_) << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07001939 << " work_line=" << work_line_->Dump(this) << "\n"
1940 << " expected=" << register_line->Dump(this);
Ian Rogersd81871c2011-10-03 13:57:23 -07001941 }
jeffhaobdb76512011-09-07 11:43:16 -07001942 }
jeffhaobdb76512011-09-07 11:43:16 -07001943 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001944 if (!CodeFlowVerifyInstruction(&start_guess)) {
David Sehr709b0702016-10-13 09:12:37 -07001945 std::string prepend(dex_file_->PrettyMethod(dex_method_idx_));
Ian Rogersad0b3a32012-04-16 14:50:24 -07001946 prepend += " failed to verify: ";
1947 PrependToLastFailMessage(prepend);
jeffhaoba5ebb92011-08-25 17:24:37 -07001948 return false;
1949 }
jeffhaobdb76512011-09-07 11:43:16 -07001950 /* Clear "changed" and mark as visited. */
Mathieu Chartierde40d472015-10-15 17:47:48 -07001951 GetInstructionFlags(insn_idx).SetVisited();
1952 GetInstructionFlags(insn_idx).ClearChanged();
jeffhaobdb76512011-09-07 11:43:16 -07001953 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001954
Andreas Gampeec6e6c12015-11-05 20:39:56 -08001955 if (kDebugVerify) {
jeffhaobdb76512011-09-07 11:43:16 -07001956 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001957 * Scan for dead code. There's nothing "evil" about dead code
jeffhaobdb76512011-09-07 11:43:16 -07001958 * (besides the wasted space), but it indicates a flaw somewhere
1959 * down the line, possibly in the verifier.
1960 *
1961 * If we've substituted "always throw" instructions into the stream,
1962 * we are almost certainly going to have some dead code.
1963 */
1964 int dead_start = -1;
Mathieu Chartier1d2d4ff2017-09-23 16:11:06 -07001965
1966 IterationRange<DexInstructionIterator> instructions = code_item_->Instructions();
1967 for (auto inst = instructions.begin(); inst != instructions.end(); ++inst) {
1968 const uint32_t insn_idx = inst.GetDexPC(instructions.begin());
jeffhaobdb76512011-09-07 11:43:16 -07001969 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001970 * Switch-statement data doesn't get "visited" by scanner. It
jeffhaobdb76512011-09-07 11:43:16 -07001971 * may or may not be preceded by a padding NOP (for alignment).
1972 */
1973 if (insns[insn_idx] == Instruction::kPackedSwitchSignature ||
1974 insns[insn_idx] == Instruction::kSparseSwitchSignature ||
1975 insns[insn_idx] == Instruction::kArrayDataSignature ||
Elliott Hughes380aaa72012-07-09 14:33:15 -07001976 (insns[insn_idx] == Instruction::NOP && (insn_idx + 1 < insns_size) &&
jeffhaobdb76512011-09-07 11:43:16 -07001977 (insns[insn_idx + 1] == Instruction::kPackedSwitchSignature ||
1978 insns[insn_idx + 1] == Instruction::kSparseSwitchSignature ||
1979 insns[insn_idx + 1] == Instruction::kArrayDataSignature))) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07001980 GetInstructionFlags(insn_idx).SetVisited();
jeffhaobdb76512011-09-07 11:43:16 -07001981 }
1982
Mathieu Chartierde40d472015-10-15 17:47:48 -07001983 if (!GetInstructionFlags(insn_idx).IsVisited()) {
Mathieu Chartier1d2d4ff2017-09-23 16:11:06 -07001984 if (dead_start < 0) {
jeffhaobdb76512011-09-07 11:43:16 -07001985 dead_start = insn_idx;
Mathieu Chartier1d2d4ff2017-09-23 16:11:06 -07001986 }
jeffhaobdb76512011-09-07 11:43:16 -07001987 } else if (dead_start >= 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001988 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start)
1989 << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaobdb76512011-09-07 11:43:16 -07001990 dead_start = -1;
1991 }
1992 }
1993 if (dead_start >= 0) {
Mathieu Chartier1d2d4ff2017-09-23 16:11:06 -07001994 LogVerifyInfo()
1995 << "dead code " << reinterpret_cast<void*>(dead_start)
Vladimir Markod7559b72017-09-28 13:50:37 +01001996 << "-" << reinterpret_cast<void*>(instructions.end().GetDexPC(instructions.begin()) - 1);
jeffhaoba5ebb92011-08-25 17:24:37 -07001997 }
Ian Rogersc9e463c2013-06-05 16:52:26 -07001998 // To dump the state of the verify after a method, do something like:
David Sehr709b0702016-10-13 09:12:37 -07001999 // if (dex_file_->PrettyMethod(dex_method_idx_) ==
Ian Rogersc9e463c2013-06-05 16:52:26 -07002000 // "boolean java.lang.String.equals(java.lang.Object)") {
2001 // LOG(INFO) << info_messages_.str();
2002 // }
jeffhaoba5ebb92011-08-25 17:24:37 -07002003 }
jeffhaobdb76512011-09-07 11:43:16 -07002004 return true;
2005}
2006
Andreas Gampe68df3202015-06-22 11:35:46 -07002007// Returns the index of the first final instance field of the given class, or kDexNoIndex if there
2008// is no such field.
Andreas Gampea5b09a62016-11-17 15:21:22 -08002009static uint32_t GetFirstFinalInstanceFieldIndex(const DexFile& dex_file, dex::TypeIndex type_idx) {
Andreas Gampe68df3202015-06-22 11:35:46 -07002010 const DexFile::ClassDef* class_def = dex_file.FindClassDef(type_idx);
2011 DCHECK(class_def != nullptr);
2012 const uint8_t* class_data = dex_file.GetClassData(*class_def);
2013 DCHECK(class_data != nullptr);
2014 ClassDataItemIterator it(dex_file, class_data);
Mathieu Chartiere17cf242017-06-19 11:05:51 -07002015 it.SkipStaticFields();
Andreas Gampe68df3202015-06-22 11:35:46 -07002016 while (it.HasNextInstanceField()) {
2017 if ((it.GetFieldAccessFlags() & kAccFinal) != 0) {
2018 return it.GetMemberIndex();
2019 }
2020 it.Next();
2021 }
Andreas Gampee2abbc62017-09-15 11:59:26 -07002022 return dex::kDexNoIndex;
Andreas Gampe68df3202015-06-22 11:35:46 -07002023}
2024
Andreas Gampea727e372015-08-25 09:22:37 -07002025// Setup a register line for the given return instruction.
2026static void AdjustReturnLine(MethodVerifier* verifier,
2027 const Instruction* ret_inst,
2028 RegisterLine* line) {
2029 Instruction::Code opcode = ret_inst->Opcode();
2030
2031 switch (opcode) {
2032 case Instruction::RETURN_VOID:
2033 case Instruction::RETURN_VOID_NO_BARRIER:
2034 SafelyMarkAllRegistersAsConflicts(verifier, line);
2035 break;
2036
2037 case Instruction::RETURN:
2038 case Instruction::RETURN_OBJECT:
2039 line->MarkAllRegistersAsConflictsExcept(verifier, ret_inst->VRegA_11x());
2040 break;
2041
2042 case Instruction::RETURN_WIDE:
2043 line->MarkAllRegistersAsConflictsExceptWide(verifier, ret_inst->VRegA_11x());
2044 break;
2045
2046 default:
2047 LOG(FATAL) << "Unknown return opcode " << opcode;
2048 UNREACHABLE();
2049 }
2050}
2051
Ian Rogers776ac1f2012-04-13 23:36:36 -07002052bool MethodVerifier::CodeFlowVerifyInstruction(uint32_t* start_guess) {
Elliott Hughes08fc03a2012-06-26 17:34:00 -07002053 // If we're doing FindLocksAtDexPc, check whether we're at the dex pc we care about.
2054 // We want the state _before_ the instruction, for the case where the dex pc we're
2055 // interested in is itself a monitor-enter instruction (which is a likely place
2056 // for a thread to be suspended).
Ian Rogers7b078e82014-09-10 14:44:24 -07002057 if (monitor_enter_dex_pcs_ != nullptr && work_insn_idx_ == interesting_dex_pc_) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -07002058 monitor_enter_dex_pcs_->clear(); // The new work line is more accurate than the previous one.
Elliott Hughes08fc03a2012-06-26 17:34:00 -07002059 for (size_t i = 0; i < work_line_->GetMonitorEnterCount(); ++i) {
2060 monitor_enter_dex_pcs_->push_back(work_line_->GetMonitorEnterDexPc(i));
2061 }
2062 }
2063
jeffhaobdb76512011-09-07 11:43:16 -07002064 /*
2065 * Once we finish decoding the instruction, we need to figure out where
jeffhaod1f0fde2011-09-08 17:25:33 -07002066 * we can go from here. There are three possible ways to transfer
jeffhaobdb76512011-09-07 11:43:16 -07002067 * control to another statement:
2068 *
jeffhaod1f0fde2011-09-08 17:25:33 -07002069 * (1) Continue to the next instruction. Applies to all but
jeffhaobdb76512011-09-07 11:43:16 -07002070 * unconditional branches, method returns, and exception throws.
jeffhaod1f0fde2011-09-08 17:25:33 -07002071 * (2) Branch to one or more possible locations. Applies to branches
jeffhaobdb76512011-09-07 11:43:16 -07002072 * and switch statements.
jeffhaod1f0fde2011-09-08 17:25:33 -07002073 * (3) Exception handlers. Applies to any instruction that can
jeffhaobdb76512011-09-07 11:43:16 -07002074 * throw an exception that is handled by an encompassing "try"
2075 * block.
2076 *
2077 * We can also return, in which case there is no successor instruction
2078 * from this point.
2079 *
Elliott Hughesadb8c672012-03-06 16:49:32 -08002080 * The behavior can be determined from the opcode flags.
jeffhaobdb76512011-09-07 11:43:16 -07002081 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002082 const uint16_t* insns = code_item_->insns_ + work_insn_idx_;
2083 const Instruction* inst = Instruction::At(insns);
Ian Rogersa75a0132012-09-28 11:41:42 -07002084 int opcode_flags = Instruction::FlagsOf(inst->Opcode());
jeffhaobdb76512011-09-07 11:43:16 -07002085
jeffhaobdb76512011-09-07 11:43:16 -07002086 int32_t branch_target = 0;
jeffhaobdb76512011-09-07 11:43:16 -07002087 bool just_set_result = false;
Andreas Gampeec6e6c12015-11-05 20:39:56 -08002088 if (kDebugVerify) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002089 // Generate processing back trace to debug verifier
Elliott Hughesc073b072012-05-24 19:29:17 -07002090 LogVerifyInfo() << "Processing " << inst->DumpString(dex_file_) << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07002091 << work_line_->Dump(this) << "\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07002092 }
jeffhaobdb76512011-09-07 11:43:16 -07002093
2094 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002095 * Make a copy of the previous register state. If the instruction
jeffhaobdb76512011-09-07 11:43:16 -07002096 * can throw an exception, we will copy/merge this into the "catch"
2097 * address rather than work_line, because we don't want the result
2098 * from the "successful" code path (e.g. a check-cast that "improves"
2099 * a type) to be visible to the exception handler.
2100 */
Ian Rogers776ac1f2012-04-13 23:36:36 -07002101 if ((opcode_flags & Instruction::kThrow) != 0 && CurrentInsnFlags()->IsInTry()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002102 saved_line_->CopyFromLine(work_line_.get());
Ian Rogers1ff3c982014-08-12 02:30:58 -07002103 } else if (kIsDebugBuild) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002104 saved_line_->FillWithGarbage();
jeffhaobdb76512011-09-07 11:43:16 -07002105 }
Andreas Gamped12e7822015-06-25 10:26:40 -07002106 DCHECK(!have_pending_runtime_throw_failure_); // Per-instruction flag, should not be set here.
jeffhaobdb76512011-09-07 11:43:16 -07002107
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07002108
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002109 // We need to ensure the work line is consistent while performing validation. When we spot a
2110 // peephole pattern we compute a new line for either the fallthrough instruction or the
2111 // branch target.
Mathieu Chartier361e04a2016-02-16 14:06:35 -08002112 RegisterLineArenaUniquePtr branch_line;
2113 RegisterLineArenaUniquePtr fallthrough_line;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002114
Sebastien Hertz5243e912013-05-21 10:55:07 +02002115 switch (inst->Opcode()) {
jeffhaobdb76512011-09-07 11:43:16 -07002116 case Instruction::NOP:
2117 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002118 * A "pure" NOP has no effect on anything. Data tables start with
jeffhaobdb76512011-09-07 11:43:16 -07002119 * a signature that looks like a NOP; if we see one of these in
2120 * the course of executing code then we have a problem.
2121 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002122 if (inst->VRegA_10x() != 0) {
jeffhaod5347e02012-03-22 17:25:05 -07002123 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "encountered data table in instruction stream";
jeffhaobdb76512011-09-07 11:43:16 -07002124 }
2125 break;
2126
2127 case Instruction::MOVE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002128 work_line_->CopyRegister1(this, inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategory1nr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02002129 break;
jeffhaobdb76512011-09-07 11:43:16 -07002130 case Instruction::MOVE_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07002131 work_line_->CopyRegister1(this, inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategory1nr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02002132 break;
jeffhaobdb76512011-09-07 11:43:16 -07002133 case Instruction::MOVE_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07002134 work_line_->CopyRegister1(this, inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategory1nr);
jeffhaobdb76512011-09-07 11:43:16 -07002135 break;
2136 case Instruction::MOVE_WIDE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002137 work_line_->CopyRegister2(this, inst->VRegA_12x(), inst->VRegB_12x());
Sebastien Hertz5243e912013-05-21 10:55:07 +02002138 break;
jeffhaobdb76512011-09-07 11:43:16 -07002139 case Instruction::MOVE_WIDE_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07002140 work_line_->CopyRegister2(this, inst->VRegA_22x(), inst->VRegB_22x());
Sebastien Hertz5243e912013-05-21 10:55:07 +02002141 break;
jeffhaobdb76512011-09-07 11:43:16 -07002142 case Instruction::MOVE_WIDE_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07002143 work_line_->CopyRegister2(this, inst->VRegA_32x(), inst->VRegB_32x());
jeffhaobdb76512011-09-07 11:43:16 -07002144 break;
2145 case Instruction::MOVE_OBJECT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002146 work_line_->CopyRegister1(this, inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategoryRef);
Sebastien Hertz5243e912013-05-21 10:55:07 +02002147 break;
jeffhaobdb76512011-09-07 11:43:16 -07002148 case Instruction::MOVE_OBJECT_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07002149 work_line_->CopyRegister1(this, inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategoryRef);
Sebastien Hertz5243e912013-05-21 10:55:07 +02002150 break;
jeffhaobdb76512011-09-07 11:43:16 -07002151 case Instruction::MOVE_OBJECT_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07002152 work_line_->CopyRegister1(this, inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategoryRef);
jeffhaobdb76512011-09-07 11:43:16 -07002153 break;
2154
2155 /*
2156 * The move-result instructions copy data out of a "pseudo-register"
jeffhaod1f0fde2011-09-08 17:25:33 -07002157 * with the results from the last method invocation. In practice we
jeffhaobdb76512011-09-07 11:43:16 -07002158 * might want to hold the result in an actual CPU register, so the
2159 * Dalvik spec requires that these only appear immediately after an
2160 * invoke or filled-new-array.
2161 *
jeffhaod1f0fde2011-09-08 17:25:33 -07002162 * These calls invalidate the "result" register. (This is now
jeffhaobdb76512011-09-07 11:43:16 -07002163 * redundant with the reset done below, but it can make the debug info
2164 * easier to read in some cases.)
2165 */
2166 case Instruction::MOVE_RESULT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002167 work_line_->CopyResultRegister1(this, inst->VRegA_11x(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002168 break;
2169 case Instruction::MOVE_RESULT_WIDE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002170 work_line_->CopyResultRegister2(this, inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07002171 break;
2172 case Instruction::MOVE_RESULT_OBJECT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002173 work_line_->CopyResultRegister1(this, inst->VRegA_11x(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002174 break;
2175
Ian Rogersd81871c2011-10-03 13:57:23 -07002176 case Instruction::MOVE_EXCEPTION: {
Sebastien Hertz270a0e12015-01-16 19:49:09 +01002177 // We do not allow MOVE_EXCEPTION as the first instruction in a method. This is a simple case
2178 // where one entrypoint to the catch block is not actually an exception path.
2179 if (work_insn_idx_ == 0) {
2180 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "move-exception at pc 0x0";
2181 break;
2182 }
jeffhaobdb76512011-09-07 11:43:16 -07002183 /*
jeffhao60f83e32012-02-13 17:16:30 -08002184 * This statement can only appear as the first instruction in an exception handler. We verify
2185 * that as part of extracting the exception type from the catch block list.
jeffhaobdb76512011-09-07 11:43:16 -07002186 */
Ian Rogersd8f69b02014-09-10 21:43:52 +00002187 const RegType& res_type = GetCaughtExceptionType();
Andreas Gampead238ce2015-08-24 21:13:08 -07002188 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_11x(), res_type);
jeffhaobdb76512011-09-07 11:43:16 -07002189 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002190 }
jeffhaobdb76512011-09-07 11:43:16 -07002191 case Instruction::RETURN_VOID:
Andreas Gampef10b6e12015-08-12 10:48:12 -07002192 if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002193 if (!GetMethodReturnType().IsConflict()) {
jeffhaod5347e02012-03-22 17:25:05 -07002194 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07002195 }
jeffhaobdb76512011-09-07 11:43:16 -07002196 }
2197 break;
2198 case Instruction::RETURN:
Andreas Gampef10b6e12015-08-12 10:48:12 -07002199 if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) {
jeffhaobdb76512011-09-07 11:43:16 -07002200 /* check the method signature */
Ian Rogersd8f69b02014-09-10 21:43:52 +00002201 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07002202 if (!return_type.IsCategory1Types()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002203 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected non-category 1 return type "
2204 << return_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002205 } else {
2206 // Compilers may generate synthetic functions that write byte values into boolean fields.
2207 // Also, it may use integer values for boolean, byte, short, and character return types.
Sebastien Hertz5243e912013-05-21 10:55:07 +02002208 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07002209 const RegType& src_type = work_line_->GetRegisterType(this, vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07002210 bool use_src = ((return_type.IsBoolean() && src_type.IsByte()) ||
2211 ((return_type.IsBoolean() || return_type.IsByte() ||
2212 return_type.IsShort() || return_type.IsChar()) &&
2213 src_type.IsInteger()));
2214 /* check the register contents */
Ian Rogersad0b3a32012-04-16 14:50:24 -07002215 bool success =
Ian Rogers7b078e82014-09-10 14:44:24 -07002216 work_line_->VerifyRegisterType(this, vregA, use_src ? src_type : return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002217 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002218 AppendToLastFailMessage(StringPrintf(" return-1nr on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07002219 }
jeffhaobdb76512011-09-07 11:43:16 -07002220 }
2221 }
2222 break;
2223 case Instruction::RETURN_WIDE:
Andreas Gampef10b6e12015-08-12 10:48:12 -07002224 if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) {
jeffhaobdb76512011-09-07 11:43:16 -07002225 /* check the method signature */
Ian Rogersd8f69b02014-09-10 21:43:52 +00002226 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07002227 if (!return_type.IsCategory2Types()) {
jeffhaod5347e02012-03-22 17:25:05 -07002228 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-wide not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07002229 } else {
2230 /* check the register contents */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002231 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07002232 bool success = work_line_->VerifyRegisterType(this, vregA, return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002233 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002234 AppendToLastFailMessage(StringPrintf(" return-wide on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07002235 }
jeffhaobdb76512011-09-07 11:43:16 -07002236 }
2237 }
2238 break;
2239 case Instruction::RETURN_OBJECT:
Andreas Gampef10b6e12015-08-12 10:48:12 -07002240 if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) {
Ian Rogersd8f69b02014-09-10 21:43:52 +00002241 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07002242 if (!return_type.IsReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002243 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-object not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07002244 } else {
2245 /* return_type is the *expected* return type, not register value */
2246 DCHECK(!return_type.IsZero());
2247 DCHECK(!return_type.IsUninitializedReference());
Sebastien Hertz5243e912013-05-21 10:55:07 +02002248 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07002249 const RegType& reg_type = work_line_->GetRegisterType(this, vregA);
Andreas Gampea32210c2015-06-24 10:26:13 -07002250 // Disallow returning undefined, conflict & uninitialized values and verify that the
2251 // reference in vAA is an instance of the "return_type."
2252 if (reg_type.IsUndefined()) {
2253 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning undefined register";
2254 } else if (reg_type.IsConflict()) {
2255 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning register with conflict";
2256 } else if (reg_type.IsUninitializedTypes()) {
David Brazdil68b5c0b2016-01-19 14:25:29 +00002257 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning uninitialized object '"
Brian Carlstrom93c33962013-07-26 10:37:43 -07002258 << reg_type << "'";
Andreas Gampea4c98f22015-11-06 16:24:49 -08002259 } else if (!reg_type.IsReferenceTypes()) {
2260 // We really do expect a reference here.
2261 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-object returns a non-reference type "
2262 << reg_type;
David Brazdilca3c8c32016-09-06 14:04:48 +01002263 } else if (!return_type.IsAssignableFrom(reg_type, this)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07002264 if (reg_type.IsUnresolvedTypes() || return_type.IsUnresolvedTypes()) {
2265 Fail(VERIFY_ERROR_NO_CLASS) << " can't resolve returned type '" << return_type
2266 << "' or '" << reg_type << "'";
2267 } else {
Andreas Gampe16f149c2015-03-23 10:10:20 -07002268 bool soft_error = false;
2269 // Check whether arrays are involved. They will show a valid class status, even
2270 // if their components are erroneous.
2271 if (reg_type.IsArrayTypes() && return_type.IsArrayTypes()) {
David Brazdilca3c8c32016-09-06 14:04:48 +01002272 return_type.CanAssignArray(reg_type, reg_types_, class_loader_, this, &soft_error);
Andreas Gampe16f149c2015-03-23 10:10:20 -07002273 if (soft_error) {
2274 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "array with erroneous component type: "
2275 << reg_type << " vs " << return_type;
2276 }
2277 }
2278
2279 if (!soft_error) {
2280 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning '" << reg_type
2281 << "', but expected from declaration '" << return_type << "'";
2282 }
Jeff Haoa3faaf42013-09-03 19:07:00 -07002283 }
jeffhaobdb76512011-09-07 11:43:16 -07002284 }
2285 }
2286 }
2287 break;
2288
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07002289 /* could be boolean, int, float, or a null reference */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002290 case Instruction::CONST_4: {
2291 int32_t val = static_cast<int32_t>(inst->VRegB_11n() << 28) >> 28;
Andreas Gampead238ce2015-08-24 21:13:08 -07002292 work_line_->SetRegisterType<LockOp::kClear>(
2293 this, inst->VRegA_11n(), DetermineCat1Constant(val, need_precise_constants_));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07002294 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02002295 }
2296 case Instruction::CONST_16: {
2297 int16_t val = static_cast<int16_t>(inst->VRegB_21s());
Andreas Gampead238ce2015-08-24 21:13:08 -07002298 work_line_->SetRegisterType<LockOp::kClear>(
2299 this, inst->VRegA_21s(), DetermineCat1Constant(val, need_precise_constants_));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07002300 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02002301 }
Sebastien Hertz849600b2013-12-20 10:28:08 +01002302 case Instruction::CONST: {
2303 int32_t val = inst->VRegB_31i();
Andreas Gampead238ce2015-08-24 21:13:08 -07002304 work_line_->SetRegisterType<LockOp::kClear>(
2305 this, inst->VRegA_31i(), DetermineCat1Constant(val, need_precise_constants_));
jeffhaobdb76512011-09-07 11:43:16 -07002306 break;
Sebastien Hertz849600b2013-12-20 10:28:08 +01002307 }
2308 case Instruction::CONST_HIGH16: {
2309 int32_t val = static_cast<int32_t>(inst->VRegB_21h() << 16);
Andreas Gampead238ce2015-08-24 21:13:08 -07002310 work_line_->SetRegisterType<LockOp::kClear>(
2311 this, inst->VRegA_21h(), DetermineCat1Constant(val, need_precise_constants_));
jeffhaobdb76512011-09-07 11:43:16 -07002312 break;
Sebastien Hertz849600b2013-12-20 10:28:08 +01002313 }
jeffhaobdb76512011-09-07 11:43:16 -07002314 /* could be long or double; resolved upon use */
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002315 case Instruction::CONST_WIDE_16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002316 int64_t val = static_cast<int16_t>(inst->VRegB_21s());
Ian Rogersd8f69b02014-09-10 21:43:52 +00002317 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
2318 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07002319 work_line_->SetRegisterTypeWide(this, inst->VRegA_21s(), lo, hi);
jeffhaobdb76512011-09-07 11:43:16 -07002320 break;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002321 }
2322 case Instruction::CONST_WIDE_32: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002323 int64_t val = static_cast<int32_t>(inst->VRegB_31i());
Ian Rogersd8f69b02014-09-10 21:43:52 +00002324 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
2325 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07002326 work_line_->SetRegisterTypeWide(this, inst->VRegA_31i(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002327 break;
2328 }
2329 case Instruction::CONST_WIDE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002330 int64_t val = inst->VRegB_51l();
Ian Rogersd8f69b02014-09-10 21:43:52 +00002331 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
2332 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07002333 work_line_->SetRegisterTypeWide(this, inst->VRegA_51l(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002334 break;
2335 }
2336 case Instruction::CONST_WIDE_HIGH16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002337 int64_t val = static_cast<uint64_t>(inst->VRegB_21h()) << 48;
Ian Rogersd8f69b02014-09-10 21:43:52 +00002338 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
2339 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07002340 work_line_->SetRegisterTypeWide(this, inst->VRegA_21h(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002341 break;
2342 }
jeffhaobdb76512011-09-07 11:43:16 -07002343 case Instruction::CONST_STRING:
Andreas Gampead238ce2015-08-24 21:13:08 -07002344 work_line_->SetRegisterType<LockOp::kClear>(
2345 this, inst->VRegA_21c(), reg_types_.JavaLangString());
Sebastien Hertz5243e912013-05-21 10:55:07 +02002346 break;
jeffhaobdb76512011-09-07 11:43:16 -07002347 case Instruction::CONST_STRING_JUMBO:
Andreas Gampead238ce2015-08-24 21:13:08 -07002348 work_line_->SetRegisterType<LockOp::kClear>(
2349 this, inst->VRegA_31c(), reg_types_.JavaLangString());
jeffhaobdb76512011-09-07 11:43:16 -07002350 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002351 case Instruction::CONST_CLASS: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002352 // Get type from instruction if unresolved then we need an access check
2353 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Andreas Gampe98be1a92017-08-28 08:25:45 -07002354 const RegType& res_type = ResolveClass<CheckAccess::kYes>(dex::TypeIndex(inst->VRegB_21c()));
Ian Rogersad0b3a32012-04-16 14:50:24 -07002355 // Register holds class, ie its type is class, on error it will hold Conflict.
Andreas Gampead238ce2015-08-24 21:13:08 -07002356 work_line_->SetRegisterType<LockOp::kClear>(
2357 this, inst->VRegA_21c(), res_type.IsConflict() ? res_type
2358 : reg_types_.JavaLangClass());
jeffhaobdb76512011-09-07 11:43:16 -07002359 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002360 }
Orion Hodson2e599942017-09-22 16:17:41 +01002361 case Instruction::CONST_METHOD_HANDLE:
2362 work_line_->SetRegisterType<LockOp::kClear>(
2363 this, inst->VRegA_21c(), reg_types_.JavaLangInvokeMethodHandle());
2364 // TODO: add compiler support for const-method-{handle,type} (b/66890674)
2365 Fail(VERIFY_ERROR_FORCE_INTERPRETER);
2366 break;
2367 case Instruction::CONST_METHOD_TYPE:
2368 work_line_->SetRegisterType<LockOp::kClear>(
2369 this, inst->VRegA_21c(), reg_types_.JavaLangInvokeMethodType());
2370 // TODO: add compiler support for const-method-{handle,type} (b/66890674)
2371 Fail(VERIFY_ERROR_FORCE_INTERPRETER);
2372 break;
jeffhaobdb76512011-09-07 11:43:16 -07002373 case Instruction::MONITOR_ENTER:
Ian Rogers7b078e82014-09-10 14:44:24 -07002374 work_line_->PushMonitor(this, inst->VRegA_11x(), work_insn_idx_);
Andreas Gampec1474102015-08-18 08:57:44 -07002375 // Check whether the previous instruction is a move-object with vAA as a source, creating
2376 // untracked lock aliasing.
Mathieu Chartierde40d472015-10-15 17:47:48 -07002377 if (0 != work_insn_idx_ && !GetInstructionFlags(work_insn_idx_).IsBranchTarget()) {
Andreas Gampec1474102015-08-18 08:57:44 -07002378 uint32_t prev_idx = work_insn_idx_ - 1;
Mathieu Chartierde40d472015-10-15 17:47:48 -07002379 while (0 != prev_idx && !GetInstructionFlags(prev_idx).IsOpcode()) {
Andreas Gampec1474102015-08-18 08:57:44 -07002380 prev_idx--;
2381 }
Mathieu Chartier1d2d4ff2017-09-23 16:11:06 -07002382 const Instruction& prev_inst = code_item_->InstructionAt(prev_idx);
2383 switch (prev_inst.Opcode()) {
Andreas Gampec1474102015-08-18 08:57:44 -07002384 case Instruction::MOVE_OBJECT:
2385 case Instruction::MOVE_OBJECT_16:
2386 case Instruction::MOVE_OBJECT_FROM16:
Mathieu Chartier1d2d4ff2017-09-23 16:11:06 -07002387 if (prev_inst.VRegB() == inst->VRegA_11x()) {
Andreas Gampec1474102015-08-18 08:57:44 -07002388 // Redo the copy. This won't change the register types, but update the lock status
2389 // for the aliased register.
2390 work_line_->CopyRegister1(this,
Mathieu Chartier1d2d4ff2017-09-23 16:11:06 -07002391 prev_inst.VRegA(),
2392 prev_inst.VRegB(),
Andreas Gampec1474102015-08-18 08:57:44 -07002393 kTypeCategoryRef);
2394 }
2395 break;
2396
2397 default: // Other instruction types ignored.
2398 break;
2399 }
2400 }
jeffhaobdb76512011-09-07 11:43:16 -07002401 break;
2402 case Instruction::MONITOR_EXIT:
2403 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002404 * monitor-exit instructions are odd. They can throw exceptions,
jeffhaobdb76512011-09-07 11:43:16 -07002405 * but when they do they act as if they succeeded and the PC is
jeffhaod1f0fde2011-09-08 17:25:33 -07002406 * pointing to the following instruction. (This behavior goes back
jeffhaobdb76512011-09-07 11:43:16 -07002407 * to the need to handle asynchronous exceptions, a now-deprecated
2408 * feature that Dalvik doesn't support.)
2409 *
jeffhaod1f0fde2011-09-08 17:25:33 -07002410 * In practice we don't need to worry about this. The only
jeffhaobdb76512011-09-07 11:43:16 -07002411 * exceptions that can be thrown from monitor-exit are for a
jeffhaod1f0fde2011-09-08 17:25:33 -07002412 * null reference and -exit without a matching -enter. If the
jeffhaobdb76512011-09-07 11:43:16 -07002413 * structured locking checks are working, the former would have
2414 * failed on the -enter instruction, and the latter is impossible.
2415 *
2416 * This is fortunate, because issue 3221411 prevents us from
2417 * chasing the "can throw" path when monitor verification is
jeffhaod1f0fde2011-09-08 17:25:33 -07002418 * enabled. If we can fully verify the locking we can ignore
jeffhaobdb76512011-09-07 11:43:16 -07002419 * some catch blocks (which will show up as "dead" code when
2420 * we skip them here); if we can't, then the code path could be
2421 * "live" so we still need to check it.
2422 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002423 opcode_flags &= ~Instruction::kThrow;
Ian Rogers7b078e82014-09-10 14:44:24 -07002424 work_line_->PopMonitor(this, inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07002425 break;
Ian Rogers28ad40d2011-10-27 15:19:26 -07002426 case Instruction::CHECK_CAST:
Ian Rogersd81871c2011-10-03 13:57:23 -07002427 case Instruction::INSTANCE_OF: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002428 /*
2429 * If this instruction succeeds, we will "downcast" register vA to the type in vB. (This
2430 * could be a "upcast" -- not expected, so we don't try to address it.)
2431 *
2432 * If it fails, an exception is thrown, which we deal with later by ignoring the update to
Elliott Hughesadb8c672012-03-06 16:49:32 -08002433 * dec_insn.vA when branching to a handler.
Ian Rogers28ad40d2011-10-27 15:19:26 -07002434 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002435 const bool is_checkcast = (inst->Opcode() == Instruction::CHECK_CAST);
Andreas Gampea5b09a62016-11-17 15:21:22 -08002436 const dex::TypeIndex type_idx((is_checkcast) ? inst->VRegB_21c() : inst->VRegC_22c());
Andreas Gampe98be1a92017-08-28 08:25:45 -07002437 const RegType& res_type = ResolveClass<CheckAccess::kYes>(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002438 if (res_type.IsConflict()) {
Andreas Gampe00633eb2014-07-17 16:13:35 -07002439 // If this is a primitive type, fail HARD.
Vladimir Marko8d6768d2017-03-14 10:13:21 +00002440 ObjPtr<mirror::Class> klass =
2441 ClassLinker::LookupResolvedType(type_idx, dex_cache_.Get(), class_loader_.Get());
Andreas Gampe00633eb2014-07-17 16:13:35 -07002442 if (klass != nullptr && klass->IsPrimitive()) {
2443 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "using primitive type "
2444 << dex_file_->StringByTypeIdx(type_idx) << " in instanceof in "
2445 << GetDeclaringClass();
2446 break;
2447 }
2448
Ian Rogersad0b3a32012-04-16 14:50:24 -07002449 DCHECK_NE(failures_.size(), 0U);
2450 if (!is_checkcast) {
Andreas Gampead238ce2015-08-24 21:13:08 -07002451 work_line_->SetRegisterType<LockOp::kClear>(this,
2452 inst->VRegA_22c(),
2453 reg_types_.Boolean());
Ian Rogersad0b3a32012-04-16 14:50:24 -07002454 }
2455 break; // bad class
Ian Rogers9f1ab122011-12-12 08:52:43 -08002456 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002457 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Sebastien Hertz5243e912013-05-21 10:55:07 +02002458 uint32_t orig_type_reg = (is_checkcast) ? inst->VRegA_21c() : inst->VRegB_22c();
Ian Rogers7b078e82014-09-10 14:44:24 -07002459 const RegType& orig_type = work_line_->GetRegisterType(this, orig_type_reg);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002460 if (!res_type.IsNonZeroReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002461 if (is_checkcast) {
2462 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on unexpected class " << res_type;
2463 } else {
2464 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on unexpected class " << res_type;
2465 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002466 } else if (!orig_type.IsReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002467 if (is_checkcast) {
2468 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on non-reference in v" << orig_type_reg;
2469 } else {
2470 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on non-reference in v" << orig_type_reg;
2471 }
David Brazdil68b5c0b2016-01-19 14:25:29 +00002472 } else if (orig_type.IsUninitializedTypes()) {
2473 if (is_checkcast) {
2474 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on uninitialized reference in v"
2475 << orig_type_reg;
2476 } else {
2477 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on uninitialized reference in v"
2478 << orig_type_reg;
2479 }
jeffhao2a8a90e2011-09-26 14:25:31 -07002480 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002481 if (is_checkcast) {
Andreas Gampead238ce2015-08-24 21:13:08 -07002482 work_line_->SetRegisterType<LockOp::kKeep>(this, inst->VRegA_21c(), res_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07002483 } else {
Andreas Gampead238ce2015-08-24 21:13:08 -07002484 work_line_->SetRegisterType<LockOp::kClear>(this,
2485 inst->VRegA_22c(),
2486 reg_types_.Boolean());
jeffhaobdb76512011-09-07 11:43:16 -07002487 }
jeffhaobdb76512011-09-07 11:43:16 -07002488 }
jeffhao2a8a90e2011-09-26 14:25:31 -07002489 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002490 }
2491 case Instruction::ARRAY_LENGTH: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002492 const RegType& res_type = work_line_->GetRegisterType(this, inst->VRegB_12x());
Ian Rogers28ad40d2011-10-27 15:19:26 -07002493 if (res_type.IsReferenceTypes()) {
Ian Rogers89310de2012-02-01 13:47:30 -08002494 if (!res_type.IsArrayTypes() && !res_type.IsZero()) { // ie not an array or null
jeffhaod5347e02012-03-22 17:25:05 -07002495 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002496 } else {
Andreas Gampead238ce2015-08-24 21:13:08 -07002497 work_line_->SetRegisterType<LockOp::kClear>(this,
2498 inst->VRegA_12x(),
2499 reg_types_.Integer());
Ian Rogersd81871c2011-10-03 13:57:23 -07002500 }
Andreas Gampe65c9db82014-07-28 13:14:34 -07002501 } else {
2502 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002503 }
2504 break;
2505 }
2506 case Instruction::NEW_INSTANCE: {
Andreas Gampe98be1a92017-08-28 08:25:45 -07002507 const RegType& res_type = ResolveClass<CheckAccess::kYes>(dex::TypeIndex(inst->VRegB_21c()));
Ian Rogersad0b3a32012-04-16 14:50:24 -07002508 if (res_type.IsConflict()) {
2509 DCHECK_NE(failures_.size(), 0U);
2510 break; // bad class
jeffhao8cd6dda2012-02-22 10:15:34 -08002511 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002512 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
2513 // can't create an instance of an interface or abstract class */
2514 if (!res_type.IsInstantiableTypes()) {
2515 Fail(VERIFY_ERROR_INSTANTIATION)
2516 << "new-instance on primitive, interface or abstract class" << res_type;
Ian Rogers08f753d2012-08-24 14:35:25 -07002517 // Soft failure so carry on to set register type.
Ian Rogersd81871c2011-10-03 13:57:23 -07002518 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00002519 const RegType& uninit_type = reg_types_.Uninitialized(res_type, work_insn_idx_);
Ian Rogers08f753d2012-08-24 14:35:25 -07002520 // Any registers holding previous allocations from this address that have not yet been
2521 // initialized must be marked invalid.
Ian Rogers7b078e82014-09-10 14:44:24 -07002522 work_line_->MarkUninitRefsAsInvalid(this, uninit_type);
Ian Rogers08f753d2012-08-24 14:35:25 -07002523 // add the new uninitialized reference to the register state
Andreas Gampead238ce2015-08-24 21:13:08 -07002524 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_21c(), uninit_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07002525 break;
2526 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08002527 case Instruction::NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002528 VerifyNewArray(inst, false, false);
jeffhaobdb76512011-09-07 11:43:16 -07002529 break;
2530 case Instruction::FILLED_NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002531 VerifyNewArray(inst, true, false);
Ian Rogers0c4a5062012-02-03 15:18:59 -08002532 just_set_result = true; // Filled new array sets result register
jeffhaobdb76512011-09-07 11:43:16 -07002533 break;
Ian Rogers0c4a5062012-02-03 15:18:59 -08002534 case Instruction::FILLED_NEW_ARRAY_RANGE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002535 VerifyNewArray(inst, true, true);
Ian Rogers0c4a5062012-02-03 15:18:59 -08002536 just_set_result = true; // Filled new array range sets result register
2537 break;
jeffhaobdb76512011-09-07 11:43:16 -07002538 case Instruction::CMPL_FLOAT:
2539 case Instruction::CMPG_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002540 if (!work_line_->VerifyRegisterType(this, inst->VRegB_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08002541 break;
2542 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002543 if (!work_line_->VerifyRegisterType(this, inst->VRegC_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08002544 break;
2545 }
Andreas Gampead238ce2015-08-24 21:13:08 -07002546 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002547 break;
2548 case Instruction::CMPL_DOUBLE:
2549 case Instruction::CMPG_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002550 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegB_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002551 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002552 break;
2553 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002554 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegC_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002555 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002556 break;
2557 }
Andreas Gampead238ce2015-08-24 21:13:08 -07002558 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002559 break;
2560 case Instruction::CMP_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002561 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegB_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002562 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002563 break;
2564 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002565 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegC_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002566 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002567 break;
2568 }
Andreas Gampead238ce2015-08-24 21:13:08 -07002569 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002570 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002571 case Instruction::THROW: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002572 const RegType& res_type = work_line_->GetRegisterType(this, inst->VRegA_11x());
David Brazdilca3c8c32016-09-06 14:04:48 +01002573 if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(res_type, this)) {
David Brazdil68b5c0b2016-01-19 14:25:29 +00002574 if (res_type.IsUninitializedTypes()) {
2575 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "thrown exception not initialized";
Pavel Vyssotski980027c2016-02-11 20:28:11 +06002576 } else if (!res_type.IsReferenceTypes()) {
2577 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "thrown value of non-reference type " << res_type;
David Brazdil68b5c0b2016-01-19 14:25:29 +00002578 } else {
2579 Fail(res_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS : VERIFY_ERROR_BAD_CLASS_SOFT)
2580 << "thrown class " << res_type << " not instanceof Throwable";
2581 }
jeffhaobdb76512011-09-07 11:43:16 -07002582 }
2583 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002584 }
jeffhaobdb76512011-09-07 11:43:16 -07002585 case Instruction::GOTO:
2586 case Instruction::GOTO_16:
2587 case Instruction::GOTO_32:
2588 /* no effect on or use of registers */
2589 break;
2590
2591 case Instruction::PACKED_SWITCH:
2592 case Instruction::SPARSE_SWITCH:
2593 /* verify that vAA is an integer, or can be converted to one */
Ian Rogers7b078e82014-09-10 14:44:24 -07002594 work_line_->VerifyRegisterType(this, inst->VRegA_31t(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002595 break;
2596
Ian Rogersd81871c2011-10-03 13:57:23 -07002597 case Instruction::FILL_ARRAY_DATA: {
2598 /* Similar to the verification done for APUT */
Ian Rogers7b078e82014-09-10 14:44:24 -07002599 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegA_31t());
Ian Rogers89310de2012-02-01 13:47:30 -08002600 /* array_type can be null if the reg type is Zero */
2601 if (!array_type.IsZero()) {
jeffhao457cc512012-02-02 16:55:13 -08002602 if (!array_type.IsArrayTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002603 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with array type "
2604 << array_type;
Andreas Gampebb18a032016-03-22 20:34:25 -07002605 } else if (array_type.IsUnresolvedTypes()) {
2606 // If it's an unresolved array type, it must be non-primitive.
2607 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data for array of type "
2608 << array_type;
Ian Rogers89310de2012-02-01 13:47:30 -08002609 } else {
Ian Rogersd8f69b02014-09-10 21:43:52 +00002610 const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
Ian Rogersad0b3a32012-04-16 14:50:24 -07002611 DCHECK(!component_type.IsConflict());
jeffhao457cc512012-02-02 16:55:13 -08002612 if (component_type.IsNonZeroReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002613 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with component type "
2614 << component_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002615 } else {
jeffhao457cc512012-02-02 16:55:13 -08002616 // Now verify if the element width in the table matches the element width declared in
2617 // the array
Andreas Gampe53de99c2015-08-17 13:43:55 -07002618 const uint16_t* array_data =
2619 insns + (insns[1] | (static_cast<int32_t>(insns[2]) << 16));
jeffhao457cc512012-02-02 16:55:13 -08002620 if (array_data[0] != Instruction::kArrayDataSignature) {
jeffhaod5347e02012-03-22 17:25:05 -07002621 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid magic for array-data";
jeffhao457cc512012-02-02 16:55:13 -08002622 } else {
2623 size_t elem_width = Primitive::ComponentSize(component_type.GetPrimitiveType());
2624 // Since we don't compress the data in Dex, expect to see equal width of data stored
2625 // in the table and expected from the array class.
2626 if (array_data[1] != elem_width) {
jeffhaod5347e02012-03-22 17:25:05 -07002627 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-data size mismatch (" << array_data[1]
2628 << " vs " << elem_width << ")";
jeffhao457cc512012-02-02 16:55:13 -08002629 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002630 }
2631 }
jeffhaobdb76512011-09-07 11:43:16 -07002632 }
2633 }
2634 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002635 }
jeffhaobdb76512011-09-07 11:43:16 -07002636 case Instruction::IF_EQ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002637 case Instruction::IF_NE: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002638 const RegType& reg_type1 = work_line_->GetRegisterType(this, inst->VRegA_22t());
2639 const RegType& reg_type2 = work_line_->GetRegisterType(this, inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002640 bool mismatch = false;
2641 if (reg_type1.IsZero()) { // zero then integral or reference expected
2642 mismatch = !reg_type2.IsReferenceTypes() && !reg_type2.IsIntegralTypes();
2643 } else if (reg_type1.IsReferenceTypes()) { // both references?
2644 mismatch = !reg_type2.IsReferenceTypes();
2645 } else { // both integral?
2646 mismatch = !reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes();
2647 }
2648 if (mismatch) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002649 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to if-eq/if-ne (" << reg_type1 << ","
2650 << reg_type2 << ") must both be references or integral";
jeffhaobdb76512011-09-07 11:43:16 -07002651 }
2652 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002653 }
jeffhaobdb76512011-09-07 11:43:16 -07002654 case Instruction::IF_LT:
2655 case Instruction::IF_GE:
2656 case Instruction::IF_GT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002657 case Instruction::IF_LE: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002658 const RegType& reg_type1 = work_line_->GetRegisterType(this, inst->VRegA_22t());
2659 const RegType& reg_type2 = work_line_->GetRegisterType(this, inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002660 if (!reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002661 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to 'if' (" << reg_type1 << ","
2662 << reg_type2 << ") must be integral";
jeffhaobdb76512011-09-07 11:43:16 -07002663 }
2664 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002665 }
jeffhaobdb76512011-09-07 11:43:16 -07002666 case Instruction::IF_EQZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002667 case Instruction::IF_NEZ: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002668 const RegType& reg_type = work_line_->GetRegisterType(this, inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002669 if (!reg_type.IsReferenceTypes() && !reg_type.IsIntegralTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002670 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
2671 << " unexpected as arg to if-eqz/if-nez";
Ian Rogersd81871c2011-10-03 13:57:23 -07002672 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002673
2674 // Find previous instruction - its existence is a precondition to peephole optimization.
Ian Rogers9b360392013-06-06 14:45:07 -07002675 uint32_t instance_of_idx = 0;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002676 if (0 != work_insn_idx_) {
Ian Rogers9b360392013-06-06 14:45:07 -07002677 instance_of_idx = work_insn_idx_ - 1;
Mathieu Chartierde40d472015-10-15 17:47:48 -07002678 while (0 != instance_of_idx && !GetInstructionFlags(instance_of_idx).IsOpcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07002679 instance_of_idx--;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002680 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07002681 if (FailOrAbort(this, GetInstructionFlags(instance_of_idx).IsOpcode(),
Andreas Gampe7c038102014-10-27 20:08:46 -07002682 "Unable to get previous instruction of if-eqz/if-nez for work index ",
2683 work_insn_idx_)) {
2684 break;
2685 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002686 } else {
2687 break;
2688 }
2689
Vladimir Markod7559b72017-09-28 13:50:37 +01002690 const Instruction& instance_of_inst = code_item_->InstructionAt(instance_of_idx);
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002691
2692 /* Check for peep-hole pattern of:
2693 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002694 * instance-of vX, vY, T;
2695 * ifXXX vX, label ;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002696 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002697 * label:
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002698 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002699 * and sharpen the type of vY to be type T.
2700 * Note, this pattern can't be if:
2701 * - if there are other branches to this branch,
2702 * - when vX == vY.
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002703 */
Ian Rogersfae370a2013-06-05 08:33:27 -07002704 if (!CurrentInsnFlags()->IsBranchTarget() &&
Vladimir Markod7559b72017-09-28 13:50:37 +01002705 (Instruction::INSTANCE_OF == instance_of_inst.Opcode()) &&
2706 (inst->VRegA_21t() == instance_of_inst.VRegA_22c()) &&
2707 (instance_of_inst.VRegA_22c() != instance_of_inst.VRegB_22c())) {
Ian Rogersebbdd872014-07-07 23:53:08 -07002708 // Check the type of the instance-of is different than that of registers type, as if they
2709 // are the same there is no work to be done here. Check that the conversion is not to or
2710 // from an unresolved type as type information is imprecise. If the instance-of is to an
2711 // interface then ignore the type information as interfaces can only be treated as Objects
2712 // and we don't want to disallow field and other operations on the object. If the value
2713 // being instance-of checked against is known null (zero) then allow the optimization as
2714 // we didn't have type information. If the merge of the instance-of type with the original
2715 // type is assignable to the original then allow optimization. This check is performed to
2716 // ensure that subsequent merges don't lose type information - such as becoming an
2717 // interface from a class that would lose information relevant to field checks.
Vladimir Markod7559b72017-09-28 13:50:37 +01002718 const RegType& orig_type = work_line_->GetRegisterType(this, instance_of_inst.VRegB_22c());
Andreas Gampe98be1a92017-08-28 08:25:45 -07002719 const RegType& cast_type = ResolveClass<CheckAccess::kYes>(
Vladimir Markod7559b72017-09-28 13:50:37 +01002720 dex::TypeIndex(instance_of_inst.VRegC_22c()));
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002721
Ian Rogersebbdd872014-07-07 23:53:08 -07002722 if (!orig_type.Equals(cast_type) &&
2723 !cast_type.IsUnresolvedTypes() && !orig_type.IsUnresolvedTypes() &&
Andreas Gampe00633eb2014-07-17 16:13:35 -07002724 cast_type.HasClass() && // Could be conflict type, make sure it has a class.
Ian Rogersebbdd872014-07-07 23:53:08 -07002725 !cast_type.GetClass()->IsInterface() &&
2726 (orig_type.IsZero() ||
David Brazdilca3c8c32016-09-06 14:04:48 +01002727 orig_type.IsStrictlyAssignableFrom(
2728 cast_type.Merge(orig_type, &reg_types_, this), this))) {
Ian Rogersd0fbd852013-09-24 18:17:04 -07002729 RegisterLine* update_line = RegisterLine::Create(code_item_->registers_size_, this);
Ian Rogersfae370a2013-06-05 08:33:27 -07002730 if (inst->Opcode() == Instruction::IF_EQZ) {
Ian Rogers9b360392013-06-06 14:45:07 -07002731 fallthrough_line.reset(update_line);
Ian Rogersfae370a2013-06-05 08:33:27 -07002732 } else {
Ian Rogers9b360392013-06-06 14:45:07 -07002733 branch_line.reset(update_line);
2734 }
2735 update_line->CopyFromLine(work_line_.get());
Andreas Gampead238ce2015-08-24 21:13:08 -07002736 update_line->SetRegisterType<LockOp::kKeep>(this,
Vladimir Markod7559b72017-09-28 13:50:37 +01002737 instance_of_inst.VRegB_22c(),
Andreas Gampead238ce2015-08-24 21:13:08 -07002738 cast_type);
Mathieu Chartierde40d472015-10-15 17:47:48 -07002739 if (!GetInstructionFlags(instance_of_idx).IsBranchTarget() && 0 != instance_of_idx) {
Ian Rogers9b360392013-06-06 14:45:07 -07002740 // See if instance-of was preceded by a move-object operation, common due to the small
2741 // register encoding space of instance-of, and propagate type information to the source
2742 // of the move-object.
2743 uint32_t move_idx = instance_of_idx - 1;
Mathieu Chartierde40d472015-10-15 17:47:48 -07002744 while (0 != move_idx && !GetInstructionFlags(move_idx).IsOpcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07002745 move_idx--;
2746 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07002747 if (FailOrAbort(this, GetInstructionFlags(move_idx).IsOpcode(),
Andreas Gampe7c038102014-10-27 20:08:46 -07002748 "Unable to get previous instruction of if-eqz/if-nez for work index ",
2749 work_insn_idx_)) {
2750 break;
2751 }
Vladimir Markod7559b72017-09-28 13:50:37 +01002752 const Instruction& move_inst = code_item_->InstructionAt(move_idx);
2753 switch (move_inst.Opcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07002754 case Instruction::MOVE_OBJECT:
Vladimir Markod7559b72017-09-28 13:50:37 +01002755 if (move_inst.VRegA_12x() == instance_of_inst.VRegB_22c()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07002756 update_line->SetRegisterType<LockOp::kKeep>(this,
Vladimir Markod7559b72017-09-28 13:50:37 +01002757 move_inst.VRegB_12x(),
Andreas Gampead238ce2015-08-24 21:13:08 -07002758 cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002759 }
2760 break;
2761 case Instruction::MOVE_OBJECT_FROM16:
Vladimir Markod7559b72017-09-28 13:50:37 +01002762 if (move_inst.VRegA_22x() == instance_of_inst.VRegB_22c()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07002763 update_line->SetRegisterType<LockOp::kKeep>(this,
Vladimir Markod7559b72017-09-28 13:50:37 +01002764 move_inst.VRegB_22x(),
Andreas Gampead238ce2015-08-24 21:13:08 -07002765 cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002766 }
2767 break;
2768 case Instruction::MOVE_OBJECT_16:
Vladimir Markod7559b72017-09-28 13:50:37 +01002769 if (move_inst.VRegA_32x() == instance_of_inst.VRegB_22c()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07002770 update_line->SetRegisterType<LockOp::kKeep>(this,
Vladimir Markod7559b72017-09-28 13:50:37 +01002771 move_inst.VRegB_32x(),
Andreas Gampead238ce2015-08-24 21:13:08 -07002772 cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002773 }
2774 break;
2775 default:
2776 break;
2777 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002778 }
2779 }
2780 }
2781
jeffhaobdb76512011-09-07 11:43:16 -07002782 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002783 }
jeffhaobdb76512011-09-07 11:43:16 -07002784 case Instruction::IF_LTZ:
2785 case Instruction::IF_GEZ:
2786 case Instruction::IF_GTZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002787 case Instruction::IF_LEZ: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002788 const RegType& reg_type = work_line_->GetRegisterType(this, inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002789 if (!reg_type.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002790 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
2791 << " unexpected as arg to if-ltz/if-gez/if-gtz/if-lez";
Ian Rogersd81871c2011-10-03 13:57:23 -07002792 }
jeffhaobdb76512011-09-07 11:43:16 -07002793 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002794 }
jeffhaobdb76512011-09-07 11:43:16 -07002795 case Instruction::AGET_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002796 VerifyAGet(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002797 break;
jeffhaobdb76512011-09-07 11:43:16 -07002798 case Instruction::AGET_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002799 VerifyAGet(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002800 break;
jeffhaobdb76512011-09-07 11:43:16 -07002801 case Instruction::AGET_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002802 VerifyAGet(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002803 break;
jeffhaobdb76512011-09-07 11:43:16 -07002804 case Instruction::AGET_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002805 VerifyAGet(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002806 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002807 case Instruction::AGET:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002808 VerifyAGet(inst, reg_types_.Integer(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002809 break;
jeffhaobdb76512011-09-07 11:43:16 -07002810 case Instruction::AGET_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002811 VerifyAGet(inst, reg_types_.LongLo(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002812 break;
2813 case Instruction::AGET_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002814 VerifyAGet(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07002815 break;
2816
Ian Rogersd81871c2011-10-03 13:57:23 -07002817 case Instruction::APUT_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002818 VerifyAPut(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002819 break;
2820 case Instruction::APUT_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002821 VerifyAPut(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002822 break;
2823 case Instruction::APUT_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002824 VerifyAPut(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002825 break;
2826 case Instruction::APUT_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002827 VerifyAPut(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002828 break;
2829 case Instruction::APUT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002830 VerifyAPut(inst, reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002831 break;
2832 case Instruction::APUT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002833 VerifyAPut(inst, reg_types_.LongLo(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002834 break;
2835 case Instruction::APUT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002836 VerifyAPut(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07002837 break;
2838
jeffhaobdb76512011-09-07 11:43:16 -07002839 case Instruction::IGET_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002840 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002841 break;
jeffhaobdb76512011-09-07 11:43:16 -07002842 case Instruction::IGET_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002843 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002844 break;
jeffhaobdb76512011-09-07 11:43:16 -07002845 case Instruction::IGET_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002846 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002847 break;
jeffhaobdb76512011-09-07 11:43:16 -07002848 case Instruction::IGET_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002849 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002850 break;
2851 case Instruction::IGET:
Andreas Gampe896df402014-10-20 22:25:29 -07002852 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002853 break;
2854 case Instruction::IGET_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002855 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002856 break;
2857 case Instruction::IGET_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002858 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false,
2859 false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002860 break;
jeffhaobdb76512011-09-07 11:43:16 -07002861
Ian Rogersd81871c2011-10-03 13:57:23 -07002862 case Instruction::IPUT_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002863 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002864 break;
2865 case Instruction::IPUT_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002866 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002867 break;
2868 case Instruction::IPUT_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002869 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002870 break;
2871 case Instruction::IPUT_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002872 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002873 break;
2874 case Instruction::IPUT:
Andreas Gampe896df402014-10-20 22:25:29 -07002875 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002876 break;
2877 case Instruction::IPUT_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002878 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002879 break;
jeffhaobdb76512011-09-07 11:43:16 -07002880 case Instruction::IPUT_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002881 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false,
2882 false);
jeffhaobdb76512011-09-07 11:43:16 -07002883 break;
2884
jeffhaobdb76512011-09-07 11:43:16 -07002885 case Instruction::SGET_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002886 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002887 break;
jeffhaobdb76512011-09-07 11:43:16 -07002888 case Instruction::SGET_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002889 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002890 break;
jeffhaobdb76512011-09-07 11:43:16 -07002891 case Instruction::SGET_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002892 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002893 break;
jeffhaobdb76512011-09-07 11:43:16 -07002894 case Instruction::SGET_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002895 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002896 break;
2897 case Instruction::SGET:
Andreas Gampe896df402014-10-20 22:25:29 -07002898 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002899 break;
2900 case Instruction::SGET_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002901 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002902 break;
2903 case Instruction::SGET_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002904 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false,
2905 true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002906 break;
2907
2908 case Instruction::SPUT_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002909 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002910 break;
2911 case Instruction::SPUT_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002912 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002913 break;
2914 case Instruction::SPUT_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002915 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002916 break;
2917 case Instruction::SPUT_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002918 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002919 break;
2920 case Instruction::SPUT:
Andreas Gampe896df402014-10-20 22:25:29 -07002921 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002922 break;
2923 case Instruction::SPUT_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002924 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002925 break;
2926 case Instruction::SPUT_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002927 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false,
2928 true);
jeffhaobdb76512011-09-07 11:43:16 -07002929 break;
2930
2931 case Instruction::INVOKE_VIRTUAL:
2932 case Instruction::INVOKE_VIRTUAL_RANGE:
2933 case Instruction::INVOKE_SUPER:
Ian Rogersd81871c2011-10-03 13:57:23 -07002934 case Instruction::INVOKE_SUPER_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002935 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE ||
2936 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002937 bool is_super = (inst->Opcode() == Instruction::INVOKE_SUPER ||
2938 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
Alex Light7268d472016-01-20 15:50:01 -08002939 MethodType type = is_super ? METHOD_SUPER : METHOD_VIRTUAL;
2940 ArtMethod* called_method = VerifyInvocationArgs(inst, type, is_range);
Ian Rogersd8f69b02014-09-10 21:43:52 +00002941 const RegType* return_type = nullptr;
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002942 if (called_method != nullptr) {
Vladimir Markob45528c2017-07-27 14:14:28 +01002943 ObjPtr<mirror::Class> return_type_class = can_load_classes_
2944 ? called_method->ResolveReturnType()
2945 : called_method->LookupResolvedReturnType();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002946 if (return_type_class != nullptr) {
Andreas Gampef23f33d2015-06-23 14:18:17 -07002947 return_type = &FromClass(called_method->GetReturnTypeDescriptor(),
Vladimir Markob45528c2017-07-27 14:14:28 +01002948 return_type_class.Ptr(),
Andreas Gampef23f33d2015-06-23 14:18:17 -07002949 return_type_class->CannotBeAssignedFromOtherTypes());
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002950 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07002951 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
2952 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002953 }
2954 }
2955 if (return_type == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002956 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002957 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
Andreas Gampea5b09a62016-11-17 15:21:22 -08002958 dex::TypeIndex return_type_idx =
2959 dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002960 const char* descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Mathieu Chartierbf99f772014-08-23 16:37:27 -07002961 return_type = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
jeffhaobdb76512011-09-07 11:43:16 -07002962 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002963 if (!return_type->IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002964 work_line_->SetResultRegisterType(this, *return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002965 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002966 work_line_->SetResultRegisterTypeWide(*return_type, return_type->HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002967 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002968 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002969 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002970 }
jeffhaobdb76512011-09-07 11:43:16 -07002971 case Instruction::INVOKE_DIRECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002972 case Instruction::INVOKE_DIRECT_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002973 bool is_range = (inst->Opcode() == Instruction::INVOKE_DIRECT_RANGE);
Alex Light7268d472016-01-20 15:50:01 -08002974 ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_DIRECT, is_range);
Ian Rogers46685432012-06-03 22:26:43 -07002975 const char* return_type_descriptor;
2976 bool is_constructor;
Ian Rogersd8f69b02014-09-10 21:43:52 +00002977 const RegType* return_type = nullptr;
Ian Rogers7b078e82014-09-10 14:44:24 -07002978 if (called_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002979 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogers46685432012-06-03 22:26:43 -07002980 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
Ian Rogersdfb325e2013-10-30 01:00:44 -07002981 is_constructor = strcmp("<init>", dex_file_->StringDataByIdx(method_id.name_idx_)) == 0;
Andreas Gampea5b09a62016-11-17 15:21:22 -08002982 dex::TypeIndex return_type_idx =
2983 dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogers46685432012-06-03 22:26:43 -07002984 return_type_descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2985 } else {
2986 is_constructor = called_method->IsConstructor();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002987 return_type_descriptor = called_method->GetReturnTypeDescriptor();
Vladimir Markob45528c2017-07-27 14:14:28 +01002988 ObjPtr<mirror::Class> return_type_class = can_load_classes_
2989 ? called_method->ResolveReturnType()
2990 : called_method->LookupResolvedReturnType();
Ian Rogers1ff3c982014-08-12 02:30:58 -07002991 if (return_type_class != nullptr) {
Andreas Gampef23f33d2015-06-23 14:18:17 -07002992 return_type = &FromClass(return_type_descriptor,
Vladimir Markob45528c2017-07-27 14:14:28 +01002993 return_type_class.Ptr(),
Andreas Gampef23f33d2015-06-23 14:18:17 -07002994 return_type_class->CannotBeAssignedFromOtherTypes());
Ian Rogers1ff3c982014-08-12 02:30:58 -07002995 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07002996 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
2997 self_->ClearException();
Ian Rogers1ff3c982014-08-12 02:30:58 -07002998 }
Ian Rogers46685432012-06-03 22:26:43 -07002999 }
3000 if (is_constructor) {
jeffhaobdb76512011-09-07 11:43:16 -07003001 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003002 * Some additional checks when calling a constructor. We know from the invocation arg check
3003 * that the "this" argument is an instance of called_method->klass. Now we further restrict
3004 * that to require that called_method->klass is the same as this->klass or this->super,
3005 * allowing the latter only if the "this" argument is the same as the "this" argument to
3006 * this method (which implies that we're in a constructor ourselves).
jeffhaobdb76512011-09-07 11:43:16 -07003007 */
Orion Hodsoncfa325e2016-10-13 10:25:54 +01003008 const RegType& this_type = work_line_->GetInvocationThis(this, inst);
jeffhaob57e9522012-04-26 18:08:21 -07003009 if (this_type.IsConflict()) // failure.
3010 break;
jeffhaobdb76512011-09-07 11:43:16 -07003011
jeffhaob57e9522012-04-26 18:08:21 -07003012 /* no null refs allowed (?) */
3013 if (this_type.IsZero()) {
3014 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unable to initialize null ref";
3015 break;
jeffhao2a8a90e2011-09-26 14:25:31 -07003016 }
jeffhaob57e9522012-04-26 18:08:21 -07003017
3018 /* must be in same class or in superclass */
Ian Rogersd8f69b02014-09-10 21:43:52 +00003019 // const RegType& this_super_klass = this_type.GetSuperClass(&reg_types_);
Ian Rogers46685432012-06-03 22:26:43 -07003020 // TODO: re-enable constructor type verification
3021 // if (this_super_klass.IsConflict()) {
jeffhaob57e9522012-04-26 18:08:21 -07003022 // Unknown super class, fail so we re-check at runtime.
Ian Rogers46685432012-06-03 22:26:43 -07003023 // Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "super class unknown for '" << this_type << "'";
3024 // break;
3025 // }
jeffhaob57e9522012-04-26 18:08:21 -07003026
3027 /* arg must be an uninitialized reference */
3028 if (!this_type.IsUninitializedTypes()) {
3029 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Expected initialization on uninitialized reference "
3030 << this_type;
3031 break;
3032 }
3033
3034 /*
3035 * Replace the uninitialized reference with an initialized one. We need to do this for all
3036 * registers that have the same object instance in them, not just the "this" register.
3037 */
Nicolas Geoffray98e6ce42016-02-16 18:42:15 +00003038 work_line_->MarkRefsAsInitialized(this, this_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003039 }
Ian Rogers1ff3c982014-08-12 02:30:58 -07003040 if (return_type == nullptr) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07003041 return_type = &reg_types_.FromDescriptor(GetClassLoader(), return_type_descriptor, false);
Ian Rogers1ff3c982014-08-12 02:30:58 -07003042 }
3043 if (!return_type->IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003044 work_line_->SetResultRegisterType(this, *return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003045 } else {
Ian Rogers1ff3c982014-08-12 02:30:58 -07003046 work_line_->SetResultRegisterTypeWide(*return_type, return_type->HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003047 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003048 just_set_result = true;
3049 break;
3050 }
3051 case Instruction::INVOKE_STATIC:
3052 case Instruction::INVOKE_STATIC_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003053 bool is_range = (inst->Opcode() == Instruction::INVOKE_STATIC_RANGE);
Alex Light7268d472016-01-20 15:50:01 -08003054 ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_STATIC, is_range);
Ian Rogers28ad40d2011-10-27 15:19:26 -07003055 const char* descriptor;
Ian Rogers7b078e82014-09-10 14:44:24 -07003056 if (called_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003057 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogers28ad40d2011-10-27 15:19:26 -07003058 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
Andreas Gampea5b09a62016-11-17 15:21:22 -08003059 dex::TypeIndex return_type_idx =
3060 dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Mathieu Chartier590fee92013-09-13 13:46:47 -07003061 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07003062 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003063 descriptor = called_method->GetReturnTypeDescriptor();
Ian Rogers28ad40d2011-10-27 15:19:26 -07003064 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00003065 const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003066 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003067 work_line_->SetResultRegisterType(this, return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003068 } else {
3069 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
3070 }
jeffhaobdb76512011-09-07 11:43:16 -07003071 just_set_result = true;
3072 }
3073 break;
jeffhaobdb76512011-09-07 11:43:16 -07003074 case Instruction::INVOKE_INTERFACE:
Ian Rogersd81871c2011-10-03 13:57:23 -07003075 case Instruction::INVOKE_INTERFACE_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003076 bool is_range = (inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE);
Alex Light7268d472016-01-20 15:50:01 -08003077 ArtMethod* abs_method = VerifyInvocationArgs(inst, METHOD_INTERFACE, is_range);
Ian Rogers7b078e82014-09-10 14:44:24 -07003078 if (abs_method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003079 mirror::Class* called_interface = abs_method->GetDeclaringClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07003080 if (!called_interface->IsInterface() && !called_interface->IsObjectClass()) {
3081 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected interface class in invoke-interface '"
David Sehr709b0702016-10-13 09:12:37 -07003082 << abs_method->PrettyMethod() << "'";
Ian Rogersad0b3a32012-04-16 14:50:24 -07003083 break;
Ian Rogers28ad40d2011-10-27 15:19:26 -07003084 }
Ian Rogers0d604842012-04-16 14:50:24 -07003085 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003086 /* Get the type of the "this" arg, which should either be a sub-interface of called
3087 * interface or Object (see comments in RegType::JoinClass).
3088 */
Orion Hodsoncfa325e2016-10-13 10:25:54 +01003089 const RegType& this_type = work_line_->GetInvocationThis(this, inst);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003090 if (this_type.IsZero()) {
3091 /* null pointer always passes (and always fails at runtime) */
3092 } else {
3093 if (this_type.IsUninitializedTypes()) {
3094 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface call on uninitialized object "
3095 << this_type;
3096 break;
3097 }
3098 // In the past we have tried to assert that "called_interface" is assignable
3099 // from "this_type.GetClass()", however, as we do an imprecise Join
3100 // (RegType::JoinClass) we don't have full information on what interfaces are
3101 // implemented by "this_type". For example, two classes may implement the same
3102 // interfaces and have a common parent that doesn't implement the interface. The
3103 // join will set "this_type" to the parent class and a test that this implements
3104 // the interface will incorrectly fail.
3105 }
3106 /*
3107 * We don't have an object instance, so we can't find the concrete method. However, all of
3108 * the type information is in the abstract method, so we're good.
3109 */
3110 const char* descriptor;
Ian Rogers7b078e82014-09-10 14:44:24 -07003111 if (abs_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003112 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07003113 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
Andreas Gampea5b09a62016-11-17 15:21:22 -08003114 dex::TypeIndex return_type_idx =
3115 dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Mathieu Chartierde40d472015-10-15 17:47:48 -07003116 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003117 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003118 descriptor = abs_method->GetReturnTypeDescriptor();
Ian Rogersad0b3a32012-04-16 14:50:24 -07003119 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00003120 const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003121 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003122 work_line_->SetResultRegisterType(this, return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003123 } else {
3124 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
3125 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003126 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07003127 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07003128 }
Narayan Kamath9823e782016-08-03 12:46:58 +01003129 case Instruction::INVOKE_POLYMORPHIC:
3130 case Instruction::INVOKE_POLYMORPHIC_RANGE: {
Orion Hodsoncfa325e2016-10-13 10:25:54 +01003131 bool is_range = (inst->Opcode() == Instruction::INVOKE_POLYMORPHIC_RANGE);
3132 ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_POLYMORPHIC, is_range);
3133 if (called_method == nullptr) {
3134 // Convert potential soft failures in VerifyInvocationArgs() to hard errors.
3135 if (failure_messages_.size() > 0) {
3136 std::string message = failure_messages_.back()->str();
3137 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << message;
3138 } else {
3139 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invoke-polymorphic verification failure.";
3140 }
3141 break;
3142 }
3143 if (!CheckSignaturePolymorphicMethod(called_method) ||
3144 !CheckSignaturePolymorphicReceiver(inst)) {
3145 break;
3146 }
3147 const uint32_t proto_idx = (is_range) ? inst->VRegH_4rcc() : inst->VRegH_45cc();
Orion Hodsonac141392017-01-13 11:53:47 +00003148 const char* return_descriptor =
Orion Hodsoncfa325e2016-10-13 10:25:54 +01003149 dex_file_->GetReturnTypeDescriptor(dex_file_->GetProtoId(proto_idx));
3150 const RegType& return_type =
Orion Hodsonac141392017-01-13 11:53:47 +00003151 reg_types_.FromDescriptor(GetClassLoader(), return_descriptor, false);
Orion Hodsoncfa325e2016-10-13 10:25:54 +01003152 if (!return_type.IsLowHalf()) {
3153 work_line_->SetResultRegisterType(this, return_type);
3154 } else {
3155 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
3156 }
Orion Hodsonac141392017-01-13 11:53:47 +00003157 just_set_result = true;
Orion Hodsoncfa325e2016-10-13 10:25:54 +01003158 break;
Narayan Kamath9823e782016-08-03 12:46:58 +01003159 }
Orion Hodsonc069a302017-01-18 09:23:12 +00003160 case Instruction::INVOKE_CUSTOM:
3161 case Instruction::INVOKE_CUSTOM_RANGE: {
3162 // Verify registers based on method_type in the call site.
3163 bool is_range = (inst->Opcode() == Instruction::INVOKE_CUSTOM_RANGE);
3164
3165 // Step 1. Check the call site that produces the method handle for invocation
3166 const uint32_t call_site_idx = is_range ? inst->VRegB_3rc() : inst->VRegB_35c();
3167 if (!CheckCallSite(call_site_idx)) {
3168 DCHECK(HasFailures());
3169 break;
3170 }
3171
3172 // Step 2. Check the register arguments correspond to the expected arguments for the
3173 // method handle produced by step 1. The dex file verifier has checked ranges for
3174 // the first three arguments and CheckCallSite has checked the method handle type.
3175 CallSiteArrayValueIterator it(*dex_file_, dex_file_->GetCallSiteId(call_site_idx));
3176 it.Next(); // Skip to name.
3177 it.Next(); // Skip to method type of the method handle
3178 const uint32_t proto_idx = static_cast<uint32_t>(it.GetJavaValue().i);
3179 const DexFile::ProtoId& proto_id = dex_file_->GetProtoId(proto_idx);
3180 DexFileParameterIterator param_it(*dex_file_, proto_id);
3181 // Treat method as static as it has yet to be determined.
3182 VerifyInvocationArgsFromIterator(&param_it, inst, METHOD_STATIC, is_range, nullptr);
3183 const char* return_descriptor = dex_file_->GetReturnTypeDescriptor(proto_id);
3184
3185 // Step 3. Propagate return type information
3186 const RegType& return_type =
3187 reg_types_.FromDescriptor(GetClassLoader(), return_descriptor, false);
3188 if (!return_type.IsLowHalf()) {
3189 work_line_->SetResultRegisterType(this, return_type);
3190 } else {
3191 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
3192 }
3193 just_set_result = true;
3194 // TODO: Add compiler support for invoke-custom (b/35337872).
3195 Fail(VERIFY_ERROR_FORCE_INTERPRETER);
3196 break;
3197 }
jeffhaobdb76512011-09-07 11:43:16 -07003198 case Instruction::NEG_INT:
3199 case Instruction::NOT_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003200 work_line_->CheckUnaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003201 break;
3202 case Instruction::NEG_LONG:
3203 case Instruction::NOT_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07003204 work_line_->CheckUnaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003205 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07003206 break;
3207 case Instruction::NEG_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003208 work_line_->CheckUnaryOp(this, inst, reg_types_.Float(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07003209 break;
3210 case Instruction::NEG_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07003211 work_line_->CheckUnaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003212 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07003213 break;
3214 case Instruction::INT_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07003215 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003216 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003217 break;
3218 case Instruction::INT_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003219 work_line_->CheckUnaryOp(this, inst, reg_types_.Float(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003220 break;
3221 case Instruction::INT_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07003222 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003223 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003224 break;
3225 case Instruction::LONG_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003226 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003227 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07003228 break;
3229 case Instruction::LONG_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003230 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003231 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07003232 break;
3233 case Instruction::LONG_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07003234 work_line_->CheckUnaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003235 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07003236 break;
3237 case Instruction::FLOAT_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003238 work_line_->CheckUnaryOp(this, inst, reg_types_.Integer(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07003239 break;
3240 case Instruction::FLOAT_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07003241 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003242 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07003243 break;
3244 case Instruction::FLOAT_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07003245 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003246 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07003247 break;
3248 case Instruction::DOUBLE_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003249 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003250 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07003251 break;
3252 case Instruction::DOUBLE_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07003253 work_line_->CheckUnaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003254 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07003255 break;
3256 case Instruction::DOUBLE_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003257 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003258 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07003259 break;
3260 case Instruction::INT_TO_BYTE:
Ian Rogers7b078e82014-09-10 14:44:24 -07003261 work_line_->CheckUnaryOp(this, inst, reg_types_.Byte(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003262 break;
3263 case Instruction::INT_TO_CHAR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003264 work_line_->CheckUnaryOp(this, inst, reg_types_.Char(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003265 break;
3266 case Instruction::INT_TO_SHORT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003267 work_line_->CheckUnaryOp(this, inst, reg_types_.Short(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003268 break;
3269
3270 case Instruction::ADD_INT:
3271 case Instruction::SUB_INT:
3272 case Instruction::MUL_INT:
3273 case Instruction::REM_INT:
3274 case Instruction::DIV_INT:
3275 case Instruction::SHL_INT:
3276 case Instruction::SHR_INT:
3277 case Instruction::USHR_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003278 work_line_->CheckBinaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003279 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07003280 break;
3281 case Instruction::AND_INT:
3282 case Instruction::OR_INT:
3283 case Instruction::XOR_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(), true);
jeffhaobdb76512011-09-07 11:43:16 -07003286 break;
3287 case Instruction::ADD_LONG:
3288 case Instruction::SUB_LONG:
3289 case Instruction::MUL_LONG:
3290 case Instruction::DIV_LONG:
3291 case Instruction::REM_LONG:
3292 case Instruction::AND_LONG:
3293 case Instruction::OR_LONG:
3294 case Instruction::XOR_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07003295 work_line_->CheckBinaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003296 reg_types_.LongLo(), reg_types_.LongHi(),
3297 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07003298 break;
3299 case Instruction::SHL_LONG:
3300 case Instruction::SHR_LONG:
3301 case Instruction::USHR_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07003302 /* shift distance is Int, making these different from other binary operations */
Ian Rogers7b078e82014-09-10 14:44:24 -07003303 work_line_->CheckBinaryOpWideShift(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003304 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003305 break;
3306 case Instruction::ADD_FLOAT:
3307 case Instruction::SUB_FLOAT:
3308 case Instruction::MUL_FLOAT:
3309 case Instruction::DIV_FLOAT:
3310 case Instruction::REM_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003311 work_line_->CheckBinaryOp(this, inst, reg_types_.Float(), reg_types_.Float(),
3312 reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07003313 break;
3314 case Instruction::ADD_DOUBLE:
3315 case Instruction::SUB_DOUBLE:
3316 case Instruction::MUL_DOUBLE:
3317 case Instruction::DIV_DOUBLE:
3318 case Instruction::REM_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07003319 work_line_->CheckBinaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003320 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
3321 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07003322 break;
3323 case Instruction::ADD_INT_2ADDR:
3324 case Instruction::SUB_INT_2ADDR:
3325 case Instruction::MUL_INT_2ADDR:
3326 case Instruction::REM_INT_2ADDR:
3327 case Instruction::SHL_INT_2ADDR:
3328 case Instruction::SHR_INT_2ADDR:
3329 case Instruction::USHR_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003330 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
3331 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07003332 break;
3333 case Instruction::AND_INT_2ADDR:
3334 case Instruction::OR_INT_2ADDR:
3335 case Instruction::XOR_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(), true);
jeffhaobdb76512011-09-07 11:43:16 -07003338 break;
3339 case Instruction::DIV_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003340 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
3341 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07003342 break;
3343 case Instruction::ADD_LONG_2ADDR:
3344 case Instruction::SUB_LONG_2ADDR:
3345 case Instruction::MUL_LONG_2ADDR:
3346 case Instruction::DIV_LONG_2ADDR:
3347 case Instruction::REM_LONG_2ADDR:
3348 case Instruction::AND_LONG_2ADDR:
3349 case Instruction::OR_LONG_2ADDR:
3350 case Instruction::XOR_LONG_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003351 work_line_->CheckBinaryOp2addrWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003352 reg_types_.LongLo(), reg_types_.LongHi(),
3353 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07003354 break;
3355 case Instruction::SHL_LONG_2ADDR:
3356 case Instruction::SHR_LONG_2ADDR:
3357 case Instruction::USHR_LONG_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003358 work_line_->CheckBinaryOp2addrWideShift(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003359 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003360 break;
3361 case Instruction::ADD_FLOAT_2ADDR:
3362 case Instruction::SUB_FLOAT_2ADDR:
3363 case Instruction::MUL_FLOAT_2ADDR:
3364 case Instruction::DIV_FLOAT_2ADDR:
3365 case Instruction::REM_FLOAT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003366 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Float(), reg_types_.Float(),
3367 reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07003368 break;
3369 case Instruction::ADD_DOUBLE_2ADDR:
3370 case Instruction::SUB_DOUBLE_2ADDR:
3371 case Instruction::MUL_DOUBLE_2ADDR:
3372 case Instruction::DIV_DOUBLE_2ADDR:
3373 case Instruction::REM_DOUBLE_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003374 work_line_->CheckBinaryOp2addrWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003375 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
3376 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07003377 break;
3378 case Instruction::ADD_INT_LIT16:
Ian Rogersf72a11d2014-10-30 15:41:08 -07003379 case Instruction::RSUB_INT_LIT16:
jeffhaobdb76512011-09-07 11:43:16 -07003380 case Instruction::MUL_INT_LIT16:
3381 case Instruction::DIV_INT_LIT16:
3382 case Instruction::REM_INT_LIT16:
Ian Rogers7b078e82014-09-10 14:44:24 -07003383 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), false,
3384 true);
jeffhaobdb76512011-09-07 11:43:16 -07003385 break;
3386 case Instruction::AND_INT_LIT16:
3387 case Instruction::OR_INT_LIT16:
3388 case Instruction::XOR_INT_LIT16:
Ian Rogers7b078e82014-09-10 14:44:24 -07003389 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), true,
3390 true);
jeffhaobdb76512011-09-07 11:43:16 -07003391 break;
3392 case Instruction::ADD_INT_LIT8:
3393 case Instruction::RSUB_INT_LIT8:
3394 case Instruction::MUL_INT_LIT8:
3395 case Instruction::DIV_INT_LIT8:
3396 case Instruction::REM_INT_LIT8:
3397 case Instruction::SHL_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07003398 case Instruction::SHR_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07003399 case Instruction::USHR_INT_LIT8:
Ian Rogers7b078e82014-09-10 14:44:24 -07003400 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), false,
3401 false);
jeffhaobdb76512011-09-07 11:43:16 -07003402 break;
3403 case Instruction::AND_INT_LIT8:
3404 case Instruction::OR_INT_LIT8:
3405 case Instruction::XOR_INT_LIT8:
Ian Rogers7b078e82014-09-10 14:44:24 -07003406 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), true,
3407 false);
jeffhaobdb76512011-09-07 11:43:16 -07003408 break;
3409
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003410 // Special instructions.
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07003411 case Instruction::RETURN_VOID_NO_BARRIER:
3412 if (IsConstructor() && !IsStatic()) {
3413 auto& declaring_class = GetDeclaringClass();
Andreas Gampe68df3202015-06-22 11:35:46 -07003414 if (declaring_class.IsUnresolvedReference()) {
3415 // We must iterate over the fields, even if we cannot use mirror classes to do so. Do it
3416 // manually over the underlying dex file.
3417 uint32_t first_index = GetFirstFinalInstanceFieldIndex(*dex_file_,
3418 dex_file_->GetMethodId(dex_method_idx_).class_idx_);
Andreas Gampee2abbc62017-09-15 11:59:26 -07003419 if (first_index != dex::kDexNoIndex) {
Andreas Gampe68df3202015-06-22 11:35:46 -07003420 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void-no-barrier not expected for field "
3421 << first_index;
3422 }
3423 break;
3424 }
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07003425 auto* klass = declaring_class.GetClass();
3426 for (uint32_t i = 0, num_fields = klass->NumInstanceFields(); i < num_fields; ++i) {
3427 if (klass->GetInstanceField(i)->IsFinal()) {
Mathieu Chartiere86deef2015-03-19 13:43:37 -07003428 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void-no-barrier not expected for "
David Sehr709b0702016-10-13 09:12:37 -07003429 << klass->GetInstanceField(i)->PrettyField();
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07003430 break;
3431 }
3432 }
Sebastien Hertzcc10e0e2013-06-28 14:24:48 +02003433 }
Andreas Gampeb2917962015-07-31 13:36:10 -07003434 // Handle this like a RETURN_VOID now. Code is duplicated to separate standard from
3435 // quickened opcodes (otherwise this could be a fall-through).
3436 if (!IsConstructor()) {
3437 if (!GetMethodReturnType().IsConflict()) {
3438 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void not expected";
3439 }
3440 }
Sebastien Hertzcc10e0e2013-06-28 14:24:48 +02003441 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003442 // Note: the following instructions encode offsets derived from class linking.
Neil Fuller0e844392016-09-08 13:43:31 +01003443 // As such they use Class*/Field*/Executable* as these offsets only have
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003444 // meaning if the class linking and resolution were successful.
3445 case Instruction::IGET_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003446 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003447 break;
3448 case Instruction::IGET_WIDE_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003449 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003450 break;
3451 case Instruction::IGET_OBJECT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003452 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003453 break;
Mathieu Chartierffc605c2014-12-10 10:35:44 -08003454 case Instruction::IGET_BOOLEAN_QUICK:
3455 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true);
3456 break;
3457 case Instruction::IGET_BYTE_QUICK:
3458 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true);
3459 break;
3460 case Instruction::IGET_CHAR_QUICK:
3461 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true);
3462 break;
3463 case Instruction::IGET_SHORT_QUICK:
3464 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true);
3465 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003466 case Instruction::IPUT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003467 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003468 break;
Fred Shih37f05ef2014-07-16 18:38:08 -07003469 case Instruction::IPUT_BOOLEAN_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003470 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07003471 break;
3472 case Instruction::IPUT_BYTE_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003473 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07003474 break;
3475 case Instruction::IPUT_CHAR_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003476 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07003477 break;
3478 case Instruction::IPUT_SHORT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003479 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07003480 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003481 case Instruction::IPUT_WIDE_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003482 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003483 break;
3484 case Instruction::IPUT_OBJECT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003485 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003486 break;
3487 case Instruction::INVOKE_VIRTUAL_QUICK:
3488 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: {
3489 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
Mathieu Chartiere401d142015-04-22 13:56:20 -07003490 ArtMethod* called_method = VerifyInvokeVirtualQuickArgs(inst, is_range);
Ian Rogers7b078e82014-09-10 14:44:24 -07003491 if (called_method != nullptr) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003492 const char* descriptor = called_method->GetReturnTypeDescriptor();
Ian Rogersd8f69b02014-09-10 21:43:52 +00003493 const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003494 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003495 work_line_->SetResultRegisterType(this, return_type);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003496 } else {
3497 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
3498 }
3499 just_set_result = true;
3500 }
3501 break;
3502 }
3503
Ian Rogersd81871c2011-10-03 13:57:23 -07003504 /* These should never appear during verification. */
Mathieu Chartierffc605c2014-12-10 10:35:44 -08003505 case Instruction::UNUSED_3E ... Instruction::UNUSED_43:
Narayan Kamath8ec3bd22016-08-03 12:46:23 +01003506 case Instruction::UNUSED_F3 ... Instruction::UNUSED_F9:
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003507 case Instruction::UNUSED_79:
3508 case Instruction::UNUSED_7A:
jeffhaod5347e02012-03-22 17:25:05 -07003509 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Unexpected opcode " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07003510 break;
3511
3512 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003513 * DO NOT add a "default" clause here. Without it the compiler will
jeffhaobdb76512011-09-07 11:43:16 -07003514 * complain if an instruction is missing (which is desirable).
3515 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003516 } // end - switch (dec_insn.opcode)
jeffhaobdb76512011-09-07 11:43:16 -07003517
Ian Rogersad0b3a32012-04-16 14:50:24 -07003518 if (have_pending_hard_failure_) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08003519 if (Runtime::Current()->IsAotCompiler()) {
3520 /* When AOT compiling, check that the last failure is a hard failure */
Andreas Gampeb588f4c2015-05-26 13:35:39 -07003521 if (failures_[failures_.size() - 1] != VERIFY_ERROR_BAD_CLASS_HARD) {
3522 LOG(ERROR) << "Pending failures:";
3523 for (auto& error : failures_) {
3524 LOG(ERROR) << error;
3525 }
3526 for (auto& error_msg : failure_messages_) {
3527 LOG(ERROR) << error_msg->str();
3528 }
3529 LOG(FATAL) << "Pending hard failure, but last failure not hard.";
3530 }
Ian Rogerse1758fe2012-04-19 11:31:15 -07003531 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003532 /* immediate failure, reject class */
3533 info_messages_ << "Rejecting opcode " << inst->DumpString(dex_file_);
3534 return false;
jeffhaofaf459e2012-08-31 15:32:47 -07003535 } else if (have_pending_runtime_throw_failure_) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07003536 /* checking interpreter will throw, mark following code as unreachable */
jeffhaofaf459e2012-08-31 15:32:47 -07003537 opcode_flags = Instruction::kThrow;
Andreas Gampea727e372015-08-25 09:22:37 -07003538 // Note: the flag must be reset as it is only global to decouple Fail and is semantically per
3539 // instruction. However, RETURN checking may throw LOCKING errors, so we clear at the
3540 // very end.
jeffhaobdb76512011-09-07 11:43:16 -07003541 }
jeffhaobdb76512011-09-07 11:43:16 -07003542 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003543 * If we didn't just set the result register, clear it out. This ensures that you can only use
3544 * "move-result" immediately after the result is set. (We could check this statically, but it's
3545 * not expensive and it makes our debugging output cleaner.)
jeffhaobdb76512011-09-07 11:43:16 -07003546 */
3547 if (!just_set_result) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003548 work_line_->SetResultTypeToUnknown(this);
jeffhaobdb76512011-09-07 11:43:16 -07003549 }
3550
jeffhaobdb76512011-09-07 11:43:16 -07003551 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003552 * Handle "branch". Tag the branch target.
jeffhaobdb76512011-09-07 11:43:16 -07003553 *
3554 * NOTE: instructions like Instruction::EQZ provide information about the
jeffhaod1f0fde2011-09-08 17:25:33 -07003555 * state of the register when the branch is taken or not taken. For example,
jeffhaobdb76512011-09-07 11:43:16 -07003556 * somebody could get a reference field, check it for zero, and if the
3557 * branch is taken immediately store that register in a boolean field
jeffhaod1f0fde2011-09-08 17:25:33 -07003558 * since the value is known to be zero. We do not currently account for
jeffhaobdb76512011-09-07 11:43:16 -07003559 * that, and will reject the code.
3560 *
3561 * TODO: avoid re-fetching the branch target
3562 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003563 if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07003564 bool isConditional, selfOkay;
Ian Rogersd81871c2011-10-03 13:57:23 -07003565 if (!GetBranchOffset(work_insn_idx_, &branch_target, &isConditional, &selfOkay)) {
jeffhaobdb76512011-09-07 11:43:16 -07003566 /* should never happen after static verification */
jeffhaod5347e02012-03-22 17:25:05 -07003567 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad branch";
jeffhaobdb76512011-09-07 11:43:16 -07003568 return false;
3569 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08003570 DCHECK_EQ(isConditional, (opcode_flags & Instruction::kContinue) != 0);
Stephen Kyle9bc61992014-09-22 13:53:15 +01003571 if (!CheckNotMoveExceptionOrMoveResult(code_item_->insns_, work_insn_idx_ + branch_target)) {
jeffhaobdb76512011-09-07 11:43:16 -07003572 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07003573 }
jeffhaobdb76512011-09-07 11:43:16 -07003574 /* update branch target, set "changed" if appropriate */
Mathieu Chartierde40d472015-10-15 17:47:48 -07003575 if (nullptr != branch_line) {
Ian Rogersebbdd872014-07-07 23:53:08 -07003576 if (!UpdateRegisters(work_insn_idx_ + branch_target, branch_line.get(), false)) {
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003577 return false;
3578 }
3579 } else {
Ian Rogersebbdd872014-07-07 23:53:08 -07003580 if (!UpdateRegisters(work_insn_idx_ + branch_target, work_line_.get(), false)) {
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003581 return false;
3582 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003583 }
jeffhaobdb76512011-09-07 11:43:16 -07003584 }
3585
3586 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003587 * Handle "switch". Tag all possible branch targets.
jeffhaobdb76512011-09-07 11:43:16 -07003588 *
3589 * We've already verified that the table is structurally sound, so we
3590 * just need to walk through and tag the targets.
3591 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003592 if ((opcode_flags & Instruction::kSwitch) != 0) {
Andreas Gampe53de99c2015-08-17 13:43:55 -07003593 int offset_to_switch = insns[1] | (static_cast<int32_t>(insns[2]) << 16);
jeffhaobdb76512011-09-07 11:43:16 -07003594 const uint16_t* switch_insns = insns + offset_to_switch;
3595 int switch_count = switch_insns[1];
3596 int offset_to_targets, targ;
3597
3598 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
3599 /* 0 = sig, 1 = count, 2/3 = first key */
3600 offset_to_targets = 4;
3601 } else {
3602 /* 0 = sig, 1 = count, 2..count * 2 = keys */
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07003603 DCHECK((*insns & 0xff) == Instruction::SPARSE_SWITCH);
jeffhaobdb76512011-09-07 11:43:16 -07003604 offset_to_targets = 2 + 2 * switch_count;
3605 }
3606
3607 /* verify each switch target */
3608 for (targ = 0; targ < switch_count; targ++) {
3609 int offset;
3610 uint32_t abs_offset;
3611
3612 /* offsets are 32-bit, and only partly endian-swapped */
3613 offset = switch_insns[offset_to_targets + targ * 2] |
Andreas Gampe53de99c2015-08-17 13:43:55 -07003614 (static_cast<int32_t>(switch_insns[offset_to_targets + targ * 2 + 1]) << 16);
Ian Rogersd81871c2011-10-03 13:57:23 -07003615 abs_offset = work_insn_idx_ + offset;
3616 DCHECK_LT(abs_offset, code_item_->insns_size_in_code_units_);
Stephen Kyle9bc61992014-09-22 13:53:15 +01003617 if (!CheckNotMoveExceptionOrMoveResult(code_item_->insns_, abs_offset)) {
jeffhaobdb76512011-09-07 11:43:16 -07003618 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07003619 }
Ian Rogersebbdd872014-07-07 23:53:08 -07003620 if (!UpdateRegisters(abs_offset, work_line_.get(), false)) {
jeffhaobdb76512011-09-07 11:43:16 -07003621 return false;
Ian Rogersebbdd872014-07-07 23:53:08 -07003622 }
jeffhaobdb76512011-09-07 11:43:16 -07003623 }
3624 }
3625
3626 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003627 * Handle instructions that can throw and that are sitting in a "try" block. (If they're not in a
3628 * "try" block when they throw, control transfers out of the method.)
jeffhaobdb76512011-09-07 11:43:16 -07003629 */
Mathieu Chartierde40d472015-10-15 17:47:48 -07003630 if ((opcode_flags & Instruction::kThrow) != 0 && GetInstructionFlags(work_insn_idx_).IsInTry()) {
Andreas Gampef91baf12014-07-18 15:41:00 -07003631 bool has_catch_all_handler = false;
Ian Rogers0571d352011-11-03 19:51:38 -07003632 CatchHandlerIterator iterator(*code_item_, work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07003633
Andreas Gampef91baf12014-07-18 15:41:00 -07003634 // Need the linker to try and resolve the handled class to check if it's Throwable.
3635 ClassLinker* linker = Runtime::Current()->GetClassLinker();
3636
Ian Rogers0571d352011-11-03 19:51:38 -07003637 for (; iterator.HasNext(); iterator.Next()) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08003638 dex::TypeIndex handler_type_idx = iterator.GetHandlerTypeIndex();
3639 if (!handler_type_idx.IsValid()) {
Andreas Gampef91baf12014-07-18 15:41:00 -07003640 has_catch_all_handler = true;
3641 } else {
3642 // It is also a catch-all if it is java.lang.Throwable.
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003643 mirror::Class* klass = linker->ResolveType(*dex_file_, handler_type_idx, dex_cache_,
3644 class_loader_);
Andreas Gampef91baf12014-07-18 15:41:00 -07003645 if (klass != nullptr) {
3646 if (klass == mirror::Throwable::GetJavaLangThrowable()) {
3647 has_catch_all_handler = true;
3648 }
3649 } else {
3650 // Clear exception.
Ian Rogers7b078e82014-09-10 14:44:24 -07003651 DCHECK(self_->IsExceptionPending());
3652 self_->ClearException();
Andreas Gampef91baf12014-07-18 15:41:00 -07003653 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003654 }
jeffhaobdb76512011-09-07 11:43:16 -07003655 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003656 * Merge registers into the "catch" block. We want to use the "savedRegs" rather than
3657 * "work_regs", because at runtime the exception will be thrown before the instruction
3658 * modifies any registers.
jeffhaobdb76512011-09-07 11:43:16 -07003659 */
Ian Rogersebbdd872014-07-07 23:53:08 -07003660 if (!UpdateRegisters(iterator.GetHandlerAddress(), saved_line_.get(), false)) {
jeffhaobdb76512011-09-07 11:43:16 -07003661 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07003662 }
jeffhaobdb76512011-09-07 11:43:16 -07003663 }
3664
3665 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003666 * If the monitor stack depth is nonzero, there must be a "catch all" handler for this
3667 * instruction. This does apply to monitor-exit because of async exception handling.
jeffhaobdb76512011-09-07 11:43:16 -07003668 */
Andreas Gampef91baf12014-07-18 15:41:00 -07003669 if (work_line_->MonitorStackDepth() > 0 && !has_catch_all_handler) {
jeffhaobdb76512011-09-07 11:43:16 -07003670 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003671 * The state in work_line reflects the post-execution state. If the current instruction is a
3672 * monitor-enter and the monitor stack was empty, we don't need a catch-all (if it throws,
jeffhaobdb76512011-09-07 11:43:16 -07003673 * it will do so before grabbing the lock).
3674 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02003675 if (inst->Opcode() != Instruction::MONITOR_ENTER || work_line_->MonitorStackDepth() != 1) {
jeffhaod5347e02012-03-22 17:25:05 -07003676 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
Ian Rogersd81871c2011-10-03 13:57:23 -07003677 << "expected to be within a catch-all for an instruction where a monitor is held";
jeffhaobdb76512011-09-07 11:43:16 -07003678 return false;
3679 }
3680 }
3681 }
3682
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003683 /* Handle "continue". Tag the next consecutive instruction.
3684 * Note: Keep the code handling "continue" case below the "branch" and "switch" cases,
3685 * because it changes work_line_ when performing peephole optimization
3686 * and this change should not be used in those cases.
3687 */
Ian Rogers6d376ae2013-07-23 15:12:40 -07003688 if ((opcode_flags & Instruction::kContinue) != 0) {
Mathieu Chartier1d2d4ff2017-09-23 16:11:06 -07003689 DCHECK_EQ(&code_item_->InstructionAt(work_insn_idx_), inst);
Ian Rogers7b078e82014-09-10 14:44:24 -07003690 uint32_t next_insn_idx = work_insn_idx_ + inst->SizeInCodeUnits();
Ian Rogers6d376ae2013-07-23 15:12:40 -07003691 if (next_insn_idx >= code_item_->insns_size_in_code_units_) {
3692 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Execution can walk off end of code area";
3693 return false;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003694 }
Ian Rogers6d376ae2013-07-23 15:12:40 -07003695 // The only way to get to a move-exception instruction is to get thrown there. Make sure the
3696 // next instruction isn't one.
3697 if (!CheckNotMoveException(code_item_->insns_, next_insn_idx)) {
3698 return false;
3699 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07003700 if (nullptr != fallthrough_line) {
Ian Rogers6d376ae2013-07-23 15:12:40 -07003701 // Make workline consistent with fallthrough computed from peephole optimization.
3702 work_line_->CopyFromLine(fallthrough_line.get());
3703 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07003704 if (GetInstructionFlags(next_insn_idx).IsReturn()) {
Ian Rogersb8c78592013-07-25 23:52:52 +00003705 // For returns we only care about the operand to the return, all other registers are dead.
3706 const Instruction* ret_inst = Instruction::At(code_item_->insns_ + next_insn_idx);
Andreas Gampea727e372015-08-25 09:22:37 -07003707 AdjustReturnLine(this, ret_inst, work_line_.get());
Ian Rogersb8c78592013-07-25 23:52:52 +00003708 }
Ian Rogers6d376ae2013-07-23 15:12:40 -07003709 RegisterLine* next_line = reg_table_.GetLine(next_insn_idx);
Ian Rogers7b078e82014-09-10 14:44:24 -07003710 if (next_line != nullptr) {
Ian Rogersebbdd872014-07-07 23:53:08 -07003711 // Merge registers into what we have for the next instruction, and set the "changed" flag if
3712 // needed. If the merge changes the state of the registers then the work line will be
3713 // updated.
3714 if (!UpdateRegisters(next_insn_idx, work_line_.get(), true)) {
Ian Rogers6d376ae2013-07-23 15:12:40 -07003715 return false;
3716 }
3717 } else {
3718 /*
3719 * We're not recording register data for the next instruction, so we don't know what the
3720 * prior state was. We have to assume that something has changed and re-evaluate it.
3721 */
Mathieu Chartierde40d472015-10-15 17:47:48 -07003722 GetInstructionFlags(next_insn_idx).SetChanged();
Ian Rogers6d376ae2013-07-23 15:12:40 -07003723 }
3724 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003725
jeffhaod1f0fde2011-09-08 17:25:33 -07003726 /* If we're returning from the method, make sure monitor stack is empty. */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003727 if ((opcode_flags & Instruction::kReturn) != 0) {
Andreas Gampea727e372015-08-25 09:22:37 -07003728 work_line_->VerifyMonitorStackEmpty(this);
jeffhaobdb76512011-09-07 11:43:16 -07003729 }
3730
3731 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003732 * Update start_guess. Advance to the next instruction of that's
3733 * possible, otherwise use the branch target if one was found. If
jeffhaobdb76512011-09-07 11:43:16 -07003734 * neither of those exists we're in a return or throw; leave start_guess
3735 * alone and let the caller sort it out.
3736 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003737 if ((opcode_flags & Instruction::kContinue) != 0) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003738 DCHECK_EQ(Instruction::At(code_item_->insns_ + work_insn_idx_), inst);
3739 *start_guess = work_insn_idx_ + inst->SizeInCodeUnits();
Elliott Hughesadb8c672012-03-06 16:49:32 -08003740 } else if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07003741 /* we're still okay if branch_target is zero */
Ian Rogersd81871c2011-10-03 13:57:23 -07003742 *start_guess = work_insn_idx_ + branch_target;
jeffhaobdb76512011-09-07 11:43:16 -07003743 }
3744
Ian Rogersd81871c2011-10-03 13:57:23 -07003745 DCHECK_LT(*start_guess, code_item_->insns_size_in_code_units_);
Mathieu Chartierde40d472015-10-15 17:47:48 -07003746 DCHECK(GetInstructionFlags(*start_guess).IsOpcode());
jeffhaobdb76512011-09-07 11:43:16 -07003747
Andreas Gampea727e372015-08-25 09:22:37 -07003748 if (have_pending_runtime_throw_failure_) {
3749 have_any_pending_runtime_throw_failure_ = true;
3750 // Reset the pending_runtime_throw flag now.
3751 have_pending_runtime_throw_failure_ = false;
3752 }
3753
jeffhaobdb76512011-09-07 11:43:16 -07003754 return true;
Brian Carlstrom7934ac22013-07-26 10:54:15 -07003755} // NOLINT(readability/fn_size)
jeffhaobdb76512011-09-07 11:43:16 -07003756
Mathieu Chartierde40d472015-10-15 17:47:48 -07003757void MethodVerifier::UninstantiableError(const char* descriptor) {
3758 Fail(VerifyError::VERIFY_ERROR_NO_CLASS) << "Could not create precise reference for "
3759 << "non-instantiable klass " << descriptor;
3760}
3761
3762inline bool MethodVerifier::IsInstantiableOrPrimitive(mirror::Class* klass) {
3763 return klass->IsInstantiable() || klass->IsPrimitive();
3764}
3765
Andreas Gampe98be1a92017-08-28 08:25:45 -07003766template <MethodVerifier::CheckAccess C>
3767const RegType& MethodVerifier::ResolveClass(dex::TypeIndex class_idx) {
Vladimir Marko8d6768d2017-03-14 10:13:21 +00003768 mirror::Class* klass = can_load_classes_
3769 ? Runtime::Current()->GetClassLinker()->ResolveType(
3770 *dex_file_, class_idx, dex_cache_, class_loader_)
3771 : ClassLinker::LookupResolvedType(class_idx, dex_cache_.Get(), class_loader_.Get()).Ptr();
3772 if (can_load_classes_ && klass == nullptr) {
3773 DCHECK(self_->IsExceptionPending());
3774 self_->ClearException();
3775 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07003776 const RegType* result = nullptr;
Vladimir Marko9cb0c462017-04-21 13:31:41 +01003777 if (klass != nullptr) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07003778 bool precise = klass->CannotBeAssignedFromOtherTypes();
3779 if (precise && !IsInstantiableOrPrimitive(klass)) {
3780 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
3781 UninstantiableError(descriptor);
3782 precise = false;
3783 }
3784 result = reg_types_.FindClass(klass, precise);
3785 if (result == nullptr) {
3786 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
3787 result = reg_types_.InsertClass(descriptor, klass, precise);
3788 }
3789 } else {
3790 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
3791 result = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003792 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07003793 DCHECK(result != nullptr);
3794 if (result->IsConflict()) {
3795 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
3796 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "accessing broken descriptor '" << descriptor
3797 << "' in " << GetDeclaringClass();
3798 return *result;
3799 }
David Brazdilca3c8c32016-09-06 14:04:48 +01003800
3801 // Record result of class resolution attempt.
3802 VerifierDeps::MaybeRecordClassResolution(*dex_file_, class_idx, klass);
3803
Andreas Gampe629be512017-08-25 17:09:32 -07003804 // If requested, check if access is allowed. Unresolved types are included in this check, as the
3805 // interpreter only tests whether access is allowed when a class is not pre-verified and runs in
3806 // the access-checks interpreter. If result is primitive, skip the access check.
3807 //
3808 // Note: we do this for unresolved classes to trigger re-verification at runtime.
3809 if (C == CheckAccess::kYes && result->IsNonZeroReferenceTypes()) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07003810 const RegType& referrer = GetDeclaringClass();
Andreas Gampe629be512017-08-25 17:09:32 -07003811 if (!referrer.CanAccess(*result)) {
3812 Fail(VERIFY_ERROR_ACCESS_CLASS) << "(possibly) illegal class access: '"
Nicolas Geoffray4e868fa2017-04-21 17:16:44 +01003813 << referrer << "' -> '" << *result << "'";
Mathieu Chartierde40d472015-10-15 17:47:48 -07003814 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07003815 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07003816 return *result;
Ian Rogersd81871c2011-10-03 13:57:23 -07003817}
3818
Andreas Gampedd309072017-08-31 07:47:37 -07003819// Instantiate ResolveClass variants. This is required as the -inl file has a function with a call
3820// to ResolveClass, and compilers may decide to inline, requiring a symbol.
Andreas Gampeae0b1f42017-08-30 17:36:26 -07003821template const RegType& MethodVerifier::ResolveClass<MethodVerifier::CheckAccess::kNo>(
3822 dex::TypeIndex class_idx);
3823template const RegType& MethodVerifier::ResolveClass<MethodVerifier::CheckAccess::kYes>(
3824 dex::TypeIndex class_idx);
3825
Ian Rogersd8f69b02014-09-10 21:43:52 +00003826const RegType& MethodVerifier::GetCaughtExceptionType() {
Ian Rogers7b078e82014-09-10 14:44:24 -07003827 const RegType* common_super = nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003828 if (code_item_->tries_size_ != 0) {
Ian Rogers13735952014-10-08 12:43:28 -07003829 const uint8_t* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
Ian Rogersd81871c2011-10-03 13:57:23 -07003830 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
3831 for (uint32_t i = 0; i < handlers_size; i++) {
Ian Rogers0571d352011-11-03 19:51:38 -07003832 CatchHandlerIterator iterator(handlers_ptr);
3833 for (; iterator.HasNext(); iterator.Next()) {
3834 if (iterator.GetHandlerAddress() == (uint32_t) work_insn_idx_) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08003835 if (!iterator.GetHandlerTypeIndex().IsValid()) {
Ian Rogersb4903572012-10-11 11:52:56 -07003836 common_super = &reg_types_.JavaLangThrowable(false);
Ian Rogersd81871c2011-10-03 13:57:23 -07003837 } else {
Andreas Gampe98be1a92017-08-28 08:25:45 -07003838 const RegType& exception =
3839 ResolveClass<CheckAccess::kYes>(iterator.GetHandlerTypeIndex());
David Brazdilca3c8c32016-09-06 14:04:48 +01003840 if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(exception, this)) {
David Brazdil68b5c0b2016-01-19 14:25:29 +00003841 DCHECK(!exception.IsUninitializedTypes()); // Comes from dex, shouldn't be uninit.
Jeff Haoc26a56c2013-11-04 12:00:47 -08003842 if (exception.IsUnresolvedTypes()) {
3843 // We don't know enough about the type. Fail here and let runtime handle it.
3844 Fail(VERIFY_ERROR_NO_CLASS) << "unresolved exception class " << exception;
3845 return exception;
3846 } else {
3847 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unexpected non-exception class " << exception;
3848 return reg_types_.Conflict();
3849 }
Jeff Haob878f212014-04-24 16:25:36 -07003850 } else if (common_super == nullptr) {
3851 common_super = &exception;
Ian Rogers28ad40d2011-10-27 15:19:26 -07003852 } else if (common_super->Equals(exception)) {
Ian Rogersc4762272012-02-01 15:55:55 -08003853 // odd case, but nothing to do
Ian Rogersd81871c2011-10-03 13:57:23 -07003854 } else {
David Brazdilca3c8c32016-09-06 14:04:48 +01003855 common_super = &common_super->Merge(exception, &reg_types_, this);
Andreas Gampe7c038102014-10-27 20:08:46 -07003856 if (FailOrAbort(this,
David Brazdilca3c8c32016-09-06 14:04:48 +01003857 reg_types_.JavaLangThrowable(false).IsAssignableFrom(
3858 *common_super, this),
Andreas Gampe7c038102014-10-27 20:08:46 -07003859 "java.lang.Throwable is not assignable-from common_super at ",
3860 work_insn_idx_)) {
3861 break;
3862 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003863 }
3864 }
3865 }
3866 }
Ian Rogers0571d352011-11-03 19:51:38 -07003867 handlers_ptr = iterator.EndDataPointer();
Ian Rogersd81871c2011-10-03 13:57:23 -07003868 }
3869 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003870 if (common_super == nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003871 /* no catch blocks, or no catches with classes we can find */
jeffhaod5347e02012-03-22 17:25:05 -07003872 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unable to find exception handler";
Ian Rogersad0b3a32012-04-16 14:50:24 -07003873 return reg_types_.Conflict();
Ian Rogersd81871c2011-10-03 13:57:23 -07003874 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07003875 return *common_super;
Ian Rogersd81871c2011-10-03 13:57:23 -07003876}
3877
Mathieu Chartiere401d142015-04-22 13:56:20 -07003878ArtMethod* MethodVerifier::ResolveMethodAndCheckAccess(
Alex Light7268d472016-01-20 15:50:01 -08003879 uint32_t dex_method_idx, MethodType method_type) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003880 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx);
Andreas Gampe98be1a92017-08-28 08:25:45 -07003881 const RegType& klass_type = ResolveClass<CheckAccess::kYes>(method_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003882 if (klass_type.IsConflict()) {
3883 std::string append(" in attempt to access method ");
3884 append += dex_file_->GetMethodName(method_id);
3885 AppendToLastFailMessage(append);
Ian Rogers7b078e82014-09-10 14:44:24 -07003886 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08003887 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003888 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003889 return nullptr; // Can't resolve Class so no more to do here
Ian Rogers90040192011-12-16 08:54:29 -08003890 }
Vladimir Markoba118822017-06-12 15:41:56 +01003891 ObjPtr<mirror::Class> klass = klass_type.GetClass();
Ian Rogersd8f69b02014-09-10 21:43:52 +00003892 const RegType& referrer = GetDeclaringClass();
Mathieu Chartiere401d142015-04-22 13:56:20 -07003893 auto* cl = Runtime::Current()->GetClassLinker();
3894 auto pointer_size = cl->GetImagePointerSize();
Andreas Gampe42ef8ab2015-12-03 17:27:32 -08003895
Mathieu Chartiere401d142015-04-22 13:56:20 -07003896 ArtMethod* res_method = dex_cache_->GetResolvedMethod(dex_method_idx, pointer_size);
Ian Rogers7b078e82014-09-10 14:44:24 -07003897 if (res_method == nullptr) {
Vladimir Markoba118822017-06-12 15:41:56 +01003898 // Try to find the method with the appropriate lookup for the klass type (interface or not).
3899 // If this lookup does not match `method_type`, errors shall be reported below.
3900 if (klass->IsInterface()) {
3901 res_method = klass->FindInterfaceMethod(dex_cache_.Get(), dex_method_idx, pointer_size);
David Brazdilca3c8c32016-09-06 14:04:48 +01003902 } else {
Vladimir Markoba118822017-06-12 15:41:56 +01003903 res_method = klass->FindClassMethod(dex_cache_.Get(), dex_method_idx, pointer_size);
Ian Rogersd81871c2011-10-03 13:57:23 -07003904 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003905 if (res_method != nullptr) {
Vladimir Markoba118822017-06-12 15:41:56 +01003906 dex_cache_->SetResolvedMethod(dex_method_idx, res_method, pointer_size);
Ian Rogersd81871c2011-10-03 13:57:23 -07003907 }
3908 }
David Brazdilca3c8c32016-09-06 14:04:48 +01003909
Vladimir Markoba118822017-06-12 15:41:56 +01003910 // Record result of method resolution attempt. The klass resolution has recorded whether
3911 // the class is an interface or not and therefore the type of the lookup performed above.
3912 // TODO: Maybe we should not record dependency if the invoke type does not match the lookup type.
3913 VerifierDeps::MaybeRecordMethodResolution(*dex_file_, dex_method_idx, res_method);
3914
Alex Lightafb66472017-08-01 09:54:49 -07003915 bool must_fail = false;
3916 // This is traditional and helps with screwy bytecode. It will tell you that, yes, a method
3917 // exists, but that it's called incorrectly. This significantly helps debugging, as locally it's
3918 // hard to see the differences.
3919 // If we don't have res_method here we must fail. Just use this bool to make sure of that with a
3920 // DCHECK.
Vladimir Markoba118822017-06-12 15:41:56 +01003921 if (res_method == nullptr) {
Alex Lightafb66472017-08-01 09:54:49 -07003922 must_fail = true;
Vladimir Markoba118822017-06-12 15:41:56 +01003923 // Try to find the method also with the other type for better error reporting below
3924 // but do not store such bogus lookup result in the DexCache or VerifierDeps.
3925 if (klass->IsInterface()) {
Alex Lightafb66472017-08-01 09:54:49 -07003926 // NB This is normally not really allowed but we want to get any static or private object
3927 // methods for error message purposes. This will never be returned.
3928 // TODO We might want to change the verifier to not require this.
Vladimir Markoba118822017-06-12 15:41:56 +01003929 res_method = klass->FindClassMethod(dex_cache_.Get(), dex_method_idx, pointer_size);
3930 } else {
3931 // If there was an interface method with the same signature,
3932 // we would have found it also in the "copied" methods.
3933 DCHECK(klass->FindInterfaceMethod(dex_cache_.Get(), dex_method_idx, pointer_size) == nullptr);
3934 }
3935 }
David Brazdilca3c8c32016-09-06 14:04:48 +01003936
3937 if (res_method == nullptr) {
3938 Fail(VERIFY_ERROR_NO_METHOD) << "couldn't find method "
David Sehr709b0702016-10-13 09:12:37 -07003939 << klass->PrettyDescriptor() << "."
David Brazdilca3c8c32016-09-06 14:04:48 +01003940 << dex_file_->GetMethodName(method_id) << " "
3941 << dex_file_->GetMethodSignature(method_id);
3942 return nullptr;
3943 }
3944
Ian Rogersd81871c2011-10-03 13:57:23 -07003945 // Make sure calls to constructors are "direct". There are additional restrictions but we don't
3946 // enforce them here.
3947 if (res_method->IsConstructor() && method_type != METHOD_DIRECT) {
jeffhaod5347e02012-03-22 17:25:05 -07003948 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting non-direct call to constructor "
David Sehr709b0702016-10-13 09:12:37 -07003949 << res_method->PrettyMethod();
Ian Rogers7b078e82014-09-10 14:44:24 -07003950 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003951 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003952 // Disallow any calls to class initializers.
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003953 if (res_method->IsClassInitializer()) {
jeffhaod5347e02012-03-22 17:25:05 -07003954 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting call to class initializer "
David Sehr709b0702016-10-13 09:12:37 -07003955 << res_method->PrettyMethod();
Ian Rogers7b078e82014-09-10 14:44:24 -07003956 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003957 }
Andreas Gampe42ef8ab2015-12-03 17:27:32 -08003958
3959 // Check that interface methods are static or match interface classes.
3960 // We only allow statics if we don't have default methods enabled.
3961 //
3962 // Note: this check must be after the initializer check, as those are required to fail a class,
3963 // while this check implies an IncompatibleClassChangeError.
3964 if (klass->IsInterface()) {
Alex Lightb55f1ac2016-04-12 15:50:55 -07003965 // methods called on interfaces should be invoke-interface, invoke-super, invoke-direct (if
3966 // dex file version is 37 or greater), or invoke-static.
Andreas Gampe42ef8ab2015-12-03 17:27:32 -08003967 if (method_type != METHOD_INTERFACE &&
Neil Fuller9724c632016-01-07 15:42:47 +00003968 method_type != METHOD_STATIC &&
Alex Lightb55f1ac2016-04-12 15:50:55 -07003969 ((dex_file_->GetVersion() < DexFile::kDefaultMethodsVersion) ||
3970 method_type != METHOD_DIRECT) &&
Neil Fuller9724c632016-01-07 15:42:47 +00003971 method_type != METHOD_SUPER) {
Andreas Gampe42ef8ab2015-12-03 17:27:32 -08003972 Fail(VERIFY_ERROR_CLASS_CHANGE)
David Sehr709b0702016-10-13 09:12:37 -07003973 << "non-interface method " << dex_file_->PrettyMethod(dex_method_idx)
3974 << " is in an interface class " << klass->PrettyClass();
Andreas Gampe42ef8ab2015-12-03 17:27:32 -08003975 return nullptr;
3976 }
3977 } else {
3978 if (method_type == METHOD_INTERFACE) {
3979 Fail(VERIFY_ERROR_CLASS_CHANGE)
David Sehr709b0702016-10-13 09:12:37 -07003980 << "interface method " << dex_file_->PrettyMethod(dex_method_idx)
3981 << " is in a non-interface class " << klass->PrettyClass();
Andreas Gampe42ef8ab2015-12-03 17:27:32 -08003982 return nullptr;
3983 }
3984 }
3985
Alex Lightafb66472017-08-01 09:54:49 -07003986 // Check specifically for non-public object methods being provided for interface dispatch. This
3987 // can occur if we failed to find a method with FindInterfaceMethod but later find one with
3988 // FindClassMethod for error message use.
3989 if (method_type == METHOD_INTERFACE &&
3990 res_method->GetDeclaringClass()->IsObjectClass() &&
3991 !res_method->IsPublic()) {
3992 Fail(VERIFY_ERROR_NO_METHOD) << "invoke-interface " << klass->PrettyDescriptor() << "."
3993 << dex_file_->GetMethodName(method_id) << " "
3994 << dex_file_->GetMethodSignature(method_id) << " resolved to "
3995 << "non-public object method " << res_method->PrettyMethod() << " "
3996 << "but non-public Object methods are excluded from interface "
3997 << "method resolution.";
3998 return nullptr;
3999 }
jeffhao8cd6dda2012-02-22 10:15:34 -08004000 // Check if access is allowed.
Ian Rogersad0b3a32012-04-16 14:50:24 -07004001 if (!referrer.CanAccessMember(res_method->GetDeclaringClass(), res_method->GetAccessFlags())) {
David Sehr709b0702016-10-13 09:12:37 -07004002 Fail(VERIFY_ERROR_ACCESS_METHOD) << "illegal method access (call "
4003 << res_method->PrettyMethod()
Ian Rogersad0b3a32012-04-16 14:50:24 -07004004 << " from " << referrer << ")";
jeffhaob57e9522012-04-26 18:08:21 -07004005 return res_method;
jeffhao8cd6dda2012-02-22 10:15:34 -08004006 }
jeffhaode0d9c92012-02-27 13:58:13 -08004007 // Check that invoke-virtual and invoke-super are not used on private methods of the same class.
Alex Light7268d472016-01-20 15:50:01 -08004008 if (res_method->IsPrivate() && (method_type == METHOD_VIRTUAL || method_type == METHOD_SUPER)) {
jeffhaod5347e02012-03-22 17:25:05 -07004009 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invoke-super/virtual can't be used on private method "
David Sehr709b0702016-10-13 09:12:37 -07004010 << res_method->PrettyMethod();
Ian Rogers7b078e82014-09-10 14:44:24 -07004011 return nullptr;
jeffhaode0d9c92012-02-27 13:58:13 -08004012 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004013 // See if the method type implied by the invoke instruction matches the access flags for the
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004014 // target method. The flags for METHOD_POLYMORPHIC are based on there being precisely two
4015 // signature polymorphic methods supported by the run-time which are native methods with variable
4016 // arguments.
Brian Carlstrombe6fa5e2014-12-09 20:15:42 -08004017 if ((method_type == METHOD_DIRECT && (!res_method->IsDirect() || res_method->IsStatic())) ||
Ian Rogersd81871c2011-10-03 13:57:23 -07004018 (method_type == METHOD_STATIC && !res_method->IsStatic()) ||
Alex Light7268d472016-01-20 15:50:01 -08004019 ((method_type == METHOD_SUPER ||
4020 method_type == METHOD_VIRTUAL ||
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004021 method_type == METHOD_INTERFACE) && res_method->IsDirect()) ||
4022 ((method_type == METHOD_POLYMORPHIC) &&
4023 (!res_method->IsNative() || !res_method->IsVarargs()))) {
Ian Rogers2fc14272012-08-30 10:56:57 -07004024 Fail(VERIFY_ERROR_CLASS_CHANGE) << "invoke type (" << method_type << ") does not match method "
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004025 "type of " << res_method->PrettyMethod();
Ian Rogers7b078e82014-09-10 14:44:24 -07004026 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07004027 }
Alex Lightafb66472017-08-01 09:54:49 -07004028 // Make sure we weren't expecting to fail.
4029 DCHECK(!must_fail) << "invoke type (" << method_type << ")"
4030 << klass->PrettyDescriptor() << "."
4031 << dex_file_->GetMethodName(method_id) << " "
4032 << dex_file_->GetMethodSignature(method_id) << " unexpectedly resolved to "
4033 << res_method->PrettyMethod() << " without error. Initially this method was "
4034 << "not found so we were expecting to fail for some reason.";
jeffhao8cd6dda2012-02-22 10:15:34 -08004035 return res_method;
4036}
4037
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004038template <class T>
Mathieu Chartiere401d142015-04-22 13:56:20 -07004039ArtMethod* MethodVerifier::VerifyInvocationArgsFromIterator(
4040 T* it, const Instruction* inst, MethodType method_type, bool is_range, ArtMethod* res_method) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004041 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
4042 // match the call to the signature. Also, we might be calling through an abstract method
4043 // definition (which doesn't have register count values).
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004044 const size_t expected_args = inst->VRegA();
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004045 /* caught by static verifier */
4046 DCHECK(is_range || expected_args <= 5);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004047
Orion Hodson1cda7c22017-08-10 13:06:45 +01004048 if (expected_args > code_item_->outs_size_) {
4049 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
4050 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
4051 return nullptr;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004052 }
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004053
4054 /*
4055 * Check the "this" argument, which must be an instance of the class that declared the method.
4056 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
4057 * rigorous check here (which is okay since we have to do it at runtime).
4058 */
4059 if (method_type != METHOD_STATIC) {
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004060 const RegType& actual_arg_type = work_line_->GetInvocationThis(this, inst);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004061 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
4062 CHECK(have_pending_hard_failure_);
4063 return nullptr;
4064 }
David Brazdil68b5c0b2016-01-19 14:25:29 +00004065 bool is_init = false;
4066 if (actual_arg_type.IsUninitializedTypes()) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004067 if (res_method) {
4068 if (!res_method->IsConstructor()) {
4069 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
4070 return nullptr;
4071 }
4072 } else {
4073 // Check whether the name of the called method is "<init>"
4074 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Jeff Hao0d087272014-08-04 14:47:17 -07004075 if (strcmp(dex_file_->GetMethodName(dex_file_->GetMethodId(method_idx)), "<init>") != 0) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004076 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
4077 return nullptr;
4078 }
4079 }
David Brazdil68b5c0b2016-01-19 14:25:29 +00004080 is_init = true;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004081 }
David Brazdil68b5c0b2016-01-19 14:25:29 +00004082 const RegType& adjusted_type = is_init
4083 ? GetRegTypeCache()->FromUninitialized(actual_arg_type)
4084 : actual_arg_type;
4085 if (method_type != METHOD_INTERFACE && !adjusted_type.IsZero()) {
Ian Rogersd8f69b02014-09-10 21:43:52 +00004086 const RegType* res_method_class;
Andreas Gamped9e23012015-06-04 22:19:58 -07004087 // Miranda methods have the declaring interface as their declaring class, not the abstract
4088 // class. It would be wrong to use this for the type check (interface type checks are
4089 // postponed to runtime).
4090 if (res_method != nullptr && !res_method->IsMiranda()) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004091 mirror::Class* klass = res_method->GetDeclaringClass();
Ian Rogers1ff3c982014-08-12 02:30:58 -07004092 std::string temp;
Andreas Gampef23f33d2015-06-23 14:18:17 -07004093 res_method_class = &FromClass(klass->GetDescriptor(&temp), klass,
4094 klass->CannotBeAssignedFromOtherTypes());
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004095 } else {
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004096 const uint32_t method_idx = inst->VRegB();
Andreas Gampea5b09a62016-11-17 15:21:22 -08004097 const dex::TypeIndex class_idx = dex_file_->GetMethodId(method_idx).class_idx_;
Mathieu Chartierde40d472015-10-15 17:47:48 -07004098 res_method_class = &reg_types_.FromDescriptor(
4099 GetClassLoader(),
4100 dex_file_->StringByTypeIdx(class_idx),
4101 false);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004102 }
David Brazdilca3c8c32016-09-06 14:04:48 +01004103 if (!res_method_class->IsAssignableFrom(adjusted_type, this)) {
David Brazdil68b5c0b2016-01-19 14:25:29 +00004104 Fail(adjusted_type.IsUnresolvedTypes()
4105 ? VERIFY_ERROR_NO_CLASS
4106 : VERIFY_ERROR_BAD_CLASS_SOFT)
4107 << "'this' argument '" << actual_arg_type << "' not instance of '"
4108 << *res_method_class << "'";
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004109 // Continue on soft failures. We need to find possible hard failures to avoid problems in
4110 // the compiler.
4111 if (have_pending_hard_failure_) {
4112 return nullptr;
4113 }
4114 }
4115 }
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004116 }
4117
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004118 uint32_t arg[5];
4119 if (!is_range) {
4120 inst->GetVarArgs(arg);
4121 }
4122 uint32_t sig_registers = (method_type == METHOD_STATIC) ? 0 : 1;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004123 for ( ; it->HasNext(); it->Next()) {
4124 if (sig_registers >= expected_args) {
4125 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, expected " << inst->VRegA() <<
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004126 " argument registers, method signature has " << sig_registers + 1 << " or more";
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004127 return nullptr;
4128 }
4129
4130 const char* param_descriptor = it->GetDescriptor();
4131
4132 if (param_descriptor == nullptr) {
4133 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation because of missing signature "
4134 "component";
4135 return nullptr;
4136 }
4137
Ian Rogersd8f69b02014-09-10 21:43:52 +00004138 const RegType& reg_type = reg_types_.FromDescriptor(GetClassLoader(), param_descriptor, false);
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004139 uint32_t get_reg = is_range ? inst->VRegC() + static_cast<uint32_t>(sig_registers) :
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004140 arg[sig_registers];
4141 if (reg_type.IsIntegralTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004142 const RegType& src_type = work_line_->GetRegisterType(this, get_reg);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004143 if (!src_type.IsIntegralTypes()) {
4144 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register v" << get_reg << " has type " << src_type
4145 << " but expected " << reg_type;
Andreas Gampeb588f4c2015-05-26 13:35:39 -07004146 return nullptr;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004147 }
Andreas Gampeda9badb2015-06-05 20:22:12 -07004148 } else {
4149 if (!work_line_->VerifyRegisterType(this, get_reg, reg_type)) {
4150 // Continue on soft failures. We need to find possible hard failures to avoid problems in
4151 // the compiler.
4152 if (have_pending_hard_failure_) {
4153 return nullptr;
4154 }
4155 } else if (reg_type.IsLongOrDoubleTypes()) {
4156 // Check that registers are consecutive (for non-range invokes). Invokes are the only
4157 // instructions not specifying register pairs by the first component, but require them
4158 // nonetheless. Only check when there's an actual register in the parameters. If there's
4159 // none, this will fail below.
4160 if (!is_range && sig_registers + 1 < expected_args) {
4161 uint32_t second_reg = arg[sig_registers + 1];
4162 if (second_reg != get_reg + 1) {
4163 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, long or double parameter "
4164 "at index " << sig_registers << " is not a pair: " << get_reg << " + "
4165 << second_reg << ".";
4166 return nullptr;
4167 }
4168 }
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004169 }
4170 }
4171 sig_registers += reg_type.IsLongOrDoubleTypes() ? 2 : 1;
4172 }
4173 if (expected_args != sig_registers) {
4174 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, expected " << expected_args <<
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004175 " argument registers, method signature has " << sig_registers;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004176 return nullptr;
4177 }
4178 return res_method;
4179}
4180
4181void MethodVerifier::VerifyInvocationArgsUnresolvedMethod(const Instruction* inst,
4182 MethodType method_type,
4183 bool is_range) {
4184 // As the method may not have been resolved, make this static check against what we expect.
4185 // The main reason for this code block is to fail hard when we find an illegal use, e.g.,
4186 // wrong number of arguments or wrong primitive types, even if the method could not be resolved.
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004187 const uint32_t method_idx = inst->VRegB();
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004188 DexFileParameterIterator it(*dex_file_,
4189 dex_file_->GetProtoId(dex_file_->GetMethodId(method_idx).proto_idx_));
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004190 VerifyInvocationArgsFromIterator(&it, inst, method_type, is_range, nullptr);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004191}
4192
Orion Hodsonc069a302017-01-18 09:23:12 +00004193bool MethodVerifier::CheckCallSite(uint32_t call_site_idx) {
Orion Hodson3a842f52017-04-21 15:24:10 +01004194 if (call_site_idx >= dex_file_->NumCallSiteIds()) {
4195 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Bad call site id #" << call_site_idx
4196 << " >= " << dex_file_->NumCallSiteIds();
4197 return false;
4198 }
4199
Orion Hodsonc069a302017-01-18 09:23:12 +00004200 CallSiteArrayValueIterator it(*dex_file_, dex_file_->GetCallSiteId(call_site_idx));
4201 // Check essential arguments are provided. The dex file verifier has verified indicies of the
4202 // main values (method handle, name, method_type).
4203 if (it.Size() < 3) {
4204 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Call site #" << call_site_idx
4205 << " has too few arguments: "
4206 << it.Size() << "< 3";
4207 return false;
4208 }
4209
Orion Hodson3a842f52017-04-21 15:24:10 +01004210 // Get and check the first argument: the method handle (index range
4211 // checked by the dex file verifier).
Orion Hodsonc069a302017-01-18 09:23:12 +00004212 uint32_t method_handle_idx = static_cast<uint32_t>(it.GetJavaValue().i);
4213 it.Next();
Orion Hodson3a842f52017-04-21 15:24:10 +01004214
Orion Hodsonc069a302017-01-18 09:23:12 +00004215 const DexFile::MethodHandleItem& mh = dex_file_->GetMethodHandle(method_handle_idx);
4216 if (mh.method_handle_type_ != static_cast<uint16_t>(DexFile::MethodHandleType::kInvokeStatic)) {
4217 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Call site #" << call_site_idx
Orion Hodson631827d2017-04-10 14:53:47 +01004218 << " argument 0 method handle type is not InvokeStatic: "
4219 << mh.method_handle_type_;
Orion Hodsonc069a302017-01-18 09:23:12 +00004220 return false;
4221 }
4222
4223 // Skip the second argument, the name to resolve, as checked by the
4224 // dex file verifier.
4225 it.Next();
4226
4227 // Skip the third argument, the method type expected, as checked by
4228 // the dex file verifier.
4229 it.Next();
4230
4231 // Check the bootstrap method handle and remaining arguments.
4232 const DexFile::MethodId& method_id = dex_file_->GetMethodId(mh.field_or_method_idx_);
4233 uint32_t length;
4234 const char* shorty = dex_file_->GetMethodShorty(method_id, &length);
4235
4236 if (it.Size() < length - 1) {
4237 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Call site #" << call_site_idx
4238 << " too few arguments for bootstrap method: "
4239 << it.Size() << " < " << (length - 1);
4240 return false;
4241 }
4242
4243 // Check the return type and first 3 arguments are references
4244 // (CallSite, Lookup, String, MethodType). If they are not of the
4245 // expected types (or subtypes), it will trigger a
4246 // WrongMethodTypeException during execution.
4247 if (shorty[0] != 'L') {
4248 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Call site #" << call_site_idx
4249 << " bootstrap return type is not a reference";
4250 return false;
4251 }
4252
4253 for (uint32_t i = 1; i < 4; ++i) {
4254 if (shorty[i] != 'L') {
4255 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Call site #" << call_site_idx
4256 << " bootstrap method argument " << (i - 1)
4257 << " is not a reference";
4258 return false;
4259 }
4260 }
4261
4262 // Check the optional arguments.
4263 for (uint32_t i = 4; i < length; ++i, it.Next()) {
4264 bool match = false;
4265 switch (it.GetValueType()) {
4266 case EncodedArrayValueIterator::ValueType::kBoolean:
4267 case EncodedArrayValueIterator::ValueType::kByte:
4268 case EncodedArrayValueIterator::ValueType::kShort:
4269 case EncodedArrayValueIterator::ValueType::kChar:
4270 case EncodedArrayValueIterator::ValueType::kInt:
4271 // These all fit within one register and encoders do not seem
4272 // too exacting on the encoding type they use (ie using
4273 // integer for all of these).
4274 match = (strchr("ZBCSI", shorty[i]) != nullptr);
4275 break;
4276 case EncodedArrayValueIterator::ValueType::kLong:
4277 match = ('J' == shorty[i]);
4278 break;
4279 case EncodedArrayValueIterator::ValueType::kFloat:
4280 match = ('F' == shorty[i]);
4281 break;
4282 case EncodedArrayValueIterator::ValueType::kDouble:
4283 match = ('D' == shorty[i]);
4284 break;
4285 case EncodedArrayValueIterator::ValueType::kMethodType:
4286 case EncodedArrayValueIterator::ValueType::kMethodHandle:
4287 case EncodedArrayValueIterator::ValueType::kString:
4288 case EncodedArrayValueIterator::ValueType::kType:
4289 case EncodedArrayValueIterator::ValueType::kNull:
4290 match = ('L' == shorty[i]);
4291 break;
4292 case EncodedArrayValueIterator::ValueType::kField:
4293 case EncodedArrayValueIterator::ValueType::kMethod:
4294 case EncodedArrayValueIterator::ValueType::kEnum:
4295 case EncodedArrayValueIterator::ValueType::kArray:
4296 case EncodedArrayValueIterator::ValueType::kAnnotation:
4297 // Unreachable based on current EncodedArrayValueIterator::Next().
4298 UNREACHABLE();
4299 }
4300
4301 if (!match) {
4302 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Call site #" << call_site_idx
4303 << " bootstrap method argument " << (i - 1)
4304 << " expected " << shorty[i]
4305 << " got value type: " << it.GetValueType();
4306 return false;
4307 }
4308 }
4309 return true;
4310}
4311
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004312class MethodParamListDescriptorIterator {
4313 public:
Mathieu Chartiere401d142015-04-22 13:56:20 -07004314 explicit MethodParamListDescriptorIterator(ArtMethod* res_method) :
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004315 res_method_(res_method), pos_(0), params_(res_method->GetParameterTypeList()),
4316 params_size_(params_ == nullptr ? 0 : params_->Size()) {
4317 }
4318
4319 bool HasNext() {
4320 return pos_ < params_size_;
4321 }
4322
4323 void Next() {
4324 ++pos_;
4325 }
4326
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07004327 const char* GetDescriptor() REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004328 return res_method_->GetTypeDescriptorFromTypeIdx(params_->GetTypeItem(pos_).type_idx_);
4329 }
4330
4331 private:
Mathieu Chartiere401d142015-04-22 13:56:20 -07004332 ArtMethod* res_method_;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004333 size_t pos_;
4334 const DexFile::TypeList* params_;
4335 const size_t params_size_;
4336};
4337
Mathieu Chartiere401d142015-04-22 13:56:20 -07004338ArtMethod* MethodVerifier::VerifyInvocationArgs(
Alex Light7268d472016-01-20 15:50:01 -08004339 const Instruction* inst, MethodType method_type, bool is_range) {
jeffhao8cd6dda2012-02-22 10:15:34 -08004340 // Resolve the method. This could be an abstract or concrete method depending on what sort of call
4341 // we're making.
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004342 const uint32_t method_idx = inst->VRegB();
Alex Light7268d472016-01-20 15:50:01 -08004343 ArtMethod* res_method = ResolveMethodAndCheckAccess(method_idx, method_type);
Ian Rogers7b078e82014-09-10 14:44:24 -07004344 if (res_method == nullptr) { // error or class is unresolved
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004345 // Check what we can statically.
4346 if (!have_pending_hard_failure_) {
4347 VerifyInvocationArgsUnresolvedMethod(inst, method_type, is_range);
4348 }
4349 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08004350 }
4351
Ian Rogersd81871c2011-10-03 13:57:23 -07004352 // If we're using invoke-super(method), make sure that the executing method's class' superclass
Alex Light705ad492015-09-21 11:36:30 -07004353 // has a vtable entry for the target method. Or the target is on a interface.
Alex Light7268d472016-01-20 15:50:01 -08004354 if (method_type == METHOD_SUPER) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08004355 dex::TypeIndex class_idx = dex_file_->GetMethodId(method_idx).class_idx_;
David Brazdilca3c8c32016-09-06 14:04:48 +01004356 const RegType& reference_type = reg_types_.FromDescriptor(
4357 GetClassLoader(),
4358 dex_file_->StringByTypeIdx(class_idx),
4359 false);
4360 if (reference_type.IsUnresolvedTypes()) {
Alex Lightfedd91d2016-01-07 14:49:16 -08004361 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "Unable to find referenced class from invoke-super";
4362 return nullptr;
4363 }
David Brazdilca3c8c32016-09-06 14:04:48 +01004364 if (reference_type.GetClass()->IsInterface()) {
Alex Lightfedd91d2016-01-07 14:49:16 -08004365 // TODO Can we verify anything else.
David Brazdil15fc7292016-09-02 14:13:18 +01004366 if (class_idx == class_def_.class_idx_) {
Alex Lightfedd91d2016-01-07 14:49:16 -08004367 Fail(VERIFY_ERROR_CLASS_CHANGE) << "Cannot invoke-super on self as interface";
Alex Light55ea94d2016-03-15 09:50:26 -07004368 return nullptr;
Alex Lightfedd91d2016-01-07 14:49:16 -08004369 }
4370 // TODO Revisit whether we want to allow invoke-super on direct interfaces only like the JLS
4371 // does.
Alex Light55ea94d2016-03-15 09:50:26 -07004372 if (!GetDeclaringClass().HasClass()) {
4373 Fail(VERIFY_ERROR_NO_CLASS) << "Unable to resolve the full class of 'this' used in an"
4374 << "interface invoke-super";
4375 return nullptr;
David Brazdilca3c8c32016-09-06 14:04:48 +01004376 } else if (!reference_type.IsStrictlyAssignableFrom(GetDeclaringClass(), this)) {
Alex Lightfedd91d2016-01-07 14:49:16 -08004377 Fail(VERIFY_ERROR_CLASS_CHANGE)
David Sehr709b0702016-10-13 09:12:37 -07004378 << "invoke-super in " << mirror::Class::PrettyClass(GetDeclaringClass().GetClass())
4379 << " in method "
4380 << dex_file_->PrettyMethod(dex_method_idx_) << " to method "
4381 << dex_file_->PrettyMethod(method_idx) << " references "
4382 << "non-super-interface type " << mirror::Class::PrettyClass(reference_type.GetClass());
Alex Lightfedd91d2016-01-07 14:49:16 -08004383 return nullptr;
Alex Light705ad492015-09-21 11:36:30 -07004384 }
4385 } else {
4386 const RegType& super = GetDeclaringClass().GetSuperClass(&reg_types_);
4387 if (super.IsUnresolvedTypes()) {
4388 Fail(VERIFY_ERROR_NO_METHOD) << "unknown super class in invoke-super from "
David Sehr709b0702016-10-13 09:12:37 -07004389 << dex_file_->PrettyMethod(dex_method_idx_)
4390 << " to super " << res_method->PrettyMethod();
Alex Light705ad492015-09-21 11:36:30 -07004391 return nullptr;
4392 }
David Brazdilca3c8c32016-09-06 14:04:48 +01004393 if (!reference_type.IsStrictlyAssignableFrom(GetDeclaringClass(), this) ||
Aart Bikf663e342016-04-04 17:28:59 -07004394 (res_method->GetMethodIndex() >= super.GetClass()->GetVTableLength())) {
Alex Light705ad492015-09-21 11:36:30 -07004395 Fail(VERIFY_ERROR_NO_METHOD) << "invalid invoke-super from "
David Sehr709b0702016-10-13 09:12:37 -07004396 << dex_file_->PrettyMethod(dex_method_idx_)
Alex Light705ad492015-09-21 11:36:30 -07004397 << " to super " << super
4398 << "." << res_method->GetName()
4399 << res_method->GetSignature();
4400 return nullptr;
4401 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004402 }
4403 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08004404
Andreas Gampe74979b12017-05-16 09:28:06 -07004405 if (UNLIKELY(method_type == METHOD_POLYMORPHIC)) {
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004406 // Process the signature of the calling site that is invoking the method handle.
4407 DexFileParameterIterator it(*dex_file_, dex_file_->GetProtoId(inst->VRegH()));
4408 return VerifyInvocationArgsFromIterator(&it, inst, method_type, is_range, res_method);
4409 } else {
4410 // Process the target method's signature.
4411 MethodParamListDescriptorIterator it(res_method);
4412 return VerifyInvocationArgsFromIterator(&it, inst, method_type, is_range, res_method);
4413 }
4414}
4415
4416bool MethodVerifier::CheckSignaturePolymorphicMethod(ArtMethod* method) {
4417 mirror::Class* klass = method->GetDeclaringClass();
4418 if (klass != mirror::MethodHandle::StaticClass()) {
4419 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
4420 << "Signature polymorphic method must be declared in java.lang.invoke.MethodClass";
4421 return false;
4422 }
4423
4424 const char* method_name = method->GetName();
4425 if (strcmp(method_name, "invoke") != 0 && strcmp(method_name, "invokeExact") != 0) {
4426 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
4427 << "Signature polymorphic method name invalid: " << method_name;
4428 return false;
4429 }
4430
4431 const DexFile::TypeList* types = method->GetParameterTypeList();
4432 if (types->Size() != 1) {
4433 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
4434 << "Signature polymorphic method has too many arguments " << types->Size() << " != 1";
4435 return false;
4436 }
4437
4438 const dex::TypeIndex argument_type_index = types->GetTypeItem(0).type_idx_;
4439 const char* argument_descriptor = method->GetTypeDescriptorFromTypeIdx(argument_type_index);
4440 if (strcmp(argument_descriptor, "[Ljava/lang/Object;") != 0) {
4441 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
4442 << "Signature polymorphic method has unexpected argument type: " << argument_descriptor;
4443 return false;
4444 }
4445
4446 const char* return_descriptor = method->GetReturnTypeDescriptor();
4447 if (strcmp(return_descriptor, "Ljava/lang/Object;") != 0) {
4448 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
4449 << "Signature polymorphic method has unexpected return type: " << return_descriptor;
4450 return false;
4451 }
4452
4453 return true;
4454}
4455
4456bool MethodVerifier::CheckSignaturePolymorphicReceiver(const Instruction* inst) {
4457 const RegType& this_type = work_line_->GetInvocationThis(this, inst);
4458 if (this_type.IsZero()) {
4459 /* null pointer always passes (and always fails at run time) */
4460 return true;
4461 } else if (!this_type.IsNonZeroReferenceTypes()) {
4462 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
4463 << "invoke-polymorphic receiver is not a reference: "
4464 << this_type;
4465 return false;
4466 } else if (this_type.IsUninitializedReference()) {
4467 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
4468 << "invoke-polymorphic receiver is uninitialized: "
4469 << this_type;
4470 return false;
4471 } else if (!this_type.HasClass()) {
4472 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
4473 << "invoke-polymorphic receiver has no class: "
4474 << this_type;
4475 return false;
4476 } else if (!this_type.GetClass()->IsSubClass(mirror::MethodHandle::StaticClass())) {
4477 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
4478 << "invoke-polymorphic receiver is not a subclass of MethodHandle: "
4479 << this_type;
4480 return false;
4481 }
4482 return true;
Ian Rogersd81871c2011-10-03 13:57:23 -07004483}
4484
Mathieu Chartiere401d142015-04-22 13:56:20 -07004485ArtMethod* MethodVerifier::GetQuickInvokedMethod(const Instruction* inst, RegisterLine* reg_line,
4486 bool is_range, bool allow_failure) {
Mathieu Chartier091d2382015-03-06 10:59:06 -08004487 if (is_range) {
4488 DCHECK_EQ(inst->Opcode(), Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
4489 } else {
4490 DCHECK_EQ(inst->Opcode(), Instruction::INVOKE_VIRTUAL_QUICK);
4491 }
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004492 const RegType& actual_arg_type = reg_line->GetInvocationThis(this, inst, allow_failure);
Ian Rogers9bc54402014-04-17 16:40:01 -07004493 if (!actual_arg_type.HasClass()) {
4494 VLOG(verifier) << "Failed to get mirror::Class* from '" << actual_arg_type << "'";
Andreas Gampe63981562014-04-17 12:28:43 -07004495 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004496 }
Ian Rogersa4cf1df2014-05-07 19:47:17 -07004497 mirror::Class* klass = actual_arg_type.GetClass();
Mingyao Yang2cdbad72014-07-16 10:44:41 -07004498 mirror::Class* dispatch_class;
Ian Rogersa4cf1df2014-05-07 19:47:17 -07004499 if (klass->IsInterface()) {
4500 // Derive Object.class from Class.class.getSuperclass().
4501 mirror::Class* object_klass = klass->GetClass()->GetSuperClass();
Andreas Gampe7c038102014-10-27 20:08:46 -07004502 if (FailOrAbort(this, object_klass->IsObjectClass(),
Mathieu Chartier091d2382015-03-06 10:59:06 -08004503 "Failed to find Object class in quickened invoke receiver", work_insn_idx_)) {
Andreas Gampe7c038102014-10-27 20:08:46 -07004504 return nullptr;
4505 }
Mingyao Yang2cdbad72014-07-16 10:44:41 -07004506 dispatch_class = object_klass;
Ian Rogersa4cf1df2014-05-07 19:47:17 -07004507 } else {
Mingyao Yang2cdbad72014-07-16 10:44:41 -07004508 dispatch_class = klass;
Ian Rogersa4cf1df2014-05-07 19:47:17 -07004509 }
Mathieu Chartier091d2382015-03-06 10:59:06 -08004510 if (!dispatch_class->HasVTable()) {
4511 FailOrAbort(this, allow_failure, "Receiver class has no vtable for quickened invoke at ",
4512 work_insn_idx_);
Andreas Gampe7c038102014-10-27 20:08:46 -07004513 return nullptr;
4514 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004515 uint16_t vtable_index = is_range ? inst->VRegB_3rc() : inst->VRegB_35c();
Mathieu Chartiere401d142015-04-22 13:56:20 -07004516 auto* cl = Runtime::Current()->GetClassLinker();
4517 auto pointer_size = cl->GetImagePointerSize();
Mathieu Chartier091d2382015-03-06 10:59:06 -08004518 if (static_cast<int32_t>(vtable_index) >= dispatch_class->GetVTableLength()) {
4519 FailOrAbort(this, allow_failure,
4520 "Receiver class has not enough vtable slots for quickened invoke at ",
4521 work_insn_idx_);
Andreas Gampe7c038102014-10-27 20:08:46 -07004522 return nullptr;
4523 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07004524 ArtMethod* res_method = dispatch_class->GetVTableEntry(vtable_index, pointer_size);
Mathieu Chartier091d2382015-03-06 10:59:06 -08004525 if (self_->IsExceptionPending()) {
4526 FailOrAbort(this, allow_failure, "Unexpected exception pending for quickened invoke at ",
4527 work_insn_idx_);
Andreas Gampe7c038102014-10-27 20:08:46 -07004528 return nullptr;
4529 }
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004530 return res_method;
4531}
4532
Mathieu Chartiere401d142015-04-22 13:56:20 -07004533ArtMethod* MethodVerifier::VerifyInvokeVirtualQuickArgs(const Instruction* inst, bool is_range) {
Andreas Gampe76bd8802014-12-10 16:43:58 -08004534 DCHECK(Runtime::Current()->IsStarted() || verify_to_dump_)
David Sehr709b0702016-10-13 09:12:37 -07004535 << dex_file_->PrettyMethod(dex_method_idx_, true) << "@" << work_insn_idx_;
Andreas Gampe76bd8802014-12-10 16:43:58 -08004536
Mathieu Chartiere401d142015-04-22 13:56:20 -07004537 ArtMethod* res_method = GetQuickInvokedMethod(inst, work_line_.get(), is_range, false);
Ian Rogers7b078e82014-09-10 14:44:24 -07004538 if (res_method == nullptr) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004539 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer method from " << inst->Name();
Ian Rogers7b078e82014-09-10 14:44:24 -07004540 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004541 }
Andreas Gampe7c038102014-10-27 20:08:46 -07004542 if (FailOrAbort(this, !res_method->IsDirect(), "Quick-invoked method is direct at ",
4543 work_insn_idx_)) {
4544 return nullptr;
4545 }
4546 if (FailOrAbort(this, !res_method->IsStatic(), "Quick-invoked method is static at ",
4547 work_insn_idx_)) {
4548 return nullptr;
4549 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004550
4551 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
4552 // match the call to the signature. Also, we might be calling through an abstract method
4553 // definition (which doesn't have register count values).
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004554 const RegType& actual_arg_type = work_line_->GetInvocationThis(this, inst);
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004555 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
Ian Rogers7b078e82014-09-10 14:44:24 -07004556 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004557 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004558 const size_t expected_args = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
4559 /* caught by static verifier */
4560 DCHECK(is_range || expected_args <= 5);
4561 if (expected_args > code_item_->outs_size_) {
4562 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
4563 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
Ian Rogers7b078e82014-09-10 14:44:24 -07004564 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004565 }
4566
4567 /*
4568 * Check the "this" argument, which must be an instance of the class that declared the method.
4569 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
4570 * rigorous check here (which is okay since we have to do it at runtime).
4571 */
David Brazdil68b5c0b2016-01-19 14:25:29 +00004572 // Note: given an uninitialized type, this should always fail. Constructors aren't virtual.
4573 if (actual_arg_type.IsUninitializedTypes() && !res_method->IsConstructor()) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004574 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
Ian Rogers7b078e82014-09-10 14:44:24 -07004575 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004576 }
4577 if (!actual_arg_type.IsZero()) {
4578 mirror::Class* klass = res_method->GetDeclaringClass();
Ian Rogers1ff3c982014-08-12 02:30:58 -07004579 std::string temp;
Ian Rogersd8f69b02014-09-10 21:43:52 +00004580 const RegType& res_method_class =
Andreas Gampef23f33d2015-06-23 14:18:17 -07004581 FromClass(klass->GetDescriptor(&temp), klass, klass->CannotBeAssignedFromOtherTypes());
David Brazdilca3c8c32016-09-06 14:04:48 +01004582 if (!res_method_class.IsAssignableFrom(actual_arg_type, this)) {
David Brazdil68b5c0b2016-01-19 14:25:29 +00004583 Fail(actual_arg_type.IsUninitializedTypes() // Just overcautious - should have never
4584 ? VERIFY_ERROR_BAD_CLASS_HARD // quickened this.
4585 : actual_arg_type.IsUnresolvedTypes()
4586 ? VERIFY_ERROR_NO_CLASS
4587 : VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004588 << "' not instance of '" << res_method_class << "'";
Ian Rogers7b078e82014-09-10 14:44:24 -07004589 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004590 }
4591 }
4592 /*
4593 * Process the target method's signature. This signature may or may not
4594 * have been verified, so we can't assume it's properly formed.
4595 */
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07004596 const DexFile::TypeList* params = res_method->GetParameterTypeList();
Ian Rogers7b078e82014-09-10 14:44:24 -07004597 size_t params_size = params == nullptr ? 0 : params->Size();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004598 uint32_t arg[5];
4599 if (!is_range) {
Ian Rogers29a26482014-05-02 15:27:29 -07004600 inst->GetVarArgs(arg);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004601 }
4602 size_t actual_args = 1;
4603 for (size_t param_index = 0; param_index < params_size; param_index++) {
4604 if (actual_args >= expected_args) {
David Sehr709b0702016-10-13 09:12:37 -07004605 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invalid call to '"
4606 << res_method->PrettyMethod()
Brian Carlstrom93c33962013-07-26 10:37:43 -07004607 << "'. Expected " << expected_args
4608 << " arguments, processing argument " << actual_args
4609 << " (where longs/doubles count twice).";
Ian Rogers7b078e82014-09-10 14:44:24 -07004610 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004611 }
4612 const char* descriptor =
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07004613 res_method->GetTypeDescriptorFromTypeIdx(params->GetTypeItem(param_index).type_idx_);
Ian Rogers7b078e82014-09-10 14:44:24 -07004614 if (descriptor == nullptr) {
David Sehr709b0702016-10-13 09:12:37 -07004615 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of "
4616 << res_method->PrettyMethod()
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004617 << " missing signature component";
Ian Rogers7b078e82014-09-10 14:44:24 -07004618 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004619 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00004620 const RegType& reg_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004621 uint32_t get_reg = is_range ? inst->VRegC_3rc() + actual_args : arg[actual_args];
Ian Rogers7b078e82014-09-10 14:44:24 -07004622 if (!work_line_->VerifyRegisterType(this, get_reg, reg_type)) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004623 return res_method;
4624 }
4625 actual_args = reg_type.IsLongOrDoubleTypes() ? actual_args + 2 : actual_args + 1;
4626 }
4627 if (actual_args != expected_args) {
David Sehr709b0702016-10-13 09:12:37 -07004628 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of "
4629 << res_method->PrettyMethod() << " expected "
4630 << expected_args << " arguments, found " << actual_args;
Ian Rogers7b078e82014-09-10 14:44:24 -07004631 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004632 } else {
4633 return res_method;
4634 }
4635}
4636
Ian Rogers62342ec2013-06-11 10:26:37 -07004637void MethodVerifier::VerifyNewArray(const Instruction* inst, bool is_filled, bool is_range) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08004638 dex::TypeIndex type_idx;
Sebastien Hertz5243e912013-05-21 10:55:07 +02004639 if (!is_filled) {
4640 DCHECK_EQ(inst->Opcode(), Instruction::NEW_ARRAY);
Andreas Gampea5b09a62016-11-17 15:21:22 -08004641 type_idx = dex::TypeIndex(inst->VRegC_22c());
Sebastien Hertz5243e912013-05-21 10:55:07 +02004642 } else if (!is_range) {
4643 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY);
Andreas Gampea5b09a62016-11-17 15:21:22 -08004644 type_idx = dex::TypeIndex(inst->VRegB_35c());
Sebastien Hertz5243e912013-05-21 10:55:07 +02004645 } else {
4646 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY_RANGE);
Andreas Gampea5b09a62016-11-17 15:21:22 -08004647 type_idx = dex::TypeIndex(inst->VRegB_3rc());
Sebastien Hertz5243e912013-05-21 10:55:07 +02004648 }
Andreas Gampe98be1a92017-08-28 08:25:45 -07004649 const RegType& res_type = ResolveClass<CheckAccess::kYes>(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004650 if (res_type.IsConflict()) { // bad class
4651 DCHECK_NE(failures_.size(), 0U);
Ian Rogers0c4a5062012-02-03 15:18:59 -08004652 } else {
4653 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
4654 if (!res_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07004655 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "new-array on non-array class " << res_type;
Ian Rogers0c4a5062012-02-03 15:18:59 -08004656 } else if (!is_filled) {
4657 /* make sure "size" register is valid type */
Ian Rogers7b078e82014-09-10 14:44:24 -07004658 work_line_->VerifyRegisterType(this, inst->VRegB_22c(), reg_types_.Integer());
Ian Rogers0c4a5062012-02-03 15:18:59 -08004659 /* set register type to array class */
Ian Rogersd8f69b02014-09-10 21:43:52 +00004660 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
Andreas Gampead238ce2015-08-24 21:13:08 -07004661 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_22c(), precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08004662 } else {
Andreas Gampebb18a032016-03-22 20:34:25 -07004663 DCHECK(!res_type.IsUnresolvedMergedReference());
Ian Rogers0c4a5062012-02-03 15:18:59 -08004664 // Verify each register. If "arg_count" is bad, VerifyRegisterType() will run off the end of
4665 // the list and fail. It's legal, if silly, for arg_count to be zero.
Ian Rogersd8f69b02014-09-10 21:43:52 +00004666 const RegType& expected_type = reg_types_.GetComponentType(res_type, GetClassLoader());
Sebastien Hertz5243e912013-05-21 10:55:07 +02004667 uint32_t arg_count = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
4668 uint32_t arg[5];
4669 if (!is_range) {
Ian Rogers29a26482014-05-02 15:27:29 -07004670 inst->GetVarArgs(arg);
Sebastien Hertz5243e912013-05-21 10:55:07 +02004671 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08004672 for (size_t ui = 0; ui < arg_count; ui++) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02004673 uint32_t get_reg = is_range ? inst->VRegC_3rc() + ui : arg[ui];
Ian Rogers7b078e82014-09-10 14:44:24 -07004674 if (!work_line_->VerifyRegisterType(this, get_reg, expected_type)) {
4675 work_line_->SetResultRegisterType(this, reg_types_.Conflict());
Ian Rogers0c4a5062012-02-03 15:18:59 -08004676 return;
4677 }
4678 }
4679 // filled-array result goes into "result" register
Ian Rogersd8f69b02014-09-10 21:43:52 +00004680 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
Ian Rogers7b078e82014-09-10 14:44:24 -07004681 work_line_->SetResultRegisterType(this, precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08004682 }
4683 }
4684}
4685
Sebastien Hertz5243e912013-05-21 10:55:07 +02004686void MethodVerifier::VerifyAGet(const Instruction* inst,
Ian Rogersd8f69b02014-09-10 21:43:52 +00004687 const RegType& insn_type, bool is_primitive) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004688 const RegType& index_type = work_line_->GetRegisterType(this, inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07004689 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07004690 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07004691 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004692 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegB_23x());
Ian Rogers89310de2012-02-01 13:47:30 -08004693 if (array_type.IsZero()) {
Nicolas Geoffray4824c272015-06-24 15:53:03 +01004694 have_pending_runtime_throw_failure_ = true;
Ian Rogers89310de2012-02-01 13:47:30 -08004695 // Null array class; this code path will fail at runtime. Infer a merge-able type from the
4696 // instruction type. TODO: have a proper notion of bottom here.
4697 if (!is_primitive || insn_type.IsCategory1Types()) {
4698 // Reference or category 1
Andreas Gampead238ce2015-08-24 21:13:08 -07004699 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), reg_types_.Zero());
Ian Rogersd81871c2011-10-03 13:57:23 -07004700 } else {
Ian Rogers89310de2012-02-01 13:47:30 -08004701 // Category 2
Ian Rogers7b078e82014-09-10 14:44:24 -07004702 work_line_->SetRegisterTypeWide(this, inst->VRegA_23x(),
4703 reg_types_.FromCat2ConstLo(0, false),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004704 reg_types_.FromCat2ConstHi(0, false));
Ian Rogers89310de2012-02-01 13:47:30 -08004705 }
jeffhaofc3144e2012-02-01 17:21:15 -08004706 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07004707 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aget";
Andreas Gampe8d8fc482016-03-25 16:24:20 -07004708 } else if (array_type.IsUnresolvedMergedReference()) {
Andreas Gampebb18a032016-03-22 20:34:25 -07004709 // Unresolved array types must be reference array types.
4710 if (is_primitive) {
4711 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type
4712 << " source for category 1 aget";
4713 } else {
4714 Fail(VERIFY_ERROR_NO_CLASS) << "cannot verify aget for " << array_type
4715 << " because of missing class";
Andreas Gampe8d8fc482016-03-25 16:24:20 -07004716 // Approximate with java.lang.Object[].
4717 work_line_->SetRegisterType<LockOp::kClear>(this,
4718 inst->VRegA_23x(),
4719 reg_types_.JavaLangObject(false));
Andreas Gampebb18a032016-03-22 20:34:25 -07004720 }
Ian Rogers89310de2012-02-01 13:47:30 -08004721 } else {
4722 /* verify the class */
Ian Rogersd8f69b02014-09-10 21:43:52 +00004723 const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
jeffhaofc3144e2012-02-01 17:21:15 -08004724 if (!component_type.IsReferenceTypes() && !is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07004725 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08004726 << " source for aget-object";
4727 } else if (component_type.IsNonZeroReferenceTypes() && is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07004728 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08004729 << " source for category 1 aget";
4730 } else if (is_primitive && !insn_type.Equals(component_type) &&
4731 !((insn_type.IsInteger() && component_type.IsFloat()) ||
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004732 (insn_type.IsLong() && component_type.IsDouble()))) {
4733 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array type " << array_type
4734 << " incompatible with aget of type " << insn_type;
Ian Rogers89310de2012-02-01 13:47:30 -08004735 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004736 // Use knowledge of the field type which is stronger than the type inferred from the
4737 // instruction, which can't differentiate object types and ints from floats, longs from
4738 // doubles.
4739 if (!component_type.IsLowHalf()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07004740 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), component_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004741 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004742 work_line_->SetRegisterTypeWide(this, inst->VRegA_23x(), component_type,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004743 component_type.HighHalf(&reg_types_));
4744 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004745 }
4746 }
4747 }
4748}
4749
Ian Rogersd8f69b02014-09-10 21:43:52 +00004750void MethodVerifier::VerifyPrimitivePut(const RegType& target_type, const RegType& insn_type,
Jeff Haofe1f7c82013-08-01 14:50:24 -07004751 const uint32_t vregA) {
4752 // Primitive assignability rules are weaker than regular assignability rules.
4753 bool instruction_compatible;
4754 bool value_compatible;
Ian Rogers7b078e82014-09-10 14:44:24 -07004755 const RegType& value_type = work_line_->GetRegisterType(this, vregA);
Jeff Haofe1f7c82013-08-01 14:50:24 -07004756 if (target_type.IsIntegralTypes()) {
Jeff Haoa4647482013-08-06 15:35:47 -07004757 instruction_compatible = target_type.Equals(insn_type);
Jeff Haofe1f7c82013-08-01 14:50:24 -07004758 value_compatible = value_type.IsIntegralTypes();
4759 } else if (target_type.IsFloat()) {
4760 instruction_compatible = insn_type.IsInteger(); // no put-float, so expect put-int
4761 value_compatible = value_type.IsFloatTypes();
4762 } else if (target_type.IsLong()) {
4763 instruction_compatible = insn_type.IsLong();
Andreas Gampe376fa682014-09-07 13:06:12 -07004764 // Additional register check: this is not checked statically (as part of VerifyInstructions),
4765 // as target_type depends on the resolved type of the field.
4766 if (instruction_compatible && work_line_->NumRegs() > vregA + 1) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004767 const RegType& value_type_hi = work_line_->GetRegisterType(this, vregA + 1);
Andreas Gampe376fa682014-09-07 13:06:12 -07004768 value_compatible = value_type.IsLongTypes() && value_type.CheckWidePair(value_type_hi);
4769 } else {
4770 value_compatible = false;
4771 }
Jeff Haofe1f7c82013-08-01 14:50:24 -07004772 } else if (target_type.IsDouble()) {
4773 instruction_compatible = insn_type.IsLong(); // no put-double, so expect put-long
Andreas Gampe376fa682014-09-07 13:06:12 -07004774 // Additional register check: this is not checked statically (as part of VerifyInstructions),
4775 // as target_type depends on the resolved type of the field.
4776 if (instruction_compatible && work_line_->NumRegs() > vregA + 1) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004777 const RegType& value_type_hi = work_line_->GetRegisterType(this, vregA + 1);
Andreas Gampe376fa682014-09-07 13:06:12 -07004778 value_compatible = value_type.IsDoubleTypes() && value_type.CheckWidePair(value_type_hi);
4779 } else {
4780 value_compatible = false;
4781 }
Jeff Haofe1f7c82013-08-01 14:50:24 -07004782 } else {
4783 instruction_compatible = false; // reference with primitive store
4784 value_compatible = false; // unused
4785 }
4786 if (!instruction_compatible) {
4787 // This is a global failure rather than a class change failure as the instructions and
4788 // the descriptors for the type should have been consistent within the same file at
4789 // compile time.
4790 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "put insn has type '" << insn_type
4791 << "' but expected type '" << target_type << "'";
4792 return;
4793 }
4794 if (!value_compatible) {
4795 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
4796 << " of type " << value_type << " but expected " << target_type << " for put";
4797 return;
4798 }
4799}
4800
Sebastien Hertz5243e912013-05-21 10:55:07 +02004801void MethodVerifier::VerifyAPut(const Instruction* inst,
Ian Rogersd8f69b02014-09-10 21:43:52 +00004802 const RegType& insn_type, bool is_primitive) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004803 const RegType& index_type = work_line_->GetRegisterType(this, inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07004804 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07004805 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07004806 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004807 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegB_23x());
Ian Rogers89310de2012-02-01 13:47:30 -08004808 if (array_type.IsZero()) {
Nicolas Geoffray66389fb2015-06-19 10:35:42 +01004809 // Null array type; this code path will fail at runtime.
4810 // Still check that the given value matches the instruction's type.
Andreas Gampe4bf4c782015-08-14 14:07:43 -07004811 // Note: this is, as usual, complicated by the fact the the instruction isn't fully typed
4812 // and fits multiple register types.
4813 const RegType* modified_reg_type = &insn_type;
4814 if ((modified_reg_type == &reg_types_.Integer()) ||
4815 (modified_reg_type == &reg_types_.LongLo())) {
4816 // May be integer or float | long or double. Overwrite insn_type accordingly.
4817 const RegType& value_type = work_line_->GetRegisterType(this, inst->VRegA_23x());
4818 if (modified_reg_type == &reg_types_.Integer()) {
4819 if (&value_type == &reg_types_.Float()) {
4820 modified_reg_type = &value_type;
4821 }
4822 } else {
4823 if (&value_type == &reg_types_.DoubleLo()) {
4824 modified_reg_type = &value_type;
4825 }
4826 }
4827 }
4828 work_line_->VerifyRegisterType(this, inst->VRegA_23x(), *modified_reg_type);
jeffhaofc3144e2012-02-01 17:21:15 -08004829 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07004830 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aput";
Andreas Gampe8d8fc482016-03-25 16:24:20 -07004831 } else if (array_type.IsUnresolvedMergedReference()) {
Andreas Gampebb18a032016-03-22 20:34:25 -07004832 // Unresolved array types must be reference array types.
4833 if (is_primitive) {
4834 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "put insn has type '" << insn_type
4835 << "' but unresolved type '" << array_type << "'";
4836 } else {
4837 Fail(VERIFY_ERROR_NO_CLASS) << "cannot verify aput for " << array_type
4838 << " because of missing class";
4839 }
Ian Rogers89310de2012-02-01 13:47:30 -08004840 } else {
Ian Rogersd8f69b02014-09-10 21:43:52 +00004841 const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
Jeff Haofe1f7c82013-08-01 14:50:24 -07004842 const uint32_t vregA = inst->VRegA_23x();
Jeff Haob24b4a72013-07-31 13:47:31 -07004843 if (is_primitive) {
Jeff Haofe1f7c82013-08-01 14:50:24 -07004844 VerifyPrimitivePut(component_type, insn_type, vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07004845 } else {
Jeff Haob24b4a72013-07-31 13:47:31 -07004846 if (!component_type.IsReferenceTypes()) {
4847 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
4848 << " source for aput-object";
4849 } else {
4850 // The instruction agrees with the type of array, confirm the value to be stored does too
4851 // Note: we use the instruction type (rather than the component type) for aput-object as
4852 // incompatible classes will be caught at runtime as an array store exception
Ian Rogers7b078e82014-09-10 14:44:24 -07004853 work_line_->VerifyRegisterType(this, vregA, insn_type);
Jeff Haob24b4a72013-07-31 13:47:31 -07004854 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004855 }
4856 }
4857 }
4858}
4859
Mathieu Chartierc7853442015-03-27 14:35:38 -07004860ArtField* MethodVerifier::GetStaticField(int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08004861 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
4862 // Check access to class
Andreas Gampe98be1a92017-08-28 08:25:45 -07004863 const RegType& klass_type = ResolveClass<CheckAccess::kYes>(field_id.class_idx_);
Brian Carlstrom7934ac22013-07-26 10:54:15 -07004864 if (klass_type.IsConflict()) { // bad class
Ian Rogersad0b3a32012-04-16 14:50:24 -07004865 AppendToLastFailMessage(StringPrintf(" in attempt to access static field %d (%s) in %s",
4866 field_idx, dex_file_->GetFieldName(field_id),
4867 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers7b078e82014-09-10 14:44:24 -07004868 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08004869 }
Elliott Hughesb25c3f62012-03-26 16:35:06 -07004870 if (klass_type.IsUnresolvedTypes()) {
Andreas Gampe629be512017-08-25 17:09:32 -07004871 // Accessibility checks depend on resolved fields.
4872 DCHECK(klass_type.Equals(GetDeclaringClass()) || !failures_.empty());
4873
Ian Rogers7b078e82014-09-10 14:44:24 -07004874 return nullptr; // Can't resolve Class so no more to do here, will do checking at runtime.
Ian Rogers90040192011-12-16 08:54:29 -08004875 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07004876 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
David Brazdilca3c8c32016-09-06 14:04:48 +01004877 ArtField* field = class_linker->ResolveFieldJLS(*dex_file_, field_idx, dex_cache_, class_loader_);
4878
4879 // Record result of the field resolution attempt.
4880 VerifierDeps::MaybeRecordFieldResolution(*dex_file_, field_idx, field);
4881
Ian Rogers7b078e82014-09-10 14:44:24 -07004882 if (field == nullptr) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07004883 VLOG(verifier) << "Unable to resolve static field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07004884 << dex_file_->GetFieldName(field_id) << ") in "
4885 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogers7b078e82014-09-10 14:44:24 -07004886 DCHECK(self_->IsExceptionPending());
4887 self_->ClearException();
4888 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004889 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
4890 field->GetAccessFlags())) {
David Sehr709b0702016-10-13 09:12:37 -07004891 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access static field " << field->PrettyField()
Ian Rogersad0b3a32012-04-16 14:50:24 -07004892 << " from " << GetDeclaringClass();
Ian Rogers7b078e82014-09-10 14:44:24 -07004893 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07004894 } else if (!field->IsStatic()) {
David Sehr709b0702016-10-13 09:12:37 -07004895 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << field->PrettyField() << " to be static";
Ian Rogers7b078e82014-09-10 14:44:24 -07004896 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07004897 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07004898 return field;
Ian Rogersd81871c2011-10-03 13:57:23 -07004899}
4900
Mathieu Chartierc7853442015-03-27 14:35:38 -07004901ArtField* MethodVerifier::GetInstanceField(const RegType& obj_type, int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08004902 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
Aart Bik31883642016-06-06 15:02:44 -07004903 // Check access to class.
Andreas Gampe98be1a92017-08-28 08:25:45 -07004904 const RegType& klass_type = ResolveClass<CheckAccess::kYes>(field_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004905 if (klass_type.IsConflict()) {
4906 AppendToLastFailMessage(StringPrintf(" in attempt to access instance field %d (%s) in %s",
4907 field_idx, dex_file_->GetFieldName(field_id),
4908 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers7b078e82014-09-10 14:44:24 -07004909 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08004910 }
jeffhao8cd6dda2012-02-22 10:15:34 -08004911 if (klass_type.IsUnresolvedTypes()) {
Andreas Gampe629be512017-08-25 17:09:32 -07004912 // Accessibility checks depend on resolved fields.
4913 DCHECK(klass_type.Equals(GetDeclaringClass()) || !failures_.empty());
4914
Ian Rogers7b078e82014-09-10 14:44:24 -07004915 return nullptr; // Can't resolve Class so no more to do here
Ian Rogers90040192011-12-16 08:54:29 -08004916 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07004917 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
David Brazdilca3c8c32016-09-06 14:04:48 +01004918 ArtField* field = class_linker->ResolveFieldJLS(*dex_file_, field_idx, dex_cache_, class_loader_);
4919
4920 // Record result of the field resolution attempt.
4921 VerifierDeps::MaybeRecordFieldResolution(*dex_file_, field_idx, field);
4922
Ian Rogers7b078e82014-09-10 14:44:24 -07004923 if (field == nullptr) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07004924 VLOG(verifier) << "Unable to resolve instance field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07004925 << dex_file_->GetFieldName(field_id) << ") in "
4926 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogers7b078e82014-09-10 14:44:24 -07004927 DCHECK(self_->IsExceptionPending());
4928 self_->ClearException();
4929 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07004930 } else if (obj_type.IsZero()) {
Aart Bik31883642016-06-06 15:02:44 -07004931 // Cannot infer and check type, however, access will cause null pointer exception.
4932 // Fall through into a few last soft failure checks below.
Stephen Kyle695c5982014-08-22 15:03:07 +01004933 } else if (!obj_type.IsReferenceTypes()) {
Aart Bik31883642016-06-06 15:02:44 -07004934 // Trying to read a field from something that isn't a reference.
Stephen Kyle695c5982014-08-22 15:03:07 +01004935 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance field access on object that has "
4936 << "non-reference type " << obj_type;
Ian Rogers7b078e82014-09-10 14:44:24 -07004937 return nullptr;
Ian Rogerse1758fe2012-04-19 11:31:15 -07004938 } else {
David Brazdil0d638bb2016-07-27 15:29:25 +01004939 std::string temp;
Mathieu Chartier3398c782016-09-30 10:27:43 -07004940 ObjPtr<mirror::Class> klass = field->GetDeclaringClass();
Ian Rogersd8f69b02014-09-10 21:43:52 +00004941 const RegType& field_klass =
Mathieu Chartier3398c782016-09-30 10:27:43 -07004942 FromClass(klass->GetDescriptor(&temp),
Mathieu Chartier1cc62e42016-10-03 18:01:28 -07004943 klass.Ptr(),
Mathieu Chartier3398c782016-09-30 10:27:43 -07004944 klass->CannotBeAssignedFromOtherTypes());
David Brazdil68b5c0b2016-01-19 14:25:29 +00004945 if (obj_type.IsUninitializedTypes()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07004946 // Field accesses through uninitialized references are only allowable for constructors where
David Brazdil68b5c0b2016-01-19 14:25:29 +00004947 // the field is declared in this class.
4948 // Note: this IsConstructor check is technically redundant, as UninitializedThis should only
4949 // appear in constructors.
4950 if (!obj_type.IsUninitializedThisReference() ||
4951 !IsConstructor() ||
4952 !field_klass.Equals(GetDeclaringClass())) {
David Sehr709b0702016-10-13 09:12:37 -07004953 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "cannot access instance field " << field->PrettyField()
David Brazdil68b5c0b2016-01-19 14:25:29 +00004954 << " of a not fully initialized object within the context"
David Sehr709b0702016-10-13 09:12:37 -07004955 << " of " << dex_file_->PrettyMethod(dex_method_idx_);
David Brazdil68b5c0b2016-01-19 14:25:29 +00004956 return nullptr;
4957 }
David Brazdilca3c8c32016-09-06 14:04:48 +01004958 } else if (!field_klass.IsAssignableFrom(obj_type, this)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07004959 // Trying to access C1.field1 using reference of type C2, which is neither C1 or a sub-class
4960 // of C1. For resolution to occur the declared class of the field must be compatible with
4961 // obj_type, we've discovered this wasn't so, so report the field didn't exist.
Andreas Gampe66596242016-04-14 10:55:04 -07004962 VerifyError type;
4963 bool is_aot = Runtime::Current()->IsAotCompiler();
4964 if (is_aot && (field_klass.IsUnresolvedTypes() || obj_type.IsUnresolvedTypes())) {
4965 // Compiler & unresolved types involved, retry at runtime.
4966 type = VerifyError::VERIFY_ERROR_NO_CLASS;
4967 } else {
Andreas Gampe8f4ade02016-04-15 10:09:16 -07004968 // Classes known (resolved; and thus assignability check is precise), or we are at runtime
4969 // and still missing classes. This is a hard failure.
Andreas Gampe66596242016-04-14 10:55:04 -07004970 type = VerifyError::VERIFY_ERROR_BAD_CLASS_HARD;
4971 }
David Sehr709b0702016-10-13 09:12:37 -07004972 Fail(type) << "cannot access instance field " << field->PrettyField()
Andreas Gampe66596242016-04-14 10:55:04 -07004973 << " from object of type " << obj_type;
Ian Rogers7b078e82014-09-10 14:44:24 -07004974 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004975 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004976 }
Aart Bik31883642016-06-06 15:02:44 -07004977
4978 // Few last soft failure checks.
4979 if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
4980 field->GetAccessFlags())) {
David Sehr709b0702016-10-13 09:12:37 -07004981 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access instance field " << field->PrettyField()
Aart Bik31883642016-06-06 15:02:44 -07004982 << " from " << GetDeclaringClass();
4983 return nullptr;
4984 } else if (field->IsStatic()) {
David Sehr709b0702016-10-13 09:12:37 -07004985 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << field->PrettyField()
Aart Bik31883642016-06-06 15:02:44 -07004986 << " to not be static";
4987 return nullptr;
4988 }
4989
4990 return field;
Ian Rogersd81871c2011-10-03 13:57:23 -07004991}
4992
Andreas Gampe896df402014-10-20 22:25:29 -07004993template <MethodVerifier::FieldAccessType kAccType>
4994void MethodVerifier::VerifyISFieldAccess(const Instruction* inst, const RegType& insn_type,
4995 bool is_primitive, bool is_static) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02004996 uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
Mathieu Chartierc7853442015-03-27 14:35:38 -07004997 ArtField* field;
Ian Rogersb94a27b2011-10-26 00:33:41 -07004998 if (is_static) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07004999 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07005000 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07005001 const RegType& object_type = work_line_->GetRegisterType(this, inst->VRegB_22c());
David Brazdil68b5c0b2016-01-19 14:25:29 +00005002
5003 // One is not allowed to access fields on uninitialized references, except to write to
5004 // fields in the constructor (before calling another constructor).
5005 // GetInstanceField does an assignability check which will fail for uninitialized types.
5006 // We thus modify the type if the uninitialized reference is a "this" reference (this also
5007 // checks at the same time that we're verifying a constructor).
5008 bool should_adjust = (kAccType == FieldAccessType::kAccPut) &&
5009 object_type.IsUninitializedThisReference();
5010 const RegType& adjusted_type = should_adjust
5011 ? GetRegTypeCache()->FromUninitialized(object_type)
5012 : object_type;
5013 field = GetInstanceField(adjusted_type, field_idx);
Andreas Gampe896df402014-10-20 22:25:29 -07005014 if (UNLIKELY(have_pending_hard_failure_)) {
5015 return;
5016 }
Alex Light4a2c8fc2016-02-12 11:01:54 -08005017 if (should_adjust) {
5018 if (field == nullptr) {
5019 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "Might be accessing a superclass instance field prior "
5020 << "to the superclass being initialized in "
David Sehr709b0702016-10-13 09:12:37 -07005021 << dex_file_->PrettyMethod(dex_method_idx_);
Alex Light4a2c8fc2016-02-12 11:01:54 -08005022 } else if (field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
5023 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "cannot access superclass instance field "
David Sehr709b0702016-10-13 09:12:37 -07005024 << field->PrettyField() << " of a not fully initialized "
Alex Light4a2c8fc2016-02-12 11:01:54 -08005025 << "object within the context of "
David Sehr709b0702016-10-13 09:12:37 -07005026 << dex_file_->PrettyMethod(dex_method_idx_);
Alex Light4a2c8fc2016-02-12 11:01:54 -08005027 return;
5028 }
5029 }
Ian Rogersb94a27b2011-10-26 00:33:41 -07005030 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00005031 const RegType* field_type = nullptr;
Ian Rogers7b078e82014-09-10 14:44:24 -07005032 if (field != nullptr) {
Andreas Gampe896df402014-10-20 22:25:29 -07005033 if (kAccType == FieldAccessType::kAccPut) {
5034 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
David Sehr709b0702016-10-13 09:12:37 -07005035 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << field->PrettyField()
Andreas Gampe896df402014-10-20 22:25:29 -07005036 << " from other class " << GetDeclaringClass();
Aart Bikc2bc2652016-05-23 14:58:49 -07005037 // Keep hunting for possible hard fails.
Andreas Gampe896df402014-10-20 22:25:29 -07005038 }
5039 }
5040
Mathieu Chartier3398c782016-09-30 10:27:43 -07005041 ObjPtr<mirror::Class> field_type_class =
Mathieu Chartierc7853442015-03-27 14:35:38 -07005042 can_load_classes_ ? field->GetType<true>() : field->GetType<false>();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07005043 if (field_type_class != nullptr) {
Mathieu Chartier3398c782016-09-30 10:27:43 -07005044 field_type = &FromClass(field->GetTypeDescriptor(),
Mathieu Chartier1cc62e42016-10-03 18:01:28 -07005045 field_type_class.Ptr(),
Andreas Gampef23f33d2015-06-23 14:18:17 -07005046 field_type_class->CannotBeAssignedFromOtherTypes());
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08005047 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07005048 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
5049 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07005050 }
Andreas Gampe7da4c402017-08-25 11:30:48 -07005051 } else {
5052 // If we don't have the field (it seems we failed resolution) and this is a PUT, we need to
5053 // redo verification at runtime as the field may be final, unless the field id shows it's in
5054 // the same class.
5055 //
5056 // For simplicity, it is OK to not distinguish compile-time vs runtime, and post this an
5057 // ACCESS_FIELD failure at runtime. This has the same effect as NO_FIELD - punting the class
5058 // to the access-checks interpreter.
5059 //
5060 // Note: see b/34966607. This and above may be changed in the future.
5061 if (kAccType == FieldAccessType::kAccPut) {
5062 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
5063 const char* field_class_descriptor = dex_file_->GetFieldDeclaringClassDescriptor(field_id);
5064 const RegType* field_class_type = &reg_types_.FromDescriptor(GetClassLoader(),
5065 field_class_descriptor,
5066 false);
5067 if (!field_class_type->Equals(GetDeclaringClass())) {
5068 Fail(VERIFY_ERROR_ACCESS_FIELD) << "could not check field put for final field modify of "
5069 << field_class_descriptor
5070 << "."
5071 << dex_file_->GetFieldName(field_id)
5072 << " from other class "
5073 << GetDeclaringClass();
5074 }
5075 }
Ian Rogers0d604842012-04-16 14:50:24 -07005076 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07005077 if (field_type == nullptr) {
5078 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
5079 const char* descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
Mathieu Chartierbf99f772014-08-23 16:37:27 -07005080 field_type = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07005081 }
Sebastien Hertz757b3042014-03-28 14:34:28 +01005082 DCHECK(field_type != nullptr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02005083 const uint32_t vregA = (is_static) ? inst->VRegA_21c() : inst->VRegA_22c();
Andreas Gampe896df402014-10-20 22:25:29 -07005084 static_assert(kAccType == FieldAccessType::kAccPut || kAccType == FieldAccessType::kAccGet,
5085 "Unexpected third access type");
5086 if (kAccType == FieldAccessType::kAccPut) {
5087 // sput or iput.
5088 if (is_primitive) {
5089 VerifyPrimitivePut(*field_type, insn_type, vregA);
Ian Rogersad0b3a32012-04-16 14:50:24 -07005090 } else {
David Brazdilca3c8c32016-09-06 14:04:48 +01005091 if (!insn_type.IsAssignableFrom(*field_type, this)) {
Vladimir Marko414000e2015-06-23 17:45:21 +01005092 // If the field type is not a reference, this is a global failure rather than
5093 // a class change failure as the instructions and the descriptors for the type
5094 // should have been consistent within the same file at compile time.
5095 VerifyError error = field_type->IsReferenceTypes() ? VERIFY_ERROR_BAD_CLASS_SOFT
5096 : VERIFY_ERROR_BAD_CLASS_HARD;
David Sehr709b0702016-10-13 09:12:37 -07005097 Fail(error) << "expected field " << ArtField::PrettyField(field)
Vladimir Marko414000e2015-06-23 17:45:21 +01005098 << " to be compatible with type '" << insn_type
5099 << "' but found type '" << *field_type
5100 << "' in put-object";
Andreas Gampe896df402014-10-20 22:25:29 -07005101 return;
5102 }
5103 work_line_->VerifyRegisterType(this, vregA, *field_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07005104 }
Andreas Gampe896df402014-10-20 22:25:29 -07005105 } else if (kAccType == FieldAccessType::kAccGet) {
5106 // sget or iget.
5107 if (is_primitive) {
5108 if (field_type->Equals(insn_type) ||
5109 (field_type->IsFloat() && insn_type.IsInteger()) ||
5110 (field_type->IsDouble() && insn_type.IsLong())) {
5111 // expected that read is of the correct primitive type or that int reads are reading
5112 // floats or long reads are reading doubles
5113 } else {
5114 // This is a global failure rather than a class change failure as the instructions and
5115 // the descriptors for the type should have been consistent within the same file at
5116 // compile time
David Sehr709b0702016-10-13 09:12:37 -07005117 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << ArtField::PrettyField(field)
Andreas Gampe896df402014-10-20 22:25:29 -07005118 << " to be of type '" << insn_type
5119 << "' but found type '" << *field_type << "' in get";
5120 return;
5121 }
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08005122 } else {
David Brazdilca3c8c32016-09-06 14:04:48 +01005123 if (!insn_type.IsAssignableFrom(*field_type, this)) {
Vladimir Marko414000e2015-06-23 17:45:21 +01005124 // If the field type is not a reference, this is a global failure rather than
5125 // a class change failure as the instructions and the descriptors for the type
5126 // should have been consistent within the same file at compile time.
5127 VerifyError error = field_type->IsReferenceTypes() ? VERIFY_ERROR_BAD_CLASS_SOFT
5128 : VERIFY_ERROR_BAD_CLASS_HARD;
David Sehr709b0702016-10-13 09:12:37 -07005129 Fail(error) << "expected field " << ArtField::PrettyField(field)
Vladimir Marko414000e2015-06-23 17:45:21 +01005130 << " to be compatible with type '" << insn_type
5131 << "' but found type '" << *field_type
5132 << "' in get-object";
Andreas Gampe890da292015-07-06 17:20:18 -07005133 if (error != VERIFY_ERROR_BAD_CLASS_HARD) {
Andreas Gampead238ce2015-08-24 21:13:08 -07005134 work_line_->SetRegisterType<LockOp::kClear>(this, vregA, reg_types_.Conflict());
Andreas Gampe890da292015-07-06 17:20:18 -07005135 }
Andreas Gampe896df402014-10-20 22:25:29 -07005136 return;
5137 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07005138 }
Andreas Gampe896df402014-10-20 22:25:29 -07005139 if (!field_type->IsLowHalf()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07005140 work_line_->SetRegisterType<LockOp::kClear>(this, vregA, *field_type);
Andreas Gampe896df402014-10-20 22:25:29 -07005141 } else {
5142 work_line_->SetRegisterTypeWide(this, vregA, *field_type, field_type->HighHalf(&reg_types_));
5143 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07005144 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07005145 LOG(FATAL) << "Unexpected case.";
Ian Rogersd81871c2011-10-03 13:57:23 -07005146 }
5147}
5148
Mathieu Chartierc7853442015-03-27 14:35:38 -07005149ArtField* MethodVerifier::GetQuickFieldAccess(const Instruction* inst,
Ian Rogers98379392014-02-24 16:53:16 -08005150 RegisterLine* reg_line) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08005151 DCHECK(IsInstructionIGetQuickOrIPutQuick(inst->Opcode())) << inst->Opcode();
Ian Rogers7b078e82014-09-10 14:44:24 -07005152 const RegType& object_type = reg_line->GetRegisterType(this, inst->VRegB_22c());
Ian Rogers9bc54402014-04-17 16:40:01 -07005153 if (!object_type.HasClass()) {
5154 VLOG(verifier) << "Failed to get mirror::Class* from '" << object_type << "'";
5155 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02005156 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02005157 uint32_t field_offset = static_cast<uint32_t>(inst->VRegC_22c());
Mathieu Chartierc7853442015-03-27 14:35:38 -07005158 ArtField* const f = ArtField::FindInstanceFieldWithOffset(object_type.GetClass(), field_offset);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08005159 DCHECK_EQ(f->GetOffset().Uint32Value(), field_offset);
Sebastien Hertz479fc1e2014-04-04 17:51:34 +02005160 if (f == nullptr) {
5161 VLOG(verifier) << "Failed to find instance field at offset '" << field_offset
David Sehr709b0702016-10-13 09:12:37 -07005162 << "' from '" << mirror::Class::PrettyDescriptor(object_type.GetClass()) << "'";
Sebastien Hertz479fc1e2014-04-04 17:51:34 +02005163 }
5164 return f;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02005165}
5166
Andreas Gampe896df402014-10-20 22:25:29 -07005167template <MethodVerifier::FieldAccessType kAccType>
5168void MethodVerifier::VerifyQuickFieldAccess(const Instruction* inst, const RegType& insn_type,
5169 bool is_primitive) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07005170 DCHECK(Runtime::Current()->IsStarted() || verify_to_dump_);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02005171
Mathieu Chartierc7853442015-03-27 14:35:38 -07005172 ArtField* field = GetQuickFieldAccess(inst, work_line_.get());
Ian Rogers7b078e82014-09-10 14:44:24 -07005173 if (field == nullptr) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02005174 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field from " << inst->Name();
5175 return;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02005176 }
Andreas Gampe896df402014-10-20 22:25:29 -07005177
5178 // For an IPUT_QUICK, we now test for final flag of the field.
5179 if (kAccType == FieldAccessType::kAccPut) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02005180 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
David Sehr709b0702016-10-13 09:12:37 -07005181 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << field->PrettyField()
Sebastien Hertzc15853b2013-06-25 17:36:27 +02005182 << " from other class " << GetDeclaringClass();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02005183 return;
5184 }
5185 }
Andreas Gampe896df402014-10-20 22:25:29 -07005186
5187 // Get the field type.
5188 const RegType* field_type;
5189 {
Mathieu Chartier3398c782016-09-30 10:27:43 -07005190 ObjPtr<mirror::Class> field_type_class = can_load_classes_ ? field->GetType<true>() :
Mathieu Chartierc7853442015-03-27 14:35:38 -07005191 field->GetType<false>();
Andreas Gampe896df402014-10-20 22:25:29 -07005192
5193 if (field_type_class != nullptr) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07005194 field_type = &FromClass(field->GetTypeDescriptor(),
Mathieu Chartier1cc62e42016-10-03 18:01:28 -07005195 field_type_class.Ptr(),
Andreas Gampef23f33d2015-06-23 14:18:17 -07005196 field_type_class->CannotBeAssignedFromOtherTypes());
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02005197 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07005198 Thread* self = Thread::Current();
5199 DCHECK(!can_load_classes_ || self->IsExceptionPending());
5200 self->ClearException();
5201 field_type = &reg_types_.FromDescriptor(field->GetDeclaringClass()->GetClassLoader(),
Mathieu Chartierde40d472015-10-15 17:47:48 -07005202 field->GetTypeDescriptor(),
5203 false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02005204 }
Andreas Gampe896df402014-10-20 22:25:29 -07005205 if (field_type == nullptr) {
5206 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field type from " << inst->Name();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02005207 return;
5208 }
Andreas Gampe896df402014-10-20 22:25:29 -07005209 }
5210
5211 const uint32_t vregA = inst->VRegA_22c();
5212 static_assert(kAccType == FieldAccessType::kAccPut || kAccType == FieldAccessType::kAccGet,
5213 "Unexpected third access type");
5214 if (kAccType == FieldAccessType::kAccPut) {
5215 if (is_primitive) {
5216 // Primitive field assignability rules are weaker than regular assignability rules
5217 bool instruction_compatible;
5218 bool value_compatible;
5219 const RegType& value_type = work_line_->GetRegisterType(this, vregA);
5220 if (field_type->IsIntegralTypes()) {
5221 instruction_compatible = insn_type.IsIntegralTypes();
5222 value_compatible = value_type.IsIntegralTypes();
5223 } else if (field_type->IsFloat()) {
5224 instruction_compatible = insn_type.IsInteger(); // no [is]put-float, so expect [is]put-int
5225 value_compatible = value_type.IsFloatTypes();
5226 } else if (field_type->IsLong()) {
5227 instruction_compatible = insn_type.IsLong();
5228 value_compatible = value_type.IsLongTypes();
5229 } else if (field_type->IsDouble()) {
5230 instruction_compatible = insn_type.IsLong(); // no [is]put-double, so expect [is]put-long
5231 value_compatible = value_type.IsDoubleTypes();
5232 } else {
5233 instruction_compatible = false; // reference field with primitive store
5234 value_compatible = false; // unused
5235 }
5236 if (!instruction_compatible) {
5237 // This is a global failure rather than a class change failure as the instructions and
5238 // the descriptors for the type should have been consistent within the same file at
5239 // compile time
David Sehr709b0702016-10-13 09:12:37 -07005240 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << ArtField::PrettyField(field)
Andreas Gampe896df402014-10-20 22:25:29 -07005241 << " to be of type '" << insn_type
5242 << "' but found type '" << *field_type
5243 << "' in put";
5244 return;
5245 }
5246 if (!value_compatible) {
5247 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
5248 << " of type " << value_type
5249 << " but expected " << *field_type
David Sehr709b0702016-10-13 09:12:37 -07005250 << " for store to " << ArtField::PrettyField(field) << " in put";
Andreas Gampe896df402014-10-20 22:25:29 -07005251 return;
5252 }
5253 } else {
David Brazdilca3c8c32016-09-06 14:04:48 +01005254 if (!insn_type.IsAssignableFrom(*field_type, this)) {
David Sehr709b0702016-10-13 09:12:37 -07005255 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << ArtField::PrettyField(field)
Andreas Gampe896df402014-10-20 22:25:29 -07005256 << " to be compatible with type '" << insn_type
5257 << "' but found type '" << *field_type
5258 << "' in put-object";
5259 return;
5260 }
5261 work_line_->VerifyRegisterType(this, vregA, *field_type);
5262 }
5263 } else if (kAccType == FieldAccessType::kAccGet) {
5264 if (is_primitive) {
5265 if (field_type->Equals(insn_type) ||
5266 (field_type->IsFloat() && insn_type.IsIntegralTypes()) ||
5267 (field_type->IsDouble() && insn_type.IsLongTypes())) {
5268 // expected that read is of the correct primitive type or that int reads are reading
5269 // floats or long reads are reading doubles
5270 } else {
5271 // This is a global failure rather than a class change failure as the instructions and
5272 // the descriptors for the type should have been consistent within the same file at
5273 // compile time
David Sehr709b0702016-10-13 09:12:37 -07005274 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << ArtField::PrettyField(field)
Andreas Gampe896df402014-10-20 22:25:29 -07005275 << " to be of type '" << insn_type
5276 << "' but found type '" << *field_type << "' in Get";
5277 return;
5278 }
5279 } else {
David Brazdilca3c8c32016-09-06 14:04:48 +01005280 if (!insn_type.IsAssignableFrom(*field_type, this)) {
David Sehr709b0702016-10-13 09:12:37 -07005281 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << ArtField::PrettyField(field)
Andreas Gampe896df402014-10-20 22:25:29 -07005282 << " to be compatible with type '" << insn_type
5283 << "' but found type '" << *field_type
5284 << "' in get-object";
Andreas Gampead238ce2015-08-24 21:13:08 -07005285 work_line_->SetRegisterType<LockOp::kClear>(this, vregA, reg_types_.Conflict());
Andreas Gampe896df402014-10-20 22:25:29 -07005286 return;
5287 }
5288 }
5289 if (!field_type->IsLowHalf()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07005290 work_line_->SetRegisterType<LockOp::kClear>(this, vregA, *field_type);
Andreas Gampe896df402014-10-20 22:25:29 -07005291 } else {
5292 work_line_->SetRegisterTypeWide(this, vregA, *field_type, field_type->HighHalf(&reg_types_));
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02005293 }
5294 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07005295 LOG(FATAL) << "Unexpected case.";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02005296 }
5297}
5298
Ian Rogers776ac1f2012-04-13 23:36:36 -07005299bool MethodVerifier::CheckNotMoveException(const uint16_t* insns, int insn_idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07005300 if ((insns[insn_idx] & 0xff) == Instruction::MOVE_EXCEPTION) {
jeffhaod5347e02012-03-22 17:25:05 -07005301 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-exception";
Ian Rogersd81871c2011-10-03 13:57:23 -07005302 return false;
5303 }
5304 return true;
5305}
5306
Stephen Kyle9bc61992014-09-22 13:53:15 +01005307bool MethodVerifier::CheckNotMoveResult(const uint16_t* insns, int insn_idx) {
5308 if (((insns[insn_idx] & 0xff) >= Instruction::MOVE_RESULT) &&
5309 ((insns[insn_idx] & 0xff) <= Instruction::MOVE_RESULT_OBJECT)) {
5310 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-result*";
5311 return false;
5312 }
5313 return true;
5314}
5315
5316bool MethodVerifier::CheckNotMoveExceptionOrMoveResult(const uint16_t* insns, int insn_idx) {
5317 return (CheckNotMoveException(insns, insn_idx) && CheckNotMoveResult(insns, insn_idx));
5318}
5319
Ian Rogersebbdd872014-07-07 23:53:08 -07005320bool MethodVerifier::UpdateRegisters(uint32_t next_insn, RegisterLine* merge_line,
5321 bool update_merge_line) {
Ian Rogersd81871c2011-10-03 13:57:23 -07005322 bool changed = true;
5323 RegisterLine* target_line = reg_table_.GetLine(next_insn);
Mathieu Chartierde40d472015-10-15 17:47:48 -07005324 if (!GetInstructionFlags(next_insn).IsVisitedOrChanged()) {
jeffhaobdb76512011-09-07 11:43:16 -07005325 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07005326 * We haven't processed this instruction before, and we haven't touched the registers here, so
5327 * there's nothing to "merge". Copy the registers over and mark it as changed. (This is the
5328 * only way a register can transition out of "unknown", so this is not just an optimization.)
jeffhaobdb76512011-09-07 11:43:16 -07005329 */
Andreas Gampea727e372015-08-25 09:22:37 -07005330 target_line->CopyFromLine(merge_line);
Mathieu Chartierde40d472015-10-15 17:47:48 -07005331 if (GetInstructionFlags(next_insn).IsReturn()) {
Jeff Haob24b4a72013-07-31 13:47:31 -07005332 // Verify that the monitor stack is empty on return.
Andreas Gampea727e372015-08-25 09:22:37 -07005333 merge_line->VerifyMonitorStackEmpty(this);
5334
Ian Rogersb8c78592013-07-25 23:52:52 +00005335 // For returns we only care about the operand to the return, all other registers are dead.
5336 // Initialize them as conflicts so they don't add to GC and deoptimization information.
5337 const Instruction* ret_inst = Instruction::At(code_item_->insns_ + next_insn);
Andreas Gampea727e372015-08-25 09:22:37 -07005338 AdjustReturnLine(this, ret_inst, target_line);
Aart Bik31883642016-06-06 15:02:44 -07005339 // Directly bail if a hard failure was found.
Aart Bikb0526322016-06-01 14:06:00 -07005340 if (have_pending_hard_failure_) {
5341 return false;
5342 }
Ian Rogersb8c78592013-07-25 23:52:52 +00005343 }
jeffhaobdb76512011-09-07 11:43:16 -07005344 } else {
Mathieu Chartier361e04a2016-02-16 14:06:35 -08005345 RegisterLineArenaUniquePtr copy;
Andreas Gampeec6e6c12015-11-05 20:39:56 -08005346 if (kDebugVerify) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07005347 copy.reset(RegisterLine::Create(target_line->NumRegs(), this));
Ian Rogers7b0c5b42012-02-16 15:29:07 -08005348 copy->CopyFromLine(target_line);
5349 }
Ian Rogers7b078e82014-09-10 14:44:24 -07005350 changed = target_line->MergeRegisters(this, merge_line);
Ian Rogersad0b3a32012-04-16 14:50:24 -07005351 if (have_pending_hard_failure_) {
Ian Rogersd81871c2011-10-03 13:57:23 -07005352 return false;
jeffhaobdb76512011-09-07 11:43:16 -07005353 }
Andreas Gampeec6e6c12015-11-05 20:39:56 -08005354 if (kDebugVerify && changed) {
Elliott Hughes398f64b2012-03-26 18:05:48 -07005355 LogVerifyInfo() << "Merging at [" << reinterpret_cast<void*>(work_insn_idx_) << "]"
Elliott Hughesc073b072012-05-24 19:29:17 -07005356 << " to [" << reinterpret_cast<void*>(next_insn) << "]: " << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07005357 << copy->Dump(this) << " MERGE\n"
5358 << merge_line->Dump(this) << " ==\n"
5359 << target_line->Dump(this) << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07005360 }
Ian Rogersebbdd872014-07-07 23:53:08 -07005361 if (update_merge_line && changed) {
5362 merge_line->CopyFromLine(target_line);
5363 }
jeffhaobdb76512011-09-07 11:43:16 -07005364 }
Ian Rogersd81871c2011-10-03 13:57:23 -07005365 if (changed) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07005366 GetInstructionFlags(next_insn).SetChanged();
jeffhaobdb76512011-09-07 11:43:16 -07005367 }
5368 return true;
5369}
5370
Ian Rogers7b3ddd22013-02-21 15:19:52 -08005371InstructionFlags* MethodVerifier::CurrentInsnFlags() {
Mathieu Chartierde40d472015-10-15 17:47:48 -07005372 return &GetInstructionFlags(work_insn_idx_);
Ian Rogers776ac1f2012-04-13 23:36:36 -07005373}
5374
Ian Rogersd8f69b02014-09-10 21:43:52 +00005375const RegType& MethodVerifier::GetMethodReturnType() {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07005376 if (return_type_ == nullptr) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07005377 if (mirror_method_ != nullptr) {
Vladimir Markob45528c2017-07-27 14:14:28 +01005378 ObjPtr<mirror::Class> return_type_class = can_load_classes_
5379 ? mirror_method_->ResolveReturnType()
5380 : mirror_method_->LookupResolvedReturnType();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07005381 if (return_type_class != nullptr) {
Andreas Gampef23f33d2015-06-23 14:18:17 -07005382 return_type_ = &FromClass(mirror_method_->GetReturnTypeDescriptor(),
Vladimir Markob45528c2017-07-27 14:14:28 +01005383 return_type_class.Ptr(),
Andreas Gampef23f33d2015-06-23 14:18:17 -07005384 return_type_class->CannotBeAssignedFromOtherTypes());
Ian Rogersfc0e94b2013-09-23 23:51:32 -07005385 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07005386 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
5387 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07005388 }
5389 }
5390 if (return_type_ == nullptr) {
5391 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
5392 const DexFile::ProtoId& proto_id = dex_file_->GetMethodPrototype(method_id);
Andreas Gampea5b09a62016-11-17 15:21:22 -08005393 dex::TypeIndex return_type_idx = proto_id.return_type_idx_;
Ian Rogersfc0e94b2013-09-23 23:51:32 -07005394 const char* descriptor = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(return_type_idx));
Mathieu Chartierbf99f772014-08-23 16:37:27 -07005395 return_type_ = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07005396 }
5397 }
5398 return *return_type_;
Ian Rogersad0b3a32012-04-16 14:50:24 -07005399}
5400
Ian Rogersd8f69b02014-09-10 21:43:52 +00005401const RegType& MethodVerifier::GetDeclaringClass() {
Ian Rogers7b078e82014-09-10 14:44:24 -07005402 if (declaring_class_ == nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08005403 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
Brian Carlstrom93c33962013-07-26 10:37:43 -07005404 const char* descriptor
5405 = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(method_id.class_idx_));
Mathieu Chartiere401d142015-04-22 13:56:20 -07005406 if (mirror_method_ != nullptr) {
Ian Rogers637c65b2013-05-31 11:46:00 -07005407 mirror::Class* klass = mirror_method_->GetDeclaringClass();
Mathieu Chartierde40d472015-10-15 17:47:48 -07005408 declaring_class_ = &FromClass(descriptor, klass, klass->CannotBeAssignedFromOtherTypes());
Ian Rogers637c65b2013-05-31 11:46:00 -07005409 } else {
Mathieu Chartierbf99f772014-08-23 16:37:27 -07005410 declaring_class_ = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers637c65b2013-05-31 11:46:00 -07005411 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07005412 }
Ian Rogers637c65b2013-05-31 11:46:00 -07005413 return *declaring_class_;
Ian Rogersad0b3a32012-04-16 14:50:24 -07005414}
5415
Ian Rogers2bcb4a42012-11-08 10:39:18 -08005416std::vector<int32_t> MethodVerifier::DescribeVRegs(uint32_t dex_pc) {
5417 RegisterLine* line = reg_table_.GetLine(dex_pc);
Sebastien Hertzaa0c00c2014-03-14 17:58:54 +01005418 DCHECK(line != nullptr) << "No register line at DEX pc " << StringPrintf("0x%x", dex_pc);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08005419 std::vector<int32_t> result;
5420 for (size_t i = 0; i < line->NumRegs(); ++i) {
Ian Rogers7b078e82014-09-10 14:44:24 -07005421 const RegType& type = line->GetRegisterType(this, i);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08005422 if (type.IsConstant()) {
5423 result.push_back(type.IsPreciseConstant() ? kConstant : kImpreciseConstant);
Ian Rogers7b078e82014-09-10 14:44:24 -07005424 const ConstantType* const_val = down_cast<const ConstantType*>(&type);
5425 result.push_back(const_val->ConstantValue());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08005426 } else if (type.IsConstantLo()) {
5427 result.push_back(type.IsPreciseConstantLo() ? kConstant : kImpreciseConstant);
Ian Rogers7b078e82014-09-10 14:44:24 -07005428 const ConstantType* const_val = down_cast<const ConstantType*>(&type);
5429 result.push_back(const_val->ConstantValueLo());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08005430 } else if (type.IsConstantHi()) {
5431 result.push_back(type.IsPreciseConstantHi() ? kConstant : kImpreciseConstant);
Ian Rogers7b078e82014-09-10 14:44:24 -07005432 const ConstantType* const_val = down_cast<const ConstantType*>(&type);
5433 result.push_back(const_val->ConstantValueHi());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08005434 } else if (type.IsIntegralTypes()) {
5435 result.push_back(kIntVReg);
5436 result.push_back(0);
5437 } else if (type.IsFloat()) {
5438 result.push_back(kFloatVReg);
5439 result.push_back(0);
5440 } else if (type.IsLong()) {
5441 result.push_back(kLongLoVReg);
5442 result.push_back(0);
5443 result.push_back(kLongHiVReg);
5444 result.push_back(0);
5445 ++i;
5446 } else if (type.IsDouble()) {
5447 result.push_back(kDoubleLoVReg);
5448 result.push_back(0);
5449 result.push_back(kDoubleHiVReg);
5450 result.push_back(0);
5451 ++i;
5452 } else if (type.IsUndefined() || type.IsConflict() || type.IsHighHalf()) {
5453 result.push_back(kUndefined);
5454 result.push_back(0);
5455 } else {
Ian Rogers7b3ddd22013-02-21 15:19:52 -08005456 CHECK(type.IsNonZeroReferenceTypes());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08005457 result.push_back(kReferenceVReg);
5458 result.push_back(0);
5459 }
5460 }
5461 return result;
5462}
5463
Ian Rogersd8f69b02014-09-10 21:43:52 +00005464const RegType& MethodVerifier::DetermineCat1Constant(int32_t value, bool precise) {
Sebastien Hertz849600b2013-12-20 10:28:08 +01005465 if (precise) {
5466 // Precise constant type.
5467 return reg_types_.FromCat1Const(value, true);
5468 } else {
5469 // Imprecise constant type.
5470 if (value < -32768) {
5471 return reg_types_.IntConstant();
5472 } else if (value < -128) {
5473 return reg_types_.ShortConstant();
5474 } else if (value < 0) {
5475 return reg_types_.ByteConstant();
5476 } else if (value == 0) {
5477 return reg_types_.Zero();
5478 } else if (value == 1) {
5479 return reg_types_.One();
5480 } else if (value < 128) {
5481 return reg_types_.PosByteConstant();
5482 } else if (value < 32768) {
5483 return reg_types_.PosShortConstant();
5484 } else if (value < 65536) {
5485 return reg_types_.CharConstant();
5486 } else {
5487 return reg_types_.IntConstant();
5488 }
5489 }
5490}
5491
Elliott Hughes0a1038b2012-06-14 16:24:17 -07005492void MethodVerifier::Init() {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08005493 art::verifier::RegTypeCache::Init();
Brian Carlstrome7d856b2012-01-11 18:10:55 -08005494}
5495
Elliott Hughes0a1038b2012-06-14 16:24:17 -07005496void MethodVerifier::Shutdown() {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08005497 verifier::RegTypeCache::ShutDown();
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08005498}
jeffhaod1224c72012-02-29 13:43:08 -08005499
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -07005500void MethodVerifier::VisitStaticRoots(RootVisitor* visitor) {
5501 RegTypeCache::VisitStaticRoots(visitor);
Mathieu Chartier7c438b12014-09-12 17:01:24 -07005502}
5503
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -07005504void MethodVerifier::VisitRoots(RootVisitor* visitor, const RootInfo& root_info) {
5505 reg_types_.VisitRoots(visitor, root_info);
Mathieu Chartierc528dba2013-11-26 12:00:11 -08005506}
5507
Andreas Gampef23f33d2015-06-23 14:18:17 -07005508const RegType& MethodVerifier::FromClass(const char* descriptor,
5509 mirror::Class* klass,
5510 bool precise) {
5511 DCHECK(klass != nullptr);
5512 if (precise && !klass->IsInstantiable() && !klass->IsPrimitive()) {
5513 Fail(VerifyError::VERIFY_ERROR_NO_CLASS) << "Could not create precise reference for "
5514 << "non-instantiable klass " << descriptor;
5515 precise = false;
5516 }
5517 return reg_types_.FromClass(descriptor, klass, precise);
5518}
5519
Ian Rogersd81871c2011-10-03 13:57:23 -07005520} // namespace verifier
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07005521} // namespace art