blob: 7137db87730cb3780bd7adaea61086ee499d5f9a [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"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080044#include "mirror/object-inl.h"
45#include "mirror/object_array-inl.h"
Ian Rogers7b078e82014-09-10 14:44:24 -070046#include "reg_type-inl.h"
Ian Rogers39ebcb82013-05-30 16:57:23 -070047#include "register_line-inl.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070048#include "runtime.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070049#include "scoped_thread_state_change-inl.h"
Vladimir Marko80afd022015-05-19 18:08:00 +010050#include "utils.h"
David Brazdilca3c8c32016-09-06 14:04:48 +010051#include "verifier_deps.h"
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070052#include "handle_scope-inl.h"
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070053
54namespace art {
Ian Rogersd81871c2011-10-03 13:57:23 -070055namespace verifier {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070056
Mathieu Chartier8e219ae2014-08-19 14:29:46 -070057static constexpr bool kTimeVerifyMethod = !kIsDebugBuild;
Andreas Gampeec6e6c12015-11-05 20:39:56 -080058static constexpr bool kDebugVerify = false;
Anwar Ghuloum75a43f12013-08-13 17:22:14 -070059// TODO: Add a constant to method_verifier to turn on verbose logging?
Ian Rogers2c8a8572011-10-24 17:11:36 -070060
Andreas Gampeebf850c2015-08-14 15:37:35 -070061// On VLOG(verifier), should we dump the whole state when we run into a hard failure?
62static constexpr bool kDumpRegLinesOnHardFailureIfVLOG = true;
63
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -080064// We print a warning blurb about "dx --no-optimize" when we find monitor-locking issues. Make
65// sure we only print this once.
66static bool gPrintedDxMonitorText = false;
67
Mathieu Chartierde40d472015-10-15 17:47:48 -070068PcToRegisterLineTable::PcToRegisterLineTable(ScopedArenaAllocator& arena)
69 : register_lines_(arena.Adapter(kArenaAllocVerifier)) {}
70
Ian Rogers7b3ddd22013-02-21 15:19:52 -080071void PcToRegisterLineTable::Init(RegisterTrackingMode mode, InstructionFlags* flags,
Ian Rogersd81871c2011-10-03 13:57:23 -070072 uint32_t insns_size, uint16_t registers_size,
Ian Rogers776ac1f2012-04-13 23:36:36 -070073 MethodVerifier* verifier) {
Ian Rogersd81871c2011-10-03 13:57:23 -070074 DCHECK_GT(insns_size, 0U);
Mathieu Chartierde40d472015-10-15 17:47:48 -070075 register_lines_.resize(insns_size);
Ian Rogersd81871c2011-10-03 13:57:23 -070076 for (uint32_t i = 0; i < insns_size; i++) {
77 bool interesting = false;
78 switch (mode) {
79 case kTrackRegsAll:
80 interesting = flags[i].IsOpcode();
81 break;
Sameer Abu Asal02c42232013-04-30 12:09:45 -070082 case kTrackCompilerInterestPoints:
Brian Carlstrom02c8cc62013-07-18 15:54:44 -070083 interesting = flags[i].IsCompileTimeInfoPoint() || flags[i].IsBranchTarget();
Ian Rogersd81871c2011-10-03 13:57:23 -070084 break;
85 case kTrackRegsBranches:
86 interesting = flags[i].IsBranchTarget();
87 break;
88 default:
89 break;
90 }
91 if (interesting) {
Mathieu Chartierde40d472015-10-15 17:47:48 -070092 register_lines_[i].reset(RegisterLine::Create(registers_size, verifier));
Ian Rogersd0fbd852013-09-24 18:17:04 -070093 }
94 }
95}
96
Mathieu Chartierde40d472015-10-15 17:47:48 -070097PcToRegisterLineTable::~PcToRegisterLineTable() {}
Ian Rogersd81871c2011-10-03 13:57:23 -070098
Andreas Gampe7c038102014-10-27 20:08:46 -070099// Note: returns true on failure.
100ALWAYS_INLINE static inline bool FailOrAbort(MethodVerifier* verifier, bool condition,
101 const char* error_msg, uint32_t work_insn_idx) {
102 if (kIsDebugBuild) {
Andreas Gampef8f36c22016-11-19 14:51:52 -0800103 // In a debug build, abort if the error condition is wrong. Only warn if
104 // we are already aborting (as this verification is likely run to print
105 // lock information).
106 if (LIKELY(gAborting == 0)) {
107 DCHECK(condition) << error_msg << work_insn_idx;
108 } else {
109 if (!condition) {
110 LOG(ERROR) << error_msg << work_insn_idx;
111 verifier->Fail(VERIFY_ERROR_BAD_CLASS_HARD) << error_msg << work_insn_idx;
112 return true;
113 }
114 }
Andreas Gampe7c038102014-10-27 20:08:46 -0700115 } else {
116 // In a non-debug build, just fail the class.
117 if (!condition) {
118 verifier->Fail(VERIFY_ERROR_BAD_CLASS_HARD) << error_msg << work_insn_idx;
119 return true;
120 }
121 }
122
123 return false;
124}
125
Stephen Kyle7e541c92014-12-17 17:10:02 +0000126static void SafelyMarkAllRegistersAsConflicts(MethodVerifier* verifier, RegisterLine* reg_line) {
Andreas Gampef10b6e12015-08-12 10:48:12 -0700127 if (verifier->IsInstanceConstructor()) {
Stephen Kyle7e541c92014-12-17 17:10:02 +0000128 // Before we mark all regs as conflicts, check that we don't have an uninitialized this.
129 reg_line->CheckConstructorReturn(verifier);
130 }
131 reg_line->MarkAllRegistersAsConflicts(verifier);
132}
133
Ian Rogers7b078e82014-09-10 14:44:24 -0700134MethodVerifier::FailureKind MethodVerifier::VerifyClass(Thread* self,
135 mirror::Class* klass,
Andreas Gampe53e32d12015-12-09 21:03:23 -0800136 CompilerCallbacks* callbacks,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700137 bool allow_soft_failures,
Andreas Gampe5fd66d02016-09-12 20:22:19 -0700138 HardFailLogMode log_level,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700139 std::string* error) {
jeffhaobdb76512011-09-07 11:43:16 -0700140 if (klass->IsVerified()) {
jeffhaof1e6b7c2012-06-05 18:33:30 -0700141 return kNoFailure;
jeffhaobdb76512011-09-07 11:43:16 -0700142 }
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800143 bool early_failure = false;
144 std::string failure_message;
Mathieu Chartierf8322842014-05-16 10:59:25 -0700145 const DexFile& dex_file = klass->GetDexFile();
146 const DexFile::ClassDef* class_def = klass->GetClassDef();
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800147 mirror::Class* super = klass->GetSuperClass();
Ian Rogers1ff3c982014-08-12 02:30:58 -0700148 std::string temp;
Ian Rogers7b078e82014-09-10 14:44:24 -0700149 if (super == nullptr && strcmp("Ljava/lang/Object;", klass->GetDescriptor(&temp)) != 0) {
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800150 early_failure = true;
151 failure_message = " that has no super class";
Ian Rogers7b078e82014-09-10 14:44:24 -0700152 } else if (super != nullptr && super->IsFinal()) {
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800153 early_failure = true;
David Sehr709b0702016-10-13 09:12:37 -0700154 failure_message = " that attempts to sub-class final class " + super->PrettyDescriptor();
Ian Rogers7b078e82014-09-10 14:44:24 -0700155 } else if (class_def == nullptr) {
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800156 early_failure = true;
157 failure_message = " that isn't present in dex file " + dex_file.GetLocation();
158 }
159 if (early_failure) {
David Sehr709b0702016-10-13 09:12:37 -0700160 *error = "Verifier rejected class " + klass->PrettyDescriptor() + failure_message;
Andreas Gampe53e32d12015-12-09 21:03:23 -0800161 if (callbacks != nullptr) {
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800162 ClassReference ref(&dex_file, klass->GetDexClassDefIndex());
Andreas Gampe53e32d12015-12-09 21:03:23 -0800163 callbacks->ClassRejected(ref);
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800164 }
jeffhaof1e6b7c2012-06-05 18:33:30 -0700165 return kHardFailure;
jeffhaobdb76512011-09-07 11:43:16 -0700166 }
Ian Rogers7b078e82014-09-10 14:44:24 -0700167 StackHandleScope<2> hs(self);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700168 Handle<mirror::DexCache> dex_cache(hs.NewHandle(klass->GetDexCache()));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700169 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(klass->GetClassLoader()));
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800170 return VerifyClass(self,
171 &dex_file,
172 dex_cache,
173 class_loader,
David Brazdil15fc7292016-09-02 14:13:18 +0100174 *class_def,
Andreas Gampe53e32d12015-12-09 21:03:23 -0800175 callbacks,
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800176 allow_soft_failures,
Andreas Gampe7fe30232016-03-25 16:58:00 -0700177 log_level,
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800178 error);
179}
180
181template <bool kDirect>
182static bool HasNextMethod(ClassDataItemIterator* it) {
183 return kDirect ? it->HasNextDirectMethod() : it->HasNextVirtualMethod();
184}
185
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800186static MethodVerifier::FailureKind FailureKindMax(MethodVerifier::FailureKind fk1,
187 MethodVerifier::FailureKind fk2) {
188 static_assert(MethodVerifier::FailureKind::kNoFailure <
189 MethodVerifier::FailureKind::kSoftFailure
190 && MethodVerifier::FailureKind::kSoftFailure <
191 MethodVerifier::FailureKind::kHardFailure,
192 "Unexpected FailureKind order");
193 return std::max(fk1, fk2);
194}
195
196void MethodVerifier::FailureData::Merge(const MethodVerifier::FailureData& fd) {
197 kind = FailureKindMax(kind, fd.kind);
198 types |= fd.types;
199}
200
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800201template <bool kDirect>
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800202MethodVerifier::FailureData MethodVerifier::VerifyMethods(Thread* self,
203 ClassLinker* linker,
204 const DexFile* dex_file,
David Brazdil15fc7292016-09-02 14:13:18 +0100205 const DexFile::ClassDef& class_def,
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800206 ClassDataItemIterator* it,
207 Handle<mirror::DexCache> dex_cache,
208 Handle<mirror::ClassLoader> class_loader,
209 CompilerCallbacks* callbacks,
210 bool allow_soft_failures,
Andreas Gampe5fd66d02016-09-12 20:22:19 -0700211 HardFailLogMode log_level,
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800212 bool need_precise_constants,
213 std::string* error_string) {
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800214 DCHECK(it != nullptr);
215
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800216 MethodVerifier::FailureData failure_data;
217
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800218 int64_t previous_method_idx = -1;
219 while (HasNextMethod<kDirect>(it)) {
220 self->AllowThreadSuspension();
221 uint32_t method_idx = it->GetMemberIndex();
222 if (method_idx == previous_method_idx) {
223 // smali can create dex files with two encoded_methods sharing the same method_idx
224 // http://code.google.com/p/smali/issues/detail?id=119
225 it->Next();
226 continue;
227 }
228 previous_method_idx = method_idx;
David Brazdil15fc7292016-09-02 14:13:18 +0100229 InvokeType type = it->GetMethodInvokeType(class_def);
Andreas Gampe42ef8ab2015-12-03 17:27:32 -0800230 ArtMethod* method = linker->ResolveMethod<ClassLinker::kNoICCECheckForCache>(
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800231 *dex_file, method_idx, dex_cache, class_loader, nullptr, type);
232 if (method == nullptr) {
233 DCHECK(self->IsExceptionPending());
234 // We couldn't resolve the method, but continue regardless.
235 self->ClearException();
236 } else {
237 DCHECK(method->GetDeclaringClassUnchecked() != nullptr) << type;
238 }
239 StackHandleScope<1> hs(self);
240 std::string hard_failure_msg;
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800241 MethodVerifier::FailureData result = VerifyMethod(self,
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800242 method_idx,
243 dex_file,
244 dex_cache,
245 class_loader,
246 class_def,
247 it->GetMethodCodeItem(),
248 method,
249 it->GetMethodAccessFlags(),
Andreas Gampe53e32d12015-12-09 21:03:23 -0800250 callbacks,
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800251 allow_soft_failures,
Andreas Gampe7fe30232016-03-25 16:58:00 -0700252 log_level,
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800253 need_precise_constants,
254 &hard_failure_msg);
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800255 if (result.kind == kHardFailure) {
256 if (failure_data.kind == kHardFailure) {
257 // If we logged an error before, we need a newline.
258 *error_string += "\n";
259 } else {
260 // If we didn't log a hard failure before, print the header of the message.
261 *error_string += "Verifier rejected class ";
David Brazdil15fc7292016-09-02 14:13:18 +0100262 *error_string += PrettyDescriptor(dex_file->GetClassDescriptor(class_def));
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800263 *error_string += ":";
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800264 }
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800265 *error_string += " ";
266 *error_string += hard_failure_msg;
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800267 }
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800268 failure_data.Merge(result);
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800269 it->Next();
270 }
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800271
272 return failure_data;
Shih-wei Liao371814f2011-10-27 16:52:10 -0700273}
274
Ian Rogers7b078e82014-09-10 14:44:24 -0700275MethodVerifier::FailureKind MethodVerifier::VerifyClass(Thread* self,
276 const DexFile* dex_file,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700277 Handle<mirror::DexCache> dex_cache,
278 Handle<mirror::ClassLoader> class_loader,
David Brazdil15fc7292016-09-02 14:13:18 +0100279 const DexFile::ClassDef& class_def,
Andreas Gampe53e32d12015-12-09 21:03:23 -0800280 CompilerCallbacks* callbacks,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700281 bool allow_soft_failures,
Andreas Gampe5fd66d02016-09-12 20:22:19 -0700282 HardFailLogMode log_level,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700283 std::string* error) {
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -0800284 ScopedTrace trace(__FUNCTION__);
Andreas Gampe507cc6f2015-06-19 22:58:47 -0700285
286 // A class must not be abstract and final.
David Brazdil15fc7292016-09-02 14:13:18 +0100287 if ((class_def.access_flags_ & (kAccAbstract | kAccFinal)) == (kAccAbstract | kAccFinal)) {
Andreas Gampe507cc6f2015-06-19 22:58:47 -0700288 *error = "Verifier rejected class ";
David Brazdil15fc7292016-09-02 14:13:18 +0100289 *error += PrettyDescriptor(dex_file->GetClassDescriptor(class_def));
Andreas Gampe507cc6f2015-06-19 22:58:47 -0700290 *error += ": class is abstract and final.";
291 return kHardFailure;
292 }
293
David Brazdil15fc7292016-09-02 14:13:18 +0100294 const uint8_t* class_data = dex_file->GetClassData(class_def);
Ian Rogers7b078e82014-09-10 14:44:24 -0700295 if (class_data == nullptr) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700296 // empty class, probably a marker interface
jeffhaof1e6b7c2012-06-05 18:33:30 -0700297 return kNoFailure;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700298 }
jeffhaof56197c2012-03-05 18:01:54 -0800299 ClassDataItemIterator it(*dex_file, class_data);
300 while (it.HasNextStaticField() || it.HasNextInstanceField()) {
301 it.Next();
302 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700303 ClassLinker* linker = Runtime::Current()->GetClassLinker();
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800304 // Direct methods.
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800305 MethodVerifier::FailureData data1 = VerifyMethods<true>(self,
306 linker,
307 dex_file,
308 class_def,
309 &it,
310 dex_cache,
311 class_loader,
312 callbacks,
313 allow_soft_failures,
Andreas Gampe7fe30232016-03-25 16:58:00 -0700314 log_level,
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800315 false /* need precise constants */,
316 error);
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800317 // Virtual methods.
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800318 MethodVerifier::FailureData data2 = VerifyMethods<false>(self,
319 linker,
320 dex_file,
321 class_def,
322 &it,
323 dex_cache,
324 class_loader,
325 callbacks,
326 allow_soft_failures,
Andreas Gampe7fe30232016-03-25 16:58:00 -0700327 log_level,
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800328 false /* need precise constants */,
329 error);
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800330
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800331 data1.Merge(data2);
332
333 if (data1.kind == kNoFailure) {
jeffhaof1e6b7c2012-06-05 18:33:30 -0700334 return kNoFailure;
335 } else {
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800336 if ((data1.types & VERIFY_ERROR_LOCKING) != 0) {
337 // Print a warning about expected slow-down. Use a string temporary to print one contiguous
338 // warning.
339 std::string tmp =
340 StringPrintf("Class %s failed lock verification and will run slower.",
David Brazdil15fc7292016-09-02 14:13:18 +0100341 PrettyDescriptor(dex_file->GetClassDescriptor(class_def)).c_str());
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800342 if (!gPrintedDxMonitorText) {
343 tmp = tmp + "\nCommon causes for lock verification issues are non-optimized dex code\n"
344 "and incorrect proguard optimizations.";
345 gPrintedDxMonitorText = true;
346 }
347 LOG(WARNING) << tmp;
348 }
349 return data1.kind;
jeffhaof1e6b7c2012-06-05 18:33:30 -0700350 }
jeffhaof56197c2012-03-05 18:01:54 -0800351}
352
Andreas Gampea4f5bf62015-05-18 20:50:12 -0700353static bool IsLargeMethod(const DexFile::CodeItem* const code_item) {
Andreas Gampe3c651fc2015-05-21 14:06:46 -0700354 if (code_item == nullptr) {
355 return false;
356 }
357
Andreas Gampea4f5bf62015-05-18 20:50:12 -0700358 uint16_t registers_size = code_item->registers_size_;
359 uint32_t insns_size = code_item->insns_size_in_code_units_;
360
361 return registers_size * insns_size > 4*1024*1024;
362}
363
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800364MethodVerifier::FailureData MethodVerifier::VerifyMethod(Thread* self,
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800365 uint32_t method_idx,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800366 const DexFile* dex_file,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700367 Handle<mirror::DexCache> dex_cache,
368 Handle<mirror::ClassLoader> class_loader,
David Brazdil15fc7292016-09-02 14:13:18 +0100369 const DexFile::ClassDef& class_def,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800370 const DexFile::CodeItem* code_item,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700371 ArtMethod* method,
Jeff Haoee988952013-04-16 14:23:47 -0700372 uint32_t method_access_flags,
Andreas Gampe53e32d12015-12-09 21:03:23 -0800373 CompilerCallbacks* callbacks,
Ian Rogers46960fe2014-05-23 10:43:43 -0700374 bool allow_soft_failures,
Andreas Gampe5fd66d02016-09-12 20:22:19 -0700375 HardFailLogMode log_level,
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800376 bool need_precise_constants,
377 std::string* hard_failure_msg) {
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800378 MethodVerifier::FailureData result;
Mathieu Chartier8e219ae2014-08-19 14:29:46 -0700379 uint64_t start_ns = kTimeVerifyMethod ? NanoTime() : 0;
Ian Rogersc8982582012-09-07 16:53:25 -0700380
Andreas Gampebf9611f2016-03-25 16:58:00 -0700381 MethodVerifier verifier(self,
382 dex_file,
383 dex_cache,
384 class_loader,
385 class_def,
386 code_item,
387 method_idx,
388 method,
389 method_access_flags,
390 true /* can_load_classes */,
391 allow_soft_failures,
392 need_precise_constants,
393 false /* verify to dump */,
394 true /* allow_thread_suspension */);
Ian Rogers46960fe2014-05-23 10:43:43 -0700395 if (verifier.Verify()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700396 // Verification completed, however failures may be pending that didn't cause the verification
397 // to hard fail.
Ian Rogers46960fe2014-05-23 10:43:43 -0700398 CHECK(!verifier.have_pending_hard_failure_);
Andreas Gampe53e32d12015-12-09 21:03:23 -0800399
400 if (code_item != nullptr && callbacks != nullptr) {
401 // Let the interested party know that the method was verified.
402 callbacks->MethodVerified(&verifier);
403 }
404
Ian Rogers46960fe2014-05-23 10:43:43 -0700405 if (verifier.failures_.size() != 0) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -0700406 if (VLOG_IS_ON(verifier)) {
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800407 verifier.DumpFailures(VLOG_STREAM(verifier) << "Soft verification failures in "
David Sehr709b0702016-10-13 09:12:37 -0700408 << dex_file->PrettyMethod(method_idx) << "\n");
Anwar Ghuloum75a43f12013-08-13 17:22:14 -0700409 }
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800410 result.kind = kSoftFailure;
Nicolas Geoffray250a3782016-04-20 16:27:53 +0100411 if (method != nullptr &&
412 !CanCompilerHandleVerificationFailure(verifier.encountered_failure_types_)) {
413 method->SetAccessFlags(method->GetAccessFlags() | kAccCompileDontBother);
414 }
415 }
Andreas Gampe56fdd0e2016-04-28 14:56:54 -0700416 if (method != nullptr) {
417 if (verifier.HasInstructionThatWillThrow()) {
418 method->SetAccessFlags(method->GetAccessFlags() | kAccCompileDontBother);
419 }
420 if ((verifier.encountered_failure_types_ & VerifyError::VERIFY_ERROR_LOCKING) != 0) {
421 method->SetAccessFlags(method->GetAccessFlags() | kAccMustCountLocks);
422 }
jeffhaof56197c2012-03-05 18:01:54 -0800423 }
424 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700425 // Bad method data.
Ian Rogers46960fe2014-05-23 10:43:43 -0700426 CHECK_NE(verifier.failures_.size(), 0U);
Igor Murashkin4d7b75f2015-07-21 17:03:36 -0700427
428 if (UNLIKELY(verifier.have_pending_experimental_failure_)) {
429 // Failed due to being forced into interpreter. This is ok because
430 // we just want to skip verification.
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800431 result.kind = kSoftFailure;
Igor Murashkin4d7b75f2015-07-21 17:03:36 -0700432 } else {
433 CHECK(verifier.have_pending_hard_failure_);
Andreas Gampe7fe30232016-03-25 16:58:00 -0700434 if (VLOG_IS_ON(verifier)) {
Andreas Gampe5fd66d02016-09-12 20:22:19 -0700435 log_level = std::max(HardFailLogMode::kLogVerbose, log_level);
Andreas Gampe7fe30232016-03-25 16:58:00 -0700436 }
Andreas Gampe5fd66d02016-09-12 20:22:19 -0700437 if (log_level >= HardFailLogMode::kLogVerbose) {
438 LogSeverity severity;
439 switch (log_level) {
440 case HardFailLogMode::kLogVerbose:
441 severity = LogSeverity::VERBOSE;
442 break;
443 case HardFailLogMode::kLogWarning:
444 severity = LogSeverity::WARNING;
445 break;
446 case HardFailLogMode::kLogInternalFatal:
Andreas Gampe3fec9ac2016-09-13 10:47:28 -0700447 severity = LogSeverity::FATAL_WITHOUT_ABORT;
Andreas Gampe5fd66d02016-09-12 20:22:19 -0700448 break;
449 default:
450 LOG(FATAL) << "Unsupported log-level " << static_cast<uint32_t>(log_level);
451 UNREACHABLE();
452 }
Andreas Gampe3fec9ac2016-09-13 10:47:28 -0700453 verifier.DumpFailures(LOG_STREAM(severity) << "Verification error in "
David Sehr709b0702016-10-13 09:12:37 -0700454 << dex_file->PrettyMethod(method_idx)
Andreas Gampe3fec9ac2016-09-13 10:47:28 -0700455 << "\n");
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800456 }
457 if (hard_failure_msg != nullptr) {
458 CHECK(!verifier.failure_messages_.empty());
459 *hard_failure_msg =
460 verifier.failure_messages_[verifier.failure_messages_.size() - 1]->str();
461 }
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800462 result.kind = kHardFailure;
Andreas Gampe53e32d12015-12-09 21:03:23 -0800463
464 if (callbacks != nullptr) {
465 // Let the interested party know that we failed the class.
David Brazdil15fc7292016-09-02 14:13:18 +0100466 ClassReference ref(dex_file, dex_file->GetIndexForClassDef(class_def));
Andreas Gampe53e32d12015-12-09 21:03:23 -0800467 callbacks->ClassRejected(ref);
468 }
Igor Murashkin4d7b75f2015-07-21 17:03:36 -0700469 }
Andreas Gampe53e32d12015-12-09 21:03:23 -0800470 if (VLOG_IS_ON(verifier)) {
Ian Rogers46960fe2014-05-23 10:43:43 -0700471 std::cout << "\n" << verifier.info_messages_.str();
472 verifier.Dump(std::cout);
jeffhaof56197c2012-03-05 18:01:54 -0800473 }
jeffhaof56197c2012-03-05 18:01:54 -0800474 }
Mathieu Chartier8e219ae2014-08-19 14:29:46 -0700475 if (kTimeVerifyMethod) {
476 uint64_t duration_ns = NanoTime() - start_ns;
477 if (duration_ns > MsToNs(100)) {
David Sehr709b0702016-10-13 09:12:37 -0700478 LOG(WARNING) << "Verification of " << dex_file->PrettyMethod(method_idx)
Andreas Gampea4f5bf62015-05-18 20:50:12 -0700479 << " took " << PrettyDuration(duration_ns)
480 << (IsLargeMethod(code_item) ? " (large method)" : "");
Mathieu Chartier8e219ae2014-08-19 14:29:46 -0700481 }
Ian Rogersc8982582012-09-07 16:53:25 -0700482 }
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800483 result.types = verifier.encountered_failure_types_;
Ian Rogersc8982582012-09-07 16:53:25 -0700484 return result;
jeffhaof56197c2012-03-05 18:01:54 -0800485}
486
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100487MethodVerifier* MethodVerifier::VerifyMethodAndDump(Thread* self,
488 VariableIndentationOutputStream* vios,
489 uint32_t dex_method_idx,
490 const DexFile* dex_file,
491 Handle<mirror::DexCache> dex_cache,
492 Handle<mirror::ClassLoader> class_loader,
David Brazdil15fc7292016-09-02 14:13:18 +0100493 const DexFile::ClassDef& class_def,
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100494 const DexFile::CodeItem* code_item,
495 ArtMethod* method,
496 uint32_t method_access_flags) {
Andreas Gampebf9611f2016-03-25 16:58:00 -0700497 MethodVerifier* verifier = new MethodVerifier(self,
498 dex_file,
499 dex_cache,
500 class_loader,
501 class_def,
502 code_item,
503 dex_method_idx,
504 method,
505 method_access_flags,
506 true /* can_load_classes */,
507 true /* allow_soft_failures */,
508 true /* need_precise_constants */,
509 true /* verify_to_dump */,
510 true /* allow_thread_suspension */);
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700511 verifier->Verify();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100512 verifier->DumpFailures(vios->Stream());
513 vios->Stream() << verifier->info_messages_.str();
Andreas Gampe5cbcde22014-09-16 14:59:49 -0700514 // Only dump and return if no hard failures. Otherwise the verifier may be not fully initialized
515 // and querying any info is dangerous/can abort.
516 if (verifier->have_pending_hard_failure_) {
517 delete verifier;
518 return nullptr;
519 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100520 verifier->Dump(vios);
Andreas Gampe5cbcde22014-09-16 14:59:49 -0700521 return verifier;
522 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800523}
524
Ian Rogers7b078e82014-09-10 14:44:24 -0700525MethodVerifier::MethodVerifier(Thread* self,
Andreas Gampe53e32d12015-12-09 21:03:23 -0800526 const DexFile* dex_file,
527 Handle<mirror::DexCache> dex_cache,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700528 Handle<mirror::ClassLoader> class_loader,
David Brazdil15fc7292016-09-02 14:13:18 +0100529 const DexFile::ClassDef& class_def,
Andreas Gampe53e32d12015-12-09 21:03:23 -0800530 const DexFile::CodeItem* code_item,
531 uint32_t dex_method_idx,
532 ArtMethod* method,
533 uint32_t method_access_flags,
534 bool can_load_classes,
535 bool allow_soft_failures,
536 bool need_precise_constants,
537 bool verify_to_dump,
Mathieu Chartier4306ef82014-12-19 18:41:47 -0800538 bool allow_thread_suspension)
Ian Rogers7b078e82014-09-10 14:44:24 -0700539 : self_(self),
Mathieu Chartierde40d472015-10-15 17:47:48 -0700540 arena_stack_(Runtime::Current()->GetArenaPool()),
541 arena_(&arena_stack_),
542 reg_types_(can_load_classes, arena_),
543 reg_table_(arena_),
Andreas Gamped5ad72f2015-06-26 17:33:47 -0700544 work_insn_idx_(DexFile::kDexNoIndex),
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800545 dex_method_idx_(dex_method_idx),
Ian Rogers637c65b2013-05-31 11:46:00 -0700546 mirror_method_(method),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700547 method_access_flags_(method_access_flags),
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700548 return_type_(nullptr),
jeffhaof56197c2012-03-05 18:01:54 -0800549 dex_file_(dex_file),
550 dex_cache_(dex_cache),
551 class_loader_(class_loader),
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700552 class_def_(class_def),
jeffhaof56197c2012-03-05 18:01:54 -0800553 code_item_(code_item),
Ian Rogers7b078e82014-09-10 14:44:24 -0700554 declaring_class_(nullptr),
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700555 interesting_dex_pc_(-1),
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700556 monitor_enter_dex_pcs_(nullptr),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700557 have_pending_hard_failure_(false),
jeffhaofaf459e2012-08-31 15:32:47 -0700558 have_pending_runtime_throw_failure_(false),
Igor Murashkin4d7b75f2015-07-21 17:03:36 -0700559 have_pending_experimental_failure_(false),
Andreas Gamped12e7822015-06-25 10:26:40 -0700560 have_any_pending_runtime_throw_failure_(false),
jeffhaof56197c2012-03-05 18:01:54 -0800561 new_instance_count_(0),
Elliott Hughes80537bb2013-01-04 16:37:26 -0800562 monitor_enter_count_(0),
Andreas Gampe0760a812015-08-26 17:12:51 -0700563 encountered_failure_types_(0),
Jeff Haoee988952013-04-16 14:23:47 -0700564 can_load_classes_(can_load_classes),
Sebastien Hertz4d4adb12013-07-24 16:14:19 +0200565 allow_soft_failures_(allow_soft_failures),
Ian Rogers46960fe2014-05-23 10:43:43 -0700566 need_precise_constants_(need_precise_constants),
Sebastien Hertz4d4adb12013-07-24 16:14:19 +0200567 has_check_casts_(false),
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700568 has_virtual_or_interface_invokes_(false),
Mathieu Chartier4306ef82014-12-19 18:41:47 -0800569 verify_to_dump_(verify_to_dump),
Andreas Gamped5ad72f2015-06-26 17:33:47 -0700570 allow_thread_suspension_(allow_thread_suspension),
Andreas Gampee6215c02015-08-31 18:54:38 -0700571 is_constructor_(false),
Andreas Gamped5ad72f2015-06-26 17:33:47 -0700572 link_(nullptr) {
Mathieu Chartierd0ad2ee2015-03-31 14:59:59 -0700573 self->PushVerifier(this);
jeffhaof56197c2012-03-05 18:01:54 -0800574}
575
Mathieu Chartier590fee92013-09-13 13:46:47 -0700576MethodVerifier::~MethodVerifier() {
Mathieu Chartierd0ad2ee2015-03-31 14:59:59 -0700577 Thread::Current()->PopVerifier(this);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700578 STLDeleteElements(&failure_messages_);
579}
580
Mathieu Chartiere401d142015-04-22 13:56:20 -0700581void MethodVerifier::FindLocksAtDexPc(ArtMethod* m, uint32_t dex_pc,
Ian Rogers46960fe2014-05-23 10:43:43 -0700582 std::vector<uint32_t>* monitor_enter_dex_pcs) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700583 StackHandleScope<2> hs(Thread::Current());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700584 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache()));
585 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader()));
Andreas Gampebf9611f2016-03-25 16:58:00 -0700586 MethodVerifier verifier(hs.Self(),
587 m->GetDexFile(),
588 dex_cache,
589 class_loader,
David Brazdil15fc7292016-09-02 14:13:18 +0100590 m->GetClassDef(),
Andreas Gampebf9611f2016-03-25 16:58:00 -0700591 m->GetCodeItem(),
592 m->GetDexMethodIndex(),
593 m,
594 m->GetAccessFlags(),
595 false /* can_load_classes */,
596 true /* allow_soft_failures */,
597 false /* need_precise_constants */,
598 false /* verify_to_dump */,
599 false /* allow_thread_suspension */);
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700600 verifier.interesting_dex_pc_ = dex_pc;
Ian Rogers46960fe2014-05-23 10:43:43 -0700601 verifier.monitor_enter_dex_pcs_ = monitor_enter_dex_pcs;
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700602 verifier.FindLocksAtDexPc();
603}
604
Andreas Gampecb3c08f2014-09-18 13:16:38 -0700605static bool HasMonitorEnterInstructions(const DexFile::CodeItem* const code_item) {
606 const Instruction* inst = Instruction::At(code_item->insns_);
607
608 uint32_t insns_size = code_item->insns_size_in_code_units_;
609 for (uint32_t dex_pc = 0; dex_pc < insns_size;) {
610 if (inst->Opcode() == Instruction::MONITOR_ENTER) {
611 return true;
612 }
613
614 dex_pc += inst->SizeInCodeUnits();
615 inst = inst->Next();
616 }
617
618 return false;
619}
620
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700621void MethodVerifier::FindLocksAtDexPc() {
Ian Rogers7b078e82014-09-10 14:44:24 -0700622 CHECK(monitor_enter_dex_pcs_ != nullptr);
623 CHECK(code_item_ != nullptr); // This only makes sense for methods with code.
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700624
Andreas Gampecb3c08f2014-09-18 13:16:38 -0700625 // Quick check whether there are any monitor_enter instructions at all.
626 if (!HasMonitorEnterInstructions(code_item_)) {
627 return;
628 }
629
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700630 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
631 // verification. In practice, the phase we want relies on data structures set up by all the
632 // earlier passes, so we just run the full method verification and bail out early when we've
633 // got what we wanted.
634 Verify();
635}
636
Mathieu Chartiere401d142015-04-22 13:56:20 -0700637ArtField* MethodVerifier::FindAccessedFieldAtDexPc(ArtMethod* m, uint32_t dex_pc) {
638 StackHandleScope<2> hs(Thread::Current());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700639 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache()));
640 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader()));
Andreas Gampebf9611f2016-03-25 16:58:00 -0700641 MethodVerifier verifier(hs.Self(),
642 m->GetDexFile(),
643 dex_cache,
644 class_loader,
David Brazdil15fc7292016-09-02 14:13:18 +0100645 m->GetClassDef(),
Andreas Gampebf9611f2016-03-25 16:58:00 -0700646 m->GetCodeItem(),
647 m->GetDexMethodIndex(),
648 m,
649 m->GetAccessFlags(),
650 true /* can_load_classes */,
651 true /* allow_soft_failures */,
652 false /* need_precise_constants */,
653 false /* verify_to_dump */,
654 true /* allow_thread_suspension */);
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200655 return verifier.FindAccessedFieldAtDexPc(dex_pc);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200656}
657
Mathieu Chartierc7853442015-03-27 14:35:38 -0700658ArtField* MethodVerifier::FindAccessedFieldAtDexPc(uint32_t dex_pc) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700659 CHECK(code_item_ != nullptr); // This only makes sense for methods with code.
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200660
661 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
662 // verification. In practice, the phase we want relies on data structures set up by all the
663 // earlier passes, so we just run the full method verification and bail out early when we've
664 // got what we wanted.
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200665 bool success = Verify();
666 if (!success) {
Ian Rogers9bc54402014-04-17 16:40:01 -0700667 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200668 }
669 RegisterLine* register_line = reg_table_.GetLine(dex_pc);
Ian Rogers7b078e82014-09-10 14:44:24 -0700670 if (register_line == nullptr) {
Ian Rogers9bc54402014-04-17 16:40:01 -0700671 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200672 }
673 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
674 return GetQuickFieldAccess(inst, register_line);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200675}
676
Mathieu Chartiere401d142015-04-22 13:56:20 -0700677ArtMethod* MethodVerifier::FindInvokedMethodAtDexPc(ArtMethod* m, uint32_t dex_pc) {
678 StackHandleScope<2> hs(Thread::Current());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700679 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache()));
680 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader()));
Andreas Gampebf9611f2016-03-25 16:58:00 -0700681 MethodVerifier verifier(hs.Self(),
682 m->GetDexFile(),
683 dex_cache,
684 class_loader,
David Brazdil15fc7292016-09-02 14:13:18 +0100685 m->GetClassDef(),
Andreas Gampebf9611f2016-03-25 16:58:00 -0700686 m->GetCodeItem(),
687 m->GetDexMethodIndex(),
688 m,
689 m->GetAccessFlags(),
690 true /* can_load_classes */,
691 true /* allow_soft_failures */,
692 false /* need_precise_constants */,
693 false /* verify_to_dump */,
694 true /* allow_thread_suspension */);
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200695 return verifier.FindInvokedMethodAtDexPc(dex_pc);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200696}
697
Mathieu Chartiere401d142015-04-22 13:56:20 -0700698ArtMethod* MethodVerifier::FindInvokedMethodAtDexPc(uint32_t dex_pc) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700699 CHECK(code_item_ != nullptr); // This only makes sense for methods with code.
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200700
701 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
702 // verification. In practice, the phase we want relies on data structures set up by all the
703 // earlier passes, so we just run the full method verification and bail out early when we've
704 // got what we wanted.
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200705 bool success = Verify();
706 if (!success) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700707 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200708 }
709 RegisterLine* register_line = reg_table_.GetLine(dex_pc);
Ian Rogers7b078e82014-09-10 14:44:24 -0700710 if (register_line == nullptr) {
711 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200712 }
713 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
714 const bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
Mathieu Chartier091d2382015-03-06 10:59:06 -0800715 return GetQuickInvokedMethod(inst, register_line, is_range, false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200716}
717
Ian Rogersad0b3a32012-04-16 14:50:24 -0700718bool MethodVerifier::Verify() {
Andreas Gampee6215c02015-08-31 18:54:38 -0700719 // Some older code doesn't correctly mark constructors as such. Test for this case by looking at
720 // the name.
721 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
722 const char* method_name = dex_file_->StringDataByIdx(method_id.name_idx_);
723 bool instance_constructor_by_name = strcmp("<init>", method_name) == 0;
724 bool static_constructor_by_name = strcmp("<clinit>", method_name) == 0;
725 bool constructor_by_name = instance_constructor_by_name || static_constructor_by_name;
726 // Check that only constructors are tagged, and check for bad code that doesn't tag constructors.
727 if ((method_access_flags_ & kAccConstructor) != 0) {
728 if (!constructor_by_name) {
729 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
730 << "method is marked as constructor, but not named accordingly";
jeffhaobdb76512011-09-07 11:43:16 -0700731 return false;
Andreas Gampee6215c02015-08-31 18:54:38 -0700732 }
733 is_constructor_ = true;
734 } else if (constructor_by_name) {
David Sehr709b0702016-10-13 09:12:37 -0700735 LOG(WARNING) << "Method " << dex_file_->PrettyMethod(dex_method_idx_)
Andreas Gampee6215c02015-08-31 18:54:38 -0700736 << " not marked as constructor.";
737 is_constructor_ = true;
738 }
739 // If it's a constructor, check whether IsStatic() matches the name.
740 // This should have been rejected by the dex file verifier. Only do in debug build.
741 if (kIsDebugBuild) {
742 if (IsConstructor()) {
743 if (IsStatic() ^ static_constructor_by_name) {
744 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
745 << "constructor name doesn't match static flag";
746 return false;
747 }
jeffhaobdb76512011-09-07 11:43:16 -0700748 }
jeffhaobdb76512011-09-07 11:43:16 -0700749 }
Andreas Gampee6215c02015-08-31 18:54:38 -0700750
751 // Methods may only have one of public/protected/private.
752 // This should have been rejected by the dex file verifier. Only do in debug build.
753 if (kIsDebugBuild) {
754 size_t access_mod_count =
755 (((method_access_flags_ & kAccPublic) == 0) ? 0 : 1) +
756 (((method_access_flags_ & kAccProtected) == 0) ? 0 : 1) +
757 (((method_access_flags_ & kAccPrivate) == 0) ? 0 : 1);
758 if (access_mod_count > 1) {
759 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "method has more than one of public/protected/private";
760 return false;
761 }
762 }
763
764 // If there aren't any instructions, make sure that's expected, then exit successfully.
765 if (code_item_ == nullptr) {
Andreas Gampe76ed99d2016-03-28 18:31:29 -0700766 // Only native or abstract methods may not have code.
767 if ((method_access_flags_ & (kAccNative | kAccAbstract)) == 0) {
768 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "zero-length code in concrete non-native method";
769 return false;
770 }
771
Andreas Gampee6215c02015-08-31 18:54:38 -0700772 // This should have been rejected by the dex file verifier. Only do in debug build.
Andreas Gampe76ed99d2016-03-28 18:31:29 -0700773 // Note: the above will also be rejected in the dex file verifier, starting in dex version 37.
Andreas Gampee6215c02015-08-31 18:54:38 -0700774 if (kIsDebugBuild) {
Andreas Gampee6215c02015-08-31 18:54:38 -0700775 if ((method_access_flags_ & kAccAbstract) != 0) {
776 // Abstract methods are not allowed to have the following flags.
777 static constexpr uint32_t kForbidden =
778 kAccPrivate |
779 kAccStatic |
780 kAccFinal |
781 kAccNative |
782 kAccStrict |
783 kAccSynchronized;
784 if ((method_access_flags_ & kForbidden) != 0) {
785 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
786 << "method can't be abstract and private/static/final/native/strict/synchronized";
787 return false;
788 }
789 }
David Brazdil15fc7292016-09-02 14:13:18 +0100790 if ((class_def_.GetJavaAccessFlags() & kAccInterface) != 0) {
Alex Lighteb7c1442015-08-31 13:17:42 -0700791 // Interface methods must be public and abstract (if default methods are disabled).
Neil Fuller9724c632016-01-07 15:42:47 +0000792 uint32_t kRequired = kAccPublic;
Alex Lighteb7c1442015-08-31 13:17:42 -0700793 if ((method_access_flags_ & kRequired) != kRequired) {
Neil Fuller9724c632016-01-07 15:42:47 +0000794 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface methods must be public";
Andreas Gampee6215c02015-08-31 18:54:38 -0700795 return false;
796 }
797 // In addition to the above, interface methods must not be protected.
798 static constexpr uint32_t kForbidden = kAccProtected;
799 if ((method_access_flags_ & kForbidden) != 0) {
800 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface methods can't be protected";
801 return false;
802 }
803 }
804 // We also don't allow constructors to be abstract or native.
805 if (IsConstructor()) {
806 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "constructors can't be abstract or native";
807 return false;
808 }
809 }
810 return true;
811 }
812
813 // This should have been rejected by the dex file verifier. Only do in debug build.
814 if (kIsDebugBuild) {
815 // When there's code, the method must not be native or abstract.
816 if ((method_access_flags_ & (kAccNative | kAccAbstract)) != 0) {
817 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "non-zero-length code in abstract or native method";
818 return false;
819 }
820
David Brazdil15fc7292016-09-02 14:13:18 +0100821 if ((class_def_.GetJavaAccessFlags() & kAccInterface) != 0) {
Alex Lighteb7c1442015-08-31 13:17:42 -0700822 // Interfaces may always have static initializers for their fields. If we are running with
823 // default methods enabled we also allow other public, static, non-final methods to have code.
824 // Otherwise that is the only type of method allowed.
Alex Light0db36b32015-10-27 14:06:34 -0700825 if (!(IsConstructor() && IsStatic())) {
Neil Fuller9724c632016-01-07 15:42:47 +0000826 if (IsInstanceConstructor()) {
827 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interfaces may not have non-static constructor";
828 return false;
829 } else if (method_access_flags_ & kAccFinal) {
830 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interfaces may not have final methods";
831 return false;
Alex Lightb55f1ac2016-04-12 15:50:55 -0700832 } else {
833 uint32_t access_flag_options = kAccPublic;
834 if (dex_file_->GetVersion() >= DexFile::kDefaultMethodsVersion) {
835 access_flag_options |= kAccPrivate;
836 }
837 if (!(method_access_flags_ & access_flag_options)) {
838 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
839 << "interfaces may not have protected or package-private members";
840 return false;
841 }
Alex Lighteb7c1442015-08-31 13:17:42 -0700842 }
Andreas Gampee6215c02015-08-31 18:54:38 -0700843 }
844 }
845
846 // Instance constructors must not be synchronized.
847 if (IsInstanceConstructor()) {
848 static constexpr uint32_t kForbidden = kAccSynchronized;
849 if ((method_access_flags_ & kForbidden) != 0) {
850 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "constructors can't be synchronized";
851 return false;
852 }
853 }
854 }
855
Ian Rogersd81871c2011-10-03 13:57:23 -0700856 // Sanity-check the register counts. ins + locals = registers, so make sure that ins <= registers.
857 if (code_item_->ins_size_ > code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700858 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad register counts (ins=" << code_item_->ins_size_
859 << " regs=" << code_item_->registers_size_;
Ian Rogersd81871c2011-10-03 13:57:23 -0700860 return false;
jeffhaobdb76512011-09-07 11:43:16 -0700861 }
Alex Lighteb7c1442015-08-31 13:17:42 -0700862
Ian Rogersd81871c2011-10-03 13:57:23 -0700863 // Allocate and initialize an array to hold instruction data.
Mathieu Chartierde40d472015-10-15 17:47:48 -0700864 insn_flags_.reset(arena_.AllocArray<InstructionFlags>(code_item_->insns_size_in_code_units_));
865 DCHECK(insn_flags_ != nullptr);
866 std::uninitialized_fill_n(insn_flags_.get(),
867 code_item_->insns_size_in_code_units_,
868 InstructionFlags());
Ian Rogersd81871c2011-10-03 13:57:23 -0700869 // Run through the instructions and see if the width checks out.
870 bool result = ComputeWidthsAndCountOps();
871 // Flag instructions guarded by a "try" block and check exception handlers.
872 result = result && ScanTryCatchBlocks();
873 // Perform static instruction verification.
874 result = result && VerifyInstructions();
Ian Rogersad0b3a32012-04-16 14:50:24 -0700875 // Perform code-flow analysis and return.
Vladimir Marko2b5eaa22013-12-13 13:59:30 +0000876 result = result && VerifyCodeFlow();
Andreas Gampe53e32d12015-12-09 21:03:23 -0800877
Vladimir Marko2b5eaa22013-12-13 13:59:30 +0000878 return result;
jeffhaoba5ebb92011-08-25 17:24:37 -0700879}
880
Ian Rogers776ac1f2012-04-13 23:36:36 -0700881std::ostream& MethodVerifier::Fail(VerifyError error) {
Andreas Gampe0760a812015-08-26 17:12:51 -0700882 // Mark the error type as encountered.
Andreas Gampea727e372015-08-25 09:22:37 -0700883 encountered_failure_types_ |= static_cast<uint32_t>(error);
Andreas Gampe0760a812015-08-26 17:12:51 -0700884
Ian Rogersad0b3a32012-04-16 14:50:24 -0700885 switch (error) {
886 case VERIFY_ERROR_NO_CLASS:
887 case VERIFY_ERROR_NO_FIELD:
888 case VERIFY_ERROR_NO_METHOD:
889 case VERIFY_ERROR_ACCESS_CLASS:
890 case VERIFY_ERROR_ACCESS_FIELD:
891 case VERIFY_ERROR_ACCESS_METHOD:
Ian Rogers08f753d2012-08-24 14:35:25 -0700892 case VERIFY_ERROR_INSTANTIATION:
893 case VERIFY_ERROR_CLASS_CHANGE:
Igor Murashkin158f35c2015-06-10 15:55:30 -0700894 case VERIFY_ERROR_FORCE_INTERPRETER:
Andreas Gampea727e372015-08-25 09:22:37 -0700895 case VERIFY_ERROR_LOCKING:
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800896 if (Runtime::Current()->IsAotCompiler() || !can_load_classes_) {
jeffhaofaf459e2012-08-31 15:32:47 -0700897 // If we're optimistically running verification at compile time, turn NO_xxx, ACCESS_xxx,
898 // class change and instantiation errors into soft verification errors so that we re-verify
899 // at runtime. We may fail to find or to agree on access because of not yet available class
900 // loaders, or class loaders that will differ at runtime. In these cases, we don't want to
901 // affect the soundness of the code being compiled. Instead, the generated code runs "slow
902 // paths" that dynamically perform the verification and cause the behavior to be that akin
903 // to an interpreter.
904 error = VERIFY_ERROR_BAD_CLASS_SOFT;
905 } else {
Jeff Haoa3faaf42013-09-03 19:07:00 -0700906 // If we fail again at runtime, mark that this instruction would throw and force this
907 // method to be executed using the interpreter with checks.
jeffhaofaf459e2012-08-31 15:32:47 -0700908 have_pending_runtime_throw_failure_ = true;
Andreas Gamped7f8d052015-03-12 11:05:47 -0700909
910 // We need to save the work_line if the instruction wasn't throwing before. Otherwise we'll
911 // try to merge garbage.
912 // Note: this assumes that Fail is called before we do any work_line modifications.
Andreas Gamped5ad72f2015-06-26 17:33:47 -0700913 // Note: this can fail before we touch any instruction, for the signature of a method. So
914 // add a check.
915 if (work_insn_idx_ < DexFile::kDexNoIndex) {
916 const uint16_t* insns = code_item_->insns_ + work_insn_idx_;
917 const Instruction* inst = Instruction::At(insns);
918 int opcode_flags = Instruction::FlagsOf(inst->Opcode());
Andreas Gamped7f8d052015-03-12 11:05:47 -0700919
Andreas Gamped5ad72f2015-06-26 17:33:47 -0700920 if ((opcode_flags & Instruction::kThrow) == 0 && CurrentInsnFlags()->IsInTry()) {
921 saved_line_->CopyFromLine(work_line_.get());
922 }
Andreas Gamped7f8d052015-03-12 11:05:47 -0700923 }
jeffhaofaf459e2012-08-31 15:32:47 -0700924 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700925 break;
Andreas Gampea727e372015-08-25 09:22:37 -0700926
Ian Rogersad0b3a32012-04-16 14:50:24 -0700927 // Indication that verification should be retried at runtime.
928 case VERIFY_ERROR_BAD_CLASS_SOFT:
Jeff Haoee988952013-04-16 14:23:47 -0700929 if (!allow_soft_failures_) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700930 have_pending_hard_failure_ = true;
931 }
932 break;
Andreas Gampea727e372015-08-25 09:22:37 -0700933
jeffhaod5347e02012-03-22 17:25:05 -0700934 // Hard verification failures at compile time will still fail at runtime, so the class is
935 // marked as rejected to prevent it from being compiled.
Ian Rogersad0b3a32012-04-16 14:50:24 -0700936 case VERIFY_ERROR_BAD_CLASS_HARD: {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700937 have_pending_hard_failure_ = true;
Andreas Gampeebf850c2015-08-14 15:37:35 -0700938 if (VLOG_IS_ON(verifier) && kDumpRegLinesOnHardFailureIfVLOG) {
939 ScopedObjectAccess soa(Thread::Current());
940 std::ostringstream oss;
941 Dump(oss);
942 LOG(ERROR) << oss.str();
943 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700944 break;
Ian Rogers47a05882012-02-03 12:23:33 -0800945 }
946 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700947 failures_.push_back(error);
David Sehr709b0702016-10-13 09:12:37 -0700948 std::string location(StringPrintf("%s: [0x%X] ", dex_file_->PrettyMethod(dex_method_idx_).c_str(),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700949 work_insn_idx_));
Elena Sayapina78480ec2014-08-15 15:52:42 +0700950 std::ostringstream* failure_message = new std::ostringstream(location, std::ostringstream::ate);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700951 failure_messages_.push_back(failure_message);
952 return *failure_message;
953}
954
Ian Rogers576ca0c2014-06-06 15:58:22 -0700955std::ostream& MethodVerifier::LogVerifyInfo() {
David Sehr709b0702016-10-13 09:12:37 -0700956 return info_messages_ << "VFY: " << dex_file_->PrettyMethod(dex_method_idx_)
Ian Rogers576ca0c2014-06-06 15:58:22 -0700957 << '[' << reinterpret_cast<void*>(work_insn_idx_) << "] : ";
958}
959
Ian Rogersad0b3a32012-04-16 14:50:24 -0700960void MethodVerifier::PrependToLastFailMessage(std::string prepend) {
961 size_t failure_num = failure_messages_.size();
962 DCHECK_NE(failure_num, 0U);
963 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
964 prepend += last_fail_message->str();
Elena Sayapina78480ec2014-08-15 15:52:42 +0700965 failure_messages_[failure_num - 1] = new std::ostringstream(prepend, std::ostringstream::ate);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700966 delete last_fail_message;
967}
968
Vladimir Marko5c657fe2016-11-03 15:12:29 +0000969void MethodVerifier::AppendToLastFailMessage(const std::string& append) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700970 size_t failure_num = failure_messages_.size();
971 DCHECK_NE(failure_num, 0U);
972 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
973 (*last_fail_message) << append;
Ian Rogers47a05882012-02-03 12:23:33 -0800974}
975
Ian Rogers776ac1f2012-04-13 23:36:36 -0700976bool MethodVerifier::ComputeWidthsAndCountOps() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700977 const uint16_t* insns = code_item_->insns_;
978 size_t insns_size = code_item_->insns_size_in_code_units_;
979 const Instruction* inst = Instruction::At(insns);
jeffhaobdb76512011-09-07 11:43:16 -0700980 size_t new_instance_count = 0;
981 size_t monitor_enter_count = 0;
Ian Rogersd81871c2011-10-03 13:57:23 -0700982 size_t dex_pc = 0;
jeffhaobdb76512011-09-07 11:43:16 -0700983
Ian Rogersd81871c2011-10-03 13:57:23 -0700984 while (dex_pc < insns_size) {
jeffhaobdb76512011-09-07 11:43:16 -0700985 Instruction::Code opcode = inst->Opcode();
Ian Rogersa9a82542013-10-04 11:17:26 -0700986 switch (opcode) {
987 case Instruction::APUT_OBJECT:
988 case Instruction::CHECK_CAST:
989 has_check_casts_ = true;
990 break;
991 case Instruction::INVOKE_VIRTUAL:
992 case Instruction::INVOKE_VIRTUAL_RANGE:
993 case Instruction::INVOKE_INTERFACE:
994 case Instruction::INVOKE_INTERFACE_RANGE:
995 has_virtual_or_interface_invokes_ = true;
996 break;
997 case Instruction::MONITOR_ENTER:
998 monitor_enter_count++;
999 break;
1000 case Instruction::NEW_INSTANCE:
1001 new_instance_count++;
1002 break;
1003 default:
1004 break;
jeffhaobdb76512011-09-07 11:43:16 -07001005 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001006 size_t inst_size = inst->SizeInCodeUnits();
Mathieu Chartierde40d472015-10-15 17:47:48 -07001007 GetInstructionFlags(dex_pc).SetIsOpcode();
Ian Rogersd81871c2011-10-03 13:57:23 -07001008 dex_pc += inst_size;
Ian Rogers7b078e82014-09-10 14:44:24 -07001009 inst = inst->RelativeAt(inst_size);
jeffhaobdb76512011-09-07 11:43:16 -07001010 }
1011
Ian Rogersd81871c2011-10-03 13:57:23 -07001012 if (dex_pc != insns_size) {
jeffhaod5347e02012-03-22 17:25:05 -07001013 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "code did not end where expected ("
1014 << dex_pc << " vs. " << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -07001015 return false;
1016 }
1017
Ian Rogersd81871c2011-10-03 13:57:23 -07001018 new_instance_count_ = new_instance_count;
1019 monitor_enter_count_ = monitor_enter_count;
jeffhaobdb76512011-09-07 11:43:16 -07001020 return true;
1021}
1022
Ian Rogers776ac1f2012-04-13 23:36:36 -07001023bool MethodVerifier::ScanTryCatchBlocks() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001024 uint32_t tries_size = code_item_->tries_size_;
jeffhaobdb76512011-09-07 11:43:16 -07001025 if (tries_size == 0) {
1026 return true;
1027 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001028 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Ian Rogers0571d352011-11-03 19:51:38 -07001029 const DexFile::TryItem* tries = DexFile::GetTryItems(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -07001030
1031 for (uint32_t idx = 0; idx < tries_size; idx++) {
1032 const DexFile::TryItem* try_item = &tries[idx];
1033 uint32_t start = try_item->start_addr_;
1034 uint32_t end = start + try_item->insn_count_;
jeffhaobdb76512011-09-07 11:43:16 -07001035 if ((start >= end) || (start >= insns_size) || (end > insns_size)) {
jeffhaod5347e02012-03-22 17:25:05 -07001036 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad exception entry: startAddr=" << start
1037 << " endAddr=" << end << " (size=" << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -07001038 return false;
1039 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07001040 if (!GetInstructionFlags(start).IsOpcode()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001041 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1042 << "'try' block starts inside an instruction (" << start << ")";
jeffhaobdb76512011-09-07 11:43:16 -07001043 return false;
1044 }
Ian Rogers7b078e82014-09-10 14:44:24 -07001045 uint32_t dex_pc = start;
1046 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
1047 while (dex_pc < end) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07001048 GetInstructionFlags(dex_pc).SetInTry();
Ian Rogers7b078e82014-09-10 14:44:24 -07001049 size_t insn_size = inst->SizeInCodeUnits();
1050 dex_pc += insn_size;
1051 inst = inst->RelativeAt(insn_size);
jeffhaobdb76512011-09-07 11:43:16 -07001052 }
1053 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -08001054 // Iterate over each of the handlers to verify target addresses.
Ian Rogers13735952014-10-08 12:43:28 -07001055 const uint8_t* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -07001056 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
Ian Rogers28ad40d2011-10-27 15:19:26 -07001057 ClassLinker* linker = Runtime::Current()->GetClassLinker();
jeffhaobdb76512011-09-07 11:43:16 -07001058 for (uint32_t idx = 0; idx < handlers_size; idx++) {
Ian Rogers0571d352011-11-03 19:51:38 -07001059 CatchHandlerIterator iterator(handlers_ptr);
1060 for (; iterator.HasNext(); iterator.Next()) {
1061 uint32_t dex_pc= iterator.GetHandlerAddress();
Mathieu Chartierde40d472015-10-15 17:47:48 -07001062 if (!GetInstructionFlags(dex_pc).IsOpcode()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001063 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1064 << "exception handler starts at bad address (" << dex_pc << ")";
jeffhaobdb76512011-09-07 11:43:16 -07001065 return false;
1066 }
Stephen Kyle9bc61992014-09-22 13:53:15 +01001067 if (!CheckNotMoveResult(code_item_->insns_, dex_pc)) {
1068 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1069 << "exception handler begins with move-result* (" << dex_pc << ")";
1070 return false;
1071 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07001072 GetInstructionFlags(dex_pc).SetBranchTarget();
Ian Rogers28ad40d2011-10-27 15:19:26 -07001073 // Ensure exception types are resolved so that they don't need resolution to be delivered,
1074 // unresolved exception types will be ignored by exception delivery
Andreas Gampea5b09a62016-11-17 15:21:22 -08001075 if (iterator.GetHandlerTypeIndex().IsValid()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001076 mirror::Class* exception_type = linker->ResolveType(*dex_file_,
1077 iterator.GetHandlerTypeIndex(),
Mathieu Chartierbf99f772014-08-23 16:37:27 -07001078 dex_cache_, class_loader_);
Ian Rogers7b078e82014-09-10 14:44:24 -07001079 if (exception_type == nullptr) {
1080 DCHECK(self_->IsExceptionPending());
1081 self_->ClearException();
Ian Rogers28ad40d2011-10-27 15:19:26 -07001082 }
1083 }
jeffhaobdb76512011-09-07 11:43:16 -07001084 }
Ian Rogers0571d352011-11-03 19:51:38 -07001085 handlers_ptr = iterator.EndDataPointer();
jeffhaobdb76512011-09-07 11:43:16 -07001086 }
jeffhaobdb76512011-09-07 11:43:16 -07001087 return true;
1088}
1089
Ian Rogers776ac1f2012-04-13 23:36:36 -07001090bool MethodVerifier::VerifyInstructions() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001091 const Instruction* inst = Instruction::At(code_item_->insns_);
jeffhaoba5ebb92011-08-25 17:24:37 -07001092
Ian Rogers0c7abda2012-09-19 13:33:42 -07001093 /* Flag the start of the method as a branch target, and a GC point due to stack overflow errors */
Mathieu Chartierde40d472015-10-15 17:47:48 -07001094 GetInstructionFlags(0).SetBranchTarget();
1095 GetInstructionFlags(0).SetCompileTimeInfoPoint();
Ian Rogersd81871c2011-10-03 13:57:23 -07001096
1097 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Elliott Hughesb25c3f62012-03-26 16:35:06 -07001098 for (uint32_t dex_pc = 0; dex_pc < insns_size;) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001099 if (!VerifyInstruction(inst, dex_pc)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001100 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -07001101 return false;
1102 }
1103 /* Flag instructions that are garbage collection points */
Sameer Abu Asal02c42232013-04-30 12:09:45 -07001104 // All invoke points are marked as "Throw" points already.
1105 // We are relying on this to also count all the invokes as interesting.
Vladimir Marko8b858e12014-11-27 14:52:37 +00001106 if (inst->IsBranch()) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07001107 GetInstructionFlags(dex_pc).SetCompileTimeInfoPoint();
Vladimir Marko8b858e12014-11-27 14:52:37 +00001108 // The compiler also needs safepoints for fall-through to loop heads.
1109 // Such a loop head must be a target of a branch.
1110 int32_t offset = 0;
1111 bool cond, self_ok;
1112 bool target_ok = GetBranchOffset(dex_pc, &offset, &cond, &self_ok);
1113 DCHECK(target_ok);
Mathieu Chartierde40d472015-10-15 17:47:48 -07001114 GetInstructionFlags(dex_pc + offset).SetCompileTimeInfoPoint();
Vladimir Marko8b858e12014-11-27 14:52:37 +00001115 } else if (inst->IsSwitch() || inst->IsThrow()) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07001116 GetInstructionFlags(dex_pc).SetCompileTimeInfoPoint();
Ian Rogersb8c78592013-07-25 23:52:52 +00001117 } else if (inst->IsReturn()) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07001118 GetInstructionFlags(dex_pc).SetCompileTimeInfoPointAndReturn();
Ian Rogersd81871c2011-10-03 13:57:23 -07001119 }
1120 dex_pc += inst->SizeInCodeUnits();
1121 inst = inst->Next();
1122 }
1123 return true;
1124}
1125
Ian Rogers776ac1f2012-04-13 23:36:36 -07001126bool MethodVerifier::VerifyInstruction(const Instruction* inst, uint32_t code_offset) {
Igor Murashkin4d7b75f2015-07-21 17:03:36 -07001127 if (UNLIKELY(inst->IsExperimental())) {
1128 // Experimental instructions don't yet have verifier support implementation.
1129 // While it is possible to use them by themselves, when we try to use stable instructions
1130 // with a virtual register that was created by an experimental instruction,
1131 // the data flow analysis will fail.
1132 Fail(VERIFY_ERROR_FORCE_INTERPRETER)
1133 << "experimental instruction is not supported by verifier; skipping verification";
1134 have_pending_experimental_failure_ = true;
1135 return false;
1136 }
1137
Ian Rogersd81871c2011-10-03 13:57:23 -07001138 bool result = true;
1139 switch (inst->GetVerifyTypeArgumentA()) {
1140 case Instruction::kVerifyRegA:
Ian Rogers29a26482014-05-02 15:27:29 -07001141 result = result && CheckRegisterIndex(inst->VRegA());
Ian Rogersd81871c2011-10-03 13:57:23 -07001142 break;
1143 case Instruction::kVerifyRegAWide:
Ian Rogers29a26482014-05-02 15:27:29 -07001144 result = result && CheckWideRegisterIndex(inst->VRegA());
Ian Rogersd81871c2011-10-03 13:57:23 -07001145 break;
1146 }
1147 switch (inst->GetVerifyTypeArgumentB()) {
1148 case Instruction::kVerifyRegB:
Ian Rogers29a26482014-05-02 15:27:29 -07001149 result = result && CheckRegisterIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -07001150 break;
1151 case Instruction::kVerifyRegBField:
Ian Rogers29a26482014-05-02 15:27:29 -07001152 result = result && CheckFieldIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -07001153 break;
1154 case Instruction::kVerifyRegBMethod:
Ian Rogers29a26482014-05-02 15:27:29 -07001155 result = result && CheckMethodIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -07001156 break;
1157 case Instruction::kVerifyRegBNewInstance:
Andreas Gampea5b09a62016-11-17 15:21:22 -08001158 result = result && CheckNewInstance(dex::TypeIndex(inst->VRegB()));
Ian Rogersd81871c2011-10-03 13:57:23 -07001159 break;
1160 case Instruction::kVerifyRegBString:
Ian Rogers29a26482014-05-02 15:27:29 -07001161 result = result && CheckStringIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -07001162 break;
1163 case Instruction::kVerifyRegBType:
Andreas Gampea5b09a62016-11-17 15:21:22 -08001164 result = result && CheckTypeIndex(dex::TypeIndex(inst->VRegB()));
Ian Rogersd81871c2011-10-03 13:57:23 -07001165 break;
1166 case Instruction::kVerifyRegBWide:
Ian Rogers29a26482014-05-02 15:27:29 -07001167 result = result && CheckWideRegisterIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -07001168 break;
1169 }
1170 switch (inst->GetVerifyTypeArgumentC()) {
1171 case Instruction::kVerifyRegC:
Ian Rogers29a26482014-05-02 15:27:29 -07001172 result = result && CheckRegisterIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -07001173 break;
1174 case Instruction::kVerifyRegCField:
Ian Rogers29a26482014-05-02 15:27:29 -07001175 result = result && CheckFieldIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -07001176 break;
1177 case Instruction::kVerifyRegCNewArray:
Andreas Gampea5b09a62016-11-17 15:21:22 -08001178 result = result && CheckNewArray(dex::TypeIndex(inst->VRegC()));
Ian Rogersd81871c2011-10-03 13:57:23 -07001179 break;
1180 case Instruction::kVerifyRegCType:
Andreas Gampea5b09a62016-11-17 15:21:22 -08001181 result = result && CheckTypeIndex(dex::TypeIndex(inst->VRegC()));
Ian Rogersd81871c2011-10-03 13:57:23 -07001182 break;
1183 case Instruction::kVerifyRegCWide:
Ian Rogers29a26482014-05-02 15:27:29 -07001184 result = result && CheckWideRegisterIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -07001185 break;
1186 }
1187 switch (inst->GetVerifyExtraFlags()) {
1188 case Instruction::kVerifyArrayData:
1189 result = result && CheckArrayData(code_offset);
1190 break;
1191 case Instruction::kVerifyBranchTarget:
1192 result = result && CheckBranchTarget(code_offset);
1193 break;
1194 case Instruction::kVerifySwitchTargets:
1195 result = result && CheckSwitchTargets(code_offset);
1196 break;
Andreas Gampec3314312014-06-19 18:13:29 -07001197 case Instruction::kVerifyVarArgNonZero:
1198 // Fall-through.
Ian Rogers29a26482014-05-02 15:27:29 -07001199 case Instruction::kVerifyVarArg: {
Taiju Tsuiki29498a22015-04-13 14:21:00 +09001200 // Instructions that can actually return a negative value shouldn't have this flag.
1201 uint32_t v_a = dchecked_integral_cast<uint32_t>(inst->VRegA());
1202 if ((inst->GetVerifyExtraFlags() == Instruction::kVerifyVarArgNonZero && v_a == 0) ||
1203 v_a > Instruction::kMaxVarArgRegs) {
1204 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << v_a << ") in "
Andreas Gampec3314312014-06-19 18:13:29 -07001205 "non-range invoke";
1206 return false;
1207 }
Taiju Tsuiki29498a22015-04-13 14:21:00 +09001208
Ian Rogers29a26482014-05-02 15:27:29 -07001209 uint32_t args[Instruction::kMaxVarArgRegs];
1210 inst->GetVarArgs(args);
Taiju Tsuiki29498a22015-04-13 14:21:00 +09001211 result = result && CheckVarArgRegs(v_a, args);
Ian Rogersd81871c2011-10-03 13:57:23 -07001212 break;
Ian Rogers29a26482014-05-02 15:27:29 -07001213 }
Andreas Gampec3314312014-06-19 18:13:29 -07001214 case Instruction::kVerifyVarArgRangeNonZero:
1215 // Fall-through.
Ian Rogersd81871c2011-10-03 13:57:23 -07001216 case Instruction::kVerifyVarArgRange:
Andreas Gampec3314312014-06-19 18:13:29 -07001217 if (inst->GetVerifyExtraFlags() == Instruction::kVerifyVarArgRangeNonZero &&
1218 inst->VRegA() <= 0) {
1219 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << inst->VRegA() << ") in "
1220 "range invoke";
1221 return false;
1222 }
Ian Rogers29a26482014-05-02 15:27:29 -07001223 result = result && CheckVarArgRangeRegs(inst->VRegA(), inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -07001224 break;
1225 case Instruction::kVerifyError:
jeffhaod5347e02012-03-22 17:25:05 -07001226 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected opcode " << inst->Name();
Ian Rogersd81871c2011-10-03 13:57:23 -07001227 result = false;
1228 break;
1229 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08001230 if (inst->GetVerifyIsRuntimeOnly() && Runtime::Current()->IsAotCompiler() && !verify_to_dump_) {
Ian Rogers5fb22a92014-06-13 10:31:28 -07001231 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "opcode only expected at runtime " << inst->Name();
1232 result = false;
1233 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001234 return result;
1235}
1236
Ian Rogers7b078e82014-09-10 14:44:24 -07001237inline bool MethodVerifier::CheckRegisterIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001238 if (idx >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07001239 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register index out of range (" << idx << " >= "
1240 << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001241 return false;
1242 }
1243 return true;
1244}
1245
Ian Rogers7b078e82014-09-10 14:44:24 -07001246inline bool MethodVerifier::CheckWideRegisterIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001247 if (idx + 1 >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07001248 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "wide register index out of range (" << idx
1249 << "+1 >= " << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001250 return false;
1251 }
1252 return true;
1253}
1254
Ian Rogers7b078e82014-09-10 14:44:24 -07001255inline bool MethodVerifier::CheckFieldIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001256 if (idx >= dex_file_->GetHeader().field_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07001257 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad field index " << idx << " (max "
1258 << dex_file_->GetHeader().field_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001259 return false;
1260 }
1261 return true;
1262}
1263
Ian Rogers7b078e82014-09-10 14:44:24 -07001264inline bool MethodVerifier::CheckMethodIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001265 if (idx >= dex_file_->GetHeader().method_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07001266 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad method index " << idx << " (max "
1267 << dex_file_->GetHeader().method_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001268 return false;
1269 }
1270 return true;
1271}
1272
Andreas Gampea5b09a62016-11-17 15:21:22 -08001273inline bool MethodVerifier::CheckNewInstance(dex::TypeIndex idx) {
1274 if (idx.index_ >= dex_file_->GetHeader().type_ids_size_) {
1275 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx.index_ << " (max "
jeffhaod5347e02012-03-22 17:25:05 -07001276 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001277 return false;
1278 }
1279 // We don't need the actual class, just a pointer to the class name.
Ian Rogers0571d352011-11-03 19:51:38 -07001280 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -07001281 if (descriptor[0] != 'L') {
jeffhaod5347e02012-03-22 17:25:05 -07001282 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "can't call new-instance on type '" << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001283 return false;
Aart Bikdb698f12016-07-25 17:52:22 -07001284 } else if (strcmp(descriptor, "Ljava/lang/Class;") == 0) {
1285 // An unlikely new instance on Class is not allowed. Fall back to interpreter to ensure an
1286 // exception is thrown when this statement is executed (compiled code would not do that).
1287 Fail(VERIFY_ERROR_INSTANTIATION);
Ian Rogersd81871c2011-10-03 13:57:23 -07001288 }
1289 return true;
1290}
1291
Ian Rogers7b078e82014-09-10 14:44:24 -07001292inline bool MethodVerifier::CheckStringIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001293 if (idx >= dex_file_->GetHeader().string_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07001294 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad string index " << idx << " (max "
1295 << dex_file_->GetHeader().string_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001296 return false;
1297 }
1298 return true;
1299}
1300
Andreas Gampea5b09a62016-11-17 15:21:22 -08001301inline bool MethodVerifier::CheckTypeIndex(dex::TypeIndex idx) {
1302 if (idx.index_ >= dex_file_->GetHeader().type_ids_size_) {
1303 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx.index_ << " (max "
jeffhaod5347e02012-03-22 17:25:05 -07001304 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001305 return false;
1306 }
1307 return true;
1308}
1309
Andreas Gampea5b09a62016-11-17 15:21:22 -08001310bool MethodVerifier::CheckNewArray(dex::TypeIndex idx) {
1311 if (idx.index_ >= dex_file_->GetHeader().type_ids_size_) {
1312 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx.index_ << " (max "
jeffhaod5347e02012-03-22 17:25:05 -07001313 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001314 return false;
1315 }
1316 int bracket_count = 0;
Ian Rogers0571d352011-11-03 19:51:38 -07001317 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -07001318 const char* cp = descriptor;
1319 while (*cp++ == '[') {
1320 bracket_count++;
1321 }
1322 if (bracket_count == 0) {
1323 /* The given class must be an array type. */
Brian Carlstrom93c33962013-07-26 10:37:43 -07001324 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1325 << "can't new-array class '" << descriptor << "' (not an array)";
Ian Rogersd81871c2011-10-03 13:57:23 -07001326 return false;
1327 } else if (bracket_count > 255) {
1328 /* It is illegal to create an array of more than 255 dimensions. */
Brian Carlstrom93c33962013-07-26 10:37:43 -07001329 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1330 << "can't new-array class '" << descriptor << "' (exceeds limit)";
Ian Rogersd81871c2011-10-03 13:57:23 -07001331 return false;
1332 }
1333 return true;
1334}
1335
Ian Rogers776ac1f2012-04-13 23:36:36 -07001336bool MethodVerifier::CheckArrayData(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001337 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
1338 const uint16_t* insns = code_item_->insns_ + cur_offset;
1339 const uint16_t* array_data;
1340 int32_t array_data_offset;
1341
1342 DCHECK_LT(cur_offset, insn_count);
1343 /* make sure the start of the array data table is in range */
Andreas Gampe53de99c2015-08-17 13:43:55 -07001344 array_data_offset = insns[1] | (static_cast<int32_t>(insns[2]) << 16);
1345 if (static_cast<int32_t>(cur_offset) + array_data_offset < 0 ||
Ian Rogersd81871c2011-10-03 13:57:23 -07001346 cur_offset + array_data_offset + 2 >= insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -07001347 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data start: at " << cur_offset
Brian Carlstrom93c33962013-07-26 10:37:43 -07001348 << ", data offset " << array_data_offset
1349 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -07001350 return false;
1351 }
1352 /* offset to array data table is a relative branch-style offset */
1353 array_data = insns + array_data_offset;
Andreas Gampe57c47582015-07-01 22:05:59 -07001354 // Make sure the table is at an even dex pc, that is, 32-bit aligned.
1355 if (!IsAligned<4>(array_data)) {
jeffhaod5347e02012-03-22 17:25:05 -07001356 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned array data table: at " << cur_offset
1357 << ", data offset " << array_data_offset;
Ian Rogersd81871c2011-10-03 13:57:23 -07001358 return false;
1359 }
Andreas Gampe57c47582015-07-01 22:05:59 -07001360 // Make sure the array-data is marked as an opcode. This ensures that it was reached when
1361 // traversing the code item linearly. It is an approximation for a by-spec padding value.
Mathieu Chartierde40d472015-10-15 17:47:48 -07001362 if (!GetInstructionFlags(cur_offset + array_data_offset).IsOpcode()) {
Andreas Gampe57c47582015-07-01 22:05:59 -07001363 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array data table at " << cur_offset
1364 << ", data offset " << array_data_offset
1365 << " not correctly visited, probably bad padding.";
1366 return false;
1367 }
1368
Ian Rogersd81871c2011-10-03 13:57:23 -07001369 uint32_t value_width = array_data[1];
Elliott Hughes398f64b2012-03-26 18:05:48 -07001370 uint32_t value_count = *reinterpret_cast<const uint32_t*>(&array_data[2]);
Ian Rogersd81871c2011-10-03 13:57:23 -07001371 uint32_t table_size = 4 + (value_width * value_count + 1) / 2;
1372 /* make sure the end of the switch is in range */
1373 if (cur_offset + array_data_offset + table_size > insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -07001374 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data end: at " << cur_offset
1375 << ", data offset " << array_data_offset << ", end "
1376 << cur_offset + array_data_offset + table_size
1377 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -07001378 return false;
1379 }
1380 return true;
1381}
1382
Ian Rogers776ac1f2012-04-13 23:36:36 -07001383bool MethodVerifier::CheckBranchTarget(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001384 int32_t offset;
1385 bool isConditional, selfOkay;
1386 if (!GetBranchOffset(cur_offset, &offset, &isConditional, &selfOkay)) {
1387 return false;
1388 }
1389 if (!selfOkay && offset == 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001390 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch offset of zero not allowed at"
1391 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -07001392 return false;
1393 }
Elliott Hughes81ff3182012-03-23 20:35:56 -07001394 // Check for 32-bit overflow. This isn't strictly necessary if we can depend on the runtime
1395 // to have identical "wrap-around" behavior, but it's unwise to depend on that.
Ian Rogersd81871c2011-10-03 13:57:23 -07001396 if (((int64_t) cur_offset + (int64_t) offset) != (int64_t) (cur_offset + offset)) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001397 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch target overflow "
1398 << reinterpret_cast<void*>(cur_offset) << " +" << offset;
Ian Rogersd81871c2011-10-03 13:57:23 -07001399 return false;
1400 }
1401 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
1402 int32_t abs_offset = cur_offset + offset;
Brian Carlstrom93c33962013-07-26 10:37:43 -07001403 if (abs_offset < 0 ||
1404 (uint32_t) abs_offset >= insn_count ||
Mathieu Chartierde40d472015-10-15 17:47:48 -07001405 !GetInstructionFlags(abs_offset).IsOpcode()) {
jeffhaod5347e02012-03-22 17:25:05 -07001406 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid branch target " << offset << " (-> "
Elliott Hughes398f64b2012-03-26 18:05:48 -07001407 << reinterpret_cast<void*>(abs_offset) << ") at "
1408 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -07001409 return false;
1410 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07001411 GetInstructionFlags(abs_offset).SetBranchTarget();
Ian Rogersd81871c2011-10-03 13:57:23 -07001412 return true;
1413}
1414
Ian Rogers776ac1f2012-04-13 23:36:36 -07001415bool MethodVerifier::GetBranchOffset(uint32_t cur_offset, int32_t* pOffset, bool* pConditional,
Ian Rogersd81871c2011-10-03 13:57:23 -07001416 bool* selfOkay) {
1417 const uint16_t* insns = code_item_->insns_ + cur_offset;
1418 *pConditional = false;
1419 *selfOkay = false;
jeffhaoba5ebb92011-08-25 17:24:37 -07001420 switch (*insns & 0xff) {
1421 case Instruction::GOTO:
1422 *pOffset = ((int16_t) *insns) >> 8;
jeffhaoba5ebb92011-08-25 17:24:37 -07001423 break;
1424 case Instruction::GOTO_32:
1425 *pOffset = insns[1] | (((uint32_t) insns[2]) << 16);
jeffhaoba5ebb92011-08-25 17:24:37 -07001426 *selfOkay = true;
1427 break;
1428 case Instruction::GOTO_16:
1429 *pOffset = (int16_t) insns[1];
jeffhaoba5ebb92011-08-25 17:24:37 -07001430 break;
1431 case Instruction::IF_EQ:
1432 case Instruction::IF_NE:
1433 case Instruction::IF_LT:
1434 case Instruction::IF_GE:
1435 case Instruction::IF_GT:
1436 case Instruction::IF_LE:
1437 case Instruction::IF_EQZ:
1438 case Instruction::IF_NEZ:
1439 case Instruction::IF_LTZ:
1440 case Instruction::IF_GEZ:
1441 case Instruction::IF_GTZ:
1442 case Instruction::IF_LEZ:
1443 *pOffset = (int16_t) insns[1];
1444 *pConditional = true;
jeffhaoba5ebb92011-08-25 17:24:37 -07001445 break;
1446 default:
1447 return false;
jeffhaoba5ebb92011-08-25 17:24:37 -07001448 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001449 return true;
1450}
1451
Ian Rogers776ac1f2012-04-13 23:36:36 -07001452bool MethodVerifier::CheckSwitchTargets(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001453 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001454 DCHECK_LT(cur_offset, insn_count);
Ian Rogersd81871c2011-10-03 13:57:23 -07001455 const uint16_t* insns = code_item_->insns_ + cur_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001456 /* make sure the start of the switch is in range */
Andreas Gampe53de99c2015-08-17 13:43:55 -07001457 int32_t switch_offset = insns[1] | (static_cast<int32_t>(insns[2]) << 16);
1458 if (static_cast<int32_t>(cur_offset) + switch_offset < 0 ||
1459 cur_offset + switch_offset + 2 > insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -07001460 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch start: at " << cur_offset
Brian Carlstrom93c33962013-07-26 10:37:43 -07001461 << ", switch offset " << switch_offset
1462 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001463 return false;
1464 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001465 /* offset to switch table is a relative branch-style offset */
Ian Rogersd81871c2011-10-03 13:57:23 -07001466 const uint16_t* switch_insns = insns + switch_offset;
Andreas Gampe57c47582015-07-01 22:05:59 -07001467 // Make sure the table is at an even dex pc, that is, 32-bit aligned.
1468 if (!IsAligned<4>(switch_insns)) {
jeffhaod5347e02012-03-22 17:25:05 -07001469 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned switch table: at " << cur_offset
1470 << ", switch offset " << switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001471 return false;
1472 }
Andreas Gampe57c47582015-07-01 22:05:59 -07001473 // Make sure the switch data is marked as an opcode. This ensures that it was reached when
1474 // traversing the code item linearly. It is an approximation for a by-spec padding value.
Mathieu Chartierde40d472015-10-15 17:47:48 -07001475 if (!GetInstructionFlags(cur_offset + switch_offset).IsOpcode()) {
Andreas Gampe57c47582015-07-01 22:05:59 -07001476 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "switch table at " << cur_offset
1477 << ", switch offset " << switch_offset
1478 << " not correctly visited, probably bad padding.";
1479 return false;
1480 }
1481
David Brazdil5469d342015-09-25 16:57:53 +01001482 bool is_packed_switch = (*insns & 0xff) == Instruction::PACKED_SWITCH;
1483
Ian Rogersd81871c2011-10-03 13:57:23 -07001484 uint32_t switch_count = switch_insns[1];
David Brazdil5469d342015-09-25 16:57:53 +01001485 int32_t targets_offset;
Ian Rogersd81871c2011-10-03 13:57:23 -07001486 uint16_t expected_signature;
David Brazdil5469d342015-09-25 16:57:53 +01001487 if (is_packed_switch) {
jeffhaoba5ebb92011-08-25 17:24:37 -07001488 /* 0=sig, 1=count, 2/3=firstKey */
1489 targets_offset = 4;
jeffhaoba5ebb92011-08-25 17:24:37 -07001490 expected_signature = Instruction::kPackedSwitchSignature;
1491 } else {
1492 /* 0=sig, 1=count, 2..count*2 = keys */
jeffhaoba5ebb92011-08-25 17:24:37 -07001493 targets_offset = 2 + 2 * switch_count;
1494 expected_signature = Instruction::kSparseSwitchSignature;
1495 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001496 uint32_t table_size = targets_offset + switch_count * 2;
jeffhaoba5ebb92011-08-25 17:24:37 -07001497 if (switch_insns[0] != expected_signature) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001498 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1499 << StringPrintf("wrong signature for switch table (%x, wanted %x)",
1500 switch_insns[0], expected_signature);
jeffhaoba5ebb92011-08-25 17:24:37 -07001501 return false;
1502 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001503 /* make sure the end of the switch is in range */
1504 if (cur_offset + switch_offset + table_size > (uint32_t) insn_count) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001505 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch end: at " << cur_offset
1506 << ", switch offset " << switch_offset
1507 << ", end " << (cur_offset + switch_offset + table_size)
jeffhaod5347e02012-03-22 17:25:05 -07001508 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001509 return false;
1510 }
David Brazdil5469d342015-09-25 16:57:53 +01001511
1512 constexpr int32_t keys_offset = 2;
1513 if (switch_count > 1) {
1514 if (is_packed_switch) {
1515 /* for a packed switch, verify that keys do not overflow int32 */
1516 int32_t first_key = switch_insns[keys_offset] | (switch_insns[keys_offset + 1] << 16);
1517 int32_t max_first_key =
1518 std::numeric_limits<int32_t>::max() - (static_cast<int32_t>(switch_count) - 1);
1519 if (first_key > max_first_key) {
1520 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid packed switch: first_key=" << first_key
1521 << ", switch_count=" << switch_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001522 return false;
1523 }
David Brazdil5469d342015-09-25 16:57:53 +01001524 } else {
1525 /* for a sparse switch, verify the keys are in ascending order */
1526 int32_t last_key = switch_insns[keys_offset] | (switch_insns[keys_offset + 1] << 16);
1527 for (uint32_t targ = 1; targ < switch_count; targ++) {
1528 int32_t key =
1529 static_cast<int32_t>(switch_insns[keys_offset + targ * 2]) |
1530 static_cast<int32_t>(switch_insns[keys_offset + targ * 2 + 1] << 16);
1531 if (key <= last_key) {
1532 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid sparse switch: last key=" << last_key
1533 << ", this=" << key;
1534 return false;
1535 }
1536 last_key = key;
1537 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001538 }
1539 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001540 /* verify each switch target */
Ian Rogersd81871c2011-10-03 13:57:23 -07001541 for (uint32_t targ = 0; targ < switch_count; targ++) {
Andreas Gampe53de99c2015-08-17 13:43:55 -07001542 int32_t offset = static_cast<int32_t>(switch_insns[targets_offset + targ * 2]) |
1543 static_cast<int32_t>(switch_insns[targets_offset + targ * 2 + 1] << 16);
Ian Rogersd81871c2011-10-03 13:57:23 -07001544 int32_t abs_offset = cur_offset + offset;
Brian Carlstrom93c33962013-07-26 10:37:43 -07001545 if (abs_offset < 0 ||
Andreas Gampe53de99c2015-08-17 13:43:55 -07001546 abs_offset >= static_cast<int32_t>(insn_count) ||
Mathieu Chartierde40d472015-10-15 17:47:48 -07001547 !GetInstructionFlags(abs_offset).IsOpcode()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001548 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch target " << offset
1549 << " (-> " << reinterpret_cast<void*>(abs_offset) << ") at "
1550 << reinterpret_cast<void*>(cur_offset)
1551 << "[" << targ << "]";
jeffhaoba5ebb92011-08-25 17:24:37 -07001552 return false;
1553 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07001554 GetInstructionFlags(abs_offset).SetBranchTarget();
Ian Rogersd81871c2011-10-03 13:57:23 -07001555 }
1556 return true;
1557}
1558
Ian Rogers776ac1f2012-04-13 23:36:36 -07001559bool MethodVerifier::CheckVarArgRegs(uint32_t vA, uint32_t arg[]) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001560 uint16_t registers_size = code_item_->registers_size_;
1561 for (uint32_t idx = 0; idx < vA; idx++) {
jeffhao457cc512012-02-02 16:55:13 -08001562 if (arg[idx] >= registers_size) {
jeffhaod5347e02012-03-22 17:25:05 -07001563 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index (" << arg[idx]
1564 << ") in non-range invoke (>= " << registers_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001565 return false;
1566 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001567 }
1568
1569 return true;
1570}
1571
Ian Rogers776ac1f2012-04-13 23:36:36 -07001572bool MethodVerifier::CheckVarArgRangeRegs(uint32_t vA, uint32_t vC) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001573 uint16_t registers_size = code_item_->registers_size_;
1574 // vA/vC are unsigned 8-bit/16-bit quantities for /range instructions, so there's no risk of
1575 // integer overflow when adding them here.
1576 if (vA + vC > registers_size) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001577 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index " << vA << "+" << vC
1578 << " in range invoke (> " << registers_size << ")";
jeffhaoba5ebb92011-08-25 17:24:37 -07001579 return false;
1580 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001581 return true;
1582}
1583
Ian Rogers776ac1f2012-04-13 23:36:36 -07001584bool MethodVerifier::VerifyCodeFlow() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001585 uint16_t registers_size = code_item_->registers_size_;
1586 uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaobdb76512011-09-07 11:43:16 -07001587
Ian Rogersd81871c2011-10-03 13:57:23 -07001588 /* Create and initialize table holding register status */
Brian Carlstrom93c33962013-07-26 10:37:43 -07001589 reg_table_.Init(kTrackCompilerInterestPoints,
1590 insn_flags_.get(),
1591 insns_size,
1592 registers_size,
1593 this);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07001594
Ian Rogersd0fbd852013-09-24 18:17:04 -07001595 work_line_.reset(RegisterLine::Create(registers_size, this));
1596 saved_line_.reset(RegisterLine::Create(registers_size, this));
jeffhaobdb76512011-09-07 11:43:16 -07001597
Ian Rogersd81871c2011-10-03 13:57:23 -07001598 /* Initialize register types of method arguments. */
1599 if (!SetTypesFromSignature()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001600 DCHECK_NE(failures_.size(), 0U);
1601 std::string prepend("Bad signature in ");
David Sehr709b0702016-10-13 09:12:37 -07001602 prepend += dex_file_->PrettyMethod(dex_method_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001603 PrependToLastFailMessage(prepend);
Ian Rogersd81871c2011-10-03 13:57:23 -07001604 return false;
1605 }
Andreas Gamped5ad72f2015-06-26 17:33:47 -07001606 // We may have a runtime failure here, clear.
1607 have_pending_runtime_throw_failure_ = false;
1608
Ian Rogersd81871c2011-10-03 13:57:23 -07001609 /* Perform code flow verification. */
1610 if (!CodeFlowVerifyMethod()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001611 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -07001612 return false;
jeffhaobdb76512011-09-07 11:43:16 -07001613 }
jeffhaobdb76512011-09-07 11:43:16 -07001614 return true;
1615}
1616
Ian Rogersad0b3a32012-04-16 14:50:24 -07001617std::ostream& MethodVerifier::DumpFailures(std::ostream& os) {
1618 DCHECK_EQ(failures_.size(), failure_messages_.size());
Jeff Hao4137f482013-11-22 11:44:57 -08001619 for (size_t i = 0; i < failures_.size(); ++i) {
1620 os << failure_messages_[i]->str() << "\n";
Ian Rogersad0b3a32012-04-16 14:50:24 -07001621 }
1622 return os;
1623}
1624
Ian Rogers776ac1f2012-04-13 23:36:36 -07001625void MethodVerifier::Dump(std::ostream& os) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001626 VariableIndentationOutputStream vios(&os);
1627 Dump(&vios);
1628}
1629
1630void MethodVerifier::Dump(VariableIndentationOutputStream* vios) {
Ian Rogers7b078e82014-09-10 14:44:24 -07001631 if (code_item_ == nullptr) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001632 vios->Stream() << "Native method\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001633 return;
jeffhaobdb76512011-09-07 11:43:16 -07001634 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001635 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001636 vios->Stream() << "Register Types:\n";
1637 ScopedIndentation indent1(vios);
1638 reg_types_.Dump(vios->Stream());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001639 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001640 vios->Stream() << "Dumping instructions and register lines:\n";
1641 ScopedIndentation indent1(vios);
Ian Rogersd81871c2011-10-03 13:57:23 -07001642 const Instruction* inst = Instruction::At(code_item_->insns_);
1643 for (size_t dex_pc = 0; dex_pc < code_item_->insns_size_in_code_units_;
Andreas Gampeebf850c2015-08-14 15:37:35 -07001644 dex_pc += inst->SizeInCodeUnits(), inst = inst->Next()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001645 RegisterLine* reg_line = reg_table_.GetLine(dex_pc);
Ian Rogers7b078e82014-09-10 14:44:24 -07001646 if (reg_line != nullptr) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001647 vios->Stream() << reg_line->Dump(this) << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07001648 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001649 vios->Stream()
Mathieu Chartierde40d472015-10-15 17:47:48 -07001650 << StringPrintf("0x%04zx", dex_pc) << ": " << GetInstructionFlags(dex_pc).ToString() << " ";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001651 const bool kDumpHexOfInstruction = false;
1652 if (kDumpHexOfInstruction) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001653 vios->Stream() << inst->DumpHex(5) << " ";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001654 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001655 vios->Stream() << inst->DumpString(dex_file_) << "\n";
jeffhaoba5ebb92011-08-25 17:24:37 -07001656 }
jeffhaobdb76512011-09-07 11:43:16 -07001657}
1658
Ian Rogersd81871c2011-10-03 13:57:23 -07001659static bool IsPrimitiveDescriptor(char descriptor) {
1660 switch (descriptor) {
jeffhaobdb76512011-09-07 11:43:16 -07001661 case 'I':
1662 case 'C':
1663 case 'S':
1664 case 'B':
1665 case 'Z':
jeffhaobdb76512011-09-07 11:43:16 -07001666 case 'F':
1667 case 'D':
1668 case 'J':
Ian Rogersd81871c2011-10-03 13:57:23 -07001669 return true;
jeffhaobdb76512011-09-07 11:43:16 -07001670 default:
1671 return false;
1672 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001673}
1674
Ian Rogers776ac1f2012-04-13 23:36:36 -07001675bool MethodVerifier::SetTypesFromSignature() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001676 RegisterLine* reg_line = reg_table_.GetLine(0);
Andreas Gampeef0b1a12015-06-19 20:37:46 -07001677
1678 // Should have been verified earlier.
1679 DCHECK_GE(code_item_->registers_size_, code_item_->ins_size_);
1680
1681 uint32_t arg_start = code_item_->registers_size_ - code_item_->ins_size_;
Ian Rogersd81871c2011-10-03 13:57:23 -07001682 size_t expected_args = code_item_->ins_size_; /* long/double count as two */
jeffhaobdb76512011-09-07 11:43:16 -07001683
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001684 // Include the "this" pointer.
Ian Rogersd81871c2011-10-03 13:57:23 -07001685 size_t cur_arg = 0;
Ian Rogersad0b3a32012-04-16 14:50:24 -07001686 if (!IsStatic()) {
Andreas Gampeef0b1a12015-06-19 20:37:46 -07001687 if (expected_args == 0) {
1688 // Expect at least a receiver.
1689 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected 0 args, but method is not static";
1690 return false;
1691 }
1692
Ian Rogersd81871c2011-10-03 13:57:23 -07001693 // If this is a constructor for a class other than java.lang.Object, mark the first ("this")
1694 // argument as uninitialized. This restricts field access until the superclass constructor is
1695 // called.
Ian Rogersd8f69b02014-09-10 21:43:52 +00001696 const RegType& declaring_class = GetDeclaringClass();
Andreas Gampef10b6e12015-08-12 10:48:12 -07001697 if (IsConstructor()) {
1698 if (declaring_class.IsJavaLangObject()) {
1699 // "this" is implicitly initialized.
1700 reg_line->SetThisInitialized();
Andreas Gampead238ce2015-08-24 21:13:08 -07001701 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, declaring_class);
Andreas Gampef10b6e12015-08-12 10:48:12 -07001702 } else {
Andreas Gampead238ce2015-08-24 21:13:08 -07001703 reg_line->SetRegisterType<LockOp::kClear>(
1704 this,
1705 arg_start + cur_arg,
1706 reg_types_.UninitializedThisArgument(declaring_class));
Andreas Gampef10b6e12015-08-12 10:48:12 -07001707 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001708 } else {
Andreas Gampead238ce2015-08-24 21:13:08 -07001709 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, declaring_class);
jeffhaobdb76512011-09-07 11:43:16 -07001710 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001711 cur_arg++;
jeffhaobdb76512011-09-07 11:43:16 -07001712 }
1713
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001714 const DexFile::ProtoId& proto_id =
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001715 dex_file_->GetMethodPrototype(dex_file_->GetMethodId(dex_method_idx_));
Ian Rogers0571d352011-11-03 19:51:38 -07001716 DexFileParameterIterator iterator(*dex_file_, proto_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07001717
1718 for (; iterator.HasNext(); iterator.Next()) {
1719 const char* descriptor = iterator.GetDescriptor();
Ian Rogers7b078e82014-09-10 14:44:24 -07001720 if (descriptor == nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001721 LOG(FATAL) << "Null descriptor";
1722 }
1723 if (cur_arg >= expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07001724 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1725 << " args, found more (" << descriptor << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001726 return false;
1727 }
1728 switch (descriptor[0]) {
1729 case 'L':
1730 case '[':
1731 // We assume that reference arguments are initialized. The only way it could be otherwise
1732 // (assuming the caller was verified) is if the current method is <init>, but in that case
1733 // it's effectively considered initialized the instant we reach here (in the sense that we
1734 // can return without doing anything or call virtual methods).
1735 {
Ian Rogersd8f69b02014-09-10 21:43:52 +00001736 const RegType& reg_type = ResolveClassAndCheckAccess(iterator.GetTypeIdx());
Sebastien Hertz2ed76f92014-04-22 17:11:08 +02001737 if (!reg_type.IsNonZeroReferenceTypes()) {
1738 DCHECK(HasFailures());
1739 return false;
1740 }
Andreas Gampead238ce2015-08-24 21:13:08 -07001741 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001742 }
1743 break;
1744 case 'Z':
Andreas Gampead238ce2015-08-24 21:13:08 -07001745 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Boolean());
Ian Rogersd81871c2011-10-03 13:57:23 -07001746 break;
1747 case 'C':
Andreas Gampead238ce2015-08-24 21:13:08 -07001748 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Char());
Ian Rogersd81871c2011-10-03 13:57:23 -07001749 break;
1750 case 'B':
Andreas Gampead238ce2015-08-24 21:13:08 -07001751 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Byte());
Ian Rogersd81871c2011-10-03 13:57:23 -07001752 break;
1753 case 'I':
Andreas Gampead238ce2015-08-24 21:13:08 -07001754 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Integer());
Ian Rogersd81871c2011-10-03 13:57:23 -07001755 break;
1756 case 'S':
Andreas Gampead238ce2015-08-24 21:13:08 -07001757 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Short());
Ian Rogersd81871c2011-10-03 13:57:23 -07001758 break;
1759 case 'F':
Andreas Gampead238ce2015-08-24 21:13:08 -07001760 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Float());
Ian Rogersd81871c2011-10-03 13:57:23 -07001761 break;
1762 case 'J':
1763 case 'D': {
Andreas Gampe77cd4d62014-06-19 17:29:48 -07001764 if (cur_arg + 1 >= expected_args) {
1765 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1766 << " args, found more (" << descriptor << ")";
1767 return false;
1768 }
1769
Ian Rogers7b078e82014-09-10 14:44:24 -07001770 const RegType* lo_half;
1771 const RegType* hi_half;
1772 if (descriptor[0] == 'J') {
1773 lo_half = &reg_types_.LongLo();
1774 hi_half = &reg_types_.LongHi();
1775 } else {
1776 lo_half = &reg_types_.DoubleLo();
1777 hi_half = &reg_types_.DoubleHi();
1778 }
1779 reg_line->SetRegisterTypeWide(this, arg_start + cur_arg, *lo_half, *hi_half);
Ian Rogersd81871c2011-10-03 13:57:23 -07001780 cur_arg++;
1781 break;
1782 }
1783 default:
Brian Carlstrom93c33962013-07-26 10:37:43 -07001784 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected signature type char '"
1785 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001786 return false;
1787 }
1788 cur_arg++;
1789 }
1790 if (cur_arg != expected_args) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001791 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1792 << " arguments, found " << cur_arg;
Ian Rogersd81871c2011-10-03 13:57:23 -07001793 return false;
1794 }
1795 const char* descriptor = dex_file_->GetReturnTypeDescriptor(proto_id);
1796 // Validate return type. We don't do the type lookup; just want to make sure that it has the right
1797 // format. Only major difference from the method argument format is that 'V' is supported.
1798 bool result;
1799 if (IsPrimitiveDescriptor(descriptor[0]) || descriptor[0] == 'V') {
1800 result = descriptor[1] == '\0';
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001801 } else if (descriptor[0] == '[') { // single/multi-dimensional array of object/primitive
Ian Rogersd81871c2011-10-03 13:57:23 -07001802 size_t i = 0;
1803 do {
1804 i++;
1805 } while (descriptor[i] == '['); // process leading [
1806 if (descriptor[i] == 'L') { // object array
1807 do {
1808 i++; // find closing ;
1809 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1810 result = descriptor[i] == ';';
1811 } else { // primitive array
1812 result = IsPrimitiveDescriptor(descriptor[i]) && descriptor[i + 1] == '\0';
1813 }
1814 } else if (descriptor[0] == 'L') {
1815 // could be more thorough here, but shouldn't be required
1816 size_t i = 0;
1817 do {
1818 i++;
1819 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1820 result = descriptor[i] == ';';
1821 } else {
1822 result = false;
1823 }
1824 if (!result) {
jeffhaod5347e02012-03-22 17:25:05 -07001825 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected char in return type descriptor '"
1826 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001827 }
1828 return result;
jeffhaobdb76512011-09-07 11:43:16 -07001829}
1830
Ian Rogers776ac1f2012-04-13 23:36:36 -07001831bool MethodVerifier::CodeFlowVerifyMethod() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001832 const uint16_t* insns = code_item_->insns_;
1833 const uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaoba5ebb92011-08-25 17:24:37 -07001834
jeffhaobdb76512011-09-07 11:43:16 -07001835 /* Begin by marking the first instruction as "changed". */
Mathieu Chartierde40d472015-10-15 17:47:48 -07001836 GetInstructionFlags(0).SetChanged();
Ian Rogersd81871c2011-10-03 13:57:23 -07001837 uint32_t start_guess = 0;
jeffhaoba5ebb92011-08-25 17:24:37 -07001838
jeffhaobdb76512011-09-07 11:43:16 -07001839 /* Continue until no instructions are marked "changed". */
1840 while (true) {
Mathieu Chartier4306ef82014-12-19 18:41:47 -08001841 if (allow_thread_suspension_) {
1842 self_->AllowThreadSuspension();
1843 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001844 // Find the first marked one. Use "start_guess" as a way to find one quickly.
1845 uint32_t insn_idx = start_guess;
1846 for (; insn_idx < insns_size; insn_idx++) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07001847 if (GetInstructionFlags(insn_idx).IsChanged())
jeffhaobdb76512011-09-07 11:43:16 -07001848 break;
1849 }
jeffhaobdb76512011-09-07 11:43:16 -07001850 if (insn_idx == insns_size) {
1851 if (start_guess != 0) {
1852 /* try again, starting from the top */
1853 start_guess = 0;
1854 continue;
1855 } else {
1856 /* all flags are clear */
1857 break;
1858 }
1859 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001860 // We carry the working set of registers from instruction to instruction. If this address can
1861 // be the target of a branch (or throw) instruction, or if we're skipping around chasing
1862 // "changed" flags, we need to load the set of registers from the table.
1863 // Because we always prefer to continue on to the next instruction, we should never have a
1864 // situation where we have a stray "changed" flag set on an instruction that isn't a branch
1865 // target.
1866 work_insn_idx_ = insn_idx;
Mathieu Chartierde40d472015-10-15 17:47:48 -07001867 if (GetInstructionFlags(insn_idx).IsBranchTarget()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001868 work_line_->CopyFromLine(reg_table_.GetLine(insn_idx));
Ian Rogersebbdd872014-07-07 23:53:08 -07001869 } else if (kIsDebugBuild) {
jeffhaobdb76512011-09-07 11:43:16 -07001870 /*
1871 * Sanity check: retrieve the stored register line (assuming
1872 * a full table) and make sure it actually matches.
1873 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001874 RegisterLine* register_line = reg_table_.GetLine(insn_idx);
Ian Rogers7b078e82014-09-10 14:44:24 -07001875 if (register_line != nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001876 if (work_line_->CompareLine(register_line) != 0) {
1877 Dump(std::cout);
1878 std::cout << info_messages_.str();
David Sehr709b0702016-10-13 09:12:37 -07001879 LOG(FATAL) << "work_line diverged in " << dex_file_->PrettyMethod(dex_method_idx_)
Elliott Hughesc073b072012-05-24 19:29:17 -07001880 << "@" << reinterpret_cast<void*>(work_insn_idx_) << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07001881 << " work_line=" << work_line_->Dump(this) << "\n"
1882 << " expected=" << register_line->Dump(this);
Ian Rogersd81871c2011-10-03 13:57:23 -07001883 }
jeffhaobdb76512011-09-07 11:43:16 -07001884 }
jeffhaobdb76512011-09-07 11:43:16 -07001885 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001886 if (!CodeFlowVerifyInstruction(&start_guess)) {
David Sehr709b0702016-10-13 09:12:37 -07001887 std::string prepend(dex_file_->PrettyMethod(dex_method_idx_));
Ian Rogersad0b3a32012-04-16 14:50:24 -07001888 prepend += " failed to verify: ";
1889 PrependToLastFailMessage(prepend);
jeffhaoba5ebb92011-08-25 17:24:37 -07001890 return false;
1891 }
jeffhaobdb76512011-09-07 11:43:16 -07001892 /* Clear "changed" and mark as visited. */
Mathieu Chartierde40d472015-10-15 17:47:48 -07001893 GetInstructionFlags(insn_idx).SetVisited();
1894 GetInstructionFlags(insn_idx).ClearChanged();
jeffhaobdb76512011-09-07 11:43:16 -07001895 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001896
Andreas Gampeec6e6c12015-11-05 20:39:56 -08001897 if (kDebugVerify) {
jeffhaobdb76512011-09-07 11:43:16 -07001898 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001899 * Scan for dead code. There's nothing "evil" about dead code
jeffhaobdb76512011-09-07 11:43:16 -07001900 * (besides the wasted space), but it indicates a flaw somewhere
1901 * down the line, possibly in the verifier.
1902 *
1903 * If we've substituted "always throw" instructions into the stream,
1904 * we are almost certainly going to have some dead code.
1905 */
1906 int dead_start = -1;
Ian Rogersd81871c2011-10-03 13:57:23 -07001907 uint32_t insn_idx = 0;
Ian Rogers7b078e82014-09-10 14:44:24 -07001908 for (; insn_idx < insns_size;
1909 insn_idx += Instruction::At(code_item_->insns_ + insn_idx)->SizeInCodeUnits()) {
jeffhaobdb76512011-09-07 11:43:16 -07001910 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001911 * Switch-statement data doesn't get "visited" by scanner. It
jeffhaobdb76512011-09-07 11:43:16 -07001912 * may or may not be preceded by a padding NOP (for alignment).
1913 */
1914 if (insns[insn_idx] == Instruction::kPackedSwitchSignature ||
1915 insns[insn_idx] == Instruction::kSparseSwitchSignature ||
1916 insns[insn_idx] == Instruction::kArrayDataSignature ||
Elliott Hughes380aaa72012-07-09 14:33:15 -07001917 (insns[insn_idx] == Instruction::NOP && (insn_idx + 1 < insns_size) &&
jeffhaobdb76512011-09-07 11:43:16 -07001918 (insns[insn_idx + 1] == Instruction::kPackedSwitchSignature ||
1919 insns[insn_idx + 1] == Instruction::kSparseSwitchSignature ||
1920 insns[insn_idx + 1] == Instruction::kArrayDataSignature))) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07001921 GetInstructionFlags(insn_idx).SetVisited();
jeffhaobdb76512011-09-07 11:43:16 -07001922 }
1923
Mathieu Chartierde40d472015-10-15 17:47:48 -07001924 if (!GetInstructionFlags(insn_idx).IsVisited()) {
jeffhaobdb76512011-09-07 11:43:16 -07001925 if (dead_start < 0)
1926 dead_start = insn_idx;
1927 } else if (dead_start >= 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001928 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start)
1929 << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaobdb76512011-09-07 11:43:16 -07001930 dead_start = -1;
1931 }
1932 }
1933 if (dead_start >= 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001934 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start)
1935 << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaoba5ebb92011-08-25 17:24:37 -07001936 }
Ian Rogersc9e463c2013-06-05 16:52:26 -07001937 // To dump the state of the verify after a method, do something like:
David Sehr709b0702016-10-13 09:12:37 -07001938 // if (dex_file_->PrettyMethod(dex_method_idx_) ==
Ian Rogersc9e463c2013-06-05 16:52:26 -07001939 // "boolean java.lang.String.equals(java.lang.Object)") {
1940 // LOG(INFO) << info_messages_.str();
1941 // }
jeffhaoba5ebb92011-08-25 17:24:37 -07001942 }
jeffhaobdb76512011-09-07 11:43:16 -07001943 return true;
1944}
1945
Andreas Gampe68df3202015-06-22 11:35:46 -07001946// Returns the index of the first final instance field of the given class, or kDexNoIndex if there
1947// is no such field.
Andreas Gampea5b09a62016-11-17 15:21:22 -08001948static uint32_t GetFirstFinalInstanceFieldIndex(const DexFile& dex_file, dex::TypeIndex type_idx) {
Andreas Gampe68df3202015-06-22 11:35:46 -07001949 const DexFile::ClassDef* class_def = dex_file.FindClassDef(type_idx);
1950 DCHECK(class_def != nullptr);
1951 const uint8_t* class_data = dex_file.GetClassData(*class_def);
1952 DCHECK(class_data != nullptr);
1953 ClassDataItemIterator it(dex_file, class_data);
1954 // Skip static fields.
1955 while (it.HasNextStaticField()) {
1956 it.Next();
1957 }
1958 while (it.HasNextInstanceField()) {
1959 if ((it.GetFieldAccessFlags() & kAccFinal) != 0) {
1960 return it.GetMemberIndex();
1961 }
1962 it.Next();
1963 }
1964 return DexFile::kDexNoIndex;
1965}
1966
Andreas Gampea727e372015-08-25 09:22:37 -07001967// Setup a register line for the given return instruction.
1968static void AdjustReturnLine(MethodVerifier* verifier,
1969 const Instruction* ret_inst,
1970 RegisterLine* line) {
1971 Instruction::Code opcode = ret_inst->Opcode();
1972
1973 switch (opcode) {
1974 case Instruction::RETURN_VOID:
1975 case Instruction::RETURN_VOID_NO_BARRIER:
1976 SafelyMarkAllRegistersAsConflicts(verifier, line);
1977 break;
1978
1979 case Instruction::RETURN:
1980 case Instruction::RETURN_OBJECT:
1981 line->MarkAllRegistersAsConflictsExcept(verifier, ret_inst->VRegA_11x());
1982 break;
1983
1984 case Instruction::RETURN_WIDE:
1985 line->MarkAllRegistersAsConflictsExceptWide(verifier, ret_inst->VRegA_11x());
1986 break;
1987
1988 default:
1989 LOG(FATAL) << "Unknown return opcode " << opcode;
1990 UNREACHABLE();
1991 }
1992}
1993
Ian Rogers776ac1f2012-04-13 23:36:36 -07001994bool MethodVerifier::CodeFlowVerifyInstruction(uint32_t* start_guess) {
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001995 // If we're doing FindLocksAtDexPc, check whether we're at the dex pc we care about.
1996 // We want the state _before_ the instruction, for the case where the dex pc we're
1997 // interested in is itself a monitor-enter instruction (which is a likely place
1998 // for a thread to be suspended).
Ian Rogers7b078e82014-09-10 14:44:24 -07001999 if (monitor_enter_dex_pcs_ != nullptr && work_insn_idx_ == interesting_dex_pc_) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -07002000 monitor_enter_dex_pcs_->clear(); // The new work line is more accurate than the previous one.
Elliott Hughes08fc03a2012-06-26 17:34:00 -07002001 for (size_t i = 0; i < work_line_->GetMonitorEnterCount(); ++i) {
2002 monitor_enter_dex_pcs_->push_back(work_line_->GetMonitorEnterDexPc(i));
2003 }
2004 }
2005
jeffhaobdb76512011-09-07 11:43:16 -07002006 /*
2007 * Once we finish decoding the instruction, we need to figure out where
jeffhaod1f0fde2011-09-08 17:25:33 -07002008 * we can go from here. There are three possible ways to transfer
jeffhaobdb76512011-09-07 11:43:16 -07002009 * control to another statement:
2010 *
jeffhaod1f0fde2011-09-08 17:25:33 -07002011 * (1) Continue to the next instruction. Applies to all but
jeffhaobdb76512011-09-07 11:43:16 -07002012 * unconditional branches, method returns, and exception throws.
jeffhaod1f0fde2011-09-08 17:25:33 -07002013 * (2) Branch to one or more possible locations. Applies to branches
jeffhaobdb76512011-09-07 11:43:16 -07002014 * and switch statements.
jeffhaod1f0fde2011-09-08 17:25:33 -07002015 * (3) Exception handlers. Applies to any instruction that can
jeffhaobdb76512011-09-07 11:43:16 -07002016 * throw an exception that is handled by an encompassing "try"
2017 * block.
2018 *
2019 * We can also return, in which case there is no successor instruction
2020 * from this point.
2021 *
Elliott Hughesadb8c672012-03-06 16:49:32 -08002022 * The behavior can be determined from the opcode flags.
jeffhaobdb76512011-09-07 11:43:16 -07002023 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002024 const uint16_t* insns = code_item_->insns_ + work_insn_idx_;
2025 const Instruction* inst = Instruction::At(insns);
Ian Rogersa75a0132012-09-28 11:41:42 -07002026 int opcode_flags = Instruction::FlagsOf(inst->Opcode());
jeffhaobdb76512011-09-07 11:43:16 -07002027
jeffhaobdb76512011-09-07 11:43:16 -07002028 int32_t branch_target = 0;
jeffhaobdb76512011-09-07 11:43:16 -07002029 bool just_set_result = false;
Andreas Gampeec6e6c12015-11-05 20:39:56 -08002030 if (kDebugVerify) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002031 // Generate processing back trace to debug verifier
Elliott Hughesc073b072012-05-24 19:29:17 -07002032 LogVerifyInfo() << "Processing " << inst->DumpString(dex_file_) << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07002033 << work_line_->Dump(this) << "\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07002034 }
jeffhaobdb76512011-09-07 11:43:16 -07002035
2036 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002037 * Make a copy of the previous register state. If the instruction
jeffhaobdb76512011-09-07 11:43:16 -07002038 * can throw an exception, we will copy/merge this into the "catch"
2039 * address rather than work_line, because we don't want the result
2040 * from the "successful" code path (e.g. a check-cast that "improves"
2041 * a type) to be visible to the exception handler.
2042 */
Ian Rogers776ac1f2012-04-13 23:36:36 -07002043 if ((opcode_flags & Instruction::kThrow) != 0 && CurrentInsnFlags()->IsInTry()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002044 saved_line_->CopyFromLine(work_line_.get());
Ian Rogers1ff3c982014-08-12 02:30:58 -07002045 } else if (kIsDebugBuild) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002046 saved_line_->FillWithGarbage();
jeffhaobdb76512011-09-07 11:43:16 -07002047 }
Andreas Gamped12e7822015-06-25 10:26:40 -07002048 DCHECK(!have_pending_runtime_throw_failure_); // Per-instruction flag, should not be set here.
jeffhaobdb76512011-09-07 11:43:16 -07002049
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07002050
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002051 // We need to ensure the work line is consistent while performing validation. When we spot a
2052 // peephole pattern we compute a new line for either the fallthrough instruction or the
2053 // branch target.
Mathieu Chartier361e04a2016-02-16 14:06:35 -08002054 RegisterLineArenaUniquePtr branch_line;
2055 RegisterLineArenaUniquePtr fallthrough_line;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002056
Sebastien Hertz5243e912013-05-21 10:55:07 +02002057 switch (inst->Opcode()) {
jeffhaobdb76512011-09-07 11:43:16 -07002058 case Instruction::NOP:
2059 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002060 * A "pure" NOP has no effect on anything. Data tables start with
jeffhaobdb76512011-09-07 11:43:16 -07002061 * a signature that looks like a NOP; if we see one of these in
2062 * the course of executing code then we have a problem.
2063 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002064 if (inst->VRegA_10x() != 0) {
jeffhaod5347e02012-03-22 17:25:05 -07002065 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "encountered data table in instruction stream";
jeffhaobdb76512011-09-07 11:43:16 -07002066 }
2067 break;
2068
2069 case Instruction::MOVE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002070 work_line_->CopyRegister1(this, inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategory1nr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02002071 break;
jeffhaobdb76512011-09-07 11:43:16 -07002072 case Instruction::MOVE_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07002073 work_line_->CopyRegister1(this, inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategory1nr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02002074 break;
jeffhaobdb76512011-09-07 11:43:16 -07002075 case Instruction::MOVE_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07002076 work_line_->CopyRegister1(this, inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategory1nr);
jeffhaobdb76512011-09-07 11:43:16 -07002077 break;
2078 case Instruction::MOVE_WIDE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002079 work_line_->CopyRegister2(this, inst->VRegA_12x(), inst->VRegB_12x());
Sebastien Hertz5243e912013-05-21 10:55:07 +02002080 break;
jeffhaobdb76512011-09-07 11:43:16 -07002081 case Instruction::MOVE_WIDE_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07002082 work_line_->CopyRegister2(this, inst->VRegA_22x(), inst->VRegB_22x());
Sebastien Hertz5243e912013-05-21 10:55:07 +02002083 break;
jeffhaobdb76512011-09-07 11:43:16 -07002084 case Instruction::MOVE_WIDE_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07002085 work_line_->CopyRegister2(this, inst->VRegA_32x(), inst->VRegB_32x());
jeffhaobdb76512011-09-07 11:43:16 -07002086 break;
2087 case Instruction::MOVE_OBJECT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002088 work_line_->CopyRegister1(this, inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategoryRef);
Sebastien Hertz5243e912013-05-21 10:55:07 +02002089 break;
jeffhaobdb76512011-09-07 11:43:16 -07002090 case Instruction::MOVE_OBJECT_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07002091 work_line_->CopyRegister1(this, inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategoryRef);
Sebastien Hertz5243e912013-05-21 10:55:07 +02002092 break;
jeffhaobdb76512011-09-07 11:43:16 -07002093 case Instruction::MOVE_OBJECT_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07002094 work_line_->CopyRegister1(this, inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategoryRef);
jeffhaobdb76512011-09-07 11:43:16 -07002095 break;
2096
2097 /*
2098 * The move-result instructions copy data out of a "pseudo-register"
jeffhaod1f0fde2011-09-08 17:25:33 -07002099 * with the results from the last method invocation. In practice we
jeffhaobdb76512011-09-07 11:43:16 -07002100 * might want to hold the result in an actual CPU register, so the
2101 * Dalvik spec requires that these only appear immediately after an
2102 * invoke or filled-new-array.
2103 *
jeffhaod1f0fde2011-09-08 17:25:33 -07002104 * These calls invalidate the "result" register. (This is now
jeffhaobdb76512011-09-07 11:43:16 -07002105 * redundant with the reset done below, but it can make the debug info
2106 * easier to read in some cases.)
2107 */
2108 case Instruction::MOVE_RESULT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002109 work_line_->CopyResultRegister1(this, inst->VRegA_11x(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002110 break;
2111 case Instruction::MOVE_RESULT_WIDE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002112 work_line_->CopyResultRegister2(this, inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07002113 break;
2114 case Instruction::MOVE_RESULT_OBJECT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002115 work_line_->CopyResultRegister1(this, inst->VRegA_11x(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002116 break;
2117
Ian Rogersd81871c2011-10-03 13:57:23 -07002118 case Instruction::MOVE_EXCEPTION: {
Sebastien Hertz270a0e12015-01-16 19:49:09 +01002119 // We do not allow MOVE_EXCEPTION as the first instruction in a method. This is a simple case
2120 // where one entrypoint to the catch block is not actually an exception path.
2121 if (work_insn_idx_ == 0) {
2122 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "move-exception at pc 0x0";
2123 break;
2124 }
jeffhaobdb76512011-09-07 11:43:16 -07002125 /*
jeffhao60f83e32012-02-13 17:16:30 -08002126 * This statement can only appear as the first instruction in an exception handler. We verify
2127 * that as part of extracting the exception type from the catch block list.
jeffhaobdb76512011-09-07 11:43:16 -07002128 */
Ian Rogersd8f69b02014-09-10 21:43:52 +00002129 const RegType& res_type = GetCaughtExceptionType();
Andreas Gampead238ce2015-08-24 21:13:08 -07002130 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_11x(), res_type);
jeffhaobdb76512011-09-07 11:43:16 -07002131 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002132 }
jeffhaobdb76512011-09-07 11:43:16 -07002133 case Instruction::RETURN_VOID:
Andreas Gampef10b6e12015-08-12 10:48:12 -07002134 if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002135 if (!GetMethodReturnType().IsConflict()) {
jeffhaod5347e02012-03-22 17:25:05 -07002136 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07002137 }
jeffhaobdb76512011-09-07 11:43:16 -07002138 }
2139 break;
2140 case Instruction::RETURN:
Andreas Gampef10b6e12015-08-12 10:48:12 -07002141 if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) {
jeffhaobdb76512011-09-07 11:43:16 -07002142 /* check the method signature */
Ian Rogersd8f69b02014-09-10 21:43:52 +00002143 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07002144 if (!return_type.IsCategory1Types()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002145 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected non-category 1 return type "
2146 << return_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002147 } else {
2148 // Compilers may generate synthetic functions that write byte values into boolean fields.
2149 // Also, it may use integer values for boolean, byte, short, and character return types.
Sebastien Hertz5243e912013-05-21 10:55:07 +02002150 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07002151 const RegType& src_type = work_line_->GetRegisterType(this, vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07002152 bool use_src = ((return_type.IsBoolean() && src_type.IsByte()) ||
2153 ((return_type.IsBoolean() || return_type.IsByte() ||
2154 return_type.IsShort() || return_type.IsChar()) &&
2155 src_type.IsInteger()));
2156 /* check the register contents */
Ian Rogersad0b3a32012-04-16 14:50:24 -07002157 bool success =
Ian Rogers7b078e82014-09-10 14:44:24 -07002158 work_line_->VerifyRegisterType(this, vregA, use_src ? src_type : return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002159 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002160 AppendToLastFailMessage(StringPrintf(" return-1nr on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07002161 }
jeffhaobdb76512011-09-07 11:43:16 -07002162 }
2163 }
2164 break;
2165 case Instruction::RETURN_WIDE:
Andreas Gampef10b6e12015-08-12 10:48:12 -07002166 if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) {
jeffhaobdb76512011-09-07 11:43:16 -07002167 /* check the method signature */
Ian Rogersd8f69b02014-09-10 21:43:52 +00002168 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07002169 if (!return_type.IsCategory2Types()) {
jeffhaod5347e02012-03-22 17:25:05 -07002170 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-wide not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07002171 } else {
2172 /* check the register contents */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002173 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07002174 bool success = work_line_->VerifyRegisterType(this, vregA, return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002175 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002176 AppendToLastFailMessage(StringPrintf(" return-wide on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07002177 }
jeffhaobdb76512011-09-07 11:43:16 -07002178 }
2179 }
2180 break;
2181 case Instruction::RETURN_OBJECT:
Andreas Gampef10b6e12015-08-12 10:48:12 -07002182 if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) {
Ian Rogersd8f69b02014-09-10 21:43:52 +00002183 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07002184 if (!return_type.IsReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002185 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-object not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07002186 } else {
2187 /* return_type is the *expected* return type, not register value */
2188 DCHECK(!return_type.IsZero());
2189 DCHECK(!return_type.IsUninitializedReference());
Sebastien Hertz5243e912013-05-21 10:55:07 +02002190 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07002191 const RegType& reg_type = work_line_->GetRegisterType(this, vregA);
Andreas Gampea32210c2015-06-24 10:26:13 -07002192 // Disallow returning undefined, conflict & uninitialized values and verify that the
2193 // reference in vAA is an instance of the "return_type."
2194 if (reg_type.IsUndefined()) {
2195 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning undefined register";
2196 } else if (reg_type.IsConflict()) {
2197 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning register with conflict";
2198 } else if (reg_type.IsUninitializedTypes()) {
David Brazdil68b5c0b2016-01-19 14:25:29 +00002199 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning uninitialized object '"
Brian Carlstrom93c33962013-07-26 10:37:43 -07002200 << reg_type << "'";
Andreas Gampea4c98f22015-11-06 16:24:49 -08002201 } else if (!reg_type.IsReferenceTypes()) {
2202 // We really do expect a reference here.
2203 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-object returns a non-reference type "
2204 << reg_type;
David Brazdilca3c8c32016-09-06 14:04:48 +01002205 } else if (!return_type.IsAssignableFrom(reg_type, this)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07002206 if (reg_type.IsUnresolvedTypes() || return_type.IsUnresolvedTypes()) {
2207 Fail(VERIFY_ERROR_NO_CLASS) << " can't resolve returned type '" << return_type
2208 << "' or '" << reg_type << "'";
2209 } else {
Andreas Gampe16f149c2015-03-23 10:10:20 -07002210 bool soft_error = false;
2211 // Check whether arrays are involved. They will show a valid class status, even
2212 // if their components are erroneous.
2213 if (reg_type.IsArrayTypes() && return_type.IsArrayTypes()) {
David Brazdilca3c8c32016-09-06 14:04:48 +01002214 return_type.CanAssignArray(reg_type, reg_types_, class_loader_, this, &soft_error);
Andreas Gampe16f149c2015-03-23 10:10:20 -07002215 if (soft_error) {
2216 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "array with erroneous component type: "
2217 << reg_type << " vs " << return_type;
2218 }
2219 }
2220
2221 if (!soft_error) {
2222 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning '" << reg_type
2223 << "', but expected from declaration '" << return_type << "'";
2224 }
Jeff Haoa3faaf42013-09-03 19:07:00 -07002225 }
jeffhaobdb76512011-09-07 11:43:16 -07002226 }
2227 }
2228 }
2229 break;
2230
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07002231 /* could be boolean, int, float, or a null reference */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002232 case Instruction::CONST_4: {
2233 int32_t val = static_cast<int32_t>(inst->VRegB_11n() << 28) >> 28;
Andreas Gampead238ce2015-08-24 21:13:08 -07002234 work_line_->SetRegisterType<LockOp::kClear>(
2235 this, inst->VRegA_11n(), DetermineCat1Constant(val, need_precise_constants_));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07002236 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02002237 }
2238 case Instruction::CONST_16: {
2239 int16_t val = static_cast<int16_t>(inst->VRegB_21s());
Andreas Gampead238ce2015-08-24 21:13:08 -07002240 work_line_->SetRegisterType<LockOp::kClear>(
2241 this, inst->VRegA_21s(), DetermineCat1Constant(val, need_precise_constants_));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07002242 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02002243 }
Sebastien Hertz849600b2013-12-20 10:28:08 +01002244 case Instruction::CONST: {
2245 int32_t val = inst->VRegB_31i();
Andreas Gampead238ce2015-08-24 21:13:08 -07002246 work_line_->SetRegisterType<LockOp::kClear>(
2247 this, inst->VRegA_31i(), DetermineCat1Constant(val, need_precise_constants_));
jeffhaobdb76512011-09-07 11:43:16 -07002248 break;
Sebastien Hertz849600b2013-12-20 10:28:08 +01002249 }
2250 case Instruction::CONST_HIGH16: {
2251 int32_t val = static_cast<int32_t>(inst->VRegB_21h() << 16);
Andreas Gampead238ce2015-08-24 21:13:08 -07002252 work_line_->SetRegisterType<LockOp::kClear>(
2253 this, inst->VRegA_21h(), DetermineCat1Constant(val, need_precise_constants_));
jeffhaobdb76512011-09-07 11:43:16 -07002254 break;
Sebastien Hertz849600b2013-12-20 10:28:08 +01002255 }
jeffhaobdb76512011-09-07 11:43:16 -07002256 /* could be long or double; resolved upon use */
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002257 case Instruction::CONST_WIDE_16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002258 int64_t val = static_cast<int16_t>(inst->VRegB_21s());
Ian Rogersd8f69b02014-09-10 21:43:52 +00002259 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
2260 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07002261 work_line_->SetRegisterTypeWide(this, inst->VRegA_21s(), lo, hi);
jeffhaobdb76512011-09-07 11:43:16 -07002262 break;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002263 }
2264 case Instruction::CONST_WIDE_32: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002265 int64_t val = static_cast<int32_t>(inst->VRegB_31i());
Ian Rogersd8f69b02014-09-10 21:43:52 +00002266 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
2267 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07002268 work_line_->SetRegisterTypeWide(this, inst->VRegA_31i(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002269 break;
2270 }
2271 case Instruction::CONST_WIDE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002272 int64_t val = inst->VRegB_51l();
Ian Rogersd8f69b02014-09-10 21:43:52 +00002273 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
2274 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07002275 work_line_->SetRegisterTypeWide(this, inst->VRegA_51l(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002276 break;
2277 }
2278 case Instruction::CONST_WIDE_HIGH16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002279 int64_t val = static_cast<uint64_t>(inst->VRegB_21h()) << 48;
Ian Rogersd8f69b02014-09-10 21:43:52 +00002280 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
2281 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07002282 work_line_->SetRegisterTypeWide(this, inst->VRegA_21h(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002283 break;
2284 }
jeffhaobdb76512011-09-07 11:43:16 -07002285 case Instruction::CONST_STRING:
Andreas Gampead238ce2015-08-24 21:13:08 -07002286 work_line_->SetRegisterType<LockOp::kClear>(
2287 this, inst->VRegA_21c(), reg_types_.JavaLangString());
Sebastien Hertz5243e912013-05-21 10:55:07 +02002288 break;
jeffhaobdb76512011-09-07 11:43:16 -07002289 case Instruction::CONST_STRING_JUMBO:
Andreas Gampead238ce2015-08-24 21:13:08 -07002290 work_line_->SetRegisterType<LockOp::kClear>(
2291 this, inst->VRegA_31c(), reg_types_.JavaLangString());
jeffhaobdb76512011-09-07 11:43:16 -07002292 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002293 case Instruction::CONST_CLASS: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002294 // Get type from instruction if unresolved then we need an access check
2295 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Andreas Gampea5b09a62016-11-17 15:21:22 -08002296 const RegType& res_type = ResolveClassAndCheckAccess(dex::TypeIndex(inst->VRegB_21c()));
Ian Rogersad0b3a32012-04-16 14:50:24 -07002297 // Register holds class, ie its type is class, on error it will hold Conflict.
Andreas Gampead238ce2015-08-24 21:13:08 -07002298 work_line_->SetRegisterType<LockOp::kClear>(
2299 this, inst->VRegA_21c(), res_type.IsConflict() ? res_type
2300 : reg_types_.JavaLangClass());
jeffhaobdb76512011-09-07 11:43:16 -07002301 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002302 }
jeffhaobdb76512011-09-07 11:43:16 -07002303 case Instruction::MONITOR_ENTER:
Ian Rogers7b078e82014-09-10 14:44:24 -07002304 work_line_->PushMonitor(this, inst->VRegA_11x(), work_insn_idx_);
Andreas Gampec1474102015-08-18 08:57:44 -07002305 // Check whether the previous instruction is a move-object with vAA as a source, creating
2306 // untracked lock aliasing.
Mathieu Chartierde40d472015-10-15 17:47:48 -07002307 if (0 != work_insn_idx_ && !GetInstructionFlags(work_insn_idx_).IsBranchTarget()) {
Andreas Gampec1474102015-08-18 08:57:44 -07002308 uint32_t prev_idx = work_insn_idx_ - 1;
Mathieu Chartierde40d472015-10-15 17:47:48 -07002309 while (0 != prev_idx && !GetInstructionFlags(prev_idx).IsOpcode()) {
Andreas Gampec1474102015-08-18 08:57:44 -07002310 prev_idx--;
2311 }
2312 const Instruction* prev_inst = Instruction::At(code_item_->insns_ + prev_idx);
2313 switch (prev_inst->Opcode()) {
2314 case Instruction::MOVE_OBJECT:
2315 case Instruction::MOVE_OBJECT_16:
2316 case Instruction::MOVE_OBJECT_FROM16:
2317 if (prev_inst->VRegB() == inst->VRegA_11x()) {
2318 // Redo the copy. This won't change the register types, but update the lock status
2319 // for the aliased register.
2320 work_line_->CopyRegister1(this,
2321 prev_inst->VRegA(),
2322 prev_inst->VRegB(),
2323 kTypeCategoryRef);
2324 }
2325 break;
2326
2327 default: // Other instruction types ignored.
2328 break;
2329 }
2330 }
jeffhaobdb76512011-09-07 11:43:16 -07002331 break;
2332 case Instruction::MONITOR_EXIT:
2333 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002334 * monitor-exit instructions are odd. They can throw exceptions,
jeffhaobdb76512011-09-07 11:43:16 -07002335 * but when they do they act as if they succeeded and the PC is
jeffhaod1f0fde2011-09-08 17:25:33 -07002336 * pointing to the following instruction. (This behavior goes back
jeffhaobdb76512011-09-07 11:43:16 -07002337 * to the need to handle asynchronous exceptions, a now-deprecated
2338 * feature that Dalvik doesn't support.)
2339 *
jeffhaod1f0fde2011-09-08 17:25:33 -07002340 * In practice we don't need to worry about this. The only
jeffhaobdb76512011-09-07 11:43:16 -07002341 * exceptions that can be thrown from monitor-exit are for a
jeffhaod1f0fde2011-09-08 17:25:33 -07002342 * null reference and -exit without a matching -enter. If the
jeffhaobdb76512011-09-07 11:43:16 -07002343 * structured locking checks are working, the former would have
2344 * failed on the -enter instruction, and the latter is impossible.
2345 *
2346 * This is fortunate, because issue 3221411 prevents us from
2347 * chasing the "can throw" path when monitor verification is
jeffhaod1f0fde2011-09-08 17:25:33 -07002348 * enabled. If we can fully verify the locking we can ignore
jeffhaobdb76512011-09-07 11:43:16 -07002349 * some catch blocks (which will show up as "dead" code when
2350 * we skip them here); if we can't, then the code path could be
2351 * "live" so we still need to check it.
2352 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002353 opcode_flags &= ~Instruction::kThrow;
Ian Rogers7b078e82014-09-10 14:44:24 -07002354 work_line_->PopMonitor(this, inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07002355 break;
Ian Rogers28ad40d2011-10-27 15:19:26 -07002356 case Instruction::CHECK_CAST:
Ian Rogersd81871c2011-10-03 13:57:23 -07002357 case Instruction::INSTANCE_OF: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002358 /*
2359 * If this instruction succeeds, we will "downcast" register vA to the type in vB. (This
2360 * could be a "upcast" -- not expected, so we don't try to address it.)
2361 *
2362 * If it fails, an exception is thrown, which we deal with later by ignoring the update to
Elliott Hughesadb8c672012-03-06 16:49:32 -08002363 * dec_insn.vA when branching to a handler.
Ian Rogers28ad40d2011-10-27 15:19:26 -07002364 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002365 const bool is_checkcast = (inst->Opcode() == Instruction::CHECK_CAST);
Andreas Gampea5b09a62016-11-17 15:21:22 -08002366 const dex::TypeIndex type_idx((is_checkcast) ? inst->VRegB_21c() : inst->VRegC_22c());
Ian Rogersd8f69b02014-09-10 21:43:52 +00002367 const RegType& res_type = ResolveClassAndCheckAccess(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002368 if (res_type.IsConflict()) {
Andreas Gampe00633eb2014-07-17 16:13:35 -07002369 // If this is a primitive type, fail HARD.
Mathieu Chartierbf99f772014-08-23 16:37:27 -07002370 mirror::Class* klass = dex_cache_->GetResolvedType(type_idx);
Andreas Gampe00633eb2014-07-17 16:13:35 -07002371 if (klass != nullptr && klass->IsPrimitive()) {
2372 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "using primitive type "
2373 << dex_file_->StringByTypeIdx(type_idx) << " in instanceof in "
2374 << GetDeclaringClass();
2375 break;
2376 }
2377
Ian Rogersad0b3a32012-04-16 14:50:24 -07002378 DCHECK_NE(failures_.size(), 0U);
2379 if (!is_checkcast) {
Andreas Gampead238ce2015-08-24 21:13:08 -07002380 work_line_->SetRegisterType<LockOp::kClear>(this,
2381 inst->VRegA_22c(),
2382 reg_types_.Boolean());
Ian Rogersad0b3a32012-04-16 14:50:24 -07002383 }
2384 break; // bad class
Ian Rogers9f1ab122011-12-12 08:52:43 -08002385 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002386 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Sebastien Hertz5243e912013-05-21 10:55:07 +02002387 uint32_t orig_type_reg = (is_checkcast) ? inst->VRegA_21c() : inst->VRegB_22c();
Ian Rogers7b078e82014-09-10 14:44:24 -07002388 const RegType& orig_type = work_line_->GetRegisterType(this, orig_type_reg);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002389 if (!res_type.IsNonZeroReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002390 if (is_checkcast) {
2391 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on unexpected class " << res_type;
2392 } else {
2393 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on unexpected class " << res_type;
2394 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002395 } else if (!orig_type.IsReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002396 if (is_checkcast) {
2397 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on non-reference in v" << orig_type_reg;
2398 } else {
2399 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on non-reference in v" << orig_type_reg;
2400 }
David Brazdil68b5c0b2016-01-19 14:25:29 +00002401 } else if (orig_type.IsUninitializedTypes()) {
2402 if (is_checkcast) {
2403 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on uninitialized reference in v"
2404 << orig_type_reg;
2405 } else {
2406 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on uninitialized reference in v"
2407 << orig_type_reg;
2408 }
jeffhao2a8a90e2011-09-26 14:25:31 -07002409 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002410 if (is_checkcast) {
Andreas Gampead238ce2015-08-24 21:13:08 -07002411 work_line_->SetRegisterType<LockOp::kKeep>(this, inst->VRegA_21c(), res_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07002412 } else {
Andreas Gampead238ce2015-08-24 21:13:08 -07002413 work_line_->SetRegisterType<LockOp::kClear>(this,
2414 inst->VRegA_22c(),
2415 reg_types_.Boolean());
jeffhaobdb76512011-09-07 11:43:16 -07002416 }
jeffhaobdb76512011-09-07 11:43:16 -07002417 }
jeffhao2a8a90e2011-09-26 14:25:31 -07002418 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002419 }
2420 case Instruction::ARRAY_LENGTH: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002421 const RegType& res_type = work_line_->GetRegisterType(this, inst->VRegB_12x());
Ian Rogers28ad40d2011-10-27 15:19:26 -07002422 if (res_type.IsReferenceTypes()) {
Ian Rogers89310de2012-02-01 13:47:30 -08002423 if (!res_type.IsArrayTypes() && !res_type.IsZero()) { // ie not an array or null
jeffhaod5347e02012-03-22 17:25:05 -07002424 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002425 } else {
Andreas Gampead238ce2015-08-24 21:13:08 -07002426 work_line_->SetRegisterType<LockOp::kClear>(this,
2427 inst->VRegA_12x(),
2428 reg_types_.Integer());
Ian Rogersd81871c2011-10-03 13:57:23 -07002429 }
Andreas Gampe65c9db82014-07-28 13:14:34 -07002430 } else {
2431 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002432 }
2433 break;
2434 }
2435 case Instruction::NEW_INSTANCE: {
Andreas Gampea5b09a62016-11-17 15:21:22 -08002436 const RegType& res_type = ResolveClassAndCheckAccess(dex::TypeIndex(inst->VRegB_21c()));
Ian Rogersad0b3a32012-04-16 14:50:24 -07002437 if (res_type.IsConflict()) {
2438 DCHECK_NE(failures_.size(), 0U);
2439 break; // bad class
jeffhao8cd6dda2012-02-22 10:15:34 -08002440 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002441 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
2442 // can't create an instance of an interface or abstract class */
2443 if (!res_type.IsInstantiableTypes()) {
2444 Fail(VERIFY_ERROR_INSTANTIATION)
2445 << "new-instance on primitive, interface or abstract class" << res_type;
Ian Rogers08f753d2012-08-24 14:35:25 -07002446 // Soft failure so carry on to set register type.
Ian Rogersd81871c2011-10-03 13:57:23 -07002447 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00002448 const RegType& uninit_type = reg_types_.Uninitialized(res_type, work_insn_idx_);
Ian Rogers08f753d2012-08-24 14:35:25 -07002449 // Any registers holding previous allocations from this address that have not yet been
2450 // initialized must be marked invalid.
Ian Rogers7b078e82014-09-10 14:44:24 -07002451 work_line_->MarkUninitRefsAsInvalid(this, uninit_type);
Ian Rogers08f753d2012-08-24 14:35:25 -07002452 // add the new uninitialized reference to the register state
Andreas Gampead238ce2015-08-24 21:13:08 -07002453 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_21c(), uninit_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07002454 break;
2455 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08002456 case Instruction::NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002457 VerifyNewArray(inst, false, false);
jeffhaobdb76512011-09-07 11:43:16 -07002458 break;
2459 case Instruction::FILLED_NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002460 VerifyNewArray(inst, true, false);
Ian Rogers0c4a5062012-02-03 15:18:59 -08002461 just_set_result = true; // Filled new array sets result register
jeffhaobdb76512011-09-07 11:43:16 -07002462 break;
Ian Rogers0c4a5062012-02-03 15:18:59 -08002463 case Instruction::FILLED_NEW_ARRAY_RANGE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002464 VerifyNewArray(inst, true, true);
Ian Rogers0c4a5062012-02-03 15:18:59 -08002465 just_set_result = true; // Filled new array range sets result register
2466 break;
jeffhaobdb76512011-09-07 11:43:16 -07002467 case Instruction::CMPL_FLOAT:
2468 case Instruction::CMPG_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002469 if (!work_line_->VerifyRegisterType(this, inst->VRegB_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08002470 break;
2471 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002472 if (!work_line_->VerifyRegisterType(this, inst->VRegC_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08002473 break;
2474 }
Andreas Gampead238ce2015-08-24 21:13:08 -07002475 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002476 break;
2477 case Instruction::CMPL_DOUBLE:
2478 case Instruction::CMPG_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002479 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegB_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002480 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002481 break;
2482 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002483 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegC_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002484 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002485 break;
2486 }
Andreas Gampead238ce2015-08-24 21:13:08 -07002487 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002488 break;
2489 case Instruction::CMP_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002490 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegB_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002491 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002492 break;
2493 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002494 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegC_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002495 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002496 break;
2497 }
Andreas Gampead238ce2015-08-24 21:13:08 -07002498 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002499 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002500 case Instruction::THROW: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002501 const RegType& res_type = work_line_->GetRegisterType(this, inst->VRegA_11x());
David Brazdilca3c8c32016-09-06 14:04:48 +01002502 if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(res_type, this)) {
David Brazdil68b5c0b2016-01-19 14:25:29 +00002503 if (res_type.IsUninitializedTypes()) {
2504 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "thrown exception not initialized";
Pavel Vyssotski980027c2016-02-11 20:28:11 +06002505 } else if (!res_type.IsReferenceTypes()) {
2506 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "thrown value of non-reference type " << res_type;
David Brazdil68b5c0b2016-01-19 14:25:29 +00002507 } else {
2508 Fail(res_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS : VERIFY_ERROR_BAD_CLASS_SOFT)
2509 << "thrown class " << res_type << " not instanceof Throwable";
2510 }
jeffhaobdb76512011-09-07 11:43:16 -07002511 }
2512 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002513 }
jeffhaobdb76512011-09-07 11:43:16 -07002514 case Instruction::GOTO:
2515 case Instruction::GOTO_16:
2516 case Instruction::GOTO_32:
2517 /* no effect on or use of registers */
2518 break;
2519
2520 case Instruction::PACKED_SWITCH:
2521 case Instruction::SPARSE_SWITCH:
2522 /* verify that vAA is an integer, or can be converted to one */
Ian Rogers7b078e82014-09-10 14:44:24 -07002523 work_line_->VerifyRegisterType(this, inst->VRegA_31t(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002524 break;
2525
Ian Rogersd81871c2011-10-03 13:57:23 -07002526 case Instruction::FILL_ARRAY_DATA: {
2527 /* Similar to the verification done for APUT */
Ian Rogers7b078e82014-09-10 14:44:24 -07002528 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegA_31t());
Ian Rogers89310de2012-02-01 13:47:30 -08002529 /* array_type can be null if the reg type is Zero */
2530 if (!array_type.IsZero()) {
jeffhao457cc512012-02-02 16:55:13 -08002531 if (!array_type.IsArrayTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002532 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with array type "
2533 << array_type;
Andreas Gampebb18a032016-03-22 20:34:25 -07002534 } else if (array_type.IsUnresolvedTypes()) {
2535 // If it's an unresolved array type, it must be non-primitive.
2536 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data for array of type "
2537 << array_type;
Ian Rogers89310de2012-02-01 13:47:30 -08002538 } else {
Ian Rogersd8f69b02014-09-10 21:43:52 +00002539 const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
Ian Rogersad0b3a32012-04-16 14:50:24 -07002540 DCHECK(!component_type.IsConflict());
jeffhao457cc512012-02-02 16:55:13 -08002541 if (component_type.IsNonZeroReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002542 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with component type "
2543 << component_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002544 } else {
jeffhao457cc512012-02-02 16:55:13 -08002545 // Now verify if the element width in the table matches the element width declared in
2546 // the array
Andreas Gampe53de99c2015-08-17 13:43:55 -07002547 const uint16_t* array_data =
2548 insns + (insns[1] | (static_cast<int32_t>(insns[2]) << 16));
jeffhao457cc512012-02-02 16:55:13 -08002549 if (array_data[0] != Instruction::kArrayDataSignature) {
jeffhaod5347e02012-03-22 17:25:05 -07002550 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid magic for array-data";
jeffhao457cc512012-02-02 16:55:13 -08002551 } else {
2552 size_t elem_width = Primitive::ComponentSize(component_type.GetPrimitiveType());
2553 // Since we don't compress the data in Dex, expect to see equal width of data stored
2554 // in the table and expected from the array class.
2555 if (array_data[1] != elem_width) {
jeffhaod5347e02012-03-22 17:25:05 -07002556 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-data size mismatch (" << array_data[1]
2557 << " vs " << elem_width << ")";
jeffhao457cc512012-02-02 16:55:13 -08002558 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002559 }
2560 }
jeffhaobdb76512011-09-07 11:43:16 -07002561 }
2562 }
2563 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002564 }
jeffhaobdb76512011-09-07 11:43:16 -07002565 case Instruction::IF_EQ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002566 case Instruction::IF_NE: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002567 const RegType& reg_type1 = work_line_->GetRegisterType(this, inst->VRegA_22t());
2568 const RegType& reg_type2 = work_line_->GetRegisterType(this, inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002569 bool mismatch = false;
2570 if (reg_type1.IsZero()) { // zero then integral or reference expected
2571 mismatch = !reg_type2.IsReferenceTypes() && !reg_type2.IsIntegralTypes();
2572 } else if (reg_type1.IsReferenceTypes()) { // both references?
2573 mismatch = !reg_type2.IsReferenceTypes();
2574 } else { // both integral?
2575 mismatch = !reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes();
2576 }
2577 if (mismatch) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002578 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to if-eq/if-ne (" << reg_type1 << ","
2579 << reg_type2 << ") must both be references or integral";
jeffhaobdb76512011-09-07 11:43:16 -07002580 }
2581 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002582 }
jeffhaobdb76512011-09-07 11:43:16 -07002583 case Instruction::IF_LT:
2584 case Instruction::IF_GE:
2585 case Instruction::IF_GT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002586 case Instruction::IF_LE: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002587 const RegType& reg_type1 = work_line_->GetRegisterType(this, inst->VRegA_22t());
2588 const RegType& reg_type2 = work_line_->GetRegisterType(this, inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002589 if (!reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002590 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to 'if' (" << reg_type1 << ","
2591 << reg_type2 << ") must be integral";
jeffhaobdb76512011-09-07 11:43:16 -07002592 }
2593 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002594 }
jeffhaobdb76512011-09-07 11:43:16 -07002595 case Instruction::IF_EQZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002596 case Instruction::IF_NEZ: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002597 const RegType& reg_type = work_line_->GetRegisterType(this, inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002598 if (!reg_type.IsReferenceTypes() && !reg_type.IsIntegralTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002599 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
2600 << " unexpected as arg to if-eqz/if-nez";
Ian Rogersd81871c2011-10-03 13:57:23 -07002601 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002602
2603 // Find previous instruction - its existence is a precondition to peephole optimization.
Ian Rogers9b360392013-06-06 14:45:07 -07002604 uint32_t instance_of_idx = 0;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002605 if (0 != work_insn_idx_) {
Ian Rogers9b360392013-06-06 14:45:07 -07002606 instance_of_idx = work_insn_idx_ - 1;
Mathieu Chartierde40d472015-10-15 17:47:48 -07002607 while (0 != instance_of_idx && !GetInstructionFlags(instance_of_idx).IsOpcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07002608 instance_of_idx--;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002609 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07002610 if (FailOrAbort(this, GetInstructionFlags(instance_of_idx).IsOpcode(),
Andreas Gampe7c038102014-10-27 20:08:46 -07002611 "Unable to get previous instruction of if-eqz/if-nez for work index ",
2612 work_insn_idx_)) {
2613 break;
2614 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002615 } else {
2616 break;
2617 }
2618
Ian Rogers9b360392013-06-06 14:45:07 -07002619 const Instruction* instance_of_inst = Instruction::At(code_item_->insns_ + instance_of_idx);
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002620
2621 /* Check for peep-hole pattern of:
2622 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002623 * instance-of vX, vY, T;
2624 * ifXXX vX, label ;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002625 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002626 * label:
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002627 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002628 * and sharpen the type of vY to be type T.
2629 * Note, this pattern can't be if:
2630 * - if there are other branches to this branch,
2631 * - when vX == vY.
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002632 */
Ian Rogersfae370a2013-06-05 08:33:27 -07002633 if (!CurrentInsnFlags()->IsBranchTarget() &&
Ian Rogers9b360392013-06-06 14:45:07 -07002634 (Instruction::INSTANCE_OF == instance_of_inst->Opcode()) &&
2635 (inst->VRegA_21t() == instance_of_inst->VRegA_22c()) &&
2636 (instance_of_inst->VRegA_22c() != instance_of_inst->VRegB_22c())) {
Ian Rogersebbdd872014-07-07 23:53:08 -07002637 // Check the type of the instance-of is different than that of registers type, as if they
2638 // are the same there is no work to be done here. Check that the conversion is not to or
2639 // from an unresolved type as type information is imprecise. If the instance-of is to an
2640 // interface then ignore the type information as interfaces can only be treated as Objects
2641 // and we don't want to disallow field and other operations on the object. If the value
2642 // being instance-of checked against is known null (zero) then allow the optimization as
2643 // we didn't have type information. If the merge of the instance-of type with the original
2644 // type is assignable to the original then allow optimization. This check is performed to
2645 // ensure that subsequent merges don't lose type information - such as becoming an
2646 // interface from a class that would lose information relevant to field checks.
Ian Rogers7b078e82014-09-10 14:44:24 -07002647 const RegType& orig_type = work_line_->GetRegisterType(this, instance_of_inst->VRegB_22c());
Andreas Gampea5b09a62016-11-17 15:21:22 -08002648 const RegType& cast_type = ResolveClassAndCheckAccess(
2649 dex::TypeIndex(instance_of_inst->VRegC_22c()));
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002650
Ian Rogersebbdd872014-07-07 23:53:08 -07002651 if (!orig_type.Equals(cast_type) &&
2652 !cast_type.IsUnresolvedTypes() && !orig_type.IsUnresolvedTypes() &&
Andreas Gampe00633eb2014-07-17 16:13:35 -07002653 cast_type.HasClass() && // Could be conflict type, make sure it has a class.
Ian Rogersebbdd872014-07-07 23:53:08 -07002654 !cast_type.GetClass()->IsInterface() &&
2655 (orig_type.IsZero() ||
David Brazdilca3c8c32016-09-06 14:04:48 +01002656 orig_type.IsStrictlyAssignableFrom(
2657 cast_type.Merge(orig_type, &reg_types_, this), this))) {
Ian Rogersd0fbd852013-09-24 18:17:04 -07002658 RegisterLine* update_line = RegisterLine::Create(code_item_->registers_size_, this);
Ian Rogersfae370a2013-06-05 08:33:27 -07002659 if (inst->Opcode() == Instruction::IF_EQZ) {
Ian Rogers9b360392013-06-06 14:45:07 -07002660 fallthrough_line.reset(update_line);
Ian Rogersfae370a2013-06-05 08:33:27 -07002661 } else {
Ian Rogers9b360392013-06-06 14:45:07 -07002662 branch_line.reset(update_line);
2663 }
2664 update_line->CopyFromLine(work_line_.get());
Andreas Gampead238ce2015-08-24 21:13:08 -07002665 update_line->SetRegisterType<LockOp::kKeep>(this,
2666 instance_of_inst->VRegB_22c(),
2667 cast_type);
Mathieu Chartierde40d472015-10-15 17:47:48 -07002668 if (!GetInstructionFlags(instance_of_idx).IsBranchTarget() && 0 != instance_of_idx) {
Ian Rogers9b360392013-06-06 14:45:07 -07002669 // See if instance-of was preceded by a move-object operation, common due to the small
2670 // register encoding space of instance-of, and propagate type information to the source
2671 // of the move-object.
2672 uint32_t move_idx = instance_of_idx - 1;
Mathieu Chartierde40d472015-10-15 17:47:48 -07002673 while (0 != move_idx && !GetInstructionFlags(move_idx).IsOpcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07002674 move_idx--;
2675 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07002676 if (FailOrAbort(this, GetInstructionFlags(move_idx).IsOpcode(),
Andreas Gampe7c038102014-10-27 20:08:46 -07002677 "Unable to get previous instruction of if-eqz/if-nez for work index ",
2678 work_insn_idx_)) {
2679 break;
2680 }
Ian Rogers9b360392013-06-06 14:45:07 -07002681 const Instruction* move_inst = Instruction::At(code_item_->insns_ + move_idx);
2682 switch (move_inst->Opcode()) {
2683 case Instruction::MOVE_OBJECT:
2684 if (move_inst->VRegA_12x() == instance_of_inst->VRegB_22c()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07002685 update_line->SetRegisterType<LockOp::kKeep>(this,
2686 move_inst->VRegB_12x(),
2687 cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002688 }
2689 break;
2690 case Instruction::MOVE_OBJECT_FROM16:
2691 if (move_inst->VRegA_22x() == instance_of_inst->VRegB_22c()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07002692 update_line->SetRegisterType<LockOp::kKeep>(this,
2693 move_inst->VRegB_22x(),
2694 cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002695 }
2696 break;
2697 case Instruction::MOVE_OBJECT_16:
2698 if (move_inst->VRegA_32x() == instance_of_inst->VRegB_22c()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07002699 update_line->SetRegisterType<LockOp::kKeep>(this,
2700 move_inst->VRegB_32x(),
2701 cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002702 }
2703 break;
2704 default:
2705 break;
2706 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002707 }
2708 }
2709 }
2710
jeffhaobdb76512011-09-07 11:43:16 -07002711 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002712 }
jeffhaobdb76512011-09-07 11:43:16 -07002713 case Instruction::IF_LTZ:
2714 case Instruction::IF_GEZ:
2715 case Instruction::IF_GTZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002716 case Instruction::IF_LEZ: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002717 const RegType& reg_type = work_line_->GetRegisterType(this, inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002718 if (!reg_type.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002719 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
2720 << " unexpected as arg to if-ltz/if-gez/if-gtz/if-lez";
Ian Rogersd81871c2011-10-03 13:57:23 -07002721 }
jeffhaobdb76512011-09-07 11:43:16 -07002722 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002723 }
jeffhaobdb76512011-09-07 11:43:16 -07002724 case Instruction::AGET_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002725 VerifyAGet(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002726 break;
jeffhaobdb76512011-09-07 11:43:16 -07002727 case Instruction::AGET_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002728 VerifyAGet(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002729 break;
jeffhaobdb76512011-09-07 11:43:16 -07002730 case Instruction::AGET_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002731 VerifyAGet(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002732 break;
jeffhaobdb76512011-09-07 11:43:16 -07002733 case Instruction::AGET_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002734 VerifyAGet(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002735 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002736 case Instruction::AGET:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002737 VerifyAGet(inst, reg_types_.Integer(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002738 break;
jeffhaobdb76512011-09-07 11:43:16 -07002739 case Instruction::AGET_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002740 VerifyAGet(inst, reg_types_.LongLo(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002741 break;
2742 case Instruction::AGET_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002743 VerifyAGet(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07002744 break;
2745
Ian Rogersd81871c2011-10-03 13:57:23 -07002746 case Instruction::APUT_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002747 VerifyAPut(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002748 break;
2749 case Instruction::APUT_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002750 VerifyAPut(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002751 break;
2752 case Instruction::APUT_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002753 VerifyAPut(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002754 break;
2755 case Instruction::APUT_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002756 VerifyAPut(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002757 break;
2758 case Instruction::APUT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002759 VerifyAPut(inst, reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002760 break;
2761 case Instruction::APUT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002762 VerifyAPut(inst, reg_types_.LongLo(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002763 break;
2764 case Instruction::APUT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002765 VerifyAPut(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07002766 break;
2767
jeffhaobdb76512011-09-07 11:43:16 -07002768 case Instruction::IGET_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002769 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002770 break;
jeffhaobdb76512011-09-07 11:43:16 -07002771 case Instruction::IGET_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002772 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002773 break;
jeffhaobdb76512011-09-07 11:43:16 -07002774 case Instruction::IGET_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002775 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002776 break;
jeffhaobdb76512011-09-07 11:43:16 -07002777 case Instruction::IGET_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002778 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002779 break;
2780 case Instruction::IGET:
Andreas Gampe896df402014-10-20 22:25:29 -07002781 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002782 break;
2783 case Instruction::IGET_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002784 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002785 break;
2786 case Instruction::IGET_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002787 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false,
2788 false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002789 break;
jeffhaobdb76512011-09-07 11:43:16 -07002790
Ian Rogersd81871c2011-10-03 13:57:23 -07002791 case Instruction::IPUT_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002792 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002793 break;
2794 case Instruction::IPUT_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002795 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002796 break;
2797 case Instruction::IPUT_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002798 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002799 break;
2800 case Instruction::IPUT_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002801 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002802 break;
2803 case Instruction::IPUT:
Andreas Gampe896df402014-10-20 22:25:29 -07002804 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002805 break;
2806 case Instruction::IPUT_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002807 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002808 break;
jeffhaobdb76512011-09-07 11:43:16 -07002809 case Instruction::IPUT_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002810 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false,
2811 false);
jeffhaobdb76512011-09-07 11:43:16 -07002812 break;
2813
jeffhaobdb76512011-09-07 11:43:16 -07002814 case Instruction::SGET_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002815 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002816 break;
jeffhaobdb76512011-09-07 11:43:16 -07002817 case Instruction::SGET_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002818 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002819 break;
jeffhaobdb76512011-09-07 11:43:16 -07002820 case Instruction::SGET_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002821 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002822 break;
jeffhaobdb76512011-09-07 11:43:16 -07002823 case Instruction::SGET_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002824 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002825 break;
2826 case Instruction::SGET:
Andreas Gampe896df402014-10-20 22:25:29 -07002827 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002828 break;
2829 case Instruction::SGET_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002830 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002831 break;
2832 case Instruction::SGET_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002833 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false,
2834 true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002835 break;
2836
2837 case Instruction::SPUT_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002838 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002839 break;
2840 case Instruction::SPUT_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002841 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002842 break;
2843 case Instruction::SPUT_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002844 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002845 break;
2846 case Instruction::SPUT_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002847 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002848 break;
2849 case Instruction::SPUT:
Andreas Gampe896df402014-10-20 22:25:29 -07002850 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002851 break;
2852 case Instruction::SPUT_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002853 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002854 break;
2855 case Instruction::SPUT_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002856 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false,
2857 true);
jeffhaobdb76512011-09-07 11:43:16 -07002858 break;
2859
2860 case Instruction::INVOKE_VIRTUAL:
2861 case Instruction::INVOKE_VIRTUAL_RANGE:
2862 case Instruction::INVOKE_SUPER:
Ian Rogersd81871c2011-10-03 13:57:23 -07002863 case Instruction::INVOKE_SUPER_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002864 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE ||
2865 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002866 bool is_super = (inst->Opcode() == Instruction::INVOKE_SUPER ||
2867 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
Alex Light7268d472016-01-20 15:50:01 -08002868 MethodType type = is_super ? METHOD_SUPER : METHOD_VIRTUAL;
2869 ArtMethod* called_method = VerifyInvocationArgs(inst, type, is_range);
Ian Rogersd8f69b02014-09-10 21:43:52 +00002870 const RegType* return_type = nullptr;
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002871 if (called_method != nullptr) {
Andreas Gampe542451c2016-07-26 09:02:02 -07002872 PointerSize pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
Vladimir Marko05792b92015-08-03 11:56:49 +01002873 mirror::Class* return_type_class = called_method->GetReturnType(can_load_classes_,
2874 pointer_size);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002875 if (return_type_class != nullptr) {
Andreas Gampef23f33d2015-06-23 14:18:17 -07002876 return_type = &FromClass(called_method->GetReturnTypeDescriptor(),
2877 return_type_class,
2878 return_type_class->CannotBeAssignedFromOtherTypes());
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002879 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07002880 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
2881 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002882 }
2883 }
2884 if (return_type == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002885 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002886 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
Andreas Gampea5b09a62016-11-17 15:21:22 -08002887 dex::TypeIndex return_type_idx =
2888 dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002889 const char* descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Mathieu Chartierbf99f772014-08-23 16:37:27 -07002890 return_type = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
jeffhaobdb76512011-09-07 11:43:16 -07002891 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002892 if (!return_type->IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002893 work_line_->SetResultRegisterType(this, *return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002894 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002895 work_line_->SetResultRegisterTypeWide(*return_type, return_type->HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002896 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002897 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002898 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002899 }
jeffhaobdb76512011-09-07 11:43:16 -07002900 case Instruction::INVOKE_DIRECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002901 case Instruction::INVOKE_DIRECT_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002902 bool is_range = (inst->Opcode() == Instruction::INVOKE_DIRECT_RANGE);
Alex Light7268d472016-01-20 15:50:01 -08002903 ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_DIRECT, is_range);
Ian Rogers46685432012-06-03 22:26:43 -07002904 const char* return_type_descriptor;
2905 bool is_constructor;
Ian Rogersd8f69b02014-09-10 21:43:52 +00002906 const RegType* return_type = nullptr;
Ian Rogers7b078e82014-09-10 14:44:24 -07002907 if (called_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002908 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogers46685432012-06-03 22:26:43 -07002909 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
Ian Rogersdfb325e2013-10-30 01:00:44 -07002910 is_constructor = strcmp("<init>", dex_file_->StringDataByIdx(method_id.name_idx_)) == 0;
Andreas Gampea5b09a62016-11-17 15:21:22 -08002911 dex::TypeIndex return_type_idx =
2912 dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogers46685432012-06-03 22:26:43 -07002913 return_type_descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2914 } else {
2915 is_constructor = called_method->IsConstructor();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002916 return_type_descriptor = called_method->GetReturnTypeDescriptor();
Andreas Gampe542451c2016-07-26 09:02:02 -07002917 PointerSize pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
Vladimir Marko05792b92015-08-03 11:56:49 +01002918 mirror::Class* return_type_class = called_method->GetReturnType(can_load_classes_,
2919 pointer_size);
Ian Rogers1ff3c982014-08-12 02:30:58 -07002920 if (return_type_class != nullptr) {
Andreas Gampef23f33d2015-06-23 14:18:17 -07002921 return_type = &FromClass(return_type_descriptor,
2922 return_type_class,
2923 return_type_class->CannotBeAssignedFromOtherTypes());
Ian Rogers1ff3c982014-08-12 02:30:58 -07002924 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07002925 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
2926 self_->ClearException();
Ian Rogers1ff3c982014-08-12 02:30:58 -07002927 }
Ian Rogers46685432012-06-03 22:26:43 -07002928 }
2929 if (is_constructor) {
jeffhaobdb76512011-09-07 11:43:16 -07002930 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002931 * Some additional checks when calling a constructor. We know from the invocation arg check
2932 * that the "this" argument is an instance of called_method->klass. Now we further restrict
2933 * that to require that called_method->klass is the same as this->klass or this->super,
2934 * allowing the latter only if the "this" argument is the same as the "this" argument to
2935 * this method (which implies that we're in a constructor ourselves).
jeffhaobdb76512011-09-07 11:43:16 -07002936 */
Ian Rogers7b078e82014-09-10 14:44:24 -07002937 const RegType& this_type = work_line_->GetInvocationThis(this, inst, is_range);
jeffhaob57e9522012-04-26 18:08:21 -07002938 if (this_type.IsConflict()) // failure.
2939 break;
jeffhaobdb76512011-09-07 11:43:16 -07002940
jeffhaob57e9522012-04-26 18:08:21 -07002941 /* no null refs allowed (?) */
2942 if (this_type.IsZero()) {
2943 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unable to initialize null ref";
2944 break;
jeffhao2a8a90e2011-09-26 14:25:31 -07002945 }
jeffhaob57e9522012-04-26 18:08:21 -07002946
2947 /* must be in same class or in superclass */
Ian Rogersd8f69b02014-09-10 21:43:52 +00002948 // const RegType& this_super_klass = this_type.GetSuperClass(&reg_types_);
Ian Rogers46685432012-06-03 22:26:43 -07002949 // TODO: re-enable constructor type verification
2950 // if (this_super_klass.IsConflict()) {
jeffhaob57e9522012-04-26 18:08:21 -07002951 // Unknown super class, fail so we re-check at runtime.
Ian Rogers46685432012-06-03 22:26:43 -07002952 // Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "super class unknown for '" << this_type << "'";
2953 // break;
2954 // }
jeffhaob57e9522012-04-26 18:08:21 -07002955
2956 /* arg must be an uninitialized reference */
2957 if (!this_type.IsUninitializedTypes()) {
2958 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Expected initialization on uninitialized reference "
2959 << this_type;
2960 break;
2961 }
2962
2963 /*
2964 * Replace the uninitialized reference with an initialized one. We need to do this for all
2965 * registers that have the same object instance in them, not just the "this" register.
2966 */
Nicolas Geoffray98e6ce42016-02-16 18:42:15 +00002967 work_line_->MarkRefsAsInitialized(this, this_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002968 }
Ian Rogers1ff3c982014-08-12 02:30:58 -07002969 if (return_type == nullptr) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07002970 return_type = &reg_types_.FromDescriptor(GetClassLoader(), return_type_descriptor, false);
Ian Rogers1ff3c982014-08-12 02:30:58 -07002971 }
2972 if (!return_type->IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002973 work_line_->SetResultRegisterType(this, *return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002974 } else {
Ian Rogers1ff3c982014-08-12 02:30:58 -07002975 work_line_->SetResultRegisterTypeWide(*return_type, return_type->HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002976 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002977 just_set_result = true;
2978 break;
2979 }
2980 case Instruction::INVOKE_STATIC:
2981 case Instruction::INVOKE_STATIC_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002982 bool is_range = (inst->Opcode() == Instruction::INVOKE_STATIC_RANGE);
Alex Light7268d472016-01-20 15:50:01 -08002983 ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_STATIC, is_range);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002984 const char* descriptor;
Ian Rogers7b078e82014-09-10 14:44:24 -07002985 if (called_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002986 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002987 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
Andreas Gampea5b09a62016-11-17 15:21:22 -08002988 dex::TypeIndex return_type_idx =
2989 dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Mathieu Chartier590fee92013-09-13 13:46:47 -07002990 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002991 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002992 descriptor = called_method->GetReturnTypeDescriptor();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002993 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00002994 const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002995 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002996 work_line_->SetResultRegisterType(this, return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002997 } else {
2998 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2999 }
jeffhaobdb76512011-09-07 11:43:16 -07003000 just_set_result = true;
3001 }
3002 break;
jeffhaobdb76512011-09-07 11:43:16 -07003003 case Instruction::INVOKE_INTERFACE:
Ian Rogersd81871c2011-10-03 13:57:23 -07003004 case Instruction::INVOKE_INTERFACE_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003005 bool is_range = (inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE);
Alex Light7268d472016-01-20 15:50:01 -08003006 ArtMethod* abs_method = VerifyInvocationArgs(inst, METHOD_INTERFACE, is_range);
Ian Rogers7b078e82014-09-10 14:44:24 -07003007 if (abs_method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003008 mirror::Class* called_interface = abs_method->GetDeclaringClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07003009 if (!called_interface->IsInterface() && !called_interface->IsObjectClass()) {
3010 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected interface class in invoke-interface '"
David Sehr709b0702016-10-13 09:12:37 -07003011 << abs_method->PrettyMethod() << "'";
Ian Rogersad0b3a32012-04-16 14:50:24 -07003012 break;
Ian Rogers28ad40d2011-10-27 15:19:26 -07003013 }
Ian Rogers0d604842012-04-16 14:50:24 -07003014 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003015 /* Get the type of the "this" arg, which should either be a sub-interface of called
3016 * interface or Object (see comments in RegType::JoinClass).
3017 */
Ian Rogers7b078e82014-09-10 14:44:24 -07003018 const RegType& this_type = work_line_->GetInvocationThis(this, inst, is_range);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003019 if (this_type.IsZero()) {
3020 /* null pointer always passes (and always fails at runtime) */
3021 } else {
3022 if (this_type.IsUninitializedTypes()) {
3023 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface call on uninitialized object "
3024 << this_type;
3025 break;
3026 }
3027 // In the past we have tried to assert that "called_interface" is assignable
3028 // from "this_type.GetClass()", however, as we do an imprecise Join
3029 // (RegType::JoinClass) we don't have full information on what interfaces are
3030 // implemented by "this_type". For example, two classes may implement the same
3031 // interfaces and have a common parent that doesn't implement the interface. The
3032 // join will set "this_type" to the parent class and a test that this implements
3033 // the interface will incorrectly fail.
3034 }
3035 /*
3036 * We don't have an object instance, so we can't find the concrete method. However, all of
3037 * the type information is in the abstract method, so we're good.
3038 */
3039 const char* descriptor;
Ian Rogers7b078e82014-09-10 14:44:24 -07003040 if (abs_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003041 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07003042 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
Andreas Gampea5b09a62016-11-17 15:21:22 -08003043 dex::TypeIndex return_type_idx =
3044 dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Mathieu Chartierde40d472015-10-15 17:47:48 -07003045 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003046 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003047 descriptor = abs_method->GetReturnTypeDescriptor();
Ian Rogersad0b3a32012-04-16 14:50:24 -07003048 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00003049 const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003050 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003051 work_line_->SetResultRegisterType(this, return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003052 } else {
3053 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
3054 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003055 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07003056 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07003057 }
Narayan Kamath9823e782016-08-03 12:46:58 +01003058 case Instruction::INVOKE_POLYMORPHIC:
3059 case Instruction::INVOKE_POLYMORPHIC_RANGE: {
3060 Fail(VERIFY_ERROR_FORCE_INTERPRETER)
3061 << "instruction is not supported by verifier; skipping verification";
3062 have_pending_experimental_failure_ = true;
3063 return false;
3064 }
jeffhaobdb76512011-09-07 11:43:16 -07003065 case Instruction::NEG_INT:
3066 case Instruction::NOT_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003067 work_line_->CheckUnaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003068 break;
3069 case Instruction::NEG_LONG:
3070 case Instruction::NOT_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07003071 work_line_->CheckUnaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003072 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07003073 break;
3074 case Instruction::NEG_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003075 work_line_->CheckUnaryOp(this, inst, reg_types_.Float(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07003076 break;
3077 case Instruction::NEG_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07003078 work_line_->CheckUnaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003079 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07003080 break;
3081 case Instruction::INT_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07003082 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003083 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003084 break;
3085 case Instruction::INT_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003086 work_line_->CheckUnaryOp(this, inst, reg_types_.Float(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003087 break;
3088 case Instruction::INT_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07003089 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003090 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003091 break;
3092 case Instruction::LONG_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003093 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003094 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07003095 break;
3096 case Instruction::LONG_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003097 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003098 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07003099 break;
3100 case Instruction::LONG_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07003101 work_line_->CheckUnaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003102 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07003103 break;
3104 case Instruction::FLOAT_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003105 work_line_->CheckUnaryOp(this, inst, reg_types_.Integer(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07003106 break;
3107 case Instruction::FLOAT_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07003108 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003109 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07003110 break;
3111 case Instruction::FLOAT_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07003112 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003113 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07003114 break;
3115 case Instruction::DOUBLE_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003116 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003117 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07003118 break;
3119 case Instruction::DOUBLE_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07003120 work_line_->CheckUnaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
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::DOUBLE_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003124 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003125 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07003126 break;
3127 case Instruction::INT_TO_BYTE:
Ian Rogers7b078e82014-09-10 14:44:24 -07003128 work_line_->CheckUnaryOp(this, inst, reg_types_.Byte(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003129 break;
3130 case Instruction::INT_TO_CHAR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003131 work_line_->CheckUnaryOp(this, inst, reg_types_.Char(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003132 break;
3133 case Instruction::INT_TO_SHORT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003134 work_line_->CheckUnaryOp(this, inst, reg_types_.Short(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003135 break;
3136
3137 case Instruction::ADD_INT:
3138 case Instruction::SUB_INT:
3139 case Instruction::MUL_INT:
3140 case Instruction::REM_INT:
3141 case Instruction::DIV_INT:
3142 case Instruction::SHL_INT:
3143 case Instruction::SHR_INT:
3144 case Instruction::USHR_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003145 work_line_->CheckBinaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003146 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07003147 break;
3148 case Instruction::AND_INT:
3149 case Instruction::OR_INT:
3150 case Instruction::XOR_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003151 work_line_->CheckBinaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003152 reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07003153 break;
3154 case Instruction::ADD_LONG:
3155 case Instruction::SUB_LONG:
3156 case Instruction::MUL_LONG:
3157 case Instruction::DIV_LONG:
3158 case Instruction::REM_LONG:
3159 case Instruction::AND_LONG:
3160 case Instruction::OR_LONG:
3161 case Instruction::XOR_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07003162 work_line_->CheckBinaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003163 reg_types_.LongLo(), reg_types_.LongHi(),
3164 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07003165 break;
3166 case Instruction::SHL_LONG:
3167 case Instruction::SHR_LONG:
3168 case Instruction::USHR_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07003169 /* shift distance is Int, making these different from other binary operations */
Ian Rogers7b078e82014-09-10 14:44:24 -07003170 work_line_->CheckBinaryOpWideShift(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003171 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003172 break;
3173 case Instruction::ADD_FLOAT:
3174 case Instruction::SUB_FLOAT:
3175 case Instruction::MUL_FLOAT:
3176 case Instruction::DIV_FLOAT:
3177 case Instruction::REM_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003178 work_line_->CheckBinaryOp(this, inst, reg_types_.Float(), reg_types_.Float(),
3179 reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07003180 break;
3181 case Instruction::ADD_DOUBLE:
3182 case Instruction::SUB_DOUBLE:
3183 case Instruction::MUL_DOUBLE:
3184 case Instruction::DIV_DOUBLE:
3185 case Instruction::REM_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07003186 work_line_->CheckBinaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003187 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
3188 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07003189 break;
3190 case Instruction::ADD_INT_2ADDR:
3191 case Instruction::SUB_INT_2ADDR:
3192 case Instruction::MUL_INT_2ADDR:
3193 case Instruction::REM_INT_2ADDR:
3194 case Instruction::SHL_INT_2ADDR:
3195 case Instruction::SHR_INT_2ADDR:
3196 case Instruction::USHR_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003197 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
3198 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07003199 break;
3200 case Instruction::AND_INT_2ADDR:
3201 case Instruction::OR_INT_2ADDR:
3202 case Instruction::XOR_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003203 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
3204 reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07003205 break;
3206 case Instruction::DIV_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003207 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
3208 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07003209 break;
3210 case Instruction::ADD_LONG_2ADDR:
3211 case Instruction::SUB_LONG_2ADDR:
3212 case Instruction::MUL_LONG_2ADDR:
3213 case Instruction::DIV_LONG_2ADDR:
3214 case Instruction::REM_LONG_2ADDR:
3215 case Instruction::AND_LONG_2ADDR:
3216 case Instruction::OR_LONG_2ADDR:
3217 case Instruction::XOR_LONG_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003218 work_line_->CheckBinaryOp2addrWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003219 reg_types_.LongLo(), reg_types_.LongHi(),
3220 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07003221 break;
3222 case Instruction::SHL_LONG_2ADDR:
3223 case Instruction::SHR_LONG_2ADDR:
3224 case Instruction::USHR_LONG_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003225 work_line_->CheckBinaryOp2addrWideShift(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003226 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003227 break;
3228 case Instruction::ADD_FLOAT_2ADDR:
3229 case Instruction::SUB_FLOAT_2ADDR:
3230 case Instruction::MUL_FLOAT_2ADDR:
3231 case Instruction::DIV_FLOAT_2ADDR:
3232 case Instruction::REM_FLOAT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003233 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Float(), reg_types_.Float(),
3234 reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07003235 break;
3236 case Instruction::ADD_DOUBLE_2ADDR:
3237 case Instruction::SUB_DOUBLE_2ADDR:
3238 case Instruction::MUL_DOUBLE_2ADDR:
3239 case Instruction::DIV_DOUBLE_2ADDR:
3240 case Instruction::REM_DOUBLE_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003241 work_line_->CheckBinaryOp2addrWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003242 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
3243 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07003244 break;
3245 case Instruction::ADD_INT_LIT16:
Ian Rogersf72a11d2014-10-30 15:41:08 -07003246 case Instruction::RSUB_INT_LIT16:
jeffhaobdb76512011-09-07 11:43:16 -07003247 case Instruction::MUL_INT_LIT16:
3248 case Instruction::DIV_INT_LIT16:
3249 case Instruction::REM_INT_LIT16:
Ian Rogers7b078e82014-09-10 14:44:24 -07003250 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), false,
3251 true);
jeffhaobdb76512011-09-07 11:43:16 -07003252 break;
3253 case Instruction::AND_INT_LIT16:
3254 case Instruction::OR_INT_LIT16:
3255 case Instruction::XOR_INT_LIT16:
Ian Rogers7b078e82014-09-10 14:44:24 -07003256 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), true,
3257 true);
jeffhaobdb76512011-09-07 11:43:16 -07003258 break;
3259 case Instruction::ADD_INT_LIT8:
3260 case Instruction::RSUB_INT_LIT8:
3261 case Instruction::MUL_INT_LIT8:
3262 case Instruction::DIV_INT_LIT8:
3263 case Instruction::REM_INT_LIT8:
3264 case Instruction::SHL_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07003265 case Instruction::SHR_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07003266 case Instruction::USHR_INT_LIT8:
Ian Rogers7b078e82014-09-10 14:44:24 -07003267 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), false,
3268 false);
jeffhaobdb76512011-09-07 11:43:16 -07003269 break;
3270 case Instruction::AND_INT_LIT8:
3271 case Instruction::OR_INT_LIT8:
3272 case Instruction::XOR_INT_LIT8:
Ian Rogers7b078e82014-09-10 14:44:24 -07003273 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), true,
3274 false);
jeffhaobdb76512011-09-07 11:43:16 -07003275 break;
3276
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003277 // Special instructions.
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07003278 case Instruction::RETURN_VOID_NO_BARRIER:
3279 if (IsConstructor() && !IsStatic()) {
3280 auto& declaring_class = GetDeclaringClass();
Andreas Gampe68df3202015-06-22 11:35:46 -07003281 if (declaring_class.IsUnresolvedReference()) {
3282 // We must iterate over the fields, even if we cannot use mirror classes to do so. Do it
3283 // manually over the underlying dex file.
3284 uint32_t first_index = GetFirstFinalInstanceFieldIndex(*dex_file_,
3285 dex_file_->GetMethodId(dex_method_idx_).class_idx_);
3286 if (first_index != DexFile::kDexNoIndex) {
3287 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void-no-barrier not expected for field "
3288 << first_index;
3289 }
3290 break;
3291 }
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07003292 auto* klass = declaring_class.GetClass();
3293 for (uint32_t i = 0, num_fields = klass->NumInstanceFields(); i < num_fields; ++i) {
3294 if (klass->GetInstanceField(i)->IsFinal()) {
Mathieu Chartiere86deef2015-03-19 13:43:37 -07003295 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void-no-barrier not expected for "
David Sehr709b0702016-10-13 09:12:37 -07003296 << klass->GetInstanceField(i)->PrettyField();
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07003297 break;
3298 }
3299 }
Sebastien Hertzcc10e0e2013-06-28 14:24:48 +02003300 }
Andreas Gampeb29179612015-07-31 13:36:10 -07003301 // Handle this like a RETURN_VOID now. Code is duplicated to separate standard from
3302 // quickened opcodes (otherwise this could be a fall-through).
3303 if (!IsConstructor()) {
3304 if (!GetMethodReturnType().IsConflict()) {
3305 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void not expected";
3306 }
3307 }
Sebastien Hertzcc10e0e2013-06-28 14:24:48 +02003308 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003309 // Note: the following instructions encode offsets derived from class linking.
Neil Fuller0e844392016-09-08 13:43:31 +01003310 // As such they use Class*/Field*/Executable* as these offsets only have
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003311 // meaning if the class linking and resolution were successful.
3312 case Instruction::IGET_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003313 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003314 break;
3315 case Instruction::IGET_WIDE_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003316 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003317 break;
3318 case Instruction::IGET_OBJECT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003319 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003320 break;
Mathieu Chartierffc605c2014-12-10 10:35:44 -08003321 case Instruction::IGET_BOOLEAN_QUICK:
3322 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true);
3323 break;
3324 case Instruction::IGET_BYTE_QUICK:
3325 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true);
3326 break;
3327 case Instruction::IGET_CHAR_QUICK:
3328 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true);
3329 break;
3330 case Instruction::IGET_SHORT_QUICK:
3331 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true);
3332 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003333 case Instruction::IPUT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003334 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003335 break;
Fred Shih37f05ef2014-07-16 18:38:08 -07003336 case Instruction::IPUT_BOOLEAN_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003337 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07003338 break;
3339 case Instruction::IPUT_BYTE_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003340 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07003341 break;
3342 case Instruction::IPUT_CHAR_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003343 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07003344 break;
3345 case Instruction::IPUT_SHORT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003346 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07003347 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003348 case Instruction::IPUT_WIDE_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003349 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003350 break;
3351 case Instruction::IPUT_OBJECT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003352 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003353 break;
3354 case Instruction::INVOKE_VIRTUAL_QUICK:
3355 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: {
3356 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
Mathieu Chartiere401d142015-04-22 13:56:20 -07003357 ArtMethod* called_method = VerifyInvokeVirtualQuickArgs(inst, is_range);
Ian Rogers7b078e82014-09-10 14:44:24 -07003358 if (called_method != nullptr) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003359 const char* descriptor = called_method->GetReturnTypeDescriptor();
Ian Rogersd8f69b02014-09-10 21:43:52 +00003360 const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003361 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003362 work_line_->SetResultRegisterType(this, return_type);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003363 } else {
3364 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
3365 }
3366 just_set_result = true;
3367 }
3368 break;
3369 }
3370
Ian Rogersd81871c2011-10-03 13:57:23 -07003371 /* These should never appear during verification. */
Mathieu Chartierffc605c2014-12-10 10:35:44 -08003372 case Instruction::UNUSED_3E ... Instruction::UNUSED_43:
Narayan Kamath8ec3bd22016-08-03 12:46:23 +01003373 case Instruction::UNUSED_F3 ... Instruction::UNUSED_F9:
3374 case Instruction::UNUSED_FC ... Instruction::UNUSED_FF:
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003375 case Instruction::UNUSED_79:
3376 case Instruction::UNUSED_7A:
jeffhaod5347e02012-03-22 17:25:05 -07003377 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Unexpected opcode " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07003378 break;
3379
3380 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003381 * DO NOT add a "default" clause here. Without it the compiler will
jeffhaobdb76512011-09-07 11:43:16 -07003382 * complain if an instruction is missing (which is desirable).
3383 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003384 } // end - switch (dec_insn.opcode)
jeffhaobdb76512011-09-07 11:43:16 -07003385
Ian Rogersad0b3a32012-04-16 14:50:24 -07003386 if (have_pending_hard_failure_) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08003387 if (Runtime::Current()->IsAotCompiler()) {
3388 /* When AOT compiling, check that the last failure is a hard failure */
Andreas Gampeb588f4c2015-05-26 13:35:39 -07003389 if (failures_[failures_.size() - 1] != VERIFY_ERROR_BAD_CLASS_HARD) {
3390 LOG(ERROR) << "Pending failures:";
3391 for (auto& error : failures_) {
3392 LOG(ERROR) << error;
3393 }
3394 for (auto& error_msg : failure_messages_) {
3395 LOG(ERROR) << error_msg->str();
3396 }
3397 LOG(FATAL) << "Pending hard failure, but last failure not hard.";
3398 }
Ian Rogerse1758fe2012-04-19 11:31:15 -07003399 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003400 /* immediate failure, reject class */
3401 info_messages_ << "Rejecting opcode " << inst->DumpString(dex_file_);
3402 return false;
jeffhaofaf459e2012-08-31 15:32:47 -07003403 } else if (have_pending_runtime_throw_failure_) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07003404 /* checking interpreter will throw, mark following code as unreachable */
jeffhaofaf459e2012-08-31 15:32:47 -07003405 opcode_flags = Instruction::kThrow;
Andreas Gampea727e372015-08-25 09:22:37 -07003406 // Note: the flag must be reset as it is only global to decouple Fail and is semantically per
3407 // instruction. However, RETURN checking may throw LOCKING errors, so we clear at the
3408 // very end.
jeffhaobdb76512011-09-07 11:43:16 -07003409 }
jeffhaobdb76512011-09-07 11:43:16 -07003410 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003411 * If we didn't just set the result register, clear it out. This ensures that you can only use
3412 * "move-result" immediately after the result is set. (We could check this statically, but it's
3413 * not expensive and it makes our debugging output cleaner.)
jeffhaobdb76512011-09-07 11:43:16 -07003414 */
3415 if (!just_set_result) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003416 work_line_->SetResultTypeToUnknown(this);
jeffhaobdb76512011-09-07 11:43:16 -07003417 }
3418
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003419
jeffhaobdb76512011-09-07 11:43:16 -07003420
3421 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003422 * Handle "branch". Tag the branch target.
jeffhaobdb76512011-09-07 11:43:16 -07003423 *
3424 * NOTE: instructions like Instruction::EQZ provide information about the
jeffhaod1f0fde2011-09-08 17:25:33 -07003425 * state of the register when the branch is taken or not taken. For example,
jeffhaobdb76512011-09-07 11:43:16 -07003426 * somebody could get a reference field, check it for zero, and if the
3427 * branch is taken immediately store that register in a boolean field
jeffhaod1f0fde2011-09-08 17:25:33 -07003428 * since the value is known to be zero. We do not currently account for
jeffhaobdb76512011-09-07 11:43:16 -07003429 * that, and will reject the code.
3430 *
3431 * TODO: avoid re-fetching the branch target
3432 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003433 if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07003434 bool isConditional, selfOkay;
Ian Rogersd81871c2011-10-03 13:57:23 -07003435 if (!GetBranchOffset(work_insn_idx_, &branch_target, &isConditional, &selfOkay)) {
jeffhaobdb76512011-09-07 11:43:16 -07003436 /* should never happen after static verification */
jeffhaod5347e02012-03-22 17:25:05 -07003437 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad branch";
jeffhaobdb76512011-09-07 11:43:16 -07003438 return false;
3439 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08003440 DCHECK_EQ(isConditional, (opcode_flags & Instruction::kContinue) != 0);
Stephen Kyle9bc61992014-09-22 13:53:15 +01003441 if (!CheckNotMoveExceptionOrMoveResult(code_item_->insns_, work_insn_idx_ + branch_target)) {
jeffhaobdb76512011-09-07 11:43:16 -07003442 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07003443 }
jeffhaobdb76512011-09-07 11:43:16 -07003444 /* update branch target, set "changed" if appropriate */
Mathieu Chartierde40d472015-10-15 17:47:48 -07003445 if (nullptr != branch_line) {
Ian Rogersebbdd872014-07-07 23:53:08 -07003446 if (!UpdateRegisters(work_insn_idx_ + branch_target, branch_line.get(), false)) {
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003447 return false;
3448 }
3449 } else {
Ian Rogersebbdd872014-07-07 23:53:08 -07003450 if (!UpdateRegisters(work_insn_idx_ + branch_target, work_line_.get(), false)) {
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003451 return false;
3452 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003453 }
jeffhaobdb76512011-09-07 11:43:16 -07003454 }
3455
3456 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003457 * Handle "switch". Tag all possible branch targets.
jeffhaobdb76512011-09-07 11:43:16 -07003458 *
3459 * We've already verified that the table is structurally sound, so we
3460 * just need to walk through and tag the targets.
3461 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003462 if ((opcode_flags & Instruction::kSwitch) != 0) {
Andreas Gampe53de99c2015-08-17 13:43:55 -07003463 int offset_to_switch = insns[1] | (static_cast<int32_t>(insns[2]) << 16);
jeffhaobdb76512011-09-07 11:43:16 -07003464 const uint16_t* switch_insns = insns + offset_to_switch;
3465 int switch_count = switch_insns[1];
3466 int offset_to_targets, targ;
3467
3468 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
3469 /* 0 = sig, 1 = count, 2/3 = first key */
3470 offset_to_targets = 4;
3471 } else {
3472 /* 0 = sig, 1 = count, 2..count * 2 = keys */
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07003473 DCHECK((*insns & 0xff) == Instruction::SPARSE_SWITCH);
jeffhaobdb76512011-09-07 11:43:16 -07003474 offset_to_targets = 2 + 2 * switch_count;
3475 }
3476
3477 /* verify each switch target */
3478 for (targ = 0; targ < switch_count; targ++) {
3479 int offset;
3480 uint32_t abs_offset;
3481
3482 /* offsets are 32-bit, and only partly endian-swapped */
3483 offset = switch_insns[offset_to_targets + targ * 2] |
Andreas Gampe53de99c2015-08-17 13:43:55 -07003484 (static_cast<int32_t>(switch_insns[offset_to_targets + targ * 2 + 1]) << 16);
Ian Rogersd81871c2011-10-03 13:57:23 -07003485 abs_offset = work_insn_idx_ + offset;
3486 DCHECK_LT(abs_offset, code_item_->insns_size_in_code_units_);
Stephen Kyle9bc61992014-09-22 13:53:15 +01003487 if (!CheckNotMoveExceptionOrMoveResult(code_item_->insns_, abs_offset)) {
jeffhaobdb76512011-09-07 11:43:16 -07003488 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07003489 }
Ian Rogersebbdd872014-07-07 23:53:08 -07003490 if (!UpdateRegisters(abs_offset, work_line_.get(), false)) {
jeffhaobdb76512011-09-07 11:43:16 -07003491 return false;
Ian Rogersebbdd872014-07-07 23:53:08 -07003492 }
jeffhaobdb76512011-09-07 11:43:16 -07003493 }
3494 }
3495
3496 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003497 * Handle instructions that can throw and that are sitting in a "try" block. (If they're not in a
3498 * "try" block when they throw, control transfers out of the method.)
jeffhaobdb76512011-09-07 11:43:16 -07003499 */
Mathieu Chartierde40d472015-10-15 17:47:48 -07003500 if ((opcode_flags & Instruction::kThrow) != 0 && GetInstructionFlags(work_insn_idx_).IsInTry()) {
Andreas Gampef91baf12014-07-18 15:41:00 -07003501 bool has_catch_all_handler = false;
Ian Rogers0571d352011-11-03 19:51:38 -07003502 CatchHandlerIterator iterator(*code_item_, work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07003503
Andreas Gampef91baf12014-07-18 15:41:00 -07003504 // Need the linker to try and resolve the handled class to check if it's Throwable.
3505 ClassLinker* linker = Runtime::Current()->GetClassLinker();
3506
Ian Rogers0571d352011-11-03 19:51:38 -07003507 for (; iterator.HasNext(); iterator.Next()) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08003508 dex::TypeIndex handler_type_idx = iterator.GetHandlerTypeIndex();
3509 if (!handler_type_idx.IsValid()) {
Andreas Gampef91baf12014-07-18 15:41:00 -07003510 has_catch_all_handler = true;
3511 } else {
3512 // It is also a catch-all if it is java.lang.Throwable.
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003513 mirror::Class* klass = linker->ResolveType(*dex_file_, handler_type_idx, dex_cache_,
3514 class_loader_);
Andreas Gampef91baf12014-07-18 15:41:00 -07003515 if (klass != nullptr) {
3516 if (klass == mirror::Throwable::GetJavaLangThrowable()) {
3517 has_catch_all_handler = true;
3518 }
3519 } else {
3520 // Clear exception.
Ian Rogers7b078e82014-09-10 14:44:24 -07003521 DCHECK(self_->IsExceptionPending());
3522 self_->ClearException();
Andreas Gampef91baf12014-07-18 15:41:00 -07003523 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003524 }
jeffhaobdb76512011-09-07 11:43:16 -07003525 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003526 * Merge registers into the "catch" block. We want to use the "savedRegs" rather than
3527 * "work_regs", because at runtime the exception will be thrown before the instruction
3528 * modifies any registers.
jeffhaobdb76512011-09-07 11:43:16 -07003529 */
Ian Rogersebbdd872014-07-07 23:53:08 -07003530 if (!UpdateRegisters(iterator.GetHandlerAddress(), saved_line_.get(), false)) {
jeffhaobdb76512011-09-07 11:43:16 -07003531 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07003532 }
jeffhaobdb76512011-09-07 11:43:16 -07003533 }
3534
3535 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003536 * If the monitor stack depth is nonzero, there must be a "catch all" handler for this
3537 * instruction. This does apply to monitor-exit because of async exception handling.
jeffhaobdb76512011-09-07 11:43:16 -07003538 */
Andreas Gampef91baf12014-07-18 15:41:00 -07003539 if (work_line_->MonitorStackDepth() > 0 && !has_catch_all_handler) {
jeffhaobdb76512011-09-07 11:43:16 -07003540 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003541 * The state in work_line reflects the post-execution state. If the current instruction is a
3542 * monitor-enter and the monitor stack was empty, we don't need a catch-all (if it throws,
jeffhaobdb76512011-09-07 11:43:16 -07003543 * it will do so before grabbing the lock).
3544 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02003545 if (inst->Opcode() != Instruction::MONITOR_ENTER || work_line_->MonitorStackDepth() != 1) {
jeffhaod5347e02012-03-22 17:25:05 -07003546 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
Ian Rogersd81871c2011-10-03 13:57:23 -07003547 << "expected to be within a catch-all for an instruction where a monitor is held";
jeffhaobdb76512011-09-07 11:43:16 -07003548 return false;
3549 }
3550 }
3551 }
3552
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003553 /* Handle "continue". Tag the next consecutive instruction.
3554 * Note: Keep the code handling "continue" case below the "branch" and "switch" cases,
3555 * because it changes work_line_ when performing peephole optimization
3556 * and this change should not be used in those cases.
3557 */
Ian Rogers6d376ae2013-07-23 15:12:40 -07003558 if ((opcode_flags & Instruction::kContinue) != 0) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003559 DCHECK_EQ(Instruction::At(code_item_->insns_ + work_insn_idx_), inst);
3560 uint32_t next_insn_idx = work_insn_idx_ + inst->SizeInCodeUnits();
Ian Rogers6d376ae2013-07-23 15:12:40 -07003561 if (next_insn_idx >= code_item_->insns_size_in_code_units_) {
3562 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Execution can walk off end of code area";
3563 return false;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003564 }
Ian Rogers6d376ae2013-07-23 15:12:40 -07003565 // The only way to get to a move-exception instruction is to get thrown there. Make sure the
3566 // next instruction isn't one.
3567 if (!CheckNotMoveException(code_item_->insns_, next_insn_idx)) {
3568 return false;
3569 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07003570 if (nullptr != fallthrough_line) {
Ian Rogers6d376ae2013-07-23 15:12:40 -07003571 // Make workline consistent with fallthrough computed from peephole optimization.
3572 work_line_->CopyFromLine(fallthrough_line.get());
3573 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07003574 if (GetInstructionFlags(next_insn_idx).IsReturn()) {
Ian Rogersb8c78592013-07-25 23:52:52 +00003575 // For returns we only care about the operand to the return, all other registers are dead.
3576 const Instruction* ret_inst = Instruction::At(code_item_->insns_ + next_insn_idx);
Andreas Gampea727e372015-08-25 09:22:37 -07003577 AdjustReturnLine(this, ret_inst, work_line_.get());
Ian Rogersb8c78592013-07-25 23:52:52 +00003578 }
Ian Rogers6d376ae2013-07-23 15:12:40 -07003579 RegisterLine* next_line = reg_table_.GetLine(next_insn_idx);
Ian Rogers7b078e82014-09-10 14:44:24 -07003580 if (next_line != nullptr) {
Ian Rogersebbdd872014-07-07 23:53:08 -07003581 // Merge registers into what we have for the next instruction, and set the "changed" flag if
3582 // needed. If the merge changes the state of the registers then the work line will be
3583 // updated.
3584 if (!UpdateRegisters(next_insn_idx, work_line_.get(), true)) {
Ian Rogers6d376ae2013-07-23 15:12:40 -07003585 return false;
3586 }
3587 } else {
3588 /*
3589 * We're not recording register data for the next instruction, so we don't know what the
3590 * prior state was. We have to assume that something has changed and re-evaluate it.
3591 */
Mathieu Chartierde40d472015-10-15 17:47:48 -07003592 GetInstructionFlags(next_insn_idx).SetChanged();
Ian Rogers6d376ae2013-07-23 15:12:40 -07003593 }
3594 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003595
jeffhaod1f0fde2011-09-08 17:25:33 -07003596 /* If we're returning from the method, make sure monitor stack is empty. */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003597 if ((opcode_flags & Instruction::kReturn) != 0) {
Andreas Gampea727e372015-08-25 09:22:37 -07003598 work_line_->VerifyMonitorStackEmpty(this);
jeffhaobdb76512011-09-07 11:43:16 -07003599 }
3600
3601 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003602 * Update start_guess. Advance to the next instruction of that's
3603 * possible, otherwise use the branch target if one was found. If
jeffhaobdb76512011-09-07 11:43:16 -07003604 * neither of those exists we're in a return or throw; leave start_guess
3605 * alone and let the caller sort it out.
3606 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003607 if ((opcode_flags & Instruction::kContinue) != 0) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003608 DCHECK_EQ(Instruction::At(code_item_->insns_ + work_insn_idx_), inst);
3609 *start_guess = work_insn_idx_ + inst->SizeInCodeUnits();
Elliott Hughesadb8c672012-03-06 16:49:32 -08003610 } else if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07003611 /* we're still okay if branch_target is zero */
Ian Rogersd81871c2011-10-03 13:57:23 -07003612 *start_guess = work_insn_idx_ + branch_target;
jeffhaobdb76512011-09-07 11:43:16 -07003613 }
3614
Ian Rogersd81871c2011-10-03 13:57:23 -07003615 DCHECK_LT(*start_guess, code_item_->insns_size_in_code_units_);
Mathieu Chartierde40d472015-10-15 17:47:48 -07003616 DCHECK(GetInstructionFlags(*start_guess).IsOpcode());
jeffhaobdb76512011-09-07 11:43:16 -07003617
Andreas Gampea727e372015-08-25 09:22:37 -07003618 if (have_pending_runtime_throw_failure_) {
3619 have_any_pending_runtime_throw_failure_ = true;
3620 // Reset the pending_runtime_throw flag now.
3621 have_pending_runtime_throw_failure_ = false;
3622 }
3623
jeffhaobdb76512011-09-07 11:43:16 -07003624 return true;
Brian Carlstrom7934ac22013-07-26 10:54:15 -07003625} // NOLINT(readability/fn_size)
jeffhaobdb76512011-09-07 11:43:16 -07003626
Mathieu Chartierde40d472015-10-15 17:47:48 -07003627void MethodVerifier::UninstantiableError(const char* descriptor) {
3628 Fail(VerifyError::VERIFY_ERROR_NO_CLASS) << "Could not create precise reference for "
3629 << "non-instantiable klass " << descriptor;
3630}
3631
3632inline bool MethodVerifier::IsInstantiableOrPrimitive(mirror::Class* klass) {
3633 return klass->IsInstantiable() || klass->IsPrimitive();
3634}
3635
Andreas Gampea5b09a62016-11-17 15:21:22 -08003636const RegType& MethodVerifier::ResolveClassAndCheckAccess(dex::TypeIndex class_idx) {
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003637 mirror::Class* klass = dex_cache_->GetResolvedType(class_idx);
Mathieu Chartierde40d472015-10-15 17:47:48 -07003638 const RegType* result = nullptr;
3639 if (klass != nullptr) {
3640 bool precise = klass->CannotBeAssignedFromOtherTypes();
3641 if (precise && !IsInstantiableOrPrimitive(klass)) {
3642 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
3643 UninstantiableError(descriptor);
3644 precise = false;
3645 }
3646 result = reg_types_.FindClass(klass, precise);
3647 if (result == nullptr) {
3648 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
3649 result = reg_types_.InsertClass(descriptor, klass, precise);
3650 }
3651 } else {
3652 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
3653 result = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003654 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07003655 DCHECK(result != nullptr);
3656 if (result->IsConflict()) {
3657 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
3658 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "accessing broken descriptor '" << descriptor
3659 << "' in " << GetDeclaringClass();
3660 return *result;
3661 }
3662 if (klass == nullptr && !result->IsUnresolvedTypes()) {
David Brazdilca3c8c32016-09-06 14:04:48 +01003663 klass = result->GetClass();
3664 dex_cache_->SetResolvedType(class_idx, klass);
Ian Rogerse1758fe2012-04-19 11:31:15 -07003665 }
David Brazdilca3c8c32016-09-06 14:04:48 +01003666
3667 // Record result of class resolution attempt.
3668 VerifierDeps::MaybeRecordClassResolution(*dex_file_, class_idx, klass);
3669
Ian Rogersad0b3a32012-04-16 14:50:24 -07003670 // Check if access is allowed. Unresolved types use xxxWithAccessCheck to
Jeff Haob24b4a72013-07-31 13:47:31 -07003671 // check at runtime if access is allowed and so pass here. If result is
3672 // primitive, skip the access check.
Mathieu Chartierde40d472015-10-15 17:47:48 -07003673 if (result->IsNonZeroReferenceTypes() && !result->IsUnresolvedTypes()) {
3674 const RegType& referrer = GetDeclaringClass();
3675 if (!referrer.IsUnresolvedTypes() && !referrer.CanAccess(*result)) {
3676 Fail(VERIFY_ERROR_ACCESS_CLASS) << "illegal class access: '"
3677 << referrer << "' -> '" << result << "'";
3678 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07003679 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07003680 return *result;
Ian Rogersd81871c2011-10-03 13:57:23 -07003681}
3682
Ian Rogersd8f69b02014-09-10 21:43:52 +00003683const RegType& MethodVerifier::GetCaughtExceptionType() {
Ian Rogers7b078e82014-09-10 14:44:24 -07003684 const RegType* common_super = nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003685 if (code_item_->tries_size_ != 0) {
Ian Rogers13735952014-10-08 12:43:28 -07003686 const uint8_t* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
Ian Rogersd81871c2011-10-03 13:57:23 -07003687 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
3688 for (uint32_t i = 0; i < handlers_size; i++) {
Ian Rogers0571d352011-11-03 19:51:38 -07003689 CatchHandlerIterator iterator(handlers_ptr);
3690 for (; iterator.HasNext(); iterator.Next()) {
3691 if (iterator.GetHandlerAddress() == (uint32_t) work_insn_idx_) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08003692 if (!iterator.GetHandlerTypeIndex().IsValid()) {
Ian Rogersb4903572012-10-11 11:52:56 -07003693 common_super = &reg_types_.JavaLangThrowable(false);
Ian Rogersd81871c2011-10-03 13:57:23 -07003694 } else {
Ian Rogersd8f69b02014-09-10 21:43:52 +00003695 const RegType& exception = ResolveClassAndCheckAccess(iterator.GetHandlerTypeIndex());
David Brazdilca3c8c32016-09-06 14:04:48 +01003696 if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(exception, this)) {
David Brazdil68b5c0b2016-01-19 14:25:29 +00003697 DCHECK(!exception.IsUninitializedTypes()); // Comes from dex, shouldn't be uninit.
Jeff Haoc26a56c2013-11-04 12:00:47 -08003698 if (exception.IsUnresolvedTypes()) {
3699 // We don't know enough about the type. Fail here and let runtime handle it.
3700 Fail(VERIFY_ERROR_NO_CLASS) << "unresolved exception class " << exception;
3701 return exception;
3702 } else {
3703 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unexpected non-exception class " << exception;
3704 return reg_types_.Conflict();
3705 }
Jeff Haob878f212014-04-24 16:25:36 -07003706 } else if (common_super == nullptr) {
3707 common_super = &exception;
Ian Rogers28ad40d2011-10-27 15:19:26 -07003708 } else if (common_super->Equals(exception)) {
Ian Rogersc4762272012-02-01 15:55:55 -08003709 // odd case, but nothing to do
Ian Rogersd81871c2011-10-03 13:57:23 -07003710 } else {
David Brazdilca3c8c32016-09-06 14:04:48 +01003711 common_super = &common_super->Merge(exception, &reg_types_, this);
Andreas Gampe7c038102014-10-27 20:08:46 -07003712 if (FailOrAbort(this,
David Brazdilca3c8c32016-09-06 14:04:48 +01003713 reg_types_.JavaLangThrowable(false).IsAssignableFrom(
3714 *common_super, this),
Andreas Gampe7c038102014-10-27 20:08:46 -07003715 "java.lang.Throwable is not assignable-from common_super at ",
3716 work_insn_idx_)) {
3717 break;
3718 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003719 }
3720 }
3721 }
3722 }
Ian Rogers0571d352011-11-03 19:51:38 -07003723 handlers_ptr = iterator.EndDataPointer();
Ian Rogersd81871c2011-10-03 13:57:23 -07003724 }
3725 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003726 if (common_super == nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003727 /* no catch blocks, or no catches with classes we can find */
jeffhaod5347e02012-03-22 17:25:05 -07003728 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unable to find exception handler";
Ian Rogersad0b3a32012-04-16 14:50:24 -07003729 return reg_types_.Conflict();
Ian Rogersd81871c2011-10-03 13:57:23 -07003730 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07003731 return *common_super;
Ian Rogersd81871c2011-10-03 13:57:23 -07003732}
3733
David Brazdilca3c8c32016-09-06 14:04:48 +01003734inline static MethodResolutionKind GetMethodResolutionKind(
3735 MethodType method_type, bool is_interface) {
3736 if (method_type == METHOD_DIRECT || method_type == METHOD_STATIC) {
3737 return kDirectMethodResolution;
3738 } else if (method_type == METHOD_INTERFACE) {
3739 return kInterfaceMethodResolution;
3740 } else if (method_type == METHOD_SUPER && is_interface) {
3741 return kInterfaceMethodResolution;
3742 } else {
3743 DCHECK(method_type == METHOD_VIRTUAL || method_type == METHOD_SUPER);
3744 return kVirtualMethodResolution;
3745 }
3746}
3747
Mathieu Chartiere401d142015-04-22 13:56:20 -07003748ArtMethod* MethodVerifier::ResolveMethodAndCheckAccess(
Alex Light7268d472016-01-20 15:50:01 -08003749 uint32_t dex_method_idx, MethodType method_type) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003750 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx);
Ian Rogersd8f69b02014-09-10 21:43:52 +00003751 const RegType& klass_type = ResolveClassAndCheckAccess(method_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003752 if (klass_type.IsConflict()) {
3753 std::string append(" in attempt to access method ");
3754 append += dex_file_->GetMethodName(method_id);
3755 AppendToLastFailMessage(append);
Ian Rogers7b078e82014-09-10 14:44:24 -07003756 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08003757 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003758 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003759 return nullptr; // Can't resolve Class so no more to do here
Ian Rogers90040192011-12-16 08:54:29 -08003760 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003761 mirror::Class* klass = klass_type.GetClass();
Ian Rogersd8f69b02014-09-10 21:43:52 +00003762 const RegType& referrer = GetDeclaringClass();
Mathieu Chartiere401d142015-04-22 13:56:20 -07003763 auto* cl = Runtime::Current()->GetClassLinker();
3764 auto pointer_size = cl->GetImagePointerSize();
David Brazdilca3c8c32016-09-06 14:04:48 +01003765 MethodResolutionKind res_kind = GetMethodResolutionKind(method_type, klass->IsInterface());
Andreas Gampe42ef8ab2015-12-03 17:27:32 -08003766
Mathieu Chartiere401d142015-04-22 13:56:20 -07003767 ArtMethod* res_method = dex_cache_->GetResolvedMethod(dex_method_idx, pointer_size);
Andreas Gampe42ef8ab2015-12-03 17:27:32 -08003768 bool stash_method = false;
Ian Rogers7b078e82014-09-10 14:44:24 -07003769 if (res_method == nullptr) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07003770 const char* name = dex_file_->GetMethodName(method_id);
Ian Rogersd91d6d62013-09-25 20:26:14 -07003771 const Signature signature = dex_file_->GetMethodSignature(method_id);
jeffhao8cd6dda2012-02-22 10:15:34 -08003772
David Brazdilca3c8c32016-09-06 14:04:48 +01003773 if (res_kind == kDirectMethodResolution) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003774 res_method = klass->FindDirectMethod(name, signature, pointer_size);
David Brazdilca3c8c32016-09-06 14:04:48 +01003775 } else if (res_kind == kVirtualMethodResolution) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003776 res_method = klass->FindVirtualMethod(name, signature, pointer_size);
David Brazdilca3c8c32016-09-06 14:04:48 +01003777 } else {
3778 DCHECK_EQ(res_kind, kInterfaceMethodResolution);
3779 res_method = klass->FindInterfaceMethod(name, signature, pointer_size);
Ian Rogersd81871c2011-10-03 13:57:23 -07003780 }
David Brazdilca3c8c32016-09-06 14:04:48 +01003781
Ian Rogers7b078e82014-09-10 14:44:24 -07003782 if (res_method != nullptr) {
Andreas Gampe42ef8ab2015-12-03 17:27:32 -08003783 stash_method = true;
Ian Rogersd81871c2011-10-03 13:57:23 -07003784 } else {
jeffhao8cd6dda2012-02-22 10:15:34 -08003785 // If a virtual or interface method wasn't found with the expected type, look in
3786 // the direct methods. This can happen when the wrong invoke type is used or when
3787 // a class has changed, and will be flagged as an error in later checks.
David Brazdilca3c8c32016-09-06 14:04:48 +01003788 // Note that in this case, we do not put the resolved method in the Dex cache
3789 // because it was not discovered using the expected type of method resolution.
3790 if (res_kind != kDirectMethodResolution) {
3791 // Record result of the initial resolution attempt.
3792 VerifierDeps::MaybeRecordMethodResolution(*dex_file_, dex_method_idx, res_kind, nullptr);
3793 // Change resolution type to 'direct' and try to resolve again.
3794 res_kind = kDirectMethodResolution;
Mathieu Chartiere401d142015-04-22 13:56:20 -07003795 res_method = klass->FindDirectMethod(name, signature, pointer_size);
jeffhao8cd6dda2012-02-22 10:15:34 -08003796 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003797 }
3798 }
David Brazdilca3c8c32016-09-06 14:04:48 +01003799
3800 // Record result of method resolution attempt.
3801 VerifierDeps::MaybeRecordMethodResolution(*dex_file_, dex_method_idx, res_kind, res_method);
3802
3803 if (res_method == nullptr) {
3804 Fail(VERIFY_ERROR_NO_METHOD) << "couldn't find method "
David Sehr709b0702016-10-13 09:12:37 -07003805 << klass->PrettyDescriptor() << "."
David Brazdilca3c8c32016-09-06 14:04:48 +01003806 << dex_file_->GetMethodName(method_id) << " "
3807 << dex_file_->GetMethodSignature(method_id);
3808 return nullptr;
3809 }
3810
Ian Rogersd81871c2011-10-03 13:57:23 -07003811 // Make sure calls to constructors are "direct". There are additional restrictions but we don't
3812 // enforce them here.
3813 if (res_method->IsConstructor() && method_type != METHOD_DIRECT) {
jeffhaod5347e02012-03-22 17:25:05 -07003814 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting non-direct call to constructor "
David Sehr709b0702016-10-13 09:12:37 -07003815 << res_method->PrettyMethod();
Ian Rogers7b078e82014-09-10 14:44:24 -07003816 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003817 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003818 // Disallow any calls to class initializers.
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003819 if (res_method->IsClassInitializer()) {
jeffhaod5347e02012-03-22 17:25:05 -07003820 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting call to class initializer "
David Sehr709b0702016-10-13 09:12:37 -07003821 << res_method->PrettyMethod();
Ian Rogers7b078e82014-09-10 14:44:24 -07003822 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003823 }
Andreas Gampe42ef8ab2015-12-03 17:27:32 -08003824
3825 // Check that interface methods are static or match interface classes.
3826 // We only allow statics if we don't have default methods enabled.
3827 //
3828 // Note: this check must be after the initializer check, as those are required to fail a class,
3829 // while this check implies an IncompatibleClassChangeError.
3830 if (klass->IsInterface()) {
Alex Lightb55f1ac2016-04-12 15:50:55 -07003831 // methods called on interfaces should be invoke-interface, invoke-super, invoke-direct (if
3832 // dex file version is 37 or greater), or invoke-static.
Andreas Gampe42ef8ab2015-12-03 17:27:32 -08003833 if (method_type != METHOD_INTERFACE &&
Neil Fuller9724c632016-01-07 15:42:47 +00003834 method_type != METHOD_STATIC &&
Alex Lightb55f1ac2016-04-12 15:50:55 -07003835 ((dex_file_->GetVersion() < DexFile::kDefaultMethodsVersion) ||
3836 method_type != METHOD_DIRECT) &&
Neil Fuller9724c632016-01-07 15:42:47 +00003837 method_type != METHOD_SUPER) {
Andreas Gampe42ef8ab2015-12-03 17:27:32 -08003838 Fail(VERIFY_ERROR_CLASS_CHANGE)
David Sehr709b0702016-10-13 09:12:37 -07003839 << "non-interface method " << dex_file_->PrettyMethod(dex_method_idx)
3840 << " is in an interface class " << klass->PrettyClass();
Andreas Gampe42ef8ab2015-12-03 17:27:32 -08003841 return nullptr;
3842 }
3843 } else {
3844 if (method_type == METHOD_INTERFACE) {
3845 Fail(VERIFY_ERROR_CLASS_CHANGE)
David Sehr709b0702016-10-13 09:12:37 -07003846 << "interface method " << dex_file_->PrettyMethod(dex_method_idx)
3847 << " is in a non-interface class " << klass->PrettyClass();
Andreas Gampe42ef8ab2015-12-03 17:27:32 -08003848 return nullptr;
3849 }
3850 }
3851
3852 // Only stash after the above passed. Otherwise the method wasn't guaranteed to be correct.
3853 if (stash_method) {
3854 dex_cache_->SetResolvedMethod(dex_method_idx, res_method, pointer_size);
3855 }
3856
jeffhao8cd6dda2012-02-22 10:15:34 -08003857 // Check if access is allowed.
Ian Rogersad0b3a32012-04-16 14:50:24 -07003858 if (!referrer.CanAccessMember(res_method->GetDeclaringClass(), res_method->GetAccessFlags())) {
David Sehr709b0702016-10-13 09:12:37 -07003859 Fail(VERIFY_ERROR_ACCESS_METHOD) << "illegal method access (call "
3860 << res_method->PrettyMethod()
Ian Rogersad0b3a32012-04-16 14:50:24 -07003861 << " from " << referrer << ")";
jeffhaob57e9522012-04-26 18:08:21 -07003862 return res_method;
jeffhao8cd6dda2012-02-22 10:15:34 -08003863 }
jeffhaode0d9c92012-02-27 13:58:13 -08003864 // Check that invoke-virtual and invoke-super are not used on private methods of the same class.
Alex Light7268d472016-01-20 15:50:01 -08003865 if (res_method->IsPrivate() && (method_type == METHOD_VIRTUAL || method_type == METHOD_SUPER)) {
jeffhaod5347e02012-03-22 17:25:05 -07003866 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invoke-super/virtual can't be used on private method "
David Sehr709b0702016-10-13 09:12:37 -07003867 << res_method->PrettyMethod();
Ian Rogers7b078e82014-09-10 14:44:24 -07003868 return nullptr;
jeffhaode0d9c92012-02-27 13:58:13 -08003869 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003870 // See if the method type implied by the invoke instruction matches the access flags for the
3871 // target method.
Brian Carlstrombe6fa5e2014-12-09 20:15:42 -08003872 if ((method_type == METHOD_DIRECT && (!res_method->IsDirect() || res_method->IsStatic())) ||
Ian Rogersd81871c2011-10-03 13:57:23 -07003873 (method_type == METHOD_STATIC && !res_method->IsStatic()) ||
Alex Light7268d472016-01-20 15:50:01 -08003874 ((method_type == METHOD_SUPER ||
3875 method_type == METHOD_VIRTUAL ||
3876 method_type == METHOD_INTERFACE) && res_method->IsDirect())
Ian Rogersd81871c2011-10-03 13:57:23 -07003877 ) {
Ian Rogers2fc14272012-08-30 10:56:57 -07003878 Fail(VERIFY_ERROR_CLASS_CHANGE) << "invoke type (" << method_type << ") does not match method "
David Sehr709b0702016-10-13 09:12:37 -07003879 " type of " << res_method->PrettyMethod();
Ian Rogers7b078e82014-09-10 14:44:24 -07003880 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003881 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003882 return res_method;
3883}
3884
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003885template <class T>
Mathieu Chartiere401d142015-04-22 13:56:20 -07003886ArtMethod* MethodVerifier::VerifyInvocationArgsFromIterator(
3887 T* it, const Instruction* inst, MethodType method_type, bool is_range, ArtMethod* res_method) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003888 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
3889 // match the call to the signature. Also, we might be calling through an abstract method
3890 // definition (which doesn't have register count values).
3891 const size_t expected_args = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
3892 /* caught by static verifier */
3893 DCHECK(is_range || expected_args <= 5);
3894 if (expected_args > code_item_->outs_size_) {
3895 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
3896 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
3897 return nullptr;
3898 }
3899
3900 uint32_t arg[5];
3901 if (!is_range) {
3902 inst->GetVarArgs(arg);
3903 }
3904 uint32_t sig_registers = 0;
3905
3906 /*
3907 * Check the "this" argument, which must be an instance of the class that declared the method.
3908 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
3909 * rigorous check here (which is okay since we have to do it at runtime).
3910 */
3911 if (method_type != METHOD_STATIC) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003912 const RegType& actual_arg_type = work_line_->GetInvocationThis(this, inst, is_range);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003913 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
3914 CHECK(have_pending_hard_failure_);
3915 return nullptr;
3916 }
David Brazdil68b5c0b2016-01-19 14:25:29 +00003917 bool is_init = false;
3918 if (actual_arg_type.IsUninitializedTypes()) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003919 if (res_method) {
3920 if (!res_method->IsConstructor()) {
3921 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
3922 return nullptr;
3923 }
3924 } else {
3925 // Check whether the name of the called method is "<init>"
3926 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Jeff Hao0d087272014-08-04 14:47:17 -07003927 if (strcmp(dex_file_->GetMethodName(dex_file_->GetMethodId(method_idx)), "<init>") != 0) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003928 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
3929 return nullptr;
3930 }
3931 }
David Brazdil68b5c0b2016-01-19 14:25:29 +00003932 is_init = true;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003933 }
David Brazdil68b5c0b2016-01-19 14:25:29 +00003934 const RegType& adjusted_type = is_init
3935 ? GetRegTypeCache()->FromUninitialized(actual_arg_type)
3936 : actual_arg_type;
3937 if (method_type != METHOD_INTERFACE && !adjusted_type.IsZero()) {
Ian Rogersd8f69b02014-09-10 21:43:52 +00003938 const RegType* res_method_class;
Andreas Gamped9e23012015-06-04 22:19:58 -07003939 // Miranda methods have the declaring interface as their declaring class, not the abstract
3940 // class. It would be wrong to use this for the type check (interface type checks are
3941 // postponed to runtime).
3942 if (res_method != nullptr && !res_method->IsMiranda()) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003943 mirror::Class* klass = res_method->GetDeclaringClass();
Ian Rogers1ff3c982014-08-12 02:30:58 -07003944 std::string temp;
Andreas Gampef23f33d2015-06-23 14:18:17 -07003945 res_method_class = &FromClass(klass->GetDescriptor(&temp), klass,
3946 klass->CannotBeAssignedFromOtherTypes());
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003947 } else {
3948 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Andreas Gampea5b09a62016-11-17 15:21:22 -08003949 const dex::TypeIndex class_idx = dex_file_->GetMethodId(method_idx).class_idx_;
Mathieu Chartierde40d472015-10-15 17:47:48 -07003950 res_method_class = &reg_types_.FromDescriptor(
3951 GetClassLoader(),
3952 dex_file_->StringByTypeIdx(class_idx),
3953 false);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003954 }
David Brazdilca3c8c32016-09-06 14:04:48 +01003955 if (!res_method_class->IsAssignableFrom(adjusted_type, this)) {
David Brazdil68b5c0b2016-01-19 14:25:29 +00003956 Fail(adjusted_type.IsUnresolvedTypes()
3957 ? VERIFY_ERROR_NO_CLASS
3958 : VERIFY_ERROR_BAD_CLASS_SOFT)
3959 << "'this' argument '" << actual_arg_type << "' not instance of '"
3960 << *res_method_class << "'";
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003961 // Continue on soft failures. We need to find possible hard failures to avoid problems in
3962 // the compiler.
3963 if (have_pending_hard_failure_) {
3964 return nullptr;
3965 }
3966 }
3967 }
3968 sig_registers = 1;
3969 }
3970
3971 for ( ; it->HasNext(); it->Next()) {
3972 if (sig_registers >= expected_args) {
3973 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, expected " << inst->VRegA() <<
3974 " arguments, found " << sig_registers << " or more.";
3975 return nullptr;
3976 }
3977
3978 const char* param_descriptor = it->GetDescriptor();
3979
3980 if (param_descriptor == nullptr) {
3981 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation because of missing signature "
3982 "component";
3983 return nullptr;
3984 }
3985
Ian Rogersd8f69b02014-09-10 21:43:52 +00003986 const RegType& reg_type = reg_types_.FromDescriptor(GetClassLoader(), param_descriptor, false);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003987 uint32_t get_reg = is_range ? inst->VRegC_3rc() + static_cast<uint32_t>(sig_registers) :
3988 arg[sig_registers];
3989 if (reg_type.IsIntegralTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003990 const RegType& src_type = work_line_->GetRegisterType(this, get_reg);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003991 if (!src_type.IsIntegralTypes()) {
3992 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register v" << get_reg << " has type " << src_type
3993 << " but expected " << reg_type;
Andreas Gampeb588f4c2015-05-26 13:35:39 -07003994 return nullptr;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003995 }
Andreas Gampeda9badb2015-06-05 20:22:12 -07003996 } else {
3997 if (!work_line_->VerifyRegisterType(this, get_reg, reg_type)) {
3998 // Continue on soft failures. We need to find possible hard failures to avoid problems in
3999 // the compiler.
4000 if (have_pending_hard_failure_) {
4001 return nullptr;
4002 }
4003 } else if (reg_type.IsLongOrDoubleTypes()) {
4004 // Check that registers are consecutive (for non-range invokes). Invokes are the only
4005 // instructions not specifying register pairs by the first component, but require them
4006 // nonetheless. Only check when there's an actual register in the parameters. If there's
4007 // none, this will fail below.
4008 if (!is_range && sig_registers + 1 < expected_args) {
4009 uint32_t second_reg = arg[sig_registers + 1];
4010 if (second_reg != get_reg + 1) {
4011 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, long or double parameter "
4012 "at index " << sig_registers << " is not a pair: " << get_reg << " + "
4013 << second_reg << ".";
4014 return nullptr;
4015 }
4016 }
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004017 }
4018 }
4019 sig_registers += reg_type.IsLongOrDoubleTypes() ? 2 : 1;
4020 }
4021 if (expected_args != sig_registers) {
4022 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, expected " << expected_args <<
4023 " arguments, found " << sig_registers;
4024 return nullptr;
4025 }
4026 return res_method;
4027}
4028
4029void MethodVerifier::VerifyInvocationArgsUnresolvedMethod(const Instruction* inst,
4030 MethodType method_type,
4031 bool is_range) {
4032 // As the method may not have been resolved, make this static check against what we expect.
4033 // The main reason for this code block is to fail hard when we find an illegal use, e.g.,
4034 // wrong number of arguments or wrong primitive types, even if the method could not be resolved.
4035 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
4036 DexFileParameterIterator it(*dex_file_,
4037 dex_file_->GetProtoId(dex_file_->GetMethodId(method_idx).proto_idx_));
4038 VerifyInvocationArgsFromIterator<DexFileParameterIterator>(&it, inst, method_type, is_range,
4039 nullptr);
4040}
4041
4042class MethodParamListDescriptorIterator {
4043 public:
Mathieu Chartiere401d142015-04-22 13:56:20 -07004044 explicit MethodParamListDescriptorIterator(ArtMethod* res_method) :
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004045 res_method_(res_method), pos_(0), params_(res_method->GetParameterTypeList()),
4046 params_size_(params_ == nullptr ? 0 : params_->Size()) {
4047 }
4048
4049 bool HasNext() {
4050 return pos_ < params_size_;
4051 }
4052
4053 void Next() {
4054 ++pos_;
4055 }
4056
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07004057 const char* GetDescriptor() REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004058 return res_method_->GetTypeDescriptorFromTypeIdx(params_->GetTypeItem(pos_).type_idx_);
4059 }
4060
4061 private:
Mathieu Chartiere401d142015-04-22 13:56:20 -07004062 ArtMethod* res_method_;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004063 size_t pos_;
4064 const DexFile::TypeList* params_;
4065 const size_t params_size_;
4066};
4067
Mathieu Chartiere401d142015-04-22 13:56:20 -07004068ArtMethod* MethodVerifier::VerifyInvocationArgs(
Alex Light7268d472016-01-20 15:50:01 -08004069 const Instruction* inst, MethodType method_type, bool is_range) {
jeffhao8cd6dda2012-02-22 10:15:34 -08004070 // Resolve the method. This could be an abstract or concrete method depending on what sort of call
4071 // we're making.
Sebastien Hertz5243e912013-05-21 10:55:07 +02004072 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Andreas Gampeacc4d2f2014-06-12 19:35:05 -07004073
Alex Light7268d472016-01-20 15:50:01 -08004074 ArtMethod* res_method = ResolveMethodAndCheckAccess(method_idx, method_type);
Ian Rogers7b078e82014-09-10 14:44:24 -07004075 if (res_method == nullptr) { // error or class is unresolved
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004076 // Check what we can statically.
4077 if (!have_pending_hard_failure_) {
4078 VerifyInvocationArgsUnresolvedMethod(inst, method_type, is_range);
4079 }
4080 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08004081 }
4082
Ian Rogersd81871c2011-10-03 13:57:23 -07004083 // If we're using invoke-super(method), make sure that the executing method's class' superclass
Alex Light705ad492015-09-21 11:36:30 -07004084 // has a vtable entry for the target method. Or the target is on a interface.
Alex Light7268d472016-01-20 15:50:01 -08004085 if (method_type == METHOD_SUPER) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08004086 dex::TypeIndex class_idx = dex_file_->GetMethodId(method_idx).class_idx_;
David Brazdilca3c8c32016-09-06 14:04:48 +01004087 const RegType& reference_type = reg_types_.FromDescriptor(
4088 GetClassLoader(),
4089 dex_file_->StringByTypeIdx(class_idx),
4090 false);
4091 if (reference_type.IsUnresolvedTypes()) {
Alex Lightfedd91d2016-01-07 14:49:16 -08004092 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "Unable to find referenced class from invoke-super";
4093 return nullptr;
4094 }
David Brazdilca3c8c32016-09-06 14:04:48 +01004095 if (reference_type.GetClass()->IsInterface()) {
Alex Lightfedd91d2016-01-07 14:49:16 -08004096 // TODO Can we verify anything else.
David Brazdil15fc7292016-09-02 14:13:18 +01004097 if (class_idx == class_def_.class_idx_) {
Alex Lightfedd91d2016-01-07 14:49:16 -08004098 Fail(VERIFY_ERROR_CLASS_CHANGE) << "Cannot invoke-super on self as interface";
Alex Light55ea94d2016-03-15 09:50:26 -07004099 return nullptr;
Alex Lightfedd91d2016-01-07 14:49:16 -08004100 }
4101 // TODO Revisit whether we want to allow invoke-super on direct interfaces only like the JLS
4102 // does.
Alex Light55ea94d2016-03-15 09:50:26 -07004103 if (!GetDeclaringClass().HasClass()) {
4104 Fail(VERIFY_ERROR_NO_CLASS) << "Unable to resolve the full class of 'this' used in an"
4105 << "interface invoke-super";
4106 return nullptr;
David Brazdilca3c8c32016-09-06 14:04:48 +01004107 } else if (!reference_type.IsStrictlyAssignableFrom(GetDeclaringClass(), this)) {
Alex Lightfedd91d2016-01-07 14:49:16 -08004108 Fail(VERIFY_ERROR_CLASS_CHANGE)
David Sehr709b0702016-10-13 09:12:37 -07004109 << "invoke-super in " << mirror::Class::PrettyClass(GetDeclaringClass().GetClass())
4110 << " in method "
4111 << dex_file_->PrettyMethod(dex_method_idx_) << " to method "
4112 << dex_file_->PrettyMethod(method_idx) << " references "
4113 << "non-super-interface type " << mirror::Class::PrettyClass(reference_type.GetClass());
Alex Lightfedd91d2016-01-07 14:49:16 -08004114 return nullptr;
Alex Light705ad492015-09-21 11:36:30 -07004115 }
4116 } else {
4117 const RegType& super = GetDeclaringClass().GetSuperClass(&reg_types_);
4118 if (super.IsUnresolvedTypes()) {
4119 Fail(VERIFY_ERROR_NO_METHOD) << "unknown super class in invoke-super from "
David Sehr709b0702016-10-13 09:12:37 -07004120 << dex_file_->PrettyMethod(dex_method_idx_)
4121 << " to super " << res_method->PrettyMethod();
Alex Light705ad492015-09-21 11:36:30 -07004122 return nullptr;
4123 }
David Brazdilca3c8c32016-09-06 14:04:48 +01004124 if (!reference_type.IsStrictlyAssignableFrom(GetDeclaringClass(), this) ||
Aart Bikf663e342016-04-04 17:28:59 -07004125 (res_method->GetMethodIndex() >= super.GetClass()->GetVTableLength())) {
Alex Light705ad492015-09-21 11:36:30 -07004126 Fail(VERIFY_ERROR_NO_METHOD) << "invalid invoke-super from "
David Sehr709b0702016-10-13 09:12:37 -07004127 << dex_file_->PrettyMethod(dex_method_idx_)
Alex Light705ad492015-09-21 11:36:30 -07004128 << " to super " << super
4129 << "." << res_method->GetName()
4130 << res_method->GetSignature();
4131 return nullptr;
4132 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004133 }
4134 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08004135
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004136 // Process the target method's signature. This signature may or may not
4137 MethodParamListDescriptorIterator it(res_method);
4138 return VerifyInvocationArgsFromIterator<MethodParamListDescriptorIterator>(&it, inst, method_type,
4139 is_range, res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07004140}
4141
Mathieu Chartiere401d142015-04-22 13:56:20 -07004142ArtMethod* MethodVerifier::GetQuickInvokedMethod(const Instruction* inst, RegisterLine* reg_line,
4143 bool is_range, bool allow_failure) {
Mathieu Chartier091d2382015-03-06 10:59:06 -08004144 if (is_range) {
4145 DCHECK_EQ(inst->Opcode(), Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
4146 } else {
4147 DCHECK_EQ(inst->Opcode(), Instruction::INVOKE_VIRTUAL_QUICK);
4148 }
4149 const RegType& actual_arg_type = reg_line->GetInvocationThis(this, inst, is_range, allow_failure);
Ian Rogers9bc54402014-04-17 16:40:01 -07004150 if (!actual_arg_type.HasClass()) {
4151 VLOG(verifier) << "Failed to get mirror::Class* from '" << actual_arg_type << "'";
Andreas Gampe63981562014-04-17 12:28:43 -07004152 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004153 }
Ian Rogersa4cf1df2014-05-07 19:47:17 -07004154 mirror::Class* klass = actual_arg_type.GetClass();
Mingyao Yang2cdbad72014-07-16 10:44:41 -07004155 mirror::Class* dispatch_class;
Ian Rogersa4cf1df2014-05-07 19:47:17 -07004156 if (klass->IsInterface()) {
4157 // Derive Object.class from Class.class.getSuperclass().
4158 mirror::Class* object_klass = klass->GetClass()->GetSuperClass();
Andreas Gampe7c038102014-10-27 20:08:46 -07004159 if (FailOrAbort(this, object_klass->IsObjectClass(),
Mathieu Chartier091d2382015-03-06 10:59:06 -08004160 "Failed to find Object class in quickened invoke receiver", work_insn_idx_)) {
Andreas Gampe7c038102014-10-27 20:08:46 -07004161 return nullptr;
4162 }
Mingyao Yang2cdbad72014-07-16 10:44:41 -07004163 dispatch_class = object_klass;
Ian Rogersa4cf1df2014-05-07 19:47:17 -07004164 } else {
Mingyao Yang2cdbad72014-07-16 10:44:41 -07004165 dispatch_class = klass;
Ian Rogersa4cf1df2014-05-07 19:47:17 -07004166 }
Mathieu Chartier091d2382015-03-06 10:59:06 -08004167 if (!dispatch_class->HasVTable()) {
4168 FailOrAbort(this, allow_failure, "Receiver class has no vtable for quickened invoke at ",
4169 work_insn_idx_);
Andreas Gampe7c038102014-10-27 20:08:46 -07004170 return nullptr;
4171 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004172 uint16_t vtable_index = is_range ? inst->VRegB_3rc() : inst->VRegB_35c();
Mathieu Chartiere401d142015-04-22 13:56:20 -07004173 auto* cl = Runtime::Current()->GetClassLinker();
4174 auto pointer_size = cl->GetImagePointerSize();
Mathieu Chartier091d2382015-03-06 10:59:06 -08004175 if (static_cast<int32_t>(vtable_index) >= dispatch_class->GetVTableLength()) {
4176 FailOrAbort(this, allow_failure,
4177 "Receiver class has not enough vtable slots for quickened invoke at ",
4178 work_insn_idx_);
Andreas Gampe7c038102014-10-27 20:08:46 -07004179 return nullptr;
4180 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07004181 ArtMethod* res_method = dispatch_class->GetVTableEntry(vtable_index, pointer_size);
Mathieu Chartier091d2382015-03-06 10:59:06 -08004182 if (self_->IsExceptionPending()) {
4183 FailOrAbort(this, allow_failure, "Unexpected exception pending for quickened invoke at ",
4184 work_insn_idx_);
Andreas Gampe7c038102014-10-27 20:08:46 -07004185 return nullptr;
4186 }
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004187 return res_method;
4188}
4189
Mathieu Chartiere401d142015-04-22 13:56:20 -07004190ArtMethod* MethodVerifier::VerifyInvokeVirtualQuickArgs(const Instruction* inst, bool is_range) {
Andreas Gampe76bd8802014-12-10 16:43:58 -08004191 DCHECK(Runtime::Current()->IsStarted() || verify_to_dump_)
David Sehr709b0702016-10-13 09:12:37 -07004192 << dex_file_->PrettyMethod(dex_method_idx_, true) << "@" << work_insn_idx_;
Andreas Gampe76bd8802014-12-10 16:43:58 -08004193
Mathieu Chartiere401d142015-04-22 13:56:20 -07004194 ArtMethod* res_method = GetQuickInvokedMethod(inst, work_line_.get(), is_range, false);
Ian Rogers7b078e82014-09-10 14:44:24 -07004195 if (res_method == nullptr) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004196 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer method from " << inst->Name();
Ian Rogers7b078e82014-09-10 14:44:24 -07004197 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004198 }
Andreas Gampe7c038102014-10-27 20:08:46 -07004199 if (FailOrAbort(this, !res_method->IsDirect(), "Quick-invoked method is direct at ",
4200 work_insn_idx_)) {
4201 return nullptr;
4202 }
4203 if (FailOrAbort(this, !res_method->IsStatic(), "Quick-invoked method is static at ",
4204 work_insn_idx_)) {
4205 return nullptr;
4206 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004207
4208 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
4209 // match the call to the signature. Also, we might be calling through an abstract method
4210 // definition (which doesn't have register count values).
Ian Rogers7b078e82014-09-10 14:44:24 -07004211 const RegType& actual_arg_type = work_line_->GetInvocationThis(this, inst, is_range);
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004212 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
Ian Rogers7b078e82014-09-10 14:44:24 -07004213 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004214 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004215 const size_t expected_args = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
4216 /* caught by static verifier */
4217 DCHECK(is_range || expected_args <= 5);
4218 if (expected_args > code_item_->outs_size_) {
4219 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
4220 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
Ian Rogers7b078e82014-09-10 14:44:24 -07004221 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004222 }
4223
4224 /*
4225 * Check the "this" argument, which must be an instance of the class that declared the method.
4226 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
4227 * rigorous check here (which is okay since we have to do it at runtime).
4228 */
David Brazdil68b5c0b2016-01-19 14:25:29 +00004229 // Note: given an uninitialized type, this should always fail. Constructors aren't virtual.
4230 if (actual_arg_type.IsUninitializedTypes() && !res_method->IsConstructor()) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004231 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
Ian Rogers7b078e82014-09-10 14:44:24 -07004232 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004233 }
4234 if (!actual_arg_type.IsZero()) {
4235 mirror::Class* klass = res_method->GetDeclaringClass();
Ian Rogers1ff3c982014-08-12 02:30:58 -07004236 std::string temp;
Ian Rogersd8f69b02014-09-10 21:43:52 +00004237 const RegType& res_method_class =
Andreas Gampef23f33d2015-06-23 14:18:17 -07004238 FromClass(klass->GetDescriptor(&temp), klass, klass->CannotBeAssignedFromOtherTypes());
David Brazdilca3c8c32016-09-06 14:04:48 +01004239 if (!res_method_class.IsAssignableFrom(actual_arg_type, this)) {
David Brazdil68b5c0b2016-01-19 14:25:29 +00004240 Fail(actual_arg_type.IsUninitializedTypes() // Just overcautious - should have never
4241 ? VERIFY_ERROR_BAD_CLASS_HARD // quickened this.
4242 : actual_arg_type.IsUnresolvedTypes()
4243 ? VERIFY_ERROR_NO_CLASS
4244 : VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004245 << "' not instance of '" << res_method_class << "'";
Ian Rogers7b078e82014-09-10 14:44:24 -07004246 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004247 }
4248 }
4249 /*
4250 * Process the target method's signature. This signature may or may not
4251 * have been verified, so we can't assume it's properly formed.
4252 */
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07004253 const DexFile::TypeList* params = res_method->GetParameterTypeList();
Ian Rogers7b078e82014-09-10 14:44:24 -07004254 size_t params_size = params == nullptr ? 0 : params->Size();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004255 uint32_t arg[5];
4256 if (!is_range) {
Ian Rogers29a26482014-05-02 15:27:29 -07004257 inst->GetVarArgs(arg);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004258 }
4259 size_t actual_args = 1;
4260 for (size_t param_index = 0; param_index < params_size; param_index++) {
4261 if (actual_args >= expected_args) {
David Sehr709b0702016-10-13 09:12:37 -07004262 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invalid call to '"
4263 << res_method->PrettyMethod()
Brian Carlstrom93c33962013-07-26 10:37:43 -07004264 << "'. Expected " << expected_args
4265 << " arguments, processing argument " << actual_args
4266 << " (where longs/doubles count twice).";
Ian Rogers7b078e82014-09-10 14:44:24 -07004267 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004268 }
4269 const char* descriptor =
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07004270 res_method->GetTypeDescriptorFromTypeIdx(params->GetTypeItem(param_index).type_idx_);
Ian Rogers7b078e82014-09-10 14:44:24 -07004271 if (descriptor == nullptr) {
David Sehr709b0702016-10-13 09:12:37 -07004272 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of "
4273 << res_method->PrettyMethod()
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004274 << " missing signature component";
Ian Rogers7b078e82014-09-10 14:44:24 -07004275 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004276 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00004277 const RegType& reg_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004278 uint32_t get_reg = is_range ? inst->VRegC_3rc() + actual_args : arg[actual_args];
Ian Rogers7b078e82014-09-10 14:44:24 -07004279 if (!work_line_->VerifyRegisterType(this, get_reg, reg_type)) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004280 return res_method;
4281 }
4282 actual_args = reg_type.IsLongOrDoubleTypes() ? actual_args + 2 : actual_args + 1;
4283 }
4284 if (actual_args != expected_args) {
David Sehr709b0702016-10-13 09:12:37 -07004285 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of "
4286 << res_method->PrettyMethod() << " expected "
4287 << expected_args << " arguments, found " << actual_args;
Ian Rogers7b078e82014-09-10 14:44:24 -07004288 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004289 } else {
4290 return res_method;
4291 }
4292}
4293
Ian Rogers62342ec2013-06-11 10:26:37 -07004294void MethodVerifier::VerifyNewArray(const Instruction* inst, bool is_filled, bool is_range) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08004295 dex::TypeIndex type_idx;
Sebastien Hertz5243e912013-05-21 10:55:07 +02004296 if (!is_filled) {
4297 DCHECK_EQ(inst->Opcode(), Instruction::NEW_ARRAY);
Andreas Gampea5b09a62016-11-17 15:21:22 -08004298 type_idx = dex::TypeIndex(inst->VRegC_22c());
Sebastien Hertz5243e912013-05-21 10:55:07 +02004299 } else if (!is_range) {
4300 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY);
Andreas Gampea5b09a62016-11-17 15:21:22 -08004301 type_idx = dex::TypeIndex(inst->VRegB_35c());
Sebastien Hertz5243e912013-05-21 10:55:07 +02004302 } else {
4303 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY_RANGE);
Andreas Gampea5b09a62016-11-17 15:21:22 -08004304 type_idx = dex::TypeIndex(inst->VRegB_3rc());
Sebastien Hertz5243e912013-05-21 10:55:07 +02004305 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00004306 const RegType& res_type = ResolveClassAndCheckAccess(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004307 if (res_type.IsConflict()) { // bad class
4308 DCHECK_NE(failures_.size(), 0U);
Ian Rogers0c4a5062012-02-03 15:18:59 -08004309 } else {
4310 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
4311 if (!res_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07004312 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "new-array on non-array class " << res_type;
Ian Rogers0c4a5062012-02-03 15:18:59 -08004313 } else if (!is_filled) {
4314 /* make sure "size" register is valid type */
Ian Rogers7b078e82014-09-10 14:44:24 -07004315 work_line_->VerifyRegisterType(this, inst->VRegB_22c(), reg_types_.Integer());
Ian Rogers0c4a5062012-02-03 15:18:59 -08004316 /* set register type to array class */
Ian Rogersd8f69b02014-09-10 21:43:52 +00004317 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
Andreas Gampead238ce2015-08-24 21:13:08 -07004318 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_22c(), precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08004319 } else {
Andreas Gampebb18a032016-03-22 20:34:25 -07004320 DCHECK(!res_type.IsUnresolvedMergedReference());
Ian Rogers0c4a5062012-02-03 15:18:59 -08004321 // Verify each register. If "arg_count" is bad, VerifyRegisterType() will run off the end of
4322 // the list and fail. It's legal, if silly, for arg_count to be zero.
Ian Rogersd8f69b02014-09-10 21:43:52 +00004323 const RegType& expected_type = reg_types_.GetComponentType(res_type, GetClassLoader());
Sebastien Hertz5243e912013-05-21 10:55:07 +02004324 uint32_t arg_count = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
4325 uint32_t arg[5];
4326 if (!is_range) {
Ian Rogers29a26482014-05-02 15:27:29 -07004327 inst->GetVarArgs(arg);
Sebastien Hertz5243e912013-05-21 10:55:07 +02004328 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08004329 for (size_t ui = 0; ui < arg_count; ui++) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02004330 uint32_t get_reg = is_range ? inst->VRegC_3rc() + ui : arg[ui];
Ian Rogers7b078e82014-09-10 14:44:24 -07004331 if (!work_line_->VerifyRegisterType(this, get_reg, expected_type)) {
4332 work_line_->SetResultRegisterType(this, reg_types_.Conflict());
Ian Rogers0c4a5062012-02-03 15:18:59 -08004333 return;
4334 }
4335 }
4336 // filled-array result goes into "result" register
Ian Rogersd8f69b02014-09-10 21:43:52 +00004337 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
Ian Rogers7b078e82014-09-10 14:44:24 -07004338 work_line_->SetResultRegisterType(this, precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08004339 }
4340 }
4341}
4342
Sebastien Hertz5243e912013-05-21 10:55:07 +02004343void MethodVerifier::VerifyAGet(const Instruction* inst,
Ian Rogersd8f69b02014-09-10 21:43:52 +00004344 const RegType& insn_type, bool is_primitive) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004345 const RegType& index_type = work_line_->GetRegisterType(this, inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07004346 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07004347 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07004348 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004349 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegB_23x());
Ian Rogers89310de2012-02-01 13:47:30 -08004350 if (array_type.IsZero()) {
Nicolas Geoffray4824c272015-06-24 15:53:03 +01004351 have_pending_runtime_throw_failure_ = true;
Ian Rogers89310de2012-02-01 13:47:30 -08004352 // Null array class; this code path will fail at runtime. Infer a merge-able type from the
4353 // instruction type. TODO: have a proper notion of bottom here.
4354 if (!is_primitive || insn_type.IsCategory1Types()) {
4355 // Reference or category 1
Andreas Gampead238ce2015-08-24 21:13:08 -07004356 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), reg_types_.Zero());
Ian Rogersd81871c2011-10-03 13:57:23 -07004357 } else {
Ian Rogers89310de2012-02-01 13:47:30 -08004358 // Category 2
Ian Rogers7b078e82014-09-10 14:44:24 -07004359 work_line_->SetRegisterTypeWide(this, inst->VRegA_23x(),
4360 reg_types_.FromCat2ConstLo(0, false),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004361 reg_types_.FromCat2ConstHi(0, false));
Ian Rogers89310de2012-02-01 13:47:30 -08004362 }
jeffhaofc3144e2012-02-01 17:21:15 -08004363 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07004364 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aget";
Andreas Gampe8d8fc482016-03-25 16:24:20 -07004365 } else if (array_type.IsUnresolvedMergedReference()) {
Andreas Gampebb18a032016-03-22 20:34:25 -07004366 // Unresolved array types must be reference array types.
4367 if (is_primitive) {
4368 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type
4369 << " source for category 1 aget";
4370 } else {
4371 Fail(VERIFY_ERROR_NO_CLASS) << "cannot verify aget for " << array_type
4372 << " because of missing class";
Andreas Gampe8d8fc482016-03-25 16:24:20 -07004373 // Approximate with java.lang.Object[].
4374 work_line_->SetRegisterType<LockOp::kClear>(this,
4375 inst->VRegA_23x(),
4376 reg_types_.JavaLangObject(false));
Andreas Gampebb18a032016-03-22 20:34:25 -07004377 }
Ian Rogers89310de2012-02-01 13:47:30 -08004378 } else {
4379 /* verify the class */
Ian Rogersd8f69b02014-09-10 21:43:52 +00004380 const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
jeffhaofc3144e2012-02-01 17:21:15 -08004381 if (!component_type.IsReferenceTypes() && !is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07004382 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08004383 << " source for aget-object";
4384 } else if (component_type.IsNonZeroReferenceTypes() && is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07004385 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08004386 << " source for category 1 aget";
4387 } else if (is_primitive && !insn_type.Equals(component_type) &&
4388 !((insn_type.IsInteger() && component_type.IsFloat()) ||
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004389 (insn_type.IsLong() && component_type.IsDouble()))) {
4390 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array type " << array_type
4391 << " incompatible with aget of type " << insn_type;
Ian Rogers89310de2012-02-01 13:47:30 -08004392 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004393 // Use knowledge of the field type which is stronger than the type inferred from the
4394 // instruction, which can't differentiate object types and ints from floats, longs from
4395 // doubles.
4396 if (!component_type.IsLowHalf()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07004397 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), component_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004398 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004399 work_line_->SetRegisterTypeWide(this, inst->VRegA_23x(), component_type,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004400 component_type.HighHalf(&reg_types_));
4401 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004402 }
4403 }
4404 }
4405}
4406
Ian Rogersd8f69b02014-09-10 21:43:52 +00004407void MethodVerifier::VerifyPrimitivePut(const RegType& target_type, const RegType& insn_type,
Jeff Haofe1f7c82013-08-01 14:50:24 -07004408 const uint32_t vregA) {
4409 // Primitive assignability rules are weaker than regular assignability rules.
4410 bool instruction_compatible;
4411 bool value_compatible;
Ian Rogers7b078e82014-09-10 14:44:24 -07004412 const RegType& value_type = work_line_->GetRegisterType(this, vregA);
Jeff Haofe1f7c82013-08-01 14:50:24 -07004413 if (target_type.IsIntegralTypes()) {
Jeff Haoa4647482013-08-06 15:35:47 -07004414 instruction_compatible = target_type.Equals(insn_type);
Jeff Haofe1f7c82013-08-01 14:50:24 -07004415 value_compatible = value_type.IsIntegralTypes();
4416 } else if (target_type.IsFloat()) {
4417 instruction_compatible = insn_type.IsInteger(); // no put-float, so expect put-int
4418 value_compatible = value_type.IsFloatTypes();
4419 } else if (target_type.IsLong()) {
4420 instruction_compatible = insn_type.IsLong();
Andreas Gampe376fa682014-09-07 13:06:12 -07004421 // Additional register check: this is not checked statically (as part of VerifyInstructions),
4422 // as target_type depends on the resolved type of the field.
4423 if (instruction_compatible && work_line_->NumRegs() > vregA + 1) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004424 const RegType& value_type_hi = work_line_->GetRegisterType(this, vregA + 1);
Andreas Gampe376fa682014-09-07 13:06:12 -07004425 value_compatible = value_type.IsLongTypes() && value_type.CheckWidePair(value_type_hi);
4426 } else {
4427 value_compatible = false;
4428 }
Jeff Haofe1f7c82013-08-01 14:50:24 -07004429 } else if (target_type.IsDouble()) {
4430 instruction_compatible = insn_type.IsLong(); // no put-double, so expect put-long
Andreas Gampe376fa682014-09-07 13:06:12 -07004431 // Additional register check: this is not checked statically (as part of VerifyInstructions),
4432 // as target_type depends on the resolved type of the field.
4433 if (instruction_compatible && work_line_->NumRegs() > vregA + 1) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004434 const RegType& value_type_hi = work_line_->GetRegisterType(this, vregA + 1);
Andreas Gampe376fa682014-09-07 13:06:12 -07004435 value_compatible = value_type.IsDoubleTypes() && value_type.CheckWidePair(value_type_hi);
4436 } else {
4437 value_compatible = false;
4438 }
Jeff Haofe1f7c82013-08-01 14:50:24 -07004439 } else {
4440 instruction_compatible = false; // reference with primitive store
4441 value_compatible = false; // unused
4442 }
4443 if (!instruction_compatible) {
4444 // This is a global failure rather than a class change failure as the instructions and
4445 // the descriptors for the type should have been consistent within the same file at
4446 // compile time.
4447 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "put insn has type '" << insn_type
4448 << "' but expected type '" << target_type << "'";
4449 return;
4450 }
4451 if (!value_compatible) {
4452 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
4453 << " of type " << value_type << " but expected " << target_type << " for put";
4454 return;
4455 }
4456}
4457
Sebastien Hertz5243e912013-05-21 10:55:07 +02004458void MethodVerifier::VerifyAPut(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 Geoffray66389fb2015-06-19 10:35:42 +01004466 // Null array type; this code path will fail at runtime.
4467 // Still check that the given value matches the instruction's type.
Andreas Gampe4bf4c782015-08-14 14:07:43 -07004468 // Note: this is, as usual, complicated by the fact the the instruction isn't fully typed
4469 // and fits multiple register types.
4470 const RegType* modified_reg_type = &insn_type;
4471 if ((modified_reg_type == &reg_types_.Integer()) ||
4472 (modified_reg_type == &reg_types_.LongLo())) {
4473 // May be integer or float | long or double. Overwrite insn_type accordingly.
4474 const RegType& value_type = work_line_->GetRegisterType(this, inst->VRegA_23x());
4475 if (modified_reg_type == &reg_types_.Integer()) {
4476 if (&value_type == &reg_types_.Float()) {
4477 modified_reg_type = &value_type;
4478 }
4479 } else {
4480 if (&value_type == &reg_types_.DoubleLo()) {
4481 modified_reg_type = &value_type;
4482 }
4483 }
4484 }
4485 work_line_->VerifyRegisterType(this, inst->VRegA_23x(), *modified_reg_type);
jeffhaofc3144e2012-02-01 17:21:15 -08004486 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07004487 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aput";
Andreas Gampe8d8fc482016-03-25 16:24:20 -07004488 } else if (array_type.IsUnresolvedMergedReference()) {
Andreas Gampebb18a032016-03-22 20:34:25 -07004489 // Unresolved array types must be reference array types.
4490 if (is_primitive) {
4491 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "put insn has type '" << insn_type
4492 << "' but unresolved type '" << array_type << "'";
4493 } else {
4494 Fail(VERIFY_ERROR_NO_CLASS) << "cannot verify aput for " << array_type
4495 << " because of missing class";
4496 }
Ian Rogers89310de2012-02-01 13:47:30 -08004497 } else {
Ian Rogersd8f69b02014-09-10 21:43:52 +00004498 const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
Jeff Haofe1f7c82013-08-01 14:50:24 -07004499 const uint32_t vregA = inst->VRegA_23x();
Jeff Haob24b4a72013-07-31 13:47:31 -07004500 if (is_primitive) {
Jeff Haofe1f7c82013-08-01 14:50:24 -07004501 VerifyPrimitivePut(component_type, insn_type, vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07004502 } else {
Jeff Haob24b4a72013-07-31 13:47:31 -07004503 if (!component_type.IsReferenceTypes()) {
4504 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
4505 << " source for aput-object";
4506 } else {
4507 // The instruction agrees with the type of array, confirm the value to be stored does too
4508 // Note: we use the instruction type (rather than the component type) for aput-object as
4509 // incompatible classes will be caught at runtime as an array store exception
Ian Rogers7b078e82014-09-10 14:44:24 -07004510 work_line_->VerifyRegisterType(this, vregA, insn_type);
Jeff Haob24b4a72013-07-31 13:47:31 -07004511 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004512 }
4513 }
4514 }
4515}
4516
Mathieu Chartierc7853442015-03-27 14:35:38 -07004517ArtField* MethodVerifier::GetStaticField(int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08004518 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
4519 // Check access to class
Ian Rogersd8f69b02014-09-10 21:43:52 +00004520 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Brian Carlstrom7934ac22013-07-26 10:54:15 -07004521 if (klass_type.IsConflict()) { // bad class
Ian Rogersad0b3a32012-04-16 14:50:24 -07004522 AppendToLastFailMessage(StringPrintf(" in attempt to access static field %d (%s) in %s",
4523 field_idx, dex_file_->GetFieldName(field_id),
4524 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers7b078e82014-09-10 14:44:24 -07004525 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08004526 }
Elliott Hughesb25c3f62012-03-26 16:35:06 -07004527 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004528 return nullptr; // Can't resolve Class so no more to do here, will do checking at runtime.
Ian Rogers90040192011-12-16 08:54:29 -08004529 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07004530 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
David Brazdilca3c8c32016-09-06 14:04:48 +01004531 ArtField* field = class_linker->ResolveFieldJLS(*dex_file_, field_idx, dex_cache_, class_loader_);
4532
4533 // Record result of the field resolution attempt.
4534 VerifierDeps::MaybeRecordFieldResolution(*dex_file_, field_idx, field);
4535
Ian Rogers7b078e82014-09-10 14:44:24 -07004536 if (field == nullptr) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07004537 VLOG(verifier) << "Unable to resolve static field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07004538 << dex_file_->GetFieldName(field_id) << ") in "
4539 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogers7b078e82014-09-10 14:44:24 -07004540 DCHECK(self_->IsExceptionPending());
4541 self_->ClearException();
4542 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004543 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
4544 field->GetAccessFlags())) {
David Sehr709b0702016-10-13 09:12:37 -07004545 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access static field " << field->PrettyField()
Ian Rogersad0b3a32012-04-16 14:50:24 -07004546 << " from " << GetDeclaringClass();
Ian Rogers7b078e82014-09-10 14:44:24 -07004547 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07004548 } else if (!field->IsStatic()) {
David Sehr709b0702016-10-13 09:12:37 -07004549 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << field->PrettyField() << " to be static";
Ian Rogers7b078e82014-09-10 14:44:24 -07004550 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07004551 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07004552 return field;
Ian Rogersd81871c2011-10-03 13:57:23 -07004553}
4554
Mathieu Chartierc7853442015-03-27 14:35:38 -07004555ArtField* MethodVerifier::GetInstanceField(const RegType& obj_type, int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08004556 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
Aart Bik31883642016-06-06 15:02:44 -07004557 // Check access to class.
Ian Rogersd8f69b02014-09-10 21:43:52 +00004558 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004559 if (klass_type.IsConflict()) {
4560 AppendToLastFailMessage(StringPrintf(" in attempt to access instance field %d (%s) in %s",
4561 field_idx, dex_file_->GetFieldName(field_id),
4562 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers7b078e82014-09-10 14:44:24 -07004563 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08004564 }
jeffhao8cd6dda2012-02-22 10:15:34 -08004565 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004566 return nullptr; // Can't resolve Class so no more to do here
Ian Rogers90040192011-12-16 08:54:29 -08004567 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07004568 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
David Brazdilca3c8c32016-09-06 14:04:48 +01004569 ArtField* field = class_linker->ResolveFieldJLS(*dex_file_, field_idx, dex_cache_, class_loader_);
4570
4571 // Record result of the field resolution attempt.
4572 VerifierDeps::MaybeRecordFieldResolution(*dex_file_, field_idx, field);
4573
Ian Rogers7b078e82014-09-10 14:44:24 -07004574 if (field == nullptr) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07004575 VLOG(verifier) << "Unable to resolve instance field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07004576 << dex_file_->GetFieldName(field_id) << ") in "
4577 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogers7b078e82014-09-10 14:44:24 -07004578 DCHECK(self_->IsExceptionPending());
4579 self_->ClearException();
4580 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07004581 } else if (obj_type.IsZero()) {
Aart Bik31883642016-06-06 15:02:44 -07004582 // Cannot infer and check type, however, access will cause null pointer exception.
4583 // Fall through into a few last soft failure checks below.
Stephen Kyle695c5982014-08-22 15:03:07 +01004584 } else if (!obj_type.IsReferenceTypes()) {
Aart Bik31883642016-06-06 15:02:44 -07004585 // Trying to read a field from something that isn't a reference.
Stephen Kyle695c5982014-08-22 15:03:07 +01004586 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance field access on object that has "
4587 << "non-reference type " << obj_type;
Ian Rogers7b078e82014-09-10 14:44:24 -07004588 return nullptr;
Ian Rogerse1758fe2012-04-19 11:31:15 -07004589 } else {
David Brazdil0d638bb2016-07-27 15:29:25 +01004590 std::string temp;
Mathieu Chartier3398c782016-09-30 10:27:43 -07004591 ObjPtr<mirror::Class> klass = field->GetDeclaringClass();
Ian Rogersd8f69b02014-09-10 21:43:52 +00004592 const RegType& field_klass =
Mathieu Chartier3398c782016-09-30 10:27:43 -07004593 FromClass(klass->GetDescriptor(&temp),
Mathieu Chartier1cc62e42016-10-03 18:01:28 -07004594 klass.Ptr(),
Mathieu Chartier3398c782016-09-30 10:27:43 -07004595 klass->CannotBeAssignedFromOtherTypes());
David Brazdil68b5c0b2016-01-19 14:25:29 +00004596 if (obj_type.IsUninitializedTypes()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07004597 // Field accesses through uninitialized references are only allowable for constructors where
David Brazdil68b5c0b2016-01-19 14:25:29 +00004598 // the field is declared in this class.
4599 // Note: this IsConstructor check is technically redundant, as UninitializedThis should only
4600 // appear in constructors.
4601 if (!obj_type.IsUninitializedThisReference() ||
4602 !IsConstructor() ||
4603 !field_klass.Equals(GetDeclaringClass())) {
David Sehr709b0702016-10-13 09:12:37 -07004604 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "cannot access instance field " << field->PrettyField()
David Brazdil68b5c0b2016-01-19 14:25:29 +00004605 << " of a not fully initialized object within the context"
David Sehr709b0702016-10-13 09:12:37 -07004606 << " of " << dex_file_->PrettyMethod(dex_method_idx_);
David Brazdil68b5c0b2016-01-19 14:25:29 +00004607 return nullptr;
4608 }
David Brazdilca3c8c32016-09-06 14:04:48 +01004609 } else if (!field_klass.IsAssignableFrom(obj_type, this)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07004610 // Trying to access C1.field1 using reference of type C2, which is neither C1 or a sub-class
4611 // of C1. For resolution to occur the declared class of the field must be compatible with
4612 // obj_type, we've discovered this wasn't so, so report the field didn't exist.
Andreas Gampe66596242016-04-14 10:55:04 -07004613 VerifyError type;
4614 bool is_aot = Runtime::Current()->IsAotCompiler();
4615 if (is_aot && (field_klass.IsUnresolvedTypes() || obj_type.IsUnresolvedTypes())) {
4616 // Compiler & unresolved types involved, retry at runtime.
4617 type = VerifyError::VERIFY_ERROR_NO_CLASS;
4618 } else {
Andreas Gampe8f4ade02016-04-15 10:09:16 -07004619 // Classes known (resolved; and thus assignability check is precise), or we are at runtime
4620 // and still missing classes. This is a hard failure.
Andreas Gampe66596242016-04-14 10:55:04 -07004621 type = VerifyError::VERIFY_ERROR_BAD_CLASS_HARD;
4622 }
David Sehr709b0702016-10-13 09:12:37 -07004623 Fail(type) << "cannot access instance field " << field->PrettyField()
Andreas Gampe66596242016-04-14 10:55:04 -07004624 << " from object of type " << obj_type;
Ian Rogers7b078e82014-09-10 14:44:24 -07004625 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004626 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004627 }
Aart Bik31883642016-06-06 15:02:44 -07004628
4629 // Few last soft failure checks.
4630 if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
4631 field->GetAccessFlags())) {
David Sehr709b0702016-10-13 09:12:37 -07004632 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access instance field " << field->PrettyField()
Aart Bik31883642016-06-06 15:02:44 -07004633 << " from " << GetDeclaringClass();
4634 return nullptr;
4635 } else if (field->IsStatic()) {
David Sehr709b0702016-10-13 09:12:37 -07004636 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << field->PrettyField()
Aart Bik31883642016-06-06 15:02:44 -07004637 << " to not be static";
4638 return nullptr;
4639 }
4640
4641 return field;
Ian Rogersd81871c2011-10-03 13:57:23 -07004642}
4643
Andreas Gampe896df402014-10-20 22:25:29 -07004644template <MethodVerifier::FieldAccessType kAccType>
4645void MethodVerifier::VerifyISFieldAccess(const Instruction* inst, const RegType& insn_type,
4646 bool is_primitive, bool is_static) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02004647 uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
Mathieu Chartierc7853442015-03-27 14:35:38 -07004648 ArtField* field;
Ian Rogersb94a27b2011-10-26 00:33:41 -07004649 if (is_static) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07004650 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07004651 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004652 const RegType& object_type = work_line_->GetRegisterType(this, inst->VRegB_22c());
David Brazdil68b5c0b2016-01-19 14:25:29 +00004653
4654 // One is not allowed to access fields on uninitialized references, except to write to
4655 // fields in the constructor (before calling another constructor).
4656 // GetInstanceField does an assignability check which will fail for uninitialized types.
4657 // We thus modify the type if the uninitialized reference is a "this" reference (this also
4658 // checks at the same time that we're verifying a constructor).
4659 bool should_adjust = (kAccType == FieldAccessType::kAccPut) &&
4660 object_type.IsUninitializedThisReference();
4661 const RegType& adjusted_type = should_adjust
4662 ? GetRegTypeCache()->FromUninitialized(object_type)
4663 : object_type;
4664 field = GetInstanceField(adjusted_type, field_idx);
Andreas Gampe896df402014-10-20 22:25:29 -07004665 if (UNLIKELY(have_pending_hard_failure_)) {
4666 return;
4667 }
Alex Light4a2c8fc2016-02-12 11:01:54 -08004668 if (should_adjust) {
4669 if (field == nullptr) {
4670 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "Might be accessing a superclass instance field prior "
4671 << "to the superclass being initialized in "
David Sehr709b0702016-10-13 09:12:37 -07004672 << dex_file_->PrettyMethod(dex_method_idx_);
Alex Light4a2c8fc2016-02-12 11:01:54 -08004673 } else if (field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
4674 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "cannot access superclass instance field "
David Sehr709b0702016-10-13 09:12:37 -07004675 << field->PrettyField() << " of a not fully initialized "
Alex Light4a2c8fc2016-02-12 11:01:54 -08004676 << "object within the context of "
David Sehr709b0702016-10-13 09:12:37 -07004677 << dex_file_->PrettyMethod(dex_method_idx_);
Alex Light4a2c8fc2016-02-12 11:01:54 -08004678 return;
4679 }
4680 }
Ian Rogersb94a27b2011-10-26 00:33:41 -07004681 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00004682 const RegType* field_type = nullptr;
Ian Rogers7b078e82014-09-10 14:44:24 -07004683 if (field != nullptr) {
Andreas Gampe896df402014-10-20 22:25:29 -07004684 if (kAccType == FieldAccessType::kAccPut) {
4685 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
David Sehr709b0702016-10-13 09:12:37 -07004686 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << field->PrettyField()
Andreas Gampe896df402014-10-20 22:25:29 -07004687 << " from other class " << GetDeclaringClass();
Aart Bikc2bc2652016-05-23 14:58:49 -07004688 // Keep hunting for possible hard fails.
Andreas Gampe896df402014-10-20 22:25:29 -07004689 }
4690 }
4691
Mathieu Chartier3398c782016-09-30 10:27:43 -07004692 ObjPtr<mirror::Class> field_type_class =
Mathieu Chartierc7853442015-03-27 14:35:38 -07004693 can_load_classes_ ? field->GetType<true>() : field->GetType<false>();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004694 if (field_type_class != nullptr) {
Mathieu Chartier3398c782016-09-30 10:27:43 -07004695 field_type = &FromClass(field->GetTypeDescriptor(),
Mathieu Chartier1cc62e42016-10-03 18:01:28 -07004696 field_type_class.Ptr(),
Andreas Gampef23f33d2015-06-23 14:18:17 -07004697 field_type_class->CannotBeAssignedFromOtherTypes());
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08004698 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004699 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
4700 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004701 }
Ian Rogers0d604842012-04-16 14:50:24 -07004702 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004703 if (field_type == nullptr) {
4704 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
4705 const char* descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
Mathieu Chartierbf99f772014-08-23 16:37:27 -07004706 field_type = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004707 }
Sebastien Hertz757b3042014-03-28 14:34:28 +01004708 DCHECK(field_type != nullptr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02004709 const uint32_t vregA = (is_static) ? inst->VRegA_21c() : inst->VRegA_22c();
Andreas Gampe896df402014-10-20 22:25:29 -07004710 static_assert(kAccType == FieldAccessType::kAccPut || kAccType == FieldAccessType::kAccGet,
4711 "Unexpected third access type");
4712 if (kAccType == FieldAccessType::kAccPut) {
4713 // sput or iput.
4714 if (is_primitive) {
4715 VerifyPrimitivePut(*field_type, insn_type, vregA);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004716 } else {
David Brazdilca3c8c32016-09-06 14:04:48 +01004717 if (!insn_type.IsAssignableFrom(*field_type, this)) {
Vladimir Marko414000e2015-06-23 17:45:21 +01004718 // If the field type is not a reference, this is a global failure rather than
4719 // a class change failure as the instructions and the descriptors for the type
4720 // should have been consistent within the same file at compile time.
4721 VerifyError error = field_type->IsReferenceTypes() ? VERIFY_ERROR_BAD_CLASS_SOFT
4722 : VERIFY_ERROR_BAD_CLASS_HARD;
David Sehr709b0702016-10-13 09:12:37 -07004723 Fail(error) << "expected field " << ArtField::PrettyField(field)
Vladimir Marko414000e2015-06-23 17:45:21 +01004724 << " to be compatible with type '" << insn_type
4725 << "' but found type '" << *field_type
4726 << "' in put-object";
Andreas Gampe896df402014-10-20 22:25:29 -07004727 return;
4728 }
4729 work_line_->VerifyRegisterType(this, vregA, *field_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004730 }
Andreas Gampe896df402014-10-20 22:25:29 -07004731 } else if (kAccType == FieldAccessType::kAccGet) {
4732 // sget or iget.
4733 if (is_primitive) {
4734 if (field_type->Equals(insn_type) ||
4735 (field_type->IsFloat() && insn_type.IsInteger()) ||
4736 (field_type->IsDouble() && insn_type.IsLong())) {
4737 // expected that read is of the correct primitive type or that int reads are reading
4738 // floats or long reads are reading doubles
4739 } else {
4740 // This is a global failure rather than a class change failure as the instructions and
4741 // the descriptors for the type should have been consistent within the same file at
4742 // compile time
David Sehr709b0702016-10-13 09:12:37 -07004743 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << ArtField::PrettyField(field)
Andreas Gampe896df402014-10-20 22:25:29 -07004744 << " to be of type '" << insn_type
4745 << "' but found type '" << *field_type << "' in get";
4746 return;
4747 }
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08004748 } else {
David Brazdilca3c8c32016-09-06 14:04:48 +01004749 if (!insn_type.IsAssignableFrom(*field_type, this)) {
Vladimir Marko414000e2015-06-23 17:45:21 +01004750 // If the field type is not a reference, this is a global failure rather than
4751 // a class change failure as the instructions and the descriptors for the type
4752 // should have been consistent within the same file at compile time.
4753 VerifyError error = field_type->IsReferenceTypes() ? VERIFY_ERROR_BAD_CLASS_SOFT
4754 : VERIFY_ERROR_BAD_CLASS_HARD;
David Sehr709b0702016-10-13 09:12:37 -07004755 Fail(error) << "expected field " << ArtField::PrettyField(field)
Vladimir Marko414000e2015-06-23 17:45:21 +01004756 << " to be compatible with type '" << insn_type
4757 << "' but found type '" << *field_type
4758 << "' in get-object";
Andreas Gampe890da292015-07-06 17:20:18 -07004759 if (error != VERIFY_ERROR_BAD_CLASS_HARD) {
Andreas Gampead238ce2015-08-24 21:13:08 -07004760 work_line_->SetRegisterType<LockOp::kClear>(this, vregA, reg_types_.Conflict());
Andreas Gampe890da292015-07-06 17:20:18 -07004761 }
Andreas Gampe896df402014-10-20 22:25:29 -07004762 return;
4763 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004764 }
Andreas Gampe896df402014-10-20 22:25:29 -07004765 if (!field_type->IsLowHalf()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07004766 work_line_->SetRegisterType<LockOp::kClear>(this, vregA, *field_type);
Andreas Gampe896df402014-10-20 22:25:29 -07004767 } else {
4768 work_line_->SetRegisterTypeWide(this, vregA, *field_type, field_type->HighHalf(&reg_types_));
4769 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07004770 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004771 LOG(FATAL) << "Unexpected case.";
Ian Rogersd81871c2011-10-03 13:57:23 -07004772 }
4773}
4774
Mathieu Chartierc7853442015-03-27 14:35:38 -07004775ArtField* MethodVerifier::GetQuickFieldAccess(const Instruction* inst,
Ian Rogers98379392014-02-24 16:53:16 -08004776 RegisterLine* reg_line) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08004777 DCHECK(IsInstructionIGetQuickOrIPutQuick(inst->Opcode())) << inst->Opcode();
Ian Rogers7b078e82014-09-10 14:44:24 -07004778 const RegType& object_type = reg_line->GetRegisterType(this, inst->VRegB_22c());
Ian Rogers9bc54402014-04-17 16:40:01 -07004779 if (!object_type.HasClass()) {
4780 VLOG(verifier) << "Failed to get mirror::Class* from '" << object_type << "'";
4781 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004782 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004783 uint32_t field_offset = static_cast<uint32_t>(inst->VRegC_22c());
Mathieu Chartierc7853442015-03-27 14:35:38 -07004784 ArtField* const f = ArtField::FindInstanceFieldWithOffset(object_type.GetClass(), field_offset);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08004785 DCHECK_EQ(f->GetOffset().Uint32Value(), field_offset);
Sebastien Hertz479fc1e2014-04-04 17:51:34 +02004786 if (f == nullptr) {
4787 VLOG(verifier) << "Failed to find instance field at offset '" << field_offset
David Sehr709b0702016-10-13 09:12:37 -07004788 << "' from '" << mirror::Class::PrettyDescriptor(object_type.GetClass()) << "'";
Sebastien Hertz479fc1e2014-04-04 17:51:34 +02004789 }
4790 return f;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004791}
4792
Andreas Gampe896df402014-10-20 22:25:29 -07004793template <MethodVerifier::FieldAccessType kAccType>
4794void MethodVerifier::VerifyQuickFieldAccess(const Instruction* inst, const RegType& insn_type,
4795 bool is_primitive) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07004796 DCHECK(Runtime::Current()->IsStarted() || verify_to_dump_);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004797
Mathieu Chartierc7853442015-03-27 14:35:38 -07004798 ArtField* field = GetQuickFieldAccess(inst, work_line_.get());
Ian Rogers7b078e82014-09-10 14:44:24 -07004799 if (field == nullptr) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004800 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field from " << inst->Name();
4801 return;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004802 }
Andreas Gampe896df402014-10-20 22:25:29 -07004803
4804 // For an IPUT_QUICK, we now test for final flag of the field.
4805 if (kAccType == FieldAccessType::kAccPut) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004806 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
David Sehr709b0702016-10-13 09:12:37 -07004807 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << field->PrettyField()
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004808 << " from other class " << GetDeclaringClass();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004809 return;
4810 }
4811 }
Andreas Gampe896df402014-10-20 22:25:29 -07004812
4813 // Get the field type.
4814 const RegType* field_type;
4815 {
Mathieu Chartier3398c782016-09-30 10:27:43 -07004816 ObjPtr<mirror::Class> field_type_class = can_load_classes_ ? field->GetType<true>() :
Mathieu Chartierc7853442015-03-27 14:35:38 -07004817 field->GetType<false>();
Andreas Gampe896df402014-10-20 22:25:29 -07004818
4819 if (field_type_class != nullptr) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07004820 field_type = &FromClass(field->GetTypeDescriptor(),
Mathieu Chartier1cc62e42016-10-03 18:01:28 -07004821 field_type_class.Ptr(),
Andreas Gampef23f33d2015-06-23 14:18:17 -07004822 field_type_class->CannotBeAssignedFromOtherTypes());
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004823 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004824 Thread* self = Thread::Current();
4825 DCHECK(!can_load_classes_ || self->IsExceptionPending());
4826 self->ClearException();
4827 field_type = &reg_types_.FromDescriptor(field->GetDeclaringClass()->GetClassLoader(),
Mathieu Chartierde40d472015-10-15 17:47:48 -07004828 field->GetTypeDescriptor(),
4829 false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004830 }
Andreas Gampe896df402014-10-20 22:25:29 -07004831 if (field_type == nullptr) {
4832 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field type from " << inst->Name();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004833 return;
4834 }
Andreas Gampe896df402014-10-20 22:25:29 -07004835 }
4836
4837 const uint32_t vregA = inst->VRegA_22c();
4838 static_assert(kAccType == FieldAccessType::kAccPut || kAccType == FieldAccessType::kAccGet,
4839 "Unexpected third access type");
4840 if (kAccType == FieldAccessType::kAccPut) {
4841 if (is_primitive) {
4842 // Primitive field assignability rules are weaker than regular assignability rules
4843 bool instruction_compatible;
4844 bool value_compatible;
4845 const RegType& value_type = work_line_->GetRegisterType(this, vregA);
4846 if (field_type->IsIntegralTypes()) {
4847 instruction_compatible = insn_type.IsIntegralTypes();
4848 value_compatible = value_type.IsIntegralTypes();
4849 } else if (field_type->IsFloat()) {
4850 instruction_compatible = insn_type.IsInteger(); // no [is]put-float, so expect [is]put-int
4851 value_compatible = value_type.IsFloatTypes();
4852 } else if (field_type->IsLong()) {
4853 instruction_compatible = insn_type.IsLong();
4854 value_compatible = value_type.IsLongTypes();
4855 } else if (field_type->IsDouble()) {
4856 instruction_compatible = insn_type.IsLong(); // no [is]put-double, so expect [is]put-long
4857 value_compatible = value_type.IsDoubleTypes();
4858 } else {
4859 instruction_compatible = false; // reference field with primitive store
4860 value_compatible = false; // unused
4861 }
4862 if (!instruction_compatible) {
4863 // This is a global failure rather than a class change failure as the instructions and
4864 // the descriptors for the type should have been consistent within the same file at
4865 // compile time
David Sehr709b0702016-10-13 09:12:37 -07004866 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << ArtField::PrettyField(field)
Andreas Gampe896df402014-10-20 22:25:29 -07004867 << " to be of type '" << insn_type
4868 << "' but found type '" << *field_type
4869 << "' in put";
4870 return;
4871 }
4872 if (!value_compatible) {
4873 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
4874 << " of type " << value_type
4875 << " but expected " << *field_type
David Sehr709b0702016-10-13 09:12:37 -07004876 << " for store to " << ArtField::PrettyField(field) << " in put";
Andreas Gampe896df402014-10-20 22:25:29 -07004877 return;
4878 }
4879 } else {
David Brazdilca3c8c32016-09-06 14:04:48 +01004880 if (!insn_type.IsAssignableFrom(*field_type, this)) {
David Sehr709b0702016-10-13 09:12:37 -07004881 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << ArtField::PrettyField(field)
Andreas Gampe896df402014-10-20 22:25:29 -07004882 << " to be compatible with type '" << insn_type
4883 << "' but found type '" << *field_type
4884 << "' in put-object";
4885 return;
4886 }
4887 work_line_->VerifyRegisterType(this, vregA, *field_type);
4888 }
4889 } else if (kAccType == FieldAccessType::kAccGet) {
4890 if (is_primitive) {
4891 if (field_type->Equals(insn_type) ||
4892 (field_type->IsFloat() && insn_type.IsIntegralTypes()) ||
4893 (field_type->IsDouble() && insn_type.IsLongTypes())) {
4894 // expected that read is of the correct primitive type or that int reads are reading
4895 // floats or long reads are reading doubles
4896 } else {
4897 // This is a global failure rather than a class change failure as the instructions and
4898 // the descriptors for the type should have been consistent within the same file at
4899 // compile time
David Sehr709b0702016-10-13 09:12:37 -07004900 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << ArtField::PrettyField(field)
Andreas Gampe896df402014-10-20 22:25:29 -07004901 << " to be of type '" << insn_type
4902 << "' but found type '" << *field_type << "' in Get";
4903 return;
4904 }
4905 } else {
David Brazdilca3c8c32016-09-06 14:04:48 +01004906 if (!insn_type.IsAssignableFrom(*field_type, this)) {
David Sehr709b0702016-10-13 09:12:37 -07004907 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << ArtField::PrettyField(field)
Andreas Gampe896df402014-10-20 22:25:29 -07004908 << " to be compatible with type '" << insn_type
4909 << "' but found type '" << *field_type
4910 << "' in get-object";
Andreas Gampead238ce2015-08-24 21:13:08 -07004911 work_line_->SetRegisterType<LockOp::kClear>(this, vregA, reg_types_.Conflict());
Andreas Gampe896df402014-10-20 22:25:29 -07004912 return;
4913 }
4914 }
4915 if (!field_type->IsLowHalf()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07004916 work_line_->SetRegisterType<LockOp::kClear>(this, vregA, *field_type);
Andreas Gampe896df402014-10-20 22:25:29 -07004917 } else {
4918 work_line_->SetRegisterTypeWide(this, vregA, *field_type, field_type->HighHalf(&reg_types_));
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004919 }
4920 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004921 LOG(FATAL) << "Unexpected case.";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004922 }
4923}
4924
Ian Rogers776ac1f2012-04-13 23:36:36 -07004925bool MethodVerifier::CheckNotMoveException(const uint16_t* insns, int insn_idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004926 if ((insns[insn_idx] & 0xff) == Instruction::MOVE_EXCEPTION) {
jeffhaod5347e02012-03-22 17:25:05 -07004927 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-exception";
Ian Rogersd81871c2011-10-03 13:57:23 -07004928 return false;
4929 }
4930 return true;
4931}
4932
Stephen Kyle9bc61992014-09-22 13:53:15 +01004933bool MethodVerifier::CheckNotMoveResult(const uint16_t* insns, int insn_idx) {
4934 if (((insns[insn_idx] & 0xff) >= Instruction::MOVE_RESULT) &&
4935 ((insns[insn_idx] & 0xff) <= Instruction::MOVE_RESULT_OBJECT)) {
4936 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-result*";
4937 return false;
4938 }
4939 return true;
4940}
4941
4942bool MethodVerifier::CheckNotMoveExceptionOrMoveResult(const uint16_t* insns, int insn_idx) {
4943 return (CheckNotMoveException(insns, insn_idx) && CheckNotMoveResult(insns, insn_idx));
4944}
4945
Ian Rogersebbdd872014-07-07 23:53:08 -07004946bool MethodVerifier::UpdateRegisters(uint32_t next_insn, RegisterLine* merge_line,
4947 bool update_merge_line) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004948 bool changed = true;
4949 RegisterLine* target_line = reg_table_.GetLine(next_insn);
Mathieu Chartierde40d472015-10-15 17:47:48 -07004950 if (!GetInstructionFlags(next_insn).IsVisitedOrChanged()) {
jeffhaobdb76512011-09-07 11:43:16 -07004951 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07004952 * We haven't processed this instruction before, and we haven't touched the registers here, so
4953 * there's nothing to "merge". Copy the registers over and mark it as changed. (This is the
4954 * only way a register can transition out of "unknown", so this is not just an optimization.)
jeffhaobdb76512011-09-07 11:43:16 -07004955 */
Andreas Gampea727e372015-08-25 09:22:37 -07004956 target_line->CopyFromLine(merge_line);
Mathieu Chartierde40d472015-10-15 17:47:48 -07004957 if (GetInstructionFlags(next_insn).IsReturn()) {
Jeff Haob24b4a72013-07-31 13:47:31 -07004958 // Verify that the monitor stack is empty on return.
Andreas Gampea727e372015-08-25 09:22:37 -07004959 merge_line->VerifyMonitorStackEmpty(this);
4960
Ian Rogersb8c78592013-07-25 23:52:52 +00004961 // For returns we only care about the operand to the return, all other registers are dead.
4962 // Initialize them as conflicts so they don't add to GC and deoptimization information.
4963 const Instruction* ret_inst = Instruction::At(code_item_->insns_ + next_insn);
Andreas Gampea727e372015-08-25 09:22:37 -07004964 AdjustReturnLine(this, ret_inst, target_line);
Aart Bik31883642016-06-06 15:02:44 -07004965 // Directly bail if a hard failure was found.
Aart Bikb0526322016-06-01 14:06:00 -07004966 if (have_pending_hard_failure_) {
4967 return false;
4968 }
Ian Rogersb8c78592013-07-25 23:52:52 +00004969 }
jeffhaobdb76512011-09-07 11:43:16 -07004970 } else {
Mathieu Chartier361e04a2016-02-16 14:06:35 -08004971 RegisterLineArenaUniquePtr copy;
Andreas Gampeec6e6c12015-11-05 20:39:56 -08004972 if (kDebugVerify) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07004973 copy.reset(RegisterLine::Create(target_line->NumRegs(), this));
Ian Rogers7b0c5b42012-02-16 15:29:07 -08004974 copy->CopyFromLine(target_line);
4975 }
Ian Rogers7b078e82014-09-10 14:44:24 -07004976 changed = target_line->MergeRegisters(this, merge_line);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004977 if (have_pending_hard_failure_) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004978 return false;
jeffhaobdb76512011-09-07 11:43:16 -07004979 }
Andreas Gampeec6e6c12015-11-05 20:39:56 -08004980 if (kDebugVerify && changed) {
Elliott Hughes398f64b2012-03-26 18:05:48 -07004981 LogVerifyInfo() << "Merging at [" << reinterpret_cast<void*>(work_insn_idx_) << "]"
Elliott Hughesc073b072012-05-24 19:29:17 -07004982 << " to [" << reinterpret_cast<void*>(next_insn) << "]: " << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07004983 << copy->Dump(this) << " MERGE\n"
4984 << merge_line->Dump(this) << " ==\n"
4985 << target_line->Dump(this) << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07004986 }
Ian Rogersebbdd872014-07-07 23:53:08 -07004987 if (update_merge_line && changed) {
4988 merge_line->CopyFromLine(target_line);
4989 }
jeffhaobdb76512011-09-07 11:43:16 -07004990 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004991 if (changed) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07004992 GetInstructionFlags(next_insn).SetChanged();
jeffhaobdb76512011-09-07 11:43:16 -07004993 }
4994 return true;
4995}
4996
Ian Rogers7b3ddd22013-02-21 15:19:52 -08004997InstructionFlags* MethodVerifier::CurrentInsnFlags() {
Mathieu Chartierde40d472015-10-15 17:47:48 -07004998 return &GetInstructionFlags(work_insn_idx_);
Ian Rogers776ac1f2012-04-13 23:36:36 -07004999}
5000
Ian Rogersd8f69b02014-09-10 21:43:52 +00005001const RegType& MethodVerifier::GetMethodReturnType() {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07005002 if (return_type_ == nullptr) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07005003 if (mirror_method_ != nullptr) {
Andreas Gampe542451c2016-07-26 09:02:02 -07005004 PointerSize pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
Vladimir Marko05792b92015-08-03 11:56:49 +01005005 mirror::Class* return_type_class = mirror_method_->GetReturnType(can_load_classes_,
5006 pointer_size);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07005007 if (return_type_class != nullptr) {
Andreas Gampef23f33d2015-06-23 14:18:17 -07005008 return_type_ = &FromClass(mirror_method_->GetReturnTypeDescriptor(),
5009 return_type_class,
5010 return_type_class->CannotBeAssignedFromOtherTypes());
Ian Rogersfc0e94b2013-09-23 23:51:32 -07005011 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07005012 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
5013 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07005014 }
5015 }
5016 if (return_type_ == nullptr) {
5017 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
5018 const DexFile::ProtoId& proto_id = dex_file_->GetMethodPrototype(method_id);
Andreas Gampea5b09a62016-11-17 15:21:22 -08005019 dex::TypeIndex return_type_idx = proto_id.return_type_idx_;
Ian Rogersfc0e94b2013-09-23 23:51:32 -07005020 const char* descriptor = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(return_type_idx));
Mathieu Chartierbf99f772014-08-23 16:37:27 -07005021 return_type_ = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07005022 }
5023 }
5024 return *return_type_;
Ian Rogersad0b3a32012-04-16 14:50:24 -07005025}
5026
Ian Rogersd8f69b02014-09-10 21:43:52 +00005027const RegType& MethodVerifier::GetDeclaringClass() {
Ian Rogers7b078e82014-09-10 14:44:24 -07005028 if (declaring_class_ == nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08005029 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
Brian Carlstrom93c33962013-07-26 10:37:43 -07005030 const char* descriptor
5031 = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(method_id.class_idx_));
Mathieu Chartiere401d142015-04-22 13:56:20 -07005032 if (mirror_method_ != nullptr) {
Ian Rogers637c65b2013-05-31 11:46:00 -07005033 mirror::Class* klass = mirror_method_->GetDeclaringClass();
Mathieu Chartierde40d472015-10-15 17:47:48 -07005034 declaring_class_ = &FromClass(descriptor, klass, klass->CannotBeAssignedFromOtherTypes());
Ian Rogers637c65b2013-05-31 11:46:00 -07005035 } else {
Mathieu Chartierbf99f772014-08-23 16:37:27 -07005036 declaring_class_ = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers637c65b2013-05-31 11:46:00 -07005037 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07005038 }
Ian Rogers637c65b2013-05-31 11:46:00 -07005039 return *declaring_class_;
Ian Rogersad0b3a32012-04-16 14:50:24 -07005040}
5041
Ian Rogers2bcb4a42012-11-08 10:39:18 -08005042std::vector<int32_t> MethodVerifier::DescribeVRegs(uint32_t dex_pc) {
5043 RegisterLine* line = reg_table_.GetLine(dex_pc);
Sebastien Hertzaa0c00c2014-03-14 17:58:54 +01005044 DCHECK(line != nullptr) << "No register line at DEX pc " << StringPrintf("0x%x", dex_pc);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08005045 std::vector<int32_t> result;
5046 for (size_t i = 0; i < line->NumRegs(); ++i) {
Ian Rogers7b078e82014-09-10 14:44:24 -07005047 const RegType& type = line->GetRegisterType(this, i);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08005048 if (type.IsConstant()) {
5049 result.push_back(type.IsPreciseConstant() ? kConstant : kImpreciseConstant);
Ian Rogers7b078e82014-09-10 14:44:24 -07005050 const ConstantType* const_val = down_cast<const ConstantType*>(&type);
5051 result.push_back(const_val->ConstantValue());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08005052 } else if (type.IsConstantLo()) {
5053 result.push_back(type.IsPreciseConstantLo() ? kConstant : kImpreciseConstant);
Ian Rogers7b078e82014-09-10 14:44:24 -07005054 const ConstantType* const_val = down_cast<const ConstantType*>(&type);
5055 result.push_back(const_val->ConstantValueLo());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08005056 } else if (type.IsConstantHi()) {
5057 result.push_back(type.IsPreciseConstantHi() ? kConstant : kImpreciseConstant);
Ian Rogers7b078e82014-09-10 14:44:24 -07005058 const ConstantType* const_val = down_cast<const ConstantType*>(&type);
5059 result.push_back(const_val->ConstantValueHi());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08005060 } else if (type.IsIntegralTypes()) {
5061 result.push_back(kIntVReg);
5062 result.push_back(0);
5063 } else if (type.IsFloat()) {
5064 result.push_back(kFloatVReg);
5065 result.push_back(0);
5066 } else if (type.IsLong()) {
5067 result.push_back(kLongLoVReg);
5068 result.push_back(0);
5069 result.push_back(kLongHiVReg);
5070 result.push_back(0);
5071 ++i;
5072 } else if (type.IsDouble()) {
5073 result.push_back(kDoubleLoVReg);
5074 result.push_back(0);
5075 result.push_back(kDoubleHiVReg);
5076 result.push_back(0);
5077 ++i;
5078 } else if (type.IsUndefined() || type.IsConflict() || type.IsHighHalf()) {
5079 result.push_back(kUndefined);
5080 result.push_back(0);
5081 } else {
Ian Rogers7b3ddd22013-02-21 15:19:52 -08005082 CHECK(type.IsNonZeroReferenceTypes());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08005083 result.push_back(kReferenceVReg);
5084 result.push_back(0);
5085 }
5086 }
5087 return result;
5088}
5089
Ian Rogersd8f69b02014-09-10 21:43:52 +00005090const RegType& MethodVerifier::DetermineCat1Constant(int32_t value, bool precise) {
Sebastien Hertz849600b2013-12-20 10:28:08 +01005091 if (precise) {
5092 // Precise constant type.
5093 return reg_types_.FromCat1Const(value, true);
5094 } else {
5095 // Imprecise constant type.
5096 if (value < -32768) {
5097 return reg_types_.IntConstant();
5098 } else if (value < -128) {
5099 return reg_types_.ShortConstant();
5100 } else if (value < 0) {
5101 return reg_types_.ByteConstant();
5102 } else if (value == 0) {
5103 return reg_types_.Zero();
5104 } else if (value == 1) {
5105 return reg_types_.One();
5106 } else if (value < 128) {
5107 return reg_types_.PosByteConstant();
5108 } else if (value < 32768) {
5109 return reg_types_.PosShortConstant();
5110 } else if (value < 65536) {
5111 return reg_types_.CharConstant();
5112 } else {
5113 return reg_types_.IntConstant();
5114 }
5115 }
5116}
5117
Elliott Hughes0a1038b2012-06-14 16:24:17 -07005118void MethodVerifier::Init() {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08005119 art::verifier::RegTypeCache::Init();
Brian Carlstrome7d856b2012-01-11 18:10:55 -08005120}
5121
Elliott Hughes0a1038b2012-06-14 16:24:17 -07005122void MethodVerifier::Shutdown() {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08005123 verifier::RegTypeCache::ShutDown();
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08005124}
jeffhaod1224c72012-02-29 13:43:08 -08005125
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -07005126void MethodVerifier::VisitStaticRoots(RootVisitor* visitor) {
5127 RegTypeCache::VisitStaticRoots(visitor);
Mathieu Chartier7c438b12014-09-12 17:01:24 -07005128}
5129
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -07005130void MethodVerifier::VisitRoots(RootVisitor* visitor, const RootInfo& root_info) {
5131 reg_types_.VisitRoots(visitor, root_info);
Mathieu Chartierc528dba2013-11-26 12:00:11 -08005132}
5133
Andreas Gampef23f33d2015-06-23 14:18:17 -07005134const RegType& MethodVerifier::FromClass(const char* descriptor,
5135 mirror::Class* klass,
5136 bool precise) {
5137 DCHECK(klass != nullptr);
5138 if (precise && !klass->IsInstantiable() && !klass->IsPrimitive()) {
5139 Fail(VerifyError::VERIFY_ERROR_NO_CLASS) << "Could not create precise reference for "
5140 << "non-instantiable klass " << descriptor;
5141 precise = false;
5142 }
5143 return reg_types_.FromClass(descriptor, klass, precise);
5144}
5145
Ian Rogersd81871c2011-10-03 13:57:23 -07005146} // namespace verifier
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07005147} // namespace art