blob: ed246118013c5b2efc3f18facedbd5b17afce9d7 [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
Ian Rogers0571d352011-11-03 19:51:38 -07001075 if (iterator.GetHandlerTypeIndex() != DexFile::kDexNoIndex16) {
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:
Ian Rogers29a26482014-05-02 15:27:29 -07001158 result = result && CheckNewInstance(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:
Ian Rogers29a26482014-05-02 15:27:29 -07001164 result = result && CheckTypeIndex(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:
Ian Rogers29a26482014-05-02 15:27:29 -07001178 result = result && CheckNewArray(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -07001179 break;
1180 case Instruction::kVerifyRegCType:
Ian Rogers29a26482014-05-02 15:27:29 -07001181 result = result && CheckTypeIndex(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
Ian Rogers7b078e82014-09-10 14:44:24 -07001273inline bool MethodVerifier::CheckNewInstance(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001274 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07001275 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
1276 << 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
Ian Rogers7b078e82014-09-10 14:44:24 -07001301inline bool MethodVerifier::CheckTypeIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001302 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07001303 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
1304 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001305 return false;
1306 }
1307 return true;
1308}
1309
Ian Rogers776ac1f2012-04-13 23:36:36 -07001310bool MethodVerifier::CheckNewArray(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001311 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07001312 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
1313 << 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.
1948static uint32_t GetFirstFinalInstanceFieldIndex(const DexFile& dex_file, uint16_t type_idx) {
1949 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
Ian Rogersd8f69b02014-09-10 21:43:52 +00002296 const RegType& res_type = ResolveClassAndCheckAccess(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);
2366 const uint32_t 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: {
Ian Rogersd8f69b02014-09-10 21:43:52 +00002436 const RegType& res_type = ResolveClassAndCheckAccess(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());
Ian Rogersd8f69b02014-09-10 21:43:52 +00002648 const RegType& cast_type = ResolveClassAndCheckAccess(instance_of_inst->VRegC_22c());
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002649
Ian Rogersebbdd872014-07-07 23:53:08 -07002650 if (!orig_type.Equals(cast_type) &&
2651 !cast_type.IsUnresolvedTypes() && !orig_type.IsUnresolvedTypes() &&
Andreas Gampe00633eb2014-07-17 16:13:35 -07002652 cast_type.HasClass() && // Could be conflict type, make sure it has a class.
Ian Rogersebbdd872014-07-07 23:53:08 -07002653 !cast_type.GetClass()->IsInterface() &&
2654 (orig_type.IsZero() ||
David Brazdilca3c8c32016-09-06 14:04:48 +01002655 orig_type.IsStrictlyAssignableFrom(
2656 cast_type.Merge(orig_type, &reg_types_, this), this))) {
Ian Rogersd0fbd852013-09-24 18:17:04 -07002657 RegisterLine* update_line = RegisterLine::Create(code_item_->registers_size_, this);
Ian Rogersfae370a2013-06-05 08:33:27 -07002658 if (inst->Opcode() == Instruction::IF_EQZ) {
Ian Rogers9b360392013-06-06 14:45:07 -07002659 fallthrough_line.reset(update_line);
Ian Rogersfae370a2013-06-05 08:33:27 -07002660 } else {
Ian Rogers9b360392013-06-06 14:45:07 -07002661 branch_line.reset(update_line);
2662 }
2663 update_line->CopyFromLine(work_line_.get());
Andreas Gampead238ce2015-08-24 21:13:08 -07002664 update_line->SetRegisterType<LockOp::kKeep>(this,
2665 instance_of_inst->VRegB_22c(),
2666 cast_type);
Mathieu Chartierde40d472015-10-15 17:47:48 -07002667 if (!GetInstructionFlags(instance_of_idx).IsBranchTarget() && 0 != instance_of_idx) {
Ian Rogers9b360392013-06-06 14:45:07 -07002668 // See if instance-of was preceded by a move-object operation, common due to the small
2669 // register encoding space of instance-of, and propagate type information to the source
2670 // of the move-object.
2671 uint32_t move_idx = instance_of_idx - 1;
Mathieu Chartierde40d472015-10-15 17:47:48 -07002672 while (0 != move_idx && !GetInstructionFlags(move_idx).IsOpcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07002673 move_idx--;
2674 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07002675 if (FailOrAbort(this, GetInstructionFlags(move_idx).IsOpcode(),
Andreas Gampe7c038102014-10-27 20:08:46 -07002676 "Unable to get previous instruction of if-eqz/if-nez for work index ",
2677 work_insn_idx_)) {
2678 break;
2679 }
Ian Rogers9b360392013-06-06 14:45:07 -07002680 const Instruction* move_inst = Instruction::At(code_item_->insns_ + move_idx);
2681 switch (move_inst->Opcode()) {
2682 case Instruction::MOVE_OBJECT:
2683 if (move_inst->VRegA_12x() == instance_of_inst->VRegB_22c()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07002684 update_line->SetRegisterType<LockOp::kKeep>(this,
2685 move_inst->VRegB_12x(),
2686 cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002687 }
2688 break;
2689 case Instruction::MOVE_OBJECT_FROM16:
2690 if (move_inst->VRegA_22x() == instance_of_inst->VRegB_22c()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07002691 update_line->SetRegisterType<LockOp::kKeep>(this,
2692 move_inst->VRegB_22x(),
2693 cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002694 }
2695 break;
2696 case Instruction::MOVE_OBJECT_16:
2697 if (move_inst->VRegA_32x() == instance_of_inst->VRegB_22c()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07002698 update_line->SetRegisterType<LockOp::kKeep>(this,
2699 move_inst->VRegB_32x(),
2700 cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002701 }
2702 break;
2703 default:
2704 break;
2705 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002706 }
2707 }
2708 }
2709
jeffhaobdb76512011-09-07 11:43:16 -07002710 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002711 }
jeffhaobdb76512011-09-07 11:43:16 -07002712 case Instruction::IF_LTZ:
2713 case Instruction::IF_GEZ:
2714 case Instruction::IF_GTZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002715 case Instruction::IF_LEZ: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002716 const RegType& reg_type = work_line_->GetRegisterType(this, inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002717 if (!reg_type.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002718 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
2719 << " unexpected as arg to if-ltz/if-gez/if-gtz/if-lez";
Ian Rogersd81871c2011-10-03 13:57:23 -07002720 }
jeffhaobdb76512011-09-07 11:43:16 -07002721 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002722 }
jeffhaobdb76512011-09-07 11:43:16 -07002723 case Instruction::AGET_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002724 VerifyAGet(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002725 break;
jeffhaobdb76512011-09-07 11:43:16 -07002726 case Instruction::AGET_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002727 VerifyAGet(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002728 break;
jeffhaobdb76512011-09-07 11:43:16 -07002729 case Instruction::AGET_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002730 VerifyAGet(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002731 break;
jeffhaobdb76512011-09-07 11:43:16 -07002732 case Instruction::AGET_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002733 VerifyAGet(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002734 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002735 case Instruction::AGET:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002736 VerifyAGet(inst, reg_types_.Integer(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002737 break;
jeffhaobdb76512011-09-07 11:43:16 -07002738 case Instruction::AGET_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002739 VerifyAGet(inst, reg_types_.LongLo(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002740 break;
2741 case Instruction::AGET_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002742 VerifyAGet(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07002743 break;
2744
Ian Rogersd81871c2011-10-03 13:57:23 -07002745 case Instruction::APUT_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002746 VerifyAPut(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002747 break;
2748 case Instruction::APUT_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002749 VerifyAPut(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002750 break;
2751 case Instruction::APUT_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002752 VerifyAPut(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002753 break;
2754 case Instruction::APUT_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002755 VerifyAPut(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002756 break;
2757 case Instruction::APUT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002758 VerifyAPut(inst, reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002759 break;
2760 case Instruction::APUT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002761 VerifyAPut(inst, reg_types_.LongLo(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002762 break;
2763 case Instruction::APUT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002764 VerifyAPut(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07002765 break;
2766
jeffhaobdb76512011-09-07 11:43:16 -07002767 case Instruction::IGET_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002768 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002769 break;
jeffhaobdb76512011-09-07 11:43:16 -07002770 case Instruction::IGET_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002771 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002772 break;
jeffhaobdb76512011-09-07 11:43:16 -07002773 case Instruction::IGET_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002774 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002775 break;
jeffhaobdb76512011-09-07 11:43:16 -07002776 case Instruction::IGET_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002777 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002778 break;
2779 case Instruction::IGET:
Andreas Gampe896df402014-10-20 22:25:29 -07002780 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002781 break;
2782 case Instruction::IGET_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002783 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002784 break;
2785 case Instruction::IGET_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002786 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false,
2787 false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002788 break;
jeffhaobdb76512011-09-07 11:43:16 -07002789
Ian Rogersd81871c2011-10-03 13:57:23 -07002790 case Instruction::IPUT_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002791 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002792 break;
2793 case Instruction::IPUT_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002794 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002795 break;
2796 case Instruction::IPUT_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002797 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002798 break;
2799 case Instruction::IPUT_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002800 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002801 break;
2802 case Instruction::IPUT:
Andreas Gampe896df402014-10-20 22:25:29 -07002803 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002804 break;
2805 case Instruction::IPUT_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002806 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002807 break;
jeffhaobdb76512011-09-07 11:43:16 -07002808 case Instruction::IPUT_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002809 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false,
2810 false);
jeffhaobdb76512011-09-07 11:43:16 -07002811 break;
2812
jeffhaobdb76512011-09-07 11:43:16 -07002813 case Instruction::SGET_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002814 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002815 break;
jeffhaobdb76512011-09-07 11:43:16 -07002816 case Instruction::SGET_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002817 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002818 break;
jeffhaobdb76512011-09-07 11:43:16 -07002819 case Instruction::SGET_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002820 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002821 break;
jeffhaobdb76512011-09-07 11:43:16 -07002822 case Instruction::SGET_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002823 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002824 break;
2825 case Instruction::SGET:
Andreas Gampe896df402014-10-20 22:25:29 -07002826 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002827 break;
2828 case Instruction::SGET_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002829 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002830 break;
2831 case Instruction::SGET_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002832 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false,
2833 true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002834 break;
2835
2836 case Instruction::SPUT_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002837 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002838 break;
2839 case Instruction::SPUT_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002840 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002841 break;
2842 case Instruction::SPUT_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002843 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002844 break;
2845 case Instruction::SPUT_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002846 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002847 break;
2848 case Instruction::SPUT:
Andreas Gampe896df402014-10-20 22:25:29 -07002849 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002850 break;
2851 case Instruction::SPUT_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002852 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002853 break;
2854 case Instruction::SPUT_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002855 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false,
2856 true);
jeffhaobdb76512011-09-07 11:43:16 -07002857 break;
2858
2859 case Instruction::INVOKE_VIRTUAL:
2860 case Instruction::INVOKE_VIRTUAL_RANGE:
2861 case Instruction::INVOKE_SUPER:
Ian Rogersd81871c2011-10-03 13:57:23 -07002862 case Instruction::INVOKE_SUPER_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002863 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE ||
2864 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002865 bool is_super = (inst->Opcode() == Instruction::INVOKE_SUPER ||
2866 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
Alex Light7268d472016-01-20 15:50:01 -08002867 MethodType type = is_super ? METHOD_SUPER : METHOD_VIRTUAL;
2868 ArtMethod* called_method = VerifyInvocationArgs(inst, type, is_range);
Ian Rogersd8f69b02014-09-10 21:43:52 +00002869 const RegType* return_type = nullptr;
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002870 if (called_method != nullptr) {
Andreas Gampe542451c2016-07-26 09:02:02 -07002871 PointerSize pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
Vladimir Marko05792b92015-08-03 11:56:49 +01002872 mirror::Class* return_type_class = called_method->GetReturnType(can_load_classes_,
2873 pointer_size);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002874 if (return_type_class != nullptr) {
Andreas Gampef23f33d2015-06-23 14:18:17 -07002875 return_type = &FromClass(called_method->GetReturnTypeDescriptor(),
2876 return_type_class,
2877 return_type_class->CannotBeAssignedFromOtherTypes());
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002878 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07002879 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
2880 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002881 }
2882 }
2883 if (return_type == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002884 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002885 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2886 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002887 const char* descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Mathieu Chartierbf99f772014-08-23 16:37:27 -07002888 return_type = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
jeffhaobdb76512011-09-07 11:43:16 -07002889 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002890 if (!return_type->IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002891 work_line_->SetResultRegisterType(this, *return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002892 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002893 work_line_->SetResultRegisterTypeWide(*return_type, return_type->HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002894 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002895 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002896 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002897 }
jeffhaobdb76512011-09-07 11:43:16 -07002898 case Instruction::INVOKE_DIRECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002899 case Instruction::INVOKE_DIRECT_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002900 bool is_range = (inst->Opcode() == Instruction::INVOKE_DIRECT_RANGE);
Alex Light7268d472016-01-20 15:50:01 -08002901 ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_DIRECT, is_range);
Ian Rogers46685432012-06-03 22:26:43 -07002902 const char* return_type_descriptor;
2903 bool is_constructor;
Ian Rogersd8f69b02014-09-10 21:43:52 +00002904 const RegType* return_type = nullptr;
Ian Rogers7b078e82014-09-10 14:44:24 -07002905 if (called_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002906 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogers46685432012-06-03 22:26:43 -07002907 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
Ian Rogersdfb325e2013-10-30 01:00:44 -07002908 is_constructor = strcmp("<init>", dex_file_->StringDataByIdx(method_id.name_idx_)) == 0;
Ian Rogers46685432012-06-03 22:26:43 -07002909 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
2910 return_type_descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2911 } else {
2912 is_constructor = called_method->IsConstructor();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002913 return_type_descriptor = called_method->GetReturnTypeDescriptor();
Andreas Gampe542451c2016-07-26 09:02:02 -07002914 PointerSize pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
Vladimir Marko05792b92015-08-03 11:56:49 +01002915 mirror::Class* return_type_class = called_method->GetReturnType(can_load_classes_,
2916 pointer_size);
Ian Rogers1ff3c982014-08-12 02:30:58 -07002917 if (return_type_class != nullptr) {
Andreas Gampef23f33d2015-06-23 14:18:17 -07002918 return_type = &FromClass(return_type_descriptor,
2919 return_type_class,
2920 return_type_class->CannotBeAssignedFromOtherTypes());
Ian Rogers1ff3c982014-08-12 02:30:58 -07002921 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07002922 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
2923 self_->ClearException();
Ian Rogers1ff3c982014-08-12 02:30:58 -07002924 }
Ian Rogers46685432012-06-03 22:26:43 -07002925 }
2926 if (is_constructor) {
jeffhaobdb76512011-09-07 11:43:16 -07002927 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002928 * Some additional checks when calling a constructor. We know from the invocation arg check
2929 * that the "this" argument is an instance of called_method->klass. Now we further restrict
2930 * that to require that called_method->klass is the same as this->klass or this->super,
2931 * allowing the latter only if the "this" argument is the same as the "this" argument to
2932 * this method (which implies that we're in a constructor ourselves).
jeffhaobdb76512011-09-07 11:43:16 -07002933 */
Ian Rogers7b078e82014-09-10 14:44:24 -07002934 const RegType& this_type = work_line_->GetInvocationThis(this, inst, is_range);
jeffhaob57e9522012-04-26 18:08:21 -07002935 if (this_type.IsConflict()) // failure.
2936 break;
jeffhaobdb76512011-09-07 11:43:16 -07002937
jeffhaob57e9522012-04-26 18:08:21 -07002938 /* no null refs allowed (?) */
2939 if (this_type.IsZero()) {
2940 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unable to initialize null ref";
2941 break;
jeffhao2a8a90e2011-09-26 14:25:31 -07002942 }
jeffhaob57e9522012-04-26 18:08:21 -07002943
2944 /* must be in same class or in superclass */
Ian Rogersd8f69b02014-09-10 21:43:52 +00002945 // const RegType& this_super_klass = this_type.GetSuperClass(&reg_types_);
Ian Rogers46685432012-06-03 22:26:43 -07002946 // TODO: re-enable constructor type verification
2947 // if (this_super_klass.IsConflict()) {
jeffhaob57e9522012-04-26 18:08:21 -07002948 // Unknown super class, fail so we re-check at runtime.
Ian Rogers46685432012-06-03 22:26:43 -07002949 // Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "super class unknown for '" << this_type << "'";
2950 // break;
2951 // }
jeffhaob57e9522012-04-26 18:08:21 -07002952
2953 /* arg must be an uninitialized reference */
2954 if (!this_type.IsUninitializedTypes()) {
2955 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Expected initialization on uninitialized reference "
2956 << this_type;
2957 break;
2958 }
2959
2960 /*
2961 * Replace the uninitialized reference with an initialized one. We need to do this for all
2962 * registers that have the same object instance in them, not just the "this" register.
2963 */
Nicolas Geoffray98e6ce42016-02-16 18:42:15 +00002964 work_line_->MarkRefsAsInitialized(this, this_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002965 }
Ian Rogers1ff3c982014-08-12 02:30:58 -07002966 if (return_type == nullptr) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07002967 return_type = &reg_types_.FromDescriptor(GetClassLoader(), return_type_descriptor, false);
Ian Rogers1ff3c982014-08-12 02:30:58 -07002968 }
2969 if (!return_type->IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002970 work_line_->SetResultRegisterType(this, *return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002971 } else {
Ian Rogers1ff3c982014-08-12 02:30:58 -07002972 work_line_->SetResultRegisterTypeWide(*return_type, return_type->HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002973 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002974 just_set_result = true;
2975 break;
2976 }
2977 case Instruction::INVOKE_STATIC:
2978 case Instruction::INVOKE_STATIC_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002979 bool is_range = (inst->Opcode() == Instruction::INVOKE_STATIC_RANGE);
Alex Light7268d472016-01-20 15:50:01 -08002980 ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_STATIC, is_range);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002981 const char* descriptor;
Ian Rogers7b078e82014-09-10 14:44:24 -07002982 if (called_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002983 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002984 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2985 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Mathieu Chartier590fee92013-09-13 13:46:47 -07002986 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002987 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002988 descriptor = called_method->GetReturnTypeDescriptor();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002989 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00002990 const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002991 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002992 work_line_->SetResultRegisterType(this, return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002993 } else {
2994 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2995 }
jeffhaobdb76512011-09-07 11:43:16 -07002996 just_set_result = true;
2997 }
2998 break;
jeffhaobdb76512011-09-07 11:43:16 -07002999 case Instruction::INVOKE_INTERFACE:
Ian Rogersd81871c2011-10-03 13:57:23 -07003000 case Instruction::INVOKE_INTERFACE_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003001 bool is_range = (inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE);
Alex Light7268d472016-01-20 15:50:01 -08003002 ArtMethod* abs_method = VerifyInvocationArgs(inst, METHOD_INTERFACE, is_range);
Ian Rogers7b078e82014-09-10 14:44:24 -07003003 if (abs_method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003004 mirror::Class* called_interface = abs_method->GetDeclaringClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07003005 if (!called_interface->IsInterface() && !called_interface->IsObjectClass()) {
3006 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected interface class in invoke-interface '"
David Sehr709b0702016-10-13 09:12:37 -07003007 << abs_method->PrettyMethod() << "'";
Ian Rogersad0b3a32012-04-16 14:50:24 -07003008 break;
Ian Rogers28ad40d2011-10-27 15:19:26 -07003009 }
Ian Rogers0d604842012-04-16 14:50:24 -07003010 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003011 /* Get the type of the "this" arg, which should either be a sub-interface of called
3012 * interface or Object (see comments in RegType::JoinClass).
3013 */
Ian Rogers7b078e82014-09-10 14:44:24 -07003014 const RegType& this_type = work_line_->GetInvocationThis(this, inst, is_range);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003015 if (this_type.IsZero()) {
3016 /* null pointer always passes (and always fails at runtime) */
3017 } else {
3018 if (this_type.IsUninitializedTypes()) {
3019 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface call on uninitialized object "
3020 << this_type;
3021 break;
3022 }
3023 // In the past we have tried to assert that "called_interface" is assignable
3024 // from "this_type.GetClass()", however, as we do an imprecise Join
3025 // (RegType::JoinClass) we don't have full information on what interfaces are
3026 // implemented by "this_type". For example, two classes may implement the same
3027 // interfaces and have a common parent that doesn't implement the interface. The
3028 // join will set "this_type" to the parent class and a test that this implements
3029 // the interface will incorrectly fail.
3030 }
3031 /*
3032 * We don't have an object instance, so we can't find the concrete method. However, all of
3033 * the type information is in the abstract method, so we're good.
3034 */
3035 const char* descriptor;
Ian Rogers7b078e82014-09-10 14:44:24 -07003036 if (abs_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003037 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07003038 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
3039 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Mathieu Chartierde40d472015-10-15 17:47:48 -07003040 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003041 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003042 descriptor = abs_method->GetReturnTypeDescriptor();
Ian Rogersad0b3a32012-04-16 14:50:24 -07003043 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00003044 const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003045 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003046 work_line_->SetResultRegisterType(this, return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003047 } else {
3048 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
3049 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003050 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07003051 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07003052 }
Narayan Kamath9823e782016-08-03 12:46:58 +01003053 case Instruction::INVOKE_POLYMORPHIC:
3054 case Instruction::INVOKE_POLYMORPHIC_RANGE: {
3055 Fail(VERIFY_ERROR_FORCE_INTERPRETER)
3056 << "instruction is not supported by verifier; skipping verification";
3057 have_pending_experimental_failure_ = true;
3058 return false;
3059 }
jeffhaobdb76512011-09-07 11:43:16 -07003060 case Instruction::NEG_INT:
3061 case Instruction::NOT_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003062 work_line_->CheckUnaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003063 break;
3064 case Instruction::NEG_LONG:
3065 case Instruction::NOT_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07003066 work_line_->CheckUnaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003067 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07003068 break;
3069 case Instruction::NEG_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003070 work_line_->CheckUnaryOp(this, inst, reg_types_.Float(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07003071 break;
3072 case Instruction::NEG_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07003073 work_line_->CheckUnaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003074 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07003075 break;
3076 case Instruction::INT_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07003077 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003078 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003079 break;
3080 case Instruction::INT_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003081 work_line_->CheckUnaryOp(this, inst, reg_types_.Float(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003082 break;
3083 case Instruction::INT_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07003084 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003085 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003086 break;
3087 case Instruction::LONG_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003088 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003089 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07003090 break;
3091 case Instruction::LONG_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003092 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003093 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07003094 break;
3095 case Instruction::LONG_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07003096 work_line_->CheckUnaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003097 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07003098 break;
3099 case Instruction::FLOAT_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003100 work_line_->CheckUnaryOp(this, inst, reg_types_.Integer(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07003101 break;
3102 case Instruction::FLOAT_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07003103 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003104 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07003105 break;
3106 case Instruction::FLOAT_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07003107 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003108 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07003109 break;
3110 case Instruction::DOUBLE_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003111 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003112 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07003113 break;
3114 case Instruction::DOUBLE_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07003115 work_line_->CheckUnaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003116 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07003117 break;
3118 case Instruction::DOUBLE_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003119 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003120 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07003121 break;
3122 case Instruction::INT_TO_BYTE:
Ian Rogers7b078e82014-09-10 14:44:24 -07003123 work_line_->CheckUnaryOp(this, inst, reg_types_.Byte(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003124 break;
3125 case Instruction::INT_TO_CHAR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003126 work_line_->CheckUnaryOp(this, inst, reg_types_.Char(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003127 break;
3128 case Instruction::INT_TO_SHORT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003129 work_line_->CheckUnaryOp(this, inst, reg_types_.Short(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003130 break;
3131
3132 case Instruction::ADD_INT:
3133 case Instruction::SUB_INT:
3134 case Instruction::MUL_INT:
3135 case Instruction::REM_INT:
3136 case Instruction::DIV_INT:
3137 case Instruction::SHL_INT:
3138 case Instruction::SHR_INT:
3139 case Instruction::USHR_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003140 work_line_->CheckBinaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003141 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07003142 break;
3143 case Instruction::AND_INT:
3144 case Instruction::OR_INT:
3145 case Instruction::XOR_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003146 work_line_->CheckBinaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003147 reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07003148 break;
3149 case Instruction::ADD_LONG:
3150 case Instruction::SUB_LONG:
3151 case Instruction::MUL_LONG:
3152 case Instruction::DIV_LONG:
3153 case Instruction::REM_LONG:
3154 case Instruction::AND_LONG:
3155 case Instruction::OR_LONG:
3156 case Instruction::XOR_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07003157 work_line_->CheckBinaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003158 reg_types_.LongLo(), reg_types_.LongHi(),
3159 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07003160 break;
3161 case Instruction::SHL_LONG:
3162 case Instruction::SHR_LONG:
3163 case Instruction::USHR_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07003164 /* shift distance is Int, making these different from other binary operations */
Ian Rogers7b078e82014-09-10 14:44:24 -07003165 work_line_->CheckBinaryOpWideShift(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003166 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003167 break;
3168 case Instruction::ADD_FLOAT:
3169 case Instruction::SUB_FLOAT:
3170 case Instruction::MUL_FLOAT:
3171 case Instruction::DIV_FLOAT:
3172 case Instruction::REM_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003173 work_line_->CheckBinaryOp(this, inst, reg_types_.Float(), reg_types_.Float(),
3174 reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07003175 break;
3176 case Instruction::ADD_DOUBLE:
3177 case Instruction::SUB_DOUBLE:
3178 case Instruction::MUL_DOUBLE:
3179 case Instruction::DIV_DOUBLE:
3180 case Instruction::REM_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07003181 work_line_->CheckBinaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003182 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
3183 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07003184 break;
3185 case Instruction::ADD_INT_2ADDR:
3186 case Instruction::SUB_INT_2ADDR:
3187 case Instruction::MUL_INT_2ADDR:
3188 case Instruction::REM_INT_2ADDR:
3189 case Instruction::SHL_INT_2ADDR:
3190 case Instruction::SHR_INT_2ADDR:
3191 case Instruction::USHR_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003192 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
3193 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07003194 break;
3195 case Instruction::AND_INT_2ADDR:
3196 case Instruction::OR_INT_2ADDR:
3197 case Instruction::XOR_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003198 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
3199 reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07003200 break;
3201 case Instruction::DIV_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003202 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
3203 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07003204 break;
3205 case Instruction::ADD_LONG_2ADDR:
3206 case Instruction::SUB_LONG_2ADDR:
3207 case Instruction::MUL_LONG_2ADDR:
3208 case Instruction::DIV_LONG_2ADDR:
3209 case Instruction::REM_LONG_2ADDR:
3210 case Instruction::AND_LONG_2ADDR:
3211 case Instruction::OR_LONG_2ADDR:
3212 case Instruction::XOR_LONG_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003213 work_line_->CheckBinaryOp2addrWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003214 reg_types_.LongLo(), reg_types_.LongHi(),
3215 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07003216 break;
3217 case Instruction::SHL_LONG_2ADDR:
3218 case Instruction::SHR_LONG_2ADDR:
3219 case Instruction::USHR_LONG_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003220 work_line_->CheckBinaryOp2addrWideShift(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003221 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003222 break;
3223 case Instruction::ADD_FLOAT_2ADDR:
3224 case Instruction::SUB_FLOAT_2ADDR:
3225 case Instruction::MUL_FLOAT_2ADDR:
3226 case Instruction::DIV_FLOAT_2ADDR:
3227 case Instruction::REM_FLOAT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003228 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Float(), reg_types_.Float(),
3229 reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07003230 break;
3231 case Instruction::ADD_DOUBLE_2ADDR:
3232 case Instruction::SUB_DOUBLE_2ADDR:
3233 case Instruction::MUL_DOUBLE_2ADDR:
3234 case Instruction::DIV_DOUBLE_2ADDR:
3235 case Instruction::REM_DOUBLE_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003236 work_line_->CheckBinaryOp2addrWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003237 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
3238 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07003239 break;
3240 case Instruction::ADD_INT_LIT16:
Ian Rogersf72a11d2014-10-30 15:41:08 -07003241 case Instruction::RSUB_INT_LIT16:
jeffhaobdb76512011-09-07 11:43:16 -07003242 case Instruction::MUL_INT_LIT16:
3243 case Instruction::DIV_INT_LIT16:
3244 case Instruction::REM_INT_LIT16:
Ian Rogers7b078e82014-09-10 14:44:24 -07003245 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), false,
3246 true);
jeffhaobdb76512011-09-07 11:43:16 -07003247 break;
3248 case Instruction::AND_INT_LIT16:
3249 case Instruction::OR_INT_LIT16:
3250 case Instruction::XOR_INT_LIT16:
Ian Rogers7b078e82014-09-10 14:44:24 -07003251 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), true,
3252 true);
jeffhaobdb76512011-09-07 11:43:16 -07003253 break;
3254 case Instruction::ADD_INT_LIT8:
3255 case Instruction::RSUB_INT_LIT8:
3256 case Instruction::MUL_INT_LIT8:
3257 case Instruction::DIV_INT_LIT8:
3258 case Instruction::REM_INT_LIT8:
3259 case Instruction::SHL_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07003260 case Instruction::SHR_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07003261 case Instruction::USHR_INT_LIT8:
Ian Rogers7b078e82014-09-10 14:44:24 -07003262 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), false,
3263 false);
jeffhaobdb76512011-09-07 11:43:16 -07003264 break;
3265 case Instruction::AND_INT_LIT8:
3266 case Instruction::OR_INT_LIT8:
3267 case Instruction::XOR_INT_LIT8:
Ian Rogers7b078e82014-09-10 14:44:24 -07003268 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), true,
3269 false);
jeffhaobdb76512011-09-07 11:43:16 -07003270 break;
3271
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003272 // Special instructions.
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07003273 case Instruction::RETURN_VOID_NO_BARRIER:
3274 if (IsConstructor() && !IsStatic()) {
3275 auto& declaring_class = GetDeclaringClass();
Andreas Gampe68df3202015-06-22 11:35:46 -07003276 if (declaring_class.IsUnresolvedReference()) {
3277 // We must iterate over the fields, even if we cannot use mirror classes to do so. Do it
3278 // manually over the underlying dex file.
3279 uint32_t first_index = GetFirstFinalInstanceFieldIndex(*dex_file_,
3280 dex_file_->GetMethodId(dex_method_idx_).class_idx_);
3281 if (first_index != DexFile::kDexNoIndex) {
3282 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void-no-barrier not expected for field "
3283 << first_index;
3284 }
3285 break;
3286 }
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07003287 auto* klass = declaring_class.GetClass();
3288 for (uint32_t i = 0, num_fields = klass->NumInstanceFields(); i < num_fields; ++i) {
3289 if (klass->GetInstanceField(i)->IsFinal()) {
Mathieu Chartiere86deef2015-03-19 13:43:37 -07003290 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void-no-barrier not expected for "
David Sehr709b0702016-10-13 09:12:37 -07003291 << klass->GetInstanceField(i)->PrettyField();
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07003292 break;
3293 }
3294 }
Sebastien Hertzcc10e0e2013-06-28 14:24:48 +02003295 }
Andreas Gampeb2917962015-07-31 13:36:10 -07003296 // Handle this like a RETURN_VOID now. Code is duplicated to separate standard from
3297 // quickened opcodes (otherwise this could be a fall-through).
3298 if (!IsConstructor()) {
3299 if (!GetMethodReturnType().IsConflict()) {
3300 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void not expected";
3301 }
3302 }
Sebastien Hertzcc10e0e2013-06-28 14:24:48 +02003303 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003304 // Note: the following instructions encode offsets derived from class linking.
Neil Fuller0e844392016-09-08 13:43:31 +01003305 // As such they use Class*/Field*/Executable* as these offsets only have
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003306 // meaning if the class linking and resolution were successful.
3307 case Instruction::IGET_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003308 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003309 break;
3310 case Instruction::IGET_WIDE_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003311 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003312 break;
3313 case Instruction::IGET_OBJECT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003314 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003315 break;
Mathieu Chartierffc605c2014-12-10 10:35:44 -08003316 case Instruction::IGET_BOOLEAN_QUICK:
3317 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true);
3318 break;
3319 case Instruction::IGET_BYTE_QUICK:
3320 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true);
3321 break;
3322 case Instruction::IGET_CHAR_QUICK:
3323 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true);
3324 break;
3325 case Instruction::IGET_SHORT_QUICK:
3326 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true);
3327 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003328 case Instruction::IPUT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003329 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003330 break;
Fred Shih37f05ef2014-07-16 18:38:08 -07003331 case Instruction::IPUT_BOOLEAN_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003332 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07003333 break;
3334 case Instruction::IPUT_BYTE_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003335 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07003336 break;
3337 case Instruction::IPUT_CHAR_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003338 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07003339 break;
3340 case Instruction::IPUT_SHORT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003341 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07003342 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003343 case Instruction::IPUT_WIDE_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003344 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003345 break;
3346 case Instruction::IPUT_OBJECT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003347 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003348 break;
3349 case Instruction::INVOKE_VIRTUAL_QUICK:
3350 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: {
3351 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
Mathieu Chartiere401d142015-04-22 13:56:20 -07003352 ArtMethod* called_method = VerifyInvokeVirtualQuickArgs(inst, is_range);
Ian Rogers7b078e82014-09-10 14:44:24 -07003353 if (called_method != nullptr) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003354 const char* descriptor = called_method->GetReturnTypeDescriptor();
Ian Rogersd8f69b02014-09-10 21:43:52 +00003355 const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003356 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003357 work_line_->SetResultRegisterType(this, return_type);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003358 } else {
3359 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
3360 }
3361 just_set_result = true;
3362 }
3363 break;
3364 }
3365
Ian Rogersd81871c2011-10-03 13:57:23 -07003366 /* These should never appear during verification. */
Mathieu Chartierffc605c2014-12-10 10:35:44 -08003367 case Instruction::UNUSED_3E ... Instruction::UNUSED_43:
Narayan Kamath8ec3bd22016-08-03 12:46:23 +01003368 case Instruction::UNUSED_F3 ... Instruction::UNUSED_F9:
3369 case Instruction::UNUSED_FC ... Instruction::UNUSED_FF:
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003370 case Instruction::UNUSED_79:
3371 case Instruction::UNUSED_7A:
jeffhaod5347e02012-03-22 17:25:05 -07003372 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Unexpected opcode " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07003373 break;
3374
3375 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003376 * DO NOT add a "default" clause here. Without it the compiler will
jeffhaobdb76512011-09-07 11:43:16 -07003377 * complain if an instruction is missing (which is desirable).
3378 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003379 } // end - switch (dec_insn.opcode)
jeffhaobdb76512011-09-07 11:43:16 -07003380
Ian Rogersad0b3a32012-04-16 14:50:24 -07003381 if (have_pending_hard_failure_) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08003382 if (Runtime::Current()->IsAotCompiler()) {
3383 /* When AOT compiling, check that the last failure is a hard failure */
Andreas Gampeb588f4c2015-05-26 13:35:39 -07003384 if (failures_[failures_.size() - 1] != VERIFY_ERROR_BAD_CLASS_HARD) {
3385 LOG(ERROR) << "Pending failures:";
3386 for (auto& error : failures_) {
3387 LOG(ERROR) << error;
3388 }
3389 for (auto& error_msg : failure_messages_) {
3390 LOG(ERROR) << error_msg->str();
3391 }
3392 LOG(FATAL) << "Pending hard failure, but last failure not hard.";
3393 }
Ian Rogerse1758fe2012-04-19 11:31:15 -07003394 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003395 /* immediate failure, reject class */
3396 info_messages_ << "Rejecting opcode " << inst->DumpString(dex_file_);
3397 return false;
jeffhaofaf459e2012-08-31 15:32:47 -07003398 } else if (have_pending_runtime_throw_failure_) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07003399 /* checking interpreter will throw, mark following code as unreachable */
jeffhaofaf459e2012-08-31 15:32:47 -07003400 opcode_flags = Instruction::kThrow;
Andreas Gampea727e372015-08-25 09:22:37 -07003401 // Note: the flag must be reset as it is only global to decouple Fail and is semantically per
3402 // instruction. However, RETURN checking may throw LOCKING errors, so we clear at the
3403 // very end.
jeffhaobdb76512011-09-07 11:43:16 -07003404 }
jeffhaobdb76512011-09-07 11:43:16 -07003405 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003406 * If we didn't just set the result register, clear it out. This ensures that you can only use
3407 * "move-result" immediately after the result is set. (We could check this statically, but it's
3408 * not expensive and it makes our debugging output cleaner.)
jeffhaobdb76512011-09-07 11:43:16 -07003409 */
3410 if (!just_set_result) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003411 work_line_->SetResultTypeToUnknown(this);
jeffhaobdb76512011-09-07 11:43:16 -07003412 }
3413
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003414
jeffhaobdb76512011-09-07 11:43:16 -07003415
3416 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003417 * Handle "branch". Tag the branch target.
jeffhaobdb76512011-09-07 11:43:16 -07003418 *
3419 * NOTE: instructions like Instruction::EQZ provide information about the
jeffhaod1f0fde2011-09-08 17:25:33 -07003420 * state of the register when the branch is taken or not taken. For example,
jeffhaobdb76512011-09-07 11:43:16 -07003421 * somebody could get a reference field, check it for zero, and if the
3422 * branch is taken immediately store that register in a boolean field
jeffhaod1f0fde2011-09-08 17:25:33 -07003423 * since the value is known to be zero. We do not currently account for
jeffhaobdb76512011-09-07 11:43:16 -07003424 * that, and will reject the code.
3425 *
3426 * TODO: avoid re-fetching the branch target
3427 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003428 if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07003429 bool isConditional, selfOkay;
Ian Rogersd81871c2011-10-03 13:57:23 -07003430 if (!GetBranchOffset(work_insn_idx_, &branch_target, &isConditional, &selfOkay)) {
jeffhaobdb76512011-09-07 11:43:16 -07003431 /* should never happen after static verification */
jeffhaod5347e02012-03-22 17:25:05 -07003432 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad branch";
jeffhaobdb76512011-09-07 11:43:16 -07003433 return false;
3434 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08003435 DCHECK_EQ(isConditional, (opcode_flags & Instruction::kContinue) != 0);
Stephen Kyle9bc61992014-09-22 13:53:15 +01003436 if (!CheckNotMoveExceptionOrMoveResult(code_item_->insns_, work_insn_idx_ + branch_target)) {
jeffhaobdb76512011-09-07 11:43:16 -07003437 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07003438 }
jeffhaobdb76512011-09-07 11:43:16 -07003439 /* update branch target, set "changed" if appropriate */
Mathieu Chartierde40d472015-10-15 17:47:48 -07003440 if (nullptr != branch_line) {
Ian Rogersebbdd872014-07-07 23:53:08 -07003441 if (!UpdateRegisters(work_insn_idx_ + branch_target, branch_line.get(), false)) {
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003442 return false;
3443 }
3444 } else {
Ian Rogersebbdd872014-07-07 23:53:08 -07003445 if (!UpdateRegisters(work_insn_idx_ + branch_target, work_line_.get(), false)) {
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003446 return false;
3447 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003448 }
jeffhaobdb76512011-09-07 11:43:16 -07003449 }
3450
3451 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003452 * Handle "switch". Tag all possible branch targets.
jeffhaobdb76512011-09-07 11:43:16 -07003453 *
3454 * We've already verified that the table is structurally sound, so we
3455 * just need to walk through and tag the targets.
3456 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003457 if ((opcode_flags & Instruction::kSwitch) != 0) {
Andreas Gampe53de99c2015-08-17 13:43:55 -07003458 int offset_to_switch = insns[1] | (static_cast<int32_t>(insns[2]) << 16);
jeffhaobdb76512011-09-07 11:43:16 -07003459 const uint16_t* switch_insns = insns + offset_to_switch;
3460 int switch_count = switch_insns[1];
3461 int offset_to_targets, targ;
3462
3463 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
3464 /* 0 = sig, 1 = count, 2/3 = first key */
3465 offset_to_targets = 4;
3466 } else {
3467 /* 0 = sig, 1 = count, 2..count * 2 = keys */
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07003468 DCHECK((*insns & 0xff) == Instruction::SPARSE_SWITCH);
jeffhaobdb76512011-09-07 11:43:16 -07003469 offset_to_targets = 2 + 2 * switch_count;
3470 }
3471
3472 /* verify each switch target */
3473 for (targ = 0; targ < switch_count; targ++) {
3474 int offset;
3475 uint32_t abs_offset;
3476
3477 /* offsets are 32-bit, and only partly endian-swapped */
3478 offset = switch_insns[offset_to_targets + targ * 2] |
Andreas Gampe53de99c2015-08-17 13:43:55 -07003479 (static_cast<int32_t>(switch_insns[offset_to_targets + targ * 2 + 1]) << 16);
Ian Rogersd81871c2011-10-03 13:57:23 -07003480 abs_offset = work_insn_idx_ + offset;
3481 DCHECK_LT(abs_offset, code_item_->insns_size_in_code_units_);
Stephen Kyle9bc61992014-09-22 13:53:15 +01003482 if (!CheckNotMoveExceptionOrMoveResult(code_item_->insns_, abs_offset)) {
jeffhaobdb76512011-09-07 11:43:16 -07003483 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07003484 }
Ian Rogersebbdd872014-07-07 23:53:08 -07003485 if (!UpdateRegisters(abs_offset, work_line_.get(), false)) {
jeffhaobdb76512011-09-07 11:43:16 -07003486 return false;
Ian Rogersebbdd872014-07-07 23:53:08 -07003487 }
jeffhaobdb76512011-09-07 11:43:16 -07003488 }
3489 }
3490
3491 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003492 * Handle instructions that can throw and that are sitting in a "try" block. (If they're not in a
3493 * "try" block when they throw, control transfers out of the method.)
jeffhaobdb76512011-09-07 11:43:16 -07003494 */
Mathieu Chartierde40d472015-10-15 17:47:48 -07003495 if ((opcode_flags & Instruction::kThrow) != 0 && GetInstructionFlags(work_insn_idx_).IsInTry()) {
Andreas Gampef91baf12014-07-18 15:41:00 -07003496 bool has_catch_all_handler = false;
Ian Rogers0571d352011-11-03 19:51:38 -07003497 CatchHandlerIterator iterator(*code_item_, work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07003498
Andreas Gampef91baf12014-07-18 15:41:00 -07003499 // Need the linker to try and resolve the handled class to check if it's Throwable.
3500 ClassLinker* linker = Runtime::Current()->GetClassLinker();
3501
Ian Rogers0571d352011-11-03 19:51:38 -07003502 for (; iterator.HasNext(); iterator.Next()) {
Andreas Gampef91baf12014-07-18 15:41:00 -07003503 uint16_t handler_type_idx = iterator.GetHandlerTypeIndex();
3504 if (handler_type_idx == DexFile::kDexNoIndex16) {
3505 has_catch_all_handler = true;
3506 } else {
3507 // It is also a catch-all if it is java.lang.Throwable.
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003508 mirror::Class* klass = linker->ResolveType(*dex_file_, handler_type_idx, dex_cache_,
3509 class_loader_);
Andreas Gampef91baf12014-07-18 15:41:00 -07003510 if (klass != nullptr) {
3511 if (klass == mirror::Throwable::GetJavaLangThrowable()) {
3512 has_catch_all_handler = true;
3513 }
3514 } else {
3515 // Clear exception.
Ian Rogers7b078e82014-09-10 14:44:24 -07003516 DCHECK(self_->IsExceptionPending());
3517 self_->ClearException();
Andreas Gampef91baf12014-07-18 15:41:00 -07003518 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003519 }
jeffhaobdb76512011-09-07 11:43:16 -07003520 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003521 * Merge registers into the "catch" block. We want to use the "savedRegs" rather than
3522 * "work_regs", because at runtime the exception will be thrown before the instruction
3523 * modifies any registers.
jeffhaobdb76512011-09-07 11:43:16 -07003524 */
Ian Rogersebbdd872014-07-07 23:53:08 -07003525 if (!UpdateRegisters(iterator.GetHandlerAddress(), saved_line_.get(), false)) {
jeffhaobdb76512011-09-07 11:43:16 -07003526 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07003527 }
jeffhaobdb76512011-09-07 11:43:16 -07003528 }
3529
3530 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003531 * If the monitor stack depth is nonzero, there must be a "catch all" handler for this
3532 * instruction. This does apply to monitor-exit because of async exception handling.
jeffhaobdb76512011-09-07 11:43:16 -07003533 */
Andreas Gampef91baf12014-07-18 15:41:00 -07003534 if (work_line_->MonitorStackDepth() > 0 && !has_catch_all_handler) {
jeffhaobdb76512011-09-07 11:43:16 -07003535 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003536 * The state in work_line reflects the post-execution state. If the current instruction is a
3537 * monitor-enter and the monitor stack was empty, we don't need a catch-all (if it throws,
jeffhaobdb76512011-09-07 11:43:16 -07003538 * it will do so before grabbing the lock).
3539 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02003540 if (inst->Opcode() != Instruction::MONITOR_ENTER || work_line_->MonitorStackDepth() != 1) {
jeffhaod5347e02012-03-22 17:25:05 -07003541 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
Ian Rogersd81871c2011-10-03 13:57:23 -07003542 << "expected to be within a catch-all for an instruction where a monitor is held";
jeffhaobdb76512011-09-07 11:43:16 -07003543 return false;
3544 }
3545 }
3546 }
3547
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003548 /* Handle "continue". Tag the next consecutive instruction.
3549 * Note: Keep the code handling "continue" case below the "branch" and "switch" cases,
3550 * because it changes work_line_ when performing peephole optimization
3551 * and this change should not be used in those cases.
3552 */
Ian Rogers6d376ae2013-07-23 15:12:40 -07003553 if ((opcode_flags & Instruction::kContinue) != 0) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003554 DCHECK_EQ(Instruction::At(code_item_->insns_ + work_insn_idx_), inst);
3555 uint32_t next_insn_idx = work_insn_idx_ + inst->SizeInCodeUnits();
Ian Rogers6d376ae2013-07-23 15:12:40 -07003556 if (next_insn_idx >= code_item_->insns_size_in_code_units_) {
3557 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Execution can walk off end of code area";
3558 return false;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003559 }
Ian Rogers6d376ae2013-07-23 15:12:40 -07003560 // The only way to get to a move-exception instruction is to get thrown there. Make sure the
3561 // next instruction isn't one.
3562 if (!CheckNotMoveException(code_item_->insns_, next_insn_idx)) {
3563 return false;
3564 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07003565 if (nullptr != fallthrough_line) {
Ian Rogers6d376ae2013-07-23 15:12:40 -07003566 // Make workline consistent with fallthrough computed from peephole optimization.
3567 work_line_->CopyFromLine(fallthrough_line.get());
3568 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07003569 if (GetInstructionFlags(next_insn_idx).IsReturn()) {
Ian Rogersb8c78592013-07-25 23:52:52 +00003570 // For returns we only care about the operand to the return, all other registers are dead.
3571 const Instruction* ret_inst = Instruction::At(code_item_->insns_ + next_insn_idx);
Andreas Gampea727e372015-08-25 09:22:37 -07003572 AdjustReturnLine(this, ret_inst, work_line_.get());
Ian Rogersb8c78592013-07-25 23:52:52 +00003573 }
Ian Rogers6d376ae2013-07-23 15:12:40 -07003574 RegisterLine* next_line = reg_table_.GetLine(next_insn_idx);
Ian Rogers7b078e82014-09-10 14:44:24 -07003575 if (next_line != nullptr) {
Ian Rogersebbdd872014-07-07 23:53:08 -07003576 // Merge registers into what we have for the next instruction, and set the "changed" flag if
3577 // needed. If the merge changes the state of the registers then the work line will be
3578 // updated.
3579 if (!UpdateRegisters(next_insn_idx, work_line_.get(), true)) {
Ian Rogers6d376ae2013-07-23 15:12:40 -07003580 return false;
3581 }
3582 } else {
3583 /*
3584 * We're not recording register data for the next instruction, so we don't know what the
3585 * prior state was. We have to assume that something has changed and re-evaluate it.
3586 */
Mathieu Chartierde40d472015-10-15 17:47:48 -07003587 GetInstructionFlags(next_insn_idx).SetChanged();
Ian Rogers6d376ae2013-07-23 15:12:40 -07003588 }
3589 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003590
jeffhaod1f0fde2011-09-08 17:25:33 -07003591 /* If we're returning from the method, make sure monitor stack is empty. */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003592 if ((opcode_flags & Instruction::kReturn) != 0) {
Andreas Gampea727e372015-08-25 09:22:37 -07003593 work_line_->VerifyMonitorStackEmpty(this);
jeffhaobdb76512011-09-07 11:43:16 -07003594 }
3595
3596 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003597 * Update start_guess. Advance to the next instruction of that's
3598 * possible, otherwise use the branch target if one was found. If
jeffhaobdb76512011-09-07 11:43:16 -07003599 * neither of those exists we're in a return or throw; leave start_guess
3600 * alone and let the caller sort it out.
3601 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003602 if ((opcode_flags & Instruction::kContinue) != 0) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003603 DCHECK_EQ(Instruction::At(code_item_->insns_ + work_insn_idx_), inst);
3604 *start_guess = work_insn_idx_ + inst->SizeInCodeUnits();
Elliott Hughesadb8c672012-03-06 16:49:32 -08003605 } else if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07003606 /* we're still okay if branch_target is zero */
Ian Rogersd81871c2011-10-03 13:57:23 -07003607 *start_guess = work_insn_idx_ + branch_target;
jeffhaobdb76512011-09-07 11:43:16 -07003608 }
3609
Ian Rogersd81871c2011-10-03 13:57:23 -07003610 DCHECK_LT(*start_guess, code_item_->insns_size_in_code_units_);
Mathieu Chartierde40d472015-10-15 17:47:48 -07003611 DCHECK(GetInstructionFlags(*start_guess).IsOpcode());
jeffhaobdb76512011-09-07 11:43:16 -07003612
Andreas Gampea727e372015-08-25 09:22:37 -07003613 if (have_pending_runtime_throw_failure_) {
3614 have_any_pending_runtime_throw_failure_ = true;
3615 // Reset the pending_runtime_throw flag now.
3616 have_pending_runtime_throw_failure_ = false;
3617 }
3618
jeffhaobdb76512011-09-07 11:43:16 -07003619 return true;
Brian Carlstrom7934ac22013-07-26 10:54:15 -07003620} // NOLINT(readability/fn_size)
jeffhaobdb76512011-09-07 11:43:16 -07003621
Mathieu Chartierde40d472015-10-15 17:47:48 -07003622void MethodVerifier::UninstantiableError(const char* descriptor) {
3623 Fail(VerifyError::VERIFY_ERROR_NO_CLASS) << "Could not create precise reference for "
3624 << "non-instantiable klass " << descriptor;
3625}
3626
3627inline bool MethodVerifier::IsInstantiableOrPrimitive(mirror::Class* klass) {
3628 return klass->IsInstantiable() || klass->IsPrimitive();
3629}
3630
Ian Rogersd8f69b02014-09-10 21:43:52 +00003631const RegType& MethodVerifier::ResolveClassAndCheckAccess(uint32_t class_idx) {
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003632 mirror::Class* klass = dex_cache_->GetResolvedType(class_idx);
Mathieu Chartierde40d472015-10-15 17:47:48 -07003633 const RegType* result = nullptr;
3634 if (klass != nullptr) {
3635 bool precise = klass->CannotBeAssignedFromOtherTypes();
3636 if (precise && !IsInstantiableOrPrimitive(klass)) {
3637 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
3638 UninstantiableError(descriptor);
3639 precise = false;
3640 }
3641 result = reg_types_.FindClass(klass, precise);
3642 if (result == nullptr) {
3643 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
3644 result = reg_types_.InsertClass(descriptor, klass, precise);
3645 }
3646 } else {
3647 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
3648 result = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003649 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07003650 DCHECK(result != nullptr);
3651 if (result->IsConflict()) {
3652 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
3653 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "accessing broken descriptor '" << descriptor
3654 << "' in " << GetDeclaringClass();
3655 return *result;
3656 }
3657 if (klass == nullptr && !result->IsUnresolvedTypes()) {
David Brazdilca3c8c32016-09-06 14:04:48 +01003658 klass = result->GetClass();
3659 dex_cache_->SetResolvedType(class_idx, klass);
Ian Rogerse1758fe2012-04-19 11:31:15 -07003660 }
David Brazdilca3c8c32016-09-06 14:04:48 +01003661
3662 // Record result of class resolution attempt.
3663 VerifierDeps::MaybeRecordClassResolution(*dex_file_, class_idx, klass);
3664
Ian Rogersad0b3a32012-04-16 14:50:24 -07003665 // Check if access is allowed. Unresolved types use xxxWithAccessCheck to
Jeff Haob24b4a72013-07-31 13:47:31 -07003666 // check at runtime if access is allowed and so pass here. If result is
3667 // primitive, skip the access check.
Mathieu Chartierde40d472015-10-15 17:47:48 -07003668 if (result->IsNonZeroReferenceTypes() && !result->IsUnresolvedTypes()) {
3669 const RegType& referrer = GetDeclaringClass();
3670 if (!referrer.IsUnresolvedTypes() && !referrer.CanAccess(*result)) {
3671 Fail(VERIFY_ERROR_ACCESS_CLASS) << "illegal class access: '"
3672 << referrer << "' -> '" << result << "'";
3673 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07003674 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07003675 return *result;
Ian Rogersd81871c2011-10-03 13:57:23 -07003676}
3677
Ian Rogersd8f69b02014-09-10 21:43:52 +00003678const RegType& MethodVerifier::GetCaughtExceptionType() {
Ian Rogers7b078e82014-09-10 14:44:24 -07003679 const RegType* common_super = nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003680 if (code_item_->tries_size_ != 0) {
Ian Rogers13735952014-10-08 12:43:28 -07003681 const uint8_t* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
Ian Rogersd81871c2011-10-03 13:57:23 -07003682 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
3683 for (uint32_t i = 0; i < handlers_size; i++) {
Ian Rogers0571d352011-11-03 19:51:38 -07003684 CatchHandlerIterator iterator(handlers_ptr);
3685 for (; iterator.HasNext(); iterator.Next()) {
3686 if (iterator.GetHandlerAddress() == (uint32_t) work_insn_idx_) {
3687 if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) {
Ian Rogersb4903572012-10-11 11:52:56 -07003688 common_super = &reg_types_.JavaLangThrowable(false);
Ian Rogersd81871c2011-10-03 13:57:23 -07003689 } else {
Ian Rogersd8f69b02014-09-10 21:43:52 +00003690 const RegType& exception = ResolveClassAndCheckAccess(iterator.GetHandlerTypeIndex());
David Brazdilca3c8c32016-09-06 14:04:48 +01003691 if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(exception, this)) {
David Brazdil68b5c0b2016-01-19 14:25:29 +00003692 DCHECK(!exception.IsUninitializedTypes()); // Comes from dex, shouldn't be uninit.
Jeff Haoc26a56c2013-11-04 12:00:47 -08003693 if (exception.IsUnresolvedTypes()) {
3694 // We don't know enough about the type. Fail here and let runtime handle it.
3695 Fail(VERIFY_ERROR_NO_CLASS) << "unresolved exception class " << exception;
3696 return exception;
3697 } else {
3698 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unexpected non-exception class " << exception;
3699 return reg_types_.Conflict();
3700 }
Jeff Haob878f212014-04-24 16:25:36 -07003701 } else if (common_super == nullptr) {
3702 common_super = &exception;
Ian Rogers28ad40d2011-10-27 15:19:26 -07003703 } else if (common_super->Equals(exception)) {
Ian Rogersc4762272012-02-01 15:55:55 -08003704 // odd case, but nothing to do
Ian Rogersd81871c2011-10-03 13:57:23 -07003705 } else {
David Brazdilca3c8c32016-09-06 14:04:48 +01003706 common_super = &common_super->Merge(exception, &reg_types_, this);
Andreas Gampe7c038102014-10-27 20:08:46 -07003707 if (FailOrAbort(this,
David Brazdilca3c8c32016-09-06 14:04:48 +01003708 reg_types_.JavaLangThrowable(false).IsAssignableFrom(
3709 *common_super, this),
Andreas Gampe7c038102014-10-27 20:08:46 -07003710 "java.lang.Throwable is not assignable-from common_super at ",
3711 work_insn_idx_)) {
3712 break;
3713 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003714 }
3715 }
3716 }
3717 }
Ian Rogers0571d352011-11-03 19:51:38 -07003718 handlers_ptr = iterator.EndDataPointer();
Ian Rogersd81871c2011-10-03 13:57:23 -07003719 }
3720 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003721 if (common_super == nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003722 /* no catch blocks, or no catches with classes we can find */
jeffhaod5347e02012-03-22 17:25:05 -07003723 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unable to find exception handler";
Ian Rogersad0b3a32012-04-16 14:50:24 -07003724 return reg_types_.Conflict();
Ian Rogersd81871c2011-10-03 13:57:23 -07003725 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07003726 return *common_super;
Ian Rogersd81871c2011-10-03 13:57:23 -07003727}
3728
David Brazdilca3c8c32016-09-06 14:04:48 +01003729inline static MethodResolutionKind GetMethodResolutionKind(
3730 MethodType method_type, bool is_interface) {
3731 if (method_type == METHOD_DIRECT || method_type == METHOD_STATIC) {
3732 return kDirectMethodResolution;
3733 } else if (method_type == METHOD_INTERFACE) {
3734 return kInterfaceMethodResolution;
3735 } else if (method_type == METHOD_SUPER && is_interface) {
3736 return kInterfaceMethodResolution;
3737 } else {
3738 DCHECK(method_type == METHOD_VIRTUAL || method_type == METHOD_SUPER);
3739 return kVirtualMethodResolution;
3740 }
3741}
3742
Mathieu Chartiere401d142015-04-22 13:56:20 -07003743ArtMethod* MethodVerifier::ResolveMethodAndCheckAccess(
Alex Light7268d472016-01-20 15:50:01 -08003744 uint32_t dex_method_idx, MethodType method_type) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003745 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx);
Ian Rogersd8f69b02014-09-10 21:43:52 +00003746 const RegType& klass_type = ResolveClassAndCheckAccess(method_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003747 if (klass_type.IsConflict()) {
3748 std::string append(" in attempt to access method ");
3749 append += dex_file_->GetMethodName(method_id);
3750 AppendToLastFailMessage(append);
Ian Rogers7b078e82014-09-10 14:44:24 -07003751 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08003752 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003753 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003754 return nullptr; // Can't resolve Class so no more to do here
Ian Rogers90040192011-12-16 08:54:29 -08003755 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003756 mirror::Class* klass = klass_type.GetClass();
Ian Rogersd8f69b02014-09-10 21:43:52 +00003757 const RegType& referrer = GetDeclaringClass();
Mathieu Chartiere401d142015-04-22 13:56:20 -07003758 auto* cl = Runtime::Current()->GetClassLinker();
3759 auto pointer_size = cl->GetImagePointerSize();
David Brazdilca3c8c32016-09-06 14:04:48 +01003760 MethodResolutionKind res_kind = GetMethodResolutionKind(method_type, klass->IsInterface());
Andreas Gampe42ef8ab2015-12-03 17:27:32 -08003761
Mathieu Chartiere401d142015-04-22 13:56:20 -07003762 ArtMethod* res_method = dex_cache_->GetResolvedMethod(dex_method_idx, pointer_size);
Andreas Gampe42ef8ab2015-12-03 17:27:32 -08003763 bool stash_method = false;
Ian Rogers7b078e82014-09-10 14:44:24 -07003764 if (res_method == nullptr) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07003765 const char* name = dex_file_->GetMethodName(method_id);
Ian Rogersd91d6d62013-09-25 20:26:14 -07003766 const Signature signature = dex_file_->GetMethodSignature(method_id);
jeffhao8cd6dda2012-02-22 10:15:34 -08003767
David Brazdilca3c8c32016-09-06 14:04:48 +01003768 if (res_kind == kDirectMethodResolution) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003769 res_method = klass->FindDirectMethod(name, signature, pointer_size);
David Brazdilca3c8c32016-09-06 14:04:48 +01003770 } else if (res_kind == kVirtualMethodResolution) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003771 res_method = klass->FindVirtualMethod(name, signature, pointer_size);
David Brazdilca3c8c32016-09-06 14:04:48 +01003772 } else {
3773 DCHECK_EQ(res_kind, kInterfaceMethodResolution);
3774 res_method = klass->FindInterfaceMethod(name, signature, pointer_size);
Ian Rogersd81871c2011-10-03 13:57:23 -07003775 }
David Brazdilca3c8c32016-09-06 14:04:48 +01003776
Ian Rogers7b078e82014-09-10 14:44:24 -07003777 if (res_method != nullptr) {
Andreas Gampe42ef8ab2015-12-03 17:27:32 -08003778 stash_method = true;
Ian Rogersd81871c2011-10-03 13:57:23 -07003779 } else {
jeffhao8cd6dda2012-02-22 10:15:34 -08003780 // If a virtual or interface method wasn't found with the expected type, look in
3781 // the direct methods. This can happen when the wrong invoke type is used or when
3782 // a class has changed, and will be flagged as an error in later checks.
David Brazdilca3c8c32016-09-06 14:04:48 +01003783 // Note that in this case, we do not put the resolved method in the Dex cache
3784 // because it was not discovered using the expected type of method resolution.
3785 if (res_kind != kDirectMethodResolution) {
3786 // Record result of the initial resolution attempt.
3787 VerifierDeps::MaybeRecordMethodResolution(*dex_file_, dex_method_idx, res_kind, nullptr);
3788 // Change resolution type to 'direct' and try to resolve again.
3789 res_kind = kDirectMethodResolution;
Mathieu Chartiere401d142015-04-22 13:56:20 -07003790 res_method = klass->FindDirectMethod(name, signature, pointer_size);
jeffhao8cd6dda2012-02-22 10:15:34 -08003791 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003792 }
3793 }
David Brazdilca3c8c32016-09-06 14:04:48 +01003794
3795 // Record result of method resolution attempt.
3796 VerifierDeps::MaybeRecordMethodResolution(*dex_file_, dex_method_idx, res_kind, res_method);
3797
3798 if (res_method == nullptr) {
3799 Fail(VERIFY_ERROR_NO_METHOD) << "couldn't find method "
David Sehr709b0702016-10-13 09:12:37 -07003800 << klass->PrettyDescriptor() << "."
David Brazdilca3c8c32016-09-06 14:04:48 +01003801 << dex_file_->GetMethodName(method_id) << " "
3802 << dex_file_->GetMethodSignature(method_id);
3803 return nullptr;
3804 }
3805
Ian Rogersd81871c2011-10-03 13:57:23 -07003806 // Make sure calls to constructors are "direct". There are additional restrictions but we don't
3807 // enforce them here.
3808 if (res_method->IsConstructor() && method_type != METHOD_DIRECT) {
jeffhaod5347e02012-03-22 17:25:05 -07003809 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting non-direct call to constructor "
David Sehr709b0702016-10-13 09:12:37 -07003810 << res_method->PrettyMethod();
Ian Rogers7b078e82014-09-10 14:44:24 -07003811 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003812 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003813 // Disallow any calls to class initializers.
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003814 if (res_method->IsClassInitializer()) {
jeffhaod5347e02012-03-22 17:25:05 -07003815 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting call to class initializer "
David Sehr709b0702016-10-13 09:12:37 -07003816 << res_method->PrettyMethod();
Ian Rogers7b078e82014-09-10 14:44:24 -07003817 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003818 }
Andreas Gampe42ef8ab2015-12-03 17:27:32 -08003819
3820 // Check that interface methods are static or match interface classes.
3821 // We only allow statics if we don't have default methods enabled.
3822 //
3823 // Note: this check must be after the initializer check, as those are required to fail a class,
3824 // while this check implies an IncompatibleClassChangeError.
3825 if (klass->IsInterface()) {
Alex Lightb55f1ac2016-04-12 15:50:55 -07003826 // methods called on interfaces should be invoke-interface, invoke-super, invoke-direct (if
3827 // dex file version is 37 or greater), or invoke-static.
Andreas Gampe42ef8ab2015-12-03 17:27:32 -08003828 if (method_type != METHOD_INTERFACE &&
Neil Fuller9724c632016-01-07 15:42:47 +00003829 method_type != METHOD_STATIC &&
Alex Lightb55f1ac2016-04-12 15:50:55 -07003830 ((dex_file_->GetVersion() < DexFile::kDefaultMethodsVersion) ||
3831 method_type != METHOD_DIRECT) &&
Neil Fuller9724c632016-01-07 15:42:47 +00003832 method_type != METHOD_SUPER) {
Andreas Gampe42ef8ab2015-12-03 17:27:32 -08003833 Fail(VERIFY_ERROR_CLASS_CHANGE)
David Sehr709b0702016-10-13 09:12:37 -07003834 << "non-interface method " << dex_file_->PrettyMethod(dex_method_idx)
3835 << " is in an interface class " << klass->PrettyClass();
Andreas Gampe42ef8ab2015-12-03 17:27:32 -08003836 return nullptr;
3837 }
3838 } else {
3839 if (method_type == METHOD_INTERFACE) {
3840 Fail(VERIFY_ERROR_CLASS_CHANGE)
David Sehr709b0702016-10-13 09:12:37 -07003841 << "interface method " << dex_file_->PrettyMethod(dex_method_idx)
3842 << " is in a non-interface class " << klass->PrettyClass();
Andreas Gampe42ef8ab2015-12-03 17:27:32 -08003843 return nullptr;
3844 }
3845 }
3846
3847 // Only stash after the above passed. Otherwise the method wasn't guaranteed to be correct.
3848 if (stash_method) {
3849 dex_cache_->SetResolvedMethod(dex_method_idx, res_method, pointer_size);
3850 }
3851
jeffhao8cd6dda2012-02-22 10:15:34 -08003852 // Check if access is allowed.
Ian Rogersad0b3a32012-04-16 14:50:24 -07003853 if (!referrer.CanAccessMember(res_method->GetDeclaringClass(), res_method->GetAccessFlags())) {
David Sehr709b0702016-10-13 09:12:37 -07003854 Fail(VERIFY_ERROR_ACCESS_METHOD) << "illegal method access (call "
3855 << res_method->PrettyMethod()
Ian Rogersad0b3a32012-04-16 14:50:24 -07003856 << " from " << referrer << ")";
jeffhaob57e9522012-04-26 18:08:21 -07003857 return res_method;
jeffhao8cd6dda2012-02-22 10:15:34 -08003858 }
jeffhaode0d9c92012-02-27 13:58:13 -08003859 // Check that invoke-virtual and invoke-super are not used on private methods of the same class.
Alex Light7268d472016-01-20 15:50:01 -08003860 if (res_method->IsPrivate() && (method_type == METHOD_VIRTUAL || method_type == METHOD_SUPER)) {
jeffhaod5347e02012-03-22 17:25:05 -07003861 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invoke-super/virtual can't be used on private method "
David Sehr709b0702016-10-13 09:12:37 -07003862 << res_method->PrettyMethod();
Ian Rogers7b078e82014-09-10 14:44:24 -07003863 return nullptr;
jeffhaode0d9c92012-02-27 13:58:13 -08003864 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003865 // See if the method type implied by the invoke instruction matches the access flags for the
3866 // target method.
Brian Carlstrombe6fa5e2014-12-09 20:15:42 -08003867 if ((method_type == METHOD_DIRECT && (!res_method->IsDirect() || res_method->IsStatic())) ||
Ian Rogersd81871c2011-10-03 13:57:23 -07003868 (method_type == METHOD_STATIC && !res_method->IsStatic()) ||
Alex Light7268d472016-01-20 15:50:01 -08003869 ((method_type == METHOD_SUPER ||
3870 method_type == METHOD_VIRTUAL ||
3871 method_type == METHOD_INTERFACE) && res_method->IsDirect())
Ian Rogersd81871c2011-10-03 13:57:23 -07003872 ) {
Ian Rogers2fc14272012-08-30 10:56:57 -07003873 Fail(VERIFY_ERROR_CLASS_CHANGE) << "invoke type (" << method_type << ") does not match method "
David Sehr709b0702016-10-13 09:12:37 -07003874 " type of " << res_method->PrettyMethod();
Ian Rogers7b078e82014-09-10 14:44:24 -07003875 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003876 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003877 return res_method;
3878}
3879
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003880template <class T>
Mathieu Chartiere401d142015-04-22 13:56:20 -07003881ArtMethod* MethodVerifier::VerifyInvocationArgsFromIterator(
3882 T* it, const Instruction* inst, MethodType method_type, bool is_range, ArtMethod* res_method) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003883 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
3884 // match the call to the signature. Also, we might be calling through an abstract method
3885 // definition (which doesn't have register count values).
3886 const size_t expected_args = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
3887 /* caught by static verifier */
3888 DCHECK(is_range || expected_args <= 5);
3889 if (expected_args > code_item_->outs_size_) {
3890 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
3891 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
3892 return nullptr;
3893 }
3894
3895 uint32_t arg[5];
3896 if (!is_range) {
3897 inst->GetVarArgs(arg);
3898 }
3899 uint32_t sig_registers = 0;
3900
3901 /*
3902 * Check the "this" argument, which must be an instance of the class that declared the method.
3903 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
3904 * rigorous check here (which is okay since we have to do it at runtime).
3905 */
3906 if (method_type != METHOD_STATIC) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003907 const RegType& actual_arg_type = work_line_->GetInvocationThis(this, inst, is_range);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003908 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
3909 CHECK(have_pending_hard_failure_);
3910 return nullptr;
3911 }
David Brazdil68b5c0b2016-01-19 14:25:29 +00003912 bool is_init = false;
3913 if (actual_arg_type.IsUninitializedTypes()) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003914 if (res_method) {
3915 if (!res_method->IsConstructor()) {
3916 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
3917 return nullptr;
3918 }
3919 } else {
3920 // Check whether the name of the called method is "<init>"
3921 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Jeff Hao0d087272014-08-04 14:47:17 -07003922 if (strcmp(dex_file_->GetMethodName(dex_file_->GetMethodId(method_idx)), "<init>") != 0) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003923 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
3924 return nullptr;
3925 }
3926 }
David Brazdil68b5c0b2016-01-19 14:25:29 +00003927 is_init = true;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003928 }
David Brazdil68b5c0b2016-01-19 14:25:29 +00003929 const RegType& adjusted_type = is_init
3930 ? GetRegTypeCache()->FromUninitialized(actual_arg_type)
3931 : actual_arg_type;
3932 if (method_type != METHOD_INTERFACE && !adjusted_type.IsZero()) {
Ian Rogersd8f69b02014-09-10 21:43:52 +00003933 const RegType* res_method_class;
Andreas Gamped9e23012015-06-04 22:19:58 -07003934 // Miranda methods have the declaring interface as their declaring class, not the abstract
3935 // class. It would be wrong to use this for the type check (interface type checks are
3936 // postponed to runtime).
3937 if (res_method != nullptr && !res_method->IsMiranda()) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003938 mirror::Class* klass = res_method->GetDeclaringClass();
Ian Rogers1ff3c982014-08-12 02:30:58 -07003939 std::string temp;
Andreas Gampef23f33d2015-06-23 14:18:17 -07003940 res_method_class = &FromClass(klass->GetDescriptor(&temp), klass,
3941 klass->CannotBeAssignedFromOtherTypes());
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003942 } else {
3943 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
3944 const uint16_t class_idx = dex_file_->GetMethodId(method_idx).class_idx_;
Mathieu Chartierde40d472015-10-15 17:47:48 -07003945 res_method_class = &reg_types_.FromDescriptor(
3946 GetClassLoader(),
3947 dex_file_->StringByTypeIdx(class_idx),
3948 false);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003949 }
David Brazdilca3c8c32016-09-06 14:04:48 +01003950 if (!res_method_class->IsAssignableFrom(adjusted_type, this)) {
David Brazdil68b5c0b2016-01-19 14:25:29 +00003951 Fail(adjusted_type.IsUnresolvedTypes()
3952 ? VERIFY_ERROR_NO_CLASS
3953 : VERIFY_ERROR_BAD_CLASS_SOFT)
3954 << "'this' argument '" << actual_arg_type << "' not instance of '"
3955 << *res_method_class << "'";
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003956 // Continue on soft failures. We need to find possible hard failures to avoid problems in
3957 // the compiler.
3958 if (have_pending_hard_failure_) {
3959 return nullptr;
3960 }
3961 }
3962 }
3963 sig_registers = 1;
3964 }
3965
3966 for ( ; it->HasNext(); it->Next()) {
3967 if (sig_registers >= expected_args) {
3968 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, expected " << inst->VRegA() <<
3969 " arguments, found " << sig_registers << " or more.";
3970 return nullptr;
3971 }
3972
3973 const char* param_descriptor = it->GetDescriptor();
3974
3975 if (param_descriptor == nullptr) {
3976 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation because of missing signature "
3977 "component";
3978 return nullptr;
3979 }
3980
Ian Rogersd8f69b02014-09-10 21:43:52 +00003981 const RegType& reg_type = reg_types_.FromDescriptor(GetClassLoader(), param_descriptor, false);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003982 uint32_t get_reg = is_range ? inst->VRegC_3rc() + static_cast<uint32_t>(sig_registers) :
3983 arg[sig_registers];
3984 if (reg_type.IsIntegralTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003985 const RegType& src_type = work_line_->GetRegisterType(this, get_reg);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003986 if (!src_type.IsIntegralTypes()) {
3987 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register v" << get_reg << " has type " << src_type
3988 << " but expected " << reg_type;
Andreas Gampeb588f4c2015-05-26 13:35:39 -07003989 return nullptr;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003990 }
Andreas Gampeda9badb2015-06-05 20:22:12 -07003991 } else {
3992 if (!work_line_->VerifyRegisterType(this, get_reg, reg_type)) {
3993 // Continue on soft failures. We need to find possible hard failures to avoid problems in
3994 // the compiler.
3995 if (have_pending_hard_failure_) {
3996 return nullptr;
3997 }
3998 } else if (reg_type.IsLongOrDoubleTypes()) {
3999 // Check that registers are consecutive (for non-range invokes). Invokes are the only
4000 // instructions not specifying register pairs by the first component, but require them
4001 // nonetheless. Only check when there's an actual register in the parameters. If there's
4002 // none, this will fail below.
4003 if (!is_range && sig_registers + 1 < expected_args) {
4004 uint32_t second_reg = arg[sig_registers + 1];
4005 if (second_reg != get_reg + 1) {
4006 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, long or double parameter "
4007 "at index " << sig_registers << " is not a pair: " << get_reg << " + "
4008 << second_reg << ".";
4009 return nullptr;
4010 }
4011 }
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004012 }
4013 }
4014 sig_registers += reg_type.IsLongOrDoubleTypes() ? 2 : 1;
4015 }
4016 if (expected_args != sig_registers) {
4017 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, expected " << expected_args <<
4018 " arguments, found " << sig_registers;
4019 return nullptr;
4020 }
4021 return res_method;
4022}
4023
4024void MethodVerifier::VerifyInvocationArgsUnresolvedMethod(const Instruction* inst,
4025 MethodType method_type,
4026 bool is_range) {
4027 // As the method may not have been resolved, make this static check against what we expect.
4028 // The main reason for this code block is to fail hard when we find an illegal use, e.g.,
4029 // wrong number of arguments or wrong primitive types, even if the method could not be resolved.
4030 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
4031 DexFileParameterIterator it(*dex_file_,
4032 dex_file_->GetProtoId(dex_file_->GetMethodId(method_idx).proto_idx_));
4033 VerifyInvocationArgsFromIterator<DexFileParameterIterator>(&it, inst, method_type, is_range,
4034 nullptr);
4035}
4036
4037class MethodParamListDescriptorIterator {
4038 public:
Mathieu Chartiere401d142015-04-22 13:56:20 -07004039 explicit MethodParamListDescriptorIterator(ArtMethod* res_method) :
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004040 res_method_(res_method), pos_(0), params_(res_method->GetParameterTypeList()),
4041 params_size_(params_ == nullptr ? 0 : params_->Size()) {
4042 }
4043
4044 bool HasNext() {
4045 return pos_ < params_size_;
4046 }
4047
4048 void Next() {
4049 ++pos_;
4050 }
4051
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07004052 const char* GetDescriptor() REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004053 return res_method_->GetTypeDescriptorFromTypeIdx(params_->GetTypeItem(pos_).type_idx_);
4054 }
4055
4056 private:
Mathieu Chartiere401d142015-04-22 13:56:20 -07004057 ArtMethod* res_method_;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004058 size_t pos_;
4059 const DexFile::TypeList* params_;
4060 const size_t params_size_;
4061};
4062
Mathieu Chartiere401d142015-04-22 13:56:20 -07004063ArtMethod* MethodVerifier::VerifyInvocationArgs(
Alex Light7268d472016-01-20 15:50:01 -08004064 const Instruction* inst, MethodType method_type, bool is_range) {
jeffhao8cd6dda2012-02-22 10:15:34 -08004065 // Resolve the method. This could be an abstract or concrete method depending on what sort of call
4066 // we're making.
Sebastien Hertz5243e912013-05-21 10:55:07 +02004067 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Andreas Gampeacc4d2f2014-06-12 19:35:05 -07004068
Alex Light7268d472016-01-20 15:50:01 -08004069 ArtMethod* res_method = ResolveMethodAndCheckAccess(method_idx, method_type);
Ian Rogers7b078e82014-09-10 14:44:24 -07004070 if (res_method == nullptr) { // error or class is unresolved
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004071 // Check what we can statically.
4072 if (!have_pending_hard_failure_) {
4073 VerifyInvocationArgsUnresolvedMethod(inst, method_type, is_range);
4074 }
4075 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08004076 }
4077
Ian Rogersd81871c2011-10-03 13:57:23 -07004078 // If we're using invoke-super(method), make sure that the executing method's class' superclass
Alex Light705ad492015-09-21 11:36:30 -07004079 // has a vtable entry for the target method. Or the target is on a interface.
Alex Light7268d472016-01-20 15:50:01 -08004080 if (method_type == METHOD_SUPER) {
Alex Lightfedd91d2016-01-07 14:49:16 -08004081 uint16_t class_idx = dex_file_->GetMethodId(method_idx).class_idx_;
David Brazdilca3c8c32016-09-06 14:04:48 +01004082 const RegType& reference_type = reg_types_.FromDescriptor(
4083 GetClassLoader(),
4084 dex_file_->StringByTypeIdx(class_idx),
4085 false);
4086 if (reference_type.IsUnresolvedTypes()) {
Alex Lightfedd91d2016-01-07 14:49:16 -08004087 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "Unable to find referenced class from invoke-super";
4088 return nullptr;
4089 }
David Brazdilca3c8c32016-09-06 14:04:48 +01004090 if (reference_type.GetClass()->IsInterface()) {
Alex Lightfedd91d2016-01-07 14:49:16 -08004091 // TODO Can we verify anything else.
David Brazdil15fc7292016-09-02 14:13:18 +01004092 if (class_idx == class_def_.class_idx_) {
Alex Lightfedd91d2016-01-07 14:49:16 -08004093 Fail(VERIFY_ERROR_CLASS_CHANGE) << "Cannot invoke-super on self as interface";
Alex Light55ea94d2016-03-15 09:50:26 -07004094 return nullptr;
Alex Lightfedd91d2016-01-07 14:49:16 -08004095 }
4096 // TODO Revisit whether we want to allow invoke-super on direct interfaces only like the JLS
4097 // does.
Alex Light55ea94d2016-03-15 09:50:26 -07004098 if (!GetDeclaringClass().HasClass()) {
4099 Fail(VERIFY_ERROR_NO_CLASS) << "Unable to resolve the full class of 'this' used in an"
4100 << "interface invoke-super";
4101 return nullptr;
David Brazdilca3c8c32016-09-06 14:04:48 +01004102 } else if (!reference_type.IsStrictlyAssignableFrom(GetDeclaringClass(), this)) {
Alex Lightfedd91d2016-01-07 14:49:16 -08004103 Fail(VERIFY_ERROR_CLASS_CHANGE)
David Sehr709b0702016-10-13 09:12:37 -07004104 << "invoke-super in " << mirror::Class::PrettyClass(GetDeclaringClass().GetClass())
4105 << " in method "
4106 << dex_file_->PrettyMethod(dex_method_idx_) << " to method "
4107 << dex_file_->PrettyMethod(method_idx) << " references "
4108 << "non-super-interface type " << mirror::Class::PrettyClass(reference_type.GetClass());
Alex Lightfedd91d2016-01-07 14:49:16 -08004109 return nullptr;
Alex Light705ad492015-09-21 11:36:30 -07004110 }
4111 } else {
4112 const RegType& super = GetDeclaringClass().GetSuperClass(&reg_types_);
4113 if (super.IsUnresolvedTypes()) {
4114 Fail(VERIFY_ERROR_NO_METHOD) << "unknown super class in invoke-super from "
David Sehr709b0702016-10-13 09:12:37 -07004115 << dex_file_->PrettyMethod(dex_method_idx_)
4116 << " to super " << res_method->PrettyMethod();
Alex Light705ad492015-09-21 11:36:30 -07004117 return nullptr;
4118 }
David Brazdilca3c8c32016-09-06 14:04:48 +01004119 if (!reference_type.IsStrictlyAssignableFrom(GetDeclaringClass(), this) ||
Aart Bikf663e342016-04-04 17:28:59 -07004120 (res_method->GetMethodIndex() >= super.GetClass()->GetVTableLength())) {
Alex Light705ad492015-09-21 11:36:30 -07004121 Fail(VERIFY_ERROR_NO_METHOD) << "invalid invoke-super from "
David Sehr709b0702016-10-13 09:12:37 -07004122 << dex_file_->PrettyMethod(dex_method_idx_)
Alex Light705ad492015-09-21 11:36:30 -07004123 << " to super " << super
4124 << "." << res_method->GetName()
4125 << res_method->GetSignature();
4126 return nullptr;
4127 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004128 }
4129 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08004130
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004131 // Process the target method's signature. This signature may or may not
4132 MethodParamListDescriptorIterator it(res_method);
4133 return VerifyInvocationArgsFromIterator<MethodParamListDescriptorIterator>(&it, inst, method_type,
4134 is_range, res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07004135}
4136
Mathieu Chartiere401d142015-04-22 13:56:20 -07004137ArtMethod* MethodVerifier::GetQuickInvokedMethod(const Instruction* inst, RegisterLine* reg_line,
4138 bool is_range, bool allow_failure) {
Mathieu Chartier091d2382015-03-06 10:59:06 -08004139 if (is_range) {
4140 DCHECK_EQ(inst->Opcode(), Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
4141 } else {
4142 DCHECK_EQ(inst->Opcode(), Instruction::INVOKE_VIRTUAL_QUICK);
4143 }
4144 const RegType& actual_arg_type = reg_line->GetInvocationThis(this, inst, is_range, allow_failure);
Ian Rogers9bc54402014-04-17 16:40:01 -07004145 if (!actual_arg_type.HasClass()) {
4146 VLOG(verifier) << "Failed to get mirror::Class* from '" << actual_arg_type << "'";
Andreas Gampe63981562014-04-17 12:28:43 -07004147 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004148 }
Ian Rogersa4cf1df2014-05-07 19:47:17 -07004149 mirror::Class* klass = actual_arg_type.GetClass();
Mingyao Yang2cdbad72014-07-16 10:44:41 -07004150 mirror::Class* dispatch_class;
Ian Rogersa4cf1df2014-05-07 19:47:17 -07004151 if (klass->IsInterface()) {
4152 // Derive Object.class from Class.class.getSuperclass().
4153 mirror::Class* object_klass = klass->GetClass()->GetSuperClass();
Andreas Gampe7c038102014-10-27 20:08:46 -07004154 if (FailOrAbort(this, object_klass->IsObjectClass(),
Mathieu Chartier091d2382015-03-06 10:59:06 -08004155 "Failed to find Object class in quickened invoke receiver", work_insn_idx_)) {
Andreas Gampe7c038102014-10-27 20:08:46 -07004156 return nullptr;
4157 }
Mingyao Yang2cdbad72014-07-16 10:44:41 -07004158 dispatch_class = object_klass;
Ian Rogersa4cf1df2014-05-07 19:47:17 -07004159 } else {
Mingyao Yang2cdbad72014-07-16 10:44:41 -07004160 dispatch_class = klass;
Ian Rogersa4cf1df2014-05-07 19:47:17 -07004161 }
Mathieu Chartier091d2382015-03-06 10:59:06 -08004162 if (!dispatch_class->HasVTable()) {
4163 FailOrAbort(this, allow_failure, "Receiver class has no vtable for quickened invoke at ",
4164 work_insn_idx_);
Andreas Gampe7c038102014-10-27 20:08:46 -07004165 return nullptr;
4166 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004167 uint16_t vtable_index = is_range ? inst->VRegB_3rc() : inst->VRegB_35c();
Mathieu Chartiere401d142015-04-22 13:56:20 -07004168 auto* cl = Runtime::Current()->GetClassLinker();
4169 auto pointer_size = cl->GetImagePointerSize();
Mathieu Chartier091d2382015-03-06 10:59:06 -08004170 if (static_cast<int32_t>(vtable_index) >= dispatch_class->GetVTableLength()) {
4171 FailOrAbort(this, allow_failure,
4172 "Receiver class has not enough vtable slots for quickened invoke at ",
4173 work_insn_idx_);
Andreas Gampe7c038102014-10-27 20:08:46 -07004174 return nullptr;
4175 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07004176 ArtMethod* res_method = dispatch_class->GetVTableEntry(vtable_index, pointer_size);
Mathieu Chartier091d2382015-03-06 10:59:06 -08004177 if (self_->IsExceptionPending()) {
4178 FailOrAbort(this, allow_failure, "Unexpected exception pending for quickened invoke at ",
4179 work_insn_idx_);
Andreas Gampe7c038102014-10-27 20:08:46 -07004180 return nullptr;
4181 }
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004182 return res_method;
4183}
4184
Mathieu Chartiere401d142015-04-22 13:56:20 -07004185ArtMethod* MethodVerifier::VerifyInvokeVirtualQuickArgs(const Instruction* inst, bool is_range) {
Andreas Gampe76bd8802014-12-10 16:43:58 -08004186 DCHECK(Runtime::Current()->IsStarted() || verify_to_dump_)
David Sehr709b0702016-10-13 09:12:37 -07004187 << dex_file_->PrettyMethod(dex_method_idx_, true) << "@" << work_insn_idx_;
Andreas Gampe76bd8802014-12-10 16:43:58 -08004188
Mathieu Chartiere401d142015-04-22 13:56:20 -07004189 ArtMethod* res_method = GetQuickInvokedMethod(inst, work_line_.get(), is_range, false);
Ian Rogers7b078e82014-09-10 14:44:24 -07004190 if (res_method == nullptr) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004191 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer method from " << inst->Name();
Ian Rogers7b078e82014-09-10 14:44:24 -07004192 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004193 }
Andreas Gampe7c038102014-10-27 20:08:46 -07004194 if (FailOrAbort(this, !res_method->IsDirect(), "Quick-invoked method is direct at ",
4195 work_insn_idx_)) {
4196 return nullptr;
4197 }
4198 if (FailOrAbort(this, !res_method->IsStatic(), "Quick-invoked method is static at ",
4199 work_insn_idx_)) {
4200 return nullptr;
4201 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004202
4203 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
4204 // match the call to the signature. Also, we might be calling through an abstract method
4205 // definition (which doesn't have register count values).
Ian Rogers7b078e82014-09-10 14:44:24 -07004206 const RegType& actual_arg_type = work_line_->GetInvocationThis(this, inst, is_range);
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004207 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
Ian Rogers7b078e82014-09-10 14:44:24 -07004208 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004209 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004210 const size_t expected_args = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
4211 /* caught by static verifier */
4212 DCHECK(is_range || expected_args <= 5);
4213 if (expected_args > code_item_->outs_size_) {
4214 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
4215 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
Ian Rogers7b078e82014-09-10 14:44:24 -07004216 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004217 }
4218
4219 /*
4220 * Check the "this" argument, which must be an instance of the class that declared the method.
4221 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
4222 * rigorous check here (which is okay since we have to do it at runtime).
4223 */
David Brazdil68b5c0b2016-01-19 14:25:29 +00004224 // Note: given an uninitialized type, this should always fail. Constructors aren't virtual.
4225 if (actual_arg_type.IsUninitializedTypes() && !res_method->IsConstructor()) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004226 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
Ian Rogers7b078e82014-09-10 14:44:24 -07004227 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004228 }
4229 if (!actual_arg_type.IsZero()) {
4230 mirror::Class* klass = res_method->GetDeclaringClass();
Ian Rogers1ff3c982014-08-12 02:30:58 -07004231 std::string temp;
Ian Rogersd8f69b02014-09-10 21:43:52 +00004232 const RegType& res_method_class =
Andreas Gampef23f33d2015-06-23 14:18:17 -07004233 FromClass(klass->GetDescriptor(&temp), klass, klass->CannotBeAssignedFromOtherTypes());
David Brazdilca3c8c32016-09-06 14:04:48 +01004234 if (!res_method_class.IsAssignableFrom(actual_arg_type, this)) {
David Brazdil68b5c0b2016-01-19 14:25:29 +00004235 Fail(actual_arg_type.IsUninitializedTypes() // Just overcautious - should have never
4236 ? VERIFY_ERROR_BAD_CLASS_HARD // quickened this.
4237 : actual_arg_type.IsUnresolvedTypes()
4238 ? VERIFY_ERROR_NO_CLASS
4239 : VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004240 << "' not instance of '" << res_method_class << "'";
Ian Rogers7b078e82014-09-10 14:44:24 -07004241 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004242 }
4243 }
4244 /*
4245 * Process the target method's signature. This signature may or may not
4246 * have been verified, so we can't assume it's properly formed.
4247 */
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07004248 const DexFile::TypeList* params = res_method->GetParameterTypeList();
Ian Rogers7b078e82014-09-10 14:44:24 -07004249 size_t params_size = params == nullptr ? 0 : params->Size();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004250 uint32_t arg[5];
4251 if (!is_range) {
Ian Rogers29a26482014-05-02 15:27:29 -07004252 inst->GetVarArgs(arg);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004253 }
4254 size_t actual_args = 1;
4255 for (size_t param_index = 0; param_index < params_size; param_index++) {
4256 if (actual_args >= expected_args) {
David Sehr709b0702016-10-13 09:12:37 -07004257 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invalid call to '"
4258 << res_method->PrettyMethod()
Brian Carlstrom93c33962013-07-26 10:37:43 -07004259 << "'. Expected " << expected_args
4260 << " arguments, processing argument " << actual_args
4261 << " (where longs/doubles count twice).";
Ian Rogers7b078e82014-09-10 14:44:24 -07004262 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004263 }
4264 const char* descriptor =
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07004265 res_method->GetTypeDescriptorFromTypeIdx(params->GetTypeItem(param_index).type_idx_);
Ian Rogers7b078e82014-09-10 14:44:24 -07004266 if (descriptor == nullptr) {
David Sehr709b0702016-10-13 09:12:37 -07004267 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of "
4268 << res_method->PrettyMethod()
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004269 << " missing signature component";
Ian Rogers7b078e82014-09-10 14:44:24 -07004270 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004271 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00004272 const RegType& reg_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004273 uint32_t get_reg = is_range ? inst->VRegC_3rc() + actual_args : arg[actual_args];
Ian Rogers7b078e82014-09-10 14:44:24 -07004274 if (!work_line_->VerifyRegisterType(this, get_reg, reg_type)) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004275 return res_method;
4276 }
4277 actual_args = reg_type.IsLongOrDoubleTypes() ? actual_args + 2 : actual_args + 1;
4278 }
4279 if (actual_args != expected_args) {
David Sehr709b0702016-10-13 09:12:37 -07004280 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of "
4281 << res_method->PrettyMethod() << " expected "
4282 << expected_args << " arguments, found " << actual_args;
Ian Rogers7b078e82014-09-10 14:44:24 -07004283 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004284 } else {
4285 return res_method;
4286 }
4287}
4288
Ian Rogers62342ec2013-06-11 10:26:37 -07004289void MethodVerifier::VerifyNewArray(const Instruction* inst, bool is_filled, bool is_range) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02004290 uint32_t type_idx;
4291 if (!is_filled) {
4292 DCHECK_EQ(inst->Opcode(), Instruction::NEW_ARRAY);
4293 type_idx = inst->VRegC_22c();
4294 } else if (!is_range) {
4295 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY);
4296 type_idx = inst->VRegB_35c();
4297 } else {
4298 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY_RANGE);
4299 type_idx = inst->VRegB_3rc();
4300 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00004301 const RegType& res_type = ResolveClassAndCheckAccess(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004302 if (res_type.IsConflict()) { // bad class
4303 DCHECK_NE(failures_.size(), 0U);
Ian Rogers0c4a5062012-02-03 15:18:59 -08004304 } else {
4305 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
4306 if (!res_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07004307 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "new-array on non-array class " << res_type;
Ian Rogers0c4a5062012-02-03 15:18:59 -08004308 } else if (!is_filled) {
4309 /* make sure "size" register is valid type */
Ian Rogers7b078e82014-09-10 14:44:24 -07004310 work_line_->VerifyRegisterType(this, inst->VRegB_22c(), reg_types_.Integer());
Ian Rogers0c4a5062012-02-03 15:18:59 -08004311 /* set register type to array class */
Ian Rogersd8f69b02014-09-10 21:43:52 +00004312 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
Andreas Gampead238ce2015-08-24 21:13:08 -07004313 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_22c(), precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08004314 } else {
Andreas Gampebb18a032016-03-22 20:34:25 -07004315 DCHECK(!res_type.IsUnresolvedMergedReference());
Ian Rogers0c4a5062012-02-03 15:18:59 -08004316 // Verify each register. If "arg_count" is bad, VerifyRegisterType() will run off the end of
4317 // the list and fail. It's legal, if silly, for arg_count to be zero.
Ian Rogersd8f69b02014-09-10 21:43:52 +00004318 const RegType& expected_type = reg_types_.GetComponentType(res_type, GetClassLoader());
Sebastien Hertz5243e912013-05-21 10:55:07 +02004319 uint32_t arg_count = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
4320 uint32_t arg[5];
4321 if (!is_range) {
Ian Rogers29a26482014-05-02 15:27:29 -07004322 inst->GetVarArgs(arg);
Sebastien Hertz5243e912013-05-21 10:55:07 +02004323 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08004324 for (size_t ui = 0; ui < arg_count; ui++) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02004325 uint32_t get_reg = is_range ? inst->VRegC_3rc() + ui : arg[ui];
Ian Rogers7b078e82014-09-10 14:44:24 -07004326 if (!work_line_->VerifyRegisterType(this, get_reg, expected_type)) {
4327 work_line_->SetResultRegisterType(this, reg_types_.Conflict());
Ian Rogers0c4a5062012-02-03 15:18:59 -08004328 return;
4329 }
4330 }
4331 // filled-array result goes into "result" register
Ian Rogersd8f69b02014-09-10 21:43:52 +00004332 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
Ian Rogers7b078e82014-09-10 14:44:24 -07004333 work_line_->SetResultRegisterType(this, precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08004334 }
4335 }
4336}
4337
Sebastien Hertz5243e912013-05-21 10:55:07 +02004338void MethodVerifier::VerifyAGet(const Instruction* inst,
Ian Rogersd8f69b02014-09-10 21:43:52 +00004339 const RegType& insn_type, bool is_primitive) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004340 const RegType& index_type = work_line_->GetRegisterType(this, inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07004341 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07004342 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07004343 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004344 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegB_23x());
Ian Rogers89310de2012-02-01 13:47:30 -08004345 if (array_type.IsZero()) {
Nicolas Geoffray4824c272015-06-24 15:53:03 +01004346 have_pending_runtime_throw_failure_ = true;
Ian Rogers89310de2012-02-01 13:47:30 -08004347 // Null array class; this code path will fail at runtime. Infer a merge-able type from the
4348 // instruction type. TODO: have a proper notion of bottom here.
4349 if (!is_primitive || insn_type.IsCategory1Types()) {
4350 // Reference or category 1
Andreas Gampead238ce2015-08-24 21:13:08 -07004351 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), reg_types_.Zero());
Ian Rogersd81871c2011-10-03 13:57:23 -07004352 } else {
Ian Rogers89310de2012-02-01 13:47:30 -08004353 // Category 2
Ian Rogers7b078e82014-09-10 14:44:24 -07004354 work_line_->SetRegisterTypeWide(this, inst->VRegA_23x(),
4355 reg_types_.FromCat2ConstLo(0, false),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004356 reg_types_.FromCat2ConstHi(0, false));
Ian Rogers89310de2012-02-01 13:47:30 -08004357 }
jeffhaofc3144e2012-02-01 17:21:15 -08004358 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07004359 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aget";
Andreas Gampe8d8fc482016-03-25 16:24:20 -07004360 } else if (array_type.IsUnresolvedMergedReference()) {
Andreas Gampebb18a032016-03-22 20:34:25 -07004361 // Unresolved array types must be reference array types.
4362 if (is_primitive) {
4363 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type
4364 << " source for category 1 aget";
4365 } else {
4366 Fail(VERIFY_ERROR_NO_CLASS) << "cannot verify aget for " << array_type
4367 << " because of missing class";
Andreas Gampe8d8fc482016-03-25 16:24:20 -07004368 // Approximate with java.lang.Object[].
4369 work_line_->SetRegisterType<LockOp::kClear>(this,
4370 inst->VRegA_23x(),
4371 reg_types_.JavaLangObject(false));
Andreas Gampebb18a032016-03-22 20:34:25 -07004372 }
Ian Rogers89310de2012-02-01 13:47:30 -08004373 } else {
4374 /* verify the class */
Ian Rogersd8f69b02014-09-10 21:43:52 +00004375 const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
jeffhaofc3144e2012-02-01 17:21:15 -08004376 if (!component_type.IsReferenceTypes() && !is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07004377 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08004378 << " source for aget-object";
4379 } else if (component_type.IsNonZeroReferenceTypes() && is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07004380 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08004381 << " source for category 1 aget";
4382 } else if (is_primitive && !insn_type.Equals(component_type) &&
4383 !((insn_type.IsInteger() && component_type.IsFloat()) ||
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004384 (insn_type.IsLong() && component_type.IsDouble()))) {
4385 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array type " << array_type
4386 << " incompatible with aget of type " << insn_type;
Ian Rogers89310de2012-02-01 13:47:30 -08004387 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004388 // Use knowledge of the field type which is stronger than the type inferred from the
4389 // instruction, which can't differentiate object types and ints from floats, longs from
4390 // doubles.
4391 if (!component_type.IsLowHalf()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07004392 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), component_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004393 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004394 work_line_->SetRegisterTypeWide(this, inst->VRegA_23x(), component_type,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004395 component_type.HighHalf(&reg_types_));
4396 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004397 }
4398 }
4399 }
4400}
4401
Ian Rogersd8f69b02014-09-10 21:43:52 +00004402void MethodVerifier::VerifyPrimitivePut(const RegType& target_type, const RegType& insn_type,
Jeff Haofe1f7c82013-08-01 14:50:24 -07004403 const uint32_t vregA) {
4404 // Primitive assignability rules are weaker than regular assignability rules.
4405 bool instruction_compatible;
4406 bool value_compatible;
Ian Rogers7b078e82014-09-10 14:44:24 -07004407 const RegType& value_type = work_line_->GetRegisterType(this, vregA);
Jeff Haofe1f7c82013-08-01 14:50:24 -07004408 if (target_type.IsIntegralTypes()) {
Jeff Haoa4647482013-08-06 15:35:47 -07004409 instruction_compatible = target_type.Equals(insn_type);
Jeff Haofe1f7c82013-08-01 14:50:24 -07004410 value_compatible = value_type.IsIntegralTypes();
4411 } else if (target_type.IsFloat()) {
4412 instruction_compatible = insn_type.IsInteger(); // no put-float, so expect put-int
4413 value_compatible = value_type.IsFloatTypes();
4414 } else if (target_type.IsLong()) {
4415 instruction_compatible = insn_type.IsLong();
Andreas Gampe376fa682014-09-07 13:06:12 -07004416 // Additional register check: this is not checked statically (as part of VerifyInstructions),
4417 // as target_type depends on the resolved type of the field.
4418 if (instruction_compatible && work_line_->NumRegs() > vregA + 1) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004419 const RegType& value_type_hi = work_line_->GetRegisterType(this, vregA + 1);
Andreas Gampe376fa682014-09-07 13:06:12 -07004420 value_compatible = value_type.IsLongTypes() && value_type.CheckWidePair(value_type_hi);
4421 } else {
4422 value_compatible = false;
4423 }
Jeff Haofe1f7c82013-08-01 14:50:24 -07004424 } else if (target_type.IsDouble()) {
4425 instruction_compatible = insn_type.IsLong(); // no put-double, so expect put-long
Andreas Gampe376fa682014-09-07 13:06:12 -07004426 // Additional register check: this is not checked statically (as part of VerifyInstructions),
4427 // as target_type depends on the resolved type of the field.
4428 if (instruction_compatible && work_line_->NumRegs() > vregA + 1) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004429 const RegType& value_type_hi = work_line_->GetRegisterType(this, vregA + 1);
Andreas Gampe376fa682014-09-07 13:06:12 -07004430 value_compatible = value_type.IsDoubleTypes() && value_type.CheckWidePair(value_type_hi);
4431 } else {
4432 value_compatible = false;
4433 }
Jeff Haofe1f7c82013-08-01 14:50:24 -07004434 } else {
4435 instruction_compatible = false; // reference with primitive store
4436 value_compatible = false; // unused
4437 }
4438 if (!instruction_compatible) {
4439 // This is a global failure rather than a class change failure as the instructions and
4440 // the descriptors for the type should have been consistent within the same file at
4441 // compile time.
4442 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "put insn has type '" << insn_type
4443 << "' but expected type '" << target_type << "'";
4444 return;
4445 }
4446 if (!value_compatible) {
4447 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
4448 << " of type " << value_type << " but expected " << target_type << " for put";
4449 return;
4450 }
4451}
4452
Sebastien Hertz5243e912013-05-21 10:55:07 +02004453void MethodVerifier::VerifyAPut(const Instruction* inst,
Ian Rogersd8f69b02014-09-10 21:43:52 +00004454 const RegType& insn_type, bool is_primitive) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004455 const RegType& index_type = work_line_->GetRegisterType(this, inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07004456 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07004457 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07004458 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004459 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegB_23x());
Ian Rogers89310de2012-02-01 13:47:30 -08004460 if (array_type.IsZero()) {
Nicolas Geoffray66389fb2015-06-19 10:35:42 +01004461 // Null array type; this code path will fail at runtime.
4462 // Still check that the given value matches the instruction's type.
Andreas Gampe4bf4c782015-08-14 14:07:43 -07004463 // Note: this is, as usual, complicated by the fact the the instruction isn't fully typed
4464 // and fits multiple register types.
4465 const RegType* modified_reg_type = &insn_type;
4466 if ((modified_reg_type == &reg_types_.Integer()) ||
4467 (modified_reg_type == &reg_types_.LongLo())) {
4468 // May be integer or float | long or double. Overwrite insn_type accordingly.
4469 const RegType& value_type = work_line_->GetRegisterType(this, inst->VRegA_23x());
4470 if (modified_reg_type == &reg_types_.Integer()) {
4471 if (&value_type == &reg_types_.Float()) {
4472 modified_reg_type = &value_type;
4473 }
4474 } else {
4475 if (&value_type == &reg_types_.DoubleLo()) {
4476 modified_reg_type = &value_type;
4477 }
4478 }
4479 }
4480 work_line_->VerifyRegisterType(this, inst->VRegA_23x(), *modified_reg_type);
jeffhaofc3144e2012-02-01 17:21:15 -08004481 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07004482 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aput";
Andreas Gampe8d8fc482016-03-25 16:24:20 -07004483 } else if (array_type.IsUnresolvedMergedReference()) {
Andreas Gampebb18a032016-03-22 20:34:25 -07004484 // Unresolved array types must be reference array types.
4485 if (is_primitive) {
4486 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "put insn has type '" << insn_type
4487 << "' but unresolved type '" << array_type << "'";
4488 } else {
4489 Fail(VERIFY_ERROR_NO_CLASS) << "cannot verify aput for " << array_type
4490 << " because of missing class";
4491 }
Ian Rogers89310de2012-02-01 13:47:30 -08004492 } else {
Ian Rogersd8f69b02014-09-10 21:43:52 +00004493 const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
Jeff Haofe1f7c82013-08-01 14:50:24 -07004494 const uint32_t vregA = inst->VRegA_23x();
Jeff Haob24b4a72013-07-31 13:47:31 -07004495 if (is_primitive) {
Jeff Haofe1f7c82013-08-01 14:50:24 -07004496 VerifyPrimitivePut(component_type, insn_type, vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07004497 } else {
Jeff Haob24b4a72013-07-31 13:47:31 -07004498 if (!component_type.IsReferenceTypes()) {
4499 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
4500 << " source for aput-object";
4501 } else {
4502 // The instruction agrees with the type of array, confirm the value to be stored does too
4503 // Note: we use the instruction type (rather than the component type) for aput-object as
4504 // incompatible classes will be caught at runtime as an array store exception
Ian Rogers7b078e82014-09-10 14:44:24 -07004505 work_line_->VerifyRegisterType(this, vregA, insn_type);
Jeff Haob24b4a72013-07-31 13:47:31 -07004506 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004507 }
4508 }
4509 }
4510}
4511
Mathieu Chartierc7853442015-03-27 14:35:38 -07004512ArtField* MethodVerifier::GetStaticField(int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08004513 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
4514 // Check access to class
Ian Rogersd8f69b02014-09-10 21:43:52 +00004515 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Brian Carlstrom7934ac22013-07-26 10:54:15 -07004516 if (klass_type.IsConflict()) { // bad class
Ian Rogersad0b3a32012-04-16 14:50:24 -07004517 AppendToLastFailMessage(StringPrintf(" in attempt to access static field %d (%s) in %s",
4518 field_idx, dex_file_->GetFieldName(field_id),
4519 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers7b078e82014-09-10 14:44:24 -07004520 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08004521 }
Elliott Hughesb25c3f62012-03-26 16:35:06 -07004522 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004523 return nullptr; // Can't resolve Class so no more to do here, will do checking at runtime.
Ian Rogers90040192011-12-16 08:54:29 -08004524 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07004525 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
David Brazdilca3c8c32016-09-06 14:04:48 +01004526 ArtField* field = class_linker->ResolveFieldJLS(*dex_file_, field_idx, dex_cache_, class_loader_);
4527
4528 // Record result of the field resolution attempt.
4529 VerifierDeps::MaybeRecordFieldResolution(*dex_file_, field_idx, field);
4530
Ian Rogers7b078e82014-09-10 14:44:24 -07004531 if (field == nullptr) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07004532 VLOG(verifier) << "Unable to resolve static field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07004533 << dex_file_->GetFieldName(field_id) << ") in "
4534 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogers7b078e82014-09-10 14:44:24 -07004535 DCHECK(self_->IsExceptionPending());
4536 self_->ClearException();
4537 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004538 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
4539 field->GetAccessFlags())) {
David Sehr709b0702016-10-13 09:12:37 -07004540 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access static field " << field->PrettyField()
Ian Rogersad0b3a32012-04-16 14:50:24 -07004541 << " from " << GetDeclaringClass();
Ian Rogers7b078e82014-09-10 14:44:24 -07004542 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07004543 } else if (!field->IsStatic()) {
David Sehr709b0702016-10-13 09:12:37 -07004544 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << field->PrettyField() << " to be static";
Ian Rogers7b078e82014-09-10 14:44:24 -07004545 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07004546 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07004547 return field;
Ian Rogersd81871c2011-10-03 13:57:23 -07004548}
4549
Mathieu Chartierc7853442015-03-27 14:35:38 -07004550ArtField* MethodVerifier::GetInstanceField(const RegType& obj_type, int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08004551 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
Aart Bik31883642016-06-06 15:02:44 -07004552 // Check access to class.
Ian Rogersd8f69b02014-09-10 21:43:52 +00004553 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004554 if (klass_type.IsConflict()) {
4555 AppendToLastFailMessage(StringPrintf(" in attempt to access instance field %d (%s) in %s",
4556 field_idx, dex_file_->GetFieldName(field_id),
4557 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers7b078e82014-09-10 14:44:24 -07004558 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08004559 }
jeffhao8cd6dda2012-02-22 10:15:34 -08004560 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004561 return nullptr; // Can't resolve Class so no more to do here
Ian Rogers90040192011-12-16 08:54:29 -08004562 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07004563 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
David Brazdilca3c8c32016-09-06 14:04:48 +01004564 ArtField* field = class_linker->ResolveFieldJLS(*dex_file_, field_idx, dex_cache_, class_loader_);
4565
4566 // Record result of the field resolution attempt.
4567 VerifierDeps::MaybeRecordFieldResolution(*dex_file_, field_idx, field);
4568
Ian Rogers7b078e82014-09-10 14:44:24 -07004569 if (field == nullptr) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07004570 VLOG(verifier) << "Unable to resolve instance field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07004571 << dex_file_->GetFieldName(field_id) << ") in "
4572 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogers7b078e82014-09-10 14:44:24 -07004573 DCHECK(self_->IsExceptionPending());
4574 self_->ClearException();
4575 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07004576 } else if (obj_type.IsZero()) {
Aart Bik31883642016-06-06 15:02:44 -07004577 // Cannot infer and check type, however, access will cause null pointer exception.
4578 // Fall through into a few last soft failure checks below.
Stephen Kyle695c5982014-08-22 15:03:07 +01004579 } else if (!obj_type.IsReferenceTypes()) {
Aart Bik31883642016-06-06 15:02:44 -07004580 // Trying to read a field from something that isn't a reference.
Stephen Kyle695c5982014-08-22 15:03:07 +01004581 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance field access on object that has "
4582 << "non-reference type " << obj_type;
Ian Rogers7b078e82014-09-10 14:44:24 -07004583 return nullptr;
Ian Rogerse1758fe2012-04-19 11:31:15 -07004584 } else {
David Brazdil0d638bb2016-07-27 15:29:25 +01004585 std::string temp;
Mathieu Chartier3398c782016-09-30 10:27:43 -07004586 ObjPtr<mirror::Class> klass = field->GetDeclaringClass();
Ian Rogersd8f69b02014-09-10 21:43:52 +00004587 const RegType& field_klass =
Mathieu Chartier3398c782016-09-30 10:27:43 -07004588 FromClass(klass->GetDescriptor(&temp),
Mathieu Chartier1cc62e42016-10-03 18:01:28 -07004589 klass.Ptr(),
Mathieu Chartier3398c782016-09-30 10:27:43 -07004590 klass->CannotBeAssignedFromOtherTypes());
David Brazdil68b5c0b2016-01-19 14:25:29 +00004591 if (obj_type.IsUninitializedTypes()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07004592 // Field accesses through uninitialized references are only allowable for constructors where
David Brazdil68b5c0b2016-01-19 14:25:29 +00004593 // the field is declared in this class.
4594 // Note: this IsConstructor check is technically redundant, as UninitializedThis should only
4595 // appear in constructors.
4596 if (!obj_type.IsUninitializedThisReference() ||
4597 !IsConstructor() ||
4598 !field_klass.Equals(GetDeclaringClass())) {
David Sehr709b0702016-10-13 09:12:37 -07004599 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "cannot access instance field " << field->PrettyField()
David Brazdil68b5c0b2016-01-19 14:25:29 +00004600 << " of a not fully initialized object within the context"
David Sehr709b0702016-10-13 09:12:37 -07004601 << " of " << dex_file_->PrettyMethod(dex_method_idx_);
David Brazdil68b5c0b2016-01-19 14:25:29 +00004602 return nullptr;
4603 }
David Brazdilca3c8c32016-09-06 14:04:48 +01004604 } else if (!field_klass.IsAssignableFrom(obj_type, this)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07004605 // Trying to access C1.field1 using reference of type C2, which is neither C1 or a sub-class
4606 // of C1. For resolution to occur the declared class of the field must be compatible with
4607 // obj_type, we've discovered this wasn't so, so report the field didn't exist.
Andreas Gampe66596242016-04-14 10:55:04 -07004608 VerifyError type;
4609 bool is_aot = Runtime::Current()->IsAotCompiler();
4610 if (is_aot && (field_klass.IsUnresolvedTypes() || obj_type.IsUnresolvedTypes())) {
4611 // Compiler & unresolved types involved, retry at runtime.
4612 type = VerifyError::VERIFY_ERROR_NO_CLASS;
4613 } else {
Andreas Gampe8f4ade02016-04-15 10:09:16 -07004614 // Classes known (resolved; and thus assignability check is precise), or we are at runtime
4615 // and still missing classes. This is a hard failure.
Andreas Gampe66596242016-04-14 10:55:04 -07004616 type = VerifyError::VERIFY_ERROR_BAD_CLASS_HARD;
4617 }
David Sehr709b0702016-10-13 09:12:37 -07004618 Fail(type) << "cannot access instance field " << field->PrettyField()
Andreas Gampe66596242016-04-14 10:55:04 -07004619 << " from object of type " << obj_type;
Ian Rogers7b078e82014-09-10 14:44:24 -07004620 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004621 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004622 }
Aart Bik31883642016-06-06 15:02:44 -07004623
4624 // Few last soft failure checks.
4625 if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
4626 field->GetAccessFlags())) {
David Sehr709b0702016-10-13 09:12:37 -07004627 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access instance field " << field->PrettyField()
Aart Bik31883642016-06-06 15:02:44 -07004628 << " from " << GetDeclaringClass();
4629 return nullptr;
4630 } else if (field->IsStatic()) {
David Sehr709b0702016-10-13 09:12:37 -07004631 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << field->PrettyField()
Aart Bik31883642016-06-06 15:02:44 -07004632 << " to not be static";
4633 return nullptr;
4634 }
4635
4636 return field;
Ian Rogersd81871c2011-10-03 13:57:23 -07004637}
4638
Andreas Gampe896df402014-10-20 22:25:29 -07004639template <MethodVerifier::FieldAccessType kAccType>
4640void MethodVerifier::VerifyISFieldAccess(const Instruction* inst, const RegType& insn_type,
4641 bool is_primitive, bool is_static) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02004642 uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
Mathieu Chartierc7853442015-03-27 14:35:38 -07004643 ArtField* field;
Ian Rogersb94a27b2011-10-26 00:33:41 -07004644 if (is_static) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07004645 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07004646 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004647 const RegType& object_type = work_line_->GetRegisterType(this, inst->VRegB_22c());
David Brazdil68b5c0b2016-01-19 14:25:29 +00004648
4649 // One is not allowed to access fields on uninitialized references, except to write to
4650 // fields in the constructor (before calling another constructor).
4651 // GetInstanceField does an assignability check which will fail for uninitialized types.
4652 // We thus modify the type if the uninitialized reference is a "this" reference (this also
4653 // checks at the same time that we're verifying a constructor).
4654 bool should_adjust = (kAccType == FieldAccessType::kAccPut) &&
4655 object_type.IsUninitializedThisReference();
4656 const RegType& adjusted_type = should_adjust
4657 ? GetRegTypeCache()->FromUninitialized(object_type)
4658 : object_type;
4659 field = GetInstanceField(adjusted_type, field_idx);
Andreas Gampe896df402014-10-20 22:25:29 -07004660 if (UNLIKELY(have_pending_hard_failure_)) {
4661 return;
4662 }
Alex Light4a2c8fc2016-02-12 11:01:54 -08004663 if (should_adjust) {
4664 if (field == nullptr) {
4665 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "Might be accessing a superclass instance field prior "
4666 << "to the superclass being initialized in "
David Sehr709b0702016-10-13 09:12:37 -07004667 << dex_file_->PrettyMethod(dex_method_idx_);
Alex Light4a2c8fc2016-02-12 11:01:54 -08004668 } else if (field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
4669 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "cannot access superclass instance field "
David Sehr709b0702016-10-13 09:12:37 -07004670 << field->PrettyField() << " of a not fully initialized "
Alex Light4a2c8fc2016-02-12 11:01:54 -08004671 << "object within the context of "
David Sehr709b0702016-10-13 09:12:37 -07004672 << dex_file_->PrettyMethod(dex_method_idx_);
Alex Light4a2c8fc2016-02-12 11:01:54 -08004673 return;
4674 }
4675 }
Ian Rogersb94a27b2011-10-26 00:33:41 -07004676 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00004677 const RegType* field_type = nullptr;
Ian Rogers7b078e82014-09-10 14:44:24 -07004678 if (field != nullptr) {
Andreas Gampe896df402014-10-20 22:25:29 -07004679 if (kAccType == FieldAccessType::kAccPut) {
4680 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
David Sehr709b0702016-10-13 09:12:37 -07004681 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << field->PrettyField()
Andreas Gampe896df402014-10-20 22:25:29 -07004682 << " from other class " << GetDeclaringClass();
Aart Bikc2bc2652016-05-23 14:58:49 -07004683 // Keep hunting for possible hard fails.
Andreas Gampe896df402014-10-20 22:25:29 -07004684 }
4685 }
4686
Mathieu Chartier3398c782016-09-30 10:27:43 -07004687 ObjPtr<mirror::Class> field_type_class =
Mathieu Chartierc7853442015-03-27 14:35:38 -07004688 can_load_classes_ ? field->GetType<true>() : field->GetType<false>();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004689 if (field_type_class != nullptr) {
Mathieu Chartier3398c782016-09-30 10:27:43 -07004690 field_type = &FromClass(field->GetTypeDescriptor(),
Mathieu Chartier1cc62e42016-10-03 18:01:28 -07004691 field_type_class.Ptr(),
Andreas Gampef23f33d2015-06-23 14:18:17 -07004692 field_type_class->CannotBeAssignedFromOtherTypes());
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08004693 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004694 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
4695 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004696 }
Ian Rogers0d604842012-04-16 14:50:24 -07004697 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004698 if (field_type == nullptr) {
4699 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
4700 const char* descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
Mathieu Chartierbf99f772014-08-23 16:37:27 -07004701 field_type = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004702 }
Sebastien Hertz757b3042014-03-28 14:34:28 +01004703 DCHECK(field_type != nullptr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02004704 const uint32_t vregA = (is_static) ? inst->VRegA_21c() : inst->VRegA_22c();
Andreas Gampe896df402014-10-20 22:25:29 -07004705 static_assert(kAccType == FieldAccessType::kAccPut || kAccType == FieldAccessType::kAccGet,
4706 "Unexpected third access type");
4707 if (kAccType == FieldAccessType::kAccPut) {
4708 // sput or iput.
4709 if (is_primitive) {
4710 VerifyPrimitivePut(*field_type, insn_type, vregA);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004711 } else {
David Brazdilca3c8c32016-09-06 14:04:48 +01004712 if (!insn_type.IsAssignableFrom(*field_type, this)) {
Vladimir Marko414000e2015-06-23 17:45:21 +01004713 // If the field type is not a reference, this is a global failure rather than
4714 // a class change failure as the instructions and the descriptors for the type
4715 // should have been consistent within the same file at compile time.
4716 VerifyError error = field_type->IsReferenceTypes() ? VERIFY_ERROR_BAD_CLASS_SOFT
4717 : VERIFY_ERROR_BAD_CLASS_HARD;
David Sehr709b0702016-10-13 09:12:37 -07004718 Fail(error) << "expected field " << ArtField::PrettyField(field)
Vladimir Marko414000e2015-06-23 17:45:21 +01004719 << " to be compatible with type '" << insn_type
4720 << "' but found type '" << *field_type
4721 << "' in put-object";
Andreas Gampe896df402014-10-20 22:25:29 -07004722 return;
4723 }
4724 work_line_->VerifyRegisterType(this, vregA, *field_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004725 }
Andreas Gampe896df402014-10-20 22:25:29 -07004726 } else if (kAccType == FieldAccessType::kAccGet) {
4727 // sget or iget.
4728 if (is_primitive) {
4729 if (field_type->Equals(insn_type) ||
4730 (field_type->IsFloat() && insn_type.IsInteger()) ||
4731 (field_type->IsDouble() && insn_type.IsLong())) {
4732 // expected that read is of the correct primitive type or that int reads are reading
4733 // floats or long reads are reading doubles
4734 } else {
4735 // This is a global failure rather than a class change failure as the instructions and
4736 // the descriptors for the type should have been consistent within the same file at
4737 // compile time
David Sehr709b0702016-10-13 09:12:37 -07004738 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << ArtField::PrettyField(field)
Andreas Gampe896df402014-10-20 22:25:29 -07004739 << " to be of type '" << insn_type
4740 << "' but found type '" << *field_type << "' in get";
4741 return;
4742 }
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08004743 } else {
David Brazdilca3c8c32016-09-06 14:04:48 +01004744 if (!insn_type.IsAssignableFrom(*field_type, this)) {
Vladimir Marko414000e2015-06-23 17:45:21 +01004745 // If the field type is not a reference, this is a global failure rather than
4746 // a class change failure as the instructions and the descriptors for the type
4747 // should have been consistent within the same file at compile time.
4748 VerifyError error = field_type->IsReferenceTypes() ? VERIFY_ERROR_BAD_CLASS_SOFT
4749 : VERIFY_ERROR_BAD_CLASS_HARD;
David Sehr709b0702016-10-13 09:12:37 -07004750 Fail(error) << "expected field " << ArtField::PrettyField(field)
Vladimir Marko414000e2015-06-23 17:45:21 +01004751 << " to be compatible with type '" << insn_type
4752 << "' but found type '" << *field_type
4753 << "' in get-object";
Andreas Gampe890da292015-07-06 17:20:18 -07004754 if (error != VERIFY_ERROR_BAD_CLASS_HARD) {
Andreas Gampead238ce2015-08-24 21:13:08 -07004755 work_line_->SetRegisterType<LockOp::kClear>(this, vregA, reg_types_.Conflict());
Andreas Gampe890da292015-07-06 17:20:18 -07004756 }
Andreas Gampe896df402014-10-20 22:25:29 -07004757 return;
4758 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004759 }
Andreas Gampe896df402014-10-20 22:25:29 -07004760 if (!field_type->IsLowHalf()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07004761 work_line_->SetRegisterType<LockOp::kClear>(this, vregA, *field_type);
Andreas Gampe896df402014-10-20 22:25:29 -07004762 } else {
4763 work_line_->SetRegisterTypeWide(this, vregA, *field_type, field_type->HighHalf(&reg_types_));
4764 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07004765 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004766 LOG(FATAL) << "Unexpected case.";
Ian Rogersd81871c2011-10-03 13:57:23 -07004767 }
4768}
4769
Mathieu Chartierc7853442015-03-27 14:35:38 -07004770ArtField* MethodVerifier::GetQuickFieldAccess(const Instruction* inst,
Ian Rogers98379392014-02-24 16:53:16 -08004771 RegisterLine* reg_line) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08004772 DCHECK(IsInstructionIGetQuickOrIPutQuick(inst->Opcode())) << inst->Opcode();
Ian Rogers7b078e82014-09-10 14:44:24 -07004773 const RegType& object_type = reg_line->GetRegisterType(this, inst->VRegB_22c());
Ian Rogers9bc54402014-04-17 16:40:01 -07004774 if (!object_type.HasClass()) {
4775 VLOG(verifier) << "Failed to get mirror::Class* from '" << object_type << "'";
4776 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004777 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004778 uint32_t field_offset = static_cast<uint32_t>(inst->VRegC_22c());
Mathieu Chartierc7853442015-03-27 14:35:38 -07004779 ArtField* const f = ArtField::FindInstanceFieldWithOffset(object_type.GetClass(), field_offset);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08004780 DCHECK_EQ(f->GetOffset().Uint32Value(), field_offset);
Sebastien Hertz479fc1e2014-04-04 17:51:34 +02004781 if (f == nullptr) {
4782 VLOG(verifier) << "Failed to find instance field at offset '" << field_offset
David Sehr709b0702016-10-13 09:12:37 -07004783 << "' from '" << mirror::Class::PrettyDescriptor(object_type.GetClass()) << "'";
Sebastien Hertz479fc1e2014-04-04 17:51:34 +02004784 }
4785 return f;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004786}
4787
Andreas Gampe896df402014-10-20 22:25:29 -07004788template <MethodVerifier::FieldAccessType kAccType>
4789void MethodVerifier::VerifyQuickFieldAccess(const Instruction* inst, const RegType& insn_type,
4790 bool is_primitive) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07004791 DCHECK(Runtime::Current()->IsStarted() || verify_to_dump_);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004792
Mathieu Chartierc7853442015-03-27 14:35:38 -07004793 ArtField* field = GetQuickFieldAccess(inst, work_line_.get());
Ian Rogers7b078e82014-09-10 14:44:24 -07004794 if (field == nullptr) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004795 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field from " << inst->Name();
4796 return;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004797 }
Andreas Gampe896df402014-10-20 22:25:29 -07004798
4799 // For an IPUT_QUICK, we now test for final flag of the field.
4800 if (kAccType == FieldAccessType::kAccPut) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004801 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
David Sehr709b0702016-10-13 09:12:37 -07004802 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << field->PrettyField()
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004803 << " from other class " << GetDeclaringClass();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004804 return;
4805 }
4806 }
Andreas Gampe896df402014-10-20 22:25:29 -07004807
4808 // Get the field type.
4809 const RegType* field_type;
4810 {
Mathieu Chartier3398c782016-09-30 10:27:43 -07004811 ObjPtr<mirror::Class> field_type_class = can_load_classes_ ? field->GetType<true>() :
Mathieu Chartierc7853442015-03-27 14:35:38 -07004812 field->GetType<false>();
Andreas Gampe896df402014-10-20 22:25:29 -07004813
4814 if (field_type_class != nullptr) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07004815 field_type = &FromClass(field->GetTypeDescriptor(),
Mathieu Chartier1cc62e42016-10-03 18:01:28 -07004816 field_type_class.Ptr(),
Andreas Gampef23f33d2015-06-23 14:18:17 -07004817 field_type_class->CannotBeAssignedFromOtherTypes());
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004818 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004819 Thread* self = Thread::Current();
4820 DCHECK(!can_load_classes_ || self->IsExceptionPending());
4821 self->ClearException();
4822 field_type = &reg_types_.FromDescriptor(field->GetDeclaringClass()->GetClassLoader(),
Mathieu Chartierde40d472015-10-15 17:47:48 -07004823 field->GetTypeDescriptor(),
4824 false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004825 }
Andreas Gampe896df402014-10-20 22:25:29 -07004826 if (field_type == nullptr) {
4827 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field type from " << inst->Name();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004828 return;
4829 }
Andreas Gampe896df402014-10-20 22:25:29 -07004830 }
4831
4832 const uint32_t vregA = inst->VRegA_22c();
4833 static_assert(kAccType == FieldAccessType::kAccPut || kAccType == FieldAccessType::kAccGet,
4834 "Unexpected third access type");
4835 if (kAccType == FieldAccessType::kAccPut) {
4836 if (is_primitive) {
4837 // Primitive field assignability rules are weaker than regular assignability rules
4838 bool instruction_compatible;
4839 bool value_compatible;
4840 const RegType& value_type = work_line_->GetRegisterType(this, vregA);
4841 if (field_type->IsIntegralTypes()) {
4842 instruction_compatible = insn_type.IsIntegralTypes();
4843 value_compatible = value_type.IsIntegralTypes();
4844 } else if (field_type->IsFloat()) {
4845 instruction_compatible = insn_type.IsInteger(); // no [is]put-float, so expect [is]put-int
4846 value_compatible = value_type.IsFloatTypes();
4847 } else if (field_type->IsLong()) {
4848 instruction_compatible = insn_type.IsLong();
4849 value_compatible = value_type.IsLongTypes();
4850 } else if (field_type->IsDouble()) {
4851 instruction_compatible = insn_type.IsLong(); // no [is]put-double, so expect [is]put-long
4852 value_compatible = value_type.IsDoubleTypes();
4853 } else {
4854 instruction_compatible = false; // reference field with primitive store
4855 value_compatible = false; // unused
4856 }
4857 if (!instruction_compatible) {
4858 // This is a global failure rather than a class change failure as the instructions and
4859 // the descriptors for the type should have been consistent within the same file at
4860 // compile time
David Sehr709b0702016-10-13 09:12:37 -07004861 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << ArtField::PrettyField(field)
Andreas Gampe896df402014-10-20 22:25:29 -07004862 << " to be of type '" << insn_type
4863 << "' but found type '" << *field_type
4864 << "' in put";
4865 return;
4866 }
4867 if (!value_compatible) {
4868 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
4869 << " of type " << value_type
4870 << " but expected " << *field_type
David Sehr709b0702016-10-13 09:12:37 -07004871 << " for store to " << ArtField::PrettyField(field) << " in put";
Andreas Gampe896df402014-10-20 22:25:29 -07004872 return;
4873 }
4874 } else {
David Brazdilca3c8c32016-09-06 14:04:48 +01004875 if (!insn_type.IsAssignableFrom(*field_type, this)) {
David Sehr709b0702016-10-13 09:12:37 -07004876 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << ArtField::PrettyField(field)
Andreas Gampe896df402014-10-20 22:25:29 -07004877 << " to be compatible with type '" << insn_type
4878 << "' but found type '" << *field_type
4879 << "' in put-object";
4880 return;
4881 }
4882 work_line_->VerifyRegisterType(this, vregA, *field_type);
4883 }
4884 } else if (kAccType == FieldAccessType::kAccGet) {
4885 if (is_primitive) {
4886 if (field_type->Equals(insn_type) ||
4887 (field_type->IsFloat() && insn_type.IsIntegralTypes()) ||
4888 (field_type->IsDouble() && insn_type.IsLongTypes())) {
4889 // expected that read is of the correct primitive type or that int reads are reading
4890 // floats or long reads are reading doubles
4891 } else {
4892 // This is a global failure rather than a class change failure as the instructions and
4893 // the descriptors for the type should have been consistent within the same file at
4894 // compile time
David Sehr709b0702016-10-13 09:12:37 -07004895 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << ArtField::PrettyField(field)
Andreas Gampe896df402014-10-20 22:25:29 -07004896 << " to be of type '" << insn_type
4897 << "' but found type '" << *field_type << "' in Get";
4898 return;
4899 }
4900 } else {
David Brazdilca3c8c32016-09-06 14:04:48 +01004901 if (!insn_type.IsAssignableFrom(*field_type, this)) {
David Sehr709b0702016-10-13 09:12:37 -07004902 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << ArtField::PrettyField(field)
Andreas Gampe896df402014-10-20 22:25:29 -07004903 << " to be compatible with type '" << insn_type
4904 << "' but found type '" << *field_type
4905 << "' in get-object";
Andreas Gampead238ce2015-08-24 21:13:08 -07004906 work_line_->SetRegisterType<LockOp::kClear>(this, vregA, reg_types_.Conflict());
Andreas Gampe896df402014-10-20 22:25:29 -07004907 return;
4908 }
4909 }
4910 if (!field_type->IsLowHalf()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07004911 work_line_->SetRegisterType<LockOp::kClear>(this, vregA, *field_type);
Andreas Gampe896df402014-10-20 22:25:29 -07004912 } else {
4913 work_line_->SetRegisterTypeWide(this, vregA, *field_type, field_type->HighHalf(&reg_types_));
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004914 }
4915 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004916 LOG(FATAL) << "Unexpected case.";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004917 }
4918}
4919
Ian Rogers776ac1f2012-04-13 23:36:36 -07004920bool MethodVerifier::CheckNotMoveException(const uint16_t* insns, int insn_idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004921 if ((insns[insn_idx] & 0xff) == Instruction::MOVE_EXCEPTION) {
jeffhaod5347e02012-03-22 17:25:05 -07004922 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-exception";
Ian Rogersd81871c2011-10-03 13:57:23 -07004923 return false;
4924 }
4925 return true;
4926}
4927
Stephen Kyle9bc61992014-09-22 13:53:15 +01004928bool MethodVerifier::CheckNotMoveResult(const uint16_t* insns, int insn_idx) {
4929 if (((insns[insn_idx] & 0xff) >= Instruction::MOVE_RESULT) &&
4930 ((insns[insn_idx] & 0xff) <= Instruction::MOVE_RESULT_OBJECT)) {
4931 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-result*";
4932 return false;
4933 }
4934 return true;
4935}
4936
4937bool MethodVerifier::CheckNotMoveExceptionOrMoveResult(const uint16_t* insns, int insn_idx) {
4938 return (CheckNotMoveException(insns, insn_idx) && CheckNotMoveResult(insns, insn_idx));
4939}
4940
Ian Rogersebbdd872014-07-07 23:53:08 -07004941bool MethodVerifier::UpdateRegisters(uint32_t next_insn, RegisterLine* merge_line,
4942 bool update_merge_line) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004943 bool changed = true;
4944 RegisterLine* target_line = reg_table_.GetLine(next_insn);
Mathieu Chartierde40d472015-10-15 17:47:48 -07004945 if (!GetInstructionFlags(next_insn).IsVisitedOrChanged()) {
jeffhaobdb76512011-09-07 11:43:16 -07004946 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07004947 * We haven't processed this instruction before, and we haven't touched the registers here, so
4948 * there's nothing to "merge". Copy the registers over and mark it as changed. (This is the
4949 * only way a register can transition out of "unknown", so this is not just an optimization.)
jeffhaobdb76512011-09-07 11:43:16 -07004950 */
Andreas Gampea727e372015-08-25 09:22:37 -07004951 target_line->CopyFromLine(merge_line);
Mathieu Chartierde40d472015-10-15 17:47:48 -07004952 if (GetInstructionFlags(next_insn).IsReturn()) {
Jeff Haob24b4a72013-07-31 13:47:31 -07004953 // Verify that the monitor stack is empty on return.
Andreas Gampea727e372015-08-25 09:22:37 -07004954 merge_line->VerifyMonitorStackEmpty(this);
4955
Ian Rogersb8c78592013-07-25 23:52:52 +00004956 // For returns we only care about the operand to the return, all other registers are dead.
4957 // Initialize them as conflicts so they don't add to GC and deoptimization information.
4958 const Instruction* ret_inst = Instruction::At(code_item_->insns_ + next_insn);
Andreas Gampea727e372015-08-25 09:22:37 -07004959 AdjustReturnLine(this, ret_inst, target_line);
Aart Bik31883642016-06-06 15:02:44 -07004960 // Directly bail if a hard failure was found.
Aart Bikb0526322016-06-01 14:06:00 -07004961 if (have_pending_hard_failure_) {
4962 return false;
4963 }
Ian Rogersb8c78592013-07-25 23:52:52 +00004964 }
jeffhaobdb76512011-09-07 11:43:16 -07004965 } else {
Mathieu Chartier361e04a2016-02-16 14:06:35 -08004966 RegisterLineArenaUniquePtr copy;
Andreas Gampeec6e6c12015-11-05 20:39:56 -08004967 if (kDebugVerify) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07004968 copy.reset(RegisterLine::Create(target_line->NumRegs(), this));
Ian Rogers7b0c5b42012-02-16 15:29:07 -08004969 copy->CopyFromLine(target_line);
4970 }
Ian Rogers7b078e82014-09-10 14:44:24 -07004971 changed = target_line->MergeRegisters(this, merge_line);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004972 if (have_pending_hard_failure_) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004973 return false;
jeffhaobdb76512011-09-07 11:43:16 -07004974 }
Andreas Gampeec6e6c12015-11-05 20:39:56 -08004975 if (kDebugVerify && changed) {
Elliott Hughes398f64b2012-03-26 18:05:48 -07004976 LogVerifyInfo() << "Merging at [" << reinterpret_cast<void*>(work_insn_idx_) << "]"
Elliott Hughesc073b072012-05-24 19:29:17 -07004977 << " to [" << reinterpret_cast<void*>(next_insn) << "]: " << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07004978 << copy->Dump(this) << " MERGE\n"
4979 << merge_line->Dump(this) << " ==\n"
4980 << target_line->Dump(this) << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07004981 }
Ian Rogersebbdd872014-07-07 23:53:08 -07004982 if (update_merge_line && changed) {
4983 merge_line->CopyFromLine(target_line);
4984 }
jeffhaobdb76512011-09-07 11:43:16 -07004985 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004986 if (changed) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07004987 GetInstructionFlags(next_insn).SetChanged();
jeffhaobdb76512011-09-07 11:43:16 -07004988 }
4989 return true;
4990}
4991
Ian Rogers7b3ddd22013-02-21 15:19:52 -08004992InstructionFlags* MethodVerifier::CurrentInsnFlags() {
Mathieu Chartierde40d472015-10-15 17:47:48 -07004993 return &GetInstructionFlags(work_insn_idx_);
Ian Rogers776ac1f2012-04-13 23:36:36 -07004994}
4995
Ian Rogersd8f69b02014-09-10 21:43:52 +00004996const RegType& MethodVerifier::GetMethodReturnType() {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004997 if (return_type_ == nullptr) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07004998 if (mirror_method_ != nullptr) {
Andreas Gampe542451c2016-07-26 09:02:02 -07004999 PointerSize pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
Vladimir Marko05792b92015-08-03 11:56:49 +01005000 mirror::Class* return_type_class = mirror_method_->GetReturnType(can_load_classes_,
5001 pointer_size);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07005002 if (return_type_class != nullptr) {
Andreas Gampef23f33d2015-06-23 14:18:17 -07005003 return_type_ = &FromClass(mirror_method_->GetReturnTypeDescriptor(),
5004 return_type_class,
5005 return_type_class->CannotBeAssignedFromOtherTypes());
Ian Rogersfc0e94b2013-09-23 23:51:32 -07005006 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07005007 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
5008 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07005009 }
5010 }
5011 if (return_type_ == nullptr) {
5012 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
5013 const DexFile::ProtoId& proto_id = dex_file_->GetMethodPrototype(method_id);
5014 uint16_t return_type_idx = proto_id.return_type_idx_;
5015 const char* descriptor = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(return_type_idx));
Mathieu Chartierbf99f772014-08-23 16:37:27 -07005016 return_type_ = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07005017 }
5018 }
5019 return *return_type_;
Ian Rogersad0b3a32012-04-16 14:50:24 -07005020}
5021
Ian Rogersd8f69b02014-09-10 21:43:52 +00005022const RegType& MethodVerifier::GetDeclaringClass() {
Ian Rogers7b078e82014-09-10 14:44:24 -07005023 if (declaring_class_ == nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08005024 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
Brian Carlstrom93c33962013-07-26 10:37:43 -07005025 const char* descriptor
5026 = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(method_id.class_idx_));
Mathieu Chartiere401d142015-04-22 13:56:20 -07005027 if (mirror_method_ != nullptr) {
Ian Rogers637c65b2013-05-31 11:46:00 -07005028 mirror::Class* klass = mirror_method_->GetDeclaringClass();
Mathieu Chartierde40d472015-10-15 17:47:48 -07005029 declaring_class_ = &FromClass(descriptor, klass, klass->CannotBeAssignedFromOtherTypes());
Ian Rogers637c65b2013-05-31 11:46:00 -07005030 } else {
Mathieu Chartierbf99f772014-08-23 16:37:27 -07005031 declaring_class_ = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers637c65b2013-05-31 11:46:00 -07005032 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07005033 }
Ian Rogers637c65b2013-05-31 11:46:00 -07005034 return *declaring_class_;
Ian Rogersad0b3a32012-04-16 14:50:24 -07005035}
5036
Ian Rogers2bcb4a42012-11-08 10:39:18 -08005037std::vector<int32_t> MethodVerifier::DescribeVRegs(uint32_t dex_pc) {
5038 RegisterLine* line = reg_table_.GetLine(dex_pc);
Sebastien Hertzaa0c00c2014-03-14 17:58:54 +01005039 DCHECK(line != nullptr) << "No register line at DEX pc " << StringPrintf("0x%x", dex_pc);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08005040 std::vector<int32_t> result;
5041 for (size_t i = 0; i < line->NumRegs(); ++i) {
Ian Rogers7b078e82014-09-10 14:44:24 -07005042 const RegType& type = line->GetRegisterType(this, i);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08005043 if (type.IsConstant()) {
5044 result.push_back(type.IsPreciseConstant() ? kConstant : kImpreciseConstant);
Ian Rogers7b078e82014-09-10 14:44:24 -07005045 const ConstantType* const_val = down_cast<const ConstantType*>(&type);
5046 result.push_back(const_val->ConstantValue());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08005047 } else if (type.IsConstantLo()) {
5048 result.push_back(type.IsPreciseConstantLo() ? kConstant : kImpreciseConstant);
Ian Rogers7b078e82014-09-10 14:44:24 -07005049 const ConstantType* const_val = down_cast<const ConstantType*>(&type);
5050 result.push_back(const_val->ConstantValueLo());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08005051 } else if (type.IsConstantHi()) {
5052 result.push_back(type.IsPreciseConstantHi() ? kConstant : kImpreciseConstant);
Ian Rogers7b078e82014-09-10 14:44:24 -07005053 const ConstantType* const_val = down_cast<const ConstantType*>(&type);
5054 result.push_back(const_val->ConstantValueHi());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08005055 } else if (type.IsIntegralTypes()) {
5056 result.push_back(kIntVReg);
5057 result.push_back(0);
5058 } else if (type.IsFloat()) {
5059 result.push_back(kFloatVReg);
5060 result.push_back(0);
5061 } else if (type.IsLong()) {
5062 result.push_back(kLongLoVReg);
5063 result.push_back(0);
5064 result.push_back(kLongHiVReg);
5065 result.push_back(0);
5066 ++i;
5067 } else if (type.IsDouble()) {
5068 result.push_back(kDoubleLoVReg);
5069 result.push_back(0);
5070 result.push_back(kDoubleHiVReg);
5071 result.push_back(0);
5072 ++i;
5073 } else if (type.IsUndefined() || type.IsConflict() || type.IsHighHalf()) {
5074 result.push_back(kUndefined);
5075 result.push_back(0);
5076 } else {
Ian Rogers7b3ddd22013-02-21 15:19:52 -08005077 CHECK(type.IsNonZeroReferenceTypes());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08005078 result.push_back(kReferenceVReg);
5079 result.push_back(0);
5080 }
5081 }
5082 return result;
5083}
5084
Ian Rogersd8f69b02014-09-10 21:43:52 +00005085const RegType& MethodVerifier::DetermineCat1Constant(int32_t value, bool precise) {
Sebastien Hertz849600b2013-12-20 10:28:08 +01005086 if (precise) {
5087 // Precise constant type.
5088 return reg_types_.FromCat1Const(value, true);
5089 } else {
5090 // Imprecise constant type.
5091 if (value < -32768) {
5092 return reg_types_.IntConstant();
5093 } else if (value < -128) {
5094 return reg_types_.ShortConstant();
5095 } else if (value < 0) {
5096 return reg_types_.ByteConstant();
5097 } else if (value == 0) {
5098 return reg_types_.Zero();
5099 } else if (value == 1) {
5100 return reg_types_.One();
5101 } else if (value < 128) {
5102 return reg_types_.PosByteConstant();
5103 } else if (value < 32768) {
5104 return reg_types_.PosShortConstant();
5105 } else if (value < 65536) {
5106 return reg_types_.CharConstant();
5107 } else {
5108 return reg_types_.IntConstant();
5109 }
5110 }
5111}
5112
Elliott Hughes0a1038b2012-06-14 16:24:17 -07005113void MethodVerifier::Init() {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08005114 art::verifier::RegTypeCache::Init();
Brian Carlstrome7d856b2012-01-11 18:10:55 -08005115}
5116
Elliott Hughes0a1038b2012-06-14 16:24:17 -07005117void MethodVerifier::Shutdown() {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08005118 verifier::RegTypeCache::ShutDown();
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08005119}
jeffhaod1224c72012-02-29 13:43:08 -08005120
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -07005121void MethodVerifier::VisitStaticRoots(RootVisitor* visitor) {
5122 RegTypeCache::VisitStaticRoots(visitor);
Mathieu Chartier7c438b12014-09-12 17:01:24 -07005123}
5124
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -07005125void MethodVerifier::VisitRoots(RootVisitor* visitor, const RootInfo& root_info) {
5126 reg_types_.VisitRoots(visitor, root_info);
Mathieu Chartierc528dba2013-11-26 12:00:11 -08005127}
5128
Andreas Gampef23f33d2015-06-23 14:18:17 -07005129const RegType& MethodVerifier::FromClass(const char* descriptor,
5130 mirror::Class* klass,
5131 bool precise) {
5132 DCHECK(klass != nullptr);
5133 if (precise && !klass->IsInstantiable() && !klass->IsPrimitive()) {
5134 Fail(VerifyError::VERIFY_ERROR_NO_CLASS) << "Could not create precise reference for "
5135 << "non-instantiable klass " << descriptor;
5136 precise = false;
5137 }
5138 return reg_types_.FromClass(descriptor, klass, precise);
5139}
5140
Ian Rogersd81871c2011-10-03 13:57:23 -07005141} // namespace verifier
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07005142} // namespace art