blob: c40609e7596dcd58cbe7e0d2dd740beffead9792 [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
Mathieu Chartierc7853442015-03-27 14:35:38 -070021#include "art_field-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070022#include "art_method-inl.h"
Andreas Gampe542451c2016-07-26 09:02:02 -070023#include "base/enums.h"
Elliott Hughes07ed66b2012-12-12 18:34:25 -080024#include "base/logging.h"
Ian Rogers637c65b2013-05-31 11:46:00 -070025#include "base/mutex-inl.h"
Vladimir Marko637ee0b2015-09-04 12:47:41 +010026#include "base/stl_util.h"
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -080027#include "base/systrace.h"
Vladimir Marko80afd022015-05-19 18:08:00 +010028#include "base/time_utils.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070029#include "class_linker.h"
Vladimir Marko2b5eaa22013-12-13 13:59:30 +000030#include "compiler_callbacks.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070031#include "dex_file-inl.h"
Ian Rogersd0583802013-06-01 10:51:46 -070032#include "dex_instruction-inl.h"
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080033#include "dex_instruction_utils.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070034#include "dex_instruction_visitor.h"
Alex Lighteb7c1442015-08-31 13:17:42 -070035#include "experimental_flags.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070036#include "gc/accounting/card_table-inl.h"
Ian Rogers2bcb4a42012-11-08 10:39:18 -080037#include "indenter.h"
Ian Rogers84fa0742011-10-25 18:13:30 -070038#include "intern_table.h"
Ian Rogers0571d352011-11-03 19:51:38 -070039#include "leb128.h"
David Brazdilca3c8c32016-09-06 14:04:48 +010040#include "method_resolution_kind.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080041#include "mirror/class.h"
42#include "mirror/class-inl.h"
Ian Rogers39ebcb82013-05-30 16:57:23 -070043#include "mirror/dex_cache-inl.h"
Orion Hodsoncfa325e2016-10-13 10:25:54 +010044#include "mirror/method_handle_impl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080045#include "mirror/object-inl.h"
46#include "mirror/object_array-inl.h"
Ian Rogers7b078e82014-09-10 14:44:24 -070047#include "reg_type-inl.h"
Ian Rogers39ebcb82013-05-30 16:57:23 -070048#include "register_line-inl.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070049#include "runtime.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070050#include "scoped_thread_state_change-inl.h"
Vladimir Marko80afd022015-05-19 18:08:00 +010051#include "utils.h"
David Brazdilca3c8c32016-09-06 14:04:48 +010052#include "verifier_deps.h"
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070053#include "handle_scope-inl.h"
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070054
55namespace art {
Ian Rogersd81871c2011-10-03 13:57:23 -070056namespace verifier {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070057
Mathieu Chartier8e219ae2014-08-19 14:29:46 -070058static constexpr bool kTimeVerifyMethod = !kIsDebugBuild;
Andreas Gampeec6e6c12015-11-05 20:39:56 -080059static constexpr bool kDebugVerify = false;
Anwar Ghuloum75a43f12013-08-13 17:22:14 -070060// TODO: Add a constant to method_verifier to turn on verbose logging?
Ian Rogers2c8a8572011-10-24 17:11:36 -070061
Andreas Gampeebf850c2015-08-14 15:37:35 -070062// On VLOG(verifier), should we dump the whole state when we run into a hard failure?
63static constexpr bool kDumpRegLinesOnHardFailureIfVLOG = true;
64
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -080065// We print a warning blurb about "dx --no-optimize" when we find monitor-locking issues. Make
66// sure we only print this once.
67static bool gPrintedDxMonitorText = false;
68
Mathieu Chartierde40d472015-10-15 17:47:48 -070069PcToRegisterLineTable::PcToRegisterLineTable(ScopedArenaAllocator& arena)
70 : register_lines_(arena.Adapter(kArenaAllocVerifier)) {}
71
Ian Rogers7b3ddd22013-02-21 15:19:52 -080072void PcToRegisterLineTable::Init(RegisterTrackingMode mode, InstructionFlags* flags,
Ian Rogersd81871c2011-10-03 13:57:23 -070073 uint32_t insns_size, uint16_t registers_size,
Ian Rogers776ac1f2012-04-13 23:36:36 -070074 MethodVerifier* verifier) {
Ian Rogersd81871c2011-10-03 13:57:23 -070075 DCHECK_GT(insns_size, 0U);
Mathieu Chartierde40d472015-10-15 17:47:48 -070076 register_lines_.resize(insns_size);
Ian Rogersd81871c2011-10-03 13:57:23 -070077 for (uint32_t i = 0; i < insns_size; i++) {
78 bool interesting = false;
79 switch (mode) {
80 case kTrackRegsAll:
81 interesting = flags[i].IsOpcode();
82 break;
Sameer Abu Asal02c42232013-04-30 12:09:45 -070083 case kTrackCompilerInterestPoints:
Brian Carlstrom02c8cc62013-07-18 15:54:44 -070084 interesting = flags[i].IsCompileTimeInfoPoint() || flags[i].IsBranchTarget();
Ian Rogersd81871c2011-10-03 13:57:23 -070085 break;
86 case kTrackRegsBranches:
87 interesting = flags[i].IsBranchTarget();
88 break;
89 default:
90 break;
91 }
92 if (interesting) {
Mathieu Chartierde40d472015-10-15 17:47:48 -070093 register_lines_[i].reset(RegisterLine::Create(registers_size, verifier));
Ian Rogersd0fbd852013-09-24 18:17:04 -070094 }
95 }
96}
97
Mathieu Chartierde40d472015-10-15 17:47:48 -070098PcToRegisterLineTable::~PcToRegisterLineTable() {}
Ian Rogersd81871c2011-10-03 13:57:23 -070099
Andreas Gampe7c038102014-10-27 20:08:46 -0700100// Note: returns true on failure.
101ALWAYS_INLINE static inline bool FailOrAbort(MethodVerifier* verifier, bool condition,
102 const char* error_msg, uint32_t work_insn_idx) {
103 if (kIsDebugBuild) {
Andreas Gampef8f36c22016-11-19 14:51:52 -0800104 // In a debug build, abort if the error condition is wrong. Only warn if
105 // we are already aborting (as this verification is likely run to print
106 // lock information).
107 if (LIKELY(gAborting == 0)) {
108 DCHECK(condition) << error_msg << work_insn_idx;
109 } else {
110 if (!condition) {
111 LOG(ERROR) << error_msg << work_insn_idx;
112 verifier->Fail(VERIFY_ERROR_BAD_CLASS_HARD) << error_msg << work_insn_idx;
113 return true;
114 }
115 }
Andreas Gampe7c038102014-10-27 20:08:46 -0700116 } else {
117 // In a non-debug build, just fail the class.
118 if (!condition) {
119 verifier->Fail(VERIFY_ERROR_BAD_CLASS_HARD) << error_msg << work_insn_idx;
120 return true;
121 }
122 }
123
124 return false;
125}
126
Stephen Kyle7e541c92014-12-17 17:10:02 +0000127static void SafelyMarkAllRegistersAsConflicts(MethodVerifier* verifier, RegisterLine* reg_line) {
Andreas Gampef10b6e12015-08-12 10:48:12 -0700128 if (verifier->IsInstanceConstructor()) {
Stephen Kyle7e541c92014-12-17 17:10:02 +0000129 // Before we mark all regs as conflicts, check that we don't have an uninitialized this.
130 reg_line->CheckConstructorReturn(verifier);
131 }
132 reg_line->MarkAllRegistersAsConflicts(verifier);
133}
134
Ian Rogers7b078e82014-09-10 14:44:24 -0700135MethodVerifier::FailureKind MethodVerifier::VerifyClass(Thread* self,
136 mirror::Class* klass,
Andreas Gampe53e32d12015-12-09 21:03:23 -0800137 CompilerCallbacks* callbacks,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700138 bool allow_soft_failures,
Andreas Gampe5fd66d02016-09-12 20:22:19 -0700139 HardFailLogMode log_level,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700140 std::string* error) {
jeffhaobdb76512011-09-07 11:43:16 -0700141 if (klass->IsVerified()) {
jeffhaof1e6b7c2012-06-05 18:33:30 -0700142 return kNoFailure;
jeffhaobdb76512011-09-07 11:43:16 -0700143 }
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800144 bool early_failure = false;
145 std::string failure_message;
Mathieu Chartierf8322842014-05-16 10:59:25 -0700146 const DexFile& dex_file = klass->GetDexFile();
147 const DexFile::ClassDef* class_def = klass->GetClassDef();
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800148 mirror::Class* super = klass->GetSuperClass();
Ian Rogers1ff3c982014-08-12 02:30:58 -0700149 std::string temp;
Ian Rogers7b078e82014-09-10 14:44:24 -0700150 if (super == nullptr && strcmp("Ljava/lang/Object;", klass->GetDescriptor(&temp)) != 0) {
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800151 early_failure = true;
152 failure_message = " that has no super class";
Ian Rogers7b078e82014-09-10 14:44:24 -0700153 } else if (super != nullptr && super->IsFinal()) {
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800154 early_failure = true;
David Sehr709b0702016-10-13 09:12:37 -0700155 failure_message = " that attempts to sub-class final class " + super->PrettyDescriptor();
Ian Rogers7b078e82014-09-10 14:44:24 -0700156 } else if (class_def == nullptr) {
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800157 early_failure = true;
158 failure_message = " that isn't present in dex file " + dex_file.GetLocation();
159 }
160 if (early_failure) {
David Sehr709b0702016-10-13 09:12:37 -0700161 *error = "Verifier rejected class " + klass->PrettyDescriptor() + failure_message;
Andreas Gampe53e32d12015-12-09 21:03:23 -0800162 if (callbacks != nullptr) {
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800163 ClassReference ref(&dex_file, klass->GetDexClassDefIndex());
Andreas Gampe53e32d12015-12-09 21:03:23 -0800164 callbacks->ClassRejected(ref);
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800165 }
jeffhaof1e6b7c2012-06-05 18:33:30 -0700166 return kHardFailure;
jeffhaobdb76512011-09-07 11:43:16 -0700167 }
Ian Rogers7b078e82014-09-10 14:44:24 -0700168 StackHandleScope<2> hs(self);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700169 Handle<mirror::DexCache> dex_cache(hs.NewHandle(klass->GetDexCache()));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700170 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(klass->GetClassLoader()));
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800171 return VerifyClass(self,
172 &dex_file,
173 dex_cache,
174 class_loader,
David Brazdil15fc7292016-09-02 14:13:18 +0100175 *class_def,
Andreas Gampe53e32d12015-12-09 21:03:23 -0800176 callbacks,
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800177 allow_soft_failures,
Andreas Gampe7fe30232016-03-25 16:58:00 -0700178 log_level,
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800179 error);
180}
181
182template <bool kDirect>
183static bool HasNextMethod(ClassDataItemIterator* it) {
184 return kDirect ? it->HasNextDirectMethod() : it->HasNextVirtualMethod();
185}
186
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800187static MethodVerifier::FailureKind FailureKindMax(MethodVerifier::FailureKind fk1,
188 MethodVerifier::FailureKind fk2) {
189 static_assert(MethodVerifier::FailureKind::kNoFailure <
190 MethodVerifier::FailureKind::kSoftFailure
191 && MethodVerifier::FailureKind::kSoftFailure <
192 MethodVerifier::FailureKind::kHardFailure,
193 "Unexpected FailureKind order");
194 return std::max(fk1, fk2);
195}
196
197void MethodVerifier::FailureData::Merge(const MethodVerifier::FailureData& fd) {
198 kind = FailureKindMax(kind, fd.kind);
199 types |= fd.types;
200}
201
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800202template <bool kDirect>
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800203MethodVerifier::FailureData MethodVerifier::VerifyMethods(Thread* self,
204 ClassLinker* linker,
205 const DexFile* dex_file,
David Brazdil15fc7292016-09-02 14:13:18 +0100206 const DexFile::ClassDef& class_def,
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800207 ClassDataItemIterator* it,
208 Handle<mirror::DexCache> dex_cache,
209 Handle<mirror::ClassLoader> class_loader,
210 CompilerCallbacks* callbacks,
211 bool allow_soft_failures,
Andreas Gampe5fd66d02016-09-12 20:22:19 -0700212 HardFailLogMode log_level,
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800213 bool need_precise_constants,
214 std::string* error_string) {
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800215 DCHECK(it != nullptr);
216
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800217 MethodVerifier::FailureData failure_data;
218
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800219 int64_t previous_method_idx = -1;
220 while (HasNextMethod<kDirect>(it)) {
221 self->AllowThreadSuspension();
222 uint32_t method_idx = it->GetMemberIndex();
223 if (method_idx == previous_method_idx) {
224 // smali can create dex files with two encoded_methods sharing the same method_idx
225 // http://code.google.com/p/smali/issues/detail?id=119
226 it->Next();
227 continue;
228 }
229 previous_method_idx = method_idx;
David Brazdil15fc7292016-09-02 14:13:18 +0100230 InvokeType type = it->GetMethodInvokeType(class_def);
Andreas Gampe42ef8ab2015-12-03 17:27:32 -0800231 ArtMethod* method = linker->ResolveMethod<ClassLinker::kNoICCECheckForCache>(
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800232 *dex_file, method_idx, dex_cache, class_loader, nullptr, type);
233 if (method == nullptr) {
234 DCHECK(self->IsExceptionPending());
235 // We couldn't resolve the method, but continue regardless.
236 self->ClearException();
237 } else {
238 DCHECK(method->GetDeclaringClassUnchecked() != nullptr) << type;
239 }
240 StackHandleScope<1> hs(self);
241 std::string hard_failure_msg;
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800242 MethodVerifier::FailureData result = VerifyMethod(self,
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800243 method_idx,
244 dex_file,
245 dex_cache,
246 class_loader,
247 class_def,
248 it->GetMethodCodeItem(),
249 method,
250 it->GetMethodAccessFlags(),
Andreas Gampe53e32d12015-12-09 21:03:23 -0800251 callbacks,
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800252 allow_soft_failures,
Andreas Gampe7fe30232016-03-25 16:58:00 -0700253 log_level,
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800254 need_precise_constants,
255 &hard_failure_msg);
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800256 if (result.kind == kHardFailure) {
257 if (failure_data.kind == kHardFailure) {
258 // If we logged an error before, we need a newline.
259 *error_string += "\n";
260 } else {
261 // If we didn't log a hard failure before, print the header of the message.
262 *error_string += "Verifier rejected class ";
David Brazdil15fc7292016-09-02 14:13:18 +0100263 *error_string += PrettyDescriptor(dex_file->GetClassDescriptor(class_def));
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800264 *error_string += ":";
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800265 }
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800266 *error_string += " ";
267 *error_string += hard_failure_msg;
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800268 }
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800269 failure_data.Merge(result);
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800270 it->Next();
271 }
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800272
273 return failure_data;
Shih-wei Liao371814f2011-10-27 16:52:10 -0700274}
275
Ian Rogers7b078e82014-09-10 14:44:24 -0700276MethodVerifier::FailureKind MethodVerifier::VerifyClass(Thread* self,
277 const DexFile* dex_file,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700278 Handle<mirror::DexCache> dex_cache,
279 Handle<mirror::ClassLoader> class_loader,
David Brazdil15fc7292016-09-02 14:13:18 +0100280 const DexFile::ClassDef& class_def,
Andreas Gampe53e32d12015-12-09 21:03:23 -0800281 CompilerCallbacks* callbacks,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700282 bool allow_soft_failures,
Andreas Gampe5fd66d02016-09-12 20:22:19 -0700283 HardFailLogMode log_level,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700284 std::string* error) {
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -0800285 ScopedTrace trace(__FUNCTION__);
Andreas Gampe507cc6f2015-06-19 22:58:47 -0700286
287 // A class must not be abstract and final.
David Brazdil15fc7292016-09-02 14:13:18 +0100288 if ((class_def.access_flags_ & (kAccAbstract | kAccFinal)) == (kAccAbstract | kAccFinal)) {
Andreas Gampe507cc6f2015-06-19 22:58:47 -0700289 *error = "Verifier rejected class ";
David Brazdil15fc7292016-09-02 14:13:18 +0100290 *error += PrettyDescriptor(dex_file->GetClassDescriptor(class_def));
Andreas Gampe507cc6f2015-06-19 22:58:47 -0700291 *error += ": class is abstract and final.";
292 return kHardFailure;
293 }
294
David Brazdil15fc7292016-09-02 14:13:18 +0100295 const uint8_t* class_data = dex_file->GetClassData(class_def);
Ian Rogers7b078e82014-09-10 14:44:24 -0700296 if (class_data == nullptr) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700297 // empty class, probably a marker interface
jeffhaof1e6b7c2012-06-05 18:33:30 -0700298 return kNoFailure;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700299 }
jeffhaof56197c2012-03-05 18:01:54 -0800300 ClassDataItemIterator it(*dex_file, class_data);
301 while (it.HasNextStaticField() || it.HasNextInstanceField()) {
302 it.Next();
303 }
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
334 if (data1.kind == kNoFailure) {
jeffhaof1e6b7c2012-06-05 18:33:30 -0700335 return kNoFailure;
336 } 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 Gampe9fcfb8a2016-02-04 20:52:54 -0800411 result.kind = kSoftFailure;
Nicolas Geoffray250a3782016-04-20 16:27:53 +0100412 if (method != nullptr &&
413 !CanCompilerHandleVerificationFailure(verifier.encountered_failure_types_)) {
Mingyao Yang063fc772016-08-02 11:02:54 -0700414 method->AddAccessFlags(kAccCompileDontBother);
Nicolas Geoffray250a3782016-04-20 16:27:53 +0100415 }
416 }
Andreas Gampe56fdd0e2016-04-28 14:56:54 -0700417 if (method != nullptr) {
418 if (verifier.HasInstructionThatWillThrow()) {
Mingyao Yang063fc772016-08-02 11:02:54 -0700419 method->AddAccessFlags(kAccCompileDontBother);
Andreas Gampe56fdd0e2016-04-28 14:56:54 -0700420 }
421 if ((verifier.encountered_failure_types_ & VerifyError::VERIFY_ERROR_LOCKING) != 0) {
Mingyao Yang063fc772016-08-02 11:02:54 -0700422 method->AddAccessFlags(kAccMustCountLocks);
Andreas Gampe56fdd0e2016-04-28 14:56:54 -0700423 }
jeffhaof56197c2012-03-05 18:01:54 -0800424 }
425 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700426 // Bad method data.
Ian Rogers46960fe2014-05-23 10:43:43 -0700427 CHECK_NE(verifier.failures_.size(), 0U);
Igor Murashkin4d7b75f2015-07-21 17:03:36 -0700428
429 if (UNLIKELY(verifier.have_pending_experimental_failure_)) {
430 // Failed due to being forced into interpreter. This is ok because
431 // we just want to skip verification.
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800432 result.kind = kSoftFailure;
Igor Murashkin4d7b75f2015-07-21 17:03:36 -0700433 } else {
434 CHECK(verifier.have_pending_hard_failure_);
Andreas Gampe7fe30232016-03-25 16:58:00 -0700435 if (VLOG_IS_ON(verifier)) {
Andreas Gampe5fd66d02016-09-12 20:22:19 -0700436 log_level = std::max(HardFailLogMode::kLogVerbose, log_level);
Andreas Gampe7fe30232016-03-25 16:58:00 -0700437 }
Andreas Gampe5fd66d02016-09-12 20:22:19 -0700438 if (log_level >= HardFailLogMode::kLogVerbose) {
439 LogSeverity severity;
440 switch (log_level) {
441 case HardFailLogMode::kLogVerbose:
442 severity = LogSeverity::VERBOSE;
443 break;
444 case HardFailLogMode::kLogWarning:
445 severity = LogSeverity::WARNING;
446 break;
447 case HardFailLogMode::kLogInternalFatal:
Andreas Gampe3fec9ac2016-09-13 10:47:28 -0700448 severity = LogSeverity::FATAL_WITHOUT_ABORT;
Andreas Gampe5fd66d02016-09-12 20:22:19 -0700449 break;
450 default:
451 LOG(FATAL) << "Unsupported log-level " << static_cast<uint32_t>(log_level);
452 UNREACHABLE();
453 }
Andreas Gampe3fec9ac2016-09-13 10:47:28 -0700454 verifier.DumpFailures(LOG_STREAM(severity) << "Verification error in "
David Sehr709b0702016-10-13 09:12:37 -0700455 << dex_file->PrettyMethod(method_idx)
Andreas Gampe3fec9ac2016-09-13 10:47:28 -0700456 << "\n");
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800457 }
458 if (hard_failure_msg != nullptr) {
459 CHECK(!verifier.failure_messages_.empty());
460 *hard_failure_msg =
461 verifier.failure_messages_[verifier.failure_messages_.size() - 1]->str();
462 }
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800463 result.kind = kHardFailure;
Andreas Gampe53e32d12015-12-09 21:03:23 -0800464
465 if (callbacks != nullptr) {
466 // Let the interested party know that we failed the class.
David Brazdil15fc7292016-09-02 14:13:18 +0100467 ClassReference ref(dex_file, dex_file->GetIndexForClassDef(class_def));
Andreas Gampe53e32d12015-12-09 21:03:23 -0800468 callbacks->ClassRejected(ref);
469 }
Igor Murashkin4d7b75f2015-07-21 17:03:36 -0700470 }
Andreas Gampe53e32d12015-12-09 21:03:23 -0800471 if (VLOG_IS_ON(verifier)) {
Ian Rogers46960fe2014-05-23 10:43:43 -0700472 std::cout << "\n" << verifier.info_messages_.str();
473 verifier.Dump(std::cout);
jeffhaof56197c2012-03-05 18:01:54 -0800474 }
jeffhaof56197c2012-03-05 18:01:54 -0800475 }
Mathieu Chartier8e219ae2014-08-19 14:29:46 -0700476 if (kTimeVerifyMethod) {
477 uint64_t duration_ns = NanoTime() - start_ns;
478 if (duration_ns > MsToNs(100)) {
David Sehr709b0702016-10-13 09:12:37 -0700479 LOG(WARNING) << "Verification of " << dex_file->PrettyMethod(method_idx)
Andreas Gampea4f5bf62015-05-18 20:50:12 -0700480 << " took " << PrettyDuration(duration_ns)
481 << (IsLargeMethod(code_item) ? " (large method)" : "");
Mathieu Chartier8e219ae2014-08-19 14:29:46 -0700482 }
Ian Rogersc8982582012-09-07 16:53:25 -0700483 }
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800484 result.types = verifier.encountered_failure_types_;
Ian Rogersc8982582012-09-07 16:53:25 -0700485 return result;
jeffhaof56197c2012-03-05 18:01:54 -0800486}
487
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100488MethodVerifier* MethodVerifier::VerifyMethodAndDump(Thread* self,
489 VariableIndentationOutputStream* vios,
490 uint32_t dex_method_idx,
491 const DexFile* dex_file,
492 Handle<mirror::DexCache> dex_cache,
493 Handle<mirror::ClassLoader> class_loader,
David Brazdil15fc7292016-09-02 14:13:18 +0100494 const DexFile::ClassDef& class_def,
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100495 const DexFile::CodeItem* code_item,
496 ArtMethod* method,
497 uint32_t method_access_flags) {
Andreas Gampebf9611f2016-03-25 16:58:00 -0700498 MethodVerifier* verifier = new MethodVerifier(self,
499 dex_file,
500 dex_cache,
501 class_loader,
502 class_def,
503 code_item,
504 dex_method_idx,
505 method,
506 method_access_flags,
507 true /* can_load_classes */,
508 true /* allow_soft_failures */,
509 true /* need_precise_constants */,
510 true /* verify_to_dump */,
511 true /* allow_thread_suspension */);
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700512 verifier->Verify();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100513 verifier->DumpFailures(vios->Stream());
514 vios->Stream() << verifier->info_messages_.str();
Andreas Gampe5cbcde22014-09-16 14:59:49 -0700515 // Only dump and return if no hard failures. Otherwise the verifier may be not fully initialized
516 // and querying any info is dangerous/can abort.
517 if (verifier->have_pending_hard_failure_) {
518 delete verifier;
519 return nullptr;
520 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100521 verifier->Dump(vios);
Andreas Gampe5cbcde22014-09-16 14:59:49 -0700522 return verifier;
523 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800524}
525
Ian Rogers7b078e82014-09-10 14:44:24 -0700526MethodVerifier::MethodVerifier(Thread* self,
Andreas Gampe53e32d12015-12-09 21:03:23 -0800527 const DexFile* dex_file,
528 Handle<mirror::DexCache> dex_cache,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700529 Handle<mirror::ClassLoader> class_loader,
David Brazdil15fc7292016-09-02 14:13:18 +0100530 const DexFile::ClassDef& class_def,
Andreas Gampe53e32d12015-12-09 21:03:23 -0800531 const DexFile::CodeItem* code_item,
532 uint32_t dex_method_idx,
533 ArtMethod* method,
534 uint32_t method_access_flags,
535 bool can_load_classes,
536 bool allow_soft_failures,
537 bool need_precise_constants,
538 bool verify_to_dump,
Mathieu Chartier4306ef82014-12-19 18:41:47 -0800539 bool allow_thread_suspension)
Ian Rogers7b078e82014-09-10 14:44:24 -0700540 : self_(self),
Mathieu Chartierde40d472015-10-15 17:47:48 -0700541 arena_stack_(Runtime::Current()->GetArenaPool()),
542 arena_(&arena_stack_),
543 reg_types_(can_load_classes, arena_),
544 reg_table_(arena_),
Andreas Gamped5ad72f2015-06-26 17:33:47 -0700545 work_insn_idx_(DexFile::kDexNoIndex),
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800546 dex_method_idx_(dex_method_idx),
Ian Rogers637c65b2013-05-31 11:46:00 -0700547 mirror_method_(method),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700548 method_access_flags_(method_access_flags),
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700549 return_type_(nullptr),
jeffhaof56197c2012-03-05 18:01:54 -0800550 dex_file_(dex_file),
551 dex_cache_(dex_cache),
552 class_loader_(class_loader),
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700553 class_def_(class_def),
jeffhaof56197c2012-03-05 18:01:54 -0800554 code_item_(code_item),
Ian Rogers7b078e82014-09-10 14:44:24 -0700555 declaring_class_(nullptr),
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700556 interesting_dex_pc_(-1),
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700557 monitor_enter_dex_pcs_(nullptr),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700558 have_pending_hard_failure_(false),
jeffhaofaf459e2012-08-31 15:32:47 -0700559 have_pending_runtime_throw_failure_(false),
Igor Murashkin4d7b75f2015-07-21 17:03:36 -0700560 have_pending_experimental_failure_(false),
Andreas Gamped12e7822015-06-25 10:26:40 -0700561 have_any_pending_runtime_throw_failure_(false),
jeffhaof56197c2012-03-05 18:01:54 -0800562 new_instance_count_(0),
Elliott Hughes80537bb2013-01-04 16:37:26 -0800563 monitor_enter_count_(0),
Andreas Gampe0760a812015-08-26 17:12:51 -0700564 encountered_failure_types_(0),
Jeff Haoee988952013-04-16 14:23:47 -0700565 can_load_classes_(can_load_classes),
Sebastien Hertz4d4adb12013-07-24 16:14:19 +0200566 allow_soft_failures_(allow_soft_failures),
Ian Rogers46960fe2014-05-23 10:43:43 -0700567 need_precise_constants_(need_precise_constants),
Sebastien Hertz4d4adb12013-07-24 16:14:19 +0200568 has_check_casts_(false),
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700569 has_virtual_or_interface_invokes_(false),
Mathieu Chartier4306ef82014-12-19 18:41:47 -0800570 verify_to_dump_(verify_to_dump),
Andreas Gamped5ad72f2015-06-26 17:33:47 -0700571 allow_thread_suspension_(allow_thread_suspension),
Andreas Gampee6215c02015-08-31 18:54:38 -0700572 is_constructor_(false),
Andreas Gamped5ad72f2015-06-26 17:33:47 -0700573 link_(nullptr) {
Mathieu Chartierd0ad2ee2015-03-31 14:59:59 -0700574 self->PushVerifier(this);
jeffhaof56197c2012-03-05 18:01:54 -0800575}
576
Mathieu Chartier590fee92013-09-13 13:46:47 -0700577MethodVerifier::~MethodVerifier() {
Mathieu Chartierd0ad2ee2015-03-31 14:59:59 -0700578 Thread::Current()->PopVerifier(this);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700579 STLDeleteElements(&failure_messages_);
580}
581
Mathieu Chartiere401d142015-04-22 13:56:20 -0700582void MethodVerifier::FindLocksAtDexPc(ArtMethod* m, uint32_t dex_pc,
Ian Rogers46960fe2014-05-23 10:43:43 -0700583 std::vector<uint32_t>* monitor_enter_dex_pcs) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700584 StackHandleScope<2> hs(Thread::Current());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700585 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache()));
586 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader()));
Andreas Gampebf9611f2016-03-25 16:58:00 -0700587 MethodVerifier verifier(hs.Self(),
588 m->GetDexFile(),
589 dex_cache,
590 class_loader,
David Brazdil15fc7292016-09-02 14:13:18 +0100591 m->GetClassDef(),
Andreas Gampebf9611f2016-03-25 16:58:00 -0700592 m->GetCodeItem(),
593 m->GetDexMethodIndex(),
594 m,
595 m->GetAccessFlags(),
596 false /* can_load_classes */,
597 true /* allow_soft_failures */,
598 false /* need_precise_constants */,
599 false /* verify_to_dump */,
600 false /* allow_thread_suspension */);
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700601 verifier.interesting_dex_pc_ = dex_pc;
Ian Rogers46960fe2014-05-23 10:43:43 -0700602 verifier.monitor_enter_dex_pcs_ = monitor_enter_dex_pcs;
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700603 verifier.FindLocksAtDexPc();
604}
605
Andreas Gampecb3c08f2014-09-18 13:16:38 -0700606static bool HasMonitorEnterInstructions(const DexFile::CodeItem* const code_item) {
607 const Instruction* inst = Instruction::At(code_item->insns_);
608
609 uint32_t insns_size = code_item->insns_size_in_code_units_;
610 for (uint32_t dex_pc = 0; dex_pc < insns_size;) {
611 if (inst->Opcode() == Instruction::MONITOR_ENTER) {
612 return true;
613 }
614
615 dex_pc += inst->SizeInCodeUnits();
616 inst = inst->Next();
617 }
618
619 return false;
620}
621
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700622void MethodVerifier::FindLocksAtDexPc() {
Ian Rogers7b078e82014-09-10 14:44:24 -0700623 CHECK(monitor_enter_dex_pcs_ != nullptr);
624 CHECK(code_item_ != nullptr); // This only makes sense for methods with code.
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700625
Andreas Gampecb3c08f2014-09-18 13:16:38 -0700626 // Quick check whether there are any monitor_enter instructions at all.
627 if (!HasMonitorEnterInstructions(code_item_)) {
628 return;
629 }
630
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700631 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
632 // verification. In practice, the phase we want relies on data structures set up by all the
633 // earlier passes, so we just run the full method verification and bail out early when we've
634 // got what we wanted.
635 Verify();
636}
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) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700660 CHECK(code_item_ != nullptr); // 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 }
674 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
675 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) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700700 CHECK(code_item_ != nullptr); // 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 }
714 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
715 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.
766 if (code_item_ == nullptr) {
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.
858 if (code_item_->ins_size_ > code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700859 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad register counts (ins=" << code_item_->ins_size_
860 << " regs=" << code_item_->registers_size_;
Ian Rogersd81871c2011-10-03 13:57:23 -0700861 return false;
jeffhaobdb76512011-09-07 11:43:16 -0700862 }
Alex Lighteb7c1442015-08-31 13:17:42 -0700863
Ian Rogersd81871c2011-10-03 13:57:23 -0700864 // Allocate and initialize an array to hold instruction data.
Mathieu Chartierde40d472015-10-15 17:47:48 -0700865 insn_flags_.reset(arena_.AllocArray<InstructionFlags>(code_item_->insns_size_in_code_units_));
866 DCHECK(insn_flags_ != nullptr);
867 std::uninitialized_fill_n(insn_flags_.get(),
868 code_item_->insns_size_in_code_units_,
869 InstructionFlags());
Ian Rogersd81871c2011-10-03 13:57:23 -0700870 // Run through the instructions and see if the width checks out.
871 bool result = ComputeWidthsAndCountOps();
872 // Flag instructions guarded by a "try" block and check exception handlers.
873 result = result && ScanTryCatchBlocks();
874 // Perform static instruction verification.
875 result = result && VerifyInstructions();
Ian Rogersad0b3a32012-04-16 14:50:24 -0700876 // Perform code-flow analysis and return.
Vladimir Marko2b5eaa22013-12-13 13:59:30 +0000877 result = result && VerifyCodeFlow();
Andreas Gampe53e32d12015-12-09 21:03:23 -0800878
Vladimir Marko2b5eaa22013-12-13 13:59:30 +0000879 return result;
jeffhaoba5ebb92011-08-25 17:24:37 -0700880}
881
Ian Rogers776ac1f2012-04-13 23:36:36 -0700882std::ostream& MethodVerifier::Fail(VerifyError error) {
Andreas Gampe0760a812015-08-26 17:12:51 -0700883 // Mark the error type as encountered.
Andreas Gampea727e372015-08-25 09:22:37 -0700884 encountered_failure_types_ |= static_cast<uint32_t>(error);
Andreas Gampe0760a812015-08-26 17:12:51 -0700885
Ian Rogersad0b3a32012-04-16 14:50:24 -0700886 switch (error) {
887 case VERIFY_ERROR_NO_CLASS:
888 case VERIFY_ERROR_NO_FIELD:
889 case VERIFY_ERROR_NO_METHOD:
890 case VERIFY_ERROR_ACCESS_CLASS:
891 case VERIFY_ERROR_ACCESS_FIELD:
892 case VERIFY_ERROR_ACCESS_METHOD:
Ian Rogers08f753d2012-08-24 14:35:25 -0700893 case VERIFY_ERROR_INSTANTIATION:
894 case VERIFY_ERROR_CLASS_CHANGE:
Igor Murashkin158f35c2015-06-10 15:55:30 -0700895 case VERIFY_ERROR_FORCE_INTERPRETER:
Andreas Gampea727e372015-08-25 09:22:37 -0700896 case VERIFY_ERROR_LOCKING:
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800897 if (Runtime::Current()->IsAotCompiler() || !can_load_classes_) {
jeffhaofaf459e2012-08-31 15:32:47 -0700898 // If we're optimistically running verification at compile time, turn NO_xxx, ACCESS_xxx,
899 // class change and instantiation errors into soft verification errors so that we re-verify
900 // at runtime. We may fail to find or to agree on access because of not yet available class
901 // loaders, or class loaders that will differ at runtime. In these cases, we don't want to
902 // affect the soundness of the code being compiled. Instead, the generated code runs "slow
903 // paths" that dynamically perform the verification and cause the behavior to be that akin
904 // to an interpreter.
905 error = VERIFY_ERROR_BAD_CLASS_SOFT;
906 } else {
Jeff Haoa3faaf42013-09-03 19:07:00 -0700907 // If we fail again at runtime, mark that this instruction would throw and force this
908 // method to be executed using the interpreter with checks.
jeffhaofaf459e2012-08-31 15:32:47 -0700909 have_pending_runtime_throw_failure_ = true;
Andreas Gamped7f8d052015-03-12 11:05:47 -0700910
911 // We need to save the work_line if the instruction wasn't throwing before. Otherwise we'll
912 // try to merge garbage.
913 // Note: this assumes that Fail is called before we do any work_line modifications.
Andreas Gamped5ad72f2015-06-26 17:33:47 -0700914 // Note: this can fail before we touch any instruction, for the signature of a method. So
915 // add a check.
916 if (work_insn_idx_ < DexFile::kDexNoIndex) {
917 const uint16_t* insns = code_item_->insns_ + work_insn_idx_;
918 const Instruction* inst = Instruction::At(insns);
919 int opcode_flags = Instruction::FlagsOf(inst->Opcode());
Andreas Gamped7f8d052015-03-12 11:05:47 -0700920
Andreas Gamped5ad72f2015-06-26 17:33:47 -0700921 if ((opcode_flags & Instruction::kThrow) == 0 && CurrentInsnFlags()->IsInTry()) {
922 saved_line_->CopyFromLine(work_line_.get());
923 }
Andreas Gamped7f8d052015-03-12 11:05:47 -0700924 }
jeffhaofaf459e2012-08-31 15:32:47 -0700925 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700926 break;
Andreas Gampea727e372015-08-25 09:22:37 -0700927
Ian Rogersad0b3a32012-04-16 14:50:24 -0700928 // Indication that verification should be retried at runtime.
929 case VERIFY_ERROR_BAD_CLASS_SOFT:
Jeff Haoee988952013-04-16 14:23:47 -0700930 if (!allow_soft_failures_) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700931 have_pending_hard_failure_ = true;
932 }
933 break;
Andreas Gampea727e372015-08-25 09:22:37 -0700934
jeffhaod5347e02012-03-22 17:25:05 -0700935 // Hard verification failures at compile time will still fail at runtime, so the class is
936 // marked as rejected to prevent it from being compiled.
Ian Rogersad0b3a32012-04-16 14:50:24 -0700937 case VERIFY_ERROR_BAD_CLASS_HARD: {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700938 have_pending_hard_failure_ = true;
Andreas Gampeebf850c2015-08-14 15:37:35 -0700939 if (VLOG_IS_ON(verifier) && kDumpRegLinesOnHardFailureIfVLOG) {
940 ScopedObjectAccess soa(Thread::Current());
941 std::ostringstream oss;
942 Dump(oss);
943 LOG(ERROR) << oss.str();
944 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700945 break;
Ian Rogers47a05882012-02-03 12:23:33 -0800946 }
947 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700948 failures_.push_back(error);
David Sehr709b0702016-10-13 09:12:37 -0700949 std::string location(StringPrintf("%s: [0x%X] ", dex_file_->PrettyMethod(dex_method_idx_).c_str(),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700950 work_insn_idx_));
Elena Sayapina78480ec2014-08-15 15:52:42 +0700951 std::ostringstream* failure_message = new std::ostringstream(location, std::ostringstream::ate);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700952 failure_messages_.push_back(failure_message);
953 return *failure_message;
954}
955
Ian Rogers576ca0c2014-06-06 15:58:22 -0700956std::ostream& MethodVerifier::LogVerifyInfo() {
David Sehr709b0702016-10-13 09:12:37 -0700957 return info_messages_ << "VFY: " << dex_file_->PrettyMethod(dex_method_idx_)
Ian Rogers576ca0c2014-06-06 15:58:22 -0700958 << '[' << reinterpret_cast<void*>(work_insn_idx_) << "] : ";
959}
960
Ian Rogersad0b3a32012-04-16 14:50:24 -0700961void MethodVerifier::PrependToLastFailMessage(std::string prepend) {
962 size_t failure_num = failure_messages_.size();
963 DCHECK_NE(failure_num, 0U);
964 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
965 prepend += last_fail_message->str();
Elena Sayapina78480ec2014-08-15 15:52:42 +0700966 failure_messages_[failure_num - 1] = new std::ostringstream(prepend, std::ostringstream::ate);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700967 delete last_fail_message;
968}
969
Vladimir Marko5c657fe2016-11-03 15:12:29 +0000970void MethodVerifier::AppendToLastFailMessage(const std::string& append) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700971 size_t failure_num = failure_messages_.size();
972 DCHECK_NE(failure_num, 0U);
973 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
974 (*last_fail_message) << append;
Ian Rogers47a05882012-02-03 12:23:33 -0800975}
976
Ian Rogers776ac1f2012-04-13 23:36:36 -0700977bool MethodVerifier::ComputeWidthsAndCountOps() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700978 const uint16_t* insns = code_item_->insns_;
979 size_t insns_size = code_item_->insns_size_in_code_units_;
980 const Instruction* inst = Instruction::At(insns);
jeffhaobdb76512011-09-07 11:43:16 -0700981 size_t new_instance_count = 0;
982 size_t monitor_enter_count = 0;
Ian Rogersd81871c2011-10-03 13:57:23 -0700983 size_t dex_pc = 0;
jeffhaobdb76512011-09-07 11:43:16 -0700984
Ian Rogersd81871c2011-10-03 13:57:23 -0700985 while (dex_pc < insns_size) {
jeffhaobdb76512011-09-07 11:43:16 -0700986 Instruction::Code opcode = inst->Opcode();
Ian Rogersa9a82542013-10-04 11:17:26 -0700987 switch (opcode) {
988 case Instruction::APUT_OBJECT:
989 case Instruction::CHECK_CAST:
990 has_check_casts_ = true;
991 break;
992 case Instruction::INVOKE_VIRTUAL:
993 case Instruction::INVOKE_VIRTUAL_RANGE:
994 case Instruction::INVOKE_INTERFACE:
995 case Instruction::INVOKE_INTERFACE_RANGE:
996 has_virtual_or_interface_invokes_ = true;
997 break;
998 case Instruction::MONITOR_ENTER:
999 monitor_enter_count++;
1000 break;
1001 case Instruction::NEW_INSTANCE:
1002 new_instance_count++;
1003 break;
1004 default:
1005 break;
jeffhaobdb76512011-09-07 11:43:16 -07001006 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001007 size_t inst_size = inst->SizeInCodeUnits();
Mathieu Chartierde40d472015-10-15 17:47:48 -07001008 GetInstructionFlags(dex_pc).SetIsOpcode();
Ian Rogersd81871c2011-10-03 13:57:23 -07001009 dex_pc += inst_size;
Ian Rogers7b078e82014-09-10 14:44:24 -07001010 inst = inst->RelativeAt(inst_size);
jeffhaobdb76512011-09-07 11:43:16 -07001011 }
1012
Ian Rogersd81871c2011-10-03 13:57:23 -07001013 if (dex_pc != insns_size) {
jeffhaod5347e02012-03-22 17:25:05 -07001014 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "code did not end where expected ("
1015 << dex_pc << " vs. " << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -07001016 return false;
1017 }
1018
Ian Rogersd81871c2011-10-03 13:57:23 -07001019 new_instance_count_ = new_instance_count;
1020 monitor_enter_count_ = monitor_enter_count;
jeffhaobdb76512011-09-07 11:43:16 -07001021 return true;
1022}
1023
Ian Rogers776ac1f2012-04-13 23:36:36 -07001024bool MethodVerifier::ScanTryCatchBlocks() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001025 uint32_t tries_size = code_item_->tries_size_;
jeffhaobdb76512011-09-07 11:43:16 -07001026 if (tries_size == 0) {
1027 return true;
1028 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001029 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Ian Rogers0571d352011-11-03 19:51:38 -07001030 const DexFile::TryItem* tries = DexFile::GetTryItems(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -07001031
1032 for (uint32_t idx = 0; idx < tries_size; idx++) {
1033 const DexFile::TryItem* try_item = &tries[idx];
1034 uint32_t start = try_item->start_addr_;
1035 uint32_t end = start + try_item->insn_count_;
jeffhaobdb76512011-09-07 11:43:16 -07001036 if ((start >= end) || (start >= insns_size) || (end > insns_size)) {
jeffhaod5347e02012-03-22 17:25:05 -07001037 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad exception entry: startAddr=" << start
1038 << " endAddr=" << end << " (size=" << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -07001039 return false;
1040 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07001041 if (!GetInstructionFlags(start).IsOpcode()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001042 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1043 << "'try' block starts inside an instruction (" << start << ")";
jeffhaobdb76512011-09-07 11:43:16 -07001044 return false;
1045 }
Ian Rogers7b078e82014-09-10 14:44:24 -07001046 uint32_t dex_pc = start;
1047 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
1048 while (dex_pc < end) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07001049 GetInstructionFlags(dex_pc).SetInTry();
Ian Rogers7b078e82014-09-10 14:44:24 -07001050 size_t insn_size = inst->SizeInCodeUnits();
1051 dex_pc += insn_size;
1052 inst = inst->RelativeAt(insn_size);
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.
Ian Rogers13735952014-10-08 12:43:28 -07001056 const uint8_t* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -07001057 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()) {
1062 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 }
Stephen Kyle9bc61992014-09-22 13:53:15 +01001068 if (!CheckNotMoveResult(code_item_->insns_, dex_pc)) {
1069 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
Ian Rogers776ac1f2012-04-13 23:36:36 -07001091bool MethodVerifier::VerifyInstructions() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001092 const Instruction* inst = Instruction::At(code_item_->insns_);
jeffhaoba5ebb92011-08-25 17:24:37 -07001093
Ian Rogers0c7abda2012-09-19 13:33:42 -07001094 /* 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 -07001095 GetInstructionFlags(0).SetBranchTarget();
1096 GetInstructionFlags(0).SetCompileTimeInfoPoint();
Ian Rogersd81871c2011-10-03 13:57:23 -07001097
1098 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Elliott Hughesb25c3f62012-03-26 16:35:06 -07001099 for (uint32_t dex_pc = 0; dex_pc < insns_size;) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001100 if (!VerifyInstruction(inst, dex_pc)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001101 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -07001102 return false;
1103 }
1104 /* Flag instructions that are garbage collection points */
Sameer Abu Asal02c42232013-04-30 12:09:45 -07001105 // All invoke points are marked as "Throw" points already.
1106 // We are relying on this to also count all the invokes as interesting.
Vladimir Marko8b858e12014-11-27 14:52:37 +00001107 if (inst->IsBranch()) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07001108 GetInstructionFlags(dex_pc).SetCompileTimeInfoPoint();
Vladimir Marko8b858e12014-11-27 14:52:37 +00001109 // The compiler also needs safepoints for fall-through to loop heads.
1110 // Such a loop head must be a target of a branch.
1111 int32_t offset = 0;
1112 bool cond, self_ok;
1113 bool target_ok = GetBranchOffset(dex_pc, &offset, &cond, &self_ok);
1114 DCHECK(target_ok);
Mathieu Chartierde40d472015-10-15 17:47:48 -07001115 GetInstructionFlags(dex_pc + offset).SetCompileTimeInfoPoint();
Vladimir Marko8b858e12014-11-27 14:52:37 +00001116 } else if (inst->IsSwitch() || inst->IsThrow()) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07001117 GetInstructionFlags(dex_pc).SetCompileTimeInfoPoint();
Ian Rogersb8c78592013-07-25 23:52:52 +00001118 } else if (inst->IsReturn()) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07001119 GetInstructionFlags(dex_pc).SetCompileTimeInfoPointAndReturn();
Ian Rogersd81871c2011-10-03 13:57:23 -07001120 }
1121 dex_pc += inst->SizeInCodeUnits();
1122 inst = inst->Next();
1123 }
1124 return true;
1125}
1126
Ian Rogers776ac1f2012-04-13 23:36:36 -07001127bool MethodVerifier::VerifyInstruction(const Instruction* inst, uint32_t code_offset) {
Igor Murashkin4d7b75f2015-07-21 17:03:36 -07001128 if (UNLIKELY(inst->IsExperimental())) {
1129 // Experimental instructions don't yet have verifier support implementation.
1130 // While it is possible to use them by themselves, when we try to use stable instructions
1131 // with a virtual register that was created by an experimental instruction,
1132 // the data flow analysis will fail.
1133 Fail(VERIFY_ERROR_FORCE_INTERPRETER)
1134 << "experimental instruction is not supported by verifier; skipping verification";
1135 have_pending_experimental_failure_ = true;
1136 return false;
1137 }
1138
Ian Rogersd81871c2011-10-03 13:57:23 -07001139 bool result = true;
1140 switch (inst->GetVerifyTypeArgumentA()) {
1141 case Instruction::kVerifyRegA:
Ian Rogers29a26482014-05-02 15:27:29 -07001142 result = result && CheckRegisterIndex(inst->VRegA());
Ian Rogersd81871c2011-10-03 13:57:23 -07001143 break;
1144 case Instruction::kVerifyRegAWide:
Ian Rogers29a26482014-05-02 15:27:29 -07001145 result = result && CheckWideRegisterIndex(inst->VRegA());
Ian Rogersd81871c2011-10-03 13:57:23 -07001146 break;
1147 }
1148 switch (inst->GetVerifyTypeArgumentB()) {
1149 case Instruction::kVerifyRegB:
Ian Rogers29a26482014-05-02 15:27:29 -07001150 result = result && CheckRegisterIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -07001151 break;
1152 case Instruction::kVerifyRegBField:
Ian Rogers29a26482014-05-02 15:27:29 -07001153 result = result && CheckFieldIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -07001154 break;
1155 case Instruction::kVerifyRegBMethod:
Ian Rogers29a26482014-05-02 15:27:29 -07001156 result = result && CheckMethodIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -07001157 break;
1158 case Instruction::kVerifyRegBNewInstance:
Andreas Gampea5b09a62016-11-17 15:21:22 -08001159 result = result && CheckNewInstance(dex::TypeIndex(inst->VRegB()));
Ian Rogersd81871c2011-10-03 13:57:23 -07001160 break;
1161 case Instruction::kVerifyRegBString:
Ian Rogers29a26482014-05-02 15:27:29 -07001162 result = result && CheckStringIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -07001163 break;
1164 case Instruction::kVerifyRegBType:
Andreas Gampea5b09a62016-11-17 15:21:22 -08001165 result = result && CheckTypeIndex(dex::TypeIndex(inst->VRegB()));
Ian Rogersd81871c2011-10-03 13:57:23 -07001166 break;
1167 case Instruction::kVerifyRegBWide:
Ian Rogers29a26482014-05-02 15:27:29 -07001168 result = result && CheckWideRegisterIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -07001169 break;
1170 }
1171 switch (inst->GetVerifyTypeArgumentC()) {
1172 case Instruction::kVerifyRegC:
Ian Rogers29a26482014-05-02 15:27:29 -07001173 result = result && CheckRegisterIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -07001174 break;
1175 case Instruction::kVerifyRegCField:
Ian Rogers29a26482014-05-02 15:27:29 -07001176 result = result && CheckFieldIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -07001177 break;
1178 case Instruction::kVerifyRegCNewArray:
Andreas Gampea5b09a62016-11-17 15:21:22 -08001179 result = result && CheckNewArray(dex::TypeIndex(inst->VRegC()));
Ian Rogersd81871c2011-10-03 13:57:23 -07001180 break;
1181 case Instruction::kVerifyRegCType:
Andreas Gampea5b09a62016-11-17 15:21:22 -08001182 result = result && CheckTypeIndex(dex::TypeIndex(inst->VRegC()));
Ian Rogersd81871c2011-10-03 13:57:23 -07001183 break;
1184 case Instruction::kVerifyRegCWide:
Ian Rogers29a26482014-05-02 15:27:29 -07001185 result = result && CheckWideRegisterIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -07001186 break;
1187 }
Orion Hodsoncfa325e2016-10-13 10:25:54 +01001188 switch (inst->GetVerifyTypeArgumentH()) {
1189 case Instruction::kVerifyRegHPrototype:
1190 result = result && CheckPrototypeIndex(inst->VRegH());
1191 break;
1192 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001193 switch (inst->GetVerifyExtraFlags()) {
1194 case Instruction::kVerifyArrayData:
1195 result = result && CheckArrayData(code_offset);
1196 break;
1197 case Instruction::kVerifyBranchTarget:
1198 result = result && CheckBranchTarget(code_offset);
1199 break;
1200 case Instruction::kVerifySwitchTargets:
1201 result = result && CheckSwitchTargets(code_offset);
1202 break;
Andreas Gampec3314312014-06-19 18:13:29 -07001203 case Instruction::kVerifyVarArgNonZero:
1204 // Fall-through.
Ian Rogers29a26482014-05-02 15:27:29 -07001205 case Instruction::kVerifyVarArg: {
Taiju Tsuiki29498a22015-04-13 14:21:00 +09001206 // Instructions that can actually return a negative value shouldn't have this flag.
1207 uint32_t v_a = dchecked_integral_cast<uint32_t>(inst->VRegA());
1208 if ((inst->GetVerifyExtraFlags() == Instruction::kVerifyVarArgNonZero && v_a == 0) ||
1209 v_a > Instruction::kMaxVarArgRegs) {
1210 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << v_a << ") in "
Andreas Gampec3314312014-06-19 18:13:29 -07001211 "non-range invoke";
1212 return false;
1213 }
Taiju Tsuiki29498a22015-04-13 14:21:00 +09001214
Ian Rogers29a26482014-05-02 15:27:29 -07001215 uint32_t args[Instruction::kMaxVarArgRegs];
1216 inst->GetVarArgs(args);
Taiju Tsuiki29498a22015-04-13 14:21:00 +09001217 result = result && CheckVarArgRegs(v_a, args);
Ian Rogersd81871c2011-10-03 13:57:23 -07001218 break;
Ian Rogers29a26482014-05-02 15:27:29 -07001219 }
Andreas Gampec3314312014-06-19 18:13:29 -07001220 case Instruction::kVerifyVarArgRangeNonZero:
1221 // Fall-through.
Ian Rogersd81871c2011-10-03 13:57:23 -07001222 case Instruction::kVerifyVarArgRange:
Andreas Gampec3314312014-06-19 18:13:29 -07001223 if (inst->GetVerifyExtraFlags() == Instruction::kVerifyVarArgRangeNonZero &&
1224 inst->VRegA() <= 0) {
1225 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << inst->VRegA() << ") in "
1226 "range invoke";
1227 return false;
1228 }
Ian Rogers29a26482014-05-02 15:27:29 -07001229 result = result && CheckVarArgRangeRegs(inst->VRegA(), inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -07001230 break;
1231 case Instruction::kVerifyError:
jeffhaod5347e02012-03-22 17:25:05 -07001232 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected opcode " << inst->Name();
Ian Rogersd81871c2011-10-03 13:57:23 -07001233 result = false;
1234 break;
1235 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08001236 if (inst->GetVerifyIsRuntimeOnly() && Runtime::Current()->IsAotCompiler() && !verify_to_dump_) {
Ian Rogers5fb22a92014-06-13 10:31:28 -07001237 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "opcode only expected at runtime " << inst->Name();
1238 result = false;
1239 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001240 return result;
1241}
1242
Ian Rogers7b078e82014-09-10 14:44:24 -07001243inline bool MethodVerifier::CheckRegisterIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001244 if (idx >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07001245 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register index out of range (" << idx << " >= "
1246 << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001247 return false;
1248 }
1249 return true;
1250}
1251
Ian Rogers7b078e82014-09-10 14:44:24 -07001252inline bool MethodVerifier::CheckWideRegisterIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001253 if (idx + 1 >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07001254 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "wide register index out of range (" << idx
1255 << "+1 >= " << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001256 return false;
1257 }
1258 return true;
1259}
1260
Ian Rogers7b078e82014-09-10 14:44:24 -07001261inline bool MethodVerifier::CheckFieldIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001262 if (idx >= dex_file_->GetHeader().field_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07001263 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad field index " << idx << " (max "
1264 << dex_file_->GetHeader().field_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001265 return false;
1266 }
1267 return true;
1268}
1269
Ian Rogers7b078e82014-09-10 14:44:24 -07001270inline bool MethodVerifier::CheckMethodIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001271 if (idx >= dex_file_->GetHeader().method_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07001272 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad method index " << idx << " (max "
1273 << dex_file_->GetHeader().method_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001274 return false;
1275 }
1276 return true;
1277}
1278
Andreas Gampea5b09a62016-11-17 15:21:22 -08001279inline bool MethodVerifier::CheckNewInstance(dex::TypeIndex idx) {
1280 if (idx.index_ >= dex_file_->GetHeader().type_ids_size_) {
1281 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx.index_ << " (max "
jeffhaod5347e02012-03-22 17:25:05 -07001282 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001283 return false;
1284 }
1285 // We don't need the actual class, just a pointer to the class name.
Ian Rogers0571d352011-11-03 19:51:38 -07001286 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -07001287 if (descriptor[0] != 'L') {
jeffhaod5347e02012-03-22 17:25:05 -07001288 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "can't call new-instance on type '" << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001289 return false;
Aart Bikdb698f12016-07-25 17:52:22 -07001290 } else if (strcmp(descriptor, "Ljava/lang/Class;") == 0) {
1291 // An unlikely new instance on Class is not allowed. Fall back to interpreter to ensure an
1292 // exception is thrown when this statement is executed (compiled code would not do that).
1293 Fail(VERIFY_ERROR_INSTANTIATION);
Ian Rogersd81871c2011-10-03 13:57:23 -07001294 }
1295 return true;
1296}
1297
Orion Hodsoncfa325e2016-10-13 10:25:54 +01001298inline bool MethodVerifier::CheckPrototypeIndex(uint32_t idx) {
1299 if (idx >= dex_file_->GetHeader().proto_ids_size_) {
1300 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad prototype index " << idx << " (max "
1301 << dex_file_->GetHeader().proto_ids_size_ << ")";
1302 return false;
1303 }
1304 return true;
1305}
1306
Ian Rogers7b078e82014-09-10 14:44:24 -07001307inline bool MethodVerifier::CheckStringIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001308 if (idx >= dex_file_->GetHeader().string_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07001309 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad string index " << idx << " (max "
1310 << dex_file_->GetHeader().string_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001311 return false;
1312 }
1313 return true;
1314}
1315
Andreas Gampea5b09a62016-11-17 15:21:22 -08001316inline bool MethodVerifier::CheckTypeIndex(dex::TypeIndex idx) {
1317 if (idx.index_ >= dex_file_->GetHeader().type_ids_size_) {
1318 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx.index_ << " (max "
jeffhaod5347e02012-03-22 17:25:05 -07001319 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001320 return false;
1321 }
1322 return true;
1323}
1324
Andreas Gampea5b09a62016-11-17 15:21:22 -08001325bool MethodVerifier::CheckNewArray(dex::TypeIndex idx) {
1326 if (idx.index_ >= dex_file_->GetHeader().type_ids_size_) {
1327 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx.index_ << " (max "
jeffhaod5347e02012-03-22 17:25:05 -07001328 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001329 return false;
1330 }
1331 int bracket_count = 0;
Ian Rogers0571d352011-11-03 19:51:38 -07001332 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -07001333 const char* cp = descriptor;
1334 while (*cp++ == '[') {
1335 bracket_count++;
1336 }
1337 if (bracket_count == 0) {
1338 /* The given class must be an array type. */
Brian Carlstrom93c33962013-07-26 10:37:43 -07001339 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1340 << "can't new-array class '" << descriptor << "' (not an array)";
Ian Rogersd81871c2011-10-03 13:57:23 -07001341 return false;
1342 } else if (bracket_count > 255) {
1343 /* It is illegal to create an array of more than 255 dimensions. */
Brian Carlstrom93c33962013-07-26 10:37:43 -07001344 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1345 << "can't new-array class '" << descriptor << "' (exceeds limit)";
Ian Rogersd81871c2011-10-03 13:57:23 -07001346 return false;
1347 }
1348 return true;
1349}
1350
Ian Rogers776ac1f2012-04-13 23:36:36 -07001351bool MethodVerifier::CheckArrayData(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001352 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
1353 const uint16_t* insns = code_item_->insns_ + cur_offset;
1354 const uint16_t* array_data;
1355 int32_t array_data_offset;
1356
1357 DCHECK_LT(cur_offset, insn_count);
1358 /* make sure the start of the array data table is in range */
Andreas Gampe53de99c2015-08-17 13:43:55 -07001359 array_data_offset = insns[1] | (static_cast<int32_t>(insns[2]) << 16);
1360 if (static_cast<int32_t>(cur_offset) + array_data_offset < 0 ||
Ian Rogersd81871c2011-10-03 13:57:23 -07001361 cur_offset + array_data_offset + 2 >= insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -07001362 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data start: at " << cur_offset
Brian Carlstrom93c33962013-07-26 10:37:43 -07001363 << ", data offset " << array_data_offset
1364 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -07001365 return false;
1366 }
1367 /* offset to array data table is a relative branch-style offset */
1368 array_data = insns + array_data_offset;
Andreas Gampe57c47582015-07-01 22:05:59 -07001369 // Make sure the table is at an even dex pc, that is, 32-bit aligned.
1370 if (!IsAligned<4>(array_data)) {
jeffhaod5347e02012-03-22 17:25:05 -07001371 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned array data table: at " << cur_offset
1372 << ", data offset " << array_data_offset;
Ian Rogersd81871c2011-10-03 13:57:23 -07001373 return false;
1374 }
Andreas Gampe57c47582015-07-01 22:05:59 -07001375 // Make sure the array-data is marked as an opcode. This ensures that it was reached when
1376 // traversing the code item linearly. It is an approximation for a by-spec padding value.
Mathieu Chartierde40d472015-10-15 17:47:48 -07001377 if (!GetInstructionFlags(cur_offset + array_data_offset).IsOpcode()) {
Andreas Gampe57c47582015-07-01 22:05:59 -07001378 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array data table at " << cur_offset
1379 << ", data offset " << array_data_offset
1380 << " not correctly visited, probably bad padding.";
1381 return false;
1382 }
1383
Ian Rogersd81871c2011-10-03 13:57:23 -07001384 uint32_t value_width = array_data[1];
Elliott Hughes398f64b2012-03-26 18:05:48 -07001385 uint32_t value_count = *reinterpret_cast<const uint32_t*>(&array_data[2]);
Ian Rogersd81871c2011-10-03 13:57:23 -07001386 uint32_t table_size = 4 + (value_width * value_count + 1) / 2;
1387 /* make sure the end of the switch is in range */
1388 if (cur_offset + array_data_offset + table_size > insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -07001389 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data end: at " << cur_offset
1390 << ", data offset " << array_data_offset << ", end "
1391 << cur_offset + array_data_offset + table_size
1392 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -07001393 return false;
1394 }
1395 return true;
1396}
1397
Ian Rogers776ac1f2012-04-13 23:36:36 -07001398bool MethodVerifier::CheckBranchTarget(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001399 int32_t offset;
1400 bool isConditional, selfOkay;
1401 if (!GetBranchOffset(cur_offset, &offset, &isConditional, &selfOkay)) {
1402 return false;
1403 }
1404 if (!selfOkay && offset == 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001405 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch offset of zero not allowed at"
1406 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -07001407 return false;
1408 }
Elliott Hughes81ff3182012-03-23 20:35:56 -07001409 // Check for 32-bit overflow. This isn't strictly necessary if we can depend on the runtime
1410 // to have identical "wrap-around" behavior, but it's unwise to depend on that.
Ian Rogersd81871c2011-10-03 13:57:23 -07001411 if (((int64_t) cur_offset + (int64_t) offset) != (int64_t) (cur_offset + offset)) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001412 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch target overflow "
1413 << reinterpret_cast<void*>(cur_offset) << " +" << offset;
Ian Rogersd81871c2011-10-03 13:57:23 -07001414 return false;
1415 }
1416 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
1417 int32_t abs_offset = cur_offset + offset;
Brian Carlstrom93c33962013-07-26 10:37:43 -07001418 if (abs_offset < 0 ||
1419 (uint32_t) abs_offset >= insn_count ||
Mathieu Chartierde40d472015-10-15 17:47:48 -07001420 !GetInstructionFlags(abs_offset).IsOpcode()) {
jeffhaod5347e02012-03-22 17:25:05 -07001421 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid branch target " << offset << " (-> "
Elliott Hughes398f64b2012-03-26 18:05:48 -07001422 << reinterpret_cast<void*>(abs_offset) << ") at "
1423 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -07001424 return false;
1425 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07001426 GetInstructionFlags(abs_offset).SetBranchTarget();
Ian Rogersd81871c2011-10-03 13:57:23 -07001427 return true;
1428}
1429
Ian Rogers776ac1f2012-04-13 23:36:36 -07001430bool MethodVerifier::GetBranchOffset(uint32_t cur_offset, int32_t* pOffset, bool* pConditional,
Ian Rogersd81871c2011-10-03 13:57:23 -07001431 bool* selfOkay) {
1432 const uint16_t* insns = code_item_->insns_ + cur_offset;
1433 *pConditional = false;
1434 *selfOkay = false;
jeffhaoba5ebb92011-08-25 17:24:37 -07001435 switch (*insns & 0xff) {
1436 case Instruction::GOTO:
1437 *pOffset = ((int16_t) *insns) >> 8;
jeffhaoba5ebb92011-08-25 17:24:37 -07001438 break;
1439 case Instruction::GOTO_32:
1440 *pOffset = insns[1] | (((uint32_t) insns[2]) << 16);
jeffhaoba5ebb92011-08-25 17:24:37 -07001441 *selfOkay = true;
1442 break;
1443 case Instruction::GOTO_16:
1444 *pOffset = (int16_t) insns[1];
jeffhaoba5ebb92011-08-25 17:24:37 -07001445 break;
1446 case Instruction::IF_EQ:
1447 case Instruction::IF_NE:
1448 case Instruction::IF_LT:
1449 case Instruction::IF_GE:
1450 case Instruction::IF_GT:
1451 case Instruction::IF_LE:
1452 case Instruction::IF_EQZ:
1453 case Instruction::IF_NEZ:
1454 case Instruction::IF_LTZ:
1455 case Instruction::IF_GEZ:
1456 case Instruction::IF_GTZ:
1457 case Instruction::IF_LEZ:
1458 *pOffset = (int16_t) insns[1];
1459 *pConditional = true;
jeffhaoba5ebb92011-08-25 17:24:37 -07001460 break;
1461 default:
1462 return false;
jeffhaoba5ebb92011-08-25 17:24:37 -07001463 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001464 return true;
1465}
1466
Ian Rogers776ac1f2012-04-13 23:36:36 -07001467bool MethodVerifier::CheckSwitchTargets(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001468 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001469 DCHECK_LT(cur_offset, insn_count);
Ian Rogersd81871c2011-10-03 13:57:23 -07001470 const uint16_t* insns = code_item_->insns_ + cur_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001471 /* make sure the start of the switch is in range */
Andreas Gampe53de99c2015-08-17 13:43:55 -07001472 int32_t switch_offset = insns[1] | (static_cast<int32_t>(insns[2]) << 16);
1473 if (static_cast<int32_t>(cur_offset) + switch_offset < 0 ||
1474 cur_offset + switch_offset + 2 > insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -07001475 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch start: at " << cur_offset
Brian Carlstrom93c33962013-07-26 10:37:43 -07001476 << ", switch offset " << switch_offset
1477 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001478 return false;
1479 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001480 /* offset to switch table is a relative branch-style offset */
Ian Rogersd81871c2011-10-03 13:57:23 -07001481 const uint16_t* switch_insns = insns + switch_offset;
Andreas Gampe57c47582015-07-01 22:05:59 -07001482 // Make sure the table is at an even dex pc, that is, 32-bit aligned.
1483 if (!IsAligned<4>(switch_insns)) {
jeffhaod5347e02012-03-22 17:25:05 -07001484 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned switch table: at " << cur_offset
1485 << ", switch offset " << switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001486 return false;
1487 }
Andreas Gampe57c47582015-07-01 22:05:59 -07001488 // Make sure the switch data is marked as an opcode. This ensures that it was reached when
1489 // traversing the code item linearly. It is an approximation for a by-spec padding value.
Mathieu Chartierde40d472015-10-15 17:47:48 -07001490 if (!GetInstructionFlags(cur_offset + switch_offset).IsOpcode()) {
Andreas Gampe57c47582015-07-01 22:05:59 -07001491 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "switch table at " << cur_offset
1492 << ", switch offset " << switch_offset
1493 << " not correctly visited, probably bad padding.";
1494 return false;
1495 }
1496
David Brazdil5469d342015-09-25 16:57:53 +01001497 bool is_packed_switch = (*insns & 0xff) == Instruction::PACKED_SWITCH;
1498
Ian Rogersd81871c2011-10-03 13:57:23 -07001499 uint32_t switch_count = switch_insns[1];
David Brazdil5469d342015-09-25 16:57:53 +01001500 int32_t targets_offset;
Ian Rogersd81871c2011-10-03 13:57:23 -07001501 uint16_t expected_signature;
David Brazdil5469d342015-09-25 16:57:53 +01001502 if (is_packed_switch) {
jeffhaoba5ebb92011-08-25 17:24:37 -07001503 /* 0=sig, 1=count, 2/3=firstKey */
1504 targets_offset = 4;
jeffhaoba5ebb92011-08-25 17:24:37 -07001505 expected_signature = Instruction::kPackedSwitchSignature;
1506 } else {
1507 /* 0=sig, 1=count, 2..count*2 = keys */
jeffhaoba5ebb92011-08-25 17:24:37 -07001508 targets_offset = 2 + 2 * switch_count;
1509 expected_signature = Instruction::kSparseSwitchSignature;
1510 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001511 uint32_t table_size = targets_offset + switch_count * 2;
jeffhaoba5ebb92011-08-25 17:24:37 -07001512 if (switch_insns[0] != expected_signature) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001513 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1514 << StringPrintf("wrong signature for switch table (%x, wanted %x)",
1515 switch_insns[0], expected_signature);
jeffhaoba5ebb92011-08-25 17:24:37 -07001516 return false;
1517 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001518 /* make sure the end of the switch is in range */
1519 if (cur_offset + switch_offset + table_size > (uint32_t) insn_count) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001520 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch end: at " << cur_offset
1521 << ", switch offset " << switch_offset
1522 << ", end " << (cur_offset + switch_offset + table_size)
jeffhaod5347e02012-03-22 17:25:05 -07001523 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001524 return false;
1525 }
David Brazdil5469d342015-09-25 16:57:53 +01001526
1527 constexpr int32_t keys_offset = 2;
1528 if (switch_count > 1) {
1529 if (is_packed_switch) {
1530 /* for a packed switch, verify that keys do not overflow int32 */
1531 int32_t first_key = switch_insns[keys_offset] | (switch_insns[keys_offset + 1] << 16);
1532 int32_t max_first_key =
1533 std::numeric_limits<int32_t>::max() - (static_cast<int32_t>(switch_count) - 1);
1534 if (first_key > max_first_key) {
1535 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid packed switch: first_key=" << first_key
1536 << ", switch_count=" << switch_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001537 return false;
1538 }
David Brazdil5469d342015-09-25 16:57:53 +01001539 } else {
1540 /* for a sparse switch, verify the keys are in ascending order */
1541 int32_t last_key = switch_insns[keys_offset] | (switch_insns[keys_offset + 1] << 16);
1542 for (uint32_t targ = 1; targ < switch_count; targ++) {
1543 int32_t key =
1544 static_cast<int32_t>(switch_insns[keys_offset + targ * 2]) |
1545 static_cast<int32_t>(switch_insns[keys_offset + targ * 2 + 1] << 16);
1546 if (key <= last_key) {
1547 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid sparse switch: last key=" << last_key
1548 << ", this=" << key;
1549 return false;
1550 }
1551 last_key = key;
1552 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001553 }
1554 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001555 /* verify each switch target */
Ian Rogersd81871c2011-10-03 13:57:23 -07001556 for (uint32_t targ = 0; targ < switch_count; targ++) {
Andreas Gampe53de99c2015-08-17 13:43:55 -07001557 int32_t offset = static_cast<int32_t>(switch_insns[targets_offset + targ * 2]) |
1558 static_cast<int32_t>(switch_insns[targets_offset + targ * 2 + 1] << 16);
Ian Rogersd81871c2011-10-03 13:57:23 -07001559 int32_t abs_offset = cur_offset + offset;
Brian Carlstrom93c33962013-07-26 10:37:43 -07001560 if (abs_offset < 0 ||
Andreas Gampe53de99c2015-08-17 13:43:55 -07001561 abs_offset >= static_cast<int32_t>(insn_count) ||
Mathieu Chartierde40d472015-10-15 17:47:48 -07001562 !GetInstructionFlags(abs_offset).IsOpcode()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001563 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch target " << offset
1564 << " (-> " << reinterpret_cast<void*>(abs_offset) << ") at "
1565 << reinterpret_cast<void*>(cur_offset)
1566 << "[" << targ << "]";
jeffhaoba5ebb92011-08-25 17:24:37 -07001567 return false;
1568 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07001569 GetInstructionFlags(abs_offset).SetBranchTarget();
Ian Rogersd81871c2011-10-03 13:57:23 -07001570 }
1571 return true;
1572}
1573
Ian Rogers776ac1f2012-04-13 23:36:36 -07001574bool MethodVerifier::CheckVarArgRegs(uint32_t vA, uint32_t arg[]) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001575 uint16_t registers_size = code_item_->registers_size_;
1576 for (uint32_t idx = 0; idx < vA; idx++) {
jeffhao457cc512012-02-02 16:55:13 -08001577 if (arg[idx] >= registers_size) {
jeffhaod5347e02012-03-22 17:25:05 -07001578 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index (" << arg[idx]
1579 << ") in non-range invoke (>= " << registers_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001580 return false;
1581 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001582 }
1583
1584 return true;
1585}
1586
Ian Rogers776ac1f2012-04-13 23:36:36 -07001587bool MethodVerifier::CheckVarArgRangeRegs(uint32_t vA, uint32_t vC) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001588 uint16_t registers_size = code_item_->registers_size_;
1589 // vA/vC are unsigned 8-bit/16-bit quantities for /range instructions, so there's no risk of
1590 // integer overflow when adding them here.
1591 if (vA + vC > registers_size) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001592 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index " << vA << "+" << vC
1593 << " in range invoke (> " << registers_size << ")";
jeffhaoba5ebb92011-08-25 17:24:37 -07001594 return false;
1595 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001596 return true;
1597}
1598
Ian Rogers776ac1f2012-04-13 23:36:36 -07001599bool MethodVerifier::VerifyCodeFlow() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001600 uint16_t registers_size = code_item_->registers_size_;
1601 uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaobdb76512011-09-07 11:43:16 -07001602
Ian Rogersd81871c2011-10-03 13:57:23 -07001603 /* Create and initialize table holding register status */
Brian Carlstrom93c33962013-07-26 10:37:43 -07001604 reg_table_.Init(kTrackCompilerInterestPoints,
1605 insn_flags_.get(),
1606 insns_size,
1607 registers_size,
1608 this);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07001609
Ian Rogersd0fbd852013-09-24 18:17:04 -07001610 work_line_.reset(RegisterLine::Create(registers_size, this));
1611 saved_line_.reset(RegisterLine::Create(registers_size, this));
jeffhaobdb76512011-09-07 11:43:16 -07001612
Ian Rogersd81871c2011-10-03 13:57:23 -07001613 /* Initialize register types of method arguments. */
1614 if (!SetTypesFromSignature()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001615 DCHECK_NE(failures_.size(), 0U);
1616 std::string prepend("Bad signature in ");
David Sehr709b0702016-10-13 09:12:37 -07001617 prepend += dex_file_->PrettyMethod(dex_method_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001618 PrependToLastFailMessage(prepend);
Ian Rogersd81871c2011-10-03 13:57:23 -07001619 return false;
1620 }
Andreas Gamped5ad72f2015-06-26 17:33:47 -07001621 // We may have a runtime failure here, clear.
1622 have_pending_runtime_throw_failure_ = false;
1623
Ian Rogersd81871c2011-10-03 13:57:23 -07001624 /* Perform code flow verification. */
1625 if (!CodeFlowVerifyMethod()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001626 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -07001627 return false;
jeffhaobdb76512011-09-07 11:43:16 -07001628 }
jeffhaobdb76512011-09-07 11:43:16 -07001629 return true;
1630}
1631
Ian Rogersad0b3a32012-04-16 14:50:24 -07001632std::ostream& MethodVerifier::DumpFailures(std::ostream& os) {
1633 DCHECK_EQ(failures_.size(), failure_messages_.size());
Jeff Hao4137f482013-11-22 11:44:57 -08001634 for (size_t i = 0; i < failures_.size(); ++i) {
1635 os << failure_messages_[i]->str() << "\n";
Ian Rogersad0b3a32012-04-16 14:50:24 -07001636 }
1637 return os;
1638}
1639
Ian Rogers776ac1f2012-04-13 23:36:36 -07001640void MethodVerifier::Dump(std::ostream& os) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001641 VariableIndentationOutputStream vios(&os);
1642 Dump(&vios);
1643}
1644
1645void MethodVerifier::Dump(VariableIndentationOutputStream* vios) {
Ian Rogers7b078e82014-09-10 14:44:24 -07001646 if (code_item_ == nullptr) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001647 vios->Stream() << "Native method\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001648 return;
jeffhaobdb76512011-09-07 11:43:16 -07001649 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001650 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001651 vios->Stream() << "Register Types:\n";
1652 ScopedIndentation indent1(vios);
1653 reg_types_.Dump(vios->Stream());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001654 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001655 vios->Stream() << "Dumping instructions and register lines:\n";
1656 ScopedIndentation indent1(vios);
Ian Rogersd81871c2011-10-03 13:57:23 -07001657 const Instruction* inst = Instruction::At(code_item_->insns_);
1658 for (size_t dex_pc = 0; dex_pc < code_item_->insns_size_in_code_units_;
Andreas Gampeebf850c2015-08-14 15:37:35 -07001659 dex_pc += inst->SizeInCodeUnits(), inst = inst->Next()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001660 RegisterLine* reg_line = reg_table_.GetLine(dex_pc);
Ian Rogers7b078e82014-09-10 14:44:24 -07001661 if (reg_line != nullptr) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001662 vios->Stream() << reg_line->Dump(this) << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07001663 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001664 vios->Stream()
Mathieu Chartierde40d472015-10-15 17:47:48 -07001665 << StringPrintf("0x%04zx", dex_pc) << ": " << GetInstructionFlags(dex_pc).ToString() << " ";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001666 const bool kDumpHexOfInstruction = false;
1667 if (kDumpHexOfInstruction) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001668 vios->Stream() << inst->DumpHex(5) << " ";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001669 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001670 vios->Stream() << inst->DumpString(dex_file_) << "\n";
jeffhaoba5ebb92011-08-25 17:24:37 -07001671 }
jeffhaobdb76512011-09-07 11:43:16 -07001672}
1673
Ian Rogersd81871c2011-10-03 13:57:23 -07001674static bool IsPrimitiveDescriptor(char descriptor) {
1675 switch (descriptor) {
jeffhaobdb76512011-09-07 11:43:16 -07001676 case 'I':
1677 case 'C':
1678 case 'S':
1679 case 'B':
1680 case 'Z':
jeffhaobdb76512011-09-07 11:43:16 -07001681 case 'F':
1682 case 'D':
1683 case 'J':
Ian Rogersd81871c2011-10-03 13:57:23 -07001684 return true;
jeffhaobdb76512011-09-07 11:43:16 -07001685 default:
1686 return false;
1687 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001688}
1689
Ian Rogers776ac1f2012-04-13 23:36:36 -07001690bool MethodVerifier::SetTypesFromSignature() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001691 RegisterLine* reg_line = reg_table_.GetLine(0);
Andreas Gampeef0b1a12015-06-19 20:37:46 -07001692
1693 // Should have been verified earlier.
1694 DCHECK_GE(code_item_->registers_size_, code_item_->ins_size_);
1695
1696 uint32_t arg_start = code_item_->registers_size_ - code_item_->ins_size_;
Ian Rogersd81871c2011-10-03 13:57:23 -07001697 size_t expected_args = code_item_->ins_size_; /* long/double count as two */
jeffhaobdb76512011-09-07 11:43:16 -07001698
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001699 // Include the "this" pointer.
Ian Rogersd81871c2011-10-03 13:57:23 -07001700 size_t cur_arg = 0;
Ian Rogersad0b3a32012-04-16 14:50:24 -07001701 if (!IsStatic()) {
Andreas Gampeef0b1a12015-06-19 20:37:46 -07001702 if (expected_args == 0) {
1703 // Expect at least a receiver.
1704 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected 0 args, but method is not static";
1705 return false;
1706 }
1707
Ian Rogersd81871c2011-10-03 13:57:23 -07001708 // If this is a constructor for a class other than java.lang.Object, mark the first ("this")
1709 // argument as uninitialized. This restricts field access until the superclass constructor is
1710 // called.
Ian Rogersd8f69b02014-09-10 21:43:52 +00001711 const RegType& declaring_class = GetDeclaringClass();
Andreas Gampef10b6e12015-08-12 10:48:12 -07001712 if (IsConstructor()) {
1713 if (declaring_class.IsJavaLangObject()) {
1714 // "this" is implicitly initialized.
1715 reg_line->SetThisInitialized();
Andreas Gampead238ce2015-08-24 21:13:08 -07001716 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, declaring_class);
Andreas Gampef10b6e12015-08-12 10:48:12 -07001717 } else {
Andreas Gampead238ce2015-08-24 21:13:08 -07001718 reg_line->SetRegisterType<LockOp::kClear>(
1719 this,
1720 arg_start + cur_arg,
1721 reg_types_.UninitializedThisArgument(declaring_class));
Andreas Gampef10b6e12015-08-12 10:48:12 -07001722 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001723 } else {
Andreas Gampead238ce2015-08-24 21:13:08 -07001724 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, declaring_class);
jeffhaobdb76512011-09-07 11:43:16 -07001725 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001726 cur_arg++;
jeffhaobdb76512011-09-07 11:43:16 -07001727 }
1728
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001729 const DexFile::ProtoId& proto_id =
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001730 dex_file_->GetMethodPrototype(dex_file_->GetMethodId(dex_method_idx_));
Ian Rogers0571d352011-11-03 19:51:38 -07001731 DexFileParameterIterator iterator(*dex_file_, proto_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07001732
1733 for (; iterator.HasNext(); iterator.Next()) {
1734 const char* descriptor = iterator.GetDescriptor();
Ian Rogers7b078e82014-09-10 14:44:24 -07001735 if (descriptor == nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001736 LOG(FATAL) << "Null descriptor";
1737 }
1738 if (cur_arg >= expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07001739 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1740 << " args, found more (" << descriptor << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001741 return false;
1742 }
1743 switch (descriptor[0]) {
1744 case 'L':
1745 case '[':
1746 // We assume that reference arguments are initialized. The only way it could be otherwise
1747 // (assuming the caller was verified) is if the current method is <init>, but in that case
1748 // it's effectively considered initialized the instant we reach here (in the sense that we
1749 // can return without doing anything or call virtual methods).
1750 {
Ian Rogersd8f69b02014-09-10 21:43:52 +00001751 const RegType& reg_type = ResolveClassAndCheckAccess(iterator.GetTypeIdx());
Sebastien Hertz2ed76f92014-04-22 17:11:08 +02001752 if (!reg_type.IsNonZeroReferenceTypes()) {
1753 DCHECK(HasFailures());
1754 return false;
1755 }
Andreas Gampead238ce2015-08-24 21:13:08 -07001756 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001757 }
1758 break;
1759 case 'Z':
Andreas Gampead238ce2015-08-24 21:13:08 -07001760 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Boolean());
Ian Rogersd81871c2011-10-03 13:57:23 -07001761 break;
1762 case 'C':
Andreas Gampead238ce2015-08-24 21:13:08 -07001763 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Char());
Ian Rogersd81871c2011-10-03 13:57:23 -07001764 break;
1765 case 'B':
Andreas Gampead238ce2015-08-24 21:13:08 -07001766 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Byte());
Ian Rogersd81871c2011-10-03 13:57:23 -07001767 break;
1768 case 'I':
Andreas Gampead238ce2015-08-24 21:13:08 -07001769 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Integer());
Ian Rogersd81871c2011-10-03 13:57:23 -07001770 break;
1771 case 'S':
Andreas Gampead238ce2015-08-24 21:13:08 -07001772 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Short());
Ian Rogersd81871c2011-10-03 13:57:23 -07001773 break;
1774 case 'F':
Andreas Gampead238ce2015-08-24 21:13:08 -07001775 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Float());
Ian Rogersd81871c2011-10-03 13:57:23 -07001776 break;
1777 case 'J':
1778 case 'D': {
Andreas Gampe77cd4d62014-06-19 17:29:48 -07001779 if (cur_arg + 1 >= expected_args) {
1780 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1781 << " args, found more (" << descriptor << ")";
1782 return false;
1783 }
1784
Ian Rogers7b078e82014-09-10 14:44:24 -07001785 const RegType* lo_half;
1786 const RegType* hi_half;
1787 if (descriptor[0] == 'J') {
1788 lo_half = &reg_types_.LongLo();
1789 hi_half = &reg_types_.LongHi();
1790 } else {
1791 lo_half = &reg_types_.DoubleLo();
1792 hi_half = &reg_types_.DoubleHi();
1793 }
1794 reg_line->SetRegisterTypeWide(this, arg_start + cur_arg, *lo_half, *hi_half);
Ian Rogersd81871c2011-10-03 13:57:23 -07001795 cur_arg++;
1796 break;
1797 }
1798 default:
Brian Carlstrom93c33962013-07-26 10:37:43 -07001799 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected signature type char '"
1800 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001801 return false;
1802 }
1803 cur_arg++;
1804 }
1805 if (cur_arg != expected_args) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001806 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1807 << " arguments, found " << cur_arg;
Ian Rogersd81871c2011-10-03 13:57:23 -07001808 return false;
1809 }
1810 const char* descriptor = dex_file_->GetReturnTypeDescriptor(proto_id);
1811 // Validate return type. We don't do the type lookup; just want to make sure that it has the right
1812 // format. Only major difference from the method argument format is that 'V' is supported.
1813 bool result;
1814 if (IsPrimitiveDescriptor(descriptor[0]) || descriptor[0] == 'V') {
1815 result = descriptor[1] == '\0';
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001816 } else if (descriptor[0] == '[') { // single/multi-dimensional array of object/primitive
Ian Rogersd81871c2011-10-03 13:57:23 -07001817 size_t i = 0;
1818 do {
1819 i++;
1820 } while (descriptor[i] == '['); // process leading [
1821 if (descriptor[i] == 'L') { // object array
1822 do {
1823 i++; // find closing ;
1824 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1825 result = descriptor[i] == ';';
1826 } else { // primitive array
1827 result = IsPrimitiveDescriptor(descriptor[i]) && descriptor[i + 1] == '\0';
1828 }
1829 } else if (descriptor[0] == 'L') {
1830 // could be more thorough here, but shouldn't be required
1831 size_t i = 0;
1832 do {
1833 i++;
1834 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1835 result = descriptor[i] == ';';
1836 } else {
1837 result = false;
1838 }
1839 if (!result) {
jeffhaod5347e02012-03-22 17:25:05 -07001840 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected char in return type descriptor '"
1841 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001842 }
1843 return result;
jeffhaobdb76512011-09-07 11:43:16 -07001844}
1845
Ian Rogers776ac1f2012-04-13 23:36:36 -07001846bool MethodVerifier::CodeFlowVerifyMethod() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001847 const uint16_t* insns = code_item_->insns_;
1848 const uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaoba5ebb92011-08-25 17:24:37 -07001849
jeffhaobdb76512011-09-07 11:43:16 -07001850 /* Begin by marking the first instruction as "changed". */
Mathieu Chartierde40d472015-10-15 17:47:48 -07001851 GetInstructionFlags(0).SetChanged();
Ian Rogersd81871c2011-10-03 13:57:23 -07001852 uint32_t start_guess = 0;
jeffhaoba5ebb92011-08-25 17:24:37 -07001853
jeffhaobdb76512011-09-07 11:43:16 -07001854 /* Continue until no instructions are marked "changed". */
1855 while (true) {
Mathieu Chartier4306ef82014-12-19 18:41:47 -08001856 if (allow_thread_suspension_) {
1857 self_->AllowThreadSuspension();
1858 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001859 // Find the first marked one. Use "start_guess" as a way to find one quickly.
1860 uint32_t insn_idx = start_guess;
1861 for (; insn_idx < insns_size; insn_idx++) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07001862 if (GetInstructionFlags(insn_idx).IsChanged())
jeffhaobdb76512011-09-07 11:43:16 -07001863 break;
1864 }
jeffhaobdb76512011-09-07 11:43:16 -07001865 if (insn_idx == insns_size) {
1866 if (start_guess != 0) {
1867 /* try again, starting from the top */
1868 start_guess = 0;
1869 continue;
1870 } else {
1871 /* all flags are clear */
1872 break;
1873 }
1874 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001875 // We carry the working set of registers from instruction to instruction. If this address can
1876 // be the target of a branch (or throw) instruction, or if we're skipping around chasing
1877 // "changed" flags, we need to load the set of registers from the table.
1878 // Because we always prefer to continue on to the next instruction, we should never have a
1879 // situation where we have a stray "changed" flag set on an instruction that isn't a branch
1880 // target.
1881 work_insn_idx_ = insn_idx;
Mathieu Chartierde40d472015-10-15 17:47:48 -07001882 if (GetInstructionFlags(insn_idx).IsBranchTarget()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001883 work_line_->CopyFromLine(reg_table_.GetLine(insn_idx));
Ian Rogersebbdd872014-07-07 23:53:08 -07001884 } else if (kIsDebugBuild) {
jeffhaobdb76512011-09-07 11:43:16 -07001885 /*
1886 * Sanity check: retrieve the stored register line (assuming
1887 * a full table) and make sure it actually matches.
1888 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001889 RegisterLine* register_line = reg_table_.GetLine(insn_idx);
Ian Rogers7b078e82014-09-10 14:44:24 -07001890 if (register_line != nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001891 if (work_line_->CompareLine(register_line) != 0) {
1892 Dump(std::cout);
1893 std::cout << info_messages_.str();
David Sehr709b0702016-10-13 09:12:37 -07001894 LOG(FATAL) << "work_line diverged in " << dex_file_->PrettyMethod(dex_method_idx_)
Elliott Hughesc073b072012-05-24 19:29:17 -07001895 << "@" << reinterpret_cast<void*>(work_insn_idx_) << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07001896 << " work_line=" << work_line_->Dump(this) << "\n"
1897 << " expected=" << register_line->Dump(this);
Ian Rogersd81871c2011-10-03 13:57:23 -07001898 }
jeffhaobdb76512011-09-07 11:43:16 -07001899 }
jeffhaobdb76512011-09-07 11:43:16 -07001900 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001901 if (!CodeFlowVerifyInstruction(&start_guess)) {
David Sehr709b0702016-10-13 09:12:37 -07001902 std::string prepend(dex_file_->PrettyMethod(dex_method_idx_));
Ian Rogersad0b3a32012-04-16 14:50:24 -07001903 prepend += " failed to verify: ";
1904 PrependToLastFailMessage(prepend);
jeffhaoba5ebb92011-08-25 17:24:37 -07001905 return false;
1906 }
jeffhaobdb76512011-09-07 11:43:16 -07001907 /* Clear "changed" and mark as visited. */
Mathieu Chartierde40d472015-10-15 17:47:48 -07001908 GetInstructionFlags(insn_idx).SetVisited();
1909 GetInstructionFlags(insn_idx).ClearChanged();
jeffhaobdb76512011-09-07 11:43:16 -07001910 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001911
Andreas Gampeec6e6c12015-11-05 20:39:56 -08001912 if (kDebugVerify) {
jeffhaobdb76512011-09-07 11:43:16 -07001913 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001914 * Scan for dead code. There's nothing "evil" about dead code
jeffhaobdb76512011-09-07 11:43:16 -07001915 * (besides the wasted space), but it indicates a flaw somewhere
1916 * down the line, possibly in the verifier.
1917 *
1918 * If we've substituted "always throw" instructions into the stream,
1919 * we are almost certainly going to have some dead code.
1920 */
1921 int dead_start = -1;
Ian Rogersd81871c2011-10-03 13:57:23 -07001922 uint32_t insn_idx = 0;
Ian Rogers7b078e82014-09-10 14:44:24 -07001923 for (; insn_idx < insns_size;
1924 insn_idx += Instruction::At(code_item_->insns_ + insn_idx)->SizeInCodeUnits()) {
jeffhaobdb76512011-09-07 11:43:16 -07001925 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001926 * Switch-statement data doesn't get "visited" by scanner. It
jeffhaobdb76512011-09-07 11:43:16 -07001927 * may or may not be preceded by a padding NOP (for alignment).
1928 */
1929 if (insns[insn_idx] == Instruction::kPackedSwitchSignature ||
1930 insns[insn_idx] == Instruction::kSparseSwitchSignature ||
1931 insns[insn_idx] == Instruction::kArrayDataSignature ||
Elliott Hughes380aaa72012-07-09 14:33:15 -07001932 (insns[insn_idx] == Instruction::NOP && (insn_idx + 1 < insns_size) &&
jeffhaobdb76512011-09-07 11:43:16 -07001933 (insns[insn_idx + 1] == Instruction::kPackedSwitchSignature ||
1934 insns[insn_idx + 1] == Instruction::kSparseSwitchSignature ||
1935 insns[insn_idx + 1] == Instruction::kArrayDataSignature))) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07001936 GetInstructionFlags(insn_idx).SetVisited();
jeffhaobdb76512011-09-07 11:43:16 -07001937 }
1938
Mathieu Chartierde40d472015-10-15 17:47:48 -07001939 if (!GetInstructionFlags(insn_idx).IsVisited()) {
jeffhaobdb76512011-09-07 11:43:16 -07001940 if (dead_start < 0)
1941 dead_start = insn_idx;
1942 } else if (dead_start >= 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001943 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start)
1944 << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaobdb76512011-09-07 11:43:16 -07001945 dead_start = -1;
1946 }
1947 }
1948 if (dead_start >= 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001949 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start)
1950 << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaoba5ebb92011-08-25 17:24:37 -07001951 }
Ian Rogersc9e463c2013-06-05 16:52:26 -07001952 // To dump the state of the verify after a method, do something like:
David Sehr709b0702016-10-13 09:12:37 -07001953 // if (dex_file_->PrettyMethod(dex_method_idx_) ==
Ian Rogersc9e463c2013-06-05 16:52:26 -07001954 // "boolean java.lang.String.equals(java.lang.Object)") {
1955 // LOG(INFO) << info_messages_.str();
1956 // }
jeffhaoba5ebb92011-08-25 17:24:37 -07001957 }
jeffhaobdb76512011-09-07 11:43:16 -07001958 return true;
1959}
1960
Andreas Gampe68df3202015-06-22 11:35:46 -07001961// Returns the index of the first final instance field of the given class, or kDexNoIndex if there
1962// is no such field.
Andreas Gampea5b09a62016-11-17 15:21:22 -08001963static uint32_t GetFirstFinalInstanceFieldIndex(const DexFile& dex_file, dex::TypeIndex type_idx) {
Andreas Gampe68df3202015-06-22 11:35:46 -07001964 const DexFile::ClassDef* class_def = dex_file.FindClassDef(type_idx);
1965 DCHECK(class_def != nullptr);
1966 const uint8_t* class_data = dex_file.GetClassData(*class_def);
1967 DCHECK(class_data != nullptr);
1968 ClassDataItemIterator it(dex_file, class_data);
1969 // Skip static fields.
1970 while (it.HasNextStaticField()) {
1971 it.Next();
1972 }
1973 while (it.HasNextInstanceField()) {
1974 if ((it.GetFieldAccessFlags() & kAccFinal) != 0) {
1975 return it.GetMemberIndex();
1976 }
1977 it.Next();
1978 }
1979 return DexFile::kDexNoIndex;
1980}
1981
Andreas Gampea727e372015-08-25 09:22:37 -07001982// Setup a register line for the given return instruction.
1983static void AdjustReturnLine(MethodVerifier* verifier,
1984 const Instruction* ret_inst,
1985 RegisterLine* line) {
1986 Instruction::Code opcode = ret_inst->Opcode();
1987
1988 switch (opcode) {
1989 case Instruction::RETURN_VOID:
1990 case Instruction::RETURN_VOID_NO_BARRIER:
1991 SafelyMarkAllRegistersAsConflicts(verifier, line);
1992 break;
1993
1994 case Instruction::RETURN:
1995 case Instruction::RETURN_OBJECT:
1996 line->MarkAllRegistersAsConflictsExcept(verifier, ret_inst->VRegA_11x());
1997 break;
1998
1999 case Instruction::RETURN_WIDE:
2000 line->MarkAllRegistersAsConflictsExceptWide(verifier, ret_inst->VRegA_11x());
2001 break;
2002
2003 default:
2004 LOG(FATAL) << "Unknown return opcode " << opcode;
2005 UNREACHABLE();
2006 }
2007}
2008
Ian Rogers776ac1f2012-04-13 23:36:36 -07002009bool MethodVerifier::CodeFlowVerifyInstruction(uint32_t* start_guess) {
Elliott Hughes08fc03a2012-06-26 17:34:00 -07002010 // If we're doing FindLocksAtDexPc, check whether we're at the dex pc we care about.
2011 // We want the state _before_ the instruction, for the case where the dex pc we're
2012 // interested in is itself a monitor-enter instruction (which is a likely place
2013 // for a thread to be suspended).
Ian Rogers7b078e82014-09-10 14:44:24 -07002014 if (monitor_enter_dex_pcs_ != nullptr && work_insn_idx_ == interesting_dex_pc_) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -07002015 monitor_enter_dex_pcs_->clear(); // The new work line is more accurate than the previous one.
Elliott Hughes08fc03a2012-06-26 17:34:00 -07002016 for (size_t i = 0; i < work_line_->GetMonitorEnterCount(); ++i) {
2017 monitor_enter_dex_pcs_->push_back(work_line_->GetMonitorEnterDexPc(i));
2018 }
2019 }
2020
jeffhaobdb76512011-09-07 11:43:16 -07002021 /*
2022 * Once we finish decoding the instruction, we need to figure out where
jeffhaod1f0fde2011-09-08 17:25:33 -07002023 * we can go from here. There are three possible ways to transfer
jeffhaobdb76512011-09-07 11:43:16 -07002024 * control to another statement:
2025 *
jeffhaod1f0fde2011-09-08 17:25:33 -07002026 * (1) Continue to the next instruction. Applies to all but
jeffhaobdb76512011-09-07 11:43:16 -07002027 * unconditional branches, method returns, and exception throws.
jeffhaod1f0fde2011-09-08 17:25:33 -07002028 * (2) Branch to one or more possible locations. Applies to branches
jeffhaobdb76512011-09-07 11:43:16 -07002029 * and switch statements.
jeffhaod1f0fde2011-09-08 17:25:33 -07002030 * (3) Exception handlers. Applies to any instruction that can
jeffhaobdb76512011-09-07 11:43:16 -07002031 * throw an exception that is handled by an encompassing "try"
2032 * block.
2033 *
2034 * We can also return, in which case there is no successor instruction
2035 * from this point.
2036 *
Elliott Hughesadb8c672012-03-06 16:49:32 -08002037 * The behavior can be determined from the opcode flags.
jeffhaobdb76512011-09-07 11:43:16 -07002038 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002039 const uint16_t* insns = code_item_->insns_ + work_insn_idx_;
2040 const Instruction* inst = Instruction::At(insns);
Ian Rogersa75a0132012-09-28 11:41:42 -07002041 int opcode_flags = Instruction::FlagsOf(inst->Opcode());
jeffhaobdb76512011-09-07 11:43:16 -07002042
jeffhaobdb76512011-09-07 11:43:16 -07002043 int32_t branch_target = 0;
jeffhaobdb76512011-09-07 11:43:16 -07002044 bool just_set_result = false;
Andreas Gampeec6e6c12015-11-05 20:39:56 -08002045 if (kDebugVerify) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002046 // Generate processing back trace to debug verifier
Elliott Hughesc073b072012-05-24 19:29:17 -07002047 LogVerifyInfo() << "Processing " << inst->DumpString(dex_file_) << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07002048 << work_line_->Dump(this) << "\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07002049 }
jeffhaobdb76512011-09-07 11:43:16 -07002050
2051 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002052 * Make a copy of the previous register state. If the instruction
jeffhaobdb76512011-09-07 11:43:16 -07002053 * can throw an exception, we will copy/merge this into the "catch"
2054 * address rather than work_line, because we don't want the result
2055 * from the "successful" code path (e.g. a check-cast that "improves"
2056 * a type) to be visible to the exception handler.
2057 */
Ian Rogers776ac1f2012-04-13 23:36:36 -07002058 if ((opcode_flags & Instruction::kThrow) != 0 && CurrentInsnFlags()->IsInTry()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002059 saved_line_->CopyFromLine(work_line_.get());
Ian Rogers1ff3c982014-08-12 02:30:58 -07002060 } else if (kIsDebugBuild) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002061 saved_line_->FillWithGarbage();
jeffhaobdb76512011-09-07 11:43:16 -07002062 }
Andreas Gamped12e7822015-06-25 10:26:40 -07002063 DCHECK(!have_pending_runtime_throw_failure_); // Per-instruction flag, should not be set here.
jeffhaobdb76512011-09-07 11:43:16 -07002064
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07002065
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002066 // We need to ensure the work line is consistent while performing validation. When we spot a
2067 // peephole pattern we compute a new line for either the fallthrough instruction or the
2068 // branch target.
Mathieu Chartier361e04a2016-02-16 14:06:35 -08002069 RegisterLineArenaUniquePtr branch_line;
2070 RegisterLineArenaUniquePtr fallthrough_line;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002071
Sebastien Hertz5243e912013-05-21 10:55:07 +02002072 switch (inst->Opcode()) {
jeffhaobdb76512011-09-07 11:43:16 -07002073 case Instruction::NOP:
2074 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002075 * A "pure" NOP has no effect on anything. Data tables start with
jeffhaobdb76512011-09-07 11:43:16 -07002076 * a signature that looks like a NOP; if we see one of these in
2077 * the course of executing code then we have a problem.
2078 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002079 if (inst->VRegA_10x() != 0) {
jeffhaod5347e02012-03-22 17:25:05 -07002080 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "encountered data table in instruction stream";
jeffhaobdb76512011-09-07 11:43:16 -07002081 }
2082 break;
2083
2084 case Instruction::MOVE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002085 work_line_->CopyRegister1(this, inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategory1nr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02002086 break;
jeffhaobdb76512011-09-07 11:43:16 -07002087 case Instruction::MOVE_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07002088 work_line_->CopyRegister1(this, inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategory1nr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02002089 break;
jeffhaobdb76512011-09-07 11:43:16 -07002090 case Instruction::MOVE_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07002091 work_line_->CopyRegister1(this, inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategory1nr);
jeffhaobdb76512011-09-07 11:43:16 -07002092 break;
2093 case Instruction::MOVE_WIDE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002094 work_line_->CopyRegister2(this, inst->VRegA_12x(), inst->VRegB_12x());
Sebastien Hertz5243e912013-05-21 10:55:07 +02002095 break;
jeffhaobdb76512011-09-07 11:43:16 -07002096 case Instruction::MOVE_WIDE_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07002097 work_line_->CopyRegister2(this, inst->VRegA_22x(), inst->VRegB_22x());
Sebastien Hertz5243e912013-05-21 10:55:07 +02002098 break;
jeffhaobdb76512011-09-07 11:43:16 -07002099 case Instruction::MOVE_WIDE_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07002100 work_line_->CopyRegister2(this, inst->VRegA_32x(), inst->VRegB_32x());
jeffhaobdb76512011-09-07 11:43:16 -07002101 break;
2102 case Instruction::MOVE_OBJECT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002103 work_line_->CopyRegister1(this, inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategoryRef);
Sebastien Hertz5243e912013-05-21 10:55:07 +02002104 break;
jeffhaobdb76512011-09-07 11:43:16 -07002105 case Instruction::MOVE_OBJECT_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07002106 work_line_->CopyRegister1(this, inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategoryRef);
Sebastien Hertz5243e912013-05-21 10:55:07 +02002107 break;
jeffhaobdb76512011-09-07 11:43:16 -07002108 case Instruction::MOVE_OBJECT_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07002109 work_line_->CopyRegister1(this, inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategoryRef);
jeffhaobdb76512011-09-07 11:43:16 -07002110 break;
2111
2112 /*
2113 * The move-result instructions copy data out of a "pseudo-register"
jeffhaod1f0fde2011-09-08 17:25:33 -07002114 * with the results from the last method invocation. In practice we
jeffhaobdb76512011-09-07 11:43:16 -07002115 * might want to hold the result in an actual CPU register, so the
2116 * Dalvik spec requires that these only appear immediately after an
2117 * invoke or filled-new-array.
2118 *
jeffhaod1f0fde2011-09-08 17:25:33 -07002119 * These calls invalidate the "result" register. (This is now
jeffhaobdb76512011-09-07 11:43:16 -07002120 * redundant with the reset done below, but it can make the debug info
2121 * easier to read in some cases.)
2122 */
2123 case Instruction::MOVE_RESULT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002124 work_line_->CopyResultRegister1(this, inst->VRegA_11x(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002125 break;
2126 case Instruction::MOVE_RESULT_WIDE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002127 work_line_->CopyResultRegister2(this, inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07002128 break;
2129 case Instruction::MOVE_RESULT_OBJECT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002130 work_line_->CopyResultRegister1(this, inst->VRegA_11x(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002131 break;
2132
Ian Rogersd81871c2011-10-03 13:57:23 -07002133 case Instruction::MOVE_EXCEPTION: {
Sebastien Hertz270a0e12015-01-16 19:49:09 +01002134 // We do not allow MOVE_EXCEPTION as the first instruction in a method. This is a simple case
2135 // where one entrypoint to the catch block is not actually an exception path.
2136 if (work_insn_idx_ == 0) {
2137 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "move-exception at pc 0x0";
2138 break;
2139 }
jeffhaobdb76512011-09-07 11:43:16 -07002140 /*
jeffhao60f83e32012-02-13 17:16:30 -08002141 * This statement can only appear as the first instruction in an exception handler. We verify
2142 * that as part of extracting the exception type from the catch block list.
jeffhaobdb76512011-09-07 11:43:16 -07002143 */
Ian Rogersd8f69b02014-09-10 21:43:52 +00002144 const RegType& res_type = GetCaughtExceptionType();
Andreas Gampead238ce2015-08-24 21:13:08 -07002145 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_11x(), res_type);
jeffhaobdb76512011-09-07 11:43:16 -07002146 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002147 }
jeffhaobdb76512011-09-07 11:43:16 -07002148 case Instruction::RETURN_VOID:
Andreas Gampef10b6e12015-08-12 10:48:12 -07002149 if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002150 if (!GetMethodReturnType().IsConflict()) {
jeffhaod5347e02012-03-22 17:25:05 -07002151 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07002152 }
jeffhaobdb76512011-09-07 11:43:16 -07002153 }
2154 break;
2155 case Instruction::RETURN:
Andreas Gampef10b6e12015-08-12 10:48:12 -07002156 if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) {
jeffhaobdb76512011-09-07 11:43:16 -07002157 /* check the method signature */
Ian Rogersd8f69b02014-09-10 21:43:52 +00002158 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07002159 if (!return_type.IsCategory1Types()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002160 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected non-category 1 return type "
2161 << return_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002162 } else {
2163 // Compilers may generate synthetic functions that write byte values into boolean fields.
2164 // Also, it may use integer values for boolean, byte, short, and character return types.
Sebastien Hertz5243e912013-05-21 10:55:07 +02002165 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07002166 const RegType& src_type = work_line_->GetRegisterType(this, vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07002167 bool use_src = ((return_type.IsBoolean() && src_type.IsByte()) ||
2168 ((return_type.IsBoolean() || return_type.IsByte() ||
2169 return_type.IsShort() || return_type.IsChar()) &&
2170 src_type.IsInteger()));
2171 /* check the register contents */
Ian Rogersad0b3a32012-04-16 14:50:24 -07002172 bool success =
Ian Rogers7b078e82014-09-10 14:44:24 -07002173 work_line_->VerifyRegisterType(this, vregA, use_src ? src_type : return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002174 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002175 AppendToLastFailMessage(StringPrintf(" return-1nr on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07002176 }
jeffhaobdb76512011-09-07 11:43:16 -07002177 }
2178 }
2179 break;
2180 case Instruction::RETURN_WIDE:
Andreas Gampef10b6e12015-08-12 10:48:12 -07002181 if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) {
jeffhaobdb76512011-09-07 11:43:16 -07002182 /* check the method signature */
Ian Rogersd8f69b02014-09-10 21:43:52 +00002183 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07002184 if (!return_type.IsCategory2Types()) {
jeffhaod5347e02012-03-22 17:25:05 -07002185 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-wide not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07002186 } else {
2187 /* check the register contents */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002188 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07002189 bool success = work_line_->VerifyRegisterType(this, vregA, return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002190 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002191 AppendToLastFailMessage(StringPrintf(" return-wide on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07002192 }
jeffhaobdb76512011-09-07 11:43:16 -07002193 }
2194 }
2195 break;
2196 case Instruction::RETURN_OBJECT:
Andreas Gampef10b6e12015-08-12 10:48:12 -07002197 if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) {
Ian Rogersd8f69b02014-09-10 21:43:52 +00002198 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07002199 if (!return_type.IsReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002200 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-object not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07002201 } else {
2202 /* return_type is the *expected* return type, not register value */
2203 DCHECK(!return_type.IsZero());
2204 DCHECK(!return_type.IsUninitializedReference());
Sebastien Hertz5243e912013-05-21 10:55:07 +02002205 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07002206 const RegType& reg_type = work_line_->GetRegisterType(this, vregA);
Andreas Gampea32210c2015-06-24 10:26:13 -07002207 // Disallow returning undefined, conflict & uninitialized values and verify that the
2208 // reference in vAA is an instance of the "return_type."
2209 if (reg_type.IsUndefined()) {
2210 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning undefined register";
2211 } else if (reg_type.IsConflict()) {
2212 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning register with conflict";
2213 } else if (reg_type.IsUninitializedTypes()) {
David Brazdil68b5c0b2016-01-19 14:25:29 +00002214 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning uninitialized object '"
Brian Carlstrom93c33962013-07-26 10:37:43 -07002215 << reg_type << "'";
Andreas Gampea4c98f22015-11-06 16:24:49 -08002216 } else if (!reg_type.IsReferenceTypes()) {
2217 // We really do expect a reference here.
2218 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-object returns a non-reference type "
2219 << reg_type;
David Brazdilca3c8c32016-09-06 14:04:48 +01002220 } else if (!return_type.IsAssignableFrom(reg_type, this)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07002221 if (reg_type.IsUnresolvedTypes() || return_type.IsUnresolvedTypes()) {
2222 Fail(VERIFY_ERROR_NO_CLASS) << " can't resolve returned type '" << return_type
2223 << "' or '" << reg_type << "'";
2224 } else {
Andreas Gampe16f149c2015-03-23 10:10:20 -07002225 bool soft_error = false;
2226 // Check whether arrays are involved. They will show a valid class status, even
2227 // if their components are erroneous.
2228 if (reg_type.IsArrayTypes() && return_type.IsArrayTypes()) {
David Brazdilca3c8c32016-09-06 14:04:48 +01002229 return_type.CanAssignArray(reg_type, reg_types_, class_loader_, this, &soft_error);
Andreas Gampe16f149c2015-03-23 10:10:20 -07002230 if (soft_error) {
2231 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "array with erroneous component type: "
2232 << reg_type << " vs " << return_type;
2233 }
2234 }
2235
2236 if (!soft_error) {
2237 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning '" << reg_type
2238 << "', but expected from declaration '" << return_type << "'";
2239 }
Jeff Haoa3faaf42013-09-03 19:07:00 -07002240 }
jeffhaobdb76512011-09-07 11:43:16 -07002241 }
2242 }
2243 }
2244 break;
2245
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07002246 /* could be boolean, int, float, or a null reference */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002247 case Instruction::CONST_4: {
2248 int32_t val = static_cast<int32_t>(inst->VRegB_11n() << 28) >> 28;
Andreas Gampead238ce2015-08-24 21:13:08 -07002249 work_line_->SetRegisterType<LockOp::kClear>(
2250 this, inst->VRegA_11n(), DetermineCat1Constant(val, need_precise_constants_));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07002251 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02002252 }
2253 case Instruction::CONST_16: {
2254 int16_t val = static_cast<int16_t>(inst->VRegB_21s());
Andreas Gampead238ce2015-08-24 21:13:08 -07002255 work_line_->SetRegisterType<LockOp::kClear>(
2256 this, inst->VRegA_21s(), DetermineCat1Constant(val, need_precise_constants_));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07002257 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02002258 }
Sebastien Hertz849600b2013-12-20 10:28:08 +01002259 case Instruction::CONST: {
2260 int32_t val = inst->VRegB_31i();
Andreas Gampead238ce2015-08-24 21:13:08 -07002261 work_line_->SetRegisterType<LockOp::kClear>(
2262 this, inst->VRegA_31i(), DetermineCat1Constant(val, need_precise_constants_));
jeffhaobdb76512011-09-07 11:43:16 -07002263 break;
Sebastien Hertz849600b2013-12-20 10:28:08 +01002264 }
2265 case Instruction::CONST_HIGH16: {
2266 int32_t val = static_cast<int32_t>(inst->VRegB_21h() << 16);
Andreas Gampead238ce2015-08-24 21:13:08 -07002267 work_line_->SetRegisterType<LockOp::kClear>(
2268 this, inst->VRegA_21h(), DetermineCat1Constant(val, need_precise_constants_));
jeffhaobdb76512011-09-07 11:43:16 -07002269 break;
Sebastien Hertz849600b2013-12-20 10:28:08 +01002270 }
jeffhaobdb76512011-09-07 11:43:16 -07002271 /* could be long or double; resolved upon use */
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002272 case Instruction::CONST_WIDE_16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002273 int64_t val = static_cast<int16_t>(inst->VRegB_21s());
Ian Rogersd8f69b02014-09-10 21:43:52 +00002274 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
2275 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07002276 work_line_->SetRegisterTypeWide(this, inst->VRegA_21s(), lo, hi);
jeffhaobdb76512011-09-07 11:43:16 -07002277 break;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002278 }
2279 case Instruction::CONST_WIDE_32: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002280 int64_t val = static_cast<int32_t>(inst->VRegB_31i());
Ian Rogersd8f69b02014-09-10 21:43:52 +00002281 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
2282 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07002283 work_line_->SetRegisterTypeWide(this, inst->VRegA_31i(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002284 break;
2285 }
2286 case Instruction::CONST_WIDE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002287 int64_t val = inst->VRegB_51l();
Ian Rogersd8f69b02014-09-10 21:43:52 +00002288 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
2289 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07002290 work_line_->SetRegisterTypeWide(this, inst->VRegA_51l(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002291 break;
2292 }
2293 case Instruction::CONST_WIDE_HIGH16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002294 int64_t val = static_cast<uint64_t>(inst->VRegB_21h()) << 48;
Ian Rogersd8f69b02014-09-10 21:43:52 +00002295 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
2296 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07002297 work_line_->SetRegisterTypeWide(this, inst->VRegA_21h(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002298 break;
2299 }
jeffhaobdb76512011-09-07 11:43:16 -07002300 case Instruction::CONST_STRING:
Andreas Gampead238ce2015-08-24 21:13:08 -07002301 work_line_->SetRegisterType<LockOp::kClear>(
2302 this, inst->VRegA_21c(), reg_types_.JavaLangString());
Sebastien Hertz5243e912013-05-21 10:55:07 +02002303 break;
jeffhaobdb76512011-09-07 11:43:16 -07002304 case Instruction::CONST_STRING_JUMBO:
Andreas Gampead238ce2015-08-24 21:13:08 -07002305 work_line_->SetRegisterType<LockOp::kClear>(
2306 this, inst->VRegA_31c(), reg_types_.JavaLangString());
jeffhaobdb76512011-09-07 11:43:16 -07002307 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002308 case Instruction::CONST_CLASS: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002309 // Get type from instruction if unresolved then we need an access check
2310 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Andreas Gampea5b09a62016-11-17 15:21:22 -08002311 const RegType& res_type = ResolveClassAndCheckAccess(dex::TypeIndex(inst->VRegB_21c()));
Ian Rogersad0b3a32012-04-16 14:50:24 -07002312 // Register holds class, ie its type is class, on error it will hold Conflict.
Andreas Gampead238ce2015-08-24 21:13:08 -07002313 work_line_->SetRegisterType<LockOp::kClear>(
2314 this, inst->VRegA_21c(), res_type.IsConflict() ? res_type
2315 : reg_types_.JavaLangClass());
jeffhaobdb76512011-09-07 11:43:16 -07002316 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002317 }
jeffhaobdb76512011-09-07 11:43:16 -07002318 case Instruction::MONITOR_ENTER:
Ian Rogers7b078e82014-09-10 14:44:24 -07002319 work_line_->PushMonitor(this, inst->VRegA_11x(), work_insn_idx_);
Andreas Gampec1474102015-08-18 08:57:44 -07002320 // Check whether the previous instruction is a move-object with vAA as a source, creating
2321 // untracked lock aliasing.
Mathieu Chartierde40d472015-10-15 17:47:48 -07002322 if (0 != work_insn_idx_ && !GetInstructionFlags(work_insn_idx_).IsBranchTarget()) {
Andreas Gampec1474102015-08-18 08:57:44 -07002323 uint32_t prev_idx = work_insn_idx_ - 1;
Mathieu Chartierde40d472015-10-15 17:47:48 -07002324 while (0 != prev_idx && !GetInstructionFlags(prev_idx).IsOpcode()) {
Andreas Gampec1474102015-08-18 08:57:44 -07002325 prev_idx--;
2326 }
2327 const Instruction* prev_inst = Instruction::At(code_item_->insns_ + prev_idx);
2328 switch (prev_inst->Opcode()) {
2329 case Instruction::MOVE_OBJECT:
2330 case Instruction::MOVE_OBJECT_16:
2331 case Instruction::MOVE_OBJECT_FROM16:
2332 if (prev_inst->VRegB() == inst->VRegA_11x()) {
2333 // Redo the copy. This won't change the register types, but update the lock status
2334 // for the aliased register.
2335 work_line_->CopyRegister1(this,
2336 prev_inst->VRegA(),
2337 prev_inst->VRegB(),
2338 kTypeCategoryRef);
2339 }
2340 break;
2341
2342 default: // Other instruction types ignored.
2343 break;
2344 }
2345 }
jeffhaobdb76512011-09-07 11:43:16 -07002346 break;
2347 case Instruction::MONITOR_EXIT:
2348 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002349 * monitor-exit instructions are odd. They can throw exceptions,
jeffhaobdb76512011-09-07 11:43:16 -07002350 * but when they do they act as if they succeeded and the PC is
jeffhaod1f0fde2011-09-08 17:25:33 -07002351 * pointing to the following instruction. (This behavior goes back
jeffhaobdb76512011-09-07 11:43:16 -07002352 * to the need to handle asynchronous exceptions, a now-deprecated
2353 * feature that Dalvik doesn't support.)
2354 *
jeffhaod1f0fde2011-09-08 17:25:33 -07002355 * In practice we don't need to worry about this. The only
jeffhaobdb76512011-09-07 11:43:16 -07002356 * exceptions that can be thrown from monitor-exit are for a
jeffhaod1f0fde2011-09-08 17:25:33 -07002357 * null reference and -exit without a matching -enter. If the
jeffhaobdb76512011-09-07 11:43:16 -07002358 * structured locking checks are working, the former would have
2359 * failed on the -enter instruction, and the latter is impossible.
2360 *
2361 * This is fortunate, because issue 3221411 prevents us from
2362 * chasing the "can throw" path when monitor verification is
jeffhaod1f0fde2011-09-08 17:25:33 -07002363 * enabled. If we can fully verify the locking we can ignore
jeffhaobdb76512011-09-07 11:43:16 -07002364 * some catch blocks (which will show up as "dead" code when
2365 * we skip them here); if we can't, then the code path could be
2366 * "live" so we still need to check it.
2367 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002368 opcode_flags &= ~Instruction::kThrow;
Ian Rogers7b078e82014-09-10 14:44:24 -07002369 work_line_->PopMonitor(this, inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07002370 break;
Ian Rogers28ad40d2011-10-27 15:19:26 -07002371 case Instruction::CHECK_CAST:
Ian Rogersd81871c2011-10-03 13:57:23 -07002372 case Instruction::INSTANCE_OF: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002373 /*
2374 * If this instruction succeeds, we will "downcast" register vA to the type in vB. (This
2375 * could be a "upcast" -- not expected, so we don't try to address it.)
2376 *
2377 * If it fails, an exception is thrown, which we deal with later by ignoring the update to
Elliott Hughesadb8c672012-03-06 16:49:32 -08002378 * dec_insn.vA when branching to a handler.
Ian Rogers28ad40d2011-10-27 15:19:26 -07002379 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002380 const bool is_checkcast = (inst->Opcode() == Instruction::CHECK_CAST);
Andreas Gampea5b09a62016-11-17 15:21:22 -08002381 const dex::TypeIndex type_idx((is_checkcast) ? inst->VRegB_21c() : inst->VRegC_22c());
Ian Rogersd8f69b02014-09-10 21:43:52 +00002382 const RegType& res_type = ResolveClassAndCheckAccess(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002383 if (res_type.IsConflict()) {
Andreas Gampe00633eb2014-07-17 16:13:35 -07002384 // If this is a primitive type, fail HARD.
Mathieu Chartierbf99f772014-08-23 16:37:27 -07002385 mirror::Class* klass = dex_cache_->GetResolvedType(type_idx);
Andreas Gampe00633eb2014-07-17 16:13:35 -07002386 if (klass != nullptr && klass->IsPrimitive()) {
2387 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "using primitive type "
2388 << dex_file_->StringByTypeIdx(type_idx) << " in instanceof in "
2389 << GetDeclaringClass();
2390 break;
2391 }
2392
Ian Rogersad0b3a32012-04-16 14:50:24 -07002393 DCHECK_NE(failures_.size(), 0U);
2394 if (!is_checkcast) {
Andreas Gampead238ce2015-08-24 21:13:08 -07002395 work_line_->SetRegisterType<LockOp::kClear>(this,
2396 inst->VRegA_22c(),
2397 reg_types_.Boolean());
Ian Rogersad0b3a32012-04-16 14:50:24 -07002398 }
2399 break; // bad class
Ian Rogers9f1ab122011-12-12 08:52:43 -08002400 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002401 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Sebastien Hertz5243e912013-05-21 10:55:07 +02002402 uint32_t orig_type_reg = (is_checkcast) ? inst->VRegA_21c() : inst->VRegB_22c();
Ian Rogers7b078e82014-09-10 14:44:24 -07002403 const RegType& orig_type = work_line_->GetRegisterType(this, orig_type_reg);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002404 if (!res_type.IsNonZeroReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002405 if (is_checkcast) {
2406 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on unexpected class " << res_type;
2407 } else {
2408 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on unexpected class " << res_type;
2409 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002410 } else if (!orig_type.IsReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002411 if (is_checkcast) {
2412 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on non-reference in v" << orig_type_reg;
2413 } else {
2414 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on non-reference in v" << orig_type_reg;
2415 }
David Brazdil68b5c0b2016-01-19 14:25:29 +00002416 } else if (orig_type.IsUninitializedTypes()) {
2417 if (is_checkcast) {
2418 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on uninitialized reference in v"
2419 << orig_type_reg;
2420 } else {
2421 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on uninitialized reference in v"
2422 << orig_type_reg;
2423 }
jeffhao2a8a90e2011-09-26 14:25:31 -07002424 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002425 if (is_checkcast) {
Andreas Gampead238ce2015-08-24 21:13:08 -07002426 work_line_->SetRegisterType<LockOp::kKeep>(this, inst->VRegA_21c(), res_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07002427 } else {
Andreas Gampead238ce2015-08-24 21:13:08 -07002428 work_line_->SetRegisterType<LockOp::kClear>(this,
2429 inst->VRegA_22c(),
2430 reg_types_.Boolean());
jeffhaobdb76512011-09-07 11:43:16 -07002431 }
jeffhaobdb76512011-09-07 11:43:16 -07002432 }
jeffhao2a8a90e2011-09-26 14:25:31 -07002433 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002434 }
2435 case Instruction::ARRAY_LENGTH: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002436 const RegType& res_type = work_line_->GetRegisterType(this, inst->VRegB_12x());
Ian Rogers28ad40d2011-10-27 15:19:26 -07002437 if (res_type.IsReferenceTypes()) {
Ian Rogers89310de2012-02-01 13:47:30 -08002438 if (!res_type.IsArrayTypes() && !res_type.IsZero()) { // ie not an array or null
jeffhaod5347e02012-03-22 17:25:05 -07002439 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002440 } else {
Andreas Gampead238ce2015-08-24 21:13:08 -07002441 work_line_->SetRegisterType<LockOp::kClear>(this,
2442 inst->VRegA_12x(),
2443 reg_types_.Integer());
Ian Rogersd81871c2011-10-03 13:57:23 -07002444 }
Andreas Gampe65c9db82014-07-28 13:14:34 -07002445 } else {
2446 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002447 }
2448 break;
2449 }
2450 case Instruction::NEW_INSTANCE: {
Andreas Gampea5b09a62016-11-17 15:21:22 -08002451 const RegType& res_type = ResolveClassAndCheckAccess(dex::TypeIndex(inst->VRegB_21c()));
Ian Rogersad0b3a32012-04-16 14:50:24 -07002452 if (res_type.IsConflict()) {
2453 DCHECK_NE(failures_.size(), 0U);
2454 break; // bad class
jeffhao8cd6dda2012-02-22 10:15:34 -08002455 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002456 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
2457 // can't create an instance of an interface or abstract class */
2458 if (!res_type.IsInstantiableTypes()) {
2459 Fail(VERIFY_ERROR_INSTANTIATION)
2460 << "new-instance on primitive, interface or abstract class" << res_type;
Ian Rogers08f753d2012-08-24 14:35:25 -07002461 // Soft failure so carry on to set register type.
Ian Rogersd81871c2011-10-03 13:57:23 -07002462 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00002463 const RegType& uninit_type = reg_types_.Uninitialized(res_type, work_insn_idx_);
Ian Rogers08f753d2012-08-24 14:35:25 -07002464 // Any registers holding previous allocations from this address that have not yet been
2465 // initialized must be marked invalid.
Ian Rogers7b078e82014-09-10 14:44:24 -07002466 work_line_->MarkUninitRefsAsInvalid(this, uninit_type);
Ian Rogers08f753d2012-08-24 14:35:25 -07002467 // add the new uninitialized reference to the register state
Andreas Gampead238ce2015-08-24 21:13:08 -07002468 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_21c(), uninit_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07002469 break;
2470 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08002471 case Instruction::NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002472 VerifyNewArray(inst, false, false);
jeffhaobdb76512011-09-07 11:43:16 -07002473 break;
2474 case Instruction::FILLED_NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002475 VerifyNewArray(inst, true, false);
Ian Rogers0c4a5062012-02-03 15:18:59 -08002476 just_set_result = true; // Filled new array sets result register
jeffhaobdb76512011-09-07 11:43:16 -07002477 break;
Ian Rogers0c4a5062012-02-03 15:18:59 -08002478 case Instruction::FILLED_NEW_ARRAY_RANGE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002479 VerifyNewArray(inst, true, true);
Ian Rogers0c4a5062012-02-03 15:18:59 -08002480 just_set_result = true; // Filled new array range sets result register
2481 break;
jeffhaobdb76512011-09-07 11:43:16 -07002482 case Instruction::CMPL_FLOAT:
2483 case Instruction::CMPG_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002484 if (!work_line_->VerifyRegisterType(this, inst->VRegB_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08002485 break;
2486 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002487 if (!work_line_->VerifyRegisterType(this, inst->VRegC_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08002488 break;
2489 }
Andreas Gampead238ce2015-08-24 21:13:08 -07002490 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002491 break;
2492 case Instruction::CMPL_DOUBLE:
2493 case Instruction::CMPG_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002494 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegB_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002495 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002496 break;
2497 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002498 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegC_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002499 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002500 break;
2501 }
Andreas Gampead238ce2015-08-24 21:13:08 -07002502 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002503 break;
2504 case Instruction::CMP_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002505 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegB_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002506 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002507 break;
2508 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002509 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegC_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002510 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002511 break;
2512 }
Andreas Gampead238ce2015-08-24 21:13:08 -07002513 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002514 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002515 case Instruction::THROW: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002516 const RegType& res_type = work_line_->GetRegisterType(this, inst->VRegA_11x());
David Brazdilca3c8c32016-09-06 14:04:48 +01002517 if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(res_type, this)) {
David Brazdil68b5c0b2016-01-19 14:25:29 +00002518 if (res_type.IsUninitializedTypes()) {
2519 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "thrown exception not initialized";
Pavel Vyssotski980027c2016-02-11 20:28:11 +06002520 } else if (!res_type.IsReferenceTypes()) {
2521 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "thrown value of non-reference type " << res_type;
David Brazdil68b5c0b2016-01-19 14:25:29 +00002522 } else {
2523 Fail(res_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS : VERIFY_ERROR_BAD_CLASS_SOFT)
2524 << "thrown class " << res_type << " not instanceof Throwable";
2525 }
jeffhaobdb76512011-09-07 11:43:16 -07002526 }
2527 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002528 }
jeffhaobdb76512011-09-07 11:43:16 -07002529 case Instruction::GOTO:
2530 case Instruction::GOTO_16:
2531 case Instruction::GOTO_32:
2532 /* no effect on or use of registers */
2533 break;
2534
2535 case Instruction::PACKED_SWITCH:
2536 case Instruction::SPARSE_SWITCH:
2537 /* verify that vAA is an integer, or can be converted to one */
Ian Rogers7b078e82014-09-10 14:44:24 -07002538 work_line_->VerifyRegisterType(this, inst->VRegA_31t(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002539 break;
2540
Ian Rogersd81871c2011-10-03 13:57:23 -07002541 case Instruction::FILL_ARRAY_DATA: {
2542 /* Similar to the verification done for APUT */
Ian Rogers7b078e82014-09-10 14:44:24 -07002543 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegA_31t());
Ian Rogers89310de2012-02-01 13:47:30 -08002544 /* array_type can be null if the reg type is Zero */
2545 if (!array_type.IsZero()) {
jeffhao457cc512012-02-02 16:55:13 -08002546 if (!array_type.IsArrayTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002547 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with array type "
2548 << array_type;
Andreas Gampebb18a032016-03-22 20:34:25 -07002549 } else if (array_type.IsUnresolvedTypes()) {
2550 // If it's an unresolved array type, it must be non-primitive.
2551 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data for array of type "
2552 << array_type;
Ian Rogers89310de2012-02-01 13:47:30 -08002553 } else {
Ian Rogersd8f69b02014-09-10 21:43:52 +00002554 const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
Ian Rogersad0b3a32012-04-16 14:50:24 -07002555 DCHECK(!component_type.IsConflict());
jeffhao457cc512012-02-02 16:55:13 -08002556 if (component_type.IsNonZeroReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002557 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with component type "
2558 << component_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002559 } else {
jeffhao457cc512012-02-02 16:55:13 -08002560 // Now verify if the element width in the table matches the element width declared in
2561 // the array
Andreas Gampe53de99c2015-08-17 13:43:55 -07002562 const uint16_t* array_data =
2563 insns + (insns[1] | (static_cast<int32_t>(insns[2]) << 16));
jeffhao457cc512012-02-02 16:55:13 -08002564 if (array_data[0] != Instruction::kArrayDataSignature) {
jeffhaod5347e02012-03-22 17:25:05 -07002565 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid magic for array-data";
jeffhao457cc512012-02-02 16:55:13 -08002566 } else {
2567 size_t elem_width = Primitive::ComponentSize(component_type.GetPrimitiveType());
2568 // Since we don't compress the data in Dex, expect to see equal width of data stored
2569 // in the table and expected from the array class.
2570 if (array_data[1] != elem_width) {
jeffhaod5347e02012-03-22 17:25:05 -07002571 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-data size mismatch (" << array_data[1]
2572 << " vs " << elem_width << ")";
jeffhao457cc512012-02-02 16:55:13 -08002573 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002574 }
2575 }
jeffhaobdb76512011-09-07 11:43:16 -07002576 }
2577 }
2578 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002579 }
jeffhaobdb76512011-09-07 11:43:16 -07002580 case Instruction::IF_EQ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002581 case Instruction::IF_NE: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002582 const RegType& reg_type1 = work_line_->GetRegisterType(this, inst->VRegA_22t());
2583 const RegType& reg_type2 = work_line_->GetRegisterType(this, inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002584 bool mismatch = false;
2585 if (reg_type1.IsZero()) { // zero then integral or reference expected
2586 mismatch = !reg_type2.IsReferenceTypes() && !reg_type2.IsIntegralTypes();
2587 } else if (reg_type1.IsReferenceTypes()) { // both references?
2588 mismatch = !reg_type2.IsReferenceTypes();
2589 } else { // both integral?
2590 mismatch = !reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes();
2591 }
2592 if (mismatch) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002593 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to if-eq/if-ne (" << reg_type1 << ","
2594 << reg_type2 << ") must both be references or integral";
jeffhaobdb76512011-09-07 11:43:16 -07002595 }
2596 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002597 }
jeffhaobdb76512011-09-07 11:43:16 -07002598 case Instruction::IF_LT:
2599 case Instruction::IF_GE:
2600 case Instruction::IF_GT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002601 case Instruction::IF_LE: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002602 const RegType& reg_type1 = work_line_->GetRegisterType(this, inst->VRegA_22t());
2603 const RegType& reg_type2 = work_line_->GetRegisterType(this, inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002604 if (!reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002605 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to 'if' (" << reg_type1 << ","
2606 << reg_type2 << ") must be integral";
jeffhaobdb76512011-09-07 11:43:16 -07002607 }
2608 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002609 }
jeffhaobdb76512011-09-07 11:43:16 -07002610 case Instruction::IF_EQZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002611 case Instruction::IF_NEZ: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002612 const RegType& reg_type = work_line_->GetRegisterType(this, inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002613 if (!reg_type.IsReferenceTypes() && !reg_type.IsIntegralTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002614 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
2615 << " unexpected as arg to if-eqz/if-nez";
Ian Rogersd81871c2011-10-03 13:57:23 -07002616 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002617
2618 // Find previous instruction - its existence is a precondition to peephole optimization.
Ian Rogers9b360392013-06-06 14:45:07 -07002619 uint32_t instance_of_idx = 0;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002620 if (0 != work_insn_idx_) {
Ian Rogers9b360392013-06-06 14:45:07 -07002621 instance_of_idx = work_insn_idx_ - 1;
Mathieu Chartierde40d472015-10-15 17:47:48 -07002622 while (0 != instance_of_idx && !GetInstructionFlags(instance_of_idx).IsOpcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07002623 instance_of_idx--;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002624 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07002625 if (FailOrAbort(this, GetInstructionFlags(instance_of_idx).IsOpcode(),
Andreas Gampe7c038102014-10-27 20:08:46 -07002626 "Unable to get previous instruction of if-eqz/if-nez for work index ",
2627 work_insn_idx_)) {
2628 break;
2629 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002630 } else {
2631 break;
2632 }
2633
Ian Rogers9b360392013-06-06 14:45:07 -07002634 const Instruction* instance_of_inst = Instruction::At(code_item_->insns_ + instance_of_idx);
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002635
2636 /* Check for peep-hole pattern of:
2637 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002638 * instance-of vX, vY, T;
2639 * ifXXX vX, label ;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002640 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002641 * label:
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002642 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002643 * and sharpen the type of vY to be type T.
2644 * Note, this pattern can't be if:
2645 * - if there are other branches to this branch,
2646 * - when vX == vY.
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002647 */
Ian Rogersfae370a2013-06-05 08:33:27 -07002648 if (!CurrentInsnFlags()->IsBranchTarget() &&
Ian Rogers9b360392013-06-06 14:45:07 -07002649 (Instruction::INSTANCE_OF == instance_of_inst->Opcode()) &&
2650 (inst->VRegA_21t() == instance_of_inst->VRegA_22c()) &&
2651 (instance_of_inst->VRegA_22c() != instance_of_inst->VRegB_22c())) {
Ian Rogersebbdd872014-07-07 23:53:08 -07002652 // Check the type of the instance-of is different than that of registers type, as if they
2653 // are the same there is no work to be done here. Check that the conversion is not to or
2654 // from an unresolved type as type information is imprecise. If the instance-of is to an
2655 // interface then ignore the type information as interfaces can only be treated as Objects
2656 // and we don't want to disallow field and other operations on the object. If the value
2657 // being instance-of checked against is known null (zero) then allow the optimization as
2658 // we didn't have type information. If the merge of the instance-of type with the original
2659 // type is assignable to the original then allow optimization. This check is performed to
2660 // ensure that subsequent merges don't lose type information - such as becoming an
2661 // interface from a class that would lose information relevant to field checks.
Ian Rogers7b078e82014-09-10 14:44:24 -07002662 const RegType& orig_type = work_line_->GetRegisterType(this, instance_of_inst->VRegB_22c());
Andreas Gampea5b09a62016-11-17 15:21:22 -08002663 const RegType& cast_type = ResolveClassAndCheckAccess(
2664 dex::TypeIndex(instance_of_inst->VRegC_22c()));
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002665
Ian Rogersebbdd872014-07-07 23:53:08 -07002666 if (!orig_type.Equals(cast_type) &&
2667 !cast_type.IsUnresolvedTypes() && !orig_type.IsUnresolvedTypes() &&
Andreas Gampe00633eb2014-07-17 16:13:35 -07002668 cast_type.HasClass() && // Could be conflict type, make sure it has a class.
Ian Rogersebbdd872014-07-07 23:53:08 -07002669 !cast_type.GetClass()->IsInterface() &&
2670 (orig_type.IsZero() ||
David Brazdilca3c8c32016-09-06 14:04:48 +01002671 orig_type.IsStrictlyAssignableFrom(
2672 cast_type.Merge(orig_type, &reg_types_, this), this))) {
Ian Rogersd0fbd852013-09-24 18:17:04 -07002673 RegisterLine* update_line = RegisterLine::Create(code_item_->registers_size_, this);
Ian Rogersfae370a2013-06-05 08:33:27 -07002674 if (inst->Opcode() == Instruction::IF_EQZ) {
Ian Rogers9b360392013-06-06 14:45:07 -07002675 fallthrough_line.reset(update_line);
Ian Rogersfae370a2013-06-05 08:33:27 -07002676 } else {
Ian Rogers9b360392013-06-06 14:45:07 -07002677 branch_line.reset(update_line);
2678 }
2679 update_line->CopyFromLine(work_line_.get());
Andreas Gampead238ce2015-08-24 21:13:08 -07002680 update_line->SetRegisterType<LockOp::kKeep>(this,
2681 instance_of_inst->VRegB_22c(),
2682 cast_type);
Mathieu Chartierde40d472015-10-15 17:47:48 -07002683 if (!GetInstructionFlags(instance_of_idx).IsBranchTarget() && 0 != instance_of_idx) {
Ian Rogers9b360392013-06-06 14:45:07 -07002684 // See if instance-of was preceded by a move-object operation, common due to the small
2685 // register encoding space of instance-of, and propagate type information to the source
2686 // of the move-object.
2687 uint32_t move_idx = instance_of_idx - 1;
Mathieu Chartierde40d472015-10-15 17:47:48 -07002688 while (0 != move_idx && !GetInstructionFlags(move_idx).IsOpcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07002689 move_idx--;
2690 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07002691 if (FailOrAbort(this, GetInstructionFlags(move_idx).IsOpcode(),
Andreas Gampe7c038102014-10-27 20:08:46 -07002692 "Unable to get previous instruction of if-eqz/if-nez for work index ",
2693 work_insn_idx_)) {
2694 break;
2695 }
Ian Rogers9b360392013-06-06 14:45:07 -07002696 const Instruction* move_inst = Instruction::At(code_item_->insns_ + move_idx);
2697 switch (move_inst->Opcode()) {
2698 case Instruction::MOVE_OBJECT:
2699 if (move_inst->VRegA_12x() == instance_of_inst->VRegB_22c()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07002700 update_line->SetRegisterType<LockOp::kKeep>(this,
2701 move_inst->VRegB_12x(),
2702 cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002703 }
2704 break;
2705 case Instruction::MOVE_OBJECT_FROM16:
2706 if (move_inst->VRegA_22x() == instance_of_inst->VRegB_22c()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07002707 update_line->SetRegisterType<LockOp::kKeep>(this,
2708 move_inst->VRegB_22x(),
2709 cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002710 }
2711 break;
2712 case Instruction::MOVE_OBJECT_16:
2713 if (move_inst->VRegA_32x() == instance_of_inst->VRegB_22c()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07002714 update_line->SetRegisterType<LockOp::kKeep>(this,
2715 move_inst->VRegB_32x(),
2716 cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002717 }
2718 break;
2719 default:
2720 break;
2721 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002722 }
2723 }
2724 }
2725
jeffhaobdb76512011-09-07 11:43:16 -07002726 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002727 }
jeffhaobdb76512011-09-07 11:43:16 -07002728 case Instruction::IF_LTZ:
2729 case Instruction::IF_GEZ:
2730 case Instruction::IF_GTZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002731 case Instruction::IF_LEZ: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002732 const RegType& reg_type = work_line_->GetRegisterType(this, inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002733 if (!reg_type.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002734 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
2735 << " unexpected as arg to if-ltz/if-gez/if-gtz/if-lez";
Ian Rogersd81871c2011-10-03 13:57:23 -07002736 }
jeffhaobdb76512011-09-07 11:43:16 -07002737 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002738 }
jeffhaobdb76512011-09-07 11:43:16 -07002739 case Instruction::AGET_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002740 VerifyAGet(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002741 break;
jeffhaobdb76512011-09-07 11:43:16 -07002742 case Instruction::AGET_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002743 VerifyAGet(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002744 break;
jeffhaobdb76512011-09-07 11:43:16 -07002745 case Instruction::AGET_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002746 VerifyAGet(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002747 break;
jeffhaobdb76512011-09-07 11:43:16 -07002748 case Instruction::AGET_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002749 VerifyAGet(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002750 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002751 case Instruction::AGET:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002752 VerifyAGet(inst, reg_types_.Integer(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002753 break;
jeffhaobdb76512011-09-07 11:43:16 -07002754 case Instruction::AGET_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002755 VerifyAGet(inst, reg_types_.LongLo(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002756 break;
2757 case Instruction::AGET_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002758 VerifyAGet(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07002759 break;
2760
Ian Rogersd81871c2011-10-03 13:57:23 -07002761 case Instruction::APUT_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002762 VerifyAPut(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002763 break;
2764 case Instruction::APUT_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002765 VerifyAPut(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002766 break;
2767 case Instruction::APUT_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002768 VerifyAPut(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002769 break;
2770 case Instruction::APUT_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002771 VerifyAPut(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002772 break;
2773 case Instruction::APUT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002774 VerifyAPut(inst, reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002775 break;
2776 case Instruction::APUT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002777 VerifyAPut(inst, reg_types_.LongLo(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002778 break;
2779 case Instruction::APUT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002780 VerifyAPut(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07002781 break;
2782
jeffhaobdb76512011-09-07 11:43:16 -07002783 case Instruction::IGET_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002784 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002785 break;
jeffhaobdb76512011-09-07 11:43:16 -07002786 case Instruction::IGET_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002787 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002788 break;
jeffhaobdb76512011-09-07 11:43:16 -07002789 case Instruction::IGET_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002790 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002791 break;
jeffhaobdb76512011-09-07 11:43:16 -07002792 case Instruction::IGET_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002793 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002794 break;
2795 case Instruction::IGET:
Andreas Gampe896df402014-10-20 22:25:29 -07002796 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002797 break;
2798 case Instruction::IGET_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002799 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002800 break;
2801 case Instruction::IGET_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002802 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false,
2803 false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002804 break;
jeffhaobdb76512011-09-07 11:43:16 -07002805
Ian Rogersd81871c2011-10-03 13:57:23 -07002806 case Instruction::IPUT_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002807 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002808 break;
2809 case Instruction::IPUT_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002810 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002811 break;
2812 case Instruction::IPUT_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002813 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002814 break;
2815 case Instruction::IPUT_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002816 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002817 break;
2818 case Instruction::IPUT:
Andreas Gampe896df402014-10-20 22:25:29 -07002819 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002820 break;
2821 case Instruction::IPUT_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002822 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002823 break;
jeffhaobdb76512011-09-07 11:43:16 -07002824 case Instruction::IPUT_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002825 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false,
2826 false);
jeffhaobdb76512011-09-07 11:43:16 -07002827 break;
2828
jeffhaobdb76512011-09-07 11:43:16 -07002829 case Instruction::SGET_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002830 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002831 break;
jeffhaobdb76512011-09-07 11:43:16 -07002832 case Instruction::SGET_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002833 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002834 break;
jeffhaobdb76512011-09-07 11:43:16 -07002835 case Instruction::SGET_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002836 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002837 break;
jeffhaobdb76512011-09-07 11:43:16 -07002838 case Instruction::SGET_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002839 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002840 break;
2841 case Instruction::SGET:
Andreas Gampe896df402014-10-20 22:25:29 -07002842 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002843 break;
2844 case Instruction::SGET_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002845 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002846 break;
2847 case Instruction::SGET_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002848 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false,
2849 true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002850 break;
2851
2852 case Instruction::SPUT_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002853 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002854 break;
2855 case Instruction::SPUT_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002856 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002857 break;
2858 case Instruction::SPUT_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002859 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002860 break;
2861 case Instruction::SPUT_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002862 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002863 break;
2864 case Instruction::SPUT:
Andreas Gampe896df402014-10-20 22:25:29 -07002865 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002866 break;
2867 case Instruction::SPUT_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002868 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002869 break;
2870 case Instruction::SPUT_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002871 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false,
2872 true);
jeffhaobdb76512011-09-07 11:43:16 -07002873 break;
2874
2875 case Instruction::INVOKE_VIRTUAL:
2876 case Instruction::INVOKE_VIRTUAL_RANGE:
2877 case Instruction::INVOKE_SUPER:
Ian Rogersd81871c2011-10-03 13:57:23 -07002878 case Instruction::INVOKE_SUPER_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002879 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE ||
2880 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002881 bool is_super = (inst->Opcode() == Instruction::INVOKE_SUPER ||
2882 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
Alex Light7268d472016-01-20 15:50:01 -08002883 MethodType type = is_super ? METHOD_SUPER : METHOD_VIRTUAL;
2884 ArtMethod* called_method = VerifyInvocationArgs(inst, type, is_range);
Ian Rogersd8f69b02014-09-10 21:43:52 +00002885 const RegType* return_type = nullptr;
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002886 if (called_method != nullptr) {
Andreas Gampe542451c2016-07-26 09:02:02 -07002887 PointerSize pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
Vladimir Marko05792b92015-08-03 11:56:49 +01002888 mirror::Class* return_type_class = called_method->GetReturnType(can_load_classes_,
2889 pointer_size);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002890 if (return_type_class != nullptr) {
Andreas Gampef23f33d2015-06-23 14:18:17 -07002891 return_type = &FromClass(called_method->GetReturnTypeDescriptor(),
2892 return_type_class,
2893 return_type_class->CannotBeAssignedFromOtherTypes());
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002894 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07002895 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
2896 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002897 }
2898 }
2899 if (return_type == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002900 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002901 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
Andreas Gampea5b09a62016-11-17 15:21:22 -08002902 dex::TypeIndex return_type_idx =
2903 dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002904 const char* descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Mathieu Chartierbf99f772014-08-23 16:37:27 -07002905 return_type = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
jeffhaobdb76512011-09-07 11:43:16 -07002906 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002907 if (!return_type->IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002908 work_line_->SetResultRegisterType(this, *return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002909 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002910 work_line_->SetResultRegisterTypeWide(*return_type, return_type->HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002911 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002912 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002913 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002914 }
jeffhaobdb76512011-09-07 11:43:16 -07002915 case Instruction::INVOKE_DIRECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002916 case Instruction::INVOKE_DIRECT_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002917 bool is_range = (inst->Opcode() == Instruction::INVOKE_DIRECT_RANGE);
Alex Light7268d472016-01-20 15:50:01 -08002918 ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_DIRECT, is_range);
Ian Rogers46685432012-06-03 22:26:43 -07002919 const char* return_type_descriptor;
2920 bool is_constructor;
Ian Rogersd8f69b02014-09-10 21:43:52 +00002921 const RegType* return_type = nullptr;
Ian Rogers7b078e82014-09-10 14:44:24 -07002922 if (called_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002923 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogers46685432012-06-03 22:26:43 -07002924 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
Ian Rogersdfb325e2013-10-30 01:00:44 -07002925 is_constructor = strcmp("<init>", dex_file_->StringDataByIdx(method_id.name_idx_)) == 0;
Andreas Gampea5b09a62016-11-17 15:21:22 -08002926 dex::TypeIndex return_type_idx =
2927 dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogers46685432012-06-03 22:26:43 -07002928 return_type_descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2929 } else {
2930 is_constructor = called_method->IsConstructor();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002931 return_type_descriptor = called_method->GetReturnTypeDescriptor();
Andreas Gampe542451c2016-07-26 09:02:02 -07002932 PointerSize pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
Vladimir Marko05792b92015-08-03 11:56:49 +01002933 mirror::Class* return_type_class = called_method->GetReturnType(can_load_classes_,
2934 pointer_size);
Ian Rogers1ff3c982014-08-12 02:30:58 -07002935 if (return_type_class != nullptr) {
Andreas Gampef23f33d2015-06-23 14:18:17 -07002936 return_type = &FromClass(return_type_descriptor,
2937 return_type_class,
2938 return_type_class->CannotBeAssignedFromOtherTypes());
Ian Rogers1ff3c982014-08-12 02:30:58 -07002939 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07002940 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
2941 self_->ClearException();
Ian Rogers1ff3c982014-08-12 02:30:58 -07002942 }
Ian Rogers46685432012-06-03 22:26:43 -07002943 }
2944 if (is_constructor) {
jeffhaobdb76512011-09-07 11:43:16 -07002945 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002946 * Some additional checks when calling a constructor. We know from the invocation arg check
2947 * that the "this" argument is an instance of called_method->klass. Now we further restrict
2948 * that to require that called_method->klass is the same as this->klass or this->super,
2949 * allowing the latter only if the "this" argument is the same as the "this" argument to
2950 * this method (which implies that we're in a constructor ourselves).
jeffhaobdb76512011-09-07 11:43:16 -07002951 */
Orion Hodsoncfa325e2016-10-13 10:25:54 +01002952 const RegType& this_type = work_line_->GetInvocationThis(this, inst);
jeffhaob57e9522012-04-26 18:08:21 -07002953 if (this_type.IsConflict()) // failure.
2954 break;
jeffhaobdb76512011-09-07 11:43:16 -07002955
jeffhaob57e9522012-04-26 18:08:21 -07002956 /* no null refs allowed (?) */
2957 if (this_type.IsZero()) {
2958 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unable to initialize null ref";
2959 break;
jeffhao2a8a90e2011-09-26 14:25:31 -07002960 }
jeffhaob57e9522012-04-26 18:08:21 -07002961
2962 /* must be in same class or in superclass */
Ian Rogersd8f69b02014-09-10 21:43:52 +00002963 // const RegType& this_super_klass = this_type.GetSuperClass(&reg_types_);
Ian Rogers46685432012-06-03 22:26:43 -07002964 // TODO: re-enable constructor type verification
2965 // if (this_super_klass.IsConflict()) {
jeffhaob57e9522012-04-26 18:08:21 -07002966 // Unknown super class, fail so we re-check at runtime.
Ian Rogers46685432012-06-03 22:26:43 -07002967 // Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "super class unknown for '" << this_type << "'";
2968 // break;
2969 // }
jeffhaob57e9522012-04-26 18:08:21 -07002970
2971 /* arg must be an uninitialized reference */
2972 if (!this_type.IsUninitializedTypes()) {
2973 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Expected initialization on uninitialized reference "
2974 << this_type;
2975 break;
2976 }
2977
2978 /*
2979 * Replace the uninitialized reference with an initialized one. We need to do this for all
2980 * registers that have the same object instance in them, not just the "this" register.
2981 */
Nicolas Geoffray98e6ce42016-02-16 18:42:15 +00002982 work_line_->MarkRefsAsInitialized(this, this_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002983 }
Ian Rogers1ff3c982014-08-12 02:30:58 -07002984 if (return_type == nullptr) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07002985 return_type = &reg_types_.FromDescriptor(GetClassLoader(), return_type_descriptor, false);
Ian Rogers1ff3c982014-08-12 02:30:58 -07002986 }
2987 if (!return_type->IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002988 work_line_->SetResultRegisterType(this, *return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002989 } else {
Ian Rogers1ff3c982014-08-12 02:30:58 -07002990 work_line_->SetResultRegisterTypeWide(*return_type, return_type->HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002991 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002992 just_set_result = true;
2993 break;
2994 }
2995 case Instruction::INVOKE_STATIC:
2996 case Instruction::INVOKE_STATIC_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002997 bool is_range = (inst->Opcode() == Instruction::INVOKE_STATIC_RANGE);
Alex Light7268d472016-01-20 15:50:01 -08002998 ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_STATIC, is_range);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002999 const char* descriptor;
Ian Rogers7b078e82014-09-10 14:44:24 -07003000 if (called_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003001 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogers28ad40d2011-10-27 15:19:26 -07003002 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
Andreas Gampea5b09a62016-11-17 15:21:22 -08003003 dex::TypeIndex return_type_idx =
3004 dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Mathieu Chartier590fee92013-09-13 13:46:47 -07003005 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07003006 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003007 descriptor = called_method->GetReturnTypeDescriptor();
Ian Rogers28ad40d2011-10-27 15:19:26 -07003008 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00003009 const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003010 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003011 work_line_->SetResultRegisterType(this, return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003012 } else {
3013 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
3014 }
jeffhaobdb76512011-09-07 11:43:16 -07003015 just_set_result = true;
3016 }
3017 break;
jeffhaobdb76512011-09-07 11:43:16 -07003018 case Instruction::INVOKE_INTERFACE:
Ian Rogersd81871c2011-10-03 13:57:23 -07003019 case Instruction::INVOKE_INTERFACE_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003020 bool is_range = (inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE);
Alex Light7268d472016-01-20 15:50:01 -08003021 ArtMethod* abs_method = VerifyInvocationArgs(inst, METHOD_INTERFACE, is_range);
Ian Rogers7b078e82014-09-10 14:44:24 -07003022 if (abs_method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003023 mirror::Class* called_interface = abs_method->GetDeclaringClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07003024 if (!called_interface->IsInterface() && !called_interface->IsObjectClass()) {
3025 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected interface class in invoke-interface '"
David Sehr709b0702016-10-13 09:12:37 -07003026 << abs_method->PrettyMethod() << "'";
Ian Rogersad0b3a32012-04-16 14:50:24 -07003027 break;
Ian Rogers28ad40d2011-10-27 15:19:26 -07003028 }
Ian Rogers0d604842012-04-16 14:50:24 -07003029 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003030 /* Get the type of the "this" arg, which should either be a sub-interface of called
3031 * interface or Object (see comments in RegType::JoinClass).
3032 */
Orion Hodsoncfa325e2016-10-13 10:25:54 +01003033 const RegType& this_type = work_line_->GetInvocationThis(this, inst);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003034 if (this_type.IsZero()) {
3035 /* null pointer always passes (and always fails at runtime) */
3036 } else {
3037 if (this_type.IsUninitializedTypes()) {
3038 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface call on uninitialized object "
3039 << this_type;
3040 break;
3041 }
3042 // In the past we have tried to assert that "called_interface" is assignable
3043 // from "this_type.GetClass()", however, as we do an imprecise Join
3044 // (RegType::JoinClass) we don't have full information on what interfaces are
3045 // implemented by "this_type". For example, two classes may implement the same
3046 // interfaces and have a common parent that doesn't implement the interface. The
3047 // join will set "this_type" to the parent class and a test that this implements
3048 // the interface will incorrectly fail.
3049 }
3050 /*
3051 * We don't have an object instance, so we can't find the concrete method. However, all of
3052 * the type information is in the abstract method, so we're good.
3053 */
3054 const char* descriptor;
Ian Rogers7b078e82014-09-10 14:44:24 -07003055 if (abs_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003056 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07003057 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
Andreas Gampea5b09a62016-11-17 15:21:22 -08003058 dex::TypeIndex return_type_idx =
3059 dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Mathieu Chartierde40d472015-10-15 17:47:48 -07003060 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003061 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003062 descriptor = abs_method->GetReturnTypeDescriptor();
Ian Rogersad0b3a32012-04-16 14:50:24 -07003063 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00003064 const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003065 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003066 work_line_->SetResultRegisterType(this, return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003067 } else {
3068 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
3069 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003070 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07003071 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07003072 }
Narayan Kamath9823e782016-08-03 12:46:58 +01003073 case Instruction::INVOKE_POLYMORPHIC:
3074 case Instruction::INVOKE_POLYMORPHIC_RANGE: {
Orion Hodsoncfa325e2016-10-13 10:25:54 +01003075 bool is_range = (inst->Opcode() == Instruction::INVOKE_POLYMORPHIC_RANGE);
3076 ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_POLYMORPHIC, is_range);
3077 if (called_method == nullptr) {
3078 // Convert potential soft failures in VerifyInvocationArgs() to hard errors.
3079 if (failure_messages_.size() > 0) {
3080 std::string message = failure_messages_.back()->str();
3081 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << message;
3082 } else {
3083 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invoke-polymorphic verification failure.";
3084 }
3085 break;
3086 }
3087 if (!CheckSignaturePolymorphicMethod(called_method) ||
3088 !CheckSignaturePolymorphicReceiver(inst)) {
3089 break;
3090 }
3091 const uint32_t proto_idx = (is_range) ? inst->VRegH_4rcc() : inst->VRegH_45cc();
3092 const char* descriptor =
3093 dex_file_->GetReturnTypeDescriptor(dex_file_->GetProtoId(proto_idx));
3094 const RegType& return_type =
3095 reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
3096 if (!return_type.IsLowHalf()) {
3097 work_line_->SetResultRegisterType(this, return_type);
3098 } else {
3099 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
3100 }
3101 // TODO(oth): remove when compiler support is available.
Narayan Kamath9823e782016-08-03 12:46:58 +01003102 Fail(VERIFY_ERROR_FORCE_INTERPRETER)
Orion Hodsoncfa325e2016-10-13 10:25:54 +01003103 << "invoke-polymorphic is not supported by compiler";
Narayan Kamath9823e782016-08-03 12:46:58 +01003104 have_pending_experimental_failure_ = true;
Orion Hodsoncfa325e2016-10-13 10:25:54 +01003105 break;
Narayan Kamath9823e782016-08-03 12:46:58 +01003106 }
jeffhaobdb76512011-09-07 11:43:16 -07003107 case Instruction::NEG_INT:
3108 case Instruction::NOT_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003109 work_line_->CheckUnaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003110 break;
3111 case Instruction::NEG_LONG:
3112 case Instruction::NOT_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07003113 work_line_->CheckUnaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003114 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07003115 break;
3116 case Instruction::NEG_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003117 work_line_->CheckUnaryOp(this, inst, reg_types_.Float(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07003118 break;
3119 case Instruction::NEG_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07003120 work_line_->CheckUnaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003121 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07003122 break;
3123 case Instruction::INT_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07003124 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003125 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003126 break;
3127 case Instruction::INT_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003128 work_line_->CheckUnaryOp(this, inst, reg_types_.Float(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003129 break;
3130 case Instruction::INT_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07003131 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003132 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003133 break;
3134 case Instruction::LONG_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003135 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003136 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07003137 break;
3138 case Instruction::LONG_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003139 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003140 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07003141 break;
3142 case Instruction::LONG_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07003143 work_line_->CheckUnaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003144 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07003145 break;
3146 case Instruction::FLOAT_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003147 work_line_->CheckUnaryOp(this, inst, reg_types_.Integer(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07003148 break;
3149 case Instruction::FLOAT_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07003150 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003151 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07003152 break;
3153 case Instruction::FLOAT_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07003154 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003155 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07003156 break;
3157 case Instruction::DOUBLE_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003158 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003159 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07003160 break;
3161 case Instruction::DOUBLE_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07003162 work_line_->CheckUnaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003163 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07003164 break;
3165 case Instruction::DOUBLE_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003166 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003167 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07003168 break;
3169 case Instruction::INT_TO_BYTE:
Ian Rogers7b078e82014-09-10 14:44:24 -07003170 work_line_->CheckUnaryOp(this, inst, reg_types_.Byte(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003171 break;
3172 case Instruction::INT_TO_CHAR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003173 work_line_->CheckUnaryOp(this, inst, reg_types_.Char(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003174 break;
3175 case Instruction::INT_TO_SHORT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003176 work_line_->CheckUnaryOp(this, inst, reg_types_.Short(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003177 break;
3178
3179 case Instruction::ADD_INT:
3180 case Instruction::SUB_INT:
3181 case Instruction::MUL_INT:
3182 case Instruction::REM_INT:
3183 case Instruction::DIV_INT:
3184 case Instruction::SHL_INT:
3185 case Instruction::SHR_INT:
3186 case Instruction::USHR_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003187 work_line_->CheckBinaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003188 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07003189 break;
3190 case Instruction::AND_INT:
3191 case Instruction::OR_INT:
3192 case Instruction::XOR_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003193 work_line_->CheckBinaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003194 reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07003195 break;
3196 case Instruction::ADD_LONG:
3197 case Instruction::SUB_LONG:
3198 case Instruction::MUL_LONG:
3199 case Instruction::DIV_LONG:
3200 case Instruction::REM_LONG:
3201 case Instruction::AND_LONG:
3202 case Instruction::OR_LONG:
3203 case Instruction::XOR_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07003204 work_line_->CheckBinaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003205 reg_types_.LongLo(), reg_types_.LongHi(),
3206 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07003207 break;
3208 case Instruction::SHL_LONG:
3209 case Instruction::SHR_LONG:
3210 case Instruction::USHR_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07003211 /* shift distance is Int, making these different from other binary operations */
Ian Rogers7b078e82014-09-10 14:44:24 -07003212 work_line_->CheckBinaryOpWideShift(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003213 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003214 break;
3215 case Instruction::ADD_FLOAT:
3216 case Instruction::SUB_FLOAT:
3217 case Instruction::MUL_FLOAT:
3218 case Instruction::DIV_FLOAT:
3219 case Instruction::REM_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003220 work_line_->CheckBinaryOp(this, inst, reg_types_.Float(), reg_types_.Float(),
3221 reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07003222 break;
3223 case Instruction::ADD_DOUBLE:
3224 case Instruction::SUB_DOUBLE:
3225 case Instruction::MUL_DOUBLE:
3226 case Instruction::DIV_DOUBLE:
3227 case Instruction::REM_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07003228 work_line_->CheckBinaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003229 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
3230 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07003231 break;
3232 case Instruction::ADD_INT_2ADDR:
3233 case Instruction::SUB_INT_2ADDR:
3234 case Instruction::MUL_INT_2ADDR:
3235 case Instruction::REM_INT_2ADDR:
3236 case Instruction::SHL_INT_2ADDR:
3237 case Instruction::SHR_INT_2ADDR:
3238 case Instruction::USHR_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003239 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
3240 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07003241 break;
3242 case Instruction::AND_INT_2ADDR:
3243 case Instruction::OR_INT_2ADDR:
3244 case Instruction::XOR_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003245 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
3246 reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07003247 break;
3248 case Instruction::DIV_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003249 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
3250 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07003251 break;
3252 case Instruction::ADD_LONG_2ADDR:
3253 case Instruction::SUB_LONG_2ADDR:
3254 case Instruction::MUL_LONG_2ADDR:
3255 case Instruction::DIV_LONG_2ADDR:
3256 case Instruction::REM_LONG_2ADDR:
3257 case Instruction::AND_LONG_2ADDR:
3258 case Instruction::OR_LONG_2ADDR:
3259 case Instruction::XOR_LONG_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003260 work_line_->CheckBinaryOp2addrWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003261 reg_types_.LongLo(), reg_types_.LongHi(),
3262 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07003263 break;
3264 case Instruction::SHL_LONG_2ADDR:
3265 case Instruction::SHR_LONG_2ADDR:
3266 case Instruction::USHR_LONG_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003267 work_line_->CheckBinaryOp2addrWideShift(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003268 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003269 break;
3270 case Instruction::ADD_FLOAT_2ADDR:
3271 case Instruction::SUB_FLOAT_2ADDR:
3272 case Instruction::MUL_FLOAT_2ADDR:
3273 case Instruction::DIV_FLOAT_2ADDR:
3274 case Instruction::REM_FLOAT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003275 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Float(), reg_types_.Float(),
3276 reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07003277 break;
3278 case Instruction::ADD_DOUBLE_2ADDR:
3279 case Instruction::SUB_DOUBLE_2ADDR:
3280 case Instruction::MUL_DOUBLE_2ADDR:
3281 case Instruction::DIV_DOUBLE_2ADDR:
3282 case Instruction::REM_DOUBLE_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003283 work_line_->CheckBinaryOp2addrWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003284 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
3285 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07003286 break;
3287 case Instruction::ADD_INT_LIT16:
Ian Rogersf72a11d2014-10-30 15:41:08 -07003288 case Instruction::RSUB_INT_LIT16:
jeffhaobdb76512011-09-07 11:43:16 -07003289 case Instruction::MUL_INT_LIT16:
3290 case Instruction::DIV_INT_LIT16:
3291 case Instruction::REM_INT_LIT16:
Ian Rogers7b078e82014-09-10 14:44:24 -07003292 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), false,
3293 true);
jeffhaobdb76512011-09-07 11:43:16 -07003294 break;
3295 case Instruction::AND_INT_LIT16:
3296 case Instruction::OR_INT_LIT16:
3297 case Instruction::XOR_INT_LIT16:
Ian Rogers7b078e82014-09-10 14:44:24 -07003298 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), true,
3299 true);
jeffhaobdb76512011-09-07 11:43:16 -07003300 break;
3301 case Instruction::ADD_INT_LIT8:
3302 case Instruction::RSUB_INT_LIT8:
3303 case Instruction::MUL_INT_LIT8:
3304 case Instruction::DIV_INT_LIT8:
3305 case Instruction::REM_INT_LIT8:
3306 case Instruction::SHL_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07003307 case Instruction::SHR_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07003308 case Instruction::USHR_INT_LIT8:
Ian Rogers7b078e82014-09-10 14:44:24 -07003309 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), false,
3310 false);
jeffhaobdb76512011-09-07 11:43:16 -07003311 break;
3312 case Instruction::AND_INT_LIT8:
3313 case Instruction::OR_INT_LIT8:
3314 case Instruction::XOR_INT_LIT8:
Ian Rogers7b078e82014-09-10 14:44:24 -07003315 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), true,
3316 false);
jeffhaobdb76512011-09-07 11:43:16 -07003317 break;
3318
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003319 // Special instructions.
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07003320 case Instruction::RETURN_VOID_NO_BARRIER:
3321 if (IsConstructor() && !IsStatic()) {
3322 auto& declaring_class = GetDeclaringClass();
Andreas Gampe68df3202015-06-22 11:35:46 -07003323 if (declaring_class.IsUnresolvedReference()) {
3324 // We must iterate over the fields, even if we cannot use mirror classes to do so. Do it
3325 // manually over the underlying dex file.
3326 uint32_t first_index = GetFirstFinalInstanceFieldIndex(*dex_file_,
3327 dex_file_->GetMethodId(dex_method_idx_).class_idx_);
3328 if (first_index != DexFile::kDexNoIndex) {
3329 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void-no-barrier not expected for field "
3330 << first_index;
3331 }
3332 break;
3333 }
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07003334 auto* klass = declaring_class.GetClass();
3335 for (uint32_t i = 0, num_fields = klass->NumInstanceFields(); i < num_fields; ++i) {
3336 if (klass->GetInstanceField(i)->IsFinal()) {
Mathieu Chartiere86deef2015-03-19 13:43:37 -07003337 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void-no-barrier not expected for "
David Sehr709b0702016-10-13 09:12:37 -07003338 << klass->GetInstanceField(i)->PrettyField();
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07003339 break;
3340 }
3341 }
Sebastien Hertzcc10e0e2013-06-28 14:24:48 +02003342 }
Andreas Gampeb2917962015-07-31 13:36:10 -07003343 // Handle this like a RETURN_VOID now. Code is duplicated to separate standard from
3344 // quickened opcodes (otherwise this could be a fall-through).
3345 if (!IsConstructor()) {
3346 if (!GetMethodReturnType().IsConflict()) {
3347 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void not expected";
3348 }
3349 }
Sebastien Hertzcc10e0e2013-06-28 14:24:48 +02003350 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003351 // Note: the following instructions encode offsets derived from class linking.
Neil Fuller0e844392016-09-08 13:43:31 +01003352 // As such they use Class*/Field*/Executable* as these offsets only have
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003353 // meaning if the class linking and resolution were successful.
3354 case Instruction::IGET_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003355 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003356 break;
3357 case Instruction::IGET_WIDE_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003358 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003359 break;
3360 case Instruction::IGET_OBJECT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003361 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003362 break;
Mathieu Chartierffc605c2014-12-10 10:35:44 -08003363 case Instruction::IGET_BOOLEAN_QUICK:
3364 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true);
3365 break;
3366 case Instruction::IGET_BYTE_QUICK:
3367 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true);
3368 break;
3369 case Instruction::IGET_CHAR_QUICK:
3370 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true);
3371 break;
3372 case Instruction::IGET_SHORT_QUICK:
3373 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true);
3374 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003375 case Instruction::IPUT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003376 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003377 break;
Fred Shih37f05ef2014-07-16 18:38:08 -07003378 case Instruction::IPUT_BOOLEAN_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003379 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07003380 break;
3381 case Instruction::IPUT_BYTE_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003382 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07003383 break;
3384 case Instruction::IPUT_CHAR_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003385 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07003386 break;
3387 case Instruction::IPUT_SHORT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003388 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07003389 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003390 case Instruction::IPUT_WIDE_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003391 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003392 break;
3393 case Instruction::IPUT_OBJECT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003394 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003395 break;
3396 case Instruction::INVOKE_VIRTUAL_QUICK:
3397 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: {
3398 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
Mathieu Chartiere401d142015-04-22 13:56:20 -07003399 ArtMethod* called_method = VerifyInvokeVirtualQuickArgs(inst, is_range);
Ian Rogers7b078e82014-09-10 14:44:24 -07003400 if (called_method != nullptr) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003401 const char* descriptor = called_method->GetReturnTypeDescriptor();
Ian Rogersd8f69b02014-09-10 21:43:52 +00003402 const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003403 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003404 work_line_->SetResultRegisterType(this, return_type);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003405 } else {
3406 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
3407 }
3408 just_set_result = true;
3409 }
3410 break;
3411 }
3412
Ian Rogersd81871c2011-10-03 13:57:23 -07003413 /* These should never appear during verification. */
Mathieu Chartierffc605c2014-12-10 10:35:44 -08003414 case Instruction::UNUSED_3E ... Instruction::UNUSED_43:
Narayan Kamath8ec3bd22016-08-03 12:46:23 +01003415 case Instruction::UNUSED_F3 ... Instruction::UNUSED_F9:
3416 case Instruction::UNUSED_FC ... Instruction::UNUSED_FF:
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003417 case Instruction::UNUSED_79:
3418 case Instruction::UNUSED_7A:
jeffhaod5347e02012-03-22 17:25:05 -07003419 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Unexpected opcode " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07003420 break;
3421
3422 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003423 * DO NOT add a "default" clause here. Without it the compiler will
jeffhaobdb76512011-09-07 11:43:16 -07003424 * complain if an instruction is missing (which is desirable).
3425 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003426 } // end - switch (dec_insn.opcode)
jeffhaobdb76512011-09-07 11:43:16 -07003427
Ian Rogersad0b3a32012-04-16 14:50:24 -07003428 if (have_pending_hard_failure_) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08003429 if (Runtime::Current()->IsAotCompiler()) {
3430 /* When AOT compiling, check that the last failure is a hard failure */
Andreas Gampeb588f4c2015-05-26 13:35:39 -07003431 if (failures_[failures_.size() - 1] != VERIFY_ERROR_BAD_CLASS_HARD) {
3432 LOG(ERROR) << "Pending failures:";
3433 for (auto& error : failures_) {
3434 LOG(ERROR) << error;
3435 }
3436 for (auto& error_msg : failure_messages_) {
3437 LOG(ERROR) << error_msg->str();
3438 }
3439 LOG(FATAL) << "Pending hard failure, but last failure not hard.";
3440 }
Ian Rogerse1758fe2012-04-19 11:31:15 -07003441 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003442 /* immediate failure, reject class */
3443 info_messages_ << "Rejecting opcode " << inst->DumpString(dex_file_);
3444 return false;
jeffhaofaf459e2012-08-31 15:32:47 -07003445 } else if (have_pending_runtime_throw_failure_) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07003446 /* checking interpreter will throw, mark following code as unreachable */
jeffhaofaf459e2012-08-31 15:32:47 -07003447 opcode_flags = Instruction::kThrow;
Andreas Gampea727e372015-08-25 09:22:37 -07003448 // Note: the flag must be reset as it is only global to decouple Fail and is semantically per
3449 // instruction. However, RETURN checking may throw LOCKING errors, so we clear at the
3450 // very end.
jeffhaobdb76512011-09-07 11:43:16 -07003451 }
jeffhaobdb76512011-09-07 11:43:16 -07003452 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003453 * If we didn't just set the result register, clear it out. This ensures that you can only use
3454 * "move-result" immediately after the result is set. (We could check this statically, but it's
3455 * not expensive and it makes our debugging output cleaner.)
jeffhaobdb76512011-09-07 11:43:16 -07003456 */
3457 if (!just_set_result) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003458 work_line_->SetResultTypeToUnknown(this);
jeffhaobdb76512011-09-07 11:43:16 -07003459 }
3460
jeffhaobdb76512011-09-07 11:43:16 -07003461 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003462 * Handle "branch". Tag the branch target.
jeffhaobdb76512011-09-07 11:43:16 -07003463 *
3464 * NOTE: instructions like Instruction::EQZ provide information about the
jeffhaod1f0fde2011-09-08 17:25:33 -07003465 * state of the register when the branch is taken or not taken. For example,
jeffhaobdb76512011-09-07 11:43:16 -07003466 * somebody could get a reference field, check it for zero, and if the
3467 * branch is taken immediately store that register in a boolean field
jeffhaod1f0fde2011-09-08 17:25:33 -07003468 * since the value is known to be zero. We do not currently account for
jeffhaobdb76512011-09-07 11:43:16 -07003469 * that, and will reject the code.
3470 *
3471 * TODO: avoid re-fetching the branch target
3472 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003473 if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07003474 bool isConditional, selfOkay;
Ian Rogersd81871c2011-10-03 13:57:23 -07003475 if (!GetBranchOffset(work_insn_idx_, &branch_target, &isConditional, &selfOkay)) {
jeffhaobdb76512011-09-07 11:43:16 -07003476 /* should never happen after static verification */
jeffhaod5347e02012-03-22 17:25:05 -07003477 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad branch";
jeffhaobdb76512011-09-07 11:43:16 -07003478 return false;
3479 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08003480 DCHECK_EQ(isConditional, (opcode_flags & Instruction::kContinue) != 0);
Stephen Kyle9bc61992014-09-22 13:53:15 +01003481 if (!CheckNotMoveExceptionOrMoveResult(code_item_->insns_, work_insn_idx_ + branch_target)) {
jeffhaobdb76512011-09-07 11:43:16 -07003482 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07003483 }
jeffhaobdb76512011-09-07 11:43:16 -07003484 /* update branch target, set "changed" if appropriate */
Mathieu Chartierde40d472015-10-15 17:47:48 -07003485 if (nullptr != branch_line) {
Ian Rogersebbdd872014-07-07 23:53:08 -07003486 if (!UpdateRegisters(work_insn_idx_ + branch_target, branch_line.get(), false)) {
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003487 return false;
3488 }
3489 } else {
Ian Rogersebbdd872014-07-07 23:53:08 -07003490 if (!UpdateRegisters(work_insn_idx_ + branch_target, work_line_.get(), false)) {
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003491 return false;
3492 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003493 }
jeffhaobdb76512011-09-07 11:43:16 -07003494 }
3495
3496 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003497 * Handle "switch". Tag all possible branch targets.
jeffhaobdb76512011-09-07 11:43:16 -07003498 *
3499 * We've already verified that the table is structurally sound, so we
3500 * just need to walk through and tag the targets.
3501 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003502 if ((opcode_flags & Instruction::kSwitch) != 0) {
Andreas Gampe53de99c2015-08-17 13:43:55 -07003503 int offset_to_switch = insns[1] | (static_cast<int32_t>(insns[2]) << 16);
jeffhaobdb76512011-09-07 11:43:16 -07003504 const uint16_t* switch_insns = insns + offset_to_switch;
3505 int switch_count = switch_insns[1];
3506 int offset_to_targets, targ;
3507
3508 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
3509 /* 0 = sig, 1 = count, 2/3 = first key */
3510 offset_to_targets = 4;
3511 } else {
3512 /* 0 = sig, 1 = count, 2..count * 2 = keys */
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07003513 DCHECK((*insns & 0xff) == Instruction::SPARSE_SWITCH);
jeffhaobdb76512011-09-07 11:43:16 -07003514 offset_to_targets = 2 + 2 * switch_count;
3515 }
3516
3517 /* verify each switch target */
3518 for (targ = 0; targ < switch_count; targ++) {
3519 int offset;
3520 uint32_t abs_offset;
3521
3522 /* offsets are 32-bit, and only partly endian-swapped */
3523 offset = switch_insns[offset_to_targets + targ * 2] |
Andreas Gampe53de99c2015-08-17 13:43:55 -07003524 (static_cast<int32_t>(switch_insns[offset_to_targets + targ * 2 + 1]) << 16);
Ian Rogersd81871c2011-10-03 13:57:23 -07003525 abs_offset = work_insn_idx_ + offset;
3526 DCHECK_LT(abs_offset, code_item_->insns_size_in_code_units_);
Stephen Kyle9bc61992014-09-22 13:53:15 +01003527 if (!CheckNotMoveExceptionOrMoveResult(code_item_->insns_, abs_offset)) {
jeffhaobdb76512011-09-07 11:43:16 -07003528 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07003529 }
Ian Rogersebbdd872014-07-07 23:53:08 -07003530 if (!UpdateRegisters(abs_offset, work_line_.get(), false)) {
jeffhaobdb76512011-09-07 11:43:16 -07003531 return false;
Ian Rogersebbdd872014-07-07 23:53:08 -07003532 }
jeffhaobdb76512011-09-07 11:43:16 -07003533 }
3534 }
3535
3536 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003537 * Handle instructions that can throw and that are sitting in a "try" block. (If they're not in a
3538 * "try" block when they throw, control transfers out of the method.)
jeffhaobdb76512011-09-07 11:43:16 -07003539 */
Mathieu Chartierde40d472015-10-15 17:47:48 -07003540 if ((opcode_flags & Instruction::kThrow) != 0 && GetInstructionFlags(work_insn_idx_).IsInTry()) {
Andreas Gampef91baf12014-07-18 15:41:00 -07003541 bool has_catch_all_handler = false;
Ian Rogers0571d352011-11-03 19:51:38 -07003542 CatchHandlerIterator iterator(*code_item_, work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07003543
Andreas Gampef91baf12014-07-18 15:41:00 -07003544 // Need the linker to try and resolve the handled class to check if it's Throwable.
3545 ClassLinker* linker = Runtime::Current()->GetClassLinker();
3546
Ian Rogers0571d352011-11-03 19:51:38 -07003547 for (; iterator.HasNext(); iterator.Next()) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08003548 dex::TypeIndex handler_type_idx = iterator.GetHandlerTypeIndex();
3549 if (!handler_type_idx.IsValid()) {
Andreas Gampef91baf12014-07-18 15:41:00 -07003550 has_catch_all_handler = true;
3551 } else {
3552 // It is also a catch-all if it is java.lang.Throwable.
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003553 mirror::Class* klass = linker->ResolveType(*dex_file_, handler_type_idx, dex_cache_,
3554 class_loader_);
Andreas Gampef91baf12014-07-18 15:41:00 -07003555 if (klass != nullptr) {
3556 if (klass == mirror::Throwable::GetJavaLangThrowable()) {
3557 has_catch_all_handler = true;
3558 }
3559 } else {
3560 // Clear exception.
Ian Rogers7b078e82014-09-10 14:44:24 -07003561 DCHECK(self_->IsExceptionPending());
3562 self_->ClearException();
Andreas Gampef91baf12014-07-18 15:41:00 -07003563 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003564 }
jeffhaobdb76512011-09-07 11:43:16 -07003565 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003566 * Merge registers into the "catch" block. We want to use the "savedRegs" rather than
3567 * "work_regs", because at runtime the exception will be thrown before the instruction
3568 * modifies any registers.
jeffhaobdb76512011-09-07 11:43:16 -07003569 */
Ian Rogersebbdd872014-07-07 23:53:08 -07003570 if (!UpdateRegisters(iterator.GetHandlerAddress(), saved_line_.get(), false)) {
jeffhaobdb76512011-09-07 11:43:16 -07003571 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07003572 }
jeffhaobdb76512011-09-07 11:43:16 -07003573 }
3574
3575 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003576 * If the monitor stack depth is nonzero, there must be a "catch all" handler for this
3577 * instruction. This does apply to monitor-exit because of async exception handling.
jeffhaobdb76512011-09-07 11:43:16 -07003578 */
Andreas Gampef91baf12014-07-18 15:41:00 -07003579 if (work_line_->MonitorStackDepth() > 0 && !has_catch_all_handler) {
jeffhaobdb76512011-09-07 11:43:16 -07003580 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003581 * The state in work_line reflects the post-execution state. If the current instruction is a
3582 * monitor-enter and the monitor stack was empty, we don't need a catch-all (if it throws,
jeffhaobdb76512011-09-07 11:43:16 -07003583 * it will do so before grabbing the lock).
3584 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02003585 if (inst->Opcode() != Instruction::MONITOR_ENTER || work_line_->MonitorStackDepth() != 1) {
jeffhaod5347e02012-03-22 17:25:05 -07003586 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
Ian Rogersd81871c2011-10-03 13:57:23 -07003587 << "expected to be within a catch-all for an instruction where a monitor is held";
jeffhaobdb76512011-09-07 11:43:16 -07003588 return false;
3589 }
3590 }
3591 }
3592
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003593 /* Handle "continue". Tag the next consecutive instruction.
3594 * Note: Keep the code handling "continue" case below the "branch" and "switch" cases,
3595 * because it changes work_line_ when performing peephole optimization
3596 * and this change should not be used in those cases.
3597 */
Ian Rogers6d376ae2013-07-23 15:12:40 -07003598 if ((opcode_flags & Instruction::kContinue) != 0) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003599 DCHECK_EQ(Instruction::At(code_item_->insns_ + work_insn_idx_), inst);
3600 uint32_t next_insn_idx = work_insn_idx_ + inst->SizeInCodeUnits();
Ian Rogers6d376ae2013-07-23 15:12:40 -07003601 if (next_insn_idx >= code_item_->insns_size_in_code_units_) {
3602 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Execution can walk off end of code area";
3603 return false;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003604 }
Ian Rogers6d376ae2013-07-23 15:12:40 -07003605 // The only way to get to a move-exception instruction is to get thrown there. Make sure the
3606 // next instruction isn't one.
3607 if (!CheckNotMoveException(code_item_->insns_, next_insn_idx)) {
3608 return false;
3609 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07003610 if (nullptr != fallthrough_line) {
Ian Rogers6d376ae2013-07-23 15:12:40 -07003611 // Make workline consistent with fallthrough computed from peephole optimization.
3612 work_line_->CopyFromLine(fallthrough_line.get());
3613 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07003614 if (GetInstructionFlags(next_insn_idx).IsReturn()) {
Ian Rogersb8c78592013-07-25 23:52:52 +00003615 // For returns we only care about the operand to the return, all other registers are dead.
3616 const Instruction* ret_inst = Instruction::At(code_item_->insns_ + next_insn_idx);
Andreas Gampea727e372015-08-25 09:22:37 -07003617 AdjustReturnLine(this, ret_inst, work_line_.get());
Ian Rogersb8c78592013-07-25 23:52:52 +00003618 }
Ian Rogers6d376ae2013-07-23 15:12:40 -07003619 RegisterLine* next_line = reg_table_.GetLine(next_insn_idx);
Ian Rogers7b078e82014-09-10 14:44:24 -07003620 if (next_line != nullptr) {
Ian Rogersebbdd872014-07-07 23:53:08 -07003621 // Merge registers into what we have for the next instruction, and set the "changed" flag if
3622 // needed. If the merge changes the state of the registers then the work line will be
3623 // updated.
3624 if (!UpdateRegisters(next_insn_idx, work_line_.get(), true)) {
Ian Rogers6d376ae2013-07-23 15:12:40 -07003625 return false;
3626 }
3627 } else {
3628 /*
3629 * We're not recording register data for the next instruction, so we don't know what the
3630 * prior state was. We have to assume that something has changed and re-evaluate it.
3631 */
Mathieu Chartierde40d472015-10-15 17:47:48 -07003632 GetInstructionFlags(next_insn_idx).SetChanged();
Ian Rogers6d376ae2013-07-23 15:12:40 -07003633 }
3634 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003635
jeffhaod1f0fde2011-09-08 17:25:33 -07003636 /* If we're returning from the method, make sure monitor stack is empty. */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003637 if ((opcode_flags & Instruction::kReturn) != 0) {
Andreas Gampea727e372015-08-25 09:22:37 -07003638 work_line_->VerifyMonitorStackEmpty(this);
jeffhaobdb76512011-09-07 11:43:16 -07003639 }
3640
3641 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003642 * Update start_guess. Advance to the next instruction of that's
3643 * possible, otherwise use the branch target if one was found. If
jeffhaobdb76512011-09-07 11:43:16 -07003644 * neither of those exists we're in a return or throw; leave start_guess
3645 * alone and let the caller sort it out.
3646 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003647 if ((opcode_flags & Instruction::kContinue) != 0) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003648 DCHECK_EQ(Instruction::At(code_item_->insns_ + work_insn_idx_), inst);
3649 *start_guess = work_insn_idx_ + inst->SizeInCodeUnits();
Elliott Hughesadb8c672012-03-06 16:49:32 -08003650 } else if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07003651 /* we're still okay if branch_target is zero */
Ian Rogersd81871c2011-10-03 13:57:23 -07003652 *start_guess = work_insn_idx_ + branch_target;
jeffhaobdb76512011-09-07 11:43:16 -07003653 }
3654
Ian Rogersd81871c2011-10-03 13:57:23 -07003655 DCHECK_LT(*start_guess, code_item_->insns_size_in_code_units_);
Mathieu Chartierde40d472015-10-15 17:47:48 -07003656 DCHECK(GetInstructionFlags(*start_guess).IsOpcode());
jeffhaobdb76512011-09-07 11:43:16 -07003657
Andreas Gampea727e372015-08-25 09:22:37 -07003658 if (have_pending_runtime_throw_failure_) {
3659 have_any_pending_runtime_throw_failure_ = true;
3660 // Reset the pending_runtime_throw flag now.
3661 have_pending_runtime_throw_failure_ = false;
3662 }
3663
jeffhaobdb76512011-09-07 11:43:16 -07003664 return true;
Brian Carlstrom7934ac22013-07-26 10:54:15 -07003665} // NOLINT(readability/fn_size)
jeffhaobdb76512011-09-07 11:43:16 -07003666
Mathieu Chartierde40d472015-10-15 17:47:48 -07003667void MethodVerifier::UninstantiableError(const char* descriptor) {
3668 Fail(VerifyError::VERIFY_ERROR_NO_CLASS) << "Could not create precise reference for "
3669 << "non-instantiable klass " << descriptor;
3670}
3671
3672inline bool MethodVerifier::IsInstantiableOrPrimitive(mirror::Class* klass) {
3673 return klass->IsInstantiable() || klass->IsPrimitive();
3674}
3675
Andreas Gampea5b09a62016-11-17 15:21:22 -08003676const RegType& MethodVerifier::ResolveClassAndCheckAccess(dex::TypeIndex class_idx) {
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003677 mirror::Class* klass = dex_cache_->GetResolvedType(class_idx);
Mathieu Chartierde40d472015-10-15 17:47:48 -07003678 const RegType* result = nullptr;
3679 if (klass != nullptr) {
3680 bool precise = klass->CannotBeAssignedFromOtherTypes();
3681 if (precise && !IsInstantiableOrPrimitive(klass)) {
3682 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
3683 UninstantiableError(descriptor);
3684 precise = false;
3685 }
3686 result = reg_types_.FindClass(klass, precise);
3687 if (result == nullptr) {
3688 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
3689 result = reg_types_.InsertClass(descriptor, klass, precise);
3690 }
3691 } else {
3692 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
3693 result = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003694 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07003695 DCHECK(result != nullptr);
3696 if (result->IsConflict()) {
3697 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
3698 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "accessing broken descriptor '" << descriptor
3699 << "' in " << GetDeclaringClass();
3700 return *result;
3701 }
3702 if (klass == nullptr && !result->IsUnresolvedTypes()) {
David Brazdilca3c8c32016-09-06 14:04:48 +01003703 klass = result->GetClass();
3704 dex_cache_->SetResolvedType(class_idx, klass);
Ian Rogerse1758fe2012-04-19 11:31:15 -07003705 }
David Brazdilca3c8c32016-09-06 14:04:48 +01003706
3707 // Record result of class resolution attempt.
3708 VerifierDeps::MaybeRecordClassResolution(*dex_file_, class_idx, klass);
3709
Ian Rogersad0b3a32012-04-16 14:50:24 -07003710 // Check if access is allowed. Unresolved types use xxxWithAccessCheck to
Jeff Haob24b4a72013-07-31 13:47:31 -07003711 // check at runtime if access is allowed and so pass here. If result is
3712 // primitive, skip the access check.
Mathieu Chartierde40d472015-10-15 17:47:48 -07003713 if (result->IsNonZeroReferenceTypes() && !result->IsUnresolvedTypes()) {
3714 const RegType& referrer = GetDeclaringClass();
3715 if (!referrer.IsUnresolvedTypes() && !referrer.CanAccess(*result)) {
3716 Fail(VERIFY_ERROR_ACCESS_CLASS) << "illegal class access: '"
3717 << referrer << "' -> '" << result << "'";
3718 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07003719 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07003720 return *result;
Ian Rogersd81871c2011-10-03 13:57:23 -07003721}
3722
Ian Rogersd8f69b02014-09-10 21:43:52 +00003723const RegType& MethodVerifier::GetCaughtExceptionType() {
Ian Rogers7b078e82014-09-10 14:44:24 -07003724 const RegType* common_super = nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003725 if (code_item_->tries_size_ != 0) {
Ian Rogers13735952014-10-08 12:43:28 -07003726 const uint8_t* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
Ian Rogersd81871c2011-10-03 13:57:23 -07003727 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
3728 for (uint32_t i = 0; i < handlers_size; i++) {
Ian Rogers0571d352011-11-03 19:51:38 -07003729 CatchHandlerIterator iterator(handlers_ptr);
3730 for (; iterator.HasNext(); iterator.Next()) {
3731 if (iterator.GetHandlerAddress() == (uint32_t) work_insn_idx_) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08003732 if (!iterator.GetHandlerTypeIndex().IsValid()) {
Ian Rogersb4903572012-10-11 11:52:56 -07003733 common_super = &reg_types_.JavaLangThrowable(false);
Ian Rogersd81871c2011-10-03 13:57:23 -07003734 } else {
Ian Rogersd8f69b02014-09-10 21:43:52 +00003735 const RegType& exception = ResolveClassAndCheckAccess(iterator.GetHandlerTypeIndex());
David Brazdilca3c8c32016-09-06 14:04:48 +01003736 if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(exception, this)) {
David Brazdil68b5c0b2016-01-19 14:25:29 +00003737 DCHECK(!exception.IsUninitializedTypes()); // Comes from dex, shouldn't be uninit.
Jeff Haoc26a56c2013-11-04 12:00:47 -08003738 if (exception.IsUnresolvedTypes()) {
3739 // We don't know enough about the type. Fail here and let runtime handle it.
3740 Fail(VERIFY_ERROR_NO_CLASS) << "unresolved exception class " << exception;
3741 return exception;
3742 } else {
3743 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unexpected non-exception class " << exception;
3744 return reg_types_.Conflict();
3745 }
Jeff Haob878f212014-04-24 16:25:36 -07003746 } else if (common_super == nullptr) {
3747 common_super = &exception;
Ian Rogers28ad40d2011-10-27 15:19:26 -07003748 } else if (common_super->Equals(exception)) {
Ian Rogersc4762272012-02-01 15:55:55 -08003749 // odd case, but nothing to do
Ian Rogersd81871c2011-10-03 13:57:23 -07003750 } else {
David Brazdilca3c8c32016-09-06 14:04:48 +01003751 common_super = &common_super->Merge(exception, &reg_types_, this);
Andreas Gampe7c038102014-10-27 20:08:46 -07003752 if (FailOrAbort(this,
David Brazdilca3c8c32016-09-06 14:04:48 +01003753 reg_types_.JavaLangThrowable(false).IsAssignableFrom(
3754 *common_super, this),
Andreas Gampe7c038102014-10-27 20:08:46 -07003755 "java.lang.Throwable is not assignable-from common_super at ",
3756 work_insn_idx_)) {
3757 break;
3758 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003759 }
3760 }
3761 }
3762 }
Ian Rogers0571d352011-11-03 19:51:38 -07003763 handlers_ptr = iterator.EndDataPointer();
Ian Rogersd81871c2011-10-03 13:57:23 -07003764 }
3765 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003766 if (common_super == nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003767 /* no catch blocks, or no catches with classes we can find */
jeffhaod5347e02012-03-22 17:25:05 -07003768 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unable to find exception handler";
Ian Rogersad0b3a32012-04-16 14:50:24 -07003769 return reg_types_.Conflict();
Ian Rogersd81871c2011-10-03 13:57:23 -07003770 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07003771 return *common_super;
Ian Rogersd81871c2011-10-03 13:57:23 -07003772}
3773
David Brazdilca3c8c32016-09-06 14:04:48 +01003774inline static MethodResolutionKind GetMethodResolutionKind(
3775 MethodType method_type, bool is_interface) {
3776 if (method_type == METHOD_DIRECT || method_type == METHOD_STATIC) {
3777 return kDirectMethodResolution;
3778 } else if (method_type == METHOD_INTERFACE) {
3779 return kInterfaceMethodResolution;
3780 } else if (method_type == METHOD_SUPER && is_interface) {
3781 return kInterfaceMethodResolution;
3782 } else {
Orion Hodsoncfa325e2016-10-13 10:25:54 +01003783 DCHECK(method_type == METHOD_VIRTUAL || method_type == METHOD_SUPER
3784 || method_type == METHOD_POLYMORPHIC);
David Brazdilca3c8c32016-09-06 14:04:48 +01003785 return kVirtualMethodResolution;
3786 }
3787}
3788
Mathieu Chartiere401d142015-04-22 13:56:20 -07003789ArtMethod* MethodVerifier::ResolveMethodAndCheckAccess(
Alex Light7268d472016-01-20 15:50:01 -08003790 uint32_t dex_method_idx, MethodType method_type) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003791 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx);
Ian Rogersd8f69b02014-09-10 21:43:52 +00003792 const RegType& klass_type = ResolveClassAndCheckAccess(method_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003793 if (klass_type.IsConflict()) {
3794 std::string append(" in attempt to access method ");
3795 append += dex_file_->GetMethodName(method_id);
3796 AppendToLastFailMessage(append);
Ian Rogers7b078e82014-09-10 14:44:24 -07003797 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08003798 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003799 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003800 return nullptr; // Can't resolve Class so no more to do here
Ian Rogers90040192011-12-16 08:54:29 -08003801 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003802 mirror::Class* klass = klass_type.GetClass();
Ian Rogersd8f69b02014-09-10 21:43:52 +00003803 const RegType& referrer = GetDeclaringClass();
Mathieu Chartiere401d142015-04-22 13:56:20 -07003804 auto* cl = Runtime::Current()->GetClassLinker();
3805 auto pointer_size = cl->GetImagePointerSize();
David Brazdilca3c8c32016-09-06 14:04:48 +01003806 MethodResolutionKind res_kind = GetMethodResolutionKind(method_type, klass->IsInterface());
Andreas Gampe42ef8ab2015-12-03 17:27:32 -08003807
Mathieu Chartiere401d142015-04-22 13:56:20 -07003808 ArtMethod* res_method = dex_cache_->GetResolvedMethod(dex_method_idx, pointer_size);
Andreas Gampe42ef8ab2015-12-03 17:27:32 -08003809 bool stash_method = false;
Ian Rogers7b078e82014-09-10 14:44:24 -07003810 if (res_method == nullptr) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07003811 const char* name = dex_file_->GetMethodName(method_id);
Ian Rogersd91d6d62013-09-25 20:26:14 -07003812 const Signature signature = dex_file_->GetMethodSignature(method_id);
jeffhao8cd6dda2012-02-22 10:15:34 -08003813
David Brazdilca3c8c32016-09-06 14:04:48 +01003814 if (res_kind == kDirectMethodResolution) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003815 res_method = klass->FindDirectMethod(name, signature, pointer_size);
David Brazdilca3c8c32016-09-06 14:04:48 +01003816 } else if (res_kind == kVirtualMethodResolution) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003817 res_method = klass->FindVirtualMethod(name, signature, pointer_size);
David Brazdilca3c8c32016-09-06 14:04:48 +01003818 } else {
3819 DCHECK_EQ(res_kind, kInterfaceMethodResolution);
3820 res_method = klass->FindInterfaceMethod(name, signature, pointer_size);
Ian Rogersd81871c2011-10-03 13:57:23 -07003821 }
David Brazdilca3c8c32016-09-06 14:04:48 +01003822
Ian Rogers7b078e82014-09-10 14:44:24 -07003823 if (res_method != nullptr) {
Andreas Gampe42ef8ab2015-12-03 17:27:32 -08003824 stash_method = true;
Ian Rogersd81871c2011-10-03 13:57:23 -07003825 } else {
jeffhao8cd6dda2012-02-22 10:15:34 -08003826 // If a virtual or interface method wasn't found with the expected type, look in
3827 // the direct methods. This can happen when the wrong invoke type is used or when
3828 // a class has changed, and will be flagged as an error in later checks.
David Brazdilca3c8c32016-09-06 14:04:48 +01003829 // Note that in this case, we do not put the resolved method in the Dex cache
3830 // because it was not discovered using the expected type of method resolution.
3831 if (res_kind != kDirectMethodResolution) {
3832 // Record result of the initial resolution attempt.
3833 VerifierDeps::MaybeRecordMethodResolution(*dex_file_, dex_method_idx, res_kind, nullptr);
3834 // Change resolution type to 'direct' and try to resolve again.
3835 res_kind = kDirectMethodResolution;
Mathieu Chartiere401d142015-04-22 13:56:20 -07003836 res_method = klass->FindDirectMethod(name, signature, pointer_size);
jeffhao8cd6dda2012-02-22 10:15:34 -08003837 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003838 }
3839 }
David Brazdilca3c8c32016-09-06 14:04:48 +01003840
3841 // Record result of method resolution attempt.
3842 VerifierDeps::MaybeRecordMethodResolution(*dex_file_, dex_method_idx, res_kind, res_method);
3843
3844 if (res_method == nullptr) {
3845 Fail(VERIFY_ERROR_NO_METHOD) << "couldn't find method "
David Sehr709b0702016-10-13 09:12:37 -07003846 << klass->PrettyDescriptor() << "."
David Brazdilca3c8c32016-09-06 14:04:48 +01003847 << dex_file_->GetMethodName(method_id) << " "
3848 << dex_file_->GetMethodSignature(method_id);
3849 return nullptr;
3850 }
3851
Ian Rogersd81871c2011-10-03 13:57:23 -07003852 // Make sure calls to constructors are "direct". There are additional restrictions but we don't
3853 // enforce them here.
3854 if (res_method->IsConstructor() && method_type != METHOD_DIRECT) {
jeffhaod5347e02012-03-22 17:25:05 -07003855 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting non-direct call to constructor "
David Sehr709b0702016-10-13 09:12:37 -07003856 << res_method->PrettyMethod();
Ian Rogers7b078e82014-09-10 14:44:24 -07003857 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003858 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003859 // Disallow any calls to class initializers.
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003860 if (res_method->IsClassInitializer()) {
jeffhaod5347e02012-03-22 17:25:05 -07003861 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting call to class initializer "
David Sehr709b0702016-10-13 09:12:37 -07003862 << res_method->PrettyMethod();
Ian Rogers7b078e82014-09-10 14:44:24 -07003863 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003864 }
Andreas Gampe42ef8ab2015-12-03 17:27:32 -08003865
3866 // Check that interface methods are static or match interface classes.
3867 // We only allow statics if we don't have default methods enabled.
3868 //
3869 // Note: this check must be after the initializer check, as those are required to fail a class,
3870 // while this check implies an IncompatibleClassChangeError.
3871 if (klass->IsInterface()) {
Alex Lightb55f1ac2016-04-12 15:50:55 -07003872 // methods called on interfaces should be invoke-interface, invoke-super, invoke-direct (if
3873 // dex file version is 37 or greater), or invoke-static.
Andreas Gampe42ef8ab2015-12-03 17:27:32 -08003874 if (method_type != METHOD_INTERFACE &&
Neil Fuller9724c632016-01-07 15:42:47 +00003875 method_type != METHOD_STATIC &&
Alex Lightb55f1ac2016-04-12 15:50:55 -07003876 ((dex_file_->GetVersion() < DexFile::kDefaultMethodsVersion) ||
3877 method_type != METHOD_DIRECT) &&
Neil Fuller9724c632016-01-07 15:42:47 +00003878 method_type != METHOD_SUPER) {
Andreas Gampe42ef8ab2015-12-03 17:27:32 -08003879 Fail(VERIFY_ERROR_CLASS_CHANGE)
David Sehr709b0702016-10-13 09:12:37 -07003880 << "non-interface method " << dex_file_->PrettyMethod(dex_method_idx)
3881 << " is in an interface class " << klass->PrettyClass();
Andreas Gampe42ef8ab2015-12-03 17:27:32 -08003882 return nullptr;
3883 }
3884 } else {
3885 if (method_type == METHOD_INTERFACE) {
3886 Fail(VERIFY_ERROR_CLASS_CHANGE)
David Sehr709b0702016-10-13 09:12:37 -07003887 << "interface method " << dex_file_->PrettyMethod(dex_method_idx)
3888 << " is in a non-interface class " << klass->PrettyClass();
Andreas Gampe42ef8ab2015-12-03 17:27:32 -08003889 return nullptr;
3890 }
3891 }
3892
3893 // Only stash after the above passed. Otherwise the method wasn't guaranteed to be correct.
3894 if (stash_method) {
3895 dex_cache_->SetResolvedMethod(dex_method_idx, res_method, pointer_size);
3896 }
3897
jeffhao8cd6dda2012-02-22 10:15:34 -08003898 // Check if access is allowed.
Ian Rogersad0b3a32012-04-16 14:50:24 -07003899 if (!referrer.CanAccessMember(res_method->GetDeclaringClass(), res_method->GetAccessFlags())) {
David Sehr709b0702016-10-13 09:12:37 -07003900 Fail(VERIFY_ERROR_ACCESS_METHOD) << "illegal method access (call "
3901 << res_method->PrettyMethod()
Ian Rogersad0b3a32012-04-16 14:50:24 -07003902 << " from " << referrer << ")";
jeffhaob57e9522012-04-26 18:08:21 -07003903 return res_method;
jeffhao8cd6dda2012-02-22 10:15:34 -08003904 }
jeffhaode0d9c92012-02-27 13:58:13 -08003905 // Check that invoke-virtual and invoke-super are not used on private methods of the same class.
Alex Light7268d472016-01-20 15:50:01 -08003906 if (res_method->IsPrivate() && (method_type == METHOD_VIRTUAL || method_type == METHOD_SUPER)) {
jeffhaod5347e02012-03-22 17:25:05 -07003907 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invoke-super/virtual can't be used on private method "
David Sehr709b0702016-10-13 09:12:37 -07003908 << res_method->PrettyMethod();
Ian Rogers7b078e82014-09-10 14:44:24 -07003909 return nullptr;
jeffhaode0d9c92012-02-27 13:58:13 -08003910 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003911 // See if the method type implied by the invoke instruction matches the access flags for the
Orion Hodsoncfa325e2016-10-13 10:25:54 +01003912 // target method. The flags for METHOD_POLYMORPHIC are based on there being precisely two
3913 // signature polymorphic methods supported by the run-time which are native methods with variable
3914 // arguments.
Brian Carlstrombe6fa5e2014-12-09 20:15:42 -08003915 if ((method_type == METHOD_DIRECT && (!res_method->IsDirect() || res_method->IsStatic())) ||
Ian Rogersd81871c2011-10-03 13:57:23 -07003916 (method_type == METHOD_STATIC && !res_method->IsStatic()) ||
Alex Light7268d472016-01-20 15:50:01 -08003917 ((method_type == METHOD_SUPER ||
3918 method_type == METHOD_VIRTUAL ||
Orion Hodsoncfa325e2016-10-13 10:25:54 +01003919 method_type == METHOD_INTERFACE) && res_method->IsDirect()) ||
3920 ((method_type == METHOD_POLYMORPHIC) &&
3921 (!res_method->IsNative() || !res_method->IsVarargs()))) {
Ian Rogers2fc14272012-08-30 10:56:57 -07003922 Fail(VERIFY_ERROR_CLASS_CHANGE) << "invoke type (" << method_type << ") does not match method "
Orion Hodsoncfa325e2016-10-13 10:25:54 +01003923 "type of " << res_method->PrettyMethod();
Ian Rogers7b078e82014-09-10 14:44:24 -07003924 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003925 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003926 return res_method;
3927}
3928
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003929template <class T>
Mathieu Chartiere401d142015-04-22 13:56:20 -07003930ArtMethod* MethodVerifier::VerifyInvocationArgsFromIterator(
3931 T* it, const Instruction* inst, MethodType method_type, bool is_range, ArtMethod* res_method) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003932 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
3933 // match the call to the signature. Also, we might be calling through an abstract method
3934 // definition (which doesn't have register count values).
Orion Hodsoncfa325e2016-10-13 10:25:54 +01003935 const size_t expected_args = inst->VRegA();
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003936 /* caught by static verifier */
3937 DCHECK(is_range || expected_args <= 5);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003938
Orion Hodson1aab04d2016-12-08 10:29:53 +00003939 if (expected_args > code_item_->outs_size_) {
3940 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
3941 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
3942 return nullptr;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003943 }
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003944
3945 /*
3946 * Check the "this" argument, which must be an instance of the class that declared the method.
3947 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
3948 * rigorous check here (which is okay since we have to do it at runtime).
3949 */
3950 if (method_type != METHOD_STATIC) {
Orion Hodsoncfa325e2016-10-13 10:25:54 +01003951 const RegType& actual_arg_type = work_line_->GetInvocationThis(this, inst);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003952 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
3953 CHECK(have_pending_hard_failure_);
3954 return nullptr;
3955 }
David Brazdil68b5c0b2016-01-19 14:25:29 +00003956 bool is_init = false;
3957 if (actual_arg_type.IsUninitializedTypes()) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003958 if (res_method) {
3959 if (!res_method->IsConstructor()) {
3960 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
3961 return nullptr;
3962 }
3963 } else {
3964 // Check whether the name of the called method is "<init>"
3965 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Jeff Hao0d087272014-08-04 14:47:17 -07003966 if (strcmp(dex_file_->GetMethodName(dex_file_->GetMethodId(method_idx)), "<init>") != 0) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003967 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
3968 return nullptr;
3969 }
3970 }
David Brazdil68b5c0b2016-01-19 14:25:29 +00003971 is_init = true;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003972 }
David Brazdil68b5c0b2016-01-19 14:25:29 +00003973 const RegType& adjusted_type = is_init
3974 ? GetRegTypeCache()->FromUninitialized(actual_arg_type)
3975 : actual_arg_type;
3976 if (method_type != METHOD_INTERFACE && !adjusted_type.IsZero()) {
Ian Rogersd8f69b02014-09-10 21:43:52 +00003977 const RegType* res_method_class;
Andreas Gamped9e23012015-06-04 22:19:58 -07003978 // Miranda methods have the declaring interface as their declaring class, not the abstract
3979 // class. It would be wrong to use this for the type check (interface type checks are
3980 // postponed to runtime).
3981 if (res_method != nullptr && !res_method->IsMiranda()) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003982 mirror::Class* klass = res_method->GetDeclaringClass();
Ian Rogers1ff3c982014-08-12 02:30:58 -07003983 std::string temp;
Andreas Gampef23f33d2015-06-23 14:18:17 -07003984 res_method_class = &FromClass(klass->GetDescriptor(&temp), klass,
3985 klass->CannotBeAssignedFromOtherTypes());
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003986 } else {
Orion Hodsoncfa325e2016-10-13 10:25:54 +01003987 const uint32_t method_idx = inst->VRegB();
Andreas Gampea5b09a62016-11-17 15:21:22 -08003988 const dex::TypeIndex class_idx = dex_file_->GetMethodId(method_idx).class_idx_;
Mathieu Chartierde40d472015-10-15 17:47:48 -07003989 res_method_class = &reg_types_.FromDescriptor(
3990 GetClassLoader(),
3991 dex_file_->StringByTypeIdx(class_idx),
3992 false);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003993 }
David Brazdilca3c8c32016-09-06 14:04:48 +01003994 if (!res_method_class->IsAssignableFrom(adjusted_type, this)) {
David Brazdil68b5c0b2016-01-19 14:25:29 +00003995 Fail(adjusted_type.IsUnresolvedTypes()
3996 ? VERIFY_ERROR_NO_CLASS
3997 : VERIFY_ERROR_BAD_CLASS_SOFT)
3998 << "'this' argument '" << actual_arg_type << "' not instance of '"
3999 << *res_method_class << "'";
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004000 // Continue on soft failures. We need to find possible hard failures to avoid problems in
4001 // the compiler.
4002 if (have_pending_hard_failure_) {
4003 return nullptr;
4004 }
4005 }
4006 }
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004007 }
4008
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004009 uint32_t arg[5];
4010 if (!is_range) {
4011 inst->GetVarArgs(arg);
4012 }
4013 uint32_t sig_registers = (method_type == METHOD_STATIC) ? 0 : 1;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004014 for ( ; it->HasNext(); it->Next()) {
4015 if (sig_registers >= expected_args) {
4016 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, expected " << inst->VRegA() <<
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004017 " argument registers, method signature has " << sig_registers + 1 << " or more";
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004018 return nullptr;
4019 }
4020
4021 const char* param_descriptor = it->GetDescriptor();
4022
4023 if (param_descriptor == nullptr) {
4024 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation because of missing signature "
4025 "component";
4026 return nullptr;
4027 }
4028
Ian Rogersd8f69b02014-09-10 21:43:52 +00004029 const RegType& reg_type = reg_types_.FromDescriptor(GetClassLoader(), param_descriptor, false);
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004030 uint32_t get_reg = is_range ? inst->VRegC() + static_cast<uint32_t>(sig_registers) :
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004031 arg[sig_registers];
4032 if (reg_type.IsIntegralTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004033 const RegType& src_type = work_line_->GetRegisterType(this, get_reg);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004034 if (!src_type.IsIntegralTypes()) {
4035 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register v" << get_reg << " has type " << src_type
4036 << " but expected " << reg_type;
Andreas Gampeb588f4c2015-05-26 13:35:39 -07004037 return nullptr;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004038 }
Andreas Gampeda9badb2015-06-05 20:22:12 -07004039 } else {
4040 if (!work_line_->VerifyRegisterType(this, get_reg, reg_type)) {
4041 // Continue on soft failures. We need to find possible hard failures to avoid problems in
4042 // the compiler.
4043 if (have_pending_hard_failure_) {
4044 return nullptr;
4045 }
4046 } else if (reg_type.IsLongOrDoubleTypes()) {
4047 // Check that registers are consecutive (for non-range invokes). Invokes are the only
4048 // instructions not specifying register pairs by the first component, but require them
4049 // nonetheless. Only check when there's an actual register in the parameters. If there's
4050 // none, this will fail below.
4051 if (!is_range && sig_registers + 1 < expected_args) {
4052 uint32_t second_reg = arg[sig_registers + 1];
4053 if (second_reg != get_reg + 1) {
4054 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, long or double parameter "
4055 "at index " << sig_registers << " is not a pair: " << get_reg << " + "
4056 << second_reg << ".";
4057 return nullptr;
4058 }
4059 }
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004060 }
4061 }
4062 sig_registers += reg_type.IsLongOrDoubleTypes() ? 2 : 1;
4063 }
4064 if (expected_args != sig_registers) {
4065 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, expected " << expected_args <<
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004066 " argument registers, method signature has " << sig_registers;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004067 return nullptr;
4068 }
4069 return res_method;
4070}
4071
4072void MethodVerifier::VerifyInvocationArgsUnresolvedMethod(const Instruction* inst,
4073 MethodType method_type,
4074 bool is_range) {
4075 // As the method may not have been resolved, make this static check against what we expect.
4076 // The main reason for this code block is to fail hard when we find an illegal use, e.g.,
4077 // wrong number of arguments or wrong primitive types, even if the method could not be resolved.
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004078 const uint32_t method_idx = inst->VRegB();
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004079 DexFileParameterIterator it(*dex_file_,
4080 dex_file_->GetProtoId(dex_file_->GetMethodId(method_idx).proto_idx_));
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004081 VerifyInvocationArgsFromIterator(&it, inst, method_type, is_range, nullptr);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004082}
4083
4084class MethodParamListDescriptorIterator {
4085 public:
Mathieu Chartiere401d142015-04-22 13:56:20 -07004086 explicit MethodParamListDescriptorIterator(ArtMethod* res_method) :
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004087 res_method_(res_method), pos_(0), params_(res_method->GetParameterTypeList()),
4088 params_size_(params_ == nullptr ? 0 : params_->Size()) {
4089 }
4090
4091 bool HasNext() {
4092 return pos_ < params_size_;
4093 }
4094
4095 void Next() {
4096 ++pos_;
4097 }
4098
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07004099 const char* GetDescriptor() REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004100 return res_method_->GetTypeDescriptorFromTypeIdx(params_->GetTypeItem(pos_).type_idx_);
4101 }
4102
4103 private:
Mathieu Chartiere401d142015-04-22 13:56:20 -07004104 ArtMethod* res_method_;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004105 size_t pos_;
4106 const DexFile::TypeList* params_;
4107 const size_t params_size_;
4108};
4109
Mathieu Chartiere401d142015-04-22 13:56:20 -07004110ArtMethod* MethodVerifier::VerifyInvocationArgs(
Alex Light7268d472016-01-20 15:50:01 -08004111 const Instruction* inst, MethodType method_type, bool is_range) {
jeffhao8cd6dda2012-02-22 10:15:34 -08004112 // Resolve the method. This could be an abstract or concrete method depending on what sort of call
4113 // we're making.
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004114 const uint32_t method_idx = inst->VRegB();
Alex Light7268d472016-01-20 15:50:01 -08004115 ArtMethod* res_method = ResolveMethodAndCheckAccess(method_idx, method_type);
Ian Rogers7b078e82014-09-10 14:44:24 -07004116 if (res_method == nullptr) { // error or class is unresolved
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004117 // Check what we can statically.
4118 if (!have_pending_hard_failure_) {
4119 VerifyInvocationArgsUnresolvedMethod(inst, method_type, is_range);
4120 }
4121 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08004122 }
4123
Ian Rogersd81871c2011-10-03 13:57:23 -07004124 // If we're using invoke-super(method), make sure that the executing method's class' superclass
Alex Light705ad492015-09-21 11:36:30 -07004125 // has a vtable entry for the target method. Or the target is on a interface.
Alex Light7268d472016-01-20 15:50:01 -08004126 if (method_type == METHOD_SUPER) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08004127 dex::TypeIndex class_idx = dex_file_->GetMethodId(method_idx).class_idx_;
David Brazdilca3c8c32016-09-06 14:04:48 +01004128 const RegType& reference_type = reg_types_.FromDescriptor(
4129 GetClassLoader(),
4130 dex_file_->StringByTypeIdx(class_idx),
4131 false);
4132 if (reference_type.IsUnresolvedTypes()) {
Alex Lightfedd91d2016-01-07 14:49:16 -08004133 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "Unable to find referenced class from invoke-super";
4134 return nullptr;
4135 }
David Brazdilca3c8c32016-09-06 14:04:48 +01004136 if (reference_type.GetClass()->IsInterface()) {
Alex Lightfedd91d2016-01-07 14:49:16 -08004137 // TODO Can we verify anything else.
David Brazdil15fc7292016-09-02 14:13:18 +01004138 if (class_idx == class_def_.class_idx_) {
Alex Lightfedd91d2016-01-07 14:49:16 -08004139 Fail(VERIFY_ERROR_CLASS_CHANGE) << "Cannot invoke-super on self as interface";
Alex Light55ea94d2016-03-15 09:50:26 -07004140 return nullptr;
Alex Lightfedd91d2016-01-07 14:49:16 -08004141 }
4142 // TODO Revisit whether we want to allow invoke-super on direct interfaces only like the JLS
4143 // does.
Alex Light55ea94d2016-03-15 09:50:26 -07004144 if (!GetDeclaringClass().HasClass()) {
4145 Fail(VERIFY_ERROR_NO_CLASS) << "Unable to resolve the full class of 'this' used in an"
4146 << "interface invoke-super";
4147 return nullptr;
David Brazdilca3c8c32016-09-06 14:04:48 +01004148 } else if (!reference_type.IsStrictlyAssignableFrom(GetDeclaringClass(), this)) {
Alex Lightfedd91d2016-01-07 14:49:16 -08004149 Fail(VERIFY_ERROR_CLASS_CHANGE)
David Sehr709b0702016-10-13 09:12:37 -07004150 << "invoke-super in " << mirror::Class::PrettyClass(GetDeclaringClass().GetClass())
4151 << " in method "
4152 << dex_file_->PrettyMethod(dex_method_idx_) << " to method "
4153 << dex_file_->PrettyMethod(method_idx) << " references "
4154 << "non-super-interface type " << mirror::Class::PrettyClass(reference_type.GetClass());
Alex Lightfedd91d2016-01-07 14:49:16 -08004155 return nullptr;
Alex Light705ad492015-09-21 11:36:30 -07004156 }
4157 } else {
4158 const RegType& super = GetDeclaringClass().GetSuperClass(&reg_types_);
4159 if (super.IsUnresolvedTypes()) {
4160 Fail(VERIFY_ERROR_NO_METHOD) << "unknown super class in invoke-super from "
David Sehr709b0702016-10-13 09:12:37 -07004161 << dex_file_->PrettyMethod(dex_method_idx_)
4162 << " to super " << res_method->PrettyMethod();
Alex Light705ad492015-09-21 11:36:30 -07004163 return nullptr;
4164 }
David Brazdilca3c8c32016-09-06 14:04:48 +01004165 if (!reference_type.IsStrictlyAssignableFrom(GetDeclaringClass(), this) ||
Aart Bikf663e342016-04-04 17:28:59 -07004166 (res_method->GetMethodIndex() >= super.GetClass()->GetVTableLength())) {
Alex Light705ad492015-09-21 11:36:30 -07004167 Fail(VERIFY_ERROR_NO_METHOD) << "invalid invoke-super from "
David Sehr709b0702016-10-13 09:12:37 -07004168 << dex_file_->PrettyMethod(dex_method_idx_)
Alex Light705ad492015-09-21 11:36:30 -07004169 << " to super " << super
4170 << "." << res_method->GetName()
4171 << res_method->GetSignature();
4172 return nullptr;
4173 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004174 }
4175 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08004176
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004177 if (method_type == METHOD_POLYMORPHIC) {
4178 // Process the signature of the calling site that is invoking the method handle.
4179 DexFileParameterIterator it(*dex_file_, dex_file_->GetProtoId(inst->VRegH()));
4180 return VerifyInvocationArgsFromIterator(&it, inst, method_type, is_range, res_method);
4181 } else {
4182 // Process the target method's signature.
4183 MethodParamListDescriptorIterator it(res_method);
4184 return VerifyInvocationArgsFromIterator(&it, inst, method_type, is_range, res_method);
4185 }
4186}
4187
4188bool MethodVerifier::CheckSignaturePolymorphicMethod(ArtMethod* method) {
4189 mirror::Class* klass = method->GetDeclaringClass();
4190 if (klass != mirror::MethodHandle::StaticClass()) {
4191 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
4192 << "Signature polymorphic method must be declared in java.lang.invoke.MethodClass";
4193 return false;
4194 }
4195
4196 const char* method_name = method->GetName();
4197 if (strcmp(method_name, "invoke") != 0 && strcmp(method_name, "invokeExact") != 0) {
4198 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
4199 << "Signature polymorphic method name invalid: " << method_name;
4200 return false;
4201 }
4202
4203 const DexFile::TypeList* types = method->GetParameterTypeList();
4204 if (types->Size() != 1) {
4205 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
4206 << "Signature polymorphic method has too many arguments " << types->Size() << " != 1";
4207 return false;
4208 }
4209
4210 const dex::TypeIndex argument_type_index = types->GetTypeItem(0).type_idx_;
4211 const char* argument_descriptor = method->GetTypeDescriptorFromTypeIdx(argument_type_index);
4212 if (strcmp(argument_descriptor, "[Ljava/lang/Object;") != 0) {
4213 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
4214 << "Signature polymorphic method has unexpected argument type: " << argument_descriptor;
4215 return false;
4216 }
4217
4218 const char* return_descriptor = method->GetReturnTypeDescriptor();
4219 if (strcmp(return_descriptor, "Ljava/lang/Object;") != 0) {
4220 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
4221 << "Signature polymorphic method has unexpected return type: " << return_descriptor;
4222 return false;
4223 }
4224
4225 return true;
4226}
4227
4228bool MethodVerifier::CheckSignaturePolymorphicReceiver(const Instruction* inst) {
4229 const RegType& this_type = work_line_->GetInvocationThis(this, inst);
4230 if (this_type.IsZero()) {
4231 /* null pointer always passes (and always fails at run time) */
4232 return true;
4233 } else if (!this_type.IsNonZeroReferenceTypes()) {
4234 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
4235 << "invoke-polymorphic receiver is not a reference: "
4236 << this_type;
4237 return false;
4238 } else if (this_type.IsUninitializedReference()) {
4239 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
4240 << "invoke-polymorphic receiver is uninitialized: "
4241 << this_type;
4242 return false;
4243 } else if (!this_type.HasClass()) {
4244 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
4245 << "invoke-polymorphic receiver has no class: "
4246 << this_type;
4247 return false;
4248 } else if (!this_type.GetClass()->IsSubClass(mirror::MethodHandle::StaticClass())) {
4249 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
4250 << "invoke-polymorphic receiver is not a subclass of MethodHandle: "
4251 << this_type;
4252 return false;
4253 }
4254 return true;
Ian Rogersd81871c2011-10-03 13:57:23 -07004255}
4256
Mathieu Chartiere401d142015-04-22 13:56:20 -07004257ArtMethod* MethodVerifier::GetQuickInvokedMethod(const Instruction* inst, RegisterLine* reg_line,
4258 bool is_range, bool allow_failure) {
Mathieu Chartier091d2382015-03-06 10:59:06 -08004259 if (is_range) {
4260 DCHECK_EQ(inst->Opcode(), Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
4261 } else {
4262 DCHECK_EQ(inst->Opcode(), Instruction::INVOKE_VIRTUAL_QUICK);
4263 }
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004264 const RegType& actual_arg_type = reg_line->GetInvocationThis(this, inst, allow_failure);
Ian Rogers9bc54402014-04-17 16:40:01 -07004265 if (!actual_arg_type.HasClass()) {
4266 VLOG(verifier) << "Failed to get mirror::Class* from '" << actual_arg_type << "'";
Andreas Gampe63981562014-04-17 12:28:43 -07004267 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004268 }
Ian Rogersa4cf1df2014-05-07 19:47:17 -07004269 mirror::Class* klass = actual_arg_type.GetClass();
Mingyao Yang2cdbad72014-07-16 10:44:41 -07004270 mirror::Class* dispatch_class;
Ian Rogersa4cf1df2014-05-07 19:47:17 -07004271 if (klass->IsInterface()) {
4272 // Derive Object.class from Class.class.getSuperclass().
4273 mirror::Class* object_klass = klass->GetClass()->GetSuperClass();
Andreas Gampe7c038102014-10-27 20:08:46 -07004274 if (FailOrAbort(this, object_klass->IsObjectClass(),
Mathieu Chartier091d2382015-03-06 10:59:06 -08004275 "Failed to find Object class in quickened invoke receiver", work_insn_idx_)) {
Andreas Gampe7c038102014-10-27 20:08:46 -07004276 return nullptr;
4277 }
Mingyao Yang2cdbad72014-07-16 10:44:41 -07004278 dispatch_class = object_klass;
Ian Rogersa4cf1df2014-05-07 19:47:17 -07004279 } else {
Mingyao Yang2cdbad72014-07-16 10:44:41 -07004280 dispatch_class = klass;
Ian Rogersa4cf1df2014-05-07 19:47:17 -07004281 }
Mathieu Chartier091d2382015-03-06 10:59:06 -08004282 if (!dispatch_class->HasVTable()) {
4283 FailOrAbort(this, allow_failure, "Receiver class has no vtable for quickened invoke at ",
4284 work_insn_idx_);
Andreas Gampe7c038102014-10-27 20:08:46 -07004285 return nullptr;
4286 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004287 uint16_t vtable_index = is_range ? inst->VRegB_3rc() : inst->VRegB_35c();
Mathieu Chartiere401d142015-04-22 13:56:20 -07004288 auto* cl = Runtime::Current()->GetClassLinker();
4289 auto pointer_size = cl->GetImagePointerSize();
Mathieu Chartier091d2382015-03-06 10:59:06 -08004290 if (static_cast<int32_t>(vtable_index) >= dispatch_class->GetVTableLength()) {
4291 FailOrAbort(this, allow_failure,
4292 "Receiver class has not enough vtable slots for quickened invoke at ",
4293 work_insn_idx_);
Andreas Gampe7c038102014-10-27 20:08:46 -07004294 return nullptr;
4295 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07004296 ArtMethod* res_method = dispatch_class->GetVTableEntry(vtable_index, pointer_size);
Mathieu Chartier091d2382015-03-06 10:59:06 -08004297 if (self_->IsExceptionPending()) {
4298 FailOrAbort(this, allow_failure, "Unexpected exception pending for quickened invoke at ",
4299 work_insn_idx_);
Andreas Gampe7c038102014-10-27 20:08:46 -07004300 return nullptr;
4301 }
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004302 return res_method;
4303}
4304
Mathieu Chartiere401d142015-04-22 13:56:20 -07004305ArtMethod* MethodVerifier::VerifyInvokeVirtualQuickArgs(const Instruction* inst, bool is_range) {
Andreas Gampe76bd8802014-12-10 16:43:58 -08004306 DCHECK(Runtime::Current()->IsStarted() || verify_to_dump_)
David Sehr709b0702016-10-13 09:12:37 -07004307 << dex_file_->PrettyMethod(dex_method_idx_, true) << "@" << work_insn_idx_;
Andreas Gampe76bd8802014-12-10 16:43:58 -08004308
Mathieu Chartiere401d142015-04-22 13:56:20 -07004309 ArtMethod* res_method = GetQuickInvokedMethod(inst, work_line_.get(), is_range, false);
Ian Rogers7b078e82014-09-10 14:44:24 -07004310 if (res_method == nullptr) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004311 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer method from " << inst->Name();
Ian Rogers7b078e82014-09-10 14:44:24 -07004312 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004313 }
Andreas Gampe7c038102014-10-27 20:08:46 -07004314 if (FailOrAbort(this, !res_method->IsDirect(), "Quick-invoked method is direct at ",
4315 work_insn_idx_)) {
4316 return nullptr;
4317 }
4318 if (FailOrAbort(this, !res_method->IsStatic(), "Quick-invoked method is static at ",
4319 work_insn_idx_)) {
4320 return nullptr;
4321 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004322
4323 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
4324 // match the call to the signature. Also, we might be calling through an abstract method
4325 // definition (which doesn't have register count values).
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004326 const RegType& actual_arg_type = work_line_->GetInvocationThis(this, inst);
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004327 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
Ian Rogers7b078e82014-09-10 14:44:24 -07004328 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004329 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004330 const size_t expected_args = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
4331 /* caught by static verifier */
4332 DCHECK(is_range || expected_args <= 5);
4333 if (expected_args > code_item_->outs_size_) {
4334 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
4335 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
Ian Rogers7b078e82014-09-10 14:44:24 -07004336 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004337 }
4338
4339 /*
4340 * Check the "this" argument, which must be an instance of the class that declared the method.
4341 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
4342 * rigorous check here (which is okay since we have to do it at runtime).
4343 */
David Brazdil68b5c0b2016-01-19 14:25:29 +00004344 // Note: given an uninitialized type, this should always fail. Constructors aren't virtual.
4345 if (actual_arg_type.IsUninitializedTypes() && !res_method->IsConstructor()) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004346 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
Ian Rogers7b078e82014-09-10 14:44:24 -07004347 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004348 }
4349 if (!actual_arg_type.IsZero()) {
4350 mirror::Class* klass = res_method->GetDeclaringClass();
Ian Rogers1ff3c982014-08-12 02:30:58 -07004351 std::string temp;
Ian Rogersd8f69b02014-09-10 21:43:52 +00004352 const RegType& res_method_class =
Andreas Gampef23f33d2015-06-23 14:18:17 -07004353 FromClass(klass->GetDescriptor(&temp), klass, klass->CannotBeAssignedFromOtherTypes());
David Brazdilca3c8c32016-09-06 14:04:48 +01004354 if (!res_method_class.IsAssignableFrom(actual_arg_type, this)) {
David Brazdil68b5c0b2016-01-19 14:25:29 +00004355 Fail(actual_arg_type.IsUninitializedTypes() // Just overcautious - should have never
4356 ? VERIFY_ERROR_BAD_CLASS_HARD // quickened this.
4357 : actual_arg_type.IsUnresolvedTypes()
4358 ? VERIFY_ERROR_NO_CLASS
4359 : VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004360 << "' not instance of '" << res_method_class << "'";
Ian Rogers7b078e82014-09-10 14:44:24 -07004361 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004362 }
4363 }
4364 /*
4365 * Process the target method's signature. This signature may or may not
4366 * have been verified, so we can't assume it's properly formed.
4367 */
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07004368 const DexFile::TypeList* params = res_method->GetParameterTypeList();
Ian Rogers7b078e82014-09-10 14:44:24 -07004369 size_t params_size = params == nullptr ? 0 : params->Size();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004370 uint32_t arg[5];
4371 if (!is_range) {
Ian Rogers29a26482014-05-02 15:27:29 -07004372 inst->GetVarArgs(arg);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004373 }
4374 size_t actual_args = 1;
4375 for (size_t param_index = 0; param_index < params_size; param_index++) {
4376 if (actual_args >= expected_args) {
David Sehr709b0702016-10-13 09:12:37 -07004377 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invalid call to '"
4378 << res_method->PrettyMethod()
Brian Carlstrom93c33962013-07-26 10:37:43 -07004379 << "'. Expected " << expected_args
4380 << " arguments, processing argument " << actual_args
4381 << " (where longs/doubles count twice).";
Ian Rogers7b078e82014-09-10 14:44:24 -07004382 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004383 }
4384 const char* descriptor =
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07004385 res_method->GetTypeDescriptorFromTypeIdx(params->GetTypeItem(param_index).type_idx_);
Ian Rogers7b078e82014-09-10 14:44:24 -07004386 if (descriptor == nullptr) {
David Sehr709b0702016-10-13 09:12:37 -07004387 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of "
4388 << res_method->PrettyMethod()
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004389 << " missing signature component";
Ian Rogers7b078e82014-09-10 14:44:24 -07004390 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004391 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00004392 const RegType& reg_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004393 uint32_t get_reg = is_range ? inst->VRegC_3rc() + actual_args : arg[actual_args];
Ian Rogers7b078e82014-09-10 14:44:24 -07004394 if (!work_line_->VerifyRegisterType(this, get_reg, reg_type)) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004395 return res_method;
4396 }
4397 actual_args = reg_type.IsLongOrDoubleTypes() ? actual_args + 2 : actual_args + 1;
4398 }
4399 if (actual_args != expected_args) {
David Sehr709b0702016-10-13 09:12:37 -07004400 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of "
4401 << res_method->PrettyMethod() << " expected "
4402 << expected_args << " arguments, found " << actual_args;
Ian Rogers7b078e82014-09-10 14:44:24 -07004403 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004404 } else {
4405 return res_method;
4406 }
4407}
4408
Ian Rogers62342ec2013-06-11 10:26:37 -07004409void MethodVerifier::VerifyNewArray(const Instruction* inst, bool is_filled, bool is_range) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08004410 dex::TypeIndex type_idx;
Sebastien Hertz5243e912013-05-21 10:55:07 +02004411 if (!is_filled) {
4412 DCHECK_EQ(inst->Opcode(), Instruction::NEW_ARRAY);
Andreas Gampea5b09a62016-11-17 15:21:22 -08004413 type_idx = dex::TypeIndex(inst->VRegC_22c());
Sebastien Hertz5243e912013-05-21 10:55:07 +02004414 } else if (!is_range) {
4415 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY);
Andreas Gampea5b09a62016-11-17 15:21:22 -08004416 type_idx = dex::TypeIndex(inst->VRegB_35c());
Sebastien Hertz5243e912013-05-21 10:55:07 +02004417 } else {
4418 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY_RANGE);
Andreas Gampea5b09a62016-11-17 15:21:22 -08004419 type_idx = dex::TypeIndex(inst->VRegB_3rc());
Sebastien Hertz5243e912013-05-21 10:55:07 +02004420 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00004421 const RegType& res_type = ResolveClassAndCheckAccess(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004422 if (res_type.IsConflict()) { // bad class
4423 DCHECK_NE(failures_.size(), 0U);
Ian Rogers0c4a5062012-02-03 15:18:59 -08004424 } else {
4425 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
4426 if (!res_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07004427 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "new-array on non-array class " << res_type;
Ian Rogers0c4a5062012-02-03 15:18:59 -08004428 } else if (!is_filled) {
4429 /* make sure "size" register is valid type */
Ian Rogers7b078e82014-09-10 14:44:24 -07004430 work_line_->VerifyRegisterType(this, inst->VRegB_22c(), reg_types_.Integer());
Ian Rogers0c4a5062012-02-03 15:18:59 -08004431 /* set register type to array class */
Ian Rogersd8f69b02014-09-10 21:43:52 +00004432 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
Andreas Gampead238ce2015-08-24 21:13:08 -07004433 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_22c(), precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08004434 } else {
Andreas Gampebb18a032016-03-22 20:34:25 -07004435 DCHECK(!res_type.IsUnresolvedMergedReference());
Ian Rogers0c4a5062012-02-03 15:18:59 -08004436 // Verify each register. If "arg_count" is bad, VerifyRegisterType() will run off the end of
4437 // the list and fail. It's legal, if silly, for arg_count to be zero.
Ian Rogersd8f69b02014-09-10 21:43:52 +00004438 const RegType& expected_type = reg_types_.GetComponentType(res_type, GetClassLoader());
Sebastien Hertz5243e912013-05-21 10:55:07 +02004439 uint32_t arg_count = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
4440 uint32_t arg[5];
4441 if (!is_range) {
Ian Rogers29a26482014-05-02 15:27:29 -07004442 inst->GetVarArgs(arg);
Sebastien Hertz5243e912013-05-21 10:55:07 +02004443 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08004444 for (size_t ui = 0; ui < arg_count; ui++) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02004445 uint32_t get_reg = is_range ? inst->VRegC_3rc() + ui : arg[ui];
Ian Rogers7b078e82014-09-10 14:44:24 -07004446 if (!work_line_->VerifyRegisterType(this, get_reg, expected_type)) {
4447 work_line_->SetResultRegisterType(this, reg_types_.Conflict());
Ian Rogers0c4a5062012-02-03 15:18:59 -08004448 return;
4449 }
4450 }
4451 // filled-array result goes into "result" register
Ian Rogersd8f69b02014-09-10 21:43:52 +00004452 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
Ian Rogers7b078e82014-09-10 14:44:24 -07004453 work_line_->SetResultRegisterType(this, precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08004454 }
4455 }
4456}
4457
Sebastien Hertz5243e912013-05-21 10:55:07 +02004458void MethodVerifier::VerifyAGet(const Instruction* inst,
Ian Rogersd8f69b02014-09-10 21:43:52 +00004459 const RegType& insn_type, bool is_primitive) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004460 const RegType& index_type = work_line_->GetRegisterType(this, inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07004461 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07004462 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07004463 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004464 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegB_23x());
Ian Rogers89310de2012-02-01 13:47:30 -08004465 if (array_type.IsZero()) {
Nicolas Geoffray4824c272015-06-24 15:53:03 +01004466 have_pending_runtime_throw_failure_ = true;
Ian Rogers89310de2012-02-01 13:47:30 -08004467 // Null array class; this code path will fail at runtime. Infer a merge-able type from the
4468 // instruction type. TODO: have a proper notion of bottom here.
4469 if (!is_primitive || insn_type.IsCategory1Types()) {
4470 // Reference or category 1
Andreas Gampead238ce2015-08-24 21:13:08 -07004471 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), reg_types_.Zero());
Ian Rogersd81871c2011-10-03 13:57:23 -07004472 } else {
Ian Rogers89310de2012-02-01 13:47:30 -08004473 // Category 2
Ian Rogers7b078e82014-09-10 14:44:24 -07004474 work_line_->SetRegisterTypeWide(this, inst->VRegA_23x(),
4475 reg_types_.FromCat2ConstLo(0, false),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004476 reg_types_.FromCat2ConstHi(0, false));
Ian Rogers89310de2012-02-01 13:47:30 -08004477 }
jeffhaofc3144e2012-02-01 17:21:15 -08004478 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07004479 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aget";
Andreas Gampe8d8fc482016-03-25 16:24:20 -07004480 } else if (array_type.IsUnresolvedMergedReference()) {
Andreas Gampebb18a032016-03-22 20:34:25 -07004481 // Unresolved array types must be reference array types.
4482 if (is_primitive) {
4483 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type
4484 << " source for category 1 aget";
4485 } else {
4486 Fail(VERIFY_ERROR_NO_CLASS) << "cannot verify aget for " << array_type
4487 << " because of missing class";
Andreas Gampe8d8fc482016-03-25 16:24:20 -07004488 // Approximate with java.lang.Object[].
4489 work_line_->SetRegisterType<LockOp::kClear>(this,
4490 inst->VRegA_23x(),
4491 reg_types_.JavaLangObject(false));
Andreas Gampebb18a032016-03-22 20:34:25 -07004492 }
Ian Rogers89310de2012-02-01 13:47:30 -08004493 } else {
4494 /* verify the class */
Ian Rogersd8f69b02014-09-10 21:43:52 +00004495 const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
jeffhaofc3144e2012-02-01 17:21:15 -08004496 if (!component_type.IsReferenceTypes() && !is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07004497 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08004498 << " source for aget-object";
4499 } else if (component_type.IsNonZeroReferenceTypes() && is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07004500 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08004501 << " source for category 1 aget";
4502 } else if (is_primitive && !insn_type.Equals(component_type) &&
4503 !((insn_type.IsInteger() && component_type.IsFloat()) ||
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004504 (insn_type.IsLong() && component_type.IsDouble()))) {
4505 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array type " << array_type
4506 << " incompatible with aget of type " << insn_type;
Ian Rogers89310de2012-02-01 13:47:30 -08004507 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004508 // Use knowledge of the field type which is stronger than the type inferred from the
4509 // instruction, which can't differentiate object types and ints from floats, longs from
4510 // doubles.
4511 if (!component_type.IsLowHalf()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07004512 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), component_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004513 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004514 work_line_->SetRegisterTypeWide(this, inst->VRegA_23x(), component_type,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004515 component_type.HighHalf(&reg_types_));
4516 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004517 }
4518 }
4519 }
4520}
4521
Ian Rogersd8f69b02014-09-10 21:43:52 +00004522void MethodVerifier::VerifyPrimitivePut(const RegType& target_type, const RegType& insn_type,
Jeff Haofe1f7c82013-08-01 14:50:24 -07004523 const uint32_t vregA) {
4524 // Primitive assignability rules are weaker than regular assignability rules.
4525 bool instruction_compatible;
4526 bool value_compatible;
Ian Rogers7b078e82014-09-10 14:44:24 -07004527 const RegType& value_type = work_line_->GetRegisterType(this, vregA);
Jeff Haofe1f7c82013-08-01 14:50:24 -07004528 if (target_type.IsIntegralTypes()) {
Jeff Haoa4647482013-08-06 15:35:47 -07004529 instruction_compatible = target_type.Equals(insn_type);
Jeff Haofe1f7c82013-08-01 14:50:24 -07004530 value_compatible = value_type.IsIntegralTypes();
4531 } else if (target_type.IsFloat()) {
4532 instruction_compatible = insn_type.IsInteger(); // no put-float, so expect put-int
4533 value_compatible = value_type.IsFloatTypes();
4534 } else if (target_type.IsLong()) {
4535 instruction_compatible = insn_type.IsLong();
Andreas Gampe376fa682014-09-07 13:06:12 -07004536 // Additional register check: this is not checked statically (as part of VerifyInstructions),
4537 // as target_type depends on the resolved type of the field.
4538 if (instruction_compatible && work_line_->NumRegs() > vregA + 1) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004539 const RegType& value_type_hi = work_line_->GetRegisterType(this, vregA + 1);
Andreas Gampe376fa682014-09-07 13:06:12 -07004540 value_compatible = value_type.IsLongTypes() && value_type.CheckWidePair(value_type_hi);
4541 } else {
4542 value_compatible = false;
4543 }
Jeff Haofe1f7c82013-08-01 14:50:24 -07004544 } else if (target_type.IsDouble()) {
4545 instruction_compatible = insn_type.IsLong(); // no put-double, so expect put-long
Andreas Gampe376fa682014-09-07 13:06:12 -07004546 // Additional register check: this is not checked statically (as part of VerifyInstructions),
4547 // as target_type depends on the resolved type of the field.
4548 if (instruction_compatible && work_line_->NumRegs() > vregA + 1) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004549 const RegType& value_type_hi = work_line_->GetRegisterType(this, vregA + 1);
Andreas Gampe376fa682014-09-07 13:06:12 -07004550 value_compatible = value_type.IsDoubleTypes() && value_type.CheckWidePair(value_type_hi);
4551 } else {
4552 value_compatible = false;
4553 }
Jeff Haofe1f7c82013-08-01 14:50:24 -07004554 } else {
4555 instruction_compatible = false; // reference with primitive store
4556 value_compatible = false; // unused
4557 }
4558 if (!instruction_compatible) {
4559 // This is a global failure rather than a class change failure as the instructions and
4560 // the descriptors for the type should have been consistent within the same file at
4561 // compile time.
4562 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "put insn has type '" << insn_type
4563 << "' but expected type '" << target_type << "'";
4564 return;
4565 }
4566 if (!value_compatible) {
4567 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
4568 << " of type " << value_type << " but expected " << target_type << " for put";
4569 return;
4570 }
4571}
4572
Sebastien Hertz5243e912013-05-21 10:55:07 +02004573void MethodVerifier::VerifyAPut(const Instruction* inst,
Ian Rogersd8f69b02014-09-10 21:43:52 +00004574 const RegType& insn_type, bool is_primitive) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004575 const RegType& index_type = work_line_->GetRegisterType(this, inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07004576 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07004577 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07004578 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004579 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegB_23x());
Ian Rogers89310de2012-02-01 13:47:30 -08004580 if (array_type.IsZero()) {
Nicolas Geoffray66389fb2015-06-19 10:35:42 +01004581 // Null array type; this code path will fail at runtime.
4582 // Still check that the given value matches the instruction's type.
Andreas Gampe4bf4c782015-08-14 14:07:43 -07004583 // Note: this is, as usual, complicated by the fact the the instruction isn't fully typed
4584 // and fits multiple register types.
4585 const RegType* modified_reg_type = &insn_type;
4586 if ((modified_reg_type == &reg_types_.Integer()) ||
4587 (modified_reg_type == &reg_types_.LongLo())) {
4588 // May be integer or float | long or double. Overwrite insn_type accordingly.
4589 const RegType& value_type = work_line_->GetRegisterType(this, inst->VRegA_23x());
4590 if (modified_reg_type == &reg_types_.Integer()) {
4591 if (&value_type == &reg_types_.Float()) {
4592 modified_reg_type = &value_type;
4593 }
4594 } else {
4595 if (&value_type == &reg_types_.DoubleLo()) {
4596 modified_reg_type = &value_type;
4597 }
4598 }
4599 }
4600 work_line_->VerifyRegisterType(this, inst->VRegA_23x(), *modified_reg_type);
jeffhaofc3144e2012-02-01 17:21:15 -08004601 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07004602 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aput";
Andreas Gampe8d8fc482016-03-25 16:24:20 -07004603 } else if (array_type.IsUnresolvedMergedReference()) {
Andreas Gampebb18a032016-03-22 20:34:25 -07004604 // Unresolved array types must be reference array types.
4605 if (is_primitive) {
4606 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "put insn has type '" << insn_type
4607 << "' but unresolved type '" << array_type << "'";
4608 } else {
4609 Fail(VERIFY_ERROR_NO_CLASS) << "cannot verify aput for " << array_type
4610 << " because of missing class";
4611 }
Ian Rogers89310de2012-02-01 13:47:30 -08004612 } else {
Ian Rogersd8f69b02014-09-10 21:43:52 +00004613 const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
Jeff Haofe1f7c82013-08-01 14:50:24 -07004614 const uint32_t vregA = inst->VRegA_23x();
Jeff Haob24b4a72013-07-31 13:47:31 -07004615 if (is_primitive) {
Jeff Haofe1f7c82013-08-01 14:50:24 -07004616 VerifyPrimitivePut(component_type, insn_type, vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07004617 } else {
Jeff Haob24b4a72013-07-31 13:47:31 -07004618 if (!component_type.IsReferenceTypes()) {
4619 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
4620 << " source for aput-object";
4621 } else {
4622 // The instruction agrees with the type of array, confirm the value to be stored does too
4623 // Note: we use the instruction type (rather than the component type) for aput-object as
4624 // incompatible classes will be caught at runtime as an array store exception
Ian Rogers7b078e82014-09-10 14:44:24 -07004625 work_line_->VerifyRegisterType(this, vregA, insn_type);
Jeff Haob24b4a72013-07-31 13:47:31 -07004626 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004627 }
4628 }
4629 }
4630}
4631
Mathieu Chartierc7853442015-03-27 14:35:38 -07004632ArtField* MethodVerifier::GetStaticField(int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08004633 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
4634 // Check access to class
Ian Rogersd8f69b02014-09-10 21:43:52 +00004635 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Brian Carlstrom7934ac22013-07-26 10:54:15 -07004636 if (klass_type.IsConflict()) { // bad class
Ian Rogersad0b3a32012-04-16 14:50:24 -07004637 AppendToLastFailMessage(StringPrintf(" in attempt to access static field %d (%s) in %s",
4638 field_idx, dex_file_->GetFieldName(field_id),
4639 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers7b078e82014-09-10 14:44:24 -07004640 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08004641 }
Elliott Hughesb25c3f62012-03-26 16:35:06 -07004642 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004643 return nullptr; // Can't resolve Class so no more to do here, will do checking at runtime.
Ian Rogers90040192011-12-16 08:54:29 -08004644 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07004645 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
David Brazdilca3c8c32016-09-06 14:04:48 +01004646 ArtField* field = class_linker->ResolveFieldJLS(*dex_file_, field_idx, dex_cache_, class_loader_);
4647
4648 // Record result of the field resolution attempt.
4649 VerifierDeps::MaybeRecordFieldResolution(*dex_file_, field_idx, field);
4650
Ian Rogers7b078e82014-09-10 14:44:24 -07004651 if (field == nullptr) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07004652 VLOG(verifier) << "Unable to resolve static field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07004653 << dex_file_->GetFieldName(field_id) << ") in "
4654 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogers7b078e82014-09-10 14:44:24 -07004655 DCHECK(self_->IsExceptionPending());
4656 self_->ClearException();
4657 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004658 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
4659 field->GetAccessFlags())) {
David Sehr709b0702016-10-13 09:12:37 -07004660 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access static field " << field->PrettyField()
Ian Rogersad0b3a32012-04-16 14:50:24 -07004661 << " from " << GetDeclaringClass();
Ian Rogers7b078e82014-09-10 14:44:24 -07004662 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07004663 } else if (!field->IsStatic()) {
David Sehr709b0702016-10-13 09:12:37 -07004664 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << field->PrettyField() << " to be static";
Ian Rogers7b078e82014-09-10 14:44:24 -07004665 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07004666 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07004667 return field;
Ian Rogersd81871c2011-10-03 13:57:23 -07004668}
4669
Mathieu Chartierc7853442015-03-27 14:35:38 -07004670ArtField* MethodVerifier::GetInstanceField(const RegType& obj_type, int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08004671 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
Aart Bik31883642016-06-06 15:02:44 -07004672 // Check access to class.
Ian Rogersd8f69b02014-09-10 21:43:52 +00004673 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004674 if (klass_type.IsConflict()) {
4675 AppendToLastFailMessage(StringPrintf(" in attempt to access instance field %d (%s) in %s",
4676 field_idx, dex_file_->GetFieldName(field_id),
4677 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers7b078e82014-09-10 14:44:24 -07004678 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08004679 }
jeffhao8cd6dda2012-02-22 10:15:34 -08004680 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004681 return nullptr; // Can't resolve Class so no more to do here
Ian Rogers90040192011-12-16 08:54:29 -08004682 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07004683 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
David Brazdilca3c8c32016-09-06 14:04:48 +01004684 ArtField* field = class_linker->ResolveFieldJLS(*dex_file_, field_idx, dex_cache_, class_loader_);
4685
4686 // Record result of the field resolution attempt.
4687 VerifierDeps::MaybeRecordFieldResolution(*dex_file_, field_idx, field);
4688
Ian Rogers7b078e82014-09-10 14:44:24 -07004689 if (field == nullptr) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07004690 VLOG(verifier) << "Unable to resolve instance field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07004691 << dex_file_->GetFieldName(field_id) << ") in "
4692 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogers7b078e82014-09-10 14:44:24 -07004693 DCHECK(self_->IsExceptionPending());
4694 self_->ClearException();
4695 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07004696 } else if (obj_type.IsZero()) {
Aart Bik31883642016-06-06 15:02:44 -07004697 // Cannot infer and check type, however, access will cause null pointer exception.
4698 // Fall through into a few last soft failure checks below.
Stephen Kyle695c5982014-08-22 15:03:07 +01004699 } else if (!obj_type.IsReferenceTypes()) {
Aart Bik31883642016-06-06 15:02:44 -07004700 // Trying to read a field from something that isn't a reference.
Stephen Kyle695c5982014-08-22 15:03:07 +01004701 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance field access on object that has "
4702 << "non-reference type " << obj_type;
Ian Rogers7b078e82014-09-10 14:44:24 -07004703 return nullptr;
Ian Rogerse1758fe2012-04-19 11:31:15 -07004704 } else {
David Brazdil0d638bb2016-07-27 15:29:25 +01004705 std::string temp;
Mathieu Chartier3398c782016-09-30 10:27:43 -07004706 ObjPtr<mirror::Class> klass = field->GetDeclaringClass();
Ian Rogersd8f69b02014-09-10 21:43:52 +00004707 const RegType& field_klass =
Mathieu Chartier3398c782016-09-30 10:27:43 -07004708 FromClass(klass->GetDescriptor(&temp),
Mathieu Chartier1cc62e42016-10-03 18:01:28 -07004709 klass.Ptr(),
Mathieu Chartier3398c782016-09-30 10:27:43 -07004710 klass->CannotBeAssignedFromOtherTypes());
David Brazdil68b5c0b2016-01-19 14:25:29 +00004711 if (obj_type.IsUninitializedTypes()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07004712 // Field accesses through uninitialized references are only allowable for constructors where
David Brazdil68b5c0b2016-01-19 14:25:29 +00004713 // the field is declared in this class.
4714 // Note: this IsConstructor check is technically redundant, as UninitializedThis should only
4715 // appear in constructors.
4716 if (!obj_type.IsUninitializedThisReference() ||
4717 !IsConstructor() ||
4718 !field_klass.Equals(GetDeclaringClass())) {
David Sehr709b0702016-10-13 09:12:37 -07004719 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "cannot access instance field " << field->PrettyField()
David Brazdil68b5c0b2016-01-19 14:25:29 +00004720 << " of a not fully initialized object within the context"
David Sehr709b0702016-10-13 09:12:37 -07004721 << " of " << dex_file_->PrettyMethod(dex_method_idx_);
David Brazdil68b5c0b2016-01-19 14:25:29 +00004722 return nullptr;
4723 }
David Brazdilca3c8c32016-09-06 14:04:48 +01004724 } else if (!field_klass.IsAssignableFrom(obj_type, this)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07004725 // Trying to access C1.field1 using reference of type C2, which is neither C1 or a sub-class
4726 // of C1. For resolution to occur the declared class of the field must be compatible with
4727 // obj_type, we've discovered this wasn't so, so report the field didn't exist.
Andreas Gampe66596242016-04-14 10:55:04 -07004728 VerifyError type;
4729 bool is_aot = Runtime::Current()->IsAotCompiler();
4730 if (is_aot && (field_klass.IsUnresolvedTypes() || obj_type.IsUnresolvedTypes())) {
4731 // Compiler & unresolved types involved, retry at runtime.
4732 type = VerifyError::VERIFY_ERROR_NO_CLASS;
4733 } else {
Andreas Gampe8f4ade02016-04-15 10:09:16 -07004734 // Classes known (resolved; and thus assignability check is precise), or we are at runtime
4735 // and still missing classes. This is a hard failure.
Andreas Gampe66596242016-04-14 10:55:04 -07004736 type = VerifyError::VERIFY_ERROR_BAD_CLASS_HARD;
4737 }
David Sehr709b0702016-10-13 09:12:37 -07004738 Fail(type) << "cannot access instance field " << field->PrettyField()
Andreas Gampe66596242016-04-14 10:55:04 -07004739 << " from object of type " << obj_type;
Ian Rogers7b078e82014-09-10 14:44:24 -07004740 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004741 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004742 }
Aart Bik31883642016-06-06 15:02:44 -07004743
4744 // Few last soft failure checks.
4745 if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
4746 field->GetAccessFlags())) {
David Sehr709b0702016-10-13 09:12:37 -07004747 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access instance field " << field->PrettyField()
Aart Bik31883642016-06-06 15:02:44 -07004748 << " from " << GetDeclaringClass();
4749 return nullptr;
4750 } else if (field->IsStatic()) {
David Sehr709b0702016-10-13 09:12:37 -07004751 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << field->PrettyField()
Aart Bik31883642016-06-06 15:02:44 -07004752 << " to not be static";
4753 return nullptr;
4754 }
4755
4756 return field;
Ian Rogersd81871c2011-10-03 13:57:23 -07004757}
4758
Andreas Gampe896df402014-10-20 22:25:29 -07004759template <MethodVerifier::FieldAccessType kAccType>
4760void MethodVerifier::VerifyISFieldAccess(const Instruction* inst, const RegType& insn_type,
4761 bool is_primitive, bool is_static) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02004762 uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
Mathieu Chartierc7853442015-03-27 14:35:38 -07004763 ArtField* field;
Ian Rogersb94a27b2011-10-26 00:33:41 -07004764 if (is_static) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07004765 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07004766 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004767 const RegType& object_type = work_line_->GetRegisterType(this, inst->VRegB_22c());
David Brazdil68b5c0b2016-01-19 14:25:29 +00004768
4769 // One is not allowed to access fields on uninitialized references, except to write to
4770 // fields in the constructor (before calling another constructor).
4771 // GetInstanceField does an assignability check which will fail for uninitialized types.
4772 // We thus modify the type if the uninitialized reference is a "this" reference (this also
4773 // checks at the same time that we're verifying a constructor).
4774 bool should_adjust = (kAccType == FieldAccessType::kAccPut) &&
4775 object_type.IsUninitializedThisReference();
4776 const RegType& adjusted_type = should_adjust
4777 ? GetRegTypeCache()->FromUninitialized(object_type)
4778 : object_type;
4779 field = GetInstanceField(adjusted_type, field_idx);
Andreas Gampe896df402014-10-20 22:25:29 -07004780 if (UNLIKELY(have_pending_hard_failure_)) {
4781 return;
4782 }
Alex Light4a2c8fc2016-02-12 11:01:54 -08004783 if (should_adjust) {
4784 if (field == nullptr) {
4785 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "Might be accessing a superclass instance field prior "
4786 << "to the superclass being initialized in "
David Sehr709b0702016-10-13 09:12:37 -07004787 << dex_file_->PrettyMethod(dex_method_idx_);
Alex Light4a2c8fc2016-02-12 11:01:54 -08004788 } else if (field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
4789 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "cannot access superclass instance field "
David Sehr709b0702016-10-13 09:12:37 -07004790 << field->PrettyField() << " of a not fully initialized "
Alex Light4a2c8fc2016-02-12 11:01:54 -08004791 << "object within the context of "
David Sehr709b0702016-10-13 09:12:37 -07004792 << dex_file_->PrettyMethod(dex_method_idx_);
Alex Light4a2c8fc2016-02-12 11:01:54 -08004793 return;
4794 }
4795 }
Ian Rogersb94a27b2011-10-26 00:33:41 -07004796 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00004797 const RegType* field_type = nullptr;
Ian Rogers7b078e82014-09-10 14:44:24 -07004798 if (field != nullptr) {
Andreas Gampe896df402014-10-20 22:25:29 -07004799 if (kAccType == FieldAccessType::kAccPut) {
4800 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
David Sehr709b0702016-10-13 09:12:37 -07004801 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << field->PrettyField()
Andreas Gampe896df402014-10-20 22:25:29 -07004802 << " from other class " << GetDeclaringClass();
Aart Bikc2bc2652016-05-23 14:58:49 -07004803 // Keep hunting for possible hard fails.
Andreas Gampe896df402014-10-20 22:25:29 -07004804 }
4805 }
4806
Mathieu Chartier3398c782016-09-30 10:27:43 -07004807 ObjPtr<mirror::Class> field_type_class =
Mathieu Chartierc7853442015-03-27 14:35:38 -07004808 can_load_classes_ ? field->GetType<true>() : field->GetType<false>();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004809 if (field_type_class != nullptr) {
Mathieu Chartier3398c782016-09-30 10:27:43 -07004810 field_type = &FromClass(field->GetTypeDescriptor(),
Mathieu Chartier1cc62e42016-10-03 18:01:28 -07004811 field_type_class.Ptr(),
Andreas Gampef23f33d2015-06-23 14:18:17 -07004812 field_type_class->CannotBeAssignedFromOtherTypes());
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08004813 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004814 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
4815 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004816 }
Ian Rogers0d604842012-04-16 14:50:24 -07004817 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004818 if (field_type == nullptr) {
4819 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
4820 const char* descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
Mathieu Chartierbf99f772014-08-23 16:37:27 -07004821 field_type = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004822 }
Sebastien Hertz757b3042014-03-28 14:34:28 +01004823 DCHECK(field_type != nullptr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02004824 const uint32_t vregA = (is_static) ? inst->VRegA_21c() : inst->VRegA_22c();
Andreas Gampe896df402014-10-20 22:25:29 -07004825 static_assert(kAccType == FieldAccessType::kAccPut || kAccType == FieldAccessType::kAccGet,
4826 "Unexpected third access type");
4827 if (kAccType == FieldAccessType::kAccPut) {
4828 // sput or iput.
4829 if (is_primitive) {
4830 VerifyPrimitivePut(*field_type, insn_type, vregA);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004831 } else {
David Brazdilca3c8c32016-09-06 14:04:48 +01004832 if (!insn_type.IsAssignableFrom(*field_type, this)) {
Vladimir Marko414000e2015-06-23 17:45:21 +01004833 // If the field type is not a reference, this is a global failure rather than
4834 // a class change failure as the instructions and the descriptors for the type
4835 // should have been consistent within the same file at compile time.
4836 VerifyError error = field_type->IsReferenceTypes() ? VERIFY_ERROR_BAD_CLASS_SOFT
4837 : VERIFY_ERROR_BAD_CLASS_HARD;
David Sehr709b0702016-10-13 09:12:37 -07004838 Fail(error) << "expected field " << ArtField::PrettyField(field)
Vladimir Marko414000e2015-06-23 17:45:21 +01004839 << " to be compatible with type '" << insn_type
4840 << "' but found type '" << *field_type
4841 << "' in put-object";
Andreas Gampe896df402014-10-20 22:25:29 -07004842 return;
4843 }
4844 work_line_->VerifyRegisterType(this, vregA, *field_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004845 }
Andreas Gampe896df402014-10-20 22:25:29 -07004846 } else if (kAccType == FieldAccessType::kAccGet) {
4847 // sget or iget.
4848 if (is_primitive) {
4849 if (field_type->Equals(insn_type) ||
4850 (field_type->IsFloat() && insn_type.IsInteger()) ||
4851 (field_type->IsDouble() && insn_type.IsLong())) {
4852 // expected that read is of the correct primitive type or that int reads are reading
4853 // floats or long reads are reading doubles
4854 } else {
4855 // This is a global failure rather than a class change failure as the instructions and
4856 // the descriptors for the type should have been consistent within the same file at
4857 // compile time
David Sehr709b0702016-10-13 09:12:37 -07004858 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << ArtField::PrettyField(field)
Andreas Gampe896df402014-10-20 22:25:29 -07004859 << " to be of type '" << insn_type
4860 << "' but found type '" << *field_type << "' in get";
4861 return;
4862 }
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08004863 } else {
David Brazdilca3c8c32016-09-06 14:04:48 +01004864 if (!insn_type.IsAssignableFrom(*field_type, this)) {
Vladimir Marko414000e2015-06-23 17:45:21 +01004865 // If the field type is not a reference, this is a global failure rather than
4866 // a class change failure as the instructions and the descriptors for the type
4867 // should have been consistent within the same file at compile time.
4868 VerifyError error = field_type->IsReferenceTypes() ? VERIFY_ERROR_BAD_CLASS_SOFT
4869 : VERIFY_ERROR_BAD_CLASS_HARD;
David Sehr709b0702016-10-13 09:12:37 -07004870 Fail(error) << "expected field " << ArtField::PrettyField(field)
Vladimir Marko414000e2015-06-23 17:45:21 +01004871 << " to be compatible with type '" << insn_type
4872 << "' but found type '" << *field_type
4873 << "' in get-object";
Andreas Gampe890da292015-07-06 17:20:18 -07004874 if (error != VERIFY_ERROR_BAD_CLASS_HARD) {
Andreas Gampead238ce2015-08-24 21:13:08 -07004875 work_line_->SetRegisterType<LockOp::kClear>(this, vregA, reg_types_.Conflict());
Andreas Gampe890da292015-07-06 17:20:18 -07004876 }
Andreas Gampe896df402014-10-20 22:25:29 -07004877 return;
4878 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004879 }
Andreas Gampe896df402014-10-20 22:25:29 -07004880 if (!field_type->IsLowHalf()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07004881 work_line_->SetRegisterType<LockOp::kClear>(this, vregA, *field_type);
Andreas Gampe896df402014-10-20 22:25:29 -07004882 } else {
4883 work_line_->SetRegisterTypeWide(this, vregA, *field_type, field_type->HighHalf(&reg_types_));
4884 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07004885 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004886 LOG(FATAL) << "Unexpected case.";
Ian Rogersd81871c2011-10-03 13:57:23 -07004887 }
4888}
4889
Mathieu Chartierc7853442015-03-27 14:35:38 -07004890ArtField* MethodVerifier::GetQuickFieldAccess(const Instruction* inst,
Ian Rogers98379392014-02-24 16:53:16 -08004891 RegisterLine* reg_line) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08004892 DCHECK(IsInstructionIGetQuickOrIPutQuick(inst->Opcode())) << inst->Opcode();
Ian Rogers7b078e82014-09-10 14:44:24 -07004893 const RegType& object_type = reg_line->GetRegisterType(this, inst->VRegB_22c());
Ian Rogers9bc54402014-04-17 16:40:01 -07004894 if (!object_type.HasClass()) {
4895 VLOG(verifier) << "Failed to get mirror::Class* from '" << object_type << "'";
4896 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004897 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004898 uint32_t field_offset = static_cast<uint32_t>(inst->VRegC_22c());
Mathieu Chartierc7853442015-03-27 14:35:38 -07004899 ArtField* const f = ArtField::FindInstanceFieldWithOffset(object_type.GetClass(), field_offset);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08004900 DCHECK_EQ(f->GetOffset().Uint32Value(), field_offset);
Sebastien Hertz479fc1e2014-04-04 17:51:34 +02004901 if (f == nullptr) {
4902 VLOG(verifier) << "Failed to find instance field at offset '" << field_offset
David Sehr709b0702016-10-13 09:12:37 -07004903 << "' from '" << mirror::Class::PrettyDescriptor(object_type.GetClass()) << "'";
Sebastien Hertz479fc1e2014-04-04 17:51:34 +02004904 }
4905 return f;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004906}
4907
Andreas Gampe896df402014-10-20 22:25:29 -07004908template <MethodVerifier::FieldAccessType kAccType>
4909void MethodVerifier::VerifyQuickFieldAccess(const Instruction* inst, const RegType& insn_type,
4910 bool is_primitive) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07004911 DCHECK(Runtime::Current()->IsStarted() || verify_to_dump_);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004912
Mathieu Chartierc7853442015-03-27 14:35:38 -07004913 ArtField* field = GetQuickFieldAccess(inst, work_line_.get());
Ian Rogers7b078e82014-09-10 14:44:24 -07004914 if (field == nullptr) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004915 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field from " << inst->Name();
4916 return;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004917 }
Andreas Gampe896df402014-10-20 22:25:29 -07004918
4919 // For an IPUT_QUICK, we now test for final flag of the field.
4920 if (kAccType == FieldAccessType::kAccPut) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004921 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
David Sehr709b0702016-10-13 09:12:37 -07004922 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << field->PrettyField()
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004923 << " from other class " << GetDeclaringClass();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004924 return;
4925 }
4926 }
Andreas Gampe896df402014-10-20 22:25:29 -07004927
4928 // Get the field type.
4929 const RegType* field_type;
4930 {
Mathieu Chartier3398c782016-09-30 10:27:43 -07004931 ObjPtr<mirror::Class> field_type_class = can_load_classes_ ? field->GetType<true>() :
Mathieu Chartierc7853442015-03-27 14:35:38 -07004932 field->GetType<false>();
Andreas Gampe896df402014-10-20 22:25:29 -07004933
4934 if (field_type_class != nullptr) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07004935 field_type = &FromClass(field->GetTypeDescriptor(),
Mathieu Chartier1cc62e42016-10-03 18:01:28 -07004936 field_type_class.Ptr(),
Andreas Gampef23f33d2015-06-23 14:18:17 -07004937 field_type_class->CannotBeAssignedFromOtherTypes());
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004938 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004939 Thread* self = Thread::Current();
4940 DCHECK(!can_load_classes_ || self->IsExceptionPending());
4941 self->ClearException();
4942 field_type = &reg_types_.FromDescriptor(field->GetDeclaringClass()->GetClassLoader(),
Mathieu Chartierde40d472015-10-15 17:47:48 -07004943 field->GetTypeDescriptor(),
4944 false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004945 }
Andreas Gampe896df402014-10-20 22:25:29 -07004946 if (field_type == nullptr) {
4947 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field type from " << inst->Name();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004948 return;
4949 }
Andreas Gampe896df402014-10-20 22:25:29 -07004950 }
4951
4952 const uint32_t vregA = inst->VRegA_22c();
4953 static_assert(kAccType == FieldAccessType::kAccPut || kAccType == FieldAccessType::kAccGet,
4954 "Unexpected third access type");
4955 if (kAccType == FieldAccessType::kAccPut) {
4956 if (is_primitive) {
4957 // Primitive field assignability rules are weaker than regular assignability rules
4958 bool instruction_compatible;
4959 bool value_compatible;
4960 const RegType& value_type = work_line_->GetRegisterType(this, vregA);
4961 if (field_type->IsIntegralTypes()) {
4962 instruction_compatible = insn_type.IsIntegralTypes();
4963 value_compatible = value_type.IsIntegralTypes();
4964 } else if (field_type->IsFloat()) {
4965 instruction_compatible = insn_type.IsInteger(); // no [is]put-float, so expect [is]put-int
4966 value_compatible = value_type.IsFloatTypes();
4967 } else if (field_type->IsLong()) {
4968 instruction_compatible = insn_type.IsLong();
4969 value_compatible = value_type.IsLongTypes();
4970 } else if (field_type->IsDouble()) {
4971 instruction_compatible = insn_type.IsLong(); // no [is]put-double, so expect [is]put-long
4972 value_compatible = value_type.IsDoubleTypes();
4973 } else {
4974 instruction_compatible = false; // reference field with primitive store
4975 value_compatible = false; // unused
4976 }
4977 if (!instruction_compatible) {
4978 // This is a global failure rather than a class change failure as the instructions and
4979 // the descriptors for the type should have been consistent within the same file at
4980 // compile time
David Sehr709b0702016-10-13 09:12:37 -07004981 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << ArtField::PrettyField(field)
Andreas Gampe896df402014-10-20 22:25:29 -07004982 << " to be of type '" << insn_type
4983 << "' but found type '" << *field_type
4984 << "' in put";
4985 return;
4986 }
4987 if (!value_compatible) {
4988 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
4989 << " of type " << value_type
4990 << " but expected " << *field_type
David Sehr709b0702016-10-13 09:12:37 -07004991 << " for store to " << ArtField::PrettyField(field) << " in put";
Andreas Gampe896df402014-10-20 22:25:29 -07004992 return;
4993 }
4994 } else {
David Brazdilca3c8c32016-09-06 14:04:48 +01004995 if (!insn_type.IsAssignableFrom(*field_type, this)) {
David Sehr709b0702016-10-13 09:12:37 -07004996 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << ArtField::PrettyField(field)
Andreas Gampe896df402014-10-20 22:25:29 -07004997 << " to be compatible with type '" << insn_type
4998 << "' but found type '" << *field_type
4999 << "' in put-object";
5000 return;
5001 }
5002 work_line_->VerifyRegisterType(this, vregA, *field_type);
5003 }
5004 } else if (kAccType == FieldAccessType::kAccGet) {
5005 if (is_primitive) {
5006 if (field_type->Equals(insn_type) ||
5007 (field_type->IsFloat() && insn_type.IsIntegralTypes()) ||
5008 (field_type->IsDouble() && insn_type.IsLongTypes())) {
5009 // expected that read is of the correct primitive type or that int reads are reading
5010 // floats or long reads are reading doubles
5011 } else {
5012 // This is a global failure rather than a class change failure as the instructions and
5013 // the descriptors for the type should have been consistent within the same file at
5014 // compile time
David Sehr709b0702016-10-13 09:12:37 -07005015 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << ArtField::PrettyField(field)
Andreas Gampe896df402014-10-20 22:25:29 -07005016 << " to be of type '" << insn_type
5017 << "' but found type '" << *field_type << "' in Get";
5018 return;
5019 }
5020 } else {
David Brazdilca3c8c32016-09-06 14:04:48 +01005021 if (!insn_type.IsAssignableFrom(*field_type, this)) {
David Sehr709b0702016-10-13 09:12:37 -07005022 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << ArtField::PrettyField(field)
Andreas Gampe896df402014-10-20 22:25:29 -07005023 << " to be compatible with type '" << insn_type
5024 << "' but found type '" << *field_type
5025 << "' in get-object";
Andreas Gampead238ce2015-08-24 21:13:08 -07005026 work_line_->SetRegisterType<LockOp::kClear>(this, vregA, reg_types_.Conflict());
Andreas Gampe896df402014-10-20 22:25:29 -07005027 return;
5028 }
5029 }
5030 if (!field_type->IsLowHalf()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07005031 work_line_->SetRegisterType<LockOp::kClear>(this, vregA, *field_type);
Andreas Gampe896df402014-10-20 22:25:29 -07005032 } else {
5033 work_line_->SetRegisterTypeWide(this, vregA, *field_type, field_type->HighHalf(&reg_types_));
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02005034 }
5035 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07005036 LOG(FATAL) << "Unexpected case.";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02005037 }
5038}
5039
Ian Rogers776ac1f2012-04-13 23:36:36 -07005040bool MethodVerifier::CheckNotMoveException(const uint16_t* insns, int insn_idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07005041 if ((insns[insn_idx] & 0xff) == Instruction::MOVE_EXCEPTION) {
jeffhaod5347e02012-03-22 17:25:05 -07005042 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-exception";
Ian Rogersd81871c2011-10-03 13:57:23 -07005043 return false;
5044 }
5045 return true;
5046}
5047
Stephen Kyle9bc61992014-09-22 13:53:15 +01005048bool MethodVerifier::CheckNotMoveResult(const uint16_t* insns, int insn_idx) {
5049 if (((insns[insn_idx] & 0xff) >= Instruction::MOVE_RESULT) &&
5050 ((insns[insn_idx] & 0xff) <= Instruction::MOVE_RESULT_OBJECT)) {
5051 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-result*";
5052 return false;
5053 }
5054 return true;
5055}
5056
5057bool MethodVerifier::CheckNotMoveExceptionOrMoveResult(const uint16_t* insns, int insn_idx) {
5058 return (CheckNotMoveException(insns, insn_idx) && CheckNotMoveResult(insns, insn_idx));
5059}
5060
Ian Rogersebbdd872014-07-07 23:53:08 -07005061bool MethodVerifier::UpdateRegisters(uint32_t next_insn, RegisterLine* merge_line,
5062 bool update_merge_line) {
Ian Rogersd81871c2011-10-03 13:57:23 -07005063 bool changed = true;
5064 RegisterLine* target_line = reg_table_.GetLine(next_insn);
Mathieu Chartierde40d472015-10-15 17:47:48 -07005065 if (!GetInstructionFlags(next_insn).IsVisitedOrChanged()) {
jeffhaobdb76512011-09-07 11:43:16 -07005066 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07005067 * We haven't processed this instruction before, and we haven't touched the registers here, so
5068 * there's nothing to "merge". Copy the registers over and mark it as changed. (This is the
5069 * only way a register can transition out of "unknown", so this is not just an optimization.)
jeffhaobdb76512011-09-07 11:43:16 -07005070 */
Andreas Gampea727e372015-08-25 09:22:37 -07005071 target_line->CopyFromLine(merge_line);
Mathieu Chartierde40d472015-10-15 17:47:48 -07005072 if (GetInstructionFlags(next_insn).IsReturn()) {
Jeff Haob24b4a72013-07-31 13:47:31 -07005073 // Verify that the monitor stack is empty on return.
Andreas Gampea727e372015-08-25 09:22:37 -07005074 merge_line->VerifyMonitorStackEmpty(this);
5075
Ian Rogersb8c78592013-07-25 23:52:52 +00005076 // For returns we only care about the operand to the return, all other registers are dead.
5077 // Initialize them as conflicts so they don't add to GC and deoptimization information.
5078 const Instruction* ret_inst = Instruction::At(code_item_->insns_ + next_insn);
Andreas Gampea727e372015-08-25 09:22:37 -07005079 AdjustReturnLine(this, ret_inst, target_line);
Aart Bik31883642016-06-06 15:02:44 -07005080 // Directly bail if a hard failure was found.
Aart Bikb0526322016-06-01 14:06:00 -07005081 if (have_pending_hard_failure_) {
5082 return false;
5083 }
Ian Rogersb8c78592013-07-25 23:52:52 +00005084 }
jeffhaobdb76512011-09-07 11:43:16 -07005085 } else {
Mathieu Chartier361e04a2016-02-16 14:06:35 -08005086 RegisterLineArenaUniquePtr copy;
Andreas Gampeec6e6c12015-11-05 20:39:56 -08005087 if (kDebugVerify) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07005088 copy.reset(RegisterLine::Create(target_line->NumRegs(), this));
Ian Rogers7b0c5b42012-02-16 15:29:07 -08005089 copy->CopyFromLine(target_line);
5090 }
Ian Rogers7b078e82014-09-10 14:44:24 -07005091 changed = target_line->MergeRegisters(this, merge_line);
Ian Rogersad0b3a32012-04-16 14:50:24 -07005092 if (have_pending_hard_failure_) {
Ian Rogersd81871c2011-10-03 13:57:23 -07005093 return false;
jeffhaobdb76512011-09-07 11:43:16 -07005094 }
Andreas Gampeec6e6c12015-11-05 20:39:56 -08005095 if (kDebugVerify && changed) {
Elliott Hughes398f64b2012-03-26 18:05:48 -07005096 LogVerifyInfo() << "Merging at [" << reinterpret_cast<void*>(work_insn_idx_) << "]"
Elliott Hughesc073b072012-05-24 19:29:17 -07005097 << " to [" << reinterpret_cast<void*>(next_insn) << "]: " << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07005098 << copy->Dump(this) << " MERGE\n"
5099 << merge_line->Dump(this) << " ==\n"
5100 << target_line->Dump(this) << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07005101 }
Ian Rogersebbdd872014-07-07 23:53:08 -07005102 if (update_merge_line && changed) {
5103 merge_line->CopyFromLine(target_line);
5104 }
jeffhaobdb76512011-09-07 11:43:16 -07005105 }
Ian Rogersd81871c2011-10-03 13:57:23 -07005106 if (changed) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07005107 GetInstructionFlags(next_insn).SetChanged();
jeffhaobdb76512011-09-07 11:43:16 -07005108 }
5109 return true;
5110}
5111
Ian Rogers7b3ddd22013-02-21 15:19:52 -08005112InstructionFlags* MethodVerifier::CurrentInsnFlags() {
Mathieu Chartierde40d472015-10-15 17:47:48 -07005113 return &GetInstructionFlags(work_insn_idx_);
Ian Rogers776ac1f2012-04-13 23:36:36 -07005114}
5115
Ian Rogersd8f69b02014-09-10 21:43:52 +00005116const RegType& MethodVerifier::GetMethodReturnType() {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07005117 if (return_type_ == nullptr) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07005118 if (mirror_method_ != nullptr) {
Andreas Gampe542451c2016-07-26 09:02:02 -07005119 PointerSize pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
Vladimir Marko05792b92015-08-03 11:56:49 +01005120 mirror::Class* return_type_class = mirror_method_->GetReturnType(can_load_classes_,
5121 pointer_size);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07005122 if (return_type_class != nullptr) {
Andreas Gampef23f33d2015-06-23 14:18:17 -07005123 return_type_ = &FromClass(mirror_method_->GetReturnTypeDescriptor(),
5124 return_type_class,
5125 return_type_class->CannotBeAssignedFromOtherTypes());
Ian Rogersfc0e94b2013-09-23 23:51:32 -07005126 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07005127 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
5128 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07005129 }
5130 }
5131 if (return_type_ == nullptr) {
5132 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
5133 const DexFile::ProtoId& proto_id = dex_file_->GetMethodPrototype(method_id);
Andreas Gampea5b09a62016-11-17 15:21:22 -08005134 dex::TypeIndex return_type_idx = proto_id.return_type_idx_;
Ian Rogersfc0e94b2013-09-23 23:51:32 -07005135 const char* descriptor = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(return_type_idx));
Mathieu Chartierbf99f772014-08-23 16:37:27 -07005136 return_type_ = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07005137 }
5138 }
5139 return *return_type_;
Ian Rogersad0b3a32012-04-16 14:50:24 -07005140}
5141
Ian Rogersd8f69b02014-09-10 21:43:52 +00005142const RegType& MethodVerifier::GetDeclaringClass() {
Ian Rogers7b078e82014-09-10 14:44:24 -07005143 if (declaring_class_ == nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08005144 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
Brian Carlstrom93c33962013-07-26 10:37:43 -07005145 const char* descriptor
5146 = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(method_id.class_idx_));
Mathieu Chartiere401d142015-04-22 13:56:20 -07005147 if (mirror_method_ != nullptr) {
Ian Rogers637c65b2013-05-31 11:46:00 -07005148 mirror::Class* klass = mirror_method_->GetDeclaringClass();
Mathieu Chartierde40d472015-10-15 17:47:48 -07005149 declaring_class_ = &FromClass(descriptor, klass, klass->CannotBeAssignedFromOtherTypes());
Ian Rogers637c65b2013-05-31 11:46:00 -07005150 } else {
Mathieu Chartierbf99f772014-08-23 16:37:27 -07005151 declaring_class_ = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers637c65b2013-05-31 11:46:00 -07005152 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07005153 }
Ian Rogers637c65b2013-05-31 11:46:00 -07005154 return *declaring_class_;
Ian Rogersad0b3a32012-04-16 14:50:24 -07005155}
5156
Ian Rogers2bcb4a42012-11-08 10:39:18 -08005157std::vector<int32_t> MethodVerifier::DescribeVRegs(uint32_t dex_pc) {
5158 RegisterLine* line = reg_table_.GetLine(dex_pc);
Sebastien Hertzaa0c00c2014-03-14 17:58:54 +01005159 DCHECK(line != nullptr) << "No register line at DEX pc " << StringPrintf("0x%x", dex_pc);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08005160 std::vector<int32_t> result;
5161 for (size_t i = 0; i < line->NumRegs(); ++i) {
Ian Rogers7b078e82014-09-10 14:44:24 -07005162 const RegType& type = line->GetRegisterType(this, i);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08005163 if (type.IsConstant()) {
5164 result.push_back(type.IsPreciseConstant() ? kConstant : kImpreciseConstant);
Ian Rogers7b078e82014-09-10 14:44:24 -07005165 const ConstantType* const_val = down_cast<const ConstantType*>(&type);
5166 result.push_back(const_val->ConstantValue());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08005167 } else if (type.IsConstantLo()) {
5168 result.push_back(type.IsPreciseConstantLo() ? kConstant : kImpreciseConstant);
Ian Rogers7b078e82014-09-10 14:44:24 -07005169 const ConstantType* const_val = down_cast<const ConstantType*>(&type);
5170 result.push_back(const_val->ConstantValueLo());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08005171 } else if (type.IsConstantHi()) {
5172 result.push_back(type.IsPreciseConstantHi() ? kConstant : kImpreciseConstant);
Ian Rogers7b078e82014-09-10 14:44:24 -07005173 const ConstantType* const_val = down_cast<const ConstantType*>(&type);
5174 result.push_back(const_val->ConstantValueHi());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08005175 } else if (type.IsIntegralTypes()) {
5176 result.push_back(kIntVReg);
5177 result.push_back(0);
5178 } else if (type.IsFloat()) {
5179 result.push_back(kFloatVReg);
5180 result.push_back(0);
5181 } else if (type.IsLong()) {
5182 result.push_back(kLongLoVReg);
5183 result.push_back(0);
5184 result.push_back(kLongHiVReg);
5185 result.push_back(0);
5186 ++i;
5187 } else if (type.IsDouble()) {
5188 result.push_back(kDoubleLoVReg);
5189 result.push_back(0);
5190 result.push_back(kDoubleHiVReg);
5191 result.push_back(0);
5192 ++i;
5193 } else if (type.IsUndefined() || type.IsConflict() || type.IsHighHalf()) {
5194 result.push_back(kUndefined);
5195 result.push_back(0);
5196 } else {
Ian Rogers7b3ddd22013-02-21 15:19:52 -08005197 CHECK(type.IsNonZeroReferenceTypes());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08005198 result.push_back(kReferenceVReg);
5199 result.push_back(0);
5200 }
5201 }
5202 return result;
5203}
5204
Ian Rogersd8f69b02014-09-10 21:43:52 +00005205const RegType& MethodVerifier::DetermineCat1Constant(int32_t value, bool precise) {
Sebastien Hertz849600b2013-12-20 10:28:08 +01005206 if (precise) {
5207 // Precise constant type.
5208 return reg_types_.FromCat1Const(value, true);
5209 } else {
5210 // Imprecise constant type.
5211 if (value < -32768) {
5212 return reg_types_.IntConstant();
5213 } else if (value < -128) {
5214 return reg_types_.ShortConstant();
5215 } else if (value < 0) {
5216 return reg_types_.ByteConstant();
5217 } else if (value == 0) {
5218 return reg_types_.Zero();
5219 } else if (value == 1) {
5220 return reg_types_.One();
5221 } else if (value < 128) {
5222 return reg_types_.PosByteConstant();
5223 } else if (value < 32768) {
5224 return reg_types_.PosShortConstant();
5225 } else if (value < 65536) {
5226 return reg_types_.CharConstant();
5227 } else {
5228 return reg_types_.IntConstant();
5229 }
5230 }
5231}
5232
Elliott Hughes0a1038b2012-06-14 16:24:17 -07005233void MethodVerifier::Init() {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08005234 art::verifier::RegTypeCache::Init();
Brian Carlstrome7d856b2012-01-11 18:10:55 -08005235}
5236
Elliott Hughes0a1038b2012-06-14 16:24:17 -07005237void MethodVerifier::Shutdown() {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08005238 verifier::RegTypeCache::ShutDown();
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08005239}
jeffhaod1224c72012-02-29 13:43:08 -08005240
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -07005241void MethodVerifier::VisitStaticRoots(RootVisitor* visitor) {
5242 RegTypeCache::VisitStaticRoots(visitor);
Mathieu Chartier7c438b12014-09-12 17:01:24 -07005243}
5244
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -07005245void MethodVerifier::VisitRoots(RootVisitor* visitor, const RootInfo& root_info) {
5246 reg_types_.VisitRoots(visitor, root_info);
Mathieu Chartierc528dba2013-11-26 12:00:11 -08005247}
5248
Andreas Gampef23f33d2015-06-23 14:18:17 -07005249const RegType& MethodVerifier::FromClass(const char* descriptor,
5250 mirror::Class* klass,
5251 bool precise) {
5252 DCHECK(klass != nullptr);
5253 if (precise && !klass->IsInstantiable() && !klass->IsPrimitive()) {
5254 Fail(VerifyError::VERIFY_ERROR_NO_CLASS) << "Could not create precise reference for "
5255 << "non-instantiable klass " << descriptor;
5256 precise = false;
5257 }
5258 return reg_types_.FromClass(descriptor, klass, precise);
5259}
5260
Ian Rogersd81871c2011-10-03 13:57:23 -07005261} // namespace verifier
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07005262} // namespace art