blob: c6ba2f7e43be16c33c228e78c12c3778582b546a [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 Lightd9aff132017-10-31 22:30:05 +0000556 reg_types_(can_load_classes, allocator_, allow_thread_suspension),
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),
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -0800567 code_item_accessor_(CodeItemDataAccessor::CreateNullable(dex_file, 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
Andreas Gampeaaf0d382017-11-27 14:10:21 -0800595void MethodVerifier::FindLocksAtDexPc(
596 ArtMethod* m,
597 uint32_t dex_pc,
598 std::vector<MethodVerifier::DexLockInfo>* monitor_enter_dex_pcs) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700599 StackHandleScope<2> hs(Thread::Current());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700600 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache()));
601 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader()));
Andreas Gampebf9611f2016-03-25 16:58:00 -0700602 MethodVerifier verifier(hs.Self(),
603 m->GetDexFile(),
604 dex_cache,
605 class_loader,
David Brazdil15fc7292016-09-02 14:13:18 +0100606 m->GetClassDef(),
Andreas Gampebf9611f2016-03-25 16:58:00 -0700607 m->GetCodeItem(),
608 m->GetDexMethodIndex(),
609 m,
610 m->GetAccessFlags(),
611 false /* can_load_classes */,
612 true /* allow_soft_failures */,
613 false /* need_precise_constants */,
614 false /* verify_to_dump */,
615 false /* allow_thread_suspension */);
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700616 verifier.interesting_dex_pc_ = dex_pc;
Ian Rogers46960fe2014-05-23 10:43:43 -0700617 verifier.monitor_enter_dex_pcs_ = monitor_enter_dex_pcs;
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700618 verifier.FindLocksAtDexPc();
619}
620
621void MethodVerifier::FindLocksAtDexPc() {
Ian Rogers7b078e82014-09-10 14:44:24 -0700622 CHECK(monitor_enter_dex_pcs_ != nullptr);
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -0800623 CHECK(code_item_accessor_.HasCodeItem()); // This only makes sense for methods with code.
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700624
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -0800625 // Quick check whether there are any monitor_enter instructions before verifying.
626 for (const DexInstructionPcPair& inst : code_item_accessor_) {
627 if (inst->Opcode() == Instruction::MONITOR_ENTER) {
628 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
629 // verification. In practice, the phase we want relies on data structures set up by all the
630 // earlier passes, so we just run the full method verification and bail out early when we've
631 // got what we wanted.
632 Verify();
633 return;
634 }
Andreas Gampecb3c08f2014-09-18 13:16:38 -0700635 }
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700636}
637
Mathieu Chartiere401d142015-04-22 13:56:20 -0700638ArtField* MethodVerifier::FindAccessedFieldAtDexPc(ArtMethod* m, uint32_t dex_pc) {
639 StackHandleScope<2> hs(Thread::Current());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700640 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache()));
641 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader()));
Andreas Gampebf9611f2016-03-25 16:58:00 -0700642 MethodVerifier verifier(hs.Self(),
643 m->GetDexFile(),
644 dex_cache,
645 class_loader,
David Brazdil15fc7292016-09-02 14:13:18 +0100646 m->GetClassDef(),
Andreas Gampebf9611f2016-03-25 16:58:00 -0700647 m->GetCodeItem(),
648 m->GetDexMethodIndex(),
649 m,
650 m->GetAccessFlags(),
651 true /* can_load_classes */,
652 true /* allow_soft_failures */,
653 false /* need_precise_constants */,
654 false /* verify_to_dump */,
655 true /* allow_thread_suspension */);
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200656 return verifier.FindAccessedFieldAtDexPc(dex_pc);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200657}
658
Mathieu Chartierc7853442015-03-27 14:35:38 -0700659ArtField* MethodVerifier::FindAccessedFieldAtDexPc(uint32_t dex_pc) {
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -0800660 CHECK(code_item_accessor_.HasCodeItem()); // This only makes sense for methods with code.
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200661
662 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
663 // verification. In practice, the phase we want relies on data structures set up by all the
664 // earlier passes, so we just run the full method verification and bail out early when we've
665 // got what we wanted.
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200666 bool success = Verify();
667 if (!success) {
Ian Rogers9bc54402014-04-17 16:40:01 -0700668 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200669 }
670 RegisterLine* register_line = reg_table_.GetLine(dex_pc);
Ian Rogers7b078e82014-09-10 14:44:24 -0700671 if (register_line == nullptr) {
Ian Rogers9bc54402014-04-17 16:40:01 -0700672 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200673 }
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -0800674 const Instruction* inst = &code_item_accessor_.InstructionAt(dex_pc);
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200675 return GetQuickFieldAccess(inst, register_line);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200676}
677
Mathieu Chartiere401d142015-04-22 13:56:20 -0700678ArtMethod* MethodVerifier::FindInvokedMethodAtDexPc(ArtMethod* m, uint32_t dex_pc) {
679 StackHandleScope<2> hs(Thread::Current());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700680 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache()));
681 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader()));
Andreas Gampebf9611f2016-03-25 16:58:00 -0700682 MethodVerifier verifier(hs.Self(),
683 m->GetDexFile(),
684 dex_cache,
685 class_loader,
David Brazdil15fc7292016-09-02 14:13:18 +0100686 m->GetClassDef(),
Andreas Gampebf9611f2016-03-25 16:58:00 -0700687 m->GetCodeItem(),
688 m->GetDexMethodIndex(),
689 m,
690 m->GetAccessFlags(),
691 true /* can_load_classes */,
692 true /* allow_soft_failures */,
693 false /* need_precise_constants */,
694 false /* verify_to_dump */,
695 true /* allow_thread_suspension */);
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200696 return verifier.FindInvokedMethodAtDexPc(dex_pc);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200697}
698
Mathieu Chartiere401d142015-04-22 13:56:20 -0700699ArtMethod* MethodVerifier::FindInvokedMethodAtDexPc(uint32_t dex_pc) {
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -0800700 CHECK(code_item_accessor_.HasCodeItem()); // This only makes sense for methods with code.
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200701
702 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
703 // verification. In practice, the phase we want relies on data structures set up by all the
704 // earlier passes, so we just run the full method verification and bail out early when we've
705 // got what we wanted.
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200706 bool success = Verify();
707 if (!success) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700708 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200709 }
710 RegisterLine* register_line = reg_table_.GetLine(dex_pc);
Ian Rogers7b078e82014-09-10 14:44:24 -0700711 if (register_line == nullptr) {
712 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200713 }
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -0800714 const Instruction* inst = &code_item_accessor_.InstructionAt(dex_pc);
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200715 const bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
Mathieu Chartier091d2382015-03-06 10:59:06 -0800716 return GetQuickInvokedMethod(inst, register_line, is_range, false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200717}
718
Ian Rogersad0b3a32012-04-16 14:50:24 -0700719bool MethodVerifier::Verify() {
Andreas Gampee6215c02015-08-31 18:54:38 -0700720 // Some older code doesn't correctly mark constructors as such. Test for this case by looking at
721 // the name.
722 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
723 const char* method_name = dex_file_->StringDataByIdx(method_id.name_idx_);
724 bool instance_constructor_by_name = strcmp("<init>", method_name) == 0;
725 bool static_constructor_by_name = strcmp("<clinit>", method_name) == 0;
726 bool constructor_by_name = instance_constructor_by_name || static_constructor_by_name;
727 // Check that only constructors are tagged, and check for bad code that doesn't tag constructors.
728 if ((method_access_flags_ & kAccConstructor) != 0) {
729 if (!constructor_by_name) {
730 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
731 << "method is marked as constructor, but not named accordingly";
jeffhaobdb76512011-09-07 11:43:16 -0700732 return false;
Andreas Gampee6215c02015-08-31 18:54:38 -0700733 }
734 is_constructor_ = true;
735 } else if (constructor_by_name) {
David Sehr709b0702016-10-13 09:12:37 -0700736 LOG(WARNING) << "Method " << dex_file_->PrettyMethod(dex_method_idx_)
Andreas Gampee6215c02015-08-31 18:54:38 -0700737 << " not marked as constructor.";
738 is_constructor_ = true;
739 }
740 // If it's a constructor, check whether IsStatic() matches the name.
741 // This should have been rejected by the dex file verifier. Only do in debug build.
742 if (kIsDebugBuild) {
743 if (IsConstructor()) {
744 if (IsStatic() ^ static_constructor_by_name) {
745 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
746 << "constructor name doesn't match static flag";
747 return false;
748 }
jeffhaobdb76512011-09-07 11:43:16 -0700749 }
jeffhaobdb76512011-09-07 11:43:16 -0700750 }
Andreas Gampee6215c02015-08-31 18:54:38 -0700751
752 // Methods may only have one of public/protected/private.
753 // This should have been rejected by the dex file verifier. Only do in debug build.
754 if (kIsDebugBuild) {
755 size_t access_mod_count =
756 (((method_access_flags_ & kAccPublic) == 0) ? 0 : 1) +
757 (((method_access_flags_ & kAccProtected) == 0) ? 0 : 1) +
758 (((method_access_flags_ & kAccPrivate) == 0) ? 0 : 1);
759 if (access_mod_count > 1) {
760 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "method has more than one of public/protected/private";
761 return false;
762 }
763 }
764
765 // If there aren't any instructions, make sure that's expected, then exit successfully.
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -0800766 if (!code_item_accessor_.HasCodeItem()) {
Andreas Gampe76ed99d2016-03-28 18:31:29 -0700767 // Only native or abstract methods may not have code.
768 if ((method_access_flags_ & (kAccNative | kAccAbstract)) == 0) {
769 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "zero-length code in concrete non-native method";
770 return false;
771 }
772
Andreas Gampee6215c02015-08-31 18:54:38 -0700773 // This should have been rejected by the dex file verifier. Only do in debug build.
Andreas Gampe76ed99d2016-03-28 18:31:29 -0700774 // Note: the above will also be rejected in the dex file verifier, starting in dex version 37.
Andreas Gampee6215c02015-08-31 18:54:38 -0700775 if (kIsDebugBuild) {
Andreas Gampee6215c02015-08-31 18:54:38 -0700776 if ((method_access_flags_ & kAccAbstract) != 0) {
777 // Abstract methods are not allowed to have the following flags.
778 static constexpr uint32_t kForbidden =
779 kAccPrivate |
780 kAccStatic |
781 kAccFinal |
782 kAccNative |
783 kAccStrict |
784 kAccSynchronized;
785 if ((method_access_flags_ & kForbidden) != 0) {
786 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
787 << "method can't be abstract and private/static/final/native/strict/synchronized";
788 return false;
789 }
790 }
David Brazdil15fc7292016-09-02 14:13:18 +0100791 if ((class_def_.GetJavaAccessFlags() & kAccInterface) != 0) {
Alex Lighteb7c1442015-08-31 13:17:42 -0700792 // Interface methods must be public and abstract (if default methods are disabled).
Neil Fuller9724c632016-01-07 15:42:47 +0000793 uint32_t kRequired = kAccPublic;
Alex Lighteb7c1442015-08-31 13:17:42 -0700794 if ((method_access_flags_ & kRequired) != kRequired) {
Neil Fuller9724c632016-01-07 15:42:47 +0000795 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface methods must be public";
Andreas Gampee6215c02015-08-31 18:54:38 -0700796 return false;
797 }
798 // In addition to the above, interface methods must not be protected.
799 static constexpr uint32_t kForbidden = kAccProtected;
800 if ((method_access_flags_ & kForbidden) != 0) {
801 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface methods can't be protected";
802 return false;
803 }
804 }
805 // We also don't allow constructors to be abstract or native.
806 if (IsConstructor()) {
807 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "constructors can't be abstract or native";
808 return false;
809 }
810 }
811 return true;
812 }
813
814 // This should have been rejected by the dex file verifier. Only do in debug build.
815 if (kIsDebugBuild) {
816 // When there's code, the method must not be native or abstract.
817 if ((method_access_flags_ & (kAccNative | kAccAbstract)) != 0) {
818 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "non-zero-length code in abstract or native method";
819 return false;
820 }
821
David Brazdil15fc7292016-09-02 14:13:18 +0100822 if ((class_def_.GetJavaAccessFlags() & kAccInterface) != 0) {
Alex Lighteb7c1442015-08-31 13:17:42 -0700823 // Interfaces may always have static initializers for their fields. If we are running with
824 // default methods enabled we also allow other public, static, non-final methods to have code.
825 // Otherwise that is the only type of method allowed.
Alex Light0db36b32015-10-27 14:06:34 -0700826 if (!(IsConstructor() && IsStatic())) {
Neil Fuller9724c632016-01-07 15:42:47 +0000827 if (IsInstanceConstructor()) {
828 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interfaces may not have non-static constructor";
829 return false;
830 } else if (method_access_flags_ & kAccFinal) {
831 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interfaces may not have final methods";
832 return false;
Alex Lightb55f1ac2016-04-12 15:50:55 -0700833 } else {
834 uint32_t access_flag_options = kAccPublic;
835 if (dex_file_->GetVersion() >= DexFile::kDefaultMethodsVersion) {
836 access_flag_options |= kAccPrivate;
837 }
838 if (!(method_access_flags_ & access_flag_options)) {
839 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
840 << "interfaces may not have protected or package-private members";
841 return false;
842 }
Alex Lighteb7c1442015-08-31 13:17:42 -0700843 }
Andreas Gampee6215c02015-08-31 18:54:38 -0700844 }
845 }
846
847 // Instance constructors must not be synchronized.
848 if (IsInstanceConstructor()) {
849 static constexpr uint32_t kForbidden = kAccSynchronized;
850 if ((method_access_flags_ & kForbidden) != 0) {
851 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "constructors can't be synchronized";
852 return false;
853 }
854 }
855 }
856
Ian Rogersd81871c2011-10-03 13:57:23 -0700857 // Sanity-check the register counts. ins + locals = registers, so make sure that ins <= registers.
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -0800858 if (code_item_accessor_.InsSize() > code_item_accessor_.RegistersSize()) {
859 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad register counts (ins="
860 << code_item_accessor_.InsSize()
861 << " regs=" << code_item_accessor_.RegistersSize();
Ian Rogersd81871c2011-10-03 13:57:23 -0700862 return false;
jeffhaobdb76512011-09-07 11:43:16 -0700863 }
Alex Lighteb7c1442015-08-31 13:17:42 -0700864
Ian Rogersd81871c2011-10-03 13:57:23 -0700865 // Allocate and initialize an array to hold instruction data.
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -0800866 insn_flags_.reset(allocator_.AllocArray<InstructionFlags>(
867 code_item_accessor_.InsnsSizeInCodeUnits()));
Mathieu Chartierde40d472015-10-15 17:47:48 -0700868 DCHECK(insn_flags_ != nullptr);
869 std::uninitialized_fill_n(insn_flags_.get(),
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -0800870 code_item_accessor_.InsnsSizeInCodeUnits(),
Mathieu Chartierde40d472015-10-15 17:47:48 -0700871 InstructionFlags());
Ian Rogersd81871c2011-10-03 13:57:23 -0700872 // Run through the instructions and see if the width checks out.
873 bool result = ComputeWidthsAndCountOps();
Andreas Gampebf1cb772017-05-15 15:39:00 -0700874 bool allow_runtime_only_instructions = !Runtime::Current()->IsAotCompiler() || verify_to_dump_;
Ian Rogersd81871c2011-10-03 13:57:23 -0700875 // Flag instructions guarded by a "try" block and check exception handlers.
876 result = result && ScanTryCatchBlocks();
877 // Perform static instruction verification.
Andreas Gampebf1cb772017-05-15 15:39:00 -0700878 result = result && (allow_runtime_only_instructions
879 ? VerifyInstructions<true>()
880 : VerifyInstructions<false>());
Ian Rogersad0b3a32012-04-16 14:50:24 -0700881 // Perform code-flow analysis and return.
Vladimir Marko2b5eaa22013-12-13 13:59:30 +0000882 result = result && VerifyCodeFlow();
Andreas Gampe53e32d12015-12-09 21:03:23 -0800883
Vladimir Marko2b5eaa22013-12-13 13:59:30 +0000884 return result;
jeffhaoba5ebb92011-08-25 17:24:37 -0700885}
886
Ian Rogers776ac1f2012-04-13 23:36:36 -0700887std::ostream& MethodVerifier::Fail(VerifyError error) {
Andreas Gampe0760a812015-08-26 17:12:51 -0700888 // Mark the error type as encountered.
Andreas Gampea727e372015-08-25 09:22:37 -0700889 encountered_failure_types_ |= static_cast<uint32_t>(error);
Andreas Gampe0760a812015-08-26 17:12:51 -0700890
Ian Rogersad0b3a32012-04-16 14:50:24 -0700891 switch (error) {
892 case VERIFY_ERROR_NO_CLASS:
893 case VERIFY_ERROR_NO_FIELD:
894 case VERIFY_ERROR_NO_METHOD:
895 case VERIFY_ERROR_ACCESS_CLASS:
896 case VERIFY_ERROR_ACCESS_FIELD:
897 case VERIFY_ERROR_ACCESS_METHOD:
Ian Rogers08f753d2012-08-24 14:35:25 -0700898 case VERIFY_ERROR_INSTANTIATION:
899 case VERIFY_ERROR_CLASS_CHANGE:
Igor Murashkin158f35c2015-06-10 15:55:30 -0700900 case VERIFY_ERROR_FORCE_INTERPRETER:
Andreas Gampea727e372015-08-25 09:22:37 -0700901 case VERIFY_ERROR_LOCKING:
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800902 if (Runtime::Current()->IsAotCompiler() || !can_load_classes_) {
jeffhaofaf459e2012-08-31 15:32:47 -0700903 // If we're optimistically running verification at compile time, turn NO_xxx, ACCESS_xxx,
904 // class change and instantiation errors into soft verification errors so that we re-verify
905 // at runtime. We may fail to find or to agree on access because of not yet available class
906 // loaders, or class loaders that will differ at runtime. In these cases, we don't want to
907 // affect the soundness of the code being compiled. Instead, the generated code runs "slow
908 // paths" that dynamically perform the verification and cause the behavior to be that akin
909 // to an interpreter.
910 error = VERIFY_ERROR_BAD_CLASS_SOFT;
911 } else {
Jeff Haoa3faaf42013-09-03 19:07:00 -0700912 // If we fail again at runtime, mark that this instruction would throw and force this
913 // method to be executed using the interpreter with checks.
jeffhaofaf459e2012-08-31 15:32:47 -0700914 have_pending_runtime_throw_failure_ = true;
Andreas Gamped7f8d052015-03-12 11:05:47 -0700915
916 // We need to save the work_line if the instruction wasn't throwing before. Otherwise we'll
917 // try to merge garbage.
918 // Note: this assumes that Fail is called before we do any work_line modifications.
Andreas Gamped5ad72f2015-06-26 17:33:47 -0700919 // Note: this can fail before we touch any instruction, for the signature of a method. So
920 // add a check.
Andreas Gampee2abbc62017-09-15 11:59:26 -0700921 if (work_insn_idx_ < dex::kDexNoIndex) {
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -0800922 const Instruction& inst = code_item_accessor_.InstructionAt(work_insn_idx_);
Mathieu Chartier1d2d4ff2017-09-23 16:11:06 -0700923 int opcode_flags = Instruction::FlagsOf(inst.Opcode());
Andreas Gamped7f8d052015-03-12 11:05:47 -0700924
Andreas Gamped5ad72f2015-06-26 17:33:47 -0700925 if ((opcode_flags & Instruction::kThrow) == 0 && CurrentInsnFlags()->IsInTry()) {
926 saved_line_->CopyFromLine(work_line_.get());
927 }
Andreas Gamped7f8d052015-03-12 11:05:47 -0700928 }
jeffhaofaf459e2012-08-31 15:32:47 -0700929 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700930 break;
Andreas Gampea727e372015-08-25 09:22:37 -0700931
Ian Rogersad0b3a32012-04-16 14:50:24 -0700932 // Indication that verification should be retried at runtime.
933 case VERIFY_ERROR_BAD_CLASS_SOFT:
Jeff Haoee988952013-04-16 14:23:47 -0700934 if (!allow_soft_failures_) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700935 have_pending_hard_failure_ = true;
936 }
937 break;
Andreas Gampea727e372015-08-25 09:22:37 -0700938
jeffhaod5347e02012-03-22 17:25:05 -0700939 // Hard verification failures at compile time will still fail at runtime, so the class is
940 // marked as rejected to prevent it from being compiled.
Ian Rogersad0b3a32012-04-16 14:50:24 -0700941 case VERIFY_ERROR_BAD_CLASS_HARD: {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700942 have_pending_hard_failure_ = true;
Andreas Gampeebf850c2015-08-14 15:37:35 -0700943 if (VLOG_IS_ON(verifier) && kDumpRegLinesOnHardFailureIfVLOG) {
944 ScopedObjectAccess soa(Thread::Current());
945 std::ostringstream oss;
946 Dump(oss);
947 LOG(ERROR) << oss.str();
948 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700949 break;
Ian Rogers47a05882012-02-03 12:23:33 -0800950 }
951 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700952 failures_.push_back(error);
David Sehr709b0702016-10-13 09:12:37 -0700953 std::string location(StringPrintf("%s: [0x%X] ", dex_file_->PrettyMethod(dex_method_idx_).c_str(),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700954 work_insn_idx_));
Elena Sayapina78480ec2014-08-15 15:52:42 +0700955 std::ostringstream* failure_message = new std::ostringstream(location, std::ostringstream::ate);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700956 failure_messages_.push_back(failure_message);
957 return *failure_message;
958}
959
Ian Rogers576ca0c2014-06-06 15:58:22 -0700960std::ostream& MethodVerifier::LogVerifyInfo() {
David Sehr709b0702016-10-13 09:12:37 -0700961 return info_messages_ << "VFY: " << dex_file_->PrettyMethod(dex_method_idx_)
Ian Rogers576ca0c2014-06-06 15:58:22 -0700962 << '[' << reinterpret_cast<void*>(work_insn_idx_) << "] : ";
963}
964
Ian Rogersad0b3a32012-04-16 14:50:24 -0700965void MethodVerifier::PrependToLastFailMessage(std::string prepend) {
966 size_t failure_num = failure_messages_.size();
967 DCHECK_NE(failure_num, 0U);
968 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
969 prepend += last_fail_message->str();
Elena Sayapina78480ec2014-08-15 15:52:42 +0700970 failure_messages_[failure_num - 1] = new std::ostringstream(prepend, std::ostringstream::ate);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700971 delete last_fail_message;
972}
973
Vladimir Marko5c657fe2016-11-03 15:12:29 +0000974void MethodVerifier::AppendToLastFailMessage(const std::string& append) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700975 size_t failure_num = failure_messages_.size();
976 DCHECK_NE(failure_num, 0U);
977 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
978 (*last_fail_message) << append;
Ian Rogers47a05882012-02-03 12:23:33 -0800979}
980
Ian Rogers776ac1f2012-04-13 23:36:36 -0700981bool MethodVerifier::ComputeWidthsAndCountOps() {
jeffhaobdb76512011-09-07 11:43:16 -0700982 size_t new_instance_count = 0;
983 size_t monitor_enter_count = 0;
jeffhaobdb76512011-09-07 11:43:16 -0700984
Mathieu Chartieraf7c9022017-10-27 09:42:46 -0700985 // We can't assume the instruction is well formed, handle the case where calculating the size
986 // goes past the end of the code item.
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -0800987 SafeDexInstructionIterator it(code_item_accessor_.begin(), code_item_accessor_.end());
988 for ( ; !it.IsErrorState() && it < code_item_accessor_.end(); ++it) {
Mathieu Chartieraf7c9022017-10-27 09:42:46 -0700989 // In case the instruction goes past the end of the code item, make sure to not process it.
990 SafeDexInstructionIterator next = it;
991 ++next;
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -0800992 if (next.IsErrorState()) {
Mathieu Chartieraf7c9022017-10-27 09:42:46 -0700993 break;
994 }
995 Instruction::Code opcode = it->Opcode();
Ian Rogersa9a82542013-10-04 11:17:26 -0700996 switch (opcode) {
997 case Instruction::APUT_OBJECT:
998 case Instruction::CHECK_CAST:
999 has_check_casts_ = true;
1000 break;
1001 case Instruction::INVOKE_VIRTUAL:
1002 case Instruction::INVOKE_VIRTUAL_RANGE:
1003 case Instruction::INVOKE_INTERFACE:
1004 case Instruction::INVOKE_INTERFACE_RANGE:
1005 has_virtual_or_interface_invokes_ = true;
1006 break;
1007 case Instruction::MONITOR_ENTER:
1008 monitor_enter_count++;
1009 break;
1010 case Instruction::NEW_INSTANCE:
1011 new_instance_count++;
1012 break;
1013 default:
1014 break;
jeffhaobdb76512011-09-07 11:43:16 -07001015 }
Mathieu Chartier2b2bef22017-10-26 17:10:19 -07001016 GetInstructionFlags(it.DexPc()).SetIsOpcode();
jeffhaobdb76512011-09-07 11:43:16 -07001017 }
1018
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001019 if (it != code_item_accessor_.end()) {
1020 const size_t insns_size = code_item_accessor_.InsnsSizeInCodeUnits();
jeffhaod5347e02012-03-22 17:25:05 -07001021 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "code did not end where expected ("
Mathieu Chartier2b2bef22017-10-26 17:10:19 -07001022 << it.DexPc() << " vs. " << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -07001023 return false;
1024 }
1025
Ian Rogersd81871c2011-10-03 13:57:23 -07001026 new_instance_count_ = new_instance_count;
1027 monitor_enter_count_ = monitor_enter_count;
jeffhaobdb76512011-09-07 11:43:16 -07001028 return true;
1029}
1030
Ian Rogers776ac1f2012-04-13 23:36:36 -07001031bool MethodVerifier::ScanTryCatchBlocks() {
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001032 const uint32_t tries_size = code_item_accessor_.TriesSize();
jeffhaobdb76512011-09-07 11:43:16 -07001033 if (tries_size == 0) {
1034 return true;
1035 }
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001036 const uint32_t insns_size = code_item_accessor_.InsnsSizeInCodeUnits();
1037 for (const DexFile::TryItem& try_item : code_item_accessor_.TryItems()) {
1038 const uint32_t start = try_item.start_addr_;
1039 const uint32_t end = start + try_item.insn_count_;
jeffhaobdb76512011-09-07 11:43:16 -07001040 if ((start >= end) || (start >= insns_size) || (end > insns_size)) {
jeffhaod5347e02012-03-22 17:25:05 -07001041 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad exception entry: startAddr=" << start
1042 << " endAddr=" << end << " (size=" << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -07001043 return false;
1044 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07001045 if (!GetInstructionFlags(start).IsOpcode()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001046 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1047 << "'try' block starts inside an instruction (" << start << ")";
jeffhaobdb76512011-09-07 11:43:16 -07001048 return false;
1049 }
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001050 DexInstructionIterator end_it(code_item_accessor_.Insns(), end);
1051 for (DexInstructionIterator it(code_item_accessor_.Insns(), start); it < end_it; ++it) {
1052 GetInstructionFlags(it.DexPc()).SetInTry();
jeffhaobdb76512011-09-07 11:43:16 -07001053 }
1054 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -08001055 // Iterate over each of the handlers to verify target addresses.
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001056 const uint8_t* handlers_ptr = code_item_accessor_.GetCatchHandlerData();
1057 const uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
Ian Rogers28ad40d2011-10-27 15:19:26 -07001058 ClassLinker* linker = Runtime::Current()->GetClassLinker();
jeffhaobdb76512011-09-07 11:43:16 -07001059 for (uint32_t idx = 0; idx < handlers_size; idx++) {
Ian Rogers0571d352011-11-03 19:51:38 -07001060 CatchHandlerIterator iterator(handlers_ptr);
1061 for (; iterator.HasNext(); iterator.Next()) {
Igor Murashkin2ffb7032017-11-08 13:35:21 -08001062 uint32_t dex_pc = iterator.GetHandlerAddress();
Mathieu Chartierde40d472015-10-15 17:47:48 -07001063 if (!GetInstructionFlags(dex_pc).IsOpcode()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001064 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1065 << "exception handler starts at bad address (" << dex_pc << ")";
jeffhaobdb76512011-09-07 11:43:16 -07001066 return false;
1067 }
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001068 if (!CheckNotMoveResult(code_item_accessor_.Insns(), dex_pc)) {
Stephen Kyle9bc61992014-09-22 13:53:15 +01001069 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1070 << "exception handler begins with move-result* (" << dex_pc << ")";
1071 return false;
1072 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07001073 GetInstructionFlags(dex_pc).SetBranchTarget();
Ian Rogers28ad40d2011-10-27 15:19:26 -07001074 // Ensure exception types are resolved so that they don't need resolution to be delivered,
1075 // unresolved exception types will be ignored by exception delivery
Andreas Gampea5b09a62016-11-17 15:21:22 -08001076 if (iterator.GetHandlerTypeIndex().IsValid()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001077 mirror::Class* exception_type = linker->ResolveType(*dex_file_,
1078 iterator.GetHandlerTypeIndex(),
Mathieu Chartierbf99f772014-08-23 16:37:27 -07001079 dex_cache_, class_loader_);
Ian Rogers7b078e82014-09-10 14:44:24 -07001080 if (exception_type == nullptr) {
1081 DCHECK(self_->IsExceptionPending());
1082 self_->ClearException();
Ian Rogers28ad40d2011-10-27 15:19:26 -07001083 }
1084 }
jeffhaobdb76512011-09-07 11:43:16 -07001085 }
Ian Rogers0571d352011-11-03 19:51:38 -07001086 handlers_ptr = iterator.EndDataPointer();
jeffhaobdb76512011-09-07 11:43:16 -07001087 }
jeffhaobdb76512011-09-07 11:43:16 -07001088 return true;
1089}
1090
Andreas Gampebf1cb772017-05-15 15:39:00 -07001091template <bool kAllowRuntimeOnlyInstructions>
Ian Rogers776ac1f2012-04-13 23:36:36 -07001092bool MethodVerifier::VerifyInstructions() {
Ian Rogers0c7abda2012-09-19 13:33:42 -07001093 /* 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 -07001094 GetInstructionFlags(0).SetBranchTarget();
1095 GetInstructionFlags(0).SetCompileTimeInfoPoint();
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001096 for (const DexInstructionPcPair& inst : code_item_accessor_) {
Mathieu Chartier2b2bef22017-10-26 17:10:19 -07001097 const uint32_t dex_pc = inst.DexPc();
1098 if (!VerifyInstruction<kAllowRuntimeOnlyInstructions>(&inst.Inst(), dex_pc)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001099 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -07001100 return false;
1101 }
1102 /* Flag instructions that are garbage collection points */
Sameer Abu Asal02c42232013-04-30 12:09:45 -07001103 // All invoke points are marked as "Throw" points already.
1104 // We are relying on this to also count all the invokes as interesting.
Vladimir Marko8b858e12014-11-27 14:52:37 +00001105 if (inst->IsBranch()) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07001106 GetInstructionFlags(dex_pc).SetCompileTimeInfoPoint();
Vladimir Marko8b858e12014-11-27 14:52:37 +00001107 // The compiler also needs safepoints for fall-through to loop heads.
1108 // Such a loop head must be a target of a branch.
1109 int32_t offset = 0;
1110 bool cond, self_ok;
1111 bool target_ok = GetBranchOffset(dex_pc, &offset, &cond, &self_ok);
1112 DCHECK(target_ok);
Mathieu Chartierde40d472015-10-15 17:47:48 -07001113 GetInstructionFlags(dex_pc + offset).SetCompileTimeInfoPoint();
Vladimir Marko8b858e12014-11-27 14:52:37 +00001114 } else if (inst->IsSwitch() || inst->IsThrow()) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07001115 GetInstructionFlags(dex_pc).SetCompileTimeInfoPoint();
Ian Rogersb8c78592013-07-25 23:52:52 +00001116 } else if (inst->IsReturn()) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07001117 GetInstructionFlags(dex_pc).SetCompileTimeInfoPointAndReturn();
Ian Rogersd81871c2011-10-03 13:57:23 -07001118 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001119 }
1120 return true;
1121}
1122
Andreas Gampebf1cb772017-05-15 15:39:00 -07001123template <bool kAllowRuntimeOnlyInstructions>
1124bool MethodVerifier::VerifyInstruction(const Instruction* inst, uint32_t code_offset) {
Andreas Gampee05cc662017-05-15 10:17:30 -07001125 if (Instruction::kHaveExperimentalInstructions && UNLIKELY(inst->IsExperimental())) {
Igor Murashkin4d7b75f2015-07-21 17:03:36 -07001126 // Experimental instructions don't yet have verifier support implementation.
1127 // While it is possible to use them by themselves, when we try to use stable instructions
1128 // with a virtual register that was created by an experimental instruction,
1129 // the data flow analysis will fail.
1130 Fail(VERIFY_ERROR_FORCE_INTERPRETER)
1131 << "experimental instruction is not supported by verifier; skipping verification";
1132 have_pending_experimental_failure_ = true;
1133 return false;
1134 }
1135
Ian Rogersd81871c2011-10-03 13:57:23 -07001136 bool result = true;
1137 switch (inst->GetVerifyTypeArgumentA()) {
1138 case Instruction::kVerifyRegA:
Ian Rogers29a26482014-05-02 15:27:29 -07001139 result = result && CheckRegisterIndex(inst->VRegA());
Ian Rogersd81871c2011-10-03 13:57:23 -07001140 break;
1141 case Instruction::kVerifyRegAWide:
Ian Rogers29a26482014-05-02 15:27:29 -07001142 result = result && CheckWideRegisterIndex(inst->VRegA());
Ian Rogersd81871c2011-10-03 13:57:23 -07001143 break;
1144 }
1145 switch (inst->GetVerifyTypeArgumentB()) {
1146 case Instruction::kVerifyRegB:
Ian Rogers29a26482014-05-02 15:27:29 -07001147 result = result && CheckRegisterIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -07001148 break;
1149 case Instruction::kVerifyRegBField:
Ian Rogers29a26482014-05-02 15:27:29 -07001150 result = result && CheckFieldIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -07001151 break;
1152 case Instruction::kVerifyRegBMethod:
Ian Rogers29a26482014-05-02 15:27:29 -07001153 result = result && CheckMethodIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -07001154 break;
1155 case Instruction::kVerifyRegBNewInstance:
Andreas Gampea5b09a62016-11-17 15:21:22 -08001156 result = result && CheckNewInstance(dex::TypeIndex(inst->VRegB()));
Ian Rogersd81871c2011-10-03 13:57:23 -07001157 break;
1158 case Instruction::kVerifyRegBString:
Ian Rogers29a26482014-05-02 15:27:29 -07001159 result = result && CheckStringIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -07001160 break;
1161 case Instruction::kVerifyRegBType:
Andreas Gampea5b09a62016-11-17 15:21:22 -08001162 result = result && CheckTypeIndex(dex::TypeIndex(inst->VRegB()));
Ian Rogersd81871c2011-10-03 13:57:23 -07001163 break;
1164 case Instruction::kVerifyRegBWide:
Ian Rogers29a26482014-05-02 15:27:29 -07001165 result = result && CheckWideRegisterIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -07001166 break;
Orion Hodson2e599942017-09-22 16:17:41 +01001167 case Instruction::kVerifyRegBCallSite:
1168 result = result && CheckCallSiteIndex(inst->VRegB());
1169 break;
1170 case Instruction::kVerifyRegBMethodHandle:
1171 result = result && CheckMethodHandleIndex(inst->VRegB());
1172 break;
1173 case Instruction::kVerifyRegBPrototype:
1174 result = result && CheckPrototypeIndex(inst->VRegB());
1175 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001176 }
1177 switch (inst->GetVerifyTypeArgumentC()) {
1178 case Instruction::kVerifyRegC:
Ian Rogers29a26482014-05-02 15:27:29 -07001179 result = result && CheckRegisterIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -07001180 break;
1181 case Instruction::kVerifyRegCField:
Ian Rogers29a26482014-05-02 15:27:29 -07001182 result = result && CheckFieldIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -07001183 break;
1184 case Instruction::kVerifyRegCNewArray:
Andreas Gampea5b09a62016-11-17 15:21:22 -08001185 result = result && CheckNewArray(dex::TypeIndex(inst->VRegC()));
Ian Rogersd81871c2011-10-03 13:57:23 -07001186 break;
1187 case Instruction::kVerifyRegCType:
Andreas Gampea5b09a62016-11-17 15:21:22 -08001188 result = result && CheckTypeIndex(dex::TypeIndex(inst->VRegC()));
Ian Rogersd81871c2011-10-03 13:57:23 -07001189 break;
1190 case Instruction::kVerifyRegCWide:
Ian Rogers29a26482014-05-02 15:27:29 -07001191 result = result && CheckWideRegisterIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -07001192 break;
1193 }
Orion Hodsoncfa325e2016-10-13 10:25:54 +01001194 switch (inst->GetVerifyTypeArgumentH()) {
1195 case Instruction::kVerifyRegHPrototype:
1196 result = result && CheckPrototypeIndex(inst->VRegH());
1197 break;
1198 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001199 switch (inst->GetVerifyExtraFlags()) {
1200 case Instruction::kVerifyArrayData:
1201 result = result && CheckArrayData(code_offset);
1202 break;
1203 case Instruction::kVerifyBranchTarget:
1204 result = result && CheckBranchTarget(code_offset);
1205 break;
1206 case Instruction::kVerifySwitchTargets:
1207 result = result && CheckSwitchTargets(code_offset);
1208 break;
Andreas Gampec3314312014-06-19 18:13:29 -07001209 case Instruction::kVerifyVarArgNonZero:
1210 // Fall-through.
Ian Rogers29a26482014-05-02 15:27:29 -07001211 case Instruction::kVerifyVarArg: {
Taiju Tsuiki29498a22015-04-13 14:21:00 +09001212 // Instructions that can actually return a negative value shouldn't have this flag.
1213 uint32_t v_a = dchecked_integral_cast<uint32_t>(inst->VRegA());
1214 if ((inst->GetVerifyExtraFlags() == Instruction::kVerifyVarArgNonZero && v_a == 0) ||
1215 v_a > Instruction::kMaxVarArgRegs) {
1216 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << v_a << ") in "
Andreas Gampec3314312014-06-19 18:13:29 -07001217 "non-range invoke";
1218 return false;
1219 }
Taiju Tsuiki29498a22015-04-13 14:21:00 +09001220
Ian Rogers29a26482014-05-02 15:27:29 -07001221 uint32_t args[Instruction::kMaxVarArgRegs];
1222 inst->GetVarArgs(args);
Taiju Tsuiki29498a22015-04-13 14:21:00 +09001223 result = result && CheckVarArgRegs(v_a, args);
Ian Rogersd81871c2011-10-03 13:57:23 -07001224 break;
Ian Rogers29a26482014-05-02 15:27:29 -07001225 }
Andreas Gampec3314312014-06-19 18:13:29 -07001226 case Instruction::kVerifyVarArgRangeNonZero:
1227 // Fall-through.
Ian Rogersd81871c2011-10-03 13:57:23 -07001228 case Instruction::kVerifyVarArgRange:
Andreas Gampec3314312014-06-19 18:13:29 -07001229 if (inst->GetVerifyExtraFlags() == Instruction::kVerifyVarArgRangeNonZero &&
1230 inst->VRegA() <= 0) {
1231 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << inst->VRegA() << ") in "
1232 "range invoke";
1233 return false;
1234 }
Ian Rogers29a26482014-05-02 15:27:29 -07001235 result = result && CheckVarArgRangeRegs(inst->VRegA(), inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -07001236 break;
1237 case Instruction::kVerifyError:
jeffhaod5347e02012-03-22 17:25:05 -07001238 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected opcode " << inst->Name();
Ian Rogersd81871c2011-10-03 13:57:23 -07001239 result = false;
1240 break;
1241 }
Andreas Gampebf1cb772017-05-15 15:39:00 -07001242 if (!kAllowRuntimeOnlyInstructions && inst->GetVerifyIsRuntimeOnly()) {
Ian Rogers5fb22a92014-06-13 10:31:28 -07001243 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "opcode only expected at runtime " << inst->Name();
1244 result = false;
1245 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001246 return result;
1247}
1248
Ian Rogers7b078e82014-09-10 14:44:24 -07001249inline bool MethodVerifier::CheckRegisterIndex(uint32_t idx) {
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001250 if (UNLIKELY(idx >= code_item_accessor_.RegistersSize())) {
jeffhaod5347e02012-03-22 17:25:05 -07001251 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register index out of range (" << idx << " >= "
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001252 << code_item_accessor_.RegistersSize() << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001253 return false;
1254 }
1255 return true;
1256}
1257
Ian Rogers7b078e82014-09-10 14:44:24 -07001258inline bool MethodVerifier::CheckWideRegisterIndex(uint32_t idx) {
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001259 if (UNLIKELY(idx + 1 >= code_item_accessor_.RegistersSize())) {
jeffhaod5347e02012-03-22 17:25:05 -07001260 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "wide register index out of range (" << idx
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001261 << "+1 >= " << code_item_accessor_.RegistersSize() << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001262 return false;
1263 }
1264 return true;
1265}
1266
Orion Hodson2e599942017-09-22 16:17:41 +01001267inline bool MethodVerifier::CheckCallSiteIndex(uint32_t idx) {
1268 uint32_t limit = dex_file_->NumCallSiteIds();
1269 if (UNLIKELY(idx >= limit)) {
1270 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad call site index " << idx << " (max "
1271 << limit << ")";
1272 return false;
1273 }
1274 return true;
1275}
1276
Ian Rogers7b078e82014-09-10 14:44:24 -07001277inline bool MethodVerifier::CheckFieldIndex(uint32_t idx) {
Andreas Gampe29e81422017-05-15 16:29:32 -07001278 if (UNLIKELY(idx >= dex_file_->GetHeader().field_ids_size_)) {
jeffhaod5347e02012-03-22 17:25:05 -07001279 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad field index " << idx << " (max "
1280 << dex_file_->GetHeader().field_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001281 return false;
1282 }
1283 return true;
1284}
1285
Ian Rogers7b078e82014-09-10 14:44:24 -07001286inline bool MethodVerifier::CheckMethodIndex(uint32_t idx) {
Andreas Gampe29e81422017-05-15 16:29:32 -07001287 if (UNLIKELY(idx >= dex_file_->GetHeader().method_ids_size_)) {
jeffhaod5347e02012-03-22 17:25:05 -07001288 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad method index " << idx << " (max "
1289 << dex_file_->GetHeader().method_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001290 return false;
1291 }
1292 return true;
1293}
1294
Orion Hodson2e599942017-09-22 16:17:41 +01001295inline bool MethodVerifier::CheckMethodHandleIndex(uint32_t idx) {
1296 uint32_t limit = dex_file_->NumMethodHandles();
1297 if (UNLIKELY(idx >= limit)) {
1298 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad method handle index " << idx << " (max "
1299 << limit << ")";
1300 return false;
1301 }
1302 return true;
1303}
1304
Andreas Gampea5b09a62016-11-17 15:21:22 -08001305inline bool MethodVerifier::CheckNewInstance(dex::TypeIndex idx) {
Andreas Gampe29e81422017-05-15 16:29:32 -07001306 if (UNLIKELY(idx.index_ >= dex_file_->GetHeader().type_ids_size_)) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08001307 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx.index_ << " (max "
jeffhaod5347e02012-03-22 17:25:05 -07001308 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001309 return false;
1310 }
1311 // We don't need the actual class, just a pointer to the class name.
Ian Rogers0571d352011-11-03 19:51:38 -07001312 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Andreas Gampe29e81422017-05-15 16:29:32 -07001313 if (UNLIKELY(descriptor[0] != 'L')) {
jeffhaod5347e02012-03-22 17:25:05 -07001314 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "can't call new-instance on type '" << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001315 return false;
Andreas Gampe29e81422017-05-15 16:29:32 -07001316 } else if (UNLIKELY(strcmp(descriptor, "Ljava/lang/Class;") == 0)) {
Aart Bikdb698f12016-07-25 17:52:22 -07001317 // An unlikely new instance on Class is not allowed. Fall back to interpreter to ensure an
1318 // exception is thrown when this statement is executed (compiled code would not do that).
1319 Fail(VERIFY_ERROR_INSTANTIATION);
Ian Rogersd81871c2011-10-03 13:57:23 -07001320 }
1321 return true;
1322}
1323
Orion Hodsoncfa325e2016-10-13 10:25:54 +01001324inline bool MethodVerifier::CheckPrototypeIndex(uint32_t idx) {
Andreas Gampe29e81422017-05-15 16:29:32 -07001325 if (UNLIKELY(idx >= dex_file_->GetHeader().proto_ids_size_)) {
Orion Hodsoncfa325e2016-10-13 10:25:54 +01001326 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad prototype index " << idx << " (max "
1327 << dex_file_->GetHeader().proto_ids_size_ << ")";
1328 return false;
1329 }
1330 return true;
1331}
1332
Ian Rogers7b078e82014-09-10 14:44:24 -07001333inline bool MethodVerifier::CheckStringIndex(uint32_t idx) {
Andreas Gampe29e81422017-05-15 16:29:32 -07001334 if (UNLIKELY(idx >= dex_file_->GetHeader().string_ids_size_)) {
jeffhaod5347e02012-03-22 17:25:05 -07001335 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad string index " << idx << " (max "
1336 << dex_file_->GetHeader().string_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001337 return false;
1338 }
1339 return true;
1340}
1341
Andreas Gampea5b09a62016-11-17 15:21:22 -08001342inline bool MethodVerifier::CheckTypeIndex(dex::TypeIndex idx) {
Andreas Gampe29e81422017-05-15 16:29:32 -07001343 if (UNLIKELY(idx.index_ >= dex_file_->GetHeader().type_ids_size_)) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08001344 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx.index_ << " (max "
jeffhaod5347e02012-03-22 17:25:05 -07001345 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001346 return false;
1347 }
1348 return true;
1349}
1350
Andreas Gampea5b09a62016-11-17 15:21:22 -08001351bool MethodVerifier::CheckNewArray(dex::TypeIndex idx) {
Andreas Gampe29e81422017-05-15 16:29:32 -07001352 if (UNLIKELY(idx.index_ >= dex_file_->GetHeader().type_ids_size_)) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08001353 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx.index_ << " (max "
jeffhaod5347e02012-03-22 17:25:05 -07001354 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001355 return false;
1356 }
1357 int bracket_count = 0;
Ian Rogers0571d352011-11-03 19:51:38 -07001358 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -07001359 const char* cp = descriptor;
1360 while (*cp++ == '[') {
1361 bracket_count++;
1362 }
Andreas Gampe29e81422017-05-15 16:29:32 -07001363 if (UNLIKELY(bracket_count == 0)) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001364 /* The given class must be an array type. */
Brian Carlstrom93c33962013-07-26 10:37:43 -07001365 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1366 << "can't new-array class '" << descriptor << "' (not an array)";
Ian Rogersd81871c2011-10-03 13:57:23 -07001367 return false;
Andreas Gampe29e81422017-05-15 16:29:32 -07001368 } else if (UNLIKELY(bracket_count > 255)) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001369 /* It is illegal to create an array of more than 255 dimensions. */
Brian Carlstrom93c33962013-07-26 10:37:43 -07001370 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1371 << "can't new-array class '" << descriptor << "' (exceeds limit)";
Ian Rogersd81871c2011-10-03 13:57:23 -07001372 return false;
1373 }
1374 return true;
1375}
1376
Ian Rogers776ac1f2012-04-13 23:36:36 -07001377bool MethodVerifier::CheckArrayData(uint32_t cur_offset) {
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001378 const uint32_t insn_count = code_item_accessor_.InsnsSizeInCodeUnits();
1379 const uint16_t* insns = code_item_accessor_.Insns() + cur_offset;
Ian Rogersd81871c2011-10-03 13:57:23 -07001380 const uint16_t* array_data;
1381 int32_t array_data_offset;
1382
1383 DCHECK_LT(cur_offset, insn_count);
1384 /* make sure the start of the array data table is in range */
Andreas Gampe53de99c2015-08-17 13:43:55 -07001385 array_data_offset = insns[1] | (static_cast<int32_t>(insns[2]) << 16);
Andreas Gampe29e81422017-05-15 16:29:32 -07001386 if (UNLIKELY(static_cast<int32_t>(cur_offset) + array_data_offset < 0 ||
1387 cur_offset + array_data_offset + 2 >= insn_count)) {
jeffhaod5347e02012-03-22 17:25:05 -07001388 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data start: at " << cur_offset
Brian Carlstrom93c33962013-07-26 10:37:43 -07001389 << ", data offset " << array_data_offset
1390 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -07001391 return false;
1392 }
1393 /* offset to array data table is a relative branch-style offset */
1394 array_data = insns + array_data_offset;
Andreas Gampe57c47582015-07-01 22:05:59 -07001395 // Make sure the table is at an even dex pc, that is, 32-bit aligned.
Andreas Gampe29e81422017-05-15 16:29:32 -07001396 if (UNLIKELY(!IsAligned<4>(array_data))) {
jeffhaod5347e02012-03-22 17:25:05 -07001397 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned array data table: at " << cur_offset
1398 << ", data offset " << array_data_offset;
Ian Rogersd81871c2011-10-03 13:57:23 -07001399 return false;
1400 }
Andreas Gampe57c47582015-07-01 22:05:59 -07001401 // Make sure the array-data is marked as an opcode. This ensures that it was reached when
1402 // traversing the code item linearly. It is an approximation for a by-spec padding value.
Andreas Gampe29e81422017-05-15 16:29:32 -07001403 if (UNLIKELY(!GetInstructionFlags(cur_offset + array_data_offset).IsOpcode())) {
Andreas Gampe57c47582015-07-01 22:05:59 -07001404 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array data table at " << cur_offset
1405 << ", data offset " << array_data_offset
1406 << " not correctly visited, probably bad padding.";
1407 return false;
1408 }
1409
Ian Rogersd81871c2011-10-03 13:57:23 -07001410 uint32_t value_width = array_data[1];
Elliott Hughes398f64b2012-03-26 18:05:48 -07001411 uint32_t value_count = *reinterpret_cast<const uint32_t*>(&array_data[2]);
Ian Rogersd81871c2011-10-03 13:57:23 -07001412 uint32_t table_size = 4 + (value_width * value_count + 1) / 2;
1413 /* make sure the end of the switch is in range */
Andreas Gampe29e81422017-05-15 16:29:32 -07001414 if (UNLIKELY(cur_offset + array_data_offset + table_size > insn_count)) {
jeffhaod5347e02012-03-22 17:25:05 -07001415 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data end: at " << cur_offset
1416 << ", data offset " << array_data_offset << ", end "
1417 << cur_offset + array_data_offset + table_size
1418 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -07001419 return false;
1420 }
1421 return true;
1422}
1423
Ian Rogers776ac1f2012-04-13 23:36:36 -07001424bool MethodVerifier::CheckBranchTarget(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001425 int32_t offset;
1426 bool isConditional, selfOkay;
1427 if (!GetBranchOffset(cur_offset, &offset, &isConditional, &selfOkay)) {
1428 return false;
1429 }
Andreas Gampe29e81422017-05-15 16:29:32 -07001430 if (UNLIKELY(!selfOkay && offset == 0)) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001431 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch offset of zero not allowed at"
1432 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -07001433 return false;
1434 }
Elliott Hughes81ff3182012-03-23 20:35:56 -07001435 // Check for 32-bit overflow. This isn't strictly necessary if we can depend on the runtime
1436 // to have identical "wrap-around" behavior, but it's unwise to depend on that.
Andreas Gampe29e81422017-05-15 16:29:32 -07001437 if (UNLIKELY(((int64_t) cur_offset + (int64_t) offset) != (int64_t) (cur_offset + offset))) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001438 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch target overflow "
1439 << reinterpret_cast<void*>(cur_offset) << " +" << offset;
Ian Rogersd81871c2011-10-03 13:57:23 -07001440 return false;
1441 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001442 int32_t abs_offset = cur_offset + offset;
Andreas Gampe29e81422017-05-15 16:29:32 -07001443 if (UNLIKELY(abs_offset < 0 ||
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001444 (uint32_t) abs_offset >= code_item_accessor_.InsnsSizeInCodeUnits() ||
Andreas Gampe29e81422017-05-15 16:29:32 -07001445 !GetInstructionFlags(abs_offset).IsOpcode())) {
jeffhaod5347e02012-03-22 17:25:05 -07001446 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid branch target " << offset << " (-> "
Elliott Hughes398f64b2012-03-26 18:05:48 -07001447 << reinterpret_cast<void*>(abs_offset) << ") at "
1448 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -07001449 return false;
1450 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07001451 GetInstructionFlags(abs_offset).SetBranchTarget();
Ian Rogersd81871c2011-10-03 13:57:23 -07001452 return true;
1453}
1454
Ian Rogers776ac1f2012-04-13 23:36:36 -07001455bool MethodVerifier::GetBranchOffset(uint32_t cur_offset, int32_t* pOffset, bool* pConditional,
Ian Rogersd81871c2011-10-03 13:57:23 -07001456 bool* selfOkay) {
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001457 const uint16_t* insns = code_item_accessor_.Insns() + cur_offset;
Ian Rogersd81871c2011-10-03 13:57:23 -07001458 *pConditional = false;
1459 *selfOkay = false;
jeffhaoba5ebb92011-08-25 17:24:37 -07001460 switch (*insns & 0xff) {
1461 case Instruction::GOTO:
1462 *pOffset = ((int16_t) *insns) >> 8;
jeffhaoba5ebb92011-08-25 17:24:37 -07001463 break;
1464 case Instruction::GOTO_32:
1465 *pOffset = insns[1] | (((uint32_t) insns[2]) << 16);
jeffhaoba5ebb92011-08-25 17:24:37 -07001466 *selfOkay = true;
1467 break;
1468 case Instruction::GOTO_16:
1469 *pOffset = (int16_t) insns[1];
jeffhaoba5ebb92011-08-25 17:24:37 -07001470 break;
1471 case Instruction::IF_EQ:
1472 case Instruction::IF_NE:
1473 case Instruction::IF_LT:
1474 case Instruction::IF_GE:
1475 case Instruction::IF_GT:
1476 case Instruction::IF_LE:
1477 case Instruction::IF_EQZ:
1478 case Instruction::IF_NEZ:
1479 case Instruction::IF_LTZ:
1480 case Instruction::IF_GEZ:
1481 case Instruction::IF_GTZ:
1482 case Instruction::IF_LEZ:
1483 *pOffset = (int16_t) insns[1];
1484 *pConditional = true;
jeffhaoba5ebb92011-08-25 17:24:37 -07001485 break;
1486 default:
1487 return false;
jeffhaoba5ebb92011-08-25 17:24:37 -07001488 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001489 return true;
1490}
1491
Ian Rogers776ac1f2012-04-13 23:36:36 -07001492bool MethodVerifier::CheckSwitchTargets(uint32_t cur_offset) {
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001493 const uint32_t insn_count = code_item_accessor_.InsnsSizeInCodeUnits();
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001494 DCHECK_LT(cur_offset, insn_count);
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001495 const uint16_t* insns = code_item_accessor_.Insns() + cur_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001496 /* make sure the start of the switch is in range */
Andreas Gampe53de99c2015-08-17 13:43:55 -07001497 int32_t switch_offset = insns[1] | (static_cast<int32_t>(insns[2]) << 16);
Andreas Gampe29e81422017-05-15 16:29:32 -07001498 if (UNLIKELY(static_cast<int32_t>(cur_offset) + switch_offset < 0 ||
1499 cur_offset + switch_offset + 2 > insn_count)) {
jeffhaod5347e02012-03-22 17:25:05 -07001500 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch start: at " << cur_offset
Brian Carlstrom93c33962013-07-26 10:37:43 -07001501 << ", switch offset " << switch_offset
1502 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001503 return false;
1504 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001505 /* offset to switch table is a relative branch-style offset */
Ian Rogersd81871c2011-10-03 13:57:23 -07001506 const uint16_t* switch_insns = insns + switch_offset;
Andreas Gampe57c47582015-07-01 22:05:59 -07001507 // Make sure the table is at an even dex pc, that is, 32-bit aligned.
Andreas Gampe29e81422017-05-15 16:29:32 -07001508 if (UNLIKELY(!IsAligned<4>(switch_insns))) {
jeffhaod5347e02012-03-22 17:25:05 -07001509 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned switch table: at " << cur_offset
1510 << ", switch offset " << switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001511 return false;
1512 }
Andreas Gampe57c47582015-07-01 22:05:59 -07001513 // Make sure the switch data is marked as an opcode. This ensures that it was reached when
1514 // traversing the code item linearly. It is an approximation for a by-spec padding value.
Andreas Gampe29e81422017-05-15 16:29:32 -07001515 if (UNLIKELY(!GetInstructionFlags(cur_offset + switch_offset).IsOpcode())) {
Andreas Gampe57c47582015-07-01 22:05:59 -07001516 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "switch table at " << cur_offset
1517 << ", switch offset " << switch_offset
1518 << " not correctly visited, probably bad padding.";
1519 return false;
1520 }
1521
David Brazdil5469d342015-09-25 16:57:53 +01001522 bool is_packed_switch = (*insns & 0xff) == Instruction::PACKED_SWITCH;
1523
Ian Rogersd81871c2011-10-03 13:57:23 -07001524 uint32_t switch_count = switch_insns[1];
David Brazdil5469d342015-09-25 16:57:53 +01001525 int32_t targets_offset;
Ian Rogersd81871c2011-10-03 13:57:23 -07001526 uint16_t expected_signature;
David Brazdil5469d342015-09-25 16:57:53 +01001527 if (is_packed_switch) {
jeffhaoba5ebb92011-08-25 17:24:37 -07001528 /* 0=sig, 1=count, 2/3=firstKey */
1529 targets_offset = 4;
jeffhaoba5ebb92011-08-25 17:24:37 -07001530 expected_signature = Instruction::kPackedSwitchSignature;
1531 } else {
1532 /* 0=sig, 1=count, 2..count*2 = keys */
jeffhaoba5ebb92011-08-25 17:24:37 -07001533 targets_offset = 2 + 2 * switch_count;
1534 expected_signature = Instruction::kSparseSwitchSignature;
1535 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001536 uint32_t table_size = targets_offset + switch_count * 2;
Andreas Gampe29e81422017-05-15 16:29:32 -07001537 if (UNLIKELY(switch_insns[0] != expected_signature)) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001538 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1539 << StringPrintf("wrong signature for switch table (%x, wanted %x)",
1540 switch_insns[0], expected_signature);
jeffhaoba5ebb92011-08-25 17:24:37 -07001541 return false;
1542 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001543 /* make sure the end of the switch is in range */
Andreas Gampe29e81422017-05-15 16:29:32 -07001544 if (UNLIKELY(cur_offset + switch_offset + table_size > (uint32_t) insn_count)) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001545 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch end: at " << cur_offset
1546 << ", switch offset " << switch_offset
1547 << ", end " << (cur_offset + switch_offset + table_size)
jeffhaod5347e02012-03-22 17:25:05 -07001548 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001549 return false;
1550 }
David Brazdil5469d342015-09-25 16:57:53 +01001551
1552 constexpr int32_t keys_offset = 2;
1553 if (switch_count > 1) {
1554 if (is_packed_switch) {
1555 /* for a packed switch, verify that keys do not overflow int32 */
1556 int32_t first_key = switch_insns[keys_offset] | (switch_insns[keys_offset + 1] << 16);
1557 int32_t max_first_key =
1558 std::numeric_limits<int32_t>::max() - (static_cast<int32_t>(switch_count) - 1);
Andreas Gampe29e81422017-05-15 16:29:32 -07001559 if (UNLIKELY(first_key > max_first_key)) {
David Brazdil5469d342015-09-25 16:57:53 +01001560 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid packed switch: first_key=" << first_key
1561 << ", switch_count=" << switch_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001562 return false;
1563 }
David Brazdil5469d342015-09-25 16:57:53 +01001564 } else {
1565 /* for a sparse switch, verify the keys are in ascending order */
1566 int32_t last_key = switch_insns[keys_offset] | (switch_insns[keys_offset + 1] << 16);
1567 for (uint32_t targ = 1; targ < switch_count; targ++) {
1568 int32_t key =
1569 static_cast<int32_t>(switch_insns[keys_offset + targ * 2]) |
1570 static_cast<int32_t>(switch_insns[keys_offset + targ * 2 + 1] << 16);
Andreas Gampe29e81422017-05-15 16:29:32 -07001571 if (UNLIKELY(key <= last_key)) {
David Brazdil5469d342015-09-25 16:57:53 +01001572 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid sparse switch: last key=" << last_key
1573 << ", this=" << key;
1574 return false;
1575 }
1576 last_key = key;
1577 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001578 }
1579 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001580 /* verify each switch target */
Ian Rogersd81871c2011-10-03 13:57:23 -07001581 for (uint32_t targ = 0; targ < switch_count; targ++) {
Andreas Gampe53de99c2015-08-17 13:43:55 -07001582 int32_t offset = static_cast<int32_t>(switch_insns[targets_offset + targ * 2]) |
1583 static_cast<int32_t>(switch_insns[targets_offset + targ * 2 + 1] << 16);
Ian Rogersd81871c2011-10-03 13:57:23 -07001584 int32_t abs_offset = cur_offset + offset;
Andreas Gampe29e81422017-05-15 16:29:32 -07001585 if (UNLIKELY(abs_offset < 0 ||
1586 abs_offset >= static_cast<int32_t>(insn_count) ||
1587 !GetInstructionFlags(abs_offset).IsOpcode())) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001588 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch target " << offset
1589 << " (-> " << reinterpret_cast<void*>(abs_offset) << ") at "
1590 << reinterpret_cast<void*>(cur_offset)
1591 << "[" << targ << "]";
jeffhaoba5ebb92011-08-25 17:24:37 -07001592 return false;
1593 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07001594 GetInstructionFlags(abs_offset).SetBranchTarget();
Ian Rogersd81871c2011-10-03 13:57:23 -07001595 }
1596 return true;
1597}
1598
Ian Rogers776ac1f2012-04-13 23:36:36 -07001599bool MethodVerifier::CheckVarArgRegs(uint32_t vA, uint32_t arg[]) {
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001600 uint16_t registers_size = code_item_accessor_.RegistersSize();
Ian Rogersd81871c2011-10-03 13:57:23 -07001601 for (uint32_t idx = 0; idx < vA; idx++) {
Andreas Gampe29e81422017-05-15 16:29:32 -07001602 if (UNLIKELY(arg[idx] >= registers_size)) {
jeffhaod5347e02012-03-22 17:25:05 -07001603 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index (" << arg[idx]
1604 << ") in non-range invoke (>= " << registers_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001605 return false;
1606 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001607 }
1608
1609 return true;
1610}
1611
Ian Rogers776ac1f2012-04-13 23:36:36 -07001612bool MethodVerifier::CheckVarArgRangeRegs(uint32_t vA, uint32_t vC) {
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001613 uint16_t registers_size = code_item_accessor_.RegistersSize();
Ian Rogersd81871c2011-10-03 13:57:23 -07001614 // vA/vC are unsigned 8-bit/16-bit quantities for /range instructions, so there's no risk of
1615 // integer overflow when adding them here.
Andreas Gampe29e81422017-05-15 16:29:32 -07001616 if (UNLIKELY(vA + vC > registers_size)) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001617 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index " << vA << "+" << vC
1618 << " in range invoke (> " << registers_size << ")";
jeffhaoba5ebb92011-08-25 17:24:37 -07001619 return false;
1620 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001621 return true;
1622}
1623
Ian Rogers776ac1f2012-04-13 23:36:36 -07001624bool MethodVerifier::VerifyCodeFlow() {
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001625 const uint16_t registers_size = code_item_accessor_.RegistersSize();
jeffhaobdb76512011-09-07 11:43:16 -07001626
Ian Rogersd81871c2011-10-03 13:57:23 -07001627 /* Create and initialize table holding register status */
Brian Carlstrom93c33962013-07-26 10:37:43 -07001628 reg_table_.Init(kTrackCompilerInterestPoints,
1629 insn_flags_.get(),
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001630 code_item_accessor_.InsnsSizeInCodeUnits(),
Brian Carlstrom93c33962013-07-26 10:37:43 -07001631 registers_size,
1632 this);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07001633
Ian Rogersd0fbd852013-09-24 18:17:04 -07001634 work_line_.reset(RegisterLine::Create(registers_size, this));
1635 saved_line_.reset(RegisterLine::Create(registers_size, this));
jeffhaobdb76512011-09-07 11:43:16 -07001636
Ian Rogersd81871c2011-10-03 13:57:23 -07001637 /* Initialize register types of method arguments. */
1638 if (!SetTypesFromSignature()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001639 DCHECK_NE(failures_.size(), 0U);
1640 std::string prepend("Bad signature in ");
David Sehr709b0702016-10-13 09:12:37 -07001641 prepend += dex_file_->PrettyMethod(dex_method_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001642 PrependToLastFailMessage(prepend);
Ian Rogersd81871c2011-10-03 13:57:23 -07001643 return false;
1644 }
Andreas Gamped5ad72f2015-06-26 17:33:47 -07001645 // We may have a runtime failure here, clear.
1646 have_pending_runtime_throw_failure_ = false;
1647
Ian Rogersd81871c2011-10-03 13:57:23 -07001648 /* Perform code flow verification. */
1649 if (!CodeFlowVerifyMethod()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001650 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -07001651 return false;
jeffhaobdb76512011-09-07 11:43:16 -07001652 }
jeffhaobdb76512011-09-07 11:43:16 -07001653 return true;
1654}
1655
Ian Rogersad0b3a32012-04-16 14:50:24 -07001656std::ostream& MethodVerifier::DumpFailures(std::ostream& os) {
1657 DCHECK_EQ(failures_.size(), failure_messages_.size());
Jeff Hao4137f482013-11-22 11:44:57 -08001658 for (size_t i = 0; i < failures_.size(); ++i) {
1659 os << failure_messages_[i]->str() << "\n";
Ian Rogersad0b3a32012-04-16 14:50:24 -07001660 }
1661 return os;
1662}
1663
Ian Rogers776ac1f2012-04-13 23:36:36 -07001664void MethodVerifier::Dump(std::ostream& os) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001665 VariableIndentationOutputStream vios(&os);
1666 Dump(&vios);
1667}
1668
1669void MethodVerifier::Dump(VariableIndentationOutputStream* vios) {
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001670 if (!code_item_accessor_.HasCodeItem()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001671 vios->Stream() << "Native method\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001672 return;
jeffhaobdb76512011-09-07 11:43:16 -07001673 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001674 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001675 vios->Stream() << "Register Types:\n";
1676 ScopedIndentation indent1(vios);
1677 reg_types_.Dump(vios->Stream());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001678 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001679 vios->Stream() << "Dumping instructions and register lines:\n";
1680 ScopedIndentation indent1(vios);
Mathieu Chartier1d2d4ff2017-09-23 16:11:06 -07001681
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001682 for (const DexInstructionPcPair& inst : code_item_accessor_) {
Mathieu Chartier2b2bef22017-10-26 17:10:19 -07001683 const size_t dex_pc = inst.DexPc();
Ian Rogersd81871c2011-10-03 13:57:23 -07001684 RegisterLine* reg_line = reg_table_.GetLine(dex_pc);
Ian Rogers7b078e82014-09-10 14:44:24 -07001685 if (reg_line != nullptr) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001686 vios->Stream() << reg_line->Dump(this) << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07001687 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001688 vios->Stream()
Mathieu Chartierde40d472015-10-15 17:47:48 -07001689 << StringPrintf("0x%04zx", dex_pc) << ": " << GetInstructionFlags(dex_pc).ToString() << " ";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001690 const bool kDumpHexOfInstruction = false;
1691 if (kDumpHexOfInstruction) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001692 vios->Stream() << inst->DumpHex(5) << " ";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001693 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001694 vios->Stream() << inst->DumpString(dex_file_) << "\n";
jeffhaoba5ebb92011-08-25 17:24:37 -07001695 }
jeffhaobdb76512011-09-07 11:43:16 -07001696}
1697
Ian Rogersd81871c2011-10-03 13:57:23 -07001698static bool IsPrimitiveDescriptor(char descriptor) {
1699 switch (descriptor) {
jeffhaobdb76512011-09-07 11:43:16 -07001700 case 'I':
1701 case 'C':
1702 case 'S':
1703 case 'B':
1704 case 'Z':
jeffhaobdb76512011-09-07 11:43:16 -07001705 case 'F':
1706 case 'D':
1707 case 'J':
Ian Rogersd81871c2011-10-03 13:57:23 -07001708 return true;
jeffhaobdb76512011-09-07 11:43:16 -07001709 default:
1710 return false;
1711 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001712}
1713
Ian Rogers776ac1f2012-04-13 23:36:36 -07001714bool MethodVerifier::SetTypesFromSignature() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001715 RegisterLine* reg_line = reg_table_.GetLine(0);
Andreas Gampeef0b1a12015-06-19 20:37:46 -07001716
1717 // Should have been verified earlier.
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001718 DCHECK_GE(code_item_accessor_.RegistersSize(), code_item_accessor_.InsSize());
Andreas Gampeef0b1a12015-06-19 20:37:46 -07001719
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001720 uint32_t arg_start = code_item_accessor_.RegistersSize() - code_item_accessor_.InsSize();
1721 size_t expected_args = code_item_accessor_.InsSize(); /* long/double count as two */
jeffhaobdb76512011-09-07 11:43:16 -07001722
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001723 // Include the "this" pointer.
Ian Rogersd81871c2011-10-03 13:57:23 -07001724 size_t cur_arg = 0;
Ian Rogersad0b3a32012-04-16 14:50:24 -07001725 if (!IsStatic()) {
Andreas Gampeef0b1a12015-06-19 20:37:46 -07001726 if (expected_args == 0) {
1727 // Expect at least a receiver.
1728 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected 0 args, but method is not static";
1729 return false;
1730 }
1731
Ian Rogersd81871c2011-10-03 13:57:23 -07001732 // If this is a constructor for a class other than java.lang.Object, mark the first ("this")
1733 // argument as uninitialized. This restricts field access until the superclass constructor is
1734 // called.
Ian Rogersd8f69b02014-09-10 21:43:52 +00001735 const RegType& declaring_class = GetDeclaringClass();
Andreas Gampef10b6e12015-08-12 10:48:12 -07001736 if (IsConstructor()) {
1737 if (declaring_class.IsJavaLangObject()) {
1738 // "this" is implicitly initialized.
1739 reg_line->SetThisInitialized();
Andreas Gampead238ce2015-08-24 21:13:08 -07001740 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, declaring_class);
Andreas Gampef10b6e12015-08-12 10:48:12 -07001741 } else {
Andreas Gampead238ce2015-08-24 21:13:08 -07001742 reg_line->SetRegisterType<LockOp::kClear>(
1743 this,
1744 arg_start + cur_arg,
1745 reg_types_.UninitializedThisArgument(declaring_class));
Andreas Gampef10b6e12015-08-12 10:48:12 -07001746 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001747 } else {
Andreas Gampead238ce2015-08-24 21:13:08 -07001748 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, declaring_class);
jeffhaobdb76512011-09-07 11:43:16 -07001749 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001750 cur_arg++;
jeffhaobdb76512011-09-07 11:43:16 -07001751 }
1752
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001753 const DexFile::ProtoId& proto_id =
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001754 dex_file_->GetMethodPrototype(dex_file_->GetMethodId(dex_method_idx_));
Ian Rogers0571d352011-11-03 19:51:38 -07001755 DexFileParameterIterator iterator(*dex_file_, proto_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07001756
1757 for (; iterator.HasNext(); iterator.Next()) {
1758 const char* descriptor = iterator.GetDescriptor();
Ian Rogers7b078e82014-09-10 14:44:24 -07001759 if (descriptor == nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001760 LOG(FATAL) << "Null descriptor";
1761 }
1762 if (cur_arg >= expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07001763 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1764 << " args, found more (" << descriptor << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001765 return false;
1766 }
1767 switch (descriptor[0]) {
1768 case 'L':
1769 case '[':
1770 // We assume that reference arguments are initialized. The only way it could be otherwise
1771 // (assuming the caller was verified) is if the current method is <init>, but in that case
1772 // it's effectively considered initialized the instant we reach here (in the sense that we
1773 // can return without doing anything or call virtual methods).
1774 {
Andreas Gampe98be1a92017-08-28 08:25:45 -07001775 // Note: don't check access. No error would be thrown for declaring or passing an
1776 // inaccessible class. Only actual accesses to fields or methods will.
1777 const RegType& reg_type = ResolveClass<CheckAccess::kNo>(iterator.GetTypeIdx());
Sebastien Hertz2ed76f92014-04-22 17:11:08 +02001778 if (!reg_type.IsNonZeroReferenceTypes()) {
1779 DCHECK(HasFailures());
1780 return false;
1781 }
Andreas Gampead238ce2015-08-24 21:13:08 -07001782 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001783 }
1784 break;
1785 case 'Z':
Andreas Gampead238ce2015-08-24 21:13:08 -07001786 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Boolean());
Ian Rogersd81871c2011-10-03 13:57:23 -07001787 break;
1788 case 'C':
Andreas Gampead238ce2015-08-24 21:13:08 -07001789 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Char());
Ian Rogersd81871c2011-10-03 13:57:23 -07001790 break;
1791 case 'B':
Andreas Gampead238ce2015-08-24 21:13:08 -07001792 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Byte());
Ian Rogersd81871c2011-10-03 13:57:23 -07001793 break;
1794 case 'I':
Andreas Gampead238ce2015-08-24 21:13:08 -07001795 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Integer());
Ian Rogersd81871c2011-10-03 13:57:23 -07001796 break;
1797 case 'S':
Andreas Gampead238ce2015-08-24 21:13:08 -07001798 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Short());
Ian Rogersd81871c2011-10-03 13:57:23 -07001799 break;
1800 case 'F':
Andreas Gampead238ce2015-08-24 21:13:08 -07001801 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Float());
Ian Rogersd81871c2011-10-03 13:57:23 -07001802 break;
1803 case 'J':
1804 case 'D': {
Andreas Gampe77cd4d62014-06-19 17:29:48 -07001805 if (cur_arg + 1 >= expected_args) {
1806 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1807 << " args, found more (" << descriptor << ")";
1808 return false;
1809 }
1810
Ian Rogers7b078e82014-09-10 14:44:24 -07001811 const RegType* lo_half;
1812 const RegType* hi_half;
1813 if (descriptor[0] == 'J') {
1814 lo_half = &reg_types_.LongLo();
1815 hi_half = &reg_types_.LongHi();
1816 } else {
1817 lo_half = &reg_types_.DoubleLo();
1818 hi_half = &reg_types_.DoubleHi();
1819 }
1820 reg_line->SetRegisterTypeWide(this, arg_start + cur_arg, *lo_half, *hi_half);
Ian Rogersd81871c2011-10-03 13:57:23 -07001821 cur_arg++;
1822 break;
1823 }
1824 default:
Brian Carlstrom93c33962013-07-26 10:37:43 -07001825 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected signature type char '"
1826 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001827 return false;
1828 }
1829 cur_arg++;
1830 }
1831 if (cur_arg != expected_args) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001832 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1833 << " arguments, found " << cur_arg;
Ian Rogersd81871c2011-10-03 13:57:23 -07001834 return false;
1835 }
1836 const char* descriptor = dex_file_->GetReturnTypeDescriptor(proto_id);
1837 // Validate return type. We don't do the type lookup; just want to make sure that it has the right
1838 // format. Only major difference from the method argument format is that 'V' is supported.
1839 bool result;
1840 if (IsPrimitiveDescriptor(descriptor[0]) || descriptor[0] == 'V') {
1841 result = descriptor[1] == '\0';
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001842 } else if (descriptor[0] == '[') { // single/multi-dimensional array of object/primitive
Ian Rogersd81871c2011-10-03 13:57:23 -07001843 size_t i = 0;
1844 do {
1845 i++;
1846 } while (descriptor[i] == '['); // process leading [
1847 if (descriptor[i] == 'L') { // object array
1848 do {
1849 i++; // find closing ;
1850 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1851 result = descriptor[i] == ';';
1852 } else { // primitive array
1853 result = IsPrimitiveDescriptor(descriptor[i]) && descriptor[i + 1] == '\0';
1854 }
1855 } else if (descriptor[0] == 'L') {
1856 // could be more thorough here, but shouldn't be required
1857 size_t i = 0;
1858 do {
1859 i++;
1860 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1861 result = descriptor[i] == ';';
1862 } else {
1863 result = false;
1864 }
1865 if (!result) {
jeffhaod5347e02012-03-22 17:25:05 -07001866 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected char in return type descriptor '"
1867 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001868 }
1869 return result;
jeffhaobdb76512011-09-07 11:43:16 -07001870}
1871
Ian Rogers776ac1f2012-04-13 23:36:36 -07001872bool MethodVerifier::CodeFlowVerifyMethod() {
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001873 const uint16_t* insns = code_item_accessor_.Insns();
1874 const uint32_t insns_size = code_item_accessor_.InsnsSizeInCodeUnits();
jeffhaoba5ebb92011-08-25 17:24:37 -07001875
jeffhaobdb76512011-09-07 11:43:16 -07001876 /* Begin by marking the first instruction as "changed". */
Mathieu Chartierde40d472015-10-15 17:47:48 -07001877 GetInstructionFlags(0).SetChanged();
Ian Rogersd81871c2011-10-03 13:57:23 -07001878 uint32_t start_guess = 0;
jeffhaoba5ebb92011-08-25 17:24:37 -07001879
jeffhaobdb76512011-09-07 11:43:16 -07001880 /* Continue until no instructions are marked "changed". */
1881 while (true) {
Mathieu Chartier4306ef82014-12-19 18:41:47 -08001882 if (allow_thread_suspension_) {
1883 self_->AllowThreadSuspension();
1884 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001885 // Find the first marked one. Use "start_guess" as a way to find one quickly.
1886 uint32_t insn_idx = start_guess;
1887 for (; insn_idx < insns_size; insn_idx++) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07001888 if (GetInstructionFlags(insn_idx).IsChanged())
jeffhaobdb76512011-09-07 11:43:16 -07001889 break;
1890 }
jeffhaobdb76512011-09-07 11:43:16 -07001891 if (insn_idx == insns_size) {
1892 if (start_guess != 0) {
1893 /* try again, starting from the top */
1894 start_guess = 0;
1895 continue;
1896 } else {
1897 /* all flags are clear */
1898 break;
1899 }
1900 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001901 // We carry the working set of registers from instruction to instruction. If this address can
1902 // be the target of a branch (or throw) instruction, or if we're skipping around chasing
1903 // "changed" flags, we need to load the set of registers from the table.
1904 // Because we always prefer to continue on to the next instruction, we should never have a
1905 // situation where we have a stray "changed" flag set on an instruction that isn't a branch
1906 // target.
1907 work_insn_idx_ = insn_idx;
Mathieu Chartierde40d472015-10-15 17:47:48 -07001908 if (GetInstructionFlags(insn_idx).IsBranchTarget()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001909 work_line_->CopyFromLine(reg_table_.GetLine(insn_idx));
Ian Rogersebbdd872014-07-07 23:53:08 -07001910 } else if (kIsDebugBuild) {
jeffhaobdb76512011-09-07 11:43:16 -07001911 /*
1912 * Sanity check: retrieve the stored register line (assuming
1913 * a full table) and make sure it actually matches.
1914 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001915 RegisterLine* register_line = reg_table_.GetLine(insn_idx);
Ian Rogers7b078e82014-09-10 14:44:24 -07001916 if (register_line != nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001917 if (work_line_->CompareLine(register_line) != 0) {
1918 Dump(std::cout);
1919 std::cout << info_messages_.str();
David Sehr709b0702016-10-13 09:12:37 -07001920 LOG(FATAL) << "work_line diverged in " << dex_file_->PrettyMethod(dex_method_idx_)
Elliott Hughesc073b072012-05-24 19:29:17 -07001921 << "@" << reinterpret_cast<void*>(work_insn_idx_) << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07001922 << " work_line=" << work_line_->Dump(this) << "\n"
1923 << " expected=" << register_line->Dump(this);
Ian Rogersd81871c2011-10-03 13:57:23 -07001924 }
jeffhaobdb76512011-09-07 11:43:16 -07001925 }
jeffhaobdb76512011-09-07 11:43:16 -07001926 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001927 if (!CodeFlowVerifyInstruction(&start_guess)) {
David Sehr709b0702016-10-13 09:12:37 -07001928 std::string prepend(dex_file_->PrettyMethod(dex_method_idx_));
Ian Rogersad0b3a32012-04-16 14:50:24 -07001929 prepend += " failed to verify: ";
1930 PrependToLastFailMessage(prepend);
jeffhaoba5ebb92011-08-25 17:24:37 -07001931 return false;
1932 }
jeffhaobdb76512011-09-07 11:43:16 -07001933 /* Clear "changed" and mark as visited. */
Mathieu Chartierde40d472015-10-15 17:47:48 -07001934 GetInstructionFlags(insn_idx).SetVisited();
1935 GetInstructionFlags(insn_idx).ClearChanged();
jeffhaobdb76512011-09-07 11:43:16 -07001936 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001937
Andreas Gampeec6e6c12015-11-05 20:39:56 -08001938 if (kDebugVerify) {
jeffhaobdb76512011-09-07 11:43:16 -07001939 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001940 * Scan for dead code. There's nothing "evil" about dead code
jeffhaobdb76512011-09-07 11:43:16 -07001941 * (besides the wasted space), but it indicates a flaw somewhere
1942 * down the line, possibly in the verifier.
1943 *
1944 * If we've substituted "always throw" instructions into the stream,
1945 * we are almost certainly going to have some dead code.
1946 */
1947 int dead_start = -1;
Mathieu Chartier1d2d4ff2017-09-23 16:11:06 -07001948
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001949 for (const DexInstructionPcPair& inst : code_item_accessor_) {
Mathieu Chartier2b2bef22017-10-26 17:10:19 -07001950 const uint32_t insn_idx = inst.DexPc();
jeffhaobdb76512011-09-07 11:43:16 -07001951 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001952 * Switch-statement data doesn't get "visited" by scanner. It
jeffhaobdb76512011-09-07 11:43:16 -07001953 * may or may not be preceded by a padding NOP (for alignment).
1954 */
1955 if (insns[insn_idx] == Instruction::kPackedSwitchSignature ||
1956 insns[insn_idx] == Instruction::kSparseSwitchSignature ||
1957 insns[insn_idx] == Instruction::kArrayDataSignature ||
Elliott Hughes380aaa72012-07-09 14:33:15 -07001958 (insns[insn_idx] == Instruction::NOP && (insn_idx + 1 < insns_size) &&
jeffhaobdb76512011-09-07 11:43:16 -07001959 (insns[insn_idx + 1] == Instruction::kPackedSwitchSignature ||
1960 insns[insn_idx + 1] == Instruction::kSparseSwitchSignature ||
1961 insns[insn_idx + 1] == Instruction::kArrayDataSignature))) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07001962 GetInstructionFlags(insn_idx).SetVisited();
jeffhaobdb76512011-09-07 11:43:16 -07001963 }
1964
Mathieu Chartierde40d472015-10-15 17:47:48 -07001965 if (!GetInstructionFlags(insn_idx).IsVisited()) {
Mathieu Chartier1d2d4ff2017-09-23 16:11:06 -07001966 if (dead_start < 0) {
jeffhaobdb76512011-09-07 11:43:16 -07001967 dead_start = insn_idx;
Mathieu Chartier1d2d4ff2017-09-23 16:11:06 -07001968 }
jeffhaobdb76512011-09-07 11:43:16 -07001969 } else if (dead_start >= 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001970 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start)
1971 << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaobdb76512011-09-07 11:43:16 -07001972 dead_start = -1;
1973 }
1974 }
1975 if (dead_start >= 0) {
Mathieu Chartier1d2d4ff2017-09-23 16:11:06 -07001976 LogVerifyInfo()
1977 << "dead code " << reinterpret_cast<void*>(dead_start)
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001978 << "-" << reinterpret_cast<void*>(code_item_accessor_.InsnsSizeInCodeUnits() - 1);
jeffhaoba5ebb92011-08-25 17:24:37 -07001979 }
Ian Rogersc9e463c2013-06-05 16:52:26 -07001980 // To dump the state of the verify after a method, do something like:
David Sehr709b0702016-10-13 09:12:37 -07001981 // if (dex_file_->PrettyMethod(dex_method_idx_) ==
Ian Rogersc9e463c2013-06-05 16:52:26 -07001982 // "boolean java.lang.String.equals(java.lang.Object)") {
1983 // LOG(INFO) << info_messages_.str();
1984 // }
jeffhaoba5ebb92011-08-25 17:24:37 -07001985 }
jeffhaobdb76512011-09-07 11:43:16 -07001986 return true;
1987}
1988
Andreas Gampe68df3202015-06-22 11:35:46 -07001989// Returns the index of the first final instance field of the given class, or kDexNoIndex if there
1990// is no such field.
Andreas Gampea5b09a62016-11-17 15:21:22 -08001991static uint32_t GetFirstFinalInstanceFieldIndex(const DexFile& dex_file, dex::TypeIndex type_idx) {
Andreas Gampe68df3202015-06-22 11:35:46 -07001992 const DexFile::ClassDef* class_def = dex_file.FindClassDef(type_idx);
1993 DCHECK(class_def != nullptr);
1994 const uint8_t* class_data = dex_file.GetClassData(*class_def);
1995 DCHECK(class_data != nullptr);
1996 ClassDataItemIterator it(dex_file, class_data);
Mathieu Chartiere17cf242017-06-19 11:05:51 -07001997 it.SkipStaticFields();
Andreas Gampe68df3202015-06-22 11:35:46 -07001998 while (it.HasNextInstanceField()) {
1999 if ((it.GetFieldAccessFlags() & kAccFinal) != 0) {
2000 return it.GetMemberIndex();
2001 }
2002 it.Next();
2003 }
Andreas Gampee2abbc62017-09-15 11:59:26 -07002004 return dex::kDexNoIndex;
Andreas Gampe68df3202015-06-22 11:35:46 -07002005}
2006
Andreas Gampea727e372015-08-25 09:22:37 -07002007// Setup a register line for the given return instruction.
2008static void AdjustReturnLine(MethodVerifier* verifier,
2009 const Instruction* ret_inst,
2010 RegisterLine* line) {
2011 Instruction::Code opcode = ret_inst->Opcode();
2012
2013 switch (opcode) {
2014 case Instruction::RETURN_VOID:
2015 case Instruction::RETURN_VOID_NO_BARRIER:
2016 SafelyMarkAllRegistersAsConflicts(verifier, line);
2017 break;
2018
2019 case Instruction::RETURN:
2020 case Instruction::RETURN_OBJECT:
2021 line->MarkAllRegistersAsConflictsExcept(verifier, ret_inst->VRegA_11x());
2022 break;
2023
2024 case Instruction::RETURN_WIDE:
2025 line->MarkAllRegistersAsConflictsExceptWide(verifier, ret_inst->VRegA_11x());
2026 break;
2027
2028 default:
2029 LOG(FATAL) << "Unknown return opcode " << opcode;
2030 UNREACHABLE();
2031 }
2032}
2033
Ian Rogers776ac1f2012-04-13 23:36:36 -07002034bool MethodVerifier::CodeFlowVerifyInstruction(uint32_t* start_guess) {
Elliott Hughes08fc03a2012-06-26 17:34:00 -07002035 // If we're doing FindLocksAtDexPc, check whether we're at the dex pc we care about.
2036 // We want the state _before_ the instruction, for the case where the dex pc we're
2037 // interested in is itself a monitor-enter instruction (which is a likely place
2038 // for a thread to be suspended).
Ian Rogers7b078e82014-09-10 14:44:24 -07002039 if (monitor_enter_dex_pcs_ != nullptr && work_insn_idx_ == interesting_dex_pc_) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -07002040 monitor_enter_dex_pcs_->clear(); // The new work line is more accurate than the previous one.
Andreas Gampeaaf0d382017-11-27 14:10:21 -08002041
2042 std::map<uint32_t, DexLockInfo> depth_to_lock_info;
2043 auto collector = [&](uint32_t dex_reg, uint32_t depth) {
2044 auto insert_pair = depth_to_lock_info.emplace(depth, DexLockInfo(depth));
2045 auto it = insert_pair.first;
2046 auto set_insert_pair = it->second.dex_registers.insert(dex_reg);
2047 DCHECK(set_insert_pair.second);
2048 };
2049 work_line_->IterateRegToLockDepths(collector);
2050 for (auto& pair : depth_to_lock_info) {
2051 monitor_enter_dex_pcs_->push_back(pair.second);
2052 // Map depth to dex PC.
2053 (*monitor_enter_dex_pcs_)[monitor_enter_dex_pcs_->size() - 1].dex_pc =
2054 work_line_->GetMonitorEnterDexPc(pair.second.dex_pc);
Elliott Hughes08fc03a2012-06-26 17:34:00 -07002055 }
2056 }
2057
jeffhaobdb76512011-09-07 11:43:16 -07002058 /*
2059 * Once we finish decoding the instruction, we need to figure out where
jeffhaod1f0fde2011-09-08 17:25:33 -07002060 * we can go from here. There are three possible ways to transfer
jeffhaobdb76512011-09-07 11:43:16 -07002061 * control to another statement:
2062 *
jeffhaod1f0fde2011-09-08 17:25:33 -07002063 * (1) Continue to the next instruction. Applies to all but
jeffhaobdb76512011-09-07 11:43:16 -07002064 * unconditional branches, method returns, and exception throws.
jeffhaod1f0fde2011-09-08 17:25:33 -07002065 * (2) Branch to one or more possible locations. Applies to branches
jeffhaobdb76512011-09-07 11:43:16 -07002066 * and switch statements.
jeffhaod1f0fde2011-09-08 17:25:33 -07002067 * (3) Exception handlers. Applies to any instruction that can
jeffhaobdb76512011-09-07 11:43:16 -07002068 * throw an exception that is handled by an encompassing "try"
2069 * block.
2070 *
2071 * We can also return, in which case there is no successor instruction
2072 * from this point.
2073 *
Elliott Hughesadb8c672012-03-06 16:49:32 -08002074 * The behavior can be determined from the opcode flags.
jeffhaobdb76512011-09-07 11:43:16 -07002075 */
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08002076 const uint16_t* insns = code_item_accessor_.Insns() + work_insn_idx_;
Ian Rogersd81871c2011-10-03 13:57:23 -07002077 const Instruction* inst = Instruction::At(insns);
Ian Rogersa75a0132012-09-28 11:41:42 -07002078 int opcode_flags = Instruction::FlagsOf(inst->Opcode());
jeffhaobdb76512011-09-07 11:43:16 -07002079
jeffhaobdb76512011-09-07 11:43:16 -07002080 int32_t branch_target = 0;
jeffhaobdb76512011-09-07 11:43:16 -07002081 bool just_set_result = false;
Andreas Gampeec6e6c12015-11-05 20:39:56 -08002082 if (kDebugVerify) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002083 // Generate processing back trace to debug verifier
Elliott Hughesc073b072012-05-24 19:29:17 -07002084 LogVerifyInfo() << "Processing " << inst->DumpString(dex_file_) << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07002085 << work_line_->Dump(this) << "\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07002086 }
jeffhaobdb76512011-09-07 11:43:16 -07002087
2088 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002089 * Make a copy of the previous register state. If the instruction
jeffhaobdb76512011-09-07 11:43:16 -07002090 * can throw an exception, we will copy/merge this into the "catch"
2091 * address rather than work_line, because we don't want the result
2092 * from the "successful" code path (e.g. a check-cast that "improves"
2093 * a type) to be visible to the exception handler.
2094 */
Ian Rogers776ac1f2012-04-13 23:36:36 -07002095 if ((opcode_flags & Instruction::kThrow) != 0 && CurrentInsnFlags()->IsInTry()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002096 saved_line_->CopyFromLine(work_line_.get());
Ian Rogers1ff3c982014-08-12 02:30:58 -07002097 } else if (kIsDebugBuild) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002098 saved_line_->FillWithGarbage();
jeffhaobdb76512011-09-07 11:43:16 -07002099 }
Andreas Gamped12e7822015-06-25 10:26:40 -07002100 DCHECK(!have_pending_runtime_throw_failure_); // Per-instruction flag, should not be set here.
jeffhaobdb76512011-09-07 11:43:16 -07002101
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07002102
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002103 // We need to ensure the work line is consistent while performing validation. When we spot a
2104 // peephole pattern we compute a new line for either the fallthrough instruction or the
2105 // branch target.
Mathieu Chartier361e04a2016-02-16 14:06:35 -08002106 RegisterLineArenaUniquePtr branch_line;
2107 RegisterLineArenaUniquePtr fallthrough_line;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002108
Sebastien Hertz5243e912013-05-21 10:55:07 +02002109 switch (inst->Opcode()) {
jeffhaobdb76512011-09-07 11:43:16 -07002110 case Instruction::NOP:
2111 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002112 * A "pure" NOP has no effect on anything. Data tables start with
jeffhaobdb76512011-09-07 11:43:16 -07002113 * a signature that looks like a NOP; if we see one of these in
2114 * the course of executing code then we have a problem.
2115 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002116 if (inst->VRegA_10x() != 0) {
jeffhaod5347e02012-03-22 17:25:05 -07002117 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "encountered data table in instruction stream";
jeffhaobdb76512011-09-07 11:43:16 -07002118 }
2119 break;
2120
2121 case Instruction::MOVE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002122 work_line_->CopyRegister1(this, inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategory1nr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02002123 break;
jeffhaobdb76512011-09-07 11:43:16 -07002124 case Instruction::MOVE_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07002125 work_line_->CopyRegister1(this, inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategory1nr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02002126 break;
jeffhaobdb76512011-09-07 11:43:16 -07002127 case Instruction::MOVE_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07002128 work_line_->CopyRegister1(this, inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategory1nr);
jeffhaobdb76512011-09-07 11:43:16 -07002129 break;
2130 case Instruction::MOVE_WIDE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002131 work_line_->CopyRegister2(this, inst->VRegA_12x(), inst->VRegB_12x());
Sebastien Hertz5243e912013-05-21 10:55:07 +02002132 break;
jeffhaobdb76512011-09-07 11:43:16 -07002133 case Instruction::MOVE_WIDE_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07002134 work_line_->CopyRegister2(this, inst->VRegA_22x(), inst->VRegB_22x());
Sebastien Hertz5243e912013-05-21 10:55:07 +02002135 break;
jeffhaobdb76512011-09-07 11:43:16 -07002136 case Instruction::MOVE_WIDE_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07002137 work_line_->CopyRegister2(this, inst->VRegA_32x(), inst->VRegB_32x());
jeffhaobdb76512011-09-07 11:43:16 -07002138 break;
2139 case Instruction::MOVE_OBJECT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002140 work_line_->CopyRegister1(this, inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategoryRef);
Sebastien Hertz5243e912013-05-21 10:55:07 +02002141 break;
jeffhaobdb76512011-09-07 11:43:16 -07002142 case Instruction::MOVE_OBJECT_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07002143 work_line_->CopyRegister1(this, inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategoryRef);
Sebastien Hertz5243e912013-05-21 10:55:07 +02002144 break;
jeffhaobdb76512011-09-07 11:43:16 -07002145 case Instruction::MOVE_OBJECT_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07002146 work_line_->CopyRegister1(this, inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategoryRef);
jeffhaobdb76512011-09-07 11:43:16 -07002147 break;
2148
2149 /*
2150 * The move-result instructions copy data out of a "pseudo-register"
jeffhaod1f0fde2011-09-08 17:25:33 -07002151 * with the results from the last method invocation. In practice we
jeffhaobdb76512011-09-07 11:43:16 -07002152 * might want to hold the result in an actual CPU register, so the
2153 * Dalvik spec requires that these only appear immediately after an
2154 * invoke or filled-new-array.
2155 *
jeffhaod1f0fde2011-09-08 17:25:33 -07002156 * These calls invalidate the "result" register. (This is now
jeffhaobdb76512011-09-07 11:43:16 -07002157 * redundant with the reset done below, but it can make the debug info
2158 * easier to read in some cases.)
2159 */
2160 case Instruction::MOVE_RESULT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002161 work_line_->CopyResultRegister1(this, inst->VRegA_11x(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002162 break;
2163 case Instruction::MOVE_RESULT_WIDE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002164 work_line_->CopyResultRegister2(this, inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07002165 break;
2166 case Instruction::MOVE_RESULT_OBJECT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002167 work_line_->CopyResultRegister1(this, inst->VRegA_11x(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002168 break;
2169
Ian Rogersd81871c2011-10-03 13:57:23 -07002170 case Instruction::MOVE_EXCEPTION: {
Sebastien Hertz270a0e12015-01-16 19:49:09 +01002171 // We do not allow MOVE_EXCEPTION as the first instruction in a method. This is a simple case
2172 // where one entrypoint to the catch block is not actually an exception path.
2173 if (work_insn_idx_ == 0) {
2174 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "move-exception at pc 0x0";
2175 break;
2176 }
jeffhaobdb76512011-09-07 11:43:16 -07002177 /*
jeffhao60f83e32012-02-13 17:16:30 -08002178 * This statement can only appear as the first instruction in an exception handler. We verify
2179 * that as part of extracting the exception type from the catch block list.
jeffhaobdb76512011-09-07 11:43:16 -07002180 */
Ian Rogersd8f69b02014-09-10 21:43:52 +00002181 const RegType& res_type = GetCaughtExceptionType();
Andreas Gampead238ce2015-08-24 21:13:08 -07002182 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_11x(), res_type);
jeffhaobdb76512011-09-07 11:43:16 -07002183 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002184 }
jeffhaobdb76512011-09-07 11:43:16 -07002185 case Instruction::RETURN_VOID:
Andreas Gampef10b6e12015-08-12 10:48:12 -07002186 if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002187 if (!GetMethodReturnType().IsConflict()) {
jeffhaod5347e02012-03-22 17:25:05 -07002188 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07002189 }
jeffhaobdb76512011-09-07 11:43:16 -07002190 }
2191 break;
2192 case Instruction::RETURN:
Andreas Gampef10b6e12015-08-12 10:48:12 -07002193 if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) {
jeffhaobdb76512011-09-07 11:43:16 -07002194 /* check the method signature */
Ian Rogersd8f69b02014-09-10 21:43:52 +00002195 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07002196 if (!return_type.IsCategory1Types()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002197 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected non-category 1 return type "
2198 << return_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002199 } else {
2200 // Compilers may generate synthetic functions that write byte values into boolean fields.
2201 // Also, it may use integer values for boolean, byte, short, and character return types.
Sebastien Hertz5243e912013-05-21 10:55:07 +02002202 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07002203 const RegType& src_type = work_line_->GetRegisterType(this, vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07002204 bool use_src = ((return_type.IsBoolean() && src_type.IsByte()) ||
2205 ((return_type.IsBoolean() || return_type.IsByte() ||
2206 return_type.IsShort() || return_type.IsChar()) &&
2207 src_type.IsInteger()));
2208 /* check the register contents */
Ian Rogersad0b3a32012-04-16 14:50:24 -07002209 bool success =
Ian Rogers7b078e82014-09-10 14:44:24 -07002210 work_line_->VerifyRegisterType(this, vregA, use_src ? src_type : return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002211 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002212 AppendToLastFailMessage(StringPrintf(" return-1nr on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07002213 }
jeffhaobdb76512011-09-07 11:43:16 -07002214 }
2215 }
2216 break;
2217 case Instruction::RETURN_WIDE:
Andreas Gampef10b6e12015-08-12 10:48:12 -07002218 if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) {
jeffhaobdb76512011-09-07 11:43:16 -07002219 /* check the method signature */
Ian Rogersd8f69b02014-09-10 21:43:52 +00002220 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07002221 if (!return_type.IsCategory2Types()) {
jeffhaod5347e02012-03-22 17:25:05 -07002222 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-wide not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07002223 } else {
2224 /* check the register contents */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002225 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07002226 bool success = work_line_->VerifyRegisterType(this, vregA, return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002227 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002228 AppendToLastFailMessage(StringPrintf(" return-wide on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07002229 }
jeffhaobdb76512011-09-07 11:43:16 -07002230 }
2231 }
2232 break;
2233 case Instruction::RETURN_OBJECT:
Andreas Gampef10b6e12015-08-12 10:48:12 -07002234 if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) {
Ian Rogersd8f69b02014-09-10 21:43:52 +00002235 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07002236 if (!return_type.IsReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002237 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-object not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07002238 } else {
2239 /* return_type is the *expected* return type, not register value */
2240 DCHECK(!return_type.IsZero());
2241 DCHECK(!return_type.IsUninitializedReference());
Sebastien Hertz5243e912013-05-21 10:55:07 +02002242 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07002243 const RegType& reg_type = work_line_->GetRegisterType(this, vregA);
Andreas Gampea32210c2015-06-24 10:26:13 -07002244 // Disallow returning undefined, conflict & uninitialized values and verify that the
2245 // reference in vAA is an instance of the "return_type."
2246 if (reg_type.IsUndefined()) {
2247 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning undefined register";
2248 } else if (reg_type.IsConflict()) {
2249 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning register with conflict";
2250 } else if (reg_type.IsUninitializedTypes()) {
David Brazdil68b5c0b2016-01-19 14:25:29 +00002251 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning uninitialized object '"
Brian Carlstrom93c33962013-07-26 10:37:43 -07002252 << reg_type << "'";
Andreas Gampea4c98f22015-11-06 16:24:49 -08002253 } else if (!reg_type.IsReferenceTypes()) {
2254 // We really do expect a reference here.
2255 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-object returns a non-reference type "
2256 << reg_type;
David Brazdilca3c8c32016-09-06 14:04:48 +01002257 } else if (!return_type.IsAssignableFrom(reg_type, this)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07002258 if (reg_type.IsUnresolvedTypes() || return_type.IsUnresolvedTypes()) {
2259 Fail(VERIFY_ERROR_NO_CLASS) << " can't resolve returned type '" << return_type
2260 << "' or '" << reg_type << "'";
2261 } else {
Andreas Gampe16f149c2015-03-23 10:10:20 -07002262 bool soft_error = false;
2263 // Check whether arrays are involved. They will show a valid class status, even
2264 // if their components are erroneous.
2265 if (reg_type.IsArrayTypes() && return_type.IsArrayTypes()) {
David Brazdilca3c8c32016-09-06 14:04:48 +01002266 return_type.CanAssignArray(reg_type, reg_types_, class_loader_, this, &soft_error);
Andreas Gampe16f149c2015-03-23 10:10:20 -07002267 if (soft_error) {
2268 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "array with erroneous component type: "
2269 << reg_type << " vs " << return_type;
2270 }
2271 }
2272
2273 if (!soft_error) {
2274 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning '" << reg_type
2275 << "', but expected from declaration '" << return_type << "'";
2276 }
Jeff Haoa3faaf42013-09-03 19:07:00 -07002277 }
jeffhaobdb76512011-09-07 11:43:16 -07002278 }
2279 }
2280 }
2281 break;
2282
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07002283 /* could be boolean, int, float, or a null reference */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002284 case Instruction::CONST_4: {
2285 int32_t val = static_cast<int32_t>(inst->VRegB_11n() << 28) >> 28;
Andreas Gampead238ce2015-08-24 21:13:08 -07002286 work_line_->SetRegisterType<LockOp::kClear>(
2287 this, inst->VRegA_11n(), DetermineCat1Constant(val, need_precise_constants_));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07002288 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02002289 }
2290 case Instruction::CONST_16: {
2291 int16_t val = static_cast<int16_t>(inst->VRegB_21s());
Andreas Gampead238ce2015-08-24 21:13:08 -07002292 work_line_->SetRegisterType<LockOp::kClear>(
2293 this, inst->VRegA_21s(), DetermineCat1Constant(val, need_precise_constants_));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07002294 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02002295 }
Sebastien Hertz849600b2013-12-20 10:28:08 +01002296 case Instruction::CONST: {
2297 int32_t val = inst->VRegB_31i();
Andreas Gampead238ce2015-08-24 21:13:08 -07002298 work_line_->SetRegisterType<LockOp::kClear>(
2299 this, inst->VRegA_31i(), DetermineCat1Constant(val, need_precise_constants_));
jeffhaobdb76512011-09-07 11:43:16 -07002300 break;
Sebastien Hertz849600b2013-12-20 10:28:08 +01002301 }
2302 case Instruction::CONST_HIGH16: {
2303 int32_t val = static_cast<int32_t>(inst->VRegB_21h() << 16);
Andreas Gampead238ce2015-08-24 21:13:08 -07002304 work_line_->SetRegisterType<LockOp::kClear>(
2305 this, inst->VRegA_21h(), DetermineCat1Constant(val, need_precise_constants_));
jeffhaobdb76512011-09-07 11:43:16 -07002306 break;
Sebastien Hertz849600b2013-12-20 10:28:08 +01002307 }
jeffhaobdb76512011-09-07 11:43:16 -07002308 /* could be long or double; resolved upon use */
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002309 case Instruction::CONST_WIDE_16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002310 int64_t val = static_cast<int16_t>(inst->VRegB_21s());
Ian Rogersd8f69b02014-09-10 21:43:52 +00002311 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
2312 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07002313 work_line_->SetRegisterTypeWide(this, inst->VRegA_21s(), lo, hi);
jeffhaobdb76512011-09-07 11:43:16 -07002314 break;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002315 }
2316 case Instruction::CONST_WIDE_32: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002317 int64_t val = static_cast<int32_t>(inst->VRegB_31i());
Ian Rogersd8f69b02014-09-10 21:43:52 +00002318 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
2319 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07002320 work_line_->SetRegisterTypeWide(this, inst->VRegA_31i(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002321 break;
2322 }
2323 case Instruction::CONST_WIDE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002324 int64_t val = inst->VRegB_51l();
Ian Rogersd8f69b02014-09-10 21:43:52 +00002325 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
2326 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07002327 work_line_->SetRegisterTypeWide(this, inst->VRegA_51l(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002328 break;
2329 }
2330 case Instruction::CONST_WIDE_HIGH16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002331 int64_t val = static_cast<uint64_t>(inst->VRegB_21h()) << 48;
Ian Rogersd8f69b02014-09-10 21:43:52 +00002332 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
2333 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07002334 work_line_->SetRegisterTypeWide(this, inst->VRegA_21h(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002335 break;
2336 }
jeffhaobdb76512011-09-07 11:43:16 -07002337 case Instruction::CONST_STRING:
Andreas Gampead238ce2015-08-24 21:13:08 -07002338 work_line_->SetRegisterType<LockOp::kClear>(
2339 this, inst->VRegA_21c(), reg_types_.JavaLangString());
Sebastien Hertz5243e912013-05-21 10:55:07 +02002340 break;
jeffhaobdb76512011-09-07 11:43:16 -07002341 case Instruction::CONST_STRING_JUMBO:
Andreas Gampead238ce2015-08-24 21:13:08 -07002342 work_line_->SetRegisterType<LockOp::kClear>(
2343 this, inst->VRegA_31c(), reg_types_.JavaLangString());
jeffhaobdb76512011-09-07 11:43:16 -07002344 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002345 case Instruction::CONST_CLASS: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002346 // Get type from instruction if unresolved then we need an access check
2347 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Andreas Gampe98be1a92017-08-28 08:25:45 -07002348 const RegType& res_type = ResolveClass<CheckAccess::kYes>(dex::TypeIndex(inst->VRegB_21c()));
Ian Rogersad0b3a32012-04-16 14:50:24 -07002349 // Register holds class, ie its type is class, on error it will hold Conflict.
Andreas Gampead238ce2015-08-24 21:13:08 -07002350 work_line_->SetRegisterType<LockOp::kClear>(
2351 this, inst->VRegA_21c(), res_type.IsConflict() ? res_type
2352 : reg_types_.JavaLangClass());
jeffhaobdb76512011-09-07 11:43:16 -07002353 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002354 }
Orion Hodson2e599942017-09-22 16:17:41 +01002355 case Instruction::CONST_METHOD_HANDLE:
2356 work_line_->SetRegisterType<LockOp::kClear>(
2357 this, inst->VRegA_21c(), reg_types_.JavaLangInvokeMethodHandle());
2358 // TODO: add compiler support for const-method-{handle,type} (b/66890674)
2359 Fail(VERIFY_ERROR_FORCE_INTERPRETER);
2360 break;
2361 case Instruction::CONST_METHOD_TYPE:
2362 work_line_->SetRegisterType<LockOp::kClear>(
2363 this, inst->VRegA_21c(), reg_types_.JavaLangInvokeMethodType());
2364 // TODO: add compiler support for const-method-{handle,type} (b/66890674)
2365 Fail(VERIFY_ERROR_FORCE_INTERPRETER);
2366 break;
jeffhaobdb76512011-09-07 11:43:16 -07002367 case Instruction::MONITOR_ENTER:
Ian Rogers7b078e82014-09-10 14:44:24 -07002368 work_line_->PushMonitor(this, inst->VRegA_11x(), work_insn_idx_);
Andreas Gampec1474102015-08-18 08:57:44 -07002369 // Check whether the previous instruction is a move-object with vAA as a source, creating
2370 // untracked lock aliasing.
Mathieu Chartierde40d472015-10-15 17:47:48 -07002371 if (0 != work_insn_idx_ && !GetInstructionFlags(work_insn_idx_).IsBranchTarget()) {
Andreas Gampec1474102015-08-18 08:57:44 -07002372 uint32_t prev_idx = work_insn_idx_ - 1;
Mathieu Chartierde40d472015-10-15 17:47:48 -07002373 while (0 != prev_idx && !GetInstructionFlags(prev_idx).IsOpcode()) {
Andreas Gampec1474102015-08-18 08:57:44 -07002374 prev_idx--;
2375 }
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08002376 const Instruction& prev_inst = code_item_accessor_.InstructionAt(prev_idx);
Mathieu Chartier1d2d4ff2017-09-23 16:11:06 -07002377 switch (prev_inst.Opcode()) {
Andreas Gampec1474102015-08-18 08:57:44 -07002378 case Instruction::MOVE_OBJECT:
2379 case Instruction::MOVE_OBJECT_16:
2380 case Instruction::MOVE_OBJECT_FROM16:
Mathieu Chartier1d2d4ff2017-09-23 16:11:06 -07002381 if (prev_inst.VRegB() == inst->VRegA_11x()) {
Andreas Gampec1474102015-08-18 08:57:44 -07002382 // Redo the copy. This won't change the register types, but update the lock status
2383 // for the aliased register.
2384 work_line_->CopyRegister1(this,
Mathieu Chartier1d2d4ff2017-09-23 16:11:06 -07002385 prev_inst.VRegA(),
2386 prev_inst.VRegB(),
Andreas Gampec1474102015-08-18 08:57:44 -07002387 kTypeCategoryRef);
2388 }
2389 break;
2390
2391 default: // Other instruction types ignored.
2392 break;
2393 }
2394 }
jeffhaobdb76512011-09-07 11:43:16 -07002395 break;
2396 case Instruction::MONITOR_EXIT:
2397 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002398 * monitor-exit instructions are odd. They can throw exceptions,
jeffhaobdb76512011-09-07 11:43:16 -07002399 * but when they do they act as if they succeeded and the PC is
jeffhaod1f0fde2011-09-08 17:25:33 -07002400 * pointing to the following instruction. (This behavior goes back
jeffhaobdb76512011-09-07 11:43:16 -07002401 * to the need to handle asynchronous exceptions, a now-deprecated
2402 * feature that Dalvik doesn't support.)
2403 *
jeffhaod1f0fde2011-09-08 17:25:33 -07002404 * In practice we don't need to worry about this. The only
jeffhaobdb76512011-09-07 11:43:16 -07002405 * exceptions that can be thrown from monitor-exit are for a
jeffhaod1f0fde2011-09-08 17:25:33 -07002406 * null reference and -exit without a matching -enter. If the
jeffhaobdb76512011-09-07 11:43:16 -07002407 * structured locking checks are working, the former would have
2408 * failed on the -enter instruction, and the latter is impossible.
2409 *
2410 * This is fortunate, because issue 3221411 prevents us from
2411 * chasing the "can throw" path when monitor verification is
jeffhaod1f0fde2011-09-08 17:25:33 -07002412 * enabled. If we can fully verify the locking we can ignore
jeffhaobdb76512011-09-07 11:43:16 -07002413 * some catch blocks (which will show up as "dead" code when
2414 * we skip them here); if we can't, then the code path could be
2415 * "live" so we still need to check it.
2416 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002417 opcode_flags &= ~Instruction::kThrow;
Ian Rogers7b078e82014-09-10 14:44:24 -07002418 work_line_->PopMonitor(this, inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07002419 break;
Ian Rogers28ad40d2011-10-27 15:19:26 -07002420 case Instruction::CHECK_CAST:
Ian Rogersd81871c2011-10-03 13:57:23 -07002421 case Instruction::INSTANCE_OF: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002422 /*
2423 * If this instruction succeeds, we will "downcast" register vA to the type in vB. (This
2424 * could be a "upcast" -- not expected, so we don't try to address it.)
2425 *
2426 * If it fails, an exception is thrown, which we deal with later by ignoring the update to
Elliott Hughesadb8c672012-03-06 16:49:32 -08002427 * dec_insn.vA when branching to a handler.
Ian Rogers28ad40d2011-10-27 15:19:26 -07002428 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002429 const bool is_checkcast = (inst->Opcode() == Instruction::CHECK_CAST);
Andreas Gampea5b09a62016-11-17 15:21:22 -08002430 const dex::TypeIndex type_idx((is_checkcast) ? inst->VRegB_21c() : inst->VRegC_22c());
Andreas Gampe98be1a92017-08-28 08:25:45 -07002431 const RegType& res_type = ResolveClass<CheckAccess::kYes>(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002432 if (res_type.IsConflict()) {
Andreas Gampe00633eb2014-07-17 16:13:35 -07002433 // If this is a primitive type, fail HARD.
Vladimir Marko8d6768d2017-03-14 10:13:21 +00002434 ObjPtr<mirror::Class> klass =
2435 ClassLinker::LookupResolvedType(type_idx, dex_cache_.Get(), class_loader_.Get());
Andreas Gampe00633eb2014-07-17 16:13:35 -07002436 if (klass != nullptr && klass->IsPrimitive()) {
2437 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "using primitive type "
2438 << dex_file_->StringByTypeIdx(type_idx) << " in instanceof in "
2439 << GetDeclaringClass();
2440 break;
2441 }
2442
Ian Rogersad0b3a32012-04-16 14:50:24 -07002443 DCHECK_NE(failures_.size(), 0U);
2444 if (!is_checkcast) {
Andreas Gampead238ce2015-08-24 21:13:08 -07002445 work_line_->SetRegisterType<LockOp::kClear>(this,
2446 inst->VRegA_22c(),
2447 reg_types_.Boolean());
Ian Rogersad0b3a32012-04-16 14:50:24 -07002448 }
2449 break; // bad class
Ian Rogers9f1ab122011-12-12 08:52:43 -08002450 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002451 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Sebastien Hertz5243e912013-05-21 10:55:07 +02002452 uint32_t orig_type_reg = (is_checkcast) ? inst->VRegA_21c() : inst->VRegB_22c();
Ian Rogers7b078e82014-09-10 14:44:24 -07002453 const RegType& orig_type = work_line_->GetRegisterType(this, orig_type_reg);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002454 if (!res_type.IsNonZeroReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002455 if (is_checkcast) {
2456 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on unexpected class " << res_type;
2457 } else {
2458 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on unexpected class " << res_type;
2459 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002460 } else if (!orig_type.IsReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002461 if (is_checkcast) {
2462 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on non-reference in v" << orig_type_reg;
2463 } else {
2464 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on non-reference in v" << orig_type_reg;
2465 }
David Brazdil68b5c0b2016-01-19 14:25:29 +00002466 } else if (orig_type.IsUninitializedTypes()) {
2467 if (is_checkcast) {
2468 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on uninitialized reference in v"
2469 << orig_type_reg;
2470 } else {
2471 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on uninitialized reference in v"
2472 << orig_type_reg;
2473 }
jeffhao2a8a90e2011-09-26 14:25:31 -07002474 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002475 if (is_checkcast) {
Andreas Gampead238ce2015-08-24 21:13:08 -07002476 work_line_->SetRegisterType<LockOp::kKeep>(this, inst->VRegA_21c(), res_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07002477 } else {
Andreas Gampead238ce2015-08-24 21:13:08 -07002478 work_line_->SetRegisterType<LockOp::kClear>(this,
2479 inst->VRegA_22c(),
2480 reg_types_.Boolean());
jeffhaobdb76512011-09-07 11:43:16 -07002481 }
jeffhaobdb76512011-09-07 11:43:16 -07002482 }
jeffhao2a8a90e2011-09-26 14:25:31 -07002483 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002484 }
2485 case Instruction::ARRAY_LENGTH: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002486 const RegType& res_type = work_line_->GetRegisterType(this, inst->VRegB_12x());
Ian Rogers28ad40d2011-10-27 15:19:26 -07002487 if (res_type.IsReferenceTypes()) {
Ian Rogers89310de2012-02-01 13:47:30 -08002488 if (!res_type.IsArrayTypes() && !res_type.IsZero()) { // ie not an array or null
jeffhaod5347e02012-03-22 17:25:05 -07002489 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002490 } else {
Andreas Gampead238ce2015-08-24 21:13:08 -07002491 work_line_->SetRegisterType<LockOp::kClear>(this,
2492 inst->VRegA_12x(),
2493 reg_types_.Integer());
Ian Rogersd81871c2011-10-03 13:57:23 -07002494 }
Andreas Gampe65c9db82014-07-28 13:14:34 -07002495 } else {
2496 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002497 }
2498 break;
2499 }
2500 case Instruction::NEW_INSTANCE: {
Andreas Gampe98be1a92017-08-28 08:25:45 -07002501 const RegType& res_type = ResolveClass<CheckAccess::kYes>(dex::TypeIndex(inst->VRegB_21c()));
Ian Rogersad0b3a32012-04-16 14:50:24 -07002502 if (res_type.IsConflict()) {
2503 DCHECK_NE(failures_.size(), 0U);
2504 break; // bad class
jeffhao8cd6dda2012-02-22 10:15:34 -08002505 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002506 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
2507 // can't create an instance of an interface or abstract class */
2508 if (!res_type.IsInstantiableTypes()) {
2509 Fail(VERIFY_ERROR_INSTANTIATION)
2510 << "new-instance on primitive, interface or abstract class" << res_type;
Ian Rogers08f753d2012-08-24 14:35:25 -07002511 // Soft failure so carry on to set register type.
Ian Rogersd81871c2011-10-03 13:57:23 -07002512 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00002513 const RegType& uninit_type = reg_types_.Uninitialized(res_type, work_insn_idx_);
Ian Rogers08f753d2012-08-24 14:35:25 -07002514 // Any registers holding previous allocations from this address that have not yet been
2515 // initialized must be marked invalid.
Ian Rogers7b078e82014-09-10 14:44:24 -07002516 work_line_->MarkUninitRefsAsInvalid(this, uninit_type);
Ian Rogers08f753d2012-08-24 14:35:25 -07002517 // add the new uninitialized reference to the register state
Andreas Gampead238ce2015-08-24 21:13:08 -07002518 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_21c(), uninit_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07002519 break;
2520 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08002521 case Instruction::NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002522 VerifyNewArray(inst, false, false);
jeffhaobdb76512011-09-07 11:43:16 -07002523 break;
2524 case Instruction::FILLED_NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002525 VerifyNewArray(inst, true, false);
Ian Rogers0c4a5062012-02-03 15:18:59 -08002526 just_set_result = true; // Filled new array sets result register
jeffhaobdb76512011-09-07 11:43:16 -07002527 break;
Ian Rogers0c4a5062012-02-03 15:18:59 -08002528 case Instruction::FILLED_NEW_ARRAY_RANGE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002529 VerifyNewArray(inst, true, true);
Ian Rogers0c4a5062012-02-03 15:18:59 -08002530 just_set_result = true; // Filled new array range sets result register
2531 break;
jeffhaobdb76512011-09-07 11:43:16 -07002532 case Instruction::CMPL_FLOAT:
2533 case Instruction::CMPG_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002534 if (!work_line_->VerifyRegisterType(this, inst->VRegB_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08002535 break;
2536 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002537 if (!work_line_->VerifyRegisterType(this, inst->VRegC_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08002538 break;
2539 }
Andreas Gampead238ce2015-08-24 21:13:08 -07002540 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002541 break;
2542 case Instruction::CMPL_DOUBLE:
2543 case Instruction::CMPG_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002544 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegB_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002545 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002546 break;
2547 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002548 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegC_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002549 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002550 break;
2551 }
Andreas Gampead238ce2015-08-24 21:13:08 -07002552 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002553 break;
2554 case Instruction::CMP_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002555 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegB_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002556 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002557 break;
2558 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002559 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegC_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002560 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002561 break;
2562 }
Andreas Gampead238ce2015-08-24 21:13:08 -07002563 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002564 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002565 case Instruction::THROW: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002566 const RegType& res_type = work_line_->GetRegisterType(this, inst->VRegA_11x());
David Brazdilca3c8c32016-09-06 14:04:48 +01002567 if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(res_type, this)) {
David Brazdil68b5c0b2016-01-19 14:25:29 +00002568 if (res_type.IsUninitializedTypes()) {
2569 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "thrown exception not initialized";
Pavel Vyssotski980027c2016-02-11 20:28:11 +06002570 } else if (!res_type.IsReferenceTypes()) {
2571 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "thrown value of non-reference type " << res_type;
David Brazdil68b5c0b2016-01-19 14:25:29 +00002572 } else {
2573 Fail(res_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS : VERIFY_ERROR_BAD_CLASS_SOFT)
2574 << "thrown class " << res_type << " not instanceof Throwable";
2575 }
jeffhaobdb76512011-09-07 11:43:16 -07002576 }
2577 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002578 }
jeffhaobdb76512011-09-07 11:43:16 -07002579 case Instruction::GOTO:
2580 case Instruction::GOTO_16:
2581 case Instruction::GOTO_32:
2582 /* no effect on or use of registers */
2583 break;
2584
2585 case Instruction::PACKED_SWITCH:
2586 case Instruction::SPARSE_SWITCH:
2587 /* verify that vAA is an integer, or can be converted to one */
Ian Rogers7b078e82014-09-10 14:44:24 -07002588 work_line_->VerifyRegisterType(this, inst->VRegA_31t(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002589 break;
2590
Ian Rogersd81871c2011-10-03 13:57:23 -07002591 case Instruction::FILL_ARRAY_DATA: {
2592 /* Similar to the verification done for APUT */
Ian Rogers7b078e82014-09-10 14:44:24 -07002593 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegA_31t());
Ian Rogers89310de2012-02-01 13:47:30 -08002594 /* array_type can be null if the reg type is Zero */
2595 if (!array_type.IsZero()) {
jeffhao457cc512012-02-02 16:55:13 -08002596 if (!array_type.IsArrayTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002597 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with array type "
2598 << array_type;
Andreas Gampebb18a032016-03-22 20:34:25 -07002599 } else if (array_type.IsUnresolvedTypes()) {
2600 // If it's an unresolved array type, it must be non-primitive.
2601 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data for array of type "
2602 << array_type;
Ian Rogers89310de2012-02-01 13:47:30 -08002603 } else {
Ian Rogersd8f69b02014-09-10 21:43:52 +00002604 const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
Ian Rogersad0b3a32012-04-16 14:50:24 -07002605 DCHECK(!component_type.IsConflict());
jeffhao457cc512012-02-02 16:55:13 -08002606 if (component_type.IsNonZeroReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002607 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with component type "
2608 << component_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002609 } else {
jeffhao457cc512012-02-02 16:55:13 -08002610 // Now verify if the element width in the table matches the element width declared in
2611 // the array
Andreas Gampe53de99c2015-08-17 13:43:55 -07002612 const uint16_t* array_data =
2613 insns + (insns[1] | (static_cast<int32_t>(insns[2]) << 16));
jeffhao457cc512012-02-02 16:55:13 -08002614 if (array_data[0] != Instruction::kArrayDataSignature) {
jeffhaod5347e02012-03-22 17:25:05 -07002615 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid magic for array-data";
jeffhao457cc512012-02-02 16:55:13 -08002616 } else {
2617 size_t elem_width = Primitive::ComponentSize(component_type.GetPrimitiveType());
2618 // Since we don't compress the data in Dex, expect to see equal width of data stored
2619 // in the table and expected from the array class.
2620 if (array_data[1] != elem_width) {
jeffhaod5347e02012-03-22 17:25:05 -07002621 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-data size mismatch (" << array_data[1]
2622 << " vs " << elem_width << ")";
jeffhao457cc512012-02-02 16:55:13 -08002623 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002624 }
2625 }
jeffhaobdb76512011-09-07 11:43:16 -07002626 }
2627 }
2628 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002629 }
jeffhaobdb76512011-09-07 11:43:16 -07002630 case Instruction::IF_EQ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002631 case Instruction::IF_NE: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002632 const RegType& reg_type1 = work_line_->GetRegisterType(this, inst->VRegA_22t());
2633 const RegType& reg_type2 = work_line_->GetRegisterType(this, inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002634 bool mismatch = false;
2635 if (reg_type1.IsZero()) { // zero then integral or reference expected
2636 mismatch = !reg_type2.IsReferenceTypes() && !reg_type2.IsIntegralTypes();
2637 } else if (reg_type1.IsReferenceTypes()) { // both references?
2638 mismatch = !reg_type2.IsReferenceTypes();
2639 } else { // both integral?
2640 mismatch = !reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes();
2641 }
2642 if (mismatch) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002643 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to if-eq/if-ne (" << reg_type1 << ","
2644 << reg_type2 << ") must both be references or integral";
jeffhaobdb76512011-09-07 11:43:16 -07002645 }
2646 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002647 }
jeffhaobdb76512011-09-07 11:43:16 -07002648 case Instruction::IF_LT:
2649 case Instruction::IF_GE:
2650 case Instruction::IF_GT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002651 case Instruction::IF_LE: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002652 const RegType& reg_type1 = work_line_->GetRegisterType(this, inst->VRegA_22t());
2653 const RegType& reg_type2 = work_line_->GetRegisterType(this, inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002654 if (!reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002655 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to 'if' (" << reg_type1 << ","
2656 << reg_type2 << ") must be integral";
jeffhaobdb76512011-09-07 11:43:16 -07002657 }
2658 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002659 }
jeffhaobdb76512011-09-07 11:43:16 -07002660 case Instruction::IF_EQZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002661 case Instruction::IF_NEZ: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002662 const RegType& reg_type = work_line_->GetRegisterType(this, inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002663 if (!reg_type.IsReferenceTypes() && !reg_type.IsIntegralTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002664 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
2665 << " unexpected as arg to if-eqz/if-nez";
Ian Rogersd81871c2011-10-03 13:57:23 -07002666 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002667
2668 // Find previous instruction - its existence is a precondition to peephole optimization.
Ian Rogers9b360392013-06-06 14:45:07 -07002669 uint32_t instance_of_idx = 0;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002670 if (0 != work_insn_idx_) {
Ian Rogers9b360392013-06-06 14:45:07 -07002671 instance_of_idx = work_insn_idx_ - 1;
Mathieu Chartierde40d472015-10-15 17:47:48 -07002672 while (0 != instance_of_idx && !GetInstructionFlags(instance_of_idx).IsOpcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07002673 instance_of_idx--;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002674 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07002675 if (FailOrAbort(this, GetInstructionFlags(instance_of_idx).IsOpcode(),
Andreas Gampe7c038102014-10-27 20:08:46 -07002676 "Unable to get previous instruction of if-eqz/if-nez for work index ",
2677 work_insn_idx_)) {
2678 break;
2679 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002680 } else {
2681 break;
2682 }
2683
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08002684 const Instruction& instance_of_inst = code_item_accessor_.InstructionAt(instance_of_idx);
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002685
2686 /* Check for peep-hole pattern of:
2687 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002688 * instance-of vX, vY, T;
2689 * ifXXX vX, label ;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002690 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002691 * label:
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002692 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002693 * and sharpen the type of vY to be type T.
2694 * Note, this pattern can't be if:
2695 * - if there are other branches to this branch,
2696 * - when vX == vY.
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002697 */
Ian Rogersfae370a2013-06-05 08:33:27 -07002698 if (!CurrentInsnFlags()->IsBranchTarget() &&
Vladimir Markod7559b72017-09-28 13:50:37 +01002699 (Instruction::INSTANCE_OF == instance_of_inst.Opcode()) &&
2700 (inst->VRegA_21t() == instance_of_inst.VRegA_22c()) &&
2701 (instance_of_inst.VRegA_22c() != instance_of_inst.VRegB_22c())) {
Ian Rogersebbdd872014-07-07 23:53:08 -07002702 // Check the type of the instance-of is different than that of registers type, as if they
2703 // are the same there is no work to be done here. Check that the conversion is not to or
2704 // from an unresolved type as type information is imprecise. If the instance-of is to an
2705 // interface then ignore the type information as interfaces can only be treated as Objects
2706 // and we don't want to disallow field and other operations on the object. If the value
2707 // being instance-of checked against is known null (zero) then allow the optimization as
2708 // we didn't have type information. If the merge of the instance-of type with the original
2709 // type is assignable to the original then allow optimization. This check is performed to
2710 // ensure that subsequent merges don't lose type information - such as becoming an
2711 // interface from a class that would lose information relevant to field checks.
Vladimir Markod7559b72017-09-28 13:50:37 +01002712 const RegType& orig_type = work_line_->GetRegisterType(this, instance_of_inst.VRegB_22c());
Andreas Gampe98be1a92017-08-28 08:25:45 -07002713 const RegType& cast_type = ResolveClass<CheckAccess::kYes>(
Vladimir Markod7559b72017-09-28 13:50:37 +01002714 dex::TypeIndex(instance_of_inst.VRegC_22c()));
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002715
Ian Rogersebbdd872014-07-07 23:53:08 -07002716 if (!orig_type.Equals(cast_type) &&
2717 !cast_type.IsUnresolvedTypes() && !orig_type.IsUnresolvedTypes() &&
Andreas Gampe00633eb2014-07-17 16:13:35 -07002718 cast_type.HasClass() && // Could be conflict type, make sure it has a class.
Ian Rogersebbdd872014-07-07 23:53:08 -07002719 !cast_type.GetClass()->IsInterface() &&
2720 (orig_type.IsZero() ||
David Brazdilca3c8c32016-09-06 14:04:48 +01002721 orig_type.IsStrictlyAssignableFrom(
2722 cast_type.Merge(orig_type, &reg_types_, this), this))) {
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08002723 RegisterLine* update_line = RegisterLine::Create(code_item_accessor_.RegistersSize(),
2724 this);
Ian Rogersfae370a2013-06-05 08:33:27 -07002725 if (inst->Opcode() == Instruction::IF_EQZ) {
Ian Rogers9b360392013-06-06 14:45:07 -07002726 fallthrough_line.reset(update_line);
Ian Rogersfae370a2013-06-05 08:33:27 -07002727 } else {
Ian Rogers9b360392013-06-06 14:45:07 -07002728 branch_line.reset(update_line);
2729 }
2730 update_line->CopyFromLine(work_line_.get());
Andreas Gampead238ce2015-08-24 21:13:08 -07002731 update_line->SetRegisterType<LockOp::kKeep>(this,
Vladimir Markod7559b72017-09-28 13:50:37 +01002732 instance_of_inst.VRegB_22c(),
Andreas Gampead238ce2015-08-24 21:13:08 -07002733 cast_type);
Mathieu Chartierde40d472015-10-15 17:47:48 -07002734 if (!GetInstructionFlags(instance_of_idx).IsBranchTarget() && 0 != instance_of_idx) {
Ian Rogers9b360392013-06-06 14:45:07 -07002735 // See if instance-of was preceded by a move-object operation, common due to the small
2736 // register encoding space of instance-of, and propagate type information to the source
2737 // of the move-object.
2738 uint32_t move_idx = instance_of_idx - 1;
Mathieu Chartierde40d472015-10-15 17:47:48 -07002739 while (0 != move_idx && !GetInstructionFlags(move_idx).IsOpcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07002740 move_idx--;
2741 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07002742 if (FailOrAbort(this, GetInstructionFlags(move_idx).IsOpcode(),
Andreas Gampe7c038102014-10-27 20:08:46 -07002743 "Unable to get previous instruction of if-eqz/if-nez for work index ",
2744 work_insn_idx_)) {
2745 break;
2746 }
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08002747 const Instruction& move_inst = code_item_accessor_.InstructionAt(move_idx);
Vladimir Markod7559b72017-09-28 13:50:37 +01002748 switch (move_inst.Opcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07002749 case Instruction::MOVE_OBJECT:
Vladimir Markod7559b72017-09-28 13:50:37 +01002750 if (move_inst.VRegA_12x() == instance_of_inst.VRegB_22c()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07002751 update_line->SetRegisterType<LockOp::kKeep>(this,
Vladimir Markod7559b72017-09-28 13:50:37 +01002752 move_inst.VRegB_12x(),
Andreas Gampead238ce2015-08-24 21:13:08 -07002753 cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002754 }
2755 break;
2756 case Instruction::MOVE_OBJECT_FROM16:
Vladimir Markod7559b72017-09-28 13:50:37 +01002757 if (move_inst.VRegA_22x() == instance_of_inst.VRegB_22c()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07002758 update_line->SetRegisterType<LockOp::kKeep>(this,
Vladimir Markod7559b72017-09-28 13:50:37 +01002759 move_inst.VRegB_22x(),
Andreas Gampead238ce2015-08-24 21:13:08 -07002760 cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002761 }
2762 break;
2763 case Instruction::MOVE_OBJECT_16:
Vladimir Markod7559b72017-09-28 13:50:37 +01002764 if (move_inst.VRegA_32x() == instance_of_inst.VRegB_22c()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07002765 update_line->SetRegisterType<LockOp::kKeep>(this,
Vladimir Markod7559b72017-09-28 13:50:37 +01002766 move_inst.VRegB_32x(),
Andreas Gampead238ce2015-08-24 21:13:08 -07002767 cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002768 }
2769 break;
2770 default:
2771 break;
2772 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002773 }
2774 }
2775 }
2776
jeffhaobdb76512011-09-07 11:43:16 -07002777 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002778 }
jeffhaobdb76512011-09-07 11:43:16 -07002779 case Instruction::IF_LTZ:
2780 case Instruction::IF_GEZ:
2781 case Instruction::IF_GTZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002782 case Instruction::IF_LEZ: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002783 const RegType& reg_type = work_line_->GetRegisterType(this, inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002784 if (!reg_type.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002785 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
2786 << " unexpected as arg to if-ltz/if-gez/if-gtz/if-lez";
Ian Rogersd81871c2011-10-03 13:57:23 -07002787 }
jeffhaobdb76512011-09-07 11:43:16 -07002788 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002789 }
jeffhaobdb76512011-09-07 11:43:16 -07002790 case Instruction::AGET_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002791 VerifyAGet(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002792 break;
jeffhaobdb76512011-09-07 11:43:16 -07002793 case Instruction::AGET_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002794 VerifyAGet(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002795 break;
jeffhaobdb76512011-09-07 11:43:16 -07002796 case Instruction::AGET_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002797 VerifyAGet(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002798 break;
jeffhaobdb76512011-09-07 11:43:16 -07002799 case Instruction::AGET_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002800 VerifyAGet(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002801 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002802 case Instruction::AGET:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002803 VerifyAGet(inst, reg_types_.Integer(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002804 break;
jeffhaobdb76512011-09-07 11:43:16 -07002805 case Instruction::AGET_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002806 VerifyAGet(inst, reg_types_.LongLo(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002807 break;
2808 case Instruction::AGET_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002809 VerifyAGet(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07002810 break;
2811
Ian Rogersd81871c2011-10-03 13:57:23 -07002812 case Instruction::APUT_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002813 VerifyAPut(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002814 break;
2815 case Instruction::APUT_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002816 VerifyAPut(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002817 break;
2818 case Instruction::APUT_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002819 VerifyAPut(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002820 break;
2821 case Instruction::APUT_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002822 VerifyAPut(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002823 break;
2824 case Instruction::APUT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002825 VerifyAPut(inst, reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002826 break;
2827 case Instruction::APUT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002828 VerifyAPut(inst, reg_types_.LongLo(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002829 break;
2830 case Instruction::APUT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002831 VerifyAPut(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07002832 break;
2833
jeffhaobdb76512011-09-07 11:43:16 -07002834 case Instruction::IGET_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002835 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002836 break;
jeffhaobdb76512011-09-07 11:43:16 -07002837 case Instruction::IGET_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002838 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002839 break;
jeffhaobdb76512011-09-07 11:43:16 -07002840 case Instruction::IGET_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002841 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002842 break;
jeffhaobdb76512011-09-07 11:43:16 -07002843 case Instruction::IGET_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002844 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002845 break;
2846 case Instruction::IGET:
Andreas Gampe896df402014-10-20 22:25:29 -07002847 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002848 break;
2849 case Instruction::IGET_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002850 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002851 break;
2852 case Instruction::IGET_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002853 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false,
2854 false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002855 break;
jeffhaobdb76512011-09-07 11:43:16 -07002856
Ian Rogersd81871c2011-10-03 13:57:23 -07002857 case Instruction::IPUT_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002858 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002859 break;
2860 case Instruction::IPUT_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002861 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002862 break;
2863 case Instruction::IPUT_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002864 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002865 break;
2866 case Instruction::IPUT_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002867 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002868 break;
2869 case Instruction::IPUT:
Andreas Gampe896df402014-10-20 22:25:29 -07002870 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002871 break;
2872 case Instruction::IPUT_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002873 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002874 break;
jeffhaobdb76512011-09-07 11:43:16 -07002875 case Instruction::IPUT_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002876 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false,
2877 false);
jeffhaobdb76512011-09-07 11:43:16 -07002878 break;
2879
jeffhaobdb76512011-09-07 11:43:16 -07002880 case Instruction::SGET_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002881 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002882 break;
jeffhaobdb76512011-09-07 11:43:16 -07002883 case Instruction::SGET_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002884 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002885 break;
jeffhaobdb76512011-09-07 11:43:16 -07002886 case Instruction::SGET_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002887 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002888 break;
jeffhaobdb76512011-09-07 11:43:16 -07002889 case Instruction::SGET_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002890 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002891 break;
2892 case Instruction::SGET:
Andreas Gampe896df402014-10-20 22:25:29 -07002893 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002894 break;
2895 case Instruction::SGET_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002896 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002897 break;
2898 case Instruction::SGET_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002899 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false,
2900 true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002901 break;
2902
2903 case Instruction::SPUT_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002904 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002905 break;
2906 case Instruction::SPUT_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002907 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002908 break;
2909 case Instruction::SPUT_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002910 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002911 break;
2912 case Instruction::SPUT_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002913 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002914 break;
2915 case Instruction::SPUT:
Andreas Gampe896df402014-10-20 22:25:29 -07002916 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002917 break;
2918 case Instruction::SPUT_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002919 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002920 break;
2921 case Instruction::SPUT_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002922 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false,
2923 true);
jeffhaobdb76512011-09-07 11:43:16 -07002924 break;
2925
2926 case Instruction::INVOKE_VIRTUAL:
2927 case Instruction::INVOKE_VIRTUAL_RANGE:
2928 case Instruction::INVOKE_SUPER:
Ian Rogersd81871c2011-10-03 13:57:23 -07002929 case Instruction::INVOKE_SUPER_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002930 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE ||
2931 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002932 bool is_super = (inst->Opcode() == Instruction::INVOKE_SUPER ||
2933 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
Alex Light7268d472016-01-20 15:50:01 -08002934 MethodType type = is_super ? METHOD_SUPER : METHOD_VIRTUAL;
2935 ArtMethod* called_method = VerifyInvocationArgs(inst, type, is_range);
Ian Rogersd8f69b02014-09-10 21:43:52 +00002936 const RegType* return_type = nullptr;
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002937 if (called_method != nullptr) {
Vladimir Markob45528c2017-07-27 14:14:28 +01002938 ObjPtr<mirror::Class> return_type_class = can_load_classes_
2939 ? called_method->ResolveReturnType()
2940 : called_method->LookupResolvedReturnType();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002941 if (return_type_class != nullptr) {
Andreas Gampef23f33d2015-06-23 14:18:17 -07002942 return_type = &FromClass(called_method->GetReturnTypeDescriptor(),
Vladimir Markob45528c2017-07-27 14:14:28 +01002943 return_type_class.Ptr(),
Andreas Gampef23f33d2015-06-23 14:18:17 -07002944 return_type_class->CannotBeAssignedFromOtherTypes());
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002945 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07002946 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
2947 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002948 }
2949 }
2950 if (return_type == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002951 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002952 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
Andreas Gampea5b09a62016-11-17 15:21:22 -08002953 dex::TypeIndex return_type_idx =
2954 dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002955 const char* descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Mathieu Chartierbf99f772014-08-23 16:37:27 -07002956 return_type = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
jeffhaobdb76512011-09-07 11:43:16 -07002957 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002958 if (!return_type->IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002959 work_line_->SetResultRegisterType(this, *return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002960 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002961 work_line_->SetResultRegisterTypeWide(*return_type, return_type->HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002962 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002963 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002964 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002965 }
jeffhaobdb76512011-09-07 11:43:16 -07002966 case Instruction::INVOKE_DIRECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002967 case Instruction::INVOKE_DIRECT_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002968 bool is_range = (inst->Opcode() == Instruction::INVOKE_DIRECT_RANGE);
Alex Light7268d472016-01-20 15:50:01 -08002969 ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_DIRECT, is_range);
Ian Rogers46685432012-06-03 22:26:43 -07002970 const char* return_type_descriptor;
2971 bool is_constructor;
Ian Rogersd8f69b02014-09-10 21:43:52 +00002972 const RegType* return_type = nullptr;
Ian Rogers7b078e82014-09-10 14:44:24 -07002973 if (called_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002974 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogers46685432012-06-03 22:26:43 -07002975 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
Ian Rogersdfb325e2013-10-30 01:00:44 -07002976 is_constructor = strcmp("<init>", dex_file_->StringDataByIdx(method_id.name_idx_)) == 0;
Andreas Gampea5b09a62016-11-17 15:21:22 -08002977 dex::TypeIndex return_type_idx =
2978 dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogers46685432012-06-03 22:26:43 -07002979 return_type_descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2980 } else {
2981 is_constructor = called_method->IsConstructor();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002982 return_type_descriptor = called_method->GetReturnTypeDescriptor();
Vladimir Markob45528c2017-07-27 14:14:28 +01002983 ObjPtr<mirror::Class> return_type_class = can_load_classes_
2984 ? called_method->ResolveReturnType()
2985 : called_method->LookupResolvedReturnType();
Ian Rogers1ff3c982014-08-12 02:30:58 -07002986 if (return_type_class != nullptr) {
Andreas Gampef23f33d2015-06-23 14:18:17 -07002987 return_type = &FromClass(return_type_descriptor,
Vladimir Markob45528c2017-07-27 14:14:28 +01002988 return_type_class.Ptr(),
Andreas Gampef23f33d2015-06-23 14:18:17 -07002989 return_type_class->CannotBeAssignedFromOtherTypes());
Ian Rogers1ff3c982014-08-12 02:30:58 -07002990 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07002991 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
2992 self_->ClearException();
Ian Rogers1ff3c982014-08-12 02:30:58 -07002993 }
Ian Rogers46685432012-06-03 22:26:43 -07002994 }
2995 if (is_constructor) {
jeffhaobdb76512011-09-07 11:43:16 -07002996 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002997 * Some additional checks when calling a constructor. We know from the invocation arg check
2998 * that the "this" argument is an instance of called_method->klass. Now we further restrict
2999 * that to require that called_method->klass is the same as this->klass or this->super,
3000 * allowing the latter only if the "this" argument is the same as the "this" argument to
3001 * this method (which implies that we're in a constructor ourselves).
jeffhaobdb76512011-09-07 11:43:16 -07003002 */
Orion Hodsoncfa325e2016-10-13 10:25:54 +01003003 const RegType& this_type = work_line_->GetInvocationThis(this, inst);
jeffhaob57e9522012-04-26 18:08:21 -07003004 if (this_type.IsConflict()) // failure.
3005 break;
jeffhaobdb76512011-09-07 11:43:16 -07003006
jeffhaob57e9522012-04-26 18:08:21 -07003007 /* no null refs allowed (?) */
3008 if (this_type.IsZero()) {
3009 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unable to initialize null ref";
3010 break;
jeffhao2a8a90e2011-09-26 14:25:31 -07003011 }
jeffhaob57e9522012-04-26 18:08:21 -07003012
3013 /* must be in same class or in superclass */
Ian Rogersd8f69b02014-09-10 21:43:52 +00003014 // const RegType& this_super_klass = this_type.GetSuperClass(&reg_types_);
Ian Rogers46685432012-06-03 22:26:43 -07003015 // TODO: re-enable constructor type verification
3016 // if (this_super_klass.IsConflict()) {
jeffhaob57e9522012-04-26 18:08:21 -07003017 // Unknown super class, fail so we re-check at runtime.
Ian Rogers46685432012-06-03 22:26:43 -07003018 // Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "super class unknown for '" << this_type << "'";
3019 // break;
3020 // }
jeffhaob57e9522012-04-26 18:08:21 -07003021
3022 /* arg must be an uninitialized reference */
3023 if (!this_type.IsUninitializedTypes()) {
3024 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Expected initialization on uninitialized reference "
3025 << this_type;
3026 break;
3027 }
3028
3029 /*
3030 * Replace the uninitialized reference with an initialized one. We need to do this for all
3031 * registers that have the same object instance in them, not just the "this" register.
3032 */
Nicolas Geoffray98e6ce42016-02-16 18:42:15 +00003033 work_line_->MarkRefsAsInitialized(this, this_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003034 }
Ian Rogers1ff3c982014-08-12 02:30:58 -07003035 if (return_type == nullptr) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07003036 return_type = &reg_types_.FromDescriptor(GetClassLoader(), return_type_descriptor, false);
Ian Rogers1ff3c982014-08-12 02:30:58 -07003037 }
3038 if (!return_type->IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003039 work_line_->SetResultRegisterType(this, *return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003040 } else {
Ian Rogers1ff3c982014-08-12 02:30:58 -07003041 work_line_->SetResultRegisterTypeWide(*return_type, return_type->HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003042 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003043 just_set_result = true;
3044 break;
3045 }
3046 case Instruction::INVOKE_STATIC:
3047 case Instruction::INVOKE_STATIC_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003048 bool is_range = (inst->Opcode() == Instruction::INVOKE_STATIC_RANGE);
Alex Light7268d472016-01-20 15:50:01 -08003049 ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_STATIC, is_range);
Ian Rogers28ad40d2011-10-27 15:19:26 -07003050 const char* descriptor;
Ian Rogers7b078e82014-09-10 14:44:24 -07003051 if (called_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003052 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogers28ad40d2011-10-27 15:19:26 -07003053 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
Andreas Gampea5b09a62016-11-17 15:21:22 -08003054 dex::TypeIndex return_type_idx =
3055 dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Mathieu Chartier590fee92013-09-13 13:46:47 -07003056 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07003057 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003058 descriptor = called_method->GetReturnTypeDescriptor();
Ian Rogers28ad40d2011-10-27 15:19:26 -07003059 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00003060 const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003061 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003062 work_line_->SetResultRegisterType(this, return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003063 } else {
3064 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
3065 }
jeffhaobdb76512011-09-07 11:43:16 -07003066 just_set_result = true;
3067 }
3068 break;
jeffhaobdb76512011-09-07 11:43:16 -07003069 case Instruction::INVOKE_INTERFACE:
Ian Rogersd81871c2011-10-03 13:57:23 -07003070 case Instruction::INVOKE_INTERFACE_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003071 bool is_range = (inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE);
Alex Light7268d472016-01-20 15:50:01 -08003072 ArtMethod* abs_method = VerifyInvocationArgs(inst, METHOD_INTERFACE, is_range);
Ian Rogers7b078e82014-09-10 14:44:24 -07003073 if (abs_method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003074 mirror::Class* called_interface = abs_method->GetDeclaringClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07003075 if (!called_interface->IsInterface() && !called_interface->IsObjectClass()) {
3076 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected interface class in invoke-interface '"
David Sehr709b0702016-10-13 09:12:37 -07003077 << abs_method->PrettyMethod() << "'";
Ian Rogersad0b3a32012-04-16 14:50:24 -07003078 break;
Ian Rogers28ad40d2011-10-27 15:19:26 -07003079 }
Ian Rogers0d604842012-04-16 14:50:24 -07003080 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003081 /* Get the type of the "this" arg, which should either be a sub-interface of called
3082 * interface or Object (see comments in RegType::JoinClass).
3083 */
Orion Hodsoncfa325e2016-10-13 10:25:54 +01003084 const RegType& this_type = work_line_->GetInvocationThis(this, inst);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003085 if (this_type.IsZero()) {
3086 /* null pointer always passes (and always fails at runtime) */
3087 } else {
3088 if (this_type.IsUninitializedTypes()) {
3089 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface call on uninitialized object "
3090 << this_type;
3091 break;
3092 }
3093 // In the past we have tried to assert that "called_interface" is assignable
3094 // from "this_type.GetClass()", however, as we do an imprecise Join
3095 // (RegType::JoinClass) we don't have full information on what interfaces are
3096 // implemented by "this_type". For example, two classes may implement the same
3097 // interfaces and have a common parent that doesn't implement the interface. The
3098 // join will set "this_type" to the parent class and a test that this implements
3099 // the interface will incorrectly fail.
3100 }
3101 /*
3102 * We don't have an object instance, so we can't find the concrete method. However, all of
3103 * the type information is in the abstract method, so we're good.
3104 */
3105 const char* descriptor;
Ian Rogers7b078e82014-09-10 14:44:24 -07003106 if (abs_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003107 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07003108 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
Andreas Gampea5b09a62016-11-17 15:21:22 -08003109 dex::TypeIndex return_type_idx =
3110 dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Mathieu Chartierde40d472015-10-15 17:47:48 -07003111 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003112 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003113 descriptor = abs_method->GetReturnTypeDescriptor();
Ian Rogersad0b3a32012-04-16 14:50:24 -07003114 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00003115 const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003116 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003117 work_line_->SetResultRegisterType(this, return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003118 } else {
3119 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
3120 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003121 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07003122 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07003123 }
Narayan Kamath9823e782016-08-03 12:46:58 +01003124 case Instruction::INVOKE_POLYMORPHIC:
3125 case Instruction::INVOKE_POLYMORPHIC_RANGE: {
Orion Hodsoncfa325e2016-10-13 10:25:54 +01003126 bool is_range = (inst->Opcode() == Instruction::INVOKE_POLYMORPHIC_RANGE);
3127 ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_POLYMORPHIC, is_range);
3128 if (called_method == nullptr) {
3129 // Convert potential soft failures in VerifyInvocationArgs() to hard errors.
3130 if (failure_messages_.size() > 0) {
3131 std::string message = failure_messages_.back()->str();
3132 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << message;
3133 } else {
3134 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invoke-polymorphic verification failure.";
3135 }
3136 break;
3137 }
3138 if (!CheckSignaturePolymorphicMethod(called_method) ||
3139 !CheckSignaturePolymorphicReceiver(inst)) {
3140 break;
3141 }
3142 const uint32_t proto_idx = (is_range) ? inst->VRegH_4rcc() : inst->VRegH_45cc();
Orion Hodsonac141392017-01-13 11:53:47 +00003143 const char* return_descriptor =
Orion Hodsoncfa325e2016-10-13 10:25:54 +01003144 dex_file_->GetReturnTypeDescriptor(dex_file_->GetProtoId(proto_idx));
3145 const RegType& return_type =
Orion Hodsonac141392017-01-13 11:53:47 +00003146 reg_types_.FromDescriptor(GetClassLoader(), return_descriptor, false);
Orion Hodsoncfa325e2016-10-13 10:25:54 +01003147 if (!return_type.IsLowHalf()) {
3148 work_line_->SetResultRegisterType(this, return_type);
3149 } else {
3150 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
3151 }
Orion Hodsonac141392017-01-13 11:53:47 +00003152 just_set_result = true;
Orion Hodsoncfa325e2016-10-13 10:25:54 +01003153 break;
Narayan Kamath9823e782016-08-03 12:46:58 +01003154 }
Orion Hodsonc069a302017-01-18 09:23:12 +00003155 case Instruction::INVOKE_CUSTOM:
3156 case Instruction::INVOKE_CUSTOM_RANGE: {
3157 // Verify registers based on method_type in the call site.
3158 bool is_range = (inst->Opcode() == Instruction::INVOKE_CUSTOM_RANGE);
3159
3160 // Step 1. Check the call site that produces the method handle for invocation
3161 const uint32_t call_site_idx = is_range ? inst->VRegB_3rc() : inst->VRegB_35c();
3162 if (!CheckCallSite(call_site_idx)) {
3163 DCHECK(HasFailures());
3164 break;
3165 }
3166
3167 // Step 2. Check the register arguments correspond to the expected arguments for the
3168 // method handle produced by step 1. The dex file verifier has checked ranges for
3169 // the first three arguments and CheckCallSite has checked the method handle type.
3170 CallSiteArrayValueIterator it(*dex_file_, dex_file_->GetCallSiteId(call_site_idx));
3171 it.Next(); // Skip to name.
3172 it.Next(); // Skip to method type of the method handle
3173 const uint32_t proto_idx = static_cast<uint32_t>(it.GetJavaValue().i);
3174 const DexFile::ProtoId& proto_id = dex_file_->GetProtoId(proto_idx);
3175 DexFileParameterIterator param_it(*dex_file_, proto_id);
3176 // Treat method as static as it has yet to be determined.
3177 VerifyInvocationArgsFromIterator(&param_it, inst, METHOD_STATIC, is_range, nullptr);
3178 const char* return_descriptor = dex_file_->GetReturnTypeDescriptor(proto_id);
3179
3180 // Step 3. Propagate return type information
3181 const RegType& return_type =
3182 reg_types_.FromDescriptor(GetClassLoader(), return_descriptor, false);
3183 if (!return_type.IsLowHalf()) {
3184 work_line_->SetResultRegisterType(this, return_type);
3185 } else {
3186 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
3187 }
3188 just_set_result = true;
3189 // TODO: Add compiler support for invoke-custom (b/35337872).
3190 Fail(VERIFY_ERROR_FORCE_INTERPRETER);
3191 break;
3192 }
jeffhaobdb76512011-09-07 11:43:16 -07003193 case Instruction::NEG_INT:
3194 case Instruction::NOT_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003195 work_line_->CheckUnaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003196 break;
3197 case Instruction::NEG_LONG:
3198 case Instruction::NOT_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07003199 work_line_->CheckUnaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003200 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07003201 break;
3202 case Instruction::NEG_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003203 work_line_->CheckUnaryOp(this, inst, reg_types_.Float(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07003204 break;
3205 case Instruction::NEG_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07003206 work_line_->CheckUnaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003207 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07003208 break;
3209 case Instruction::INT_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07003210 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003211 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003212 break;
3213 case Instruction::INT_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003214 work_line_->CheckUnaryOp(this, inst, reg_types_.Float(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003215 break;
3216 case Instruction::INT_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07003217 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003218 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003219 break;
3220 case Instruction::LONG_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003221 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003222 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07003223 break;
3224 case Instruction::LONG_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003225 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003226 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07003227 break;
3228 case Instruction::LONG_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07003229 work_line_->CheckUnaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003230 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07003231 break;
3232 case Instruction::FLOAT_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003233 work_line_->CheckUnaryOp(this, inst, reg_types_.Integer(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07003234 break;
3235 case Instruction::FLOAT_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07003236 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003237 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07003238 break;
3239 case Instruction::FLOAT_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07003240 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003241 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07003242 break;
3243 case Instruction::DOUBLE_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003244 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003245 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07003246 break;
3247 case Instruction::DOUBLE_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07003248 work_line_->CheckUnaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003249 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07003250 break;
3251 case Instruction::DOUBLE_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003252 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003253 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07003254 break;
3255 case Instruction::INT_TO_BYTE:
Ian Rogers7b078e82014-09-10 14:44:24 -07003256 work_line_->CheckUnaryOp(this, inst, reg_types_.Byte(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003257 break;
3258 case Instruction::INT_TO_CHAR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003259 work_line_->CheckUnaryOp(this, inst, reg_types_.Char(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003260 break;
3261 case Instruction::INT_TO_SHORT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003262 work_line_->CheckUnaryOp(this, inst, reg_types_.Short(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003263 break;
3264
3265 case Instruction::ADD_INT:
3266 case Instruction::SUB_INT:
3267 case Instruction::MUL_INT:
3268 case Instruction::REM_INT:
3269 case Instruction::DIV_INT:
3270 case Instruction::SHL_INT:
3271 case Instruction::SHR_INT:
3272 case Instruction::USHR_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003273 work_line_->CheckBinaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003274 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07003275 break;
3276 case Instruction::AND_INT:
3277 case Instruction::OR_INT:
3278 case Instruction::XOR_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003279 work_line_->CheckBinaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003280 reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07003281 break;
3282 case Instruction::ADD_LONG:
3283 case Instruction::SUB_LONG:
3284 case Instruction::MUL_LONG:
3285 case Instruction::DIV_LONG:
3286 case Instruction::REM_LONG:
3287 case Instruction::AND_LONG:
3288 case Instruction::OR_LONG:
3289 case Instruction::XOR_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07003290 work_line_->CheckBinaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003291 reg_types_.LongLo(), reg_types_.LongHi(),
3292 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07003293 break;
3294 case Instruction::SHL_LONG:
3295 case Instruction::SHR_LONG:
3296 case Instruction::USHR_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07003297 /* shift distance is Int, making these different from other binary operations */
Ian Rogers7b078e82014-09-10 14:44:24 -07003298 work_line_->CheckBinaryOpWideShift(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003299 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003300 break;
3301 case Instruction::ADD_FLOAT:
3302 case Instruction::SUB_FLOAT:
3303 case Instruction::MUL_FLOAT:
3304 case Instruction::DIV_FLOAT:
3305 case Instruction::REM_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003306 work_line_->CheckBinaryOp(this, inst, reg_types_.Float(), reg_types_.Float(),
3307 reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07003308 break;
3309 case Instruction::ADD_DOUBLE:
3310 case Instruction::SUB_DOUBLE:
3311 case Instruction::MUL_DOUBLE:
3312 case Instruction::DIV_DOUBLE:
3313 case Instruction::REM_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07003314 work_line_->CheckBinaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003315 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
3316 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07003317 break;
3318 case Instruction::ADD_INT_2ADDR:
3319 case Instruction::SUB_INT_2ADDR:
3320 case Instruction::MUL_INT_2ADDR:
3321 case Instruction::REM_INT_2ADDR:
3322 case Instruction::SHL_INT_2ADDR:
3323 case Instruction::SHR_INT_2ADDR:
3324 case Instruction::USHR_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003325 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
3326 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07003327 break;
3328 case Instruction::AND_INT_2ADDR:
3329 case Instruction::OR_INT_2ADDR:
3330 case Instruction::XOR_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003331 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
3332 reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07003333 break;
3334 case Instruction::DIV_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003335 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
3336 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07003337 break;
3338 case Instruction::ADD_LONG_2ADDR:
3339 case Instruction::SUB_LONG_2ADDR:
3340 case Instruction::MUL_LONG_2ADDR:
3341 case Instruction::DIV_LONG_2ADDR:
3342 case Instruction::REM_LONG_2ADDR:
3343 case Instruction::AND_LONG_2ADDR:
3344 case Instruction::OR_LONG_2ADDR:
3345 case Instruction::XOR_LONG_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003346 work_line_->CheckBinaryOp2addrWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003347 reg_types_.LongLo(), reg_types_.LongHi(),
3348 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07003349 break;
3350 case Instruction::SHL_LONG_2ADDR:
3351 case Instruction::SHR_LONG_2ADDR:
3352 case Instruction::USHR_LONG_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003353 work_line_->CheckBinaryOp2addrWideShift(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003354 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003355 break;
3356 case Instruction::ADD_FLOAT_2ADDR:
3357 case Instruction::SUB_FLOAT_2ADDR:
3358 case Instruction::MUL_FLOAT_2ADDR:
3359 case Instruction::DIV_FLOAT_2ADDR:
3360 case Instruction::REM_FLOAT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003361 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Float(), reg_types_.Float(),
3362 reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07003363 break;
3364 case Instruction::ADD_DOUBLE_2ADDR:
3365 case Instruction::SUB_DOUBLE_2ADDR:
3366 case Instruction::MUL_DOUBLE_2ADDR:
3367 case Instruction::DIV_DOUBLE_2ADDR:
3368 case Instruction::REM_DOUBLE_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003369 work_line_->CheckBinaryOp2addrWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003370 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
3371 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07003372 break;
3373 case Instruction::ADD_INT_LIT16:
Ian Rogersf72a11d2014-10-30 15:41:08 -07003374 case Instruction::RSUB_INT_LIT16:
jeffhaobdb76512011-09-07 11:43:16 -07003375 case Instruction::MUL_INT_LIT16:
3376 case Instruction::DIV_INT_LIT16:
3377 case Instruction::REM_INT_LIT16:
Ian Rogers7b078e82014-09-10 14:44:24 -07003378 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), false,
3379 true);
jeffhaobdb76512011-09-07 11:43:16 -07003380 break;
3381 case Instruction::AND_INT_LIT16:
3382 case Instruction::OR_INT_LIT16:
3383 case Instruction::XOR_INT_LIT16:
Ian Rogers7b078e82014-09-10 14:44:24 -07003384 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), true,
3385 true);
jeffhaobdb76512011-09-07 11:43:16 -07003386 break;
3387 case Instruction::ADD_INT_LIT8:
3388 case Instruction::RSUB_INT_LIT8:
3389 case Instruction::MUL_INT_LIT8:
3390 case Instruction::DIV_INT_LIT8:
3391 case Instruction::REM_INT_LIT8:
3392 case Instruction::SHL_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07003393 case Instruction::SHR_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07003394 case Instruction::USHR_INT_LIT8:
Ian Rogers7b078e82014-09-10 14:44:24 -07003395 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), false,
3396 false);
jeffhaobdb76512011-09-07 11:43:16 -07003397 break;
3398 case Instruction::AND_INT_LIT8:
3399 case Instruction::OR_INT_LIT8:
3400 case Instruction::XOR_INT_LIT8:
Ian Rogers7b078e82014-09-10 14:44:24 -07003401 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), true,
3402 false);
jeffhaobdb76512011-09-07 11:43:16 -07003403 break;
3404
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003405 // Special instructions.
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07003406 case Instruction::RETURN_VOID_NO_BARRIER:
3407 if (IsConstructor() && !IsStatic()) {
3408 auto& declaring_class = GetDeclaringClass();
Andreas Gampe68df3202015-06-22 11:35:46 -07003409 if (declaring_class.IsUnresolvedReference()) {
3410 // We must iterate over the fields, even if we cannot use mirror classes to do so. Do it
3411 // manually over the underlying dex file.
3412 uint32_t first_index = GetFirstFinalInstanceFieldIndex(*dex_file_,
3413 dex_file_->GetMethodId(dex_method_idx_).class_idx_);
Andreas Gampee2abbc62017-09-15 11:59:26 -07003414 if (first_index != dex::kDexNoIndex) {
Andreas Gampe68df3202015-06-22 11:35:46 -07003415 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void-no-barrier not expected for field "
3416 << first_index;
3417 }
3418 break;
3419 }
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07003420 auto* klass = declaring_class.GetClass();
3421 for (uint32_t i = 0, num_fields = klass->NumInstanceFields(); i < num_fields; ++i) {
3422 if (klass->GetInstanceField(i)->IsFinal()) {
Mathieu Chartiere86deef2015-03-19 13:43:37 -07003423 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void-no-barrier not expected for "
David Sehr709b0702016-10-13 09:12:37 -07003424 << klass->GetInstanceField(i)->PrettyField();
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07003425 break;
3426 }
3427 }
Sebastien Hertzcc10e0e2013-06-28 14:24:48 +02003428 }
Andreas Gampeb2917962015-07-31 13:36:10 -07003429 // Handle this like a RETURN_VOID now. Code is duplicated to separate standard from
3430 // quickened opcodes (otherwise this could be a fall-through).
3431 if (!IsConstructor()) {
3432 if (!GetMethodReturnType().IsConflict()) {
3433 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void not expected";
3434 }
3435 }
Sebastien Hertzcc10e0e2013-06-28 14:24:48 +02003436 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003437 // Note: the following instructions encode offsets derived from class linking.
Neil Fuller0e844392016-09-08 13:43:31 +01003438 // As such they use Class*/Field*/Executable* as these offsets only have
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003439 // meaning if the class linking and resolution were successful.
3440 case Instruction::IGET_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003441 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003442 break;
3443 case Instruction::IGET_WIDE_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003444 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003445 break;
3446 case Instruction::IGET_OBJECT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003447 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003448 break;
Mathieu Chartierffc605c2014-12-10 10:35:44 -08003449 case Instruction::IGET_BOOLEAN_QUICK:
3450 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true);
3451 break;
3452 case Instruction::IGET_BYTE_QUICK:
3453 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true);
3454 break;
3455 case Instruction::IGET_CHAR_QUICK:
3456 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true);
3457 break;
3458 case Instruction::IGET_SHORT_QUICK:
3459 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true);
3460 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003461 case Instruction::IPUT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003462 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003463 break;
Fred Shih37f05ef2014-07-16 18:38:08 -07003464 case Instruction::IPUT_BOOLEAN_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003465 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07003466 break;
3467 case Instruction::IPUT_BYTE_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003468 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07003469 break;
3470 case Instruction::IPUT_CHAR_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003471 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07003472 break;
3473 case Instruction::IPUT_SHORT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003474 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07003475 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003476 case Instruction::IPUT_WIDE_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003477 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003478 break;
3479 case Instruction::IPUT_OBJECT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003480 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003481 break;
3482 case Instruction::INVOKE_VIRTUAL_QUICK:
3483 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: {
3484 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
Mathieu Chartiere401d142015-04-22 13:56:20 -07003485 ArtMethod* called_method = VerifyInvokeVirtualQuickArgs(inst, is_range);
Ian Rogers7b078e82014-09-10 14:44:24 -07003486 if (called_method != nullptr) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003487 const char* descriptor = called_method->GetReturnTypeDescriptor();
Ian Rogersd8f69b02014-09-10 21:43:52 +00003488 const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003489 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003490 work_line_->SetResultRegisterType(this, return_type);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003491 } else {
3492 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
3493 }
3494 just_set_result = true;
3495 }
3496 break;
3497 }
3498
Ian Rogersd81871c2011-10-03 13:57:23 -07003499 /* These should never appear during verification. */
Mathieu Chartierffc605c2014-12-10 10:35:44 -08003500 case Instruction::UNUSED_3E ... Instruction::UNUSED_43:
Narayan Kamath8ec3bd22016-08-03 12:46:23 +01003501 case Instruction::UNUSED_F3 ... Instruction::UNUSED_F9:
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003502 case Instruction::UNUSED_79:
3503 case Instruction::UNUSED_7A:
jeffhaod5347e02012-03-22 17:25:05 -07003504 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Unexpected opcode " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07003505 break;
3506
3507 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003508 * DO NOT add a "default" clause here. Without it the compiler will
jeffhaobdb76512011-09-07 11:43:16 -07003509 * complain if an instruction is missing (which is desirable).
3510 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003511 } // end - switch (dec_insn.opcode)
jeffhaobdb76512011-09-07 11:43:16 -07003512
Ian Rogersad0b3a32012-04-16 14:50:24 -07003513 if (have_pending_hard_failure_) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08003514 if (Runtime::Current()->IsAotCompiler()) {
3515 /* When AOT compiling, check that the last failure is a hard failure */
Andreas Gampeb588f4c2015-05-26 13:35:39 -07003516 if (failures_[failures_.size() - 1] != VERIFY_ERROR_BAD_CLASS_HARD) {
3517 LOG(ERROR) << "Pending failures:";
3518 for (auto& error : failures_) {
3519 LOG(ERROR) << error;
3520 }
3521 for (auto& error_msg : failure_messages_) {
3522 LOG(ERROR) << error_msg->str();
3523 }
3524 LOG(FATAL) << "Pending hard failure, but last failure not hard.";
3525 }
Ian Rogerse1758fe2012-04-19 11:31:15 -07003526 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003527 /* immediate failure, reject class */
3528 info_messages_ << "Rejecting opcode " << inst->DumpString(dex_file_);
3529 return false;
jeffhaofaf459e2012-08-31 15:32:47 -07003530 } else if (have_pending_runtime_throw_failure_) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07003531 /* checking interpreter will throw, mark following code as unreachable */
jeffhaofaf459e2012-08-31 15:32:47 -07003532 opcode_flags = Instruction::kThrow;
Andreas Gampea727e372015-08-25 09:22:37 -07003533 // Note: the flag must be reset as it is only global to decouple Fail and is semantically per
3534 // instruction. However, RETURN checking may throw LOCKING errors, so we clear at the
3535 // very end.
jeffhaobdb76512011-09-07 11:43:16 -07003536 }
jeffhaobdb76512011-09-07 11:43:16 -07003537 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003538 * If we didn't just set the result register, clear it out. This ensures that you can only use
3539 * "move-result" immediately after the result is set. (We could check this statically, but it's
3540 * not expensive and it makes our debugging output cleaner.)
jeffhaobdb76512011-09-07 11:43:16 -07003541 */
3542 if (!just_set_result) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003543 work_line_->SetResultTypeToUnknown(this);
jeffhaobdb76512011-09-07 11:43:16 -07003544 }
3545
jeffhaobdb76512011-09-07 11:43:16 -07003546 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003547 * Handle "branch". Tag the branch target.
jeffhaobdb76512011-09-07 11:43:16 -07003548 *
3549 * NOTE: instructions like Instruction::EQZ provide information about the
jeffhaod1f0fde2011-09-08 17:25:33 -07003550 * state of the register when the branch is taken or not taken. For example,
jeffhaobdb76512011-09-07 11:43:16 -07003551 * somebody could get a reference field, check it for zero, and if the
3552 * branch is taken immediately store that register in a boolean field
jeffhaod1f0fde2011-09-08 17:25:33 -07003553 * since the value is known to be zero. We do not currently account for
jeffhaobdb76512011-09-07 11:43:16 -07003554 * that, and will reject the code.
3555 *
3556 * TODO: avoid re-fetching the branch target
3557 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003558 if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07003559 bool isConditional, selfOkay;
Ian Rogersd81871c2011-10-03 13:57:23 -07003560 if (!GetBranchOffset(work_insn_idx_, &branch_target, &isConditional, &selfOkay)) {
jeffhaobdb76512011-09-07 11:43:16 -07003561 /* should never happen after static verification */
jeffhaod5347e02012-03-22 17:25:05 -07003562 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad branch";
jeffhaobdb76512011-09-07 11:43:16 -07003563 return false;
3564 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08003565 DCHECK_EQ(isConditional, (opcode_flags & Instruction::kContinue) != 0);
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08003566 if (!CheckNotMoveExceptionOrMoveResult(code_item_accessor_.Insns(),
3567 work_insn_idx_ + branch_target)) {
jeffhaobdb76512011-09-07 11:43:16 -07003568 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07003569 }
jeffhaobdb76512011-09-07 11:43:16 -07003570 /* update branch target, set "changed" if appropriate */
Mathieu Chartierde40d472015-10-15 17:47:48 -07003571 if (nullptr != branch_line) {
Ian Rogersebbdd872014-07-07 23:53:08 -07003572 if (!UpdateRegisters(work_insn_idx_ + branch_target, branch_line.get(), false)) {
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003573 return false;
3574 }
3575 } else {
Ian Rogersebbdd872014-07-07 23:53:08 -07003576 if (!UpdateRegisters(work_insn_idx_ + branch_target, work_line_.get(), false)) {
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003577 return false;
3578 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003579 }
jeffhaobdb76512011-09-07 11:43:16 -07003580 }
3581
3582 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003583 * Handle "switch". Tag all possible branch targets.
jeffhaobdb76512011-09-07 11:43:16 -07003584 *
3585 * We've already verified that the table is structurally sound, so we
3586 * just need to walk through and tag the targets.
3587 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003588 if ((opcode_flags & Instruction::kSwitch) != 0) {
Andreas Gampe53de99c2015-08-17 13:43:55 -07003589 int offset_to_switch = insns[1] | (static_cast<int32_t>(insns[2]) << 16);
jeffhaobdb76512011-09-07 11:43:16 -07003590 const uint16_t* switch_insns = insns + offset_to_switch;
3591 int switch_count = switch_insns[1];
3592 int offset_to_targets, targ;
3593
3594 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
3595 /* 0 = sig, 1 = count, 2/3 = first key */
3596 offset_to_targets = 4;
3597 } else {
3598 /* 0 = sig, 1 = count, 2..count * 2 = keys */
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07003599 DCHECK((*insns & 0xff) == Instruction::SPARSE_SWITCH);
jeffhaobdb76512011-09-07 11:43:16 -07003600 offset_to_targets = 2 + 2 * switch_count;
3601 }
3602
3603 /* verify each switch target */
3604 for (targ = 0; targ < switch_count; targ++) {
3605 int offset;
3606 uint32_t abs_offset;
3607
3608 /* offsets are 32-bit, and only partly endian-swapped */
3609 offset = switch_insns[offset_to_targets + targ * 2] |
Andreas Gampe53de99c2015-08-17 13:43:55 -07003610 (static_cast<int32_t>(switch_insns[offset_to_targets + targ * 2 + 1]) << 16);
Ian Rogersd81871c2011-10-03 13:57:23 -07003611 abs_offset = work_insn_idx_ + offset;
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08003612 DCHECK_LT(abs_offset, code_item_accessor_.InsnsSizeInCodeUnits());
3613 if (!CheckNotMoveExceptionOrMoveResult(code_item_accessor_.Insns(), abs_offset)) {
jeffhaobdb76512011-09-07 11:43:16 -07003614 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07003615 }
Ian Rogersebbdd872014-07-07 23:53:08 -07003616 if (!UpdateRegisters(abs_offset, work_line_.get(), false)) {
jeffhaobdb76512011-09-07 11:43:16 -07003617 return false;
Ian Rogersebbdd872014-07-07 23:53:08 -07003618 }
jeffhaobdb76512011-09-07 11:43:16 -07003619 }
3620 }
3621
3622 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003623 * Handle instructions that can throw and that are sitting in a "try" block. (If they're not in a
3624 * "try" block when they throw, control transfers out of the method.)
jeffhaobdb76512011-09-07 11:43:16 -07003625 */
Mathieu Chartierde40d472015-10-15 17:47:48 -07003626 if ((opcode_flags & Instruction::kThrow) != 0 && GetInstructionFlags(work_insn_idx_).IsInTry()) {
Andreas Gampef91baf12014-07-18 15:41:00 -07003627 bool has_catch_all_handler = false;
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08003628 const DexFile::TryItem* try_item = code_item_accessor_.FindTryItem(work_insn_idx_);
3629 CHECK(try_item != nullptr);
3630 CatchHandlerIterator iterator(code_item_accessor_.GetCatchHandlerData(try_item->handler_off_));
jeffhaobdb76512011-09-07 11:43:16 -07003631
Andreas Gampef91baf12014-07-18 15:41:00 -07003632 // Need the linker to try and resolve the handled class to check if it's Throwable.
3633 ClassLinker* linker = Runtime::Current()->GetClassLinker();
3634
Ian Rogers0571d352011-11-03 19:51:38 -07003635 for (; iterator.HasNext(); iterator.Next()) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08003636 dex::TypeIndex handler_type_idx = iterator.GetHandlerTypeIndex();
3637 if (!handler_type_idx.IsValid()) {
Andreas Gampef91baf12014-07-18 15:41:00 -07003638 has_catch_all_handler = true;
3639 } else {
3640 // It is also a catch-all if it is java.lang.Throwable.
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003641 mirror::Class* klass = linker->ResolveType(*dex_file_, handler_type_idx, dex_cache_,
3642 class_loader_);
Andreas Gampef91baf12014-07-18 15:41:00 -07003643 if (klass != nullptr) {
3644 if (klass == mirror::Throwable::GetJavaLangThrowable()) {
3645 has_catch_all_handler = true;
3646 }
3647 } else {
3648 // Clear exception.
Ian Rogers7b078e82014-09-10 14:44:24 -07003649 DCHECK(self_->IsExceptionPending());
3650 self_->ClearException();
Andreas Gampef91baf12014-07-18 15:41:00 -07003651 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003652 }
jeffhaobdb76512011-09-07 11:43:16 -07003653 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003654 * Merge registers into the "catch" block. We want to use the "savedRegs" rather than
3655 * "work_regs", because at runtime the exception will be thrown before the instruction
3656 * modifies any registers.
jeffhaobdb76512011-09-07 11:43:16 -07003657 */
Ian Rogersebbdd872014-07-07 23:53:08 -07003658 if (!UpdateRegisters(iterator.GetHandlerAddress(), saved_line_.get(), false)) {
jeffhaobdb76512011-09-07 11:43:16 -07003659 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07003660 }
jeffhaobdb76512011-09-07 11:43:16 -07003661 }
3662
3663 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003664 * If the monitor stack depth is nonzero, there must be a "catch all" handler for this
3665 * instruction. This does apply to monitor-exit because of async exception handling.
jeffhaobdb76512011-09-07 11:43:16 -07003666 */
Andreas Gampef91baf12014-07-18 15:41:00 -07003667 if (work_line_->MonitorStackDepth() > 0 && !has_catch_all_handler) {
jeffhaobdb76512011-09-07 11:43:16 -07003668 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003669 * The state in work_line reflects the post-execution state. If the current instruction is a
3670 * monitor-enter and the monitor stack was empty, we don't need a catch-all (if it throws,
jeffhaobdb76512011-09-07 11:43:16 -07003671 * it will do so before grabbing the lock).
3672 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02003673 if (inst->Opcode() != Instruction::MONITOR_ENTER || work_line_->MonitorStackDepth() != 1) {
jeffhaod5347e02012-03-22 17:25:05 -07003674 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
Ian Rogersd81871c2011-10-03 13:57:23 -07003675 << "expected to be within a catch-all for an instruction where a monitor is held";
jeffhaobdb76512011-09-07 11:43:16 -07003676 return false;
3677 }
3678 }
3679 }
3680
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003681 /* Handle "continue". Tag the next consecutive instruction.
3682 * Note: Keep the code handling "continue" case below the "branch" and "switch" cases,
3683 * because it changes work_line_ when performing peephole optimization
3684 * and this change should not be used in those cases.
3685 */
Ian Rogers6d376ae2013-07-23 15:12:40 -07003686 if ((opcode_flags & Instruction::kContinue) != 0) {
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08003687 DCHECK_EQ(&code_item_accessor_.InstructionAt(work_insn_idx_), inst);
Ian Rogers7b078e82014-09-10 14:44:24 -07003688 uint32_t next_insn_idx = work_insn_idx_ + inst->SizeInCodeUnits();
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08003689 if (next_insn_idx >= code_item_accessor_.InsnsSizeInCodeUnits()) {
Ian Rogers6d376ae2013-07-23 15:12:40 -07003690 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Execution can walk off end of code area";
3691 return false;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003692 }
Ian Rogers6d376ae2013-07-23 15:12:40 -07003693 // The only way to get to a move-exception instruction is to get thrown there. Make sure the
3694 // next instruction isn't one.
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08003695 if (!CheckNotMoveException(code_item_accessor_.Insns(), next_insn_idx)) {
Ian Rogers6d376ae2013-07-23 15:12:40 -07003696 return false;
3697 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07003698 if (nullptr != fallthrough_line) {
Ian Rogers6d376ae2013-07-23 15:12:40 -07003699 // Make workline consistent with fallthrough computed from peephole optimization.
3700 work_line_->CopyFromLine(fallthrough_line.get());
3701 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07003702 if (GetInstructionFlags(next_insn_idx).IsReturn()) {
Ian Rogersb8c78592013-07-25 23:52:52 +00003703 // For returns we only care about the operand to the return, all other registers are dead.
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08003704 const Instruction* ret_inst = &code_item_accessor_.InstructionAt(next_insn_idx);
Andreas Gampea727e372015-08-25 09:22:37 -07003705 AdjustReturnLine(this, ret_inst, work_line_.get());
Ian Rogersb8c78592013-07-25 23:52:52 +00003706 }
Ian Rogers6d376ae2013-07-23 15:12:40 -07003707 RegisterLine* next_line = reg_table_.GetLine(next_insn_idx);
Ian Rogers7b078e82014-09-10 14:44:24 -07003708 if (next_line != nullptr) {
Ian Rogersebbdd872014-07-07 23:53:08 -07003709 // Merge registers into what we have for the next instruction, and set the "changed" flag if
3710 // needed. If the merge changes the state of the registers then the work line will be
3711 // updated.
3712 if (!UpdateRegisters(next_insn_idx, work_line_.get(), true)) {
Ian Rogers6d376ae2013-07-23 15:12:40 -07003713 return false;
3714 }
3715 } else {
3716 /*
3717 * We're not recording register data for the next instruction, so we don't know what the
3718 * prior state was. We have to assume that something has changed and re-evaluate it.
3719 */
Mathieu Chartierde40d472015-10-15 17:47:48 -07003720 GetInstructionFlags(next_insn_idx).SetChanged();
Ian Rogers6d376ae2013-07-23 15:12:40 -07003721 }
3722 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003723
jeffhaod1f0fde2011-09-08 17:25:33 -07003724 /* If we're returning from the method, make sure monitor stack is empty. */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003725 if ((opcode_flags & Instruction::kReturn) != 0) {
Andreas Gampea727e372015-08-25 09:22:37 -07003726 work_line_->VerifyMonitorStackEmpty(this);
jeffhaobdb76512011-09-07 11:43:16 -07003727 }
3728
3729 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003730 * Update start_guess. Advance to the next instruction of that's
3731 * possible, otherwise use the branch target if one was found. If
jeffhaobdb76512011-09-07 11:43:16 -07003732 * neither of those exists we're in a return or throw; leave start_guess
3733 * alone and let the caller sort it out.
3734 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003735 if ((opcode_flags & Instruction::kContinue) != 0) {
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08003736 DCHECK_EQ(&code_item_accessor_.InstructionAt(work_insn_idx_), inst);
Ian Rogers7b078e82014-09-10 14:44:24 -07003737 *start_guess = work_insn_idx_ + inst->SizeInCodeUnits();
Elliott Hughesadb8c672012-03-06 16:49:32 -08003738 } else if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07003739 /* we're still okay if branch_target is zero */
Ian Rogersd81871c2011-10-03 13:57:23 -07003740 *start_guess = work_insn_idx_ + branch_target;
jeffhaobdb76512011-09-07 11:43:16 -07003741 }
3742
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08003743 DCHECK_LT(*start_guess, code_item_accessor_.InsnsSizeInCodeUnits());
Mathieu Chartierde40d472015-10-15 17:47:48 -07003744 DCHECK(GetInstructionFlags(*start_guess).IsOpcode());
jeffhaobdb76512011-09-07 11:43:16 -07003745
Andreas Gampea727e372015-08-25 09:22:37 -07003746 if (have_pending_runtime_throw_failure_) {
3747 have_any_pending_runtime_throw_failure_ = true;
3748 // Reset the pending_runtime_throw flag now.
3749 have_pending_runtime_throw_failure_ = false;
3750 }
3751
jeffhaobdb76512011-09-07 11:43:16 -07003752 return true;
Brian Carlstrom7934ac22013-07-26 10:54:15 -07003753} // NOLINT(readability/fn_size)
jeffhaobdb76512011-09-07 11:43:16 -07003754
Mathieu Chartierde40d472015-10-15 17:47:48 -07003755void MethodVerifier::UninstantiableError(const char* descriptor) {
3756 Fail(VerifyError::VERIFY_ERROR_NO_CLASS) << "Could not create precise reference for "
3757 << "non-instantiable klass " << descriptor;
3758}
3759
3760inline bool MethodVerifier::IsInstantiableOrPrimitive(mirror::Class* klass) {
3761 return klass->IsInstantiable() || klass->IsPrimitive();
3762}
3763
Andreas Gampe98be1a92017-08-28 08:25:45 -07003764template <MethodVerifier::CheckAccess C>
3765const RegType& MethodVerifier::ResolveClass(dex::TypeIndex class_idx) {
Vladimir Marko8d6768d2017-03-14 10:13:21 +00003766 mirror::Class* klass = can_load_classes_
3767 ? Runtime::Current()->GetClassLinker()->ResolveType(
3768 *dex_file_, class_idx, dex_cache_, class_loader_)
3769 : ClassLinker::LookupResolvedType(class_idx, dex_cache_.Get(), class_loader_.Get()).Ptr();
3770 if (can_load_classes_ && klass == nullptr) {
3771 DCHECK(self_->IsExceptionPending());
3772 self_->ClearException();
3773 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07003774 const RegType* result = nullptr;
Vladimir Marko9cb0c462017-04-21 13:31:41 +01003775 if (klass != nullptr) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07003776 bool precise = klass->CannotBeAssignedFromOtherTypes();
3777 if (precise && !IsInstantiableOrPrimitive(klass)) {
3778 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
3779 UninstantiableError(descriptor);
3780 precise = false;
3781 }
3782 result = reg_types_.FindClass(klass, precise);
3783 if (result == nullptr) {
3784 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
3785 result = reg_types_.InsertClass(descriptor, klass, precise);
3786 }
3787 } else {
3788 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
3789 result = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003790 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07003791 DCHECK(result != nullptr);
3792 if (result->IsConflict()) {
3793 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
3794 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "accessing broken descriptor '" << descriptor
3795 << "' in " << GetDeclaringClass();
3796 return *result;
3797 }
David Brazdilca3c8c32016-09-06 14:04:48 +01003798
3799 // Record result of class resolution attempt.
3800 VerifierDeps::MaybeRecordClassResolution(*dex_file_, class_idx, klass);
3801
Andreas Gampe629be512017-08-25 17:09:32 -07003802 // If requested, check if access is allowed. Unresolved types are included in this check, as the
3803 // interpreter only tests whether access is allowed when a class is not pre-verified and runs in
3804 // the access-checks interpreter. If result is primitive, skip the access check.
3805 //
3806 // Note: we do this for unresolved classes to trigger re-verification at runtime.
3807 if (C == CheckAccess::kYes && result->IsNonZeroReferenceTypes()) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07003808 const RegType& referrer = GetDeclaringClass();
Andreas Gampe629be512017-08-25 17:09:32 -07003809 if (!referrer.CanAccess(*result)) {
3810 Fail(VERIFY_ERROR_ACCESS_CLASS) << "(possibly) illegal class access: '"
Nicolas Geoffray4e868fa2017-04-21 17:16:44 +01003811 << referrer << "' -> '" << *result << "'";
Mathieu Chartierde40d472015-10-15 17:47:48 -07003812 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07003813 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07003814 return *result;
Ian Rogersd81871c2011-10-03 13:57:23 -07003815}
3816
Andreas Gampedd309072017-08-31 07:47:37 -07003817// Instantiate ResolveClass variants. This is required as the -inl file has a function with a call
3818// to ResolveClass, and compilers may decide to inline, requiring a symbol.
Andreas Gampeae0b1f42017-08-30 17:36:26 -07003819template const RegType& MethodVerifier::ResolveClass<MethodVerifier::CheckAccess::kNo>(
3820 dex::TypeIndex class_idx);
3821template const RegType& MethodVerifier::ResolveClass<MethodVerifier::CheckAccess::kYes>(
3822 dex::TypeIndex class_idx);
3823
Ian Rogersd8f69b02014-09-10 21:43:52 +00003824const RegType& MethodVerifier::GetCaughtExceptionType() {
Ian Rogers7b078e82014-09-10 14:44:24 -07003825 const RegType* common_super = nullptr;
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08003826 if (code_item_accessor_.TriesSize() != 0) {
3827 const uint8_t* handlers_ptr = code_item_accessor_.GetCatchHandlerData();
Ian Rogersd81871c2011-10-03 13:57:23 -07003828 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
3829 for (uint32_t i = 0; i < handlers_size; i++) {
Ian Rogers0571d352011-11-03 19:51:38 -07003830 CatchHandlerIterator iterator(handlers_ptr);
3831 for (; iterator.HasNext(); iterator.Next()) {
3832 if (iterator.GetHandlerAddress() == (uint32_t) work_insn_idx_) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08003833 if (!iterator.GetHandlerTypeIndex().IsValid()) {
Ian Rogersb4903572012-10-11 11:52:56 -07003834 common_super = &reg_types_.JavaLangThrowable(false);
Ian Rogersd81871c2011-10-03 13:57:23 -07003835 } else {
Andreas Gampe98be1a92017-08-28 08:25:45 -07003836 const RegType& exception =
3837 ResolveClass<CheckAccess::kYes>(iterator.GetHandlerTypeIndex());
David Brazdilca3c8c32016-09-06 14:04:48 +01003838 if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(exception, this)) {
David Brazdil68b5c0b2016-01-19 14:25:29 +00003839 DCHECK(!exception.IsUninitializedTypes()); // Comes from dex, shouldn't be uninit.
Jeff Haoc26a56c2013-11-04 12:00:47 -08003840 if (exception.IsUnresolvedTypes()) {
3841 // We don't know enough about the type. Fail here and let runtime handle it.
3842 Fail(VERIFY_ERROR_NO_CLASS) << "unresolved exception class " << exception;
3843 return exception;
3844 } else {
3845 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unexpected non-exception class " << exception;
3846 return reg_types_.Conflict();
3847 }
Jeff Haob878f212014-04-24 16:25:36 -07003848 } else if (common_super == nullptr) {
3849 common_super = &exception;
Ian Rogers28ad40d2011-10-27 15:19:26 -07003850 } else if (common_super->Equals(exception)) {
Ian Rogersc4762272012-02-01 15:55:55 -08003851 // odd case, but nothing to do
Ian Rogersd81871c2011-10-03 13:57:23 -07003852 } else {
David Brazdilca3c8c32016-09-06 14:04:48 +01003853 common_super = &common_super->Merge(exception, &reg_types_, this);
Andreas Gampe7c038102014-10-27 20:08:46 -07003854 if (FailOrAbort(this,
David Brazdilca3c8c32016-09-06 14:04:48 +01003855 reg_types_.JavaLangThrowable(false).IsAssignableFrom(
3856 *common_super, this),
Andreas Gampe7c038102014-10-27 20:08:46 -07003857 "java.lang.Throwable is not assignable-from common_super at ",
3858 work_insn_idx_)) {
3859 break;
3860 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003861 }
3862 }
3863 }
3864 }
Ian Rogers0571d352011-11-03 19:51:38 -07003865 handlers_ptr = iterator.EndDataPointer();
Ian Rogersd81871c2011-10-03 13:57:23 -07003866 }
3867 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003868 if (common_super == nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003869 /* no catch blocks, or no catches with classes we can find */
jeffhaod5347e02012-03-22 17:25:05 -07003870 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unable to find exception handler";
Ian Rogersad0b3a32012-04-16 14:50:24 -07003871 return reg_types_.Conflict();
Ian Rogersd81871c2011-10-03 13:57:23 -07003872 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07003873 return *common_super;
Ian Rogersd81871c2011-10-03 13:57:23 -07003874}
3875
Mathieu Chartiere401d142015-04-22 13:56:20 -07003876ArtMethod* MethodVerifier::ResolveMethodAndCheckAccess(
Alex Light7268d472016-01-20 15:50:01 -08003877 uint32_t dex_method_idx, MethodType method_type) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003878 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx);
Andreas Gampe98be1a92017-08-28 08:25:45 -07003879 const RegType& klass_type = ResolveClass<CheckAccess::kYes>(method_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003880 if (klass_type.IsConflict()) {
3881 std::string append(" in attempt to access method ");
3882 append += dex_file_->GetMethodName(method_id);
3883 AppendToLastFailMessage(append);
Ian Rogers7b078e82014-09-10 14:44:24 -07003884 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08003885 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003886 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003887 return nullptr; // Can't resolve Class so no more to do here
Ian Rogers90040192011-12-16 08:54:29 -08003888 }
Vladimir Markoba118822017-06-12 15:41:56 +01003889 ObjPtr<mirror::Class> klass = klass_type.GetClass();
Ian Rogersd8f69b02014-09-10 21:43:52 +00003890 const RegType& referrer = GetDeclaringClass();
Mathieu Chartiere401d142015-04-22 13:56:20 -07003891 auto* cl = Runtime::Current()->GetClassLinker();
3892 auto pointer_size = cl->GetImagePointerSize();
Andreas Gampe42ef8ab2015-12-03 17:27:32 -08003893
Mathieu Chartiere401d142015-04-22 13:56:20 -07003894 ArtMethod* res_method = dex_cache_->GetResolvedMethod(dex_method_idx, pointer_size);
Ian Rogers7b078e82014-09-10 14:44:24 -07003895 if (res_method == nullptr) {
Vladimir Markoba118822017-06-12 15:41:56 +01003896 // Try to find the method with the appropriate lookup for the klass type (interface or not).
3897 // If this lookup does not match `method_type`, errors shall be reported below.
3898 if (klass->IsInterface()) {
3899 res_method = klass->FindInterfaceMethod(dex_cache_.Get(), dex_method_idx, pointer_size);
David Brazdilca3c8c32016-09-06 14:04:48 +01003900 } else {
Vladimir Markoba118822017-06-12 15:41:56 +01003901 res_method = klass->FindClassMethod(dex_cache_.Get(), dex_method_idx, pointer_size);
Ian Rogersd81871c2011-10-03 13:57:23 -07003902 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003903 if (res_method != nullptr) {
Vladimir Markoba118822017-06-12 15:41:56 +01003904 dex_cache_->SetResolvedMethod(dex_method_idx, res_method, pointer_size);
Ian Rogersd81871c2011-10-03 13:57:23 -07003905 }
3906 }
David Brazdilca3c8c32016-09-06 14:04:48 +01003907
Vladimir Markoba118822017-06-12 15:41:56 +01003908 // Record result of method resolution attempt. The klass resolution has recorded whether
3909 // the class is an interface or not and therefore the type of the lookup performed above.
3910 // TODO: Maybe we should not record dependency if the invoke type does not match the lookup type.
3911 VerifierDeps::MaybeRecordMethodResolution(*dex_file_, dex_method_idx, res_method);
3912
Alex Lightafb66472017-08-01 09:54:49 -07003913 bool must_fail = false;
3914 // This is traditional and helps with screwy bytecode. It will tell you that, yes, a method
3915 // exists, but that it's called incorrectly. This significantly helps debugging, as locally it's
3916 // hard to see the differences.
3917 // If we don't have res_method here we must fail. Just use this bool to make sure of that with a
3918 // DCHECK.
Vladimir Markoba118822017-06-12 15:41:56 +01003919 if (res_method == nullptr) {
Alex Lightafb66472017-08-01 09:54:49 -07003920 must_fail = true;
Vladimir Markoba118822017-06-12 15:41:56 +01003921 // Try to find the method also with the other type for better error reporting below
3922 // but do not store such bogus lookup result in the DexCache or VerifierDeps.
3923 if (klass->IsInterface()) {
Alex Lightafb66472017-08-01 09:54:49 -07003924 // NB This is normally not really allowed but we want to get any static or private object
3925 // methods for error message purposes. This will never be returned.
3926 // TODO We might want to change the verifier to not require this.
Vladimir Markoba118822017-06-12 15:41:56 +01003927 res_method = klass->FindClassMethod(dex_cache_.Get(), dex_method_idx, pointer_size);
3928 } else {
3929 // If there was an interface method with the same signature,
3930 // we would have found it also in the "copied" methods.
3931 DCHECK(klass->FindInterfaceMethod(dex_cache_.Get(), dex_method_idx, pointer_size) == nullptr);
3932 }
3933 }
David Brazdilca3c8c32016-09-06 14:04:48 +01003934
3935 if (res_method == nullptr) {
3936 Fail(VERIFY_ERROR_NO_METHOD) << "couldn't find method "
David Sehr709b0702016-10-13 09:12:37 -07003937 << klass->PrettyDescriptor() << "."
David Brazdilca3c8c32016-09-06 14:04:48 +01003938 << dex_file_->GetMethodName(method_id) << " "
3939 << dex_file_->GetMethodSignature(method_id);
3940 return nullptr;
3941 }
3942
Ian Rogersd81871c2011-10-03 13:57:23 -07003943 // Make sure calls to constructors are "direct". There are additional restrictions but we don't
3944 // enforce them here.
3945 if (res_method->IsConstructor() && method_type != METHOD_DIRECT) {
jeffhaod5347e02012-03-22 17:25:05 -07003946 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting non-direct call to constructor "
David Sehr709b0702016-10-13 09:12:37 -07003947 << res_method->PrettyMethod();
Ian Rogers7b078e82014-09-10 14:44:24 -07003948 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003949 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003950 // Disallow any calls to class initializers.
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003951 if (res_method->IsClassInitializer()) {
jeffhaod5347e02012-03-22 17:25:05 -07003952 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting call to class initializer "
David Sehr709b0702016-10-13 09:12:37 -07003953 << res_method->PrettyMethod();
Ian Rogers7b078e82014-09-10 14:44:24 -07003954 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003955 }
Andreas Gampe42ef8ab2015-12-03 17:27:32 -08003956
3957 // Check that interface methods are static or match interface classes.
3958 // We only allow statics if we don't have default methods enabled.
3959 //
3960 // Note: this check must be after the initializer check, as those are required to fail a class,
3961 // while this check implies an IncompatibleClassChangeError.
3962 if (klass->IsInterface()) {
Alex Lightb55f1ac2016-04-12 15:50:55 -07003963 // methods called on interfaces should be invoke-interface, invoke-super, invoke-direct (if
3964 // dex file version is 37 or greater), or invoke-static.
Andreas Gampe42ef8ab2015-12-03 17:27:32 -08003965 if (method_type != METHOD_INTERFACE &&
Neil Fuller9724c632016-01-07 15:42:47 +00003966 method_type != METHOD_STATIC &&
Alex Lightb55f1ac2016-04-12 15:50:55 -07003967 ((dex_file_->GetVersion() < DexFile::kDefaultMethodsVersion) ||
3968 method_type != METHOD_DIRECT) &&
Neil Fuller9724c632016-01-07 15:42:47 +00003969 method_type != METHOD_SUPER) {
Andreas Gampe42ef8ab2015-12-03 17:27:32 -08003970 Fail(VERIFY_ERROR_CLASS_CHANGE)
David Sehr709b0702016-10-13 09:12:37 -07003971 << "non-interface method " << dex_file_->PrettyMethod(dex_method_idx)
3972 << " is in an interface class " << klass->PrettyClass();
Andreas Gampe42ef8ab2015-12-03 17:27:32 -08003973 return nullptr;
3974 }
3975 } else {
3976 if (method_type == METHOD_INTERFACE) {
3977 Fail(VERIFY_ERROR_CLASS_CHANGE)
David Sehr709b0702016-10-13 09:12:37 -07003978 << "interface method " << dex_file_->PrettyMethod(dex_method_idx)
3979 << " is in a non-interface class " << klass->PrettyClass();
Andreas Gampe42ef8ab2015-12-03 17:27:32 -08003980 return nullptr;
3981 }
3982 }
3983
Alex Lightafb66472017-08-01 09:54:49 -07003984 // Check specifically for non-public object methods being provided for interface dispatch. This
3985 // can occur if we failed to find a method with FindInterfaceMethod but later find one with
3986 // FindClassMethod for error message use.
3987 if (method_type == METHOD_INTERFACE &&
3988 res_method->GetDeclaringClass()->IsObjectClass() &&
3989 !res_method->IsPublic()) {
3990 Fail(VERIFY_ERROR_NO_METHOD) << "invoke-interface " << klass->PrettyDescriptor() << "."
3991 << dex_file_->GetMethodName(method_id) << " "
3992 << dex_file_->GetMethodSignature(method_id) << " resolved to "
3993 << "non-public object method " << res_method->PrettyMethod() << " "
3994 << "but non-public Object methods are excluded from interface "
3995 << "method resolution.";
3996 return nullptr;
3997 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003998 // Check if access is allowed.
Ian Rogersad0b3a32012-04-16 14:50:24 -07003999 if (!referrer.CanAccessMember(res_method->GetDeclaringClass(), res_method->GetAccessFlags())) {
David Sehr709b0702016-10-13 09:12:37 -07004000 Fail(VERIFY_ERROR_ACCESS_METHOD) << "illegal method access (call "
4001 << res_method->PrettyMethod()
Ian Rogersad0b3a32012-04-16 14:50:24 -07004002 << " from " << referrer << ")";
jeffhaob57e9522012-04-26 18:08:21 -07004003 return res_method;
jeffhao8cd6dda2012-02-22 10:15:34 -08004004 }
jeffhaode0d9c92012-02-27 13:58:13 -08004005 // Check that invoke-virtual and invoke-super are not used on private methods of the same class.
Alex Light7268d472016-01-20 15:50:01 -08004006 if (res_method->IsPrivate() && (method_type == METHOD_VIRTUAL || method_type == METHOD_SUPER)) {
jeffhaod5347e02012-03-22 17:25:05 -07004007 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invoke-super/virtual can't be used on private method "
David Sehr709b0702016-10-13 09:12:37 -07004008 << res_method->PrettyMethod();
Ian Rogers7b078e82014-09-10 14:44:24 -07004009 return nullptr;
jeffhaode0d9c92012-02-27 13:58:13 -08004010 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004011 // See if the method type implied by the invoke instruction matches the access flags for the
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004012 // target method. The flags for METHOD_POLYMORPHIC are based on there being precisely two
4013 // signature polymorphic methods supported by the run-time which are native methods with variable
4014 // arguments.
Brian Carlstrombe6fa5e2014-12-09 20:15:42 -08004015 if ((method_type == METHOD_DIRECT && (!res_method->IsDirect() || res_method->IsStatic())) ||
Ian Rogersd81871c2011-10-03 13:57:23 -07004016 (method_type == METHOD_STATIC && !res_method->IsStatic()) ||
Alex Light7268d472016-01-20 15:50:01 -08004017 ((method_type == METHOD_SUPER ||
4018 method_type == METHOD_VIRTUAL ||
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004019 method_type == METHOD_INTERFACE) && res_method->IsDirect()) ||
4020 ((method_type == METHOD_POLYMORPHIC) &&
4021 (!res_method->IsNative() || !res_method->IsVarargs()))) {
Ian Rogers2fc14272012-08-30 10:56:57 -07004022 Fail(VERIFY_ERROR_CLASS_CHANGE) << "invoke type (" << method_type << ") does not match method "
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004023 "type of " << res_method->PrettyMethod();
Ian Rogers7b078e82014-09-10 14:44:24 -07004024 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07004025 }
Alex Lightafb66472017-08-01 09:54:49 -07004026 // Make sure we weren't expecting to fail.
4027 DCHECK(!must_fail) << "invoke type (" << method_type << ")"
4028 << klass->PrettyDescriptor() << "."
4029 << dex_file_->GetMethodName(method_id) << " "
4030 << dex_file_->GetMethodSignature(method_id) << " unexpectedly resolved to "
4031 << res_method->PrettyMethod() << " without error. Initially this method was "
4032 << "not found so we were expecting to fail for some reason.";
jeffhao8cd6dda2012-02-22 10:15:34 -08004033 return res_method;
4034}
4035
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004036template <class T>
Mathieu Chartiere401d142015-04-22 13:56:20 -07004037ArtMethod* MethodVerifier::VerifyInvocationArgsFromIterator(
4038 T* it, const Instruction* inst, MethodType method_type, bool is_range, ArtMethod* res_method) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004039 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
4040 // match the call to the signature. Also, we might be calling through an abstract method
4041 // definition (which doesn't have register count values).
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004042 const size_t expected_args = inst->VRegA();
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004043 /* caught by static verifier */
4044 DCHECK(is_range || expected_args <= 5);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004045
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08004046 if (expected_args > code_item_accessor_.OutsSize()) {
Orion Hodson1cda7c22017-08-10 13:06:45 +01004047 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08004048 << ") exceeds outsSize ("
4049 << code_item_accessor_.OutsSize() << ")";
Orion Hodson1cda7c22017-08-10 13:06:45 +01004050 return nullptr;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004051 }
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004052
4053 /*
4054 * Check the "this" argument, which must be an instance of the class that declared the method.
4055 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
4056 * rigorous check here (which is okay since we have to do it at runtime).
4057 */
4058 if (method_type != METHOD_STATIC) {
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004059 const RegType& actual_arg_type = work_line_->GetInvocationThis(this, inst);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004060 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
4061 CHECK(have_pending_hard_failure_);
4062 return nullptr;
4063 }
David Brazdil68b5c0b2016-01-19 14:25:29 +00004064 bool is_init = false;
4065 if (actual_arg_type.IsUninitializedTypes()) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004066 if (res_method) {
4067 if (!res_method->IsConstructor()) {
4068 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
4069 return nullptr;
4070 }
4071 } else {
4072 // Check whether the name of the called method is "<init>"
4073 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Jeff Hao0d087272014-08-04 14:47:17 -07004074 if (strcmp(dex_file_->GetMethodName(dex_file_->GetMethodId(method_idx)), "<init>") != 0) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004075 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
4076 return nullptr;
4077 }
4078 }
David Brazdil68b5c0b2016-01-19 14:25:29 +00004079 is_init = true;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004080 }
David Brazdil68b5c0b2016-01-19 14:25:29 +00004081 const RegType& adjusted_type = is_init
4082 ? GetRegTypeCache()->FromUninitialized(actual_arg_type)
4083 : actual_arg_type;
4084 if (method_type != METHOD_INTERFACE && !adjusted_type.IsZero()) {
Ian Rogersd8f69b02014-09-10 21:43:52 +00004085 const RegType* res_method_class;
Andreas Gamped9e23012015-06-04 22:19:58 -07004086 // Miranda methods have the declaring interface as their declaring class, not the abstract
4087 // class. It would be wrong to use this for the type check (interface type checks are
4088 // postponed to runtime).
4089 if (res_method != nullptr && !res_method->IsMiranda()) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004090 mirror::Class* klass = res_method->GetDeclaringClass();
Ian Rogers1ff3c982014-08-12 02:30:58 -07004091 std::string temp;
Andreas Gampef23f33d2015-06-23 14:18:17 -07004092 res_method_class = &FromClass(klass->GetDescriptor(&temp), klass,
4093 klass->CannotBeAssignedFromOtherTypes());
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004094 } else {
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004095 const uint32_t method_idx = inst->VRegB();
Andreas Gampea5b09a62016-11-17 15:21:22 -08004096 const dex::TypeIndex class_idx = dex_file_->GetMethodId(method_idx).class_idx_;
Mathieu Chartierde40d472015-10-15 17:47:48 -07004097 res_method_class = &reg_types_.FromDescriptor(
4098 GetClassLoader(),
4099 dex_file_->StringByTypeIdx(class_idx),
4100 false);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004101 }
David Brazdilca3c8c32016-09-06 14:04:48 +01004102 if (!res_method_class->IsAssignableFrom(adjusted_type, this)) {
David Brazdil68b5c0b2016-01-19 14:25:29 +00004103 Fail(adjusted_type.IsUnresolvedTypes()
4104 ? VERIFY_ERROR_NO_CLASS
4105 : VERIFY_ERROR_BAD_CLASS_SOFT)
4106 << "'this' argument '" << actual_arg_type << "' not instance of '"
4107 << *res_method_class << "'";
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004108 // Continue on soft failures. We need to find possible hard failures to avoid problems in
4109 // the compiler.
4110 if (have_pending_hard_failure_) {
4111 return nullptr;
4112 }
4113 }
4114 }
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004115 }
4116
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004117 uint32_t arg[5];
4118 if (!is_range) {
4119 inst->GetVarArgs(arg);
4120 }
4121 uint32_t sig_registers = (method_type == METHOD_STATIC) ? 0 : 1;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004122 for ( ; it->HasNext(); it->Next()) {
4123 if (sig_registers >= expected_args) {
4124 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, expected " << inst->VRegA() <<
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004125 " argument registers, method signature has " << sig_registers + 1 << " or more";
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004126 return nullptr;
4127 }
4128
4129 const char* param_descriptor = it->GetDescriptor();
4130
4131 if (param_descriptor == nullptr) {
4132 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation because of missing signature "
4133 "component";
4134 return nullptr;
4135 }
4136
Ian Rogersd8f69b02014-09-10 21:43:52 +00004137 const RegType& reg_type = reg_types_.FromDescriptor(GetClassLoader(), param_descriptor, false);
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004138 uint32_t get_reg = is_range ? inst->VRegC() + static_cast<uint32_t>(sig_registers) :
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004139 arg[sig_registers];
4140 if (reg_type.IsIntegralTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004141 const RegType& src_type = work_line_->GetRegisterType(this, get_reg);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004142 if (!src_type.IsIntegralTypes()) {
4143 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register v" << get_reg << " has type " << src_type
4144 << " but expected " << reg_type;
Andreas Gampeb588f4c2015-05-26 13:35:39 -07004145 return nullptr;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004146 }
Andreas Gampeda9badb2015-06-05 20:22:12 -07004147 } else {
4148 if (!work_line_->VerifyRegisterType(this, get_reg, reg_type)) {
4149 // Continue on soft failures. We need to find possible hard failures to avoid problems in
4150 // the compiler.
4151 if (have_pending_hard_failure_) {
4152 return nullptr;
4153 }
4154 } else if (reg_type.IsLongOrDoubleTypes()) {
4155 // Check that registers are consecutive (for non-range invokes). Invokes are the only
4156 // instructions not specifying register pairs by the first component, but require them
4157 // nonetheless. Only check when there's an actual register in the parameters. If there's
4158 // none, this will fail below.
4159 if (!is_range && sig_registers + 1 < expected_args) {
4160 uint32_t second_reg = arg[sig_registers + 1];
4161 if (second_reg != get_reg + 1) {
4162 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, long or double parameter "
4163 "at index " << sig_registers << " is not a pair: " << get_reg << " + "
4164 << second_reg << ".";
4165 return nullptr;
4166 }
4167 }
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004168 }
4169 }
4170 sig_registers += reg_type.IsLongOrDoubleTypes() ? 2 : 1;
4171 }
4172 if (expected_args != sig_registers) {
4173 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, expected " << expected_args <<
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004174 " argument registers, method signature has " << sig_registers;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004175 return nullptr;
4176 }
4177 return res_method;
4178}
4179
4180void MethodVerifier::VerifyInvocationArgsUnresolvedMethod(const Instruction* inst,
4181 MethodType method_type,
4182 bool is_range) {
4183 // As the method may not have been resolved, make this static check against what we expect.
4184 // The main reason for this code block is to fail hard when we find an illegal use, e.g.,
4185 // wrong number of arguments or wrong primitive types, even if the method could not be resolved.
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004186 const uint32_t method_idx = inst->VRegB();
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004187 DexFileParameterIterator it(*dex_file_,
4188 dex_file_->GetProtoId(dex_file_->GetMethodId(method_idx).proto_idx_));
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004189 VerifyInvocationArgsFromIterator(&it, inst, method_type, is_range, nullptr);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004190}
4191
Orion Hodsonc069a302017-01-18 09:23:12 +00004192bool MethodVerifier::CheckCallSite(uint32_t call_site_idx) {
Orion Hodson3a842f52017-04-21 15:24:10 +01004193 if (call_site_idx >= dex_file_->NumCallSiteIds()) {
4194 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Bad call site id #" << call_site_idx
4195 << " >= " << dex_file_->NumCallSiteIds();
4196 return false;
4197 }
4198
Orion Hodsonc069a302017-01-18 09:23:12 +00004199 CallSiteArrayValueIterator it(*dex_file_, dex_file_->GetCallSiteId(call_site_idx));
4200 // Check essential arguments are provided. The dex file verifier has verified indicies of the
4201 // main values (method handle, name, method_type).
4202 if (it.Size() < 3) {
4203 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Call site #" << call_site_idx
4204 << " has too few arguments: "
4205 << it.Size() << "< 3";
4206 return false;
4207 }
4208
Orion Hodson3a842f52017-04-21 15:24:10 +01004209 // Get and check the first argument: the method handle (index range
4210 // checked by the dex file verifier).
Orion Hodsonc069a302017-01-18 09:23:12 +00004211 uint32_t method_handle_idx = static_cast<uint32_t>(it.GetJavaValue().i);
4212 it.Next();
Orion Hodson3a842f52017-04-21 15:24:10 +01004213
Orion Hodsonc069a302017-01-18 09:23:12 +00004214 const DexFile::MethodHandleItem& mh = dex_file_->GetMethodHandle(method_handle_idx);
4215 if (mh.method_handle_type_ != static_cast<uint16_t>(DexFile::MethodHandleType::kInvokeStatic)) {
4216 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Call site #" << call_site_idx
Orion Hodson631827d2017-04-10 14:53:47 +01004217 << " argument 0 method handle type is not InvokeStatic: "
4218 << mh.method_handle_type_;
Orion Hodsonc069a302017-01-18 09:23:12 +00004219 return false;
4220 }
4221
4222 // Skip the second argument, the name to resolve, as checked by the
4223 // dex file verifier.
4224 it.Next();
4225
4226 // Skip the third argument, the method type expected, as checked by
4227 // the dex file verifier.
4228 it.Next();
4229
4230 // Check the bootstrap method handle and remaining arguments.
4231 const DexFile::MethodId& method_id = dex_file_->GetMethodId(mh.field_or_method_idx_);
4232 uint32_t length;
4233 const char* shorty = dex_file_->GetMethodShorty(method_id, &length);
4234
4235 if (it.Size() < length - 1) {
4236 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Call site #" << call_site_idx
4237 << " too few arguments for bootstrap method: "
4238 << it.Size() << " < " << (length - 1);
4239 return false;
4240 }
4241
4242 // Check the return type and first 3 arguments are references
4243 // (CallSite, Lookup, String, MethodType). If they are not of the
4244 // expected types (or subtypes), it will trigger a
4245 // WrongMethodTypeException during execution.
4246 if (shorty[0] != 'L') {
4247 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Call site #" << call_site_idx
4248 << " bootstrap return type is not a reference";
4249 return false;
4250 }
4251
4252 for (uint32_t i = 1; i < 4; ++i) {
4253 if (shorty[i] != 'L') {
4254 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Call site #" << call_site_idx
4255 << " bootstrap method argument " << (i - 1)
4256 << " is not a reference";
4257 return false;
4258 }
4259 }
4260
4261 // Check the optional arguments.
4262 for (uint32_t i = 4; i < length; ++i, it.Next()) {
4263 bool match = false;
4264 switch (it.GetValueType()) {
4265 case EncodedArrayValueIterator::ValueType::kBoolean:
4266 case EncodedArrayValueIterator::ValueType::kByte:
4267 case EncodedArrayValueIterator::ValueType::kShort:
4268 case EncodedArrayValueIterator::ValueType::kChar:
4269 case EncodedArrayValueIterator::ValueType::kInt:
4270 // These all fit within one register and encoders do not seem
4271 // too exacting on the encoding type they use (ie using
4272 // integer for all of these).
4273 match = (strchr("ZBCSI", shorty[i]) != nullptr);
4274 break;
4275 case EncodedArrayValueIterator::ValueType::kLong:
4276 match = ('J' == shorty[i]);
4277 break;
4278 case EncodedArrayValueIterator::ValueType::kFloat:
4279 match = ('F' == shorty[i]);
4280 break;
4281 case EncodedArrayValueIterator::ValueType::kDouble:
4282 match = ('D' == shorty[i]);
4283 break;
4284 case EncodedArrayValueIterator::ValueType::kMethodType:
4285 case EncodedArrayValueIterator::ValueType::kMethodHandle:
4286 case EncodedArrayValueIterator::ValueType::kString:
4287 case EncodedArrayValueIterator::ValueType::kType:
4288 case EncodedArrayValueIterator::ValueType::kNull:
4289 match = ('L' == shorty[i]);
4290 break;
4291 case EncodedArrayValueIterator::ValueType::kField:
4292 case EncodedArrayValueIterator::ValueType::kMethod:
4293 case EncodedArrayValueIterator::ValueType::kEnum:
4294 case EncodedArrayValueIterator::ValueType::kArray:
4295 case EncodedArrayValueIterator::ValueType::kAnnotation:
4296 // Unreachable based on current EncodedArrayValueIterator::Next().
4297 UNREACHABLE();
4298 }
4299
4300 if (!match) {
4301 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Call site #" << call_site_idx
4302 << " bootstrap method argument " << (i - 1)
4303 << " expected " << shorty[i]
4304 << " got value type: " << it.GetValueType();
4305 return false;
4306 }
4307 }
4308 return true;
4309}
4310
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004311class MethodParamListDescriptorIterator {
4312 public:
Mathieu Chartiere401d142015-04-22 13:56:20 -07004313 explicit MethodParamListDescriptorIterator(ArtMethod* res_method) :
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004314 res_method_(res_method), pos_(0), params_(res_method->GetParameterTypeList()),
4315 params_size_(params_ == nullptr ? 0 : params_->Size()) {
4316 }
4317
4318 bool HasNext() {
4319 return pos_ < params_size_;
4320 }
4321
4322 void Next() {
4323 ++pos_;
4324 }
4325
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07004326 const char* GetDescriptor() REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004327 return res_method_->GetTypeDescriptorFromTypeIdx(params_->GetTypeItem(pos_).type_idx_);
4328 }
4329
4330 private:
Mathieu Chartiere401d142015-04-22 13:56:20 -07004331 ArtMethod* res_method_;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004332 size_t pos_;
4333 const DexFile::TypeList* params_;
4334 const size_t params_size_;
4335};
4336
Mathieu Chartiere401d142015-04-22 13:56:20 -07004337ArtMethod* MethodVerifier::VerifyInvocationArgs(
Alex Light7268d472016-01-20 15:50:01 -08004338 const Instruction* inst, MethodType method_type, bool is_range) {
jeffhao8cd6dda2012-02-22 10:15:34 -08004339 // Resolve the method. This could be an abstract or concrete method depending on what sort of call
4340 // we're making.
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004341 const uint32_t method_idx = inst->VRegB();
Alex Light7268d472016-01-20 15:50:01 -08004342 ArtMethod* res_method = ResolveMethodAndCheckAccess(method_idx, method_type);
Ian Rogers7b078e82014-09-10 14:44:24 -07004343 if (res_method == nullptr) { // error or class is unresolved
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004344 // Check what we can statically.
4345 if (!have_pending_hard_failure_) {
4346 VerifyInvocationArgsUnresolvedMethod(inst, method_type, is_range);
4347 }
4348 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08004349 }
4350
Ian Rogersd81871c2011-10-03 13:57:23 -07004351 // If we're using invoke-super(method), make sure that the executing method's class' superclass
Alex Light705ad492015-09-21 11:36:30 -07004352 // has a vtable entry for the target method. Or the target is on a interface.
Alex Light7268d472016-01-20 15:50:01 -08004353 if (method_type == METHOD_SUPER) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08004354 dex::TypeIndex class_idx = dex_file_->GetMethodId(method_idx).class_idx_;
David Brazdilca3c8c32016-09-06 14:04:48 +01004355 const RegType& reference_type = reg_types_.FromDescriptor(
4356 GetClassLoader(),
4357 dex_file_->StringByTypeIdx(class_idx),
4358 false);
4359 if (reference_type.IsUnresolvedTypes()) {
Alex Lightfedd91d2016-01-07 14:49:16 -08004360 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "Unable to find referenced class from invoke-super";
4361 return nullptr;
4362 }
David Brazdilca3c8c32016-09-06 14:04:48 +01004363 if (reference_type.GetClass()->IsInterface()) {
Alex Lightfedd91d2016-01-07 14:49:16 -08004364 // TODO Can we verify anything else.
David Brazdil15fc7292016-09-02 14:13:18 +01004365 if (class_idx == class_def_.class_idx_) {
Alex Lightfedd91d2016-01-07 14:49:16 -08004366 Fail(VERIFY_ERROR_CLASS_CHANGE) << "Cannot invoke-super on self as interface";
Alex Light55ea94d2016-03-15 09:50:26 -07004367 return nullptr;
Alex Lightfedd91d2016-01-07 14:49:16 -08004368 }
4369 // TODO Revisit whether we want to allow invoke-super on direct interfaces only like the JLS
4370 // does.
Alex Light55ea94d2016-03-15 09:50:26 -07004371 if (!GetDeclaringClass().HasClass()) {
4372 Fail(VERIFY_ERROR_NO_CLASS) << "Unable to resolve the full class of 'this' used in an"
4373 << "interface invoke-super";
4374 return nullptr;
David Brazdilca3c8c32016-09-06 14:04:48 +01004375 } else if (!reference_type.IsStrictlyAssignableFrom(GetDeclaringClass(), this)) {
Alex Lightfedd91d2016-01-07 14:49:16 -08004376 Fail(VERIFY_ERROR_CLASS_CHANGE)
David Sehr709b0702016-10-13 09:12:37 -07004377 << "invoke-super in " << mirror::Class::PrettyClass(GetDeclaringClass().GetClass())
4378 << " in method "
4379 << dex_file_->PrettyMethod(dex_method_idx_) << " to method "
4380 << dex_file_->PrettyMethod(method_idx) << " references "
4381 << "non-super-interface type " << mirror::Class::PrettyClass(reference_type.GetClass());
Alex Lightfedd91d2016-01-07 14:49:16 -08004382 return nullptr;
Alex Light705ad492015-09-21 11:36:30 -07004383 }
4384 } else {
4385 const RegType& super = GetDeclaringClass().GetSuperClass(&reg_types_);
4386 if (super.IsUnresolvedTypes()) {
4387 Fail(VERIFY_ERROR_NO_METHOD) << "unknown super class in invoke-super from "
David Sehr709b0702016-10-13 09:12:37 -07004388 << dex_file_->PrettyMethod(dex_method_idx_)
4389 << " to super " << res_method->PrettyMethod();
Alex Light705ad492015-09-21 11:36:30 -07004390 return nullptr;
4391 }
David Brazdilca3c8c32016-09-06 14:04:48 +01004392 if (!reference_type.IsStrictlyAssignableFrom(GetDeclaringClass(), this) ||
Aart Bikf663e342016-04-04 17:28:59 -07004393 (res_method->GetMethodIndex() >= super.GetClass()->GetVTableLength())) {
Alex Light705ad492015-09-21 11:36:30 -07004394 Fail(VERIFY_ERROR_NO_METHOD) << "invalid invoke-super from "
David Sehr709b0702016-10-13 09:12:37 -07004395 << dex_file_->PrettyMethod(dex_method_idx_)
Alex Light705ad492015-09-21 11:36:30 -07004396 << " to super " << super
4397 << "." << res_method->GetName()
4398 << res_method->GetSignature();
4399 return nullptr;
4400 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004401 }
4402 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08004403
Andreas Gampe74979b12017-05-16 09:28:06 -07004404 if (UNLIKELY(method_type == METHOD_POLYMORPHIC)) {
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004405 // Process the signature of the calling site that is invoking the method handle.
4406 DexFileParameterIterator it(*dex_file_, dex_file_->GetProtoId(inst->VRegH()));
4407 return VerifyInvocationArgsFromIterator(&it, inst, method_type, is_range, res_method);
4408 } else {
4409 // Process the target method's signature.
4410 MethodParamListDescriptorIterator it(res_method);
4411 return VerifyInvocationArgsFromIterator(&it, inst, method_type, is_range, res_method);
4412 }
4413}
4414
4415bool MethodVerifier::CheckSignaturePolymorphicMethod(ArtMethod* method) {
4416 mirror::Class* klass = method->GetDeclaringClass();
4417 if (klass != mirror::MethodHandle::StaticClass()) {
4418 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
4419 << "Signature polymorphic method must be declared in java.lang.invoke.MethodClass";
4420 return false;
4421 }
4422
4423 const char* method_name = method->GetName();
4424 if (strcmp(method_name, "invoke") != 0 && strcmp(method_name, "invokeExact") != 0) {
4425 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
4426 << "Signature polymorphic method name invalid: " << method_name;
4427 return false;
4428 }
4429
4430 const DexFile::TypeList* types = method->GetParameterTypeList();
4431 if (types->Size() != 1) {
4432 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
4433 << "Signature polymorphic method has too many arguments " << types->Size() << " != 1";
4434 return false;
4435 }
4436
4437 const dex::TypeIndex argument_type_index = types->GetTypeItem(0).type_idx_;
4438 const char* argument_descriptor = method->GetTypeDescriptorFromTypeIdx(argument_type_index);
4439 if (strcmp(argument_descriptor, "[Ljava/lang/Object;") != 0) {
4440 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
4441 << "Signature polymorphic method has unexpected argument type: " << argument_descriptor;
4442 return false;
4443 }
4444
4445 const char* return_descriptor = method->GetReturnTypeDescriptor();
4446 if (strcmp(return_descriptor, "Ljava/lang/Object;") != 0) {
4447 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
4448 << "Signature polymorphic method has unexpected return type: " << return_descriptor;
4449 return false;
4450 }
4451
4452 return true;
4453}
4454
4455bool MethodVerifier::CheckSignaturePolymorphicReceiver(const Instruction* inst) {
4456 const RegType& this_type = work_line_->GetInvocationThis(this, inst);
4457 if (this_type.IsZero()) {
4458 /* null pointer always passes (and always fails at run time) */
4459 return true;
4460 } else if (!this_type.IsNonZeroReferenceTypes()) {
4461 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
4462 << "invoke-polymorphic receiver is not a reference: "
4463 << this_type;
4464 return false;
4465 } else if (this_type.IsUninitializedReference()) {
4466 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
4467 << "invoke-polymorphic receiver is uninitialized: "
4468 << this_type;
4469 return false;
4470 } else if (!this_type.HasClass()) {
4471 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
4472 << "invoke-polymorphic receiver has no class: "
4473 << this_type;
4474 return false;
4475 } else if (!this_type.GetClass()->IsSubClass(mirror::MethodHandle::StaticClass())) {
4476 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
4477 << "invoke-polymorphic receiver is not a subclass of MethodHandle: "
4478 << this_type;
4479 return false;
4480 }
4481 return true;
Ian Rogersd81871c2011-10-03 13:57:23 -07004482}
4483
Mathieu Chartiere401d142015-04-22 13:56:20 -07004484ArtMethod* MethodVerifier::GetQuickInvokedMethod(const Instruction* inst, RegisterLine* reg_line,
4485 bool is_range, bool allow_failure) {
Mathieu Chartier091d2382015-03-06 10:59:06 -08004486 if (is_range) {
4487 DCHECK_EQ(inst->Opcode(), Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
4488 } else {
4489 DCHECK_EQ(inst->Opcode(), Instruction::INVOKE_VIRTUAL_QUICK);
4490 }
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004491 const RegType& actual_arg_type = reg_line->GetInvocationThis(this, inst, allow_failure);
Ian Rogers9bc54402014-04-17 16:40:01 -07004492 if (!actual_arg_type.HasClass()) {
4493 VLOG(verifier) << "Failed to get mirror::Class* from '" << actual_arg_type << "'";
Andreas Gampe63981562014-04-17 12:28:43 -07004494 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004495 }
Ian Rogersa4cf1df2014-05-07 19:47:17 -07004496 mirror::Class* klass = actual_arg_type.GetClass();
Mingyao Yang2cdbad72014-07-16 10:44:41 -07004497 mirror::Class* dispatch_class;
Ian Rogersa4cf1df2014-05-07 19:47:17 -07004498 if (klass->IsInterface()) {
4499 // Derive Object.class from Class.class.getSuperclass().
4500 mirror::Class* object_klass = klass->GetClass()->GetSuperClass();
Andreas Gampe7c038102014-10-27 20:08:46 -07004501 if (FailOrAbort(this, object_klass->IsObjectClass(),
Mathieu Chartier091d2382015-03-06 10:59:06 -08004502 "Failed to find Object class in quickened invoke receiver", work_insn_idx_)) {
Andreas Gampe7c038102014-10-27 20:08:46 -07004503 return nullptr;
4504 }
Mingyao Yang2cdbad72014-07-16 10:44:41 -07004505 dispatch_class = object_klass;
Ian Rogersa4cf1df2014-05-07 19:47:17 -07004506 } else {
Mingyao Yang2cdbad72014-07-16 10:44:41 -07004507 dispatch_class = klass;
Ian Rogersa4cf1df2014-05-07 19:47:17 -07004508 }
Mathieu Chartier091d2382015-03-06 10:59:06 -08004509 if (!dispatch_class->HasVTable()) {
4510 FailOrAbort(this, allow_failure, "Receiver class has no vtable for quickened invoke at ",
4511 work_insn_idx_);
Andreas Gampe7c038102014-10-27 20:08:46 -07004512 return nullptr;
4513 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004514 uint16_t vtable_index = is_range ? inst->VRegB_3rc() : inst->VRegB_35c();
Mathieu Chartiere401d142015-04-22 13:56:20 -07004515 auto* cl = Runtime::Current()->GetClassLinker();
4516 auto pointer_size = cl->GetImagePointerSize();
Mathieu Chartier091d2382015-03-06 10:59:06 -08004517 if (static_cast<int32_t>(vtable_index) >= dispatch_class->GetVTableLength()) {
4518 FailOrAbort(this, allow_failure,
4519 "Receiver class has not enough vtable slots for quickened invoke at ",
4520 work_insn_idx_);
Andreas Gampe7c038102014-10-27 20:08:46 -07004521 return nullptr;
4522 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07004523 ArtMethod* res_method = dispatch_class->GetVTableEntry(vtable_index, pointer_size);
Mathieu Chartier091d2382015-03-06 10:59:06 -08004524 if (self_->IsExceptionPending()) {
4525 FailOrAbort(this, allow_failure, "Unexpected exception pending for quickened invoke at ",
4526 work_insn_idx_);
Andreas Gampe7c038102014-10-27 20:08:46 -07004527 return nullptr;
4528 }
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004529 return res_method;
4530}
4531
Mathieu Chartiere401d142015-04-22 13:56:20 -07004532ArtMethod* MethodVerifier::VerifyInvokeVirtualQuickArgs(const Instruction* inst, bool is_range) {
Andreas Gampe76bd8802014-12-10 16:43:58 -08004533 DCHECK(Runtime::Current()->IsStarted() || verify_to_dump_)
David Sehr709b0702016-10-13 09:12:37 -07004534 << dex_file_->PrettyMethod(dex_method_idx_, true) << "@" << work_insn_idx_;
Andreas Gampe76bd8802014-12-10 16:43:58 -08004535
Mathieu Chartiere401d142015-04-22 13:56:20 -07004536 ArtMethod* res_method = GetQuickInvokedMethod(inst, work_line_.get(), is_range, false);
Ian Rogers7b078e82014-09-10 14:44:24 -07004537 if (res_method == nullptr) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004538 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer method from " << inst->Name();
Ian Rogers7b078e82014-09-10 14:44:24 -07004539 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004540 }
Andreas Gampe7c038102014-10-27 20:08:46 -07004541 if (FailOrAbort(this, !res_method->IsDirect(), "Quick-invoked method is direct at ",
4542 work_insn_idx_)) {
4543 return nullptr;
4544 }
4545 if (FailOrAbort(this, !res_method->IsStatic(), "Quick-invoked method is static at ",
4546 work_insn_idx_)) {
4547 return nullptr;
4548 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004549
4550 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
4551 // match the call to the signature. Also, we might be calling through an abstract method
4552 // definition (which doesn't have register count values).
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004553 const RegType& actual_arg_type = work_line_->GetInvocationThis(this, inst);
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004554 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
Ian Rogers7b078e82014-09-10 14:44:24 -07004555 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004556 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004557 const size_t expected_args = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
4558 /* caught by static verifier */
4559 DCHECK(is_range || expected_args <= 5);
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08004560 if (expected_args > code_item_accessor_.OutsSize()) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004561 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08004562 << ") exceeds outsSize (" << code_item_accessor_.OutsSize() << ")";
Ian Rogers7b078e82014-09-10 14:44:24 -07004563 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004564 }
4565
4566 /*
4567 * Check the "this" argument, which must be an instance of the class that declared the method.
4568 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
4569 * rigorous check here (which is okay since we have to do it at runtime).
4570 */
David Brazdil68b5c0b2016-01-19 14:25:29 +00004571 // Note: given an uninitialized type, this should always fail. Constructors aren't virtual.
4572 if (actual_arg_type.IsUninitializedTypes() && !res_method->IsConstructor()) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004573 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
Ian Rogers7b078e82014-09-10 14:44:24 -07004574 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004575 }
4576 if (!actual_arg_type.IsZero()) {
4577 mirror::Class* klass = res_method->GetDeclaringClass();
Ian Rogers1ff3c982014-08-12 02:30:58 -07004578 std::string temp;
Ian Rogersd8f69b02014-09-10 21:43:52 +00004579 const RegType& res_method_class =
Andreas Gampef23f33d2015-06-23 14:18:17 -07004580 FromClass(klass->GetDescriptor(&temp), klass, klass->CannotBeAssignedFromOtherTypes());
David Brazdilca3c8c32016-09-06 14:04:48 +01004581 if (!res_method_class.IsAssignableFrom(actual_arg_type, this)) {
David Brazdil68b5c0b2016-01-19 14:25:29 +00004582 Fail(actual_arg_type.IsUninitializedTypes() // Just overcautious - should have never
4583 ? VERIFY_ERROR_BAD_CLASS_HARD // quickened this.
4584 : actual_arg_type.IsUnresolvedTypes()
4585 ? VERIFY_ERROR_NO_CLASS
4586 : VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004587 << "' not instance of '" << res_method_class << "'";
Ian Rogers7b078e82014-09-10 14:44:24 -07004588 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004589 }
4590 }
4591 /*
4592 * Process the target method's signature. This signature may or may not
4593 * have been verified, so we can't assume it's properly formed.
4594 */
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07004595 const DexFile::TypeList* params = res_method->GetParameterTypeList();
Ian Rogers7b078e82014-09-10 14:44:24 -07004596 size_t params_size = params == nullptr ? 0 : params->Size();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004597 uint32_t arg[5];
4598 if (!is_range) {
Ian Rogers29a26482014-05-02 15:27:29 -07004599 inst->GetVarArgs(arg);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004600 }
4601 size_t actual_args = 1;
4602 for (size_t param_index = 0; param_index < params_size; param_index++) {
4603 if (actual_args >= expected_args) {
David Sehr709b0702016-10-13 09:12:37 -07004604 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invalid call to '"
4605 << res_method->PrettyMethod()
Brian Carlstrom93c33962013-07-26 10:37:43 -07004606 << "'. Expected " << expected_args
4607 << " arguments, processing argument " << actual_args
4608 << " (where longs/doubles count twice).";
Ian Rogers7b078e82014-09-10 14:44:24 -07004609 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004610 }
4611 const char* descriptor =
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07004612 res_method->GetTypeDescriptorFromTypeIdx(params->GetTypeItem(param_index).type_idx_);
Ian Rogers7b078e82014-09-10 14:44:24 -07004613 if (descriptor == nullptr) {
David Sehr709b0702016-10-13 09:12:37 -07004614 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of "
4615 << res_method->PrettyMethod()
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004616 << " missing signature component";
Ian Rogers7b078e82014-09-10 14:44:24 -07004617 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004618 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00004619 const RegType& reg_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004620 uint32_t get_reg = is_range ? inst->VRegC_3rc() + actual_args : arg[actual_args];
Ian Rogers7b078e82014-09-10 14:44:24 -07004621 if (!work_line_->VerifyRegisterType(this, get_reg, reg_type)) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004622 return res_method;
4623 }
4624 actual_args = reg_type.IsLongOrDoubleTypes() ? actual_args + 2 : actual_args + 1;
4625 }
4626 if (actual_args != expected_args) {
David Sehr709b0702016-10-13 09:12:37 -07004627 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of "
4628 << res_method->PrettyMethod() << " expected "
4629 << expected_args << " arguments, found " << actual_args;
Ian Rogers7b078e82014-09-10 14:44:24 -07004630 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004631 } else {
4632 return res_method;
4633 }
4634}
4635
Ian Rogers62342ec2013-06-11 10:26:37 -07004636void MethodVerifier::VerifyNewArray(const Instruction* inst, bool is_filled, bool is_range) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08004637 dex::TypeIndex type_idx;
Sebastien Hertz5243e912013-05-21 10:55:07 +02004638 if (!is_filled) {
4639 DCHECK_EQ(inst->Opcode(), Instruction::NEW_ARRAY);
Andreas Gampea5b09a62016-11-17 15:21:22 -08004640 type_idx = dex::TypeIndex(inst->VRegC_22c());
Sebastien Hertz5243e912013-05-21 10:55:07 +02004641 } else if (!is_range) {
4642 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY);
Andreas Gampea5b09a62016-11-17 15:21:22 -08004643 type_idx = dex::TypeIndex(inst->VRegB_35c());
Sebastien Hertz5243e912013-05-21 10:55:07 +02004644 } else {
4645 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY_RANGE);
Andreas Gampea5b09a62016-11-17 15:21:22 -08004646 type_idx = dex::TypeIndex(inst->VRegB_3rc());
Sebastien Hertz5243e912013-05-21 10:55:07 +02004647 }
Andreas Gampe98be1a92017-08-28 08:25:45 -07004648 const RegType& res_type = ResolveClass<CheckAccess::kYes>(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004649 if (res_type.IsConflict()) { // bad class
4650 DCHECK_NE(failures_.size(), 0U);
Ian Rogers0c4a5062012-02-03 15:18:59 -08004651 } else {
4652 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
4653 if (!res_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07004654 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "new-array on non-array class " << res_type;
Ian Rogers0c4a5062012-02-03 15:18:59 -08004655 } else if (!is_filled) {
4656 /* make sure "size" register is valid type */
Ian Rogers7b078e82014-09-10 14:44:24 -07004657 work_line_->VerifyRegisterType(this, inst->VRegB_22c(), reg_types_.Integer());
Ian Rogers0c4a5062012-02-03 15:18:59 -08004658 /* set register type to array class */
Ian Rogersd8f69b02014-09-10 21:43:52 +00004659 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
Andreas Gampead238ce2015-08-24 21:13:08 -07004660 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_22c(), precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08004661 } else {
Andreas Gampebb18a032016-03-22 20:34:25 -07004662 DCHECK(!res_type.IsUnresolvedMergedReference());
Ian Rogers0c4a5062012-02-03 15:18:59 -08004663 // Verify each register. If "arg_count" is bad, VerifyRegisterType() will run off the end of
4664 // the list and fail. It's legal, if silly, for arg_count to be zero.
Ian Rogersd8f69b02014-09-10 21:43:52 +00004665 const RegType& expected_type = reg_types_.GetComponentType(res_type, GetClassLoader());
Sebastien Hertz5243e912013-05-21 10:55:07 +02004666 uint32_t arg_count = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
4667 uint32_t arg[5];
4668 if (!is_range) {
Ian Rogers29a26482014-05-02 15:27:29 -07004669 inst->GetVarArgs(arg);
Sebastien Hertz5243e912013-05-21 10:55:07 +02004670 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08004671 for (size_t ui = 0; ui < arg_count; ui++) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02004672 uint32_t get_reg = is_range ? inst->VRegC_3rc() + ui : arg[ui];
Ian Rogers7b078e82014-09-10 14:44:24 -07004673 if (!work_line_->VerifyRegisterType(this, get_reg, expected_type)) {
4674 work_line_->SetResultRegisterType(this, reg_types_.Conflict());
Ian Rogers0c4a5062012-02-03 15:18:59 -08004675 return;
4676 }
4677 }
4678 // filled-array result goes into "result" register
Ian Rogersd8f69b02014-09-10 21:43:52 +00004679 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
Ian Rogers7b078e82014-09-10 14:44:24 -07004680 work_line_->SetResultRegisterType(this, precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08004681 }
4682 }
4683}
4684
Sebastien Hertz5243e912013-05-21 10:55:07 +02004685void MethodVerifier::VerifyAGet(const Instruction* inst,
Ian Rogersd8f69b02014-09-10 21:43:52 +00004686 const RegType& insn_type, bool is_primitive) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004687 const RegType& index_type = work_line_->GetRegisterType(this, inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07004688 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07004689 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07004690 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004691 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegB_23x());
Ian Rogers89310de2012-02-01 13:47:30 -08004692 if (array_type.IsZero()) {
Nicolas Geoffray4824c272015-06-24 15:53:03 +01004693 have_pending_runtime_throw_failure_ = true;
Ian Rogers89310de2012-02-01 13:47:30 -08004694 // Null array class; this code path will fail at runtime. Infer a merge-able type from the
4695 // instruction type. TODO: have a proper notion of bottom here.
4696 if (!is_primitive || insn_type.IsCategory1Types()) {
4697 // Reference or category 1
Andreas Gampead238ce2015-08-24 21:13:08 -07004698 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), reg_types_.Zero());
Ian Rogersd81871c2011-10-03 13:57:23 -07004699 } else {
Ian Rogers89310de2012-02-01 13:47:30 -08004700 // Category 2
Ian Rogers7b078e82014-09-10 14:44:24 -07004701 work_line_->SetRegisterTypeWide(this, inst->VRegA_23x(),
4702 reg_types_.FromCat2ConstLo(0, false),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004703 reg_types_.FromCat2ConstHi(0, false));
Ian Rogers89310de2012-02-01 13:47:30 -08004704 }
jeffhaofc3144e2012-02-01 17:21:15 -08004705 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07004706 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aget";
Andreas Gampe8d8fc482016-03-25 16:24:20 -07004707 } else if (array_type.IsUnresolvedMergedReference()) {
Andreas Gampebb18a032016-03-22 20:34:25 -07004708 // Unresolved array types must be reference array types.
4709 if (is_primitive) {
4710 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type
4711 << " source for category 1 aget";
4712 } else {
4713 Fail(VERIFY_ERROR_NO_CLASS) << "cannot verify aget for " << array_type
4714 << " because of missing class";
Andreas Gampe8d8fc482016-03-25 16:24:20 -07004715 // Approximate with java.lang.Object[].
4716 work_line_->SetRegisterType<LockOp::kClear>(this,
4717 inst->VRegA_23x(),
4718 reg_types_.JavaLangObject(false));
Andreas Gampebb18a032016-03-22 20:34:25 -07004719 }
Ian Rogers89310de2012-02-01 13:47:30 -08004720 } else {
4721 /* verify the class */
Ian Rogersd8f69b02014-09-10 21:43:52 +00004722 const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
jeffhaofc3144e2012-02-01 17:21:15 -08004723 if (!component_type.IsReferenceTypes() && !is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07004724 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08004725 << " source for aget-object";
4726 } else if (component_type.IsNonZeroReferenceTypes() && is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07004727 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08004728 << " source for category 1 aget";
4729 } else if (is_primitive && !insn_type.Equals(component_type) &&
4730 !((insn_type.IsInteger() && component_type.IsFloat()) ||
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004731 (insn_type.IsLong() && component_type.IsDouble()))) {
4732 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array type " << array_type
4733 << " incompatible with aget of type " << insn_type;
Ian Rogers89310de2012-02-01 13:47:30 -08004734 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004735 // Use knowledge of the field type which is stronger than the type inferred from the
4736 // instruction, which can't differentiate object types and ints from floats, longs from
4737 // doubles.
4738 if (!component_type.IsLowHalf()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07004739 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), component_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004740 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004741 work_line_->SetRegisterTypeWide(this, inst->VRegA_23x(), component_type,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004742 component_type.HighHalf(&reg_types_));
4743 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004744 }
4745 }
4746 }
4747}
4748
Ian Rogersd8f69b02014-09-10 21:43:52 +00004749void MethodVerifier::VerifyPrimitivePut(const RegType& target_type, const RegType& insn_type,
Jeff Haofe1f7c82013-08-01 14:50:24 -07004750 const uint32_t vregA) {
4751 // Primitive assignability rules are weaker than regular assignability rules.
4752 bool instruction_compatible;
4753 bool value_compatible;
Ian Rogers7b078e82014-09-10 14:44:24 -07004754 const RegType& value_type = work_line_->GetRegisterType(this, vregA);
Jeff Haofe1f7c82013-08-01 14:50:24 -07004755 if (target_type.IsIntegralTypes()) {
Jeff Haoa4647482013-08-06 15:35:47 -07004756 instruction_compatible = target_type.Equals(insn_type);
Jeff Haofe1f7c82013-08-01 14:50:24 -07004757 value_compatible = value_type.IsIntegralTypes();
4758 } else if (target_type.IsFloat()) {
4759 instruction_compatible = insn_type.IsInteger(); // no put-float, so expect put-int
4760 value_compatible = value_type.IsFloatTypes();
4761 } else if (target_type.IsLong()) {
4762 instruction_compatible = insn_type.IsLong();
Andreas Gampe376fa682014-09-07 13:06:12 -07004763 // Additional register check: this is not checked statically (as part of VerifyInstructions),
4764 // as target_type depends on the resolved type of the field.
4765 if (instruction_compatible && work_line_->NumRegs() > vregA + 1) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004766 const RegType& value_type_hi = work_line_->GetRegisterType(this, vregA + 1);
Andreas Gampe376fa682014-09-07 13:06:12 -07004767 value_compatible = value_type.IsLongTypes() && value_type.CheckWidePair(value_type_hi);
4768 } else {
4769 value_compatible = false;
4770 }
Jeff Haofe1f7c82013-08-01 14:50:24 -07004771 } else if (target_type.IsDouble()) {
4772 instruction_compatible = insn_type.IsLong(); // no put-double, so expect put-long
Andreas Gampe376fa682014-09-07 13:06:12 -07004773 // Additional register check: this is not checked statically (as part of VerifyInstructions),
4774 // as target_type depends on the resolved type of the field.
4775 if (instruction_compatible && work_line_->NumRegs() > vregA + 1) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004776 const RegType& value_type_hi = work_line_->GetRegisterType(this, vregA + 1);
Andreas Gampe376fa682014-09-07 13:06:12 -07004777 value_compatible = value_type.IsDoubleTypes() && value_type.CheckWidePair(value_type_hi);
4778 } else {
4779 value_compatible = false;
4780 }
Jeff Haofe1f7c82013-08-01 14:50:24 -07004781 } else {
4782 instruction_compatible = false; // reference with primitive store
4783 value_compatible = false; // unused
4784 }
4785 if (!instruction_compatible) {
4786 // This is a global failure rather than a class change failure as the instructions and
4787 // the descriptors for the type should have been consistent within the same file at
4788 // compile time.
4789 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "put insn has type '" << insn_type
4790 << "' but expected type '" << target_type << "'";
4791 return;
4792 }
4793 if (!value_compatible) {
4794 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
4795 << " of type " << value_type << " but expected " << target_type << " for put";
4796 return;
4797 }
4798}
4799
Sebastien Hertz5243e912013-05-21 10:55:07 +02004800void MethodVerifier::VerifyAPut(const Instruction* inst,
Ian Rogersd8f69b02014-09-10 21:43:52 +00004801 const RegType& insn_type, bool is_primitive) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004802 const RegType& index_type = work_line_->GetRegisterType(this, inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07004803 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07004804 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07004805 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004806 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegB_23x());
Ian Rogers89310de2012-02-01 13:47:30 -08004807 if (array_type.IsZero()) {
Nicolas Geoffray66389fb2015-06-19 10:35:42 +01004808 // Null array type; this code path will fail at runtime.
4809 // Still check that the given value matches the instruction's type.
Andreas Gampe4bf4c782015-08-14 14:07:43 -07004810 // Note: this is, as usual, complicated by the fact the the instruction isn't fully typed
4811 // and fits multiple register types.
4812 const RegType* modified_reg_type = &insn_type;
4813 if ((modified_reg_type == &reg_types_.Integer()) ||
4814 (modified_reg_type == &reg_types_.LongLo())) {
4815 // May be integer or float | long or double. Overwrite insn_type accordingly.
4816 const RegType& value_type = work_line_->GetRegisterType(this, inst->VRegA_23x());
4817 if (modified_reg_type == &reg_types_.Integer()) {
4818 if (&value_type == &reg_types_.Float()) {
4819 modified_reg_type = &value_type;
4820 }
4821 } else {
4822 if (&value_type == &reg_types_.DoubleLo()) {
4823 modified_reg_type = &value_type;
4824 }
4825 }
4826 }
4827 work_line_->VerifyRegisterType(this, inst->VRegA_23x(), *modified_reg_type);
jeffhaofc3144e2012-02-01 17:21:15 -08004828 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07004829 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aput";
Andreas Gampe8d8fc482016-03-25 16:24:20 -07004830 } else if (array_type.IsUnresolvedMergedReference()) {
Andreas Gampebb18a032016-03-22 20:34:25 -07004831 // Unresolved array types must be reference array types.
4832 if (is_primitive) {
4833 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "put insn has type '" << insn_type
4834 << "' but unresolved type '" << array_type << "'";
4835 } else {
4836 Fail(VERIFY_ERROR_NO_CLASS) << "cannot verify aput for " << array_type
4837 << " because of missing class";
4838 }
Ian Rogers89310de2012-02-01 13:47:30 -08004839 } else {
Ian Rogersd8f69b02014-09-10 21:43:52 +00004840 const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
Jeff Haofe1f7c82013-08-01 14:50:24 -07004841 const uint32_t vregA = inst->VRegA_23x();
Jeff Haob24b4a72013-07-31 13:47:31 -07004842 if (is_primitive) {
Jeff Haofe1f7c82013-08-01 14:50:24 -07004843 VerifyPrimitivePut(component_type, insn_type, vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07004844 } else {
Jeff Haob24b4a72013-07-31 13:47:31 -07004845 if (!component_type.IsReferenceTypes()) {
4846 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
4847 << " source for aput-object";
4848 } else {
4849 // The instruction agrees with the type of array, confirm the value to be stored does too
4850 // Note: we use the instruction type (rather than the component type) for aput-object as
4851 // incompatible classes will be caught at runtime as an array store exception
Ian Rogers7b078e82014-09-10 14:44:24 -07004852 work_line_->VerifyRegisterType(this, vregA, insn_type);
Jeff Haob24b4a72013-07-31 13:47:31 -07004853 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004854 }
4855 }
4856 }
4857}
4858
Mathieu Chartierc7853442015-03-27 14:35:38 -07004859ArtField* MethodVerifier::GetStaticField(int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08004860 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
4861 // Check access to class
Andreas Gampe98be1a92017-08-28 08:25:45 -07004862 const RegType& klass_type = ResolveClass<CheckAccess::kYes>(field_id.class_idx_);
Brian Carlstrom7934ac22013-07-26 10:54:15 -07004863 if (klass_type.IsConflict()) { // bad class
Ian Rogersad0b3a32012-04-16 14:50:24 -07004864 AppendToLastFailMessage(StringPrintf(" in attempt to access static field %d (%s) in %s",
4865 field_idx, dex_file_->GetFieldName(field_id),
4866 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers7b078e82014-09-10 14:44:24 -07004867 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08004868 }
Elliott Hughesb25c3f62012-03-26 16:35:06 -07004869 if (klass_type.IsUnresolvedTypes()) {
Andreas Gampe629be512017-08-25 17:09:32 -07004870 // Accessibility checks depend on resolved fields.
4871 DCHECK(klass_type.Equals(GetDeclaringClass()) || !failures_.empty());
4872
Ian Rogers7b078e82014-09-10 14:44:24 -07004873 return nullptr; // Can't resolve Class so no more to do here, will do checking at runtime.
Ian Rogers90040192011-12-16 08:54:29 -08004874 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07004875 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
David Brazdilca3c8c32016-09-06 14:04:48 +01004876 ArtField* field = class_linker->ResolveFieldJLS(*dex_file_, field_idx, dex_cache_, class_loader_);
4877
4878 // Record result of the field resolution attempt.
4879 VerifierDeps::MaybeRecordFieldResolution(*dex_file_, field_idx, field);
4880
Ian Rogers7b078e82014-09-10 14:44:24 -07004881 if (field == nullptr) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07004882 VLOG(verifier) << "Unable to resolve static field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07004883 << dex_file_->GetFieldName(field_id) << ") in "
4884 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogers7b078e82014-09-10 14:44:24 -07004885 DCHECK(self_->IsExceptionPending());
4886 self_->ClearException();
4887 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004888 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
4889 field->GetAccessFlags())) {
David Sehr709b0702016-10-13 09:12:37 -07004890 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access static field " << field->PrettyField()
Ian Rogersad0b3a32012-04-16 14:50:24 -07004891 << " from " << GetDeclaringClass();
Ian Rogers7b078e82014-09-10 14:44:24 -07004892 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07004893 } else if (!field->IsStatic()) {
David Sehr709b0702016-10-13 09:12:37 -07004894 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << field->PrettyField() << " to be static";
Ian Rogers7b078e82014-09-10 14:44:24 -07004895 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07004896 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07004897 return field;
Ian Rogersd81871c2011-10-03 13:57:23 -07004898}
4899
Mathieu Chartierc7853442015-03-27 14:35:38 -07004900ArtField* MethodVerifier::GetInstanceField(const RegType& obj_type, int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08004901 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
Aart Bik31883642016-06-06 15:02:44 -07004902 // Check access to class.
Andreas Gampe98be1a92017-08-28 08:25:45 -07004903 const RegType& klass_type = ResolveClass<CheckAccess::kYes>(field_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004904 if (klass_type.IsConflict()) {
4905 AppendToLastFailMessage(StringPrintf(" in attempt to access instance field %d (%s) in %s",
4906 field_idx, dex_file_->GetFieldName(field_id),
4907 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers7b078e82014-09-10 14:44:24 -07004908 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08004909 }
jeffhao8cd6dda2012-02-22 10:15:34 -08004910 if (klass_type.IsUnresolvedTypes()) {
Andreas Gampe629be512017-08-25 17:09:32 -07004911 // Accessibility checks depend on resolved fields.
4912 DCHECK(klass_type.Equals(GetDeclaringClass()) || !failures_.empty());
4913
Ian Rogers7b078e82014-09-10 14:44:24 -07004914 return nullptr; // Can't resolve Class so no more to do here
Ian Rogers90040192011-12-16 08:54:29 -08004915 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07004916 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
David Brazdilca3c8c32016-09-06 14:04:48 +01004917 ArtField* field = class_linker->ResolveFieldJLS(*dex_file_, field_idx, dex_cache_, class_loader_);
4918
4919 // Record result of the field resolution attempt.
4920 VerifierDeps::MaybeRecordFieldResolution(*dex_file_, field_idx, field);
4921
Ian Rogers7b078e82014-09-10 14:44:24 -07004922 if (field == nullptr) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07004923 VLOG(verifier) << "Unable to resolve instance field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07004924 << dex_file_->GetFieldName(field_id) << ") in "
4925 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogers7b078e82014-09-10 14:44:24 -07004926 DCHECK(self_->IsExceptionPending());
4927 self_->ClearException();
4928 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07004929 } else if (obj_type.IsZero()) {
Aart Bik31883642016-06-06 15:02:44 -07004930 // Cannot infer and check type, however, access will cause null pointer exception.
4931 // Fall through into a few last soft failure checks below.
Stephen Kyle695c5982014-08-22 15:03:07 +01004932 } else if (!obj_type.IsReferenceTypes()) {
Aart Bik31883642016-06-06 15:02:44 -07004933 // Trying to read a field from something that isn't a reference.
Stephen Kyle695c5982014-08-22 15:03:07 +01004934 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance field access on object that has "
4935 << "non-reference type " << obj_type;
Ian Rogers7b078e82014-09-10 14:44:24 -07004936 return nullptr;
Ian Rogerse1758fe2012-04-19 11:31:15 -07004937 } else {
David Brazdil0d638bb2016-07-27 15:29:25 +01004938 std::string temp;
Mathieu Chartier3398c782016-09-30 10:27:43 -07004939 ObjPtr<mirror::Class> klass = field->GetDeclaringClass();
Ian Rogersd8f69b02014-09-10 21:43:52 +00004940 const RegType& field_klass =
Mathieu Chartier3398c782016-09-30 10:27:43 -07004941 FromClass(klass->GetDescriptor(&temp),
Mathieu Chartier1cc62e42016-10-03 18:01:28 -07004942 klass.Ptr(),
Mathieu Chartier3398c782016-09-30 10:27:43 -07004943 klass->CannotBeAssignedFromOtherTypes());
David Brazdil68b5c0b2016-01-19 14:25:29 +00004944 if (obj_type.IsUninitializedTypes()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07004945 // Field accesses through uninitialized references are only allowable for constructors where
David Brazdil68b5c0b2016-01-19 14:25:29 +00004946 // the field is declared in this class.
4947 // Note: this IsConstructor check is technically redundant, as UninitializedThis should only
4948 // appear in constructors.
4949 if (!obj_type.IsUninitializedThisReference() ||
4950 !IsConstructor() ||
4951 !field_klass.Equals(GetDeclaringClass())) {
David Sehr709b0702016-10-13 09:12:37 -07004952 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "cannot access instance field " << field->PrettyField()
David Brazdil68b5c0b2016-01-19 14:25:29 +00004953 << " of a not fully initialized object within the context"
David Sehr709b0702016-10-13 09:12:37 -07004954 << " of " << dex_file_->PrettyMethod(dex_method_idx_);
David Brazdil68b5c0b2016-01-19 14:25:29 +00004955 return nullptr;
4956 }
David Brazdilca3c8c32016-09-06 14:04:48 +01004957 } else if (!field_klass.IsAssignableFrom(obj_type, this)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07004958 // Trying to access C1.field1 using reference of type C2, which is neither C1 or a sub-class
4959 // of C1. For resolution to occur the declared class of the field must be compatible with
4960 // obj_type, we've discovered this wasn't so, so report the field didn't exist.
Andreas Gampe66596242016-04-14 10:55:04 -07004961 VerifyError type;
4962 bool is_aot = Runtime::Current()->IsAotCompiler();
4963 if (is_aot && (field_klass.IsUnresolvedTypes() || obj_type.IsUnresolvedTypes())) {
4964 // Compiler & unresolved types involved, retry at runtime.
4965 type = VerifyError::VERIFY_ERROR_NO_CLASS;
4966 } else {
Andreas Gampe8f4ade02016-04-15 10:09:16 -07004967 // Classes known (resolved; and thus assignability check is precise), or we are at runtime
4968 // and still missing classes. This is a hard failure.
Andreas Gampe66596242016-04-14 10:55:04 -07004969 type = VerifyError::VERIFY_ERROR_BAD_CLASS_HARD;
4970 }
David Sehr709b0702016-10-13 09:12:37 -07004971 Fail(type) << "cannot access instance field " << field->PrettyField()
Andreas Gampe66596242016-04-14 10:55:04 -07004972 << " from object of type " << obj_type;
Ian Rogers7b078e82014-09-10 14:44:24 -07004973 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004974 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004975 }
Aart Bik31883642016-06-06 15:02:44 -07004976
4977 // Few last soft failure checks.
4978 if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
4979 field->GetAccessFlags())) {
David Sehr709b0702016-10-13 09:12:37 -07004980 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access instance field " << field->PrettyField()
Aart Bik31883642016-06-06 15:02:44 -07004981 << " from " << GetDeclaringClass();
4982 return nullptr;
4983 } else if (field->IsStatic()) {
David Sehr709b0702016-10-13 09:12:37 -07004984 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << field->PrettyField()
Aart Bik31883642016-06-06 15:02:44 -07004985 << " to not be static";
4986 return nullptr;
4987 }
4988
4989 return field;
Ian Rogersd81871c2011-10-03 13:57:23 -07004990}
4991
Andreas Gampe896df402014-10-20 22:25:29 -07004992template <MethodVerifier::FieldAccessType kAccType>
4993void MethodVerifier::VerifyISFieldAccess(const Instruction* inst, const RegType& insn_type,
4994 bool is_primitive, bool is_static) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02004995 uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
Mathieu Chartierc7853442015-03-27 14:35:38 -07004996 ArtField* field;
Ian Rogersb94a27b2011-10-26 00:33:41 -07004997 if (is_static) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07004998 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07004999 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07005000 const RegType& object_type = work_line_->GetRegisterType(this, inst->VRegB_22c());
David Brazdil68b5c0b2016-01-19 14:25:29 +00005001
5002 // One is not allowed to access fields on uninitialized references, except to write to
5003 // fields in the constructor (before calling another constructor).
5004 // GetInstanceField does an assignability check which will fail for uninitialized types.
5005 // We thus modify the type if the uninitialized reference is a "this" reference (this also
5006 // checks at the same time that we're verifying a constructor).
5007 bool should_adjust = (kAccType == FieldAccessType::kAccPut) &&
5008 object_type.IsUninitializedThisReference();
5009 const RegType& adjusted_type = should_adjust
5010 ? GetRegTypeCache()->FromUninitialized(object_type)
5011 : object_type;
5012 field = GetInstanceField(adjusted_type, field_idx);
Andreas Gampe896df402014-10-20 22:25:29 -07005013 if (UNLIKELY(have_pending_hard_failure_)) {
5014 return;
5015 }
Alex Light4a2c8fc2016-02-12 11:01:54 -08005016 if (should_adjust) {
5017 if (field == nullptr) {
5018 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "Might be accessing a superclass instance field prior "
5019 << "to the superclass being initialized in "
David Sehr709b0702016-10-13 09:12:37 -07005020 << dex_file_->PrettyMethod(dex_method_idx_);
Alex Light4a2c8fc2016-02-12 11:01:54 -08005021 } else if (field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
5022 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "cannot access superclass instance field "
David Sehr709b0702016-10-13 09:12:37 -07005023 << field->PrettyField() << " of a not fully initialized "
Alex Light4a2c8fc2016-02-12 11:01:54 -08005024 << "object within the context of "
David Sehr709b0702016-10-13 09:12:37 -07005025 << dex_file_->PrettyMethod(dex_method_idx_);
Alex Light4a2c8fc2016-02-12 11:01:54 -08005026 return;
5027 }
5028 }
Ian Rogersb94a27b2011-10-26 00:33:41 -07005029 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00005030 const RegType* field_type = nullptr;
Ian Rogers7b078e82014-09-10 14:44:24 -07005031 if (field != nullptr) {
Andreas Gampe896df402014-10-20 22:25:29 -07005032 if (kAccType == FieldAccessType::kAccPut) {
5033 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
David Sehr709b0702016-10-13 09:12:37 -07005034 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << field->PrettyField()
Andreas Gampe896df402014-10-20 22:25:29 -07005035 << " from other class " << GetDeclaringClass();
Aart Bikc2bc2652016-05-23 14:58:49 -07005036 // Keep hunting for possible hard fails.
Andreas Gampe896df402014-10-20 22:25:29 -07005037 }
5038 }
5039
Mathieu Chartier3398c782016-09-30 10:27:43 -07005040 ObjPtr<mirror::Class> field_type_class =
Vladimir Marko4098a7a2017-11-06 16:00:51 +00005041 can_load_classes_ ? field->ResolveType() : field->LookupType();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07005042 if (field_type_class != nullptr) {
Mathieu Chartier3398c782016-09-30 10:27:43 -07005043 field_type = &FromClass(field->GetTypeDescriptor(),
Mathieu Chartier1cc62e42016-10-03 18:01:28 -07005044 field_type_class.Ptr(),
Andreas Gampef23f33d2015-06-23 14:18:17 -07005045 field_type_class->CannotBeAssignedFromOtherTypes());
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08005046 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07005047 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
5048 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07005049 }
Andreas Gampe7da4c402017-08-25 11:30:48 -07005050 } else {
5051 // If we don't have the field (it seems we failed resolution) and this is a PUT, we need to
5052 // redo verification at runtime as the field may be final, unless the field id shows it's in
5053 // the same class.
5054 //
5055 // For simplicity, it is OK to not distinguish compile-time vs runtime, and post this an
5056 // ACCESS_FIELD failure at runtime. This has the same effect as NO_FIELD - punting the class
5057 // to the access-checks interpreter.
5058 //
5059 // Note: see b/34966607. This and above may be changed in the future.
5060 if (kAccType == FieldAccessType::kAccPut) {
5061 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
5062 const char* field_class_descriptor = dex_file_->GetFieldDeclaringClassDescriptor(field_id);
5063 const RegType* field_class_type = &reg_types_.FromDescriptor(GetClassLoader(),
5064 field_class_descriptor,
5065 false);
5066 if (!field_class_type->Equals(GetDeclaringClass())) {
5067 Fail(VERIFY_ERROR_ACCESS_FIELD) << "could not check field put for final field modify of "
5068 << field_class_descriptor
5069 << "."
5070 << dex_file_->GetFieldName(field_id)
5071 << " from other class "
5072 << GetDeclaringClass();
5073 }
5074 }
Ian Rogers0d604842012-04-16 14:50:24 -07005075 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07005076 if (field_type == nullptr) {
5077 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
5078 const char* descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
Mathieu Chartierbf99f772014-08-23 16:37:27 -07005079 field_type = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07005080 }
Sebastien Hertz757b3042014-03-28 14:34:28 +01005081 DCHECK(field_type != nullptr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02005082 const uint32_t vregA = (is_static) ? inst->VRegA_21c() : inst->VRegA_22c();
Andreas Gampe896df402014-10-20 22:25:29 -07005083 static_assert(kAccType == FieldAccessType::kAccPut || kAccType == FieldAccessType::kAccGet,
5084 "Unexpected third access type");
5085 if (kAccType == FieldAccessType::kAccPut) {
5086 // sput or iput.
5087 if (is_primitive) {
5088 VerifyPrimitivePut(*field_type, insn_type, vregA);
Ian Rogersad0b3a32012-04-16 14:50:24 -07005089 } else {
David Brazdilca3c8c32016-09-06 14:04:48 +01005090 if (!insn_type.IsAssignableFrom(*field_type, this)) {
Vladimir Marko414000e2015-06-23 17:45:21 +01005091 // If the field type is not a reference, this is a global failure rather than
5092 // a class change failure as the instructions and the descriptors for the type
5093 // should have been consistent within the same file at compile time.
5094 VerifyError error = field_type->IsReferenceTypes() ? VERIFY_ERROR_BAD_CLASS_SOFT
5095 : VERIFY_ERROR_BAD_CLASS_HARD;
David Sehr709b0702016-10-13 09:12:37 -07005096 Fail(error) << "expected field " << ArtField::PrettyField(field)
Vladimir Marko414000e2015-06-23 17:45:21 +01005097 << " to be compatible with type '" << insn_type
5098 << "' but found type '" << *field_type
5099 << "' in put-object";
Andreas Gampe896df402014-10-20 22:25:29 -07005100 return;
5101 }
5102 work_line_->VerifyRegisterType(this, vregA, *field_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07005103 }
Andreas Gampe896df402014-10-20 22:25:29 -07005104 } else if (kAccType == FieldAccessType::kAccGet) {
5105 // sget or iget.
5106 if (is_primitive) {
5107 if (field_type->Equals(insn_type) ||
5108 (field_type->IsFloat() && insn_type.IsInteger()) ||
5109 (field_type->IsDouble() && insn_type.IsLong())) {
5110 // expected that read is of the correct primitive type or that int reads are reading
5111 // floats or long reads are reading doubles
5112 } else {
5113 // This is a global failure rather than a class change failure as the instructions and
5114 // the descriptors for the type should have been consistent within the same file at
5115 // compile time
David Sehr709b0702016-10-13 09:12:37 -07005116 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << ArtField::PrettyField(field)
Andreas Gampe896df402014-10-20 22:25:29 -07005117 << " to be of type '" << insn_type
5118 << "' but found type '" << *field_type << "' in get";
5119 return;
5120 }
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08005121 } else {
David Brazdilca3c8c32016-09-06 14:04:48 +01005122 if (!insn_type.IsAssignableFrom(*field_type, this)) {
Vladimir Marko414000e2015-06-23 17:45:21 +01005123 // If the field type is not a reference, this is a global failure rather than
5124 // a class change failure as the instructions and the descriptors for the type
5125 // should have been consistent within the same file at compile time.
5126 VerifyError error = field_type->IsReferenceTypes() ? VERIFY_ERROR_BAD_CLASS_SOFT
5127 : VERIFY_ERROR_BAD_CLASS_HARD;
David Sehr709b0702016-10-13 09:12:37 -07005128 Fail(error) << "expected field " << ArtField::PrettyField(field)
Vladimir Marko414000e2015-06-23 17:45:21 +01005129 << " to be compatible with type '" << insn_type
5130 << "' but found type '" << *field_type
5131 << "' in get-object";
Andreas Gampe890da292015-07-06 17:20:18 -07005132 if (error != VERIFY_ERROR_BAD_CLASS_HARD) {
Andreas Gampead238ce2015-08-24 21:13:08 -07005133 work_line_->SetRegisterType<LockOp::kClear>(this, vregA, reg_types_.Conflict());
Andreas Gampe890da292015-07-06 17:20:18 -07005134 }
Andreas Gampe896df402014-10-20 22:25:29 -07005135 return;
5136 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07005137 }
Andreas Gampe896df402014-10-20 22:25:29 -07005138 if (!field_type->IsLowHalf()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07005139 work_line_->SetRegisterType<LockOp::kClear>(this, vregA, *field_type);
Andreas Gampe896df402014-10-20 22:25:29 -07005140 } else {
5141 work_line_->SetRegisterTypeWide(this, vregA, *field_type, field_type->HighHalf(&reg_types_));
5142 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07005143 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07005144 LOG(FATAL) << "Unexpected case.";
Ian Rogersd81871c2011-10-03 13:57:23 -07005145 }
5146}
5147
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08005148ArtField* MethodVerifier::GetQuickFieldAccess(const Instruction* inst, RegisterLine* reg_line) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08005149 DCHECK(IsInstructionIGetQuickOrIPutQuick(inst->Opcode())) << inst->Opcode();
Ian Rogers7b078e82014-09-10 14:44:24 -07005150 const RegType& object_type = reg_line->GetRegisterType(this, inst->VRegB_22c());
Ian Rogers9bc54402014-04-17 16:40:01 -07005151 if (!object_type.HasClass()) {
5152 VLOG(verifier) << "Failed to get mirror::Class* from '" << object_type << "'";
5153 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02005154 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02005155 uint32_t field_offset = static_cast<uint32_t>(inst->VRegC_22c());
Mathieu Chartierc7853442015-03-27 14:35:38 -07005156 ArtField* const f = ArtField::FindInstanceFieldWithOffset(object_type.GetClass(), field_offset);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08005157 DCHECK_EQ(f->GetOffset().Uint32Value(), field_offset);
Sebastien Hertz479fc1e2014-04-04 17:51:34 +02005158 if (f == nullptr) {
5159 VLOG(verifier) << "Failed to find instance field at offset '" << field_offset
David Sehr709b0702016-10-13 09:12:37 -07005160 << "' from '" << mirror::Class::PrettyDescriptor(object_type.GetClass()) << "'";
Sebastien Hertz479fc1e2014-04-04 17:51:34 +02005161 }
5162 return f;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02005163}
5164
Andreas Gampe896df402014-10-20 22:25:29 -07005165template <MethodVerifier::FieldAccessType kAccType>
5166void MethodVerifier::VerifyQuickFieldAccess(const Instruction* inst, const RegType& insn_type,
5167 bool is_primitive) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07005168 DCHECK(Runtime::Current()->IsStarted() || verify_to_dump_);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02005169
Mathieu Chartierc7853442015-03-27 14:35:38 -07005170 ArtField* field = GetQuickFieldAccess(inst, work_line_.get());
Ian Rogers7b078e82014-09-10 14:44:24 -07005171 if (field == nullptr) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02005172 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field from " << inst->Name();
5173 return;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02005174 }
Andreas Gampe896df402014-10-20 22:25:29 -07005175
5176 // For an IPUT_QUICK, we now test for final flag of the field.
5177 if (kAccType == FieldAccessType::kAccPut) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02005178 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
David Sehr709b0702016-10-13 09:12:37 -07005179 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << field->PrettyField()
Sebastien Hertzc15853b2013-06-25 17:36:27 +02005180 << " from other class " << GetDeclaringClass();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02005181 return;
5182 }
5183 }
Andreas Gampe896df402014-10-20 22:25:29 -07005184
5185 // Get the field type.
5186 const RegType* field_type;
5187 {
Vladimir Marko4098a7a2017-11-06 16:00:51 +00005188 ObjPtr<mirror::Class> field_type_class =
5189 can_load_classes_ ? field->ResolveType() : field->LookupType();
Andreas Gampe896df402014-10-20 22:25:29 -07005190
5191 if (field_type_class != nullptr) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07005192 field_type = &FromClass(field->GetTypeDescriptor(),
Mathieu Chartier1cc62e42016-10-03 18:01:28 -07005193 field_type_class.Ptr(),
Andreas Gampef23f33d2015-06-23 14:18:17 -07005194 field_type_class->CannotBeAssignedFromOtherTypes());
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02005195 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07005196 Thread* self = Thread::Current();
5197 DCHECK(!can_load_classes_ || self->IsExceptionPending());
5198 self->ClearException();
5199 field_type = &reg_types_.FromDescriptor(field->GetDeclaringClass()->GetClassLoader(),
Mathieu Chartierde40d472015-10-15 17:47:48 -07005200 field->GetTypeDescriptor(),
5201 false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02005202 }
Andreas Gampe896df402014-10-20 22:25:29 -07005203 if (field_type == nullptr) {
5204 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field type from " << inst->Name();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02005205 return;
5206 }
Andreas Gampe896df402014-10-20 22:25:29 -07005207 }
5208
5209 const uint32_t vregA = inst->VRegA_22c();
5210 static_assert(kAccType == FieldAccessType::kAccPut || kAccType == FieldAccessType::kAccGet,
5211 "Unexpected third access type");
5212 if (kAccType == FieldAccessType::kAccPut) {
5213 if (is_primitive) {
5214 // Primitive field assignability rules are weaker than regular assignability rules
5215 bool instruction_compatible;
5216 bool value_compatible;
5217 const RegType& value_type = work_line_->GetRegisterType(this, vregA);
5218 if (field_type->IsIntegralTypes()) {
5219 instruction_compatible = insn_type.IsIntegralTypes();
5220 value_compatible = value_type.IsIntegralTypes();
5221 } else if (field_type->IsFloat()) {
5222 instruction_compatible = insn_type.IsInteger(); // no [is]put-float, so expect [is]put-int
5223 value_compatible = value_type.IsFloatTypes();
5224 } else if (field_type->IsLong()) {
5225 instruction_compatible = insn_type.IsLong();
5226 value_compatible = value_type.IsLongTypes();
5227 } else if (field_type->IsDouble()) {
5228 instruction_compatible = insn_type.IsLong(); // no [is]put-double, so expect [is]put-long
5229 value_compatible = value_type.IsDoubleTypes();
5230 } else {
5231 instruction_compatible = false; // reference field with primitive store
5232 value_compatible = false; // unused
5233 }
5234 if (!instruction_compatible) {
5235 // This is a global failure rather than a class change failure as the instructions and
5236 // the descriptors for the type should have been consistent within the same file at
5237 // compile time
David Sehr709b0702016-10-13 09:12:37 -07005238 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << ArtField::PrettyField(field)
Andreas Gampe896df402014-10-20 22:25:29 -07005239 << " to be of type '" << insn_type
5240 << "' but found type '" << *field_type
5241 << "' in put";
5242 return;
5243 }
5244 if (!value_compatible) {
5245 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
5246 << " of type " << value_type
5247 << " but expected " << *field_type
David Sehr709b0702016-10-13 09:12:37 -07005248 << " for store to " << ArtField::PrettyField(field) << " in put";
Andreas Gampe896df402014-10-20 22:25:29 -07005249 return;
5250 }
5251 } else {
David Brazdilca3c8c32016-09-06 14:04:48 +01005252 if (!insn_type.IsAssignableFrom(*field_type, this)) {
David Sehr709b0702016-10-13 09:12:37 -07005253 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << ArtField::PrettyField(field)
Andreas Gampe896df402014-10-20 22:25:29 -07005254 << " to be compatible with type '" << insn_type
5255 << "' but found type '" << *field_type
5256 << "' in put-object";
5257 return;
5258 }
5259 work_line_->VerifyRegisterType(this, vregA, *field_type);
5260 }
5261 } else if (kAccType == FieldAccessType::kAccGet) {
5262 if (is_primitive) {
5263 if (field_type->Equals(insn_type) ||
5264 (field_type->IsFloat() && insn_type.IsIntegralTypes()) ||
5265 (field_type->IsDouble() && insn_type.IsLongTypes())) {
5266 // expected that read is of the correct primitive type or that int reads are reading
5267 // floats or long reads are reading doubles
5268 } else {
5269 // This is a global failure rather than a class change failure as the instructions and
5270 // the descriptors for the type should have been consistent within the same file at
5271 // compile time
David Sehr709b0702016-10-13 09:12:37 -07005272 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << ArtField::PrettyField(field)
Andreas Gampe896df402014-10-20 22:25:29 -07005273 << " to be of type '" << insn_type
5274 << "' but found type '" << *field_type << "' in Get";
5275 return;
5276 }
5277 } else {
David Brazdilca3c8c32016-09-06 14:04:48 +01005278 if (!insn_type.IsAssignableFrom(*field_type, this)) {
David Sehr709b0702016-10-13 09:12:37 -07005279 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << ArtField::PrettyField(field)
Andreas Gampe896df402014-10-20 22:25:29 -07005280 << " to be compatible with type '" << insn_type
5281 << "' but found type '" << *field_type
5282 << "' in get-object";
Andreas Gampead238ce2015-08-24 21:13:08 -07005283 work_line_->SetRegisterType<LockOp::kClear>(this, vregA, reg_types_.Conflict());
Andreas Gampe896df402014-10-20 22:25:29 -07005284 return;
5285 }
5286 }
5287 if (!field_type->IsLowHalf()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07005288 work_line_->SetRegisterType<LockOp::kClear>(this, vregA, *field_type);
Andreas Gampe896df402014-10-20 22:25:29 -07005289 } else {
5290 work_line_->SetRegisterTypeWide(this, vregA, *field_type, field_type->HighHalf(&reg_types_));
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02005291 }
5292 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07005293 LOG(FATAL) << "Unexpected case.";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02005294 }
5295}
5296
Ian Rogers776ac1f2012-04-13 23:36:36 -07005297bool MethodVerifier::CheckNotMoveException(const uint16_t* insns, int insn_idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07005298 if ((insns[insn_idx] & 0xff) == Instruction::MOVE_EXCEPTION) {
jeffhaod5347e02012-03-22 17:25:05 -07005299 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-exception";
Ian Rogersd81871c2011-10-03 13:57:23 -07005300 return false;
5301 }
5302 return true;
5303}
5304
Stephen Kyle9bc61992014-09-22 13:53:15 +01005305bool MethodVerifier::CheckNotMoveResult(const uint16_t* insns, int insn_idx) {
5306 if (((insns[insn_idx] & 0xff) >= Instruction::MOVE_RESULT) &&
5307 ((insns[insn_idx] & 0xff) <= Instruction::MOVE_RESULT_OBJECT)) {
5308 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-result*";
5309 return false;
5310 }
5311 return true;
5312}
5313
5314bool MethodVerifier::CheckNotMoveExceptionOrMoveResult(const uint16_t* insns, int insn_idx) {
5315 return (CheckNotMoveException(insns, insn_idx) && CheckNotMoveResult(insns, insn_idx));
5316}
5317
Ian Rogersebbdd872014-07-07 23:53:08 -07005318bool MethodVerifier::UpdateRegisters(uint32_t next_insn, RegisterLine* merge_line,
5319 bool update_merge_line) {
Ian Rogersd81871c2011-10-03 13:57:23 -07005320 bool changed = true;
5321 RegisterLine* target_line = reg_table_.GetLine(next_insn);
Mathieu Chartierde40d472015-10-15 17:47:48 -07005322 if (!GetInstructionFlags(next_insn).IsVisitedOrChanged()) {
jeffhaobdb76512011-09-07 11:43:16 -07005323 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07005324 * We haven't processed this instruction before, and we haven't touched the registers here, so
5325 * there's nothing to "merge". Copy the registers over and mark it as changed. (This is the
5326 * only way a register can transition out of "unknown", so this is not just an optimization.)
jeffhaobdb76512011-09-07 11:43:16 -07005327 */
Andreas Gampea727e372015-08-25 09:22:37 -07005328 target_line->CopyFromLine(merge_line);
Mathieu Chartierde40d472015-10-15 17:47:48 -07005329 if (GetInstructionFlags(next_insn).IsReturn()) {
Jeff Haob24b4a72013-07-31 13:47:31 -07005330 // Verify that the monitor stack is empty on return.
Andreas Gampea727e372015-08-25 09:22:37 -07005331 merge_line->VerifyMonitorStackEmpty(this);
5332
Ian Rogersb8c78592013-07-25 23:52:52 +00005333 // For returns we only care about the operand to the return, all other registers are dead.
5334 // Initialize them as conflicts so they don't add to GC and deoptimization information.
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08005335 const Instruction* ret_inst = &code_item_accessor_.InstructionAt(next_insn);
Andreas Gampea727e372015-08-25 09:22:37 -07005336 AdjustReturnLine(this, ret_inst, target_line);
Aart Bik31883642016-06-06 15:02:44 -07005337 // Directly bail if a hard failure was found.
Aart Bikb0526322016-06-01 14:06:00 -07005338 if (have_pending_hard_failure_) {
5339 return false;
5340 }
Ian Rogersb8c78592013-07-25 23:52:52 +00005341 }
jeffhaobdb76512011-09-07 11:43:16 -07005342 } else {
Mathieu Chartier361e04a2016-02-16 14:06:35 -08005343 RegisterLineArenaUniquePtr copy;
Andreas Gampeec6e6c12015-11-05 20:39:56 -08005344 if (kDebugVerify) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07005345 copy.reset(RegisterLine::Create(target_line->NumRegs(), this));
Ian Rogers7b0c5b42012-02-16 15:29:07 -08005346 copy->CopyFromLine(target_line);
5347 }
Ian Rogers7b078e82014-09-10 14:44:24 -07005348 changed = target_line->MergeRegisters(this, merge_line);
Ian Rogersad0b3a32012-04-16 14:50:24 -07005349 if (have_pending_hard_failure_) {
Ian Rogersd81871c2011-10-03 13:57:23 -07005350 return false;
jeffhaobdb76512011-09-07 11:43:16 -07005351 }
Andreas Gampeec6e6c12015-11-05 20:39:56 -08005352 if (kDebugVerify && changed) {
Elliott Hughes398f64b2012-03-26 18:05:48 -07005353 LogVerifyInfo() << "Merging at [" << reinterpret_cast<void*>(work_insn_idx_) << "]"
Elliott Hughesc073b072012-05-24 19:29:17 -07005354 << " to [" << reinterpret_cast<void*>(next_insn) << "]: " << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07005355 << copy->Dump(this) << " MERGE\n"
5356 << merge_line->Dump(this) << " ==\n"
5357 << target_line->Dump(this) << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07005358 }
Ian Rogersebbdd872014-07-07 23:53:08 -07005359 if (update_merge_line && changed) {
5360 merge_line->CopyFromLine(target_line);
5361 }
jeffhaobdb76512011-09-07 11:43:16 -07005362 }
Ian Rogersd81871c2011-10-03 13:57:23 -07005363 if (changed) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07005364 GetInstructionFlags(next_insn).SetChanged();
jeffhaobdb76512011-09-07 11:43:16 -07005365 }
5366 return true;
5367}
5368
Ian Rogers7b3ddd22013-02-21 15:19:52 -08005369InstructionFlags* MethodVerifier::CurrentInsnFlags() {
Mathieu Chartierde40d472015-10-15 17:47:48 -07005370 return &GetInstructionFlags(work_insn_idx_);
Ian Rogers776ac1f2012-04-13 23:36:36 -07005371}
5372
Ian Rogersd8f69b02014-09-10 21:43:52 +00005373const RegType& MethodVerifier::GetMethodReturnType() {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07005374 if (return_type_ == nullptr) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07005375 if (mirror_method_ != nullptr) {
Vladimir Markob45528c2017-07-27 14:14:28 +01005376 ObjPtr<mirror::Class> return_type_class = can_load_classes_
5377 ? mirror_method_->ResolveReturnType()
5378 : mirror_method_->LookupResolvedReturnType();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07005379 if (return_type_class != nullptr) {
Andreas Gampef23f33d2015-06-23 14:18:17 -07005380 return_type_ = &FromClass(mirror_method_->GetReturnTypeDescriptor(),
Vladimir Markob45528c2017-07-27 14:14:28 +01005381 return_type_class.Ptr(),
Andreas Gampef23f33d2015-06-23 14:18:17 -07005382 return_type_class->CannotBeAssignedFromOtherTypes());
Ian Rogersfc0e94b2013-09-23 23:51:32 -07005383 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07005384 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
5385 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07005386 }
5387 }
5388 if (return_type_ == nullptr) {
5389 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
5390 const DexFile::ProtoId& proto_id = dex_file_->GetMethodPrototype(method_id);
Andreas Gampea5b09a62016-11-17 15:21:22 -08005391 dex::TypeIndex return_type_idx = proto_id.return_type_idx_;
Ian Rogersfc0e94b2013-09-23 23:51:32 -07005392 const char* descriptor = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(return_type_idx));
Mathieu Chartierbf99f772014-08-23 16:37:27 -07005393 return_type_ = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07005394 }
5395 }
5396 return *return_type_;
Ian Rogersad0b3a32012-04-16 14:50:24 -07005397}
5398
Ian Rogersd8f69b02014-09-10 21:43:52 +00005399const RegType& MethodVerifier::GetDeclaringClass() {
Ian Rogers7b078e82014-09-10 14:44:24 -07005400 if (declaring_class_ == nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08005401 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
Brian Carlstrom93c33962013-07-26 10:37:43 -07005402 const char* descriptor
5403 = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(method_id.class_idx_));
Mathieu Chartiere401d142015-04-22 13:56:20 -07005404 if (mirror_method_ != nullptr) {
Ian Rogers637c65b2013-05-31 11:46:00 -07005405 mirror::Class* klass = mirror_method_->GetDeclaringClass();
Mathieu Chartierde40d472015-10-15 17:47:48 -07005406 declaring_class_ = &FromClass(descriptor, klass, klass->CannotBeAssignedFromOtherTypes());
Ian Rogers637c65b2013-05-31 11:46:00 -07005407 } else {
Mathieu Chartierbf99f772014-08-23 16:37:27 -07005408 declaring_class_ = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers637c65b2013-05-31 11:46:00 -07005409 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07005410 }
Ian Rogers637c65b2013-05-31 11:46:00 -07005411 return *declaring_class_;
Ian Rogersad0b3a32012-04-16 14:50:24 -07005412}
5413
Ian Rogers2bcb4a42012-11-08 10:39:18 -08005414std::vector<int32_t> MethodVerifier::DescribeVRegs(uint32_t dex_pc) {
5415 RegisterLine* line = reg_table_.GetLine(dex_pc);
Sebastien Hertzaa0c00c2014-03-14 17:58:54 +01005416 DCHECK(line != nullptr) << "No register line at DEX pc " << StringPrintf("0x%x", dex_pc);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08005417 std::vector<int32_t> result;
5418 for (size_t i = 0; i < line->NumRegs(); ++i) {
Ian Rogers7b078e82014-09-10 14:44:24 -07005419 const RegType& type = line->GetRegisterType(this, i);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08005420 if (type.IsConstant()) {
5421 result.push_back(type.IsPreciseConstant() ? kConstant : kImpreciseConstant);
Ian Rogers7b078e82014-09-10 14:44:24 -07005422 const ConstantType* const_val = down_cast<const ConstantType*>(&type);
5423 result.push_back(const_val->ConstantValue());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08005424 } else if (type.IsConstantLo()) {
5425 result.push_back(type.IsPreciseConstantLo() ? kConstant : kImpreciseConstant);
Ian Rogers7b078e82014-09-10 14:44:24 -07005426 const ConstantType* const_val = down_cast<const ConstantType*>(&type);
5427 result.push_back(const_val->ConstantValueLo());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08005428 } else if (type.IsConstantHi()) {
5429 result.push_back(type.IsPreciseConstantHi() ? kConstant : kImpreciseConstant);
Ian Rogers7b078e82014-09-10 14:44:24 -07005430 const ConstantType* const_val = down_cast<const ConstantType*>(&type);
5431 result.push_back(const_val->ConstantValueHi());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08005432 } else if (type.IsIntegralTypes()) {
5433 result.push_back(kIntVReg);
5434 result.push_back(0);
5435 } else if (type.IsFloat()) {
5436 result.push_back(kFloatVReg);
5437 result.push_back(0);
5438 } else if (type.IsLong()) {
5439 result.push_back(kLongLoVReg);
5440 result.push_back(0);
5441 result.push_back(kLongHiVReg);
5442 result.push_back(0);
5443 ++i;
5444 } else if (type.IsDouble()) {
5445 result.push_back(kDoubleLoVReg);
5446 result.push_back(0);
5447 result.push_back(kDoubleHiVReg);
5448 result.push_back(0);
5449 ++i;
5450 } else if (type.IsUndefined() || type.IsConflict() || type.IsHighHalf()) {
5451 result.push_back(kUndefined);
5452 result.push_back(0);
5453 } else {
Ian Rogers7b3ddd22013-02-21 15:19:52 -08005454 CHECK(type.IsNonZeroReferenceTypes());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08005455 result.push_back(kReferenceVReg);
5456 result.push_back(0);
5457 }
5458 }
5459 return result;
5460}
5461
Ian Rogersd8f69b02014-09-10 21:43:52 +00005462const RegType& MethodVerifier::DetermineCat1Constant(int32_t value, bool precise) {
Sebastien Hertz849600b2013-12-20 10:28:08 +01005463 if (precise) {
5464 // Precise constant type.
5465 return reg_types_.FromCat1Const(value, true);
5466 } else {
5467 // Imprecise constant type.
5468 if (value < -32768) {
5469 return reg_types_.IntConstant();
5470 } else if (value < -128) {
5471 return reg_types_.ShortConstant();
5472 } else if (value < 0) {
5473 return reg_types_.ByteConstant();
5474 } else if (value == 0) {
5475 return reg_types_.Zero();
5476 } else if (value == 1) {
5477 return reg_types_.One();
5478 } else if (value < 128) {
5479 return reg_types_.PosByteConstant();
5480 } else if (value < 32768) {
5481 return reg_types_.PosShortConstant();
5482 } else if (value < 65536) {
5483 return reg_types_.CharConstant();
5484 } else {
5485 return reg_types_.IntConstant();
5486 }
5487 }
5488}
5489
Elliott Hughes0a1038b2012-06-14 16:24:17 -07005490void MethodVerifier::Init() {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08005491 art::verifier::RegTypeCache::Init();
Brian Carlstrome7d856b2012-01-11 18:10:55 -08005492}
5493
Elliott Hughes0a1038b2012-06-14 16:24:17 -07005494void MethodVerifier::Shutdown() {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08005495 verifier::RegTypeCache::ShutDown();
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08005496}
jeffhaod1224c72012-02-29 13:43:08 -08005497
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -07005498void MethodVerifier::VisitStaticRoots(RootVisitor* visitor) {
5499 RegTypeCache::VisitStaticRoots(visitor);
Mathieu Chartier7c438b12014-09-12 17:01:24 -07005500}
5501
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -07005502void MethodVerifier::VisitRoots(RootVisitor* visitor, const RootInfo& root_info) {
5503 reg_types_.VisitRoots(visitor, root_info);
Mathieu Chartierc528dba2013-11-26 12:00:11 -08005504}
5505
Andreas Gampef23f33d2015-06-23 14:18:17 -07005506const RegType& MethodVerifier::FromClass(const char* descriptor,
5507 mirror::Class* klass,
5508 bool precise) {
5509 DCHECK(klass != nullptr);
5510 if (precise && !klass->IsInstantiable() && !klass->IsPrimitive()) {
5511 Fail(VerifyError::VERIFY_ERROR_NO_CLASS) << "Could not create precise reference for "
5512 << "non-instantiable klass " << descriptor;
5513 precise = false;
5514 }
5515 return reg_types_.FromClass(descriptor, klass, precise);
5516}
5517
Ian Rogersd81871c2011-10-03 13:57:23 -07005518} // namespace verifier
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07005519} // namespace art